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 if (already_deleted)
1291 wpas_p2p_group_delete(wpa_s,
1292 P2P_GROUP_REMOVAL_FORMATION_FAILED);
1296 wpa_msg_global(wpa_s->parent, MSG_INFO,
1297 P2P_EVENT_GROUP_FORMATION_SUCCESS);
1299 ssid = wpa_s->current_ssid;
1300 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1301 ssid->mode = WPAS_MODE_P2P_GO;
1302 p2p_group_notif_formation_done(wpa_s->p2p_group);
1303 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1308 client = ssid->mode == WPAS_MODE_INFRA;
1309 if (ssid->mode == WPAS_MODE_P2P_GO) {
1310 persistent = ssid->p2p_persistent_group;
1311 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1314 persistent = wpas_p2p_persistent_group(wpa_s,
1319 client = wpa_s->p2p_group_interface ==
1320 P2P_GROUP_INTERFACE_CLIENT;
1321 os_memset(go_dev_addr, 0, ETH_ALEN);
1324 wpa_s->show_group_started = 0;
1327 * Indicate event only after successfully completed 4-way
1328 * handshake, i.e., when the interface is ready for data
1331 wpa_s->show_group_started = 1;
1333 wpas_p2p_group_started(wpa_s, 1, ssid,
1334 ssid ? ssid->frequency : 0,
1335 ssid && ssid->passphrase == NULL &&
1336 ssid->psk_set ? ssid->psk : NULL,
1337 ssid ? ssid->passphrase : NULL,
1338 go_dev_addr, persistent, "");
1339 wpas_p2p_cross_connect_setup(wpa_s);
1340 wpas_p2p_set_group_idle_timeout(wpa_s);
1344 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1347 os_free(wpa_s->global->add_psk);
1348 wpa_s->global->add_psk = NULL;
1350 if (network_id < 0 && ssid)
1351 network_id = ssid->id;
1353 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1354 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1359 struct send_action_work {
1365 unsigned int wait_time;
1370 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1373 struct wpa_supplicant *wpa_s = eloop_ctx;
1375 if (!wpa_s->p2p_send_action_work)
1378 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1379 os_free(wpa_s->p2p_send_action_work->ctx);
1380 radio_work_done(wpa_s->p2p_send_action_work);
1381 wpa_s->p2p_send_action_work = NULL;
1385 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1387 if (wpa_s->p2p_send_action_work) {
1388 struct send_action_work *awork;
1389 awork = wpa_s->p2p_send_action_work->ctx;
1390 if (awork->wait_time == 0) {
1392 radio_work_done(wpa_s->p2p_send_action_work);
1393 wpa_s->p2p_send_action_work = NULL;
1396 * In theory, this should not be needed, but number of
1397 * places in the P2P code is still using non-zero wait
1398 * time for the last Action frame in the sequence and
1399 * some of these do not call send_action_done().
1401 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1403 eloop_register_timeout(
1404 0, awork->wait_time * 1000,
1405 wpas_p2p_send_action_work_timeout,
1412 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1414 const u8 *dst, const u8 *src,
1416 const u8 *data, size_t data_len,
1417 enum offchannel_send_action_result
1420 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1422 wpas_p2p_action_tx_clear(wpa_s);
1424 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1428 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1429 res = P2P_SEND_ACTION_SUCCESS;
1431 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1432 res = P2P_SEND_ACTION_NO_ACK;
1434 case OFFCHANNEL_SEND_ACTION_FAILED:
1435 res = P2P_SEND_ACTION_FAILED;
1439 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1441 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1442 wpa_s->pending_pd_before_join &&
1443 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1444 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1445 wpa_s->p2p_fallback_to_go_neg) {
1446 wpa_s->pending_pd_before_join = 0;
1447 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1448 "during p2p_connect-auto");
1449 wpa_msg_global(wpa_s->parent, MSG_INFO,
1450 P2P_EVENT_FALLBACK_TO_GO_NEG
1451 "reason=no-ACK-to-PD-Req");
1452 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1458 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1460 struct wpa_supplicant *wpa_s = work->wpa_s;
1461 struct send_action_work *awork = work->ctx;
1464 if (work->started) {
1465 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1467 wpa_s->p2p_send_action_work = NULL;
1468 offchannel_send_action_done(wpa_s);
1474 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1475 awork->bssid, awork->buf, awork->len,
1477 wpas_p2p_send_action_tx_status, 1) < 0) {
1479 radio_work_done(work);
1482 wpa_s->p2p_send_action_work = work;
1486 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1487 unsigned int freq, const u8 *dst,
1488 const u8 *src, const u8 *bssid, const u8 *buf,
1489 size_t len, unsigned int wait_time)
1491 struct send_action_work *awork;
1493 if (wpa_s->p2p_send_action_work) {
1494 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1498 awork = os_zalloc(sizeof(*awork) + len);
1503 os_memcpy(awork->dst, dst, ETH_ALEN);
1504 os_memcpy(awork->src, src, ETH_ALEN);
1505 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1507 awork->wait_time = wait_time;
1508 os_memcpy(awork->buf, buf, len);
1510 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1511 wpas_send_action_cb, awork) < 0) {
1520 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1521 const u8 *src, const u8 *bssid, const u8 *buf,
1522 size_t len, unsigned int wait_time)
1524 struct wpa_supplicant *wpa_s = ctx;
1525 int listen_freq = -1, send_freq = -1;
1527 if (wpa_s->p2p_listen_work)
1528 listen_freq = wpa_s->p2p_listen_work->freq;
1529 if (wpa_s->p2p_send_action_work)
1530 send_freq = wpa_s->p2p_send_action_work->freq;
1531 if (listen_freq != (int) freq && send_freq != (int) freq) {
1532 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1533 listen_freq, send_freq);
1534 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1538 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1539 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1541 wpas_p2p_send_action_tx_status, 1);
1545 static void wpas_send_action_done(void *ctx)
1547 struct wpa_supplicant *wpa_s = ctx;
1549 if (wpa_s->p2p_send_action_work) {
1550 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1552 os_free(wpa_s->p2p_send_action_work->ctx);
1553 radio_work_done(wpa_s->p2p_send_action_work);
1554 wpa_s->p2p_send_action_work = NULL;
1557 offchannel_send_action_done(wpa_s);
1561 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1562 struct p2p_go_neg_results *params)
1564 if (wpa_s->go_params == NULL) {
1565 wpa_s->go_params = os_malloc(sizeof(*params));
1566 if (wpa_s->go_params == NULL)
1569 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1574 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1575 struct p2p_go_neg_results *res)
1577 wpa_s->group_formation_reported = 0;
1578 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1579 " dev_addr " MACSTR " wps_method %d",
1580 MAC2STR(res->peer_interface_addr),
1581 MAC2STR(res->peer_device_addr), res->wps_method);
1582 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1583 res->ssid, res->ssid_len);
1584 wpa_supplicant_ap_deinit(wpa_s);
1585 wpas_copy_go_neg_results(wpa_s, res);
1586 if (res->wps_method == WPS_PBC) {
1587 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1588 #ifdef CONFIG_WPS_NFC
1589 } else if (res->wps_method == WPS_NFC) {
1590 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1591 res->peer_interface_addr,
1592 wpa_s->parent->p2p_oob_dev_pw,
1593 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1594 wpa_s->parent->p2p_oob_dev_pw_id ==
1595 DEV_PW_NFC_CONNECTION_HANDOVER ?
1596 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1599 #endif /* CONFIG_WPS_NFC */
1601 u16 dev_pw_id = DEV_PW_DEFAULT;
1602 if (wpa_s->p2p_wps_method == WPS_P2PS)
1603 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1604 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1605 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1606 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1607 wpa_s->p2p_pin, 1, dev_pw_id);
1612 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1613 struct wpa_ssid *ssid)
1615 struct wpa_ssid *persistent;
1616 struct psk_list_entry *psk;
1617 struct hostapd_data *hapd;
1619 if (!wpa_s->ap_iface)
1622 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1624 if (persistent == NULL)
1627 hapd = wpa_s->ap_iface->bss[0];
1629 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1631 struct hostapd_wpa_psk *hpsk;
1633 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1635 MAC2STR(psk->addr), psk->p2p);
1636 hpsk = os_zalloc(sizeof(*hpsk));
1639 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1641 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1643 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1644 hpsk->next = hapd->conf->ssid.wpa_psk;
1645 hapd->conf->ssid.wpa_psk = hpsk;
1650 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1654 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1655 wpa_s->p2p_group_common_freqs_num);
1657 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1658 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1659 i, wpa_s->p2p_group_common_freqs[i]);
1663 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1664 struct p2p_go_neg_results *params)
1666 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1668 wpa_s->p2p_group_common_freqs_num = 0;
1669 os_free(wpa_s->p2p_group_common_freqs);
1670 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1671 if (!wpa_s->p2p_group_common_freqs)
1674 for (i = 0; i < len; i++) {
1675 if (!wpa_s->go_params->freq_list[i])
1677 wpa_s->p2p_group_common_freqs[i] =
1678 wpa_s->go_params->freq_list[i];
1680 wpa_s->p2p_group_common_freqs_num = i;
1684 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1686 #ifndef CONFIG_NO_CONFIG_WRITE
1687 if (wpa_s->parent->conf->update_config &&
1688 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1689 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1690 #endif /* CONFIG_NO_CONFIG_WRITE */
1694 static void p2p_go_configured(void *ctx, void *data)
1696 struct wpa_supplicant *wpa_s = ctx;
1697 struct p2p_go_neg_results *params = data;
1698 struct wpa_ssid *ssid;
1699 int network_id = -1;
1701 p2p_go_save_group_common_freqs(wpa_s, params);
1702 p2p_go_dump_common_freqs(wpa_s);
1704 ssid = wpa_s->current_ssid;
1705 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1706 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1707 if (wpa_s->global->p2p_group_formation == wpa_s)
1708 wpa_s->global->p2p_group_formation = NULL;
1709 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1710 params->passphrase[0] == '\0' ?
1713 wpa_s->global->p2p_dev_addr,
1714 params->persistent_group, "");
1715 wpa_s->group_formation_reported = 1;
1717 if (wpa_s->parent->p2ps_method_config_any) {
1718 if (is_zero_ether_addr(wpa_s->parent->p2ps_join_addr)) {
1719 wpa_dbg(wpa_s, MSG_DEBUG,
1720 "P2PS: Setting default PIN for ANY");
1721 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1722 "12345670", NULL, 0,
1725 wpa_dbg(wpa_s, MSG_DEBUG,
1726 "P2PS: Setting default PIN for " MACSTR,
1727 MAC2STR(wpa_s->parent->p2ps_join_addr));
1728 wpa_supplicant_ap_wps_pin(
1729 wpa_s, wpa_s->parent->p2ps_join_addr,
1730 "12345670", NULL, 0, 0);
1732 wpa_s->parent->p2ps_method_config_any = 0;
1735 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1736 if (params->persistent_group) {
1737 network_id = wpas_p2p_store_persistent_group(
1738 wpa_s->parent, ssid,
1739 wpa_s->global->p2p_dev_addr);
1740 wpas_p2p_add_psk_list(wpa_s, ssid);
1743 network_id = ssid->id;
1744 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1745 wpas_p2p_cross_connect_setup(wpa_s);
1746 wpas_p2p_set_group_idle_timeout(wpa_s);
1748 if (wpa_s->p2p_first_connection_timeout) {
1749 wpa_dbg(wpa_s, MSG_DEBUG,
1750 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1751 wpa_s->p2p_first_connection_timeout);
1752 wpa_s->p2p_go_group_formation_completed = 0;
1753 wpa_s->global->p2p_group_formation = wpa_s;
1754 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1755 wpa_s->parent, NULL);
1756 eloop_register_timeout(
1757 wpa_s->p2p_first_connection_timeout, 0,
1758 wpas_p2p_group_formation_timeout,
1759 wpa_s->parent, NULL);
1765 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1766 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1767 params->peer_interface_addr)) {
1768 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1772 if (params->wps_method == WPS_PBC) {
1773 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1774 params->peer_device_addr);
1775 #ifdef CONFIG_WPS_NFC
1776 } else if (params->wps_method == WPS_NFC) {
1777 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1778 DEV_PW_NFC_CONNECTION_HANDOVER &&
1779 !wpa_s->parent->p2p_oob_dev_pw) {
1780 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1783 wpas_ap_wps_add_nfc_pw(
1784 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1785 wpa_s->parent->p2p_oob_dev_pw,
1786 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1787 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1788 #endif /* CONFIG_WPS_NFC */
1789 } else if (wpa_s->p2p_pin[0])
1790 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1791 wpa_s->p2p_pin, NULL, 0, 0);
1792 os_free(wpa_s->go_params);
1793 wpa_s->go_params = NULL;
1797 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1798 struct p2p_go_neg_results *params,
1799 int group_formation)
1801 struct wpa_ssid *ssid;
1803 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1804 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1805 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1810 ssid = wpa_config_add_network(wpa_s->conf);
1812 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1816 wpa_s->show_group_started = 0;
1817 wpa_s->p2p_go_group_formation_completed = 0;
1818 wpa_s->group_formation_reported = 0;
1819 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1821 wpa_config_set_network_defaults(ssid);
1822 ssid->temporary = 1;
1823 ssid->p2p_group = 1;
1824 ssid->p2p_persistent_group = params->persistent_group;
1825 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1827 ssid->frequency = params->freq;
1828 ssid->ht40 = params->ht40;
1829 ssid->vht = params->vht;
1830 ssid->ssid = os_zalloc(params->ssid_len + 1);
1832 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1833 ssid->ssid_len = params->ssid_len;
1835 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1836 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1837 ssid->proto = WPA_PROTO_RSN;
1838 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1839 ssid->group_cipher = WPA_CIPHER_CCMP;
1840 if (params->freq > 56160) {
1842 * Enable GCMP instead of CCMP as pairwise_cipher and
1843 * group_cipher in 60 GHz.
1845 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1846 ssid->group_cipher = WPA_CIPHER_GCMP;
1848 if (os_strlen(params->passphrase) > 0) {
1849 ssid->passphrase = os_strdup(params->passphrase);
1850 if (ssid->passphrase == NULL) {
1851 wpa_msg_global(wpa_s, MSG_ERROR,
1852 "P2P: Failed to copy passphrase for GO");
1853 wpa_config_remove_network(wpa_s->conf, ssid->id);
1857 ssid->passphrase = NULL;
1858 ssid->psk_set = params->psk_set;
1860 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1861 else if (ssid->passphrase)
1862 wpa_config_update_psk(ssid);
1863 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1865 wpa_s->ap_configured_cb = p2p_go_configured;
1866 wpa_s->ap_configured_cb_ctx = wpa_s;
1867 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1868 wpa_s->scan_req = NORMAL_SCAN_REQ;
1869 wpa_s->connect_without_scan = ssid;
1870 wpa_s->reassociate = 1;
1871 wpa_s->disconnected = 0;
1872 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1874 wpa_supplicant_req_scan(wpa_s, 0, 0);
1878 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1879 const struct wpa_supplicant *src)
1881 struct wpa_config *d;
1882 const struct wpa_config *s;
1887 #define C(n) if (s->n) d->n = os_strdup(s->n)
1896 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1897 os_memcpy(d->sec_device_type, s->sec_device_type,
1898 sizeof(d->sec_device_type));
1899 d->num_sec_device_types = s->num_sec_device_types;
1901 d->p2p_group_idle = s->p2p_group_idle;
1902 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
1903 d->p2p_intra_bss = s->p2p_intra_bss;
1904 d->persistent_reconnect = s->persistent_reconnect;
1905 d->max_num_sta = s->max_num_sta;
1906 d->pbc_in_m1 = s->pbc_in_m1;
1907 d->ignore_old_scan_res = s->ignore_old_scan_res;
1908 d->beacon_int = s->beacon_int;
1909 d->dtim_period = s->dtim_period;
1910 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
1911 d->disassoc_low_ack = s->disassoc_low_ack;
1912 d->disable_scan_offload = s->disable_scan_offload;
1913 d->passive_scan = s->passive_scan;
1915 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1916 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1917 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1919 d->p2p_cli_probe = s->p2p_cli_probe;
1923 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1924 char *ifname, size_t len)
1926 char *ifname_ptr = wpa_s->ifname;
1928 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1929 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1930 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1933 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1934 if (os_strlen(ifname) >= IFNAMSIZ &&
1935 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1938 /* Try to avoid going over the IFNAMSIZ length limit */
1939 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1940 if (os_snprintf_error(len, res) && len)
1941 ifname[len - 1] = '\0';
1946 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1947 enum wpa_driver_if_type type)
1949 char ifname[120], force_ifname[120];
1951 if (wpa_s->pending_interface_name[0]) {
1952 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1953 "- skip creation of a new one");
1954 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1955 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1956 "unknown?! ifname='%s'",
1957 wpa_s->pending_interface_name);
1963 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1964 force_ifname[0] = '\0';
1966 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1968 wpa_s->p2p_group_idx++;
1970 wpa_s->pending_interface_type = type;
1971 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1972 wpa_s->pending_interface_addr, NULL) < 0) {
1973 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1978 if (force_ifname[0]) {
1979 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1981 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1982 sizeof(wpa_s->pending_interface_name));
1984 os_strlcpy(wpa_s->pending_interface_name, ifname,
1985 sizeof(wpa_s->pending_interface_name));
1986 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1987 MACSTR, wpa_s->pending_interface_name,
1988 MAC2STR(wpa_s->pending_interface_addr));
1994 static void wpas_p2p_remove_pending_group_interface(
1995 struct wpa_supplicant *wpa_s)
1997 if (!wpa_s->pending_interface_name[0] ||
1998 is_zero_ether_addr(wpa_s->pending_interface_addr))
1999 return; /* No pending virtual interface */
2001 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2002 wpa_s->pending_interface_name);
2003 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2004 wpa_s->pending_interface_name);
2005 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2006 wpa_s->pending_interface_name[0] = '\0';
2007 wpa_s->global->pending_group_iface_for_p2ps = 0;
2011 static struct wpa_supplicant *
2012 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2014 struct wpa_interface iface;
2015 struct wpa_supplicant *group_wpa_s;
2017 if (!wpa_s->pending_interface_name[0]) {
2018 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2019 if (!wpas_p2p_create_iface(wpa_s))
2022 * Something has forced us to remove the pending interface; try
2023 * to create a new one and hope for the best that we will get
2024 * the same local address.
2026 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2027 WPA_IF_P2P_CLIENT) < 0)
2031 os_memset(&iface, 0, sizeof(iface));
2032 iface.ifname = wpa_s->pending_interface_name;
2033 iface.driver = wpa_s->driver->name;
2034 if (wpa_s->conf->ctrl_interface == NULL &&
2035 wpa_s->parent != wpa_s &&
2037 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2038 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2040 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
2041 iface.driver_param = wpa_s->conf->driver_param;
2042 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
2043 if (group_wpa_s == NULL) {
2044 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2045 "wpa_supplicant interface");
2048 wpa_s->pending_interface_name[0] = '\0';
2049 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2050 P2P_GROUP_INTERFACE_CLIENT;
2051 wpa_s->global->p2p_group_formation = group_wpa_s;
2052 wpa_s->global->pending_group_iface_for_p2ps = 0;
2054 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2060 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2063 struct wpa_supplicant *wpa_s = eloop_ctx;
2064 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2065 wpas_p2p_group_formation_failed(wpa_s, 0);
2069 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2070 int already_deleted)
2072 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2073 wpa_s->parent, NULL);
2074 if (wpa_s->global->p2p)
2075 p2p_group_formation_failed(wpa_s->global->p2p);
2076 wpas_group_formation_completed(wpa_s, 0, already_deleted);
2080 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2082 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2083 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2084 wpa_s->parent, NULL);
2085 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2086 wpa_s->parent, NULL);
2087 wpa_s->global->p2p_fail_on_wps_complete = 0;
2091 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2093 if (wpa_s->global->p2p_group_formation != wpa_s)
2095 /* Speed up group formation timeout since this cannot succeed */
2096 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2097 wpa_s->parent, NULL);
2098 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2099 wpa_s->parent, NULL);
2103 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2105 struct wpa_supplicant *wpa_s = ctx;
2107 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2108 wpa_drv_cancel_remain_on_channel(wpa_s);
2109 wpa_s->off_channel_freq = 0;
2110 wpa_s->roc_waiting_drv_freq = 0;
2114 wpa_msg_global(wpa_s, MSG_INFO,
2115 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2117 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2118 wpas_p2p_remove_pending_group_interface(wpa_s);
2122 if (!res->role_go) {
2123 /* Inform driver of the operating channel of GO. */
2124 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2127 if (wpa_s->p2p_go_ht40)
2129 if (wpa_s->p2p_go_vht)
2132 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2133 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2135 res->role_go ? "GO" : "client", res->freq, res->ht40,
2136 MAC2STR(res->peer_device_addr),
2137 MAC2STR(res->peer_interface_addr),
2138 p2p_wps_method_text(res->wps_method));
2139 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2141 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2142 struct wpa_ssid *ssid;
2143 ssid = wpa_config_get_network(wpa_s->conf,
2144 wpa_s->p2p_persistent_id);
2145 if (ssid && ssid->disabled == 2 &&
2146 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2147 size_t len = os_strlen(ssid->passphrase);
2148 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2149 "on requested persistent group");
2150 os_memcpy(res->passphrase, ssid->passphrase, len);
2151 res->passphrase[len] = '\0';
2155 if (wpa_s->create_p2p_iface) {
2156 struct wpa_supplicant *group_wpa_s =
2157 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2158 if (group_wpa_s == NULL) {
2159 wpas_p2p_remove_pending_group_interface(wpa_s);
2160 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2162 wpas_p2p_group_formation_failed(wpa_s, 1);
2165 if (group_wpa_s != wpa_s) {
2166 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2167 sizeof(group_wpa_s->p2p_pin));
2168 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2170 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2171 wpa_s->pending_interface_name[0] = '\0';
2172 group_wpa_s->p2p_in_provisioning = 1;
2175 wpas_start_wps_go(group_wpa_s, res, 1);
2177 wpas_start_wps_enrollee(group_wpa_s, res);
2179 wpa_s->p2p_in_provisioning = 1;
2180 wpa_s->global->p2p_group_formation = wpa_s;
2183 wpas_start_wps_go(wpa_s, res, 1);
2185 wpas_start_wps_enrollee(ctx, res);
2188 wpa_s->p2p_long_listen = 0;
2189 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2191 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2192 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2193 (res->peer_config_timeout % 100) * 10000,
2194 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2198 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2201 struct wpa_supplicant *wpa_s = ctx;
2202 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2203 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2204 dev_passwd_id, go_intent);
2206 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
2210 static void wpas_dev_found(void *ctx, const u8 *addr,
2211 const struct p2p_peer_info *info,
2214 #ifndef CONFIG_NO_STDOUT_DEBUG
2215 struct wpa_supplicant *wpa_s = ctx;
2216 char devtype[WPS_DEV_TYPE_BUFSIZE];
2217 char *wfd_dev_info_hex = NULL;
2219 #ifdef CONFIG_WIFI_DISPLAY
2220 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2221 WFD_SUBELEM_DEVICE_INFO);
2222 #endif /* CONFIG_WIFI_DISPLAY */
2224 if (info->p2ps_instance) {
2226 const u8 *buf = wpabuf_head(info->p2ps_instance);
2227 size_t len = wpabuf_len(info->p2ps_instance);
2234 if (len < 4 + 2 + 1)
2236 id = WPA_GET_LE32(buf);
2238 methods = WPA_GET_BE16(buf);
2241 if (str_len > len - 4 - 2 - 1)
2243 os_memcpy(str, buf, str_len);
2244 str[str_len] = '\0';
2246 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2248 wpa_msg_global(wpa_s, MSG_INFO,
2249 P2P_EVENT_DEVICE_FOUND MACSTR
2250 " p2p_dev_addr=" MACSTR
2251 " pri_dev_type=%s name='%s'"
2252 " config_methods=0x%x"
2255 " adv_id=%x asp_svc=%s%s",
2257 MAC2STR(info->p2p_device_addr),
2258 wps_dev_type_bin2str(
2260 devtype, sizeof(devtype)),
2261 info->device_name, methods,
2262 info->dev_capab, info->group_capab,
2264 info->vendor_elems ?
2265 " vendor_elems=1" : "");
2270 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2271 " p2p_dev_addr=" MACSTR
2272 " pri_dev_type=%s name='%s' config_methods=0x%x "
2273 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2274 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2275 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2277 info->device_name, info->config_methods,
2278 info->dev_capab, info->group_capab,
2279 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2280 wfd_dev_info_hex ? wfd_dev_info_hex : "",
2281 info->vendor_elems ? " vendor_elems=1" : "",
2285 os_free(wfd_dev_info_hex);
2286 #endif /* CONFIG_NO_STDOUT_DEBUG */
2288 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2292 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2294 struct wpa_supplicant *wpa_s = ctx;
2296 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2297 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2299 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2303 static void wpas_find_stopped(void *ctx)
2305 struct wpa_supplicant *wpa_s = ctx;
2306 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2307 wpas_notify_p2p_find_stopped(wpa_s);
2311 struct wpas_p2p_listen_work {
2313 unsigned int duration;
2314 struct wpabuf *probe_resp_ie;
2318 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2322 wpabuf_free(lwork->probe_resp_ie);
2327 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2329 struct wpas_p2p_listen_work *lwork;
2331 if (!wpa_s->p2p_listen_work)
2334 lwork = wpa_s->p2p_listen_work->ctx;
2335 wpas_p2p_listen_work_free(lwork);
2336 radio_work_done(wpa_s->p2p_listen_work);
2337 wpa_s->p2p_listen_work = NULL;
2341 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2343 struct wpa_supplicant *wpa_s = work->wpa_s;
2344 struct wpas_p2p_listen_work *lwork = work->ctx;
2345 unsigned int duration;
2348 if (work->started) {
2349 wpa_s->p2p_listen_work = NULL;
2350 wpas_stop_listen(wpa_s);
2352 wpas_p2p_listen_work_free(lwork);
2356 wpa_s->p2p_listen_work = work;
2358 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2360 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2361 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2362 "report received Probe Request frames");
2363 wpas_p2p_listen_work_done(wpa_s);
2367 wpa_s->pending_listen_freq = lwork->freq;
2368 wpa_s->pending_listen_duration = lwork->duration;
2370 duration = lwork->duration;
2371 #ifdef CONFIG_TESTING_OPTIONS
2372 if (wpa_s->extra_roc_dur) {
2373 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2374 duration, duration + wpa_s->extra_roc_dur);
2375 duration += wpa_s->extra_roc_dur;
2377 #endif /* CONFIG_TESTING_OPTIONS */
2379 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2380 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2381 "to remain on channel (%u MHz) for Listen "
2382 "state", lwork->freq);
2383 wpas_p2p_listen_work_done(wpa_s);
2384 wpa_s->pending_listen_freq = 0;
2387 wpa_s->off_channel_freq = 0;
2388 wpa_s->roc_waiting_drv_freq = lwork->freq;
2392 static int wpas_start_listen(void *ctx, unsigned int freq,
2393 unsigned int duration,
2394 const struct wpabuf *probe_resp_ie)
2396 struct wpa_supplicant *wpa_s = ctx;
2397 struct wpas_p2p_listen_work *lwork;
2399 if (wpa_s->p2p_listen_work) {
2400 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2404 lwork = os_zalloc(sizeof(*lwork));
2408 lwork->duration = duration;
2409 if (probe_resp_ie) {
2410 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2411 if (lwork->probe_resp_ie == NULL) {
2412 wpas_p2p_listen_work_free(lwork);
2417 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2419 wpas_p2p_listen_work_free(lwork);
2427 static void wpas_stop_listen(void *ctx)
2429 struct wpa_supplicant *wpa_s = ctx;
2430 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2431 wpa_drv_cancel_remain_on_channel(wpa_s);
2432 wpa_s->off_channel_freq = 0;
2433 wpa_s->roc_waiting_drv_freq = 0;
2435 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2438 * Don't cancel Probe Request RX reporting for a connected P2P Client
2439 * handling Probe Request frames.
2441 if (!wpa_s->p2p_cli_probe)
2442 wpa_drv_probe_req_report(wpa_s, 0);
2444 wpas_p2p_listen_work_done(wpa_s);
2448 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2451 struct wpa_supplicant *wpa_s = ctx;
2452 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2457 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2458 const u8 *peer, const char *params,
2459 unsigned int generated_pin)
2461 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2462 " %08d%s", MAC2STR(peer), generated_pin, params);
2466 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2467 const u8 *peer, const char *params)
2469 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2470 "%s", MAC2STR(peer), params);
2474 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2475 const u8 *dev_addr, const u8 *pri_dev_type,
2476 const char *dev_name, u16 supp_config_methods,
2477 u8 dev_capab, u8 group_capab, const u8 *group_id,
2478 size_t group_id_len)
2480 struct wpa_supplicant *wpa_s = ctx;
2481 char devtype[WPS_DEV_TYPE_BUFSIZE];
2483 u8 empty_dev_type[8];
2484 unsigned int generated_pin = 0;
2485 struct wpa_supplicant *group = NULL;
2489 for (group = wpa_s->global->ifaces; group; group = group->next)
2491 struct wpa_ssid *s = group->current_ssid;
2493 s->mode == WPAS_MODE_P2P_GO &&
2494 group_id_len - ETH_ALEN == s->ssid_len &&
2495 os_memcmp(group_id + ETH_ALEN, s->ssid,
2501 if (pri_dev_type == NULL) {
2502 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2503 pri_dev_type = empty_dev_type;
2505 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2506 " pri_dev_type=%s name='%s' config_methods=0x%x "
2507 "dev_capab=0x%x group_capab=0x%x%s%s",
2509 wps_dev_type_bin2str(pri_dev_type, devtype,
2511 dev_name, supp_config_methods, dev_capab, group_capab,
2512 group ? " group=" : "",
2513 group ? group->ifname : "");
2514 if (os_snprintf_error(sizeof(params), res))
2515 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
2516 params[sizeof(params) - 1] = '\0';
2518 if (config_methods & WPS_CONFIG_DISPLAY) {
2519 generated_pin = wps_generate_pin();
2520 wpas_prov_disc_local_display(wpa_s, peer, params,
2522 } else if (config_methods & WPS_CONFIG_KEYPAD)
2523 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2524 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2525 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2526 MACSTR "%s", MAC2STR(peer), params);
2528 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2529 P2P_PROV_DISC_SUCCESS,
2530 config_methods, generated_pin);
2534 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2536 struct wpa_supplicant *wpa_s = ctx;
2537 unsigned int generated_pin = 0;
2540 if (wpa_s->pending_pd_before_join &&
2541 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2542 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2543 wpa_s->pending_pd_before_join = 0;
2544 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2545 "join-existing-group operation");
2546 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2550 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2551 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2554 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2555 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2556 if (os_snprintf_error(sizeof(params), res))
2557 params[sizeof(params) - 1] = '\0';
2561 if (config_methods & WPS_CONFIG_DISPLAY)
2562 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2563 else if (config_methods & WPS_CONFIG_KEYPAD) {
2564 generated_pin = wps_generate_pin();
2565 wpas_prov_disc_local_display(wpa_s, peer, params,
2567 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2568 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2569 MACSTR "%s", MAC2STR(peer), params);
2571 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2572 P2P_PROV_DISC_SUCCESS,
2573 config_methods, generated_pin);
2577 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2578 enum p2p_prov_disc_status status,
2579 u32 adv_id, const u8 *adv_mac,
2580 const char *deferred_session_resp)
2582 struct wpa_supplicant *wpa_s = ctx;
2584 if (wpa_s->p2p_fallback_to_go_neg) {
2585 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2586 "failed - fall back to GO Negotiation");
2587 wpa_msg_global(wpa_s->parent, MSG_INFO,
2588 P2P_EVENT_FALLBACK_TO_GO_NEG
2589 "reason=PD-failed");
2590 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2594 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2595 wpa_s->pending_pd_before_join = 0;
2596 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2597 "join-existing-group operation (no ACK for PD "
2599 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2603 if (adv_id && adv_mac && deferred_session_resp) {
2604 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2605 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2606 " deferred_session_resp='%s'",
2607 MAC2STR(peer), status, adv_id,
2608 deferred_session_resp);
2609 } else if (adv_id && adv_mac) {
2610 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2611 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2612 MAC2STR(peer), status, adv_id);
2614 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2615 " p2p_dev_addr=" MACSTR " status=%d",
2616 MAC2STR(peer), status);
2619 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2624 static int freq_included(struct wpa_supplicant *wpa_s,
2625 const struct p2p_channels *channels,
2628 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2629 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2635 static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2637 unsigned int num = P2P_MAX_CHANNELS;
2641 p2p_go_dump_common_freqs(wpa_s);
2642 common_freqs = os_calloc(num, sizeof(int));
2646 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2648 wpa_dbg(wpa_s, MSG_DEBUG,
2649 "P2P: Failed to get group common freqs");
2650 os_free(common_freqs);
2654 os_free(wpa_s->p2p_group_common_freqs);
2655 wpa_s->p2p_group_common_freqs = common_freqs;
2656 wpa_s->p2p_group_common_freqs_num = num;
2657 p2p_go_dump_common_freqs(wpa_s);
2662 * Check if the given frequency is one of the possible operating frequencies
2663 * set after the completion of the GO Negotiation.
2665 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2669 p2p_go_dump_common_freqs(wpa_s);
2671 /* assume no restrictions */
2672 if (!wpa_s->p2p_group_common_freqs_num)
2675 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2676 if (wpa_s->p2p_group_common_freqs[i] == freq)
2684 * Pick the best frequency to use from all the currently used frequencies.
2686 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2687 struct wpa_used_freq_data *freqs,
2692 /* find a candidate freq that is supported by P2P */
2693 for (c = 0; c < num; c++)
2694 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2700 /* once we have a candidate, try to find a 'better' one */
2701 for (i = c + 1; i < num; i++) {
2702 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2706 * 1. Infrastructure station interfaces have higher preference.
2707 * 2. P2P Clients have higher preference.
2710 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2715 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2718 return freqs[c].freq;
2722 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2723 const u8 *go_dev_addr, const u8 *ssid,
2724 size_t ssid_len, int *go, u8 *group_bssid,
2725 int *force_freq, int persistent_group,
2726 const struct p2p_channels *channels,
2729 struct wpa_supplicant *wpa_s = ctx;
2731 struct wpa_used_freq_data *freqs;
2732 struct wpa_supplicant *grp;
2735 if (!persistent_group) {
2736 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2737 " to join an active group (SSID: %s)",
2738 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2739 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2740 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2742 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2743 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2744 "authorized invitation");
2748 #ifdef CONFIG_WPS_NFC
2749 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2750 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
2751 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2752 wpa_s->p2p_wps_method = WPS_NFC;
2753 wpa_s->pending_join_wps_method = WPS_NFC;
2754 os_memcpy(wpa_s->pending_join_dev_addr,
2755 go_dev_addr, ETH_ALEN);
2756 os_memcpy(wpa_s->pending_join_iface_addr,
2760 #endif /* CONFIG_WPS_NFC */
2763 * Do not accept the invitation automatically; notify user and
2766 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2769 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2771 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2772 "running persistent group");
2774 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2778 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2779 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2780 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2781 "invitation to re-invoke a persistent group");
2782 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2783 } else if (!wpa_s->conf->persistent_reconnect)
2784 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2786 for (s = wpa_s->conf->ssid; s; s = s->next) {
2787 if (s->disabled == 2 &&
2788 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2789 s->ssid_len == ssid_len &&
2790 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2795 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2796 " requested reinvocation of an unknown group",
2798 return P2P_SC_FAIL_UNKNOWN_GROUP;
2801 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2803 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2804 wpa_printf(MSG_DEBUG, "P2P: The only available "
2805 "interface is already in use - reject "
2807 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2809 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2810 } else if (s->mode == WPAS_MODE_P2P_GO) {
2812 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2814 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2815 "interface address for the group");
2816 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2818 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2823 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2826 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2827 sizeof(struct wpa_used_freq_data));
2829 int num_channels = wpa_s->num_multichan_concurrent;
2830 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2831 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2835 /* Get one of the frequencies currently in use */
2836 if (best_freq > 0) {
2837 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2838 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
2840 if (wpa_s->num_multichan_concurrent < 2 ||
2841 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2842 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2843 *force_freq = best_freq;
2847 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2848 wpas_p2p_num_unused_channels(wpa_s) > 0) {
2850 /* We are the client */
2851 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2852 "running a GO but we are capable of MCC, "
2853 "figure out the best channel to use");
2855 } else if (!freq_included(wpa_s, channels, *force_freq)) {
2856 /* We are the GO, and *force_freq is not in the
2858 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2859 "in intersection but we are capable of MCC, "
2860 "figure out the best channel to use",
2866 return P2P_SC_SUCCESS;
2870 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2871 const u8 *ssid, size_t ssid_len,
2872 const u8 *go_dev_addr, u8 status,
2875 struct wpa_supplicant *wpa_s = ctx;
2878 for (s = wpa_s->conf->ssid; s; s = s->next) {
2879 if (s->disabled == 2 &&
2880 s->ssid_len == ssid_len &&
2881 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2885 if (status == P2P_SC_SUCCESS) {
2886 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2887 " was accepted; op_freq=%d MHz, SSID=%s",
2888 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
2890 int go = s->mode == WPAS_MODE_P2P_GO;
2891 wpas_p2p_group_add_persistent(
2892 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
2893 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
2895 wpa_s->user_initiated_pd = 0;
2896 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2897 wpa_s->p2p_wps_method, 0, op_freq,
2903 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2904 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2905 " was rejected (status %u)", MAC2STR(sa), status);
2911 wpa_msg_global(wpa_s, MSG_INFO,
2912 P2P_EVENT_INVITATION_RECEIVED
2913 "sa=" MACSTR " go_dev_addr=" MACSTR
2914 " bssid=" MACSTR " unknown-network",
2915 MAC2STR(sa), MAC2STR(go_dev_addr),
2918 wpa_msg_global(wpa_s, MSG_INFO,
2919 P2P_EVENT_INVITATION_RECEIVED
2920 "sa=" MACSTR " go_dev_addr=" MACSTR
2922 MAC2STR(sa), MAC2STR(go_dev_addr));
2927 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2928 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2929 "sa=" MACSTR " persistent=%d freq=%d",
2930 MAC2STR(sa), s->id, op_freq);
2932 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2933 "sa=" MACSTR " persistent=%d",
2934 MAC2STR(sa), s->id);
2939 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2940 struct wpa_ssid *ssid,
2941 const u8 *peer, int inv)
2944 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
2949 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2950 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
2954 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
2955 if (ssid->mode != WPAS_MODE_P2P_GO &&
2956 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2957 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2958 "due to invitation result", ssid->id);
2959 wpas_notify_network_removed(wpa_s, ssid);
2960 wpa_config_remove_network(wpa_s->conf, ssid->id);
2963 return; /* Peer not found in client list */
2966 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2967 "group %d client list%s",
2968 MAC2STR(peer), ssid->id,
2969 inv ? " due to invitation result" : "");
2970 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
2971 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
2972 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
2973 ssid->num_p2p_clients--;
2974 if (p2p_wpa_s->conf->update_config &&
2975 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
2976 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2980 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2983 struct wpa_ssid *ssid;
2985 wpa_s = wpa_s->global->p2p_invite_group;
2987 return; /* No known invitation group */
2988 ssid = wpa_s->current_ssid;
2989 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2990 !ssid->p2p_persistent_group)
2991 return; /* Not operating as a GO in persistent group */
2992 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2993 ssid->ssid, ssid->ssid_len);
2994 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2998 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2999 const struct p2p_channels *channels,
3000 const u8 *peer, int neg_freq,
3003 struct wpa_supplicant *wpa_s = ctx;
3004 struct wpa_ssid *ssid;
3008 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3009 "status=%d " MACSTR,
3010 status, MAC2STR(bssid));
3012 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3013 "status=%d ", status);
3015 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3017 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3018 status, MAC2STR(peer));
3019 if (wpa_s->pending_invite_ssid_id == -1) {
3020 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3021 wpas_remove_persistent_client(wpa_s, peer);
3022 return; /* Invitation to active group */
3025 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3026 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3027 "invitation exchange to indicate readiness for "
3031 if (status != P2P_SC_SUCCESS) {
3032 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3033 ssid = wpa_config_get_network(
3034 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3035 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3037 wpas_p2p_remove_pending_group_interface(wpa_s);
3041 ssid = wpa_config_get_network(wpa_s->conf,
3042 wpa_s->pending_invite_ssid_id);
3044 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3045 "data matching with invitation");
3050 * The peer could have missed our ctrl::ack frame for Invitation
3051 * Response and continue retransmitting the frame. To reduce the
3052 * likelihood of the peer not getting successful TX status for the
3053 * Invitation Response frame, wait a short time here before starting
3054 * the persistent group so that we will remain on the current channel to
3055 * acknowledge any possible retransmission from the peer.
3057 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3058 "starting persistent group");
3061 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3062 freq_included(wpa_s, channels, neg_freq))
3064 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3065 freq_included(wpa_s, channels, peer_oper_freq))
3066 freq = peer_oper_freq;
3070 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3071 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3072 wpas_p2p_group_add_persistent(wpa_s, ssid,
3073 ssid->mode == WPAS_MODE_P2P_GO,
3074 wpa_s->p2p_persistent_go_freq,
3076 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3078 ssid->mode == WPAS_MODE_P2P_GO ?
3079 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3084 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3087 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3089 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3093 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3095 reg->channel[reg->channels] = chan;
3100 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3101 struct p2p_channels *chan,
3102 struct p2p_channels *cli_chan)
3106 wpa_s->global->p2p_24ghz_social_channels = 1;
3108 os_memset(cli_chan, 0, sizeof(*cli_chan));
3110 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3113 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3114 chan->reg_class[cla].reg_class = 81;
3115 chan->reg_class[cla].channels = 0;
3116 for (i = 0; i < 11; i++) {
3117 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3118 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3120 if (chan->reg_class[cla].channels)
3123 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3126 /* Operating class 115 - 5 GHz, channels 36-48 */
3127 chan->reg_class[cla].reg_class = 115;
3128 chan->reg_class[cla].channels = 0;
3129 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3130 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3131 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3132 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3133 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3134 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3135 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3136 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3137 if (chan->reg_class[cla].channels)
3140 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3143 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3144 chan->reg_class[cla].reg_class = 124;
3145 chan->reg_class[cla].channels = 0;
3146 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3147 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3148 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3149 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3150 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3151 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3152 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3153 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3154 if (chan->reg_class[cla].channels)
3157 chan->reg_classes = cla;
3162 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3164 enum hostapd_hw_mode mode)
3168 for (i = 0; i < num_modes; i++) {
3169 if (modes[i].mode == mode)
3178 NOT_ALLOWED, NO_IR, ALLOWED
3181 static int has_channel(struct wpa_global *global,
3182 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3187 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3189 if (wpas_p2p_disallowed_freq(global, freq))
3192 for (i = 0; i < mode->num_channels; i++) {
3193 if (mode->channels[i].chan == chan) {
3195 *flags = mode->channels[i].flag;
3196 if (mode->channels[i].flag &
3197 (HOSTAPD_CHAN_DISABLED |
3198 HOSTAPD_CHAN_RADAR))
3200 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3210 struct p2p_oper_class_map {
3211 enum hostapd_hw_mode mode;
3216 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3219 static const struct p2p_oper_class_map op_class[] = {
3220 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3221 #if 0 /* Do not enable HT40 on 2 GHz for now */
3222 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3223 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3225 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3226 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3227 { HOSTAPD_MODE_IEEE80211A, 125, 149, 169, 4, BW20 },
3228 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3229 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3230 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3231 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3234 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3235 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3236 * 80 MHz, but currently use the following definition for simplicity
3237 * (these center frequencies are not actual channels, which makes
3238 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3239 * removing invalid channels.
3241 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3242 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3243 { -1, 0, 0, 0, 0, BW20 }
3247 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3248 struct hostapd_hw_modes *mode,
3251 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3254 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3257 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3259 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3260 * so the center channel is 6 channels away from the start/end.
3262 if (channel >= center_channels[i] - 6 &&
3263 channel <= center_channels[i] + 6)
3264 return center_channels[i];
3270 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3271 struct hostapd_hw_modes *mode,
3276 enum chan_allowed res, ret = ALLOWED;
3278 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3281 if (center_chan >= 58 && center_chan <= 138)
3282 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3284 /* check all the channels are available */
3285 for (i = 0; i < 4; i++) {
3286 int adj_chan = center_chan - 6 + i * 4;
3288 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3289 if (res == NOT_ALLOWED)
3294 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3296 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3298 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3300 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3308 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3309 struct hostapd_hw_modes *mode,
3313 enum chan_allowed res, res2;
3315 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3316 if (bw == BW40MINUS) {
3317 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3319 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3320 } else if (bw == BW40PLUS) {
3321 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3323 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3324 } else if (bw == BW80) {
3325 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3328 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3330 if (res == NO_IR || res2 == NO_IR)
3336 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3337 struct p2p_channels *chan,
3338 struct p2p_channels *cli_chan)
3340 struct hostapd_hw_modes *mode;
3341 int cla, op, cli_cla;
3343 if (wpa_s->hw.modes == NULL) {
3344 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3345 "of all supported channels; assume dualband "
3347 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3352 for (op = 0; op_class[op].op_class; op++) {
3353 const struct p2p_oper_class_map *o = &op_class[op];
3355 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3357 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3360 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3361 wpa_s->global->p2p_24ghz_social_channels = 1;
3362 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3363 enum chan_allowed res;
3364 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3365 if (res == ALLOWED) {
3367 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3369 reg = &chan->reg_class[cla];
3371 reg->reg_class = o->op_class;
3373 reg->channel[reg->channels] = ch;
3375 } else if (res == NO_IR &&
3376 wpa_s->conf->p2p_add_cli_chan) {
3377 if (cli_reg == NULL) {
3378 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3380 cli_reg = &cli_chan->reg_class[cli_cla];
3382 cli_reg->reg_class = o->op_class;
3384 cli_reg->channel[cli_reg->channels] = ch;
3385 cli_reg->channels++;
3389 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3390 reg->channel, reg->channels);
3393 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3394 cli_reg->channel, cli_reg->channels);
3398 chan->reg_classes = cla;
3399 cli_chan->reg_classes = cli_cla;
3405 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3406 struct hostapd_hw_modes *mode, u8 channel)
3409 enum chan_allowed ret;
3411 for (op = 0; op_class[op].op_class; op++) {
3412 const struct p2p_oper_class_map *o = &op_class[op];
3415 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3416 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3417 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3420 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3422 return (o->bw == BW40MINUS) ? -1 : 1;
3429 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3430 struct hostapd_hw_modes *mode, u8 channel)
3432 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3435 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3439 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3442 struct wpa_supplicant *wpa_s = ctx;
3444 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3445 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3451 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3455 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3456 const u8 *ssid, size_t ssid_len)
3458 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3459 struct wpa_ssid *s = wpa_s->current_ssid;
3462 if (s->mode != WPAS_MODE_P2P_GO &&
3463 s->mode != WPAS_MODE_AP &&
3464 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3466 if (s->ssid_len != ssid_len ||
3467 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3477 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3478 const u8 *peer_dev_addr)
3480 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3481 struct wpa_ssid *ssid = wpa_s->current_ssid;
3482 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
3484 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3492 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3494 struct wpa_supplicant *wpa_s = ctx;
3496 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3500 static int wpas_is_concurrent_session_active(void *ctx)
3502 struct wpa_supplicant *wpa_s = ctx;
3503 struct wpa_supplicant *ifs;
3505 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3508 if (ifs->wpa_state > WPA_ASSOCIATED)
3515 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3517 struct wpa_supplicant *wpa_s = ctx;
3518 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3522 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3523 const char *conf_p2p_dev)
3525 struct wpa_interface iface;
3526 struct wpa_supplicant *p2pdev_wpa_s;
3528 char force_name[100];
3531 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3533 if (os_snprintf_error(sizeof(ifname), ret))
3535 force_name[0] = '\0';
3536 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3537 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3538 force_name, wpa_s->pending_interface_addr, NULL);
3540 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3543 os_strlcpy(wpa_s->pending_interface_name, ifname,
3544 sizeof(wpa_s->pending_interface_name));
3546 os_memset(&iface, 0, sizeof(iface));
3548 iface.ifname = wpa_s->pending_interface_name;
3549 iface.driver = wpa_s->driver->name;
3550 iface.driver_param = wpa_s->conf->driver_param;
3553 * If a P2P Device configuration file was given, use it as the interface
3554 * configuration file (instead of using parent's configuration file.
3557 iface.confname = conf_p2p_dev;
3558 iface.ctrl_interface = NULL;
3560 iface.confname = wpa_s->confname;
3561 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3564 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
3565 if (!p2pdev_wpa_s) {
3566 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3570 wpa_s->pending_interface_name[0] = '\0';
3575 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3576 const u8 *noa, size_t noa_len)
3578 struct wpa_supplicant *wpa_s, *intf = ctx;
3581 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3582 if (wpa_s->waiting_presence_resp)
3586 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3589 wpa_s->waiting_presence_resp = 0;
3591 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3592 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3593 " status=%u noa=%s", MAC2STR(src), status, hex);
3597 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3598 size_t ssid_len, u8 *go_dev_addr,
3599 u8 *ret_ssid, size_t *ret_ssid_len,
3600 u8 *intended_iface_addr)
3602 struct wpa_supplicant *wpa_s = ctx;
3605 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3607 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3608 *ret_ssid_len = s->ssid_len;
3609 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3611 if (s->mode != WPAS_MODE_P2P_GO) {
3612 os_memset(intended_iface_addr, 0, ETH_ALEN);
3613 } else if (wpas_p2p_create_iface(wpa_s)) {
3614 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3617 os_memcpy(intended_iface_addr,
3618 wpa_s->pending_interface_addr, ETH_ALEN);
3620 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3630 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3631 u8 *ssid, size_t *ssid_len, int *group_iface)
3633 struct wpa_supplicant *wpa_s = ctx;
3638 * group_iface will be set to 1 only if a dedicated interface for P2P
3639 * role is required. First, we try to reuse an active GO. However,
3640 * if it is not present, we will try to reactivate an existing
3641 * persistent group and set group_iface to 1, so the caller will know
3642 * that the pending interface should be used.
3645 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
3647 s = wpas_p2p_get_persistent_go(wpa_s);
3648 *group_iface = wpas_p2p_create_iface(wpa_s);
3650 os_memcpy(bssid, s->bssid, ETH_ALEN);
3655 os_memcpy(intended_addr, bssid, ETH_ALEN);
3656 os_memcpy(ssid, s->ssid, s->ssid_len);
3657 *ssid_len = s->ssid_len;
3663 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3664 const u8 *ssid, size_t ssid_len)
3666 struct wpa_supplicant *wpa_s = ctx;
3668 int save_config = 0;
3671 /* Start with our first choice of Persistent Groups */
3672 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3673 if (go && ssid && ssid_len &&
3674 s->ssid_len == ssid_len &&
3675 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3676 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3679 /* Remove stale persistent group */
3680 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3681 wpa_config_remove_network(wpa_s->conf, s->id);
3686 for (i = 0; i < s->num_p2p_clients; i++) {
3687 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3688 peer, ETH_ALEN) != 0)
3691 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3692 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3693 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3696 s->num_p2p_clients--;
3701 p2p_config_write(wpa_s);
3703 /* Return TRUE if valid SSID remains */
3708 static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3709 const u8 *feat_cap, size_t feat_cap_len)
3711 static const char pref[] = " feature_cap=";
3717 * We expect a feature capability to contain at least one byte to be
3718 * reported. The string buffer provided by the caller function is
3719 * expected to be big enough to contain all bytes of the attribute for
3720 * known specifications. This function truncates the reported bytes if
3721 * the feature capability data exceeds the string buffer size.
3723 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3726 os_memcpy(buf, pref, sizeof(pref));
3727 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3728 buf_len - sizeof(pref) + 1,
3729 feat_cap, feat_cap_len);
3731 if (ret != (2 * (int) feat_cap_len))
3732 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3736 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3737 const u8 *adv_mac, const u8 *ses_mac,
3738 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3739 u8 conncap, int passwd_id,
3740 const u8 *persist_ssid,
3741 size_t persist_ssid_size, int response_done,
3742 int prov_start, const char *session_info,
3743 const u8 *feat_cap, size_t feat_cap_len)
3745 struct wpa_supplicant *wpa_s = ctx;
3747 struct wpa_ssid *persistent_go, *stale, *s;
3748 int save_config = 0;
3749 struct wpa_supplicant *go_wpa_s;
3750 char feat_cap_str[256];
3755 os_memset(mac, 0, ETH_ALEN);
3763 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3764 feat_cap, feat_cap_len);
3767 if (session_info == NULL) {
3768 wpa_msg_global(wpa_s, MSG_INFO,
3769 P2P_EVENT_P2PS_PROVISION_START MACSTR
3770 " adv_id=%x conncap=%x"
3772 " session=%x mac=" MACSTR
3773 " dev_passwd_id=%d%s",
3774 MAC2STR(dev), adv_id, conncap,
3776 ses_id, MAC2STR(ses_mac),
3777 passwd_id, feat_cap_str);
3779 wpa_msg_global(wpa_s, MSG_INFO,
3780 P2P_EVENT_P2PS_PROVISION_START MACSTR
3781 " adv_id=%x conncap=%x"
3783 " session=%x mac=" MACSTR
3784 " dev_passwd_id=%d info='%s'%s",
3785 MAC2STR(dev), adv_id, conncap,
3787 ses_id, MAC2STR(ses_mac),
3788 passwd_id, session_info, feat_cap_str);
3793 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3794 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3796 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3797 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3798 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3800 if (persistent_go && !persistent_go->num_p2p_clients) {
3801 /* remove empty persistent GO */
3802 wpa_config_remove_network(wpa_s->conf,
3806 wpa_msg_global(wpa_s, MSG_INFO,
3807 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3809 " adv_id=%x adv_mac=" MACSTR
3810 " session=%x mac=" MACSTR "%s",
3811 MAC2STR(dev), status,
3812 adv_id, MAC2STR(adv_mac),
3813 ses_id, MAC2STR(ses_mac), feat_cap_str);
3817 /* Clean up stale persistent groups with this device */
3818 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3821 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
3822 is_zero_ether_addr(grp_mac)) {
3823 wpa_dbg(wpa_s, MSG_ERROR,
3824 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
3829 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3833 if (s && s->ssid_len == stale->ssid_len &&
3834 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3835 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3838 /* Remove stale persistent group */
3839 if (stale->mode != WPAS_MODE_P2P_GO ||
3840 stale->num_p2p_clients <= 1) {
3841 wpa_config_remove_network(wpa_s->conf, stale->id);
3845 for (i = 0; i < stale->num_p2p_clients; i++) {
3846 if (os_memcmp(stale->p2p_client_list +
3848 dev, ETH_ALEN) == 0) {
3849 os_memmove(stale->p2p_client_list +
3851 stale->p2p_client_list +
3853 (stale->num_p2p_clients -
3858 stale->num_p2p_clients--;
3864 p2p_config_write(wpa_s);
3867 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3868 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3870 if (persistent_go && s != persistent_go &&
3871 !persistent_go->num_p2p_clients) {
3872 /* remove empty persistent GO */
3873 wpa_config_remove_network(wpa_s->conf,
3878 wpa_msg_global(wpa_s, MSG_INFO,
3879 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3881 " adv_id=%x adv_mac=" MACSTR
3882 " session=%x mac=" MACSTR
3884 MAC2STR(dev), status,
3885 adv_id, MAC2STR(adv_mac),
3886 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
3890 if (conncap == P2PS_SETUP_GROUP_OWNER) {
3892 * We need to copy the interface name. Simply saving a
3893 * pointer isn't enough, since if we use pending_interface_name
3894 * it will be overwritten when the group is added.
3896 char go_ifname[100];
3898 go_ifname[0] = '\0';
3900 wpa_s->global->pending_p2ps_group = 1;
3902 if (!wpas_p2p_create_iface(wpa_s))
3903 os_memcpy(go_ifname, wpa_s->ifname,
3905 else if (wpa_s->pending_interface_name[0])
3906 os_memcpy(go_ifname,
3907 wpa_s->pending_interface_name,
3910 if (!go_ifname[0]) {
3911 wpas_p2ps_prov_complete(
3912 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3913 dev, adv_mac, ses_mac,
3914 grp_mac, adv_id, ses_id, 0, 0,
3915 NULL, 0, 0, 0, NULL, NULL, 0);
3919 /* If PD Resp complete, start up the GO */
3920 if (response_done && persistent_go) {
3921 wpas_p2p_group_add_persistent(
3922 wpa_s, persistent_go,
3923 0, 0, 0, 0, 0, NULL,
3924 persistent_go->mode ==
3926 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3928 } else if (response_done) {
3929 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3932 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3933 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
3935 wpa_s->p2ps_method_config_any = 1;
3937 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3938 os_memcpy(go_ifname, go_wpa_s->ifname,
3941 if (is_zero_ether_addr(grp_mac)) {
3942 wpa_dbg(go_wpa_s, MSG_DEBUG,
3943 "P2P: Setting PIN-1 for ANY");
3944 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
3945 "12345670", NULL, 0,
3948 wpa_dbg(go_wpa_s, MSG_DEBUG,
3949 "P2P: Setting PIN-1 for " MACSTR,
3951 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
3952 "12345670", NULL, 0,
3956 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
3957 wpa_s->p2ps_method_config_any = 1;
3960 wpa_msg_global(wpa_s, MSG_INFO,
3961 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3962 " status=%d conncap=%x"
3963 " adv_id=%x adv_mac=" MACSTR
3964 " session=%x mac=" MACSTR
3965 " dev_passwd_id=%d go=%s%s",
3966 MAC2STR(dev), status, conncap,
3967 adv_id, MAC2STR(adv_mac),
3968 ses_id, MAC2STR(ses_mac),
3969 passwd_id, go_ifname, feat_cap_str);
3973 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3974 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3976 if (persistent_go && !persistent_go->num_p2p_clients) {
3977 /* remove empty persistent GO */
3978 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
3981 if (conncap == P2PS_SETUP_CLIENT) {
3982 wpa_msg_global(wpa_s, MSG_INFO,
3983 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3984 " status=%d conncap=%x"
3985 " adv_id=%x adv_mac=" MACSTR
3986 " session=%x mac=" MACSTR
3987 " dev_passwd_id=%d join=" MACSTR "%s",
3988 MAC2STR(dev), status, conncap,
3989 adv_id, MAC2STR(adv_mac),
3990 ses_id, MAC2STR(ses_mac),
3991 passwd_id, MAC2STR(grp_mac), feat_cap_str);
3993 wpa_msg_global(wpa_s, MSG_INFO,
3994 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3995 " status=%d conncap=%x"
3996 " adv_id=%x adv_mac=" MACSTR
3997 " session=%x mac=" MACSTR
3998 " dev_passwd_id=%d%s",
3999 MAC2STR(dev), status, conncap,
4000 adv_id, MAC2STR(adv_mac),
4001 ses_id, MAC2STR(ses_mac),
4002 passwd_id, feat_cap_str);
4007 static int _wpas_p2p_in_progress(void *ctx)
4009 struct wpa_supplicant *wpa_s = ctx;
4010 return wpas_p2p_in_progress(wpa_s);
4014 static int wpas_prov_disc_resp_cb(void *ctx)
4016 struct wpa_supplicant *wpa_s = ctx;
4017 struct wpa_ssid *persistent_go;
4019 if (!wpa_s->global->pending_p2ps_group)
4022 wpa_s->global->pending_p2ps_group = 0;
4024 if (wpas_p2p_get_go_group(wpa_s))
4026 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4028 if (persistent_go) {
4029 wpas_p2p_group_add_persistent(
4030 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
4031 persistent_go->mode == WPAS_MODE_P2P_GO ?
4032 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
4034 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
4041 static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4043 unsigned int *freq_list)
4045 struct wpa_supplicant *wpa_s = ctx;
4047 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4048 WPA_IF_P2P_CLIENT, len, freq_list);
4053 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4054 * @global: Pointer to global data from wpa_supplicant_init()
4055 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4056 * Returns: 0 on success, -1 on failure
4058 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4060 struct p2p_config p2p;
4063 if (wpa_s->conf->p2p_disabled)
4066 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4072 os_memset(&p2p, 0, sizeof(p2p));
4074 p2p.debug_print = wpas_p2p_debug_print;
4075 p2p.p2p_scan = wpas_p2p_scan;
4076 p2p.send_action = wpas_send_action;
4077 p2p.send_action_done = wpas_send_action_done;
4078 p2p.go_neg_completed = wpas_go_neg_completed;
4079 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4080 p2p.dev_found = wpas_dev_found;
4081 p2p.dev_lost = wpas_dev_lost;
4082 p2p.find_stopped = wpas_find_stopped;
4083 p2p.start_listen = wpas_start_listen;
4084 p2p.stop_listen = wpas_stop_listen;
4085 p2p.send_probe_resp = wpas_send_probe_resp;
4086 p2p.sd_request = wpas_sd_request;
4087 p2p.sd_response = wpas_sd_response;
4088 p2p.prov_disc_req = wpas_prov_disc_req;
4089 p2p.prov_disc_resp = wpas_prov_disc_resp;
4090 p2p.prov_disc_fail = wpas_prov_disc_fail;
4091 p2p.invitation_process = wpas_invitation_process;
4092 p2p.invitation_received = wpas_invitation_received;
4093 p2p.invitation_result = wpas_invitation_result;
4094 p2p.get_noa = wpas_get_noa;
4095 p2p.go_connected = wpas_go_connected;
4096 p2p.presence_resp = wpas_presence_resp;
4097 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4098 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4099 p2p.get_persistent_group = wpas_get_persistent_group;
4100 p2p.get_go_info = wpas_get_go_info;
4101 p2p.remove_stale_groups = wpas_remove_stale_groups;
4102 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4103 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4104 p2p.p2ps_group_capability = p2ps_group_capability;
4105 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
4107 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4108 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4109 p2p.dev_name = wpa_s->conf->device_name;
4110 p2p.manufacturer = wpa_s->conf->manufacturer;
4111 p2p.model_name = wpa_s->conf->model_name;
4112 p2p.model_number = wpa_s->conf->model_number;
4113 p2p.serial_number = wpa_s->conf->serial_number;
4115 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4116 p2p.config_methods = wpa_s->wps->config_methods;
4119 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4120 wpa_printf(MSG_ERROR,
4121 "P2P: Failed to configure supported channel list");
4125 if (wpa_s->conf->p2p_listen_reg_class &&
4126 wpa_s->conf->p2p_listen_channel) {
4127 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4128 p2p.channel = wpa_s->conf->p2p_listen_channel;
4129 p2p.channel_forced = 1;
4132 * Pick one of the social channels randomly as the listen
4135 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4136 &p2p.channel) != 0) {
4137 wpa_printf(MSG_INFO,
4138 "P2P: No social channels supported by the driver - do not enable P2P");
4141 p2p.channel_forced = 0;
4143 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4144 p2p.reg_class, p2p.channel);
4146 if (wpa_s->conf->p2p_oper_reg_class &&
4147 wpa_s->conf->p2p_oper_channel) {
4148 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4149 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4150 p2p.cfg_op_channel = 1;
4151 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4152 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4156 * Use random operation channel from 2.4 GHz band social
4157 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4158 * other preference is indicated.
4160 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4161 &p2p.op_channel) != 0) {
4162 wpa_printf(MSG_ERROR,
4163 "P2P: Failed to select random social channel as operation channel");
4166 p2p.cfg_op_channel = 0;
4167 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4168 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4171 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4172 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4173 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4176 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4177 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4178 p2p.country[2] = 0x04;
4180 os_memcpy(p2p.country, "XX\x04", 3);
4182 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4185 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4186 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4187 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4189 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4190 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4192 p2p.max_peers = 100;
4194 if (wpa_s->conf->p2p_ssid_postfix) {
4195 p2p.ssid_postfix_len =
4196 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4197 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4198 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4199 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4200 p2p.ssid_postfix_len);
4203 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4205 p2p.max_listen = wpa_s->max_remain_on_chan;
4207 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4208 wpa_s->conf->p2p_passphrase_len <= 63)
4209 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4211 p2p.passphrase_len = 8;
4213 global->p2p = p2p_init(&p2p);
4214 if (global->p2p == NULL)
4216 global->p2p_init_wpa_s = wpa_s;
4218 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4219 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4221 p2p_add_wps_vendor_extension(
4222 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4225 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4232 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4233 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4235 * This function deinitialize per-interface P2P data.
4237 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4239 if (wpa_s->driver && wpa_s->drv_priv)
4240 wpa_drv_probe_req_report(wpa_s, 0);
4242 if (wpa_s->go_params) {
4243 /* Clear any stored provisioning info */
4244 p2p_clear_provisioning_info(
4246 wpa_s->go_params->peer_device_addr);
4249 os_free(wpa_s->go_params);
4250 wpa_s->go_params = NULL;
4251 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4252 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4253 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4254 wpa_s->p2p_long_listen = 0;
4255 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4256 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4257 wpas_p2p_remove_pending_group_interface(wpa_s);
4258 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4259 wpas_p2p_listen_work_done(wpa_s);
4260 if (wpa_s->p2p_send_action_work) {
4261 os_free(wpa_s->p2p_send_action_work->ctx);
4262 radio_work_done(wpa_s->p2p_send_action_work);
4263 wpa_s->p2p_send_action_work = NULL;
4265 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4267 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4268 wpa_s->p2p_oob_dev_pw = NULL;
4270 os_free(wpa_s->p2p_group_common_freqs);
4271 wpa_s->p2p_group_common_freqs = NULL;
4272 wpa_s->p2p_group_common_freqs_num = 0;
4274 /* TODO: remove group interface from the driver if this wpa_s instance
4275 * is on top of a P2P group interface */
4280 * wpas_p2p_deinit_global - Deinitialize global P2P module
4281 * @global: Pointer to global data from wpa_supplicant_init()
4283 * This function deinitializes the global (per device) P2P module.
4285 static void wpas_p2p_deinit_global(struct wpa_global *global)
4287 struct wpa_supplicant *wpa_s, *tmp;
4289 wpa_s = global->ifaces;
4291 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4293 /* Remove remaining P2P group interfaces */
4294 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4295 wpa_s = wpa_s->next;
4297 tmp = global->ifaces;
4300 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4305 /* Disconnect from the P2P group and deinit the interface */
4306 wpas_p2p_disconnect(tmp);
4310 * Deinit GO data on any possibly remaining interface (if main
4311 * interface is used as GO).
4313 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4314 if (wpa_s->ap_iface)
4315 wpas_p2p_group_deinit(wpa_s);
4318 p2p_deinit(global->p2p);
4320 global->p2p_init_wpa_s = NULL;
4324 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4326 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4327 wpa_s->conf->p2p_no_group_iface)
4328 return 0; /* separate interface disabled per configuration */
4329 if (wpa_s->drv_flags &
4330 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4331 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4332 return 1; /* P2P group requires a new interface in every case
4334 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4335 return 0; /* driver does not support concurrent operations */
4336 if (wpa_s->global->ifaces->next)
4337 return 1; /* more that one interface already in use */
4338 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4339 return 1; /* this interface is already in use */
4344 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4345 const u8 *peer_addr,
4346 enum p2p_wps_method wps_method,
4347 int go_intent, const u8 *own_interface_addr,
4348 unsigned int force_freq, int persistent_group,
4349 struct wpa_ssid *ssid, unsigned int pref_freq)
4351 if (persistent_group && wpa_s->conf->persistent_reconnect)
4352 persistent_group = 2;
4355 * Increase GO config timeout if HT40 is used since it takes some time
4356 * to scan channels for coex purposes before the BSS can be started.
4358 p2p_set_config_timeout(wpa_s->global->p2p,
4359 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4361 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4362 go_intent, own_interface_addr, force_freq,
4363 persistent_group, ssid ? ssid->ssid : NULL,
4364 ssid ? ssid->ssid_len : 0,
4365 wpa_s->p2p_pd_before_go_neg, pref_freq,
4366 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4371 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4372 const u8 *peer_addr,
4373 enum p2p_wps_method wps_method,
4374 int go_intent, const u8 *own_interface_addr,
4375 unsigned int force_freq, int persistent_group,
4376 struct wpa_ssid *ssid, unsigned int pref_freq)
4378 if (persistent_group && wpa_s->conf->persistent_reconnect)
4379 persistent_group = 2;
4381 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4382 go_intent, own_interface_addr, force_freq,
4383 persistent_group, ssid ? ssid->ssid : NULL,
4384 ssid ? ssid->ssid_len : 0, pref_freq,
4385 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4390 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4392 wpa_s->p2p_join_scan_count++;
4393 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4394 wpa_s->p2p_join_scan_count);
4395 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4396 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4397 " for join operationg - stop join attempt",
4398 MAC2STR(wpa_s->pending_join_iface_addr));
4399 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4400 if (wpa_s->p2p_auto_pd) {
4401 wpa_s->p2p_auto_pd = 0;
4402 wpa_msg_global(wpa_s, MSG_INFO,
4403 P2P_EVENT_PROV_DISC_FAILURE
4404 " p2p_dev_addr=" MACSTR " status=N/A",
4405 MAC2STR(wpa_s->pending_join_dev_addr));
4408 wpa_msg_global(wpa_s->parent, MSG_INFO,
4409 P2P_EVENT_GROUP_FORMATION_FAILURE);
4414 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4417 unsigned int num, i;
4418 struct wpa_used_freq_data *freqs;
4420 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4421 /* Multiple channels are supported and not all are in use */
4425 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4426 sizeof(struct wpa_used_freq_data));
4430 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4431 wpa_s->num_multichan_concurrent);
4433 for (i = 0; i < num; i++) {
4434 if (freqs[i].freq == freq) {
4435 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4442 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4451 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4452 const u8 *peer_dev_addr)
4454 struct wpa_bss *bss;
4457 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4460 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4461 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4466 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4468 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4469 "%ld.%06ld (%supdated in last scan)",
4470 bss->last_update.sec, bss->last_update.usec,
4471 updated ? "": "not ");
4477 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4478 struct wpa_scan_results *scan_res)
4480 struct wpa_bss *bss = NULL;
4482 u8 iface_addr[ETH_ALEN];
4484 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4486 if (wpa_s->global->p2p_disabled)
4489 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4490 scan_res ? (int) scan_res->num : -1,
4491 wpa_s->p2p_auto_join ? "auto_" : "");
4494 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4496 if (wpa_s->p2p_auto_pd) {
4497 int join = wpas_p2p_peer_go(wpa_s,
4498 wpa_s->pending_join_dev_addr);
4500 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4501 wpa_s->auto_pd_scan_retry++;
4502 bss = wpa_bss_get_bssid_latest(
4503 wpa_s, wpa_s->pending_join_dev_addr);
4506 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4507 "the peer " MACSTR " at %d MHz",
4508 wpa_s->auto_pd_scan_retry,
4510 pending_join_dev_addr),
4512 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4520 wpa_s->p2p_auto_pd = 0;
4521 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4522 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4523 MAC2STR(wpa_s->pending_join_dev_addr), join);
4524 if (p2p_prov_disc_req(wpa_s->global->p2p,
4525 wpa_s->pending_join_dev_addr, NULL,
4526 wpa_s->pending_pd_config_methods, join,
4527 0, wpa_s->user_initiated_pd) < 0) {
4528 wpa_s->p2p_auto_pd = 0;
4529 wpa_msg_global(wpa_s, MSG_INFO,
4530 P2P_EVENT_PROV_DISC_FAILURE
4531 " p2p_dev_addr=" MACSTR " status=N/A",
4532 MAC2STR(wpa_s->pending_join_dev_addr));
4537 if (wpa_s->p2p_auto_join) {
4538 int join = wpas_p2p_peer_go(wpa_s,
4539 wpa_s->pending_join_dev_addr);
4541 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4542 "running a GO -> use GO Negotiation");
4543 wpa_msg_global(wpa_s->parent, MSG_INFO,
4544 P2P_EVENT_FALLBACK_TO_GO_NEG
4545 "reason=peer-not-running-GO");
4546 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4547 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4548 wpa_s->p2p_persistent_group, 0, 0, 0,
4549 wpa_s->p2p_go_intent,
4550 wpa_s->p2p_connect_freq,
4551 wpa_s->p2p_persistent_id,
4552 wpa_s->p2p_pd_before_go_neg,
4558 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4559 "try to join the group", join ? "" :
4562 wpa_msg_global(wpa_s->parent, MSG_INFO,
4563 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
4564 wpa_s->p2p_fallback_to_go_neg = 1;
4568 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4569 wpa_s->pending_join_iface_addr);
4571 p2p_get_interface_addr(wpa_s->global->p2p,
4572 wpa_s->pending_join_dev_addr,
4574 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4575 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4576 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4577 "address for join from " MACSTR " to " MACSTR
4578 " based on newly discovered P2P peer entry",
4579 MAC2STR(wpa_s->pending_join_iface_addr),
4580 MAC2STR(iface_addr));
4581 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4584 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4585 wpa_s->pending_join_iface_addr);
4588 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4589 "from P2P peer table: %d MHz", freq);
4591 if (wpa_s->p2p_join_ssid_len) {
4592 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4593 MACSTR " and SSID %s",
4594 MAC2STR(wpa_s->pending_join_iface_addr),
4595 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4596 wpa_s->p2p_join_ssid_len));
4597 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4598 wpa_s->p2p_join_ssid,
4599 wpa_s->p2p_join_ssid_len);
4602 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4603 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4604 bss = wpa_bss_get_bssid_latest(wpa_s,
4605 wpa_s->pending_join_iface_addr);
4608 u8 dev_addr[ETH_ALEN];
4611 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4612 "from BSS table: %d MHz (SSID %s)", freq,
4613 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4614 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4616 os_memcmp(wpa_s->pending_join_dev_addr,
4617 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4618 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4620 wpa_printf(MSG_DEBUG,
4621 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4623 MAC2STR(wpa_s->pending_join_dev_addr));
4624 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4631 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4632 wpa_msg_global(wpa_s->parent, MSG_INFO,
4633 P2P_EVENT_GROUP_FORMATION_FAILURE
4634 "reason=FREQ_CONFLICT");
4638 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4639 "prior to joining an existing group (GO " MACSTR
4641 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4642 wpa_s->pending_pd_before_join = 1;
4644 switch (wpa_s->pending_join_wps_method) {
4645 case WPS_PIN_DISPLAY:
4646 method = WPS_CONFIG_KEYPAD;
4648 case WPS_PIN_KEYPAD:
4649 method = WPS_CONFIG_DISPLAY;
4652 method = WPS_CONFIG_PUSHBUTTON;
4659 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4660 wpa_s->pending_join_dev_addr) ==
4663 * We have already performed provision discovery for
4664 * joining the group. Proceed directly to join
4665 * operation without duplicated provision discovery. */
4666 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4667 "with " MACSTR " already done - proceed to "
4669 MAC2STR(wpa_s->pending_join_dev_addr));
4670 wpa_s->pending_pd_before_join = 0;
4674 if (p2p_prov_disc_req(wpa_s->global->p2p,
4675 wpa_s->pending_join_dev_addr,
4677 freq, wpa_s->user_initiated_pd) < 0) {
4678 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4679 "Discovery Request before joining an "
4681 wpa_s->pending_pd_before_join = 0;
4687 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4688 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4689 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4690 wpas_p2p_check_join_scan_limit(wpa_s);
4694 /* Start join operation immediately */
4695 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4699 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4700 const u8 *ssid, size_t ssid_len)
4703 struct wpa_driver_scan_params params;
4704 struct wpabuf *wps_ie, *ies;
4706 int freqs[2] = { 0, 0 };
4708 os_memset(¶ms, 0, sizeof(params));
4710 /* P2P Wildcard SSID */
4711 params.num_ssids = 1;
4712 if (ssid && ssid_len) {
4713 params.ssids[0].ssid = ssid;
4714 params.ssids[0].ssid_len = ssid_len;
4715 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4716 wpa_s->p2p_join_ssid_len = ssid_len;
4718 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4719 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4720 wpa_s->p2p_join_ssid_len = 0;
4723 wpa_s->wps->dev.p2p = 1;
4724 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4725 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4727 if (wps_ie == NULL) {
4728 wpas_p2p_scan_res_join(wpa_s, NULL);
4732 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4733 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4735 wpabuf_free(wps_ie);
4736 wpas_p2p_scan_res_join(wpa_s, NULL);
4739 wpabuf_put_buf(ies, wps_ie);
4740 wpabuf_free(wps_ie);
4742 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4744 params.p2p_probe = 1;
4745 params.extra_ies = wpabuf_head(ies);
4746 params.extra_ies_len = wpabuf_len(ies);
4751 * If freq is not provided, check the operating freq of the GO
4752 * and use a single channel scan on if possible.
4754 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4755 wpa_s->pending_join_iface_addr);
4761 params.freqs = freqs;
4765 * Run a scan to update BSS table and start Provision Discovery once
4766 * the new scan results become available.
4768 ret = wpa_drv_scan(wpa_s, ¶ms);
4770 os_get_reltime(&wpa_s->scan_trigger_time);
4771 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4772 wpa_s->own_scan_requested = 1;
4778 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4780 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4781 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4782 wpas_p2p_check_join_scan_limit(wpa_s);
4787 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4789 struct wpa_supplicant *wpa_s = eloop_ctx;
4790 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4794 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4795 const u8 *dev_addr, enum p2p_wps_method wps_method,
4796 int auto_join, int op_freq,
4797 const u8 *ssid, size_t ssid_len)
4799 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4800 MACSTR " dev " MACSTR " op_freq=%d)%s",
4801 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4802 auto_join ? " (auto_join)" : "");
4803 if (ssid && ssid_len) {
4804 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4805 wpa_ssid_txt(ssid, ssid_len));
4808 wpa_s->p2p_auto_pd = 0;
4809 wpa_s->p2p_auto_join = !!auto_join;
4810 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4811 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4812 wpa_s->pending_join_wps_method = wps_method;
4814 /* Make sure we are not running find during connection establishment */
4815 wpas_p2p_stop_find(wpa_s);
4817 wpa_s->p2p_join_scan_count = 0;
4818 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4823 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4824 const u8 *ssid, size_t ssid_len)
4826 struct wpa_supplicant *group;
4827 struct p2p_go_neg_results res;
4828 struct wpa_bss *bss;
4830 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4833 if (group != wpa_s) {
4834 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4835 sizeof(group->p2p_pin));
4836 group->p2p_wps_method = wpa_s->p2p_wps_method;
4839 * Need to mark the current interface for p2p_group_formation
4840 * when a separate group interface is not used. This is needed
4841 * to allow p2p_cancel stop a pending p2p_connect-join.
4842 * wpas_p2p_init_group_interface() addresses this for the case
4843 * where a separate group interface is used.
4845 wpa_s->global->p2p_group_formation = wpa_s;
4848 group->p2p_in_provisioning = 1;
4849 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4851 os_memset(&res, 0, sizeof(res));
4852 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4853 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4855 res.wps_method = wpa_s->pending_join_wps_method;
4856 if (freq && ssid && ssid_len) {
4858 res.ssid_len = ssid_len;
4859 os_memcpy(res.ssid, ssid, ssid_len);
4861 bss = wpa_bss_get_bssid_latest(wpa_s,
4862 wpa_s->pending_join_iface_addr);
4864 res.freq = bss->freq;
4865 res.ssid_len = bss->ssid_len;
4866 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4867 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4869 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4873 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4874 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4876 wpa_drv_cancel_remain_on_channel(wpa_s);
4877 wpa_s->off_channel_freq = 0;
4878 wpa_s->roc_waiting_drv_freq = 0;
4880 wpas_start_wps_enrollee(group, &res);
4883 * Allow a longer timeout for join-a-running-group than normal 15
4884 * second group formation timeout since the GO may not have authorized
4885 * our connection yet.
4887 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4888 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4895 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4896 int *force_freq, int *pref_freq, int go,
4897 unsigned int *pref_freq_list,
4898 unsigned int *num_pref_freq)
4900 struct wpa_used_freq_data *freqs;
4901 int res, best_freq, num_unused;
4902 unsigned int freq_in_use = 0, num, i, max_pref_freq;
4904 max_pref_freq = *num_pref_freq;
4907 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4908 sizeof(struct wpa_used_freq_data));
4912 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4913 wpa_s->num_multichan_concurrent);
4916 * It is possible that the total number of used frequencies is bigger
4917 * than the number of frequencies used for P2P, so get the system wide
4918 * number of unused frequencies.
4920 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4922 wpa_printf(MSG_DEBUG,
4923 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4924 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4929 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4931 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4933 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
4934 ieee80211_is_dfs(freq)) {
4936 * If freq is a DFS channel and DFS is offloaded
4937 * to the driver, allow P2P GO to use it.
4939 wpa_printf(MSG_DEBUG,
4940 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
4943 wpa_printf(MSG_DEBUG,
4944 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
4951 for (i = 0; i < num; i++) {
4952 if (freqs[i].freq == freq)
4956 if (num_unused <= 0 && !freq_in_use) {
4957 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4962 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4963 "requested channel (%u MHz)", freq);
4968 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4970 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
4971 enum wpa_driver_if_type iface_type;
4974 iface_type = WPA_IF_P2P_GO;
4976 iface_type = WPA_IF_P2P_CLIENT;
4978 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
4981 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
4984 if (!res && max_pref_freq > 0) {
4985 *num_pref_freq = max_pref_freq;
4987 while (wpas_p2p_disallowed_freq(wpa_s->global,
4988 pref_freq_list[i]) &&
4989 i < *num_pref_freq) {
4990 wpa_printf(MSG_DEBUG,
4991 "P2P: preferred_freq_list[%d]=%d is disallowed",
4992 i, pref_freq_list[i]);
4995 if (i != *num_pref_freq) {
4996 best_freq = pref_freq_list[i];
4997 wpa_printf(MSG_DEBUG,
4998 "P2P: Using preferred_freq_list[%d]=%d",
5001 wpa_printf(MSG_DEBUG,
5002 "P2P: All driver preferred frequencies are disallowed for P2P use");
5006 wpa_printf(MSG_DEBUG,
5007 "P2P: No preferred frequency list available");
5011 /* We have a candidate frequency to use */
5012 if (best_freq > 0) {
5013 if (*pref_freq == 0 && num_unused > 0) {
5014 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
5016 *pref_freq = best_freq;
5018 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
5020 *force_freq = best_freq;
5022 } else if (num_unused > 0) {
5023 wpa_printf(MSG_DEBUG,
5024 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5027 wpa_printf(MSG_DEBUG,
5028 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5042 * wpas_p2p_connect - Request P2P Group Formation to be started
5043 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5044 * @peer_addr: Address of the peer P2P Device
5045 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5046 * @persistent_group: Whether to create a persistent group
5047 * @auto_join: Whether to select join vs. GO Negotiation automatically
5048 * @join: Whether to join an existing group (as a client) instead of starting
5049 * Group Owner negotiation; @peer_addr is BSSID in that case
5050 * @auth: Whether to only authorize the connection instead of doing that and
5051 * initiating Group Owner negotiation
5052 * @go_intent: GO Intent or -1 to use default
5053 * @freq: Frequency for the group or 0 for auto-selection
5054 * @persistent_id: Persistent group credentials to use for forcing GO
5055 * parameters or -1 to generate new values (SSID/passphrase)
5056 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5057 * interoperability workaround when initiating group formation
5058 * @ht40: Start GO with 40 MHz channel width
5059 * @vht: Start GO with VHT support
5060 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5061 * failure, -2 on failure due to channel not currently available,
5062 * -3 if forced channel is not supported
5064 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5065 const char *pin, enum p2p_wps_method wps_method,
5066 int persistent_group, int auto_join, int join, int auth,
5067 int go_intent, int freq, int persistent_id, int pd,
5070 int force_freq = 0, pref_freq = 0;
5072 enum wpa_driver_if_type iftype;
5074 struct wpa_ssid *ssid = NULL;
5075 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
5077 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5080 if (persistent_id >= 0) {
5081 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5082 if (ssid == NULL || ssid->disabled != 2 ||
5083 ssid->mode != WPAS_MODE_P2P_GO)
5087 os_free(wpa_s->global->add_psk);
5088 wpa_s->global->add_psk = NULL;
5090 wpa_s->global->p2p_fail_on_wps_complete = 0;
5091 wpa_s->global->pending_p2ps_group = 0;
5092 wpa_s->p2ps_method_config_any = 0;
5095 go_intent = wpa_s->conf->p2p_go_intent;
5098 wpa_s->p2p_long_listen = 0;
5100 wpa_s->p2p_wps_method = wps_method;
5101 wpa_s->p2p_persistent_group = !!persistent_group;
5102 wpa_s->p2p_persistent_id = persistent_id;
5103 wpa_s->p2p_go_intent = go_intent;
5104 wpa_s->p2p_connect_freq = freq;
5105 wpa_s->p2p_fallback_to_go_neg = 0;
5106 wpa_s->p2p_pd_before_go_neg = !!pd;
5107 wpa_s->p2p_go_ht40 = !!ht40;
5108 wpa_s->p2p_go_vht = !!vht;
5111 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5112 else if (wps_method == WPS_PIN_DISPLAY) {
5113 ret = wps_generate_pin();
5114 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5116 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5117 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5118 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5121 wpa_s->p2p_pin[0] = '\0';
5123 if (join || auto_join) {
5124 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5126 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5127 "connect a running group from " MACSTR,
5128 MAC2STR(peer_addr));
5129 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5132 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5133 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5135 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5136 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5140 os_get_reltime(&wpa_s->p2p_auto_started);
5141 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5143 wpa_s->p2p_auto_started.sec,
5144 wpa_s->p2p_auto_started.usec);
5146 wpa_s->user_initiated_pd = 1;
5147 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5148 auto_join, freq, NULL, 0) < 0)
5153 size = P2P_MAX_PREF_CHANNELS;
5154 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5155 go_intent == 15, pref_freq_list, &size);
5158 wpas_p2p_set_own_freq_preference(wpa_s,
5159 force_freq ? force_freq : pref_freq);
5161 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5163 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5165 if (wpa_s->create_p2p_iface) {
5166 /* Prepare to add a new interface for the group */
5167 iftype = WPA_IF_P2P_GROUP;
5168 if (go_intent == 15)
5169 iftype = WPA_IF_P2P_GO;
5170 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5171 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5172 "interface for the group");
5176 if_addr = wpa_s->pending_interface_addr;
5178 if_addr = wpa_s->own_addr;
5179 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5183 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5185 force_freq, persistent_group, ssid,
5191 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5192 go_intent, if_addr, force_freq,
5193 persistent_group, ssid, pref_freq) < 0) {
5194 if (wpa_s->create_p2p_iface)
5195 wpas_p2p_remove_pending_group_interface(wpa_s);
5203 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5204 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5205 * @freq: Frequency of the channel in MHz
5206 * @duration: Duration of the stay on the channel in milliseconds
5208 * This callback is called when the driver indicates that it has started the
5209 * requested remain-on-channel duration.
5211 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5212 unsigned int freq, unsigned int duration)
5214 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5216 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)",
5217 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5218 wpa_s->roc_waiting_drv_freq, freq, duration);
5219 if (wpa_s->off_channel_freq &&
5220 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5221 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5222 wpa_s->pending_listen_duration);
5223 wpa_s->pending_listen_freq = 0;
5225 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5226 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5232 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5234 /* Limit maximum Listen state time based on driver limitation. */
5235 if (timeout > wpa_s->max_remain_on_chan)
5236 timeout = wpa_s->max_remain_on_chan;
5238 return p2p_listen(wpa_s->global->p2p, timeout);
5243 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5244 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5245 * @freq: Frequency of the channel in MHz
5247 * This callback is called when the driver indicates that a remain-on-channel
5248 * operation has been completed, i.e., the duration on the requested channel
5251 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5254 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5255 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5256 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5257 wpas_p2p_listen_work_done(wpa_s);
5258 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5260 if (wpa_s->p2p_long_listen > 0)
5261 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5262 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5263 return; /* P2P module started a new operation */
5264 if (offchannel_pending_action_tx(wpa_s))
5266 if (wpa_s->p2p_long_listen > 0) {
5267 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5268 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5271 * When listen duration is over, stop listen & update p2p_state
5274 p2p_stop_listen(wpa_s->global->p2p);
5280 * wpas_p2p_group_remove - Remove a P2P group
5281 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5282 * @ifname: Network interface name of the group interface or "*" to remove all
5284 * Returns: 0 on success, -1 on failure
5286 * This function is used to remove a P2P group. This can be used to disconnect
5287 * from a group in which the local end is a P2P Client or to end a P2P Group in
5288 * case the local end is the Group Owner. If a virtual network interface was
5289 * created for this group, that interface will be removed. Otherwise, only the
5290 * configured P2P group network will be removed from the interface.
5292 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5294 struct wpa_global *global = wpa_s->global;
5295 struct wpa_supplicant *calling_wpa_s = wpa_s;
5297 if (os_strcmp(ifname, "*") == 0) {
5298 struct wpa_supplicant *prev;
5299 wpa_s = global->ifaces;
5302 wpa_s = wpa_s->next;
5303 if (prev->p2p_group_interface !=
5304 NOT_P2P_GROUP_INTERFACE ||
5305 (prev->current_ssid &&
5306 prev->current_ssid->p2p_group))
5307 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5312 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5313 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5317 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5321 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5325 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5326 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5327 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5330 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5331 &size, pref_freq_list);
5332 if (!res && size > 0) {
5334 while (wpas_p2p_disallowed_freq(wpa_s->global,
5335 pref_freq_list[i]) &&
5337 wpa_printf(MSG_DEBUG,
5338 "P2P: preferred_freq_list[%d]=%d is disallowed",
5339 i, pref_freq_list[i]);
5343 freq = pref_freq_list[i];
5344 wpa_printf(MSG_DEBUG,
5345 "P2P: Using preferred_freq_list[%d]=%d",
5348 wpa_printf(MSG_DEBUG,
5349 "P2P: All driver preferred frequencies are disallowed for P2P use");
5352 wpa_printf(MSG_DEBUG,
5353 "P2P: No preferred frequency list available");
5358 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5360 if (wpa_s->best_24_freq > 0 &&
5361 p2p_supported_freq_go(wpa_s->global->p2p,
5362 wpa_s->best_24_freq)) {
5363 freq = wpa_s->best_24_freq;
5364 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5365 "channel: %d MHz", freq);
5367 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5369 freq = 2412 + (r % 3) * 25;
5370 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5371 "channel: %d MHz", freq);
5376 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5378 if (wpa_s->best_5_freq > 0 &&
5379 p2p_supported_freq_go(wpa_s->global->p2p,
5380 wpa_s->best_5_freq)) {
5381 freq = wpa_s->best_5_freq;
5382 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5383 "channel: %d MHz", freq);
5385 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5387 freq = 5180 + (r % 4) * 20;
5388 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5389 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5390 "5 GHz channel for P2P group");
5393 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5394 "channel: %d MHz", freq);
5398 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5399 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5400 ieee80211_is_dfs(freq)) {
5402 * If freq is a DFS channel and DFS is offloaded to the
5403 * driver, allow P2P GO to use it.
5405 wpa_printf(MSG_DEBUG, "P2P: "
5406 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5410 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5411 "(%u MHz) is not supported for P2P uses",
5420 static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5421 const struct p2p_channels *channels,
5424 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5425 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5426 freq_included(wpa_s, channels, freq))
5432 static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5433 struct p2p_go_neg_results *params,
5434 const struct p2p_channels *channels)
5438 /* first try some random selection of the social channels */
5439 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5442 for (i = 0; i < 3; i++) {
5443 params->freq = 2412 + ((r + i) % 3) * 25;
5444 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5448 /* try all other channels in operating class 81 */
5449 for (i = 0; i < 11; i++) {
5450 params->freq = 2412 + i * 5;
5452 /* skip social channels; covered in the previous loop */
5453 if (params->freq == 2412 ||
5454 params->freq == 2437 ||
5455 params->freq == 2462)
5458 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5462 /* try all channels in operating class 115 */
5463 for (i = 0; i < 4; i++) {
5464 params->freq = 5180 + i * 20;
5465 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5466 freq_included(wpa_s, channels, params->freq) &&
5467 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5471 /* try all channels in operating class 124 */
5472 for (i = 0; i < 4; i++) {
5473 params->freq = 5745 + i * 20;
5474 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5475 freq_included(wpa_s, channels, params->freq) &&
5476 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5480 /* try social channel class 180 channel 2 */
5481 params->freq = 58320 + 1 * 2160;
5482 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5483 freq_included(wpa_s, channels, params->freq) &&
5484 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5487 /* try all channels in reg. class 180 */
5488 for (i = 0; i < 4; i++) {
5489 params->freq = 58320 + i * 2160;
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))
5497 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
5500 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5505 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5506 struct p2p_go_neg_results *params,
5507 int freq, int ht40, int vht,
5508 const struct p2p_channels *channels)
5510 struct wpa_used_freq_data *freqs;
5512 unsigned int num, i;
5514 os_memset(params, 0, sizeof(*params));
5515 params->role_go = 1;
5516 params->ht40 = ht40;
5519 if (wpa_s->p2p_group_common_freqs_num)
5520 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5523 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5524 sizeof(struct wpa_used_freq_data));
5528 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5529 wpa_s->num_multichan_concurrent);
5531 /* try using the forced freq */
5533 if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
5534 wpa_printf(MSG_DEBUG,
5535 "P2P: Forced GO freq %d MHz not accepted",
5540 for (i = 0; i < num; i++) {
5541 if (freqs[i].freq == freq) {
5542 wpa_printf(MSG_DEBUG,
5543 "P2P: forced freq (%d MHz) is also shared",
5545 params->freq = freq;
5550 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5551 wpa_printf(MSG_DEBUG,
5552 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5557 wpa_printf(MSG_DEBUG,
5558 "P2P: force GO freq (%d MHz) on a free channel",
5560 params->freq = freq;
5564 /* consider using one of the shared frequencies */
5566 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5567 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5568 wpa_printf(MSG_DEBUG,
5569 "P2P: Use shared freq (%d MHz) for GO",
5571 params->freq = cand;
5575 /* try using one of the shared freqs */
5576 for (i = 0; i < num; i++) {
5577 if (wpas_p2p_supported_freq_go(wpa_s, channels,
5579 wpa_printf(MSG_DEBUG,
5580 "P2P: Use shared freq (%d MHz) for GO",
5582 params->freq = freqs[i].freq;
5588 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5589 wpa_printf(MSG_DEBUG,
5590 "P2P: Cannot force GO on any of the channels we are already using");
5594 /* try using the setting from the configuration file */
5595 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5596 wpa_s->conf->p2p_oper_channel >= 1 &&
5597 wpa_s->conf->p2p_oper_channel <= 11 &&
5598 wpas_p2p_supported_freq_go(
5600 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5601 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5602 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5603 "frequency %d MHz", params->freq);
5607 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5608 wpa_s->conf->p2p_oper_reg_class == 116 ||
5609 wpa_s->conf->p2p_oper_reg_class == 117 ||
5610 wpa_s->conf->p2p_oper_reg_class == 124 ||
5611 wpa_s->conf->p2p_oper_reg_class == 125 ||
5612 wpa_s->conf->p2p_oper_reg_class == 126 ||
5613 wpa_s->conf->p2p_oper_reg_class == 127) &&
5614 wpas_p2p_supported_freq_go(wpa_s, channels,
5616 5 * wpa_s->conf->p2p_oper_channel)) {
5617 params->freq = 5000 + 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 /* Try using best channels */
5624 if (wpa_s->conf->p2p_oper_channel == 0 &&
5625 wpa_s->best_overall_freq > 0 &&
5626 wpas_p2p_supported_freq_go(wpa_s, channels,
5627 wpa_s->best_overall_freq)) {
5628 params->freq = wpa_s->best_overall_freq;
5629 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5630 "channel %d MHz", params->freq);
5634 if (wpa_s->conf->p2p_oper_channel == 0 &&
5635 wpa_s->best_24_freq > 0 &&
5636 wpas_p2p_supported_freq_go(wpa_s, channels,
5637 wpa_s->best_24_freq)) {
5638 params->freq = wpa_s->best_24_freq;
5639 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5640 "channel %d MHz", params->freq);
5644 if (wpa_s->conf->p2p_oper_channel == 0 &&
5645 wpa_s->best_5_freq > 0 &&
5646 wpas_p2p_supported_freq_go(wpa_s, channels,
5647 wpa_s->best_5_freq)) {
5648 params->freq = wpa_s->best_5_freq;
5649 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5650 "channel %d MHz", params->freq);
5654 /* try using preferred channels */
5655 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
5656 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5657 params->freq = cand;
5658 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5659 "channels", params->freq);
5663 /* Try using one of the group common freqs */
5664 if (wpa_s->p2p_group_common_freqs) {
5665 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
5666 cand = wpa_s->p2p_group_common_freqs[i];
5667 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5668 params->freq = cand;
5669 wpa_printf(MSG_DEBUG,
5670 "P2P: Use freq %d MHz common with the peer",
5677 /* no preference, select some channel */
5678 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
5680 if (params->freq == 0) {
5681 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
5694 static struct wpa_supplicant *
5695 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5698 struct wpa_supplicant *group_wpa_s;
5700 if (!wpas_p2p_create_iface(wpa_s)) {
5701 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5703 wpa_s->p2p_first_connection_timeout = 0;
5707 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5708 WPA_IF_P2P_CLIENT) < 0) {
5709 wpa_msg_global(wpa_s, MSG_ERROR,
5710 "P2P: Failed to add group interface");
5713 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5714 if (group_wpa_s == NULL) {
5715 wpa_msg_global(wpa_s, MSG_ERROR,
5716 "P2P: Failed to initialize group interface");
5717 wpas_p2p_remove_pending_group_interface(wpa_s);
5721 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5722 group_wpa_s->ifname);
5723 group_wpa_s->p2p_first_connection_timeout = 0;
5729 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5730 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5731 * @persistent_group: Whether to create a persistent group
5732 * @freq: Frequency for the group or 0 to indicate no hardcoding
5733 * @ht40: Start GO with 40 MHz channel width
5734 * @vht: Start GO with VHT support
5735 * Returns: 0 on success, -1 on failure
5737 * This function creates a new P2P group with the local end as the Group Owner,
5738 * i.e., without using Group Owner Negotiation.
5740 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5741 int freq, int ht40, int vht)
5743 struct p2p_go_neg_results params;
5745 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5748 os_free(wpa_s->global->add_psk);
5749 wpa_s->global->add_psk = NULL;
5751 /* Make sure we are not running find during connection establishment */
5752 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5753 wpas_p2p_stop_find_oper(wpa_s);
5755 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5759 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5762 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5763 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5764 ieee80211_is_dfs(params.freq)) {
5766 * If freq is a DFS channel and DFS is offloaded to the
5767 * driver, allow P2P GO to use it.
5769 wpa_printf(MSG_DEBUG,
5770 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5771 __func__, params.freq);
5773 wpa_printf(MSG_DEBUG,
5774 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5779 p2p_go_params(wpa_s->global->p2p, ¶ms);
5780 params.persistent_group = persistent_group;
5782 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5785 wpas_start_wps_go(wpa_s, ¶ms, 0);
5791 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5792 struct wpa_ssid *params, int addr_allocated,
5795 struct wpa_ssid *ssid;
5797 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5800 wpa_s->p2p_last_4way_hs_fail = NULL;
5802 wpa_supplicant_ap_deinit(wpa_s);
5804 ssid = wpa_config_add_network(wpa_s->conf);
5807 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5808 wpa_config_set_network_defaults(ssid);
5809 ssid->temporary = 1;
5810 ssid->proto = WPA_PROTO_RSN;
5811 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5812 ssid->group_cipher = WPA_CIPHER_CCMP;
5813 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5814 ssid->ssid = os_malloc(params->ssid_len);
5815 if (ssid->ssid == NULL) {
5816 wpa_config_remove_network(wpa_s->conf, ssid->id);
5819 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5820 ssid->ssid_len = params->ssid_len;
5821 ssid->p2p_group = 1;
5822 ssid->export_keys = 1;
5823 if (params->psk_set) {
5824 os_memcpy(ssid->psk, params->psk, 32);
5827 if (params->passphrase)
5828 ssid->passphrase = os_strdup(params->passphrase);
5830 wpa_s->show_group_started = 1;
5831 wpa_s->p2p_in_invitation = 1;
5832 wpa_s->p2p_invite_go_freq = freq;
5834 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5836 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5837 wpas_p2p_group_formation_timeout,
5838 wpa_s->parent, NULL);
5839 wpa_supplicant_select_network(wpa_s, ssid);
5845 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5846 struct wpa_ssid *ssid, int addr_allocated,
5847 int force_freq, int neg_freq, int ht40,
5848 int vht, const struct p2p_channels *channels,
5849 int connection_timeout)
5851 struct p2p_go_neg_results params;
5854 if (ssid->disabled != 2 || ssid->ssid == NULL)
5857 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5858 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5859 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5862 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5863 wpa_s->parent, NULL)) {
5865 * This can happen if Invitation Response frame was lost
5866 * and the peer (GO of a persistent group) tries to
5867 * invite us again. Reschedule the timeout to avoid
5868 * terminating the wait for the connection too early
5869 * since we now know that the peer is still trying to
5870 * invite us instead of having already started the GO.
5872 wpa_printf(MSG_DEBUG,
5873 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
5874 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5875 wpas_p2p_group_formation_timeout,
5876 wpa_s->parent, NULL);
5881 os_free(wpa_s->global->add_psk);
5882 wpa_s->global->add_psk = NULL;
5884 /* Make sure we are not running find during connection establishment */
5885 wpas_p2p_stop_find_oper(wpa_s);
5887 wpa_s->p2p_fallback_to_go_neg = 0;
5889 if (ssid->mode == WPAS_MODE_P2P_GO) {
5890 if (force_freq > 0) {
5891 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5895 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5897 (freq > 0 && !freq_included(wpa_s, channels, freq)))
5900 } else if (ssid->mode == WPAS_MODE_INFRA) {
5902 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
5903 struct os_reltime now;
5904 struct wpa_bss *bss =
5905 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
5907 os_get_reltime(&now);
5909 !os_reltime_expired(&now, &bss->last_update, 5) &&
5910 freq_included(wpa_s, channels, bss->freq))
5916 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5921 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5925 params.psk_set = ssid->psk_set;
5927 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5928 if (ssid->passphrase) {
5929 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5930 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5931 "persistent group");
5934 os_strlcpy(params.passphrase, ssid->passphrase,
5935 sizeof(params.passphrase));
5937 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5938 params.ssid_len = ssid->ssid_len;
5939 params.persistent_group = 1;
5941 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5945 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5947 wpa_s->p2p_first_connection_timeout = connection_timeout;
5948 wpas_start_wps_go(wpa_s, ¶ms, 0);
5954 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5955 struct wpabuf *proberesp_ies)
5957 struct wpa_supplicant *wpa_s = ctx;
5958 if (wpa_s->ap_iface) {
5959 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5960 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5961 wpabuf_free(beacon_ies);
5962 wpabuf_free(proberesp_ies);
5966 wpabuf_free(hapd->p2p_beacon_ie);
5967 hapd->p2p_beacon_ie = beacon_ies;
5969 wpabuf_free(hapd->p2p_probe_resp_ie);
5970 hapd->p2p_probe_resp_ie = proberesp_ies;
5972 wpabuf_free(beacon_ies);
5973 wpabuf_free(proberesp_ies);
5975 wpa_supplicant_ap_update_beacon(wpa_s);
5979 static void wpas_p2p_idle_update(void *ctx, int idle)
5981 struct wpa_supplicant *wpa_s = ctx;
5982 if (!wpa_s->ap_iface)
5984 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5986 if (wpa_s->global->p2p_fail_on_wps_complete &&
5987 wpa_s->p2p_in_provisioning) {
5988 wpas_p2p_grpform_fail_after_wps(wpa_s);
5991 wpas_p2p_set_group_idle_timeout(wpa_s);
5993 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5997 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5998 struct wpa_ssid *ssid)
6000 struct p2p_group *group;
6001 struct p2p_group_config *cfg;
6003 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6006 cfg = os_zalloc(sizeof(*cfg));
6010 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
6011 cfg->persistent_group = 2;
6012 else if (ssid->p2p_persistent_group)
6013 cfg->persistent_group = 1;
6014 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6015 if (wpa_s->max_stations &&
6016 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6017 cfg->max_clients = wpa_s->max_stations;
6019 cfg->max_clients = wpa_s->conf->max_num_sta;
6020 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6021 cfg->ssid_len = ssid->ssid_len;
6022 cfg->freq = ssid->frequency;
6023 cfg->cb_ctx = wpa_s;
6024 cfg->ie_update = wpas_p2p_ie_update;
6025 cfg->idle_update = wpas_p2p_idle_update;
6027 group = p2p_group_init(wpa_s->global->p2p, cfg);
6030 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
6031 p2p_group_notif_formation_done(group);
6032 wpa_s->p2p_group = group;
6037 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6040 struct wpa_ssid *ssid = wpa_s->current_ssid;
6042 if (!wpa_s->p2p_in_provisioning) {
6043 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6044 "provisioning not in progress");
6048 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6049 u8 go_dev_addr[ETH_ALEN];
6050 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6051 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6053 /* Clear any stored provisioning info */
6054 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6057 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6059 wpa_s->p2p_go_group_formation_completed = 1;
6060 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6062 * Use a separate timeout for initial data connection to
6063 * complete to allow the group to be removed automatically if
6064 * something goes wrong in this step before the P2P group idle
6065 * timeout mechanism is taken into use.
6067 wpa_dbg(wpa_s, MSG_DEBUG,
6068 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6069 P2P_MAX_INITIAL_CONN_WAIT);
6070 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6071 wpas_p2p_group_formation_timeout,
6072 wpa_s->parent, NULL);
6075 * Use a separate timeout for initial data connection to
6076 * complete to allow the group to be removed automatically if
6077 * the client does not complete data connection successfully.
6079 wpa_dbg(wpa_s, MSG_DEBUG,
6080 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6081 P2P_MAX_INITIAL_CONN_WAIT_GO);
6082 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6083 wpas_p2p_group_formation_timeout,
6084 wpa_s->parent, NULL);
6086 * Complete group formation on first successful data connection
6088 wpa_s->p2p_go_group_formation_completed = 0;
6090 if (wpa_s->global->p2p)
6091 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
6092 wpas_group_formation_completed(wpa_s, 1, 0);
6096 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6097 struct wps_event_fail *fail)
6099 if (!wpa_s->p2p_in_provisioning) {
6100 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6101 "provisioning not in progress");
6105 if (wpa_s->go_params) {
6106 p2p_clear_provisioning_info(
6108 wpa_s->go_params->peer_device_addr);
6111 wpas_notify_p2p_wps_failed(wpa_s, fail);
6113 if (wpa_s == wpa_s->global->p2p_group_formation) {
6115 * Allow some time for the failed WPS negotiation exchange to
6116 * complete, but remove the group since group formation cannot
6117 * succeed after provisioning failure.
6119 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6120 wpa_s->global->p2p_fail_on_wps_complete = 1;
6121 eloop_deplete_timeout(0, 50000,
6122 wpas_p2p_group_formation_timeout,
6123 wpa_s->parent, NULL);
6128 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6130 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6131 !wpa_s->p2p_in_provisioning)
6134 wpas_p2p_grpform_fail_after_wps(wpa_s);
6140 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6141 const char *config_method,
6142 enum wpas_p2p_prov_disc_use use,
6143 struct p2ps_provision *p2ps_prov)
6147 wpa_s->global->pending_p2ps_group = 0;
6148 wpa_s->p2p_fallback_to_go_neg = 0;
6149 wpa_s->pending_pd_use = NORMAL_PD;
6150 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6151 p2ps_prov->conncap = p2ps_group_capability(
6152 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
6153 wpa_printf(MSG_DEBUG,
6154 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6155 __func__, p2ps_prov->conncap,
6156 p2ps_prov->adv_id, p2ps_prov->conncap,
6157 p2ps_prov->status, p2ps_prov->info);
6160 } else if (os_strncmp(config_method, "display", 7) == 0)
6161 config_methods = WPS_CONFIG_DISPLAY;
6162 else if (os_strncmp(config_method, "keypad", 6) == 0)
6163 config_methods = WPS_CONFIG_KEYPAD;
6164 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6165 os_strncmp(config_method, "pushbutton", 10) == 0)
6166 config_methods = WPS_CONFIG_PUSHBUTTON;
6168 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
6173 if (use == WPAS_P2P_PD_AUTO) {
6174 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6175 wpa_s->pending_pd_config_methods = config_methods;
6176 wpa_s->p2p_auto_pd = 1;
6177 wpa_s->p2p_auto_join = 0;
6178 wpa_s->pending_pd_before_join = 0;
6179 wpa_s->auto_pd_scan_retry = 0;
6180 wpas_p2p_stop_find(wpa_s);
6181 wpa_s->p2p_join_scan_count = 0;
6182 os_get_reltime(&wpa_s->p2p_auto_started);
6183 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6184 wpa_s->p2p_auto_started.sec,
6185 wpa_s->p2p_auto_started.usec);
6186 wpas_p2p_join_scan(wpa_s, NULL);
6190 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6195 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6196 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6201 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6204 return p2p_scan_result_text(ies, ies_len, buf, end);
6208 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6210 if (!offchannel_pending_action_tx(wpa_s))
6213 wpas_p2p_action_tx_clear(wpa_s);
6215 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6216 "operation request");
6217 offchannel_clear_pending_action_tx(wpa_s);
6221 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6222 enum p2p_discovery_type type,
6223 unsigned int num_req_dev_types, const u8 *req_dev_types,
6224 const u8 *dev_id, unsigned int search_delay,
6225 u8 seek_cnt, const char **seek_string, int freq)
6227 wpas_p2p_clear_pending_action_tx(wpa_s);
6228 wpa_s->p2p_long_listen = 0;
6230 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6231 wpa_s->p2p_in_provisioning)
6234 wpa_supplicant_cancel_sched_scan(wpa_s);
6236 return p2p_find(wpa_s->global->p2p, timeout, type,
6237 num_req_dev_types, req_dev_types, dev_id,
6238 search_delay, seek_cnt, seek_string, freq);
6242 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6243 struct wpa_scan_results *scan_res)
6245 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6247 if (wpa_s->p2p_scan_work) {
6248 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6249 wpa_s->p2p_scan_work = NULL;
6250 radio_work_done(work);
6253 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6257 * Indicate that results have been processed so that the P2P module can
6258 * continue pending tasks.
6260 p2p_scan_res_handled(wpa_s->global->p2p);
6264 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
6266 wpas_p2p_clear_pending_action_tx(wpa_s);
6267 wpa_s->p2p_long_listen = 0;
6268 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6269 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6271 if (wpa_s->global->p2p)
6272 p2p_stop_find(wpa_s->global->p2p);
6274 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6275 wpa_printf(MSG_DEBUG,
6276 "P2P: Do not consider the scan results after stop_find");
6277 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6282 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6284 wpas_p2p_stop_find_oper(wpa_s);
6285 if (!wpa_s->global->pending_group_iface_for_p2ps)
6286 wpas_p2p_remove_pending_group_interface(wpa_s);
6290 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6292 struct wpa_supplicant *wpa_s = eloop_ctx;
6293 wpa_s->p2p_long_listen = 0;
6297 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6301 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6304 wpa_supplicant_cancel_sched_scan(wpa_s);
6305 wpas_p2p_clear_pending_action_tx(wpa_s);
6309 * This is a request for unlimited Listen state. However, at
6310 * least for now, this is mapped to a Listen state for one
6315 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6316 wpa_s->p2p_long_listen = 0;
6319 * Stop previous find/listen operation to avoid trying to request a new
6320 * remain-on-channel operation while the driver is still running the
6323 if (wpa_s->global->p2p)
6324 p2p_stop_find(wpa_s->global->p2p);
6326 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6327 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6328 wpa_s->p2p_long_listen = timeout * 1000;
6329 eloop_register_timeout(timeout, 0,
6330 wpas_p2p_long_listen_timeout,
6338 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6339 u8 *buf, size_t len, int p2p_group)
6341 struct wpabuf *p2p_ie;
6344 if (wpa_s->global->p2p_disabled)
6347 * Advertize mandatory cross connection capability even on
6348 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6350 if (wpa_s->conf->p2p_disabled && p2p_group)
6352 if (wpa_s->global->p2p == NULL)
6357 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6358 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6360 wpabuf_free(p2p_ie);
6366 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
6367 const u8 *dst, const u8 *bssid,
6368 const u8 *ie, size_t ie_len,
6369 unsigned int rx_freq, int ssi_signal)
6371 if (wpa_s->global->p2p_disabled)
6373 if (wpa_s->global->p2p == NULL)
6376 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6377 ie, ie_len, rx_freq)) {
6378 case P2P_PREQ_NOT_P2P:
6379 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6382 case P2P_PREQ_MALFORMED:
6383 case P2P_PREQ_NOT_LISTEN:
6384 case P2P_PREQ_NOT_PROCESSED:
6385 default: /* make gcc happy */
6387 case P2P_PREQ_PROCESSED:
6393 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6394 const u8 *sa, const u8 *bssid,
6395 u8 category, const u8 *data, size_t len, int freq)
6397 if (wpa_s->global->p2p_disabled)
6399 if (wpa_s->global->p2p == NULL)
6402 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6407 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6409 if (wpa_s->global->p2p_disabled)
6411 if (wpa_s->global->p2p == NULL)
6414 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6418 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6420 p2p_group_deinit(wpa_s->p2p_group);
6421 wpa_s->p2p_group = NULL;
6423 wpa_s->ap_configured_cb = NULL;
6424 wpa_s->ap_configured_cb_ctx = NULL;
6425 wpa_s->ap_configured_cb_data = NULL;
6426 wpa_s->connect_without_scan = NULL;
6430 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6432 wpa_s->p2p_long_listen = 0;
6434 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6437 return p2p_reject(wpa_s->global->p2p, addr);
6441 /* Invite to reinvoke a persistent group */
6442 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6443 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6444 int ht40, int vht, int pref_freq)
6446 enum p2p_invite_role role;
6450 int no_pref_freq_given = pref_freq == 0;
6451 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6453 wpa_s->global->p2p_invite_group = NULL;
6455 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6457 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6459 wpa_s->p2p_persistent_go_freq = freq;
6460 wpa_s->p2p_go_ht40 = !!ht40;
6461 if (ssid->mode == WPAS_MODE_P2P_GO) {
6462 role = P2P_INVITE_ROLE_GO;
6463 if (peer_addr == NULL) {
6464 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6465 "address in invitation command");
6468 if (wpas_p2p_create_iface(wpa_s)) {
6469 if (wpas_p2p_add_group_interface(wpa_s,
6470 WPA_IF_P2P_GO) < 0) {
6471 wpa_printf(MSG_ERROR, "P2P: Failed to "
6472 "allocate a new interface for the "
6476 bssid = wpa_s->pending_interface_addr;
6478 bssid = wpa_s->own_addr;
6480 role = P2P_INVITE_ROLE_CLIENT;
6481 peer_addr = ssid->bssid;
6483 wpa_s->pending_invite_ssid_id = ssid->id;
6485 size = P2P_MAX_PREF_CHANNELS;
6486 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6487 role == P2P_INVITE_ROLE_GO,
6488 pref_freq_list, &size);
6492 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6495 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6496 no_pref_freq_given && pref_freq > 0 &&
6497 wpa_s->num_multichan_concurrent > 1 &&
6498 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6499 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6505 * Stop any find/listen operations before invitation and possibly
6506 * connection establishment.
6508 wpas_p2p_stop_find_oper(wpa_s);
6510 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6511 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6516 /* Invite to join an active group */
6517 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6518 const u8 *peer_addr, const u8 *go_dev_addr)
6520 struct wpa_global *global = wpa_s->global;
6521 enum p2p_invite_role role;
6523 struct wpa_ssid *ssid;
6525 int freq = 0, force_freq = 0, pref_freq = 0;
6527 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6529 wpa_s->p2p_persistent_go_freq = 0;
6530 wpa_s->p2p_go_ht40 = 0;
6531 wpa_s->p2p_go_vht = 0;
6533 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6534 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6537 if (wpa_s == NULL) {
6538 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6542 ssid = wpa_s->current_ssid;
6544 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6549 wpa_s->global->p2p_invite_group = wpa_s;
6550 persistent = ssid->p2p_persistent_group &&
6551 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6552 ssid->ssid, ssid->ssid_len);
6554 if (ssid->mode == WPAS_MODE_P2P_GO) {
6555 role = P2P_INVITE_ROLE_ACTIVE_GO;
6556 bssid = wpa_s->own_addr;
6557 if (go_dev_addr == NULL)
6558 go_dev_addr = wpa_s->global->p2p_dev_addr;
6559 freq = ssid->frequency;
6561 role = P2P_INVITE_ROLE_CLIENT;
6562 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6563 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6564 "invite to current group");
6567 bssid = wpa_s->bssid;
6568 if (go_dev_addr == NULL &&
6569 !is_zero_ether_addr(wpa_s->go_dev_addr))
6570 go_dev_addr = wpa_s->go_dev_addr;
6571 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6572 (int) wpa_s->assoc_freq;
6574 wpa_s->parent->pending_invite_ssid_id = -1;
6576 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6579 size = P2P_MAX_PREF_CHANNELS;
6580 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6581 role == P2P_INVITE_ROLE_ACTIVE_GO,
6582 pref_freq_list, &size);
6585 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6587 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6588 ssid->ssid, ssid->ssid_len, force_freq,
6589 go_dev_addr, persistent, pref_freq, -1);
6593 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6595 struct wpa_ssid *ssid = wpa_s->current_ssid;
6596 u8 go_dev_addr[ETH_ALEN];
6597 int network_id = -1;
6603 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6604 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6605 wpa_s->parent, NULL);
6608 if (!wpa_s->show_group_started || !ssid)
6611 wpa_s->show_group_started = 0;
6613 os_memset(go_dev_addr, 0, ETH_ALEN);
6614 if (ssid->bssid_set)
6615 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6616 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6618 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6620 if (wpa_s->global->p2p_group_formation == wpa_s)
6621 wpa_s->global->p2p_group_formation = NULL;
6623 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6624 (int) wpa_s->assoc_freq;
6627 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6630 res = os_snprintf(ip_addr, sizeof(ip_addr),
6631 " ip_addr=%u.%u.%u.%u "
6632 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6633 ip[0], ip[1], ip[2], ip[3],
6634 ip[4], ip[5], ip[6], ip[7],
6635 ip[8], ip[9], ip[10], ip[11]);
6636 if (os_snprintf_error(sizeof(ip_addr), res))
6640 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6641 ssid->passphrase == NULL && ssid->psk_set ?
6643 ssid->passphrase, go_dev_addr, persistent,
6647 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6650 network_id = ssid->id;
6651 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6655 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6656 u32 interval1, u32 duration2, u32 interval2)
6660 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6663 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6664 wpa_s->current_ssid == NULL ||
6665 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6668 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6669 wpa_s->own_addr, wpa_s->assoc_freq,
6670 duration1, interval1, duration2, interval2);
6672 wpa_s->waiting_presence_resp = 1;
6678 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6679 unsigned int interval)
6681 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6684 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6688 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6690 if (wpa_s->current_ssid == NULL) {
6692 * current_ssid can be cleared when P2P client interface gets
6693 * disconnected, so assume this interface was used as P2P
6698 return wpa_s->current_ssid->p2p_group &&
6699 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6703 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6705 struct wpa_supplicant *wpa_s = eloop_ctx;
6707 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6708 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6713 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6715 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6719 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6723 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6724 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6726 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6729 timeout = wpa_s->conf->p2p_group_idle;
6730 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6731 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6732 timeout = P2P_MAX_CLIENT_IDLE;
6738 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6739 timeout = 0; /* special client mode no-timeout */
6744 if (wpa_s->p2p_in_provisioning) {
6746 * Use the normal group formation timeout during the
6747 * provisioning phase to avoid terminating this process too
6748 * early due to group idle timeout.
6750 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6751 "during provisioning");
6755 if (wpa_s->show_group_started) {
6757 * Use the normal group formation timeout between the end of
6758 * the provisioning phase and completion of 4-way handshake to
6759 * avoid terminating this process too early due to group idle
6762 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6763 "while waiting for initial 4-way handshake to "
6768 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6770 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6775 /* Returns 1 if the interface was removed */
6776 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6777 u16 reason_code, const u8 *ie, size_t ie_len,
6778 int locally_generated)
6780 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6783 if (!locally_generated)
6784 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6787 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6788 wpa_s->current_ssid &&
6789 wpa_s->current_ssid->p2p_group &&
6790 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6791 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6792 "session is ending");
6793 if (wpas_p2p_group_delete(wpa_s,
6794 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6803 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6804 u16 reason_code, const u8 *ie, size_t ie_len,
6805 int locally_generated)
6807 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6810 if (!locally_generated)
6811 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6816 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6818 struct p2p_data *p2p = wpa_s->global->p2p;
6823 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6826 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6827 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6829 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6830 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6833 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6834 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6836 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6837 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6839 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6840 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6841 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6842 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6843 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6846 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6847 p2p_set_sec_dev_types(p2p,
6848 (void *) wpa_s->conf->sec_device_type,
6849 wpa_s->conf->num_sec_device_types);
6851 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6853 p2p_remove_wps_vendor_extensions(p2p);
6854 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6855 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6857 p2p_add_wps_vendor_extension(
6858 p2p, wpa_s->conf->wps_vendor_ext[i]);
6862 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6863 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6865 country[0] = wpa_s->conf->country[0];
6866 country[1] = wpa_s->conf->country[1];
6868 p2p_set_country(p2p, country);
6871 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6872 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6873 wpa_s->conf->p2p_ssid_postfix ?
6874 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6878 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6879 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6881 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6882 u8 reg_class, channel;
6887 if (wpa_s->conf->p2p_listen_reg_class &&
6888 wpa_s->conf->p2p_listen_channel) {
6889 reg_class = wpa_s->conf->p2p_listen_reg_class;
6890 channel = wpa_s->conf->p2p_listen_channel;
6895 * Pick one of the social channels randomly as the
6898 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6901 channel = 1 + (r % 3) * 5;
6904 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6907 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6910 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6911 u8 op_reg_class, op_channel, cfg_op_channel;
6914 if (wpa_s->conf->p2p_oper_reg_class &&
6915 wpa_s->conf->p2p_oper_channel) {
6916 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6917 op_channel = wpa_s->conf->p2p_oper_channel;
6922 * Use random operation channel from (1, 6, 11)
6923 *if no other preference is indicated.
6925 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6928 op_channel = 1 + (r % 3) * 5;
6931 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6934 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6938 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6939 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6940 wpa_s->conf->p2p_pref_chan) < 0) {
6941 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6945 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6946 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6951 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6952 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6956 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6959 if (!wpa_s->ap_iface)
6961 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6966 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6968 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6971 wpa_s->global->cross_connection = enabled;
6972 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6975 struct wpa_supplicant *iface;
6977 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6979 if (iface->cross_connect_enabled == 0)
6982 iface->cross_connect_enabled = 0;
6983 iface->cross_connect_in_use = 0;
6984 wpa_msg_global(iface->parent, MSG_INFO,
6985 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6987 iface->cross_connect_uplink);
6995 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6997 struct wpa_supplicant *iface;
6999 if (!uplink->global->cross_connection)
7002 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7003 if (!iface->cross_connect_enabled)
7005 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7008 if (iface->ap_iface == NULL)
7010 if (iface->cross_connect_in_use)
7013 iface->cross_connect_in_use = 1;
7014 wpa_msg_global(iface->parent, MSG_INFO,
7015 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7016 iface->ifname, iface->cross_connect_uplink);
7021 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7023 struct wpa_supplicant *iface;
7025 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7026 if (!iface->cross_connect_enabled)
7028 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7031 if (!iface->cross_connect_in_use)
7034 wpa_msg_global(iface->parent, MSG_INFO,
7035 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7036 iface->ifname, iface->cross_connect_uplink);
7037 iface->cross_connect_in_use = 0;
7042 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7044 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7045 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7046 wpa_s->cross_connect_disallowed)
7047 wpas_p2p_disable_cross_connect(wpa_s);
7049 wpas_p2p_enable_cross_connect(wpa_s);
7050 if (!wpa_s->ap_iface &&
7051 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7052 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7056 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7058 wpas_p2p_disable_cross_connect(wpa_s);
7059 if (!wpa_s->ap_iface &&
7060 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7062 wpas_p2p_set_group_idle_timeout(wpa_s);
7066 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7068 struct wpa_supplicant *iface;
7070 if (!wpa_s->global->cross_connection)
7073 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7076 if (iface->drv_flags &
7077 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7079 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7080 iface != wpa_s->parent)
7083 wpa_s->cross_connect_enabled = 1;
7084 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7085 sizeof(wpa_s->cross_connect_uplink));
7086 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7087 "%s to %s whenever uplink is available",
7088 wpa_s->ifname, wpa_s->cross_connect_uplink);
7090 if (iface->ap_iface || iface->current_ssid == NULL ||
7091 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7092 iface->cross_connect_disallowed ||
7093 iface->wpa_state != WPA_COMPLETED)
7096 wpa_s->cross_connect_in_use = 1;
7097 wpa_msg_global(wpa_s->parent, MSG_INFO,
7098 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7099 wpa_s->ifname, wpa_s->cross_connect_uplink);
7105 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7107 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7108 !wpa_s->p2p_in_provisioning)
7109 return 0; /* not P2P client operation */
7111 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7113 if (wpa_s != wpa_s->parent)
7114 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
7115 wpas_p2p_group_formation_failed(wpa_s, 0);
7120 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7122 struct wpa_supplicant *wpa_s = eloop_ctx;
7123 wpas_p2p_notif_pbc_overlap(wpa_s);
7127 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7128 enum wpas_p2p_channel_update_trig trig)
7130 struct p2p_channels chan, cli_chan;
7131 struct wpa_used_freq_data *freqs = NULL;
7132 unsigned int num = wpa_s->num_multichan_concurrent;
7134 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7137 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7141 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7143 os_memset(&chan, 0, sizeof(chan));
7144 os_memset(&cli_chan, 0, sizeof(cli_chan));
7145 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
7146 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7151 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
7153 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7156 * The used frequencies map changed, so it is possible that a GO is
7157 * using a channel that is no longer valid for P2P use. It is also
7158 * possible that due to policy consideration, it would be preferable to
7159 * move it to a frequency already used by other station interfaces.
7161 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7167 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7168 struct wpa_scan_results *scan_res)
7170 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7174 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7176 struct wpa_global *global = wpa_s->global;
7180 if (global->p2p == NULL)
7183 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7185 if (wpa_s->pending_interface_name[0] &&
7186 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7189 peer = p2p_get_go_neg_peer(global->p2p);
7191 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7192 MACSTR, MAC2STR(peer));
7193 p2p_unauthorize(global->p2p, peer);
7197 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7198 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7199 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7203 if (wpa_s->pending_pd_before_join) {
7204 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7205 wpa_s->pending_pd_before_join = 0;
7209 wpas_p2p_stop_find(wpa_s);
7211 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7212 if (wpa_s == global->p2p_group_formation &&
7213 (wpa_s->p2p_in_provisioning ||
7214 wpa_s->parent->pending_interface_type ==
7215 WPA_IF_P2P_CLIENT)) {
7216 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7217 "formation found - cancelling",
7220 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7221 wpa_s->parent, NULL);
7222 if (wpa_s->p2p_in_provisioning) {
7223 wpas_group_formation_completed(wpa_s, 0, 0);
7226 wpas_p2p_group_delete(wpa_s,
7227 P2P_GROUP_REMOVAL_REQUESTED);
7229 } else if (wpa_s->p2p_in_invitation) {
7230 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7233 wpas_p2p_group_formation_failed(wpa_s, 0);
7238 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7246 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7248 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7251 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7252 "being available anymore");
7253 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
7257 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7258 int freq_24, int freq_5, int freq_overall)
7260 struct p2p_data *p2p = wpa_s->global->p2p;
7263 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7267 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7270 struct p2p_data *p2p = wpa_s->global->p2p;
7275 if (hwaddr_aton(addr, peer))
7278 return p2p_unauthorize(p2p, peer);
7283 * wpas_p2p_disconnect - Disconnect from a P2P Group
7284 * @wpa_s: Pointer to wpa_supplicant data
7285 * Returns: 0 on success, -1 on failure
7287 * This can be used to disconnect from a group in which the local end is a P2P
7288 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7289 * virtual network interface was created for this group, that interface will be
7290 * removed. Otherwise, only the configured P2P group network will be removed
7291 * from the interface.
7293 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7299 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7304 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7308 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7311 ret = p2p_in_progress(wpa_s->global->p2p);
7314 * Check whether there is an ongoing WPS provisioning step (or
7315 * other parts of group formation) on another interface since
7316 * p2p_in_progress() does not report this to avoid issues for
7317 * scans during such provisioning step.
7319 if (wpa_s->global->p2p_group_formation &&
7320 wpa_s->global->p2p_group_formation != wpa_s) {
7321 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7322 "in group formation",
7323 wpa_s->global->p2p_group_formation->ifname);
7328 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
7329 struct os_reltime now;
7330 os_get_reltime(&now);
7331 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7332 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
7333 /* Wait for the first client has expired */
7334 wpa_s->global->p2p_go_wait_client.sec = 0;
7336 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7345 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7346 struct wpa_ssid *ssid)
7348 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7349 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7350 wpa_s->parent, NULL) > 0) {
7352 * Remove the network by scheduling the group formation
7353 * timeout to happen immediately. The teardown code
7354 * needs to be scheduled to run asynch later so that we
7355 * don't delete data from under ourselves unexpectedly.
7356 * Calling wpas_p2p_group_formation_timeout directly
7357 * causes a series of crashes in WPS failure scenarios.
7359 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7360 "P2P group network getting removed");
7361 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7362 wpa_s->parent, NULL);
7367 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
7368 const u8 *addr, const u8 *ssid,
7374 for (s = wpa_s->conf->ssid; s; s = s->next) {
7375 if (s->disabled != 2)
7378 (ssid_len != s->ssid_len ||
7379 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7382 if (s->mode == WPAS_MODE_P2P_GO)
7386 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7387 return s; /* peer is GO in the persistent group */
7388 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7390 for (i = 0; i < s->num_p2p_clients; i++) {
7391 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
7392 addr, ETH_ALEN) == 0)
7393 return s; /* peer is P2P client in persistent
7402 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7405 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7406 wpa_s->parent, NULL) > 0) {
7408 * This can happen if WPS provisioning step is not terminated
7409 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7410 * peer was able to connect, there is no need to time out group
7411 * formation after this, though. In addition, this is used with
7412 * the initial connection wait on the GO as a separate formation
7413 * timeout and as such, expected to be hit after the initial WPS
7414 * provisioning step.
7416 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7418 if (!wpa_s->p2p_go_group_formation_completed &&
7419 !wpa_s->group_formation_reported) {
7421 * GO has not yet notified group formation success since
7422 * the WPS step was not completed cleanly. Do that
7423 * notification now since the P2P Client was able to
7424 * connect and as such, must have received the
7425 * credential from the WPS step.
7427 if (wpa_s->global->p2p)
7428 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7429 wpas_group_formation_completed(wpa_s, 1, 0);
7432 if (!wpa_s->p2p_go_group_formation_completed) {
7433 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7434 wpa_s->p2p_go_group_formation_completed = 1;
7435 wpa_s->global->p2p_group_formation = NULL;
7436 wpa_s->p2p_in_provisioning = 0;
7437 wpa_s->p2p_in_invitation = 0;
7439 wpa_s->global->p2p_go_wait_client.sec = 0;
7442 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7446 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7449 struct wpa_supplicant *group = wpa_s;
7452 if (wpa_s->global->p2p_group_formation)
7453 group = wpa_s->global->p2p_group_formation;
7454 wpa_s = wpa_s->global->p2p_init_wpa_s;
7455 offchannel_send_action_done(wpa_s);
7457 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7458 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7459 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7460 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7461 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7462 wpa_s->p2p_persistent_id,
7463 wpa_s->p2p_pd_before_go_neg,
7470 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7474 if (!wpa_s->p2p_fallback_to_go_neg ||
7475 wpa_s->p2p_in_provisioning <= 5)
7478 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7479 return 0; /* peer operating as a GO */
7481 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7482 "fallback to GO Negotiation");
7483 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7484 "reason=GO-not-found");
7485 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7487 return res == 1 ? 2 : 1;
7491 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7493 struct wpa_supplicant *ifs;
7495 if (wpa_s->wpa_state > WPA_SCANNING) {
7496 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7497 "concurrent operation",
7498 wpa_s->conf->p2p_search_delay);
7499 return wpa_s->conf->p2p_search_delay;
7502 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7504 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7505 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7506 "delay due to concurrent operation on "
7508 wpa_s->conf->p2p_search_delay,
7510 return wpa_s->conf->p2p_search_delay;
7518 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7519 struct wpa_ssid *s, const u8 *addr,
7522 struct psk_list_entry *psk, *tmp;
7525 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7527 if ((iface_addr && !psk->p2p &&
7528 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7529 (!iface_addr && psk->p2p &&
7530 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7531 wpa_dbg(wpa_s, MSG_DEBUG,
7532 "P2P: Remove persistent group PSK list entry for "
7534 MAC2STR(psk->addr), psk->p2p);
7535 dl_list_del(&psk->list);
7545 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7546 const u8 *p2p_dev_addr,
7547 const u8 *psk, size_t psk_len)
7549 struct wpa_ssid *ssid = wpa_s->current_ssid;
7550 struct wpa_ssid *persistent;
7551 struct psk_list_entry *p, *last;
7553 if (psk_len != sizeof(p->psk))
7557 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7558 " p2p_dev_addr=" MACSTR,
7559 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7560 if (is_zero_ether_addr(p2p_dev_addr))
7561 p2p_dev_addr = NULL;
7563 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7567 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7568 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7569 /* To be added to persistent group once created */
7570 if (wpa_s->global->add_psk == NULL) {
7571 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7572 if (wpa_s->global->add_psk == NULL)
7575 p = wpa_s->global->add_psk;
7578 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7581 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7583 os_memcpy(p->psk, psk, psk_len);
7587 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7588 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7592 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7595 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7599 p = os_zalloc(sizeof(*p));
7604 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7607 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7609 os_memcpy(p->psk, psk, psk_len);
7611 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7612 (last = dl_list_last(&persistent->psk_list,
7613 struct psk_list_entry, list))) {
7614 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7615 MACSTR " (p2p=%u) to make room for a new one",
7616 MAC2STR(last->addr), last->p2p);
7617 dl_list_del(&last->list);
7621 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7622 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7623 p2p_dev_addr == NULL);
7625 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7626 MACSTR, MAC2STR(p2p_dev_addr));
7628 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7631 dl_list_add(&persistent->psk_list, &p->list);
7633 if (wpa_s->parent->conf->update_config &&
7634 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7635 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7639 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7640 struct wpa_ssid *s, const u8 *addr,
7645 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7646 if (res > 0 && wpa_s->conf->update_config &&
7647 wpa_config_write(wpa_s->confname, wpa_s->conf))
7648 wpa_dbg(wpa_s, MSG_DEBUG,
7649 "P2P: Failed to update configuration");
7653 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7654 const u8 *peer, int iface_addr)
7656 struct hostapd_data *hapd;
7657 struct hostapd_wpa_psk *psk, *prev, *rem;
7658 struct sta_info *sta;
7660 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7661 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7664 /* Remove per-station PSK entry */
7665 hapd = wpa_s->ap_iface->bss[0];
7667 psk = hapd->conf->ssid.wpa_psk;
7669 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7671 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7672 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7673 MACSTR " iface_addr=%d",
7674 MAC2STR(peer), iface_addr);
7676 prev->next = psk->next;
7678 hapd->conf->ssid.wpa_psk = psk->next;
7688 /* Disconnect from group */
7690 sta = ap_get_sta(hapd, peer);
7692 sta = ap_get_sta_p2p(hapd, peer);
7694 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7695 " (iface_addr=%d) from group",
7696 MAC2STR(peer), iface_addr);
7697 hostapd_drv_sta_deauth(hapd, sta->addr,
7698 WLAN_REASON_DEAUTH_LEAVING);
7699 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7704 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7708 struct wpa_supplicant *w;
7709 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
7711 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7713 /* Remove from any persistent group */
7714 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
7715 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7718 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7719 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
7722 /* Remove from any operating group */
7723 for (w = wpa_s->global->ifaces; w; w = w->next)
7724 wpas_p2p_remove_client_go(w, peer, iface_addr);
7728 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7730 struct wpa_supplicant *wpa_s = eloop_ctx;
7731 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7735 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7737 struct wpa_supplicant *wpa_s = eloop_ctx;
7739 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7740 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7744 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7745 struct wpa_ssid *ssid)
7747 struct wpa_supplicant *iface;
7749 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7750 if (!iface->current_ssid ||
7751 iface->current_ssid->frequency == freq ||
7752 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7753 !iface->current_ssid->p2p_group))
7756 /* Remove the connection with least priority */
7757 if (!wpas_is_p2p_prioritized(iface)) {
7758 /* STA connection has priority over existing
7759 * P2P connection, so remove the interface. */
7760 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7761 eloop_register_timeout(0, 0,
7762 wpas_p2p_group_freq_conflict,
7764 /* If connection in progress is P2P connection, do not
7765 * proceed for the connection. */
7771 /* P2P connection has priority, disable the STA network
7773 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7775 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7776 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7777 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7779 /* If P2P connection is in progress, continue
7792 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7794 struct wpa_ssid *ssid = wpa_s->current_ssid;
7796 if (ssid == NULL || !ssid->p2p_group)
7799 if (wpa_s->p2p_last_4way_hs_fail &&
7800 wpa_s->p2p_last_4way_hs_fail == ssid) {
7801 u8 go_dev_addr[ETH_ALEN];
7802 struct wpa_ssid *persistent;
7804 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7806 ssid->ssid_len) <= 0) {
7807 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7811 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7812 MACSTR, MAC2STR(go_dev_addr));
7813 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7816 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7817 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7820 wpa_msg_global(wpa_s->parent, MSG_INFO,
7821 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7824 wpa_s->p2p_last_4way_hs_fail = NULL;
7826 * Remove the group from a timeout to avoid issues with caller
7827 * continuing to use the interface if this is on a P2P group
7830 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7835 wpa_s->p2p_last_4way_hs_fail = ssid;
7840 #ifdef CONFIG_WPS_NFC
7842 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7850 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7854 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7855 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7862 wpabuf_put_be16(ret, wsc_len);
7864 wpabuf_put_buf(ret, wsc);
7865 wpabuf_put_be16(ret, wpabuf_len(p2p));
7866 wpabuf_put_buf(ret, p2p);
7870 wpa_hexdump_buf(MSG_DEBUG,
7871 "P2P: Generated NFC connection handover message", ret);
7875 tmp = ndef_build_p2p(ret);
7878 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7888 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7889 struct wpa_ssid **ssid, u8 *go_dev_addr)
7891 struct wpa_supplicant *iface;
7894 os_memset(go_dev_addr, 0, ETH_ALEN);
7897 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7898 if (iface->wpa_state < WPA_ASSOCIATING ||
7899 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7900 !iface->current_ssid->p2p_group ||
7901 iface->current_ssid->mode != WPAS_MODE_INFRA)
7904 *ssid = iface->current_ssid;
7906 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7907 return iface->assoc_freq;
7913 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7916 struct wpabuf *wsc, *p2p;
7917 struct wpa_ssid *ssid;
7918 u8 go_dev_addr[ETH_ALEN];
7919 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7921 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7922 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7926 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7927 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7928 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7929 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7933 if (cli_freq == 0) {
7934 wsc = wps_build_nfc_handover_req_p2p(
7935 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7938 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7939 go_dev_addr, ssid ? ssid->ssid : NULL,
7940 ssid ? ssid->ssid_len : 0);
7942 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7946 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7949 struct wpabuf *wsc, *p2p;
7950 struct wpa_ssid *ssid;
7951 u8 go_dev_addr[ETH_ALEN];
7952 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7954 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7957 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7958 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7959 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7962 if (cli_freq == 0) {
7963 wsc = wps_build_nfc_handover_sel_p2p(
7965 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7966 DEV_PW_NFC_CONNECTION_HANDOVER,
7967 wpa_s->conf->wps_nfc_dh_pubkey,
7968 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7971 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7972 go_dev_addr, ssid ? ssid->ssid : NULL,
7973 ssid ? ssid->ssid_len : 0);
7975 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7979 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7980 struct p2p_nfc_params *params)
7982 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7983 "connection handover (freq=%d)",
7986 if (params->go_freq && params->go_ssid_len) {
7987 wpa_s->p2p_wps_method = WPS_NFC;
7988 wpa_s->pending_join_wps_method = WPS_NFC;
7989 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7990 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7992 return wpas_p2p_join_start(wpa_s, params->go_freq,
7994 params->go_ssid_len);
7997 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7998 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7999 params->go_freq, -1, 0, 1, 1);
8003 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8004 struct p2p_nfc_params *params, int tag)
8006 int res, persistent;
8007 struct wpa_ssid *ssid;
8009 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8010 "connection handover");
8011 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8012 ssid = wpa_s->current_ssid;
8015 if (ssid->mode != WPAS_MODE_P2P_GO)
8017 if (wpa_s->ap_iface == NULL)
8021 if (wpa_s == NULL) {
8022 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8026 if (wpa_s->parent->p2p_oob_dev_pw_id !=
8027 DEV_PW_NFC_CONNECTION_HANDOVER &&
8028 !wpa_s->parent->p2p_oob_dev_pw) {
8029 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8032 res = wpas_ap_wps_add_nfc_pw(
8033 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
8034 wpa_s->parent->p2p_oob_dev_pw,
8035 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
8036 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
8041 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8045 if (!params->peer ||
8046 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8049 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8050 " to join", MAC2STR(params->peer->p2p_device_addr));
8052 wpa_s->global->p2p_invite_group = wpa_s;
8053 persistent = ssid->p2p_persistent_group &&
8054 wpas_p2p_get_persistent(wpa_s->parent,
8055 params->peer->p2p_device_addr,
8056 ssid->ssid, ssid->ssid_len);
8057 wpa_s->parent->pending_invite_ssid_id = -1;
8059 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8060 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8061 ssid->ssid, ssid->ssid_len, ssid->frequency,
8062 wpa_s->global->p2p_dev_addr, persistent, 0,
8063 wpa_s->parent->p2p_oob_dev_pw_id);
8067 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8068 struct p2p_nfc_params *params,
8071 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8072 "connection handover");
8073 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8074 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8075 forced_freq, -1, 0, 1, 1);
8079 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8080 struct p2p_nfc_params *params,
8085 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8086 "connection handover");
8087 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8088 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8089 forced_freq, -1, 0, 1, 1);
8093 res = wpas_p2p_listen(wpa_s, 60);
8095 p2p_unauthorize(wpa_s->global->p2p,
8096 params->peer->p2p_device_addr);
8103 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8104 const struct wpabuf *data,
8105 int sel, int tag, int forced_freq)
8107 const u8 *pos, *end;
8109 struct p2p_nfc_params params;
8112 os_memset(¶ms, 0, sizeof(params));
8115 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8117 pos = wpabuf_head(data);
8118 end = pos + wpabuf_len(data);
8120 if (end - pos < 2) {
8121 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8125 len = WPA_GET_BE16(pos);
8127 if (len > end - pos) {
8128 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8132 params.wsc_attr = pos;
8133 params.wsc_len = len;
8136 if (end - pos < 2) {
8137 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8141 len = WPA_GET_BE16(pos);
8143 if (len > end - pos) {
8144 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8148 params.p2p_attr = pos;
8149 params.p2p_len = len;
8152 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8153 params.wsc_attr, params.wsc_len);
8154 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8155 params.p2p_attr, params.p2p_len);
8157 wpa_hexdump(MSG_DEBUG,
8158 "P2P: Ignored extra data after P2P attributes",
8162 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
8166 if (params.next_step == NO_ACTION)
8169 if (params.next_step == BOTH_GO) {
8170 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8171 MAC2STR(params.peer->p2p_device_addr));
8175 if (params.next_step == PEER_CLIENT) {
8176 if (!is_zero_ether_addr(params.go_dev_addr)) {
8177 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8178 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8180 MAC2STR(params.peer->p2p_device_addr),
8182 MAC2STR(params.go_dev_addr),
8183 wpa_ssid_txt(params.go_ssid,
8184 params.go_ssid_len));
8186 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8187 "peer=" MACSTR " freq=%d",
8188 MAC2STR(params.peer->p2p_device_addr),
8194 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8195 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8196 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8200 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8201 wpa_s->p2p_oob_dev_pw = NULL;
8203 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8204 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8209 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8210 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8211 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8212 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8213 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8214 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8215 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8219 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8220 "peer OOB Device Password Id %u", id);
8223 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8224 "Device Password Id %u", id);
8225 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8226 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8227 params.oob_dev_pw_len -
8228 WPS_OOB_PUBKEY_HASH_LEN - 2);
8229 wpa_s->p2p_oob_dev_pw_id = id;
8230 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8231 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8232 params.oob_dev_pw_len -
8233 WPS_OOB_PUBKEY_HASH_LEN - 2);
8234 if (wpa_s->p2p_oob_dev_pw == NULL)
8237 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8238 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8239 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8242 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8243 "without Device Password");
8244 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8247 switch (params.next_step) {
8251 /* already covered above */
8254 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
8256 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
8258 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
8260 /* TODO: use own OOB Dev Pw */
8261 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
8268 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8269 const struct wpabuf *data, int forced_freq)
8271 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8274 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8278 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8279 const struct wpabuf *req,
8280 const struct wpabuf *sel, int forced_freq)
8285 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8288 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8290 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8291 wpabuf_head(req), wpabuf_len(req));
8292 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8293 wpabuf_head(sel), wpabuf_len(sel));
8295 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8296 tmp = ndef_parse_p2p(init ? sel : req);
8298 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8302 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8310 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8313 int go_intent = wpa_s->conf->p2p_go_intent;
8314 struct wpa_supplicant *iface;
8316 if (wpa_s->global->p2p == NULL)
8320 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8321 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8323 if (!iface->ap_iface)
8325 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8327 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8329 if (wpa_s->p2p_nfc_tag_enabled)
8330 wpas_p2p_remove_pending_group_interface(wpa_s);
8331 wpa_s->p2p_nfc_tag_enabled = 0;
8335 if (wpa_s->global->p2p_disabled)
8338 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8339 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8340 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8341 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8342 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8343 "to allow static handover cases");
8347 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8349 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8350 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8351 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8352 if (wpa_s->p2p_oob_dev_pw == NULL)
8354 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8356 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8357 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8359 * P2P Group Interface present and the command came on group
8360 * interface, so enable the token for the current interface.
8362 wpa_s->create_p2p_iface = 0;
8364 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8367 if (wpa_s->create_p2p_iface) {
8368 enum wpa_driver_if_type iftype;
8369 /* Prepare to add a new interface for the group */
8370 iftype = WPA_IF_P2P_GROUP;
8371 if (go_intent == 15)
8372 iftype = WPA_IF_P2P_GO;
8373 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8374 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8375 "interface for the group");
8379 if_addr = wpa_s->pending_interface_addr;
8381 if_addr = wpa_s->own_addr;
8383 wpa_s->p2p_nfc_tag_enabled = enabled;
8385 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8386 struct hostapd_data *hapd;
8387 if (iface->ap_iface == NULL)
8389 hapd = iface->ap_iface->bss[0];
8390 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8391 hapd->conf->wps_nfc_dh_pubkey =
8392 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8393 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8394 hapd->conf->wps_nfc_dh_privkey =
8395 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8396 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8397 hapd->conf->wps_nfc_dev_pw =
8398 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8399 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8401 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8402 wpa_dbg(iface, MSG_DEBUG,
8403 "P2P: Failed to enable NFC Tag for GO");
8406 p2p_set_authorized_oob_dev_pw_id(
8407 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8413 #endif /* CONFIG_WPS_NFC */
8416 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8417 struct wpa_used_freq_data *freqs,
8420 u8 curr_chan, cand, chan;
8424 * If possible, optimize the Listen channel to be a channel that is
8425 * already used by one of the other interfaces.
8427 if (!wpa_s->conf->p2p_optimize_listen_chan)
8430 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8433 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8434 for (i = 0, cand = 0; i < num; i++) {
8435 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8436 if (curr_chan == chan) {
8441 if (chan == 1 || chan == 6 || chan == 11)
8446 wpa_dbg(wpa_s, MSG_DEBUG,
8447 "P2P: Update Listen channel to %u based on operating channel",
8449 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8454 static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
8456 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8457 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8461 /* TODO: Add CSA support */
8462 wpa_dbg(wpa_s, MSG_DEBUG, "Moving GO with CSA is not implemented");
8467 static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
8469 struct p2p_go_neg_results params;
8470 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8472 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
8474 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
8475 current_ssid->frequency);
8477 /* Stop the AP functionality */
8478 /* TODO: Should do this in a way that does not indicated to possible
8479 * P2P Clients in the group that the group is terminated. */
8480 wpa_supplicant_ap_deinit(wpa_s);
8482 /* Reselect the GO frequency */
8483 if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, NULL)) {
8484 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
8485 wpas_p2p_group_delete(wpa_s,
8486 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8489 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
8493 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
8494 wpa_printf(MSG_DEBUG,
8495 "P2P: Selected freq (%u MHz) is not valid for P2P",
8497 wpas_p2p_group_delete(wpa_s,
8498 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8502 /* Update the frequency */
8503 current_ssid->frequency = params.freq;
8504 wpa_s->connect_without_scan = current_ssid;
8505 wpa_s->reassociate = 1;
8506 wpa_s->disconnected = 0;
8507 wpa_supplicant_req_scan(wpa_s, 0, 0);
8511 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
8513 struct wpa_supplicant *wpa_s = eloop_ctx;
8515 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
8518 wpas_p2p_go_update_common_freqs(wpa_s);
8521 * First, try a channel switch flow. If it is not supported or fails,
8522 * take down the GO and bring it up again.
8524 if (wpas_p2p_move_go_csa(wpa_s) < 0)
8525 wpas_p2p_move_go_no_csa(wpa_s);
8529 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
8531 struct wpa_supplicant *wpa_s = eloop_ctx;
8532 struct wpa_used_freq_data *freqs = NULL;
8533 unsigned int num = wpa_s->num_multichan_concurrent;
8535 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8539 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8541 /* Previous attempt to move a GO was not possible -- try again. */
8542 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
8543 WPAS_P2P_CHANNEL_UPDATE_ANY);
8550 * Consider moving a GO from its currently used frequency:
8551 * 1. It is possible that due to regulatory consideration the frequency
8552 * can no longer be used and there is a need to evacuate the GO.
8553 * 2. It is possible that due to MCC considerations, it would be preferable
8554 * to move the GO to a channel that is currently used by some other
8555 * station interface.
8557 * In case a frequency that became invalid is once again valid, cancel a
8558 * previously initiated GO frequency change.
8560 static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
8561 struct wpa_used_freq_data *freqs,
8564 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
8565 unsigned int timeout;
8568 wpas_p2p_go_update_common_freqs(wpa_s);
8570 freq = wpa_s->current_ssid->frequency;
8571 for (i = 0, invalid_freq = 0; i < num; i++) {
8572 if (freqs[i].freq == freq) {
8573 flags = freqs[i].flags;
8575 /* The channel is invalid, must change it */
8576 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
8577 wpa_dbg(wpa_s, MSG_DEBUG,
8578 "P2P: Freq=%d MHz no longer valid for GO",
8582 } else if (freqs[i].flags == 0) {
8583 /* Freq is not used by any other station interface */
8585 } else if (!p2p_supported_freq(wpa_s->global->p2p,
8587 /* Freq is not valid for P2P use cases */
8589 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8590 P2P_GO_FREQ_MOVE_SCM) {
8592 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8593 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
8594 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
8599 wpa_dbg(wpa_s, MSG_DEBUG,
8600 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
8601 invalid_freq, policy_move, flags);
8604 * The channel is valid, or we are going to have a policy move, so
8607 if (!invalid_freq || policy_move) {
8608 wpa_dbg(wpa_s, MSG_DEBUG,
8609 "P2P: Cancel a GO move from freq=%d MHz", freq);
8610 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8612 if (wpas_p2p_in_progress(wpa_s)) {
8613 wpa_dbg(wpa_s, MSG_DEBUG,
8614 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
8615 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
8617 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
8618 wpas_p2p_reconsider_moving_go,
8624 if (!invalid_freq && (!policy_move || flags != 0)) {
8625 wpa_dbg(wpa_s, MSG_DEBUG,
8626 "P2P: Not initiating a GO frequency change");
8630 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
8631 timeout = P2P_GO_FREQ_CHANGE_TIME;
8635 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
8637 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8638 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
8642 static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
8643 struct wpa_used_freq_data *freqs,
8645 enum wpas_p2p_channel_update_trig trig)
8647 struct wpa_supplicant *ifs;
8649 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
8653 * Travers all the radio interfaces, and for each GO interface, check
8654 * if there is a need to move the GO from the frequency it is using,
8655 * or in case the frequency is valid again, cancel the evacuation flow.
8657 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8659 if (ifs->current_ssid == NULL ||
8660 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
8664 * The GO was just started or completed channel switch, no need
8668 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
8669 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
8670 wpa_dbg(wpa_s, MSG_DEBUG,
8671 "P2P: GO move - schedule re-consideration");
8672 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
8673 wpas_p2p_reconsider_moving_go,
8678 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
8683 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8685 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8688 wpas_p2p_update_channel_list(wpa_s,
8689 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
8693 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8695 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8696 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8697 "the management interface is being removed");
8698 wpas_p2p_deinit_global(wpa_s->global);
8703 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8705 if (wpa_s->ap_iface->bss)
8706 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8707 wpas_p2p_group_deinit(wpa_s);