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
56 #ifndef P2P_MAX_CLIENT_IDLE
58 * How many seconds to try to reconnect to the GO when connection in P2P client
61 #define P2P_MAX_CLIENT_IDLE 10
62 #endif /* P2P_MAX_CLIENT_IDLE */
64 #ifndef P2P_MAX_INITIAL_CONN_WAIT
66 * How many seconds to wait for initial 4-way handshake to get completed after
67 * WPS provisioning step or after the re-invocation of a persistent group on a
70 #define P2P_MAX_INITIAL_CONN_WAIT 10
71 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
73 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
75 * How many seconds to wait for initial 4-way handshake to get completed after
76 * WPS provisioning step on the GO. This controls the extra time the P2P
77 * operation is considered to be in progress (e.g., to delay other scans) after
78 * WPS provisioning has been completed on the GO during group formation.
80 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
81 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
83 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
85 * How many seconds to wait for initial 4-way handshake to get completed after
86 * re-invocation of a persistent group on the GO when the client is expected
87 * to connect automatically (no user interaction).
89 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
90 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
92 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
94 enum p2p_group_removal_reason {
95 P2P_GROUP_REMOVAL_UNKNOWN,
96 P2P_GROUP_REMOVAL_SILENT,
97 P2P_GROUP_REMOVAL_FORMATION_FAILED,
98 P2P_GROUP_REMOVAL_REQUESTED,
99 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
100 P2P_GROUP_REMOVAL_UNAVAILABLE,
101 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
102 P2P_GROUP_REMOVAL_PSK_FAILURE,
103 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
104 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
108 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
109 static struct wpa_supplicant *
110 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
112 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
113 const u8 *ssid, size_t ssid_len);
114 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
115 const u8 *ssid, size_t ssid_len);
116 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
117 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
118 const u8 *dev_addr, enum p2p_wps_method wps_method,
119 int auto_join, int freq,
120 const u8 *ssid, size_t ssid_len);
121 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
122 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
123 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
124 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
125 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
127 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
128 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
130 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
131 static void wpas_stop_listen(void *ctx);
132 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
133 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
134 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
135 enum wpa_driver_if_type type);
136 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
137 int already_deleted);
138 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
139 struct wpa_used_freq_data *freqs,
141 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
142 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
143 static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
144 struct wpa_used_freq_data *freqs,
149 * Get the number of concurrent channels that the HW can operate, but that are
150 * currently not in use by any of the wpa_supplicant interfaces.
152 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
157 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
161 num = get_shared_radio_freqs(wpa_s, freqs,
162 wpa_s->num_multichan_concurrent);
165 unused = wpa_s->num_multichan_concurrent - num;
166 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
172 * Get the frequencies that are currently in use by one or more of the virtual
173 * interfaces, and that are also valid for P2P operation.
176 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
177 struct wpa_used_freq_data *p2p_freqs,
180 struct wpa_used_freq_data *freqs;
181 unsigned int num, i, j;
183 freqs = os_calloc(wpa_s->num_multichan_concurrent,
184 sizeof(struct wpa_used_freq_data));
188 num = get_shared_radio_freqs_data(wpa_s, freqs,
189 wpa_s->num_multichan_concurrent);
191 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
193 for (i = 0, j = 0; i < num && j < len; i++) {
194 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
195 p2p_freqs[j++] = freqs[i];
200 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
206 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
209 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
212 /* Use the wpa_s used to control the P2P Device operation */
213 wpa_s = wpa_s->global->p2p_init_wpa_s;
215 if (wpa_s->conf->p2p_ignore_shared_freq &&
216 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
217 wpas_p2p_num_unused_channels(wpa_s) > 0) {
218 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
222 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
226 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
227 struct wpa_scan_results *scan_res)
231 if (wpa_s->p2p_scan_work) {
232 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
233 wpa_s->p2p_scan_work = NULL;
234 radio_work_done(work);
237 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
240 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
241 (int) scan_res->num);
243 for (i = 0; i < scan_res->num; i++) {
244 struct wpa_scan_res *bss = scan_res->res[i];
245 struct os_reltime time_tmp_age, entry_ts;
249 time_tmp_age.sec = bss->age / 1000;
250 time_tmp_age.usec = (bss->age % 1000) * 1000;
251 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
253 ies = (const u8 *) (bss + 1);
254 ies_len = bss->ie_len;
255 if (bss->beacon_ie_len > 0 &&
256 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
257 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
258 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
259 MACSTR, MAC2STR(bss->bssid));
261 ies_len = bss->beacon_ie_len;
265 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
266 bss->freq, &entry_ts, bss->level,
271 p2p_scan_res_handled(wpa_s->global->p2p);
275 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
277 struct wpa_supplicant *wpa_s = work->wpa_s;
278 struct wpa_driver_scan_params *params = work->ctx;
282 if (!work->started) {
283 wpa_scan_free_params(params);
287 wpa_s->p2p_scan_work = NULL;
291 ret = wpa_drv_scan(wpa_s, params);
292 wpa_scan_free_params(params);
295 radio_work_done(work);
296 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
300 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
301 os_get_reltime(&wpa_s->scan_trigger_time);
302 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
303 wpa_s->own_scan_requested = 1;
304 wpa_s->p2p_scan_work = work;
308 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
311 if (wpa_s->global->p2p_24ghz_social_channels &&
312 (freq == 2412 || freq == 2437 || freq == 2462)) {
314 * Search all social channels regardless of whether these have
315 * been disabled for P2P operating channel use to avoid missing
320 return p2p_supported_freq(wpa_s->global->p2p, freq);
324 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
325 unsigned int num_req_dev_types,
326 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
328 struct wpa_supplicant *wpa_s = ctx;
329 struct wpa_driver_scan_params *params = NULL;
330 struct wpabuf *wps_ie, *ies;
331 unsigned int num_channels = 0;
332 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
336 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
339 if (wpa_s->p2p_scan_work) {
340 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
344 params = os_zalloc(sizeof(*params));
348 /* P2P Wildcard SSID */
349 params->num_ssids = 1;
350 n = os_malloc(P2P_WILDCARD_SSID_LEN);
353 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
354 params->ssids[0].ssid = n;
355 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
357 wpa_s->wps->dev.p2p = 1;
358 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
359 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
360 num_req_dev_types, req_dev_types);
364 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
365 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
370 wpabuf_put_buf(ies, wps_ie);
373 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
375 params->p2p_probe = 1;
376 n = os_malloc(wpabuf_len(ies));
381 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
382 params->extra_ies = n;
383 params->extra_ies_len = wpabuf_len(ies);
387 case P2P_SCAN_SOCIAL:
388 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
390 if (params->freqs == NULL)
392 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
393 if (wpas_p2p_search_social_channel(
394 wpa_s, social_channels_freq[i]))
395 params->freqs[num_channels++] =
396 social_channels_freq[i];
398 params->freqs[num_channels++] = 0;
402 case P2P_SCAN_SPECIFIC:
403 params->freqs = os_calloc(2, sizeof(int));
404 if (params->freqs == NULL)
406 params->freqs[0] = freq;
407 params->freqs[1] = 0;
409 case P2P_SCAN_SOCIAL_PLUS_ONE:
410 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
412 if (params->freqs == NULL)
414 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
415 if (wpas_p2p_search_social_channel(
416 wpa_s, social_channels_freq[i]))
417 params->freqs[num_channels++] =
418 social_channels_freq[i];
420 if (p2p_supported_freq(wpa_s->global->p2p, freq))
421 params->freqs[num_channels++] = freq;
422 params->freqs[num_channels++] = 0;
426 radio_remove_works(wpa_s, "p2p-scan", 0);
427 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
433 wpa_scan_free_params(params);
438 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
440 switch (p2p_group_interface) {
441 case P2P_GROUP_INTERFACE_PENDING:
442 return WPA_IF_P2P_GROUP;
443 case P2P_GROUP_INTERFACE_GO:
444 return WPA_IF_P2P_GO;
445 case P2P_GROUP_INTERFACE_CLIENT:
446 return WPA_IF_P2P_CLIENT;
449 return WPA_IF_P2P_GROUP;
453 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
455 size_t ssid_len, int *go)
459 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
460 for (s = wpa_s->conf->ssid; s; s = s->next) {
461 if (s->disabled != 0 || !s->p2p_group ||
462 s->ssid_len != ssid_len ||
463 os_memcmp(ssid, s->ssid, ssid_len) != 0)
465 if (s->mode == WPAS_MODE_P2P_GO &&
466 s != wpa_s->current_ssid)
469 *go = s->mode == WPAS_MODE_P2P_GO;
478 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
480 struct wpa_supplicant *wpa_s = eloop_ctx;
481 wpa_printf(MSG_DEBUG,
482 "P2P: Complete previously requested removal of %s",
484 wpas_p2p_disconnect(wpa_s);
488 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
489 struct wpa_supplicant *calling_wpa_s)
491 if (calling_wpa_s == wpa_s && wpa_s &&
492 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
494 * The calling wpa_s instance is going to be removed. Do that
495 * from an eloop callback to keep the instance available until
496 * the caller has returned. This my be needed, e.g., to provide
497 * control interface responses on the per-interface socket.
499 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
505 return wpas_p2p_disconnect(wpa_s);
509 /* Determine total number of clients in active groups where we are the GO */
510 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
512 unsigned int count = 0;
515 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
516 for (s = wpa_s->conf->ssid; s; s = s->next) {
517 wpa_printf(MSG_DEBUG,
518 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
519 wpa_s, s, s->disabled, s->p2p_group,
521 if (!s->disabled && s->p2p_group &&
522 s->mode == WPAS_MODE_P2P_GO) {
523 count += p2p_get_group_num_members(
533 /* Find an interface for a P2P group where we are the GO */
534 static struct wpa_supplicant *
535 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
537 struct wpa_supplicant *save = NULL;
543 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
544 for (s = wpa_s->conf->ssid; s; s = s->next) {
545 if (s->disabled || !s->p2p_group ||
546 s->mode != WPAS_MODE_P2P_GO)
549 /* Prefer a group with connected clients */
550 if (p2p_get_group_num_members(wpa_s->p2p_group))
556 /* No group with connected clients, so pick the one without (if any) */
561 /* Find an active P2P group where we are the GO */
562 static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
565 struct wpa_ssid *s, *empty = NULL;
570 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
571 for (s = wpa_s->conf->ssid; s; s = s->next) {
572 if (s->disabled || !s->p2p_group ||
573 s->mode != WPAS_MODE_P2P_GO)
576 os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
577 if (p2p_get_group_num_members(wpa_s->p2p_group))
587 /* Find a persistent group where we are the GO */
588 static struct wpa_ssid *
589 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
593 for (s = wpa_s->conf->ssid; s; s = s->next) {
594 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
602 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
604 struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
606 u8 conncap = P2PS_SETUP_NONE;
607 unsigned int owned_members = 0;
608 unsigned int owner = 0;
609 unsigned int client = 0;
610 struct wpa_supplicant *go_wpa_s;
611 struct wpa_ssid *persistent_go;
612 int p2p_no_group_iface;
614 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
617 * For non-concurrent capable devices:
618 * If persistent_go, then no new.
619 * If GO, then no client.
620 * If client, then no GO.
622 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
623 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
624 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
626 wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
627 go_wpa_s, persistent_go);
629 for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
630 tmp_wpa_s = tmp_wpa_s->next) {
631 for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
632 wpa_printf(MSG_DEBUG,
633 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
634 tmp_wpa_s, s, s->disabled,
635 s->p2p_group, s->mode);
636 if (!s->disabled && s->p2p_group) {
637 if (s->mode == WPAS_MODE_P2P_GO) {
639 p2p_get_group_num_members(
640 tmp_wpa_s->p2p_group);
648 /* If not concurrent, restrict our choices */
649 if (p2p_no_group_iface) {
650 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
653 return P2PS_SETUP_NONE;
656 if (role == P2PS_SETUP_CLIENT ||
657 incoming == P2PS_SETUP_GROUP_OWNER ||
658 p2p_client_limit_reached(go_wpa_s->p2p_group))
659 return P2PS_SETUP_NONE;
661 return P2PS_SETUP_GROUP_OWNER;
665 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
667 return P2PS_SETUP_GROUP_OWNER |
669 if (incoming == P2PS_SETUP_NEW) {
672 if (os_get_random(&r, sizeof(r)) < 0 ||
674 return P2PS_SETUP_CLIENT;
675 return P2PS_SETUP_GROUP_OWNER;
681 /* If a required role has been specified, handle it here */
682 if (role && role != P2PS_SETUP_NEW) {
684 case P2PS_SETUP_NONE:
686 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
687 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
691 case P2PS_SETUP_GROUP_OWNER:
693 * Must be a complimentary role - cannot be a client to
694 * more than one peer.
696 if (incoming == role || client)
697 return P2PS_SETUP_NONE;
699 return P2PS_SETUP_CLIENT;
701 case P2PS_SETUP_CLIENT:
702 /* Must be a complimentary role */
703 if (incoming != role) {
704 conncap = P2PS_SETUP_GROUP_OWNER;
709 return P2PS_SETUP_NONE;
714 * For now, we only will support ownership of one group, and being a
715 * client of one group. Therefore, if we have either an existing GO
716 * group, or an existing client group, we will not do a new GO
717 * negotiation, but rather try to re-use the existing groups.
720 case P2PS_SETUP_NONE:
723 conncap = P2PS_SETUP_GROUP_OWNER;
724 else if (!owned_members)
725 conncap = P2PS_SETUP_NEW;
726 else if (incoming == P2PS_SETUP_NONE)
727 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
729 conncap = P2PS_SETUP_CLIENT;
732 case P2PS_SETUP_CLIENT:
733 conncap = P2PS_SETUP_GROUP_OWNER;
736 case P2PS_SETUP_GROUP_OWNER:
738 conncap = P2PS_SETUP_CLIENT;
741 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
742 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
744 conncap = P2PS_SETUP_GROUP_OWNER;
748 if (os_get_random(&r, sizeof(r)) < 0 ||
750 conncap = P2PS_SETUP_CLIENT;
752 conncap = P2PS_SETUP_GROUP_OWNER;
757 return P2PS_SETUP_NONE;
761 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
762 (!incoming && (conncap & P2PS_SETUP_NEW))) {
763 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
764 conncap &= ~P2PS_SETUP_GROUP_OWNER;
765 wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
766 owner, owned_members, conncap);
768 s = wpas_p2p_get_persistent_go(wpa_s);
770 if (!s && !owner && p2p_no_group_iface) {
771 p2p_set_intended_addr(wpa_s->global->p2p,
773 } else if (!s && !owner) {
774 if (wpas_p2p_add_group_interface(wpa_s,
775 WPA_IF_P2P_GO) < 0) {
776 wpa_printf(MSG_ERROR,
777 "P2P: Failed to allocate a new interface for the group");
778 return P2PS_SETUP_NONE;
780 wpa_s->global->pending_group_iface_for_p2ps = 1;
781 p2p_set_intended_addr(wpa_s->global->p2p,
782 wpa_s->pending_interface_addr);
790 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
791 enum p2p_group_removal_reason removal_reason)
793 struct wpa_ssid *ssid;
797 ssid = wpa_s->current_ssid;
800 * The current SSID was not known, but there may still be a
801 * pending P2P group interface waiting for provisioning or a
802 * P2P group that is trying to reconnect.
804 ssid = wpa_s->conf->ssid;
806 if (ssid->p2p_group && ssid->disabled != 2)
811 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
813 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
818 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
820 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
821 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
822 wpa_s->reassociate = 0;
823 wpa_s->disconnected = 1;
828 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
829 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
831 if (os_strcmp(gtype, "client") == 0) {
832 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
833 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
835 wpa_printf(MSG_DEBUG,
836 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
837 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
838 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
843 if (wpa_s->cross_connect_in_use) {
844 wpa_s->cross_connect_in_use = 0;
845 wpa_msg_global(wpa_s->parent, MSG_INFO,
846 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
847 wpa_s->ifname, wpa_s->cross_connect_uplink);
849 switch (removal_reason) {
850 case P2P_GROUP_REMOVAL_REQUESTED:
851 reason = " reason=REQUESTED";
853 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
854 reason = " reason=FORMATION_FAILED";
856 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
857 reason = " reason=IDLE";
859 case P2P_GROUP_REMOVAL_UNAVAILABLE:
860 reason = " reason=UNAVAILABLE";
862 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
863 reason = " reason=GO_ENDING_SESSION";
865 case P2P_GROUP_REMOVAL_PSK_FAILURE:
866 reason = " reason=PSK_FAILURE";
868 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
869 reason = " reason=FREQ_CONFLICT";
875 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
876 wpa_msg_global(wpa_s->parent, MSG_INFO,
877 P2P_EVENT_GROUP_REMOVED "%s %s%s",
878 wpa_s->ifname, gtype, reason);
881 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
882 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
883 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
884 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
885 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
886 wpa_s->parent, NULL) > 0) {
887 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
889 wpa_s->p2p_in_provisioning = 0;
890 wpas_p2p_group_formation_failed(wpa_s, 1);
893 wpa_s->p2p_in_invitation = 0;
894 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
897 * Make sure wait for the first client does not remain active after the
898 * group has been removed.
900 wpa_s->global->p2p_go_wait_client.sec = 0;
902 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
903 struct wpa_global *global;
905 enum wpa_driver_if_type type;
906 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
908 global = wpa_s->global;
909 ifname = os_strdup(wpa_s->ifname);
910 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
911 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
912 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
913 wpa_s = global->ifaces;
915 wpa_drv_if_remove(wpa_s, type, ifname);
920 if (!wpa_s->p2p_go_group_formation_completed) {
921 wpa_s->global->p2p_group_formation = NULL;
922 wpa_s->p2p_in_provisioning = 0;
925 wpa_s->show_group_started = 0;
926 os_free(wpa_s->go_params);
927 wpa_s->go_params = NULL;
929 os_free(wpa_s->p2p_group_common_freqs);
930 wpa_s->p2p_group_common_freqs = NULL;
931 wpa_s->p2p_group_common_freqs_num = 0;
933 wpa_s->waiting_presence_resp = 0;
935 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
936 if (ssid && (ssid->p2p_group ||
937 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
938 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
940 if (ssid == wpa_s->current_ssid) {
941 wpa_sm_set_config(wpa_s->wpa, NULL);
942 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
943 wpa_s->current_ssid = NULL;
946 * Networks objects created during any P2P activities are not
947 * exposed out as they might/will confuse certain non-P2P aware
948 * applications since these network objects won't behave like
951 * Likewise, we don't send out network removed signals for such
954 wpa_config_remove_network(wpa_s->conf, id);
955 wpa_supplicant_clear_status(wpa_s);
956 wpa_supplicant_cancel_sched_scan(wpa_s);
958 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
962 wpa_supplicant_ap_deinit(wpa_s);
964 wpa_drv_deinit_p2p_cli(wpa_s);
966 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
972 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
974 const u8 *ssid, size_t ssid_len)
982 if (wpa_s->go_params)
983 bssid = wpa_s->go_params->peer_interface_addr;
985 bssid = wpa_s->bssid;
987 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
988 if (bss == NULL && wpa_s->go_params &&
989 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
990 bss = wpa_bss_get_p2p_dev_addr(
991 wpa_s, wpa_s->go_params->peer_device_addr);
993 u8 iface_addr[ETH_ALEN];
994 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
996 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
999 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1000 "group is persistent - BSS " MACSTR " not found",
1005 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1007 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1008 P2P_IE_VENDOR_TYPE);
1010 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1011 "group is persistent - BSS " MACSTR
1012 " did not include P2P IE", MAC2STR(bssid));
1013 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1014 (u8 *) (bss + 1), bss->ie_len);
1015 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1016 ((u8 *) bss + 1) + bss->ie_len,
1017 bss->beacon_ie_len);
1021 group_capab = p2p_get_group_capab(p2p);
1022 addr = p2p_get_go_dev_addr(p2p);
1023 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1024 "group_capab=0x%x", group_capab);
1026 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1027 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1030 os_memset(go_dev_addr, 0, ETH_ALEN);
1033 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1034 "go_dev_addr=" MACSTR,
1035 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1037 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1041 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1042 struct wpa_ssid *ssid,
1043 const u8 *go_dev_addr)
1048 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1049 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1050 for (s = wpa_s->conf->ssid; s; s = s->next) {
1051 if (s->disabled == 2 &&
1052 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1053 s->ssid_len == ssid->ssid_len &&
1054 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1059 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1061 if (ssid->passphrase && !s->passphrase)
1063 else if (ssid->passphrase && s->passphrase &&
1064 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1067 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1070 s = wpa_config_add_network(wpa_s->conf);
1075 * Instead of network_added we emit persistent_group_added
1076 * notification. Also to keep the defense checks in
1077 * persistent_group obj registration method, we set the
1078 * relevant flags in s to designate it as a persistent group.
1081 s->p2p_persistent_group = 1;
1082 wpas_notify_persistent_group_added(wpa_s, s);
1083 wpa_config_set_network_defaults(s);
1087 s->p2p_persistent_group = 1;
1090 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1091 s->mode = ssid->mode;
1092 s->auth_alg = WPA_AUTH_ALG_OPEN;
1093 s->key_mgmt = WPA_KEY_MGMT_PSK;
1094 s->proto = WPA_PROTO_RSN;
1095 s->pairwise_cipher = WPA_CIPHER_CCMP;
1097 if (ssid->passphrase) {
1098 os_free(s->passphrase);
1099 s->passphrase = os_strdup(ssid->passphrase);
1101 if (ssid->psk_set) {
1103 os_memcpy(s->psk, ssid->psk, 32);
1105 if (s->passphrase && !s->psk_set)
1106 wpa_config_update_psk(s);
1107 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1109 s->ssid = os_malloc(ssid->ssid_len);
1112 s->ssid_len = ssid->ssid_len;
1113 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1115 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1116 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1117 wpa_s->global->add_psk = NULL;
1121 if (changed && wpa_s->conf->update_config &&
1122 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1123 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1130 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1133 struct wpa_ssid *ssid, *s;
1137 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
1139 ssid = wpa_s->current_ssid;
1140 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1141 !ssid->p2p_persistent_group)
1144 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
1145 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1148 if (s->ssid_len == ssid->ssid_len &&
1149 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1156 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
1157 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
1161 if (i == s->num_p2p_clients - 1)
1162 return; /* already the most recent entry */
1164 /* move the entry to mark it most recent */
1165 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1166 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1167 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
1168 os_memcpy(s->p2p_client_list +
1169 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1171 os_memset(s->p2p_client_list +
1172 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1178 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1179 n = os_realloc_array(s->p2p_client_list,
1180 s->num_p2p_clients + 1, 2 * ETH_ALEN);
1183 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1185 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1187 s->p2p_client_list = n;
1188 s->num_p2p_clients++;
1189 } else if (!found && s->p2p_client_list) {
1190 /* Not enough room for an additional entry - drop the oldest
1192 os_memmove(s->p2p_client_list,
1193 s->p2p_client_list + 2 * ETH_ALEN,
1194 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1195 os_memcpy(s->p2p_client_list +
1196 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1198 os_memset(s->p2p_client_list +
1199 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1203 if (p2p_wpa_s->conf->update_config &&
1204 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
1205 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1209 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1210 int go, struct wpa_ssid *ssid, int freq,
1211 const u8 *psk, const char *passphrase,
1212 const u8 *go_dev_addr, int persistent,
1215 const char *ssid_txt;
1219 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1224 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1228 if (passphrase && passphrase[0] == '\0')
1232 * Include PSK/passphrase only in the control interface message and
1233 * leave it out from the debug log entry.
1235 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1236 P2P_EVENT_GROUP_STARTED
1237 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1239 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1240 psk ? " psk=" : "", psk_txt,
1241 passphrase ? " passphrase=\"" : "",
1242 passphrase ? passphrase : "",
1243 passphrase ? "\"" : "",
1244 MAC2STR(go_dev_addr),
1245 persistent ? " [PERSISTENT]" : "", extra);
1246 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1247 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1248 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1249 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1254 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1255 int success, int already_deleted)
1257 struct wpa_ssid *ssid;
1260 u8 go_dev_addr[ETH_ALEN];
1261 int network_id = -1;
1264 * This callback is likely called for the main interface. Update wpa_s
1265 * to use the group interface if a new interface was created for the
1268 if (wpa_s->global->p2p_group_formation)
1269 wpa_s = wpa_s->global->p2p_group_formation;
1270 if (wpa_s->p2p_go_group_formation_completed) {
1271 wpa_s->global->p2p_group_formation = NULL;
1272 wpa_s->p2p_in_provisioning = 0;
1274 wpa_s->p2p_in_invitation = 0;
1275 wpa_s->group_formation_reported = 1;
1278 wpa_msg_global(wpa_s->parent, MSG_INFO,
1279 P2P_EVENT_GROUP_FORMATION_FAILURE);
1280 if (already_deleted)
1282 wpas_p2p_group_delete(wpa_s,
1283 P2P_GROUP_REMOVAL_FORMATION_FAILED);
1287 wpa_msg_global(wpa_s->parent, MSG_INFO,
1288 P2P_EVENT_GROUP_FORMATION_SUCCESS);
1290 ssid = wpa_s->current_ssid;
1291 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1292 ssid->mode = WPAS_MODE_P2P_GO;
1293 p2p_group_notif_formation_done(wpa_s->p2p_group);
1294 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1299 client = ssid->mode == WPAS_MODE_INFRA;
1300 if (ssid->mode == WPAS_MODE_P2P_GO) {
1301 persistent = ssid->p2p_persistent_group;
1302 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1305 persistent = wpas_p2p_persistent_group(wpa_s,
1310 client = wpa_s->p2p_group_interface ==
1311 P2P_GROUP_INTERFACE_CLIENT;
1312 os_memset(go_dev_addr, 0, ETH_ALEN);
1315 wpa_s->show_group_started = 0;
1318 * Indicate event only after successfully completed 4-way
1319 * handshake, i.e., when the interface is ready for data
1322 wpa_s->show_group_started = 1;
1324 wpas_p2p_group_started(wpa_s, 1, ssid,
1325 ssid ? ssid->frequency : 0,
1326 ssid && ssid->passphrase == NULL &&
1327 ssid->psk_set ? ssid->psk : NULL,
1328 ssid ? ssid->passphrase : NULL,
1329 go_dev_addr, persistent, "");
1330 wpas_p2p_cross_connect_setup(wpa_s);
1331 wpas_p2p_set_group_idle_timeout(wpa_s);
1335 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1338 os_free(wpa_s->global->add_psk);
1339 wpa_s->global->add_psk = NULL;
1341 if (network_id < 0 && ssid)
1342 network_id = ssid->id;
1344 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1345 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1350 struct send_action_work {
1356 unsigned int wait_time;
1361 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1364 struct wpa_supplicant *wpa_s = eloop_ctx;
1366 if (!wpa_s->p2p_send_action_work)
1369 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1370 os_free(wpa_s->p2p_send_action_work->ctx);
1371 radio_work_done(wpa_s->p2p_send_action_work);
1372 wpa_s->p2p_send_action_work = NULL;
1376 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1378 if (wpa_s->p2p_send_action_work) {
1379 struct send_action_work *awork;
1380 awork = wpa_s->p2p_send_action_work->ctx;
1381 if (awork->wait_time == 0) {
1383 radio_work_done(wpa_s->p2p_send_action_work);
1384 wpa_s->p2p_send_action_work = NULL;
1387 * In theory, this should not be needed, but number of
1388 * places in the P2P code is still using non-zero wait
1389 * time for the last Action frame in the sequence and
1390 * some of these do not call send_action_done().
1392 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1394 eloop_register_timeout(
1395 0, awork->wait_time * 1000,
1396 wpas_p2p_send_action_work_timeout,
1403 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1405 const u8 *dst, const u8 *src,
1407 const u8 *data, size_t data_len,
1408 enum offchannel_send_action_result
1411 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1413 wpas_p2p_action_tx_clear(wpa_s);
1415 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1419 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1420 res = P2P_SEND_ACTION_SUCCESS;
1422 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1423 res = P2P_SEND_ACTION_NO_ACK;
1425 case OFFCHANNEL_SEND_ACTION_FAILED:
1426 res = P2P_SEND_ACTION_FAILED;
1430 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1432 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1433 wpa_s->pending_pd_before_join &&
1434 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1435 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1436 wpa_s->p2p_fallback_to_go_neg) {
1437 wpa_s->pending_pd_before_join = 0;
1438 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1439 "during p2p_connect-auto");
1440 wpa_msg_global(wpa_s->parent, MSG_INFO,
1441 P2P_EVENT_FALLBACK_TO_GO_NEG
1442 "reason=no-ACK-to-PD-Req");
1443 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1449 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1451 struct wpa_supplicant *wpa_s = work->wpa_s;
1452 struct send_action_work *awork = work->ctx;
1455 if (work->started) {
1456 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1458 wpa_s->p2p_send_action_work = NULL;
1459 offchannel_send_action_done(wpa_s);
1465 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1466 awork->bssid, awork->buf, awork->len,
1468 wpas_p2p_send_action_tx_status, 1) < 0) {
1470 radio_work_done(work);
1473 wpa_s->p2p_send_action_work = work;
1477 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1478 unsigned int freq, const u8 *dst,
1479 const u8 *src, const u8 *bssid, const u8 *buf,
1480 size_t len, unsigned int wait_time)
1482 struct send_action_work *awork;
1484 if (wpa_s->p2p_send_action_work) {
1485 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1489 awork = os_zalloc(sizeof(*awork) + len);
1494 os_memcpy(awork->dst, dst, ETH_ALEN);
1495 os_memcpy(awork->src, src, ETH_ALEN);
1496 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1498 awork->wait_time = wait_time;
1499 os_memcpy(awork->buf, buf, len);
1501 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1502 wpas_send_action_cb, awork) < 0) {
1511 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1512 const u8 *src, const u8 *bssid, const u8 *buf,
1513 size_t len, unsigned int wait_time)
1515 struct wpa_supplicant *wpa_s = ctx;
1516 int listen_freq = -1, send_freq = -1;
1518 if (wpa_s->p2p_listen_work)
1519 listen_freq = wpa_s->p2p_listen_work->freq;
1520 if (wpa_s->p2p_send_action_work)
1521 send_freq = wpa_s->p2p_send_action_work->freq;
1522 if (listen_freq != (int) freq && send_freq != (int) freq) {
1523 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1524 listen_freq, send_freq);
1525 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1529 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1530 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1532 wpas_p2p_send_action_tx_status, 1);
1536 static void wpas_send_action_done(void *ctx)
1538 struct wpa_supplicant *wpa_s = ctx;
1540 if (wpa_s->p2p_send_action_work) {
1541 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1543 os_free(wpa_s->p2p_send_action_work->ctx);
1544 radio_work_done(wpa_s->p2p_send_action_work);
1545 wpa_s->p2p_send_action_work = NULL;
1548 offchannel_send_action_done(wpa_s);
1552 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1553 struct p2p_go_neg_results *params)
1555 if (wpa_s->go_params == NULL) {
1556 wpa_s->go_params = os_malloc(sizeof(*params));
1557 if (wpa_s->go_params == NULL)
1560 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1565 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1566 struct p2p_go_neg_results *res)
1568 wpa_s->group_formation_reported = 0;
1569 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1570 " dev_addr " MACSTR " wps_method %d",
1571 MAC2STR(res->peer_interface_addr),
1572 MAC2STR(res->peer_device_addr), res->wps_method);
1573 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1574 res->ssid, res->ssid_len);
1575 wpa_supplicant_ap_deinit(wpa_s);
1576 wpas_copy_go_neg_results(wpa_s, res);
1577 if (res->wps_method == WPS_PBC) {
1578 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1579 #ifdef CONFIG_WPS_NFC
1580 } else if (res->wps_method == WPS_NFC) {
1581 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1582 res->peer_interface_addr,
1583 wpa_s->parent->p2p_oob_dev_pw,
1584 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1585 wpa_s->parent->p2p_oob_dev_pw_id ==
1586 DEV_PW_NFC_CONNECTION_HANDOVER ?
1587 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1590 #endif /* CONFIG_WPS_NFC */
1592 u16 dev_pw_id = DEV_PW_DEFAULT;
1593 if (wpa_s->p2p_wps_method == WPS_P2PS)
1594 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1595 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1596 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1597 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1598 wpa_s->p2p_pin, 1, dev_pw_id);
1603 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1604 struct wpa_ssid *ssid)
1606 struct wpa_ssid *persistent;
1607 struct psk_list_entry *psk;
1608 struct hostapd_data *hapd;
1610 if (!wpa_s->ap_iface)
1613 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1615 if (persistent == NULL)
1618 hapd = wpa_s->ap_iface->bss[0];
1620 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1622 struct hostapd_wpa_psk *hpsk;
1624 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1626 MAC2STR(psk->addr), psk->p2p);
1627 hpsk = os_zalloc(sizeof(*hpsk));
1630 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1632 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1634 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1635 hpsk->next = hapd->conf->ssid.wpa_psk;
1636 hapd->conf->ssid.wpa_psk = hpsk;
1641 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1645 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1646 wpa_s->p2p_group_common_freqs_num);
1648 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1649 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1650 i, wpa_s->p2p_group_common_freqs[i]);
1654 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1655 struct p2p_go_neg_results *params)
1657 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1659 wpa_s->p2p_group_common_freqs_num = 0;
1660 os_free(wpa_s->p2p_group_common_freqs);
1661 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1662 if (!wpa_s->p2p_group_common_freqs)
1665 for (i = 0; i < len; i++) {
1666 if (!wpa_s->go_params->freq_list[i])
1668 wpa_s->p2p_group_common_freqs[i] =
1669 wpa_s->go_params->freq_list[i];
1671 wpa_s->p2p_group_common_freqs_num = i;
1675 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1677 #ifndef CONFIG_NO_CONFIG_WRITE
1678 if (wpa_s->parent->conf->update_config &&
1679 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1680 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1681 #endif /* CONFIG_NO_CONFIG_WRITE */
1685 static void p2p_go_configured(void *ctx, void *data)
1687 struct wpa_supplicant *wpa_s = ctx;
1688 struct p2p_go_neg_results *params = data;
1689 struct wpa_ssid *ssid;
1690 int network_id = -1;
1692 p2p_go_save_group_common_freqs(wpa_s, params);
1693 p2p_go_dump_common_freqs(wpa_s);
1695 ssid = wpa_s->current_ssid;
1696 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1697 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1698 if (wpa_s->global->p2p_group_formation == wpa_s)
1699 wpa_s->global->p2p_group_formation = NULL;
1700 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1701 params->passphrase[0] == '\0' ?
1704 wpa_s->global->p2p_dev_addr,
1705 params->persistent_group, "");
1706 wpa_s->group_formation_reported = 1;
1708 if (wpa_s->parent->p2ps_join_addr_valid) {
1709 wpa_dbg(wpa_s, MSG_DEBUG,
1710 "P2PS: Setting default PIN for " MACSTR,
1711 MAC2STR(wpa_s->parent->p2ps_join_addr));
1712 wpa_supplicant_ap_wps_pin(wpa_s,
1713 wpa_s->parent->p2ps_join_addr,
1714 "12345670", NULL, 0, 0);
1715 wpa_s->parent->p2ps_join_addr_valid = 0;
1718 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1719 if (params->persistent_group) {
1720 network_id = wpas_p2p_store_persistent_group(
1721 wpa_s->parent, ssid,
1722 wpa_s->global->p2p_dev_addr);
1723 wpas_p2p_add_psk_list(wpa_s, ssid);
1726 network_id = ssid->id;
1727 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1728 wpas_p2p_cross_connect_setup(wpa_s);
1729 wpas_p2p_set_group_idle_timeout(wpa_s);
1731 if (wpa_s->p2p_first_connection_timeout) {
1732 wpa_dbg(wpa_s, MSG_DEBUG,
1733 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1734 wpa_s->p2p_first_connection_timeout);
1735 wpa_s->p2p_go_group_formation_completed = 0;
1736 wpa_s->global->p2p_group_formation = wpa_s;
1737 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1738 wpa_s->parent, NULL);
1739 eloop_register_timeout(
1740 wpa_s->p2p_first_connection_timeout, 0,
1741 wpas_p2p_group_formation_timeout,
1742 wpa_s->parent, NULL);
1748 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1749 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1750 params->peer_interface_addr)) {
1751 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1755 if (params->wps_method == WPS_PBC) {
1756 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1757 params->peer_device_addr);
1758 #ifdef CONFIG_WPS_NFC
1759 } else if (params->wps_method == WPS_NFC) {
1760 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1761 DEV_PW_NFC_CONNECTION_HANDOVER &&
1762 !wpa_s->parent->p2p_oob_dev_pw) {
1763 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1766 wpas_ap_wps_add_nfc_pw(
1767 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1768 wpa_s->parent->p2p_oob_dev_pw,
1769 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1770 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1771 #endif /* CONFIG_WPS_NFC */
1772 } else if (wpa_s->p2p_pin[0])
1773 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1774 wpa_s->p2p_pin, NULL, 0, 0);
1775 os_free(wpa_s->go_params);
1776 wpa_s->go_params = NULL;
1780 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1781 struct p2p_go_neg_results *params,
1782 int group_formation)
1784 struct wpa_ssid *ssid;
1786 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1787 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1788 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1793 ssid = wpa_config_add_network(wpa_s->conf);
1795 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1799 wpa_s->show_group_started = 0;
1800 wpa_s->p2p_go_group_formation_completed = 0;
1801 wpa_s->group_formation_reported = 0;
1802 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1804 wpa_config_set_network_defaults(ssid);
1805 ssid->temporary = 1;
1806 ssid->p2p_group = 1;
1807 ssid->p2p_persistent_group = params->persistent_group;
1808 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1810 ssid->frequency = params->freq;
1811 ssid->ht40 = params->ht40;
1812 ssid->vht = params->vht;
1813 ssid->ssid = os_zalloc(params->ssid_len + 1);
1815 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1816 ssid->ssid_len = params->ssid_len;
1818 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1819 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1820 ssid->proto = WPA_PROTO_RSN;
1821 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1822 ssid->group_cipher = WPA_CIPHER_CCMP;
1823 if (params->freq > 56160) {
1825 * Enable GCMP instead of CCMP as pairwise_cipher and
1826 * group_cipher in 60 GHz.
1828 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1829 ssid->group_cipher = WPA_CIPHER_GCMP;
1831 if (os_strlen(params->passphrase) > 0) {
1832 ssid->passphrase = os_strdup(params->passphrase);
1833 if (ssid->passphrase == NULL) {
1834 wpa_msg_global(wpa_s, MSG_ERROR,
1835 "P2P: Failed to copy passphrase for GO");
1836 wpa_config_remove_network(wpa_s->conf, ssid->id);
1840 ssid->passphrase = NULL;
1841 ssid->psk_set = params->psk_set;
1843 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1844 else if (ssid->passphrase)
1845 wpa_config_update_psk(ssid);
1846 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1848 wpa_s->ap_configured_cb = p2p_go_configured;
1849 wpa_s->ap_configured_cb_ctx = wpa_s;
1850 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1851 wpa_s->scan_req = NORMAL_SCAN_REQ;
1852 wpa_s->connect_without_scan = ssid;
1853 wpa_s->reassociate = 1;
1854 wpa_s->disconnected = 0;
1855 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1857 wpa_supplicant_req_scan(wpa_s, 0, 0);
1861 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1862 const struct wpa_supplicant *src)
1864 struct wpa_config *d;
1865 const struct wpa_config *s;
1870 #define C(n) if (s->n) d->n = os_strdup(s->n)
1879 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1880 os_memcpy(d->sec_device_type, s->sec_device_type,
1881 sizeof(d->sec_device_type));
1882 d->num_sec_device_types = s->num_sec_device_types;
1884 d->p2p_group_idle = s->p2p_group_idle;
1885 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
1886 d->p2p_intra_bss = s->p2p_intra_bss;
1887 d->persistent_reconnect = s->persistent_reconnect;
1888 d->max_num_sta = s->max_num_sta;
1889 d->pbc_in_m1 = s->pbc_in_m1;
1890 d->ignore_old_scan_res = s->ignore_old_scan_res;
1891 d->beacon_int = s->beacon_int;
1892 d->dtim_period = s->dtim_period;
1893 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
1894 d->disassoc_low_ack = s->disassoc_low_ack;
1895 d->disable_scan_offload = s->disable_scan_offload;
1896 d->passive_scan = s->passive_scan;
1898 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1899 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1900 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1902 d->p2p_cli_probe = s->p2p_cli_probe;
1906 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1907 char *ifname, size_t len)
1909 char *ifname_ptr = wpa_s->ifname;
1911 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1912 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1913 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1916 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1917 if (os_strlen(ifname) >= IFNAMSIZ &&
1918 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1921 /* Try to avoid going over the IFNAMSIZ length limit */
1922 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1923 if (os_snprintf_error(len, res) && len)
1924 ifname[len - 1] = '\0';
1929 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1930 enum wpa_driver_if_type type)
1932 char ifname[120], force_ifname[120];
1934 if (wpa_s->pending_interface_name[0]) {
1935 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1936 "- skip creation of a new one");
1937 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1938 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1939 "unknown?! ifname='%s'",
1940 wpa_s->pending_interface_name);
1946 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1947 force_ifname[0] = '\0';
1949 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1951 wpa_s->p2p_group_idx++;
1953 wpa_s->pending_interface_type = type;
1954 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1955 wpa_s->pending_interface_addr, NULL) < 0) {
1956 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1961 if (force_ifname[0]) {
1962 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1964 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1965 sizeof(wpa_s->pending_interface_name));
1967 os_strlcpy(wpa_s->pending_interface_name, ifname,
1968 sizeof(wpa_s->pending_interface_name));
1969 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1970 MACSTR, wpa_s->pending_interface_name,
1971 MAC2STR(wpa_s->pending_interface_addr));
1977 static void wpas_p2p_remove_pending_group_interface(
1978 struct wpa_supplicant *wpa_s)
1980 if (!wpa_s->pending_interface_name[0] ||
1981 is_zero_ether_addr(wpa_s->pending_interface_addr))
1982 return; /* No pending virtual interface */
1984 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1985 wpa_s->pending_interface_name);
1986 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1987 wpa_s->pending_interface_name);
1988 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1989 wpa_s->pending_interface_name[0] = '\0';
1990 wpa_s->global->pending_group_iface_for_p2ps = 0;
1994 static struct wpa_supplicant *
1995 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1997 struct wpa_interface iface;
1998 struct wpa_supplicant *group_wpa_s;
2000 if (!wpa_s->pending_interface_name[0]) {
2001 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2002 if (!wpas_p2p_create_iface(wpa_s))
2005 * Something has forced us to remove the pending interface; try
2006 * to create a new one and hope for the best that we will get
2007 * the same local address.
2009 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2010 WPA_IF_P2P_CLIENT) < 0)
2014 os_memset(&iface, 0, sizeof(iface));
2015 iface.ifname = wpa_s->pending_interface_name;
2016 iface.driver = wpa_s->driver->name;
2017 if (wpa_s->conf->ctrl_interface == NULL &&
2018 wpa_s->parent != wpa_s &&
2020 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2021 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2023 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
2024 iface.driver_param = wpa_s->conf->driver_param;
2025 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
2026 if (group_wpa_s == NULL) {
2027 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2028 "wpa_supplicant interface");
2031 wpa_s->pending_interface_name[0] = '\0';
2032 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2033 P2P_GROUP_INTERFACE_CLIENT;
2034 wpa_s->global->p2p_group_formation = group_wpa_s;
2035 wpa_s->global->pending_group_iface_for_p2ps = 0;
2037 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2043 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2046 struct wpa_supplicant *wpa_s = eloop_ctx;
2047 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2048 wpas_p2p_group_formation_failed(wpa_s, 0);
2052 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2053 int already_deleted)
2055 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2056 wpa_s->parent, NULL);
2057 if (wpa_s->global->p2p)
2058 p2p_group_formation_failed(wpa_s->global->p2p);
2059 wpas_group_formation_completed(wpa_s, 0, already_deleted);
2063 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2065 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2066 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2067 wpa_s->parent, NULL);
2068 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2069 wpa_s->parent, NULL);
2070 wpa_s->global->p2p_fail_on_wps_complete = 0;
2074 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2076 if (wpa_s->global->p2p_group_formation != wpa_s)
2078 /* Speed up group formation timeout since this cannot succeed */
2079 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2080 wpa_s->parent, NULL);
2081 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2082 wpa_s->parent, NULL);
2086 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2088 struct wpa_supplicant *wpa_s = ctx;
2090 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2091 wpa_drv_cancel_remain_on_channel(wpa_s);
2092 wpa_s->off_channel_freq = 0;
2093 wpa_s->roc_waiting_drv_freq = 0;
2097 wpa_msg_global(wpa_s, MSG_INFO,
2098 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2100 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2101 wpas_p2p_remove_pending_group_interface(wpa_s);
2105 if (!res->role_go) {
2106 /* Inform driver of the operating channel of GO. */
2107 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2110 if (wpa_s->p2p_go_ht40)
2112 if (wpa_s->p2p_go_vht)
2115 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2116 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2118 res->role_go ? "GO" : "client", res->freq, res->ht40,
2119 MAC2STR(res->peer_device_addr),
2120 MAC2STR(res->peer_interface_addr),
2121 p2p_wps_method_text(res->wps_method));
2122 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2124 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2125 struct wpa_ssid *ssid;
2126 ssid = wpa_config_get_network(wpa_s->conf,
2127 wpa_s->p2p_persistent_id);
2128 if (ssid && ssid->disabled == 2 &&
2129 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2130 size_t len = os_strlen(ssid->passphrase);
2131 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2132 "on requested persistent group");
2133 os_memcpy(res->passphrase, ssid->passphrase, len);
2134 res->passphrase[len] = '\0';
2138 if (wpa_s->create_p2p_iface) {
2139 struct wpa_supplicant *group_wpa_s =
2140 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2141 if (group_wpa_s == NULL) {
2142 wpas_p2p_remove_pending_group_interface(wpa_s);
2143 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2145 wpas_p2p_group_formation_failed(wpa_s, 1);
2148 if (group_wpa_s != wpa_s) {
2149 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2150 sizeof(group_wpa_s->p2p_pin));
2151 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2153 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2154 wpa_s->pending_interface_name[0] = '\0';
2155 group_wpa_s->p2p_in_provisioning = 1;
2158 wpas_start_wps_go(group_wpa_s, res, 1);
2160 wpas_start_wps_enrollee(group_wpa_s, res);
2162 wpa_s->p2p_in_provisioning = 1;
2163 wpa_s->global->p2p_group_formation = wpa_s;
2166 wpas_start_wps_go(wpa_s, res, 1);
2168 wpas_start_wps_enrollee(ctx, res);
2171 wpa_s->p2p_long_listen = 0;
2172 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2174 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2175 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2176 (res->peer_config_timeout % 100) * 10000,
2177 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2181 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2184 struct wpa_supplicant *wpa_s = ctx;
2185 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2186 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2187 dev_passwd_id, go_intent);
2189 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
2193 static void wpas_dev_found(void *ctx, const u8 *addr,
2194 const struct p2p_peer_info *info,
2197 #ifndef CONFIG_NO_STDOUT_DEBUG
2198 struct wpa_supplicant *wpa_s = ctx;
2199 char devtype[WPS_DEV_TYPE_BUFSIZE];
2200 char *wfd_dev_info_hex = NULL;
2202 #ifdef CONFIG_WIFI_DISPLAY
2203 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2204 WFD_SUBELEM_DEVICE_INFO);
2205 #endif /* CONFIG_WIFI_DISPLAY */
2207 if (info->p2ps_instance) {
2209 const u8 *buf = wpabuf_head(info->p2ps_instance);
2210 size_t len = wpabuf_len(info->p2ps_instance);
2217 if (len < 4 + 2 + 1)
2219 id = WPA_GET_LE32(buf);
2221 methods = WPA_GET_BE16(buf);
2224 if (str_len > len - 4 - 2 - 1)
2226 os_memcpy(str, buf, str_len);
2227 str[str_len] = '\0';
2229 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2231 wpa_msg_global(wpa_s, MSG_INFO,
2232 P2P_EVENT_DEVICE_FOUND MACSTR
2233 " p2p_dev_addr=" MACSTR
2234 " pri_dev_type=%s name='%s'"
2235 " config_methods=0x%x"
2238 " adv_id=%x asp_svc=%s%s",
2240 MAC2STR(info->p2p_device_addr),
2241 wps_dev_type_bin2str(
2243 devtype, sizeof(devtype)),
2244 info->device_name, methods,
2245 info->dev_capab, info->group_capab,
2247 info->vendor_elems ?
2248 " vendor_elems=1" : "");
2253 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2254 " p2p_dev_addr=" MACSTR
2255 " pri_dev_type=%s name='%s' config_methods=0x%x "
2256 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2257 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2258 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2260 info->device_name, info->config_methods,
2261 info->dev_capab, info->group_capab,
2262 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2263 wfd_dev_info_hex ? wfd_dev_info_hex : "",
2264 info->vendor_elems ? " vendor_elems=1" : "",
2268 os_free(wfd_dev_info_hex);
2269 #endif /* CONFIG_NO_STDOUT_DEBUG */
2271 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2275 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2277 struct wpa_supplicant *wpa_s = ctx;
2279 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2280 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2282 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2286 static void wpas_find_stopped(void *ctx)
2288 struct wpa_supplicant *wpa_s = ctx;
2289 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2290 wpas_notify_p2p_find_stopped(wpa_s);
2294 struct wpas_p2p_listen_work {
2296 unsigned int duration;
2297 struct wpabuf *probe_resp_ie;
2301 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2305 wpabuf_free(lwork->probe_resp_ie);
2310 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2312 struct wpas_p2p_listen_work *lwork;
2314 if (!wpa_s->p2p_listen_work)
2317 lwork = wpa_s->p2p_listen_work->ctx;
2318 wpas_p2p_listen_work_free(lwork);
2319 radio_work_done(wpa_s->p2p_listen_work);
2320 wpa_s->p2p_listen_work = NULL;
2324 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2326 struct wpa_supplicant *wpa_s = work->wpa_s;
2327 struct wpas_p2p_listen_work *lwork = work->ctx;
2328 unsigned int duration;
2331 if (work->started) {
2332 wpa_s->p2p_listen_work = NULL;
2333 wpas_stop_listen(wpa_s);
2335 wpas_p2p_listen_work_free(lwork);
2339 wpa_s->p2p_listen_work = work;
2341 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2343 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2344 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2345 "report received Probe Request frames");
2346 wpas_p2p_listen_work_done(wpa_s);
2350 wpa_s->pending_listen_freq = lwork->freq;
2351 wpa_s->pending_listen_duration = lwork->duration;
2353 duration = lwork->duration;
2354 #ifdef CONFIG_TESTING_OPTIONS
2355 if (wpa_s->extra_roc_dur) {
2356 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2357 duration, duration + wpa_s->extra_roc_dur);
2358 duration += wpa_s->extra_roc_dur;
2360 #endif /* CONFIG_TESTING_OPTIONS */
2362 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2363 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2364 "to remain on channel (%u MHz) for Listen "
2365 "state", lwork->freq);
2366 wpas_p2p_listen_work_done(wpa_s);
2367 wpa_s->pending_listen_freq = 0;
2370 wpa_s->off_channel_freq = 0;
2371 wpa_s->roc_waiting_drv_freq = lwork->freq;
2375 static int wpas_start_listen(void *ctx, unsigned int freq,
2376 unsigned int duration,
2377 const struct wpabuf *probe_resp_ie)
2379 struct wpa_supplicant *wpa_s = ctx;
2380 struct wpas_p2p_listen_work *lwork;
2382 if (wpa_s->p2p_listen_work) {
2383 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2387 lwork = os_zalloc(sizeof(*lwork));
2391 lwork->duration = duration;
2392 if (probe_resp_ie) {
2393 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2394 if (lwork->probe_resp_ie == NULL) {
2395 wpas_p2p_listen_work_free(lwork);
2400 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2402 wpas_p2p_listen_work_free(lwork);
2410 static void wpas_stop_listen(void *ctx)
2412 struct wpa_supplicant *wpa_s = ctx;
2413 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2414 wpa_drv_cancel_remain_on_channel(wpa_s);
2415 wpa_s->off_channel_freq = 0;
2416 wpa_s->roc_waiting_drv_freq = 0;
2418 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2421 * Don't cancel Probe Request RX reporting for a connected P2P Client
2422 * handling Probe Request frames.
2424 if (!wpa_s->p2p_cli_probe)
2425 wpa_drv_probe_req_report(wpa_s, 0);
2427 wpas_p2p_listen_work_done(wpa_s);
2431 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2434 struct wpa_supplicant *wpa_s = ctx;
2435 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2440 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2441 const u8 *peer, const char *params,
2442 unsigned int generated_pin)
2444 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2445 " %08d%s", MAC2STR(peer), generated_pin, params);
2449 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2450 const u8 *peer, const char *params)
2452 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2453 "%s", MAC2STR(peer), params);
2457 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2458 const u8 *dev_addr, const u8 *pri_dev_type,
2459 const char *dev_name, u16 supp_config_methods,
2460 u8 dev_capab, u8 group_capab, const u8 *group_id,
2461 size_t group_id_len)
2463 struct wpa_supplicant *wpa_s = ctx;
2464 char devtype[WPS_DEV_TYPE_BUFSIZE];
2466 u8 empty_dev_type[8];
2467 unsigned int generated_pin = 0;
2468 struct wpa_supplicant *group = NULL;
2472 for (group = wpa_s->global->ifaces; group; group = group->next)
2474 struct wpa_ssid *s = group->current_ssid;
2476 s->mode == WPAS_MODE_P2P_GO &&
2477 group_id_len - ETH_ALEN == s->ssid_len &&
2478 os_memcmp(group_id + ETH_ALEN, s->ssid,
2484 if (pri_dev_type == NULL) {
2485 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2486 pri_dev_type = empty_dev_type;
2488 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2489 " pri_dev_type=%s name='%s' config_methods=0x%x "
2490 "dev_capab=0x%x group_capab=0x%x%s%s",
2492 wps_dev_type_bin2str(pri_dev_type, devtype,
2494 dev_name, supp_config_methods, dev_capab, group_capab,
2495 group ? " group=" : "",
2496 group ? group->ifname : "");
2497 if (os_snprintf_error(sizeof(params), res))
2498 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
2499 params[sizeof(params) - 1] = '\0';
2501 if (config_methods & WPS_CONFIG_DISPLAY) {
2502 generated_pin = wps_generate_pin();
2503 wpas_prov_disc_local_display(wpa_s, peer, params,
2505 } else if (config_methods & WPS_CONFIG_KEYPAD)
2506 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2507 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2508 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2509 MACSTR "%s", MAC2STR(peer), params);
2511 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2512 P2P_PROV_DISC_SUCCESS,
2513 config_methods, generated_pin);
2517 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2519 struct wpa_supplicant *wpa_s = ctx;
2520 unsigned int generated_pin = 0;
2523 if (wpa_s->pending_pd_before_join &&
2524 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2525 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2526 wpa_s->pending_pd_before_join = 0;
2527 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2528 "join-existing-group operation");
2529 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2533 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2534 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2537 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2538 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2539 if (os_snprintf_error(sizeof(params), res))
2540 params[sizeof(params) - 1] = '\0';
2544 if (config_methods & WPS_CONFIG_DISPLAY)
2545 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2546 else if (config_methods & WPS_CONFIG_KEYPAD) {
2547 generated_pin = wps_generate_pin();
2548 wpas_prov_disc_local_display(wpa_s, peer, params,
2550 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2551 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2552 MACSTR "%s", MAC2STR(peer), params);
2554 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2555 P2P_PROV_DISC_SUCCESS,
2556 config_methods, generated_pin);
2560 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2561 enum p2p_prov_disc_status status,
2562 u32 adv_id, const u8 *adv_mac,
2563 const char *deferred_session_resp)
2565 struct wpa_supplicant *wpa_s = ctx;
2567 if (wpa_s->p2p_fallback_to_go_neg) {
2568 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2569 "failed - fall back to GO Negotiation");
2570 wpa_msg_global(wpa_s->parent, MSG_INFO,
2571 P2P_EVENT_FALLBACK_TO_GO_NEG
2572 "reason=PD-failed");
2573 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2577 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2578 wpa_s->pending_pd_before_join = 0;
2579 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2580 "join-existing-group operation (no ACK for PD "
2582 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2586 if (adv_id && adv_mac && deferred_session_resp) {
2587 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2588 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2589 " deferred_session_resp='%s'",
2590 MAC2STR(peer), status, adv_id,
2591 deferred_session_resp);
2592 } else if (adv_id && adv_mac) {
2593 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2594 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2595 MAC2STR(peer), status, adv_id);
2597 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2598 " p2p_dev_addr=" MACSTR " status=%d",
2599 MAC2STR(peer), status);
2602 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2607 static int freq_included(struct wpa_supplicant *wpa_s,
2608 const struct p2p_channels *channels,
2611 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2612 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2618 static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2620 unsigned int num = P2P_MAX_CHANNELS;
2624 p2p_go_dump_common_freqs(wpa_s);
2625 common_freqs = os_calloc(num, sizeof(int));
2629 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2631 wpa_dbg(wpa_s, MSG_DEBUG,
2632 "P2P: Failed to get group common freqs");
2633 os_free(common_freqs);
2637 os_free(wpa_s->p2p_group_common_freqs);
2638 wpa_s->p2p_group_common_freqs = common_freqs;
2639 wpa_s->p2p_group_common_freqs_num = num;
2640 p2p_go_dump_common_freqs(wpa_s);
2645 * Check if the given frequency is one of the possible operating frequencies
2646 * set after the completion of the GO Negotiation.
2648 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2652 p2p_go_dump_common_freqs(wpa_s);
2654 /* assume no restrictions */
2655 if (!wpa_s->p2p_group_common_freqs_num)
2658 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2659 if (wpa_s->p2p_group_common_freqs[i] == freq)
2667 * Pick the best frequency to use from all the currently used frequencies.
2669 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2670 struct wpa_used_freq_data *freqs,
2675 /* find a candidate freq that is supported by P2P */
2676 for (c = 0; c < num; c++)
2677 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2683 /* once we have a candidate, try to find a 'better' one */
2684 for (i = c + 1; i < num; i++) {
2685 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2689 * 1. Infrastructure station interfaces have higher preference.
2690 * 2. P2P Clients have higher preference.
2693 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2698 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2701 return freqs[c].freq;
2705 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2706 const u8 *go_dev_addr, const u8 *ssid,
2707 size_t ssid_len, int *go, u8 *group_bssid,
2708 int *force_freq, int persistent_group,
2709 const struct p2p_channels *channels,
2712 struct wpa_supplicant *wpa_s = ctx;
2714 struct wpa_used_freq_data *freqs;
2715 struct wpa_supplicant *grp;
2718 if (!persistent_group) {
2719 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2720 " to join an active group (SSID: %s)",
2721 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2722 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2723 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2725 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2726 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2727 "authorized invitation");
2731 #ifdef CONFIG_WPS_NFC
2732 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2733 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
2734 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2735 wpa_s->p2p_wps_method = WPS_NFC;
2736 wpa_s->pending_join_wps_method = WPS_NFC;
2737 os_memcpy(wpa_s->pending_join_dev_addr,
2738 go_dev_addr, ETH_ALEN);
2739 os_memcpy(wpa_s->pending_join_iface_addr,
2743 #endif /* CONFIG_WPS_NFC */
2746 * Do not accept the invitation automatically; notify user and
2749 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2752 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2754 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2755 "running persistent group");
2757 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2761 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2762 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2763 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2764 "invitation to re-invoke a persistent group");
2765 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2766 } else if (!wpa_s->conf->persistent_reconnect)
2767 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2769 for (s = wpa_s->conf->ssid; s; s = s->next) {
2770 if (s->disabled == 2 &&
2771 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2772 s->ssid_len == ssid_len &&
2773 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2778 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2779 " requested reinvocation of an unknown group",
2781 return P2P_SC_FAIL_UNKNOWN_GROUP;
2784 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2786 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2787 wpa_printf(MSG_DEBUG, "P2P: The only available "
2788 "interface is already in use - reject "
2790 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2792 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2793 } else if (s->mode == WPAS_MODE_P2P_GO) {
2795 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2797 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2798 "interface address for the group");
2799 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2801 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2806 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2809 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2810 sizeof(struct wpa_used_freq_data));
2812 int num_channels = wpa_s->num_multichan_concurrent;
2813 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2814 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2818 /* Get one of the frequencies currently in use */
2819 if (best_freq > 0) {
2820 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2821 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
2823 if (wpa_s->num_multichan_concurrent < 2 ||
2824 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2825 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2826 *force_freq = best_freq;
2830 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2831 wpas_p2p_num_unused_channels(wpa_s) > 0) {
2833 /* We are the client */
2834 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2835 "running a GO but we are capable of MCC, "
2836 "figure out the best channel to use");
2838 } else if (!freq_included(wpa_s, channels, *force_freq)) {
2839 /* We are the GO, and *force_freq is not in the
2841 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2842 "in intersection but we are capable of MCC, "
2843 "figure out the best channel to use",
2849 return P2P_SC_SUCCESS;
2853 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2854 const u8 *ssid, size_t ssid_len,
2855 const u8 *go_dev_addr, u8 status,
2858 struct wpa_supplicant *wpa_s = ctx;
2861 for (s = wpa_s->conf->ssid; s; s = s->next) {
2862 if (s->disabled == 2 &&
2863 s->ssid_len == ssid_len &&
2864 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2868 if (status == P2P_SC_SUCCESS) {
2869 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2870 " was accepted; op_freq=%d MHz, SSID=%s",
2871 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
2873 int go = s->mode == WPAS_MODE_P2P_GO;
2874 wpas_p2p_group_add_persistent(
2875 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
2876 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
2878 wpa_s->user_initiated_pd = 0;
2879 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2880 wpa_s->p2p_wps_method, 0, op_freq,
2886 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2887 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2888 " was rejected (status %u)", MAC2STR(sa), status);
2894 wpa_msg_global(wpa_s, MSG_INFO,
2895 P2P_EVENT_INVITATION_RECEIVED
2896 "sa=" MACSTR " go_dev_addr=" MACSTR
2897 " bssid=" MACSTR " unknown-network",
2898 MAC2STR(sa), MAC2STR(go_dev_addr),
2901 wpa_msg_global(wpa_s, MSG_INFO,
2902 P2P_EVENT_INVITATION_RECEIVED
2903 "sa=" MACSTR " go_dev_addr=" MACSTR
2905 MAC2STR(sa), MAC2STR(go_dev_addr));
2910 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2911 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2912 "sa=" MACSTR " persistent=%d freq=%d",
2913 MAC2STR(sa), s->id, op_freq);
2915 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2916 "sa=" MACSTR " persistent=%d",
2917 MAC2STR(sa), s->id);
2922 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2923 struct wpa_ssid *ssid,
2924 const u8 *peer, int inv)
2927 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
2932 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2933 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
2937 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
2938 if (ssid->mode != WPAS_MODE_P2P_GO &&
2939 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2940 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2941 "due to invitation result", ssid->id);
2942 wpas_notify_network_removed(wpa_s, ssid);
2943 wpa_config_remove_network(wpa_s->conf, ssid->id);
2946 return; /* Peer not found in client list */
2949 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2950 "group %d client list%s",
2951 MAC2STR(peer), ssid->id,
2952 inv ? " due to invitation result" : "");
2953 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
2954 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
2955 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
2956 ssid->num_p2p_clients--;
2957 if (p2p_wpa_s->conf->update_config &&
2958 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
2959 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2963 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2966 struct wpa_ssid *ssid;
2968 wpa_s = wpa_s->global->p2p_invite_group;
2970 return; /* No known invitation group */
2971 ssid = wpa_s->current_ssid;
2972 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2973 !ssid->p2p_persistent_group)
2974 return; /* Not operating as a GO in persistent group */
2975 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2976 ssid->ssid, ssid->ssid_len);
2977 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2981 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2982 const struct p2p_channels *channels,
2983 const u8 *peer, int neg_freq,
2986 struct wpa_supplicant *wpa_s = ctx;
2987 struct wpa_ssid *ssid;
2991 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2992 "status=%d " MACSTR,
2993 status, MAC2STR(bssid));
2995 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2996 "status=%d ", status);
2998 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3000 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3001 status, MAC2STR(peer));
3002 if (wpa_s->pending_invite_ssid_id == -1) {
3003 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3004 wpas_remove_persistent_client(wpa_s, peer);
3005 return; /* Invitation to active group */
3008 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3009 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3010 "invitation exchange to indicate readiness for "
3014 if (status != P2P_SC_SUCCESS) {
3015 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3016 ssid = wpa_config_get_network(
3017 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3018 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3020 wpas_p2p_remove_pending_group_interface(wpa_s);
3024 ssid = wpa_config_get_network(wpa_s->conf,
3025 wpa_s->pending_invite_ssid_id);
3027 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3028 "data matching with invitation");
3033 * The peer could have missed our ctrl::ack frame for Invitation
3034 * Response and continue retransmitting the frame. To reduce the
3035 * likelihood of the peer not getting successful TX status for the
3036 * Invitation Response frame, wait a short time here before starting
3037 * the persistent group so that we will remain on the current channel to
3038 * acknowledge any possible retransmission from the peer.
3040 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3041 "starting persistent group");
3044 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3045 freq_included(wpa_s, channels, neg_freq))
3047 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3048 freq_included(wpa_s, channels, peer_oper_freq))
3049 freq = peer_oper_freq;
3053 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3054 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3055 wpas_p2p_group_add_persistent(wpa_s, ssid,
3056 ssid->mode == WPAS_MODE_P2P_GO,
3057 wpa_s->p2p_persistent_go_freq,
3059 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3061 ssid->mode == WPAS_MODE_P2P_GO ?
3062 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3067 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3070 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3072 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3076 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3078 reg->channel[reg->channels] = chan;
3083 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3084 struct p2p_channels *chan,
3085 struct p2p_channels *cli_chan)
3089 wpa_s->global->p2p_24ghz_social_channels = 1;
3091 os_memset(cli_chan, 0, sizeof(*cli_chan));
3093 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3096 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3097 chan->reg_class[cla].reg_class = 81;
3098 chan->reg_class[cla].channels = 0;
3099 for (i = 0; i < 11; i++) {
3100 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3101 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3103 if (chan->reg_class[cla].channels)
3106 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3109 /* Operating class 115 - 5 GHz, channels 36-48 */
3110 chan->reg_class[cla].reg_class = 115;
3111 chan->reg_class[cla].channels = 0;
3112 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3113 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3114 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3115 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3116 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3117 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3118 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3119 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3120 if (chan->reg_class[cla].channels)
3123 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3126 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3127 chan->reg_class[cla].reg_class = 124;
3128 chan->reg_class[cla].channels = 0;
3129 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3130 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3131 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3132 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3133 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3134 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3135 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3136 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3137 if (chan->reg_class[cla].channels)
3140 chan->reg_classes = cla;
3145 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3147 enum hostapd_hw_mode mode)
3151 for (i = 0; i < num_modes; i++) {
3152 if (modes[i].mode == mode)
3161 NOT_ALLOWED, NO_IR, ALLOWED
3164 static int has_channel(struct wpa_global *global,
3165 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3170 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3172 if (wpas_p2p_disallowed_freq(global, freq))
3175 for (i = 0; i < mode->num_channels; i++) {
3176 if (mode->channels[i].chan == chan) {
3178 *flags = mode->channels[i].flag;
3179 if (mode->channels[i].flag &
3180 (HOSTAPD_CHAN_DISABLED |
3181 HOSTAPD_CHAN_RADAR))
3183 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3193 struct p2p_oper_class_map {
3194 enum hostapd_hw_mode mode;
3199 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3202 static const struct p2p_oper_class_map op_class[] = {
3203 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3204 #if 0 /* Do not enable HT40 on 2 GHz for now */
3205 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3206 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3208 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3209 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3210 { HOSTAPD_MODE_IEEE80211A, 125, 149, 169, 4, BW20 },
3211 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3212 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3213 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3214 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3217 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3218 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3219 * 80 MHz, but currently use the following definition for simplicity
3220 * (these center frequencies are not actual channels, which makes
3221 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3222 * removing invalid channels.
3224 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3225 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3226 { -1, 0, 0, 0, 0, BW20 }
3230 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3231 struct hostapd_hw_modes *mode,
3234 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3237 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3240 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3242 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3243 * so the center channel is 6 channels away from the start/end.
3245 if (channel >= center_channels[i] - 6 &&
3246 channel <= center_channels[i] + 6)
3247 return center_channels[i];
3253 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3254 struct hostapd_hw_modes *mode,
3259 enum chan_allowed res, ret = ALLOWED;
3261 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3264 if (center_chan >= 58 && center_chan <= 138)
3265 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3267 /* check all the channels are available */
3268 for (i = 0; i < 4; i++) {
3269 int adj_chan = center_chan - 6 + i * 4;
3271 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3272 if (res == NOT_ALLOWED)
3277 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3279 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3281 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3283 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3291 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3292 struct hostapd_hw_modes *mode,
3296 enum chan_allowed res, res2;
3298 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3299 if (bw == BW40MINUS) {
3300 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3302 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3303 } else if (bw == BW40PLUS) {
3304 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3306 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3307 } else if (bw == BW80) {
3308 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3311 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3313 if (res == NO_IR || res2 == NO_IR)
3319 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3320 struct p2p_channels *chan,
3321 struct p2p_channels *cli_chan)
3323 struct hostapd_hw_modes *mode;
3324 int cla, op, cli_cla;
3326 if (wpa_s->hw.modes == NULL) {
3327 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3328 "of all supported channels; assume dualband "
3330 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3335 for (op = 0; op_class[op].op_class; op++) {
3336 const struct p2p_oper_class_map *o = &op_class[op];
3338 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3340 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3343 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3344 wpa_s->global->p2p_24ghz_social_channels = 1;
3345 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3346 enum chan_allowed res;
3347 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3348 if (res == ALLOWED) {
3350 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3352 reg = &chan->reg_class[cla];
3354 reg->reg_class = o->op_class;
3356 reg->channel[reg->channels] = ch;
3358 } else if (res == NO_IR &&
3359 wpa_s->conf->p2p_add_cli_chan) {
3360 if (cli_reg == NULL) {
3361 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3363 cli_reg = &cli_chan->reg_class[cli_cla];
3365 cli_reg->reg_class = o->op_class;
3367 cli_reg->channel[cli_reg->channels] = ch;
3368 cli_reg->channels++;
3372 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3373 reg->channel, reg->channels);
3376 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3377 cli_reg->channel, cli_reg->channels);
3381 chan->reg_classes = cla;
3382 cli_chan->reg_classes = cli_cla;
3388 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3389 struct hostapd_hw_modes *mode, u8 channel)
3392 enum chan_allowed ret;
3394 for (op = 0; op_class[op].op_class; op++) {
3395 const struct p2p_oper_class_map *o = &op_class[op];
3398 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3399 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3400 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3403 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3405 return (o->bw == BW40MINUS) ? -1 : 1;
3412 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3413 struct hostapd_hw_modes *mode, u8 channel)
3415 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3418 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3422 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3425 struct wpa_supplicant *wpa_s = ctx;
3427 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3428 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3434 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3438 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3439 const u8 *ssid, size_t ssid_len)
3441 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3442 struct wpa_ssid *s = wpa_s->current_ssid;
3445 if (s->mode != WPAS_MODE_P2P_GO &&
3446 s->mode != WPAS_MODE_AP &&
3447 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3449 if (s->ssid_len != ssid_len ||
3450 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3460 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3461 const u8 *peer_dev_addr)
3463 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3464 struct wpa_ssid *ssid = wpa_s->current_ssid;
3465 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
3467 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3475 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3477 struct wpa_supplicant *wpa_s = ctx;
3479 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3483 static int wpas_is_concurrent_session_active(void *ctx)
3485 struct wpa_supplicant *wpa_s = ctx;
3486 struct wpa_supplicant *ifs;
3488 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3491 if (ifs->wpa_state > WPA_ASSOCIATED)
3498 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3500 struct wpa_supplicant *wpa_s = ctx;
3501 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3505 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3506 const char *conf_p2p_dev)
3508 struct wpa_interface iface;
3509 struct wpa_supplicant *p2pdev_wpa_s;
3511 char force_name[100];
3514 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3516 if (os_snprintf_error(sizeof(ifname), ret))
3518 force_name[0] = '\0';
3519 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3520 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3521 force_name, wpa_s->pending_interface_addr, NULL);
3523 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3526 os_strlcpy(wpa_s->pending_interface_name, ifname,
3527 sizeof(wpa_s->pending_interface_name));
3529 os_memset(&iface, 0, sizeof(iface));
3531 iface.ifname = wpa_s->pending_interface_name;
3532 iface.driver = wpa_s->driver->name;
3533 iface.driver_param = wpa_s->conf->driver_param;
3536 * If a P2P Device configuration file was given, use it as the interface
3537 * configuration file (instead of using parent's configuration file.
3540 iface.confname = conf_p2p_dev;
3541 iface.ctrl_interface = NULL;
3543 iface.confname = wpa_s->confname;
3544 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3547 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
3548 if (!p2pdev_wpa_s) {
3549 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3553 wpa_s->pending_interface_name[0] = '\0';
3558 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3559 const u8 *noa, size_t noa_len)
3561 struct wpa_supplicant *wpa_s, *intf = ctx;
3564 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3565 if (wpa_s->waiting_presence_resp)
3569 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3572 wpa_s->waiting_presence_resp = 0;
3574 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3575 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3576 " status=%u noa=%s", MAC2STR(src), status, hex);
3580 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3581 size_t ssid_len, u8 *go_dev_addr,
3582 u8 *ret_ssid, size_t *ret_ssid_len,
3583 u8 *intended_iface_addr)
3585 struct wpa_supplicant *wpa_s = ctx;
3588 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3590 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3591 *ret_ssid_len = s->ssid_len;
3592 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3594 if (s->mode != WPAS_MODE_P2P_GO) {
3595 os_memset(intended_iface_addr, 0, ETH_ALEN);
3596 } else if (wpas_p2p_create_iface(wpa_s)) {
3597 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3600 os_memcpy(intended_iface_addr,
3601 wpa_s->pending_interface_addr, ETH_ALEN);
3603 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3613 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3614 u8 *ssid, size_t *ssid_len, int *group_iface)
3616 struct wpa_supplicant *wpa_s = ctx;
3621 * group_iface will be set to 1 only if a dedicated interface for P2P
3622 * role is required. First, we try to reuse an active GO. However,
3623 * if it is not present, we will try to reactivate an existing
3624 * persistent group and set group_iface to 1, so the caller will know
3625 * that the pending interface should be used.
3628 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
3630 s = wpas_p2p_get_persistent_go(wpa_s);
3631 *group_iface = wpas_p2p_create_iface(wpa_s);
3633 os_memcpy(bssid, s->bssid, ETH_ALEN);
3638 os_memcpy(intended_addr, bssid, ETH_ALEN);
3639 os_memcpy(ssid, s->ssid, s->ssid_len);
3640 *ssid_len = s->ssid_len;
3646 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3647 const u8 *ssid, size_t ssid_len)
3649 struct wpa_supplicant *wpa_s = ctx;
3651 int save_config = 0;
3654 /* Start with our first choice of Persistent Groups */
3655 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3656 if (go && ssid && ssid_len &&
3657 s->ssid_len == ssid_len &&
3658 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3659 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3662 /* Remove stale persistent group */
3663 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3664 wpa_config_remove_network(wpa_s->conf, s->id);
3669 for (i = 0; i < s->num_p2p_clients; i++) {
3670 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3671 peer, ETH_ALEN) != 0)
3674 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3675 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3676 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3679 s->num_p2p_clients--;
3684 p2p_config_write(wpa_s);
3686 /* Return TRUE if valid SSID remains */
3691 static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3692 const u8 *feat_cap, size_t feat_cap_len)
3694 static const char pref[] = " feature_cap=";
3700 * We expect a feature capability to contain at least one byte to be
3701 * reported. The string buffer provided by the caller function is
3702 * expected to be big enough to contain all bytes of the attribute for
3703 * known specifications. This function truncates the reported bytes if
3704 * the feature capability data exceeds the string buffer size.
3706 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3709 os_memcpy(buf, pref, sizeof(pref));
3710 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3711 buf_len - sizeof(pref) + 1,
3712 feat_cap, feat_cap_len);
3714 if (ret != (2 * (int) feat_cap_len))
3715 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3719 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3720 const u8 *adv_mac, const u8 *ses_mac,
3721 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3722 u8 conncap, int passwd_id,
3723 const u8 *persist_ssid,
3724 size_t persist_ssid_size, int response_done,
3725 int prov_start, const char *session_info,
3726 const u8 *feat_cap, size_t feat_cap_len)
3728 struct wpa_supplicant *wpa_s = ctx;
3730 struct wpa_ssid *persistent_go, *stale, *s;
3731 int save_config = 0;
3732 struct wpa_supplicant *go_wpa_s;
3733 char feat_cap_str[256];
3738 os_memset(mac, 0, ETH_ALEN);
3746 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3747 feat_cap, feat_cap_len);
3750 if (session_info == NULL) {
3751 wpa_msg_global(wpa_s, MSG_INFO,
3752 P2P_EVENT_P2PS_PROVISION_START MACSTR
3753 " adv_id=%x conncap=%x"
3755 " session=%x mac=" MACSTR
3756 " dev_passwd_id=%d%s",
3757 MAC2STR(dev), adv_id, conncap,
3759 ses_id, MAC2STR(ses_mac),
3760 passwd_id, feat_cap_str);
3762 wpa_msg_global(wpa_s, MSG_INFO,
3763 P2P_EVENT_P2PS_PROVISION_START MACSTR
3764 " adv_id=%x conncap=%x"
3766 " session=%x mac=" MACSTR
3767 " dev_passwd_id=%d info='%s'%s",
3768 MAC2STR(dev), adv_id, conncap,
3770 ses_id, MAC2STR(ses_mac),
3771 passwd_id, session_info, feat_cap_str);
3776 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3777 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3779 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3780 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3781 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3783 if (persistent_go && !persistent_go->num_p2p_clients) {
3784 /* remove empty persistent GO */
3785 wpa_config_remove_network(wpa_s->conf,
3789 wpa_msg_global(wpa_s, MSG_INFO,
3790 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3792 " adv_id=%x adv_mac=" MACSTR
3793 " session=%x mac=" MACSTR "%s",
3794 MAC2STR(dev), status,
3795 adv_id, MAC2STR(adv_mac),
3796 ses_id, MAC2STR(ses_mac), feat_cap_str);
3800 /* Clean up stale persistent groups with this device */
3801 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3804 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
3805 os_memcmp(grp_mac, mac, ETH_ALEN) == 0) {
3806 wpa_dbg(wpa_s, MSG_ERROR,
3807 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
3812 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3816 if (s && s->ssid_len == stale->ssid_len &&
3817 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3818 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3821 /* Remove stale persistent group */
3822 if (stale->mode != WPAS_MODE_P2P_GO ||
3823 stale->num_p2p_clients <= 1) {
3824 wpa_config_remove_network(wpa_s->conf, stale->id);
3828 for (i = 0; i < stale->num_p2p_clients; i++) {
3829 if (os_memcmp(stale->p2p_client_list +
3831 dev, ETH_ALEN) == 0) {
3832 os_memmove(stale->p2p_client_list +
3834 stale->p2p_client_list +
3836 (stale->num_p2p_clients -
3841 stale->num_p2p_clients--;
3847 p2p_config_write(wpa_s);
3850 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3851 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3853 if (persistent_go && s != persistent_go &&
3854 !persistent_go->num_p2p_clients) {
3855 /* remove empty persistent GO */
3856 wpa_config_remove_network(wpa_s->conf,
3861 wpa_msg_global(wpa_s, MSG_INFO,
3862 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3864 " adv_id=%x adv_mac=" MACSTR
3865 " session=%x mac=" MACSTR
3867 MAC2STR(dev), status,
3868 adv_id, MAC2STR(adv_mac),
3869 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
3873 if (conncap == P2PS_SETUP_GROUP_OWNER) {
3875 * We need to copy the interface name. Simply saving a
3876 * pointer isn't enough, since if we use pending_interface_name
3877 * it will be overwritten when the group is added.
3879 char go_ifname[100];
3881 go_ifname[0] = '\0';
3883 wpa_s->global->pending_p2ps_group = 1;
3885 if (!wpas_p2p_create_iface(wpa_s))
3886 os_memcpy(go_ifname, wpa_s->ifname,
3888 else if (wpa_s->pending_interface_name[0])
3889 os_memcpy(go_ifname,
3890 wpa_s->pending_interface_name,
3893 if (!go_ifname[0]) {
3894 wpas_p2ps_prov_complete(
3895 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3896 dev, adv_mac, ses_mac,
3897 NULL, adv_id, ses_id, 0, 0,
3898 NULL, 0, 0, 0, NULL, NULL, 0);
3902 /* If PD Resp complete, start up the GO */
3903 if (response_done && persistent_go) {
3904 wpas_p2p_group_add_persistent(
3905 wpa_s, persistent_go,
3906 0, 0, 0, 0, 0, NULL,
3907 persistent_go->mode ==
3909 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3911 } else if (response_done) {
3912 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3915 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3916 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3917 wpa_s->p2ps_join_addr_valid = 1;
3918 wpa_dbg(wpa_s, MSG_DEBUG,
3919 "P2PS: Saving PIN for " MACSTR,
3922 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3923 os_memcpy(go_ifname, go_wpa_s->ifname,
3926 wpa_dbg(go_wpa_s, MSG_DEBUG,
3927 "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
3928 wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
3931 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3932 wpa_s->p2ps_join_addr_valid = 1;
3933 wpa_dbg(wpa_s, MSG_DEBUG,
3934 "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
3937 wpa_msg_global(wpa_s, MSG_INFO,
3938 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3939 " status=%d conncap=%x"
3940 " adv_id=%x adv_mac=" MACSTR
3941 " session=%x mac=" MACSTR
3942 " dev_passwd_id=%d go=%s%s",
3943 MAC2STR(dev), status, conncap,
3944 adv_id, MAC2STR(adv_mac),
3945 ses_id, MAC2STR(ses_mac),
3946 passwd_id, go_ifname, feat_cap_str);
3950 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3951 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3953 if (persistent_go && !persistent_go->num_p2p_clients) {
3954 /* remove empty persistent GO */
3955 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
3958 if (conncap == P2PS_SETUP_CLIENT) {
3959 wpa_msg_global(wpa_s, MSG_INFO,
3960 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3961 " status=%d conncap=%x"
3962 " adv_id=%x adv_mac=" MACSTR
3963 " session=%x mac=" MACSTR
3964 " dev_passwd_id=%d join=" MACSTR "%s",
3965 MAC2STR(dev), status, conncap,
3966 adv_id, MAC2STR(adv_mac),
3967 ses_id, MAC2STR(ses_mac),
3968 passwd_id, MAC2STR(grp_mac), feat_cap_str);
3970 wpa_msg_global(wpa_s, MSG_INFO,
3971 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3972 " status=%d conncap=%x"
3973 " adv_id=%x adv_mac=" MACSTR
3974 " session=%x mac=" MACSTR
3975 " dev_passwd_id=%d%s",
3976 MAC2STR(dev), status, conncap,
3977 adv_id, MAC2STR(adv_mac),
3978 ses_id, MAC2STR(ses_mac),
3979 passwd_id, feat_cap_str);
3984 static int _wpas_p2p_in_progress(void *ctx)
3986 struct wpa_supplicant *wpa_s = ctx;
3987 return wpas_p2p_in_progress(wpa_s);
3991 static int wpas_prov_disc_resp_cb(void *ctx)
3993 struct wpa_supplicant *wpa_s = ctx;
3994 struct wpa_ssid *persistent_go;
3996 if (!wpa_s->global->pending_p2ps_group)
3999 wpa_s->global->pending_p2ps_group = 0;
4001 if (wpas_p2p_get_go_group(wpa_s))
4003 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4005 if (persistent_go) {
4006 wpas_p2p_group_add_persistent(
4007 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
4008 persistent_go->mode == WPAS_MODE_P2P_GO ?
4009 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
4011 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
4018 static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4020 unsigned int *freq_list)
4022 struct wpa_supplicant *wpa_s = ctx;
4024 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4025 WPA_IF_P2P_CLIENT, len, freq_list);
4030 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4031 * @global: Pointer to global data from wpa_supplicant_init()
4032 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4033 * Returns: 0 on success, -1 on failure
4035 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4037 struct p2p_config p2p;
4040 if (wpa_s->conf->p2p_disabled)
4043 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4049 os_memset(&p2p, 0, sizeof(p2p));
4051 p2p.debug_print = wpas_p2p_debug_print;
4052 p2p.p2p_scan = wpas_p2p_scan;
4053 p2p.send_action = wpas_send_action;
4054 p2p.send_action_done = wpas_send_action_done;
4055 p2p.go_neg_completed = wpas_go_neg_completed;
4056 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4057 p2p.dev_found = wpas_dev_found;
4058 p2p.dev_lost = wpas_dev_lost;
4059 p2p.find_stopped = wpas_find_stopped;
4060 p2p.start_listen = wpas_start_listen;
4061 p2p.stop_listen = wpas_stop_listen;
4062 p2p.send_probe_resp = wpas_send_probe_resp;
4063 p2p.sd_request = wpas_sd_request;
4064 p2p.sd_response = wpas_sd_response;
4065 p2p.prov_disc_req = wpas_prov_disc_req;
4066 p2p.prov_disc_resp = wpas_prov_disc_resp;
4067 p2p.prov_disc_fail = wpas_prov_disc_fail;
4068 p2p.invitation_process = wpas_invitation_process;
4069 p2p.invitation_received = wpas_invitation_received;
4070 p2p.invitation_result = wpas_invitation_result;
4071 p2p.get_noa = wpas_get_noa;
4072 p2p.go_connected = wpas_go_connected;
4073 p2p.presence_resp = wpas_presence_resp;
4074 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4075 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4076 p2p.get_persistent_group = wpas_get_persistent_group;
4077 p2p.get_go_info = wpas_get_go_info;
4078 p2p.remove_stale_groups = wpas_remove_stale_groups;
4079 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4080 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4081 p2p.p2ps_group_capability = p2ps_group_capability;
4082 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
4084 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4085 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4086 p2p.dev_name = wpa_s->conf->device_name;
4087 p2p.manufacturer = wpa_s->conf->manufacturer;
4088 p2p.model_name = wpa_s->conf->model_name;
4089 p2p.model_number = wpa_s->conf->model_number;
4090 p2p.serial_number = wpa_s->conf->serial_number;
4092 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4093 p2p.config_methods = wpa_s->wps->config_methods;
4096 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4097 wpa_printf(MSG_ERROR,
4098 "P2P: Failed to configure supported channel list");
4102 if (wpa_s->conf->p2p_listen_reg_class &&
4103 wpa_s->conf->p2p_listen_channel) {
4104 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4105 p2p.channel = wpa_s->conf->p2p_listen_channel;
4106 p2p.channel_forced = 1;
4109 * Pick one of the social channels randomly as the listen
4112 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4113 &p2p.channel) != 0) {
4114 wpa_printf(MSG_INFO,
4115 "P2P: No social channels supported by the driver - do not enable P2P");
4118 p2p.channel_forced = 0;
4120 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4121 p2p.reg_class, p2p.channel);
4123 if (wpa_s->conf->p2p_oper_reg_class &&
4124 wpa_s->conf->p2p_oper_channel) {
4125 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4126 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4127 p2p.cfg_op_channel = 1;
4128 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4129 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4133 * Use random operation channel from 2.4 GHz band social
4134 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4135 * other preference is indicated.
4137 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4138 &p2p.op_channel) != 0) {
4139 wpa_printf(MSG_ERROR,
4140 "P2P: Failed to select random social channel as operation channel");
4143 p2p.cfg_op_channel = 0;
4144 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4145 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4148 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4149 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4150 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4153 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4154 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4155 p2p.country[2] = 0x04;
4157 os_memcpy(p2p.country, "XX\x04", 3);
4159 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4162 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4163 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4164 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4166 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4167 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4169 p2p.max_peers = 100;
4171 if (wpa_s->conf->p2p_ssid_postfix) {
4172 p2p.ssid_postfix_len =
4173 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4174 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4175 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4176 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4177 p2p.ssid_postfix_len);
4180 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4182 p2p.max_listen = wpa_s->max_remain_on_chan;
4184 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4185 wpa_s->conf->p2p_passphrase_len <= 63)
4186 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4188 p2p.passphrase_len = 8;
4190 global->p2p = p2p_init(&p2p);
4191 if (global->p2p == NULL)
4193 global->p2p_init_wpa_s = wpa_s;
4195 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4196 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4198 p2p_add_wps_vendor_extension(
4199 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4202 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4209 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4210 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4212 * This function deinitialize per-interface P2P data.
4214 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4216 if (wpa_s->driver && wpa_s->drv_priv)
4217 wpa_drv_probe_req_report(wpa_s, 0);
4219 if (wpa_s->go_params) {
4220 /* Clear any stored provisioning info */
4221 p2p_clear_provisioning_info(
4223 wpa_s->go_params->peer_device_addr);
4226 os_free(wpa_s->go_params);
4227 wpa_s->go_params = NULL;
4228 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4229 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4230 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4231 wpa_s->p2p_long_listen = 0;
4232 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4233 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4234 wpas_p2p_remove_pending_group_interface(wpa_s);
4235 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4236 wpas_p2p_listen_work_done(wpa_s);
4237 if (wpa_s->p2p_send_action_work) {
4238 os_free(wpa_s->p2p_send_action_work->ctx);
4239 radio_work_done(wpa_s->p2p_send_action_work);
4240 wpa_s->p2p_send_action_work = NULL;
4242 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4244 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4245 wpa_s->p2p_oob_dev_pw = NULL;
4247 os_free(wpa_s->p2p_group_common_freqs);
4248 wpa_s->p2p_group_common_freqs = NULL;
4249 wpa_s->p2p_group_common_freqs_num = 0;
4251 /* TODO: remove group interface from the driver if this wpa_s instance
4252 * is on top of a P2P group interface */
4257 * wpas_p2p_deinit_global - Deinitialize global P2P module
4258 * @global: Pointer to global data from wpa_supplicant_init()
4260 * This function deinitializes the global (per device) P2P module.
4262 static void wpas_p2p_deinit_global(struct wpa_global *global)
4264 struct wpa_supplicant *wpa_s, *tmp;
4266 wpa_s = global->ifaces;
4268 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4270 /* Remove remaining P2P group interfaces */
4271 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4272 wpa_s = wpa_s->next;
4274 tmp = global->ifaces;
4277 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4282 /* Disconnect from the P2P group and deinit the interface */
4283 wpas_p2p_disconnect(tmp);
4287 * Deinit GO data on any possibly remaining interface (if main
4288 * interface is used as GO).
4290 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4291 if (wpa_s->ap_iface)
4292 wpas_p2p_group_deinit(wpa_s);
4295 p2p_deinit(global->p2p);
4297 global->p2p_init_wpa_s = NULL;
4301 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4303 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4304 wpa_s->conf->p2p_no_group_iface)
4305 return 0; /* separate interface disabled per configuration */
4306 if (wpa_s->drv_flags &
4307 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4308 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4309 return 1; /* P2P group requires a new interface in every case
4311 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4312 return 0; /* driver does not support concurrent operations */
4313 if (wpa_s->global->ifaces->next)
4314 return 1; /* more that one interface already in use */
4315 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4316 return 1; /* this interface is already in use */
4321 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4322 const u8 *peer_addr,
4323 enum p2p_wps_method wps_method,
4324 int go_intent, const u8 *own_interface_addr,
4325 unsigned int force_freq, int persistent_group,
4326 struct wpa_ssid *ssid, unsigned int pref_freq)
4328 if (persistent_group && wpa_s->conf->persistent_reconnect)
4329 persistent_group = 2;
4332 * Increase GO config timeout if HT40 is used since it takes some time
4333 * to scan channels for coex purposes before the BSS can be started.
4335 p2p_set_config_timeout(wpa_s->global->p2p,
4336 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4338 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4339 go_intent, own_interface_addr, force_freq,
4340 persistent_group, ssid ? ssid->ssid : NULL,
4341 ssid ? ssid->ssid_len : 0,
4342 wpa_s->p2p_pd_before_go_neg, pref_freq,
4343 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4348 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4349 const u8 *peer_addr,
4350 enum p2p_wps_method wps_method,
4351 int go_intent, const u8 *own_interface_addr,
4352 unsigned int force_freq, int persistent_group,
4353 struct wpa_ssid *ssid, unsigned int pref_freq)
4355 if (persistent_group && wpa_s->conf->persistent_reconnect)
4356 persistent_group = 2;
4358 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4359 go_intent, own_interface_addr, force_freq,
4360 persistent_group, ssid ? ssid->ssid : NULL,
4361 ssid ? ssid->ssid_len : 0, pref_freq,
4362 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4367 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4369 wpa_s->p2p_join_scan_count++;
4370 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4371 wpa_s->p2p_join_scan_count);
4372 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4373 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4374 " for join operationg - stop join attempt",
4375 MAC2STR(wpa_s->pending_join_iface_addr));
4376 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4377 if (wpa_s->p2p_auto_pd) {
4378 wpa_s->p2p_auto_pd = 0;
4379 wpa_msg_global(wpa_s, MSG_INFO,
4380 P2P_EVENT_PROV_DISC_FAILURE
4381 " p2p_dev_addr=" MACSTR " status=N/A",
4382 MAC2STR(wpa_s->pending_join_dev_addr));
4385 wpa_msg_global(wpa_s->parent, MSG_INFO,
4386 P2P_EVENT_GROUP_FORMATION_FAILURE);
4391 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4394 unsigned int num, i;
4395 struct wpa_used_freq_data *freqs;
4397 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4398 /* Multiple channels are supported and not all are in use */
4402 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4403 sizeof(struct wpa_used_freq_data));
4407 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4408 wpa_s->num_multichan_concurrent);
4410 for (i = 0; i < num; i++) {
4411 if (freqs[i].freq == freq) {
4412 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4419 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4428 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4429 const u8 *peer_dev_addr)
4431 struct wpa_bss *bss;
4434 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4437 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4438 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4443 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4445 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4446 "%ld.%06ld (%supdated in last scan)",
4447 bss->last_update.sec, bss->last_update.usec,
4448 updated ? "": "not ");
4454 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4455 struct wpa_scan_results *scan_res)
4457 struct wpa_bss *bss = NULL;
4459 u8 iface_addr[ETH_ALEN];
4461 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4463 if (wpa_s->global->p2p_disabled)
4466 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4467 scan_res ? (int) scan_res->num : -1,
4468 wpa_s->p2p_auto_join ? "auto_" : "");
4471 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4473 if (wpa_s->p2p_auto_pd) {
4474 int join = wpas_p2p_peer_go(wpa_s,
4475 wpa_s->pending_join_dev_addr);
4477 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4478 wpa_s->auto_pd_scan_retry++;
4479 bss = wpa_bss_get_bssid_latest(
4480 wpa_s, wpa_s->pending_join_dev_addr);
4483 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4484 "the peer " MACSTR " at %d MHz",
4485 wpa_s->auto_pd_scan_retry,
4487 pending_join_dev_addr),
4489 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4497 wpa_s->p2p_auto_pd = 0;
4498 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4499 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4500 MAC2STR(wpa_s->pending_join_dev_addr), join);
4501 if (p2p_prov_disc_req(wpa_s->global->p2p,
4502 wpa_s->pending_join_dev_addr, NULL,
4503 wpa_s->pending_pd_config_methods, join,
4504 0, wpa_s->user_initiated_pd) < 0) {
4505 wpa_s->p2p_auto_pd = 0;
4506 wpa_msg_global(wpa_s, MSG_INFO,
4507 P2P_EVENT_PROV_DISC_FAILURE
4508 " p2p_dev_addr=" MACSTR " status=N/A",
4509 MAC2STR(wpa_s->pending_join_dev_addr));
4514 if (wpa_s->p2p_auto_join) {
4515 int join = wpas_p2p_peer_go(wpa_s,
4516 wpa_s->pending_join_dev_addr);
4518 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4519 "running a GO -> use GO Negotiation");
4520 wpa_msg_global(wpa_s->parent, MSG_INFO,
4521 P2P_EVENT_FALLBACK_TO_GO_NEG
4522 "reason=peer-not-running-GO");
4523 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4524 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4525 wpa_s->p2p_persistent_group, 0, 0, 0,
4526 wpa_s->p2p_go_intent,
4527 wpa_s->p2p_connect_freq,
4528 wpa_s->p2p_persistent_id,
4529 wpa_s->p2p_pd_before_go_neg,
4535 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4536 "try to join the group", join ? "" :
4539 wpa_msg_global(wpa_s->parent, MSG_INFO,
4540 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
4541 wpa_s->p2p_fallback_to_go_neg = 1;
4545 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4546 wpa_s->pending_join_iface_addr);
4548 p2p_get_interface_addr(wpa_s->global->p2p,
4549 wpa_s->pending_join_dev_addr,
4551 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4552 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4553 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4554 "address for join from " MACSTR " to " MACSTR
4555 " based on newly discovered P2P peer entry",
4556 MAC2STR(wpa_s->pending_join_iface_addr),
4557 MAC2STR(iface_addr));
4558 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4561 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4562 wpa_s->pending_join_iface_addr);
4565 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4566 "from P2P peer table: %d MHz", freq);
4568 if (wpa_s->p2p_join_ssid_len) {
4569 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4570 MACSTR " and SSID %s",
4571 MAC2STR(wpa_s->pending_join_iface_addr),
4572 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4573 wpa_s->p2p_join_ssid_len));
4574 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4575 wpa_s->p2p_join_ssid,
4576 wpa_s->p2p_join_ssid_len);
4579 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4580 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4581 bss = wpa_bss_get_bssid_latest(wpa_s,
4582 wpa_s->pending_join_iface_addr);
4585 u8 dev_addr[ETH_ALEN];
4588 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4589 "from BSS table: %d MHz (SSID %s)", freq,
4590 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4591 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4593 os_memcmp(wpa_s->pending_join_dev_addr,
4594 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4595 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4597 wpa_printf(MSG_DEBUG,
4598 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4600 MAC2STR(wpa_s->pending_join_dev_addr));
4601 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4608 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4609 wpa_msg_global(wpa_s->parent, MSG_INFO,
4610 P2P_EVENT_GROUP_FORMATION_FAILURE
4611 "reason=FREQ_CONFLICT");
4615 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4616 "prior to joining an existing group (GO " MACSTR
4618 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4619 wpa_s->pending_pd_before_join = 1;
4621 switch (wpa_s->pending_join_wps_method) {
4622 case WPS_PIN_DISPLAY:
4623 method = WPS_CONFIG_KEYPAD;
4625 case WPS_PIN_KEYPAD:
4626 method = WPS_CONFIG_DISPLAY;
4629 method = WPS_CONFIG_PUSHBUTTON;
4636 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4637 wpa_s->pending_join_dev_addr) ==
4640 * We have already performed provision discovery for
4641 * joining the group. Proceed directly to join
4642 * operation without duplicated provision discovery. */
4643 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4644 "with " MACSTR " already done - proceed to "
4646 MAC2STR(wpa_s->pending_join_dev_addr));
4647 wpa_s->pending_pd_before_join = 0;
4651 if (p2p_prov_disc_req(wpa_s->global->p2p,
4652 wpa_s->pending_join_dev_addr,
4654 freq, wpa_s->user_initiated_pd) < 0) {
4655 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4656 "Discovery Request before joining an "
4658 wpa_s->pending_pd_before_join = 0;
4664 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4665 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4666 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4667 wpas_p2p_check_join_scan_limit(wpa_s);
4671 /* Start join operation immediately */
4672 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4676 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4677 const u8 *ssid, size_t ssid_len)
4680 struct wpa_driver_scan_params params;
4681 struct wpabuf *wps_ie, *ies;
4683 int freqs[2] = { 0, 0 };
4685 os_memset(¶ms, 0, sizeof(params));
4687 /* P2P Wildcard SSID */
4688 params.num_ssids = 1;
4689 if (ssid && ssid_len) {
4690 params.ssids[0].ssid = ssid;
4691 params.ssids[0].ssid_len = ssid_len;
4692 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4693 wpa_s->p2p_join_ssid_len = ssid_len;
4695 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4696 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4697 wpa_s->p2p_join_ssid_len = 0;
4700 wpa_s->wps->dev.p2p = 1;
4701 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4702 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4704 if (wps_ie == NULL) {
4705 wpas_p2p_scan_res_join(wpa_s, NULL);
4709 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4710 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4712 wpabuf_free(wps_ie);
4713 wpas_p2p_scan_res_join(wpa_s, NULL);
4716 wpabuf_put_buf(ies, wps_ie);
4717 wpabuf_free(wps_ie);
4719 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4721 params.p2p_probe = 1;
4722 params.extra_ies = wpabuf_head(ies);
4723 params.extra_ies_len = wpabuf_len(ies);
4728 * If freq is not provided, check the operating freq of the GO
4729 * and use a single channel scan on if possible.
4731 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4732 wpa_s->pending_join_iface_addr);
4738 params.freqs = freqs;
4742 * Run a scan to update BSS table and start Provision Discovery once
4743 * the new scan results become available.
4745 ret = wpa_drv_scan(wpa_s, ¶ms);
4747 os_get_reltime(&wpa_s->scan_trigger_time);
4748 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4749 wpa_s->own_scan_requested = 1;
4755 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4757 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4758 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4759 wpas_p2p_check_join_scan_limit(wpa_s);
4764 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4766 struct wpa_supplicant *wpa_s = eloop_ctx;
4767 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4771 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4772 const u8 *dev_addr, enum p2p_wps_method wps_method,
4773 int auto_join, int op_freq,
4774 const u8 *ssid, size_t ssid_len)
4776 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4777 MACSTR " dev " MACSTR " op_freq=%d)%s",
4778 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4779 auto_join ? " (auto_join)" : "");
4780 if (ssid && ssid_len) {
4781 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4782 wpa_ssid_txt(ssid, ssid_len));
4785 wpa_s->p2p_auto_pd = 0;
4786 wpa_s->p2p_auto_join = !!auto_join;
4787 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4788 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4789 wpa_s->pending_join_wps_method = wps_method;
4791 /* Make sure we are not running find during connection establishment */
4792 wpas_p2p_stop_find(wpa_s);
4794 wpa_s->p2p_join_scan_count = 0;
4795 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4800 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4801 const u8 *ssid, size_t ssid_len)
4803 struct wpa_supplicant *group;
4804 struct p2p_go_neg_results res;
4805 struct wpa_bss *bss;
4807 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4810 if (group != wpa_s) {
4811 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4812 sizeof(group->p2p_pin));
4813 group->p2p_wps_method = wpa_s->p2p_wps_method;
4816 * Need to mark the current interface for p2p_group_formation
4817 * when a separate group interface is not used. This is needed
4818 * to allow p2p_cancel stop a pending p2p_connect-join.
4819 * wpas_p2p_init_group_interface() addresses this for the case
4820 * where a separate group interface is used.
4822 wpa_s->global->p2p_group_formation = wpa_s;
4825 group->p2p_in_provisioning = 1;
4826 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4828 os_memset(&res, 0, sizeof(res));
4829 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4830 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4832 res.wps_method = wpa_s->pending_join_wps_method;
4833 if (freq && ssid && ssid_len) {
4835 res.ssid_len = ssid_len;
4836 os_memcpy(res.ssid, ssid, ssid_len);
4838 bss = wpa_bss_get_bssid_latest(wpa_s,
4839 wpa_s->pending_join_iface_addr);
4841 res.freq = bss->freq;
4842 res.ssid_len = bss->ssid_len;
4843 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4844 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4846 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4850 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4851 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4853 wpa_drv_cancel_remain_on_channel(wpa_s);
4854 wpa_s->off_channel_freq = 0;
4855 wpa_s->roc_waiting_drv_freq = 0;
4857 wpas_start_wps_enrollee(group, &res);
4860 * Allow a longer timeout for join-a-running-group than normal 15
4861 * second group formation timeout since the GO may not have authorized
4862 * our connection yet.
4864 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4865 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4872 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4873 int *force_freq, int *pref_freq, int go,
4874 unsigned int *pref_freq_list,
4875 unsigned int *num_pref_freq)
4877 struct wpa_used_freq_data *freqs;
4878 int res, best_freq, num_unused;
4879 unsigned int freq_in_use = 0, num, i, max_pref_freq;
4881 max_pref_freq = *num_pref_freq;
4884 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4885 sizeof(struct wpa_used_freq_data));
4889 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4890 wpa_s->num_multichan_concurrent);
4893 * It is possible that the total number of used frequencies is bigger
4894 * than the number of frequencies used for P2P, so get the system wide
4895 * number of unused frequencies.
4897 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4899 wpa_printf(MSG_DEBUG,
4900 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4901 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4906 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4908 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4910 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
4911 ieee80211_is_dfs(freq)) {
4913 * If freq is a DFS channel and DFS is offloaded
4914 * to the driver, allow P2P GO to use it.
4916 wpa_printf(MSG_DEBUG,
4917 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
4920 wpa_printf(MSG_DEBUG,
4921 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
4928 for (i = 0; i < num; i++) {
4929 if (freqs[i].freq == freq)
4933 if (num_unused <= 0 && !freq_in_use) {
4934 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4939 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4940 "requested channel (%u MHz)", freq);
4945 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4947 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
4948 enum wpa_driver_if_type iface_type;
4951 iface_type = WPA_IF_P2P_GO;
4953 iface_type = WPA_IF_P2P_CLIENT;
4955 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
4958 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
4961 if (!res && max_pref_freq > 0) {
4962 *num_pref_freq = max_pref_freq;
4964 while (wpas_p2p_disallowed_freq(wpa_s->global,
4965 pref_freq_list[i]) &&
4966 i < *num_pref_freq) {
4967 wpa_printf(MSG_DEBUG,
4968 "P2P: preferred_freq_list[%d]=%d is disallowed",
4969 i, pref_freq_list[i]);
4972 if (i != *num_pref_freq) {
4973 best_freq = pref_freq_list[i];
4974 wpa_printf(MSG_DEBUG,
4975 "P2P: Using preferred_freq_list[%d]=%d",
4978 wpa_printf(MSG_DEBUG,
4979 "P2P: All driver preferred frequencies are disallowed for P2P use");
4983 wpa_printf(MSG_DEBUG,
4984 "P2P: No preferred frequency list available");
4988 /* We have a candidate frequency to use */
4989 if (best_freq > 0) {
4990 if (*pref_freq == 0 && num_unused > 0) {
4991 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4993 *pref_freq = best_freq;
4995 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4997 *force_freq = best_freq;
4999 } else if (num_unused > 0) {
5000 wpa_printf(MSG_DEBUG,
5001 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5004 wpa_printf(MSG_DEBUG,
5005 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5019 * wpas_p2p_connect - Request P2P Group Formation to be started
5020 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5021 * @peer_addr: Address of the peer P2P Device
5022 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5023 * @persistent_group: Whether to create a persistent group
5024 * @auto_join: Whether to select join vs. GO Negotiation automatically
5025 * @join: Whether to join an existing group (as a client) instead of starting
5026 * Group Owner negotiation; @peer_addr is BSSID in that case
5027 * @auth: Whether to only authorize the connection instead of doing that and
5028 * initiating Group Owner negotiation
5029 * @go_intent: GO Intent or -1 to use default
5030 * @freq: Frequency for the group or 0 for auto-selection
5031 * @persistent_id: Persistent group credentials to use for forcing GO
5032 * parameters or -1 to generate new values (SSID/passphrase)
5033 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5034 * interoperability workaround when initiating group formation
5035 * @ht40: Start GO with 40 MHz channel width
5036 * @vht: Start GO with VHT support
5037 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5038 * failure, -2 on failure due to channel not currently available,
5039 * -3 if forced channel is not supported
5041 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5042 const char *pin, enum p2p_wps_method wps_method,
5043 int persistent_group, int auto_join, int join, int auth,
5044 int go_intent, int freq, int persistent_id, int pd,
5047 int force_freq = 0, pref_freq = 0;
5049 enum wpa_driver_if_type iftype;
5051 struct wpa_ssid *ssid = NULL;
5052 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
5054 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5057 if (persistent_id >= 0) {
5058 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5059 if (ssid == NULL || ssid->disabled != 2 ||
5060 ssid->mode != WPAS_MODE_P2P_GO)
5064 os_free(wpa_s->global->add_psk);
5065 wpa_s->global->add_psk = NULL;
5067 wpa_s->global->p2p_fail_on_wps_complete = 0;
5068 wpa_s->global->pending_p2ps_group = 0;
5071 go_intent = wpa_s->conf->p2p_go_intent;
5074 wpa_s->p2p_long_listen = 0;
5076 wpa_s->p2p_wps_method = wps_method;
5077 wpa_s->p2p_persistent_group = !!persistent_group;
5078 wpa_s->p2p_persistent_id = persistent_id;
5079 wpa_s->p2p_go_intent = go_intent;
5080 wpa_s->p2p_connect_freq = freq;
5081 wpa_s->p2p_fallback_to_go_neg = 0;
5082 wpa_s->p2p_pd_before_go_neg = !!pd;
5083 wpa_s->p2p_go_ht40 = !!ht40;
5084 wpa_s->p2p_go_vht = !!vht;
5087 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5088 else if (wps_method == WPS_PIN_DISPLAY) {
5089 ret = wps_generate_pin();
5090 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5092 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5093 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5094 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5097 wpa_s->p2p_pin[0] = '\0';
5099 if (join || auto_join) {
5100 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5102 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5103 "connect a running group from " MACSTR,
5104 MAC2STR(peer_addr));
5105 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5108 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5109 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5111 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5112 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5116 os_get_reltime(&wpa_s->p2p_auto_started);
5117 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5119 wpa_s->p2p_auto_started.sec,
5120 wpa_s->p2p_auto_started.usec);
5122 wpa_s->user_initiated_pd = 1;
5123 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5124 auto_join, freq, NULL, 0) < 0)
5129 size = P2P_MAX_PREF_CHANNELS;
5130 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5131 go_intent == 15, pref_freq_list, &size);
5134 wpas_p2p_set_own_freq_preference(wpa_s,
5135 force_freq ? force_freq : pref_freq);
5137 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5139 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5141 if (wpa_s->create_p2p_iface) {
5142 /* Prepare to add a new interface for the group */
5143 iftype = WPA_IF_P2P_GROUP;
5144 if (go_intent == 15)
5145 iftype = WPA_IF_P2P_GO;
5146 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5147 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5148 "interface for the group");
5152 if_addr = wpa_s->pending_interface_addr;
5154 if_addr = wpa_s->own_addr;
5155 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5159 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5161 force_freq, persistent_group, ssid,
5167 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5168 go_intent, if_addr, force_freq,
5169 persistent_group, ssid, pref_freq) < 0) {
5170 if (wpa_s->create_p2p_iface)
5171 wpas_p2p_remove_pending_group_interface(wpa_s);
5179 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5180 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5181 * @freq: Frequency of the channel in MHz
5182 * @duration: Duration of the stay on the channel in milliseconds
5184 * This callback is called when the driver indicates that it has started the
5185 * requested remain-on-channel duration.
5187 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5188 unsigned int freq, unsigned int duration)
5190 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5192 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)",
5193 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5194 wpa_s->roc_waiting_drv_freq, freq, duration);
5195 if (wpa_s->off_channel_freq &&
5196 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5197 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5198 wpa_s->pending_listen_duration);
5199 wpa_s->pending_listen_freq = 0;
5201 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5202 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5208 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5210 /* Limit maximum Listen state time based on driver limitation. */
5211 if (timeout > wpa_s->max_remain_on_chan)
5212 timeout = wpa_s->max_remain_on_chan;
5214 return p2p_listen(wpa_s->global->p2p, timeout);
5219 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5220 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5221 * @freq: Frequency of the channel in MHz
5223 * This callback is called when the driver indicates that a remain-on-channel
5224 * operation has been completed, i.e., the duration on the requested channel
5227 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5230 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5231 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5232 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5233 wpas_p2p_listen_work_done(wpa_s);
5234 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5236 if (wpa_s->p2p_long_listen > 0)
5237 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5238 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5239 return; /* P2P module started a new operation */
5240 if (offchannel_pending_action_tx(wpa_s))
5242 if (wpa_s->p2p_long_listen > 0) {
5243 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5244 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5247 * When listen duration is over, stop listen & update p2p_state
5250 p2p_stop_listen(wpa_s->global->p2p);
5256 * wpas_p2p_group_remove - Remove a P2P group
5257 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5258 * @ifname: Network interface name of the group interface or "*" to remove all
5260 * Returns: 0 on success, -1 on failure
5262 * This function is used to remove a P2P group. This can be used to disconnect
5263 * from a group in which the local end is a P2P Client or to end a P2P Group in
5264 * case the local end is the Group Owner. If a virtual network interface was
5265 * created for this group, that interface will be removed. Otherwise, only the
5266 * configured P2P group network will be removed from the interface.
5268 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5270 struct wpa_global *global = wpa_s->global;
5271 struct wpa_supplicant *calling_wpa_s = wpa_s;
5273 if (os_strcmp(ifname, "*") == 0) {
5274 struct wpa_supplicant *prev;
5275 wpa_s = global->ifaces;
5278 wpa_s = wpa_s->next;
5279 if (prev->p2p_group_interface !=
5280 NOT_P2P_GROUP_INTERFACE ||
5281 (prev->current_ssid &&
5282 prev->current_ssid->p2p_group))
5283 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5288 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5289 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5293 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5297 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5302 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5304 if (wpa_s->best_24_freq > 0 &&
5305 p2p_supported_freq_go(wpa_s->global->p2p,
5306 wpa_s->best_24_freq)) {
5307 freq = wpa_s->best_24_freq;
5308 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5309 "channel: %d MHz", freq);
5311 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5313 freq = 2412 + (r % 3) * 25;
5314 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5315 "channel: %d MHz", freq);
5320 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5322 if (wpa_s->best_5_freq > 0 &&
5323 p2p_supported_freq_go(wpa_s->global->p2p,
5324 wpa_s->best_5_freq)) {
5325 freq = wpa_s->best_5_freq;
5326 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5327 "channel: %d MHz", freq);
5329 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5331 freq = 5180 + (r % 4) * 20;
5332 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5333 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5334 "5 GHz channel for P2P group");
5337 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5338 "channel: %d MHz", freq);
5342 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5343 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5344 ieee80211_is_dfs(freq)) {
5346 * If freq is a DFS channel and DFS is offloaded to the
5347 * driver, allow P2P GO to use it.
5349 wpa_printf(MSG_DEBUG, "P2P: "
5350 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5354 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5355 "(%u MHz) is not supported for P2P uses",
5364 static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5365 const struct p2p_channels *channels,
5368 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5369 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5370 freq_included(wpa_s, channels, freq))
5376 static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5377 struct p2p_go_neg_results *params,
5378 const struct p2p_channels *channels)
5382 /* first try some random selection of the social channels */
5383 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5386 for (i = 0; i < 3; i++) {
5387 params->freq = 2412 + ((r + i) % 3) * 25;
5388 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5392 /* try all other channels in operating class 81 */
5393 for (i = 0; i < 11; i++) {
5394 params->freq = 2412 + i * 5;
5396 /* skip social channels; covered in the previous loop */
5397 if (params->freq == 2412 ||
5398 params->freq == 2437 ||
5399 params->freq == 2462)
5402 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5406 /* try all channels in operating class 115 */
5407 for (i = 0; i < 4; i++) {
5408 params->freq = 5180 + i * 20;
5409 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5410 freq_included(wpa_s, channels, params->freq) &&
5411 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5415 /* try all channels in operating class 124 */
5416 for (i = 0; i < 4; i++) {
5417 params->freq = 5745 + i * 20;
5418 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5419 freq_included(wpa_s, channels, params->freq) &&
5420 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5424 /* try social channel class 180 channel 2 */
5425 params->freq = 58320 + 1 * 2160;
5426 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5427 freq_included(wpa_s, channels, params->freq) &&
5428 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5431 /* try all channels in reg. class 180 */
5432 for (i = 0; i < 4; i++) {
5433 params->freq = 58320 + i * 2160;
5434 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5435 freq_included(wpa_s, channels, params->freq) &&
5436 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5441 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
5444 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5449 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5450 struct p2p_go_neg_results *params,
5451 int freq, int ht40, int vht,
5452 const struct p2p_channels *channels)
5454 struct wpa_used_freq_data *freqs;
5456 unsigned int num, i;
5458 os_memset(params, 0, sizeof(*params));
5459 params->role_go = 1;
5460 params->ht40 = ht40;
5463 if (wpa_s->p2p_group_common_freqs_num)
5464 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5467 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5468 sizeof(struct wpa_used_freq_data));
5472 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5473 wpa_s->num_multichan_concurrent);
5475 /* try using the forced freq */
5477 if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
5478 wpa_printf(MSG_DEBUG,
5479 "P2P: Forced GO freq %d MHz not accepted",
5484 for (i = 0; i < num; i++) {
5485 if (freqs[i].freq == freq) {
5486 wpa_printf(MSG_DEBUG,
5487 "P2P: forced freq (%d MHz) is also shared",
5489 params->freq = freq;
5494 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5495 wpa_printf(MSG_DEBUG,
5496 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5501 wpa_printf(MSG_DEBUG,
5502 "P2P: force GO freq (%d MHz) on a free channel",
5504 params->freq = freq;
5508 /* consider using one of the shared frequencies */
5510 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5511 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5512 wpa_printf(MSG_DEBUG,
5513 "P2P: Use shared freq (%d MHz) for GO",
5515 params->freq = cand;
5519 /* try using one of the shared freqs */
5520 for (i = 0; i < num; i++) {
5521 if (wpas_p2p_supported_freq_go(wpa_s, channels,
5523 wpa_printf(MSG_DEBUG,
5524 "P2P: Use shared freq (%d MHz) for GO",
5526 params->freq = freqs[i].freq;
5532 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5533 wpa_printf(MSG_DEBUG,
5534 "P2P: Cannot force GO on any of the channels we are already using");
5538 /* try using the setting from the configuration file */
5539 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5540 wpa_s->conf->p2p_oper_channel >= 1 &&
5541 wpa_s->conf->p2p_oper_channel <= 11 &&
5542 wpas_p2p_supported_freq_go(
5544 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5545 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5546 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5547 "frequency %d MHz", params->freq);
5551 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5552 wpa_s->conf->p2p_oper_reg_class == 116 ||
5553 wpa_s->conf->p2p_oper_reg_class == 117 ||
5554 wpa_s->conf->p2p_oper_reg_class == 124 ||
5555 wpa_s->conf->p2p_oper_reg_class == 125 ||
5556 wpa_s->conf->p2p_oper_reg_class == 126 ||
5557 wpa_s->conf->p2p_oper_reg_class == 127) &&
5558 wpas_p2p_supported_freq_go(wpa_s, channels,
5560 5 * wpa_s->conf->p2p_oper_channel)) {
5561 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5562 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5563 "frequency %d MHz", params->freq);
5567 /* Try using best channels */
5568 if (wpa_s->conf->p2p_oper_channel == 0 &&
5569 wpa_s->best_overall_freq > 0 &&
5570 wpas_p2p_supported_freq_go(wpa_s, channels,
5571 wpa_s->best_overall_freq)) {
5572 params->freq = wpa_s->best_overall_freq;
5573 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5574 "channel %d MHz", params->freq);
5578 if (wpa_s->conf->p2p_oper_channel == 0 &&
5579 wpa_s->best_24_freq > 0 &&
5580 wpas_p2p_supported_freq_go(wpa_s, channels,
5581 wpa_s->best_24_freq)) {
5582 params->freq = wpa_s->best_24_freq;
5583 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5584 "channel %d MHz", params->freq);
5588 if (wpa_s->conf->p2p_oper_channel == 0 &&
5589 wpa_s->best_5_freq > 0 &&
5590 wpas_p2p_supported_freq_go(wpa_s, channels,
5591 wpa_s->best_5_freq)) {
5592 params->freq = wpa_s->best_5_freq;
5593 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5594 "channel %d MHz", params->freq);
5598 /* try using preferred channels */
5599 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
5600 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5601 params->freq = cand;
5602 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5603 "channels", params->freq);
5607 /* Try using one of the group common freqs */
5608 if (wpa_s->p2p_group_common_freqs) {
5609 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
5610 cand = wpa_s->p2p_group_common_freqs[i];
5611 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5612 params->freq = cand;
5613 wpa_printf(MSG_DEBUG,
5614 "P2P: Use freq %d MHz common with the peer",
5621 /* no preference, select some channel */
5622 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
5624 if (params->freq == 0) {
5625 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
5638 static struct wpa_supplicant *
5639 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5642 struct wpa_supplicant *group_wpa_s;
5644 if (!wpas_p2p_create_iface(wpa_s)) {
5645 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5647 wpa_s->p2p_first_connection_timeout = 0;
5651 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5652 WPA_IF_P2P_CLIENT) < 0) {
5653 wpa_msg_global(wpa_s, MSG_ERROR,
5654 "P2P: Failed to add group interface");
5657 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5658 if (group_wpa_s == NULL) {
5659 wpa_msg_global(wpa_s, MSG_ERROR,
5660 "P2P: Failed to initialize group interface");
5661 wpas_p2p_remove_pending_group_interface(wpa_s);
5665 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5666 group_wpa_s->ifname);
5667 group_wpa_s->p2p_first_connection_timeout = 0;
5673 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5674 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5675 * @persistent_group: Whether to create a persistent group
5676 * @freq: Frequency for the group or 0 to indicate no hardcoding
5677 * @ht40: Start GO with 40 MHz channel width
5678 * @vht: Start GO with VHT support
5679 * Returns: 0 on success, -1 on failure
5681 * This function creates a new P2P group with the local end as the Group Owner,
5682 * i.e., without using Group Owner Negotiation.
5684 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5685 int freq, int ht40, int vht)
5687 struct p2p_go_neg_results params;
5689 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5692 os_free(wpa_s->global->add_psk);
5693 wpa_s->global->add_psk = NULL;
5695 /* Make sure we are not running find during connection establishment */
5696 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5697 wpas_p2p_stop_find_oper(wpa_s);
5699 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5703 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5706 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5707 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5708 ieee80211_is_dfs(params.freq)) {
5710 * If freq is a DFS channel and DFS is offloaded to the
5711 * driver, allow P2P GO to use it.
5713 wpa_printf(MSG_DEBUG,
5714 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5715 __func__, params.freq);
5717 wpa_printf(MSG_DEBUG,
5718 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5723 p2p_go_params(wpa_s->global->p2p, ¶ms);
5724 params.persistent_group = persistent_group;
5726 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5729 wpas_start_wps_go(wpa_s, ¶ms, 0);
5735 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5736 struct wpa_ssid *params, int addr_allocated,
5739 struct wpa_ssid *ssid;
5741 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5744 wpa_s->p2p_last_4way_hs_fail = NULL;
5746 wpa_supplicant_ap_deinit(wpa_s);
5748 ssid = wpa_config_add_network(wpa_s->conf);
5751 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5752 wpa_config_set_network_defaults(ssid);
5753 ssid->temporary = 1;
5754 ssid->proto = WPA_PROTO_RSN;
5755 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5756 ssid->group_cipher = WPA_CIPHER_CCMP;
5757 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5758 ssid->ssid = os_malloc(params->ssid_len);
5759 if (ssid->ssid == NULL) {
5760 wpa_config_remove_network(wpa_s->conf, ssid->id);
5763 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5764 ssid->ssid_len = params->ssid_len;
5765 ssid->p2p_group = 1;
5766 ssid->export_keys = 1;
5767 if (params->psk_set) {
5768 os_memcpy(ssid->psk, params->psk, 32);
5771 if (params->passphrase)
5772 ssid->passphrase = os_strdup(params->passphrase);
5774 wpa_s->show_group_started = 1;
5775 wpa_s->p2p_in_invitation = 1;
5776 wpa_s->p2p_invite_go_freq = freq;
5778 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5780 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5781 wpas_p2p_group_formation_timeout,
5782 wpa_s->parent, NULL);
5783 wpa_supplicant_select_network(wpa_s, ssid);
5789 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5790 struct wpa_ssid *ssid, int addr_allocated,
5791 int force_freq, int neg_freq, int ht40,
5792 int vht, const struct p2p_channels *channels,
5793 int connection_timeout)
5795 struct p2p_go_neg_results params;
5798 if (ssid->disabled != 2 || ssid->ssid == NULL)
5801 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5802 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5803 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5806 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5807 wpa_s->parent, NULL)) {
5809 * This can happen if Invitation Response frame was lost
5810 * and the peer (GO of a persistent group) tries to
5811 * invite us again. Reschedule the timeout to avoid
5812 * terminating the wait for the connection too early
5813 * since we now know that the peer is still trying to
5814 * invite us instead of having already started the GO.
5816 wpa_printf(MSG_DEBUG,
5817 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
5818 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5819 wpas_p2p_group_formation_timeout,
5820 wpa_s->parent, NULL);
5825 os_free(wpa_s->global->add_psk);
5826 wpa_s->global->add_psk = NULL;
5828 /* Make sure we are not running find during connection establishment */
5829 wpas_p2p_stop_find_oper(wpa_s);
5831 wpa_s->p2p_fallback_to_go_neg = 0;
5833 if (ssid->mode == WPAS_MODE_P2P_GO) {
5834 if (force_freq > 0) {
5835 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5839 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5841 (freq > 0 && !freq_included(wpa_s, channels, freq)))
5844 } else if (ssid->mode == WPAS_MODE_INFRA) {
5846 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
5847 struct os_reltime now;
5848 struct wpa_bss *bss =
5849 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
5851 os_get_reltime(&now);
5853 !os_reltime_expired(&now, &bss->last_update, 5) &&
5854 freq_included(wpa_s, channels, bss->freq))
5860 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5865 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5869 params.psk_set = ssid->psk_set;
5871 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5872 if (ssid->passphrase) {
5873 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5874 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5875 "persistent group");
5878 os_strlcpy(params.passphrase, ssid->passphrase,
5879 sizeof(params.passphrase));
5881 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5882 params.ssid_len = ssid->ssid_len;
5883 params.persistent_group = 1;
5885 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5889 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5891 wpa_s->p2p_first_connection_timeout = connection_timeout;
5892 wpas_start_wps_go(wpa_s, ¶ms, 0);
5898 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5899 struct wpabuf *proberesp_ies)
5901 struct wpa_supplicant *wpa_s = ctx;
5902 if (wpa_s->ap_iface) {
5903 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5904 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5905 wpabuf_free(beacon_ies);
5906 wpabuf_free(proberesp_ies);
5910 wpabuf_free(hapd->p2p_beacon_ie);
5911 hapd->p2p_beacon_ie = beacon_ies;
5913 wpabuf_free(hapd->p2p_probe_resp_ie);
5914 hapd->p2p_probe_resp_ie = proberesp_ies;
5916 wpabuf_free(beacon_ies);
5917 wpabuf_free(proberesp_ies);
5919 wpa_supplicant_ap_update_beacon(wpa_s);
5923 static void wpas_p2p_idle_update(void *ctx, int idle)
5925 struct wpa_supplicant *wpa_s = ctx;
5926 if (!wpa_s->ap_iface)
5928 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5930 if (wpa_s->global->p2p_fail_on_wps_complete &&
5931 wpa_s->p2p_in_provisioning) {
5932 wpas_p2p_grpform_fail_after_wps(wpa_s);
5935 wpas_p2p_set_group_idle_timeout(wpa_s);
5937 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5941 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5942 struct wpa_ssid *ssid)
5944 struct p2p_group *group;
5945 struct p2p_group_config *cfg;
5947 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5950 cfg = os_zalloc(sizeof(*cfg));
5954 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5955 cfg->persistent_group = 2;
5956 else if (ssid->p2p_persistent_group)
5957 cfg->persistent_group = 1;
5958 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5959 if (wpa_s->max_stations &&
5960 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5961 cfg->max_clients = wpa_s->max_stations;
5963 cfg->max_clients = wpa_s->conf->max_num_sta;
5964 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5965 cfg->ssid_len = ssid->ssid_len;
5966 cfg->freq = ssid->frequency;
5967 cfg->cb_ctx = wpa_s;
5968 cfg->ie_update = wpas_p2p_ie_update;
5969 cfg->idle_update = wpas_p2p_idle_update;
5971 group = p2p_group_init(wpa_s->global->p2p, cfg);
5974 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5975 p2p_group_notif_formation_done(group);
5976 wpa_s->p2p_group = group;
5981 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5984 struct wpa_ssid *ssid = wpa_s->current_ssid;
5986 if (!wpa_s->p2p_in_provisioning) {
5987 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5988 "provisioning not in progress");
5992 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5993 u8 go_dev_addr[ETH_ALEN];
5994 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5995 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5997 /* Clear any stored provisioning info */
5998 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6001 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6003 wpa_s->p2p_go_group_formation_completed = 1;
6004 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6006 * Use a separate timeout for initial data connection to
6007 * complete to allow the group to be removed automatically if
6008 * something goes wrong in this step before the P2P group idle
6009 * timeout mechanism is taken into use.
6011 wpa_dbg(wpa_s, MSG_DEBUG,
6012 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6013 P2P_MAX_INITIAL_CONN_WAIT);
6014 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6015 wpas_p2p_group_formation_timeout,
6016 wpa_s->parent, NULL);
6019 * Use a separate timeout for initial data connection to
6020 * complete to allow the group to be removed automatically if
6021 * the client does not complete data connection successfully.
6023 wpa_dbg(wpa_s, MSG_DEBUG,
6024 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6025 P2P_MAX_INITIAL_CONN_WAIT_GO);
6026 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6027 wpas_p2p_group_formation_timeout,
6028 wpa_s->parent, NULL);
6030 * Complete group formation on first successful data connection
6032 wpa_s->p2p_go_group_formation_completed = 0;
6034 if (wpa_s->global->p2p)
6035 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
6036 wpas_group_formation_completed(wpa_s, 1, 0);
6040 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6041 struct wps_event_fail *fail)
6043 if (!wpa_s->p2p_in_provisioning) {
6044 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6045 "provisioning not in progress");
6049 if (wpa_s->go_params) {
6050 p2p_clear_provisioning_info(
6052 wpa_s->go_params->peer_device_addr);
6055 wpas_notify_p2p_wps_failed(wpa_s, fail);
6057 if (wpa_s == wpa_s->global->p2p_group_formation) {
6059 * Allow some time for the failed WPS negotiation exchange to
6060 * complete, but remove the group since group formation cannot
6061 * succeed after provisioning failure.
6063 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6064 wpa_s->global->p2p_fail_on_wps_complete = 1;
6065 eloop_deplete_timeout(0, 50000,
6066 wpas_p2p_group_formation_timeout,
6067 wpa_s->parent, NULL);
6072 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6074 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6075 !wpa_s->p2p_in_provisioning)
6078 wpas_p2p_grpform_fail_after_wps(wpa_s);
6084 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6085 const char *config_method,
6086 enum wpas_p2p_prov_disc_use use,
6087 struct p2ps_provision *p2ps_prov)
6091 wpa_s->global->pending_p2ps_group = 0;
6092 wpa_s->p2p_fallback_to_go_neg = 0;
6093 wpa_s->pending_pd_use = NORMAL_PD;
6094 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6095 p2ps_prov->conncap = p2ps_group_capability(
6096 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
6097 wpa_printf(MSG_DEBUG,
6098 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6099 __func__, p2ps_prov->conncap,
6100 p2ps_prov->adv_id, p2ps_prov->conncap,
6101 p2ps_prov->status, p2ps_prov->info);
6104 } else if (os_strncmp(config_method, "display", 7) == 0)
6105 config_methods = WPS_CONFIG_DISPLAY;
6106 else if (os_strncmp(config_method, "keypad", 6) == 0)
6107 config_methods = WPS_CONFIG_KEYPAD;
6108 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6109 os_strncmp(config_method, "pushbutton", 10) == 0)
6110 config_methods = WPS_CONFIG_PUSHBUTTON;
6112 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
6117 if (use == WPAS_P2P_PD_AUTO) {
6118 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6119 wpa_s->pending_pd_config_methods = config_methods;
6120 wpa_s->p2p_auto_pd = 1;
6121 wpa_s->p2p_auto_join = 0;
6122 wpa_s->pending_pd_before_join = 0;
6123 wpa_s->auto_pd_scan_retry = 0;
6124 wpas_p2p_stop_find(wpa_s);
6125 wpa_s->p2p_join_scan_count = 0;
6126 os_get_reltime(&wpa_s->p2p_auto_started);
6127 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6128 wpa_s->p2p_auto_started.sec,
6129 wpa_s->p2p_auto_started.usec);
6130 wpas_p2p_join_scan(wpa_s, NULL);
6134 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6139 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6140 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6145 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6148 return p2p_scan_result_text(ies, ies_len, buf, end);
6152 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6154 if (!offchannel_pending_action_tx(wpa_s))
6157 wpas_p2p_action_tx_clear(wpa_s);
6159 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6160 "operation request");
6161 offchannel_clear_pending_action_tx(wpa_s);
6165 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6166 enum p2p_discovery_type type,
6167 unsigned int num_req_dev_types, const u8 *req_dev_types,
6168 const u8 *dev_id, unsigned int search_delay,
6169 u8 seek_cnt, const char **seek_string, int freq)
6171 wpas_p2p_clear_pending_action_tx(wpa_s);
6172 wpa_s->p2p_long_listen = 0;
6174 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6175 wpa_s->p2p_in_provisioning)
6178 wpa_supplicant_cancel_sched_scan(wpa_s);
6180 return p2p_find(wpa_s->global->p2p, timeout, type,
6181 num_req_dev_types, req_dev_types, dev_id,
6182 search_delay, seek_cnt, seek_string, freq);
6186 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6187 struct wpa_scan_results *scan_res)
6189 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6191 if (wpa_s->p2p_scan_work) {
6192 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6193 wpa_s->p2p_scan_work = NULL;
6194 radio_work_done(work);
6197 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6201 * Indicate that results have been processed so that the P2P module can
6202 * continue pending tasks.
6204 p2p_scan_res_handled(wpa_s->global->p2p);
6208 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
6210 wpas_p2p_clear_pending_action_tx(wpa_s);
6211 wpa_s->p2p_long_listen = 0;
6212 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6213 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6215 if (wpa_s->global->p2p)
6216 p2p_stop_find(wpa_s->global->p2p);
6218 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6219 wpa_printf(MSG_DEBUG,
6220 "P2P: Do not consider the scan results after stop_find");
6221 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6226 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6228 wpas_p2p_stop_find_oper(wpa_s);
6229 if (!wpa_s->global->pending_group_iface_for_p2ps)
6230 wpas_p2p_remove_pending_group_interface(wpa_s);
6234 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6236 struct wpa_supplicant *wpa_s = eloop_ctx;
6237 wpa_s->p2p_long_listen = 0;
6241 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6245 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6248 wpa_supplicant_cancel_sched_scan(wpa_s);
6249 wpas_p2p_clear_pending_action_tx(wpa_s);
6253 * This is a request for unlimited Listen state. However, at
6254 * least for now, this is mapped to a Listen state for one
6259 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6260 wpa_s->p2p_long_listen = 0;
6263 * Stop previous find/listen operation to avoid trying to request a new
6264 * remain-on-channel operation while the driver is still running the
6267 if (wpa_s->global->p2p)
6268 p2p_stop_find(wpa_s->global->p2p);
6270 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6271 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6272 wpa_s->p2p_long_listen = timeout * 1000;
6273 eloop_register_timeout(timeout, 0,
6274 wpas_p2p_long_listen_timeout,
6282 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6283 u8 *buf, size_t len, int p2p_group)
6285 struct wpabuf *p2p_ie;
6288 if (wpa_s->global->p2p_disabled)
6291 * Advertize mandatory cross connection capability even on
6292 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6294 if (wpa_s->conf->p2p_disabled && p2p_group)
6296 if (wpa_s->global->p2p == NULL)
6301 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6302 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6304 wpabuf_free(p2p_ie);
6310 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
6311 const u8 *dst, const u8 *bssid,
6312 const u8 *ie, size_t ie_len,
6313 unsigned int rx_freq, int ssi_signal)
6315 if (wpa_s->global->p2p_disabled)
6317 if (wpa_s->global->p2p == NULL)
6320 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6321 ie, ie_len, rx_freq)) {
6322 case P2P_PREQ_NOT_P2P:
6323 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6326 case P2P_PREQ_MALFORMED:
6327 case P2P_PREQ_NOT_LISTEN:
6328 case P2P_PREQ_NOT_PROCESSED:
6329 default: /* make gcc happy */
6331 case P2P_PREQ_PROCESSED:
6337 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6338 const u8 *sa, const u8 *bssid,
6339 u8 category, const u8 *data, size_t len, int freq)
6341 if (wpa_s->global->p2p_disabled)
6343 if (wpa_s->global->p2p == NULL)
6346 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6351 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6353 if (wpa_s->global->p2p_disabled)
6355 if (wpa_s->global->p2p == NULL)
6358 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6362 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6364 p2p_group_deinit(wpa_s->p2p_group);
6365 wpa_s->p2p_group = NULL;
6367 wpa_s->ap_configured_cb = NULL;
6368 wpa_s->ap_configured_cb_ctx = NULL;
6369 wpa_s->ap_configured_cb_data = NULL;
6370 wpa_s->connect_without_scan = NULL;
6374 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6376 wpa_s->p2p_long_listen = 0;
6378 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6381 return p2p_reject(wpa_s->global->p2p, addr);
6385 /* Invite to reinvoke a persistent group */
6386 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6387 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6388 int ht40, int vht, int pref_freq)
6390 enum p2p_invite_role role;
6394 int no_pref_freq_given = pref_freq == 0;
6395 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6397 wpa_s->global->p2p_invite_group = NULL;
6399 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6401 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6403 wpa_s->p2p_persistent_go_freq = freq;
6404 wpa_s->p2p_go_ht40 = !!ht40;
6405 if (ssid->mode == WPAS_MODE_P2P_GO) {
6406 role = P2P_INVITE_ROLE_GO;
6407 if (peer_addr == NULL) {
6408 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6409 "address in invitation command");
6412 if (wpas_p2p_create_iface(wpa_s)) {
6413 if (wpas_p2p_add_group_interface(wpa_s,
6414 WPA_IF_P2P_GO) < 0) {
6415 wpa_printf(MSG_ERROR, "P2P: Failed to "
6416 "allocate a new interface for the "
6420 bssid = wpa_s->pending_interface_addr;
6422 bssid = wpa_s->own_addr;
6424 role = P2P_INVITE_ROLE_CLIENT;
6425 peer_addr = ssid->bssid;
6427 wpa_s->pending_invite_ssid_id = ssid->id;
6429 size = P2P_MAX_PREF_CHANNELS;
6430 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6431 role == P2P_INVITE_ROLE_GO,
6432 pref_freq_list, &size);
6436 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6439 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6440 no_pref_freq_given && pref_freq > 0 &&
6441 wpa_s->num_multichan_concurrent > 1 &&
6442 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6443 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6449 * Stop any find/listen operations before invitation and possibly
6450 * connection establishment.
6452 wpas_p2p_stop_find_oper(wpa_s);
6454 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6455 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6460 /* Invite to join an active group */
6461 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6462 const u8 *peer_addr, const u8 *go_dev_addr)
6464 struct wpa_global *global = wpa_s->global;
6465 enum p2p_invite_role role;
6467 struct wpa_ssid *ssid;
6469 int freq = 0, force_freq = 0, pref_freq = 0;
6471 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6473 wpa_s->p2p_persistent_go_freq = 0;
6474 wpa_s->p2p_go_ht40 = 0;
6475 wpa_s->p2p_go_vht = 0;
6477 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6478 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6481 if (wpa_s == NULL) {
6482 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6486 ssid = wpa_s->current_ssid;
6488 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6493 wpa_s->global->p2p_invite_group = wpa_s;
6494 persistent = ssid->p2p_persistent_group &&
6495 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6496 ssid->ssid, ssid->ssid_len);
6498 if (ssid->mode == WPAS_MODE_P2P_GO) {
6499 role = P2P_INVITE_ROLE_ACTIVE_GO;
6500 bssid = wpa_s->own_addr;
6501 if (go_dev_addr == NULL)
6502 go_dev_addr = wpa_s->global->p2p_dev_addr;
6503 freq = ssid->frequency;
6505 role = P2P_INVITE_ROLE_CLIENT;
6506 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6507 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6508 "invite to current group");
6511 bssid = wpa_s->bssid;
6512 if (go_dev_addr == NULL &&
6513 !is_zero_ether_addr(wpa_s->go_dev_addr))
6514 go_dev_addr = wpa_s->go_dev_addr;
6515 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6516 (int) wpa_s->assoc_freq;
6518 wpa_s->parent->pending_invite_ssid_id = -1;
6520 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6523 size = P2P_MAX_PREF_CHANNELS;
6524 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6525 role == P2P_INVITE_ROLE_ACTIVE_GO,
6526 pref_freq_list, &size);
6529 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6531 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6532 ssid->ssid, ssid->ssid_len, force_freq,
6533 go_dev_addr, persistent, pref_freq, -1);
6537 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6539 struct wpa_ssid *ssid = wpa_s->current_ssid;
6540 u8 go_dev_addr[ETH_ALEN];
6541 int network_id = -1;
6547 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6548 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6549 wpa_s->parent, NULL);
6552 if (!wpa_s->show_group_started || !ssid)
6555 wpa_s->show_group_started = 0;
6557 os_memset(go_dev_addr, 0, ETH_ALEN);
6558 if (ssid->bssid_set)
6559 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6560 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6562 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6564 if (wpa_s->global->p2p_group_formation == wpa_s)
6565 wpa_s->global->p2p_group_formation = NULL;
6567 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6568 (int) wpa_s->assoc_freq;
6571 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6574 res = os_snprintf(ip_addr, sizeof(ip_addr),
6575 " ip_addr=%u.%u.%u.%u "
6576 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6577 ip[0], ip[1], ip[2], ip[3],
6578 ip[4], ip[5], ip[6], ip[7],
6579 ip[8], ip[9], ip[10], ip[11]);
6580 if (os_snprintf_error(sizeof(ip_addr), res))
6584 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6585 ssid->passphrase == NULL && ssid->psk_set ?
6587 ssid->passphrase, go_dev_addr, persistent,
6591 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6594 network_id = ssid->id;
6595 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6599 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6600 u32 interval1, u32 duration2, u32 interval2)
6604 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6607 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6608 wpa_s->current_ssid == NULL ||
6609 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6612 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6613 wpa_s->own_addr, wpa_s->assoc_freq,
6614 duration1, interval1, duration2, interval2);
6616 wpa_s->waiting_presence_resp = 1;
6622 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6623 unsigned int interval)
6625 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6628 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6632 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6634 if (wpa_s->current_ssid == NULL) {
6636 * current_ssid can be cleared when P2P client interface gets
6637 * disconnected, so assume this interface was used as P2P
6642 return wpa_s->current_ssid->p2p_group &&
6643 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6647 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6649 struct wpa_supplicant *wpa_s = eloop_ctx;
6651 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6652 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6657 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6659 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6663 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6667 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6668 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6670 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6673 timeout = wpa_s->conf->p2p_group_idle;
6674 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6675 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6676 timeout = P2P_MAX_CLIENT_IDLE;
6682 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6683 timeout = 0; /* special client mode no-timeout */
6688 if (wpa_s->p2p_in_provisioning) {
6690 * Use the normal group formation timeout during the
6691 * provisioning phase to avoid terminating this process too
6692 * early due to group idle timeout.
6694 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6695 "during provisioning");
6699 if (wpa_s->show_group_started) {
6701 * Use the normal group formation timeout between the end of
6702 * the provisioning phase and completion of 4-way handshake to
6703 * avoid terminating this process too early due to group idle
6706 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6707 "while waiting for initial 4-way handshake to "
6712 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6714 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6719 /* Returns 1 if the interface was removed */
6720 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6721 u16 reason_code, const u8 *ie, size_t ie_len,
6722 int locally_generated)
6724 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6727 if (!locally_generated)
6728 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6731 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6732 wpa_s->current_ssid &&
6733 wpa_s->current_ssid->p2p_group &&
6734 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6735 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6736 "session is ending");
6737 if (wpas_p2p_group_delete(wpa_s,
6738 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6747 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6748 u16 reason_code, const u8 *ie, size_t ie_len,
6749 int locally_generated)
6751 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6754 if (!locally_generated)
6755 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6760 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6762 struct p2p_data *p2p = wpa_s->global->p2p;
6767 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6770 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6771 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6773 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6774 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6777 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6778 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6780 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6781 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6783 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6784 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6785 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6786 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6787 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6790 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6791 p2p_set_sec_dev_types(p2p,
6792 (void *) wpa_s->conf->sec_device_type,
6793 wpa_s->conf->num_sec_device_types);
6795 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6797 p2p_remove_wps_vendor_extensions(p2p);
6798 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6799 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6801 p2p_add_wps_vendor_extension(
6802 p2p, wpa_s->conf->wps_vendor_ext[i]);
6806 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6807 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6809 country[0] = wpa_s->conf->country[0];
6810 country[1] = wpa_s->conf->country[1];
6812 p2p_set_country(p2p, country);
6815 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6816 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6817 wpa_s->conf->p2p_ssid_postfix ?
6818 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6822 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6823 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6825 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6826 u8 reg_class, channel;
6831 if (wpa_s->conf->p2p_listen_reg_class &&
6832 wpa_s->conf->p2p_listen_channel) {
6833 reg_class = wpa_s->conf->p2p_listen_reg_class;
6834 channel = wpa_s->conf->p2p_listen_channel;
6839 * Pick one of the social channels randomly as the
6842 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6845 channel = 1 + (r % 3) * 5;
6848 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6851 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6854 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6855 u8 op_reg_class, op_channel, cfg_op_channel;
6858 if (wpa_s->conf->p2p_oper_reg_class &&
6859 wpa_s->conf->p2p_oper_channel) {
6860 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6861 op_channel = wpa_s->conf->p2p_oper_channel;
6866 * Use random operation channel from (1, 6, 11)
6867 *if no other preference is indicated.
6869 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6872 op_channel = 1 + (r % 3) * 5;
6875 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6878 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6882 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6883 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6884 wpa_s->conf->p2p_pref_chan) < 0) {
6885 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6889 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6890 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6895 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6896 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6900 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6903 if (!wpa_s->ap_iface)
6905 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6910 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6912 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6915 wpa_s->global->cross_connection = enabled;
6916 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6919 struct wpa_supplicant *iface;
6921 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6923 if (iface->cross_connect_enabled == 0)
6926 iface->cross_connect_enabled = 0;
6927 iface->cross_connect_in_use = 0;
6928 wpa_msg_global(iface->parent, MSG_INFO,
6929 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6931 iface->cross_connect_uplink);
6939 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6941 struct wpa_supplicant *iface;
6943 if (!uplink->global->cross_connection)
6946 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6947 if (!iface->cross_connect_enabled)
6949 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6952 if (iface->ap_iface == NULL)
6954 if (iface->cross_connect_in_use)
6957 iface->cross_connect_in_use = 1;
6958 wpa_msg_global(iface->parent, MSG_INFO,
6959 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6960 iface->ifname, iface->cross_connect_uplink);
6965 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6967 struct wpa_supplicant *iface;
6969 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6970 if (!iface->cross_connect_enabled)
6972 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6975 if (!iface->cross_connect_in_use)
6978 wpa_msg_global(iface->parent, MSG_INFO,
6979 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6980 iface->ifname, iface->cross_connect_uplink);
6981 iface->cross_connect_in_use = 0;
6986 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6988 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6989 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6990 wpa_s->cross_connect_disallowed)
6991 wpas_p2p_disable_cross_connect(wpa_s);
6993 wpas_p2p_enable_cross_connect(wpa_s);
6994 if (!wpa_s->ap_iface &&
6995 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6996 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7000 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7002 wpas_p2p_disable_cross_connect(wpa_s);
7003 if (!wpa_s->ap_iface &&
7004 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7006 wpas_p2p_set_group_idle_timeout(wpa_s);
7010 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7012 struct wpa_supplicant *iface;
7014 if (!wpa_s->global->cross_connection)
7017 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7020 if (iface->drv_flags &
7021 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7023 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7024 iface != wpa_s->parent)
7027 wpa_s->cross_connect_enabled = 1;
7028 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7029 sizeof(wpa_s->cross_connect_uplink));
7030 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7031 "%s to %s whenever uplink is available",
7032 wpa_s->ifname, wpa_s->cross_connect_uplink);
7034 if (iface->ap_iface || iface->current_ssid == NULL ||
7035 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7036 iface->cross_connect_disallowed ||
7037 iface->wpa_state != WPA_COMPLETED)
7040 wpa_s->cross_connect_in_use = 1;
7041 wpa_msg_global(wpa_s->parent, MSG_INFO,
7042 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7043 wpa_s->ifname, wpa_s->cross_connect_uplink);
7049 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7051 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7052 !wpa_s->p2p_in_provisioning)
7053 return 0; /* not P2P client operation */
7055 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7057 if (wpa_s != wpa_s->parent)
7058 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
7059 wpas_p2p_group_formation_failed(wpa_s, 0);
7064 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7066 struct wpa_supplicant *wpa_s = eloop_ctx;
7067 wpas_p2p_notif_pbc_overlap(wpa_s);
7071 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
7073 struct p2p_channels chan, cli_chan;
7074 struct wpa_used_freq_data *freqs = NULL;
7075 unsigned int num = wpa_s->num_multichan_concurrent;
7077 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7080 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7084 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7086 os_memset(&chan, 0, sizeof(chan));
7087 os_memset(&cli_chan, 0, sizeof(cli_chan));
7088 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
7089 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7094 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
7096 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7099 * The used frequencies map changed, so it is possible that a GO is
7100 * using a channel that is no longer valid for P2P use. It is also
7101 * possible that due to policy consideration, it would be preferable to
7102 * move it to a frequency already used by other station interfaces.
7104 wpas_p2p_consider_moving_gos(wpa_s, freqs, num);
7110 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7111 struct wpa_scan_results *scan_res)
7113 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7117 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7119 struct wpa_global *global = wpa_s->global;
7123 if (global->p2p == NULL)
7126 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7128 if (wpa_s->pending_interface_name[0] &&
7129 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7132 peer = p2p_get_go_neg_peer(global->p2p);
7134 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7135 MACSTR, MAC2STR(peer));
7136 p2p_unauthorize(global->p2p, peer);
7140 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7141 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7142 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7146 if (wpa_s->pending_pd_before_join) {
7147 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7148 wpa_s->pending_pd_before_join = 0;
7152 wpas_p2p_stop_find(wpa_s);
7154 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7155 if (wpa_s == global->p2p_group_formation &&
7156 (wpa_s->p2p_in_provisioning ||
7157 wpa_s->parent->pending_interface_type ==
7158 WPA_IF_P2P_CLIENT)) {
7159 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7160 "formation found - cancelling",
7163 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7164 wpa_s->parent, NULL);
7165 if (wpa_s->p2p_in_provisioning) {
7166 wpas_group_formation_completed(wpa_s, 0, 0);
7169 wpas_p2p_group_delete(wpa_s,
7170 P2P_GROUP_REMOVAL_REQUESTED);
7172 } else if (wpa_s->p2p_in_invitation) {
7173 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7176 wpas_p2p_group_formation_failed(wpa_s, 0);
7181 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7189 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7191 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7194 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7195 "being available anymore");
7196 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
7200 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7201 int freq_24, int freq_5, int freq_overall)
7203 struct p2p_data *p2p = wpa_s->global->p2p;
7206 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7210 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7213 struct p2p_data *p2p = wpa_s->global->p2p;
7218 if (hwaddr_aton(addr, peer))
7221 return p2p_unauthorize(p2p, peer);
7226 * wpas_p2p_disconnect - Disconnect from a P2P Group
7227 * @wpa_s: Pointer to wpa_supplicant data
7228 * Returns: 0 on success, -1 on failure
7230 * This can be used to disconnect from a group in which the local end is a P2P
7231 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7232 * virtual network interface was created for this group, that interface will be
7233 * removed. Otherwise, only the configured P2P group network will be removed
7234 * from the interface.
7236 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7242 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7247 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7251 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7254 ret = p2p_in_progress(wpa_s->global->p2p);
7257 * Check whether there is an ongoing WPS provisioning step (or
7258 * other parts of group formation) on another interface since
7259 * p2p_in_progress() does not report this to avoid issues for
7260 * scans during such provisioning step.
7262 if (wpa_s->global->p2p_group_formation &&
7263 wpa_s->global->p2p_group_formation != wpa_s) {
7264 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7265 "in group formation",
7266 wpa_s->global->p2p_group_formation->ifname);
7271 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
7272 struct os_reltime now;
7273 os_get_reltime(&now);
7274 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7275 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
7276 /* Wait for the first client has expired */
7277 wpa_s->global->p2p_go_wait_client.sec = 0;
7279 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7288 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7289 struct wpa_ssid *ssid)
7291 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7292 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7293 wpa_s->parent, NULL) > 0) {
7295 * Remove the network by scheduling the group formation
7296 * timeout to happen immediately. The teardown code
7297 * needs to be scheduled to run asynch later so that we
7298 * don't delete data from under ourselves unexpectedly.
7299 * Calling wpas_p2p_group_formation_timeout directly
7300 * causes a series of crashes in WPS failure scenarios.
7302 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7303 "P2P group network getting removed");
7304 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7305 wpa_s->parent, NULL);
7310 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
7311 const u8 *addr, const u8 *ssid,
7317 for (s = wpa_s->conf->ssid; s; s = s->next) {
7318 if (s->disabled != 2)
7321 (ssid_len != s->ssid_len ||
7322 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7325 if (s->mode == WPAS_MODE_P2P_GO)
7329 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7330 return s; /* peer is GO in the persistent group */
7331 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7333 for (i = 0; i < s->num_p2p_clients; i++) {
7334 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
7335 addr, ETH_ALEN) == 0)
7336 return s; /* peer is P2P client in persistent
7345 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7348 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7349 wpa_s->parent, NULL) > 0) {
7351 * This can happen if WPS provisioning step is not terminated
7352 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7353 * peer was able to connect, there is no need to time out group
7354 * formation after this, though. In addition, this is used with
7355 * the initial connection wait on the GO as a separate formation
7356 * timeout and as such, expected to be hit after the initial WPS
7357 * provisioning step.
7359 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7361 if (!wpa_s->p2p_go_group_formation_completed &&
7362 !wpa_s->group_formation_reported) {
7364 * GO has not yet notified group formation success since
7365 * the WPS step was not completed cleanly. Do that
7366 * notification now since the P2P Client was able to
7367 * connect and as such, must have received the
7368 * credential from the WPS step.
7370 if (wpa_s->global->p2p)
7371 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7372 wpas_group_formation_completed(wpa_s, 1, 0);
7375 if (!wpa_s->p2p_go_group_formation_completed) {
7376 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7377 wpa_s->p2p_go_group_formation_completed = 1;
7378 wpa_s->global->p2p_group_formation = NULL;
7379 wpa_s->p2p_in_provisioning = 0;
7380 wpa_s->p2p_in_invitation = 0;
7382 wpa_s->global->p2p_go_wait_client.sec = 0;
7385 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7389 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7392 struct wpa_supplicant *group = wpa_s;
7395 if (wpa_s->global->p2p_group_formation)
7396 group = wpa_s->global->p2p_group_formation;
7397 wpa_s = wpa_s->global->p2p_init_wpa_s;
7398 offchannel_send_action_done(wpa_s);
7400 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7401 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7402 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7403 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7404 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7405 wpa_s->p2p_persistent_id,
7406 wpa_s->p2p_pd_before_go_neg,
7413 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7417 if (!wpa_s->p2p_fallback_to_go_neg ||
7418 wpa_s->p2p_in_provisioning <= 5)
7421 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7422 return 0; /* peer operating as a GO */
7424 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7425 "fallback to GO Negotiation");
7426 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7427 "reason=GO-not-found");
7428 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7430 return res == 1 ? 2 : 1;
7434 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7436 struct wpa_supplicant *ifs;
7438 if (wpa_s->wpa_state > WPA_SCANNING) {
7439 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7440 "concurrent operation",
7441 wpa_s->conf->p2p_search_delay);
7442 return wpa_s->conf->p2p_search_delay;
7445 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7447 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7448 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7449 "delay due to concurrent operation on "
7451 wpa_s->conf->p2p_search_delay,
7453 return wpa_s->conf->p2p_search_delay;
7461 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7462 struct wpa_ssid *s, const u8 *addr,
7465 struct psk_list_entry *psk, *tmp;
7468 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7470 if ((iface_addr && !psk->p2p &&
7471 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7472 (!iface_addr && psk->p2p &&
7473 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7474 wpa_dbg(wpa_s, MSG_DEBUG,
7475 "P2P: Remove persistent group PSK list entry for "
7477 MAC2STR(psk->addr), psk->p2p);
7478 dl_list_del(&psk->list);
7488 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7489 const u8 *p2p_dev_addr,
7490 const u8 *psk, size_t psk_len)
7492 struct wpa_ssid *ssid = wpa_s->current_ssid;
7493 struct wpa_ssid *persistent;
7494 struct psk_list_entry *p, *last;
7496 if (psk_len != sizeof(p->psk))
7500 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7501 " p2p_dev_addr=" MACSTR,
7502 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7503 if (is_zero_ether_addr(p2p_dev_addr))
7504 p2p_dev_addr = NULL;
7506 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7510 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7511 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7512 /* To be added to persistent group once created */
7513 if (wpa_s->global->add_psk == NULL) {
7514 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7515 if (wpa_s->global->add_psk == NULL)
7518 p = wpa_s->global->add_psk;
7521 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7524 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7526 os_memcpy(p->psk, psk, psk_len);
7530 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7531 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7535 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7538 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7542 p = os_zalloc(sizeof(*p));
7547 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7550 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7552 os_memcpy(p->psk, psk, psk_len);
7554 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7555 (last = dl_list_last(&persistent->psk_list,
7556 struct psk_list_entry, list))) {
7557 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7558 MACSTR " (p2p=%u) to make room for a new one",
7559 MAC2STR(last->addr), last->p2p);
7560 dl_list_del(&last->list);
7564 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7565 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7566 p2p_dev_addr == NULL);
7568 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7569 MACSTR, MAC2STR(p2p_dev_addr));
7571 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7574 dl_list_add(&persistent->psk_list, &p->list);
7576 if (wpa_s->parent->conf->update_config &&
7577 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7578 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7582 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7583 struct wpa_ssid *s, const u8 *addr,
7588 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7589 if (res > 0 && wpa_s->conf->update_config &&
7590 wpa_config_write(wpa_s->confname, wpa_s->conf))
7591 wpa_dbg(wpa_s, MSG_DEBUG,
7592 "P2P: Failed to update configuration");
7596 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7597 const u8 *peer, int iface_addr)
7599 struct hostapd_data *hapd;
7600 struct hostapd_wpa_psk *psk, *prev, *rem;
7601 struct sta_info *sta;
7603 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7604 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7607 /* Remove per-station PSK entry */
7608 hapd = wpa_s->ap_iface->bss[0];
7610 psk = hapd->conf->ssid.wpa_psk;
7612 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7614 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7615 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7616 MACSTR " iface_addr=%d",
7617 MAC2STR(peer), iface_addr);
7619 prev->next = psk->next;
7621 hapd->conf->ssid.wpa_psk = psk->next;
7631 /* Disconnect from group */
7633 sta = ap_get_sta(hapd, peer);
7635 sta = ap_get_sta_p2p(hapd, peer);
7637 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7638 " (iface_addr=%d) from group",
7639 MAC2STR(peer), iface_addr);
7640 hostapd_drv_sta_deauth(hapd, sta->addr,
7641 WLAN_REASON_DEAUTH_LEAVING);
7642 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7647 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7651 struct wpa_supplicant *w;
7652 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
7654 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7656 /* Remove from any persistent group */
7657 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
7658 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7661 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7662 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
7665 /* Remove from any operating group */
7666 for (w = wpa_s->global->ifaces; w; w = w->next)
7667 wpas_p2p_remove_client_go(w, peer, iface_addr);
7671 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7673 struct wpa_supplicant *wpa_s = eloop_ctx;
7674 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7678 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7680 struct wpa_supplicant *wpa_s = eloop_ctx;
7682 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7683 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7687 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7688 struct wpa_ssid *ssid)
7690 struct wpa_supplicant *iface;
7692 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7693 if (!iface->current_ssid ||
7694 iface->current_ssid->frequency == freq ||
7695 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7696 !iface->current_ssid->p2p_group))
7699 /* Remove the connection with least priority */
7700 if (!wpas_is_p2p_prioritized(iface)) {
7701 /* STA connection has priority over existing
7702 * P2P connection, so remove the interface. */
7703 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7704 eloop_register_timeout(0, 0,
7705 wpas_p2p_group_freq_conflict,
7707 /* If connection in progress is P2P connection, do not
7708 * proceed for the connection. */
7714 /* P2P connection has priority, disable the STA network
7716 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7718 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7719 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7720 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7722 /* If P2P connection is in progress, continue
7735 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7737 struct wpa_ssid *ssid = wpa_s->current_ssid;
7739 if (ssid == NULL || !ssid->p2p_group)
7742 if (wpa_s->p2p_last_4way_hs_fail &&
7743 wpa_s->p2p_last_4way_hs_fail == ssid) {
7744 u8 go_dev_addr[ETH_ALEN];
7745 struct wpa_ssid *persistent;
7747 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7749 ssid->ssid_len) <= 0) {
7750 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7754 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7755 MACSTR, MAC2STR(go_dev_addr));
7756 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7759 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7760 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7763 wpa_msg_global(wpa_s->parent, MSG_INFO,
7764 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7767 wpa_s->p2p_last_4way_hs_fail = NULL;
7769 * Remove the group from a timeout to avoid issues with caller
7770 * continuing to use the interface if this is on a P2P group
7773 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7778 wpa_s->p2p_last_4way_hs_fail = ssid;
7783 #ifdef CONFIG_WPS_NFC
7785 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7793 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7797 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7798 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7805 wpabuf_put_be16(ret, wsc_len);
7807 wpabuf_put_buf(ret, wsc);
7808 wpabuf_put_be16(ret, wpabuf_len(p2p));
7809 wpabuf_put_buf(ret, p2p);
7813 wpa_hexdump_buf(MSG_DEBUG,
7814 "P2P: Generated NFC connection handover message", ret);
7818 tmp = ndef_build_p2p(ret);
7821 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7831 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7832 struct wpa_ssid **ssid, u8 *go_dev_addr)
7834 struct wpa_supplicant *iface;
7837 os_memset(go_dev_addr, 0, ETH_ALEN);
7840 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7841 if (iface->wpa_state < WPA_ASSOCIATING ||
7842 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7843 !iface->current_ssid->p2p_group ||
7844 iface->current_ssid->mode != WPAS_MODE_INFRA)
7847 *ssid = iface->current_ssid;
7849 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7850 return iface->assoc_freq;
7856 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7859 struct wpabuf *wsc, *p2p;
7860 struct wpa_ssid *ssid;
7861 u8 go_dev_addr[ETH_ALEN];
7862 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7864 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7865 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7869 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7870 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7871 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7872 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7876 if (cli_freq == 0) {
7877 wsc = wps_build_nfc_handover_req_p2p(
7878 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7881 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7882 go_dev_addr, ssid ? ssid->ssid : NULL,
7883 ssid ? ssid->ssid_len : 0);
7885 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7889 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7892 struct wpabuf *wsc, *p2p;
7893 struct wpa_ssid *ssid;
7894 u8 go_dev_addr[ETH_ALEN];
7895 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7897 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7900 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7901 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7902 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7905 if (cli_freq == 0) {
7906 wsc = wps_build_nfc_handover_sel_p2p(
7908 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7909 DEV_PW_NFC_CONNECTION_HANDOVER,
7910 wpa_s->conf->wps_nfc_dh_pubkey,
7911 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7914 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7915 go_dev_addr, ssid ? ssid->ssid : NULL,
7916 ssid ? ssid->ssid_len : 0);
7918 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7922 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7923 struct p2p_nfc_params *params)
7925 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7926 "connection handover (freq=%d)",
7929 if (params->go_freq && params->go_ssid_len) {
7930 wpa_s->p2p_wps_method = WPS_NFC;
7931 wpa_s->pending_join_wps_method = WPS_NFC;
7932 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7933 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7935 return wpas_p2p_join_start(wpa_s, params->go_freq,
7937 params->go_ssid_len);
7940 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7941 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7942 params->go_freq, -1, 0, 1, 1);
7946 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7947 struct p2p_nfc_params *params, int tag)
7949 int res, persistent;
7950 struct wpa_ssid *ssid;
7952 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7953 "connection handover");
7954 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7955 ssid = wpa_s->current_ssid;
7958 if (ssid->mode != WPAS_MODE_P2P_GO)
7960 if (wpa_s->ap_iface == NULL)
7964 if (wpa_s == NULL) {
7965 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7969 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7970 DEV_PW_NFC_CONNECTION_HANDOVER &&
7971 !wpa_s->parent->p2p_oob_dev_pw) {
7972 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7975 res = wpas_ap_wps_add_nfc_pw(
7976 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7977 wpa_s->parent->p2p_oob_dev_pw,
7978 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7979 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7984 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7988 if (!params->peer ||
7989 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7992 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7993 " to join", MAC2STR(params->peer->p2p_device_addr));
7995 wpa_s->global->p2p_invite_group = wpa_s;
7996 persistent = ssid->p2p_persistent_group &&
7997 wpas_p2p_get_persistent(wpa_s->parent,
7998 params->peer->p2p_device_addr,
7999 ssid->ssid, ssid->ssid_len);
8000 wpa_s->parent->pending_invite_ssid_id = -1;
8002 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8003 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8004 ssid->ssid, ssid->ssid_len, ssid->frequency,
8005 wpa_s->global->p2p_dev_addr, persistent, 0,
8006 wpa_s->parent->p2p_oob_dev_pw_id);
8010 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8011 struct p2p_nfc_params *params,
8014 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8015 "connection handover");
8016 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8017 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8018 forced_freq, -1, 0, 1, 1);
8022 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8023 struct p2p_nfc_params *params,
8028 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8029 "connection handover");
8030 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8031 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8032 forced_freq, -1, 0, 1, 1);
8036 res = wpas_p2p_listen(wpa_s, 60);
8038 p2p_unauthorize(wpa_s->global->p2p,
8039 params->peer->p2p_device_addr);
8046 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8047 const struct wpabuf *data,
8048 int sel, int tag, int forced_freq)
8050 const u8 *pos, *end;
8052 struct p2p_nfc_params params;
8055 os_memset(¶ms, 0, sizeof(params));
8058 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8060 pos = wpabuf_head(data);
8061 end = pos + wpabuf_len(data);
8063 if (end - pos < 2) {
8064 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8068 len = WPA_GET_BE16(pos);
8070 if (len > end - pos) {
8071 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8075 params.wsc_attr = pos;
8076 params.wsc_len = len;
8079 if (end - pos < 2) {
8080 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8084 len = WPA_GET_BE16(pos);
8086 if (len > end - pos) {
8087 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8091 params.p2p_attr = pos;
8092 params.p2p_len = len;
8095 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8096 params.wsc_attr, params.wsc_len);
8097 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8098 params.p2p_attr, params.p2p_len);
8100 wpa_hexdump(MSG_DEBUG,
8101 "P2P: Ignored extra data after P2P attributes",
8105 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
8109 if (params.next_step == NO_ACTION)
8112 if (params.next_step == BOTH_GO) {
8113 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8114 MAC2STR(params.peer->p2p_device_addr));
8118 if (params.next_step == PEER_CLIENT) {
8119 if (!is_zero_ether_addr(params.go_dev_addr)) {
8120 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8121 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8123 MAC2STR(params.peer->p2p_device_addr),
8125 MAC2STR(params.go_dev_addr),
8126 wpa_ssid_txt(params.go_ssid,
8127 params.go_ssid_len));
8129 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8130 "peer=" MACSTR " freq=%d",
8131 MAC2STR(params.peer->p2p_device_addr),
8137 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8138 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8139 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8143 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8144 wpa_s->p2p_oob_dev_pw = NULL;
8146 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8147 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8152 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8153 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8154 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8155 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8156 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8157 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8158 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8162 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8163 "peer OOB Device Password Id %u", id);
8166 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8167 "Device Password Id %u", id);
8168 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8169 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8170 params.oob_dev_pw_len -
8171 WPS_OOB_PUBKEY_HASH_LEN - 2);
8172 wpa_s->p2p_oob_dev_pw_id = id;
8173 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8174 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8175 params.oob_dev_pw_len -
8176 WPS_OOB_PUBKEY_HASH_LEN - 2);
8177 if (wpa_s->p2p_oob_dev_pw == NULL)
8180 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8181 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8182 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8185 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8186 "without Device Password");
8187 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8190 switch (params.next_step) {
8194 /* already covered above */
8197 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
8199 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
8201 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
8203 /* TODO: use own OOB Dev Pw */
8204 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
8211 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8212 const struct wpabuf *data, int forced_freq)
8214 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8217 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8221 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8222 const struct wpabuf *req,
8223 const struct wpabuf *sel, int forced_freq)
8228 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8231 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8233 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8234 wpabuf_head(req), wpabuf_len(req));
8235 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8236 wpabuf_head(sel), wpabuf_len(sel));
8238 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8239 tmp = ndef_parse_p2p(init ? sel : req);
8241 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8245 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8253 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8256 int go_intent = wpa_s->conf->p2p_go_intent;
8257 struct wpa_supplicant *iface;
8259 if (wpa_s->global->p2p == NULL)
8263 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8264 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8266 if (!iface->ap_iface)
8268 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8270 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8272 if (wpa_s->p2p_nfc_tag_enabled)
8273 wpas_p2p_remove_pending_group_interface(wpa_s);
8274 wpa_s->p2p_nfc_tag_enabled = 0;
8278 if (wpa_s->global->p2p_disabled)
8281 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8282 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8283 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8284 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8285 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8286 "to allow static handover cases");
8290 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8292 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8293 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8294 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8295 if (wpa_s->p2p_oob_dev_pw == NULL)
8297 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8299 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8300 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8302 * P2P Group Interface present and the command came on group
8303 * interface, so enable the token for the current interface.
8305 wpa_s->create_p2p_iface = 0;
8307 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8310 if (wpa_s->create_p2p_iface) {
8311 enum wpa_driver_if_type iftype;
8312 /* Prepare to add a new interface for the group */
8313 iftype = WPA_IF_P2P_GROUP;
8314 if (go_intent == 15)
8315 iftype = WPA_IF_P2P_GO;
8316 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8317 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8318 "interface for the group");
8322 if_addr = wpa_s->pending_interface_addr;
8324 if_addr = wpa_s->own_addr;
8326 wpa_s->p2p_nfc_tag_enabled = enabled;
8328 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8329 struct hostapd_data *hapd;
8330 if (iface->ap_iface == NULL)
8332 hapd = iface->ap_iface->bss[0];
8333 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8334 hapd->conf->wps_nfc_dh_pubkey =
8335 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8336 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8337 hapd->conf->wps_nfc_dh_privkey =
8338 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8339 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8340 hapd->conf->wps_nfc_dev_pw =
8341 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8342 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8344 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8345 wpa_dbg(iface, MSG_DEBUG,
8346 "P2P: Failed to enable NFC Tag for GO");
8349 p2p_set_authorized_oob_dev_pw_id(
8350 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8356 #endif /* CONFIG_WPS_NFC */
8359 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8360 struct wpa_used_freq_data *freqs,
8363 u8 curr_chan, cand, chan;
8367 * If possible, optimize the Listen channel to be a channel that is
8368 * already used by one of the other interfaces.
8370 if (!wpa_s->conf->p2p_optimize_listen_chan)
8373 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8376 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8377 for (i = 0, cand = 0; i < num; i++) {
8378 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8379 if (curr_chan == chan) {
8384 if (chan == 1 || chan == 6 || chan == 11)
8389 wpa_dbg(wpa_s, MSG_DEBUG,
8390 "P2P: Update Listen channel to %u based on operating channel",
8392 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8397 static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
8399 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8400 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8404 /* TODO: Add CSA support */
8405 wpa_dbg(wpa_s, MSG_DEBUG, "Moving GO with CSA is not implemented");
8410 static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
8412 struct p2p_go_neg_results params;
8413 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8415 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
8417 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
8418 current_ssid->frequency);
8420 /* Stop the AP functionality */
8421 /* TODO: Should do this in a way that does not indicated to possible
8422 * P2P Clients in the group that the group is terminated. */
8423 wpa_supplicant_ap_deinit(wpa_s);
8425 /* Reselect the GO frequency */
8426 if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, NULL)) {
8427 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
8428 wpas_p2p_group_delete(wpa_s,
8429 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8432 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
8436 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
8437 wpa_printf(MSG_DEBUG,
8438 "P2P: Selected freq (%u MHz) is not valid for P2P",
8440 wpas_p2p_group_delete(wpa_s,
8441 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8445 /* Update the frequency */
8446 current_ssid->frequency = params.freq;
8447 wpa_s->connect_without_scan = current_ssid;
8448 wpa_s->reassociate = 1;
8449 wpa_s->disconnected = 0;
8450 wpa_supplicant_req_scan(wpa_s, 0, 0);
8454 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
8456 struct wpa_supplicant *wpa_s = eloop_ctx;
8458 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
8461 wpas_p2p_go_update_common_freqs(wpa_s);
8464 * First, try a channel switch flow. If it is not supported or fails,
8465 * take down the GO and bring it up again.
8467 if (wpas_p2p_move_go_csa(wpa_s) < 0)
8468 wpas_p2p_move_go_no_csa(wpa_s);
8473 * Consider moving a GO from its currently used frequency:
8474 * 1. It is possible that due to regulatory consideration the frequency
8475 * can no longer be used and there is a need to evacuate the GO.
8476 * 2. It is possible that due to MCC considerations, it would be preferable
8477 * to move the GO to a channel that is currently used by some other
8478 * station interface.
8480 * In case a frequency that became invalid is once again valid, cancel a
8481 * previously initiated GO frequency change.
8483 static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
8484 struct wpa_used_freq_data *freqs,
8487 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
8488 unsigned int timeout;
8491 wpas_p2p_go_update_common_freqs(wpa_s);
8493 freq = wpa_s->current_ssid->frequency;
8494 for (i = 0, invalid_freq = 0; i < num; i++) {
8495 if (freqs[i].freq == freq) {
8496 flags = freqs[i].flags;
8498 /* The channel is invalid, must change it */
8499 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
8500 wpa_dbg(wpa_s, MSG_DEBUG,
8501 "P2P: Freq=%d MHz no longer valid for GO",
8505 } else if (freqs[i].flags == 0) {
8506 /* Freq is not used by any other station interface */
8508 } else if (!p2p_supported_freq(wpa_s->global->p2p,
8510 /* Freq is not valid for P2P use cases */
8512 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8513 P2P_GO_FREQ_MOVE_SCM) {
8515 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8516 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
8517 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
8522 wpa_dbg(wpa_s, MSG_DEBUG,
8523 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
8524 invalid_freq, policy_move, flags);
8527 * The channel is valid, or we are going to have a policy move, so
8530 if (!invalid_freq || policy_move) {
8531 wpa_dbg(wpa_s, MSG_DEBUG,
8532 "P2P: Cancel a GO move from freq=%d MHz", freq);
8533 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8536 if (!invalid_freq && (!policy_move || flags != 0)) {
8537 wpa_dbg(wpa_s, MSG_DEBUG,
8538 "P2P: Not initiating a GO frequency change");
8542 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
8543 timeout = P2P_GO_FREQ_CHANGE_TIME;
8547 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
8549 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8550 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
8554 static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
8555 struct wpa_used_freq_data *freqs,
8558 struct wpa_supplicant *ifs;
8561 * Travers all the radio interfaces, and for each GO interface, check
8562 * if there is a need to move the GO from the frequency it is using,
8563 * or in case the frequency is valid again, cancel the evacuation flow.
8565 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8567 if (ifs->current_ssid == NULL ||
8568 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
8571 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
8576 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8578 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8581 wpas_p2p_update_channel_list(wpa_s);
8585 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8587 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8588 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8589 "the management interface is being removed");
8590 wpas_p2p_deinit_global(wpa_s->global);
8595 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8597 if (wpa_s->ap_iface->bss)
8598 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8599 wpas_p2p_group_deinit(wpa_s);