3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "common/ieee802_11_common.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/wpa_ctrl.h"
16 #include "wps/wps_i.h"
18 #include "ap/hostapd.h"
19 #include "ap/ap_config.h"
20 #include "ap/p2p_hostapd.h"
21 #include "eapol_supp/eapol_supp_sm.h"
22 #include "rsn_supp/wpa.h"
23 #include "wpa_supplicant_i.h"
26 #include "config_ssid.h"
31 #include "offchannel.h"
32 #include "wps_supplicant.h"
33 #include "p2p_supplicant.h"
37 * How many times to try to scan to find the GO before giving up on join
40 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
42 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
44 #ifndef P2P_MAX_CLIENT_IDLE
46 * How many seconds to try to reconnect to the GO when connection in P2P client
50 #define P2P_MAX_CLIENT_IDLE 20
52 #define P2P_MAX_CLIENT_IDLE 10
53 #endif /* ANDROID_P2P */
54 #endif /* P2P_MAX_CLIENT_IDLE */
56 #ifndef P2P_MAX_INITIAL_CONN_WAIT
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
61 #define P2P_MAX_INITIAL_CONN_WAIT 10
62 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
64 #ifndef P2P_CONCURRENT_SEARCH_DELAY
66 #define P2P_CONCURRENT_SEARCH_DELAY 0
68 #define P2P_CONCURRENT_SEARCH_DELAY 500
70 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
72 enum p2p_group_removal_reason {
73 P2P_GROUP_REMOVAL_UNKNOWN,
74 P2P_GROUP_REMOVAL_SILENT,
75 P2P_GROUP_REMOVAL_FORMATION_FAILED,
76 P2P_GROUP_REMOVAL_REQUESTED,
77 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
78 P2P_GROUP_REMOVAL_UNAVAILABLE,
79 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
81 P2P_GROUP_REMOVAL_FREQ_CONFLICT
86 static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s);
88 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
89 static struct wpa_supplicant *
90 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
92 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
93 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
94 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
95 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
96 const u8 *dev_addr, enum p2p_wps_method wps_method,
98 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
100 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
101 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
102 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
103 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
104 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
108 static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s)
110 struct wpa_supplicant *iface = NULL;
112 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
113 if(iface->scanning || iface->wpa_state == WPA_SCANNING) {
114 wpa_printf(MSG_DEBUG, "P2P: Scan in progress on %s,"
115 "defer P2P SEARCH", iface->ifname);
124 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
125 struct wpa_scan_results *scan_res)
129 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
132 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
133 (int) scan_res->num);
135 for (i = 0; i < scan_res->num; i++) {
136 struct wpa_scan_res *bss = scan_res->res[i];
137 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
138 bss->freq, bss->level,
139 (const u8 *) (bss + 1),
144 p2p_scan_res_handled(wpa_s->global->p2p);
148 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
149 unsigned int num_req_dev_types,
150 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
152 struct wpa_supplicant *wpa_s = ctx;
153 struct wpa_supplicant *ifs;
154 struct wpa_driver_scan_params params;
156 struct wpabuf *wps_ie, *ies;
157 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
160 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
163 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
164 if (ifs->sta_scan_pending &&
165 wpas_p2p_in_progress(wpa_s) == 2) {
166 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
167 "pending station mode scan to be "
168 "completed on interface %s", ifs->ifname);
169 wpa_s->p2p_cb_on_scan_complete = 1;
170 wpa_supplicant_req_scan(ifs, 0, 0);
175 os_memset(¶ms, 0, sizeof(params));
177 /* P2P Wildcard SSID */
178 params.num_ssids = 1;
179 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
180 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
182 wpa_s->wps->dev.p2p = 1;
183 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
184 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
185 num_req_dev_types, req_dev_types);
189 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
190 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
195 wpabuf_put_buf(ies, wps_ie);
198 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
200 params.p2p_probe = 1;
201 params.extra_ies = wpabuf_head(ies);
202 params.extra_ies_len = wpabuf_len(ies);
205 case P2P_SCAN_SOCIAL:
206 params.freqs = social_channels;
210 case P2P_SCAN_SPECIFIC:
211 social_channels[0] = freq;
212 social_channels[1] = 0;
213 params.freqs = social_channels;
215 case P2P_SCAN_SOCIAL_PLUS_ONE:
216 social_channels[3] = freq;
217 params.freqs = social_channels;
221 ret = wpa_drv_scan(wpa_s, ¶ms);
226 if (wpa_s->scanning ||
227 wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
228 wpa_s->p2p_cb_on_scan_complete = 1;
232 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
238 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
240 switch (p2p_group_interface) {
241 case P2P_GROUP_INTERFACE_PENDING:
242 return WPA_IF_P2P_GROUP;
243 case P2P_GROUP_INTERFACE_GO:
244 return WPA_IF_P2P_GO;
245 case P2P_GROUP_INTERFACE_CLIENT:
246 return WPA_IF_P2P_CLIENT;
249 return WPA_IF_P2P_GROUP;
253 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
255 size_t ssid_len, int *go)
259 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
260 for (s = wpa_s->conf->ssid; s; s = s->next) {
261 if (s->disabled != 0 || !s->p2p_group ||
262 s->ssid_len != ssid_len ||
263 os_memcmp(ssid, s->ssid, ssid_len) != 0)
265 if (s->mode == WPAS_MODE_P2P_GO &&
266 s != wpa_s->current_ssid)
269 *go = s->mode == WPAS_MODE_P2P_GO;
278 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
279 enum p2p_group_removal_reason removal_reason)
281 struct wpa_ssid *ssid;
285 ssid = wpa_s->current_ssid;
287 if ((ssid == NULL) && (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
292 * The current SSID was not known, but there may still be a
293 * pending P2P group interface waiting for provisioning.
295 ssid = wpa_s->conf->ssid;
297 if (ssid->p2p_group &&
298 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
299 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
304 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
309 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
311 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
312 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
313 wpa_s->reassociate = 0;
314 wpa_s->disconnected = 1;
315 wpa_supplicant_deauthenticate(wpa_s,
316 WLAN_REASON_DEAUTH_LEAVING);
320 if (wpa_s->cross_connect_in_use) {
321 wpa_s->cross_connect_in_use = 0;
322 wpa_msg(wpa_s->parent, MSG_INFO,
323 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
324 wpa_s->ifname, wpa_s->cross_connect_uplink);
326 switch (removal_reason) {
327 case P2P_GROUP_REMOVAL_REQUESTED:
328 reason = " reason=REQUESTED";
330 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
331 reason = " reason=FORMATION_FAILED";
333 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
334 reason = " reason=IDLE";
336 case P2P_GROUP_REMOVAL_UNAVAILABLE:
337 reason = " reason=UNAVAILABLE";
339 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
340 reason = " reason=GO_ENDING_SESSION";
343 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
344 reason = " reason=FREQ_CONFLICT";
351 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
352 wpa_msg(wpa_s->parent, MSG_INFO,
353 P2P_EVENT_GROUP_REMOVED "%s %s%s",
354 wpa_s->ifname, gtype, reason);
357 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
358 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
360 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
361 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
363 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
364 struct wpa_global *global;
366 enum wpa_driver_if_type type;
367 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
369 global = wpa_s->global;
370 ifname = os_strdup(wpa_s->ifname);
371 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
372 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
373 wpa_s = global->ifaces;
375 wpa_drv_if_remove(wpa_s, type, ifname);
380 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
381 if (ssid && (ssid->p2p_group ||
382 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
383 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
385 if (ssid == wpa_s->current_ssid) {
386 wpa_sm_set_config(wpa_s->wpa, NULL);
387 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
388 wpa_s->current_ssid = NULL;
391 * Networks objects created during any P2P activities are not
392 * exposed out as they might/will confuse certain non-P2P aware
393 * applications since these network objects won't behave like
396 * Likewise, we don't send out network removed signals for such
399 wpa_config_remove_network(wpa_s->conf, id);
400 wpa_supplicant_clear_status(wpa_s);
401 wpa_supplicant_cancel_sched_scan(wpa_s);
403 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
407 wpa_supplicant_ap_deinit(wpa_s);
409 wpa_drv_deinit_p2p_cli(wpa_s);
415 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
417 const u8 *ssid, size_t ssid_len)
425 if (wpa_s->go_params)
426 bssid = wpa_s->go_params->peer_interface_addr;
428 bssid = wpa_s->bssid;
430 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
432 u8 iface_addr[ETH_ALEN];
433 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
435 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
438 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
439 "group is persistent - BSS " MACSTR " not found",
444 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
446 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
447 "group is persistent - BSS " MACSTR
448 " did not include P2P IE", MAC2STR(bssid));
449 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
450 (u8 *) (bss + 1), bss->ie_len);
451 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
452 ((u8 *) bss + 1) + bss->ie_len,
457 group_capab = p2p_get_group_capab(p2p);
458 addr = p2p_get_go_dev_addr(p2p);
459 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
460 "group_capab=0x%x", group_capab);
462 os_memcpy(go_dev_addr, addr, ETH_ALEN);
463 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
466 os_memset(go_dev_addr, 0, ETH_ALEN);
469 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
470 "go_dev_addr=" MACSTR,
471 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
473 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
477 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
478 struct wpa_ssid *ssid,
479 const u8 *go_dev_addr)
484 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
485 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
486 for (s = wpa_s->conf->ssid; s; s = s->next) {
487 if (s->disabled == 2 &&
488 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
489 s->ssid_len == ssid->ssid_len &&
490 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
495 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
497 if (ssid->passphrase && !s->passphrase)
499 else if (ssid->passphrase && s->passphrase &&
500 os_strcmp(ssid->passphrase, s->passphrase) != 0)
503 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
506 s = wpa_config_add_network(wpa_s->conf);
511 * Instead of network_added we emit persistent_group_added
512 * notification. Also to keep the defense checks in
513 * persistent_group obj registration method, we set the
514 * relevant flags in s to designate it as a persistent group.
517 s->p2p_persistent_group = 1;
518 wpas_notify_persistent_group_added(wpa_s, s);
519 wpa_config_set_network_defaults(s);
523 s->p2p_persistent_group = 1;
526 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
527 s->mode = ssid->mode;
528 s->auth_alg = WPA_AUTH_ALG_OPEN;
529 s->key_mgmt = WPA_KEY_MGMT_PSK;
530 s->proto = WPA_PROTO_RSN;
531 s->pairwise_cipher = WPA_CIPHER_CCMP;
533 if (ssid->passphrase) {
534 os_free(s->passphrase);
535 s->passphrase = os_strdup(ssid->passphrase);
539 os_memcpy(s->psk, ssid->psk, 32);
541 if (s->passphrase && !s->psk_set)
542 wpa_config_update_psk(s);
543 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
545 s->ssid = os_malloc(ssid->ssid_len);
548 s->ssid_len = ssid->ssid_len;
549 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
552 #ifndef CONFIG_NO_CONFIG_WRITE
553 if (changed && wpa_s->conf->update_config &&
554 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
555 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
557 #endif /* CONFIG_NO_CONFIG_WRITE */
563 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
566 struct wpa_ssid *ssid, *s;
571 ssid = wpa_s->current_ssid;
572 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
573 !ssid->p2p_persistent_group)
576 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
577 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
580 if (s->ssid_len == ssid->ssid_len &&
581 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
588 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
589 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
593 if (i == s->num_p2p_clients - 1)
594 return; /* already the most recent entry */
596 /* move the entry to mark it most recent */
597 os_memmove(s->p2p_client_list + i * ETH_ALEN,
598 s->p2p_client_list + (i + 1) * ETH_ALEN,
599 (s->num_p2p_clients - i - 1) * ETH_ALEN);
600 os_memcpy(s->p2p_client_list +
601 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
606 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
607 n = os_realloc_array(s->p2p_client_list,
608 s->num_p2p_clients + 1, ETH_ALEN);
611 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
612 s->p2p_client_list = n;
613 s->num_p2p_clients++;
615 /* Not enough room for an additional entry - drop the oldest
617 os_memmove(s->p2p_client_list,
618 s->p2p_client_list + ETH_ALEN,
619 (s->num_p2p_clients - 1) * ETH_ALEN);
620 os_memcpy(s->p2p_client_list +
621 (s->num_p2p_clients - 1) * ETH_ALEN,
625 #ifndef CONFIG_NO_CONFIG_WRITE
626 if (wpa_s->parent->conf->update_config &&
627 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
628 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
629 #endif /* CONFIG_NO_CONFIG_WRITE */
633 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
636 struct wpa_ssid *ssid;
637 const char *ssid_txt;
640 u8 go_dev_addr[ETH_ALEN];
644 * This callback is likely called for the main interface. Update wpa_s
645 * to use the group interface if a new interface was created for the
648 if (wpa_s->global->p2p_group_formation)
649 wpa_s = wpa_s->global->p2p_group_formation;
650 wpa_s->global->p2p_group_formation = NULL;
651 wpa_s->p2p_in_provisioning = 0;
654 wpa_msg(wpa_s->parent, MSG_INFO,
655 P2P_EVENT_GROUP_FORMATION_FAILURE);
656 wpas_p2p_group_delete(wpa_s,
657 P2P_GROUP_REMOVAL_FORMATION_FAILED);
661 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
663 ssid = wpa_s->current_ssid;
664 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
665 ssid->mode = WPAS_MODE_P2P_GO;
666 p2p_group_notif_formation_done(wpa_s->p2p_group);
667 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
672 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
673 client = ssid->mode == WPAS_MODE_INFRA;
674 if (ssid->mode == WPAS_MODE_P2P_GO) {
675 persistent = ssid->p2p_persistent_group;
676 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
679 persistent = wpas_p2p_persistent_group(wpa_s,
685 client = wpa_s->p2p_group_interface ==
686 P2P_GROUP_INTERFACE_CLIENT;
687 os_memset(go_dev_addr, 0, ETH_ALEN);
690 wpa_s->show_group_started = 0;
693 * Indicate event only after successfully completed 4-way
694 * handshake, i.e., when the interface is ready for data
697 wpa_s->show_group_started = 1;
699 /* For client Second phase of Group formation (4-way handshake) can be still pending
700 * So we need to restore wpa_s->global->p2p_group_formation */
701 wpa_s->global->p2p_group_formation = wpa_s;
704 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
706 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
707 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
708 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
710 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
711 MAC2STR(go_dev_addr),
712 persistent ? " [PERSISTENT]" : "");
713 wpas_p2p_cross_connect_setup(wpa_s);
714 wpas_p2p_set_group_idle_timeout(wpa_s);
716 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
717 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
718 "go_dev_addr=" MACSTR "%s",
719 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
720 ssid && ssid->passphrase ? ssid->passphrase : "",
721 MAC2STR(go_dev_addr),
722 persistent ? " [PERSISTENT]" : "");
723 wpas_p2p_cross_connect_setup(wpa_s);
724 wpas_p2p_set_group_idle_timeout(wpa_s);
728 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
730 if (network_id < 0 && ssid)
731 network_id = ssid->id;
733 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
737 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
739 const u8 *dst, const u8 *src,
741 const u8 *data, size_t data_len,
742 enum offchannel_send_action_result
745 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
747 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
749 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
753 case OFFCHANNEL_SEND_ACTION_SUCCESS:
754 res = P2P_SEND_ACTION_SUCCESS;
756 case OFFCHANNEL_SEND_ACTION_NO_ACK:
757 res = P2P_SEND_ACTION_NO_ACK;
759 case OFFCHANNEL_SEND_ACTION_FAILED:
760 res = P2P_SEND_ACTION_FAILED;
764 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
766 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
767 wpa_s->pending_pd_before_join &&
768 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
769 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
770 wpa_s->pending_pd_before_join = 0;
771 if (wpa_s->p2p_fallback_to_go_neg) {
772 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
773 "during p2p_connect-auto");
774 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
778 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
779 "join-existing-group operation (no ACK for PD "
781 wpas_p2p_join_start(wpa_s);
786 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
787 const u8 *src, const u8 *bssid, const u8 *buf,
788 size_t len, unsigned int wait_time)
790 struct wpa_supplicant *wpa_s = ctx;
791 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
793 wpas_p2p_send_action_tx_status, 1);
797 static void wpas_send_action_done(void *ctx)
799 struct wpa_supplicant *wpa_s = ctx;
800 offchannel_send_action_done(wpa_s);
804 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
805 struct p2p_go_neg_results *params)
807 if (wpa_s->go_params == NULL) {
808 wpa_s->go_params = os_malloc(sizeof(*params));
809 if (wpa_s->go_params == NULL)
812 os_memcpy(wpa_s->go_params, params, sizeof(*params));
817 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
818 struct p2p_go_neg_results *res)
820 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
821 MAC2STR(res->peer_interface_addr));
822 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
823 res->ssid, res->ssid_len);
824 wpa_supplicant_ap_deinit(wpa_s);
825 wpas_copy_go_neg_results(wpa_s, res);
826 if (res->wps_method == WPS_PBC)
827 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
829 u16 dev_pw_id = DEV_PW_DEFAULT;
830 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
831 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
832 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
833 wpa_s->p2p_pin, 1, dev_pw_id);
838 static void p2p_go_configured(void *ctx, void *data)
840 struct wpa_supplicant *wpa_s = ctx;
841 struct p2p_go_neg_results *params = data;
842 struct wpa_ssid *ssid;
845 ssid = wpa_s->current_ssid;
846 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
847 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
848 if (wpa_s->global->p2p_group_formation == wpa_s)
849 wpa_s->global->p2p_group_formation = NULL;
850 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
851 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
852 "go_dev_addr=" MACSTR "%s",
854 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
856 params->passphrase ? params->passphrase : "",
857 MAC2STR(wpa_s->global->p2p_dev_addr),
858 params->persistent_group ? " [PERSISTENT]" : "");
860 if (params->persistent_group)
861 network_id = wpas_p2p_store_persistent_group(
863 wpa_s->global->p2p_dev_addr);
865 network_id = ssid->id;
866 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
867 wpas_p2p_cross_connect_setup(wpa_s);
868 wpas_p2p_set_group_idle_timeout(wpa_s);
872 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
873 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
874 params->peer_interface_addr)) {
875 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
879 if (params->wps_method == WPS_PBC)
880 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
881 params->peer_device_addr);
882 else if (wpa_s->p2p_pin[0])
883 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
884 wpa_s->p2p_pin, NULL, 0);
885 os_free(wpa_s->go_params);
886 wpa_s->go_params = NULL;
890 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
891 struct p2p_go_neg_results *params,
894 struct wpa_ssid *ssid;
896 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
899 ssid = wpa_config_add_network(wpa_s->conf);
903 wpa_s->show_group_started = 0;
905 wpa_config_set_network_defaults(ssid);
908 ssid->p2p_persistent_group = params->persistent_group;
909 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
911 ssid->frequency = params->freq;
912 ssid->ht40 = params->ht40;
913 ssid->ssid = os_zalloc(params->ssid_len + 1);
915 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
916 ssid->ssid_len = params->ssid_len;
918 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
919 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
920 ssid->proto = WPA_PROTO_RSN;
921 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
922 ssid->passphrase = os_strdup(params->passphrase);
923 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
925 wpa_s->ap_configured_cb = p2p_go_configured;
926 wpa_s->ap_configured_cb_ctx = wpa_s;
927 wpa_s->ap_configured_cb_data = wpa_s->go_params;
928 wpa_s->connect_without_scan = ssid;
929 wpa_s->reassociate = 1;
930 wpa_s->disconnected = 0;
931 wpa_supplicant_req_scan(wpa_s, 0, 0);
935 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
936 const struct wpa_supplicant *src)
938 struct wpa_config *d;
939 const struct wpa_config *s;
944 #define C(n) if (s->n) d->n = os_strdup(s->n)
953 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
954 os_memcpy(d->sec_device_type, s->sec_device_type,
955 sizeof(d->sec_device_type));
956 d->num_sec_device_types = s->num_sec_device_types;
958 d->p2p_group_idle = s->p2p_group_idle;
959 d->p2p_intra_bss = s->p2p_intra_bss;
960 d->persistent_reconnect = s->persistent_reconnect;
961 d->max_num_sta = s->max_num_sta;
962 d->pbc_in_m1 = s->pbc_in_m1;
966 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
967 enum wpa_driver_if_type type)
969 char ifname[120], force_ifname[120];
971 if (wpa_s->pending_interface_name[0]) {
972 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
973 "- skip creation of a new one");
974 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
975 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
976 "unknown?! ifname='%s'",
977 wpa_s->pending_interface_name);
983 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
984 wpa_s->p2p_group_idx);
985 if (os_strlen(ifname) >= IFNAMSIZ &&
986 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
987 /* Try to avoid going over the IFNAMSIZ length limit */
988 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
989 wpa_s->p2p_group_idx);
991 force_ifname[0] = '\0';
993 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
995 wpa_s->p2p_group_idx++;
997 wpa_s->pending_interface_type = type;
998 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
999 wpa_s->pending_interface_addr, NULL) < 0) {
1000 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1005 if (force_ifname[0]) {
1006 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1008 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1009 sizeof(wpa_s->pending_interface_name));
1011 os_strlcpy(wpa_s->pending_interface_name, ifname,
1012 sizeof(wpa_s->pending_interface_name));
1013 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1014 MACSTR, wpa_s->pending_interface_name,
1015 MAC2STR(wpa_s->pending_interface_addr));
1021 static void wpas_p2p_remove_pending_group_interface(
1022 struct wpa_supplicant *wpa_s)
1024 if (!wpa_s->pending_interface_name[0] ||
1025 is_zero_ether_addr(wpa_s->pending_interface_addr))
1026 return; /* No pending virtual interface */
1028 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1029 wpa_s->pending_interface_name);
1030 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1031 wpa_s->pending_interface_name);
1032 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1033 wpa_s->pending_interface_name[0] = '\0';
1037 static struct wpa_supplicant *
1038 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1040 struct wpa_interface iface;
1041 struct wpa_supplicant *group_wpa_s;
1043 if (!wpa_s->pending_interface_name[0]) {
1044 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1045 if (!wpas_p2p_create_iface(wpa_s))
1048 * Something has forced us to remove the pending interface; try
1049 * to create a new one and hope for the best that we will get
1050 * the same local address.
1052 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1053 WPA_IF_P2P_CLIENT) < 0)
1057 os_memset(&iface, 0, sizeof(iface));
1058 iface.ifname = wpa_s->pending_interface_name;
1059 iface.driver = wpa_s->driver->name;
1060 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1061 iface.driver_param = wpa_s->conf->driver_param;
1062 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1063 if (group_wpa_s == NULL) {
1064 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1065 "wpa_supplicant interface");
1068 wpa_s->pending_interface_name[0] = '\0';
1069 group_wpa_s->parent = wpa_s;
1070 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1071 P2P_GROUP_INTERFACE_CLIENT;
1072 wpa_s->global->p2p_group_formation = group_wpa_s;
1074 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1080 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1083 struct wpa_supplicant *wpa_s = eloop_ctx;
1084 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1085 if (wpa_s->global->p2p)
1086 p2p_group_formation_failed(wpa_s->global->p2p);
1087 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1088 wpa_drv_p2p_group_formation_failed(wpa_s);
1089 wpas_group_formation_completed(wpa_s, 0);
1093 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1095 struct wpa_supplicant *wpa_s = ctx;
1097 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1098 wpa_drv_cancel_remain_on_channel(wpa_s);
1099 wpa_s->off_channel_freq = 0;
1100 wpa_s->roc_waiting_drv_freq = 0;
1104 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1106 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1107 wpas_p2p_remove_pending_group_interface(wpa_s);
1111 if (wpa_s->p2p_go_ht40)
1114 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1115 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1117 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1118 struct wpa_ssid *ssid;
1119 ssid = wpa_config_get_network(wpa_s->conf,
1120 wpa_s->p2p_persistent_id);
1121 if (ssid && ssid->disabled == 2 &&
1122 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1123 size_t len = os_strlen(ssid->passphrase);
1124 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1125 "on requested persistent group");
1126 os_memcpy(res->passphrase, ssid->passphrase, len);
1127 res->passphrase[len] = '\0';
1131 if (wpa_s->create_p2p_iface) {
1132 struct wpa_supplicant *group_wpa_s =
1133 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1134 if (group_wpa_s == NULL) {
1135 wpas_p2p_remove_pending_group_interface(wpa_s);
1138 if (group_wpa_s != wpa_s) {
1139 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1140 sizeof(group_wpa_s->p2p_pin));
1141 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1143 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1144 wpa_s->pending_interface_name[0] = '\0';
1145 group_wpa_s->p2p_in_provisioning = 1;
1148 wpas_start_wps_go(group_wpa_s, res, 1);
1150 wpas_start_wps_enrollee(group_wpa_s, res);
1152 wpa_s->p2p_in_provisioning = 1;
1153 wpa_s->global->p2p_group_formation = wpa_s;
1156 wpas_start_wps_go(wpa_s, res, 1);
1158 wpas_start_wps_enrollee(ctx, res);
1161 wpa_s->p2p_long_listen = 0;
1162 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1164 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1165 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1166 (res->peer_config_timeout % 100) * 10000,
1167 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1171 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1173 struct wpa_supplicant *wpa_s = ctx;
1174 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1175 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1177 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1181 void wpas_dev_found(void *ctx, const u8 *addr,
1182 const struct p2p_peer_info *info,
1185 #ifndef CONFIG_NO_STDOUT_DEBUG
1186 struct wpa_supplicant *wpa_s = ctx;
1187 char devtype[WPS_DEV_TYPE_BUFSIZE];
1189 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1190 " p2p_dev_addr=" MACSTR
1191 " pri_dev_type=%s name='%s' config_methods=0x%x "
1192 "dev_capab=0x%x group_capab=0x%x",
1193 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1194 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1196 info->device_name, info->config_methods,
1197 info->dev_capab, info->group_capab);
1198 #endif /* CONFIG_NO_STDOUT_DEBUG */
1200 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1204 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1206 struct wpa_supplicant *wpa_s = ctx;
1208 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1209 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1211 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1215 static int wpas_start_listen(void *ctx, unsigned int freq,
1216 unsigned int duration,
1217 const struct wpabuf *probe_resp_ie)
1219 struct wpa_supplicant *wpa_s = ctx;
1221 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1223 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1224 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1225 "report received Probe Request frames");
1229 wpa_s->pending_listen_freq = freq;
1230 wpa_s->pending_listen_duration = duration;
1232 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1233 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1234 "to remain on channel (%u MHz) for Listen "
1236 wpa_s->pending_listen_freq = 0;
1239 wpa_s->off_channel_freq = 0;
1240 wpa_s->roc_waiting_drv_freq = freq;
1246 static void wpas_stop_listen(void *ctx)
1248 struct wpa_supplicant *wpa_s = ctx;
1249 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1250 wpa_drv_cancel_remain_on_channel(wpa_s);
1251 wpa_s->off_channel_freq = 0;
1252 wpa_s->roc_waiting_drv_freq = 0;
1254 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1255 wpa_drv_probe_req_report(wpa_s, 0);
1259 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1261 struct wpa_supplicant *wpa_s = ctx;
1262 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1266 static struct p2p_srv_bonjour *
1267 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1268 const struct wpabuf *query)
1270 struct p2p_srv_bonjour *bsrv;
1273 len = wpabuf_len(query);
1274 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1275 struct p2p_srv_bonjour, list) {
1276 if (len == wpabuf_len(bsrv->query) &&
1277 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1285 static struct p2p_srv_upnp *
1286 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1287 const char *service)
1289 struct p2p_srv_upnp *usrv;
1291 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1292 struct p2p_srv_upnp, list) {
1293 if (version == usrv->version &&
1294 os_strcmp(service, usrv->service) == 0)
1301 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1306 if (wpabuf_tailroom(resp) < 5)
1309 /* Length (to be filled) */
1310 len_pos = wpabuf_put(resp, 2);
1311 wpabuf_put_u8(resp, srv_proto);
1312 wpabuf_put_u8(resp, srv_trans_id);
1314 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1315 /* Response Data: empty */
1316 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1320 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1321 struct wpabuf *resp, u8 srv_trans_id)
1323 struct p2p_srv_bonjour *bsrv;
1326 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1328 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1329 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1333 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1334 struct p2p_srv_bonjour, list) {
1335 if (wpabuf_tailroom(resp) <
1336 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1338 /* Length (to be filled) */
1339 len_pos = wpabuf_put(resp, 2);
1340 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1341 wpabuf_put_u8(resp, srv_trans_id);
1343 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1344 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1345 wpabuf_head(bsrv->resp),
1346 wpabuf_len(bsrv->resp));
1348 wpabuf_put_buf(resp, bsrv->query); /* Key */
1349 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1350 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1356 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1357 struct wpabuf *resp, u8 srv_trans_id,
1358 const u8 *query, size_t query_len)
1360 struct p2p_srv_bonjour *bsrv;
1364 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1366 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1367 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1368 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1373 if (query_len == 0) {
1374 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1378 if (wpabuf_tailroom(resp) < 5)
1380 /* Length (to be filled) */
1381 len_pos = wpabuf_put(resp, 2);
1382 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1383 wpabuf_put_u8(resp, srv_trans_id);
1385 wpabuf_set(&buf, query, query_len);
1386 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1388 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1392 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1393 /* Response Data: empty */
1394 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1400 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1401 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1402 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1404 if (wpabuf_tailroom(resp) >=
1405 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1407 wpabuf_put_buf(resp, bsrv->query); /* Key */
1408 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1410 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1414 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1415 struct wpabuf *resp, u8 srv_trans_id)
1417 struct p2p_srv_upnp *usrv;
1420 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1422 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1423 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1427 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1428 struct p2p_srv_upnp, list) {
1429 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1432 /* Length (to be filled) */
1433 len_pos = wpabuf_put(resp, 2);
1434 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1435 wpabuf_put_u8(resp, srv_trans_id);
1438 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1440 wpabuf_put_u8(resp, usrv->version);
1441 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1443 wpabuf_put_str(resp, usrv->service);
1444 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1450 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1451 struct wpabuf *resp, u8 srv_trans_id,
1452 const u8 *query, size_t query_len)
1454 struct p2p_srv_upnp *usrv;
1460 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1463 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1464 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1465 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1470 if (query_len == 0) {
1471 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1475 if (wpabuf_tailroom(resp) < 5)
1478 /* Length (to be filled) */
1479 len_pos = wpabuf_put(resp, 2);
1480 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1481 wpabuf_put_u8(resp, srv_trans_id);
1484 str = os_malloc(query_len);
1487 os_memcpy(str, query + 1, query_len - 1);
1488 str[query_len - 1] = '\0';
1490 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1491 struct p2p_srv_upnp, list) {
1492 if (version != usrv->version)
1495 if (os_strcmp(str, "ssdp:all") != 0 &&
1496 os_strstr(usrv->service, str) == NULL)
1499 if (wpabuf_tailroom(resp) < 2)
1503 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1505 wpabuf_put_u8(resp, version);
1507 wpabuf_put_u8(resp, ',');
1511 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1513 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1515 wpabuf_put_str(resp, usrv->service);
1520 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1523 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1524 /* Response Data: empty */
1527 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1531 #ifdef CONFIG_WIFI_DISPLAY
1532 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1533 struct wpabuf *resp, u8 srv_trans_id,
1534 const u8 *query, size_t query_len)
1540 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1542 if (!wpa_s->global->wifi_display) {
1543 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1544 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1549 if (query_len < 1) {
1550 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1555 if (wpabuf_tailroom(resp) < 5)
1560 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1562 /* TODO: role specific handling */
1564 /* Length (to be filled) */
1565 len_pos = wpabuf_put(resp, 2);
1566 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1567 wpabuf_put_u8(resp, srv_trans_id);
1568 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1570 while (pos < query + query_len) {
1571 if (*pos < MAX_WFD_SUBELEMS &&
1572 wpa_s->global->wfd_subelem[*pos] &&
1573 wpabuf_tailroom(resp) >=
1574 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1575 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1576 "subelement %u", *pos);
1577 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1582 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1584 #endif /* CONFIG_WIFI_DISPLAY */
1587 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1588 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1590 struct wpa_supplicant *wpa_s = ctx;
1591 const u8 *pos = tlvs;
1592 const u8 *end = tlvs + tlvs_len;
1595 struct wpabuf *resp;
1596 u8 srv_proto, srv_trans_id;
1600 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1602 buf_len = 2 * tlvs_len + 1;
1603 buf = os_malloc(buf_len);
1605 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1606 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1608 freq, MAC2STR(sa), dialog_token, update_indic,
1613 if (wpa_s->p2p_sd_over_ctrl_iface) {
1614 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1615 update_indic, tlvs, tlvs_len);
1616 return; /* to be processed by an external program */
1619 resp = wpabuf_alloc(10000);
1623 while (pos + 1 < end) {
1624 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1625 slen = WPA_GET_LE16(pos);
1627 if (pos + slen > end || slen < 2) {
1628 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1633 tlv_end = pos + slen;
1636 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1638 srv_trans_id = *pos++;
1639 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1642 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1643 pos, tlv_end - pos);
1646 if (wpa_s->force_long_sd) {
1647 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1649 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1650 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1654 switch (srv_proto) {
1655 case P2P_SERV_ALL_SERVICES:
1656 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1657 "for all services");
1658 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1659 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1660 wpa_printf(MSG_DEBUG, "P2P: No service "
1661 "discovery protocols available");
1662 wpas_sd_add_proto_not_avail(
1663 resp, P2P_SERV_ALL_SERVICES,
1667 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1668 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1670 case P2P_SERV_BONJOUR:
1671 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1672 pos, tlv_end - pos);
1675 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1676 pos, tlv_end - pos);
1678 #ifdef CONFIG_WIFI_DISPLAY
1679 case P2P_SERV_WIFI_DISPLAY:
1680 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1681 pos, tlv_end - pos);
1683 #endif /* CONFIG_WIFI_DISPLAY */
1685 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1686 "protocol %u", srv_proto);
1687 wpas_sd_add_proto_not_avail(resp, srv_proto,
1696 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1697 update_indic, tlvs, tlvs_len);
1699 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1705 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1706 const u8 *tlvs, size_t tlvs_len)
1708 struct wpa_supplicant *wpa_s = ctx;
1709 const u8 *pos = tlvs;
1710 const u8 *end = tlvs + tlvs_len;
1716 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1718 if (tlvs_len > 1500) {
1719 /* TODO: better way for handling this */
1720 wpa_msg_ctrl(wpa_s, MSG_INFO,
1721 P2P_EVENT_SERV_DISC_RESP MACSTR
1722 " %u <long response: %u bytes>",
1723 MAC2STR(sa), update_indic,
1724 (unsigned int) tlvs_len);
1726 buf_len = 2 * tlvs_len + 1;
1727 buf = os_malloc(buf_len);
1729 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1730 wpa_msg_ctrl(wpa_s, MSG_INFO,
1731 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1732 MAC2STR(sa), update_indic, buf);
1738 u8 srv_proto, srv_trans_id, status;
1740 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1741 slen = WPA_GET_LE16(pos);
1743 if (pos + slen > end || slen < 3) {
1744 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1748 tlv_end = pos + slen;
1751 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1753 srv_trans_id = *pos++;
1754 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1757 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1760 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1761 pos, tlv_end - pos);
1766 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1770 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1771 const struct wpabuf *tlvs)
1773 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1774 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1775 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1777 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1781 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1782 u8 version, const char *query)
1784 struct wpabuf *tlvs;
1787 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1790 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1791 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1792 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1793 wpabuf_put_u8(tlvs, version);
1794 wpabuf_put_str(tlvs, query);
1795 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1801 #ifdef CONFIG_WIFI_DISPLAY
1803 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1804 const struct wpabuf *tlvs)
1806 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1808 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1810 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1814 #define MAX_WFD_SD_SUBELEMS 20
1816 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1817 const char *subelems)
1824 len = wpabuf_put(tlvs, 2);
1825 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1826 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1828 wpabuf_put_u8(tlvs, role);
1833 if (val >= 0 && val < 256) {
1834 wpabuf_put_u8(tlvs, val);
1836 if (count == MAX_WFD_SD_SUBELEMS)
1839 pos = os_strchr(pos + 1, ',');
1845 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1849 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1850 const u8 *dst, const char *role)
1852 struct wpabuf *tlvs;
1854 const char *subelems;
1857 subelems = os_strchr(role, ' ');
1858 if (subelems == NULL)
1862 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1866 if (os_strstr(role, "[source]"))
1867 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1868 if (os_strstr(role, "[pri-sink]"))
1869 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1870 if (os_strstr(role, "[sec-sink]"))
1871 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1872 if (os_strstr(role, "[source+sink]"))
1873 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1875 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1880 #endif /* CONFIG_WIFI_DISPLAY */
1883 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
1885 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1886 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
1887 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1889 return p2p_sd_cancel_request(wpa_s->global->p2p,
1890 (void *) (uintptr_t) req);
1894 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1895 const u8 *dst, u8 dialog_token,
1896 const struct wpabuf *resp_tlvs)
1898 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1899 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1903 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1905 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1910 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1912 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1915 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1916 wpa_drv_p2p_service_update(wpa_s);
1919 if (wpa_s->global->p2p)
1921 p2p_sd_service_update(wpa_s->global->p2p, action);
1923 p2p_sd_service_update(wpa_s->global->p2p);
1928 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1930 dl_list_del(&bsrv->list);
1931 wpabuf_free(bsrv->query);
1932 wpabuf_free(bsrv->resp);
1937 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1939 dl_list_del(&usrv->list);
1940 os_free(usrv->service);
1945 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1947 struct p2p_srv_bonjour *bsrv, *bn;
1948 struct p2p_srv_upnp *usrv, *un;
1950 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1951 struct p2p_srv_bonjour, list)
1952 wpas_p2p_srv_bonjour_free(bsrv);
1954 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1955 struct p2p_srv_upnp, list)
1956 wpas_p2p_srv_upnp_free(usrv);
1959 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1961 wpas_p2p_sd_service_update(wpa_s);
1966 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1967 struct wpabuf *query, struct wpabuf *resp)
1969 struct p2p_srv_bonjour *bsrv;
1971 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1974 wpabuf_free(bsrv->resp);
1979 bsrv = os_zalloc(sizeof(*bsrv));
1982 bsrv->query = query;
1984 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1987 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
1989 wpas_p2p_sd_service_update(wpa_s);
1995 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1996 const struct wpabuf *query)
1998 struct p2p_srv_bonjour *bsrv;
2000 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2003 wpas_p2p_srv_bonjour_free(bsrv);
2005 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2007 wpas_p2p_sd_service_update(wpa_s);
2013 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2014 const char *service)
2016 struct p2p_srv_upnp *usrv;
2018 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2019 return 0; /* Already listed */
2020 usrv = os_zalloc(sizeof(*usrv));
2023 usrv->version = version;
2024 usrv->service = os_strdup(service);
2025 if (usrv->service == NULL) {
2029 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2032 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2034 wpas_p2p_sd_service_update(wpa_s);
2040 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2041 const char *service)
2043 struct p2p_srv_upnp *usrv;
2045 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2048 wpas_p2p_srv_upnp_free(usrv);
2050 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2052 wpas_p2p_sd_service_update(wpa_s);
2058 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2059 const u8 *peer, const char *params,
2060 unsigned int generated_pin)
2062 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2063 MAC2STR(peer), generated_pin, params);
2067 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2068 const u8 *peer, const char *params)
2070 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2071 MAC2STR(peer), params);
2075 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2076 const u8 *dev_addr, const u8 *pri_dev_type,
2077 const char *dev_name, u16 supp_config_methods,
2078 u8 dev_capab, u8 group_capab, const u8 *group_id,
2079 size_t group_id_len)
2081 struct wpa_supplicant *wpa_s = ctx;
2082 char devtype[WPS_DEV_TYPE_BUFSIZE];
2084 u8 empty_dev_type[8];
2085 unsigned int generated_pin = 0;
2086 struct wpa_supplicant *group = NULL;
2089 for (group = wpa_s->global->ifaces; group; group = group->next)
2091 struct wpa_ssid *s = group->current_ssid;
2093 s->mode == WPAS_MODE_P2P_GO &&
2094 group_id_len - ETH_ALEN == s->ssid_len &&
2095 os_memcmp(group_id + ETH_ALEN, s->ssid,
2101 if (pri_dev_type == NULL) {
2102 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2103 pri_dev_type = empty_dev_type;
2105 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2106 " pri_dev_type=%s name='%s' config_methods=0x%x "
2107 "dev_capab=0x%x group_capab=0x%x%s%s",
2109 wps_dev_type_bin2str(pri_dev_type, devtype,
2111 dev_name, supp_config_methods, dev_capab, group_capab,
2112 group ? " group=" : "",
2113 group ? group->ifname : "");
2114 params[sizeof(params) - 1] = '\0';
2116 if (config_methods & WPS_CONFIG_DISPLAY) {
2117 generated_pin = wps_generate_pin();
2118 wpas_prov_disc_local_display(wpa_s, peer, params,
2120 } else if (config_methods & WPS_CONFIG_KEYPAD)
2121 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2122 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2123 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2124 "%s", MAC2STR(peer), params);
2126 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2127 P2P_PROV_DISC_SUCCESS,
2128 config_methods, generated_pin);
2132 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2134 struct wpa_supplicant *wpa_s = ctx;
2135 unsigned int generated_pin = 0;
2138 if (wpa_s->pending_pd_before_join &&
2139 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2140 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2141 wpa_s->pending_pd_before_join = 0;
2142 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2143 "join-existing-group operation");
2144 wpas_p2p_join_start(wpa_s);
2148 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2149 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2150 os_snprintf(params, sizeof(params), " peer_go=%d",
2151 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2155 if (config_methods & WPS_CONFIG_DISPLAY)
2156 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2157 else if (config_methods & WPS_CONFIG_KEYPAD) {
2158 generated_pin = wps_generate_pin();
2159 wpas_prov_disc_local_display(wpa_s, peer, params,
2161 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2162 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2163 "%s", MAC2STR(peer), params);
2165 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2166 P2P_PROV_DISC_SUCCESS,
2167 config_methods, generated_pin);
2171 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2172 enum p2p_prov_disc_status status)
2174 struct wpa_supplicant *wpa_s = ctx;
2176 if (wpa_s->p2p_fallback_to_go_neg) {
2177 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2178 "failed - fall back to GO Negotiation");
2179 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2183 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2184 " p2p_dev_addr=" MACSTR " status=%d",
2185 MAC2STR(peer), status);
2187 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2192 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2193 const u8 *go_dev_addr, const u8 *ssid,
2194 size_t ssid_len, int *go, u8 *group_bssid,
2195 int *force_freq, int persistent_group)
2197 struct wpa_supplicant *wpa_s = ctx;
2199 u8 cur_bssid[ETH_ALEN];
2201 struct wpa_supplicant *grp;
2203 if (!persistent_group) {
2204 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2205 " to join an active group", MAC2STR(sa));
2206 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2207 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2209 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2210 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2211 "authorized invitation");
2215 * Do not accept the invitation automatically; notify user and
2218 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2221 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2223 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2224 "running persistent group");
2226 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2230 if (!wpa_s->conf->persistent_reconnect)
2231 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2233 for (s = wpa_s->conf->ssid; s; s = s->next) {
2234 if (s->disabled == 2 &&
2235 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2236 s->ssid_len == ssid_len &&
2237 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2242 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2243 " requested reinvocation of an unknown group",
2245 return P2P_SC_FAIL_UNKNOWN_GROUP;
2248 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2250 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2251 wpa_printf(MSG_DEBUG, "P2P: The only available "
2252 "interface is already in use - reject "
2254 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2256 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2257 } else if (s->mode == WPAS_MODE_P2P_GO) {
2259 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2261 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2262 "interface address for the group");
2263 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2265 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2270 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2271 wpa_s->assoc_freq) {
2272 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2273 "the channel we are already using");
2274 *force_freq = wpa_s->assoc_freq;
2277 res = wpa_drv_shared_freq(wpa_s);
2279 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2280 "with the channel we are already using on a "
2281 "shared interface");
2285 return P2P_SC_SUCCESS;
2289 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2290 const u8 *ssid, size_t ssid_len,
2291 const u8 *go_dev_addr, u8 status,
2294 struct wpa_supplicant *wpa_s = ctx;
2297 for (s = wpa_s->conf->ssid; s; s = s->next) {
2298 if (s->disabled == 2 &&
2299 s->ssid_len == ssid_len &&
2300 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2304 if (status == P2P_SC_SUCCESS) {
2305 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2306 " was accepted; op_freq=%d MHz",
2307 MAC2STR(sa), op_freq);
2309 wpas_p2p_group_add_persistent(
2310 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
2312 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2313 wpa_s->p2p_wps_method, 0);
2318 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2319 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2320 " was rejected (status %u)", MAC2STR(sa), status);
2326 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2327 "sa=" MACSTR " go_dev_addr=" MACSTR
2328 " bssid=" MACSTR " unknown-network",
2329 MAC2STR(sa), MAC2STR(go_dev_addr),
2332 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2333 "sa=" MACSTR " go_dev_addr=" MACSTR
2335 MAC2STR(sa), MAC2STR(go_dev_addr));
2340 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2341 " persistent=%d", MAC2STR(sa), s->id);
2345 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2347 struct wpa_supplicant *wpa_s = ctx;
2348 struct wpa_ssid *ssid;
2351 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2352 "status=%d " MACSTR,
2353 status, MAC2STR(bssid));
2355 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2356 "status=%d ", status);
2358 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2360 if (wpa_s->pending_invite_ssid_id == -1)
2361 return; /* Invitation to active group */
2363 if (status != P2P_SC_SUCCESS) {
2364 wpas_p2p_remove_pending_group_interface(wpa_s);
2368 ssid = wpa_config_get_network(wpa_s->conf,
2369 wpa_s->pending_invite_ssid_id);
2371 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2372 "data matching with invitation");
2376 wpas_p2p_group_add_persistent(wpa_s, ssid,
2377 ssid->mode == WPAS_MODE_P2P_GO,
2378 wpa_s->p2p_persistent_go_freq,
2379 wpa_s->p2p_go_ht40);
2383 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2388 if (global->p2p_disallow_freq == NULL)
2391 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2392 if (freq >= global->p2p_disallow_freq[i].min &&
2393 freq <= global->p2p_disallow_freq[i].max)
2401 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2403 reg->channel[reg->channels] = chan;
2408 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2409 struct p2p_channels *chan)
2413 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2416 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2417 chan->reg_class[cla].reg_class = 81;
2418 chan->reg_class[cla].channels = 0;
2419 for (i = 0; i < 11; i++) {
2420 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2421 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2423 if (chan->reg_class[cla].channels)
2426 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2429 /* Operating class 115 - 5 GHz, channels 36-48 */
2430 chan->reg_class[cla].reg_class = 115;
2431 chan->reg_class[cla].channels = 0;
2432 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2433 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2434 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2435 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2436 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2437 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2438 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2439 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2440 if (chan->reg_class[cla].channels)
2443 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2446 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2447 chan->reg_class[cla].reg_class = 124;
2448 chan->reg_class[cla].channels = 0;
2449 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2450 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2451 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2452 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2453 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2454 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2455 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2456 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2457 if (chan->reg_class[cla].channels)
2460 chan->reg_classes = cla;
2465 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2467 enum hostapd_hw_mode mode)
2471 for (i = 0; i < num_modes; i++) {
2472 if (modes[i].mode == mode)
2480 static int has_channel(struct wpa_global *global,
2481 struct hostapd_hw_modes *mode, u8 chan, int *flags)
2486 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2488 if (wpas_p2p_disallowed_freq(global, freq))
2491 for (i = 0; i < mode->num_channels; i++) {
2492 if (mode->channels[i].chan == chan) {
2494 *flags = mode->channels[i].flag;
2495 return !(mode->channels[i].flag &
2496 (HOSTAPD_CHAN_DISABLED |
2497 HOSTAPD_CHAN_PASSIVE_SCAN |
2498 HOSTAPD_CHAN_NO_IBSS |
2499 HOSTAPD_CHAN_RADAR));
2507 struct p2p_oper_class_map {
2508 enum hostapd_hw_mode mode;
2513 enum { BW20, BW40PLUS, BW40MINUS } bw;
2516 static struct p2p_oper_class_map op_class[] = {
2517 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2518 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2519 #if 0 /* Do not enable HT40 on 2 GHz for now */
2520 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2521 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2523 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2524 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2525 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2526 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2527 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2528 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2529 { -1, 0, 0, 0, 0, BW20 }
2533 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2534 struct hostapd_hw_modes *mode,
2539 if (!has_channel(wpa_s->global, mode, channel, &flag))
2541 if (bw == BW40MINUS &&
2542 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2543 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2545 if (bw == BW40PLUS &&
2546 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2547 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2553 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2554 struct p2p_channels *chan)
2556 struct hostapd_hw_modes *mode;
2559 if (wpa_s->hw.modes == NULL) {
2560 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2561 "of all supported channels; assume dualband "
2563 return wpas_p2p_default_channels(wpa_s, chan);
2568 for (op = 0; op_class[op].op_class; op++) {
2569 struct p2p_oper_class_map *o = &op_class[op];
2571 struct p2p_reg_class *reg = NULL;
2573 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2576 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2577 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2580 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2581 "class %u", o->op_class);
2582 reg = &chan->reg_class[cla];
2584 reg->reg_class = o->op_class;
2586 reg->channel[reg->channels] = ch;
2590 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2591 reg->channel, reg->channels);
2595 chan->reg_classes = cla;
2601 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2602 struct hostapd_hw_modes *mode, u8 channel)
2606 for (op = 0; op_class[op].op_class; op++) {
2607 struct p2p_oper_class_map *o = &op_class[op];
2610 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2611 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2612 o->bw == BW20 || ch != channel)
2614 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2618 return (o->bw == BW40MINUS) ? -1 : 1;
2627 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2630 struct wpa_supplicant *wpa_s = ctx;
2632 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2633 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2639 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2643 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2645 struct wpa_supplicant *wpa_s = ctx;
2647 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2648 struct wpa_ssid *ssid = wpa_s->current_ssid;
2651 if (ssid->mode != WPAS_MODE_INFRA)
2653 if (wpa_s->wpa_state != WPA_COMPLETED &&
2654 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2656 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2665 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2666 * @global: Pointer to global data from wpa_supplicant_init()
2667 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2668 * Returns: 0 on success, -1 on failure
2670 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2672 struct p2p_config p2p;
2676 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2682 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2683 struct p2p_params params;
2685 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2686 os_memset(¶ms, 0, sizeof(params));
2687 params.dev_name = wpa_s->conf->device_name;
2688 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2690 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2691 os_memcpy(params.sec_dev_type,
2692 wpa_s->conf->sec_device_type,
2693 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2695 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2701 os_memset(&p2p, 0, sizeof(p2p));
2702 p2p.msg_ctx = wpa_s;
2704 p2p.p2p_scan = wpas_p2p_scan;
2705 p2p.send_action = wpas_send_action;
2706 p2p.send_action_done = wpas_send_action_done;
2707 p2p.go_neg_completed = wpas_go_neg_completed;
2708 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2709 p2p.dev_found = wpas_dev_found;
2710 p2p.dev_lost = wpas_dev_lost;
2711 p2p.start_listen = wpas_start_listen;
2712 p2p.stop_listen = wpas_stop_listen;
2713 p2p.send_probe_resp = wpas_send_probe_resp;
2714 p2p.sd_request = wpas_sd_request;
2715 p2p.sd_response = wpas_sd_response;
2716 p2p.prov_disc_req = wpas_prov_disc_req;
2717 p2p.prov_disc_resp = wpas_prov_disc_resp;
2718 p2p.prov_disc_fail = wpas_prov_disc_fail;
2719 p2p.invitation_process = wpas_invitation_process;
2720 p2p.invitation_received = wpas_invitation_received;
2721 p2p.invitation_result = wpas_invitation_result;
2722 p2p.get_noa = wpas_get_noa;
2723 p2p.go_connected = wpas_go_connected;
2725 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2726 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2727 p2p.dev_name = wpa_s->conf->device_name;
2728 p2p.manufacturer = wpa_s->conf->manufacturer;
2729 p2p.model_name = wpa_s->conf->model_name;
2730 p2p.model_number = wpa_s->conf->model_number;
2731 p2p.serial_number = wpa_s->conf->serial_number;
2733 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2734 p2p.config_methods = wpa_s->wps->config_methods;
2737 if (wpa_s->conf->p2p_listen_reg_class &&
2738 wpa_s->conf->p2p_listen_channel) {
2739 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2740 p2p.channel = wpa_s->conf->p2p_listen_channel;
2744 * Pick one of the social channels randomly as the listen
2747 os_get_random((u8 *) &r, sizeof(r));
2748 p2p.channel = 1 + (r % 3) * 5;
2750 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2752 if (wpa_s->conf->p2p_oper_reg_class &&
2753 wpa_s->conf->p2p_oper_channel) {
2754 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2755 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2756 p2p.cfg_op_channel = 1;
2757 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2758 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2761 p2p.op_reg_class = 81;
2763 * Use random operation channel from (1, 6, 11) if no other
2764 * preference is indicated.
2766 os_get_random((u8 *) &r, sizeof(r));
2767 p2p.op_channel = 1 + (r % 3) * 5;
2768 p2p.cfg_op_channel = 0;
2769 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2770 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2772 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2773 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2774 p2p.country[2] = 0x04;
2776 os_memcpy(p2p.country, "XX\x04", 3);
2778 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2779 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2784 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2787 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2788 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2789 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2791 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2792 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2794 p2p.max_peers = 100;
2796 if (wpa_s->conf->p2p_ssid_postfix) {
2797 p2p.ssid_postfix_len =
2798 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2799 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2800 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2801 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2802 p2p.ssid_postfix_len);
2805 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2808 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2809 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
2810 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)
2811 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
2814 global->p2p = p2p_init(&p2p);
2815 if (global->p2p == NULL)
2817 global->p2p_init_wpa_s = wpa_s;
2819 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2820 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2822 p2p_add_wps_vendor_extension(
2823 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2831 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2832 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2834 * This function deinitialize per-interface P2P data.
2836 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2838 if (wpa_s->driver && wpa_s->drv_priv)
2839 wpa_drv_probe_req_report(wpa_s, 0);
2841 if (wpa_s->go_params) {
2842 /* Clear any stored provisioning info */
2843 p2p_clear_provisioning_info(
2845 wpa_s->go_params->peer_device_addr);
2848 os_free(wpa_s->go_params);
2849 wpa_s->go_params = NULL;
2850 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2851 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2852 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2853 wpa_s->p2p_long_listen = 0;
2854 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2855 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2856 wpas_p2p_remove_pending_group_interface(wpa_s);
2858 /* TODO: remove group interface from the driver if this wpa_s instance
2859 * is on top of a P2P group interface */
2864 * wpas_p2p_deinit_global - Deinitialize global P2P module
2865 * @global: Pointer to global data from wpa_supplicant_init()
2867 * This function deinitializes the global (per device) P2P module.
2869 void wpas_p2p_deinit_global(struct wpa_global *global)
2871 struct wpa_supplicant *wpa_s, *tmp;
2873 wpa_s = global->ifaces;
2875 wpas_p2p_service_flush(wpa_s);
2877 if (global->p2p == NULL)
2880 /* Remove remaining P2P group interfaces */
2881 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2882 wpa_s = wpa_s->next;
2884 tmp = global->ifaces;
2887 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2892 /* Disconnect from the P2P group and deinit the interface */
2893 wpas_p2p_disconnect(tmp);
2897 * Deinit GO data on any possibly remaining interface (if main
2898 * interface is used as GO).
2900 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2901 if (wpa_s->ap_iface)
2902 wpas_p2p_group_deinit(wpa_s);
2905 p2p_deinit(global->p2p);
2907 global->p2p_init_wpa_s = NULL;
2911 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2913 if (wpa_s->drv_flags &
2914 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2915 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2916 return 1; /* P2P group requires a new interface in every case
2918 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2919 return 0; /* driver does not support concurrent operations */
2920 if (wpa_s->global->ifaces->next)
2921 return 1; /* more that one interface already in use */
2922 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2923 return 1; /* this interface is already in use */
2928 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2929 const u8 *peer_addr,
2930 enum p2p_wps_method wps_method,
2931 int go_intent, const u8 *own_interface_addr,
2932 unsigned int force_freq, int persistent_group,
2933 struct wpa_ssid *ssid)
2935 if (persistent_group && wpa_s->conf->persistent_reconnect)
2936 persistent_group = 2;
2938 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2939 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2940 go_intent, own_interface_addr,
2941 force_freq, persistent_group);
2945 * Increase GO config timeout if HT40 is used since it takes some time
2946 * to scan channels for coex purposes before the BSS can be started.
2948 p2p_set_config_timeout(wpa_s->global->p2p,
2949 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2951 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2952 go_intent, own_interface_addr, force_freq,
2953 persistent_group, ssid ? ssid->ssid : NULL,
2954 ssid ? ssid->ssid_len : 0,
2955 wpa_s->p2p_pd_before_go_neg);
2959 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2960 const u8 *peer_addr,
2961 enum p2p_wps_method wps_method,
2962 int go_intent, const u8 *own_interface_addr,
2963 unsigned int force_freq, int persistent_group,
2964 struct wpa_ssid *ssid)
2966 if (persistent_group && wpa_s->conf->persistent_reconnect)
2967 persistent_group = 2;
2969 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2972 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2973 go_intent, own_interface_addr, force_freq,
2974 persistent_group, ssid ? ssid->ssid : NULL,
2975 ssid ? ssid->ssid_len : 0);
2979 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2981 wpa_s->p2p_join_scan_count++;
2982 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2983 wpa_s->p2p_join_scan_count);
2984 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2985 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2986 " for join operationg - stop join attempt",
2987 MAC2STR(wpa_s->pending_join_iface_addr));
2988 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2989 if (wpa_s->p2p_auto_pd) {
2990 wpa_s->p2p_auto_pd = 0;
2991 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2992 " p2p_dev_addr=" MACSTR " status=N/A",
2993 MAC2STR(wpa_s->pending_join_dev_addr));
2996 wpa_msg(wpa_s->parent, MSG_INFO,
2997 P2P_EVENT_GROUP_FORMATION_FAILURE);
3002 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
3004 struct wpa_supplicant *wpa_s = eloop_ctx;
3005 if (!wpa_s->pending_pd_before_join)
3008 * Provision Discovery Response may have been lost - try to connect
3009 * anyway since we do not need any information from this PD.
3011 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
3012 "try to connect anyway");
3013 wpas_p2p_join_start(wpa_s);
3017 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3019 struct wpa_supplicant *iface;
3023 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3026 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3027 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3029 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3031 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3032 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3033 shared_freq = iface->current_ssid->frequency;
3034 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3035 shared_freq = iface->assoc_freq;
3039 if (shared_freq && freq != shared_freq) {
3040 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3041 "connected on %d MHz - new connection on "
3042 "%d MHz", iface->ifname, shared_freq, freq);
3047 shared_freq = wpa_drv_shared_freq(wpa_s);
3048 if (shared_freq > 0 && shared_freq != freq) {
3049 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3050 "virtual interface connected on %d MHz - new "
3051 "connection on %d MHz", shared_freq, freq);
3059 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3060 const u8 *peer_dev_addr)
3062 struct wpa_bss *bss;
3065 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3068 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3069 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3074 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3075 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3076 "%ld.%06ld (%supdated in last scan)",
3077 bss->last_update.sec, bss->last_update.usec,
3078 updated ? "": "not ");
3084 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3085 struct wpa_scan_results *scan_res)
3087 struct wpa_bss *bss;
3089 u8 iface_addr[ETH_ALEN];
3091 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3093 if (wpa_s->global->p2p_disabled)
3096 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3097 scan_res ? (int) scan_res->num : -1,
3098 wpa_s->p2p_auto_join ? "auto_" : "");
3101 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3103 if (wpa_s->p2p_auto_pd) {
3104 int join = wpas_p2p_peer_go(wpa_s,
3105 wpa_s->pending_join_dev_addr);
3107 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3108 wpa_s->auto_pd_scan_retry++;
3109 bss = wpa_bss_get_bssid(wpa_s,
3110 wpa_s->pending_join_dev_addr);
3113 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3114 "the peer " MACSTR " at %d MHz",
3115 wpa_s->auto_pd_scan_retry,
3117 pending_join_dev_addr),
3119 wpas_p2p_join_scan_req(wpa_s, freq);
3127 wpa_s->p2p_auto_pd = 0;
3128 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3129 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3130 MAC2STR(wpa_s->pending_join_dev_addr), join);
3131 if (p2p_prov_disc_req(wpa_s->global->p2p,
3132 wpa_s->pending_join_dev_addr,
3133 wpa_s->pending_pd_config_methods, join,
3135 wpa_s->p2p_auto_pd = 0;
3136 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3137 " p2p_dev_addr=" MACSTR " status=N/A",
3138 MAC2STR(wpa_s->pending_join_dev_addr));
3143 if (wpa_s->p2p_auto_join) {
3144 int join = wpas_p2p_peer_go(wpa_s,
3145 wpa_s->pending_join_dev_addr);
3147 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3148 "running a GO -> use GO Negotiation");
3149 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3150 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3151 wpa_s->p2p_persistent_group, 0, 0, 0,
3152 wpa_s->p2p_go_intent,
3153 wpa_s->p2p_connect_freq,
3154 wpa_s->p2p_persistent_id,
3155 wpa_s->p2p_pd_before_go_neg,
3156 wpa_s->p2p_go_ht40);
3160 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3161 "try to join the group", join ? "" :
3164 wpa_s->p2p_fallback_to_go_neg = 1;
3167 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3168 wpa_s->pending_join_iface_addr);
3170 p2p_get_interface_addr(wpa_s->global->p2p,
3171 wpa_s->pending_join_dev_addr,
3173 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3175 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3176 "address for join from " MACSTR " to " MACSTR
3177 " based on newly discovered P2P peer entry",
3178 MAC2STR(wpa_s->pending_join_iface_addr),
3179 MAC2STR(iface_addr));
3180 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3183 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3184 wpa_s->pending_join_iface_addr);
3187 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3188 "from P2P peer table: %d MHz", freq);
3190 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3193 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3194 "from BSS table: %d MHz", freq);
3199 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3200 wpa_msg(wpa_s->parent, MSG_INFO,
3201 P2P_EVENT_GROUP_FORMATION_FAILURE
3202 "reason=FREQ_CONFLICT");
3206 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3207 "prior to joining an existing group (GO " MACSTR
3209 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3210 wpa_s->pending_pd_before_join = 1;
3212 switch (wpa_s->pending_join_wps_method) {
3213 case WPS_PIN_DISPLAY:
3214 method = WPS_CONFIG_KEYPAD;
3216 case WPS_PIN_KEYPAD:
3217 method = WPS_CONFIG_DISPLAY;
3220 method = WPS_CONFIG_PUSHBUTTON;
3227 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3228 wpa_s->pending_join_dev_addr) ==
3231 * We have already performed provision discovery for
3232 * joining the group. Proceed directly to join
3233 * operation without duplicated provision discovery. */
3234 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3235 "with " MACSTR " already done - proceed to "
3237 MAC2STR(wpa_s->pending_join_dev_addr));
3238 wpa_s->pending_pd_before_join = 0;
3242 if (p2p_prov_disc_req(wpa_s->global->p2p,
3243 wpa_s->pending_join_dev_addr, method, 1,
3245 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3246 "Discovery Request before joining an "
3248 wpa_s->pending_pd_before_join = 0;
3253 * Actual join operation will be started from the Action frame
3254 * TX status callback (if no ACK is received) or when the
3255 * Provision Discovery Response is received. Use a short
3256 * timeout as a backup mechanism should the Provision Discovery
3257 * Response be lost for any reason.
3259 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3261 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3266 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3267 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3268 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3269 wpas_p2p_check_join_scan_limit(wpa_s);
3273 /* Start join operation immediately */
3274 wpas_p2p_join_start(wpa_s);
3278 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3281 struct wpa_driver_scan_params params;
3282 struct wpabuf *wps_ie, *ies;
3284 int freqs[2] = { 0, 0 };
3288 /* If freq is not provided, check the operating freq of the GO and do a
3289 * a directed scan to save time
3292 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3293 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3296 os_memset(¶ms, 0, sizeof(params));
3298 /* P2P Wildcard SSID */
3299 params.num_ssids = 1;
3300 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3301 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3303 wpa_s->wps->dev.p2p = 1;
3304 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3305 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3307 if (wps_ie == NULL) {
3308 wpas_p2p_scan_res_join(wpa_s, NULL);
3312 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3313 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3315 wpabuf_free(wps_ie);
3316 wpas_p2p_scan_res_join(wpa_s, NULL);
3319 wpabuf_put_buf(ies, wps_ie);
3320 wpabuf_free(wps_ie);
3322 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3324 params.p2p_probe = 1;
3325 params.extra_ies = wpabuf_head(ies);
3326 params.extra_ies_len = wpabuf_len(ies);
3329 params.freqs = freqs;
3333 * Run a scan to update BSS table and start Provision Discovery once
3334 * the new scan results become available.
3336 ret = wpa_drv_scan(wpa_s, ¶ms);
3338 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3343 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3345 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3346 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3347 wpas_p2p_check_join_scan_limit(wpa_s);
3352 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3354 struct wpa_supplicant *wpa_s = eloop_ctx;
3355 wpas_p2p_join_scan_req(wpa_s, 0);
3359 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3360 const u8 *dev_addr, enum p2p_wps_method wps_method,
3363 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3364 MACSTR " dev " MACSTR ")%s",
3365 MAC2STR(iface_addr), MAC2STR(dev_addr),
3366 auto_join ? " (auto_join)" : "");
3368 wpa_s->p2p_auto_pd = 0;
3369 wpa_s->p2p_auto_join = !!auto_join;
3370 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3371 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3372 wpa_s->pending_join_wps_method = wps_method;
3374 /* Make sure we are not running find during connection establishment */
3375 wpas_p2p_stop_find(wpa_s);
3377 wpa_s->p2p_join_scan_count = 0;
3378 wpas_p2p_join_scan(wpa_s, NULL);
3383 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3385 struct wpa_supplicant *group;
3386 struct p2p_go_neg_results res;
3387 struct wpa_bss *bss;
3389 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
3390 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3393 if (group != wpa_s) {
3394 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3395 sizeof(group->p2p_pin));
3396 group->p2p_wps_method = wpa_s->p2p_wps_method;
3399 group->p2p_in_provisioning = 1;
3400 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3402 os_memset(&res, 0, sizeof(res));
3403 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3405 res.wps_method = wpa_s->pending_join_wps_method;
3406 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3408 res.freq = bss->freq;
3409 res.ssid_len = bss->ssid_len;
3410 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3413 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3414 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3416 wpa_drv_cancel_remain_on_channel(wpa_s);
3417 wpa_s->off_channel_freq = 0;
3418 wpa_s->roc_waiting_drv_freq = 0;
3420 wpas_start_wps_enrollee(group, &res);
3423 * Allow a longer timeout for join-a-running-group than normal 15
3424 * second group formation timeout since the GO may not have authorized
3425 * our connection yet.
3427 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3428 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3436 * wpas_p2p_connect - Request P2P Group Formation to be started
3437 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3438 * @peer_addr: Address of the peer P2P Device
3439 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3440 * @persistent_group: Whether to create a persistent group
3441 * @auto_join: Whether to select join vs. GO Negotiation automatically
3442 * @join: Whether to join an existing group (as a client) instead of starting
3443 * Group Owner negotiation; @peer_addr is BSSID in that case
3444 * @auth: Whether to only authorize the connection instead of doing that and
3445 * initiating Group Owner negotiation
3446 * @go_intent: GO Intent or -1 to use default
3447 * @freq: Frequency for the group or 0 for auto-selection
3448 * @persistent_id: Persistent group credentials to use for forcing GO
3449 * parameters or -1 to generate new values (SSID/passphrase)
3450 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3451 * interoperability workaround when initiating group formation
3452 * @ht40: Start GO with 40 MHz channel width
3453 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3454 * failure, -2 on failure due to channel not currently available,
3455 * -3 if forced channel is not supported
3457 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3458 const char *pin, enum p2p_wps_method wps_method,
3459 int persistent_group, int auto_join, int join, int auth,
3460 int go_intent, int freq, int persistent_id, int pd,
3463 int force_freq = 0, oper_freq = 0;
3466 enum wpa_driver_if_type iftype;
3468 struct wpa_ssid *ssid = NULL;
3470 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3473 if (persistent_id >= 0) {
3474 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3475 if (ssid == NULL || ssid->disabled != 2 ||
3476 ssid->mode != WPAS_MODE_P2P_GO)
3481 go_intent = wpa_s->conf->p2p_go_intent;
3484 wpa_s->p2p_long_listen = 0;
3486 wpa_s->p2p_wps_method = wps_method;
3487 wpa_s->p2p_persistent_group = !!persistent_group;
3488 wpa_s->p2p_persistent_id = persistent_id;
3489 wpa_s->p2p_go_intent = go_intent;
3490 wpa_s->p2p_connect_freq = freq;
3491 wpa_s->p2p_fallback_to_go_neg = 0;
3492 wpa_s->p2p_pd_before_go_neg = !!pd;
3493 wpa_s->p2p_go_ht40 = !!ht40;
3496 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3497 else if (wps_method == WPS_PIN_DISPLAY) {
3498 ret = wps_generate_pin();
3499 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3501 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3504 wpa_s->p2p_pin[0] = '\0';
3506 if (join || auto_join) {
3507 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3509 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3510 "connect a running group from " MACSTR,
3511 MAC2STR(peer_addr));
3512 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3515 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3516 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3518 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3519 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3523 os_get_time(&wpa_s->p2p_auto_started);
3524 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3526 wpa_s->p2p_auto_started.sec,
3527 wpa_s->p2p_auto_started.usec);
3529 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3535 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3537 oper_freq = wpa_s->assoc_freq;
3539 oper_freq = wpa_drv_shared_freq(wpa_s);
3545 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3546 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3547 "(%u MHz) is not supported for P2P uses",
3552 if (oper_freq > 0 && freq != oper_freq &&
3553 !(wpa_s->drv_flags &
3554 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3555 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3556 "on %u MHz while connected on another "
3557 "channel (%u MHz)", freq, oper_freq);
3560 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3561 "requested channel (%u MHz)", freq);
3563 } else if (oper_freq > 0 &&
3564 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3565 if (!(wpa_s->drv_flags &
3566 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3567 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3568 "while connected on non-P2P supported "
3569 "channel (%u MHz)", oper_freq);
3572 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3573 "(%u MHz) not available for P2P - try to use "
3574 "another channel", oper_freq);
3576 } else if (oper_freq > 0) {
3577 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3578 "channel we are already using (%u MHz) on another "
3579 "interface", oper_freq);
3580 force_freq = oper_freq;
3583 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3585 if (wpa_s->create_p2p_iface) {
3586 /* Prepare to add a new interface for the group */
3587 iftype = WPA_IF_P2P_GROUP;
3588 if (go_intent == 15)
3589 iftype = WPA_IF_P2P_GO;
3590 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3591 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3592 "interface for the group");
3596 if_addr = wpa_s->pending_interface_addr;
3598 if_addr = wpa_s->own_addr;
3601 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3603 force_freq, persistent_group, ssid) <
3609 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3610 go_intent, if_addr, force_freq,
3611 persistent_group, ssid) < 0) {
3612 if (wpa_s->create_p2p_iface)
3613 wpas_p2p_remove_pending_group_interface(wpa_s);
3621 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3622 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3623 * @freq: Frequency of the channel in MHz
3624 * @duration: Duration of the stay on the channel in milliseconds
3626 * This callback is called when the driver indicates that it has started the
3627 * requested remain-on-channel duration.
3629 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3630 unsigned int freq, unsigned int duration)
3632 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3634 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3635 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3636 wpa_s->pending_listen_duration);
3637 wpa_s->pending_listen_freq = 0;
3642 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3643 unsigned int timeout)
3645 /* Limit maximum Listen state time based on driver limitation. */
3646 if (timeout > wpa_s->max_remain_on_chan)
3647 timeout = wpa_s->max_remain_on_chan;
3649 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3650 return wpa_drv_p2p_listen(wpa_s, timeout);
3652 return p2p_listen(wpa_s->global->p2p, timeout);
3657 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3658 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3659 * @freq: Frequency of the channel in MHz
3661 * This callback is called when the driver indicates that a remain-on-channel
3662 * operation has been completed, i.e., the duration on the requested channel
3665 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3668 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3669 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3670 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3671 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3673 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3674 return; /* P2P module started a new operation */
3675 if (wpa_s->pending_action_tx)
3677 if (wpa_s->p2p_long_listen > 0)
3678 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3679 if (wpa_s->p2p_long_listen > 0) {
3680 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3681 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3687 * wpas_p2p_group_remove - Remove a P2P group
3688 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3689 * @ifname: Network interface name of the group interface or "*" to remove all
3691 * Returns: 0 on success, -1 on failure
3693 * This function is used to remove a P2P group. This can be used to disconnect
3694 * from a group in which the local end is a P2P Client or to end a P2P Group in
3695 * case the local end is the Group Owner. If a virtual network interface was
3696 * created for this group, that interface will be removed. Otherwise, only the
3697 * configured P2P group network will be removed from the interface.
3699 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3701 struct wpa_global *global = wpa_s->global;
3703 if (os_strcmp(ifname, "*") == 0) {
3704 struct wpa_supplicant *prev;
3705 wpa_s = global->ifaces;
3708 wpa_s = wpa_s->next;
3709 wpas_p2p_disconnect(prev);
3714 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3715 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3719 return wpas_p2p_disconnect(wpa_s);
3723 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3724 struct p2p_go_neg_results *params,
3730 os_memset(params, 0, sizeof(*params));
3731 params->role_go = 1;
3732 params->ht40 = ht40;
3734 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3735 "frequency %d MHz", freq);
3736 params->freq = freq;
3737 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3738 wpa_s->conf->p2p_oper_channel >= 1 &&
3739 wpa_s->conf->p2p_oper_channel <= 11) {
3740 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3741 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3742 "frequency %d MHz", params->freq);
3743 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3744 wpa_s->conf->p2p_oper_reg_class == 124) {
3745 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3746 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3747 "frequency %d MHz", params->freq);
3748 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3749 wpa_s->best_overall_freq > 0 &&
3750 p2p_supported_freq(wpa_s->global->p2p,
3751 wpa_s->best_overall_freq)) {
3752 params->freq = wpa_s->best_overall_freq;
3753 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3754 "channel %d MHz", params->freq);
3755 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3756 wpa_s->best_24_freq > 0 &&
3757 p2p_supported_freq(wpa_s->global->p2p,
3758 wpa_s->best_24_freq)) {
3759 params->freq = wpa_s->best_24_freq;
3760 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3761 "channel %d MHz", params->freq);
3762 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3763 wpa_s->best_5_freq > 0 &&
3764 p2p_supported_freq(wpa_s->global->p2p,
3765 wpa_s->best_5_freq)) {
3766 params->freq = wpa_s->best_5_freq;
3767 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3768 "channel %d MHz", params->freq);
3771 for (chan = 0; chan < 11; chan++) {
3772 params->freq = 2412 + chan * 5;
3773 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3778 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3782 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3783 "known)", params->freq);
3786 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3787 wpa_s->assoc_freq && !freq) {
3788 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3790 params->freq = wpa_s->assoc_freq;
3793 res = wpa_drv_shared_freq(wpa_s);
3794 if (res > 0 && !freq) {
3795 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3796 "already using on a shared interface");
3798 } else if (res > 0 && freq != res &&
3799 !(wpa_s->drv_flags &
3800 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3801 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3802 "while connected on another channel (%u MHz)",
3811 static struct wpa_supplicant *
3812 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3815 struct wpa_supplicant *group_wpa_s;
3817 if (!wpas_p2p_create_iface(wpa_s))
3820 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3821 WPA_IF_P2P_CLIENT) < 0)
3823 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3824 if (group_wpa_s == NULL) {
3825 wpas_p2p_remove_pending_group_interface(wpa_s);
3834 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3835 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3836 * @persistent_group: Whether to create a persistent group
3837 * @freq: Frequency for the group or 0 to indicate no hardcoding
3838 * Returns: 0 on success, -1 on failure
3840 * This function creates a new P2P group with the local end as the Group Owner,
3841 * i.e., without using Group Owner Negotiation.
3843 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3846 struct p2p_go_neg_results params;
3849 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3852 /* Make sure we are not running find during connection establishment */
3853 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3854 wpas_p2p_stop_find(wpa_s);
3857 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3859 if (wpa_s->best_24_freq > 0 &&
3860 p2p_supported_freq(wpa_s->global->p2p,
3861 wpa_s->best_24_freq)) {
3862 freq = wpa_s->best_24_freq;
3863 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3864 "channel: %d MHz", freq);
3866 os_get_random((u8 *) &r, sizeof(r));
3867 freq = 2412 + (r % 3) * 25;
3868 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3869 "channel: %d MHz", freq);
3874 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3876 if (wpa_s->best_5_freq > 0 &&
3877 p2p_supported_freq(wpa_s->global->p2p,
3878 wpa_s->best_5_freq)) {
3879 freq = wpa_s->best_5_freq;
3880 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3881 "channel: %d MHz", freq);
3883 os_get_random((u8 *) &r, sizeof(r));
3884 freq = 5180 + (r % 4) * 20;
3885 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3886 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3887 "5 GHz channel for P2P group");
3890 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3891 "channel: %d MHz", freq);
3895 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3896 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3897 "(%u MHz) is not supported for P2P uses",
3902 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40))
3905 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3906 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3907 "(%u MHz) is not supported for P2P uses",
3911 p2p_go_params(wpa_s->global->p2p, ¶ms);
3912 params.persistent_group = persistent_group;
3914 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3917 wpas_start_wps_go(wpa_s, ¶ms, 0);
3923 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3924 struct wpa_ssid *params, int addr_allocated)
3926 struct wpa_ssid *ssid;
3928 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3932 wpa_supplicant_ap_deinit(wpa_s);
3934 ssid = wpa_config_add_network(wpa_s->conf);
3937 wpa_config_set_network_defaults(ssid);
3938 ssid->temporary = 1;
3939 ssid->proto = WPA_PROTO_RSN;
3940 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3941 ssid->group_cipher = WPA_CIPHER_CCMP;
3942 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3943 ssid->ssid = os_malloc(params->ssid_len);
3944 if (ssid->ssid == NULL) {
3945 wpa_config_remove_network(wpa_s->conf, ssid->id);
3948 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3949 ssid->ssid_len = params->ssid_len;
3950 ssid->p2p_group = 1;
3951 ssid->export_keys = 1;
3952 if (params->psk_set) {
3953 os_memcpy(ssid->psk, params->psk, 32);
3956 if (params->passphrase)
3957 ssid->passphrase = os_strdup(params->passphrase);
3959 wpa_supplicant_select_network(wpa_s, ssid);
3961 wpa_s->show_group_started = 1;
3967 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3968 struct wpa_ssid *ssid, int addr_allocated,
3971 struct p2p_go_neg_results params;
3974 if (ssid->disabled != 2 || ssid->ssid == NULL)
3977 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3978 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3979 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3984 /* Make sure we are not running find during connection establishment */
3985 wpas_p2p_stop_find(wpa_s);
3987 wpa_s->p2p_fallback_to_go_neg = 0;
3989 if (ssid->mode == WPAS_MODE_INFRA)
3990 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3992 if (ssid->mode != WPAS_MODE_P2P_GO)
3995 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40))
3999 if (ssid->passphrase == NULL ||
4000 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4001 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4005 os_strlcpy(params.passphrase, ssid->passphrase,
4006 sizeof(params.passphrase));
4007 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4008 params.ssid_len = ssid->ssid_len;
4009 params.persistent_group = 1;
4011 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4015 wpas_start_wps_go(wpa_s, ¶ms, 0);
4021 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4022 struct wpabuf *proberesp_ies)
4024 struct wpa_supplicant *wpa_s = ctx;
4025 if (wpa_s->ap_iface) {
4026 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4027 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4028 wpabuf_free(beacon_ies);
4029 wpabuf_free(proberesp_ies);
4033 wpabuf_free(hapd->p2p_beacon_ie);
4034 hapd->p2p_beacon_ie = beacon_ies;
4036 wpabuf_free(hapd->p2p_probe_resp_ie);
4037 hapd->p2p_probe_resp_ie = proberesp_ies;
4039 wpabuf_free(beacon_ies);
4040 wpabuf_free(proberesp_ies);
4042 wpa_supplicant_ap_update_beacon(wpa_s);
4046 static void wpas_p2p_idle_update(void *ctx, int idle)
4048 struct wpa_supplicant *wpa_s = ctx;
4049 if (!wpa_s->ap_iface)
4051 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4053 wpas_p2p_set_group_idle_timeout(wpa_s);
4055 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4059 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4060 struct wpa_ssid *ssid)
4062 struct p2p_group *group;
4063 struct p2p_group_config *cfg;
4065 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4067 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4070 cfg = os_zalloc(sizeof(*cfg));
4074 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4075 cfg->persistent_group = 2;
4076 else if (ssid->p2p_persistent_group)
4077 cfg->persistent_group = 1;
4078 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4079 if (wpa_s->max_stations &&
4080 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4081 cfg->max_clients = wpa_s->max_stations;
4083 cfg->max_clients = wpa_s->conf->max_num_sta;
4084 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4085 cfg->ssid_len = ssid->ssid_len;
4086 cfg->cb_ctx = wpa_s;
4087 cfg->ie_update = wpas_p2p_ie_update;
4088 cfg->idle_update = wpas_p2p_idle_update;
4090 group = p2p_group_init(wpa_s->global->p2p, cfg);
4093 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4094 p2p_group_notif_formation_done(group);
4095 wpa_s->p2p_group = group;
4100 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4103 struct wpa_ssid *ssid = wpa_s->current_ssid;
4105 if (!wpa_s->p2p_in_provisioning) {
4106 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4107 "provisioning not in progress");
4111 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4112 u8 go_dev_addr[ETH_ALEN];
4113 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4114 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4116 /* Clear any stored provisioning info */
4117 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4120 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4122 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4124 * Use a separate timeout for initial data connection to
4125 * complete to allow the group to be removed automatically if
4126 * something goes wrong in this step before the P2P group idle
4127 * timeout mechanism is taken into use.
4129 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4130 wpas_p2p_group_formation_timeout,
4131 wpa_s->parent, NULL);
4133 if (wpa_s->global->p2p)
4134 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4135 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4136 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4137 wpas_group_formation_completed(wpa_s, 1);
4141 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4142 struct wps_event_fail *fail)
4144 if (!wpa_s->p2p_in_provisioning) {
4145 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4146 "provisioning not in progress");
4150 if (wpa_s->go_params) {
4151 p2p_clear_provisioning_info(
4153 wpa_s->go_params->peer_device_addr);
4156 wpas_notify_p2p_wps_failed(wpa_s, fail);
4160 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4161 const char *config_method,
4162 enum wpas_p2p_prov_disc_use use)
4166 wpa_s->p2p_fallback_to_go_neg = 0;
4167 wpa_s->pending_pd_use = NORMAL_PD;
4168 if (os_strncmp(config_method, "display", 7) == 0)
4169 config_methods = WPS_CONFIG_DISPLAY;
4170 else if (os_strncmp(config_method, "keypad", 6) == 0)
4171 config_methods = WPS_CONFIG_KEYPAD;
4172 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4173 os_strncmp(config_method, "pushbutton", 10) == 0)
4174 config_methods = WPS_CONFIG_PUSHBUTTON;
4176 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
4180 if (use == WPAS_P2P_PD_AUTO) {
4181 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4182 wpa_s->pending_pd_config_methods = config_methods;
4183 wpa_s->p2p_auto_pd = 1;
4184 wpa_s->p2p_auto_join = 0;
4185 wpa_s->pending_pd_before_join = 0;
4186 wpa_s->auto_pd_scan_retry = 0;
4187 wpas_p2p_stop_find(wpa_s);
4188 wpa_s->p2p_join_scan_count = 0;
4189 os_get_time(&wpa_s->p2p_auto_started);
4190 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4191 wpa_s->p2p_auto_started.sec,
4192 wpa_s->p2p_auto_started.usec);
4193 wpas_p2p_join_scan(wpa_s, NULL);
4197 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4198 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
4200 use == WPAS_P2P_PD_FOR_JOIN);
4203 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4206 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
4207 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4212 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4215 return p2p_scan_result_text(ies, ies_len, buf, end);
4219 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4221 if (!wpa_s->pending_action_tx)
4224 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4225 "operation request");
4226 wpabuf_free(wpa_s->pending_action_tx);
4227 wpa_s->pending_action_tx = NULL;
4231 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4232 enum p2p_discovery_type type,
4233 unsigned int num_req_dev_types, const u8 *req_dev_types,
4234 const u8 *dev_id, unsigned int search_delay)
4236 wpas_p2p_clear_pending_action_tx(wpa_s);
4237 wpa_s->p2p_long_listen = 0;
4239 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4240 return wpa_drv_p2p_find(wpa_s, timeout, type);
4242 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4243 wpa_s->p2p_in_provisioning)
4246 wpa_supplicant_cancel_sched_scan(wpa_s);
4248 return p2p_find(wpa_s->global->p2p, timeout, type,
4249 num_req_dev_types, req_dev_types, dev_id,
4254 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4256 wpas_p2p_clear_pending_action_tx(wpa_s);
4257 wpa_s->p2p_long_listen = 0;
4258 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4259 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4260 wpa_s->p2p_cb_on_scan_complete = 0;
4262 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4263 wpa_drv_p2p_stop_find(wpa_s);
4267 if (wpa_s->global->p2p)
4268 p2p_stop_find(wpa_s->global->p2p);
4270 wpas_p2p_remove_pending_group_interface(wpa_s);
4274 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4276 struct wpa_supplicant *wpa_s = eloop_ctx;
4277 wpa_s->p2p_long_listen = 0;
4281 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4285 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4288 wpa_supplicant_cancel_sched_scan(wpa_s);
4289 wpas_p2p_clear_pending_action_tx(wpa_s);
4293 * This is a request for unlimited Listen state. However, at
4294 * least for now, this is mapped to a Listen state for one
4299 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4300 wpa_s->p2p_long_listen = 0;
4303 * Stop previous find/listen operation to avoid trying to request a new
4304 * remain-on-channel operation while the driver is still running the
4307 if (wpa_s->global->p2p)
4308 p2p_stop_find(wpa_s->global->p2p);
4310 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4311 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4312 wpa_s->p2p_long_listen = timeout * 1000;
4313 eloop_register_timeout(timeout, 0,
4314 wpas_p2p_long_listen_timeout,
4322 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4323 u8 *buf, size_t len, int p2p_group)
4325 struct wpabuf *p2p_ie;
4328 if (wpa_s->global->p2p_disabled)
4330 if (wpa_s->global->p2p == NULL)
4335 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4336 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4338 wpabuf_free(p2p_ie);
4344 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4345 const u8 *dst, const u8 *bssid,
4346 const u8 *ie, size_t ie_len, int ssi_signal)
4348 if (wpa_s->global->p2p_disabled)
4350 if (wpa_s->global->p2p == NULL)
4353 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4355 case P2P_PREQ_NOT_P2P:
4356 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4359 case P2P_PREQ_MALFORMED:
4360 case P2P_PREQ_NOT_LISTEN:
4361 case P2P_PREQ_NOT_PROCESSED:
4362 default: /* make gcc happy */
4364 case P2P_PREQ_PROCESSED:
4370 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4371 const u8 *sa, const u8 *bssid,
4372 u8 category, const u8 *data, size_t len, int freq)
4374 if (wpa_s->global->p2p_disabled)
4376 if (wpa_s->global->p2p == NULL)
4379 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4384 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4386 if (wpa_s->global->p2p_disabled)
4388 if (wpa_s->global->p2p == NULL)
4391 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4395 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4397 p2p_group_deinit(wpa_s->p2p_group);
4398 wpa_s->p2p_group = NULL;
4400 wpa_s->ap_configured_cb = NULL;
4401 wpa_s->ap_configured_cb_ctx = NULL;
4402 wpa_s->ap_configured_cb_data = NULL;
4403 wpa_s->connect_without_scan = NULL;
4407 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4409 wpa_s->p2p_long_listen = 0;
4411 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4412 return wpa_drv_p2p_reject(wpa_s, addr);
4414 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4417 return p2p_reject(wpa_s->global->p2p, addr);
4421 /* Invite to reinvoke a persistent group */
4422 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4423 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4426 enum p2p_invite_role role;
4429 wpa_s->p2p_persistent_go_freq = freq;
4430 wpa_s->p2p_go_ht40 = !!ht40;
4431 if (ssid->mode == WPAS_MODE_P2P_GO) {
4432 role = P2P_INVITE_ROLE_GO;
4433 if (peer_addr == NULL) {
4434 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4435 "address in invitation command");
4438 if (wpas_p2p_create_iface(wpa_s)) {
4439 if (wpas_p2p_add_group_interface(wpa_s,
4440 WPA_IF_P2P_GO) < 0) {
4441 wpa_printf(MSG_ERROR, "P2P: Failed to "
4442 "allocate a new interface for the "
4446 bssid = wpa_s->pending_interface_addr;
4448 bssid = wpa_s->own_addr;
4450 role = P2P_INVITE_ROLE_CLIENT;
4451 peer_addr = ssid->bssid;
4453 wpa_s->pending_invite_ssid_id = ssid->id;
4455 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4456 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4457 ssid->ssid, ssid->ssid_len,
4460 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4463 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4464 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4468 /* Invite to join an active group */
4469 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4470 const u8 *peer_addr, const u8 *go_dev_addr)
4472 struct wpa_global *global = wpa_s->global;
4473 enum p2p_invite_role role;
4475 struct wpa_ssid *ssid;
4478 wpa_s->p2p_persistent_go_freq = 0;
4479 wpa_s->p2p_go_ht40 = 0;
4481 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4482 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4485 if (wpa_s == NULL) {
4486 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4490 ssid = wpa_s->current_ssid;
4492 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4497 persistent = ssid->p2p_persistent_group &&
4498 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4499 ssid->ssid, ssid->ssid_len);
4501 if (ssid->mode == WPAS_MODE_P2P_GO) {
4502 role = P2P_INVITE_ROLE_ACTIVE_GO;
4503 bssid = wpa_s->own_addr;
4504 if (go_dev_addr == NULL)
4505 go_dev_addr = wpa_s->global->p2p_dev_addr;
4507 role = P2P_INVITE_ROLE_CLIENT;
4508 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4509 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4510 "invite to current group");
4513 bssid = wpa_s->bssid;
4514 if (go_dev_addr == NULL &&
4515 !is_zero_ether_addr(wpa_s->go_dev_addr))
4516 go_dev_addr = wpa_s->go_dev_addr;
4518 wpa_s->parent->pending_invite_ssid_id = -1;
4520 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4521 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4522 ssid->ssid, ssid->ssid_len,
4523 go_dev_addr, persistent);
4525 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4528 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4529 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
4530 go_dev_addr, persistent);
4534 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4536 struct wpa_ssid *ssid = wpa_s->current_ssid;
4537 const char *ssid_txt;
4538 u8 go_dev_addr[ETH_ALEN];
4539 int network_id = -1;
4543 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4544 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4545 wpa_s->parent, NULL);
4548 if (!wpa_s->show_group_started || !ssid)
4551 wpa_s->show_group_started = 0;
4553 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4554 os_memset(go_dev_addr, 0, ETH_ALEN);
4555 if (ssid->bssid_set)
4556 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4557 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4559 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4561 if (wpa_s->global->p2p_group_formation == wpa_s)
4562 wpa_s->global->p2p_group_formation = NULL;
4564 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4565 (int) wpa_s->assoc_freq;
4566 if (ssid->passphrase == NULL && ssid->psk_set) {
4568 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4569 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4570 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4572 wpa_s->ifname, ssid_txt, freq, psk,
4573 MAC2STR(go_dev_addr),
4574 persistent ? " [PERSISTENT]" : "");
4576 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4577 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4578 "go_dev_addr=" MACSTR "%s",
4579 wpa_s->ifname, ssid_txt, freq,
4580 ssid->passphrase ? ssid->passphrase : "",
4581 MAC2STR(go_dev_addr),
4582 persistent ? " [PERSISTENT]" : "");
4586 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4589 network_id = ssid->id;
4590 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4593 if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4594 wpa_s->global->p2p != NULL) {
4595 wpa_s->p2p_cb_on_scan_complete = 0;
4596 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4597 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4598 "continued after successful connection");
4599 p2p_increase_search_delay(
4601 wpas_p2p_search_delay(wpa_s));
4607 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4608 u32 interval1, u32 duration2, u32 interval2)
4610 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4612 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4615 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4616 wpa_s->current_ssid == NULL ||
4617 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4620 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4621 wpa_s->own_addr, wpa_s->assoc_freq,
4622 duration1, interval1, duration2, interval2);
4626 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4627 unsigned int interval)
4629 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4632 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4635 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4639 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4641 return wpa_s->current_ssid != NULL &&
4642 wpa_s->current_ssid->p2p_group &&
4643 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4647 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4649 struct wpa_supplicant *wpa_s = eloop_ctx;
4651 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4652 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4657 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4659 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
4663 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4667 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4668 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4670 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4673 timeout = wpa_s->conf->p2p_group_idle;
4674 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4675 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4676 timeout = P2P_MAX_CLIENT_IDLE;
4682 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4683 timeout = 0; /* special client mode no-timeout */
4688 if (wpa_s->p2p_in_provisioning) {
4690 * Use the normal group formation timeout during the
4691 * provisioning phase to avoid terminating this process too
4692 * early due to group idle timeout.
4694 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4695 "during provisioning");
4699 if (wpa_s->show_group_started) {
4701 * Use the normal group formation timeout between the end of
4702 * the provisioning phase and completion of 4-way handshake to
4703 * avoid terminating this process too early due to group idle
4706 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4707 "while waiting for initial 4-way handshake to "
4713 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4715 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4720 /* Returns 1 if the interface was removed */
4721 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4722 u16 reason_code, const u8 *ie, size_t ie_len,
4723 int locally_generated)
4725 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4727 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4730 if (!locally_generated)
4731 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4734 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4735 wpa_s->current_ssid &&
4736 wpa_s->current_ssid->p2p_group &&
4737 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4738 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4739 "session is ending");
4740 if (wpas_p2p_group_delete(wpa_s,
4741 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4750 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4751 u16 reason_code, const u8 *ie, size_t ie_len,
4752 int locally_generated)
4754 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4756 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4759 if (!locally_generated)
4760 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4765 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4767 struct p2p_data *p2p = wpa_s->global->p2p;
4772 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4775 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4776 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4778 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4779 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4782 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4783 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4785 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4786 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4788 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4789 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4790 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4791 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4792 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4795 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4796 p2p_set_sec_dev_types(p2p,
4797 (void *) wpa_s->conf->sec_device_type,
4798 wpa_s->conf->num_sec_device_types);
4800 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4802 p2p_remove_wps_vendor_extensions(p2p);
4803 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4804 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4806 p2p_add_wps_vendor_extension(
4807 p2p, wpa_s->conf->wps_vendor_ext[i]);
4811 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4812 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4814 country[0] = wpa_s->conf->country[0];
4815 country[1] = wpa_s->conf->country[1];
4817 p2p_set_country(p2p, country);
4820 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4821 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4822 wpa_s->conf->p2p_ssid_postfix ?
4823 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4827 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4828 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4830 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4831 u8 reg_class, channel;
4834 if (wpa_s->conf->p2p_listen_reg_class &&
4835 wpa_s->conf->p2p_listen_channel) {
4836 reg_class = wpa_s->conf->p2p_listen_reg_class;
4837 channel = wpa_s->conf->p2p_listen_channel;
4841 * Pick one of the social channels randomly as the
4844 os_get_random((u8 *) &r, sizeof(r));
4845 channel = 1 + (r % 3) * 5;
4847 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4849 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4852 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4853 u8 op_reg_class, op_channel, cfg_op_channel;
4856 if (wpa_s->conf->p2p_oper_reg_class &&
4857 wpa_s->conf->p2p_oper_channel) {
4858 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4859 op_channel = wpa_s->conf->p2p_oper_channel;
4864 * Use random operation channel from (1, 6, 11)
4865 *if no other preference is indicated.
4867 os_get_random((u8 *) &r, sizeof(r));
4868 op_channel = 1 + (r % 3) * 5;
4871 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4874 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4878 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4879 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4880 wpa_s->conf->p2p_pref_chan) < 0) {
4881 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4888 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4891 if (!wpa_s->ap_iface)
4893 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4898 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4900 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4902 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4905 wpa_s->global->cross_connection = enabled;
4906 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4909 struct wpa_supplicant *iface;
4911 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4913 if (iface->cross_connect_enabled == 0)
4916 iface->cross_connect_enabled = 0;
4917 iface->cross_connect_in_use = 0;
4918 wpa_msg(iface->parent, MSG_INFO,
4919 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4920 iface->ifname, iface->cross_connect_uplink);
4928 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4930 struct wpa_supplicant *iface;
4932 if (!uplink->global->cross_connection)
4935 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4936 if (!iface->cross_connect_enabled)
4938 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4941 if (iface->ap_iface == NULL)
4943 if (iface->cross_connect_in_use)
4946 iface->cross_connect_in_use = 1;
4947 wpa_msg(iface->parent, MSG_INFO,
4948 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4949 iface->ifname, iface->cross_connect_uplink);
4954 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4956 struct wpa_supplicant *iface;
4958 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4959 if (!iface->cross_connect_enabled)
4961 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4964 if (!iface->cross_connect_in_use)
4967 wpa_msg(iface->parent, MSG_INFO,
4968 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4969 iface->ifname, iface->cross_connect_uplink);
4970 iface->cross_connect_in_use = 0;
4975 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4977 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4978 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4979 wpa_s->cross_connect_disallowed)
4980 wpas_p2p_disable_cross_connect(wpa_s);
4982 wpas_p2p_enable_cross_connect(wpa_s);
4983 if (!wpa_s->ap_iface &&
4984 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4985 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4989 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4991 wpas_p2p_disable_cross_connect(wpa_s);
4992 if (!wpa_s->ap_iface &&
4993 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4995 wpas_p2p_set_group_idle_timeout(wpa_s);
4999 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5001 struct wpa_supplicant *iface;
5003 if (!wpa_s->global->cross_connection)
5006 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5009 if (iface->drv_flags &
5010 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5012 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5015 wpa_s->cross_connect_enabled = 1;
5016 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5017 sizeof(wpa_s->cross_connect_uplink));
5018 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5019 "%s to %s whenever uplink is available",
5020 wpa_s->ifname, wpa_s->cross_connect_uplink);
5022 if (iface->ap_iface || iface->current_ssid == NULL ||
5023 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5024 iface->cross_connect_disallowed ||
5025 iface->wpa_state != WPA_COMPLETED)
5028 wpa_s->cross_connect_in_use = 1;
5029 wpa_msg(wpa_s->parent, MSG_INFO,
5030 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5031 wpa_s->ifname, wpa_s->cross_connect_uplink);
5037 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5039 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5040 !wpa_s->p2p_in_provisioning)
5041 return 0; /* not P2P client operation */
5043 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5045 if (wpa_s != wpa_s->parent)
5046 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5048 if (wpa_s->global->p2p)
5049 p2p_group_formation_failed(wpa_s->global->p2p);
5051 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5052 wpa_s->parent, NULL);
5054 wpas_group_formation_completed(wpa_s, 0);
5059 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5061 struct p2p_channels chan;
5063 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5066 os_memset(&chan, 0, sizeof(chan));
5067 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5068 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5073 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5077 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5079 struct wpa_global *global = wpa_s->global;
5083 if (global->p2p == NULL)
5086 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5088 if (wpa_s->pending_interface_name[0] &&
5089 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5092 peer = p2p_get_go_neg_peer(global->p2p);
5094 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5095 MACSTR, MAC2STR(peer));
5096 p2p_unauthorize(global->p2p, peer);
5100 wpas_p2p_stop_find(wpa_s);
5102 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5103 if (wpa_s == global->p2p_group_formation &&
5104 (wpa_s->p2p_in_provisioning ||
5105 wpa_s->parent->pending_interface_type ==
5106 WPA_IF_P2P_CLIENT)) {
5107 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5108 "formation found - cancelling",
5111 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5112 wpa_s->parent, NULL);
5113 wpas_p2p_group_delete(wpa_s,
5114 P2P_GROUP_REMOVAL_REQUESTED);
5120 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5128 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5130 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5133 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5134 "being available anymore");
5135 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
5139 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5140 int freq_24, int freq_5, int freq_overall)
5142 struct p2p_data *p2p = wpa_s->global->p2p;
5143 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5145 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5149 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5152 struct p2p_data *p2p = wpa_s->global->p2p;
5154 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5157 if (hwaddr_aton(addr, peer))
5160 return p2p_unauthorize(p2p, peer);
5165 * wpas_p2p_disconnect - Disconnect from a P2P Group
5166 * @wpa_s: Pointer to wpa_supplicant data
5167 * Returns: 0 on success, -1 on failure
5169 * This can be used to disconnect from a group in which the local end is a P2P
5170 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5171 * virtual network interface was created for this group, that interface will be
5172 * removed. Otherwise, only the configured P2P group network will be removed
5173 * from the interface.
5175 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5181 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5186 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5189 struct wpa_supplicant *group = wpa_s;
5192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5196 while (group && (group->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)) {
5197 if(group->wpa_state == WPA_ASSOCIATED) {
5198 /* WPA_ASSOCIATED hasn't moved to WPA_COMPLETED. So it could be in WPS
5199 * or 4Way Hanshake phase. Avoid allowing scan during this time critical
5202 wpa_printf(MSG_ERROR, "P2P: WPS/4way handshake in Progress."
5206 group = group->next;
5210 return p2p_in_progress(wpa_s->global->p2p);
5214 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5215 struct wpa_ssid *ssid)
5217 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5218 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5219 wpa_s->parent, NULL) > 0) {
5221 * Remove the network by scheduling the group formation
5222 * timeout to happen immediately. The teardown code
5223 * needs to be scheduled to run asynch later so that we
5224 * don't delete data from under ourselves unexpectedly.
5225 * Calling wpas_p2p_group_formation_timeout directly
5226 * causes a series of crashes in WPS failure scenarios.
5228 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5229 "P2P group network getting removed");
5230 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5231 wpa_s->parent, NULL);
5236 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
5237 const u8 *addr, const u8 *ssid,
5243 for (s = wpa_s->conf->ssid; s; s = s->next) {
5244 if (s->disabled != 2)
5247 (ssid_len != s->ssid_len ||
5248 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5250 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5251 return s; /* peer is GO in the persistent group */
5252 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5254 for (i = 0; i < s->num_p2p_clients; i++) {
5255 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5256 addr, ETH_ALEN) == 0)
5257 return s; /* peer is P2P client in persistent
5266 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5271 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5275 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5278 struct wpa_supplicant *group = wpa_s;
5279 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5280 if (wpa_s->global->p2p_group_formation)
5281 group = wpa_s->global->p2p_group_formation;
5282 wpa_s = wpa_s->parent;
5283 offchannel_send_action_done(wpa_s);
5285 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
5286 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5287 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5288 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5289 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5290 wpa_s->p2p_persistent_id,
5291 wpa_s->p2p_pd_before_go_neg,
5292 wpa_s->p2p_go_ht40);
5296 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5298 if (!wpa_s->p2p_fallback_to_go_neg ||
5299 wpa_s->p2p_in_provisioning <= 5)
5302 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5303 return 0; /* peer operating as a GO */
5305 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5306 "fallback to GO Negotiation");
5307 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5313 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5315 const char *rn, *rn2;
5316 struct wpa_supplicant *ifs;
5318 if (wpa_s->wpa_state > WPA_SCANNING) {
5319 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5320 "concurrent operation",
5321 P2P_CONCURRENT_SEARCH_DELAY);
5322 return P2P_CONCURRENT_SEARCH_DELAY;
5325 if (!wpa_s->driver->get_radio_name)
5327 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5328 if (rn == NULL || rn[0] == '\0')
5331 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5332 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5335 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5336 if (!rn2 || os_strcmp(rn, rn2) != 0)
5338 if (ifs->wpa_state > WPA_SCANNING) {
5339 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5340 "delay due to concurrent operation on "
5342 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5343 return P2P_CONCURRENT_SEARCH_DELAY;
5351 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
5353 struct wpa_supplicant *iface = NULL;
5354 struct p2p_data *p2p = wpa_s->global->p2p;
5356 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5357 if((iface->p2p_group_interface) && (iface->current_ssid) &&
5358 (iface->current_ssid->frequency != freq)) {
5360 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
5361 /* Try to see whether we can move the GO. If it
5362 * is not possible, remove the GO interface
5364 if(wpa_drv_switch_channel(iface, freq) == 0) {
5365 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5366 iface->current_ssid->frequency = freq;
5371 /* If GO cannot be moved or if the conflicting interface is a
5372 * P2P Client, remove the interface depending up on the connection
5374 if(!wpas_is_p2p_prioritized(wpa_s)) {
5375 /* STA connection has priority over existing
5376 * P2P connection. So remove the interface */
5377 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5378 "concurrent mode frequency conflict");
5379 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
5381 /* Existing connection has the priority. Disable the newly
5382 * selected network and let the application know about it.