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)
3817 struct wpa_supplicant *wpa_s = ctx;
3819 struct wpa_ssid *persistent_go, *stale, *s;
3820 int save_config = 0;
3821 struct wpa_supplicant *go_wpa_s;
3822 char feat_cap_str[256];
3827 os_memset(mac, 0, ETH_ALEN);
3835 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3836 feat_cap, feat_cap_len);
3839 if (session_info == NULL) {
3840 wpa_msg_global(wpa_s, MSG_INFO,
3841 P2P_EVENT_P2PS_PROVISION_START MACSTR
3842 " adv_id=%x conncap=%x"
3844 " session=%x mac=" MACSTR
3845 " dev_passwd_id=%d%s",
3846 MAC2STR(dev), adv_id, conncap,
3848 ses_id, MAC2STR(ses_mac),
3849 passwd_id, feat_cap_str);
3851 wpa_msg_global(wpa_s, MSG_INFO,
3852 P2P_EVENT_P2PS_PROVISION_START MACSTR
3853 " adv_id=%x conncap=%x"
3855 " session=%x mac=" MACSTR
3856 " dev_passwd_id=%d info='%s'%s",
3857 MAC2STR(dev), adv_id, conncap,
3859 ses_id, MAC2STR(ses_mac),
3860 passwd_id, session_info, feat_cap_str);
3865 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3866 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3868 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3869 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3870 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3872 if (persistent_go && !persistent_go->num_p2p_clients) {
3873 /* remove empty persistent GO */
3874 wpa_config_remove_network(wpa_s->conf,
3878 wpa_msg_global(wpa_s, MSG_INFO,
3879 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3881 " adv_id=%x adv_mac=" MACSTR
3882 " session=%x mac=" MACSTR "%s",
3883 MAC2STR(dev), status,
3884 adv_id, MAC2STR(adv_mac),
3885 ses_id, MAC2STR(ses_mac), feat_cap_str);
3889 /* Clean up stale persistent groups with this device */
3890 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3893 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
3894 is_zero_ether_addr(grp_mac)) {
3895 wpa_dbg(wpa_s, MSG_ERROR,
3896 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
3901 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3905 if (s && s->ssid_len == stale->ssid_len &&
3906 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3907 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3910 /* Remove stale persistent group */
3911 if (stale->mode != WPAS_MODE_P2P_GO ||
3912 stale->num_p2p_clients <= 1) {
3913 wpa_config_remove_network(wpa_s->conf, stale->id);
3917 for (i = 0; i < stale->num_p2p_clients; i++) {
3918 if (os_memcmp(stale->p2p_client_list +
3920 dev, ETH_ALEN) == 0) {
3921 os_memmove(stale->p2p_client_list +
3923 stale->p2p_client_list +
3925 (stale->num_p2p_clients -
3930 stale->num_p2p_clients--;
3936 p2p_config_write(wpa_s);
3939 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3940 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3942 if (persistent_go && s != persistent_go &&
3943 !persistent_go->num_p2p_clients) {
3944 /* remove empty persistent GO */
3945 wpa_config_remove_network(wpa_s->conf,
3950 wpa_msg_global(wpa_s, MSG_INFO,
3951 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3953 " adv_id=%x adv_mac=" MACSTR
3954 " session=%x mac=" MACSTR
3956 MAC2STR(dev), status,
3957 adv_id, MAC2STR(adv_mac),
3958 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
3962 if (conncap == P2PS_SETUP_GROUP_OWNER) {
3964 * We need to copy the interface name. Simply saving a
3965 * pointer isn't enough, since if we use pending_interface_name
3966 * it will be overwritten when the group is added.
3968 char go_ifname[100];
3970 go_ifname[0] = '\0';
3972 wpa_s->global->pending_p2ps_group = 1;
3974 if (!wpas_p2p_create_iface(wpa_s))
3975 os_memcpy(go_ifname, wpa_s->ifname,
3977 else if (wpa_s->pending_interface_name[0])
3978 os_memcpy(go_ifname,
3979 wpa_s->pending_interface_name,
3982 if (!go_ifname[0]) {
3983 wpas_p2ps_prov_complete(
3984 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3985 dev, adv_mac, ses_mac,
3986 grp_mac, adv_id, ses_id, 0, 0,
3987 NULL, 0, 0, 0, NULL, NULL, 0);
3991 /* If PD Resp complete, start up the GO */
3992 if (response_done && persistent_go) {
3993 wpas_p2p_group_add_persistent(
3994 wpa_s, persistent_go,
3995 0, 0, 0, 0, 0, NULL,
3996 persistent_go->mode ==
3998 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4000 } else if (response_done) {
4001 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
4004 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4005 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4007 wpa_s->p2ps_method_config_any = 1;
4009 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4010 os_memcpy(go_ifname, go_wpa_s->ifname,
4013 if (is_zero_ether_addr(grp_mac)) {
4014 wpa_dbg(go_wpa_s, MSG_DEBUG,
4015 "P2P: Setting PIN-1 for ANY");
4016 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4017 "12345670", NULL, 0,
4020 wpa_dbg(go_wpa_s, MSG_DEBUG,
4021 "P2P: Setting PIN-1 for " MACSTR,
4023 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4024 "12345670", NULL, 0,
4028 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4029 wpa_s->p2ps_method_config_any = 1;
4032 wpa_msg_global(wpa_s, MSG_INFO,
4033 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4034 " status=%d conncap=%x"
4035 " adv_id=%x adv_mac=" MACSTR
4036 " session=%x mac=" MACSTR
4037 " dev_passwd_id=%d go=%s%s",
4038 MAC2STR(dev), status, conncap,
4039 adv_id, MAC2STR(adv_mac),
4040 ses_id, MAC2STR(ses_mac),
4041 passwd_id, go_ifname, feat_cap_str);
4045 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4046 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4048 if (persistent_go && !persistent_go->num_p2p_clients) {
4049 /* remove empty persistent GO */
4050 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4053 if (conncap == P2PS_SETUP_CLIENT) {
4054 wpa_msg_global(wpa_s, MSG_INFO,
4055 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4056 " status=%d conncap=%x"
4057 " adv_id=%x adv_mac=" MACSTR
4058 " session=%x mac=" MACSTR
4059 " dev_passwd_id=%d join=" MACSTR "%s",
4060 MAC2STR(dev), status, conncap,
4061 adv_id, MAC2STR(adv_mac),
4062 ses_id, MAC2STR(ses_mac),
4063 passwd_id, MAC2STR(grp_mac), feat_cap_str);
4065 wpa_msg_global(wpa_s, MSG_INFO,
4066 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4067 " status=%d conncap=%x"
4068 " adv_id=%x adv_mac=" MACSTR
4069 " session=%x mac=" MACSTR
4070 " dev_passwd_id=%d%s",
4071 MAC2STR(dev), status, conncap,
4072 adv_id, MAC2STR(adv_mac),
4073 ses_id, MAC2STR(ses_mac),
4074 passwd_id, feat_cap_str);
4079 static int _wpas_p2p_in_progress(void *ctx)
4081 struct wpa_supplicant *wpa_s = ctx;
4082 return wpas_p2p_in_progress(wpa_s);
4086 static int wpas_prov_disc_resp_cb(void *ctx)
4088 struct wpa_supplicant *wpa_s = ctx;
4089 struct wpa_ssid *persistent_go;
4091 if (!wpa_s->global->pending_p2ps_group)
4094 wpa_s->global->pending_p2ps_group = 0;
4096 if (wpas_p2p_get_go_group(wpa_s))
4098 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4100 if (persistent_go) {
4101 wpas_p2p_group_add_persistent(
4102 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
4103 persistent_go->mode == WPAS_MODE_P2P_GO ?
4104 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
4106 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
4113 static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4115 unsigned int *freq_list)
4117 struct wpa_supplicant *wpa_s = ctx;
4119 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4120 WPA_IF_P2P_CLIENT, len, freq_list);
4125 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4126 * @global: Pointer to global data from wpa_supplicant_init()
4127 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4128 * Returns: 0 on success, -1 on failure
4130 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4132 struct p2p_config p2p;
4135 if (wpa_s->conf->p2p_disabled)
4138 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4144 os_memset(&p2p, 0, sizeof(p2p));
4146 p2p.debug_print = wpas_p2p_debug_print;
4147 p2p.p2p_scan = wpas_p2p_scan;
4148 p2p.send_action = wpas_send_action;
4149 p2p.send_action_done = wpas_send_action_done;
4150 p2p.go_neg_completed = wpas_go_neg_completed;
4151 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4152 p2p.dev_found = wpas_dev_found;
4153 p2p.dev_lost = wpas_dev_lost;
4154 p2p.find_stopped = wpas_find_stopped;
4155 p2p.start_listen = wpas_start_listen;
4156 p2p.stop_listen = wpas_stop_listen;
4157 p2p.send_probe_resp = wpas_send_probe_resp;
4158 p2p.sd_request = wpas_sd_request;
4159 p2p.sd_response = wpas_sd_response;
4160 p2p.prov_disc_req = wpas_prov_disc_req;
4161 p2p.prov_disc_resp = wpas_prov_disc_resp;
4162 p2p.prov_disc_fail = wpas_prov_disc_fail;
4163 p2p.invitation_process = wpas_invitation_process;
4164 p2p.invitation_received = wpas_invitation_received;
4165 p2p.invitation_result = wpas_invitation_result;
4166 p2p.get_noa = wpas_get_noa;
4167 p2p.go_connected = wpas_go_connected;
4168 p2p.presence_resp = wpas_presence_resp;
4169 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4170 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4171 p2p.get_persistent_group = wpas_get_persistent_group;
4172 p2p.get_go_info = wpas_get_go_info;
4173 p2p.remove_stale_groups = wpas_remove_stale_groups;
4174 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4175 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4176 p2p.p2ps_group_capability = p2ps_group_capability;
4177 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
4179 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4180 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4181 p2p.dev_name = wpa_s->conf->device_name;
4182 p2p.manufacturer = wpa_s->conf->manufacturer;
4183 p2p.model_name = wpa_s->conf->model_name;
4184 p2p.model_number = wpa_s->conf->model_number;
4185 p2p.serial_number = wpa_s->conf->serial_number;
4187 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4188 p2p.config_methods = wpa_s->wps->config_methods;
4191 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4192 wpa_printf(MSG_ERROR,
4193 "P2P: Failed to configure supported channel list");
4197 if (wpa_s->conf->p2p_listen_reg_class &&
4198 wpa_s->conf->p2p_listen_channel) {
4199 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4200 p2p.channel = wpa_s->conf->p2p_listen_channel;
4201 p2p.channel_forced = 1;
4204 * Pick one of the social channels randomly as the listen
4207 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4208 &p2p.channel) != 0) {
4209 wpa_printf(MSG_INFO,
4210 "P2P: No social channels supported by the driver - do not enable P2P");
4213 p2p.channel_forced = 0;
4215 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4216 p2p.reg_class, p2p.channel);
4218 if (wpa_s->conf->p2p_oper_reg_class &&
4219 wpa_s->conf->p2p_oper_channel) {
4220 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4221 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4222 p2p.cfg_op_channel = 1;
4223 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4224 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4228 * Use random operation channel from 2.4 GHz band social
4229 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4230 * other preference is indicated.
4232 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4233 &p2p.op_channel) != 0) {
4234 wpa_printf(MSG_ERROR,
4235 "P2P: Failed to select random social channel as operation channel");
4238 p2p.cfg_op_channel = 0;
4239 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4240 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4243 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4244 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4245 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4248 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4249 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4250 p2p.country[2] = 0x04;
4252 os_memcpy(p2p.country, "XX\x04", 3);
4254 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4257 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4258 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4259 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4261 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4262 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4264 p2p.max_peers = 100;
4266 if (wpa_s->conf->p2p_ssid_postfix) {
4267 p2p.ssid_postfix_len =
4268 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4269 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4270 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4271 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4272 p2p.ssid_postfix_len);
4275 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4277 p2p.max_listen = wpa_s->max_remain_on_chan;
4279 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4280 wpa_s->conf->p2p_passphrase_len <= 63)
4281 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4283 p2p.passphrase_len = 8;
4285 global->p2p = p2p_init(&p2p);
4286 if (global->p2p == NULL)
4288 global->p2p_init_wpa_s = wpa_s;
4290 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4291 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4293 p2p_add_wps_vendor_extension(
4294 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4297 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4304 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4305 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4307 * This function deinitialize per-interface P2P data.
4309 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4311 if (wpa_s->driver && wpa_s->drv_priv)
4312 wpa_drv_probe_req_report(wpa_s, 0);
4314 if (wpa_s->go_params) {
4315 /* Clear any stored provisioning info */
4316 p2p_clear_provisioning_info(
4318 wpa_s->go_params->peer_device_addr);
4321 os_free(wpa_s->go_params);
4322 wpa_s->go_params = NULL;
4323 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4324 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4325 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4326 wpa_s->p2p_long_listen = 0;
4327 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4328 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4329 wpas_p2p_remove_pending_group_interface(wpa_s);
4330 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4331 wpas_p2p_listen_work_done(wpa_s);
4332 if (wpa_s->p2p_send_action_work) {
4333 os_free(wpa_s->p2p_send_action_work->ctx);
4334 radio_work_done(wpa_s->p2p_send_action_work);
4335 wpa_s->p2p_send_action_work = NULL;
4337 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4339 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4340 wpa_s->p2p_oob_dev_pw = NULL;
4342 os_free(wpa_s->p2p_group_common_freqs);
4343 wpa_s->p2p_group_common_freqs = NULL;
4344 wpa_s->p2p_group_common_freqs_num = 0;
4346 /* TODO: remove group interface from the driver if this wpa_s instance
4347 * is on top of a P2P group interface */
4352 * wpas_p2p_deinit_global - Deinitialize global P2P module
4353 * @global: Pointer to global data from wpa_supplicant_init()
4355 * This function deinitializes the global (per device) P2P module.
4357 static void wpas_p2p_deinit_global(struct wpa_global *global)
4359 struct wpa_supplicant *wpa_s, *tmp;
4361 wpa_s = global->ifaces;
4363 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4365 /* Remove remaining P2P group interfaces */
4366 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4367 wpa_s = wpa_s->next;
4369 tmp = global->ifaces;
4372 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4377 /* Disconnect from the P2P group and deinit the interface */
4378 wpas_p2p_disconnect(tmp);
4382 * Deinit GO data on any possibly remaining interface (if main
4383 * interface is used as GO).
4385 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4386 if (wpa_s->ap_iface)
4387 wpas_p2p_group_deinit(wpa_s);
4390 p2p_deinit(global->p2p);
4392 global->p2p_init_wpa_s = NULL;
4396 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4398 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4399 wpa_s->conf->p2p_no_group_iface)
4400 return 0; /* separate interface disabled per configuration */
4401 if (wpa_s->drv_flags &
4402 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4403 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4404 return 1; /* P2P group requires a new interface in every case
4406 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4407 return 0; /* driver does not support concurrent operations */
4408 if (wpa_s->global->ifaces->next)
4409 return 1; /* more that one interface already in use */
4410 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4411 return 1; /* this interface is already in use */
4416 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4417 const u8 *peer_addr,
4418 enum p2p_wps_method wps_method,
4419 int go_intent, const u8 *own_interface_addr,
4420 unsigned int force_freq, int persistent_group,
4421 struct wpa_ssid *ssid, unsigned int pref_freq)
4423 if (persistent_group && wpa_s->conf->persistent_reconnect)
4424 persistent_group = 2;
4427 * Increase GO config timeout if HT40 is used since it takes some time
4428 * to scan channels for coex purposes before the BSS can be started.
4430 p2p_set_config_timeout(wpa_s->global->p2p,
4431 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4433 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4434 go_intent, own_interface_addr, force_freq,
4435 persistent_group, ssid ? ssid->ssid : NULL,
4436 ssid ? ssid->ssid_len : 0,
4437 wpa_s->p2p_pd_before_go_neg, pref_freq,
4438 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4443 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4444 const u8 *peer_addr,
4445 enum p2p_wps_method wps_method,
4446 int go_intent, const u8 *own_interface_addr,
4447 unsigned int force_freq, int persistent_group,
4448 struct wpa_ssid *ssid, unsigned int pref_freq)
4450 if (persistent_group && wpa_s->conf->persistent_reconnect)
4451 persistent_group = 2;
4453 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4454 go_intent, own_interface_addr, force_freq,
4455 persistent_group, ssid ? ssid->ssid : NULL,
4456 ssid ? ssid->ssid_len : 0, pref_freq,
4457 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4462 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4464 wpa_s->p2p_join_scan_count++;
4465 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4466 wpa_s->p2p_join_scan_count);
4467 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4468 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4469 " for join operationg - stop join attempt",
4470 MAC2STR(wpa_s->pending_join_iface_addr));
4471 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4472 if (wpa_s->p2p_auto_pd) {
4473 wpa_s->p2p_auto_pd = 0;
4474 wpa_msg_global(wpa_s, MSG_INFO,
4475 P2P_EVENT_PROV_DISC_FAILURE
4476 " p2p_dev_addr=" MACSTR " status=N/A",
4477 MAC2STR(wpa_s->pending_join_dev_addr));
4480 wpa_msg_global(wpa_s->parent, MSG_INFO,
4481 P2P_EVENT_GROUP_FORMATION_FAILURE);
4482 wpas_notify_p2p_group_formation_failure(wpa_s, "");
4487 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4490 unsigned int num, i;
4491 struct wpa_used_freq_data *freqs;
4493 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4494 /* Multiple channels are supported and not all are in use */
4498 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4499 sizeof(struct wpa_used_freq_data));
4503 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4504 wpa_s->num_multichan_concurrent);
4506 for (i = 0; i < num; i++) {
4507 if (freqs[i].freq == freq) {
4508 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4515 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4524 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4525 const u8 *peer_dev_addr)
4527 struct wpa_bss *bss;
4530 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4533 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4534 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4539 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4541 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4542 "%ld.%06ld (%supdated in last scan)",
4543 bss->last_update.sec, bss->last_update.usec,
4544 updated ? "": "not ");
4550 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4551 struct wpa_scan_results *scan_res)
4553 struct wpa_bss *bss = NULL;
4555 u8 iface_addr[ETH_ALEN];
4557 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4559 if (wpa_s->global->p2p_disabled)
4562 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4563 scan_res ? (int) scan_res->num : -1,
4564 wpa_s->p2p_auto_join ? "auto_" : "");
4567 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4569 if (wpa_s->p2p_auto_pd) {
4570 int join = wpas_p2p_peer_go(wpa_s,
4571 wpa_s->pending_join_dev_addr);
4573 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4574 wpa_s->auto_pd_scan_retry++;
4575 bss = wpa_bss_get_bssid_latest(
4576 wpa_s, wpa_s->pending_join_dev_addr);
4579 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4580 "the peer " MACSTR " at %d MHz",
4581 wpa_s->auto_pd_scan_retry,
4583 pending_join_dev_addr),
4585 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4593 wpa_s->p2p_auto_pd = 0;
4594 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4595 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4596 MAC2STR(wpa_s->pending_join_dev_addr), join);
4597 if (p2p_prov_disc_req(wpa_s->global->p2p,
4598 wpa_s->pending_join_dev_addr, NULL,
4599 wpa_s->pending_pd_config_methods, join,
4600 0, wpa_s->user_initiated_pd) < 0) {
4601 wpa_s->p2p_auto_pd = 0;
4602 wpa_msg_global(wpa_s, MSG_INFO,
4603 P2P_EVENT_PROV_DISC_FAILURE
4604 " p2p_dev_addr=" MACSTR " status=N/A",
4605 MAC2STR(wpa_s->pending_join_dev_addr));
4610 if (wpa_s->p2p_auto_join) {
4611 int join = wpas_p2p_peer_go(wpa_s,
4612 wpa_s->pending_join_dev_addr);
4614 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4615 "running a GO -> use GO Negotiation");
4616 wpa_msg_global(wpa_s->parent, MSG_INFO,
4617 P2P_EVENT_FALLBACK_TO_GO_NEG
4618 "reason=peer-not-running-GO");
4619 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4620 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4621 wpa_s->p2p_persistent_group, 0, 0, 0,
4622 wpa_s->p2p_go_intent,
4623 wpa_s->p2p_connect_freq,
4624 wpa_s->p2p_persistent_id,
4625 wpa_s->p2p_pd_before_go_neg,
4631 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4632 "try to join the group", join ? "" :
4635 wpa_msg_global(wpa_s->parent, MSG_INFO,
4636 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
4637 wpa_s->p2p_fallback_to_go_neg = 1;
4641 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4642 wpa_s->pending_join_iface_addr);
4644 p2p_get_interface_addr(wpa_s->global->p2p,
4645 wpa_s->pending_join_dev_addr,
4647 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4648 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4649 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4650 "address for join from " MACSTR " to " MACSTR
4651 " based on newly discovered P2P peer entry",
4652 MAC2STR(wpa_s->pending_join_iface_addr),
4653 MAC2STR(iface_addr));
4654 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4657 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4658 wpa_s->pending_join_iface_addr);
4661 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4662 "from P2P peer table: %d MHz", freq);
4664 if (wpa_s->p2p_join_ssid_len) {
4665 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4666 MACSTR " and SSID %s",
4667 MAC2STR(wpa_s->pending_join_iface_addr),
4668 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4669 wpa_s->p2p_join_ssid_len));
4670 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4671 wpa_s->p2p_join_ssid,
4672 wpa_s->p2p_join_ssid_len);
4675 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4676 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4677 bss = wpa_bss_get_bssid_latest(wpa_s,
4678 wpa_s->pending_join_iface_addr);
4681 u8 dev_addr[ETH_ALEN];
4684 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4685 "from BSS table: %d MHz (SSID %s)", freq,
4686 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4687 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4689 os_memcmp(wpa_s->pending_join_dev_addr,
4690 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4691 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4693 wpa_printf(MSG_DEBUG,
4694 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4696 MAC2STR(wpa_s->pending_join_dev_addr));
4697 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4704 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4705 wpa_msg_global(wpa_s->parent, MSG_INFO,
4706 P2P_EVENT_GROUP_FORMATION_FAILURE
4707 "reason=FREQ_CONFLICT");
4708 wpas_notify_p2p_group_formation_failure(
4709 wpa_s, "FREQ_CONFLICT");
4713 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4714 "prior to joining an existing group (GO " MACSTR
4716 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4717 wpa_s->pending_pd_before_join = 1;
4719 switch (wpa_s->pending_join_wps_method) {
4720 case WPS_PIN_DISPLAY:
4721 method = WPS_CONFIG_KEYPAD;
4723 case WPS_PIN_KEYPAD:
4724 method = WPS_CONFIG_DISPLAY;
4727 method = WPS_CONFIG_PUSHBUTTON;
4730 method = WPS_CONFIG_P2PS;
4737 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4738 wpa_s->pending_join_dev_addr) ==
4741 * We have already performed provision discovery for
4742 * joining the group. Proceed directly to join
4743 * operation without duplicated provision discovery. */
4744 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4745 "with " MACSTR " already done - proceed to "
4747 MAC2STR(wpa_s->pending_join_dev_addr));
4748 wpa_s->pending_pd_before_join = 0;
4752 if (p2p_prov_disc_req(wpa_s->global->p2p,
4753 wpa_s->pending_join_dev_addr,
4755 freq, wpa_s->user_initiated_pd) < 0) {
4756 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4757 "Discovery Request before joining an "
4759 wpa_s->pending_pd_before_join = 0;
4765 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4766 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4767 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4768 wpas_p2p_check_join_scan_limit(wpa_s);
4772 /* Start join operation immediately */
4773 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4777 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4778 const u8 *ssid, size_t ssid_len)
4781 struct wpa_driver_scan_params params;
4782 struct wpabuf *wps_ie, *ies;
4784 int freqs[2] = { 0, 0 };
4786 os_memset(¶ms, 0, sizeof(params));
4788 /* P2P Wildcard SSID */
4789 params.num_ssids = 1;
4790 if (ssid && ssid_len) {
4791 params.ssids[0].ssid = ssid;
4792 params.ssids[0].ssid_len = ssid_len;
4793 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4794 wpa_s->p2p_join_ssid_len = ssid_len;
4796 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4797 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4798 wpa_s->p2p_join_ssid_len = 0;
4801 wpa_s->wps->dev.p2p = 1;
4802 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4803 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4805 if (wps_ie == NULL) {
4806 wpas_p2p_scan_res_join(wpa_s, NULL);
4810 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4811 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4813 wpabuf_free(wps_ie);
4814 wpas_p2p_scan_res_join(wpa_s, NULL);
4817 wpabuf_put_buf(ies, wps_ie);
4818 wpabuf_free(wps_ie);
4820 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4822 params.p2p_probe = 1;
4823 params.extra_ies = wpabuf_head(ies);
4824 params.extra_ies_len = wpabuf_len(ies);
4829 * If freq is not provided, check the operating freq of the GO
4830 * and use a single channel scan on if possible.
4832 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4833 wpa_s->pending_join_iface_addr);
4839 params.freqs = freqs;
4843 * Run a scan to update BSS table and start Provision Discovery once
4844 * the new scan results become available.
4846 ret = wpa_drv_scan(wpa_s, ¶ms);
4848 os_get_reltime(&wpa_s->scan_trigger_time);
4849 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4850 wpa_s->own_scan_requested = 1;
4856 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4858 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4859 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4860 wpas_p2p_check_join_scan_limit(wpa_s);
4865 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4867 struct wpa_supplicant *wpa_s = eloop_ctx;
4868 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4872 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4873 const u8 *dev_addr, enum p2p_wps_method wps_method,
4874 int auto_join, int op_freq,
4875 const u8 *ssid, size_t ssid_len)
4877 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4878 MACSTR " dev " MACSTR " op_freq=%d)%s",
4879 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4880 auto_join ? " (auto_join)" : "");
4881 if (ssid && ssid_len) {
4882 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4883 wpa_ssid_txt(ssid, ssid_len));
4886 wpa_s->p2p_auto_pd = 0;
4887 wpa_s->p2p_auto_join = !!auto_join;
4888 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4889 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4890 wpa_s->pending_join_wps_method = wps_method;
4892 /* Make sure we are not running find during connection establishment */
4893 wpas_p2p_stop_find(wpa_s);
4895 wpa_s->p2p_join_scan_count = 0;
4896 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4901 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4902 const u8 *ssid, size_t ssid_len)
4904 struct wpa_supplicant *group;
4905 struct p2p_go_neg_results res;
4906 struct wpa_bss *bss;
4908 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4911 if (group != wpa_s) {
4912 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4913 sizeof(group->p2p_pin));
4914 group->p2p_wps_method = wpa_s->p2p_wps_method;
4917 * Need to mark the current interface for p2p_group_formation
4918 * when a separate group interface is not used. This is needed
4919 * to allow p2p_cancel stop a pending p2p_connect-join.
4920 * wpas_p2p_init_group_interface() addresses this for the case
4921 * where a separate group interface is used.
4923 wpa_s->global->p2p_group_formation = wpa_s;
4926 group->p2p_in_provisioning = 1;
4927 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4929 os_memset(&res, 0, sizeof(res));
4930 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4931 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4933 res.wps_method = wpa_s->pending_join_wps_method;
4934 if (freq && ssid && ssid_len) {
4936 res.ssid_len = ssid_len;
4937 os_memcpy(res.ssid, ssid, ssid_len);
4939 bss = wpa_bss_get_bssid_latest(wpa_s,
4940 wpa_s->pending_join_iface_addr);
4942 res.freq = bss->freq;
4943 res.ssid_len = bss->ssid_len;
4944 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4945 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4947 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4951 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4952 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4954 wpa_drv_cancel_remain_on_channel(wpa_s);
4955 wpa_s->off_channel_freq = 0;
4956 wpa_s->roc_waiting_drv_freq = 0;
4958 wpas_start_wps_enrollee(group, &res);
4961 * Allow a longer timeout for join-a-running-group than normal 15
4962 * second group formation timeout since the GO may not have authorized
4963 * our connection yet.
4965 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4966 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4973 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4974 int *force_freq, int *pref_freq, int go,
4975 unsigned int *pref_freq_list,
4976 unsigned int *num_pref_freq)
4978 struct wpa_used_freq_data *freqs;
4979 int res, best_freq, num_unused;
4980 unsigned int freq_in_use = 0, num, i, max_pref_freq;
4982 max_pref_freq = *num_pref_freq;
4985 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4986 sizeof(struct wpa_used_freq_data));
4990 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4991 wpa_s->num_multichan_concurrent);
4994 * It is possible that the total number of used frequencies is bigger
4995 * than the number of frequencies used for P2P, so get the system wide
4996 * number of unused frequencies.
4998 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5000 wpa_printf(MSG_DEBUG,
5001 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5002 freq, wpa_s->num_multichan_concurrent, num, num_unused);
5007 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5009 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5011 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5012 ieee80211_is_dfs(freq)) {
5014 * If freq is a DFS channel and DFS is offloaded
5015 * to the driver, allow P2P GO to use it.
5017 wpa_printf(MSG_DEBUG,
5018 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5021 wpa_printf(MSG_DEBUG,
5022 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5029 for (i = 0; i < num; i++) {
5030 if (freqs[i].freq == freq)
5034 if (num_unused <= 0 && !freq_in_use) {
5035 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5040 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5041 "requested channel (%u MHz)", freq);
5046 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5048 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5049 enum wpa_driver_if_type iface_type;
5052 iface_type = WPA_IF_P2P_GO;
5054 iface_type = WPA_IF_P2P_CLIENT;
5056 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5059 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5062 if (!res && max_pref_freq > 0) {
5063 *num_pref_freq = max_pref_freq;
5065 while (wpas_p2p_disallowed_freq(wpa_s->global,
5066 pref_freq_list[i]) &&
5067 i < *num_pref_freq) {
5068 wpa_printf(MSG_DEBUG,
5069 "P2P: preferred_freq_list[%d]=%d is disallowed",
5070 i, pref_freq_list[i]);
5073 if (i != *num_pref_freq) {
5074 best_freq = pref_freq_list[i];
5075 wpa_printf(MSG_DEBUG,
5076 "P2P: Using preferred_freq_list[%d]=%d",
5079 wpa_printf(MSG_DEBUG,
5080 "P2P: All driver preferred frequencies are disallowed for P2P use");
5084 wpa_printf(MSG_DEBUG,
5085 "P2P: No preferred frequency list available");
5089 /* We have a candidate frequency to use */
5090 if (best_freq > 0) {
5091 if (*pref_freq == 0 && num_unused > 0) {
5092 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
5094 *pref_freq = best_freq;
5096 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
5098 *force_freq = best_freq;
5100 } else if (num_unused > 0) {
5101 wpa_printf(MSG_DEBUG,
5102 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5105 wpa_printf(MSG_DEBUG,
5106 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5120 * wpas_p2p_connect - Request P2P Group Formation to be started
5121 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5122 * @peer_addr: Address of the peer P2P Device
5123 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5124 * @persistent_group: Whether to create a persistent group
5125 * @auto_join: Whether to select join vs. GO Negotiation automatically
5126 * @join: Whether to join an existing group (as a client) instead of starting
5127 * Group Owner negotiation; @peer_addr is BSSID in that case
5128 * @auth: Whether to only authorize the connection instead of doing that and
5129 * initiating Group Owner negotiation
5130 * @go_intent: GO Intent or -1 to use default
5131 * @freq: Frequency for the group or 0 for auto-selection
5132 * @persistent_id: Persistent group credentials to use for forcing GO
5133 * parameters or -1 to generate new values (SSID/passphrase)
5134 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5135 * interoperability workaround when initiating group formation
5136 * @ht40: Start GO with 40 MHz channel width
5137 * @vht: Start GO with VHT support
5138 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5139 * failure, -2 on failure due to channel not currently available,
5140 * -3 if forced channel is not supported
5142 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5143 const char *pin, enum p2p_wps_method wps_method,
5144 int persistent_group, int auto_join, int join, int auth,
5145 int go_intent, int freq, int persistent_id, int pd,
5148 int force_freq = 0, pref_freq = 0;
5150 enum wpa_driver_if_type iftype;
5152 struct wpa_ssid *ssid = NULL;
5153 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
5155 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5158 if (persistent_id >= 0) {
5159 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5160 if (ssid == NULL || ssid->disabled != 2 ||
5161 ssid->mode != WPAS_MODE_P2P_GO)
5165 os_free(wpa_s->global->add_psk);
5166 wpa_s->global->add_psk = NULL;
5168 wpa_s->global->p2p_fail_on_wps_complete = 0;
5169 wpa_s->global->pending_p2ps_group = 0;
5170 wpa_s->p2ps_method_config_any = 0;
5173 go_intent = wpa_s->conf->p2p_go_intent;
5176 wpa_s->p2p_long_listen = 0;
5178 wpa_s->p2p_wps_method = wps_method;
5179 wpa_s->p2p_persistent_group = !!persistent_group;
5180 wpa_s->p2p_persistent_id = persistent_id;
5181 wpa_s->p2p_go_intent = go_intent;
5182 wpa_s->p2p_connect_freq = freq;
5183 wpa_s->p2p_fallback_to_go_neg = 0;
5184 wpa_s->p2p_pd_before_go_neg = !!pd;
5185 wpa_s->p2p_go_ht40 = !!ht40;
5186 wpa_s->p2p_go_vht = !!vht;
5189 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5190 else if (wps_method == WPS_PIN_DISPLAY) {
5191 ret = wps_generate_pin();
5192 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5194 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5195 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5196 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5199 wpa_s->p2p_pin[0] = '\0';
5201 if (join || auto_join) {
5202 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5204 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5205 "connect a running group from " MACSTR,
5206 MAC2STR(peer_addr));
5207 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5210 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5211 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5213 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5214 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5218 os_get_reltime(&wpa_s->p2p_auto_started);
5219 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5221 wpa_s->p2p_auto_started.sec,
5222 wpa_s->p2p_auto_started.usec);
5224 wpa_s->user_initiated_pd = 1;
5225 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5226 auto_join, freq, NULL, 0) < 0)
5231 size = P2P_MAX_PREF_CHANNELS;
5232 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5233 go_intent == 15, pref_freq_list, &size);
5236 wpas_p2p_set_own_freq_preference(wpa_s,
5237 force_freq ? force_freq : pref_freq);
5239 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5241 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5243 if (wpa_s->create_p2p_iface) {
5244 /* Prepare to add a new interface for the group */
5245 iftype = WPA_IF_P2P_GROUP;
5246 if (go_intent == 15)
5247 iftype = WPA_IF_P2P_GO;
5248 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5249 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5250 "interface for the group");
5254 if_addr = wpa_s->pending_interface_addr;
5256 if_addr = wpa_s->own_addr;
5257 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5261 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5263 force_freq, persistent_group, ssid,
5269 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5270 go_intent, if_addr, force_freq,
5271 persistent_group, ssid, pref_freq) < 0) {
5272 if (wpa_s->create_p2p_iface)
5273 wpas_p2p_remove_pending_group_interface(wpa_s);
5281 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5282 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5283 * @freq: Frequency of the channel in MHz
5284 * @duration: Duration of the stay on the channel in milliseconds
5286 * This callback is called when the driver indicates that it has started the
5287 * requested remain-on-channel duration.
5289 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5290 unsigned int freq, unsigned int duration)
5292 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5294 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)",
5295 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5296 wpa_s->roc_waiting_drv_freq, freq, duration);
5297 if (wpa_s->off_channel_freq &&
5298 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5299 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5300 wpa_s->pending_listen_duration);
5301 wpa_s->pending_listen_freq = 0;
5303 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5304 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5310 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5312 /* Limit maximum Listen state time based on driver limitation. */
5313 if (timeout > wpa_s->max_remain_on_chan)
5314 timeout = wpa_s->max_remain_on_chan;
5316 return p2p_listen(wpa_s->global->p2p, timeout);
5321 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5322 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5323 * @freq: Frequency of the channel in MHz
5325 * This callback is called when the driver indicates that a remain-on-channel
5326 * operation has been completed, i.e., the duration on the requested channel
5329 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5332 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5333 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5334 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5335 wpas_p2p_listen_work_done(wpa_s);
5336 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5338 if (wpa_s->p2p_long_listen > 0)
5339 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5340 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5341 return; /* P2P module started a new operation */
5342 if (offchannel_pending_action_tx(wpa_s))
5344 if (wpa_s->p2p_long_listen > 0) {
5345 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5346 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5349 * When listen duration is over, stop listen & update p2p_state
5352 p2p_stop_listen(wpa_s->global->p2p);
5358 * wpas_p2p_group_remove - Remove a P2P group
5359 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5360 * @ifname: Network interface name of the group interface or "*" to remove all
5362 * Returns: 0 on success, -1 on failure
5364 * This function is used to remove a P2P group. This can be used to disconnect
5365 * from a group in which the local end is a P2P Client or to end a P2P Group in
5366 * case the local end is the Group Owner. If a virtual network interface was
5367 * created for this group, that interface will be removed. Otherwise, only the
5368 * configured P2P group network will be removed from the interface.
5370 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5372 struct wpa_global *global = wpa_s->global;
5373 struct wpa_supplicant *calling_wpa_s = wpa_s;
5375 if (os_strcmp(ifname, "*") == 0) {
5376 struct wpa_supplicant *prev;
5377 wpa_s = global->ifaces;
5380 wpa_s = wpa_s->next;
5381 if (prev->p2p_group_interface !=
5382 NOT_P2P_GROUP_INTERFACE ||
5383 (prev->current_ssid &&
5384 prev->current_ssid->p2p_group))
5385 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5390 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5391 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5395 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5399 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5403 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5404 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5405 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5408 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5409 &size, pref_freq_list);
5410 if (!res && size > 0) {
5412 while (wpas_p2p_disallowed_freq(wpa_s->global,
5413 pref_freq_list[i]) &&
5415 wpa_printf(MSG_DEBUG,
5416 "P2P: preferred_freq_list[%d]=%d is disallowed",
5417 i, pref_freq_list[i]);
5421 freq = pref_freq_list[i];
5422 wpa_printf(MSG_DEBUG,
5423 "P2P: Using preferred_freq_list[%d]=%d",
5426 wpa_printf(MSG_DEBUG,
5427 "P2P: All driver preferred frequencies are disallowed for P2P use");
5430 wpa_printf(MSG_DEBUG,
5431 "P2P: No preferred frequency list available");
5436 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5438 if (wpa_s->best_24_freq > 0 &&
5439 p2p_supported_freq_go(wpa_s->global->p2p,
5440 wpa_s->best_24_freq)) {
5441 freq = wpa_s->best_24_freq;
5442 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5443 "channel: %d MHz", freq);
5445 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5447 freq = 2412 + (r % 3) * 25;
5448 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5449 "channel: %d MHz", freq);
5454 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5456 if (wpa_s->best_5_freq > 0 &&
5457 p2p_supported_freq_go(wpa_s->global->p2p,
5458 wpa_s->best_5_freq)) {
5459 freq = wpa_s->best_5_freq;
5460 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5461 "channel: %d MHz", freq);
5463 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5465 freq = 5180 + (r % 4) * 20;
5466 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5467 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5468 "5 GHz channel for P2P group");
5471 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5472 "channel: %d MHz", freq);
5476 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5477 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5478 ieee80211_is_dfs(freq)) {
5480 * If freq is a DFS channel and DFS is offloaded to the
5481 * driver, allow P2P GO to use it.
5483 wpa_printf(MSG_DEBUG, "P2P: "
5484 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5488 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5489 "(%u MHz) is not supported for P2P uses",
5498 static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5499 const struct p2p_channels *channels,
5502 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5503 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5504 freq_included(wpa_s, channels, freq))
5510 static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5511 struct p2p_go_neg_results *params,
5512 const struct p2p_channels *channels)
5516 /* first try some random selection of the social channels */
5517 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5520 for (i = 0; i < 3; i++) {
5521 params->freq = 2412 + ((r + i) % 3) * 25;
5522 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5526 /* try all other channels in operating class 81 */
5527 for (i = 0; i < 11; i++) {
5528 params->freq = 2412 + i * 5;
5530 /* skip social channels; covered in the previous loop */
5531 if (params->freq == 2412 ||
5532 params->freq == 2437 ||
5533 params->freq == 2462)
5536 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5540 /* try all channels in operating class 115 */
5541 for (i = 0; i < 4; i++) {
5542 params->freq = 5180 + i * 20;
5543 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5544 freq_included(wpa_s, channels, params->freq) &&
5545 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5549 /* try all channels in operating class 124 */
5550 for (i = 0; i < 4; i++) {
5551 params->freq = 5745 + i * 20;
5552 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5553 freq_included(wpa_s, channels, params->freq) &&
5554 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5558 /* try social channel class 180 channel 2 */
5559 params->freq = 58320 + 1 * 2160;
5560 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5561 freq_included(wpa_s, channels, params->freq) &&
5562 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5565 /* try all channels in reg. class 180 */
5566 for (i = 0; i < 4; i++) {
5567 params->freq = 58320 + i * 2160;
5568 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5569 freq_included(wpa_s, channels, params->freq) &&
5570 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5575 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
5578 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5583 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5584 struct p2p_go_neg_results *params,
5585 int freq, int ht40, int vht,
5586 const struct p2p_channels *channels)
5588 struct wpa_used_freq_data *freqs;
5590 unsigned int num, i;
5591 int ignore_no_freqs = 0;
5593 os_memset(params, 0, sizeof(*params));
5594 params->role_go = 1;
5595 params->ht40 = ht40;
5598 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5599 sizeof(struct wpa_used_freq_data));
5603 num = get_shared_radio_freqs_data(wpa_s, freqs,
5604 wpa_s->num_multichan_concurrent);
5606 if (wpa_s->current_ssid &&
5607 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5608 wpa_s->wpa_state == WPA_COMPLETED) {
5609 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5613 * If the frequency selection is done for an active P2P GO that
5614 * is not sharing a frequency, allow to select a new frequency
5615 * even if there are no unused frequencies as we are about to
5616 * move the P2P GO so its frequency can be re-used.
5618 for (i = 0; i < num; i++) {
5619 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5620 freqs[i].flags == 0) {
5621 ignore_no_freqs = 1;
5627 /* try using the forced freq */
5629 if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
5630 wpa_printf(MSG_DEBUG,
5631 "P2P: Forced GO freq %d MHz not accepted",
5636 for (i = 0; i < num; i++) {
5637 if (freqs[i].freq == freq) {
5638 wpa_printf(MSG_DEBUG,
5639 "P2P: forced freq (%d MHz) is also shared",
5641 params->freq = freq;
5646 if (!ignore_no_freqs &&
5647 wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5648 wpa_printf(MSG_DEBUG,
5649 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5654 wpa_printf(MSG_DEBUG,
5655 "P2P: force GO freq (%d MHz) on a free channel",
5657 params->freq = freq;
5661 /* consider using one of the shared frequencies */
5663 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5664 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5665 wpa_printf(MSG_DEBUG,
5666 "P2P: Use shared freq (%d MHz) for GO",
5668 params->freq = cand;
5672 /* try using one of the shared freqs */
5673 for (i = 0; i < num; i++) {
5674 if (wpas_p2p_supported_freq_go(wpa_s, channels,
5676 wpa_printf(MSG_DEBUG,
5677 "P2P: Use shared freq (%d MHz) for GO",
5679 params->freq = freqs[i].freq;
5685 if (!ignore_no_freqs &&
5686 wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5687 wpa_printf(MSG_DEBUG,
5688 "P2P: Cannot force GO on any of the channels we are already using");
5692 /* try using the setting from the configuration file */
5693 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5694 wpa_s->conf->p2p_oper_channel >= 1 &&
5695 wpa_s->conf->p2p_oper_channel <= 11 &&
5696 wpas_p2p_supported_freq_go(
5698 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5699 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5700 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5701 "frequency %d MHz", params->freq);
5705 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5706 wpa_s->conf->p2p_oper_reg_class == 116 ||
5707 wpa_s->conf->p2p_oper_reg_class == 117 ||
5708 wpa_s->conf->p2p_oper_reg_class == 124 ||
5709 wpa_s->conf->p2p_oper_reg_class == 125 ||
5710 wpa_s->conf->p2p_oper_reg_class == 126 ||
5711 wpa_s->conf->p2p_oper_reg_class == 127) &&
5712 wpas_p2p_supported_freq_go(wpa_s, channels,
5714 5 * wpa_s->conf->p2p_oper_channel)) {
5715 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5716 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5717 "frequency %d MHz", params->freq);
5721 /* Try using best channels */
5722 if (wpa_s->conf->p2p_oper_channel == 0 &&
5723 wpa_s->best_overall_freq > 0 &&
5724 wpas_p2p_supported_freq_go(wpa_s, channels,
5725 wpa_s->best_overall_freq)) {
5726 params->freq = wpa_s->best_overall_freq;
5727 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5728 "channel %d MHz", params->freq);
5732 if (wpa_s->conf->p2p_oper_channel == 0 &&
5733 wpa_s->best_24_freq > 0 &&
5734 wpas_p2p_supported_freq_go(wpa_s, channels,
5735 wpa_s->best_24_freq)) {
5736 params->freq = wpa_s->best_24_freq;
5737 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5738 "channel %d MHz", params->freq);
5742 if (wpa_s->conf->p2p_oper_channel == 0 &&
5743 wpa_s->best_5_freq > 0 &&
5744 wpas_p2p_supported_freq_go(wpa_s, channels,
5745 wpa_s->best_5_freq)) {
5746 params->freq = wpa_s->best_5_freq;
5747 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5748 "channel %d MHz", params->freq);
5752 /* try using preferred channels */
5753 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
5754 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5755 params->freq = cand;
5756 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5757 "channels", params->freq);
5761 /* Try using one of the group common freqs */
5762 if (wpa_s->p2p_group_common_freqs) {
5763 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
5764 cand = wpa_s->p2p_group_common_freqs[i];
5765 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5766 params->freq = cand;
5767 wpa_printf(MSG_DEBUG,
5768 "P2P: Use freq %d MHz common with the peer",
5775 /* no preference, select some channel */
5776 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
5778 if (params->freq == 0) {
5779 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
5792 static struct wpa_supplicant *
5793 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5796 struct wpa_supplicant *group_wpa_s;
5798 if (!wpas_p2p_create_iface(wpa_s)) {
5799 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5801 wpa_s->p2p_first_connection_timeout = 0;
5805 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5806 WPA_IF_P2P_CLIENT) < 0) {
5807 wpa_msg_global(wpa_s, MSG_ERROR,
5808 "P2P: Failed to add group interface");
5811 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5812 if (group_wpa_s == NULL) {
5813 wpa_msg_global(wpa_s, MSG_ERROR,
5814 "P2P: Failed to initialize group interface");
5815 wpas_p2p_remove_pending_group_interface(wpa_s);
5819 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5820 group_wpa_s->ifname);
5821 group_wpa_s->p2p_first_connection_timeout = 0;
5827 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5828 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5829 * @persistent_group: Whether to create a persistent group
5830 * @freq: Frequency for the group or 0 to indicate no hardcoding
5831 * @ht40: Start GO with 40 MHz channel width
5832 * @vht: Start GO with VHT support
5833 * Returns: 0 on success, -1 on failure
5835 * This function creates a new P2P group with the local end as the Group Owner,
5836 * i.e., without using Group Owner Negotiation.
5838 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5839 int freq, int ht40, int vht)
5841 struct p2p_go_neg_results params;
5843 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5846 os_free(wpa_s->global->add_psk);
5847 wpa_s->global->add_psk = NULL;
5849 /* Make sure we are not running find during connection establishment */
5850 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5851 wpas_p2p_stop_find_oper(wpa_s);
5853 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5857 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5860 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5861 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5862 ieee80211_is_dfs(params.freq)) {
5864 * If freq is a DFS channel and DFS is offloaded to the
5865 * driver, allow P2P GO to use it.
5867 wpa_printf(MSG_DEBUG,
5868 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5869 __func__, params.freq);
5871 wpa_printf(MSG_DEBUG,
5872 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5877 p2p_go_params(wpa_s->global->p2p, ¶ms);
5878 params.persistent_group = persistent_group;
5880 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5883 wpas_start_wps_go(wpa_s, ¶ms, 0);
5889 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5890 struct wpa_ssid *params, int addr_allocated,
5891 int freq, int force_scan)
5893 struct wpa_ssid *ssid;
5895 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5899 os_get_reltime(&wpa_s->scan_min_time);
5900 wpa_s->p2p_last_4way_hs_fail = NULL;
5902 wpa_supplicant_ap_deinit(wpa_s);
5904 ssid = wpa_config_add_network(wpa_s->conf);
5907 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5908 wpa_config_set_network_defaults(ssid);
5909 ssid->temporary = 1;
5910 ssid->proto = WPA_PROTO_RSN;
5911 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5912 ssid->group_cipher = WPA_CIPHER_CCMP;
5913 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5914 ssid->ssid = os_malloc(params->ssid_len);
5915 if (ssid->ssid == NULL) {
5916 wpa_config_remove_network(wpa_s->conf, ssid->id);
5919 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5920 ssid->ssid_len = params->ssid_len;
5921 ssid->p2p_group = 1;
5922 ssid->export_keys = 1;
5923 if (params->psk_set) {
5924 os_memcpy(ssid->psk, params->psk, 32);
5927 if (params->passphrase)
5928 ssid->passphrase = os_strdup(params->passphrase);
5930 wpa_s->show_group_started = 1;
5931 wpa_s->p2p_in_invitation = 1;
5932 wpa_s->p2p_invite_go_freq = freq;
5934 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5936 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5937 wpas_p2p_group_formation_timeout,
5938 wpa_s->parent, NULL);
5939 wpa_supplicant_select_network(wpa_s, ssid);
5945 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5946 struct wpa_ssid *ssid, int addr_allocated,
5947 int force_freq, int neg_freq, int ht40,
5948 int vht, const struct p2p_channels *channels,
5949 int connection_timeout, int force_scan)
5951 struct p2p_go_neg_results params;
5954 if (ssid->disabled != 2 || ssid->ssid == NULL)
5957 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5958 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5959 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5962 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5963 wpa_s->parent, NULL)) {
5965 * This can happen if Invitation Response frame was lost
5966 * and the peer (GO of a persistent group) tries to
5967 * invite us again. Reschedule the timeout to avoid
5968 * terminating the wait for the connection too early
5969 * since we now know that the peer is still trying to
5970 * invite us instead of having already started the GO.
5972 wpa_printf(MSG_DEBUG,
5973 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
5974 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5975 wpas_p2p_group_formation_timeout,
5976 wpa_s->parent, NULL);
5981 os_free(wpa_s->global->add_psk);
5982 wpa_s->global->add_psk = NULL;
5984 /* Make sure we are not running find during connection establishment */
5985 wpas_p2p_stop_find_oper(wpa_s);
5987 wpa_s->p2p_fallback_to_go_neg = 0;
5989 if (ssid->mode == WPAS_MODE_P2P_GO) {
5990 if (force_freq > 0) {
5991 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5995 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5997 (freq > 0 && !freq_included(wpa_s, channels, freq)))
6000 } else if (ssid->mode == WPAS_MODE_INFRA) {
6002 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
6003 struct os_reltime now;
6004 struct wpa_bss *bss =
6005 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
6007 os_get_reltime(&now);
6009 !os_reltime_expired(&now, &bss->last_update, 5) &&
6010 freq_included(wpa_s, channels, bss->freq))
6016 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6022 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
6026 params.psk_set = ssid->psk_set;
6028 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
6029 if (ssid->passphrase) {
6030 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6031 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6032 "persistent group");
6035 os_strlcpy(params.passphrase, ssid->passphrase,
6036 sizeof(params.passphrase));
6038 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6039 params.ssid_len = ssid->ssid_len;
6040 params.persistent_group = 1;
6042 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6046 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6048 wpa_s->p2p_first_connection_timeout = connection_timeout;
6049 wpas_start_wps_go(wpa_s, ¶ms, 0);
6055 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6056 struct wpabuf *proberesp_ies)
6058 struct wpa_supplicant *wpa_s = ctx;
6059 if (wpa_s->ap_iface) {
6060 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
6061 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6062 wpabuf_free(beacon_ies);
6063 wpabuf_free(proberesp_ies);
6067 wpabuf_free(hapd->p2p_beacon_ie);
6068 hapd->p2p_beacon_ie = beacon_ies;
6070 wpabuf_free(hapd->p2p_probe_resp_ie);
6071 hapd->p2p_probe_resp_ie = proberesp_ies;
6073 wpabuf_free(beacon_ies);
6074 wpabuf_free(proberesp_ies);
6076 wpa_supplicant_ap_update_beacon(wpa_s);
6080 static void wpas_p2p_idle_update(void *ctx, int idle)
6082 struct wpa_supplicant *wpa_s = ctx;
6083 if (!wpa_s->ap_iface)
6085 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
6087 if (wpa_s->global->p2p_fail_on_wps_complete &&
6088 wpa_s->p2p_in_provisioning) {
6089 wpas_p2p_grpform_fail_after_wps(wpa_s);
6092 wpas_p2p_set_group_idle_timeout(wpa_s);
6094 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6098 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6099 struct wpa_ssid *ssid)
6101 struct p2p_group *group;
6102 struct p2p_group_config *cfg;
6104 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6107 cfg = os_zalloc(sizeof(*cfg));
6111 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
6112 cfg->persistent_group = 2;
6113 else if (ssid->p2p_persistent_group)
6114 cfg->persistent_group = 1;
6115 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6116 if (wpa_s->max_stations &&
6117 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6118 cfg->max_clients = wpa_s->max_stations;
6120 cfg->max_clients = wpa_s->conf->max_num_sta;
6121 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6122 cfg->ssid_len = ssid->ssid_len;
6123 cfg->freq = ssid->frequency;
6124 cfg->cb_ctx = wpa_s;
6125 cfg->ie_update = wpas_p2p_ie_update;
6126 cfg->idle_update = wpas_p2p_idle_update;
6128 group = p2p_group_init(wpa_s->global->p2p, cfg);
6131 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
6132 p2p_group_notif_formation_done(group);
6133 wpa_s->p2p_group = group;
6138 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6141 struct wpa_ssid *ssid = wpa_s->current_ssid;
6143 if (!wpa_s->p2p_in_provisioning) {
6144 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6145 "provisioning not in progress");
6149 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6150 u8 go_dev_addr[ETH_ALEN];
6151 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6152 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6154 /* Clear any stored provisioning info */
6155 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6158 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6160 wpa_s->p2p_go_group_formation_completed = 1;
6161 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6163 * Use a separate timeout for initial data connection to
6164 * complete to allow the group to be removed automatically if
6165 * something goes wrong in this step before the P2P group idle
6166 * timeout mechanism is taken into use.
6168 wpa_dbg(wpa_s, MSG_DEBUG,
6169 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6170 P2P_MAX_INITIAL_CONN_WAIT);
6171 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6172 wpas_p2p_group_formation_timeout,
6173 wpa_s->parent, NULL);
6176 * Use a separate timeout for initial data connection to
6177 * complete to allow the group to be removed automatically if
6178 * the client does not complete data connection successfully.
6180 wpa_dbg(wpa_s, MSG_DEBUG,
6181 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6182 P2P_MAX_INITIAL_CONN_WAIT_GO);
6183 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6184 wpas_p2p_group_formation_timeout,
6185 wpa_s->parent, NULL);
6187 * Complete group formation on first successful data connection
6189 wpa_s->p2p_go_group_formation_completed = 0;
6191 if (wpa_s->global->p2p)
6192 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
6193 wpas_group_formation_completed(wpa_s, 1, 0);
6197 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6198 struct wps_event_fail *fail)
6200 if (!wpa_s->p2p_in_provisioning) {
6201 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6202 "provisioning not in progress");
6206 if (wpa_s->go_params) {
6207 p2p_clear_provisioning_info(
6209 wpa_s->go_params->peer_device_addr);
6212 wpas_notify_p2p_wps_failed(wpa_s, fail);
6214 if (wpa_s == wpa_s->global->p2p_group_formation) {
6216 * Allow some time for the failed WPS negotiation exchange to
6217 * complete, but remove the group since group formation cannot
6218 * succeed after provisioning failure.
6220 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6221 wpa_s->global->p2p_fail_on_wps_complete = 1;
6222 eloop_deplete_timeout(0, 50000,
6223 wpas_p2p_group_formation_timeout,
6224 wpa_s->parent, NULL);
6229 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6231 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6232 !wpa_s->p2p_in_provisioning)
6235 wpas_p2p_grpform_fail_after_wps(wpa_s);
6241 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6242 const char *config_method,
6243 enum wpas_p2p_prov_disc_use use,
6244 struct p2ps_provision *p2ps_prov)
6248 wpa_s->global->pending_p2ps_group = 0;
6249 wpa_s->p2p_fallback_to_go_neg = 0;
6250 wpa_s->pending_pd_use = NORMAL_PD;
6251 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6252 p2ps_prov->conncap = p2ps_group_capability(
6253 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6254 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6256 wpa_printf(MSG_DEBUG,
6257 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6258 __func__, p2ps_prov->conncap,
6259 p2ps_prov->adv_id, p2ps_prov->conncap,
6260 p2ps_prov->status, p2ps_prov->info);
6263 } else if (os_strncmp(config_method, "display", 7) == 0)
6264 config_methods = WPS_CONFIG_DISPLAY;
6265 else if (os_strncmp(config_method, "keypad", 6) == 0)
6266 config_methods = WPS_CONFIG_KEYPAD;
6267 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6268 os_strncmp(config_method, "pushbutton", 10) == 0)
6269 config_methods = WPS_CONFIG_PUSHBUTTON;
6271 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
6276 if (use == WPAS_P2P_PD_AUTO) {
6277 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6278 wpa_s->pending_pd_config_methods = config_methods;
6279 wpa_s->p2p_auto_pd = 1;
6280 wpa_s->p2p_auto_join = 0;
6281 wpa_s->pending_pd_before_join = 0;
6282 wpa_s->auto_pd_scan_retry = 0;
6283 wpas_p2p_stop_find(wpa_s);
6284 wpa_s->p2p_join_scan_count = 0;
6285 os_get_reltime(&wpa_s->p2p_auto_started);
6286 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6287 wpa_s->p2p_auto_started.sec,
6288 wpa_s->p2p_auto_started.usec);
6289 wpas_p2p_join_scan(wpa_s, NULL);
6293 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6298 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6299 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6304 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6307 return p2p_scan_result_text(ies, ies_len, buf, end);
6311 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6313 if (!offchannel_pending_action_tx(wpa_s))
6316 wpas_p2p_action_tx_clear(wpa_s);
6318 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6319 "operation request");
6320 offchannel_clear_pending_action_tx(wpa_s);
6324 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6325 enum p2p_discovery_type type,
6326 unsigned int num_req_dev_types, const u8 *req_dev_types,
6327 const u8 *dev_id, unsigned int search_delay,
6328 u8 seek_cnt, const char **seek_string, int freq)
6330 wpas_p2p_clear_pending_action_tx(wpa_s);
6331 wpa_s->p2p_long_listen = 0;
6333 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6334 wpa_s->p2p_in_provisioning)
6337 wpa_supplicant_cancel_sched_scan(wpa_s);
6339 return p2p_find(wpa_s->global->p2p, timeout, type,
6340 num_req_dev_types, req_dev_types, dev_id,
6341 search_delay, seek_cnt, seek_string, freq);
6345 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6346 struct wpa_scan_results *scan_res)
6348 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6350 if (wpa_s->p2p_scan_work) {
6351 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6352 wpa_s->p2p_scan_work = NULL;
6353 radio_work_done(work);
6356 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6360 * Indicate that results have been processed so that the P2P module can
6361 * continue pending tasks.
6363 p2p_scan_res_handled(wpa_s->global->p2p);
6367 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
6369 wpas_p2p_clear_pending_action_tx(wpa_s);
6370 wpa_s->p2p_long_listen = 0;
6371 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6372 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6374 if (wpa_s->global->p2p)
6375 p2p_stop_find(wpa_s->global->p2p);
6377 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6378 wpa_printf(MSG_DEBUG,
6379 "P2P: Do not consider the scan results after stop_find");
6380 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6385 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6387 wpas_p2p_stop_find_oper(wpa_s);
6388 if (!wpa_s->global->pending_group_iface_for_p2ps)
6389 wpas_p2p_remove_pending_group_interface(wpa_s);
6393 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6395 struct wpa_supplicant *wpa_s = eloop_ctx;
6396 wpa_s->p2p_long_listen = 0;
6400 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6404 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6407 wpa_supplicant_cancel_sched_scan(wpa_s);
6408 wpas_p2p_clear_pending_action_tx(wpa_s);
6412 * This is a request for unlimited Listen state. However, at
6413 * least for now, this is mapped to a Listen state for one
6418 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6419 wpa_s->p2p_long_listen = 0;
6422 * Stop previous find/listen operation to avoid trying to request a new
6423 * remain-on-channel operation while the driver is still running the
6426 if (wpa_s->global->p2p)
6427 p2p_stop_find(wpa_s->global->p2p);
6429 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6430 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6431 wpa_s->p2p_long_listen = timeout * 1000;
6432 eloop_register_timeout(timeout, 0,
6433 wpas_p2p_long_listen_timeout,
6441 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6442 u8 *buf, size_t len, int p2p_group)
6444 struct wpabuf *p2p_ie;
6447 if (wpa_s->global->p2p_disabled)
6450 * Advertize mandatory cross connection capability even on
6451 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6453 if (wpa_s->conf->p2p_disabled && p2p_group)
6455 if (wpa_s->global->p2p == NULL)
6460 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6461 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6463 wpabuf_free(p2p_ie);
6469 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
6470 const u8 *dst, const u8 *bssid,
6471 const u8 *ie, size_t ie_len,
6472 unsigned int rx_freq, int ssi_signal)
6474 if (wpa_s->global->p2p_disabled)
6476 if (wpa_s->global->p2p == NULL)
6479 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6480 ie, ie_len, rx_freq)) {
6481 case P2P_PREQ_NOT_P2P:
6482 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6485 case P2P_PREQ_MALFORMED:
6486 case P2P_PREQ_NOT_LISTEN:
6487 case P2P_PREQ_NOT_PROCESSED:
6488 default: /* make gcc happy */
6490 case P2P_PREQ_PROCESSED:
6496 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6497 const u8 *sa, const u8 *bssid,
6498 u8 category, const u8 *data, size_t len, int freq)
6500 if (wpa_s->global->p2p_disabled)
6502 if (wpa_s->global->p2p == NULL)
6505 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6510 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6512 if (wpa_s->global->p2p_disabled)
6514 if (wpa_s->global->p2p == NULL)
6517 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6521 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6523 p2p_group_deinit(wpa_s->p2p_group);
6524 wpa_s->p2p_group = NULL;
6526 wpa_s->ap_configured_cb = NULL;
6527 wpa_s->ap_configured_cb_ctx = NULL;
6528 wpa_s->ap_configured_cb_data = NULL;
6529 wpa_s->connect_without_scan = NULL;
6533 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6535 wpa_s->p2p_long_listen = 0;
6537 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6540 return p2p_reject(wpa_s->global->p2p, addr);
6544 /* Invite to reinvoke a persistent group */
6545 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6546 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6547 int ht40, int vht, int pref_freq)
6549 enum p2p_invite_role role;
6553 int no_pref_freq_given = pref_freq == 0;
6554 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6556 wpa_s->global->p2p_invite_group = NULL;
6558 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6560 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6562 wpa_s->p2p_persistent_go_freq = freq;
6563 wpa_s->p2p_go_ht40 = !!ht40;
6564 if (ssid->mode == WPAS_MODE_P2P_GO) {
6565 role = P2P_INVITE_ROLE_GO;
6566 if (peer_addr == NULL) {
6567 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6568 "address in invitation command");
6571 if (wpas_p2p_create_iface(wpa_s)) {
6572 if (wpas_p2p_add_group_interface(wpa_s,
6573 WPA_IF_P2P_GO) < 0) {
6574 wpa_printf(MSG_ERROR, "P2P: Failed to "
6575 "allocate a new interface for the "
6579 bssid = wpa_s->pending_interface_addr;
6581 bssid = wpa_s->own_addr;
6583 role = P2P_INVITE_ROLE_CLIENT;
6584 peer_addr = ssid->bssid;
6586 wpa_s->pending_invite_ssid_id = ssid->id;
6588 size = P2P_MAX_PREF_CHANNELS;
6589 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6590 role == P2P_INVITE_ROLE_GO,
6591 pref_freq_list, &size);
6594 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6596 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6599 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6600 no_pref_freq_given && pref_freq > 0 &&
6601 wpa_s->num_multichan_concurrent > 1 &&
6602 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6603 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6609 * Stop any find/listen operations before invitation and possibly
6610 * connection establishment.
6612 wpas_p2p_stop_find_oper(wpa_s);
6614 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6615 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6620 /* Invite to join an active group */
6621 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6622 const u8 *peer_addr, const u8 *go_dev_addr)
6624 struct wpa_global *global = wpa_s->global;
6625 enum p2p_invite_role role;
6627 struct wpa_ssid *ssid;
6629 int freq = 0, force_freq = 0, pref_freq = 0;
6631 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6633 wpa_s->p2p_persistent_go_freq = 0;
6634 wpa_s->p2p_go_ht40 = 0;
6635 wpa_s->p2p_go_vht = 0;
6637 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6638 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6641 if (wpa_s == NULL) {
6642 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6646 ssid = wpa_s->current_ssid;
6648 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6653 wpa_s->global->p2p_invite_group = wpa_s;
6654 persistent = ssid->p2p_persistent_group &&
6655 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6656 ssid->ssid, ssid->ssid_len);
6658 if (ssid->mode == WPAS_MODE_P2P_GO) {
6659 role = P2P_INVITE_ROLE_ACTIVE_GO;
6660 bssid = wpa_s->own_addr;
6661 if (go_dev_addr == NULL)
6662 go_dev_addr = wpa_s->global->p2p_dev_addr;
6663 freq = ssid->frequency;
6665 role = P2P_INVITE_ROLE_CLIENT;
6666 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6667 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6668 "invite to current group");
6671 bssid = wpa_s->bssid;
6672 if (go_dev_addr == NULL &&
6673 !is_zero_ether_addr(wpa_s->go_dev_addr))
6674 go_dev_addr = wpa_s->go_dev_addr;
6675 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6676 (int) wpa_s->assoc_freq;
6678 wpa_s->parent->pending_invite_ssid_id = -1;
6680 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6683 size = P2P_MAX_PREF_CHANNELS;
6684 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6685 role == P2P_INVITE_ROLE_ACTIVE_GO,
6686 pref_freq_list, &size);
6689 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6691 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6692 ssid->ssid, ssid->ssid_len, force_freq,
6693 go_dev_addr, persistent, pref_freq, -1);
6697 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6699 struct wpa_ssid *ssid = wpa_s->current_ssid;
6700 u8 go_dev_addr[ETH_ALEN];
6701 int network_id = -1;
6707 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6708 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6709 wpa_s->parent, NULL);
6712 if (!wpa_s->show_group_started || !ssid)
6715 wpa_s->show_group_started = 0;
6717 os_memset(go_dev_addr, 0, ETH_ALEN);
6718 if (ssid->bssid_set)
6719 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6720 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6722 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6724 if (wpa_s->global->p2p_group_formation == wpa_s)
6725 wpa_s->global->p2p_group_formation = NULL;
6727 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6728 (int) wpa_s->assoc_freq;
6731 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6734 res = os_snprintf(ip_addr, sizeof(ip_addr),
6735 " ip_addr=%u.%u.%u.%u "
6736 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6737 ip[0], ip[1], ip[2], ip[3],
6738 ip[4], ip[5], ip[6], ip[7],
6739 ip[8], ip[9], ip[10], ip[11]);
6740 if (os_snprintf_error(sizeof(ip_addr), res))
6744 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6745 ssid->passphrase == NULL && ssid->psk_set ?
6747 ssid->passphrase, go_dev_addr, persistent,
6751 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6754 network_id = ssid->id;
6755 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6759 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6760 u32 interval1, u32 duration2, u32 interval2)
6764 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6767 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6768 wpa_s->current_ssid == NULL ||
6769 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6772 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6773 wpa_s->own_addr, wpa_s->assoc_freq,
6774 duration1, interval1, duration2, interval2);
6776 wpa_s->waiting_presence_resp = 1;
6782 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6783 unsigned int interval)
6785 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6788 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6792 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6794 if (wpa_s->current_ssid == NULL) {
6796 * current_ssid can be cleared when P2P client interface gets
6797 * disconnected, so assume this interface was used as P2P
6802 return wpa_s->current_ssid->p2p_group &&
6803 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6807 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6809 struct wpa_supplicant *wpa_s = eloop_ctx;
6811 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6812 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6817 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6819 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6823 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6827 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6828 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6830 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6833 timeout = wpa_s->conf->p2p_group_idle;
6834 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6835 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6836 timeout = P2P_MAX_CLIENT_IDLE;
6842 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6843 timeout = 0; /* special client mode no-timeout */
6848 if (wpa_s->p2p_in_provisioning) {
6850 * Use the normal group formation timeout during the
6851 * provisioning phase to avoid terminating this process too
6852 * early due to group idle timeout.
6854 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6855 "during provisioning");
6859 if (wpa_s->show_group_started) {
6861 * Use the normal group formation timeout between the end of
6862 * the provisioning phase and completion of 4-way handshake to
6863 * avoid terminating this process too early due to group idle
6866 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6867 "while waiting for initial 4-way handshake to "
6872 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6874 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6879 /* Returns 1 if the interface was removed */
6880 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6881 u16 reason_code, const u8 *ie, size_t ie_len,
6882 int locally_generated)
6884 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6887 if (!locally_generated)
6888 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6891 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6892 wpa_s->current_ssid &&
6893 wpa_s->current_ssid->p2p_group &&
6894 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6895 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6896 "session is ending");
6897 if (wpas_p2p_group_delete(wpa_s,
6898 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6907 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6908 u16 reason_code, const u8 *ie, size_t ie_len,
6909 int locally_generated)
6911 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6914 if (!locally_generated)
6915 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6920 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6922 struct p2p_data *p2p = wpa_s->global->p2p;
6927 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6930 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6931 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6933 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6934 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6937 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6938 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6940 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6941 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6943 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6944 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6945 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6946 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6947 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6950 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6951 p2p_set_sec_dev_types(p2p,
6952 (void *) wpa_s->conf->sec_device_type,
6953 wpa_s->conf->num_sec_device_types);
6955 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6957 p2p_remove_wps_vendor_extensions(p2p);
6958 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6959 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6961 p2p_add_wps_vendor_extension(
6962 p2p, wpa_s->conf->wps_vendor_ext[i]);
6966 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6967 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6969 country[0] = wpa_s->conf->country[0];
6970 country[1] = wpa_s->conf->country[1];
6972 p2p_set_country(p2p, country);
6975 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6976 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6977 wpa_s->conf->p2p_ssid_postfix ?
6978 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6982 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6983 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6985 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6986 u8 reg_class, channel;
6991 if (wpa_s->conf->p2p_listen_reg_class &&
6992 wpa_s->conf->p2p_listen_channel) {
6993 reg_class = wpa_s->conf->p2p_listen_reg_class;
6994 channel = wpa_s->conf->p2p_listen_channel;
6999 * Pick one of the social channels randomly as the
7002 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7005 channel = 1 + (r % 3) * 5;
7008 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7011 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7014 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7015 u8 op_reg_class, op_channel, cfg_op_channel;
7018 if (wpa_s->conf->p2p_oper_reg_class &&
7019 wpa_s->conf->p2p_oper_channel) {
7020 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7021 op_channel = wpa_s->conf->p2p_oper_channel;
7026 * Use random operation channel from (1, 6, 11)
7027 *if no other preference is indicated.
7029 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7032 op_channel = 1 + (r % 3) * 5;
7035 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7038 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7042 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7043 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7044 wpa_s->conf->p2p_pref_chan) < 0) {
7045 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7049 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7050 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7055 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7056 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
7060 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7063 if (!wpa_s->ap_iface)
7065 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7070 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7072 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7075 wpa_s->global->cross_connection = enabled;
7076 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7079 struct wpa_supplicant *iface;
7081 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7083 if (iface->cross_connect_enabled == 0)
7086 iface->cross_connect_enabled = 0;
7087 iface->cross_connect_in_use = 0;
7088 wpa_msg_global(iface->parent, MSG_INFO,
7089 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7091 iface->cross_connect_uplink);
7099 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7101 struct wpa_supplicant *iface;
7103 if (!uplink->global->cross_connection)
7106 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7107 if (!iface->cross_connect_enabled)
7109 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7112 if (iface->ap_iface == NULL)
7114 if (iface->cross_connect_in_use)
7117 iface->cross_connect_in_use = 1;
7118 wpa_msg_global(iface->parent, MSG_INFO,
7119 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7120 iface->ifname, iface->cross_connect_uplink);
7125 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7127 struct wpa_supplicant *iface;
7129 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7130 if (!iface->cross_connect_enabled)
7132 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7135 if (!iface->cross_connect_in_use)
7138 wpa_msg_global(iface->parent, MSG_INFO,
7139 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7140 iface->ifname, iface->cross_connect_uplink);
7141 iface->cross_connect_in_use = 0;
7146 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7148 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7149 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7150 wpa_s->cross_connect_disallowed)
7151 wpas_p2p_disable_cross_connect(wpa_s);
7153 wpas_p2p_enable_cross_connect(wpa_s);
7154 if (!wpa_s->ap_iface &&
7155 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7156 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7160 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7162 wpas_p2p_disable_cross_connect(wpa_s);
7163 if (!wpa_s->ap_iface &&
7164 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7166 wpas_p2p_set_group_idle_timeout(wpa_s);
7170 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7172 struct wpa_supplicant *iface;
7174 if (!wpa_s->global->cross_connection)
7177 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7180 if (iface->drv_flags &
7181 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7183 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7184 iface != wpa_s->parent)
7187 wpa_s->cross_connect_enabled = 1;
7188 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7189 sizeof(wpa_s->cross_connect_uplink));
7190 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7191 "%s to %s whenever uplink is available",
7192 wpa_s->ifname, wpa_s->cross_connect_uplink);
7194 if (iface->ap_iface || iface->current_ssid == NULL ||
7195 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7196 iface->cross_connect_disallowed ||
7197 iface->wpa_state != WPA_COMPLETED)
7200 wpa_s->cross_connect_in_use = 1;
7201 wpa_msg_global(wpa_s->parent, MSG_INFO,
7202 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7203 wpa_s->ifname, wpa_s->cross_connect_uplink);
7209 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7211 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7212 !wpa_s->p2p_in_provisioning)
7213 return 0; /* not P2P client operation */
7215 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7217 if (wpa_s != wpa_s->parent)
7218 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
7219 wpas_p2p_group_formation_failed(wpa_s, 0);
7224 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7226 struct wpa_supplicant *wpa_s = eloop_ctx;
7227 wpas_p2p_notif_pbc_overlap(wpa_s);
7231 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7232 enum wpas_p2p_channel_update_trig trig)
7234 struct p2p_channels chan, cli_chan;
7235 struct wpa_used_freq_data *freqs = NULL;
7236 unsigned int num = wpa_s->num_multichan_concurrent;
7238 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7241 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7245 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7247 os_memset(&chan, 0, sizeof(chan));
7248 os_memset(&cli_chan, 0, sizeof(cli_chan));
7249 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
7250 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7255 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
7257 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7260 * The used frequencies map changed, so it is possible that a GO is
7261 * using a channel that is no longer valid for P2P use. It is also
7262 * possible that due to policy consideration, it would be preferable to
7263 * move it to a frequency already used by other station interfaces.
7265 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7271 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7272 struct wpa_scan_results *scan_res)
7274 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7278 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7280 struct wpa_global *global = wpa_s->global;
7284 if (global->p2p == NULL)
7287 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7289 if (wpa_s->pending_interface_name[0] &&
7290 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7293 peer = p2p_get_go_neg_peer(global->p2p);
7295 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7296 MACSTR, MAC2STR(peer));
7297 p2p_unauthorize(global->p2p, peer);
7301 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7302 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7303 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7307 if (wpa_s->pending_pd_before_join) {
7308 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7309 wpa_s->pending_pd_before_join = 0;
7313 wpas_p2p_stop_find(wpa_s);
7315 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7316 if (wpa_s == global->p2p_group_formation &&
7317 (wpa_s->p2p_in_provisioning ||
7318 wpa_s->parent->pending_interface_type ==
7319 WPA_IF_P2P_CLIENT)) {
7320 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7321 "formation found - cancelling",
7324 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7325 wpa_s->parent, NULL);
7326 if (wpa_s->p2p_in_provisioning) {
7327 wpas_group_formation_completed(wpa_s, 0, 0);
7330 wpas_p2p_group_delete(wpa_s,
7331 P2P_GROUP_REMOVAL_REQUESTED);
7333 } else if (wpa_s->p2p_in_invitation) {
7334 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7337 wpas_p2p_group_formation_failed(wpa_s, 0);
7342 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7350 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7352 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7355 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7356 "being available anymore");
7357 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
7361 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7362 int freq_24, int freq_5, int freq_overall)
7364 struct p2p_data *p2p = wpa_s->global->p2p;
7367 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7371 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7374 struct p2p_data *p2p = wpa_s->global->p2p;
7379 if (hwaddr_aton(addr, peer))
7382 return p2p_unauthorize(p2p, peer);
7387 * wpas_p2p_disconnect - Disconnect from a P2P Group
7388 * @wpa_s: Pointer to wpa_supplicant data
7389 * Returns: 0 on success, -1 on failure
7391 * This can be used to disconnect from a group in which the local end is a P2P
7392 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7393 * virtual network interface was created for this group, that interface will be
7394 * removed. Otherwise, only the configured P2P group network will be removed
7395 * from the interface.
7397 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7403 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7408 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7412 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7415 ret = p2p_in_progress(wpa_s->global->p2p);
7418 * Check whether there is an ongoing WPS provisioning step (or
7419 * other parts of group formation) on another interface since
7420 * p2p_in_progress() does not report this to avoid issues for
7421 * scans during such provisioning step.
7423 if (wpa_s->global->p2p_group_formation &&
7424 wpa_s->global->p2p_group_formation != wpa_s) {
7425 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7426 "in group formation",
7427 wpa_s->global->p2p_group_formation->ifname);
7432 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
7433 struct os_reltime now;
7434 os_get_reltime(&now);
7435 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7436 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
7437 /* Wait for the first client has expired */
7438 wpa_s->global->p2p_go_wait_client.sec = 0;
7440 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7449 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7450 struct wpa_ssid *ssid)
7452 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7453 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7454 wpa_s->parent, NULL) > 0) {
7456 * Remove the network by scheduling the group formation
7457 * timeout to happen immediately. The teardown code
7458 * needs to be scheduled to run asynch later so that we
7459 * don't delete data from under ourselves unexpectedly.
7460 * Calling wpas_p2p_group_formation_timeout directly
7461 * causes a series of crashes in WPS failure scenarios.
7463 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7464 "P2P group network getting removed");
7465 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7466 wpa_s->parent, NULL);
7471 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
7472 const u8 *addr, const u8 *ssid,
7478 for (s = wpa_s->conf->ssid; s; s = s->next) {
7479 if (s->disabled != 2)
7482 (ssid_len != s->ssid_len ||
7483 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7486 if (s->mode == WPAS_MODE_P2P_GO)
7490 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7491 return s; /* peer is GO in the persistent group */
7492 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7494 for (i = 0; i < s->num_p2p_clients; i++) {
7495 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
7496 addr, ETH_ALEN) == 0)
7497 return s; /* peer is P2P client in persistent
7506 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7509 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7510 wpa_s->parent, NULL) > 0) {
7512 * This can happen if WPS provisioning step is not terminated
7513 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7514 * peer was able to connect, there is no need to time out group
7515 * formation after this, though. In addition, this is used with
7516 * the initial connection wait on the GO as a separate formation
7517 * timeout and as such, expected to be hit after the initial WPS
7518 * provisioning step.
7520 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7522 if (!wpa_s->p2p_go_group_formation_completed &&
7523 !wpa_s->group_formation_reported) {
7525 * GO has not yet notified group formation success since
7526 * the WPS step was not completed cleanly. Do that
7527 * notification now since the P2P Client was able to
7528 * connect and as such, must have received the
7529 * credential from the WPS step.
7531 if (wpa_s->global->p2p)
7532 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7533 wpas_group_formation_completed(wpa_s, 1, 0);
7536 if (!wpa_s->p2p_go_group_formation_completed) {
7537 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7538 wpa_s->p2p_go_group_formation_completed = 1;
7539 wpa_s->global->p2p_group_formation = NULL;
7540 wpa_s->p2p_in_provisioning = 0;
7541 wpa_s->p2p_in_invitation = 0;
7543 wpa_s->global->p2p_go_wait_client.sec = 0;
7546 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7550 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7553 struct wpa_supplicant *group = wpa_s;
7556 if (wpa_s->global->p2p_group_formation)
7557 group = wpa_s->global->p2p_group_formation;
7558 wpa_s = wpa_s->global->p2p_init_wpa_s;
7559 offchannel_send_action_done(wpa_s);
7561 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7562 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7563 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7564 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7565 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7566 wpa_s->p2p_persistent_id,
7567 wpa_s->p2p_pd_before_go_neg,
7574 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7578 if (!wpa_s->p2p_fallback_to_go_neg ||
7579 wpa_s->p2p_in_provisioning <= 5)
7582 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7583 return 0; /* peer operating as a GO */
7585 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7586 "fallback to GO Negotiation");
7587 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7588 "reason=GO-not-found");
7589 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7591 return res == 1 ? 2 : 1;
7595 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7597 struct wpa_supplicant *ifs;
7599 if (wpa_s->wpa_state > WPA_SCANNING) {
7600 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7601 "concurrent operation",
7602 wpa_s->conf->p2p_search_delay);
7603 return wpa_s->conf->p2p_search_delay;
7606 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7608 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7609 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7610 "delay due to concurrent operation on "
7612 wpa_s->conf->p2p_search_delay,
7614 return wpa_s->conf->p2p_search_delay;
7622 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7623 struct wpa_ssid *s, const u8 *addr,
7626 struct psk_list_entry *psk, *tmp;
7629 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7631 if ((iface_addr && !psk->p2p &&
7632 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7633 (!iface_addr && psk->p2p &&
7634 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7635 wpa_dbg(wpa_s, MSG_DEBUG,
7636 "P2P: Remove persistent group PSK list entry for "
7638 MAC2STR(psk->addr), psk->p2p);
7639 dl_list_del(&psk->list);
7649 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7650 const u8 *p2p_dev_addr,
7651 const u8 *psk, size_t psk_len)
7653 struct wpa_ssid *ssid = wpa_s->current_ssid;
7654 struct wpa_ssid *persistent;
7655 struct psk_list_entry *p, *last;
7657 if (psk_len != sizeof(p->psk))
7661 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7662 " p2p_dev_addr=" MACSTR,
7663 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7664 if (is_zero_ether_addr(p2p_dev_addr))
7665 p2p_dev_addr = NULL;
7667 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7671 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7672 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7673 /* To be added to persistent group once created */
7674 if (wpa_s->global->add_psk == NULL) {
7675 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7676 if (wpa_s->global->add_psk == NULL)
7679 p = wpa_s->global->add_psk;
7682 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7685 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7687 os_memcpy(p->psk, psk, psk_len);
7691 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7692 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7696 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7699 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7703 p = os_zalloc(sizeof(*p));
7708 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7711 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7713 os_memcpy(p->psk, psk, psk_len);
7715 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7716 (last = dl_list_last(&persistent->psk_list,
7717 struct psk_list_entry, list))) {
7718 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7719 MACSTR " (p2p=%u) to make room for a new one",
7720 MAC2STR(last->addr), last->p2p);
7721 dl_list_del(&last->list);
7725 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7726 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7727 p2p_dev_addr == NULL);
7729 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7730 MACSTR, MAC2STR(p2p_dev_addr));
7732 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7735 dl_list_add(&persistent->psk_list, &p->list);
7737 if (wpa_s->parent->conf->update_config &&
7738 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7739 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7743 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7744 struct wpa_ssid *s, const u8 *addr,
7749 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7750 if (res > 0 && wpa_s->conf->update_config &&
7751 wpa_config_write(wpa_s->confname, wpa_s->conf))
7752 wpa_dbg(wpa_s, MSG_DEBUG,
7753 "P2P: Failed to update configuration");
7757 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7758 const u8 *peer, int iface_addr)
7760 struct hostapd_data *hapd;
7761 struct hostapd_wpa_psk *psk, *prev, *rem;
7762 struct sta_info *sta;
7764 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7765 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7768 /* Remove per-station PSK entry */
7769 hapd = wpa_s->ap_iface->bss[0];
7771 psk = hapd->conf->ssid.wpa_psk;
7773 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7775 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7776 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7777 MACSTR " iface_addr=%d",
7778 MAC2STR(peer), iface_addr);
7780 prev->next = psk->next;
7782 hapd->conf->ssid.wpa_psk = psk->next;
7792 /* Disconnect from group */
7794 sta = ap_get_sta(hapd, peer);
7796 sta = ap_get_sta_p2p(hapd, peer);
7798 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7799 " (iface_addr=%d) from group",
7800 MAC2STR(peer), iface_addr);
7801 hostapd_drv_sta_deauth(hapd, sta->addr,
7802 WLAN_REASON_DEAUTH_LEAVING);
7803 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7808 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7812 struct wpa_supplicant *w;
7813 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
7815 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7817 /* Remove from any persistent group */
7818 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
7819 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7822 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7823 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
7826 /* Remove from any operating group */
7827 for (w = wpa_s->global->ifaces; w; w = w->next)
7828 wpas_p2p_remove_client_go(w, peer, iface_addr);
7832 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7834 struct wpa_supplicant *wpa_s = eloop_ctx;
7835 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7839 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7841 struct wpa_supplicant *wpa_s = eloop_ctx;
7843 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7844 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7848 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7849 struct wpa_ssid *ssid)
7851 struct wpa_supplicant *iface;
7853 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7854 if (!iface->current_ssid ||
7855 iface->current_ssid->frequency == freq ||
7856 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7857 !iface->current_ssid->p2p_group))
7860 /* Remove the connection with least priority */
7861 if (!wpas_is_p2p_prioritized(iface)) {
7862 /* STA connection has priority over existing
7863 * P2P connection, so remove the interface. */
7864 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7865 eloop_register_timeout(0, 0,
7866 wpas_p2p_group_freq_conflict,
7868 /* If connection in progress is P2P connection, do not
7869 * proceed for the connection. */
7875 /* P2P connection has priority, disable the STA network
7877 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7879 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7880 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7881 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7883 /* If P2P connection is in progress, continue
7896 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7898 struct wpa_ssid *ssid = wpa_s->current_ssid;
7900 if (ssid == NULL || !ssid->p2p_group)
7903 if (wpa_s->p2p_last_4way_hs_fail &&
7904 wpa_s->p2p_last_4way_hs_fail == ssid) {
7905 u8 go_dev_addr[ETH_ALEN];
7906 struct wpa_ssid *persistent;
7908 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7910 ssid->ssid_len) <= 0) {
7911 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7915 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7916 MACSTR, MAC2STR(go_dev_addr));
7917 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7920 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7921 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7924 wpa_msg_global(wpa_s->parent, MSG_INFO,
7925 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7928 wpa_s->p2p_last_4way_hs_fail = NULL;
7930 * Remove the group from a timeout to avoid issues with caller
7931 * continuing to use the interface if this is on a P2P group
7934 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7939 wpa_s->p2p_last_4way_hs_fail = ssid;
7944 #ifdef CONFIG_WPS_NFC
7946 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7954 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7958 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7959 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7966 wpabuf_put_be16(ret, wsc_len);
7968 wpabuf_put_buf(ret, wsc);
7969 wpabuf_put_be16(ret, wpabuf_len(p2p));
7970 wpabuf_put_buf(ret, p2p);
7974 wpa_hexdump_buf(MSG_DEBUG,
7975 "P2P: Generated NFC connection handover message", ret);
7979 tmp = ndef_build_p2p(ret);
7982 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7992 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7993 struct wpa_ssid **ssid, u8 *go_dev_addr)
7995 struct wpa_supplicant *iface;
7998 os_memset(go_dev_addr, 0, ETH_ALEN);
8001 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8002 if (iface->wpa_state < WPA_ASSOCIATING ||
8003 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8004 !iface->current_ssid->p2p_group ||
8005 iface->current_ssid->mode != WPAS_MODE_INFRA)
8008 *ssid = iface->current_ssid;
8010 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8011 return iface->assoc_freq;
8017 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8020 struct wpabuf *wsc, *p2p;
8021 struct wpa_ssid *ssid;
8022 u8 go_dev_addr[ETH_ALEN];
8023 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8025 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8026 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8030 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8031 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8032 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8033 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8037 if (cli_freq == 0) {
8038 wsc = wps_build_nfc_handover_req_p2p(
8039 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8042 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8043 go_dev_addr, ssid ? ssid->ssid : NULL,
8044 ssid ? ssid->ssid_len : 0);
8046 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8050 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8053 struct wpabuf *wsc, *p2p;
8054 struct wpa_ssid *ssid;
8055 u8 go_dev_addr[ETH_ALEN];
8056 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8058 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8061 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8062 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8063 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8066 if (cli_freq == 0) {
8067 wsc = wps_build_nfc_handover_sel_p2p(
8069 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8070 DEV_PW_NFC_CONNECTION_HANDOVER,
8071 wpa_s->conf->wps_nfc_dh_pubkey,
8072 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8075 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8076 go_dev_addr, ssid ? ssid->ssid : NULL,
8077 ssid ? ssid->ssid_len : 0);
8079 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8083 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8084 struct p2p_nfc_params *params)
8086 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8087 "connection handover (freq=%d)",
8090 if (params->go_freq && params->go_ssid_len) {
8091 wpa_s->p2p_wps_method = WPS_NFC;
8092 wpa_s->pending_join_wps_method = WPS_NFC;
8093 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8094 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8096 return wpas_p2p_join_start(wpa_s, params->go_freq,
8098 params->go_ssid_len);
8101 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8102 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
8103 params->go_freq, -1, 0, 1, 1);
8107 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8108 struct p2p_nfc_params *params, int tag)
8110 int res, persistent;
8111 struct wpa_ssid *ssid;
8113 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8114 "connection handover");
8115 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8116 ssid = wpa_s->current_ssid;
8119 if (ssid->mode != WPAS_MODE_P2P_GO)
8121 if (wpa_s->ap_iface == NULL)
8125 if (wpa_s == NULL) {
8126 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8130 if (wpa_s->parent->p2p_oob_dev_pw_id !=
8131 DEV_PW_NFC_CONNECTION_HANDOVER &&
8132 !wpa_s->parent->p2p_oob_dev_pw) {
8133 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8136 res = wpas_ap_wps_add_nfc_pw(
8137 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
8138 wpa_s->parent->p2p_oob_dev_pw,
8139 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
8140 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
8145 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8149 if (!params->peer ||
8150 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8153 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8154 " to join", MAC2STR(params->peer->p2p_device_addr));
8156 wpa_s->global->p2p_invite_group = wpa_s;
8157 persistent = ssid->p2p_persistent_group &&
8158 wpas_p2p_get_persistent(wpa_s->parent,
8159 params->peer->p2p_device_addr,
8160 ssid->ssid, ssid->ssid_len);
8161 wpa_s->parent->pending_invite_ssid_id = -1;
8163 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8164 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8165 ssid->ssid, ssid->ssid_len, ssid->frequency,
8166 wpa_s->global->p2p_dev_addr, persistent, 0,
8167 wpa_s->parent->p2p_oob_dev_pw_id);
8171 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8172 struct p2p_nfc_params *params,
8175 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8176 "connection handover");
8177 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8178 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8179 forced_freq, -1, 0, 1, 1);
8183 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8184 struct p2p_nfc_params *params,
8189 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8190 "connection handover");
8191 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8192 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8193 forced_freq, -1, 0, 1, 1);
8197 res = wpas_p2p_listen(wpa_s, 60);
8199 p2p_unauthorize(wpa_s->global->p2p,
8200 params->peer->p2p_device_addr);
8207 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8208 const struct wpabuf *data,
8209 int sel, int tag, int forced_freq)
8211 const u8 *pos, *end;
8213 struct p2p_nfc_params params;
8216 os_memset(¶ms, 0, sizeof(params));
8219 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8221 pos = wpabuf_head(data);
8222 end = pos + wpabuf_len(data);
8224 if (end - pos < 2) {
8225 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8229 len = WPA_GET_BE16(pos);
8231 if (len > end - pos) {
8232 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8236 params.wsc_attr = pos;
8237 params.wsc_len = len;
8240 if (end - pos < 2) {
8241 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8245 len = WPA_GET_BE16(pos);
8247 if (len > end - pos) {
8248 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8252 params.p2p_attr = pos;
8253 params.p2p_len = len;
8256 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8257 params.wsc_attr, params.wsc_len);
8258 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8259 params.p2p_attr, params.p2p_len);
8261 wpa_hexdump(MSG_DEBUG,
8262 "P2P: Ignored extra data after P2P attributes",
8266 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
8270 if (params.next_step == NO_ACTION)
8273 if (params.next_step == BOTH_GO) {
8274 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8275 MAC2STR(params.peer->p2p_device_addr));
8279 if (params.next_step == PEER_CLIENT) {
8280 if (!is_zero_ether_addr(params.go_dev_addr)) {
8281 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8282 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8284 MAC2STR(params.peer->p2p_device_addr),
8286 MAC2STR(params.go_dev_addr),
8287 wpa_ssid_txt(params.go_ssid,
8288 params.go_ssid_len));
8290 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8291 "peer=" MACSTR " freq=%d",
8292 MAC2STR(params.peer->p2p_device_addr),
8298 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8299 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8300 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8304 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8305 wpa_s->p2p_oob_dev_pw = NULL;
8307 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8308 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8313 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8314 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8315 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8316 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8317 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8318 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8319 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8323 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8324 "peer OOB Device Password Id %u", id);
8327 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8328 "Device Password Id %u", id);
8329 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8330 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8331 params.oob_dev_pw_len -
8332 WPS_OOB_PUBKEY_HASH_LEN - 2);
8333 wpa_s->p2p_oob_dev_pw_id = id;
8334 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8335 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8336 params.oob_dev_pw_len -
8337 WPS_OOB_PUBKEY_HASH_LEN - 2);
8338 if (wpa_s->p2p_oob_dev_pw == NULL)
8341 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8342 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8343 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8346 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8347 "without Device Password");
8348 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8351 switch (params.next_step) {
8355 /* already covered above */
8358 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
8360 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
8362 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
8364 /* TODO: use own OOB Dev Pw */
8365 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
8372 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8373 const struct wpabuf *data, int forced_freq)
8375 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8378 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8382 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8383 const struct wpabuf *req,
8384 const struct wpabuf *sel, int forced_freq)
8389 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8392 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8394 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8395 wpabuf_head(req), wpabuf_len(req));
8396 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8397 wpabuf_head(sel), wpabuf_len(sel));
8399 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8400 tmp = ndef_parse_p2p(init ? sel : req);
8402 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8406 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8414 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8417 int go_intent = wpa_s->conf->p2p_go_intent;
8418 struct wpa_supplicant *iface;
8420 if (wpa_s->global->p2p == NULL)
8424 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8425 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8427 if (!iface->ap_iface)
8429 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8431 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8433 if (wpa_s->p2p_nfc_tag_enabled)
8434 wpas_p2p_remove_pending_group_interface(wpa_s);
8435 wpa_s->p2p_nfc_tag_enabled = 0;
8439 if (wpa_s->global->p2p_disabled)
8442 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8443 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8444 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8445 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8446 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8447 "to allow static handover cases");
8451 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8453 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8454 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8455 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8456 if (wpa_s->p2p_oob_dev_pw == NULL)
8458 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8460 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8461 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8463 * P2P Group Interface present and the command came on group
8464 * interface, so enable the token for the current interface.
8466 wpa_s->create_p2p_iface = 0;
8468 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8471 if (wpa_s->create_p2p_iface) {
8472 enum wpa_driver_if_type iftype;
8473 /* Prepare to add a new interface for the group */
8474 iftype = WPA_IF_P2P_GROUP;
8475 if (go_intent == 15)
8476 iftype = WPA_IF_P2P_GO;
8477 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8478 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8479 "interface for the group");
8483 if_addr = wpa_s->pending_interface_addr;
8485 if_addr = wpa_s->own_addr;
8487 wpa_s->p2p_nfc_tag_enabled = enabled;
8489 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8490 struct hostapd_data *hapd;
8491 if (iface->ap_iface == NULL)
8493 hapd = iface->ap_iface->bss[0];
8494 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8495 hapd->conf->wps_nfc_dh_pubkey =
8496 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8497 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8498 hapd->conf->wps_nfc_dh_privkey =
8499 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8500 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8501 hapd->conf->wps_nfc_dev_pw =
8502 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8503 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8505 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8506 wpa_dbg(iface, MSG_DEBUG,
8507 "P2P: Failed to enable NFC Tag for GO");
8510 p2p_set_authorized_oob_dev_pw_id(
8511 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8517 #endif /* CONFIG_WPS_NFC */
8520 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8521 struct wpa_used_freq_data *freqs,
8524 u8 curr_chan, cand, chan;
8528 * If possible, optimize the Listen channel to be a channel that is
8529 * already used by one of the other interfaces.
8531 if (!wpa_s->conf->p2p_optimize_listen_chan)
8534 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8537 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8538 for (i = 0, cand = 0; i < num; i++) {
8539 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8540 if (curr_chan == chan) {
8545 if (chan == 1 || chan == 6 || chan == 11)
8550 wpa_dbg(wpa_s, MSG_DEBUG,
8551 "P2P: Update Listen channel to %u based on operating channel",
8553 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8558 static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
8560 struct hostapd_config *conf;
8561 struct p2p_go_neg_results params;
8562 struct csa_settings csa_settings;
8563 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8564 int old_freq = current_ssid->frequency;
8567 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8568 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8573 * TODO: This function may not always work correctly. For example,
8574 * when we have a running GO and a BSS on a DFS channel.
8576 if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, NULL)) {
8577 wpa_dbg(wpa_s, MSG_DEBUG,
8578 "P2P CSA: Failed to select new frequency for GO");
8582 if (current_ssid->frequency == params.freq) {
8583 wpa_dbg(wpa_s, MSG_DEBUG,
8584 "P2P CSA: Selected same frequency - not moving GO");
8588 conf = hostapd_config_defaults();
8590 wpa_dbg(wpa_s, MSG_DEBUG,
8591 "P2P CSA: Failed to allocate default config");
8595 current_ssid->frequency = params.freq;
8596 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
8597 wpa_dbg(wpa_s, MSG_DEBUG,
8598 "P2P CSA: Failed to create new GO config");
8603 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
8604 wpa_dbg(wpa_s, MSG_DEBUG,
8605 "P2P CSA: CSA to a different band is not supported");
8610 os_memset(&csa_settings, 0, sizeof(csa_settings));
8611 csa_settings.cs_count = P2P_GO_CSA_COUNT;
8612 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
8613 csa_settings.freq_params.freq = params.freq;
8614 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
8615 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
8616 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
8618 if (conf->ieee80211ac) {
8619 int freq1 = 0, freq2 = 0;
8622 if (ieee80211_freq_to_channel_ext(params.freq,
8623 conf->secondary_channel,
8624 conf->vht_oper_chwidth,
8626 NUM_HOSTAPD_MODES) {
8627 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
8632 if (conf->vht_oper_centr_freq_seg0_idx)
8633 freq1 = ieee80211_chan_to_freq(
8635 conf->vht_oper_centr_freq_seg0_idx);
8637 if (conf->vht_oper_centr_freq_seg1_idx)
8638 freq2 = ieee80211_chan_to_freq(
8640 conf->vht_oper_centr_freq_seg1_idx);
8642 if (freq1 < 0 || freq2 < 0) {
8643 wpa_dbg(wpa_s, MSG_DEBUG,
8644 "P2P CSA: Selected invalid VHT center freqs");
8649 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
8650 csa_settings.freq_params.center_freq1 = freq1;
8651 csa_settings.freq_params.center_freq2 = freq2;
8653 switch (conf->vht_oper_chwidth) {
8654 case VHT_CHANWIDTH_80MHZ:
8655 case VHT_CHANWIDTH_80P80MHZ:
8656 csa_settings.freq_params.bandwidth = 80;
8658 case VHT_CHANWIDTH_160MHZ:
8659 csa_settings.freq_params.bandwidth = 160;
8664 ret = ap_switch_channel(wpa_s, &csa_settings);
8666 current_ssid->frequency = old_freq;
8667 hostapd_config_free(conf);
8672 static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
8674 struct p2p_go_neg_results params;
8675 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8677 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
8679 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
8680 current_ssid->frequency);
8682 /* Stop the AP functionality */
8683 /* TODO: Should do this in a way that does not indicated to possible
8684 * P2P Clients in the group that the group is terminated. */
8685 wpa_supplicant_ap_deinit(wpa_s);
8687 /* Reselect the GO frequency */
8688 if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, NULL)) {
8689 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
8690 wpas_p2p_group_delete(wpa_s,
8691 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8694 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
8698 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
8699 wpa_printf(MSG_DEBUG,
8700 "P2P: Selected freq (%u MHz) is not valid for P2P",
8702 wpas_p2p_group_delete(wpa_s,
8703 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8707 /* Update the frequency */
8708 current_ssid->frequency = params.freq;
8709 wpa_s->connect_without_scan = current_ssid;
8710 wpa_s->reassociate = 1;
8711 wpa_s->disconnected = 0;
8712 wpa_supplicant_req_scan(wpa_s, 0, 0);
8716 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
8718 struct wpa_supplicant *wpa_s = eloop_ctx;
8720 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
8723 wpas_p2p_go_update_common_freqs(wpa_s);
8725 /* Do not move GO in the middle of a CSA */
8726 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
8727 wpa_printf(MSG_DEBUG,
8728 "P2P: CSA is in progress - not moving GO");
8733 * First, try a channel switch flow. If it is not supported or fails,
8734 * take down the GO and bring it up again.
8736 if (wpas_p2p_move_go_csa(wpa_s) < 0)
8737 wpas_p2p_move_go_no_csa(wpa_s);
8741 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
8743 struct wpa_supplicant *wpa_s = eloop_ctx;
8744 struct wpa_used_freq_data *freqs = NULL;
8745 unsigned int num = wpa_s->num_multichan_concurrent;
8747 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8751 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8753 /* Previous attempt to move a GO was not possible -- try again. */
8754 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
8755 WPAS_P2P_CHANNEL_UPDATE_ANY);
8762 * Consider moving a GO from its currently used frequency:
8763 * 1. It is possible that due to regulatory consideration the frequency
8764 * can no longer be used and there is a need to evacuate the GO.
8765 * 2. It is possible that due to MCC considerations, it would be preferable
8766 * to move the GO to a channel that is currently used by some other
8767 * station interface.
8769 * In case a frequency that became invalid is once again valid, cancel a
8770 * previously initiated GO frequency change.
8772 static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
8773 struct wpa_used_freq_data *freqs,
8776 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
8777 unsigned int timeout;
8780 wpas_p2p_go_update_common_freqs(wpa_s);
8782 freq = wpa_s->current_ssid->frequency;
8783 for (i = 0, invalid_freq = 0; i < num; i++) {
8784 if (freqs[i].freq == freq) {
8785 flags = freqs[i].flags;
8787 /* The channel is invalid, must change it */
8788 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
8789 wpa_dbg(wpa_s, MSG_DEBUG,
8790 "P2P: Freq=%d MHz no longer valid for GO",
8794 } else if (freqs[i].flags == 0) {
8795 /* Freq is not used by any other station interface */
8797 } else if (!p2p_supported_freq(wpa_s->global->p2p,
8799 /* Freq is not valid for P2P use cases */
8801 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8802 P2P_GO_FREQ_MOVE_SCM) {
8804 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8805 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
8806 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
8808 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
8809 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
8810 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
8811 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
8813 } else if ((wpa_s->drv_flags &
8814 WPA_DRIVER_FLAGS_AP_CSA) &&
8815 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
8819 * We do not support CSA between bands, so move
8820 * GO only within the same band.
8822 if (wpa_s->ap_iface->current_mode->mode ==
8823 ieee80211_freq_to_chan(freqs[i].freq,
8830 wpa_dbg(wpa_s, MSG_DEBUG,
8831 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
8832 invalid_freq, policy_move, flags);
8835 * The channel is valid, or we are going to have a policy move, so
8838 if (!invalid_freq || policy_move) {
8839 wpa_dbg(wpa_s, MSG_DEBUG,
8840 "P2P: Cancel a GO move from freq=%d MHz", freq);
8841 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8843 if (wpas_p2p_in_progress(wpa_s)) {
8844 wpa_dbg(wpa_s, MSG_DEBUG,
8845 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
8846 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
8848 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
8849 wpas_p2p_reconsider_moving_go,
8855 if (!invalid_freq && (!policy_move || flags != 0)) {
8856 wpa_dbg(wpa_s, MSG_DEBUG,
8857 "P2P: Not initiating a GO frequency change");
8862 * Do not consider moving GO if it is in the middle of a CSA. When the
8863 * CSA is finished this flow should be retriggered.
8865 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
8866 wpa_dbg(wpa_s, MSG_DEBUG,
8867 "P2P: Not initiating a GO frequency change - CSA is in progress");
8871 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
8872 timeout = P2P_GO_FREQ_CHANGE_TIME;
8876 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
8878 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8879 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
8883 static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
8884 struct wpa_used_freq_data *freqs,
8886 enum wpas_p2p_channel_update_trig trig)
8888 struct wpa_supplicant *ifs;
8890 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
8894 * Travers all the radio interfaces, and for each GO interface, check
8895 * if there is a need to move the GO from the frequency it is using,
8896 * or in case the frequency is valid again, cancel the evacuation flow.
8898 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8900 if (ifs->current_ssid == NULL ||
8901 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
8905 * The GO was just started or completed channel switch, no need
8909 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
8910 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
8911 wpa_dbg(wpa_s, MSG_DEBUG,
8912 "P2P: GO move - schedule re-consideration");
8913 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
8914 wpas_p2p_reconsider_moving_go,
8919 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
8924 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8926 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8929 wpas_p2p_update_channel_list(wpa_s,
8930 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
8934 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8936 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8937 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8938 "the management interface is being removed");
8939 wpas_p2p_deinit_global(wpa_s->global);
8944 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8946 if (wpa_s->ap_iface->bss)
8947 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8948 wpas_p2p_group_deinit(wpa_s);