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 int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
122 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
123 static void wpas_stop_listen(void *ctx);
124 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
125 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
126 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
127 enum wpa_driver_if_type type);
131 * Get the number of concurrent channels that the HW can operate, but that are
132 * currently not in use by any of the wpa_supplicant interfaces.
134 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
139 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
143 num = get_shared_radio_freqs(wpa_s, freqs,
144 wpa_s->num_multichan_concurrent);
147 unused = wpa_s->num_multichan_concurrent - num;
148 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
154 * Get the frequencies that are currently in use by one or more of the virtual
155 * interfaces, and that are also valid for P2P operation.
158 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
159 struct wpa_used_freq_data *p2p_freqs,
162 struct wpa_used_freq_data *freqs;
163 unsigned int num, i, j;
165 freqs = os_calloc(wpa_s->num_multichan_concurrent,
166 sizeof(struct wpa_used_freq_data));
170 num = get_shared_radio_freqs_data(wpa_s, freqs,
171 wpa_s->num_multichan_concurrent);
173 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
175 for (i = 0, j = 0; i < num && j < len; i++) {
176 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
177 p2p_freqs[j++] = freqs[i];
182 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
188 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
191 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
193 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
194 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
195 wpas_p2p_num_unused_channels(wpa_s) > 0) {
196 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
200 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
204 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
205 struct wpa_scan_results *scan_res)
209 if (wpa_s->p2p_scan_work) {
210 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
211 wpa_s->p2p_scan_work = NULL;
212 radio_work_done(work);
215 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
218 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
219 (int) scan_res->num);
221 for (i = 0; i < scan_res->num; i++) {
222 struct wpa_scan_res *bss = scan_res->res[i];
223 struct os_reltime time_tmp_age, entry_ts;
227 time_tmp_age.sec = bss->age / 1000;
228 time_tmp_age.usec = (bss->age % 1000) * 1000;
229 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
231 ies = (const u8 *) (bss + 1);
232 ies_len = bss->ie_len;
233 if (bss->beacon_ie_len > 0 &&
234 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
235 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
236 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
237 MACSTR, MAC2STR(bss->bssid));
239 ies_len = bss->beacon_ie_len;
243 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
244 bss->freq, &entry_ts, bss->level,
249 p2p_scan_res_handled(wpa_s->global->p2p);
253 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
255 struct wpa_supplicant *wpa_s = work->wpa_s;
256 struct wpa_driver_scan_params *params = work->ctx;
260 if (!work->started) {
261 wpa_scan_free_params(params);
265 wpa_s->p2p_scan_work = NULL;
269 ret = wpa_drv_scan(wpa_s, params);
270 wpa_scan_free_params(params);
273 radio_work_done(work);
274 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
278 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
279 os_get_reltime(&wpa_s->scan_trigger_time);
280 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
281 wpa_s->own_scan_requested = 1;
282 wpa_s->p2p_scan_work = work;
286 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
289 if (wpa_s->global->p2p_24ghz_social_channels &&
290 (freq == 2412 || freq == 2437 || freq == 2462)) {
292 * Search all social channels regardless of whether these have
293 * been disabled for P2P operating channel use to avoid missing
298 return p2p_supported_freq(wpa_s->global->p2p, freq);
302 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
303 unsigned int num_req_dev_types,
304 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
306 struct wpa_supplicant *wpa_s = ctx;
307 struct wpa_driver_scan_params *params = NULL;
308 struct wpabuf *wps_ie, *ies;
309 unsigned int num_channels = 0;
310 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
314 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
317 if (wpa_s->p2p_scan_work) {
318 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
322 params = os_zalloc(sizeof(*params));
326 /* P2P Wildcard SSID */
327 params->num_ssids = 1;
328 n = os_malloc(P2P_WILDCARD_SSID_LEN);
331 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
332 params->ssids[0].ssid = n;
333 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
335 wpa_s->wps->dev.p2p = 1;
336 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
337 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
338 num_req_dev_types, req_dev_types);
342 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
343 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
348 wpabuf_put_buf(ies, wps_ie);
351 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
353 params->p2p_probe = 1;
354 n = os_malloc(wpabuf_len(ies));
359 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
360 params->extra_ies = n;
361 params->extra_ies_len = wpabuf_len(ies);
365 case P2P_SCAN_SOCIAL:
366 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
368 if (params->freqs == NULL)
370 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
371 if (wpas_p2p_search_social_channel(
372 wpa_s, social_channels_freq[i]))
373 params->freqs[num_channels++] =
374 social_channels_freq[i];
376 params->freqs[num_channels++] = 0;
380 case P2P_SCAN_SPECIFIC:
381 params->freqs = os_calloc(2, sizeof(int));
382 if (params->freqs == NULL)
384 params->freqs[0] = freq;
385 params->freqs[1] = 0;
387 case P2P_SCAN_SOCIAL_PLUS_ONE:
388 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
390 if (params->freqs == NULL)
392 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
393 if (wpas_p2p_search_social_channel(
394 wpa_s, social_channels_freq[i]))
395 params->freqs[num_channels++] =
396 social_channels_freq[i];
398 if (p2p_supported_freq(wpa_s->global->p2p, freq))
399 params->freqs[num_channels++] = freq;
400 params->freqs[num_channels++] = 0;
404 radio_remove_works(wpa_s, "p2p-scan", 0);
405 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
411 wpa_scan_free_params(params);
416 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
418 switch (p2p_group_interface) {
419 case P2P_GROUP_INTERFACE_PENDING:
420 return WPA_IF_P2P_GROUP;
421 case P2P_GROUP_INTERFACE_GO:
422 return WPA_IF_P2P_GO;
423 case P2P_GROUP_INTERFACE_CLIENT:
424 return WPA_IF_P2P_CLIENT;
427 return WPA_IF_P2P_GROUP;
431 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
433 size_t ssid_len, int *go)
437 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
438 for (s = wpa_s->conf->ssid; s; s = s->next) {
439 if (s->disabled != 0 || !s->p2p_group ||
440 s->ssid_len != ssid_len ||
441 os_memcmp(ssid, s->ssid, ssid_len) != 0)
443 if (s->mode == WPAS_MODE_P2P_GO &&
444 s != wpa_s->current_ssid)
447 *go = s->mode == WPAS_MODE_P2P_GO;
456 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
458 struct wpa_supplicant *wpa_s = eloop_ctx;
459 wpa_printf(MSG_DEBUG,
460 "P2P: Complete previously requested removal of %s",
462 wpas_p2p_disconnect(wpa_s);
466 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
467 struct wpa_supplicant *calling_wpa_s)
469 if (calling_wpa_s == wpa_s && wpa_s &&
470 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
472 * The calling wpa_s instance is going to be removed. Do that
473 * from an eloop callback to keep the instance available until
474 * the caller has returned. This my be needed, e.g., to provide
475 * control interface responses on the per-interface socket.
477 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
483 return wpas_p2p_disconnect(wpa_s);
487 /* Determine total number of clients in active groups where we are the GO */
488 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
490 unsigned int count = 0;
493 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
494 for (s = wpa_s->conf->ssid; s; s = s->next) {
495 wpa_printf(MSG_DEBUG,
496 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
497 wpa_s, s, s->disabled, s->p2p_group,
499 if (!s->disabled && s->p2p_group &&
500 s->mode == WPAS_MODE_P2P_GO) {
501 count += p2p_get_group_num_members(
511 /* Find an interface for a P2P group where we are the GO */
512 static struct wpa_supplicant *
513 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
515 struct wpa_supplicant *save = NULL;
521 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
522 for (s = wpa_s->conf->ssid; s; s = s->next) {
523 if (s->disabled || !s->p2p_group ||
524 s->mode != WPAS_MODE_P2P_GO)
527 /* Prefer a group with connected clients */
528 if (p2p_get_group_num_members(wpa_s->p2p_group))
534 /* No group with connected clients, so pick the one without (if any) */
539 /* Find an active P2P group where we are the GO */
540 static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
543 struct wpa_ssid *s, *empty = NULL;
548 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
549 for (s = wpa_s->conf->ssid; s; s = s->next) {
550 if (s->disabled || !s->p2p_group ||
551 s->mode != WPAS_MODE_P2P_GO)
554 os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
555 if (p2p_get_group_num_members(wpa_s->p2p_group))
565 /* Find a persistent group where we are the GO */
566 static struct wpa_ssid *
567 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
571 for (s = wpa_s->conf->ssid; s; s = s->next) {
572 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
580 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
582 struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
584 u8 conncap = P2PS_SETUP_NONE;
585 unsigned int owned_members = 0;
586 unsigned int owner = 0;
587 unsigned int client = 0;
588 struct wpa_supplicant *go_wpa_s;
589 struct wpa_ssid *persistent_go;
590 int p2p_no_group_iface;
592 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
595 * For non-concurrent capable devices:
596 * If persistent_go, then no new.
597 * If GO, then no client.
598 * If client, then no GO.
600 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
601 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
602 p2p_no_group_iface = wpa_s->conf->p2p_no_group_iface;
604 wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
605 go_wpa_s, persistent_go);
607 for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
608 tmp_wpa_s = tmp_wpa_s->next) {
609 for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
610 wpa_printf(MSG_DEBUG,
611 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
612 tmp_wpa_s, s, s->disabled,
613 s->p2p_group, s->mode);
614 if (!s->disabled && s->p2p_group) {
615 if (s->mode == WPAS_MODE_P2P_GO) {
617 p2p_get_group_num_members(
618 tmp_wpa_s->p2p_group);
626 /* If not concurrent, restrict our choices */
627 if (p2p_no_group_iface) {
628 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
631 return P2PS_SETUP_NONE;
634 if (role == P2PS_SETUP_CLIENT ||
635 incoming == P2PS_SETUP_GROUP_OWNER ||
636 p2p_client_limit_reached(go_wpa_s->p2p_group))
637 return P2PS_SETUP_NONE;
639 return P2PS_SETUP_GROUP_OWNER;
643 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
645 return P2PS_SETUP_GROUP_OWNER |
647 if (incoming == P2PS_SETUP_NEW) {
650 if (os_get_random(&r, sizeof(r)) < 0 ||
652 return P2PS_SETUP_CLIENT;
653 return P2PS_SETUP_GROUP_OWNER;
659 /* If a required role has been specified, handle it here */
660 if (role && role != P2PS_SETUP_NEW) {
662 case P2PS_SETUP_NONE:
664 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
665 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
669 case P2PS_SETUP_GROUP_OWNER:
671 * Must be a complimentary role - cannot be a client to
672 * more than one peer.
674 if (incoming == role || client)
675 return P2PS_SETUP_NONE;
677 return P2PS_SETUP_CLIENT;
679 case P2PS_SETUP_CLIENT:
680 /* Must be a complimentary role */
681 if (incoming != role) {
682 conncap = P2PS_SETUP_GROUP_OWNER;
687 return P2PS_SETUP_NONE;
692 * For now, we only will support ownership of one group, and being a
693 * client of one group. Therefore, if we have either an existing GO
694 * group, or an existing client group, we will not do a new GO
695 * negotiation, but rather try to re-use the existing groups.
698 case P2PS_SETUP_NONE:
701 conncap = P2PS_SETUP_GROUP_OWNER;
702 else if (!owned_members)
703 conncap = P2PS_SETUP_NEW;
704 else if (incoming == P2PS_SETUP_NONE)
705 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
707 conncap = P2PS_SETUP_CLIENT;
710 case P2PS_SETUP_CLIENT:
711 conncap = P2PS_SETUP_GROUP_OWNER;
714 case P2PS_SETUP_GROUP_OWNER:
716 conncap = P2PS_SETUP_CLIENT;
719 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
720 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
722 conncap = P2PS_SETUP_GROUP_OWNER;
726 if (os_get_random(&r, sizeof(r)) < 0 ||
728 conncap = P2PS_SETUP_CLIENT;
730 conncap = P2PS_SETUP_GROUP_OWNER;
735 return P2PS_SETUP_NONE;
739 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
740 (!incoming && (conncap & P2PS_SETUP_NEW))) {
741 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
742 conncap &= ~P2PS_SETUP_GROUP_OWNER;
743 wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
744 owner, owned_members, conncap);
746 s = wpas_p2p_get_persistent_go(wpa_s);
748 if (!s && !owner && p2p_no_group_iface) {
749 p2p_set_intended_addr(wpa_s->global->p2p,
751 } else if (!s && !owner) {
752 if (wpas_p2p_add_group_interface(wpa_s,
753 WPA_IF_P2P_GO) < 0) {
754 wpa_printf(MSG_ERROR,
755 "P2P: Failed to allocate a new interface for the group");
756 return P2PS_SETUP_NONE;
758 wpa_s->global->pending_group_iface_for_p2ps = 1;
759 p2p_set_intended_addr(wpa_s->global->p2p,
760 wpa_s->pending_interface_addr);
768 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
769 enum p2p_group_removal_reason removal_reason)
771 struct wpa_ssid *ssid;
775 ssid = wpa_s->current_ssid;
778 * The current SSID was not known, but there may still be a
779 * pending P2P group interface waiting for provisioning or a
780 * P2P group that is trying to reconnect.
782 ssid = wpa_s->conf->ssid;
784 if (ssid->p2p_group && ssid->disabled != 2)
789 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
791 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
796 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
798 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
799 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
800 wpa_s->reassociate = 0;
801 wpa_s->disconnected = 1;
806 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
807 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
809 if (os_strcmp(gtype, "client") == 0) {
810 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
811 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
813 wpa_printf(MSG_DEBUG,
814 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
815 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
816 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
821 if (wpa_s->cross_connect_in_use) {
822 wpa_s->cross_connect_in_use = 0;
823 wpa_msg_global(wpa_s->parent, MSG_INFO,
824 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
825 wpa_s->ifname, wpa_s->cross_connect_uplink);
827 switch (removal_reason) {
828 case P2P_GROUP_REMOVAL_REQUESTED:
829 reason = " reason=REQUESTED";
831 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
832 reason = " reason=FORMATION_FAILED";
834 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
835 reason = " reason=IDLE";
837 case P2P_GROUP_REMOVAL_UNAVAILABLE:
838 reason = " reason=UNAVAILABLE";
840 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
841 reason = " reason=GO_ENDING_SESSION";
843 case P2P_GROUP_REMOVAL_PSK_FAILURE:
844 reason = " reason=PSK_FAILURE";
846 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
847 reason = " reason=FREQ_CONFLICT";
853 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
854 wpa_msg_global(wpa_s->parent, MSG_INFO,
855 P2P_EVENT_GROUP_REMOVED "%s %s%s",
856 wpa_s->ifname, gtype, reason);
859 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
860 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
861 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
862 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
863 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
864 wpa_s->parent, NULL) > 0) {
865 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
867 wpa_s->p2p_in_provisioning = 0;
870 wpa_s->p2p_in_invitation = 0;
873 * Make sure wait for the first client does not remain active after the
874 * group has been removed.
876 wpa_s->global->p2p_go_wait_client.sec = 0;
878 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
879 struct wpa_global *global;
881 enum wpa_driver_if_type type;
882 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
884 global = wpa_s->global;
885 ifname = os_strdup(wpa_s->ifname);
886 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
887 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
888 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
889 wpa_s = global->ifaces;
891 wpa_drv_if_remove(wpa_s, type, ifname);
896 if (!wpa_s->p2p_go_group_formation_completed) {
897 wpa_s->global->p2p_group_formation = NULL;
898 wpa_s->p2p_in_provisioning = 0;
901 wpa_s->show_group_started = 0;
902 os_free(wpa_s->go_params);
903 wpa_s->go_params = NULL;
905 os_free(wpa_s->p2p_group_common_freqs);
906 wpa_s->p2p_group_common_freqs = NULL;
907 wpa_s->p2p_group_common_freqs_num = 0;
909 wpa_s->waiting_presence_resp = 0;
911 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
912 if (ssid && (ssid->p2p_group ||
913 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
914 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
916 if (ssid == wpa_s->current_ssid) {
917 wpa_sm_set_config(wpa_s->wpa, NULL);
918 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
919 wpa_s->current_ssid = NULL;
922 * Networks objects created during any P2P activities are not
923 * exposed out as they might/will confuse certain non-P2P aware
924 * applications since these network objects won't behave like
927 * Likewise, we don't send out network removed signals for such
930 wpa_config_remove_network(wpa_s->conf, id);
931 wpa_supplicant_clear_status(wpa_s);
932 wpa_supplicant_cancel_sched_scan(wpa_s);
934 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
938 wpa_supplicant_ap_deinit(wpa_s);
940 wpa_drv_deinit_p2p_cli(wpa_s);
946 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
948 const u8 *ssid, size_t ssid_len)
956 if (wpa_s->go_params)
957 bssid = wpa_s->go_params->peer_interface_addr;
959 bssid = wpa_s->bssid;
961 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
962 if (bss == NULL && wpa_s->go_params &&
963 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
964 bss = wpa_bss_get_p2p_dev_addr(
965 wpa_s, wpa_s->go_params->peer_device_addr);
967 u8 iface_addr[ETH_ALEN];
968 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
970 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
973 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
974 "group is persistent - BSS " MACSTR " not found",
979 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
981 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
984 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
985 "group is persistent - BSS " MACSTR
986 " did not include P2P IE", MAC2STR(bssid));
987 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
988 (u8 *) (bss + 1), bss->ie_len);
989 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
990 ((u8 *) bss + 1) + bss->ie_len,
995 group_capab = p2p_get_group_capab(p2p);
996 addr = p2p_get_go_dev_addr(p2p);
997 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
998 "group_capab=0x%x", group_capab);
1000 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1001 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1004 os_memset(go_dev_addr, 0, ETH_ALEN);
1007 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1008 "go_dev_addr=" MACSTR,
1009 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1011 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1015 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1016 struct wpa_ssid *ssid,
1017 const u8 *go_dev_addr)
1022 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1023 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1024 for (s = wpa_s->conf->ssid; s; s = s->next) {
1025 if (s->disabled == 2 &&
1026 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1027 s->ssid_len == ssid->ssid_len &&
1028 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1033 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1035 if (ssid->passphrase && !s->passphrase)
1037 else if (ssid->passphrase && s->passphrase &&
1038 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1041 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1044 s = wpa_config_add_network(wpa_s->conf);
1049 * Instead of network_added we emit persistent_group_added
1050 * notification. Also to keep the defense checks in
1051 * persistent_group obj registration method, we set the
1052 * relevant flags in s to designate it as a persistent group.
1055 s->p2p_persistent_group = 1;
1056 wpas_notify_persistent_group_added(wpa_s, s);
1057 wpa_config_set_network_defaults(s);
1061 s->p2p_persistent_group = 1;
1064 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1065 s->mode = ssid->mode;
1066 s->auth_alg = WPA_AUTH_ALG_OPEN;
1067 s->key_mgmt = WPA_KEY_MGMT_PSK;
1068 s->proto = WPA_PROTO_RSN;
1069 s->pairwise_cipher = WPA_CIPHER_CCMP;
1071 if (ssid->passphrase) {
1072 os_free(s->passphrase);
1073 s->passphrase = os_strdup(ssid->passphrase);
1075 if (ssid->psk_set) {
1077 os_memcpy(s->psk, ssid->psk, 32);
1079 if (s->passphrase && !s->psk_set)
1080 wpa_config_update_psk(s);
1081 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1083 s->ssid = os_malloc(ssid->ssid_len);
1086 s->ssid_len = ssid->ssid_len;
1087 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1089 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1090 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1091 wpa_s->global->add_psk = NULL;
1095 if (changed && wpa_s->conf->update_config &&
1096 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1097 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1104 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1107 struct wpa_ssid *ssid, *s;
1112 ssid = wpa_s->current_ssid;
1113 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1114 !ssid->p2p_persistent_group)
1117 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
1118 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1121 if (s->ssid_len == ssid->ssid_len &&
1122 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1129 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
1130 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
1134 if (i == s->num_p2p_clients - 1)
1135 return; /* already the most recent entry */
1137 /* move the entry to mark it most recent */
1138 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1139 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1140 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
1141 os_memcpy(s->p2p_client_list +
1142 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1144 os_memset(s->p2p_client_list +
1145 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1151 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1152 n = os_realloc_array(s->p2p_client_list,
1153 s->num_p2p_clients + 1, 2 * ETH_ALEN);
1156 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1158 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1160 s->p2p_client_list = n;
1161 s->num_p2p_clients++;
1162 } else if (!found && s->p2p_client_list) {
1163 /* Not enough room for an additional entry - drop the oldest
1165 os_memmove(s->p2p_client_list,
1166 s->p2p_client_list + 2 * ETH_ALEN,
1167 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1168 os_memcpy(s->p2p_client_list +
1169 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1171 os_memset(s->p2p_client_list +
1172 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1176 if (wpa_s->parent->conf->update_config &&
1177 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1178 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1182 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1183 int go, struct wpa_ssid *ssid, int freq,
1184 const u8 *psk, const char *passphrase,
1185 const u8 *go_dev_addr, int persistent,
1188 const char *ssid_txt;
1192 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1197 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1201 if (passphrase && passphrase[0] == '\0')
1205 * Include PSK/passphrase only in the control interface message and
1206 * leave it out from the debug log entry.
1208 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1209 P2P_EVENT_GROUP_STARTED
1210 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1212 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1213 psk ? " psk=" : "", psk_txt,
1214 passphrase ? " passphrase=\"" : "",
1215 passphrase ? passphrase : "",
1216 passphrase ? "\"" : "",
1217 MAC2STR(go_dev_addr),
1218 persistent ? " [PERSISTENT]" : "", extra);
1219 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1220 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1221 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1222 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1227 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1230 struct wpa_ssid *ssid;
1233 u8 go_dev_addr[ETH_ALEN];
1234 int network_id = -1;
1237 * This callback is likely called for the main interface. Update wpa_s
1238 * to use the group interface if a new interface was created for the
1241 if (wpa_s->global->p2p_group_formation)
1242 wpa_s = wpa_s->global->p2p_group_formation;
1243 if (wpa_s->p2p_go_group_formation_completed) {
1244 wpa_s->global->p2p_group_formation = NULL;
1245 wpa_s->p2p_in_provisioning = 0;
1247 wpa_s->p2p_in_invitation = 0;
1248 wpa_s->group_formation_reported = 1;
1251 wpa_msg_global(wpa_s->parent, MSG_INFO,
1252 P2P_EVENT_GROUP_FORMATION_FAILURE);
1253 wpas_p2p_group_delete(wpa_s,
1254 P2P_GROUP_REMOVAL_FORMATION_FAILED);
1258 wpa_msg_global(wpa_s->parent, MSG_INFO,
1259 P2P_EVENT_GROUP_FORMATION_SUCCESS);
1261 ssid = wpa_s->current_ssid;
1262 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1263 ssid->mode = WPAS_MODE_P2P_GO;
1264 p2p_group_notif_formation_done(wpa_s->p2p_group);
1265 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1270 client = ssid->mode == WPAS_MODE_INFRA;
1271 if (ssid->mode == WPAS_MODE_P2P_GO) {
1272 persistent = ssid->p2p_persistent_group;
1273 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1276 persistent = wpas_p2p_persistent_group(wpa_s,
1281 client = wpa_s->p2p_group_interface ==
1282 P2P_GROUP_INTERFACE_CLIENT;
1283 os_memset(go_dev_addr, 0, ETH_ALEN);
1286 wpa_s->show_group_started = 0;
1289 * Indicate event only after successfully completed 4-way
1290 * handshake, i.e., when the interface is ready for data
1293 wpa_s->show_group_started = 1;
1295 wpas_p2p_group_started(wpa_s, 1, ssid,
1296 ssid ? ssid->frequency : 0,
1297 ssid && ssid->passphrase == NULL &&
1298 ssid->psk_set ? ssid->psk : NULL,
1299 ssid ? ssid->passphrase : NULL,
1300 go_dev_addr, persistent, "");
1301 wpas_p2p_cross_connect_setup(wpa_s);
1302 wpas_p2p_set_group_idle_timeout(wpa_s);
1306 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1309 os_free(wpa_s->global->add_psk);
1310 wpa_s->global->add_psk = NULL;
1312 if (network_id < 0 && ssid)
1313 network_id = ssid->id;
1315 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1316 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1321 struct send_action_work {
1327 unsigned int wait_time;
1332 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1335 struct wpa_supplicant *wpa_s = eloop_ctx;
1337 if (!wpa_s->p2p_send_action_work)
1340 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1341 os_free(wpa_s->p2p_send_action_work->ctx);
1342 radio_work_done(wpa_s->p2p_send_action_work);
1343 wpa_s->p2p_send_action_work = NULL;
1347 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1349 if (wpa_s->p2p_send_action_work) {
1350 struct send_action_work *awork;
1351 awork = wpa_s->p2p_send_action_work->ctx;
1352 if (awork->wait_time == 0) {
1354 radio_work_done(wpa_s->p2p_send_action_work);
1355 wpa_s->p2p_send_action_work = NULL;
1358 * In theory, this should not be needed, but number of
1359 * places in the P2P code is still using non-zero wait
1360 * time for the last Action frame in the sequence and
1361 * some of these do not call send_action_done().
1363 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1365 eloop_register_timeout(
1366 0, awork->wait_time * 1000,
1367 wpas_p2p_send_action_work_timeout,
1374 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1376 const u8 *dst, const u8 *src,
1378 const u8 *data, size_t data_len,
1379 enum offchannel_send_action_result
1382 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1384 wpas_p2p_action_tx_clear(wpa_s);
1386 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1390 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1391 res = P2P_SEND_ACTION_SUCCESS;
1393 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1394 res = P2P_SEND_ACTION_NO_ACK;
1396 case OFFCHANNEL_SEND_ACTION_FAILED:
1397 res = P2P_SEND_ACTION_FAILED;
1401 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1403 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1404 wpa_s->pending_pd_before_join &&
1405 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1406 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1407 wpa_s->p2p_fallback_to_go_neg) {
1408 wpa_s->pending_pd_before_join = 0;
1409 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1410 "during p2p_connect-auto");
1411 wpa_msg_global(wpa_s->parent, MSG_INFO,
1412 P2P_EVENT_FALLBACK_TO_GO_NEG
1413 "reason=no-ACK-to-PD-Req");
1414 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1420 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1422 struct wpa_supplicant *wpa_s = work->wpa_s;
1423 struct send_action_work *awork = work->ctx;
1426 if (work->started) {
1427 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1429 wpa_s->p2p_send_action_work = NULL;
1430 offchannel_send_action_done(wpa_s);
1436 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1437 awork->bssid, awork->buf, awork->len,
1439 wpas_p2p_send_action_tx_status, 1) < 0) {
1441 radio_work_done(work);
1444 wpa_s->p2p_send_action_work = work;
1448 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1449 unsigned int freq, const u8 *dst,
1450 const u8 *src, const u8 *bssid, const u8 *buf,
1451 size_t len, unsigned int wait_time)
1453 struct send_action_work *awork;
1455 if (wpa_s->p2p_send_action_work) {
1456 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1460 awork = os_zalloc(sizeof(*awork) + len);
1465 os_memcpy(awork->dst, dst, ETH_ALEN);
1466 os_memcpy(awork->src, src, ETH_ALEN);
1467 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1469 awork->wait_time = wait_time;
1470 os_memcpy(awork->buf, buf, len);
1472 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1473 wpas_send_action_cb, awork) < 0) {
1482 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1483 const u8 *src, const u8 *bssid, const u8 *buf,
1484 size_t len, unsigned int wait_time)
1486 struct wpa_supplicant *wpa_s = ctx;
1487 int listen_freq = -1, send_freq = -1;
1489 if (wpa_s->p2p_listen_work)
1490 listen_freq = wpa_s->p2p_listen_work->freq;
1491 if (wpa_s->p2p_send_action_work)
1492 send_freq = wpa_s->p2p_send_action_work->freq;
1493 if (listen_freq != (int) freq && send_freq != (int) freq) {
1494 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1495 listen_freq, send_freq);
1496 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1500 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1501 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1503 wpas_p2p_send_action_tx_status, 1);
1507 static void wpas_send_action_done(void *ctx)
1509 struct wpa_supplicant *wpa_s = ctx;
1511 if (wpa_s->p2p_send_action_work) {
1512 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1514 os_free(wpa_s->p2p_send_action_work->ctx);
1515 radio_work_done(wpa_s->p2p_send_action_work);
1516 wpa_s->p2p_send_action_work = NULL;
1519 offchannel_send_action_done(wpa_s);
1523 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1524 struct p2p_go_neg_results *params)
1526 if (wpa_s->go_params == NULL) {
1527 wpa_s->go_params = os_malloc(sizeof(*params));
1528 if (wpa_s->go_params == NULL)
1531 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1536 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1537 struct p2p_go_neg_results *res)
1539 wpa_s->group_formation_reported = 0;
1540 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1541 " dev_addr " MACSTR " wps_method %d",
1542 MAC2STR(res->peer_interface_addr),
1543 MAC2STR(res->peer_device_addr), res->wps_method);
1544 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1545 res->ssid, res->ssid_len);
1546 wpa_supplicant_ap_deinit(wpa_s);
1547 wpas_copy_go_neg_results(wpa_s, res);
1548 if (res->wps_method == WPS_PBC) {
1549 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1550 #ifdef CONFIG_WPS_NFC
1551 } else if (res->wps_method == WPS_NFC) {
1552 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1553 res->peer_interface_addr,
1554 wpa_s->parent->p2p_oob_dev_pw,
1555 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1556 wpa_s->parent->p2p_oob_dev_pw_id ==
1557 DEV_PW_NFC_CONNECTION_HANDOVER ?
1558 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1561 #endif /* CONFIG_WPS_NFC */
1563 u16 dev_pw_id = DEV_PW_DEFAULT;
1564 if (wpa_s->p2p_wps_method == WPS_P2PS)
1565 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1566 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1567 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1568 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1569 wpa_s->p2p_pin, 1, dev_pw_id);
1574 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1575 struct wpa_ssid *ssid)
1577 struct wpa_ssid *persistent;
1578 struct psk_list_entry *psk;
1579 struct hostapd_data *hapd;
1581 if (!wpa_s->ap_iface)
1584 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1586 if (persistent == NULL)
1589 hapd = wpa_s->ap_iface->bss[0];
1591 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1593 struct hostapd_wpa_psk *hpsk;
1595 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1597 MAC2STR(psk->addr), psk->p2p);
1598 hpsk = os_zalloc(sizeof(*hpsk));
1601 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1603 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1605 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1606 hpsk->next = hapd->conf->ssid.wpa_psk;
1607 hapd->conf->ssid.wpa_psk = hpsk;
1612 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1616 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1617 wpa_s->p2p_group_common_freqs_num);
1619 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1620 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1621 i, wpa_s->p2p_group_common_freqs[i]);
1625 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1626 struct p2p_go_neg_results *params)
1628 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1630 wpa_s->p2p_group_common_freqs_num = 0;
1631 os_free(wpa_s->p2p_group_common_freqs);
1632 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1633 if (!wpa_s->p2p_group_common_freqs)
1636 for (i = 0; i < len; i++) {
1637 if (!wpa_s->go_params->freq_list[i])
1639 wpa_s->p2p_group_common_freqs[i] =
1640 wpa_s->go_params->freq_list[i];
1642 wpa_s->p2p_group_common_freqs_num = i;
1646 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1648 #ifndef CONFIG_NO_CONFIG_WRITE
1649 if (wpa_s->parent->conf->update_config &&
1650 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1651 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1652 #endif /* CONFIG_NO_CONFIG_WRITE */
1656 static void p2p_go_configured(void *ctx, void *data)
1658 struct wpa_supplicant *wpa_s = ctx;
1659 struct p2p_go_neg_results *params = data;
1660 struct wpa_ssid *ssid;
1661 int network_id = -1;
1663 p2p_go_save_group_common_freqs(wpa_s, params);
1664 p2p_go_dump_common_freqs(wpa_s);
1666 ssid = wpa_s->current_ssid;
1667 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1668 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1669 if (wpa_s->global->p2p_group_formation == wpa_s)
1670 wpa_s->global->p2p_group_formation = NULL;
1671 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1672 params->passphrase[0] == '\0' ?
1675 wpa_s->global->p2p_dev_addr,
1676 params->persistent_group, "");
1677 wpa_s->group_formation_reported = 1;
1679 if (wpa_s->parent->p2ps_join_addr_valid) {
1680 wpa_dbg(wpa_s, MSG_DEBUG,
1681 "P2PS: Setting default PIN for " MACSTR,
1682 MAC2STR(wpa_s->parent->p2ps_join_addr));
1683 wpa_supplicant_ap_wps_pin(wpa_s,
1684 wpa_s->parent->p2ps_join_addr,
1685 "12345670", NULL, 0, 0);
1686 wpa_s->parent->p2ps_join_addr_valid = 0;
1689 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1690 if (params->persistent_group) {
1691 network_id = wpas_p2p_store_persistent_group(
1692 wpa_s->parent, ssid,
1693 wpa_s->global->p2p_dev_addr);
1694 wpas_p2p_add_psk_list(wpa_s, ssid);
1697 network_id = ssid->id;
1698 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1699 wpas_p2p_cross_connect_setup(wpa_s);
1700 wpas_p2p_set_group_idle_timeout(wpa_s);
1702 if (wpa_s->p2p_first_connection_timeout) {
1703 wpa_dbg(wpa_s, MSG_DEBUG,
1704 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1705 wpa_s->p2p_first_connection_timeout);
1706 wpa_s->p2p_go_group_formation_completed = 0;
1707 wpa_s->global->p2p_group_formation = wpa_s;
1708 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1709 wpa_s->parent, NULL);
1710 eloop_register_timeout(
1711 wpa_s->p2p_first_connection_timeout, 0,
1712 wpas_p2p_group_formation_timeout,
1713 wpa_s->parent, NULL);
1719 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1720 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1721 params->peer_interface_addr)) {
1722 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1726 if (params->wps_method == WPS_PBC) {
1727 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1728 params->peer_device_addr);
1729 #ifdef CONFIG_WPS_NFC
1730 } else if (params->wps_method == WPS_NFC) {
1731 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1732 DEV_PW_NFC_CONNECTION_HANDOVER &&
1733 !wpa_s->parent->p2p_oob_dev_pw) {
1734 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1737 wpas_ap_wps_add_nfc_pw(
1738 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1739 wpa_s->parent->p2p_oob_dev_pw,
1740 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1741 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1742 #endif /* CONFIG_WPS_NFC */
1743 } else if (wpa_s->p2p_pin[0])
1744 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1745 wpa_s->p2p_pin, NULL, 0, 0);
1746 os_free(wpa_s->go_params);
1747 wpa_s->go_params = NULL;
1751 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1752 struct p2p_go_neg_results *params,
1753 int group_formation)
1755 struct wpa_ssid *ssid;
1757 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1758 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1759 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1764 ssid = wpa_config_add_network(wpa_s->conf);
1766 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1770 wpa_s->show_group_started = 0;
1771 wpa_s->p2p_go_group_formation_completed = 0;
1772 wpa_s->group_formation_reported = 0;
1774 wpa_config_set_network_defaults(ssid);
1775 ssid->temporary = 1;
1776 ssid->p2p_group = 1;
1777 ssid->p2p_persistent_group = params->persistent_group;
1778 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1780 ssid->frequency = params->freq;
1781 ssid->ht40 = params->ht40;
1782 ssid->vht = params->vht;
1783 ssid->ssid = os_zalloc(params->ssid_len + 1);
1785 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1786 ssid->ssid_len = params->ssid_len;
1788 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1789 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1790 ssid->proto = WPA_PROTO_RSN;
1791 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1792 ssid->group_cipher = WPA_CIPHER_CCMP;
1793 if (params->freq > 56160) {
1795 * Enable GCMP instead of CCMP as pairwise_cipher and
1796 * group_cipher in 60 GHz.
1798 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1799 ssid->group_cipher = WPA_CIPHER_GCMP;
1801 if (os_strlen(params->passphrase) > 0) {
1802 ssid->passphrase = os_strdup(params->passphrase);
1803 if (ssid->passphrase == NULL) {
1804 wpa_msg_global(wpa_s, MSG_ERROR,
1805 "P2P: Failed to copy passphrase for GO");
1806 wpa_config_remove_network(wpa_s->conf, ssid->id);
1810 ssid->passphrase = NULL;
1811 ssid->psk_set = params->psk_set;
1813 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1814 else if (ssid->passphrase)
1815 wpa_config_update_psk(ssid);
1816 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1818 wpa_s->ap_configured_cb = p2p_go_configured;
1819 wpa_s->ap_configured_cb_ctx = wpa_s;
1820 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1821 wpa_s->scan_req = NORMAL_SCAN_REQ;
1822 wpa_s->connect_without_scan = ssid;
1823 wpa_s->reassociate = 1;
1824 wpa_s->disconnected = 0;
1825 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1827 wpa_supplicant_req_scan(wpa_s, 0, 0);
1831 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1832 const struct wpa_supplicant *src)
1834 struct wpa_config *d;
1835 const struct wpa_config *s;
1840 #define C(n) if (s->n) d->n = os_strdup(s->n)
1849 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1850 os_memcpy(d->sec_device_type, s->sec_device_type,
1851 sizeof(d->sec_device_type));
1852 d->num_sec_device_types = s->num_sec_device_types;
1854 d->p2p_group_idle = s->p2p_group_idle;
1855 d->p2p_intra_bss = s->p2p_intra_bss;
1856 d->persistent_reconnect = s->persistent_reconnect;
1857 d->max_num_sta = s->max_num_sta;
1858 d->pbc_in_m1 = s->pbc_in_m1;
1859 d->ignore_old_scan_res = s->ignore_old_scan_res;
1860 d->beacon_int = s->beacon_int;
1861 d->dtim_period = s->dtim_period;
1862 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
1863 d->disassoc_low_ack = s->disassoc_low_ack;
1864 d->disable_scan_offload = s->disable_scan_offload;
1865 d->passive_scan = s->passive_scan;
1867 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1868 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1869 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1874 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1875 char *ifname, size_t len)
1877 char *ifname_ptr = wpa_s->ifname;
1879 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1880 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1881 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1884 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1885 if (os_strlen(ifname) >= IFNAMSIZ &&
1886 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1889 /* Try to avoid going over the IFNAMSIZ length limit */
1890 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1891 if (os_snprintf_error(len, res) && len)
1892 ifname[len - 1] = '\0';
1897 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1898 enum wpa_driver_if_type type)
1900 char ifname[120], force_ifname[120];
1902 if (wpa_s->pending_interface_name[0]) {
1903 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1904 "- skip creation of a new one");
1905 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1906 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1907 "unknown?! ifname='%s'",
1908 wpa_s->pending_interface_name);
1914 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1915 force_ifname[0] = '\0';
1917 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1919 wpa_s->p2p_group_idx++;
1921 wpa_s->pending_interface_type = type;
1922 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1923 wpa_s->pending_interface_addr, NULL) < 0) {
1924 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1929 if (force_ifname[0]) {
1930 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1932 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1933 sizeof(wpa_s->pending_interface_name));
1935 os_strlcpy(wpa_s->pending_interface_name, ifname,
1936 sizeof(wpa_s->pending_interface_name));
1937 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1938 MACSTR, wpa_s->pending_interface_name,
1939 MAC2STR(wpa_s->pending_interface_addr));
1945 static void wpas_p2p_remove_pending_group_interface(
1946 struct wpa_supplicant *wpa_s)
1948 if (!wpa_s->pending_interface_name[0] ||
1949 is_zero_ether_addr(wpa_s->pending_interface_addr))
1950 return; /* No pending virtual interface */
1952 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1953 wpa_s->pending_interface_name);
1954 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1955 wpa_s->pending_interface_name);
1956 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1957 wpa_s->pending_interface_name[0] = '\0';
1958 wpa_s->global->pending_group_iface_for_p2ps = 0;
1962 static struct wpa_supplicant *
1963 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1965 struct wpa_interface iface;
1966 struct wpa_supplicant *group_wpa_s;
1968 if (!wpa_s->pending_interface_name[0]) {
1969 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1970 if (!wpas_p2p_create_iface(wpa_s))
1973 * Something has forced us to remove the pending interface; try
1974 * to create a new one and hope for the best that we will get
1975 * the same local address.
1977 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1978 WPA_IF_P2P_CLIENT) < 0)
1982 os_memset(&iface, 0, sizeof(iface));
1983 iface.ifname = wpa_s->pending_interface_name;
1984 iface.driver = wpa_s->driver->name;
1985 if (wpa_s->conf->ctrl_interface == NULL &&
1986 wpa_s->parent != wpa_s &&
1988 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1989 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1991 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1992 iface.driver_param = wpa_s->conf->driver_param;
1993 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
1994 if (group_wpa_s == NULL) {
1995 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1996 "wpa_supplicant interface");
1999 wpa_s->pending_interface_name[0] = '\0';
2000 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2001 P2P_GROUP_INTERFACE_CLIENT;
2002 wpa_s->global->p2p_group_formation = group_wpa_s;
2003 wpa_s->global->pending_group_iface_for_p2ps = 0;
2005 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2011 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2014 struct wpa_supplicant *wpa_s = eloop_ctx;
2015 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2016 wpas_p2p_group_formation_failed(wpa_s);
2020 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
2022 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2023 wpa_s->parent, NULL);
2024 if (wpa_s->global->p2p)
2025 p2p_group_formation_failed(wpa_s->global->p2p);
2026 wpas_group_formation_completed(wpa_s, 0);
2030 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2032 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2033 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2034 wpa_s->parent, NULL);
2035 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2036 wpa_s->parent, NULL);
2037 wpa_s->global->p2p_fail_on_wps_complete = 0;
2041 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2043 if (wpa_s->global->p2p_group_formation != wpa_s)
2045 /* Speed up group formation timeout since this cannot succeed */
2046 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2047 wpa_s->parent, NULL);
2048 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2049 wpa_s->parent, NULL);
2053 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2055 struct wpa_supplicant *wpa_s = ctx;
2057 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2058 wpa_drv_cancel_remain_on_channel(wpa_s);
2059 wpa_s->off_channel_freq = 0;
2060 wpa_s->roc_waiting_drv_freq = 0;
2064 wpa_msg_global(wpa_s, MSG_INFO,
2065 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2067 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2068 wpas_p2p_remove_pending_group_interface(wpa_s);
2072 if (wpa_s->p2p_go_ht40)
2074 if (wpa_s->p2p_go_vht)
2077 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2078 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2080 res->role_go ? "GO" : "client", res->freq, res->ht40,
2081 MAC2STR(res->peer_device_addr),
2082 MAC2STR(res->peer_interface_addr),
2083 p2p_wps_method_text(res->wps_method));
2084 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2086 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2087 struct wpa_ssid *ssid;
2088 ssid = wpa_config_get_network(wpa_s->conf,
2089 wpa_s->p2p_persistent_id);
2090 if (ssid && ssid->disabled == 2 &&
2091 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2092 size_t len = os_strlen(ssid->passphrase);
2093 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2094 "on requested persistent group");
2095 os_memcpy(res->passphrase, ssid->passphrase, len);
2096 res->passphrase[len] = '\0';
2100 if (wpa_s->create_p2p_iface) {
2101 struct wpa_supplicant *group_wpa_s =
2102 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2103 if (group_wpa_s == NULL) {
2104 wpas_p2p_remove_pending_group_interface(wpa_s);
2105 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2107 wpas_p2p_group_formation_failed(wpa_s);
2110 if (group_wpa_s != wpa_s) {
2111 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2112 sizeof(group_wpa_s->p2p_pin));
2113 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2115 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2116 wpa_s->pending_interface_name[0] = '\0';
2117 group_wpa_s->p2p_in_provisioning = 1;
2120 wpas_start_wps_go(group_wpa_s, res, 1);
2122 wpas_start_wps_enrollee(group_wpa_s, res);
2124 wpa_s->p2p_in_provisioning = 1;
2125 wpa_s->global->p2p_group_formation = wpa_s;
2128 wpas_start_wps_go(wpa_s, res, 1);
2130 wpas_start_wps_enrollee(ctx, res);
2133 wpa_s->p2p_long_listen = 0;
2134 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2136 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2137 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2138 (res->peer_config_timeout % 100) * 10000,
2139 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2143 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
2145 struct wpa_supplicant *wpa_s = ctx;
2146 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2147 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
2149 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
2153 static void wpas_dev_found(void *ctx, const u8 *addr,
2154 const struct p2p_peer_info *info,
2157 #ifndef CONFIG_NO_STDOUT_DEBUG
2158 struct wpa_supplicant *wpa_s = ctx;
2159 char devtype[WPS_DEV_TYPE_BUFSIZE];
2160 char *wfd_dev_info_hex = NULL;
2162 #ifdef CONFIG_WIFI_DISPLAY
2163 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2164 WFD_SUBELEM_DEVICE_INFO);
2165 #endif /* CONFIG_WIFI_DISPLAY */
2167 if (info->p2ps_instance) {
2169 const u8 *buf = wpabuf_head(info->p2ps_instance);
2170 size_t len = wpabuf_len(info->p2ps_instance);
2177 if (len < 4 + 2 + 1)
2179 id = WPA_GET_LE32(buf);
2181 methods = WPA_GET_BE16(buf);
2184 if (str_len > len - 4 - 2 - 1)
2186 os_memcpy(str, buf, str_len);
2187 str[str_len] = '\0';
2189 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2191 wpa_msg_global(wpa_s, MSG_INFO,
2192 P2P_EVENT_DEVICE_FOUND MACSTR
2193 " p2p_dev_addr=" MACSTR
2194 " pri_dev_type=%s name='%s'"
2195 " config_methods=0x%x"
2198 " adv_id=%x asp_svc=%s%s",
2200 MAC2STR(info->p2p_device_addr),
2201 wps_dev_type_bin2str(
2203 devtype, sizeof(devtype)),
2204 info->device_name, methods,
2205 info->dev_capab, info->group_capab,
2207 info->vendor_elems ?
2208 " vendor_elems=1" : "");
2213 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2214 " p2p_dev_addr=" MACSTR
2215 " pri_dev_type=%s name='%s' config_methods=0x%x "
2216 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2217 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2218 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2220 info->device_name, info->config_methods,
2221 info->dev_capab, info->group_capab,
2222 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2223 wfd_dev_info_hex ? wfd_dev_info_hex : "",
2224 info->vendor_elems ? " vendor_elems=1" : "",
2228 os_free(wfd_dev_info_hex);
2229 #endif /* CONFIG_NO_STDOUT_DEBUG */
2231 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2235 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2237 struct wpa_supplicant *wpa_s = ctx;
2239 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2240 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2242 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2246 static void wpas_find_stopped(void *ctx)
2248 struct wpa_supplicant *wpa_s = ctx;
2249 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2253 struct wpas_p2p_listen_work {
2255 unsigned int duration;
2256 struct wpabuf *probe_resp_ie;
2260 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2264 wpabuf_free(lwork->probe_resp_ie);
2269 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2271 struct wpas_p2p_listen_work *lwork;
2273 if (!wpa_s->p2p_listen_work)
2276 lwork = wpa_s->p2p_listen_work->ctx;
2277 wpas_p2p_listen_work_free(lwork);
2278 radio_work_done(wpa_s->p2p_listen_work);
2279 wpa_s->p2p_listen_work = NULL;
2283 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2285 struct wpa_supplicant *wpa_s = work->wpa_s;
2286 struct wpas_p2p_listen_work *lwork = work->ctx;
2287 unsigned int duration;
2290 if (work->started) {
2291 wpa_s->p2p_listen_work = NULL;
2292 wpas_stop_listen(wpa_s);
2294 wpas_p2p_listen_work_free(lwork);
2298 wpa_s->p2p_listen_work = work;
2300 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2302 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2303 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2304 "report received Probe Request frames");
2305 wpas_p2p_listen_work_done(wpa_s);
2309 wpa_s->pending_listen_freq = lwork->freq;
2310 wpa_s->pending_listen_duration = lwork->duration;
2312 duration = lwork->duration;
2313 #ifdef CONFIG_TESTING_OPTIONS
2314 if (wpa_s->extra_roc_dur) {
2315 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2316 duration, duration + wpa_s->extra_roc_dur);
2317 duration += wpa_s->extra_roc_dur;
2319 #endif /* CONFIG_TESTING_OPTIONS */
2321 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2322 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2323 "to remain on channel (%u MHz) for Listen "
2324 "state", lwork->freq);
2325 wpas_p2p_listen_work_done(wpa_s);
2326 wpa_s->pending_listen_freq = 0;
2329 wpa_s->off_channel_freq = 0;
2330 wpa_s->roc_waiting_drv_freq = lwork->freq;
2334 static int wpas_start_listen(void *ctx, unsigned int freq,
2335 unsigned int duration,
2336 const struct wpabuf *probe_resp_ie)
2338 struct wpa_supplicant *wpa_s = ctx;
2339 struct wpas_p2p_listen_work *lwork;
2341 if (wpa_s->p2p_listen_work) {
2342 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2346 lwork = os_zalloc(sizeof(*lwork));
2350 lwork->duration = duration;
2351 if (probe_resp_ie) {
2352 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2353 if (lwork->probe_resp_ie == NULL) {
2354 wpas_p2p_listen_work_free(lwork);
2359 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2361 wpas_p2p_listen_work_free(lwork);
2369 static void wpas_stop_listen(void *ctx)
2371 struct wpa_supplicant *wpa_s = ctx;
2372 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2373 wpa_drv_cancel_remain_on_channel(wpa_s);
2374 wpa_s->off_channel_freq = 0;
2375 wpa_s->roc_waiting_drv_freq = 0;
2377 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2378 wpa_drv_probe_req_report(wpa_s, 0);
2379 wpas_p2p_listen_work_done(wpa_s);
2383 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
2385 struct wpa_supplicant *wpa_s = ctx;
2386 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
2391 * DNS Header section is used only to calculate compression pointers, so the
2392 * contents of this data does not matter, but the length needs to be reserved
2393 * in the virtual packet.
2395 #define DNS_HEADER_LEN 12
2398 * 27-octet in-memory packet from P2P specification containing two implied
2399 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
2401 #define P2P_SD_IN_MEMORY_LEN 27
2403 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
2404 u8 **spos, const u8 *end)
2406 while (*spos < end) {
2407 u8 val = ((*spos)[0] & 0xc0) >> 6;
2410 if (val == 1 || val == 2) {
2411 /* These are reserved values in RFC 1035 */
2412 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2413 "sequence starting with 0x%x", val);
2422 if (*spos + 2 > end) {
2423 wpa_printf(MSG_DEBUG, "P2P: No room for full "
2424 "DNS offset field");
2428 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
2429 if (offset >= *spos - start) {
2430 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
2431 "pointer offset %u", offset);
2436 spos_tmp = start + offset;
2437 return p2p_sd_dns_uncompress_label(upos, uend, start,
2443 len = (*spos)[0] & 0x3f;
2448 if (*spos + len > end) {
2449 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2450 "sequence - no room for label with length "
2455 if (*upos + len + 2 > uend)
2458 os_memcpy(*upos, *spos, len);
2470 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
2471 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
2472 * not large enough */
2473 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
2474 size_t msg_len, size_t offset)
2476 /* 27-octet in-memory packet from P2P specification */
2477 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
2478 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
2479 u8 *tmp, *end, *spos;
2485 if (offset > msg_len)
2488 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
2491 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
2492 end = spos + msg_len;
2495 os_memset(tmp, 0, DNS_HEADER_LEN);
2496 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
2497 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
2500 uend = buf + buf_len;
2502 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
2511 } else if (upos[-1] == '.')
2519 static struct p2p_srv_bonjour *
2520 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2521 const struct wpabuf *query)
2523 struct p2p_srv_bonjour *bsrv;
2526 len = wpabuf_len(query);
2527 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2528 struct p2p_srv_bonjour, list) {
2529 if (len == wpabuf_len(bsrv->query) &&
2530 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2538 static struct p2p_srv_upnp *
2539 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2540 const char *service)
2542 struct p2p_srv_upnp *usrv;
2544 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2545 struct p2p_srv_upnp, list) {
2546 if (version == usrv->version &&
2547 os_strcmp(service, usrv->service) == 0)
2554 static void wpas_sd_add_empty(struct wpabuf *resp, u8 srv_proto,
2555 u8 srv_trans_id, u8 status)
2559 if (wpabuf_tailroom(resp) < 5)
2562 /* Length (to be filled) */
2563 len_pos = wpabuf_put(resp, 2);
2564 wpabuf_put_u8(resp, srv_proto);
2565 wpabuf_put_u8(resp, srv_trans_id);
2567 wpabuf_put_u8(resp, status);
2568 /* Response Data: empty */
2569 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2573 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2576 wpas_sd_add_empty(resp, srv_proto, srv_trans_id,
2577 P2P_SD_PROTO_NOT_AVAILABLE);
2581 static void wpas_sd_add_bad_request(struct wpabuf *resp, u8 srv_proto,
2584 wpas_sd_add_empty(resp, srv_proto, srv_trans_id, P2P_SD_BAD_REQUEST);
2588 static void wpas_sd_add_not_found(struct wpabuf *resp, u8 srv_proto,
2591 wpas_sd_add_empty(resp, srv_proto, srv_trans_id,
2592 P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2596 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2597 struct wpabuf *resp, u8 srv_trans_id)
2599 struct p2p_srv_bonjour *bsrv;
2602 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2604 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2605 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2609 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2610 struct p2p_srv_bonjour, list) {
2611 if (wpabuf_tailroom(resp) <
2612 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2614 /* Length (to be filled) */
2615 len_pos = wpabuf_put(resp, 2);
2616 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2617 wpabuf_put_u8(resp, srv_trans_id);
2619 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2620 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2621 wpabuf_head(bsrv->resp),
2622 wpabuf_len(bsrv->resp));
2624 wpabuf_put_buf(resp, bsrv->query); /* Key */
2625 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2626 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2632 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2635 char str_rx[256], str_srv[256];
2637 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2638 return 0; /* Too short to include DNS Type and Version */
2639 if (os_memcmp(query + query_len - 3,
2640 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2642 return 0; /* Mismatch in DNS Type or Version */
2643 if (query_len == wpabuf_len(bsrv->query) &&
2644 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2645 return 1; /* Binary match */
2647 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2649 return 0; /* Failed to uncompress query */
2650 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2651 wpabuf_head(bsrv->query),
2652 wpabuf_len(bsrv->query) - 3, 0))
2653 return 0; /* Failed to uncompress service */
2655 return os_strcmp(str_rx, str_srv) == 0;
2659 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2660 struct wpabuf *resp, u8 srv_trans_id,
2661 const u8 *query, size_t query_len)
2663 struct p2p_srv_bonjour *bsrv;
2667 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2669 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2670 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2671 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2676 if (query_len == 0) {
2677 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2681 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2682 struct p2p_srv_bonjour, list) {
2683 if (!match_bonjour_query(bsrv, query, query_len))
2686 if (wpabuf_tailroom(resp) <
2687 5 + query_len + wpabuf_len(bsrv->resp))
2692 /* Length (to be filled) */
2693 len_pos = wpabuf_put(resp, 2);
2694 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2695 wpabuf_put_u8(resp, srv_trans_id);
2698 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2699 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2700 wpabuf_head(bsrv->resp),
2701 wpabuf_len(bsrv->resp));
2704 wpabuf_put_data(resp, query, query_len); /* Key */
2705 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2707 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2711 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2713 if (wpabuf_tailroom(resp) < 5)
2716 /* Length (to be filled) */
2717 len_pos = wpabuf_put(resp, 2);
2718 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2719 wpabuf_put_u8(resp, srv_trans_id);
2722 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2723 /* Response Data: empty */
2724 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2730 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2731 struct wpabuf *resp, u8 srv_trans_id)
2733 struct p2p_srv_upnp *usrv;
2736 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2738 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2739 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2743 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2744 struct p2p_srv_upnp, list) {
2745 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2748 /* Length (to be filled) */
2749 len_pos = wpabuf_put(resp, 2);
2750 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2751 wpabuf_put_u8(resp, srv_trans_id);
2754 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2756 wpabuf_put_u8(resp, usrv->version);
2757 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2759 wpabuf_put_str(resp, usrv->service);
2760 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2766 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2767 struct wpabuf *resp, u8 srv_trans_id,
2768 const u8 *query, size_t query_len)
2770 struct p2p_srv_upnp *usrv;
2776 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2779 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2780 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2781 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2786 if (query_len == 0) {
2787 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2791 if (wpabuf_tailroom(resp) < 5)
2794 /* Length (to be filled) */
2795 len_pos = wpabuf_put(resp, 2);
2796 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2797 wpabuf_put_u8(resp, srv_trans_id);
2800 str = os_malloc(query_len);
2803 os_memcpy(str, query + 1, query_len - 1);
2804 str[query_len - 1] = '\0';
2806 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2807 struct p2p_srv_upnp, list) {
2808 if (version != usrv->version)
2811 if (os_strcmp(str, "ssdp:all") != 0 &&
2812 os_strstr(usrv->service, str) == NULL)
2815 if (wpabuf_tailroom(resp) < 2)
2819 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2821 wpabuf_put_u8(resp, version);
2823 wpabuf_put_u8(resp, ',');
2827 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2829 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2831 wpabuf_put_str(resp, usrv->service);
2836 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2839 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2840 /* Response Data: empty */
2843 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2847 #ifdef CONFIG_WIFI_DISPLAY
2848 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2849 struct wpabuf *resp, u8 srv_trans_id,
2850 const u8 *query, size_t query_len)
2856 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2858 if (!wpa_s->global->wifi_display) {
2859 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2860 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2865 if (query_len < 1) {
2866 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2871 if (wpabuf_tailroom(resp) < 5)
2876 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2878 /* TODO: role specific handling */
2880 /* Length (to be filled) */
2881 len_pos = wpabuf_put(resp, 2);
2882 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2883 wpabuf_put_u8(resp, srv_trans_id);
2884 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2886 while (pos < query + query_len) {
2887 if (*pos < MAX_WFD_SUBELEMS &&
2888 wpa_s->global->wfd_subelem[*pos] &&
2889 wpabuf_tailroom(resp) >=
2890 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2891 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2892 "subelement %u", *pos);
2893 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2898 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2900 #endif /* CONFIG_WIFI_DISPLAY */
2903 static int find_p2ps_substr(struct p2ps_advertisement *adv_data,
2904 const u8 *needle, size_t needle_len)
2906 const u8 *haystack = (const u8 *) adv_data->svc_info;
2907 size_t haystack_len, i;
2909 /* Allow search term to be empty */
2910 if (!needle || !needle_len)
2916 haystack_len = os_strlen(adv_data->svc_info);
2917 for (i = 0; i < haystack_len; i++) {
2918 if (haystack_len - i < needle_len)
2920 if (os_memcmp(haystack + i, needle, needle_len) == 0)
2928 static void wpas_sd_req_asp(struct wpa_supplicant *wpa_s,
2929 struct wpabuf *resp, u8 srv_trans_id,
2930 const u8 *query, size_t query_len)
2932 struct p2ps_advertisement *adv_data;
2933 const u8 *svc = &query[1];
2934 const u8 *info = NULL;
2935 size_t svc_len = query[0];
2936 size_t info_len = 0;
2938 u8 *count_pos = NULL;
2941 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for ASP", query, query_len);
2943 if (!wpa_s->global->p2p) {
2944 wpa_printf(MSG_DEBUG, "P2P: ASP protocol not available");
2945 wpas_sd_add_proto_not_avail(resp, P2P_SERV_P2PS, srv_trans_id);
2949 /* Info block is optional */
2950 if (svc_len + 1 < query_len) {
2951 info = &svc[svc_len];
2955 /* Range check length of svc string and info block */
2956 if (svc_len + (info_len ? info_len + 2 : 1) > query_len) {
2957 wpa_printf(MSG_DEBUG, "P2P: ASP bad request");
2958 wpas_sd_add_bad_request(resp, P2P_SERV_P2PS, srv_trans_id);
2962 /* Detect and correct for prefix search */
2963 if (svc_len && svc[svc_len - 1] == '*') {
2968 for (adv_data = p2p_get_p2ps_adv_list(wpa_s->global->p2p);
2969 adv_data; adv_data = adv_data->next) {
2970 /* If not a prefix match, reject length mismatches */
2971 if (!prefix && svc_len != os_strlen(adv_data->svc_name))
2974 /* Search each service for request */
2975 if (os_memcmp(adv_data->svc_name, svc, svc_len) == 0 &&
2976 find_p2ps_substr(adv_data, info, info_len)) {
2977 size_t len = os_strlen(adv_data->svc_name);
2978 size_t svc_info_len = 0;
2980 if (adv_data->svc_info)
2981 svc_info_len = os_strlen(adv_data->svc_info);
2983 if (len > 0xff || svc_info_len > 0xffff)
2986 /* Length & Count to be filled as we go */
2987 if (!len_pos && !count_pos) {
2988 if (wpabuf_tailroom(resp) <
2989 len + svc_info_len + 16)
2992 len_pos = wpabuf_put(resp, 2);
2993 wpabuf_put_u8(resp, P2P_SERV_P2PS);
2994 wpabuf_put_u8(resp, srv_trans_id);
2996 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2997 count_pos = wpabuf_put(resp, 1);
2999 } else if (wpabuf_tailroom(resp) <
3000 len + svc_info_len + 10)
3004 wpa_printf(MSG_DEBUG,
3005 "P2P: Add Svc: %s info: %s",
3007 adv_data->svc_info);
3009 wpa_printf(MSG_DEBUG, "P2P: Add Svc: %s",
3010 adv_data->svc_name);
3013 /* Advertisement ID */
3014 wpabuf_put_le32(resp, adv_data->id);
3016 /* Config Methods */
3017 wpabuf_put_be16(resp, adv_data->config_methods);
3020 wpabuf_put_u8(resp, (u8) len);
3021 wpabuf_put_data(resp, adv_data->svc_name, len);
3024 wpabuf_put_u8(resp, adv_data->state);
3026 /* Service Information */
3027 wpabuf_put_le16(resp, (u16) svc_info_len);
3028 wpabuf_put_data(resp, adv_data->svc_info, svc_info_len);
3030 /* Update length and count */
3032 WPA_PUT_LE16(len_pos,
3033 (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
3037 /* Return error if no matching svc found */
3038 if (count_pos == NULL) {
3039 wpa_printf(MSG_DEBUG, "P2P: ASP service not found");
3040 wpas_sd_add_not_found(resp, P2P_SERV_P2PS, srv_trans_id);
3045 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
3046 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
3048 struct wpa_supplicant *wpa_s = ctx;
3049 const u8 *pos = tlvs;
3050 const u8 *end = tlvs + tlvs_len;
3053 struct wpabuf *resp;
3054 u8 srv_proto, srv_trans_id;
3058 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
3060 buf_len = 2 * tlvs_len + 1;
3061 buf = os_malloc(buf_len);
3063 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
3064 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
3066 freq, MAC2STR(sa), dialog_token, update_indic,
3071 if (wpa_s->p2p_sd_over_ctrl_iface) {
3072 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
3073 update_indic, tlvs, tlvs_len);
3074 return; /* to be processed by an external program */
3077 resp = wpabuf_alloc(10000);
3081 while (pos + 1 < end) {
3082 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
3083 slen = WPA_GET_LE16(pos);
3085 if (pos + slen > end || slen < 2) {
3086 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
3091 tlv_end = pos + slen;
3094 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
3096 srv_trans_id = *pos++;
3097 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
3100 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
3101 pos, tlv_end - pos);
3104 if (wpa_s->force_long_sd) {
3105 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
3107 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
3108 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
3112 switch (srv_proto) {
3113 case P2P_SERV_ALL_SERVICES:
3114 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
3115 "for all services");
3116 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
3117 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
3118 wpa_printf(MSG_DEBUG, "P2P: No service "
3119 "discovery protocols available");
3120 wpas_sd_add_proto_not_avail(
3121 resp, P2P_SERV_ALL_SERVICES,
3125 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
3126 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
3128 case P2P_SERV_BONJOUR:
3129 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
3130 pos, tlv_end - pos);
3133 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
3134 pos, tlv_end - pos);
3136 #ifdef CONFIG_WIFI_DISPLAY
3137 case P2P_SERV_WIFI_DISPLAY:
3138 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
3139 pos, tlv_end - pos);
3141 #endif /* CONFIG_WIFI_DISPLAY */
3143 wpas_sd_req_asp(wpa_s, resp, srv_trans_id,
3144 pos, tlv_end - pos);
3147 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
3148 "protocol %u", srv_proto);
3149 wpas_sd_add_proto_not_avail(resp, srv_proto,
3158 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
3159 update_indic, tlvs, tlvs_len);
3161 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
3167 static void wpas_sd_p2ps_serv_response(struct wpa_supplicant *wpa_s,
3168 const u8 *sa, u8 srv_trans_id,
3169 const u8 *pos, const u8 *tlv_end)
3177 while (left-- && pos < tlv_end) {
3182 /* Sanity check fixed length+svc_str */
3183 if (pos + 6 >= tlv_end)
3186 if (pos + svc_len + 10 > tlv_end)
3189 /* Advertisement ID */
3190 adv_id = WPA_GET_LE32(pos);
3193 /* Config Methods */
3194 config_methods = WPA_GET_BE16(pos);
3198 pos++; /* svc_len */
3199 os_memcpy(svc_str, pos, svc_len);
3200 svc_str[svc_len] = '\0';
3203 /* Service Status */
3204 svc_status = *pos++;
3206 /* Service Information Length */
3207 buf_len = WPA_GET_LE16(pos);
3210 /* Sanity check buffer length */
3211 if (buf_len > (unsigned int) (tlv_end - pos))
3215 buf = os_zalloc(2 * buf_len + 1);
3217 utf8_escape((const char *) pos, buf_len, buf,
3225 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_SERV_ASP_RESP
3226 MACSTR " %x %x %x %x %s '%s'",
3227 MAC2STR(sa), srv_trans_id, adv_id,
3228 svc_status, config_methods, svc_str,
3232 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_SERV_ASP_RESP
3233 MACSTR " %x %x %x %x %s",
3234 MAC2STR(sa), srv_trans_id, adv_id,
3235 svc_status, config_methods, svc_str);
3241 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
3242 const u8 *tlvs, size_t tlvs_len)
3244 struct wpa_supplicant *wpa_s = ctx;
3245 const u8 *pos = tlvs;
3246 const u8 *end = tlvs + tlvs_len;
3252 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
3254 if (tlvs_len > 1500) {
3255 /* TODO: better way for handling this */
3256 wpa_msg_ctrl(wpa_s, MSG_INFO,
3257 P2P_EVENT_SERV_DISC_RESP MACSTR
3258 " %u <long response: %u bytes>",
3259 MAC2STR(sa), update_indic,
3260 (unsigned int) tlvs_len);
3262 buf_len = 2 * tlvs_len + 1;
3263 buf = os_malloc(buf_len);
3265 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
3266 wpa_msg_ctrl(wpa_s, MSG_INFO,
3267 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
3268 MAC2STR(sa), update_indic, buf);
3274 u8 srv_proto, srv_trans_id, status;
3276 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
3277 slen = WPA_GET_LE16(pos);
3279 if (pos + slen > end || slen < 3) {
3280 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
3284 tlv_end = pos + slen;
3287 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
3289 srv_trans_id = *pos++;
3290 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
3293 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
3296 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
3297 pos, tlv_end - pos);
3299 if (srv_proto == P2P_SERV_P2PS && pos < tlv_end) {
3300 wpas_sd_p2ps_serv_response(wpa_s, sa, srv_trans_id,
3307 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
3311 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3312 const struct wpabuf *tlvs)
3314 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3316 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
3320 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
3321 u8 version, const char *query)
3323 struct wpabuf *tlvs;
3326 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
3329 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
3330 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
3331 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
3332 wpabuf_put_u8(tlvs, version);
3333 wpabuf_put_str(tlvs, query);
3334 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
3340 u64 wpas_p2p_sd_request_asp(struct wpa_supplicant *wpa_s, const u8 *dst, u8 id,
3341 const char *svc_str, const char *info_substr)
3343 struct wpabuf *tlvs;
3344 size_t plen, svc_len, substr_len = 0;
3347 svc_len = os_strlen(svc_str);
3349 substr_len = os_strlen(info_substr);
3351 if (svc_len > 0xff || substr_len > 0xff)
3354 plen = 1 + 1 + 1 + svc_len + 1 + substr_len;
3355 tlvs = wpabuf_alloc(2 + plen);
3359 wpabuf_put_le16(tlvs, plen);
3360 wpabuf_put_u8(tlvs, P2P_SERV_P2PS);
3361 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
3362 wpabuf_put_u8(tlvs, (u8) svc_len); /* Service String Length */
3363 wpabuf_put_data(tlvs, svc_str, svc_len);
3364 wpabuf_put_u8(tlvs, (u8) substr_len); /* Info Substring Length */
3365 wpabuf_put_data(tlvs, info_substr, substr_len);
3366 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
3373 #ifdef CONFIG_WIFI_DISPLAY
3375 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
3376 const struct wpabuf *tlvs)
3378 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3380 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
3384 #define MAX_WFD_SD_SUBELEMS 20
3386 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
3387 const char *subelems)
3394 len = wpabuf_put(tlvs, 2);
3395 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
3396 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
3398 wpabuf_put_u8(tlvs, role);
3403 if (val >= 0 && val < 256) {
3404 wpabuf_put_u8(tlvs, val);
3406 if (count == MAX_WFD_SD_SUBELEMS)
3409 pos = os_strchr(pos + 1, ',');
3415 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
3419 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
3420 const u8 *dst, const char *role)
3422 struct wpabuf *tlvs;
3424 const char *subelems;
3427 subelems = os_strchr(role, ' ');
3428 if (subelems == NULL)
3432 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
3436 if (os_strstr(role, "[source]"))
3437 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
3438 if (os_strstr(role, "[pri-sink]"))
3439 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
3440 if (os_strstr(role, "[sec-sink]"))
3441 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
3442 if (os_strstr(role, "[source+sink]"))
3443 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
3445 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
3450 #endif /* CONFIG_WIFI_DISPLAY */
3453 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
3455 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3457 return p2p_sd_cancel_request(wpa_s->global->p2p,
3458 (void *) (uintptr_t) req);
3462 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
3463 const u8 *dst, u8 dialog_token,
3464 const struct wpabuf *resp_tlvs)
3466 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3468 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
3473 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
3475 if (wpa_s->global->p2p)
3476 p2p_sd_service_update(wpa_s->global->p2p);
3480 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
3482 dl_list_del(&bsrv->list);
3483 wpabuf_free(bsrv->query);
3484 wpabuf_free(bsrv->resp);
3489 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
3491 dl_list_del(&usrv->list);
3492 os_free(usrv->service);
3497 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
3499 struct p2p_srv_bonjour *bsrv, *bn;
3500 struct p2p_srv_upnp *usrv, *un;
3502 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
3503 struct p2p_srv_bonjour, list)
3504 wpas_p2p_srv_bonjour_free(bsrv);
3506 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
3507 struct p2p_srv_upnp, list)
3508 wpas_p2p_srv_upnp_free(usrv);
3510 wpas_p2p_sd_service_update(wpa_s);
3514 int wpas_p2p_service_p2ps_id_exists(struct wpa_supplicant *wpa_s, u32 adv_id)
3519 if (p2p_service_p2ps_id(wpa_s->global->p2p, adv_id))
3526 int wpas_p2p_service_del_asp(struct wpa_supplicant *wpa_s, u32 adv_id)
3528 return p2p_service_del_asp(wpa_s->global->p2p, adv_id);
3532 int wpas_p2p_service_add_asp(struct wpa_supplicant *wpa_s,
3533 int auto_accept, u32 adv_id,
3534 const char *adv_str, u8 svc_state,
3535 u16 config_methods, const char *svc_info)
3537 return p2p_service_add_asp(wpa_s->global->p2p, auto_accept, adv_id,
3538 adv_str, svc_state, config_methods,
3543 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
3544 struct wpabuf *query, struct wpabuf *resp)
3546 struct p2p_srv_bonjour *bsrv;
3548 bsrv = os_zalloc(sizeof(*bsrv));
3551 bsrv->query = query;
3553 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
3555 wpas_p2p_sd_service_update(wpa_s);
3560 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
3561 const struct wpabuf *query)
3563 struct p2p_srv_bonjour *bsrv;
3565 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
3568 wpas_p2p_srv_bonjour_free(bsrv);
3569 wpas_p2p_sd_service_update(wpa_s);
3574 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
3575 const char *service)
3577 struct p2p_srv_upnp *usrv;
3579 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
3580 return 0; /* Already listed */
3581 usrv = os_zalloc(sizeof(*usrv));
3584 usrv->version = version;
3585 usrv->service = os_strdup(service);
3586 if (usrv->service == NULL) {
3590 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
3592 wpas_p2p_sd_service_update(wpa_s);
3597 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
3598 const char *service)
3600 struct p2p_srv_upnp *usrv;
3602 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
3605 wpas_p2p_srv_upnp_free(usrv);
3606 wpas_p2p_sd_service_update(wpa_s);
3611 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
3612 const u8 *peer, const char *params,
3613 unsigned int generated_pin)
3615 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
3616 " %08d%s", MAC2STR(peer), generated_pin, params);
3620 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
3621 const u8 *peer, const char *params)
3623 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
3624 "%s", MAC2STR(peer), params);
3628 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
3629 const u8 *dev_addr, const u8 *pri_dev_type,
3630 const char *dev_name, u16 supp_config_methods,
3631 u8 dev_capab, u8 group_capab, const u8 *group_id,
3632 size_t group_id_len)
3634 struct wpa_supplicant *wpa_s = ctx;
3635 char devtype[WPS_DEV_TYPE_BUFSIZE];
3637 u8 empty_dev_type[8];
3638 unsigned int generated_pin = 0;
3639 struct wpa_supplicant *group = NULL;
3643 for (group = wpa_s->global->ifaces; group; group = group->next)
3645 struct wpa_ssid *s = group->current_ssid;
3647 s->mode == WPAS_MODE_P2P_GO &&
3648 group_id_len - ETH_ALEN == s->ssid_len &&
3649 os_memcmp(group_id + ETH_ALEN, s->ssid,
3655 if (pri_dev_type == NULL) {
3656 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
3657 pri_dev_type = empty_dev_type;
3659 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
3660 " pri_dev_type=%s name='%s' config_methods=0x%x "
3661 "dev_capab=0x%x group_capab=0x%x%s%s",
3663 wps_dev_type_bin2str(pri_dev_type, devtype,
3665 dev_name, supp_config_methods, dev_capab, group_capab,
3666 group ? " group=" : "",
3667 group ? group->ifname : "");
3668 if (os_snprintf_error(sizeof(params), res))
3669 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
3670 params[sizeof(params) - 1] = '\0';
3672 if (config_methods & WPS_CONFIG_DISPLAY) {
3673 generated_pin = wps_generate_pin();
3674 wpas_prov_disc_local_display(wpa_s, peer, params,
3676 } else if (config_methods & WPS_CONFIG_KEYPAD)
3677 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3678 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3679 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
3680 MACSTR "%s", MAC2STR(peer), params);
3682 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
3683 P2P_PROV_DISC_SUCCESS,
3684 config_methods, generated_pin);
3688 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
3690 struct wpa_supplicant *wpa_s = ctx;
3691 unsigned int generated_pin = 0;
3694 if (wpa_s->pending_pd_before_join &&
3695 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
3696 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
3697 wpa_s->pending_pd_before_join = 0;
3698 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3699 "join-existing-group operation");
3700 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3704 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
3705 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
3708 res = os_snprintf(params, sizeof(params), " peer_go=%d",
3709 wpa_s->pending_pd_use == AUTO_PD_JOIN);
3710 if (os_snprintf_error(sizeof(params), res))
3711 params[sizeof(params) - 1] = '\0';
3715 if (config_methods & WPS_CONFIG_DISPLAY)
3716 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3717 else if (config_methods & WPS_CONFIG_KEYPAD) {
3718 generated_pin = wps_generate_pin();
3719 wpas_prov_disc_local_display(wpa_s, peer, params,
3721 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3722 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
3723 MACSTR "%s", MAC2STR(peer), params);
3725 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3726 P2P_PROV_DISC_SUCCESS,
3727 config_methods, generated_pin);
3731 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
3732 enum p2p_prov_disc_status status,
3733 u32 adv_id, const u8 *adv_mac,
3734 const char *deferred_session_resp)
3736 struct wpa_supplicant *wpa_s = ctx;
3738 if (wpa_s->p2p_fallback_to_go_neg) {
3739 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
3740 "failed - fall back to GO Negotiation");
3741 wpa_msg_global(wpa_s->parent, MSG_INFO,
3742 P2P_EVENT_FALLBACK_TO_GO_NEG
3743 "reason=PD-failed");
3744 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
3748 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
3749 wpa_s->pending_pd_before_join = 0;
3750 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3751 "join-existing-group operation (no ACK for PD "
3753 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3757 if (adv_id && adv_mac && deferred_session_resp) {
3758 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3759 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3760 " deferred_session_resp='%s'",
3761 MAC2STR(peer), status, adv_id,
3762 deferred_session_resp);
3763 } else if (adv_id && adv_mac) {
3764 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3765 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3766 MAC2STR(peer), status, adv_id);
3768 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3769 " p2p_dev_addr=" MACSTR " status=%d",
3770 MAC2STR(peer), status);
3773 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3778 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3780 if (channels == NULL)
3781 return 1; /* Assume no restrictions */
3782 return p2p_channels_includes_freq(channels, freq);
3788 * Pick the best frequency to use from all the currently used frequencies.
3790 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3791 struct wpa_used_freq_data *freqs,
3796 /* find a candidate freq that is supported by P2P */
3797 for (c = 0; c < num; c++)
3798 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3804 /* once we have a candidate, try to find a 'better' one */
3805 for (i = c + 1; i < num; i++) {
3806 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3810 * 1. Infrastructure station interfaces have higher preference.
3811 * 2. P2P Clients have higher preference.
3814 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3819 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3822 return freqs[c].freq;
3826 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3827 const u8 *go_dev_addr, const u8 *ssid,
3828 size_t ssid_len, int *go, u8 *group_bssid,
3829 int *force_freq, int persistent_group,
3830 const struct p2p_channels *channels,
3833 struct wpa_supplicant *wpa_s = ctx;
3835 struct wpa_used_freq_data *freqs;
3836 struct wpa_supplicant *grp;
3839 if (!persistent_group) {
3840 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3841 " to join an active group (SSID: %s)",
3842 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
3843 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3844 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3846 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3847 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3848 "authorized invitation");
3852 #ifdef CONFIG_WPS_NFC
3853 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3854 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
3855 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3856 wpa_s->p2p_wps_method = WPS_NFC;
3857 wpa_s->pending_join_wps_method = WPS_NFC;
3858 os_memcpy(wpa_s->pending_join_dev_addr,
3859 go_dev_addr, ETH_ALEN);
3860 os_memcpy(wpa_s->pending_join_iface_addr,
3864 #endif /* CONFIG_WPS_NFC */
3867 * Do not accept the invitation automatically; notify user and
3870 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3873 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3875 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3876 "running persistent group");
3878 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3882 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3883 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3884 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3885 "invitation to re-invoke a persistent group");
3886 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3887 } else if (!wpa_s->conf->persistent_reconnect)
3888 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3890 for (s = wpa_s->conf->ssid; s; s = s->next) {
3891 if (s->disabled == 2 &&
3892 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3893 s->ssid_len == ssid_len &&
3894 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3899 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3900 " requested reinvocation of an unknown group",
3902 return P2P_SC_FAIL_UNKNOWN_GROUP;
3905 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3907 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3908 wpa_printf(MSG_DEBUG, "P2P: The only available "
3909 "interface is already in use - reject "
3911 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3913 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3914 } else if (s->mode == WPAS_MODE_P2P_GO) {
3916 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3918 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3919 "interface address for the group");
3920 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3922 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3927 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3930 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3931 sizeof(struct wpa_used_freq_data));
3933 int num_channels = wpa_s->num_multichan_concurrent;
3934 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3935 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3939 /* Get one of the frequencies currently in use */
3940 if (best_freq > 0) {
3941 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3942 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3944 if (wpa_s->num_multichan_concurrent < 2 ||
3945 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3946 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3947 *force_freq = best_freq;
3951 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3952 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3954 /* We are the client */
3955 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3956 "running a GO but we are capable of MCC, "
3957 "figure out the best channel to use");
3959 } else if (!freq_included(channels, *force_freq)) {
3960 /* We are the GO, and *force_freq is not in the
3962 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3963 "in intersection but we are capable of MCC, "
3964 "figure out the best channel to use",
3970 return P2P_SC_SUCCESS;
3974 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3975 const u8 *ssid, size_t ssid_len,
3976 const u8 *go_dev_addr, u8 status,
3979 struct wpa_supplicant *wpa_s = ctx;
3982 for (s = wpa_s->conf->ssid; s; s = s->next) {
3983 if (s->disabled == 2 &&
3984 s->ssid_len == ssid_len &&
3985 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3989 if (status == P2P_SC_SUCCESS) {
3990 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3991 " was accepted; op_freq=%d MHz, SSID=%s",
3992 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3994 int go = s->mode == WPAS_MODE_P2P_GO;
3995 wpas_p2p_group_add_persistent(
3996 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
3997 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3999 wpa_s->user_initiated_pd = 0;
4000 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
4001 wpa_s->p2p_wps_method, 0, op_freq,
4007 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
4008 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
4009 " was rejected (status %u)", MAC2STR(sa), status);
4015 wpa_msg_global(wpa_s, MSG_INFO,
4016 P2P_EVENT_INVITATION_RECEIVED
4017 "sa=" MACSTR " go_dev_addr=" MACSTR
4018 " bssid=" MACSTR " unknown-network",
4019 MAC2STR(sa), MAC2STR(go_dev_addr),
4022 wpa_msg_global(wpa_s, MSG_INFO,
4023 P2P_EVENT_INVITATION_RECEIVED
4024 "sa=" MACSTR " go_dev_addr=" MACSTR
4026 MAC2STR(sa), MAC2STR(go_dev_addr));
4031 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
4032 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
4033 "sa=" MACSTR " persistent=%d freq=%d",
4034 MAC2STR(sa), s->id, op_freq);
4036 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
4037 "sa=" MACSTR " persistent=%d",
4038 MAC2STR(sa), s->id);
4043 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
4044 struct wpa_ssid *ssid,
4045 const u8 *peer, int inv)
4052 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
4053 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
4057 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
4058 if (ssid->mode != WPAS_MODE_P2P_GO &&
4059 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
4060 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
4061 "due to invitation result", ssid->id);
4062 wpas_notify_network_removed(wpa_s, ssid);
4063 wpa_config_remove_network(wpa_s->conf, ssid->id);
4066 return; /* Peer not found in client list */
4069 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
4070 "group %d client list%s",
4071 MAC2STR(peer), ssid->id,
4072 inv ? " due to invitation result" : "");
4073 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
4074 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4075 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4076 ssid->num_p2p_clients--;
4077 if (wpa_s->parent->conf->update_config &&
4078 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
4079 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
4083 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
4086 struct wpa_ssid *ssid;
4088 wpa_s = wpa_s->global->p2p_invite_group;
4090 return; /* No known invitation group */
4091 ssid = wpa_s->current_ssid;
4092 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4093 !ssid->p2p_persistent_group)
4094 return; /* Not operating as a GO in persistent group */
4095 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
4096 ssid->ssid, ssid->ssid_len);
4097 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
4101 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
4102 const struct p2p_channels *channels,
4103 const u8 *peer, int neg_freq,
4106 struct wpa_supplicant *wpa_s = ctx;
4107 struct wpa_ssid *ssid;
4111 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
4112 "status=%d " MACSTR,
4113 status, MAC2STR(bssid));
4115 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
4116 "status=%d ", status);
4118 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
4120 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
4121 status, MAC2STR(peer));
4122 if (wpa_s->pending_invite_ssid_id == -1) {
4123 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
4124 wpas_remove_persistent_client(wpa_s, peer);
4125 return; /* Invitation to active group */
4128 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
4129 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
4130 "invitation exchange to indicate readiness for "
4134 if (status != P2P_SC_SUCCESS) {
4135 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
4136 ssid = wpa_config_get_network(
4137 wpa_s->conf, wpa_s->pending_invite_ssid_id);
4138 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
4140 wpas_p2p_remove_pending_group_interface(wpa_s);
4144 ssid = wpa_config_get_network(wpa_s->conf,
4145 wpa_s->pending_invite_ssid_id);
4147 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
4148 "data matching with invitation");
4153 * The peer could have missed our ctrl::ack frame for Invitation
4154 * Response and continue retransmitting the frame. To reduce the
4155 * likelihood of the peer not getting successful TX status for the
4156 * Invitation Response frame, wait a short time here before starting
4157 * the persistent group so that we will remain on the current channel to
4158 * acknowledge any possible retransmission from the peer.
4160 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
4161 "starting persistent group");
4164 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
4165 freq_included(channels, neg_freq))
4167 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
4168 freq_included(channels, peer_oper_freq))
4169 freq = peer_oper_freq;
4173 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
4174 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4175 wpas_p2p_group_add_persistent(wpa_s, ssid,
4176 ssid->mode == WPAS_MODE_P2P_GO,
4177 wpa_s->p2p_persistent_go_freq,
4179 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
4181 ssid->mode == WPAS_MODE_P2P_GO ?
4182 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4187 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
4190 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
4192 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
4196 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
4198 reg->channel[reg->channels] = chan;
4203 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
4204 struct p2p_channels *chan,
4205 struct p2p_channels *cli_chan)
4209 wpa_s->global->p2p_24ghz_social_channels = 1;
4211 os_memset(cli_chan, 0, sizeof(*cli_chan));
4213 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
4216 /* Operating class 81 - 2.4 GHz band channels 1..13 */
4217 chan->reg_class[cla].reg_class = 81;
4218 chan->reg_class[cla].channels = 0;
4219 for (i = 0; i < 11; i++) {
4220 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
4221 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
4223 if (chan->reg_class[cla].channels)
4226 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
4229 /* Operating class 115 - 5 GHz, channels 36-48 */
4230 chan->reg_class[cla].reg_class = 115;
4231 chan->reg_class[cla].channels = 0;
4232 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
4233 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
4234 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
4235 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
4236 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
4237 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
4238 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
4239 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
4240 if (chan->reg_class[cla].channels)
4243 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
4246 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
4247 chan->reg_class[cla].reg_class = 124;
4248 chan->reg_class[cla].channels = 0;
4249 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
4250 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
4251 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
4252 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
4253 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
4254 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
4255 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
4256 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
4257 if (chan->reg_class[cla].channels)
4260 chan->reg_classes = cla;
4265 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
4267 enum hostapd_hw_mode mode)
4271 for (i = 0; i < num_modes; i++) {
4272 if (modes[i].mode == mode)
4281 NOT_ALLOWED, NO_IR, ALLOWED
4284 static int has_channel(struct wpa_global *global,
4285 struct hostapd_hw_modes *mode, u8 chan, int *flags)
4290 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
4292 if (wpas_p2p_disallowed_freq(global, freq))
4295 for (i = 0; i < mode->num_channels; i++) {
4296 if (mode->channels[i].chan == chan) {
4298 *flags = mode->channels[i].flag;
4299 if (mode->channels[i].flag &
4300 (HOSTAPD_CHAN_DISABLED |
4301 HOSTAPD_CHAN_RADAR))
4303 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
4313 struct p2p_oper_class_map {
4314 enum hostapd_hw_mode mode;
4319 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
4322 static struct p2p_oper_class_map op_class[] = {
4323 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
4324 #if 0 /* Do not enable HT40 on 2 GHz for now */
4325 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
4326 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
4328 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
4329 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
4330 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
4331 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
4332 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
4333 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
4336 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
4337 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
4338 * 80 MHz, but currently use the following definition for simplicity
4339 * (these center frequencies are not actual channels, which makes
4340 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
4341 * removing invalid channels.
4343 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
4344 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
4345 { -1, 0, 0, 0, 0, BW20 }
4349 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
4350 struct hostapd_hw_modes *mode,
4353 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
4356 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
4359 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
4361 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
4362 * so the center channel is 6 channels away from the start/end.
4364 if (channel >= center_channels[i] - 6 &&
4365 channel <= center_channels[i] + 6)
4366 return center_channels[i];
4372 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
4373 struct hostapd_hw_modes *mode,
4378 enum chan_allowed res, ret = ALLOWED;
4380 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
4383 if (center_chan >= 58 && center_chan <= 138)
4384 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
4386 /* check all the channels are available */
4387 for (i = 0; i < 4; i++) {
4388 int adj_chan = center_chan - 6 + i * 4;
4390 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
4391 if (res == NOT_ALLOWED)
4396 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
4398 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
4400 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
4402 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
4410 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
4411 struct hostapd_hw_modes *mode,
4415 enum chan_allowed res, res2;
4417 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
4418 if (bw == BW40MINUS) {
4419 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
4421 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
4422 } else if (bw == BW40PLUS) {
4423 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
4425 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
4426 } else if (bw == BW80) {
4427 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
4430 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
4432 if (res == NO_IR || res2 == NO_IR)
4438 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
4439 struct p2p_channels *chan,
4440 struct p2p_channels *cli_chan)
4442 struct hostapd_hw_modes *mode;
4443 int cla, op, cli_cla;
4445 if (wpa_s->hw.modes == NULL) {
4446 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
4447 "of all supported channels; assume dualband "
4449 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
4454 for (op = 0; op_class[op].op_class; op++) {
4455 struct p2p_oper_class_map *o = &op_class[op];
4457 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
4459 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
4462 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4463 wpa_s->global->p2p_24ghz_social_channels = 1;
4464 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4465 enum chan_allowed res;
4466 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
4467 if (res == ALLOWED) {
4469 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4471 reg = &chan->reg_class[cla];
4473 reg->reg_class = o->op_class;
4475 reg->channel[reg->channels] = ch;
4477 } else if (res == NO_IR &&
4478 wpa_s->conf->p2p_add_cli_chan) {
4479 if (cli_reg == NULL) {
4480 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4482 cli_reg = &cli_chan->reg_class[cli_cla];
4484 cli_reg->reg_class = o->op_class;
4486 cli_reg->channel[cli_reg->channels] = ch;
4487 cli_reg->channels++;
4491 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4492 reg->channel, reg->channels);
4495 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4496 cli_reg->channel, cli_reg->channels);
4500 chan->reg_classes = cla;
4501 cli_chan->reg_classes = cli_cla;
4507 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
4508 struct hostapd_hw_modes *mode, u8 channel)
4511 enum chan_allowed ret;
4513 for (op = 0; op_class[op].op_class; op++) {
4514 struct p2p_oper_class_map *o = &op_class[op];
4517 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4518 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
4519 o->bw == BW20 || ch != channel)
4521 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
4523 return (o->bw == BW40MINUS) ? -1 : 1;
4530 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
4531 struct hostapd_hw_modes *mode, u8 channel)
4533 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
4536 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
4540 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4543 struct wpa_supplicant *wpa_s = ctx;
4545 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4546 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4552 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4556 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4557 const u8 *ssid, size_t ssid_len)
4559 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4560 struct wpa_ssid *s = wpa_s->current_ssid;
4563 if (s->mode != WPAS_MODE_P2P_GO &&
4564 s->mode != WPAS_MODE_AP &&
4565 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4567 if (s->ssid_len != ssid_len ||
4568 os_memcmp(ssid, s->ssid, ssid_len) != 0)
4578 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4579 const u8 *peer_dev_addr)
4581 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4582 struct wpa_ssid *ssid = wpa_s->current_ssid;
4585 if (ssid->mode != WPAS_MODE_INFRA)
4587 if (wpa_s->wpa_state != WPA_COMPLETED &&
4588 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
4590 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4598 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4600 struct wpa_supplicant *wpa_s = ctx;
4602 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
4606 static int wpas_is_concurrent_session_active(void *ctx)
4608 struct wpa_supplicant *wpa_s = ctx;
4609 struct wpa_supplicant *ifs;
4611 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4614 if (ifs->wpa_state > WPA_ASSOCIATED)
4621 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4623 struct wpa_supplicant *wpa_s = ctx;
4624 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4628 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4629 const char *conf_p2p_dev)
4631 struct wpa_interface iface;
4632 struct wpa_supplicant *p2pdev_wpa_s;
4634 char force_name[100];
4637 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4639 if (os_snprintf_error(sizeof(ifname), ret))
4641 force_name[0] = '\0';
4642 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
4643 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
4644 force_name, wpa_s->pending_interface_addr, NULL);
4646 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4649 os_strlcpy(wpa_s->pending_interface_name, ifname,
4650 sizeof(wpa_s->pending_interface_name));
4652 os_memset(&iface, 0, sizeof(iface));
4654 iface.ifname = wpa_s->pending_interface_name;
4655 iface.driver = wpa_s->driver->name;
4656 iface.driver_param = wpa_s->conf->driver_param;
4659 * If a P2P Device configuration file was given, use it as the interface
4660 * configuration file (instead of using parent's configuration file.
4663 iface.confname = conf_p2p_dev;
4664 iface.ctrl_interface = NULL;
4666 iface.confname = wpa_s->confname;
4667 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4669 iface.conf_p2p_dev = NULL;
4671 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
4672 if (!p2pdev_wpa_s) {
4673 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4676 wpa_s->p2p_dev = p2pdev_wpa_s;
4678 wpa_s->pending_interface_name[0] = '\0';
4683 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4684 const u8 *noa, size_t noa_len)
4686 struct wpa_supplicant *wpa_s, *intf = ctx;
4689 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4690 if (wpa_s->waiting_presence_resp)
4694 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4697 wpa_s->waiting_presence_resp = 0;
4699 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4700 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4701 " status=%u noa=%s", MAC2STR(src), status, hex);
4705 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4706 size_t ssid_len, u8 *go_dev_addr,
4707 u8 *ret_ssid, size_t *ret_ssid_len)
4709 struct wpa_supplicant *wpa_s = ctx;
4712 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4714 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4715 *ret_ssid_len = s->ssid_len;
4716 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
4724 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
4725 u8 *ssid, size_t *ssid_len, int *group_iface)
4727 struct wpa_supplicant *wpa_s = ctx;
4731 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
4733 s = wpas_p2p_get_persistent_go(wpa_s);
4735 os_memcpy(bssid, s->bssid, ETH_ALEN);
4738 *group_iface = wpas_p2p_create_iface(wpa_s);
4742 os_memcpy(intended_addr, bssid, ETH_ALEN);
4743 os_memcpy(ssid, s->ssid, s->ssid_len);
4744 *ssid_len = s->ssid_len;
4750 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4751 const u8 *ssid, size_t ssid_len)
4753 struct wpa_supplicant *wpa_s = ctx;
4755 int save_config = 0;
4758 /* Start with our first choice of Persistent Groups */
4759 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4760 if (go && ssid && ssid_len &&
4761 s->ssid_len == ssid_len &&
4762 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4763 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4766 /* Remove stale persistent group */
4767 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
4768 wpa_config_remove_network(wpa_s->conf, s->id);
4773 for (i = 0; i < s->num_p2p_clients; i++) {
4774 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4775 peer, ETH_ALEN) != 0)
4778 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4779 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4780 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4783 s->num_p2p_clients--;
4788 p2p_config_write(wpa_s);
4790 /* Return TRUE if valid SSID remains */
4795 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4796 const u8 *adv_mac, const u8 *ses_mac,
4797 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4798 u8 conncap, int passwd_id,
4799 const u8 *persist_ssid,
4800 size_t persist_ssid_size, int response_done,
4801 int prov_start, const char *session_info)
4803 struct wpa_supplicant *wpa_s = ctx;
4805 struct wpa_ssid *persistent_go, *stale, *s;
4806 int save_config = 0;
4807 struct wpa_supplicant *go_wpa_s;
4812 os_memset(mac, 0, ETH_ALEN);
4821 if (session_info == NULL) {
4822 wpa_msg_global(wpa_s, MSG_INFO,
4823 P2P_EVENT_P2PS_PROVISION_START MACSTR
4824 " adv_id=%x conncap=%x"
4826 " session=%x mac=" MACSTR
4827 " dev_passwd_id=%d",
4828 MAC2STR(dev), adv_id, conncap,
4830 ses_id, MAC2STR(ses_mac),
4833 wpa_msg_global(wpa_s, MSG_INFO,
4834 P2P_EVENT_P2PS_PROVISION_START MACSTR
4835 " adv_id=%x conncap=%x"
4837 " session=%x mac=" MACSTR
4838 " dev_passwd_id=%d info='%s'",
4839 MAC2STR(dev), adv_id, conncap,
4841 ses_id, MAC2STR(ses_mac),
4842 passwd_id, session_info);
4847 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4848 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4850 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4851 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4852 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4854 if (persistent_go && !persistent_go->num_p2p_clients) {
4855 /* remove empty persistent GO */
4856 wpa_config_remove_network(wpa_s->conf,
4860 wpa_msg_global(wpa_s, MSG_INFO,
4861 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4863 " adv_id=%x adv_mac=" MACSTR
4864 " session=%x mac=" MACSTR,
4865 MAC2STR(dev), status,
4866 adv_id, MAC2STR(adv_mac),
4867 ses_id, MAC2STR(ses_mac));
4871 /* Clean up stale persistent groups with this device */
4872 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4875 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4879 if (s && s->ssid_len == stale->ssid_len &&
4880 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4881 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4884 /* Remove stale persistent group */
4885 if (stale->mode != WPAS_MODE_P2P_GO ||
4886 stale->num_p2p_clients <= 1) {
4887 wpa_config_remove_network(wpa_s->conf, stale->id);
4891 for (i = 0; i < stale->num_p2p_clients; i++) {
4892 if (os_memcmp(stale->p2p_client_list +
4894 dev, ETH_ALEN) == 0) {
4895 os_memmove(stale->p2p_client_list +
4897 stale->p2p_client_list +
4899 (stale->num_p2p_clients -
4904 stale->num_p2p_clients--;
4910 p2p_config_write(wpa_s);
4913 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4914 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4916 if (persistent_go && s != persistent_go &&
4917 !persistent_go->num_p2p_clients) {
4918 /* remove empty persistent GO */
4919 wpa_config_remove_network(wpa_s->conf,
4924 wpa_msg_global(wpa_s, MSG_INFO,
4925 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4927 " adv_id=%x adv_mac=" MACSTR
4928 " session=%x mac=" MACSTR
4930 MAC2STR(dev), status,
4931 adv_id, MAC2STR(adv_mac),
4932 ses_id, MAC2STR(ses_mac), s->id);
4936 if (conncap == P2PS_SETUP_GROUP_OWNER) {
4937 const char *go_ifname = NULL;
4939 wpa_s->global->pending_p2ps_group = 1;
4941 if (wpa_s->conf->p2p_no_group_iface)
4942 go_ifname = wpa_s->ifname;
4943 else if (wpa_s->pending_interface_name[0])
4944 go_ifname = wpa_s->pending_interface_name;
4947 wpas_p2ps_prov_complete(
4948 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4949 dev, adv_mac, ses_mac,
4950 NULL, adv_id, ses_id, 0, 0,
4951 NULL, 0, 0, 0, NULL);
4955 /* If PD Resp complete, start up the GO */
4956 if (response_done && persistent_go) {
4957 wpas_p2p_group_add_persistent(
4958 wpa_s, persistent_go,
4959 0, 0, 0, 0, 0, NULL,
4960 persistent_go->mode ==
4962 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4964 } else if (response_done) {
4965 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
4968 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4969 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
4970 wpa_s->p2ps_join_addr_valid = 1;
4971 wpa_dbg(wpa_s, MSG_DEBUG,
4972 "P2PS: Saving PIN for " MACSTR,
4975 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4976 go_ifname = go_wpa_s->ifname;
4978 wpa_dbg(go_wpa_s, MSG_DEBUG,
4979 "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
4980 wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
4983 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
4984 wpa_s->p2ps_join_addr_valid = 1;
4985 wpa_dbg(wpa_s, MSG_DEBUG,
4986 "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
4989 wpa_msg_global(wpa_s, MSG_INFO,
4990 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4991 " status=%d conncap=%x"
4992 " adv_id=%x adv_mac=" MACSTR
4993 " session=%x mac=" MACSTR
4994 " dev_passwd_id=%d go=%s",
4995 MAC2STR(dev), status, conncap,
4996 adv_id, MAC2STR(adv_mac),
4997 ses_id, MAC2STR(ses_mac),
4998 passwd_id, go_ifname);
5002 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
5003 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
5005 if (persistent_go && !persistent_go->num_p2p_clients) {
5006 /* remove empty persistent GO */
5007 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
5010 if (conncap == P2PS_SETUP_CLIENT) {
5011 wpa_msg_global(wpa_s, MSG_INFO,
5012 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5013 " status=%d conncap=%x"
5014 " adv_id=%x adv_mac=" MACSTR
5015 " session=%x mac=" MACSTR
5016 " dev_passwd_id=%d join=" MACSTR,
5017 MAC2STR(dev), status, conncap,
5018 adv_id, MAC2STR(adv_mac),
5019 ses_id, MAC2STR(ses_mac),
5020 passwd_id, MAC2STR(grp_mac));
5022 wpa_msg_global(wpa_s, MSG_INFO,
5023 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5024 " status=%d conncap=%x"
5025 " adv_id=%x adv_mac=" MACSTR
5026 " session=%x mac=" MACSTR
5027 " dev_passwd_id=%d",
5028 MAC2STR(dev), status, conncap,
5029 adv_id, MAC2STR(adv_mac),
5030 ses_id, MAC2STR(ses_mac),
5036 static int _wpas_p2p_in_progress(void *ctx)
5038 struct wpa_supplicant *wpa_s = ctx;
5039 return wpas_p2p_in_progress(wpa_s);
5043 static int wpas_prov_disc_resp_cb(void *ctx)
5045 struct wpa_supplicant *wpa_s = ctx;
5046 struct wpa_ssid *persistent_go;
5048 if (!wpa_s->global->pending_p2ps_group)
5051 wpa_s->global->pending_p2ps_group = 0;
5053 if (wpas_p2p_get_go_group(wpa_s))
5055 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
5057 if (persistent_go) {
5058 wpas_p2p_group_add_persistent(
5059 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
5060 persistent_go->mode == WPAS_MODE_P2P_GO ?
5061 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
5063 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
5071 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
5072 * @global: Pointer to global data from wpa_supplicant_init()
5073 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5074 * Returns: 0 on success, -1 on failure
5076 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
5078 struct p2p_config p2p;
5081 if (wpa_s->conf->p2p_disabled)
5084 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5090 os_memset(&p2p, 0, sizeof(p2p));
5092 p2p.debug_print = wpas_p2p_debug_print;
5093 p2p.p2p_scan = wpas_p2p_scan;
5094 p2p.send_action = wpas_send_action;
5095 p2p.send_action_done = wpas_send_action_done;
5096 p2p.go_neg_completed = wpas_go_neg_completed;
5097 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
5098 p2p.dev_found = wpas_dev_found;
5099 p2p.dev_lost = wpas_dev_lost;
5100 p2p.find_stopped = wpas_find_stopped;
5101 p2p.start_listen = wpas_start_listen;
5102 p2p.stop_listen = wpas_stop_listen;
5103 p2p.send_probe_resp = wpas_send_probe_resp;
5104 p2p.sd_request = wpas_sd_request;
5105 p2p.sd_response = wpas_sd_response;
5106 p2p.prov_disc_req = wpas_prov_disc_req;
5107 p2p.prov_disc_resp = wpas_prov_disc_resp;
5108 p2p.prov_disc_fail = wpas_prov_disc_fail;
5109 p2p.invitation_process = wpas_invitation_process;
5110 p2p.invitation_received = wpas_invitation_received;
5111 p2p.invitation_result = wpas_invitation_result;
5112 p2p.get_noa = wpas_get_noa;
5113 p2p.go_connected = wpas_go_connected;
5114 p2p.presence_resp = wpas_presence_resp;
5115 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
5116 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
5117 p2p.get_persistent_group = wpas_get_persistent_group;
5118 p2p.get_go_info = wpas_get_go_info;
5119 p2p.remove_stale_groups = wpas_remove_stale_groups;
5120 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
5121 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
5122 p2p.p2ps_group_capability = p2ps_group_capability;
5124 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
5125 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
5126 p2p.dev_name = wpa_s->conf->device_name;
5127 p2p.manufacturer = wpa_s->conf->manufacturer;
5128 p2p.model_name = wpa_s->conf->model_name;
5129 p2p.model_number = wpa_s->conf->model_number;
5130 p2p.serial_number = wpa_s->conf->serial_number;
5132 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
5133 p2p.config_methods = wpa_s->wps->config_methods;
5136 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
5137 wpa_printf(MSG_ERROR,
5138 "P2P: Failed to configure supported channel list");
5142 if (wpa_s->conf->p2p_listen_reg_class &&
5143 wpa_s->conf->p2p_listen_channel) {
5144 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5145 p2p.channel = wpa_s->conf->p2p_listen_channel;
5146 p2p.channel_forced = 1;
5149 * Pick one of the social channels randomly as the listen
5152 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
5153 &p2p.channel) != 0) {
5154 wpa_printf(MSG_ERROR,
5155 "P2P: Failed to select random social channel as listen channel");
5158 p2p.channel_forced = 0;
5160 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5161 p2p.reg_class, p2p.channel);
5163 if (wpa_s->conf->p2p_oper_reg_class &&
5164 wpa_s->conf->p2p_oper_channel) {
5165 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5166 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5167 p2p.cfg_op_channel = 1;
5168 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5169 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5173 * Use random operation channel from 2.4 GHz band social
5174 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5175 * other preference is indicated.
5177 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
5178 &p2p.op_channel) != 0) {
5179 wpa_printf(MSG_ERROR,
5180 "P2P: Failed to select random social channel as operation channel");
5183 p2p.cfg_op_channel = 0;
5184 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5185 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5188 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5189 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5190 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5193 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5194 os_memcpy(p2p.country, wpa_s->conf->country, 2);
5195 p2p.country[2] = 0x04;
5197 os_memcpy(p2p.country, "XX\x04", 3);
5199 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5202 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5203 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5204 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5206 p2p.concurrent_operations = !!(wpa_s->drv_flags &
5207 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5209 p2p.max_peers = 100;
5211 if (wpa_s->conf->p2p_ssid_postfix) {
5212 p2p.ssid_postfix_len =
5213 os_strlen(wpa_s->conf->p2p_ssid_postfix);
5214 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5215 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5216 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5217 p2p.ssid_postfix_len);
5220 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5222 p2p.max_listen = wpa_s->max_remain_on_chan;
5224 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5225 wpa_s->conf->p2p_passphrase_len <= 63)
5226 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5228 p2p.passphrase_len = 8;
5230 global->p2p = p2p_init(&p2p);
5231 if (global->p2p == NULL)
5233 global->p2p_init_wpa_s = wpa_s;
5235 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5236 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5238 p2p_add_wps_vendor_extension(
5239 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5242 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5249 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5250 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5252 * This function deinitialize per-interface P2P data.
5254 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5256 if (wpa_s->driver && wpa_s->drv_priv)
5257 wpa_drv_probe_req_report(wpa_s, 0);
5259 if (wpa_s->go_params) {
5260 /* Clear any stored provisioning info */
5261 p2p_clear_provisioning_info(
5263 wpa_s->go_params->peer_device_addr);
5266 os_free(wpa_s->go_params);
5267 wpa_s->go_params = NULL;
5268 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
5269 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5270 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5271 wpa_s->p2p_long_listen = 0;
5272 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5273 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5274 wpas_p2p_remove_pending_group_interface(wpa_s);
5275 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
5276 wpas_p2p_listen_work_done(wpa_s);
5277 if (wpa_s->p2p_send_action_work) {
5278 os_free(wpa_s->p2p_send_action_work->ctx);
5279 radio_work_done(wpa_s->p2p_send_action_work);
5280 wpa_s->p2p_send_action_work = NULL;
5282 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
5284 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5285 wpa_s->p2p_oob_dev_pw = NULL;
5287 os_free(wpa_s->p2p_group_common_freqs);
5288 wpa_s->p2p_group_common_freqs = NULL;
5289 wpa_s->p2p_group_common_freqs_num = 0;
5291 /* TODO: remove group interface from the driver if this wpa_s instance
5292 * is on top of a P2P group interface */
5297 * wpas_p2p_deinit_global - Deinitialize global P2P module
5298 * @global: Pointer to global data from wpa_supplicant_init()
5300 * This function deinitializes the global (per device) P2P module.
5302 static void wpas_p2p_deinit_global(struct wpa_global *global)
5304 struct wpa_supplicant *wpa_s, *tmp;
5306 wpa_s = global->ifaces;
5308 wpas_p2p_service_flush(global->p2p_init_wpa_s);
5310 /* Remove remaining P2P group interfaces */
5311 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5312 wpa_s = wpa_s->next;
5314 tmp = global->ifaces;
5317 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5322 /* Disconnect from the P2P group and deinit the interface */
5323 wpas_p2p_disconnect(tmp);
5327 * Deinit GO data on any possibly remaining interface (if main
5328 * interface is used as GO).
5330 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5331 if (wpa_s->ap_iface)
5332 wpas_p2p_group_deinit(wpa_s);
5335 p2p_deinit(global->p2p);
5337 global->p2p_init_wpa_s = NULL;
5341 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5343 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
5344 wpa_s->conf->p2p_no_group_iface)
5345 return 0; /* separate interface disabled per configuration */
5346 if (wpa_s->drv_flags &
5347 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5348 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5349 return 1; /* P2P group requires a new interface in every case
5351 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5352 return 0; /* driver does not support concurrent operations */
5353 if (wpa_s->global->ifaces->next)
5354 return 1; /* more that one interface already in use */
5355 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5356 return 1; /* this interface is already in use */
5361 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5362 const u8 *peer_addr,
5363 enum p2p_wps_method wps_method,
5364 int go_intent, const u8 *own_interface_addr,
5365 unsigned int force_freq, int persistent_group,
5366 struct wpa_ssid *ssid, unsigned int pref_freq)
5368 if (persistent_group && wpa_s->conf->persistent_reconnect)
5369 persistent_group = 2;
5372 * Increase GO config timeout if HT40 is used since it takes some time
5373 * to scan channels for coex purposes before the BSS can be started.
5375 p2p_set_config_timeout(wpa_s->global->p2p,
5376 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5378 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5379 go_intent, own_interface_addr, force_freq,
5380 persistent_group, ssid ? ssid->ssid : NULL,
5381 ssid ? ssid->ssid_len : 0,
5382 wpa_s->p2p_pd_before_go_neg, pref_freq,
5383 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5388 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5389 const u8 *peer_addr,
5390 enum p2p_wps_method wps_method,
5391 int go_intent, const u8 *own_interface_addr,
5392 unsigned int force_freq, int persistent_group,
5393 struct wpa_ssid *ssid, unsigned int pref_freq)
5395 if (persistent_group && wpa_s->conf->persistent_reconnect)
5396 persistent_group = 2;
5398 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5399 go_intent, own_interface_addr, force_freq,
5400 persistent_group, ssid ? ssid->ssid : NULL,
5401 ssid ? ssid->ssid_len : 0, pref_freq,
5402 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5407 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5409 wpa_s->p2p_join_scan_count++;
5410 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5411 wpa_s->p2p_join_scan_count);
5412 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5413 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5414 " for join operationg - stop join attempt",
5415 MAC2STR(wpa_s->pending_join_iface_addr));
5416 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5417 if (wpa_s->p2p_auto_pd) {
5418 wpa_s->p2p_auto_pd = 0;
5419 wpa_msg_global(wpa_s, MSG_INFO,
5420 P2P_EVENT_PROV_DISC_FAILURE
5421 " p2p_dev_addr=" MACSTR " status=N/A",
5422 MAC2STR(wpa_s->pending_join_dev_addr));
5425 wpa_msg_global(wpa_s->parent, MSG_INFO,
5426 P2P_EVENT_GROUP_FORMATION_FAILURE);
5431 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5434 unsigned int num, i;
5435 struct wpa_used_freq_data *freqs;
5437 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5438 /* Multiple channels are supported and not all are in use */
5442 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5443 sizeof(struct wpa_used_freq_data));
5447 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5448 wpa_s->num_multichan_concurrent);
5450 for (i = 0; i < num; i++) {
5451 if (freqs[i].freq == freq) {
5452 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5459 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
5468 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5469 const u8 *peer_dev_addr)
5471 struct wpa_bss *bss;
5474 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5477 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5478 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5483 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5485 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5486 "%ld.%06ld (%supdated in last scan)",
5487 bss->last_update.sec, bss->last_update.usec,
5488 updated ? "": "not ");
5494 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5495 struct wpa_scan_results *scan_res)
5497 struct wpa_bss *bss = NULL;
5499 u8 iface_addr[ETH_ALEN];
5501 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5503 if (wpa_s->global->p2p_disabled)
5506 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5507 scan_res ? (int) scan_res->num : -1,
5508 wpa_s->p2p_auto_join ? "auto_" : "");
5511 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5513 if (wpa_s->p2p_auto_pd) {
5514 int join = wpas_p2p_peer_go(wpa_s,
5515 wpa_s->pending_join_dev_addr);
5517 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5518 wpa_s->auto_pd_scan_retry++;
5519 bss = wpa_bss_get_bssid_latest(
5520 wpa_s, wpa_s->pending_join_dev_addr);
5523 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5524 "the peer " MACSTR " at %d MHz",
5525 wpa_s->auto_pd_scan_retry,
5527 pending_join_dev_addr),
5529 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
5537 wpa_s->p2p_auto_pd = 0;
5538 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5539 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5540 MAC2STR(wpa_s->pending_join_dev_addr), join);
5541 if (p2p_prov_disc_req(wpa_s->global->p2p,
5542 wpa_s->pending_join_dev_addr, NULL,
5543 wpa_s->pending_pd_config_methods, join,
5544 0, wpa_s->user_initiated_pd) < 0) {
5545 wpa_s->p2p_auto_pd = 0;
5546 wpa_msg_global(wpa_s, MSG_INFO,
5547 P2P_EVENT_PROV_DISC_FAILURE
5548 " p2p_dev_addr=" MACSTR " status=N/A",
5549 MAC2STR(wpa_s->pending_join_dev_addr));
5554 if (wpa_s->p2p_auto_join) {
5555 int join = wpas_p2p_peer_go(wpa_s,
5556 wpa_s->pending_join_dev_addr);
5558 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5559 "running a GO -> use GO Negotiation");
5560 wpa_msg_global(wpa_s->parent, MSG_INFO,
5561 P2P_EVENT_FALLBACK_TO_GO_NEG
5562 "reason=peer-not-running-GO");
5563 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5564 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5565 wpa_s->p2p_persistent_group, 0, 0, 0,
5566 wpa_s->p2p_go_intent,
5567 wpa_s->p2p_connect_freq,
5568 wpa_s->p2p_persistent_id,
5569 wpa_s->p2p_pd_before_go_neg,
5575 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5576 "try to join the group", join ? "" :
5579 wpa_msg_global(wpa_s->parent, MSG_INFO,
5580 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
5581 wpa_s->p2p_fallback_to_go_neg = 1;
5585 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5586 wpa_s->pending_join_iface_addr);
5588 p2p_get_interface_addr(wpa_s->global->p2p,
5589 wpa_s->pending_join_dev_addr,
5591 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5592 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
5593 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5594 "address for join from " MACSTR " to " MACSTR
5595 " based on newly discovered P2P peer entry",
5596 MAC2STR(wpa_s->pending_join_iface_addr),
5597 MAC2STR(iface_addr));
5598 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5601 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5602 wpa_s->pending_join_iface_addr);
5605 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5606 "from P2P peer table: %d MHz", freq);
5608 if (wpa_s->p2p_join_ssid_len) {
5609 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5610 MACSTR " and SSID %s",
5611 MAC2STR(wpa_s->pending_join_iface_addr),
5612 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5613 wpa_s->p2p_join_ssid_len));
5614 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5615 wpa_s->p2p_join_ssid,
5616 wpa_s->p2p_join_ssid_len);
5619 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5620 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5621 bss = wpa_bss_get_bssid_latest(wpa_s,
5622 wpa_s->pending_join_iface_addr);
5626 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5627 "from BSS table: %d MHz (SSID %s)", freq,
5628 wpa_ssid_txt(bss->ssid, bss->ssid_len));
5633 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
5634 wpa_msg_global(wpa_s->parent, MSG_INFO,
5635 P2P_EVENT_GROUP_FORMATION_FAILURE
5636 "reason=FREQ_CONFLICT");
5640 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5641 "prior to joining an existing group (GO " MACSTR
5643 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5644 wpa_s->pending_pd_before_join = 1;
5646 switch (wpa_s->pending_join_wps_method) {
5647 case WPS_PIN_DISPLAY:
5648 method = WPS_CONFIG_KEYPAD;
5650 case WPS_PIN_KEYPAD:
5651 method = WPS_CONFIG_DISPLAY;
5654 method = WPS_CONFIG_PUSHBUTTON;
5661 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5662 wpa_s->pending_join_dev_addr) ==
5665 * We have already performed provision discovery for
5666 * joining the group. Proceed directly to join
5667 * operation without duplicated provision discovery. */
5668 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5669 "with " MACSTR " already done - proceed to "
5671 MAC2STR(wpa_s->pending_join_dev_addr));
5672 wpa_s->pending_pd_before_join = 0;
5676 if (p2p_prov_disc_req(wpa_s->global->p2p,
5677 wpa_s->pending_join_dev_addr,
5679 freq, wpa_s->user_initiated_pd) < 0) {
5680 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5681 "Discovery Request before joining an "
5683 wpa_s->pending_pd_before_join = 0;
5689 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5690 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5691 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5692 wpas_p2p_check_join_scan_limit(wpa_s);
5696 /* Start join operation immediately */
5697 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
5701 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5702 const u8 *ssid, size_t ssid_len)
5705 struct wpa_driver_scan_params params;
5706 struct wpabuf *wps_ie, *ies;
5708 int freqs[2] = { 0, 0 };
5710 os_memset(¶ms, 0, sizeof(params));
5712 /* P2P Wildcard SSID */
5713 params.num_ssids = 1;
5714 if (ssid && ssid_len) {
5715 params.ssids[0].ssid = ssid;
5716 params.ssids[0].ssid_len = ssid_len;
5717 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5718 wpa_s->p2p_join_ssid_len = ssid_len;
5720 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5721 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5722 wpa_s->p2p_join_ssid_len = 0;
5725 wpa_s->wps->dev.p2p = 1;
5726 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5727 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5729 if (wps_ie == NULL) {
5730 wpas_p2p_scan_res_join(wpa_s, NULL);
5734 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5735 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5737 wpabuf_free(wps_ie);
5738 wpas_p2p_scan_res_join(wpa_s, NULL);
5741 wpabuf_put_buf(ies, wps_ie);
5742 wpabuf_free(wps_ie);
5744 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5746 params.p2p_probe = 1;
5747 params.extra_ies = wpabuf_head(ies);
5748 params.extra_ies_len = wpabuf_len(ies);
5753 * If freq is not provided, check the operating freq of the GO
5754 * and use a single channel scan on if possible.
5756 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5757 wpa_s->pending_join_iface_addr);
5763 params.freqs = freqs;
5767 * Run a scan to update BSS table and start Provision Discovery once
5768 * the new scan results become available.
5770 ret = wpa_drv_scan(wpa_s, ¶ms);
5772 os_get_reltime(&wpa_s->scan_trigger_time);
5773 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
5774 wpa_s->own_scan_requested = 1;
5780 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5782 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5783 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5784 wpas_p2p_check_join_scan_limit(wpa_s);
5789 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5791 struct wpa_supplicant *wpa_s = eloop_ctx;
5792 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
5796 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
5797 const u8 *dev_addr, enum p2p_wps_method wps_method,
5798 int auto_join, int op_freq,
5799 const u8 *ssid, size_t ssid_len)
5801 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
5802 MACSTR " dev " MACSTR " op_freq=%d)%s",
5803 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
5804 auto_join ? " (auto_join)" : "");
5805 if (ssid && ssid_len) {
5806 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5807 wpa_ssid_txt(ssid, ssid_len));
5810 wpa_s->p2p_auto_pd = 0;
5811 wpa_s->p2p_auto_join = !!auto_join;
5812 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5813 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5814 wpa_s->pending_join_wps_method = wps_method;
5816 /* Make sure we are not running find during connection establishment */
5817 wpas_p2p_stop_find(wpa_s);
5819 wpa_s->p2p_join_scan_count = 0;
5820 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
5825 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5826 const u8 *ssid, size_t ssid_len)
5828 struct wpa_supplicant *group;
5829 struct p2p_go_neg_results res;
5830 struct wpa_bss *bss;
5832 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5835 if (group != wpa_s) {
5836 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5837 sizeof(group->p2p_pin));
5838 group->p2p_wps_method = wpa_s->p2p_wps_method;
5841 * Need to mark the current interface for p2p_group_formation
5842 * when a separate group interface is not used. This is needed
5843 * to allow p2p_cancel stop a pending p2p_connect-join.
5844 * wpas_p2p_init_group_interface() addresses this for the case
5845 * where a separate group interface is used.
5847 wpa_s->global->p2p_group_formation = wpa_s;
5850 group->p2p_in_provisioning = 1;
5851 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
5853 os_memset(&res, 0, sizeof(res));
5854 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
5855 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5857 res.wps_method = wpa_s->pending_join_wps_method;
5858 if (freq && ssid && ssid_len) {
5860 res.ssid_len = ssid_len;
5861 os_memcpy(res.ssid, ssid, ssid_len);
5863 bss = wpa_bss_get_bssid_latest(wpa_s,
5864 wpa_s->pending_join_iface_addr);
5866 res.freq = bss->freq;
5867 res.ssid_len = bss->ssid_len;
5868 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5869 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5871 wpa_ssid_txt(bss->ssid, bss->ssid_len));
5875 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5876 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5878 wpa_drv_cancel_remain_on_channel(wpa_s);
5879 wpa_s->off_channel_freq = 0;
5880 wpa_s->roc_waiting_drv_freq = 0;
5882 wpas_start_wps_enrollee(group, &res);
5885 * Allow a longer timeout for join-a-running-group than normal 15
5886 * second group formation timeout since the GO may not have authorized
5887 * our connection yet.
5889 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5890 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5897 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
5898 int *force_freq, int *pref_freq, int go)
5900 struct wpa_used_freq_data *freqs;
5901 int res, best_freq, num_unused;
5902 unsigned int freq_in_use = 0, num, i;
5904 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5905 sizeof(struct wpa_used_freq_data));
5909 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5910 wpa_s->num_multichan_concurrent);
5913 * It is possible that the total number of used frequencies is bigger
5914 * than the number of frequencies used for P2P, so get the system wide
5915 * number of unused frequencies.
5917 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5919 wpa_printf(MSG_DEBUG,
5920 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5921 freq, wpa_s->num_multichan_concurrent, num, num_unused);
5926 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5928 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5930 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
5931 "(%u MHz) is not supported for P2P uses",
5937 for (i = 0; i < num; i++) {
5938 if (freqs[i].freq == freq)
5942 if (num_unused <= 0 && !freq_in_use) {
5943 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5948 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5949 "requested channel (%u MHz)", freq);
5954 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5956 /* We have a candidate frequency to use */
5957 if (best_freq > 0) {
5958 if (*pref_freq == 0 && num_unused > 0) {
5959 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
5961 *pref_freq = best_freq;
5963 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
5965 *force_freq = best_freq;
5967 } else if (num_unused > 0) {
5968 wpa_printf(MSG_DEBUG,
5969 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5972 wpa_printf(MSG_DEBUG,
5973 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5987 * wpas_p2p_connect - Request P2P Group Formation to be started
5988 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5989 * @peer_addr: Address of the peer P2P Device
5990 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5991 * @persistent_group: Whether to create a persistent group
5992 * @auto_join: Whether to select join vs. GO Negotiation automatically
5993 * @join: Whether to join an existing group (as a client) instead of starting
5994 * Group Owner negotiation; @peer_addr is BSSID in that case
5995 * @auth: Whether to only authorize the connection instead of doing that and
5996 * initiating Group Owner negotiation
5997 * @go_intent: GO Intent or -1 to use default
5998 * @freq: Frequency for the group or 0 for auto-selection
5999 * @persistent_id: Persistent group credentials to use for forcing GO
6000 * parameters or -1 to generate new values (SSID/passphrase)
6001 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
6002 * interoperability workaround when initiating group formation
6003 * @ht40: Start GO with 40 MHz channel width
6004 * @vht: Start GO with VHT support
6005 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6006 * failure, -2 on failure due to channel not currently available,
6007 * -3 if forced channel is not supported
6009 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6010 const char *pin, enum p2p_wps_method wps_method,
6011 int persistent_group, int auto_join, int join, int auth,
6012 int go_intent, int freq, int persistent_id, int pd,
6015 int force_freq = 0, pref_freq = 0;
6017 enum wpa_driver_if_type iftype;
6019 struct wpa_ssid *ssid = NULL;
6021 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6024 if (persistent_id >= 0) {
6025 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6026 if (ssid == NULL || ssid->disabled != 2 ||
6027 ssid->mode != WPAS_MODE_P2P_GO)
6031 os_free(wpa_s->global->add_psk);
6032 wpa_s->global->add_psk = NULL;
6034 wpa_s->global->p2p_fail_on_wps_complete = 0;
6035 wpa_s->global->pending_p2ps_group = 0;
6038 go_intent = wpa_s->conf->p2p_go_intent;
6041 wpa_s->p2p_long_listen = 0;
6043 wpa_s->p2p_wps_method = wps_method;
6044 wpa_s->p2p_persistent_group = !!persistent_group;
6045 wpa_s->p2p_persistent_id = persistent_id;
6046 wpa_s->p2p_go_intent = go_intent;
6047 wpa_s->p2p_connect_freq = freq;
6048 wpa_s->p2p_fallback_to_go_neg = 0;
6049 wpa_s->p2p_pd_before_go_neg = !!pd;
6050 wpa_s->p2p_go_ht40 = !!ht40;
6051 wpa_s->p2p_go_vht = !!vht;
6054 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6055 else if (wps_method == WPS_PIN_DISPLAY) {
6056 ret = wps_generate_pin();
6057 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6059 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6060 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
6061 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6064 wpa_s->p2p_pin[0] = '\0';
6066 if (join || auto_join) {
6067 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6069 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6070 "connect a running group from " MACSTR,
6071 MAC2STR(peer_addr));
6072 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6075 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6076 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6078 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6079 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6083 os_get_reltime(&wpa_s->p2p_auto_started);
6084 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6086 wpa_s->p2p_auto_started.sec,
6087 wpa_s->p2p_auto_started.usec);
6089 wpa_s->user_initiated_pd = 1;
6090 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
6091 auto_join, freq, NULL, 0) < 0)
6096 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6100 wpas_p2p_set_own_freq_preference(wpa_s,
6101 force_freq ? force_freq : pref_freq);
6103 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6105 if (wpa_s->create_p2p_iface) {
6106 /* Prepare to add a new interface for the group */
6107 iftype = WPA_IF_P2P_GROUP;
6108 if (go_intent == 15)
6109 iftype = WPA_IF_P2P_GO;
6110 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6111 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6112 "interface for the group");
6116 if_addr = wpa_s->pending_interface_addr;
6118 if_addr = wpa_s->own_addr;
6121 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
6123 force_freq, persistent_group, ssid,
6129 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6130 go_intent, if_addr, force_freq,
6131 persistent_group, ssid, pref_freq) < 0) {
6132 if (wpa_s->create_p2p_iface)
6133 wpas_p2p_remove_pending_group_interface(wpa_s);
6141 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6142 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6143 * @freq: Frequency of the channel in MHz
6144 * @duration: Duration of the stay on the channel in milliseconds
6146 * This callback is called when the driver indicates that it has started the
6147 * requested remain-on-channel duration.
6149 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6150 unsigned int freq, unsigned int duration)
6152 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6154 wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
6155 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6156 wpa_s->roc_waiting_drv_freq, freq, duration);
6157 if (wpa_s->off_channel_freq &&
6158 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
6159 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6160 wpa_s->pending_listen_duration);
6161 wpa_s->pending_listen_freq = 0;
6163 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6164 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6170 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
6172 /* Limit maximum Listen state time based on driver limitation. */
6173 if (timeout > wpa_s->max_remain_on_chan)
6174 timeout = wpa_s->max_remain_on_chan;
6176 return p2p_listen(wpa_s->global->p2p, timeout);
6181 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6182 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6183 * @freq: Frequency of the channel in MHz
6185 * This callback is called when the driver indicates that a remain-on-channel
6186 * operation has been completed, i.e., the duration on the requested channel
6189 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6192 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6193 "(p2p_long_listen=%d ms pending_action_tx=%p)",
6194 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
6195 wpas_p2p_listen_work_done(wpa_s);
6196 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6198 if (wpa_s->p2p_long_listen > 0)
6199 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
6200 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6201 return; /* P2P module started a new operation */
6202 if (offchannel_pending_action_tx(wpa_s))
6204 if (wpa_s->p2p_long_listen > 0) {
6205 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
6206 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
6209 * When listen duration is over, stop listen & update p2p_state
6212 p2p_stop_listen(wpa_s->global->p2p);
6218 * wpas_p2p_group_remove - Remove a P2P group
6219 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6220 * @ifname: Network interface name of the group interface or "*" to remove all
6222 * Returns: 0 on success, -1 on failure
6224 * This function is used to remove a P2P group. This can be used to disconnect
6225 * from a group in which the local end is a P2P Client or to end a P2P Group in
6226 * case the local end is the Group Owner. If a virtual network interface was
6227 * created for this group, that interface will be removed. Otherwise, only the
6228 * configured P2P group network will be removed from the interface.
6230 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6232 struct wpa_global *global = wpa_s->global;
6233 struct wpa_supplicant *calling_wpa_s = wpa_s;
6235 if (os_strcmp(ifname, "*") == 0) {
6236 struct wpa_supplicant *prev;
6237 wpa_s = global->ifaces;
6240 wpa_s = wpa_s->next;
6241 if (prev->p2p_group_interface !=
6242 NOT_P2P_GROUP_INTERFACE ||
6243 (prev->current_ssid &&
6244 prev->current_ssid->p2p_group))
6245 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
6250 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6251 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6255 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
6259 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6264 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6266 if (wpa_s->best_24_freq > 0 &&
6267 p2p_supported_freq_go(wpa_s->global->p2p,
6268 wpa_s->best_24_freq)) {
6269 freq = wpa_s->best_24_freq;
6270 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6271 "channel: %d MHz", freq);
6273 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6275 freq = 2412 + (r % 3) * 25;
6276 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6277 "channel: %d MHz", freq);
6282 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6284 if (wpa_s->best_5_freq > 0 &&
6285 p2p_supported_freq_go(wpa_s->global->p2p,
6286 wpa_s->best_5_freq)) {
6287 freq = wpa_s->best_5_freq;
6288 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6289 "channel: %d MHz", freq);
6291 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6293 freq = 5180 + (r % 4) * 20;
6294 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6295 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6296 "5 GHz channel for P2P group");
6299 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6300 "channel: %d MHz", freq);
6304 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6305 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6306 "(%u MHz) is not supported for P2P uses",
6315 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
6316 struct p2p_go_neg_results *params,
6317 const struct p2p_channels *channels)
6321 /* first try some random selection of the social channels */
6322 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6325 for (i = 0; i < 3; i++) {
6326 params->freq = 2412 + ((r + i) % 3) * 25;
6327 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6328 freq_included(channels, params->freq) &&
6329 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6333 /* try all channels in reg. class 81 */
6334 for (i = 0; i < 11; i++) {
6335 params->freq = 2412 + i * 5;
6336 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6337 freq_included(channels, params->freq) &&
6338 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6342 /* try all channels in operating class 115 */
6343 for (i = 0; i < 4; i++) {
6344 params->freq = 5180 + i * 20;
6345 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6346 freq_included(channels, params->freq) &&
6347 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6351 /* try all channels in operating class 124 */
6352 for (i = 0; i < 4; i++) {
6353 params->freq = 5745 + i * 20;
6354 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6355 freq_included(channels, params->freq) &&
6356 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6360 /* try social channel class 180 channel 2 */
6361 params->freq = 58320 + 1 * 2160;
6362 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6363 freq_included(channels, params->freq) &&
6364 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6367 /* try all channels in reg. class 180 */
6368 for (i = 0; i < 4; i++) {
6369 params->freq = 58320 + i * 2160;
6370 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6371 freq_included(channels, params->freq) &&
6372 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6376 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
6379 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6385 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6386 struct p2p_go_neg_results *params,
6387 int freq, int ht40, int vht,
6388 const struct p2p_channels *channels)
6390 struct wpa_used_freq_data *freqs;
6391 unsigned int pref_freq, cand_freq;
6392 unsigned int num, i;
6394 os_memset(params, 0, sizeof(*params));
6395 params->role_go = 1;
6396 params->ht40 = ht40;
6399 if (!freq_included(channels, freq)) {
6400 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
6404 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
6405 "frequency %d MHz", freq);
6406 params->freq = freq;
6407 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6408 wpa_s->conf->p2p_oper_channel >= 1 &&
6409 wpa_s->conf->p2p_oper_channel <= 11 &&
6410 freq_included(channels,
6411 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6412 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6413 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6414 "frequency %d MHz", params->freq);
6415 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6416 wpa_s->conf->p2p_oper_reg_class == 116 ||
6417 wpa_s->conf->p2p_oper_reg_class == 117 ||
6418 wpa_s->conf->p2p_oper_reg_class == 124 ||
6419 wpa_s->conf->p2p_oper_reg_class == 126 ||
6420 wpa_s->conf->p2p_oper_reg_class == 127) &&
6421 freq_included(channels,
6422 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
6423 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6424 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6425 "frequency %d MHz", params->freq);
6426 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6427 wpa_s->best_overall_freq > 0 &&
6428 p2p_supported_freq_go(wpa_s->global->p2p,
6429 wpa_s->best_overall_freq) &&
6430 freq_included(channels, wpa_s->best_overall_freq)) {
6431 params->freq = wpa_s->best_overall_freq;
6432 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6433 "channel %d MHz", params->freq);
6434 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6435 wpa_s->best_24_freq > 0 &&
6436 p2p_supported_freq_go(wpa_s->global->p2p,
6437 wpa_s->best_24_freq) &&
6438 freq_included(channels, wpa_s->best_24_freq)) {
6439 params->freq = wpa_s->best_24_freq;
6440 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6441 "channel %d MHz", params->freq);
6442 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6443 wpa_s->best_5_freq > 0 &&
6444 p2p_supported_freq_go(wpa_s->global->p2p,
6445 wpa_s->best_5_freq) &&
6446 freq_included(channels, wpa_s->best_5_freq)) {
6447 params->freq = wpa_s->best_5_freq;
6448 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6449 "channel %d MHz", params->freq);
6450 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
6452 params->freq = pref_freq;
6453 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6454 "channels", params->freq);
6456 /* no preference, select some channel */
6457 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
6461 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6462 sizeof(struct wpa_used_freq_data));
6466 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
6467 wpa_s->num_multichan_concurrent);
6469 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6471 /* First try the best used frequency if possible */
6472 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
6473 params->freq = cand_freq;
6475 /* Try any of the used frequencies */
6476 for (i = 0; i < num; i++) {
6477 if (freq_included(channels, freqs[i].freq)) {
6478 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
6480 params->freq = freqs[i].freq;
6486 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
6487 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
6491 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
6495 for (i = 0; i < num; i++) {
6496 if (freqs[i].freq == freq)
6501 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
6503 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
6507 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
6517 static struct wpa_supplicant *
6518 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6521 struct wpa_supplicant *group_wpa_s;
6523 if (!wpas_p2p_create_iface(wpa_s)) {
6524 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
6526 wpa_s->p2p_first_connection_timeout = 0;
6530 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
6531 WPA_IF_P2P_CLIENT) < 0) {
6532 wpa_msg_global(wpa_s, MSG_ERROR,
6533 "P2P: Failed to add group interface");
6536 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6537 if (group_wpa_s == NULL) {
6538 wpa_msg_global(wpa_s, MSG_ERROR,
6539 "P2P: Failed to initialize group interface");
6540 wpas_p2p_remove_pending_group_interface(wpa_s);
6544 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6545 group_wpa_s->ifname);
6546 group_wpa_s->p2p_first_connection_timeout = 0;
6552 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6553 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6554 * @persistent_group: Whether to create a persistent group
6555 * @freq: Frequency for the group or 0 to indicate no hardcoding
6556 * @ht40: Start GO with 40 MHz channel width
6557 * @vht: Start GO with VHT support
6558 * Returns: 0 on success, -1 on failure
6560 * This function creates a new P2P group with the local end as the Group Owner,
6561 * i.e., without using Group Owner Negotiation.
6563 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
6564 int freq, int ht40, int vht)
6566 struct p2p_go_neg_results params;
6568 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6571 os_free(wpa_s->global->add_psk);
6572 wpa_s->global->add_psk = NULL;
6574 /* Make sure we are not running find during connection establishment */
6575 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
6576 wpas_p2p_stop_find_oper(wpa_s);
6578 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6582 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
6585 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
6586 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
6587 "(%u MHz) is not supported for P2P uses",
6591 p2p_go_params(wpa_s->global->p2p, ¶ms);
6592 params.persistent_group = persistent_group;
6594 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6597 wpas_start_wps_go(wpa_s, ¶ms, 0);
6603 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
6604 struct wpa_ssid *params, int addr_allocated,
6607 struct wpa_ssid *ssid;
6609 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6612 wpa_s->p2p_last_4way_hs_fail = NULL;
6614 wpa_supplicant_ap_deinit(wpa_s);
6616 ssid = wpa_config_add_network(wpa_s->conf);
6619 wpa_config_set_network_defaults(ssid);
6620 ssid->temporary = 1;
6621 ssid->proto = WPA_PROTO_RSN;
6622 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
6623 ssid->group_cipher = WPA_CIPHER_CCMP;
6624 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6625 ssid->ssid = os_malloc(params->ssid_len);
6626 if (ssid->ssid == NULL) {
6627 wpa_config_remove_network(wpa_s->conf, ssid->id);
6630 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6631 ssid->ssid_len = params->ssid_len;
6632 ssid->p2p_group = 1;
6633 ssid->export_keys = 1;
6634 if (params->psk_set) {
6635 os_memcpy(ssid->psk, params->psk, 32);
6638 if (params->passphrase)
6639 ssid->passphrase = os_strdup(params->passphrase);
6641 wpa_s->show_group_started = 1;
6642 wpa_s->p2p_in_invitation = 1;
6643 wpa_s->p2p_invite_go_freq = freq;
6645 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6647 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6648 wpas_p2p_group_formation_timeout,
6649 wpa_s->parent, NULL);
6650 wpa_supplicant_select_network(wpa_s, ssid);
6656 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6657 struct wpa_ssid *ssid, int addr_allocated,
6658 int force_freq, int neg_freq, int ht40,
6659 int vht, const struct p2p_channels *channels,
6660 int connection_timeout)
6662 struct p2p_go_neg_results params;
6665 if (ssid->disabled != 2 || ssid->ssid == NULL)
6668 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6669 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6670 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6675 os_free(wpa_s->global->add_psk);
6676 wpa_s->global->add_psk = NULL;
6678 /* Make sure we are not running find during connection establishment */
6679 wpas_p2p_stop_find_oper(wpa_s);
6681 wpa_s->p2p_fallback_to_go_neg = 0;
6683 if (ssid->mode == WPAS_MODE_P2P_GO) {
6684 if (force_freq > 0) {
6685 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6689 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6691 (freq > 0 && !freq_included(channels, freq)))
6697 (freq > 0 && !freq_included(channels, freq)))
6701 if (ssid->mode == WPAS_MODE_INFRA)
6702 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
6704 if (ssid->mode != WPAS_MODE_P2P_GO)
6707 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
6711 params.psk_set = ssid->psk_set;
6713 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
6714 if (ssid->passphrase) {
6715 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6716 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6717 "persistent group");
6720 os_strlcpy(params.passphrase, ssid->passphrase,
6721 sizeof(params.passphrase));
6723 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6724 params.ssid_len = ssid->ssid_len;
6725 params.persistent_group = 1;
6727 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6731 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6733 wpa_s->p2p_first_connection_timeout = connection_timeout;
6734 wpas_start_wps_go(wpa_s, ¶ms, 0);
6740 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6741 struct wpabuf *proberesp_ies)
6743 struct wpa_supplicant *wpa_s = ctx;
6744 if (wpa_s->ap_iface) {
6745 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
6746 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6747 wpabuf_free(beacon_ies);
6748 wpabuf_free(proberesp_ies);
6752 wpabuf_free(hapd->p2p_beacon_ie);
6753 hapd->p2p_beacon_ie = beacon_ies;
6755 wpabuf_free(hapd->p2p_probe_resp_ie);
6756 hapd->p2p_probe_resp_ie = proberesp_ies;
6758 wpabuf_free(beacon_ies);
6759 wpabuf_free(proberesp_ies);
6761 wpa_supplicant_ap_update_beacon(wpa_s);
6765 static void wpas_p2p_idle_update(void *ctx, int idle)
6767 struct wpa_supplicant *wpa_s = ctx;
6768 if (!wpa_s->ap_iface)
6770 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
6772 if (wpa_s->global->p2p_fail_on_wps_complete &&
6773 wpa_s->p2p_in_provisioning) {
6774 wpas_p2p_grpform_fail_after_wps(wpa_s);
6777 wpas_p2p_set_group_idle_timeout(wpa_s);
6779 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6783 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6784 struct wpa_ssid *ssid)
6786 struct p2p_group *group;
6787 struct p2p_group_config *cfg;
6789 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6792 cfg = os_zalloc(sizeof(*cfg));
6796 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
6797 cfg->persistent_group = 2;
6798 else if (ssid->p2p_persistent_group)
6799 cfg->persistent_group = 1;
6800 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6801 if (wpa_s->max_stations &&
6802 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6803 cfg->max_clients = wpa_s->max_stations;
6805 cfg->max_clients = wpa_s->conf->max_num_sta;
6806 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6807 cfg->ssid_len = ssid->ssid_len;
6808 cfg->freq = ssid->frequency;
6809 cfg->cb_ctx = wpa_s;
6810 cfg->ie_update = wpas_p2p_ie_update;
6811 cfg->idle_update = wpas_p2p_idle_update;
6813 group = p2p_group_init(wpa_s->global->p2p, cfg);
6816 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
6817 p2p_group_notif_formation_done(group);
6818 wpa_s->p2p_group = group;
6823 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6826 struct wpa_ssid *ssid = wpa_s->current_ssid;
6828 if (!wpa_s->p2p_in_provisioning) {
6829 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6830 "provisioning not in progress");
6834 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6835 u8 go_dev_addr[ETH_ALEN];
6836 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6837 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6839 /* Clear any stored provisioning info */
6840 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6843 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6845 wpa_s->p2p_go_group_formation_completed = 1;
6846 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6848 * Use a separate timeout for initial data connection to
6849 * complete to allow the group to be removed automatically if
6850 * something goes wrong in this step before the P2P group idle
6851 * timeout mechanism is taken into use.
6853 wpa_dbg(wpa_s, MSG_DEBUG,
6854 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6855 P2P_MAX_INITIAL_CONN_WAIT);
6856 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6857 wpas_p2p_group_formation_timeout,
6858 wpa_s->parent, NULL);
6861 * Use a separate timeout for initial data connection to
6862 * complete to allow the group to be removed automatically if
6863 * the client does not complete data connection successfully.
6865 wpa_dbg(wpa_s, MSG_DEBUG,
6866 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6867 P2P_MAX_INITIAL_CONN_WAIT_GO);
6868 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6869 wpas_p2p_group_formation_timeout,
6870 wpa_s->parent, NULL);
6872 * Complete group formation on first successful data connection
6874 wpa_s->p2p_go_group_formation_completed = 0;
6876 if (wpa_s->global->p2p)
6877 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
6878 wpas_group_formation_completed(wpa_s, 1);
6882 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6883 struct wps_event_fail *fail)
6885 if (!wpa_s->p2p_in_provisioning) {
6886 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6887 "provisioning not in progress");
6891 if (wpa_s->go_params) {
6892 p2p_clear_provisioning_info(
6894 wpa_s->go_params->peer_device_addr);
6897 wpas_notify_p2p_wps_failed(wpa_s, fail);
6899 if (wpa_s == wpa_s->global->p2p_group_formation) {
6901 * Allow some time for the failed WPS negotiation exchange to
6902 * complete, but remove the group since group formation cannot
6903 * succeed after provisioning failure.
6905 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6906 wpa_s->global->p2p_fail_on_wps_complete = 1;
6907 eloop_deplete_timeout(0, 50000,
6908 wpas_p2p_group_formation_timeout,
6909 wpa_s->parent, NULL);
6914 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6916 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6917 !wpa_s->p2p_in_provisioning)
6920 wpas_p2p_grpform_fail_after_wps(wpa_s);
6926 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6927 const char *config_method,
6928 enum wpas_p2p_prov_disc_use use,
6929 struct p2ps_provision *p2ps_prov)
6933 wpa_s->global->pending_p2ps_group = 0;
6934 wpa_s->p2p_fallback_to_go_neg = 0;
6935 wpa_s->pending_pd_use = NORMAL_PD;
6936 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6937 p2ps_prov->conncap = p2ps_group_capability(
6938 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
6939 wpa_printf(MSG_DEBUG,
6940 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6941 __func__, p2ps_prov->conncap,
6942 p2ps_prov->adv_id, p2ps_prov->conncap,
6943 p2ps_prov->status, p2ps_prov->info);
6946 } else if (os_strncmp(config_method, "display", 7) == 0)
6947 config_methods = WPS_CONFIG_DISPLAY;
6948 else if (os_strncmp(config_method, "keypad", 6) == 0)
6949 config_methods = WPS_CONFIG_KEYPAD;
6950 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6951 os_strncmp(config_method, "pushbutton", 10) == 0)
6952 config_methods = WPS_CONFIG_PUSHBUTTON;
6954 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
6959 if (use == WPAS_P2P_PD_AUTO) {
6960 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6961 wpa_s->pending_pd_config_methods = config_methods;
6962 wpa_s->p2p_auto_pd = 1;
6963 wpa_s->p2p_auto_join = 0;
6964 wpa_s->pending_pd_before_join = 0;
6965 wpa_s->auto_pd_scan_retry = 0;
6966 wpas_p2p_stop_find(wpa_s);
6967 wpa_s->p2p_join_scan_count = 0;
6968 os_get_reltime(&wpa_s->p2p_auto_started);
6969 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6970 wpa_s->p2p_auto_started.sec,
6971 wpa_s->p2p_auto_started.usec);
6972 wpas_p2p_join_scan(wpa_s, NULL);
6976 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6981 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6982 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6987 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6990 return p2p_scan_result_text(ies, ies_len, buf, end);
6994 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6996 if (!offchannel_pending_action_tx(wpa_s))
6999 wpas_p2p_action_tx_clear(wpa_s);
7001 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7002 "operation request");
7003 offchannel_clear_pending_action_tx(wpa_s);
7007 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7008 enum p2p_discovery_type type,
7009 unsigned int num_req_dev_types, const u8 *req_dev_types,
7010 const u8 *dev_id, unsigned int search_delay,
7011 u8 seek_cnt, const char **seek_string, int freq)
7013 wpas_p2p_clear_pending_action_tx(wpa_s);
7014 wpa_s->p2p_long_listen = 0;
7016 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7017 wpa_s->p2p_in_provisioning)
7020 wpa_supplicant_cancel_sched_scan(wpa_s);
7022 return p2p_find(wpa_s->global->p2p, timeout, type,
7023 num_req_dev_types, req_dev_types, dev_id,
7024 search_delay, seek_cnt, seek_string, freq);
7028 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7029 struct wpa_scan_results *scan_res)
7031 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7033 if (wpa_s->p2p_scan_work) {
7034 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7035 wpa_s->p2p_scan_work = NULL;
7036 radio_work_done(work);
7039 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7043 * Indicate that results have been processed so that the P2P module can
7044 * continue pending tasks.
7046 p2p_scan_res_handled(wpa_s->global->p2p);
7050 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
7052 wpas_p2p_clear_pending_action_tx(wpa_s);
7053 wpa_s->p2p_long_listen = 0;
7054 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7055 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
7057 if (wpa_s->global->p2p)
7058 p2p_stop_find(wpa_s->global->p2p);
7060 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7061 wpa_printf(MSG_DEBUG,
7062 "P2P: Do not consider the scan results after stop_find");
7063 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7068 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7070 wpas_p2p_stop_find_oper(wpa_s);
7071 if (!wpa_s->global->pending_group_iface_for_p2ps)
7072 wpas_p2p_remove_pending_group_interface(wpa_s);
7076 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7078 struct wpa_supplicant *wpa_s = eloop_ctx;
7079 wpa_s->p2p_long_listen = 0;
7083 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7087 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7090 wpa_supplicant_cancel_sched_scan(wpa_s);
7091 wpas_p2p_clear_pending_action_tx(wpa_s);
7095 * This is a request for unlimited Listen state. However, at
7096 * least for now, this is mapped to a Listen state for one
7101 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7102 wpa_s->p2p_long_listen = 0;
7105 * Stop previous find/listen operation to avoid trying to request a new
7106 * remain-on-channel operation while the driver is still running the
7109 if (wpa_s->global->p2p)
7110 p2p_stop_find(wpa_s->global->p2p);
7112 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7113 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
7114 wpa_s->p2p_long_listen = timeout * 1000;
7115 eloop_register_timeout(timeout, 0,
7116 wpas_p2p_long_listen_timeout,
7124 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7125 u8 *buf, size_t len, int p2p_group)
7127 struct wpabuf *p2p_ie;
7130 if (wpa_s->global->p2p_disabled)
7132 if (wpa_s->conf->p2p_disabled)
7134 if (wpa_s->global->p2p == NULL)
7139 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7140 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7142 wpabuf_free(p2p_ie);
7148 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
7149 const u8 *dst, const u8 *bssid,
7150 const u8 *ie, size_t ie_len, int ssi_signal)
7152 if (wpa_s->global->p2p_disabled)
7154 if (wpa_s->global->p2p == NULL)
7157 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
7159 case P2P_PREQ_NOT_P2P:
7160 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7163 case P2P_PREQ_MALFORMED:
7164 case P2P_PREQ_NOT_LISTEN:
7165 case P2P_PREQ_NOT_PROCESSED:
7166 default: /* make gcc happy */
7168 case P2P_PREQ_PROCESSED:
7174 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7175 const u8 *sa, const u8 *bssid,
7176 u8 category, const u8 *data, size_t len, int freq)
7178 if (wpa_s->global->p2p_disabled)
7180 if (wpa_s->global->p2p == NULL)
7183 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7188 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7190 if (wpa_s->global->p2p_disabled)
7192 if (wpa_s->global->p2p == NULL)
7195 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
7199 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
7201 p2p_group_deinit(wpa_s->p2p_group);
7202 wpa_s->p2p_group = NULL;
7204 wpa_s->ap_configured_cb = NULL;
7205 wpa_s->ap_configured_cb_ctx = NULL;
7206 wpa_s->ap_configured_cb_data = NULL;
7207 wpa_s->connect_without_scan = NULL;
7211 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7213 wpa_s->p2p_long_listen = 0;
7215 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7218 return p2p_reject(wpa_s->global->p2p, addr);
7222 /* Invite to reinvoke a persistent group */
7223 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7224 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
7225 int ht40, int vht, int pref_freq)
7227 enum p2p_invite_role role;
7231 int no_pref_freq_given = pref_freq == 0;
7233 wpa_s->global->p2p_invite_group = NULL;
7235 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7237 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7239 wpa_s->p2p_persistent_go_freq = freq;
7240 wpa_s->p2p_go_ht40 = !!ht40;
7241 if (ssid->mode == WPAS_MODE_P2P_GO) {
7242 role = P2P_INVITE_ROLE_GO;
7243 if (peer_addr == NULL) {
7244 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7245 "address in invitation command");
7248 if (wpas_p2p_create_iface(wpa_s)) {
7249 if (wpas_p2p_add_group_interface(wpa_s,
7250 WPA_IF_P2P_GO) < 0) {
7251 wpa_printf(MSG_ERROR, "P2P: Failed to "
7252 "allocate a new interface for the "
7256 bssid = wpa_s->pending_interface_addr;
7258 bssid = wpa_s->own_addr;
7260 role = P2P_INVITE_ROLE_CLIENT;
7261 peer_addr = ssid->bssid;
7263 wpa_s->pending_invite_ssid_id = ssid->id;
7265 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7266 role == P2P_INVITE_ROLE_GO);
7270 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7273 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7274 no_pref_freq_given && pref_freq > 0 &&
7275 wpa_s->num_multichan_concurrent > 1 &&
7276 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7277 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7283 * Stop any find/listen operations before invitation and possibly
7284 * connection establishment.
7286 wpas_p2p_stop_find_oper(wpa_s);
7288 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
7289 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
7294 /* Invite to join an active group */
7295 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7296 const u8 *peer_addr, const u8 *go_dev_addr)
7298 struct wpa_global *global = wpa_s->global;
7299 enum p2p_invite_role role;
7301 struct wpa_ssid *ssid;
7303 int freq = 0, force_freq = 0, pref_freq = 0;
7306 wpa_s->p2p_persistent_go_freq = 0;
7307 wpa_s->p2p_go_ht40 = 0;
7308 wpa_s->p2p_go_vht = 0;
7310 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7311 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7314 if (wpa_s == NULL) {
7315 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7319 ssid = wpa_s->current_ssid;
7321 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7326 wpa_s->global->p2p_invite_group = wpa_s;
7327 persistent = ssid->p2p_persistent_group &&
7328 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
7329 ssid->ssid, ssid->ssid_len);
7331 if (ssid->mode == WPAS_MODE_P2P_GO) {
7332 role = P2P_INVITE_ROLE_ACTIVE_GO;
7333 bssid = wpa_s->own_addr;
7334 if (go_dev_addr == NULL)
7335 go_dev_addr = wpa_s->global->p2p_dev_addr;
7336 freq = ssid->frequency;
7338 role = P2P_INVITE_ROLE_CLIENT;
7339 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7340 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7341 "invite to current group");
7344 bssid = wpa_s->bssid;
7345 if (go_dev_addr == NULL &&
7346 !is_zero_ether_addr(wpa_s->go_dev_addr))
7347 go_dev_addr = wpa_s->go_dev_addr;
7348 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7349 (int) wpa_s->assoc_freq;
7351 wpa_s->parent->pending_invite_ssid_id = -1;
7353 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7356 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7357 role == P2P_INVITE_ROLE_ACTIVE_GO);
7360 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
7362 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
7363 ssid->ssid, ssid->ssid_len, force_freq,
7364 go_dev_addr, persistent, pref_freq, -1);
7368 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7370 struct wpa_ssid *ssid = wpa_s->current_ssid;
7371 u8 go_dev_addr[ETH_ALEN];
7372 int network_id = -1;
7378 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7379 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7380 wpa_s->parent, NULL);
7383 if (!wpa_s->show_group_started || !ssid)
7386 wpa_s->show_group_started = 0;
7388 os_memset(go_dev_addr, 0, ETH_ALEN);
7389 if (ssid->bssid_set)
7390 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7391 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7393 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7395 if (wpa_s->global->p2p_group_formation == wpa_s)
7396 wpa_s->global->p2p_group_formation = NULL;
7398 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7399 (int) wpa_s->assoc_freq;
7402 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
7405 res = os_snprintf(ip_addr, sizeof(ip_addr),
7406 " ip_addr=%u.%u.%u.%u "
7407 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7408 ip[0], ip[1], ip[2], ip[3],
7409 ip[4], ip[5], ip[6], ip[7],
7410 ip[8], ip[9], ip[10], ip[11]);
7411 if (os_snprintf_error(sizeof(ip_addr), res))
7415 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7416 ssid->passphrase == NULL && ssid->psk_set ?
7418 ssid->passphrase, go_dev_addr, persistent,
7422 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
7425 network_id = ssid->id;
7426 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
7430 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7431 u32 interval1, u32 duration2, u32 interval2)
7435 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7438 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7439 wpa_s->current_ssid == NULL ||
7440 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7443 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7444 wpa_s->own_addr, wpa_s->assoc_freq,
7445 duration1, interval1, duration2, interval2);
7447 wpa_s->waiting_presence_resp = 1;
7453 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7454 unsigned int interval)
7456 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7459 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7463 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7465 if (wpa_s->current_ssid == NULL) {
7467 * current_ssid can be cleared when P2P client interface gets
7468 * disconnected, so assume this interface was used as P2P
7473 return wpa_s->current_ssid->p2p_group &&
7474 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7478 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7480 struct wpa_supplicant *wpa_s = eloop_ctx;
7482 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
7483 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7488 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7490 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
7494 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7498 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7499 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7501 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7504 timeout = wpa_s->conf->p2p_group_idle;
7505 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7506 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7507 timeout = P2P_MAX_CLIENT_IDLE;
7513 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7514 timeout = 0; /* special client mode no-timeout */
7519 if (wpa_s->p2p_in_provisioning) {
7521 * Use the normal group formation timeout during the
7522 * provisioning phase to avoid terminating this process too
7523 * early due to group idle timeout.
7525 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7526 "during provisioning");
7530 if (wpa_s->show_group_started) {
7532 * Use the normal group formation timeout between the end of
7533 * the provisioning phase and completion of 4-way handshake to
7534 * avoid terminating this process too early due to group idle
7537 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7538 "while waiting for initial 4-way handshake to "
7543 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
7545 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7550 /* Returns 1 if the interface was removed */
7551 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7552 u16 reason_code, const u8 *ie, size_t ie_len,
7553 int locally_generated)
7555 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7558 if (!locally_generated)
7559 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7562 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7563 wpa_s->current_ssid &&
7564 wpa_s->current_ssid->p2p_group &&
7565 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7566 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7567 "session is ending");
7568 if (wpas_p2p_group_delete(wpa_s,
7569 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7578 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7579 u16 reason_code, const u8 *ie, size_t ie_len,
7580 int locally_generated)
7582 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7585 if (!locally_generated)
7586 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7591 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7593 struct p2p_data *p2p = wpa_s->global->p2p;
7598 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7601 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7602 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7604 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7605 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7608 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7609 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7611 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7612 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7614 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7615 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7616 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7617 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7618 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7621 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7622 p2p_set_sec_dev_types(p2p,
7623 (void *) wpa_s->conf->sec_device_type,
7624 wpa_s->conf->num_sec_device_types);
7626 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7628 p2p_remove_wps_vendor_extensions(p2p);
7629 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7630 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7632 p2p_add_wps_vendor_extension(
7633 p2p, wpa_s->conf->wps_vendor_ext[i]);
7637 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7638 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7640 country[0] = wpa_s->conf->country[0];
7641 country[1] = wpa_s->conf->country[1];
7643 p2p_set_country(p2p, country);
7646 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7647 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7648 wpa_s->conf->p2p_ssid_postfix ?
7649 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7653 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7654 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
7656 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7657 u8 reg_class, channel;
7662 if (wpa_s->conf->p2p_listen_reg_class &&
7663 wpa_s->conf->p2p_listen_channel) {
7664 reg_class = wpa_s->conf->p2p_listen_reg_class;
7665 channel = wpa_s->conf->p2p_listen_channel;
7670 * Pick one of the social channels randomly as the
7673 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7676 channel = 1 + (r % 3) * 5;
7679 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7682 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7685 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7686 u8 op_reg_class, op_channel, cfg_op_channel;
7689 if (wpa_s->conf->p2p_oper_reg_class &&
7690 wpa_s->conf->p2p_oper_channel) {
7691 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7692 op_channel = wpa_s->conf->p2p_oper_channel;
7697 * Use random operation channel from (1, 6, 11)
7698 *if no other preference is indicated.
7700 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7703 op_channel = 1 + (r % 3) * 5;
7706 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7709 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7713 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7714 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7715 wpa_s->conf->p2p_pref_chan) < 0) {
7716 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7720 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7721 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7726 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7727 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
7731 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7734 if (!wpa_s->ap_iface)
7736 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7741 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7743 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7746 wpa_s->global->cross_connection = enabled;
7747 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7750 struct wpa_supplicant *iface;
7752 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7754 if (iface->cross_connect_enabled == 0)
7757 iface->cross_connect_enabled = 0;
7758 iface->cross_connect_in_use = 0;
7759 wpa_msg_global(iface->parent, MSG_INFO,
7760 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7762 iface->cross_connect_uplink);
7770 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7772 struct wpa_supplicant *iface;
7774 if (!uplink->global->cross_connection)
7777 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7778 if (!iface->cross_connect_enabled)
7780 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7783 if (iface->ap_iface == NULL)
7785 if (iface->cross_connect_in_use)
7788 iface->cross_connect_in_use = 1;
7789 wpa_msg_global(iface->parent, MSG_INFO,
7790 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7791 iface->ifname, iface->cross_connect_uplink);
7796 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7798 struct wpa_supplicant *iface;
7800 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7801 if (!iface->cross_connect_enabled)
7803 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7806 if (!iface->cross_connect_in_use)
7809 wpa_msg_global(iface->parent, MSG_INFO,
7810 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7811 iface->ifname, iface->cross_connect_uplink);
7812 iface->cross_connect_in_use = 0;
7817 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7819 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7820 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7821 wpa_s->cross_connect_disallowed)
7822 wpas_p2p_disable_cross_connect(wpa_s);
7824 wpas_p2p_enable_cross_connect(wpa_s);
7825 if (!wpa_s->ap_iface &&
7826 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7827 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7831 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7833 wpas_p2p_disable_cross_connect(wpa_s);
7834 if (!wpa_s->ap_iface &&
7835 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7837 wpas_p2p_set_group_idle_timeout(wpa_s);
7841 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7843 struct wpa_supplicant *iface;
7845 if (!wpa_s->global->cross_connection)
7848 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7851 if (iface->drv_flags &
7852 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7854 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7855 iface != wpa_s->parent)
7858 wpa_s->cross_connect_enabled = 1;
7859 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7860 sizeof(wpa_s->cross_connect_uplink));
7861 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7862 "%s to %s whenever uplink is available",
7863 wpa_s->ifname, wpa_s->cross_connect_uplink);
7865 if (iface->ap_iface || iface->current_ssid == NULL ||
7866 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7867 iface->cross_connect_disallowed ||
7868 iface->wpa_state != WPA_COMPLETED)
7871 wpa_s->cross_connect_in_use = 1;
7872 wpa_msg_global(wpa_s->parent, MSG_INFO,
7873 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7874 wpa_s->ifname, wpa_s->cross_connect_uplink);
7880 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7882 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7883 !wpa_s->p2p_in_provisioning)
7884 return 0; /* not P2P client operation */
7886 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7888 if (wpa_s != wpa_s->parent)
7889 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
7890 wpas_p2p_group_formation_failed(wpa_s);
7895 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7897 struct wpa_supplicant *wpa_s = eloop_ctx;
7898 wpas_p2p_notif_pbc_overlap(wpa_s);
7902 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
7904 struct p2p_channels chan, cli_chan;
7905 struct wpa_supplicant *ifs;
7907 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7910 os_memset(&chan, 0, sizeof(chan));
7911 os_memset(&cli_chan, 0, sizeof(cli_chan));
7912 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
7913 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7918 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
7920 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
7922 if (!ifs->current_ssid ||
7923 !ifs->current_ssid->p2p_group ||
7924 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
7925 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
7927 freq = ifs->current_ssid->frequency;
7928 if (freq_included(&chan, freq)) {
7929 wpa_dbg(ifs, MSG_DEBUG,
7930 "P2P GO operating frequency %d MHz in valid range",
7935 wpa_dbg(ifs, MSG_DEBUG,
7936 "P2P GO operating in invalid frequency %d MHz", freq);
7937 /* TODO: Consider using CSA or removing the group within
7939 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
7944 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7945 struct wpa_scan_results *scan_res)
7947 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7951 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7953 struct wpa_global *global = wpa_s->global;
7957 if (global->p2p == NULL)
7960 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7962 if (wpa_s->pending_interface_name[0] &&
7963 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7966 peer = p2p_get_go_neg_peer(global->p2p);
7968 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7969 MACSTR, MAC2STR(peer));
7970 p2p_unauthorize(global->p2p, peer);
7974 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7975 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7976 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7980 if (wpa_s->pending_pd_before_join) {
7981 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7982 wpa_s->pending_pd_before_join = 0;
7986 wpas_p2p_stop_find(wpa_s);
7988 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7989 if (wpa_s == global->p2p_group_formation &&
7990 (wpa_s->p2p_in_provisioning ||
7991 wpa_s->parent->pending_interface_type ==
7992 WPA_IF_P2P_CLIENT)) {
7993 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7994 "formation found - cancelling",
7997 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7998 wpa_s->parent, NULL);
7999 if (wpa_s->p2p_in_provisioning) {
8000 wpas_group_formation_completed(wpa_s, 0);
8003 wpas_p2p_group_delete(wpa_s,
8004 P2P_GROUP_REMOVAL_REQUESTED);
8006 } else if (wpa_s->p2p_in_invitation) {
8007 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8010 wpas_p2p_group_formation_failed(wpa_s);
8015 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8023 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8025 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8028 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8029 "being available anymore");
8030 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
8034 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8035 int freq_24, int freq_5, int freq_overall)
8037 struct p2p_data *p2p = wpa_s->global->p2p;
8040 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8044 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8047 struct p2p_data *p2p = wpa_s->global->p2p;
8052 if (hwaddr_aton(addr, peer))
8055 return p2p_unauthorize(p2p, peer);
8060 * wpas_p2p_disconnect - Disconnect from a P2P Group
8061 * @wpa_s: Pointer to wpa_supplicant data
8062 * Returns: 0 on success, -1 on failure
8064 * This can be used to disconnect from a group in which the local end is a P2P
8065 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8066 * virtual network interface was created for this group, that interface will be
8067 * removed. Otherwise, only the configured P2P group network will be removed
8068 * from the interface.
8070 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8076 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8081 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8085 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8088 ret = p2p_in_progress(wpa_s->global->p2p);
8091 * Check whether there is an ongoing WPS provisioning step (or
8092 * other parts of group formation) on another interface since
8093 * p2p_in_progress() does not report this to avoid issues for
8094 * scans during such provisioning step.
8096 if (wpa_s->global->p2p_group_formation &&
8097 wpa_s->global->p2p_group_formation != wpa_s) {
8098 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8099 "in group formation",
8100 wpa_s->global->p2p_group_formation->ifname);
8105 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
8106 struct os_reltime now;
8107 os_get_reltime(&now);
8108 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8109 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
8110 /* Wait for the first client has expired */
8111 wpa_s->global->p2p_go_wait_client.sec = 0;
8113 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8122 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8123 struct wpa_ssid *ssid)
8125 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8126 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8127 wpa_s->parent, NULL) > 0) {
8129 * Remove the network by scheduling the group formation
8130 * timeout to happen immediately. The teardown code
8131 * needs to be scheduled to run asynch later so that we
8132 * don't delete data from under ourselves unexpectedly.
8133 * Calling wpas_p2p_group_formation_timeout directly
8134 * causes a series of crashes in WPS failure scenarios.
8136 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8137 "P2P group network getting removed");
8138 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
8139 wpa_s->parent, NULL);
8144 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
8145 const u8 *addr, const u8 *ssid,
8151 for (s = wpa_s->conf->ssid; s; s = s->next) {
8152 if (s->disabled != 2)
8155 (ssid_len != s->ssid_len ||
8156 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8159 if (s->mode == WPAS_MODE_P2P_GO)
8163 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8164 return s; /* peer is GO in the persistent group */
8165 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8167 for (i = 0; i < s->num_p2p_clients; i++) {
8168 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
8169 addr, ETH_ALEN) == 0)
8170 return s; /* peer is P2P client in persistent
8179 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8182 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8183 wpa_s->parent, NULL) > 0) {
8185 * This can happen if WPS provisioning step is not terminated
8186 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8187 * peer was able to connect, there is no need to time out group
8188 * formation after this, though. In addition, this is used with
8189 * the initial connection wait on the GO as a separate formation
8190 * timeout and as such, expected to be hit after the initial WPS
8191 * provisioning step.
8193 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
8195 if (!wpa_s->p2p_go_group_formation_completed &&
8196 !wpa_s->group_formation_reported) {
8198 * GO has not yet notified group formation success since
8199 * the WPS step was not completed cleanly. Do that
8200 * notification now since the P2P Client was able to
8201 * connect and as such, must have received the
8202 * credential from the WPS step.
8204 if (wpa_s->global->p2p)
8205 p2p_wps_success_cb(wpa_s->global->p2p, addr);
8206 wpas_group_formation_completed(wpa_s, 1);
8209 if (!wpa_s->p2p_go_group_formation_completed) {
8210 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8211 wpa_s->p2p_go_group_formation_completed = 1;
8212 wpa_s->global->p2p_group_formation = NULL;
8213 wpa_s->p2p_in_provisioning = 0;
8214 wpa_s->p2p_in_invitation = 0;
8216 wpa_s->global->p2p_go_wait_client.sec = 0;
8219 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8223 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8226 struct wpa_supplicant *group = wpa_s;
8229 if (wpa_s->global->p2p_group_formation)
8230 group = wpa_s->global->p2p_group_formation;
8231 wpa_s = wpa_s->parent;
8232 offchannel_send_action_done(wpa_s);
8234 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
8235 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8236 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8237 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8238 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
8239 wpa_s->p2p_persistent_id,
8240 wpa_s->p2p_pd_before_go_neg,
8247 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8251 if (!wpa_s->p2p_fallback_to_go_neg ||
8252 wpa_s->p2p_in_provisioning <= 5)
8255 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8256 return 0; /* peer operating as a GO */
8258 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8259 "fallback to GO Negotiation");
8260 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
8261 "reason=GO-not-found");
8262 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
8264 return res == 1 ? 2 : 1;
8268 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8270 struct wpa_supplicant *ifs;
8272 if (wpa_s->wpa_state > WPA_SCANNING) {
8273 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8274 "concurrent operation",
8275 wpa_s->conf->p2p_search_delay);
8276 return wpa_s->conf->p2p_search_delay;
8279 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8281 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
8282 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8283 "delay due to concurrent operation on "
8285 wpa_s->conf->p2p_search_delay,
8287 return wpa_s->conf->p2p_search_delay;
8295 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8296 struct wpa_ssid *s, const u8 *addr,
8299 struct psk_list_entry *psk, *tmp;
8302 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8304 if ((iface_addr && !psk->p2p &&
8305 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8306 (!iface_addr && psk->p2p &&
8307 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8308 wpa_dbg(wpa_s, MSG_DEBUG,
8309 "P2P: Remove persistent group PSK list entry for "
8311 MAC2STR(psk->addr), psk->p2p);
8312 dl_list_del(&psk->list);
8322 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8323 const u8 *p2p_dev_addr,
8324 const u8 *psk, size_t psk_len)
8326 struct wpa_ssid *ssid = wpa_s->current_ssid;
8327 struct wpa_ssid *persistent;
8328 struct psk_list_entry *p, *last;
8330 if (psk_len != sizeof(p->psk))
8334 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8335 " p2p_dev_addr=" MACSTR,
8336 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8337 if (is_zero_ether_addr(p2p_dev_addr))
8338 p2p_dev_addr = NULL;
8340 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8344 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8345 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8346 /* To be added to persistent group once created */
8347 if (wpa_s->global->add_psk == NULL) {
8348 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8349 if (wpa_s->global->add_psk == NULL)
8352 p = wpa_s->global->add_psk;
8355 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8358 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8360 os_memcpy(p->psk, psk, psk_len);
8364 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8365 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8369 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
8372 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8376 p = os_zalloc(sizeof(*p));
8381 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8384 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8386 os_memcpy(p->psk, psk, psk_len);
8388 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8389 (last = dl_list_last(&persistent->psk_list,
8390 struct psk_list_entry, list))) {
8391 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8392 MACSTR " (p2p=%u) to make room for a new one",
8393 MAC2STR(last->addr), last->p2p);
8394 dl_list_del(&last->list);
8398 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
8399 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8400 p2p_dev_addr == NULL);
8402 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8403 MACSTR, MAC2STR(p2p_dev_addr));
8405 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8408 dl_list_add(&persistent->psk_list, &p->list);
8410 if (wpa_s->parent->conf->update_config &&
8411 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
8412 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
8416 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8417 struct wpa_ssid *s, const u8 *addr,
8422 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
8423 if (res > 0 && wpa_s->conf->update_config &&
8424 wpa_config_write(wpa_s->confname, wpa_s->conf))
8425 wpa_dbg(wpa_s, MSG_DEBUG,
8426 "P2P: Failed to update configuration");
8430 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8431 const u8 *peer, int iface_addr)
8433 struct hostapd_data *hapd;
8434 struct hostapd_wpa_psk *psk, *prev, *rem;
8435 struct sta_info *sta;
8437 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8438 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8441 /* Remove per-station PSK entry */
8442 hapd = wpa_s->ap_iface->bss[0];
8444 psk = hapd->conf->ssid.wpa_psk;
8446 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8448 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8449 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8450 MACSTR " iface_addr=%d",
8451 MAC2STR(peer), iface_addr);
8453 prev->next = psk->next;
8455 hapd->conf->ssid.wpa_psk = psk->next;
8465 /* Disconnect from group */
8467 sta = ap_get_sta(hapd, peer);
8469 sta = ap_get_sta_p2p(hapd, peer);
8471 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8472 " (iface_addr=%d) from group",
8473 MAC2STR(peer), iface_addr);
8474 hostapd_drv_sta_deauth(hapd, sta->addr,
8475 WLAN_REASON_DEAUTH_LEAVING);
8476 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8481 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8485 struct wpa_supplicant *w;
8487 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8489 /* Remove from any persistent group */
8490 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
8491 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8494 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
8495 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
8498 /* Remove from any operating group */
8499 for (w = wpa_s->global->ifaces; w; w = w->next)
8500 wpas_p2p_remove_client_go(w, peer, iface_addr);
8504 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8506 struct wpa_supplicant *wpa_s = eloop_ctx;
8507 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8511 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8513 struct wpa_supplicant *wpa_s = eloop_ctx;
8515 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8516 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8520 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8521 struct wpa_ssid *ssid)
8523 struct wpa_supplicant *iface;
8525 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8526 if (!iface->current_ssid ||
8527 iface->current_ssid->frequency == freq ||
8528 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8529 !iface->current_ssid->p2p_group))
8532 /* Remove the connection with least priority */
8533 if (!wpas_is_p2p_prioritized(iface)) {
8534 /* STA connection has priority over existing
8535 * P2P connection, so remove the interface. */
8536 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8537 eloop_register_timeout(0, 0,
8538 wpas_p2p_group_freq_conflict,
8540 /* If connection in progress is P2P connection, do not
8541 * proceed for the connection. */
8547 /* P2P connection has priority, disable the STA network
8549 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8551 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8552 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8553 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8555 /* If P2P connection is in progress, continue
8568 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8570 struct wpa_ssid *ssid = wpa_s->current_ssid;
8572 if (ssid == NULL || !ssid->p2p_group)
8575 if (wpa_s->p2p_last_4way_hs_fail &&
8576 wpa_s->p2p_last_4way_hs_fail == ssid) {
8577 u8 go_dev_addr[ETH_ALEN];
8578 struct wpa_ssid *persistent;
8580 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8582 ssid->ssid_len) <= 0) {
8583 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8587 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8588 MACSTR, MAC2STR(go_dev_addr));
8589 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
8592 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8593 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8596 wpa_msg_global(wpa_s->parent, MSG_INFO,
8597 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8600 wpa_s->p2p_last_4way_hs_fail = NULL;
8602 * Remove the group from a timeout to avoid issues with caller
8603 * continuing to use the interface if this is on a P2P group
8606 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8611 wpa_s->p2p_last_4way_hs_fail = ssid;
8616 #ifdef CONFIG_WPS_NFC
8618 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8626 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8630 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8631 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8638 wpabuf_put_be16(ret, wsc_len);
8640 wpabuf_put_buf(ret, wsc);
8641 wpabuf_put_be16(ret, wpabuf_len(p2p));
8642 wpabuf_put_buf(ret, p2p);
8646 wpa_hexdump_buf(MSG_DEBUG,
8647 "P2P: Generated NFC connection handover message", ret);
8651 tmp = ndef_build_p2p(ret);
8654 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8664 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8665 struct wpa_ssid **ssid, u8 *go_dev_addr)
8667 struct wpa_supplicant *iface;
8670 os_memset(go_dev_addr, 0, ETH_ALEN);
8673 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8674 if (iface->wpa_state < WPA_ASSOCIATING ||
8675 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8676 !iface->current_ssid->p2p_group ||
8677 iface->current_ssid->mode != WPAS_MODE_INFRA)
8680 *ssid = iface->current_ssid;
8682 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8683 return iface->assoc_freq;
8689 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8692 struct wpabuf *wsc, *p2p;
8693 struct wpa_ssid *ssid;
8694 u8 go_dev_addr[ETH_ALEN];
8695 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8697 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8698 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8702 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8703 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8704 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8705 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8709 if (cli_freq == 0) {
8710 wsc = wps_build_nfc_handover_req_p2p(
8711 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8714 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8715 go_dev_addr, ssid ? ssid->ssid : NULL,
8716 ssid ? ssid->ssid_len : 0);
8718 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8722 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8725 struct wpabuf *wsc, *p2p;
8726 struct wpa_ssid *ssid;
8727 u8 go_dev_addr[ETH_ALEN];
8728 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8730 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8733 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8734 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8735 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8738 if (cli_freq == 0) {
8739 wsc = wps_build_nfc_handover_sel_p2p(
8741 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8742 DEV_PW_NFC_CONNECTION_HANDOVER,
8743 wpa_s->conf->wps_nfc_dh_pubkey,
8744 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8747 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8748 go_dev_addr, ssid ? ssid->ssid : NULL,
8749 ssid ? ssid->ssid_len : 0);
8751 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8755 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8756 struct p2p_nfc_params *params)
8758 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8759 "connection handover (freq=%d)",
8762 if (params->go_freq && params->go_ssid_len) {
8763 wpa_s->p2p_wps_method = WPS_NFC;
8764 wpa_s->pending_join_wps_method = WPS_NFC;
8765 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8766 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8768 return wpas_p2p_join_start(wpa_s, params->go_freq,
8770 params->go_ssid_len);
8773 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8774 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
8775 params->go_freq, -1, 0, 1, 1);
8779 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8780 struct p2p_nfc_params *params, int tag)
8782 int res, persistent;
8783 struct wpa_ssid *ssid;
8785 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8786 "connection handover");
8787 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8788 ssid = wpa_s->current_ssid;
8791 if (ssid->mode != WPAS_MODE_P2P_GO)
8793 if (wpa_s->ap_iface == NULL)
8797 if (wpa_s == NULL) {
8798 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8802 if (wpa_s->parent->p2p_oob_dev_pw_id !=
8803 DEV_PW_NFC_CONNECTION_HANDOVER &&
8804 !wpa_s->parent->p2p_oob_dev_pw) {
8805 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8808 res = wpas_ap_wps_add_nfc_pw(
8809 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
8810 wpa_s->parent->p2p_oob_dev_pw,
8811 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
8812 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
8817 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8821 if (!params->peer ||
8822 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8825 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8826 " to join", MAC2STR(params->peer->p2p_device_addr));
8828 wpa_s->global->p2p_invite_group = wpa_s;
8829 persistent = ssid->p2p_persistent_group &&
8830 wpas_p2p_get_persistent(wpa_s->parent,
8831 params->peer->p2p_device_addr,
8832 ssid->ssid, ssid->ssid_len);
8833 wpa_s->parent->pending_invite_ssid_id = -1;
8835 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8836 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8837 ssid->ssid, ssid->ssid_len, ssid->frequency,
8838 wpa_s->global->p2p_dev_addr, persistent, 0,
8839 wpa_s->parent->p2p_oob_dev_pw_id);
8843 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8844 struct p2p_nfc_params *params,
8847 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8848 "connection handover");
8849 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8850 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8851 forced_freq, -1, 0, 1, 1);
8855 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8856 struct p2p_nfc_params *params,
8861 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8862 "connection handover");
8863 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8864 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8865 forced_freq, -1, 0, 1, 1);
8869 res = wpas_p2p_listen(wpa_s, 60);
8871 p2p_unauthorize(wpa_s->global->p2p,
8872 params->peer->p2p_device_addr);
8879 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8880 const struct wpabuf *data,
8881 int sel, int tag, int forced_freq)
8883 const u8 *pos, *end;
8885 struct p2p_nfc_params params;
8888 os_memset(¶ms, 0, sizeof(params));
8891 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8893 pos = wpabuf_head(data);
8894 end = pos + wpabuf_len(data);
8896 if (end - pos < 2) {
8897 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8901 len = WPA_GET_BE16(pos);
8903 if (len > end - pos) {
8904 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8908 params.wsc_attr = pos;
8909 params.wsc_len = len;
8912 if (end - pos < 2) {
8913 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8917 len = WPA_GET_BE16(pos);
8919 if (len > end - pos) {
8920 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8924 params.p2p_attr = pos;
8925 params.p2p_len = len;
8928 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8929 params.wsc_attr, params.wsc_len);
8930 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8931 params.p2p_attr, params.p2p_len);
8933 wpa_hexdump(MSG_DEBUG,
8934 "P2P: Ignored extra data after P2P attributes",
8938 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
8942 if (params.next_step == NO_ACTION)
8945 if (params.next_step == BOTH_GO) {
8946 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8947 MAC2STR(params.peer->p2p_device_addr));
8951 if (params.next_step == PEER_CLIENT) {
8952 if (!is_zero_ether_addr(params.go_dev_addr)) {
8953 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8954 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8956 MAC2STR(params.peer->p2p_device_addr),
8958 MAC2STR(params.go_dev_addr),
8959 wpa_ssid_txt(params.go_ssid,
8960 params.go_ssid_len));
8962 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8963 "peer=" MACSTR " freq=%d",
8964 MAC2STR(params.peer->p2p_device_addr),
8970 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8971 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8972 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8976 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8977 wpa_s->p2p_oob_dev_pw = NULL;
8979 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8980 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8985 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8986 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8987 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8988 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8989 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8990 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8991 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8995 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8996 "peer OOB Device Password Id %u", id);
8999 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9000 "Device Password Id %u", id);
9001 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9002 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9003 params.oob_dev_pw_len -
9004 WPS_OOB_PUBKEY_HASH_LEN - 2);
9005 wpa_s->p2p_oob_dev_pw_id = id;
9006 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9007 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9008 params.oob_dev_pw_len -
9009 WPS_OOB_PUBKEY_HASH_LEN - 2);
9010 if (wpa_s->p2p_oob_dev_pw == NULL)
9013 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9014 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9015 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9018 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9019 "without Device Password");
9020 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9023 switch (params.next_step) {
9027 /* already covered above */
9030 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
9032 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
9034 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
9036 /* TODO: use own OOB Dev Pw */
9037 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
9044 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9045 const struct wpabuf *data, int forced_freq)
9047 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9050 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9054 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9055 const struct wpabuf *req,
9056 const struct wpabuf *sel, int forced_freq)
9061 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9064 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9066 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9067 wpabuf_head(req), wpabuf_len(req));
9068 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9069 wpabuf_head(sel), wpabuf_len(sel));
9071 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9072 tmp = ndef_parse_p2p(init ? sel : req);
9074 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9078 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9086 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9089 int go_intent = wpa_s->conf->p2p_go_intent;
9090 struct wpa_supplicant *iface;
9092 if (wpa_s->global->p2p == NULL)
9096 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9097 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9099 if (!iface->ap_iface)
9101 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9103 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9105 if (wpa_s->p2p_nfc_tag_enabled)
9106 wpas_p2p_remove_pending_group_interface(wpa_s);
9107 wpa_s->p2p_nfc_tag_enabled = 0;
9111 if (wpa_s->global->p2p_disabled)
9114 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9115 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9116 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9117 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9118 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9119 "to allow static handover cases");
9123 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9125 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9126 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9127 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9128 if (wpa_s->p2p_oob_dev_pw == NULL)
9130 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9132 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9133 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9135 * P2P Group Interface present and the command came on group
9136 * interface, so enable the token for the current interface.
9138 wpa_s->create_p2p_iface = 0;
9140 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9143 if (wpa_s->create_p2p_iface) {
9144 enum wpa_driver_if_type iftype;
9145 /* Prepare to add a new interface for the group */
9146 iftype = WPA_IF_P2P_GROUP;
9147 if (go_intent == 15)
9148 iftype = WPA_IF_P2P_GO;
9149 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9150 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9151 "interface for the group");
9155 if_addr = wpa_s->pending_interface_addr;
9157 if_addr = wpa_s->own_addr;
9159 wpa_s->p2p_nfc_tag_enabled = enabled;
9161 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9162 struct hostapd_data *hapd;
9163 if (iface->ap_iface == NULL)
9165 hapd = iface->ap_iface->bss[0];
9166 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9167 hapd->conf->wps_nfc_dh_pubkey =
9168 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9169 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9170 hapd->conf->wps_nfc_dh_privkey =
9171 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9172 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9173 hapd->conf->wps_nfc_dev_pw =
9174 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9175 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9177 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9178 wpa_dbg(iface, MSG_DEBUG,
9179 "P2P: Failed to enable NFC Tag for GO");
9182 p2p_set_authorized_oob_dev_pw_id(
9183 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9189 #endif /* CONFIG_WPS_NFC */
9192 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9193 struct wpa_used_freq_data *freqs,
9196 u8 curr_chan, cand, chan;
9199 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9200 for (i = 0, cand = 0; i < num; i++) {
9201 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9202 if (curr_chan == chan) {
9207 if (chan == 1 || chan == 6 || chan == 11)
9212 wpa_dbg(wpa_s, MSG_DEBUG,
9213 "P2P: Update Listen channel to %u based on operating channel",
9215 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9220 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9222 struct wpa_used_freq_data *freqs;
9223 unsigned int num = wpa_s->num_multichan_concurrent;
9225 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9229 * If possible, optimize the Listen channel to be a channel that is
9230 * already used by one of the other interfaces.
9232 if (!wpa_s->conf->p2p_optimize_listen_chan)
9235 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9238 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9242 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9244 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
9249 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9251 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9252 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9253 "the management interface is being removed");
9254 wpas_p2p_deinit_global(wpa_s->global);
9259 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9261 if (wpa_s->ap_iface->bss)
9262 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9263 wpas_p2p_group_deinit(wpa_s);