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++;
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_group_formation_completed(struct wpa_supplicant *wpa_s,
809 struct wpa_ssid *ssid;
810 const char *ssid_txt;
813 u8 go_dev_addr[ETH_ALEN];
817 * This callback is likely called for the main interface. Update wpa_s
818 * to use the group interface if a new interface was created for the
821 if (wpa_s->global->p2p_group_formation)
822 wpa_s = wpa_s->global->p2p_group_formation;
823 if (wpa_s->p2p_go_group_formation_completed) {
824 wpa_s->global->p2p_group_formation = NULL;
825 wpa_s->p2p_in_provisioning = 0;
827 wpa_s->p2p_in_invitation = 0;
830 wpa_msg_global(wpa_s->parent, MSG_INFO,
831 P2P_EVENT_GROUP_FORMATION_FAILURE);
832 wpas_p2p_group_delete(wpa_s,
833 P2P_GROUP_REMOVAL_FORMATION_FAILED);
837 wpa_msg_global(wpa_s->parent, MSG_INFO,
838 P2P_EVENT_GROUP_FORMATION_SUCCESS);
840 ssid = wpa_s->current_ssid;
841 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
842 ssid->mode = WPAS_MODE_P2P_GO;
843 p2p_group_notif_formation_done(wpa_s->p2p_group);
844 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
849 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
850 client = ssid->mode == WPAS_MODE_INFRA;
851 if (ssid->mode == WPAS_MODE_P2P_GO) {
852 persistent = ssid->p2p_persistent_group;
853 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
856 persistent = wpas_p2p_persistent_group(wpa_s,
862 client = wpa_s->p2p_group_interface ==
863 P2P_GROUP_INTERFACE_CLIENT;
864 os_memset(go_dev_addr, 0, ETH_ALEN);
867 wpa_s->show_group_started = 0;
870 * Indicate event only after successfully completed 4-way
871 * handshake, i.e., when the interface is ready for data
874 wpa_s->show_group_started = 1;
875 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
877 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
878 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
879 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
881 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
882 MAC2STR(go_dev_addr),
883 persistent ? " [PERSISTENT]" : "");
884 wpas_p2p_cross_connect_setup(wpa_s);
885 wpas_p2p_set_group_idle_timeout(wpa_s);
887 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
888 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
889 "go_dev_addr=" MACSTR "%s",
890 wpa_s->ifname, ssid_txt,
891 ssid ? ssid->frequency : 0,
892 ssid && ssid->passphrase ? ssid->passphrase : "",
893 MAC2STR(go_dev_addr),
894 persistent ? " [PERSISTENT]" : "");
895 wpas_p2p_cross_connect_setup(wpa_s);
896 wpas_p2p_set_group_idle_timeout(wpa_s);
900 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
903 os_free(wpa_s->global->add_psk);
904 wpa_s->global->add_psk = NULL;
906 if (network_id < 0 && ssid)
907 network_id = ssid->id;
909 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
910 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
915 struct send_action_work {
921 unsigned int wait_time;
926 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
929 struct wpa_supplicant *wpa_s = eloop_ctx;
931 if (!wpa_s->p2p_send_action_work)
934 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
935 os_free(wpa_s->p2p_send_action_work->ctx);
936 radio_work_done(wpa_s->p2p_send_action_work);
937 wpa_s->p2p_send_action_work = NULL;
941 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
943 const u8 *dst, const u8 *src,
945 const u8 *data, size_t data_len,
946 enum offchannel_send_action_result
949 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
951 if (wpa_s->p2p_send_action_work) {
952 struct send_action_work *awork;
953 awork = wpa_s->p2p_send_action_work->ctx;
954 if (awork->wait_time == 0) {
956 radio_work_done(wpa_s->p2p_send_action_work);
957 wpa_s->p2p_send_action_work = NULL;
960 * In theory, this should not be needed, but number of
961 * places in the P2P code is still using non-zero wait
962 * time for the last Action frame in the sequence and
963 * some of these do not call send_action_done().
965 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
967 eloop_register_timeout(
968 0, awork->wait_time * 1000,
969 wpas_p2p_send_action_work_timeout,
974 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
978 case OFFCHANNEL_SEND_ACTION_SUCCESS:
979 res = P2P_SEND_ACTION_SUCCESS;
981 case OFFCHANNEL_SEND_ACTION_NO_ACK:
982 res = P2P_SEND_ACTION_NO_ACK;
984 case OFFCHANNEL_SEND_ACTION_FAILED:
985 res = P2P_SEND_ACTION_FAILED;
989 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
991 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
992 wpa_s->pending_pd_before_join &&
993 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
994 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
995 wpa_s->p2p_fallback_to_go_neg) {
996 wpa_s->pending_pd_before_join = 0;
997 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
998 "during p2p_connect-auto");
999 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1005 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1007 struct wpa_supplicant *wpa_s = work->wpa_s;
1008 struct send_action_work *awork = work->ctx;
1011 if (work->started) {
1012 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1014 wpa_s->p2p_send_action_work = NULL;
1015 offchannel_send_action_done(wpa_s);
1021 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1022 awork->bssid, awork->buf, awork->len,
1024 wpas_p2p_send_action_tx_status, 1) < 0) {
1026 radio_work_done(work);
1029 wpa_s->p2p_send_action_work = work;
1033 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1034 unsigned int freq, const u8 *dst,
1035 const u8 *src, const u8 *bssid, const u8 *buf,
1036 size_t len, unsigned int wait_time)
1038 struct send_action_work *awork;
1040 if (wpa_s->p2p_send_action_work) {
1041 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1045 awork = os_zalloc(sizeof(*awork) + len);
1050 os_memcpy(awork->dst, dst, ETH_ALEN);
1051 os_memcpy(awork->src, src, ETH_ALEN);
1052 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1054 awork->wait_time = wait_time;
1055 os_memcpy(awork->buf, buf, len);
1057 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1058 wpas_send_action_cb, awork) < 0) {
1067 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1068 const u8 *src, const u8 *bssid, const u8 *buf,
1069 size_t len, unsigned int wait_time)
1071 struct wpa_supplicant *wpa_s = ctx;
1072 int listen_freq = -1, send_freq = -1;
1074 if (wpa_s->p2p_listen_work)
1075 listen_freq = wpa_s->p2p_listen_work->freq;
1076 if (wpa_s->p2p_send_action_work)
1077 send_freq = wpa_s->p2p_send_action_work->freq;
1078 if (listen_freq != (int) freq && send_freq != (int) freq) {
1079 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1080 listen_freq, send_freq);
1081 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1085 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1086 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1088 wpas_p2p_send_action_tx_status, 1);
1092 static void wpas_send_action_done(void *ctx)
1094 struct wpa_supplicant *wpa_s = ctx;
1096 if (wpa_s->p2p_send_action_work) {
1097 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1099 os_free(wpa_s->p2p_send_action_work->ctx);
1100 radio_work_done(wpa_s->p2p_send_action_work);
1101 wpa_s->p2p_send_action_work = NULL;
1104 offchannel_send_action_done(wpa_s);
1108 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1109 struct p2p_go_neg_results *params)
1111 if (wpa_s->go_params == NULL) {
1112 wpa_s->go_params = os_malloc(sizeof(*params));
1113 if (wpa_s->go_params == NULL)
1116 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1121 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1122 struct p2p_go_neg_results *res)
1124 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1125 " dev_addr " MACSTR " wps_method %d",
1126 MAC2STR(res->peer_interface_addr),
1127 MAC2STR(res->peer_device_addr), res->wps_method);
1128 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1129 res->ssid, res->ssid_len);
1130 wpa_supplicant_ap_deinit(wpa_s);
1131 wpas_copy_go_neg_results(wpa_s, res);
1132 if (res->wps_method == WPS_PBC) {
1133 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1134 #ifdef CONFIG_WPS_NFC
1135 } else if (res->wps_method == WPS_NFC) {
1136 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1137 res->peer_interface_addr,
1138 wpa_s->parent->p2p_oob_dev_pw,
1139 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1140 wpa_s->parent->p2p_oob_dev_pw_id ==
1141 DEV_PW_NFC_CONNECTION_HANDOVER ?
1142 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1145 #endif /* CONFIG_WPS_NFC */
1147 u16 dev_pw_id = DEV_PW_DEFAULT;
1148 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1149 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1150 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1151 wpa_s->p2p_pin, 1, dev_pw_id);
1156 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1157 struct wpa_ssid *ssid)
1159 struct wpa_ssid *persistent;
1160 struct psk_list_entry *psk;
1161 struct hostapd_data *hapd;
1163 if (!wpa_s->ap_iface)
1166 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1168 if (persistent == NULL)
1171 hapd = wpa_s->ap_iface->bss[0];
1173 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1175 struct hostapd_wpa_psk *hpsk;
1177 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1179 MAC2STR(psk->addr), psk->p2p);
1180 hpsk = os_zalloc(sizeof(*hpsk));
1183 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1185 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1187 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1188 hpsk->next = hapd->conf->ssid.wpa_psk;
1189 hapd->conf->ssid.wpa_psk = hpsk;
1194 static void p2p_go_configured(void *ctx, void *data)
1196 struct wpa_supplicant *wpa_s = ctx;
1197 struct p2p_go_neg_results *params = data;
1198 struct wpa_ssid *ssid;
1199 int network_id = -1;
1201 ssid = wpa_s->current_ssid;
1202 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1203 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1204 if (wpa_s->global->p2p_group_formation == wpa_s)
1205 wpa_s->global->p2p_group_formation = NULL;
1206 if (os_strlen(params->passphrase) > 0) {
1207 wpa_msg_global(wpa_s->parent, MSG_INFO,
1208 P2P_EVENT_GROUP_STARTED
1209 "%s GO ssid=\"%s\" freq=%d "
1210 "passphrase=\"%s\" go_dev_addr=" MACSTR
1211 "%s", wpa_s->ifname,
1212 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1213 ssid->frequency, params->passphrase,
1214 MAC2STR(wpa_s->global->p2p_dev_addr),
1215 params->persistent_group ?
1216 " [PERSISTENT]" : "");
1219 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1220 sizeof(params->psk));
1221 wpa_msg_global(wpa_s->parent, MSG_INFO,
1222 P2P_EVENT_GROUP_STARTED
1223 "%s GO ssid=\"%s\" freq=%d psk=%s "
1224 "go_dev_addr=" MACSTR "%s",
1226 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1227 ssid->frequency, psk,
1228 MAC2STR(wpa_s->global->p2p_dev_addr),
1229 params->persistent_group ?
1230 " [PERSISTENT]" : "");
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",
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 : "");
1705 os_free(wfd_dev_info_hex);
1706 #endif /* CONFIG_NO_STDOUT_DEBUG */
1708 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1712 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1714 struct wpa_supplicant *wpa_s = ctx;
1716 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1717 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1719 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1723 static void wpas_find_stopped(void *ctx)
1725 struct wpa_supplicant *wpa_s = ctx;
1726 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1730 struct wpas_p2p_listen_work {
1732 unsigned int duration;
1733 struct wpabuf *probe_resp_ie;
1737 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1741 wpabuf_free(lwork->probe_resp_ie);
1746 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1748 struct wpas_p2p_listen_work *lwork;
1750 if (!wpa_s->p2p_listen_work)
1753 lwork = wpa_s->p2p_listen_work->ctx;
1754 wpas_p2p_listen_work_free(lwork);
1755 radio_work_done(wpa_s->p2p_listen_work);
1756 wpa_s->p2p_listen_work = NULL;
1760 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1762 struct wpa_supplicant *wpa_s = work->wpa_s;
1763 struct wpas_p2p_listen_work *lwork = work->ctx;
1766 if (work->started) {
1767 wpa_s->p2p_listen_work = NULL;
1768 wpas_stop_listen(wpa_s);
1770 wpas_p2p_listen_work_free(lwork);
1774 wpa_s->p2p_listen_work = work;
1776 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1778 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1779 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1780 "report received Probe Request frames");
1781 wpas_p2p_listen_work_done(wpa_s);
1785 wpa_s->pending_listen_freq = lwork->freq;
1786 wpa_s->pending_listen_duration = lwork->duration;
1788 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1790 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1791 "to remain on channel (%u MHz) for Listen "
1792 "state", lwork->freq);
1793 wpas_p2p_listen_work_done(wpa_s);
1794 wpa_s->pending_listen_freq = 0;
1797 wpa_s->off_channel_freq = 0;
1798 wpa_s->roc_waiting_drv_freq = lwork->freq;
1802 static int wpas_start_listen(void *ctx, unsigned int freq,
1803 unsigned int duration,
1804 const struct wpabuf *probe_resp_ie)
1806 struct wpa_supplicant *wpa_s = ctx;
1807 struct wpas_p2p_listen_work *lwork;
1809 if (wpa_s->p2p_listen_work) {
1810 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
1814 lwork = os_zalloc(sizeof(*lwork));
1818 lwork->duration = duration;
1819 if (probe_resp_ie) {
1820 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1821 if (lwork->probe_resp_ie == NULL) {
1822 wpas_p2p_listen_work_free(lwork);
1827 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1829 wpas_p2p_listen_work_free(lwork);
1837 static void wpas_stop_listen(void *ctx)
1839 struct wpa_supplicant *wpa_s = ctx;
1840 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1841 wpa_drv_cancel_remain_on_channel(wpa_s);
1842 wpa_s->off_channel_freq = 0;
1843 wpa_s->roc_waiting_drv_freq = 0;
1845 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1846 wpa_drv_probe_req_report(wpa_s, 0);
1847 wpas_p2p_listen_work_done(wpa_s);
1851 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1853 struct wpa_supplicant *wpa_s = ctx;
1854 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1859 * DNS Header section is used only to calculate compression pointers, so the
1860 * contents of this data does not matter, but the length needs to be reserved
1861 * in the virtual packet.
1863 #define DNS_HEADER_LEN 12
1866 * 27-octet in-memory packet from P2P specification containing two implied
1867 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1869 #define P2P_SD_IN_MEMORY_LEN 27
1871 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1872 u8 **spos, const u8 *end)
1874 while (*spos < end) {
1875 u8 val = ((*spos)[0] & 0xc0) >> 6;
1878 if (val == 1 || val == 2) {
1879 /* These are reserved values in RFC 1035 */
1880 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1881 "sequence starting with 0x%x", val);
1890 if (*spos + 2 > end) {
1891 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1892 "DNS offset field");
1896 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1897 if (offset >= *spos - start) {
1898 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1899 "pointer offset %u", offset);
1904 spos_tmp = start + offset;
1905 return p2p_sd_dns_uncompress_label(upos, uend, start,
1911 len = (*spos)[0] & 0x3f;
1916 if (*spos + len > end) {
1917 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1918 "sequence - no room for label with length "
1923 if (*upos + len + 2 > uend)
1926 os_memcpy(*upos, *spos, len);
1938 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1939 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1940 * not large enough */
1941 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1942 size_t msg_len, size_t offset)
1944 /* 27-octet in-memory packet from P2P specification */
1945 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1946 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1947 u8 *tmp, *end, *spos;
1953 if (offset > msg_len)
1956 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1959 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1960 end = spos + msg_len;
1963 os_memset(tmp, 0, DNS_HEADER_LEN);
1964 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1965 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1968 uend = buf + buf_len;
1970 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1979 } else if (upos[-1] == '.')
1987 static struct p2p_srv_bonjour *
1988 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1989 const struct wpabuf *query)
1991 struct p2p_srv_bonjour *bsrv;
1994 len = wpabuf_len(query);
1995 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1996 struct p2p_srv_bonjour, list) {
1997 if (len == wpabuf_len(bsrv->query) &&
1998 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2006 static struct p2p_srv_upnp *
2007 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2008 const char *service)
2010 struct p2p_srv_upnp *usrv;
2012 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2013 struct p2p_srv_upnp, list) {
2014 if (version == usrv->version &&
2015 os_strcmp(service, usrv->service) == 0)
2022 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2027 if (wpabuf_tailroom(resp) < 5)
2030 /* Length (to be filled) */
2031 len_pos = wpabuf_put(resp, 2);
2032 wpabuf_put_u8(resp, srv_proto);
2033 wpabuf_put_u8(resp, srv_trans_id);
2035 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2036 /* Response Data: empty */
2037 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2041 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2042 struct wpabuf *resp, u8 srv_trans_id)
2044 struct p2p_srv_bonjour *bsrv;
2047 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2049 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2050 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2054 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2055 struct p2p_srv_bonjour, list) {
2056 if (wpabuf_tailroom(resp) <
2057 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2059 /* Length (to be filled) */
2060 len_pos = wpabuf_put(resp, 2);
2061 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2062 wpabuf_put_u8(resp, srv_trans_id);
2064 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2065 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2066 wpabuf_head(bsrv->resp),
2067 wpabuf_len(bsrv->resp));
2069 wpabuf_put_buf(resp, bsrv->query); /* Key */
2070 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2071 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2077 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2080 char str_rx[256], str_srv[256];
2082 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2083 return 0; /* Too short to include DNS Type and Version */
2084 if (os_memcmp(query + query_len - 3,
2085 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2087 return 0; /* Mismatch in DNS Type or Version */
2088 if (query_len == wpabuf_len(bsrv->query) &&
2089 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2090 return 1; /* Binary match */
2092 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2094 return 0; /* Failed to uncompress query */
2095 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2096 wpabuf_head(bsrv->query),
2097 wpabuf_len(bsrv->query) - 3, 0))
2098 return 0; /* Failed to uncompress service */
2100 return os_strcmp(str_rx, str_srv) == 0;
2104 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2105 struct wpabuf *resp, u8 srv_trans_id,
2106 const u8 *query, size_t query_len)
2108 struct p2p_srv_bonjour *bsrv;
2112 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2114 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2115 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2116 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2121 if (query_len == 0) {
2122 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2126 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2127 struct p2p_srv_bonjour, list) {
2128 if (!match_bonjour_query(bsrv, query, query_len))
2131 if (wpabuf_tailroom(resp) <
2132 5 + query_len + wpabuf_len(bsrv->resp))
2137 /* Length (to be filled) */
2138 len_pos = wpabuf_put(resp, 2);
2139 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2140 wpabuf_put_u8(resp, srv_trans_id);
2143 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2144 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2145 wpabuf_head(bsrv->resp),
2146 wpabuf_len(bsrv->resp));
2149 wpabuf_put_data(resp, query, query_len); /* Key */
2150 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2152 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2156 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2158 if (wpabuf_tailroom(resp) < 5)
2161 /* Length (to be filled) */
2162 len_pos = wpabuf_put(resp, 2);
2163 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2164 wpabuf_put_u8(resp, srv_trans_id);
2167 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2168 /* Response Data: empty */
2169 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2175 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2176 struct wpabuf *resp, u8 srv_trans_id)
2178 struct p2p_srv_upnp *usrv;
2181 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2183 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2184 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2188 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2189 struct p2p_srv_upnp, list) {
2190 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2193 /* Length (to be filled) */
2194 len_pos = wpabuf_put(resp, 2);
2195 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2196 wpabuf_put_u8(resp, srv_trans_id);
2199 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2201 wpabuf_put_u8(resp, usrv->version);
2202 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2204 wpabuf_put_str(resp, usrv->service);
2205 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2211 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2212 struct wpabuf *resp, u8 srv_trans_id,
2213 const u8 *query, size_t query_len)
2215 struct p2p_srv_upnp *usrv;
2221 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2224 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2225 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2226 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2231 if (query_len == 0) {
2232 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2236 if (wpabuf_tailroom(resp) < 5)
2239 /* Length (to be filled) */
2240 len_pos = wpabuf_put(resp, 2);
2241 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2242 wpabuf_put_u8(resp, srv_trans_id);
2245 str = os_malloc(query_len);
2248 os_memcpy(str, query + 1, query_len - 1);
2249 str[query_len - 1] = '\0';
2251 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2252 struct p2p_srv_upnp, list) {
2253 if (version != usrv->version)
2256 if (os_strcmp(str, "ssdp:all") != 0 &&
2257 os_strstr(usrv->service, str) == NULL)
2260 if (wpabuf_tailroom(resp) < 2)
2264 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2266 wpabuf_put_u8(resp, version);
2268 wpabuf_put_u8(resp, ',');
2272 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2274 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2276 wpabuf_put_str(resp, usrv->service);
2281 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2284 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2285 /* Response Data: empty */
2288 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2292 #ifdef CONFIG_WIFI_DISPLAY
2293 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2294 struct wpabuf *resp, u8 srv_trans_id,
2295 const u8 *query, size_t query_len)
2301 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2303 if (!wpa_s->global->wifi_display) {
2304 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2305 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2310 if (query_len < 1) {
2311 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2316 if (wpabuf_tailroom(resp) < 5)
2321 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2323 /* TODO: role specific handling */
2325 /* Length (to be filled) */
2326 len_pos = wpabuf_put(resp, 2);
2327 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2328 wpabuf_put_u8(resp, srv_trans_id);
2329 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2331 while (pos < query + query_len) {
2332 if (*pos < MAX_WFD_SUBELEMS &&
2333 wpa_s->global->wfd_subelem[*pos] &&
2334 wpabuf_tailroom(resp) >=
2335 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2336 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2337 "subelement %u", *pos);
2338 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2343 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2345 #endif /* CONFIG_WIFI_DISPLAY */
2348 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2349 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2351 struct wpa_supplicant *wpa_s = ctx;
2352 const u8 *pos = tlvs;
2353 const u8 *end = tlvs + tlvs_len;
2356 struct wpabuf *resp;
2357 u8 srv_proto, srv_trans_id;
2361 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2363 buf_len = 2 * tlvs_len + 1;
2364 buf = os_malloc(buf_len);
2366 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2367 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2369 freq, MAC2STR(sa), dialog_token, update_indic,
2374 if (wpa_s->p2p_sd_over_ctrl_iface) {
2375 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2376 update_indic, tlvs, tlvs_len);
2377 return; /* to be processed by an external program */
2380 resp = wpabuf_alloc(10000);
2384 while (pos + 1 < end) {
2385 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2386 slen = WPA_GET_LE16(pos);
2388 if (pos + slen > end || slen < 2) {
2389 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2394 tlv_end = pos + slen;
2397 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2399 srv_trans_id = *pos++;
2400 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2403 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2404 pos, tlv_end - pos);
2407 if (wpa_s->force_long_sd) {
2408 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2410 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2411 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2415 switch (srv_proto) {
2416 case P2P_SERV_ALL_SERVICES:
2417 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2418 "for all services");
2419 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2420 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2421 wpa_printf(MSG_DEBUG, "P2P: No service "
2422 "discovery protocols available");
2423 wpas_sd_add_proto_not_avail(
2424 resp, P2P_SERV_ALL_SERVICES,
2428 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2429 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2431 case P2P_SERV_BONJOUR:
2432 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2433 pos, tlv_end - pos);
2436 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2437 pos, tlv_end - pos);
2439 #ifdef CONFIG_WIFI_DISPLAY
2440 case P2P_SERV_WIFI_DISPLAY:
2441 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2442 pos, tlv_end - pos);
2444 #endif /* CONFIG_WIFI_DISPLAY */
2446 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2447 "protocol %u", srv_proto);
2448 wpas_sd_add_proto_not_avail(resp, srv_proto,
2457 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2458 update_indic, tlvs, tlvs_len);
2460 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2466 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2467 const u8 *tlvs, size_t tlvs_len)
2469 struct wpa_supplicant *wpa_s = ctx;
2470 const u8 *pos = tlvs;
2471 const u8 *end = tlvs + tlvs_len;
2477 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2479 if (tlvs_len > 1500) {
2480 /* TODO: better way for handling this */
2481 wpa_msg_ctrl(wpa_s, MSG_INFO,
2482 P2P_EVENT_SERV_DISC_RESP MACSTR
2483 " %u <long response: %u bytes>",
2484 MAC2STR(sa), update_indic,
2485 (unsigned int) tlvs_len);
2487 buf_len = 2 * tlvs_len + 1;
2488 buf = os_malloc(buf_len);
2490 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2491 wpa_msg_ctrl(wpa_s, MSG_INFO,
2492 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2493 MAC2STR(sa), update_indic, buf);
2499 u8 srv_proto, srv_trans_id, status;
2501 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2502 slen = WPA_GET_LE16(pos);
2504 if (pos + slen > end || slen < 3) {
2505 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2509 tlv_end = pos + slen;
2512 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2514 srv_trans_id = *pos++;
2515 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2518 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2521 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2522 pos, tlv_end - pos);
2527 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2531 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2532 const struct wpabuf *tlvs)
2534 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2536 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2540 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2541 u8 version, const char *query)
2543 struct wpabuf *tlvs;
2546 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2549 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2550 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2551 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2552 wpabuf_put_u8(tlvs, version);
2553 wpabuf_put_str(tlvs, query);
2554 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2560 #ifdef CONFIG_WIFI_DISPLAY
2562 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2563 const struct wpabuf *tlvs)
2565 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2567 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2571 #define MAX_WFD_SD_SUBELEMS 20
2573 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2574 const char *subelems)
2581 len = wpabuf_put(tlvs, 2);
2582 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2583 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2585 wpabuf_put_u8(tlvs, role);
2590 if (val >= 0 && val < 256) {
2591 wpabuf_put_u8(tlvs, val);
2593 if (count == MAX_WFD_SD_SUBELEMS)
2596 pos = os_strchr(pos + 1, ',');
2602 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2606 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2607 const u8 *dst, const char *role)
2609 struct wpabuf *tlvs;
2611 const char *subelems;
2614 subelems = os_strchr(role, ' ');
2615 if (subelems == NULL)
2619 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2623 if (os_strstr(role, "[source]"))
2624 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2625 if (os_strstr(role, "[pri-sink]"))
2626 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2627 if (os_strstr(role, "[sec-sink]"))
2628 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2629 if (os_strstr(role, "[source+sink]"))
2630 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2632 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2637 #endif /* CONFIG_WIFI_DISPLAY */
2640 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2642 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2644 return p2p_sd_cancel_request(wpa_s->global->p2p,
2645 (void *) (uintptr_t) req);
2649 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2650 const u8 *dst, u8 dialog_token,
2651 const struct wpabuf *resp_tlvs)
2653 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2655 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2660 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2662 if (wpa_s->global->p2p)
2663 p2p_sd_service_update(wpa_s->global->p2p);
2667 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2669 dl_list_del(&bsrv->list);
2670 wpabuf_free(bsrv->query);
2671 wpabuf_free(bsrv->resp);
2676 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2678 dl_list_del(&usrv->list);
2679 os_free(usrv->service);
2684 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2686 struct p2p_srv_bonjour *bsrv, *bn;
2687 struct p2p_srv_upnp *usrv, *un;
2689 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2690 struct p2p_srv_bonjour, list)
2691 wpas_p2p_srv_bonjour_free(bsrv);
2693 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2694 struct p2p_srv_upnp, list)
2695 wpas_p2p_srv_upnp_free(usrv);
2697 wpas_p2p_sd_service_update(wpa_s);
2701 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2702 struct wpabuf *query, struct wpabuf *resp)
2704 struct p2p_srv_bonjour *bsrv;
2706 bsrv = os_zalloc(sizeof(*bsrv));
2709 bsrv->query = query;
2711 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2713 wpas_p2p_sd_service_update(wpa_s);
2718 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2719 const struct wpabuf *query)
2721 struct p2p_srv_bonjour *bsrv;
2723 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2726 wpas_p2p_srv_bonjour_free(bsrv);
2727 wpas_p2p_sd_service_update(wpa_s);
2732 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2733 const char *service)
2735 struct p2p_srv_upnp *usrv;
2737 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2738 return 0; /* Already listed */
2739 usrv = os_zalloc(sizeof(*usrv));
2742 usrv->version = version;
2743 usrv->service = os_strdup(service);
2744 if (usrv->service == NULL) {
2748 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2750 wpas_p2p_sd_service_update(wpa_s);
2755 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2756 const char *service)
2758 struct p2p_srv_upnp *usrv;
2760 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2763 wpas_p2p_srv_upnp_free(usrv);
2764 wpas_p2p_sd_service_update(wpa_s);
2769 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2770 const u8 *peer, const char *params,
2771 unsigned int generated_pin)
2773 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2774 " %08d%s", MAC2STR(peer), generated_pin, params);
2778 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2779 const u8 *peer, const char *params)
2781 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2782 "%s", MAC2STR(peer), params);
2786 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2787 const u8 *dev_addr, const u8 *pri_dev_type,
2788 const char *dev_name, u16 supp_config_methods,
2789 u8 dev_capab, u8 group_capab, const u8 *group_id,
2790 size_t group_id_len)
2792 struct wpa_supplicant *wpa_s = ctx;
2793 char devtype[WPS_DEV_TYPE_BUFSIZE];
2795 u8 empty_dev_type[8];
2796 unsigned int generated_pin = 0;
2797 struct wpa_supplicant *group = NULL;
2800 for (group = wpa_s->global->ifaces; group; group = group->next)
2802 struct wpa_ssid *s = group->current_ssid;
2804 s->mode == WPAS_MODE_P2P_GO &&
2805 group_id_len - ETH_ALEN == s->ssid_len &&
2806 os_memcmp(group_id + ETH_ALEN, s->ssid,
2812 if (pri_dev_type == NULL) {
2813 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2814 pri_dev_type = empty_dev_type;
2816 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2817 " pri_dev_type=%s name='%s' config_methods=0x%x "
2818 "dev_capab=0x%x group_capab=0x%x%s%s",
2820 wps_dev_type_bin2str(pri_dev_type, devtype,
2822 dev_name, supp_config_methods, dev_capab, group_capab,
2823 group ? " group=" : "",
2824 group ? group->ifname : "");
2825 params[sizeof(params) - 1] = '\0';
2827 if (config_methods & WPS_CONFIG_DISPLAY) {
2828 generated_pin = wps_generate_pin();
2829 wpas_prov_disc_local_display(wpa_s, peer, params,
2831 } else if (config_methods & WPS_CONFIG_KEYPAD)
2832 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2833 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2834 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2835 MACSTR "%s", MAC2STR(peer), params);
2837 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2838 P2P_PROV_DISC_SUCCESS,
2839 config_methods, generated_pin);
2843 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2845 struct wpa_supplicant *wpa_s = ctx;
2846 unsigned int generated_pin = 0;
2849 if (wpa_s->pending_pd_before_join &&
2850 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2851 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2852 wpa_s->pending_pd_before_join = 0;
2853 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2854 "join-existing-group operation");
2855 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2859 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2860 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2861 os_snprintf(params, sizeof(params), " peer_go=%d",
2862 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2866 if (config_methods & WPS_CONFIG_DISPLAY)
2867 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2868 else if (config_methods & WPS_CONFIG_KEYPAD) {
2869 generated_pin = wps_generate_pin();
2870 wpas_prov_disc_local_display(wpa_s, peer, params,
2872 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2873 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2874 MACSTR "%s", MAC2STR(peer), params);
2876 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2877 P2P_PROV_DISC_SUCCESS,
2878 config_methods, generated_pin);
2882 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2883 enum p2p_prov_disc_status status)
2885 struct wpa_supplicant *wpa_s = ctx;
2887 if (wpa_s->p2p_fallback_to_go_neg) {
2888 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2889 "failed - fall back to GO Negotiation");
2890 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2894 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2895 wpa_s->pending_pd_before_join = 0;
2896 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2897 "join-existing-group operation (no ACK for PD "
2899 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2903 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2904 " p2p_dev_addr=" MACSTR " status=%d",
2905 MAC2STR(peer), status);
2907 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2912 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2914 if (channels == NULL)
2915 return 1; /* Assume no restrictions */
2916 return p2p_channels_includes_freq(channels, freq);
2922 * Pick the best frequency to use from all the currently used frequencies.
2924 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2925 struct wpa_used_freq_data *freqs,
2930 /* find a candidate freq that is supported by P2P */
2931 for (c = 0; c < num; c++)
2932 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2938 /* once we have a candidate, try to find a 'better' one */
2939 for (i = c + 1; i < num; i++) {
2940 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2944 * 1. Infrastructure station interfaces have higher preference.
2945 * 2. P2P Clients have higher preference.
2948 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2953 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2956 return freqs[c].freq;
2960 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2961 const u8 *go_dev_addr, const u8 *ssid,
2962 size_t ssid_len, int *go, u8 *group_bssid,
2963 int *force_freq, int persistent_group,
2964 const struct p2p_channels *channels,
2967 struct wpa_supplicant *wpa_s = ctx;
2969 struct wpa_used_freq_data *freqs;
2970 struct wpa_supplicant *grp;
2973 if (!persistent_group) {
2974 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2975 " to join an active group (SSID: %s)",
2976 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2977 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2978 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2980 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2981 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2982 "authorized invitation");
2986 #ifdef CONFIG_WPS_NFC
2987 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2988 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2989 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2990 wpa_s->parent->p2p_wps_method = WPS_NFC;
2991 wpa_s->parent->pending_join_wps_method = WPS_NFC;
2992 os_memcpy(wpa_s->parent->pending_join_dev_addr,
2993 go_dev_addr, ETH_ALEN);
2994 os_memcpy(wpa_s->parent->pending_join_iface_addr,
2998 #endif /* CONFIG_WPS_NFC */
3001 * Do not accept the invitation automatically; notify user and
3004 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3007 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3009 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3010 "running persistent group");
3012 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3016 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3017 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3018 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3019 "invitation to re-invoke a persistent group");
3020 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3021 } else if (!wpa_s->conf->persistent_reconnect)
3022 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3024 for (s = wpa_s->conf->ssid; s; s = s->next) {
3025 if (s->disabled == 2 &&
3026 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3027 s->ssid_len == ssid_len &&
3028 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3033 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3034 " requested reinvocation of an unknown group",
3036 return P2P_SC_FAIL_UNKNOWN_GROUP;
3039 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3041 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3042 wpa_printf(MSG_DEBUG, "P2P: The only available "
3043 "interface is already in use - reject "
3045 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3047 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3048 } else if (s->mode == WPAS_MODE_P2P_GO) {
3050 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3052 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3053 "interface address for the group");
3054 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3056 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3061 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3064 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3065 sizeof(struct wpa_used_freq_data));
3067 int num_channels = wpa_s->num_multichan_concurrent;
3068 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3069 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3073 /* Get one of the frequencies currently in use */
3074 if (best_freq > 0) {
3075 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3076 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3078 if (wpa_s->num_multichan_concurrent < 2 ||
3079 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3080 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3081 *force_freq = best_freq;
3085 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3086 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3088 /* We are the client */
3089 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3090 "running a GO but we are capable of MCC, "
3091 "figure out the best channel to use");
3093 } else if (!freq_included(channels, *force_freq)) {
3094 /* We are the GO, and *force_freq is not in the
3096 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3097 "in intersection but we are capable of MCC, "
3098 "figure out the best channel to use",
3104 return P2P_SC_SUCCESS;
3108 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3109 const u8 *ssid, size_t ssid_len,
3110 const u8 *go_dev_addr, u8 status,
3113 struct wpa_supplicant *wpa_s = ctx;
3116 for (s = wpa_s->conf->ssid; s; s = s->next) {
3117 if (s->disabled == 2 &&
3118 s->ssid_len == ssid_len &&
3119 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3123 if (status == P2P_SC_SUCCESS) {
3124 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3125 " was accepted; op_freq=%d MHz, SSID=%s",
3126 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3128 int go = s->mode == WPAS_MODE_P2P_GO;
3129 wpas_p2p_group_add_persistent(
3130 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
3131 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3133 wpa_s->user_initiated_pd = 0;
3134 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3135 wpa_s->p2p_wps_method, 0, op_freq,
3141 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3142 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3143 " was rejected (status %u)", MAC2STR(sa), status);
3149 wpa_msg_global(wpa_s, MSG_INFO,
3150 P2P_EVENT_INVITATION_RECEIVED
3151 "sa=" MACSTR " go_dev_addr=" MACSTR
3152 " bssid=" MACSTR " unknown-network",
3153 MAC2STR(sa), MAC2STR(go_dev_addr),
3156 wpa_msg_global(wpa_s, MSG_INFO,
3157 P2P_EVENT_INVITATION_RECEIVED
3158 "sa=" MACSTR " go_dev_addr=" MACSTR
3160 MAC2STR(sa), MAC2STR(go_dev_addr));
3165 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3166 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3167 "sa=" MACSTR " persistent=%d freq=%d",
3168 MAC2STR(sa), s->id, op_freq);
3170 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3171 "sa=" MACSTR " persistent=%d",
3172 MAC2STR(sa), s->id);
3177 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3178 struct wpa_ssid *ssid,
3179 const u8 *peer, int inv)
3186 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3187 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3191 if (i >= ssid->num_p2p_clients) {
3192 if (ssid->mode != WPAS_MODE_P2P_GO &&
3193 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3194 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3195 "due to invitation result", ssid->id);
3196 wpas_notify_network_removed(wpa_s, ssid);
3197 wpa_config_remove_network(wpa_s->conf, ssid->id);
3200 return; /* Peer not found in client list */
3203 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3204 "group %d client list%s",
3205 MAC2STR(peer), ssid->id,
3206 inv ? " due to invitation result" : "");
3207 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3208 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3209 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3210 ssid->num_p2p_clients--;
3211 if (wpa_s->parent->conf->update_config &&
3212 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3213 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3217 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3220 struct wpa_ssid *ssid;
3222 wpa_s = wpa_s->global->p2p_invite_group;
3224 return; /* No known invitation group */
3225 ssid = wpa_s->current_ssid;
3226 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3227 !ssid->p2p_persistent_group)
3228 return; /* Not operating as a GO in persistent group */
3229 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3230 ssid->ssid, ssid->ssid_len);
3231 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3235 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3236 const struct p2p_channels *channels,
3237 const u8 *peer, int neg_freq,
3240 struct wpa_supplicant *wpa_s = ctx;
3241 struct wpa_ssid *ssid;
3245 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3246 "status=%d " MACSTR,
3247 status, MAC2STR(bssid));
3249 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3250 "status=%d ", status);
3252 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3254 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3255 status, MAC2STR(peer));
3256 if (wpa_s->pending_invite_ssid_id == -1) {
3257 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3258 wpas_remove_persistent_client(wpa_s, peer);
3259 return; /* Invitation to active group */
3262 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3263 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3264 "invitation exchange to indicate readiness for "
3268 if (status != P2P_SC_SUCCESS) {
3269 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3270 ssid = wpa_config_get_network(
3271 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3272 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3274 wpas_p2p_remove_pending_group_interface(wpa_s);
3278 ssid = wpa_config_get_network(wpa_s->conf,
3279 wpa_s->pending_invite_ssid_id);
3281 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3282 "data matching with invitation");
3287 * The peer could have missed our ctrl::ack frame for Invitation
3288 * Response and continue retransmitting the frame. To reduce the
3289 * likelihood of the peer not getting successful TX status for the
3290 * Invitation Response frame, wait a short time here before starting
3291 * the persistent group so that we will remain on the current channel to
3292 * acknowledge any possible retransmission from the peer.
3294 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3295 "starting persistent group");
3298 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3299 freq_included(channels, neg_freq))
3301 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3302 freq_included(channels, peer_oper_freq))
3303 freq = peer_oper_freq;
3307 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3308 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3309 wpas_p2p_group_add_persistent(wpa_s, ssid,
3310 ssid->mode == WPAS_MODE_P2P_GO,
3311 wpa_s->p2p_persistent_go_freq,
3313 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3315 ssid->mode == WPAS_MODE_P2P_GO ?
3316 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3321 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3324 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3326 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3330 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3332 reg->channel[reg->channels] = chan;
3337 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3338 struct p2p_channels *chan,
3339 struct p2p_channels *cli_chan)
3343 os_memset(cli_chan, 0, sizeof(*cli_chan));
3345 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3348 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3349 chan->reg_class[cla].reg_class = 81;
3350 chan->reg_class[cla].channels = 0;
3351 for (i = 0; i < 11; i++) {
3352 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3353 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3355 if (chan->reg_class[cla].channels)
3358 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3361 /* Operating class 115 - 5 GHz, channels 36-48 */
3362 chan->reg_class[cla].reg_class = 115;
3363 chan->reg_class[cla].channels = 0;
3364 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3365 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3366 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3367 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3368 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3369 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3370 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3371 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3372 if (chan->reg_class[cla].channels)
3375 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3378 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3379 chan->reg_class[cla].reg_class = 124;
3380 chan->reg_class[cla].channels = 0;
3381 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3382 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3383 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3384 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3385 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3386 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3387 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3388 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3389 if (chan->reg_class[cla].channels)
3392 chan->reg_classes = cla;
3397 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3399 enum hostapd_hw_mode mode)
3403 for (i = 0; i < num_modes; i++) {
3404 if (modes[i].mode == mode)
3413 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3416 static int has_channel(struct wpa_global *global,
3417 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3422 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3424 if (wpas_p2p_disallowed_freq(global, freq))
3427 for (i = 0; i < mode->num_channels; i++) {
3428 if (mode->channels[i].chan == chan) {
3430 *flags = mode->channels[i].flag;
3431 if (mode->channels[i].flag &
3432 (HOSTAPD_CHAN_DISABLED |
3433 HOSTAPD_CHAN_RADAR))
3435 if (mode->channels[i].flag &
3436 (HOSTAPD_CHAN_PASSIVE_SCAN |
3437 HOSTAPD_CHAN_NO_IBSS))
3438 return PASSIVE_ONLY;
3447 struct p2p_oper_class_map {
3448 enum hostapd_hw_mode mode;
3453 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
3456 static struct p2p_oper_class_map op_class[] = {
3457 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3458 #if 0 /* Do not enable HT40 on 2 GHz for now */
3459 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3460 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3462 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3463 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3464 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3465 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3466 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3467 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3470 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3471 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3472 * 80 MHz, but currently use the following definition for simplicity
3473 * (these center frequencies are not actual channels, which makes
3474 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3475 * removing invalid channels.
3477 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3478 { -1, 0, 0, 0, 0, BW20 }
3482 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3483 struct hostapd_hw_modes *mode,
3486 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3489 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3492 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3494 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3495 * so the center channel is 6 channels away from the start/end.
3497 if (channel >= center_channels[i] - 6 &&
3498 channel <= center_channels[i] + 6)
3499 return center_channels[i];
3505 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3506 struct hostapd_hw_modes *mode,
3511 enum chan_allowed res, ret = ALLOWED;
3513 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3516 if (center_chan >= 58 && center_chan <= 138)
3517 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3519 /* check all the channels are available */
3520 for (i = 0; i < 4; i++) {
3521 int adj_chan = center_chan - 6 + i * 4;
3523 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3524 if (res == NOT_ALLOWED)
3526 if (res == PASSIVE_ONLY)
3529 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3531 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3533 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3535 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3543 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3544 struct hostapd_hw_modes *mode,
3548 enum chan_allowed res, res2;
3550 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3551 if (bw == BW40MINUS) {
3552 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3554 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3555 } else if (bw == BW40PLUS) {
3556 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3558 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3559 } else if (bw == BW80) {
3560 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3563 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3565 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3566 return PASSIVE_ONLY;
3571 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3572 struct p2p_channels *chan,
3573 struct p2p_channels *cli_chan)
3575 struct hostapd_hw_modes *mode;
3576 int cla, op, cli_cla;
3578 if (wpa_s->hw.modes == NULL) {
3579 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3580 "of all supported channels; assume dualband "
3582 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3587 for (op = 0; op_class[op].op_class; op++) {
3588 struct p2p_oper_class_map *o = &op_class[op];
3590 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3592 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3595 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3596 enum chan_allowed res;
3597 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3598 if (res == ALLOWED) {
3600 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3602 reg = &chan->reg_class[cla];
3604 reg->reg_class = o->op_class;
3606 reg->channel[reg->channels] = ch;
3608 } else if (res == PASSIVE_ONLY &&
3609 wpa_s->conf->p2p_add_cli_chan) {
3610 if (cli_reg == NULL) {
3611 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3613 cli_reg = &cli_chan->reg_class[cli_cla];
3615 cli_reg->reg_class = o->op_class;
3617 cli_reg->channel[cli_reg->channels] = ch;
3618 cli_reg->channels++;
3622 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3623 reg->channel, reg->channels);
3626 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3627 cli_reg->channel, cli_reg->channels);
3631 chan->reg_classes = cla;
3632 cli_chan->reg_classes = cli_cla;
3638 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3639 struct hostapd_hw_modes *mode, u8 channel)
3642 enum chan_allowed ret;
3644 for (op = 0; op_class[op].op_class; op++) {
3645 struct p2p_oper_class_map *o = &op_class[op];
3648 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3649 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3650 o->bw == BW20 || ch != channel)
3652 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3654 return (o->bw == BW40MINUS) ? -1 : 1;
3661 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3662 struct hostapd_hw_modes *mode, u8 channel)
3664 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3667 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3671 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3674 struct wpa_supplicant *wpa_s = ctx;
3676 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3677 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3683 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3687 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3688 const u8 *ssid, size_t ssid_len)
3690 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3691 struct wpa_ssid *s = wpa_s->current_ssid;
3694 if (s->mode != WPAS_MODE_P2P_GO &&
3695 s->mode != WPAS_MODE_AP &&
3696 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3698 if (s->ssid_len != ssid_len ||
3699 os_memcmp(s, s->ssid, ssid_len) != 0)
3709 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3710 const u8 *peer_dev_addr)
3712 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3713 struct wpa_ssid *ssid = wpa_s->current_ssid;
3716 if (ssid->mode != WPAS_MODE_INFRA)
3718 if (wpa_s->wpa_state != WPA_COMPLETED &&
3719 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3721 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3729 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3731 struct wpa_supplicant *wpa_s = ctx;
3733 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3737 static int wpas_is_concurrent_session_active(void *ctx)
3739 struct wpa_supplicant *wpa_s = ctx;
3740 struct wpa_supplicant *ifs;
3742 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3745 if (ifs->wpa_state > WPA_ASSOCIATED)
3752 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3754 struct wpa_supplicant *wpa_s = ctx;
3755 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3759 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3761 struct wpa_interface iface;
3762 struct wpa_supplicant *p2pdev_wpa_s;
3764 char force_name[100];
3767 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3769 force_name[0] = '\0';
3770 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3771 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3772 force_name, wpa_s->pending_interface_addr, NULL);
3774 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3777 os_strlcpy(wpa_s->pending_interface_name, ifname,
3778 sizeof(wpa_s->pending_interface_name));
3780 os_memset(&iface, 0, sizeof(iface));
3782 iface.ifname = wpa_s->pending_interface_name;
3783 iface.driver = wpa_s->driver->name;
3784 iface.driver_param = wpa_s->conf->driver_param;
3787 * If a P2P Device configuration file was given, use it as the interface
3788 * configuration file (instead of using parent's configuration file.
3790 if (wpa_s->conf_p2p_dev) {
3791 iface.confname = wpa_s->conf_p2p_dev;
3792 iface.ctrl_interface = NULL;
3794 iface.confname = wpa_s->confname;
3795 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3797 iface.conf_p2p_dev = NULL;
3799 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3800 if (!p2pdev_wpa_s) {
3801 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3804 p2pdev_wpa_s->parent = wpa_s;
3806 wpa_s->pending_interface_name[0] = '\0';
3811 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3812 const u8 *noa, size_t noa_len)
3814 struct wpa_supplicant *wpa_s, *intf = ctx;
3817 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3818 if (wpa_s->waiting_presence_resp)
3822 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3825 wpa_s->waiting_presence_resp = 0;
3827 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3828 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3829 " status=%u noa=%s", MAC2STR(src), status, hex);
3833 static int _wpas_p2p_in_progress(void *ctx)
3835 struct wpa_supplicant *wpa_s = ctx;
3836 return wpas_p2p_in_progress(wpa_s);
3841 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3842 * @global: Pointer to global data from wpa_supplicant_init()
3843 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3844 * Returns: 0 on success, -1 on failure
3846 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3848 struct p2p_config p2p;
3852 if (wpa_s->conf->p2p_disabled)
3855 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3861 os_memset(&p2p, 0, sizeof(p2p));
3863 p2p.debug_print = wpas_p2p_debug_print;
3864 p2p.p2p_scan = wpas_p2p_scan;
3865 p2p.send_action = wpas_send_action;
3866 p2p.send_action_done = wpas_send_action_done;
3867 p2p.go_neg_completed = wpas_go_neg_completed;
3868 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3869 p2p.dev_found = wpas_dev_found;
3870 p2p.dev_lost = wpas_dev_lost;
3871 p2p.find_stopped = wpas_find_stopped;
3872 p2p.start_listen = wpas_start_listen;
3873 p2p.stop_listen = wpas_stop_listen;
3874 p2p.send_probe_resp = wpas_send_probe_resp;
3875 p2p.sd_request = wpas_sd_request;
3876 p2p.sd_response = wpas_sd_response;
3877 p2p.prov_disc_req = wpas_prov_disc_req;
3878 p2p.prov_disc_resp = wpas_prov_disc_resp;
3879 p2p.prov_disc_fail = wpas_prov_disc_fail;
3880 p2p.invitation_process = wpas_invitation_process;
3881 p2p.invitation_received = wpas_invitation_received;
3882 p2p.invitation_result = wpas_invitation_result;
3883 p2p.get_noa = wpas_get_noa;
3884 p2p.go_connected = wpas_go_connected;
3885 p2p.presence_resp = wpas_presence_resp;
3886 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
3887 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
3889 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3890 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3891 p2p.dev_name = wpa_s->conf->device_name;
3892 p2p.manufacturer = wpa_s->conf->manufacturer;
3893 p2p.model_name = wpa_s->conf->model_name;
3894 p2p.model_number = wpa_s->conf->model_number;
3895 p2p.serial_number = wpa_s->conf->serial_number;
3897 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3898 p2p.config_methods = wpa_s->wps->config_methods;
3901 if (wpa_s->conf->p2p_listen_reg_class &&
3902 wpa_s->conf->p2p_listen_channel) {
3903 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3904 p2p.channel = wpa_s->conf->p2p_listen_channel;
3905 p2p.channel_forced = 1;
3909 * Pick one of the social channels randomly as the listen
3912 os_get_random((u8 *) &r, sizeof(r));
3913 p2p.channel = 1 + (r % 3) * 5;
3914 p2p.channel_forced = 0;
3916 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3918 if (wpa_s->conf->p2p_oper_reg_class &&
3919 wpa_s->conf->p2p_oper_channel) {
3920 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3921 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3922 p2p.cfg_op_channel = 1;
3923 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3924 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3927 p2p.op_reg_class = 81;
3929 * Use random operation channel from (1, 6, 11) if no other
3930 * preference is indicated.
3932 os_get_random((u8 *) &r, sizeof(r));
3933 p2p.op_channel = 1 + (r % 3) * 5;
3934 p2p.cfg_op_channel = 0;
3935 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3936 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3939 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3940 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3941 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3944 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3945 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3946 p2p.country[2] = 0x04;
3948 os_memcpy(p2p.country, "XX\x04", 3);
3950 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3951 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3956 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3959 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3960 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3961 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3963 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3964 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3966 p2p.max_peers = 100;
3968 if (wpa_s->conf->p2p_ssid_postfix) {
3969 p2p.ssid_postfix_len =
3970 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3971 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3972 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3973 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3974 p2p.ssid_postfix_len);
3977 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3979 p2p.max_listen = wpa_s->max_remain_on_chan;
3981 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
3982 wpa_s->conf->p2p_passphrase_len <= 63)
3983 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
3985 p2p.passphrase_len = 8;
3987 global->p2p = p2p_init(&p2p);
3988 if (global->p2p == NULL)
3990 global->p2p_init_wpa_s = wpa_s;
3992 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3993 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3995 p2p_add_wps_vendor_extension(
3996 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3999 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4006 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4007 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4009 * This function deinitialize per-interface P2P data.
4011 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4013 if (wpa_s->driver && wpa_s->drv_priv)
4014 wpa_drv_probe_req_report(wpa_s, 0);
4016 if (wpa_s->go_params) {
4017 /* Clear any stored provisioning info */
4018 p2p_clear_provisioning_info(
4020 wpa_s->go_params->peer_device_addr);
4023 os_free(wpa_s->go_params);
4024 wpa_s->go_params = NULL;
4025 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4026 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4027 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4028 wpa_s->p2p_long_listen = 0;
4029 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4030 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4031 wpas_p2p_remove_pending_group_interface(wpa_s);
4032 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4033 wpas_p2p_listen_work_done(wpa_s);
4034 if (wpa_s->p2p_send_action_work) {
4035 os_free(wpa_s->p2p_send_action_work->ctx);
4036 radio_work_done(wpa_s->p2p_send_action_work);
4037 wpa_s->p2p_send_action_work = NULL;
4039 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4041 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4042 wpa_s->p2p_oob_dev_pw = NULL;
4044 /* TODO: remove group interface from the driver if this wpa_s instance
4045 * is on top of a P2P group interface */
4050 * wpas_p2p_deinit_global - Deinitialize global P2P module
4051 * @global: Pointer to global data from wpa_supplicant_init()
4053 * This function deinitializes the global (per device) P2P module.
4055 static void wpas_p2p_deinit_global(struct wpa_global *global)
4057 struct wpa_supplicant *wpa_s, *tmp;
4059 wpa_s = global->ifaces;
4061 wpas_p2p_service_flush(wpa_s);
4063 if (global->p2p == NULL)
4066 /* Remove remaining P2P group interfaces */
4067 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4068 wpa_s = wpa_s->next;
4070 tmp = global->ifaces;
4073 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4078 /* Disconnect from the P2P group and deinit the interface */
4079 wpas_p2p_disconnect(tmp);
4083 * Deinit GO data on any possibly remaining interface (if main
4084 * interface is used as GO).
4086 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4087 if (wpa_s->ap_iface)
4088 wpas_p2p_group_deinit(wpa_s);
4091 p2p_deinit(global->p2p);
4093 global->p2p_init_wpa_s = NULL;
4097 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4099 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4100 wpa_s->conf->p2p_no_group_iface)
4101 return 0; /* separate interface disabled per configuration */
4102 if (wpa_s->drv_flags &
4103 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4104 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4105 return 1; /* P2P group requires a new interface in every case
4107 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4108 return 0; /* driver does not support concurrent operations */
4109 if (wpa_s->global->ifaces->next)
4110 return 1; /* more that one interface already in use */
4111 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4112 return 1; /* this interface is already in use */
4117 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4118 const u8 *peer_addr,
4119 enum p2p_wps_method wps_method,
4120 int go_intent, const u8 *own_interface_addr,
4121 unsigned int force_freq, int persistent_group,
4122 struct wpa_ssid *ssid, unsigned int pref_freq)
4124 if (persistent_group && wpa_s->conf->persistent_reconnect)
4125 persistent_group = 2;
4128 * Increase GO config timeout if HT40 is used since it takes some time
4129 * to scan channels for coex purposes before the BSS can be started.
4131 p2p_set_config_timeout(wpa_s->global->p2p,
4132 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4134 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4135 go_intent, own_interface_addr, force_freq,
4136 persistent_group, ssid ? ssid->ssid : NULL,
4137 ssid ? ssid->ssid_len : 0,
4138 wpa_s->p2p_pd_before_go_neg, pref_freq,
4139 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4144 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4145 const u8 *peer_addr,
4146 enum p2p_wps_method wps_method,
4147 int go_intent, const u8 *own_interface_addr,
4148 unsigned int force_freq, int persistent_group,
4149 struct wpa_ssid *ssid, unsigned int pref_freq)
4151 if (persistent_group && wpa_s->conf->persistent_reconnect)
4152 persistent_group = 2;
4154 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4155 go_intent, own_interface_addr, force_freq,
4156 persistent_group, ssid ? ssid->ssid : NULL,
4157 ssid ? ssid->ssid_len : 0, pref_freq,
4158 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4163 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4165 wpa_s->p2p_join_scan_count++;
4166 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4167 wpa_s->p2p_join_scan_count);
4168 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4169 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4170 " for join operationg - stop join attempt",
4171 MAC2STR(wpa_s->pending_join_iface_addr));
4172 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4173 if (wpa_s->p2p_auto_pd) {
4174 wpa_s->p2p_auto_pd = 0;
4175 wpa_msg_global(wpa_s, MSG_INFO,
4176 P2P_EVENT_PROV_DISC_FAILURE
4177 " p2p_dev_addr=" MACSTR " status=N/A",
4178 MAC2STR(wpa_s->pending_join_dev_addr));
4181 wpa_msg_global(wpa_s->parent, MSG_INFO,
4182 P2P_EVENT_GROUP_FORMATION_FAILURE);
4187 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4190 unsigned int num, i;
4191 struct wpa_used_freq_data *freqs;
4193 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4194 /* Multiple channels are supported and not all are in use */
4198 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4199 sizeof(struct wpa_used_freq_data));
4203 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4204 wpa_s->num_multichan_concurrent);
4206 for (i = 0; i < num; i++) {
4207 if (freqs[i].freq == freq) {
4208 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4215 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4224 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4225 const u8 *peer_dev_addr)
4227 struct wpa_bss *bss;
4230 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4233 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4234 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4239 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4241 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4242 "%ld.%06ld (%supdated in last scan)",
4243 bss->last_update.sec, bss->last_update.usec,
4244 updated ? "": "not ");
4250 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4251 struct wpa_scan_results *scan_res)
4253 struct wpa_bss *bss = NULL;
4255 u8 iface_addr[ETH_ALEN];
4257 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4259 if (wpa_s->global->p2p_disabled)
4262 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4263 scan_res ? (int) scan_res->num : -1,
4264 wpa_s->p2p_auto_join ? "auto_" : "");
4267 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4269 if (wpa_s->p2p_auto_pd) {
4270 int join = wpas_p2p_peer_go(wpa_s,
4271 wpa_s->pending_join_dev_addr);
4273 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4274 wpa_s->auto_pd_scan_retry++;
4275 bss = wpa_bss_get_bssid_latest(
4276 wpa_s, wpa_s->pending_join_dev_addr);
4279 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4280 "the peer " MACSTR " at %d MHz",
4281 wpa_s->auto_pd_scan_retry,
4283 pending_join_dev_addr),
4285 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4293 wpa_s->p2p_auto_pd = 0;
4294 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4295 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4296 MAC2STR(wpa_s->pending_join_dev_addr), join);
4297 if (p2p_prov_disc_req(wpa_s->global->p2p,
4298 wpa_s->pending_join_dev_addr,
4299 wpa_s->pending_pd_config_methods, join,
4300 0, wpa_s->user_initiated_pd) < 0) {
4301 wpa_s->p2p_auto_pd = 0;
4302 wpa_msg_global(wpa_s, MSG_INFO,
4303 P2P_EVENT_PROV_DISC_FAILURE
4304 " p2p_dev_addr=" MACSTR " status=N/A",
4305 MAC2STR(wpa_s->pending_join_dev_addr));
4310 if (wpa_s->p2p_auto_join) {
4311 int join = wpas_p2p_peer_go(wpa_s,
4312 wpa_s->pending_join_dev_addr);
4314 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4315 "running a GO -> use GO Negotiation");
4316 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4317 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4318 wpa_s->p2p_persistent_group, 0, 0, 0,
4319 wpa_s->p2p_go_intent,
4320 wpa_s->p2p_connect_freq,
4321 wpa_s->p2p_persistent_id,
4322 wpa_s->p2p_pd_before_go_neg,
4328 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4329 "try to join the group", join ? "" :
4332 wpa_s->p2p_fallback_to_go_neg = 1;
4335 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4336 wpa_s->pending_join_iface_addr);
4338 p2p_get_interface_addr(wpa_s->global->p2p,
4339 wpa_s->pending_join_dev_addr,
4341 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4343 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4344 "address for join from " MACSTR " to " MACSTR
4345 " based on newly discovered P2P peer entry",
4346 MAC2STR(wpa_s->pending_join_iface_addr),
4347 MAC2STR(iface_addr));
4348 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4351 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4352 wpa_s->pending_join_iface_addr);
4355 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4356 "from P2P peer table: %d MHz", freq);
4358 if (wpa_s->p2p_join_ssid_len) {
4359 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4360 MACSTR " and SSID %s",
4361 MAC2STR(wpa_s->pending_join_iface_addr),
4362 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4363 wpa_s->p2p_join_ssid_len));
4364 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4365 wpa_s->p2p_join_ssid,
4366 wpa_s->p2p_join_ssid_len);
4369 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4370 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4371 bss = wpa_bss_get_bssid_latest(wpa_s,
4372 wpa_s->pending_join_iface_addr);
4376 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4377 "from BSS table: %d MHz (SSID %s)", freq,
4378 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4383 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4384 wpa_msg_global(wpa_s->parent, MSG_INFO,
4385 P2P_EVENT_GROUP_FORMATION_FAILURE
4386 "reason=FREQ_CONFLICT");
4390 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4391 "prior to joining an existing group (GO " MACSTR
4393 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4394 wpa_s->pending_pd_before_join = 1;
4396 switch (wpa_s->pending_join_wps_method) {
4397 case WPS_PIN_DISPLAY:
4398 method = WPS_CONFIG_KEYPAD;
4400 case WPS_PIN_KEYPAD:
4401 method = WPS_CONFIG_DISPLAY;
4404 method = WPS_CONFIG_PUSHBUTTON;
4411 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4412 wpa_s->pending_join_dev_addr) ==
4415 * We have already performed provision discovery for
4416 * joining the group. Proceed directly to join
4417 * operation without duplicated provision discovery. */
4418 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4419 "with " MACSTR " already done - proceed to "
4421 MAC2STR(wpa_s->pending_join_dev_addr));
4422 wpa_s->pending_pd_before_join = 0;
4426 if (p2p_prov_disc_req(wpa_s->global->p2p,
4427 wpa_s->pending_join_dev_addr, method, 1,
4428 freq, wpa_s->user_initiated_pd) < 0) {
4429 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4430 "Discovery Request before joining an "
4432 wpa_s->pending_pd_before_join = 0;
4438 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4439 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4440 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4441 wpas_p2p_check_join_scan_limit(wpa_s);
4445 /* Start join operation immediately */
4446 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4450 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4451 const u8 *ssid, size_t ssid_len)
4454 struct wpa_driver_scan_params params;
4455 struct wpabuf *wps_ie, *ies;
4457 int freqs[2] = { 0, 0 };
4459 os_memset(¶ms, 0, sizeof(params));
4461 /* P2P Wildcard SSID */
4462 params.num_ssids = 1;
4463 if (ssid && ssid_len) {
4464 params.ssids[0].ssid = ssid;
4465 params.ssids[0].ssid_len = ssid_len;
4466 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4467 wpa_s->p2p_join_ssid_len = ssid_len;
4469 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4470 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4471 wpa_s->p2p_join_ssid_len = 0;
4474 wpa_s->wps->dev.p2p = 1;
4475 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4476 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4478 if (wps_ie == NULL) {
4479 wpas_p2p_scan_res_join(wpa_s, NULL);
4483 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4484 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4486 wpabuf_free(wps_ie);
4487 wpas_p2p_scan_res_join(wpa_s, NULL);
4490 wpabuf_put_buf(ies, wps_ie);
4491 wpabuf_free(wps_ie);
4493 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4495 params.p2p_probe = 1;
4496 params.extra_ies = wpabuf_head(ies);
4497 params.extra_ies_len = wpabuf_len(ies);
4502 * If freq is not provided, check the operating freq of the GO
4503 * and use a single channel scan on if possible.
4505 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4506 wpa_s->pending_join_iface_addr);
4512 params.freqs = freqs;
4516 * Run a scan to update BSS table and start Provision Discovery once
4517 * the new scan results become available.
4519 ret = wpa_drv_scan(wpa_s, ¶ms);
4521 os_get_reltime(&wpa_s->scan_trigger_time);
4522 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4523 wpa_s->own_scan_requested = 1;
4529 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4531 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4532 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4533 wpas_p2p_check_join_scan_limit(wpa_s);
4538 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4540 struct wpa_supplicant *wpa_s = eloop_ctx;
4541 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4545 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4546 const u8 *dev_addr, enum p2p_wps_method wps_method,
4547 int auto_join, int op_freq,
4548 const u8 *ssid, size_t ssid_len)
4550 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4551 MACSTR " dev " MACSTR " op_freq=%d)%s",
4552 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4553 auto_join ? " (auto_join)" : "");
4554 if (ssid && ssid_len) {
4555 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4556 wpa_ssid_txt(ssid, ssid_len));
4559 wpa_s->p2p_auto_pd = 0;
4560 wpa_s->p2p_auto_join = !!auto_join;
4561 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4562 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4563 wpa_s->pending_join_wps_method = wps_method;
4565 /* Make sure we are not running find during connection establishment */
4566 wpas_p2p_stop_find(wpa_s);
4568 wpa_s->p2p_join_scan_count = 0;
4569 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4574 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4575 const u8 *ssid, size_t ssid_len)
4577 struct wpa_supplicant *group;
4578 struct p2p_go_neg_results res;
4579 struct wpa_bss *bss;
4581 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4584 if (group != wpa_s) {
4585 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4586 sizeof(group->p2p_pin));
4587 group->p2p_wps_method = wpa_s->p2p_wps_method;
4590 * Need to mark the current interface for p2p_group_formation
4591 * when a separate group interface is not used. This is needed
4592 * to allow p2p_cancel stop a pending p2p_connect-join.
4593 * wpas_p2p_init_group_interface() addresses this for the case
4594 * where a separate group interface is used.
4596 wpa_s->global->p2p_group_formation = wpa_s;
4599 group->p2p_in_provisioning = 1;
4600 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4602 os_memset(&res, 0, sizeof(res));
4603 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4604 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4606 res.wps_method = wpa_s->pending_join_wps_method;
4607 if (freq && ssid && ssid_len) {
4609 res.ssid_len = ssid_len;
4610 os_memcpy(res.ssid, ssid, ssid_len);
4612 bss = wpa_bss_get_bssid_latest(wpa_s,
4613 wpa_s->pending_join_iface_addr);
4615 res.freq = bss->freq;
4616 res.ssid_len = bss->ssid_len;
4617 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4618 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4620 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4624 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4625 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4627 wpa_drv_cancel_remain_on_channel(wpa_s);
4628 wpa_s->off_channel_freq = 0;
4629 wpa_s->roc_waiting_drv_freq = 0;
4631 wpas_start_wps_enrollee(group, &res);
4634 * Allow a longer timeout for join-a-running-group than normal 15
4635 * second group formation timeout since the GO may not have authorized
4636 * our connection yet.
4638 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4639 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4646 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4647 int *force_freq, int *pref_freq, int go)
4649 struct wpa_used_freq_data *freqs;
4650 int res, best_freq, num_unused;
4651 unsigned int freq_in_use = 0, num, i;
4653 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4654 sizeof(struct wpa_used_freq_data));
4658 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4659 wpa_s->num_multichan_concurrent);
4662 * It is possible that the total number of used frequencies is bigger
4663 * than the number of frequencies used for P2P, so get the system wide
4664 * number of unused frequencies.
4666 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4668 wpa_printf(MSG_DEBUG,
4669 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4670 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4675 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4677 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4679 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4680 "(%u MHz) is not supported for P2P uses",
4686 for (i = 0; i < num; i++) {
4687 if (freqs[i].freq == freq)
4691 if (num_unused <= 0 && !freq_in_use) {
4692 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4697 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4698 "requested channel (%u MHz)", freq);
4703 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4705 /* We have a candidate frequency to use */
4706 if (best_freq > 0) {
4707 if (*pref_freq == 0 && num_unused > 0) {
4708 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4710 *pref_freq = best_freq;
4712 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4714 *force_freq = best_freq;
4716 } else if (num_unused > 0) {
4717 wpa_printf(MSG_DEBUG,
4718 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4721 wpa_printf(MSG_DEBUG,
4722 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4736 * wpas_p2p_connect - Request P2P Group Formation to be started
4737 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4738 * @peer_addr: Address of the peer P2P Device
4739 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4740 * @persistent_group: Whether to create a persistent group
4741 * @auto_join: Whether to select join vs. GO Negotiation automatically
4742 * @join: Whether to join an existing group (as a client) instead of starting
4743 * Group Owner negotiation; @peer_addr is BSSID in that case
4744 * @auth: Whether to only authorize the connection instead of doing that and
4745 * initiating Group Owner negotiation
4746 * @go_intent: GO Intent or -1 to use default
4747 * @freq: Frequency for the group or 0 for auto-selection
4748 * @persistent_id: Persistent group credentials to use for forcing GO
4749 * parameters or -1 to generate new values (SSID/passphrase)
4750 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4751 * interoperability workaround when initiating group formation
4752 * @ht40: Start GO with 40 MHz channel width
4753 * @vht: Start GO with VHT support
4754 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4755 * failure, -2 on failure due to channel not currently available,
4756 * -3 if forced channel is not supported
4758 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4759 const char *pin, enum p2p_wps_method wps_method,
4760 int persistent_group, int auto_join, int join, int auth,
4761 int go_intent, int freq, int persistent_id, int pd,
4764 int force_freq = 0, pref_freq = 0;
4766 enum wpa_driver_if_type iftype;
4768 struct wpa_ssid *ssid = NULL;
4770 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4773 if (persistent_id >= 0) {
4774 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4775 if (ssid == NULL || ssid->disabled != 2 ||
4776 ssid->mode != WPAS_MODE_P2P_GO)
4780 os_free(wpa_s->global->add_psk);
4781 wpa_s->global->add_psk = NULL;
4783 wpa_s->global->p2p_fail_on_wps_complete = 0;
4786 go_intent = wpa_s->conf->p2p_go_intent;
4789 wpa_s->p2p_long_listen = 0;
4791 wpa_s->p2p_wps_method = wps_method;
4792 wpa_s->p2p_persistent_group = !!persistent_group;
4793 wpa_s->p2p_persistent_id = persistent_id;
4794 wpa_s->p2p_go_intent = go_intent;
4795 wpa_s->p2p_connect_freq = freq;
4796 wpa_s->p2p_fallback_to_go_neg = 0;
4797 wpa_s->p2p_pd_before_go_neg = !!pd;
4798 wpa_s->p2p_go_ht40 = !!ht40;
4799 wpa_s->p2p_go_vht = !!vht;
4802 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4803 else if (wps_method == WPS_PIN_DISPLAY) {
4804 ret = wps_generate_pin();
4805 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4807 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4810 wpa_s->p2p_pin[0] = '\0';
4812 if (join || auto_join) {
4813 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4815 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4816 "connect a running group from " MACSTR,
4817 MAC2STR(peer_addr));
4818 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4821 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4822 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4824 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4825 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4829 os_get_reltime(&wpa_s->p2p_auto_started);
4830 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4832 wpa_s->p2p_auto_started.sec,
4833 wpa_s->p2p_auto_started.usec);
4835 wpa_s->user_initiated_pd = 1;
4836 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4837 auto_join, freq, NULL, 0) < 0)
4842 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4846 wpas_p2p_set_own_freq_preference(wpa_s,
4847 force_freq ? force_freq : pref_freq);
4849 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4851 if (wpa_s->create_p2p_iface) {
4852 /* Prepare to add a new interface for the group */
4853 iftype = WPA_IF_P2P_GROUP;
4854 if (go_intent == 15)
4855 iftype = WPA_IF_P2P_GO;
4856 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4857 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4858 "interface for the group");
4862 if_addr = wpa_s->pending_interface_addr;
4864 if_addr = wpa_s->own_addr;
4867 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4869 force_freq, persistent_group, ssid,
4875 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4876 go_intent, if_addr, force_freq,
4877 persistent_group, ssid, pref_freq) < 0) {
4878 if (wpa_s->create_p2p_iface)
4879 wpas_p2p_remove_pending_group_interface(wpa_s);
4887 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4888 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4889 * @freq: Frequency of the channel in MHz
4890 * @duration: Duration of the stay on the channel in milliseconds
4892 * This callback is called when the driver indicates that it has started the
4893 * requested remain-on-channel duration.
4895 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4896 unsigned int freq, unsigned int duration)
4898 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4900 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4901 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4902 wpa_s->pending_listen_duration);
4903 wpa_s->pending_listen_freq = 0;
4905 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4906 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4912 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4913 unsigned int timeout)
4915 /* Limit maximum Listen state time based on driver limitation. */
4916 if (timeout > wpa_s->max_remain_on_chan)
4917 timeout = wpa_s->max_remain_on_chan;
4919 return p2p_listen(wpa_s->global->p2p, timeout);
4924 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4925 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4926 * @freq: Frequency of the channel in MHz
4928 * This callback is called when the driver indicates that a remain-on-channel
4929 * operation has been completed, i.e., the duration on the requested channel
4932 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4935 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4936 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4937 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4938 wpas_p2p_listen_work_done(wpa_s);
4939 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4941 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4942 return; /* P2P module started a new operation */
4943 if (offchannel_pending_action_tx(wpa_s))
4945 if (wpa_s->p2p_long_listen > 0)
4946 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4947 if (wpa_s->p2p_long_listen > 0) {
4948 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4949 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4952 * When listen duration is over, stop listen & update p2p_state
4955 p2p_stop_listen(wpa_s->global->p2p);
4961 * wpas_p2p_group_remove - Remove a P2P group
4962 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4963 * @ifname: Network interface name of the group interface or "*" to remove all
4965 * Returns: 0 on success, -1 on failure
4967 * This function is used to remove a P2P group. This can be used to disconnect
4968 * from a group in which the local end is a P2P Client or to end a P2P Group in
4969 * case the local end is the Group Owner. If a virtual network interface was
4970 * created for this group, that interface will be removed. Otherwise, only the
4971 * configured P2P group network will be removed from the interface.
4973 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4975 struct wpa_global *global = wpa_s->global;
4977 if (os_strcmp(ifname, "*") == 0) {
4978 struct wpa_supplicant *prev;
4979 wpa_s = global->ifaces;
4982 wpa_s = wpa_s->next;
4983 if (prev->p2p_group_interface !=
4984 NOT_P2P_GROUP_INTERFACE ||
4985 (prev->current_ssid &&
4986 prev->current_ssid->p2p_group))
4987 wpas_p2p_disconnect(prev);
4992 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4993 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4997 return wpas_p2p_disconnect(wpa_s);
5001 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5006 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5008 if (wpa_s->best_24_freq > 0 &&
5009 p2p_supported_freq_go(wpa_s->global->p2p,
5010 wpa_s->best_24_freq)) {
5011 freq = wpa_s->best_24_freq;
5012 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5013 "channel: %d MHz", freq);
5015 os_get_random((u8 *) &r, sizeof(r));
5016 freq = 2412 + (r % 3) * 25;
5017 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5018 "channel: %d MHz", freq);
5023 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5025 if (wpa_s->best_5_freq > 0 &&
5026 p2p_supported_freq_go(wpa_s->global->p2p,
5027 wpa_s->best_5_freq)) {
5028 freq = wpa_s->best_5_freq;
5029 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5030 "channel: %d MHz", freq);
5032 os_get_random((u8 *) &r, sizeof(r));
5033 freq = 5180 + (r % 4) * 20;
5034 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5035 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5036 "5 GHz channel for P2P group");
5039 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5040 "channel: %d MHz", freq);
5044 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5045 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5046 "(%u MHz) is not supported for P2P uses",
5055 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5056 struct p2p_go_neg_results *params,
5057 const struct p2p_channels *channels)
5061 /* first try some random selection of the social channels */
5062 os_get_random((u8 *) &r, sizeof(r));
5064 for (i = 0; i < 3; i++) {
5065 params->freq = 2412 + ((r + i) % 3) * 25;
5066 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5067 freq_included(channels, params->freq))
5071 /* try all channels in reg. class 81 */
5072 for (i = 0; i < 11; i++) {
5073 params->freq = 2412 + i * 5;
5074 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5075 freq_included(channels, params->freq))
5079 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel allowed");
5082 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5088 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5089 struct p2p_go_neg_results *params,
5090 int freq, int ht40, int vht,
5091 const struct p2p_channels *channels)
5093 struct wpa_used_freq_data *freqs;
5094 unsigned int pref_freq, cand_freq;
5095 unsigned int num, i;
5097 os_memset(params, 0, sizeof(*params));
5098 params->role_go = 1;
5099 params->ht40 = ht40;
5102 if (!freq_included(channels, freq)) {
5103 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5107 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5108 "frequency %d MHz", freq);
5109 params->freq = freq;
5110 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5111 wpa_s->conf->p2p_oper_channel >= 1 &&
5112 wpa_s->conf->p2p_oper_channel <= 11 &&
5113 freq_included(channels,
5114 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5115 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5116 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5117 "frequency %d MHz", params->freq);
5118 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5119 wpa_s->conf->p2p_oper_reg_class == 116 ||
5120 wpa_s->conf->p2p_oper_reg_class == 117 ||
5121 wpa_s->conf->p2p_oper_reg_class == 124 ||
5122 wpa_s->conf->p2p_oper_reg_class == 126 ||
5123 wpa_s->conf->p2p_oper_reg_class == 127) &&
5124 freq_included(channels,
5125 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
5126 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5127 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5128 "frequency %d MHz", params->freq);
5129 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5130 wpa_s->best_overall_freq > 0 &&
5131 p2p_supported_freq_go(wpa_s->global->p2p,
5132 wpa_s->best_overall_freq) &&
5133 freq_included(channels, wpa_s->best_overall_freq)) {
5134 params->freq = wpa_s->best_overall_freq;
5135 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5136 "channel %d MHz", params->freq);
5137 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5138 wpa_s->best_24_freq > 0 &&
5139 p2p_supported_freq_go(wpa_s->global->p2p,
5140 wpa_s->best_24_freq) &&
5141 freq_included(channels, wpa_s->best_24_freq)) {
5142 params->freq = wpa_s->best_24_freq;
5143 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5144 "channel %d MHz", params->freq);
5145 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5146 wpa_s->best_5_freq > 0 &&
5147 p2p_supported_freq_go(wpa_s->global->p2p,
5148 wpa_s->best_5_freq) &&
5149 freq_included(channels, wpa_s->best_5_freq)) {
5150 params->freq = wpa_s->best_5_freq;
5151 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5152 "channel %d MHz", params->freq);
5153 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5155 params->freq = pref_freq;
5156 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5157 "channels", params->freq);
5159 /* no preference, select some channel */
5160 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
5164 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5165 sizeof(struct wpa_used_freq_data));
5169 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5170 wpa_s->num_multichan_concurrent);
5172 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5174 /* First try the best used frequency if possible */
5175 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5176 params->freq = cand_freq;
5178 /* Try any of the used frequencies */
5179 for (i = 0; i < num; i++) {
5180 if (freq_included(channels, freqs[i].freq)) {
5181 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5183 params->freq = freqs[i].freq;
5189 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5190 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5194 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5198 for (i = 0; i < num; i++) {
5199 if (freqs[i].freq == freq)
5204 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5206 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5210 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5220 static struct wpa_supplicant *
5221 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5224 struct wpa_supplicant *group_wpa_s;
5226 if (!wpas_p2p_create_iface(wpa_s)) {
5227 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5229 wpa_s->p2p_first_connection_timeout = 0;
5233 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5234 WPA_IF_P2P_CLIENT) < 0) {
5235 wpa_msg_global(wpa_s, MSG_ERROR,
5236 "P2P: Failed to add group interface");
5239 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5240 if (group_wpa_s == NULL) {
5241 wpa_msg_global(wpa_s, MSG_ERROR,
5242 "P2P: Failed to initialize group interface");
5243 wpas_p2p_remove_pending_group_interface(wpa_s);
5247 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5248 group_wpa_s->ifname);
5249 group_wpa_s->p2p_first_connection_timeout = 0;
5255 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5256 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5257 * @persistent_group: Whether to create a persistent group
5258 * @freq: Frequency for the group or 0 to indicate no hardcoding
5259 * @ht40: Start GO with 40 MHz channel width
5260 * @vht: Start GO with VHT support
5261 * Returns: 0 on success, -1 on failure
5263 * This function creates a new P2P group with the local end as the Group Owner,
5264 * i.e., without using Group Owner Negotiation.
5266 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5267 int freq, int ht40, int vht)
5269 struct p2p_go_neg_results params;
5271 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5274 os_free(wpa_s->global->add_psk);
5275 wpa_s->global->add_psk = NULL;
5277 /* Make sure we are not running find during connection establishment */
5278 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5279 wpas_p2p_stop_find_oper(wpa_s);
5281 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5285 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5288 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5289 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5290 "(%u MHz) is not supported for P2P uses",
5294 p2p_go_params(wpa_s->global->p2p, ¶ms);
5295 params.persistent_group = persistent_group;
5297 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5300 wpas_start_wps_go(wpa_s, ¶ms, 0);
5306 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5307 struct wpa_ssid *params, int addr_allocated,
5310 struct wpa_ssid *ssid;
5312 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5315 wpa_s->p2p_last_4way_hs_fail = NULL;
5317 wpa_supplicant_ap_deinit(wpa_s);
5319 ssid = wpa_config_add_network(wpa_s->conf);
5322 wpa_config_set_network_defaults(ssid);
5323 ssid->temporary = 1;
5324 ssid->proto = WPA_PROTO_RSN;
5325 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5326 ssid->group_cipher = WPA_CIPHER_CCMP;
5327 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5328 ssid->ssid = os_malloc(params->ssid_len);
5329 if (ssid->ssid == NULL) {
5330 wpa_config_remove_network(wpa_s->conf, ssid->id);
5333 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5334 ssid->ssid_len = params->ssid_len;
5335 ssid->p2p_group = 1;
5336 ssid->export_keys = 1;
5337 if (params->psk_set) {
5338 os_memcpy(ssid->psk, params->psk, 32);
5341 if (params->passphrase)
5342 ssid->passphrase = os_strdup(params->passphrase);
5344 wpa_s->show_group_started = 1;
5345 wpa_s->p2p_in_invitation = 1;
5346 wpa_s->p2p_invite_go_freq = freq;
5348 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5350 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5351 wpas_p2p_group_formation_timeout,
5352 wpa_s->parent, NULL);
5353 wpa_supplicant_select_network(wpa_s, ssid);
5359 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5360 struct wpa_ssid *ssid, int addr_allocated,
5361 int force_freq, int neg_freq, int ht40,
5362 int vht, const struct p2p_channels *channels,
5363 int connection_timeout)
5365 struct p2p_go_neg_results params;
5368 if (ssid->disabled != 2 || ssid->ssid == NULL)
5371 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5372 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5373 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5378 os_free(wpa_s->global->add_psk);
5379 wpa_s->global->add_psk = NULL;
5381 /* Make sure we are not running find during connection establishment */
5382 wpas_p2p_stop_find_oper(wpa_s);
5384 wpa_s->p2p_fallback_to_go_neg = 0;
5386 if (force_freq > 0) {
5387 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5391 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5392 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5396 if (ssid->mode == WPAS_MODE_INFRA)
5397 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5399 if (ssid->mode != WPAS_MODE_P2P_GO)
5402 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5406 params.psk_set = ssid->psk_set;
5408 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5409 if (ssid->passphrase) {
5410 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5411 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5412 "persistent group");
5415 os_strlcpy(params.passphrase, ssid->passphrase,
5416 sizeof(params.passphrase));
5418 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5419 params.ssid_len = ssid->ssid_len;
5420 params.persistent_group = 1;
5422 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5426 wpa_s->p2p_first_connection_timeout = connection_timeout;
5427 wpas_start_wps_go(wpa_s, ¶ms, 0);
5433 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5434 struct wpabuf *proberesp_ies)
5436 struct wpa_supplicant *wpa_s = ctx;
5437 if (wpa_s->ap_iface) {
5438 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5439 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5440 wpabuf_free(beacon_ies);
5441 wpabuf_free(proberesp_ies);
5445 wpabuf_free(hapd->p2p_beacon_ie);
5446 hapd->p2p_beacon_ie = beacon_ies;
5448 wpabuf_free(hapd->p2p_probe_resp_ie);
5449 hapd->p2p_probe_resp_ie = proberesp_ies;
5451 wpabuf_free(beacon_ies);
5452 wpabuf_free(proberesp_ies);
5454 wpa_supplicant_ap_update_beacon(wpa_s);
5458 static void wpas_p2p_idle_update(void *ctx, int idle)
5460 struct wpa_supplicant *wpa_s = ctx;
5461 if (!wpa_s->ap_iface)
5463 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5465 if (wpa_s->global->p2p_fail_on_wps_complete &&
5466 wpa_s->p2p_in_provisioning) {
5467 wpas_p2p_grpform_fail_after_wps(wpa_s);
5470 wpas_p2p_set_group_idle_timeout(wpa_s);
5472 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5476 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5477 struct wpa_ssid *ssid)
5479 struct p2p_group *group;
5480 struct p2p_group_config *cfg;
5482 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5485 cfg = os_zalloc(sizeof(*cfg));
5489 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5490 cfg->persistent_group = 2;
5491 else if (ssid->p2p_persistent_group)
5492 cfg->persistent_group = 1;
5493 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5494 if (wpa_s->max_stations &&
5495 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5496 cfg->max_clients = wpa_s->max_stations;
5498 cfg->max_clients = wpa_s->conf->max_num_sta;
5499 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5500 cfg->ssid_len = ssid->ssid_len;
5501 cfg->freq = ssid->frequency;
5502 cfg->cb_ctx = wpa_s;
5503 cfg->ie_update = wpas_p2p_ie_update;
5504 cfg->idle_update = wpas_p2p_idle_update;
5506 group = p2p_group_init(wpa_s->global->p2p, cfg);
5509 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5510 p2p_group_notif_formation_done(group);
5511 wpa_s->p2p_group = group;
5516 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5519 struct wpa_ssid *ssid = wpa_s->current_ssid;
5521 if (!wpa_s->p2p_in_provisioning) {
5522 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5523 "provisioning not in progress");
5527 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5528 u8 go_dev_addr[ETH_ALEN];
5529 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5530 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5532 /* Clear any stored provisioning info */
5533 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5536 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5538 wpa_s->p2p_go_group_formation_completed = 1;
5539 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5541 * Use a separate timeout for initial data connection to
5542 * complete to allow the group to be removed automatically if
5543 * something goes wrong in this step before the P2P group idle
5544 * timeout mechanism is taken into use.
5546 wpa_dbg(wpa_s, MSG_DEBUG,
5547 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5548 P2P_MAX_INITIAL_CONN_WAIT);
5549 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5550 wpas_p2p_group_formation_timeout,
5551 wpa_s->parent, NULL);
5554 * Use a separate timeout for initial data connection to
5555 * complete to allow the group to be removed automatically if
5556 * the client does not complete data connection successfully.
5558 wpa_dbg(wpa_s, MSG_DEBUG,
5559 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5560 P2P_MAX_INITIAL_CONN_WAIT_GO);
5561 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5562 wpas_p2p_group_formation_timeout,
5563 wpa_s->parent, NULL);
5565 * Complete group formation on first successful data connection
5567 wpa_s->p2p_go_group_formation_completed = 0;
5569 if (wpa_s->global->p2p)
5570 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5571 wpas_group_formation_completed(wpa_s, 1);
5575 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5576 struct wps_event_fail *fail)
5578 if (!wpa_s->p2p_in_provisioning) {
5579 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5580 "provisioning not in progress");
5584 if (wpa_s->go_params) {
5585 p2p_clear_provisioning_info(
5587 wpa_s->go_params->peer_device_addr);
5590 wpas_notify_p2p_wps_failed(wpa_s, fail);
5592 if (wpa_s == wpa_s->global->p2p_group_formation) {
5594 * Allow some time for the failed WPS negotiation exchange to
5595 * complete, but remove the group since group formation cannot
5596 * succeed after provisioning failure.
5598 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5599 wpa_s->global->p2p_fail_on_wps_complete = 1;
5600 eloop_deplete_timeout(0, 50000,
5601 wpas_p2p_group_formation_timeout,
5602 wpa_s->parent, NULL);
5607 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5609 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5610 !wpa_s->p2p_in_provisioning)
5613 wpas_p2p_grpform_fail_after_wps(wpa_s);
5619 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5620 const char *config_method,
5621 enum wpas_p2p_prov_disc_use use)
5625 wpa_s->p2p_fallback_to_go_neg = 0;
5626 wpa_s->pending_pd_use = NORMAL_PD;
5627 if (os_strncmp(config_method, "display", 7) == 0)
5628 config_methods = WPS_CONFIG_DISPLAY;
5629 else if (os_strncmp(config_method, "keypad", 6) == 0)
5630 config_methods = WPS_CONFIG_KEYPAD;
5631 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5632 os_strncmp(config_method, "pushbutton", 10) == 0)
5633 config_methods = WPS_CONFIG_PUSHBUTTON;
5635 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5639 if (use == WPAS_P2P_PD_AUTO) {
5640 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5641 wpa_s->pending_pd_config_methods = config_methods;
5642 wpa_s->p2p_auto_pd = 1;
5643 wpa_s->p2p_auto_join = 0;
5644 wpa_s->pending_pd_before_join = 0;
5645 wpa_s->auto_pd_scan_retry = 0;
5646 wpas_p2p_stop_find(wpa_s);
5647 wpa_s->p2p_join_scan_count = 0;
5648 os_get_reltime(&wpa_s->p2p_auto_started);
5649 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5650 wpa_s->p2p_auto_started.sec,
5651 wpa_s->p2p_auto_started.usec);
5652 wpas_p2p_join_scan(wpa_s, NULL);
5656 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5659 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5660 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5665 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5668 return p2p_scan_result_text(ies, ies_len, buf, end);
5672 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5674 if (!offchannel_pending_action_tx(wpa_s))
5677 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5678 "operation request");
5679 offchannel_clear_pending_action_tx(wpa_s);
5683 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5684 enum p2p_discovery_type type,
5685 unsigned int num_req_dev_types, const u8 *req_dev_types,
5686 const u8 *dev_id, unsigned int search_delay)
5688 wpas_p2p_clear_pending_action_tx(wpa_s);
5689 wpa_s->p2p_long_listen = 0;
5691 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5692 wpa_s->p2p_in_provisioning)
5695 wpa_supplicant_cancel_sched_scan(wpa_s);
5697 return p2p_find(wpa_s->global->p2p, timeout, type,
5698 num_req_dev_types, req_dev_types, dev_id,
5703 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5705 wpas_p2p_clear_pending_action_tx(wpa_s);
5706 wpa_s->p2p_long_listen = 0;
5707 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5708 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5710 if (wpa_s->global->p2p)
5711 p2p_stop_find(wpa_s->global->p2p);
5717 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5719 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5721 wpas_p2p_remove_pending_group_interface(wpa_s);
5725 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5727 struct wpa_supplicant *wpa_s = eloop_ctx;
5728 wpa_s->p2p_long_listen = 0;
5732 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5736 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5739 wpa_supplicant_cancel_sched_scan(wpa_s);
5740 wpas_p2p_clear_pending_action_tx(wpa_s);
5744 * This is a request for unlimited Listen state. However, at
5745 * least for now, this is mapped to a Listen state for one
5750 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5751 wpa_s->p2p_long_listen = 0;
5754 * Stop previous find/listen operation to avoid trying to request a new
5755 * remain-on-channel operation while the driver is still running the
5758 if (wpa_s->global->p2p)
5759 p2p_stop_find(wpa_s->global->p2p);
5761 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5762 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5763 wpa_s->p2p_long_listen = timeout * 1000;
5764 eloop_register_timeout(timeout, 0,
5765 wpas_p2p_long_listen_timeout,
5773 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5774 u8 *buf, size_t len, int p2p_group)
5776 struct wpabuf *p2p_ie;
5779 if (wpa_s->global->p2p_disabled)
5781 if (wpa_s->global->p2p == NULL)
5786 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5787 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5789 wpabuf_free(p2p_ie);
5795 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5796 const u8 *dst, const u8 *bssid,
5797 const u8 *ie, size_t ie_len, int ssi_signal)
5799 if (wpa_s->global->p2p_disabled)
5801 if (wpa_s->global->p2p == NULL)
5804 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5806 case P2P_PREQ_NOT_P2P:
5807 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5810 case P2P_PREQ_MALFORMED:
5811 case P2P_PREQ_NOT_LISTEN:
5812 case P2P_PREQ_NOT_PROCESSED:
5813 default: /* make gcc happy */
5815 case P2P_PREQ_PROCESSED:
5821 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5822 const u8 *sa, const u8 *bssid,
5823 u8 category, const u8 *data, size_t len, int freq)
5825 if (wpa_s->global->p2p_disabled)
5827 if (wpa_s->global->p2p == NULL)
5830 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5835 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5837 if (wpa_s->global->p2p_disabled)
5839 if (wpa_s->global->p2p == NULL)
5842 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5846 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5848 p2p_group_deinit(wpa_s->p2p_group);
5849 wpa_s->p2p_group = NULL;
5851 wpa_s->ap_configured_cb = NULL;
5852 wpa_s->ap_configured_cb_ctx = NULL;
5853 wpa_s->ap_configured_cb_data = NULL;
5854 wpa_s->connect_without_scan = NULL;
5858 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5860 wpa_s->p2p_long_listen = 0;
5862 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5865 return p2p_reject(wpa_s->global->p2p, addr);
5869 /* Invite to reinvoke a persistent group */
5870 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5871 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5872 int ht40, int vht, int pref_freq)
5874 enum p2p_invite_role role;
5878 int no_pref_freq_given = pref_freq == 0;
5880 wpa_s->global->p2p_invite_group = NULL;
5882 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5884 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5886 wpa_s->p2p_persistent_go_freq = freq;
5887 wpa_s->p2p_go_ht40 = !!ht40;
5888 if (ssid->mode == WPAS_MODE_P2P_GO) {
5889 role = P2P_INVITE_ROLE_GO;
5890 if (peer_addr == NULL) {
5891 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5892 "address in invitation command");
5895 if (wpas_p2p_create_iface(wpa_s)) {
5896 if (wpas_p2p_add_group_interface(wpa_s,
5897 WPA_IF_P2P_GO) < 0) {
5898 wpa_printf(MSG_ERROR, "P2P: Failed to "
5899 "allocate a new interface for the "
5903 bssid = wpa_s->pending_interface_addr;
5905 bssid = wpa_s->own_addr;
5907 role = P2P_INVITE_ROLE_CLIENT;
5908 peer_addr = ssid->bssid;
5910 wpa_s->pending_invite_ssid_id = ssid->id;
5912 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5913 role == P2P_INVITE_ROLE_GO);
5917 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5920 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5921 no_pref_freq_given && pref_freq > 0 &&
5922 wpa_s->num_multichan_concurrent > 1 &&
5923 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5924 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5929 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5930 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5935 /* Invite to join an active group */
5936 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5937 const u8 *peer_addr, const u8 *go_dev_addr)
5939 struct wpa_global *global = wpa_s->global;
5940 enum p2p_invite_role role;
5942 struct wpa_ssid *ssid;
5944 int freq = 0, force_freq = 0, pref_freq = 0;
5947 wpa_s->p2p_persistent_go_freq = 0;
5948 wpa_s->p2p_go_ht40 = 0;
5949 wpa_s->p2p_go_vht = 0;
5951 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5952 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5955 if (wpa_s == NULL) {
5956 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5960 ssid = wpa_s->current_ssid;
5962 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5967 wpa_s->global->p2p_invite_group = wpa_s;
5968 persistent = ssid->p2p_persistent_group &&
5969 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5970 ssid->ssid, ssid->ssid_len);
5972 if (ssid->mode == WPAS_MODE_P2P_GO) {
5973 role = P2P_INVITE_ROLE_ACTIVE_GO;
5974 bssid = wpa_s->own_addr;
5975 if (go_dev_addr == NULL)
5976 go_dev_addr = wpa_s->global->p2p_dev_addr;
5977 freq = ssid->frequency;
5979 role = P2P_INVITE_ROLE_CLIENT;
5980 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5981 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5982 "invite to current group");
5985 bssid = wpa_s->bssid;
5986 if (go_dev_addr == NULL &&
5987 !is_zero_ether_addr(wpa_s->go_dev_addr))
5988 go_dev_addr = wpa_s->go_dev_addr;
5989 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5990 (int) wpa_s->assoc_freq;
5992 wpa_s->parent->pending_invite_ssid_id = -1;
5994 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5997 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5998 role == P2P_INVITE_ROLE_ACTIVE_GO);
6001 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6003 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6004 ssid->ssid, ssid->ssid_len, force_freq,
6005 go_dev_addr, persistent, pref_freq, -1);
6009 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6011 struct wpa_ssid *ssid = wpa_s->current_ssid;
6012 const char *ssid_txt;
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 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
6031 os_memset(go_dev_addr, 0, ETH_ALEN);
6032 if (ssid->bssid_set)
6033 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6034 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6036 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6038 if (wpa_s->global->p2p_group_formation == wpa_s)
6039 wpa_s->global->p2p_group_formation = NULL;
6041 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6042 (int) wpa_s->assoc_freq;
6045 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6046 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
6047 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6048 ip[0], ip[1], ip[2], ip[3],
6049 ip[4], ip[5], ip[6], ip[7],
6050 ip[8], ip[9], ip[10], ip[11]);
6053 if (ssid->passphrase == NULL && ssid->psk_set) {
6055 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
6056 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
6057 "%s client ssid=\"%s\" freq=%d psk=%s "
6058 "go_dev_addr=" MACSTR "%s%s",
6059 wpa_s->ifname, ssid_txt, freq, psk,
6060 MAC2STR(go_dev_addr),
6061 persistent ? " [PERSISTENT]" : "", ip_addr);
6063 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
6064 "%s client ssid=\"%s\" freq=%d "
6065 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
6066 wpa_s->ifname, ssid_txt, freq,
6067 ssid->passphrase ? ssid->passphrase : "",
6068 MAC2STR(go_dev_addr),
6069 persistent ? " [PERSISTENT]" : "", ip_addr);
6073 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6076 network_id = ssid->id;
6077 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6081 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6082 u32 interval1, u32 duration2, u32 interval2)
6086 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6089 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6090 wpa_s->current_ssid == NULL ||
6091 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6094 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6095 wpa_s->own_addr, wpa_s->assoc_freq,
6096 duration1, interval1, duration2, interval2);
6098 wpa_s->waiting_presence_resp = 1;
6104 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6105 unsigned int interval)
6107 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6110 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6114 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6116 if (wpa_s->current_ssid == NULL) {
6118 * current_ssid can be cleared when P2P client interface gets
6119 * disconnected, so assume this interface was used as P2P
6124 return wpa_s->current_ssid->p2p_group &&
6125 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6129 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6131 struct wpa_supplicant *wpa_s = eloop_ctx;
6133 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6134 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6139 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6141 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6145 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6149 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6150 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6152 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6155 timeout = wpa_s->conf->p2p_group_idle;
6156 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6157 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6158 timeout = P2P_MAX_CLIENT_IDLE;
6164 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6165 timeout = 0; /* special client mode no-timeout */
6170 if (wpa_s->p2p_in_provisioning) {
6172 * Use the normal group formation timeout during the
6173 * provisioning phase to avoid terminating this process too
6174 * early due to group idle timeout.
6176 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6177 "during provisioning");
6181 if (wpa_s->show_group_started) {
6183 * Use the normal group formation timeout between the end of
6184 * the provisioning phase and completion of 4-way handshake to
6185 * avoid terminating this process too early due to group idle
6188 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6189 "while waiting for initial 4-way handshake to "
6194 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6196 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6201 /* Returns 1 if the interface was removed */
6202 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6203 u16 reason_code, const u8 *ie, size_t ie_len,
6204 int locally_generated)
6206 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6209 if (!locally_generated)
6210 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6213 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6214 wpa_s->current_ssid &&
6215 wpa_s->current_ssid->p2p_group &&
6216 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6217 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6218 "session is ending");
6219 if (wpas_p2p_group_delete(wpa_s,
6220 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6229 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6230 u16 reason_code, const u8 *ie, size_t ie_len,
6231 int locally_generated)
6233 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6236 if (!locally_generated)
6237 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6242 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6244 struct p2p_data *p2p = wpa_s->global->p2p;
6249 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6252 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6253 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6255 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6256 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6259 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6260 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6262 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6263 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6265 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6266 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6267 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6268 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6269 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6272 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6273 p2p_set_sec_dev_types(p2p,
6274 (void *) wpa_s->conf->sec_device_type,
6275 wpa_s->conf->num_sec_device_types);
6277 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6279 p2p_remove_wps_vendor_extensions(p2p);
6280 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6281 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6283 p2p_add_wps_vendor_extension(
6284 p2p, wpa_s->conf->wps_vendor_ext[i]);
6288 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6289 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6291 country[0] = wpa_s->conf->country[0];
6292 country[1] = wpa_s->conf->country[1];
6294 p2p_set_country(p2p, country);
6297 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6298 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6299 wpa_s->conf->p2p_ssid_postfix ?
6300 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6304 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6305 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6307 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6308 u8 reg_class, channel;
6313 if (wpa_s->conf->p2p_listen_reg_class &&
6314 wpa_s->conf->p2p_listen_channel) {
6315 reg_class = wpa_s->conf->p2p_listen_reg_class;
6316 channel = wpa_s->conf->p2p_listen_channel;
6321 * Pick one of the social channels randomly as the
6324 os_get_random((u8 *) &r, sizeof(r));
6325 channel = 1 + (r % 3) * 5;
6328 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6331 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6334 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6335 u8 op_reg_class, op_channel, cfg_op_channel;
6338 if (wpa_s->conf->p2p_oper_reg_class &&
6339 wpa_s->conf->p2p_oper_channel) {
6340 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6341 op_channel = wpa_s->conf->p2p_oper_channel;
6346 * Use random operation channel from (1, 6, 11)
6347 *if no other preference is indicated.
6349 os_get_random((u8 *) &r, sizeof(r));
6350 op_channel = 1 + (r % 3) * 5;
6353 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6356 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6360 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6361 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6362 wpa_s->conf->p2p_pref_chan) < 0) {
6363 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6367 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6368 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6373 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6374 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6378 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6381 if (!wpa_s->ap_iface)
6383 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6388 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6390 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6393 wpa_s->global->cross_connection = enabled;
6394 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6397 struct wpa_supplicant *iface;
6399 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6401 if (iface->cross_connect_enabled == 0)
6404 iface->cross_connect_enabled = 0;
6405 iface->cross_connect_in_use = 0;
6406 wpa_msg_global(iface->parent, MSG_INFO,
6407 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6409 iface->cross_connect_uplink);
6417 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6419 struct wpa_supplicant *iface;
6421 if (!uplink->global->cross_connection)
6424 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6425 if (!iface->cross_connect_enabled)
6427 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6430 if (iface->ap_iface == NULL)
6432 if (iface->cross_connect_in_use)
6435 iface->cross_connect_in_use = 1;
6436 wpa_msg_global(iface->parent, MSG_INFO,
6437 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6438 iface->ifname, iface->cross_connect_uplink);
6443 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6445 struct wpa_supplicant *iface;
6447 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6448 if (!iface->cross_connect_enabled)
6450 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6453 if (!iface->cross_connect_in_use)
6456 wpa_msg_global(iface->parent, MSG_INFO,
6457 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6458 iface->ifname, iface->cross_connect_uplink);
6459 iface->cross_connect_in_use = 0;
6464 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6466 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6467 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6468 wpa_s->cross_connect_disallowed)
6469 wpas_p2p_disable_cross_connect(wpa_s);
6471 wpas_p2p_enable_cross_connect(wpa_s);
6472 if (!wpa_s->ap_iface &&
6473 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6474 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6478 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6480 wpas_p2p_disable_cross_connect(wpa_s);
6481 if (!wpa_s->ap_iface &&
6482 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6484 wpas_p2p_set_group_idle_timeout(wpa_s);
6488 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6490 struct wpa_supplicant *iface;
6492 if (!wpa_s->global->cross_connection)
6495 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6498 if (iface->drv_flags &
6499 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6501 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6504 wpa_s->cross_connect_enabled = 1;
6505 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6506 sizeof(wpa_s->cross_connect_uplink));
6507 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6508 "%s to %s whenever uplink is available",
6509 wpa_s->ifname, wpa_s->cross_connect_uplink);
6511 if (iface->ap_iface || iface->current_ssid == NULL ||
6512 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6513 iface->cross_connect_disallowed ||
6514 iface->wpa_state != WPA_COMPLETED)
6517 wpa_s->cross_connect_in_use = 1;
6518 wpa_msg_global(wpa_s->parent, MSG_INFO,
6519 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6520 wpa_s->ifname, wpa_s->cross_connect_uplink);
6526 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6528 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6529 !wpa_s->p2p_in_provisioning)
6530 return 0; /* not P2P client operation */
6532 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6534 if (wpa_s != wpa_s->parent)
6535 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6536 wpas_p2p_group_formation_failed(wpa_s);
6541 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6543 struct wpa_supplicant *wpa_s = eloop_ctx;
6544 wpas_p2p_notif_pbc_overlap(wpa_s);
6548 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6550 struct p2p_channels chan, cli_chan;
6551 struct wpa_supplicant *ifs;
6553 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6556 os_memset(&chan, 0, sizeof(chan));
6557 os_memset(&cli_chan, 0, sizeof(cli_chan));
6558 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6559 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6564 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6566 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6568 if (!ifs->current_ssid ||
6569 !ifs->current_ssid->p2p_group ||
6570 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6571 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6573 freq = ifs->current_ssid->frequency;
6574 if (freq_included(&chan, freq)) {
6575 wpa_dbg(ifs, MSG_DEBUG,
6576 "P2P GO operating frequency %d MHz in valid range",
6581 wpa_dbg(ifs, MSG_DEBUG,
6582 "P2P GO operating in invalid frequency %d MHz", freq);
6583 /* TODO: Consider using CSA or removing the group within
6585 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6590 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6591 struct wpa_scan_results *scan_res)
6593 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6597 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6599 struct wpa_global *global = wpa_s->global;
6603 if (global->p2p == NULL)
6606 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6608 if (wpa_s->pending_interface_name[0] &&
6609 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6612 peer = p2p_get_go_neg_peer(global->p2p);
6614 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6615 MACSTR, MAC2STR(peer));
6616 p2p_unauthorize(global->p2p, peer);
6620 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6621 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6622 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6626 if (wpa_s->pending_pd_before_join) {
6627 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6628 wpa_s->pending_pd_before_join = 0;
6632 wpas_p2p_stop_find(wpa_s);
6634 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6635 if (wpa_s == global->p2p_group_formation &&
6636 (wpa_s->p2p_in_provisioning ||
6637 wpa_s->parent->pending_interface_type ==
6638 WPA_IF_P2P_CLIENT)) {
6639 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6640 "formation found - cancelling",
6643 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6644 wpa_s->parent, NULL);
6645 if (wpa_s->p2p_in_provisioning) {
6646 wpas_group_formation_completed(wpa_s, 0);
6649 wpas_p2p_group_delete(wpa_s,
6650 P2P_GROUP_REMOVAL_REQUESTED);
6652 } else if (wpa_s->p2p_in_invitation) {
6653 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6656 wpas_p2p_group_formation_failed(wpa_s);
6661 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6669 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6671 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6674 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6675 "being available anymore");
6676 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6680 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6681 int freq_24, int freq_5, int freq_overall)
6683 struct p2p_data *p2p = wpa_s->global->p2p;
6686 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6690 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6693 struct p2p_data *p2p = wpa_s->global->p2p;
6698 if (hwaddr_aton(addr, peer))
6701 return p2p_unauthorize(p2p, peer);
6706 * wpas_p2p_disconnect - Disconnect from a P2P Group
6707 * @wpa_s: Pointer to wpa_supplicant data
6708 * Returns: 0 on success, -1 on failure
6710 * This can be used to disconnect from a group in which the local end is a P2P
6711 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6712 * virtual network interface was created for this group, that interface will be
6713 * removed. Otherwise, only the configured P2P group network will be removed
6714 * from the interface.
6716 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6722 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6727 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6731 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6734 ret = p2p_in_progress(wpa_s->global->p2p);
6737 * Check whether there is an ongoing WPS provisioning step (or
6738 * other parts of group formation) on another interface since
6739 * p2p_in_progress() does not report this to avoid issues for
6740 * scans during such provisioning step.
6742 if (wpa_s->global->p2p_group_formation &&
6743 wpa_s->global->p2p_group_formation != wpa_s) {
6744 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6745 "in group formation",
6746 wpa_s->global->p2p_group_formation->ifname);
6751 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6752 struct os_reltime now;
6753 os_get_reltime(&now);
6754 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6755 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6756 /* Wait for the first client has expired */
6757 wpa_s->global->p2p_go_wait_client.sec = 0;
6759 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6768 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6769 struct wpa_ssid *ssid)
6771 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6772 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6773 wpa_s->parent, NULL) > 0) {
6775 * Remove the network by scheduling the group formation
6776 * timeout to happen immediately. The teardown code
6777 * needs to be scheduled to run asynch later so that we
6778 * don't delete data from under ourselves unexpectedly.
6779 * Calling wpas_p2p_group_formation_timeout directly
6780 * causes a series of crashes in WPS failure scenarios.
6782 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6783 "P2P group network getting removed");
6784 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6785 wpa_s->parent, NULL);
6790 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6791 const u8 *addr, const u8 *ssid,
6797 for (s = wpa_s->conf->ssid; s; s = s->next) {
6798 if (s->disabled != 2)
6801 (ssid_len != s->ssid_len ||
6802 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6805 if (s->mode == WPAS_MODE_P2P_GO)
6809 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6810 return s; /* peer is GO in the persistent group */
6811 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6813 for (i = 0; i < s->num_p2p_clients; i++) {
6814 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6815 addr, ETH_ALEN) == 0)
6816 return s; /* peer is P2P client in persistent
6825 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6828 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6829 wpa_s->parent, NULL) > 0) {
6831 * This can happen if WPS provisioning step is not terminated
6832 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6833 * peer was able to connect, there is no need to time out group
6834 * formation after this, though. In addition, this is used with
6835 * the initial connection wait on the GO as a separate formation
6836 * timeout and as such, expected to be hit after the initial WPS
6837 * provisioning step.
6839 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6841 if (!wpa_s->p2p_go_group_formation_completed) {
6842 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6843 wpa_s->p2p_go_group_formation_completed = 1;
6844 wpa_s->global->p2p_group_formation = NULL;
6845 wpa_s->p2p_in_provisioning = 0;
6846 wpa_s->p2p_in_invitation = 0;
6848 wpa_s->global->p2p_go_wait_client.sec = 0;
6851 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6855 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6858 struct wpa_supplicant *group = wpa_s;
6859 if (wpa_s->global->p2p_group_formation)
6860 group = wpa_s->global->p2p_group_formation;
6861 wpa_s = wpa_s->parent;
6862 offchannel_send_action_done(wpa_s);
6864 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6865 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6866 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6867 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6868 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6869 wpa_s->p2p_persistent_id,
6870 wpa_s->p2p_pd_before_go_neg,
6876 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6878 if (!wpa_s->p2p_fallback_to_go_neg ||
6879 wpa_s->p2p_in_provisioning <= 5)
6882 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6883 return 0; /* peer operating as a GO */
6885 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6886 "fallback to GO Negotiation");
6887 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6893 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6895 struct wpa_supplicant *ifs;
6897 if (wpa_s->wpa_state > WPA_SCANNING) {
6898 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6899 "concurrent operation",
6900 wpa_s->conf->p2p_search_delay);
6901 return wpa_s->conf->p2p_search_delay;
6904 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6906 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
6907 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6908 "delay due to concurrent operation on "
6910 wpa_s->conf->p2p_search_delay,
6912 return wpa_s->conf->p2p_search_delay;
6920 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6921 struct wpa_ssid *s, const u8 *addr,
6924 struct psk_list_entry *psk, *tmp;
6927 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6929 if ((iface_addr && !psk->p2p &&
6930 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6931 (!iface_addr && psk->p2p &&
6932 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6933 wpa_dbg(wpa_s, MSG_DEBUG,
6934 "P2P: Remove persistent group PSK list entry for "
6936 MAC2STR(psk->addr), psk->p2p);
6937 dl_list_del(&psk->list);
6947 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6948 const u8 *p2p_dev_addr,
6949 const u8 *psk, size_t psk_len)
6951 struct wpa_ssid *ssid = wpa_s->current_ssid;
6952 struct wpa_ssid *persistent;
6953 struct psk_list_entry *p, *last;
6955 if (psk_len != sizeof(p->psk))
6959 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6960 " p2p_dev_addr=" MACSTR,
6961 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6962 if (is_zero_ether_addr(p2p_dev_addr))
6963 p2p_dev_addr = NULL;
6965 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6969 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6970 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6971 /* To be added to persistent group once created */
6972 if (wpa_s->global->add_psk == NULL) {
6973 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6974 if (wpa_s->global->add_psk == NULL)
6977 p = wpa_s->global->add_psk;
6980 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6983 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6985 os_memcpy(p->psk, psk, psk_len);
6989 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6990 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6994 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6997 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7001 p = os_zalloc(sizeof(*p));
7006 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7009 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7011 os_memcpy(p->psk, psk, psk_len);
7013 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7014 (last = dl_list_last(&persistent->psk_list,
7015 struct psk_list_entry, list))) {
7016 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7017 MACSTR " (p2p=%u) to make room for a new one",
7018 MAC2STR(last->addr), last->p2p);
7019 dl_list_del(&last->list);
7023 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7024 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7025 p2p_dev_addr == NULL);
7027 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7028 MACSTR, MAC2STR(p2p_dev_addr));
7030 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7033 dl_list_add(&persistent->psk_list, &p->list);
7035 if (wpa_s->parent->conf->update_config &&
7036 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7037 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7041 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7042 struct wpa_ssid *s, const u8 *addr,
7047 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7048 if (res > 0 && wpa_s->conf->update_config &&
7049 wpa_config_write(wpa_s->confname, wpa_s->conf))
7050 wpa_dbg(wpa_s, MSG_DEBUG,
7051 "P2P: Failed to update configuration");
7055 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7056 const u8 *peer, int iface_addr)
7058 struct hostapd_data *hapd;
7059 struct hostapd_wpa_psk *psk, *prev, *rem;
7060 struct sta_info *sta;
7062 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7063 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7066 /* Remove per-station PSK entry */
7067 hapd = wpa_s->ap_iface->bss[0];
7069 psk = hapd->conf->ssid.wpa_psk;
7071 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7073 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7074 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7075 MACSTR " iface_addr=%d",
7076 MAC2STR(peer), iface_addr);
7078 prev->next = psk->next;
7080 hapd->conf->ssid.wpa_psk = psk->next;
7090 /* Disconnect from group */
7092 sta = ap_get_sta(hapd, peer);
7094 sta = ap_get_sta_p2p(hapd, peer);
7096 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7097 " (iface_addr=%d) from group",
7098 MAC2STR(peer), iface_addr);
7099 hostapd_drv_sta_deauth(hapd, sta->addr,
7100 WLAN_REASON_DEAUTH_LEAVING);
7101 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7106 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7110 struct wpa_supplicant *w;
7112 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7114 /* Remove from any persistent group */
7115 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7116 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7119 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7120 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7123 /* Remove from any operating group */
7124 for (w = wpa_s->global->ifaces; w; w = w->next)
7125 wpas_p2p_remove_client_go(w, peer, iface_addr);
7129 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7131 struct wpa_supplicant *wpa_s = eloop_ctx;
7132 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7136 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7138 struct wpa_supplicant *wpa_s = eloop_ctx;
7140 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7141 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7145 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7146 struct wpa_ssid *ssid)
7148 struct wpa_supplicant *iface;
7150 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7151 if (!iface->current_ssid ||
7152 iface->current_ssid->frequency == freq ||
7153 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7154 !iface->current_ssid->p2p_group))
7157 /* Remove the connection with least priority */
7158 if (!wpas_is_p2p_prioritized(iface)) {
7159 /* STA connection has priority over existing
7160 * P2P connection, so remove the interface. */
7161 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7162 eloop_register_timeout(0, 0,
7163 wpas_p2p_group_freq_conflict,
7165 /* If connection in progress is P2P connection, do not
7166 * proceed for the connection. */
7172 /* P2P connection has priority, disable the STA network
7174 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7176 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7177 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7178 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7180 /* If P2P connection is in progress, continue
7193 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7195 struct wpa_ssid *ssid = wpa_s->current_ssid;
7197 if (ssid == NULL || !ssid->p2p_group)
7200 if (wpa_s->p2p_last_4way_hs_fail &&
7201 wpa_s->p2p_last_4way_hs_fail == ssid) {
7202 u8 go_dev_addr[ETH_ALEN];
7203 struct wpa_ssid *persistent;
7205 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7207 ssid->ssid_len) <= 0) {
7208 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7212 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7213 MACSTR, MAC2STR(go_dev_addr));
7214 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7217 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7218 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7221 wpa_msg_global(wpa_s->parent, MSG_INFO,
7222 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7225 wpa_s->p2p_last_4way_hs_fail = NULL;
7227 * Remove the group from a timeout to avoid issues with caller
7228 * continuing to use the interface if this is on a P2P group
7231 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7236 wpa_s->p2p_last_4way_hs_fail = ssid;
7241 #ifdef CONFIG_WPS_NFC
7243 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7251 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7255 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7256 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7263 wpabuf_put_be16(ret, wsc_len);
7265 wpabuf_put_buf(ret, wsc);
7266 wpabuf_put_be16(ret, wpabuf_len(p2p));
7267 wpabuf_put_buf(ret, p2p);
7271 wpa_hexdump_buf(MSG_DEBUG,
7272 "P2P: Generated NFC connection handover message", ret);
7276 tmp = ndef_build_p2p(ret);
7279 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7289 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7290 struct wpa_ssid **ssid, u8 *go_dev_addr)
7292 struct wpa_supplicant *iface;
7295 os_memset(go_dev_addr, 0, ETH_ALEN);
7298 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7299 if (iface->wpa_state < WPA_ASSOCIATING ||
7300 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7301 !iface->current_ssid->p2p_group ||
7302 iface->current_ssid->mode != WPAS_MODE_INFRA)
7305 *ssid = iface->current_ssid;
7307 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7308 return iface->assoc_freq;
7314 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7317 struct wpabuf *wsc, *p2p;
7318 struct wpa_ssid *ssid;
7319 u8 go_dev_addr[ETH_ALEN];
7320 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7322 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7323 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7327 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7328 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7329 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7330 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7334 if (cli_freq == 0) {
7335 wsc = wps_build_nfc_handover_req_p2p(
7336 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7339 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7340 go_dev_addr, ssid ? ssid->ssid : NULL,
7341 ssid ? ssid->ssid_len : 0);
7343 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7347 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7350 struct wpabuf *wsc, *p2p;
7351 struct wpa_ssid *ssid;
7352 u8 go_dev_addr[ETH_ALEN];
7353 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7355 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7358 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7359 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7360 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7363 if (cli_freq == 0) {
7364 wsc = wps_build_nfc_handover_sel_p2p(
7366 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7367 DEV_PW_NFC_CONNECTION_HANDOVER,
7368 wpa_s->conf->wps_nfc_dh_pubkey,
7369 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7372 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7373 go_dev_addr, ssid ? ssid->ssid : NULL,
7374 ssid ? ssid->ssid_len : 0);
7376 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7380 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7381 struct p2p_nfc_params *params)
7383 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7384 "connection handover (freq=%d)",
7387 if (params->go_freq && params->go_ssid_len) {
7388 wpa_s->p2p_wps_method = WPS_NFC;
7389 wpa_s->pending_join_wps_method = WPS_NFC;
7390 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7391 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7393 return wpas_p2p_join_start(wpa_s, params->go_freq,
7395 params->go_ssid_len);
7398 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7399 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7400 params->go_freq, -1, 0, 1, 1);
7404 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7405 struct p2p_nfc_params *params, int tag)
7407 int res, persistent;
7408 struct wpa_ssid *ssid;
7410 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7411 "connection handover");
7412 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7413 ssid = wpa_s->current_ssid;
7416 if (ssid->mode != WPAS_MODE_P2P_GO)
7418 if (wpa_s->ap_iface == NULL)
7422 if (wpa_s == NULL) {
7423 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7427 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7428 DEV_PW_NFC_CONNECTION_HANDOVER &&
7429 !wpa_s->parent->p2p_oob_dev_pw) {
7430 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7433 res = wpas_ap_wps_add_nfc_pw(
7434 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7435 wpa_s->parent->p2p_oob_dev_pw,
7436 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7437 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7442 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7446 if (!params->peer ||
7447 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7450 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7451 " to join", MAC2STR(params->peer->p2p_device_addr));
7453 wpa_s->global->p2p_invite_group = wpa_s;
7454 persistent = ssid->p2p_persistent_group &&
7455 wpas_p2p_get_persistent(wpa_s->parent,
7456 params->peer->p2p_device_addr,
7457 ssid->ssid, ssid->ssid_len);
7458 wpa_s->parent->pending_invite_ssid_id = -1;
7460 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7461 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7462 ssid->ssid, ssid->ssid_len, ssid->frequency,
7463 wpa_s->global->p2p_dev_addr, persistent, 0,
7464 wpa_s->parent->p2p_oob_dev_pw_id);
7468 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7469 struct p2p_nfc_params *params,
7472 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7473 "connection handover");
7474 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7475 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7476 forced_freq, -1, 0, 1, 1);
7480 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7481 struct p2p_nfc_params *params,
7486 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7487 "connection handover");
7488 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7489 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7490 forced_freq, -1, 0, 1, 1);
7494 res = wpas_p2p_listen(wpa_s, 60);
7496 p2p_unauthorize(wpa_s->global->p2p,
7497 params->peer->p2p_device_addr);
7504 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7505 const struct wpabuf *data,
7506 int sel, int tag, int forced_freq)
7508 const u8 *pos, *end;
7510 struct p2p_nfc_params params;
7513 os_memset(¶ms, 0, sizeof(params));
7516 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7518 pos = wpabuf_head(data);
7519 end = pos + wpabuf_len(data);
7521 if (end - pos < 2) {
7522 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7526 len = WPA_GET_BE16(pos);
7528 if (pos + len > end) {
7529 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7533 params.wsc_attr = pos;
7534 params.wsc_len = len;
7537 if (end - pos < 2) {
7538 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7542 len = WPA_GET_BE16(pos);
7544 if (pos + len > end) {
7545 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7549 params.p2p_attr = pos;
7550 params.p2p_len = len;
7553 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7554 params.wsc_attr, params.wsc_len);
7555 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7556 params.p2p_attr, params.p2p_len);
7558 wpa_hexdump(MSG_DEBUG,
7559 "P2P: Ignored extra data after P2P attributes",
7563 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
7567 if (params.next_step == NO_ACTION)
7570 if (params.next_step == BOTH_GO) {
7571 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7572 MAC2STR(params.peer->p2p_device_addr));
7576 if (params.next_step == PEER_CLIENT) {
7577 if (!is_zero_ether_addr(params.go_dev_addr)) {
7578 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7579 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7581 MAC2STR(params.peer->p2p_device_addr),
7583 MAC2STR(params.go_dev_addr),
7584 wpa_ssid_txt(params.go_ssid,
7585 params.go_ssid_len));
7587 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7588 "peer=" MACSTR " freq=%d",
7589 MAC2STR(params.peer->p2p_device_addr),
7595 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7596 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7597 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7601 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7602 wpa_s->p2p_oob_dev_pw = NULL;
7604 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7605 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7610 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7611 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7612 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7613 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7614 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7615 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7616 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7620 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7621 "peer OOB Device Password Id %u", id);
7624 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7625 "Device Password Id %u", id);
7626 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7627 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7628 params.oob_dev_pw_len -
7629 WPS_OOB_PUBKEY_HASH_LEN - 2);
7630 wpa_s->p2p_oob_dev_pw_id = id;
7631 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7632 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7633 params.oob_dev_pw_len -
7634 WPS_OOB_PUBKEY_HASH_LEN - 2);
7635 if (wpa_s->p2p_oob_dev_pw == NULL)
7638 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7639 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7640 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7643 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7644 "without Device Password");
7645 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7648 switch (params.next_step) {
7652 /* already covered above */
7655 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
7657 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
7659 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
7661 /* TODO: use own OOB Dev Pw */
7662 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
7669 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7670 const struct wpabuf *data, int forced_freq)
7672 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7675 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7679 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7680 const struct wpabuf *req,
7681 const struct wpabuf *sel, int forced_freq)
7686 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7689 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7691 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7692 wpabuf_head(req), wpabuf_len(req));
7693 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7694 wpabuf_head(sel), wpabuf_len(sel));
7696 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7697 tmp = ndef_parse_p2p(init ? sel : req);
7699 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7703 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7711 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7714 int go_intent = wpa_s->conf->p2p_go_intent;
7715 struct wpa_supplicant *iface;
7717 if (wpa_s->global->p2p == NULL)
7721 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7722 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7724 if (!iface->ap_iface)
7726 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7728 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7730 if (wpa_s->p2p_nfc_tag_enabled)
7731 wpas_p2p_remove_pending_group_interface(wpa_s);
7732 wpa_s->p2p_nfc_tag_enabled = 0;
7736 if (wpa_s->global->p2p_disabled)
7739 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7740 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7741 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7742 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7743 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7744 "to allow static handover cases");
7748 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7750 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7751 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7752 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7753 if (wpa_s->p2p_oob_dev_pw == NULL)
7755 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7757 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7758 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7760 * P2P Group Interface present and the command came on group
7761 * interface, so enable the token for the current interface.
7763 wpa_s->create_p2p_iface = 0;
7765 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7768 if (wpa_s->create_p2p_iface) {
7769 enum wpa_driver_if_type iftype;
7770 /* Prepare to add a new interface for the group */
7771 iftype = WPA_IF_P2P_GROUP;
7772 if (go_intent == 15)
7773 iftype = WPA_IF_P2P_GO;
7774 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7775 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7776 "interface for the group");
7780 if_addr = wpa_s->pending_interface_addr;
7782 if_addr = wpa_s->own_addr;
7784 wpa_s->p2p_nfc_tag_enabled = enabled;
7786 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7787 struct hostapd_data *hapd;
7788 if (iface->ap_iface == NULL)
7790 hapd = iface->ap_iface->bss[0];
7791 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7792 hapd->conf->wps_nfc_dh_pubkey =
7793 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7794 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7795 hapd->conf->wps_nfc_dh_privkey =
7796 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7797 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7798 hapd->conf->wps_nfc_dev_pw =
7799 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7800 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7802 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7803 wpa_dbg(iface, MSG_DEBUG,
7804 "P2P: Failed to enable NFC Tag for GO");
7807 p2p_set_authorized_oob_dev_pw_id(
7808 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7814 #endif /* CONFIG_WPS_NFC */
7817 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
7818 struct wpa_used_freq_data *freqs,
7821 u8 curr_chan, cand, chan;
7824 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
7825 for (i = 0, cand = 0; i < num; i++) {
7826 ieee80211_freq_to_chan(freqs[i].freq, &chan);
7827 if (curr_chan == chan) {
7832 if (chan == 1 || chan == 6 || chan == 11)
7837 wpa_dbg(wpa_s, MSG_DEBUG,
7838 "P2P: Update Listen channel to %u baased on operating channel",
7840 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
7845 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
7847 struct wpa_used_freq_data *freqs;
7848 unsigned int num = wpa_s->num_multichan_concurrent;
7850 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7854 * If possible, optimize the Listen channel to be a channel that is
7855 * already used by one of the other interfaces.
7857 if (!wpa_s->conf->p2p_optimize_listen_chan)
7860 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
7863 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7867 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7869 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7874 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
7876 if (wpa_s == wpa_s->parent)
7877 wpas_p2p_group_remove(wpa_s, "*");
7878 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
7879 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
7880 "the management interface is being removed");
7881 wpas_p2p_deinit_global(wpa_s->global);
7886 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
7888 if (wpa_s->ap_iface->bss)
7889 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
7890 wpas_p2p_group_deinit(wpa_s);