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_SOCIAL_PLUS_ONE:
381 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
383 if (params->freqs == NULL)
385 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
386 if (wpas_p2p_search_social_channel(
387 wpa_s, social_channels_freq[i]))
388 params->freqs[num_channels++] =
389 social_channels_freq[i];
391 if (p2p_supported_freq(wpa_s->global->p2p, freq))
392 params->freqs[num_channels++] = freq;
393 params->freqs[num_channels++] = 0;
397 radio_remove_works(wpa_s, "p2p-scan", 0);
398 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
404 wpa_scan_free_params(params);
409 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
411 switch (p2p_group_interface) {
412 case P2P_GROUP_INTERFACE_PENDING:
413 return WPA_IF_P2P_GROUP;
414 case P2P_GROUP_INTERFACE_GO:
415 return WPA_IF_P2P_GO;
416 case P2P_GROUP_INTERFACE_CLIENT:
417 return WPA_IF_P2P_CLIENT;
420 return WPA_IF_P2P_GROUP;
424 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
426 size_t ssid_len, int *go)
430 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
431 for (s = wpa_s->conf->ssid; s; s = s->next) {
432 if (s->disabled != 0 || !s->p2p_group ||
433 s->ssid_len != ssid_len ||
434 os_memcmp(ssid, s->ssid, ssid_len) != 0)
436 if (s->mode == WPAS_MODE_P2P_GO &&
437 s != wpa_s->current_ssid)
440 *go = s->mode == WPAS_MODE_P2P_GO;
449 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
451 struct wpa_supplicant *wpa_s = eloop_ctx;
452 wpa_printf(MSG_DEBUG,
453 "P2P: Complete previously requested removal of %s",
455 wpas_p2p_disconnect(wpa_s);
459 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
460 struct wpa_supplicant *calling_wpa_s)
462 if (calling_wpa_s == wpa_s && wpa_s &&
463 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
465 * The calling wpa_s instance is going to be removed. Do that
466 * from an eloop callback to keep the instance available until
467 * the caller has returned. This my be needed, e.g., to provide
468 * control interface responses on the per-interface socket.
470 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
476 return wpas_p2p_disconnect(wpa_s);
480 /* Determine total number of clients in active groups where we are the GO */
481 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
483 unsigned int count = 0;
486 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
487 for (s = wpa_s->conf->ssid; s; s = s->next) {
488 wpa_printf(MSG_DEBUG,
489 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
490 wpa_s, s, s->disabled, s->p2p_group,
492 if (!s->disabled && s->p2p_group &&
493 s->mode == WPAS_MODE_P2P_GO) {
494 count += p2p_get_group_num_members(
504 /* Find an interface for a P2P group where we are the GO */
505 static struct wpa_supplicant *
506 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
508 struct wpa_supplicant *save = NULL;
514 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
515 for (s = wpa_s->conf->ssid; s; s = s->next) {
516 if (s->disabled || !s->p2p_group ||
517 s->mode != WPAS_MODE_P2P_GO)
520 /* Prefer a group with connected clients */
521 if (p2p_get_group_num_members(wpa_s->p2p_group))
527 /* No group with connected clients, so pick the one without (if any) */
532 /* Find an active P2P group where we are the GO */
533 static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
536 struct wpa_ssid *s, *empty = NULL;
541 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
542 for (s = wpa_s->conf->ssid; s; s = s->next) {
543 if (s->disabled || !s->p2p_group ||
544 s->mode != WPAS_MODE_P2P_GO)
547 os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
548 if (p2p_get_group_num_members(wpa_s->p2p_group))
558 /* Find a persistent group where we are the GO */
559 static struct wpa_ssid *
560 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
564 for (s = wpa_s->conf->ssid; s; s = s->next) {
565 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
573 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
575 struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
577 u8 conncap = P2PS_SETUP_NONE;
578 unsigned int owned_members = 0;
579 unsigned int owner = 0;
580 unsigned int client = 0;
581 struct wpa_supplicant *go_wpa_s;
582 struct wpa_ssid *persistent_go;
583 int p2p_no_group_iface;
585 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
588 * For non-concurrent capable devices:
589 * If persistent_go, then no new.
590 * If GO, then no client.
591 * If client, then no GO.
593 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
594 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
595 p2p_no_group_iface = wpa_s->conf->p2p_no_group_iface;
597 wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
598 go_wpa_s, persistent_go);
600 for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
601 tmp_wpa_s = tmp_wpa_s->next) {
602 for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
603 wpa_printf(MSG_DEBUG,
604 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
605 tmp_wpa_s, s, s->disabled,
606 s->p2p_group, s->mode);
607 if (!s->disabled && s->p2p_group) {
608 if (s->mode == WPAS_MODE_P2P_GO) {
610 p2p_get_group_num_members(
611 tmp_wpa_s->p2p_group);
619 /* If not concurrent, restrict our choices */
620 if (p2p_no_group_iface) {
621 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
624 return P2PS_SETUP_NONE;
627 if (role == P2PS_SETUP_CLIENT ||
628 incoming == P2PS_SETUP_GROUP_OWNER ||
629 p2p_client_limit_reached(go_wpa_s->p2p_group))
630 return P2PS_SETUP_NONE;
632 return P2PS_SETUP_GROUP_OWNER;
636 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
638 return P2PS_SETUP_GROUP_OWNER |
640 if (incoming == P2PS_SETUP_NEW) {
643 if (os_get_random(&r, sizeof(r)) < 0 ||
645 return P2PS_SETUP_CLIENT;
646 return P2PS_SETUP_GROUP_OWNER;
652 /* If a required role has been specified, handle it here */
653 if (role && role != P2PS_SETUP_NEW) {
655 case P2PS_SETUP_NONE:
657 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
658 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
662 case P2PS_SETUP_GROUP_OWNER:
664 * Must be a complimentary role - cannot be a client to
665 * more than one peer.
667 if (incoming == role || client)
668 return P2PS_SETUP_NONE;
670 return P2PS_SETUP_CLIENT;
672 case P2PS_SETUP_CLIENT:
673 /* Must be a complimentary role */
674 if (incoming != role) {
675 conncap = P2PS_SETUP_GROUP_OWNER;
680 return P2PS_SETUP_NONE;
685 * For now, we only will support ownership of one group, and being a
686 * client of one group. Therefore, if we have either an existing GO
687 * group, or an existing client group, we will not do a new GO
688 * negotiation, but rather try to re-use the existing groups.
691 case P2PS_SETUP_NONE:
694 conncap = P2PS_SETUP_GROUP_OWNER;
695 else if (!owned_members)
696 conncap = P2PS_SETUP_NEW;
697 else if (incoming == P2PS_SETUP_NONE)
698 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
700 conncap = P2PS_SETUP_CLIENT;
703 case P2PS_SETUP_CLIENT:
704 conncap = P2PS_SETUP_GROUP_OWNER;
707 case P2PS_SETUP_GROUP_OWNER:
709 conncap = P2PS_SETUP_CLIENT;
712 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
713 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
715 conncap = P2PS_SETUP_GROUP_OWNER;
719 if (os_get_random(&r, sizeof(r)) < 0 ||
721 conncap = P2PS_SETUP_CLIENT;
723 conncap = P2PS_SETUP_GROUP_OWNER;
728 return P2PS_SETUP_NONE;
732 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
733 (!incoming && (conncap & P2PS_SETUP_NEW))) {
734 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
735 conncap &= ~P2PS_SETUP_GROUP_OWNER;
736 wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
737 owner, owned_members, conncap);
739 s = wpas_p2p_get_persistent_go(wpa_s);
741 if (!s && !owner && p2p_no_group_iface) {
742 p2p_set_intended_addr(wpa_s->global->p2p,
744 } else if (!s && !owner) {
745 if (wpas_p2p_add_group_interface(wpa_s,
746 WPA_IF_P2P_GO) < 0) {
747 wpa_printf(MSG_ERROR,
748 "P2P: Failed to allocate a new interface for the group");
749 return P2PS_SETUP_NONE;
751 wpa_s->global->pending_group_iface_for_p2ps = 1;
752 p2p_set_intended_addr(wpa_s->global->p2p,
753 wpa_s->pending_interface_addr);
761 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
762 enum p2p_group_removal_reason removal_reason)
764 struct wpa_ssid *ssid;
768 ssid = wpa_s->current_ssid;
771 * The current SSID was not known, but there may still be a
772 * pending P2P group interface waiting for provisioning or a
773 * P2P group that is trying to reconnect.
775 ssid = wpa_s->conf->ssid;
777 if (ssid->p2p_group && ssid->disabled != 2)
782 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
784 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
789 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
791 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
792 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
793 wpa_s->reassociate = 0;
794 wpa_s->disconnected = 1;
799 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
800 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
802 if (os_strcmp(gtype, "client") == 0) {
803 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
804 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
806 wpa_printf(MSG_DEBUG,
807 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
808 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
809 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
814 if (wpa_s->cross_connect_in_use) {
815 wpa_s->cross_connect_in_use = 0;
816 wpa_msg_global(wpa_s->parent, MSG_INFO,
817 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
818 wpa_s->ifname, wpa_s->cross_connect_uplink);
820 switch (removal_reason) {
821 case P2P_GROUP_REMOVAL_REQUESTED:
822 reason = " reason=REQUESTED";
824 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
825 reason = " reason=FORMATION_FAILED";
827 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
828 reason = " reason=IDLE";
830 case P2P_GROUP_REMOVAL_UNAVAILABLE:
831 reason = " reason=UNAVAILABLE";
833 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
834 reason = " reason=GO_ENDING_SESSION";
836 case P2P_GROUP_REMOVAL_PSK_FAILURE:
837 reason = " reason=PSK_FAILURE";
839 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
840 reason = " reason=FREQ_CONFLICT";
846 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
847 wpa_msg_global(wpa_s->parent, MSG_INFO,
848 P2P_EVENT_GROUP_REMOVED "%s %s%s",
849 wpa_s->ifname, gtype, reason);
852 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
853 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
854 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
855 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
856 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
857 wpa_s->parent, NULL) > 0) {
858 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
860 wpa_s->p2p_in_provisioning = 0;
863 wpa_s->p2p_in_invitation = 0;
866 * Make sure wait for the first client does not remain active after the
867 * group has been removed.
869 wpa_s->global->p2p_go_wait_client.sec = 0;
871 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
872 struct wpa_global *global;
874 enum wpa_driver_if_type type;
875 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
877 global = wpa_s->global;
878 ifname = os_strdup(wpa_s->ifname);
879 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
880 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
881 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
882 wpa_s = global->ifaces;
884 wpa_drv_if_remove(wpa_s, type, ifname);
889 if (!wpa_s->p2p_go_group_formation_completed) {
890 wpa_s->global->p2p_group_formation = NULL;
891 wpa_s->p2p_in_provisioning = 0;
894 wpa_s->show_group_started = 0;
895 os_free(wpa_s->go_params);
896 wpa_s->go_params = NULL;
898 os_free(wpa_s->p2p_group_common_freqs);
899 wpa_s->p2p_group_common_freqs = NULL;
900 wpa_s->p2p_group_common_freqs_num = 0;
902 wpa_s->waiting_presence_resp = 0;
904 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
905 if (ssid && (ssid->p2p_group ||
906 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
907 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
909 if (ssid == wpa_s->current_ssid) {
910 wpa_sm_set_config(wpa_s->wpa, NULL);
911 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
912 wpa_s->current_ssid = NULL;
915 * Networks objects created during any P2P activities are not
916 * exposed out as they might/will confuse certain non-P2P aware
917 * applications since these network objects won't behave like
920 * Likewise, we don't send out network removed signals for such
923 wpa_config_remove_network(wpa_s->conf, id);
924 wpa_supplicant_clear_status(wpa_s);
925 wpa_supplicant_cancel_sched_scan(wpa_s);
927 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
931 wpa_supplicant_ap_deinit(wpa_s);
933 wpa_drv_deinit_p2p_cli(wpa_s);
939 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
941 const u8 *ssid, size_t ssid_len)
949 if (wpa_s->go_params)
950 bssid = wpa_s->go_params->peer_interface_addr;
952 bssid = wpa_s->bssid;
954 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
955 if (bss == NULL && wpa_s->go_params &&
956 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
957 bss = wpa_bss_get_p2p_dev_addr(
958 wpa_s, wpa_s->go_params->peer_device_addr);
960 u8 iface_addr[ETH_ALEN];
961 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
963 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
966 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
967 "group is persistent - BSS " MACSTR " not found",
972 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
974 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
977 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
978 "group is persistent - BSS " MACSTR
979 " did not include P2P IE", MAC2STR(bssid));
980 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
981 (u8 *) (bss + 1), bss->ie_len);
982 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
983 ((u8 *) bss + 1) + bss->ie_len,
988 group_capab = p2p_get_group_capab(p2p);
989 addr = p2p_get_go_dev_addr(p2p);
990 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
991 "group_capab=0x%x", group_capab);
993 os_memcpy(go_dev_addr, addr, ETH_ALEN);
994 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
997 os_memset(go_dev_addr, 0, ETH_ALEN);
1000 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1001 "go_dev_addr=" MACSTR,
1002 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1004 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1008 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1009 struct wpa_ssid *ssid,
1010 const u8 *go_dev_addr)
1015 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1016 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1017 for (s = wpa_s->conf->ssid; s; s = s->next) {
1018 if (s->disabled == 2 &&
1019 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1020 s->ssid_len == ssid->ssid_len &&
1021 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1026 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1028 if (ssid->passphrase && !s->passphrase)
1030 else if (ssid->passphrase && s->passphrase &&
1031 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1034 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1037 s = wpa_config_add_network(wpa_s->conf);
1042 * Instead of network_added we emit persistent_group_added
1043 * notification. Also to keep the defense checks in
1044 * persistent_group obj registration method, we set the
1045 * relevant flags in s to designate it as a persistent group.
1048 s->p2p_persistent_group = 1;
1049 wpas_notify_persistent_group_added(wpa_s, s);
1050 wpa_config_set_network_defaults(s);
1054 s->p2p_persistent_group = 1;
1057 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1058 s->mode = ssid->mode;
1059 s->auth_alg = WPA_AUTH_ALG_OPEN;
1060 s->key_mgmt = WPA_KEY_MGMT_PSK;
1061 s->proto = WPA_PROTO_RSN;
1062 s->pairwise_cipher = WPA_CIPHER_CCMP;
1064 if (ssid->passphrase) {
1065 os_free(s->passphrase);
1066 s->passphrase = os_strdup(ssid->passphrase);
1068 if (ssid->psk_set) {
1070 os_memcpy(s->psk, ssid->psk, 32);
1072 if (s->passphrase && !s->psk_set)
1073 wpa_config_update_psk(s);
1074 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1076 s->ssid = os_malloc(ssid->ssid_len);
1079 s->ssid_len = ssid->ssid_len;
1080 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1082 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1083 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1084 wpa_s->global->add_psk = NULL;
1088 if (changed && wpa_s->conf->update_config &&
1089 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1090 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1097 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1100 struct wpa_ssid *ssid, *s;
1105 ssid = wpa_s->current_ssid;
1106 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1107 !ssid->p2p_persistent_group)
1110 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
1111 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1114 if (s->ssid_len == ssid->ssid_len &&
1115 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1122 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
1123 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
1127 if (i == s->num_p2p_clients - 1)
1128 return; /* already the most recent entry */
1130 /* move the entry to mark it most recent */
1131 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1132 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1133 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
1134 os_memcpy(s->p2p_client_list +
1135 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1137 os_memset(s->p2p_client_list +
1138 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1144 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1145 n = os_realloc_array(s->p2p_client_list,
1146 s->num_p2p_clients + 1, 2 * ETH_ALEN);
1149 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1151 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1153 s->p2p_client_list = n;
1154 s->num_p2p_clients++;
1155 } else if (!found && s->p2p_client_list) {
1156 /* Not enough room for an additional entry - drop the oldest
1158 os_memmove(s->p2p_client_list,
1159 s->p2p_client_list + 2 * ETH_ALEN,
1160 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1161 os_memcpy(s->p2p_client_list +
1162 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1164 os_memset(s->p2p_client_list +
1165 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1169 if (wpa_s->parent->conf->update_config &&
1170 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1171 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1175 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1176 int go, struct wpa_ssid *ssid, int freq,
1177 const u8 *psk, const char *passphrase,
1178 const u8 *go_dev_addr, int persistent,
1181 const char *ssid_txt;
1185 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1190 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1194 if (passphrase && passphrase[0] == '\0')
1198 * Include PSK/passphrase only in the control interface message and
1199 * leave it out from the debug log entry.
1201 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1202 P2P_EVENT_GROUP_STARTED
1203 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1205 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1206 psk ? " psk=" : "", psk_txt,
1207 passphrase ? " passphrase=\"" : "",
1208 passphrase ? passphrase : "",
1209 passphrase ? "\"" : "",
1210 MAC2STR(go_dev_addr),
1211 persistent ? " [PERSISTENT]" : "", extra);
1212 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1213 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1214 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1215 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1220 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1223 struct wpa_ssid *ssid;
1226 u8 go_dev_addr[ETH_ALEN];
1227 int network_id = -1;
1230 * This callback is likely called for the main interface. Update wpa_s
1231 * to use the group interface if a new interface was created for the
1234 if (wpa_s->global->p2p_group_formation)
1235 wpa_s = wpa_s->global->p2p_group_formation;
1236 if (wpa_s->p2p_go_group_formation_completed) {
1237 wpa_s->global->p2p_group_formation = NULL;
1238 wpa_s->p2p_in_provisioning = 0;
1240 wpa_s->p2p_in_invitation = 0;
1241 wpa_s->group_formation_reported = 1;
1244 wpa_msg_global(wpa_s->parent, MSG_INFO,
1245 P2P_EVENT_GROUP_FORMATION_FAILURE);
1246 wpas_p2p_group_delete(wpa_s,
1247 P2P_GROUP_REMOVAL_FORMATION_FAILED);
1251 wpa_msg_global(wpa_s->parent, MSG_INFO,
1252 P2P_EVENT_GROUP_FORMATION_SUCCESS);
1254 ssid = wpa_s->current_ssid;
1255 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1256 ssid->mode = WPAS_MODE_P2P_GO;
1257 p2p_group_notif_formation_done(wpa_s->p2p_group);
1258 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1263 client = ssid->mode == WPAS_MODE_INFRA;
1264 if (ssid->mode == WPAS_MODE_P2P_GO) {
1265 persistent = ssid->p2p_persistent_group;
1266 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1269 persistent = wpas_p2p_persistent_group(wpa_s,
1274 client = wpa_s->p2p_group_interface ==
1275 P2P_GROUP_INTERFACE_CLIENT;
1276 os_memset(go_dev_addr, 0, ETH_ALEN);
1279 wpa_s->show_group_started = 0;
1282 * Indicate event only after successfully completed 4-way
1283 * handshake, i.e., when the interface is ready for data
1286 wpa_s->show_group_started = 1;
1288 wpas_p2p_group_started(wpa_s, 1, ssid,
1289 ssid ? ssid->frequency : 0,
1290 ssid && ssid->passphrase == NULL &&
1291 ssid->psk_set ? ssid->psk : NULL,
1292 ssid ? ssid->passphrase : NULL,
1293 go_dev_addr, persistent, "");
1294 wpas_p2p_cross_connect_setup(wpa_s);
1295 wpas_p2p_set_group_idle_timeout(wpa_s);
1299 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1302 os_free(wpa_s->global->add_psk);
1303 wpa_s->global->add_psk = NULL;
1305 if (network_id < 0 && ssid)
1306 network_id = ssid->id;
1308 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1309 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1314 struct send_action_work {
1320 unsigned int wait_time;
1325 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1328 struct wpa_supplicant *wpa_s = eloop_ctx;
1330 if (!wpa_s->p2p_send_action_work)
1333 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1334 os_free(wpa_s->p2p_send_action_work->ctx);
1335 radio_work_done(wpa_s->p2p_send_action_work);
1336 wpa_s->p2p_send_action_work = NULL;
1340 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1342 if (wpa_s->p2p_send_action_work) {
1343 struct send_action_work *awork;
1344 awork = wpa_s->p2p_send_action_work->ctx;
1345 if (awork->wait_time == 0) {
1347 radio_work_done(wpa_s->p2p_send_action_work);
1348 wpa_s->p2p_send_action_work = NULL;
1351 * In theory, this should not be needed, but number of
1352 * places in the P2P code is still using non-zero wait
1353 * time for the last Action frame in the sequence and
1354 * some of these do not call send_action_done().
1356 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1358 eloop_register_timeout(
1359 0, awork->wait_time * 1000,
1360 wpas_p2p_send_action_work_timeout,
1367 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1369 const u8 *dst, const u8 *src,
1371 const u8 *data, size_t data_len,
1372 enum offchannel_send_action_result
1375 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1377 wpas_p2p_action_tx_clear(wpa_s);
1379 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1383 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1384 res = P2P_SEND_ACTION_SUCCESS;
1386 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1387 res = P2P_SEND_ACTION_NO_ACK;
1389 case OFFCHANNEL_SEND_ACTION_FAILED:
1390 res = P2P_SEND_ACTION_FAILED;
1394 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1396 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1397 wpa_s->pending_pd_before_join &&
1398 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1399 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1400 wpa_s->p2p_fallback_to_go_neg) {
1401 wpa_s->pending_pd_before_join = 0;
1402 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1403 "during p2p_connect-auto");
1404 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1410 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1412 struct wpa_supplicant *wpa_s = work->wpa_s;
1413 struct send_action_work *awork = work->ctx;
1416 if (work->started) {
1417 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1419 wpa_s->p2p_send_action_work = NULL;
1420 offchannel_send_action_done(wpa_s);
1426 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1427 awork->bssid, awork->buf, awork->len,
1429 wpas_p2p_send_action_tx_status, 1) < 0) {
1431 radio_work_done(work);
1434 wpa_s->p2p_send_action_work = work;
1438 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1439 unsigned int freq, const u8 *dst,
1440 const u8 *src, const u8 *bssid, const u8 *buf,
1441 size_t len, unsigned int wait_time)
1443 struct send_action_work *awork;
1445 if (wpa_s->p2p_send_action_work) {
1446 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1450 awork = os_zalloc(sizeof(*awork) + len);
1455 os_memcpy(awork->dst, dst, ETH_ALEN);
1456 os_memcpy(awork->src, src, ETH_ALEN);
1457 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1459 awork->wait_time = wait_time;
1460 os_memcpy(awork->buf, buf, len);
1462 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1463 wpas_send_action_cb, awork) < 0) {
1472 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1473 const u8 *src, const u8 *bssid, const u8 *buf,
1474 size_t len, unsigned int wait_time)
1476 struct wpa_supplicant *wpa_s = ctx;
1477 int listen_freq = -1, send_freq = -1;
1479 if (wpa_s->p2p_listen_work)
1480 listen_freq = wpa_s->p2p_listen_work->freq;
1481 if (wpa_s->p2p_send_action_work)
1482 send_freq = wpa_s->p2p_send_action_work->freq;
1483 if (listen_freq != (int) freq && send_freq != (int) freq) {
1484 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1485 listen_freq, send_freq);
1486 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1490 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1491 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1493 wpas_p2p_send_action_tx_status, 1);
1497 static void wpas_send_action_done(void *ctx)
1499 struct wpa_supplicant *wpa_s = ctx;
1501 if (wpa_s->p2p_send_action_work) {
1502 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1504 os_free(wpa_s->p2p_send_action_work->ctx);
1505 radio_work_done(wpa_s->p2p_send_action_work);
1506 wpa_s->p2p_send_action_work = NULL;
1509 offchannel_send_action_done(wpa_s);
1513 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1514 struct p2p_go_neg_results *params)
1516 if (wpa_s->go_params == NULL) {
1517 wpa_s->go_params = os_malloc(sizeof(*params));
1518 if (wpa_s->go_params == NULL)
1521 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1526 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1527 struct p2p_go_neg_results *res)
1529 wpa_s->group_formation_reported = 0;
1530 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1531 " dev_addr " MACSTR " wps_method %d",
1532 MAC2STR(res->peer_interface_addr),
1533 MAC2STR(res->peer_device_addr), res->wps_method);
1534 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1535 res->ssid, res->ssid_len);
1536 wpa_supplicant_ap_deinit(wpa_s);
1537 wpas_copy_go_neg_results(wpa_s, res);
1538 if (res->wps_method == WPS_PBC) {
1539 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1540 #ifdef CONFIG_WPS_NFC
1541 } else if (res->wps_method == WPS_NFC) {
1542 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1543 res->peer_interface_addr,
1544 wpa_s->parent->p2p_oob_dev_pw,
1545 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1546 wpa_s->parent->p2p_oob_dev_pw_id ==
1547 DEV_PW_NFC_CONNECTION_HANDOVER ?
1548 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1551 #endif /* CONFIG_WPS_NFC */
1553 u16 dev_pw_id = DEV_PW_DEFAULT;
1554 if (wpa_s->p2p_wps_method == WPS_P2PS)
1555 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1556 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1557 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1558 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1559 wpa_s->p2p_pin, 1, dev_pw_id);
1564 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1565 struct wpa_ssid *ssid)
1567 struct wpa_ssid *persistent;
1568 struct psk_list_entry *psk;
1569 struct hostapd_data *hapd;
1571 if (!wpa_s->ap_iface)
1574 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1576 if (persistent == NULL)
1579 hapd = wpa_s->ap_iface->bss[0];
1581 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1583 struct hostapd_wpa_psk *hpsk;
1585 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1587 MAC2STR(psk->addr), psk->p2p);
1588 hpsk = os_zalloc(sizeof(*hpsk));
1591 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1593 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1595 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1596 hpsk->next = hapd->conf->ssid.wpa_psk;
1597 hapd->conf->ssid.wpa_psk = hpsk;
1602 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1606 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1607 wpa_s->p2p_group_common_freqs_num);
1609 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1610 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1611 i, wpa_s->p2p_group_common_freqs[i]);
1615 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1616 struct p2p_go_neg_results *params)
1618 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1620 wpa_s->p2p_group_common_freqs_num = 0;
1621 os_free(wpa_s->p2p_group_common_freqs);
1622 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1623 if (!wpa_s->p2p_group_common_freqs)
1626 for (i = 0; i < len; i++) {
1627 if (!wpa_s->go_params->freq_list[i])
1629 wpa_s->p2p_group_common_freqs[i] =
1630 wpa_s->go_params->freq_list[i];
1632 wpa_s->p2p_group_common_freqs_num = i;
1636 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1638 #ifndef CONFIG_NO_CONFIG_WRITE
1639 if (wpa_s->parent->conf->update_config &&
1640 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1641 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1642 #endif /* CONFIG_NO_CONFIG_WRITE */
1646 static void p2p_go_configured(void *ctx, void *data)
1648 struct wpa_supplicant *wpa_s = ctx;
1649 struct p2p_go_neg_results *params = data;
1650 struct wpa_ssid *ssid;
1651 int network_id = -1;
1653 p2p_go_save_group_common_freqs(wpa_s, params);
1654 p2p_go_dump_common_freqs(wpa_s);
1656 ssid = wpa_s->current_ssid;
1657 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1658 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1659 if (wpa_s->global->p2p_group_formation == wpa_s)
1660 wpa_s->global->p2p_group_formation = NULL;
1661 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1662 params->passphrase[0] == '\0' ?
1665 wpa_s->global->p2p_dev_addr,
1666 params->persistent_group, "");
1667 wpa_s->group_formation_reported = 1;
1669 if (wpa_s->parent->p2ps_join_addr_valid) {
1670 wpa_dbg(wpa_s, MSG_DEBUG,
1671 "P2PS: Setting default PIN for " MACSTR,
1672 MAC2STR(wpa_s->parent->p2ps_join_addr));
1673 wpa_supplicant_ap_wps_pin(wpa_s,
1674 wpa_s->parent->p2ps_join_addr,
1675 "12345670", NULL, 0, 0);
1676 wpa_s->parent->p2ps_join_addr_valid = 0;
1679 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1680 if (params->persistent_group) {
1681 network_id = wpas_p2p_store_persistent_group(
1682 wpa_s->parent, ssid,
1683 wpa_s->global->p2p_dev_addr);
1684 wpas_p2p_add_psk_list(wpa_s, ssid);
1687 network_id = ssid->id;
1688 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1689 wpas_p2p_cross_connect_setup(wpa_s);
1690 wpas_p2p_set_group_idle_timeout(wpa_s);
1692 if (wpa_s->p2p_first_connection_timeout) {
1693 wpa_dbg(wpa_s, MSG_DEBUG,
1694 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1695 wpa_s->p2p_first_connection_timeout);
1696 wpa_s->p2p_go_group_formation_completed = 0;
1697 wpa_s->global->p2p_group_formation = wpa_s;
1698 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1699 wpa_s->parent, NULL);
1700 eloop_register_timeout(
1701 wpa_s->p2p_first_connection_timeout, 0,
1702 wpas_p2p_group_formation_timeout,
1703 wpa_s->parent, NULL);
1709 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1710 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1711 params->peer_interface_addr)) {
1712 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1716 if (params->wps_method == WPS_PBC) {
1717 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1718 params->peer_device_addr);
1719 #ifdef CONFIG_WPS_NFC
1720 } else if (params->wps_method == WPS_NFC) {
1721 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1722 DEV_PW_NFC_CONNECTION_HANDOVER &&
1723 !wpa_s->parent->p2p_oob_dev_pw) {
1724 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1727 wpas_ap_wps_add_nfc_pw(
1728 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1729 wpa_s->parent->p2p_oob_dev_pw,
1730 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1731 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1732 #endif /* CONFIG_WPS_NFC */
1733 } else if (wpa_s->p2p_pin[0])
1734 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1735 wpa_s->p2p_pin, NULL, 0, 0);
1736 os_free(wpa_s->go_params);
1737 wpa_s->go_params = NULL;
1741 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1742 struct p2p_go_neg_results *params,
1743 int group_formation)
1745 struct wpa_ssid *ssid;
1747 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1748 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1749 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1754 ssid = wpa_config_add_network(wpa_s->conf);
1756 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1760 wpa_s->show_group_started = 0;
1761 wpa_s->p2p_go_group_formation_completed = 0;
1762 wpa_s->group_formation_reported = 0;
1764 wpa_config_set_network_defaults(ssid);
1765 ssid->temporary = 1;
1766 ssid->p2p_group = 1;
1767 ssid->p2p_persistent_group = params->persistent_group;
1768 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1770 ssid->frequency = params->freq;
1771 ssid->ht40 = params->ht40;
1772 ssid->vht = params->vht;
1773 ssid->ssid = os_zalloc(params->ssid_len + 1);
1775 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1776 ssid->ssid_len = params->ssid_len;
1778 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1779 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1780 ssid->proto = WPA_PROTO_RSN;
1781 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1782 ssid->group_cipher = WPA_CIPHER_CCMP;
1783 if (params->freq > 56160) {
1785 * Enable GCMP instead of CCMP as pairwise_cipher and
1786 * group_cipher in 60 GHz.
1788 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1789 ssid->group_cipher = WPA_CIPHER_GCMP;
1791 if (os_strlen(params->passphrase) > 0) {
1792 ssid->passphrase = os_strdup(params->passphrase);
1793 if (ssid->passphrase == NULL) {
1794 wpa_msg_global(wpa_s, MSG_ERROR,
1795 "P2P: Failed to copy passphrase for GO");
1796 wpa_config_remove_network(wpa_s->conf, ssid->id);
1800 ssid->passphrase = NULL;
1801 ssid->psk_set = params->psk_set;
1803 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1804 else if (ssid->passphrase)
1805 wpa_config_update_psk(ssid);
1806 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1808 wpa_s->ap_configured_cb = p2p_go_configured;
1809 wpa_s->ap_configured_cb_ctx = wpa_s;
1810 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1811 wpa_s->scan_req = NORMAL_SCAN_REQ;
1812 wpa_s->connect_without_scan = ssid;
1813 wpa_s->reassociate = 1;
1814 wpa_s->disconnected = 0;
1815 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1817 wpa_supplicant_req_scan(wpa_s, 0, 0);
1821 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1822 const struct wpa_supplicant *src)
1824 struct wpa_config *d;
1825 const struct wpa_config *s;
1830 #define C(n) if (s->n) d->n = os_strdup(s->n)
1839 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1840 os_memcpy(d->sec_device_type, s->sec_device_type,
1841 sizeof(d->sec_device_type));
1842 d->num_sec_device_types = s->num_sec_device_types;
1844 d->p2p_group_idle = s->p2p_group_idle;
1845 d->p2p_intra_bss = s->p2p_intra_bss;
1846 d->persistent_reconnect = s->persistent_reconnect;
1847 d->max_num_sta = s->max_num_sta;
1848 d->pbc_in_m1 = s->pbc_in_m1;
1849 d->ignore_old_scan_res = s->ignore_old_scan_res;
1850 d->beacon_int = s->beacon_int;
1851 d->dtim_period = s->dtim_period;
1852 d->disassoc_low_ack = s->disassoc_low_ack;
1853 d->disable_scan_offload = s->disable_scan_offload;
1854 d->passive_scan = s->passive_scan;
1856 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1857 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1858 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1863 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1864 char *ifname, size_t len)
1866 char *ifname_ptr = wpa_s->ifname;
1868 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1869 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1870 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1873 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1874 if (os_strlen(ifname) >= IFNAMSIZ &&
1875 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1878 /* Try to avoid going over the IFNAMSIZ length limit */
1879 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1880 if (os_snprintf_error(len, res) && len)
1881 ifname[len - 1] = '\0';
1886 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1887 enum wpa_driver_if_type type)
1889 char ifname[120], force_ifname[120];
1891 if (wpa_s->pending_interface_name[0]) {
1892 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1893 "- skip creation of a new one");
1894 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1895 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1896 "unknown?! ifname='%s'",
1897 wpa_s->pending_interface_name);
1903 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1904 force_ifname[0] = '\0';
1906 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1908 wpa_s->p2p_group_idx++;
1910 wpa_s->pending_interface_type = type;
1911 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1912 wpa_s->pending_interface_addr, NULL) < 0) {
1913 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1918 if (force_ifname[0]) {
1919 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1921 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1922 sizeof(wpa_s->pending_interface_name));
1924 os_strlcpy(wpa_s->pending_interface_name, ifname,
1925 sizeof(wpa_s->pending_interface_name));
1926 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1927 MACSTR, wpa_s->pending_interface_name,
1928 MAC2STR(wpa_s->pending_interface_addr));
1934 static void wpas_p2p_remove_pending_group_interface(
1935 struct wpa_supplicant *wpa_s)
1937 if (!wpa_s->pending_interface_name[0] ||
1938 is_zero_ether_addr(wpa_s->pending_interface_addr))
1939 return; /* No pending virtual interface */
1941 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1942 wpa_s->pending_interface_name);
1943 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1944 wpa_s->pending_interface_name);
1945 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1946 wpa_s->pending_interface_name[0] = '\0';
1947 wpa_s->global->pending_group_iface_for_p2ps = 0;
1951 static struct wpa_supplicant *
1952 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1954 struct wpa_interface iface;
1955 struct wpa_supplicant *group_wpa_s;
1957 if (!wpa_s->pending_interface_name[0]) {
1958 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1959 if (!wpas_p2p_create_iface(wpa_s))
1962 * Something has forced us to remove the pending interface; try
1963 * to create a new one and hope for the best that we will get
1964 * the same local address.
1966 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1967 WPA_IF_P2P_CLIENT) < 0)
1971 os_memset(&iface, 0, sizeof(iface));
1972 iface.ifname = wpa_s->pending_interface_name;
1973 iface.driver = wpa_s->driver->name;
1974 if (wpa_s->conf->ctrl_interface == NULL &&
1975 wpa_s->parent != wpa_s &&
1977 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1978 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1980 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1981 iface.driver_param = wpa_s->conf->driver_param;
1982 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1983 if (group_wpa_s == NULL) {
1984 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1985 "wpa_supplicant interface");
1988 wpa_s->pending_interface_name[0] = '\0';
1989 group_wpa_s->parent = wpa_s;
1990 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1991 P2P_GROUP_INTERFACE_CLIENT;
1992 wpa_s->global->p2p_group_formation = group_wpa_s;
1993 wpa_s->global->pending_group_iface_for_p2ps = 0;
1995 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2001 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2004 struct wpa_supplicant *wpa_s = eloop_ctx;
2005 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2006 wpas_p2p_group_formation_failed(wpa_s);
2010 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
2012 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2013 wpa_s->parent, NULL);
2014 if (wpa_s->global->p2p)
2015 p2p_group_formation_failed(wpa_s->global->p2p);
2016 wpas_group_formation_completed(wpa_s, 0);
2020 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2022 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2023 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2024 wpa_s->parent, NULL);
2025 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2026 wpa_s->parent, NULL);
2027 wpa_s->global->p2p_fail_on_wps_complete = 0;
2031 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2033 if (wpa_s->global->p2p_group_formation != wpa_s)
2035 /* Speed up group formation timeout since this cannot succeed */
2036 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2037 wpa_s->parent, NULL);
2038 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2039 wpa_s->parent, NULL);
2043 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2045 struct wpa_supplicant *wpa_s = ctx;
2047 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2048 wpa_drv_cancel_remain_on_channel(wpa_s);
2049 wpa_s->off_channel_freq = 0;
2050 wpa_s->roc_waiting_drv_freq = 0;
2054 wpa_msg_global(wpa_s, MSG_INFO,
2055 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2057 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2058 wpas_p2p_remove_pending_group_interface(wpa_s);
2062 if (wpa_s->p2p_go_ht40)
2064 if (wpa_s->p2p_go_vht)
2067 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2068 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2070 res->role_go ? "GO" : "client", res->freq, res->ht40,
2071 MAC2STR(res->peer_device_addr),
2072 MAC2STR(res->peer_interface_addr),
2073 p2p_wps_method_text(res->wps_method));
2074 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2076 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2077 struct wpa_ssid *ssid;
2078 ssid = wpa_config_get_network(wpa_s->conf,
2079 wpa_s->p2p_persistent_id);
2080 if (ssid && ssid->disabled == 2 &&
2081 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2082 size_t len = os_strlen(ssid->passphrase);
2083 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2084 "on requested persistent group");
2085 os_memcpy(res->passphrase, ssid->passphrase, len);
2086 res->passphrase[len] = '\0';
2090 if (wpa_s->create_p2p_iface) {
2091 struct wpa_supplicant *group_wpa_s =
2092 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2093 if (group_wpa_s == NULL) {
2094 wpas_p2p_remove_pending_group_interface(wpa_s);
2095 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2097 wpas_p2p_group_formation_failed(wpa_s);
2100 if (group_wpa_s != wpa_s) {
2101 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2102 sizeof(group_wpa_s->p2p_pin));
2103 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2105 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2106 wpa_s->pending_interface_name[0] = '\0';
2107 group_wpa_s->p2p_in_provisioning = 1;
2110 wpas_start_wps_go(group_wpa_s, res, 1);
2112 wpas_start_wps_enrollee(group_wpa_s, res);
2114 wpa_s->p2p_in_provisioning = 1;
2115 wpa_s->global->p2p_group_formation = wpa_s;
2118 wpas_start_wps_go(wpa_s, res, 1);
2120 wpas_start_wps_enrollee(ctx, res);
2123 wpa_s->p2p_long_listen = 0;
2124 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2126 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2127 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2128 (res->peer_config_timeout % 100) * 10000,
2129 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2133 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
2135 struct wpa_supplicant *wpa_s = ctx;
2136 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2137 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
2139 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
2143 static void wpas_dev_found(void *ctx, const u8 *addr,
2144 const struct p2p_peer_info *info,
2147 #ifndef CONFIG_NO_STDOUT_DEBUG
2148 struct wpa_supplicant *wpa_s = ctx;
2149 char devtype[WPS_DEV_TYPE_BUFSIZE];
2150 char *wfd_dev_info_hex = NULL;
2152 #ifdef CONFIG_WIFI_DISPLAY
2153 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2154 WFD_SUBELEM_DEVICE_INFO);
2155 #endif /* CONFIG_WIFI_DISPLAY */
2157 if (info->p2ps_instance) {
2159 const u8 *buf = wpabuf_head(info->p2ps_instance);
2160 size_t len = wpabuf_len(info->p2ps_instance);
2167 if (len < 4 + 2 + 1)
2169 id = WPA_GET_LE32(buf);
2171 methods = WPA_GET_BE16(buf);
2174 if (str_len > len - 4 - 2 - 1)
2176 os_memcpy(str, buf, str_len);
2177 str[str_len] = '\0';
2179 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2181 wpa_msg_global(wpa_s, MSG_INFO,
2182 P2P_EVENT_DEVICE_FOUND MACSTR
2183 " p2p_dev_addr=" MACSTR
2184 " pri_dev_type=%s name='%s'"
2185 " config_methods=0x%x"
2188 " adv_id=%x asp_svc=%s%s",
2190 MAC2STR(info->p2p_device_addr),
2191 wps_dev_type_bin2str(
2193 devtype, sizeof(devtype)),
2194 info->device_name, methods,
2195 info->dev_capab, info->group_capab,
2197 info->vendor_elems ?
2198 " vendor_elems=1" : "");
2203 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2204 " p2p_dev_addr=" MACSTR
2205 " pri_dev_type=%s name='%s' config_methods=0x%x "
2206 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2207 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2208 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2210 info->device_name, info->config_methods,
2211 info->dev_capab, info->group_capab,
2212 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2213 wfd_dev_info_hex ? wfd_dev_info_hex : "",
2214 info->vendor_elems ? " vendor_elems=1" : "",
2218 os_free(wfd_dev_info_hex);
2219 #endif /* CONFIG_NO_STDOUT_DEBUG */
2221 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2225 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2227 struct wpa_supplicant *wpa_s = ctx;
2229 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2230 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2232 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2236 static void wpas_find_stopped(void *ctx)
2238 struct wpa_supplicant *wpa_s = ctx;
2239 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2243 struct wpas_p2p_listen_work {
2245 unsigned int duration;
2246 struct wpabuf *probe_resp_ie;
2250 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2254 wpabuf_free(lwork->probe_resp_ie);
2259 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2261 struct wpas_p2p_listen_work *lwork;
2263 if (!wpa_s->p2p_listen_work)
2266 lwork = wpa_s->p2p_listen_work->ctx;
2267 wpas_p2p_listen_work_free(lwork);
2268 radio_work_done(wpa_s->p2p_listen_work);
2269 wpa_s->p2p_listen_work = NULL;
2273 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2275 struct wpa_supplicant *wpa_s = work->wpa_s;
2276 struct wpas_p2p_listen_work *lwork = work->ctx;
2277 unsigned int duration;
2280 if (work->started) {
2281 wpa_s->p2p_listen_work = NULL;
2282 wpas_stop_listen(wpa_s);
2284 wpas_p2p_listen_work_free(lwork);
2288 wpa_s->p2p_listen_work = work;
2290 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2292 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2293 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2294 "report received Probe Request frames");
2295 wpas_p2p_listen_work_done(wpa_s);
2299 wpa_s->pending_listen_freq = lwork->freq;
2300 wpa_s->pending_listen_duration = lwork->duration;
2302 duration = lwork->duration;
2303 #ifdef CONFIG_TESTING_OPTIONS
2304 if (wpa_s->extra_roc_dur) {
2305 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2306 duration, duration + wpa_s->extra_roc_dur);
2307 duration += wpa_s->extra_roc_dur;
2309 #endif /* CONFIG_TESTING_OPTIONS */
2311 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2312 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2313 "to remain on channel (%u MHz) for Listen "
2314 "state", lwork->freq);
2315 wpas_p2p_listen_work_done(wpa_s);
2316 wpa_s->pending_listen_freq = 0;
2319 wpa_s->off_channel_freq = 0;
2320 wpa_s->roc_waiting_drv_freq = lwork->freq;
2324 static int wpas_start_listen(void *ctx, unsigned int freq,
2325 unsigned int duration,
2326 const struct wpabuf *probe_resp_ie)
2328 struct wpa_supplicant *wpa_s = ctx;
2329 struct wpas_p2p_listen_work *lwork;
2331 if (wpa_s->p2p_listen_work) {
2332 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2336 lwork = os_zalloc(sizeof(*lwork));
2340 lwork->duration = duration;
2341 if (probe_resp_ie) {
2342 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2343 if (lwork->probe_resp_ie == NULL) {
2344 wpas_p2p_listen_work_free(lwork);
2349 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2351 wpas_p2p_listen_work_free(lwork);
2359 static void wpas_stop_listen(void *ctx)
2361 struct wpa_supplicant *wpa_s = ctx;
2362 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2363 wpa_drv_cancel_remain_on_channel(wpa_s);
2364 wpa_s->off_channel_freq = 0;
2365 wpa_s->roc_waiting_drv_freq = 0;
2367 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2368 wpa_drv_probe_req_report(wpa_s, 0);
2369 wpas_p2p_listen_work_done(wpa_s);
2373 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
2375 struct wpa_supplicant *wpa_s = ctx;
2376 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
2381 * DNS Header section is used only to calculate compression pointers, so the
2382 * contents of this data does not matter, but the length needs to be reserved
2383 * in the virtual packet.
2385 #define DNS_HEADER_LEN 12
2388 * 27-octet in-memory packet from P2P specification containing two implied
2389 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
2391 #define P2P_SD_IN_MEMORY_LEN 27
2393 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
2394 u8 **spos, const u8 *end)
2396 while (*spos < end) {
2397 u8 val = ((*spos)[0] & 0xc0) >> 6;
2400 if (val == 1 || val == 2) {
2401 /* These are reserved values in RFC 1035 */
2402 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2403 "sequence starting with 0x%x", val);
2412 if (*spos + 2 > end) {
2413 wpa_printf(MSG_DEBUG, "P2P: No room for full "
2414 "DNS offset field");
2418 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
2419 if (offset >= *spos - start) {
2420 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
2421 "pointer offset %u", offset);
2426 spos_tmp = start + offset;
2427 return p2p_sd_dns_uncompress_label(upos, uend, start,
2433 len = (*spos)[0] & 0x3f;
2438 if (*spos + len > end) {
2439 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2440 "sequence - no room for label with length "
2445 if (*upos + len + 2 > uend)
2448 os_memcpy(*upos, *spos, len);
2460 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
2461 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
2462 * not large enough */
2463 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
2464 size_t msg_len, size_t offset)
2466 /* 27-octet in-memory packet from P2P specification */
2467 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
2468 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
2469 u8 *tmp, *end, *spos;
2475 if (offset > msg_len)
2478 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
2481 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
2482 end = spos + msg_len;
2485 os_memset(tmp, 0, DNS_HEADER_LEN);
2486 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
2487 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
2490 uend = buf + buf_len;
2492 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
2501 } else if (upos[-1] == '.')
2509 static struct p2p_srv_bonjour *
2510 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2511 const struct wpabuf *query)
2513 struct p2p_srv_bonjour *bsrv;
2516 len = wpabuf_len(query);
2517 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2518 struct p2p_srv_bonjour, list) {
2519 if (len == wpabuf_len(bsrv->query) &&
2520 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2528 static struct p2p_srv_upnp *
2529 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2530 const char *service)
2532 struct p2p_srv_upnp *usrv;
2534 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2535 struct p2p_srv_upnp, list) {
2536 if (version == usrv->version &&
2537 os_strcmp(service, usrv->service) == 0)
2544 static void wpas_sd_add_empty(struct wpabuf *resp, u8 srv_proto,
2545 u8 srv_trans_id, u8 status)
2549 if (wpabuf_tailroom(resp) < 5)
2552 /* Length (to be filled) */
2553 len_pos = wpabuf_put(resp, 2);
2554 wpabuf_put_u8(resp, srv_proto);
2555 wpabuf_put_u8(resp, srv_trans_id);
2557 wpabuf_put_u8(resp, status);
2558 /* Response Data: empty */
2559 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2563 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2566 wpas_sd_add_empty(resp, srv_proto, srv_trans_id,
2567 P2P_SD_PROTO_NOT_AVAILABLE);
2571 static void wpas_sd_add_bad_request(struct wpabuf *resp, u8 srv_proto,
2574 wpas_sd_add_empty(resp, srv_proto, srv_trans_id, P2P_SD_BAD_REQUEST);
2578 static void wpas_sd_add_not_found(struct wpabuf *resp, u8 srv_proto,
2581 wpas_sd_add_empty(resp, srv_proto, srv_trans_id,
2582 P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2586 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2587 struct wpabuf *resp, u8 srv_trans_id)
2589 struct p2p_srv_bonjour *bsrv;
2592 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2594 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2595 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2599 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2600 struct p2p_srv_bonjour, list) {
2601 if (wpabuf_tailroom(resp) <
2602 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2604 /* Length (to be filled) */
2605 len_pos = wpabuf_put(resp, 2);
2606 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2607 wpabuf_put_u8(resp, srv_trans_id);
2609 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2610 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2611 wpabuf_head(bsrv->resp),
2612 wpabuf_len(bsrv->resp));
2614 wpabuf_put_buf(resp, bsrv->query); /* Key */
2615 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2616 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2622 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2625 char str_rx[256], str_srv[256];
2627 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2628 return 0; /* Too short to include DNS Type and Version */
2629 if (os_memcmp(query + query_len - 3,
2630 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2632 return 0; /* Mismatch in DNS Type or Version */
2633 if (query_len == wpabuf_len(bsrv->query) &&
2634 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2635 return 1; /* Binary match */
2637 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2639 return 0; /* Failed to uncompress query */
2640 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2641 wpabuf_head(bsrv->query),
2642 wpabuf_len(bsrv->query) - 3, 0))
2643 return 0; /* Failed to uncompress service */
2645 return os_strcmp(str_rx, str_srv) == 0;
2649 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2650 struct wpabuf *resp, u8 srv_trans_id,
2651 const u8 *query, size_t query_len)
2653 struct p2p_srv_bonjour *bsrv;
2657 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2659 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2660 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2661 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2666 if (query_len == 0) {
2667 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2671 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2672 struct p2p_srv_bonjour, list) {
2673 if (!match_bonjour_query(bsrv, query, query_len))
2676 if (wpabuf_tailroom(resp) <
2677 5 + query_len + wpabuf_len(bsrv->resp))
2682 /* Length (to be filled) */
2683 len_pos = wpabuf_put(resp, 2);
2684 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2685 wpabuf_put_u8(resp, srv_trans_id);
2688 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2689 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2690 wpabuf_head(bsrv->resp),
2691 wpabuf_len(bsrv->resp));
2694 wpabuf_put_data(resp, query, query_len); /* Key */
2695 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2697 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2701 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2703 if (wpabuf_tailroom(resp) < 5)
2706 /* Length (to be filled) */
2707 len_pos = wpabuf_put(resp, 2);
2708 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2709 wpabuf_put_u8(resp, srv_trans_id);
2712 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2713 /* Response Data: empty */
2714 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2720 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2721 struct wpabuf *resp, u8 srv_trans_id)
2723 struct p2p_srv_upnp *usrv;
2726 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2728 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2729 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2733 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2734 struct p2p_srv_upnp, list) {
2735 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2738 /* Length (to be filled) */
2739 len_pos = wpabuf_put(resp, 2);
2740 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2741 wpabuf_put_u8(resp, srv_trans_id);
2744 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2746 wpabuf_put_u8(resp, usrv->version);
2747 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2749 wpabuf_put_str(resp, usrv->service);
2750 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2756 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2757 struct wpabuf *resp, u8 srv_trans_id,
2758 const u8 *query, size_t query_len)
2760 struct p2p_srv_upnp *usrv;
2766 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2769 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2770 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2771 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2776 if (query_len == 0) {
2777 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2781 if (wpabuf_tailroom(resp) < 5)
2784 /* Length (to be filled) */
2785 len_pos = wpabuf_put(resp, 2);
2786 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2787 wpabuf_put_u8(resp, srv_trans_id);
2790 str = os_malloc(query_len);
2793 os_memcpy(str, query + 1, query_len - 1);
2794 str[query_len - 1] = '\0';
2796 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2797 struct p2p_srv_upnp, list) {
2798 if (version != usrv->version)
2801 if (os_strcmp(str, "ssdp:all") != 0 &&
2802 os_strstr(usrv->service, str) == NULL)
2805 if (wpabuf_tailroom(resp) < 2)
2809 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2811 wpabuf_put_u8(resp, version);
2813 wpabuf_put_u8(resp, ',');
2817 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2819 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2821 wpabuf_put_str(resp, usrv->service);
2826 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2829 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2830 /* Response Data: empty */
2833 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2837 #ifdef CONFIG_WIFI_DISPLAY
2838 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2839 struct wpabuf *resp, u8 srv_trans_id,
2840 const u8 *query, size_t query_len)
2846 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2848 if (!wpa_s->global->wifi_display) {
2849 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2850 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2855 if (query_len < 1) {
2856 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2861 if (wpabuf_tailroom(resp) < 5)
2866 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2868 /* TODO: role specific handling */
2870 /* Length (to be filled) */
2871 len_pos = wpabuf_put(resp, 2);
2872 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2873 wpabuf_put_u8(resp, srv_trans_id);
2874 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2876 while (pos < query + query_len) {
2877 if (*pos < MAX_WFD_SUBELEMS &&
2878 wpa_s->global->wfd_subelem[*pos] &&
2879 wpabuf_tailroom(resp) >=
2880 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2881 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2882 "subelement %u", *pos);
2883 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2888 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2890 #endif /* CONFIG_WIFI_DISPLAY */
2893 static int find_p2ps_substr(struct p2ps_advertisement *adv_data,
2894 const u8 *needle, size_t needle_len)
2896 const u8 *haystack = (const u8 *) adv_data->svc_info;
2897 size_t haystack_len, i;
2899 /* Allow search term to be empty */
2900 if (!needle || !needle_len)
2906 haystack_len = os_strlen(adv_data->svc_info);
2907 for (i = 0; i < haystack_len; i++) {
2908 if (haystack_len - i < needle_len)
2910 if (os_memcmp(haystack + i, needle, needle_len) == 0)
2918 static void wpas_sd_req_asp(struct wpa_supplicant *wpa_s,
2919 struct wpabuf *resp, u8 srv_trans_id,
2920 const u8 *query, size_t query_len)
2922 struct p2ps_advertisement *adv_data;
2923 const u8 *svc = &query[1];
2924 const u8 *info = NULL;
2925 size_t svc_len = query[0];
2926 size_t info_len = 0;
2928 u8 *count_pos = NULL;
2931 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for ASP", query, query_len);
2933 if (!wpa_s->global->p2p) {
2934 wpa_printf(MSG_DEBUG, "P2P: ASP protocol not available");
2935 wpas_sd_add_proto_not_avail(resp, P2P_SERV_P2PS, srv_trans_id);
2939 /* Info block is optional */
2940 if (svc_len + 1 < query_len) {
2941 info = &svc[svc_len];
2945 /* Range check length of svc string and info block */
2946 if (svc_len + (info_len ? info_len + 2 : 1) > query_len) {
2947 wpa_printf(MSG_DEBUG, "P2P: ASP bad request");
2948 wpas_sd_add_bad_request(resp, P2P_SERV_P2PS, srv_trans_id);
2952 /* Detect and correct for prefix search */
2953 if (svc_len && svc[svc_len - 1] == '*') {
2958 for (adv_data = p2p_get_p2ps_adv_list(wpa_s->global->p2p);
2959 adv_data; adv_data = adv_data->next) {
2960 /* If not a prefix match, reject length mismatches */
2961 if (!prefix && svc_len != os_strlen(adv_data->svc_name))
2964 /* Search each service for request */
2965 if (os_memcmp(adv_data->svc_name, svc, svc_len) == 0 &&
2966 find_p2ps_substr(adv_data, info, info_len)) {
2967 size_t len = os_strlen(adv_data->svc_name);
2968 size_t svc_info_len = 0;
2970 if (adv_data->svc_info)
2971 svc_info_len = os_strlen(adv_data->svc_info);
2973 if (len > 0xff || svc_info_len > 0xffff)
2976 /* Length & Count to be filled as we go */
2977 if (!len_pos && !count_pos) {
2978 if (wpabuf_tailroom(resp) <
2979 len + svc_info_len + 16)
2982 len_pos = wpabuf_put(resp, 2);
2983 wpabuf_put_u8(resp, P2P_SERV_P2PS);
2984 wpabuf_put_u8(resp, srv_trans_id);
2986 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2987 count_pos = wpabuf_put(resp, 1);
2989 } else if (wpabuf_tailroom(resp) <
2990 len + svc_info_len + 10)
2994 wpa_printf(MSG_DEBUG,
2995 "P2P: Add Svc: %s info: %s",
2997 adv_data->svc_info);
2999 wpa_printf(MSG_DEBUG, "P2P: Add Svc: %s",
3000 adv_data->svc_name);
3003 /* Advertisement ID */
3004 wpabuf_put_le32(resp, adv_data->id);
3006 /* Config Methods */
3007 wpabuf_put_be16(resp, adv_data->config_methods);
3010 wpabuf_put_u8(resp, (u8) len);
3011 wpabuf_put_data(resp, adv_data->svc_name, len);
3014 wpabuf_put_u8(resp, adv_data->state);
3016 /* Service Information */
3017 wpabuf_put_le16(resp, (u16) svc_info_len);
3018 wpabuf_put_data(resp, adv_data->svc_info, svc_info_len);
3020 /* Update length and count */
3022 WPA_PUT_LE16(len_pos,
3023 (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
3027 /* Return error if no matching svc found */
3028 if (count_pos == NULL) {
3029 wpa_printf(MSG_DEBUG, "P2P: ASP service not found");
3030 wpas_sd_add_not_found(resp, P2P_SERV_P2PS, srv_trans_id);
3035 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
3036 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
3038 struct wpa_supplicant *wpa_s = ctx;
3039 const u8 *pos = tlvs;
3040 const u8 *end = tlvs + tlvs_len;
3043 struct wpabuf *resp;
3044 u8 srv_proto, srv_trans_id;
3048 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
3050 buf_len = 2 * tlvs_len + 1;
3051 buf = os_malloc(buf_len);
3053 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
3054 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
3056 freq, MAC2STR(sa), dialog_token, update_indic,
3061 if (wpa_s->p2p_sd_over_ctrl_iface) {
3062 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
3063 update_indic, tlvs, tlvs_len);
3064 return; /* to be processed by an external program */
3067 resp = wpabuf_alloc(10000);
3071 while (pos + 1 < end) {
3072 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
3073 slen = WPA_GET_LE16(pos);
3075 if (pos + slen > end || slen < 2) {
3076 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
3081 tlv_end = pos + slen;
3084 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
3086 srv_trans_id = *pos++;
3087 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
3090 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
3091 pos, tlv_end - pos);
3094 if (wpa_s->force_long_sd) {
3095 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
3097 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
3098 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
3102 switch (srv_proto) {
3103 case P2P_SERV_ALL_SERVICES:
3104 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
3105 "for all services");
3106 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
3107 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
3108 wpa_printf(MSG_DEBUG, "P2P: No service "
3109 "discovery protocols available");
3110 wpas_sd_add_proto_not_avail(
3111 resp, P2P_SERV_ALL_SERVICES,
3115 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
3116 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
3118 case P2P_SERV_BONJOUR:
3119 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
3120 pos, tlv_end - pos);
3123 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
3124 pos, tlv_end - pos);
3126 #ifdef CONFIG_WIFI_DISPLAY
3127 case P2P_SERV_WIFI_DISPLAY:
3128 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
3129 pos, tlv_end - pos);
3131 #endif /* CONFIG_WIFI_DISPLAY */
3133 wpas_sd_req_asp(wpa_s, resp, srv_trans_id,
3134 pos, tlv_end - pos);
3137 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
3138 "protocol %u", srv_proto);
3139 wpas_sd_add_proto_not_avail(resp, srv_proto,
3148 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
3149 update_indic, tlvs, tlvs_len);
3151 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
3157 static void wpas_sd_p2ps_serv_response(struct wpa_supplicant *wpa_s,
3158 const u8 *sa, u8 srv_trans_id,
3159 const u8 *pos, const u8 *tlv_end)
3167 while (left-- && pos < tlv_end) {
3172 /* Sanity check fixed length+svc_str */
3173 if (pos + 6 >= tlv_end)
3176 if (pos + svc_len + 10 > tlv_end)
3179 /* Advertisement ID */
3180 adv_id = WPA_GET_LE32(pos);
3183 /* Config Methods */
3184 config_methods = WPA_GET_BE16(pos);
3188 pos++; /* svc_len */
3189 os_memcpy(svc_str, pos, svc_len);
3190 svc_str[svc_len] = '\0';
3193 /* Service Status */
3194 svc_status = *pos++;
3196 /* Service Information Length */
3197 buf_len = WPA_GET_LE16(pos);
3200 /* Sanity check buffer length */
3201 if (buf_len > (unsigned int) (tlv_end - pos))
3205 buf = os_zalloc(2 * buf_len + 1);
3207 utf8_escape((const char *) pos, buf_len, buf,
3215 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_SERV_ASP_RESP
3216 MACSTR " %x %x %x %x %s '%s'",
3217 MAC2STR(sa), srv_trans_id, adv_id,
3218 svc_status, config_methods, svc_str,
3222 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_SERV_ASP_RESP
3223 MACSTR " %x %x %x %x %s",
3224 MAC2STR(sa), srv_trans_id, adv_id,
3225 svc_status, config_methods, svc_str);
3231 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
3232 const u8 *tlvs, size_t tlvs_len)
3234 struct wpa_supplicant *wpa_s = ctx;
3235 const u8 *pos = tlvs;
3236 const u8 *end = tlvs + tlvs_len;
3242 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
3244 if (tlvs_len > 1500) {
3245 /* TODO: better way for handling this */
3246 wpa_msg_ctrl(wpa_s, MSG_INFO,
3247 P2P_EVENT_SERV_DISC_RESP MACSTR
3248 " %u <long response: %u bytes>",
3249 MAC2STR(sa), update_indic,
3250 (unsigned int) tlvs_len);
3252 buf_len = 2 * tlvs_len + 1;
3253 buf = os_malloc(buf_len);
3255 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
3256 wpa_msg_ctrl(wpa_s, MSG_INFO,
3257 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
3258 MAC2STR(sa), update_indic, buf);
3264 u8 srv_proto, srv_trans_id, status;
3266 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
3267 slen = WPA_GET_LE16(pos);
3269 if (pos + slen > end || slen < 3) {
3270 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
3274 tlv_end = pos + slen;
3277 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
3279 srv_trans_id = *pos++;
3280 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
3283 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
3286 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
3287 pos, tlv_end - pos);
3289 if (srv_proto == P2P_SERV_P2PS && pos < tlv_end) {
3290 wpas_sd_p2ps_serv_response(wpa_s, sa, srv_trans_id,
3297 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
3301 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3302 const struct wpabuf *tlvs)
3304 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3306 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
3310 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
3311 u8 version, const char *query)
3313 struct wpabuf *tlvs;
3316 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
3319 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
3320 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
3321 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
3322 wpabuf_put_u8(tlvs, version);
3323 wpabuf_put_str(tlvs, query);
3324 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
3330 u64 wpas_p2p_sd_request_asp(struct wpa_supplicant *wpa_s, const u8 *dst, u8 id,
3331 const char *svc_str, const char *info_substr)
3333 struct wpabuf *tlvs;
3334 size_t plen, svc_len, substr_len = 0;
3337 svc_len = os_strlen(svc_str);
3339 substr_len = os_strlen(info_substr);
3341 if (svc_len > 0xff || substr_len > 0xff)
3344 plen = 1 + 1 + 1 + svc_len + 1 + substr_len;
3345 tlvs = wpabuf_alloc(2 + plen);
3349 wpabuf_put_le16(tlvs, plen);
3350 wpabuf_put_u8(tlvs, P2P_SERV_P2PS);
3351 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
3352 wpabuf_put_u8(tlvs, (u8) svc_len); /* Service String Length */
3353 wpabuf_put_data(tlvs, svc_str, svc_len);
3354 wpabuf_put_u8(tlvs, (u8) substr_len); /* Info Substring Length */
3355 wpabuf_put_data(tlvs, info_substr, substr_len);
3356 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
3363 #ifdef CONFIG_WIFI_DISPLAY
3365 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
3366 const struct wpabuf *tlvs)
3368 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3370 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
3374 #define MAX_WFD_SD_SUBELEMS 20
3376 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
3377 const char *subelems)
3384 len = wpabuf_put(tlvs, 2);
3385 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
3386 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
3388 wpabuf_put_u8(tlvs, role);
3393 if (val >= 0 && val < 256) {
3394 wpabuf_put_u8(tlvs, val);
3396 if (count == MAX_WFD_SD_SUBELEMS)
3399 pos = os_strchr(pos + 1, ',');
3405 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
3409 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
3410 const u8 *dst, const char *role)
3412 struct wpabuf *tlvs;
3414 const char *subelems;
3417 subelems = os_strchr(role, ' ');
3418 if (subelems == NULL)
3422 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
3426 if (os_strstr(role, "[source]"))
3427 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
3428 if (os_strstr(role, "[pri-sink]"))
3429 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
3430 if (os_strstr(role, "[sec-sink]"))
3431 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
3432 if (os_strstr(role, "[source+sink]"))
3433 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
3435 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
3440 #endif /* CONFIG_WIFI_DISPLAY */
3443 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
3445 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3447 return p2p_sd_cancel_request(wpa_s->global->p2p,
3448 (void *) (uintptr_t) req);
3452 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
3453 const u8 *dst, u8 dialog_token,
3454 const struct wpabuf *resp_tlvs)
3456 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3458 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
3463 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
3465 if (wpa_s->global->p2p)
3466 p2p_sd_service_update(wpa_s->global->p2p);
3470 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
3472 dl_list_del(&bsrv->list);
3473 wpabuf_free(bsrv->query);
3474 wpabuf_free(bsrv->resp);
3479 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
3481 dl_list_del(&usrv->list);
3482 os_free(usrv->service);
3487 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
3489 struct p2p_srv_bonjour *bsrv, *bn;
3490 struct p2p_srv_upnp *usrv, *un;
3492 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
3493 struct p2p_srv_bonjour, list)
3494 wpas_p2p_srv_bonjour_free(bsrv);
3496 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
3497 struct p2p_srv_upnp, list)
3498 wpas_p2p_srv_upnp_free(usrv);
3500 wpas_p2p_sd_service_update(wpa_s);
3504 int wpas_p2p_service_p2ps_id_exists(struct wpa_supplicant *wpa_s, u32 adv_id)
3509 if (p2p_service_p2ps_id(wpa_s->global->p2p, adv_id))
3516 int wpas_p2p_service_del_asp(struct wpa_supplicant *wpa_s, u32 adv_id)
3518 return p2p_service_del_asp(wpa_s->global->p2p, adv_id);
3522 int wpas_p2p_service_add_asp(struct wpa_supplicant *wpa_s,
3523 int auto_accept, u32 adv_id,
3524 const char *adv_str, u8 svc_state,
3525 u16 config_methods, const char *svc_info)
3527 return p2p_service_add_asp(wpa_s->global->p2p, auto_accept, adv_id,
3528 adv_str, svc_state, config_methods,
3533 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
3534 struct wpabuf *query, struct wpabuf *resp)
3536 struct p2p_srv_bonjour *bsrv;
3538 bsrv = os_zalloc(sizeof(*bsrv));
3541 bsrv->query = query;
3543 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
3545 wpas_p2p_sd_service_update(wpa_s);
3550 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
3551 const struct wpabuf *query)
3553 struct p2p_srv_bonjour *bsrv;
3555 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
3558 wpas_p2p_srv_bonjour_free(bsrv);
3559 wpas_p2p_sd_service_update(wpa_s);
3564 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
3565 const char *service)
3567 struct p2p_srv_upnp *usrv;
3569 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
3570 return 0; /* Already listed */
3571 usrv = os_zalloc(sizeof(*usrv));
3574 usrv->version = version;
3575 usrv->service = os_strdup(service);
3576 if (usrv->service == NULL) {
3580 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
3582 wpas_p2p_sd_service_update(wpa_s);
3587 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
3588 const char *service)
3590 struct p2p_srv_upnp *usrv;
3592 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
3595 wpas_p2p_srv_upnp_free(usrv);
3596 wpas_p2p_sd_service_update(wpa_s);
3601 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
3602 const u8 *peer, const char *params,
3603 unsigned int generated_pin)
3605 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
3606 " %08d%s", MAC2STR(peer), generated_pin, params);
3610 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
3611 const u8 *peer, const char *params)
3613 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
3614 "%s", MAC2STR(peer), params);
3618 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
3619 const u8 *dev_addr, const u8 *pri_dev_type,
3620 const char *dev_name, u16 supp_config_methods,
3621 u8 dev_capab, u8 group_capab, const u8 *group_id,
3622 size_t group_id_len)
3624 struct wpa_supplicant *wpa_s = ctx;
3625 char devtype[WPS_DEV_TYPE_BUFSIZE];
3627 u8 empty_dev_type[8];
3628 unsigned int generated_pin = 0;
3629 struct wpa_supplicant *group = NULL;
3633 for (group = wpa_s->global->ifaces; group; group = group->next)
3635 struct wpa_ssid *s = group->current_ssid;
3637 s->mode == WPAS_MODE_P2P_GO &&
3638 group_id_len - ETH_ALEN == s->ssid_len &&
3639 os_memcmp(group_id + ETH_ALEN, s->ssid,
3645 if (pri_dev_type == NULL) {
3646 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
3647 pri_dev_type = empty_dev_type;
3649 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
3650 " pri_dev_type=%s name='%s' config_methods=0x%x "
3651 "dev_capab=0x%x group_capab=0x%x%s%s",
3653 wps_dev_type_bin2str(pri_dev_type, devtype,
3655 dev_name, supp_config_methods, dev_capab, group_capab,
3656 group ? " group=" : "",
3657 group ? group->ifname : "");
3658 if (os_snprintf_error(sizeof(params), res))
3659 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
3660 params[sizeof(params) - 1] = '\0';
3662 if (config_methods & WPS_CONFIG_DISPLAY) {
3663 generated_pin = wps_generate_pin();
3664 wpas_prov_disc_local_display(wpa_s, peer, params,
3666 } else if (config_methods & WPS_CONFIG_KEYPAD)
3667 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3668 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3669 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
3670 MACSTR "%s", MAC2STR(peer), params);
3672 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
3673 P2P_PROV_DISC_SUCCESS,
3674 config_methods, generated_pin);
3678 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
3680 struct wpa_supplicant *wpa_s = ctx;
3681 unsigned int generated_pin = 0;
3684 if (wpa_s->pending_pd_before_join &&
3685 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
3686 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
3687 wpa_s->pending_pd_before_join = 0;
3688 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3689 "join-existing-group operation");
3690 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3694 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
3695 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
3698 res = os_snprintf(params, sizeof(params), " peer_go=%d",
3699 wpa_s->pending_pd_use == AUTO_PD_JOIN);
3700 if (os_snprintf_error(sizeof(params), res))
3701 params[sizeof(params) - 1] = '\0';
3705 if (config_methods & WPS_CONFIG_DISPLAY)
3706 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3707 else if (config_methods & WPS_CONFIG_KEYPAD) {
3708 generated_pin = wps_generate_pin();
3709 wpas_prov_disc_local_display(wpa_s, peer, params,
3711 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3712 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
3713 MACSTR "%s", MAC2STR(peer), params);
3715 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3716 P2P_PROV_DISC_SUCCESS,
3717 config_methods, generated_pin);
3721 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
3722 enum p2p_prov_disc_status status,
3723 u32 adv_id, const u8 *adv_mac,
3724 const char *deferred_session_resp)
3726 struct wpa_supplicant *wpa_s = ctx;
3728 if (wpa_s->p2p_fallback_to_go_neg) {
3729 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
3730 "failed - fall back to GO Negotiation");
3731 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
3735 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
3736 wpa_s->pending_pd_before_join = 0;
3737 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3738 "join-existing-group operation (no ACK for PD "
3740 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3744 if (adv_id && adv_mac && deferred_session_resp) {
3745 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3746 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3747 " deferred_session_resp='%s'",
3748 MAC2STR(peer), status, adv_id,
3749 deferred_session_resp);
3750 } else if (adv_id && adv_mac) {
3751 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3752 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3753 MAC2STR(peer), status, adv_id);
3755 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3756 " p2p_dev_addr=" MACSTR " status=%d",
3757 MAC2STR(peer), status);
3760 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3765 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3767 if (channels == NULL)
3768 return 1; /* Assume no restrictions */
3769 return p2p_channels_includes_freq(channels, freq);
3775 * Pick the best frequency to use from all the currently used frequencies.
3777 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3778 struct wpa_used_freq_data *freqs,
3783 /* find a candidate freq that is supported by P2P */
3784 for (c = 0; c < num; c++)
3785 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3791 /* once we have a candidate, try to find a 'better' one */
3792 for (i = c + 1; i < num; i++) {
3793 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3797 * 1. Infrastructure station interfaces have higher preference.
3798 * 2. P2P Clients have higher preference.
3801 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3806 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3809 return freqs[c].freq;
3813 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3814 const u8 *go_dev_addr, const u8 *ssid,
3815 size_t ssid_len, int *go, u8 *group_bssid,
3816 int *force_freq, int persistent_group,
3817 const struct p2p_channels *channels,
3820 struct wpa_supplicant *wpa_s = ctx;
3822 struct wpa_used_freq_data *freqs;
3823 struct wpa_supplicant *grp;
3826 if (!persistent_group) {
3827 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3828 " to join an active group (SSID: %s)",
3829 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
3830 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3831 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3833 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3834 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3835 "authorized invitation");
3839 #ifdef CONFIG_WPS_NFC
3840 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3841 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
3842 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3843 wpa_s->p2p_wps_method = WPS_NFC;
3844 wpa_s->pending_join_wps_method = WPS_NFC;
3845 os_memcpy(wpa_s->pending_join_dev_addr,
3846 go_dev_addr, ETH_ALEN);
3847 os_memcpy(wpa_s->pending_join_iface_addr,
3851 #endif /* CONFIG_WPS_NFC */
3854 * Do not accept the invitation automatically; notify user and
3857 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3860 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3862 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3863 "running persistent group");
3865 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3869 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3870 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3871 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3872 "invitation to re-invoke a persistent group");
3873 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3874 } else if (!wpa_s->conf->persistent_reconnect)
3875 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3877 for (s = wpa_s->conf->ssid; s; s = s->next) {
3878 if (s->disabled == 2 &&
3879 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3880 s->ssid_len == ssid_len &&
3881 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3886 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3887 " requested reinvocation of an unknown group",
3889 return P2P_SC_FAIL_UNKNOWN_GROUP;
3892 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3894 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3895 wpa_printf(MSG_DEBUG, "P2P: The only available "
3896 "interface is already in use - reject "
3898 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3900 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3901 } else if (s->mode == WPAS_MODE_P2P_GO) {
3903 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3905 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3906 "interface address for the group");
3907 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3909 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3914 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3917 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3918 sizeof(struct wpa_used_freq_data));
3920 int num_channels = wpa_s->num_multichan_concurrent;
3921 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3922 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3926 /* Get one of the frequencies currently in use */
3927 if (best_freq > 0) {
3928 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3929 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3931 if (wpa_s->num_multichan_concurrent < 2 ||
3932 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3933 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3934 *force_freq = best_freq;
3938 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3939 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3941 /* We are the client */
3942 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3943 "running a GO but we are capable of MCC, "
3944 "figure out the best channel to use");
3946 } else if (!freq_included(channels, *force_freq)) {
3947 /* We are the GO, and *force_freq is not in the
3949 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3950 "in intersection but we are capable of MCC, "
3951 "figure out the best channel to use",
3957 return P2P_SC_SUCCESS;
3961 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3962 const u8 *ssid, size_t ssid_len,
3963 const u8 *go_dev_addr, u8 status,
3966 struct wpa_supplicant *wpa_s = ctx;
3969 for (s = wpa_s->conf->ssid; s; s = s->next) {
3970 if (s->disabled == 2 &&
3971 s->ssid_len == ssid_len &&
3972 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3976 if (status == P2P_SC_SUCCESS) {
3977 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3978 " was accepted; op_freq=%d MHz, SSID=%s",
3979 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3981 int go = s->mode == WPAS_MODE_P2P_GO;
3982 wpas_p2p_group_add_persistent(
3983 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
3984 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3986 wpa_s->user_initiated_pd = 0;
3987 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3988 wpa_s->p2p_wps_method, 0, op_freq,
3994 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3995 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3996 " was rejected (status %u)", MAC2STR(sa), status);
4002 wpa_msg_global(wpa_s, MSG_INFO,
4003 P2P_EVENT_INVITATION_RECEIVED
4004 "sa=" MACSTR " go_dev_addr=" MACSTR
4005 " bssid=" MACSTR " unknown-network",
4006 MAC2STR(sa), MAC2STR(go_dev_addr),
4009 wpa_msg_global(wpa_s, MSG_INFO,
4010 P2P_EVENT_INVITATION_RECEIVED
4011 "sa=" MACSTR " go_dev_addr=" MACSTR
4013 MAC2STR(sa), MAC2STR(go_dev_addr));
4018 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
4019 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
4020 "sa=" MACSTR " persistent=%d freq=%d",
4021 MAC2STR(sa), s->id, op_freq);
4023 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
4024 "sa=" MACSTR " persistent=%d",
4025 MAC2STR(sa), s->id);
4030 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
4031 struct wpa_ssid *ssid,
4032 const u8 *peer, int inv)
4039 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
4040 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
4044 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
4045 if (ssid->mode != WPAS_MODE_P2P_GO &&
4046 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
4047 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
4048 "due to invitation result", ssid->id);
4049 wpas_notify_network_removed(wpa_s, ssid);
4050 wpa_config_remove_network(wpa_s->conf, ssid->id);
4053 return; /* Peer not found in client list */
4056 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
4057 "group %d client list%s",
4058 MAC2STR(peer), ssid->id,
4059 inv ? " due to invitation result" : "");
4060 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
4061 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4062 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4063 ssid->num_p2p_clients--;
4064 if (wpa_s->parent->conf->update_config &&
4065 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
4066 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
4070 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
4073 struct wpa_ssid *ssid;
4075 wpa_s = wpa_s->global->p2p_invite_group;
4077 return; /* No known invitation group */
4078 ssid = wpa_s->current_ssid;
4079 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4080 !ssid->p2p_persistent_group)
4081 return; /* Not operating as a GO in persistent group */
4082 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
4083 ssid->ssid, ssid->ssid_len);
4084 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
4088 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
4089 const struct p2p_channels *channels,
4090 const u8 *peer, int neg_freq,
4093 struct wpa_supplicant *wpa_s = ctx;
4094 struct wpa_ssid *ssid;
4098 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
4099 "status=%d " MACSTR,
4100 status, MAC2STR(bssid));
4102 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
4103 "status=%d ", status);
4105 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
4107 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
4108 status, MAC2STR(peer));
4109 if (wpa_s->pending_invite_ssid_id == -1) {
4110 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
4111 wpas_remove_persistent_client(wpa_s, peer);
4112 return; /* Invitation to active group */
4115 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
4116 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
4117 "invitation exchange to indicate readiness for "
4121 if (status != P2P_SC_SUCCESS) {
4122 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
4123 ssid = wpa_config_get_network(
4124 wpa_s->conf, wpa_s->pending_invite_ssid_id);
4125 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
4127 wpas_p2p_remove_pending_group_interface(wpa_s);
4131 ssid = wpa_config_get_network(wpa_s->conf,
4132 wpa_s->pending_invite_ssid_id);
4134 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
4135 "data matching with invitation");
4140 * The peer could have missed our ctrl::ack frame for Invitation
4141 * Response and continue retransmitting the frame. To reduce the
4142 * likelihood of the peer not getting successful TX status for the
4143 * Invitation Response frame, wait a short time here before starting
4144 * the persistent group so that we will remain on the current channel to
4145 * acknowledge any possible retransmission from the peer.
4147 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
4148 "starting persistent group");
4151 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
4152 freq_included(channels, neg_freq))
4154 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
4155 freq_included(channels, peer_oper_freq))
4156 freq = peer_oper_freq;
4160 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
4161 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4162 wpas_p2p_group_add_persistent(wpa_s, ssid,
4163 ssid->mode == WPAS_MODE_P2P_GO,
4164 wpa_s->p2p_persistent_go_freq,
4166 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
4168 ssid->mode == WPAS_MODE_P2P_GO ?
4169 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4174 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
4177 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
4179 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
4183 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
4185 reg->channel[reg->channels] = chan;
4190 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
4191 struct p2p_channels *chan,
4192 struct p2p_channels *cli_chan)
4196 wpa_s->global->p2p_24ghz_social_channels = 1;
4198 os_memset(cli_chan, 0, sizeof(*cli_chan));
4200 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
4203 /* Operating class 81 - 2.4 GHz band channels 1..13 */
4204 chan->reg_class[cla].reg_class = 81;
4205 chan->reg_class[cla].channels = 0;
4206 for (i = 0; i < 11; i++) {
4207 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
4208 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
4210 if (chan->reg_class[cla].channels)
4213 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
4216 /* Operating class 115 - 5 GHz, channels 36-48 */
4217 chan->reg_class[cla].reg_class = 115;
4218 chan->reg_class[cla].channels = 0;
4219 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
4220 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
4221 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
4222 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
4223 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
4224 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
4225 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
4226 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
4227 if (chan->reg_class[cla].channels)
4230 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
4233 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
4234 chan->reg_class[cla].reg_class = 124;
4235 chan->reg_class[cla].channels = 0;
4236 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
4237 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
4238 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
4239 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
4240 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
4241 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
4242 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
4243 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
4244 if (chan->reg_class[cla].channels)
4247 chan->reg_classes = cla;
4252 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
4254 enum hostapd_hw_mode mode)
4258 for (i = 0; i < num_modes; i++) {
4259 if (modes[i].mode == mode)
4268 NOT_ALLOWED, NO_IR, ALLOWED
4271 static int has_channel(struct wpa_global *global,
4272 struct hostapd_hw_modes *mode, u8 chan, int *flags)
4277 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
4279 if (wpas_p2p_disallowed_freq(global, freq))
4282 for (i = 0; i < mode->num_channels; i++) {
4283 if (mode->channels[i].chan == chan) {
4285 *flags = mode->channels[i].flag;
4286 if (mode->channels[i].flag &
4287 (HOSTAPD_CHAN_DISABLED |
4288 HOSTAPD_CHAN_RADAR))
4290 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
4300 struct p2p_oper_class_map {
4301 enum hostapd_hw_mode mode;
4306 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
4309 static struct p2p_oper_class_map op_class[] = {
4310 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
4311 #if 0 /* Do not enable HT40 on 2 GHz for now */
4312 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
4313 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
4315 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
4316 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
4317 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
4318 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
4319 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
4320 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
4323 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
4324 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
4325 * 80 MHz, but currently use the following definition for simplicity
4326 * (these center frequencies are not actual channels, which makes
4327 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
4328 * removing invalid channels.
4330 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
4331 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
4332 { -1, 0, 0, 0, 0, BW20 }
4336 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
4337 struct hostapd_hw_modes *mode,
4340 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
4343 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
4346 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
4348 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
4349 * so the center channel is 6 channels away from the start/end.
4351 if (channel >= center_channels[i] - 6 &&
4352 channel <= center_channels[i] + 6)
4353 return center_channels[i];
4359 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
4360 struct hostapd_hw_modes *mode,
4365 enum chan_allowed res, ret = ALLOWED;
4367 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
4370 if (center_chan >= 58 && center_chan <= 138)
4371 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
4373 /* check all the channels are available */
4374 for (i = 0; i < 4; i++) {
4375 int adj_chan = center_chan - 6 + i * 4;
4377 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
4378 if (res == NOT_ALLOWED)
4383 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
4385 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
4387 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
4389 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
4397 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
4398 struct hostapd_hw_modes *mode,
4402 enum chan_allowed res, res2;
4404 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
4405 if (bw == BW40MINUS) {
4406 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
4408 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
4409 } else if (bw == BW40PLUS) {
4410 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
4412 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
4413 } else if (bw == BW80) {
4414 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
4417 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
4419 if (res == NO_IR || res2 == NO_IR)
4425 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
4426 struct p2p_channels *chan,
4427 struct p2p_channels *cli_chan)
4429 struct hostapd_hw_modes *mode;
4430 int cla, op, cli_cla;
4432 if (wpa_s->hw.modes == NULL) {
4433 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
4434 "of all supported channels; assume dualband "
4436 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
4441 for (op = 0; op_class[op].op_class; op++) {
4442 struct p2p_oper_class_map *o = &op_class[op];
4444 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
4446 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
4449 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4450 wpa_s->global->p2p_24ghz_social_channels = 1;
4451 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4452 enum chan_allowed res;
4453 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
4454 if (res == ALLOWED) {
4456 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4458 reg = &chan->reg_class[cla];
4460 reg->reg_class = o->op_class;
4462 reg->channel[reg->channels] = ch;
4464 } else if (res == NO_IR &&
4465 wpa_s->conf->p2p_add_cli_chan) {
4466 if (cli_reg == NULL) {
4467 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4469 cli_reg = &cli_chan->reg_class[cli_cla];
4471 cli_reg->reg_class = o->op_class;
4473 cli_reg->channel[cli_reg->channels] = ch;
4474 cli_reg->channels++;
4478 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4479 reg->channel, reg->channels);
4482 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4483 cli_reg->channel, cli_reg->channels);
4487 chan->reg_classes = cla;
4488 cli_chan->reg_classes = cli_cla;
4494 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
4495 struct hostapd_hw_modes *mode, u8 channel)
4498 enum chan_allowed ret;
4500 for (op = 0; op_class[op].op_class; op++) {
4501 struct p2p_oper_class_map *o = &op_class[op];
4504 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4505 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
4506 o->bw == BW20 || ch != channel)
4508 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
4510 return (o->bw == BW40MINUS) ? -1 : 1;
4517 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
4518 struct hostapd_hw_modes *mode, u8 channel)
4520 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
4523 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
4527 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4530 struct wpa_supplicant *wpa_s = ctx;
4532 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4533 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4539 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4543 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4544 const u8 *ssid, size_t ssid_len)
4546 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4547 struct wpa_ssid *s = wpa_s->current_ssid;
4550 if (s->mode != WPAS_MODE_P2P_GO &&
4551 s->mode != WPAS_MODE_AP &&
4552 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4554 if (s->ssid_len != ssid_len ||
4555 os_memcmp(ssid, s->ssid, ssid_len) != 0)
4565 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4566 const u8 *peer_dev_addr)
4568 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4569 struct wpa_ssid *ssid = wpa_s->current_ssid;
4572 if (ssid->mode != WPAS_MODE_INFRA)
4574 if (wpa_s->wpa_state != WPA_COMPLETED &&
4575 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
4577 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4585 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4587 struct wpa_supplicant *wpa_s = ctx;
4589 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
4593 static int wpas_is_concurrent_session_active(void *ctx)
4595 struct wpa_supplicant *wpa_s = ctx;
4596 struct wpa_supplicant *ifs;
4598 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4601 if (ifs->wpa_state > WPA_ASSOCIATED)
4608 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4610 struct wpa_supplicant *wpa_s = ctx;
4611 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4615 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4616 const char *conf_p2p_dev)
4618 struct wpa_interface iface;
4619 struct wpa_supplicant *p2pdev_wpa_s;
4621 char force_name[100];
4624 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4626 if (os_snprintf_error(sizeof(ifname), ret))
4628 force_name[0] = '\0';
4629 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
4630 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
4631 force_name, wpa_s->pending_interface_addr, NULL);
4633 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4636 os_strlcpy(wpa_s->pending_interface_name, ifname,
4637 sizeof(wpa_s->pending_interface_name));
4639 os_memset(&iface, 0, sizeof(iface));
4641 iface.ifname = wpa_s->pending_interface_name;
4642 iface.driver = wpa_s->driver->name;
4643 iface.driver_param = wpa_s->conf->driver_param;
4646 * If a P2P Device configuration file was given, use it as the interface
4647 * configuration file (instead of using parent's configuration file.
4650 iface.confname = conf_p2p_dev;
4651 iface.ctrl_interface = NULL;
4653 iface.confname = wpa_s->confname;
4654 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4656 iface.conf_p2p_dev = NULL;
4658 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
4659 if (!p2pdev_wpa_s) {
4660 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4663 p2pdev_wpa_s->parent = wpa_s;
4664 wpa_s->p2p_dev = p2pdev_wpa_s;
4666 wpa_s->pending_interface_name[0] = '\0';
4671 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4672 const u8 *noa, size_t noa_len)
4674 struct wpa_supplicant *wpa_s, *intf = ctx;
4677 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4678 if (wpa_s->waiting_presence_resp)
4682 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4685 wpa_s->waiting_presence_resp = 0;
4687 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4688 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4689 " status=%u noa=%s", MAC2STR(src), status, hex);
4693 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4694 size_t ssid_len, u8 *go_dev_addr,
4695 u8 *ret_ssid, size_t *ret_ssid_len)
4697 struct wpa_supplicant *wpa_s = ctx;
4700 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4702 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4703 *ret_ssid_len = s->ssid_len;
4704 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
4712 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
4713 u8 *ssid, size_t *ssid_len, int *group_iface)
4715 struct wpa_supplicant *wpa_s = ctx;
4719 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
4721 s = wpas_p2p_get_persistent_go(wpa_s);
4723 os_memcpy(bssid, s->bssid, ETH_ALEN);
4726 *group_iface = wpas_p2p_create_iface(wpa_s);
4730 os_memcpy(intended_addr, bssid, ETH_ALEN);
4731 os_memcpy(ssid, s->ssid, s->ssid_len);
4732 *ssid_len = s->ssid_len;
4738 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4739 const u8 *ssid, size_t ssid_len)
4741 struct wpa_supplicant *wpa_s = ctx;
4743 int save_config = 0;
4746 /* Start with our first choice of Persistent Groups */
4747 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4748 if (go && ssid && ssid_len &&
4749 s->ssid_len == ssid_len &&
4750 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4751 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4754 /* Remove stale persistent group */
4755 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
4756 wpa_config_remove_network(wpa_s->conf, s->id);
4761 for (i = 0; i < s->num_p2p_clients; i++) {
4762 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4763 peer, ETH_ALEN) != 0)
4766 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4767 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4768 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4771 s->num_p2p_clients--;
4776 p2p_config_write(wpa_s);
4778 /* Return TRUE if valid SSID remains */
4783 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4784 const u8 *adv_mac, const u8 *ses_mac,
4785 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4786 u8 conncap, int passwd_id,
4787 const u8 *persist_ssid,
4788 size_t persist_ssid_size, int response_done,
4789 int prov_start, const char *session_info)
4791 struct wpa_supplicant *wpa_s = ctx;
4793 struct wpa_ssid *persistent_go, *stale, *s;
4794 int save_config = 0;
4795 struct wpa_supplicant *go_wpa_s;
4800 os_memset(mac, 0, ETH_ALEN);
4809 if (session_info == NULL) {
4810 wpa_msg_global(wpa_s, MSG_INFO,
4811 P2P_EVENT_P2PS_PROVISION_START MACSTR
4812 " adv_id=%x conncap=%x"
4814 " session=%x mac=" MACSTR
4815 " dev_passwd_id=%d",
4816 MAC2STR(dev), adv_id, conncap,
4818 ses_id, MAC2STR(ses_mac),
4821 wpa_msg_global(wpa_s, MSG_INFO,
4822 P2P_EVENT_P2PS_PROVISION_START MACSTR
4823 " adv_id=%x conncap=%x"
4825 " session=%x mac=" MACSTR
4826 " dev_passwd_id=%d info='%s'",
4827 MAC2STR(dev), adv_id, conncap,
4829 ses_id, MAC2STR(ses_mac),
4830 passwd_id, session_info);
4835 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4836 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4838 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4839 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4840 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4842 if (persistent_go && !persistent_go->num_p2p_clients) {
4843 /* remove empty persistent GO */
4844 wpa_config_remove_network(wpa_s->conf,
4848 wpa_msg_global(wpa_s, MSG_INFO,
4849 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4851 " adv_id=%x adv_mac=" MACSTR
4852 " session=%x mac=" MACSTR,
4853 MAC2STR(dev), status,
4854 adv_id, MAC2STR(adv_mac),
4855 ses_id, MAC2STR(ses_mac));
4859 /* Clean up stale persistent groups with this device */
4860 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4863 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4867 if (s && s->ssid_len == stale->ssid_len &&
4868 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4869 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4872 /* Remove stale persistent group */
4873 if (stale->mode != WPAS_MODE_P2P_GO ||
4874 stale->num_p2p_clients <= 1) {
4875 wpa_config_remove_network(wpa_s->conf, stale->id);
4879 for (i = 0; i < stale->num_p2p_clients; i++) {
4880 if (os_memcmp(stale->p2p_client_list +
4882 dev, ETH_ALEN) == 0) {
4883 os_memmove(stale->p2p_client_list +
4885 stale->p2p_client_list +
4887 (stale->num_p2p_clients -
4892 stale->num_p2p_clients--;
4898 p2p_config_write(wpa_s);
4901 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4902 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4904 if (persistent_go && s != persistent_go &&
4905 !persistent_go->num_p2p_clients) {
4906 /* remove empty persistent GO */
4907 wpa_config_remove_network(wpa_s->conf,
4912 wpa_msg_global(wpa_s, MSG_INFO,
4913 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4915 " adv_id=%x adv_mac=" MACSTR
4916 " session=%x mac=" MACSTR
4918 MAC2STR(dev), status,
4919 adv_id, MAC2STR(adv_mac),
4920 ses_id, MAC2STR(ses_mac), s->id);
4924 if (conncap == P2PS_SETUP_GROUP_OWNER) {
4925 const char *go_ifname = NULL;
4927 wpa_s->global->pending_p2ps_group = 1;
4929 if (wpa_s->conf->p2p_no_group_iface)
4930 go_ifname = wpa_s->ifname;
4931 else if (wpa_s->pending_interface_name[0])
4932 go_ifname = wpa_s->pending_interface_name;
4935 wpas_p2ps_prov_complete(
4936 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4937 dev, adv_mac, ses_mac,
4938 NULL, adv_id, ses_id, 0, 0,
4939 NULL, 0, 0, 0, NULL);
4943 /* If PD Resp complete, start up the GO */
4944 if (response_done && persistent_go) {
4945 wpas_p2p_group_add_persistent(
4946 wpa_s, persistent_go,
4947 0, 0, 0, 0, 0, NULL,
4948 persistent_go->mode ==
4950 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4952 } else if (response_done) {
4953 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
4956 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4957 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
4958 wpa_s->p2ps_join_addr_valid = 1;
4959 wpa_dbg(wpa_s, MSG_DEBUG,
4960 "P2PS: Saving PIN for " MACSTR,
4963 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4964 go_ifname = go_wpa_s->ifname;
4966 wpa_dbg(go_wpa_s, MSG_DEBUG,
4967 "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
4968 wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
4971 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
4972 wpa_s->p2ps_join_addr_valid = 1;
4973 wpa_dbg(wpa_s, MSG_DEBUG,
4974 "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
4977 wpa_msg_global(wpa_s, MSG_INFO,
4978 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4979 " status=%d conncap=%x"
4980 " adv_id=%x adv_mac=" MACSTR
4981 " session=%x mac=" MACSTR
4982 " dev_passwd_id=%d go=%s",
4983 MAC2STR(dev), status, conncap,
4984 adv_id, MAC2STR(adv_mac),
4985 ses_id, MAC2STR(ses_mac),
4986 passwd_id, go_ifname);
4990 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4991 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4993 if (persistent_go && !persistent_go->num_p2p_clients) {
4994 /* remove empty persistent GO */
4995 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4998 if (conncap == P2PS_SETUP_CLIENT) {
4999 wpa_msg_global(wpa_s, MSG_INFO,
5000 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5001 " status=%d conncap=%x"
5002 " adv_id=%x adv_mac=" MACSTR
5003 " session=%x mac=" MACSTR
5004 " dev_passwd_id=%d join=" MACSTR,
5005 MAC2STR(dev), status, conncap,
5006 adv_id, MAC2STR(adv_mac),
5007 ses_id, MAC2STR(ses_mac),
5008 passwd_id, MAC2STR(grp_mac));
5010 wpa_msg_global(wpa_s, MSG_INFO,
5011 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5012 " status=%d conncap=%x"
5013 " adv_id=%x adv_mac=" MACSTR
5014 " session=%x mac=" MACSTR
5015 " dev_passwd_id=%d",
5016 MAC2STR(dev), status, conncap,
5017 adv_id, MAC2STR(adv_mac),
5018 ses_id, MAC2STR(ses_mac),
5024 static int _wpas_p2p_in_progress(void *ctx)
5026 struct wpa_supplicant *wpa_s = ctx;
5027 return wpas_p2p_in_progress(wpa_s);
5031 static int wpas_prov_disc_resp_cb(void *ctx)
5033 struct wpa_supplicant *wpa_s = ctx;
5034 struct wpa_ssid *persistent_go;
5036 if (!wpa_s->global->pending_p2ps_group)
5039 wpa_s->global->pending_p2ps_group = 0;
5041 if (wpas_p2p_get_go_group(wpa_s))
5043 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
5045 if (persistent_go) {
5046 wpas_p2p_group_add_persistent(
5047 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
5048 persistent_go->mode == WPAS_MODE_P2P_GO ?
5049 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
5051 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
5059 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
5060 * @global: Pointer to global data from wpa_supplicant_init()
5061 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5062 * Returns: 0 on success, -1 on failure
5064 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
5066 struct p2p_config p2p;
5069 if (wpa_s->conf->p2p_disabled)
5072 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5078 os_memset(&p2p, 0, sizeof(p2p));
5080 p2p.debug_print = wpas_p2p_debug_print;
5081 p2p.p2p_scan = wpas_p2p_scan;
5082 p2p.send_action = wpas_send_action;
5083 p2p.send_action_done = wpas_send_action_done;
5084 p2p.go_neg_completed = wpas_go_neg_completed;
5085 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
5086 p2p.dev_found = wpas_dev_found;
5087 p2p.dev_lost = wpas_dev_lost;
5088 p2p.find_stopped = wpas_find_stopped;
5089 p2p.start_listen = wpas_start_listen;
5090 p2p.stop_listen = wpas_stop_listen;
5091 p2p.send_probe_resp = wpas_send_probe_resp;
5092 p2p.sd_request = wpas_sd_request;
5093 p2p.sd_response = wpas_sd_response;
5094 p2p.prov_disc_req = wpas_prov_disc_req;
5095 p2p.prov_disc_resp = wpas_prov_disc_resp;
5096 p2p.prov_disc_fail = wpas_prov_disc_fail;
5097 p2p.invitation_process = wpas_invitation_process;
5098 p2p.invitation_received = wpas_invitation_received;
5099 p2p.invitation_result = wpas_invitation_result;
5100 p2p.get_noa = wpas_get_noa;
5101 p2p.go_connected = wpas_go_connected;
5102 p2p.presence_resp = wpas_presence_resp;
5103 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
5104 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
5105 p2p.get_persistent_group = wpas_get_persistent_group;
5106 p2p.get_go_info = wpas_get_go_info;
5107 p2p.remove_stale_groups = wpas_remove_stale_groups;
5108 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
5109 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
5110 p2p.p2ps_group_capability = p2ps_group_capability;
5112 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
5113 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
5114 p2p.dev_name = wpa_s->conf->device_name;
5115 p2p.manufacturer = wpa_s->conf->manufacturer;
5116 p2p.model_name = wpa_s->conf->model_name;
5117 p2p.model_number = wpa_s->conf->model_number;
5118 p2p.serial_number = wpa_s->conf->serial_number;
5120 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
5121 p2p.config_methods = wpa_s->wps->config_methods;
5124 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
5125 wpa_printf(MSG_ERROR,
5126 "P2P: Failed to configure supported channel list");
5130 if (wpa_s->conf->p2p_listen_reg_class &&
5131 wpa_s->conf->p2p_listen_channel) {
5132 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5133 p2p.channel = wpa_s->conf->p2p_listen_channel;
5134 p2p.channel_forced = 1;
5137 * Pick one of the social channels randomly as the listen
5140 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
5141 &p2p.channel) != 0) {
5142 wpa_printf(MSG_ERROR,
5143 "P2P: Failed to select random social channel as listen channel");
5146 p2p.channel_forced = 0;
5148 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5149 p2p.reg_class, p2p.channel);
5151 if (wpa_s->conf->p2p_oper_reg_class &&
5152 wpa_s->conf->p2p_oper_channel) {
5153 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5154 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5155 p2p.cfg_op_channel = 1;
5156 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5157 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5161 * Use random operation channel from 2.4 GHz band social
5162 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5163 * other preference is indicated.
5165 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
5166 &p2p.op_channel) != 0) {
5167 wpa_printf(MSG_ERROR,
5168 "P2P: Failed to select random social channel as operation channel");
5171 p2p.cfg_op_channel = 0;
5172 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5173 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5176 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5177 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5178 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5181 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5182 os_memcpy(p2p.country, wpa_s->conf->country, 2);
5183 p2p.country[2] = 0x04;
5185 os_memcpy(p2p.country, "XX\x04", 3);
5187 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5190 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5191 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5192 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5194 p2p.concurrent_operations = !!(wpa_s->drv_flags &
5195 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5197 p2p.max_peers = 100;
5199 if (wpa_s->conf->p2p_ssid_postfix) {
5200 p2p.ssid_postfix_len =
5201 os_strlen(wpa_s->conf->p2p_ssid_postfix);
5202 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5203 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5204 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5205 p2p.ssid_postfix_len);
5208 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5210 p2p.max_listen = wpa_s->max_remain_on_chan;
5212 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5213 wpa_s->conf->p2p_passphrase_len <= 63)
5214 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5216 p2p.passphrase_len = 8;
5218 global->p2p = p2p_init(&p2p);
5219 if (global->p2p == NULL)
5221 global->p2p_init_wpa_s = wpa_s;
5223 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5224 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5226 p2p_add_wps_vendor_extension(
5227 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5230 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5237 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5238 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5240 * This function deinitialize per-interface P2P data.
5242 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5244 if (wpa_s->driver && wpa_s->drv_priv)
5245 wpa_drv_probe_req_report(wpa_s, 0);
5247 if (wpa_s->go_params) {
5248 /* Clear any stored provisioning info */
5249 p2p_clear_provisioning_info(
5251 wpa_s->go_params->peer_device_addr);
5254 os_free(wpa_s->go_params);
5255 wpa_s->go_params = NULL;
5256 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
5257 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5258 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5259 wpa_s->p2p_long_listen = 0;
5260 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5261 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5262 wpas_p2p_remove_pending_group_interface(wpa_s);
5263 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
5264 wpas_p2p_listen_work_done(wpa_s);
5265 if (wpa_s->p2p_send_action_work) {
5266 os_free(wpa_s->p2p_send_action_work->ctx);
5267 radio_work_done(wpa_s->p2p_send_action_work);
5268 wpa_s->p2p_send_action_work = NULL;
5270 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
5272 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5273 wpa_s->p2p_oob_dev_pw = NULL;
5275 os_free(wpa_s->p2p_group_common_freqs);
5276 wpa_s->p2p_group_common_freqs = NULL;
5277 wpa_s->p2p_group_common_freqs_num = 0;
5279 /* TODO: remove group interface from the driver if this wpa_s instance
5280 * is on top of a P2P group interface */
5285 * wpas_p2p_deinit_global - Deinitialize global P2P module
5286 * @global: Pointer to global data from wpa_supplicant_init()
5288 * This function deinitializes the global (per device) P2P module.
5290 static void wpas_p2p_deinit_global(struct wpa_global *global)
5292 struct wpa_supplicant *wpa_s, *tmp;
5294 wpa_s = global->ifaces;
5296 wpas_p2p_service_flush(global->p2p_init_wpa_s);
5298 /* Remove remaining P2P group interfaces */
5299 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5300 wpa_s = wpa_s->next;
5302 tmp = global->ifaces;
5305 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5310 /* Disconnect from the P2P group and deinit the interface */
5311 wpas_p2p_disconnect(tmp);
5315 * Deinit GO data on any possibly remaining interface (if main
5316 * interface is used as GO).
5318 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5319 if (wpa_s->ap_iface)
5320 wpas_p2p_group_deinit(wpa_s);
5323 p2p_deinit(global->p2p);
5325 global->p2p_init_wpa_s = NULL;
5329 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5331 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
5332 wpa_s->conf->p2p_no_group_iface)
5333 return 0; /* separate interface disabled per configuration */
5334 if (wpa_s->drv_flags &
5335 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5336 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5337 return 1; /* P2P group requires a new interface in every case
5339 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5340 return 0; /* driver does not support concurrent operations */
5341 if (wpa_s->global->ifaces->next)
5342 return 1; /* more that one interface already in use */
5343 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5344 return 1; /* this interface is already in use */
5349 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5350 const u8 *peer_addr,
5351 enum p2p_wps_method wps_method,
5352 int go_intent, const u8 *own_interface_addr,
5353 unsigned int force_freq, int persistent_group,
5354 struct wpa_ssid *ssid, unsigned int pref_freq)
5356 if (persistent_group && wpa_s->conf->persistent_reconnect)
5357 persistent_group = 2;
5360 * Increase GO config timeout if HT40 is used since it takes some time
5361 * to scan channels for coex purposes before the BSS can be started.
5363 p2p_set_config_timeout(wpa_s->global->p2p,
5364 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5366 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5367 go_intent, own_interface_addr, force_freq,
5368 persistent_group, ssid ? ssid->ssid : NULL,
5369 ssid ? ssid->ssid_len : 0,
5370 wpa_s->p2p_pd_before_go_neg, pref_freq,
5371 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5376 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5377 const u8 *peer_addr,
5378 enum p2p_wps_method wps_method,
5379 int go_intent, const u8 *own_interface_addr,
5380 unsigned int force_freq, int persistent_group,
5381 struct wpa_ssid *ssid, unsigned int pref_freq)
5383 if (persistent_group && wpa_s->conf->persistent_reconnect)
5384 persistent_group = 2;
5386 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5387 go_intent, own_interface_addr, force_freq,
5388 persistent_group, ssid ? ssid->ssid : NULL,
5389 ssid ? ssid->ssid_len : 0, pref_freq,
5390 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5395 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5397 wpa_s->p2p_join_scan_count++;
5398 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5399 wpa_s->p2p_join_scan_count);
5400 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5401 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5402 " for join operationg - stop join attempt",
5403 MAC2STR(wpa_s->pending_join_iface_addr));
5404 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5405 if (wpa_s->p2p_auto_pd) {
5406 wpa_s->p2p_auto_pd = 0;
5407 wpa_msg_global(wpa_s, MSG_INFO,
5408 P2P_EVENT_PROV_DISC_FAILURE
5409 " p2p_dev_addr=" MACSTR " status=N/A",
5410 MAC2STR(wpa_s->pending_join_dev_addr));
5413 wpa_msg_global(wpa_s->parent, MSG_INFO,
5414 P2P_EVENT_GROUP_FORMATION_FAILURE);
5419 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5422 unsigned int num, i;
5423 struct wpa_used_freq_data *freqs;
5425 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5426 /* Multiple channels are supported and not all are in use */
5430 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5431 sizeof(struct wpa_used_freq_data));
5435 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5436 wpa_s->num_multichan_concurrent);
5438 for (i = 0; i < num; i++) {
5439 if (freqs[i].freq == freq) {
5440 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5447 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
5456 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5457 const u8 *peer_dev_addr)
5459 struct wpa_bss *bss;
5462 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5465 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5466 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5471 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5473 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5474 "%ld.%06ld (%supdated in last scan)",
5475 bss->last_update.sec, bss->last_update.usec,
5476 updated ? "": "not ");
5482 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5483 struct wpa_scan_results *scan_res)
5485 struct wpa_bss *bss = NULL;
5487 u8 iface_addr[ETH_ALEN];
5489 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5491 if (wpa_s->global->p2p_disabled)
5494 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5495 scan_res ? (int) scan_res->num : -1,
5496 wpa_s->p2p_auto_join ? "auto_" : "");
5499 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5501 if (wpa_s->p2p_auto_pd) {
5502 int join = wpas_p2p_peer_go(wpa_s,
5503 wpa_s->pending_join_dev_addr);
5505 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5506 wpa_s->auto_pd_scan_retry++;
5507 bss = wpa_bss_get_bssid_latest(
5508 wpa_s, wpa_s->pending_join_dev_addr);
5511 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5512 "the peer " MACSTR " at %d MHz",
5513 wpa_s->auto_pd_scan_retry,
5515 pending_join_dev_addr),
5517 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
5525 wpa_s->p2p_auto_pd = 0;
5526 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5527 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5528 MAC2STR(wpa_s->pending_join_dev_addr), join);
5529 if (p2p_prov_disc_req(wpa_s->global->p2p,
5530 wpa_s->pending_join_dev_addr, NULL,
5531 wpa_s->pending_pd_config_methods, join,
5532 0, wpa_s->user_initiated_pd) < 0) {
5533 wpa_s->p2p_auto_pd = 0;
5534 wpa_msg_global(wpa_s, MSG_INFO,
5535 P2P_EVENT_PROV_DISC_FAILURE
5536 " p2p_dev_addr=" MACSTR " status=N/A",
5537 MAC2STR(wpa_s->pending_join_dev_addr));
5542 if (wpa_s->p2p_auto_join) {
5543 int join = wpas_p2p_peer_go(wpa_s,
5544 wpa_s->pending_join_dev_addr);
5546 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5547 "running a GO -> use GO Negotiation");
5548 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5549 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5550 wpa_s->p2p_persistent_group, 0, 0, 0,
5551 wpa_s->p2p_go_intent,
5552 wpa_s->p2p_connect_freq,
5553 wpa_s->p2p_persistent_id,
5554 wpa_s->p2p_pd_before_go_neg,
5560 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5561 "try to join the group", join ? "" :
5564 wpa_s->p2p_fallback_to_go_neg = 1;
5567 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5568 wpa_s->pending_join_iface_addr);
5570 p2p_get_interface_addr(wpa_s->global->p2p,
5571 wpa_s->pending_join_dev_addr,
5573 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5574 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
5575 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5576 "address for join from " MACSTR " to " MACSTR
5577 " based on newly discovered P2P peer entry",
5578 MAC2STR(wpa_s->pending_join_iface_addr),
5579 MAC2STR(iface_addr));
5580 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5583 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5584 wpa_s->pending_join_iface_addr);
5587 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5588 "from P2P peer table: %d MHz", freq);
5590 if (wpa_s->p2p_join_ssid_len) {
5591 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5592 MACSTR " and SSID %s",
5593 MAC2STR(wpa_s->pending_join_iface_addr),
5594 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5595 wpa_s->p2p_join_ssid_len));
5596 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5597 wpa_s->p2p_join_ssid,
5598 wpa_s->p2p_join_ssid_len);
5601 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5602 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5603 bss = wpa_bss_get_bssid_latest(wpa_s,
5604 wpa_s->pending_join_iface_addr);
5608 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5609 "from BSS table: %d MHz (SSID %s)", freq,
5610 wpa_ssid_txt(bss->ssid, bss->ssid_len));
5615 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
5616 wpa_msg_global(wpa_s->parent, MSG_INFO,
5617 P2P_EVENT_GROUP_FORMATION_FAILURE
5618 "reason=FREQ_CONFLICT");
5622 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5623 "prior to joining an existing group (GO " MACSTR
5625 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5626 wpa_s->pending_pd_before_join = 1;
5628 switch (wpa_s->pending_join_wps_method) {
5629 case WPS_PIN_DISPLAY:
5630 method = WPS_CONFIG_KEYPAD;
5632 case WPS_PIN_KEYPAD:
5633 method = WPS_CONFIG_DISPLAY;
5636 method = WPS_CONFIG_PUSHBUTTON;
5643 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5644 wpa_s->pending_join_dev_addr) ==
5647 * We have already performed provision discovery for
5648 * joining the group. Proceed directly to join
5649 * operation without duplicated provision discovery. */
5650 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5651 "with " MACSTR " already done - proceed to "
5653 MAC2STR(wpa_s->pending_join_dev_addr));
5654 wpa_s->pending_pd_before_join = 0;
5658 if (p2p_prov_disc_req(wpa_s->global->p2p,
5659 wpa_s->pending_join_dev_addr,
5661 freq, wpa_s->user_initiated_pd) < 0) {
5662 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5663 "Discovery Request before joining an "
5665 wpa_s->pending_pd_before_join = 0;
5671 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5672 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5673 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5674 wpas_p2p_check_join_scan_limit(wpa_s);
5678 /* Start join operation immediately */
5679 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
5683 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5684 const u8 *ssid, size_t ssid_len)
5687 struct wpa_driver_scan_params params;
5688 struct wpabuf *wps_ie, *ies;
5690 int freqs[2] = { 0, 0 };
5692 os_memset(¶ms, 0, sizeof(params));
5694 /* P2P Wildcard SSID */
5695 params.num_ssids = 1;
5696 if (ssid && ssid_len) {
5697 params.ssids[0].ssid = ssid;
5698 params.ssids[0].ssid_len = ssid_len;
5699 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5700 wpa_s->p2p_join_ssid_len = ssid_len;
5702 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5703 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5704 wpa_s->p2p_join_ssid_len = 0;
5707 wpa_s->wps->dev.p2p = 1;
5708 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5709 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5711 if (wps_ie == NULL) {
5712 wpas_p2p_scan_res_join(wpa_s, NULL);
5716 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5717 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5719 wpabuf_free(wps_ie);
5720 wpas_p2p_scan_res_join(wpa_s, NULL);
5723 wpabuf_put_buf(ies, wps_ie);
5724 wpabuf_free(wps_ie);
5726 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5728 params.p2p_probe = 1;
5729 params.extra_ies = wpabuf_head(ies);
5730 params.extra_ies_len = wpabuf_len(ies);
5735 * If freq is not provided, check the operating freq of the GO
5736 * and use a single channel scan on if possible.
5738 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5739 wpa_s->pending_join_iface_addr);
5745 params.freqs = freqs;
5749 * Run a scan to update BSS table and start Provision Discovery once
5750 * the new scan results become available.
5752 ret = wpa_drv_scan(wpa_s, ¶ms);
5754 os_get_reltime(&wpa_s->scan_trigger_time);
5755 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
5756 wpa_s->own_scan_requested = 1;
5762 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5764 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5765 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5766 wpas_p2p_check_join_scan_limit(wpa_s);
5771 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5773 struct wpa_supplicant *wpa_s = eloop_ctx;
5774 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
5778 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
5779 const u8 *dev_addr, enum p2p_wps_method wps_method,
5780 int auto_join, int op_freq,
5781 const u8 *ssid, size_t ssid_len)
5783 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
5784 MACSTR " dev " MACSTR " op_freq=%d)%s",
5785 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
5786 auto_join ? " (auto_join)" : "");
5787 if (ssid && ssid_len) {
5788 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5789 wpa_ssid_txt(ssid, ssid_len));
5792 wpa_s->p2p_auto_pd = 0;
5793 wpa_s->p2p_auto_join = !!auto_join;
5794 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5795 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5796 wpa_s->pending_join_wps_method = wps_method;
5798 /* Make sure we are not running find during connection establishment */
5799 wpas_p2p_stop_find(wpa_s);
5801 wpa_s->p2p_join_scan_count = 0;
5802 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
5807 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5808 const u8 *ssid, size_t ssid_len)
5810 struct wpa_supplicant *group;
5811 struct p2p_go_neg_results res;
5812 struct wpa_bss *bss;
5814 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5817 if (group != wpa_s) {
5818 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5819 sizeof(group->p2p_pin));
5820 group->p2p_wps_method = wpa_s->p2p_wps_method;
5823 * Need to mark the current interface for p2p_group_formation
5824 * when a separate group interface is not used. This is needed
5825 * to allow p2p_cancel stop a pending p2p_connect-join.
5826 * wpas_p2p_init_group_interface() addresses this for the case
5827 * where a separate group interface is used.
5829 wpa_s->global->p2p_group_formation = wpa_s;
5832 group->p2p_in_provisioning = 1;
5833 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
5835 os_memset(&res, 0, sizeof(res));
5836 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
5837 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5839 res.wps_method = wpa_s->pending_join_wps_method;
5840 if (freq && ssid && ssid_len) {
5842 res.ssid_len = ssid_len;
5843 os_memcpy(res.ssid, ssid, ssid_len);
5845 bss = wpa_bss_get_bssid_latest(wpa_s,
5846 wpa_s->pending_join_iface_addr);
5848 res.freq = bss->freq;
5849 res.ssid_len = bss->ssid_len;
5850 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5851 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5853 wpa_ssid_txt(bss->ssid, bss->ssid_len));
5857 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5858 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5860 wpa_drv_cancel_remain_on_channel(wpa_s);
5861 wpa_s->off_channel_freq = 0;
5862 wpa_s->roc_waiting_drv_freq = 0;
5864 wpas_start_wps_enrollee(group, &res);
5867 * Allow a longer timeout for join-a-running-group than normal 15
5868 * second group formation timeout since the GO may not have authorized
5869 * our connection yet.
5871 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5872 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5879 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
5880 int *force_freq, int *pref_freq, int go)
5882 struct wpa_used_freq_data *freqs;
5883 int res, best_freq, num_unused;
5884 unsigned int freq_in_use = 0, num, i;
5886 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5887 sizeof(struct wpa_used_freq_data));
5891 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5892 wpa_s->num_multichan_concurrent);
5895 * It is possible that the total number of used frequencies is bigger
5896 * than the number of frequencies used for P2P, so get the system wide
5897 * number of unused frequencies.
5899 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5901 wpa_printf(MSG_DEBUG,
5902 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5903 freq, wpa_s->num_multichan_concurrent, num, num_unused);
5908 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5910 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5912 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
5913 "(%u MHz) is not supported for P2P uses",
5919 for (i = 0; i < num; i++) {
5920 if (freqs[i].freq == freq)
5924 if (num_unused <= 0 && !freq_in_use) {
5925 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5930 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5931 "requested channel (%u MHz)", freq);
5936 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5938 /* We have a candidate frequency to use */
5939 if (best_freq > 0) {
5940 if (*pref_freq == 0 && num_unused > 0) {
5941 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
5943 *pref_freq = best_freq;
5945 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
5947 *force_freq = best_freq;
5949 } else if (num_unused > 0) {
5950 wpa_printf(MSG_DEBUG,
5951 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5954 wpa_printf(MSG_DEBUG,
5955 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5969 * wpas_p2p_connect - Request P2P Group Formation to be started
5970 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5971 * @peer_addr: Address of the peer P2P Device
5972 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5973 * @persistent_group: Whether to create a persistent group
5974 * @auto_join: Whether to select join vs. GO Negotiation automatically
5975 * @join: Whether to join an existing group (as a client) instead of starting
5976 * Group Owner negotiation; @peer_addr is BSSID in that case
5977 * @auth: Whether to only authorize the connection instead of doing that and
5978 * initiating Group Owner negotiation
5979 * @go_intent: GO Intent or -1 to use default
5980 * @freq: Frequency for the group or 0 for auto-selection
5981 * @persistent_id: Persistent group credentials to use for forcing GO
5982 * parameters or -1 to generate new values (SSID/passphrase)
5983 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5984 * interoperability workaround when initiating group formation
5985 * @ht40: Start GO with 40 MHz channel width
5986 * @vht: Start GO with VHT support
5987 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5988 * failure, -2 on failure due to channel not currently available,
5989 * -3 if forced channel is not supported
5991 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5992 const char *pin, enum p2p_wps_method wps_method,
5993 int persistent_group, int auto_join, int join, int auth,
5994 int go_intent, int freq, int persistent_id, int pd,
5997 int force_freq = 0, pref_freq = 0;
5999 enum wpa_driver_if_type iftype;
6001 struct wpa_ssid *ssid = NULL;
6003 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6006 if (persistent_id >= 0) {
6007 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6008 if (ssid == NULL || ssid->disabled != 2 ||
6009 ssid->mode != WPAS_MODE_P2P_GO)
6013 os_free(wpa_s->global->add_psk);
6014 wpa_s->global->add_psk = NULL;
6016 wpa_s->global->p2p_fail_on_wps_complete = 0;
6017 wpa_s->global->pending_p2ps_group = 0;
6020 go_intent = wpa_s->conf->p2p_go_intent;
6023 wpa_s->p2p_long_listen = 0;
6025 wpa_s->p2p_wps_method = wps_method;
6026 wpa_s->p2p_persistent_group = !!persistent_group;
6027 wpa_s->p2p_persistent_id = persistent_id;
6028 wpa_s->p2p_go_intent = go_intent;
6029 wpa_s->p2p_connect_freq = freq;
6030 wpa_s->p2p_fallback_to_go_neg = 0;
6031 wpa_s->p2p_pd_before_go_neg = !!pd;
6032 wpa_s->p2p_go_ht40 = !!ht40;
6033 wpa_s->p2p_go_vht = !!vht;
6036 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6037 else if (wps_method == WPS_PIN_DISPLAY) {
6038 ret = wps_generate_pin();
6039 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6041 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6042 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
6043 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6046 wpa_s->p2p_pin[0] = '\0';
6048 if (join || auto_join) {
6049 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6051 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6052 "connect a running group from " MACSTR,
6053 MAC2STR(peer_addr));
6054 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6057 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6058 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6060 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6061 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6065 os_get_reltime(&wpa_s->p2p_auto_started);
6066 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6068 wpa_s->p2p_auto_started.sec,
6069 wpa_s->p2p_auto_started.usec);
6071 wpa_s->user_initiated_pd = 1;
6072 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
6073 auto_join, freq, NULL, 0) < 0)
6078 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6082 wpas_p2p_set_own_freq_preference(wpa_s,
6083 force_freq ? force_freq : pref_freq);
6085 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6087 if (wpa_s->create_p2p_iface) {
6088 /* Prepare to add a new interface for the group */
6089 iftype = WPA_IF_P2P_GROUP;
6090 if (go_intent == 15)
6091 iftype = WPA_IF_P2P_GO;
6092 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6093 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6094 "interface for the group");
6098 if_addr = wpa_s->pending_interface_addr;
6100 if_addr = wpa_s->own_addr;
6103 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
6105 force_freq, persistent_group, ssid,
6111 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6112 go_intent, if_addr, force_freq,
6113 persistent_group, ssid, pref_freq) < 0) {
6114 if (wpa_s->create_p2p_iface)
6115 wpas_p2p_remove_pending_group_interface(wpa_s);
6123 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6124 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6125 * @freq: Frequency of the channel in MHz
6126 * @duration: Duration of the stay on the channel in milliseconds
6128 * This callback is called when the driver indicates that it has started the
6129 * requested remain-on-channel duration.
6131 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6132 unsigned int freq, unsigned int duration)
6134 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6136 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)",
6137 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6138 wpa_s->roc_waiting_drv_freq, freq, duration);
6139 if (wpa_s->off_channel_freq &&
6140 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
6141 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6142 wpa_s->pending_listen_duration);
6143 wpa_s->pending_listen_freq = 0;
6145 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6146 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6152 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
6154 /* Limit maximum Listen state time based on driver limitation. */
6155 if (timeout > wpa_s->max_remain_on_chan)
6156 timeout = wpa_s->max_remain_on_chan;
6158 return p2p_listen(wpa_s->global->p2p, timeout);
6163 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6164 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6165 * @freq: Frequency of the channel in MHz
6167 * This callback is called when the driver indicates that a remain-on-channel
6168 * operation has been completed, i.e., the duration on the requested channel
6171 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6174 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6175 "(p2p_long_listen=%d ms pending_action_tx=%p)",
6176 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
6177 wpas_p2p_listen_work_done(wpa_s);
6178 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6180 if (wpa_s->p2p_long_listen > 0)
6181 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
6182 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6183 return; /* P2P module started a new operation */
6184 if (offchannel_pending_action_tx(wpa_s))
6186 if (wpa_s->p2p_long_listen > 0) {
6187 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
6188 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
6191 * When listen duration is over, stop listen & update p2p_state
6194 p2p_stop_listen(wpa_s->global->p2p);
6200 * wpas_p2p_group_remove - Remove a P2P group
6201 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6202 * @ifname: Network interface name of the group interface or "*" to remove all
6204 * Returns: 0 on success, -1 on failure
6206 * This function is used to remove a P2P group. This can be used to disconnect
6207 * from a group in which the local end is a P2P Client or to end a P2P Group in
6208 * case the local end is the Group Owner. If a virtual network interface was
6209 * created for this group, that interface will be removed. Otherwise, only the
6210 * configured P2P group network will be removed from the interface.
6212 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6214 struct wpa_global *global = wpa_s->global;
6215 struct wpa_supplicant *calling_wpa_s = wpa_s;
6217 if (os_strcmp(ifname, "*") == 0) {
6218 struct wpa_supplicant *prev;
6219 wpa_s = global->ifaces;
6222 wpa_s = wpa_s->next;
6223 if (prev->p2p_group_interface !=
6224 NOT_P2P_GROUP_INTERFACE ||
6225 (prev->current_ssid &&
6226 prev->current_ssid->p2p_group))
6227 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
6232 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6233 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6237 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
6241 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6246 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6248 if (wpa_s->best_24_freq > 0 &&
6249 p2p_supported_freq_go(wpa_s->global->p2p,
6250 wpa_s->best_24_freq)) {
6251 freq = wpa_s->best_24_freq;
6252 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6253 "channel: %d MHz", freq);
6255 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6257 freq = 2412 + (r % 3) * 25;
6258 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6259 "channel: %d MHz", freq);
6264 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6266 if (wpa_s->best_5_freq > 0 &&
6267 p2p_supported_freq_go(wpa_s->global->p2p,
6268 wpa_s->best_5_freq)) {
6269 freq = wpa_s->best_5_freq;
6270 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6271 "channel: %d MHz", freq);
6273 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6275 freq = 5180 + (r % 4) * 20;
6276 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6277 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6278 "5 GHz channel for P2P group");
6281 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6282 "channel: %d MHz", freq);
6286 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6287 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6288 "(%u MHz) is not supported for P2P uses",
6297 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
6298 struct p2p_go_neg_results *params,
6299 const struct p2p_channels *channels)
6303 /* first try some random selection of the social channels */
6304 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6307 for (i = 0; i < 3; i++) {
6308 params->freq = 2412 + ((r + i) % 3) * 25;
6309 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6310 freq_included(channels, params->freq) &&
6311 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6315 /* try all channels in reg. class 81 */
6316 for (i = 0; i < 11; i++) {
6317 params->freq = 2412 + i * 5;
6318 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6319 freq_included(channels, params->freq) &&
6320 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6324 /* try social channel class 180 channel 2 */
6325 params->freq = 58320 + 1 * 2160;
6326 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6327 freq_included(channels, params->freq) &&
6328 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6331 /* try all channels in reg. class 180 */
6332 for (i = 0; i < 4; i++) {
6333 params->freq = 58320 + i * 2160;
6334 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6335 freq_included(channels, params->freq) &&
6336 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6340 wpa_printf(MSG_DEBUG, "P2P: No 2.4 and 60 GHz channel allowed");
6343 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6349 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6350 struct p2p_go_neg_results *params,
6351 int freq, int ht40, int vht,
6352 const struct p2p_channels *channels)
6354 struct wpa_used_freq_data *freqs;
6355 unsigned int pref_freq, cand_freq;
6356 unsigned int num, i;
6358 os_memset(params, 0, sizeof(*params));
6359 params->role_go = 1;
6360 params->ht40 = ht40;
6363 if (!freq_included(channels, freq)) {
6364 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
6368 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
6369 "frequency %d MHz", freq);
6370 params->freq = freq;
6371 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6372 wpa_s->conf->p2p_oper_channel >= 1 &&
6373 wpa_s->conf->p2p_oper_channel <= 11 &&
6374 freq_included(channels,
6375 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6376 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6377 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6378 "frequency %d MHz", params->freq);
6379 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6380 wpa_s->conf->p2p_oper_reg_class == 116 ||
6381 wpa_s->conf->p2p_oper_reg_class == 117 ||
6382 wpa_s->conf->p2p_oper_reg_class == 124 ||
6383 wpa_s->conf->p2p_oper_reg_class == 126 ||
6384 wpa_s->conf->p2p_oper_reg_class == 127) &&
6385 freq_included(channels,
6386 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
6387 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6388 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6389 "frequency %d MHz", params->freq);
6390 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6391 wpa_s->best_overall_freq > 0 &&
6392 p2p_supported_freq_go(wpa_s->global->p2p,
6393 wpa_s->best_overall_freq) &&
6394 freq_included(channels, wpa_s->best_overall_freq)) {
6395 params->freq = wpa_s->best_overall_freq;
6396 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6397 "channel %d MHz", params->freq);
6398 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6399 wpa_s->best_24_freq > 0 &&
6400 p2p_supported_freq_go(wpa_s->global->p2p,
6401 wpa_s->best_24_freq) &&
6402 freq_included(channels, wpa_s->best_24_freq)) {
6403 params->freq = wpa_s->best_24_freq;
6404 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6405 "channel %d MHz", params->freq);
6406 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6407 wpa_s->best_5_freq > 0 &&
6408 p2p_supported_freq_go(wpa_s->global->p2p,
6409 wpa_s->best_5_freq) &&
6410 freq_included(channels, wpa_s->best_5_freq)) {
6411 params->freq = wpa_s->best_5_freq;
6412 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6413 "channel %d MHz", params->freq);
6414 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
6416 params->freq = pref_freq;
6417 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6418 "channels", params->freq);
6420 /* no preference, select some channel */
6421 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
6425 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6426 sizeof(struct wpa_used_freq_data));
6430 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
6431 wpa_s->num_multichan_concurrent);
6433 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6435 /* First try the best used frequency if possible */
6436 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
6437 params->freq = cand_freq;
6439 /* Try any of the used frequencies */
6440 for (i = 0; i < num; i++) {
6441 if (freq_included(channels, freqs[i].freq)) {
6442 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
6444 params->freq = freqs[i].freq;
6450 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
6451 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
6455 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
6459 for (i = 0; i < num; i++) {
6460 if (freqs[i].freq == freq)
6465 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
6467 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
6471 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
6481 static struct wpa_supplicant *
6482 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6485 struct wpa_supplicant *group_wpa_s;
6487 if (!wpas_p2p_create_iface(wpa_s)) {
6488 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
6490 wpa_s->p2p_first_connection_timeout = 0;
6494 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
6495 WPA_IF_P2P_CLIENT) < 0) {
6496 wpa_msg_global(wpa_s, MSG_ERROR,
6497 "P2P: Failed to add group interface");
6500 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6501 if (group_wpa_s == NULL) {
6502 wpa_msg_global(wpa_s, MSG_ERROR,
6503 "P2P: Failed to initialize group interface");
6504 wpas_p2p_remove_pending_group_interface(wpa_s);
6508 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6509 group_wpa_s->ifname);
6510 group_wpa_s->p2p_first_connection_timeout = 0;
6516 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6517 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6518 * @persistent_group: Whether to create a persistent group
6519 * @freq: Frequency for the group or 0 to indicate no hardcoding
6520 * @ht40: Start GO with 40 MHz channel width
6521 * @vht: Start GO with VHT support
6522 * Returns: 0 on success, -1 on failure
6524 * This function creates a new P2P group with the local end as the Group Owner,
6525 * i.e., without using Group Owner Negotiation.
6527 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
6528 int freq, int ht40, int vht)
6530 struct p2p_go_neg_results params;
6532 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6535 os_free(wpa_s->global->add_psk);
6536 wpa_s->global->add_psk = NULL;
6538 /* Make sure we are not running find during connection establishment */
6539 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
6540 wpas_p2p_stop_find_oper(wpa_s);
6542 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6546 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
6549 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
6550 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
6551 "(%u MHz) is not supported for P2P uses",
6555 p2p_go_params(wpa_s->global->p2p, ¶ms);
6556 params.persistent_group = persistent_group;
6558 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6561 wpas_start_wps_go(wpa_s, ¶ms, 0);
6567 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
6568 struct wpa_ssid *params, int addr_allocated,
6571 struct wpa_ssid *ssid;
6573 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6576 wpa_s->p2p_last_4way_hs_fail = NULL;
6578 wpa_supplicant_ap_deinit(wpa_s);
6580 ssid = wpa_config_add_network(wpa_s->conf);
6583 wpa_config_set_network_defaults(ssid);
6584 ssid->temporary = 1;
6585 ssid->proto = WPA_PROTO_RSN;
6586 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
6587 ssid->group_cipher = WPA_CIPHER_CCMP;
6588 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6589 ssid->ssid = os_malloc(params->ssid_len);
6590 if (ssid->ssid == NULL) {
6591 wpa_config_remove_network(wpa_s->conf, ssid->id);
6594 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6595 ssid->ssid_len = params->ssid_len;
6596 ssid->p2p_group = 1;
6597 ssid->export_keys = 1;
6598 if (params->psk_set) {
6599 os_memcpy(ssid->psk, params->psk, 32);
6602 if (params->passphrase)
6603 ssid->passphrase = os_strdup(params->passphrase);
6605 wpa_s->show_group_started = 1;
6606 wpa_s->p2p_in_invitation = 1;
6607 wpa_s->p2p_invite_go_freq = freq;
6609 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6611 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6612 wpas_p2p_group_formation_timeout,
6613 wpa_s->parent, NULL);
6614 wpa_supplicant_select_network(wpa_s, ssid);
6620 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6621 struct wpa_ssid *ssid, int addr_allocated,
6622 int force_freq, int neg_freq, int ht40,
6623 int vht, const struct p2p_channels *channels,
6624 int connection_timeout)
6626 struct p2p_go_neg_results params;
6629 if (ssid->disabled != 2 || ssid->ssid == NULL)
6632 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6633 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6634 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6639 os_free(wpa_s->global->add_psk);
6640 wpa_s->global->add_psk = NULL;
6642 /* Make sure we are not running find during connection establishment */
6643 wpas_p2p_stop_find_oper(wpa_s);
6645 wpa_s->p2p_fallback_to_go_neg = 0;
6647 if (ssid->mode == WPAS_MODE_P2P_GO) {
6648 if (force_freq > 0) {
6649 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6653 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6655 (freq > 0 && !freq_included(channels, freq)))
6661 (freq > 0 && !freq_included(channels, freq)))
6665 if (ssid->mode == WPAS_MODE_INFRA)
6666 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
6668 if (ssid->mode != WPAS_MODE_P2P_GO)
6671 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
6675 params.psk_set = ssid->psk_set;
6677 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
6678 if (ssid->passphrase) {
6679 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6680 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6681 "persistent group");
6684 os_strlcpy(params.passphrase, ssid->passphrase,
6685 sizeof(params.passphrase));
6687 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6688 params.ssid_len = ssid->ssid_len;
6689 params.persistent_group = 1;
6691 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6695 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6697 wpa_s->p2p_first_connection_timeout = connection_timeout;
6698 wpas_start_wps_go(wpa_s, ¶ms, 0);
6704 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6705 struct wpabuf *proberesp_ies)
6707 struct wpa_supplicant *wpa_s = ctx;
6708 if (wpa_s->ap_iface) {
6709 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
6710 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6711 wpabuf_free(beacon_ies);
6712 wpabuf_free(proberesp_ies);
6716 wpabuf_free(hapd->p2p_beacon_ie);
6717 hapd->p2p_beacon_ie = beacon_ies;
6719 wpabuf_free(hapd->p2p_probe_resp_ie);
6720 hapd->p2p_probe_resp_ie = proberesp_ies;
6722 wpabuf_free(beacon_ies);
6723 wpabuf_free(proberesp_ies);
6725 wpa_supplicant_ap_update_beacon(wpa_s);
6729 static void wpas_p2p_idle_update(void *ctx, int idle)
6731 struct wpa_supplicant *wpa_s = ctx;
6732 if (!wpa_s->ap_iface)
6734 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
6736 if (wpa_s->global->p2p_fail_on_wps_complete &&
6737 wpa_s->p2p_in_provisioning) {
6738 wpas_p2p_grpform_fail_after_wps(wpa_s);
6741 wpas_p2p_set_group_idle_timeout(wpa_s);
6743 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6747 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6748 struct wpa_ssid *ssid)
6750 struct p2p_group *group;
6751 struct p2p_group_config *cfg;
6753 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6756 cfg = os_zalloc(sizeof(*cfg));
6760 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
6761 cfg->persistent_group = 2;
6762 else if (ssid->p2p_persistent_group)
6763 cfg->persistent_group = 1;
6764 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6765 if (wpa_s->max_stations &&
6766 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6767 cfg->max_clients = wpa_s->max_stations;
6769 cfg->max_clients = wpa_s->conf->max_num_sta;
6770 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6771 cfg->ssid_len = ssid->ssid_len;
6772 cfg->freq = ssid->frequency;
6773 cfg->cb_ctx = wpa_s;
6774 cfg->ie_update = wpas_p2p_ie_update;
6775 cfg->idle_update = wpas_p2p_idle_update;
6777 group = p2p_group_init(wpa_s->global->p2p, cfg);
6780 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
6781 p2p_group_notif_formation_done(group);
6782 wpa_s->p2p_group = group;
6787 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6790 struct wpa_ssid *ssid = wpa_s->current_ssid;
6792 if (!wpa_s->p2p_in_provisioning) {
6793 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6794 "provisioning not in progress");
6798 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6799 u8 go_dev_addr[ETH_ALEN];
6800 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6801 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6803 /* Clear any stored provisioning info */
6804 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6807 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6809 wpa_s->p2p_go_group_formation_completed = 1;
6810 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6812 * Use a separate timeout for initial data connection to
6813 * complete to allow the group to be removed automatically if
6814 * something goes wrong in this step before the P2P group idle
6815 * timeout mechanism is taken into use.
6817 wpa_dbg(wpa_s, MSG_DEBUG,
6818 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6819 P2P_MAX_INITIAL_CONN_WAIT);
6820 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6821 wpas_p2p_group_formation_timeout,
6822 wpa_s->parent, NULL);
6825 * Use a separate timeout for initial data connection to
6826 * complete to allow the group to be removed automatically if
6827 * the client does not complete data connection successfully.
6829 wpa_dbg(wpa_s, MSG_DEBUG,
6830 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6831 P2P_MAX_INITIAL_CONN_WAIT_GO);
6832 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6833 wpas_p2p_group_formation_timeout,
6834 wpa_s->parent, NULL);
6836 * Complete group formation on first successful data connection
6838 wpa_s->p2p_go_group_formation_completed = 0;
6840 if (wpa_s->global->p2p)
6841 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
6842 wpas_group_formation_completed(wpa_s, 1);
6846 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6847 struct wps_event_fail *fail)
6849 if (!wpa_s->p2p_in_provisioning) {
6850 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6851 "provisioning not in progress");
6855 if (wpa_s->go_params) {
6856 p2p_clear_provisioning_info(
6858 wpa_s->go_params->peer_device_addr);
6861 wpas_notify_p2p_wps_failed(wpa_s, fail);
6863 if (wpa_s == wpa_s->global->p2p_group_formation) {
6865 * Allow some time for the failed WPS negotiation exchange to
6866 * complete, but remove the group since group formation cannot
6867 * succeed after provisioning failure.
6869 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6870 wpa_s->global->p2p_fail_on_wps_complete = 1;
6871 eloop_deplete_timeout(0, 50000,
6872 wpas_p2p_group_formation_timeout,
6873 wpa_s->parent, NULL);
6878 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6880 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6881 !wpa_s->p2p_in_provisioning)
6884 wpas_p2p_grpform_fail_after_wps(wpa_s);
6890 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6891 const char *config_method,
6892 enum wpas_p2p_prov_disc_use use,
6893 struct p2ps_provision *p2ps_prov)
6897 wpa_s->global->pending_p2ps_group = 0;
6898 wpa_s->p2p_fallback_to_go_neg = 0;
6899 wpa_s->pending_pd_use = NORMAL_PD;
6900 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6901 p2ps_prov->conncap = p2ps_group_capability(
6902 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
6903 wpa_printf(MSG_DEBUG,
6904 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6905 __func__, p2ps_prov->conncap,
6906 p2ps_prov->adv_id, p2ps_prov->conncap,
6907 p2ps_prov->status, p2ps_prov->info);
6910 } else if (os_strncmp(config_method, "display", 7) == 0)
6911 config_methods = WPS_CONFIG_DISPLAY;
6912 else if (os_strncmp(config_method, "keypad", 6) == 0)
6913 config_methods = WPS_CONFIG_KEYPAD;
6914 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6915 os_strncmp(config_method, "pushbutton", 10) == 0)
6916 config_methods = WPS_CONFIG_PUSHBUTTON;
6918 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
6923 if (use == WPAS_P2P_PD_AUTO) {
6924 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6925 wpa_s->pending_pd_config_methods = config_methods;
6926 wpa_s->p2p_auto_pd = 1;
6927 wpa_s->p2p_auto_join = 0;
6928 wpa_s->pending_pd_before_join = 0;
6929 wpa_s->auto_pd_scan_retry = 0;
6930 wpas_p2p_stop_find(wpa_s);
6931 wpa_s->p2p_join_scan_count = 0;
6932 os_get_reltime(&wpa_s->p2p_auto_started);
6933 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6934 wpa_s->p2p_auto_started.sec,
6935 wpa_s->p2p_auto_started.usec);
6936 wpas_p2p_join_scan(wpa_s, NULL);
6940 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6945 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6946 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6951 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6954 return p2p_scan_result_text(ies, ies_len, buf, end);
6958 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6960 if (!offchannel_pending_action_tx(wpa_s))
6963 wpas_p2p_action_tx_clear(wpa_s);
6965 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6966 "operation request");
6967 offchannel_clear_pending_action_tx(wpa_s);
6971 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6972 enum p2p_discovery_type type,
6973 unsigned int num_req_dev_types, const u8 *req_dev_types,
6974 const u8 *dev_id, unsigned int search_delay,
6975 u8 seek_cnt, const char **seek_string)
6977 wpas_p2p_clear_pending_action_tx(wpa_s);
6978 wpa_s->p2p_long_listen = 0;
6980 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6981 wpa_s->p2p_in_provisioning)
6984 wpa_supplicant_cancel_sched_scan(wpa_s);
6986 return p2p_find(wpa_s->global->p2p, timeout, type,
6987 num_req_dev_types, req_dev_types, dev_id,
6988 search_delay, seek_cnt, seek_string);
6992 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6993 struct wpa_scan_results *scan_res)
6995 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6997 if (wpa_s->p2p_scan_work) {
6998 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6999 wpa_s->p2p_scan_work = NULL;
7000 radio_work_done(work);
7003 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7007 * Indicate that results have been processed so that the P2P module can
7008 * continue pending tasks.
7010 p2p_scan_res_handled(wpa_s->global->p2p);
7014 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
7016 wpas_p2p_clear_pending_action_tx(wpa_s);
7017 wpa_s->p2p_long_listen = 0;
7018 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7019 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
7021 if (wpa_s->global->p2p)
7022 p2p_stop_find(wpa_s->global->p2p);
7024 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7025 wpa_printf(MSG_DEBUG,
7026 "P2P: Do not consider the scan results after stop_find");
7027 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7032 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7034 wpas_p2p_stop_find_oper(wpa_s);
7035 if (!wpa_s->global->pending_group_iface_for_p2ps)
7036 wpas_p2p_remove_pending_group_interface(wpa_s);
7040 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7042 struct wpa_supplicant *wpa_s = eloop_ctx;
7043 wpa_s->p2p_long_listen = 0;
7047 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7051 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7054 wpa_supplicant_cancel_sched_scan(wpa_s);
7055 wpas_p2p_clear_pending_action_tx(wpa_s);
7059 * This is a request for unlimited Listen state. However, at
7060 * least for now, this is mapped to a Listen state for one
7065 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7066 wpa_s->p2p_long_listen = 0;
7069 * Stop previous find/listen operation to avoid trying to request a new
7070 * remain-on-channel operation while the driver is still running the
7073 if (wpa_s->global->p2p)
7074 p2p_stop_find(wpa_s->global->p2p);
7076 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7077 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
7078 wpa_s->p2p_long_listen = timeout * 1000;
7079 eloop_register_timeout(timeout, 0,
7080 wpas_p2p_long_listen_timeout,
7088 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7089 u8 *buf, size_t len, int p2p_group)
7091 struct wpabuf *p2p_ie;
7094 if (wpa_s->global->p2p_disabled)
7096 if (wpa_s->conf->p2p_disabled)
7098 if (wpa_s->global->p2p == NULL)
7103 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7104 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7106 wpabuf_free(p2p_ie);
7112 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
7113 const u8 *dst, const u8 *bssid,
7114 const u8 *ie, size_t ie_len, int ssi_signal)
7116 if (wpa_s->global->p2p_disabled)
7118 if (wpa_s->global->p2p == NULL)
7121 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
7123 case P2P_PREQ_NOT_P2P:
7124 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7127 case P2P_PREQ_MALFORMED:
7128 case P2P_PREQ_NOT_LISTEN:
7129 case P2P_PREQ_NOT_PROCESSED:
7130 default: /* make gcc happy */
7132 case P2P_PREQ_PROCESSED:
7138 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7139 const u8 *sa, const u8 *bssid,
7140 u8 category, const u8 *data, size_t len, int freq)
7142 if (wpa_s->global->p2p_disabled)
7144 if (wpa_s->global->p2p == NULL)
7147 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7152 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7154 if (wpa_s->global->p2p_disabled)
7156 if (wpa_s->global->p2p == NULL)
7159 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
7163 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
7165 p2p_group_deinit(wpa_s->p2p_group);
7166 wpa_s->p2p_group = NULL;
7168 wpa_s->ap_configured_cb = NULL;
7169 wpa_s->ap_configured_cb_ctx = NULL;
7170 wpa_s->ap_configured_cb_data = NULL;
7171 wpa_s->connect_without_scan = NULL;
7175 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7177 wpa_s->p2p_long_listen = 0;
7179 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7182 return p2p_reject(wpa_s->global->p2p, addr);
7186 /* Invite to reinvoke a persistent group */
7187 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7188 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
7189 int ht40, int vht, int pref_freq)
7191 enum p2p_invite_role role;
7195 int no_pref_freq_given = pref_freq == 0;
7197 wpa_s->global->p2p_invite_group = NULL;
7199 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7201 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7203 wpa_s->p2p_persistent_go_freq = freq;
7204 wpa_s->p2p_go_ht40 = !!ht40;
7205 if (ssid->mode == WPAS_MODE_P2P_GO) {
7206 role = P2P_INVITE_ROLE_GO;
7207 if (peer_addr == NULL) {
7208 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7209 "address in invitation command");
7212 if (wpas_p2p_create_iface(wpa_s)) {
7213 if (wpas_p2p_add_group_interface(wpa_s,
7214 WPA_IF_P2P_GO) < 0) {
7215 wpa_printf(MSG_ERROR, "P2P: Failed to "
7216 "allocate a new interface for the "
7220 bssid = wpa_s->pending_interface_addr;
7222 bssid = wpa_s->own_addr;
7224 role = P2P_INVITE_ROLE_CLIENT;
7225 peer_addr = ssid->bssid;
7227 wpa_s->pending_invite_ssid_id = ssid->id;
7229 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7230 role == P2P_INVITE_ROLE_GO);
7234 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7237 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7238 no_pref_freq_given && pref_freq > 0 &&
7239 wpa_s->num_multichan_concurrent > 1 &&
7240 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7241 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7247 * Stop any find/listen operations before invitation and possibly
7248 * connection establishment.
7250 wpas_p2p_stop_find_oper(wpa_s);
7252 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
7253 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
7258 /* Invite to join an active group */
7259 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7260 const u8 *peer_addr, const u8 *go_dev_addr)
7262 struct wpa_global *global = wpa_s->global;
7263 enum p2p_invite_role role;
7265 struct wpa_ssid *ssid;
7267 int freq = 0, force_freq = 0, pref_freq = 0;
7270 wpa_s->p2p_persistent_go_freq = 0;
7271 wpa_s->p2p_go_ht40 = 0;
7272 wpa_s->p2p_go_vht = 0;
7274 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7275 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7278 if (wpa_s == NULL) {
7279 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7283 ssid = wpa_s->current_ssid;
7285 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7290 wpa_s->global->p2p_invite_group = wpa_s;
7291 persistent = ssid->p2p_persistent_group &&
7292 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
7293 ssid->ssid, ssid->ssid_len);
7295 if (ssid->mode == WPAS_MODE_P2P_GO) {
7296 role = P2P_INVITE_ROLE_ACTIVE_GO;
7297 bssid = wpa_s->own_addr;
7298 if (go_dev_addr == NULL)
7299 go_dev_addr = wpa_s->global->p2p_dev_addr;
7300 freq = ssid->frequency;
7302 role = P2P_INVITE_ROLE_CLIENT;
7303 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7304 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7305 "invite to current group");
7308 bssid = wpa_s->bssid;
7309 if (go_dev_addr == NULL &&
7310 !is_zero_ether_addr(wpa_s->go_dev_addr))
7311 go_dev_addr = wpa_s->go_dev_addr;
7312 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7313 (int) wpa_s->assoc_freq;
7315 wpa_s->parent->pending_invite_ssid_id = -1;
7317 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7320 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7321 role == P2P_INVITE_ROLE_ACTIVE_GO);
7324 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
7326 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
7327 ssid->ssid, ssid->ssid_len, force_freq,
7328 go_dev_addr, persistent, pref_freq, -1);
7332 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7334 struct wpa_ssid *ssid = wpa_s->current_ssid;
7335 u8 go_dev_addr[ETH_ALEN];
7336 int network_id = -1;
7342 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7343 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7344 wpa_s->parent, NULL);
7347 if (!wpa_s->show_group_started || !ssid)
7350 wpa_s->show_group_started = 0;
7352 os_memset(go_dev_addr, 0, ETH_ALEN);
7353 if (ssid->bssid_set)
7354 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7355 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7357 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7359 if (wpa_s->global->p2p_group_formation == wpa_s)
7360 wpa_s->global->p2p_group_formation = NULL;
7362 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7363 (int) wpa_s->assoc_freq;
7366 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
7369 res = os_snprintf(ip_addr, sizeof(ip_addr),
7370 " ip_addr=%u.%u.%u.%u "
7371 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7372 ip[0], ip[1], ip[2], ip[3],
7373 ip[4], ip[5], ip[6], ip[7],
7374 ip[8], ip[9], ip[10], ip[11]);
7375 if (os_snprintf_error(sizeof(ip_addr), res))
7379 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7380 ssid->passphrase == NULL && ssid->psk_set ?
7382 ssid->passphrase, go_dev_addr, persistent,
7386 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
7389 network_id = ssid->id;
7390 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
7394 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7395 u32 interval1, u32 duration2, u32 interval2)
7399 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7402 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7403 wpa_s->current_ssid == NULL ||
7404 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7407 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7408 wpa_s->own_addr, wpa_s->assoc_freq,
7409 duration1, interval1, duration2, interval2);
7411 wpa_s->waiting_presence_resp = 1;
7417 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7418 unsigned int interval)
7420 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7423 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7427 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7429 if (wpa_s->current_ssid == NULL) {
7431 * current_ssid can be cleared when P2P client interface gets
7432 * disconnected, so assume this interface was used as P2P
7437 return wpa_s->current_ssid->p2p_group &&
7438 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7442 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7444 struct wpa_supplicant *wpa_s = eloop_ctx;
7446 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
7447 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7452 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7454 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
7458 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7462 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7463 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7465 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7468 timeout = wpa_s->conf->p2p_group_idle;
7469 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7470 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7471 timeout = P2P_MAX_CLIENT_IDLE;
7477 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7478 timeout = 0; /* special client mode no-timeout */
7483 if (wpa_s->p2p_in_provisioning) {
7485 * Use the normal group formation timeout during the
7486 * provisioning phase to avoid terminating this process too
7487 * early due to group idle timeout.
7489 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7490 "during provisioning");
7494 if (wpa_s->show_group_started) {
7496 * Use the normal group formation timeout between the end of
7497 * the provisioning phase and completion of 4-way handshake to
7498 * avoid terminating this process too early due to group idle
7501 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7502 "while waiting for initial 4-way handshake to "
7507 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
7509 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7514 /* Returns 1 if the interface was removed */
7515 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7516 u16 reason_code, const u8 *ie, size_t ie_len,
7517 int locally_generated)
7519 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7522 if (!locally_generated)
7523 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7526 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7527 wpa_s->current_ssid &&
7528 wpa_s->current_ssid->p2p_group &&
7529 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7530 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7531 "session is ending");
7532 if (wpas_p2p_group_delete(wpa_s,
7533 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7542 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7543 u16 reason_code, const u8 *ie, size_t ie_len,
7544 int locally_generated)
7546 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7549 if (!locally_generated)
7550 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7555 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7557 struct p2p_data *p2p = wpa_s->global->p2p;
7562 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7565 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7566 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7568 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7569 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7572 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7573 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7575 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7576 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7578 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7579 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7580 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7581 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7582 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7585 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7586 p2p_set_sec_dev_types(p2p,
7587 (void *) wpa_s->conf->sec_device_type,
7588 wpa_s->conf->num_sec_device_types);
7590 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7592 p2p_remove_wps_vendor_extensions(p2p);
7593 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7594 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7596 p2p_add_wps_vendor_extension(
7597 p2p, wpa_s->conf->wps_vendor_ext[i]);
7601 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7602 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7604 country[0] = wpa_s->conf->country[0];
7605 country[1] = wpa_s->conf->country[1];
7607 p2p_set_country(p2p, country);
7610 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7611 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7612 wpa_s->conf->p2p_ssid_postfix ?
7613 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7617 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7618 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
7620 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7621 u8 reg_class, channel;
7626 if (wpa_s->conf->p2p_listen_reg_class &&
7627 wpa_s->conf->p2p_listen_channel) {
7628 reg_class = wpa_s->conf->p2p_listen_reg_class;
7629 channel = wpa_s->conf->p2p_listen_channel;
7634 * Pick one of the social channels randomly as the
7637 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7640 channel = 1 + (r % 3) * 5;
7643 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7646 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7649 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7650 u8 op_reg_class, op_channel, cfg_op_channel;
7653 if (wpa_s->conf->p2p_oper_reg_class &&
7654 wpa_s->conf->p2p_oper_channel) {
7655 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7656 op_channel = wpa_s->conf->p2p_oper_channel;
7661 * Use random operation channel from (1, 6, 11)
7662 *if no other preference is indicated.
7664 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7667 op_channel = 1 + (r % 3) * 5;
7670 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7673 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7677 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7678 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7679 wpa_s->conf->p2p_pref_chan) < 0) {
7680 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7684 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7685 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7690 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7691 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
7695 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7698 if (!wpa_s->ap_iface)
7700 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7705 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7707 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7710 wpa_s->global->cross_connection = enabled;
7711 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7714 struct wpa_supplicant *iface;
7716 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7718 if (iface->cross_connect_enabled == 0)
7721 iface->cross_connect_enabled = 0;
7722 iface->cross_connect_in_use = 0;
7723 wpa_msg_global(iface->parent, MSG_INFO,
7724 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7726 iface->cross_connect_uplink);
7734 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7736 struct wpa_supplicant *iface;
7738 if (!uplink->global->cross_connection)
7741 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7742 if (!iface->cross_connect_enabled)
7744 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7747 if (iface->ap_iface == NULL)
7749 if (iface->cross_connect_in_use)
7752 iface->cross_connect_in_use = 1;
7753 wpa_msg_global(iface->parent, MSG_INFO,
7754 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7755 iface->ifname, iface->cross_connect_uplink);
7760 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7762 struct wpa_supplicant *iface;
7764 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7765 if (!iface->cross_connect_enabled)
7767 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7770 if (!iface->cross_connect_in_use)
7773 wpa_msg_global(iface->parent, MSG_INFO,
7774 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7775 iface->ifname, iface->cross_connect_uplink);
7776 iface->cross_connect_in_use = 0;
7781 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7783 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7784 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7785 wpa_s->cross_connect_disallowed)
7786 wpas_p2p_disable_cross_connect(wpa_s);
7788 wpas_p2p_enable_cross_connect(wpa_s);
7789 if (!wpa_s->ap_iface &&
7790 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7791 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7795 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7797 wpas_p2p_disable_cross_connect(wpa_s);
7798 if (!wpa_s->ap_iface &&
7799 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7801 wpas_p2p_set_group_idle_timeout(wpa_s);
7805 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7807 struct wpa_supplicant *iface;
7809 if (!wpa_s->global->cross_connection)
7812 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7815 if (iface->drv_flags &
7816 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7818 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7819 iface != wpa_s->parent)
7822 wpa_s->cross_connect_enabled = 1;
7823 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7824 sizeof(wpa_s->cross_connect_uplink));
7825 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7826 "%s to %s whenever uplink is available",
7827 wpa_s->ifname, wpa_s->cross_connect_uplink);
7829 if (iface->ap_iface || iface->current_ssid == NULL ||
7830 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7831 iface->cross_connect_disallowed ||
7832 iface->wpa_state != WPA_COMPLETED)
7835 wpa_s->cross_connect_in_use = 1;
7836 wpa_msg_global(wpa_s->parent, MSG_INFO,
7837 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7838 wpa_s->ifname, wpa_s->cross_connect_uplink);
7844 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7846 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7847 !wpa_s->p2p_in_provisioning)
7848 return 0; /* not P2P client operation */
7850 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7852 if (wpa_s != wpa_s->parent)
7853 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
7854 wpas_p2p_group_formation_failed(wpa_s);
7859 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7861 struct wpa_supplicant *wpa_s = eloop_ctx;
7862 wpas_p2p_notif_pbc_overlap(wpa_s);
7866 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
7868 struct p2p_channels chan, cli_chan;
7869 struct wpa_supplicant *ifs;
7871 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7874 os_memset(&chan, 0, sizeof(chan));
7875 os_memset(&cli_chan, 0, sizeof(cli_chan));
7876 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
7877 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7882 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
7884 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
7886 if (!ifs->current_ssid ||
7887 !ifs->current_ssid->p2p_group ||
7888 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
7889 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
7891 freq = ifs->current_ssid->frequency;
7892 if (freq_included(&chan, freq)) {
7893 wpa_dbg(ifs, MSG_DEBUG,
7894 "P2P GO operating frequency %d MHz in valid range",
7899 wpa_dbg(ifs, MSG_DEBUG,
7900 "P2P GO operating in invalid frequency %d MHz", freq);
7901 /* TODO: Consider using CSA or removing the group within
7903 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
7908 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7909 struct wpa_scan_results *scan_res)
7911 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7915 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7917 struct wpa_global *global = wpa_s->global;
7921 if (global->p2p == NULL)
7924 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7926 if (wpa_s->pending_interface_name[0] &&
7927 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7930 peer = p2p_get_go_neg_peer(global->p2p);
7932 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7933 MACSTR, MAC2STR(peer));
7934 p2p_unauthorize(global->p2p, peer);
7938 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7939 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7940 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7944 if (wpa_s->pending_pd_before_join) {
7945 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7946 wpa_s->pending_pd_before_join = 0;
7950 wpas_p2p_stop_find(wpa_s);
7952 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7953 if (wpa_s == global->p2p_group_formation &&
7954 (wpa_s->p2p_in_provisioning ||
7955 wpa_s->parent->pending_interface_type ==
7956 WPA_IF_P2P_CLIENT)) {
7957 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7958 "formation found - cancelling",
7961 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7962 wpa_s->parent, NULL);
7963 if (wpa_s->p2p_in_provisioning) {
7964 wpas_group_formation_completed(wpa_s, 0);
7967 wpas_p2p_group_delete(wpa_s,
7968 P2P_GROUP_REMOVAL_REQUESTED);
7970 } else if (wpa_s->p2p_in_invitation) {
7971 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7974 wpas_p2p_group_formation_failed(wpa_s);
7979 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7987 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7989 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7992 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7993 "being available anymore");
7994 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
7998 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7999 int freq_24, int freq_5, int freq_overall)
8001 struct p2p_data *p2p = wpa_s->global->p2p;
8004 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8008 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8011 struct p2p_data *p2p = wpa_s->global->p2p;
8016 if (hwaddr_aton(addr, peer))
8019 return p2p_unauthorize(p2p, peer);
8024 * wpas_p2p_disconnect - Disconnect from a P2P Group
8025 * @wpa_s: Pointer to wpa_supplicant data
8026 * Returns: 0 on success, -1 on failure
8028 * This can be used to disconnect from a group in which the local end is a P2P
8029 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8030 * virtual network interface was created for this group, that interface will be
8031 * removed. Otherwise, only the configured P2P group network will be removed
8032 * from the interface.
8034 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8040 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8045 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8049 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8052 ret = p2p_in_progress(wpa_s->global->p2p);
8055 * Check whether there is an ongoing WPS provisioning step (or
8056 * other parts of group formation) on another interface since
8057 * p2p_in_progress() does not report this to avoid issues for
8058 * scans during such provisioning step.
8060 if (wpa_s->global->p2p_group_formation &&
8061 wpa_s->global->p2p_group_formation != wpa_s) {
8062 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8063 "in group formation",
8064 wpa_s->global->p2p_group_formation->ifname);
8069 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
8070 struct os_reltime now;
8071 os_get_reltime(&now);
8072 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8073 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
8074 /* Wait for the first client has expired */
8075 wpa_s->global->p2p_go_wait_client.sec = 0;
8077 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8086 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8087 struct wpa_ssid *ssid)
8089 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8090 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8091 wpa_s->parent, NULL) > 0) {
8093 * Remove the network by scheduling the group formation
8094 * timeout to happen immediately. The teardown code
8095 * needs to be scheduled to run asynch later so that we
8096 * don't delete data from under ourselves unexpectedly.
8097 * Calling wpas_p2p_group_formation_timeout directly
8098 * causes a series of crashes in WPS failure scenarios.
8100 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8101 "P2P group network getting removed");
8102 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
8103 wpa_s->parent, NULL);
8108 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
8109 const u8 *addr, const u8 *ssid,
8115 for (s = wpa_s->conf->ssid; s; s = s->next) {
8116 if (s->disabled != 2)
8119 (ssid_len != s->ssid_len ||
8120 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8123 if (s->mode == WPAS_MODE_P2P_GO)
8127 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8128 return s; /* peer is GO in the persistent group */
8129 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8131 for (i = 0; i < s->num_p2p_clients; i++) {
8132 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
8133 addr, ETH_ALEN) == 0)
8134 return s; /* peer is P2P client in persistent
8143 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8146 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8147 wpa_s->parent, NULL) > 0) {
8149 * This can happen if WPS provisioning step is not terminated
8150 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8151 * peer was able to connect, there is no need to time out group
8152 * formation after this, though. In addition, this is used with
8153 * the initial connection wait on the GO as a separate formation
8154 * timeout and as such, expected to be hit after the initial WPS
8155 * provisioning step.
8157 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
8159 if (!wpa_s->p2p_go_group_formation_completed &&
8160 !wpa_s->group_formation_reported) {
8162 * GO has not yet notified group formation success since
8163 * the WPS step was not completed cleanly. Do that
8164 * notification now since the P2P Client was able to
8165 * connect and as such, must have received the
8166 * credential from the WPS step.
8168 if (wpa_s->global->p2p)
8169 p2p_wps_success_cb(wpa_s->global->p2p, addr);
8170 wpas_group_formation_completed(wpa_s, 1);
8173 if (!wpa_s->p2p_go_group_formation_completed) {
8174 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8175 wpa_s->p2p_go_group_formation_completed = 1;
8176 wpa_s->global->p2p_group_formation = NULL;
8177 wpa_s->p2p_in_provisioning = 0;
8178 wpa_s->p2p_in_invitation = 0;
8180 wpa_s->global->p2p_go_wait_client.sec = 0;
8183 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8187 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8190 struct wpa_supplicant *group = wpa_s;
8193 if (wpa_s->global->p2p_group_formation)
8194 group = wpa_s->global->p2p_group_formation;
8195 wpa_s = wpa_s->parent;
8196 offchannel_send_action_done(wpa_s);
8198 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
8199 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8200 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8201 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8202 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
8203 wpa_s->p2p_persistent_id,
8204 wpa_s->p2p_pd_before_go_neg,
8211 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8215 if (!wpa_s->p2p_fallback_to_go_neg ||
8216 wpa_s->p2p_in_provisioning <= 5)
8219 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8220 return 0; /* peer operating as a GO */
8222 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8223 "fallback to GO Negotiation");
8224 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
8226 return res == 1 ? 2 : 1;
8230 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8232 struct wpa_supplicant *ifs;
8234 if (wpa_s->wpa_state > WPA_SCANNING) {
8235 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8236 "concurrent operation",
8237 wpa_s->conf->p2p_search_delay);
8238 return wpa_s->conf->p2p_search_delay;
8241 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8243 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
8244 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8245 "delay due to concurrent operation on "
8247 wpa_s->conf->p2p_search_delay,
8249 return wpa_s->conf->p2p_search_delay;
8257 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8258 struct wpa_ssid *s, const u8 *addr,
8261 struct psk_list_entry *psk, *tmp;
8264 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8266 if ((iface_addr && !psk->p2p &&
8267 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8268 (!iface_addr && psk->p2p &&
8269 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8270 wpa_dbg(wpa_s, MSG_DEBUG,
8271 "P2P: Remove persistent group PSK list entry for "
8273 MAC2STR(psk->addr), psk->p2p);
8274 dl_list_del(&psk->list);
8284 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8285 const u8 *p2p_dev_addr,
8286 const u8 *psk, size_t psk_len)
8288 struct wpa_ssid *ssid = wpa_s->current_ssid;
8289 struct wpa_ssid *persistent;
8290 struct psk_list_entry *p, *last;
8292 if (psk_len != sizeof(p->psk))
8296 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8297 " p2p_dev_addr=" MACSTR,
8298 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8299 if (is_zero_ether_addr(p2p_dev_addr))
8300 p2p_dev_addr = NULL;
8302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8306 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8307 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8308 /* To be added to persistent group once created */
8309 if (wpa_s->global->add_psk == NULL) {
8310 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8311 if (wpa_s->global->add_psk == NULL)
8314 p = wpa_s->global->add_psk;
8317 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8320 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8322 os_memcpy(p->psk, psk, psk_len);
8326 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8327 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8331 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
8334 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8338 p = os_zalloc(sizeof(*p));
8343 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8346 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8348 os_memcpy(p->psk, psk, psk_len);
8350 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8351 (last = dl_list_last(&persistent->psk_list,
8352 struct psk_list_entry, list))) {
8353 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8354 MACSTR " (p2p=%u) to make room for a new one",
8355 MAC2STR(last->addr), last->p2p);
8356 dl_list_del(&last->list);
8360 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
8361 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8362 p2p_dev_addr == NULL);
8364 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8365 MACSTR, MAC2STR(p2p_dev_addr));
8367 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8370 dl_list_add(&persistent->psk_list, &p->list);
8372 if (wpa_s->parent->conf->update_config &&
8373 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
8374 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
8378 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8379 struct wpa_ssid *s, const u8 *addr,
8384 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
8385 if (res > 0 && wpa_s->conf->update_config &&
8386 wpa_config_write(wpa_s->confname, wpa_s->conf))
8387 wpa_dbg(wpa_s, MSG_DEBUG,
8388 "P2P: Failed to update configuration");
8392 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8393 const u8 *peer, int iface_addr)
8395 struct hostapd_data *hapd;
8396 struct hostapd_wpa_psk *psk, *prev, *rem;
8397 struct sta_info *sta;
8399 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8400 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8403 /* Remove per-station PSK entry */
8404 hapd = wpa_s->ap_iface->bss[0];
8406 psk = hapd->conf->ssid.wpa_psk;
8408 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8410 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8411 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8412 MACSTR " iface_addr=%d",
8413 MAC2STR(peer), iface_addr);
8415 prev->next = psk->next;
8417 hapd->conf->ssid.wpa_psk = psk->next;
8427 /* Disconnect from group */
8429 sta = ap_get_sta(hapd, peer);
8431 sta = ap_get_sta_p2p(hapd, peer);
8433 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8434 " (iface_addr=%d) from group",
8435 MAC2STR(peer), iface_addr);
8436 hostapd_drv_sta_deauth(hapd, sta->addr,
8437 WLAN_REASON_DEAUTH_LEAVING);
8438 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8443 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8447 struct wpa_supplicant *w;
8449 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8451 /* Remove from any persistent group */
8452 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
8453 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8456 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
8457 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
8460 /* Remove from any operating group */
8461 for (w = wpa_s->global->ifaces; w; w = w->next)
8462 wpas_p2p_remove_client_go(w, peer, iface_addr);
8466 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8468 struct wpa_supplicant *wpa_s = eloop_ctx;
8469 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8473 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8475 struct wpa_supplicant *wpa_s = eloop_ctx;
8477 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8478 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8482 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8483 struct wpa_ssid *ssid)
8485 struct wpa_supplicant *iface;
8487 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8488 if (!iface->current_ssid ||
8489 iface->current_ssid->frequency == freq ||
8490 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8491 !iface->current_ssid->p2p_group))
8494 /* Remove the connection with least priority */
8495 if (!wpas_is_p2p_prioritized(iface)) {
8496 /* STA connection has priority over existing
8497 * P2P connection, so remove the interface. */
8498 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8499 eloop_register_timeout(0, 0,
8500 wpas_p2p_group_freq_conflict,
8502 /* If connection in progress is P2P connection, do not
8503 * proceed for the connection. */
8509 /* P2P connection has priority, disable the STA network
8511 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8513 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8514 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8515 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8517 /* If P2P connection is in progress, continue
8530 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8532 struct wpa_ssid *ssid = wpa_s->current_ssid;
8534 if (ssid == NULL || !ssid->p2p_group)
8537 if (wpa_s->p2p_last_4way_hs_fail &&
8538 wpa_s->p2p_last_4way_hs_fail == ssid) {
8539 u8 go_dev_addr[ETH_ALEN];
8540 struct wpa_ssid *persistent;
8542 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8544 ssid->ssid_len) <= 0) {
8545 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8549 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8550 MACSTR, MAC2STR(go_dev_addr));
8551 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
8554 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8555 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8558 wpa_msg_global(wpa_s->parent, MSG_INFO,
8559 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8562 wpa_s->p2p_last_4way_hs_fail = NULL;
8564 * Remove the group from a timeout to avoid issues with caller
8565 * continuing to use the interface if this is on a P2P group
8568 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8573 wpa_s->p2p_last_4way_hs_fail = ssid;
8578 #ifdef CONFIG_WPS_NFC
8580 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8588 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8592 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8593 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8600 wpabuf_put_be16(ret, wsc_len);
8602 wpabuf_put_buf(ret, wsc);
8603 wpabuf_put_be16(ret, wpabuf_len(p2p));
8604 wpabuf_put_buf(ret, p2p);
8608 wpa_hexdump_buf(MSG_DEBUG,
8609 "P2P: Generated NFC connection handover message", ret);
8613 tmp = ndef_build_p2p(ret);
8616 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8626 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8627 struct wpa_ssid **ssid, u8 *go_dev_addr)
8629 struct wpa_supplicant *iface;
8632 os_memset(go_dev_addr, 0, ETH_ALEN);
8635 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8636 if (iface->wpa_state < WPA_ASSOCIATING ||
8637 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8638 !iface->current_ssid->p2p_group ||
8639 iface->current_ssid->mode != WPAS_MODE_INFRA)
8642 *ssid = iface->current_ssid;
8644 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8645 return iface->assoc_freq;
8651 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8654 struct wpabuf *wsc, *p2p;
8655 struct wpa_ssid *ssid;
8656 u8 go_dev_addr[ETH_ALEN];
8657 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8659 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8660 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8664 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8665 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8666 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8667 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8671 if (cli_freq == 0) {
8672 wsc = wps_build_nfc_handover_req_p2p(
8673 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8676 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8677 go_dev_addr, ssid ? ssid->ssid : NULL,
8678 ssid ? ssid->ssid_len : 0);
8680 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8684 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8687 struct wpabuf *wsc, *p2p;
8688 struct wpa_ssid *ssid;
8689 u8 go_dev_addr[ETH_ALEN];
8690 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8692 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8695 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8696 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8697 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8700 if (cli_freq == 0) {
8701 wsc = wps_build_nfc_handover_sel_p2p(
8703 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8704 DEV_PW_NFC_CONNECTION_HANDOVER,
8705 wpa_s->conf->wps_nfc_dh_pubkey,
8706 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8709 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8710 go_dev_addr, ssid ? ssid->ssid : NULL,
8711 ssid ? ssid->ssid_len : 0);
8713 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8717 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8718 struct p2p_nfc_params *params)
8720 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8721 "connection handover (freq=%d)",
8724 if (params->go_freq && params->go_ssid_len) {
8725 wpa_s->p2p_wps_method = WPS_NFC;
8726 wpa_s->pending_join_wps_method = WPS_NFC;
8727 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8728 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8730 return wpas_p2p_join_start(wpa_s, params->go_freq,
8732 params->go_ssid_len);
8735 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8736 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
8737 params->go_freq, -1, 0, 1, 1);
8741 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8742 struct p2p_nfc_params *params, int tag)
8744 int res, persistent;
8745 struct wpa_ssid *ssid;
8747 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8748 "connection handover");
8749 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8750 ssid = wpa_s->current_ssid;
8753 if (ssid->mode != WPAS_MODE_P2P_GO)
8755 if (wpa_s->ap_iface == NULL)
8759 if (wpa_s == NULL) {
8760 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8764 if (wpa_s->parent->p2p_oob_dev_pw_id !=
8765 DEV_PW_NFC_CONNECTION_HANDOVER &&
8766 !wpa_s->parent->p2p_oob_dev_pw) {
8767 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8770 res = wpas_ap_wps_add_nfc_pw(
8771 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
8772 wpa_s->parent->p2p_oob_dev_pw,
8773 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
8774 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
8779 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8783 if (!params->peer ||
8784 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8787 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8788 " to join", MAC2STR(params->peer->p2p_device_addr));
8790 wpa_s->global->p2p_invite_group = wpa_s;
8791 persistent = ssid->p2p_persistent_group &&
8792 wpas_p2p_get_persistent(wpa_s->parent,
8793 params->peer->p2p_device_addr,
8794 ssid->ssid, ssid->ssid_len);
8795 wpa_s->parent->pending_invite_ssid_id = -1;
8797 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8798 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8799 ssid->ssid, ssid->ssid_len, ssid->frequency,
8800 wpa_s->global->p2p_dev_addr, persistent, 0,
8801 wpa_s->parent->p2p_oob_dev_pw_id);
8805 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8806 struct p2p_nfc_params *params,
8809 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8810 "connection handover");
8811 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8812 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8813 forced_freq, -1, 0, 1, 1);
8817 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8818 struct p2p_nfc_params *params,
8823 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8824 "connection handover");
8825 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8826 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8827 forced_freq, -1, 0, 1, 1);
8831 res = wpas_p2p_listen(wpa_s, 60);
8833 p2p_unauthorize(wpa_s->global->p2p,
8834 params->peer->p2p_device_addr);
8841 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8842 const struct wpabuf *data,
8843 int sel, int tag, int forced_freq)
8845 const u8 *pos, *end;
8847 struct p2p_nfc_params params;
8850 os_memset(¶ms, 0, sizeof(params));
8853 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8855 pos = wpabuf_head(data);
8856 end = pos + wpabuf_len(data);
8858 if (end - pos < 2) {
8859 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8863 len = WPA_GET_BE16(pos);
8865 if (len > end - pos) {
8866 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8870 params.wsc_attr = pos;
8871 params.wsc_len = len;
8874 if (end - pos < 2) {
8875 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8879 len = WPA_GET_BE16(pos);
8881 if (len > end - pos) {
8882 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8886 params.p2p_attr = pos;
8887 params.p2p_len = len;
8890 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8891 params.wsc_attr, params.wsc_len);
8892 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8893 params.p2p_attr, params.p2p_len);
8895 wpa_hexdump(MSG_DEBUG,
8896 "P2P: Ignored extra data after P2P attributes",
8900 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
8904 if (params.next_step == NO_ACTION)
8907 if (params.next_step == BOTH_GO) {
8908 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8909 MAC2STR(params.peer->p2p_device_addr));
8913 if (params.next_step == PEER_CLIENT) {
8914 if (!is_zero_ether_addr(params.go_dev_addr)) {
8915 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8916 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8918 MAC2STR(params.peer->p2p_device_addr),
8920 MAC2STR(params.go_dev_addr),
8921 wpa_ssid_txt(params.go_ssid,
8922 params.go_ssid_len));
8924 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8925 "peer=" MACSTR " freq=%d",
8926 MAC2STR(params.peer->p2p_device_addr),
8932 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8933 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8934 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8938 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8939 wpa_s->p2p_oob_dev_pw = NULL;
8941 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8942 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8947 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8948 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8949 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8950 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8951 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8952 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8953 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8957 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8958 "peer OOB Device Password Id %u", id);
8961 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8962 "Device Password Id %u", id);
8963 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8964 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8965 params.oob_dev_pw_len -
8966 WPS_OOB_PUBKEY_HASH_LEN - 2);
8967 wpa_s->p2p_oob_dev_pw_id = id;
8968 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8969 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8970 params.oob_dev_pw_len -
8971 WPS_OOB_PUBKEY_HASH_LEN - 2);
8972 if (wpa_s->p2p_oob_dev_pw == NULL)
8975 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8976 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8977 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8980 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8981 "without Device Password");
8982 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8985 switch (params.next_step) {
8989 /* already covered above */
8992 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
8994 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
8996 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
8998 /* TODO: use own OOB Dev Pw */
8999 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
9006 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9007 const struct wpabuf *data, int forced_freq)
9009 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9012 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9016 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9017 const struct wpabuf *req,
9018 const struct wpabuf *sel, int forced_freq)
9023 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9026 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9028 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9029 wpabuf_head(req), wpabuf_len(req));
9030 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9031 wpabuf_head(sel), wpabuf_len(sel));
9033 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9034 tmp = ndef_parse_p2p(init ? sel : req);
9036 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9040 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9048 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9051 int go_intent = wpa_s->conf->p2p_go_intent;
9052 struct wpa_supplicant *iface;
9054 if (wpa_s->global->p2p == NULL)
9058 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9059 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9061 if (!iface->ap_iface)
9063 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9065 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9067 if (wpa_s->p2p_nfc_tag_enabled)
9068 wpas_p2p_remove_pending_group_interface(wpa_s);
9069 wpa_s->p2p_nfc_tag_enabled = 0;
9073 if (wpa_s->global->p2p_disabled)
9076 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9077 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9078 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9079 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9080 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9081 "to allow static handover cases");
9085 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9087 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9088 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9089 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9090 if (wpa_s->p2p_oob_dev_pw == NULL)
9092 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9094 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9095 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9097 * P2P Group Interface present and the command came on group
9098 * interface, so enable the token for the current interface.
9100 wpa_s->create_p2p_iface = 0;
9102 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9105 if (wpa_s->create_p2p_iface) {
9106 enum wpa_driver_if_type iftype;
9107 /* Prepare to add a new interface for the group */
9108 iftype = WPA_IF_P2P_GROUP;
9109 if (go_intent == 15)
9110 iftype = WPA_IF_P2P_GO;
9111 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9112 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9113 "interface for the group");
9117 if_addr = wpa_s->pending_interface_addr;
9119 if_addr = wpa_s->own_addr;
9121 wpa_s->p2p_nfc_tag_enabled = enabled;
9123 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9124 struct hostapd_data *hapd;
9125 if (iface->ap_iface == NULL)
9127 hapd = iface->ap_iface->bss[0];
9128 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9129 hapd->conf->wps_nfc_dh_pubkey =
9130 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9131 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9132 hapd->conf->wps_nfc_dh_privkey =
9133 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9134 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9135 hapd->conf->wps_nfc_dev_pw =
9136 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9137 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9139 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9140 wpa_dbg(iface, MSG_DEBUG,
9141 "P2P: Failed to enable NFC Tag for GO");
9144 p2p_set_authorized_oob_dev_pw_id(
9145 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9151 #endif /* CONFIG_WPS_NFC */
9154 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9155 struct wpa_used_freq_data *freqs,
9158 u8 curr_chan, cand, chan;
9161 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9162 for (i = 0, cand = 0; i < num; i++) {
9163 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9164 if (curr_chan == chan) {
9169 if (chan == 1 || chan == 6 || chan == 11)
9174 wpa_dbg(wpa_s, MSG_DEBUG,
9175 "P2P: Update Listen channel to %u based on operating channel",
9177 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9182 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9184 struct wpa_used_freq_data *freqs;
9185 unsigned int num = wpa_s->num_multichan_concurrent;
9187 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9191 * If possible, optimize the Listen channel to be a channel that is
9192 * already used by one of the other interfaces.
9194 if (!wpa_s->conf->p2p_optimize_listen_chan)
9197 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9200 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9204 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9206 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
9211 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9213 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9214 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9215 "the management interface is being removed");
9216 wpas_p2p_deinit_global(wpa_s->global);
9221 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9223 if (wpa_s->ap_iface->bss)
9224 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9225 wpas_p2p_group_deinit(wpa_s);