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
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
54 #define P2P_GO_FREQ_CHANGE_TIME 5
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
60 #define P2P_GO_CSA_COUNT 7
61 #define P2P_GO_CSA_BLOCK_TX 0
63 #ifndef P2P_MAX_CLIENT_IDLE
65 * How many seconds to try to reconnect to the GO when connection in P2P client
68 #define P2P_MAX_CLIENT_IDLE 10
69 #endif /* P2P_MAX_CLIENT_IDLE */
71 #ifndef P2P_MAX_INITIAL_CONN_WAIT
73 * How many seconds to wait for initial 4-way handshake to get completed after
74 * WPS provisioning step or after the re-invocation of a persistent group on a
77 #define P2P_MAX_INITIAL_CONN_WAIT 10
78 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
80 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
87 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
90 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
96 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
99 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
105 #define P2P_RECONSIDER_GO_MOVE_DELAY 30
107 enum p2p_group_removal_reason {
108 P2P_GROUP_REMOVAL_UNKNOWN,
109 P2P_GROUP_REMOVAL_SILENT,
110 P2P_GROUP_REMOVAL_FORMATION_FAILED,
111 P2P_GROUP_REMOVAL_REQUESTED,
112 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
113 P2P_GROUP_REMOVAL_UNAVAILABLE,
114 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
115 P2P_GROUP_REMOVAL_PSK_FAILURE,
116 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
117 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
121 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
122 static struct wpa_supplicant *
123 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
125 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
126 const u8 *ssid, size_t ssid_len);
127 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
128 int *force_freq, int *pref_freq, int go,
129 unsigned int *pref_freq_list,
130 unsigned int *num_pref_freq);
131 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
132 const u8 *ssid, size_t ssid_len);
133 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
134 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
135 const u8 *dev_addr, enum p2p_wps_method wps_method,
136 int auto_join, int freq,
137 const u8 *ssid, size_t ssid_len);
138 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
139 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
140 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
141 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
142 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
144 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
145 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
147 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
148 static void wpas_stop_listen(void *ctx);
149 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
150 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
151 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
152 enum wpa_driver_if_type type);
153 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
154 int already_deleted);
155 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
156 struct wpa_used_freq_data *freqs,
158 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
159 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
161 wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
162 struct wpa_used_freq_data *freqs, unsigned int num,
163 enum wpas_p2p_channel_update_trig trig);
164 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
168 * Get the number of concurrent channels that the HW can operate, but that are
169 * currently not in use by any of the wpa_supplicant interfaces.
171 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
176 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
180 num = get_shared_radio_freqs(wpa_s, freqs,
181 wpa_s->num_multichan_concurrent);
184 unused = wpa_s->num_multichan_concurrent - num;
185 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
191 * Get the frequencies that are currently in use by one or more of the virtual
192 * interfaces, and that are also valid for P2P operation.
195 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
196 struct wpa_used_freq_data *p2p_freqs,
199 struct wpa_used_freq_data *freqs;
200 unsigned int num, i, j;
202 freqs = os_calloc(wpa_s->num_multichan_concurrent,
203 sizeof(struct wpa_used_freq_data));
207 num = get_shared_radio_freqs_data(wpa_s, freqs,
208 wpa_s->num_multichan_concurrent);
210 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
212 for (i = 0, j = 0; i < num && j < len; i++) {
213 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
214 p2p_freqs[j++] = freqs[i];
219 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
225 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
228 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
231 /* Use the wpa_s used to control the P2P Device operation */
232 wpa_s = wpa_s->global->p2p_init_wpa_s;
234 if (wpa_s->conf->p2p_ignore_shared_freq &&
235 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
236 wpas_p2p_num_unused_channels(wpa_s) > 0) {
237 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
241 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
245 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
246 struct wpa_scan_results *scan_res)
250 if (wpa_s->p2p_scan_work) {
251 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
252 wpa_s->p2p_scan_work = NULL;
253 radio_work_done(work);
256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
259 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
260 (int) scan_res->num);
262 for (i = 0; i < scan_res->num; i++) {
263 struct wpa_scan_res *bss = scan_res->res[i];
264 struct os_reltime time_tmp_age, entry_ts;
268 time_tmp_age.sec = bss->age / 1000;
269 time_tmp_age.usec = (bss->age % 1000) * 1000;
270 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
272 ies = (const u8 *) (bss + 1);
273 ies_len = bss->ie_len;
274 if (bss->beacon_ie_len > 0 &&
275 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
276 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
277 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
278 MACSTR, MAC2STR(bss->bssid));
280 ies_len = bss->beacon_ie_len;
284 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
285 bss->freq, &entry_ts, bss->level,
290 p2p_scan_res_handled(wpa_s->global->p2p);
294 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
296 struct wpa_supplicant *wpa_s = work->wpa_s;
297 struct wpa_driver_scan_params *params = work->ctx;
301 if (!work->started) {
302 wpa_scan_free_params(params);
306 wpa_s->p2p_scan_work = NULL;
310 if (wpa_s->clear_driver_scan_cache) {
311 wpa_printf(MSG_DEBUG,
312 "Request driver to clear scan cache due to local BSS flush");
313 params->only_new_results = 1;
315 ret = wpa_drv_scan(wpa_s, params);
317 wpa_s->curr_scan_cookie = params->scan_cookie;
318 wpa_scan_free_params(params);
321 radio_work_done(work);
322 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
326 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
327 os_get_reltime(&wpa_s->scan_trigger_time);
328 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
329 wpa_s->own_scan_requested = 1;
330 wpa_s->clear_driver_scan_cache = 0;
331 wpa_s->p2p_scan_work = work;
335 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
338 if (wpa_s->global->p2p_24ghz_social_channels &&
339 (freq == 2412 || freq == 2437 || freq == 2462)) {
341 * Search all social channels regardless of whether these have
342 * been disabled for P2P operating channel use to avoid missing
347 return p2p_supported_freq(wpa_s->global->p2p, freq);
351 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
352 unsigned int num_req_dev_types,
353 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
355 struct wpa_supplicant *wpa_s = ctx;
356 struct wpa_driver_scan_params *params = NULL;
357 struct wpabuf *wps_ie, *ies;
358 unsigned int num_channels = 0;
359 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
364 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
367 if (wpa_s->p2p_scan_work) {
368 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
372 params = os_zalloc(sizeof(*params));
376 /* P2P Wildcard SSID */
377 params->num_ssids = 1;
378 n = os_malloc(P2P_WILDCARD_SSID_LEN);
381 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
382 params->ssids[0].ssid = n;
383 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
385 wpa_s->wps->dev.p2p = 1;
386 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
387 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
388 num_req_dev_types, req_dev_types);
393 case P2P_SCAN_SOCIAL:
394 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
396 if (params->freqs == NULL)
398 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
399 if (wpas_p2p_search_social_channel(
400 wpa_s, social_channels_freq[i]))
401 params->freqs[num_channels++] =
402 social_channels_freq[i];
404 params->freqs[num_channels++] = 0;
408 case P2P_SCAN_SPECIFIC:
409 params->freqs = os_calloc(2, sizeof(int));
410 if (params->freqs == NULL)
412 params->freqs[0] = freq;
413 params->freqs[1] = 0;
415 case P2P_SCAN_SOCIAL_PLUS_ONE:
416 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
418 if (params->freqs == NULL)
420 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
421 if (wpas_p2p_search_social_channel(
422 wpa_s, social_channels_freq[i]))
423 params->freqs[num_channels++] =
424 social_channels_freq[i];
426 if (p2p_supported_freq(wpa_s->global->p2p, freq))
427 params->freqs[num_channels++] = freq;
428 params->freqs[num_channels++] = 0;
432 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
433 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
438 wpabuf_put_buf(ies, wps_ie);
441 bands = wpas_get_bands(wpa_s, params->freqs);
442 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
444 params->p2p_probe = 1;
445 n = os_malloc(wpabuf_len(ies));
450 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
451 params->extra_ies = n;
452 params->extra_ies_len = wpabuf_len(ies);
455 radio_remove_works(wpa_s, "p2p-scan", 0);
456 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
462 wpa_scan_free_params(params);
467 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
469 switch (p2p_group_interface) {
470 case P2P_GROUP_INTERFACE_PENDING:
471 return WPA_IF_P2P_GROUP;
472 case P2P_GROUP_INTERFACE_GO:
473 return WPA_IF_P2P_GO;
474 case P2P_GROUP_INTERFACE_CLIENT:
475 return WPA_IF_P2P_CLIENT;
478 return WPA_IF_P2P_GROUP;
482 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
484 size_t ssid_len, int *go)
488 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
489 for (s = wpa_s->conf->ssid; s; s = s->next) {
490 if (s->disabled != 0 || !s->p2p_group ||
491 s->ssid_len != ssid_len ||
492 os_memcmp(ssid, s->ssid, ssid_len) != 0)
494 if (s->mode == WPAS_MODE_P2P_GO &&
495 s != wpa_s->current_ssid)
498 *go = s->mode == WPAS_MODE_P2P_GO;
507 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
509 struct wpa_supplicant *wpa_s = eloop_ctx;
510 wpa_printf(MSG_DEBUG,
511 "P2P: Complete previously requested removal of %s",
513 wpas_p2p_disconnect(wpa_s);
517 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
518 struct wpa_supplicant *calling_wpa_s)
520 if (calling_wpa_s == wpa_s && wpa_s &&
521 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
523 * The calling wpa_s instance is going to be removed. Do that
524 * from an eloop callback to keep the instance available until
525 * the caller has returned. This my be needed, e.g., to provide
526 * control interface responses on the per-interface socket.
528 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
534 return wpas_p2p_disconnect(wpa_s);
538 /* Determine total number of clients in active groups where we are the GO */
539 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
541 unsigned int count = 0;
544 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
545 for (s = wpa_s->conf->ssid; s; s = s->next) {
546 wpa_printf(MSG_DEBUG,
547 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
548 wpa_s, s, s->disabled, s->p2p_group,
550 if (!s->disabled && s->p2p_group &&
551 s->mode == WPAS_MODE_P2P_GO) {
552 count += p2p_get_group_num_members(
562 static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
564 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
565 !wpa_s->current_ssid->disabled &&
566 wpa_s->current_ssid->p2p_group &&
567 wpa_s->current_ssid->p2p_persistent_group;
571 static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
573 return p2p_is_active_persistent_group(wpa_s) &&
574 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
578 /* Find an interface for a P2P group where we are the GO */
579 static struct wpa_supplicant *
580 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
582 struct wpa_supplicant *save = NULL;
587 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
588 if (!p2p_is_active_persistent_go(wpa_s))
591 /* Prefer a group with connected clients */
592 if (p2p_get_group_num_members(wpa_s->p2p_group))
597 /* No group with connected clients, so pick the one without (if any) */
602 static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
604 return p2p_is_active_persistent_group(wpa_s) &&
605 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
609 /* Find an interface for a P2P group where we are the P2P Client */
610 static struct wpa_supplicant *
611 wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
613 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
614 if (p2p_is_active_persistent_cli(wpa_s))
622 /* Find a persistent group where we are the GO */
623 static struct wpa_ssid *
624 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
628 for (s = wpa_s->conf->ssid; s; s = s->next) {
629 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
637 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
638 unsigned int *force_freq,
639 unsigned int *pref_freq)
641 struct wpa_supplicant *wpa_s = ctx;
643 u8 conncap = P2PS_SETUP_NONE;
644 unsigned int owned_members = 0;
645 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
646 struct wpa_ssid *persistent_go;
647 int p2p_no_group_iface;
648 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
650 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
657 size = P2P_MAX_PREF_CHANNELS;
658 if (force_freq && pref_freq &&
659 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
660 (int *) pref_freq, 0, pref_freq_list, &size))
661 wpas_p2p_set_own_freq_preference(wpa_s,
662 *force_freq ? *force_freq :
666 * For non-concurrent capable devices:
667 * If persistent_go, then no new.
668 * If GO, then no client.
669 * If client, then no GO.
671 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
673 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
674 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
675 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
676 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
678 wpa_printf(MSG_DEBUG,
679 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
680 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
682 /* If not concurrent, restrict our choices */
683 if (p2p_no_group_iface) {
684 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
687 return P2PS_SETUP_NONE;
690 if (role == P2PS_SETUP_CLIENT ||
691 incoming == P2PS_SETUP_GROUP_OWNER ||
692 p2p_client_limit_reached(go_wpa_s->p2p_group))
693 return P2PS_SETUP_NONE;
695 return P2PS_SETUP_GROUP_OWNER;
699 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
701 return P2PS_SETUP_GROUP_OWNER |
703 if (incoming == P2PS_SETUP_NEW) {
706 if (os_get_random(&r, sizeof(r)) < 0 ||
708 return P2PS_SETUP_CLIENT;
709 return P2PS_SETUP_GROUP_OWNER;
715 /* If a required role has been specified, handle it here */
716 if (role && role != P2PS_SETUP_NEW) {
718 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
719 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
721 * Peer has an active GO, so if the role allows it and
722 * we do not have any active roles, become client.
724 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
726 return P2PS_SETUP_CLIENT;
730 case P2PS_SETUP_NONE:
735 case P2PS_SETUP_GROUP_OWNER:
737 * Must be a complimentary role - cannot be a client to
738 * more than one peer.
740 if (incoming == role || cli_wpa_s)
741 return P2PS_SETUP_NONE;
743 return P2PS_SETUP_CLIENT;
745 case P2PS_SETUP_CLIENT:
746 /* Must be a complimentary role */
747 if (incoming != role) {
748 conncap = P2PS_SETUP_GROUP_OWNER;
754 return P2PS_SETUP_NONE;
759 * For now, we only will support ownership of one group, and being a
760 * client of one group. Therefore, if we have either an existing GO
761 * group, or an existing client group, we will not do a new GO
762 * negotiation, but rather try to re-use the existing groups.
765 case P2PS_SETUP_NONE:
768 conncap = P2PS_SETUP_GROUP_OWNER;
769 else if (!owned_members)
770 conncap = P2PS_SETUP_NEW;
771 else if (incoming == P2PS_SETUP_NONE)
772 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
774 conncap = P2PS_SETUP_CLIENT;
777 case P2PS_SETUP_CLIENT:
778 conncap = P2PS_SETUP_GROUP_OWNER;
781 case P2PS_SETUP_GROUP_OWNER:
783 conncap = P2PS_SETUP_CLIENT;
786 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
787 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
789 conncap = P2PS_SETUP_GROUP_OWNER;
793 if (os_get_random(&r, sizeof(r)) < 0 ||
795 conncap = P2PS_SETUP_CLIENT;
797 conncap = P2PS_SETUP_GROUP_OWNER;
802 return P2PS_SETUP_NONE;
806 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
807 (!incoming && (conncap & P2PS_SETUP_NEW))) {
808 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
809 conncap &= ~P2PS_SETUP_GROUP_OWNER;
811 s = wpas_p2p_get_persistent_go(wpa_s);
812 if (!s && !go_wpa_s && p2p_no_group_iface) {
813 p2p_set_intended_addr(wpa_s->global->p2p,
815 wpa_s->parent->own_addr :
817 } else if (!s && !go_wpa_s) {
818 if (wpas_p2p_add_group_interface(wpa_s,
819 WPA_IF_P2P_GROUP) < 0) {
820 wpa_printf(MSG_ERROR,
821 "P2P: Failed to allocate a new interface for the group");
822 return P2PS_SETUP_NONE;
824 wpa_s->global->pending_group_iface_for_p2ps = 1;
825 p2p_set_intended_addr(wpa_s->global->p2p,
826 wpa_s->pending_interface_addr);
834 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
835 enum p2p_group_removal_reason removal_reason)
837 struct wpa_ssid *ssid;
841 ssid = wpa_s->current_ssid;
844 * The current SSID was not known, but there may still be a
845 * pending P2P group interface waiting for provisioning or a
846 * P2P group that is trying to reconnect.
848 ssid = wpa_s->conf->ssid;
850 if (ssid->p2p_group && ssid->disabled != 2)
855 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
857 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
862 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
864 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
865 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
866 wpa_s->reassociate = 0;
867 wpa_s->disconnected = 1;
872 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
873 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
875 if (os_strcmp(gtype, "client") == 0) {
876 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
877 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
879 wpa_printf(MSG_DEBUG,
880 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
881 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
882 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
887 if (wpa_s->cross_connect_in_use) {
888 wpa_s->cross_connect_in_use = 0;
889 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
890 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
891 wpa_s->ifname, wpa_s->cross_connect_uplink);
893 switch (removal_reason) {
894 case P2P_GROUP_REMOVAL_REQUESTED:
895 reason = " reason=REQUESTED";
897 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
898 reason = " reason=FORMATION_FAILED";
900 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
901 reason = " reason=IDLE";
903 case P2P_GROUP_REMOVAL_UNAVAILABLE:
904 reason = " reason=UNAVAILABLE";
906 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
907 reason = " reason=GO_ENDING_SESSION";
909 case P2P_GROUP_REMOVAL_PSK_FAILURE:
910 reason = " reason=PSK_FAILURE";
912 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
913 reason = " reason=FREQ_CONFLICT";
919 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
920 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
921 P2P_EVENT_GROUP_REMOVED "%s %s%s",
922 wpa_s->ifname, gtype, reason);
925 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
926 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
927 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
928 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
929 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
930 wpa_s->p2pdev, NULL) > 0) {
931 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
933 wpa_s->p2p_in_provisioning = 0;
934 wpas_p2p_group_formation_failed(wpa_s, 1);
937 wpa_s->p2p_in_invitation = 0;
938 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
939 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
942 * Make sure wait for the first client does not remain active after the
943 * group has been removed.
945 wpa_s->global->p2p_go_wait_client.sec = 0;
947 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
948 struct wpa_global *global;
950 enum wpa_driver_if_type type;
951 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
953 global = wpa_s->global;
954 ifname = os_strdup(wpa_s->ifname);
955 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
956 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
957 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
958 wpa_s = global->ifaces;
960 wpa_drv_if_remove(wpa_s, type, ifname);
966 * The primary interface was used for P2P group operations, so
967 * need to reset its p2pdev.
969 wpa_s->p2pdev = wpa_s->parent;
971 if (!wpa_s->p2p_go_group_formation_completed) {
972 wpa_s->global->p2p_group_formation = NULL;
973 wpa_s->p2p_in_provisioning = 0;
976 wpa_s->show_group_started = 0;
977 os_free(wpa_s->go_params);
978 wpa_s->go_params = NULL;
980 os_free(wpa_s->p2p_group_common_freqs);
981 wpa_s->p2p_group_common_freqs = NULL;
982 wpa_s->p2p_group_common_freqs_num = 0;
983 wpa_s->p2p_go_do_acs = 0;
985 wpa_s->waiting_presence_resp = 0;
987 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
988 if (ssid && (ssid->p2p_group ||
989 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
990 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
992 if (ssid == wpa_s->current_ssid) {
993 wpa_sm_set_config(wpa_s->wpa, NULL);
994 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
995 wpa_s->current_ssid = NULL;
998 * Networks objects created during any P2P activities are not
999 * exposed out as they might/will confuse certain non-P2P aware
1000 * applications since these network objects won't behave like
1003 * Likewise, we don't send out network removed signals for such
1006 wpa_config_remove_network(wpa_s->conf, id);
1007 wpa_supplicant_clear_status(wpa_s);
1008 wpa_supplicant_cancel_sched_scan(wpa_s);
1010 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1013 if (wpa_s->ap_iface)
1014 wpa_supplicant_ap_deinit(wpa_s);
1016 wpa_drv_deinit_p2p_cli(wpa_s);
1018 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1024 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1026 const u8 *ssid, size_t ssid_len)
1028 struct wpa_bss *bss;
1034 if (wpa_s->go_params)
1035 bssid = wpa_s->go_params->peer_interface_addr;
1037 bssid = wpa_s->bssid;
1039 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
1040 if (bss == NULL && wpa_s->go_params &&
1041 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1042 bss = wpa_bss_get_p2p_dev_addr(
1043 wpa_s, wpa_s->go_params->peer_device_addr);
1045 u8 iface_addr[ETH_ALEN];
1046 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1048 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1051 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1052 "group is persistent - BSS " MACSTR " not found",
1057 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1059 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1060 P2P_IE_VENDOR_TYPE);
1062 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1063 "group is persistent - BSS " MACSTR
1064 " did not include P2P IE", MAC2STR(bssid));
1065 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1066 (u8 *) (bss + 1), bss->ie_len);
1067 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1068 ((u8 *) bss + 1) + bss->ie_len,
1069 bss->beacon_ie_len);
1073 group_capab = p2p_get_group_capab(p2p);
1074 addr = p2p_get_go_dev_addr(p2p);
1075 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1076 "group_capab=0x%x", group_capab);
1078 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1079 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1082 os_memset(go_dev_addr, 0, ETH_ALEN);
1085 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1086 "go_dev_addr=" MACSTR,
1087 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1089 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
1093 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1094 struct wpa_ssid *ssid,
1095 const u8 *go_dev_addr)
1100 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1101 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1102 for (s = wpa_s->conf->ssid; s; s = s->next) {
1103 if (s->disabled == 2 &&
1104 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1105 s->ssid_len == ssid->ssid_len &&
1106 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1111 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1113 if (ssid->passphrase && !s->passphrase)
1115 else if (ssid->passphrase && s->passphrase &&
1116 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1119 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1122 s = wpa_config_add_network(wpa_s->conf);
1127 * Instead of network_added we emit persistent_group_added
1128 * notification. Also to keep the defense checks in
1129 * persistent_group obj registration method, we set the
1130 * relevant flags in s to designate it as a persistent group.
1133 s->p2p_persistent_group = 1;
1134 wpas_notify_persistent_group_added(wpa_s, s);
1135 wpa_config_set_network_defaults(s);
1139 s->p2p_persistent_group = 1;
1142 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1143 s->mode = ssid->mode;
1144 s->auth_alg = WPA_AUTH_ALG_OPEN;
1145 s->key_mgmt = WPA_KEY_MGMT_PSK;
1146 s->proto = WPA_PROTO_RSN;
1147 s->pbss = ssid->pbss;
1148 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
1150 if (ssid->passphrase) {
1151 os_free(s->passphrase);
1152 s->passphrase = os_strdup(ssid->passphrase);
1154 if (ssid->psk_set) {
1156 os_memcpy(s->psk, ssid->psk, 32);
1158 if (s->passphrase && !s->psk_set)
1159 wpa_config_update_psk(s);
1160 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1162 s->ssid = os_malloc(ssid->ssid_len);
1165 s->ssid_len = ssid->ssid_len;
1166 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1168 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1169 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1170 wpa_s->global->add_psk = NULL;
1174 if (changed && wpa_s->conf->update_config &&
1175 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1176 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1183 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1186 struct wpa_ssid *ssid, *s;
1190 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
1192 ssid = wpa_s->current_ssid;
1193 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1194 !ssid->p2p_persistent_group)
1197 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
1198 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1201 if (s->ssid_len == ssid->ssid_len &&
1202 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1209 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
1210 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
1214 if (i == s->num_p2p_clients - 1)
1215 return; /* already the most recent entry */
1217 /* move the entry to mark it most recent */
1218 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1219 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1220 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
1221 os_memcpy(s->p2p_client_list +
1222 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1224 os_memset(s->p2p_client_list +
1225 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1231 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1232 n = os_realloc_array(s->p2p_client_list,
1233 s->num_p2p_clients + 1, 2 * ETH_ALEN);
1236 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1238 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1240 s->p2p_client_list = n;
1241 s->num_p2p_clients++;
1242 } else if (!found && s->p2p_client_list) {
1243 /* Not enough room for an additional entry - drop the oldest
1245 os_memmove(s->p2p_client_list,
1246 s->p2p_client_list + 2 * ETH_ALEN,
1247 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1248 os_memcpy(s->p2p_client_list +
1249 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1251 os_memset(s->p2p_client_list +
1252 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1256 if (p2p_wpa_s->conf->update_config &&
1257 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
1258 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1262 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1263 int go, struct wpa_ssid *ssid, int freq,
1264 const u8 *psk, const char *passphrase,
1265 const u8 *go_dev_addr, int persistent,
1268 const char *ssid_txt;
1272 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1277 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1281 if (passphrase && passphrase[0] == '\0')
1285 * Include PSK/passphrase only in the control interface message and
1286 * leave it out from the debug log entry.
1288 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
1289 P2P_EVENT_GROUP_STARTED
1290 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1292 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1293 psk ? " psk=" : "", psk_txt,
1294 passphrase ? " passphrase=\"" : "",
1295 passphrase ? passphrase : "",
1296 passphrase ? "\"" : "",
1297 MAC2STR(go_dev_addr),
1298 persistent ? " [PERSISTENT]" : "", extra);
1299 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1300 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1301 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1302 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1307 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1308 int success, int already_deleted)
1310 struct wpa_ssid *ssid;
1313 u8 go_dev_addr[ETH_ALEN];
1316 * This callback is likely called for the main interface. Update wpa_s
1317 * to use the group interface if a new interface was created for the
1320 if (wpa_s->global->p2p_group_formation)
1321 wpa_s = wpa_s->global->p2p_group_formation;
1322 if (wpa_s->p2p_go_group_formation_completed) {
1323 wpa_s->global->p2p_group_formation = NULL;
1324 wpa_s->p2p_in_provisioning = 0;
1325 } else if (wpa_s->p2p_in_provisioning && !success) {
1326 wpa_msg(wpa_s, MSG_DEBUG,
1327 "P2P: Stop provisioning state due to failure");
1328 wpa_s->p2p_in_provisioning = 0;
1330 wpa_s->p2p_in_invitation = 0;
1331 wpa_s->group_formation_reported = 1;
1334 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
1335 P2P_EVENT_GROUP_FORMATION_FAILURE);
1336 wpas_notify_p2p_group_formation_failure(wpa_s, "");
1337 if (already_deleted)
1339 wpas_p2p_group_delete(wpa_s,
1340 P2P_GROUP_REMOVAL_FORMATION_FAILED);
1344 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
1345 P2P_EVENT_GROUP_FORMATION_SUCCESS);
1347 ssid = wpa_s->current_ssid;
1348 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1349 ssid->mode = WPAS_MODE_P2P_GO;
1350 p2p_group_notif_formation_done(wpa_s->p2p_group);
1351 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1356 client = ssid->mode == WPAS_MODE_INFRA;
1357 if (ssid->mode == WPAS_MODE_P2P_GO) {
1358 persistent = ssid->p2p_persistent_group;
1359 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1362 persistent = wpas_p2p_persistent_group(wpa_s,
1367 client = wpa_s->p2p_group_interface ==
1368 P2P_GROUP_INTERFACE_CLIENT;
1369 os_memset(go_dev_addr, 0, ETH_ALEN);
1372 wpa_s->show_group_started = 0;
1375 * Indicate event only after successfully completed 4-way
1376 * handshake, i.e., when the interface is ready for data
1379 wpa_s->show_group_started = 1;
1381 wpas_p2p_group_started(wpa_s, 1, ssid,
1382 ssid ? ssid->frequency : 0,
1383 ssid && ssid->passphrase == NULL &&
1384 ssid->psk_set ? ssid->psk : NULL,
1385 ssid ? ssid->passphrase : NULL,
1386 go_dev_addr, persistent, "");
1387 wpas_p2p_cross_connect_setup(wpa_s);
1388 wpas_p2p_set_group_idle_timeout(wpa_s);
1392 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1395 os_free(wpa_s->global->add_psk);
1396 wpa_s->global->add_psk = NULL;
1400 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
1401 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1406 struct send_action_work {
1412 unsigned int wait_time;
1417 static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1419 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1421 wpa_printf(MSG_DEBUG,
1422 "P2P: Free Action frame radio work @%p (freq=%u dst="
1423 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1424 wpa_s->p2p_send_action_work, awork->freq,
1425 MAC2STR(awork->dst), MAC2STR(awork->src),
1426 MAC2STR(awork->bssid), awork->wait_time);
1427 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1428 awork->buf, awork->len);
1430 wpa_s->p2p_send_action_work->ctx = NULL;
1431 radio_work_done(wpa_s->p2p_send_action_work);
1432 wpa_s->p2p_send_action_work = NULL;
1436 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1439 struct wpa_supplicant *wpa_s = eloop_ctx;
1441 if (!wpa_s->p2p_send_action_work)
1444 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1445 wpas_p2p_free_send_action_work(wpa_s);
1449 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1451 if (wpa_s->p2p_send_action_work) {
1452 struct send_action_work *awork;
1454 awork = wpa_s->p2p_send_action_work->ctx;
1455 wpa_printf(MSG_DEBUG,
1456 "P2P: Clear Action TX work @%p (wait_time=%u)",
1457 wpa_s->p2p_send_action_work, awork->wait_time);
1458 if (awork->wait_time == 0) {
1459 wpas_p2p_free_send_action_work(wpa_s);
1462 * In theory, this should not be needed, but number of
1463 * places in the P2P code is still using non-zero wait
1464 * time for the last Action frame in the sequence and
1465 * some of these do not call send_action_done().
1467 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1469 eloop_register_timeout(
1470 0, awork->wait_time * 1000,
1471 wpas_p2p_send_action_work_timeout,
1478 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1480 const u8 *dst, const u8 *src,
1482 const u8 *data, size_t data_len,
1483 enum offchannel_send_action_result
1486 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1488 wpas_p2p_action_tx_clear(wpa_s);
1490 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1494 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1495 res = P2P_SEND_ACTION_SUCCESS;
1497 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1498 res = P2P_SEND_ACTION_NO_ACK;
1500 case OFFCHANNEL_SEND_ACTION_FAILED:
1501 res = P2P_SEND_ACTION_FAILED;
1505 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1507 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1508 wpa_s->pending_pd_before_join &&
1509 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1510 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1511 wpa_s->p2p_fallback_to_go_neg) {
1512 wpa_s->pending_pd_before_join = 0;
1513 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1514 "during p2p_connect-auto");
1515 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
1516 P2P_EVENT_FALLBACK_TO_GO_NEG
1517 "reason=no-ACK-to-PD-Req");
1518 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1524 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1526 struct wpa_supplicant *wpa_s = work->wpa_s;
1527 struct send_action_work *awork = work->ctx;
1530 if (work->started) {
1531 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1533 wpa_s->p2p_send_action_work = NULL;
1534 offchannel_send_action_done(wpa_s);
1540 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1541 awork->bssid, awork->buf, awork->len,
1543 wpas_p2p_send_action_tx_status, 1) < 0) {
1545 radio_work_done(work);
1548 wpa_s->p2p_send_action_work = work;
1552 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1553 unsigned int freq, const u8 *dst,
1554 const u8 *src, const u8 *bssid, const u8 *buf,
1555 size_t len, unsigned int wait_time)
1557 struct send_action_work *awork;
1559 if (radio_work_pending(wpa_s, "p2p-send-action")) {
1560 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1564 awork = os_zalloc(sizeof(*awork) + len);
1569 os_memcpy(awork->dst, dst, ETH_ALEN);
1570 os_memcpy(awork->src, src, ETH_ALEN);
1571 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1573 awork->wait_time = wait_time;
1574 os_memcpy(awork->buf, buf, len);
1576 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
1577 wpas_send_action_cb, awork) < 0) {
1586 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1587 const u8 *src, const u8 *bssid, const u8 *buf,
1588 size_t len, unsigned int wait_time, int *scheduled)
1590 struct wpa_supplicant *wpa_s = ctx;
1591 int listen_freq = -1, send_freq = -1;
1595 if (wpa_s->p2p_listen_work)
1596 listen_freq = wpa_s->p2p_listen_work->freq;
1597 if (wpa_s->p2p_send_action_work)
1598 send_freq = wpa_s->p2p_send_action_work->freq;
1599 if (listen_freq != (int) freq && send_freq != (int) freq) {
1602 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1603 listen_freq, send_freq, freq);
1604 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1606 if (res == 0 && scheduled)
1611 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1612 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1614 wpas_p2p_send_action_tx_status, 1);
1618 static void wpas_send_action_done(void *ctx)
1620 struct wpa_supplicant *wpa_s = ctx;
1622 if (wpa_s->p2p_send_action_work) {
1623 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1625 os_free(wpa_s->p2p_send_action_work->ctx);
1626 radio_work_done(wpa_s->p2p_send_action_work);
1627 wpa_s->p2p_send_action_work = NULL;
1630 offchannel_send_action_done(wpa_s);
1634 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1635 struct p2p_go_neg_results *params)
1637 if (wpa_s->go_params == NULL) {
1638 wpa_s->go_params = os_malloc(sizeof(*params));
1639 if (wpa_s->go_params == NULL)
1642 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1647 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1648 struct p2p_go_neg_results *res)
1650 wpa_s->group_formation_reported = 0;
1651 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1652 " dev_addr " MACSTR " wps_method %d",
1653 MAC2STR(res->peer_interface_addr),
1654 MAC2STR(res->peer_device_addr), res->wps_method);
1655 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1656 res->ssid, res->ssid_len);
1657 wpa_supplicant_ap_deinit(wpa_s);
1658 wpas_copy_go_neg_results(wpa_s, res);
1659 if (res->wps_method == WPS_PBC) {
1660 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
1661 #ifdef CONFIG_WPS_NFC
1662 } else if (res->wps_method == WPS_NFC) {
1663 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1664 res->peer_interface_addr,
1665 wpa_s->p2pdev->p2p_oob_dev_pw,
1666 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1667 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
1668 DEV_PW_NFC_CONNECTION_HANDOVER ?
1669 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
1672 #endif /* CONFIG_WPS_NFC */
1674 u16 dev_pw_id = DEV_PW_DEFAULT;
1675 if (wpa_s->p2p_wps_method == WPS_P2PS)
1676 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1677 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1678 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1679 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1680 wpa_s->p2p_pin, 1, dev_pw_id);
1685 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1686 struct wpa_ssid *ssid)
1688 struct wpa_ssid *persistent;
1689 struct psk_list_entry *psk;
1690 struct hostapd_data *hapd;
1692 if (!wpa_s->ap_iface)
1695 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
1697 if (persistent == NULL)
1700 hapd = wpa_s->ap_iface->bss[0];
1702 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1704 struct hostapd_wpa_psk *hpsk;
1706 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1708 MAC2STR(psk->addr), psk->p2p);
1709 hpsk = os_zalloc(sizeof(*hpsk));
1712 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1714 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1716 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1717 hpsk->next = hapd->conf->ssid.wpa_psk;
1718 hapd->conf->ssid.wpa_psk = hpsk;
1723 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1725 char buf[20 + P2P_MAX_CHANNELS * 6];
1731 end = pos + sizeof(buf);
1732 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1733 res = os_snprintf(pos, end - pos, " %d",
1734 wpa_s->p2p_group_common_freqs[i]);
1735 if (os_snprintf_error(end - pos, res))
1741 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
1745 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1746 struct p2p_go_neg_results *params)
1748 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1750 wpa_s->p2p_group_common_freqs_num = 0;
1751 os_free(wpa_s->p2p_group_common_freqs);
1752 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1753 if (!wpa_s->p2p_group_common_freqs)
1756 for (i = 0; i < len; i++) {
1757 if (!wpa_s->go_params->freq_list[i])
1759 wpa_s->p2p_group_common_freqs[i] =
1760 wpa_s->go_params->freq_list[i];
1762 wpa_s->p2p_group_common_freqs_num = i;
1766 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1768 #ifndef CONFIG_NO_CONFIG_WRITE
1769 if (wpa_s->p2pdev->conf->update_config &&
1770 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
1771 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1772 #endif /* CONFIG_NO_CONFIG_WRITE */
1776 static void p2p_go_configured(void *ctx, void *data)
1778 struct wpa_supplicant *wpa_s = ctx;
1779 struct p2p_go_neg_results *params = data;
1780 struct wpa_ssid *ssid;
1782 wpa_s->ap_configured_cb = NULL;
1783 wpa_s->ap_configured_cb_ctx = NULL;
1784 wpa_s->ap_configured_cb_data = NULL;
1785 if (!wpa_s->go_params) {
1786 wpa_printf(MSG_ERROR,
1787 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1791 p2p_go_save_group_common_freqs(wpa_s, params);
1792 p2p_go_dump_common_freqs(wpa_s);
1794 ssid = wpa_s->current_ssid;
1795 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1796 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1797 if (wpa_s->global->p2p_group_formation == wpa_s)
1798 wpa_s->global->p2p_group_formation = NULL;
1799 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1800 params->passphrase[0] == '\0' ?
1803 wpa_s->global->p2p_dev_addr,
1804 params->persistent_group, "");
1805 wpa_s->group_formation_reported = 1;
1807 if (wpa_s->p2pdev->p2ps_method_config_any) {
1808 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
1809 wpa_dbg(wpa_s, MSG_DEBUG,
1810 "P2PS: Setting default PIN for ANY");
1811 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1812 "12345670", NULL, 0,
1815 wpa_dbg(wpa_s, MSG_DEBUG,
1816 "P2PS: Setting default PIN for " MACSTR,
1817 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
1818 wpa_supplicant_ap_wps_pin(
1819 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
1820 "12345670", NULL, 0, 0);
1822 wpa_s->p2pdev->p2ps_method_config_any = 0;
1825 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1826 if (params->persistent_group) {
1827 wpas_p2p_store_persistent_group(
1828 wpa_s->p2pdev, ssid,
1829 wpa_s->global->p2p_dev_addr);
1830 wpas_p2p_add_psk_list(wpa_s, ssid);
1833 wpas_notify_p2p_group_started(wpa_s, ssid,
1834 params->persistent_group, 0,
1836 wpas_p2p_cross_connect_setup(wpa_s);
1837 wpas_p2p_set_group_idle_timeout(wpa_s);
1839 if (wpa_s->p2p_first_connection_timeout) {
1840 wpa_dbg(wpa_s, MSG_DEBUG,
1841 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1842 wpa_s->p2p_first_connection_timeout);
1843 wpa_s->p2p_go_group_formation_completed = 0;
1844 wpa_s->global->p2p_group_formation = wpa_s;
1845 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1846 wpa_s->p2pdev, NULL);
1847 eloop_register_timeout(
1848 wpa_s->p2p_first_connection_timeout, 0,
1849 wpas_p2p_group_formation_timeout,
1850 wpa_s->p2pdev, NULL);
1856 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1857 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1858 params->peer_interface_addr)) {
1859 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1863 if (params->wps_method == WPS_PBC) {
1864 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1865 params->peer_device_addr);
1866 #ifdef CONFIG_WPS_NFC
1867 } else if (params->wps_method == WPS_NFC) {
1868 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
1869 DEV_PW_NFC_CONNECTION_HANDOVER &&
1870 !wpa_s->p2pdev->p2p_oob_dev_pw) {
1871 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1874 wpas_ap_wps_add_nfc_pw(
1875 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1876 wpa_s->p2pdev->p2p_oob_dev_pw,
1877 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1878 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
1879 #endif /* CONFIG_WPS_NFC */
1880 } else if (wpa_s->p2p_pin[0])
1881 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1882 wpa_s->p2p_pin, NULL, 0, 0);
1883 os_free(wpa_s->go_params);
1884 wpa_s->go_params = NULL;
1888 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1889 struct p2p_go_neg_results *params,
1890 int group_formation)
1892 struct wpa_ssid *ssid;
1894 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1895 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1896 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1901 ssid = wpa_config_add_network(wpa_s->conf);
1903 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1907 wpa_s->show_group_started = 0;
1908 wpa_s->p2p_go_group_formation_completed = 0;
1909 wpa_s->group_formation_reported = 0;
1910 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1912 wpa_config_set_network_defaults(ssid);
1913 ssid->temporary = 1;
1914 ssid->p2p_group = 1;
1915 ssid->p2p_persistent_group = !!params->persistent_group;
1916 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1918 ssid->frequency = params->freq;
1919 ssid->ht40 = params->ht40;
1920 ssid->vht = params->vht;
1921 ssid->max_oper_chwidth = params->max_oper_chwidth;
1922 ssid->vht_center_freq2 = params->vht_center_freq2;
1923 ssid->he = params->he;
1924 ssid->ssid = os_zalloc(params->ssid_len + 1);
1926 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1927 ssid->ssid_len = params->ssid_len;
1929 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1930 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1931 ssid->proto = WPA_PROTO_RSN;
1932 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1933 ssid->group_cipher = WPA_CIPHER_CCMP;
1934 if (params->freq > 56160) {
1936 * Enable GCMP instead of CCMP as pairwise_cipher and
1937 * group_cipher in 60 GHz.
1939 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1940 ssid->group_cipher = WPA_CIPHER_GCMP;
1941 /* P2P GO in 60 GHz is always a PCP (PBSS) */
1944 if (os_strlen(params->passphrase) > 0) {
1945 ssid->passphrase = os_strdup(params->passphrase);
1946 if (ssid->passphrase == NULL) {
1947 wpa_msg_global(wpa_s, MSG_ERROR,
1948 "P2P: Failed to copy passphrase for GO");
1949 wpa_config_remove_network(wpa_s->conf, ssid->id);
1953 ssid->passphrase = NULL;
1954 ssid->psk_set = params->psk_set;
1956 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1957 else if (ssid->passphrase)
1958 wpa_config_update_psk(ssid);
1959 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
1961 wpa_s->ap_configured_cb = p2p_go_configured;
1962 wpa_s->ap_configured_cb_ctx = wpa_s;
1963 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1964 wpa_s->scan_req = NORMAL_SCAN_REQ;
1965 wpa_s->connect_without_scan = ssid;
1966 wpa_s->reassociate = 1;
1967 wpa_s->disconnected = 0;
1968 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1970 wpa_supplicant_req_scan(wpa_s, 0, 0);
1974 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1975 const struct wpa_supplicant *src)
1977 struct wpa_config *d;
1978 const struct wpa_config *s;
1985 if (s->n && !d->n) \
1986 d->n = os_strdup(s->n); \
1997 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1998 os_memcpy(d->sec_device_type, s->sec_device_type,
1999 sizeof(d->sec_device_type));
2000 d->num_sec_device_types = s->num_sec_device_types;
2002 d->p2p_group_idle = s->p2p_group_idle;
2003 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
2004 d->p2p_intra_bss = s->p2p_intra_bss;
2005 d->persistent_reconnect = s->persistent_reconnect;
2006 d->max_num_sta = s->max_num_sta;
2007 d->pbc_in_m1 = s->pbc_in_m1;
2008 d->ignore_old_scan_res = s->ignore_old_scan_res;
2009 d->beacon_int = s->beacon_int;
2010 d->dtim_period = s->dtim_period;
2011 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
2012 d->disassoc_low_ack = s->disassoc_low_ack;
2013 d->disable_scan_offload = s->disable_scan_offload;
2014 d->passive_scan = s->passive_scan;
2016 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2017 !d->wps_nfc_pw_from_config) {
2018 wpabuf_free(d->wps_nfc_dh_privkey);
2019 wpabuf_free(d->wps_nfc_dh_pubkey);
2020 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2021 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2023 d->p2p_cli_probe = s->p2p_cli_probe;
2024 d->go_interworking = s->go_interworking;
2025 d->go_access_network_type = s->go_access_network_type;
2026 d->go_internet = s->go_internet;
2027 d->go_venue_group = s->go_venue_group;
2028 d->go_venue_type = s->go_venue_type;
2032 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2033 char *ifname, size_t len)
2035 char *ifname_ptr = wpa_s->ifname;
2037 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2038 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2039 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2042 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2043 if (os_strlen(ifname) >= IFNAMSIZ &&
2044 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
2047 /* Try to avoid going over the IFNAMSIZ length limit */
2048 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2049 if (os_snprintf_error(len, res) && len)
2050 ifname[len - 1] = '\0';
2055 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2056 enum wpa_driver_if_type type)
2058 char ifname[120], force_ifname[120];
2060 if (wpa_s->pending_interface_name[0]) {
2061 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2062 "- skip creation of a new one");
2063 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2064 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2065 "unknown?! ifname='%s'",
2066 wpa_s->pending_interface_name);
2072 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
2073 force_ifname[0] = '\0';
2075 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2077 wpa_s->p2p_group_idx++;
2079 wpa_s->pending_interface_type = type;
2080 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2081 wpa_s->pending_interface_addr, NULL) < 0) {
2082 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2087 if (wpa_s->conf->p2p_interface_random_mac_addr) {
2088 random_mac_addr(wpa_s->pending_interface_addr);
2089 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2091 MAC2STR(wpa_s->pending_interface_addr));
2094 if (force_ifname[0]) {
2095 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2097 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2098 sizeof(wpa_s->pending_interface_name));
2100 os_strlcpy(wpa_s->pending_interface_name, ifname,
2101 sizeof(wpa_s->pending_interface_name));
2102 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2103 MACSTR, wpa_s->pending_interface_name,
2104 MAC2STR(wpa_s->pending_interface_addr));
2110 static void wpas_p2p_remove_pending_group_interface(
2111 struct wpa_supplicant *wpa_s)
2113 if (!wpa_s->pending_interface_name[0] ||
2114 is_zero_ether_addr(wpa_s->pending_interface_addr))
2115 return; /* No pending virtual interface */
2117 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2118 wpa_s->pending_interface_name);
2119 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2120 wpa_s->pending_interface_name);
2121 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2122 wpa_s->pending_interface_name[0] = '\0';
2123 wpa_s->global->pending_group_iface_for_p2ps = 0;
2127 static struct wpa_supplicant *
2128 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2130 struct wpa_interface iface;
2131 struct wpa_supplicant *group_wpa_s;
2133 if (!wpa_s->pending_interface_name[0]) {
2134 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2135 if (!wpas_p2p_create_iface(wpa_s))
2138 * Something has forced us to remove the pending interface; try
2139 * to create a new one and hope for the best that we will get
2140 * the same local address.
2142 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2143 WPA_IF_P2P_CLIENT) < 0)
2147 os_memset(&iface, 0, sizeof(iface));
2148 iface.ifname = wpa_s->pending_interface_name;
2149 iface.driver = wpa_s->driver->name;
2150 if (wpa_s->conf->ctrl_interface == NULL &&
2151 wpa_s->parent != wpa_s &&
2153 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2154 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2156 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
2157 iface.driver_param = wpa_s->conf->driver_param;
2158 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
2159 if (group_wpa_s == NULL) {
2160 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2161 "wpa_supplicant interface");
2164 wpa_s->pending_interface_name[0] = '\0';
2165 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2166 P2P_GROUP_INTERFACE_CLIENT;
2167 wpa_s->global->p2p_group_formation = group_wpa_s;
2168 wpa_s->global->pending_group_iface_for_p2ps = 0;
2170 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2172 if (wpa_s->conf->p2p_interface_random_mac_addr) {
2173 if (wpa_drv_set_mac_addr(group_wpa_s,
2174 wpa_s->pending_interface_addr) < 0) {
2175 wpa_msg(group_wpa_s, MSG_INFO,
2176 "Failed to set random MAC address");
2177 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2182 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2183 wpa_msg(group_wpa_s, MSG_INFO,
2184 "Could not update MAC address information");
2185 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2190 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2192 MAC2STR(wpa_s->pending_interface_addr));
2199 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2202 struct wpa_supplicant *wpa_s = eloop_ctx;
2203 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2204 wpas_p2p_group_formation_failed(wpa_s, 0);
2208 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2209 int already_deleted)
2211 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2212 wpa_s->p2pdev, NULL);
2213 if (wpa_s->global->p2p)
2214 p2p_group_formation_failed(wpa_s->global->p2p);
2215 wpas_group_formation_completed(wpa_s, 0, already_deleted);
2219 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2221 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2222 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2223 wpa_s->p2pdev, NULL);
2224 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2225 wpa_s->p2pdev, NULL);
2226 wpa_s->global->p2p_fail_on_wps_complete = 0;
2230 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2232 if (wpa_s->global->p2p_group_formation != wpa_s)
2234 /* Speed up group formation timeout since this cannot succeed */
2235 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2236 wpa_s->p2pdev, NULL);
2237 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2238 wpa_s->p2pdev, NULL);
2242 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2244 struct wpa_supplicant *wpa_s = ctx;
2245 struct wpa_supplicant *group_wpa_s;
2247 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2248 wpa_drv_cancel_remain_on_channel(wpa_s);
2249 wpa_s->off_channel_freq = 0;
2250 wpa_s->roc_waiting_drv_freq = 0;
2254 wpa_msg_global(wpa_s, MSG_INFO,
2255 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2257 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2258 wpas_p2p_remove_pending_group_interface(wpa_s);
2262 if (!res->role_go) {
2263 /* Inform driver of the operating channel of GO. */
2264 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2267 if (wpa_s->p2p_go_ht40)
2269 if (wpa_s->p2p_go_vht)
2271 if (wpa_s->p2p_go_he)
2273 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2274 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
2276 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2277 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2279 res->role_go ? "GO" : "client", res->freq, res->ht40,
2280 MAC2STR(res->peer_device_addr),
2281 MAC2STR(res->peer_interface_addr),
2282 p2p_wps_method_text(res->wps_method));
2283 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2285 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2286 struct wpa_ssid *ssid;
2287 ssid = wpa_config_get_network(wpa_s->conf,
2288 wpa_s->p2p_persistent_id);
2289 if (ssid && ssid->disabled == 2 &&
2290 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2291 size_t len = os_strlen(ssid->passphrase);
2292 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2293 "on requested persistent group");
2294 os_memcpy(res->passphrase, ssid->passphrase, len);
2295 res->passphrase[len] = '\0';
2299 if (wpa_s->create_p2p_iface) {
2301 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2302 if (group_wpa_s == NULL) {
2303 wpas_p2p_remove_pending_group_interface(wpa_s);
2304 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2306 wpas_p2p_group_formation_failed(wpa_s, 1);
2309 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2310 wpa_s->pending_interface_name[0] = '\0';
2312 group_wpa_s = wpa_s->parent;
2313 wpa_s->global->p2p_group_formation = group_wpa_s;
2314 if (group_wpa_s != wpa_s)
2315 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2318 group_wpa_s->p2p_in_provisioning = 1;
2319 group_wpa_s->p2pdev = wpa_s;
2320 if (group_wpa_s != wpa_s) {
2321 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2322 sizeof(group_wpa_s->p2p_pin));
2323 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2326 wpas_start_wps_go(group_wpa_s, res, 1);
2328 os_get_reltime(&group_wpa_s->scan_min_time);
2329 wpas_start_wps_enrollee(group_wpa_s, res);
2332 wpa_s->p2p_long_listen = 0;
2333 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2335 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2336 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2337 (res->peer_config_timeout % 100) * 10000,
2338 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2342 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2345 struct wpa_supplicant *wpa_s = ctx;
2346 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2347 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2348 dev_passwd_id, go_intent);
2350 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
2354 static void wpas_dev_found(void *ctx, const u8 *addr,
2355 const struct p2p_peer_info *info,
2358 #ifndef CONFIG_NO_STDOUT_DEBUG
2359 struct wpa_supplicant *wpa_s = ctx;
2360 char devtype[WPS_DEV_TYPE_BUFSIZE];
2361 char *wfd_dev_info_hex = NULL;
2363 #ifdef CONFIG_WIFI_DISPLAY
2364 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2365 WFD_SUBELEM_DEVICE_INFO);
2366 #endif /* CONFIG_WIFI_DISPLAY */
2368 if (info->p2ps_instance) {
2370 const u8 *buf = wpabuf_head(info->p2ps_instance);
2371 size_t len = wpabuf_len(info->p2ps_instance);
2378 if (len < 4 + 2 + 1)
2380 id = WPA_GET_LE32(buf);
2382 methods = WPA_GET_BE16(buf);
2385 if (str_len > len - 4 - 2 - 1)
2387 os_memcpy(str, buf, str_len);
2388 str[str_len] = '\0';
2390 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2392 wpa_msg_global(wpa_s, MSG_INFO,
2393 P2P_EVENT_DEVICE_FOUND MACSTR
2394 " p2p_dev_addr=" MACSTR
2395 " pri_dev_type=%s name='%s'"
2396 " config_methods=0x%x"
2399 " adv_id=%x asp_svc=%s%s",
2401 MAC2STR(info->p2p_device_addr),
2402 wps_dev_type_bin2str(
2404 devtype, sizeof(devtype)),
2405 info->device_name, methods,
2406 info->dev_capab, info->group_capab,
2408 info->vendor_elems ?
2409 " vendor_elems=1" : "");
2414 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2415 " p2p_dev_addr=" MACSTR
2416 " pri_dev_type=%s name='%s' config_methods=0x%x "
2417 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2418 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2419 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2421 info->device_name, info->config_methods,
2422 info->dev_capab, info->group_capab,
2423 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2424 wfd_dev_info_hex ? wfd_dev_info_hex : "",
2425 info->vendor_elems ? " vendor_elems=1" : "",
2429 os_free(wfd_dev_info_hex);
2430 #endif /* CONFIG_NO_STDOUT_DEBUG */
2432 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2436 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2438 struct wpa_supplicant *wpa_s = ctx;
2440 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2441 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2443 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2447 static void wpas_find_stopped(void *ctx)
2449 struct wpa_supplicant *wpa_s = ctx;
2451 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2452 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2454 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2455 wpas_notify_p2p_find_stopped(wpa_s);
2459 struct wpas_p2p_listen_work {
2461 unsigned int duration;
2462 struct wpabuf *probe_resp_ie;
2466 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2470 wpabuf_free(lwork->probe_resp_ie);
2475 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2477 struct wpas_p2p_listen_work *lwork;
2479 if (!wpa_s->p2p_listen_work)
2482 lwork = wpa_s->p2p_listen_work->ctx;
2483 wpas_p2p_listen_work_free(lwork);
2484 radio_work_done(wpa_s->p2p_listen_work);
2485 wpa_s->p2p_listen_work = NULL;
2489 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2491 struct wpa_supplicant *wpa_s = work->wpa_s;
2492 struct wpas_p2p_listen_work *lwork = work->ctx;
2493 unsigned int duration;
2496 if (work->started) {
2497 wpa_s->p2p_listen_work = NULL;
2498 wpas_stop_listen(wpa_s);
2500 wpas_p2p_listen_work_free(lwork);
2504 wpa_s->p2p_listen_work = work;
2506 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2508 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2509 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2510 "report received Probe Request frames");
2511 wpas_p2p_listen_work_done(wpa_s);
2515 wpa_s->pending_listen_freq = lwork->freq;
2516 wpa_s->pending_listen_duration = lwork->duration;
2518 duration = lwork->duration;
2519 #ifdef CONFIG_TESTING_OPTIONS
2520 if (wpa_s->extra_roc_dur) {
2521 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2522 duration, duration + wpa_s->extra_roc_dur);
2523 duration += wpa_s->extra_roc_dur;
2525 #endif /* CONFIG_TESTING_OPTIONS */
2527 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2528 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2529 "to remain on channel (%u MHz) for Listen "
2530 "state", lwork->freq);
2531 wpas_p2p_listen_work_done(wpa_s);
2532 wpa_s->pending_listen_freq = 0;
2535 wpa_s->off_channel_freq = 0;
2536 wpa_s->roc_waiting_drv_freq = lwork->freq;
2540 static int wpas_start_listen(void *ctx, unsigned int freq,
2541 unsigned int duration,
2542 const struct wpabuf *probe_resp_ie)
2544 struct wpa_supplicant *wpa_s = ctx;
2545 struct wpas_p2p_listen_work *lwork;
2547 if (wpa_s->p2p_listen_work) {
2548 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2552 lwork = os_zalloc(sizeof(*lwork));
2556 lwork->duration = duration;
2557 if (probe_resp_ie) {
2558 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2559 if (lwork->probe_resp_ie == NULL) {
2560 wpas_p2p_listen_work_free(lwork);
2565 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2567 wpas_p2p_listen_work_free(lwork);
2575 static void wpas_stop_listen(void *ctx)
2577 struct wpa_supplicant *wpa_s = ctx;
2578 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2579 wpa_drv_cancel_remain_on_channel(wpa_s);
2580 wpa_s->off_channel_freq = 0;
2581 wpa_s->roc_waiting_drv_freq = 0;
2583 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2586 * Don't cancel Probe Request RX reporting for a connected P2P Client
2587 * handling Probe Request frames.
2589 if (!wpa_s->p2p_cli_probe)
2590 wpa_drv_probe_req_report(wpa_s, 0);
2592 wpas_p2p_listen_work_done(wpa_s);
2596 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2599 struct wpa_supplicant *wpa_s = ctx;
2600 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2605 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2606 const u8 *peer, const char *params,
2607 unsigned int generated_pin)
2609 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2610 " %08d%s", MAC2STR(peer), generated_pin, params);
2614 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2615 const u8 *peer, const char *params)
2617 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2618 "%s", MAC2STR(peer), params);
2622 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2623 const u8 *dev_addr, const u8 *pri_dev_type,
2624 const char *dev_name, u16 supp_config_methods,
2625 u8 dev_capab, u8 group_capab, const u8 *group_id,
2626 size_t group_id_len)
2628 struct wpa_supplicant *wpa_s = ctx;
2629 char devtype[WPS_DEV_TYPE_BUFSIZE];
2631 u8 empty_dev_type[8];
2632 unsigned int generated_pin = 0;
2633 struct wpa_supplicant *group = NULL;
2637 for (group = wpa_s->global->ifaces; group; group = group->next)
2639 struct wpa_ssid *s = group->current_ssid;
2641 s->mode == WPAS_MODE_P2P_GO &&
2642 group_id_len - ETH_ALEN == s->ssid_len &&
2643 os_memcmp(group_id + ETH_ALEN, s->ssid,
2649 if (pri_dev_type == NULL) {
2650 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2651 pri_dev_type = empty_dev_type;
2653 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2654 " pri_dev_type=%s name='%s' config_methods=0x%x "
2655 "dev_capab=0x%x group_capab=0x%x%s%s",
2657 wps_dev_type_bin2str(pri_dev_type, devtype,
2659 dev_name, supp_config_methods, dev_capab, group_capab,
2660 group ? " group=" : "",
2661 group ? group->ifname : "");
2662 if (os_snprintf_error(sizeof(params), res))
2663 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
2664 params[sizeof(params) - 1] = '\0';
2666 if (config_methods & WPS_CONFIG_DISPLAY) {
2667 if (wps_generate_pin(&generated_pin) < 0) {
2668 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2669 wpas_notify_p2p_provision_discovery(
2670 wpa_s, peer, 0 /* response */,
2671 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2674 wpas_prov_disc_local_display(wpa_s, peer, params,
2676 } else if (config_methods & WPS_CONFIG_KEYPAD)
2677 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2678 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2679 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2680 MACSTR "%s", MAC2STR(peer), params);
2682 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2683 P2P_PROV_DISC_SUCCESS,
2684 config_methods, generated_pin);
2688 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2690 struct wpa_supplicant *wpa_s = ctx;
2691 unsigned int generated_pin = 0;
2694 if (wpa_s->pending_pd_before_join &&
2695 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2696 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2697 wpa_s->pending_pd_before_join = 0;
2698 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2699 "join-existing-group operation");
2700 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2704 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2705 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2708 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2709 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2710 if (os_snprintf_error(sizeof(params), res))
2711 params[sizeof(params) - 1] = '\0';
2715 if (config_methods & WPS_CONFIG_DISPLAY)
2716 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2717 else if (config_methods & WPS_CONFIG_KEYPAD) {
2718 if (wps_generate_pin(&generated_pin) < 0) {
2719 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2720 wpas_notify_p2p_provision_discovery(
2721 wpa_s, peer, 0 /* response */,
2722 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2725 wpas_prov_disc_local_display(wpa_s, peer, params,
2727 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2728 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2729 MACSTR "%s", MAC2STR(peer), params);
2731 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2732 P2P_PROV_DISC_SUCCESS,
2733 config_methods, generated_pin);
2737 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2738 enum p2p_prov_disc_status status,
2739 u32 adv_id, const u8 *adv_mac,
2740 const char *deferred_session_resp)
2742 struct wpa_supplicant *wpa_s = ctx;
2744 if (wpa_s->p2p_fallback_to_go_neg) {
2745 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2746 "failed - fall back to GO Negotiation");
2747 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
2748 P2P_EVENT_FALLBACK_TO_GO_NEG
2749 "reason=PD-failed");
2750 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2754 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2755 wpa_s->pending_pd_before_join = 0;
2756 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2757 "join-existing-group operation (no ACK for PD "
2759 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2763 if (adv_id && adv_mac && deferred_session_resp) {
2764 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2765 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2766 " deferred_session_resp='%s'",
2767 MAC2STR(peer), status, adv_id,
2768 deferred_session_resp);
2769 } else if (adv_id && adv_mac) {
2770 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2771 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2772 MAC2STR(peer), status, adv_id);
2774 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2775 " p2p_dev_addr=" MACSTR " status=%d",
2776 MAC2STR(peer), status);
2779 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2784 static int freq_included(struct wpa_supplicant *wpa_s,
2785 const struct p2p_channels *channels,
2788 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2789 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2795 static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2797 unsigned int num = P2P_MAX_CHANNELS;
2801 p2p_go_dump_common_freqs(wpa_s);
2802 common_freqs = os_calloc(num, sizeof(int));
2806 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2808 wpa_dbg(wpa_s, MSG_DEBUG,
2809 "P2P: Failed to get group common freqs");
2810 os_free(common_freqs);
2814 os_free(wpa_s->p2p_group_common_freqs);
2815 wpa_s->p2p_group_common_freqs = common_freqs;
2816 wpa_s->p2p_group_common_freqs_num = num;
2817 p2p_go_dump_common_freqs(wpa_s);
2822 * Check if the given frequency is one of the possible operating frequencies
2823 * set after the completion of the GO Negotiation.
2825 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2829 p2p_go_dump_common_freqs(wpa_s);
2831 /* assume no restrictions */
2832 if (!wpa_s->p2p_group_common_freqs_num)
2835 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2836 if (wpa_s->p2p_group_common_freqs[i] == freq)
2843 static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2844 struct sta_info *sta, void *ctx)
2846 int *ecsa_support = ctx;
2848 *ecsa_support &= sta->ecsa_supported;
2854 /* Check if all the peers support eCSA */
2855 static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2857 int ecsa_support = 1;
2859 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2862 return ecsa_support;
2867 * Pick the best frequency to use from all the currently used frequencies.
2869 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2870 struct wpa_used_freq_data *freqs,
2875 /* find a candidate freq that is supported by P2P */
2876 for (c = 0; c < num; c++)
2877 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2883 /* once we have a candidate, try to find a 'better' one */
2884 for (i = c + 1; i < num; i++) {
2885 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2889 * 1. Infrastructure station interfaces have higher preference.
2890 * 2. P2P Clients have higher preference.
2893 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2898 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2901 return freqs[c].freq;
2905 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2906 const u8 *go_dev_addr, const u8 *ssid,
2907 size_t ssid_len, int *go, u8 *group_bssid,
2908 int *force_freq, int persistent_group,
2909 const struct p2p_channels *channels,
2912 struct wpa_supplicant *wpa_s = ctx;
2914 struct wpa_used_freq_data *freqs;
2915 struct wpa_supplicant *grp;
2918 if (!persistent_group) {
2919 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2920 " to join an active group (SSID: %s)",
2921 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2922 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2923 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2925 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2926 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2927 "authorized invitation");
2931 #ifdef CONFIG_WPS_NFC
2932 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2933 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
2934 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2935 wpa_s->p2p_wps_method = WPS_NFC;
2936 wpa_s->pending_join_wps_method = WPS_NFC;
2937 os_memcpy(wpa_s->pending_join_dev_addr,
2938 go_dev_addr, ETH_ALEN);
2939 os_memcpy(wpa_s->pending_join_iface_addr,
2943 #endif /* CONFIG_WPS_NFC */
2946 * Do not accept the invitation automatically; notify user and
2949 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2952 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2954 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2955 "running persistent group");
2957 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2961 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2962 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2963 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2964 "invitation to re-invoke a persistent group");
2965 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2966 } else if (!wpa_s->conf->persistent_reconnect)
2967 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2969 for (s = wpa_s->conf->ssid; s; s = s->next) {
2970 if (s->disabled == 2 &&
2971 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2972 s->ssid_len == ssid_len &&
2973 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2978 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2979 " requested reinvocation of an unknown group",
2981 return P2P_SC_FAIL_UNKNOWN_GROUP;
2984 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2986 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2987 wpa_printf(MSG_DEBUG, "P2P: The only available "
2988 "interface is already in use - reject "
2990 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2992 if (wpa_s->p2p_mgmt)
2993 os_memcpy(group_bssid, wpa_s->parent->own_addr,
2996 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2997 } else if (s->mode == WPAS_MODE_P2P_GO) {
2999 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3001 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3002 "interface address for the group");
3003 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3005 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3010 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3013 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3014 sizeof(struct wpa_used_freq_data));
3016 int num_channels = wpa_s->num_multichan_concurrent;
3017 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3018 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3022 /* Get one of the frequencies currently in use */
3023 if (best_freq > 0) {
3024 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3025 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3027 if (wpa_s->num_multichan_concurrent < 2 ||
3028 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3029 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3030 *force_freq = best_freq;
3034 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3035 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3037 /* We are the client */
3038 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3039 "running a GO but we are capable of MCC, "
3040 "figure out the best channel to use");
3042 } else if (!freq_included(wpa_s, channels, *force_freq)) {
3043 /* We are the GO, and *force_freq is not in the
3045 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3046 "in intersection but we are capable of MCC, "
3047 "figure out the best channel to use",
3053 return P2P_SC_SUCCESS;
3057 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3058 const u8 *ssid, size_t ssid_len,
3059 const u8 *go_dev_addr, u8 status,
3062 struct wpa_supplicant *wpa_s = ctx;
3065 for (s = wpa_s->conf->ssid; s; s = s->next) {
3066 if (s->disabled == 2 &&
3067 s->ssid_len == ssid_len &&
3068 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3072 if (status == P2P_SC_SUCCESS) {
3073 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3074 " was accepted; op_freq=%d MHz, SSID=%s",
3075 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3077 int go = s->mode == WPAS_MODE_P2P_GO;
3079 wpa_msg_global(wpa_s, MSG_INFO,
3080 P2P_EVENT_INVITATION_ACCEPTED
3082 " persistent=%d freq=%d",
3083 MAC2STR(sa), s->id, op_freq);
3085 wpa_msg_global(wpa_s, MSG_INFO,
3086 P2P_EVENT_INVITATION_ACCEPTED
3089 MAC2STR(sa), s->id);
3091 wpas_p2p_group_add_persistent(
3092 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, 0, NULL,
3093 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3096 wpa_s->user_initiated_pd = 0;
3097 wpa_msg_global(wpa_s, MSG_INFO,
3098 P2P_EVENT_INVITATION_ACCEPTED
3099 "sa=" MACSTR " go_dev_addr=" MACSTR
3100 " bssid=" MACSTR " unknown-network",
3101 MAC2STR(sa), MAC2STR(go_dev_addr),
3103 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3104 wpa_s->p2p_wps_method, 0, op_freq,
3110 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3111 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3112 " was rejected (status %u)", MAC2STR(sa), status);
3118 wpa_msg_global(wpa_s, MSG_INFO,
3119 P2P_EVENT_INVITATION_RECEIVED
3120 "sa=" MACSTR " go_dev_addr=" MACSTR
3121 " bssid=" MACSTR " unknown-network",
3122 MAC2STR(sa), MAC2STR(go_dev_addr),
3125 wpa_msg_global(wpa_s, MSG_INFO,
3126 P2P_EVENT_INVITATION_RECEIVED
3127 "sa=" MACSTR " go_dev_addr=" MACSTR
3129 MAC2STR(sa), MAC2STR(go_dev_addr));
3131 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3136 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3137 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3138 "sa=" MACSTR " persistent=%d freq=%d",
3139 MAC2STR(sa), s->id, op_freq);
3141 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3142 "sa=" MACSTR " persistent=%d",
3143 MAC2STR(sa), s->id);
3145 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3150 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3151 struct wpa_ssid *ssid,
3152 const u8 *peer, int inv)
3155 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
3160 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3161 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
3165 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
3166 if (ssid->mode != WPAS_MODE_P2P_GO &&
3167 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3168 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3169 "due to invitation result", ssid->id);
3170 wpas_notify_network_removed(wpa_s, ssid);
3171 wpa_config_remove_network(wpa_s->conf, ssid->id);
3174 return; /* Peer not found in client list */
3177 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3178 "group %d client list%s",
3179 MAC2STR(peer), ssid->id,
3180 inv ? " due to invitation result" : "");
3181 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3182 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3183 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3184 ssid->num_p2p_clients--;
3185 if (p2p_wpa_s->conf->update_config &&
3186 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
3187 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3191 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3194 struct wpa_ssid *ssid;
3196 wpa_s = wpa_s->global->p2p_invite_group;
3198 return; /* No known invitation group */
3199 ssid = wpa_s->current_ssid;
3200 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3201 !ssid->p2p_persistent_group)
3202 return; /* Not operating as a GO in persistent group */
3203 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
3204 ssid->ssid, ssid->ssid_len);
3205 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3209 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3210 const struct p2p_channels *channels,
3211 const u8 *peer, int neg_freq,
3214 struct wpa_supplicant *wpa_s = ctx;
3215 struct wpa_ssid *ssid;
3219 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3220 "status=%d " MACSTR,
3221 status, MAC2STR(bssid));
3223 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3224 "status=%d ", status);
3226 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3228 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3229 status, MAC2STR(peer));
3230 if (wpa_s->pending_invite_ssid_id == -1) {
3231 struct wpa_supplicant *group_if =
3232 wpa_s->global->p2p_invite_group;
3234 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3235 wpas_remove_persistent_client(wpa_s, peer);
3238 * Invitation to an active group. If this is successful and we
3239 * are the GO, set the client wait to postpone some concurrent
3240 * operations and to allow provisioning and connection to happen
3243 if (status == P2P_SC_SUCCESS &&
3244 group_if && group_if->current_ssid &&
3245 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3246 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3247 #ifdef CONFIG_TESTING_OPTIONS
3248 if (group_if->p2p_go_csa_on_inv) {
3249 wpa_printf(MSG_DEBUG,
3250 "Testing: force P2P GO CSA after invitation");
3251 eloop_cancel_timeout(
3252 wpas_p2p_reconsider_moving_go,
3254 eloop_register_timeout(
3256 wpas_p2p_reconsider_moving_go,
3259 #endif /* CONFIG_TESTING_OPTIONS */
3264 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3265 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3266 "invitation exchange to indicate readiness for "
3270 if (status != P2P_SC_SUCCESS) {
3271 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3272 ssid = wpa_config_get_network(
3273 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3274 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3276 wpas_p2p_remove_pending_group_interface(wpa_s);
3280 ssid = wpa_config_get_network(wpa_s->conf,
3281 wpa_s->pending_invite_ssid_id);
3283 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3284 "data matching with invitation");
3289 * The peer could have missed our ctrl::ack frame for Invitation
3290 * Response and continue retransmitting the frame. To reduce the
3291 * likelihood of the peer not getting successful TX status for the
3292 * Invitation Response frame, wait a short time here before starting
3293 * the persistent group so that we will remain on the current channel to
3294 * acknowledge any possible retransmission from the peer.
3296 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3297 "starting persistent group");
3300 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3301 freq_included(wpa_s, channels, neg_freq))
3303 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3304 freq_included(wpa_s, channels, peer_oper_freq))
3305 freq = peer_oper_freq;
3309 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3310 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3311 wpas_p2p_group_add_persistent(wpa_s, ssid,
3312 ssid->mode == WPAS_MODE_P2P_GO,
3313 wpa_s->p2p_persistent_go_freq,
3315 wpa_s->p2p_go_vht_center_freq2,
3316 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3317 wpa_s->p2p_go_max_oper_chwidth,
3320 ssid->mode == WPAS_MODE_P2P_GO ?
3321 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3326 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3329 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3331 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3335 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3337 reg->channel[reg->channels] = chan;
3342 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3343 struct p2p_channels *chan,
3344 struct p2p_channels *cli_chan)
3348 wpa_s->global->p2p_24ghz_social_channels = 1;
3350 os_memset(cli_chan, 0, sizeof(*cli_chan));
3352 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3355 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3356 chan->reg_class[cla].reg_class = 81;
3357 chan->reg_class[cla].channels = 0;
3358 for (i = 0; i < 11; i++) {
3359 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3360 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3362 if (chan->reg_class[cla].channels)
3365 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3368 /* Operating class 115 - 5 GHz, channels 36-48 */
3369 chan->reg_class[cla].reg_class = 115;
3370 chan->reg_class[cla].channels = 0;
3371 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3372 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3373 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3374 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3375 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3376 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3377 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3378 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3379 if (chan->reg_class[cla].channels)
3382 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3385 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3386 chan->reg_class[cla].reg_class = 124;
3387 chan->reg_class[cla].channels = 0;
3388 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3389 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3390 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3391 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3392 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3393 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3394 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3395 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3396 if (chan->reg_class[cla].channels)
3399 chan->reg_classes = cla;
3404 static int has_channel(struct wpa_global *global,
3405 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3410 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3412 if (wpas_p2p_disallowed_freq(global, freq))
3415 for (i = 0; i < mode->num_channels; i++) {
3416 if (mode->channels[i].chan == chan) {
3418 *flags = mode->channels[i].flag;
3419 if (mode->channels[i].flag &
3420 (HOSTAPD_CHAN_DISABLED |
3421 HOSTAPD_CHAN_RADAR))
3423 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3433 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3434 struct hostapd_hw_modes *mode,
3437 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3440 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3443 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3445 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3446 * so the center channel is 6 channels away from the start/end.
3448 if (channel >= center_channels[i] - 6 &&
3449 channel <= center_channels[i] + 6)
3450 return center_channels[i];
3456 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3457 struct hostapd_hw_modes *mode,
3462 enum chan_allowed res, ret = ALLOWED;
3464 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3467 if (center_chan >= 58 && center_chan <= 138)
3468 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3470 /* check all the channels are available */
3471 for (i = 0; i < 4; i++) {
3472 int adj_chan = center_chan - 6 + i * 4;
3474 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3475 if (res == NOT_ALLOWED)
3480 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3482 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3484 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3486 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3494 static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3495 struct hostapd_hw_modes *mode,
3498 u8 center_channels[] = { 50, 114 };
3501 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3504 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3506 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3507 * so the center channel is 14 channels away from the start/end.
3509 if (channel >= center_channels[i] - 14 &&
3510 channel <= center_channels[i] + 14)
3511 return center_channels[i];
3517 static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3518 struct hostapd_hw_modes *mode,
3523 enum chan_allowed res, ret = ALLOWED;
3525 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3528 /* VHT 160 MHz uses DFS channels in most countries. */
3530 /* Check all the channels are available */
3531 for (i = 0; i < 8; i++) {
3532 int adj_chan = center_chan - 14 + i * 4;
3534 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3535 if (res == NOT_ALLOWED)
3541 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3543 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3545 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3547 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3549 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3551 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3553 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3555 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3563 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3564 struct hostapd_hw_modes *mode,
3568 enum chan_allowed res, res2;
3570 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3571 if (bw == BW40MINUS) {
3572 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3574 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3575 } else if (bw == BW40PLUS) {
3576 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3578 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3579 } else if (bw == BW80) {
3580 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3581 } else if (bw == BW160) {
3582 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
3585 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3587 if (res == NO_IR || res2 == NO_IR)
3593 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3594 struct p2p_channels *chan,
3595 struct p2p_channels *cli_chan)
3597 struct hostapd_hw_modes *mode;
3598 int cla, op, cli_cla;
3600 if (wpa_s->hw.modes == NULL) {
3601 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3602 "of all supported channels; assume dualband "
3604 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3609 for (op = 0; global_op_class[op].op_class; op++) {
3610 const struct oper_class_map *o = &global_op_class[op];
3612 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3614 if (o->p2p == NO_P2P_SUPP)
3617 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3620 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3621 wpa_s->global->p2p_24ghz_social_channels = 1;
3622 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3623 enum chan_allowed res;
3624 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3625 if (res == ALLOWED) {
3627 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3629 reg = &chan->reg_class[cla];
3631 reg->reg_class = o->op_class;
3633 reg->channel[reg->channels] = ch;
3635 } else if (res == NO_IR &&
3636 wpa_s->conf->p2p_add_cli_chan) {
3637 if (cli_reg == NULL) {
3638 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3640 cli_reg = &cli_chan->reg_class[cli_cla];
3642 cli_reg->reg_class = o->op_class;
3644 cli_reg->channel[cli_reg->channels] = ch;
3645 cli_reg->channels++;
3649 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3650 reg->channel, reg->channels);
3653 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3654 cli_reg->channel, cli_reg->channels);
3658 chan->reg_classes = cla;
3659 cli_chan->reg_classes = cli_cla;
3665 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3666 struct hostapd_hw_modes *mode, u8 channel)
3669 enum chan_allowed ret;
3671 for (op = 0; global_op_class[op].op_class; op++) {
3672 const struct oper_class_map *o = &global_op_class[op];
3675 if (o->p2p == NO_P2P_SUPP)
3678 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3679 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3680 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3683 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3685 return (o->bw == BW40MINUS) ? -1 : 1;
3692 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3693 struct hostapd_hw_modes *mode, u8 channel)
3695 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3698 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3702 int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3703 struct hostapd_hw_modes *mode, u8 channel)
3705 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3707 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3711 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3714 struct wpa_supplicant *wpa_s = ctx;
3716 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3717 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3723 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3727 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3728 const u8 *ssid, size_t ssid_len)
3730 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3731 struct wpa_ssid *s = wpa_s->current_ssid;
3734 if (s->mode != WPAS_MODE_P2P_GO &&
3735 s->mode != WPAS_MODE_AP &&
3736 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3738 if (s->ssid_len != ssid_len ||
3739 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3749 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3750 const u8 *peer_dev_addr)
3752 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3753 struct wpa_ssid *ssid = wpa_s->current_ssid;
3754 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
3756 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3764 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3766 struct wpa_supplicant *wpa_s = ctx;
3768 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3772 static int wpas_is_concurrent_session_active(void *ctx)
3774 struct wpa_supplicant *wpa_s = ctx;
3775 struct wpa_supplicant *ifs;
3777 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3780 if (ifs->wpa_state > WPA_ASSOCIATED)
3787 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3789 struct wpa_supplicant *wpa_s = ctx;
3790 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3794 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3795 const char *conf_p2p_dev)
3797 struct wpa_interface iface;
3798 struct wpa_supplicant *p2pdev_wpa_s;
3800 char force_name[100];
3803 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3805 if (os_snprintf_error(sizeof(ifname), ret))
3807 force_name[0] = '\0';
3808 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3809 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3810 force_name, wpa_s->pending_interface_addr, NULL);
3812 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3815 os_strlcpy(wpa_s->pending_interface_name, ifname,
3816 sizeof(wpa_s->pending_interface_name));
3818 os_memset(&iface, 0, sizeof(iface));
3820 iface.ifname = wpa_s->pending_interface_name;
3821 iface.driver = wpa_s->driver->name;
3822 iface.driver_param = wpa_s->conf->driver_param;
3825 * If a P2P Device configuration file was given, use it as the interface
3826 * configuration file (instead of using parent's configuration file.
3829 iface.confname = conf_p2p_dev;
3830 iface.ctrl_interface = NULL;
3832 iface.confname = wpa_s->confname;
3833 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3836 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
3837 if (!p2pdev_wpa_s) {
3838 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3842 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
3843 wpa_s->pending_interface_name[0] = '\0';
3848 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3849 const u8 *noa, size_t noa_len)
3851 struct wpa_supplicant *wpa_s, *intf = ctx;
3854 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3855 if (wpa_s->waiting_presence_resp)
3859 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3862 wpa_s->waiting_presence_resp = 0;
3864 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3865 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3866 " status=%u noa=%s", MAC2STR(src), status, hex);
3870 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3871 size_t ssid_len, u8 *go_dev_addr,
3872 u8 *ret_ssid, size_t *ret_ssid_len,
3873 u8 *intended_iface_addr)
3875 struct wpa_supplicant *wpa_s = ctx;
3878 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3880 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3881 *ret_ssid_len = s->ssid_len;
3882 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3884 if (s->mode != WPAS_MODE_P2P_GO) {
3885 os_memset(intended_iface_addr, 0, ETH_ALEN);
3886 } else if (wpas_p2p_create_iface(wpa_s)) {
3887 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3890 os_memcpy(intended_iface_addr,
3891 wpa_s->pending_interface_addr, ETH_ALEN);
3893 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3903 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3904 u8 *ssid, size_t *ssid_len, int *group_iface,
3907 struct wpa_supplicant *wpa_s = ctx;
3908 struct wpa_supplicant *go;
3912 * group_iface will be set to 1 only if a dedicated interface for P2P
3913 * role is required. First, we try to reuse an active GO. However,
3914 * if it is not present, we will try to reactivate an existing
3915 * persistent group and set group_iface to 1, so the caller will know
3916 * that the pending interface should be used.
3923 go = wpas_p2p_get_go_group(wpa_s);
3925 s = wpas_p2p_get_persistent_go(wpa_s);
3926 *group_iface = wpas_p2p_create_iface(wpa_s);
3928 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3932 s = go->current_ssid;
3933 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3935 *freq = go->assoc_freq;
3938 os_memcpy(ssid, s->ssid, s->ssid_len);
3939 *ssid_len = s->ssid_len;
3945 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3946 const u8 *ssid, size_t ssid_len)
3948 struct wpa_supplicant *wpa_s = ctx;
3950 int save_config = 0;
3953 /* Start with our first choice of Persistent Groups */
3954 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3955 if (go && ssid && ssid_len &&
3956 s->ssid_len == ssid_len &&
3957 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3958 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3961 /* Remove stale persistent group */
3962 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3963 wpa_dbg(wpa_s, MSG_DEBUG,
3964 "P2P: Remove stale persistent group id=%d",
3966 wpas_notify_persistent_group_removed(wpa_s, s);
3967 wpa_config_remove_network(wpa_s->conf, s->id);
3972 for (i = 0; i < s->num_p2p_clients; i++) {
3973 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3974 peer, ETH_ALEN) != 0)
3977 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3978 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3979 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3982 s->num_p2p_clients--;
3987 p2p_config_write(wpa_s);
3989 /* Return TRUE if valid SSID remains */
3994 static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3995 const u8 *feat_cap, size_t feat_cap_len)
3997 static const char pref[] = " feature_cap=";
4003 * We expect a feature capability to contain at least one byte to be
4004 * reported. The string buffer provided by the caller function is
4005 * expected to be big enough to contain all bytes of the attribute for
4006 * known specifications. This function truncates the reported bytes if
4007 * the feature capability data exceeds the string buffer size.
4009 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4012 os_memcpy(buf, pref, sizeof(pref));
4013 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4014 buf_len - sizeof(pref) + 1,
4015 feat_cap, feat_cap_len);
4017 if (ret != (2 * (int) feat_cap_len))
4018 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4022 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4023 const u8 *adv_mac, const u8 *ses_mac,
4024 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4025 u8 conncap, int passwd_id,
4026 const u8 *persist_ssid,
4027 size_t persist_ssid_size, int response_done,
4028 int prov_start, const char *session_info,
4029 const u8 *feat_cap, size_t feat_cap_len,
4031 const u8 *group_ssid, size_t group_ssid_len)
4033 struct wpa_supplicant *wpa_s = ctx;
4035 struct wpa_ssid *persistent_go, *stale, *s = NULL;
4036 int save_config = 0;
4037 struct wpa_supplicant *go_wpa_s;
4038 char feat_cap_str[256];
4043 os_memset(mac, 0, ETH_ALEN);
4051 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4052 feat_cap, feat_cap_len);
4055 if (session_info == NULL) {
4056 wpa_msg_global(wpa_s, MSG_INFO,
4057 P2P_EVENT_P2PS_PROVISION_START MACSTR
4058 " adv_id=%x conncap=%x"
4060 " session=%x mac=" MACSTR
4061 " dev_passwd_id=%d%s",
4062 MAC2STR(dev), adv_id, conncap,
4064 ses_id, MAC2STR(ses_mac),
4065 passwd_id, feat_cap_str);
4067 wpa_msg_global(wpa_s, MSG_INFO,
4068 P2P_EVENT_P2PS_PROVISION_START MACSTR
4069 " adv_id=%x conncap=%x"
4071 " session=%x mac=" MACSTR
4072 " dev_passwd_id=%d info='%s'%s",
4073 MAC2STR(dev), adv_id, conncap,
4075 ses_id, MAC2STR(ses_mac),
4076 passwd_id, session_info, feat_cap_str);
4081 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4082 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4084 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4085 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4086 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4088 if (persistent_go && !persistent_go->num_p2p_clients) {
4089 /* remove empty persistent GO */
4090 wpa_dbg(wpa_s, MSG_DEBUG,
4091 "P2P: Remove empty persistent group id=%d",
4093 wpas_notify_persistent_group_removed(wpa_s,
4095 wpa_config_remove_network(wpa_s->conf,
4099 wpa_msg_global(wpa_s, MSG_INFO,
4100 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4102 " adv_id=%x adv_mac=" MACSTR
4103 " session=%x mac=" MACSTR "%s",
4104 MAC2STR(dev), status,
4105 adv_id, MAC2STR(adv_mac),
4106 ses_id, MAC2STR(ses_mac), feat_cap_str);
4110 /* Clean up stale persistent groups with this device */
4111 if (persist_ssid && persist_ssid_size)
4112 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4115 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4116 is_zero_ether_addr(grp_mac)) {
4117 wpa_dbg(wpa_s, MSG_ERROR,
4118 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4123 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4127 if (s && s->ssid_len == stale->ssid_len &&
4128 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4129 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4132 /* Remove stale persistent group */
4133 if (stale->mode != WPAS_MODE_P2P_GO ||
4134 stale->num_p2p_clients <= 1) {
4135 wpa_dbg(wpa_s, MSG_DEBUG,
4136 "P2P: Remove stale persistent group id=%d",
4138 wpas_notify_persistent_group_removed(wpa_s, stale);
4139 wpa_config_remove_network(wpa_s->conf, stale->id);
4143 for (i = 0; i < stale->num_p2p_clients; i++) {
4144 if (os_memcmp(stale->p2p_client_list +
4146 dev, ETH_ALEN) == 0) {
4147 os_memmove(stale->p2p_client_list +
4149 stale->p2p_client_list +
4151 (stale->num_p2p_clients -
4156 stale->num_p2p_clients--;
4162 p2p_config_write(wpa_s);
4165 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4166 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4168 if (persistent_go && s != persistent_go &&
4169 !persistent_go->num_p2p_clients) {
4170 /* remove empty persistent GO */
4171 wpa_dbg(wpa_s, MSG_DEBUG,
4172 "P2P: Remove empty persistent group id=%d",
4174 wpas_notify_persistent_group_removed(wpa_s,
4176 wpa_config_remove_network(wpa_s->conf,
4181 wpa_msg_global(wpa_s, MSG_INFO,
4182 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4184 " adv_id=%x adv_mac=" MACSTR
4185 " session=%x mac=" MACSTR
4187 MAC2STR(dev), status,
4188 adv_id, MAC2STR(adv_mac),
4189 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
4193 wpa_s->global->pending_p2ps_group = 0;
4194 wpa_s->global->pending_p2ps_group_freq = 0;
4196 if (conncap == P2PS_SETUP_GROUP_OWNER) {
4198 * We need to copy the interface name. Simply saving a
4199 * pointer isn't enough, since if we use pending_interface_name
4200 * it will be overwritten when the group is added.
4202 char go_ifname[100];
4204 go_ifname[0] = '\0';
4206 if (!response_done) {
4207 wpa_s->global->pending_p2ps_group = 1;
4208 wpa_s->global->pending_p2ps_group_freq = freq;
4211 if (!wpas_p2p_create_iface(wpa_s))
4212 os_memcpy(go_ifname, wpa_s->ifname,
4214 else if (wpa_s->pending_interface_name[0])
4215 os_memcpy(go_ifname,
4216 wpa_s->pending_interface_name,
4219 if (!go_ifname[0]) {
4220 wpas_p2ps_prov_complete(
4221 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4222 dev, adv_mac, ses_mac,
4223 grp_mac, adv_id, ses_id, 0, 0,
4224 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4229 /* If PD Resp complete, start up the GO */
4230 if (response_done && persistent_go) {
4231 wpas_p2p_group_add_persistent(
4232 wpa_s, persistent_go,
4233 0, 0, freq, 0, 0, 0, 0, 0, NULL,
4234 persistent_go->mode ==
4236 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4238 } else if (response_done) {
4239 wpas_p2p_group_add(wpa_s, 1, freq,
4243 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4244 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4246 wpa_s->p2ps_method_config_any = 1;
4248 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4249 os_memcpy(go_ifname, go_wpa_s->ifname,
4252 if (is_zero_ether_addr(grp_mac)) {
4253 wpa_dbg(go_wpa_s, MSG_DEBUG,
4254 "P2P: Setting PIN-1 for ANY");
4255 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4256 "12345670", NULL, 0,
4259 wpa_dbg(go_wpa_s, MSG_DEBUG,
4260 "P2P: Setting PIN-1 for " MACSTR,
4262 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4263 "12345670", NULL, 0,
4267 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4268 wpa_s->p2ps_method_config_any = 1;
4271 wpa_msg_global(wpa_s, MSG_INFO,
4272 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4273 " status=%d conncap=%x"
4274 " adv_id=%x adv_mac=" MACSTR
4275 " session=%x mac=" MACSTR
4276 " dev_passwd_id=%d go=%s%s",
4277 MAC2STR(dev), status, conncap,
4278 adv_id, MAC2STR(adv_mac),
4279 ses_id, MAC2STR(ses_mac),
4280 passwd_id, go_ifname, feat_cap_str);
4284 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4285 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4287 if (persistent_go && !persistent_go->num_p2p_clients) {
4288 /* remove empty persistent GO */
4289 wpa_dbg(wpa_s, MSG_DEBUG,
4290 "P2P: Remove empty persistent group id=%d",
4292 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
4293 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4296 if (conncap == P2PS_SETUP_CLIENT) {
4297 char ssid_hex[32 * 2 + 1];
4300 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4301 group_ssid, group_ssid_len);
4304 wpa_msg_global(wpa_s, MSG_INFO,
4305 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4306 " status=%d conncap=%x"
4307 " adv_id=%x adv_mac=" MACSTR
4308 " session=%x mac=" MACSTR
4309 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
4310 MAC2STR(dev), status, conncap,
4311 adv_id, MAC2STR(adv_mac),
4312 ses_id, MAC2STR(ses_mac),
4313 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4314 group_ssid ? " group_ssid=" : "", ssid_hex);
4316 wpa_msg_global(wpa_s, MSG_INFO,
4317 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4318 " status=%d conncap=%x"
4319 " adv_id=%x adv_mac=" MACSTR
4320 " session=%x mac=" MACSTR
4321 " dev_passwd_id=%d%s",
4322 MAC2STR(dev), status, conncap,
4323 adv_id, MAC2STR(adv_mac),
4324 ses_id, MAC2STR(ses_mac),
4325 passwd_id, feat_cap_str);
4330 static int _wpas_p2p_in_progress(void *ctx)
4332 struct wpa_supplicant *wpa_s = ctx;
4333 return wpas_p2p_in_progress(wpa_s);
4337 static int wpas_prov_disc_resp_cb(void *ctx)
4339 struct wpa_supplicant *wpa_s = ctx;
4340 struct wpa_ssid *persistent_go;
4343 if (!wpa_s->global->pending_p2ps_group)
4346 freq = wpa_s->global->pending_p2ps_group_freq;
4347 wpa_s->global->pending_p2ps_group_freq = 0;
4348 wpa_s->global->pending_p2ps_group = 0;
4350 if (wpas_p2p_get_go_group(wpa_s))
4352 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4354 if (persistent_go) {
4355 wpas_p2p_group_add_persistent(
4356 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, NULL,
4357 persistent_go->mode == WPAS_MODE_P2P_GO ?
4358 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
4360 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0);
4367 static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4369 unsigned int *freq_list)
4371 struct wpa_supplicant *wpa_s = ctx;
4373 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4374 WPA_IF_P2P_CLIENT, len, freq_list);
4378 int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4380 u8 addr[ETH_ALEN] = {0};
4382 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4385 if (!wpa_s->conf->ssid) {
4386 if (random_mac_addr(addr) < 0) {
4387 wpa_msg(wpa_s, MSG_INFO,
4388 "Failed to generate random MAC address");
4392 /* Store generated MAC address. */
4393 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4396 /* If there are existing saved groups, restore last MAC address.
4397 * if there is no last used MAC address, the last one is
4399 if (is_zero_ether_addr(
4400 wpa_s->conf->p2p_device_persistent_mac_addr))
4402 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4404 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4407 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4408 wpa_msg(wpa_s, MSG_INFO,
4409 "Failed to set random MAC address");
4413 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4414 wpa_msg(wpa_s, MSG_INFO,
4415 "Could not update MAC address information");
4419 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4427 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4428 * @global: Pointer to global data from wpa_supplicant_init()
4429 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4430 * Returns: 0 on success, -1 on failure
4432 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4434 struct p2p_config p2p;
4437 if (wpa_s->conf->p2p_disabled)
4440 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4446 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4447 wpa_msg(wpa_s, MSG_ERROR,
4448 "Failed to initialize P2P random MAC address.");
4452 os_memset(&p2p, 0, sizeof(p2p));
4454 p2p.debug_print = wpas_p2p_debug_print;
4455 p2p.p2p_scan = wpas_p2p_scan;
4456 p2p.send_action = wpas_send_action;
4457 p2p.send_action_done = wpas_send_action_done;
4458 p2p.go_neg_completed = wpas_go_neg_completed;
4459 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4460 p2p.dev_found = wpas_dev_found;
4461 p2p.dev_lost = wpas_dev_lost;
4462 p2p.find_stopped = wpas_find_stopped;
4463 p2p.start_listen = wpas_start_listen;
4464 p2p.stop_listen = wpas_stop_listen;
4465 p2p.send_probe_resp = wpas_send_probe_resp;
4466 p2p.sd_request = wpas_sd_request;
4467 p2p.sd_response = wpas_sd_response;
4468 p2p.prov_disc_req = wpas_prov_disc_req;
4469 p2p.prov_disc_resp = wpas_prov_disc_resp;
4470 p2p.prov_disc_fail = wpas_prov_disc_fail;
4471 p2p.invitation_process = wpas_invitation_process;
4472 p2p.invitation_received = wpas_invitation_received;
4473 p2p.invitation_result = wpas_invitation_result;
4474 p2p.get_noa = wpas_get_noa;
4475 p2p.go_connected = wpas_go_connected;
4476 p2p.presence_resp = wpas_presence_resp;
4477 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4478 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4479 p2p.get_persistent_group = wpas_get_persistent_group;
4480 p2p.get_go_info = wpas_get_go_info;
4481 p2p.remove_stale_groups = wpas_remove_stale_groups;
4482 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4483 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4484 p2p.p2ps_group_capability = p2ps_group_capability;
4485 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
4487 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4488 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4489 p2p.dev_name = wpa_s->conf->device_name;
4490 p2p.manufacturer = wpa_s->conf->manufacturer;
4491 p2p.model_name = wpa_s->conf->model_name;
4492 p2p.model_number = wpa_s->conf->model_number;
4493 p2p.serial_number = wpa_s->conf->serial_number;
4495 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4496 p2p.config_methods = wpa_s->wps->config_methods;
4499 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4500 wpa_printf(MSG_ERROR,
4501 "P2P: Failed to configure supported channel list");
4505 if (wpa_s->conf->p2p_listen_reg_class &&
4506 wpa_s->conf->p2p_listen_channel) {
4507 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4508 p2p.channel = wpa_s->conf->p2p_listen_channel;
4509 p2p.channel_forced = 1;
4512 * Pick one of the social channels randomly as the listen
4515 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4517 &global->p2p_go_avoid_freq,
4518 &global->p2p_disallow_freq) !=
4520 wpa_printf(MSG_INFO,
4521 "P2P: No social channels supported by the driver - do not enable P2P");
4524 p2p.channel_forced = 0;
4526 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4527 p2p.reg_class, p2p.channel);
4529 if (wpa_s->conf->p2p_oper_reg_class &&
4530 wpa_s->conf->p2p_oper_channel) {
4531 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4532 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4533 p2p.cfg_op_channel = 1;
4534 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4535 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4539 * Use random operation channel from 2.4 GHz band social
4540 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4541 * other preference is indicated.
4543 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4544 &p2p.op_channel, NULL,
4546 wpa_printf(MSG_INFO,
4547 "P2P: Failed to select random social channel as operation channel");
4548 p2p.op_reg_class = 0;
4550 /* This will be overridden during group setup in
4551 * p2p_prepare_channel(), so allow setup to continue. */
4553 p2p.cfg_op_channel = 0;
4554 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4555 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4558 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4559 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4560 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4563 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4564 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4565 p2p.country[2] = 0x04;
4567 os_memcpy(p2p.country, "XX\x04", 3);
4569 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4572 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4573 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4574 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4576 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4577 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4579 p2p.max_peers = 100;
4581 if (wpa_s->conf->p2p_ssid_postfix) {
4582 p2p.ssid_postfix_len =
4583 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4584 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4585 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4586 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4587 p2p.ssid_postfix_len);
4590 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4592 p2p.max_listen = wpa_s->max_remain_on_chan;
4594 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4595 wpa_s->conf->p2p_passphrase_len <= 63)
4596 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4598 p2p.passphrase_len = 8;
4600 global->p2p = p2p_init(&p2p);
4601 if (global->p2p == NULL)
4603 global->p2p_init_wpa_s = wpa_s;
4605 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4606 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4608 p2p_add_wps_vendor_extension(
4609 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4612 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4619 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4620 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4622 * This function deinitialize per-interface P2P data.
4624 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4626 if (wpa_s->driver && wpa_s->drv_priv)
4627 wpa_drv_probe_req_report(wpa_s, 0);
4629 if (wpa_s->go_params) {
4630 /* Clear any stored provisioning info */
4631 p2p_clear_provisioning_info(
4633 wpa_s->go_params->peer_device_addr);
4636 os_free(wpa_s->go_params);
4637 wpa_s->go_params = NULL;
4638 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4639 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4640 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4641 wpa_s->p2p_long_listen = 0;
4642 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4643 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4644 wpas_p2p_remove_pending_group_interface(wpa_s);
4645 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4646 wpas_p2p_listen_work_done(wpa_s);
4647 if (wpa_s->p2p_send_action_work) {
4648 os_free(wpa_s->p2p_send_action_work->ctx);
4649 radio_work_done(wpa_s->p2p_send_action_work);
4650 wpa_s->p2p_send_action_work = NULL;
4652 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4654 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4655 wpa_s->p2p_oob_dev_pw = NULL;
4657 os_free(wpa_s->p2p_group_common_freqs);
4658 wpa_s->p2p_group_common_freqs = NULL;
4659 wpa_s->p2p_group_common_freqs_num = 0;
4661 /* TODO: remove group interface from the driver if this wpa_s instance
4662 * is on top of a P2P group interface */
4667 * wpas_p2p_deinit_global - Deinitialize global P2P module
4668 * @global: Pointer to global data from wpa_supplicant_init()
4670 * This function deinitializes the global (per device) P2P module.
4672 static void wpas_p2p_deinit_global(struct wpa_global *global)
4674 struct wpa_supplicant *wpa_s, *tmp;
4676 wpa_s = global->ifaces;
4678 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4680 /* Remove remaining P2P group interfaces */
4681 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4682 wpa_s = wpa_s->next;
4684 tmp = global->ifaces;
4687 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4692 /* Disconnect from the P2P group and deinit the interface */
4693 wpas_p2p_disconnect(tmp);
4697 * Deinit GO data on any possibly remaining interface (if main
4698 * interface is used as GO).
4700 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4701 if (wpa_s->ap_iface)
4702 wpas_p2p_group_deinit(wpa_s);
4705 p2p_deinit(global->p2p);
4707 global->p2p_init_wpa_s = NULL;
4711 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4713 if (wpa_s->conf->p2p_no_group_iface)
4714 return 0; /* separate interface disabled per configuration */
4715 if (wpa_s->drv_flags &
4716 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4717 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4718 return 1; /* P2P group requires a new interface in every case
4720 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4721 return 0; /* driver does not support concurrent operations */
4722 if (wpa_s->global->ifaces->next)
4723 return 1; /* more that one interface already in use */
4724 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4725 return 1; /* this interface is already in use */
4730 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4731 const u8 *peer_addr,
4732 enum p2p_wps_method wps_method,
4733 int go_intent, const u8 *own_interface_addr,
4734 unsigned int force_freq, int persistent_group,
4735 struct wpa_ssid *ssid, unsigned int pref_freq)
4737 if (persistent_group && wpa_s->conf->persistent_reconnect)
4738 persistent_group = 2;
4741 * Increase GO config timeout if HT40 is used since it takes some time
4742 * to scan channels for coex purposes before the BSS can be started.
4744 p2p_set_config_timeout(wpa_s->global->p2p,
4745 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4747 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4748 go_intent, own_interface_addr, force_freq,
4749 persistent_group, ssid ? ssid->ssid : NULL,
4750 ssid ? ssid->ssid_len : 0,
4751 wpa_s->p2p_pd_before_go_neg, pref_freq,
4752 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4757 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4758 const u8 *peer_addr,
4759 enum p2p_wps_method wps_method,
4760 int go_intent, const u8 *own_interface_addr,
4761 unsigned int force_freq, int persistent_group,
4762 struct wpa_ssid *ssid, unsigned int pref_freq)
4764 if (persistent_group && wpa_s->conf->persistent_reconnect)
4765 persistent_group = 2;
4767 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4768 go_intent, own_interface_addr, force_freq,
4769 persistent_group, ssid ? ssid->ssid : NULL,
4770 ssid ? ssid->ssid_len : 0, pref_freq,
4771 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4776 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4778 wpa_s->p2p_join_scan_count++;
4779 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4780 wpa_s->p2p_join_scan_count);
4781 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4782 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4783 " for join operationg - stop join attempt",
4784 MAC2STR(wpa_s->pending_join_iface_addr));
4785 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4786 if (wpa_s->p2p_auto_pd) {
4787 wpa_s->p2p_auto_pd = 0;
4788 wpa_msg_global(wpa_s, MSG_INFO,
4789 P2P_EVENT_PROV_DISC_FAILURE
4790 " p2p_dev_addr=" MACSTR " status=N/A",
4791 MAC2STR(wpa_s->pending_join_dev_addr));
4794 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
4795 P2P_EVENT_GROUP_FORMATION_FAILURE);
4796 wpas_notify_p2p_group_formation_failure(wpa_s, "");
4801 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4804 unsigned int num, i;
4805 struct wpa_used_freq_data *freqs;
4807 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4808 /* Multiple channels are supported and not all are in use */
4812 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4813 sizeof(struct wpa_used_freq_data));
4817 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4818 wpa_s->num_multichan_concurrent);
4820 for (i = 0; i < num; i++) {
4821 if (freqs[i].freq == freq) {
4822 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4829 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4838 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4839 const u8 *peer_dev_addr)
4841 struct wpa_bss *bss;
4844 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4847 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4848 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4853 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4855 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4856 "%ld.%06ld (%supdated in last scan)",
4857 bss->last_update.sec, bss->last_update.usec,
4858 updated ? "": "not ");
4864 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4865 struct wpa_scan_results *scan_res)
4867 struct wpa_bss *bss = NULL;
4869 u8 iface_addr[ETH_ALEN];
4871 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4873 if (wpa_s->global->p2p_disabled)
4876 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4877 scan_res ? (int) scan_res->num : -1,
4878 wpa_s->p2p_auto_join ? "auto_" : "");
4881 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4883 if (wpa_s->p2p_auto_pd) {
4884 int join = wpas_p2p_peer_go(wpa_s,
4885 wpa_s->pending_join_dev_addr);
4887 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4888 wpa_s->auto_pd_scan_retry++;
4889 bss = wpa_bss_get_bssid_latest(
4890 wpa_s, wpa_s->pending_join_dev_addr);
4893 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4894 "the peer " MACSTR " at %d MHz",
4895 wpa_s->auto_pd_scan_retry,
4897 pending_join_dev_addr),
4899 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4907 wpa_s->p2p_auto_pd = 0;
4908 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4909 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4910 MAC2STR(wpa_s->pending_join_dev_addr), join);
4911 if (p2p_prov_disc_req(wpa_s->global->p2p,
4912 wpa_s->pending_join_dev_addr, NULL,
4913 wpa_s->pending_pd_config_methods, join,
4914 0, wpa_s->user_initiated_pd) < 0) {
4915 wpa_s->p2p_auto_pd = 0;
4916 wpa_msg_global(wpa_s, MSG_INFO,
4917 P2P_EVENT_PROV_DISC_FAILURE
4918 " p2p_dev_addr=" MACSTR " status=N/A",
4919 MAC2STR(wpa_s->pending_join_dev_addr));
4924 if (wpa_s->p2p_auto_join) {
4925 int join = wpas_p2p_peer_go(wpa_s,
4926 wpa_s->pending_join_dev_addr);
4928 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4929 "running a GO -> use GO Negotiation");
4930 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
4931 P2P_EVENT_FALLBACK_TO_GO_NEG
4932 "reason=peer-not-running-GO");
4933 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4934 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4935 wpa_s->p2p_persistent_group, 0, 0, 0,
4936 wpa_s->p2p_go_intent,
4937 wpa_s->p2p_connect_freq,
4938 wpa_s->p2p_go_vht_center_freq2,
4939 wpa_s->p2p_persistent_id,
4940 wpa_s->p2p_pd_before_go_neg,
4943 wpa_s->p2p_go_max_oper_chwidth,
4949 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4950 "try to join the group", join ? "" :
4953 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
4954 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
4955 wpa_s->p2p_fallback_to_go_neg = 1;
4959 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4960 wpa_s->pending_join_iface_addr);
4962 p2p_get_interface_addr(wpa_s->global->p2p,
4963 wpa_s->pending_join_dev_addr,
4965 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4966 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4967 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4968 "address for join from " MACSTR " to " MACSTR
4969 " based on newly discovered P2P peer entry",
4970 MAC2STR(wpa_s->pending_join_iface_addr),
4971 MAC2STR(iface_addr));
4972 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4975 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4976 wpa_s->pending_join_iface_addr);
4979 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4980 "from P2P peer table: %d MHz", freq);
4982 if (wpa_s->p2p_join_ssid_len) {
4983 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4984 MACSTR " and SSID %s",
4985 MAC2STR(wpa_s->pending_join_iface_addr),
4986 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4987 wpa_s->p2p_join_ssid_len));
4988 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4989 wpa_s->p2p_join_ssid,
4990 wpa_s->p2p_join_ssid_len);
4992 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4993 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4994 bss = wpa_bss_get_bssid_latest(wpa_s,
4995 wpa_s->pending_join_iface_addr);
4998 u8 dev_addr[ETH_ALEN];
5001 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5002 "from BSS table: %d MHz (SSID %s)", freq,
5003 wpa_ssid_txt(bss->ssid, bss->ssid_len));
5004 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
5006 os_memcmp(wpa_s->pending_join_dev_addr,
5007 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5008 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5010 wpa_printf(MSG_DEBUG,
5011 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5013 MAC2STR(wpa_s->pending_join_dev_addr));
5014 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5021 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
5022 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5023 P2P_EVENT_GROUP_FORMATION_FAILURE
5024 "reason=FREQ_CONFLICT");
5025 wpas_notify_p2p_group_formation_failure(
5026 wpa_s, "FREQ_CONFLICT");
5030 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5031 "prior to joining an existing group (GO " MACSTR
5033 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5034 wpa_s->pending_pd_before_join = 1;
5036 switch (wpa_s->pending_join_wps_method) {
5037 case WPS_PIN_DISPLAY:
5038 method = WPS_CONFIG_KEYPAD;
5040 case WPS_PIN_KEYPAD:
5041 method = WPS_CONFIG_DISPLAY;
5044 method = WPS_CONFIG_PUSHBUTTON;
5047 method = WPS_CONFIG_P2PS;
5054 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5055 wpa_s->pending_join_dev_addr) ==
5058 * We have already performed provision discovery for
5059 * joining the group. Proceed directly to join
5060 * operation without duplicated provision discovery. */
5061 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5062 "with " MACSTR " already done - proceed to "
5064 MAC2STR(wpa_s->pending_join_dev_addr));
5065 wpa_s->pending_pd_before_join = 0;
5069 if (p2p_prov_disc_req(wpa_s->global->p2p,
5070 wpa_s->pending_join_dev_addr,
5072 freq, wpa_s->user_initiated_pd) < 0) {
5073 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5074 "Discovery Request before joining an "
5076 wpa_s->pending_pd_before_join = 0;
5082 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5083 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5084 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5085 wpas_p2p_check_join_scan_limit(wpa_s);
5089 /* Start join operation immediately */
5090 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5091 wpa_s->p2p_join_ssid_len);
5095 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5096 const u8 *ssid, size_t ssid_len)
5099 struct wpa_driver_scan_params params;
5100 struct wpabuf *wps_ie, *ies;
5102 int freqs[2] = { 0, 0 };
5105 os_memset(¶ms, 0, sizeof(params));
5107 /* P2P Wildcard SSID */
5108 params.num_ssids = 1;
5109 if (ssid && ssid_len) {
5110 params.ssids[0].ssid = ssid;
5111 params.ssids[0].ssid_len = ssid_len;
5112 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5113 wpa_s->p2p_join_ssid_len = ssid_len;
5115 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5116 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5117 wpa_s->p2p_join_ssid_len = 0;
5120 wpa_s->wps->dev.p2p = 1;
5121 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5122 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5124 if (wps_ie == NULL) {
5125 wpas_p2p_scan_res_join(wpa_s, NULL);
5132 * If freq is not provided, check the operating freq of the GO
5133 * and use a single channel scan on if possible.
5135 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5136 wpa_s->pending_join_iface_addr);
5142 params.freqs = freqs;
5145 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5146 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5148 wpabuf_free(wps_ie);
5149 wpas_p2p_scan_res_join(wpa_s, NULL);
5152 wpabuf_put_buf(ies, wps_ie);
5153 wpabuf_free(wps_ie);
5155 bands = wpas_get_bands(wpa_s, freqs);
5156 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5158 params.p2p_probe = 1;
5159 params.extra_ies = wpabuf_head(ies);
5160 params.extra_ies_len = wpabuf_len(ies);
5162 if (wpa_s->clear_driver_scan_cache) {
5163 wpa_printf(MSG_DEBUG,
5164 "Request driver to clear scan cache due to local BSS flush");
5165 params.only_new_results = 1;
5169 * Run a scan to update BSS table and start Provision Discovery once
5170 * the new scan results become available.
5172 ret = wpa_drv_scan(wpa_s, ¶ms);
5174 os_get_reltime(&wpa_s->scan_trigger_time);
5175 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
5176 wpa_s->own_scan_requested = 1;
5177 wpa_s->clear_driver_scan_cache = 0;
5183 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5185 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5186 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5187 wpas_p2p_check_join_scan_limit(wpa_s);
5192 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5194 struct wpa_supplicant *wpa_s = eloop_ctx;
5195 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
5199 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
5200 const u8 *dev_addr, enum p2p_wps_method wps_method,
5201 int auto_join, int op_freq,
5202 const u8 *ssid, size_t ssid_len)
5204 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
5205 MACSTR " dev " MACSTR " op_freq=%d)%s",
5206 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
5207 auto_join ? " (auto_join)" : "");
5208 if (ssid && ssid_len) {
5209 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5210 wpa_ssid_txt(ssid, ssid_len));
5213 wpa_s->p2p_auto_pd = 0;
5214 wpa_s->p2p_auto_join = !!auto_join;
5215 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5216 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5217 wpa_s->pending_join_wps_method = wps_method;
5219 /* Make sure we are not running find during connection establishment */
5220 wpas_p2p_stop_find(wpa_s);
5222 wpa_s->p2p_join_scan_count = 0;
5223 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
5228 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5229 const u8 *ssid, size_t ssid_len)
5231 struct wpa_supplicant *group;
5232 struct p2p_go_neg_results res;
5233 struct wpa_bss *bss;
5235 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5238 if (group != wpa_s) {
5239 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5240 sizeof(group->p2p_pin));
5241 group->p2p_wps_method = wpa_s->p2p_wps_method;
5245 * Need to mark the current interface for p2p_group_formation
5246 * when a separate group interface is not used. This is needed
5247 * to allow p2p_cancel stop a pending p2p_connect-join.
5248 * wpas_p2p_init_group_interface() addresses this for the case
5249 * where a separate group interface is used.
5251 if (group == wpa_s->parent)
5252 wpa_s->global->p2p_group_formation = group;
5254 group->p2p_in_provisioning = 1;
5255 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
5257 os_memset(&res, 0, sizeof(res));
5258 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
5259 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5261 res.wps_method = wpa_s->pending_join_wps_method;
5262 if (freq && ssid && ssid_len) {
5264 res.ssid_len = ssid_len;
5265 os_memcpy(res.ssid, ssid, ssid_len);
5267 if (ssid && ssid_len) {
5268 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5271 bss = wpa_bss_get_bssid_latest(
5272 wpa_s, wpa_s->pending_join_iface_addr);
5275 res.freq = bss->freq;
5276 res.ssid_len = bss->ssid_len;
5277 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5278 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5280 wpa_ssid_txt(bss->ssid, bss->ssid_len));
5281 } else if (ssid && ssid_len) {
5282 res.ssid_len = ssid_len;
5283 os_memcpy(res.ssid, ssid, ssid_len);
5284 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5285 wpa_ssid_txt(ssid, ssid_len));
5289 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5290 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5292 wpa_drv_cancel_remain_on_channel(wpa_s);
5293 wpa_s->off_channel_freq = 0;
5294 wpa_s->roc_waiting_drv_freq = 0;
5296 wpas_start_wps_enrollee(group, &res);
5299 * Allow a longer timeout for join-a-running-group than normal 15
5300 * second group formation timeout since the GO may not have authorized
5301 * our connection yet.
5303 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5304 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5311 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
5312 int *force_freq, int *pref_freq, int go,
5313 unsigned int *pref_freq_list,
5314 unsigned int *num_pref_freq)
5316 struct wpa_used_freq_data *freqs;
5317 int res, best_freq, num_unused;
5318 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5320 max_pref_freq = *num_pref_freq;
5323 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5324 sizeof(struct wpa_used_freq_data));
5328 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5329 wpa_s->num_multichan_concurrent);
5332 * It is possible that the total number of used frequencies is bigger
5333 * than the number of frequencies used for P2P, so get the system wide
5334 * number of unused frequencies.
5336 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5338 wpa_printf(MSG_DEBUG,
5339 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5340 freq, wpa_s->num_multichan_concurrent, num, num_unused);
5345 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5347 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5349 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5350 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5351 wpa_s->hw.num_modes)) {
5353 * If freq is a DFS channel and DFS is offloaded
5354 * to the driver, allow P2P GO to use it.
5356 wpa_printf(MSG_DEBUG,
5357 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5360 wpa_printf(MSG_DEBUG,
5361 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5368 for (i = 0; i < num; i++) {
5369 if (freqs[i].freq == freq)
5373 if (num_unused <= 0 && !freq_in_use) {
5374 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5379 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5380 "requested channel (%u MHz)", freq);
5385 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5387 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5388 enum wpa_driver_if_type iface_type;
5391 iface_type = WPA_IF_P2P_GO;
5393 iface_type = WPA_IF_P2P_CLIENT;
5395 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5398 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5401 if (!res && max_pref_freq > 0) {
5402 *num_pref_freq = max_pref_freq;
5404 while (i < *num_pref_freq &&
5405 (!p2p_supported_freq(wpa_s->global->p2p,
5406 pref_freq_list[i]) ||
5407 wpas_p2p_disallowed_freq(wpa_s->global,
5408 pref_freq_list[i]))) {
5409 wpa_printf(MSG_DEBUG,
5410 "P2P: preferred_freq_list[%d]=%d is disallowed",
5411 i, pref_freq_list[i]);
5414 if (i != *num_pref_freq) {
5415 best_freq = pref_freq_list[i];
5416 wpa_printf(MSG_DEBUG,
5417 "P2P: Using preferred_freq_list[%d]=%d",
5420 wpa_printf(MSG_DEBUG,
5421 "P2P: All driver preferred frequencies are disallowed for P2P use");
5425 wpa_printf(MSG_DEBUG,
5426 "P2P: No preferred frequency list available");
5430 /* We have a candidate frequency to use */
5431 if (best_freq > 0) {
5432 if (*pref_freq == 0 && num_unused > 0) {
5433 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
5435 *pref_freq = best_freq;
5437 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
5439 *force_freq = best_freq;
5441 } else if (num_unused > 0) {
5442 wpa_printf(MSG_DEBUG,
5443 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5446 wpa_printf(MSG_DEBUG,
5447 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5461 * wpas_p2p_connect - Request P2P Group Formation to be started
5462 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5463 * @peer_addr: Address of the peer P2P Device
5464 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5465 * @persistent_group: Whether to create a persistent group
5466 * @auto_join: Whether to select join vs. GO Negotiation automatically
5467 * @join: Whether to join an existing group (as a client) instead of starting
5468 * Group Owner negotiation; @peer_addr is BSSID in that case
5469 * @auth: Whether to only authorize the connection instead of doing that and
5470 * initiating Group Owner negotiation
5471 * @go_intent: GO Intent or -1 to use default
5472 * @freq: Frequency for the group or 0 for auto-selection
5473 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
5474 * @persistent_id: Persistent group credentials to use for forcing GO
5475 * parameters or -1 to generate new values (SSID/passphrase)
5476 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5477 * interoperability workaround when initiating group formation
5478 * @ht40: Start GO with 40 MHz channel width
5479 * @vht: Start GO with VHT support
5480 * @vht_chwidth: Channel width supported by GO operating with VHT support
5482 * @group_ssid: Specific Group SSID for join or %NULL if not set
5483 * @group_ssid_len: Length of @group_ssid in octets
5484 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5485 * failure, -2 on failure due to channel not currently available,
5486 * -3 if forced channel is not supported
5488 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5489 const char *pin, enum p2p_wps_method wps_method,
5490 int persistent_group, int auto_join, int join, int auth,
5491 int go_intent, int freq, unsigned int vht_center_freq2,
5492 int persistent_id, int pd, int ht40, int vht,
5493 unsigned int vht_chwidth, int he, const u8 *group_ssid,
5494 size_t group_ssid_len)
5496 int force_freq = 0, pref_freq = 0;
5498 enum wpa_driver_if_type iftype;
5500 struct wpa_ssid *ssid = NULL;
5501 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
5503 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5506 if (persistent_id >= 0) {
5507 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5508 if (ssid == NULL || ssid->disabled != 2 ||
5509 ssid->mode != WPAS_MODE_P2P_GO)
5513 os_free(wpa_s->global->add_psk);
5514 wpa_s->global->add_psk = NULL;
5516 wpa_s->global->p2p_fail_on_wps_complete = 0;
5517 wpa_s->global->pending_p2ps_group = 0;
5518 wpa_s->global->pending_p2ps_group_freq = 0;
5519 wpa_s->p2ps_method_config_any = 0;
5522 go_intent = wpa_s->conf->p2p_go_intent;
5525 wpa_s->p2p_long_listen = 0;
5527 wpa_s->p2p_wps_method = wps_method;
5528 wpa_s->p2p_persistent_group = !!persistent_group;
5529 wpa_s->p2p_persistent_id = persistent_id;
5530 wpa_s->p2p_go_intent = go_intent;
5531 wpa_s->p2p_connect_freq = freq;
5532 wpa_s->p2p_fallback_to_go_neg = 0;
5533 wpa_s->p2p_pd_before_go_neg = !!pd;
5534 wpa_s->p2p_go_ht40 = !!ht40;
5535 wpa_s->p2p_go_vht = !!vht;
5536 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5537 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
5538 wpa_s->p2p_go_he = !!he;
5541 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5542 else if (wps_method == WPS_PIN_DISPLAY) {
5543 if (wps_generate_pin((unsigned int *) &ret) < 0)
5545 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5547 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5548 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5549 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5551 } else if (wps_method == WPS_P2PS) {
5552 /* Force the P2Ps default PIN to be used */
5553 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
5555 wpa_s->p2p_pin[0] = '\0';
5557 if (join || auto_join) {
5558 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5560 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5561 "connect a running group from " MACSTR,
5562 MAC2STR(peer_addr));
5563 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5566 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5567 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5569 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5570 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5574 os_get_reltime(&wpa_s->p2p_auto_started);
5575 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5577 wpa_s->p2p_auto_started.sec,
5578 wpa_s->p2p_auto_started.usec);
5580 wpa_s->user_initiated_pd = 1;
5581 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5583 group_ssid, group_ssid_len) < 0)
5588 size = P2P_MAX_PREF_CHANNELS;
5589 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5590 go_intent == 15, pref_freq_list, &size);
5593 wpas_p2p_set_own_freq_preference(wpa_s,
5594 force_freq ? force_freq : pref_freq);
5596 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5598 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5600 if (wpa_s->create_p2p_iface) {
5601 /* Prepare to add a new interface for the group */
5602 iftype = WPA_IF_P2P_GROUP;
5603 if (go_intent == 15)
5604 iftype = WPA_IF_P2P_GO;
5605 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5606 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5607 "interface for the group");
5611 if_addr = wpa_s->pending_interface_addr;
5613 if (wpa_s->p2p_mgmt)
5614 if_addr = wpa_s->parent->own_addr;
5616 if_addr = wpa_s->own_addr;
5617 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5621 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5623 force_freq, persistent_group, ssid,
5629 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5630 go_intent, if_addr, force_freq,
5631 persistent_group, ssid, pref_freq) < 0) {
5632 if (wpa_s->create_p2p_iface)
5633 wpas_p2p_remove_pending_group_interface(wpa_s);
5641 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5642 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5643 * @freq: Frequency of the channel in MHz
5644 * @duration: Duration of the stay on the channel in milliseconds
5646 * This callback is called when the driver indicates that it has started the
5647 * requested remain-on-channel duration.
5649 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5650 unsigned int freq, unsigned int duration)
5652 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5654 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)",
5655 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5656 wpa_s->roc_waiting_drv_freq, freq, duration);
5657 if (wpa_s->off_channel_freq &&
5658 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5659 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5660 wpa_s->pending_listen_duration);
5661 wpa_s->pending_listen_freq = 0;
5663 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5664 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5670 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5672 /* Limit maximum Listen state time based on driver limitation. */
5673 if (timeout > wpa_s->max_remain_on_chan)
5674 timeout = wpa_s->max_remain_on_chan;
5676 return p2p_listen(wpa_s->global->p2p, timeout);
5681 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5682 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5683 * @freq: Frequency of the channel in MHz
5685 * This callback is called when the driver indicates that a remain-on-channel
5686 * operation has been completed, i.e., the duration on the requested channel
5689 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5692 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5693 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5694 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5695 wpas_p2p_listen_work_done(wpa_s);
5696 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5698 if (wpa_s->p2p_long_listen > 0)
5699 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5700 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5701 return; /* P2P module started a new operation */
5702 if (offchannel_pending_action_tx(wpa_s))
5704 if (wpa_s->p2p_long_listen > 0) {
5705 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5706 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5709 * When listen duration is over, stop listen & update p2p_state
5712 p2p_stop_listen(wpa_s->global->p2p);
5718 * wpas_p2p_group_remove - Remove a P2P group
5719 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5720 * @ifname: Network interface name of the group interface or "*" to remove all
5722 * Returns: 0 on success, -1 on failure
5724 * This function is used to remove a P2P group. This can be used to disconnect
5725 * from a group in which the local end is a P2P Client or to end a P2P Group in
5726 * case the local end is the Group Owner. If a virtual network interface was
5727 * created for this group, that interface will be removed. Otherwise, only the
5728 * configured P2P group network will be removed from the interface.
5730 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5732 struct wpa_global *global = wpa_s->global;
5733 struct wpa_supplicant *calling_wpa_s = wpa_s;
5735 if (os_strcmp(ifname, "*") == 0) {
5736 struct wpa_supplicant *prev;
5737 wpa_s = global->ifaces;
5740 wpa_s = wpa_s->next;
5741 if (prev->p2p_group_interface !=
5742 NOT_P2P_GROUP_INTERFACE ||
5743 (prev->current_ssid &&
5744 prev->current_ssid->p2p_group))
5745 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5750 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5751 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5755 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5759 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5763 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5764 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5765 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5768 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5769 &size, pref_freq_list);
5770 if (!res && size > 0) {
5773 (!p2p_supported_freq(wpa_s->global->p2p,
5774 pref_freq_list[i]) ||
5775 wpas_p2p_disallowed_freq(wpa_s->global,
5776 pref_freq_list[i]))) {
5777 wpa_printf(MSG_DEBUG,
5778 "P2P: preferred_freq_list[%d]=%d is disallowed",
5779 i, pref_freq_list[i]);
5783 freq = pref_freq_list[i];
5784 wpa_printf(MSG_DEBUG,
5785 "P2P: Using preferred_freq_list[%d]=%d",
5788 wpa_printf(MSG_DEBUG,
5789 "P2P: All driver preferred frequencies are disallowed for P2P use");
5792 wpa_printf(MSG_DEBUG,
5793 "P2P: No preferred frequency list available");
5798 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5800 if (wpa_s->best_24_freq > 0 &&
5801 p2p_supported_freq_go(wpa_s->global->p2p,
5802 wpa_s->best_24_freq)) {
5803 freq = wpa_s->best_24_freq;
5804 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5805 "channel: %d MHz", freq);
5807 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5809 freq = 2412 + (r % 3) * 25;
5810 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5811 "channel: %d MHz", freq);
5816 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5818 if (wpa_s->best_5_freq > 0 &&
5819 p2p_supported_freq_go(wpa_s->global->p2p,
5820 wpa_s->best_5_freq)) {
5821 freq = wpa_s->best_5_freq;
5822 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5823 "channel: %d MHz", freq);
5825 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5827 freq = 5180 + (r % 4) * 20;
5828 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5829 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5830 "5 GHz channel for P2P group");
5833 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5834 "channel: %d MHz", freq);
5838 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5839 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5840 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5841 wpa_s->hw.num_modes)) {
5843 * If freq is a DFS channel and DFS is offloaded to the
5844 * driver, allow P2P GO to use it.
5846 wpa_printf(MSG_DEBUG, "P2P: "
5847 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5851 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5852 "(%u MHz) is not supported for P2P uses",
5861 static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5862 const struct p2p_channels *channels,
5865 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5866 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5867 freq_included(wpa_s, channels, freq))
5873 static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5874 struct p2p_go_neg_results *params,
5875 const struct p2p_channels *channels)
5879 /* try all channels in operating class 115 */
5880 for (i = 0; i < 4; i++) {
5881 params->freq = 5180 + i * 20;
5882 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5883 freq_included(wpa_s, channels, params->freq) &&
5884 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5888 /* try all channels in operating class 124 */
5889 for (i = 0; i < 4; i++) {
5890 params->freq = 5745 + i * 20;
5891 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5892 freq_included(wpa_s, channels, params->freq) &&
5893 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5897 /* try social channel class 180 channel 2 */
5898 params->freq = 58320 + 1 * 2160;
5899 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5900 freq_included(wpa_s, channels, params->freq) &&
5901 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5904 /* try all channels in reg. class 180 */
5905 for (i = 0; i < 4; i++) {
5906 params->freq = 58320 + i * 2160;
5907 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5908 freq_included(wpa_s, channels, params->freq) &&
5909 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5913 /* try some random selection of the social channels */
5914 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5917 for (i = 0; i < 3; i++) {
5918 params->freq = 2412 + ((r + i) % 3) * 25;
5919 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5923 /* try all other channels in operating class 81 */
5924 for (i = 0; i < 11; i++) {
5925 params->freq = 2412 + i * 5;
5927 /* skip social channels; covered in the previous loop */
5928 if (params->freq == 2412 ||
5929 params->freq == 2437 ||
5930 params->freq == 2462)
5933 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5938 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
5941 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5946 static int wpas_same_band(int freq1, int freq2)
5948 enum hostapd_hw_mode mode1, mode2;
5951 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
5952 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
5953 if (mode1 == NUM_HOSTAPD_MODES)
5955 return mode1 == mode2;
5959 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5960 struct p2p_go_neg_results *params,
5961 int freq, int vht_center_freq2, int ht40,
5962 int vht, int max_oper_chwidth, int he,
5963 const struct p2p_channels *channels)
5965 struct wpa_used_freq_data *freqs;
5967 unsigned int num, i;
5968 int ignore_no_freqs = 0;
5969 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
5971 os_memset(params, 0, sizeof(*params));
5972 params->role_go = 1;
5973 params->ht40 = ht40;
5976 params->max_oper_chwidth = max_oper_chwidth;
5977 params->vht_center_freq2 = vht_center_freq2;
5979 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5980 sizeof(struct wpa_used_freq_data));
5984 num = get_shared_radio_freqs_data(wpa_s, freqs,
5985 wpa_s->num_multichan_concurrent);
5987 if (wpa_s->current_ssid &&
5988 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5989 wpa_s->wpa_state == WPA_COMPLETED) {
5990 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5994 * If the frequency selection is done for an active P2P GO that
5995 * is not sharing a frequency, allow to select a new frequency
5996 * even if there are no unused frequencies as we are about to
5997 * move the P2P GO so its frequency can be re-used.
5999 for (i = 0; i < num; i++) {
6000 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6001 freqs[i].flags == 0) {
6002 ignore_no_freqs = 1;
6008 /* try using the forced freq */
6010 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6011 !freq_included(wpa_s, channels, freq)) {
6012 wpa_printf(MSG_DEBUG,
6013 "P2P: Forced GO freq %d MHz disallowed",
6017 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6018 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
6019 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6020 wpa_s->hw.num_modes)) {
6022 * If freq is a DFS channel and DFS is offloaded
6023 * to the driver, allow P2P GO to use it.
6025 wpa_printf(MSG_DEBUG,
6026 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6029 wpa_printf(MSG_DEBUG,
6030 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6036 for (i = 0; i < num; i++) {
6037 if (freqs[i].freq == freq) {
6038 wpa_printf(MSG_DEBUG,
6039 "P2P: forced freq (%d MHz) is also shared",
6041 params->freq = freq;
6046 if (!ignore_no_freqs && !unused_channels) {
6047 wpa_printf(MSG_DEBUG,
6048 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6053 wpa_printf(MSG_DEBUG,
6054 "P2P: force GO freq (%d MHz) on a free channel",
6056 params->freq = freq;
6060 /* consider using one of the shared frequencies */
6062 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
6063 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6064 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6065 wpa_printf(MSG_DEBUG,
6066 "P2P: Use shared freq (%d MHz) for GO",
6068 params->freq = cand;
6072 /* try using one of the shared freqs */
6073 for (i = 0; i < num; i++) {
6074 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6076 wpa_printf(MSG_DEBUG,
6077 "P2P: Use shared freq (%d MHz) for GO",
6079 params->freq = freqs[i].freq;
6085 if (!ignore_no_freqs && !unused_channels) {
6086 wpa_printf(MSG_DEBUG,
6087 "P2P: Cannot force GO on any of the channels we are already using");
6091 /* try using the setting from the configuration file */
6092 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6093 wpa_s->conf->p2p_oper_channel >= 1 &&
6094 wpa_s->conf->p2p_oper_channel <= 11 &&
6095 wpas_p2p_supported_freq_go(
6097 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6098 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6099 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6100 "frequency %d MHz", params->freq);
6104 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6105 wpa_s->conf->p2p_oper_reg_class == 116 ||
6106 wpa_s->conf->p2p_oper_reg_class == 117 ||
6107 wpa_s->conf->p2p_oper_reg_class == 124 ||
6108 wpa_s->conf->p2p_oper_reg_class == 125 ||
6109 wpa_s->conf->p2p_oper_reg_class == 126 ||
6110 wpa_s->conf->p2p_oper_reg_class == 127) &&
6111 wpas_p2p_supported_freq_go(wpa_s, channels,
6113 5 * wpa_s->conf->p2p_oper_channel)) {
6114 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6115 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6116 "frequency %d MHz", params->freq);
6120 /* Try using best channels */
6121 if (wpa_s->conf->p2p_oper_channel == 0 &&
6122 wpa_s->best_overall_freq > 0 &&
6123 wpas_p2p_supported_freq_go(wpa_s, channels,
6124 wpa_s->best_overall_freq)) {
6125 params->freq = wpa_s->best_overall_freq;
6126 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6127 "channel %d MHz", params->freq);
6131 if (wpa_s->conf->p2p_oper_channel == 0 &&
6132 wpa_s->best_24_freq > 0 &&
6133 wpas_p2p_supported_freq_go(wpa_s, channels,
6134 wpa_s->best_24_freq)) {
6135 params->freq = wpa_s->best_24_freq;
6136 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6137 "channel %d MHz", params->freq);
6141 if (wpa_s->conf->p2p_oper_channel == 0 &&
6142 wpa_s->best_5_freq > 0 &&
6143 wpas_p2p_supported_freq_go(wpa_s, channels,
6144 wpa_s->best_5_freq)) {
6145 params->freq = wpa_s->best_5_freq;
6146 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6147 "channel %d MHz", params->freq);
6151 /* try using preferred channels */
6152 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6153 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6154 params->freq = cand;
6155 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6156 "channels", params->freq);
6160 /* Try using a channel that allows VHT to be used with 80 MHz */
6161 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6162 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6163 enum hostapd_hw_mode mode;
6164 struct hostapd_hw_modes *hwmode;
6167 cand = wpa_s->p2p_group_common_freqs[i];
6168 mode = ieee80211_freq_to_chan(cand, &chan);
6169 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6172 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6175 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6176 params->freq = cand;
6177 wpa_printf(MSG_DEBUG,
6178 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6185 /* Try using a channel that allows HT to be used with 40 MHz on the same
6186 * band so that CSA can be used */
6187 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6188 wpa_s->p2p_group_common_freqs) {
6189 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6190 enum hostapd_hw_mode mode;
6191 struct hostapd_hw_modes *hwmode;
6194 cand = wpa_s->p2p_group_common_freqs[i];
6195 mode = ieee80211_freq_to_chan(cand, &chan);
6196 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6198 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6201 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6202 BW40MINUS) != ALLOWED &&
6203 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6204 BW40PLUS) != ALLOWED))
6206 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6207 params->freq = cand;
6208 wpa_printf(MSG_DEBUG,
6209 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6216 /* Try using one of the group common freqs on the same band so that CSA
6218 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6219 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6220 cand = wpa_s->p2p_group_common_freqs[i];
6221 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6224 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6225 params->freq = cand;
6226 wpa_printf(MSG_DEBUG,
6227 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6234 /* Try using one of the group common freqs */
6235 if (wpa_s->p2p_group_common_freqs) {
6236 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6237 cand = wpa_s->p2p_group_common_freqs[i];
6238 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6239 params->freq = cand;
6240 wpa_printf(MSG_DEBUG,
6241 "P2P: Use freq %d MHz common with the peer",
6248 /* no preference, select some channel */
6249 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6251 if (params->freq == 0) {
6252 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6265 static struct wpa_supplicant *
6266 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6269 struct wpa_supplicant *group_wpa_s;
6271 if (!wpas_p2p_create_iface(wpa_s)) {
6272 if (wpa_s->p2p_mgmt) {
6274 * We may be called on the p2p_dev interface which
6275 * cannot be used for group operations, so always use
6276 * the primary interface.
6278 wpa_s->parent->p2pdev = wpa_s;
6279 wpa_s = wpa_s->parent;
6281 wpa_dbg(wpa_s, MSG_DEBUG,
6282 "P2P: Use primary interface for group operations");
6283 wpa_s->p2p_first_connection_timeout = 0;
6284 if (wpa_s != wpa_s->p2pdev)
6285 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
6289 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
6290 WPA_IF_P2P_CLIENT) < 0) {
6291 wpa_msg_global(wpa_s, MSG_ERROR,
6292 "P2P: Failed to add group interface");
6295 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6296 if (group_wpa_s == NULL) {
6297 wpa_msg_global(wpa_s, MSG_ERROR,
6298 "P2P: Failed to initialize group interface");
6299 wpas_p2p_remove_pending_group_interface(wpa_s);
6303 if (go && wpa_s->p2p_go_do_acs) {
6304 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6305 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6306 wpa_s->p2p_go_do_acs = 0;
6309 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6310 group_wpa_s->ifname);
6311 group_wpa_s->p2p_first_connection_timeout = 0;
6317 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6318 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6319 * @persistent_group: Whether to create a persistent group
6320 * @freq: Frequency for the group or 0 to indicate no hardcoding
6321 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
6322 * @ht40: Start GO with 40 MHz channel width
6323 * @vht: Start GO with VHT support
6324 * @vht_chwidth: channel bandwidth for GO operating with VHT support
6325 * Returns: 0 on success, -1 on failure
6327 * This function creates a new P2P group with the local end as the Group Owner,
6328 * i.e., without using Group Owner Negotiation.
6330 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
6331 int freq, int vht_center_freq2, int ht40, int vht,
6332 int max_oper_chwidth, int he)
6334 struct p2p_go_neg_results params;
6336 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6339 os_free(wpa_s->global->add_psk);
6340 wpa_s->global->add_psk = NULL;
6342 /* Make sure we are not running find during connection establishment */
6343 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
6344 wpas_p2p_stop_find_oper(wpa_s);
6346 if (!wpa_s->p2p_go_do_acs) {
6347 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6352 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, vht_center_freq2,
6353 ht40, vht, max_oper_chwidth, he, NULL))
6356 p2p_go_params(wpa_s->global->p2p, ¶ms);
6357 params.persistent_group = persistent_group;
6359 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6362 wpas_start_wps_go(wpa_s, ¶ms, 0);
6368 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
6369 struct wpa_ssid *params, int addr_allocated,
6370 int freq, int force_scan)
6372 struct wpa_ssid *ssid;
6374 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6378 os_get_reltime(&wpa_s->scan_min_time);
6379 wpa_s->p2p_last_4way_hs_fail = NULL;
6381 wpa_supplicant_ap_deinit(wpa_s);
6383 ssid = wpa_config_add_network(wpa_s->conf);
6386 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6387 wpa_config_set_network_defaults(ssid);
6388 ssid->temporary = 1;
6389 ssid->proto = WPA_PROTO_RSN;
6390 ssid->pbss = params->pbss;
6391 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6393 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
6394 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6395 ssid->ssid = os_malloc(params->ssid_len);
6396 if (ssid->ssid == NULL) {
6397 wpa_config_remove_network(wpa_s->conf, ssid->id);
6400 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6401 ssid->ssid_len = params->ssid_len;
6402 ssid->p2p_group = 1;
6403 ssid->export_keys = 1;
6404 if (params->psk_set) {
6405 os_memcpy(ssid->psk, params->psk, 32);
6408 if (params->passphrase)
6409 ssid->passphrase = os_strdup(params->passphrase);
6411 wpa_s->show_group_started = 1;
6412 wpa_s->p2p_in_invitation = 1;
6413 wpa_s->p2p_invite_go_freq = freq;
6414 wpa_s->p2p_go_group_formation_completed = 0;
6415 wpa_s->global->p2p_group_formation = wpa_s;
6417 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
6419 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6420 wpas_p2p_group_formation_timeout,
6421 wpa_s->p2pdev, NULL);
6422 wpa_supplicant_select_network(wpa_s, ssid);
6428 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6429 struct wpa_ssid *ssid, int addr_allocated,
6430 int force_freq, int neg_freq,
6431 int vht_center_freq2, int ht40,
6432 int vht, int max_oper_chwidth, int he,
6433 const struct p2p_channels *channels,
6434 int connection_timeout, int force_scan)
6436 struct p2p_go_neg_results params;
6439 if (ssid->disabled != 2 || ssid->ssid == NULL)
6442 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6443 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6444 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6447 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6448 wpa_s->p2pdev, NULL)) {
6450 * This can happen if Invitation Response frame was lost
6451 * and the peer (GO of a persistent group) tries to
6452 * invite us again. Reschedule the timeout to avoid
6453 * terminating the wait for the connection too early
6454 * since we now know that the peer is still trying to
6455 * invite us instead of having already started the GO.
6457 wpa_printf(MSG_DEBUG,
6458 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6459 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6460 wpas_p2p_group_formation_timeout,
6461 wpa_s->p2pdev, NULL);
6466 os_free(wpa_s->global->add_psk);
6467 wpa_s->global->add_psk = NULL;
6469 /* Make sure we are not running find during connection establishment */
6470 wpas_p2p_stop_find_oper(wpa_s);
6472 wpa_s->p2p_fallback_to_go_neg = 0;
6474 if (ssid->mode == WPAS_MODE_P2P_GO) {
6475 if (force_freq > 0) {
6476 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6480 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6482 (freq > 0 && !freq_included(wpa_s, channels, freq)))
6485 } else if (ssid->mode == WPAS_MODE_INFRA) {
6487 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
6488 struct os_reltime now;
6489 struct wpa_bss *bss =
6490 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
6492 os_get_reltime(&now);
6494 !os_reltime_expired(&now, &bss->last_update, 5) &&
6495 freq_included(wpa_s, channels, bss->freq))
6501 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6507 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, vht_center_freq2,
6508 ht40, vht, max_oper_chwidth, he, channels))
6512 params.psk_set = ssid->psk_set;
6514 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
6515 if (ssid->passphrase) {
6516 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6517 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6518 "persistent group");
6521 os_strlcpy(params.passphrase, ssid->passphrase,
6522 sizeof(params.passphrase));
6524 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6525 params.ssid_len = ssid->ssid_len;
6526 params.persistent_group = 1;
6528 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6532 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6534 wpa_s->p2p_first_connection_timeout = connection_timeout;
6535 wpas_start_wps_go(wpa_s, ¶ms, 0);
6541 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6542 struct wpabuf *proberesp_ies)
6544 struct wpa_supplicant *wpa_s = ctx;
6545 if (wpa_s->ap_iface) {
6546 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
6547 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6548 wpabuf_free(beacon_ies);
6549 wpabuf_free(proberesp_ies);
6553 wpabuf_free(hapd->p2p_beacon_ie);
6554 hapd->p2p_beacon_ie = beacon_ies;
6556 wpabuf_free(hapd->p2p_probe_resp_ie);
6557 hapd->p2p_probe_resp_ie = proberesp_ies;
6559 wpabuf_free(beacon_ies);
6560 wpabuf_free(proberesp_ies);
6562 wpa_supplicant_ap_update_beacon(wpa_s);
6566 static void wpas_p2p_idle_update(void *ctx, int idle)
6568 struct wpa_supplicant *wpa_s = ctx;
6569 if (!wpa_s->ap_iface)
6571 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
6573 if (wpa_s->global->p2p_fail_on_wps_complete &&
6574 wpa_s->p2p_in_provisioning) {
6575 wpas_p2p_grpform_fail_after_wps(wpa_s);
6578 wpas_p2p_set_group_idle_timeout(wpa_s);
6580 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6584 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6585 struct wpa_ssid *ssid)
6587 struct p2p_group *group;
6588 struct p2p_group_config *cfg;
6590 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6594 cfg = os_zalloc(sizeof(*cfg));
6598 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
6599 cfg->persistent_group = 2;
6600 else if (ssid->p2p_persistent_group)
6601 cfg->persistent_group = 1;
6602 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6603 if (wpa_s->max_stations &&
6604 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6605 cfg->max_clients = wpa_s->max_stations;
6607 cfg->max_clients = wpa_s->conf->max_num_sta;
6608 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6609 cfg->ssid_len = ssid->ssid_len;
6610 cfg->freq = ssid->frequency;
6611 cfg->cb_ctx = wpa_s;
6612 cfg->ie_update = wpas_p2p_ie_update;
6613 cfg->idle_update = wpas_p2p_idle_update;
6614 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6617 group = p2p_group_init(wpa_s->global->p2p, cfg);
6620 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
6621 p2p_group_notif_formation_done(group);
6622 wpa_s->p2p_group = group;
6627 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6630 struct wpa_ssid *ssid = wpa_s->current_ssid;
6632 if (!wpa_s->p2p_in_provisioning) {
6633 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6634 "provisioning not in progress");
6638 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6639 u8 go_dev_addr[ETH_ALEN];
6640 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6641 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6643 /* Clear any stored provisioning info */
6644 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6647 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
6649 wpa_s->p2p_go_group_formation_completed = 1;
6650 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6652 * Use a separate timeout for initial data connection to
6653 * complete to allow the group to be removed automatically if
6654 * something goes wrong in this step before the P2P group idle
6655 * timeout mechanism is taken into use.
6657 wpa_dbg(wpa_s, MSG_DEBUG,
6658 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6659 P2P_MAX_INITIAL_CONN_WAIT);
6660 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6661 wpas_p2p_group_formation_timeout,
6662 wpa_s->p2pdev, NULL);
6663 /* Complete group formation on successful data connection. */
6664 wpa_s->p2p_go_group_formation_completed = 0;
6667 * Use a separate timeout for initial data connection to
6668 * complete to allow the group to be removed automatically if
6669 * the client does not complete data connection successfully.
6671 wpa_dbg(wpa_s, MSG_DEBUG,
6672 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6673 P2P_MAX_INITIAL_CONN_WAIT_GO);
6674 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6675 wpas_p2p_group_formation_timeout,
6676 wpa_s->p2pdev, NULL);
6678 * Complete group formation on first successful data connection
6680 wpa_s->p2p_go_group_formation_completed = 0;
6682 if (wpa_s->global->p2p)
6683 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
6684 wpas_group_formation_completed(wpa_s, 1, 0);
6688 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6689 struct wps_event_fail *fail)
6691 if (!wpa_s->p2p_in_provisioning) {
6692 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6693 "provisioning not in progress");
6697 if (wpa_s->go_params) {
6698 p2p_clear_provisioning_info(
6700 wpa_s->go_params->peer_device_addr);
6703 wpas_notify_p2p_wps_failed(wpa_s, fail);
6705 if (wpa_s == wpa_s->global->p2p_group_formation) {
6707 * Allow some time for the failed WPS negotiation exchange to
6708 * complete, but remove the group since group formation cannot
6709 * succeed after provisioning failure.
6711 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6712 wpa_s->global->p2p_fail_on_wps_complete = 1;
6713 eloop_deplete_timeout(0, 50000,
6714 wpas_p2p_group_formation_timeout,
6715 wpa_s->p2pdev, NULL);
6720 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6722 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6723 !wpa_s->p2p_in_provisioning)
6726 wpas_p2p_grpform_fail_after_wps(wpa_s);
6732 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6733 const char *config_method,
6734 enum wpas_p2p_prov_disc_use use,
6735 struct p2ps_provision *p2ps_prov)
6739 wpa_s->global->pending_p2ps_group = 0;
6740 wpa_s->global->pending_p2ps_group_freq = 0;
6741 wpa_s->p2p_fallback_to_go_neg = 0;
6742 wpa_s->pending_pd_use = NORMAL_PD;
6743 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6744 p2ps_prov->conncap = p2ps_group_capability(
6745 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6746 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6748 wpa_printf(MSG_DEBUG,
6749 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6750 __func__, p2ps_prov->conncap,
6751 p2ps_prov->adv_id, p2ps_prov->conncap,
6752 p2ps_prov->status, p2ps_prov->info);
6755 } else if (os_strncmp(config_method, "display", 7) == 0)
6756 config_methods = WPS_CONFIG_DISPLAY;
6757 else if (os_strncmp(config_method, "keypad", 6) == 0)
6758 config_methods = WPS_CONFIG_KEYPAD;
6759 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6760 os_strncmp(config_method, "pushbutton", 10) == 0)
6761 config_methods = WPS_CONFIG_PUSHBUTTON;
6763 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
6768 if (use == WPAS_P2P_PD_AUTO) {
6769 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6770 wpa_s->pending_pd_config_methods = config_methods;
6771 wpa_s->p2p_auto_pd = 1;
6772 wpa_s->p2p_auto_join = 0;
6773 wpa_s->pending_pd_before_join = 0;
6774 wpa_s->auto_pd_scan_retry = 0;
6775 wpas_p2p_stop_find(wpa_s);
6776 wpa_s->p2p_join_scan_count = 0;
6777 os_get_reltime(&wpa_s->p2p_auto_started);
6778 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6779 wpa_s->p2p_auto_started.sec,
6780 wpa_s->p2p_auto_started.usec);
6781 wpas_p2p_join_scan(wpa_s, NULL);
6785 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6790 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6791 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6796 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6799 return p2p_scan_result_text(ies, ies_len, buf, end);
6803 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6805 if (!offchannel_pending_action_tx(wpa_s))
6808 if (wpa_s->p2p_send_action_work) {
6809 wpas_p2p_free_send_action_work(wpa_s);
6810 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6812 offchannel_send_action_done(wpa_s);
6815 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6816 "operation request");
6817 offchannel_clear_pending_action_tx(wpa_s);
6821 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6822 enum p2p_discovery_type type,
6823 unsigned int num_req_dev_types, const u8 *req_dev_types,
6824 const u8 *dev_id, unsigned int search_delay,
6825 u8 seek_cnt, const char **seek_string, int freq)
6827 wpas_p2p_clear_pending_action_tx(wpa_s);
6828 wpa_s->p2p_long_listen = 0;
6830 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6831 wpa_s->p2p_in_provisioning) {
6832 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6833 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6834 " (P2P disabled)" : "",
6835 wpa_s->p2p_in_provisioning ?
6836 " (p2p_in_provisioning)" : "");
6840 wpa_supplicant_cancel_sched_scan(wpa_s);
6842 return p2p_find(wpa_s->global->p2p, timeout, type,
6843 num_req_dev_types, req_dev_types, dev_id,
6844 search_delay, seek_cnt, seek_string, freq);
6848 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6849 struct wpa_scan_results *scan_res)
6851 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6853 if (wpa_s->p2p_scan_work) {
6854 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6855 wpa_s->p2p_scan_work = NULL;
6856 radio_work_done(work);
6859 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6863 * Indicate that results have been processed so that the P2P module can
6864 * continue pending tasks.
6866 p2p_scan_res_handled(wpa_s->global->p2p);
6870 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
6872 wpas_p2p_clear_pending_action_tx(wpa_s);
6873 wpa_s->p2p_long_listen = 0;
6874 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6875 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6877 if (wpa_s->global->p2p)
6878 p2p_stop_find(wpa_s->global->p2p);
6880 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6881 wpa_printf(MSG_DEBUG,
6882 "P2P: Do not consider the scan results after stop_find");
6883 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6888 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6890 wpas_p2p_stop_find_oper(wpa_s);
6891 if (!wpa_s->global->pending_group_iface_for_p2ps)
6892 wpas_p2p_remove_pending_group_interface(wpa_s);
6896 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6898 struct wpa_supplicant *wpa_s = eloop_ctx;
6899 wpa_s->p2p_long_listen = 0;
6903 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6907 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6910 if (wpa_s->p2p_lo_started) {
6911 wpa_printf(MSG_DEBUG,
6912 "P2P: Cannot start P2P listen, it is offloaded");
6916 wpa_supplicant_cancel_sched_scan(wpa_s);
6917 wpas_p2p_clear_pending_action_tx(wpa_s);
6921 * This is a request for unlimited Listen state. However, at
6922 * least for now, this is mapped to a Listen state for one
6927 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6928 wpa_s->p2p_long_listen = 0;
6931 * Stop previous find/listen operation to avoid trying to request a new
6932 * remain-on-channel operation while the driver is still running the
6935 if (wpa_s->global->p2p)
6936 p2p_stop_find(wpa_s->global->p2p);
6938 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6939 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6940 wpa_s->p2p_long_listen = timeout * 1000;
6941 eloop_register_timeout(timeout, 0,
6942 wpas_p2p_long_listen_timeout,
6950 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6951 u8 *buf, size_t len, int p2p_group)
6953 struct wpabuf *p2p_ie;
6956 if (wpa_s->global->p2p_disabled)
6959 * Advertize mandatory cross connection capability even on
6960 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6962 if (wpa_s->conf->p2p_disabled && p2p_group)
6964 if (wpa_s->global->p2p == NULL)
6969 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6970 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6972 wpabuf_free(p2p_ie);
6978 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
6979 const u8 *dst, const u8 *bssid,
6980 const u8 *ie, size_t ie_len,
6981 unsigned int rx_freq, int ssi_signal)
6983 if (wpa_s->global->p2p_disabled)
6985 if (wpa_s->global->p2p == NULL)
6988 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6989 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
6990 case P2P_PREQ_NOT_P2P:
6991 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6994 case P2P_PREQ_MALFORMED:
6995 case P2P_PREQ_NOT_LISTEN:
6996 case P2P_PREQ_NOT_PROCESSED:
6997 default: /* make gcc happy */
6999 case P2P_PREQ_PROCESSED:
7005 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7006 const u8 *sa, const u8 *bssid,
7007 u8 category, const u8 *data, size_t len, int freq)
7009 if (wpa_s->global->p2p_disabled)
7011 if (wpa_s->global->p2p == NULL)
7014 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7019 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7023 if (wpa_s->global->p2p_disabled)
7025 if (wpa_s->global->p2p == NULL)
7028 bands = wpas_get_bands(wpa_s, NULL);
7029 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
7033 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
7035 p2p_group_deinit(wpa_s->p2p_group);
7036 wpa_s->p2p_group = NULL;
7038 wpa_s->ap_configured_cb = NULL;
7039 wpa_s->ap_configured_cb_ctx = NULL;
7040 wpa_s->ap_configured_cb_data = NULL;
7041 wpa_s->connect_without_scan = NULL;
7045 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7047 wpa_s->p2p_long_listen = 0;
7049 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7052 return p2p_reject(wpa_s->global->p2p, addr);
7056 /* Invite to reinvoke a persistent group */
7057 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7058 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
7059 int vht_center_freq2, int ht40, int vht, int max_chwidth,
7060 int pref_freq, int he)
7062 enum p2p_invite_role role;
7066 int no_pref_freq_given = pref_freq == 0;
7067 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
7069 wpa_s->global->p2p_invite_group = NULL;
7071 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7073 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7075 wpa_s->p2p_persistent_go_freq = freq;
7076 wpa_s->p2p_go_ht40 = !!ht40;
7077 wpa_s->p2p_go_vht = !!vht;
7078 wpa_s->p2p_go_he = !!he;
7079 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7080 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
7081 if (ssid->mode == WPAS_MODE_P2P_GO) {
7082 role = P2P_INVITE_ROLE_GO;
7083 if (peer_addr == NULL) {
7084 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7085 "address in invitation command");
7088 if (wpas_p2p_create_iface(wpa_s)) {
7089 if (wpas_p2p_add_group_interface(wpa_s,
7090 WPA_IF_P2P_GO) < 0) {
7091 wpa_printf(MSG_ERROR, "P2P: Failed to "
7092 "allocate a new interface for the "
7096 bssid = wpa_s->pending_interface_addr;
7097 } else if (wpa_s->p2p_mgmt)
7098 bssid = wpa_s->parent->own_addr;
7100 bssid = wpa_s->own_addr;
7102 role = P2P_INVITE_ROLE_CLIENT;
7103 peer_addr = ssid->bssid;
7105 wpa_s->pending_invite_ssid_id = ssid->id;
7107 size = P2P_MAX_PREF_CHANNELS;
7108 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7109 role == P2P_INVITE_ROLE_GO,
7110 pref_freq_list, &size);
7114 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7117 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7119 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7120 no_pref_freq_given && pref_freq > 0 &&
7121 wpa_s->num_multichan_concurrent > 1 &&
7122 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7123 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7129 * Stop any find/listen operations before invitation and possibly
7130 * connection establishment.
7132 wpas_p2p_stop_find_oper(wpa_s);
7134 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
7135 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
7140 /* Invite to join an active group */
7141 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7142 const u8 *peer_addr, const u8 *go_dev_addr)
7144 struct wpa_global *global = wpa_s->global;
7145 enum p2p_invite_role role;
7147 struct wpa_ssid *ssid;
7149 int freq = 0, force_freq = 0, pref_freq = 0;
7151 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
7153 wpa_s->p2p_persistent_go_freq = 0;
7154 wpa_s->p2p_go_ht40 = 0;
7155 wpa_s->p2p_go_vht = 0;
7156 wpa_s->p2p_go_vht_center_freq2 = 0;
7157 wpa_s->p2p_go_max_oper_chwidth = 0;
7159 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7160 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7163 if (wpa_s == NULL) {
7164 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7168 ssid = wpa_s->current_ssid;
7170 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7175 wpa_s->global->p2p_invite_group = wpa_s;
7176 persistent = ssid->p2p_persistent_group &&
7177 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
7178 ssid->ssid, ssid->ssid_len);
7180 if (ssid->mode == WPAS_MODE_P2P_GO) {
7181 role = P2P_INVITE_ROLE_ACTIVE_GO;
7182 bssid = wpa_s->own_addr;
7183 if (go_dev_addr == NULL)
7184 go_dev_addr = wpa_s->global->p2p_dev_addr;
7185 freq = ssid->frequency;
7187 role = P2P_INVITE_ROLE_CLIENT;
7188 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7189 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7190 "invite to current group");
7193 bssid = wpa_s->bssid;
7194 if (go_dev_addr == NULL &&
7195 !is_zero_ether_addr(wpa_s->go_dev_addr))
7196 go_dev_addr = wpa_s->go_dev_addr;
7197 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7198 (int) wpa_s->assoc_freq;
7200 wpa_s->p2pdev->pending_invite_ssid_id = -1;
7202 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7205 size = P2P_MAX_PREF_CHANNELS;
7206 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7207 role == P2P_INVITE_ROLE_ACTIVE_GO,
7208 pref_freq_list, &size);
7211 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
7213 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
7214 ssid->ssid, ssid->ssid_len, force_freq,
7215 go_dev_addr, persistent, pref_freq, -1);
7219 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7221 struct wpa_ssid *ssid = wpa_s->current_ssid;
7222 u8 go_dev_addr[ETH_ALEN];
7225 u8 ip[3 * 4], *ip_ptr = NULL;
7228 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7229 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7230 wpa_s->p2pdev, NULL);
7233 if (!wpa_s->show_group_started || !ssid)
7236 wpa_s->show_group_started = 0;
7237 if (!wpa_s->p2p_go_group_formation_completed &&
7238 wpa_s->global->p2p_group_formation == wpa_s) {
7239 wpa_dbg(wpa_s, MSG_DEBUG,
7240 "P2P: Marking group formation completed on client on data connection");
7241 wpa_s->p2p_go_group_formation_completed = 1;
7242 wpa_s->global->p2p_group_formation = NULL;
7243 wpa_s->p2p_in_provisioning = 0;
7244 wpa_s->p2p_in_invitation = 0;
7247 os_memset(go_dev_addr, 0, ETH_ALEN);
7248 if (ssid->bssid_set)
7249 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7250 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7252 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7254 if (wpa_s->global->p2p_group_formation == wpa_s)
7255 wpa_s->global->p2p_group_formation = NULL;
7257 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7258 (int) wpa_s->assoc_freq;
7261 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
7264 res = os_snprintf(ip_addr, sizeof(ip_addr),
7265 " ip_addr=%u.%u.%u.%u "
7266 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7267 ip[0], ip[1], ip[2], ip[3],
7268 ip[4], ip[5], ip[6], ip[7],
7269 ip[8], ip[9], ip[10], ip[11]);
7270 if (os_snprintf_error(sizeof(ip_addr), res))
7275 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7276 ssid->passphrase == NULL && ssid->psk_set ?
7278 ssid->passphrase, go_dev_addr, persistent,
7282 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7285 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
7289 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7290 u32 interval1, u32 duration2, u32 interval2)
7294 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7297 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7298 wpa_s->current_ssid == NULL ||
7299 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7302 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7303 wpa_s->own_addr, wpa_s->assoc_freq,
7304 duration1, interval1, duration2, interval2);
7306 wpa_s->waiting_presence_resp = 1;
7312 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7313 unsigned int interval)
7315 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7318 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7322 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7324 if (wpa_s->current_ssid == NULL) {
7326 * current_ssid can be cleared when P2P client interface gets
7327 * disconnected, so assume this interface was used as P2P
7332 return wpa_s->current_ssid->p2p_group &&
7333 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7337 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7339 struct wpa_supplicant *wpa_s = eloop_ctx;
7341 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
7342 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7347 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7349 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
7353 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7357 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7358 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7360 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7363 timeout = wpa_s->conf->p2p_group_idle;
7364 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7365 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7366 timeout = P2P_MAX_CLIENT_IDLE;
7372 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7373 timeout = 0; /* special client mode no-timeout */
7378 if (wpa_s->p2p_in_provisioning) {
7380 * Use the normal group formation timeout during the
7381 * provisioning phase to avoid terminating this process too
7382 * early due to group idle timeout.
7384 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7385 "during provisioning");
7389 if (wpa_s->show_group_started) {
7391 * Use the normal group formation timeout between the end of
7392 * the provisioning phase and completion of 4-way handshake to
7393 * avoid terminating this process too early due to group idle
7396 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7397 "while waiting for initial 4-way handshake to "
7402 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
7404 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7409 /* Returns 1 if the interface was removed */
7410 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7411 u16 reason_code, const u8 *ie, size_t ie_len,
7412 int locally_generated)
7414 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7417 if (!locally_generated)
7418 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7421 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7422 wpa_s->current_ssid &&
7423 wpa_s->current_ssid->p2p_group &&
7424 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7425 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7426 "session is ending");
7427 if (wpas_p2p_group_delete(wpa_s,
7428 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7437 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7438 u16 reason_code, const u8 *ie, size_t ie_len,
7439 int locally_generated)
7441 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7444 if (!locally_generated)
7445 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7450 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7452 struct p2p_data *p2p = wpa_s->global->p2p;
7457 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7460 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7461 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7463 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7464 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7467 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7468 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7470 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7471 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7473 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7474 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7475 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7476 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7477 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7480 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7481 p2p_set_sec_dev_types(p2p,
7482 (void *) wpa_s->conf->sec_device_type,
7483 wpa_s->conf->num_sec_device_types);
7485 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7487 p2p_remove_wps_vendor_extensions(p2p);
7488 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7489 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7491 p2p_add_wps_vendor_extension(
7492 p2p, wpa_s->conf->wps_vendor_ext[i]);
7496 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7497 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7499 country[0] = wpa_s->conf->country[0];
7500 country[1] = wpa_s->conf->country[1];
7502 p2p_set_country(p2p, country);
7505 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7506 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7507 wpa_s->conf->p2p_ssid_postfix ?
7508 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7512 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7513 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
7515 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7516 u8 reg_class, channel;
7521 if (wpa_s->conf->p2p_listen_reg_class &&
7522 wpa_s->conf->p2p_listen_channel) {
7523 reg_class = wpa_s->conf->p2p_listen_reg_class;
7524 channel = wpa_s->conf->p2p_listen_channel;
7529 * Pick one of the social channels randomly as the
7532 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7535 channel = 1 + (r % 3) * 5;
7538 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7541 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7544 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7545 u8 op_reg_class, op_channel, cfg_op_channel;
7548 if (wpa_s->conf->p2p_oper_reg_class &&
7549 wpa_s->conf->p2p_oper_channel) {
7550 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7551 op_channel = wpa_s->conf->p2p_oper_channel;
7556 * Use random operation channel from (1, 6, 11)
7557 *if no other preference is indicated.
7559 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7562 op_channel = 1 + (r % 3) * 5;
7565 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7568 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7572 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7573 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7574 wpa_s->conf->p2p_pref_chan) < 0) {
7575 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7579 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7580 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7585 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7586 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
7590 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7593 if (!wpa_s->ap_iface)
7595 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7600 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7602 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7605 wpa_s->global->cross_connection = enabled;
7606 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7609 struct wpa_supplicant *iface;
7611 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7613 if (iface->cross_connect_enabled == 0)
7616 iface->cross_connect_enabled = 0;
7617 iface->cross_connect_in_use = 0;
7618 wpa_msg_global(iface->p2pdev, MSG_INFO,
7619 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7621 iface->cross_connect_uplink);
7629 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7631 struct wpa_supplicant *iface;
7633 if (!uplink->global->cross_connection)
7636 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7637 if (!iface->cross_connect_enabled)
7639 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7642 if (iface->ap_iface == NULL)
7644 if (iface->cross_connect_in_use)
7647 iface->cross_connect_in_use = 1;
7648 wpa_msg_global(iface->p2pdev, MSG_INFO,
7649 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7650 iface->ifname, iface->cross_connect_uplink);
7655 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7657 struct wpa_supplicant *iface;
7659 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7660 if (!iface->cross_connect_enabled)
7662 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7665 if (!iface->cross_connect_in_use)
7668 wpa_msg_global(iface->p2pdev, MSG_INFO,
7669 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7670 iface->ifname, iface->cross_connect_uplink);
7671 iface->cross_connect_in_use = 0;
7676 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7678 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7679 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7680 wpa_s->cross_connect_disallowed)
7681 wpas_p2p_disable_cross_connect(wpa_s);
7683 wpas_p2p_enable_cross_connect(wpa_s);
7684 if (!wpa_s->ap_iface &&
7685 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7686 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7690 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7692 wpas_p2p_disable_cross_connect(wpa_s);
7693 if (!wpa_s->ap_iface &&
7694 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7696 wpas_p2p_set_group_idle_timeout(wpa_s);
7700 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7702 struct wpa_supplicant *iface;
7704 if (!wpa_s->global->cross_connection)
7707 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7710 if (iface->drv_flags &
7711 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7713 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7714 iface != wpa_s->parent)
7717 wpa_s->cross_connect_enabled = 1;
7718 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7719 sizeof(wpa_s->cross_connect_uplink));
7720 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7721 "%s to %s whenever uplink is available",
7722 wpa_s->ifname, wpa_s->cross_connect_uplink);
7724 if (iface->ap_iface || iface->current_ssid == NULL ||
7725 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7726 iface->cross_connect_disallowed ||
7727 iface->wpa_state != WPA_COMPLETED)
7730 wpa_s->cross_connect_in_use = 1;
7731 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
7732 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7733 wpa_s->ifname, wpa_s->cross_connect_uplink);
7739 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7741 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7742 !wpa_s->p2p_in_provisioning)
7743 return 0; /* not P2P client operation */
7745 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7747 if (wpa_s != wpa_s->p2pdev)
7748 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
7749 wpas_p2p_group_formation_failed(wpa_s, 0);
7754 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7756 struct wpa_supplicant *wpa_s = eloop_ctx;
7757 wpas_p2p_notif_pbc_overlap(wpa_s);
7761 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7762 enum wpas_p2p_channel_update_trig trig)
7764 struct p2p_channels chan, cli_chan;
7765 struct wpa_used_freq_data *freqs = NULL;
7766 unsigned int num = wpa_s->num_multichan_concurrent;
7768 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7771 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7775 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7777 os_memset(&chan, 0, sizeof(chan));
7778 os_memset(&cli_chan, 0, sizeof(cli_chan));
7779 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
7780 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7785 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
7787 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7790 * The used frequencies map changed, so it is possible that a GO is
7791 * using a channel that is no longer valid for P2P use. It is also
7792 * possible that due to policy consideration, it would be preferable to
7793 * move it to a frequency already used by other station interfaces.
7795 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7801 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7802 struct wpa_scan_results *scan_res)
7804 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7808 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7810 struct wpa_global *global = wpa_s->global;
7814 if (global->p2p == NULL)
7817 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7819 if (wpa_s->pending_interface_name[0] &&
7820 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7823 peer = p2p_get_go_neg_peer(global->p2p);
7825 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7826 MACSTR, MAC2STR(peer));
7827 p2p_unauthorize(global->p2p, peer);
7831 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7832 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7833 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7837 if (wpa_s->pending_pd_before_join) {
7838 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7839 wpa_s->pending_pd_before_join = 0;
7843 wpas_p2p_stop_find(wpa_s);
7845 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7846 if (wpa_s == global->p2p_group_formation &&
7847 (wpa_s->p2p_in_provisioning ||
7848 wpa_s->parent->pending_interface_type ==
7849 WPA_IF_P2P_CLIENT)) {
7850 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7851 "formation found - cancelling",
7854 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7855 wpa_s->p2pdev, NULL);
7856 if (wpa_s->p2p_in_provisioning) {
7857 wpas_group_formation_completed(wpa_s, 0, 0);
7860 wpas_p2p_group_delete(wpa_s,
7861 P2P_GROUP_REMOVAL_REQUESTED);
7863 } else if (wpa_s->p2p_in_invitation) {
7864 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7867 wpas_p2p_group_formation_failed(wpa_s, 0);
7873 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7881 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7883 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7886 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7887 "being available anymore");
7888 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
7892 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7893 int freq_24, int freq_5, int freq_overall)
7895 struct p2p_data *p2p = wpa_s->global->p2p;
7898 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7902 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7905 struct p2p_data *p2p = wpa_s->global->p2p;
7910 if (hwaddr_aton(addr, peer))
7913 return p2p_unauthorize(p2p, peer);
7918 * wpas_p2p_disconnect - Disconnect from a P2P Group
7919 * @wpa_s: Pointer to wpa_supplicant data
7920 * Returns: 0 on success, -1 on failure
7922 * This can be used to disconnect from a group in which the local end is a P2P
7923 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7924 * virtual network interface was created for this group, that interface will be
7925 * removed. Otherwise, only the configured P2P group network will be removed
7926 * from the interface.
7928 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7934 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7939 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7943 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7946 ret = p2p_in_progress(wpa_s->global->p2p);
7949 * Check whether there is an ongoing WPS provisioning step (or
7950 * other parts of group formation) on another interface since
7951 * p2p_in_progress() does not report this to avoid issues for
7952 * scans during such provisioning step.
7954 if (wpa_s->global->p2p_group_formation &&
7955 wpa_s->global->p2p_group_formation != wpa_s) {
7956 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7957 "in group formation",
7958 wpa_s->global->p2p_group_formation->ifname);
7963 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
7964 struct os_reltime now;
7965 os_get_reltime(&now);
7966 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7967 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
7968 /* Wait for the first client has expired */
7969 wpa_s->global->p2p_go_wait_client.sec = 0;
7971 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7980 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7981 struct wpa_ssid *ssid)
7983 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7984 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7985 wpa_s->p2pdev, NULL) > 0) {
7987 * Remove the network by scheduling the group formation
7988 * timeout to happen immediately. The teardown code
7989 * needs to be scheduled to run asynch later so that we
7990 * don't delete data from under ourselves unexpectedly.
7991 * Calling wpas_p2p_group_formation_timeout directly
7992 * causes a series of crashes in WPS failure scenarios.
7994 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7995 "P2P group network getting removed");
7996 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7997 wpa_s->p2pdev, NULL);
8002 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
8003 const u8 *addr, const u8 *ssid,
8009 for (s = wpa_s->conf->ssid; s; s = s->next) {
8010 if (s->disabled != 2)
8013 (ssid_len != s->ssid_len ||
8014 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8017 if (s->mode == WPAS_MODE_P2P_GO)
8021 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8022 return s; /* peer is GO in the persistent group */
8023 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8025 for (i = 0; i < s->num_p2p_clients; i++) {
8026 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
8027 addr, ETH_ALEN) == 0)
8028 return s; /* peer is P2P client in persistent
8037 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8040 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8041 wpa_s->p2pdev, NULL) > 0) {
8043 * This can happen if WPS provisioning step is not terminated
8044 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8045 * peer was able to connect, there is no need to time out group
8046 * formation after this, though. In addition, this is used with
8047 * the initial connection wait on the GO as a separate formation
8048 * timeout and as such, expected to be hit after the initial WPS
8049 * provisioning step.
8051 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
8053 if (!wpa_s->p2p_go_group_formation_completed &&
8054 !wpa_s->group_formation_reported) {
8056 * GO has not yet notified group formation success since
8057 * the WPS step was not completed cleanly. Do that
8058 * notification now since the P2P Client was able to
8059 * connect and as such, must have received the
8060 * credential from the WPS step.
8062 if (wpa_s->global->p2p)
8063 p2p_wps_success_cb(wpa_s->global->p2p, addr);
8064 wpas_group_formation_completed(wpa_s, 1, 0);
8067 if (!wpa_s->p2p_go_group_formation_completed) {
8068 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8069 wpa_s->p2p_go_group_formation_completed = 1;
8070 wpa_s->global->p2p_group_formation = NULL;
8071 wpa_s->p2p_in_provisioning = 0;
8072 wpa_s->p2p_in_invitation = 0;
8074 wpa_s->global->p2p_go_wait_client.sec = 0;
8077 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8081 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8084 struct wpa_supplicant *group = wpa_s;
8087 if (wpa_s->global->p2p_group_formation)
8088 group = wpa_s->global->p2p_group_formation;
8089 wpa_s = wpa_s->global->p2p_init_wpa_s;
8090 offchannel_send_action_done(wpa_s);
8092 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
8093 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8094 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8095 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8096 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
8097 wpa_s->p2p_go_vht_center_freq2,
8098 wpa_s->p2p_persistent_id,
8099 wpa_s->p2p_pd_before_go_neg,
8102 wpa_s->p2p_go_max_oper_chwidth,
8103 wpa_s->p2p_go_he, NULL, 0);
8108 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8112 if (!wpa_s->p2p_fallback_to_go_neg ||
8113 wpa_s->p2p_in_provisioning <= 5)
8116 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8117 return 0; /* peer operating as a GO */
8119 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8120 "fallback to GO Negotiation");
8121 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
8122 "reason=GO-not-found");
8123 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
8125 return res == 1 ? 2 : 1;
8129 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8131 struct wpa_supplicant *ifs;
8133 if (wpa_s->wpa_state > WPA_SCANNING) {
8134 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8135 "concurrent operation",
8136 wpa_s->conf->p2p_search_delay);
8137 return wpa_s->conf->p2p_search_delay;
8140 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8142 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
8143 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8144 "delay due to concurrent operation on "
8146 wpa_s->conf->p2p_search_delay,
8148 return wpa_s->conf->p2p_search_delay;
8156 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8157 struct wpa_ssid *s, const u8 *addr,
8160 struct psk_list_entry *psk, *tmp;
8163 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8165 if ((iface_addr && !psk->p2p &&
8166 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8167 (!iface_addr && psk->p2p &&
8168 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8169 wpa_dbg(wpa_s, MSG_DEBUG,
8170 "P2P: Remove persistent group PSK list entry for "
8172 MAC2STR(psk->addr), psk->p2p);
8173 dl_list_del(&psk->list);
8183 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8184 const u8 *p2p_dev_addr,
8185 const u8 *psk, size_t psk_len)
8187 struct wpa_ssid *ssid = wpa_s->current_ssid;
8188 struct wpa_ssid *persistent;
8189 struct psk_list_entry *p, *last;
8191 if (psk_len != sizeof(p->psk))
8195 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8196 " p2p_dev_addr=" MACSTR,
8197 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8198 if (is_zero_ether_addr(p2p_dev_addr))
8199 p2p_dev_addr = NULL;
8201 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8205 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8206 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8207 /* To be added to persistent group once created */
8208 if (wpa_s->global->add_psk == NULL) {
8209 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8210 if (wpa_s->global->add_psk == NULL)
8213 p = wpa_s->global->add_psk;
8216 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8219 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8221 os_memcpy(p->psk, psk, psk_len);
8225 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8226 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8230 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
8233 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8237 p = os_zalloc(sizeof(*p));
8242 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8245 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8247 os_memcpy(p->psk, psk, psk_len);
8249 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8250 (last = dl_list_last(&persistent->psk_list,
8251 struct psk_list_entry, list))) {
8252 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8253 MACSTR " (p2p=%u) to make room for a new one",
8254 MAC2STR(last->addr), last->p2p);
8255 dl_list_del(&last->list);
8259 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
8260 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8261 p2p_dev_addr == NULL);
8263 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8264 MACSTR, MAC2STR(p2p_dev_addr));
8266 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8269 dl_list_add(&persistent->psk_list, &p->list);
8271 if (wpa_s->p2pdev->conf->update_config &&
8272 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
8273 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
8277 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8278 struct wpa_ssid *s, const u8 *addr,
8283 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
8284 if (res > 0 && wpa_s->conf->update_config &&
8285 wpa_config_write(wpa_s->confname, wpa_s->conf))
8286 wpa_dbg(wpa_s, MSG_DEBUG,
8287 "P2P: Failed to update configuration");
8291 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8292 const u8 *peer, int iface_addr)
8294 struct hostapd_data *hapd;
8295 struct hostapd_wpa_psk *psk, *prev, *rem;
8296 struct sta_info *sta;
8298 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8299 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8302 /* Remove per-station PSK entry */
8303 hapd = wpa_s->ap_iface->bss[0];
8305 psk = hapd->conf->ssid.wpa_psk;
8307 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8309 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8310 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8311 MACSTR " iface_addr=%d",
8312 MAC2STR(peer), iface_addr);
8314 prev->next = psk->next;
8316 hapd->conf->ssid.wpa_psk = psk->next;
8326 /* Disconnect from group */
8328 sta = ap_get_sta(hapd, peer);
8330 sta = ap_get_sta_p2p(hapd, peer);
8332 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8333 " (iface_addr=%d) from group",
8334 MAC2STR(peer), iface_addr);
8335 hostapd_drv_sta_deauth(hapd, sta->addr,
8336 WLAN_REASON_DEAUTH_LEAVING);
8337 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8342 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8346 struct wpa_supplicant *w;
8347 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
8349 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8351 /* Remove from any persistent group */
8352 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
8353 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8356 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8357 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
8360 /* Remove from any operating group */
8361 for (w = wpa_s->global->ifaces; w; w = w->next)
8362 wpas_p2p_remove_client_go(w, peer, iface_addr);
8366 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8368 struct wpa_supplicant *wpa_s = eloop_ctx;
8369 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8373 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8375 struct wpa_supplicant *wpa_s = eloop_ctx;
8377 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8378 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8382 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8383 struct wpa_ssid *ssid)
8385 struct wpa_supplicant *iface;
8387 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8388 if (!iface->current_ssid ||
8389 iface->current_ssid->frequency == freq ||
8390 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8391 !iface->current_ssid->p2p_group))
8394 /* Remove the connection with least priority */
8395 if (!wpas_is_p2p_prioritized(iface)) {
8396 /* STA connection has priority over existing
8397 * P2P connection, so remove the interface. */
8398 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8399 eloop_register_timeout(0, 0,
8400 wpas_p2p_group_freq_conflict,
8402 /* If connection in progress is P2P connection, do not
8403 * proceed for the connection. */
8409 /* P2P connection has priority, disable the STA network
8411 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8413 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8414 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8415 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8417 /* If P2P connection is in progress, continue
8430 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8432 struct wpa_ssid *ssid = wpa_s->current_ssid;
8434 if (ssid == NULL || !ssid->p2p_group)
8437 if (wpa_s->p2p_last_4way_hs_fail &&
8438 wpa_s->p2p_last_4way_hs_fail == ssid) {
8439 u8 go_dev_addr[ETH_ALEN];
8440 struct wpa_ssid *persistent;
8442 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8444 ssid->ssid_len) <= 0) {
8445 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8449 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8450 MACSTR, MAC2STR(go_dev_addr));
8451 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
8454 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8455 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8458 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
8459 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8462 wpa_s->p2p_last_4way_hs_fail = NULL;
8464 * Remove the group from a timeout to avoid issues with caller
8465 * continuing to use the interface if this is on a P2P group
8468 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8473 wpa_s->p2p_last_4way_hs_fail = ssid;
8478 #ifdef CONFIG_WPS_NFC
8480 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8488 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8492 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8493 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8500 wpabuf_put_be16(ret, wsc_len);
8502 wpabuf_put_buf(ret, wsc);
8503 wpabuf_put_be16(ret, wpabuf_len(p2p));
8504 wpabuf_put_buf(ret, p2p);
8508 wpa_hexdump_buf(MSG_DEBUG,
8509 "P2P: Generated NFC connection handover message", ret);
8513 tmp = ndef_build_p2p(ret);
8516 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8526 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8527 struct wpa_ssid **ssid, u8 *go_dev_addr)
8529 struct wpa_supplicant *iface;
8532 os_memset(go_dev_addr, 0, ETH_ALEN);
8535 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8536 if (iface->wpa_state < WPA_ASSOCIATING ||
8537 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8538 !iface->current_ssid->p2p_group ||
8539 iface->current_ssid->mode != WPAS_MODE_INFRA)
8542 *ssid = iface->current_ssid;
8544 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8545 return iface->assoc_freq;
8551 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8554 struct wpabuf *wsc, *p2p;
8555 struct wpa_ssid *ssid;
8556 u8 go_dev_addr[ETH_ALEN];
8557 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8559 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8560 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8564 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8565 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8566 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8567 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8571 if (cli_freq == 0) {
8572 wsc = wps_build_nfc_handover_req_p2p(
8573 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8576 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8577 go_dev_addr, ssid ? ssid->ssid : NULL,
8578 ssid ? ssid->ssid_len : 0);
8580 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8584 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8587 struct wpabuf *wsc, *p2p;
8588 struct wpa_ssid *ssid;
8589 u8 go_dev_addr[ETH_ALEN];
8590 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8592 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8595 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8596 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8597 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8600 if (cli_freq == 0) {
8601 wsc = wps_build_nfc_handover_sel_p2p(
8603 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8604 DEV_PW_NFC_CONNECTION_HANDOVER,
8605 wpa_s->conf->wps_nfc_dh_pubkey,
8606 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8609 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8610 go_dev_addr, ssid ? ssid->ssid : NULL,
8611 ssid ? ssid->ssid_len : 0);
8613 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8617 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8618 struct p2p_nfc_params *params)
8620 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8621 "connection handover (freq=%d)",
8624 if (params->go_freq && params->go_ssid_len) {
8625 wpa_s->p2p_wps_method = WPS_NFC;
8626 wpa_s->pending_join_wps_method = WPS_NFC;
8627 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8628 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8630 return wpas_p2p_join_start(wpa_s, params->go_freq,
8632 params->go_ssid_len);
8635 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8636 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
8637 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
8638 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8640 params->go_ssid_len ? params->go_ssid : NULL,
8641 params->go_ssid_len);
8645 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8646 struct p2p_nfc_params *params, int tag)
8648 int res, persistent;
8649 struct wpa_ssid *ssid;
8651 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8652 "connection handover");
8653 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8654 ssid = wpa_s->current_ssid;
8657 if (ssid->mode != WPAS_MODE_P2P_GO)
8659 if (wpa_s->ap_iface == NULL)
8663 if (wpa_s == NULL) {
8664 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8668 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
8669 DEV_PW_NFC_CONNECTION_HANDOVER &&
8670 !wpa_s->p2pdev->p2p_oob_dev_pw) {
8671 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8674 res = wpas_ap_wps_add_nfc_pw(
8675 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8676 wpa_s->p2pdev->p2p_oob_dev_pw,
8677 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8678 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
8683 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8687 if (!params->peer ||
8688 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8691 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8692 " to join", MAC2STR(params->peer->p2p_device_addr));
8694 wpa_s->global->p2p_invite_group = wpa_s;
8695 persistent = ssid->p2p_persistent_group &&
8696 wpas_p2p_get_persistent(wpa_s->p2pdev,
8697 params->peer->p2p_device_addr,
8698 ssid->ssid, ssid->ssid_len);
8699 wpa_s->p2pdev->pending_invite_ssid_id = -1;
8701 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8702 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8703 ssid->ssid, ssid->ssid_len, ssid->frequency,
8704 wpa_s->global->p2p_dev_addr, persistent, 0,
8705 wpa_s->p2pdev->p2p_oob_dev_pw_id);
8709 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8710 struct p2p_nfc_params *params,
8713 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8714 "connection handover");
8715 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8716 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8717 forced_freq, wpa_s->p2p_go_vht_center_freq2,
8718 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8719 wpa_s->p2p_go_he, NULL, 0);
8723 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8724 struct p2p_nfc_params *params,
8729 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8730 "connection handover");
8731 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8732 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8733 forced_freq, wpa_s->p2p_go_vht_center_freq2,
8734 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8735 wpa_s->p2p_go_he, NULL, 0);
8739 res = wpas_p2p_listen(wpa_s, 60);
8741 p2p_unauthorize(wpa_s->global->p2p,
8742 params->peer->p2p_device_addr);
8749 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8750 const struct wpabuf *data,
8751 int sel, int tag, int forced_freq)
8753 const u8 *pos, *end;
8755 struct p2p_nfc_params params;
8758 os_memset(¶ms, 0, sizeof(params));
8761 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8763 pos = wpabuf_head(data);
8764 end = pos + wpabuf_len(data);
8766 if (end - pos < 2) {
8767 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8771 len = WPA_GET_BE16(pos);
8773 if (len > end - pos) {
8774 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8778 params.wsc_attr = pos;
8779 params.wsc_len = len;
8782 if (end - pos < 2) {
8783 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8787 len = WPA_GET_BE16(pos);
8789 if (len > end - pos) {
8790 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8794 params.p2p_attr = pos;
8795 params.p2p_len = len;
8798 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8799 params.wsc_attr, params.wsc_len);
8800 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8801 params.p2p_attr, params.p2p_len);
8803 wpa_hexdump(MSG_DEBUG,
8804 "P2P: Ignored extra data after P2P attributes",
8808 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
8812 if (params.next_step == NO_ACTION)
8815 if (params.next_step == BOTH_GO) {
8816 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8817 MAC2STR(params.peer->p2p_device_addr));
8821 if (params.next_step == PEER_CLIENT) {
8822 if (!is_zero_ether_addr(params.go_dev_addr)) {
8823 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8824 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8826 MAC2STR(params.peer->p2p_device_addr),
8828 MAC2STR(params.go_dev_addr),
8829 wpa_ssid_txt(params.go_ssid,
8830 params.go_ssid_len));
8832 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8833 "peer=" MACSTR " freq=%d",
8834 MAC2STR(params.peer->p2p_device_addr),
8840 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8841 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8842 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8846 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8847 wpa_s->p2p_oob_dev_pw = NULL;
8849 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8850 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8855 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8856 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8857 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8858 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8859 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8860 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8861 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8865 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8866 "peer OOB Device Password Id %u", id);
8869 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8870 "Device Password Id %u", id);
8871 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8872 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8873 params.oob_dev_pw_len -
8874 WPS_OOB_PUBKEY_HASH_LEN - 2);
8875 wpa_s->p2p_oob_dev_pw_id = id;
8876 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8877 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8878 params.oob_dev_pw_len -
8879 WPS_OOB_PUBKEY_HASH_LEN - 2);
8880 if (wpa_s->p2p_oob_dev_pw == NULL)
8883 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8884 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8885 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8888 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8889 "without Device Password");
8890 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8893 switch (params.next_step) {
8897 /* already covered above */
8900 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
8902 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
8904 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
8906 /* TODO: use own OOB Dev Pw */
8907 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
8914 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8915 const struct wpabuf *data, int forced_freq)
8917 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8920 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8924 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8925 const struct wpabuf *req,
8926 const struct wpabuf *sel, int forced_freq)
8931 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8934 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8936 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8937 wpabuf_head(req), wpabuf_len(req));
8938 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8939 wpabuf_head(sel), wpabuf_len(sel));
8941 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8942 tmp = ndef_parse_p2p(init ? sel : req);
8944 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8948 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8956 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8959 int go_intent = wpa_s->conf->p2p_go_intent;
8960 struct wpa_supplicant *iface;
8962 if (wpa_s->global->p2p == NULL)
8966 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8967 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8969 if (!iface->ap_iface)
8971 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8973 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8975 if (wpa_s->p2p_nfc_tag_enabled)
8976 wpas_p2p_remove_pending_group_interface(wpa_s);
8977 wpa_s->p2p_nfc_tag_enabled = 0;
8981 if (wpa_s->global->p2p_disabled)
8984 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8985 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8986 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8987 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8988 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8989 "to allow static handover cases");
8993 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8995 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8996 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8997 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8998 if (wpa_s->p2p_oob_dev_pw == NULL)
9000 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9002 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9003 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9005 * P2P Group Interface present and the command came on group
9006 * interface, so enable the token for the current interface.
9008 wpa_s->create_p2p_iface = 0;
9010 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9013 if (wpa_s->create_p2p_iface) {
9014 enum wpa_driver_if_type iftype;
9015 /* Prepare to add a new interface for the group */
9016 iftype = WPA_IF_P2P_GROUP;
9017 if (go_intent == 15)
9018 iftype = WPA_IF_P2P_GO;
9019 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9020 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9021 "interface for the group");
9025 if_addr = wpa_s->pending_interface_addr;
9026 } else if (wpa_s->p2p_mgmt)
9027 if_addr = wpa_s->parent->own_addr;
9029 if_addr = wpa_s->own_addr;
9031 wpa_s->p2p_nfc_tag_enabled = enabled;
9033 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9034 struct hostapd_data *hapd;
9035 if (iface->ap_iface == NULL)
9037 hapd = iface->ap_iface->bss[0];
9038 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9039 hapd->conf->wps_nfc_dh_pubkey =
9040 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9041 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9042 hapd->conf->wps_nfc_dh_privkey =
9043 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9044 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9045 hapd->conf->wps_nfc_dev_pw =
9046 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9047 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9049 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9050 wpa_dbg(iface, MSG_DEBUG,
9051 "P2P: Failed to enable NFC Tag for GO");
9054 p2p_set_authorized_oob_dev_pw_id(
9055 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9061 #endif /* CONFIG_WPS_NFC */
9064 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9065 struct wpa_used_freq_data *freqs,
9068 u8 curr_chan, cand, chan;
9072 * If possible, optimize the Listen channel to be a channel that is
9073 * already used by one of the other interfaces.
9075 if (!wpa_s->conf->p2p_optimize_listen_chan)
9078 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9081 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9082 for (i = 0, cand = 0; i < num; i++) {
9083 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9084 if (curr_chan == chan) {
9089 if (chan == 1 || chan == 6 || chan == 11)
9094 wpa_dbg(wpa_s, MSG_DEBUG,
9095 "P2P: Update Listen channel to %u based on operating channel",
9097 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9102 static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
9104 struct hostapd_config *conf;
9105 struct p2p_go_neg_results params;
9106 struct csa_settings csa_settings;
9107 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9108 int old_freq = current_ssid->frequency;
9111 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9112 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9117 * TODO: This function may not always work correctly. For example,
9118 * when we have a running GO and a BSS on a DFS channel.
9120 if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, 0, 0, 0, NULL)) {
9121 wpa_dbg(wpa_s, MSG_DEBUG,
9122 "P2P CSA: Failed to select new frequency for GO");
9126 if (current_ssid->frequency == params.freq) {
9127 wpa_dbg(wpa_s, MSG_DEBUG,
9128 "P2P CSA: Selected same frequency - not moving GO");
9132 conf = hostapd_config_defaults();
9134 wpa_dbg(wpa_s, MSG_DEBUG,
9135 "P2P CSA: Failed to allocate default config");
9139 current_ssid->frequency = params.freq;
9140 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9141 wpa_dbg(wpa_s, MSG_DEBUG,
9142 "P2P CSA: Failed to create new GO config");
9147 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9148 wpa_dbg(wpa_s, MSG_DEBUG,
9149 "P2P CSA: CSA to a different band is not supported");
9154 os_memset(&csa_settings, 0, sizeof(csa_settings));
9155 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9156 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9157 csa_settings.freq_params.freq = params.freq;
9158 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9159 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9160 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9162 if (conf->ieee80211ac) {
9163 int freq1 = 0, freq2 = 0;
9166 if (ieee80211_freq_to_channel_ext(params.freq,
9167 conf->secondary_channel,
9168 conf->vht_oper_chwidth,
9170 NUM_HOSTAPD_MODES) {
9171 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9176 if (conf->vht_oper_centr_freq_seg0_idx)
9177 freq1 = ieee80211_chan_to_freq(
9179 conf->vht_oper_centr_freq_seg0_idx);
9181 if (conf->vht_oper_centr_freq_seg1_idx)
9182 freq2 = ieee80211_chan_to_freq(
9184 conf->vht_oper_centr_freq_seg1_idx);
9186 if (freq1 < 0 || freq2 < 0) {
9187 wpa_dbg(wpa_s, MSG_DEBUG,
9188 "P2P CSA: Selected invalid VHT center freqs");
9193 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9194 csa_settings.freq_params.center_freq1 = freq1;
9195 csa_settings.freq_params.center_freq2 = freq2;
9197 switch (conf->vht_oper_chwidth) {
9198 case CHANWIDTH_80MHZ:
9199 case CHANWIDTH_80P80MHZ:
9200 csa_settings.freq_params.bandwidth = 80;
9202 case CHANWIDTH_160MHZ:
9203 csa_settings.freq_params.bandwidth = 160;
9208 ret = ap_switch_channel(wpa_s, &csa_settings);
9210 current_ssid->frequency = old_freq;
9211 hostapd_config_free(conf);
9216 static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9218 struct p2p_go_neg_results params;
9219 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9221 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9223 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9224 current_ssid->frequency);
9226 /* Stop the AP functionality */
9227 /* TODO: Should do this in a way that does not indicated to possible
9228 * P2P Clients in the group that the group is terminated. */
9229 wpa_supplicant_ap_deinit(wpa_s);
9231 /* Reselect the GO frequency */
9232 if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, 0, 0, 0, NULL)) {
9233 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9234 wpas_p2p_group_delete(wpa_s,
9235 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9238 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9242 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9243 wpa_printf(MSG_DEBUG,
9244 "P2P: Selected freq (%u MHz) is not valid for P2P",
9246 wpas_p2p_group_delete(wpa_s,
9247 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9251 /* Update the frequency */
9252 current_ssid->frequency = params.freq;
9253 wpa_s->connect_without_scan = current_ssid;
9254 wpa_s->reassociate = 1;
9255 wpa_s->disconnected = 0;
9256 wpa_supplicant_req_scan(wpa_s, 0, 0);
9260 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9262 struct wpa_supplicant *wpa_s = eloop_ctx;
9264 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
9267 wpas_p2p_go_update_common_freqs(wpa_s);
9269 /* Do not move GO in the middle of a CSA */
9270 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9271 wpa_printf(MSG_DEBUG,
9272 "P2P: CSA is in progress - not moving GO");
9277 * First, try a channel switch flow. If it is not supported or fails,
9278 * take down the GO and bring it up again.
9280 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9281 wpas_p2p_move_go_no_csa(wpa_s);
9285 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9287 struct wpa_supplicant *wpa_s = eloop_ctx;
9288 struct wpa_used_freq_data *freqs = NULL;
9289 unsigned int num = wpa_s->num_multichan_concurrent;
9291 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9295 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9297 /* Previous attempt to move a GO was not possible -- try again. */
9298 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9299 WPAS_P2P_CHANNEL_UPDATE_ANY);
9306 * Consider moving a GO from its currently used frequency:
9307 * 1. It is possible that due to regulatory consideration the frequency
9308 * can no longer be used and there is a need to evacuate the GO.
9309 * 2. It is possible that due to MCC considerations, it would be preferable
9310 * to move the GO to a channel that is currently used by some other
9311 * station interface.
9313 * In case a frequency that became invalid is once again valid, cancel a
9314 * previously initiated GO frequency change.
9316 static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9317 struct wpa_used_freq_data *freqs,
9320 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9321 unsigned int timeout;
9325 wpas_p2p_go_update_common_freqs(wpa_s);
9327 freq = wpa_s->current_ssid->frequency;
9328 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
9329 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
9330 for (i = 0, invalid_freq = 0; i < num; i++) {
9331 if (freqs[i].freq == freq) {
9332 flags = freqs[i].flags;
9334 /* The channel is invalid, must change it */
9335 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9337 wpa_dbg(wpa_s, MSG_DEBUG,
9338 "P2P: Freq=%d MHz no longer valid for GO",
9342 } else if (freqs[i].flags == 0) {
9343 /* Freq is not used by any other station interface */
9345 } else if (!p2p_supported_freq(wpa_s->global->p2p,
9346 freqs[i].freq) && !dfs_offload) {
9347 /* Freq is not valid for P2P use cases */
9349 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9350 P2P_GO_FREQ_MOVE_SCM) {
9352 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9353 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9354 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9356 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9357 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9358 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9359 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9361 } else if ((wpa_s->drv_flags &
9362 WPA_DRIVER_FLAGS_AP_CSA) &&
9363 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9367 * We do not support CSA between bands, so move
9368 * GO only within the same band.
9370 if (wpa_s->ap_iface->current_mode->mode ==
9371 ieee80211_freq_to_chan(freqs[i].freq,
9378 wpa_dbg(wpa_s, MSG_DEBUG,
9379 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9380 invalid_freq, policy_move, flags);
9383 * The channel is valid, or we are going to have a policy move, so
9386 if (!invalid_freq || policy_move) {
9387 wpa_dbg(wpa_s, MSG_DEBUG,
9388 "P2P: Cancel a GO move from freq=%d MHz", freq);
9389 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9391 if (wpas_p2p_in_progress(wpa_s)) {
9392 wpa_dbg(wpa_s, MSG_DEBUG,
9393 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9394 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9396 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9397 wpas_p2p_reconsider_moving_go,
9403 if (!invalid_freq && (!policy_move || flags != 0)) {
9404 wpa_dbg(wpa_s, MSG_DEBUG,
9405 "P2P: Not initiating a GO frequency change");
9410 * Do not consider moving GO if it is in the middle of a CSA. When the
9411 * CSA is finished this flow should be retriggered.
9413 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9414 wpa_dbg(wpa_s, MSG_DEBUG,
9415 "P2P: Not initiating a GO frequency change - CSA is in progress");
9419 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9420 timeout = P2P_GO_FREQ_CHANGE_TIME;
9424 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9426 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9427 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9431 static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9432 struct wpa_used_freq_data *freqs,
9434 enum wpas_p2p_channel_update_trig trig)
9436 struct wpa_supplicant *ifs;
9438 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9442 * Travers all the radio interfaces, and for each GO interface, check
9443 * if there is a need to move the GO from the frequency it is using,
9444 * or in case the frequency is valid again, cancel the evacuation flow.
9446 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9448 if (ifs->current_ssid == NULL ||
9449 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9453 * The GO was just started or completed channel switch, no need
9457 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9458 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9459 wpa_dbg(wpa_s, MSG_DEBUG,
9460 "P2P: GO move - schedule re-consideration");
9461 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9462 wpas_p2p_reconsider_moving_go,
9467 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9472 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9474 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9477 wpas_p2p_update_channel_list(wpa_s,
9478 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9482 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9484 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9485 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9486 "the management interface is being removed");
9487 wpas_p2p_deinit_global(wpa_s->global);
9492 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9494 if (wpa_s->ap_iface->bss)
9495 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9496 wpas_p2p_group_deinit(wpa_s);
9500 int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9501 unsigned int period, unsigned int interval,
9504 struct p2p_data *p2p = wpa_s->global->p2p;
9506 size_t dev_types_len;
9510 if (wpa_s->p2p_lo_started) {
9511 wpa_dbg(wpa_s, MSG_DEBUG,
9512 "P2P Listen offload is already started");
9516 if (wpa_s->global->p2p == NULL ||
9517 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9518 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9522 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9523 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9528 /* Get device type */
9529 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9531 device_types = os_malloc(dev_types_len);
9534 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9535 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9536 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9538 /* Get Probe Response IE(s) */
9539 buf = p2p_build_probe_resp_template(p2p, freq);
9541 os_free(device_types);
9545 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9546 device_types, dev_types_len,
9547 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9549 wpa_dbg(wpa_s, MSG_DEBUG,
9550 "P2P: Failed to start P2P listen offload");
9552 os_free(device_types);
9556 wpa_s->p2p_lo_started = 1;
9558 /* Stop current P2P listen if any */
9559 wpas_stop_listen(wpa_s);
9566 int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9570 if (!wpa_s->p2p_lo_started)
9573 ret = wpa_drv_p2p_lo_stop(wpa_s);
9575 wpa_dbg(wpa_s, MSG_DEBUG,
9576 "P2P: Failed to stop P2P listen offload");
9578 wpa_s->p2p_lo_started = 0;