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
50 #ifndef P2P_MAX_CLIENT_IDLE
52 * How many seconds to try to reconnect to the GO when connection in P2P client
55 #define P2P_MAX_CLIENT_IDLE 10
56 #endif /* P2P_MAX_CLIENT_IDLE */
58 #ifndef P2P_MAX_INITIAL_CONN_WAIT
60 * How many seconds to wait for initial 4-way handshake to get completed after
61 * WPS provisioning step or after the re-invocation of a persistent group on a
64 #define P2P_MAX_INITIAL_CONN_WAIT 10
65 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
67 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
69 * How many seconds to wait for initial 4-way handshake to get completed after
70 * WPS provisioning step on the GO. This controls the extra time the P2P
71 * operation is considered to be in progress (e.g., to delay other scans) after
72 * WPS provisioning has been completed on the GO during group formation.
74 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
75 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
77 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
79 * How many seconds to wait for initial 4-way handshake to get completed after
80 * re-invocation of a persistent group on the GO when the client is expected
81 * to connect automatically (no user interaction).
83 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
84 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
86 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
88 enum p2p_group_removal_reason {
89 P2P_GROUP_REMOVAL_UNKNOWN,
90 P2P_GROUP_REMOVAL_SILENT,
91 P2P_GROUP_REMOVAL_FORMATION_FAILED,
92 P2P_GROUP_REMOVAL_REQUESTED,
93 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
94 P2P_GROUP_REMOVAL_UNAVAILABLE,
95 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
96 P2P_GROUP_REMOVAL_PSK_FAILURE,
97 P2P_GROUP_REMOVAL_FREQ_CONFLICT
101 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
102 static struct wpa_supplicant *
103 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
105 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
106 const u8 *ssid, size_t ssid_len);
107 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
108 const u8 *ssid, size_t ssid_len);
109 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
110 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
111 const u8 *dev_addr, enum p2p_wps_method wps_method,
112 int auto_join, int freq,
113 const u8 *ssid, size_t ssid_len);
114 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
115 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
116 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
117 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
118 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
120 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
121 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
123 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
124 static void wpas_stop_listen(void *ctx);
125 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
126 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
127 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
128 enum wpa_driver_if_type type);
132 * Get the number of concurrent channels that the HW can operate, but that are
133 * currently not in use by any of the wpa_supplicant interfaces.
135 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
140 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
144 num = get_shared_radio_freqs(wpa_s, freqs,
145 wpa_s->num_multichan_concurrent);
148 unused = wpa_s->num_multichan_concurrent - num;
149 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
155 * Get the frequencies that are currently in use by one or more of the virtual
156 * interfaces, and that are also valid for P2P operation.
159 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
160 struct wpa_used_freq_data *p2p_freqs,
163 struct wpa_used_freq_data *freqs;
164 unsigned int num, i, j;
166 freqs = os_calloc(wpa_s->num_multichan_concurrent,
167 sizeof(struct wpa_used_freq_data));
171 num = get_shared_radio_freqs_data(wpa_s, freqs,
172 wpa_s->num_multichan_concurrent);
174 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
176 for (i = 0, j = 0; i < num && j < len; i++) {
177 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
178 p2p_freqs[j++] = freqs[i];
183 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
189 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
194 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
195 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
196 wpas_p2p_num_unused_channels(wpa_s) > 0) {
197 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
201 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
205 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
206 struct wpa_scan_results *scan_res)
210 if (wpa_s->p2p_scan_work) {
211 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
212 wpa_s->p2p_scan_work = NULL;
213 radio_work_done(work);
216 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
219 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
220 (int) scan_res->num);
222 for (i = 0; i < scan_res->num; i++) {
223 struct wpa_scan_res *bss = scan_res->res[i];
224 struct os_reltime time_tmp_age, entry_ts;
228 time_tmp_age.sec = bss->age / 1000;
229 time_tmp_age.usec = (bss->age % 1000) * 1000;
230 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
232 ies = (const u8 *) (bss + 1);
233 ies_len = bss->ie_len;
234 if (bss->beacon_ie_len > 0 &&
235 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
236 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
237 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
238 MACSTR, MAC2STR(bss->bssid));
240 ies_len = bss->beacon_ie_len;
244 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
245 bss->freq, &entry_ts, bss->level,
250 p2p_scan_res_handled(wpa_s->global->p2p);
254 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
256 struct wpa_supplicant *wpa_s = work->wpa_s;
257 struct wpa_driver_scan_params *params = work->ctx;
261 if (!work->started) {
262 wpa_scan_free_params(params);
266 wpa_s->p2p_scan_work = NULL;
270 ret = wpa_drv_scan(wpa_s, params);
271 wpa_scan_free_params(params);
274 radio_work_done(work);
275 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
279 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
280 os_get_reltime(&wpa_s->scan_trigger_time);
281 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
282 wpa_s->own_scan_requested = 1;
283 wpa_s->p2p_scan_work = work;
287 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
290 if (wpa_s->global->p2p_24ghz_social_channels &&
291 (freq == 2412 || freq == 2437 || freq == 2462)) {
293 * Search all social channels regardless of whether these have
294 * been disabled for P2P operating channel use to avoid missing
299 return p2p_supported_freq(wpa_s->global->p2p, freq);
303 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
304 unsigned int num_req_dev_types,
305 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
307 struct wpa_supplicant *wpa_s = ctx;
308 struct wpa_driver_scan_params *params = NULL;
309 struct wpabuf *wps_ie, *ies;
310 unsigned int num_channels = 0;
311 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
315 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
318 if (wpa_s->p2p_scan_work) {
319 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
323 params = os_zalloc(sizeof(*params));
327 /* P2P Wildcard SSID */
328 params->num_ssids = 1;
329 n = os_malloc(P2P_WILDCARD_SSID_LEN);
332 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
333 params->ssids[0].ssid = n;
334 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
336 wpa_s->wps->dev.p2p = 1;
337 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
338 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
339 num_req_dev_types, req_dev_types);
343 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
344 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
349 wpabuf_put_buf(ies, wps_ie);
352 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
354 params->p2p_probe = 1;
355 n = os_malloc(wpabuf_len(ies));
360 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
361 params->extra_ies = n;
362 params->extra_ies_len = wpabuf_len(ies);
366 case P2P_SCAN_SOCIAL:
367 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
369 if (params->freqs == NULL)
371 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
372 if (wpas_p2p_search_social_channel(
373 wpa_s, social_channels_freq[i]))
374 params->freqs[num_channels++] =
375 social_channels_freq[i];
377 params->freqs[num_channels++] = 0;
381 case P2P_SCAN_SPECIFIC:
382 params->freqs = os_calloc(2, sizeof(int));
383 if (params->freqs == NULL)
385 params->freqs[0] = freq;
386 params->freqs[1] = 0;
388 case P2P_SCAN_SOCIAL_PLUS_ONE:
389 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
391 if (params->freqs == NULL)
393 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
394 if (wpas_p2p_search_social_channel(
395 wpa_s, social_channels_freq[i]))
396 params->freqs[num_channels++] =
397 social_channels_freq[i];
399 if (p2p_supported_freq(wpa_s->global->p2p, freq))
400 params->freqs[num_channels++] = freq;
401 params->freqs[num_channels++] = 0;
405 radio_remove_works(wpa_s, "p2p-scan", 0);
406 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
412 wpa_scan_free_params(params);
417 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
419 switch (p2p_group_interface) {
420 case P2P_GROUP_INTERFACE_PENDING:
421 return WPA_IF_P2P_GROUP;
422 case P2P_GROUP_INTERFACE_GO:
423 return WPA_IF_P2P_GO;
424 case P2P_GROUP_INTERFACE_CLIENT:
425 return WPA_IF_P2P_CLIENT;
428 return WPA_IF_P2P_GROUP;
432 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
434 size_t ssid_len, int *go)
438 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
439 for (s = wpa_s->conf->ssid; s; s = s->next) {
440 if (s->disabled != 0 || !s->p2p_group ||
441 s->ssid_len != ssid_len ||
442 os_memcmp(ssid, s->ssid, ssid_len) != 0)
444 if (s->mode == WPAS_MODE_P2P_GO &&
445 s != wpa_s->current_ssid)
448 *go = s->mode == WPAS_MODE_P2P_GO;
457 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
459 struct wpa_supplicant *wpa_s = eloop_ctx;
460 wpa_printf(MSG_DEBUG,
461 "P2P: Complete previously requested removal of %s",
463 wpas_p2p_disconnect(wpa_s);
467 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
468 struct wpa_supplicant *calling_wpa_s)
470 if (calling_wpa_s == wpa_s && wpa_s &&
471 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
473 * The calling wpa_s instance is going to be removed. Do that
474 * from an eloop callback to keep the instance available until
475 * the caller has returned. This my be needed, e.g., to provide
476 * control interface responses on the per-interface socket.
478 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
484 return wpas_p2p_disconnect(wpa_s);
488 /* Determine total number of clients in active groups where we are the GO */
489 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
491 unsigned int count = 0;
494 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
495 for (s = wpa_s->conf->ssid; s; s = s->next) {
496 wpa_printf(MSG_DEBUG,
497 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
498 wpa_s, s, s->disabled, s->p2p_group,
500 if (!s->disabled && s->p2p_group &&
501 s->mode == WPAS_MODE_P2P_GO) {
502 count += p2p_get_group_num_members(
512 /* Find an interface for a P2P group where we are the GO */
513 static struct wpa_supplicant *
514 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
516 struct wpa_supplicant *save = NULL;
522 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
523 for (s = wpa_s->conf->ssid; s; s = s->next) {
524 if (s->disabled || !s->p2p_group ||
525 s->mode != WPAS_MODE_P2P_GO)
528 /* Prefer a group with connected clients */
529 if (p2p_get_group_num_members(wpa_s->p2p_group))
535 /* No group with connected clients, so pick the one without (if any) */
540 /* Find an active P2P group where we are the GO */
541 static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
544 struct wpa_ssid *s, *empty = NULL;
549 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
550 for (s = wpa_s->conf->ssid; s; s = s->next) {
551 if (s->disabled || !s->p2p_group ||
552 s->mode != WPAS_MODE_P2P_GO)
555 os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
556 if (p2p_get_group_num_members(wpa_s->p2p_group))
566 /* Find a persistent group where we are the GO */
567 static struct wpa_ssid *
568 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
572 for (s = wpa_s->conf->ssid; s; s = s->next) {
573 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
581 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
583 struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
585 u8 conncap = P2PS_SETUP_NONE;
586 unsigned int owned_members = 0;
587 unsigned int owner = 0;
588 unsigned int client = 0;
589 struct wpa_supplicant *go_wpa_s;
590 struct wpa_ssid *persistent_go;
591 int p2p_no_group_iface;
593 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
596 * For non-concurrent capable devices:
597 * If persistent_go, then no new.
598 * If GO, then no client.
599 * If client, then no GO.
601 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
602 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
603 p2p_no_group_iface = wpa_s->conf->p2p_no_group_iface;
605 wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
606 go_wpa_s, persistent_go);
608 for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
609 tmp_wpa_s = tmp_wpa_s->next) {
610 for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
611 wpa_printf(MSG_DEBUG,
612 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
613 tmp_wpa_s, s, s->disabled,
614 s->p2p_group, s->mode);
615 if (!s->disabled && s->p2p_group) {
616 if (s->mode == WPAS_MODE_P2P_GO) {
618 p2p_get_group_num_members(
619 tmp_wpa_s->p2p_group);
627 /* If not concurrent, restrict our choices */
628 if (p2p_no_group_iface) {
629 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
632 return P2PS_SETUP_NONE;
635 if (role == P2PS_SETUP_CLIENT ||
636 incoming == P2PS_SETUP_GROUP_OWNER ||
637 p2p_client_limit_reached(go_wpa_s->p2p_group))
638 return P2PS_SETUP_NONE;
640 return P2PS_SETUP_GROUP_OWNER;
644 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
646 return P2PS_SETUP_GROUP_OWNER |
648 if (incoming == P2PS_SETUP_NEW) {
651 if (os_get_random(&r, sizeof(r)) < 0 ||
653 return P2PS_SETUP_CLIENT;
654 return P2PS_SETUP_GROUP_OWNER;
660 /* If a required role has been specified, handle it here */
661 if (role && role != P2PS_SETUP_NEW) {
663 case P2PS_SETUP_NONE:
665 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
666 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
670 case P2PS_SETUP_GROUP_OWNER:
672 * Must be a complimentary role - cannot be a client to
673 * more than one peer.
675 if (incoming == role || client)
676 return P2PS_SETUP_NONE;
678 return P2PS_SETUP_CLIENT;
680 case P2PS_SETUP_CLIENT:
681 /* Must be a complimentary role */
682 if (incoming != role) {
683 conncap = P2PS_SETUP_GROUP_OWNER;
688 return P2PS_SETUP_NONE;
693 * For now, we only will support ownership of one group, and being a
694 * client of one group. Therefore, if we have either an existing GO
695 * group, or an existing client group, we will not do a new GO
696 * negotiation, but rather try to re-use the existing groups.
699 case P2PS_SETUP_NONE:
702 conncap = P2PS_SETUP_GROUP_OWNER;
703 else if (!owned_members)
704 conncap = P2PS_SETUP_NEW;
705 else if (incoming == P2PS_SETUP_NONE)
706 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
708 conncap = P2PS_SETUP_CLIENT;
711 case P2PS_SETUP_CLIENT:
712 conncap = P2PS_SETUP_GROUP_OWNER;
715 case P2PS_SETUP_GROUP_OWNER:
717 conncap = P2PS_SETUP_CLIENT;
720 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
721 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
723 conncap = P2PS_SETUP_GROUP_OWNER;
727 if (os_get_random(&r, sizeof(r)) < 0 ||
729 conncap = P2PS_SETUP_CLIENT;
731 conncap = P2PS_SETUP_GROUP_OWNER;
736 return P2PS_SETUP_NONE;
740 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
741 (!incoming && (conncap & P2PS_SETUP_NEW))) {
742 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
743 conncap &= ~P2PS_SETUP_GROUP_OWNER;
744 wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
745 owner, owned_members, conncap);
747 s = wpas_p2p_get_persistent_go(wpa_s);
749 if (!s && !owner && p2p_no_group_iface) {
750 p2p_set_intended_addr(wpa_s->global->p2p,
752 } else if (!s && !owner) {
753 if (wpas_p2p_add_group_interface(wpa_s,
754 WPA_IF_P2P_GO) < 0) {
755 wpa_printf(MSG_ERROR,
756 "P2P: Failed to allocate a new interface for the group");
757 return P2PS_SETUP_NONE;
759 wpa_s->global->pending_group_iface_for_p2ps = 1;
760 p2p_set_intended_addr(wpa_s->global->p2p,
761 wpa_s->pending_interface_addr);
769 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
770 enum p2p_group_removal_reason removal_reason)
772 struct wpa_ssid *ssid;
776 ssid = wpa_s->current_ssid;
779 * The current SSID was not known, but there may still be a
780 * pending P2P group interface waiting for provisioning or a
781 * P2P group that is trying to reconnect.
783 ssid = wpa_s->conf->ssid;
785 if (ssid->p2p_group && ssid->disabled != 2)
790 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
792 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
797 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
799 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
800 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
801 wpa_s->reassociate = 0;
802 wpa_s->disconnected = 1;
807 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
808 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
810 if (os_strcmp(gtype, "client") == 0) {
811 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
812 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
814 wpa_printf(MSG_DEBUG,
815 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
816 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
817 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
822 if (wpa_s->cross_connect_in_use) {
823 wpa_s->cross_connect_in_use = 0;
824 wpa_msg_global(wpa_s->parent, MSG_INFO,
825 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
826 wpa_s->ifname, wpa_s->cross_connect_uplink);
828 switch (removal_reason) {
829 case P2P_GROUP_REMOVAL_REQUESTED:
830 reason = " reason=REQUESTED";
832 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
833 reason = " reason=FORMATION_FAILED";
835 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
836 reason = " reason=IDLE";
838 case P2P_GROUP_REMOVAL_UNAVAILABLE:
839 reason = " reason=UNAVAILABLE";
841 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
842 reason = " reason=GO_ENDING_SESSION";
844 case P2P_GROUP_REMOVAL_PSK_FAILURE:
845 reason = " reason=PSK_FAILURE";
847 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
848 reason = " reason=FREQ_CONFLICT";
854 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
855 wpa_msg_global(wpa_s->parent, MSG_INFO,
856 P2P_EVENT_GROUP_REMOVED "%s %s%s",
857 wpa_s->ifname, gtype, reason);
860 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
861 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
862 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
863 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
864 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
865 wpa_s->parent, NULL) > 0) {
866 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
868 wpa_s->p2p_in_provisioning = 0;
871 wpa_s->p2p_in_invitation = 0;
874 * Make sure wait for the first client does not remain active after the
875 * group has been removed.
877 wpa_s->global->p2p_go_wait_client.sec = 0;
879 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
880 struct wpa_global *global;
882 enum wpa_driver_if_type type;
883 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
885 global = wpa_s->global;
886 ifname = os_strdup(wpa_s->ifname);
887 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
888 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
889 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
890 wpa_s = global->ifaces;
892 wpa_drv_if_remove(wpa_s, type, ifname);
897 if (!wpa_s->p2p_go_group_formation_completed) {
898 wpa_s->global->p2p_group_formation = NULL;
899 wpa_s->p2p_in_provisioning = 0;
902 wpa_s->show_group_started = 0;
903 os_free(wpa_s->go_params);
904 wpa_s->go_params = NULL;
906 os_free(wpa_s->p2p_group_common_freqs);
907 wpa_s->p2p_group_common_freqs = NULL;
908 wpa_s->p2p_group_common_freqs_num = 0;
910 wpa_s->waiting_presence_resp = 0;
912 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
913 if (ssid && (ssid->p2p_group ||
914 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
915 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
917 if (ssid == wpa_s->current_ssid) {
918 wpa_sm_set_config(wpa_s->wpa, NULL);
919 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
920 wpa_s->current_ssid = NULL;
923 * Networks objects created during any P2P activities are not
924 * exposed out as they might/will confuse certain non-P2P aware
925 * applications since these network objects won't behave like
928 * Likewise, we don't send out network removed signals for such
931 wpa_config_remove_network(wpa_s->conf, id);
932 wpa_supplicant_clear_status(wpa_s);
933 wpa_supplicant_cancel_sched_scan(wpa_s);
935 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
939 wpa_supplicant_ap_deinit(wpa_s);
941 wpa_drv_deinit_p2p_cli(wpa_s);
947 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
949 const u8 *ssid, size_t ssid_len)
957 if (wpa_s->go_params)
958 bssid = wpa_s->go_params->peer_interface_addr;
960 bssid = wpa_s->bssid;
962 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
963 if (bss == NULL && wpa_s->go_params &&
964 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
965 bss = wpa_bss_get_p2p_dev_addr(
966 wpa_s, wpa_s->go_params->peer_device_addr);
968 u8 iface_addr[ETH_ALEN];
969 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
971 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
974 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
975 "group is persistent - BSS " MACSTR " not found",
980 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
982 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
985 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
986 "group is persistent - BSS " MACSTR
987 " did not include P2P IE", MAC2STR(bssid));
988 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
989 (u8 *) (bss + 1), bss->ie_len);
990 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
991 ((u8 *) bss + 1) + bss->ie_len,
996 group_capab = p2p_get_group_capab(p2p);
997 addr = p2p_get_go_dev_addr(p2p);
998 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
999 "group_capab=0x%x", group_capab);
1001 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1002 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1005 os_memset(go_dev_addr, 0, ETH_ALEN);
1008 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1009 "go_dev_addr=" MACSTR,
1010 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1012 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1016 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1017 struct wpa_ssid *ssid,
1018 const u8 *go_dev_addr)
1023 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1024 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1025 for (s = wpa_s->conf->ssid; s; s = s->next) {
1026 if (s->disabled == 2 &&
1027 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1028 s->ssid_len == ssid->ssid_len &&
1029 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1034 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1036 if (ssid->passphrase && !s->passphrase)
1038 else if (ssid->passphrase && s->passphrase &&
1039 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1042 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1045 s = wpa_config_add_network(wpa_s->conf);
1050 * Instead of network_added we emit persistent_group_added
1051 * notification. Also to keep the defense checks in
1052 * persistent_group obj registration method, we set the
1053 * relevant flags in s to designate it as a persistent group.
1056 s->p2p_persistent_group = 1;
1057 wpas_notify_persistent_group_added(wpa_s, s);
1058 wpa_config_set_network_defaults(s);
1062 s->p2p_persistent_group = 1;
1065 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1066 s->mode = ssid->mode;
1067 s->auth_alg = WPA_AUTH_ALG_OPEN;
1068 s->key_mgmt = WPA_KEY_MGMT_PSK;
1069 s->proto = WPA_PROTO_RSN;
1070 s->pairwise_cipher = WPA_CIPHER_CCMP;
1072 if (ssid->passphrase) {
1073 os_free(s->passphrase);
1074 s->passphrase = os_strdup(ssid->passphrase);
1076 if (ssid->psk_set) {
1078 os_memcpy(s->psk, ssid->psk, 32);
1080 if (s->passphrase && !s->psk_set)
1081 wpa_config_update_psk(s);
1082 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1084 s->ssid = os_malloc(ssid->ssid_len);
1087 s->ssid_len = ssid->ssid_len;
1088 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1090 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1091 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1092 wpa_s->global->add_psk = NULL;
1096 if (changed && wpa_s->conf->update_config &&
1097 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1098 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1105 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1108 struct wpa_ssid *ssid, *s;
1113 ssid = wpa_s->current_ssid;
1114 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1115 !ssid->p2p_persistent_group)
1118 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
1119 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1122 if (s->ssid_len == ssid->ssid_len &&
1123 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1130 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
1131 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
1135 if (i == s->num_p2p_clients - 1)
1136 return; /* already the most recent entry */
1138 /* move the entry to mark it most recent */
1139 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1140 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1141 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
1142 os_memcpy(s->p2p_client_list +
1143 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1145 os_memset(s->p2p_client_list +
1146 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1152 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1153 n = os_realloc_array(s->p2p_client_list,
1154 s->num_p2p_clients + 1, 2 * ETH_ALEN);
1157 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1159 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1161 s->p2p_client_list = n;
1162 s->num_p2p_clients++;
1163 } else if (!found && s->p2p_client_list) {
1164 /* Not enough room for an additional entry - drop the oldest
1166 os_memmove(s->p2p_client_list,
1167 s->p2p_client_list + 2 * ETH_ALEN,
1168 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1169 os_memcpy(s->p2p_client_list +
1170 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1172 os_memset(s->p2p_client_list +
1173 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1177 if (wpa_s->parent->conf->update_config &&
1178 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1179 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1183 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1184 int go, struct wpa_ssid *ssid, int freq,
1185 const u8 *psk, const char *passphrase,
1186 const u8 *go_dev_addr, int persistent,
1189 const char *ssid_txt;
1193 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1198 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1202 if (passphrase && passphrase[0] == '\0')
1206 * Include PSK/passphrase only in the control interface message and
1207 * leave it out from the debug log entry.
1209 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1210 P2P_EVENT_GROUP_STARTED
1211 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1213 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1214 psk ? " psk=" : "", psk_txt,
1215 passphrase ? " passphrase=\"" : "",
1216 passphrase ? passphrase : "",
1217 passphrase ? "\"" : "",
1218 MAC2STR(go_dev_addr),
1219 persistent ? " [PERSISTENT]" : "", extra);
1220 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1221 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1222 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1223 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1228 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1231 struct wpa_ssid *ssid;
1234 u8 go_dev_addr[ETH_ALEN];
1235 int network_id = -1;
1238 * This callback is likely called for the main interface. Update wpa_s
1239 * to use the group interface if a new interface was created for the
1242 if (wpa_s->global->p2p_group_formation)
1243 wpa_s = wpa_s->global->p2p_group_formation;
1244 if (wpa_s->p2p_go_group_formation_completed) {
1245 wpa_s->global->p2p_group_formation = NULL;
1246 wpa_s->p2p_in_provisioning = 0;
1248 wpa_s->p2p_in_invitation = 0;
1249 wpa_s->group_formation_reported = 1;
1252 wpa_msg_global(wpa_s->parent, MSG_INFO,
1253 P2P_EVENT_GROUP_FORMATION_FAILURE);
1254 wpas_p2p_group_delete(wpa_s,
1255 P2P_GROUP_REMOVAL_FORMATION_FAILED);
1259 wpa_msg_global(wpa_s->parent, MSG_INFO,
1260 P2P_EVENT_GROUP_FORMATION_SUCCESS);
1262 ssid = wpa_s->current_ssid;
1263 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1264 ssid->mode = WPAS_MODE_P2P_GO;
1265 p2p_group_notif_formation_done(wpa_s->p2p_group);
1266 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1271 client = ssid->mode == WPAS_MODE_INFRA;
1272 if (ssid->mode == WPAS_MODE_P2P_GO) {
1273 persistent = ssid->p2p_persistent_group;
1274 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1277 persistent = wpas_p2p_persistent_group(wpa_s,
1282 client = wpa_s->p2p_group_interface ==
1283 P2P_GROUP_INTERFACE_CLIENT;
1284 os_memset(go_dev_addr, 0, ETH_ALEN);
1287 wpa_s->show_group_started = 0;
1290 * Indicate event only after successfully completed 4-way
1291 * handshake, i.e., when the interface is ready for data
1294 wpa_s->show_group_started = 1;
1296 wpas_p2p_group_started(wpa_s, 1, ssid,
1297 ssid ? ssid->frequency : 0,
1298 ssid && ssid->passphrase == NULL &&
1299 ssid->psk_set ? ssid->psk : NULL,
1300 ssid ? ssid->passphrase : NULL,
1301 go_dev_addr, persistent, "");
1302 wpas_p2p_cross_connect_setup(wpa_s);
1303 wpas_p2p_set_group_idle_timeout(wpa_s);
1307 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1310 os_free(wpa_s->global->add_psk);
1311 wpa_s->global->add_psk = NULL;
1313 if (network_id < 0 && ssid)
1314 network_id = ssid->id;
1316 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1317 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1322 struct send_action_work {
1328 unsigned int wait_time;
1333 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1336 struct wpa_supplicant *wpa_s = eloop_ctx;
1338 if (!wpa_s->p2p_send_action_work)
1341 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1342 os_free(wpa_s->p2p_send_action_work->ctx);
1343 radio_work_done(wpa_s->p2p_send_action_work);
1344 wpa_s->p2p_send_action_work = NULL;
1348 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1350 if (wpa_s->p2p_send_action_work) {
1351 struct send_action_work *awork;
1352 awork = wpa_s->p2p_send_action_work->ctx;
1353 if (awork->wait_time == 0) {
1355 radio_work_done(wpa_s->p2p_send_action_work);
1356 wpa_s->p2p_send_action_work = NULL;
1359 * In theory, this should not be needed, but number of
1360 * places in the P2P code is still using non-zero wait
1361 * time for the last Action frame in the sequence and
1362 * some of these do not call send_action_done().
1364 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1366 eloop_register_timeout(
1367 0, awork->wait_time * 1000,
1368 wpas_p2p_send_action_work_timeout,
1375 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1377 const u8 *dst, const u8 *src,
1379 const u8 *data, size_t data_len,
1380 enum offchannel_send_action_result
1383 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1385 wpas_p2p_action_tx_clear(wpa_s);
1387 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1391 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1392 res = P2P_SEND_ACTION_SUCCESS;
1394 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1395 res = P2P_SEND_ACTION_NO_ACK;
1397 case OFFCHANNEL_SEND_ACTION_FAILED:
1398 res = P2P_SEND_ACTION_FAILED;
1402 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1404 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1405 wpa_s->pending_pd_before_join &&
1406 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1407 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1408 wpa_s->p2p_fallback_to_go_neg) {
1409 wpa_s->pending_pd_before_join = 0;
1410 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1411 "during p2p_connect-auto");
1412 wpa_msg_global(wpa_s->parent, MSG_INFO,
1413 P2P_EVENT_FALLBACK_TO_GO_NEG
1414 "reason=no-ACK-to-PD-Req");
1415 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1421 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1423 struct wpa_supplicant *wpa_s = work->wpa_s;
1424 struct send_action_work *awork = work->ctx;
1427 if (work->started) {
1428 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1430 wpa_s->p2p_send_action_work = NULL;
1431 offchannel_send_action_done(wpa_s);
1437 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1438 awork->bssid, awork->buf, awork->len,
1440 wpas_p2p_send_action_tx_status, 1) < 0) {
1442 radio_work_done(work);
1445 wpa_s->p2p_send_action_work = work;
1449 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1450 unsigned int freq, const u8 *dst,
1451 const u8 *src, const u8 *bssid, const u8 *buf,
1452 size_t len, unsigned int wait_time)
1454 struct send_action_work *awork;
1456 if (wpa_s->p2p_send_action_work) {
1457 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1461 awork = os_zalloc(sizeof(*awork) + len);
1466 os_memcpy(awork->dst, dst, ETH_ALEN);
1467 os_memcpy(awork->src, src, ETH_ALEN);
1468 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1470 awork->wait_time = wait_time;
1471 os_memcpy(awork->buf, buf, len);
1473 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1474 wpas_send_action_cb, awork) < 0) {
1483 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1484 const u8 *src, const u8 *bssid, const u8 *buf,
1485 size_t len, unsigned int wait_time)
1487 struct wpa_supplicant *wpa_s = ctx;
1488 int listen_freq = -1, send_freq = -1;
1490 if (wpa_s->p2p_listen_work)
1491 listen_freq = wpa_s->p2p_listen_work->freq;
1492 if (wpa_s->p2p_send_action_work)
1493 send_freq = wpa_s->p2p_send_action_work->freq;
1494 if (listen_freq != (int) freq && send_freq != (int) freq) {
1495 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1496 listen_freq, send_freq);
1497 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1501 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1502 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1504 wpas_p2p_send_action_tx_status, 1);
1508 static void wpas_send_action_done(void *ctx)
1510 struct wpa_supplicant *wpa_s = ctx;
1512 if (wpa_s->p2p_send_action_work) {
1513 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1515 os_free(wpa_s->p2p_send_action_work->ctx);
1516 radio_work_done(wpa_s->p2p_send_action_work);
1517 wpa_s->p2p_send_action_work = NULL;
1520 offchannel_send_action_done(wpa_s);
1524 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1525 struct p2p_go_neg_results *params)
1527 if (wpa_s->go_params == NULL) {
1528 wpa_s->go_params = os_malloc(sizeof(*params));
1529 if (wpa_s->go_params == NULL)
1532 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1537 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1538 struct p2p_go_neg_results *res)
1540 wpa_s->group_formation_reported = 0;
1541 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1542 " dev_addr " MACSTR " wps_method %d",
1543 MAC2STR(res->peer_interface_addr),
1544 MAC2STR(res->peer_device_addr), res->wps_method);
1545 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1546 res->ssid, res->ssid_len);
1547 wpa_supplicant_ap_deinit(wpa_s);
1548 wpas_copy_go_neg_results(wpa_s, res);
1549 if (res->wps_method == WPS_PBC) {
1550 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1551 #ifdef CONFIG_WPS_NFC
1552 } else if (res->wps_method == WPS_NFC) {
1553 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1554 res->peer_interface_addr,
1555 wpa_s->parent->p2p_oob_dev_pw,
1556 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1557 wpa_s->parent->p2p_oob_dev_pw_id ==
1558 DEV_PW_NFC_CONNECTION_HANDOVER ?
1559 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1562 #endif /* CONFIG_WPS_NFC */
1564 u16 dev_pw_id = DEV_PW_DEFAULT;
1565 if (wpa_s->p2p_wps_method == WPS_P2PS)
1566 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1567 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1568 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1569 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1570 wpa_s->p2p_pin, 1, dev_pw_id);
1575 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1576 struct wpa_ssid *ssid)
1578 struct wpa_ssid *persistent;
1579 struct psk_list_entry *psk;
1580 struct hostapd_data *hapd;
1582 if (!wpa_s->ap_iface)
1585 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1587 if (persistent == NULL)
1590 hapd = wpa_s->ap_iface->bss[0];
1592 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1594 struct hostapd_wpa_psk *hpsk;
1596 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1598 MAC2STR(psk->addr), psk->p2p);
1599 hpsk = os_zalloc(sizeof(*hpsk));
1602 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1604 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1606 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1607 hpsk->next = hapd->conf->ssid.wpa_psk;
1608 hapd->conf->ssid.wpa_psk = hpsk;
1613 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1617 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1618 wpa_s->p2p_group_common_freqs_num);
1620 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1621 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1622 i, wpa_s->p2p_group_common_freqs[i]);
1626 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1627 struct p2p_go_neg_results *params)
1629 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1631 wpa_s->p2p_group_common_freqs_num = 0;
1632 os_free(wpa_s->p2p_group_common_freqs);
1633 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1634 if (!wpa_s->p2p_group_common_freqs)
1637 for (i = 0; i < len; i++) {
1638 if (!wpa_s->go_params->freq_list[i])
1640 wpa_s->p2p_group_common_freqs[i] =
1641 wpa_s->go_params->freq_list[i];
1643 wpa_s->p2p_group_common_freqs_num = i;
1647 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1649 #ifndef CONFIG_NO_CONFIG_WRITE
1650 if (wpa_s->parent->conf->update_config &&
1651 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1652 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1653 #endif /* CONFIG_NO_CONFIG_WRITE */
1657 static void p2p_go_configured(void *ctx, void *data)
1659 struct wpa_supplicant *wpa_s = ctx;
1660 struct p2p_go_neg_results *params = data;
1661 struct wpa_ssid *ssid;
1662 int network_id = -1;
1664 p2p_go_save_group_common_freqs(wpa_s, params);
1665 p2p_go_dump_common_freqs(wpa_s);
1667 ssid = wpa_s->current_ssid;
1668 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1669 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1670 if (wpa_s->global->p2p_group_formation == wpa_s)
1671 wpa_s->global->p2p_group_formation = NULL;
1672 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1673 params->passphrase[0] == '\0' ?
1676 wpa_s->global->p2p_dev_addr,
1677 params->persistent_group, "");
1678 wpa_s->group_formation_reported = 1;
1680 if (wpa_s->parent->p2ps_join_addr_valid) {
1681 wpa_dbg(wpa_s, MSG_DEBUG,
1682 "P2PS: Setting default PIN for " MACSTR,
1683 MAC2STR(wpa_s->parent->p2ps_join_addr));
1684 wpa_supplicant_ap_wps_pin(wpa_s,
1685 wpa_s->parent->p2ps_join_addr,
1686 "12345670", NULL, 0, 0);
1687 wpa_s->parent->p2ps_join_addr_valid = 0;
1690 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1691 if (params->persistent_group) {
1692 network_id = wpas_p2p_store_persistent_group(
1693 wpa_s->parent, ssid,
1694 wpa_s->global->p2p_dev_addr);
1695 wpas_p2p_add_psk_list(wpa_s, ssid);
1698 network_id = ssid->id;
1699 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1700 wpas_p2p_cross_connect_setup(wpa_s);
1701 wpas_p2p_set_group_idle_timeout(wpa_s);
1703 if (wpa_s->p2p_first_connection_timeout) {
1704 wpa_dbg(wpa_s, MSG_DEBUG,
1705 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1706 wpa_s->p2p_first_connection_timeout);
1707 wpa_s->p2p_go_group_formation_completed = 0;
1708 wpa_s->global->p2p_group_formation = wpa_s;
1709 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1710 wpa_s->parent, NULL);
1711 eloop_register_timeout(
1712 wpa_s->p2p_first_connection_timeout, 0,
1713 wpas_p2p_group_formation_timeout,
1714 wpa_s->parent, NULL);
1720 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1721 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1722 params->peer_interface_addr)) {
1723 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1727 if (params->wps_method == WPS_PBC) {
1728 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1729 params->peer_device_addr);
1730 #ifdef CONFIG_WPS_NFC
1731 } else if (params->wps_method == WPS_NFC) {
1732 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1733 DEV_PW_NFC_CONNECTION_HANDOVER &&
1734 !wpa_s->parent->p2p_oob_dev_pw) {
1735 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1738 wpas_ap_wps_add_nfc_pw(
1739 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1740 wpa_s->parent->p2p_oob_dev_pw,
1741 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1742 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1743 #endif /* CONFIG_WPS_NFC */
1744 } else if (wpa_s->p2p_pin[0])
1745 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1746 wpa_s->p2p_pin, NULL, 0, 0);
1747 os_free(wpa_s->go_params);
1748 wpa_s->go_params = NULL;
1752 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1753 struct p2p_go_neg_results *params,
1754 int group_formation)
1756 struct wpa_ssid *ssid;
1758 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1759 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1760 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1765 ssid = wpa_config_add_network(wpa_s->conf);
1767 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1771 wpa_s->show_group_started = 0;
1772 wpa_s->p2p_go_group_formation_completed = 0;
1773 wpa_s->group_formation_reported = 0;
1775 wpa_config_set_network_defaults(ssid);
1776 ssid->temporary = 1;
1777 ssid->p2p_group = 1;
1778 ssid->p2p_persistent_group = params->persistent_group;
1779 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1781 ssid->frequency = params->freq;
1782 ssid->ht40 = params->ht40;
1783 ssid->vht = params->vht;
1784 ssid->ssid = os_zalloc(params->ssid_len + 1);
1786 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1787 ssid->ssid_len = params->ssid_len;
1789 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1790 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1791 ssid->proto = WPA_PROTO_RSN;
1792 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1793 ssid->group_cipher = WPA_CIPHER_CCMP;
1794 if (params->freq > 56160) {
1796 * Enable GCMP instead of CCMP as pairwise_cipher and
1797 * group_cipher in 60 GHz.
1799 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1800 ssid->group_cipher = WPA_CIPHER_GCMP;
1802 if (os_strlen(params->passphrase) > 0) {
1803 ssid->passphrase = os_strdup(params->passphrase);
1804 if (ssid->passphrase == NULL) {
1805 wpa_msg_global(wpa_s, MSG_ERROR,
1806 "P2P: Failed to copy passphrase for GO");
1807 wpa_config_remove_network(wpa_s->conf, ssid->id);
1811 ssid->passphrase = NULL;
1812 ssid->psk_set = params->psk_set;
1814 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1815 else if (ssid->passphrase)
1816 wpa_config_update_psk(ssid);
1817 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1819 wpa_s->ap_configured_cb = p2p_go_configured;
1820 wpa_s->ap_configured_cb_ctx = wpa_s;
1821 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1822 wpa_s->scan_req = NORMAL_SCAN_REQ;
1823 wpa_s->connect_without_scan = ssid;
1824 wpa_s->reassociate = 1;
1825 wpa_s->disconnected = 0;
1826 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1828 wpa_supplicant_req_scan(wpa_s, 0, 0);
1832 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1833 const struct wpa_supplicant *src)
1835 struct wpa_config *d;
1836 const struct wpa_config *s;
1841 #define C(n) if (s->n) d->n = os_strdup(s->n)
1850 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1851 os_memcpy(d->sec_device_type, s->sec_device_type,
1852 sizeof(d->sec_device_type));
1853 d->num_sec_device_types = s->num_sec_device_types;
1855 d->p2p_group_idle = s->p2p_group_idle;
1856 d->p2p_intra_bss = s->p2p_intra_bss;
1857 d->persistent_reconnect = s->persistent_reconnect;
1858 d->max_num_sta = s->max_num_sta;
1859 d->pbc_in_m1 = s->pbc_in_m1;
1860 d->ignore_old_scan_res = s->ignore_old_scan_res;
1861 d->beacon_int = s->beacon_int;
1862 d->dtim_period = s->dtim_period;
1863 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
1864 d->disassoc_low_ack = s->disassoc_low_ack;
1865 d->disable_scan_offload = s->disable_scan_offload;
1866 d->passive_scan = s->passive_scan;
1868 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1869 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1870 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1875 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1876 char *ifname, size_t len)
1878 char *ifname_ptr = wpa_s->ifname;
1880 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1881 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1882 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1885 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1886 if (os_strlen(ifname) >= IFNAMSIZ &&
1887 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1890 /* Try to avoid going over the IFNAMSIZ length limit */
1891 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1892 if (os_snprintf_error(len, res) && len)
1893 ifname[len - 1] = '\0';
1898 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1899 enum wpa_driver_if_type type)
1901 char ifname[120], force_ifname[120];
1903 if (wpa_s->pending_interface_name[0]) {
1904 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1905 "- skip creation of a new one");
1906 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1907 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1908 "unknown?! ifname='%s'",
1909 wpa_s->pending_interface_name);
1915 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1916 force_ifname[0] = '\0';
1918 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1920 wpa_s->p2p_group_idx++;
1922 wpa_s->pending_interface_type = type;
1923 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1924 wpa_s->pending_interface_addr, NULL) < 0) {
1925 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1930 if (force_ifname[0]) {
1931 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1933 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1934 sizeof(wpa_s->pending_interface_name));
1936 os_strlcpy(wpa_s->pending_interface_name, ifname,
1937 sizeof(wpa_s->pending_interface_name));
1938 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1939 MACSTR, wpa_s->pending_interface_name,
1940 MAC2STR(wpa_s->pending_interface_addr));
1946 static void wpas_p2p_remove_pending_group_interface(
1947 struct wpa_supplicant *wpa_s)
1949 if (!wpa_s->pending_interface_name[0] ||
1950 is_zero_ether_addr(wpa_s->pending_interface_addr))
1951 return; /* No pending virtual interface */
1953 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1954 wpa_s->pending_interface_name);
1955 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1956 wpa_s->pending_interface_name);
1957 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1958 wpa_s->pending_interface_name[0] = '\0';
1959 wpa_s->global->pending_group_iface_for_p2ps = 0;
1963 static struct wpa_supplicant *
1964 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1966 struct wpa_interface iface;
1967 struct wpa_supplicant *group_wpa_s;
1969 if (!wpa_s->pending_interface_name[0]) {
1970 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1971 if (!wpas_p2p_create_iface(wpa_s))
1974 * Something has forced us to remove the pending interface; try
1975 * to create a new one and hope for the best that we will get
1976 * the same local address.
1978 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1979 WPA_IF_P2P_CLIENT) < 0)
1983 os_memset(&iface, 0, sizeof(iface));
1984 iface.ifname = wpa_s->pending_interface_name;
1985 iface.driver = wpa_s->driver->name;
1986 if (wpa_s->conf->ctrl_interface == NULL &&
1987 wpa_s->parent != wpa_s &&
1989 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1990 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1992 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1993 iface.driver_param = wpa_s->conf->driver_param;
1994 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
1995 if (group_wpa_s == NULL) {
1996 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1997 "wpa_supplicant interface");
2000 wpa_s->pending_interface_name[0] = '\0';
2001 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2002 P2P_GROUP_INTERFACE_CLIENT;
2003 wpa_s->global->p2p_group_formation = group_wpa_s;
2004 wpa_s->global->pending_group_iface_for_p2ps = 0;
2006 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2012 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2015 struct wpa_supplicant *wpa_s = eloop_ctx;
2016 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2017 wpas_p2p_group_formation_failed(wpa_s);
2021 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
2023 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2024 wpa_s->parent, NULL);
2025 if (wpa_s->global->p2p)
2026 p2p_group_formation_failed(wpa_s->global->p2p);
2027 wpas_group_formation_completed(wpa_s, 0);
2031 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2033 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2034 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2035 wpa_s->parent, NULL);
2036 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2037 wpa_s->parent, NULL);
2038 wpa_s->global->p2p_fail_on_wps_complete = 0;
2042 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2044 if (wpa_s->global->p2p_group_formation != wpa_s)
2046 /* Speed up group formation timeout since this cannot succeed */
2047 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2048 wpa_s->parent, NULL);
2049 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2050 wpa_s->parent, NULL);
2054 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2056 struct wpa_supplicant *wpa_s = ctx;
2058 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2059 wpa_drv_cancel_remain_on_channel(wpa_s);
2060 wpa_s->off_channel_freq = 0;
2061 wpa_s->roc_waiting_drv_freq = 0;
2065 wpa_msg_global(wpa_s, MSG_INFO,
2066 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2068 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2069 wpas_p2p_remove_pending_group_interface(wpa_s);
2073 if (wpa_s->p2p_go_ht40)
2075 if (wpa_s->p2p_go_vht)
2078 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2079 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2081 res->role_go ? "GO" : "client", res->freq, res->ht40,
2082 MAC2STR(res->peer_device_addr),
2083 MAC2STR(res->peer_interface_addr),
2084 p2p_wps_method_text(res->wps_method));
2085 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2087 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2088 struct wpa_ssid *ssid;
2089 ssid = wpa_config_get_network(wpa_s->conf,
2090 wpa_s->p2p_persistent_id);
2091 if (ssid && ssid->disabled == 2 &&
2092 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2093 size_t len = os_strlen(ssid->passphrase);
2094 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2095 "on requested persistent group");
2096 os_memcpy(res->passphrase, ssid->passphrase, len);
2097 res->passphrase[len] = '\0';
2101 if (wpa_s->create_p2p_iface) {
2102 struct wpa_supplicant *group_wpa_s =
2103 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2104 if (group_wpa_s == NULL) {
2105 wpas_p2p_remove_pending_group_interface(wpa_s);
2106 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2108 wpas_p2p_group_formation_failed(wpa_s);
2111 if (group_wpa_s != wpa_s) {
2112 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2113 sizeof(group_wpa_s->p2p_pin));
2114 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2116 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2117 wpa_s->pending_interface_name[0] = '\0';
2118 group_wpa_s->p2p_in_provisioning = 1;
2121 wpas_start_wps_go(group_wpa_s, res, 1);
2123 wpas_start_wps_enrollee(group_wpa_s, res);
2125 wpa_s->p2p_in_provisioning = 1;
2126 wpa_s->global->p2p_group_formation = wpa_s;
2129 wpas_start_wps_go(wpa_s, res, 1);
2131 wpas_start_wps_enrollee(ctx, res);
2134 wpa_s->p2p_long_listen = 0;
2135 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2137 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2138 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2139 (res->peer_config_timeout % 100) * 10000,
2140 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2144 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
2146 struct wpa_supplicant *wpa_s = ctx;
2147 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2148 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
2150 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
2154 static void wpas_dev_found(void *ctx, const u8 *addr,
2155 const struct p2p_peer_info *info,
2158 #ifndef CONFIG_NO_STDOUT_DEBUG
2159 struct wpa_supplicant *wpa_s = ctx;
2160 char devtype[WPS_DEV_TYPE_BUFSIZE];
2161 char *wfd_dev_info_hex = NULL;
2163 #ifdef CONFIG_WIFI_DISPLAY
2164 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2165 WFD_SUBELEM_DEVICE_INFO);
2166 #endif /* CONFIG_WIFI_DISPLAY */
2168 if (info->p2ps_instance) {
2170 const u8 *buf = wpabuf_head(info->p2ps_instance);
2171 size_t len = wpabuf_len(info->p2ps_instance);
2178 if (len < 4 + 2 + 1)
2180 id = WPA_GET_LE32(buf);
2182 methods = WPA_GET_BE16(buf);
2185 if (str_len > len - 4 - 2 - 1)
2187 os_memcpy(str, buf, str_len);
2188 str[str_len] = '\0';
2190 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2192 wpa_msg_global(wpa_s, MSG_INFO,
2193 P2P_EVENT_DEVICE_FOUND MACSTR
2194 " p2p_dev_addr=" MACSTR
2195 " pri_dev_type=%s name='%s'"
2196 " config_methods=0x%x"
2199 " adv_id=%x asp_svc=%s%s",
2201 MAC2STR(info->p2p_device_addr),
2202 wps_dev_type_bin2str(
2204 devtype, sizeof(devtype)),
2205 info->device_name, methods,
2206 info->dev_capab, info->group_capab,
2208 info->vendor_elems ?
2209 " vendor_elems=1" : "");
2214 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2215 " p2p_dev_addr=" MACSTR
2216 " pri_dev_type=%s name='%s' config_methods=0x%x "
2217 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2218 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2219 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2221 info->device_name, info->config_methods,
2222 info->dev_capab, info->group_capab,
2223 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2224 wfd_dev_info_hex ? wfd_dev_info_hex : "",
2225 info->vendor_elems ? " vendor_elems=1" : "",
2229 os_free(wfd_dev_info_hex);
2230 #endif /* CONFIG_NO_STDOUT_DEBUG */
2232 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2236 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2238 struct wpa_supplicant *wpa_s = ctx;
2240 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2241 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2243 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2247 static void wpas_find_stopped(void *ctx)
2249 struct wpa_supplicant *wpa_s = ctx;
2250 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2254 struct wpas_p2p_listen_work {
2256 unsigned int duration;
2257 struct wpabuf *probe_resp_ie;
2261 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2265 wpabuf_free(lwork->probe_resp_ie);
2270 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2272 struct wpas_p2p_listen_work *lwork;
2274 if (!wpa_s->p2p_listen_work)
2277 lwork = wpa_s->p2p_listen_work->ctx;
2278 wpas_p2p_listen_work_free(lwork);
2279 radio_work_done(wpa_s->p2p_listen_work);
2280 wpa_s->p2p_listen_work = NULL;
2284 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2286 struct wpa_supplicant *wpa_s = work->wpa_s;
2287 struct wpas_p2p_listen_work *lwork = work->ctx;
2288 unsigned int duration;
2291 if (work->started) {
2292 wpa_s->p2p_listen_work = NULL;
2293 wpas_stop_listen(wpa_s);
2295 wpas_p2p_listen_work_free(lwork);
2299 wpa_s->p2p_listen_work = work;
2301 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2303 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2304 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2305 "report received Probe Request frames");
2306 wpas_p2p_listen_work_done(wpa_s);
2310 wpa_s->pending_listen_freq = lwork->freq;
2311 wpa_s->pending_listen_duration = lwork->duration;
2313 duration = lwork->duration;
2314 #ifdef CONFIG_TESTING_OPTIONS
2315 if (wpa_s->extra_roc_dur) {
2316 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2317 duration, duration + wpa_s->extra_roc_dur);
2318 duration += wpa_s->extra_roc_dur;
2320 #endif /* CONFIG_TESTING_OPTIONS */
2322 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2323 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2324 "to remain on channel (%u MHz) for Listen "
2325 "state", lwork->freq);
2326 wpas_p2p_listen_work_done(wpa_s);
2327 wpa_s->pending_listen_freq = 0;
2330 wpa_s->off_channel_freq = 0;
2331 wpa_s->roc_waiting_drv_freq = lwork->freq;
2335 static int wpas_start_listen(void *ctx, unsigned int freq,
2336 unsigned int duration,
2337 const struct wpabuf *probe_resp_ie)
2339 struct wpa_supplicant *wpa_s = ctx;
2340 struct wpas_p2p_listen_work *lwork;
2342 if (wpa_s->p2p_listen_work) {
2343 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2347 lwork = os_zalloc(sizeof(*lwork));
2351 lwork->duration = duration;
2352 if (probe_resp_ie) {
2353 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2354 if (lwork->probe_resp_ie == NULL) {
2355 wpas_p2p_listen_work_free(lwork);
2360 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2362 wpas_p2p_listen_work_free(lwork);
2370 static void wpas_stop_listen(void *ctx)
2372 struct wpa_supplicant *wpa_s = ctx;
2373 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2374 wpa_drv_cancel_remain_on_channel(wpa_s);
2375 wpa_s->off_channel_freq = 0;
2376 wpa_s->roc_waiting_drv_freq = 0;
2378 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2379 wpa_drv_probe_req_report(wpa_s, 0);
2380 wpas_p2p_listen_work_done(wpa_s);
2384 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
2386 struct wpa_supplicant *wpa_s = ctx;
2387 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
2392 * DNS Header section is used only to calculate compression pointers, so the
2393 * contents of this data does not matter, but the length needs to be reserved
2394 * in the virtual packet.
2396 #define DNS_HEADER_LEN 12
2399 * 27-octet in-memory packet from P2P specification containing two implied
2400 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
2402 #define P2P_SD_IN_MEMORY_LEN 27
2404 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
2405 u8 **spos, const u8 *end)
2407 while (*spos < end) {
2408 u8 val = ((*spos)[0] & 0xc0) >> 6;
2411 if (val == 1 || val == 2) {
2412 /* These are reserved values in RFC 1035 */
2413 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2414 "sequence starting with 0x%x", val);
2423 if (*spos + 2 > end) {
2424 wpa_printf(MSG_DEBUG, "P2P: No room for full "
2425 "DNS offset field");
2429 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
2430 if (offset >= *spos - start) {
2431 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
2432 "pointer offset %u", offset);
2437 spos_tmp = start + offset;
2438 return p2p_sd_dns_uncompress_label(upos, uend, start,
2444 len = (*spos)[0] & 0x3f;
2449 if (*spos + len > end) {
2450 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2451 "sequence - no room for label with length "
2456 if (*upos + len + 2 > uend)
2459 os_memcpy(*upos, *spos, len);
2471 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
2472 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
2473 * not large enough */
2474 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
2475 size_t msg_len, size_t offset)
2477 /* 27-octet in-memory packet from P2P specification */
2478 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
2479 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
2480 u8 *tmp, *end, *spos;
2486 if (offset > msg_len)
2489 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
2492 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
2493 end = spos + msg_len;
2496 os_memset(tmp, 0, DNS_HEADER_LEN);
2497 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
2498 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
2501 uend = buf + buf_len;
2503 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
2512 } else if (upos[-1] == '.')
2520 static struct p2p_srv_bonjour *
2521 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2522 const struct wpabuf *query)
2524 struct p2p_srv_bonjour *bsrv;
2527 len = wpabuf_len(query);
2528 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2529 struct p2p_srv_bonjour, list) {
2530 if (len == wpabuf_len(bsrv->query) &&
2531 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2539 static struct p2p_srv_upnp *
2540 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2541 const char *service)
2543 struct p2p_srv_upnp *usrv;
2545 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2546 struct p2p_srv_upnp, list) {
2547 if (version == usrv->version &&
2548 os_strcmp(service, usrv->service) == 0)
2555 static void wpas_sd_add_empty(struct wpabuf *resp, u8 srv_proto,
2556 u8 srv_trans_id, u8 status)
2560 if (wpabuf_tailroom(resp) < 5)
2563 /* Length (to be filled) */
2564 len_pos = wpabuf_put(resp, 2);
2565 wpabuf_put_u8(resp, srv_proto);
2566 wpabuf_put_u8(resp, srv_trans_id);
2568 wpabuf_put_u8(resp, status);
2569 /* Response Data: empty */
2570 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2574 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2577 wpas_sd_add_empty(resp, srv_proto, srv_trans_id,
2578 P2P_SD_PROTO_NOT_AVAILABLE);
2582 static void wpas_sd_add_bad_request(struct wpabuf *resp, u8 srv_proto,
2585 wpas_sd_add_empty(resp, srv_proto, srv_trans_id, P2P_SD_BAD_REQUEST);
2589 static void wpas_sd_add_not_found(struct wpabuf *resp, u8 srv_proto,
2592 wpas_sd_add_empty(resp, srv_proto, srv_trans_id,
2593 P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2597 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2598 struct wpabuf *resp, u8 srv_trans_id)
2600 struct p2p_srv_bonjour *bsrv;
2603 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2605 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2606 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2610 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2611 struct p2p_srv_bonjour, list) {
2612 if (wpabuf_tailroom(resp) <
2613 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2615 /* Length (to be filled) */
2616 len_pos = wpabuf_put(resp, 2);
2617 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2618 wpabuf_put_u8(resp, srv_trans_id);
2620 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2621 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2622 wpabuf_head(bsrv->resp),
2623 wpabuf_len(bsrv->resp));
2625 wpabuf_put_buf(resp, bsrv->query); /* Key */
2626 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2627 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2633 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2636 char str_rx[256], str_srv[256];
2638 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2639 return 0; /* Too short to include DNS Type and Version */
2640 if (os_memcmp(query + query_len - 3,
2641 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2643 return 0; /* Mismatch in DNS Type or Version */
2644 if (query_len == wpabuf_len(bsrv->query) &&
2645 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2646 return 1; /* Binary match */
2648 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2650 return 0; /* Failed to uncompress query */
2651 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2652 wpabuf_head(bsrv->query),
2653 wpabuf_len(bsrv->query) - 3, 0))
2654 return 0; /* Failed to uncompress service */
2656 return os_strcmp(str_rx, str_srv) == 0;
2660 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2661 struct wpabuf *resp, u8 srv_trans_id,
2662 const u8 *query, size_t query_len)
2664 struct p2p_srv_bonjour *bsrv;
2668 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2670 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2671 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2672 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2677 if (query_len == 0) {
2678 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2682 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2683 struct p2p_srv_bonjour, list) {
2684 if (!match_bonjour_query(bsrv, query, query_len))
2687 if (wpabuf_tailroom(resp) <
2688 5 + query_len + wpabuf_len(bsrv->resp))
2693 /* Length (to be filled) */
2694 len_pos = wpabuf_put(resp, 2);
2695 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2696 wpabuf_put_u8(resp, srv_trans_id);
2699 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2700 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2701 wpabuf_head(bsrv->resp),
2702 wpabuf_len(bsrv->resp));
2705 wpabuf_put_data(resp, query, query_len); /* Key */
2706 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2708 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2712 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2714 if (wpabuf_tailroom(resp) < 5)
2717 /* Length (to be filled) */
2718 len_pos = wpabuf_put(resp, 2);
2719 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2720 wpabuf_put_u8(resp, srv_trans_id);
2723 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2724 /* Response Data: empty */
2725 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2731 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2732 struct wpabuf *resp, u8 srv_trans_id)
2734 struct p2p_srv_upnp *usrv;
2737 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2739 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2740 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2744 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2745 struct p2p_srv_upnp, list) {
2746 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2749 /* Length (to be filled) */
2750 len_pos = wpabuf_put(resp, 2);
2751 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2752 wpabuf_put_u8(resp, srv_trans_id);
2755 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2757 wpabuf_put_u8(resp, usrv->version);
2758 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2760 wpabuf_put_str(resp, usrv->service);
2761 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2767 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2768 struct wpabuf *resp, u8 srv_trans_id,
2769 const u8 *query, size_t query_len)
2771 struct p2p_srv_upnp *usrv;
2777 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2780 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2781 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2782 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2787 if (query_len == 0) {
2788 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2792 if (wpabuf_tailroom(resp) < 5)
2795 /* Length (to be filled) */
2796 len_pos = wpabuf_put(resp, 2);
2797 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2798 wpabuf_put_u8(resp, srv_trans_id);
2801 str = os_malloc(query_len);
2804 os_memcpy(str, query + 1, query_len - 1);
2805 str[query_len - 1] = '\0';
2807 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2808 struct p2p_srv_upnp, list) {
2809 if (version != usrv->version)
2812 if (os_strcmp(str, "ssdp:all") != 0 &&
2813 os_strstr(usrv->service, str) == NULL)
2816 if (wpabuf_tailroom(resp) < 2)
2820 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2822 wpabuf_put_u8(resp, version);
2824 wpabuf_put_u8(resp, ',');
2828 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2830 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2832 wpabuf_put_str(resp, usrv->service);
2837 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2840 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2841 /* Response Data: empty */
2844 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2848 #ifdef CONFIG_WIFI_DISPLAY
2849 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2850 struct wpabuf *resp, u8 srv_trans_id,
2851 const u8 *query, size_t query_len)
2857 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2859 if (!wpa_s->global->wifi_display) {
2860 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2861 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2866 if (query_len < 1) {
2867 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2872 if (wpabuf_tailroom(resp) < 5)
2877 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2879 /* TODO: role specific handling */
2881 /* Length (to be filled) */
2882 len_pos = wpabuf_put(resp, 2);
2883 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2884 wpabuf_put_u8(resp, srv_trans_id);
2885 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2887 while (pos < query + query_len) {
2888 if (*pos < MAX_WFD_SUBELEMS &&
2889 wpa_s->global->wfd_subelem[*pos] &&
2890 wpabuf_tailroom(resp) >=
2891 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2892 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2893 "subelement %u", *pos);
2894 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2899 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2901 #endif /* CONFIG_WIFI_DISPLAY */
2904 static int find_p2ps_substr(struct p2ps_advertisement *adv_data,
2905 const u8 *needle, size_t needle_len)
2907 const u8 *haystack = (const u8 *) adv_data->svc_info;
2908 size_t haystack_len, i;
2910 /* Allow search term to be empty */
2911 if (!needle || !needle_len)
2917 haystack_len = os_strlen(adv_data->svc_info);
2918 for (i = 0; i < haystack_len; i++) {
2919 if (haystack_len - i < needle_len)
2921 if (os_memcmp(haystack + i, needle, needle_len) == 0)
2929 static void wpas_sd_req_asp(struct wpa_supplicant *wpa_s,
2930 struct wpabuf *resp, u8 srv_trans_id,
2931 const u8 *query, size_t query_len)
2933 struct p2ps_advertisement *adv_data;
2934 const u8 *svc = &query[1];
2935 const u8 *info = NULL;
2936 size_t svc_len = query[0];
2937 size_t info_len = 0;
2939 u8 *count_pos = NULL;
2942 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for ASP", query, query_len);
2944 if (!wpa_s->global->p2p) {
2945 wpa_printf(MSG_DEBUG, "P2P: ASP protocol not available");
2946 wpas_sd_add_proto_not_avail(resp, P2P_SERV_P2PS, srv_trans_id);
2950 /* Info block is optional */
2951 if (svc_len + 1 < query_len) {
2952 info = &svc[svc_len];
2956 /* Range check length of svc string and info block */
2957 if (svc_len + (info_len ? info_len + 2 : 1) > query_len) {
2958 wpa_printf(MSG_DEBUG, "P2P: ASP bad request");
2959 wpas_sd_add_bad_request(resp, P2P_SERV_P2PS, srv_trans_id);
2963 /* Detect and correct for prefix search */
2964 if (svc_len && svc[svc_len - 1] == '*') {
2969 for (adv_data = p2p_get_p2ps_adv_list(wpa_s->global->p2p);
2970 adv_data; adv_data = adv_data->next) {
2971 /* If not a prefix match, reject length mismatches */
2972 if (!prefix && svc_len != os_strlen(adv_data->svc_name))
2975 /* Search each service for request */
2976 if (os_memcmp(adv_data->svc_name, svc, svc_len) == 0 &&
2977 find_p2ps_substr(adv_data, info, info_len)) {
2978 size_t len = os_strlen(adv_data->svc_name);
2979 size_t svc_info_len = 0;
2981 if (adv_data->svc_info)
2982 svc_info_len = os_strlen(adv_data->svc_info);
2984 if (len > 0xff || svc_info_len > 0xffff)
2987 /* Length & Count to be filled as we go */
2988 if (!len_pos && !count_pos) {
2989 if (wpabuf_tailroom(resp) <
2990 len + svc_info_len + 16)
2993 len_pos = wpabuf_put(resp, 2);
2994 wpabuf_put_u8(resp, P2P_SERV_P2PS);
2995 wpabuf_put_u8(resp, srv_trans_id);
2997 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2998 count_pos = wpabuf_put(resp, 1);
3000 } else if (wpabuf_tailroom(resp) <
3001 len + svc_info_len + 10)
3005 wpa_printf(MSG_DEBUG,
3006 "P2P: Add Svc: %s info: %s",
3008 adv_data->svc_info);
3010 wpa_printf(MSG_DEBUG, "P2P: Add Svc: %s",
3011 adv_data->svc_name);
3014 /* Advertisement ID */
3015 wpabuf_put_le32(resp, adv_data->id);
3017 /* Config Methods */
3018 wpabuf_put_be16(resp, adv_data->config_methods);
3021 wpabuf_put_u8(resp, (u8) len);
3022 wpabuf_put_data(resp, adv_data->svc_name, len);
3025 wpabuf_put_u8(resp, adv_data->state);
3027 /* Service Information */
3028 wpabuf_put_le16(resp, (u16) svc_info_len);
3029 wpabuf_put_data(resp, adv_data->svc_info, svc_info_len);
3031 /* Update length and count */
3033 WPA_PUT_LE16(len_pos,
3034 (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
3038 /* Return error if no matching svc found */
3039 if (count_pos == NULL) {
3040 wpa_printf(MSG_DEBUG, "P2P: ASP service not found");
3041 wpas_sd_add_not_found(resp, P2P_SERV_P2PS, srv_trans_id);
3046 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
3047 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
3049 struct wpa_supplicant *wpa_s = ctx;
3050 const u8 *pos = tlvs;
3051 const u8 *end = tlvs + tlvs_len;
3054 struct wpabuf *resp;
3055 u8 srv_proto, srv_trans_id;
3059 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
3061 buf_len = 2 * tlvs_len + 1;
3062 buf = os_malloc(buf_len);
3064 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
3065 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
3067 freq, MAC2STR(sa), dialog_token, update_indic,
3072 if (wpa_s->p2p_sd_over_ctrl_iface) {
3073 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
3074 update_indic, tlvs, tlvs_len);
3075 return; /* to be processed by an external program */
3078 resp = wpabuf_alloc(10000);
3082 while (pos + 1 < end) {
3083 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
3084 slen = WPA_GET_LE16(pos);
3086 if (pos + slen > end || slen < 2) {
3087 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
3092 tlv_end = pos + slen;
3095 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
3097 srv_trans_id = *pos++;
3098 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
3101 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
3102 pos, tlv_end - pos);
3105 if (wpa_s->force_long_sd) {
3106 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
3108 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
3109 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
3113 switch (srv_proto) {
3114 case P2P_SERV_ALL_SERVICES:
3115 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
3116 "for all services");
3117 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
3118 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
3119 wpa_printf(MSG_DEBUG, "P2P: No service "
3120 "discovery protocols available");
3121 wpas_sd_add_proto_not_avail(
3122 resp, P2P_SERV_ALL_SERVICES,
3126 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
3127 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
3129 case P2P_SERV_BONJOUR:
3130 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
3131 pos, tlv_end - pos);
3134 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
3135 pos, tlv_end - pos);
3137 #ifdef CONFIG_WIFI_DISPLAY
3138 case P2P_SERV_WIFI_DISPLAY:
3139 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
3140 pos, tlv_end - pos);
3142 #endif /* CONFIG_WIFI_DISPLAY */
3144 wpas_sd_req_asp(wpa_s, resp, srv_trans_id,
3145 pos, tlv_end - pos);
3148 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
3149 "protocol %u", srv_proto);
3150 wpas_sd_add_proto_not_avail(resp, srv_proto,
3159 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
3160 update_indic, tlvs, tlvs_len);
3162 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
3168 static void wpas_sd_p2ps_serv_response(struct wpa_supplicant *wpa_s,
3169 const u8 *sa, u8 srv_trans_id,
3170 const u8 *pos, const u8 *tlv_end)
3178 while (left-- && pos < tlv_end) {
3183 /* Sanity check fixed length+svc_str */
3184 if (pos + 6 >= tlv_end)
3187 if (pos + svc_len + 10 > tlv_end)
3190 /* Advertisement ID */
3191 adv_id = WPA_GET_LE32(pos);
3194 /* Config Methods */
3195 config_methods = WPA_GET_BE16(pos);
3199 pos++; /* svc_len */
3200 os_memcpy(svc_str, pos, svc_len);
3201 svc_str[svc_len] = '\0';
3204 /* Service Status */
3205 svc_status = *pos++;
3207 /* Service Information Length */
3208 buf_len = WPA_GET_LE16(pos);
3211 /* Sanity check buffer length */
3212 if (buf_len > (unsigned int) (tlv_end - pos))
3216 buf = os_zalloc(2 * buf_len + 1);
3218 utf8_escape((const char *) pos, buf_len, buf,
3226 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_SERV_ASP_RESP
3227 MACSTR " %x %x %x %x %s '%s'",
3228 MAC2STR(sa), srv_trans_id, adv_id,
3229 svc_status, config_methods, svc_str,
3233 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_SERV_ASP_RESP
3234 MACSTR " %x %x %x %x %s",
3235 MAC2STR(sa), srv_trans_id, adv_id,
3236 svc_status, config_methods, svc_str);
3242 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
3243 const u8 *tlvs, size_t tlvs_len)
3245 struct wpa_supplicant *wpa_s = ctx;
3246 const u8 *pos = tlvs;
3247 const u8 *end = tlvs + tlvs_len;
3253 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
3255 if (tlvs_len > 1500) {
3256 /* TODO: better way for handling this */
3257 wpa_msg_ctrl(wpa_s, MSG_INFO,
3258 P2P_EVENT_SERV_DISC_RESP MACSTR
3259 " %u <long response: %u bytes>",
3260 MAC2STR(sa), update_indic,
3261 (unsigned int) tlvs_len);
3263 buf_len = 2 * tlvs_len + 1;
3264 buf = os_malloc(buf_len);
3266 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
3267 wpa_msg_ctrl(wpa_s, MSG_INFO,
3268 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
3269 MAC2STR(sa), update_indic, buf);
3275 u8 srv_proto, srv_trans_id, status;
3277 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
3278 slen = WPA_GET_LE16(pos);
3280 if (pos + slen > end || slen < 3) {
3281 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
3285 tlv_end = pos + slen;
3288 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
3290 srv_trans_id = *pos++;
3291 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
3294 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
3297 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
3298 pos, tlv_end - pos);
3300 if (srv_proto == P2P_SERV_P2PS && pos < tlv_end) {
3301 wpas_sd_p2ps_serv_response(wpa_s, sa, srv_trans_id,
3308 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
3312 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3313 const struct wpabuf *tlvs)
3315 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3317 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
3321 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
3322 u8 version, const char *query)
3324 struct wpabuf *tlvs;
3327 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
3330 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
3331 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
3332 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
3333 wpabuf_put_u8(tlvs, version);
3334 wpabuf_put_str(tlvs, query);
3335 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
3341 u64 wpas_p2p_sd_request_asp(struct wpa_supplicant *wpa_s, const u8 *dst, u8 id,
3342 const char *svc_str, const char *info_substr)
3344 struct wpabuf *tlvs;
3345 size_t plen, svc_len, substr_len = 0;
3348 svc_len = os_strlen(svc_str);
3350 substr_len = os_strlen(info_substr);
3352 if (svc_len > 0xff || substr_len > 0xff)
3355 plen = 1 + 1 + 1 + svc_len + 1 + substr_len;
3356 tlvs = wpabuf_alloc(2 + plen);
3360 wpabuf_put_le16(tlvs, plen);
3361 wpabuf_put_u8(tlvs, P2P_SERV_P2PS);
3362 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
3363 wpabuf_put_u8(tlvs, (u8) svc_len); /* Service String Length */
3364 wpabuf_put_data(tlvs, svc_str, svc_len);
3365 wpabuf_put_u8(tlvs, (u8) substr_len); /* Info Substring Length */
3366 wpabuf_put_data(tlvs, info_substr, substr_len);
3367 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
3374 #ifdef CONFIG_WIFI_DISPLAY
3376 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
3377 const struct wpabuf *tlvs)
3379 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3381 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
3385 #define MAX_WFD_SD_SUBELEMS 20
3387 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
3388 const char *subelems)
3395 len = wpabuf_put(tlvs, 2);
3396 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
3397 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
3399 wpabuf_put_u8(tlvs, role);
3404 if (val >= 0 && val < 256) {
3405 wpabuf_put_u8(tlvs, val);
3407 if (count == MAX_WFD_SD_SUBELEMS)
3410 pos = os_strchr(pos + 1, ',');
3416 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
3420 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
3421 const u8 *dst, const char *role)
3423 struct wpabuf *tlvs;
3425 const char *subelems;
3428 subelems = os_strchr(role, ' ');
3429 if (subelems == NULL)
3433 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
3437 if (os_strstr(role, "[source]"))
3438 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
3439 if (os_strstr(role, "[pri-sink]"))
3440 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
3441 if (os_strstr(role, "[sec-sink]"))
3442 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
3443 if (os_strstr(role, "[source+sink]"))
3444 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
3446 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
3451 #endif /* CONFIG_WIFI_DISPLAY */
3454 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
3456 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3458 return p2p_sd_cancel_request(wpa_s->global->p2p,
3459 (void *) (uintptr_t) req);
3463 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
3464 const u8 *dst, u8 dialog_token,
3465 const struct wpabuf *resp_tlvs)
3467 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3469 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
3474 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
3476 if (wpa_s->global->p2p)
3477 p2p_sd_service_update(wpa_s->global->p2p);
3481 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
3483 dl_list_del(&bsrv->list);
3484 wpabuf_free(bsrv->query);
3485 wpabuf_free(bsrv->resp);
3490 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
3492 dl_list_del(&usrv->list);
3493 os_free(usrv->service);
3498 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
3500 struct p2p_srv_bonjour *bsrv, *bn;
3501 struct p2p_srv_upnp *usrv, *un;
3503 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
3504 struct p2p_srv_bonjour, list)
3505 wpas_p2p_srv_bonjour_free(bsrv);
3507 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
3508 struct p2p_srv_upnp, list)
3509 wpas_p2p_srv_upnp_free(usrv);
3511 wpas_p2p_sd_service_update(wpa_s);
3515 int wpas_p2p_service_p2ps_id_exists(struct wpa_supplicant *wpa_s, u32 adv_id)
3520 if (p2p_service_p2ps_id(wpa_s->global->p2p, adv_id))
3527 int wpas_p2p_service_del_asp(struct wpa_supplicant *wpa_s, u32 adv_id)
3529 return p2p_service_del_asp(wpa_s->global->p2p, adv_id);
3533 int wpas_p2p_service_add_asp(struct wpa_supplicant *wpa_s,
3534 int auto_accept, u32 adv_id,
3535 const char *adv_str, u8 svc_state,
3536 u16 config_methods, const char *svc_info)
3538 return p2p_service_add_asp(wpa_s->global->p2p, auto_accept, adv_id,
3539 adv_str, svc_state, config_methods,
3544 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
3545 struct wpabuf *query, struct wpabuf *resp)
3547 struct p2p_srv_bonjour *bsrv;
3549 bsrv = os_zalloc(sizeof(*bsrv));
3552 bsrv->query = query;
3554 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
3556 wpas_p2p_sd_service_update(wpa_s);
3561 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
3562 const struct wpabuf *query)
3564 struct p2p_srv_bonjour *bsrv;
3566 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
3569 wpas_p2p_srv_bonjour_free(bsrv);
3570 wpas_p2p_sd_service_update(wpa_s);
3575 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
3576 const char *service)
3578 struct p2p_srv_upnp *usrv;
3580 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
3581 return 0; /* Already listed */
3582 usrv = os_zalloc(sizeof(*usrv));
3585 usrv->version = version;
3586 usrv->service = os_strdup(service);
3587 if (usrv->service == NULL) {
3591 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
3593 wpas_p2p_sd_service_update(wpa_s);
3598 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
3599 const char *service)
3601 struct p2p_srv_upnp *usrv;
3603 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
3606 wpas_p2p_srv_upnp_free(usrv);
3607 wpas_p2p_sd_service_update(wpa_s);
3612 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
3613 const u8 *peer, const char *params,
3614 unsigned int generated_pin)
3616 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
3617 " %08d%s", MAC2STR(peer), generated_pin, params);
3621 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
3622 const u8 *peer, const char *params)
3624 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
3625 "%s", MAC2STR(peer), params);
3629 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
3630 const u8 *dev_addr, const u8 *pri_dev_type,
3631 const char *dev_name, u16 supp_config_methods,
3632 u8 dev_capab, u8 group_capab, const u8 *group_id,
3633 size_t group_id_len)
3635 struct wpa_supplicant *wpa_s = ctx;
3636 char devtype[WPS_DEV_TYPE_BUFSIZE];
3638 u8 empty_dev_type[8];
3639 unsigned int generated_pin = 0;
3640 struct wpa_supplicant *group = NULL;
3644 for (group = wpa_s->global->ifaces; group; group = group->next)
3646 struct wpa_ssid *s = group->current_ssid;
3648 s->mode == WPAS_MODE_P2P_GO &&
3649 group_id_len - ETH_ALEN == s->ssid_len &&
3650 os_memcmp(group_id + ETH_ALEN, s->ssid,
3656 if (pri_dev_type == NULL) {
3657 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
3658 pri_dev_type = empty_dev_type;
3660 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
3661 " pri_dev_type=%s name='%s' config_methods=0x%x "
3662 "dev_capab=0x%x group_capab=0x%x%s%s",
3664 wps_dev_type_bin2str(pri_dev_type, devtype,
3666 dev_name, supp_config_methods, dev_capab, group_capab,
3667 group ? " group=" : "",
3668 group ? group->ifname : "");
3669 if (os_snprintf_error(sizeof(params), res))
3670 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
3671 params[sizeof(params) - 1] = '\0';
3673 if (config_methods & WPS_CONFIG_DISPLAY) {
3674 generated_pin = wps_generate_pin();
3675 wpas_prov_disc_local_display(wpa_s, peer, params,
3677 } else if (config_methods & WPS_CONFIG_KEYPAD)
3678 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3679 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3680 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
3681 MACSTR "%s", MAC2STR(peer), params);
3683 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
3684 P2P_PROV_DISC_SUCCESS,
3685 config_methods, generated_pin);
3689 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
3691 struct wpa_supplicant *wpa_s = ctx;
3692 unsigned int generated_pin = 0;
3695 if (wpa_s->pending_pd_before_join &&
3696 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
3697 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
3698 wpa_s->pending_pd_before_join = 0;
3699 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3700 "join-existing-group operation");
3701 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3705 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
3706 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
3709 res = os_snprintf(params, sizeof(params), " peer_go=%d",
3710 wpa_s->pending_pd_use == AUTO_PD_JOIN);
3711 if (os_snprintf_error(sizeof(params), res))
3712 params[sizeof(params) - 1] = '\0';
3716 if (config_methods & WPS_CONFIG_DISPLAY)
3717 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3718 else if (config_methods & WPS_CONFIG_KEYPAD) {
3719 generated_pin = wps_generate_pin();
3720 wpas_prov_disc_local_display(wpa_s, peer, params,
3722 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3723 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
3724 MACSTR "%s", MAC2STR(peer), params);
3726 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3727 P2P_PROV_DISC_SUCCESS,
3728 config_methods, generated_pin);
3732 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
3733 enum p2p_prov_disc_status status,
3734 u32 adv_id, const u8 *adv_mac,
3735 const char *deferred_session_resp)
3737 struct wpa_supplicant *wpa_s = ctx;
3739 if (wpa_s->p2p_fallback_to_go_neg) {
3740 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
3741 "failed - fall back to GO Negotiation");
3742 wpa_msg_global(wpa_s->parent, MSG_INFO,
3743 P2P_EVENT_FALLBACK_TO_GO_NEG
3744 "reason=PD-failed");
3745 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
3749 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
3750 wpa_s->pending_pd_before_join = 0;
3751 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3752 "join-existing-group operation (no ACK for PD "
3754 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3758 if (adv_id && adv_mac && deferred_session_resp) {
3759 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3760 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3761 " deferred_session_resp='%s'",
3762 MAC2STR(peer), status, adv_id,
3763 deferred_session_resp);
3764 } else if (adv_id && adv_mac) {
3765 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3766 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3767 MAC2STR(peer), status, adv_id);
3769 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3770 " p2p_dev_addr=" MACSTR " status=%d",
3771 MAC2STR(peer), status);
3774 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3779 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3781 if (channels == NULL)
3782 return 1; /* Assume no restrictions */
3783 return p2p_channels_includes_freq(channels, freq);
3789 * Pick the best frequency to use from all the currently used frequencies.
3791 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3792 struct wpa_used_freq_data *freqs,
3797 /* find a candidate freq that is supported by P2P */
3798 for (c = 0; c < num; c++)
3799 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3805 /* once we have a candidate, try to find a 'better' one */
3806 for (i = c + 1; i < num; i++) {
3807 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3811 * 1. Infrastructure station interfaces have higher preference.
3812 * 2. P2P Clients have higher preference.
3815 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3820 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3823 return freqs[c].freq;
3827 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3828 const u8 *go_dev_addr, const u8 *ssid,
3829 size_t ssid_len, int *go, u8 *group_bssid,
3830 int *force_freq, int persistent_group,
3831 const struct p2p_channels *channels,
3834 struct wpa_supplicant *wpa_s = ctx;
3836 struct wpa_used_freq_data *freqs;
3837 struct wpa_supplicant *grp;
3840 if (!persistent_group) {
3841 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3842 " to join an active group (SSID: %s)",
3843 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
3844 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3845 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3847 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3848 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3849 "authorized invitation");
3853 #ifdef CONFIG_WPS_NFC
3854 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3855 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
3856 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3857 wpa_s->p2p_wps_method = WPS_NFC;
3858 wpa_s->pending_join_wps_method = WPS_NFC;
3859 os_memcpy(wpa_s->pending_join_dev_addr,
3860 go_dev_addr, ETH_ALEN);
3861 os_memcpy(wpa_s->pending_join_iface_addr,
3865 #endif /* CONFIG_WPS_NFC */
3868 * Do not accept the invitation automatically; notify user and
3871 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3874 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3876 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3877 "running persistent group");
3879 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3883 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3884 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3885 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3886 "invitation to re-invoke a persistent group");
3887 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3888 } else if (!wpa_s->conf->persistent_reconnect)
3889 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3891 for (s = wpa_s->conf->ssid; s; s = s->next) {
3892 if (s->disabled == 2 &&
3893 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3894 s->ssid_len == ssid_len &&
3895 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3900 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3901 " requested reinvocation of an unknown group",
3903 return P2P_SC_FAIL_UNKNOWN_GROUP;
3906 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3908 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3909 wpa_printf(MSG_DEBUG, "P2P: The only available "
3910 "interface is already in use - reject "
3912 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3914 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3915 } else if (s->mode == WPAS_MODE_P2P_GO) {
3917 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3919 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3920 "interface address for the group");
3921 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3923 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3928 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3931 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3932 sizeof(struct wpa_used_freq_data));
3934 int num_channels = wpa_s->num_multichan_concurrent;
3935 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3936 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3940 /* Get one of the frequencies currently in use */
3941 if (best_freq > 0) {
3942 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3943 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3945 if (wpa_s->num_multichan_concurrent < 2 ||
3946 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3947 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3948 *force_freq = best_freq;
3952 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3953 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3955 /* We are the client */
3956 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3957 "running a GO but we are capable of MCC, "
3958 "figure out the best channel to use");
3960 } else if (!freq_included(channels, *force_freq)) {
3961 /* We are the GO, and *force_freq is not in the
3963 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3964 "in intersection but we are capable of MCC, "
3965 "figure out the best channel to use",
3971 return P2P_SC_SUCCESS;
3975 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3976 const u8 *ssid, size_t ssid_len,
3977 const u8 *go_dev_addr, u8 status,
3980 struct wpa_supplicant *wpa_s = ctx;
3983 for (s = wpa_s->conf->ssid; s; s = s->next) {
3984 if (s->disabled == 2 &&
3985 s->ssid_len == ssid_len &&
3986 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3990 if (status == P2P_SC_SUCCESS) {
3991 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3992 " was accepted; op_freq=%d MHz, SSID=%s",
3993 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3995 int go = s->mode == WPAS_MODE_P2P_GO;
3996 wpas_p2p_group_add_persistent(
3997 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
3998 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
4000 wpa_s->user_initiated_pd = 0;
4001 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
4002 wpa_s->p2p_wps_method, 0, op_freq,
4008 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
4009 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
4010 " was rejected (status %u)", MAC2STR(sa), status);
4016 wpa_msg_global(wpa_s, MSG_INFO,
4017 P2P_EVENT_INVITATION_RECEIVED
4018 "sa=" MACSTR " go_dev_addr=" MACSTR
4019 " bssid=" MACSTR " unknown-network",
4020 MAC2STR(sa), MAC2STR(go_dev_addr),
4023 wpa_msg_global(wpa_s, MSG_INFO,
4024 P2P_EVENT_INVITATION_RECEIVED
4025 "sa=" MACSTR " go_dev_addr=" MACSTR
4027 MAC2STR(sa), MAC2STR(go_dev_addr));
4032 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
4033 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
4034 "sa=" MACSTR " persistent=%d freq=%d",
4035 MAC2STR(sa), s->id, op_freq);
4037 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
4038 "sa=" MACSTR " persistent=%d",
4039 MAC2STR(sa), s->id);
4044 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
4045 struct wpa_ssid *ssid,
4046 const u8 *peer, int inv)
4053 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
4054 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
4058 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
4059 if (ssid->mode != WPAS_MODE_P2P_GO &&
4060 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
4061 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
4062 "due to invitation result", ssid->id);
4063 wpas_notify_network_removed(wpa_s, ssid);
4064 wpa_config_remove_network(wpa_s->conf, ssid->id);
4067 return; /* Peer not found in client list */
4070 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
4071 "group %d client list%s",
4072 MAC2STR(peer), ssid->id,
4073 inv ? " due to invitation result" : "");
4074 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
4075 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4076 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4077 ssid->num_p2p_clients--;
4078 if (wpa_s->parent->conf->update_config &&
4079 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
4080 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
4084 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
4087 struct wpa_ssid *ssid;
4089 wpa_s = wpa_s->global->p2p_invite_group;
4091 return; /* No known invitation group */
4092 ssid = wpa_s->current_ssid;
4093 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4094 !ssid->p2p_persistent_group)
4095 return; /* Not operating as a GO in persistent group */
4096 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
4097 ssid->ssid, ssid->ssid_len);
4098 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
4102 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
4103 const struct p2p_channels *channels,
4104 const u8 *peer, int neg_freq,
4107 struct wpa_supplicant *wpa_s = ctx;
4108 struct wpa_ssid *ssid;
4112 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
4113 "status=%d " MACSTR,
4114 status, MAC2STR(bssid));
4116 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
4117 "status=%d ", status);
4119 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
4121 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
4122 status, MAC2STR(peer));
4123 if (wpa_s->pending_invite_ssid_id == -1) {
4124 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
4125 wpas_remove_persistent_client(wpa_s, peer);
4126 return; /* Invitation to active group */
4129 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
4130 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
4131 "invitation exchange to indicate readiness for "
4135 if (status != P2P_SC_SUCCESS) {
4136 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
4137 ssid = wpa_config_get_network(
4138 wpa_s->conf, wpa_s->pending_invite_ssid_id);
4139 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
4141 wpas_p2p_remove_pending_group_interface(wpa_s);
4145 ssid = wpa_config_get_network(wpa_s->conf,
4146 wpa_s->pending_invite_ssid_id);
4148 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
4149 "data matching with invitation");
4154 * The peer could have missed our ctrl::ack frame for Invitation
4155 * Response and continue retransmitting the frame. To reduce the
4156 * likelihood of the peer not getting successful TX status for the
4157 * Invitation Response frame, wait a short time here before starting
4158 * the persistent group so that we will remain on the current channel to
4159 * acknowledge any possible retransmission from the peer.
4161 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
4162 "starting persistent group");
4165 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
4166 freq_included(channels, neg_freq))
4168 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
4169 freq_included(channels, peer_oper_freq))
4170 freq = peer_oper_freq;
4174 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
4175 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4176 wpas_p2p_group_add_persistent(wpa_s, ssid,
4177 ssid->mode == WPAS_MODE_P2P_GO,
4178 wpa_s->p2p_persistent_go_freq,
4180 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
4182 ssid->mode == WPAS_MODE_P2P_GO ?
4183 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4188 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
4191 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
4193 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
4197 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
4199 reg->channel[reg->channels] = chan;
4204 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
4205 struct p2p_channels *chan,
4206 struct p2p_channels *cli_chan)
4210 wpa_s->global->p2p_24ghz_social_channels = 1;
4212 os_memset(cli_chan, 0, sizeof(*cli_chan));
4214 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
4217 /* Operating class 81 - 2.4 GHz band channels 1..13 */
4218 chan->reg_class[cla].reg_class = 81;
4219 chan->reg_class[cla].channels = 0;
4220 for (i = 0; i < 11; i++) {
4221 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
4222 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
4224 if (chan->reg_class[cla].channels)
4227 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
4230 /* Operating class 115 - 5 GHz, channels 36-48 */
4231 chan->reg_class[cla].reg_class = 115;
4232 chan->reg_class[cla].channels = 0;
4233 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
4234 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
4235 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
4236 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
4237 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
4238 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
4239 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
4240 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
4241 if (chan->reg_class[cla].channels)
4244 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
4247 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
4248 chan->reg_class[cla].reg_class = 124;
4249 chan->reg_class[cla].channels = 0;
4250 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
4251 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
4252 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
4253 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
4254 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
4255 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
4256 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
4257 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
4258 if (chan->reg_class[cla].channels)
4261 chan->reg_classes = cla;
4266 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
4268 enum hostapd_hw_mode mode)
4272 for (i = 0; i < num_modes; i++) {
4273 if (modes[i].mode == mode)
4282 NOT_ALLOWED, NO_IR, ALLOWED
4285 static int has_channel(struct wpa_global *global,
4286 struct hostapd_hw_modes *mode, u8 chan, int *flags)
4291 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
4293 if (wpas_p2p_disallowed_freq(global, freq))
4296 for (i = 0; i < mode->num_channels; i++) {
4297 if (mode->channels[i].chan == chan) {
4299 *flags = mode->channels[i].flag;
4300 if (mode->channels[i].flag &
4301 (HOSTAPD_CHAN_DISABLED |
4302 HOSTAPD_CHAN_RADAR))
4304 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
4314 struct p2p_oper_class_map {
4315 enum hostapd_hw_mode mode;
4320 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
4323 static struct p2p_oper_class_map op_class[] = {
4324 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
4325 #if 0 /* Do not enable HT40 on 2 GHz for now */
4326 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
4327 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
4329 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
4330 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
4331 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
4332 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
4333 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
4334 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
4337 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
4338 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
4339 * 80 MHz, but currently use the following definition for simplicity
4340 * (these center frequencies are not actual channels, which makes
4341 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
4342 * removing invalid channels.
4344 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
4345 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
4346 { -1, 0, 0, 0, 0, BW20 }
4350 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
4351 struct hostapd_hw_modes *mode,
4354 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
4357 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
4360 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
4362 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
4363 * so the center channel is 6 channels away from the start/end.
4365 if (channel >= center_channels[i] - 6 &&
4366 channel <= center_channels[i] + 6)
4367 return center_channels[i];
4373 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
4374 struct hostapd_hw_modes *mode,
4379 enum chan_allowed res, ret = ALLOWED;
4381 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
4384 if (center_chan >= 58 && center_chan <= 138)
4385 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
4387 /* check all the channels are available */
4388 for (i = 0; i < 4; i++) {
4389 int adj_chan = center_chan - 6 + i * 4;
4391 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
4392 if (res == NOT_ALLOWED)
4397 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
4399 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
4401 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
4403 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
4411 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
4412 struct hostapd_hw_modes *mode,
4416 enum chan_allowed res, res2;
4418 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
4419 if (bw == BW40MINUS) {
4420 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
4422 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
4423 } else if (bw == BW40PLUS) {
4424 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
4426 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
4427 } else if (bw == BW80) {
4428 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
4431 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
4433 if (res == NO_IR || res2 == NO_IR)
4439 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
4440 struct p2p_channels *chan,
4441 struct p2p_channels *cli_chan)
4443 struct hostapd_hw_modes *mode;
4444 int cla, op, cli_cla;
4446 if (wpa_s->hw.modes == NULL) {
4447 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
4448 "of all supported channels; assume dualband "
4450 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
4455 for (op = 0; op_class[op].op_class; op++) {
4456 struct p2p_oper_class_map *o = &op_class[op];
4458 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
4460 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
4463 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4464 wpa_s->global->p2p_24ghz_social_channels = 1;
4465 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4466 enum chan_allowed res;
4467 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
4468 if (res == ALLOWED) {
4470 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4472 reg = &chan->reg_class[cla];
4474 reg->reg_class = o->op_class;
4476 reg->channel[reg->channels] = ch;
4478 } else if (res == NO_IR &&
4479 wpa_s->conf->p2p_add_cli_chan) {
4480 if (cli_reg == NULL) {
4481 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4483 cli_reg = &cli_chan->reg_class[cli_cla];
4485 cli_reg->reg_class = o->op_class;
4487 cli_reg->channel[cli_reg->channels] = ch;
4488 cli_reg->channels++;
4492 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4493 reg->channel, reg->channels);
4496 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4497 cli_reg->channel, cli_reg->channels);
4501 chan->reg_classes = cla;
4502 cli_chan->reg_classes = cli_cla;
4508 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
4509 struct hostapd_hw_modes *mode, u8 channel)
4512 enum chan_allowed ret;
4514 for (op = 0; op_class[op].op_class; op++) {
4515 struct p2p_oper_class_map *o = &op_class[op];
4518 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4519 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
4520 o->bw == BW20 || ch != channel)
4522 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
4524 return (o->bw == BW40MINUS) ? -1 : 1;
4531 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
4532 struct hostapd_hw_modes *mode, u8 channel)
4534 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
4537 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
4541 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4544 struct wpa_supplicant *wpa_s = ctx;
4546 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4547 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4553 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4557 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4558 const u8 *ssid, size_t ssid_len)
4560 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4561 struct wpa_ssid *s = wpa_s->current_ssid;
4564 if (s->mode != WPAS_MODE_P2P_GO &&
4565 s->mode != WPAS_MODE_AP &&
4566 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4568 if (s->ssid_len != ssid_len ||
4569 os_memcmp(ssid, s->ssid, ssid_len) != 0)
4579 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4580 const u8 *peer_dev_addr)
4582 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4583 struct wpa_ssid *ssid = wpa_s->current_ssid;
4586 if (ssid->mode != WPAS_MODE_INFRA)
4588 if (wpa_s->wpa_state != WPA_COMPLETED &&
4589 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
4591 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4599 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4601 struct wpa_supplicant *wpa_s = ctx;
4603 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
4607 static int wpas_is_concurrent_session_active(void *ctx)
4609 struct wpa_supplicant *wpa_s = ctx;
4610 struct wpa_supplicant *ifs;
4612 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4615 if (ifs->wpa_state > WPA_ASSOCIATED)
4622 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4624 struct wpa_supplicant *wpa_s = ctx;
4625 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4629 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4630 const char *conf_p2p_dev)
4632 struct wpa_interface iface;
4633 struct wpa_supplicant *p2pdev_wpa_s;
4635 char force_name[100];
4638 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4640 if (os_snprintf_error(sizeof(ifname), ret))
4642 force_name[0] = '\0';
4643 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
4644 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
4645 force_name, wpa_s->pending_interface_addr, NULL);
4647 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4650 os_strlcpy(wpa_s->pending_interface_name, ifname,
4651 sizeof(wpa_s->pending_interface_name));
4653 os_memset(&iface, 0, sizeof(iface));
4655 iface.ifname = wpa_s->pending_interface_name;
4656 iface.driver = wpa_s->driver->name;
4657 iface.driver_param = wpa_s->conf->driver_param;
4660 * If a P2P Device configuration file was given, use it as the interface
4661 * configuration file (instead of using parent's configuration file.
4664 iface.confname = conf_p2p_dev;
4665 iface.ctrl_interface = NULL;
4667 iface.confname = wpa_s->confname;
4668 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4670 iface.conf_p2p_dev = NULL;
4672 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
4673 if (!p2pdev_wpa_s) {
4674 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4677 wpa_s->p2p_dev = p2pdev_wpa_s;
4679 wpa_s->pending_interface_name[0] = '\0';
4684 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4685 const u8 *noa, size_t noa_len)
4687 struct wpa_supplicant *wpa_s, *intf = ctx;
4690 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4691 if (wpa_s->waiting_presence_resp)
4695 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4698 wpa_s->waiting_presence_resp = 0;
4700 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4701 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4702 " status=%u noa=%s", MAC2STR(src), status, hex);
4706 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4707 size_t ssid_len, u8 *go_dev_addr,
4708 u8 *ret_ssid, size_t *ret_ssid_len)
4710 struct wpa_supplicant *wpa_s = ctx;
4713 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4715 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4716 *ret_ssid_len = s->ssid_len;
4717 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
4725 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
4726 u8 *ssid, size_t *ssid_len, int *group_iface)
4728 struct wpa_supplicant *wpa_s = ctx;
4732 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
4734 s = wpas_p2p_get_persistent_go(wpa_s);
4736 os_memcpy(bssid, s->bssid, ETH_ALEN);
4739 *group_iface = wpas_p2p_create_iface(wpa_s);
4743 os_memcpy(intended_addr, bssid, ETH_ALEN);
4744 os_memcpy(ssid, s->ssid, s->ssid_len);
4745 *ssid_len = s->ssid_len;
4751 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4752 const u8 *ssid, size_t ssid_len)
4754 struct wpa_supplicant *wpa_s = ctx;
4756 int save_config = 0;
4759 /* Start with our first choice of Persistent Groups */
4760 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4761 if (go && ssid && ssid_len &&
4762 s->ssid_len == ssid_len &&
4763 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4764 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4767 /* Remove stale persistent group */
4768 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
4769 wpa_config_remove_network(wpa_s->conf, s->id);
4774 for (i = 0; i < s->num_p2p_clients; i++) {
4775 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4776 peer, ETH_ALEN) != 0)
4779 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4780 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4781 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4784 s->num_p2p_clients--;
4789 p2p_config_write(wpa_s);
4791 /* Return TRUE if valid SSID remains */
4796 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4797 const u8 *adv_mac, const u8 *ses_mac,
4798 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4799 u8 conncap, int passwd_id,
4800 const u8 *persist_ssid,
4801 size_t persist_ssid_size, int response_done,
4802 int prov_start, const char *session_info)
4804 struct wpa_supplicant *wpa_s = ctx;
4806 struct wpa_ssid *persistent_go, *stale, *s;
4807 int save_config = 0;
4808 struct wpa_supplicant *go_wpa_s;
4813 os_memset(mac, 0, ETH_ALEN);
4822 if (session_info == NULL) {
4823 wpa_msg_global(wpa_s, MSG_INFO,
4824 P2P_EVENT_P2PS_PROVISION_START MACSTR
4825 " adv_id=%x conncap=%x"
4827 " session=%x mac=" MACSTR
4828 " dev_passwd_id=%d",
4829 MAC2STR(dev), adv_id, conncap,
4831 ses_id, MAC2STR(ses_mac),
4834 wpa_msg_global(wpa_s, MSG_INFO,
4835 P2P_EVENT_P2PS_PROVISION_START MACSTR
4836 " adv_id=%x conncap=%x"
4838 " session=%x mac=" MACSTR
4839 " dev_passwd_id=%d info='%s'",
4840 MAC2STR(dev), adv_id, conncap,
4842 ses_id, MAC2STR(ses_mac),
4843 passwd_id, session_info);
4848 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4849 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4851 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4852 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4853 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4855 if (persistent_go && !persistent_go->num_p2p_clients) {
4856 /* remove empty persistent GO */
4857 wpa_config_remove_network(wpa_s->conf,
4861 wpa_msg_global(wpa_s, MSG_INFO,
4862 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4864 " adv_id=%x adv_mac=" MACSTR
4865 " session=%x mac=" MACSTR,
4866 MAC2STR(dev), status,
4867 adv_id, MAC2STR(adv_mac),
4868 ses_id, MAC2STR(ses_mac));
4872 /* Clean up stale persistent groups with this device */
4873 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4876 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4880 if (s && s->ssid_len == stale->ssid_len &&
4881 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4882 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4885 /* Remove stale persistent group */
4886 if (stale->mode != WPAS_MODE_P2P_GO ||
4887 stale->num_p2p_clients <= 1) {
4888 wpa_config_remove_network(wpa_s->conf, stale->id);
4892 for (i = 0; i < stale->num_p2p_clients; i++) {
4893 if (os_memcmp(stale->p2p_client_list +
4895 dev, ETH_ALEN) == 0) {
4896 os_memmove(stale->p2p_client_list +
4898 stale->p2p_client_list +
4900 (stale->num_p2p_clients -
4905 stale->num_p2p_clients--;
4911 p2p_config_write(wpa_s);
4914 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4915 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4917 if (persistent_go && s != persistent_go &&
4918 !persistent_go->num_p2p_clients) {
4919 /* remove empty persistent GO */
4920 wpa_config_remove_network(wpa_s->conf,
4925 wpa_msg_global(wpa_s, MSG_INFO,
4926 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4928 " adv_id=%x adv_mac=" MACSTR
4929 " session=%x mac=" MACSTR
4931 MAC2STR(dev), status,
4932 adv_id, MAC2STR(adv_mac),
4933 ses_id, MAC2STR(ses_mac), s->id);
4937 if (conncap == P2PS_SETUP_GROUP_OWNER) {
4938 const char *go_ifname = NULL;
4940 wpa_s->global->pending_p2ps_group = 1;
4942 if (wpa_s->conf->p2p_no_group_iface)
4943 go_ifname = wpa_s->ifname;
4944 else if (wpa_s->pending_interface_name[0])
4945 go_ifname = wpa_s->pending_interface_name;
4948 wpas_p2ps_prov_complete(
4949 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4950 dev, adv_mac, ses_mac,
4951 NULL, adv_id, ses_id, 0, 0,
4952 NULL, 0, 0, 0, NULL);
4956 /* If PD Resp complete, start up the GO */
4957 if (response_done && persistent_go) {
4958 wpas_p2p_group_add_persistent(
4959 wpa_s, persistent_go,
4960 0, 0, 0, 0, 0, NULL,
4961 persistent_go->mode ==
4963 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4965 } else if (response_done) {
4966 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
4969 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4970 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
4971 wpa_s->p2ps_join_addr_valid = 1;
4972 wpa_dbg(wpa_s, MSG_DEBUG,
4973 "P2PS: Saving PIN for " MACSTR,
4976 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4977 go_ifname = go_wpa_s->ifname;
4979 wpa_dbg(go_wpa_s, MSG_DEBUG,
4980 "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
4981 wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
4984 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
4985 wpa_s->p2ps_join_addr_valid = 1;
4986 wpa_dbg(wpa_s, MSG_DEBUG,
4987 "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
4990 wpa_msg_global(wpa_s, MSG_INFO,
4991 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4992 " status=%d conncap=%x"
4993 " adv_id=%x adv_mac=" MACSTR
4994 " session=%x mac=" MACSTR
4995 " dev_passwd_id=%d go=%s",
4996 MAC2STR(dev), status, conncap,
4997 adv_id, MAC2STR(adv_mac),
4998 ses_id, MAC2STR(ses_mac),
4999 passwd_id, go_ifname);
5003 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
5004 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
5006 if (persistent_go && !persistent_go->num_p2p_clients) {
5007 /* remove empty persistent GO */
5008 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
5011 if (conncap == P2PS_SETUP_CLIENT) {
5012 wpa_msg_global(wpa_s, MSG_INFO,
5013 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5014 " status=%d conncap=%x"
5015 " adv_id=%x adv_mac=" MACSTR
5016 " session=%x mac=" MACSTR
5017 " dev_passwd_id=%d join=" MACSTR,
5018 MAC2STR(dev), status, conncap,
5019 adv_id, MAC2STR(adv_mac),
5020 ses_id, MAC2STR(ses_mac),
5021 passwd_id, MAC2STR(grp_mac));
5023 wpa_msg_global(wpa_s, MSG_INFO,
5024 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5025 " status=%d conncap=%x"
5026 " adv_id=%x adv_mac=" MACSTR
5027 " session=%x mac=" MACSTR
5028 " dev_passwd_id=%d",
5029 MAC2STR(dev), status, conncap,
5030 adv_id, MAC2STR(adv_mac),
5031 ses_id, MAC2STR(ses_mac),
5037 static int _wpas_p2p_in_progress(void *ctx)
5039 struct wpa_supplicant *wpa_s = ctx;
5040 return wpas_p2p_in_progress(wpa_s);
5044 static int wpas_prov_disc_resp_cb(void *ctx)
5046 struct wpa_supplicant *wpa_s = ctx;
5047 struct wpa_ssid *persistent_go;
5049 if (!wpa_s->global->pending_p2ps_group)
5052 wpa_s->global->pending_p2ps_group = 0;
5054 if (wpas_p2p_get_go_group(wpa_s))
5056 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
5058 if (persistent_go) {
5059 wpas_p2p_group_add_persistent(
5060 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
5061 persistent_go->mode == WPAS_MODE_P2P_GO ?
5062 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
5064 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
5072 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
5073 * @global: Pointer to global data from wpa_supplicant_init()
5074 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5075 * Returns: 0 on success, -1 on failure
5077 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
5079 struct p2p_config p2p;
5082 if (wpa_s->conf->p2p_disabled)
5085 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5091 os_memset(&p2p, 0, sizeof(p2p));
5093 p2p.debug_print = wpas_p2p_debug_print;
5094 p2p.p2p_scan = wpas_p2p_scan;
5095 p2p.send_action = wpas_send_action;
5096 p2p.send_action_done = wpas_send_action_done;
5097 p2p.go_neg_completed = wpas_go_neg_completed;
5098 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
5099 p2p.dev_found = wpas_dev_found;
5100 p2p.dev_lost = wpas_dev_lost;
5101 p2p.find_stopped = wpas_find_stopped;
5102 p2p.start_listen = wpas_start_listen;
5103 p2p.stop_listen = wpas_stop_listen;
5104 p2p.send_probe_resp = wpas_send_probe_resp;
5105 p2p.sd_request = wpas_sd_request;
5106 p2p.sd_response = wpas_sd_response;
5107 p2p.prov_disc_req = wpas_prov_disc_req;
5108 p2p.prov_disc_resp = wpas_prov_disc_resp;
5109 p2p.prov_disc_fail = wpas_prov_disc_fail;
5110 p2p.invitation_process = wpas_invitation_process;
5111 p2p.invitation_received = wpas_invitation_received;
5112 p2p.invitation_result = wpas_invitation_result;
5113 p2p.get_noa = wpas_get_noa;
5114 p2p.go_connected = wpas_go_connected;
5115 p2p.presence_resp = wpas_presence_resp;
5116 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
5117 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
5118 p2p.get_persistent_group = wpas_get_persistent_group;
5119 p2p.get_go_info = wpas_get_go_info;
5120 p2p.remove_stale_groups = wpas_remove_stale_groups;
5121 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
5122 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
5123 p2p.p2ps_group_capability = p2ps_group_capability;
5125 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
5126 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
5127 p2p.dev_name = wpa_s->conf->device_name;
5128 p2p.manufacturer = wpa_s->conf->manufacturer;
5129 p2p.model_name = wpa_s->conf->model_name;
5130 p2p.model_number = wpa_s->conf->model_number;
5131 p2p.serial_number = wpa_s->conf->serial_number;
5133 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
5134 p2p.config_methods = wpa_s->wps->config_methods;
5137 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
5138 wpa_printf(MSG_ERROR,
5139 "P2P: Failed to configure supported channel list");
5143 if (wpa_s->conf->p2p_listen_reg_class &&
5144 wpa_s->conf->p2p_listen_channel) {
5145 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5146 p2p.channel = wpa_s->conf->p2p_listen_channel;
5147 p2p.channel_forced = 1;
5150 * Pick one of the social channels randomly as the listen
5153 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
5154 &p2p.channel) != 0) {
5155 wpa_printf(MSG_ERROR,
5156 "P2P: Failed to select random social channel as listen channel");
5159 p2p.channel_forced = 0;
5161 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5162 p2p.reg_class, p2p.channel);
5164 if (wpa_s->conf->p2p_oper_reg_class &&
5165 wpa_s->conf->p2p_oper_channel) {
5166 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5167 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5168 p2p.cfg_op_channel = 1;
5169 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5170 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5174 * Use random operation channel from 2.4 GHz band social
5175 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5176 * other preference is indicated.
5178 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
5179 &p2p.op_channel) != 0) {
5180 wpa_printf(MSG_ERROR,
5181 "P2P: Failed to select random social channel as operation channel");
5184 p2p.cfg_op_channel = 0;
5185 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5186 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5189 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5190 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5191 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5194 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5195 os_memcpy(p2p.country, wpa_s->conf->country, 2);
5196 p2p.country[2] = 0x04;
5198 os_memcpy(p2p.country, "XX\x04", 3);
5200 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5203 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5204 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5205 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5207 p2p.concurrent_operations = !!(wpa_s->drv_flags &
5208 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5210 p2p.max_peers = 100;
5212 if (wpa_s->conf->p2p_ssid_postfix) {
5213 p2p.ssid_postfix_len =
5214 os_strlen(wpa_s->conf->p2p_ssid_postfix);
5215 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5216 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5217 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5218 p2p.ssid_postfix_len);
5221 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5223 p2p.max_listen = wpa_s->max_remain_on_chan;
5225 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5226 wpa_s->conf->p2p_passphrase_len <= 63)
5227 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5229 p2p.passphrase_len = 8;
5231 global->p2p = p2p_init(&p2p);
5232 if (global->p2p == NULL)
5234 global->p2p_init_wpa_s = wpa_s;
5236 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5237 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5239 p2p_add_wps_vendor_extension(
5240 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5243 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5250 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5251 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5253 * This function deinitialize per-interface P2P data.
5255 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5257 if (wpa_s->driver && wpa_s->drv_priv)
5258 wpa_drv_probe_req_report(wpa_s, 0);
5260 if (wpa_s->go_params) {
5261 /* Clear any stored provisioning info */
5262 p2p_clear_provisioning_info(
5264 wpa_s->go_params->peer_device_addr);
5267 os_free(wpa_s->go_params);
5268 wpa_s->go_params = NULL;
5269 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
5270 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5271 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5272 wpa_s->p2p_long_listen = 0;
5273 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5274 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5275 wpas_p2p_remove_pending_group_interface(wpa_s);
5276 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
5277 wpas_p2p_listen_work_done(wpa_s);
5278 if (wpa_s->p2p_send_action_work) {
5279 os_free(wpa_s->p2p_send_action_work->ctx);
5280 radio_work_done(wpa_s->p2p_send_action_work);
5281 wpa_s->p2p_send_action_work = NULL;
5283 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
5285 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5286 wpa_s->p2p_oob_dev_pw = NULL;
5288 os_free(wpa_s->p2p_group_common_freqs);
5289 wpa_s->p2p_group_common_freqs = NULL;
5290 wpa_s->p2p_group_common_freqs_num = 0;
5292 /* TODO: remove group interface from the driver if this wpa_s instance
5293 * is on top of a P2P group interface */
5298 * wpas_p2p_deinit_global - Deinitialize global P2P module
5299 * @global: Pointer to global data from wpa_supplicant_init()
5301 * This function deinitializes the global (per device) P2P module.
5303 static void wpas_p2p_deinit_global(struct wpa_global *global)
5305 struct wpa_supplicant *wpa_s, *tmp;
5307 wpa_s = global->ifaces;
5309 wpas_p2p_service_flush(global->p2p_init_wpa_s);
5311 /* Remove remaining P2P group interfaces */
5312 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5313 wpa_s = wpa_s->next;
5315 tmp = global->ifaces;
5318 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5323 /* Disconnect from the P2P group and deinit the interface */
5324 wpas_p2p_disconnect(tmp);
5328 * Deinit GO data on any possibly remaining interface (if main
5329 * interface is used as GO).
5331 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5332 if (wpa_s->ap_iface)
5333 wpas_p2p_group_deinit(wpa_s);
5336 p2p_deinit(global->p2p);
5338 global->p2p_init_wpa_s = NULL;
5342 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5344 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
5345 wpa_s->conf->p2p_no_group_iface)
5346 return 0; /* separate interface disabled per configuration */
5347 if (wpa_s->drv_flags &
5348 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5349 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5350 return 1; /* P2P group requires a new interface in every case
5352 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5353 return 0; /* driver does not support concurrent operations */
5354 if (wpa_s->global->ifaces->next)
5355 return 1; /* more that one interface already in use */
5356 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5357 return 1; /* this interface is already in use */
5362 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5363 const u8 *peer_addr,
5364 enum p2p_wps_method wps_method,
5365 int go_intent, const u8 *own_interface_addr,
5366 unsigned int force_freq, int persistent_group,
5367 struct wpa_ssid *ssid, unsigned int pref_freq)
5369 if (persistent_group && wpa_s->conf->persistent_reconnect)
5370 persistent_group = 2;
5373 * Increase GO config timeout if HT40 is used since it takes some time
5374 * to scan channels for coex purposes before the BSS can be started.
5376 p2p_set_config_timeout(wpa_s->global->p2p,
5377 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5379 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5380 go_intent, own_interface_addr, force_freq,
5381 persistent_group, ssid ? ssid->ssid : NULL,
5382 ssid ? ssid->ssid_len : 0,
5383 wpa_s->p2p_pd_before_go_neg, pref_freq,
5384 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5389 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5390 const u8 *peer_addr,
5391 enum p2p_wps_method wps_method,
5392 int go_intent, const u8 *own_interface_addr,
5393 unsigned int force_freq, int persistent_group,
5394 struct wpa_ssid *ssid, unsigned int pref_freq)
5396 if (persistent_group && wpa_s->conf->persistent_reconnect)
5397 persistent_group = 2;
5399 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5400 go_intent, own_interface_addr, force_freq,
5401 persistent_group, ssid ? ssid->ssid : NULL,
5402 ssid ? ssid->ssid_len : 0, pref_freq,
5403 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5408 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5410 wpa_s->p2p_join_scan_count++;
5411 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5412 wpa_s->p2p_join_scan_count);
5413 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5414 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5415 " for join operationg - stop join attempt",
5416 MAC2STR(wpa_s->pending_join_iface_addr));
5417 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5418 if (wpa_s->p2p_auto_pd) {
5419 wpa_s->p2p_auto_pd = 0;
5420 wpa_msg_global(wpa_s, MSG_INFO,
5421 P2P_EVENT_PROV_DISC_FAILURE
5422 " p2p_dev_addr=" MACSTR " status=N/A",
5423 MAC2STR(wpa_s->pending_join_dev_addr));
5426 wpa_msg_global(wpa_s->parent, MSG_INFO,
5427 P2P_EVENT_GROUP_FORMATION_FAILURE);
5432 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5435 unsigned int num, i;
5436 struct wpa_used_freq_data *freqs;
5438 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5439 /* Multiple channels are supported and not all are in use */
5443 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5444 sizeof(struct wpa_used_freq_data));
5448 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5449 wpa_s->num_multichan_concurrent);
5451 for (i = 0; i < num; i++) {
5452 if (freqs[i].freq == freq) {
5453 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5460 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
5469 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5470 const u8 *peer_dev_addr)
5472 struct wpa_bss *bss;
5475 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5478 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5479 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5484 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5486 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5487 "%ld.%06ld (%supdated in last scan)",
5488 bss->last_update.sec, bss->last_update.usec,
5489 updated ? "": "not ");
5495 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5496 struct wpa_scan_results *scan_res)
5498 struct wpa_bss *bss = NULL;
5500 u8 iface_addr[ETH_ALEN];
5502 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5504 if (wpa_s->global->p2p_disabled)
5507 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5508 scan_res ? (int) scan_res->num : -1,
5509 wpa_s->p2p_auto_join ? "auto_" : "");
5512 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5514 if (wpa_s->p2p_auto_pd) {
5515 int join = wpas_p2p_peer_go(wpa_s,
5516 wpa_s->pending_join_dev_addr);
5518 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5519 wpa_s->auto_pd_scan_retry++;
5520 bss = wpa_bss_get_bssid_latest(
5521 wpa_s, wpa_s->pending_join_dev_addr);
5524 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5525 "the peer " MACSTR " at %d MHz",
5526 wpa_s->auto_pd_scan_retry,
5528 pending_join_dev_addr),
5530 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
5538 wpa_s->p2p_auto_pd = 0;
5539 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5540 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5541 MAC2STR(wpa_s->pending_join_dev_addr), join);
5542 if (p2p_prov_disc_req(wpa_s->global->p2p,
5543 wpa_s->pending_join_dev_addr, NULL,
5544 wpa_s->pending_pd_config_methods, join,
5545 0, wpa_s->user_initiated_pd) < 0) {
5546 wpa_s->p2p_auto_pd = 0;
5547 wpa_msg_global(wpa_s, MSG_INFO,
5548 P2P_EVENT_PROV_DISC_FAILURE
5549 " p2p_dev_addr=" MACSTR " status=N/A",
5550 MAC2STR(wpa_s->pending_join_dev_addr));
5555 if (wpa_s->p2p_auto_join) {
5556 int join = wpas_p2p_peer_go(wpa_s,
5557 wpa_s->pending_join_dev_addr);
5559 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5560 "running a GO -> use GO Negotiation");
5561 wpa_msg_global(wpa_s->parent, MSG_INFO,
5562 P2P_EVENT_FALLBACK_TO_GO_NEG
5563 "reason=peer-not-running-GO");
5564 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5565 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5566 wpa_s->p2p_persistent_group, 0, 0, 0,
5567 wpa_s->p2p_go_intent,
5568 wpa_s->p2p_connect_freq,
5569 wpa_s->p2p_persistent_id,
5570 wpa_s->p2p_pd_before_go_neg,
5576 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5577 "try to join the group", join ? "" :
5580 wpa_msg_global(wpa_s->parent, MSG_INFO,
5581 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
5582 wpa_s->p2p_fallback_to_go_neg = 1;
5586 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5587 wpa_s->pending_join_iface_addr);
5589 p2p_get_interface_addr(wpa_s->global->p2p,
5590 wpa_s->pending_join_dev_addr,
5592 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5593 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
5594 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5595 "address for join from " MACSTR " to " MACSTR
5596 " based on newly discovered P2P peer entry",
5597 MAC2STR(wpa_s->pending_join_iface_addr),
5598 MAC2STR(iface_addr));
5599 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5602 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5603 wpa_s->pending_join_iface_addr);
5606 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5607 "from P2P peer table: %d MHz", freq);
5609 if (wpa_s->p2p_join_ssid_len) {
5610 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5611 MACSTR " and SSID %s",
5612 MAC2STR(wpa_s->pending_join_iface_addr),
5613 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5614 wpa_s->p2p_join_ssid_len));
5615 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5616 wpa_s->p2p_join_ssid,
5617 wpa_s->p2p_join_ssid_len);
5620 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5621 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5622 bss = wpa_bss_get_bssid_latest(wpa_s,
5623 wpa_s->pending_join_iface_addr);
5627 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5628 "from BSS table: %d MHz (SSID %s)", freq,
5629 wpa_ssid_txt(bss->ssid, bss->ssid_len));
5634 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
5635 wpa_msg_global(wpa_s->parent, MSG_INFO,
5636 P2P_EVENT_GROUP_FORMATION_FAILURE
5637 "reason=FREQ_CONFLICT");
5641 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5642 "prior to joining an existing group (GO " MACSTR
5644 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5645 wpa_s->pending_pd_before_join = 1;
5647 switch (wpa_s->pending_join_wps_method) {
5648 case WPS_PIN_DISPLAY:
5649 method = WPS_CONFIG_KEYPAD;
5651 case WPS_PIN_KEYPAD:
5652 method = WPS_CONFIG_DISPLAY;
5655 method = WPS_CONFIG_PUSHBUTTON;
5662 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5663 wpa_s->pending_join_dev_addr) ==
5666 * We have already performed provision discovery for
5667 * joining the group. Proceed directly to join
5668 * operation without duplicated provision discovery. */
5669 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5670 "with " MACSTR " already done - proceed to "
5672 MAC2STR(wpa_s->pending_join_dev_addr));
5673 wpa_s->pending_pd_before_join = 0;
5677 if (p2p_prov_disc_req(wpa_s->global->p2p,
5678 wpa_s->pending_join_dev_addr,
5680 freq, wpa_s->user_initiated_pd) < 0) {
5681 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5682 "Discovery Request before joining an "
5684 wpa_s->pending_pd_before_join = 0;
5690 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5691 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5692 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5693 wpas_p2p_check_join_scan_limit(wpa_s);
5697 /* Start join operation immediately */
5698 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
5702 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5703 const u8 *ssid, size_t ssid_len)
5706 struct wpa_driver_scan_params params;
5707 struct wpabuf *wps_ie, *ies;
5709 int freqs[2] = { 0, 0 };
5711 os_memset(¶ms, 0, sizeof(params));
5713 /* P2P Wildcard SSID */
5714 params.num_ssids = 1;
5715 if (ssid && ssid_len) {
5716 params.ssids[0].ssid = ssid;
5717 params.ssids[0].ssid_len = ssid_len;
5718 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5719 wpa_s->p2p_join_ssid_len = ssid_len;
5721 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5722 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5723 wpa_s->p2p_join_ssid_len = 0;
5726 wpa_s->wps->dev.p2p = 1;
5727 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5728 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5730 if (wps_ie == NULL) {
5731 wpas_p2p_scan_res_join(wpa_s, NULL);
5735 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5736 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5738 wpabuf_free(wps_ie);
5739 wpas_p2p_scan_res_join(wpa_s, NULL);
5742 wpabuf_put_buf(ies, wps_ie);
5743 wpabuf_free(wps_ie);
5745 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5747 params.p2p_probe = 1;
5748 params.extra_ies = wpabuf_head(ies);
5749 params.extra_ies_len = wpabuf_len(ies);
5754 * If freq is not provided, check the operating freq of the GO
5755 * and use a single channel scan on if possible.
5757 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5758 wpa_s->pending_join_iface_addr);
5764 params.freqs = freqs;
5768 * Run a scan to update BSS table and start Provision Discovery once
5769 * the new scan results become available.
5771 ret = wpa_drv_scan(wpa_s, ¶ms);
5773 os_get_reltime(&wpa_s->scan_trigger_time);
5774 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
5775 wpa_s->own_scan_requested = 1;
5781 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5783 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5784 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5785 wpas_p2p_check_join_scan_limit(wpa_s);
5790 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5792 struct wpa_supplicant *wpa_s = eloop_ctx;
5793 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
5797 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
5798 const u8 *dev_addr, enum p2p_wps_method wps_method,
5799 int auto_join, int op_freq,
5800 const u8 *ssid, size_t ssid_len)
5802 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
5803 MACSTR " dev " MACSTR " op_freq=%d)%s",
5804 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
5805 auto_join ? " (auto_join)" : "");
5806 if (ssid && ssid_len) {
5807 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5808 wpa_ssid_txt(ssid, ssid_len));
5811 wpa_s->p2p_auto_pd = 0;
5812 wpa_s->p2p_auto_join = !!auto_join;
5813 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5814 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5815 wpa_s->pending_join_wps_method = wps_method;
5817 /* Make sure we are not running find during connection establishment */
5818 wpas_p2p_stop_find(wpa_s);
5820 wpa_s->p2p_join_scan_count = 0;
5821 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
5826 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5827 const u8 *ssid, size_t ssid_len)
5829 struct wpa_supplicant *group;
5830 struct p2p_go_neg_results res;
5831 struct wpa_bss *bss;
5833 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5836 if (group != wpa_s) {
5837 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5838 sizeof(group->p2p_pin));
5839 group->p2p_wps_method = wpa_s->p2p_wps_method;
5842 * Need to mark the current interface for p2p_group_formation
5843 * when a separate group interface is not used. This is needed
5844 * to allow p2p_cancel stop a pending p2p_connect-join.
5845 * wpas_p2p_init_group_interface() addresses this for the case
5846 * where a separate group interface is used.
5848 wpa_s->global->p2p_group_formation = wpa_s;
5851 group->p2p_in_provisioning = 1;
5852 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
5854 os_memset(&res, 0, sizeof(res));
5855 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
5856 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5858 res.wps_method = wpa_s->pending_join_wps_method;
5859 if (freq && ssid && ssid_len) {
5861 res.ssid_len = ssid_len;
5862 os_memcpy(res.ssid, ssid, ssid_len);
5864 bss = wpa_bss_get_bssid_latest(wpa_s,
5865 wpa_s->pending_join_iface_addr);
5867 res.freq = bss->freq;
5868 res.ssid_len = bss->ssid_len;
5869 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5870 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5872 wpa_ssid_txt(bss->ssid, bss->ssid_len));
5876 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5877 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5879 wpa_drv_cancel_remain_on_channel(wpa_s);
5880 wpa_s->off_channel_freq = 0;
5881 wpa_s->roc_waiting_drv_freq = 0;
5883 wpas_start_wps_enrollee(group, &res);
5886 * Allow a longer timeout for join-a-running-group than normal 15
5887 * second group formation timeout since the GO may not have authorized
5888 * our connection yet.
5890 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5891 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5898 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
5899 int *force_freq, int *pref_freq, int go)
5901 struct wpa_used_freq_data *freqs;
5902 int res, best_freq, num_unused;
5903 unsigned int freq_in_use = 0, num, i;
5905 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5906 sizeof(struct wpa_used_freq_data));
5910 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5911 wpa_s->num_multichan_concurrent);
5914 * It is possible that the total number of used frequencies is bigger
5915 * than the number of frequencies used for P2P, so get the system wide
5916 * number of unused frequencies.
5918 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5920 wpa_printf(MSG_DEBUG,
5921 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5922 freq, wpa_s->num_multichan_concurrent, num, num_unused);
5927 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5929 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5931 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5932 ieee80211_is_dfs(freq)) {
5934 * If freq is a DFS channel and DFS is offloaded
5935 * to the driver, allow P2P GO to use it.
5937 wpa_printf(MSG_DEBUG,
5938 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5941 wpa_printf(MSG_DEBUG,
5942 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5949 for (i = 0; i < num; i++) {
5950 if (freqs[i].freq == freq)
5954 if (num_unused <= 0 && !freq_in_use) {
5955 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5960 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5961 "requested channel (%u MHz)", freq);
5966 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5968 /* We have a candidate frequency to use */
5969 if (best_freq > 0) {
5970 if (*pref_freq == 0 && num_unused > 0) {
5971 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
5973 *pref_freq = best_freq;
5975 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
5977 *force_freq = best_freq;
5979 } else if (num_unused > 0) {
5980 wpa_printf(MSG_DEBUG,
5981 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5984 wpa_printf(MSG_DEBUG,
5985 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5999 * wpas_p2p_connect - Request P2P Group Formation to be started
6000 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6001 * @peer_addr: Address of the peer P2P Device
6002 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
6003 * @persistent_group: Whether to create a persistent group
6004 * @auto_join: Whether to select join vs. GO Negotiation automatically
6005 * @join: Whether to join an existing group (as a client) instead of starting
6006 * Group Owner negotiation; @peer_addr is BSSID in that case
6007 * @auth: Whether to only authorize the connection instead of doing that and
6008 * initiating Group Owner negotiation
6009 * @go_intent: GO Intent or -1 to use default
6010 * @freq: Frequency for the group or 0 for auto-selection
6011 * @persistent_id: Persistent group credentials to use for forcing GO
6012 * parameters or -1 to generate new values (SSID/passphrase)
6013 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
6014 * interoperability workaround when initiating group formation
6015 * @ht40: Start GO with 40 MHz channel width
6016 * @vht: Start GO with VHT support
6017 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6018 * failure, -2 on failure due to channel not currently available,
6019 * -3 if forced channel is not supported
6021 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6022 const char *pin, enum p2p_wps_method wps_method,
6023 int persistent_group, int auto_join, int join, int auth,
6024 int go_intent, int freq, int persistent_id, int pd,
6027 int force_freq = 0, pref_freq = 0;
6029 enum wpa_driver_if_type iftype;
6031 struct wpa_ssid *ssid = NULL;
6033 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6036 if (persistent_id >= 0) {
6037 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6038 if (ssid == NULL || ssid->disabled != 2 ||
6039 ssid->mode != WPAS_MODE_P2P_GO)
6043 os_free(wpa_s->global->add_psk);
6044 wpa_s->global->add_psk = NULL;
6046 wpa_s->global->p2p_fail_on_wps_complete = 0;
6047 wpa_s->global->pending_p2ps_group = 0;
6050 go_intent = wpa_s->conf->p2p_go_intent;
6053 wpa_s->p2p_long_listen = 0;
6055 wpa_s->p2p_wps_method = wps_method;
6056 wpa_s->p2p_persistent_group = !!persistent_group;
6057 wpa_s->p2p_persistent_id = persistent_id;
6058 wpa_s->p2p_go_intent = go_intent;
6059 wpa_s->p2p_connect_freq = freq;
6060 wpa_s->p2p_fallback_to_go_neg = 0;
6061 wpa_s->p2p_pd_before_go_neg = !!pd;
6062 wpa_s->p2p_go_ht40 = !!ht40;
6063 wpa_s->p2p_go_vht = !!vht;
6066 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6067 else if (wps_method == WPS_PIN_DISPLAY) {
6068 ret = wps_generate_pin();
6069 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6071 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6072 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
6073 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6076 wpa_s->p2p_pin[0] = '\0';
6078 if (join || auto_join) {
6079 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6081 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6082 "connect a running group from " MACSTR,
6083 MAC2STR(peer_addr));
6084 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6087 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6088 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6090 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6091 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6095 os_get_reltime(&wpa_s->p2p_auto_started);
6096 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6098 wpa_s->p2p_auto_started.sec,
6099 wpa_s->p2p_auto_started.usec);
6101 wpa_s->user_initiated_pd = 1;
6102 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
6103 auto_join, freq, NULL, 0) < 0)
6108 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6112 wpas_p2p_set_own_freq_preference(wpa_s,
6113 force_freq ? force_freq : pref_freq);
6115 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6117 if (wpa_s->create_p2p_iface) {
6118 /* Prepare to add a new interface for the group */
6119 iftype = WPA_IF_P2P_GROUP;
6120 if (go_intent == 15)
6121 iftype = WPA_IF_P2P_GO;
6122 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6123 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6124 "interface for the group");
6128 if_addr = wpa_s->pending_interface_addr;
6130 if_addr = wpa_s->own_addr;
6133 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
6135 force_freq, persistent_group, ssid,
6141 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6142 go_intent, if_addr, force_freq,
6143 persistent_group, ssid, pref_freq) < 0) {
6144 if (wpa_s->create_p2p_iface)
6145 wpas_p2p_remove_pending_group_interface(wpa_s);
6153 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6154 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6155 * @freq: Frequency of the channel in MHz
6156 * @duration: Duration of the stay on the channel in milliseconds
6158 * This callback is called when the driver indicates that it has started the
6159 * requested remain-on-channel duration.
6161 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6162 unsigned int freq, unsigned int duration)
6164 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6166 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)",
6167 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6168 wpa_s->roc_waiting_drv_freq, freq, duration);
6169 if (wpa_s->off_channel_freq &&
6170 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
6171 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6172 wpa_s->pending_listen_duration);
6173 wpa_s->pending_listen_freq = 0;
6175 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6176 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6182 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
6184 /* Limit maximum Listen state time based on driver limitation. */
6185 if (timeout > wpa_s->max_remain_on_chan)
6186 timeout = wpa_s->max_remain_on_chan;
6188 return p2p_listen(wpa_s->global->p2p, timeout);
6193 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6194 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6195 * @freq: Frequency of the channel in MHz
6197 * This callback is called when the driver indicates that a remain-on-channel
6198 * operation has been completed, i.e., the duration on the requested channel
6201 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6204 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6205 "(p2p_long_listen=%d ms pending_action_tx=%p)",
6206 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
6207 wpas_p2p_listen_work_done(wpa_s);
6208 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6210 if (wpa_s->p2p_long_listen > 0)
6211 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
6212 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6213 return; /* P2P module started a new operation */
6214 if (offchannel_pending_action_tx(wpa_s))
6216 if (wpa_s->p2p_long_listen > 0) {
6217 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
6218 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
6221 * When listen duration is over, stop listen & update p2p_state
6224 p2p_stop_listen(wpa_s->global->p2p);
6230 * wpas_p2p_group_remove - Remove a P2P group
6231 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6232 * @ifname: Network interface name of the group interface or "*" to remove all
6234 * Returns: 0 on success, -1 on failure
6236 * This function is used to remove a P2P group. This can be used to disconnect
6237 * from a group in which the local end is a P2P Client or to end a P2P Group in
6238 * case the local end is the Group Owner. If a virtual network interface was
6239 * created for this group, that interface will be removed. Otherwise, only the
6240 * configured P2P group network will be removed from the interface.
6242 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6244 struct wpa_global *global = wpa_s->global;
6245 struct wpa_supplicant *calling_wpa_s = wpa_s;
6247 if (os_strcmp(ifname, "*") == 0) {
6248 struct wpa_supplicant *prev;
6249 wpa_s = global->ifaces;
6252 wpa_s = wpa_s->next;
6253 if (prev->p2p_group_interface !=
6254 NOT_P2P_GROUP_INTERFACE ||
6255 (prev->current_ssid &&
6256 prev->current_ssid->p2p_group))
6257 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
6262 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6263 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6267 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
6271 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6276 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6278 if (wpa_s->best_24_freq > 0 &&
6279 p2p_supported_freq_go(wpa_s->global->p2p,
6280 wpa_s->best_24_freq)) {
6281 freq = wpa_s->best_24_freq;
6282 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6283 "channel: %d MHz", freq);
6285 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6287 freq = 2412 + (r % 3) * 25;
6288 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6289 "channel: %d MHz", freq);
6294 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6296 if (wpa_s->best_5_freq > 0 &&
6297 p2p_supported_freq_go(wpa_s->global->p2p,
6298 wpa_s->best_5_freq)) {
6299 freq = wpa_s->best_5_freq;
6300 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6301 "channel: %d MHz", freq);
6303 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6305 freq = 5180 + (r % 4) * 20;
6306 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6307 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6308 "5 GHz channel for P2P group");
6311 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6312 "channel: %d MHz", freq);
6316 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6317 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
6318 ieee80211_is_dfs(freq)) {
6320 * If freq is a DFS channel and DFS is offloaded to the
6321 * driver, allow P2P GO to use it.
6323 wpa_printf(MSG_DEBUG, "P2P: "
6324 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6328 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6329 "(%u MHz) is not supported for P2P uses",
6338 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
6339 struct p2p_go_neg_results *params,
6340 const struct p2p_channels *channels)
6344 /* first try some random selection of the social channels */
6345 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6348 for (i = 0; i < 3; i++) {
6349 params->freq = 2412 + ((r + i) % 3) * 25;
6350 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6351 freq_included(channels, params->freq) &&
6352 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6356 /* try all channels in reg. class 81 */
6357 for (i = 0; i < 11; i++) {
6358 params->freq = 2412 + i * 5;
6359 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6360 freq_included(channels, params->freq) &&
6361 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6365 /* try all channels in operating class 115 */
6366 for (i = 0; i < 4; i++) {
6367 params->freq = 5180 + i * 20;
6368 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6369 freq_included(channels, params->freq) &&
6370 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6374 /* try all channels in operating class 124 */
6375 for (i = 0; i < 4; i++) {
6376 params->freq = 5745 + i * 20;
6377 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6378 freq_included(channels, params->freq) &&
6379 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6383 /* try social channel class 180 channel 2 */
6384 params->freq = 58320 + 1 * 2160;
6385 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6386 freq_included(channels, params->freq) &&
6387 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6390 /* try all channels in reg. class 180 */
6391 for (i = 0; i < 4; i++) {
6392 params->freq = 58320 + i * 2160;
6393 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6394 freq_included(channels, params->freq) &&
6395 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6399 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
6402 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6408 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6409 struct p2p_go_neg_results *params,
6410 int freq, int ht40, int vht,
6411 const struct p2p_channels *channels)
6413 struct wpa_used_freq_data *freqs;
6414 unsigned int pref_freq, cand_freq;
6415 unsigned int num, i;
6417 os_memset(params, 0, sizeof(*params));
6418 params->role_go = 1;
6419 params->ht40 = ht40;
6422 if (!freq_included(channels, freq)) {
6423 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
6427 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
6428 "frequency %d MHz", freq);
6429 params->freq = freq;
6430 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6431 wpa_s->conf->p2p_oper_channel >= 1 &&
6432 wpa_s->conf->p2p_oper_channel <= 11 &&
6433 freq_included(channels,
6434 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6435 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6436 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6437 "frequency %d MHz", params->freq);
6438 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6439 wpa_s->conf->p2p_oper_reg_class == 116 ||
6440 wpa_s->conf->p2p_oper_reg_class == 117 ||
6441 wpa_s->conf->p2p_oper_reg_class == 124 ||
6442 wpa_s->conf->p2p_oper_reg_class == 126 ||
6443 wpa_s->conf->p2p_oper_reg_class == 127) &&
6444 freq_included(channels,
6445 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
6446 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6447 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6448 "frequency %d MHz", params->freq);
6449 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6450 wpa_s->best_overall_freq > 0 &&
6451 p2p_supported_freq_go(wpa_s->global->p2p,
6452 wpa_s->best_overall_freq) &&
6453 freq_included(channels, wpa_s->best_overall_freq)) {
6454 params->freq = wpa_s->best_overall_freq;
6455 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6456 "channel %d MHz", params->freq);
6457 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6458 wpa_s->best_24_freq > 0 &&
6459 p2p_supported_freq_go(wpa_s->global->p2p,
6460 wpa_s->best_24_freq) &&
6461 freq_included(channels, wpa_s->best_24_freq)) {
6462 params->freq = wpa_s->best_24_freq;
6463 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6464 "channel %d MHz", params->freq);
6465 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6466 wpa_s->best_5_freq > 0 &&
6467 p2p_supported_freq_go(wpa_s->global->p2p,
6468 wpa_s->best_5_freq) &&
6469 freq_included(channels, wpa_s->best_5_freq)) {
6470 params->freq = wpa_s->best_5_freq;
6471 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6472 "channel %d MHz", params->freq);
6473 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
6475 params->freq = pref_freq;
6476 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6477 "channels", params->freq);
6479 /* no preference, select some channel */
6480 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
6484 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6485 sizeof(struct wpa_used_freq_data));
6489 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
6490 wpa_s->num_multichan_concurrent);
6492 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6494 /* First try the best used frequency if possible */
6495 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
6496 params->freq = cand_freq;
6498 /* Try any of the used frequencies */
6499 for (i = 0; i < num; i++) {
6500 if (freq_included(channels, freqs[i].freq)) {
6501 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
6503 params->freq = freqs[i].freq;
6509 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
6510 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
6514 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
6518 for (i = 0; i < num; i++) {
6519 if (freqs[i].freq == freq)
6524 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
6526 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
6530 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
6540 static struct wpa_supplicant *
6541 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6544 struct wpa_supplicant *group_wpa_s;
6546 if (!wpas_p2p_create_iface(wpa_s)) {
6547 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
6549 wpa_s->p2p_first_connection_timeout = 0;
6553 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
6554 WPA_IF_P2P_CLIENT) < 0) {
6555 wpa_msg_global(wpa_s, MSG_ERROR,
6556 "P2P: Failed to add group interface");
6559 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6560 if (group_wpa_s == NULL) {
6561 wpa_msg_global(wpa_s, MSG_ERROR,
6562 "P2P: Failed to initialize group interface");
6563 wpas_p2p_remove_pending_group_interface(wpa_s);
6567 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6568 group_wpa_s->ifname);
6569 group_wpa_s->p2p_first_connection_timeout = 0;
6575 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6576 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6577 * @persistent_group: Whether to create a persistent group
6578 * @freq: Frequency for the group or 0 to indicate no hardcoding
6579 * @ht40: Start GO with 40 MHz channel width
6580 * @vht: Start GO with VHT support
6581 * Returns: 0 on success, -1 on failure
6583 * This function creates a new P2P group with the local end as the Group Owner,
6584 * i.e., without using Group Owner Negotiation.
6586 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
6587 int freq, int ht40, int vht)
6589 struct p2p_go_neg_results params;
6591 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6594 os_free(wpa_s->global->add_psk);
6595 wpa_s->global->add_psk = NULL;
6597 /* Make sure we are not running find during connection establishment */
6598 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
6599 wpas_p2p_stop_find_oper(wpa_s);
6601 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6605 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
6608 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
6609 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
6610 ieee80211_is_dfs(params.freq)) {
6612 * If freq is a DFS channel and DFS is offloaded to the
6613 * driver, allow P2P GO to use it.
6615 wpa_printf(MSG_DEBUG,
6616 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
6617 __func__, params.freq);
6619 wpa_printf(MSG_DEBUG,
6620 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
6625 p2p_go_params(wpa_s->global->p2p, ¶ms);
6626 params.persistent_group = persistent_group;
6628 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6631 wpas_start_wps_go(wpa_s, ¶ms, 0);
6637 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
6638 struct wpa_ssid *params, int addr_allocated,
6641 struct wpa_ssid *ssid;
6643 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6646 wpa_s->p2p_last_4way_hs_fail = NULL;
6648 wpa_supplicant_ap_deinit(wpa_s);
6650 ssid = wpa_config_add_network(wpa_s->conf);
6653 wpa_config_set_network_defaults(ssid);
6654 ssid->temporary = 1;
6655 ssid->proto = WPA_PROTO_RSN;
6656 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
6657 ssid->group_cipher = WPA_CIPHER_CCMP;
6658 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6659 ssid->ssid = os_malloc(params->ssid_len);
6660 if (ssid->ssid == NULL) {
6661 wpa_config_remove_network(wpa_s->conf, ssid->id);
6664 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6665 ssid->ssid_len = params->ssid_len;
6666 ssid->p2p_group = 1;
6667 ssid->export_keys = 1;
6668 if (params->psk_set) {
6669 os_memcpy(ssid->psk, params->psk, 32);
6672 if (params->passphrase)
6673 ssid->passphrase = os_strdup(params->passphrase);
6675 wpa_s->show_group_started = 1;
6676 wpa_s->p2p_in_invitation = 1;
6677 wpa_s->p2p_invite_go_freq = freq;
6679 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6681 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6682 wpas_p2p_group_formation_timeout,
6683 wpa_s->parent, NULL);
6684 wpa_supplicant_select_network(wpa_s, ssid);
6690 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6691 struct wpa_ssid *ssid, int addr_allocated,
6692 int force_freq, int neg_freq, int ht40,
6693 int vht, const struct p2p_channels *channels,
6694 int connection_timeout)
6696 struct p2p_go_neg_results params;
6699 if (ssid->disabled != 2 || ssid->ssid == NULL)
6702 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6703 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6704 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6709 os_free(wpa_s->global->add_psk);
6710 wpa_s->global->add_psk = NULL;
6712 /* Make sure we are not running find during connection establishment */
6713 wpas_p2p_stop_find_oper(wpa_s);
6715 wpa_s->p2p_fallback_to_go_neg = 0;
6717 if (ssid->mode == WPAS_MODE_P2P_GO) {
6718 if (force_freq > 0) {
6719 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6723 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6725 (freq > 0 && !freq_included(channels, freq)))
6731 (freq > 0 && !freq_included(channels, freq)))
6735 if (ssid->mode == WPAS_MODE_INFRA)
6736 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
6738 if (ssid->mode != WPAS_MODE_P2P_GO)
6741 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
6745 params.psk_set = ssid->psk_set;
6747 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
6748 if (ssid->passphrase) {
6749 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6750 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6751 "persistent group");
6754 os_strlcpy(params.passphrase, ssid->passphrase,
6755 sizeof(params.passphrase));
6757 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6758 params.ssid_len = ssid->ssid_len;
6759 params.persistent_group = 1;
6761 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6765 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6767 wpa_s->p2p_first_connection_timeout = connection_timeout;
6768 wpas_start_wps_go(wpa_s, ¶ms, 0);
6774 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6775 struct wpabuf *proberesp_ies)
6777 struct wpa_supplicant *wpa_s = ctx;
6778 if (wpa_s->ap_iface) {
6779 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
6780 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6781 wpabuf_free(beacon_ies);
6782 wpabuf_free(proberesp_ies);
6786 wpabuf_free(hapd->p2p_beacon_ie);
6787 hapd->p2p_beacon_ie = beacon_ies;
6789 wpabuf_free(hapd->p2p_probe_resp_ie);
6790 hapd->p2p_probe_resp_ie = proberesp_ies;
6792 wpabuf_free(beacon_ies);
6793 wpabuf_free(proberesp_ies);
6795 wpa_supplicant_ap_update_beacon(wpa_s);
6799 static void wpas_p2p_idle_update(void *ctx, int idle)
6801 struct wpa_supplicant *wpa_s = ctx;
6802 if (!wpa_s->ap_iface)
6804 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
6806 if (wpa_s->global->p2p_fail_on_wps_complete &&
6807 wpa_s->p2p_in_provisioning) {
6808 wpas_p2p_grpform_fail_after_wps(wpa_s);
6811 wpas_p2p_set_group_idle_timeout(wpa_s);
6813 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6817 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6818 struct wpa_ssid *ssid)
6820 struct p2p_group *group;
6821 struct p2p_group_config *cfg;
6823 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6826 cfg = os_zalloc(sizeof(*cfg));
6830 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
6831 cfg->persistent_group = 2;
6832 else if (ssid->p2p_persistent_group)
6833 cfg->persistent_group = 1;
6834 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6835 if (wpa_s->max_stations &&
6836 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6837 cfg->max_clients = wpa_s->max_stations;
6839 cfg->max_clients = wpa_s->conf->max_num_sta;
6840 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6841 cfg->ssid_len = ssid->ssid_len;
6842 cfg->freq = ssid->frequency;
6843 cfg->cb_ctx = wpa_s;
6844 cfg->ie_update = wpas_p2p_ie_update;
6845 cfg->idle_update = wpas_p2p_idle_update;
6847 group = p2p_group_init(wpa_s->global->p2p, cfg);
6850 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
6851 p2p_group_notif_formation_done(group);
6852 wpa_s->p2p_group = group;
6857 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6860 struct wpa_ssid *ssid = wpa_s->current_ssid;
6862 if (!wpa_s->p2p_in_provisioning) {
6863 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6864 "provisioning not in progress");
6868 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6869 u8 go_dev_addr[ETH_ALEN];
6870 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6871 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6873 /* Clear any stored provisioning info */
6874 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6877 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6879 wpa_s->p2p_go_group_formation_completed = 1;
6880 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6882 * Use a separate timeout for initial data connection to
6883 * complete to allow the group to be removed automatically if
6884 * something goes wrong in this step before the P2P group idle
6885 * timeout mechanism is taken into use.
6887 wpa_dbg(wpa_s, MSG_DEBUG,
6888 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6889 P2P_MAX_INITIAL_CONN_WAIT);
6890 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6891 wpas_p2p_group_formation_timeout,
6892 wpa_s->parent, NULL);
6895 * Use a separate timeout for initial data connection to
6896 * complete to allow the group to be removed automatically if
6897 * the client does not complete data connection successfully.
6899 wpa_dbg(wpa_s, MSG_DEBUG,
6900 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6901 P2P_MAX_INITIAL_CONN_WAIT_GO);
6902 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6903 wpas_p2p_group_formation_timeout,
6904 wpa_s->parent, NULL);
6906 * Complete group formation on first successful data connection
6908 wpa_s->p2p_go_group_formation_completed = 0;
6910 if (wpa_s->global->p2p)
6911 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
6912 wpas_group_formation_completed(wpa_s, 1);
6916 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6917 struct wps_event_fail *fail)
6919 if (!wpa_s->p2p_in_provisioning) {
6920 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6921 "provisioning not in progress");
6925 if (wpa_s->go_params) {
6926 p2p_clear_provisioning_info(
6928 wpa_s->go_params->peer_device_addr);
6931 wpas_notify_p2p_wps_failed(wpa_s, fail);
6933 if (wpa_s == wpa_s->global->p2p_group_formation) {
6935 * Allow some time for the failed WPS negotiation exchange to
6936 * complete, but remove the group since group formation cannot
6937 * succeed after provisioning failure.
6939 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6940 wpa_s->global->p2p_fail_on_wps_complete = 1;
6941 eloop_deplete_timeout(0, 50000,
6942 wpas_p2p_group_formation_timeout,
6943 wpa_s->parent, NULL);
6948 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6950 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6951 !wpa_s->p2p_in_provisioning)
6954 wpas_p2p_grpform_fail_after_wps(wpa_s);
6960 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6961 const char *config_method,
6962 enum wpas_p2p_prov_disc_use use,
6963 struct p2ps_provision *p2ps_prov)
6967 wpa_s->global->pending_p2ps_group = 0;
6968 wpa_s->p2p_fallback_to_go_neg = 0;
6969 wpa_s->pending_pd_use = NORMAL_PD;
6970 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6971 p2ps_prov->conncap = p2ps_group_capability(
6972 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
6973 wpa_printf(MSG_DEBUG,
6974 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6975 __func__, p2ps_prov->conncap,
6976 p2ps_prov->adv_id, p2ps_prov->conncap,
6977 p2ps_prov->status, p2ps_prov->info);
6980 } else if (os_strncmp(config_method, "display", 7) == 0)
6981 config_methods = WPS_CONFIG_DISPLAY;
6982 else if (os_strncmp(config_method, "keypad", 6) == 0)
6983 config_methods = WPS_CONFIG_KEYPAD;
6984 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6985 os_strncmp(config_method, "pushbutton", 10) == 0)
6986 config_methods = WPS_CONFIG_PUSHBUTTON;
6988 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
6993 if (use == WPAS_P2P_PD_AUTO) {
6994 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6995 wpa_s->pending_pd_config_methods = config_methods;
6996 wpa_s->p2p_auto_pd = 1;
6997 wpa_s->p2p_auto_join = 0;
6998 wpa_s->pending_pd_before_join = 0;
6999 wpa_s->auto_pd_scan_retry = 0;
7000 wpas_p2p_stop_find(wpa_s);
7001 wpa_s->p2p_join_scan_count = 0;
7002 os_get_reltime(&wpa_s->p2p_auto_started);
7003 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7004 wpa_s->p2p_auto_started.sec,
7005 wpa_s->p2p_auto_started.usec);
7006 wpas_p2p_join_scan(wpa_s, NULL);
7010 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7015 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
7016 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
7021 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7024 return p2p_scan_result_text(ies, ies_len, buf, end);
7028 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7030 if (!offchannel_pending_action_tx(wpa_s))
7033 wpas_p2p_action_tx_clear(wpa_s);
7035 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7036 "operation request");
7037 offchannel_clear_pending_action_tx(wpa_s);
7041 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7042 enum p2p_discovery_type type,
7043 unsigned int num_req_dev_types, const u8 *req_dev_types,
7044 const u8 *dev_id, unsigned int search_delay,
7045 u8 seek_cnt, const char **seek_string, int freq)
7047 wpas_p2p_clear_pending_action_tx(wpa_s);
7048 wpa_s->p2p_long_listen = 0;
7050 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7051 wpa_s->p2p_in_provisioning)
7054 wpa_supplicant_cancel_sched_scan(wpa_s);
7056 return p2p_find(wpa_s->global->p2p, timeout, type,
7057 num_req_dev_types, req_dev_types, dev_id,
7058 search_delay, seek_cnt, seek_string, freq);
7062 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7063 struct wpa_scan_results *scan_res)
7065 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7067 if (wpa_s->p2p_scan_work) {
7068 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7069 wpa_s->p2p_scan_work = NULL;
7070 radio_work_done(work);
7073 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7077 * Indicate that results have been processed so that the P2P module can
7078 * continue pending tasks.
7080 p2p_scan_res_handled(wpa_s->global->p2p);
7084 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
7086 wpas_p2p_clear_pending_action_tx(wpa_s);
7087 wpa_s->p2p_long_listen = 0;
7088 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7089 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
7091 if (wpa_s->global->p2p)
7092 p2p_stop_find(wpa_s->global->p2p);
7094 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7095 wpa_printf(MSG_DEBUG,
7096 "P2P: Do not consider the scan results after stop_find");
7097 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7102 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7104 wpas_p2p_stop_find_oper(wpa_s);
7105 if (!wpa_s->global->pending_group_iface_for_p2ps)
7106 wpas_p2p_remove_pending_group_interface(wpa_s);
7110 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7112 struct wpa_supplicant *wpa_s = eloop_ctx;
7113 wpa_s->p2p_long_listen = 0;
7117 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7121 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7124 wpa_supplicant_cancel_sched_scan(wpa_s);
7125 wpas_p2p_clear_pending_action_tx(wpa_s);
7129 * This is a request for unlimited Listen state. However, at
7130 * least for now, this is mapped to a Listen state for one
7135 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7136 wpa_s->p2p_long_listen = 0;
7139 * Stop previous find/listen operation to avoid trying to request a new
7140 * remain-on-channel operation while the driver is still running the
7143 if (wpa_s->global->p2p)
7144 p2p_stop_find(wpa_s->global->p2p);
7146 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7147 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
7148 wpa_s->p2p_long_listen = timeout * 1000;
7149 eloop_register_timeout(timeout, 0,
7150 wpas_p2p_long_listen_timeout,
7158 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7159 u8 *buf, size_t len, int p2p_group)
7161 struct wpabuf *p2p_ie;
7164 if (wpa_s->global->p2p_disabled)
7166 if (wpa_s->conf->p2p_disabled)
7168 if (wpa_s->global->p2p == NULL)
7173 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7174 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7176 wpabuf_free(p2p_ie);
7182 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
7183 const u8 *dst, const u8 *bssid,
7184 const u8 *ie, size_t ie_len, int ssi_signal)
7186 if (wpa_s->global->p2p_disabled)
7188 if (wpa_s->global->p2p == NULL)
7191 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
7193 case P2P_PREQ_NOT_P2P:
7194 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7197 case P2P_PREQ_MALFORMED:
7198 case P2P_PREQ_NOT_LISTEN:
7199 case P2P_PREQ_NOT_PROCESSED:
7200 default: /* make gcc happy */
7202 case P2P_PREQ_PROCESSED:
7208 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7209 const u8 *sa, const u8 *bssid,
7210 u8 category, const u8 *data, size_t len, int freq)
7212 if (wpa_s->global->p2p_disabled)
7214 if (wpa_s->global->p2p == NULL)
7217 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7222 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7224 if (wpa_s->global->p2p_disabled)
7226 if (wpa_s->global->p2p == NULL)
7229 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
7233 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
7235 p2p_group_deinit(wpa_s->p2p_group);
7236 wpa_s->p2p_group = NULL;
7238 wpa_s->ap_configured_cb = NULL;
7239 wpa_s->ap_configured_cb_ctx = NULL;
7240 wpa_s->ap_configured_cb_data = NULL;
7241 wpa_s->connect_without_scan = NULL;
7245 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7247 wpa_s->p2p_long_listen = 0;
7249 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7252 return p2p_reject(wpa_s->global->p2p, addr);
7256 /* Invite to reinvoke a persistent group */
7257 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7258 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
7259 int ht40, int vht, int pref_freq)
7261 enum p2p_invite_role role;
7265 int no_pref_freq_given = pref_freq == 0;
7267 wpa_s->global->p2p_invite_group = NULL;
7269 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7271 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7273 wpa_s->p2p_persistent_go_freq = freq;
7274 wpa_s->p2p_go_ht40 = !!ht40;
7275 if (ssid->mode == WPAS_MODE_P2P_GO) {
7276 role = P2P_INVITE_ROLE_GO;
7277 if (peer_addr == NULL) {
7278 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7279 "address in invitation command");
7282 if (wpas_p2p_create_iface(wpa_s)) {
7283 if (wpas_p2p_add_group_interface(wpa_s,
7284 WPA_IF_P2P_GO) < 0) {
7285 wpa_printf(MSG_ERROR, "P2P: Failed to "
7286 "allocate a new interface for the "
7290 bssid = wpa_s->pending_interface_addr;
7292 bssid = wpa_s->own_addr;
7294 role = P2P_INVITE_ROLE_CLIENT;
7295 peer_addr = ssid->bssid;
7297 wpa_s->pending_invite_ssid_id = ssid->id;
7299 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7300 role == P2P_INVITE_ROLE_GO);
7304 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7307 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7308 no_pref_freq_given && pref_freq > 0 &&
7309 wpa_s->num_multichan_concurrent > 1 &&
7310 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7311 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7317 * Stop any find/listen operations before invitation and possibly
7318 * connection establishment.
7320 wpas_p2p_stop_find_oper(wpa_s);
7322 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
7323 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
7328 /* Invite to join an active group */
7329 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7330 const u8 *peer_addr, const u8 *go_dev_addr)
7332 struct wpa_global *global = wpa_s->global;
7333 enum p2p_invite_role role;
7335 struct wpa_ssid *ssid;
7337 int freq = 0, force_freq = 0, pref_freq = 0;
7340 wpa_s->p2p_persistent_go_freq = 0;
7341 wpa_s->p2p_go_ht40 = 0;
7342 wpa_s->p2p_go_vht = 0;
7344 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7345 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7348 if (wpa_s == NULL) {
7349 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7353 ssid = wpa_s->current_ssid;
7355 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7360 wpa_s->global->p2p_invite_group = wpa_s;
7361 persistent = ssid->p2p_persistent_group &&
7362 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
7363 ssid->ssid, ssid->ssid_len);
7365 if (ssid->mode == WPAS_MODE_P2P_GO) {
7366 role = P2P_INVITE_ROLE_ACTIVE_GO;
7367 bssid = wpa_s->own_addr;
7368 if (go_dev_addr == NULL)
7369 go_dev_addr = wpa_s->global->p2p_dev_addr;
7370 freq = ssid->frequency;
7372 role = P2P_INVITE_ROLE_CLIENT;
7373 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7374 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7375 "invite to current group");
7378 bssid = wpa_s->bssid;
7379 if (go_dev_addr == NULL &&
7380 !is_zero_ether_addr(wpa_s->go_dev_addr))
7381 go_dev_addr = wpa_s->go_dev_addr;
7382 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7383 (int) wpa_s->assoc_freq;
7385 wpa_s->parent->pending_invite_ssid_id = -1;
7387 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7390 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7391 role == P2P_INVITE_ROLE_ACTIVE_GO);
7394 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
7396 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
7397 ssid->ssid, ssid->ssid_len, force_freq,
7398 go_dev_addr, persistent, pref_freq, -1);
7402 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7404 struct wpa_ssid *ssid = wpa_s->current_ssid;
7405 u8 go_dev_addr[ETH_ALEN];
7406 int network_id = -1;
7412 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7413 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7414 wpa_s->parent, NULL);
7417 if (!wpa_s->show_group_started || !ssid)
7420 wpa_s->show_group_started = 0;
7422 os_memset(go_dev_addr, 0, ETH_ALEN);
7423 if (ssid->bssid_set)
7424 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7425 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7427 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7429 if (wpa_s->global->p2p_group_formation == wpa_s)
7430 wpa_s->global->p2p_group_formation = NULL;
7432 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7433 (int) wpa_s->assoc_freq;
7436 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
7439 res = os_snprintf(ip_addr, sizeof(ip_addr),
7440 " ip_addr=%u.%u.%u.%u "
7441 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7442 ip[0], ip[1], ip[2], ip[3],
7443 ip[4], ip[5], ip[6], ip[7],
7444 ip[8], ip[9], ip[10], ip[11]);
7445 if (os_snprintf_error(sizeof(ip_addr), res))
7449 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7450 ssid->passphrase == NULL && ssid->psk_set ?
7452 ssid->passphrase, go_dev_addr, persistent,
7456 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
7459 network_id = ssid->id;
7460 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
7464 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7465 u32 interval1, u32 duration2, u32 interval2)
7469 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7472 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7473 wpa_s->current_ssid == NULL ||
7474 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7477 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7478 wpa_s->own_addr, wpa_s->assoc_freq,
7479 duration1, interval1, duration2, interval2);
7481 wpa_s->waiting_presence_resp = 1;
7487 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7488 unsigned int interval)
7490 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7493 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7497 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7499 if (wpa_s->current_ssid == NULL) {
7501 * current_ssid can be cleared when P2P client interface gets
7502 * disconnected, so assume this interface was used as P2P
7507 return wpa_s->current_ssid->p2p_group &&
7508 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7512 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7514 struct wpa_supplicant *wpa_s = eloop_ctx;
7516 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
7517 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7522 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7524 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
7528 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7532 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7533 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7535 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7538 timeout = wpa_s->conf->p2p_group_idle;
7539 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7540 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7541 timeout = P2P_MAX_CLIENT_IDLE;
7547 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7548 timeout = 0; /* special client mode no-timeout */
7553 if (wpa_s->p2p_in_provisioning) {
7555 * Use the normal group formation timeout during the
7556 * provisioning phase to avoid terminating this process too
7557 * early due to group idle timeout.
7559 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7560 "during provisioning");
7564 if (wpa_s->show_group_started) {
7566 * Use the normal group formation timeout between the end of
7567 * the provisioning phase and completion of 4-way handshake to
7568 * avoid terminating this process too early due to group idle
7571 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7572 "while waiting for initial 4-way handshake to "
7577 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
7579 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7584 /* Returns 1 if the interface was removed */
7585 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7586 u16 reason_code, const u8 *ie, size_t ie_len,
7587 int locally_generated)
7589 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7592 if (!locally_generated)
7593 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7596 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7597 wpa_s->current_ssid &&
7598 wpa_s->current_ssid->p2p_group &&
7599 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7600 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7601 "session is ending");
7602 if (wpas_p2p_group_delete(wpa_s,
7603 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7612 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7613 u16 reason_code, const u8 *ie, size_t ie_len,
7614 int locally_generated)
7616 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7619 if (!locally_generated)
7620 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7625 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7627 struct p2p_data *p2p = wpa_s->global->p2p;
7632 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7635 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7636 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7638 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7639 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7642 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7643 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7645 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7646 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7648 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7649 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7650 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7651 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7652 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7655 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7656 p2p_set_sec_dev_types(p2p,
7657 (void *) wpa_s->conf->sec_device_type,
7658 wpa_s->conf->num_sec_device_types);
7660 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7662 p2p_remove_wps_vendor_extensions(p2p);
7663 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7664 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7666 p2p_add_wps_vendor_extension(
7667 p2p, wpa_s->conf->wps_vendor_ext[i]);
7671 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7672 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7674 country[0] = wpa_s->conf->country[0];
7675 country[1] = wpa_s->conf->country[1];
7677 p2p_set_country(p2p, country);
7680 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7681 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7682 wpa_s->conf->p2p_ssid_postfix ?
7683 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7687 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7688 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
7690 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7691 u8 reg_class, channel;
7696 if (wpa_s->conf->p2p_listen_reg_class &&
7697 wpa_s->conf->p2p_listen_channel) {
7698 reg_class = wpa_s->conf->p2p_listen_reg_class;
7699 channel = wpa_s->conf->p2p_listen_channel;
7704 * Pick one of the social channels randomly as the
7707 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7710 channel = 1 + (r % 3) * 5;
7713 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7716 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7719 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7720 u8 op_reg_class, op_channel, cfg_op_channel;
7723 if (wpa_s->conf->p2p_oper_reg_class &&
7724 wpa_s->conf->p2p_oper_channel) {
7725 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7726 op_channel = wpa_s->conf->p2p_oper_channel;
7731 * Use random operation channel from (1, 6, 11)
7732 *if no other preference is indicated.
7734 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7737 op_channel = 1 + (r % 3) * 5;
7740 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7743 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7747 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7748 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7749 wpa_s->conf->p2p_pref_chan) < 0) {
7750 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7754 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7755 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7760 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7761 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
7765 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7768 if (!wpa_s->ap_iface)
7770 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7775 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7777 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7780 wpa_s->global->cross_connection = enabled;
7781 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7784 struct wpa_supplicant *iface;
7786 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7788 if (iface->cross_connect_enabled == 0)
7791 iface->cross_connect_enabled = 0;
7792 iface->cross_connect_in_use = 0;
7793 wpa_msg_global(iface->parent, MSG_INFO,
7794 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7796 iface->cross_connect_uplink);
7804 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7806 struct wpa_supplicant *iface;
7808 if (!uplink->global->cross_connection)
7811 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7812 if (!iface->cross_connect_enabled)
7814 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7817 if (iface->ap_iface == NULL)
7819 if (iface->cross_connect_in_use)
7822 iface->cross_connect_in_use = 1;
7823 wpa_msg_global(iface->parent, MSG_INFO,
7824 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7825 iface->ifname, iface->cross_connect_uplink);
7830 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7832 struct wpa_supplicant *iface;
7834 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7835 if (!iface->cross_connect_enabled)
7837 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7840 if (!iface->cross_connect_in_use)
7843 wpa_msg_global(iface->parent, MSG_INFO,
7844 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7845 iface->ifname, iface->cross_connect_uplink);
7846 iface->cross_connect_in_use = 0;
7851 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7853 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7854 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7855 wpa_s->cross_connect_disallowed)
7856 wpas_p2p_disable_cross_connect(wpa_s);
7858 wpas_p2p_enable_cross_connect(wpa_s);
7859 if (!wpa_s->ap_iface &&
7860 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7861 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7865 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7867 wpas_p2p_disable_cross_connect(wpa_s);
7868 if (!wpa_s->ap_iface &&
7869 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7871 wpas_p2p_set_group_idle_timeout(wpa_s);
7875 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7877 struct wpa_supplicant *iface;
7879 if (!wpa_s->global->cross_connection)
7882 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7885 if (iface->drv_flags &
7886 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7888 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7889 iface != wpa_s->parent)
7892 wpa_s->cross_connect_enabled = 1;
7893 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7894 sizeof(wpa_s->cross_connect_uplink));
7895 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7896 "%s to %s whenever uplink is available",
7897 wpa_s->ifname, wpa_s->cross_connect_uplink);
7899 if (iface->ap_iface || iface->current_ssid == NULL ||
7900 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7901 iface->cross_connect_disallowed ||
7902 iface->wpa_state != WPA_COMPLETED)
7905 wpa_s->cross_connect_in_use = 1;
7906 wpa_msg_global(wpa_s->parent, MSG_INFO,
7907 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7908 wpa_s->ifname, wpa_s->cross_connect_uplink);
7914 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7916 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7917 !wpa_s->p2p_in_provisioning)
7918 return 0; /* not P2P client operation */
7920 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7922 if (wpa_s != wpa_s->parent)
7923 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
7924 wpas_p2p_group_formation_failed(wpa_s);
7929 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7931 struct wpa_supplicant *wpa_s = eloop_ctx;
7932 wpas_p2p_notif_pbc_overlap(wpa_s);
7936 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
7938 struct p2p_channels chan, cli_chan;
7939 struct wpa_supplicant *ifs;
7941 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7944 os_memset(&chan, 0, sizeof(chan));
7945 os_memset(&cli_chan, 0, sizeof(cli_chan));
7946 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
7947 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7952 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
7954 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
7956 if (!ifs->current_ssid ||
7957 !ifs->current_ssid->p2p_group ||
7958 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
7959 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
7961 freq = ifs->current_ssid->frequency;
7962 if (freq_included(&chan, freq)) {
7963 wpa_dbg(ifs, MSG_DEBUG,
7964 "P2P GO operating frequency %d MHz in valid range",
7969 wpa_dbg(ifs, MSG_DEBUG,
7970 "P2P GO operating in invalid frequency %d MHz", freq);
7971 /* TODO: Consider using CSA or removing the group within
7973 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
7978 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7979 struct wpa_scan_results *scan_res)
7981 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7985 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7987 struct wpa_global *global = wpa_s->global;
7991 if (global->p2p == NULL)
7994 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7996 if (wpa_s->pending_interface_name[0] &&
7997 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8000 peer = p2p_get_go_neg_peer(global->p2p);
8002 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8003 MACSTR, MAC2STR(peer));
8004 p2p_unauthorize(global->p2p, peer);
8008 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8009 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8010 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8014 if (wpa_s->pending_pd_before_join) {
8015 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8016 wpa_s->pending_pd_before_join = 0;
8020 wpas_p2p_stop_find(wpa_s);
8022 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8023 if (wpa_s == global->p2p_group_formation &&
8024 (wpa_s->p2p_in_provisioning ||
8025 wpa_s->parent->pending_interface_type ==
8026 WPA_IF_P2P_CLIENT)) {
8027 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8028 "formation found - cancelling",
8031 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8032 wpa_s->parent, NULL);
8033 if (wpa_s->p2p_in_provisioning) {
8034 wpas_group_formation_completed(wpa_s, 0);
8037 wpas_p2p_group_delete(wpa_s,
8038 P2P_GROUP_REMOVAL_REQUESTED);
8040 } else if (wpa_s->p2p_in_invitation) {
8041 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8044 wpas_p2p_group_formation_failed(wpa_s);
8049 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8057 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8059 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8062 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8063 "being available anymore");
8064 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
8068 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8069 int freq_24, int freq_5, int freq_overall)
8071 struct p2p_data *p2p = wpa_s->global->p2p;
8074 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8078 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8081 struct p2p_data *p2p = wpa_s->global->p2p;
8086 if (hwaddr_aton(addr, peer))
8089 return p2p_unauthorize(p2p, peer);
8094 * wpas_p2p_disconnect - Disconnect from a P2P Group
8095 * @wpa_s: Pointer to wpa_supplicant data
8096 * Returns: 0 on success, -1 on failure
8098 * This can be used to disconnect from a group in which the local end is a P2P
8099 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8100 * virtual network interface was created for this group, that interface will be
8101 * removed. Otherwise, only the configured P2P group network will be removed
8102 * from the interface.
8104 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8110 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8115 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8119 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8122 ret = p2p_in_progress(wpa_s->global->p2p);
8125 * Check whether there is an ongoing WPS provisioning step (or
8126 * other parts of group formation) on another interface since
8127 * p2p_in_progress() does not report this to avoid issues for
8128 * scans during such provisioning step.
8130 if (wpa_s->global->p2p_group_formation &&
8131 wpa_s->global->p2p_group_formation != wpa_s) {
8132 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8133 "in group formation",
8134 wpa_s->global->p2p_group_formation->ifname);
8139 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
8140 struct os_reltime now;
8141 os_get_reltime(&now);
8142 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8143 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
8144 /* Wait for the first client has expired */
8145 wpa_s->global->p2p_go_wait_client.sec = 0;
8147 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8156 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8157 struct wpa_ssid *ssid)
8159 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8160 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8161 wpa_s->parent, NULL) > 0) {
8163 * Remove the network by scheduling the group formation
8164 * timeout to happen immediately. The teardown code
8165 * needs to be scheduled to run asynch later so that we
8166 * don't delete data from under ourselves unexpectedly.
8167 * Calling wpas_p2p_group_formation_timeout directly
8168 * causes a series of crashes in WPS failure scenarios.
8170 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8171 "P2P group network getting removed");
8172 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
8173 wpa_s->parent, NULL);
8178 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
8179 const u8 *addr, const u8 *ssid,
8185 for (s = wpa_s->conf->ssid; s; s = s->next) {
8186 if (s->disabled != 2)
8189 (ssid_len != s->ssid_len ||
8190 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8193 if (s->mode == WPAS_MODE_P2P_GO)
8197 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8198 return s; /* peer is GO in the persistent group */
8199 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8201 for (i = 0; i < s->num_p2p_clients; i++) {
8202 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
8203 addr, ETH_ALEN) == 0)
8204 return s; /* peer is P2P client in persistent
8213 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8216 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8217 wpa_s->parent, NULL) > 0) {
8219 * This can happen if WPS provisioning step is not terminated
8220 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8221 * peer was able to connect, there is no need to time out group
8222 * formation after this, though. In addition, this is used with
8223 * the initial connection wait on the GO as a separate formation
8224 * timeout and as such, expected to be hit after the initial WPS
8225 * provisioning step.
8227 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
8229 if (!wpa_s->p2p_go_group_formation_completed &&
8230 !wpa_s->group_formation_reported) {
8232 * GO has not yet notified group formation success since
8233 * the WPS step was not completed cleanly. Do that
8234 * notification now since the P2P Client was able to
8235 * connect and as such, must have received the
8236 * credential from the WPS step.
8238 if (wpa_s->global->p2p)
8239 p2p_wps_success_cb(wpa_s->global->p2p, addr);
8240 wpas_group_formation_completed(wpa_s, 1);
8243 if (!wpa_s->p2p_go_group_formation_completed) {
8244 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8245 wpa_s->p2p_go_group_formation_completed = 1;
8246 wpa_s->global->p2p_group_formation = NULL;
8247 wpa_s->p2p_in_provisioning = 0;
8248 wpa_s->p2p_in_invitation = 0;
8250 wpa_s->global->p2p_go_wait_client.sec = 0;
8253 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8257 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8260 struct wpa_supplicant *group = wpa_s;
8263 if (wpa_s->global->p2p_group_formation)
8264 group = wpa_s->global->p2p_group_formation;
8265 wpa_s = wpa_s->parent;
8266 offchannel_send_action_done(wpa_s);
8268 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
8269 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8270 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8271 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8272 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
8273 wpa_s->p2p_persistent_id,
8274 wpa_s->p2p_pd_before_go_neg,
8281 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8285 if (!wpa_s->p2p_fallback_to_go_neg ||
8286 wpa_s->p2p_in_provisioning <= 5)
8289 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8290 return 0; /* peer operating as a GO */
8292 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8293 "fallback to GO Negotiation");
8294 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
8295 "reason=GO-not-found");
8296 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
8298 return res == 1 ? 2 : 1;
8302 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8304 struct wpa_supplicant *ifs;
8306 if (wpa_s->wpa_state > WPA_SCANNING) {
8307 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8308 "concurrent operation",
8309 wpa_s->conf->p2p_search_delay);
8310 return wpa_s->conf->p2p_search_delay;
8313 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8315 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
8316 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8317 "delay due to concurrent operation on "
8319 wpa_s->conf->p2p_search_delay,
8321 return wpa_s->conf->p2p_search_delay;
8329 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8330 struct wpa_ssid *s, const u8 *addr,
8333 struct psk_list_entry *psk, *tmp;
8336 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8338 if ((iface_addr && !psk->p2p &&
8339 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8340 (!iface_addr && psk->p2p &&
8341 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8342 wpa_dbg(wpa_s, MSG_DEBUG,
8343 "P2P: Remove persistent group PSK list entry for "
8345 MAC2STR(psk->addr), psk->p2p);
8346 dl_list_del(&psk->list);
8356 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8357 const u8 *p2p_dev_addr,
8358 const u8 *psk, size_t psk_len)
8360 struct wpa_ssid *ssid = wpa_s->current_ssid;
8361 struct wpa_ssid *persistent;
8362 struct psk_list_entry *p, *last;
8364 if (psk_len != sizeof(p->psk))
8368 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8369 " p2p_dev_addr=" MACSTR,
8370 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8371 if (is_zero_ether_addr(p2p_dev_addr))
8372 p2p_dev_addr = NULL;
8374 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8378 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8379 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8380 /* To be added to persistent group once created */
8381 if (wpa_s->global->add_psk == NULL) {
8382 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8383 if (wpa_s->global->add_psk == NULL)
8386 p = wpa_s->global->add_psk;
8389 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8392 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8394 os_memcpy(p->psk, psk, psk_len);
8398 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8399 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8403 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
8406 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8410 p = os_zalloc(sizeof(*p));
8415 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8418 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8420 os_memcpy(p->psk, psk, psk_len);
8422 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8423 (last = dl_list_last(&persistent->psk_list,
8424 struct psk_list_entry, list))) {
8425 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8426 MACSTR " (p2p=%u) to make room for a new one",
8427 MAC2STR(last->addr), last->p2p);
8428 dl_list_del(&last->list);
8432 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
8433 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8434 p2p_dev_addr == NULL);
8436 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8437 MACSTR, MAC2STR(p2p_dev_addr));
8439 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8442 dl_list_add(&persistent->psk_list, &p->list);
8444 if (wpa_s->parent->conf->update_config &&
8445 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
8446 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
8450 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8451 struct wpa_ssid *s, const u8 *addr,
8456 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
8457 if (res > 0 && wpa_s->conf->update_config &&
8458 wpa_config_write(wpa_s->confname, wpa_s->conf))
8459 wpa_dbg(wpa_s, MSG_DEBUG,
8460 "P2P: Failed to update configuration");
8464 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8465 const u8 *peer, int iface_addr)
8467 struct hostapd_data *hapd;
8468 struct hostapd_wpa_psk *psk, *prev, *rem;
8469 struct sta_info *sta;
8471 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8472 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8475 /* Remove per-station PSK entry */
8476 hapd = wpa_s->ap_iface->bss[0];
8478 psk = hapd->conf->ssid.wpa_psk;
8480 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8482 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8483 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8484 MACSTR " iface_addr=%d",
8485 MAC2STR(peer), iface_addr);
8487 prev->next = psk->next;
8489 hapd->conf->ssid.wpa_psk = psk->next;
8499 /* Disconnect from group */
8501 sta = ap_get_sta(hapd, peer);
8503 sta = ap_get_sta_p2p(hapd, peer);
8505 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8506 " (iface_addr=%d) from group",
8507 MAC2STR(peer), iface_addr);
8508 hostapd_drv_sta_deauth(hapd, sta->addr,
8509 WLAN_REASON_DEAUTH_LEAVING);
8510 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8515 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8519 struct wpa_supplicant *w;
8521 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8523 /* Remove from any persistent group */
8524 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
8525 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8528 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
8529 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
8532 /* Remove from any operating group */
8533 for (w = wpa_s->global->ifaces; w; w = w->next)
8534 wpas_p2p_remove_client_go(w, peer, iface_addr);
8538 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8540 struct wpa_supplicant *wpa_s = eloop_ctx;
8541 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8545 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8547 struct wpa_supplicant *wpa_s = eloop_ctx;
8549 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8550 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8554 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8555 struct wpa_ssid *ssid)
8557 struct wpa_supplicant *iface;
8559 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8560 if (!iface->current_ssid ||
8561 iface->current_ssid->frequency == freq ||
8562 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8563 !iface->current_ssid->p2p_group))
8566 /* Remove the connection with least priority */
8567 if (!wpas_is_p2p_prioritized(iface)) {
8568 /* STA connection has priority over existing
8569 * P2P connection, so remove the interface. */
8570 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8571 eloop_register_timeout(0, 0,
8572 wpas_p2p_group_freq_conflict,
8574 /* If connection in progress is P2P connection, do not
8575 * proceed for the connection. */
8581 /* P2P connection has priority, disable the STA network
8583 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8585 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8586 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8587 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8589 /* If P2P connection is in progress, continue
8602 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8604 struct wpa_ssid *ssid = wpa_s->current_ssid;
8606 if (ssid == NULL || !ssid->p2p_group)
8609 if (wpa_s->p2p_last_4way_hs_fail &&
8610 wpa_s->p2p_last_4way_hs_fail == ssid) {
8611 u8 go_dev_addr[ETH_ALEN];
8612 struct wpa_ssid *persistent;
8614 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8616 ssid->ssid_len) <= 0) {
8617 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8621 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8622 MACSTR, MAC2STR(go_dev_addr));
8623 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
8626 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8627 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8630 wpa_msg_global(wpa_s->parent, MSG_INFO,
8631 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8634 wpa_s->p2p_last_4way_hs_fail = NULL;
8636 * Remove the group from a timeout to avoid issues with caller
8637 * continuing to use the interface if this is on a P2P group
8640 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8645 wpa_s->p2p_last_4way_hs_fail = ssid;
8650 #ifdef CONFIG_WPS_NFC
8652 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8660 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8664 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8665 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8672 wpabuf_put_be16(ret, wsc_len);
8674 wpabuf_put_buf(ret, wsc);
8675 wpabuf_put_be16(ret, wpabuf_len(p2p));
8676 wpabuf_put_buf(ret, p2p);
8680 wpa_hexdump_buf(MSG_DEBUG,
8681 "P2P: Generated NFC connection handover message", ret);
8685 tmp = ndef_build_p2p(ret);
8688 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8698 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8699 struct wpa_ssid **ssid, u8 *go_dev_addr)
8701 struct wpa_supplicant *iface;
8704 os_memset(go_dev_addr, 0, ETH_ALEN);
8707 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8708 if (iface->wpa_state < WPA_ASSOCIATING ||
8709 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8710 !iface->current_ssid->p2p_group ||
8711 iface->current_ssid->mode != WPAS_MODE_INFRA)
8714 *ssid = iface->current_ssid;
8716 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8717 return iface->assoc_freq;
8723 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8726 struct wpabuf *wsc, *p2p;
8727 struct wpa_ssid *ssid;
8728 u8 go_dev_addr[ETH_ALEN];
8729 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8731 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8732 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8736 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8737 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8738 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8739 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8743 if (cli_freq == 0) {
8744 wsc = wps_build_nfc_handover_req_p2p(
8745 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8748 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8749 go_dev_addr, ssid ? ssid->ssid : NULL,
8750 ssid ? ssid->ssid_len : 0);
8752 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8756 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8759 struct wpabuf *wsc, *p2p;
8760 struct wpa_ssid *ssid;
8761 u8 go_dev_addr[ETH_ALEN];
8762 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8764 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8767 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8768 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8769 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8772 if (cli_freq == 0) {
8773 wsc = wps_build_nfc_handover_sel_p2p(
8775 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8776 DEV_PW_NFC_CONNECTION_HANDOVER,
8777 wpa_s->conf->wps_nfc_dh_pubkey,
8778 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8781 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8782 go_dev_addr, ssid ? ssid->ssid : NULL,
8783 ssid ? ssid->ssid_len : 0);
8785 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8789 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8790 struct p2p_nfc_params *params)
8792 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8793 "connection handover (freq=%d)",
8796 if (params->go_freq && params->go_ssid_len) {
8797 wpa_s->p2p_wps_method = WPS_NFC;
8798 wpa_s->pending_join_wps_method = WPS_NFC;
8799 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8800 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8802 return wpas_p2p_join_start(wpa_s, params->go_freq,
8804 params->go_ssid_len);
8807 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8808 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
8809 params->go_freq, -1, 0, 1, 1);
8813 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8814 struct p2p_nfc_params *params, int tag)
8816 int res, persistent;
8817 struct wpa_ssid *ssid;
8819 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8820 "connection handover");
8821 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8822 ssid = wpa_s->current_ssid;
8825 if (ssid->mode != WPAS_MODE_P2P_GO)
8827 if (wpa_s->ap_iface == NULL)
8831 if (wpa_s == NULL) {
8832 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8836 if (wpa_s->parent->p2p_oob_dev_pw_id !=
8837 DEV_PW_NFC_CONNECTION_HANDOVER &&
8838 !wpa_s->parent->p2p_oob_dev_pw) {
8839 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8842 res = wpas_ap_wps_add_nfc_pw(
8843 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
8844 wpa_s->parent->p2p_oob_dev_pw,
8845 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
8846 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
8851 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8855 if (!params->peer ||
8856 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8859 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8860 " to join", MAC2STR(params->peer->p2p_device_addr));
8862 wpa_s->global->p2p_invite_group = wpa_s;
8863 persistent = ssid->p2p_persistent_group &&
8864 wpas_p2p_get_persistent(wpa_s->parent,
8865 params->peer->p2p_device_addr,
8866 ssid->ssid, ssid->ssid_len);
8867 wpa_s->parent->pending_invite_ssid_id = -1;
8869 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8870 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8871 ssid->ssid, ssid->ssid_len, ssid->frequency,
8872 wpa_s->global->p2p_dev_addr, persistent, 0,
8873 wpa_s->parent->p2p_oob_dev_pw_id);
8877 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8878 struct p2p_nfc_params *params,
8881 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8882 "connection handover");
8883 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8884 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8885 forced_freq, -1, 0, 1, 1);
8889 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8890 struct p2p_nfc_params *params,
8895 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8896 "connection handover");
8897 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8898 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8899 forced_freq, -1, 0, 1, 1);
8903 res = wpas_p2p_listen(wpa_s, 60);
8905 p2p_unauthorize(wpa_s->global->p2p,
8906 params->peer->p2p_device_addr);
8913 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8914 const struct wpabuf *data,
8915 int sel, int tag, int forced_freq)
8917 const u8 *pos, *end;
8919 struct p2p_nfc_params params;
8922 os_memset(¶ms, 0, sizeof(params));
8925 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8927 pos = wpabuf_head(data);
8928 end = pos + wpabuf_len(data);
8930 if (end - pos < 2) {
8931 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8935 len = WPA_GET_BE16(pos);
8937 if (len > end - pos) {
8938 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8942 params.wsc_attr = pos;
8943 params.wsc_len = len;
8946 if (end - pos < 2) {
8947 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8951 len = WPA_GET_BE16(pos);
8953 if (len > end - pos) {
8954 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8958 params.p2p_attr = pos;
8959 params.p2p_len = len;
8962 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8963 params.wsc_attr, params.wsc_len);
8964 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8965 params.p2p_attr, params.p2p_len);
8967 wpa_hexdump(MSG_DEBUG,
8968 "P2P: Ignored extra data after P2P attributes",
8972 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
8976 if (params.next_step == NO_ACTION)
8979 if (params.next_step == BOTH_GO) {
8980 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8981 MAC2STR(params.peer->p2p_device_addr));
8985 if (params.next_step == PEER_CLIENT) {
8986 if (!is_zero_ether_addr(params.go_dev_addr)) {
8987 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8988 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8990 MAC2STR(params.peer->p2p_device_addr),
8992 MAC2STR(params.go_dev_addr),
8993 wpa_ssid_txt(params.go_ssid,
8994 params.go_ssid_len));
8996 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8997 "peer=" MACSTR " freq=%d",
8998 MAC2STR(params.peer->p2p_device_addr),
9004 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9005 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9006 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9010 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9011 wpa_s->p2p_oob_dev_pw = NULL;
9013 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9014 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9019 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9020 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9021 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9022 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9023 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9024 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9025 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9029 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9030 "peer OOB Device Password Id %u", id);
9033 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9034 "Device Password Id %u", id);
9035 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9036 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9037 params.oob_dev_pw_len -
9038 WPS_OOB_PUBKEY_HASH_LEN - 2);
9039 wpa_s->p2p_oob_dev_pw_id = id;
9040 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9041 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9042 params.oob_dev_pw_len -
9043 WPS_OOB_PUBKEY_HASH_LEN - 2);
9044 if (wpa_s->p2p_oob_dev_pw == NULL)
9047 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9048 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9049 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9052 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9053 "without Device Password");
9054 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9057 switch (params.next_step) {
9061 /* already covered above */
9064 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
9066 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
9068 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
9070 /* TODO: use own OOB Dev Pw */
9071 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
9078 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9079 const struct wpabuf *data, int forced_freq)
9081 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9084 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9088 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9089 const struct wpabuf *req,
9090 const struct wpabuf *sel, int forced_freq)
9095 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9098 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9100 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9101 wpabuf_head(req), wpabuf_len(req));
9102 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9103 wpabuf_head(sel), wpabuf_len(sel));
9105 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9106 tmp = ndef_parse_p2p(init ? sel : req);
9108 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9112 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9120 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9123 int go_intent = wpa_s->conf->p2p_go_intent;
9124 struct wpa_supplicant *iface;
9126 if (wpa_s->global->p2p == NULL)
9130 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9131 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9133 if (!iface->ap_iface)
9135 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9137 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9139 if (wpa_s->p2p_nfc_tag_enabled)
9140 wpas_p2p_remove_pending_group_interface(wpa_s);
9141 wpa_s->p2p_nfc_tag_enabled = 0;
9145 if (wpa_s->global->p2p_disabled)
9148 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9149 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9150 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9151 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9152 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9153 "to allow static handover cases");
9157 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9159 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9160 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9161 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9162 if (wpa_s->p2p_oob_dev_pw == NULL)
9164 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9166 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9167 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9169 * P2P Group Interface present and the command came on group
9170 * interface, so enable the token for the current interface.
9172 wpa_s->create_p2p_iface = 0;
9174 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9177 if (wpa_s->create_p2p_iface) {
9178 enum wpa_driver_if_type iftype;
9179 /* Prepare to add a new interface for the group */
9180 iftype = WPA_IF_P2P_GROUP;
9181 if (go_intent == 15)
9182 iftype = WPA_IF_P2P_GO;
9183 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9184 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9185 "interface for the group");
9189 if_addr = wpa_s->pending_interface_addr;
9191 if_addr = wpa_s->own_addr;
9193 wpa_s->p2p_nfc_tag_enabled = enabled;
9195 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9196 struct hostapd_data *hapd;
9197 if (iface->ap_iface == NULL)
9199 hapd = iface->ap_iface->bss[0];
9200 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9201 hapd->conf->wps_nfc_dh_pubkey =
9202 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9203 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9204 hapd->conf->wps_nfc_dh_privkey =
9205 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9206 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9207 hapd->conf->wps_nfc_dev_pw =
9208 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9209 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9211 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9212 wpa_dbg(iface, MSG_DEBUG,
9213 "P2P: Failed to enable NFC Tag for GO");
9216 p2p_set_authorized_oob_dev_pw_id(
9217 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9223 #endif /* CONFIG_WPS_NFC */
9226 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9227 struct wpa_used_freq_data *freqs,
9230 u8 curr_chan, cand, chan;
9233 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9234 for (i = 0, cand = 0; i < num; i++) {
9235 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9236 if (curr_chan == chan) {
9241 if (chan == 1 || chan == 6 || chan == 11)
9246 wpa_dbg(wpa_s, MSG_DEBUG,
9247 "P2P: Update Listen channel to %u based on operating channel",
9249 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9254 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9256 struct wpa_used_freq_data *freqs;
9257 unsigned int num = wpa_s->num_multichan_concurrent;
9259 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9263 * If possible, optimize the Listen channel to be a channel that is
9264 * already used by one of the other interfaces.
9266 if (!wpa_s->conf->p2p_optimize_listen_chan)
9269 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9272 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9276 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9278 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
9283 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9285 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9286 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9287 "the management interface is being removed");
9288 wpas_p2p_deinit_global(wpa_s->global);
9293 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9295 if (wpa_s->ap_iface->bss)
9296 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9297 wpas_p2p_group_deinit(wpa_s);