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 ret = wpa_drv_scan(wpa_s, params);
311 wpa_scan_free_params(params);
314 radio_work_done(work);
315 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
319 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
320 os_get_reltime(&wpa_s->scan_trigger_time);
321 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
322 wpa_s->own_scan_requested = 1;
323 wpa_s->p2p_scan_work = work;
327 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
330 if (wpa_s->global->p2p_24ghz_social_channels &&
331 (freq == 2412 || freq == 2437 || freq == 2462)) {
333 * Search all social channels regardless of whether these have
334 * been disabled for P2P operating channel use to avoid missing
339 return p2p_supported_freq(wpa_s->global->p2p, freq);
343 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
344 unsigned int num_req_dev_types,
345 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
347 struct wpa_supplicant *wpa_s = ctx;
348 struct wpa_driver_scan_params *params = NULL;
349 struct wpabuf *wps_ie, *ies;
350 unsigned int num_channels = 0;
351 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
355 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
358 if (wpa_s->p2p_scan_work) {
359 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
363 params = os_zalloc(sizeof(*params));
367 /* P2P Wildcard SSID */
368 params->num_ssids = 1;
369 n = os_malloc(P2P_WILDCARD_SSID_LEN);
372 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
373 params->ssids[0].ssid = n;
374 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
376 wpa_s->wps->dev.p2p = 1;
377 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
378 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
379 num_req_dev_types, req_dev_types);
383 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
384 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
389 wpabuf_put_buf(ies, wps_ie);
392 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
394 params->p2p_probe = 1;
395 n = os_malloc(wpabuf_len(ies));
400 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
401 params->extra_ies = n;
402 params->extra_ies_len = wpabuf_len(ies);
406 case P2P_SCAN_SOCIAL:
407 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
409 if (params->freqs == NULL)
411 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
412 if (wpas_p2p_search_social_channel(
413 wpa_s, social_channels_freq[i]))
414 params->freqs[num_channels++] =
415 social_channels_freq[i];
417 params->freqs[num_channels++] = 0;
421 case P2P_SCAN_SPECIFIC:
422 params->freqs = os_calloc(2, sizeof(int));
423 if (params->freqs == NULL)
425 params->freqs[0] = freq;
426 params->freqs[1] = 0;
428 case P2P_SCAN_SOCIAL_PLUS_ONE:
429 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
431 if (params->freqs == NULL)
433 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
434 if (wpas_p2p_search_social_channel(
435 wpa_s, social_channels_freq[i]))
436 params->freqs[num_channels++] =
437 social_channels_freq[i];
439 if (p2p_supported_freq(wpa_s->global->p2p, freq))
440 params->freqs[num_channels++] = freq;
441 params->freqs[num_channels++] = 0;
445 radio_remove_works(wpa_s, "p2p-scan", 0);
446 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
452 wpa_scan_free_params(params);
457 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
459 switch (p2p_group_interface) {
460 case P2P_GROUP_INTERFACE_PENDING:
461 return WPA_IF_P2P_GROUP;
462 case P2P_GROUP_INTERFACE_GO:
463 return WPA_IF_P2P_GO;
464 case P2P_GROUP_INTERFACE_CLIENT:
465 return WPA_IF_P2P_CLIENT;
468 return WPA_IF_P2P_GROUP;
472 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
474 size_t ssid_len, int *go)
478 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
479 for (s = wpa_s->conf->ssid; s; s = s->next) {
480 if (s->disabled != 0 || !s->p2p_group ||
481 s->ssid_len != ssid_len ||
482 os_memcmp(ssid, s->ssid, ssid_len) != 0)
484 if (s->mode == WPAS_MODE_P2P_GO &&
485 s != wpa_s->current_ssid)
488 *go = s->mode == WPAS_MODE_P2P_GO;
497 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
499 struct wpa_supplicant *wpa_s = eloop_ctx;
500 wpa_printf(MSG_DEBUG,
501 "P2P: Complete previously requested removal of %s",
503 wpas_p2p_disconnect(wpa_s);
507 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
508 struct wpa_supplicant *calling_wpa_s)
510 if (calling_wpa_s == wpa_s && wpa_s &&
511 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
513 * The calling wpa_s instance is going to be removed. Do that
514 * from an eloop callback to keep the instance available until
515 * the caller has returned. This my be needed, e.g., to provide
516 * control interface responses on the per-interface socket.
518 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
524 return wpas_p2p_disconnect(wpa_s);
528 /* Determine total number of clients in active groups where we are the GO */
529 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
531 unsigned int count = 0;
534 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
535 for (s = wpa_s->conf->ssid; s; s = s->next) {
536 wpa_printf(MSG_DEBUG,
537 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
538 wpa_s, s, s->disabled, s->p2p_group,
540 if (!s->disabled && s->p2p_group &&
541 s->mode == WPAS_MODE_P2P_GO) {
542 count += p2p_get_group_num_members(
552 static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
554 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
555 !wpa_s->current_ssid->disabled &&
556 wpa_s->current_ssid->p2p_group &&
557 wpa_s->current_ssid->p2p_persistent_group;
561 static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
563 return p2p_is_active_persistent_group(wpa_s) &&
564 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
568 /* Find an interface for a P2P group where we are the GO */
569 static struct wpa_supplicant *
570 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
572 struct wpa_supplicant *save = NULL;
577 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
578 if (!p2p_is_active_persistent_go(wpa_s))
581 /* Prefer a group with connected clients */
582 if (p2p_get_group_num_members(wpa_s->p2p_group))
587 /* No group with connected clients, so pick the one without (if any) */
592 static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
594 return p2p_is_active_persistent_group(wpa_s) &&
595 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
599 /* Find an interface for a P2P group where we are the P2P Client */
600 static struct wpa_supplicant *
601 wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
603 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
604 if (p2p_is_active_persistent_cli(wpa_s))
612 /* Find a persistent group where we are the GO */
613 static struct wpa_ssid *
614 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
618 for (s = wpa_s->conf->ssid; s; s = s->next) {
619 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
627 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
628 unsigned int *force_freq,
629 unsigned int *pref_freq)
631 struct wpa_supplicant *wpa_s = ctx;
633 u8 conncap = P2PS_SETUP_NONE;
634 unsigned int owned_members = 0;
635 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
636 struct wpa_ssid *persistent_go;
637 int p2p_no_group_iface;
638 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
640 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
647 size = P2P_MAX_PREF_CHANNELS;
648 if (force_freq && pref_freq &&
649 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
650 (int *) pref_freq, 0, pref_freq_list, &size))
651 wpas_p2p_set_own_freq_preference(wpa_s,
652 *force_freq ? *force_freq :
656 * For non-concurrent capable devices:
657 * If persistent_go, then no new.
658 * If GO, then no client.
659 * If client, then no GO.
661 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
663 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
664 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
665 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
666 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
668 wpa_printf(MSG_DEBUG,
669 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
670 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
672 /* If not concurrent, restrict our choices */
673 if (p2p_no_group_iface) {
674 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
677 return P2PS_SETUP_NONE;
680 if (role == P2PS_SETUP_CLIENT ||
681 incoming == P2PS_SETUP_GROUP_OWNER ||
682 p2p_client_limit_reached(go_wpa_s->p2p_group))
683 return P2PS_SETUP_NONE;
685 return P2PS_SETUP_GROUP_OWNER;
689 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
691 return P2PS_SETUP_GROUP_OWNER |
693 if (incoming == P2PS_SETUP_NEW) {
696 if (os_get_random(&r, sizeof(r)) < 0 ||
698 return P2PS_SETUP_CLIENT;
699 return P2PS_SETUP_GROUP_OWNER;
705 /* If a required role has been specified, handle it here */
706 if (role && role != P2PS_SETUP_NEW) {
708 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
709 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
711 * Peer has an active GO, so if the role allows it and
712 * we do not have any active roles, become client.
714 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
716 return P2PS_SETUP_CLIENT;
720 case P2PS_SETUP_NONE:
725 case P2PS_SETUP_GROUP_OWNER:
727 * Must be a complimentary role - cannot be a client to
728 * more than one peer.
730 if (incoming == role || cli_wpa_s)
731 return P2PS_SETUP_NONE;
733 return P2PS_SETUP_CLIENT;
735 case P2PS_SETUP_CLIENT:
736 /* Must be a complimentary role */
737 if (incoming != role) {
738 conncap = P2PS_SETUP_GROUP_OWNER;
743 return P2PS_SETUP_NONE;
748 * For now, we only will support ownership of one group, and being a
749 * client of one group. Therefore, if we have either an existing GO
750 * group, or an existing client group, we will not do a new GO
751 * negotiation, but rather try to re-use the existing groups.
754 case P2PS_SETUP_NONE:
757 conncap = P2PS_SETUP_GROUP_OWNER;
758 else if (!owned_members)
759 conncap = P2PS_SETUP_NEW;
760 else if (incoming == P2PS_SETUP_NONE)
761 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
763 conncap = P2PS_SETUP_CLIENT;
766 case P2PS_SETUP_CLIENT:
767 conncap = P2PS_SETUP_GROUP_OWNER;
770 case P2PS_SETUP_GROUP_OWNER:
772 conncap = P2PS_SETUP_CLIENT;
775 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
776 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
778 * Peer has an active GO, so if the role allows it and
779 * we do not have any active roles, become client.
781 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s && !cli_wpa_s)
782 return P2PS_SETUP_CLIENT;
785 conncap = P2PS_SETUP_GROUP_OWNER;
789 if (os_get_random(&r, sizeof(r)) < 0 ||
791 conncap = P2PS_SETUP_CLIENT;
793 conncap = P2PS_SETUP_GROUP_OWNER;
798 return P2PS_SETUP_NONE;
802 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
803 (!incoming && (conncap & P2PS_SETUP_NEW))) {
804 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
805 conncap &= ~P2PS_SETUP_GROUP_OWNER;
807 s = wpas_p2p_get_persistent_go(wpa_s);
808 if (!s && !go_wpa_s && p2p_no_group_iface) {
809 p2p_set_intended_addr(wpa_s->global->p2p,
811 } else if (!s && !go_wpa_s) {
812 if (wpas_p2p_add_group_interface(wpa_s,
813 WPA_IF_P2P_GO) < 0) {
814 wpa_printf(MSG_ERROR,
815 "P2P: Failed to allocate a new interface for the group");
816 return P2PS_SETUP_NONE;
818 wpa_s->global->pending_group_iface_for_p2ps = 1;
819 p2p_set_intended_addr(wpa_s->global->p2p,
820 wpa_s->pending_interface_addr);
828 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
829 enum p2p_group_removal_reason removal_reason)
831 struct wpa_ssid *ssid;
835 ssid = wpa_s->current_ssid;
838 * The current SSID was not known, but there may still be a
839 * pending P2P group interface waiting for provisioning or a
840 * P2P group that is trying to reconnect.
842 ssid = wpa_s->conf->ssid;
844 if (ssid->p2p_group && ssid->disabled != 2)
849 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
851 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
856 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
858 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
859 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
860 wpa_s->reassociate = 0;
861 wpa_s->disconnected = 1;
866 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
867 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
869 if (os_strcmp(gtype, "client") == 0) {
870 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
871 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
873 wpa_printf(MSG_DEBUG,
874 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
875 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
876 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
881 if (wpa_s->cross_connect_in_use) {
882 wpa_s->cross_connect_in_use = 0;
883 wpa_msg_global(wpa_s->parent, MSG_INFO,
884 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
885 wpa_s->ifname, wpa_s->cross_connect_uplink);
887 switch (removal_reason) {
888 case P2P_GROUP_REMOVAL_REQUESTED:
889 reason = " reason=REQUESTED";
891 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
892 reason = " reason=FORMATION_FAILED";
894 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
895 reason = " reason=IDLE";
897 case P2P_GROUP_REMOVAL_UNAVAILABLE:
898 reason = " reason=UNAVAILABLE";
900 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
901 reason = " reason=GO_ENDING_SESSION";
903 case P2P_GROUP_REMOVAL_PSK_FAILURE:
904 reason = " reason=PSK_FAILURE";
906 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
907 reason = " reason=FREQ_CONFLICT";
913 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
914 wpa_msg_global(wpa_s->parent, MSG_INFO,
915 P2P_EVENT_GROUP_REMOVED "%s %s%s",
916 wpa_s->ifname, gtype, reason);
919 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
920 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
921 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
922 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
923 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
924 wpa_s->parent, NULL) > 0) {
925 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
927 wpa_s->p2p_in_provisioning = 0;
928 wpas_p2p_group_formation_failed(wpa_s, 1);
931 wpa_s->p2p_in_invitation = 0;
932 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
933 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
936 * Make sure wait for the first client does not remain active after the
937 * group has been removed.
939 wpa_s->global->p2p_go_wait_client.sec = 0;
941 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
942 struct wpa_global *global;
944 enum wpa_driver_if_type type;
945 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
947 global = wpa_s->global;
948 ifname = os_strdup(wpa_s->ifname);
949 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
950 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
951 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
952 wpa_s = global->ifaces;
954 wpa_drv_if_remove(wpa_s, type, ifname);
959 if (!wpa_s->p2p_go_group_formation_completed) {
960 wpa_s->global->p2p_group_formation = NULL;
961 wpa_s->p2p_in_provisioning = 0;
964 wpa_s->show_group_started = 0;
965 os_free(wpa_s->go_params);
966 wpa_s->go_params = NULL;
968 os_free(wpa_s->p2p_group_common_freqs);
969 wpa_s->p2p_group_common_freqs = NULL;
970 wpa_s->p2p_group_common_freqs_num = 0;
972 wpa_s->waiting_presence_resp = 0;
974 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
975 if (ssid && (ssid->p2p_group ||
976 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
977 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
979 if (ssid == wpa_s->current_ssid) {
980 wpa_sm_set_config(wpa_s->wpa, NULL);
981 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
982 wpa_s->current_ssid = NULL;
985 * Networks objects created during any P2P activities are not
986 * exposed out as they might/will confuse certain non-P2P aware
987 * applications since these network objects won't behave like
990 * Likewise, we don't send out network removed signals for such
993 wpa_config_remove_network(wpa_s->conf, id);
994 wpa_supplicant_clear_status(wpa_s);
995 wpa_supplicant_cancel_sched_scan(wpa_s);
997 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1000 if (wpa_s->ap_iface)
1001 wpa_supplicant_ap_deinit(wpa_s);
1003 wpa_drv_deinit_p2p_cli(wpa_s);
1005 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1011 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1013 const u8 *ssid, size_t ssid_len)
1015 struct wpa_bss *bss;
1021 if (wpa_s->go_params)
1022 bssid = wpa_s->go_params->peer_interface_addr;
1024 bssid = wpa_s->bssid;
1026 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
1027 if (bss == NULL && wpa_s->go_params &&
1028 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1029 bss = wpa_bss_get_p2p_dev_addr(
1030 wpa_s, wpa_s->go_params->peer_device_addr);
1032 u8 iface_addr[ETH_ALEN];
1033 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1035 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1038 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1039 "group is persistent - BSS " MACSTR " not found",
1044 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1046 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1047 P2P_IE_VENDOR_TYPE);
1049 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1050 "group is persistent - BSS " MACSTR
1051 " did not include P2P IE", MAC2STR(bssid));
1052 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1053 (u8 *) (bss + 1), bss->ie_len);
1054 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1055 ((u8 *) bss + 1) + bss->ie_len,
1056 bss->beacon_ie_len);
1060 group_capab = p2p_get_group_capab(p2p);
1061 addr = p2p_get_go_dev_addr(p2p);
1062 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1063 "group_capab=0x%x", group_capab);
1065 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1066 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1069 os_memset(go_dev_addr, 0, ETH_ALEN);
1072 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1073 "go_dev_addr=" MACSTR,
1074 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1076 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1080 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1081 struct wpa_ssid *ssid,
1082 const u8 *go_dev_addr)
1087 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1088 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1089 for (s = wpa_s->conf->ssid; s; s = s->next) {
1090 if (s->disabled == 2 &&
1091 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1092 s->ssid_len == ssid->ssid_len &&
1093 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1098 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1100 if (ssid->passphrase && !s->passphrase)
1102 else if (ssid->passphrase && s->passphrase &&
1103 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1106 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1109 s = wpa_config_add_network(wpa_s->conf);
1114 * Instead of network_added we emit persistent_group_added
1115 * notification. Also to keep the defense checks in
1116 * persistent_group obj registration method, we set the
1117 * relevant flags in s to designate it as a persistent group.
1120 s->p2p_persistent_group = 1;
1121 wpas_notify_persistent_group_added(wpa_s, s);
1122 wpa_config_set_network_defaults(s);
1126 s->p2p_persistent_group = 1;
1129 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1130 s->mode = ssid->mode;
1131 s->auth_alg = WPA_AUTH_ALG_OPEN;
1132 s->key_mgmt = WPA_KEY_MGMT_PSK;
1133 s->proto = WPA_PROTO_RSN;
1134 s->pairwise_cipher = WPA_CIPHER_CCMP;
1136 if (ssid->passphrase) {
1137 os_free(s->passphrase);
1138 s->passphrase = os_strdup(ssid->passphrase);
1140 if (ssid->psk_set) {
1142 os_memcpy(s->psk, ssid->psk, 32);
1144 if (s->passphrase && !s->psk_set)
1145 wpa_config_update_psk(s);
1146 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1148 s->ssid = os_malloc(ssid->ssid_len);
1151 s->ssid_len = ssid->ssid_len;
1152 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1154 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1155 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1156 wpa_s->global->add_psk = NULL;
1160 if (changed && wpa_s->conf->update_config &&
1161 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1162 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1169 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1172 struct wpa_ssid *ssid, *s;
1176 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
1178 ssid = wpa_s->current_ssid;
1179 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1180 !ssid->p2p_persistent_group)
1183 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
1184 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1187 if (s->ssid_len == ssid->ssid_len &&
1188 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1195 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
1196 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
1200 if (i == s->num_p2p_clients - 1)
1201 return; /* already the most recent entry */
1203 /* move the entry to mark it most recent */
1204 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1205 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1206 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
1207 os_memcpy(s->p2p_client_list +
1208 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1210 os_memset(s->p2p_client_list +
1211 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1217 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1218 n = os_realloc_array(s->p2p_client_list,
1219 s->num_p2p_clients + 1, 2 * ETH_ALEN);
1222 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1224 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1226 s->p2p_client_list = n;
1227 s->num_p2p_clients++;
1228 } else if (!found && s->p2p_client_list) {
1229 /* Not enough room for an additional entry - drop the oldest
1231 os_memmove(s->p2p_client_list,
1232 s->p2p_client_list + 2 * ETH_ALEN,
1233 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1234 os_memcpy(s->p2p_client_list +
1235 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1237 os_memset(s->p2p_client_list +
1238 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1242 if (p2p_wpa_s->conf->update_config &&
1243 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
1244 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1248 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1249 int go, struct wpa_ssid *ssid, int freq,
1250 const u8 *psk, const char *passphrase,
1251 const u8 *go_dev_addr, int persistent,
1254 const char *ssid_txt;
1258 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1263 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1267 if (passphrase && passphrase[0] == '\0')
1271 * Include PSK/passphrase only in the control interface message and
1272 * leave it out from the debug log entry.
1274 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1275 P2P_EVENT_GROUP_STARTED
1276 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1278 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1279 psk ? " psk=" : "", psk_txt,
1280 passphrase ? " passphrase=\"" : "",
1281 passphrase ? passphrase : "",
1282 passphrase ? "\"" : "",
1283 MAC2STR(go_dev_addr),
1284 persistent ? " [PERSISTENT]" : "", extra);
1285 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1286 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1287 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1288 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1293 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1294 int success, int already_deleted)
1296 struct wpa_ssid *ssid;
1299 u8 go_dev_addr[ETH_ALEN];
1300 int network_id = -1;
1303 * This callback is likely called for the main interface. Update wpa_s
1304 * to use the group interface if a new interface was created for the
1307 if (wpa_s->global->p2p_group_formation)
1308 wpa_s = wpa_s->global->p2p_group_formation;
1309 if (wpa_s->p2p_go_group_formation_completed) {
1310 wpa_s->global->p2p_group_formation = NULL;
1311 wpa_s->p2p_in_provisioning = 0;
1313 wpa_s->p2p_in_invitation = 0;
1314 wpa_s->group_formation_reported = 1;
1317 wpa_msg_global(wpa_s->parent, MSG_INFO,
1318 P2P_EVENT_GROUP_FORMATION_FAILURE);
1319 wpas_notify_p2p_group_formation_failure(wpa_s, "");
1320 if (already_deleted)
1322 wpas_p2p_group_delete(wpa_s,
1323 P2P_GROUP_REMOVAL_FORMATION_FAILED);
1327 wpa_msg_global(wpa_s->parent, MSG_INFO,
1328 P2P_EVENT_GROUP_FORMATION_SUCCESS);
1330 ssid = wpa_s->current_ssid;
1331 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1332 ssid->mode = WPAS_MODE_P2P_GO;
1333 p2p_group_notif_formation_done(wpa_s->p2p_group);
1334 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1339 client = ssid->mode == WPAS_MODE_INFRA;
1340 if (ssid->mode == WPAS_MODE_P2P_GO) {
1341 persistent = ssid->p2p_persistent_group;
1342 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1345 persistent = wpas_p2p_persistent_group(wpa_s,
1350 client = wpa_s->p2p_group_interface ==
1351 P2P_GROUP_INTERFACE_CLIENT;
1352 os_memset(go_dev_addr, 0, ETH_ALEN);
1355 wpa_s->show_group_started = 0;
1358 * Indicate event only after successfully completed 4-way
1359 * handshake, i.e., when the interface is ready for data
1362 wpa_s->show_group_started = 1;
1364 wpas_p2p_group_started(wpa_s, 1, ssid,
1365 ssid ? ssid->frequency : 0,
1366 ssid && ssid->passphrase == NULL &&
1367 ssid->psk_set ? ssid->psk : NULL,
1368 ssid ? ssid->passphrase : NULL,
1369 go_dev_addr, persistent, "");
1370 wpas_p2p_cross_connect_setup(wpa_s);
1371 wpas_p2p_set_group_idle_timeout(wpa_s);
1375 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1378 os_free(wpa_s->global->add_psk);
1379 wpa_s->global->add_psk = NULL;
1381 if (network_id < 0 && ssid)
1382 network_id = ssid->id;
1384 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1385 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1390 struct send_action_work {
1396 unsigned int wait_time;
1401 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1404 struct wpa_supplicant *wpa_s = eloop_ctx;
1406 if (!wpa_s->p2p_send_action_work)
1409 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1410 os_free(wpa_s->p2p_send_action_work->ctx);
1411 radio_work_done(wpa_s->p2p_send_action_work);
1412 wpa_s->p2p_send_action_work = NULL;
1416 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1418 if (wpa_s->p2p_send_action_work) {
1419 struct send_action_work *awork;
1420 awork = wpa_s->p2p_send_action_work->ctx;
1421 if (awork->wait_time == 0) {
1423 radio_work_done(wpa_s->p2p_send_action_work);
1424 wpa_s->p2p_send_action_work = NULL;
1427 * In theory, this should not be needed, but number of
1428 * places in the P2P code is still using non-zero wait
1429 * time for the last Action frame in the sequence and
1430 * some of these do not call send_action_done().
1432 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1434 eloop_register_timeout(
1435 0, awork->wait_time * 1000,
1436 wpas_p2p_send_action_work_timeout,
1443 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1445 const u8 *dst, const u8 *src,
1447 const u8 *data, size_t data_len,
1448 enum offchannel_send_action_result
1451 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1453 wpas_p2p_action_tx_clear(wpa_s);
1455 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1459 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1460 res = P2P_SEND_ACTION_SUCCESS;
1462 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1463 res = P2P_SEND_ACTION_NO_ACK;
1465 case OFFCHANNEL_SEND_ACTION_FAILED:
1466 res = P2P_SEND_ACTION_FAILED;
1470 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1472 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1473 wpa_s->pending_pd_before_join &&
1474 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1475 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1476 wpa_s->p2p_fallback_to_go_neg) {
1477 wpa_s->pending_pd_before_join = 0;
1478 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1479 "during p2p_connect-auto");
1480 wpa_msg_global(wpa_s->parent, MSG_INFO,
1481 P2P_EVENT_FALLBACK_TO_GO_NEG
1482 "reason=no-ACK-to-PD-Req");
1483 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1489 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1491 struct wpa_supplicant *wpa_s = work->wpa_s;
1492 struct send_action_work *awork = work->ctx;
1495 if (work->started) {
1496 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1498 wpa_s->p2p_send_action_work = NULL;
1499 offchannel_send_action_done(wpa_s);
1505 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1506 awork->bssid, awork->buf, awork->len,
1508 wpas_p2p_send_action_tx_status, 1) < 0) {
1510 radio_work_done(work);
1513 wpa_s->p2p_send_action_work = work;
1517 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1518 unsigned int freq, const u8 *dst,
1519 const u8 *src, const u8 *bssid, const u8 *buf,
1520 size_t len, unsigned int wait_time)
1522 struct send_action_work *awork;
1524 if (wpa_s->p2p_send_action_work) {
1525 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1529 awork = os_zalloc(sizeof(*awork) + len);
1534 os_memcpy(awork->dst, dst, ETH_ALEN);
1535 os_memcpy(awork->src, src, ETH_ALEN);
1536 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1538 awork->wait_time = wait_time;
1539 os_memcpy(awork->buf, buf, len);
1541 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1542 wpas_send_action_cb, awork) < 0) {
1551 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1552 const u8 *src, const u8 *bssid, const u8 *buf,
1553 size_t len, unsigned int wait_time)
1555 struct wpa_supplicant *wpa_s = ctx;
1556 int listen_freq = -1, send_freq = -1;
1558 if (wpa_s->p2p_listen_work)
1559 listen_freq = wpa_s->p2p_listen_work->freq;
1560 if (wpa_s->p2p_send_action_work)
1561 send_freq = wpa_s->p2p_send_action_work->freq;
1562 if (listen_freq != (int) freq && send_freq != (int) freq) {
1563 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1564 listen_freq, send_freq);
1565 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1569 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1570 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1572 wpas_p2p_send_action_tx_status, 1);
1576 static void wpas_send_action_done(void *ctx)
1578 struct wpa_supplicant *wpa_s = ctx;
1580 if (wpa_s->p2p_send_action_work) {
1581 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1583 os_free(wpa_s->p2p_send_action_work->ctx);
1584 radio_work_done(wpa_s->p2p_send_action_work);
1585 wpa_s->p2p_send_action_work = NULL;
1588 offchannel_send_action_done(wpa_s);
1592 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1593 struct p2p_go_neg_results *params)
1595 if (wpa_s->go_params == NULL) {
1596 wpa_s->go_params = os_malloc(sizeof(*params));
1597 if (wpa_s->go_params == NULL)
1600 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1605 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1606 struct p2p_go_neg_results *res)
1608 wpa_s->group_formation_reported = 0;
1609 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1610 " dev_addr " MACSTR " wps_method %d",
1611 MAC2STR(res->peer_interface_addr),
1612 MAC2STR(res->peer_device_addr), res->wps_method);
1613 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1614 res->ssid, res->ssid_len);
1615 wpa_supplicant_ap_deinit(wpa_s);
1616 wpas_copy_go_neg_results(wpa_s, res);
1617 if (res->wps_method == WPS_PBC) {
1618 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1619 #ifdef CONFIG_WPS_NFC
1620 } else if (res->wps_method == WPS_NFC) {
1621 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1622 res->peer_interface_addr,
1623 wpa_s->parent->p2p_oob_dev_pw,
1624 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1625 wpa_s->parent->p2p_oob_dev_pw_id ==
1626 DEV_PW_NFC_CONNECTION_HANDOVER ?
1627 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1630 #endif /* CONFIG_WPS_NFC */
1632 u16 dev_pw_id = DEV_PW_DEFAULT;
1633 if (wpa_s->p2p_wps_method == WPS_P2PS)
1634 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1635 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1636 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1637 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1638 wpa_s->p2p_pin, 1, dev_pw_id);
1643 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1644 struct wpa_ssid *ssid)
1646 struct wpa_ssid *persistent;
1647 struct psk_list_entry *psk;
1648 struct hostapd_data *hapd;
1650 if (!wpa_s->ap_iface)
1653 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1655 if (persistent == NULL)
1658 hapd = wpa_s->ap_iface->bss[0];
1660 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1662 struct hostapd_wpa_psk *hpsk;
1664 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1666 MAC2STR(psk->addr), psk->p2p);
1667 hpsk = os_zalloc(sizeof(*hpsk));
1670 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1672 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1674 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1675 hpsk->next = hapd->conf->ssid.wpa_psk;
1676 hapd->conf->ssid.wpa_psk = hpsk;
1681 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1685 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1686 wpa_s->p2p_group_common_freqs_num);
1688 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1689 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1690 i, wpa_s->p2p_group_common_freqs[i]);
1694 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1695 struct p2p_go_neg_results *params)
1697 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1699 wpa_s->p2p_group_common_freqs_num = 0;
1700 os_free(wpa_s->p2p_group_common_freqs);
1701 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1702 if (!wpa_s->p2p_group_common_freqs)
1705 for (i = 0; i < len; i++) {
1706 if (!wpa_s->go_params->freq_list[i])
1708 wpa_s->p2p_group_common_freqs[i] =
1709 wpa_s->go_params->freq_list[i];
1711 wpa_s->p2p_group_common_freqs_num = i;
1715 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1717 #ifndef CONFIG_NO_CONFIG_WRITE
1718 if (wpa_s->parent->conf->update_config &&
1719 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1720 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1721 #endif /* CONFIG_NO_CONFIG_WRITE */
1725 static void p2p_go_configured(void *ctx, void *data)
1727 struct wpa_supplicant *wpa_s = ctx;
1728 struct p2p_go_neg_results *params = data;
1729 struct wpa_ssid *ssid;
1730 int network_id = -1;
1732 p2p_go_save_group_common_freqs(wpa_s, params);
1733 p2p_go_dump_common_freqs(wpa_s);
1735 ssid = wpa_s->current_ssid;
1736 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1737 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1738 if (wpa_s->global->p2p_group_formation == wpa_s)
1739 wpa_s->global->p2p_group_formation = NULL;
1740 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1741 params->passphrase[0] == '\0' ?
1744 wpa_s->global->p2p_dev_addr,
1745 params->persistent_group, "");
1746 wpa_s->group_formation_reported = 1;
1748 if (wpa_s->parent->p2ps_method_config_any) {
1749 if (is_zero_ether_addr(wpa_s->parent->p2ps_join_addr)) {
1750 wpa_dbg(wpa_s, MSG_DEBUG,
1751 "P2PS: Setting default PIN for ANY");
1752 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1753 "12345670", NULL, 0,
1756 wpa_dbg(wpa_s, MSG_DEBUG,
1757 "P2PS: Setting default PIN for " MACSTR,
1758 MAC2STR(wpa_s->parent->p2ps_join_addr));
1759 wpa_supplicant_ap_wps_pin(
1760 wpa_s, wpa_s->parent->p2ps_join_addr,
1761 "12345670", NULL, 0, 0);
1763 wpa_s->parent->p2ps_method_config_any = 0;
1766 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1767 if (params->persistent_group) {
1768 network_id = wpas_p2p_store_persistent_group(
1769 wpa_s->parent, ssid,
1770 wpa_s->global->p2p_dev_addr);
1771 wpas_p2p_add_psk_list(wpa_s, ssid);
1774 network_id = ssid->id;
1775 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1776 wpas_p2p_cross_connect_setup(wpa_s);
1777 wpas_p2p_set_group_idle_timeout(wpa_s);
1779 if (wpa_s->p2p_first_connection_timeout) {
1780 wpa_dbg(wpa_s, MSG_DEBUG,
1781 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1782 wpa_s->p2p_first_connection_timeout);
1783 wpa_s->p2p_go_group_formation_completed = 0;
1784 wpa_s->global->p2p_group_formation = wpa_s;
1785 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1786 wpa_s->parent, NULL);
1787 eloop_register_timeout(
1788 wpa_s->p2p_first_connection_timeout, 0,
1789 wpas_p2p_group_formation_timeout,
1790 wpa_s->parent, NULL);
1796 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1797 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1798 params->peer_interface_addr)) {
1799 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1803 if (params->wps_method == WPS_PBC) {
1804 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1805 params->peer_device_addr);
1806 #ifdef CONFIG_WPS_NFC
1807 } else if (params->wps_method == WPS_NFC) {
1808 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1809 DEV_PW_NFC_CONNECTION_HANDOVER &&
1810 !wpa_s->parent->p2p_oob_dev_pw) {
1811 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1814 wpas_ap_wps_add_nfc_pw(
1815 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1816 wpa_s->parent->p2p_oob_dev_pw,
1817 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1818 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1819 #endif /* CONFIG_WPS_NFC */
1820 } else if (wpa_s->p2p_pin[0])
1821 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1822 wpa_s->p2p_pin, NULL, 0, 0);
1823 os_free(wpa_s->go_params);
1824 wpa_s->go_params = NULL;
1828 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1829 struct p2p_go_neg_results *params,
1830 int group_formation)
1832 struct wpa_ssid *ssid;
1834 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1835 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1836 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1841 ssid = wpa_config_add_network(wpa_s->conf);
1843 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1847 wpa_s->show_group_started = 0;
1848 wpa_s->p2p_go_group_formation_completed = 0;
1849 wpa_s->group_formation_reported = 0;
1850 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1852 wpa_config_set_network_defaults(ssid);
1853 ssid->temporary = 1;
1854 ssid->p2p_group = 1;
1855 ssid->p2p_persistent_group = params->persistent_group;
1856 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1858 ssid->frequency = params->freq;
1859 ssid->ht40 = params->ht40;
1860 ssid->vht = params->vht;
1861 ssid->ssid = os_zalloc(params->ssid_len + 1);
1863 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1864 ssid->ssid_len = params->ssid_len;
1866 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1867 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1868 ssid->proto = WPA_PROTO_RSN;
1869 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1870 ssid->group_cipher = WPA_CIPHER_CCMP;
1871 if (params->freq > 56160) {
1873 * Enable GCMP instead of CCMP as pairwise_cipher and
1874 * group_cipher in 60 GHz.
1876 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1877 ssid->group_cipher = WPA_CIPHER_GCMP;
1879 if (os_strlen(params->passphrase) > 0) {
1880 ssid->passphrase = os_strdup(params->passphrase);
1881 if (ssid->passphrase == NULL) {
1882 wpa_msg_global(wpa_s, MSG_ERROR,
1883 "P2P: Failed to copy passphrase for GO");
1884 wpa_config_remove_network(wpa_s->conf, ssid->id);
1888 ssid->passphrase = NULL;
1889 ssid->psk_set = params->psk_set;
1891 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1892 else if (ssid->passphrase)
1893 wpa_config_update_psk(ssid);
1894 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1896 wpa_s->ap_configured_cb = p2p_go_configured;
1897 wpa_s->ap_configured_cb_ctx = wpa_s;
1898 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1899 wpa_s->scan_req = NORMAL_SCAN_REQ;
1900 wpa_s->connect_without_scan = ssid;
1901 wpa_s->reassociate = 1;
1902 wpa_s->disconnected = 0;
1903 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1905 wpa_supplicant_req_scan(wpa_s, 0, 0);
1909 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1910 const struct wpa_supplicant *src)
1912 struct wpa_config *d;
1913 const struct wpa_config *s;
1918 #define C(n) if (s->n) d->n = os_strdup(s->n)
1927 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1928 os_memcpy(d->sec_device_type, s->sec_device_type,
1929 sizeof(d->sec_device_type));
1930 d->num_sec_device_types = s->num_sec_device_types;
1932 d->p2p_group_idle = s->p2p_group_idle;
1933 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
1934 d->p2p_intra_bss = s->p2p_intra_bss;
1935 d->persistent_reconnect = s->persistent_reconnect;
1936 d->max_num_sta = s->max_num_sta;
1937 d->pbc_in_m1 = s->pbc_in_m1;
1938 d->ignore_old_scan_res = s->ignore_old_scan_res;
1939 d->beacon_int = s->beacon_int;
1940 d->dtim_period = s->dtim_period;
1941 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
1942 d->disassoc_low_ack = s->disassoc_low_ack;
1943 d->disable_scan_offload = s->disable_scan_offload;
1944 d->passive_scan = s->passive_scan;
1946 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1947 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1948 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1950 d->p2p_cli_probe = s->p2p_cli_probe;
1954 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1955 char *ifname, size_t len)
1957 char *ifname_ptr = wpa_s->ifname;
1959 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1960 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1961 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1964 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1965 if (os_strlen(ifname) >= IFNAMSIZ &&
1966 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1969 /* Try to avoid going over the IFNAMSIZ length limit */
1970 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1971 if (os_snprintf_error(len, res) && len)
1972 ifname[len - 1] = '\0';
1977 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1978 enum wpa_driver_if_type type)
1980 char ifname[120], force_ifname[120];
1982 if (wpa_s->pending_interface_name[0]) {
1983 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1984 "- skip creation of a new one");
1985 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1986 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1987 "unknown?! ifname='%s'",
1988 wpa_s->pending_interface_name);
1994 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1995 force_ifname[0] = '\0';
1997 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1999 wpa_s->p2p_group_idx++;
2001 wpa_s->pending_interface_type = type;
2002 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2003 wpa_s->pending_interface_addr, NULL) < 0) {
2004 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2009 if (force_ifname[0]) {
2010 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2012 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2013 sizeof(wpa_s->pending_interface_name));
2015 os_strlcpy(wpa_s->pending_interface_name, ifname,
2016 sizeof(wpa_s->pending_interface_name));
2017 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2018 MACSTR, wpa_s->pending_interface_name,
2019 MAC2STR(wpa_s->pending_interface_addr));
2025 static void wpas_p2p_remove_pending_group_interface(
2026 struct wpa_supplicant *wpa_s)
2028 if (!wpa_s->pending_interface_name[0] ||
2029 is_zero_ether_addr(wpa_s->pending_interface_addr))
2030 return; /* No pending virtual interface */
2032 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2033 wpa_s->pending_interface_name);
2034 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2035 wpa_s->pending_interface_name);
2036 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2037 wpa_s->pending_interface_name[0] = '\0';
2038 wpa_s->global->pending_group_iface_for_p2ps = 0;
2042 static struct wpa_supplicant *
2043 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2045 struct wpa_interface iface;
2046 struct wpa_supplicant *group_wpa_s;
2048 if (!wpa_s->pending_interface_name[0]) {
2049 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2050 if (!wpas_p2p_create_iface(wpa_s))
2053 * Something has forced us to remove the pending interface; try
2054 * to create a new one and hope for the best that we will get
2055 * the same local address.
2057 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2058 WPA_IF_P2P_CLIENT) < 0)
2062 os_memset(&iface, 0, sizeof(iface));
2063 iface.ifname = wpa_s->pending_interface_name;
2064 iface.driver = wpa_s->driver->name;
2065 if (wpa_s->conf->ctrl_interface == NULL &&
2066 wpa_s->parent != wpa_s &&
2068 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2069 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2071 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
2072 iface.driver_param = wpa_s->conf->driver_param;
2073 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
2074 if (group_wpa_s == NULL) {
2075 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2076 "wpa_supplicant interface");
2079 wpa_s->pending_interface_name[0] = '\0';
2080 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2081 P2P_GROUP_INTERFACE_CLIENT;
2082 wpa_s->global->p2p_group_formation = group_wpa_s;
2083 wpa_s->global->pending_group_iface_for_p2ps = 0;
2085 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2091 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2094 struct wpa_supplicant *wpa_s = eloop_ctx;
2095 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2096 wpas_p2p_group_formation_failed(wpa_s, 0);
2100 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2101 int already_deleted)
2103 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2104 wpa_s->parent, NULL);
2105 if (wpa_s->global->p2p)
2106 p2p_group_formation_failed(wpa_s->global->p2p);
2107 wpas_group_formation_completed(wpa_s, 0, already_deleted);
2111 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2113 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2114 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2115 wpa_s->parent, NULL);
2116 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2117 wpa_s->parent, NULL);
2118 wpa_s->global->p2p_fail_on_wps_complete = 0;
2122 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2124 if (wpa_s->global->p2p_group_formation != wpa_s)
2126 /* Speed up group formation timeout since this cannot succeed */
2127 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2128 wpa_s->parent, NULL);
2129 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2130 wpa_s->parent, NULL);
2134 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2136 struct wpa_supplicant *wpa_s = ctx;
2138 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2139 wpa_drv_cancel_remain_on_channel(wpa_s);
2140 wpa_s->off_channel_freq = 0;
2141 wpa_s->roc_waiting_drv_freq = 0;
2145 wpa_msg_global(wpa_s, MSG_INFO,
2146 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2148 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2149 wpas_p2p_remove_pending_group_interface(wpa_s);
2153 if (!res->role_go) {
2154 /* Inform driver of the operating channel of GO. */
2155 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2158 if (wpa_s->p2p_go_ht40)
2160 if (wpa_s->p2p_go_vht)
2163 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2164 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2166 res->role_go ? "GO" : "client", res->freq, res->ht40,
2167 MAC2STR(res->peer_device_addr),
2168 MAC2STR(res->peer_interface_addr),
2169 p2p_wps_method_text(res->wps_method));
2170 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2172 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2173 struct wpa_ssid *ssid;
2174 ssid = wpa_config_get_network(wpa_s->conf,
2175 wpa_s->p2p_persistent_id);
2176 if (ssid && ssid->disabled == 2 &&
2177 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2178 size_t len = os_strlen(ssid->passphrase);
2179 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2180 "on requested persistent group");
2181 os_memcpy(res->passphrase, ssid->passphrase, len);
2182 res->passphrase[len] = '\0';
2186 if (wpa_s->create_p2p_iface) {
2187 struct wpa_supplicant *group_wpa_s =
2188 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2189 if (group_wpa_s == NULL) {
2190 wpas_p2p_remove_pending_group_interface(wpa_s);
2191 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2193 wpas_p2p_group_formation_failed(wpa_s, 1);
2196 if (group_wpa_s != wpa_s) {
2197 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2198 sizeof(group_wpa_s->p2p_pin));
2199 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2201 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2202 wpa_s->pending_interface_name[0] = '\0';
2203 group_wpa_s->p2p_in_provisioning = 1;
2206 wpas_start_wps_go(group_wpa_s, res, 1);
2208 os_get_reltime(&group_wpa_s->scan_min_time);
2209 wpas_start_wps_enrollee(group_wpa_s, res);
2212 wpa_s->p2p_in_provisioning = 1;
2213 wpa_s->global->p2p_group_formation = wpa_s;
2216 wpas_start_wps_go(wpa_s, res, 1);
2218 os_get_reltime(&wpa_s->scan_min_time);
2219 wpas_start_wps_enrollee(ctx, res);
2223 wpa_s->p2p_long_listen = 0;
2224 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2226 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2227 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2228 (res->peer_config_timeout % 100) * 10000,
2229 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2233 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2236 struct wpa_supplicant *wpa_s = ctx;
2237 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2238 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2239 dev_passwd_id, go_intent);
2241 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
2245 static void wpas_dev_found(void *ctx, const u8 *addr,
2246 const struct p2p_peer_info *info,
2249 #ifndef CONFIG_NO_STDOUT_DEBUG
2250 struct wpa_supplicant *wpa_s = ctx;
2251 char devtype[WPS_DEV_TYPE_BUFSIZE];
2252 char *wfd_dev_info_hex = NULL;
2254 #ifdef CONFIG_WIFI_DISPLAY
2255 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2256 WFD_SUBELEM_DEVICE_INFO);
2257 #endif /* CONFIG_WIFI_DISPLAY */
2259 if (info->p2ps_instance) {
2261 const u8 *buf = wpabuf_head(info->p2ps_instance);
2262 size_t len = wpabuf_len(info->p2ps_instance);
2269 if (len < 4 + 2 + 1)
2271 id = WPA_GET_LE32(buf);
2273 methods = WPA_GET_BE16(buf);
2276 if (str_len > len - 4 - 2 - 1)
2278 os_memcpy(str, buf, str_len);
2279 str[str_len] = '\0';
2281 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2283 wpa_msg_global(wpa_s, MSG_INFO,
2284 P2P_EVENT_DEVICE_FOUND MACSTR
2285 " p2p_dev_addr=" MACSTR
2286 " pri_dev_type=%s name='%s'"
2287 " config_methods=0x%x"
2290 " adv_id=%x asp_svc=%s%s",
2292 MAC2STR(info->p2p_device_addr),
2293 wps_dev_type_bin2str(
2295 devtype, sizeof(devtype)),
2296 info->device_name, methods,
2297 info->dev_capab, info->group_capab,
2299 info->vendor_elems ?
2300 " vendor_elems=1" : "");
2305 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2306 " p2p_dev_addr=" MACSTR
2307 " pri_dev_type=%s name='%s' config_methods=0x%x "
2308 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2309 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2310 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2312 info->device_name, info->config_methods,
2313 info->dev_capab, info->group_capab,
2314 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2315 wfd_dev_info_hex ? wfd_dev_info_hex : "",
2316 info->vendor_elems ? " vendor_elems=1" : "",
2320 os_free(wfd_dev_info_hex);
2321 #endif /* CONFIG_NO_STDOUT_DEBUG */
2323 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2327 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2329 struct wpa_supplicant *wpa_s = ctx;
2331 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2332 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2334 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2338 static void wpas_find_stopped(void *ctx)
2340 struct wpa_supplicant *wpa_s = ctx;
2341 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2342 wpas_notify_p2p_find_stopped(wpa_s);
2346 struct wpas_p2p_listen_work {
2348 unsigned int duration;
2349 struct wpabuf *probe_resp_ie;
2353 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2357 wpabuf_free(lwork->probe_resp_ie);
2362 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2364 struct wpas_p2p_listen_work *lwork;
2366 if (!wpa_s->p2p_listen_work)
2369 lwork = wpa_s->p2p_listen_work->ctx;
2370 wpas_p2p_listen_work_free(lwork);
2371 radio_work_done(wpa_s->p2p_listen_work);
2372 wpa_s->p2p_listen_work = NULL;
2376 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2378 struct wpa_supplicant *wpa_s = work->wpa_s;
2379 struct wpas_p2p_listen_work *lwork = work->ctx;
2380 unsigned int duration;
2383 if (work->started) {
2384 wpa_s->p2p_listen_work = NULL;
2385 wpas_stop_listen(wpa_s);
2387 wpas_p2p_listen_work_free(lwork);
2391 wpa_s->p2p_listen_work = work;
2393 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2395 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2396 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2397 "report received Probe Request frames");
2398 wpas_p2p_listen_work_done(wpa_s);
2402 wpa_s->pending_listen_freq = lwork->freq;
2403 wpa_s->pending_listen_duration = lwork->duration;
2405 duration = lwork->duration;
2406 #ifdef CONFIG_TESTING_OPTIONS
2407 if (wpa_s->extra_roc_dur) {
2408 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2409 duration, duration + wpa_s->extra_roc_dur);
2410 duration += wpa_s->extra_roc_dur;
2412 #endif /* CONFIG_TESTING_OPTIONS */
2414 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2415 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2416 "to remain on channel (%u MHz) for Listen "
2417 "state", lwork->freq);
2418 wpas_p2p_listen_work_done(wpa_s);
2419 wpa_s->pending_listen_freq = 0;
2422 wpa_s->off_channel_freq = 0;
2423 wpa_s->roc_waiting_drv_freq = lwork->freq;
2427 static int wpas_start_listen(void *ctx, unsigned int freq,
2428 unsigned int duration,
2429 const struct wpabuf *probe_resp_ie)
2431 struct wpa_supplicant *wpa_s = ctx;
2432 struct wpas_p2p_listen_work *lwork;
2434 if (wpa_s->p2p_listen_work) {
2435 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2439 lwork = os_zalloc(sizeof(*lwork));
2443 lwork->duration = duration;
2444 if (probe_resp_ie) {
2445 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2446 if (lwork->probe_resp_ie == NULL) {
2447 wpas_p2p_listen_work_free(lwork);
2452 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2454 wpas_p2p_listen_work_free(lwork);
2462 static void wpas_stop_listen(void *ctx)
2464 struct wpa_supplicant *wpa_s = ctx;
2465 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2466 wpa_drv_cancel_remain_on_channel(wpa_s);
2467 wpa_s->off_channel_freq = 0;
2468 wpa_s->roc_waiting_drv_freq = 0;
2470 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2473 * Don't cancel Probe Request RX reporting for a connected P2P Client
2474 * handling Probe Request frames.
2476 if (!wpa_s->p2p_cli_probe)
2477 wpa_drv_probe_req_report(wpa_s, 0);
2479 wpas_p2p_listen_work_done(wpa_s);
2483 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2486 struct wpa_supplicant *wpa_s = ctx;
2487 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2492 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2493 const u8 *peer, const char *params,
2494 unsigned int generated_pin)
2496 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2497 " %08d%s", MAC2STR(peer), generated_pin, params);
2501 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2502 const u8 *peer, const char *params)
2504 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2505 "%s", MAC2STR(peer), params);
2509 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2510 const u8 *dev_addr, const u8 *pri_dev_type,
2511 const char *dev_name, u16 supp_config_methods,
2512 u8 dev_capab, u8 group_capab, const u8 *group_id,
2513 size_t group_id_len)
2515 struct wpa_supplicant *wpa_s = ctx;
2516 char devtype[WPS_DEV_TYPE_BUFSIZE];
2518 u8 empty_dev_type[8];
2519 unsigned int generated_pin = 0;
2520 struct wpa_supplicant *group = NULL;
2524 for (group = wpa_s->global->ifaces; group; group = group->next)
2526 struct wpa_ssid *s = group->current_ssid;
2528 s->mode == WPAS_MODE_P2P_GO &&
2529 group_id_len - ETH_ALEN == s->ssid_len &&
2530 os_memcmp(group_id + ETH_ALEN, s->ssid,
2536 if (pri_dev_type == NULL) {
2537 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2538 pri_dev_type = empty_dev_type;
2540 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2541 " pri_dev_type=%s name='%s' config_methods=0x%x "
2542 "dev_capab=0x%x group_capab=0x%x%s%s",
2544 wps_dev_type_bin2str(pri_dev_type, devtype,
2546 dev_name, supp_config_methods, dev_capab, group_capab,
2547 group ? " group=" : "",
2548 group ? group->ifname : "");
2549 if (os_snprintf_error(sizeof(params), res))
2550 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
2551 params[sizeof(params) - 1] = '\0';
2553 if (config_methods & WPS_CONFIG_DISPLAY) {
2554 generated_pin = wps_generate_pin();
2555 wpas_prov_disc_local_display(wpa_s, peer, params,
2557 } else if (config_methods & WPS_CONFIG_KEYPAD)
2558 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2559 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2560 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2561 MACSTR "%s", MAC2STR(peer), params);
2563 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2564 P2P_PROV_DISC_SUCCESS,
2565 config_methods, generated_pin);
2569 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2571 struct wpa_supplicant *wpa_s = ctx;
2572 unsigned int generated_pin = 0;
2575 if (wpa_s->pending_pd_before_join &&
2576 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2577 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2578 wpa_s->pending_pd_before_join = 0;
2579 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2580 "join-existing-group operation");
2581 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2585 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2586 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2589 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2590 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2591 if (os_snprintf_error(sizeof(params), res))
2592 params[sizeof(params) - 1] = '\0';
2596 if (config_methods & WPS_CONFIG_DISPLAY)
2597 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2598 else if (config_methods & WPS_CONFIG_KEYPAD) {
2599 generated_pin = wps_generate_pin();
2600 wpas_prov_disc_local_display(wpa_s, peer, params,
2602 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2603 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2604 MACSTR "%s", MAC2STR(peer), params);
2606 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2607 P2P_PROV_DISC_SUCCESS,
2608 config_methods, generated_pin);
2612 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2613 enum p2p_prov_disc_status status,
2614 u32 adv_id, const u8 *adv_mac,
2615 const char *deferred_session_resp)
2617 struct wpa_supplicant *wpa_s = ctx;
2619 if (wpa_s->p2p_fallback_to_go_neg) {
2620 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2621 "failed - fall back to GO Negotiation");
2622 wpa_msg_global(wpa_s->parent, MSG_INFO,
2623 P2P_EVENT_FALLBACK_TO_GO_NEG
2624 "reason=PD-failed");
2625 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2629 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2630 wpa_s->pending_pd_before_join = 0;
2631 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2632 "join-existing-group operation (no ACK for PD "
2634 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2638 if (adv_id && adv_mac && deferred_session_resp) {
2639 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2640 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2641 " deferred_session_resp='%s'",
2642 MAC2STR(peer), status, adv_id,
2643 deferred_session_resp);
2644 } else if (adv_id && adv_mac) {
2645 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2646 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2647 MAC2STR(peer), status, adv_id);
2649 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2650 " p2p_dev_addr=" MACSTR " status=%d",
2651 MAC2STR(peer), status);
2654 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2659 static int freq_included(struct wpa_supplicant *wpa_s,
2660 const struct p2p_channels *channels,
2663 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2664 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2670 static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2672 unsigned int num = P2P_MAX_CHANNELS;
2676 p2p_go_dump_common_freqs(wpa_s);
2677 common_freqs = os_calloc(num, sizeof(int));
2681 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2683 wpa_dbg(wpa_s, MSG_DEBUG,
2684 "P2P: Failed to get group common freqs");
2685 os_free(common_freqs);
2689 os_free(wpa_s->p2p_group_common_freqs);
2690 wpa_s->p2p_group_common_freqs = common_freqs;
2691 wpa_s->p2p_group_common_freqs_num = num;
2692 p2p_go_dump_common_freqs(wpa_s);
2697 * Check if the given frequency is one of the possible operating frequencies
2698 * set after the completion of the GO Negotiation.
2700 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2704 p2p_go_dump_common_freqs(wpa_s);
2706 /* assume no restrictions */
2707 if (!wpa_s->p2p_group_common_freqs_num)
2710 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2711 if (wpa_s->p2p_group_common_freqs[i] == freq)
2718 static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2719 struct sta_info *sta, void *ctx)
2721 int *ecsa_support = ctx;
2723 *ecsa_support &= sta->ecsa_supported;
2729 /* Check if all the peers support eCSA */
2730 static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2732 int ecsa_support = 1;
2734 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2737 return ecsa_support;
2742 * Pick the best frequency to use from all the currently used frequencies.
2744 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2745 struct wpa_used_freq_data *freqs,
2750 /* find a candidate freq that is supported by P2P */
2751 for (c = 0; c < num; c++)
2752 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2758 /* once we have a candidate, try to find a 'better' one */
2759 for (i = c + 1; i < num; i++) {
2760 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2764 * 1. Infrastructure station interfaces have higher preference.
2765 * 2. P2P Clients have higher preference.
2768 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2773 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2776 return freqs[c].freq;
2780 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2781 const u8 *go_dev_addr, const u8 *ssid,
2782 size_t ssid_len, int *go, u8 *group_bssid,
2783 int *force_freq, int persistent_group,
2784 const struct p2p_channels *channels,
2787 struct wpa_supplicant *wpa_s = ctx;
2789 struct wpa_used_freq_data *freqs;
2790 struct wpa_supplicant *grp;
2793 if (!persistent_group) {
2794 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2795 " to join an active group (SSID: %s)",
2796 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2797 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2798 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2800 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2801 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2802 "authorized invitation");
2806 #ifdef CONFIG_WPS_NFC
2807 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2808 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
2809 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2810 wpa_s->p2p_wps_method = WPS_NFC;
2811 wpa_s->pending_join_wps_method = WPS_NFC;
2812 os_memcpy(wpa_s->pending_join_dev_addr,
2813 go_dev_addr, ETH_ALEN);
2814 os_memcpy(wpa_s->pending_join_iface_addr,
2818 #endif /* CONFIG_WPS_NFC */
2821 * Do not accept the invitation automatically; notify user and
2824 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2827 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2829 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2830 "running persistent group");
2832 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2836 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2837 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2838 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2839 "invitation to re-invoke a persistent group");
2840 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2841 } else if (!wpa_s->conf->persistent_reconnect)
2842 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2844 for (s = wpa_s->conf->ssid; s; s = s->next) {
2845 if (s->disabled == 2 &&
2846 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2847 s->ssid_len == ssid_len &&
2848 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2853 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2854 " requested reinvocation of an unknown group",
2856 return P2P_SC_FAIL_UNKNOWN_GROUP;
2859 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2861 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2862 wpa_printf(MSG_DEBUG, "P2P: The only available "
2863 "interface is already in use - reject "
2865 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2867 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2868 } else if (s->mode == WPAS_MODE_P2P_GO) {
2870 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2872 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2873 "interface address for the group");
2874 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2876 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2881 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2884 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2885 sizeof(struct wpa_used_freq_data));
2887 int num_channels = wpa_s->num_multichan_concurrent;
2888 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2889 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2893 /* Get one of the frequencies currently in use */
2894 if (best_freq > 0) {
2895 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2896 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
2898 if (wpa_s->num_multichan_concurrent < 2 ||
2899 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2900 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2901 *force_freq = best_freq;
2905 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2906 wpas_p2p_num_unused_channels(wpa_s) > 0) {
2908 /* We are the client */
2909 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2910 "running a GO but we are capable of MCC, "
2911 "figure out the best channel to use");
2913 } else if (!freq_included(wpa_s, channels, *force_freq)) {
2914 /* We are the GO, and *force_freq is not in the
2916 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2917 "in intersection but we are capable of MCC, "
2918 "figure out the best channel to use",
2924 return P2P_SC_SUCCESS;
2928 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2929 const u8 *ssid, size_t ssid_len,
2930 const u8 *go_dev_addr, u8 status,
2933 struct wpa_supplicant *wpa_s = ctx;
2936 for (s = wpa_s->conf->ssid; s; s = s->next) {
2937 if (s->disabled == 2 &&
2938 s->ssid_len == ssid_len &&
2939 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2943 if (status == P2P_SC_SUCCESS) {
2944 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2945 " was accepted; op_freq=%d MHz, SSID=%s",
2946 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
2948 int go = s->mode == WPAS_MODE_P2P_GO;
2949 wpas_p2p_group_add_persistent(
2950 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
2951 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
2954 wpa_s->user_initiated_pd = 0;
2955 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2956 wpa_s->p2p_wps_method, 0, op_freq,
2962 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2963 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2964 " was rejected (status %u)", MAC2STR(sa), status);
2970 wpa_msg_global(wpa_s, MSG_INFO,
2971 P2P_EVENT_INVITATION_RECEIVED
2972 "sa=" MACSTR " go_dev_addr=" MACSTR
2973 " bssid=" MACSTR " unknown-network",
2974 MAC2STR(sa), MAC2STR(go_dev_addr),
2977 wpa_msg_global(wpa_s, MSG_INFO,
2978 P2P_EVENT_INVITATION_RECEIVED
2979 "sa=" MACSTR " go_dev_addr=" MACSTR
2981 MAC2STR(sa), MAC2STR(go_dev_addr));
2983 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
2988 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2989 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2990 "sa=" MACSTR " persistent=%d freq=%d",
2991 MAC2STR(sa), s->id, op_freq);
2993 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2994 "sa=" MACSTR " persistent=%d",
2995 MAC2STR(sa), s->id);
2997 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3002 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3003 struct wpa_ssid *ssid,
3004 const u8 *peer, int inv)
3007 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
3012 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3013 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
3017 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
3018 if (ssid->mode != WPAS_MODE_P2P_GO &&
3019 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3020 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3021 "due to invitation result", ssid->id);
3022 wpas_notify_network_removed(wpa_s, ssid);
3023 wpa_config_remove_network(wpa_s->conf, ssid->id);
3026 return; /* Peer not found in client list */
3029 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3030 "group %d client list%s",
3031 MAC2STR(peer), ssid->id,
3032 inv ? " due to invitation result" : "");
3033 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3034 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3035 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3036 ssid->num_p2p_clients--;
3037 if (p2p_wpa_s->conf->update_config &&
3038 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
3039 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3043 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3046 struct wpa_ssid *ssid;
3048 wpa_s = wpa_s->global->p2p_invite_group;
3050 return; /* No known invitation group */
3051 ssid = wpa_s->current_ssid;
3052 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3053 !ssid->p2p_persistent_group)
3054 return; /* Not operating as a GO in persistent group */
3055 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3056 ssid->ssid, ssid->ssid_len);
3057 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3061 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3062 const struct p2p_channels *channels,
3063 const u8 *peer, int neg_freq,
3066 struct wpa_supplicant *wpa_s = ctx;
3067 struct wpa_ssid *ssid;
3071 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3072 "status=%d " MACSTR,
3073 status, MAC2STR(bssid));
3075 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3076 "status=%d ", status);
3078 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3080 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3081 status, MAC2STR(peer));
3082 if (wpa_s->pending_invite_ssid_id == -1) {
3083 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3084 wpas_remove_persistent_client(wpa_s, peer);
3085 return; /* Invitation to active group */
3088 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3089 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3090 "invitation exchange to indicate readiness for "
3094 if (status != P2P_SC_SUCCESS) {
3095 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3096 ssid = wpa_config_get_network(
3097 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3098 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3100 wpas_p2p_remove_pending_group_interface(wpa_s);
3104 ssid = wpa_config_get_network(wpa_s->conf,
3105 wpa_s->pending_invite_ssid_id);
3107 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3108 "data matching with invitation");
3113 * The peer could have missed our ctrl::ack frame for Invitation
3114 * Response and continue retransmitting the frame. To reduce the
3115 * likelihood of the peer not getting successful TX status for the
3116 * Invitation Response frame, wait a short time here before starting
3117 * the persistent group so that we will remain on the current channel to
3118 * acknowledge any possible retransmission from the peer.
3120 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3121 "starting persistent group");
3124 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3125 freq_included(wpa_s, channels, neg_freq))
3127 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3128 freq_included(wpa_s, channels, peer_oper_freq))
3129 freq = peer_oper_freq;
3133 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3134 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3135 wpas_p2p_group_add_persistent(wpa_s, ssid,
3136 ssid->mode == WPAS_MODE_P2P_GO,
3137 wpa_s->p2p_persistent_go_freq,
3139 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3141 ssid->mode == WPAS_MODE_P2P_GO ?
3142 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3147 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3150 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3152 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3156 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3158 reg->channel[reg->channels] = chan;
3163 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3164 struct p2p_channels *chan,
3165 struct p2p_channels *cli_chan)
3169 wpa_s->global->p2p_24ghz_social_channels = 1;
3171 os_memset(cli_chan, 0, sizeof(*cli_chan));
3173 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3176 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3177 chan->reg_class[cla].reg_class = 81;
3178 chan->reg_class[cla].channels = 0;
3179 for (i = 0; i < 11; i++) {
3180 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3181 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3183 if (chan->reg_class[cla].channels)
3186 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3189 /* Operating class 115 - 5 GHz, channels 36-48 */
3190 chan->reg_class[cla].reg_class = 115;
3191 chan->reg_class[cla].channels = 0;
3192 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3193 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3194 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3195 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3196 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3197 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3198 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3199 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3200 if (chan->reg_class[cla].channels)
3203 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3206 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3207 chan->reg_class[cla].reg_class = 124;
3208 chan->reg_class[cla].channels = 0;
3209 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3210 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3211 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3212 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3213 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3214 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3215 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3216 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3217 if (chan->reg_class[cla].channels)
3220 chan->reg_classes = cla;
3225 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3227 enum hostapd_hw_mode mode)
3231 for (i = 0; i < num_modes; i++) {
3232 if (modes[i].mode == mode)
3241 NOT_ALLOWED, NO_IR, ALLOWED
3244 static int has_channel(struct wpa_global *global,
3245 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3250 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3252 if (wpas_p2p_disallowed_freq(global, freq))
3255 for (i = 0; i < mode->num_channels; i++) {
3256 if (mode->channels[i].chan == chan) {
3258 *flags = mode->channels[i].flag;
3259 if (mode->channels[i].flag &
3260 (HOSTAPD_CHAN_DISABLED |
3261 HOSTAPD_CHAN_RADAR))
3263 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3273 struct p2p_oper_class_map {
3274 enum hostapd_hw_mode mode;
3279 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3282 static const struct p2p_oper_class_map op_class[] = {
3283 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3284 #if 0 /* Do not enable HT40 on 2 GHz for now */
3285 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3286 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3288 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3289 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3290 { HOSTAPD_MODE_IEEE80211A, 125, 149, 169, 4, BW20 },
3291 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3292 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3293 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3294 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3297 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3298 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3299 * 80 MHz, but currently use the following definition for simplicity
3300 * (these center frequencies are not actual channels, which makes
3301 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3302 * removing invalid channels.
3304 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3305 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3306 { -1, 0, 0, 0, 0, BW20 }
3310 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3311 struct hostapd_hw_modes *mode,
3314 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3317 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3320 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3322 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3323 * so the center channel is 6 channels away from the start/end.
3325 if (channel >= center_channels[i] - 6 &&
3326 channel <= center_channels[i] + 6)
3327 return center_channels[i];
3333 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3334 struct hostapd_hw_modes *mode,
3339 enum chan_allowed res, ret = ALLOWED;
3341 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3344 if (center_chan >= 58 && center_chan <= 138)
3345 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3347 /* check all the channels are available */
3348 for (i = 0; i < 4; i++) {
3349 int adj_chan = center_chan - 6 + i * 4;
3351 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3352 if (res == NOT_ALLOWED)
3357 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3359 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3361 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3363 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3371 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3372 struct hostapd_hw_modes *mode,
3376 enum chan_allowed res, res2;
3378 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3379 if (bw == BW40MINUS) {
3380 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3382 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3383 } else if (bw == BW40PLUS) {
3384 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3386 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3387 } else if (bw == BW80) {
3388 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3391 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3393 if (res == NO_IR || res2 == NO_IR)
3399 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3400 struct p2p_channels *chan,
3401 struct p2p_channels *cli_chan)
3403 struct hostapd_hw_modes *mode;
3404 int cla, op, cli_cla;
3406 if (wpa_s->hw.modes == NULL) {
3407 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3408 "of all supported channels; assume dualband "
3410 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3415 for (op = 0; op_class[op].op_class; op++) {
3416 const struct p2p_oper_class_map *o = &op_class[op];
3418 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3420 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3423 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3424 wpa_s->global->p2p_24ghz_social_channels = 1;
3425 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3426 enum chan_allowed res;
3427 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3428 if (res == ALLOWED) {
3430 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3432 reg = &chan->reg_class[cla];
3434 reg->reg_class = o->op_class;
3436 reg->channel[reg->channels] = ch;
3438 } else if (res == NO_IR &&
3439 wpa_s->conf->p2p_add_cli_chan) {
3440 if (cli_reg == NULL) {
3441 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3443 cli_reg = &cli_chan->reg_class[cli_cla];
3445 cli_reg->reg_class = o->op_class;
3447 cli_reg->channel[cli_reg->channels] = ch;
3448 cli_reg->channels++;
3452 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3453 reg->channel, reg->channels);
3456 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3457 cli_reg->channel, cli_reg->channels);
3461 chan->reg_classes = cla;
3462 cli_chan->reg_classes = cli_cla;
3468 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3469 struct hostapd_hw_modes *mode, u8 channel)
3472 enum chan_allowed ret;
3474 for (op = 0; op_class[op].op_class; op++) {
3475 const struct p2p_oper_class_map *o = &op_class[op];
3478 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3479 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3480 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3483 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3485 return (o->bw == BW40MINUS) ? -1 : 1;
3492 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3493 struct hostapd_hw_modes *mode, u8 channel)
3495 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3498 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3502 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3505 struct wpa_supplicant *wpa_s = ctx;
3507 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3508 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3514 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3518 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3519 const u8 *ssid, size_t ssid_len)
3521 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3522 struct wpa_ssid *s = wpa_s->current_ssid;
3525 if (s->mode != WPAS_MODE_P2P_GO &&
3526 s->mode != WPAS_MODE_AP &&
3527 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3529 if (s->ssid_len != ssid_len ||
3530 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3540 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3541 const u8 *peer_dev_addr)
3543 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3544 struct wpa_ssid *ssid = wpa_s->current_ssid;
3545 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
3547 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3555 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3557 struct wpa_supplicant *wpa_s = ctx;
3559 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3563 static int wpas_is_concurrent_session_active(void *ctx)
3565 struct wpa_supplicant *wpa_s = ctx;
3566 struct wpa_supplicant *ifs;
3568 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3571 if (ifs->wpa_state > WPA_ASSOCIATED)
3578 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3580 struct wpa_supplicant *wpa_s = ctx;
3581 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3585 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3586 const char *conf_p2p_dev)
3588 struct wpa_interface iface;
3589 struct wpa_supplicant *p2pdev_wpa_s;
3591 char force_name[100];
3594 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3596 if (os_snprintf_error(sizeof(ifname), ret))
3598 force_name[0] = '\0';
3599 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3600 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3601 force_name, wpa_s->pending_interface_addr, NULL);
3603 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3606 os_strlcpy(wpa_s->pending_interface_name, ifname,
3607 sizeof(wpa_s->pending_interface_name));
3609 os_memset(&iface, 0, sizeof(iface));
3611 iface.ifname = wpa_s->pending_interface_name;
3612 iface.driver = wpa_s->driver->name;
3613 iface.driver_param = wpa_s->conf->driver_param;
3616 * If a P2P Device configuration file was given, use it as the interface
3617 * configuration file (instead of using parent's configuration file.
3620 iface.confname = conf_p2p_dev;
3621 iface.ctrl_interface = NULL;
3623 iface.confname = wpa_s->confname;
3624 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3627 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
3628 if (!p2pdev_wpa_s) {
3629 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3633 wpa_s->pending_interface_name[0] = '\0';
3638 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3639 const u8 *noa, size_t noa_len)
3641 struct wpa_supplicant *wpa_s, *intf = ctx;
3644 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3645 if (wpa_s->waiting_presence_resp)
3649 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3652 wpa_s->waiting_presence_resp = 0;
3654 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3655 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3656 " status=%u noa=%s", MAC2STR(src), status, hex);
3660 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3661 size_t ssid_len, u8 *go_dev_addr,
3662 u8 *ret_ssid, size_t *ret_ssid_len,
3663 u8 *intended_iface_addr)
3665 struct wpa_supplicant *wpa_s = ctx;
3668 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3670 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3671 *ret_ssid_len = s->ssid_len;
3672 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3674 if (s->mode != WPAS_MODE_P2P_GO) {
3675 os_memset(intended_iface_addr, 0, ETH_ALEN);
3676 } else if (wpas_p2p_create_iface(wpa_s)) {
3677 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3680 os_memcpy(intended_iface_addr,
3681 wpa_s->pending_interface_addr, ETH_ALEN);
3683 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3693 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3694 u8 *ssid, size_t *ssid_len, int *group_iface,
3697 struct wpa_supplicant *wpa_s = ctx;
3698 struct wpa_supplicant *go;
3702 * group_iface will be set to 1 only if a dedicated interface for P2P
3703 * role is required. First, we try to reuse an active GO. However,
3704 * if it is not present, we will try to reactivate an existing
3705 * persistent group and set group_iface to 1, so the caller will know
3706 * that the pending interface should be used.
3713 go = wpas_p2p_get_go_group(wpa_s);
3715 s = wpas_p2p_get_persistent_go(wpa_s);
3716 *group_iface = wpas_p2p_create_iface(wpa_s);
3718 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3722 s = go->current_ssid;
3723 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3725 *freq = go->assoc_freq;
3728 os_memcpy(ssid, s->ssid, s->ssid_len);
3729 *ssid_len = s->ssid_len;
3735 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3736 const u8 *ssid, size_t ssid_len)
3738 struct wpa_supplicant *wpa_s = ctx;
3740 int save_config = 0;
3743 /* Start with our first choice of Persistent Groups */
3744 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3745 if (go && ssid && ssid_len &&
3746 s->ssid_len == ssid_len &&
3747 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3748 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3751 /* Remove stale persistent group */
3752 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3753 wpa_config_remove_network(wpa_s->conf, s->id);
3758 for (i = 0; i < s->num_p2p_clients; i++) {
3759 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3760 peer, ETH_ALEN) != 0)
3763 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3764 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3765 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3768 s->num_p2p_clients--;
3773 p2p_config_write(wpa_s);
3775 /* Return TRUE if valid SSID remains */
3780 static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3781 const u8 *feat_cap, size_t feat_cap_len)
3783 static const char pref[] = " feature_cap=";
3789 * We expect a feature capability to contain at least one byte to be
3790 * reported. The string buffer provided by the caller function is
3791 * expected to be big enough to contain all bytes of the attribute for
3792 * known specifications. This function truncates the reported bytes if
3793 * the feature capability data exceeds the string buffer size.
3795 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3798 os_memcpy(buf, pref, sizeof(pref));
3799 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3800 buf_len - sizeof(pref) + 1,
3801 feat_cap, feat_cap_len);
3803 if (ret != (2 * (int) feat_cap_len))
3804 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3808 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3809 const u8 *adv_mac, const u8 *ses_mac,
3810 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3811 u8 conncap, int passwd_id,
3812 const u8 *persist_ssid,
3813 size_t persist_ssid_size, int response_done,
3814 int prov_start, const char *session_info,
3815 const u8 *feat_cap, size_t feat_cap_len,
3818 struct wpa_supplicant *wpa_s = ctx;
3820 struct wpa_ssid *persistent_go, *stale, *s = NULL;
3821 int save_config = 0;
3822 struct wpa_supplicant *go_wpa_s;
3823 char feat_cap_str[256];
3828 os_memset(mac, 0, ETH_ALEN);
3836 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3837 feat_cap, feat_cap_len);
3840 if (session_info == NULL) {
3841 wpa_msg_global(wpa_s, MSG_INFO,
3842 P2P_EVENT_P2PS_PROVISION_START MACSTR
3843 " adv_id=%x conncap=%x"
3845 " session=%x mac=" MACSTR
3846 " dev_passwd_id=%d%s",
3847 MAC2STR(dev), adv_id, conncap,
3849 ses_id, MAC2STR(ses_mac),
3850 passwd_id, feat_cap_str);
3852 wpa_msg_global(wpa_s, MSG_INFO,
3853 P2P_EVENT_P2PS_PROVISION_START MACSTR
3854 " adv_id=%x conncap=%x"
3856 " session=%x mac=" MACSTR
3857 " dev_passwd_id=%d info='%s'%s",
3858 MAC2STR(dev), adv_id, conncap,
3860 ses_id, MAC2STR(ses_mac),
3861 passwd_id, session_info, feat_cap_str);
3866 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3867 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3869 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3870 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3871 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3873 if (persistent_go && !persistent_go->num_p2p_clients) {
3874 /* remove empty persistent GO */
3875 wpa_config_remove_network(wpa_s->conf,
3879 wpa_msg_global(wpa_s, MSG_INFO,
3880 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3882 " adv_id=%x adv_mac=" MACSTR
3883 " session=%x mac=" MACSTR "%s",
3884 MAC2STR(dev), status,
3885 adv_id, MAC2STR(adv_mac),
3886 ses_id, MAC2STR(ses_mac), feat_cap_str);
3890 /* Clean up stale persistent groups with this device */
3891 if (persist_ssid && persist_ssid_size)
3892 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3895 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
3896 is_zero_ether_addr(grp_mac)) {
3897 wpa_dbg(wpa_s, MSG_ERROR,
3898 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
3903 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3907 if (s && s->ssid_len == stale->ssid_len &&
3908 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3909 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3912 /* Remove stale persistent group */
3913 if (stale->mode != WPAS_MODE_P2P_GO ||
3914 stale->num_p2p_clients <= 1) {
3915 wpa_config_remove_network(wpa_s->conf, stale->id);
3919 for (i = 0; i < stale->num_p2p_clients; i++) {
3920 if (os_memcmp(stale->p2p_client_list +
3922 dev, ETH_ALEN) == 0) {
3923 os_memmove(stale->p2p_client_list +
3925 stale->p2p_client_list +
3927 (stale->num_p2p_clients -
3932 stale->num_p2p_clients--;
3938 p2p_config_write(wpa_s);
3941 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3942 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3944 if (persistent_go && s != persistent_go &&
3945 !persistent_go->num_p2p_clients) {
3946 /* remove empty persistent GO */
3947 wpa_config_remove_network(wpa_s->conf,
3952 wpa_msg_global(wpa_s, MSG_INFO,
3953 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3955 " adv_id=%x adv_mac=" MACSTR
3956 " session=%x mac=" MACSTR
3958 MAC2STR(dev), status,
3959 adv_id, MAC2STR(adv_mac),
3960 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
3964 if (conncap == P2PS_SETUP_GROUP_OWNER) {
3966 * We need to copy the interface name. Simply saving a
3967 * pointer isn't enough, since if we use pending_interface_name
3968 * it will be overwritten when the group is added.
3970 char go_ifname[100];
3972 go_ifname[0] = '\0';
3974 wpa_s->global->pending_p2ps_group = 1;
3975 wpa_s->global->pending_p2ps_group_freq = freq;
3977 if (!wpas_p2p_create_iface(wpa_s))
3978 os_memcpy(go_ifname, wpa_s->ifname,
3980 else if (wpa_s->pending_interface_name[0])
3981 os_memcpy(go_ifname,
3982 wpa_s->pending_interface_name,
3985 if (!go_ifname[0]) {
3986 wpas_p2ps_prov_complete(
3987 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3988 dev, adv_mac, ses_mac,
3989 grp_mac, adv_id, ses_id, 0, 0,
3990 NULL, 0, 0, 0, NULL, NULL, 0, 0);
3994 /* If PD Resp complete, start up the GO */
3995 if (response_done && persistent_go) {
3996 wpas_p2p_group_add_persistent(
3997 wpa_s, persistent_go,
3998 0, 0, freq, 0, 0, NULL,
3999 persistent_go->mode ==
4001 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4003 } else if (response_done) {
4004 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0);
4007 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4008 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4010 wpa_s->p2ps_method_config_any = 1;
4012 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4013 os_memcpy(go_ifname, go_wpa_s->ifname,
4016 if (is_zero_ether_addr(grp_mac)) {
4017 wpa_dbg(go_wpa_s, MSG_DEBUG,
4018 "P2P: Setting PIN-1 for ANY");
4019 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4020 "12345670", NULL, 0,
4023 wpa_dbg(go_wpa_s, MSG_DEBUG,
4024 "P2P: Setting PIN-1 for " MACSTR,
4026 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4027 "12345670", NULL, 0,
4031 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4032 wpa_s->p2ps_method_config_any = 1;
4035 wpa_msg_global(wpa_s, MSG_INFO,
4036 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4037 " status=%d conncap=%x"
4038 " adv_id=%x adv_mac=" MACSTR
4039 " session=%x mac=" MACSTR
4040 " dev_passwd_id=%d go=%s%s",
4041 MAC2STR(dev), status, conncap,
4042 adv_id, MAC2STR(adv_mac),
4043 ses_id, MAC2STR(ses_mac),
4044 passwd_id, go_ifname, feat_cap_str);
4048 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4049 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4051 if (persistent_go && !persistent_go->num_p2p_clients) {
4052 /* remove empty persistent GO */
4053 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4056 if (conncap == P2PS_SETUP_CLIENT) {
4057 wpa_msg_global(wpa_s, MSG_INFO,
4058 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4059 " status=%d conncap=%x"
4060 " adv_id=%x adv_mac=" MACSTR
4061 " session=%x mac=" MACSTR
4062 " dev_passwd_id=%d join=" MACSTR "%s",
4063 MAC2STR(dev), status, conncap,
4064 adv_id, MAC2STR(adv_mac),
4065 ses_id, MAC2STR(ses_mac),
4066 passwd_id, MAC2STR(grp_mac), feat_cap_str);
4068 wpa_msg_global(wpa_s, MSG_INFO,
4069 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4070 " status=%d conncap=%x"
4071 " adv_id=%x adv_mac=" MACSTR
4072 " session=%x mac=" MACSTR
4073 " dev_passwd_id=%d%s",
4074 MAC2STR(dev), status, conncap,
4075 adv_id, MAC2STR(adv_mac),
4076 ses_id, MAC2STR(ses_mac),
4077 passwd_id, feat_cap_str);
4082 static int _wpas_p2p_in_progress(void *ctx)
4084 struct wpa_supplicant *wpa_s = ctx;
4085 return wpas_p2p_in_progress(wpa_s);
4089 static int wpas_prov_disc_resp_cb(void *ctx)
4091 struct wpa_supplicant *wpa_s = ctx;
4092 struct wpa_ssid *persistent_go;
4095 if (!wpa_s->global->pending_p2ps_group)
4098 freq = wpa_s->global->pending_p2ps_group_freq;
4099 wpa_s->global->pending_p2ps_group_freq = 0;
4100 wpa_s->global->pending_p2ps_group = 0;
4102 if (wpas_p2p_get_go_group(wpa_s))
4104 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4106 if (persistent_go) {
4107 wpas_p2p_group_add_persistent(
4108 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
4109 persistent_go->mode == WPAS_MODE_P2P_GO ?
4110 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
4112 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0);
4119 static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4121 unsigned int *freq_list)
4123 struct wpa_supplicant *wpa_s = ctx;
4125 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4126 WPA_IF_P2P_CLIENT, len, freq_list);
4131 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4132 * @global: Pointer to global data from wpa_supplicant_init()
4133 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4134 * Returns: 0 on success, -1 on failure
4136 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4138 struct p2p_config p2p;
4141 if (wpa_s->conf->p2p_disabled)
4144 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4150 os_memset(&p2p, 0, sizeof(p2p));
4152 p2p.debug_print = wpas_p2p_debug_print;
4153 p2p.p2p_scan = wpas_p2p_scan;
4154 p2p.send_action = wpas_send_action;
4155 p2p.send_action_done = wpas_send_action_done;
4156 p2p.go_neg_completed = wpas_go_neg_completed;
4157 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4158 p2p.dev_found = wpas_dev_found;
4159 p2p.dev_lost = wpas_dev_lost;
4160 p2p.find_stopped = wpas_find_stopped;
4161 p2p.start_listen = wpas_start_listen;
4162 p2p.stop_listen = wpas_stop_listen;
4163 p2p.send_probe_resp = wpas_send_probe_resp;
4164 p2p.sd_request = wpas_sd_request;
4165 p2p.sd_response = wpas_sd_response;
4166 p2p.prov_disc_req = wpas_prov_disc_req;
4167 p2p.prov_disc_resp = wpas_prov_disc_resp;
4168 p2p.prov_disc_fail = wpas_prov_disc_fail;
4169 p2p.invitation_process = wpas_invitation_process;
4170 p2p.invitation_received = wpas_invitation_received;
4171 p2p.invitation_result = wpas_invitation_result;
4172 p2p.get_noa = wpas_get_noa;
4173 p2p.go_connected = wpas_go_connected;
4174 p2p.presence_resp = wpas_presence_resp;
4175 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4176 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4177 p2p.get_persistent_group = wpas_get_persistent_group;
4178 p2p.get_go_info = wpas_get_go_info;
4179 p2p.remove_stale_groups = wpas_remove_stale_groups;
4180 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4181 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4182 p2p.p2ps_group_capability = p2ps_group_capability;
4183 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
4185 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4186 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4187 p2p.dev_name = wpa_s->conf->device_name;
4188 p2p.manufacturer = wpa_s->conf->manufacturer;
4189 p2p.model_name = wpa_s->conf->model_name;
4190 p2p.model_number = wpa_s->conf->model_number;
4191 p2p.serial_number = wpa_s->conf->serial_number;
4193 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4194 p2p.config_methods = wpa_s->wps->config_methods;
4197 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4198 wpa_printf(MSG_ERROR,
4199 "P2P: Failed to configure supported channel list");
4203 if (wpa_s->conf->p2p_listen_reg_class &&
4204 wpa_s->conf->p2p_listen_channel) {
4205 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4206 p2p.channel = wpa_s->conf->p2p_listen_channel;
4207 p2p.channel_forced = 1;
4210 * Pick one of the social channels randomly as the listen
4213 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4214 &p2p.channel) != 0) {
4215 wpa_printf(MSG_INFO,
4216 "P2P: No social channels supported by the driver - do not enable P2P");
4219 p2p.channel_forced = 0;
4221 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4222 p2p.reg_class, p2p.channel);
4224 if (wpa_s->conf->p2p_oper_reg_class &&
4225 wpa_s->conf->p2p_oper_channel) {
4226 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4227 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4228 p2p.cfg_op_channel = 1;
4229 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4230 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4234 * Use random operation channel from 2.4 GHz band social
4235 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4236 * other preference is indicated.
4238 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4239 &p2p.op_channel) != 0) {
4240 wpa_printf(MSG_ERROR,
4241 "P2P: Failed to select random social channel as operation channel");
4244 p2p.cfg_op_channel = 0;
4245 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4246 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4249 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4250 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4251 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4254 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4255 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4256 p2p.country[2] = 0x04;
4258 os_memcpy(p2p.country, "XX\x04", 3);
4260 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4263 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4264 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4265 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4267 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4268 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4270 p2p.max_peers = 100;
4272 if (wpa_s->conf->p2p_ssid_postfix) {
4273 p2p.ssid_postfix_len =
4274 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4275 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4276 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4277 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4278 p2p.ssid_postfix_len);
4281 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4283 p2p.max_listen = wpa_s->max_remain_on_chan;
4285 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4286 wpa_s->conf->p2p_passphrase_len <= 63)
4287 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4289 p2p.passphrase_len = 8;
4291 global->p2p = p2p_init(&p2p);
4292 if (global->p2p == NULL)
4294 global->p2p_init_wpa_s = wpa_s;
4296 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4297 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4299 p2p_add_wps_vendor_extension(
4300 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4303 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4310 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4311 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4313 * This function deinitialize per-interface P2P data.
4315 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4317 if (wpa_s->driver && wpa_s->drv_priv)
4318 wpa_drv_probe_req_report(wpa_s, 0);
4320 if (wpa_s->go_params) {
4321 /* Clear any stored provisioning info */
4322 p2p_clear_provisioning_info(
4324 wpa_s->go_params->peer_device_addr);
4327 os_free(wpa_s->go_params);
4328 wpa_s->go_params = NULL;
4329 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4330 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4331 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4332 wpa_s->p2p_long_listen = 0;
4333 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4334 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4335 wpas_p2p_remove_pending_group_interface(wpa_s);
4336 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4337 wpas_p2p_listen_work_done(wpa_s);
4338 if (wpa_s->p2p_send_action_work) {
4339 os_free(wpa_s->p2p_send_action_work->ctx);
4340 radio_work_done(wpa_s->p2p_send_action_work);
4341 wpa_s->p2p_send_action_work = NULL;
4343 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4345 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4346 wpa_s->p2p_oob_dev_pw = NULL;
4348 os_free(wpa_s->p2p_group_common_freqs);
4349 wpa_s->p2p_group_common_freqs = NULL;
4350 wpa_s->p2p_group_common_freqs_num = 0;
4352 /* TODO: remove group interface from the driver if this wpa_s instance
4353 * is on top of a P2P group interface */
4358 * wpas_p2p_deinit_global - Deinitialize global P2P module
4359 * @global: Pointer to global data from wpa_supplicant_init()
4361 * This function deinitializes the global (per device) P2P module.
4363 static void wpas_p2p_deinit_global(struct wpa_global *global)
4365 struct wpa_supplicant *wpa_s, *tmp;
4367 wpa_s = global->ifaces;
4369 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4371 /* Remove remaining P2P group interfaces */
4372 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4373 wpa_s = wpa_s->next;
4375 tmp = global->ifaces;
4378 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4383 /* Disconnect from the P2P group and deinit the interface */
4384 wpas_p2p_disconnect(tmp);
4388 * Deinit GO data on any possibly remaining interface (if main
4389 * interface is used as GO).
4391 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4392 if (wpa_s->ap_iface)
4393 wpas_p2p_group_deinit(wpa_s);
4396 p2p_deinit(global->p2p);
4398 global->p2p_init_wpa_s = NULL;
4402 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4404 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4405 wpa_s->conf->p2p_no_group_iface)
4406 return 0; /* separate interface disabled per configuration */
4407 if (wpa_s->drv_flags &
4408 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4409 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4410 return 1; /* P2P group requires a new interface in every case
4412 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4413 return 0; /* driver does not support concurrent operations */
4414 if (wpa_s->global->ifaces->next)
4415 return 1; /* more that one interface already in use */
4416 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4417 return 1; /* this interface is already in use */
4422 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4423 const u8 *peer_addr,
4424 enum p2p_wps_method wps_method,
4425 int go_intent, const u8 *own_interface_addr,
4426 unsigned int force_freq, int persistent_group,
4427 struct wpa_ssid *ssid, unsigned int pref_freq)
4429 if (persistent_group && wpa_s->conf->persistent_reconnect)
4430 persistent_group = 2;
4433 * Increase GO config timeout if HT40 is used since it takes some time
4434 * to scan channels for coex purposes before the BSS can be started.
4436 p2p_set_config_timeout(wpa_s->global->p2p,
4437 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4439 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4440 go_intent, own_interface_addr, force_freq,
4441 persistent_group, ssid ? ssid->ssid : NULL,
4442 ssid ? ssid->ssid_len : 0,
4443 wpa_s->p2p_pd_before_go_neg, pref_freq,
4444 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4449 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4450 const u8 *peer_addr,
4451 enum p2p_wps_method wps_method,
4452 int go_intent, const u8 *own_interface_addr,
4453 unsigned int force_freq, int persistent_group,
4454 struct wpa_ssid *ssid, unsigned int pref_freq)
4456 if (persistent_group && wpa_s->conf->persistent_reconnect)
4457 persistent_group = 2;
4459 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4460 go_intent, own_interface_addr, force_freq,
4461 persistent_group, ssid ? ssid->ssid : NULL,
4462 ssid ? ssid->ssid_len : 0, pref_freq,
4463 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4468 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4470 wpa_s->p2p_join_scan_count++;
4471 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4472 wpa_s->p2p_join_scan_count);
4473 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4474 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4475 " for join operationg - stop join attempt",
4476 MAC2STR(wpa_s->pending_join_iface_addr));
4477 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4478 if (wpa_s->p2p_auto_pd) {
4479 wpa_s->p2p_auto_pd = 0;
4480 wpa_msg_global(wpa_s, MSG_INFO,
4481 P2P_EVENT_PROV_DISC_FAILURE
4482 " p2p_dev_addr=" MACSTR " status=N/A",
4483 MAC2STR(wpa_s->pending_join_dev_addr));
4486 wpa_msg_global(wpa_s->parent, MSG_INFO,
4487 P2P_EVENT_GROUP_FORMATION_FAILURE);
4488 wpas_notify_p2p_group_formation_failure(wpa_s, "");
4493 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4496 unsigned int num, i;
4497 struct wpa_used_freq_data *freqs;
4499 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4500 /* Multiple channels are supported and not all are in use */
4504 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4505 sizeof(struct wpa_used_freq_data));
4509 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4510 wpa_s->num_multichan_concurrent);
4512 for (i = 0; i < num; i++) {
4513 if (freqs[i].freq == freq) {
4514 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4521 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4530 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4531 const u8 *peer_dev_addr)
4533 struct wpa_bss *bss;
4536 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4539 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4540 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4545 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4547 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4548 "%ld.%06ld (%supdated in last scan)",
4549 bss->last_update.sec, bss->last_update.usec,
4550 updated ? "": "not ");
4556 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4557 struct wpa_scan_results *scan_res)
4559 struct wpa_bss *bss = NULL;
4561 u8 iface_addr[ETH_ALEN];
4563 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4565 if (wpa_s->global->p2p_disabled)
4568 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4569 scan_res ? (int) scan_res->num : -1,
4570 wpa_s->p2p_auto_join ? "auto_" : "");
4573 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4575 if (wpa_s->p2p_auto_pd) {
4576 int join = wpas_p2p_peer_go(wpa_s,
4577 wpa_s->pending_join_dev_addr);
4579 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4580 wpa_s->auto_pd_scan_retry++;
4581 bss = wpa_bss_get_bssid_latest(
4582 wpa_s, wpa_s->pending_join_dev_addr);
4585 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4586 "the peer " MACSTR " at %d MHz",
4587 wpa_s->auto_pd_scan_retry,
4589 pending_join_dev_addr),
4591 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4599 wpa_s->p2p_auto_pd = 0;
4600 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4601 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4602 MAC2STR(wpa_s->pending_join_dev_addr), join);
4603 if (p2p_prov_disc_req(wpa_s->global->p2p,
4604 wpa_s->pending_join_dev_addr, NULL,
4605 wpa_s->pending_pd_config_methods, join,
4606 0, wpa_s->user_initiated_pd) < 0) {
4607 wpa_s->p2p_auto_pd = 0;
4608 wpa_msg_global(wpa_s, MSG_INFO,
4609 P2P_EVENT_PROV_DISC_FAILURE
4610 " p2p_dev_addr=" MACSTR " status=N/A",
4611 MAC2STR(wpa_s->pending_join_dev_addr));
4616 if (wpa_s->p2p_auto_join) {
4617 int join = wpas_p2p_peer_go(wpa_s,
4618 wpa_s->pending_join_dev_addr);
4620 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4621 "running a GO -> use GO Negotiation");
4622 wpa_msg_global(wpa_s->parent, MSG_INFO,
4623 P2P_EVENT_FALLBACK_TO_GO_NEG
4624 "reason=peer-not-running-GO");
4625 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4626 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4627 wpa_s->p2p_persistent_group, 0, 0, 0,
4628 wpa_s->p2p_go_intent,
4629 wpa_s->p2p_connect_freq,
4630 wpa_s->p2p_persistent_id,
4631 wpa_s->p2p_pd_before_go_neg,
4637 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4638 "try to join the group", join ? "" :
4641 wpa_msg_global(wpa_s->parent, MSG_INFO,
4642 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
4643 wpa_s->p2p_fallback_to_go_neg = 1;
4647 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4648 wpa_s->pending_join_iface_addr);
4650 p2p_get_interface_addr(wpa_s->global->p2p,
4651 wpa_s->pending_join_dev_addr,
4653 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4654 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4655 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4656 "address for join from " MACSTR " to " MACSTR
4657 " based on newly discovered P2P peer entry",
4658 MAC2STR(wpa_s->pending_join_iface_addr),
4659 MAC2STR(iface_addr));
4660 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4663 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4664 wpa_s->pending_join_iface_addr);
4667 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4668 "from P2P peer table: %d MHz", freq);
4670 if (wpa_s->p2p_join_ssid_len) {
4671 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4672 MACSTR " and SSID %s",
4673 MAC2STR(wpa_s->pending_join_iface_addr),
4674 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4675 wpa_s->p2p_join_ssid_len));
4676 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4677 wpa_s->p2p_join_ssid,
4678 wpa_s->p2p_join_ssid_len);
4681 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4682 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4683 bss = wpa_bss_get_bssid_latest(wpa_s,
4684 wpa_s->pending_join_iface_addr);
4687 u8 dev_addr[ETH_ALEN];
4690 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4691 "from BSS table: %d MHz (SSID %s)", freq,
4692 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4693 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4695 os_memcmp(wpa_s->pending_join_dev_addr,
4696 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4697 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4699 wpa_printf(MSG_DEBUG,
4700 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4702 MAC2STR(wpa_s->pending_join_dev_addr));
4703 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4710 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4711 wpa_msg_global(wpa_s->parent, MSG_INFO,
4712 P2P_EVENT_GROUP_FORMATION_FAILURE
4713 "reason=FREQ_CONFLICT");
4714 wpas_notify_p2p_group_formation_failure(
4715 wpa_s, "FREQ_CONFLICT");
4719 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4720 "prior to joining an existing group (GO " MACSTR
4722 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4723 wpa_s->pending_pd_before_join = 1;
4725 switch (wpa_s->pending_join_wps_method) {
4726 case WPS_PIN_DISPLAY:
4727 method = WPS_CONFIG_KEYPAD;
4729 case WPS_PIN_KEYPAD:
4730 method = WPS_CONFIG_DISPLAY;
4733 method = WPS_CONFIG_PUSHBUTTON;
4736 method = WPS_CONFIG_P2PS;
4743 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4744 wpa_s->pending_join_dev_addr) ==
4747 * We have already performed provision discovery for
4748 * joining the group. Proceed directly to join
4749 * operation without duplicated provision discovery. */
4750 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4751 "with " MACSTR " already done - proceed to "
4753 MAC2STR(wpa_s->pending_join_dev_addr));
4754 wpa_s->pending_pd_before_join = 0;
4758 if (p2p_prov_disc_req(wpa_s->global->p2p,
4759 wpa_s->pending_join_dev_addr,
4761 freq, wpa_s->user_initiated_pd) < 0) {
4762 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4763 "Discovery Request before joining an "
4765 wpa_s->pending_pd_before_join = 0;
4771 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4772 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4773 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4774 wpas_p2p_check_join_scan_limit(wpa_s);
4778 /* Start join operation immediately */
4779 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4783 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4784 const u8 *ssid, size_t ssid_len)
4787 struct wpa_driver_scan_params params;
4788 struct wpabuf *wps_ie, *ies;
4790 int freqs[2] = { 0, 0 };
4792 os_memset(¶ms, 0, sizeof(params));
4794 /* P2P Wildcard SSID */
4795 params.num_ssids = 1;
4796 if (ssid && ssid_len) {
4797 params.ssids[0].ssid = ssid;
4798 params.ssids[0].ssid_len = ssid_len;
4799 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4800 wpa_s->p2p_join_ssid_len = ssid_len;
4802 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4803 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4804 wpa_s->p2p_join_ssid_len = 0;
4807 wpa_s->wps->dev.p2p = 1;
4808 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4809 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4811 if (wps_ie == NULL) {
4812 wpas_p2p_scan_res_join(wpa_s, NULL);
4816 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4817 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4819 wpabuf_free(wps_ie);
4820 wpas_p2p_scan_res_join(wpa_s, NULL);
4823 wpabuf_put_buf(ies, wps_ie);
4824 wpabuf_free(wps_ie);
4826 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4828 params.p2p_probe = 1;
4829 params.extra_ies = wpabuf_head(ies);
4830 params.extra_ies_len = wpabuf_len(ies);
4835 * If freq is not provided, check the operating freq of the GO
4836 * and use a single channel scan on if possible.
4838 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4839 wpa_s->pending_join_iface_addr);
4845 params.freqs = freqs;
4849 * Run a scan to update BSS table and start Provision Discovery once
4850 * the new scan results become available.
4852 ret = wpa_drv_scan(wpa_s, ¶ms);
4854 os_get_reltime(&wpa_s->scan_trigger_time);
4855 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4856 wpa_s->own_scan_requested = 1;
4862 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4864 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4865 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4866 wpas_p2p_check_join_scan_limit(wpa_s);
4871 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4873 struct wpa_supplicant *wpa_s = eloop_ctx;
4874 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4878 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4879 const u8 *dev_addr, enum p2p_wps_method wps_method,
4880 int auto_join, int op_freq,
4881 const u8 *ssid, size_t ssid_len)
4883 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4884 MACSTR " dev " MACSTR " op_freq=%d)%s",
4885 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4886 auto_join ? " (auto_join)" : "");
4887 if (ssid && ssid_len) {
4888 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4889 wpa_ssid_txt(ssid, ssid_len));
4892 wpa_s->p2p_auto_pd = 0;
4893 wpa_s->p2p_auto_join = !!auto_join;
4894 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4895 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4896 wpa_s->pending_join_wps_method = wps_method;
4898 /* Make sure we are not running find during connection establishment */
4899 wpas_p2p_stop_find(wpa_s);
4901 wpa_s->p2p_join_scan_count = 0;
4902 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4907 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4908 const u8 *ssid, size_t ssid_len)
4910 struct wpa_supplicant *group;
4911 struct p2p_go_neg_results res;
4912 struct wpa_bss *bss;
4914 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4917 if (group != wpa_s) {
4918 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4919 sizeof(group->p2p_pin));
4920 group->p2p_wps_method = wpa_s->p2p_wps_method;
4923 * Need to mark the current interface for p2p_group_formation
4924 * when a separate group interface is not used. This is needed
4925 * to allow p2p_cancel stop a pending p2p_connect-join.
4926 * wpas_p2p_init_group_interface() addresses this for the case
4927 * where a separate group interface is used.
4929 wpa_s->global->p2p_group_formation = wpa_s;
4932 group->p2p_in_provisioning = 1;
4933 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4935 os_memset(&res, 0, sizeof(res));
4936 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4937 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4939 res.wps_method = wpa_s->pending_join_wps_method;
4940 if (freq && ssid && ssid_len) {
4942 res.ssid_len = ssid_len;
4943 os_memcpy(res.ssid, ssid, ssid_len);
4945 bss = wpa_bss_get_bssid_latest(wpa_s,
4946 wpa_s->pending_join_iface_addr);
4948 res.freq = bss->freq;
4949 res.ssid_len = bss->ssid_len;
4950 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4951 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4953 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4957 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4958 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4960 wpa_drv_cancel_remain_on_channel(wpa_s);
4961 wpa_s->off_channel_freq = 0;
4962 wpa_s->roc_waiting_drv_freq = 0;
4964 wpas_start_wps_enrollee(group, &res);
4967 * Allow a longer timeout for join-a-running-group than normal 15
4968 * second group formation timeout since the GO may not have authorized
4969 * our connection yet.
4971 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4972 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4979 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4980 int *force_freq, int *pref_freq, int go,
4981 unsigned int *pref_freq_list,
4982 unsigned int *num_pref_freq)
4984 struct wpa_used_freq_data *freqs;
4985 int res, best_freq, num_unused;
4986 unsigned int freq_in_use = 0, num, i, max_pref_freq;
4988 max_pref_freq = *num_pref_freq;
4991 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4992 sizeof(struct wpa_used_freq_data));
4996 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4997 wpa_s->num_multichan_concurrent);
5000 * It is possible that the total number of used frequencies is bigger
5001 * than the number of frequencies used for P2P, so get the system wide
5002 * number of unused frequencies.
5004 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5006 wpa_printf(MSG_DEBUG,
5007 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5008 freq, wpa_s->num_multichan_concurrent, num, num_unused);
5013 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5015 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5017 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5018 ieee80211_is_dfs(freq)) {
5020 * If freq is a DFS channel and DFS is offloaded
5021 * to the driver, allow P2P GO to use it.
5023 wpa_printf(MSG_DEBUG,
5024 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5027 wpa_printf(MSG_DEBUG,
5028 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5035 for (i = 0; i < num; i++) {
5036 if (freqs[i].freq == freq)
5040 if (num_unused <= 0 && !freq_in_use) {
5041 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5046 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5047 "requested channel (%u MHz)", freq);
5052 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5054 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5055 enum wpa_driver_if_type iface_type;
5058 iface_type = WPA_IF_P2P_GO;
5060 iface_type = WPA_IF_P2P_CLIENT;
5062 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5065 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5068 if (!res && max_pref_freq > 0) {
5069 *num_pref_freq = max_pref_freq;
5071 while (wpas_p2p_disallowed_freq(wpa_s->global,
5072 pref_freq_list[i]) &&
5073 i < *num_pref_freq) {
5074 wpa_printf(MSG_DEBUG,
5075 "P2P: preferred_freq_list[%d]=%d is disallowed",
5076 i, pref_freq_list[i]);
5079 if (i != *num_pref_freq) {
5080 best_freq = pref_freq_list[i];
5081 wpa_printf(MSG_DEBUG,
5082 "P2P: Using preferred_freq_list[%d]=%d",
5085 wpa_printf(MSG_DEBUG,
5086 "P2P: All driver preferred frequencies are disallowed for P2P use");
5090 wpa_printf(MSG_DEBUG,
5091 "P2P: No preferred frequency list available");
5095 /* We have a candidate frequency to use */
5096 if (best_freq > 0) {
5097 if (*pref_freq == 0 && num_unused > 0) {
5098 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
5100 *pref_freq = best_freq;
5102 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
5104 *force_freq = best_freq;
5106 } else if (num_unused > 0) {
5107 wpa_printf(MSG_DEBUG,
5108 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5111 wpa_printf(MSG_DEBUG,
5112 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5126 * wpas_p2p_connect - Request P2P Group Formation to be started
5127 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5128 * @peer_addr: Address of the peer P2P Device
5129 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5130 * @persistent_group: Whether to create a persistent group
5131 * @auto_join: Whether to select join vs. GO Negotiation automatically
5132 * @join: Whether to join an existing group (as a client) instead of starting
5133 * Group Owner negotiation; @peer_addr is BSSID in that case
5134 * @auth: Whether to only authorize the connection instead of doing that and
5135 * initiating Group Owner negotiation
5136 * @go_intent: GO Intent or -1 to use default
5137 * @freq: Frequency for the group or 0 for auto-selection
5138 * @persistent_id: Persistent group credentials to use for forcing GO
5139 * parameters or -1 to generate new values (SSID/passphrase)
5140 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5141 * interoperability workaround when initiating group formation
5142 * @ht40: Start GO with 40 MHz channel width
5143 * @vht: Start GO with VHT support
5144 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5145 * failure, -2 on failure due to channel not currently available,
5146 * -3 if forced channel is not supported
5148 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5149 const char *pin, enum p2p_wps_method wps_method,
5150 int persistent_group, int auto_join, int join, int auth,
5151 int go_intent, int freq, int persistent_id, int pd,
5154 int force_freq = 0, pref_freq = 0;
5156 enum wpa_driver_if_type iftype;
5158 struct wpa_ssid *ssid = NULL;
5159 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
5161 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5164 if (persistent_id >= 0) {
5165 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5166 if (ssid == NULL || ssid->disabled != 2 ||
5167 ssid->mode != WPAS_MODE_P2P_GO)
5171 os_free(wpa_s->global->add_psk);
5172 wpa_s->global->add_psk = NULL;
5174 wpa_s->global->p2p_fail_on_wps_complete = 0;
5175 wpa_s->global->pending_p2ps_group = 0;
5176 wpa_s->global->pending_p2ps_group_freq = 0;
5177 wpa_s->p2ps_method_config_any = 0;
5180 go_intent = wpa_s->conf->p2p_go_intent;
5183 wpa_s->p2p_long_listen = 0;
5185 wpa_s->p2p_wps_method = wps_method;
5186 wpa_s->p2p_persistent_group = !!persistent_group;
5187 wpa_s->p2p_persistent_id = persistent_id;
5188 wpa_s->p2p_go_intent = go_intent;
5189 wpa_s->p2p_connect_freq = freq;
5190 wpa_s->p2p_fallback_to_go_neg = 0;
5191 wpa_s->p2p_pd_before_go_neg = !!pd;
5192 wpa_s->p2p_go_ht40 = !!ht40;
5193 wpa_s->p2p_go_vht = !!vht;
5196 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5197 else if (wps_method == WPS_PIN_DISPLAY) {
5198 ret = wps_generate_pin();
5199 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5201 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5202 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5203 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5206 wpa_s->p2p_pin[0] = '\0';
5208 if (join || auto_join) {
5209 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5211 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5212 "connect a running group from " MACSTR,
5213 MAC2STR(peer_addr));
5214 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5217 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5218 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5220 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5221 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5225 os_get_reltime(&wpa_s->p2p_auto_started);
5226 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5228 wpa_s->p2p_auto_started.sec,
5229 wpa_s->p2p_auto_started.usec);
5231 wpa_s->user_initiated_pd = 1;
5232 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5233 auto_join, freq, NULL, 0) < 0)
5238 size = P2P_MAX_PREF_CHANNELS;
5239 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5240 go_intent == 15, pref_freq_list, &size);
5243 wpas_p2p_set_own_freq_preference(wpa_s,
5244 force_freq ? force_freq : pref_freq);
5246 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5248 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5250 if (wpa_s->create_p2p_iface) {
5251 /* Prepare to add a new interface for the group */
5252 iftype = WPA_IF_P2P_GROUP;
5253 if (go_intent == 15)
5254 iftype = WPA_IF_P2P_GO;
5255 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5256 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5257 "interface for the group");
5261 if_addr = wpa_s->pending_interface_addr;
5263 if_addr = wpa_s->own_addr;
5264 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5268 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5270 force_freq, persistent_group, ssid,
5276 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5277 go_intent, if_addr, force_freq,
5278 persistent_group, ssid, pref_freq) < 0) {
5279 if (wpa_s->create_p2p_iface)
5280 wpas_p2p_remove_pending_group_interface(wpa_s);
5288 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5289 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5290 * @freq: Frequency of the channel in MHz
5291 * @duration: Duration of the stay on the channel in milliseconds
5293 * This callback is called when the driver indicates that it has started the
5294 * requested remain-on-channel duration.
5296 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5297 unsigned int freq, unsigned int duration)
5299 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5301 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)",
5302 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5303 wpa_s->roc_waiting_drv_freq, freq, duration);
5304 if (wpa_s->off_channel_freq &&
5305 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5306 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5307 wpa_s->pending_listen_duration);
5308 wpa_s->pending_listen_freq = 0;
5310 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5311 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5317 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5319 /* Limit maximum Listen state time based on driver limitation. */
5320 if (timeout > wpa_s->max_remain_on_chan)
5321 timeout = wpa_s->max_remain_on_chan;
5323 return p2p_listen(wpa_s->global->p2p, timeout);
5328 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5329 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5330 * @freq: Frequency of the channel in MHz
5332 * This callback is called when the driver indicates that a remain-on-channel
5333 * operation has been completed, i.e., the duration on the requested channel
5336 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5339 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5340 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5341 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5342 wpas_p2p_listen_work_done(wpa_s);
5343 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5345 if (wpa_s->p2p_long_listen > 0)
5346 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5347 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5348 return; /* P2P module started a new operation */
5349 if (offchannel_pending_action_tx(wpa_s))
5351 if (wpa_s->p2p_long_listen > 0) {
5352 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5353 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5356 * When listen duration is over, stop listen & update p2p_state
5359 p2p_stop_listen(wpa_s->global->p2p);
5365 * wpas_p2p_group_remove - Remove a P2P group
5366 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5367 * @ifname: Network interface name of the group interface or "*" to remove all
5369 * Returns: 0 on success, -1 on failure
5371 * This function is used to remove a P2P group. This can be used to disconnect
5372 * from a group in which the local end is a P2P Client or to end a P2P Group in
5373 * case the local end is the Group Owner. If a virtual network interface was
5374 * created for this group, that interface will be removed. Otherwise, only the
5375 * configured P2P group network will be removed from the interface.
5377 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5379 struct wpa_global *global = wpa_s->global;
5380 struct wpa_supplicant *calling_wpa_s = wpa_s;
5382 if (os_strcmp(ifname, "*") == 0) {
5383 struct wpa_supplicant *prev;
5384 wpa_s = global->ifaces;
5387 wpa_s = wpa_s->next;
5388 if (prev->p2p_group_interface !=
5389 NOT_P2P_GROUP_INTERFACE ||
5390 (prev->current_ssid &&
5391 prev->current_ssid->p2p_group))
5392 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5397 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5398 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5402 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5406 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5410 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5411 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5412 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5415 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5416 &size, pref_freq_list);
5417 if (!res && size > 0) {
5419 while (wpas_p2p_disallowed_freq(wpa_s->global,
5420 pref_freq_list[i]) &&
5422 wpa_printf(MSG_DEBUG,
5423 "P2P: preferred_freq_list[%d]=%d is disallowed",
5424 i, pref_freq_list[i]);
5428 freq = pref_freq_list[i];
5429 wpa_printf(MSG_DEBUG,
5430 "P2P: Using preferred_freq_list[%d]=%d",
5433 wpa_printf(MSG_DEBUG,
5434 "P2P: All driver preferred frequencies are disallowed for P2P use");
5437 wpa_printf(MSG_DEBUG,
5438 "P2P: No preferred frequency list available");
5443 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5445 if (wpa_s->best_24_freq > 0 &&
5446 p2p_supported_freq_go(wpa_s->global->p2p,
5447 wpa_s->best_24_freq)) {
5448 freq = wpa_s->best_24_freq;
5449 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5450 "channel: %d MHz", freq);
5452 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5454 freq = 2412 + (r % 3) * 25;
5455 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5456 "channel: %d MHz", freq);
5461 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5463 if (wpa_s->best_5_freq > 0 &&
5464 p2p_supported_freq_go(wpa_s->global->p2p,
5465 wpa_s->best_5_freq)) {
5466 freq = wpa_s->best_5_freq;
5467 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5468 "channel: %d MHz", freq);
5470 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5472 freq = 5180 + (r % 4) * 20;
5473 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5474 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5475 "5 GHz channel for P2P group");
5478 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5479 "channel: %d MHz", freq);
5483 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5484 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5485 ieee80211_is_dfs(freq)) {
5487 * If freq is a DFS channel and DFS is offloaded to the
5488 * driver, allow P2P GO to use it.
5490 wpa_printf(MSG_DEBUG, "P2P: "
5491 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5495 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5496 "(%u MHz) is not supported for P2P uses",
5505 static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5506 const struct p2p_channels *channels,
5509 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5510 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5511 freq_included(wpa_s, channels, freq))
5517 static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5518 struct p2p_go_neg_results *params,
5519 const struct p2p_channels *channels)
5523 /* first try some random selection of the social channels */
5524 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5527 for (i = 0; i < 3; i++) {
5528 params->freq = 2412 + ((r + i) % 3) * 25;
5529 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5533 /* try all other channels in operating class 81 */
5534 for (i = 0; i < 11; i++) {
5535 params->freq = 2412 + i * 5;
5537 /* skip social channels; covered in the previous loop */
5538 if (params->freq == 2412 ||
5539 params->freq == 2437 ||
5540 params->freq == 2462)
5543 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5547 /* try all channels in operating class 115 */
5548 for (i = 0; i < 4; i++) {
5549 params->freq = 5180 + i * 20;
5550 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5551 freq_included(wpa_s, channels, params->freq) &&
5552 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5556 /* try all channels in operating class 124 */
5557 for (i = 0; i < 4; i++) {
5558 params->freq = 5745 + i * 20;
5559 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5560 freq_included(wpa_s, channels, params->freq) &&
5561 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5565 /* try social channel class 180 channel 2 */
5566 params->freq = 58320 + 1 * 2160;
5567 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5568 freq_included(wpa_s, channels, params->freq) &&
5569 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5572 /* try all channels in reg. class 180 */
5573 for (i = 0; i < 4; i++) {
5574 params->freq = 58320 + i * 2160;
5575 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5576 freq_included(wpa_s, channels, params->freq) &&
5577 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5582 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
5585 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5590 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5591 struct p2p_go_neg_results *params,
5592 int freq, int ht40, int vht,
5593 const struct p2p_channels *channels)
5595 struct wpa_used_freq_data *freqs;
5597 unsigned int num, i;
5598 int ignore_no_freqs = 0;
5600 os_memset(params, 0, sizeof(*params));
5601 params->role_go = 1;
5602 params->ht40 = ht40;
5605 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5606 sizeof(struct wpa_used_freq_data));
5610 num = get_shared_radio_freqs_data(wpa_s, freqs,
5611 wpa_s->num_multichan_concurrent);
5613 if (wpa_s->current_ssid &&
5614 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5615 wpa_s->wpa_state == WPA_COMPLETED) {
5616 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5620 * If the frequency selection is done for an active P2P GO that
5621 * is not sharing a frequency, allow to select a new frequency
5622 * even if there are no unused frequencies as we are about to
5623 * move the P2P GO so its frequency can be re-used.
5625 for (i = 0; i < num; i++) {
5626 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5627 freqs[i].flags == 0) {
5628 ignore_no_freqs = 1;
5634 /* try using the forced freq */
5636 if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
5637 wpa_printf(MSG_DEBUG,
5638 "P2P: Forced GO freq %d MHz not accepted",
5643 for (i = 0; i < num; i++) {
5644 if (freqs[i].freq == freq) {
5645 wpa_printf(MSG_DEBUG,
5646 "P2P: forced freq (%d MHz) is also shared",
5648 params->freq = freq;
5653 if (!ignore_no_freqs &&
5654 wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5655 wpa_printf(MSG_DEBUG,
5656 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5661 wpa_printf(MSG_DEBUG,
5662 "P2P: force GO freq (%d MHz) on a free channel",
5664 params->freq = freq;
5668 /* consider using one of the shared frequencies */
5670 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5671 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5672 wpa_printf(MSG_DEBUG,
5673 "P2P: Use shared freq (%d MHz) for GO",
5675 params->freq = cand;
5679 /* try using one of the shared freqs */
5680 for (i = 0; i < num; i++) {
5681 if (wpas_p2p_supported_freq_go(wpa_s, channels,
5683 wpa_printf(MSG_DEBUG,
5684 "P2P: Use shared freq (%d MHz) for GO",
5686 params->freq = freqs[i].freq;
5692 if (!ignore_no_freqs &&
5693 wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5694 wpa_printf(MSG_DEBUG,
5695 "P2P: Cannot force GO on any of the channels we are already using");
5699 /* try using the setting from the configuration file */
5700 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5701 wpa_s->conf->p2p_oper_channel >= 1 &&
5702 wpa_s->conf->p2p_oper_channel <= 11 &&
5703 wpas_p2p_supported_freq_go(
5705 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5706 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5707 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5708 "frequency %d MHz", params->freq);
5712 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5713 wpa_s->conf->p2p_oper_reg_class == 116 ||
5714 wpa_s->conf->p2p_oper_reg_class == 117 ||
5715 wpa_s->conf->p2p_oper_reg_class == 124 ||
5716 wpa_s->conf->p2p_oper_reg_class == 125 ||
5717 wpa_s->conf->p2p_oper_reg_class == 126 ||
5718 wpa_s->conf->p2p_oper_reg_class == 127) &&
5719 wpas_p2p_supported_freq_go(wpa_s, channels,
5721 5 * wpa_s->conf->p2p_oper_channel)) {
5722 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5723 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5724 "frequency %d MHz", params->freq);
5728 /* Try using best channels */
5729 if (wpa_s->conf->p2p_oper_channel == 0 &&
5730 wpa_s->best_overall_freq > 0 &&
5731 wpas_p2p_supported_freq_go(wpa_s, channels,
5732 wpa_s->best_overall_freq)) {
5733 params->freq = wpa_s->best_overall_freq;
5734 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5735 "channel %d MHz", params->freq);
5739 if (wpa_s->conf->p2p_oper_channel == 0 &&
5740 wpa_s->best_24_freq > 0 &&
5741 wpas_p2p_supported_freq_go(wpa_s, channels,
5742 wpa_s->best_24_freq)) {
5743 params->freq = wpa_s->best_24_freq;
5744 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5745 "channel %d MHz", params->freq);
5749 if (wpa_s->conf->p2p_oper_channel == 0 &&
5750 wpa_s->best_5_freq > 0 &&
5751 wpas_p2p_supported_freq_go(wpa_s, channels,
5752 wpa_s->best_5_freq)) {
5753 params->freq = wpa_s->best_5_freq;
5754 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5755 "channel %d MHz", params->freq);
5759 /* try using preferred channels */
5760 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
5761 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5762 params->freq = cand;
5763 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5764 "channels", params->freq);
5768 /* Try using one of the group common freqs */
5769 if (wpa_s->p2p_group_common_freqs) {
5770 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
5771 cand = wpa_s->p2p_group_common_freqs[i];
5772 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5773 params->freq = cand;
5774 wpa_printf(MSG_DEBUG,
5775 "P2P: Use freq %d MHz common with the peer",
5782 /* no preference, select some channel */
5783 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
5785 if (params->freq == 0) {
5786 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
5799 static struct wpa_supplicant *
5800 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5803 struct wpa_supplicant *group_wpa_s;
5805 if (!wpas_p2p_create_iface(wpa_s)) {
5806 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5808 wpa_s->p2p_first_connection_timeout = 0;
5812 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5813 WPA_IF_P2P_CLIENT) < 0) {
5814 wpa_msg_global(wpa_s, MSG_ERROR,
5815 "P2P: Failed to add group interface");
5818 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5819 if (group_wpa_s == NULL) {
5820 wpa_msg_global(wpa_s, MSG_ERROR,
5821 "P2P: Failed to initialize group interface");
5822 wpas_p2p_remove_pending_group_interface(wpa_s);
5826 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5827 group_wpa_s->ifname);
5828 group_wpa_s->p2p_first_connection_timeout = 0;
5834 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5835 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5836 * @persistent_group: Whether to create a persistent group
5837 * @freq: Frequency for the group or 0 to indicate no hardcoding
5838 * @ht40: Start GO with 40 MHz channel width
5839 * @vht: Start GO with VHT support
5840 * Returns: 0 on success, -1 on failure
5842 * This function creates a new P2P group with the local end as the Group Owner,
5843 * i.e., without using Group Owner Negotiation.
5845 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5846 int freq, int ht40, int vht)
5848 struct p2p_go_neg_results params;
5850 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5853 os_free(wpa_s->global->add_psk);
5854 wpa_s->global->add_psk = NULL;
5856 /* Make sure we are not running find during connection establishment */
5857 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5858 wpas_p2p_stop_find_oper(wpa_s);
5860 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5864 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5867 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5868 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5869 ieee80211_is_dfs(params.freq)) {
5871 * If freq is a DFS channel and DFS is offloaded to the
5872 * driver, allow P2P GO to use it.
5874 wpa_printf(MSG_DEBUG,
5875 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5876 __func__, params.freq);
5878 wpa_printf(MSG_DEBUG,
5879 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5884 p2p_go_params(wpa_s->global->p2p, ¶ms);
5885 params.persistent_group = persistent_group;
5887 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5890 wpas_start_wps_go(wpa_s, ¶ms, 0);
5896 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5897 struct wpa_ssid *params, int addr_allocated,
5898 int freq, int force_scan)
5900 struct wpa_ssid *ssid;
5902 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5906 os_get_reltime(&wpa_s->scan_min_time);
5907 wpa_s->p2p_last_4way_hs_fail = NULL;
5909 wpa_supplicant_ap_deinit(wpa_s);
5911 ssid = wpa_config_add_network(wpa_s->conf);
5914 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5915 wpa_config_set_network_defaults(ssid);
5916 ssid->temporary = 1;
5917 ssid->proto = WPA_PROTO_RSN;
5918 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5919 ssid->group_cipher = WPA_CIPHER_CCMP;
5920 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5921 ssid->ssid = os_malloc(params->ssid_len);
5922 if (ssid->ssid == NULL) {
5923 wpa_config_remove_network(wpa_s->conf, ssid->id);
5926 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5927 ssid->ssid_len = params->ssid_len;
5928 ssid->p2p_group = 1;
5929 ssid->export_keys = 1;
5930 if (params->psk_set) {
5931 os_memcpy(ssid->psk, params->psk, 32);
5934 if (params->passphrase)
5935 ssid->passphrase = os_strdup(params->passphrase);
5937 wpa_s->show_group_started = 1;
5938 wpa_s->p2p_in_invitation = 1;
5939 wpa_s->p2p_invite_go_freq = freq;
5941 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5943 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5944 wpas_p2p_group_formation_timeout,
5945 wpa_s->parent, NULL);
5946 wpa_supplicant_select_network(wpa_s, ssid);
5952 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5953 struct wpa_ssid *ssid, int addr_allocated,
5954 int force_freq, int neg_freq, int ht40,
5955 int vht, const struct p2p_channels *channels,
5956 int connection_timeout, int force_scan)
5958 struct p2p_go_neg_results params;
5961 if (ssid->disabled != 2 || ssid->ssid == NULL)
5964 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5965 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5966 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5969 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5970 wpa_s->parent, NULL)) {
5972 * This can happen if Invitation Response frame was lost
5973 * and the peer (GO of a persistent group) tries to
5974 * invite us again. Reschedule the timeout to avoid
5975 * terminating the wait for the connection too early
5976 * since we now know that the peer is still trying to
5977 * invite us instead of having already started the GO.
5979 wpa_printf(MSG_DEBUG,
5980 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
5981 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5982 wpas_p2p_group_formation_timeout,
5983 wpa_s->parent, NULL);
5988 os_free(wpa_s->global->add_psk);
5989 wpa_s->global->add_psk = NULL;
5991 /* Make sure we are not running find during connection establishment */
5992 wpas_p2p_stop_find_oper(wpa_s);
5994 wpa_s->p2p_fallback_to_go_neg = 0;
5996 if (ssid->mode == WPAS_MODE_P2P_GO) {
5997 if (force_freq > 0) {
5998 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6002 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6004 (freq > 0 && !freq_included(wpa_s, channels, freq)))
6007 } else if (ssid->mode == WPAS_MODE_INFRA) {
6009 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
6010 struct os_reltime now;
6011 struct wpa_bss *bss =
6012 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
6014 os_get_reltime(&now);
6016 !os_reltime_expired(&now, &bss->last_update, 5) &&
6017 freq_included(wpa_s, channels, bss->freq))
6023 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6029 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
6033 params.psk_set = ssid->psk_set;
6035 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
6036 if (ssid->passphrase) {
6037 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6038 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6039 "persistent group");
6042 os_strlcpy(params.passphrase, ssid->passphrase,
6043 sizeof(params.passphrase));
6045 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6046 params.ssid_len = ssid->ssid_len;
6047 params.persistent_group = 1;
6049 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6053 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6055 wpa_s->p2p_first_connection_timeout = connection_timeout;
6056 wpas_start_wps_go(wpa_s, ¶ms, 0);
6062 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6063 struct wpabuf *proberesp_ies)
6065 struct wpa_supplicant *wpa_s = ctx;
6066 if (wpa_s->ap_iface) {
6067 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
6068 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6069 wpabuf_free(beacon_ies);
6070 wpabuf_free(proberesp_ies);
6074 wpabuf_free(hapd->p2p_beacon_ie);
6075 hapd->p2p_beacon_ie = beacon_ies;
6077 wpabuf_free(hapd->p2p_probe_resp_ie);
6078 hapd->p2p_probe_resp_ie = proberesp_ies;
6080 wpabuf_free(beacon_ies);
6081 wpabuf_free(proberesp_ies);
6083 wpa_supplicant_ap_update_beacon(wpa_s);
6087 static void wpas_p2p_idle_update(void *ctx, int idle)
6089 struct wpa_supplicant *wpa_s = ctx;
6090 if (!wpa_s->ap_iface)
6092 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
6094 if (wpa_s->global->p2p_fail_on_wps_complete &&
6095 wpa_s->p2p_in_provisioning) {
6096 wpas_p2p_grpform_fail_after_wps(wpa_s);
6099 wpas_p2p_set_group_idle_timeout(wpa_s);
6101 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6105 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6106 struct wpa_ssid *ssid)
6108 struct p2p_group *group;
6109 struct p2p_group_config *cfg;
6111 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6114 cfg = os_zalloc(sizeof(*cfg));
6118 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
6119 cfg->persistent_group = 2;
6120 else if (ssid->p2p_persistent_group)
6121 cfg->persistent_group = 1;
6122 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6123 if (wpa_s->max_stations &&
6124 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6125 cfg->max_clients = wpa_s->max_stations;
6127 cfg->max_clients = wpa_s->conf->max_num_sta;
6128 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6129 cfg->ssid_len = ssid->ssid_len;
6130 cfg->freq = ssid->frequency;
6131 cfg->cb_ctx = wpa_s;
6132 cfg->ie_update = wpas_p2p_ie_update;
6133 cfg->idle_update = wpas_p2p_idle_update;
6135 group = p2p_group_init(wpa_s->global->p2p, cfg);
6138 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
6139 p2p_group_notif_formation_done(group);
6140 wpa_s->p2p_group = group;
6145 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6148 struct wpa_ssid *ssid = wpa_s->current_ssid;
6150 if (!wpa_s->p2p_in_provisioning) {
6151 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6152 "provisioning not in progress");
6156 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6157 u8 go_dev_addr[ETH_ALEN];
6158 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6159 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6161 /* Clear any stored provisioning info */
6162 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6165 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6167 wpa_s->p2p_go_group_formation_completed = 1;
6168 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6170 * Use a separate timeout for initial data connection to
6171 * complete to allow the group to be removed automatically if
6172 * something goes wrong in this step before the P2P group idle
6173 * timeout mechanism is taken into use.
6175 wpa_dbg(wpa_s, MSG_DEBUG,
6176 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6177 P2P_MAX_INITIAL_CONN_WAIT);
6178 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6179 wpas_p2p_group_formation_timeout,
6180 wpa_s->parent, NULL);
6183 * Use a separate timeout for initial data connection to
6184 * complete to allow the group to be removed automatically if
6185 * the client does not complete data connection successfully.
6187 wpa_dbg(wpa_s, MSG_DEBUG,
6188 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6189 P2P_MAX_INITIAL_CONN_WAIT_GO);
6190 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6191 wpas_p2p_group_formation_timeout,
6192 wpa_s->parent, NULL);
6194 * Complete group formation on first successful data connection
6196 wpa_s->p2p_go_group_formation_completed = 0;
6198 if (wpa_s->global->p2p)
6199 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
6200 wpas_group_formation_completed(wpa_s, 1, 0);
6204 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6205 struct wps_event_fail *fail)
6207 if (!wpa_s->p2p_in_provisioning) {
6208 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6209 "provisioning not in progress");
6213 if (wpa_s->go_params) {
6214 p2p_clear_provisioning_info(
6216 wpa_s->go_params->peer_device_addr);
6219 wpas_notify_p2p_wps_failed(wpa_s, fail);
6221 if (wpa_s == wpa_s->global->p2p_group_formation) {
6223 * Allow some time for the failed WPS negotiation exchange to
6224 * complete, but remove the group since group formation cannot
6225 * succeed after provisioning failure.
6227 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6228 wpa_s->global->p2p_fail_on_wps_complete = 1;
6229 eloop_deplete_timeout(0, 50000,
6230 wpas_p2p_group_formation_timeout,
6231 wpa_s->parent, NULL);
6236 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6238 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6239 !wpa_s->p2p_in_provisioning)
6242 wpas_p2p_grpform_fail_after_wps(wpa_s);
6248 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6249 const char *config_method,
6250 enum wpas_p2p_prov_disc_use use,
6251 struct p2ps_provision *p2ps_prov)
6255 wpa_s->global->pending_p2ps_group = 0;
6256 wpa_s->global->pending_p2ps_group_freq = 0;
6257 wpa_s->p2p_fallback_to_go_neg = 0;
6258 wpa_s->pending_pd_use = NORMAL_PD;
6259 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6260 p2ps_prov->conncap = p2ps_group_capability(
6261 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6262 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6264 wpa_printf(MSG_DEBUG,
6265 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6266 __func__, p2ps_prov->conncap,
6267 p2ps_prov->adv_id, p2ps_prov->conncap,
6268 p2ps_prov->status, p2ps_prov->info);
6271 } else if (os_strncmp(config_method, "display", 7) == 0)
6272 config_methods = WPS_CONFIG_DISPLAY;
6273 else if (os_strncmp(config_method, "keypad", 6) == 0)
6274 config_methods = WPS_CONFIG_KEYPAD;
6275 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6276 os_strncmp(config_method, "pushbutton", 10) == 0)
6277 config_methods = WPS_CONFIG_PUSHBUTTON;
6279 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
6284 if (use == WPAS_P2P_PD_AUTO) {
6285 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6286 wpa_s->pending_pd_config_methods = config_methods;
6287 wpa_s->p2p_auto_pd = 1;
6288 wpa_s->p2p_auto_join = 0;
6289 wpa_s->pending_pd_before_join = 0;
6290 wpa_s->auto_pd_scan_retry = 0;
6291 wpas_p2p_stop_find(wpa_s);
6292 wpa_s->p2p_join_scan_count = 0;
6293 os_get_reltime(&wpa_s->p2p_auto_started);
6294 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6295 wpa_s->p2p_auto_started.sec,
6296 wpa_s->p2p_auto_started.usec);
6297 wpas_p2p_join_scan(wpa_s, NULL);
6301 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6306 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6307 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6312 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6315 return p2p_scan_result_text(ies, ies_len, buf, end);
6319 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6321 if (!offchannel_pending_action_tx(wpa_s))
6324 wpas_p2p_action_tx_clear(wpa_s);
6326 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6327 "operation request");
6328 offchannel_clear_pending_action_tx(wpa_s);
6332 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6333 enum p2p_discovery_type type,
6334 unsigned int num_req_dev_types, const u8 *req_dev_types,
6335 const u8 *dev_id, unsigned int search_delay,
6336 u8 seek_cnt, const char **seek_string, int freq)
6338 wpas_p2p_clear_pending_action_tx(wpa_s);
6339 wpa_s->p2p_long_listen = 0;
6341 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6342 wpa_s->p2p_in_provisioning)
6345 wpa_supplicant_cancel_sched_scan(wpa_s);
6347 return p2p_find(wpa_s->global->p2p, timeout, type,
6348 num_req_dev_types, req_dev_types, dev_id,
6349 search_delay, seek_cnt, seek_string, freq);
6353 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6354 struct wpa_scan_results *scan_res)
6356 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6358 if (wpa_s->p2p_scan_work) {
6359 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6360 wpa_s->p2p_scan_work = NULL;
6361 radio_work_done(work);
6364 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6368 * Indicate that results have been processed so that the P2P module can
6369 * continue pending tasks.
6371 p2p_scan_res_handled(wpa_s->global->p2p);
6375 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
6377 wpas_p2p_clear_pending_action_tx(wpa_s);
6378 wpa_s->p2p_long_listen = 0;
6379 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6380 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6382 if (wpa_s->global->p2p)
6383 p2p_stop_find(wpa_s->global->p2p);
6385 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6386 wpa_printf(MSG_DEBUG,
6387 "P2P: Do not consider the scan results after stop_find");
6388 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6393 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6395 wpas_p2p_stop_find_oper(wpa_s);
6396 if (!wpa_s->global->pending_group_iface_for_p2ps)
6397 wpas_p2p_remove_pending_group_interface(wpa_s);
6401 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6403 struct wpa_supplicant *wpa_s = eloop_ctx;
6404 wpa_s->p2p_long_listen = 0;
6408 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6412 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6415 wpa_supplicant_cancel_sched_scan(wpa_s);
6416 wpas_p2p_clear_pending_action_tx(wpa_s);
6420 * This is a request for unlimited Listen state. However, at
6421 * least for now, this is mapped to a Listen state for one
6426 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6427 wpa_s->p2p_long_listen = 0;
6430 * Stop previous find/listen operation to avoid trying to request a new
6431 * remain-on-channel operation while the driver is still running the
6434 if (wpa_s->global->p2p)
6435 p2p_stop_find(wpa_s->global->p2p);
6437 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6438 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6439 wpa_s->p2p_long_listen = timeout * 1000;
6440 eloop_register_timeout(timeout, 0,
6441 wpas_p2p_long_listen_timeout,
6449 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6450 u8 *buf, size_t len, int p2p_group)
6452 struct wpabuf *p2p_ie;
6455 if (wpa_s->global->p2p_disabled)
6458 * Advertize mandatory cross connection capability even on
6459 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6461 if (wpa_s->conf->p2p_disabled && p2p_group)
6463 if (wpa_s->global->p2p == NULL)
6468 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6469 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6471 wpabuf_free(p2p_ie);
6477 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
6478 const u8 *dst, const u8 *bssid,
6479 const u8 *ie, size_t ie_len,
6480 unsigned int rx_freq, int ssi_signal)
6482 if (wpa_s->global->p2p_disabled)
6484 if (wpa_s->global->p2p == NULL)
6487 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6488 ie, ie_len, rx_freq)) {
6489 case P2P_PREQ_NOT_P2P:
6490 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6493 case P2P_PREQ_MALFORMED:
6494 case P2P_PREQ_NOT_LISTEN:
6495 case P2P_PREQ_NOT_PROCESSED:
6496 default: /* make gcc happy */
6498 case P2P_PREQ_PROCESSED:
6504 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6505 const u8 *sa, const u8 *bssid,
6506 u8 category, const u8 *data, size_t len, int freq)
6508 if (wpa_s->global->p2p_disabled)
6510 if (wpa_s->global->p2p == NULL)
6513 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6518 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6520 if (wpa_s->global->p2p_disabled)
6522 if (wpa_s->global->p2p == NULL)
6525 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6529 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6531 p2p_group_deinit(wpa_s->p2p_group);
6532 wpa_s->p2p_group = NULL;
6534 wpa_s->ap_configured_cb = NULL;
6535 wpa_s->ap_configured_cb_ctx = NULL;
6536 wpa_s->ap_configured_cb_data = NULL;
6537 wpa_s->connect_without_scan = NULL;
6541 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6543 wpa_s->p2p_long_listen = 0;
6545 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6548 return p2p_reject(wpa_s->global->p2p, addr);
6552 /* Invite to reinvoke a persistent group */
6553 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6554 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6555 int ht40, int vht, int pref_freq)
6557 enum p2p_invite_role role;
6561 int no_pref_freq_given = pref_freq == 0;
6562 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6564 wpa_s->global->p2p_invite_group = NULL;
6566 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6568 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6570 wpa_s->p2p_persistent_go_freq = freq;
6571 wpa_s->p2p_go_ht40 = !!ht40;
6572 if (ssid->mode == WPAS_MODE_P2P_GO) {
6573 role = P2P_INVITE_ROLE_GO;
6574 if (peer_addr == NULL) {
6575 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6576 "address in invitation command");
6579 if (wpas_p2p_create_iface(wpa_s)) {
6580 if (wpas_p2p_add_group_interface(wpa_s,
6581 WPA_IF_P2P_GO) < 0) {
6582 wpa_printf(MSG_ERROR, "P2P: Failed to "
6583 "allocate a new interface for the "
6587 bssid = wpa_s->pending_interface_addr;
6589 bssid = wpa_s->own_addr;
6591 role = P2P_INVITE_ROLE_CLIENT;
6592 peer_addr = ssid->bssid;
6594 wpa_s->pending_invite_ssid_id = ssid->id;
6596 size = P2P_MAX_PREF_CHANNELS;
6597 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6598 role == P2P_INVITE_ROLE_GO,
6599 pref_freq_list, &size);
6602 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6604 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6607 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6608 no_pref_freq_given && pref_freq > 0 &&
6609 wpa_s->num_multichan_concurrent > 1 &&
6610 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6611 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6617 * Stop any find/listen operations before invitation and possibly
6618 * connection establishment.
6620 wpas_p2p_stop_find_oper(wpa_s);
6622 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6623 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6628 /* Invite to join an active group */
6629 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6630 const u8 *peer_addr, const u8 *go_dev_addr)
6632 struct wpa_global *global = wpa_s->global;
6633 enum p2p_invite_role role;
6635 struct wpa_ssid *ssid;
6637 int freq = 0, force_freq = 0, pref_freq = 0;
6639 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6641 wpa_s->p2p_persistent_go_freq = 0;
6642 wpa_s->p2p_go_ht40 = 0;
6643 wpa_s->p2p_go_vht = 0;
6645 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6646 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6649 if (wpa_s == NULL) {
6650 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6654 ssid = wpa_s->current_ssid;
6656 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6661 wpa_s->global->p2p_invite_group = wpa_s;
6662 persistent = ssid->p2p_persistent_group &&
6663 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6664 ssid->ssid, ssid->ssid_len);
6666 if (ssid->mode == WPAS_MODE_P2P_GO) {
6667 role = P2P_INVITE_ROLE_ACTIVE_GO;
6668 bssid = wpa_s->own_addr;
6669 if (go_dev_addr == NULL)
6670 go_dev_addr = wpa_s->global->p2p_dev_addr;
6671 freq = ssid->frequency;
6673 role = P2P_INVITE_ROLE_CLIENT;
6674 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6675 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6676 "invite to current group");
6679 bssid = wpa_s->bssid;
6680 if (go_dev_addr == NULL &&
6681 !is_zero_ether_addr(wpa_s->go_dev_addr))
6682 go_dev_addr = wpa_s->go_dev_addr;
6683 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6684 (int) wpa_s->assoc_freq;
6686 wpa_s->parent->pending_invite_ssid_id = -1;
6688 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6691 size = P2P_MAX_PREF_CHANNELS;
6692 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6693 role == P2P_INVITE_ROLE_ACTIVE_GO,
6694 pref_freq_list, &size);
6697 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6699 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6700 ssid->ssid, ssid->ssid_len, force_freq,
6701 go_dev_addr, persistent, pref_freq, -1);
6705 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6707 struct wpa_ssid *ssid = wpa_s->current_ssid;
6708 u8 go_dev_addr[ETH_ALEN];
6709 int network_id = -1;
6715 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6716 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6717 wpa_s->parent, NULL);
6720 if (!wpa_s->show_group_started || !ssid)
6723 wpa_s->show_group_started = 0;
6725 os_memset(go_dev_addr, 0, ETH_ALEN);
6726 if (ssid->bssid_set)
6727 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6728 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6730 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6732 if (wpa_s->global->p2p_group_formation == wpa_s)
6733 wpa_s->global->p2p_group_formation = NULL;
6735 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6736 (int) wpa_s->assoc_freq;
6739 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6742 res = os_snprintf(ip_addr, sizeof(ip_addr),
6743 " ip_addr=%u.%u.%u.%u "
6744 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6745 ip[0], ip[1], ip[2], ip[3],
6746 ip[4], ip[5], ip[6], ip[7],
6747 ip[8], ip[9], ip[10], ip[11]);
6748 if (os_snprintf_error(sizeof(ip_addr), res))
6752 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6753 ssid->passphrase == NULL && ssid->psk_set ?
6755 ssid->passphrase, go_dev_addr, persistent,
6759 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6762 network_id = ssid->id;
6763 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6767 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6768 u32 interval1, u32 duration2, u32 interval2)
6772 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6775 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6776 wpa_s->current_ssid == NULL ||
6777 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6780 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6781 wpa_s->own_addr, wpa_s->assoc_freq,
6782 duration1, interval1, duration2, interval2);
6784 wpa_s->waiting_presence_resp = 1;
6790 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6791 unsigned int interval)
6793 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6796 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6800 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6802 if (wpa_s->current_ssid == NULL) {
6804 * current_ssid can be cleared when P2P client interface gets
6805 * disconnected, so assume this interface was used as P2P
6810 return wpa_s->current_ssid->p2p_group &&
6811 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6815 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6817 struct wpa_supplicant *wpa_s = eloop_ctx;
6819 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6820 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6825 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6827 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6831 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6835 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6836 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6838 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6841 timeout = wpa_s->conf->p2p_group_idle;
6842 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6843 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6844 timeout = P2P_MAX_CLIENT_IDLE;
6850 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6851 timeout = 0; /* special client mode no-timeout */
6856 if (wpa_s->p2p_in_provisioning) {
6858 * Use the normal group formation timeout during the
6859 * provisioning phase to avoid terminating this process too
6860 * early due to group idle timeout.
6862 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6863 "during provisioning");
6867 if (wpa_s->show_group_started) {
6869 * Use the normal group formation timeout between the end of
6870 * the provisioning phase and completion of 4-way handshake to
6871 * avoid terminating this process too early due to group idle
6874 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6875 "while waiting for initial 4-way handshake to "
6880 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6882 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6887 /* Returns 1 if the interface was removed */
6888 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6889 u16 reason_code, const u8 *ie, size_t ie_len,
6890 int locally_generated)
6892 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6895 if (!locally_generated)
6896 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6899 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6900 wpa_s->current_ssid &&
6901 wpa_s->current_ssid->p2p_group &&
6902 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6903 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6904 "session is ending");
6905 if (wpas_p2p_group_delete(wpa_s,
6906 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6915 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6916 u16 reason_code, const u8 *ie, size_t ie_len,
6917 int locally_generated)
6919 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6922 if (!locally_generated)
6923 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6928 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6930 struct p2p_data *p2p = wpa_s->global->p2p;
6935 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6938 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6939 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6941 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6942 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6945 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6946 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6948 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6949 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6951 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6952 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6953 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6954 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6955 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6958 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6959 p2p_set_sec_dev_types(p2p,
6960 (void *) wpa_s->conf->sec_device_type,
6961 wpa_s->conf->num_sec_device_types);
6963 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6965 p2p_remove_wps_vendor_extensions(p2p);
6966 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6967 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6969 p2p_add_wps_vendor_extension(
6970 p2p, wpa_s->conf->wps_vendor_ext[i]);
6974 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6975 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6977 country[0] = wpa_s->conf->country[0];
6978 country[1] = wpa_s->conf->country[1];
6980 p2p_set_country(p2p, country);
6983 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6984 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6985 wpa_s->conf->p2p_ssid_postfix ?
6986 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6990 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6991 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6993 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6994 u8 reg_class, channel;
6999 if (wpa_s->conf->p2p_listen_reg_class &&
7000 wpa_s->conf->p2p_listen_channel) {
7001 reg_class = wpa_s->conf->p2p_listen_reg_class;
7002 channel = wpa_s->conf->p2p_listen_channel;
7007 * Pick one of the social channels randomly as the
7010 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7013 channel = 1 + (r % 3) * 5;
7016 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7019 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7022 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7023 u8 op_reg_class, op_channel, cfg_op_channel;
7026 if (wpa_s->conf->p2p_oper_reg_class &&
7027 wpa_s->conf->p2p_oper_channel) {
7028 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7029 op_channel = wpa_s->conf->p2p_oper_channel;
7034 * Use random operation channel from (1, 6, 11)
7035 *if no other preference is indicated.
7037 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7040 op_channel = 1 + (r % 3) * 5;
7043 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7046 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7050 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7051 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7052 wpa_s->conf->p2p_pref_chan) < 0) {
7053 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7057 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7058 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7063 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7064 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
7068 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7071 if (!wpa_s->ap_iface)
7073 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7078 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7080 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7083 wpa_s->global->cross_connection = enabled;
7084 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7087 struct wpa_supplicant *iface;
7089 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7091 if (iface->cross_connect_enabled == 0)
7094 iface->cross_connect_enabled = 0;
7095 iface->cross_connect_in_use = 0;
7096 wpa_msg_global(iface->parent, MSG_INFO,
7097 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7099 iface->cross_connect_uplink);
7107 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7109 struct wpa_supplicant *iface;
7111 if (!uplink->global->cross_connection)
7114 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7115 if (!iface->cross_connect_enabled)
7117 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7120 if (iface->ap_iface == NULL)
7122 if (iface->cross_connect_in_use)
7125 iface->cross_connect_in_use = 1;
7126 wpa_msg_global(iface->parent, MSG_INFO,
7127 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7128 iface->ifname, iface->cross_connect_uplink);
7133 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7135 struct wpa_supplicant *iface;
7137 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7138 if (!iface->cross_connect_enabled)
7140 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7143 if (!iface->cross_connect_in_use)
7146 wpa_msg_global(iface->parent, MSG_INFO,
7147 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7148 iface->ifname, iface->cross_connect_uplink);
7149 iface->cross_connect_in_use = 0;
7154 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7156 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7157 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7158 wpa_s->cross_connect_disallowed)
7159 wpas_p2p_disable_cross_connect(wpa_s);
7161 wpas_p2p_enable_cross_connect(wpa_s);
7162 if (!wpa_s->ap_iface &&
7163 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7164 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7168 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7170 wpas_p2p_disable_cross_connect(wpa_s);
7171 if (!wpa_s->ap_iface &&
7172 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7174 wpas_p2p_set_group_idle_timeout(wpa_s);
7178 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7180 struct wpa_supplicant *iface;
7182 if (!wpa_s->global->cross_connection)
7185 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7188 if (iface->drv_flags &
7189 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7191 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7192 iface != wpa_s->parent)
7195 wpa_s->cross_connect_enabled = 1;
7196 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7197 sizeof(wpa_s->cross_connect_uplink));
7198 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7199 "%s to %s whenever uplink is available",
7200 wpa_s->ifname, wpa_s->cross_connect_uplink);
7202 if (iface->ap_iface || iface->current_ssid == NULL ||
7203 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7204 iface->cross_connect_disallowed ||
7205 iface->wpa_state != WPA_COMPLETED)
7208 wpa_s->cross_connect_in_use = 1;
7209 wpa_msg_global(wpa_s->parent, MSG_INFO,
7210 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7211 wpa_s->ifname, wpa_s->cross_connect_uplink);
7217 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7219 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7220 !wpa_s->p2p_in_provisioning)
7221 return 0; /* not P2P client operation */
7223 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7225 if (wpa_s != wpa_s->parent)
7226 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
7227 wpas_p2p_group_formation_failed(wpa_s, 0);
7232 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7234 struct wpa_supplicant *wpa_s = eloop_ctx;
7235 wpas_p2p_notif_pbc_overlap(wpa_s);
7239 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7240 enum wpas_p2p_channel_update_trig trig)
7242 struct p2p_channels chan, cli_chan;
7243 struct wpa_used_freq_data *freqs = NULL;
7244 unsigned int num = wpa_s->num_multichan_concurrent;
7246 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7249 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7253 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7255 os_memset(&chan, 0, sizeof(chan));
7256 os_memset(&cli_chan, 0, sizeof(cli_chan));
7257 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
7258 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7263 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
7265 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7268 * The used frequencies map changed, so it is possible that a GO is
7269 * using a channel that is no longer valid for P2P use. It is also
7270 * possible that due to policy consideration, it would be preferable to
7271 * move it to a frequency already used by other station interfaces.
7273 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7279 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7280 struct wpa_scan_results *scan_res)
7282 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7286 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7288 struct wpa_global *global = wpa_s->global;
7292 if (global->p2p == NULL)
7295 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7297 if (wpa_s->pending_interface_name[0] &&
7298 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7301 peer = p2p_get_go_neg_peer(global->p2p);
7303 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7304 MACSTR, MAC2STR(peer));
7305 p2p_unauthorize(global->p2p, peer);
7309 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7310 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7311 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7315 if (wpa_s->pending_pd_before_join) {
7316 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7317 wpa_s->pending_pd_before_join = 0;
7321 wpas_p2p_stop_find(wpa_s);
7323 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7324 if (wpa_s == global->p2p_group_formation &&
7325 (wpa_s->p2p_in_provisioning ||
7326 wpa_s->parent->pending_interface_type ==
7327 WPA_IF_P2P_CLIENT)) {
7328 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7329 "formation found - cancelling",
7332 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7333 wpa_s->parent, NULL);
7334 if (wpa_s->p2p_in_provisioning) {
7335 wpas_group_formation_completed(wpa_s, 0, 0);
7338 wpas_p2p_group_delete(wpa_s,
7339 P2P_GROUP_REMOVAL_REQUESTED);
7341 } else if (wpa_s->p2p_in_invitation) {
7342 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7345 wpas_p2p_group_formation_failed(wpa_s, 0);
7350 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7358 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7360 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7363 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7364 "being available anymore");
7365 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
7369 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7370 int freq_24, int freq_5, int freq_overall)
7372 struct p2p_data *p2p = wpa_s->global->p2p;
7375 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7379 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7382 struct p2p_data *p2p = wpa_s->global->p2p;
7387 if (hwaddr_aton(addr, peer))
7390 return p2p_unauthorize(p2p, peer);
7395 * wpas_p2p_disconnect - Disconnect from a P2P Group
7396 * @wpa_s: Pointer to wpa_supplicant data
7397 * Returns: 0 on success, -1 on failure
7399 * This can be used to disconnect from a group in which the local end is a P2P
7400 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7401 * virtual network interface was created for this group, that interface will be
7402 * removed. Otherwise, only the configured P2P group network will be removed
7403 * from the interface.
7405 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7411 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7416 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7420 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7423 ret = p2p_in_progress(wpa_s->global->p2p);
7426 * Check whether there is an ongoing WPS provisioning step (or
7427 * other parts of group formation) on another interface since
7428 * p2p_in_progress() does not report this to avoid issues for
7429 * scans during such provisioning step.
7431 if (wpa_s->global->p2p_group_formation &&
7432 wpa_s->global->p2p_group_formation != wpa_s) {
7433 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7434 "in group formation",
7435 wpa_s->global->p2p_group_formation->ifname);
7440 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
7441 struct os_reltime now;
7442 os_get_reltime(&now);
7443 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7444 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
7445 /* Wait for the first client has expired */
7446 wpa_s->global->p2p_go_wait_client.sec = 0;
7448 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7457 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7458 struct wpa_ssid *ssid)
7460 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7461 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7462 wpa_s->parent, NULL) > 0) {
7464 * Remove the network by scheduling the group formation
7465 * timeout to happen immediately. The teardown code
7466 * needs to be scheduled to run asynch later so that we
7467 * don't delete data from under ourselves unexpectedly.
7468 * Calling wpas_p2p_group_formation_timeout directly
7469 * causes a series of crashes in WPS failure scenarios.
7471 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7472 "P2P group network getting removed");
7473 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7474 wpa_s->parent, NULL);
7479 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
7480 const u8 *addr, const u8 *ssid,
7486 for (s = wpa_s->conf->ssid; s; s = s->next) {
7487 if (s->disabled != 2)
7490 (ssid_len != s->ssid_len ||
7491 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7494 if (s->mode == WPAS_MODE_P2P_GO)
7498 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7499 return s; /* peer is GO in the persistent group */
7500 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7502 for (i = 0; i < s->num_p2p_clients; i++) {
7503 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
7504 addr, ETH_ALEN) == 0)
7505 return s; /* peer is P2P client in persistent
7514 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7517 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7518 wpa_s->parent, NULL) > 0) {
7520 * This can happen if WPS provisioning step is not terminated
7521 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7522 * peer was able to connect, there is no need to time out group
7523 * formation after this, though. In addition, this is used with
7524 * the initial connection wait on the GO as a separate formation
7525 * timeout and as such, expected to be hit after the initial WPS
7526 * provisioning step.
7528 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7530 if (!wpa_s->p2p_go_group_formation_completed &&
7531 !wpa_s->group_formation_reported) {
7533 * GO has not yet notified group formation success since
7534 * the WPS step was not completed cleanly. Do that
7535 * notification now since the P2P Client was able to
7536 * connect and as such, must have received the
7537 * credential from the WPS step.
7539 if (wpa_s->global->p2p)
7540 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7541 wpas_group_formation_completed(wpa_s, 1, 0);
7544 if (!wpa_s->p2p_go_group_formation_completed) {
7545 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7546 wpa_s->p2p_go_group_formation_completed = 1;
7547 wpa_s->global->p2p_group_formation = NULL;
7548 wpa_s->p2p_in_provisioning = 0;
7549 wpa_s->p2p_in_invitation = 0;
7551 wpa_s->global->p2p_go_wait_client.sec = 0;
7554 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7558 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7561 struct wpa_supplicant *group = wpa_s;
7564 if (wpa_s->global->p2p_group_formation)
7565 group = wpa_s->global->p2p_group_formation;
7566 wpa_s = wpa_s->global->p2p_init_wpa_s;
7567 offchannel_send_action_done(wpa_s);
7569 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7570 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7571 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7572 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7573 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7574 wpa_s->p2p_persistent_id,
7575 wpa_s->p2p_pd_before_go_neg,
7582 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7586 if (!wpa_s->p2p_fallback_to_go_neg ||
7587 wpa_s->p2p_in_provisioning <= 5)
7590 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7591 return 0; /* peer operating as a GO */
7593 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7594 "fallback to GO Negotiation");
7595 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7596 "reason=GO-not-found");
7597 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7599 return res == 1 ? 2 : 1;
7603 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7605 struct wpa_supplicant *ifs;
7607 if (wpa_s->wpa_state > WPA_SCANNING) {
7608 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7609 "concurrent operation",
7610 wpa_s->conf->p2p_search_delay);
7611 return wpa_s->conf->p2p_search_delay;
7614 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7616 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7617 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7618 "delay due to concurrent operation on "
7620 wpa_s->conf->p2p_search_delay,
7622 return wpa_s->conf->p2p_search_delay;
7630 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7631 struct wpa_ssid *s, const u8 *addr,
7634 struct psk_list_entry *psk, *tmp;
7637 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7639 if ((iface_addr && !psk->p2p &&
7640 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7641 (!iface_addr && psk->p2p &&
7642 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7643 wpa_dbg(wpa_s, MSG_DEBUG,
7644 "P2P: Remove persistent group PSK list entry for "
7646 MAC2STR(psk->addr), psk->p2p);
7647 dl_list_del(&psk->list);
7657 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7658 const u8 *p2p_dev_addr,
7659 const u8 *psk, size_t psk_len)
7661 struct wpa_ssid *ssid = wpa_s->current_ssid;
7662 struct wpa_ssid *persistent;
7663 struct psk_list_entry *p, *last;
7665 if (psk_len != sizeof(p->psk))
7669 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7670 " p2p_dev_addr=" MACSTR,
7671 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7672 if (is_zero_ether_addr(p2p_dev_addr))
7673 p2p_dev_addr = NULL;
7675 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7679 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7680 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7681 /* To be added to persistent group once created */
7682 if (wpa_s->global->add_psk == NULL) {
7683 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7684 if (wpa_s->global->add_psk == NULL)
7687 p = wpa_s->global->add_psk;
7690 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7693 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7695 os_memcpy(p->psk, psk, psk_len);
7699 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7700 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7704 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7707 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7711 p = os_zalloc(sizeof(*p));
7716 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7719 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7721 os_memcpy(p->psk, psk, psk_len);
7723 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7724 (last = dl_list_last(&persistent->psk_list,
7725 struct psk_list_entry, list))) {
7726 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7727 MACSTR " (p2p=%u) to make room for a new one",
7728 MAC2STR(last->addr), last->p2p);
7729 dl_list_del(&last->list);
7733 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7734 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7735 p2p_dev_addr == NULL);
7737 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7738 MACSTR, MAC2STR(p2p_dev_addr));
7740 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7743 dl_list_add(&persistent->psk_list, &p->list);
7745 if (wpa_s->parent->conf->update_config &&
7746 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7747 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7751 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7752 struct wpa_ssid *s, const u8 *addr,
7757 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7758 if (res > 0 && wpa_s->conf->update_config &&
7759 wpa_config_write(wpa_s->confname, wpa_s->conf))
7760 wpa_dbg(wpa_s, MSG_DEBUG,
7761 "P2P: Failed to update configuration");
7765 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7766 const u8 *peer, int iface_addr)
7768 struct hostapd_data *hapd;
7769 struct hostapd_wpa_psk *psk, *prev, *rem;
7770 struct sta_info *sta;
7772 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7773 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7776 /* Remove per-station PSK entry */
7777 hapd = wpa_s->ap_iface->bss[0];
7779 psk = hapd->conf->ssid.wpa_psk;
7781 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7783 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7784 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7785 MACSTR " iface_addr=%d",
7786 MAC2STR(peer), iface_addr);
7788 prev->next = psk->next;
7790 hapd->conf->ssid.wpa_psk = psk->next;
7800 /* Disconnect from group */
7802 sta = ap_get_sta(hapd, peer);
7804 sta = ap_get_sta_p2p(hapd, peer);
7806 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7807 " (iface_addr=%d) from group",
7808 MAC2STR(peer), iface_addr);
7809 hostapd_drv_sta_deauth(hapd, sta->addr,
7810 WLAN_REASON_DEAUTH_LEAVING);
7811 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7816 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7820 struct wpa_supplicant *w;
7821 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
7823 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7825 /* Remove from any persistent group */
7826 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
7827 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7830 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7831 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
7834 /* Remove from any operating group */
7835 for (w = wpa_s->global->ifaces; w; w = w->next)
7836 wpas_p2p_remove_client_go(w, peer, iface_addr);
7840 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7842 struct wpa_supplicant *wpa_s = eloop_ctx;
7843 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7847 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7849 struct wpa_supplicant *wpa_s = eloop_ctx;
7851 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7852 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7856 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7857 struct wpa_ssid *ssid)
7859 struct wpa_supplicant *iface;
7861 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7862 if (!iface->current_ssid ||
7863 iface->current_ssid->frequency == freq ||
7864 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7865 !iface->current_ssid->p2p_group))
7868 /* Remove the connection with least priority */
7869 if (!wpas_is_p2p_prioritized(iface)) {
7870 /* STA connection has priority over existing
7871 * P2P connection, so remove the interface. */
7872 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7873 eloop_register_timeout(0, 0,
7874 wpas_p2p_group_freq_conflict,
7876 /* If connection in progress is P2P connection, do not
7877 * proceed for the connection. */
7883 /* P2P connection has priority, disable the STA network
7885 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7887 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7888 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7889 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7891 /* If P2P connection is in progress, continue
7904 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7906 struct wpa_ssid *ssid = wpa_s->current_ssid;
7908 if (ssid == NULL || !ssid->p2p_group)
7911 if (wpa_s->p2p_last_4way_hs_fail &&
7912 wpa_s->p2p_last_4way_hs_fail == ssid) {
7913 u8 go_dev_addr[ETH_ALEN];
7914 struct wpa_ssid *persistent;
7916 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7918 ssid->ssid_len) <= 0) {
7919 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7923 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7924 MACSTR, MAC2STR(go_dev_addr));
7925 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7928 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7929 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7932 wpa_msg_global(wpa_s->parent, MSG_INFO,
7933 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7936 wpa_s->p2p_last_4way_hs_fail = NULL;
7938 * Remove the group from a timeout to avoid issues with caller
7939 * continuing to use the interface if this is on a P2P group
7942 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7947 wpa_s->p2p_last_4way_hs_fail = ssid;
7952 #ifdef CONFIG_WPS_NFC
7954 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7962 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7966 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7967 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7974 wpabuf_put_be16(ret, wsc_len);
7976 wpabuf_put_buf(ret, wsc);
7977 wpabuf_put_be16(ret, wpabuf_len(p2p));
7978 wpabuf_put_buf(ret, p2p);
7982 wpa_hexdump_buf(MSG_DEBUG,
7983 "P2P: Generated NFC connection handover message", ret);
7987 tmp = ndef_build_p2p(ret);
7990 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8000 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8001 struct wpa_ssid **ssid, u8 *go_dev_addr)
8003 struct wpa_supplicant *iface;
8006 os_memset(go_dev_addr, 0, ETH_ALEN);
8009 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8010 if (iface->wpa_state < WPA_ASSOCIATING ||
8011 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8012 !iface->current_ssid->p2p_group ||
8013 iface->current_ssid->mode != WPAS_MODE_INFRA)
8016 *ssid = iface->current_ssid;
8018 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8019 return iface->assoc_freq;
8025 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8028 struct wpabuf *wsc, *p2p;
8029 struct wpa_ssid *ssid;
8030 u8 go_dev_addr[ETH_ALEN];
8031 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8033 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8034 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8038 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8039 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8040 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8041 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8045 if (cli_freq == 0) {
8046 wsc = wps_build_nfc_handover_req_p2p(
8047 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8050 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8051 go_dev_addr, ssid ? ssid->ssid : NULL,
8052 ssid ? ssid->ssid_len : 0);
8054 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8058 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8061 struct wpabuf *wsc, *p2p;
8062 struct wpa_ssid *ssid;
8063 u8 go_dev_addr[ETH_ALEN];
8064 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8066 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8069 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8070 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8071 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8074 if (cli_freq == 0) {
8075 wsc = wps_build_nfc_handover_sel_p2p(
8077 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8078 DEV_PW_NFC_CONNECTION_HANDOVER,
8079 wpa_s->conf->wps_nfc_dh_pubkey,
8080 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8083 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8084 go_dev_addr, ssid ? ssid->ssid : NULL,
8085 ssid ? ssid->ssid_len : 0);
8087 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8091 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8092 struct p2p_nfc_params *params)
8094 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8095 "connection handover (freq=%d)",
8098 if (params->go_freq && params->go_ssid_len) {
8099 wpa_s->p2p_wps_method = WPS_NFC;
8100 wpa_s->pending_join_wps_method = WPS_NFC;
8101 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8102 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8104 return wpas_p2p_join_start(wpa_s, params->go_freq,
8106 params->go_ssid_len);
8109 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8110 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
8111 params->go_freq, -1, 0, 1, 1);
8115 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8116 struct p2p_nfc_params *params, int tag)
8118 int res, persistent;
8119 struct wpa_ssid *ssid;
8121 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8122 "connection handover");
8123 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8124 ssid = wpa_s->current_ssid;
8127 if (ssid->mode != WPAS_MODE_P2P_GO)
8129 if (wpa_s->ap_iface == NULL)
8133 if (wpa_s == NULL) {
8134 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8138 if (wpa_s->parent->p2p_oob_dev_pw_id !=
8139 DEV_PW_NFC_CONNECTION_HANDOVER &&
8140 !wpa_s->parent->p2p_oob_dev_pw) {
8141 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8144 res = wpas_ap_wps_add_nfc_pw(
8145 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
8146 wpa_s->parent->p2p_oob_dev_pw,
8147 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
8148 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
8153 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8157 if (!params->peer ||
8158 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8161 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8162 " to join", MAC2STR(params->peer->p2p_device_addr));
8164 wpa_s->global->p2p_invite_group = wpa_s;
8165 persistent = ssid->p2p_persistent_group &&
8166 wpas_p2p_get_persistent(wpa_s->parent,
8167 params->peer->p2p_device_addr,
8168 ssid->ssid, ssid->ssid_len);
8169 wpa_s->parent->pending_invite_ssid_id = -1;
8171 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8172 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8173 ssid->ssid, ssid->ssid_len, ssid->frequency,
8174 wpa_s->global->p2p_dev_addr, persistent, 0,
8175 wpa_s->parent->p2p_oob_dev_pw_id);
8179 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8180 struct p2p_nfc_params *params,
8183 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8184 "connection handover");
8185 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8186 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8187 forced_freq, -1, 0, 1, 1);
8191 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8192 struct p2p_nfc_params *params,
8197 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8198 "connection handover");
8199 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8200 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8201 forced_freq, -1, 0, 1, 1);
8205 res = wpas_p2p_listen(wpa_s, 60);
8207 p2p_unauthorize(wpa_s->global->p2p,
8208 params->peer->p2p_device_addr);
8215 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8216 const struct wpabuf *data,
8217 int sel, int tag, int forced_freq)
8219 const u8 *pos, *end;
8221 struct p2p_nfc_params params;
8224 os_memset(¶ms, 0, sizeof(params));
8227 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8229 pos = wpabuf_head(data);
8230 end = pos + wpabuf_len(data);
8232 if (end - pos < 2) {
8233 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8237 len = WPA_GET_BE16(pos);
8239 if (len > end - pos) {
8240 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8244 params.wsc_attr = pos;
8245 params.wsc_len = len;
8248 if (end - pos < 2) {
8249 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8253 len = WPA_GET_BE16(pos);
8255 if (len > end - pos) {
8256 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8260 params.p2p_attr = pos;
8261 params.p2p_len = len;
8264 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8265 params.wsc_attr, params.wsc_len);
8266 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8267 params.p2p_attr, params.p2p_len);
8269 wpa_hexdump(MSG_DEBUG,
8270 "P2P: Ignored extra data after P2P attributes",
8274 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
8278 if (params.next_step == NO_ACTION)
8281 if (params.next_step == BOTH_GO) {
8282 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8283 MAC2STR(params.peer->p2p_device_addr));
8287 if (params.next_step == PEER_CLIENT) {
8288 if (!is_zero_ether_addr(params.go_dev_addr)) {
8289 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8290 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8292 MAC2STR(params.peer->p2p_device_addr),
8294 MAC2STR(params.go_dev_addr),
8295 wpa_ssid_txt(params.go_ssid,
8296 params.go_ssid_len));
8298 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8299 "peer=" MACSTR " freq=%d",
8300 MAC2STR(params.peer->p2p_device_addr),
8306 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8307 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8308 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8312 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8313 wpa_s->p2p_oob_dev_pw = NULL;
8315 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8316 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8321 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8322 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8323 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8324 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8325 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8326 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8327 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8331 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8332 "peer OOB Device Password Id %u", id);
8335 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8336 "Device Password Id %u", id);
8337 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8338 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8339 params.oob_dev_pw_len -
8340 WPS_OOB_PUBKEY_HASH_LEN - 2);
8341 wpa_s->p2p_oob_dev_pw_id = id;
8342 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8343 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8344 params.oob_dev_pw_len -
8345 WPS_OOB_PUBKEY_HASH_LEN - 2);
8346 if (wpa_s->p2p_oob_dev_pw == NULL)
8349 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8350 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8351 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8354 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8355 "without Device Password");
8356 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8359 switch (params.next_step) {
8363 /* already covered above */
8366 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
8368 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
8370 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
8372 /* TODO: use own OOB Dev Pw */
8373 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
8380 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8381 const struct wpabuf *data, int forced_freq)
8383 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8386 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8390 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8391 const struct wpabuf *req,
8392 const struct wpabuf *sel, int forced_freq)
8397 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8400 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8402 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8403 wpabuf_head(req), wpabuf_len(req));
8404 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8405 wpabuf_head(sel), wpabuf_len(sel));
8407 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8408 tmp = ndef_parse_p2p(init ? sel : req);
8410 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8414 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8422 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8425 int go_intent = wpa_s->conf->p2p_go_intent;
8426 struct wpa_supplicant *iface;
8428 if (wpa_s->global->p2p == NULL)
8432 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8433 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8435 if (!iface->ap_iface)
8437 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8439 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8441 if (wpa_s->p2p_nfc_tag_enabled)
8442 wpas_p2p_remove_pending_group_interface(wpa_s);
8443 wpa_s->p2p_nfc_tag_enabled = 0;
8447 if (wpa_s->global->p2p_disabled)
8450 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8451 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8452 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8453 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8454 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8455 "to allow static handover cases");
8459 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8461 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8462 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8463 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8464 if (wpa_s->p2p_oob_dev_pw == NULL)
8466 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8468 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8469 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8471 * P2P Group Interface present and the command came on group
8472 * interface, so enable the token for the current interface.
8474 wpa_s->create_p2p_iface = 0;
8476 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8479 if (wpa_s->create_p2p_iface) {
8480 enum wpa_driver_if_type iftype;
8481 /* Prepare to add a new interface for the group */
8482 iftype = WPA_IF_P2P_GROUP;
8483 if (go_intent == 15)
8484 iftype = WPA_IF_P2P_GO;
8485 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8486 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8487 "interface for the group");
8491 if_addr = wpa_s->pending_interface_addr;
8493 if_addr = wpa_s->own_addr;
8495 wpa_s->p2p_nfc_tag_enabled = enabled;
8497 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8498 struct hostapd_data *hapd;
8499 if (iface->ap_iface == NULL)
8501 hapd = iface->ap_iface->bss[0];
8502 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8503 hapd->conf->wps_nfc_dh_pubkey =
8504 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8505 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8506 hapd->conf->wps_nfc_dh_privkey =
8507 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8508 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8509 hapd->conf->wps_nfc_dev_pw =
8510 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8511 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8513 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8514 wpa_dbg(iface, MSG_DEBUG,
8515 "P2P: Failed to enable NFC Tag for GO");
8518 p2p_set_authorized_oob_dev_pw_id(
8519 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8525 #endif /* CONFIG_WPS_NFC */
8528 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8529 struct wpa_used_freq_data *freqs,
8532 u8 curr_chan, cand, chan;
8536 * If possible, optimize the Listen channel to be a channel that is
8537 * already used by one of the other interfaces.
8539 if (!wpa_s->conf->p2p_optimize_listen_chan)
8542 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8545 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8546 for (i = 0, cand = 0; i < num; i++) {
8547 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8548 if (curr_chan == chan) {
8553 if (chan == 1 || chan == 6 || chan == 11)
8558 wpa_dbg(wpa_s, MSG_DEBUG,
8559 "P2P: Update Listen channel to %u based on operating channel",
8561 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8566 static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
8568 struct hostapd_config *conf;
8569 struct p2p_go_neg_results params;
8570 struct csa_settings csa_settings;
8571 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8572 int old_freq = current_ssid->frequency;
8575 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8576 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8581 * TODO: This function may not always work correctly. For example,
8582 * when we have a running GO and a BSS on a DFS channel.
8584 if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, NULL)) {
8585 wpa_dbg(wpa_s, MSG_DEBUG,
8586 "P2P CSA: Failed to select new frequency for GO");
8590 if (current_ssid->frequency == params.freq) {
8591 wpa_dbg(wpa_s, MSG_DEBUG,
8592 "P2P CSA: Selected same frequency - not moving GO");
8596 conf = hostapd_config_defaults();
8598 wpa_dbg(wpa_s, MSG_DEBUG,
8599 "P2P CSA: Failed to allocate default config");
8603 current_ssid->frequency = params.freq;
8604 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
8605 wpa_dbg(wpa_s, MSG_DEBUG,
8606 "P2P CSA: Failed to create new GO config");
8611 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
8612 wpa_dbg(wpa_s, MSG_DEBUG,
8613 "P2P CSA: CSA to a different band is not supported");
8618 os_memset(&csa_settings, 0, sizeof(csa_settings));
8619 csa_settings.cs_count = P2P_GO_CSA_COUNT;
8620 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
8621 csa_settings.freq_params.freq = params.freq;
8622 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
8623 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
8624 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
8626 if (conf->ieee80211ac) {
8627 int freq1 = 0, freq2 = 0;
8630 if (ieee80211_freq_to_channel_ext(params.freq,
8631 conf->secondary_channel,
8632 conf->vht_oper_chwidth,
8634 NUM_HOSTAPD_MODES) {
8635 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
8640 if (conf->vht_oper_centr_freq_seg0_idx)
8641 freq1 = ieee80211_chan_to_freq(
8643 conf->vht_oper_centr_freq_seg0_idx);
8645 if (conf->vht_oper_centr_freq_seg1_idx)
8646 freq2 = ieee80211_chan_to_freq(
8648 conf->vht_oper_centr_freq_seg1_idx);
8650 if (freq1 < 0 || freq2 < 0) {
8651 wpa_dbg(wpa_s, MSG_DEBUG,
8652 "P2P CSA: Selected invalid VHT center freqs");
8657 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
8658 csa_settings.freq_params.center_freq1 = freq1;
8659 csa_settings.freq_params.center_freq2 = freq2;
8661 switch (conf->vht_oper_chwidth) {
8662 case VHT_CHANWIDTH_80MHZ:
8663 case VHT_CHANWIDTH_80P80MHZ:
8664 csa_settings.freq_params.bandwidth = 80;
8666 case VHT_CHANWIDTH_160MHZ:
8667 csa_settings.freq_params.bandwidth = 160;
8672 ret = ap_switch_channel(wpa_s, &csa_settings);
8674 current_ssid->frequency = old_freq;
8675 hostapd_config_free(conf);
8680 static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
8682 struct p2p_go_neg_results params;
8683 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8685 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
8687 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
8688 current_ssid->frequency);
8690 /* Stop the AP functionality */
8691 /* TODO: Should do this in a way that does not indicated to possible
8692 * P2P Clients in the group that the group is terminated. */
8693 wpa_supplicant_ap_deinit(wpa_s);
8695 /* Reselect the GO frequency */
8696 if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, NULL)) {
8697 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
8698 wpas_p2p_group_delete(wpa_s,
8699 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8702 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
8706 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
8707 wpa_printf(MSG_DEBUG,
8708 "P2P: Selected freq (%u MHz) is not valid for P2P",
8710 wpas_p2p_group_delete(wpa_s,
8711 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8715 /* Update the frequency */
8716 current_ssid->frequency = params.freq;
8717 wpa_s->connect_without_scan = current_ssid;
8718 wpa_s->reassociate = 1;
8719 wpa_s->disconnected = 0;
8720 wpa_supplicant_req_scan(wpa_s, 0, 0);
8724 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
8726 struct wpa_supplicant *wpa_s = eloop_ctx;
8728 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
8731 wpas_p2p_go_update_common_freqs(wpa_s);
8733 /* Do not move GO in the middle of a CSA */
8734 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
8735 wpa_printf(MSG_DEBUG,
8736 "P2P: CSA is in progress - not moving GO");
8741 * First, try a channel switch flow. If it is not supported or fails,
8742 * take down the GO and bring it up again.
8744 if (wpas_p2p_move_go_csa(wpa_s) < 0)
8745 wpas_p2p_move_go_no_csa(wpa_s);
8749 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
8751 struct wpa_supplicant *wpa_s = eloop_ctx;
8752 struct wpa_used_freq_data *freqs = NULL;
8753 unsigned int num = wpa_s->num_multichan_concurrent;
8755 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8759 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8761 /* Previous attempt to move a GO was not possible -- try again. */
8762 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
8763 WPAS_P2P_CHANNEL_UPDATE_ANY);
8770 * Consider moving a GO from its currently used frequency:
8771 * 1. It is possible that due to regulatory consideration the frequency
8772 * can no longer be used and there is a need to evacuate the GO.
8773 * 2. It is possible that due to MCC considerations, it would be preferable
8774 * to move the GO to a channel that is currently used by some other
8775 * station interface.
8777 * In case a frequency that became invalid is once again valid, cancel a
8778 * previously initiated GO frequency change.
8780 static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
8781 struct wpa_used_freq_data *freqs,
8784 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
8785 unsigned int timeout;
8788 wpas_p2p_go_update_common_freqs(wpa_s);
8790 freq = wpa_s->current_ssid->frequency;
8791 for (i = 0, invalid_freq = 0; i < num; i++) {
8792 if (freqs[i].freq == freq) {
8793 flags = freqs[i].flags;
8795 /* The channel is invalid, must change it */
8796 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
8797 wpa_dbg(wpa_s, MSG_DEBUG,
8798 "P2P: Freq=%d MHz no longer valid for GO",
8802 } else if (freqs[i].flags == 0) {
8803 /* Freq is not used by any other station interface */
8805 } else if (!p2p_supported_freq(wpa_s->global->p2p,
8807 /* Freq is not valid for P2P use cases */
8809 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8810 P2P_GO_FREQ_MOVE_SCM) {
8812 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8813 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
8814 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
8816 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
8817 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
8818 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
8819 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
8821 } else if ((wpa_s->drv_flags &
8822 WPA_DRIVER_FLAGS_AP_CSA) &&
8823 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
8827 * We do not support CSA between bands, so move
8828 * GO only within the same band.
8830 if (wpa_s->ap_iface->current_mode->mode ==
8831 ieee80211_freq_to_chan(freqs[i].freq,
8838 wpa_dbg(wpa_s, MSG_DEBUG,
8839 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
8840 invalid_freq, policy_move, flags);
8843 * The channel is valid, or we are going to have a policy move, so
8846 if (!invalid_freq || policy_move) {
8847 wpa_dbg(wpa_s, MSG_DEBUG,
8848 "P2P: Cancel a GO move from freq=%d MHz", freq);
8849 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8851 if (wpas_p2p_in_progress(wpa_s)) {
8852 wpa_dbg(wpa_s, MSG_DEBUG,
8853 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
8854 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
8856 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
8857 wpas_p2p_reconsider_moving_go,
8863 if (!invalid_freq && (!policy_move || flags != 0)) {
8864 wpa_dbg(wpa_s, MSG_DEBUG,
8865 "P2P: Not initiating a GO frequency change");
8870 * Do not consider moving GO if it is in the middle of a CSA. When the
8871 * CSA is finished this flow should be retriggered.
8873 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
8874 wpa_dbg(wpa_s, MSG_DEBUG,
8875 "P2P: Not initiating a GO frequency change - CSA is in progress");
8879 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
8880 timeout = P2P_GO_FREQ_CHANGE_TIME;
8884 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
8886 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8887 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
8891 static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
8892 struct wpa_used_freq_data *freqs,
8894 enum wpas_p2p_channel_update_trig trig)
8896 struct wpa_supplicant *ifs;
8898 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
8902 * Travers all the radio interfaces, and for each GO interface, check
8903 * if there is a need to move the GO from the frequency it is using,
8904 * or in case the frequency is valid again, cancel the evacuation flow.
8906 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8908 if (ifs->current_ssid == NULL ||
8909 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
8913 * The GO was just started or completed channel switch, no need
8917 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
8918 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
8919 wpa_dbg(wpa_s, MSG_DEBUG,
8920 "P2P: GO move - schedule re-consideration");
8921 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
8922 wpas_p2p_reconsider_moving_go,
8927 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
8932 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8934 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8937 wpas_p2p_update_channel_list(wpa_s,
8938 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
8942 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8944 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8945 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8946 "the management interface is being removed");
8947 wpas_p2p_deinit_global(wpa_s->global);
8952 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8954 if (wpa_s->ap_iface->bss)
8955 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8956 wpas_p2p_group_deinit(wpa_s);