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"
26 #include "eapol_supp/eapol_supp_sm.h"
27 #include "rsn_supp/wpa.h"
28 #include "wpa_supplicant_i.h"
31 #include "config_ssid.h"
36 #include "offchannel.h"
37 #include "wps_supplicant.h"
38 #include "p2p_supplicant.h"
39 #include "wifi_display.h"
43 * How many times to try to scan to find the GO before giving up on join
46 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
48 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
50 #ifndef P2P_MAX_CLIENT_IDLE
52 * How many seconds to try to reconnect to the GO when connection in P2P client
55 #define P2P_MAX_CLIENT_IDLE 10
56 #endif /* P2P_MAX_CLIENT_IDLE */
58 #ifndef P2P_MAX_INITIAL_CONN_WAIT
60 * How many seconds to wait for initial 4-way handshake to get completed after
61 * WPS provisioning step or after the re-invocation of a persistent group on a
64 #define P2P_MAX_INITIAL_CONN_WAIT 10
65 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
67 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
69 * How many seconds to wait for initial 4-way handshake to get completed after
70 * WPS provisioning step on the GO. This controls the extra time the P2P
71 * operation is considered to be in progress (e.g., to delay other scans) after
72 * WPS provisioning has been completed on the GO during group formation.
74 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
75 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
77 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
79 * How many seconds to wait for initial 4-way handshake to get completed after
80 * re-invocation of a persistent group on the GO when the client is expected
81 * to connect automatically (no user interaction).
83 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
84 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
86 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
88 enum p2p_group_removal_reason {
89 P2P_GROUP_REMOVAL_UNKNOWN,
90 P2P_GROUP_REMOVAL_SILENT,
91 P2P_GROUP_REMOVAL_FORMATION_FAILED,
92 P2P_GROUP_REMOVAL_REQUESTED,
93 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
94 P2P_GROUP_REMOVAL_UNAVAILABLE,
95 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
96 P2P_GROUP_REMOVAL_PSK_FAILURE,
97 P2P_GROUP_REMOVAL_FREQ_CONFLICT
101 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
102 static struct wpa_supplicant *
103 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
105 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
106 const u8 *ssid, size_t ssid_len);
107 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
108 const u8 *ssid, size_t ssid_len);
109 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
110 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
111 const u8 *dev_addr, enum p2p_wps_method wps_method,
112 int auto_join, int freq,
113 const u8 *ssid, size_t ssid_len);
114 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
115 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
116 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
117 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
118 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
120 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
121 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
123 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
124 static void wpas_stop_listen(void *ctx);
125 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
126 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
127 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
128 enum wpa_driver_if_type type);
129 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
130 int already_deleted);
134 * Get the number of concurrent channels that the HW can operate, but that are
135 * currently not in use by any of the wpa_supplicant interfaces.
137 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
142 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
146 num = get_shared_radio_freqs(wpa_s, freqs,
147 wpa_s->num_multichan_concurrent);
150 unused = wpa_s->num_multichan_concurrent - num;
151 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
157 * Get the frequencies that are currently in use by one or more of the virtual
158 * interfaces, and that are also valid for P2P operation.
161 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
162 struct wpa_used_freq_data *p2p_freqs,
165 struct wpa_used_freq_data *freqs;
166 unsigned int num, i, j;
168 freqs = os_calloc(wpa_s->num_multichan_concurrent,
169 sizeof(struct wpa_used_freq_data));
173 num = get_shared_radio_freqs_data(wpa_s, freqs,
174 wpa_s->num_multichan_concurrent);
176 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
178 for (i = 0, j = 0; i < num && j < len; i++) {
179 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
180 p2p_freqs[j++] = freqs[i];
185 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
191 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
194 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
197 /* Use the wpa_s used to control the P2P Device operation */
198 wpa_s = wpa_s->global->p2p_init_wpa_s;
200 if (wpa_s->conf->p2p_ignore_shared_freq &&
201 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
202 wpas_p2p_num_unused_channels(wpa_s) > 0) {
203 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
207 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
211 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
212 struct wpa_scan_results *scan_res)
216 if (wpa_s->p2p_scan_work) {
217 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
218 wpa_s->p2p_scan_work = NULL;
219 radio_work_done(work);
222 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
225 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
226 (int) scan_res->num);
228 for (i = 0; i < scan_res->num; i++) {
229 struct wpa_scan_res *bss = scan_res->res[i];
230 struct os_reltime time_tmp_age, entry_ts;
234 time_tmp_age.sec = bss->age / 1000;
235 time_tmp_age.usec = (bss->age % 1000) * 1000;
236 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
238 ies = (const u8 *) (bss + 1);
239 ies_len = bss->ie_len;
240 if (bss->beacon_ie_len > 0 &&
241 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
242 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
243 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
244 MACSTR, MAC2STR(bss->bssid));
246 ies_len = bss->beacon_ie_len;
250 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
251 bss->freq, &entry_ts, bss->level,
256 p2p_scan_res_handled(wpa_s->global->p2p);
260 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
262 struct wpa_supplicant *wpa_s = work->wpa_s;
263 struct wpa_driver_scan_params *params = work->ctx;
267 if (!work->started) {
268 wpa_scan_free_params(params);
272 wpa_s->p2p_scan_work = NULL;
276 ret = wpa_drv_scan(wpa_s, params);
277 wpa_scan_free_params(params);
280 radio_work_done(work);
281 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
285 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
286 os_get_reltime(&wpa_s->scan_trigger_time);
287 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
288 wpa_s->own_scan_requested = 1;
289 wpa_s->p2p_scan_work = work;
293 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
296 if (wpa_s->global->p2p_24ghz_social_channels &&
297 (freq == 2412 || freq == 2437 || freq == 2462)) {
299 * Search all social channels regardless of whether these have
300 * been disabled for P2P operating channel use to avoid missing
305 return p2p_supported_freq(wpa_s->global->p2p, freq);
309 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
310 unsigned int num_req_dev_types,
311 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
313 struct wpa_supplicant *wpa_s = ctx;
314 struct wpa_driver_scan_params *params = NULL;
315 struct wpabuf *wps_ie, *ies;
316 unsigned int num_channels = 0;
317 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
321 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
324 if (wpa_s->p2p_scan_work) {
325 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
329 params = os_zalloc(sizeof(*params));
333 /* P2P Wildcard SSID */
334 params->num_ssids = 1;
335 n = os_malloc(P2P_WILDCARD_SSID_LEN);
338 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
339 params->ssids[0].ssid = n;
340 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
342 wpa_s->wps->dev.p2p = 1;
343 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
344 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
345 num_req_dev_types, req_dev_types);
349 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
350 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
355 wpabuf_put_buf(ies, wps_ie);
358 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
360 params->p2p_probe = 1;
361 n = os_malloc(wpabuf_len(ies));
366 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
367 params->extra_ies = n;
368 params->extra_ies_len = wpabuf_len(ies);
372 case P2P_SCAN_SOCIAL:
373 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
375 if (params->freqs == NULL)
377 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
378 if (wpas_p2p_search_social_channel(
379 wpa_s, social_channels_freq[i]))
380 params->freqs[num_channels++] =
381 social_channels_freq[i];
383 params->freqs[num_channels++] = 0;
387 case P2P_SCAN_SPECIFIC:
388 params->freqs = os_calloc(2, sizeof(int));
389 if (params->freqs == NULL)
391 params->freqs[0] = freq;
392 params->freqs[1] = 0;
394 case P2P_SCAN_SOCIAL_PLUS_ONE:
395 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
397 if (params->freqs == NULL)
399 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
400 if (wpas_p2p_search_social_channel(
401 wpa_s, social_channels_freq[i]))
402 params->freqs[num_channels++] =
403 social_channels_freq[i];
405 if (p2p_supported_freq(wpa_s->global->p2p, freq))
406 params->freqs[num_channels++] = freq;
407 params->freqs[num_channels++] = 0;
411 radio_remove_works(wpa_s, "p2p-scan", 0);
412 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
418 wpa_scan_free_params(params);
423 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
425 switch (p2p_group_interface) {
426 case P2P_GROUP_INTERFACE_PENDING:
427 return WPA_IF_P2P_GROUP;
428 case P2P_GROUP_INTERFACE_GO:
429 return WPA_IF_P2P_GO;
430 case P2P_GROUP_INTERFACE_CLIENT:
431 return WPA_IF_P2P_CLIENT;
434 return WPA_IF_P2P_GROUP;
438 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
440 size_t ssid_len, int *go)
444 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
445 for (s = wpa_s->conf->ssid; s; s = s->next) {
446 if (s->disabled != 0 || !s->p2p_group ||
447 s->ssid_len != ssid_len ||
448 os_memcmp(ssid, s->ssid, ssid_len) != 0)
450 if (s->mode == WPAS_MODE_P2P_GO &&
451 s != wpa_s->current_ssid)
454 *go = s->mode == WPAS_MODE_P2P_GO;
463 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
465 struct wpa_supplicant *wpa_s = eloop_ctx;
466 wpa_printf(MSG_DEBUG,
467 "P2P: Complete previously requested removal of %s",
469 wpas_p2p_disconnect(wpa_s);
473 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
474 struct wpa_supplicant *calling_wpa_s)
476 if (calling_wpa_s == wpa_s && wpa_s &&
477 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
479 * The calling wpa_s instance is going to be removed. Do that
480 * from an eloop callback to keep the instance available until
481 * the caller has returned. This my be needed, e.g., to provide
482 * control interface responses on the per-interface socket.
484 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
490 return wpas_p2p_disconnect(wpa_s);
494 /* Determine total number of clients in active groups where we are the GO */
495 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
497 unsigned int count = 0;
500 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
501 for (s = wpa_s->conf->ssid; s; s = s->next) {
502 wpa_printf(MSG_DEBUG,
503 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
504 wpa_s, s, s->disabled, s->p2p_group,
506 if (!s->disabled && s->p2p_group &&
507 s->mode == WPAS_MODE_P2P_GO) {
508 count += p2p_get_group_num_members(
518 /* Find an interface for a P2P group where we are the GO */
519 static struct wpa_supplicant *
520 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
522 struct wpa_supplicant *save = NULL;
528 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
529 for (s = wpa_s->conf->ssid; s; s = s->next) {
530 if (s->disabled || !s->p2p_group ||
531 s->mode != WPAS_MODE_P2P_GO)
534 /* Prefer a group with connected clients */
535 if (p2p_get_group_num_members(wpa_s->p2p_group))
541 /* No group with connected clients, so pick the one without (if any) */
546 /* Find an active P2P group where we are the GO */
547 static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
550 struct wpa_ssid *s, *empty = NULL;
555 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
556 for (s = wpa_s->conf->ssid; s; s = s->next) {
557 if (s->disabled || !s->p2p_group ||
558 s->mode != WPAS_MODE_P2P_GO)
561 os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
562 if (p2p_get_group_num_members(wpa_s->p2p_group))
572 /* Find a persistent group where we are the GO */
573 static struct wpa_ssid *
574 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
578 for (s = wpa_s->conf->ssid; s; s = s->next) {
579 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
587 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
589 struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
591 u8 conncap = P2PS_SETUP_NONE;
592 unsigned int owned_members = 0;
593 unsigned int owner = 0;
594 unsigned int client = 0;
595 struct wpa_supplicant *go_wpa_s;
596 struct wpa_ssid *persistent_go;
597 int p2p_no_group_iface;
599 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
602 * For non-concurrent capable devices:
603 * If persistent_go, then no new.
604 * If GO, then no client.
605 * If client, then no GO.
607 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
608 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
609 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
611 wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
612 go_wpa_s, persistent_go);
614 for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
615 tmp_wpa_s = tmp_wpa_s->next) {
616 for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
617 wpa_printf(MSG_DEBUG,
618 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
619 tmp_wpa_s, s, s->disabled,
620 s->p2p_group, s->mode);
621 if (!s->disabled && s->p2p_group) {
622 if (s->mode == WPAS_MODE_P2P_GO) {
624 p2p_get_group_num_members(
625 tmp_wpa_s->p2p_group);
633 /* If not concurrent, restrict our choices */
634 if (p2p_no_group_iface) {
635 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
638 return P2PS_SETUP_NONE;
641 if (role == P2PS_SETUP_CLIENT ||
642 incoming == P2PS_SETUP_GROUP_OWNER ||
643 p2p_client_limit_reached(go_wpa_s->p2p_group))
644 return P2PS_SETUP_NONE;
646 return P2PS_SETUP_GROUP_OWNER;
650 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
652 return P2PS_SETUP_GROUP_OWNER |
654 if (incoming == P2PS_SETUP_NEW) {
657 if (os_get_random(&r, sizeof(r)) < 0 ||
659 return P2PS_SETUP_CLIENT;
660 return P2PS_SETUP_GROUP_OWNER;
666 /* If a required role has been specified, handle it here */
667 if (role && role != P2PS_SETUP_NEW) {
669 case P2PS_SETUP_NONE:
671 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
672 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
676 case P2PS_SETUP_GROUP_OWNER:
678 * Must be a complimentary role - cannot be a client to
679 * more than one peer.
681 if (incoming == role || client)
682 return P2PS_SETUP_NONE;
684 return P2PS_SETUP_CLIENT;
686 case P2PS_SETUP_CLIENT:
687 /* Must be a complimentary role */
688 if (incoming != role) {
689 conncap = P2PS_SETUP_GROUP_OWNER;
694 return P2PS_SETUP_NONE;
699 * For now, we only will support ownership of one group, and being a
700 * client of one group. Therefore, if we have either an existing GO
701 * group, or an existing client group, we will not do a new GO
702 * negotiation, but rather try to re-use the existing groups.
705 case P2PS_SETUP_NONE:
708 conncap = P2PS_SETUP_GROUP_OWNER;
709 else if (!owned_members)
710 conncap = P2PS_SETUP_NEW;
711 else if (incoming == P2PS_SETUP_NONE)
712 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
714 conncap = P2PS_SETUP_CLIENT;
717 case P2PS_SETUP_CLIENT:
718 conncap = P2PS_SETUP_GROUP_OWNER;
721 case P2PS_SETUP_GROUP_OWNER:
723 conncap = P2PS_SETUP_CLIENT;
726 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
727 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
729 conncap = P2PS_SETUP_GROUP_OWNER;
733 if (os_get_random(&r, sizeof(r)) < 0 ||
735 conncap = P2PS_SETUP_CLIENT;
737 conncap = P2PS_SETUP_GROUP_OWNER;
742 return P2PS_SETUP_NONE;
746 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
747 (!incoming && (conncap & P2PS_SETUP_NEW))) {
748 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
749 conncap &= ~P2PS_SETUP_GROUP_OWNER;
750 wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
751 owner, owned_members, conncap);
753 s = wpas_p2p_get_persistent_go(wpa_s);
755 if (!s && !owner && p2p_no_group_iface) {
756 p2p_set_intended_addr(wpa_s->global->p2p,
758 } else if (!s && !owner) {
759 if (wpas_p2p_add_group_interface(wpa_s,
760 WPA_IF_P2P_GO) < 0) {
761 wpa_printf(MSG_ERROR,
762 "P2P: Failed to allocate a new interface for the group");
763 return P2PS_SETUP_NONE;
765 wpa_s->global->pending_group_iface_for_p2ps = 1;
766 p2p_set_intended_addr(wpa_s->global->p2p,
767 wpa_s->pending_interface_addr);
775 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
776 enum p2p_group_removal_reason removal_reason)
778 struct wpa_ssid *ssid;
782 ssid = wpa_s->current_ssid;
785 * The current SSID was not known, but there may still be a
786 * pending P2P group interface waiting for provisioning or a
787 * P2P group that is trying to reconnect.
789 ssid = wpa_s->conf->ssid;
791 if (ssid->p2p_group && ssid->disabled != 2)
796 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
798 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
803 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
805 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
806 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
807 wpa_s->reassociate = 0;
808 wpa_s->disconnected = 1;
813 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
814 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
816 if (os_strcmp(gtype, "client") == 0) {
817 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
818 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
820 wpa_printf(MSG_DEBUG,
821 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
822 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
823 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
828 if (wpa_s->cross_connect_in_use) {
829 wpa_s->cross_connect_in_use = 0;
830 wpa_msg_global(wpa_s->parent, MSG_INFO,
831 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
832 wpa_s->ifname, wpa_s->cross_connect_uplink);
834 switch (removal_reason) {
835 case P2P_GROUP_REMOVAL_REQUESTED:
836 reason = " reason=REQUESTED";
838 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
839 reason = " reason=FORMATION_FAILED";
841 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
842 reason = " reason=IDLE";
844 case P2P_GROUP_REMOVAL_UNAVAILABLE:
845 reason = " reason=UNAVAILABLE";
847 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
848 reason = " reason=GO_ENDING_SESSION";
850 case P2P_GROUP_REMOVAL_PSK_FAILURE:
851 reason = " reason=PSK_FAILURE";
853 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
854 reason = " reason=FREQ_CONFLICT";
860 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
861 wpa_msg_global(wpa_s->parent, MSG_INFO,
862 P2P_EVENT_GROUP_REMOVED "%s %s%s",
863 wpa_s->ifname, gtype, reason);
866 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
867 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
868 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
869 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
870 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
871 wpa_s->parent, NULL) > 0) {
872 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
874 wpa_s->p2p_in_provisioning = 0;
875 wpas_p2p_group_formation_failed(wpa_s, 1);
878 wpa_s->p2p_in_invitation = 0;
881 * Make sure wait for the first client does not remain active after the
882 * group has been removed.
884 wpa_s->global->p2p_go_wait_client.sec = 0;
886 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
887 struct wpa_global *global;
889 enum wpa_driver_if_type type;
890 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
892 global = wpa_s->global;
893 ifname = os_strdup(wpa_s->ifname);
894 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
895 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
896 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
897 wpa_s = global->ifaces;
899 wpa_drv_if_remove(wpa_s, type, ifname);
904 if (!wpa_s->p2p_go_group_formation_completed) {
905 wpa_s->global->p2p_group_formation = NULL;
906 wpa_s->p2p_in_provisioning = 0;
909 wpa_s->show_group_started = 0;
910 os_free(wpa_s->go_params);
911 wpa_s->go_params = NULL;
913 os_free(wpa_s->p2p_group_common_freqs);
914 wpa_s->p2p_group_common_freqs = NULL;
915 wpa_s->p2p_group_common_freqs_num = 0;
917 wpa_s->waiting_presence_resp = 0;
919 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
920 if (ssid && (ssid->p2p_group ||
921 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
922 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
924 if (ssid == wpa_s->current_ssid) {
925 wpa_sm_set_config(wpa_s->wpa, NULL);
926 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
927 wpa_s->current_ssid = NULL;
930 * Networks objects created during any P2P activities are not
931 * exposed out as they might/will confuse certain non-P2P aware
932 * applications since these network objects won't behave like
935 * Likewise, we don't send out network removed signals for such
938 wpa_config_remove_network(wpa_s->conf, id);
939 wpa_supplicant_clear_status(wpa_s);
940 wpa_supplicant_cancel_sched_scan(wpa_s);
942 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
946 wpa_supplicant_ap_deinit(wpa_s);
948 wpa_drv_deinit_p2p_cli(wpa_s);
950 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
956 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
958 const u8 *ssid, size_t ssid_len)
966 if (wpa_s->go_params)
967 bssid = wpa_s->go_params->peer_interface_addr;
969 bssid = wpa_s->bssid;
971 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
972 if (bss == NULL && wpa_s->go_params &&
973 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
974 bss = wpa_bss_get_p2p_dev_addr(
975 wpa_s, wpa_s->go_params->peer_device_addr);
977 u8 iface_addr[ETH_ALEN];
978 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
980 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
983 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
984 "group is persistent - BSS " MACSTR " not found",
989 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
991 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
994 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
995 "group is persistent - BSS " MACSTR
996 " did not include P2P IE", MAC2STR(bssid));
997 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
998 (u8 *) (bss + 1), bss->ie_len);
999 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1000 ((u8 *) bss + 1) + bss->ie_len,
1001 bss->beacon_ie_len);
1005 group_capab = p2p_get_group_capab(p2p);
1006 addr = p2p_get_go_dev_addr(p2p);
1007 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1008 "group_capab=0x%x", group_capab);
1010 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1011 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1014 os_memset(go_dev_addr, 0, ETH_ALEN);
1017 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1018 "go_dev_addr=" MACSTR,
1019 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1021 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1025 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1026 struct wpa_ssid *ssid,
1027 const u8 *go_dev_addr)
1032 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1033 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1034 for (s = wpa_s->conf->ssid; s; s = s->next) {
1035 if (s->disabled == 2 &&
1036 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1037 s->ssid_len == ssid->ssid_len &&
1038 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1043 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1045 if (ssid->passphrase && !s->passphrase)
1047 else if (ssid->passphrase && s->passphrase &&
1048 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1051 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1054 s = wpa_config_add_network(wpa_s->conf);
1059 * Instead of network_added we emit persistent_group_added
1060 * notification. Also to keep the defense checks in
1061 * persistent_group obj registration method, we set the
1062 * relevant flags in s to designate it as a persistent group.
1065 s->p2p_persistent_group = 1;
1066 wpas_notify_persistent_group_added(wpa_s, s);
1067 wpa_config_set_network_defaults(s);
1071 s->p2p_persistent_group = 1;
1074 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1075 s->mode = ssid->mode;
1076 s->auth_alg = WPA_AUTH_ALG_OPEN;
1077 s->key_mgmt = WPA_KEY_MGMT_PSK;
1078 s->proto = WPA_PROTO_RSN;
1079 s->pairwise_cipher = WPA_CIPHER_CCMP;
1081 if (ssid->passphrase) {
1082 os_free(s->passphrase);
1083 s->passphrase = os_strdup(ssid->passphrase);
1085 if (ssid->psk_set) {
1087 os_memcpy(s->psk, ssid->psk, 32);
1089 if (s->passphrase && !s->psk_set)
1090 wpa_config_update_psk(s);
1091 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1093 s->ssid = os_malloc(ssid->ssid_len);
1096 s->ssid_len = ssid->ssid_len;
1097 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1099 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1100 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1101 wpa_s->global->add_psk = NULL;
1105 if (changed && wpa_s->conf->update_config &&
1106 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1107 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1114 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1117 struct wpa_ssid *ssid, *s;
1121 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
1123 ssid = wpa_s->current_ssid;
1124 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1125 !ssid->p2p_persistent_group)
1128 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
1129 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1132 if (s->ssid_len == ssid->ssid_len &&
1133 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1140 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
1141 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
1145 if (i == s->num_p2p_clients - 1)
1146 return; /* already the most recent entry */
1148 /* move the entry to mark it most recent */
1149 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1150 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1151 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
1152 os_memcpy(s->p2p_client_list +
1153 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1155 os_memset(s->p2p_client_list +
1156 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1162 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1163 n = os_realloc_array(s->p2p_client_list,
1164 s->num_p2p_clients + 1, 2 * ETH_ALEN);
1167 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1169 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1171 s->p2p_client_list = n;
1172 s->num_p2p_clients++;
1173 } else if (!found && s->p2p_client_list) {
1174 /* Not enough room for an additional entry - drop the oldest
1176 os_memmove(s->p2p_client_list,
1177 s->p2p_client_list + 2 * ETH_ALEN,
1178 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1179 os_memcpy(s->p2p_client_list +
1180 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1182 os_memset(s->p2p_client_list +
1183 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1187 if (p2p_wpa_s->conf->update_config &&
1188 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
1189 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1193 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1194 int go, struct wpa_ssid *ssid, int freq,
1195 const u8 *psk, const char *passphrase,
1196 const u8 *go_dev_addr, int persistent,
1199 const char *ssid_txt;
1203 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1208 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1212 if (passphrase && passphrase[0] == '\0')
1216 * Include PSK/passphrase only in the control interface message and
1217 * leave it out from the debug log entry.
1219 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1220 P2P_EVENT_GROUP_STARTED
1221 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1223 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1224 psk ? " psk=" : "", psk_txt,
1225 passphrase ? " passphrase=\"" : "",
1226 passphrase ? passphrase : "",
1227 passphrase ? "\"" : "",
1228 MAC2STR(go_dev_addr),
1229 persistent ? " [PERSISTENT]" : "", extra);
1230 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1231 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1232 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1233 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1238 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1239 int success, int already_deleted)
1241 struct wpa_ssid *ssid;
1244 u8 go_dev_addr[ETH_ALEN];
1245 int network_id = -1;
1248 * This callback is likely called for the main interface. Update wpa_s
1249 * to use the group interface if a new interface was created for the
1252 if (wpa_s->global->p2p_group_formation)
1253 wpa_s = wpa_s->global->p2p_group_formation;
1254 if (wpa_s->p2p_go_group_formation_completed) {
1255 wpa_s->global->p2p_group_formation = NULL;
1256 wpa_s->p2p_in_provisioning = 0;
1258 wpa_s->p2p_in_invitation = 0;
1259 wpa_s->group_formation_reported = 1;
1262 wpa_msg_global(wpa_s->parent, MSG_INFO,
1263 P2P_EVENT_GROUP_FORMATION_FAILURE);
1264 if (already_deleted)
1266 wpas_p2p_group_delete(wpa_s,
1267 P2P_GROUP_REMOVAL_FORMATION_FAILED);
1271 wpa_msg_global(wpa_s->parent, MSG_INFO,
1272 P2P_EVENT_GROUP_FORMATION_SUCCESS);
1274 ssid = wpa_s->current_ssid;
1275 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1276 ssid->mode = WPAS_MODE_P2P_GO;
1277 p2p_group_notif_formation_done(wpa_s->p2p_group);
1278 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1283 client = ssid->mode == WPAS_MODE_INFRA;
1284 if (ssid->mode == WPAS_MODE_P2P_GO) {
1285 persistent = ssid->p2p_persistent_group;
1286 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1289 persistent = wpas_p2p_persistent_group(wpa_s,
1294 client = wpa_s->p2p_group_interface ==
1295 P2P_GROUP_INTERFACE_CLIENT;
1296 os_memset(go_dev_addr, 0, ETH_ALEN);
1299 wpa_s->show_group_started = 0;
1302 * Indicate event only after successfully completed 4-way
1303 * handshake, i.e., when the interface is ready for data
1306 wpa_s->show_group_started = 1;
1308 wpas_p2p_group_started(wpa_s, 1, ssid,
1309 ssid ? ssid->frequency : 0,
1310 ssid && ssid->passphrase == NULL &&
1311 ssid->psk_set ? ssid->psk : NULL,
1312 ssid ? ssid->passphrase : NULL,
1313 go_dev_addr, persistent, "");
1314 wpas_p2p_cross_connect_setup(wpa_s);
1315 wpas_p2p_set_group_idle_timeout(wpa_s);
1319 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1322 os_free(wpa_s->global->add_psk);
1323 wpa_s->global->add_psk = NULL;
1325 if (network_id < 0 && ssid)
1326 network_id = ssid->id;
1328 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1329 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1334 struct send_action_work {
1340 unsigned int wait_time;
1345 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1348 struct wpa_supplicant *wpa_s = eloop_ctx;
1350 if (!wpa_s->p2p_send_action_work)
1353 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1354 os_free(wpa_s->p2p_send_action_work->ctx);
1355 radio_work_done(wpa_s->p2p_send_action_work);
1356 wpa_s->p2p_send_action_work = NULL;
1360 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1362 if (wpa_s->p2p_send_action_work) {
1363 struct send_action_work *awork;
1364 awork = wpa_s->p2p_send_action_work->ctx;
1365 if (awork->wait_time == 0) {
1367 radio_work_done(wpa_s->p2p_send_action_work);
1368 wpa_s->p2p_send_action_work = NULL;
1371 * In theory, this should not be needed, but number of
1372 * places in the P2P code is still using non-zero wait
1373 * time for the last Action frame in the sequence and
1374 * some of these do not call send_action_done().
1376 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1378 eloop_register_timeout(
1379 0, awork->wait_time * 1000,
1380 wpas_p2p_send_action_work_timeout,
1387 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1389 const u8 *dst, const u8 *src,
1391 const u8 *data, size_t data_len,
1392 enum offchannel_send_action_result
1395 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1397 wpas_p2p_action_tx_clear(wpa_s);
1399 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1403 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1404 res = P2P_SEND_ACTION_SUCCESS;
1406 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1407 res = P2P_SEND_ACTION_NO_ACK;
1409 case OFFCHANNEL_SEND_ACTION_FAILED:
1410 res = P2P_SEND_ACTION_FAILED;
1414 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1416 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1417 wpa_s->pending_pd_before_join &&
1418 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1419 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1420 wpa_s->p2p_fallback_to_go_neg) {
1421 wpa_s->pending_pd_before_join = 0;
1422 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1423 "during p2p_connect-auto");
1424 wpa_msg_global(wpa_s->parent, MSG_INFO,
1425 P2P_EVENT_FALLBACK_TO_GO_NEG
1426 "reason=no-ACK-to-PD-Req");
1427 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1433 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1435 struct wpa_supplicant *wpa_s = work->wpa_s;
1436 struct send_action_work *awork = work->ctx;
1439 if (work->started) {
1440 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1442 wpa_s->p2p_send_action_work = NULL;
1443 offchannel_send_action_done(wpa_s);
1449 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1450 awork->bssid, awork->buf, awork->len,
1452 wpas_p2p_send_action_tx_status, 1) < 0) {
1454 radio_work_done(work);
1457 wpa_s->p2p_send_action_work = work;
1461 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1462 unsigned int freq, const u8 *dst,
1463 const u8 *src, const u8 *bssid, const u8 *buf,
1464 size_t len, unsigned int wait_time)
1466 struct send_action_work *awork;
1468 if (wpa_s->p2p_send_action_work) {
1469 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1473 awork = os_zalloc(sizeof(*awork) + len);
1478 os_memcpy(awork->dst, dst, ETH_ALEN);
1479 os_memcpy(awork->src, src, ETH_ALEN);
1480 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1482 awork->wait_time = wait_time;
1483 os_memcpy(awork->buf, buf, len);
1485 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1486 wpas_send_action_cb, awork) < 0) {
1495 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1496 const u8 *src, const u8 *bssid, const u8 *buf,
1497 size_t len, unsigned int wait_time)
1499 struct wpa_supplicant *wpa_s = ctx;
1500 int listen_freq = -1, send_freq = -1;
1502 if (wpa_s->p2p_listen_work)
1503 listen_freq = wpa_s->p2p_listen_work->freq;
1504 if (wpa_s->p2p_send_action_work)
1505 send_freq = wpa_s->p2p_send_action_work->freq;
1506 if (listen_freq != (int) freq && send_freq != (int) freq) {
1507 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1508 listen_freq, send_freq);
1509 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1513 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1514 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1516 wpas_p2p_send_action_tx_status, 1);
1520 static void wpas_send_action_done(void *ctx)
1522 struct wpa_supplicant *wpa_s = ctx;
1524 if (wpa_s->p2p_send_action_work) {
1525 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1527 os_free(wpa_s->p2p_send_action_work->ctx);
1528 radio_work_done(wpa_s->p2p_send_action_work);
1529 wpa_s->p2p_send_action_work = NULL;
1532 offchannel_send_action_done(wpa_s);
1536 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1537 struct p2p_go_neg_results *params)
1539 if (wpa_s->go_params == NULL) {
1540 wpa_s->go_params = os_malloc(sizeof(*params));
1541 if (wpa_s->go_params == NULL)
1544 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1549 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1550 struct p2p_go_neg_results *res)
1552 wpa_s->group_formation_reported = 0;
1553 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1554 " dev_addr " MACSTR " wps_method %d",
1555 MAC2STR(res->peer_interface_addr),
1556 MAC2STR(res->peer_device_addr), res->wps_method);
1557 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1558 res->ssid, res->ssid_len);
1559 wpa_supplicant_ap_deinit(wpa_s);
1560 wpas_copy_go_neg_results(wpa_s, res);
1561 if (res->wps_method == WPS_PBC) {
1562 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1563 #ifdef CONFIG_WPS_NFC
1564 } else if (res->wps_method == WPS_NFC) {
1565 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1566 res->peer_interface_addr,
1567 wpa_s->parent->p2p_oob_dev_pw,
1568 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1569 wpa_s->parent->p2p_oob_dev_pw_id ==
1570 DEV_PW_NFC_CONNECTION_HANDOVER ?
1571 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1574 #endif /* CONFIG_WPS_NFC */
1576 u16 dev_pw_id = DEV_PW_DEFAULT;
1577 if (wpa_s->p2p_wps_method == WPS_P2PS)
1578 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1579 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1580 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1581 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1582 wpa_s->p2p_pin, 1, dev_pw_id);
1587 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1588 struct wpa_ssid *ssid)
1590 struct wpa_ssid *persistent;
1591 struct psk_list_entry *psk;
1592 struct hostapd_data *hapd;
1594 if (!wpa_s->ap_iface)
1597 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1599 if (persistent == NULL)
1602 hapd = wpa_s->ap_iface->bss[0];
1604 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1606 struct hostapd_wpa_psk *hpsk;
1608 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1610 MAC2STR(psk->addr), psk->p2p);
1611 hpsk = os_zalloc(sizeof(*hpsk));
1614 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1616 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1618 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1619 hpsk->next = hapd->conf->ssid.wpa_psk;
1620 hapd->conf->ssid.wpa_psk = hpsk;
1625 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1629 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1630 wpa_s->p2p_group_common_freqs_num);
1632 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1633 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1634 i, wpa_s->p2p_group_common_freqs[i]);
1638 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1639 struct p2p_go_neg_results *params)
1641 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1643 wpa_s->p2p_group_common_freqs_num = 0;
1644 os_free(wpa_s->p2p_group_common_freqs);
1645 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1646 if (!wpa_s->p2p_group_common_freqs)
1649 for (i = 0; i < len; i++) {
1650 if (!wpa_s->go_params->freq_list[i])
1652 wpa_s->p2p_group_common_freqs[i] =
1653 wpa_s->go_params->freq_list[i];
1655 wpa_s->p2p_group_common_freqs_num = i;
1659 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1661 #ifndef CONFIG_NO_CONFIG_WRITE
1662 if (wpa_s->parent->conf->update_config &&
1663 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1664 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1665 #endif /* CONFIG_NO_CONFIG_WRITE */
1669 static void p2p_go_configured(void *ctx, void *data)
1671 struct wpa_supplicant *wpa_s = ctx;
1672 struct p2p_go_neg_results *params = data;
1673 struct wpa_ssid *ssid;
1674 int network_id = -1;
1676 p2p_go_save_group_common_freqs(wpa_s, params);
1677 p2p_go_dump_common_freqs(wpa_s);
1679 ssid = wpa_s->current_ssid;
1680 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1681 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1682 if (wpa_s->global->p2p_group_formation == wpa_s)
1683 wpa_s->global->p2p_group_formation = NULL;
1684 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1685 params->passphrase[0] == '\0' ?
1688 wpa_s->global->p2p_dev_addr,
1689 params->persistent_group, "");
1690 wpa_s->group_formation_reported = 1;
1692 if (wpa_s->parent->p2ps_join_addr_valid) {
1693 wpa_dbg(wpa_s, MSG_DEBUG,
1694 "P2PS: Setting default PIN for " MACSTR,
1695 MAC2STR(wpa_s->parent->p2ps_join_addr));
1696 wpa_supplicant_ap_wps_pin(wpa_s,
1697 wpa_s->parent->p2ps_join_addr,
1698 "12345670", NULL, 0, 0);
1699 wpa_s->parent->p2ps_join_addr_valid = 0;
1702 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1703 if (params->persistent_group) {
1704 network_id = wpas_p2p_store_persistent_group(
1705 wpa_s->parent, ssid,
1706 wpa_s->global->p2p_dev_addr);
1707 wpas_p2p_add_psk_list(wpa_s, ssid);
1710 network_id = ssid->id;
1711 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1712 wpas_p2p_cross_connect_setup(wpa_s);
1713 wpas_p2p_set_group_idle_timeout(wpa_s);
1715 if (wpa_s->p2p_first_connection_timeout) {
1716 wpa_dbg(wpa_s, MSG_DEBUG,
1717 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1718 wpa_s->p2p_first_connection_timeout);
1719 wpa_s->p2p_go_group_formation_completed = 0;
1720 wpa_s->global->p2p_group_formation = wpa_s;
1721 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1722 wpa_s->parent, NULL);
1723 eloop_register_timeout(
1724 wpa_s->p2p_first_connection_timeout, 0,
1725 wpas_p2p_group_formation_timeout,
1726 wpa_s->parent, NULL);
1732 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1733 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1734 params->peer_interface_addr)) {
1735 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1739 if (params->wps_method == WPS_PBC) {
1740 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1741 params->peer_device_addr);
1742 #ifdef CONFIG_WPS_NFC
1743 } else if (params->wps_method == WPS_NFC) {
1744 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1745 DEV_PW_NFC_CONNECTION_HANDOVER &&
1746 !wpa_s->parent->p2p_oob_dev_pw) {
1747 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1750 wpas_ap_wps_add_nfc_pw(
1751 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1752 wpa_s->parent->p2p_oob_dev_pw,
1753 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1754 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1755 #endif /* CONFIG_WPS_NFC */
1756 } else if (wpa_s->p2p_pin[0])
1757 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1758 wpa_s->p2p_pin, NULL, 0, 0);
1759 os_free(wpa_s->go_params);
1760 wpa_s->go_params = NULL;
1764 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1765 struct p2p_go_neg_results *params,
1766 int group_formation)
1768 struct wpa_ssid *ssid;
1770 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1771 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1772 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1777 ssid = wpa_config_add_network(wpa_s->conf);
1779 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1783 wpa_s->show_group_started = 0;
1784 wpa_s->p2p_go_group_formation_completed = 0;
1785 wpa_s->group_formation_reported = 0;
1786 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1788 wpa_config_set_network_defaults(ssid);
1789 ssid->temporary = 1;
1790 ssid->p2p_group = 1;
1791 ssid->p2p_persistent_group = params->persistent_group;
1792 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1794 ssid->frequency = params->freq;
1795 ssid->ht40 = params->ht40;
1796 ssid->vht = params->vht;
1797 ssid->ssid = os_zalloc(params->ssid_len + 1);
1799 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1800 ssid->ssid_len = params->ssid_len;
1802 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1803 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1804 ssid->proto = WPA_PROTO_RSN;
1805 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1806 ssid->group_cipher = WPA_CIPHER_CCMP;
1807 if (params->freq > 56160) {
1809 * Enable GCMP instead of CCMP as pairwise_cipher and
1810 * group_cipher in 60 GHz.
1812 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1813 ssid->group_cipher = WPA_CIPHER_GCMP;
1815 if (os_strlen(params->passphrase) > 0) {
1816 ssid->passphrase = os_strdup(params->passphrase);
1817 if (ssid->passphrase == NULL) {
1818 wpa_msg_global(wpa_s, MSG_ERROR,
1819 "P2P: Failed to copy passphrase for GO");
1820 wpa_config_remove_network(wpa_s->conf, ssid->id);
1824 ssid->passphrase = NULL;
1825 ssid->psk_set = params->psk_set;
1827 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1828 else if (ssid->passphrase)
1829 wpa_config_update_psk(ssid);
1830 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1832 wpa_s->ap_configured_cb = p2p_go_configured;
1833 wpa_s->ap_configured_cb_ctx = wpa_s;
1834 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1835 wpa_s->scan_req = NORMAL_SCAN_REQ;
1836 wpa_s->connect_without_scan = ssid;
1837 wpa_s->reassociate = 1;
1838 wpa_s->disconnected = 0;
1839 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1841 wpa_supplicant_req_scan(wpa_s, 0, 0);
1845 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1846 const struct wpa_supplicant *src)
1848 struct wpa_config *d;
1849 const struct wpa_config *s;
1854 #define C(n) if (s->n) d->n = os_strdup(s->n)
1863 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1864 os_memcpy(d->sec_device_type, s->sec_device_type,
1865 sizeof(d->sec_device_type));
1866 d->num_sec_device_types = s->num_sec_device_types;
1868 d->p2p_group_idle = s->p2p_group_idle;
1869 d->p2p_intra_bss = s->p2p_intra_bss;
1870 d->persistent_reconnect = s->persistent_reconnect;
1871 d->max_num_sta = s->max_num_sta;
1872 d->pbc_in_m1 = s->pbc_in_m1;
1873 d->ignore_old_scan_res = s->ignore_old_scan_res;
1874 d->beacon_int = s->beacon_int;
1875 d->dtim_period = s->dtim_period;
1876 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
1877 d->disassoc_low_ack = s->disassoc_low_ack;
1878 d->disable_scan_offload = s->disable_scan_offload;
1879 d->passive_scan = s->passive_scan;
1881 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1882 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1883 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1885 d->p2p_cli_probe = s->p2p_cli_probe;
1889 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1890 char *ifname, size_t len)
1892 char *ifname_ptr = wpa_s->ifname;
1894 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1895 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1896 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1899 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1900 if (os_strlen(ifname) >= IFNAMSIZ &&
1901 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1904 /* Try to avoid going over the IFNAMSIZ length limit */
1905 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1906 if (os_snprintf_error(len, res) && len)
1907 ifname[len - 1] = '\0';
1912 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1913 enum wpa_driver_if_type type)
1915 char ifname[120], force_ifname[120];
1917 if (wpa_s->pending_interface_name[0]) {
1918 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1919 "- skip creation of a new one");
1920 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1921 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1922 "unknown?! ifname='%s'",
1923 wpa_s->pending_interface_name);
1929 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1930 force_ifname[0] = '\0';
1932 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1934 wpa_s->p2p_group_idx++;
1936 wpa_s->pending_interface_type = type;
1937 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1938 wpa_s->pending_interface_addr, NULL) < 0) {
1939 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1944 if (force_ifname[0]) {
1945 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1947 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1948 sizeof(wpa_s->pending_interface_name));
1950 os_strlcpy(wpa_s->pending_interface_name, ifname,
1951 sizeof(wpa_s->pending_interface_name));
1952 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1953 MACSTR, wpa_s->pending_interface_name,
1954 MAC2STR(wpa_s->pending_interface_addr));
1960 static void wpas_p2p_remove_pending_group_interface(
1961 struct wpa_supplicant *wpa_s)
1963 if (!wpa_s->pending_interface_name[0] ||
1964 is_zero_ether_addr(wpa_s->pending_interface_addr))
1965 return; /* No pending virtual interface */
1967 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1968 wpa_s->pending_interface_name);
1969 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1970 wpa_s->pending_interface_name);
1971 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1972 wpa_s->pending_interface_name[0] = '\0';
1973 wpa_s->global->pending_group_iface_for_p2ps = 0;
1977 static struct wpa_supplicant *
1978 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1980 struct wpa_interface iface;
1981 struct wpa_supplicant *group_wpa_s;
1983 if (!wpa_s->pending_interface_name[0]) {
1984 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1985 if (!wpas_p2p_create_iface(wpa_s))
1988 * Something has forced us to remove the pending interface; try
1989 * to create a new one and hope for the best that we will get
1990 * the same local address.
1992 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1993 WPA_IF_P2P_CLIENT) < 0)
1997 os_memset(&iface, 0, sizeof(iface));
1998 iface.ifname = wpa_s->pending_interface_name;
1999 iface.driver = wpa_s->driver->name;
2000 if (wpa_s->conf->ctrl_interface == NULL &&
2001 wpa_s->parent != wpa_s &&
2003 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2004 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2006 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
2007 iface.driver_param = wpa_s->conf->driver_param;
2008 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
2009 if (group_wpa_s == NULL) {
2010 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2011 "wpa_supplicant interface");
2014 wpa_s->pending_interface_name[0] = '\0';
2015 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2016 P2P_GROUP_INTERFACE_CLIENT;
2017 wpa_s->global->p2p_group_formation = group_wpa_s;
2018 wpa_s->global->pending_group_iface_for_p2ps = 0;
2020 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2026 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2029 struct wpa_supplicant *wpa_s = eloop_ctx;
2030 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2031 wpas_p2p_group_formation_failed(wpa_s, 0);
2035 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2036 int already_deleted)
2038 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2039 wpa_s->parent, NULL);
2040 if (wpa_s->global->p2p)
2041 p2p_group_formation_failed(wpa_s->global->p2p);
2042 wpas_group_formation_completed(wpa_s, 0, already_deleted);
2046 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2048 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2049 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2050 wpa_s->parent, NULL);
2051 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2052 wpa_s->parent, NULL);
2053 wpa_s->global->p2p_fail_on_wps_complete = 0;
2057 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2059 if (wpa_s->global->p2p_group_formation != wpa_s)
2061 /* Speed up group formation timeout since this cannot succeed */
2062 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2063 wpa_s->parent, NULL);
2064 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2065 wpa_s->parent, NULL);
2069 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2071 struct wpa_supplicant *wpa_s = ctx;
2073 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2074 wpa_drv_cancel_remain_on_channel(wpa_s);
2075 wpa_s->off_channel_freq = 0;
2076 wpa_s->roc_waiting_drv_freq = 0;
2080 wpa_msg_global(wpa_s, MSG_INFO,
2081 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2083 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2084 wpas_p2p_remove_pending_group_interface(wpa_s);
2088 if (!res->role_go) {
2089 /* Inform driver of the operating channel of GO. */
2090 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2093 if (wpa_s->p2p_go_ht40)
2095 if (wpa_s->p2p_go_vht)
2098 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2099 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2101 res->role_go ? "GO" : "client", res->freq, res->ht40,
2102 MAC2STR(res->peer_device_addr),
2103 MAC2STR(res->peer_interface_addr),
2104 p2p_wps_method_text(res->wps_method));
2105 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2107 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2108 struct wpa_ssid *ssid;
2109 ssid = wpa_config_get_network(wpa_s->conf,
2110 wpa_s->p2p_persistent_id);
2111 if (ssid && ssid->disabled == 2 &&
2112 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2113 size_t len = os_strlen(ssid->passphrase);
2114 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2115 "on requested persistent group");
2116 os_memcpy(res->passphrase, ssid->passphrase, len);
2117 res->passphrase[len] = '\0';
2121 if (wpa_s->create_p2p_iface) {
2122 struct wpa_supplicant *group_wpa_s =
2123 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2124 if (group_wpa_s == NULL) {
2125 wpas_p2p_remove_pending_group_interface(wpa_s);
2126 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2128 wpas_p2p_group_formation_failed(wpa_s, 1);
2131 if (group_wpa_s != wpa_s) {
2132 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2133 sizeof(group_wpa_s->p2p_pin));
2134 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2136 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2137 wpa_s->pending_interface_name[0] = '\0';
2138 group_wpa_s->p2p_in_provisioning = 1;
2141 wpas_start_wps_go(group_wpa_s, res, 1);
2143 wpas_start_wps_enrollee(group_wpa_s, res);
2145 wpa_s->p2p_in_provisioning = 1;
2146 wpa_s->global->p2p_group_formation = wpa_s;
2149 wpas_start_wps_go(wpa_s, res, 1);
2151 wpas_start_wps_enrollee(ctx, res);
2154 wpa_s->p2p_long_listen = 0;
2155 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2157 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2158 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2159 (res->peer_config_timeout % 100) * 10000,
2160 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2164 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2167 struct wpa_supplicant *wpa_s = ctx;
2168 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2169 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2170 dev_passwd_id, go_intent);
2172 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
2176 static void wpas_dev_found(void *ctx, const u8 *addr,
2177 const struct p2p_peer_info *info,
2180 #ifndef CONFIG_NO_STDOUT_DEBUG
2181 struct wpa_supplicant *wpa_s = ctx;
2182 char devtype[WPS_DEV_TYPE_BUFSIZE];
2183 char *wfd_dev_info_hex = NULL;
2185 #ifdef CONFIG_WIFI_DISPLAY
2186 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2187 WFD_SUBELEM_DEVICE_INFO);
2188 #endif /* CONFIG_WIFI_DISPLAY */
2190 if (info->p2ps_instance) {
2192 const u8 *buf = wpabuf_head(info->p2ps_instance);
2193 size_t len = wpabuf_len(info->p2ps_instance);
2200 if (len < 4 + 2 + 1)
2202 id = WPA_GET_LE32(buf);
2204 methods = WPA_GET_BE16(buf);
2207 if (str_len > len - 4 - 2 - 1)
2209 os_memcpy(str, buf, str_len);
2210 str[str_len] = '\0';
2212 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2214 wpa_msg_global(wpa_s, MSG_INFO,
2215 P2P_EVENT_DEVICE_FOUND MACSTR
2216 " p2p_dev_addr=" MACSTR
2217 " pri_dev_type=%s name='%s'"
2218 " config_methods=0x%x"
2221 " adv_id=%x asp_svc=%s%s",
2223 MAC2STR(info->p2p_device_addr),
2224 wps_dev_type_bin2str(
2226 devtype, sizeof(devtype)),
2227 info->device_name, methods,
2228 info->dev_capab, info->group_capab,
2230 info->vendor_elems ?
2231 " vendor_elems=1" : "");
2236 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2237 " p2p_dev_addr=" MACSTR
2238 " pri_dev_type=%s name='%s' config_methods=0x%x "
2239 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2240 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2241 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2243 info->device_name, info->config_methods,
2244 info->dev_capab, info->group_capab,
2245 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2246 wfd_dev_info_hex ? wfd_dev_info_hex : "",
2247 info->vendor_elems ? " vendor_elems=1" : "",
2251 os_free(wfd_dev_info_hex);
2252 #endif /* CONFIG_NO_STDOUT_DEBUG */
2254 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2258 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2260 struct wpa_supplicant *wpa_s = ctx;
2262 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2263 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2265 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2269 static void wpas_find_stopped(void *ctx)
2271 struct wpa_supplicant *wpa_s = ctx;
2272 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2273 wpas_notify_p2p_find_stopped(wpa_s);
2277 struct wpas_p2p_listen_work {
2279 unsigned int duration;
2280 struct wpabuf *probe_resp_ie;
2284 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2288 wpabuf_free(lwork->probe_resp_ie);
2293 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2295 struct wpas_p2p_listen_work *lwork;
2297 if (!wpa_s->p2p_listen_work)
2300 lwork = wpa_s->p2p_listen_work->ctx;
2301 wpas_p2p_listen_work_free(lwork);
2302 radio_work_done(wpa_s->p2p_listen_work);
2303 wpa_s->p2p_listen_work = NULL;
2307 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2309 struct wpa_supplicant *wpa_s = work->wpa_s;
2310 struct wpas_p2p_listen_work *lwork = work->ctx;
2311 unsigned int duration;
2314 if (work->started) {
2315 wpa_s->p2p_listen_work = NULL;
2316 wpas_stop_listen(wpa_s);
2318 wpas_p2p_listen_work_free(lwork);
2322 wpa_s->p2p_listen_work = work;
2324 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2326 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2327 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2328 "report received Probe Request frames");
2329 wpas_p2p_listen_work_done(wpa_s);
2333 wpa_s->pending_listen_freq = lwork->freq;
2334 wpa_s->pending_listen_duration = lwork->duration;
2336 duration = lwork->duration;
2337 #ifdef CONFIG_TESTING_OPTIONS
2338 if (wpa_s->extra_roc_dur) {
2339 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2340 duration, duration + wpa_s->extra_roc_dur);
2341 duration += wpa_s->extra_roc_dur;
2343 #endif /* CONFIG_TESTING_OPTIONS */
2345 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2346 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2347 "to remain on channel (%u MHz) for Listen "
2348 "state", lwork->freq);
2349 wpas_p2p_listen_work_done(wpa_s);
2350 wpa_s->pending_listen_freq = 0;
2353 wpa_s->off_channel_freq = 0;
2354 wpa_s->roc_waiting_drv_freq = lwork->freq;
2358 static int wpas_start_listen(void *ctx, unsigned int freq,
2359 unsigned int duration,
2360 const struct wpabuf *probe_resp_ie)
2362 struct wpa_supplicant *wpa_s = ctx;
2363 struct wpas_p2p_listen_work *lwork;
2365 if (wpa_s->p2p_listen_work) {
2366 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2370 lwork = os_zalloc(sizeof(*lwork));
2374 lwork->duration = duration;
2375 if (probe_resp_ie) {
2376 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2377 if (lwork->probe_resp_ie == NULL) {
2378 wpas_p2p_listen_work_free(lwork);
2383 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2385 wpas_p2p_listen_work_free(lwork);
2393 static void wpas_stop_listen(void *ctx)
2395 struct wpa_supplicant *wpa_s = ctx;
2396 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2397 wpa_drv_cancel_remain_on_channel(wpa_s);
2398 wpa_s->off_channel_freq = 0;
2399 wpa_s->roc_waiting_drv_freq = 0;
2401 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2404 * Don't cancel Probe Request RX reporting for a connected P2P Client
2405 * handling Probe Request frames.
2407 if (!wpa_s->p2p_cli_probe)
2408 wpa_drv_probe_req_report(wpa_s, 0);
2410 wpas_p2p_listen_work_done(wpa_s);
2414 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2417 struct wpa_supplicant *wpa_s = ctx;
2418 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2423 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2424 const u8 *peer, const char *params,
2425 unsigned int generated_pin)
2427 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2428 " %08d%s", MAC2STR(peer), generated_pin, params);
2432 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2433 const u8 *peer, const char *params)
2435 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2436 "%s", MAC2STR(peer), params);
2440 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2441 const u8 *dev_addr, const u8 *pri_dev_type,
2442 const char *dev_name, u16 supp_config_methods,
2443 u8 dev_capab, u8 group_capab, const u8 *group_id,
2444 size_t group_id_len)
2446 struct wpa_supplicant *wpa_s = ctx;
2447 char devtype[WPS_DEV_TYPE_BUFSIZE];
2449 u8 empty_dev_type[8];
2450 unsigned int generated_pin = 0;
2451 struct wpa_supplicant *group = NULL;
2455 for (group = wpa_s->global->ifaces; group; group = group->next)
2457 struct wpa_ssid *s = group->current_ssid;
2459 s->mode == WPAS_MODE_P2P_GO &&
2460 group_id_len - ETH_ALEN == s->ssid_len &&
2461 os_memcmp(group_id + ETH_ALEN, s->ssid,
2467 if (pri_dev_type == NULL) {
2468 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2469 pri_dev_type = empty_dev_type;
2471 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2472 " pri_dev_type=%s name='%s' config_methods=0x%x "
2473 "dev_capab=0x%x group_capab=0x%x%s%s",
2475 wps_dev_type_bin2str(pri_dev_type, devtype,
2477 dev_name, supp_config_methods, dev_capab, group_capab,
2478 group ? " group=" : "",
2479 group ? group->ifname : "");
2480 if (os_snprintf_error(sizeof(params), res))
2481 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
2482 params[sizeof(params) - 1] = '\0';
2484 if (config_methods & WPS_CONFIG_DISPLAY) {
2485 generated_pin = wps_generate_pin();
2486 wpas_prov_disc_local_display(wpa_s, peer, params,
2488 } else if (config_methods & WPS_CONFIG_KEYPAD)
2489 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2490 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2491 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2492 MACSTR "%s", MAC2STR(peer), params);
2494 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2495 P2P_PROV_DISC_SUCCESS,
2496 config_methods, generated_pin);
2500 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2502 struct wpa_supplicant *wpa_s = ctx;
2503 unsigned int generated_pin = 0;
2506 if (wpa_s->pending_pd_before_join &&
2507 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2508 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2509 wpa_s->pending_pd_before_join = 0;
2510 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2511 "join-existing-group operation");
2512 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2516 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2517 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2520 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2521 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2522 if (os_snprintf_error(sizeof(params), res))
2523 params[sizeof(params) - 1] = '\0';
2527 if (config_methods & WPS_CONFIG_DISPLAY)
2528 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2529 else if (config_methods & WPS_CONFIG_KEYPAD) {
2530 generated_pin = wps_generate_pin();
2531 wpas_prov_disc_local_display(wpa_s, peer, params,
2533 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2534 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2535 MACSTR "%s", MAC2STR(peer), params);
2537 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2538 P2P_PROV_DISC_SUCCESS,
2539 config_methods, generated_pin);
2543 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2544 enum p2p_prov_disc_status status,
2545 u32 adv_id, const u8 *adv_mac,
2546 const char *deferred_session_resp)
2548 struct wpa_supplicant *wpa_s = ctx;
2550 if (wpa_s->p2p_fallback_to_go_neg) {
2551 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2552 "failed - fall back to GO Negotiation");
2553 wpa_msg_global(wpa_s->parent, MSG_INFO,
2554 P2P_EVENT_FALLBACK_TO_GO_NEG
2555 "reason=PD-failed");
2556 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2560 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2561 wpa_s->pending_pd_before_join = 0;
2562 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2563 "join-existing-group operation (no ACK for PD "
2565 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2569 if (adv_id && adv_mac && deferred_session_resp) {
2570 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2571 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2572 " deferred_session_resp='%s'",
2573 MAC2STR(peer), status, adv_id,
2574 deferred_session_resp);
2575 } else if (adv_id && adv_mac) {
2576 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2577 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2578 MAC2STR(peer), status, adv_id);
2580 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2581 " p2p_dev_addr=" MACSTR " status=%d",
2582 MAC2STR(peer), status);
2585 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2590 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2592 if (channels == NULL)
2593 return 1; /* Assume no restrictions */
2594 return p2p_channels_includes_freq(channels, freq);
2600 * Pick the best frequency to use from all the currently used frequencies.
2602 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2603 struct wpa_used_freq_data *freqs,
2608 /* find a candidate freq that is supported by P2P */
2609 for (c = 0; c < num; c++)
2610 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2616 /* once we have a candidate, try to find a 'better' one */
2617 for (i = c + 1; i < num; i++) {
2618 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2622 * 1. Infrastructure station interfaces have higher preference.
2623 * 2. P2P Clients have higher preference.
2626 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2631 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2634 return freqs[c].freq;
2638 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2639 const u8 *go_dev_addr, const u8 *ssid,
2640 size_t ssid_len, int *go, u8 *group_bssid,
2641 int *force_freq, int persistent_group,
2642 const struct p2p_channels *channels,
2645 struct wpa_supplicant *wpa_s = ctx;
2647 struct wpa_used_freq_data *freqs;
2648 struct wpa_supplicant *grp;
2651 if (!persistent_group) {
2652 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2653 " to join an active group (SSID: %s)",
2654 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2655 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2656 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2658 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2659 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2660 "authorized invitation");
2664 #ifdef CONFIG_WPS_NFC
2665 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2666 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
2667 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2668 wpa_s->p2p_wps_method = WPS_NFC;
2669 wpa_s->pending_join_wps_method = WPS_NFC;
2670 os_memcpy(wpa_s->pending_join_dev_addr,
2671 go_dev_addr, ETH_ALEN);
2672 os_memcpy(wpa_s->pending_join_iface_addr,
2676 #endif /* CONFIG_WPS_NFC */
2679 * Do not accept the invitation automatically; notify user and
2682 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2685 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2687 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2688 "running persistent group");
2690 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2694 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2695 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2696 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2697 "invitation to re-invoke a persistent group");
2698 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2699 } else if (!wpa_s->conf->persistent_reconnect)
2700 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2702 for (s = wpa_s->conf->ssid; s; s = s->next) {
2703 if (s->disabled == 2 &&
2704 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2705 s->ssid_len == ssid_len &&
2706 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2711 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2712 " requested reinvocation of an unknown group",
2714 return P2P_SC_FAIL_UNKNOWN_GROUP;
2717 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2719 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2720 wpa_printf(MSG_DEBUG, "P2P: The only available "
2721 "interface is already in use - reject "
2723 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2725 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2726 } else if (s->mode == WPAS_MODE_P2P_GO) {
2728 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2730 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2731 "interface address for the group");
2732 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2734 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2739 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2742 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2743 sizeof(struct wpa_used_freq_data));
2745 int num_channels = wpa_s->num_multichan_concurrent;
2746 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2747 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2751 /* Get one of the frequencies currently in use */
2752 if (best_freq > 0) {
2753 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2754 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
2756 if (wpa_s->num_multichan_concurrent < 2 ||
2757 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2758 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2759 *force_freq = best_freq;
2763 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2764 wpas_p2p_num_unused_channels(wpa_s) > 0) {
2766 /* We are the client */
2767 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2768 "running a GO but we are capable of MCC, "
2769 "figure out the best channel to use");
2771 } else if (!freq_included(channels, *force_freq)) {
2772 /* We are the GO, and *force_freq is not in the
2774 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2775 "in intersection but we are capable of MCC, "
2776 "figure out the best channel to use",
2782 return P2P_SC_SUCCESS;
2786 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2787 const u8 *ssid, size_t ssid_len,
2788 const u8 *go_dev_addr, u8 status,
2791 struct wpa_supplicant *wpa_s = ctx;
2794 for (s = wpa_s->conf->ssid; s; s = s->next) {
2795 if (s->disabled == 2 &&
2796 s->ssid_len == ssid_len &&
2797 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2801 if (status == P2P_SC_SUCCESS) {
2802 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2803 " was accepted; op_freq=%d MHz, SSID=%s",
2804 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
2806 int go = s->mode == WPAS_MODE_P2P_GO;
2807 wpas_p2p_group_add_persistent(
2808 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
2809 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
2811 wpa_s->user_initiated_pd = 0;
2812 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2813 wpa_s->p2p_wps_method, 0, op_freq,
2819 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2820 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2821 " was rejected (status %u)", MAC2STR(sa), status);
2827 wpa_msg_global(wpa_s, MSG_INFO,
2828 P2P_EVENT_INVITATION_RECEIVED
2829 "sa=" MACSTR " go_dev_addr=" MACSTR
2830 " bssid=" MACSTR " unknown-network",
2831 MAC2STR(sa), MAC2STR(go_dev_addr),
2834 wpa_msg_global(wpa_s, MSG_INFO,
2835 P2P_EVENT_INVITATION_RECEIVED
2836 "sa=" MACSTR " go_dev_addr=" MACSTR
2838 MAC2STR(sa), MAC2STR(go_dev_addr));
2843 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2844 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2845 "sa=" MACSTR " persistent=%d freq=%d",
2846 MAC2STR(sa), s->id, op_freq);
2848 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2849 "sa=" MACSTR " persistent=%d",
2850 MAC2STR(sa), s->id);
2855 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2856 struct wpa_ssid *ssid,
2857 const u8 *peer, int inv)
2860 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
2865 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2866 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
2870 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
2871 if (ssid->mode != WPAS_MODE_P2P_GO &&
2872 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2873 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2874 "due to invitation result", ssid->id);
2875 wpas_notify_network_removed(wpa_s, ssid);
2876 wpa_config_remove_network(wpa_s->conf, ssid->id);
2879 return; /* Peer not found in client list */
2882 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2883 "group %d client list%s",
2884 MAC2STR(peer), ssid->id,
2885 inv ? " due to invitation result" : "");
2886 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
2887 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
2888 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
2889 ssid->num_p2p_clients--;
2890 if (p2p_wpa_s->conf->update_config &&
2891 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
2892 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2896 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2899 struct wpa_ssid *ssid;
2901 wpa_s = wpa_s->global->p2p_invite_group;
2903 return; /* No known invitation group */
2904 ssid = wpa_s->current_ssid;
2905 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2906 !ssid->p2p_persistent_group)
2907 return; /* Not operating as a GO in persistent group */
2908 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2909 ssid->ssid, ssid->ssid_len);
2910 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2914 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2915 const struct p2p_channels *channels,
2916 const u8 *peer, int neg_freq,
2919 struct wpa_supplicant *wpa_s = ctx;
2920 struct wpa_ssid *ssid;
2924 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2925 "status=%d " MACSTR,
2926 status, MAC2STR(bssid));
2928 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2929 "status=%d ", status);
2931 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2933 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2934 status, MAC2STR(peer));
2935 if (wpa_s->pending_invite_ssid_id == -1) {
2936 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2937 wpas_remove_persistent_client(wpa_s, peer);
2938 return; /* Invitation to active group */
2941 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2942 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2943 "invitation exchange to indicate readiness for "
2947 if (status != P2P_SC_SUCCESS) {
2948 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2949 ssid = wpa_config_get_network(
2950 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2951 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2953 wpas_p2p_remove_pending_group_interface(wpa_s);
2957 ssid = wpa_config_get_network(wpa_s->conf,
2958 wpa_s->pending_invite_ssid_id);
2960 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2961 "data matching with invitation");
2966 * The peer could have missed our ctrl::ack frame for Invitation
2967 * Response and continue retransmitting the frame. To reduce the
2968 * likelihood of the peer not getting successful TX status for the
2969 * Invitation Response frame, wait a short time here before starting
2970 * the persistent group so that we will remain on the current channel to
2971 * acknowledge any possible retransmission from the peer.
2973 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2974 "starting persistent group");
2977 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2978 freq_included(channels, neg_freq))
2980 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
2981 freq_included(channels, peer_oper_freq))
2982 freq = peer_oper_freq;
2986 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
2987 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2988 wpas_p2p_group_add_persistent(wpa_s, ssid,
2989 ssid->mode == WPAS_MODE_P2P_GO,
2990 wpa_s->p2p_persistent_go_freq,
2992 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2994 ssid->mode == WPAS_MODE_P2P_GO ?
2995 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3000 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3003 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3005 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3009 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3011 reg->channel[reg->channels] = chan;
3016 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3017 struct p2p_channels *chan,
3018 struct p2p_channels *cli_chan)
3022 wpa_s->global->p2p_24ghz_social_channels = 1;
3024 os_memset(cli_chan, 0, sizeof(*cli_chan));
3026 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3029 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3030 chan->reg_class[cla].reg_class = 81;
3031 chan->reg_class[cla].channels = 0;
3032 for (i = 0; i < 11; i++) {
3033 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3034 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3036 if (chan->reg_class[cla].channels)
3039 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3042 /* Operating class 115 - 5 GHz, channels 36-48 */
3043 chan->reg_class[cla].reg_class = 115;
3044 chan->reg_class[cla].channels = 0;
3045 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3046 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3047 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3048 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3049 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3050 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3051 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3052 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3053 if (chan->reg_class[cla].channels)
3056 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3059 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3060 chan->reg_class[cla].reg_class = 124;
3061 chan->reg_class[cla].channels = 0;
3062 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3063 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3064 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3065 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3066 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3067 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3068 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3069 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3070 if (chan->reg_class[cla].channels)
3073 chan->reg_classes = cla;
3078 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3080 enum hostapd_hw_mode mode)
3084 for (i = 0; i < num_modes; i++) {
3085 if (modes[i].mode == mode)
3094 NOT_ALLOWED, NO_IR, ALLOWED
3097 static int has_channel(struct wpa_global *global,
3098 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3103 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3105 if (wpas_p2p_disallowed_freq(global, freq))
3108 for (i = 0; i < mode->num_channels; i++) {
3109 if (mode->channels[i].chan == chan) {
3111 *flags = mode->channels[i].flag;
3112 if (mode->channels[i].flag &
3113 (HOSTAPD_CHAN_DISABLED |
3114 HOSTAPD_CHAN_RADAR))
3116 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3126 struct p2p_oper_class_map {
3127 enum hostapd_hw_mode mode;
3132 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3135 static const struct p2p_oper_class_map op_class[] = {
3136 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3137 #if 0 /* Do not enable HT40 on 2 GHz for now */
3138 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3139 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3141 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3142 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3143 { HOSTAPD_MODE_IEEE80211A, 125, 149, 169, 4, BW20 },
3144 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3145 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3146 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3147 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3150 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3151 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3152 * 80 MHz, but currently use the following definition for simplicity
3153 * (these center frequencies are not actual channels, which makes
3154 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3155 * removing invalid channels.
3157 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3158 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3159 { -1, 0, 0, 0, 0, BW20 }
3163 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3164 struct hostapd_hw_modes *mode,
3167 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3170 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3173 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3175 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3176 * so the center channel is 6 channels away from the start/end.
3178 if (channel >= center_channels[i] - 6 &&
3179 channel <= center_channels[i] + 6)
3180 return center_channels[i];
3186 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3187 struct hostapd_hw_modes *mode,
3192 enum chan_allowed res, ret = ALLOWED;
3194 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3197 if (center_chan >= 58 && center_chan <= 138)
3198 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3200 /* check all the channels are available */
3201 for (i = 0; i < 4; i++) {
3202 int adj_chan = center_chan - 6 + i * 4;
3204 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3205 if (res == NOT_ALLOWED)
3210 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3212 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3214 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3216 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3224 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3225 struct hostapd_hw_modes *mode,
3229 enum chan_allowed res, res2;
3231 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3232 if (bw == BW40MINUS) {
3233 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3235 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3236 } else if (bw == BW40PLUS) {
3237 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3239 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3240 } else if (bw == BW80) {
3241 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3244 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3246 if (res == NO_IR || res2 == NO_IR)
3252 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3253 struct p2p_channels *chan,
3254 struct p2p_channels *cli_chan)
3256 struct hostapd_hw_modes *mode;
3257 int cla, op, cli_cla;
3259 if (wpa_s->hw.modes == NULL) {
3260 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3261 "of all supported channels; assume dualband "
3263 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3268 for (op = 0; op_class[op].op_class; op++) {
3269 const struct p2p_oper_class_map *o = &op_class[op];
3271 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3273 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3276 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3277 wpa_s->global->p2p_24ghz_social_channels = 1;
3278 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3279 enum chan_allowed res;
3280 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3281 if (res == ALLOWED) {
3283 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3285 reg = &chan->reg_class[cla];
3287 reg->reg_class = o->op_class;
3289 reg->channel[reg->channels] = ch;
3291 } else if (res == NO_IR &&
3292 wpa_s->conf->p2p_add_cli_chan) {
3293 if (cli_reg == NULL) {
3294 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3296 cli_reg = &cli_chan->reg_class[cli_cla];
3298 cli_reg->reg_class = o->op_class;
3300 cli_reg->channel[cli_reg->channels] = ch;
3301 cli_reg->channels++;
3305 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3306 reg->channel, reg->channels);
3309 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3310 cli_reg->channel, cli_reg->channels);
3314 chan->reg_classes = cla;
3315 cli_chan->reg_classes = cli_cla;
3321 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3322 struct hostapd_hw_modes *mode, u8 channel)
3325 enum chan_allowed ret;
3327 for (op = 0; op_class[op].op_class; op++) {
3328 const struct p2p_oper_class_map *o = &op_class[op];
3331 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3332 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3333 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3336 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3338 return (o->bw == BW40MINUS) ? -1 : 1;
3345 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3346 struct hostapd_hw_modes *mode, u8 channel)
3348 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3351 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3355 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3358 struct wpa_supplicant *wpa_s = ctx;
3360 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3361 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3367 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3371 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3372 const u8 *ssid, size_t ssid_len)
3374 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3375 struct wpa_ssid *s = wpa_s->current_ssid;
3378 if (s->mode != WPAS_MODE_P2P_GO &&
3379 s->mode != WPAS_MODE_AP &&
3380 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3382 if (s->ssid_len != ssid_len ||
3383 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3393 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3394 const u8 *peer_dev_addr)
3396 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3397 struct wpa_ssid *ssid = wpa_s->current_ssid;
3398 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
3400 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3408 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3410 struct wpa_supplicant *wpa_s = ctx;
3412 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3416 static int wpas_is_concurrent_session_active(void *ctx)
3418 struct wpa_supplicant *wpa_s = ctx;
3419 struct wpa_supplicant *ifs;
3421 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3424 if (ifs->wpa_state > WPA_ASSOCIATED)
3431 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3433 struct wpa_supplicant *wpa_s = ctx;
3434 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3438 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3439 const char *conf_p2p_dev)
3441 struct wpa_interface iface;
3442 struct wpa_supplicant *p2pdev_wpa_s;
3444 char force_name[100];
3447 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3449 if (os_snprintf_error(sizeof(ifname), ret))
3451 force_name[0] = '\0';
3452 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3453 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3454 force_name, wpa_s->pending_interface_addr, NULL);
3456 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3459 os_strlcpy(wpa_s->pending_interface_name, ifname,
3460 sizeof(wpa_s->pending_interface_name));
3462 os_memset(&iface, 0, sizeof(iface));
3464 iface.ifname = wpa_s->pending_interface_name;
3465 iface.driver = wpa_s->driver->name;
3466 iface.driver_param = wpa_s->conf->driver_param;
3469 * If a P2P Device configuration file was given, use it as the interface
3470 * configuration file (instead of using parent's configuration file.
3473 iface.confname = conf_p2p_dev;
3474 iface.ctrl_interface = NULL;
3476 iface.confname = wpa_s->confname;
3477 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3480 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
3481 if (!p2pdev_wpa_s) {
3482 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3486 wpa_s->pending_interface_name[0] = '\0';
3491 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3492 const u8 *noa, size_t noa_len)
3494 struct wpa_supplicant *wpa_s, *intf = ctx;
3497 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3498 if (wpa_s->waiting_presence_resp)
3502 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3505 wpa_s->waiting_presence_resp = 0;
3507 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3508 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3509 " status=%u noa=%s", MAC2STR(src), status, hex);
3513 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3514 size_t ssid_len, u8 *go_dev_addr,
3515 u8 *ret_ssid, size_t *ret_ssid_len,
3516 u8 *intended_iface_addr)
3518 struct wpa_supplicant *wpa_s = ctx;
3521 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3523 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3524 *ret_ssid_len = s->ssid_len;
3525 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3527 if (s->mode != WPAS_MODE_P2P_GO) {
3528 os_memset(intended_iface_addr, 0, ETH_ALEN);
3529 } else if (wpas_p2p_create_iface(wpa_s)) {
3530 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3533 os_memcpy(intended_iface_addr,
3534 wpa_s->pending_interface_addr, ETH_ALEN);
3536 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3546 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3547 u8 *ssid, size_t *ssid_len, int *group_iface)
3549 struct wpa_supplicant *wpa_s = ctx;
3554 * group_iface will be set to 1 only if a dedicated interface for P2P
3555 * role is required. First, we try to reuse an active GO. However,
3556 * if it is not present, we will try to reactivate an existing
3557 * persistent group and set group_iface to 1, so the caller will know
3558 * that the pending interface should be used.
3561 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
3563 s = wpas_p2p_get_persistent_go(wpa_s);
3564 *group_iface = wpas_p2p_create_iface(wpa_s);
3566 os_memcpy(bssid, s->bssid, ETH_ALEN);
3571 os_memcpy(intended_addr, bssid, ETH_ALEN);
3572 os_memcpy(ssid, s->ssid, s->ssid_len);
3573 *ssid_len = s->ssid_len;
3579 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3580 const u8 *ssid, size_t ssid_len)
3582 struct wpa_supplicant *wpa_s = ctx;
3584 int save_config = 0;
3587 /* Start with our first choice of Persistent Groups */
3588 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3589 if (go && ssid && ssid_len &&
3590 s->ssid_len == ssid_len &&
3591 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3592 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3595 /* Remove stale persistent group */
3596 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3597 wpa_config_remove_network(wpa_s->conf, s->id);
3602 for (i = 0; i < s->num_p2p_clients; i++) {
3603 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3604 peer, ETH_ALEN) != 0)
3607 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3608 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3609 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3612 s->num_p2p_clients--;
3617 p2p_config_write(wpa_s);
3619 /* Return TRUE if valid SSID remains */
3624 static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3625 const u8 *feat_cap, size_t feat_cap_len)
3627 static const char pref[] = " feature_cap=";
3633 * We expect a feature capability to contain at least one byte to be
3634 * reported. The string buffer provided by the caller function is
3635 * expected to be big enough to contain all bytes of the attribute for
3636 * known specifications. This function truncates the reported bytes if
3637 * the feature capability data exceeds the string buffer size.
3639 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3642 os_memcpy(buf, pref, sizeof(pref));
3643 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3644 buf_len - sizeof(pref) + 1,
3645 feat_cap, feat_cap_len);
3647 if (ret != (2 * (int) feat_cap_len))
3648 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3652 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3653 const u8 *adv_mac, const u8 *ses_mac,
3654 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3655 u8 conncap, int passwd_id,
3656 const u8 *persist_ssid,
3657 size_t persist_ssid_size, int response_done,
3658 int prov_start, const char *session_info,
3659 const u8 *feat_cap, size_t feat_cap_len)
3661 struct wpa_supplicant *wpa_s = ctx;
3663 struct wpa_ssid *persistent_go, *stale, *s;
3664 int save_config = 0;
3665 struct wpa_supplicant *go_wpa_s;
3666 char feat_cap_str[256];
3671 os_memset(mac, 0, ETH_ALEN);
3679 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3680 feat_cap, feat_cap_len);
3683 if (session_info == NULL) {
3684 wpa_msg_global(wpa_s, MSG_INFO,
3685 P2P_EVENT_P2PS_PROVISION_START MACSTR
3686 " adv_id=%x conncap=%x"
3688 " session=%x mac=" MACSTR
3689 " dev_passwd_id=%d%s",
3690 MAC2STR(dev), adv_id, conncap,
3692 ses_id, MAC2STR(ses_mac),
3693 passwd_id, feat_cap_str);
3695 wpa_msg_global(wpa_s, MSG_INFO,
3696 P2P_EVENT_P2PS_PROVISION_START MACSTR
3697 " adv_id=%x conncap=%x"
3699 " session=%x mac=" MACSTR
3700 " dev_passwd_id=%d info='%s'%s",
3701 MAC2STR(dev), adv_id, conncap,
3703 ses_id, MAC2STR(ses_mac),
3704 passwd_id, session_info, feat_cap_str);
3709 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3710 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3712 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3713 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3714 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3716 if (persistent_go && !persistent_go->num_p2p_clients) {
3717 /* remove empty persistent GO */
3718 wpa_config_remove_network(wpa_s->conf,
3722 wpa_msg_global(wpa_s, MSG_INFO,
3723 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3725 " adv_id=%x adv_mac=" MACSTR
3726 " session=%x mac=" MACSTR "%s",
3727 MAC2STR(dev), status,
3728 adv_id, MAC2STR(adv_mac),
3729 ses_id, MAC2STR(ses_mac), feat_cap_str);
3733 /* Clean up stale persistent groups with this device */
3734 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3737 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
3738 os_memcmp(grp_mac, mac, ETH_ALEN) == 0) {
3739 wpa_dbg(wpa_s, MSG_ERROR,
3740 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
3745 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3749 if (s && s->ssid_len == stale->ssid_len &&
3750 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3751 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3754 /* Remove stale persistent group */
3755 if (stale->mode != WPAS_MODE_P2P_GO ||
3756 stale->num_p2p_clients <= 1) {
3757 wpa_config_remove_network(wpa_s->conf, stale->id);
3761 for (i = 0; i < stale->num_p2p_clients; i++) {
3762 if (os_memcmp(stale->p2p_client_list +
3764 dev, ETH_ALEN) == 0) {
3765 os_memmove(stale->p2p_client_list +
3767 stale->p2p_client_list +
3769 (stale->num_p2p_clients -
3774 stale->num_p2p_clients--;
3780 p2p_config_write(wpa_s);
3783 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3784 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3786 if (persistent_go && s != persistent_go &&
3787 !persistent_go->num_p2p_clients) {
3788 /* remove empty persistent GO */
3789 wpa_config_remove_network(wpa_s->conf,
3794 wpa_msg_global(wpa_s, MSG_INFO,
3795 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3797 " adv_id=%x adv_mac=" MACSTR
3798 " session=%x mac=" MACSTR
3800 MAC2STR(dev), status,
3801 adv_id, MAC2STR(adv_mac),
3802 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
3806 if (conncap == P2PS_SETUP_GROUP_OWNER) {
3808 * We need to copy the interface name. Simply saving a
3809 * pointer isn't enough, since if we use pending_interface_name
3810 * it will be overwritten when the group is added.
3812 char go_ifname[100];
3814 go_ifname[0] = '\0';
3816 wpa_s->global->pending_p2ps_group = 1;
3818 if (!wpas_p2p_create_iface(wpa_s))
3819 os_memcpy(go_ifname, wpa_s->ifname,
3821 else if (wpa_s->pending_interface_name[0])
3822 os_memcpy(go_ifname,
3823 wpa_s->pending_interface_name,
3826 if (!go_ifname[0]) {
3827 wpas_p2ps_prov_complete(
3828 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3829 dev, adv_mac, ses_mac,
3830 NULL, adv_id, ses_id, 0, 0,
3831 NULL, 0, 0, 0, NULL, NULL, 0);
3835 /* If PD Resp complete, start up the GO */
3836 if (response_done && persistent_go) {
3837 wpas_p2p_group_add_persistent(
3838 wpa_s, persistent_go,
3839 0, 0, 0, 0, 0, NULL,
3840 persistent_go->mode ==
3842 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3844 } else if (response_done) {
3845 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3848 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3849 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3850 wpa_s->p2ps_join_addr_valid = 1;
3851 wpa_dbg(wpa_s, MSG_DEBUG,
3852 "P2PS: Saving PIN for " MACSTR,
3855 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3856 os_memcpy(go_ifname, go_wpa_s->ifname,
3859 wpa_dbg(go_wpa_s, MSG_DEBUG,
3860 "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
3861 wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
3864 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3865 wpa_s->p2ps_join_addr_valid = 1;
3866 wpa_dbg(wpa_s, MSG_DEBUG,
3867 "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
3870 wpa_msg_global(wpa_s, MSG_INFO,
3871 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3872 " status=%d conncap=%x"
3873 " adv_id=%x adv_mac=" MACSTR
3874 " session=%x mac=" MACSTR
3875 " dev_passwd_id=%d go=%s%s",
3876 MAC2STR(dev), status, conncap,
3877 adv_id, MAC2STR(adv_mac),
3878 ses_id, MAC2STR(ses_mac),
3879 passwd_id, go_ifname, feat_cap_str);
3883 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3884 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3886 if (persistent_go && !persistent_go->num_p2p_clients) {
3887 /* remove empty persistent GO */
3888 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
3891 if (conncap == P2PS_SETUP_CLIENT) {
3892 wpa_msg_global(wpa_s, MSG_INFO,
3893 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3894 " status=%d conncap=%x"
3895 " adv_id=%x adv_mac=" MACSTR
3896 " session=%x mac=" MACSTR
3897 " dev_passwd_id=%d join=" MACSTR "%s",
3898 MAC2STR(dev), status, conncap,
3899 adv_id, MAC2STR(adv_mac),
3900 ses_id, MAC2STR(ses_mac),
3901 passwd_id, MAC2STR(grp_mac), feat_cap_str);
3903 wpa_msg_global(wpa_s, MSG_INFO,
3904 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3905 " status=%d conncap=%x"
3906 " adv_id=%x adv_mac=" MACSTR
3907 " session=%x mac=" MACSTR
3908 " dev_passwd_id=%d%s",
3909 MAC2STR(dev), status, conncap,
3910 adv_id, MAC2STR(adv_mac),
3911 ses_id, MAC2STR(ses_mac),
3912 passwd_id, feat_cap_str);
3917 static int _wpas_p2p_in_progress(void *ctx)
3919 struct wpa_supplicant *wpa_s = ctx;
3920 return wpas_p2p_in_progress(wpa_s);
3924 static int wpas_prov_disc_resp_cb(void *ctx)
3926 struct wpa_supplicant *wpa_s = ctx;
3927 struct wpa_ssid *persistent_go;
3929 if (!wpa_s->global->pending_p2ps_group)
3932 wpa_s->global->pending_p2ps_group = 0;
3934 if (wpas_p2p_get_go_group(wpa_s))
3936 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3938 if (persistent_go) {
3939 wpas_p2p_group_add_persistent(
3940 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
3941 persistent_go->mode == WPAS_MODE_P2P_GO ?
3942 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3944 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3951 static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
3953 unsigned int *freq_list)
3955 struct wpa_supplicant *wpa_s = ctx;
3957 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
3958 WPA_IF_P2P_CLIENT, len, freq_list);
3963 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3964 * @global: Pointer to global data from wpa_supplicant_init()
3965 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3966 * Returns: 0 on success, -1 on failure
3968 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3970 struct p2p_config p2p;
3973 if (wpa_s->conf->p2p_disabled)
3976 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3982 os_memset(&p2p, 0, sizeof(p2p));
3984 p2p.debug_print = wpas_p2p_debug_print;
3985 p2p.p2p_scan = wpas_p2p_scan;
3986 p2p.send_action = wpas_send_action;
3987 p2p.send_action_done = wpas_send_action_done;
3988 p2p.go_neg_completed = wpas_go_neg_completed;
3989 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3990 p2p.dev_found = wpas_dev_found;
3991 p2p.dev_lost = wpas_dev_lost;
3992 p2p.find_stopped = wpas_find_stopped;
3993 p2p.start_listen = wpas_start_listen;
3994 p2p.stop_listen = wpas_stop_listen;
3995 p2p.send_probe_resp = wpas_send_probe_resp;
3996 p2p.sd_request = wpas_sd_request;
3997 p2p.sd_response = wpas_sd_response;
3998 p2p.prov_disc_req = wpas_prov_disc_req;
3999 p2p.prov_disc_resp = wpas_prov_disc_resp;
4000 p2p.prov_disc_fail = wpas_prov_disc_fail;
4001 p2p.invitation_process = wpas_invitation_process;
4002 p2p.invitation_received = wpas_invitation_received;
4003 p2p.invitation_result = wpas_invitation_result;
4004 p2p.get_noa = wpas_get_noa;
4005 p2p.go_connected = wpas_go_connected;
4006 p2p.presence_resp = wpas_presence_resp;
4007 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4008 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4009 p2p.get_persistent_group = wpas_get_persistent_group;
4010 p2p.get_go_info = wpas_get_go_info;
4011 p2p.remove_stale_groups = wpas_remove_stale_groups;
4012 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4013 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4014 p2p.p2ps_group_capability = p2ps_group_capability;
4015 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
4017 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4018 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4019 p2p.dev_name = wpa_s->conf->device_name;
4020 p2p.manufacturer = wpa_s->conf->manufacturer;
4021 p2p.model_name = wpa_s->conf->model_name;
4022 p2p.model_number = wpa_s->conf->model_number;
4023 p2p.serial_number = wpa_s->conf->serial_number;
4025 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4026 p2p.config_methods = wpa_s->wps->config_methods;
4029 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4030 wpa_printf(MSG_ERROR,
4031 "P2P: Failed to configure supported channel list");
4035 if (wpa_s->conf->p2p_listen_reg_class &&
4036 wpa_s->conf->p2p_listen_channel) {
4037 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4038 p2p.channel = wpa_s->conf->p2p_listen_channel;
4039 p2p.channel_forced = 1;
4042 * Pick one of the social channels randomly as the listen
4045 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4046 &p2p.channel) != 0) {
4047 wpa_printf(MSG_INFO,
4048 "P2P: No social channels supported by the driver - do not enable P2P");
4051 p2p.channel_forced = 0;
4053 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4054 p2p.reg_class, p2p.channel);
4056 if (wpa_s->conf->p2p_oper_reg_class &&
4057 wpa_s->conf->p2p_oper_channel) {
4058 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4059 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4060 p2p.cfg_op_channel = 1;
4061 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4062 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4066 * Use random operation channel from 2.4 GHz band social
4067 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4068 * other preference is indicated.
4070 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4071 &p2p.op_channel) != 0) {
4072 wpa_printf(MSG_ERROR,
4073 "P2P: Failed to select random social channel as operation channel");
4076 p2p.cfg_op_channel = 0;
4077 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4078 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4081 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4082 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4083 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4086 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4087 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4088 p2p.country[2] = 0x04;
4090 os_memcpy(p2p.country, "XX\x04", 3);
4092 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4095 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4096 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4097 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4099 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4100 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4102 p2p.max_peers = 100;
4104 if (wpa_s->conf->p2p_ssid_postfix) {
4105 p2p.ssid_postfix_len =
4106 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4107 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4108 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4109 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4110 p2p.ssid_postfix_len);
4113 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4115 p2p.max_listen = wpa_s->max_remain_on_chan;
4117 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4118 wpa_s->conf->p2p_passphrase_len <= 63)
4119 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4121 p2p.passphrase_len = 8;
4123 global->p2p = p2p_init(&p2p);
4124 if (global->p2p == NULL)
4126 global->p2p_init_wpa_s = wpa_s;
4128 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4129 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4131 p2p_add_wps_vendor_extension(
4132 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4135 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4142 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4143 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4145 * This function deinitialize per-interface P2P data.
4147 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4149 if (wpa_s->driver && wpa_s->drv_priv)
4150 wpa_drv_probe_req_report(wpa_s, 0);
4152 if (wpa_s->go_params) {
4153 /* Clear any stored provisioning info */
4154 p2p_clear_provisioning_info(
4156 wpa_s->go_params->peer_device_addr);
4159 os_free(wpa_s->go_params);
4160 wpa_s->go_params = NULL;
4161 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4162 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4163 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4164 wpa_s->p2p_long_listen = 0;
4165 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4166 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4167 wpas_p2p_remove_pending_group_interface(wpa_s);
4168 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4169 wpas_p2p_listen_work_done(wpa_s);
4170 if (wpa_s->p2p_send_action_work) {
4171 os_free(wpa_s->p2p_send_action_work->ctx);
4172 radio_work_done(wpa_s->p2p_send_action_work);
4173 wpa_s->p2p_send_action_work = NULL;
4175 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4177 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4178 wpa_s->p2p_oob_dev_pw = NULL;
4180 os_free(wpa_s->p2p_group_common_freqs);
4181 wpa_s->p2p_group_common_freqs = NULL;
4182 wpa_s->p2p_group_common_freqs_num = 0;
4184 /* TODO: remove group interface from the driver if this wpa_s instance
4185 * is on top of a P2P group interface */
4190 * wpas_p2p_deinit_global - Deinitialize global P2P module
4191 * @global: Pointer to global data from wpa_supplicant_init()
4193 * This function deinitializes the global (per device) P2P module.
4195 static void wpas_p2p_deinit_global(struct wpa_global *global)
4197 struct wpa_supplicant *wpa_s, *tmp;
4199 wpa_s = global->ifaces;
4201 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4203 /* Remove remaining P2P group interfaces */
4204 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4205 wpa_s = wpa_s->next;
4207 tmp = global->ifaces;
4210 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4215 /* Disconnect from the P2P group and deinit the interface */
4216 wpas_p2p_disconnect(tmp);
4220 * Deinit GO data on any possibly remaining interface (if main
4221 * interface is used as GO).
4223 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4224 if (wpa_s->ap_iface)
4225 wpas_p2p_group_deinit(wpa_s);
4228 p2p_deinit(global->p2p);
4230 global->p2p_init_wpa_s = NULL;
4234 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4236 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4237 wpa_s->conf->p2p_no_group_iface)
4238 return 0; /* separate interface disabled per configuration */
4239 if (wpa_s->drv_flags &
4240 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4241 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4242 return 1; /* P2P group requires a new interface in every case
4244 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4245 return 0; /* driver does not support concurrent operations */
4246 if (wpa_s->global->ifaces->next)
4247 return 1; /* more that one interface already in use */
4248 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4249 return 1; /* this interface is already in use */
4254 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4255 const u8 *peer_addr,
4256 enum p2p_wps_method wps_method,
4257 int go_intent, const u8 *own_interface_addr,
4258 unsigned int force_freq, int persistent_group,
4259 struct wpa_ssid *ssid, unsigned int pref_freq)
4261 if (persistent_group && wpa_s->conf->persistent_reconnect)
4262 persistent_group = 2;
4265 * Increase GO config timeout if HT40 is used since it takes some time
4266 * to scan channels for coex purposes before the BSS can be started.
4268 p2p_set_config_timeout(wpa_s->global->p2p,
4269 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4271 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4272 go_intent, own_interface_addr, force_freq,
4273 persistent_group, ssid ? ssid->ssid : NULL,
4274 ssid ? ssid->ssid_len : 0,
4275 wpa_s->p2p_pd_before_go_neg, pref_freq,
4276 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4281 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4282 const u8 *peer_addr,
4283 enum p2p_wps_method wps_method,
4284 int go_intent, const u8 *own_interface_addr,
4285 unsigned int force_freq, int persistent_group,
4286 struct wpa_ssid *ssid, unsigned int pref_freq)
4288 if (persistent_group && wpa_s->conf->persistent_reconnect)
4289 persistent_group = 2;
4291 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4292 go_intent, own_interface_addr, force_freq,
4293 persistent_group, ssid ? ssid->ssid : NULL,
4294 ssid ? ssid->ssid_len : 0, pref_freq,
4295 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4300 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4302 wpa_s->p2p_join_scan_count++;
4303 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4304 wpa_s->p2p_join_scan_count);
4305 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4306 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4307 " for join operationg - stop join attempt",
4308 MAC2STR(wpa_s->pending_join_iface_addr));
4309 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4310 if (wpa_s->p2p_auto_pd) {
4311 wpa_s->p2p_auto_pd = 0;
4312 wpa_msg_global(wpa_s, MSG_INFO,
4313 P2P_EVENT_PROV_DISC_FAILURE
4314 " p2p_dev_addr=" MACSTR " status=N/A",
4315 MAC2STR(wpa_s->pending_join_dev_addr));
4318 wpa_msg_global(wpa_s->parent, MSG_INFO,
4319 P2P_EVENT_GROUP_FORMATION_FAILURE);
4324 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4327 unsigned int num, i;
4328 struct wpa_used_freq_data *freqs;
4330 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4331 /* Multiple channels are supported and not all are in use */
4335 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4336 sizeof(struct wpa_used_freq_data));
4340 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4341 wpa_s->num_multichan_concurrent);
4343 for (i = 0; i < num; i++) {
4344 if (freqs[i].freq == freq) {
4345 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4352 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4361 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4362 const u8 *peer_dev_addr)
4364 struct wpa_bss *bss;
4367 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4370 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4371 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4376 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4378 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4379 "%ld.%06ld (%supdated in last scan)",
4380 bss->last_update.sec, bss->last_update.usec,
4381 updated ? "": "not ");
4387 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4388 struct wpa_scan_results *scan_res)
4390 struct wpa_bss *bss = NULL;
4392 u8 iface_addr[ETH_ALEN];
4394 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4396 if (wpa_s->global->p2p_disabled)
4399 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4400 scan_res ? (int) scan_res->num : -1,
4401 wpa_s->p2p_auto_join ? "auto_" : "");
4404 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4406 if (wpa_s->p2p_auto_pd) {
4407 int join = wpas_p2p_peer_go(wpa_s,
4408 wpa_s->pending_join_dev_addr);
4410 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4411 wpa_s->auto_pd_scan_retry++;
4412 bss = wpa_bss_get_bssid_latest(
4413 wpa_s, wpa_s->pending_join_dev_addr);
4416 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4417 "the peer " MACSTR " at %d MHz",
4418 wpa_s->auto_pd_scan_retry,
4420 pending_join_dev_addr),
4422 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4430 wpa_s->p2p_auto_pd = 0;
4431 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4432 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4433 MAC2STR(wpa_s->pending_join_dev_addr), join);
4434 if (p2p_prov_disc_req(wpa_s->global->p2p,
4435 wpa_s->pending_join_dev_addr, NULL,
4436 wpa_s->pending_pd_config_methods, join,
4437 0, wpa_s->user_initiated_pd) < 0) {
4438 wpa_s->p2p_auto_pd = 0;
4439 wpa_msg_global(wpa_s, MSG_INFO,
4440 P2P_EVENT_PROV_DISC_FAILURE
4441 " p2p_dev_addr=" MACSTR " status=N/A",
4442 MAC2STR(wpa_s->pending_join_dev_addr));
4447 if (wpa_s->p2p_auto_join) {
4448 int join = wpas_p2p_peer_go(wpa_s,
4449 wpa_s->pending_join_dev_addr);
4451 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4452 "running a GO -> use GO Negotiation");
4453 wpa_msg_global(wpa_s->parent, MSG_INFO,
4454 P2P_EVENT_FALLBACK_TO_GO_NEG
4455 "reason=peer-not-running-GO");
4456 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4457 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4458 wpa_s->p2p_persistent_group, 0, 0, 0,
4459 wpa_s->p2p_go_intent,
4460 wpa_s->p2p_connect_freq,
4461 wpa_s->p2p_persistent_id,
4462 wpa_s->p2p_pd_before_go_neg,
4468 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4469 "try to join the group", join ? "" :
4472 wpa_msg_global(wpa_s->parent, MSG_INFO,
4473 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
4474 wpa_s->p2p_fallback_to_go_neg = 1;
4478 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4479 wpa_s->pending_join_iface_addr);
4481 p2p_get_interface_addr(wpa_s->global->p2p,
4482 wpa_s->pending_join_dev_addr,
4484 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4485 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4486 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4487 "address for join from " MACSTR " to " MACSTR
4488 " based on newly discovered P2P peer entry",
4489 MAC2STR(wpa_s->pending_join_iface_addr),
4490 MAC2STR(iface_addr));
4491 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4494 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4495 wpa_s->pending_join_iface_addr);
4498 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4499 "from P2P peer table: %d MHz", freq);
4501 if (wpa_s->p2p_join_ssid_len) {
4502 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4503 MACSTR " and SSID %s",
4504 MAC2STR(wpa_s->pending_join_iface_addr),
4505 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4506 wpa_s->p2p_join_ssid_len));
4507 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4508 wpa_s->p2p_join_ssid,
4509 wpa_s->p2p_join_ssid_len);
4512 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4513 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4514 bss = wpa_bss_get_bssid_latest(wpa_s,
4515 wpa_s->pending_join_iface_addr);
4518 u8 dev_addr[ETH_ALEN];
4521 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4522 "from BSS table: %d MHz (SSID %s)", freq,
4523 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4524 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4526 os_memcmp(wpa_s->pending_join_dev_addr,
4527 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4528 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4530 wpa_printf(MSG_DEBUG,
4531 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4533 MAC2STR(wpa_s->pending_join_dev_addr));
4534 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4541 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4542 wpa_msg_global(wpa_s->parent, MSG_INFO,
4543 P2P_EVENT_GROUP_FORMATION_FAILURE
4544 "reason=FREQ_CONFLICT");
4548 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4549 "prior to joining an existing group (GO " MACSTR
4551 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4552 wpa_s->pending_pd_before_join = 1;
4554 switch (wpa_s->pending_join_wps_method) {
4555 case WPS_PIN_DISPLAY:
4556 method = WPS_CONFIG_KEYPAD;
4558 case WPS_PIN_KEYPAD:
4559 method = WPS_CONFIG_DISPLAY;
4562 method = WPS_CONFIG_PUSHBUTTON;
4569 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4570 wpa_s->pending_join_dev_addr) ==
4573 * We have already performed provision discovery for
4574 * joining the group. Proceed directly to join
4575 * operation without duplicated provision discovery. */
4576 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4577 "with " MACSTR " already done - proceed to "
4579 MAC2STR(wpa_s->pending_join_dev_addr));
4580 wpa_s->pending_pd_before_join = 0;
4584 if (p2p_prov_disc_req(wpa_s->global->p2p,
4585 wpa_s->pending_join_dev_addr,
4587 freq, wpa_s->user_initiated_pd) < 0) {
4588 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4589 "Discovery Request before joining an "
4591 wpa_s->pending_pd_before_join = 0;
4597 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4598 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4599 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4600 wpas_p2p_check_join_scan_limit(wpa_s);
4604 /* Start join operation immediately */
4605 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4609 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4610 const u8 *ssid, size_t ssid_len)
4613 struct wpa_driver_scan_params params;
4614 struct wpabuf *wps_ie, *ies;
4616 int freqs[2] = { 0, 0 };
4618 os_memset(¶ms, 0, sizeof(params));
4620 /* P2P Wildcard SSID */
4621 params.num_ssids = 1;
4622 if (ssid && ssid_len) {
4623 params.ssids[0].ssid = ssid;
4624 params.ssids[0].ssid_len = ssid_len;
4625 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4626 wpa_s->p2p_join_ssid_len = ssid_len;
4628 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4629 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4630 wpa_s->p2p_join_ssid_len = 0;
4633 wpa_s->wps->dev.p2p = 1;
4634 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4635 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4637 if (wps_ie == NULL) {
4638 wpas_p2p_scan_res_join(wpa_s, NULL);
4642 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4643 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4645 wpabuf_free(wps_ie);
4646 wpas_p2p_scan_res_join(wpa_s, NULL);
4649 wpabuf_put_buf(ies, wps_ie);
4650 wpabuf_free(wps_ie);
4652 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4654 params.p2p_probe = 1;
4655 params.extra_ies = wpabuf_head(ies);
4656 params.extra_ies_len = wpabuf_len(ies);
4661 * If freq is not provided, check the operating freq of the GO
4662 * and use a single channel scan on if possible.
4664 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4665 wpa_s->pending_join_iface_addr);
4671 params.freqs = freqs;
4675 * Run a scan to update BSS table and start Provision Discovery once
4676 * the new scan results become available.
4678 ret = wpa_drv_scan(wpa_s, ¶ms);
4680 os_get_reltime(&wpa_s->scan_trigger_time);
4681 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4682 wpa_s->own_scan_requested = 1;
4688 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4690 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4691 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4692 wpas_p2p_check_join_scan_limit(wpa_s);
4697 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4699 struct wpa_supplicant *wpa_s = eloop_ctx;
4700 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4704 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4705 const u8 *dev_addr, enum p2p_wps_method wps_method,
4706 int auto_join, int op_freq,
4707 const u8 *ssid, size_t ssid_len)
4709 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4710 MACSTR " dev " MACSTR " op_freq=%d)%s",
4711 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4712 auto_join ? " (auto_join)" : "");
4713 if (ssid && ssid_len) {
4714 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4715 wpa_ssid_txt(ssid, ssid_len));
4718 wpa_s->p2p_auto_pd = 0;
4719 wpa_s->p2p_auto_join = !!auto_join;
4720 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4721 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4722 wpa_s->pending_join_wps_method = wps_method;
4724 /* Make sure we are not running find during connection establishment */
4725 wpas_p2p_stop_find(wpa_s);
4727 wpa_s->p2p_join_scan_count = 0;
4728 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4733 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4734 const u8 *ssid, size_t ssid_len)
4736 struct wpa_supplicant *group;
4737 struct p2p_go_neg_results res;
4738 struct wpa_bss *bss;
4740 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4743 if (group != wpa_s) {
4744 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4745 sizeof(group->p2p_pin));
4746 group->p2p_wps_method = wpa_s->p2p_wps_method;
4749 * Need to mark the current interface for p2p_group_formation
4750 * when a separate group interface is not used. This is needed
4751 * to allow p2p_cancel stop a pending p2p_connect-join.
4752 * wpas_p2p_init_group_interface() addresses this for the case
4753 * where a separate group interface is used.
4755 wpa_s->global->p2p_group_formation = wpa_s;
4758 group->p2p_in_provisioning = 1;
4759 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4761 os_memset(&res, 0, sizeof(res));
4762 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4763 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4765 res.wps_method = wpa_s->pending_join_wps_method;
4766 if (freq && ssid && ssid_len) {
4768 res.ssid_len = ssid_len;
4769 os_memcpy(res.ssid, ssid, ssid_len);
4771 bss = wpa_bss_get_bssid_latest(wpa_s,
4772 wpa_s->pending_join_iface_addr);
4774 res.freq = bss->freq;
4775 res.ssid_len = bss->ssid_len;
4776 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4777 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4779 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4783 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4784 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4786 wpa_drv_cancel_remain_on_channel(wpa_s);
4787 wpa_s->off_channel_freq = 0;
4788 wpa_s->roc_waiting_drv_freq = 0;
4790 wpas_start_wps_enrollee(group, &res);
4793 * Allow a longer timeout for join-a-running-group than normal 15
4794 * second group formation timeout since the GO may not have authorized
4795 * our connection yet.
4797 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4798 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4805 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4806 int *force_freq, int *pref_freq, int go,
4807 unsigned int *pref_freq_list,
4808 unsigned int *num_pref_freq)
4810 struct wpa_used_freq_data *freqs;
4811 int res, best_freq, num_unused;
4812 unsigned int freq_in_use = 0, num, i, max_pref_freq;
4814 max_pref_freq = *num_pref_freq;
4817 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4818 sizeof(struct wpa_used_freq_data));
4822 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4823 wpa_s->num_multichan_concurrent);
4826 * It is possible that the total number of used frequencies is bigger
4827 * than the number of frequencies used for P2P, so get the system wide
4828 * number of unused frequencies.
4830 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4832 wpa_printf(MSG_DEBUG,
4833 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4834 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4839 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4841 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4843 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
4844 ieee80211_is_dfs(freq)) {
4846 * If freq is a DFS channel and DFS is offloaded
4847 * to the driver, allow P2P GO to use it.
4849 wpa_printf(MSG_DEBUG,
4850 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
4853 wpa_printf(MSG_DEBUG,
4854 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
4861 for (i = 0; i < num; i++) {
4862 if (freqs[i].freq == freq)
4866 if (num_unused <= 0 && !freq_in_use) {
4867 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4872 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4873 "requested channel (%u MHz)", freq);
4878 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4880 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
4881 enum wpa_driver_if_type iface_type;
4884 iface_type = WPA_IF_P2P_GO;
4886 iface_type = WPA_IF_P2P_CLIENT;
4888 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
4891 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
4894 if (!res && max_pref_freq > 0) {
4895 *num_pref_freq = max_pref_freq;
4897 while (wpas_p2p_disallowed_freq(wpa_s->global,
4898 pref_freq_list[i]) &&
4899 i < *num_pref_freq) {
4900 wpa_printf(MSG_DEBUG,
4901 "P2P: preferred_freq_list[%d]=%d is disallowed",
4902 i, pref_freq_list[i]);
4905 if (i != *num_pref_freq) {
4906 best_freq = pref_freq_list[i];
4907 wpa_printf(MSG_DEBUG,
4908 "P2P: Using preferred_freq_list[%d]=%d",
4911 wpa_printf(MSG_DEBUG,
4912 "P2P: All driver preferred frequencies are disallowed for P2P use");
4916 wpa_printf(MSG_DEBUG,
4917 "P2P: No preferred frequency list available");
4921 /* We have a candidate frequency to use */
4922 if (best_freq > 0) {
4923 if (*pref_freq == 0 && num_unused > 0) {
4924 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4926 *pref_freq = best_freq;
4928 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4930 *force_freq = best_freq;
4932 } else if (num_unused > 0) {
4933 wpa_printf(MSG_DEBUG,
4934 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4937 wpa_printf(MSG_DEBUG,
4938 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4952 * wpas_p2p_connect - Request P2P Group Formation to be started
4953 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4954 * @peer_addr: Address of the peer P2P Device
4955 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4956 * @persistent_group: Whether to create a persistent group
4957 * @auto_join: Whether to select join vs. GO Negotiation automatically
4958 * @join: Whether to join an existing group (as a client) instead of starting
4959 * Group Owner negotiation; @peer_addr is BSSID in that case
4960 * @auth: Whether to only authorize the connection instead of doing that and
4961 * initiating Group Owner negotiation
4962 * @go_intent: GO Intent or -1 to use default
4963 * @freq: Frequency for the group or 0 for auto-selection
4964 * @persistent_id: Persistent group credentials to use for forcing GO
4965 * parameters or -1 to generate new values (SSID/passphrase)
4966 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4967 * interoperability workaround when initiating group formation
4968 * @ht40: Start GO with 40 MHz channel width
4969 * @vht: Start GO with VHT support
4970 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4971 * failure, -2 on failure due to channel not currently available,
4972 * -3 if forced channel is not supported
4974 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4975 const char *pin, enum p2p_wps_method wps_method,
4976 int persistent_group, int auto_join, int join, int auth,
4977 int go_intent, int freq, int persistent_id, int pd,
4980 int force_freq = 0, pref_freq = 0;
4982 enum wpa_driver_if_type iftype;
4984 struct wpa_ssid *ssid = NULL;
4985 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
4987 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4990 if (persistent_id >= 0) {
4991 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4992 if (ssid == NULL || ssid->disabled != 2 ||
4993 ssid->mode != WPAS_MODE_P2P_GO)
4997 os_free(wpa_s->global->add_psk);
4998 wpa_s->global->add_psk = NULL;
5000 wpa_s->global->p2p_fail_on_wps_complete = 0;
5001 wpa_s->global->pending_p2ps_group = 0;
5004 go_intent = wpa_s->conf->p2p_go_intent;
5007 wpa_s->p2p_long_listen = 0;
5009 wpa_s->p2p_wps_method = wps_method;
5010 wpa_s->p2p_persistent_group = !!persistent_group;
5011 wpa_s->p2p_persistent_id = persistent_id;
5012 wpa_s->p2p_go_intent = go_intent;
5013 wpa_s->p2p_connect_freq = freq;
5014 wpa_s->p2p_fallback_to_go_neg = 0;
5015 wpa_s->p2p_pd_before_go_neg = !!pd;
5016 wpa_s->p2p_go_ht40 = !!ht40;
5017 wpa_s->p2p_go_vht = !!vht;
5020 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5021 else if (wps_method == WPS_PIN_DISPLAY) {
5022 ret = wps_generate_pin();
5023 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5025 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5026 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5027 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5030 wpa_s->p2p_pin[0] = '\0';
5032 if (join || auto_join) {
5033 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5035 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5036 "connect a running group from " MACSTR,
5037 MAC2STR(peer_addr));
5038 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5041 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5042 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5044 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5045 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5049 os_get_reltime(&wpa_s->p2p_auto_started);
5050 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5052 wpa_s->p2p_auto_started.sec,
5053 wpa_s->p2p_auto_started.usec);
5055 wpa_s->user_initiated_pd = 1;
5056 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5057 auto_join, freq, NULL, 0) < 0)
5062 size = P2P_MAX_PREF_CHANNELS;
5063 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5064 go_intent == 15, pref_freq_list, &size);
5067 wpas_p2p_set_own_freq_preference(wpa_s,
5068 force_freq ? force_freq : pref_freq);
5070 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5072 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5074 if (wpa_s->create_p2p_iface) {
5075 /* Prepare to add a new interface for the group */
5076 iftype = WPA_IF_P2P_GROUP;
5077 if (go_intent == 15)
5078 iftype = WPA_IF_P2P_GO;
5079 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5080 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5081 "interface for the group");
5085 if_addr = wpa_s->pending_interface_addr;
5087 if_addr = wpa_s->own_addr;
5088 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5092 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5094 force_freq, persistent_group, ssid,
5100 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5101 go_intent, if_addr, force_freq,
5102 persistent_group, ssid, pref_freq) < 0) {
5103 if (wpa_s->create_p2p_iface)
5104 wpas_p2p_remove_pending_group_interface(wpa_s);
5112 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5113 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5114 * @freq: Frequency of the channel in MHz
5115 * @duration: Duration of the stay on the channel in milliseconds
5117 * This callback is called when the driver indicates that it has started the
5118 * requested remain-on-channel duration.
5120 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5121 unsigned int freq, unsigned int duration)
5123 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5125 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)",
5126 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5127 wpa_s->roc_waiting_drv_freq, freq, duration);
5128 if (wpa_s->off_channel_freq &&
5129 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5130 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5131 wpa_s->pending_listen_duration);
5132 wpa_s->pending_listen_freq = 0;
5134 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5135 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5141 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5143 /* Limit maximum Listen state time based on driver limitation. */
5144 if (timeout > wpa_s->max_remain_on_chan)
5145 timeout = wpa_s->max_remain_on_chan;
5147 return p2p_listen(wpa_s->global->p2p, timeout);
5152 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5153 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5154 * @freq: Frequency of the channel in MHz
5156 * This callback is called when the driver indicates that a remain-on-channel
5157 * operation has been completed, i.e., the duration on the requested channel
5160 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5163 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5164 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5165 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5166 wpas_p2p_listen_work_done(wpa_s);
5167 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5169 if (wpa_s->p2p_long_listen > 0)
5170 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5171 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5172 return; /* P2P module started a new operation */
5173 if (offchannel_pending_action_tx(wpa_s))
5175 if (wpa_s->p2p_long_listen > 0) {
5176 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5177 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5180 * When listen duration is over, stop listen & update p2p_state
5183 p2p_stop_listen(wpa_s->global->p2p);
5189 * wpas_p2p_group_remove - Remove a P2P group
5190 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5191 * @ifname: Network interface name of the group interface or "*" to remove all
5193 * Returns: 0 on success, -1 on failure
5195 * This function is used to remove a P2P group. This can be used to disconnect
5196 * from a group in which the local end is a P2P Client or to end a P2P Group in
5197 * case the local end is the Group Owner. If a virtual network interface was
5198 * created for this group, that interface will be removed. Otherwise, only the
5199 * configured P2P group network will be removed from the interface.
5201 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5203 struct wpa_global *global = wpa_s->global;
5204 struct wpa_supplicant *calling_wpa_s = wpa_s;
5206 if (os_strcmp(ifname, "*") == 0) {
5207 struct wpa_supplicant *prev;
5208 wpa_s = global->ifaces;
5211 wpa_s = wpa_s->next;
5212 if (prev->p2p_group_interface !=
5213 NOT_P2P_GROUP_INTERFACE ||
5214 (prev->current_ssid &&
5215 prev->current_ssid->p2p_group))
5216 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5221 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5222 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5226 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5230 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5235 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5237 if (wpa_s->best_24_freq > 0 &&
5238 p2p_supported_freq_go(wpa_s->global->p2p,
5239 wpa_s->best_24_freq)) {
5240 freq = wpa_s->best_24_freq;
5241 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5242 "channel: %d MHz", freq);
5244 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5246 freq = 2412 + (r % 3) * 25;
5247 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5248 "channel: %d MHz", freq);
5253 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5255 if (wpa_s->best_5_freq > 0 &&
5256 p2p_supported_freq_go(wpa_s->global->p2p,
5257 wpa_s->best_5_freq)) {
5258 freq = wpa_s->best_5_freq;
5259 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5260 "channel: %d MHz", freq);
5262 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5264 freq = 5180 + (r % 4) * 20;
5265 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5266 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5267 "5 GHz channel for P2P group");
5270 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5271 "channel: %d MHz", freq);
5275 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5276 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5277 ieee80211_is_dfs(freq)) {
5279 * If freq is a DFS channel and DFS is offloaded to the
5280 * driver, allow P2P GO to use it.
5282 wpa_printf(MSG_DEBUG, "P2P: "
5283 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5287 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5288 "(%u MHz) is not supported for P2P uses",
5297 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5298 struct p2p_go_neg_results *params,
5299 const struct p2p_channels *channels)
5303 /* first try some random selection of the social channels */
5304 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5307 for (i = 0; i < 3; i++) {
5308 params->freq = 2412 + ((r + i) % 3) * 25;
5309 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5310 freq_included(channels, params->freq) &&
5311 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5315 /* try all channels in reg. class 81 */
5316 for (i = 0; i < 11; i++) {
5317 params->freq = 2412 + i * 5;
5318 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5319 freq_included(channels, params->freq) &&
5320 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5324 /* try all channels in operating class 115 */
5325 for (i = 0; i < 4; i++) {
5326 params->freq = 5180 + i * 20;
5327 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5328 freq_included(channels, params->freq) &&
5329 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5333 /* try all channels in operating class 124 */
5334 for (i = 0; i < 4; i++) {
5335 params->freq = 5745 + i * 20;
5336 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5337 freq_included(channels, params->freq) &&
5338 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5342 /* try social channel class 180 channel 2 */
5343 params->freq = 58320 + 1 * 2160;
5344 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5345 freq_included(channels, params->freq) &&
5346 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5349 /* try all channels in reg. class 180 */
5350 for (i = 0; i < 4; i++) {
5351 params->freq = 58320 + i * 2160;
5352 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5353 freq_included(channels, params->freq) &&
5354 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5358 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
5361 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5367 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5368 struct p2p_go_neg_results *params,
5369 int freq, int ht40, int vht,
5370 const struct p2p_channels *channels)
5372 struct wpa_used_freq_data *freqs;
5373 unsigned int pref_freq, cand_freq;
5374 unsigned int num, i;
5376 os_memset(params, 0, sizeof(*params));
5377 params->role_go = 1;
5378 params->ht40 = ht40;
5381 if (!freq_included(channels, freq)) {
5382 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5386 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5387 "frequency %d MHz", freq);
5388 params->freq = freq;
5389 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5390 wpa_s->conf->p2p_oper_channel >= 1 &&
5391 wpa_s->conf->p2p_oper_channel <= 11 &&
5392 freq_included(channels,
5393 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5394 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5395 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5396 "frequency %d MHz", params->freq);
5397 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5398 wpa_s->conf->p2p_oper_reg_class == 116 ||
5399 wpa_s->conf->p2p_oper_reg_class == 117 ||
5400 wpa_s->conf->p2p_oper_reg_class == 124 ||
5401 wpa_s->conf->p2p_oper_reg_class == 125 ||
5402 wpa_s->conf->p2p_oper_reg_class == 126 ||
5403 wpa_s->conf->p2p_oper_reg_class == 127) &&
5404 freq_included(channels,
5405 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
5406 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5407 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5408 "frequency %d MHz", params->freq);
5409 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5410 wpa_s->best_overall_freq > 0 &&
5411 p2p_supported_freq_go(wpa_s->global->p2p,
5412 wpa_s->best_overall_freq) &&
5413 freq_included(channels, wpa_s->best_overall_freq)) {
5414 params->freq = wpa_s->best_overall_freq;
5415 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5416 "channel %d MHz", params->freq);
5417 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5418 wpa_s->best_24_freq > 0 &&
5419 p2p_supported_freq_go(wpa_s->global->p2p,
5420 wpa_s->best_24_freq) &&
5421 freq_included(channels, wpa_s->best_24_freq)) {
5422 params->freq = wpa_s->best_24_freq;
5423 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5424 "channel %d MHz", params->freq);
5425 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5426 wpa_s->best_5_freq > 0 &&
5427 p2p_supported_freq_go(wpa_s->global->p2p,
5428 wpa_s->best_5_freq) &&
5429 freq_included(channels, wpa_s->best_5_freq)) {
5430 params->freq = wpa_s->best_5_freq;
5431 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5432 "channel %d MHz", params->freq);
5433 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5435 params->freq = pref_freq;
5436 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5437 "channels", params->freq);
5439 /* no preference, select some channel */
5440 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
5444 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5445 sizeof(struct wpa_used_freq_data));
5449 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5450 wpa_s->num_multichan_concurrent);
5452 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5454 /* First try the best used frequency if possible */
5455 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5456 params->freq = cand_freq;
5458 /* Try any of the used frequencies */
5459 for (i = 0; i < num; i++) {
5460 if (freq_included(channels, freqs[i].freq)) {
5461 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5463 params->freq = freqs[i].freq;
5469 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5470 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5474 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5478 for (i = 0; i < num; i++) {
5479 if (freqs[i].freq == freq)
5484 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5486 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5490 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5500 static struct wpa_supplicant *
5501 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5504 struct wpa_supplicant *group_wpa_s;
5506 if (!wpas_p2p_create_iface(wpa_s)) {
5507 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5509 wpa_s->p2p_first_connection_timeout = 0;
5513 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5514 WPA_IF_P2P_CLIENT) < 0) {
5515 wpa_msg_global(wpa_s, MSG_ERROR,
5516 "P2P: Failed to add group interface");
5519 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5520 if (group_wpa_s == NULL) {
5521 wpa_msg_global(wpa_s, MSG_ERROR,
5522 "P2P: Failed to initialize group interface");
5523 wpas_p2p_remove_pending_group_interface(wpa_s);
5527 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5528 group_wpa_s->ifname);
5529 group_wpa_s->p2p_first_connection_timeout = 0;
5535 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5536 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5537 * @persistent_group: Whether to create a persistent group
5538 * @freq: Frequency for the group or 0 to indicate no hardcoding
5539 * @ht40: Start GO with 40 MHz channel width
5540 * @vht: Start GO with VHT support
5541 * Returns: 0 on success, -1 on failure
5543 * This function creates a new P2P group with the local end as the Group Owner,
5544 * i.e., without using Group Owner Negotiation.
5546 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5547 int freq, int ht40, int vht)
5549 struct p2p_go_neg_results params;
5551 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5554 os_free(wpa_s->global->add_psk);
5555 wpa_s->global->add_psk = NULL;
5557 /* Make sure we are not running find during connection establishment */
5558 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5559 wpas_p2p_stop_find_oper(wpa_s);
5561 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5565 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5568 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5569 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5570 ieee80211_is_dfs(params.freq)) {
5572 * If freq is a DFS channel and DFS is offloaded to the
5573 * driver, allow P2P GO to use it.
5575 wpa_printf(MSG_DEBUG,
5576 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5577 __func__, params.freq);
5579 wpa_printf(MSG_DEBUG,
5580 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5585 p2p_go_params(wpa_s->global->p2p, ¶ms);
5586 params.persistent_group = persistent_group;
5588 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5591 wpas_start_wps_go(wpa_s, ¶ms, 0);
5597 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5598 struct wpa_ssid *params, int addr_allocated,
5601 struct wpa_ssid *ssid;
5603 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5606 wpa_s->p2p_last_4way_hs_fail = NULL;
5608 wpa_supplicant_ap_deinit(wpa_s);
5610 ssid = wpa_config_add_network(wpa_s->conf);
5613 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5614 wpa_config_set_network_defaults(ssid);
5615 ssid->temporary = 1;
5616 ssid->proto = WPA_PROTO_RSN;
5617 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5618 ssid->group_cipher = WPA_CIPHER_CCMP;
5619 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5620 ssid->ssid = os_malloc(params->ssid_len);
5621 if (ssid->ssid == NULL) {
5622 wpa_config_remove_network(wpa_s->conf, ssid->id);
5625 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5626 ssid->ssid_len = params->ssid_len;
5627 ssid->p2p_group = 1;
5628 ssid->export_keys = 1;
5629 if (params->psk_set) {
5630 os_memcpy(ssid->psk, params->psk, 32);
5633 if (params->passphrase)
5634 ssid->passphrase = os_strdup(params->passphrase);
5636 wpa_s->show_group_started = 1;
5637 wpa_s->p2p_in_invitation = 1;
5638 wpa_s->p2p_invite_go_freq = freq;
5640 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5642 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5643 wpas_p2p_group_formation_timeout,
5644 wpa_s->parent, NULL);
5645 wpa_supplicant_select_network(wpa_s, ssid);
5651 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5652 struct wpa_ssid *ssid, int addr_allocated,
5653 int force_freq, int neg_freq, int ht40,
5654 int vht, const struct p2p_channels *channels,
5655 int connection_timeout)
5657 struct p2p_go_neg_results params;
5660 if (ssid->disabled != 2 || ssid->ssid == NULL)
5663 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5664 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5665 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5668 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5669 wpa_s->parent, NULL)) {
5671 * This can happen if Invitation Response frame was lost
5672 * and the peer (GO of a persistent group) tries to
5673 * invite us again. Reschedule the timeout to avoid
5674 * terminating the wait for the connection too early
5675 * since we now know that the peer is still trying to
5676 * invite us instead of having already started the GO.
5678 wpa_printf(MSG_DEBUG,
5679 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
5680 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5681 wpas_p2p_group_formation_timeout,
5682 wpa_s->parent, NULL);
5687 os_free(wpa_s->global->add_psk);
5688 wpa_s->global->add_psk = NULL;
5690 /* Make sure we are not running find during connection establishment */
5691 wpas_p2p_stop_find_oper(wpa_s);
5693 wpa_s->p2p_fallback_to_go_neg = 0;
5695 if (ssid->mode == WPAS_MODE_P2P_GO) {
5696 if (force_freq > 0) {
5697 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5701 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5703 (freq > 0 && !freq_included(channels, freq)))
5706 } else if (ssid->mode == WPAS_MODE_INFRA) {
5708 if (freq <= 0 || !freq_included(channels, freq)) {
5709 struct os_reltime now;
5710 struct wpa_bss *bss =
5711 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
5713 os_get_reltime(&now);
5715 !os_reltime_expired(&now, &bss->last_update, 5) &&
5716 freq_included(channels, bss->freq))
5722 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5727 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5731 params.psk_set = ssid->psk_set;
5733 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5734 if (ssid->passphrase) {
5735 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5736 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5737 "persistent group");
5740 os_strlcpy(params.passphrase, ssid->passphrase,
5741 sizeof(params.passphrase));
5743 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5744 params.ssid_len = ssid->ssid_len;
5745 params.persistent_group = 1;
5747 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5751 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5753 wpa_s->p2p_first_connection_timeout = connection_timeout;
5754 wpas_start_wps_go(wpa_s, ¶ms, 0);
5760 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5761 struct wpabuf *proberesp_ies)
5763 struct wpa_supplicant *wpa_s = ctx;
5764 if (wpa_s->ap_iface) {
5765 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5766 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5767 wpabuf_free(beacon_ies);
5768 wpabuf_free(proberesp_ies);
5772 wpabuf_free(hapd->p2p_beacon_ie);
5773 hapd->p2p_beacon_ie = beacon_ies;
5775 wpabuf_free(hapd->p2p_probe_resp_ie);
5776 hapd->p2p_probe_resp_ie = proberesp_ies;
5778 wpabuf_free(beacon_ies);
5779 wpabuf_free(proberesp_ies);
5781 wpa_supplicant_ap_update_beacon(wpa_s);
5785 static void wpas_p2p_idle_update(void *ctx, int idle)
5787 struct wpa_supplicant *wpa_s = ctx;
5788 if (!wpa_s->ap_iface)
5790 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5792 if (wpa_s->global->p2p_fail_on_wps_complete &&
5793 wpa_s->p2p_in_provisioning) {
5794 wpas_p2p_grpform_fail_after_wps(wpa_s);
5797 wpas_p2p_set_group_idle_timeout(wpa_s);
5799 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5803 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5804 struct wpa_ssid *ssid)
5806 struct p2p_group *group;
5807 struct p2p_group_config *cfg;
5809 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5812 cfg = os_zalloc(sizeof(*cfg));
5816 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5817 cfg->persistent_group = 2;
5818 else if (ssid->p2p_persistent_group)
5819 cfg->persistent_group = 1;
5820 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5821 if (wpa_s->max_stations &&
5822 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5823 cfg->max_clients = wpa_s->max_stations;
5825 cfg->max_clients = wpa_s->conf->max_num_sta;
5826 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5827 cfg->ssid_len = ssid->ssid_len;
5828 cfg->freq = ssid->frequency;
5829 cfg->cb_ctx = wpa_s;
5830 cfg->ie_update = wpas_p2p_ie_update;
5831 cfg->idle_update = wpas_p2p_idle_update;
5833 group = p2p_group_init(wpa_s->global->p2p, cfg);
5836 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5837 p2p_group_notif_formation_done(group);
5838 wpa_s->p2p_group = group;
5843 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5846 struct wpa_ssid *ssid = wpa_s->current_ssid;
5848 if (!wpa_s->p2p_in_provisioning) {
5849 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5850 "provisioning not in progress");
5854 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5855 u8 go_dev_addr[ETH_ALEN];
5856 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5857 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5859 /* Clear any stored provisioning info */
5860 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5863 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5865 wpa_s->p2p_go_group_formation_completed = 1;
5866 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5868 * Use a separate timeout for initial data connection to
5869 * complete to allow the group to be removed automatically if
5870 * something goes wrong in this step before the P2P group idle
5871 * timeout mechanism is taken into use.
5873 wpa_dbg(wpa_s, MSG_DEBUG,
5874 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5875 P2P_MAX_INITIAL_CONN_WAIT);
5876 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5877 wpas_p2p_group_formation_timeout,
5878 wpa_s->parent, NULL);
5881 * Use a separate timeout for initial data connection to
5882 * complete to allow the group to be removed automatically if
5883 * the client does not complete data connection successfully.
5885 wpa_dbg(wpa_s, MSG_DEBUG,
5886 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5887 P2P_MAX_INITIAL_CONN_WAIT_GO);
5888 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5889 wpas_p2p_group_formation_timeout,
5890 wpa_s->parent, NULL);
5892 * Complete group formation on first successful data connection
5894 wpa_s->p2p_go_group_formation_completed = 0;
5896 if (wpa_s->global->p2p)
5897 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5898 wpas_group_formation_completed(wpa_s, 1, 0);
5902 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5903 struct wps_event_fail *fail)
5905 if (!wpa_s->p2p_in_provisioning) {
5906 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5907 "provisioning not in progress");
5911 if (wpa_s->go_params) {
5912 p2p_clear_provisioning_info(
5914 wpa_s->go_params->peer_device_addr);
5917 wpas_notify_p2p_wps_failed(wpa_s, fail);
5919 if (wpa_s == wpa_s->global->p2p_group_formation) {
5921 * Allow some time for the failed WPS negotiation exchange to
5922 * complete, but remove the group since group formation cannot
5923 * succeed after provisioning failure.
5925 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5926 wpa_s->global->p2p_fail_on_wps_complete = 1;
5927 eloop_deplete_timeout(0, 50000,
5928 wpas_p2p_group_formation_timeout,
5929 wpa_s->parent, NULL);
5934 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5936 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5937 !wpa_s->p2p_in_provisioning)
5940 wpas_p2p_grpform_fail_after_wps(wpa_s);
5946 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5947 const char *config_method,
5948 enum wpas_p2p_prov_disc_use use,
5949 struct p2ps_provision *p2ps_prov)
5953 wpa_s->global->pending_p2ps_group = 0;
5954 wpa_s->p2p_fallback_to_go_neg = 0;
5955 wpa_s->pending_pd_use = NORMAL_PD;
5956 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
5957 p2ps_prov->conncap = p2ps_group_capability(
5958 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
5959 wpa_printf(MSG_DEBUG,
5960 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
5961 __func__, p2ps_prov->conncap,
5962 p2ps_prov->adv_id, p2ps_prov->conncap,
5963 p2ps_prov->status, p2ps_prov->info);
5966 } else if (os_strncmp(config_method, "display", 7) == 0)
5967 config_methods = WPS_CONFIG_DISPLAY;
5968 else if (os_strncmp(config_method, "keypad", 6) == 0)
5969 config_methods = WPS_CONFIG_KEYPAD;
5970 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5971 os_strncmp(config_method, "pushbutton", 10) == 0)
5972 config_methods = WPS_CONFIG_PUSHBUTTON;
5974 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5979 if (use == WPAS_P2P_PD_AUTO) {
5980 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5981 wpa_s->pending_pd_config_methods = config_methods;
5982 wpa_s->p2p_auto_pd = 1;
5983 wpa_s->p2p_auto_join = 0;
5984 wpa_s->pending_pd_before_join = 0;
5985 wpa_s->auto_pd_scan_retry = 0;
5986 wpas_p2p_stop_find(wpa_s);
5987 wpa_s->p2p_join_scan_count = 0;
5988 os_get_reltime(&wpa_s->p2p_auto_started);
5989 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5990 wpa_s->p2p_auto_started.sec,
5991 wpa_s->p2p_auto_started.usec);
5992 wpas_p2p_join_scan(wpa_s, NULL);
5996 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6001 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6002 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6007 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6010 return p2p_scan_result_text(ies, ies_len, buf, end);
6014 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6016 if (!offchannel_pending_action_tx(wpa_s))
6019 wpas_p2p_action_tx_clear(wpa_s);
6021 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6022 "operation request");
6023 offchannel_clear_pending_action_tx(wpa_s);
6027 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6028 enum p2p_discovery_type type,
6029 unsigned int num_req_dev_types, const u8 *req_dev_types,
6030 const u8 *dev_id, unsigned int search_delay,
6031 u8 seek_cnt, const char **seek_string, int freq)
6033 wpas_p2p_clear_pending_action_tx(wpa_s);
6034 wpa_s->p2p_long_listen = 0;
6036 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6037 wpa_s->p2p_in_provisioning)
6040 wpa_supplicant_cancel_sched_scan(wpa_s);
6042 return p2p_find(wpa_s->global->p2p, timeout, type,
6043 num_req_dev_types, req_dev_types, dev_id,
6044 search_delay, seek_cnt, seek_string, freq);
6048 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6049 struct wpa_scan_results *scan_res)
6051 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6053 if (wpa_s->p2p_scan_work) {
6054 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6055 wpa_s->p2p_scan_work = NULL;
6056 radio_work_done(work);
6059 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6063 * Indicate that results have been processed so that the P2P module can
6064 * continue pending tasks.
6066 p2p_scan_res_handled(wpa_s->global->p2p);
6070 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
6072 wpas_p2p_clear_pending_action_tx(wpa_s);
6073 wpa_s->p2p_long_listen = 0;
6074 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6075 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6077 if (wpa_s->global->p2p)
6078 p2p_stop_find(wpa_s->global->p2p);
6080 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6081 wpa_printf(MSG_DEBUG,
6082 "P2P: Do not consider the scan results after stop_find");
6083 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6088 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6090 wpas_p2p_stop_find_oper(wpa_s);
6091 if (!wpa_s->global->pending_group_iface_for_p2ps)
6092 wpas_p2p_remove_pending_group_interface(wpa_s);
6096 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6098 struct wpa_supplicant *wpa_s = eloop_ctx;
6099 wpa_s->p2p_long_listen = 0;
6103 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6107 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6110 wpa_supplicant_cancel_sched_scan(wpa_s);
6111 wpas_p2p_clear_pending_action_tx(wpa_s);
6115 * This is a request for unlimited Listen state. However, at
6116 * least for now, this is mapped to a Listen state for one
6121 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6122 wpa_s->p2p_long_listen = 0;
6125 * Stop previous find/listen operation to avoid trying to request a new
6126 * remain-on-channel operation while the driver is still running the
6129 if (wpa_s->global->p2p)
6130 p2p_stop_find(wpa_s->global->p2p);
6132 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6133 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6134 wpa_s->p2p_long_listen = timeout * 1000;
6135 eloop_register_timeout(timeout, 0,
6136 wpas_p2p_long_listen_timeout,
6144 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6145 u8 *buf, size_t len, int p2p_group)
6147 struct wpabuf *p2p_ie;
6150 if (wpa_s->global->p2p_disabled)
6153 * Advertize mandatory cross connection capability even on
6154 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6156 if (wpa_s->conf->p2p_disabled && p2p_group)
6158 if (wpa_s->global->p2p == NULL)
6163 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6164 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6166 wpabuf_free(p2p_ie);
6172 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
6173 const u8 *dst, const u8 *bssid,
6174 const u8 *ie, size_t ie_len,
6175 unsigned int rx_freq, int ssi_signal)
6177 if (wpa_s->global->p2p_disabled)
6179 if (wpa_s->global->p2p == NULL)
6182 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6183 ie, ie_len, rx_freq)) {
6184 case P2P_PREQ_NOT_P2P:
6185 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6188 case P2P_PREQ_MALFORMED:
6189 case P2P_PREQ_NOT_LISTEN:
6190 case P2P_PREQ_NOT_PROCESSED:
6191 default: /* make gcc happy */
6193 case P2P_PREQ_PROCESSED:
6199 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6200 const u8 *sa, const u8 *bssid,
6201 u8 category, const u8 *data, size_t len, int freq)
6203 if (wpa_s->global->p2p_disabled)
6205 if (wpa_s->global->p2p == NULL)
6208 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6213 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6215 if (wpa_s->global->p2p_disabled)
6217 if (wpa_s->global->p2p == NULL)
6220 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6224 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6226 p2p_group_deinit(wpa_s->p2p_group);
6227 wpa_s->p2p_group = NULL;
6229 wpa_s->ap_configured_cb = NULL;
6230 wpa_s->ap_configured_cb_ctx = NULL;
6231 wpa_s->ap_configured_cb_data = NULL;
6232 wpa_s->connect_without_scan = NULL;
6236 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6238 wpa_s->p2p_long_listen = 0;
6240 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6243 return p2p_reject(wpa_s->global->p2p, addr);
6247 /* Invite to reinvoke a persistent group */
6248 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6249 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6250 int ht40, int vht, int pref_freq)
6252 enum p2p_invite_role role;
6256 int no_pref_freq_given = pref_freq == 0;
6257 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6259 wpa_s->global->p2p_invite_group = NULL;
6261 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6263 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6265 wpa_s->p2p_persistent_go_freq = freq;
6266 wpa_s->p2p_go_ht40 = !!ht40;
6267 if (ssid->mode == WPAS_MODE_P2P_GO) {
6268 role = P2P_INVITE_ROLE_GO;
6269 if (peer_addr == NULL) {
6270 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6271 "address in invitation command");
6274 if (wpas_p2p_create_iface(wpa_s)) {
6275 if (wpas_p2p_add_group_interface(wpa_s,
6276 WPA_IF_P2P_GO) < 0) {
6277 wpa_printf(MSG_ERROR, "P2P: Failed to "
6278 "allocate a new interface for the "
6282 bssid = wpa_s->pending_interface_addr;
6284 bssid = wpa_s->own_addr;
6286 role = P2P_INVITE_ROLE_CLIENT;
6287 peer_addr = ssid->bssid;
6289 wpa_s->pending_invite_ssid_id = ssid->id;
6291 size = P2P_MAX_PREF_CHANNELS;
6292 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6293 role == P2P_INVITE_ROLE_GO,
6294 pref_freq_list, &size);
6298 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6301 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6302 no_pref_freq_given && pref_freq > 0 &&
6303 wpa_s->num_multichan_concurrent > 1 &&
6304 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6305 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6311 * Stop any find/listen operations before invitation and possibly
6312 * connection establishment.
6314 wpas_p2p_stop_find_oper(wpa_s);
6316 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6317 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6322 /* Invite to join an active group */
6323 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6324 const u8 *peer_addr, const u8 *go_dev_addr)
6326 struct wpa_global *global = wpa_s->global;
6327 enum p2p_invite_role role;
6329 struct wpa_ssid *ssid;
6331 int freq = 0, force_freq = 0, pref_freq = 0;
6333 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6335 wpa_s->p2p_persistent_go_freq = 0;
6336 wpa_s->p2p_go_ht40 = 0;
6337 wpa_s->p2p_go_vht = 0;
6339 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6340 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6343 if (wpa_s == NULL) {
6344 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6348 ssid = wpa_s->current_ssid;
6350 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6355 wpa_s->global->p2p_invite_group = wpa_s;
6356 persistent = ssid->p2p_persistent_group &&
6357 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6358 ssid->ssid, ssid->ssid_len);
6360 if (ssid->mode == WPAS_MODE_P2P_GO) {
6361 role = P2P_INVITE_ROLE_ACTIVE_GO;
6362 bssid = wpa_s->own_addr;
6363 if (go_dev_addr == NULL)
6364 go_dev_addr = wpa_s->global->p2p_dev_addr;
6365 freq = ssid->frequency;
6367 role = P2P_INVITE_ROLE_CLIENT;
6368 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6369 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6370 "invite to current group");
6373 bssid = wpa_s->bssid;
6374 if (go_dev_addr == NULL &&
6375 !is_zero_ether_addr(wpa_s->go_dev_addr))
6376 go_dev_addr = wpa_s->go_dev_addr;
6377 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6378 (int) wpa_s->assoc_freq;
6380 wpa_s->parent->pending_invite_ssid_id = -1;
6382 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6385 size = P2P_MAX_PREF_CHANNELS;
6386 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6387 role == P2P_INVITE_ROLE_ACTIVE_GO,
6388 pref_freq_list, &size);
6391 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6393 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6394 ssid->ssid, ssid->ssid_len, force_freq,
6395 go_dev_addr, persistent, pref_freq, -1);
6399 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6401 struct wpa_ssid *ssid = wpa_s->current_ssid;
6402 u8 go_dev_addr[ETH_ALEN];
6403 int network_id = -1;
6409 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6410 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6411 wpa_s->parent, NULL);
6414 if (!wpa_s->show_group_started || !ssid)
6417 wpa_s->show_group_started = 0;
6419 os_memset(go_dev_addr, 0, ETH_ALEN);
6420 if (ssid->bssid_set)
6421 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6422 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6424 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6426 if (wpa_s->global->p2p_group_formation == wpa_s)
6427 wpa_s->global->p2p_group_formation = NULL;
6429 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6430 (int) wpa_s->assoc_freq;
6433 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6436 res = os_snprintf(ip_addr, sizeof(ip_addr),
6437 " ip_addr=%u.%u.%u.%u "
6438 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6439 ip[0], ip[1], ip[2], ip[3],
6440 ip[4], ip[5], ip[6], ip[7],
6441 ip[8], ip[9], ip[10], ip[11]);
6442 if (os_snprintf_error(sizeof(ip_addr), res))
6446 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6447 ssid->passphrase == NULL && ssid->psk_set ?
6449 ssid->passphrase, go_dev_addr, persistent,
6453 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6456 network_id = ssid->id;
6457 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6461 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6462 u32 interval1, u32 duration2, u32 interval2)
6466 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6469 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6470 wpa_s->current_ssid == NULL ||
6471 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6474 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6475 wpa_s->own_addr, wpa_s->assoc_freq,
6476 duration1, interval1, duration2, interval2);
6478 wpa_s->waiting_presence_resp = 1;
6484 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6485 unsigned int interval)
6487 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6490 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6494 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6496 if (wpa_s->current_ssid == NULL) {
6498 * current_ssid can be cleared when P2P client interface gets
6499 * disconnected, so assume this interface was used as P2P
6504 return wpa_s->current_ssid->p2p_group &&
6505 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6509 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6511 struct wpa_supplicant *wpa_s = eloop_ctx;
6513 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6514 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6519 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6521 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6525 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6529 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6530 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6532 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6535 timeout = wpa_s->conf->p2p_group_idle;
6536 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6537 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6538 timeout = P2P_MAX_CLIENT_IDLE;
6544 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6545 timeout = 0; /* special client mode no-timeout */
6550 if (wpa_s->p2p_in_provisioning) {
6552 * Use the normal group formation timeout during the
6553 * provisioning phase to avoid terminating this process too
6554 * early due to group idle timeout.
6556 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6557 "during provisioning");
6561 if (wpa_s->show_group_started) {
6563 * Use the normal group formation timeout between the end of
6564 * the provisioning phase and completion of 4-way handshake to
6565 * avoid terminating this process too early due to group idle
6568 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6569 "while waiting for initial 4-way handshake to "
6574 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6576 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6581 /* Returns 1 if the interface was removed */
6582 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6583 u16 reason_code, const u8 *ie, size_t ie_len,
6584 int locally_generated)
6586 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6589 if (!locally_generated)
6590 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6593 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6594 wpa_s->current_ssid &&
6595 wpa_s->current_ssid->p2p_group &&
6596 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6597 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6598 "session is ending");
6599 if (wpas_p2p_group_delete(wpa_s,
6600 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6609 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6610 u16 reason_code, const u8 *ie, size_t ie_len,
6611 int locally_generated)
6613 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6616 if (!locally_generated)
6617 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6622 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6624 struct p2p_data *p2p = wpa_s->global->p2p;
6629 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6632 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6633 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6635 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6636 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6639 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6640 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6642 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6643 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6645 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6646 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6647 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6648 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6649 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6652 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6653 p2p_set_sec_dev_types(p2p,
6654 (void *) wpa_s->conf->sec_device_type,
6655 wpa_s->conf->num_sec_device_types);
6657 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6659 p2p_remove_wps_vendor_extensions(p2p);
6660 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6661 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6663 p2p_add_wps_vendor_extension(
6664 p2p, wpa_s->conf->wps_vendor_ext[i]);
6668 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6669 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6671 country[0] = wpa_s->conf->country[0];
6672 country[1] = wpa_s->conf->country[1];
6674 p2p_set_country(p2p, country);
6677 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6678 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6679 wpa_s->conf->p2p_ssid_postfix ?
6680 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6684 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6685 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6687 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6688 u8 reg_class, channel;
6693 if (wpa_s->conf->p2p_listen_reg_class &&
6694 wpa_s->conf->p2p_listen_channel) {
6695 reg_class = wpa_s->conf->p2p_listen_reg_class;
6696 channel = wpa_s->conf->p2p_listen_channel;
6701 * Pick one of the social channels randomly as the
6704 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6707 channel = 1 + (r % 3) * 5;
6710 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6713 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6716 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6717 u8 op_reg_class, op_channel, cfg_op_channel;
6720 if (wpa_s->conf->p2p_oper_reg_class &&
6721 wpa_s->conf->p2p_oper_channel) {
6722 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6723 op_channel = wpa_s->conf->p2p_oper_channel;
6728 * Use random operation channel from (1, 6, 11)
6729 *if no other preference is indicated.
6731 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6734 op_channel = 1 + (r % 3) * 5;
6737 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6740 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6744 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6745 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6746 wpa_s->conf->p2p_pref_chan) < 0) {
6747 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6751 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6752 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6757 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6758 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6762 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6765 if (!wpa_s->ap_iface)
6767 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6772 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6774 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6777 wpa_s->global->cross_connection = enabled;
6778 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6781 struct wpa_supplicant *iface;
6783 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6785 if (iface->cross_connect_enabled == 0)
6788 iface->cross_connect_enabled = 0;
6789 iface->cross_connect_in_use = 0;
6790 wpa_msg_global(iface->parent, MSG_INFO,
6791 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6793 iface->cross_connect_uplink);
6801 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6803 struct wpa_supplicant *iface;
6805 if (!uplink->global->cross_connection)
6808 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6809 if (!iface->cross_connect_enabled)
6811 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6814 if (iface->ap_iface == NULL)
6816 if (iface->cross_connect_in_use)
6819 iface->cross_connect_in_use = 1;
6820 wpa_msg_global(iface->parent, MSG_INFO,
6821 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6822 iface->ifname, iface->cross_connect_uplink);
6827 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6829 struct wpa_supplicant *iface;
6831 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6832 if (!iface->cross_connect_enabled)
6834 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6837 if (!iface->cross_connect_in_use)
6840 wpa_msg_global(iface->parent, MSG_INFO,
6841 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6842 iface->ifname, iface->cross_connect_uplink);
6843 iface->cross_connect_in_use = 0;
6848 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6850 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6851 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6852 wpa_s->cross_connect_disallowed)
6853 wpas_p2p_disable_cross_connect(wpa_s);
6855 wpas_p2p_enable_cross_connect(wpa_s);
6856 if (!wpa_s->ap_iface &&
6857 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6858 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6862 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6864 wpas_p2p_disable_cross_connect(wpa_s);
6865 if (!wpa_s->ap_iface &&
6866 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6868 wpas_p2p_set_group_idle_timeout(wpa_s);
6872 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6874 struct wpa_supplicant *iface;
6876 if (!wpa_s->global->cross_connection)
6879 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6882 if (iface->drv_flags &
6883 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6885 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
6886 iface != wpa_s->parent)
6889 wpa_s->cross_connect_enabled = 1;
6890 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6891 sizeof(wpa_s->cross_connect_uplink));
6892 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6893 "%s to %s whenever uplink is available",
6894 wpa_s->ifname, wpa_s->cross_connect_uplink);
6896 if (iface->ap_iface || iface->current_ssid == NULL ||
6897 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6898 iface->cross_connect_disallowed ||
6899 iface->wpa_state != WPA_COMPLETED)
6902 wpa_s->cross_connect_in_use = 1;
6903 wpa_msg_global(wpa_s->parent, MSG_INFO,
6904 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6905 wpa_s->ifname, wpa_s->cross_connect_uplink);
6911 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6913 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6914 !wpa_s->p2p_in_provisioning)
6915 return 0; /* not P2P client operation */
6917 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6919 if (wpa_s != wpa_s->parent)
6920 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6921 wpas_p2p_group_formation_failed(wpa_s, 0);
6926 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6928 struct wpa_supplicant *wpa_s = eloop_ctx;
6929 wpas_p2p_notif_pbc_overlap(wpa_s);
6933 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6935 struct p2p_channels chan, cli_chan;
6936 struct wpa_supplicant *ifs;
6938 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6941 os_memset(&chan, 0, sizeof(chan));
6942 os_memset(&cli_chan, 0, sizeof(cli_chan));
6943 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6944 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6949 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6951 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6953 if (!ifs->current_ssid ||
6954 !ifs->current_ssid->p2p_group ||
6955 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6956 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6958 freq = ifs->current_ssid->frequency;
6959 if (freq_included(&chan, freq)) {
6960 wpa_dbg(ifs, MSG_DEBUG,
6961 "P2P GO operating frequency %d MHz in valid range",
6966 wpa_dbg(ifs, MSG_DEBUG,
6967 "P2P GO operating in invalid frequency %d MHz", freq);
6968 /* TODO: Consider using CSA or removing the group within
6970 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6975 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6976 struct wpa_scan_results *scan_res)
6978 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6982 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6984 struct wpa_global *global = wpa_s->global;
6988 if (global->p2p == NULL)
6991 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6993 if (wpa_s->pending_interface_name[0] &&
6994 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6997 peer = p2p_get_go_neg_peer(global->p2p);
6999 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7000 MACSTR, MAC2STR(peer));
7001 p2p_unauthorize(global->p2p, peer);
7005 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7006 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7007 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7011 if (wpa_s->pending_pd_before_join) {
7012 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7013 wpa_s->pending_pd_before_join = 0;
7017 wpas_p2p_stop_find(wpa_s);
7019 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7020 if (wpa_s == global->p2p_group_formation &&
7021 (wpa_s->p2p_in_provisioning ||
7022 wpa_s->parent->pending_interface_type ==
7023 WPA_IF_P2P_CLIENT)) {
7024 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7025 "formation found - cancelling",
7028 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7029 wpa_s->parent, NULL);
7030 if (wpa_s->p2p_in_provisioning) {
7031 wpas_group_formation_completed(wpa_s, 0, 0);
7034 wpas_p2p_group_delete(wpa_s,
7035 P2P_GROUP_REMOVAL_REQUESTED);
7037 } else if (wpa_s->p2p_in_invitation) {
7038 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7041 wpas_p2p_group_formation_failed(wpa_s, 0);
7046 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7054 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7056 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7059 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7060 "being available anymore");
7061 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
7065 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7066 int freq_24, int freq_5, int freq_overall)
7068 struct p2p_data *p2p = wpa_s->global->p2p;
7071 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7075 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7078 struct p2p_data *p2p = wpa_s->global->p2p;
7083 if (hwaddr_aton(addr, peer))
7086 return p2p_unauthorize(p2p, peer);
7091 * wpas_p2p_disconnect - Disconnect from a P2P Group
7092 * @wpa_s: Pointer to wpa_supplicant data
7093 * Returns: 0 on success, -1 on failure
7095 * This can be used to disconnect from a group in which the local end is a P2P
7096 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7097 * virtual network interface was created for this group, that interface will be
7098 * removed. Otherwise, only the configured P2P group network will be removed
7099 * from the interface.
7101 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7107 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7112 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7116 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7119 ret = p2p_in_progress(wpa_s->global->p2p);
7122 * Check whether there is an ongoing WPS provisioning step (or
7123 * other parts of group formation) on another interface since
7124 * p2p_in_progress() does not report this to avoid issues for
7125 * scans during such provisioning step.
7127 if (wpa_s->global->p2p_group_formation &&
7128 wpa_s->global->p2p_group_formation != wpa_s) {
7129 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7130 "in group formation",
7131 wpa_s->global->p2p_group_formation->ifname);
7136 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
7137 struct os_reltime now;
7138 os_get_reltime(&now);
7139 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7140 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
7141 /* Wait for the first client has expired */
7142 wpa_s->global->p2p_go_wait_client.sec = 0;
7144 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7153 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7154 struct wpa_ssid *ssid)
7156 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7157 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7158 wpa_s->parent, NULL) > 0) {
7160 * Remove the network by scheduling the group formation
7161 * timeout to happen immediately. The teardown code
7162 * needs to be scheduled to run asynch later so that we
7163 * don't delete data from under ourselves unexpectedly.
7164 * Calling wpas_p2p_group_formation_timeout directly
7165 * causes a series of crashes in WPS failure scenarios.
7167 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7168 "P2P group network getting removed");
7169 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7170 wpa_s->parent, NULL);
7175 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
7176 const u8 *addr, const u8 *ssid,
7182 for (s = wpa_s->conf->ssid; s; s = s->next) {
7183 if (s->disabled != 2)
7186 (ssid_len != s->ssid_len ||
7187 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7190 if (s->mode == WPAS_MODE_P2P_GO)
7194 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7195 return s; /* peer is GO in the persistent group */
7196 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7198 for (i = 0; i < s->num_p2p_clients; i++) {
7199 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
7200 addr, ETH_ALEN) == 0)
7201 return s; /* peer is P2P client in persistent
7210 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7213 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7214 wpa_s->parent, NULL) > 0) {
7216 * This can happen if WPS provisioning step is not terminated
7217 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7218 * peer was able to connect, there is no need to time out group
7219 * formation after this, though. In addition, this is used with
7220 * the initial connection wait on the GO as a separate formation
7221 * timeout and as such, expected to be hit after the initial WPS
7222 * provisioning step.
7224 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7226 if (!wpa_s->p2p_go_group_formation_completed &&
7227 !wpa_s->group_formation_reported) {
7229 * GO has not yet notified group formation success since
7230 * the WPS step was not completed cleanly. Do that
7231 * notification now since the P2P Client was able to
7232 * connect and as such, must have received the
7233 * credential from the WPS step.
7235 if (wpa_s->global->p2p)
7236 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7237 wpas_group_formation_completed(wpa_s, 1, 0);
7240 if (!wpa_s->p2p_go_group_formation_completed) {
7241 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7242 wpa_s->p2p_go_group_formation_completed = 1;
7243 wpa_s->global->p2p_group_formation = NULL;
7244 wpa_s->p2p_in_provisioning = 0;
7245 wpa_s->p2p_in_invitation = 0;
7247 wpa_s->global->p2p_go_wait_client.sec = 0;
7250 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7254 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7257 struct wpa_supplicant *group = wpa_s;
7260 if (wpa_s->global->p2p_group_formation)
7261 group = wpa_s->global->p2p_group_formation;
7262 wpa_s = wpa_s->global->p2p_init_wpa_s;
7263 offchannel_send_action_done(wpa_s);
7265 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7266 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7267 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7268 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7269 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7270 wpa_s->p2p_persistent_id,
7271 wpa_s->p2p_pd_before_go_neg,
7278 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7282 if (!wpa_s->p2p_fallback_to_go_neg ||
7283 wpa_s->p2p_in_provisioning <= 5)
7286 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7287 return 0; /* peer operating as a GO */
7289 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7290 "fallback to GO Negotiation");
7291 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7292 "reason=GO-not-found");
7293 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7295 return res == 1 ? 2 : 1;
7299 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7301 struct wpa_supplicant *ifs;
7303 if (wpa_s->wpa_state > WPA_SCANNING) {
7304 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7305 "concurrent operation",
7306 wpa_s->conf->p2p_search_delay);
7307 return wpa_s->conf->p2p_search_delay;
7310 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7312 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7313 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7314 "delay due to concurrent operation on "
7316 wpa_s->conf->p2p_search_delay,
7318 return wpa_s->conf->p2p_search_delay;
7326 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7327 struct wpa_ssid *s, const u8 *addr,
7330 struct psk_list_entry *psk, *tmp;
7333 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7335 if ((iface_addr && !psk->p2p &&
7336 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7337 (!iface_addr && psk->p2p &&
7338 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7339 wpa_dbg(wpa_s, MSG_DEBUG,
7340 "P2P: Remove persistent group PSK list entry for "
7342 MAC2STR(psk->addr), psk->p2p);
7343 dl_list_del(&psk->list);
7353 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7354 const u8 *p2p_dev_addr,
7355 const u8 *psk, size_t psk_len)
7357 struct wpa_ssid *ssid = wpa_s->current_ssid;
7358 struct wpa_ssid *persistent;
7359 struct psk_list_entry *p, *last;
7361 if (psk_len != sizeof(p->psk))
7365 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7366 " p2p_dev_addr=" MACSTR,
7367 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7368 if (is_zero_ether_addr(p2p_dev_addr))
7369 p2p_dev_addr = NULL;
7371 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7375 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7376 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7377 /* To be added to persistent group once created */
7378 if (wpa_s->global->add_psk == NULL) {
7379 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7380 if (wpa_s->global->add_psk == NULL)
7383 p = wpa_s->global->add_psk;
7386 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7389 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7391 os_memcpy(p->psk, psk, psk_len);
7395 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7396 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7400 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7403 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7407 p = os_zalloc(sizeof(*p));
7412 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7415 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7417 os_memcpy(p->psk, psk, psk_len);
7419 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7420 (last = dl_list_last(&persistent->psk_list,
7421 struct psk_list_entry, list))) {
7422 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7423 MACSTR " (p2p=%u) to make room for a new one",
7424 MAC2STR(last->addr), last->p2p);
7425 dl_list_del(&last->list);
7429 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7430 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7431 p2p_dev_addr == NULL);
7433 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7434 MACSTR, MAC2STR(p2p_dev_addr));
7436 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7439 dl_list_add(&persistent->psk_list, &p->list);
7441 if (wpa_s->parent->conf->update_config &&
7442 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7443 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7447 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7448 struct wpa_ssid *s, const u8 *addr,
7453 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7454 if (res > 0 && wpa_s->conf->update_config &&
7455 wpa_config_write(wpa_s->confname, wpa_s->conf))
7456 wpa_dbg(wpa_s, MSG_DEBUG,
7457 "P2P: Failed to update configuration");
7461 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7462 const u8 *peer, int iface_addr)
7464 struct hostapd_data *hapd;
7465 struct hostapd_wpa_psk *psk, *prev, *rem;
7466 struct sta_info *sta;
7468 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7469 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7472 /* Remove per-station PSK entry */
7473 hapd = wpa_s->ap_iface->bss[0];
7475 psk = hapd->conf->ssid.wpa_psk;
7477 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7479 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7480 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7481 MACSTR " iface_addr=%d",
7482 MAC2STR(peer), iface_addr);
7484 prev->next = psk->next;
7486 hapd->conf->ssid.wpa_psk = psk->next;
7496 /* Disconnect from group */
7498 sta = ap_get_sta(hapd, peer);
7500 sta = ap_get_sta_p2p(hapd, peer);
7502 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7503 " (iface_addr=%d) from group",
7504 MAC2STR(peer), iface_addr);
7505 hostapd_drv_sta_deauth(hapd, sta->addr,
7506 WLAN_REASON_DEAUTH_LEAVING);
7507 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7512 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7516 struct wpa_supplicant *w;
7517 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
7519 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7521 /* Remove from any persistent group */
7522 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
7523 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7526 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7527 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
7530 /* Remove from any operating group */
7531 for (w = wpa_s->global->ifaces; w; w = w->next)
7532 wpas_p2p_remove_client_go(w, peer, iface_addr);
7536 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7538 struct wpa_supplicant *wpa_s = eloop_ctx;
7539 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7543 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7545 struct wpa_supplicant *wpa_s = eloop_ctx;
7547 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7548 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7552 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7553 struct wpa_ssid *ssid)
7555 struct wpa_supplicant *iface;
7557 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7558 if (!iface->current_ssid ||
7559 iface->current_ssid->frequency == freq ||
7560 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7561 !iface->current_ssid->p2p_group))
7564 /* Remove the connection with least priority */
7565 if (!wpas_is_p2p_prioritized(iface)) {
7566 /* STA connection has priority over existing
7567 * P2P connection, so remove the interface. */
7568 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7569 eloop_register_timeout(0, 0,
7570 wpas_p2p_group_freq_conflict,
7572 /* If connection in progress is P2P connection, do not
7573 * proceed for the connection. */
7579 /* P2P connection has priority, disable the STA network
7581 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7583 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7584 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7585 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7587 /* If P2P connection is in progress, continue
7600 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7602 struct wpa_ssid *ssid = wpa_s->current_ssid;
7604 if (ssid == NULL || !ssid->p2p_group)
7607 if (wpa_s->p2p_last_4way_hs_fail &&
7608 wpa_s->p2p_last_4way_hs_fail == ssid) {
7609 u8 go_dev_addr[ETH_ALEN];
7610 struct wpa_ssid *persistent;
7612 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7614 ssid->ssid_len) <= 0) {
7615 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7619 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7620 MACSTR, MAC2STR(go_dev_addr));
7621 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7624 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7625 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7628 wpa_msg_global(wpa_s->parent, MSG_INFO,
7629 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7632 wpa_s->p2p_last_4way_hs_fail = NULL;
7634 * Remove the group from a timeout to avoid issues with caller
7635 * continuing to use the interface if this is on a P2P group
7638 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7643 wpa_s->p2p_last_4way_hs_fail = ssid;
7648 #ifdef CONFIG_WPS_NFC
7650 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7658 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7662 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7663 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7670 wpabuf_put_be16(ret, wsc_len);
7672 wpabuf_put_buf(ret, wsc);
7673 wpabuf_put_be16(ret, wpabuf_len(p2p));
7674 wpabuf_put_buf(ret, p2p);
7678 wpa_hexdump_buf(MSG_DEBUG,
7679 "P2P: Generated NFC connection handover message", ret);
7683 tmp = ndef_build_p2p(ret);
7686 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7696 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7697 struct wpa_ssid **ssid, u8 *go_dev_addr)
7699 struct wpa_supplicant *iface;
7702 os_memset(go_dev_addr, 0, ETH_ALEN);
7705 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7706 if (iface->wpa_state < WPA_ASSOCIATING ||
7707 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7708 !iface->current_ssid->p2p_group ||
7709 iface->current_ssid->mode != WPAS_MODE_INFRA)
7712 *ssid = iface->current_ssid;
7714 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7715 return iface->assoc_freq;
7721 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7724 struct wpabuf *wsc, *p2p;
7725 struct wpa_ssid *ssid;
7726 u8 go_dev_addr[ETH_ALEN];
7727 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7729 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7730 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7734 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7735 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7736 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7737 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7741 if (cli_freq == 0) {
7742 wsc = wps_build_nfc_handover_req_p2p(
7743 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7746 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7747 go_dev_addr, ssid ? ssid->ssid : NULL,
7748 ssid ? ssid->ssid_len : 0);
7750 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7754 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7757 struct wpabuf *wsc, *p2p;
7758 struct wpa_ssid *ssid;
7759 u8 go_dev_addr[ETH_ALEN];
7760 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7762 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7765 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7766 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7767 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7770 if (cli_freq == 0) {
7771 wsc = wps_build_nfc_handover_sel_p2p(
7773 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7774 DEV_PW_NFC_CONNECTION_HANDOVER,
7775 wpa_s->conf->wps_nfc_dh_pubkey,
7776 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7779 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7780 go_dev_addr, ssid ? ssid->ssid : NULL,
7781 ssid ? ssid->ssid_len : 0);
7783 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7787 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7788 struct p2p_nfc_params *params)
7790 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7791 "connection handover (freq=%d)",
7794 if (params->go_freq && params->go_ssid_len) {
7795 wpa_s->p2p_wps_method = WPS_NFC;
7796 wpa_s->pending_join_wps_method = WPS_NFC;
7797 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7798 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7800 return wpas_p2p_join_start(wpa_s, params->go_freq,
7802 params->go_ssid_len);
7805 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7806 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7807 params->go_freq, -1, 0, 1, 1);
7811 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7812 struct p2p_nfc_params *params, int tag)
7814 int res, persistent;
7815 struct wpa_ssid *ssid;
7817 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7818 "connection handover");
7819 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7820 ssid = wpa_s->current_ssid;
7823 if (ssid->mode != WPAS_MODE_P2P_GO)
7825 if (wpa_s->ap_iface == NULL)
7829 if (wpa_s == NULL) {
7830 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7834 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7835 DEV_PW_NFC_CONNECTION_HANDOVER &&
7836 !wpa_s->parent->p2p_oob_dev_pw) {
7837 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7840 res = wpas_ap_wps_add_nfc_pw(
7841 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7842 wpa_s->parent->p2p_oob_dev_pw,
7843 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7844 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7849 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7853 if (!params->peer ||
7854 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7857 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7858 " to join", MAC2STR(params->peer->p2p_device_addr));
7860 wpa_s->global->p2p_invite_group = wpa_s;
7861 persistent = ssid->p2p_persistent_group &&
7862 wpas_p2p_get_persistent(wpa_s->parent,
7863 params->peer->p2p_device_addr,
7864 ssid->ssid, ssid->ssid_len);
7865 wpa_s->parent->pending_invite_ssid_id = -1;
7867 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7868 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7869 ssid->ssid, ssid->ssid_len, ssid->frequency,
7870 wpa_s->global->p2p_dev_addr, persistent, 0,
7871 wpa_s->parent->p2p_oob_dev_pw_id);
7875 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7876 struct p2p_nfc_params *params,
7879 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7880 "connection handover");
7881 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7882 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7883 forced_freq, -1, 0, 1, 1);
7887 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7888 struct p2p_nfc_params *params,
7893 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7894 "connection handover");
7895 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7896 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7897 forced_freq, -1, 0, 1, 1);
7901 res = wpas_p2p_listen(wpa_s, 60);
7903 p2p_unauthorize(wpa_s->global->p2p,
7904 params->peer->p2p_device_addr);
7911 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7912 const struct wpabuf *data,
7913 int sel, int tag, int forced_freq)
7915 const u8 *pos, *end;
7917 struct p2p_nfc_params params;
7920 os_memset(¶ms, 0, sizeof(params));
7923 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7925 pos = wpabuf_head(data);
7926 end = pos + wpabuf_len(data);
7928 if (end - pos < 2) {
7929 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7933 len = WPA_GET_BE16(pos);
7935 if (len > end - pos) {
7936 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7940 params.wsc_attr = pos;
7941 params.wsc_len = len;
7944 if (end - pos < 2) {
7945 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7949 len = WPA_GET_BE16(pos);
7951 if (len > end - pos) {
7952 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7956 params.p2p_attr = pos;
7957 params.p2p_len = len;
7960 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7961 params.wsc_attr, params.wsc_len);
7962 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7963 params.p2p_attr, params.p2p_len);
7965 wpa_hexdump(MSG_DEBUG,
7966 "P2P: Ignored extra data after P2P attributes",
7970 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
7974 if (params.next_step == NO_ACTION)
7977 if (params.next_step == BOTH_GO) {
7978 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7979 MAC2STR(params.peer->p2p_device_addr));
7983 if (params.next_step == PEER_CLIENT) {
7984 if (!is_zero_ether_addr(params.go_dev_addr)) {
7985 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7986 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7988 MAC2STR(params.peer->p2p_device_addr),
7990 MAC2STR(params.go_dev_addr),
7991 wpa_ssid_txt(params.go_ssid,
7992 params.go_ssid_len));
7994 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7995 "peer=" MACSTR " freq=%d",
7996 MAC2STR(params.peer->p2p_device_addr),
8002 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8003 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8004 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8008 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8009 wpa_s->p2p_oob_dev_pw = NULL;
8011 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8012 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8017 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8018 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8019 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8020 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8021 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8022 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8023 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8027 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8028 "peer OOB Device Password Id %u", id);
8031 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8032 "Device Password Id %u", id);
8033 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8034 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8035 params.oob_dev_pw_len -
8036 WPS_OOB_PUBKEY_HASH_LEN - 2);
8037 wpa_s->p2p_oob_dev_pw_id = id;
8038 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8039 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8040 params.oob_dev_pw_len -
8041 WPS_OOB_PUBKEY_HASH_LEN - 2);
8042 if (wpa_s->p2p_oob_dev_pw == NULL)
8045 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8046 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8047 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8050 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8051 "without Device Password");
8052 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8055 switch (params.next_step) {
8059 /* already covered above */
8062 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
8064 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
8066 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
8068 /* TODO: use own OOB Dev Pw */
8069 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
8076 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8077 const struct wpabuf *data, int forced_freq)
8079 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8082 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8086 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8087 const struct wpabuf *req,
8088 const struct wpabuf *sel, int forced_freq)
8093 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8096 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8098 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8099 wpabuf_head(req), wpabuf_len(req));
8100 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8101 wpabuf_head(sel), wpabuf_len(sel));
8103 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8104 tmp = ndef_parse_p2p(init ? sel : req);
8106 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8110 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8118 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8121 int go_intent = wpa_s->conf->p2p_go_intent;
8122 struct wpa_supplicant *iface;
8124 if (wpa_s->global->p2p == NULL)
8128 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8129 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8131 if (!iface->ap_iface)
8133 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8135 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8137 if (wpa_s->p2p_nfc_tag_enabled)
8138 wpas_p2p_remove_pending_group_interface(wpa_s);
8139 wpa_s->p2p_nfc_tag_enabled = 0;
8143 if (wpa_s->global->p2p_disabled)
8146 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8147 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8148 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8149 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8150 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8151 "to allow static handover cases");
8155 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8157 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8158 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8159 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8160 if (wpa_s->p2p_oob_dev_pw == NULL)
8162 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8164 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8165 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8167 * P2P Group Interface present and the command came on group
8168 * interface, so enable the token for the current interface.
8170 wpa_s->create_p2p_iface = 0;
8172 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8175 if (wpa_s->create_p2p_iface) {
8176 enum wpa_driver_if_type iftype;
8177 /* Prepare to add a new interface for the group */
8178 iftype = WPA_IF_P2P_GROUP;
8179 if (go_intent == 15)
8180 iftype = WPA_IF_P2P_GO;
8181 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8182 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8183 "interface for the group");
8187 if_addr = wpa_s->pending_interface_addr;
8189 if_addr = wpa_s->own_addr;
8191 wpa_s->p2p_nfc_tag_enabled = enabled;
8193 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8194 struct hostapd_data *hapd;
8195 if (iface->ap_iface == NULL)
8197 hapd = iface->ap_iface->bss[0];
8198 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8199 hapd->conf->wps_nfc_dh_pubkey =
8200 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8201 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8202 hapd->conf->wps_nfc_dh_privkey =
8203 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8204 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8205 hapd->conf->wps_nfc_dev_pw =
8206 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8207 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8209 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8210 wpa_dbg(iface, MSG_DEBUG,
8211 "P2P: Failed to enable NFC Tag for GO");
8214 p2p_set_authorized_oob_dev_pw_id(
8215 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8221 #endif /* CONFIG_WPS_NFC */
8224 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8225 struct wpa_used_freq_data *freqs,
8228 u8 curr_chan, cand, chan;
8231 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8232 for (i = 0, cand = 0; i < num; i++) {
8233 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8234 if (curr_chan == chan) {
8239 if (chan == 1 || chan == 6 || chan == 11)
8244 wpa_dbg(wpa_s, MSG_DEBUG,
8245 "P2P: Update Listen channel to %u based on operating channel",
8247 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8252 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8254 struct wpa_used_freq_data *freqs;
8255 unsigned int num = wpa_s->num_multichan_concurrent;
8257 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8261 * If possible, optimize the Listen channel to be a channel that is
8262 * already used by one of the other interfaces.
8264 if (!wpa_s->conf->p2p_optimize_listen_chan)
8267 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8270 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8274 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8276 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8281 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8283 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8284 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8285 "the management interface is being removed");
8286 wpas_p2p_deinit_global(wpa_s->global);
8291 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8293 if (wpa_s->ap_iface->bss)
8294 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8295 wpas_p2p_group_deinit(wpa_s);