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-"
95 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
98 #define P2P_RECONSIDER_GO_MOVE_DELAY 30
100 enum p2p_group_removal_reason {
101 P2P_GROUP_REMOVAL_UNKNOWN,
102 P2P_GROUP_REMOVAL_SILENT,
103 P2P_GROUP_REMOVAL_FORMATION_FAILED,
104 P2P_GROUP_REMOVAL_REQUESTED,
105 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
106 P2P_GROUP_REMOVAL_UNAVAILABLE,
107 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
108 P2P_GROUP_REMOVAL_PSK_FAILURE,
109 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
110 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
114 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
115 static struct wpa_supplicant *
116 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
118 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
119 const u8 *ssid, size_t ssid_len);
120 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
121 const u8 *ssid, size_t ssid_len);
122 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
123 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
124 const u8 *dev_addr, enum p2p_wps_method wps_method,
125 int auto_join, int freq,
126 const u8 *ssid, size_t ssid_len);
127 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
128 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
129 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
130 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
131 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
133 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
134 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
136 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
137 static void wpas_stop_listen(void *ctx);
138 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
139 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
140 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
141 enum wpa_driver_if_type type);
142 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
143 int already_deleted);
144 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
145 struct wpa_used_freq_data *freqs,
147 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
148 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
150 wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
151 struct wpa_used_freq_data *freqs, unsigned int num,
152 enum wpas_p2p_channel_update_trig trig);
153 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
157 * Get the number of concurrent channels that the HW can operate, but that are
158 * currently not in use by any of the wpa_supplicant interfaces.
160 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
165 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
169 num = get_shared_radio_freqs(wpa_s, freqs,
170 wpa_s->num_multichan_concurrent);
173 unused = wpa_s->num_multichan_concurrent - num;
174 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
180 * Get the frequencies that are currently in use by one or more of the virtual
181 * interfaces, and that are also valid for P2P operation.
184 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
185 struct wpa_used_freq_data *p2p_freqs,
188 struct wpa_used_freq_data *freqs;
189 unsigned int num, i, j;
191 freqs = os_calloc(wpa_s->num_multichan_concurrent,
192 sizeof(struct wpa_used_freq_data));
196 num = get_shared_radio_freqs_data(wpa_s, freqs,
197 wpa_s->num_multichan_concurrent);
199 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
201 for (i = 0, j = 0; i < num && j < len; i++) {
202 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
203 p2p_freqs[j++] = freqs[i];
208 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
214 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
217 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
220 /* Use the wpa_s used to control the P2P Device operation */
221 wpa_s = wpa_s->global->p2p_init_wpa_s;
223 if (wpa_s->conf->p2p_ignore_shared_freq &&
224 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
225 wpas_p2p_num_unused_channels(wpa_s) > 0) {
226 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
230 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
234 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
235 struct wpa_scan_results *scan_res)
239 if (wpa_s->p2p_scan_work) {
240 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
241 wpa_s->p2p_scan_work = NULL;
242 radio_work_done(work);
245 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
248 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
249 (int) scan_res->num);
251 for (i = 0; i < scan_res->num; i++) {
252 struct wpa_scan_res *bss = scan_res->res[i];
253 struct os_reltime time_tmp_age, entry_ts;
257 time_tmp_age.sec = bss->age / 1000;
258 time_tmp_age.usec = (bss->age % 1000) * 1000;
259 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
261 ies = (const u8 *) (bss + 1);
262 ies_len = bss->ie_len;
263 if (bss->beacon_ie_len > 0 &&
264 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
265 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
266 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
267 MACSTR, MAC2STR(bss->bssid));
269 ies_len = bss->beacon_ie_len;
273 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
274 bss->freq, &entry_ts, bss->level,
279 p2p_scan_res_handled(wpa_s->global->p2p);
283 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
285 struct wpa_supplicant *wpa_s = work->wpa_s;
286 struct wpa_driver_scan_params *params = work->ctx;
290 if (!work->started) {
291 wpa_scan_free_params(params);
295 wpa_s->p2p_scan_work = NULL;
299 ret = wpa_drv_scan(wpa_s, params);
300 wpa_scan_free_params(params);
303 radio_work_done(work);
304 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
308 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
309 os_get_reltime(&wpa_s->scan_trigger_time);
310 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
311 wpa_s->own_scan_requested = 1;
312 wpa_s->p2p_scan_work = work;
316 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
319 if (wpa_s->global->p2p_24ghz_social_channels &&
320 (freq == 2412 || freq == 2437 || freq == 2462)) {
322 * Search all social channels regardless of whether these have
323 * been disabled for P2P operating channel use to avoid missing
328 return p2p_supported_freq(wpa_s->global->p2p, freq);
332 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
333 unsigned int num_req_dev_types,
334 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
336 struct wpa_supplicant *wpa_s = ctx;
337 struct wpa_driver_scan_params *params = NULL;
338 struct wpabuf *wps_ie, *ies;
339 unsigned int num_channels = 0;
340 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
344 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
347 if (wpa_s->p2p_scan_work) {
348 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
352 params = os_zalloc(sizeof(*params));
356 /* P2P Wildcard SSID */
357 params->num_ssids = 1;
358 n = os_malloc(P2P_WILDCARD_SSID_LEN);
361 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
362 params->ssids[0].ssid = n;
363 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
365 wpa_s->wps->dev.p2p = 1;
366 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
367 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
368 num_req_dev_types, req_dev_types);
372 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
373 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
378 wpabuf_put_buf(ies, wps_ie);
381 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
383 params->p2p_probe = 1;
384 n = os_malloc(wpabuf_len(ies));
389 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
390 params->extra_ies = n;
391 params->extra_ies_len = wpabuf_len(ies);
395 case P2P_SCAN_SOCIAL:
396 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
398 if (params->freqs == NULL)
400 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
401 if (wpas_p2p_search_social_channel(
402 wpa_s, social_channels_freq[i]))
403 params->freqs[num_channels++] =
404 social_channels_freq[i];
406 params->freqs[num_channels++] = 0;
410 case P2P_SCAN_SPECIFIC:
411 params->freqs = os_calloc(2, sizeof(int));
412 if (params->freqs == NULL)
414 params->freqs[0] = freq;
415 params->freqs[1] = 0;
417 case P2P_SCAN_SOCIAL_PLUS_ONE:
418 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
420 if (params->freqs == NULL)
422 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
423 if (wpas_p2p_search_social_channel(
424 wpa_s, social_channels_freq[i]))
425 params->freqs[num_channels++] =
426 social_channels_freq[i];
428 if (p2p_supported_freq(wpa_s->global->p2p, freq))
429 params->freqs[num_channels++] = freq;
430 params->freqs[num_channels++] = 0;
434 radio_remove_works(wpa_s, "p2p-scan", 0);
435 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
441 wpa_scan_free_params(params);
446 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
448 switch (p2p_group_interface) {
449 case P2P_GROUP_INTERFACE_PENDING:
450 return WPA_IF_P2P_GROUP;
451 case P2P_GROUP_INTERFACE_GO:
452 return WPA_IF_P2P_GO;
453 case P2P_GROUP_INTERFACE_CLIENT:
454 return WPA_IF_P2P_CLIENT;
457 return WPA_IF_P2P_GROUP;
461 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
463 size_t ssid_len, int *go)
467 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
468 for (s = wpa_s->conf->ssid; s; s = s->next) {
469 if (s->disabled != 0 || !s->p2p_group ||
470 s->ssid_len != ssid_len ||
471 os_memcmp(ssid, s->ssid, ssid_len) != 0)
473 if (s->mode == WPAS_MODE_P2P_GO &&
474 s != wpa_s->current_ssid)
477 *go = s->mode == WPAS_MODE_P2P_GO;
486 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
488 struct wpa_supplicant *wpa_s = eloop_ctx;
489 wpa_printf(MSG_DEBUG,
490 "P2P: Complete previously requested removal of %s",
492 wpas_p2p_disconnect(wpa_s);
496 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
497 struct wpa_supplicant *calling_wpa_s)
499 if (calling_wpa_s == wpa_s && wpa_s &&
500 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
502 * The calling wpa_s instance is going to be removed. Do that
503 * from an eloop callback to keep the instance available until
504 * the caller has returned. This my be needed, e.g., to provide
505 * control interface responses on the per-interface socket.
507 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
513 return wpas_p2p_disconnect(wpa_s);
517 /* Determine total number of clients in active groups where we are the GO */
518 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
520 unsigned int count = 0;
523 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
524 for (s = wpa_s->conf->ssid; s; s = s->next) {
525 wpa_printf(MSG_DEBUG,
526 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
527 wpa_s, s, s->disabled, s->p2p_group,
529 if (!s->disabled && s->p2p_group &&
530 s->mode == WPAS_MODE_P2P_GO) {
531 count += p2p_get_group_num_members(
541 /* Find an interface for a P2P group where we are the GO */
542 static struct wpa_supplicant *
543 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
545 struct wpa_supplicant *save = NULL;
551 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
552 for (s = wpa_s->conf->ssid; s; s = s->next) {
553 if (s->disabled || !s->p2p_group ||
554 s->mode != WPAS_MODE_P2P_GO)
557 /* Prefer a group with connected clients */
558 if (p2p_get_group_num_members(wpa_s->p2p_group))
564 /* No group with connected clients, so pick the one without (if any) */
569 /* Find an active P2P group where we are the GO */
570 static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
573 struct wpa_ssid *s, *empty = NULL;
578 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
579 for (s = wpa_s->conf->ssid; s; s = s->next) {
580 if (s->disabled || !s->p2p_group ||
581 s->mode != WPAS_MODE_P2P_GO)
584 os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
585 if (p2p_get_group_num_members(wpa_s->p2p_group))
595 /* Find a persistent group where we are the GO */
596 static struct wpa_ssid *
597 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
601 for (s = wpa_s->conf->ssid; s; s = s->next) {
602 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
610 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
612 struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
614 u8 conncap = P2PS_SETUP_NONE;
615 unsigned int owned_members = 0;
616 unsigned int owner = 0;
617 unsigned int client = 0;
618 struct wpa_supplicant *go_wpa_s;
619 struct wpa_ssid *persistent_go;
620 int p2p_no_group_iface;
622 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
625 * For non-concurrent capable devices:
626 * If persistent_go, then no new.
627 * If GO, then no client.
628 * If client, then no GO.
630 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
631 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
632 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
634 wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
635 go_wpa_s, persistent_go);
637 for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
638 tmp_wpa_s = tmp_wpa_s->next) {
639 for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
640 wpa_printf(MSG_DEBUG,
641 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
642 tmp_wpa_s, s, s->disabled,
643 s->p2p_group, s->mode);
644 if (!s->disabled && s->p2p_group) {
645 if (s->mode == WPAS_MODE_P2P_GO) {
647 p2p_get_group_num_members(
648 tmp_wpa_s->p2p_group);
656 /* If not concurrent, restrict our choices */
657 if (p2p_no_group_iface) {
658 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
661 return P2PS_SETUP_NONE;
664 if (role == P2PS_SETUP_CLIENT ||
665 incoming == P2PS_SETUP_GROUP_OWNER ||
666 p2p_client_limit_reached(go_wpa_s->p2p_group))
667 return P2PS_SETUP_NONE;
669 return P2PS_SETUP_GROUP_OWNER;
673 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
675 return P2PS_SETUP_GROUP_OWNER |
677 if (incoming == P2PS_SETUP_NEW) {
680 if (os_get_random(&r, sizeof(r)) < 0 ||
682 return P2PS_SETUP_CLIENT;
683 return P2PS_SETUP_GROUP_OWNER;
689 /* If a required role has been specified, handle it here */
690 if (role && role != P2PS_SETUP_NEW) {
692 case P2PS_SETUP_NONE:
694 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
695 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
699 case P2PS_SETUP_GROUP_OWNER:
701 * Must be a complimentary role - cannot be a client to
702 * more than one peer.
704 if (incoming == role || client)
705 return P2PS_SETUP_NONE;
707 return P2PS_SETUP_CLIENT;
709 case P2PS_SETUP_CLIENT:
710 /* Must be a complimentary role */
711 if (incoming != role) {
712 conncap = P2PS_SETUP_GROUP_OWNER;
717 return P2PS_SETUP_NONE;
722 * For now, we only will support ownership of one group, and being a
723 * client of one group. Therefore, if we have either an existing GO
724 * group, or an existing client group, we will not do a new GO
725 * negotiation, but rather try to re-use the existing groups.
728 case P2PS_SETUP_NONE:
731 conncap = P2PS_SETUP_GROUP_OWNER;
732 else if (!owned_members)
733 conncap = P2PS_SETUP_NEW;
734 else if (incoming == P2PS_SETUP_NONE)
735 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
737 conncap = P2PS_SETUP_CLIENT;
740 case P2PS_SETUP_CLIENT:
741 conncap = P2PS_SETUP_GROUP_OWNER;
744 case P2PS_SETUP_GROUP_OWNER:
746 conncap = P2PS_SETUP_CLIENT;
749 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
750 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
752 conncap = P2PS_SETUP_GROUP_OWNER;
756 if (os_get_random(&r, sizeof(r)) < 0 ||
758 conncap = P2PS_SETUP_CLIENT;
760 conncap = P2PS_SETUP_GROUP_OWNER;
765 return P2PS_SETUP_NONE;
769 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
770 (!incoming && (conncap & P2PS_SETUP_NEW))) {
771 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
772 conncap &= ~P2PS_SETUP_GROUP_OWNER;
773 wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
774 owner, owned_members, conncap);
776 s = wpas_p2p_get_persistent_go(wpa_s);
778 if (!s && !owner && p2p_no_group_iface) {
779 p2p_set_intended_addr(wpa_s->global->p2p,
781 } else if (!s && !owner) {
782 if (wpas_p2p_add_group_interface(wpa_s,
783 WPA_IF_P2P_GO) < 0) {
784 wpa_printf(MSG_ERROR,
785 "P2P: Failed to allocate a new interface for the group");
786 return P2PS_SETUP_NONE;
788 wpa_s->global->pending_group_iface_for_p2ps = 1;
789 p2p_set_intended_addr(wpa_s->global->p2p,
790 wpa_s->pending_interface_addr);
798 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
799 enum p2p_group_removal_reason removal_reason)
801 struct wpa_ssid *ssid;
805 ssid = wpa_s->current_ssid;
808 * The current SSID was not known, but there may still be a
809 * pending P2P group interface waiting for provisioning or a
810 * P2P group that is trying to reconnect.
812 ssid = wpa_s->conf->ssid;
814 if (ssid->p2p_group && ssid->disabled != 2)
819 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
821 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
826 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
828 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
829 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
830 wpa_s->reassociate = 0;
831 wpa_s->disconnected = 1;
836 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
837 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
839 if (os_strcmp(gtype, "client") == 0) {
840 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
841 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
843 wpa_printf(MSG_DEBUG,
844 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
845 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
846 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
851 if (wpa_s->cross_connect_in_use) {
852 wpa_s->cross_connect_in_use = 0;
853 wpa_msg_global(wpa_s->parent, MSG_INFO,
854 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
855 wpa_s->ifname, wpa_s->cross_connect_uplink);
857 switch (removal_reason) {
858 case P2P_GROUP_REMOVAL_REQUESTED:
859 reason = " reason=REQUESTED";
861 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
862 reason = " reason=FORMATION_FAILED";
864 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
865 reason = " reason=IDLE";
867 case P2P_GROUP_REMOVAL_UNAVAILABLE:
868 reason = " reason=UNAVAILABLE";
870 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
871 reason = " reason=GO_ENDING_SESSION";
873 case P2P_GROUP_REMOVAL_PSK_FAILURE:
874 reason = " reason=PSK_FAILURE";
876 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
877 reason = " reason=FREQ_CONFLICT";
883 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
884 wpa_msg_global(wpa_s->parent, MSG_INFO,
885 P2P_EVENT_GROUP_REMOVED "%s %s%s",
886 wpa_s->ifname, gtype, reason);
889 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
890 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
891 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
892 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
893 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
894 wpa_s->parent, NULL) > 0) {
895 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
897 wpa_s->p2p_in_provisioning = 0;
898 wpas_p2p_group_formation_failed(wpa_s, 1);
901 wpa_s->p2p_in_invitation = 0;
902 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
903 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
906 * Make sure wait for the first client does not remain active after the
907 * group has been removed.
909 wpa_s->global->p2p_go_wait_client.sec = 0;
911 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
912 struct wpa_global *global;
914 enum wpa_driver_if_type type;
915 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
917 global = wpa_s->global;
918 ifname = os_strdup(wpa_s->ifname);
919 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
920 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
921 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
922 wpa_s = global->ifaces;
924 wpa_drv_if_remove(wpa_s, type, ifname);
929 if (!wpa_s->p2p_go_group_formation_completed) {
930 wpa_s->global->p2p_group_formation = NULL;
931 wpa_s->p2p_in_provisioning = 0;
934 wpa_s->show_group_started = 0;
935 os_free(wpa_s->go_params);
936 wpa_s->go_params = NULL;
938 os_free(wpa_s->p2p_group_common_freqs);
939 wpa_s->p2p_group_common_freqs = NULL;
940 wpa_s->p2p_group_common_freqs_num = 0;
942 wpa_s->waiting_presence_resp = 0;
944 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
945 if (ssid && (ssid->p2p_group ||
946 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
947 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
949 if (ssid == wpa_s->current_ssid) {
950 wpa_sm_set_config(wpa_s->wpa, NULL);
951 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
952 wpa_s->current_ssid = NULL;
955 * Networks objects created during any P2P activities are not
956 * exposed out as they might/will confuse certain non-P2P aware
957 * applications since these network objects won't behave like
960 * Likewise, we don't send out network removed signals for such
963 wpa_config_remove_network(wpa_s->conf, id);
964 wpa_supplicant_clear_status(wpa_s);
965 wpa_supplicant_cancel_sched_scan(wpa_s);
967 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
971 wpa_supplicant_ap_deinit(wpa_s);
973 wpa_drv_deinit_p2p_cli(wpa_s);
975 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
981 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
983 const u8 *ssid, size_t ssid_len)
991 if (wpa_s->go_params)
992 bssid = wpa_s->go_params->peer_interface_addr;
994 bssid = wpa_s->bssid;
996 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
997 if (bss == NULL && wpa_s->go_params &&
998 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
999 bss = wpa_bss_get_p2p_dev_addr(
1000 wpa_s, wpa_s->go_params->peer_device_addr);
1002 u8 iface_addr[ETH_ALEN];
1003 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1005 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1008 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1009 "group is persistent - BSS " MACSTR " not found",
1014 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1016 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1017 P2P_IE_VENDOR_TYPE);
1019 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1020 "group is persistent - BSS " MACSTR
1021 " did not include P2P IE", MAC2STR(bssid));
1022 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1023 (u8 *) (bss + 1), bss->ie_len);
1024 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1025 ((u8 *) bss + 1) + bss->ie_len,
1026 bss->beacon_ie_len);
1030 group_capab = p2p_get_group_capab(p2p);
1031 addr = p2p_get_go_dev_addr(p2p);
1032 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1033 "group_capab=0x%x", group_capab);
1035 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1036 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1039 os_memset(go_dev_addr, 0, ETH_ALEN);
1042 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1043 "go_dev_addr=" MACSTR,
1044 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1046 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1050 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1051 struct wpa_ssid *ssid,
1052 const u8 *go_dev_addr)
1057 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1058 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1059 for (s = wpa_s->conf->ssid; s; s = s->next) {
1060 if (s->disabled == 2 &&
1061 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1062 s->ssid_len == ssid->ssid_len &&
1063 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1068 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1070 if (ssid->passphrase && !s->passphrase)
1072 else if (ssid->passphrase && s->passphrase &&
1073 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1076 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1079 s = wpa_config_add_network(wpa_s->conf);
1084 * Instead of network_added we emit persistent_group_added
1085 * notification. Also to keep the defense checks in
1086 * persistent_group obj registration method, we set the
1087 * relevant flags in s to designate it as a persistent group.
1090 s->p2p_persistent_group = 1;
1091 wpas_notify_persistent_group_added(wpa_s, s);
1092 wpa_config_set_network_defaults(s);
1096 s->p2p_persistent_group = 1;
1099 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1100 s->mode = ssid->mode;
1101 s->auth_alg = WPA_AUTH_ALG_OPEN;
1102 s->key_mgmt = WPA_KEY_MGMT_PSK;
1103 s->proto = WPA_PROTO_RSN;
1104 s->pairwise_cipher = WPA_CIPHER_CCMP;
1106 if (ssid->passphrase) {
1107 os_free(s->passphrase);
1108 s->passphrase = os_strdup(ssid->passphrase);
1110 if (ssid->psk_set) {
1112 os_memcpy(s->psk, ssid->psk, 32);
1114 if (s->passphrase && !s->psk_set)
1115 wpa_config_update_psk(s);
1116 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1118 s->ssid = os_malloc(ssid->ssid_len);
1121 s->ssid_len = ssid->ssid_len;
1122 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1124 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1125 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1126 wpa_s->global->add_psk = NULL;
1130 if (changed && wpa_s->conf->update_config &&
1131 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1132 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1139 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1142 struct wpa_ssid *ssid, *s;
1146 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
1148 ssid = wpa_s->current_ssid;
1149 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1150 !ssid->p2p_persistent_group)
1153 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
1154 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1157 if (s->ssid_len == ssid->ssid_len &&
1158 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1165 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
1166 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
1170 if (i == s->num_p2p_clients - 1)
1171 return; /* already the most recent entry */
1173 /* move the entry to mark it most recent */
1174 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1175 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1176 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
1177 os_memcpy(s->p2p_client_list +
1178 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1180 os_memset(s->p2p_client_list +
1181 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1187 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1188 n = os_realloc_array(s->p2p_client_list,
1189 s->num_p2p_clients + 1, 2 * ETH_ALEN);
1192 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1194 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1196 s->p2p_client_list = n;
1197 s->num_p2p_clients++;
1198 } else if (!found && s->p2p_client_list) {
1199 /* Not enough room for an additional entry - drop the oldest
1201 os_memmove(s->p2p_client_list,
1202 s->p2p_client_list + 2 * ETH_ALEN,
1203 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1204 os_memcpy(s->p2p_client_list +
1205 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1207 os_memset(s->p2p_client_list +
1208 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1212 if (p2p_wpa_s->conf->update_config &&
1213 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
1214 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1218 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1219 int go, struct wpa_ssid *ssid, int freq,
1220 const u8 *psk, const char *passphrase,
1221 const u8 *go_dev_addr, int persistent,
1224 const char *ssid_txt;
1228 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1233 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1237 if (passphrase && passphrase[0] == '\0')
1241 * Include PSK/passphrase only in the control interface message and
1242 * leave it out from the debug log entry.
1244 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1245 P2P_EVENT_GROUP_STARTED
1246 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1248 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1249 psk ? " psk=" : "", psk_txt,
1250 passphrase ? " passphrase=\"" : "",
1251 passphrase ? passphrase : "",
1252 passphrase ? "\"" : "",
1253 MAC2STR(go_dev_addr),
1254 persistent ? " [PERSISTENT]" : "", extra);
1255 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1256 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1257 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1258 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1263 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1264 int success, int already_deleted)
1266 struct wpa_ssid *ssid;
1269 u8 go_dev_addr[ETH_ALEN];
1270 int network_id = -1;
1273 * This callback is likely called for the main interface. Update wpa_s
1274 * to use the group interface if a new interface was created for the
1277 if (wpa_s->global->p2p_group_formation)
1278 wpa_s = wpa_s->global->p2p_group_formation;
1279 if (wpa_s->p2p_go_group_formation_completed) {
1280 wpa_s->global->p2p_group_formation = NULL;
1281 wpa_s->p2p_in_provisioning = 0;
1283 wpa_s->p2p_in_invitation = 0;
1284 wpa_s->group_formation_reported = 1;
1287 wpa_msg_global(wpa_s->parent, MSG_INFO,
1288 P2P_EVENT_GROUP_FORMATION_FAILURE);
1289 wpas_notify_p2p_group_formation_failure(wpa_s, "");
1290 if (already_deleted)
1292 wpas_p2p_group_delete(wpa_s,
1293 P2P_GROUP_REMOVAL_FORMATION_FAILED);
1297 wpa_msg_global(wpa_s->parent, MSG_INFO,
1298 P2P_EVENT_GROUP_FORMATION_SUCCESS);
1300 ssid = wpa_s->current_ssid;
1301 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1302 ssid->mode = WPAS_MODE_P2P_GO;
1303 p2p_group_notif_formation_done(wpa_s->p2p_group);
1304 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1309 client = ssid->mode == WPAS_MODE_INFRA;
1310 if (ssid->mode == WPAS_MODE_P2P_GO) {
1311 persistent = ssid->p2p_persistent_group;
1312 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1315 persistent = wpas_p2p_persistent_group(wpa_s,
1320 client = wpa_s->p2p_group_interface ==
1321 P2P_GROUP_INTERFACE_CLIENT;
1322 os_memset(go_dev_addr, 0, ETH_ALEN);
1325 wpa_s->show_group_started = 0;
1328 * Indicate event only after successfully completed 4-way
1329 * handshake, i.e., when the interface is ready for data
1332 wpa_s->show_group_started = 1;
1334 wpas_p2p_group_started(wpa_s, 1, ssid,
1335 ssid ? ssid->frequency : 0,
1336 ssid && ssid->passphrase == NULL &&
1337 ssid->psk_set ? ssid->psk : NULL,
1338 ssid ? ssid->passphrase : NULL,
1339 go_dev_addr, persistent, "");
1340 wpas_p2p_cross_connect_setup(wpa_s);
1341 wpas_p2p_set_group_idle_timeout(wpa_s);
1345 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1348 os_free(wpa_s->global->add_psk);
1349 wpa_s->global->add_psk = NULL;
1351 if (network_id < 0 && ssid)
1352 network_id = ssid->id;
1354 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1355 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1360 struct send_action_work {
1366 unsigned int wait_time;
1371 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1374 struct wpa_supplicant *wpa_s = eloop_ctx;
1376 if (!wpa_s->p2p_send_action_work)
1379 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1380 os_free(wpa_s->p2p_send_action_work->ctx);
1381 radio_work_done(wpa_s->p2p_send_action_work);
1382 wpa_s->p2p_send_action_work = NULL;
1386 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1388 if (wpa_s->p2p_send_action_work) {
1389 struct send_action_work *awork;
1390 awork = wpa_s->p2p_send_action_work->ctx;
1391 if (awork->wait_time == 0) {
1393 radio_work_done(wpa_s->p2p_send_action_work);
1394 wpa_s->p2p_send_action_work = NULL;
1397 * In theory, this should not be needed, but number of
1398 * places in the P2P code is still using non-zero wait
1399 * time for the last Action frame in the sequence and
1400 * some of these do not call send_action_done().
1402 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1404 eloop_register_timeout(
1405 0, awork->wait_time * 1000,
1406 wpas_p2p_send_action_work_timeout,
1413 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1415 const u8 *dst, const u8 *src,
1417 const u8 *data, size_t data_len,
1418 enum offchannel_send_action_result
1421 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1423 wpas_p2p_action_tx_clear(wpa_s);
1425 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1429 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1430 res = P2P_SEND_ACTION_SUCCESS;
1432 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1433 res = P2P_SEND_ACTION_NO_ACK;
1435 case OFFCHANNEL_SEND_ACTION_FAILED:
1436 res = P2P_SEND_ACTION_FAILED;
1440 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1442 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1443 wpa_s->pending_pd_before_join &&
1444 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1445 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1446 wpa_s->p2p_fallback_to_go_neg) {
1447 wpa_s->pending_pd_before_join = 0;
1448 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1449 "during p2p_connect-auto");
1450 wpa_msg_global(wpa_s->parent, MSG_INFO,
1451 P2P_EVENT_FALLBACK_TO_GO_NEG
1452 "reason=no-ACK-to-PD-Req");
1453 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1459 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1461 struct wpa_supplicant *wpa_s = work->wpa_s;
1462 struct send_action_work *awork = work->ctx;
1465 if (work->started) {
1466 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1468 wpa_s->p2p_send_action_work = NULL;
1469 offchannel_send_action_done(wpa_s);
1475 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1476 awork->bssid, awork->buf, awork->len,
1478 wpas_p2p_send_action_tx_status, 1) < 0) {
1480 radio_work_done(work);
1483 wpa_s->p2p_send_action_work = work;
1487 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1488 unsigned int freq, const u8 *dst,
1489 const u8 *src, const u8 *bssid, const u8 *buf,
1490 size_t len, unsigned int wait_time)
1492 struct send_action_work *awork;
1494 if (wpa_s->p2p_send_action_work) {
1495 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1499 awork = os_zalloc(sizeof(*awork) + len);
1504 os_memcpy(awork->dst, dst, ETH_ALEN);
1505 os_memcpy(awork->src, src, ETH_ALEN);
1506 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1508 awork->wait_time = wait_time;
1509 os_memcpy(awork->buf, buf, len);
1511 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1512 wpas_send_action_cb, awork) < 0) {
1521 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1522 const u8 *src, const u8 *bssid, const u8 *buf,
1523 size_t len, unsigned int wait_time)
1525 struct wpa_supplicant *wpa_s = ctx;
1526 int listen_freq = -1, send_freq = -1;
1528 if (wpa_s->p2p_listen_work)
1529 listen_freq = wpa_s->p2p_listen_work->freq;
1530 if (wpa_s->p2p_send_action_work)
1531 send_freq = wpa_s->p2p_send_action_work->freq;
1532 if (listen_freq != (int) freq && send_freq != (int) freq) {
1533 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1534 listen_freq, send_freq);
1535 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1539 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1540 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1542 wpas_p2p_send_action_tx_status, 1);
1546 static void wpas_send_action_done(void *ctx)
1548 struct wpa_supplicant *wpa_s = ctx;
1550 if (wpa_s->p2p_send_action_work) {
1551 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1553 os_free(wpa_s->p2p_send_action_work->ctx);
1554 radio_work_done(wpa_s->p2p_send_action_work);
1555 wpa_s->p2p_send_action_work = NULL;
1558 offchannel_send_action_done(wpa_s);
1562 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1563 struct p2p_go_neg_results *params)
1565 if (wpa_s->go_params == NULL) {
1566 wpa_s->go_params = os_malloc(sizeof(*params));
1567 if (wpa_s->go_params == NULL)
1570 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1575 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1576 struct p2p_go_neg_results *res)
1578 wpa_s->group_formation_reported = 0;
1579 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1580 " dev_addr " MACSTR " wps_method %d",
1581 MAC2STR(res->peer_interface_addr),
1582 MAC2STR(res->peer_device_addr), res->wps_method);
1583 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1584 res->ssid, res->ssid_len);
1585 wpa_supplicant_ap_deinit(wpa_s);
1586 wpas_copy_go_neg_results(wpa_s, res);
1587 if (res->wps_method == WPS_PBC) {
1588 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1589 #ifdef CONFIG_WPS_NFC
1590 } else if (res->wps_method == WPS_NFC) {
1591 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1592 res->peer_interface_addr,
1593 wpa_s->parent->p2p_oob_dev_pw,
1594 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1595 wpa_s->parent->p2p_oob_dev_pw_id ==
1596 DEV_PW_NFC_CONNECTION_HANDOVER ?
1597 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1600 #endif /* CONFIG_WPS_NFC */
1602 u16 dev_pw_id = DEV_PW_DEFAULT;
1603 if (wpa_s->p2p_wps_method == WPS_P2PS)
1604 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1605 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1606 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1607 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1608 wpa_s->p2p_pin, 1, dev_pw_id);
1613 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1614 struct wpa_ssid *ssid)
1616 struct wpa_ssid *persistent;
1617 struct psk_list_entry *psk;
1618 struct hostapd_data *hapd;
1620 if (!wpa_s->ap_iface)
1623 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1625 if (persistent == NULL)
1628 hapd = wpa_s->ap_iface->bss[0];
1630 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1632 struct hostapd_wpa_psk *hpsk;
1634 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1636 MAC2STR(psk->addr), psk->p2p);
1637 hpsk = os_zalloc(sizeof(*hpsk));
1640 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1642 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1644 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1645 hpsk->next = hapd->conf->ssid.wpa_psk;
1646 hapd->conf->ssid.wpa_psk = hpsk;
1651 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1655 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1656 wpa_s->p2p_group_common_freqs_num);
1658 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1659 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1660 i, wpa_s->p2p_group_common_freqs[i]);
1664 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1665 struct p2p_go_neg_results *params)
1667 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1669 wpa_s->p2p_group_common_freqs_num = 0;
1670 os_free(wpa_s->p2p_group_common_freqs);
1671 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1672 if (!wpa_s->p2p_group_common_freqs)
1675 for (i = 0; i < len; i++) {
1676 if (!wpa_s->go_params->freq_list[i])
1678 wpa_s->p2p_group_common_freqs[i] =
1679 wpa_s->go_params->freq_list[i];
1681 wpa_s->p2p_group_common_freqs_num = i;
1685 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1687 #ifndef CONFIG_NO_CONFIG_WRITE
1688 if (wpa_s->parent->conf->update_config &&
1689 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1690 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1691 #endif /* CONFIG_NO_CONFIG_WRITE */
1695 static void p2p_go_configured(void *ctx, void *data)
1697 struct wpa_supplicant *wpa_s = ctx;
1698 struct p2p_go_neg_results *params = data;
1699 struct wpa_ssid *ssid;
1700 int network_id = -1;
1702 p2p_go_save_group_common_freqs(wpa_s, params);
1703 p2p_go_dump_common_freqs(wpa_s);
1705 ssid = wpa_s->current_ssid;
1706 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1707 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1708 if (wpa_s->global->p2p_group_formation == wpa_s)
1709 wpa_s->global->p2p_group_formation = NULL;
1710 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1711 params->passphrase[0] == '\0' ?
1714 wpa_s->global->p2p_dev_addr,
1715 params->persistent_group, "");
1716 wpa_s->group_formation_reported = 1;
1718 if (wpa_s->parent->p2ps_method_config_any) {
1719 if (is_zero_ether_addr(wpa_s->parent->p2ps_join_addr)) {
1720 wpa_dbg(wpa_s, MSG_DEBUG,
1721 "P2PS: Setting default PIN for ANY");
1722 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1723 "12345670", NULL, 0,
1726 wpa_dbg(wpa_s, MSG_DEBUG,
1727 "P2PS: Setting default PIN for " MACSTR,
1728 MAC2STR(wpa_s->parent->p2ps_join_addr));
1729 wpa_supplicant_ap_wps_pin(
1730 wpa_s, wpa_s->parent->p2ps_join_addr,
1731 "12345670", NULL, 0, 0);
1733 wpa_s->parent->p2ps_method_config_any = 0;
1736 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1737 if (params->persistent_group) {
1738 network_id = wpas_p2p_store_persistent_group(
1739 wpa_s->parent, ssid,
1740 wpa_s->global->p2p_dev_addr);
1741 wpas_p2p_add_psk_list(wpa_s, ssid);
1744 network_id = ssid->id;
1745 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1746 wpas_p2p_cross_connect_setup(wpa_s);
1747 wpas_p2p_set_group_idle_timeout(wpa_s);
1749 if (wpa_s->p2p_first_connection_timeout) {
1750 wpa_dbg(wpa_s, MSG_DEBUG,
1751 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1752 wpa_s->p2p_first_connection_timeout);
1753 wpa_s->p2p_go_group_formation_completed = 0;
1754 wpa_s->global->p2p_group_formation = wpa_s;
1755 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1756 wpa_s->parent, NULL);
1757 eloop_register_timeout(
1758 wpa_s->p2p_first_connection_timeout, 0,
1759 wpas_p2p_group_formation_timeout,
1760 wpa_s->parent, NULL);
1766 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1767 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1768 params->peer_interface_addr)) {
1769 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1773 if (params->wps_method == WPS_PBC) {
1774 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1775 params->peer_device_addr);
1776 #ifdef CONFIG_WPS_NFC
1777 } else if (params->wps_method == WPS_NFC) {
1778 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1779 DEV_PW_NFC_CONNECTION_HANDOVER &&
1780 !wpa_s->parent->p2p_oob_dev_pw) {
1781 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1784 wpas_ap_wps_add_nfc_pw(
1785 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1786 wpa_s->parent->p2p_oob_dev_pw,
1787 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1788 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1789 #endif /* CONFIG_WPS_NFC */
1790 } else if (wpa_s->p2p_pin[0])
1791 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1792 wpa_s->p2p_pin, NULL, 0, 0);
1793 os_free(wpa_s->go_params);
1794 wpa_s->go_params = NULL;
1798 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1799 struct p2p_go_neg_results *params,
1800 int group_formation)
1802 struct wpa_ssid *ssid;
1804 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1805 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1806 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1811 ssid = wpa_config_add_network(wpa_s->conf);
1813 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1817 wpa_s->show_group_started = 0;
1818 wpa_s->p2p_go_group_formation_completed = 0;
1819 wpa_s->group_formation_reported = 0;
1820 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1822 wpa_config_set_network_defaults(ssid);
1823 ssid->temporary = 1;
1824 ssid->p2p_group = 1;
1825 ssid->p2p_persistent_group = params->persistent_group;
1826 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1828 ssid->frequency = params->freq;
1829 ssid->ht40 = params->ht40;
1830 ssid->vht = params->vht;
1831 ssid->ssid = os_zalloc(params->ssid_len + 1);
1833 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1834 ssid->ssid_len = params->ssid_len;
1836 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1837 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1838 ssid->proto = WPA_PROTO_RSN;
1839 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1840 ssid->group_cipher = WPA_CIPHER_CCMP;
1841 if (params->freq > 56160) {
1843 * Enable GCMP instead of CCMP as pairwise_cipher and
1844 * group_cipher in 60 GHz.
1846 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1847 ssid->group_cipher = WPA_CIPHER_GCMP;
1849 if (os_strlen(params->passphrase) > 0) {
1850 ssid->passphrase = os_strdup(params->passphrase);
1851 if (ssid->passphrase == NULL) {
1852 wpa_msg_global(wpa_s, MSG_ERROR,
1853 "P2P: Failed to copy passphrase for GO");
1854 wpa_config_remove_network(wpa_s->conf, ssid->id);
1858 ssid->passphrase = NULL;
1859 ssid->psk_set = params->psk_set;
1861 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1862 else if (ssid->passphrase)
1863 wpa_config_update_psk(ssid);
1864 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1866 wpa_s->ap_configured_cb = p2p_go_configured;
1867 wpa_s->ap_configured_cb_ctx = wpa_s;
1868 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1869 wpa_s->scan_req = NORMAL_SCAN_REQ;
1870 wpa_s->connect_without_scan = ssid;
1871 wpa_s->reassociate = 1;
1872 wpa_s->disconnected = 0;
1873 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1875 wpa_supplicant_req_scan(wpa_s, 0, 0);
1879 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1880 const struct wpa_supplicant *src)
1882 struct wpa_config *d;
1883 const struct wpa_config *s;
1888 #define C(n) if (s->n) d->n = os_strdup(s->n)
1897 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1898 os_memcpy(d->sec_device_type, s->sec_device_type,
1899 sizeof(d->sec_device_type));
1900 d->num_sec_device_types = s->num_sec_device_types;
1902 d->p2p_group_idle = s->p2p_group_idle;
1903 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
1904 d->p2p_intra_bss = s->p2p_intra_bss;
1905 d->persistent_reconnect = s->persistent_reconnect;
1906 d->max_num_sta = s->max_num_sta;
1907 d->pbc_in_m1 = s->pbc_in_m1;
1908 d->ignore_old_scan_res = s->ignore_old_scan_res;
1909 d->beacon_int = s->beacon_int;
1910 d->dtim_period = s->dtim_period;
1911 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
1912 d->disassoc_low_ack = s->disassoc_low_ack;
1913 d->disable_scan_offload = s->disable_scan_offload;
1914 d->passive_scan = s->passive_scan;
1916 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1917 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1918 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1920 d->p2p_cli_probe = s->p2p_cli_probe;
1924 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1925 char *ifname, size_t len)
1927 char *ifname_ptr = wpa_s->ifname;
1929 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1930 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1931 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1934 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1935 if (os_strlen(ifname) >= IFNAMSIZ &&
1936 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1939 /* Try to avoid going over the IFNAMSIZ length limit */
1940 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1941 if (os_snprintf_error(len, res) && len)
1942 ifname[len - 1] = '\0';
1947 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1948 enum wpa_driver_if_type type)
1950 char ifname[120], force_ifname[120];
1952 if (wpa_s->pending_interface_name[0]) {
1953 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1954 "- skip creation of a new one");
1955 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1956 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1957 "unknown?! ifname='%s'",
1958 wpa_s->pending_interface_name);
1964 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1965 force_ifname[0] = '\0';
1967 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1969 wpa_s->p2p_group_idx++;
1971 wpa_s->pending_interface_type = type;
1972 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1973 wpa_s->pending_interface_addr, NULL) < 0) {
1974 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1979 if (force_ifname[0]) {
1980 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1982 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1983 sizeof(wpa_s->pending_interface_name));
1985 os_strlcpy(wpa_s->pending_interface_name, ifname,
1986 sizeof(wpa_s->pending_interface_name));
1987 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1988 MACSTR, wpa_s->pending_interface_name,
1989 MAC2STR(wpa_s->pending_interface_addr));
1995 static void wpas_p2p_remove_pending_group_interface(
1996 struct wpa_supplicant *wpa_s)
1998 if (!wpa_s->pending_interface_name[0] ||
1999 is_zero_ether_addr(wpa_s->pending_interface_addr))
2000 return; /* No pending virtual interface */
2002 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2003 wpa_s->pending_interface_name);
2004 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2005 wpa_s->pending_interface_name);
2006 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2007 wpa_s->pending_interface_name[0] = '\0';
2008 wpa_s->global->pending_group_iface_for_p2ps = 0;
2012 static struct wpa_supplicant *
2013 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2015 struct wpa_interface iface;
2016 struct wpa_supplicant *group_wpa_s;
2018 if (!wpa_s->pending_interface_name[0]) {
2019 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2020 if (!wpas_p2p_create_iface(wpa_s))
2023 * Something has forced us to remove the pending interface; try
2024 * to create a new one and hope for the best that we will get
2025 * the same local address.
2027 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2028 WPA_IF_P2P_CLIENT) < 0)
2032 os_memset(&iface, 0, sizeof(iface));
2033 iface.ifname = wpa_s->pending_interface_name;
2034 iface.driver = wpa_s->driver->name;
2035 if (wpa_s->conf->ctrl_interface == NULL &&
2036 wpa_s->parent != wpa_s &&
2038 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2039 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2041 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
2042 iface.driver_param = wpa_s->conf->driver_param;
2043 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
2044 if (group_wpa_s == NULL) {
2045 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2046 "wpa_supplicant interface");
2049 wpa_s->pending_interface_name[0] = '\0';
2050 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2051 P2P_GROUP_INTERFACE_CLIENT;
2052 wpa_s->global->p2p_group_formation = group_wpa_s;
2053 wpa_s->global->pending_group_iface_for_p2ps = 0;
2055 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2061 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2064 struct wpa_supplicant *wpa_s = eloop_ctx;
2065 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2066 wpas_p2p_group_formation_failed(wpa_s, 0);
2070 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2071 int already_deleted)
2073 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2074 wpa_s->parent, NULL);
2075 if (wpa_s->global->p2p)
2076 p2p_group_formation_failed(wpa_s->global->p2p);
2077 wpas_group_formation_completed(wpa_s, 0, already_deleted);
2081 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2083 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2084 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2085 wpa_s->parent, NULL);
2086 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2087 wpa_s->parent, NULL);
2088 wpa_s->global->p2p_fail_on_wps_complete = 0;
2092 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2094 if (wpa_s->global->p2p_group_formation != wpa_s)
2096 /* Speed up group formation timeout since this cannot succeed */
2097 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2098 wpa_s->parent, NULL);
2099 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2100 wpa_s->parent, NULL);
2104 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2106 struct wpa_supplicant *wpa_s = ctx;
2108 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2109 wpa_drv_cancel_remain_on_channel(wpa_s);
2110 wpa_s->off_channel_freq = 0;
2111 wpa_s->roc_waiting_drv_freq = 0;
2115 wpa_msg_global(wpa_s, MSG_INFO,
2116 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2118 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2119 wpas_p2p_remove_pending_group_interface(wpa_s);
2123 if (!res->role_go) {
2124 /* Inform driver of the operating channel of GO. */
2125 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2128 if (wpa_s->p2p_go_ht40)
2130 if (wpa_s->p2p_go_vht)
2133 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2134 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2136 res->role_go ? "GO" : "client", res->freq, res->ht40,
2137 MAC2STR(res->peer_device_addr),
2138 MAC2STR(res->peer_interface_addr),
2139 p2p_wps_method_text(res->wps_method));
2140 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2142 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2143 struct wpa_ssid *ssid;
2144 ssid = wpa_config_get_network(wpa_s->conf,
2145 wpa_s->p2p_persistent_id);
2146 if (ssid && ssid->disabled == 2 &&
2147 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2148 size_t len = os_strlen(ssid->passphrase);
2149 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2150 "on requested persistent group");
2151 os_memcpy(res->passphrase, ssid->passphrase, len);
2152 res->passphrase[len] = '\0';
2156 if (wpa_s->create_p2p_iface) {
2157 struct wpa_supplicant *group_wpa_s =
2158 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2159 if (group_wpa_s == NULL) {
2160 wpas_p2p_remove_pending_group_interface(wpa_s);
2161 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2163 wpas_p2p_group_formation_failed(wpa_s, 1);
2166 if (group_wpa_s != wpa_s) {
2167 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2168 sizeof(group_wpa_s->p2p_pin));
2169 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2171 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2172 wpa_s->pending_interface_name[0] = '\0';
2173 group_wpa_s->p2p_in_provisioning = 1;
2176 wpas_start_wps_go(group_wpa_s, res, 1);
2178 os_get_reltime(&group_wpa_s->scan_min_time);
2179 wpas_start_wps_enrollee(group_wpa_s, res);
2182 wpa_s->p2p_in_provisioning = 1;
2183 wpa_s->global->p2p_group_formation = wpa_s;
2186 wpas_start_wps_go(wpa_s, res, 1);
2188 os_get_reltime(&wpa_s->scan_min_time);
2189 wpas_start_wps_enrollee(ctx, res);
2193 wpa_s->p2p_long_listen = 0;
2194 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2196 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2197 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2198 (res->peer_config_timeout % 100) * 10000,
2199 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2203 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2206 struct wpa_supplicant *wpa_s = ctx;
2207 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2208 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2209 dev_passwd_id, go_intent);
2211 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
2215 static void wpas_dev_found(void *ctx, const u8 *addr,
2216 const struct p2p_peer_info *info,
2219 #ifndef CONFIG_NO_STDOUT_DEBUG
2220 struct wpa_supplicant *wpa_s = ctx;
2221 char devtype[WPS_DEV_TYPE_BUFSIZE];
2222 char *wfd_dev_info_hex = NULL;
2224 #ifdef CONFIG_WIFI_DISPLAY
2225 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2226 WFD_SUBELEM_DEVICE_INFO);
2227 #endif /* CONFIG_WIFI_DISPLAY */
2229 if (info->p2ps_instance) {
2231 const u8 *buf = wpabuf_head(info->p2ps_instance);
2232 size_t len = wpabuf_len(info->p2ps_instance);
2239 if (len < 4 + 2 + 1)
2241 id = WPA_GET_LE32(buf);
2243 methods = WPA_GET_BE16(buf);
2246 if (str_len > len - 4 - 2 - 1)
2248 os_memcpy(str, buf, str_len);
2249 str[str_len] = '\0';
2251 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2253 wpa_msg_global(wpa_s, MSG_INFO,
2254 P2P_EVENT_DEVICE_FOUND MACSTR
2255 " p2p_dev_addr=" MACSTR
2256 " pri_dev_type=%s name='%s'"
2257 " config_methods=0x%x"
2260 " adv_id=%x asp_svc=%s%s",
2262 MAC2STR(info->p2p_device_addr),
2263 wps_dev_type_bin2str(
2265 devtype, sizeof(devtype)),
2266 info->device_name, methods,
2267 info->dev_capab, info->group_capab,
2269 info->vendor_elems ?
2270 " vendor_elems=1" : "");
2275 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2276 " p2p_dev_addr=" MACSTR
2277 " pri_dev_type=%s name='%s' config_methods=0x%x "
2278 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2279 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2280 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2282 info->device_name, info->config_methods,
2283 info->dev_capab, info->group_capab,
2284 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2285 wfd_dev_info_hex ? wfd_dev_info_hex : "",
2286 info->vendor_elems ? " vendor_elems=1" : "",
2290 os_free(wfd_dev_info_hex);
2291 #endif /* CONFIG_NO_STDOUT_DEBUG */
2293 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2297 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2299 struct wpa_supplicant *wpa_s = ctx;
2301 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2302 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2304 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2308 static void wpas_find_stopped(void *ctx)
2310 struct wpa_supplicant *wpa_s = ctx;
2311 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2312 wpas_notify_p2p_find_stopped(wpa_s);
2316 struct wpas_p2p_listen_work {
2318 unsigned int duration;
2319 struct wpabuf *probe_resp_ie;
2323 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2327 wpabuf_free(lwork->probe_resp_ie);
2332 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2334 struct wpas_p2p_listen_work *lwork;
2336 if (!wpa_s->p2p_listen_work)
2339 lwork = wpa_s->p2p_listen_work->ctx;
2340 wpas_p2p_listen_work_free(lwork);
2341 radio_work_done(wpa_s->p2p_listen_work);
2342 wpa_s->p2p_listen_work = NULL;
2346 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2348 struct wpa_supplicant *wpa_s = work->wpa_s;
2349 struct wpas_p2p_listen_work *lwork = work->ctx;
2350 unsigned int duration;
2353 if (work->started) {
2354 wpa_s->p2p_listen_work = NULL;
2355 wpas_stop_listen(wpa_s);
2357 wpas_p2p_listen_work_free(lwork);
2361 wpa_s->p2p_listen_work = work;
2363 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2365 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2366 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2367 "report received Probe Request frames");
2368 wpas_p2p_listen_work_done(wpa_s);
2372 wpa_s->pending_listen_freq = lwork->freq;
2373 wpa_s->pending_listen_duration = lwork->duration;
2375 duration = lwork->duration;
2376 #ifdef CONFIG_TESTING_OPTIONS
2377 if (wpa_s->extra_roc_dur) {
2378 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2379 duration, duration + wpa_s->extra_roc_dur);
2380 duration += wpa_s->extra_roc_dur;
2382 #endif /* CONFIG_TESTING_OPTIONS */
2384 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2385 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2386 "to remain on channel (%u MHz) for Listen "
2387 "state", lwork->freq);
2388 wpas_p2p_listen_work_done(wpa_s);
2389 wpa_s->pending_listen_freq = 0;
2392 wpa_s->off_channel_freq = 0;
2393 wpa_s->roc_waiting_drv_freq = lwork->freq;
2397 static int wpas_start_listen(void *ctx, unsigned int freq,
2398 unsigned int duration,
2399 const struct wpabuf *probe_resp_ie)
2401 struct wpa_supplicant *wpa_s = ctx;
2402 struct wpas_p2p_listen_work *lwork;
2404 if (wpa_s->p2p_listen_work) {
2405 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2409 lwork = os_zalloc(sizeof(*lwork));
2413 lwork->duration = duration;
2414 if (probe_resp_ie) {
2415 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2416 if (lwork->probe_resp_ie == NULL) {
2417 wpas_p2p_listen_work_free(lwork);
2422 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2424 wpas_p2p_listen_work_free(lwork);
2432 static void wpas_stop_listen(void *ctx)
2434 struct wpa_supplicant *wpa_s = ctx;
2435 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2436 wpa_drv_cancel_remain_on_channel(wpa_s);
2437 wpa_s->off_channel_freq = 0;
2438 wpa_s->roc_waiting_drv_freq = 0;
2440 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2443 * Don't cancel Probe Request RX reporting for a connected P2P Client
2444 * handling Probe Request frames.
2446 if (!wpa_s->p2p_cli_probe)
2447 wpa_drv_probe_req_report(wpa_s, 0);
2449 wpas_p2p_listen_work_done(wpa_s);
2453 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2456 struct wpa_supplicant *wpa_s = ctx;
2457 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2462 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2463 const u8 *peer, const char *params,
2464 unsigned int generated_pin)
2466 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2467 " %08d%s", MAC2STR(peer), generated_pin, params);
2471 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2472 const u8 *peer, const char *params)
2474 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2475 "%s", MAC2STR(peer), params);
2479 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2480 const u8 *dev_addr, const u8 *pri_dev_type,
2481 const char *dev_name, u16 supp_config_methods,
2482 u8 dev_capab, u8 group_capab, const u8 *group_id,
2483 size_t group_id_len)
2485 struct wpa_supplicant *wpa_s = ctx;
2486 char devtype[WPS_DEV_TYPE_BUFSIZE];
2488 u8 empty_dev_type[8];
2489 unsigned int generated_pin = 0;
2490 struct wpa_supplicant *group = NULL;
2494 for (group = wpa_s->global->ifaces; group; group = group->next)
2496 struct wpa_ssid *s = group->current_ssid;
2498 s->mode == WPAS_MODE_P2P_GO &&
2499 group_id_len - ETH_ALEN == s->ssid_len &&
2500 os_memcmp(group_id + ETH_ALEN, s->ssid,
2506 if (pri_dev_type == NULL) {
2507 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2508 pri_dev_type = empty_dev_type;
2510 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2511 " pri_dev_type=%s name='%s' config_methods=0x%x "
2512 "dev_capab=0x%x group_capab=0x%x%s%s",
2514 wps_dev_type_bin2str(pri_dev_type, devtype,
2516 dev_name, supp_config_methods, dev_capab, group_capab,
2517 group ? " group=" : "",
2518 group ? group->ifname : "");
2519 if (os_snprintf_error(sizeof(params), res))
2520 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
2521 params[sizeof(params) - 1] = '\0';
2523 if (config_methods & WPS_CONFIG_DISPLAY) {
2524 generated_pin = wps_generate_pin();
2525 wpas_prov_disc_local_display(wpa_s, peer, params,
2527 } else if (config_methods & WPS_CONFIG_KEYPAD)
2528 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2529 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2530 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2531 MACSTR "%s", MAC2STR(peer), params);
2533 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2534 P2P_PROV_DISC_SUCCESS,
2535 config_methods, generated_pin);
2539 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2541 struct wpa_supplicant *wpa_s = ctx;
2542 unsigned int generated_pin = 0;
2545 if (wpa_s->pending_pd_before_join &&
2546 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2547 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2548 wpa_s->pending_pd_before_join = 0;
2549 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2550 "join-existing-group operation");
2551 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2555 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2556 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2559 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2560 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2561 if (os_snprintf_error(sizeof(params), res))
2562 params[sizeof(params) - 1] = '\0';
2566 if (config_methods & WPS_CONFIG_DISPLAY)
2567 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2568 else if (config_methods & WPS_CONFIG_KEYPAD) {
2569 generated_pin = wps_generate_pin();
2570 wpas_prov_disc_local_display(wpa_s, peer, params,
2572 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2573 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2574 MACSTR "%s", MAC2STR(peer), params);
2576 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2577 P2P_PROV_DISC_SUCCESS,
2578 config_methods, generated_pin);
2582 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2583 enum p2p_prov_disc_status status,
2584 u32 adv_id, const u8 *adv_mac,
2585 const char *deferred_session_resp)
2587 struct wpa_supplicant *wpa_s = ctx;
2589 if (wpa_s->p2p_fallback_to_go_neg) {
2590 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2591 "failed - fall back to GO Negotiation");
2592 wpa_msg_global(wpa_s->parent, MSG_INFO,
2593 P2P_EVENT_FALLBACK_TO_GO_NEG
2594 "reason=PD-failed");
2595 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2599 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2600 wpa_s->pending_pd_before_join = 0;
2601 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2602 "join-existing-group operation (no ACK for PD "
2604 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2608 if (adv_id && adv_mac && deferred_session_resp) {
2609 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2610 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2611 " deferred_session_resp='%s'",
2612 MAC2STR(peer), status, adv_id,
2613 deferred_session_resp);
2614 } else if (adv_id && adv_mac) {
2615 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2616 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2617 MAC2STR(peer), status, adv_id);
2619 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2620 " p2p_dev_addr=" MACSTR " status=%d",
2621 MAC2STR(peer), status);
2624 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2629 static int freq_included(struct wpa_supplicant *wpa_s,
2630 const struct p2p_channels *channels,
2633 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2634 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2640 static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2642 unsigned int num = P2P_MAX_CHANNELS;
2646 p2p_go_dump_common_freqs(wpa_s);
2647 common_freqs = os_calloc(num, sizeof(int));
2651 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2653 wpa_dbg(wpa_s, MSG_DEBUG,
2654 "P2P: Failed to get group common freqs");
2655 os_free(common_freqs);
2659 os_free(wpa_s->p2p_group_common_freqs);
2660 wpa_s->p2p_group_common_freqs = common_freqs;
2661 wpa_s->p2p_group_common_freqs_num = num;
2662 p2p_go_dump_common_freqs(wpa_s);
2667 * Check if the given frequency is one of the possible operating frequencies
2668 * set after the completion of the GO Negotiation.
2670 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2674 p2p_go_dump_common_freqs(wpa_s);
2676 /* assume no restrictions */
2677 if (!wpa_s->p2p_group_common_freqs_num)
2680 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2681 if (wpa_s->p2p_group_common_freqs[i] == freq)
2689 * Pick the best frequency to use from all the currently used frequencies.
2691 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2692 struct wpa_used_freq_data *freqs,
2697 /* find a candidate freq that is supported by P2P */
2698 for (c = 0; c < num; c++)
2699 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2705 /* once we have a candidate, try to find a 'better' one */
2706 for (i = c + 1; i < num; i++) {
2707 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2711 * 1. Infrastructure station interfaces have higher preference.
2712 * 2. P2P Clients have higher preference.
2715 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2720 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2723 return freqs[c].freq;
2727 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2728 const u8 *go_dev_addr, const u8 *ssid,
2729 size_t ssid_len, int *go, u8 *group_bssid,
2730 int *force_freq, int persistent_group,
2731 const struct p2p_channels *channels,
2734 struct wpa_supplicant *wpa_s = ctx;
2736 struct wpa_used_freq_data *freqs;
2737 struct wpa_supplicant *grp;
2740 if (!persistent_group) {
2741 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2742 " to join an active group (SSID: %s)",
2743 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2744 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2745 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2747 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2748 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2749 "authorized invitation");
2753 #ifdef CONFIG_WPS_NFC
2754 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2755 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
2756 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2757 wpa_s->p2p_wps_method = WPS_NFC;
2758 wpa_s->pending_join_wps_method = WPS_NFC;
2759 os_memcpy(wpa_s->pending_join_dev_addr,
2760 go_dev_addr, ETH_ALEN);
2761 os_memcpy(wpa_s->pending_join_iface_addr,
2765 #endif /* CONFIG_WPS_NFC */
2768 * Do not accept the invitation automatically; notify user and
2771 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2774 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2776 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2777 "running persistent group");
2779 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2783 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2784 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2785 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2786 "invitation to re-invoke a persistent group");
2787 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2788 } else if (!wpa_s->conf->persistent_reconnect)
2789 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2791 for (s = wpa_s->conf->ssid; s; s = s->next) {
2792 if (s->disabled == 2 &&
2793 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2794 s->ssid_len == ssid_len &&
2795 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2800 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2801 " requested reinvocation of an unknown group",
2803 return P2P_SC_FAIL_UNKNOWN_GROUP;
2806 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2808 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2809 wpa_printf(MSG_DEBUG, "P2P: The only available "
2810 "interface is already in use - reject "
2812 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2814 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2815 } else if (s->mode == WPAS_MODE_P2P_GO) {
2817 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2819 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2820 "interface address for the group");
2821 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2823 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2828 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2831 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2832 sizeof(struct wpa_used_freq_data));
2834 int num_channels = wpa_s->num_multichan_concurrent;
2835 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2836 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2840 /* Get one of the frequencies currently in use */
2841 if (best_freq > 0) {
2842 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2843 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
2845 if (wpa_s->num_multichan_concurrent < 2 ||
2846 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2847 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2848 *force_freq = best_freq;
2852 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2853 wpas_p2p_num_unused_channels(wpa_s) > 0) {
2855 /* We are the client */
2856 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2857 "running a GO but we are capable of MCC, "
2858 "figure out the best channel to use");
2860 } else if (!freq_included(wpa_s, channels, *force_freq)) {
2861 /* We are the GO, and *force_freq is not in the
2863 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2864 "in intersection but we are capable of MCC, "
2865 "figure out the best channel to use",
2871 return P2P_SC_SUCCESS;
2875 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2876 const u8 *ssid, size_t ssid_len,
2877 const u8 *go_dev_addr, u8 status,
2880 struct wpa_supplicant *wpa_s = ctx;
2883 for (s = wpa_s->conf->ssid; s; s = s->next) {
2884 if (s->disabled == 2 &&
2885 s->ssid_len == ssid_len &&
2886 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2890 if (status == P2P_SC_SUCCESS) {
2891 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2892 " was accepted; op_freq=%d MHz, SSID=%s",
2893 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
2895 int go = s->mode == WPAS_MODE_P2P_GO;
2896 wpas_p2p_group_add_persistent(
2897 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
2898 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
2901 wpa_s->user_initiated_pd = 0;
2902 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2903 wpa_s->p2p_wps_method, 0, op_freq,
2909 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2910 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2911 " was rejected (status %u)", MAC2STR(sa), status);
2917 wpa_msg_global(wpa_s, MSG_INFO,
2918 P2P_EVENT_INVITATION_RECEIVED
2919 "sa=" MACSTR " go_dev_addr=" MACSTR
2920 " bssid=" MACSTR " unknown-network",
2921 MAC2STR(sa), MAC2STR(go_dev_addr),
2924 wpa_msg_global(wpa_s, MSG_INFO,
2925 P2P_EVENT_INVITATION_RECEIVED
2926 "sa=" MACSTR " go_dev_addr=" MACSTR
2928 MAC2STR(sa), MAC2STR(go_dev_addr));
2930 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
2935 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2936 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2937 "sa=" MACSTR " persistent=%d freq=%d",
2938 MAC2STR(sa), s->id, op_freq);
2940 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2941 "sa=" MACSTR " persistent=%d",
2942 MAC2STR(sa), s->id);
2944 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
2949 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2950 struct wpa_ssid *ssid,
2951 const u8 *peer, int inv)
2954 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
2959 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2960 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
2964 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
2965 if (ssid->mode != WPAS_MODE_P2P_GO &&
2966 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2967 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2968 "due to invitation result", ssid->id);
2969 wpas_notify_network_removed(wpa_s, ssid);
2970 wpa_config_remove_network(wpa_s->conf, ssid->id);
2973 return; /* Peer not found in client list */
2976 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2977 "group %d client list%s",
2978 MAC2STR(peer), ssid->id,
2979 inv ? " due to invitation result" : "");
2980 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
2981 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
2982 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
2983 ssid->num_p2p_clients--;
2984 if (p2p_wpa_s->conf->update_config &&
2985 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
2986 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2990 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2993 struct wpa_ssid *ssid;
2995 wpa_s = wpa_s->global->p2p_invite_group;
2997 return; /* No known invitation group */
2998 ssid = wpa_s->current_ssid;
2999 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3000 !ssid->p2p_persistent_group)
3001 return; /* Not operating as a GO in persistent group */
3002 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3003 ssid->ssid, ssid->ssid_len);
3004 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3008 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3009 const struct p2p_channels *channels,
3010 const u8 *peer, int neg_freq,
3013 struct wpa_supplicant *wpa_s = ctx;
3014 struct wpa_ssid *ssid;
3018 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3019 "status=%d " MACSTR,
3020 status, MAC2STR(bssid));
3022 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3023 "status=%d ", status);
3025 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3027 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3028 status, MAC2STR(peer));
3029 if (wpa_s->pending_invite_ssid_id == -1) {
3030 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3031 wpas_remove_persistent_client(wpa_s, peer);
3032 return; /* Invitation to active group */
3035 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3036 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3037 "invitation exchange to indicate readiness for "
3041 if (status != P2P_SC_SUCCESS) {
3042 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3043 ssid = wpa_config_get_network(
3044 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3045 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3047 wpas_p2p_remove_pending_group_interface(wpa_s);
3051 ssid = wpa_config_get_network(wpa_s->conf,
3052 wpa_s->pending_invite_ssid_id);
3054 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3055 "data matching with invitation");
3060 * The peer could have missed our ctrl::ack frame for Invitation
3061 * Response and continue retransmitting the frame. To reduce the
3062 * likelihood of the peer not getting successful TX status for the
3063 * Invitation Response frame, wait a short time here before starting
3064 * the persistent group so that we will remain on the current channel to
3065 * acknowledge any possible retransmission from the peer.
3067 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3068 "starting persistent group");
3071 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3072 freq_included(wpa_s, channels, neg_freq))
3074 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3075 freq_included(wpa_s, channels, peer_oper_freq))
3076 freq = peer_oper_freq;
3080 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3081 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3082 wpas_p2p_group_add_persistent(wpa_s, ssid,
3083 ssid->mode == WPAS_MODE_P2P_GO,
3084 wpa_s->p2p_persistent_go_freq,
3086 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3088 ssid->mode == WPAS_MODE_P2P_GO ?
3089 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3094 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3097 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3099 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3103 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3105 reg->channel[reg->channels] = chan;
3110 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3111 struct p2p_channels *chan,
3112 struct p2p_channels *cli_chan)
3116 wpa_s->global->p2p_24ghz_social_channels = 1;
3118 os_memset(cli_chan, 0, sizeof(*cli_chan));
3120 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3123 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3124 chan->reg_class[cla].reg_class = 81;
3125 chan->reg_class[cla].channels = 0;
3126 for (i = 0; i < 11; i++) {
3127 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3128 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3130 if (chan->reg_class[cla].channels)
3133 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3136 /* Operating class 115 - 5 GHz, channels 36-48 */
3137 chan->reg_class[cla].reg_class = 115;
3138 chan->reg_class[cla].channels = 0;
3139 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3140 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3141 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3142 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3143 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3144 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3145 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3146 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3147 if (chan->reg_class[cla].channels)
3150 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3153 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3154 chan->reg_class[cla].reg_class = 124;
3155 chan->reg_class[cla].channels = 0;
3156 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3157 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3158 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3159 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3160 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3161 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3162 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3163 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3164 if (chan->reg_class[cla].channels)
3167 chan->reg_classes = cla;
3172 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3174 enum hostapd_hw_mode mode)
3178 for (i = 0; i < num_modes; i++) {
3179 if (modes[i].mode == mode)
3188 NOT_ALLOWED, NO_IR, ALLOWED
3191 static int has_channel(struct wpa_global *global,
3192 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3197 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3199 if (wpas_p2p_disallowed_freq(global, freq))
3202 for (i = 0; i < mode->num_channels; i++) {
3203 if (mode->channels[i].chan == chan) {
3205 *flags = mode->channels[i].flag;
3206 if (mode->channels[i].flag &
3207 (HOSTAPD_CHAN_DISABLED |
3208 HOSTAPD_CHAN_RADAR))
3210 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3220 struct p2p_oper_class_map {
3221 enum hostapd_hw_mode mode;
3226 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3229 static const struct p2p_oper_class_map op_class[] = {
3230 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3231 #if 0 /* Do not enable HT40 on 2 GHz for now */
3232 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3233 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3235 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3236 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3237 { HOSTAPD_MODE_IEEE80211A, 125, 149, 169, 4, BW20 },
3238 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3239 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3240 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3241 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3244 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3245 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3246 * 80 MHz, but currently use the following definition for simplicity
3247 * (these center frequencies are not actual channels, which makes
3248 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3249 * removing invalid channels.
3251 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3252 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3253 { -1, 0, 0, 0, 0, BW20 }
3257 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3258 struct hostapd_hw_modes *mode,
3261 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3264 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3267 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3269 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3270 * so the center channel is 6 channels away from the start/end.
3272 if (channel >= center_channels[i] - 6 &&
3273 channel <= center_channels[i] + 6)
3274 return center_channels[i];
3280 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3281 struct hostapd_hw_modes *mode,
3286 enum chan_allowed res, ret = ALLOWED;
3288 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3291 if (center_chan >= 58 && center_chan <= 138)
3292 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3294 /* check all the channels are available */
3295 for (i = 0; i < 4; i++) {
3296 int adj_chan = center_chan - 6 + i * 4;
3298 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3299 if (res == NOT_ALLOWED)
3304 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3306 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3308 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3310 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3318 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3319 struct hostapd_hw_modes *mode,
3323 enum chan_allowed res, res2;
3325 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3326 if (bw == BW40MINUS) {
3327 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3329 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3330 } else if (bw == BW40PLUS) {
3331 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3333 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3334 } else if (bw == BW80) {
3335 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3338 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3340 if (res == NO_IR || res2 == NO_IR)
3346 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3347 struct p2p_channels *chan,
3348 struct p2p_channels *cli_chan)
3350 struct hostapd_hw_modes *mode;
3351 int cla, op, cli_cla;
3353 if (wpa_s->hw.modes == NULL) {
3354 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3355 "of all supported channels; assume dualband "
3357 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3362 for (op = 0; op_class[op].op_class; op++) {
3363 const struct p2p_oper_class_map *o = &op_class[op];
3365 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3367 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3370 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3371 wpa_s->global->p2p_24ghz_social_channels = 1;
3372 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3373 enum chan_allowed res;
3374 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3375 if (res == ALLOWED) {
3377 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3379 reg = &chan->reg_class[cla];
3381 reg->reg_class = o->op_class;
3383 reg->channel[reg->channels] = ch;
3385 } else if (res == NO_IR &&
3386 wpa_s->conf->p2p_add_cli_chan) {
3387 if (cli_reg == NULL) {
3388 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3390 cli_reg = &cli_chan->reg_class[cli_cla];
3392 cli_reg->reg_class = o->op_class;
3394 cli_reg->channel[cli_reg->channels] = ch;
3395 cli_reg->channels++;
3399 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3400 reg->channel, reg->channels);
3403 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3404 cli_reg->channel, cli_reg->channels);
3408 chan->reg_classes = cla;
3409 cli_chan->reg_classes = cli_cla;
3415 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3416 struct hostapd_hw_modes *mode, u8 channel)
3419 enum chan_allowed ret;
3421 for (op = 0; op_class[op].op_class; op++) {
3422 const struct p2p_oper_class_map *o = &op_class[op];
3425 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3426 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3427 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3430 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3432 return (o->bw == BW40MINUS) ? -1 : 1;
3439 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3440 struct hostapd_hw_modes *mode, u8 channel)
3442 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3445 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3449 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3452 struct wpa_supplicant *wpa_s = ctx;
3454 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3455 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3461 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3465 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3466 const u8 *ssid, size_t ssid_len)
3468 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3469 struct wpa_ssid *s = wpa_s->current_ssid;
3472 if (s->mode != WPAS_MODE_P2P_GO &&
3473 s->mode != WPAS_MODE_AP &&
3474 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3476 if (s->ssid_len != ssid_len ||
3477 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3487 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3488 const u8 *peer_dev_addr)
3490 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3491 struct wpa_ssid *ssid = wpa_s->current_ssid;
3492 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
3494 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3502 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3504 struct wpa_supplicant *wpa_s = ctx;
3506 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3510 static int wpas_is_concurrent_session_active(void *ctx)
3512 struct wpa_supplicant *wpa_s = ctx;
3513 struct wpa_supplicant *ifs;
3515 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3518 if (ifs->wpa_state > WPA_ASSOCIATED)
3525 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3527 struct wpa_supplicant *wpa_s = ctx;
3528 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3532 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3533 const char *conf_p2p_dev)
3535 struct wpa_interface iface;
3536 struct wpa_supplicant *p2pdev_wpa_s;
3538 char force_name[100];
3541 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3543 if (os_snprintf_error(sizeof(ifname), ret))
3545 force_name[0] = '\0';
3546 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3547 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3548 force_name, wpa_s->pending_interface_addr, NULL);
3550 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3553 os_strlcpy(wpa_s->pending_interface_name, ifname,
3554 sizeof(wpa_s->pending_interface_name));
3556 os_memset(&iface, 0, sizeof(iface));
3558 iface.ifname = wpa_s->pending_interface_name;
3559 iface.driver = wpa_s->driver->name;
3560 iface.driver_param = wpa_s->conf->driver_param;
3563 * If a P2P Device configuration file was given, use it as the interface
3564 * configuration file (instead of using parent's configuration file.
3567 iface.confname = conf_p2p_dev;
3568 iface.ctrl_interface = NULL;
3570 iface.confname = wpa_s->confname;
3571 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3574 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
3575 if (!p2pdev_wpa_s) {
3576 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3580 wpa_s->pending_interface_name[0] = '\0';
3585 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3586 const u8 *noa, size_t noa_len)
3588 struct wpa_supplicant *wpa_s, *intf = ctx;
3591 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3592 if (wpa_s->waiting_presence_resp)
3596 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3599 wpa_s->waiting_presence_resp = 0;
3601 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3602 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3603 " status=%u noa=%s", MAC2STR(src), status, hex);
3607 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3608 size_t ssid_len, u8 *go_dev_addr,
3609 u8 *ret_ssid, size_t *ret_ssid_len,
3610 u8 *intended_iface_addr)
3612 struct wpa_supplicant *wpa_s = ctx;
3615 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3617 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3618 *ret_ssid_len = s->ssid_len;
3619 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3621 if (s->mode != WPAS_MODE_P2P_GO) {
3622 os_memset(intended_iface_addr, 0, ETH_ALEN);
3623 } else if (wpas_p2p_create_iface(wpa_s)) {
3624 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3627 os_memcpy(intended_iface_addr,
3628 wpa_s->pending_interface_addr, ETH_ALEN);
3630 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3640 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3641 u8 *ssid, size_t *ssid_len, int *group_iface)
3643 struct wpa_supplicant *wpa_s = ctx;
3648 * group_iface will be set to 1 only if a dedicated interface for P2P
3649 * role is required. First, we try to reuse an active GO. However,
3650 * if it is not present, we will try to reactivate an existing
3651 * persistent group and set group_iface to 1, so the caller will know
3652 * that the pending interface should be used.
3655 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
3657 s = wpas_p2p_get_persistent_go(wpa_s);
3658 *group_iface = wpas_p2p_create_iface(wpa_s);
3660 os_memcpy(bssid, s->bssid, ETH_ALEN);
3665 os_memcpy(intended_addr, bssid, ETH_ALEN);
3666 os_memcpy(ssid, s->ssid, s->ssid_len);
3667 *ssid_len = s->ssid_len;
3673 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3674 const u8 *ssid, size_t ssid_len)
3676 struct wpa_supplicant *wpa_s = ctx;
3678 int save_config = 0;
3681 /* Start with our first choice of Persistent Groups */
3682 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3683 if (go && ssid && ssid_len &&
3684 s->ssid_len == ssid_len &&
3685 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3686 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3689 /* Remove stale persistent group */
3690 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3691 wpa_config_remove_network(wpa_s->conf, s->id);
3696 for (i = 0; i < s->num_p2p_clients; i++) {
3697 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3698 peer, ETH_ALEN) != 0)
3701 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3702 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3703 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3706 s->num_p2p_clients--;
3711 p2p_config_write(wpa_s);
3713 /* Return TRUE if valid SSID remains */
3718 static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3719 const u8 *feat_cap, size_t feat_cap_len)
3721 static const char pref[] = " feature_cap=";
3727 * We expect a feature capability to contain at least one byte to be
3728 * reported. The string buffer provided by the caller function is
3729 * expected to be big enough to contain all bytes of the attribute for
3730 * known specifications. This function truncates the reported bytes if
3731 * the feature capability data exceeds the string buffer size.
3733 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3736 os_memcpy(buf, pref, sizeof(pref));
3737 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3738 buf_len - sizeof(pref) + 1,
3739 feat_cap, feat_cap_len);
3741 if (ret != (2 * (int) feat_cap_len))
3742 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3746 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3747 const u8 *adv_mac, const u8 *ses_mac,
3748 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3749 u8 conncap, int passwd_id,
3750 const u8 *persist_ssid,
3751 size_t persist_ssid_size, int response_done,
3752 int prov_start, const char *session_info,
3753 const u8 *feat_cap, size_t feat_cap_len)
3755 struct wpa_supplicant *wpa_s = ctx;
3757 struct wpa_ssid *persistent_go, *stale, *s;
3758 int save_config = 0;
3759 struct wpa_supplicant *go_wpa_s;
3760 char feat_cap_str[256];
3765 os_memset(mac, 0, ETH_ALEN);
3773 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3774 feat_cap, feat_cap_len);
3777 if (session_info == NULL) {
3778 wpa_msg_global(wpa_s, MSG_INFO,
3779 P2P_EVENT_P2PS_PROVISION_START MACSTR
3780 " adv_id=%x conncap=%x"
3782 " session=%x mac=" MACSTR
3783 " dev_passwd_id=%d%s",
3784 MAC2STR(dev), adv_id, conncap,
3786 ses_id, MAC2STR(ses_mac),
3787 passwd_id, feat_cap_str);
3789 wpa_msg_global(wpa_s, MSG_INFO,
3790 P2P_EVENT_P2PS_PROVISION_START MACSTR
3791 " adv_id=%x conncap=%x"
3793 " session=%x mac=" MACSTR
3794 " dev_passwd_id=%d info='%s'%s",
3795 MAC2STR(dev), adv_id, conncap,
3797 ses_id, MAC2STR(ses_mac),
3798 passwd_id, session_info, feat_cap_str);
3803 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3804 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3806 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3807 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3808 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3810 if (persistent_go && !persistent_go->num_p2p_clients) {
3811 /* remove empty persistent GO */
3812 wpa_config_remove_network(wpa_s->conf,
3816 wpa_msg_global(wpa_s, MSG_INFO,
3817 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3819 " adv_id=%x adv_mac=" MACSTR
3820 " session=%x mac=" MACSTR "%s",
3821 MAC2STR(dev), status,
3822 adv_id, MAC2STR(adv_mac),
3823 ses_id, MAC2STR(ses_mac), feat_cap_str);
3827 /* Clean up stale persistent groups with this device */
3828 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3831 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
3832 is_zero_ether_addr(grp_mac)) {
3833 wpa_dbg(wpa_s, MSG_ERROR,
3834 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
3839 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3843 if (s && s->ssid_len == stale->ssid_len &&
3844 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3845 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3848 /* Remove stale persistent group */
3849 if (stale->mode != WPAS_MODE_P2P_GO ||
3850 stale->num_p2p_clients <= 1) {
3851 wpa_config_remove_network(wpa_s->conf, stale->id);
3855 for (i = 0; i < stale->num_p2p_clients; i++) {
3856 if (os_memcmp(stale->p2p_client_list +
3858 dev, ETH_ALEN) == 0) {
3859 os_memmove(stale->p2p_client_list +
3861 stale->p2p_client_list +
3863 (stale->num_p2p_clients -
3868 stale->num_p2p_clients--;
3874 p2p_config_write(wpa_s);
3877 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3878 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3880 if (persistent_go && s != persistent_go &&
3881 !persistent_go->num_p2p_clients) {
3882 /* remove empty persistent GO */
3883 wpa_config_remove_network(wpa_s->conf,
3888 wpa_msg_global(wpa_s, MSG_INFO,
3889 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3891 " adv_id=%x adv_mac=" MACSTR
3892 " session=%x mac=" MACSTR
3894 MAC2STR(dev), status,
3895 adv_id, MAC2STR(adv_mac),
3896 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
3900 if (conncap == P2PS_SETUP_GROUP_OWNER) {
3902 * We need to copy the interface name. Simply saving a
3903 * pointer isn't enough, since if we use pending_interface_name
3904 * it will be overwritten when the group is added.
3906 char go_ifname[100];
3908 go_ifname[0] = '\0';
3910 wpa_s->global->pending_p2ps_group = 1;
3912 if (!wpas_p2p_create_iface(wpa_s))
3913 os_memcpy(go_ifname, wpa_s->ifname,
3915 else if (wpa_s->pending_interface_name[0])
3916 os_memcpy(go_ifname,
3917 wpa_s->pending_interface_name,
3920 if (!go_ifname[0]) {
3921 wpas_p2ps_prov_complete(
3922 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3923 dev, adv_mac, ses_mac,
3924 grp_mac, adv_id, ses_id, 0, 0,
3925 NULL, 0, 0, 0, NULL, NULL, 0);
3929 /* If PD Resp complete, start up the GO */
3930 if (response_done && persistent_go) {
3931 wpas_p2p_group_add_persistent(
3932 wpa_s, persistent_go,
3933 0, 0, 0, 0, 0, NULL,
3934 persistent_go->mode ==
3936 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3938 } else if (response_done) {
3939 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3942 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3943 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
3945 wpa_s->p2ps_method_config_any = 1;
3947 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3948 os_memcpy(go_ifname, go_wpa_s->ifname,
3951 if (is_zero_ether_addr(grp_mac)) {
3952 wpa_dbg(go_wpa_s, MSG_DEBUG,
3953 "P2P: Setting PIN-1 for ANY");
3954 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
3955 "12345670", NULL, 0,
3958 wpa_dbg(go_wpa_s, MSG_DEBUG,
3959 "P2P: Setting PIN-1 for " MACSTR,
3961 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
3962 "12345670", NULL, 0,
3966 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
3967 wpa_s->p2ps_method_config_any = 1;
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 go=%s%s",
3976 MAC2STR(dev), status, conncap,
3977 adv_id, MAC2STR(adv_mac),
3978 ses_id, MAC2STR(ses_mac),
3979 passwd_id, go_ifname, feat_cap_str);
3983 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3984 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3986 if (persistent_go && !persistent_go->num_p2p_clients) {
3987 /* remove empty persistent GO */
3988 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
3991 if (conncap == P2PS_SETUP_CLIENT) {
3992 wpa_msg_global(wpa_s, MSG_INFO,
3993 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3994 " status=%d conncap=%x"
3995 " adv_id=%x adv_mac=" MACSTR
3996 " session=%x mac=" MACSTR
3997 " dev_passwd_id=%d join=" MACSTR "%s",
3998 MAC2STR(dev), status, conncap,
3999 adv_id, MAC2STR(adv_mac),
4000 ses_id, MAC2STR(ses_mac),
4001 passwd_id, MAC2STR(grp_mac), feat_cap_str);
4003 wpa_msg_global(wpa_s, MSG_INFO,
4004 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4005 " status=%d conncap=%x"
4006 " adv_id=%x adv_mac=" MACSTR
4007 " session=%x mac=" MACSTR
4008 " dev_passwd_id=%d%s",
4009 MAC2STR(dev), status, conncap,
4010 adv_id, MAC2STR(adv_mac),
4011 ses_id, MAC2STR(ses_mac),
4012 passwd_id, feat_cap_str);
4017 static int _wpas_p2p_in_progress(void *ctx)
4019 struct wpa_supplicant *wpa_s = ctx;
4020 return wpas_p2p_in_progress(wpa_s);
4024 static int wpas_prov_disc_resp_cb(void *ctx)
4026 struct wpa_supplicant *wpa_s = ctx;
4027 struct wpa_ssid *persistent_go;
4029 if (!wpa_s->global->pending_p2ps_group)
4032 wpa_s->global->pending_p2ps_group = 0;
4034 if (wpas_p2p_get_go_group(wpa_s))
4036 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4038 if (persistent_go) {
4039 wpas_p2p_group_add_persistent(
4040 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
4041 persistent_go->mode == WPAS_MODE_P2P_GO ?
4042 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
4044 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
4051 static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4053 unsigned int *freq_list)
4055 struct wpa_supplicant *wpa_s = ctx;
4057 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4058 WPA_IF_P2P_CLIENT, len, freq_list);
4063 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4064 * @global: Pointer to global data from wpa_supplicant_init()
4065 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4066 * Returns: 0 on success, -1 on failure
4068 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4070 struct p2p_config p2p;
4073 if (wpa_s->conf->p2p_disabled)
4076 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4082 os_memset(&p2p, 0, sizeof(p2p));
4084 p2p.debug_print = wpas_p2p_debug_print;
4085 p2p.p2p_scan = wpas_p2p_scan;
4086 p2p.send_action = wpas_send_action;
4087 p2p.send_action_done = wpas_send_action_done;
4088 p2p.go_neg_completed = wpas_go_neg_completed;
4089 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4090 p2p.dev_found = wpas_dev_found;
4091 p2p.dev_lost = wpas_dev_lost;
4092 p2p.find_stopped = wpas_find_stopped;
4093 p2p.start_listen = wpas_start_listen;
4094 p2p.stop_listen = wpas_stop_listen;
4095 p2p.send_probe_resp = wpas_send_probe_resp;
4096 p2p.sd_request = wpas_sd_request;
4097 p2p.sd_response = wpas_sd_response;
4098 p2p.prov_disc_req = wpas_prov_disc_req;
4099 p2p.prov_disc_resp = wpas_prov_disc_resp;
4100 p2p.prov_disc_fail = wpas_prov_disc_fail;
4101 p2p.invitation_process = wpas_invitation_process;
4102 p2p.invitation_received = wpas_invitation_received;
4103 p2p.invitation_result = wpas_invitation_result;
4104 p2p.get_noa = wpas_get_noa;
4105 p2p.go_connected = wpas_go_connected;
4106 p2p.presence_resp = wpas_presence_resp;
4107 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4108 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4109 p2p.get_persistent_group = wpas_get_persistent_group;
4110 p2p.get_go_info = wpas_get_go_info;
4111 p2p.remove_stale_groups = wpas_remove_stale_groups;
4112 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4113 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4114 p2p.p2ps_group_capability = p2ps_group_capability;
4115 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
4117 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4118 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4119 p2p.dev_name = wpa_s->conf->device_name;
4120 p2p.manufacturer = wpa_s->conf->manufacturer;
4121 p2p.model_name = wpa_s->conf->model_name;
4122 p2p.model_number = wpa_s->conf->model_number;
4123 p2p.serial_number = wpa_s->conf->serial_number;
4125 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4126 p2p.config_methods = wpa_s->wps->config_methods;
4129 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4130 wpa_printf(MSG_ERROR,
4131 "P2P: Failed to configure supported channel list");
4135 if (wpa_s->conf->p2p_listen_reg_class &&
4136 wpa_s->conf->p2p_listen_channel) {
4137 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4138 p2p.channel = wpa_s->conf->p2p_listen_channel;
4139 p2p.channel_forced = 1;
4142 * Pick one of the social channels randomly as the listen
4145 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4146 &p2p.channel) != 0) {
4147 wpa_printf(MSG_INFO,
4148 "P2P: No social channels supported by the driver - do not enable P2P");
4151 p2p.channel_forced = 0;
4153 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4154 p2p.reg_class, p2p.channel);
4156 if (wpa_s->conf->p2p_oper_reg_class &&
4157 wpa_s->conf->p2p_oper_channel) {
4158 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4159 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4160 p2p.cfg_op_channel = 1;
4161 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4162 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4166 * Use random operation channel from 2.4 GHz band social
4167 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4168 * other preference is indicated.
4170 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4171 &p2p.op_channel) != 0) {
4172 wpa_printf(MSG_ERROR,
4173 "P2P: Failed to select random social channel as operation channel");
4176 p2p.cfg_op_channel = 0;
4177 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4178 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4181 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4182 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4183 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4186 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4187 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4188 p2p.country[2] = 0x04;
4190 os_memcpy(p2p.country, "XX\x04", 3);
4192 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4195 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4196 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4197 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4199 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4200 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4202 p2p.max_peers = 100;
4204 if (wpa_s->conf->p2p_ssid_postfix) {
4205 p2p.ssid_postfix_len =
4206 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4207 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4208 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4209 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4210 p2p.ssid_postfix_len);
4213 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4215 p2p.max_listen = wpa_s->max_remain_on_chan;
4217 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4218 wpa_s->conf->p2p_passphrase_len <= 63)
4219 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4221 p2p.passphrase_len = 8;
4223 global->p2p = p2p_init(&p2p);
4224 if (global->p2p == NULL)
4226 global->p2p_init_wpa_s = wpa_s;
4228 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4229 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4231 p2p_add_wps_vendor_extension(
4232 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4235 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4242 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4243 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4245 * This function deinitialize per-interface P2P data.
4247 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4249 if (wpa_s->driver && wpa_s->drv_priv)
4250 wpa_drv_probe_req_report(wpa_s, 0);
4252 if (wpa_s->go_params) {
4253 /* Clear any stored provisioning info */
4254 p2p_clear_provisioning_info(
4256 wpa_s->go_params->peer_device_addr);
4259 os_free(wpa_s->go_params);
4260 wpa_s->go_params = NULL;
4261 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4262 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4263 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4264 wpa_s->p2p_long_listen = 0;
4265 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4266 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4267 wpas_p2p_remove_pending_group_interface(wpa_s);
4268 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4269 wpas_p2p_listen_work_done(wpa_s);
4270 if (wpa_s->p2p_send_action_work) {
4271 os_free(wpa_s->p2p_send_action_work->ctx);
4272 radio_work_done(wpa_s->p2p_send_action_work);
4273 wpa_s->p2p_send_action_work = NULL;
4275 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4277 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4278 wpa_s->p2p_oob_dev_pw = NULL;
4280 os_free(wpa_s->p2p_group_common_freqs);
4281 wpa_s->p2p_group_common_freqs = NULL;
4282 wpa_s->p2p_group_common_freqs_num = 0;
4284 /* TODO: remove group interface from the driver if this wpa_s instance
4285 * is on top of a P2P group interface */
4290 * wpas_p2p_deinit_global - Deinitialize global P2P module
4291 * @global: Pointer to global data from wpa_supplicant_init()
4293 * This function deinitializes the global (per device) P2P module.
4295 static void wpas_p2p_deinit_global(struct wpa_global *global)
4297 struct wpa_supplicant *wpa_s, *tmp;
4299 wpa_s = global->ifaces;
4301 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4303 /* Remove remaining P2P group interfaces */
4304 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4305 wpa_s = wpa_s->next;
4307 tmp = global->ifaces;
4310 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4315 /* Disconnect from the P2P group and deinit the interface */
4316 wpas_p2p_disconnect(tmp);
4320 * Deinit GO data on any possibly remaining interface (if main
4321 * interface is used as GO).
4323 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4324 if (wpa_s->ap_iface)
4325 wpas_p2p_group_deinit(wpa_s);
4328 p2p_deinit(global->p2p);
4330 global->p2p_init_wpa_s = NULL;
4334 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4336 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4337 wpa_s->conf->p2p_no_group_iface)
4338 return 0; /* separate interface disabled per configuration */
4339 if (wpa_s->drv_flags &
4340 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4341 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4342 return 1; /* P2P group requires a new interface in every case
4344 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4345 return 0; /* driver does not support concurrent operations */
4346 if (wpa_s->global->ifaces->next)
4347 return 1; /* more that one interface already in use */
4348 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4349 return 1; /* this interface is already in use */
4354 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4355 const u8 *peer_addr,
4356 enum p2p_wps_method wps_method,
4357 int go_intent, const u8 *own_interface_addr,
4358 unsigned int force_freq, int persistent_group,
4359 struct wpa_ssid *ssid, unsigned int pref_freq)
4361 if (persistent_group && wpa_s->conf->persistent_reconnect)
4362 persistent_group = 2;
4365 * Increase GO config timeout if HT40 is used since it takes some time
4366 * to scan channels for coex purposes before the BSS can be started.
4368 p2p_set_config_timeout(wpa_s->global->p2p,
4369 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4371 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4372 go_intent, own_interface_addr, force_freq,
4373 persistent_group, ssid ? ssid->ssid : NULL,
4374 ssid ? ssid->ssid_len : 0,
4375 wpa_s->p2p_pd_before_go_neg, pref_freq,
4376 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4381 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4382 const u8 *peer_addr,
4383 enum p2p_wps_method wps_method,
4384 int go_intent, const u8 *own_interface_addr,
4385 unsigned int force_freq, int persistent_group,
4386 struct wpa_ssid *ssid, unsigned int pref_freq)
4388 if (persistent_group && wpa_s->conf->persistent_reconnect)
4389 persistent_group = 2;
4391 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4392 go_intent, own_interface_addr, force_freq,
4393 persistent_group, ssid ? ssid->ssid : NULL,
4394 ssid ? ssid->ssid_len : 0, pref_freq,
4395 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4400 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4402 wpa_s->p2p_join_scan_count++;
4403 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4404 wpa_s->p2p_join_scan_count);
4405 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4406 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4407 " for join operationg - stop join attempt",
4408 MAC2STR(wpa_s->pending_join_iface_addr));
4409 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4410 if (wpa_s->p2p_auto_pd) {
4411 wpa_s->p2p_auto_pd = 0;
4412 wpa_msg_global(wpa_s, MSG_INFO,
4413 P2P_EVENT_PROV_DISC_FAILURE
4414 " p2p_dev_addr=" MACSTR " status=N/A",
4415 MAC2STR(wpa_s->pending_join_dev_addr));
4418 wpa_msg_global(wpa_s->parent, MSG_INFO,
4419 P2P_EVENT_GROUP_FORMATION_FAILURE);
4420 wpas_notify_p2p_group_formation_failure(wpa_s, "");
4425 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4428 unsigned int num, i;
4429 struct wpa_used_freq_data *freqs;
4431 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4432 /* Multiple channels are supported and not all are in use */
4436 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4437 sizeof(struct wpa_used_freq_data));
4441 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4442 wpa_s->num_multichan_concurrent);
4444 for (i = 0; i < num; i++) {
4445 if (freqs[i].freq == freq) {
4446 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4453 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4462 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4463 const u8 *peer_dev_addr)
4465 struct wpa_bss *bss;
4468 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4471 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4472 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4477 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4479 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4480 "%ld.%06ld (%supdated in last scan)",
4481 bss->last_update.sec, bss->last_update.usec,
4482 updated ? "": "not ");
4488 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4489 struct wpa_scan_results *scan_res)
4491 struct wpa_bss *bss = NULL;
4493 u8 iface_addr[ETH_ALEN];
4495 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4497 if (wpa_s->global->p2p_disabled)
4500 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4501 scan_res ? (int) scan_res->num : -1,
4502 wpa_s->p2p_auto_join ? "auto_" : "");
4505 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4507 if (wpa_s->p2p_auto_pd) {
4508 int join = wpas_p2p_peer_go(wpa_s,
4509 wpa_s->pending_join_dev_addr);
4511 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4512 wpa_s->auto_pd_scan_retry++;
4513 bss = wpa_bss_get_bssid_latest(
4514 wpa_s, wpa_s->pending_join_dev_addr);
4517 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4518 "the peer " MACSTR " at %d MHz",
4519 wpa_s->auto_pd_scan_retry,
4521 pending_join_dev_addr),
4523 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4531 wpa_s->p2p_auto_pd = 0;
4532 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4533 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4534 MAC2STR(wpa_s->pending_join_dev_addr), join);
4535 if (p2p_prov_disc_req(wpa_s->global->p2p,
4536 wpa_s->pending_join_dev_addr, NULL,
4537 wpa_s->pending_pd_config_methods, join,
4538 0, wpa_s->user_initiated_pd) < 0) {
4539 wpa_s->p2p_auto_pd = 0;
4540 wpa_msg_global(wpa_s, MSG_INFO,
4541 P2P_EVENT_PROV_DISC_FAILURE
4542 " p2p_dev_addr=" MACSTR " status=N/A",
4543 MAC2STR(wpa_s->pending_join_dev_addr));
4548 if (wpa_s->p2p_auto_join) {
4549 int join = wpas_p2p_peer_go(wpa_s,
4550 wpa_s->pending_join_dev_addr);
4552 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4553 "running a GO -> use GO Negotiation");
4554 wpa_msg_global(wpa_s->parent, MSG_INFO,
4555 P2P_EVENT_FALLBACK_TO_GO_NEG
4556 "reason=peer-not-running-GO");
4557 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4558 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4559 wpa_s->p2p_persistent_group, 0, 0, 0,
4560 wpa_s->p2p_go_intent,
4561 wpa_s->p2p_connect_freq,
4562 wpa_s->p2p_persistent_id,
4563 wpa_s->p2p_pd_before_go_neg,
4569 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4570 "try to join the group", join ? "" :
4573 wpa_msg_global(wpa_s->parent, MSG_INFO,
4574 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
4575 wpa_s->p2p_fallback_to_go_neg = 1;
4579 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4580 wpa_s->pending_join_iface_addr);
4582 p2p_get_interface_addr(wpa_s->global->p2p,
4583 wpa_s->pending_join_dev_addr,
4585 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4586 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4587 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4588 "address for join from " MACSTR " to " MACSTR
4589 " based on newly discovered P2P peer entry",
4590 MAC2STR(wpa_s->pending_join_iface_addr),
4591 MAC2STR(iface_addr));
4592 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4595 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4596 wpa_s->pending_join_iface_addr);
4599 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4600 "from P2P peer table: %d MHz", freq);
4602 if (wpa_s->p2p_join_ssid_len) {
4603 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4604 MACSTR " and SSID %s",
4605 MAC2STR(wpa_s->pending_join_iface_addr),
4606 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4607 wpa_s->p2p_join_ssid_len));
4608 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4609 wpa_s->p2p_join_ssid,
4610 wpa_s->p2p_join_ssid_len);
4613 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4614 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4615 bss = wpa_bss_get_bssid_latest(wpa_s,
4616 wpa_s->pending_join_iface_addr);
4619 u8 dev_addr[ETH_ALEN];
4622 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4623 "from BSS table: %d MHz (SSID %s)", freq,
4624 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4625 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4627 os_memcmp(wpa_s->pending_join_dev_addr,
4628 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4629 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4631 wpa_printf(MSG_DEBUG,
4632 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4634 MAC2STR(wpa_s->pending_join_dev_addr));
4635 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4642 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4643 wpa_msg_global(wpa_s->parent, MSG_INFO,
4644 P2P_EVENT_GROUP_FORMATION_FAILURE
4645 "reason=FREQ_CONFLICT");
4646 wpas_notify_p2p_group_formation_failure(
4647 wpa_s, "FREQ_CONFLICT");
4651 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4652 "prior to joining an existing group (GO " MACSTR
4654 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4655 wpa_s->pending_pd_before_join = 1;
4657 switch (wpa_s->pending_join_wps_method) {
4658 case WPS_PIN_DISPLAY:
4659 method = WPS_CONFIG_KEYPAD;
4661 case WPS_PIN_KEYPAD:
4662 method = WPS_CONFIG_DISPLAY;
4665 method = WPS_CONFIG_PUSHBUTTON;
4668 method = WPS_CONFIG_P2PS;
4675 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4676 wpa_s->pending_join_dev_addr) ==
4679 * We have already performed provision discovery for
4680 * joining the group. Proceed directly to join
4681 * operation without duplicated provision discovery. */
4682 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4683 "with " MACSTR " already done - proceed to "
4685 MAC2STR(wpa_s->pending_join_dev_addr));
4686 wpa_s->pending_pd_before_join = 0;
4690 if (p2p_prov_disc_req(wpa_s->global->p2p,
4691 wpa_s->pending_join_dev_addr,
4693 freq, wpa_s->user_initiated_pd) < 0) {
4694 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4695 "Discovery Request before joining an "
4697 wpa_s->pending_pd_before_join = 0;
4703 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4704 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4705 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4706 wpas_p2p_check_join_scan_limit(wpa_s);
4710 /* Start join operation immediately */
4711 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4715 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4716 const u8 *ssid, size_t ssid_len)
4719 struct wpa_driver_scan_params params;
4720 struct wpabuf *wps_ie, *ies;
4722 int freqs[2] = { 0, 0 };
4724 os_memset(¶ms, 0, sizeof(params));
4726 /* P2P Wildcard SSID */
4727 params.num_ssids = 1;
4728 if (ssid && ssid_len) {
4729 params.ssids[0].ssid = ssid;
4730 params.ssids[0].ssid_len = ssid_len;
4731 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4732 wpa_s->p2p_join_ssid_len = ssid_len;
4734 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4735 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4736 wpa_s->p2p_join_ssid_len = 0;
4739 wpa_s->wps->dev.p2p = 1;
4740 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4741 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4743 if (wps_ie == NULL) {
4744 wpas_p2p_scan_res_join(wpa_s, NULL);
4748 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4749 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4751 wpabuf_free(wps_ie);
4752 wpas_p2p_scan_res_join(wpa_s, NULL);
4755 wpabuf_put_buf(ies, wps_ie);
4756 wpabuf_free(wps_ie);
4758 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4760 params.p2p_probe = 1;
4761 params.extra_ies = wpabuf_head(ies);
4762 params.extra_ies_len = wpabuf_len(ies);
4767 * If freq is not provided, check the operating freq of the GO
4768 * and use a single channel scan on if possible.
4770 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4771 wpa_s->pending_join_iface_addr);
4777 params.freqs = freqs;
4781 * Run a scan to update BSS table and start Provision Discovery once
4782 * the new scan results become available.
4784 ret = wpa_drv_scan(wpa_s, ¶ms);
4786 os_get_reltime(&wpa_s->scan_trigger_time);
4787 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4788 wpa_s->own_scan_requested = 1;
4794 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4796 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4797 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4798 wpas_p2p_check_join_scan_limit(wpa_s);
4803 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4805 struct wpa_supplicant *wpa_s = eloop_ctx;
4806 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4810 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4811 const u8 *dev_addr, enum p2p_wps_method wps_method,
4812 int auto_join, int op_freq,
4813 const u8 *ssid, size_t ssid_len)
4815 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4816 MACSTR " dev " MACSTR " op_freq=%d)%s",
4817 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4818 auto_join ? " (auto_join)" : "");
4819 if (ssid && ssid_len) {
4820 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4821 wpa_ssid_txt(ssid, ssid_len));
4824 wpa_s->p2p_auto_pd = 0;
4825 wpa_s->p2p_auto_join = !!auto_join;
4826 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4827 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4828 wpa_s->pending_join_wps_method = wps_method;
4830 /* Make sure we are not running find during connection establishment */
4831 wpas_p2p_stop_find(wpa_s);
4833 wpa_s->p2p_join_scan_count = 0;
4834 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4839 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4840 const u8 *ssid, size_t ssid_len)
4842 struct wpa_supplicant *group;
4843 struct p2p_go_neg_results res;
4844 struct wpa_bss *bss;
4846 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4849 if (group != wpa_s) {
4850 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4851 sizeof(group->p2p_pin));
4852 group->p2p_wps_method = wpa_s->p2p_wps_method;
4855 * Need to mark the current interface for p2p_group_formation
4856 * when a separate group interface is not used. This is needed
4857 * to allow p2p_cancel stop a pending p2p_connect-join.
4858 * wpas_p2p_init_group_interface() addresses this for the case
4859 * where a separate group interface is used.
4861 wpa_s->global->p2p_group_formation = wpa_s;
4864 group->p2p_in_provisioning = 1;
4865 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4867 os_memset(&res, 0, sizeof(res));
4868 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4869 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4871 res.wps_method = wpa_s->pending_join_wps_method;
4872 if (freq && ssid && ssid_len) {
4874 res.ssid_len = ssid_len;
4875 os_memcpy(res.ssid, ssid, ssid_len);
4877 bss = wpa_bss_get_bssid_latest(wpa_s,
4878 wpa_s->pending_join_iface_addr);
4880 res.freq = bss->freq;
4881 res.ssid_len = bss->ssid_len;
4882 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4883 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4885 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4889 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4890 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4892 wpa_drv_cancel_remain_on_channel(wpa_s);
4893 wpa_s->off_channel_freq = 0;
4894 wpa_s->roc_waiting_drv_freq = 0;
4896 wpas_start_wps_enrollee(group, &res);
4899 * Allow a longer timeout for join-a-running-group than normal 15
4900 * second group formation timeout since the GO may not have authorized
4901 * our connection yet.
4903 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4904 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4911 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4912 int *force_freq, int *pref_freq, int go,
4913 unsigned int *pref_freq_list,
4914 unsigned int *num_pref_freq)
4916 struct wpa_used_freq_data *freqs;
4917 int res, best_freq, num_unused;
4918 unsigned int freq_in_use = 0, num, i, max_pref_freq;
4920 max_pref_freq = *num_pref_freq;
4923 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4924 sizeof(struct wpa_used_freq_data));
4928 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4929 wpa_s->num_multichan_concurrent);
4932 * It is possible that the total number of used frequencies is bigger
4933 * than the number of frequencies used for P2P, so get the system wide
4934 * number of unused frequencies.
4936 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4938 wpa_printf(MSG_DEBUG,
4939 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4940 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4945 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4947 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4949 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
4950 ieee80211_is_dfs(freq)) {
4952 * If freq is a DFS channel and DFS is offloaded
4953 * to the driver, allow P2P GO to use it.
4955 wpa_printf(MSG_DEBUG,
4956 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
4959 wpa_printf(MSG_DEBUG,
4960 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
4967 for (i = 0; i < num; i++) {
4968 if (freqs[i].freq == freq)
4972 if (num_unused <= 0 && !freq_in_use) {
4973 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4978 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4979 "requested channel (%u MHz)", freq);
4984 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4986 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
4987 enum wpa_driver_if_type iface_type;
4990 iface_type = WPA_IF_P2P_GO;
4992 iface_type = WPA_IF_P2P_CLIENT;
4994 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
4997 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5000 if (!res && max_pref_freq > 0) {
5001 *num_pref_freq = max_pref_freq;
5003 while (wpas_p2p_disallowed_freq(wpa_s->global,
5004 pref_freq_list[i]) &&
5005 i < *num_pref_freq) {
5006 wpa_printf(MSG_DEBUG,
5007 "P2P: preferred_freq_list[%d]=%d is disallowed",
5008 i, pref_freq_list[i]);
5011 if (i != *num_pref_freq) {
5012 best_freq = pref_freq_list[i];
5013 wpa_printf(MSG_DEBUG,
5014 "P2P: Using preferred_freq_list[%d]=%d",
5017 wpa_printf(MSG_DEBUG,
5018 "P2P: All driver preferred frequencies are disallowed for P2P use");
5022 wpa_printf(MSG_DEBUG,
5023 "P2P: No preferred frequency list available");
5027 /* We have a candidate frequency to use */
5028 if (best_freq > 0) {
5029 if (*pref_freq == 0 && num_unused > 0) {
5030 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
5032 *pref_freq = best_freq;
5034 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
5036 *force_freq = best_freq;
5038 } else if (num_unused > 0) {
5039 wpa_printf(MSG_DEBUG,
5040 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5043 wpa_printf(MSG_DEBUG,
5044 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5058 * wpas_p2p_connect - Request P2P Group Formation to be started
5059 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5060 * @peer_addr: Address of the peer P2P Device
5061 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5062 * @persistent_group: Whether to create a persistent group
5063 * @auto_join: Whether to select join vs. GO Negotiation automatically
5064 * @join: Whether to join an existing group (as a client) instead of starting
5065 * Group Owner negotiation; @peer_addr is BSSID in that case
5066 * @auth: Whether to only authorize the connection instead of doing that and
5067 * initiating Group Owner negotiation
5068 * @go_intent: GO Intent or -1 to use default
5069 * @freq: Frequency for the group or 0 for auto-selection
5070 * @persistent_id: Persistent group credentials to use for forcing GO
5071 * parameters or -1 to generate new values (SSID/passphrase)
5072 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5073 * interoperability workaround when initiating group formation
5074 * @ht40: Start GO with 40 MHz channel width
5075 * @vht: Start GO with VHT support
5076 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5077 * failure, -2 on failure due to channel not currently available,
5078 * -3 if forced channel is not supported
5080 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5081 const char *pin, enum p2p_wps_method wps_method,
5082 int persistent_group, int auto_join, int join, int auth,
5083 int go_intent, int freq, int persistent_id, int pd,
5086 int force_freq = 0, pref_freq = 0;
5088 enum wpa_driver_if_type iftype;
5090 struct wpa_ssid *ssid = NULL;
5091 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
5093 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5096 if (persistent_id >= 0) {
5097 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5098 if (ssid == NULL || ssid->disabled != 2 ||
5099 ssid->mode != WPAS_MODE_P2P_GO)
5103 os_free(wpa_s->global->add_psk);
5104 wpa_s->global->add_psk = NULL;
5106 wpa_s->global->p2p_fail_on_wps_complete = 0;
5107 wpa_s->global->pending_p2ps_group = 0;
5108 wpa_s->p2ps_method_config_any = 0;
5111 go_intent = wpa_s->conf->p2p_go_intent;
5114 wpa_s->p2p_long_listen = 0;
5116 wpa_s->p2p_wps_method = wps_method;
5117 wpa_s->p2p_persistent_group = !!persistent_group;
5118 wpa_s->p2p_persistent_id = persistent_id;
5119 wpa_s->p2p_go_intent = go_intent;
5120 wpa_s->p2p_connect_freq = freq;
5121 wpa_s->p2p_fallback_to_go_neg = 0;
5122 wpa_s->p2p_pd_before_go_neg = !!pd;
5123 wpa_s->p2p_go_ht40 = !!ht40;
5124 wpa_s->p2p_go_vht = !!vht;
5127 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5128 else if (wps_method == WPS_PIN_DISPLAY) {
5129 ret = wps_generate_pin();
5130 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5132 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5133 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5134 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5137 wpa_s->p2p_pin[0] = '\0';
5139 if (join || auto_join) {
5140 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5142 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5143 "connect a running group from " MACSTR,
5144 MAC2STR(peer_addr));
5145 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5148 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5149 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5151 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5152 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5156 os_get_reltime(&wpa_s->p2p_auto_started);
5157 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5159 wpa_s->p2p_auto_started.sec,
5160 wpa_s->p2p_auto_started.usec);
5162 wpa_s->user_initiated_pd = 1;
5163 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5164 auto_join, freq, NULL, 0) < 0)
5169 size = P2P_MAX_PREF_CHANNELS;
5170 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5171 go_intent == 15, pref_freq_list, &size);
5174 wpas_p2p_set_own_freq_preference(wpa_s,
5175 force_freq ? force_freq : pref_freq);
5177 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5179 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5181 if (wpa_s->create_p2p_iface) {
5182 /* Prepare to add a new interface for the group */
5183 iftype = WPA_IF_P2P_GROUP;
5184 if (go_intent == 15)
5185 iftype = WPA_IF_P2P_GO;
5186 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5187 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5188 "interface for the group");
5192 if_addr = wpa_s->pending_interface_addr;
5194 if_addr = wpa_s->own_addr;
5195 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5199 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5201 force_freq, persistent_group, ssid,
5207 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5208 go_intent, if_addr, force_freq,
5209 persistent_group, ssid, pref_freq) < 0) {
5210 if (wpa_s->create_p2p_iface)
5211 wpas_p2p_remove_pending_group_interface(wpa_s);
5219 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5220 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5221 * @freq: Frequency of the channel in MHz
5222 * @duration: Duration of the stay on the channel in milliseconds
5224 * This callback is called when the driver indicates that it has started the
5225 * requested remain-on-channel duration.
5227 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5228 unsigned int freq, unsigned int duration)
5230 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5232 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)",
5233 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5234 wpa_s->roc_waiting_drv_freq, freq, duration);
5235 if (wpa_s->off_channel_freq &&
5236 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5237 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5238 wpa_s->pending_listen_duration);
5239 wpa_s->pending_listen_freq = 0;
5241 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5242 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5248 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5250 /* Limit maximum Listen state time based on driver limitation. */
5251 if (timeout > wpa_s->max_remain_on_chan)
5252 timeout = wpa_s->max_remain_on_chan;
5254 return p2p_listen(wpa_s->global->p2p, timeout);
5259 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5260 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5261 * @freq: Frequency of the channel in MHz
5263 * This callback is called when the driver indicates that a remain-on-channel
5264 * operation has been completed, i.e., the duration on the requested channel
5267 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5270 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5271 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5272 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5273 wpas_p2p_listen_work_done(wpa_s);
5274 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5276 if (wpa_s->p2p_long_listen > 0)
5277 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5278 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5279 return; /* P2P module started a new operation */
5280 if (offchannel_pending_action_tx(wpa_s))
5282 if (wpa_s->p2p_long_listen > 0) {
5283 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5284 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5287 * When listen duration is over, stop listen & update p2p_state
5290 p2p_stop_listen(wpa_s->global->p2p);
5296 * wpas_p2p_group_remove - Remove a P2P group
5297 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5298 * @ifname: Network interface name of the group interface or "*" to remove all
5300 * Returns: 0 on success, -1 on failure
5302 * This function is used to remove a P2P group. This can be used to disconnect
5303 * from a group in which the local end is a P2P Client or to end a P2P Group in
5304 * case the local end is the Group Owner. If a virtual network interface was
5305 * created for this group, that interface will be removed. Otherwise, only the
5306 * configured P2P group network will be removed from the interface.
5308 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5310 struct wpa_global *global = wpa_s->global;
5311 struct wpa_supplicant *calling_wpa_s = wpa_s;
5313 if (os_strcmp(ifname, "*") == 0) {
5314 struct wpa_supplicant *prev;
5315 wpa_s = global->ifaces;
5318 wpa_s = wpa_s->next;
5319 if (prev->p2p_group_interface !=
5320 NOT_P2P_GROUP_INTERFACE ||
5321 (prev->current_ssid &&
5322 prev->current_ssid->p2p_group))
5323 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5328 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5329 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5333 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5337 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5341 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5342 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5343 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5346 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5347 &size, pref_freq_list);
5348 if (!res && size > 0) {
5350 while (wpas_p2p_disallowed_freq(wpa_s->global,
5351 pref_freq_list[i]) &&
5353 wpa_printf(MSG_DEBUG,
5354 "P2P: preferred_freq_list[%d]=%d is disallowed",
5355 i, pref_freq_list[i]);
5359 freq = pref_freq_list[i];
5360 wpa_printf(MSG_DEBUG,
5361 "P2P: Using preferred_freq_list[%d]=%d",
5364 wpa_printf(MSG_DEBUG,
5365 "P2P: All driver preferred frequencies are disallowed for P2P use");
5368 wpa_printf(MSG_DEBUG,
5369 "P2P: No preferred frequency list available");
5374 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5376 if (wpa_s->best_24_freq > 0 &&
5377 p2p_supported_freq_go(wpa_s->global->p2p,
5378 wpa_s->best_24_freq)) {
5379 freq = wpa_s->best_24_freq;
5380 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5381 "channel: %d MHz", freq);
5383 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5385 freq = 2412 + (r % 3) * 25;
5386 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5387 "channel: %d MHz", freq);
5392 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5394 if (wpa_s->best_5_freq > 0 &&
5395 p2p_supported_freq_go(wpa_s->global->p2p,
5396 wpa_s->best_5_freq)) {
5397 freq = wpa_s->best_5_freq;
5398 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5399 "channel: %d MHz", freq);
5401 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5403 freq = 5180 + (r % 4) * 20;
5404 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5405 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5406 "5 GHz channel for P2P group");
5409 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5410 "channel: %d MHz", freq);
5414 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5415 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5416 ieee80211_is_dfs(freq)) {
5418 * If freq is a DFS channel and DFS is offloaded to the
5419 * driver, allow P2P GO to use it.
5421 wpa_printf(MSG_DEBUG, "P2P: "
5422 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5426 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5427 "(%u MHz) is not supported for P2P uses",
5436 static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5437 const struct p2p_channels *channels,
5440 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5441 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5442 freq_included(wpa_s, channels, freq))
5448 static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5449 struct p2p_go_neg_results *params,
5450 const struct p2p_channels *channels)
5454 /* first try some random selection of the social channels */
5455 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5458 for (i = 0; i < 3; i++) {
5459 params->freq = 2412 + ((r + i) % 3) * 25;
5460 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5464 /* try all other channels in operating class 81 */
5465 for (i = 0; i < 11; i++) {
5466 params->freq = 2412 + i * 5;
5468 /* skip social channels; covered in the previous loop */
5469 if (params->freq == 2412 ||
5470 params->freq == 2437 ||
5471 params->freq == 2462)
5474 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5478 /* try all channels in operating class 115 */
5479 for (i = 0; i < 4; i++) {
5480 params->freq = 5180 + i * 20;
5481 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5482 freq_included(wpa_s, channels, params->freq) &&
5483 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5487 /* try all channels in operating class 124 */
5488 for (i = 0; i < 4; i++) {
5489 params->freq = 5745 + i * 20;
5490 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5491 freq_included(wpa_s, channels, params->freq) &&
5492 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5496 /* try social channel class 180 channel 2 */
5497 params->freq = 58320 + 1 * 2160;
5498 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5499 freq_included(wpa_s, channels, params->freq) &&
5500 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5503 /* try all channels in reg. class 180 */
5504 for (i = 0; i < 4; i++) {
5505 params->freq = 58320 + i * 2160;
5506 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5507 freq_included(wpa_s, channels, params->freq) &&
5508 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5513 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
5516 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5521 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5522 struct p2p_go_neg_results *params,
5523 int freq, int ht40, int vht,
5524 const struct p2p_channels *channels)
5526 struct wpa_used_freq_data *freqs;
5528 unsigned int num, i;
5530 os_memset(params, 0, sizeof(*params));
5531 params->role_go = 1;
5532 params->ht40 = ht40;
5535 if (wpa_s->p2p_group_common_freqs_num)
5536 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5539 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5540 sizeof(struct wpa_used_freq_data));
5544 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5545 wpa_s->num_multichan_concurrent);
5547 /* try using the forced freq */
5549 if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
5550 wpa_printf(MSG_DEBUG,
5551 "P2P: Forced GO freq %d MHz not accepted",
5556 for (i = 0; i < num; i++) {
5557 if (freqs[i].freq == freq) {
5558 wpa_printf(MSG_DEBUG,
5559 "P2P: forced freq (%d MHz) is also shared",
5561 params->freq = freq;
5566 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5567 wpa_printf(MSG_DEBUG,
5568 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5573 wpa_printf(MSG_DEBUG,
5574 "P2P: force GO freq (%d MHz) on a free channel",
5576 params->freq = freq;
5580 /* consider using one of the shared frequencies */
5582 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5583 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5584 wpa_printf(MSG_DEBUG,
5585 "P2P: Use shared freq (%d MHz) for GO",
5587 params->freq = cand;
5591 /* try using one of the shared freqs */
5592 for (i = 0; i < num; i++) {
5593 if (wpas_p2p_supported_freq_go(wpa_s, channels,
5595 wpa_printf(MSG_DEBUG,
5596 "P2P: Use shared freq (%d MHz) for GO",
5598 params->freq = freqs[i].freq;
5604 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5605 wpa_printf(MSG_DEBUG,
5606 "P2P: Cannot force GO on any of the channels we are already using");
5610 /* try using the setting from the configuration file */
5611 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5612 wpa_s->conf->p2p_oper_channel >= 1 &&
5613 wpa_s->conf->p2p_oper_channel <= 11 &&
5614 wpas_p2p_supported_freq_go(
5616 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5617 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5618 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5619 "frequency %d MHz", params->freq);
5623 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5624 wpa_s->conf->p2p_oper_reg_class == 116 ||
5625 wpa_s->conf->p2p_oper_reg_class == 117 ||
5626 wpa_s->conf->p2p_oper_reg_class == 124 ||
5627 wpa_s->conf->p2p_oper_reg_class == 125 ||
5628 wpa_s->conf->p2p_oper_reg_class == 126 ||
5629 wpa_s->conf->p2p_oper_reg_class == 127) &&
5630 wpas_p2p_supported_freq_go(wpa_s, channels,
5632 5 * wpa_s->conf->p2p_oper_channel)) {
5633 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5634 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5635 "frequency %d MHz", params->freq);
5639 /* Try using best channels */
5640 if (wpa_s->conf->p2p_oper_channel == 0 &&
5641 wpa_s->best_overall_freq > 0 &&
5642 wpas_p2p_supported_freq_go(wpa_s, channels,
5643 wpa_s->best_overall_freq)) {
5644 params->freq = wpa_s->best_overall_freq;
5645 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5646 "channel %d MHz", params->freq);
5650 if (wpa_s->conf->p2p_oper_channel == 0 &&
5651 wpa_s->best_24_freq > 0 &&
5652 wpas_p2p_supported_freq_go(wpa_s, channels,
5653 wpa_s->best_24_freq)) {
5654 params->freq = wpa_s->best_24_freq;
5655 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5656 "channel %d MHz", params->freq);
5660 if (wpa_s->conf->p2p_oper_channel == 0 &&
5661 wpa_s->best_5_freq > 0 &&
5662 wpas_p2p_supported_freq_go(wpa_s, channels,
5663 wpa_s->best_5_freq)) {
5664 params->freq = wpa_s->best_5_freq;
5665 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5666 "channel %d MHz", params->freq);
5670 /* try using preferred channels */
5671 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
5672 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5673 params->freq = cand;
5674 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5675 "channels", params->freq);
5679 /* Try using one of the group common freqs */
5680 if (wpa_s->p2p_group_common_freqs) {
5681 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
5682 cand = wpa_s->p2p_group_common_freqs[i];
5683 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5684 params->freq = cand;
5685 wpa_printf(MSG_DEBUG,
5686 "P2P: Use freq %d MHz common with the peer",
5693 /* no preference, select some channel */
5694 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
5696 if (params->freq == 0) {
5697 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
5710 static struct wpa_supplicant *
5711 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5714 struct wpa_supplicant *group_wpa_s;
5716 if (!wpas_p2p_create_iface(wpa_s)) {
5717 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5719 wpa_s->p2p_first_connection_timeout = 0;
5723 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5724 WPA_IF_P2P_CLIENT) < 0) {
5725 wpa_msg_global(wpa_s, MSG_ERROR,
5726 "P2P: Failed to add group interface");
5729 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5730 if (group_wpa_s == NULL) {
5731 wpa_msg_global(wpa_s, MSG_ERROR,
5732 "P2P: Failed to initialize group interface");
5733 wpas_p2p_remove_pending_group_interface(wpa_s);
5737 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5738 group_wpa_s->ifname);
5739 group_wpa_s->p2p_first_connection_timeout = 0;
5745 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5746 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5747 * @persistent_group: Whether to create a persistent group
5748 * @freq: Frequency for the group or 0 to indicate no hardcoding
5749 * @ht40: Start GO with 40 MHz channel width
5750 * @vht: Start GO with VHT support
5751 * Returns: 0 on success, -1 on failure
5753 * This function creates a new P2P group with the local end as the Group Owner,
5754 * i.e., without using Group Owner Negotiation.
5756 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5757 int freq, int ht40, int vht)
5759 struct p2p_go_neg_results params;
5761 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5764 os_free(wpa_s->global->add_psk);
5765 wpa_s->global->add_psk = NULL;
5767 /* Make sure we are not running find during connection establishment */
5768 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5769 wpas_p2p_stop_find_oper(wpa_s);
5771 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5775 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5778 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5779 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5780 ieee80211_is_dfs(params.freq)) {
5782 * If freq is a DFS channel and DFS is offloaded to the
5783 * driver, allow P2P GO to use it.
5785 wpa_printf(MSG_DEBUG,
5786 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5787 __func__, params.freq);
5789 wpa_printf(MSG_DEBUG,
5790 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5795 p2p_go_params(wpa_s->global->p2p, ¶ms);
5796 params.persistent_group = persistent_group;
5798 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5801 wpas_start_wps_go(wpa_s, ¶ms, 0);
5807 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5808 struct wpa_ssid *params, int addr_allocated,
5809 int freq, int force_scan)
5811 struct wpa_ssid *ssid;
5813 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5817 os_get_reltime(&wpa_s->scan_min_time);
5818 wpa_s->p2p_last_4way_hs_fail = NULL;
5820 wpa_supplicant_ap_deinit(wpa_s);
5822 ssid = wpa_config_add_network(wpa_s->conf);
5825 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5826 wpa_config_set_network_defaults(ssid);
5827 ssid->temporary = 1;
5828 ssid->proto = WPA_PROTO_RSN;
5829 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5830 ssid->group_cipher = WPA_CIPHER_CCMP;
5831 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5832 ssid->ssid = os_malloc(params->ssid_len);
5833 if (ssid->ssid == NULL) {
5834 wpa_config_remove_network(wpa_s->conf, ssid->id);
5837 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5838 ssid->ssid_len = params->ssid_len;
5839 ssid->p2p_group = 1;
5840 ssid->export_keys = 1;
5841 if (params->psk_set) {
5842 os_memcpy(ssid->psk, params->psk, 32);
5845 if (params->passphrase)
5846 ssid->passphrase = os_strdup(params->passphrase);
5848 wpa_s->show_group_started = 1;
5849 wpa_s->p2p_in_invitation = 1;
5850 wpa_s->p2p_invite_go_freq = freq;
5852 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5854 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5855 wpas_p2p_group_formation_timeout,
5856 wpa_s->parent, NULL);
5857 wpa_supplicant_select_network(wpa_s, ssid);
5863 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5864 struct wpa_ssid *ssid, int addr_allocated,
5865 int force_freq, int neg_freq, int ht40,
5866 int vht, const struct p2p_channels *channels,
5867 int connection_timeout, int force_scan)
5869 struct p2p_go_neg_results params;
5872 if (ssid->disabled != 2 || ssid->ssid == NULL)
5875 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5876 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5877 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5880 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5881 wpa_s->parent, NULL)) {
5883 * This can happen if Invitation Response frame was lost
5884 * and the peer (GO of a persistent group) tries to
5885 * invite us again. Reschedule the timeout to avoid
5886 * terminating the wait for the connection too early
5887 * since we now know that the peer is still trying to
5888 * invite us instead of having already started the GO.
5890 wpa_printf(MSG_DEBUG,
5891 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
5892 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5893 wpas_p2p_group_formation_timeout,
5894 wpa_s->parent, NULL);
5899 os_free(wpa_s->global->add_psk);
5900 wpa_s->global->add_psk = NULL;
5902 /* Make sure we are not running find during connection establishment */
5903 wpas_p2p_stop_find_oper(wpa_s);
5905 wpa_s->p2p_fallback_to_go_neg = 0;
5907 if (ssid->mode == WPAS_MODE_P2P_GO) {
5908 if (force_freq > 0) {
5909 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5913 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5915 (freq > 0 && !freq_included(wpa_s, channels, freq)))
5918 } else if (ssid->mode == WPAS_MODE_INFRA) {
5920 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
5921 struct os_reltime now;
5922 struct wpa_bss *bss =
5923 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
5925 os_get_reltime(&now);
5927 !os_reltime_expired(&now, &bss->last_update, 5) &&
5928 freq_included(wpa_s, channels, bss->freq))
5934 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
5940 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5944 params.psk_set = ssid->psk_set;
5946 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5947 if (ssid->passphrase) {
5948 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5949 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5950 "persistent group");
5953 os_strlcpy(params.passphrase, ssid->passphrase,
5954 sizeof(params.passphrase));
5956 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5957 params.ssid_len = ssid->ssid_len;
5958 params.persistent_group = 1;
5960 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5964 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5966 wpa_s->p2p_first_connection_timeout = connection_timeout;
5967 wpas_start_wps_go(wpa_s, ¶ms, 0);
5973 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5974 struct wpabuf *proberesp_ies)
5976 struct wpa_supplicant *wpa_s = ctx;
5977 if (wpa_s->ap_iface) {
5978 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5979 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5980 wpabuf_free(beacon_ies);
5981 wpabuf_free(proberesp_ies);
5985 wpabuf_free(hapd->p2p_beacon_ie);
5986 hapd->p2p_beacon_ie = beacon_ies;
5988 wpabuf_free(hapd->p2p_probe_resp_ie);
5989 hapd->p2p_probe_resp_ie = proberesp_ies;
5991 wpabuf_free(beacon_ies);
5992 wpabuf_free(proberesp_ies);
5994 wpa_supplicant_ap_update_beacon(wpa_s);
5998 static void wpas_p2p_idle_update(void *ctx, int idle)
6000 struct wpa_supplicant *wpa_s = ctx;
6001 if (!wpa_s->ap_iface)
6003 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
6005 if (wpa_s->global->p2p_fail_on_wps_complete &&
6006 wpa_s->p2p_in_provisioning) {
6007 wpas_p2p_grpform_fail_after_wps(wpa_s);
6010 wpas_p2p_set_group_idle_timeout(wpa_s);
6012 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6016 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6017 struct wpa_ssid *ssid)
6019 struct p2p_group *group;
6020 struct p2p_group_config *cfg;
6022 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6025 cfg = os_zalloc(sizeof(*cfg));
6029 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
6030 cfg->persistent_group = 2;
6031 else if (ssid->p2p_persistent_group)
6032 cfg->persistent_group = 1;
6033 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6034 if (wpa_s->max_stations &&
6035 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6036 cfg->max_clients = wpa_s->max_stations;
6038 cfg->max_clients = wpa_s->conf->max_num_sta;
6039 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6040 cfg->ssid_len = ssid->ssid_len;
6041 cfg->freq = ssid->frequency;
6042 cfg->cb_ctx = wpa_s;
6043 cfg->ie_update = wpas_p2p_ie_update;
6044 cfg->idle_update = wpas_p2p_idle_update;
6046 group = p2p_group_init(wpa_s->global->p2p, cfg);
6049 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
6050 p2p_group_notif_formation_done(group);
6051 wpa_s->p2p_group = group;
6056 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6059 struct wpa_ssid *ssid = wpa_s->current_ssid;
6061 if (!wpa_s->p2p_in_provisioning) {
6062 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6063 "provisioning not in progress");
6067 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6068 u8 go_dev_addr[ETH_ALEN];
6069 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6070 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6072 /* Clear any stored provisioning info */
6073 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6076 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6078 wpa_s->p2p_go_group_formation_completed = 1;
6079 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6081 * Use a separate timeout for initial data connection to
6082 * complete to allow the group to be removed automatically if
6083 * something goes wrong in this step before the P2P group idle
6084 * timeout mechanism is taken into use.
6086 wpa_dbg(wpa_s, MSG_DEBUG,
6087 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6088 P2P_MAX_INITIAL_CONN_WAIT);
6089 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6090 wpas_p2p_group_formation_timeout,
6091 wpa_s->parent, NULL);
6094 * Use a separate timeout for initial data connection to
6095 * complete to allow the group to be removed automatically if
6096 * the client does not complete data connection successfully.
6098 wpa_dbg(wpa_s, MSG_DEBUG,
6099 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6100 P2P_MAX_INITIAL_CONN_WAIT_GO);
6101 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6102 wpas_p2p_group_formation_timeout,
6103 wpa_s->parent, NULL);
6105 * Complete group formation on first successful data connection
6107 wpa_s->p2p_go_group_formation_completed = 0;
6109 if (wpa_s->global->p2p)
6110 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
6111 wpas_group_formation_completed(wpa_s, 1, 0);
6115 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6116 struct wps_event_fail *fail)
6118 if (!wpa_s->p2p_in_provisioning) {
6119 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6120 "provisioning not in progress");
6124 if (wpa_s->go_params) {
6125 p2p_clear_provisioning_info(
6127 wpa_s->go_params->peer_device_addr);
6130 wpas_notify_p2p_wps_failed(wpa_s, fail);
6132 if (wpa_s == wpa_s->global->p2p_group_formation) {
6134 * Allow some time for the failed WPS negotiation exchange to
6135 * complete, but remove the group since group formation cannot
6136 * succeed after provisioning failure.
6138 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6139 wpa_s->global->p2p_fail_on_wps_complete = 1;
6140 eloop_deplete_timeout(0, 50000,
6141 wpas_p2p_group_formation_timeout,
6142 wpa_s->parent, NULL);
6147 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6149 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6150 !wpa_s->p2p_in_provisioning)
6153 wpas_p2p_grpform_fail_after_wps(wpa_s);
6159 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6160 const char *config_method,
6161 enum wpas_p2p_prov_disc_use use,
6162 struct p2ps_provision *p2ps_prov)
6166 wpa_s->global->pending_p2ps_group = 0;
6167 wpa_s->p2p_fallback_to_go_neg = 0;
6168 wpa_s->pending_pd_use = NORMAL_PD;
6169 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6170 p2ps_prov->conncap = p2ps_group_capability(
6171 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
6172 wpa_printf(MSG_DEBUG,
6173 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6174 __func__, p2ps_prov->conncap,
6175 p2ps_prov->adv_id, p2ps_prov->conncap,
6176 p2ps_prov->status, p2ps_prov->info);
6179 } else if (os_strncmp(config_method, "display", 7) == 0)
6180 config_methods = WPS_CONFIG_DISPLAY;
6181 else if (os_strncmp(config_method, "keypad", 6) == 0)
6182 config_methods = WPS_CONFIG_KEYPAD;
6183 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6184 os_strncmp(config_method, "pushbutton", 10) == 0)
6185 config_methods = WPS_CONFIG_PUSHBUTTON;
6187 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
6192 if (use == WPAS_P2P_PD_AUTO) {
6193 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6194 wpa_s->pending_pd_config_methods = config_methods;
6195 wpa_s->p2p_auto_pd = 1;
6196 wpa_s->p2p_auto_join = 0;
6197 wpa_s->pending_pd_before_join = 0;
6198 wpa_s->auto_pd_scan_retry = 0;
6199 wpas_p2p_stop_find(wpa_s);
6200 wpa_s->p2p_join_scan_count = 0;
6201 os_get_reltime(&wpa_s->p2p_auto_started);
6202 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6203 wpa_s->p2p_auto_started.sec,
6204 wpa_s->p2p_auto_started.usec);
6205 wpas_p2p_join_scan(wpa_s, NULL);
6209 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6214 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6215 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6220 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6223 return p2p_scan_result_text(ies, ies_len, buf, end);
6227 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6229 if (!offchannel_pending_action_tx(wpa_s))
6232 wpas_p2p_action_tx_clear(wpa_s);
6234 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6235 "operation request");
6236 offchannel_clear_pending_action_tx(wpa_s);
6240 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6241 enum p2p_discovery_type type,
6242 unsigned int num_req_dev_types, const u8 *req_dev_types,
6243 const u8 *dev_id, unsigned int search_delay,
6244 u8 seek_cnt, const char **seek_string, int freq)
6246 wpas_p2p_clear_pending_action_tx(wpa_s);
6247 wpa_s->p2p_long_listen = 0;
6249 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6250 wpa_s->p2p_in_provisioning)
6253 wpa_supplicant_cancel_sched_scan(wpa_s);
6255 return p2p_find(wpa_s->global->p2p, timeout, type,
6256 num_req_dev_types, req_dev_types, dev_id,
6257 search_delay, seek_cnt, seek_string, freq);
6261 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6262 struct wpa_scan_results *scan_res)
6264 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6266 if (wpa_s->p2p_scan_work) {
6267 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6268 wpa_s->p2p_scan_work = NULL;
6269 radio_work_done(work);
6272 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6276 * Indicate that results have been processed so that the P2P module can
6277 * continue pending tasks.
6279 p2p_scan_res_handled(wpa_s->global->p2p);
6283 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
6285 wpas_p2p_clear_pending_action_tx(wpa_s);
6286 wpa_s->p2p_long_listen = 0;
6287 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6288 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6290 if (wpa_s->global->p2p)
6291 p2p_stop_find(wpa_s->global->p2p);
6293 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6294 wpa_printf(MSG_DEBUG,
6295 "P2P: Do not consider the scan results after stop_find");
6296 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6301 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6303 wpas_p2p_stop_find_oper(wpa_s);
6304 if (!wpa_s->global->pending_group_iface_for_p2ps)
6305 wpas_p2p_remove_pending_group_interface(wpa_s);
6309 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6311 struct wpa_supplicant *wpa_s = eloop_ctx;
6312 wpa_s->p2p_long_listen = 0;
6316 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6320 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6323 wpa_supplicant_cancel_sched_scan(wpa_s);
6324 wpas_p2p_clear_pending_action_tx(wpa_s);
6328 * This is a request for unlimited Listen state. However, at
6329 * least for now, this is mapped to a Listen state for one
6334 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6335 wpa_s->p2p_long_listen = 0;
6338 * Stop previous find/listen operation to avoid trying to request a new
6339 * remain-on-channel operation while the driver is still running the
6342 if (wpa_s->global->p2p)
6343 p2p_stop_find(wpa_s->global->p2p);
6345 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6346 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6347 wpa_s->p2p_long_listen = timeout * 1000;
6348 eloop_register_timeout(timeout, 0,
6349 wpas_p2p_long_listen_timeout,
6357 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6358 u8 *buf, size_t len, int p2p_group)
6360 struct wpabuf *p2p_ie;
6363 if (wpa_s->global->p2p_disabled)
6366 * Advertize mandatory cross connection capability even on
6367 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6369 if (wpa_s->conf->p2p_disabled && p2p_group)
6371 if (wpa_s->global->p2p == NULL)
6376 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6377 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6379 wpabuf_free(p2p_ie);
6385 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
6386 const u8 *dst, const u8 *bssid,
6387 const u8 *ie, size_t ie_len,
6388 unsigned int rx_freq, int ssi_signal)
6390 if (wpa_s->global->p2p_disabled)
6392 if (wpa_s->global->p2p == NULL)
6395 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6396 ie, ie_len, rx_freq)) {
6397 case P2P_PREQ_NOT_P2P:
6398 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6401 case P2P_PREQ_MALFORMED:
6402 case P2P_PREQ_NOT_LISTEN:
6403 case P2P_PREQ_NOT_PROCESSED:
6404 default: /* make gcc happy */
6406 case P2P_PREQ_PROCESSED:
6412 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6413 const u8 *sa, const u8 *bssid,
6414 u8 category, const u8 *data, size_t len, int freq)
6416 if (wpa_s->global->p2p_disabled)
6418 if (wpa_s->global->p2p == NULL)
6421 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6426 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6428 if (wpa_s->global->p2p_disabled)
6430 if (wpa_s->global->p2p == NULL)
6433 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6437 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6439 p2p_group_deinit(wpa_s->p2p_group);
6440 wpa_s->p2p_group = NULL;
6442 wpa_s->ap_configured_cb = NULL;
6443 wpa_s->ap_configured_cb_ctx = NULL;
6444 wpa_s->ap_configured_cb_data = NULL;
6445 wpa_s->connect_without_scan = NULL;
6449 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6451 wpa_s->p2p_long_listen = 0;
6453 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6456 return p2p_reject(wpa_s->global->p2p, addr);
6460 /* Invite to reinvoke a persistent group */
6461 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6462 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6463 int ht40, int vht, int pref_freq)
6465 enum p2p_invite_role role;
6469 int no_pref_freq_given = pref_freq == 0;
6470 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6472 wpa_s->global->p2p_invite_group = NULL;
6474 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6476 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6478 wpa_s->p2p_persistent_go_freq = freq;
6479 wpa_s->p2p_go_ht40 = !!ht40;
6480 if (ssid->mode == WPAS_MODE_P2P_GO) {
6481 role = P2P_INVITE_ROLE_GO;
6482 if (peer_addr == NULL) {
6483 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6484 "address in invitation command");
6487 if (wpas_p2p_create_iface(wpa_s)) {
6488 if (wpas_p2p_add_group_interface(wpa_s,
6489 WPA_IF_P2P_GO) < 0) {
6490 wpa_printf(MSG_ERROR, "P2P: Failed to "
6491 "allocate a new interface for the "
6495 bssid = wpa_s->pending_interface_addr;
6497 bssid = wpa_s->own_addr;
6499 role = P2P_INVITE_ROLE_CLIENT;
6500 peer_addr = ssid->bssid;
6502 wpa_s->pending_invite_ssid_id = ssid->id;
6504 size = P2P_MAX_PREF_CHANNELS;
6505 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6506 role == P2P_INVITE_ROLE_GO,
6507 pref_freq_list, &size);
6510 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6512 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6515 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6516 no_pref_freq_given && pref_freq > 0 &&
6517 wpa_s->num_multichan_concurrent > 1 &&
6518 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6519 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6525 * Stop any find/listen operations before invitation and possibly
6526 * connection establishment.
6528 wpas_p2p_stop_find_oper(wpa_s);
6530 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6531 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6536 /* Invite to join an active group */
6537 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6538 const u8 *peer_addr, const u8 *go_dev_addr)
6540 struct wpa_global *global = wpa_s->global;
6541 enum p2p_invite_role role;
6543 struct wpa_ssid *ssid;
6545 int freq = 0, force_freq = 0, pref_freq = 0;
6547 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6549 wpa_s->p2p_persistent_go_freq = 0;
6550 wpa_s->p2p_go_ht40 = 0;
6551 wpa_s->p2p_go_vht = 0;
6553 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6554 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6557 if (wpa_s == NULL) {
6558 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6562 ssid = wpa_s->current_ssid;
6564 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6569 wpa_s->global->p2p_invite_group = wpa_s;
6570 persistent = ssid->p2p_persistent_group &&
6571 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6572 ssid->ssid, ssid->ssid_len);
6574 if (ssid->mode == WPAS_MODE_P2P_GO) {
6575 role = P2P_INVITE_ROLE_ACTIVE_GO;
6576 bssid = wpa_s->own_addr;
6577 if (go_dev_addr == NULL)
6578 go_dev_addr = wpa_s->global->p2p_dev_addr;
6579 freq = ssid->frequency;
6581 role = P2P_INVITE_ROLE_CLIENT;
6582 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6583 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6584 "invite to current group");
6587 bssid = wpa_s->bssid;
6588 if (go_dev_addr == NULL &&
6589 !is_zero_ether_addr(wpa_s->go_dev_addr))
6590 go_dev_addr = wpa_s->go_dev_addr;
6591 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6592 (int) wpa_s->assoc_freq;
6594 wpa_s->parent->pending_invite_ssid_id = -1;
6596 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6599 size = P2P_MAX_PREF_CHANNELS;
6600 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6601 role == P2P_INVITE_ROLE_ACTIVE_GO,
6602 pref_freq_list, &size);
6605 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6607 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6608 ssid->ssid, ssid->ssid_len, force_freq,
6609 go_dev_addr, persistent, pref_freq, -1);
6613 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6615 struct wpa_ssid *ssid = wpa_s->current_ssid;
6616 u8 go_dev_addr[ETH_ALEN];
6617 int network_id = -1;
6623 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6624 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6625 wpa_s->parent, NULL);
6628 if (!wpa_s->show_group_started || !ssid)
6631 wpa_s->show_group_started = 0;
6633 os_memset(go_dev_addr, 0, ETH_ALEN);
6634 if (ssid->bssid_set)
6635 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6636 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6638 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6640 if (wpa_s->global->p2p_group_formation == wpa_s)
6641 wpa_s->global->p2p_group_formation = NULL;
6643 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6644 (int) wpa_s->assoc_freq;
6647 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6650 res = os_snprintf(ip_addr, sizeof(ip_addr),
6651 " ip_addr=%u.%u.%u.%u "
6652 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6653 ip[0], ip[1], ip[2], ip[3],
6654 ip[4], ip[5], ip[6], ip[7],
6655 ip[8], ip[9], ip[10], ip[11]);
6656 if (os_snprintf_error(sizeof(ip_addr), res))
6660 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6661 ssid->passphrase == NULL && ssid->psk_set ?
6663 ssid->passphrase, go_dev_addr, persistent,
6667 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6670 network_id = ssid->id;
6671 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6675 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6676 u32 interval1, u32 duration2, u32 interval2)
6680 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6683 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6684 wpa_s->current_ssid == NULL ||
6685 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6688 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6689 wpa_s->own_addr, wpa_s->assoc_freq,
6690 duration1, interval1, duration2, interval2);
6692 wpa_s->waiting_presence_resp = 1;
6698 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6699 unsigned int interval)
6701 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6704 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6708 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6710 if (wpa_s->current_ssid == NULL) {
6712 * current_ssid can be cleared when P2P client interface gets
6713 * disconnected, so assume this interface was used as P2P
6718 return wpa_s->current_ssid->p2p_group &&
6719 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6723 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6725 struct wpa_supplicant *wpa_s = eloop_ctx;
6727 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6728 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6733 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6735 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6739 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6743 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6744 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6746 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6749 timeout = wpa_s->conf->p2p_group_idle;
6750 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6751 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6752 timeout = P2P_MAX_CLIENT_IDLE;
6758 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6759 timeout = 0; /* special client mode no-timeout */
6764 if (wpa_s->p2p_in_provisioning) {
6766 * Use the normal group formation timeout during the
6767 * provisioning phase to avoid terminating this process too
6768 * early due to group idle timeout.
6770 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6771 "during provisioning");
6775 if (wpa_s->show_group_started) {
6777 * Use the normal group formation timeout between the end of
6778 * the provisioning phase and completion of 4-way handshake to
6779 * avoid terminating this process too early due to group idle
6782 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6783 "while waiting for initial 4-way handshake to "
6788 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6790 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6795 /* Returns 1 if the interface was removed */
6796 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6797 u16 reason_code, const u8 *ie, size_t ie_len,
6798 int locally_generated)
6800 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6803 if (!locally_generated)
6804 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6807 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6808 wpa_s->current_ssid &&
6809 wpa_s->current_ssid->p2p_group &&
6810 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6811 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6812 "session is ending");
6813 if (wpas_p2p_group_delete(wpa_s,
6814 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6823 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6824 u16 reason_code, const u8 *ie, size_t ie_len,
6825 int locally_generated)
6827 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6830 if (!locally_generated)
6831 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6836 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6838 struct p2p_data *p2p = wpa_s->global->p2p;
6843 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6846 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6847 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6849 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6850 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6853 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6854 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6856 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6857 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6859 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6860 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6861 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6862 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6863 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6866 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6867 p2p_set_sec_dev_types(p2p,
6868 (void *) wpa_s->conf->sec_device_type,
6869 wpa_s->conf->num_sec_device_types);
6871 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6873 p2p_remove_wps_vendor_extensions(p2p);
6874 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6875 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6877 p2p_add_wps_vendor_extension(
6878 p2p, wpa_s->conf->wps_vendor_ext[i]);
6882 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6883 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6885 country[0] = wpa_s->conf->country[0];
6886 country[1] = wpa_s->conf->country[1];
6888 p2p_set_country(p2p, country);
6891 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6892 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6893 wpa_s->conf->p2p_ssid_postfix ?
6894 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6898 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6899 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6901 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6902 u8 reg_class, channel;
6907 if (wpa_s->conf->p2p_listen_reg_class &&
6908 wpa_s->conf->p2p_listen_channel) {
6909 reg_class = wpa_s->conf->p2p_listen_reg_class;
6910 channel = wpa_s->conf->p2p_listen_channel;
6915 * Pick one of the social channels randomly as the
6918 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6921 channel = 1 + (r % 3) * 5;
6924 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6927 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6930 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6931 u8 op_reg_class, op_channel, cfg_op_channel;
6934 if (wpa_s->conf->p2p_oper_reg_class &&
6935 wpa_s->conf->p2p_oper_channel) {
6936 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6937 op_channel = wpa_s->conf->p2p_oper_channel;
6942 * Use random operation channel from (1, 6, 11)
6943 *if no other preference is indicated.
6945 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6948 op_channel = 1 + (r % 3) * 5;
6951 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6954 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6958 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6959 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6960 wpa_s->conf->p2p_pref_chan) < 0) {
6961 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6965 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6966 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6971 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6972 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6976 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6979 if (!wpa_s->ap_iface)
6981 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6986 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6988 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6991 wpa_s->global->cross_connection = enabled;
6992 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6995 struct wpa_supplicant *iface;
6997 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6999 if (iface->cross_connect_enabled == 0)
7002 iface->cross_connect_enabled = 0;
7003 iface->cross_connect_in_use = 0;
7004 wpa_msg_global(iface->parent, MSG_INFO,
7005 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7007 iface->cross_connect_uplink);
7015 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7017 struct wpa_supplicant *iface;
7019 if (!uplink->global->cross_connection)
7022 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7023 if (!iface->cross_connect_enabled)
7025 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7028 if (iface->ap_iface == NULL)
7030 if (iface->cross_connect_in_use)
7033 iface->cross_connect_in_use = 1;
7034 wpa_msg_global(iface->parent, MSG_INFO,
7035 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7036 iface->ifname, iface->cross_connect_uplink);
7041 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7043 struct wpa_supplicant *iface;
7045 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7046 if (!iface->cross_connect_enabled)
7048 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7051 if (!iface->cross_connect_in_use)
7054 wpa_msg_global(iface->parent, MSG_INFO,
7055 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7056 iface->ifname, iface->cross_connect_uplink);
7057 iface->cross_connect_in_use = 0;
7062 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7064 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7065 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7066 wpa_s->cross_connect_disallowed)
7067 wpas_p2p_disable_cross_connect(wpa_s);
7069 wpas_p2p_enable_cross_connect(wpa_s);
7070 if (!wpa_s->ap_iface &&
7071 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7072 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7076 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7078 wpas_p2p_disable_cross_connect(wpa_s);
7079 if (!wpa_s->ap_iface &&
7080 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7082 wpas_p2p_set_group_idle_timeout(wpa_s);
7086 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7088 struct wpa_supplicant *iface;
7090 if (!wpa_s->global->cross_connection)
7093 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7096 if (iface->drv_flags &
7097 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7099 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7100 iface != wpa_s->parent)
7103 wpa_s->cross_connect_enabled = 1;
7104 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7105 sizeof(wpa_s->cross_connect_uplink));
7106 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7107 "%s to %s whenever uplink is available",
7108 wpa_s->ifname, wpa_s->cross_connect_uplink);
7110 if (iface->ap_iface || iface->current_ssid == NULL ||
7111 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7112 iface->cross_connect_disallowed ||
7113 iface->wpa_state != WPA_COMPLETED)
7116 wpa_s->cross_connect_in_use = 1;
7117 wpa_msg_global(wpa_s->parent, MSG_INFO,
7118 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7119 wpa_s->ifname, wpa_s->cross_connect_uplink);
7125 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7127 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7128 !wpa_s->p2p_in_provisioning)
7129 return 0; /* not P2P client operation */
7131 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7133 if (wpa_s != wpa_s->parent)
7134 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
7135 wpas_p2p_group_formation_failed(wpa_s, 0);
7140 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7142 struct wpa_supplicant *wpa_s = eloop_ctx;
7143 wpas_p2p_notif_pbc_overlap(wpa_s);
7147 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7148 enum wpas_p2p_channel_update_trig trig)
7150 struct p2p_channels chan, cli_chan;
7151 struct wpa_used_freq_data *freqs = NULL;
7152 unsigned int num = wpa_s->num_multichan_concurrent;
7154 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7157 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7161 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7163 os_memset(&chan, 0, sizeof(chan));
7164 os_memset(&cli_chan, 0, sizeof(cli_chan));
7165 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
7166 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7171 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
7173 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7176 * The used frequencies map changed, so it is possible that a GO is
7177 * using a channel that is no longer valid for P2P use. It is also
7178 * possible that due to policy consideration, it would be preferable to
7179 * move it to a frequency already used by other station interfaces.
7181 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7187 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7188 struct wpa_scan_results *scan_res)
7190 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7194 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7196 struct wpa_global *global = wpa_s->global;
7200 if (global->p2p == NULL)
7203 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7205 if (wpa_s->pending_interface_name[0] &&
7206 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7209 peer = p2p_get_go_neg_peer(global->p2p);
7211 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7212 MACSTR, MAC2STR(peer));
7213 p2p_unauthorize(global->p2p, peer);
7217 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7218 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7219 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7223 if (wpa_s->pending_pd_before_join) {
7224 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7225 wpa_s->pending_pd_before_join = 0;
7229 wpas_p2p_stop_find(wpa_s);
7231 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7232 if (wpa_s == global->p2p_group_formation &&
7233 (wpa_s->p2p_in_provisioning ||
7234 wpa_s->parent->pending_interface_type ==
7235 WPA_IF_P2P_CLIENT)) {
7236 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7237 "formation found - cancelling",
7240 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7241 wpa_s->parent, NULL);
7242 if (wpa_s->p2p_in_provisioning) {
7243 wpas_group_formation_completed(wpa_s, 0, 0);
7246 wpas_p2p_group_delete(wpa_s,
7247 P2P_GROUP_REMOVAL_REQUESTED);
7249 } else if (wpa_s->p2p_in_invitation) {
7250 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7253 wpas_p2p_group_formation_failed(wpa_s, 0);
7258 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7266 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7268 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7271 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7272 "being available anymore");
7273 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
7277 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7278 int freq_24, int freq_5, int freq_overall)
7280 struct p2p_data *p2p = wpa_s->global->p2p;
7283 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7287 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7290 struct p2p_data *p2p = wpa_s->global->p2p;
7295 if (hwaddr_aton(addr, peer))
7298 return p2p_unauthorize(p2p, peer);
7303 * wpas_p2p_disconnect - Disconnect from a P2P Group
7304 * @wpa_s: Pointer to wpa_supplicant data
7305 * Returns: 0 on success, -1 on failure
7307 * This can be used to disconnect from a group in which the local end is a P2P
7308 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7309 * virtual network interface was created for this group, that interface will be
7310 * removed. Otherwise, only the configured P2P group network will be removed
7311 * from the interface.
7313 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7319 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7324 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7328 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7331 ret = p2p_in_progress(wpa_s->global->p2p);
7334 * Check whether there is an ongoing WPS provisioning step (or
7335 * other parts of group formation) on another interface since
7336 * p2p_in_progress() does not report this to avoid issues for
7337 * scans during such provisioning step.
7339 if (wpa_s->global->p2p_group_formation &&
7340 wpa_s->global->p2p_group_formation != wpa_s) {
7341 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7342 "in group formation",
7343 wpa_s->global->p2p_group_formation->ifname);
7348 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
7349 struct os_reltime now;
7350 os_get_reltime(&now);
7351 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7352 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
7353 /* Wait for the first client has expired */
7354 wpa_s->global->p2p_go_wait_client.sec = 0;
7356 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7365 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7366 struct wpa_ssid *ssid)
7368 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7369 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7370 wpa_s->parent, NULL) > 0) {
7372 * Remove the network by scheduling the group formation
7373 * timeout to happen immediately. The teardown code
7374 * needs to be scheduled to run asynch later so that we
7375 * don't delete data from under ourselves unexpectedly.
7376 * Calling wpas_p2p_group_formation_timeout directly
7377 * causes a series of crashes in WPS failure scenarios.
7379 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7380 "P2P group network getting removed");
7381 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7382 wpa_s->parent, NULL);
7387 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
7388 const u8 *addr, const u8 *ssid,
7394 for (s = wpa_s->conf->ssid; s; s = s->next) {
7395 if (s->disabled != 2)
7398 (ssid_len != s->ssid_len ||
7399 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7402 if (s->mode == WPAS_MODE_P2P_GO)
7406 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7407 return s; /* peer is GO in the persistent group */
7408 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7410 for (i = 0; i < s->num_p2p_clients; i++) {
7411 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
7412 addr, ETH_ALEN) == 0)
7413 return s; /* peer is P2P client in persistent
7422 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7425 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7426 wpa_s->parent, NULL) > 0) {
7428 * This can happen if WPS provisioning step is not terminated
7429 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7430 * peer was able to connect, there is no need to time out group
7431 * formation after this, though. In addition, this is used with
7432 * the initial connection wait on the GO as a separate formation
7433 * timeout and as such, expected to be hit after the initial WPS
7434 * provisioning step.
7436 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7438 if (!wpa_s->p2p_go_group_formation_completed &&
7439 !wpa_s->group_formation_reported) {
7441 * GO has not yet notified group formation success since
7442 * the WPS step was not completed cleanly. Do that
7443 * notification now since the P2P Client was able to
7444 * connect and as such, must have received the
7445 * credential from the WPS step.
7447 if (wpa_s->global->p2p)
7448 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7449 wpas_group_formation_completed(wpa_s, 1, 0);
7452 if (!wpa_s->p2p_go_group_formation_completed) {
7453 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7454 wpa_s->p2p_go_group_formation_completed = 1;
7455 wpa_s->global->p2p_group_formation = NULL;
7456 wpa_s->p2p_in_provisioning = 0;
7457 wpa_s->p2p_in_invitation = 0;
7459 wpa_s->global->p2p_go_wait_client.sec = 0;
7462 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7466 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7469 struct wpa_supplicant *group = wpa_s;
7472 if (wpa_s->global->p2p_group_formation)
7473 group = wpa_s->global->p2p_group_formation;
7474 wpa_s = wpa_s->global->p2p_init_wpa_s;
7475 offchannel_send_action_done(wpa_s);
7477 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7478 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7479 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7480 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7481 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7482 wpa_s->p2p_persistent_id,
7483 wpa_s->p2p_pd_before_go_neg,
7490 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7494 if (!wpa_s->p2p_fallback_to_go_neg ||
7495 wpa_s->p2p_in_provisioning <= 5)
7498 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7499 return 0; /* peer operating as a GO */
7501 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7502 "fallback to GO Negotiation");
7503 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7504 "reason=GO-not-found");
7505 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7507 return res == 1 ? 2 : 1;
7511 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7513 struct wpa_supplicant *ifs;
7515 if (wpa_s->wpa_state > WPA_SCANNING) {
7516 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7517 "concurrent operation",
7518 wpa_s->conf->p2p_search_delay);
7519 return wpa_s->conf->p2p_search_delay;
7522 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7524 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7525 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7526 "delay due to concurrent operation on "
7528 wpa_s->conf->p2p_search_delay,
7530 return wpa_s->conf->p2p_search_delay;
7538 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7539 struct wpa_ssid *s, const u8 *addr,
7542 struct psk_list_entry *psk, *tmp;
7545 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7547 if ((iface_addr && !psk->p2p &&
7548 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7549 (!iface_addr && psk->p2p &&
7550 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7551 wpa_dbg(wpa_s, MSG_DEBUG,
7552 "P2P: Remove persistent group PSK list entry for "
7554 MAC2STR(psk->addr), psk->p2p);
7555 dl_list_del(&psk->list);
7565 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7566 const u8 *p2p_dev_addr,
7567 const u8 *psk, size_t psk_len)
7569 struct wpa_ssid *ssid = wpa_s->current_ssid;
7570 struct wpa_ssid *persistent;
7571 struct psk_list_entry *p, *last;
7573 if (psk_len != sizeof(p->psk))
7577 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7578 " p2p_dev_addr=" MACSTR,
7579 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7580 if (is_zero_ether_addr(p2p_dev_addr))
7581 p2p_dev_addr = NULL;
7583 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7587 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7588 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7589 /* To be added to persistent group once created */
7590 if (wpa_s->global->add_psk == NULL) {
7591 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7592 if (wpa_s->global->add_psk == NULL)
7595 p = wpa_s->global->add_psk;
7598 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7601 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7603 os_memcpy(p->psk, psk, psk_len);
7607 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7608 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7612 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7615 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7619 p = os_zalloc(sizeof(*p));
7624 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7627 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7629 os_memcpy(p->psk, psk, psk_len);
7631 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7632 (last = dl_list_last(&persistent->psk_list,
7633 struct psk_list_entry, list))) {
7634 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7635 MACSTR " (p2p=%u) to make room for a new one",
7636 MAC2STR(last->addr), last->p2p);
7637 dl_list_del(&last->list);
7641 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7642 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7643 p2p_dev_addr == NULL);
7645 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7646 MACSTR, MAC2STR(p2p_dev_addr));
7648 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7651 dl_list_add(&persistent->psk_list, &p->list);
7653 if (wpa_s->parent->conf->update_config &&
7654 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7655 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7659 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7660 struct wpa_ssid *s, const u8 *addr,
7665 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7666 if (res > 0 && wpa_s->conf->update_config &&
7667 wpa_config_write(wpa_s->confname, wpa_s->conf))
7668 wpa_dbg(wpa_s, MSG_DEBUG,
7669 "P2P: Failed to update configuration");
7673 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7674 const u8 *peer, int iface_addr)
7676 struct hostapd_data *hapd;
7677 struct hostapd_wpa_psk *psk, *prev, *rem;
7678 struct sta_info *sta;
7680 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7681 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7684 /* Remove per-station PSK entry */
7685 hapd = wpa_s->ap_iface->bss[0];
7687 psk = hapd->conf->ssid.wpa_psk;
7689 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7691 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7692 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7693 MACSTR " iface_addr=%d",
7694 MAC2STR(peer), iface_addr);
7696 prev->next = psk->next;
7698 hapd->conf->ssid.wpa_psk = psk->next;
7708 /* Disconnect from group */
7710 sta = ap_get_sta(hapd, peer);
7712 sta = ap_get_sta_p2p(hapd, peer);
7714 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7715 " (iface_addr=%d) from group",
7716 MAC2STR(peer), iface_addr);
7717 hostapd_drv_sta_deauth(hapd, sta->addr,
7718 WLAN_REASON_DEAUTH_LEAVING);
7719 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7724 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7728 struct wpa_supplicant *w;
7729 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
7731 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7733 /* Remove from any persistent group */
7734 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
7735 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7738 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7739 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
7742 /* Remove from any operating group */
7743 for (w = wpa_s->global->ifaces; w; w = w->next)
7744 wpas_p2p_remove_client_go(w, peer, iface_addr);
7748 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7750 struct wpa_supplicant *wpa_s = eloop_ctx;
7751 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7755 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7757 struct wpa_supplicant *wpa_s = eloop_ctx;
7759 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7760 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7764 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7765 struct wpa_ssid *ssid)
7767 struct wpa_supplicant *iface;
7769 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7770 if (!iface->current_ssid ||
7771 iface->current_ssid->frequency == freq ||
7772 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7773 !iface->current_ssid->p2p_group))
7776 /* Remove the connection with least priority */
7777 if (!wpas_is_p2p_prioritized(iface)) {
7778 /* STA connection has priority over existing
7779 * P2P connection, so remove the interface. */
7780 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7781 eloop_register_timeout(0, 0,
7782 wpas_p2p_group_freq_conflict,
7784 /* If connection in progress is P2P connection, do not
7785 * proceed for the connection. */
7791 /* P2P connection has priority, disable the STA network
7793 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7795 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7796 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7797 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7799 /* If P2P connection is in progress, continue
7812 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7814 struct wpa_ssid *ssid = wpa_s->current_ssid;
7816 if (ssid == NULL || !ssid->p2p_group)
7819 if (wpa_s->p2p_last_4way_hs_fail &&
7820 wpa_s->p2p_last_4way_hs_fail == ssid) {
7821 u8 go_dev_addr[ETH_ALEN];
7822 struct wpa_ssid *persistent;
7824 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7826 ssid->ssid_len) <= 0) {
7827 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7831 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7832 MACSTR, MAC2STR(go_dev_addr));
7833 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7836 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7837 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7840 wpa_msg_global(wpa_s->parent, MSG_INFO,
7841 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7844 wpa_s->p2p_last_4way_hs_fail = NULL;
7846 * Remove the group from a timeout to avoid issues with caller
7847 * continuing to use the interface if this is on a P2P group
7850 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7855 wpa_s->p2p_last_4way_hs_fail = ssid;
7860 #ifdef CONFIG_WPS_NFC
7862 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7870 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7874 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7875 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7882 wpabuf_put_be16(ret, wsc_len);
7884 wpabuf_put_buf(ret, wsc);
7885 wpabuf_put_be16(ret, wpabuf_len(p2p));
7886 wpabuf_put_buf(ret, p2p);
7890 wpa_hexdump_buf(MSG_DEBUG,
7891 "P2P: Generated NFC connection handover message", ret);
7895 tmp = ndef_build_p2p(ret);
7898 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7908 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7909 struct wpa_ssid **ssid, u8 *go_dev_addr)
7911 struct wpa_supplicant *iface;
7914 os_memset(go_dev_addr, 0, ETH_ALEN);
7917 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7918 if (iface->wpa_state < WPA_ASSOCIATING ||
7919 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7920 !iface->current_ssid->p2p_group ||
7921 iface->current_ssid->mode != WPAS_MODE_INFRA)
7924 *ssid = iface->current_ssid;
7926 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7927 return iface->assoc_freq;
7933 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7936 struct wpabuf *wsc, *p2p;
7937 struct wpa_ssid *ssid;
7938 u8 go_dev_addr[ETH_ALEN];
7939 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7941 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7942 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7946 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7947 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7948 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7949 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7953 if (cli_freq == 0) {
7954 wsc = wps_build_nfc_handover_req_p2p(
7955 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7958 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7959 go_dev_addr, ssid ? ssid->ssid : NULL,
7960 ssid ? ssid->ssid_len : 0);
7962 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7966 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7969 struct wpabuf *wsc, *p2p;
7970 struct wpa_ssid *ssid;
7971 u8 go_dev_addr[ETH_ALEN];
7972 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7974 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7977 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7978 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7979 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7982 if (cli_freq == 0) {
7983 wsc = wps_build_nfc_handover_sel_p2p(
7985 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7986 DEV_PW_NFC_CONNECTION_HANDOVER,
7987 wpa_s->conf->wps_nfc_dh_pubkey,
7988 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7991 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7992 go_dev_addr, ssid ? ssid->ssid : NULL,
7993 ssid ? ssid->ssid_len : 0);
7995 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7999 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8000 struct p2p_nfc_params *params)
8002 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8003 "connection handover (freq=%d)",
8006 if (params->go_freq && params->go_ssid_len) {
8007 wpa_s->p2p_wps_method = WPS_NFC;
8008 wpa_s->pending_join_wps_method = WPS_NFC;
8009 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8010 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8012 return wpas_p2p_join_start(wpa_s, params->go_freq,
8014 params->go_ssid_len);
8017 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8018 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
8019 params->go_freq, -1, 0, 1, 1);
8023 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8024 struct p2p_nfc_params *params, int tag)
8026 int res, persistent;
8027 struct wpa_ssid *ssid;
8029 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8030 "connection handover");
8031 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8032 ssid = wpa_s->current_ssid;
8035 if (ssid->mode != WPAS_MODE_P2P_GO)
8037 if (wpa_s->ap_iface == NULL)
8041 if (wpa_s == NULL) {
8042 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8046 if (wpa_s->parent->p2p_oob_dev_pw_id !=
8047 DEV_PW_NFC_CONNECTION_HANDOVER &&
8048 !wpa_s->parent->p2p_oob_dev_pw) {
8049 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8052 res = wpas_ap_wps_add_nfc_pw(
8053 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
8054 wpa_s->parent->p2p_oob_dev_pw,
8055 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
8056 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
8061 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8065 if (!params->peer ||
8066 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8069 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8070 " to join", MAC2STR(params->peer->p2p_device_addr));
8072 wpa_s->global->p2p_invite_group = wpa_s;
8073 persistent = ssid->p2p_persistent_group &&
8074 wpas_p2p_get_persistent(wpa_s->parent,
8075 params->peer->p2p_device_addr,
8076 ssid->ssid, ssid->ssid_len);
8077 wpa_s->parent->pending_invite_ssid_id = -1;
8079 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8080 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8081 ssid->ssid, ssid->ssid_len, ssid->frequency,
8082 wpa_s->global->p2p_dev_addr, persistent, 0,
8083 wpa_s->parent->p2p_oob_dev_pw_id);
8087 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8088 struct p2p_nfc_params *params,
8091 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8092 "connection handover");
8093 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8094 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8095 forced_freq, -1, 0, 1, 1);
8099 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8100 struct p2p_nfc_params *params,
8105 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8106 "connection handover");
8107 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8108 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8109 forced_freq, -1, 0, 1, 1);
8113 res = wpas_p2p_listen(wpa_s, 60);
8115 p2p_unauthorize(wpa_s->global->p2p,
8116 params->peer->p2p_device_addr);
8123 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8124 const struct wpabuf *data,
8125 int sel, int tag, int forced_freq)
8127 const u8 *pos, *end;
8129 struct p2p_nfc_params params;
8132 os_memset(¶ms, 0, sizeof(params));
8135 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8137 pos = wpabuf_head(data);
8138 end = pos + wpabuf_len(data);
8140 if (end - pos < 2) {
8141 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8145 len = WPA_GET_BE16(pos);
8147 if (len > end - pos) {
8148 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8152 params.wsc_attr = pos;
8153 params.wsc_len = len;
8156 if (end - pos < 2) {
8157 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8161 len = WPA_GET_BE16(pos);
8163 if (len > end - pos) {
8164 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8168 params.p2p_attr = pos;
8169 params.p2p_len = len;
8172 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8173 params.wsc_attr, params.wsc_len);
8174 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8175 params.p2p_attr, params.p2p_len);
8177 wpa_hexdump(MSG_DEBUG,
8178 "P2P: Ignored extra data after P2P attributes",
8182 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
8186 if (params.next_step == NO_ACTION)
8189 if (params.next_step == BOTH_GO) {
8190 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8191 MAC2STR(params.peer->p2p_device_addr));
8195 if (params.next_step == PEER_CLIENT) {
8196 if (!is_zero_ether_addr(params.go_dev_addr)) {
8197 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8198 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8200 MAC2STR(params.peer->p2p_device_addr),
8202 MAC2STR(params.go_dev_addr),
8203 wpa_ssid_txt(params.go_ssid,
8204 params.go_ssid_len));
8206 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8207 "peer=" MACSTR " freq=%d",
8208 MAC2STR(params.peer->p2p_device_addr),
8214 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8215 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8216 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8220 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8221 wpa_s->p2p_oob_dev_pw = NULL;
8223 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8224 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8229 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8230 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8231 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8232 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8233 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8234 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8235 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8239 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8240 "peer OOB Device Password Id %u", id);
8243 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8244 "Device Password Id %u", id);
8245 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8246 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8247 params.oob_dev_pw_len -
8248 WPS_OOB_PUBKEY_HASH_LEN - 2);
8249 wpa_s->p2p_oob_dev_pw_id = id;
8250 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8251 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8252 params.oob_dev_pw_len -
8253 WPS_OOB_PUBKEY_HASH_LEN - 2);
8254 if (wpa_s->p2p_oob_dev_pw == NULL)
8257 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8258 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8259 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8262 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8263 "without Device Password");
8264 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8267 switch (params.next_step) {
8271 /* already covered above */
8274 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
8276 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
8278 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
8280 /* TODO: use own OOB Dev Pw */
8281 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
8288 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8289 const struct wpabuf *data, int forced_freq)
8291 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8294 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8298 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8299 const struct wpabuf *req,
8300 const struct wpabuf *sel, int forced_freq)
8305 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8308 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8310 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8311 wpabuf_head(req), wpabuf_len(req));
8312 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8313 wpabuf_head(sel), wpabuf_len(sel));
8315 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8316 tmp = ndef_parse_p2p(init ? sel : req);
8318 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8322 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8330 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8333 int go_intent = wpa_s->conf->p2p_go_intent;
8334 struct wpa_supplicant *iface;
8336 if (wpa_s->global->p2p == NULL)
8340 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8341 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8343 if (!iface->ap_iface)
8345 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8347 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8349 if (wpa_s->p2p_nfc_tag_enabled)
8350 wpas_p2p_remove_pending_group_interface(wpa_s);
8351 wpa_s->p2p_nfc_tag_enabled = 0;
8355 if (wpa_s->global->p2p_disabled)
8358 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8359 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8360 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8361 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8362 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8363 "to allow static handover cases");
8367 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8369 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8370 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8371 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8372 if (wpa_s->p2p_oob_dev_pw == NULL)
8374 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8376 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8377 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8379 * P2P Group Interface present and the command came on group
8380 * interface, so enable the token for the current interface.
8382 wpa_s->create_p2p_iface = 0;
8384 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8387 if (wpa_s->create_p2p_iface) {
8388 enum wpa_driver_if_type iftype;
8389 /* Prepare to add a new interface for the group */
8390 iftype = WPA_IF_P2P_GROUP;
8391 if (go_intent == 15)
8392 iftype = WPA_IF_P2P_GO;
8393 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8394 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8395 "interface for the group");
8399 if_addr = wpa_s->pending_interface_addr;
8401 if_addr = wpa_s->own_addr;
8403 wpa_s->p2p_nfc_tag_enabled = enabled;
8405 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8406 struct hostapd_data *hapd;
8407 if (iface->ap_iface == NULL)
8409 hapd = iface->ap_iface->bss[0];
8410 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8411 hapd->conf->wps_nfc_dh_pubkey =
8412 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8413 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8414 hapd->conf->wps_nfc_dh_privkey =
8415 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8416 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8417 hapd->conf->wps_nfc_dev_pw =
8418 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8419 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8421 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8422 wpa_dbg(iface, MSG_DEBUG,
8423 "P2P: Failed to enable NFC Tag for GO");
8426 p2p_set_authorized_oob_dev_pw_id(
8427 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8433 #endif /* CONFIG_WPS_NFC */
8436 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8437 struct wpa_used_freq_data *freqs,
8440 u8 curr_chan, cand, chan;
8444 * If possible, optimize the Listen channel to be a channel that is
8445 * already used by one of the other interfaces.
8447 if (!wpa_s->conf->p2p_optimize_listen_chan)
8450 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8453 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8454 for (i = 0, cand = 0; i < num; i++) {
8455 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8456 if (curr_chan == chan) {
8461 if (chan == 1 || chan == 6 || chan == 11)
8466 wpa_dbg(wpa_s, MSG_DEBUG,
8467 "P2P: Update Listen channel to %u based on operating channel",
8469 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8474 static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
8476 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8477 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8481 /* TODO: Add CSA support */
8482 wpa_dbg(wpa_s, MSG_DEBUG, "Moving GO with CSA is not implemented");
8487 static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
8489 struct p2p_go_neg_results params;
8490 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8492 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
8494 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
8495 current_ssid->frequency);
8497 /* Stop the AP functionality */
8498 /* TODO: Should do this in a way that does not indicated to possible
8499 * P2P Clients in the group that the group is terminated. */
8500 wpa_supplicant_ap_deinit(wpa_s);
8502 /* Reselect the GO frequency */
8503 if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, NULL)) {
8504 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
8505 wpas_p2p_group_delete(wpa_s,
8506 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8509 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
8513 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
8514 wpa_printf(MSG_DEBUG,
8515 "P2P: Selected freq (%u MHz) is not valid for P2P",
8517 wpas_p2p_group_delete(wpa_s,
8518 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8522 /* Update the frequency */
8523 current_ssid->frequency = params.freq;
8524 wpa_s->connect_without_scan = current_ssid;
8525 wpa_s->reassociate = 1;
8526 wpa_s->disconnected = 0;
8527 wpa_supplicant_req_scan(wpa_s, 0, 0);
8531 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
8533 struct wpa_supplicant *wpa_s = eloop_ctx;
8535 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
8538 wpas_p2p_go_update_common_freqs(wpa_s);
8541 * First, try a channel switch flow. If it is not supported or fails,
8542 * take down the GO and bring it up again.
8544 if (wpas_p2p_move_go_csa(wpa_s) < 0)
8545 wpas_p2p_move_go_no_csa(wpa_s);
8549 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
8551 struct wpa_supplicant *wpa_s = eloop_ctx;
8552 struct wpa_used_freq_data *freqs = NULL;
8553 unsigned int num = wpa_s->num_multichan_concurrent;
8555 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8559 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8561 /* Previous attempt to move a GO was not possible -- try again. */
8562 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
8563 WPAS_P2P_CHANNEL_UPDATE_ANY);
8570 * Consider moving a GO from its currently used frequency:
8571 * 1. It is possible that due to regulatory consideration the frequency
8572 * can no longer be used and there is a need to evacuate the GO.
8573 * 2. It is possible that due to MCC considerations, it would be preferable
8574 * to move the GO to a channel that is currently used by some other
8575 * station interface.
8577 * In case a frequency that became invalid is once again valid, cancel a
8578 * previously initiated GO frequency change.
8580 static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
8581 struct wpa_used_freq_data *freqs,
8584 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
8585 unsigned int timeout;
8588 wpas_p2p_go_update_common_freqs(wpa_s);
8590 freq = wpa_s->current_ssid->frequency;
8591 for (i = 0, invalid_freq = 0; i < num; i++) {
8592 if (freqs[i].freq == freq) {
8593 flags = freqs[i].flags;
8595 /* The channel is invalid, must change it */
8596 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
8597 wpa_dbg(wpa_s, MSG_DEBUG,
8598 "P2P: Freq=%d MHz no longer valid for GO",
8602 } else if (freqs[i].flags == 0) {
8603 /* Freq is not used by any other station interface */
8605 } else if (!p2p_supported_freq(wpa_s->global->p2p,
8607 /* Freq is not valid for P2P use cases */
8609 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8610 P2P_GO_FREQ_MOVE_SCM) {
8612 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8613 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
8614 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
8619 wpa_dbg(wpa_s, MSG_DEBUG,
8620 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
8621 invalid_freq, policy_move, flags);
8624 * The channel is valid, or we are going to have a policy move, so
8627 if (!invalid_freq || policy_move) {
8628 wpa_dbg(wpa_s, MSG_DEBUG,
8629 "P2P: Cancel a GO move from freq=%d MHz", freq);
8630 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8632 if (wpas_p2p_in_progress(wpa_s)) {
8633 wpa_dbg(wpa_s, MSG_DEBUG,
8634 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
8635 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
8637 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
8638 wpas_p2p_reconsider_moving_go,
8644 if (!invalid_freq && (!policy_move || flags != 0)) {
8645 wpa_dbg(wpa_s, MSG_DEBUG,
8646 "P2P: Not initiating a GO frequency change");
8650 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
8651 timeout = P2P_GO_FREQ_CHANGE_TIME;
8655 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
8657 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8658 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
8662 static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
8663 struct wpa_used_freq_data *freqs,
8665 enum wpas_p2p_channel_update_trig trig)
8667 struct wpa_supplicant *ifs;
8669 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
8673 * Travers all the radio interfaces, and for each GO interface, check
8674 * if there is a need to move the GO from the frequency it is using,
8675 * or in case the frequency is valid again, cancel the evacuation flow.
8677 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8679 if (ifs->current_ssid == NULL ||
8680 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
8684 * The GO was just started or completed channel switch, no need
8688 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
8689 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
8690 wpa_dbg(wpa_s, MSG_DEBUG,
8691 "P2P: GO move - schedule re-consideration");
8692 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
8693 wpas_p2p_reconsider_moving_go,
8698 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
8703 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8705 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8708 wpas_p2p_update_channel_list(wpa_s,
8709 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
8713 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8715 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8716 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8717 "the management interface is being removed");
8718 wpas_p2p_deinit_global(wpa_s->global);
8723 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8725 if (wpa_s->ap_iface->bss)
8726 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8727 wpas_p2p_group_deinit(wpa_s);