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);
132 * Get the number of concurrent channels that the HW can operate, but that are
133 * currently not in use by any of the wpa_supplicant interfaces.
135 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
140 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
144 num = get_shared_radio_freqs(wpa_s, freqs,
145 wpa_s->num_multichan_concurrent);
148 unused = wpa_s->num_multichan_concurrent - num;
149 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
155 * Get the frequencies that are currently in use by one or more of the virtual
156 * interfaces, and that are also valid for P2P operation.
159 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
160 struct wpa_used_freq_data *p2p_freqs,
163 struct wpa_used_freq_data *freqs;
164 unsigned int num, i, j;
166 freqs = os_calloc(wpa_s->num_multichan_concurrent,
167 sizeof(struct wpa_used_freq_data));
171 num = get_shared_radio_freqs_data(wpa_s, freqs,
172 wpa_s->num_multichan_concurrent);
174 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
176 for (i = 0, j = 0; i < num && j < len; i++) {
177 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
178 p2p_freqs[j++] = freqs[i];
183 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
189 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
195 /* Use the wpa_s used to control the P2P Device operation */
196 wpa_s = wpa_s->global->p2p_init_wpa_s;
198 if (wpa_s->conf->p2p_ignore_shared_freq &&
199 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
200 wpas_p2p_num_unused_channels(wpa_s) > 0) {
201 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
205 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
209 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
210 struct wpa_scan_results *scan_res)
214 if (wpa_s->p2p_scan_work) {
215 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
216 wpa_s->p2p_scan_work = NULL;
217 radio_work_done(work);
220 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
223 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
224 (int) scan_res->num);
226 for (i = 0; i < scan_res->num; i++) {
227 struct wpa_scan_res *bss = scan_res->res[i];
228 struct os_reltime time_tmp_age, entry_ts;
232 time_tmp_age.sec = bss->age / 1000;
233 time_tmp_age.usec = (bss->age % 1000) * 1000;
234 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
236 ies = (const u8 *) (bss + 1);
237 ies_len = bss->ie_len;
238 if (bss->beacon_ie_len > 0 &&
239 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
240 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
241 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
242 MACSTR, MAC2STR(bss->bssid));
244 ies_len = bss->beacon_ie_len;
248 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
249 bss->freq, &entry_ts, bss->level,
254 p2p_scan_res_handled(wpa_s->global->p2p);
258 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
260 struct wpa_supplicant *wpa_s = work->wpa_s;
261 struct wpa_driver_scan_params *params = work->ctx;
265 if (!work->started) {
266 wpa_scan_free_params(params);
270 wpa_s->p2p_scan_work = NULL;
274 ret = wpa_drv_scan(wpa_s, params);
275 wpa_scan_free_params(params);
278 radio_work_done(work);
279 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
283 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
284 os_get_reltime(&wpa_s->scan_trigger_time);
285 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
286 wpa_s->own_scan_requested = 1;
287 wpa_s->p2p_scan_work = work;
291 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
294 if (wpa_s->global->p2p_24ghz_social_channels &&
295 (freq == 2412 || freq == 2437 || freq == 2462)) {
297 * Search all social channels regardless of whether these have
298 * been disabled for P2P operating channel use to avoid missing
303 return p2p_supported_freq(wpa_s->global->p2p, freq);
307 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
308 unsigned int num_req_dev_types,
309 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
311 struct wpa_supplicant *wpa_s = ctx;
312 struct wpa_driver_scan_params *params = NULL;
313 struct wpabuf *wps_ie, *ies;
314 unsigned int num_channels = 0;
315 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
319 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
322 if (wpa_s->p2p_scan_work) {
323 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
327 params = os_zalloc(sizeof(*params));
331 /* P2P Wildcard SSID */
332 params->num_ssids = 1;
333 n = os_malloc(P2P_WILDCARD_SSID_LEN);
336 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
337 params->ssids[0].ssid = n;
338 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
340 wpa_s->wps->dev.p2p = 1;
341 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
342 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
343 num_req_dev_types, req_dev_types);
347 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
348 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
353 wpabuf_put_buf(ies, wps_ie);
356 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
358 params->p2p_probe = 1;
359 n = os_malloc(wpabuf_len(ies));
364 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
365 params->extra_ies = n;
366 params->extra_ies_len = wpabuf_len(ies);
370 case P2P_SCAN_SOCIAL:
371 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
373 if (params->freqs == NULL)
375 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
376 if (wpas_p2p_search_social_channel(
377 wpa_s, social_channels_freq[i]))
378 params->freqs[num_channels++] =
379 social_channels_freq[i];
381 params->freqs[num_channels++] = 0;
385 case P2P_SCAN_SPECIFIC:
386 params->freqs = os_calloc(2, sizeof(int));
387 if (params->freqs == NULL)
389 params->freqs[0] = freq;
390 params->freqs[1] = 0;
392 case P2P_SCAN_SOCIAL_PLUS_ONE:
393 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
395 if (params->freqs == NULL)
397 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
398 if (wpas_p2p_search_social_channel(
399 wpa_s, social_channels_freq[i]))
400 params->freqs[num_channels++] =
401 social_channels_freq[i];
403 if (p2p_supported_freq(wpa_s->global->p2p, freq))
404 params->freqs[num_channels++] = freq;
405 params->freqs[num_channels++] = 0;
409 radio_remove_works(wpa_s, "p2p-scan", 0);
410 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
416 wpa_scan_free_params(params);
421 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
423 switch (p2p_group_interface) {
424 case P2P_GROUP_INTERFACE_PENDING:
425 return WPA_IF_P2P_GROUP;
426 case P2P_GROUP_INTERFACE_GO:
427 return WPA_IF_P2P_GO;
428 case P2P_GROUP_INTERFACE_CLIENT:
429 return WPA_IF_P2P_CLIENT;
432 return WPA_IF_P2P_GROUP;
436 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
438 size_t ssid_len, int *go)
442 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
443 for (s = wpa_s->conf->ssid; s; s = s->next) {
444 if (s->disabled != 0 || !s->p2p_group ||
445 s->ssid_len != ssid_len ||
446 os_memcmp(ssid, s->ssid, ssid_len) != 0)
448 if (s->mode == WPAS_MODE_P2P_GO &&
449 s != wpa_s->current_ssid)
452 *go = s->mode == WPAS_MODE_P2P_GO;
461 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
463 struct wpa_supplicant *wpa_s = eloop_ctx;
464 wpa_printf(MSG_DEBUG,
465 "P2P: Complete previously requested removal of %s",
467 wpas_p2p_disconnect(wpa_s);
471 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
472 struct wpa_supplicant *calling_wpa_s)
474 if (calling_wpa_s == wpa_s && wpa_s &&
475 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
477 * The calling wpa_s instance is going to be removed. Do that
478 * from an eloop callback to keep the instance available until
479 * the caller has returned. This my be needed, e.g., to provide
480 * control interface responses on the per-interface socket.
482 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
488 return wpas_p2p_disconnect(wpa_s);
492 /* Determine total number of clients in active groups where we are the GO */
493 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
495 unsigned int count = 0;
498 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
499 for (s = wpa_s->conf->ssid; s; s = s->next) {
500 wpa_printf(MSG_DEBUG,
501 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
502 wpa_s, s, s->disabled, s->p2p_group,
504 if (!s->disabled && s->p2p_group &&
505 s->mode == WPAS_MODE_P2P_GO) {
506 count += p2p_get_group_num_members(
516 /* Find an interface for a P2P group where we are the GO */
517 static struct wpa_supplicant *
518 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
520 struct wpa_supplicant *save = NULL;
526 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
527 for (s = wpa_s->conf->ssid; s; s = s->next) {
528 if (s->disabled || !s->p2p_group ||
529 s->mode != WPAS_MODE_P2P_GO)
532 /* Prefer a group with connected clients */
533 if (p2p_get_group_num_members(wpa_s->p2p_group))
539 /* No group with connected clients, so pick the one without (if any) */
544 /* Find an active P2P group where we are the GO */
545 static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
548 struct wpa_ssid *s, *empty = NULL;
553 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
554 for (s = wpa_s->conf->ssid; s; s = s->next) {
555 if (s->disabled || !s->p2p_group ||
556 s->mode != WPAS_MODE_P2P_GO)
559 os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
560 if (p2p_get_group_num_members(wpa_s->p2p_group))
570 /* Find a persistent group where we are the GO */
571 static struct wpa_ssid *
572 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
576 for (s = wpa_s->conf->ssid; s; s = s->next) {
577 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
585 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
587 struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
589 u8 conncap = P2PS_SETUP_NONE;
590 unsigned int owned_members = 0;
591 unsigned int owner = 0;
592 unsigned int client = 0;
593 struct wpa_supplicant *go_wpa_s;
594 struct wpa_ssid *persistent_go;
595 int p2p_no_group_iface;
597 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
600 * For non-concurrent capable devices:
601 * If persistent_go, then no new.
602 * If GO, then no client.
603 * If client, then no GO.
605 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
606 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
607 p2p_no_group_iface = wpa_s->conf->p2p_no_group_iface;
609 wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
610 go_wpa_s, persistent_go);
612 for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
613 tmp_wpa_s = tmp_wpa_s->next) {
614 for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
615 wpa_printf(MSG_DEBUG,
616 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
617 tmp_wpa_s, s, s->disabled,
618 s->p2p_group, s->mode);
619 if (!s->disabled && s->p2p_group) {
620 if (s->mode == WPAS_MODE_P2P_GO) {
622 p2p_get_group_num_members(
623 tmp_wpa_s->p2p_group);
631 /* If not concurrent, restrict our choices */
632 if (p2p_no_group_iface) {
633 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
636 return P2PS_SETUP_NONE;
639 if (role == P2PS_SETUP_CLIENT ||
640 incoming == P2PS_SETUP_GROUP_OWNER ||
641 p2p_client_limit_reached(go_wpa_s->p2p_group))
642 return P2PS_SETUP_NONE;
644 return P2PS_SETUP_GROUP_OWNER;
648 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
650 return P2PS_SETUP_GROUP_OWNER |
652 if (incoming == P2PS_SETUP_NEW) {
655 if (os_get_random(&r, sizeof(r)) < 0 ||
657 return P2PS_SETUP_CLIENT;
658 return P2PS_SETUP_GROUP_OWNER;
664 /* If a required role has been specified, handle it here */
665 if (role && role != P2PS_SETUP_NEW) {
667 case P2PS_SETUP_NONE:
669 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
670 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
674 case P2PS_SETUP_GROUP_OWNER:
676 * Must be a complimentary role - cannot be a client to
677 * more than one peer.
679 if (incoming == role || client)
680 return P2PS_SETUP_NONE;
682 return P2PS_SETUP_CLIENT;
684 case P2PS_SETUP_CLIENT:
685 /* Must be a complimentary role */
686 if (incoming != role) {
687 conncap = P2PS_SETUP_GROUP_OWNER;
692 return P2PS_SETUP_NONE;
697 * For now, we only will support ownership of one group, and being a
698 * client of one group. Therefore, if we have either an existing GO
699 * group, or an existing client group, we will not do a new GO
700 * negotiation, but rather try to re-use the existing groups.
703 case P2PS_SETUP_NONE:
706 conncap = P2PS_SETUP_GROUP_OWNER;
707 else if (!owned_members)
708 conncap = P2PS_SETUP_NEW;
709 else if (incoming == P2PS_SETUP_NONE)
710 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
712 conncap = P2PS_SETUP_CLIENT;
715 case P2PS_SETUP_CLIENT:
716 conncap = P2PS_SETUP_GROUP_OWNER;
719 case P2PS_SETUP_GROUP_OWNER:
721 conncap = P2PS_SETUP_CLIENT;
724 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
725 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
727 conncap = P2PS_SETUP_GROUP_OWNER;
731 if (os_get_random(&r, sizeof(r)) < 0 ||
733 conncap = P2PS_SETUP_CLIENT;
735 conncap = P2PS_SETUP_GROUP_OWNER;
740 return P2PS_SETUP_NONE;
744 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
745 (!incoming && (conncap & P2PS_SETUP_NEW))) {
746 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
747 conncap &= ~P2PS_SETUP_GROUP_OWNER;
748 wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
749 owner, owned_members, conncap);
751 s = wpas_p2p_get_persistent_go(wpa_s);
753 if (!s && !owner && p2p_no_group_iface) {
754 p2p_set_intended_addr(wpa_s->global->p2p,
756 } else if (!s && !owner) {
757 if (wpas_p2p_add_group_interface(wpa_s,
758 WPA_IF_P2P_GO) < 0) {
759 wpa_printf(MSG_ERROR,
760 "P2P: Failed to allocate a new interface for the group");
761 return P2PS_SETUP_NONE;
763 wpa_s->global->pending_group_iface_for_p2ps = 1;
764 p2p_set_intended_addr(wpa_s->global->p2p,
765 wpa_s->pending_interface_addr);
773 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
774 enum p2p_group_removal_reason removal_reason)
776 struct wpa_ssid *ssid;
780 ssid = wpa_s->current_ssid;
783 * The current SSID was not known, but there may still be a
784 * pending P2P group interface waiting for provisioning or a
785 * P2P group that is trying to reconnect.
787 ssid = wpa_s->conf->ssid;
789 if (ssid->p2p_group && ssid->disabled != 2)
794 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
796 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
801 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
803 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
804 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
805 wpa_s->reassociate = 0;
806 wpa_s->disconnected = 1;
811 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
812 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
814 if (os_strcmp(gtype, "client") == 0) {
815 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
816 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
818 wpa_printf(MSG_DEBUG,
819 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
820 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
821 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
826 if (wpa_s->cross_connect_in_use) {
827 wpa_s->cross_connect_in_use = 0;
828 wpa_msg_global(wpa_s->parent, MSG_INFO,
829 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
830 wpa_s->ifname, wpa_s->cross_connect_uplink);
832 switch (removal_reason) {
833 case P2P_GROUP_REMOVAL_REQUESTED:
834 reason = " reason=REQUESTED";
836 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
837 reason = " reason=FORMATION_FAILED";
839 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
840 reason = " reason=IDLE";
842 case P2P_GROUP_REMOVAL_UNAVAILABLE:
843 reason = " reason=UNAVAILABLE";
845 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
846 reason = " reason=GO_ENDING_SESSION";
848 case P2P_GROUP_REMOVAL_PSK_FAILURE:
849 reason = " reason=PSK_FAILURE";
851 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
852 reason = " reason=FREQ_CONFLICT";
858 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
859 wpa_msg_global(wpa_s->parent, MSG_INFO,
860 P2P_EVENT_GROUP_REMOVED "%s %s%s",
861 wpa_s->ifname, gtype, reason);
864 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
865 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
866 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
867 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
868 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
869 wpa_s->parent, NULL) > 0) {
870 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
872 wpa_s->p2p_in_provisioning = 0;
875 wpa_s->p2p_in_invitation = 0;
878 * Make sure wait for the first client does not remain active after the
879 * group has been removed.
881 wpa_s->global->p2p_go_wait_client.sec = 0;
883 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
884 struct wpa_global *global;
886 enum wpa_driver_if_type type;
887 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
889 global = wpa_s->global;
890 ifname = os_strdup(wpa_s->ifname);
891 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
892 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
893 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
894 wpa_s = global->ifaces;
896 wpa_drv_if_remove(wpa_s, type, ifname);
901 if (!wpa_s->p2p_go_group_formation_completed) {
902 wpa_s->global->p2p_group_formation = NULL;
903 wpa_s->p2p_in_provisioning = 0;
906 wpa_s->show_group_started = 0;
907 os_free(wpa_s->go_params);
908 wpa_s->go_params = NULL;
910 os_free(wpa_s->p2p_group_common_freqs);
911 wpa_s->p2p_group_common_freqs = NULL;
912 wpa_s->p2p_group_common_freqs_num = 0;
914 wpa_s->waiting_presence_resp = 0;
916 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
917 if (ssid && (ssid->p2p_group ||
918 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
919 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
921 if (ssid == wpa_s->current_ssid) {
922 wpa_sm_set_config(wpa_s->wpa, NULL);
923 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
924 wpa_s->current_ssid = NULL;
927 * Networks objects created during any P2P activities are not
928 * exposed out as they might/will confuse certain non-P2P aware
929 * applications since these network objects won't behave like
932 * Likewise, we don't send out network removed signals for such
935 wpa_config_remove_network(wpa_s->conf, id);
936 wpa_supplicant_clear_status(wpa_s);
937 wpa_supplicant_cancel_sched_scan(wpa_s);
939 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
943 wpa_supplicant_ap_deinit(wpa_s);
945 wpa_drv_deinit_p2p_cli(wpa_s);
951 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
953 const u8 *ssid, size_t ssid_len)
961 if (wpa_s->go_params)
962 bssid = wpa_s->go_params->peer_interface_addr;
964 bssid = wpa_s->bssid;
966 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
967 if (bss == NULL && wpa_s->go_params &&
968 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
969 bss = wpa_bss_get_p2p_dev_addr(
970 wpa_s, wpa_s->go_params->peer_device_addr);
972 u8 iface_addr[ETH_ALEN];
973 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
975 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
978 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
979 "group is persistent - BSS " MACSTR " not found",
984 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
986 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
989 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
990 "group is persistent - BSS " MACSTR
991 " did not include P2P IE", MAC2STR(bssid));
992 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
993 (u8 *) (bss + 1), bss->ie_len);
994 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
995 ((u8 *) bss + 1) + bss->ie_len,
1000 group_capab = p2p_get_group_capab(p2p);
1001 addr = p2p_get_go_dev_addr(p2p);
1002 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1003 "group_capab=0x%x", group_capab);
1005 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1006 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1009 os_memset(go_dev_addr, 0, ETH_ALEN);
1012 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1013 "go_dev_addr=" MACSTR,
1014 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1016 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1020 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1021 struct wpa_ssid *ssid,
1022 const u8 *go_dev_addr)
1027 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1028 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1029 for (s = wpa_s->conf->ssid; s; s = s->next) {
1030 if (s->disabled == 2 &&
1031 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1032 s->ssid_len == ssid->ssid_len &&
1033 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1038 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1040 if (ssid->passphrase && !s->passphrase)
1042 else if (ssid->passphrase && s->passphrase &&
1043 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1046 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1049 s = wpa_config_add_network(wpa_s->conf);
1054 * Instead of network_added we emit persistent_group_added
1055 * notification. Also to keep the defense checks in
1056 * persistent_group obj registration method, we set the
1057 * relevant flags in s to designate it as a persistent group.
1060 s->p2p_persistent_group = 1;
1061 wpas_notify_persistent_group_added(wpa_s, s);
1062 wpa_config_set_network_defaults(s);
1066 s->p2p_persistent_group = 1;
1069 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1070 s->mode = ssid->mode;
1071 s->auth_alg = WPA_AUTH_ALG_OPEN;
1072 s->key_mgmt = WPA_KEY_MGMT_PSK;
1073 s->proto = WPA_PROTO_RSN;
1074 s->pairwise_cipher = WPA_CIPHER_CCMP;
1076 if (ssid->passphrase) {
1077 os_free(s->passphrase);
1078 s->passphrase = os_strdup(ssid->passphrase);
1080 if (ssid->psk_set) {
1082 os_memcpy(s->psk, ssid->psk, 32);
1084 if (s->passphrase && !s->psk_set)
1085 wpa_config_update_psk(s);
1086 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1088 s->ssid = os_malloc(ssid->ssid_len);
1091 s->ssid_len = ssid->ssid_len;
1092 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1094 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1095 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1096 wpa_s->global->add_psk = NULL;
1100 if (changed && wpa_s->conf->update_config &&
1101 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1102 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1109 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1112 struct wpa_ssid *ssid, *s;
1116 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
1118 ssid = wpa_s->current_ssid;
1119 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1120 !ssid->p2p_persistent_group)
1123 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
1124 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1127 if (s->ssid_len == ssid->ssid_len &&
1128 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1135 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
1136 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
1140 if (i == s->num_p2p_clients - 1)
1141 return; /* already the most recent entry */
1143 /* move the entry to mark it most recent */
1144 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1145 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1146 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
1147 os_memcpy(s->p2p_client_list +
1148 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1150 os_memset(s->p2p_client_list +
1151 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1157 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1158 n = os_realloc_array(s->p2p_client_list,
1159 s->num_p2p_clients + 1, 2 * ETH_ALEN);
1162 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1164 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1166 s->p2p_client_list = n;
1167 s->num_p2p_clients++;
1168 } else if (!found && s->p2p_client_list) {
1169 /* Not enough room for an additional entry - drop the oldest
1171 os_memmove(s->p2p_client_list,
1172 s->p2p_client_list + 2 * ETH_ALEN,
1173 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1174 os_memcpy(s->p2p_client_list +
1175 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1177 os_memset(s->p2p_client_list +
1178 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1182 if (p2p_wpa_s->conf->update_config &&
1183 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
1184 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1188 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1189 int go, struct wpa_ssid *ssid, int freq,
1190 const u8 *psk, const char *passphrase,
1191 const u8 *go_dev_addr, int persistent,
1194 const char *ssid_txt;
1198 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1203 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1207 if (passphrase && passphrase[0] == '\0')
1211 * Include PSK/passphrase only in the control interface message and
1212 * leave it out from the debug log entry.
1214 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1215 P2P_EVENT_GROUP_STARTED
1216 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1218 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1219 psk ? " psk=" : "", psk_txt,
1220 passphrase ? " passphrase=\"" : "",
1221 passphrase ? passphrase : "",
1222 passphrase ? "\"" : "",
1223 MAC2STR(go_dev_addr),
1224 persistent ? " [PERSISTENT]" : "", extra);
1225 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1226 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1227 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1228 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1233 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1236 struct wpa_ssid *ssid;
1239 u8 go_dev_addr[ETH_ALEN];
1240 int network_id = -1;
1243 * This callback is likely called for the main interface. Update wpa_s
1244 * to use the group interface if a new interface was created for the
1247 if (wpa_s->global->p2p_group_formation)
1248 wpa_s = wpa_s->global->p2p_group_formation;
1249 if (wpa_s->p2p_go_group_formation_completed) {
1250 wpa_s->global->p2p_group_formation = NULL;
1251 wpa_s->p2p_in_provisioning = 0;
1253 wpa_s->p2p_in_invitation = 0;
1254 wpa_s->group_formation_reported = 1;
1257 wpa_msg_global(wpa_s->parent, MSG_INFO,
1258 P2P_EVENT_GROUP_FORMATION_FAILURE);
1259 wpas_p2p_group_delete(wpa_s,
1260 P2P_GROUP_REMOVAL_FORMATION_FAILED);
1264 wpa_msg_global(wpa_s->parent, MSG_INFO,
1265 P2P_EVENT_GROUP_FORMATION_SUCCESS);
1267 ssid = wpa_s->current_ssid;
1268 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1269 ssid->mode = WPAS_MODE_P2P_GO;
1270 p2p_group_notif_formation_done(wpa_s->p2p_group);
1271 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1276 client = ssid->mode == WPAS_MODE_INFRA;
1277 if (ssid->mode == WPAS_MODE_P2P_GO) {
1278 persistent = ssid->p2p_persistent_group;
1279 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1282 persistent = wpas_p2p_persistent_group(wpa_s,
1287 client = wpa_s->p2p_group_interface ==
1288 P2P_GROUP_INTERFACE_CLIENT;
1289 os_memset(go_dev_addr, 0, ETH_ALEN);
1292 wpa_s->show_group_started = 0;
1295 * Indicate event only after successfully completed 4-way
1296 * handshake, i.e., when the interface is ready for data
1299 wpa_s->show_group_started = 1;
1301 wpas_p2p_group_started(wpa_s, 1, ssid,
1302 ssid ? ssid->frequency : 0,
1303 ssid && ssid->passphrase == NULL &&
1304 ssid->psk_set ? ssid->psk : NULL,
1305 ssid ? ssid->passphrase : NULL,
1306 go_dev_addr, persistent, "");
1307 wpas_p2p_cross_connect_setup(wpa_s);
1308 wpas_p2p_set_group_idle_timeout(wpa_s);
1312 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1315 os_free(wpa_s->global->add_psk);
1316 wpa_s->global->add_psk = NULL;
1318 if (network_id < 0 && ssid)
1319 network_id = ssid->id;
1321 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1322 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1327 struct send_action_work {
1333 unsigned int wait_time;
1338 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1341 struct wpa_supplicant *wpa_s = eloop_ctx;
1343 if (!wpa_s->p2p_send_action_work)
1346 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1347 os_free(wpa_s->p2p_send_action_work->ctx);
1348 radio_work_done(wpa_s->p2p_send_action_work);
1349 wpa_s->p2p_send_action_work = NULL;
1353 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1355 if (wpa_s->p2p_send_action_work) {
1356 struct send_action_work *awork;
1357 awork = wpa_s->p2p_send_action_work->ctx;
1358 if (awork->wait_time == 0) {
1360 radio_work_done(wpa_s->p2p_send_action_work);
1361 wpa_s->p2p_send_action_work = NULL;
1364 * In theory, this should not be needed, but number of
1365 * places in the P2P code is still using non-zero wait
1366 * time for the last Action frame in the sequence and
1367 * some of these do not call send_action_done().
1369 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1371 eloop_register_timeout(
1372 0, awork->wait_time * 1000,
1373 wpas_p2p_send_action_work_timeout,
1380 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1382 const u8 *dst, const u8 *src,
1384 const u8 *data, size_t data_len,
1385 enum offchannel_send_action_result
1388 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1390 wpas_p2p_action_tx_clear(wpa_s);
1392 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1396 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1397 res = P2P_SEND_ACTION_SUCCESS;
1399 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1400 res = P2P_SEND_ACTION_NO_ACK;
1402 case OFFCHANNEL_SEND_ACTION_FAILED:
1403 res = P2P_SEND_ACTION_FAILED;
1407 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1409 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1410 wpa_s->pending_pd_before_join &&
1411 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1412 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1413 wpa_s->p2p_fallback_to_go_neg) {
1414 wpa_s->pending_pd_before_join = 0;
1415 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1416 "during p2p_connect-auto");
1417 wpa_msg_global(wpa_s->parent, MSG_INFO,
1418 P2P_EVENT_FALLBACK_TO_GO_NEG
1419 "reason=no-ACK-to-PD-Req");
1420 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1426 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1428 struct wpa_supplicant *wpa_s = work->wpa_s;
1429 struct send_action_work *awork = work->ctx;
1432 if (work->started) {
1433 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1435 wpa_s->p2p_send_action_work = NULL;
1436 offchannel_send_action_done(wpa_s);
1442 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1443 awork->bssid, awork->buf, awork->len,
1445 wpas_p2p_send_action_tx_status, 1) < 0) {
1447 radio_work_done(work);
1450 wpa_s->p2p_send_action_work = work;
1454 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1455 unsigned int freq, const u8 *dst,
1456 const u8 *src, const u8 *bssid, const u8 *buf,
1457 size_t len, unsigned int wait_time)
1459 struct send_action_work *awork;
1461 if (wpa_s->p2p_send_action_work) {
1462 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1466 awork = os_zalloc(sizeof(*awork) + len);
1471 os_memcpy(awork->dst, dst, ETH_ALEN);
1472 os_memcpy(awork->src, src, ETH_ALEN);
1473 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1475 awork->wait_time = wait_time;
1476 os_memcpy(awork->buf, buf, len);
1478 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1479 wpas_send_action_cb, awork) < 0) {
1488 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1489 const u8 *src, const u8 *bssid, const u8 *buf,
1490 size_t len, unsigned int wait_time)
1492 struct wpa_supplicant *wpa_s = ctx;
1493 int listen_freq = -1, send_freq = -1;
1495 if (wpa_s->p2p_listen_work)
1496 listen_freq = wpa_s->p2p_listen_work->freq;
1497 if (wpa_s->p2p_send_action_work)
1498 send_freq = wpa_s->p2p_send_action_work->freq;
1499 if (listen_freq != (int) freq && send_freq != (int) freq) {
1500 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1501 listen_freq, send_freq);
1502 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1506 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1507 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1509 wpas_p2p_send_action_tx_status, 1);
1513 static void wpas_send_action_done(void *ctx)
1515 struct wpa_supplicant *wpa_s = ctx;
1517 if (wpa_s->p2p_send_action_work) {
1518 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1520 os_free(wpa_s->p2p_send_action_work->ctx);
1521 radio_work_done(wpa_s->p2p_send_action_work);
1522 wpa_s->p2p_send_action_work = NULL;
1525 offchannel_send_action_done(wpa_s);
1529 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1530 struct p2p_go_neg_results *params)
1532 if (wpa_s->go_params == NULL) {
1533 wpa_s->go_params = os_malloc(sizeof(*params));
1534 if (wpa_s->go_params == NULL)
1537 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1542 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1543 struct p2p_go_neg_results *res)
1545 wpa_s->group_formation_reported = 0;
1546 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1547 " dev_addr " MACSTR " wps_method %d",
1548 MAC2STR(res->peer_interface_addr),
1549 MAC2STR(res->peer_device_addr), res->wps_method);
1550 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1551 res->ssid, res->ssid_len);
1552 wpa_supplicant_ap_deinit(wpa_s);
1553 wpas_copy_go_neg_results(wpa_s, res);
1554 if (res->wps_method == WPS_PBC) {
1555 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1556 #ifdef CONFIG_WPS_NFC
1557 } else if (res->wps_method == WPS_NFC) {
1558 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1559 res->peer_interface_addr,
1560 wpa_s->parent->p2p_oob_dev_pw,
1561 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1562 wpa_s->parent->p2p_oob_dev_pw_id ==
1563 DEV_PW_NFC_CONNECTION_HANDOVER ?
1564 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1567 #endif /* CONFIG_WPS_NFC */
1569 u16 dev_pw_id = DEV_PW_DEFAULT;
1570 if (wpa_s->p2p_wps_method == WPS_P2PS)
1571 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1572 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1573 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1574 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1575 wpa_s->p2p_pin, 1, dev_pw_id);
1580 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1581 struct wpa_ssid *ssid)
1583 struct wpa_ssid *persistent;
1584 struct psk_list_entry *psk;
1585 struct hostapd_data *hapd;
1587 if (!wpa_s->ap_iface)
1590 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1592 if (persistent == NULL)
1595 hapd = wpa_s->ap_iface->bss[0];
1597 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1599 struct hostapd_wpa_psk *hpsk;
1601 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1603 MAC2STR(psk->addr), psk->p2p);
1604 hpsk = os_zalloc(sizeof(*hpsk));
1607 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1609 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1611 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1612 hpsk->next = hapd->conf->ssid.wpa_psk;
1613 hapd->conf->ssid.wpa_psk = hpsk;
1618 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1622 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1623 wpa_s->p2p_group_common_freqs_num);
1625 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1626 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1627 i, wpa_s->p2p_group_common_freqs[i]);
1631 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1632 struct p2p_go_neg_results *params)
1634 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1636 wpa_s->p2p_group_common_freqs_num = 0;
1637 os_free(wpa_s->p2p_group_common_freqs);
1638 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1639 if (!wpa_s->p2p_group_common_freqs)
1642 for (i = 0; i < len; i++) {
1643 if (!wpa_s->go_params->freq_list[i])
1645 wpa_s->p2p_group_common_freqs[i] =
1646 wpa_s->go_params->freq_list[i];
1648 wpa_s->p2p_group_common_freqs_num = i;
1652 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1654 #ifndef CONFIG_NO_CONFIG_WRITE
1655 if (wpa_s->parent->conf->update_config &&
1656 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1657 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1658 #endif /* CONFIG_NO_CONFIG_WRITE */
1662 static void p2p_go_configured(void *ctx, void *data)
1664 struct wpa_supplicant *wpa_s = ctx;
1665 struct p2p_go_neg_results *params = data;
1666 struct wpa_ssid *ssid;
1667 int network_id = -1;
1669 p2p_go_save_group_common_freqs(wpa_s, params);
1670 p2p_go_dump_common_freqs(wpa_s);
1672 ssid = wpa_s->current_ssid;
1673 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1674 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1675 if (wpa_s->global->p2p_group_formation == wpa_s)
1676 wpa_s->global->p2p_group_formation = NULL;
1677 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1678 params->passphrase[0] == '\0' ?
1681 wpa_s->global->p2p_dev_addr,
1682 params->persistent_group, "");
1683 wpa_s->group_formation_reported = 1;
1685 if (wpa_s->parent->p2ps_join_addr_valid) {
1686 wpa_dbg(wpa_s, MSG_DEBUG,
1687 "P2PS: Setting default PIN for " MACSTR,
1688 MAC2STR(wpa_s->parent->p2ps_join_addr));
1689 wpa_supplicant_ap_wps_pin(wpa_s,
1690 wpa_s->parent->p2ps_join_addr,
1691 "12345670", NULL, 0, 0);
1692 wpa_s->parent->p2ps_join_addr_valid = 0;
1695 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1696 if (params->persistent_group) {
1697 network_id = wpas_p2p_store_persistent_group(
1698 wpa_s->parent, ssid,
1699 wpa_s->global->p2p_dev_addr);
1700 wpas_p2p_add_psk_list(wpa_s, ssid);
1703 network_id = ssid->id;
1704 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1705 wpas_p2p_cross_connect_setup(wpa_s);
1706 wpas_p2p_set_group_idle_timeout(wpa_s);
1708 if (wpa_s->p2p_first_connection_timeout) {
1709 wpa_dbg(wpa_s, MSG_DEBUG,
1710 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1711 wpa_s->p2p_first_connection_timeout);
1712 wpa_s->p2p_go_group_formation_completed = 0;
1713 wpa_s->global->p2p_group_formation = wpa_s;
1714 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1715 wpa_s->parent, NULL);
1716 eloop_register_timeout(
1717 wpa_s->p2p_first_connection_timeout, 0,
1718 wpas_p2p_group_formation_timeout,
1719 wpa_s->parent, NULL);
1725 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1726 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1727 params->peer_interface_addr)) {
1728 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1732 if (params->wps_method == WPS_PBC) {
1733 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1734 params->peer_device_addr);
1735 #ifdef CONFIG_WPS_NFC
1736 } else if (params->wps_method == WPS_NFC) {
1737 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1738 DEV_PW_NFC_CONNECTION_HANDOVER &&
1739 !wpa_s->parent->p2p_oob_dev_pw) {
1740 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1743 wpas_ap_wps_add_nfc_pw(
1744 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1745 wpa_s->parent->p2p_oob_dev_pw,
1746 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1747 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1748 #endif /* CONFIG_WPS_NFC */
1749 } else if (wpa_s->p2p_pin[0])
1750 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1751 wpa_s->p2p_pin, NULL, 0, 0);
1752 os_free(wpa_s->go_params);
1753 wpa_s->go_params = NULL;
1757 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1758 struct p2p_go_neg_results *params,
1759 int group_formation)
1761 struct wpa_ssid *ssid;
1763 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1764 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1765 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1770 ssid = wpa_config_add_network(wpa_s->conf);
1772 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1776 wpa_s->show_group_started = 0;
1777 wpa_s->p2p_go_group_formation_completed = 0;
1778 wpa_s->group_formation_reported = 0;
1780 wpa_config_set_network_defaults(ssid);
1781 ssid->temporary = 1;
1782 ssid->p2p_group = 1;
1783 ssid->p2p_persistent_group = params->persistent_group;
1784 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1786 ssid->frequency = params->freq;
1787 ssid->ht40 = params->ht40;
1788 ssid->vht = params->vht;
1789 ssid->ssid = os_zalloc(params->ssid_len + 1);
1791 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1792 ssid->ssid_len = params->ssid_len;
1794 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1795 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1796 ssid->proto = WPA_PROTO_RSN;
1797 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1798 ssid->group_cipher = WPA_CIPHER_CCMP;
1799 if (params->freq > 56160) {
1801 * Enable GCMP instead of CCMP as pairwise_cipher and
1802 * group_cipher in 60 GHz.
1804 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1805 ssid->group_cipher = WPA_CIPHER_GCMP;
1807 if (os_strlen(params->passphrase) > 0) {
1808 ssid->passphrase = os_strdup(params->passphrase);
1809 if (ssid->passphrase == NULL) {
1810 wpa_msg_global(wpa_s, MSG_ERROR,
1811 "P2P: Failed to copy passphrase for GO");
1812 wpa_config_remove_network(wpa_s->conf, ssid->id);
1816 ssid->passphrase = NULL;
1817 ssid->psk_set = params->psk_set;
1819 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1820 else if (ssid->passphrase)
1821 wpa_config_update_psk(ssid);
1822 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1824 wpa_s->ap_configured_cb = p2p_go_configured;
1825 wpa_s->ap_configured_cb_ctx = wpa_s;
1826 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1827 wpa_s->scan_req = NORMAL_SCAN_REQ;
1828 wpa_s->connect_without_scan = ssid;
1829 wpa_s->reassociate = 1;
1830 wpa_s->disconnected = 0;
1831 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1833 wpa_supplicant_req_scan(wpa_s, 0, 0);
1837 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1838 const struct wpa_supplicant *src)
1840 struct wpa_config *d;
1841 const struct wpa_config *s;
1846 #define C(n) if (s->n) d->n = os_strdup(s->n)
1855 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1856 os_memcpy(d->sec_device_type, s->sec_device_type,
1857 sizeof(d->sec_device_type));
1858 d->num_sec_device_types = s->num_sec_device_types;
1860 d->p2p_group_idle = s->p2p_group_idle;
1861 d->p2p_intra_bss = s->p2p_intra_bss;
1862 d->persistent_reconnect = s->persistent_reconnect;
1863 d->max_num_sta = s->max_num_sta;
1864 d->pbc_in_m1 = s->pbc_in_m1;
1865 d->ignore_old_scan_res = s->ignore_old_scan_res;
1866 d->beacon_int = s->beacon_int;
1867 d->dtim_period = s->dtim_period;
1868 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
1869 d->disassoc_low_ack = s->disassoc_low_ack;
1870 d->disable_scan_offload = s->disable_scan_offload;
1871 d->passive_scan = s->passive_scan;
1873 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1874 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1875 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1880 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1881 char *ifname, size_t len)
1883 char *ifname_ptr = wpa_s->ifname;
1885 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1886 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1887 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1890 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1891 if (os_strlen(ifname) >= IFNAMSIZ &&
1892 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1895 /* Try to avoid going over the IFNAMSIZ length limit */
1896 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1897 if (os_snprintf_error(len, res) && len)
1898 ifname[len - 1] = '\0';
1903 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1904 enum wpa_driver_if_type type)
1906 char ifname[120], force_ifname[120];
1908 if (wpa_s->pending_interface_name[0]) {
1909 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1910 "- skip creation of a new one");
1911 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1912 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1913 "unknown?! ifname='%s'",
1914 wpa_s->pending_interface_name);
1920 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1921 force_ifname[0] = '\0';
1923 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1925 wpa_s->p2p_group_idx++;
1927 wpa_s->pending_interface_type = type;
1928 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1929 wpa_s->pending_interface_addr, NULL) < 0) {
1930 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1935 if (force_ifname[0]) {
1936 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1938 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1939 sizeof(wpa_s->pending_interface_name));
1941 os_strlcpy(wpa_s->pending_interface_name, ifname,
1942 sizeof(wpa_s->pending_interface_name));
1943 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1944 MACSTR, wpa_s->pending_interface_name,
1945 MAC2STR(wpa_s->pending_interface_addr));
1951 static void wpas_p2p_remove_pending_group_interface(
1952 struct wpa_supplicant *wpa_s)
1954 if (!wpa_s->pending_interface_name[0] ||
1955 is_zero_ether_addr(wpa_s->pending_interface_addr))
1956 return; /* No pending virtual interface */
1958 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1959 wpa_s->pending_interface_name);
1960 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1961 wpa_s->pending_interface_name);
1962 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1963 wpa_s->pending_interface_name[0] = '\0';
1964 wpa_s->global->pending_group_iface_for_p2ps = 0;
1968 static struct wpa_supplicant *
1969 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1971 struct wpa_interface iface;
1972 struct wpa_supplicant *group_wpa_s;
1974 if (!wpa_s->pending_interface_name[0]) {
1975 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1976 if (!wpas_p2p_create_iface(wpa_s))
1979 * Something has forced us to remove the pending interface; try
1980 * to create a new one and hope for the best that we will get
1981 * the same local address.
1983 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1984 WPA_IF_P2P_CLIENT) < 0)
1988 os_memset(&iface, 0, sizeof(iface));
1989 iface.ifname = wpa_s->pending_interface_name;
1990 iface.driver = wpa_s->driver->name;
1991 if (wpa_s->conf->ctrl_interface == NULL &&
1992 wpa_s->parent != wpa_s &&
1994 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1995 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1997 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1998 iface.driver_param = wpa_s->conf->driver_param;
1999 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
2000 if (group_wpa_s == NULL) {
2001 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2002 "wpa_supplicant interface");
2005 wpa_s->pending_interface_name[0] = '\0';
2006 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2007 P2P_GROUP_INTERFACE_CLIENT;
2008 wpa_s->global->p2p_group_formation = group_wpa_s;
2009 wpa_s->global->pending_group_iface_for_p2ps = 0;
2011 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2017 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2020 struct wpa_supplicant *wpa_s = eloop_ctx;
2021 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2022 wpas_p2p_group_formation_failed(wpa_s);
2026 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
2028 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2029 wpa_s->parent, NULL);
2030 if (wpa_s->global->p2p)
2031 p2p_group_formation_failed(wpa_s->global->p2p);
2032 wpas_group_formation_completed(wpa_s, 0);
2036 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2038 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2039 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2040 wpa_s->parent, NULL);
2041 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2042 wpa_s->parent, NULL);
2043 wpa_s->global->p2p_fail_on_wps_complete = 0;
2047 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2049 if (wpa_s->global->p2p_group_formation != wpa_s)
2051 /* Speed up group formation timeout since this cannot succeed */
2052 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2053 wpa_s->parent, NULL);
2054 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2055 wpa_s->parent, NULL);
2059 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2061 struct wpa_supplicant *wpa_s = ctx;
2063 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2064 wpa_drv_cancel_remain_on_channel(wpa_s);
2065 wpa_s->off_channel_freq = 0;
2066 wpa_s->roc_waiting_drv_freq = 0;
2070 wpa_msg_global(wpa_s, MSG_INFO,
2071 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2073 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2074 wpas_p2p_remove_pending_group_interface(wpa_s);
2078 if (wpa_s->p2p_go_ht40)
2080 if (wpa_s->p2p_go_vht)
2083 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2084 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2086 res->role_go ? "GO" : "client", res->freq, res->ht40,
2087 MAC2STR(res->peer_device_addr),
2088 MAC2STR(res->peer_interface_addr),
2089 p2p_wps_method_text(res->wps_method));
2090 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2092 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2093 struct wpa_ssid *ssid;
2094 ssid = wpa_config_get_network(wpa_s->conf,
2095 wpa_s->p2p_persistent_id);
2096 if (ssid && ssid->disabled == 2 &&
2097 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2098 size_t len = os_strlen(ssid->passphrase);
2099 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2100 "on requested persistent group");
2101 os_memcpy(res->passphrase, ssid->passphrase, len);
2102 res->passphrase[len] = '\0';
2106 if (wpa_s->create_p2p_iface) {
2107 struct wpa_supplicant *group_wpa_s =
2108 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2109 if (group_wpa_s == NULL) {
2110 wpas_p2p_remove_pending_group_interface(wpa_s);
2111 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2113 wpas_p2p_group_formation_failed(wpa_s);
2116 if (group_wpa_s != wpa_s) {
2117 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2118 sizeof(group_wpa_s->p2p_pin));
2119 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2121 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2122 wpa_s->pending_interface_name[0] = '\0';
2123 group_wpa_s->p2p_in_provisioning = 1;
2126 wpas_start_wps_go(group_wpa_s, res, 1);
2128 wpas_start_wps_enrollee(group_wpa_s, res);
2130 wpa_s->p2p_in_provisioning = 1;
2131 wpa_s->global->p2p_group_formation = wpa_s;
2134 wpas_start_wps_go(wpa_s, res, 1);
2136 wpas_start_wps_enrollee(ctx, res);
2139 wpa_s->p2p_long_listen = 0;
2140 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2142 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2143 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2144 (res->peer_config_timeout % 100) * 10000,
2145 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2149 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
2151 struct wpa_supplicant *wpa_s = ctx;
2152 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2153 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
2155 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
2159 static void wpas_dev_found(void *ctx, const u8 *addr,
2160 const struct p2p_peer_info *info,
2163 #ifndef CONFIG_NO_STDOUT_DEBUG
2164 struct wpa_supplicant *wpa_s = ctx;
2165 char devtype[WPS_DEV_TYPE_BUFSIZE];
2166 char *wfd_dev_info_hex = NULL;
2168 #ifdef CONFIG_WIFI_DISPLAY
2169 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2170 WFD_SUBELEM_DEVICE_INFO);
2171 #endif /* CONFIG_WIFI_DISPLAY */
2173 if (info->p2ps_instance) {
2175 const u8 *buf = wpabuf_head(info->p2ps_instance);
2176 size_t len = wpabuf_len(info->p2ps_instance);
2183 if (len < 4 + 2 + 1)
2185 id = WPA_GET_LE32(buf);
2187 methods = WPA_GET_BE16(buf);
2190 if (str_len > len - 4 - 2 - 1)
2192 os_memcpy(str, buf, str_len);
2193 str[str_len] = '\0';
2195 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2197 wpa_msg_global(wpa_s, MSG_INFO,
2198 P2P_EVENT_DEVICE_FOUND MACSTR
2199 " p2p_dev_addr=" MACSTR
2200 " pri_dev_type=%s name='%s'"
2201 " config_methods=0x%x"
2204 " adv_id=%x asp_svc=%s%s",
2206 MAC2STR(info->p2p_device_addr),
2207 wps_dev_type_bin2str(
2209 devtype, sizeof(devtype)),
2210 info->device_name, methods,
2211 info->dev_capab, info->group_capab,
2213 info->vendor_elems ?
2214 " vendor_elems=1" : "");
2219 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2220 " p2p_dev_addr=" MACSTR
2221 " pri_dev_type=%s name='%s' config_methods=0x%x "
2222 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2223 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2224 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2226 info->device_name, info->config_methods,
2227 info->dev_capab, info->group_capab,
2228 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2229 wfd_dev_info_hex ? wfd_dev_info_hex : "",
2230 info->vendor_elems ? " vendor_elems=1" : "",
2234 os_free(wfd_dev_info_hex);
2235 #endif /* CONFIG_NO_STDOUT_DEBUG */
2237 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2241 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2243 struct wpa_supplicant *wpa_s = ctx;
2245 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2246 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2248 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2252 static void wpas_find_stopped(void *ctx)
2254 struct wpa_supplicant *wpa_s = ctx;
2255 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2256 wpas_notify_p2p_find_stopped(wpa_s);
2260 struct wpas_p2p_listen_work {
2262 unsigned int duration;
2263 struct wpabuf *probe_resp_ie;
2267 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2271 wpabuf_free(lwork->probe_resp_ie);
2276 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2278 struct wpas_p2p_listen_work *lwork;
2280 if (!wpa_s->p2p_listen_work)
2283 lwork = wpa_s->p2p_listen_work->ctx;
2284 wpas_p2p_listen_work_free(lwork);
2285 radio_work_done(wpa_s->p2p_listen_work);
2286 wpa_s->p2p_listen_work = NULL;
2290 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2292 struct wpa_supplicant *wpa_s = work->wpa_s;
2293 struct wpas_p2p_listen_work *lwork = work->ctx;
2294 unsigned int duration;
2297 if (work->started) {
2298 wpa_s->p2p_listen_work = NULL;
2299 wpas_stop_listen(wpa_s);
2301 wpas_p2p_listen_work_free(lwork);
2305 wpa_s->p2p_listen_work = work;
2307 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2309 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2310 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2311 "report received Probe Request frames");
2312 wpas_p2p_listen_work_done(wpa_s);
2316 wpa_s->pending_listen_freq = lwork->freq;
2317 wpa_s->pending_listen_duration = lwork->duration;
2319 duration = lwork->duration;
2320 #ifdef CONFIG_TESTING_OPTIONS
2321 if (wpa_s->extra_roc_dur) {
2322 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2323 duration, duration + wpa_s->extra_roc_dur);
2324 duration += wpa_s->extra_roc_dur;
2326 #endif /* CONFIG_TESTING_OPTIONS */
2328 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2329 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2330 "to remain on channel (%u MHz) for Listen "
2331 "state", lwork->freq);
2332 wpas_p2p_listen_work_done(wpa_s);
2333 wpa_s->pending_listen_freq = 0;
2336 wpa_s->off_channel_freq = 0;
2337 wpa_s->roc_waiting_drv_freq = lwork->freq;
2341 static int wpas_start_listen(void *ctx, unsigned int freq,
2342 unsigned int duration,
2343 const struct wpabuf *probe_resp_ie)
2345 struct wpa_supplicant *wpa_s = ctx;
2346 struct wpas_p2p_listen_work *lwork;
2348 if (wpa_s->p2p_listen_work) {
2349 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2353 lwork = os_zalloc(sizeof(*lwork));
2357 lwork->duration = duration;
2358 if (probe_resp_ie) {
2359 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2360 if (lwork->probe_resp_ie == NULL) {
2361 wpas_p2p_listen_work_free(lwork);
2366 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2368 wpas_p2p_listen_work_free(lwork);
2376 static void wpas_stop_listen(void *ctx)
2378 struct wpa_supplicant *wpa_s = ctx;
2379 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2380 wpa_drv_cancel_remain_on_channel(wpa_s);
2381 wpa_s->off_channel_freq = 0;
2382 wpa_s->roc_waiting_drv_freq = 0;
2384 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2385 wpa_drv_probe_req_report(wpa_s, 0);
2386 wpas_p2p_listen_work_done(wpa_s);
2390 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
2392 struct wpa_supplicant *wpa_s = ctx;
2393 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
2397 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2398 const u8 *peer, const char *params,
2399 unsigned int generated_pin)
2401 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2402 " %08d%s", MAC2STR(peer), generated_pin, params);
2406 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2407 const u8 *peer, const char *params)
2409 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2410 "%s", MAC2STR(peer), params);
2414 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2415 const u8 *dev_addr, const u8 *pri_dev_type,
2416 const char *dev_name, u16 supp_config_methods,
2417 u8 dev_capab, u8 group_capab, const u8 *group_id,
2418 size_t group_id_len)
2420 struct wpa_supplicant *wpa_s = ctx;
2421 char devtype[WPS_DEV_TYPE_BUFSIZE];
2423 u8 empty_dev_type[8];
2424 unsigned int generated_pin = 0;
2425 struct wpa_supplicant *group = NULL;
2429 for (group = wpa_s->global->ifaces; group; group = group->next)
2431 struct wpa_ssid *s = group->current_ssid;
2433 s->mode == WPAS_MODE_P2P_GO &&
2434 group_id_len - ETH_ALEN == s->ssid_len &&
2435 os_memcmp(group_id + ETH_ALEN, s->ssid,
2441 if (pri_dev_type == NULL) {
2442 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2443 pri_dev_type = empty_dev_type;
2445 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2446 " pri_dev_type=%s name='%s' config_methods=0x%x "
2447 "dev_capab=0x%x group_capab=0x%x%s%s",
2449 wps_dev_type_bin2str(pri_dev_type, devtype,
2451 dev_name, supp_config_methods, dev_capab, group_capab,
2452 group ? " group=" : "",
2453 group ? group->ifname : "");
2454 if (os_snprintf_error(sizeof(params), res))
2455 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
2456 params[sizeof(params) - 1] = '\0';
2458 if (config_methods & WPS_CONFIG_DISPLAY) {
2459 generated_pin = wps_generate_pin();
2460 wpas_prov_disc_local_display(wpa_s, peer, params,
2462 } else if (config_methods & WPS_CONFIG_KEYPAD)
2463 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2464 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2465 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2466 MACSTR "%s", MAC2STR(peer), params);
2468 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2469 P2P_PROV_DISC_SUCCESS,
2470 config_methods, generated_pin);
2474 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2476 struct wpa_supplicant *wpa_s = ctx;
2477 unsigned int generated_pin = 0;
2480 if (wpa_s->pending_pd_before_join &&
2481 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2482 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2483 wpa_s->pending_pd_before_join = 0;
2484 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2485 "join-existing-group operation");
2486 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2490 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2491 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2494 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2495 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2496 if (os_snprintf_error(sizeof(params), res))
2497 params[sizeof(params) - 1] = '\0';
2501 if (config_methods & WPS_CONFIG_DISPLAY)
2502 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2503 else if (config_methods & WPS_CONFIG_KEYPAD) {
2504 generated_pin = wps_generate_pin();
2505 wpas_prov_disc_local_display(wpa_s, peer, params,
2507 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2508 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2509 MACSTR "%s", MAC2STR(peer), params);
2511 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2512 P2P_PROV_DISC_SUCCESS,
2513 config_methods, generated_pin);
2517 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2518 enum p2p_prov_disc_status status,
2519 u32 adv_id, const u8 *adv_mac,
2520 const char *deferred_session_resp)
2522 struct wpa_supplicant *wpa_s = ctx;
2524 if (wpa_s->p2p_fallback_to_go_neg) {
2525 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2526 "failed - fall back to GO Negotiation");
2527 wpa_msg_global(wpa_s->parent, MSG_INFO,
2528 P2P_EVENT_FALLBACK_TO_GO_NEG
2529 "reason=PD-failed");
2530 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2534 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2535 wpa_s->pending_pd_before_join = 0;
2536 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2537 "join-existing-group operation (no ACK for PD "
2539 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2543 if (adv_id && adv_mac && deferred_session_resp) {
2544 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2545 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2546 " deferred_session_resp='%s'",
2547 MAC2STR(peer), status, adv_id,
2548 deferred_session_resp);
2549 } else if (adv_id && adv_mac) {
2550 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2551 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2552 MAC2STR(peer), status, adv_id);
2554 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2555 " p2p_dev_addr=" MACSTR " status=%d",
2556 MAC2STR(peer), status);
2559 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2564 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2566 if (channels == NULL)
2567 return 1; /* Assume no restrictions */
2568 return p2p_channels_includes_freq(channels, freq);
2574 * Pick the best frequency to use from all the currently used frequencies.
2576 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2577 struct wpa_used_freq_data *freqs,
2582 /* find a candidate freq that is supported by P2P */
2583 for (c = 0; c < num; c++)
2584 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2590 /* once we have a candidate, try to find a 'better' one */
2591 for (i = c + 1; i < num; i++) {
2592 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2596 * 1. Infrastructure station interfaces have higher preference.
2597 * 2. P2P Clients have higher preference.
2600 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2605 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2608 return freqs[c].freq;
2612 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2613 const u8 *go_dev_addr, const u8 *ssid,
2614 size_t ssid_len, int *go, u8 *group_bssid,
2615 int *force_freq, int persistent_group,
2616 const struct p2p_channels *channels,
2619 struct wpa_supplicant *wpa_s = ctx;
2621 struct wpa_used_freq_data *freqs;
2622 struct wpa_supplicant *grp;
2625 if (!persistent_group) {
2626 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2627 " to join an active group (SSID: %s)",
2628 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2629 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2630 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2632 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2633 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2634 "authorized invitation");
2638 #ifdef CONFIG_WPS_NFC
2639 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2640 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
2641 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2642 wpa_s->p2p_wps_method = WPS_NFC;
2643 wpa_s->pending_join_wps_method = WPS_NFC;
2644 os_memcpy(wpa_s->pending_join_dev_addr,
2645 go_dev_addr, ETH_ALEN);
2646 os_memcpy(wpa_s->pending_join_iface_addr,
2650 #endif /* CONFIG_WPS_NFC */
2653 * Do not accept the invitation automatically; notify user and
2656 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2659 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2661 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2662 "running persistent group");
2664 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2668 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2669 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2670 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2671 "invitation to re-invoke a persistent group");
2672 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2673 } else if (!wpa_s->conf->persistent_reconnect)
2674 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2676 for (s = wpa_s->conf->ssid; s; s = s->next) {
2677 if (s->disabled == 2 &&
2678 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2679 s->ssid_len == ssid_len &&
2680 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2685 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2686 " requested reinvocation of an unknown group",
2688 return P2P_SC_FAIL_UNKNOWN_GROUP;
2691 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2693 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2694 wpa_printf(MSG_DEBUG, "P2P: The only available "
2695 "interface is already in use - reject "
2697 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2699 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2700 } else if (s->mode == WPAS_MODE_P2P_GO) {
2702 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2704 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2705 "interface address for the group");
2706 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2708 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2713 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2716 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2717 sizeof(struct wpa_used_freq_data));
2719 int num_channels = wpa_s->num_multichan_concurrent;
2720 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2721 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2725 /* Get one of the frequencies currently in use */
2726 if (best_freq > 0) {
2727 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2728 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
2730 if (wpa_s->num_multichan_concurrent < 2 ||
2731 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2732 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2733 *force_freq = best_freq;
2737 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2738 wpas_p2p_num_unused_channels(wpa_s) > 0) {
2740 /* We are the client */
2741 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2742 "running a GO but we are capable of MCC, "
2743 "figure out the best channel to use");
2745 } else if (!freq_included(channels, *force_freq)) {
2746 /* We are the GO, and *force_freq is not in the
2748 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2749 "in intersection but we are capable of MCC, "
2750 "figure out the best channel to use",
2756 return P2P_SC_SUCCESS;
2760 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2761 const u8 *ssid, size_t ssid_len,
2762 const u8 *go_dev_addr, u8 status,
2765 struct wpa_supplicant *wpa_s = ctx;
2768 for (s = wpa_s->conf->ssid; s; s = s->next) {
2769 if (s->disabled == 2 &&
2770 s->ssid_len == ssid_len &&
2771 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2775 if (status == P2P_SC_SUCCESS) {
2776 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2777 " was accepted; op_freq=%d MHz, SSID=%s",
2778 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
2780 int go = s->mode == WPAS_MODE_P2P_GO;
2781 wpas_p2p_group_add_persistent(
2782 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
2783 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
2785 wpa_s->user_initiated_pd = 0;
2786 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2787 wpa_s->p2p_wps_method, 0, op_freq,
2793 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2794 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2795 " was rejected (status %u)", MAC2STR(sa), status);
2801 wpa_msg_global(wpa_s, MSG_INFO,
2802 P2P_EVENT_INVITATION_RECEIVED
2803 "sa=" MACSTR " go_dev_addr=" MACSTR
2804 " bssid=" MACSTR " unknown-network",
2805 MAC2STR(sa), MAC2STR(go_dev_addr),
2808 wpa_msg_global(wpa_s, MSG_INFO,
2809 P2P_EVENT_INVITATION_RECEIVED
2810 "sa=" MACSTR " go_dev_addr=" MACSTR
2812 MAC2STR(sa), MAC2STR(go_dev_addr));
2817 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2818 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2819 "sa=" MACSTR " persistent=%d freq=%d",
2820 MAC2STR(sa), s->id, op_freq);
2822 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2823 "sa=" MACSTR " persistent=%d",
2824 MAC2STR(sa), s->id);
2829 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2830 struct wpa_ssid *ssid,
2831 const u8 *peer, int inv)
2834 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
2839 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2840 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
2844 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
2845 if (ssid->mode != WPAS_MODE_P2P_GO &&
2846 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2847 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2848 "due to invitation result", ssid->id);
2849 wpas_notify_network_removed(wpa_s, ssid);
2850 wpa_config_remove_network(wpa_s->conf, ssid->id);
2853 return; /* Peer not found in client list */
2856 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2857 "group %d client list%s",
2858 MAC2STR(peer), ssid->id,
2859 inv ? " due to invitation result" : "");
2860 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
2861 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
2862 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
2863 ssid->num_p2p_clients--;
2864 if (p2p_wpa_s->conf->update_config &&
2865 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
2866 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2870 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2873 struct wpa_ssid *ssid;
2875 wpa_s = wpa_s->global->p2p_invite_group;
2877 return; /* No known invitation group */
2878 ssid = wpa_s->current_ssid;
2879 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2880 !ssid->p2p_persistent_group)
2881 return; /* Not operating as a GO in persistent group */
2882 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2883 ssid->ssid, ssid->ssid_len);
2884 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2888 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2889 const struct p2p_channels *channels,
2890 const u8 *peer, int neg_freq,
2893 struct wpa_supplicant *wpa_s = ctx;
2894 struct wpa_ssid *ssid;
2898 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2899 "status=%d " MACSTR,
2900 status, MAC2STR(bssid));
2902 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2903 "status=%d ", status);
2905 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2907 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2908 status, MAC2STR(peer));
2909 if (wpa_s->pending_invite_ssid_id == -1) {
2910 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2911 wpas_remove_persistent_client(wpa_s, peer);
2912 return; /* Invitation to active group */
2915 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2916 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2917 "invitation exchange to indicate readiness for "
2921 if (status != P2P_SC_SUCCESS) {
2922 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2923 ssid = wpa_config_get_network(
2924 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2925 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2927 wpas_p2p_remove_pending_group_interface(wpa_s);
2931 ssid = wpa_config_get_network(wpa_s->conf,
2932 wpa_s->pending_invite_ssid_id);
2934 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2935 "data matching with invitation");
2940 * The peer could have missed our ctrl::ack frame for Invitation
2941 * Response and continue retransmitting the frame. To reduce the
2942 * likelihood of the peer not getting successful TX status for the
2943 * Invitation Response frame, wait a short time here before starting
2944 * the persistent group so that we will remain on the current channel to
2945 * acknowledge any possible retransmission from the peer.
2947 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2948 "starting persistent group");
2951 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2952 freq_included(channels, neg_freq))
2954 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
2955 freq_included(channels, peer_oper_freq))
2956 freq = peer_oper_freq;
2960 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
2961 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2962 wpas_p2p_group_add_persistent(wpa_s, ssid,
2963 ssid->mode == WPAS_MODE_P2P_GO,
2964 wpa_s->p2p_persistent_go_freq,
2966 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2968 ssid->mode == WPAS_MODE_P2P_GO ?
2969 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2974 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2977 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
2979 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
2983 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2985 reg->channel[reg->channels] = chan;
2990 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2991 struct p2p_channels *chan,
2992 struct p2p_channels *cli_chan)
2996 wpa_s->global->p2p_24ghz_social_channels = 1;
2998 os_memset(cli_chan, 0, sizeof(*cli_chan));
3000 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3003 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3004 chan->reg_class[cla].reg_class = 81;
3005 chan->reg_class[cla].channels = 0;
3006 for (i = 0; i < 11; i++) {
3007 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3008 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3010 if (chan->reg_class[cla].channels)
3013 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3016 /* Operating class 115 - 5 GHz, channels 36-48 */
3017 chan->reg_class[cla].reg_class = 115;
3018 chan->reg_class[cla].channels = 0;
3019 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3020 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3021 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3022 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3023 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3024 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3025 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3026 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3027 if (chan->reg_class[cla].channels)
3030 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3033 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3034 chan->reg_class[cla].reg_class = 124;
3035 chan->reg_class[cla].channels = 0;
3036 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3037 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3038 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3039 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3040 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3041 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3042 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3043 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3044 if (chan->reg_class[cla].channels)
3047 chan->reg_classes = cla;
3052 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3054 enum hostapd_hw_mode mode)
3058 for (i = 0; i < num_modes; i++) {
3059 if (modes[i].mode == mode)
3068 NOT_ALLOWED, NO_IR, ALLOWED
3071 static int has_channel(struct wpa_global *global,
3072 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3077 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3079 if (wpas_p2p_disallowed_freq(global, freq))
3082 for (i = 0; i < mode->num_channels; i++) {
3083 if (mode->channels[i].chan == chan) {
3085 *flags = mode->channels[i].flag;
3086 if (mode->channels[i].flag &
3087 (HOSTAPD_CHAN_DISABLED |
3088 HOSTAPD_CHAN_RADAR))
3090 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3100 struct p2p_oper_class_map {
3101 enum hostapd_hw_mode mode;
3106 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3109 static const struct p2p_oper_class_map op_class[] = {
3110 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3111 #if 0 /* Do not enable HT40 on 2 GHz for now */
3112 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3113 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3115 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3116 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3117 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3118 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3119 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3120 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3123 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3124 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3125 * 80 MHz, but currently use the following definition for simplicity
3126 * (these center frequencies are not actual channels, which makes
3127 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3128 * removing invalid channels.
3130 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3131 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3132 { -1, 0, 0, 0, 0, BW20 }
3136 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3137 struct hostapd_hw_modes *mode,
3140 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3143 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3146 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3148 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3149 * so the center channel is 6 channels away from the start/end.
3151 if (channel >= center_channels[i] - 6 &&
3152 channel <= center_channels[i] + 6)
3153 return center_channels[i];
3159 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3160 struct hostapd_hw_modes *mode,
3165 enum chan_allowed res, ret = ALLOWED;
3167 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3170 if (center_chan >= 58 && center_chan <= 138)
3171 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3173 /* check all the channels are available */
3174 for (i = 0; i < 4; i++) {
3175 int adj_chan = center_chan - 6 + i * 4;
3177 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3178 if (res == NOT_ALLOWED)
3183 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3185 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3187 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3189 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3197 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3198 struct hostapd_hw_modes *mode,
3202 enum chan_allowed res, res2;
3204 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3205 if (bw == BW40MINUS) {
3206 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3208 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3209 } else if (bw == BW40PLUS) {
3210 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3212 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3213 } else if (bw == BW80) {
3214 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3217 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3219 if (res == NO_IR || res2 == NO_IR)
3225 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3226 struct p2p_channels *chan,
3227 struct p2p_channels *cli_chan)
3229 struct hostapd_hw_modes *mode;
3230 int cla, op, cli_cla;
3232 if (wpa_s->hw.modes == NULL) {
3233 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3234 "of all supported channels; assume dualband "
3236 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3241 for (op = 0; op_class[op].op_class; op++) {
3242 const struct p2p_oper_class_map *o = &op_class[op];
3244 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3246 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3249 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3250 wpa_s->global->p2p_24ghz_social_channels = 1;
3251 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3252 enum chan_allowed res;
3253 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3254 if (res == ALLOWED) {
3256 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3258 reg = &chan->reg_class[cla];
3260 reg->reg_class = o->op_class;
3262 reg->channel[reg->channels] = ch;
3264 } else if (res == NO_IR &&
3265 wpa_s->conf->p2p_add_cli_chan) {
3266 if (cli_reg == NULL) {
3267 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3269 cli_reg = &cli_chan->reg_class[cli_cla];
3271 cli_reg->reg_class = o->op_class;
3273 cli_reg->channel[cli_reg->channels] = ch;
3274 cli_reg->channels++;
3278 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3279 reg->channel, reg->channels);
3282 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3283 cli_reg->channel, cli_reg->channels);
3287 chan->reg_classes = cla;
3288 cli_chan->reg_classes = cli_cla;
3294 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3295 struct hostapd_hw_modes *mode, u8 channel)
3298 enum chan_allowed ret;
3300 for (op = 0; op_class[op].op_class; op++) {
3301 const struct p2p_oper_class_map *o = &op_class[op];
3304 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3305 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3306 o->bw == BW20 || ch != channel)
3308 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3310 return (o->bw == BW40MINUS) ? -1 : 1;
3317 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3318 struct hostapd_hw_modes *mode, u8 channel)
3320 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3323 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3327 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3330 struct wpa_supplicant *wpa_s = ctx;
3332 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3333 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3339 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3343 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3344 const u8 *ssid, size_t ssid_len)
3346 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3347 struct wpa_ssid *s = wpa_s->current_ssid;
3350 if (s->mode != WPAS_MODE_P2P_GO &&
3351 s->mode != WPAS_MODE_AP &&
3352 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3354 if (s->ssid_len != ssid_len ||
3355 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3365 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3366 const u8 *peer_dev_addr)
3368 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3369 struct wpa_ssid *ssid = wpa_s->current_ssid;
3372 if (ssid->mode != WPAS_MODE_INFRA)
3374 if (wpa_s->wpa_state != WPA_COMPLETED &&
3375 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3377 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3385 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3387 struct wpa_supplicant *wpa_s = ctx;
3389 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3393 static int wpas_is_concurrent_session_active(void *ctx)
3395 struct wpa_supplicant *wpa_s = ctx;
3396 struct wpa_supplicant *ifs;
3398 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3401 if (ifs->wpa_state > WPA_ASSOCIATED)
3408 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3410 struct wpa_supplicant *wpa_s = ctx;
3411 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3415 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3416 const char *conf_p2p_dev)
3418 struct wpa_interface iface;
3419 struct wpa_supplicant *p2pdev_wpa_s;
3421 char force_name[100];
3424 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3426 if (os_snprintf_error(sizeof(ifname), ret))
3428 force_name[0] = '\0';
3429 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3430 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3431 force_name, wpa_s->pending_interface_addr, NULL);
3433 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3436 os_strlcpy(wpa_s->pending_interface_name, ifname,
3437 sizeof(wpa_s->pending_interface_name));
3439 os_memset(&iface, 0, sizeof(iface));
3441 iface.ifname = wpa_s->pending_interface_name;
3442 iface.driver = wpa_s->driver->name;
3443 iface.driver_param = wpa_s->conf->driver_param;
3446 * If a P2P Device configuration file was given, use it as the interface
3447 * configuration file (instead of using parent's configuration file.
3450 iface.confname = conf_p2p_dev;
3451 iface.ctrl_interface = NULL;
3453 iface.confname = wpa_s->confname;
3454 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3456 iface.conf_p2p_dev = NULL;
3458 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
3459 if (!p2pdev_wpa_s) {
3460 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3463 wpa_s->p2p_dev = p2pdev_wpa_s;
3465 wpa_s->pending_interface_name[0] = '\0';
3470 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3471 const u8 *noa, size_t noa_len)
3473 struct wpa_supplicant *wpa_s, *intf = ctx;
3476 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3477 if (wpa_s->waiting_presence_resp)
3481 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3484 wpa_s->waiting_presence_resp = 0;
3486 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3487 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3488 " status=%u noa=%s", MAC2STR(src), status, hex);
3492 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3493 size_t ssid_len, u8 *go_dev_addr,
3494 u8 *ret_ssid, size_t *ret_ssid_len)
3496 struct wpa_supplicant *wpa_s = ctx;
3499 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3501 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3502 *ret_ssid_len = s->ssid_len;
3503 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3511 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3512 u8 *ssid, size_t *ssid_len, int *group_iface)
3514 struct wpa_supplicant *wpa_s = ctx;
3518 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
3520 s = wpas_p2p_get_persistent_go(wpa_s);
3522 os_memcpy(bssid, s->bssid, ETH_ALEN);
3525 *group_iface = wpas_p2p_create_iface(wpa_s);
3529 os_memcpy(intended_addr, bssid, ETH_ALEN);
3530 os_memcpy(ssid, s->ssid, s->ssid_len);
3531 *ssid_len = s->ssid_len;
3537 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3538 const u8 *ssid, size_t ssid_len)
3540 struct wpa_supplicant *wpa_s = ctx;
3542 int save_config = 0;
3545 /* Start with our first choice of Persistent Groups */
3546 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3547 if (go && ssid && ssid_len &&
3548 s->ssid_len == ssid_len &&
3549 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3550 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3553 /* Remove stale persistent group */
3554 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3555 wpa_config_remove_network(wpa_s->conf, s->id);
3560 for (i = 0; i < s->num_p2p_clients; i++) {
3561 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3562 peer, ETH_ALEN) != 0)
3565 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3566 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3567 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3570 s->num_p2p_clients--;
3575 p2p_config_write(wpa_s);
3577 /* Return TRUE if valid SSID remains */
3582 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3583 const u8 *adv_mac, const u8 *ses_mac,
3584 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3585 u8 conncap, int passwd_id,
3586 const u8 *persist_ssid,
3587 size_t persist_ssid_size, int response_done,
3588 int prov_start, const char *session_info)
3590 struct wpa_supplicant *wpa_s = ctx;
3592 struct wpa_ssid *persistent_go, *stale, *s;
3593 int save_config = 0;
3594 struct wpa_supplicant *go_wpa_s;
3599 os_memset(mac, 0, ETH_ALEN);
3608 if (session_info == NULL) {
3609 wpa_msg_global(wpa_s, MSG_INFO,
3610 P2P_EVENT_P2PS_PROVISION_START MACSTR
3611 " adv_id=%x conncap=%x"
3613 " session=%x mac=" MACSTR
3614 " dev_passwd_id=%d",
3615 MAC2STR(dev), adv_id, conncap,
3617 ses_id, MAC2STR(ses_mac),
3620 wpa_msg_global(wpa_s, MSG_INFO,
3621 P2P_EVENT_P2PS_PROVISION_START MACSTR
3622 " adv_id=%x conncap=%x"
3624 " session=%x mac=" MACSTR
3625 " dev_passwd_id=%d info='%s'",
3626 MAC2STR(dev), adv_id, conncap,
3628 ses_id, MAC2STR(ses_mac),
3629 passwd_id, session_info);
3634 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3635 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3637 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3638 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3639 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3641 if (persistent_go && !persistent_go->num_p2p_clients) {
3642 /* remove empty persistent GO */
3643 wpa_config_remove_network(wpa_s->conf,
3647 wpa_msg_global(wpa_s, MSG_INFO,
3648 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3650 " adv_id=%x adv_mac=" MACSTR
3651 " session=%x mac=" MACSTR,
3652 MAC2STR(dev), status,
3653 adv_id, MAC2STR(adv_mac),
3654 ses_id, MAC2STR(ses_mac));
3658 /* Clean up stale persistent groups with this device */
3659 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3662 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3666 if (s && s->ssid_len == stale->ssid_len &&
3667 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3668 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3671 /* Remove stale persistent group */
3672 if (stale->mode != WPAS_MODE_P2P_GO ||
3673 stale->num_p2p_clients <= 1) {
3674 wpa_config_remove_network(wpa_s->conf, stale->id);
3678 for (i = 0; i < stale->num_p2p_clients; i++) {
3679 if (os_memcmp(stale->p2p_client_list +
3681 dev, ETH_ALEN) == 0) {
3682 os_memmove(stale->p2p_client_list +
3684 stale->p2p_client_list +
3686 (stale->num_p2p_clients -
3691 stale->num_p2p_clients--;
3697 p2p_config_write(wpa_s);
3700 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3701 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3703 if (persistent_go && s != persistent_go &&
3704 !persistent_go->num_p2p_clients) {
3705 /* remove empty persistent GO */
3706 wpa_config_remove_network(wpa_s->conf,
3711 wpa_msg_global(wpa_s, MSG_INFO,
3712 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3714 " adv_id=%x adv_mac=" MACSTR
3715 " session=%x mac=" MACSTR
3717 MAC2STR(dev), status,
3718 adv_id, MAC2STR(adv_mac),
3719 ses_id, MAC2STR(ses_mac), s->id);
3723 if (conncap == P2PS_SETUP_GROUP_OWNER) {
3724 const char *go_ifname = NULL;
3726 wpa_s->global->pending_p2ps_group = 1;
3728 if (wpa_s->conf->p2p_no_group_iface)
3729 go_ifname = wpa_s->ifname;
3730 else if (wpa_s->pending_interface_name[0])
3731 go_ifname = wpa_s->pending_interface_name;
3734 wpas_p2ps_prov_complete(
3735 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3736 dev, adv_mac, ses_mac,
3737 NULL, adv_id, ses_id, 0, 0,
3738 NULL, 0, 0, 0, NULL);
3742 /* If PD Resp complete, start up the GO */
3743 if (response_done && persistent_go) {
3744 wpas_p2p_group_add_persistent(
3745 wpa_s, persistent_go,
3746 0, 0, 0, 0, 0, NULL,
3747 persistent_go->mode ==
3749 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3751 } else if (response_done) {
3752 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3755 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3756 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3757 wpa_s->p2ps_join_addr_valid = 1;
3758 wpa_dbg(wpa_s, MSG_DEBUG,
3759 "P2PS: Saving PIN for " MACSTR,
3762 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3763 go_ifname = go_wpa_s->ifname;
3765 wpa_dbg(go_wpa_s, MSG_DEBUG,
3766 "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
3767 wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
3770 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3771 wpa_s->p2ps_join_addr_valid = 1;
3772 wpa_dbg(wpa_s, MSG_DEBUG,
3773 "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
3776 wpa_msg_global(wpa_s, MSG_INFO,
3777 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3778 " status=%d conncap=%x"
3779 " adv_id=%x adv_mac=" MACSTR
3780 " session=%x mac=" MACSTR
3781 " dev_passwd_id=%d go=%s",
3782 MAC2STR(dev), status, conncap,
3783 adv_id, MAC2STR(adv_mac),
3784 ses_id, MAC2STR(ses_mac),
3785 passwd_id, go_ifname);
3789 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3790 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3792 if (persistent_go && !persistent_go->num_p2p_clients) {
3793 /* remove empty persistent GO */
3794 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
3797 if (conncap == P2PS_SETUP_CLIENT) {
3798 wpa_msg_global(wpa_s, MSG_INFO,
3799 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3800 " status=%d conncap=%x"
3801 " adv_id=%x adv_mac=" MACSTR
3802 " session=%x mac=" MACSTR
3803 " dev_passwd_id=%d join=" MACSTR,
3804 MAC2STR(dev), status, conncap,
3805 adv_id, MAC2STR(adv_mac),
3806 ses_id, MAC2STR(ses_mac),
3807 passwd_id, MAC2STR(grp_mac));
3809 wpa_msg_global(wpa_s, MSG_INFO,
3810 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3811 " status=%d conncap=%x"
3812 " adv_id=%x adv_mac=" MACSTR
3813 " session=%x mac=" MACSTR
3814 " dev_passwd_id=%d",
3815 MAC2STR(dev), status, conncap,
3816 adv_id, MAC2STR(adv_mac),
3817 ses_id, MAC2STR(ses_mac),
3823 static int _wpas_p2p_in_progress(void *ctx)
3825 struct wpa_supplicant *wpa_s = ctx;
3826 return wpas_p2p_in_progress(wpa_s);
3830 static int wpas_prov_disc_resp_cb(void *ctx)
3832 struct wpa_supplicant *wpa_s = ctx;
3833 struct wpa_ssid *persistent_go;
3835 if (!wpa_s->global->pending_p2ps_group)
3838 wpa_s->global->pending_p2ps_group = 0;
3840 if (wpas_p2p_get_go_group(wpa_s))
3842 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3844 if (persistent_go) {
3845 wpas_p2p_group_add_persistent(
3846 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
3847 persistent_go->mode == WPAS_MODE_P2P_GO ?
3848 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3850 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3858 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3859 * @global: Pointer to global data from wpa_supplicant_init()
3860 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3861 * Returns: 0 on success, -1 on failure
3863 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3865 struct p2p_config p2p;
3868 if (wpa_s->conf->p2p_disabled)
3871 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3877 os_memset(&p2p, 0, sizeof(p2p));
3879 p2p.debug_print = wpas_p2p_debug_print;
3880 p2p.p2p_scan = wpas_p2p_scan;
3881 p2p.send_action = wpas_send_action;
3882 p2p.send_action_done = wpas_send_action_done;
3883 p2p.go_neg_completed = wpas_go_neg_completed;
3884 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3885 p2p.dev_found = wpas_dev_found;
3886 p2p.dev_lost = wpas_dev_lost;
3887 p2p.find_stopped = wpas_find_stopped;
3888 p2p.start_listen = wpas_start_listen;
3889 p2p.stop_listen = wpas_stop_listen;
3890 p2p.send_probe_resp = wpas_send_probe_resp;
3891 p2p.sd_request = wpas_sd_request;
3892 p2p.sd_response = wpas_sd_response;
3893 p2p.prov_disc_req = wpas_prov_disc_req;
3894 p2p.prov_disc_resp = wpas_prov_disc_resp;
3895 p2p.prov_disc_fail = wpas_prov_disc_fail;
3896 p2p.invitation_process = wpas_invitation_process;
3897 p2p.invitation_received = wpas_invitation_received;
3898 p2p.invitation_result = wpas_invitation_result;
3899 p2p.get_noa = wpas_get_noa;
3900 p2p.go_connected = wpas_go_connected;
3901 p2p.presence_resp = wpas_presence_resp;
3902 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
3903 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
3904 p2p.get_persistent_group = wpas_get_persistent_group;
3905 p2p.get_go_info = wpas_get_go_info;
3906 p2p.remove_stale_groups = wpas_remove_stale_groups;
3907 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
3908 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
3909 p2p.p2ps_group_capability = p2ps_group_capability;
3911 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3912 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3913 p2p.dev_name = wpa_s->conf->device_name;
3914 p2p.manufacturer = wpa_s->conf->manufacturer;
3915 p2p.model_name = wpa_s->conf->model_name;
3916 p2p.model_number = wpa_s->conf->model_number;
3917 p2p.serial_number = wpa_s->conf->serial_number;
3919 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3920 p2p.config_methods = wpa_s->wps->config_methods;
3923 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3924 wpa_printf(MSG_ERROR,
3925 "P2P: Failed to configure supported channel list");
3929 if (wpa_s->conf->p2p_listen_reg_class &&
3930 wpa_s->conf->p2p_listen_channel) {
3931 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3932 p2p.channel = wpa_s->conf->p2p_listen_channel;
3933 p2p.channel_forced = 1;
3936 * Pick one of the social channels randomly as the listen
3939 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
3940 &p2p.channel) != 0) {
3941 wpa_printf(MSG_INFO,
3942 "P2P: No social channels supported by the driver - do not enable P2P");
3945 p2p.channel_forced = 0;
3947 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
3948 p2p.reg_class, p2p.channel);
3950 if (wpa_s->conf->p2p_oper_reg_class &&
3951 wpa_s->conf->p2p_oper_channel) {
3952 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3953 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3954 p2p.cfg_op_channel = 1;
3955 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3956 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3960 * Use random operation channel from 2.4 GHz band social
3961 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
3962 * other preference is indicated.
3964 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
3965 &p2p.op_channel) != 0) {
3966 wpa_printf(MSG_ERROR,
3967 "P2P: Failed to select random social channel as operation channel");
3970 p2p.cfg_op_channel = 0;
3971 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3972 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3975 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3976 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3977 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3980 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3981 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3982 p2p.country[2] = 0x04;
3984 os_memcpy(p2p.country, "XX\x04", 3);
3986 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3989 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3990 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3991 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3993 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3994 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3996 p2p.max_peers = 100;
3998 if (wpa_s->conf->p2p_ssid_postfix) {
3999 p2p.ssid_postfix_len =
4000 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4001 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4002 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4003 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4004 p2p.ssid_postfix_len);
4007 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4009 p2p.max_listen = wpa_s->max_remain_on_chan;
4011 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4012 wpa_s->conf->p2p_passphrase_len <= 63)
4013 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4015 p2p.passphrase_len = 8;
4017 global->p2p = p2p_init(&p2p);
4018 if (global->p2p == NULL)
4020 global->p2p_init_wpa_s = wpa_s;
4022 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4023 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4025 p2p_add_wps_vendor_extension(
4026 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4029 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4036 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4037 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4039 * This function deinitialize per-interface P2P data.
4041 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4043 if (wpa_s->driver && wpa_s->drv_priv)
4044 wpa_drv_probe_req_report(wpa_s, 0);
4046 if (wpa_s->go_params) {
4047 /* Clear any stored provisioning info */
4048 p2p_clear_provisioning_info(
4050 wpa_s->go_params->peer_device_addr);
4053 os_free(wpa_s->go_params);
4054 wpa_s->go_params = NULL;
4055 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4056 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4057 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4058 wpa_s->p2p_long_listen = 0;
4059 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4060 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4061 wpas_p2p_remove_pending_group_interface(wpa_s);
4062 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4063 wpas_p2p_listen_work_done(wpa_s);
4064 if (wpa_s->p2p_send_action_work) {
4065 os_free(wpa_s->p2p_send_action_work->ctx);
4066 radio_work_done(wpa_s->p2p_send_action_work);
4067 wpa_s->p2p_send_action_work = NULL;
4069 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4071 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4072 wpa_s->p2p_oob_dev_pw = NULL;
4074 os_free(wpa_s->p2p_group_common_freqs);
4075 wpa_s->p2p_group_common_freqs = NULL;
4076 wpa_s->p2p_group_common_freqs_num = 0;
4078 /* TODO: remove group interface from the driver if this wpa_s instance
4079 * is on top of a P2P group interface */
4084 * wpas_p2p_deinit_global - Deinitialize global P2P module
4085 * @global: Pointer to global data from wpa_supplicant_init()
4087 * This function deinitializes the global (per device) P2P module.
4089 static void wpas_p2p_deinit_global(struct wpa_global *global)
4091 struct wpa_supplicant *wpa_s, *tmp;
4093 wpa_s = global->ifaces;
4095 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4097 /* Remove remaining P2P group interfaces */
4098 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4099 wpa_s = wpa_s->next;
4101 tmp = global->ifaces;
4104 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4109 /* Disconnect from the P2P group and deinit the interface */
4110 wpas_p2p_disconnect(tmp);
4114 * Deinit GO data on any possibly remaining interface (if main
4115 * interface is used as GO).
4117 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4118 if (wpa_s->ap_iface)
4119 wpas_p2p_group_deinit(wpa_s);
4122 p2p_deinit(global->p2p);
4124 global->p2p_init_wpa_s = NULL;
4128 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4130 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4131 wpa_s->conf->p2p_no_group_iface)
4132 return 0; /* separate interface disabled per configuration */
4133 if (wpa_s->drv_flags &
4134 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4135 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4136 return 1; /* P2P group requires a new interface in every case
4138 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4139 return 0; /* driver does not support concurrent operations */
4140 if (wpa_s->global->ifaces->next)
4141 return 1; /* more that one interface already in use */
4142 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4143 return 1; /* this interface is already in use */
4148 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4149 const u8 *peer_addr,
4150 enum p2p_wps_method wps_method,
4151 int go_intent, const u8 *own_interface_addr,
4152 unsigned int force_freq, int persistent_group,
4153 struct wpa_ssid *ssid, unsigned int pref_freq)
4155 if (persistent_group && wpa_s->conf->persistent_reconnect)
4156 persistent_group = 2;
4159 * Increase GO config timeout if HT40 is used since it takes some time
4160 * to scan channels for coex purposes before the BSS can be started.
4162 p2p_set_config_timeout(wpa_s->global->p2p,
4163 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4165 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4166 go_intent, own_interface_addr, force_freq,
4167 persistent_group, ssid ? ssid->ssid : NULL,
4168 ssid ? ssid->ssid_len : 0,
4169 wpa_s->p2p_pd_before_go_neg, pref_freq,
4170 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4175 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4176 const u8 *peer_addr,
4177 enum p2p_wps_method wps_method,
4178 int go_intent, const u8 *own_interface_addr,
4179 unsigned int force_freq, int persistent_group,
4180 struct wpa_ssid *ssid, unsigned int pref_freq)
4182 if (persistent_group && wpa_s->conf->persistent_reconnect)
4183 persistent_group = 2;
4185 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4186 go_intent, own_interface_addr, force_freq,
4187 persistent_group, ssid ? ssid->ssid : NULL,
4188 ssid ? ssid->ssid_len : 0, pref_freq,
4189 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4194 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4196 wpa_s->p2p_join_scan_count++;
4197 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4198 wpa_s->p2p_join_scan_count);
4199 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4200 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4201 " for join operationg - stop join attempt",
4202 MAC2STR(wpa_s->pending_join_iface_addr));
4203 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4204 if (wpa_s->p2p_auto_pd) {
4205 wpa_s->p2p_auto_pd = 0;
4206 wpa_msg_global(wpa_s, MSG_INFO,
4207 P2P_EVENT_PROV_DISC_FAILURE
4208 " p2p_dev_addr=" MACSTR " status=N/A",
4209 MAC2STR(wpa_s->pending_join_dev_addr));
4212 wpa_msg_global(wpa_s->parent, MSG_INFO,
4213 P2P_EVENT_GROUP_FORMATION_FAILURE);
4218 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4221 unsigned int num, i;
4222 struct wpa_used_freq_data *freqs;
4224 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4225 /* Multiple channels are supported and not all are in use */
4229 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4230 sizeof(struct wpa_used_freq_data));
4234 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4235 wpa_s->num_multichan_concurrent);
4237 for (i = 0; i < num; i++) {
4238 if (freqs[i].freq == freq) {
4239 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4246 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4255 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4256 const u8 *peer_dev_addr)
4258 struct wpa_bss *bss;
4261 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4264 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4265 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4270 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4272 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4273 "%ld.%06ld (%supdated in last scan)",
4274 bss->last_update.sec, bss->last_update.usec,
4275 updated ? "": "not ");
4281 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4282 struct wpa_scan_results *scan_res)
4284 struct wpa_bss *bss = NULL;
4286 u8 iface_addr[ETH_ALEN];
4288 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4290 if (wpa_s->global->p2p_disabled)
4293 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4294 scan_res ? (int) scan_res->num : -1,
4295 wpa_s->p2p_auto_join ? "auto_" : "");
4298 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4300 if (wpa_s->p2p_auto_pd) {
4301 int join = wpas_p2p_peer_go(wpa_s,
4302 wpa_s->pending_join_dev_addr);
4304 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4305 wpa_s->auto_pd_scan_retry++;
4306 bss = wpa_bss_get_bssid_latest(
4307 wpa_s, wpa_s->pending_join_dev_addr);
4310 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4311 "the peer " MACSTR " at %d MHz",
4312 wpa_s->auto_pd_scan_retry,
4314 pending_join_dev_addr),
4316 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4324 wpa_s->p2p_auto_pd = 0;
4325 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4326 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4327 MAC2STR(wpa_s->pending_join_dev_addr), join);
4328 if (p2p_prov_disc_req(wpa_s->global->p2p,
4329 wpa_s->pending_join_dev_addr, NULL,
4330 wpa_s->pending_pd_config_methods, join,
4331 0, wpa_s->user_initiated_pd) < 0) {
4332 wpa_s->p2p_auto_pd = 0;
4333 wpa_msg_global(wpa_s, MSG_INFO,
4334 P2P_EVENT_PROV_DISC_FAILURE
4335 " p2p_dev_addr=" MACSTR " status=N/A",
4336 MAC2STR(wpa_s->pending_join_dev_addr));
4341 if (wpa_s->p2p_auto_join) {
4342 int join = wpas_p2p_peer_go(wpa_s,
4343 wpa_s->pending_join_dev_addr);
4345 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4346 "running a GO -> use GO Negotiation");
4347 wpa_msg_global(wpa_s->parent, MSG_INFO,
4348 P2P_EVENT_FALLBACK_TO_GO_NEG
4349 "reason=peer-not-running-GO");
4350 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4351 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4352 wpa_s->p2p_persistent_group, 0, 0, 0,
4353 wpa_s->p2p_go_intent,
4354 wpa_s->p2p_connect_freq,
4355 wpa_s->p2p_persistent_id,
4356 wpa_s->p2p_pd_before_go_neg,
4362 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4363 "try to join the group", join ? "" :
4366 wpa_msg_global(wpa_s->parent, MSG_INFO,
4367 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
4368 wpa_s->p2p_fallback_to_go_neg = 1;
4372 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4373 wpa_s->pending_join_iface_addr);
4375 p2p_get_interface_addr(wpa_s->global->p2p,
4376 wpa_s->pending_join_dev_addr,
4378 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4379 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4380 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4381 "address for join from " MACSTR " to " MACSTR
4382 " based on newly discovered P2P peer entry",
4383 MAC2STR(wpa_s->pending_join_iface_addr),
4384 MAC2STR(iface_addr));
4385 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4388 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4389 wpa_s->pending_join_iface_addr);
4392 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4393 "from P2P peer table: %d MHz", freq);
4395 if (wpa_s->p2p_join_ssid_len) {
4396 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4397 MACSTR " and SSID %s",
4398 MAC2STR(wpa_s->pending_join_iface_addr),
4399 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4400 wpa_s->p2p_join_ssid_len));
4401 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4402 wpa_s->p2p_join_ssid,
4403 wpa_s->p2p_join_ssid_len);
4406 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4407 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4408 bss = wpa_bss_get_bssid_latest(wpa_s,
4409 wpa_s->pending_join_iface_addr);
4413 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4414 "from BSS table: %d MHz (SSID %s)", freq,
4415 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4420 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4421 wpa_msg_global(wpa_s->parent, MSG_INFO,
4422 P2P_EVENT_GROUP_FORMATION_FAILURE
4423 "reason=FREQ_CONFLICT");
4427 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4428 "prior to joining an existing group (GO " MACSTR
4430 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4431 wpa_s->pending_pd_before_join = 1;
4433 switch (wpa_s->pending_join_wps_method) {
4434 case WPS_PIN_DISPLAY:
4435 method = WPS_CONFIG_KEYPAD;
4437 case WPS_PIN_KEYPAD:
4438 method = WPS_CONFIG_DISPLAY;
4441 method = WPS_CONFIG_PUSHBUTTON;
4448 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4449 wpa_s->pending_join_dev_addr) ==
4452 * We have already performed provision discovery for
4453 * joining the group. Proceed directly to join
4454 * operation without duplicated provision discovery. */
4455 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4456 "with " MACSTR " already done - proceed to "
4458 MAC2STR(wpa_s->pending_join_dev_addr));
4459 wpa_s->pending_pd_before_join = 0;
4463 if (p2p_prov_disc_req(wpa_s->global->p2p,
4464 wpa_s->pending_join_dev_addr,
4466 freq, wpa_s->user_initiated_pd) < 0) {
4467 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4468 "Discovery Request before joining an "
4470 wpa_s->pending_pd_before_join = 0;
4476 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4477 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4478 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4479 wpas_p2p_check_join_scan_limit(wpa_s);
4483 /* Start join operation immediately */
4484 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4488 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4489 const u8 *ssid, size_t ssid_len)
4492 struct wpa_driver_scan_params params;
4493 struct wpabuf *wps_ie, *ies;
4495 int freqs[2] = { 0, 0 };
4497 os_memset(¶ms, 0, sizeof(params));
4499 /* P2P Wildcard SSID */
4500 params.num_ssids = 1;
4501 if (ssid && ssid_len) {
4502 params.ssids[0].ssid = ssid;
4503 params.ssids[0].ssid_len = ssid_len;
4504 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4505 wpa_s->p2p_join_ssid_len = ssid_len;
4507 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4508 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4509 wpa_s->p2p_join_ssid_len = 0;
4512 wpa_s->wps->dev.p2p = 1;
4513 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4514 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4516 if (wps_ie == NULL) {
4517 wpas_p2p_scan_res_join(wpa_s, NULL);
4521 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4522 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4524 wpabuf_free(wps_ie);
4525 wpas_p2p_scan_res_join(wpa_s, NULL);
4528 wpabuf_put_buf(ies, wps_ie);
4529 wpabuf_free(wps_ie);
4531 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4533 params.p2p_probe = 1;
4534 params.extra_ies = wpabuf_head(ies);
4535 params.extra_ies_len = wpabuf_len(ies);
4540 * If freq is not provided, check the operating freq of the GO
4541 * and use a single channel scan on if possible.
4543 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4544 wpa_s->pending_join_iface_addr);
4550 params.freqs = freqs;
4554 * Run a scan to update BSS table and start Provision Discovery once
4555 * the new scan results become available.
4557 ret = wpa_drv_scan(wpa_s, ¶ms);
4559 os_get_reltime(&wpa_s->scan_trigger_time);
4560 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4561 wpa_s->own_scan_requested = 1;
4567 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4569 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4570 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4571 wpas_p2p_check_join_scan_limit(wpa_s);
4576 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4578 struct wpa_supplicant *wpa_s = eloop_ctx;
4579 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4583 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4584 const u8 *dev_addr, enum p2p_wps_method wps_method,
4585 int auto_join, int op_freq,
4586 const u8 *ssid, size_t ssid_len)
4588 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4589 MACSTR " dev " MACSTR " op_freq=%d)%s",
4590 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4591 auto_join ? " (auto_join)" : "");
4592 if (ssid && ssid_len) {
4593 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4594 wpa_ssid_txt(ssid, ssid_len));
4597 wpa_s->p2p_auto_pd = 0;
4598 wpa_s->p2p_auto_join = !!auto_join;
4599 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4600 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4601 wpa_s->pending_join_wps_method = wps_method;
4603 /* Make sure we are not running find during connection establishment */
4604 wpas_p2p_stop_find(wpa_s);
4606 wpa_s->p2p_join_scan_count = 0;
4607 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4612 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4613 const u8 *ssid, size_t ssid_len)
4615 struct wpa_supplicant *group;
4616 struct p2p_go_neg_results res;
4617 struct wpa_bss *bss;
4619 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4622 if (group != wpa_s) {
4623 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4624 sizeof(group->p2p_pin));
4625 group->p2p_wps_method = wpa_s->p2p_wps_method;
4628 * Need to mark the current interface for p2p_group_formation
4629 * when a separate group interface is not used. This is needed
4630 * to allow p2p_cancel stop a pending p2p_connect-join.
4631 * wpas_p2p_init_group_interface() addresses this for the case
4632 * where a separate group interface is used.
4634 wpa_s->global->p2p_group_formation = wpa_s;
4637 group->p2p_in_provisioning = 1;
4638 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4640 os_memset(&res, 0, sizeof(res));
4641 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4642 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4644 res.wps_method = wpa_s->pending_join_wps_method;
4645 if (freq && ssid && ssid_len) {
4647 res.ssid_len = ssid_len;
4648 os_memcpy(res.ssid, ssid, ssid_len);
4650 bss = wpa_bss_get_bssid_latest(wpa_s,
4651 wpa_s->pending_join_iface_addr);
4653 res.freq = bss->freq;
4654 res.ssid_len = bss->ssid_len;
4655 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4656 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4658 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4662 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4663 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4665 wpa_drv_cancel_remain_on_channel(wpa_s);
4666 wpa_s->off_channel_freq = 0;
4667 wpa_s->roc_waiting_drv_freq = 0;
4669 wpas_start_wps_enrollee(group, &res);
4672 * Allow a longer timeout for join-a-running-group than normal 15
4673 * second group formation timeout since the GO may not have authorized
4674 * our connection yet.
4676 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4677 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4684 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4685 int *force_freq, int *pref_freq, int go)
4687 struct wpa_used_freq_data *freqs;
4688 int res, best_freq, num_unused;
4689 unsigned int freq_in_use = 0, num, i;
4691 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4692 sizeof(struct wpa_used_freq_data));
4696 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4697 wpa_s->num_multichan_concurrent);
4700 * It is possible that the total number of used frequencies is bigger
4701 * than the number of frequencies used for P2P, so get the system wide
4702 * number of unused frequencies.
4704 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4706 wpa_printf(MSG_DEBUG,
4707 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4708 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4713 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4715 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4717 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
4718 ieee80211_is_dfs(freq)) {
4720 * If freq is a DFS channel and DFS is offloaded
4721 * to the driver, allow P2P GO to use it.
4723 wpa_printf(MSG_DEBUG,
4724 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
4727 wpa_printf(MSG_DEBUG,
4728 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
4735 for (i = 0; i < num; i++) {
4736 if (freqs[i].freq == freq)
4740 if (num_unused <= 0 && !freq_in_use) {
4741 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4746 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4747 "requested channel (%u MHz)", freq);
4752 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4754 /* We have a candidate frequency to use */
4755 if (best_freq > 0) {
4756 if (*pref_freq == 0 && num_unused > 0) {
4757 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4759 *pref_freq = best_freq;
4761 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4763 *force_freq = best_freq;
4765 } else if (num_unused > 0) {
4766 wpa_printf(MSG_DEBUG,
4767 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4770 wpa_printf(MSG_DEBUG,
4771 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4785 * wpas_p2p_connect - Request P2P Group Formation to be started
4786 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4787 * @peer_addr: Address of the peer P2P Device
4788 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4789 * @persistent_group: Whether to create a persistent group
4790 * @auto_join: Whether to select join vs. GO Negotiation automatically
4791 * @join: Whether to join an existing group (as a client) instead of starting
4792 * Group Owner negotiation; @peer_addr is BSSID in that case
4793 * @auth: Whether to only authorize the connection instead of doing that and
4794 * initiating Group Owner negotiation
4795 * @go_intent: GO Intent or -1 to use default
4796 * @freq: Frequency for the group or 0 for auto-selection
4797 * @persistent_id: Persistent group credentials to use for forcing GO
4798 * parameters or -1 to generate new values (SSID/passphrase)
4799 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4800 * interoperability workaround when initiating group formation
4801 * @ht40: Start GO with 40 MHz channel width
4802 * @vht: Start GO with VHT support
4803 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4804 * failure, -2 on failure due to channel not currently available,
4805 * -3 if forced channel is not supported
4807 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4808 const char *pin, enum p2p_wps_method wps_method,
4809 int persistent_group, int auto_join, int join, int auth,
4810 int go_intent, int freq, int persistent_id, int pd,
4813 int force_freq = 0, pref_freq = 0;
4815 enum wpa_driver_if_type iftype;
4817 struct wpa_ssid *ssid = NULL;
4819 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4822 if (persistent_id >= 0) {
4823 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4824 if (ssid == NULL || ssid->disabled != 2 ||
4825 ssid->mode != WPAS_MODE_P2P_GO)
4829 os_free(wpa_s->global->add_psk);
4830 wpa_s->global->add_psk = NULL;
4832 wpa_s->global->p2p_fail_on_wps_complete = 0;
4833 wpa_s->global->pending_p2ps_group = 0;
4836 go_intent = wpa_s->conf->p2p_go_intent;
4839 wpa_s->p2p_long_listen = 0;
4841 wpa_s->p2p_wps_method = wps_method;
4842 wpa_s->p2p_persistent_group = !!persistent_group;
4843 wpa_s->p2p_persistent_id = persistent_id;
4844 wpa_s->p2p_go_intent = go_intent;
4845 wpa_s->p2p_connect_freq = freq;
4846 wpa_s->p2p_fallback_to_go_neg = 0;
4847 wpa_s->p2p_pd_before_go_neg = !!pd;
4848 wpa_s->p2p_go_ht40 = !!ht40;
4849 wpa_s->p2p_go_vht = !!vht;
4852 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4853 else if (wps_method == WPS_PIN_DISPLAY) {
4854 ret = wps_generate_pin();
4855 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
4857 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
4858 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
4859 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4862 wpa_s->p2p_pin[0] = '\0';
4864 if (join || auto_join) {
4865 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4867 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4868 "connect a running group from " MACSTR,
4869 MAC2STR(peer_addr));
4870 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4873 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4874 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4876 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4877 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4881 os_get_reltime(&wpa_s->p2p_auto_started);
4882 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4884 wpa_s->p2p_auto_started.sec,
4885 wpa_s->p2p_auto_started.usec);
4887 wpa_s->user_initiated_pd = 1;
4888 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4889 auto_join, freq, NULL, 0) < 0)
4894 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4898 wpas_p2p_set_own_freq_preference(wpa_s,
4899 force_freq ? force_freq : pref_freq);
4901 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4903 if (wpa_s->create_p2p_iface) {
4904 /* Prepare to add a new interface for the group */
4905 iftype = WPA_IF_P2P_GROUP;
4906 if (go_intent == 15)
4907 iftype = WPA_IF_P2P_GO;
4908 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4909 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4910 "interface for the group");
4914 if_addr = wpa_s->pending_interface_addr;
4916 if_addr = wpa_s->own_addr;
4919 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4921 force_freq, persistent_group, ssid,
4927 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4928 go_intent, if_addr, force_freq,
4929 persistent_group, ssid, pref_freq) < 0) {
4930 if (wpa_s->create_p2p_iface)
4931 wpas_p2p_remove_pending_group_interface(wpa_s);
4939 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4940 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4941 * @freq: Frequency of the channel in MHz
4942 * @duration: Duration of the stay on the channel in milliseconds
4944 * This callback is called when the driver indicates that it has started the
4945 * requested remain-on-channel duration.
4947 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4948 unsigned int freq, unsigned int duration)
4950 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4952 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)",
4953 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4954 wpa_s->roc_waiting_drv_freq, freq, duration);
4955 if (wpa_s->off_channel_freq &&
4956 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4957 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4958 wpa_s->pending_listen_duration);
4959 wpa_s->pending_listen_freq = 0;
4961 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4962 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4968 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
4970 /* Limit maximum Listen state time based on driver limitation. */
4971 if (timeout > wpa_s->max_remain_on_chan)
4972 timeout = wpa_s->max_remain_on_chan;
4974 return p2p_listen(wpa_s->global->p2p, timeout);
4979 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4980 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4981 * @freq: Frequency of the channel in MHz
4983 * This callback is called when the driver indicates that a remain-on-channel
4984 * operation has been completed, i.e., the duration on the requested channel
4987 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4990 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4991 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4992 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4993 wpas_p2p_listen_work_done(wpa_s);
4994 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4996 if (wpa_s->p2p_long_listen > 0)
4997 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4998 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4999 return; /* P2P module started a new operation */
5000 if (offchannel_pending_action_tx(wpa_s))
5002 if (wpa_s->p2p_long_listen > 0) {
5003 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5004 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5007 * When listen duration is over, stop listen & update p2p_state
5010 p2p_stop_listen(wpa_s->global->p2p);
5016 * wpas_p2p_group_remove - Remove a P2P group
5017 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5018 * @ifname: Network interface name of the group interface or "*" to remove all
5020 * Returns: 0 on success, -1 on failure
5022 * This function is used to remove a P2P group. This can be used to disconnect
5023 * from a group in which the local end is a P2P Client or to end a P2P Group in
5024 * case the local end is the Group Owner. If a virtual network interface was
5025 * created for this group, that interface will be removed. Otherwise, only the
5026 * configured P2P group network will be removed from the interface.
5028 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5030 struct wpa_global *global = wpa_s->global;
5031 struct wpa_supplicant *calling_wpa_s = wpa_s;
5033 if (os_strcmp(ifname, "*") == 0) {
5034 struct wpa_supplicant *prev;
5035 wpa_s = global->ifaces;
5038 wpa_s = wpa_s->next;
5039 if (prev->p2p_group_interface !=
5040 NOT_P2P_GROUP_INTERFACE ||
5041 (prev->current_ssid &&
5042 prev->current_ssid->p2p_group))
5043 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5048 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5049 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5053 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5057 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5062 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5064 if (wpa_s->best_24_freq > 0 &&
5065 p2p_supported_freq_go(wpa_s->global->p2p,
5066 wpa_s->best_24_freq)) {
5067 freq = wpa_s->best_24_freq;
5068 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5069 "channel: %d MHz", freq);
5071 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5073 freq = 2412 + (r % 3) * 25;
5074 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5075 "channel: %d MHz", freq);
5080 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5082 if (wpa_s->best_5_freq > 0 &&
5083 p2p_supported_freq_go(wpa_s->global->p2p,
5084 wpa_s->best_5_freq)) {
5085 freq = wpa_s->best_5_freq;
5086 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5087 "channel: %d MHz", freq);
5089 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5091 freq = 5180 + (r % 4) * 20;
5092 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5093 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5094 "5 GHz channel for P2P group");
5097 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5098 "channel: %d MHz", freq);
5102 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5103 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5104 ieee80211_is_dfs(freq)) {
5106 * If freq is a DFS channel and DFS is offloaded to the
5107 * driver, allow P2P GO to use it.
5109 wpa_printf(MSG_DEBUG, "P2P: "
5110 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5114 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5115 "(%u MHz) is not supported for P2P uses",
5124 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5125 struct p2p_go_neg_results *params,
5126 const struct p2p_channels *channels)
5130 /* first try some random selection of the social channels */
5131 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5134 for (i = 0; i < 3; i++) {
5135 params->freq = 2412 + ((r + i) % 3) * 25;
5136 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5137 freq_included(channels, params->freq) &&
5138 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5142 /* try all channels in reg. class 81 */
5143 for (i = 0; i < 11; i++) {
5144 params->freq = 2412 + i * 5;
5145 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5146 freq_included(channels, params->freq) &&
5147 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5151 /* try all channels in operating class 115 */
5152 for (i = 0; i < 4; i++) {
5153 params->freq = 5180 + i * 20;
5154 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5155 freq_included(channels, params->freq) &&
5156 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5160 /* try all channels in operating class 124 */
5161 for (i = 0; i < 4; i++) {
5162 params->freq = 5745 + i * 20;
5163 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5164 freq_included(channels, params->freq) &&
5165 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5169 /* try social channel class 180 channel 2 */
5170 params->freq = 58320 + 1 * 2160;
5171 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5172 freq_included(channels, params->freq) &&
5173 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5176 /* try all channels in reg. class 180 */
5177 for (i = 0; i < 4; i++) {
5178 params->freq = 58320 + i * 2160;
5179 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5180 freq_included(channels, params->freq) &&
5181 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5185 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
5188 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5194 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5195 struct p2p_go_neg_results *params,
5196 int freq, int ht40, int vht,
5197 const struct p2p_channels *channels)
5199 struct wpa_used_freq_data *freqs;
5200 unsigned int pref_freq, cand_freq;
5201 unsigned int num, i;
5203 os_memset(params, 0, sizeof(*params));
5204 params->role_go = 1;
5205 params->ht40 = ht40;
5208 if (!freq_included(channels, freq)) {
5209 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5213 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5214 "frequency %d MHz", freq);
5215 params->freq = freq;
5216 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5217 wpa_s->conf->p2p_oper_channel >= 1 &&
5218 wpa_s->conf->p2p_oper_channel <= 11 &&
5219 freq_included(channels,
5220 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5221 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5222 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5223 "frequency %d MHz", params->freq);
5224 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5225 wpa_s->conf->p2p_oper_reg_class == 116 ||
5226 wpa_s->conf->p2p_oper_reg_class == 117 ||
5227 wpa_s->conf->p2p_oper_reg_class == 124 ||
5228 wpa_s->conf->p2p_oper_reg_class == 126 ||
5229 wpa_s->conf->p2p_oper_reg_class == 127) &&
5230 freq_included(channels,
5231 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
5232 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5233 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5234 "frequency %d MHz", params->freq);
5235 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5236 wpa_s->best_overall_freq > 0 &&
5237 p2p_supported_freq_go(wpa_s->global->p2p,
5238 wpa_s->best_overall_freq) &&
5239 freq_included(channels, wpa_s->best_overall_freq)) {
5240 params->freq = wpa_s->best_overall_freq;
5241 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5242 "channel %d MHz", params->freq);
5243 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5244 wpa_s->best_24_freq > 0 &&
5245 p2p_supported_freq_go(wpa_s->global->p2p,
5246 wpa_s->best_24_freq) &&
5247 freq_included(channels, wpa_s->best_24_freq)) {
5248 params->freq = wpa_s->best_24_freq;
5249 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5250 "channel %d MHz", params->freq);
5251 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5252 wpa_s->best_5_freq > 0 &&
5253 p2p_supported_freq_go(wpa_s->global->p2p,
5254 wpa_s->best_5_freq) &&
5255 freq_included(channels, wpa_s->best_5_freq)) {
5256 params->freq = wpa_s->best_5_freq;
5257 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5258 "channel %d MHz", params->freq);
5259 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5261 params->freq = pref_freq;
5262 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5263 "channels", params->freq);
5265 /* no preference, select some channel */
5266 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
5270 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5271 sizeof(struct wpa_used_freq_data));
5275 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5276 wpa_s->num_multichan_concurrent);
5278 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5280 /* First try the best used frequency if possible */
5281 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5282 params->freq = cand_freq;
5284 /* Try any of the used frequencies */
5285 for (i = 0; i < num; i++) {
5286 if (freq_included(channels, freqs[i].freq)) {
5287 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5289 params->freq = freqs[i].freq;
5295 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5296 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5300 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5304 for (i = 0; i < num; i++) {
5305 if (freqs[i].freq == freq)
5310 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5312 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5316 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5326 static struct wpa_supplicant *
5327 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5330 struct wpa_supplicant *group_wpa_s;
5332 if (!wpas_p2p_create_iface(wpa_s)) {
5333 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5335 wpa_s->p2p_first_connection_timeout = 0;
5339 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5340 WPA_IF_P2P_CLIENT) < 0) {
5341 wpa_msg_global(wpa_s, MSG_ERROR,
5342 "P2P: Failed to add group interface");
5345 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5346 if (group_wpa_s == NULL) {
5347 wpa_msg_global(wpa_s, MSG_ERROR,
5348 "P2P: Failed to initialize group interface");
5349 wpas_p2p_remove_pending_group_interface(wpa_s);
5353 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5354 group_wpa_s->ifname);
5355 group_wpa_s->p2p_first_connection_timeout = 0;
5361 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5362 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5363 * @persistent_group: Whether to create a persistent group
5364 * @freq: Frequency for the group or 0 to indicate no hardcoding
5365 * @ht40: Start GO with 40 MHz channel width
5366 * @vht: Start GO with VHT support
5367 * Returns: 0 on success, -1 on failure
5369 * This function creates a new P2P group with the local end as the Group Owner,
5370 * i.e., without using Group Owner Negotiation.
5372 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5373 int freq, int ht40, int vht)
5375 struct p2p_go_neg_results params;
5377 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5380 os_free(wpa_s->global->add_psk);
5381 wpa_s->global->add_psk = NULL;
5383 /* Make sure we are not running find during connection establishment */
5384 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5385 wpas_p2p_stop_find_oper(wpa_s);
5387 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5391 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5394 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5395 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5396 ieee80211_is_dfs(params.freq)) {
5398 * If freq is a DFS channel and DFS is offloaded to the
5399 * driver, allow P2P GO to use it.
5401 wpa_printf(MSG_DEBUG,
5402 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5403 __func__, params.freq);
5405 wpa_printf(MSG_DEBUG,
5406 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5411 p2p_go_params(wpa_s->global->p2p, ¶ms);
5412 params.persistent_group = persistent_group;
5414 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5417 wpas_start_wps_go(wpa_s, ¶ms, 0);
5423 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5424 struct wpa_ssid *params, int addr_allocated,
5427 struct wpa_ssid *ssid;
5429 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5432 wpa_s->p2p_last_4way_hs_fail = NULL;
5434 wpa_supplicant_ap_deinit(wpa_s);
5436 ssid = wpa_config_add_network(wpa_s->conf);
5439 wpa_config_set_network_defaults(ssid);
5440 ssid->temporary = 1;
5441 ssid->proto = WPA_PROTO_RSN;
5442 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5443 ssid->group_cipher = WPA_CIPHER_CCMP;
5444 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5445 ssid->ssid = os_malloc(params->ssid_len);
5446 if (ssid->ssid == NULL) {
5447 wpa_config_remove_network(wpa_s->conf, ssid->id);
5450 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5451 ssid->ssid_len = params->ssid_len;
5452 ssid->p2p_group = 1;
5453 ssid->export_keys = 1;
5454 if (params->psk_set) {
5455 os_memcpy(ssid->psk, params->psk, 32);
5458 if (params->passphrase)
5459 ssid->passphrase = os_strdup(params->passphrase);
5461 wpa_s->show_group_started = 1;
5462 wpa_s->p2p_in_invitation = 1;
5463 wpa_s->p2p_invite_go_freq = freq;
5465 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5467 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5468 wpas_p2p_group_formation_timeout,
5469 wpa_s->parent, NULL);
5470 wpa_supplicant_select_network(wpa_s, ssid);
5476 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5477 struct wpa_ssid *ssid, int addr_allocated,
5478 int force_freq, int neg_freq, int ht40,
5479 int vht, const struct p2p_channels *channels,
5480 int connection_timeout)
5482 struct p2p_go_neg_results params;
5485 if (ssid->disabled != 2 || ssid->ssid == NULL)
5488 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5489 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5490 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5495 os_free(wpa_s->global->add_psk);
5496 wpa_s->global->add_psk = NULL;
5498 /* Make sure we are not running find during connection establishment */
5499 wpas_p2p_stop_find_oper(wpa_s);
5501 wpa_s->p2p_fallback_to_go_neg = 0;
5503 if (ssid->mode == WPAS_MODE_P2P_GO) {
5504 if (force_freq > 0) {
5505 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5509 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5511 (freq > 0 && !freq_included(channels, freq)))
5514 } else if (ssid->mode == WPAS_MODE_INFRA) {
5516 if (freq <= 0 || !freq_included(channels, freq)) {
5517 struct os_reltime now;
5518 struct wpa_bss *bss =
5519 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
5521 os_get_reltime(&now);
5523 !os_reltime_expired(&now, &bss->last_update, 5) &&
5524 freq_included(channels, bss->freq))
5530 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5535 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5539 params.psk_set = ssid->psk_set;
5541 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5542 if (ssid->passphrase) {
5543 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5544 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5545 "persistent group");
5548 os_strlcpy(params.passphrase, ssid->passphrase,
5549 sizeof(params.passphrase));
5551 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5552 params.ssid_len = ssid->ssid_len;
5553 params.persistent_group = 1;
5555 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5559 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5561 wpa_s->p2p_first_connection_timeout = connection_timeout;
5562 wpas_start_wps_go(wpa_s, ¶ms, 0);
5568 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5569 struct wpabuf *proberesp_ies)
5571 struct wpa_supplicant *wpa_s = ctx;
5572 if (wpa_s->ap_iface) {
5573 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5574 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5575 wpabuf_free(beacon_ies);
5576 wpabuf_free(proberesp_ies);
5580 wpabuf_free(hapd->p2p_beacon_ie);
5581 hapd->p2p_beacon_ie = beacon_ies;
5583 wpabuf_free(hapd->p2p_probe_resp_ie);
5584 hapd->p2p_probe_resp_ie = proberesp_ies;
5586 wpabuf_free(beacon_ies);
5587 wpabuf_free(proberesp_ies);
5589 wpa_supplicant_ap_update_beacon(wpa_s);
5593 static void wpas_p2p_idle_update(void *ctx, int idle)
5595 struct wpa_supplicant *wpa_s = ctx;
5596 if (!wpa_s->ap_iface)
5598 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5600 if (wpa_s->global->p2p_fail_on_wps_complete &&
5601 wpa_s->p2p_in_provisioning) {
5602 wpas_p2p_grpform_fail_after_wps(wpa_s);
5605 wpas_p2p_set_group_idle_timeout(wpa_s);
5607 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5611 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5612 struct wpa_ssid *ssid)
5614 struct p2p_group *group;
5615 struct p2p_group_config *cfg;
5617 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5620 cfg = os_zalloc(sizeof(*cfg));
5624 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5625 cfg->persistent_group = 2;
5626 else if (ssid->p2p_persistent_group)
5627 cfg->persistent_group = 1;
5628 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5629 if (wpa_s->max_stations &&
5630 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5631 cfg->max_clients = wpa_s->max_stations;
5633 cfg->max_clients = wpa_s->conf->max_num_sta;
5634 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5635 cfg->ssid_len = ssid->ssid_len;
5636 cfg->freq = ssid->frequency;
5637 cfg->cb_ctx = wpa_s;
5638 cfg->ie_update = wpas_p2p_ie_update;
5639 cfg->idle_update = wpas_p2p_idle_update;
5641 group = p2p_group_init(wpa_s->global->p2p, cfg);
5644 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5645 p2p_group_notif_formation_done(group);
5646 wpa_s->p2p_group = group;
5651 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5654 struct wpa_ssid *ssid = wpa_s->current_ssid;
5656 if (!wpa_s->p2p_in_provisioning) {
5657 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5658 "provisioning not in progress");
5662 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5663 u8 go_dev_addr[ETH_ALEN];
5664 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5665 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5667 /* Clear any stored provisioning info */
5668 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5671 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5673 wpa_s->p2p_go_group_formation_completed = 1;
5674 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5676 * Use a separate timeout for initial data connection to
5677 * complete to allow the group to be removed automatically if
5678 * something goes wrong in this step before the P2P group idle
5679 * timeout mechanism is taken into use.
5681 wpa_dbg(wpa_s, MSG_DEBUG,
5682 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5683 P2P_MAX_INITIAL_CONN_WAIT);
5684 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5685 wpas_p2p_group_formation_timeout,
5686 wpa_s->parent, NULL);
5689 * Use a separate timeout for initial data connection to
5690 * complete to allow the group to be removed automatically if
5691 * the client does not complete data connection successfully.
5693 wpa_dbg(wpa_s, MSG_DEBUG,
5694 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5695 P2P_MAX_INITIAL_CONN_WAIT_GO);
5696 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5697 wpas_p2p_group_formation_timeout,
5698 wpa_s->parent, NULL);
5700 * Complete group formation on first successful data connection
5702 wpa_s->p2p_go_group_formation_completed = 0;
5704 if (wpa_s->global->p2p)
5705 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5706 wpas_group_formation_completed(wpa_s, 1);
5710 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5711 struct wps_event_fail *fail)
5713 if (!wpa_s->p2p_in_provisioning) {
5714 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5715 "provisioning not in progress");
5719 if (wpa_s->go_params) {
5720 p2p_clear_provisioning_info(
5722 wpa_s->go_params->peer_device_addr);
5725 wpas_notify_p2p_wps_failed(wpa_s, fail);
5727 if (wpa_s == wpa_s->global->p2p_group_formation) {
5729 * Allow some time for the failed WPS negotiation exchange to
5730 * complete, but remove the group since group formation cannot
5731 * succeed after provisioning failure.
5733 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5734 wpa_s->global->p2p_fail_on_wps_complete = 1;
5735 eloop_deplete_timeout(0, 50000,
5736 wpas_p2p_group_formation_timeout,
5737 wpa_s->parent, NULL);
5742 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5744 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5745 !wpa_s->p2p_in_provisioning)
5748 wpas_p2p_grpform_fail_after_wps(wpa_s);
5754 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5755 const char *config_method,
5756 enum wpas_p2p_prov_disc_use use,
5757 struct p2ps_provision *p2ps_prov)
5761 wpa_s->global->pending_p2ps_group = 0;
5762 wpa_s->p2p_fallback_to_go_neg = 0;
5763 wpa_s->pending_pd_use = NORMAL_PD;
5764 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
5765 p2ps_prov->conncap = p2ps_group_capability(
5766 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
5767 wpa_printf(MSG_DEBUG,
5768 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
5769 __func__, p2ps_prov->conncap,
5770 p2ps_prov->adv_id, p2ps_prov->conncap,
5771 p2ps_prov->status, p2ps_prov->info);
5774 } else if (os_strncmp(config_method, "display", 7) == 0)
5775 config_methods = WPS_CONFIG_DISPLAY;
5776 else if (os_strncmp(config_method, "keypad", 6) == 0)
5777 config_methods = WPS_CONFIG_KEYPAD;
5778 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5779 os_strncmp(config_method, "pushbutton", 10) == 0)
5780 config_methods = WPS_CONFIG_PUSHBUTTON;
5782 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5787 if (use == WPAS_P2P_PD_AUTO) {
5788 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5789 wpa_s->pending_pd_config_methods = config_methods;
5790 wpa_s->p2p_auto_pd = 1;
5791 wpa_s->p2p_auto_join = 0;
5792 wpa_s->pending_pd_before_join = 0;
5793 wpa_s->auto_pd_scan_retry = 0;
5794 wpas_p2p_stop_find(wpa_s);
5795 wpa_s->p2p_join_scan_count = 0;
5796 os_get_reltime(&wpa_s->p2p_auto_started);
5797 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5798 wpa_s->p2p_auto_started.sec,
5799 wpa_s->p2p_auto_started.usec);
5800 wpas_p2p_join_scan(wpa_s, NULL);
5804 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
5809 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
5810 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5815 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5818 return p2p_scan_result_text(ies, ies_len, buf, end);
5822 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5824 if (!offchannel_pending_action_tx(wpa_s))
5827 wpas_p2p_action_tx_clear(wpa_s);
5829 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5830 "operation request");
5831 offchannel_clear_pending_action_tx(wpa_s);
5835 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5836 enum p2p_discovery_type type,
5837 unsigned int num_req_dev_types, const u8 *req_dev_types,
5838 const u8 *dev_id, unsigned int search_delay,
5839 u8 seek_cnt, const char **seek_string, int freq)
5841 wpas_p2p_clear_pending_action_tx(wpa_s);
5842 wpa_s->p2p_long_listen = 0;
5844 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5845 wpa_s->p2p_in_provisioning)
5848 wpa_supplicant_cancel_sched_scan(wpa_s);
5850 return p2p_find(wpa_s->global->p2p, timeout, type,
5851 num_req_dev_types, req_dev_types, dev_id,
5852 search_delay, seek_cnt, seek_string, freq);
5856 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
5857 struct wpa_scan_results *scan_res)
5859 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5861 if (wpa_s->p2p_scan_work) {
5862 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
5863 wpa_s->p2p_scan_work = NULL;
5864 radio_work_done(work);
5867 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5871 * Indicate that results have been processed so that the P2P module can
5872 * continue pending tasks.
5874 p2p_scan_res_handled(wpa_s->global->p2p);
5878 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5880 wpas_p2p_clear_pending_action_tx(wpa_s);
5881 wpa_s->p2p_long_listen = 0;
5882 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5883 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5885 if (wpa_s->global->p2p)
5886 p2p_stop_find(wpa_s->global->p2p);
5888 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
5889 wpa_printf(MSG_DEBUG,
5890 "P2P: Do not consider the scan results after stop_find");
5891 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
5896 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5898 wpas_p2p_stop_find_oper(wpa_s);
5899 if (!wpa_s->global->pending_group_iface_for_p2ps)
5900 wpas_p2p_remove_pending_group_interface(wpa_s);
5904 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5906 struct wpa_supplicant *wpa_s = eloop_ctx;
5907 wpa_s->p2p_long_listen = 0;
5911 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5915 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5918 wpa_supplicant_cancel_sched_scan(wpa_s);
5919 wpas_p2p_clear_pending_action_tx(wpa_s);
5923 * This is a request for unlimited Listen state. However, at
5924 * least for now, this is mapped to a Listen state for one
5929 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5930 wpa_s->p2p_long_listen = 0;
5933 * Stop previous find/listen operation to avoid trying to request a new
5934 * remain-on-channel operation while the driver is still running the
5937 if (wpa_s->global->p2p)
5938 p2p_stop_find(wpa_s->global->p2p);
5940 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5941 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5942 wpa_s->p2p_long_listen = timeout * 1000;
5943 eloop_register_timeout(timeout, 0,
5944 wpas_p2p_long_listen_timeout,
5952 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5953 u8 *buf, size_t len, int p2p_group)
5955 struct wpabuf *p2p_ie;
5958 if (wpa_s->global->p2p_disabled)
5961 * Advertize mandatory cross connection capability even on
5962 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
5964 if (wpa_s->conf->p2p_disabled && p2p_group)
5966 if (wpa_s->global->p2p == NULL)
5971 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5972 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5974 wpabuf_free(p2p_ie);
5980 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5981 const u8 *dst, const u8 *bssid,
5982 const u8 *ie, size_t ie_len, int ssi_signal)
5984 if (wpa_s->global->p2p_disabled)
5986 if (wpa_s->global->p2p == NULL)
5989 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5991 case P2P_PREQ_NOT_P2P:
5992 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5995 case P2P_PREQ_MALFORMED:
5996 case P2P_PREQ_NOT_LISTEN:
5997 case P2P_PREQ_NOT_PROCESSED:
5998 default: /* make gcc happy */
6000 case P2P_PREQ_PROCESSED:
6006 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6007 const u8 *sa, const u8 *bssid,
6008 u8 category, const u8 *data, size_t len, int freq)
6010 if (wpa_s->global->p2p_disabled)
6012 if (wpa_s->global->p2p == NULL)
6015 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6020 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6022 if (wpa_s->global->p2p_disabled)
6024 if (wpa_s->global->p2p == NULL)
6027 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6031 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6033 p2p_group_deinit(wpa_s->p2p_group);
6034 wpa_s->p2p_group = NULL;
6036 wpa_s->ap_configured_cb = NULL;
6037 wpa_s->ap_configured_cb_ctx = NULL;
6038 wpa_s->ap_configured_cb_data = NULL;
6039 wpa_s->connect_without_scan = NULL;
6043 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6045 wpa_s->p2p_long_listen = 0;
6047 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6050 return p2p_reject(wpa_s->global->p2p, addr);
6054 /* Invite to reinvoke a persistent group */
6055 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6056 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6057 int ht40, int vht, int pref_freq)
6059 enum p2p_invite_role role;
6063 int no_pref_freq_given = pref_freq == 0;
6065 wpa_s->global->p2p_invite_group = NULL;
6067 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6069 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6071 wpa_s->p2p_persistent_go_freq = freq;
6072 wpa_s->p2p_go_ht40 = !!ht40;
6073 if (ssid->mode == WPAS_MODE_P2P_GO) {
6074 role = P2P_INVITE_ROLE_GO;
6075 if (peer_addr == NULL) {
6076 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6077 "address in invitation command");
6080 if (wpas_p2p_create_iface(wpa_s)) {
6081 if (wpas_p2p_add_group_interface(wpa_s,
6082 WPA_IF_P2P_GO) < 0) {
6083 wpa_printf(MSG_ERROR, "P2P: Failed to "
6084 "allocate a new interface for the "
6088 bssid = wpa_s->pending_interface_addr;
6090 bssid = wpa_s->own_addr;
6092 role = P2P_INVITE_ROLE_CLIENT;
6093 peer_addr = ssid->bssid;
6095 wpa_s->pending_invite_ssid_id = ssid->id;
6097 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6098 role == P2P_INVITE_ROLE_GO);
6102 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6105 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6106 no_pref_freq_given && pref_freq > 0 &&
6107 wpa_s->num_multichan_concurrent > 1 &&
6108 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6109 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6115 * Stop any find/listen operations before invitation and possibly
6116 * connection establishment.
6118 wpas_p2p_stop_find_oper(wpa_s);
6120 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6121 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6126 /* Invite to join an active group */
6127 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6128 const u8 *peer_addr, const u8 *go_dev_addr)
6130 struct wpa_global *global = wpa_s->global;
6131 enum p2p_invite_role role;
6133 struct wpa_ssid *ssid;
6135 int freq = 0, force_freq = 0, pref_freq = 0;
6138 wpa_s->p2p_persistent_go_freq = 0;
6139 wpa_s->p2p_go_ht40 = 0;
6140 wpa_s->p2p_go_vht = 0;
6142 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6143 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6146 if (wpa_s == NULL) {
6147 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6151 ssid = wpa_s->current_ssid;
6153 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6158 wpa_s->global->p2p_invite_group = wpa_s;
6159 persistent = ssid->p2p_persistent_group &&
6160 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6161 ssid->ssid, ssid->ssid_len);
6163 if (ssid->mode == WPAS_MODE_P2P_GO) {
6164 role = P2P_INVITE_ROLE_ACTIVE_GO;
6165 bssid = wpa_s->own_addr;
6166 if (go_dev_addr == NULL)
6167 go_dev_addr = wpa_s->global->p2p_dev_addr;
6168 freq = ssid->frequency;
6170 role = P2P_INVITE_ROLE_CLIENT;
6171 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6172 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6173 "invite to current group");
6176 bssid = wpa_s->bssid;
6177 if (go_dev_addr == NULL &&
6178 !is_zero_ether_addr(wpa_s->go_dev_addr))
6179 go_dev_addr = wpa_s->go_dev_addr;
6180 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6181 (int) wpa_s->assoc_freq;
6183 wpa_s->parent->pending_invite_ssid_id = -1;
6185 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6188 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6189 role == P2P_INVITE_ROLE_ACTIVE_GO);
6192 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6194 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6195 ssid->ssid, ssid->ssid_len, force_freq,
6196 go_dev_addr, persistent, pref_freq, -1);
6200 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6202 struct wpa_ssid *ssid = wpa_s->current_ssid;
6203 u8 go_dev_addr[ETH_ALEN];
6204 int network_id = -1;
6210 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6211 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6212 wpa_s->parent, NULL);
6215 if (!wpa_s->show_group_started || !ssid)
6218 wpa_s->show_group_started = 0;
6220 os_memset(go_dev_addr, 0, ETH_ALEN);
6221 if (ssid->bssid_set)
6222 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6223 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6225 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6227 if (wpa_s->global->p2p_group_formation == wpa_s)
6228 wpa_s->global->p2p_group_formation = NULL;
6230 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6231 (int) wpa_s->assoc_freq;
6234 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6237 res = os_snprintf(ip_addr, sizeof(ip_addr),
6238 " ip_addr=%u.%u.%u.%u "
6239 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6240 ip[0], ip[1], ip[2], ip[3],
6241 ip[4], ip[5], ip[6], ip[7],
6242 ip[8], ip[9], ip[10], ip[11]);
6243 if (os_snprintf_error(sizeof(ip_addr), res))
6247 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6248 ssid->passphrase == NULL && ssid->psk_set ?
6250 ssid->passphrase, go_dev_addr, persistent,
6254 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6257 network_id = ssid->id;
6258 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6262 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6263 u32 interval1, u32 duration2, u32 interval2)
6267 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6270 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6271 wpa_s->current_ssid == NULL ||
6272 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6275 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6276 wpa_s->own_addr, wpa_s->assoc_freq,
6277 duration1, interval1, duration2, interval2);
6279 wpa_s->waiting_presence_resp = 1;
6285 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6286 unsigned int interval)
6288 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6291 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6295 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6297 if (wpa_s->current_ssid == NULL) {
6299 * current_ssid can be cleared when P2P client interface gets
6300 * disconnected, so assume this interface was used as P2P
6305 return wpa_s->current_ssid->p2p_group &&
6306 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6310 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6312 struct wpa_supplicant *wpa_s = eloop_ctx;
6314 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6315 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6320 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6322 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6326 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6330 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6331 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6333 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6336 timeout = wpa_s->conf->p2p_group_idle;
6337 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6338 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6339 timeout = P2P_MAX_CLIENT_IDLE;
6345 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6346 timeout = 0; /* special client mode no-timeout */
6351 if (wpa_s->p2p_in_provisioning) {
6353 * Use the normal group formation timeout during the
6354 * provisioning phase to avoid terminating this process too
6355 * early due to group idle timeout.
6357 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6358 "during provisioning");
6362 if (wpa_s->show_group_started) {
6364 * Use the normal group formation timeout between the end of
6365 * the provisioning phase and completion of 4-way handshake to
6366 * avoid terminating this process too early due to group idle
6369 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6370 "while waiting for initial 4-way handshake to "
6375 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6377 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6382 /* Returns 1 if the interface was removed */
6383 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6384 u16 reason_code, const u8 *ie, size_t ie_len,
6385 int locally_generated)
6387 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6390 if (!locally_generated)
6391 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6394 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6395 wpa_s->current_ssid &&
6396 wpa_s->current_ssid->p2p_group &&
6397 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6398 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6399 "session is ending");
6400 if (wpas_p2p_group_delete(wpa_s,
6401 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6410 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6411 u16 reason_code, const u8 *ie, size_t ie_len,
6412 int locally_generated)
6414 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6417 if (!locally_generated)
6418 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6423 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6425 struct p2p_data *p2p = wpa_s->global->p2p;
6430 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6433 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6434 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6436 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6437 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6440 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6441 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6443 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6444 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6446 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6447 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6448 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6449 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6450 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6453 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6454 p2p_set_sec_dev_types(p2p,
6455 (void *) wpa_s->conf->sec_device_type,
6456 wpa_s->conf->num_sec_device_types);
6458 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6460 p2p_remove_wps_vendor_extensions(p2p);
6461 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6462 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6464 p2p_add_wps_vendor_extension(
6465 p2p, wpa_s->conf->wps_vendor_ext[i]);
6469 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6470 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6472 country[0] = wpa_s->conf->country[0];
6473 country[1] = wpa_s->conf->country[1];
6475 p2p_set_country(p2p, country);
6478 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6479 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6480 wpa_s->conf->p2p_ssid_postfix ?
6481 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6485 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6486 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6488 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6489 u8 reg_class, channel;
6494 if (wpa_s->conf->p2p_listen_reg_class &&
6495 wpa_s->conf->p2p_listen_channel) {
6496 reg_class = wpa_s->conf->p2p_listen_reg_class;
6497 channel = wpa_s->conf->p2p_listen_channel;
6502 * Pick one of the social channels randomly as the
6505 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6508 channel = 1 + (r % 3) * 5;
6511 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6514 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6517 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6518 u8 op_reg_class, op_channel, cfg_op_channel;
6521 if (wpa_s->conf->p2p_oper_reg_class &&
6522 wpa_s->conf->p2p_oper_channel) {
6523 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6524 op_channel = wpa_s->conf->p2p_oper_channel;
6529 * Use random operation channel from (1, 6, 11)
6530 *if no other preference is indicated.
6532 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6535 op_channel = 1 + (r % 3) * 5;
6538 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6541 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6545 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6546 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6547 wpa_s->conf->p2p_pref_chan) < 0) {
6548 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6552 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6553 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6558 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6559 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6563 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6566 if (!wpa_s->ap_iface)
6568 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6573 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6575 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6578 wpa_s->global->cross_connection = enabled;
6579 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6582 struct wpa_supplicant *iface;
6584 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6586 if (iface->cross_connect_enabled == 0)
6589 iface->cross_connect_enabled = 0;
6590 iface->cross_connect_in_use = 0;
6591 wpa_msg_global(iface->parent, MSG_INFO,
6592 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6594 iface->cross_connect_uplink);
6602 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6604 struct wpa_supplicant *iface;
6606 if (!uplink->global->cross_connection)
6609 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6610 if (!iface->cross_connect_enabled)
6612 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6615 if (iface->ap_iface == NULL)
6617 if (iface->cross_connect_in_use)
6620 iface->cross_connect_in_use = 1;
6621 wpa_msg_global(iface->parent, MSG_INFO,
6622 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6623 iface->ifname, iface->cross_connect_uplink);
6628 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6630 struct wpa_supplicant *iface;
6632 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6633 if (!iface->cross_connect_enabled)
6635 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6638 if (!iface->cross_connect_in_use)
6641 wpa_msg_global(iface->parent, MSG_INFO,
6642 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6643 iface->ifname, iface->cross_connect_uplink);
6644 iface->cross_connect_in_use = 0;
6649 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6651 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6652 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6653 wpa_s->cross_connect_disallowed)
6654 wpas_p2p_disable_cross_connect(wpa_s);
6656 wpas_p2p_enable_cross_connect(wpa_s);
6657 if (!wpa_s->ap_iface &&
6658 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6659 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6663 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6665 wpas_p2p_disable_cross_connect(wpa_s);
6666 if (!wpa_s->ap_iface &&
6667 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6669 wpas_p2p_set_group_idle_timeout(wpa_s);
6673 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6675 struct wpa_supplicant *iface;
6677 if (!wpa_s->global->cross_connection)
6680 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6683 if (iface->drv_flags &
6684 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6686 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
6687 iface != wpa_s->parent)
6690 wpa_s->cross_connect_enabled = 1;
6691 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6692 sizeof(wpa_s->cross_connect_uplink));
6693 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6694 "%s to %s whenever uplink is available",
6695 wpa_s->ifname, wpa_s->cross_connect_uplink);
6697 if (iface->ap_iface || iface->current_ssid == NULL ||
6698 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6699 iface->cross_connect_disallowed ||
6700 iface->wpa_state != WPA_COMPLETED)
6703 wpa_s->cross_connect_in_use = 1;
6704 wpa_msg_global(wpa_s->parent, MSG_INFO,
6705 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6706 wpa_s->ifname, wpa_s->cross_connect_uplink);
6712 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6714 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6715 !wpa_s->p2p_in_provisioning)
6716 return 0; /* not P2P client operation */
6718 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6720 if (wpa_s != wpa_s->parent)
6721 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6722 wpas_p2p_group_formation_failed(wpa_s);
6727 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6729 struct wpa_supplicant *wpa_s = eloop_ctx;
6730 wpas_p2p_notif_pbc_overlap(wpa_s);
6734 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6736 struct p2p_channels chan, cli_chan;
6737 struct wpa_supplicant *ifs;
6739 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6742 os_memset(&chan, 0, sizeof(chan));
6743 os_memset(&cli_chan, 0, sizeof(cli_chan));
6744 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6745 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6750 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6752 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6754 if (!ifs->current_ssid ||
6755 !ifs->current_ssid->p2p_group ||
6756 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6757 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6759 freq = ifs->current_ssid->frequency;
6760 if (freq_included(&chan, freq)) {
6761 wpa_dbg(ifs, MSG_DEBUG,
6762 "P2P GO operating frequency %d MHz in valid range",
6767 wpa_dbg(ifs, MSG_DEBUG,
6768 "P2P GO operating in invalid frequency %d MHz", freq);
6769 /* TODO: Consider using CSA or removing the group within
6771 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6776 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6777 struct wpa_scan_results *scan_res)
6779 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6783 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6785 struct wpa_global *global = wpa_s->global;
6789 if (global->p2p == NULL)
6792 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6794 if (wpa_s->pending_interface_name[0] &&
6795 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6798 peer = p2p_get_go_neg_peer(global->p2p);
6800 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6801 MACSTR, MAC2STR(peer));
6802 p2p_unauthorize(global->p2p, peer);
6806 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6807 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6808 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6812 if (wpa_s->pending_pd_before_join) {
6813 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6814 wpa_s->pending_pd_before_join = 0;
6818 wpas_p2p_stop_find(wpa_s);
6820 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6821 if (wpa_s == global->p2p_group_formation &&
6822 (wpa_s->p2p_in_provisioning ||
6823 wpa_s->parent->pending_interface_type ==
6824 WPA_IF_P2P_CLIENT)) {
6825 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6826 "formation found - cancelling",
6829 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6830 wpa_s->parent, NULL);
6831 if (wpa_s->p2p_in_provisioning) {
6832 wpas_group_formation_completed(wpa_s, 0);
6835 wpas_p2p_group_delete(wpa_s,
6836 P2P_GROUP_REMOVAL_REQUESTED);
6838 } else if (wpa_s->p2p_in_invitation) {
6839 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6842 wpas_p2p_group_formation_failed(wpa_s);
6847 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6855 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6857 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6860 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6861 "being available anymore");
6862 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6866 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6867 int freq_24, int freq_5, int freq_overall)
6869 struct p2p_data *p2p = wpa_s->global->p2p;
6872 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6876 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6879 struct p2p_data *p2p = wpa_s->global->p2p;
6884 if (hwaddr_aton(addr, peer))
6887 return p2p_unauthorize(p2p, peer);
6892 * wpas_p2p_disconnect - Disconnect from a P2P Group
6893 * @wpa_s: Pointer to wpa_supplicant data
6894 * Returns: 0 on success, -1 on failure
6896 * This can be used to disconnect from a group in which the local end is a P2P
6897 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6898 * virtual network interface was created for this group, that interface will be
6899 * removed. Otherwise, only the configured P2P group network will be removed
6900 * from the interface.
6902 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6908 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6913 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6917 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6920 ret = p2p_in_progress(wpa_s->global->p2p);
6923 * Check whether there is an ongoing WPS provisioning step (or
6924 * other parts of group formation) on another interface since
6925 * p2p_in_progress() does not report this to avoid issues for
6926 * scans during such provisioning step.
6928 if (wpa_s->global->p2p_group_formation &&
6929 wpa_s->global->p2p_group_formation != wpa_s) {
6930 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6931 "in group formation",
6932 wpa_s->global->p2p_group_formation->ifname);
6937 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6938 struct os_reltime now;
6939 os_get_reltime(&now);
6940 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6941 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6942 /* Wait for the first client has expired */
6943 wpa_s->global->p2p_go_wait_client.sec = 0;
6945 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6954 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6955 struct wpa_ssid *ssid)
6957 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6958 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6959 wpa_s->parent, NULL) > 0) {
6961 * Remove the network by scheduling the group formation
6962 * timeout to happen immediately. The teardown code
6963 * needs to be scheduled to run asynch later so that we
6964 * don't delete data from under ourselves unexpectedly.
6965 * Calling wpas_p2p_group_formation_timeout directly
6966 * causes a series of crashes in WPS failure scenarios.
6968 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6969 "P2P group network getting removed");
6970 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6971 wpa_s->parent, NULL);
6976 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6977 const u8 *addr, const u8 *ssid,
6983 for (s = wpa_s->conf->ssid; s; s = s->next) {
6984 if (s->disabled != 2)
6987 (ssid_len != s->ssid_len ||
6988 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6991 if (s->mode == WPAS_MODE_P2P_GO)
6995 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6996 return s; /* peer is GO in the persistent group */
6997 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6999 for (i = 0; i < s->num_p2p_clients; i++) {
7000 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
7001 addr, ETH_ALEN) == 0)
7002 return s; /* peer is P2P client in persistent
7011 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7014 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7015 wpa_s->parent, NULL) > 0) {
7017 * This can happen if WPS provisioning step is not terminated
7018 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7019 * peer was able to connect, there is no need to time out group
7020 * formation after this, though. In addition, this is used with
7021 * the initial connection wait on the GO as a separate formation
7022 * timeout and as such, expected to be hit after the initial WPS
7023 * provisioning step.
7025 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7027 if (!wpa_s->p2p_go_group_formation_completed &&
7028 !wpa_s->group_formation_reported) {
7030 * GO has not yet notified group formation success since
7031 * the WPS step was not completed cleanly. Do that
7032 * notification now since the P2P Client was able to
7033 * connect and as such, must have received the
7034 * credential from the WPS step.
7036 if (wpa_s->global->p2p)
7037 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7038 wpas_group_formation_completed(wpa_s, 1);
7041 if (!wpa_s->p2p_go_group_formation_completed) {
7042 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7043 wpa_s->p2p_go_group_formation_completed = 1;
7044 wpa_s->global->p2p_group_formation = NULL;
7045 wpa_s->p2p_in_provisioning = 0;
7046 wpa_s->p2p_in_invitation = 0;
7048 wpa_s->global->p2p_go_wait_client.sec = 0;
7051 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7055 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7058 struct wpa_supplicant *group = wpa_s;
7061 if (wpa_s->global->p2p_group_formation)
7062 group = wpa_s->global->p2p_group_formation;
7063 wpa_s = wpa_s->parent;
7064 offchannel_send_action_done(wpa_s);
7066 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7067 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7068 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7069 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7070 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7071 wpa_s->p2p_persistent_id,
7072 wpa_s->p2p_pd_before_go_neg,
7079 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7083 if (!wpa_s->p2p_fallback_to_go_neg ||
7084 wpa_s->p2p_in_provisioning <= 5)
7087 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7088 return 0; /* peer operating as a GO */
7090 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7091 "fallback to GO Negotiation");
7092 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7093 "reason=GO-not-found");
7094 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7096 return res == 1 ? 2 : 1;
7100 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7102 struct wpa_supplicant *ifs;
7104 if (wpa_s->wpa_state > WPA_SCANNING) {
7105 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7106 "concurrent operation",
7107 wpa_s->conf->p2p_search_delay);
7108 return wpa_s->conf->p2p_search_delay;
7111 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7113 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7114 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7115 "delay due to concurrent operation on "
7117 wpa_s->conf->p2p_search_delay,
7119 return wpa_s->conf->p2p_search_delay;
7127 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7128 struct wpa_ssid *s, const u8 *addr,
7131 struct psk_list_entry *psk, *tmp;
7134 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7136 if ((iface_addr && !psk->p2p &&
7137 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7138 (!iface_addr && psk->p2p &&
7139 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7140 wpa_dbg(wpa_s, MSG_DEBUG,
7141 "P2P: Remove persistent group PSK list entry for "
7143 MAC2STR(psk->addr), psk->p2p);
7144 dl_list_del(&psk->list);
7154 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7155 const u8 *p2p_dev_addr,
7156 const u8 *psk, size_t psk_len)
7158 struct wpa_ssid *ssid = wpa_s->current_ssid;
7159 struct wpa_ssid *persistent;
7160 struct psk_list_entry *p, *last;
7162 if (psk_len != sizeof(p->psk))
7166 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7167 " p2p_dev_addr=" MACSTR,
7168 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7169 if (is_zero_ether_addr(p2p_dev_addr))
7170 p2p_dev_addr = NULL;
7172 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7176 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7177 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7178 /* To be added to persistent group once created */
7179 if (wpa_s->global->add_psk == NULL) {
7180 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7181 if (wpa_s->global->add_psk == NULL)
7184 p = wpa_s->global->add_psk;
7187 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7190 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7192 os_memcpy(p->psk, psk, psk_len);
7196 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7197 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7201 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7204 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7208 p = os_zalloc(sizeof(*p));
7213 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7216 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7218 os_memcpy(p->psk, psk, psk_len);
7220 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7221 (last = dl_list_last(&persistent->psk_list,
7222 struct psk_list_entry, list))) {
7223 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7224 MACSTR " (p2p=%u) to make room for a new one",
7225 MAC2STR(last->addr), last->p2p);
7226 dl_list_del(&last->list);
7230 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7231 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7232 p2p_dev_addr == NULL);
7234 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7235 MACSTR, MAC2STR(p2p_dev_addr));
7237 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7240 dl_list_add(&persistent->psk_list, &p->list);
7242 if (wpa_s->parent->conf->update_config &&
7243 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7244 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7248 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7249 struct wpa_ssid *s, const u8 *addr,
7254 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7255 if (res > 0 && wpa_s->conf->update_config &&
7256 wpa_config_write(wpa_s->confname, wpa_s->conf))
7257 wpa_dbg(wpa_s, MSG_DEBUG,
7258 "P2P: Failed to update configuration");
7262 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7263 const u8 *peer, int iface_addr)
7265 struct hostapd_data *hapd;
7266 struct hostapd_wpa_psk *psk, *prev, *rem;
7267 struct sta_info *sta;
7269 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7270 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7273 /* Remove per-station PSK entry */
7274 hapd = wpa_s->ap_iface->bss[0];
7276 psk = hapd->conf->ssid.wpa_psk;
7278 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7280 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7281 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7282 MACSTR " iface_addr=%d",
7283 MAC2STR(peer), iface_addr);
7285 prev->next = psk->next;
7287 hapd->conf->ssid.wpa_psk = psk->next;
7297 /* Disconnect from group */
7299 sta = ap_get_sta(hapd, peer);
7301 sta = ap_get_sta_p2p(hapd, peer);
7303 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7304 " (iface_addr=%d) from group",
7305 MAC2STR(peer), iface_addr);
7306 hostapd_drv_sta_deauth(hapd, sta->addr,
7307 WLAN_REASON_DEAUTH_LEAVING);
7308 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7313 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7317 struct wpa_supplicant *w;
7318 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
7320 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7322 /* Remove from any persistent group */
7323 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
7324 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7327 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7328 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
7331 /* Remove from any operating group */
7332 for (w = wpa_s->global->ifaces; w; w = w->next)
7333 wpas_p2p_remove_client_go(w, peer, iface_addr);
7337 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7339 struct wpa_supplicant *wpa_s = eloop_ctx;
7340 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7344 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7346 struct wpa_supplicant *wpa_s = eloop_ctx;
7348 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7349 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7353 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7354 struct wpa_ssid *ssid)
7356 struct wpa_supplicant *iface;
7358 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7359 if (!iface->current_ssid ||
7360 iface->current_ssid->frequency == freq ||
7361 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7362 !iface->current_ssid->p2p_group))
7365 /* Remove the connection with least priority */
7366 if (!wpas_is_p2p_prioritized(iface)) {
7367 /* STA connection has priority over existing
7368 * P2P connection, so remove the interface. */
7369 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7370 eloop_register_timeout(0, 0,
7371 wpas_p2p_group_freq_conflict,
7373 /* If connection in progress is P2P connection, do not
7374 * proceed for the connection. */
7380 /* P2P connection has priority, disable the STA network
7382 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7384 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7385 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7386 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7388 /* If P2P connection is in progress, continue
7401 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7403 struct wpa_ssid *ssid = wpa_s->current_ssid;
7405 if (ssid == NULL || !ssid->p2p_group)
7408 if (wpa_s->p2p_last_4way_hs_fail &&
7409 wpa_s->p2p_last_4way_hs_fail == ssid) {
7410 u8 go_dev_addr[ETH_ALEN];
7411 struct wpa_ssid *persistent;
7413 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7415 ssid->ssid_len) <= 0) {
7416 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7420 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7421 MACSTR, MAC2STR(go_dev_addr));
7422 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7425 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7426 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7429 wpa_msg_global(wpa_s->parent, MSG_INFO,
7430 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7433 wpa_s->p2p_last_4way_hs_fail = NULL;
7435 * Remove the group from a timeout to avoid issues with caller
7436 * continuing to use the interface if this is on a P2P group
7439 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7444 wpa_s->p2p_last_4way_hs_fail = ssid;
7449 #ifdef CONFIG_WPS_NFC
7451 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7459 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7463 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7464 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7471 wpabuf_put_be16(ret, wsc_len);
7473 wpabuf_put_buf(ret, wsc);
7474 wpabuf_put_be16(ret, wpabuf_len(p2p));
7475 wpabuf_put_buf(ret, p2p);
7479 wpa_hexdump_buf(MSG_DEBUG,
7480 "P2P: Generated NFC connection handover message", ret);
7484 tmp = ndef_build_p2p(ret);
7487 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7497 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7498 struct wpa_ssid **ssid, u8 *go_dev_addr)
7500 struct wpa_supplicant *iface;
7503 os_memset(go_dev_addr, 0, ETH_ALEN);
7506 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7507 if (iface->wpa_state < WPA_ASSOCIATING ||
7508 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7509 !iface->current_ssid->p2p_group ||
7510 iface->current_ssid->mode != WPAS_MODE_INFRA)
7513 *ssid = iface->current_ssid;
7515 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7516 return iface->assoc_freq;
7522 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7525 struct wpabuf *wsc, *p2p;
7526 struct wpa_ssid *ssid;
7527 u8 go_dev_addr[ETH_ALEN];
7528 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7530 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7531 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7535 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7536 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7537 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7538 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7542 if (cli_freq == 0) {
7543 wsc = wps_build_nfc_handover_req_p2p(
7544 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7547 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7548 go_dev_addr, ssid ? ssid->ssid : NULL,
7549 ssid ? ssid->ssid_len : 0);
7551 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7555 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7558 struct wpabuf *wsc, *p2p;
7559 struct wpa_ssid *ssid;
7560 u8 go_dev_addr[ETH_ALEN];
7561 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7563 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7566 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7567 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7568 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7571 if (cli_freq == 0) {
7572 wsc = wps_build_nfc_handover_sel_p2p(
7574 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7575 DEV_PW_NFC_CONNECTION_HANDOVER,
7576 wpa_s->conf->wps_nfc_dh_pubkey,
7577 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7580 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7581 go_dev_addr, ssid ? ssid->ssid : NULL,
7582 ssid ? ssid->ssid_len : 0);
7584 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7588 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7589 struct p2p_nfc_params *params)
7591 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7592 "connection handover (freq=%d)",
7595 if (params->go_freq && params->go_ssid_len) {
7596 wpa_s->p2p_wps_method = WPS_NFC;
7597 wpa_s->pending_join_wps_method = WPS_NFC;
7598 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7599 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7601 return wpas_p2p_join_start(wpa_s, params->go_freq,
7603 params->go_ssid_len);
7606 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7607 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7608 params->go_freq, -1, 0, 1, 1);
7612 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7613 struct p2p_nfc_params *params, int tag)
7615 int res, persistent;
7616 struct wpa_ssid *ssid;
7618 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7619 "connection handover");
7620 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7621 ssid = wpa_s->current_ssid;
7624 if (ssid->mode != WPAS_MODE_P2P_GO)
7626 if (wpa_s->ap_iface == NULL)
7630 if (wpa_s == NULL) {
7631 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7635 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7636 DEV_PW_NFC_CONNECTION_HANDOVER &&
7637 !wpa_s->parent->p2p_oob_dev_pw) {
7638 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7641 res = wpas_ap_wps_add_nfc_pw(
7642 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7643 wpa_s->parent->p2p_oob_dev_pw,
7644 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7645 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7650 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7654 if (!params->peer ||
7655 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7658 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7659 " to join", MAC2STR(params->peer->p2p_device_addr));
7661 wpa_s->global->p2p_invite_group = wpa_s;
7662 persistent = ssid->p2p_persistent_group &&
7663 wpas_p2p_get_persistent(wpa_s->parent,
7664 params->peer->p2p_device_addr,
7665 ssid->ssid, ssid->ssid_len);
7666 wpa_s->parent->pending_invite_ssid_id = -1;
7668 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7669 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7670 ssid->ssid, ssid->ssid_len, ssid->frequency,
7671 wpa_s->global->p2p_dev_addr, persistent, 0,
7672 wpa_s->parent->p2p_oob_dev_pw_id);
7676 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7677 struct p2p_nfc_params *params,
7680 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7681 "connection handover");
7682 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7683 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7684 forced_freq, -1, 0, 1, 1);
7688 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7689 struct p2p_nfc_params *params,
7694 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7695 "connection handover");
7696 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7697 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7698 forced_freq, -1, 0, 1, 1);
7702 res = wpas_p2p_listen(wpa_s, 60);
7704 p2p_unauthorize(wpa_s->global->p2p,
7705 params->peer->p2p_device_addr);
7712 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7713 const struct wpabuf *data,
7714 int sel, int tag, int forced_freq)
7716 const u8 *pos, *end;
7718 struct p2p_nfc_params params;
7721 os_memset(¶ms, 0, sizeof(params));
7724 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7726 pos = wpabuf_head(data);
7727 end = pos + wpabuf_len(data);
7729 if (end - pos < 2) {
7730 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7734 len = WPA_GET_BE16(pos);
7736 if (len > end - pos) {
7737 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7741 params.wsc_attr = pos;
7742 params.wsc_len = len;
7745 if (end - pos < 2) {
7746 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7750 len = WPA_GET_BE16(pos);
7752 if (len > end - pos) {
7753 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7757 params.p2p_attr = pos;
7758 params.p2p_len = len;
7761 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7762 params.wsc_attr, params.wsc_len);
7763 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7764 params.p2p_attr, params.p2p_len);
7766 wpa_hexdump(MSG_DEBUG,
7767 "P2P: Ignored extra data after P2P attributes",
7771 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
7775 if (params.next_step == NO_ACTION)
7778 if (params.next_step == BOTH_GO) {
7779 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7780 MAC2STR(params.peer->p2p_device_addr));
7784 if (params.next_step == PEER_CLIENT) {
7785 if (!is_zero_ether_addr(params.go_dev_addr)) {
7786 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7787 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7789 MAC2STR(params.peer->p2p_device_addr),
7791 MAC2STR(params.go_dev_addr),
7792 wpa_ssid_txt(params.go_ssid,
7793 params.go_ssid_len));
7795 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7796 "peer=" MACSTR " freq=%d",
7797 MAC2STR(params.peer->p2p_device_addr),
7803 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7804 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7805 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7809 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7810 wpa_s->p2p_oob_dev_pw = NULL;
7812 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7813 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7818 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7819 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7820 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7821 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7822 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7823 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7824 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7828 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7829 "peer OOB Device Password Id %u", id);
7832 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7833 "Device Password Id %u", id);
7834 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7835 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7836 params.oob_dev_pw_len -
7837 WPS_OOB_PUBKEY_HASH_LEN - 2);
7838 wpa_s->p2p_oob_dev_pw_id = id;
7839 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7840 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7841 params.oob_dev_pw_len -
7842 WPS_OOB_PUBKEY_HASH_LEN - 2);
7843 if (wpa_s->p2p_oob_dev_pw == NULL)
7846 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7847 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7848 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7851 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7852 "without Device Password");
7853 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7856 switch (params.next_step) {
7860 /* already covered above */
7863 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
7865 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
7867 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
7869 /* TODO: use own OOB Dev Pw */
7870 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
7877 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7878 const struct wpabuf *data, int forced_freq)
7880 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7883 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7887 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7888 const struct wpabuf *req,
7889 const struct wpabuf *sel, int forced_freq)
7894 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7897 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7899 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7900 wpabuf_head(req), wpabuf_len(req));
7901 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7902 wpabuf_head(sel), wpabuf_len(sel));
7904 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7905 tmp = ndef_parse_p2p(init ? sel : req);
7907 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7911 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7919 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7922 int go_intent = wpa_s->conf->p2p_go_intent;
7923 struct wpa_supplicant *iface;
7925 if (wpa_s->global->p2p == NULL)
7929 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7930 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7932 if (!iface->ap_iface)
7934 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7936 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7938 if (wpa_s->p2p_nfc_tag_enabled)
7939 wpas_p2p_remove_pending_group_interface(wpa_s);
7940 wpa_s->p2p_nfc_tag_enabled = 0;
7944 if (wpa_s->global->p2p_disabled)
7947 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7948 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7949 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7950 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7951 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7952 "to allow static handover cases");
7956 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7958 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7959 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7960 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7961 if (wpa_s->p2p_oob_dev_pw == NULL)
7963 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7965 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7966 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7968 * P2P Group Interface present and the command came on group
7969 * interface, so enable the token for the current interface.
7971 wpa_s->create_p2p_iface = 0;
7973 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7976 if (wpa_s->create_p2p_iface) {
7977 enum wpa_driver_if_type iftype;
7978 /* Prepare to add a new interface for the group */
7979 iftype = WPA_IF_P2P_GROUP;
7980 if (go_intent == 15)
7981 iftype = WPA_IF_P2P_GO;
7982 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7983 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7984 "interface for the group");
7988 if_addr = wpa_s->pending_interface_addr;
7990 if_addr = wpa_s->own_addr;
7992 wpa_s->p2p_nfc_tag_enabled = enabled;
7994 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7995 struct hostapd_data *hapd;
7996 if (iface->ap_iface == NULL)
7998 hapd = iface->ap_iface->bss[0];
7999 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8000 hapd->conf->wps_nfc_dh_pubkey =
8001 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8002 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8003 hapd->conf->wps_nfc_dh_privkey =
8004 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8005 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8006 hapd->conf->wps_nfc_dev_pw =
8007 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8008 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8010 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8011 wpa_dbg(iface, MSG_DEBUG,
8012 "P2P: Failed to enable NFC Tag for GO");
8015 p2p_set_authorized_oob_dev_pw_id(
8016 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8022 #endif /* CONFIG_WPS_NFC */
8025 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8026 struct wpa_used_freq_data *freqs,
8029 u8 curr_chan, cand, chan;
8032 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8033 for (i = 0, cand = 0; i < num; i++) {
8034 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8035 if (curr_chan == chan) {
8040 if (chan == 1 || chan == 6 || chan == 11)
8045 wpa_dbg(wpa_s, MSG_DEBUG,
8046 "P2P: Update Listen channel to %u based on operating channel",
8048 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8053 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8055 struct wpa_used_freq_data *freqs;
8056 unsigned int num = wpa_s->num_multichan_concurrent;
8058 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8062 * If possible, optimize the Listen channel to be a channel that is
8063 * already used by one of the other interfaces.
8065 if (!wpa_s->conf->p2p_optimize_listen_chan)
8068 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8071 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8075 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8077 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8082 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8084 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8085 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8086 "the management interface is being removed");
8087 wpas_p2p_deinit_global(wpa_s->global);
8092 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8094 if (wpa_s->ap_iface->bss)
8095 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8096 wpas_p2p_group_deinit(wpa_s);