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_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
306 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
311 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
313 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
314 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
315 wpa_s->reassociate = 0;
316 wpa_s->disconnected = 1;
317 wpa_supplicant_deauthenticate(wpa_s,
318 WLAN_REASON_DEAUTH_LEAVING);
322 if (wpa_s->cross_connect_in_use) {
323 wpa_s->cross_connect_in_use = 0;
324 wpa_msg(wpa_s->parent, MSG_INFO,
325 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
326 wpa_s->ifname, wpa_s->cross_connect_uplink);
328 switch (removal_reason) {
329 case P2P_GROUP_REMOVAL_REQUESTED:
330 reason = " reason=REQUESTED";
332 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
333 reason = " reason=FORMATION_FAILED";
335 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
336 reason = " reason=IDLE";
338 case P2P_GROUP_REMOVAL_UNAVAILABLE:
339 reason = " reason=UNAVAILABLE";
341 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
342 reason = " reason=GO_ENDING_SESSION";
345 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
346 reason = " reason=FREQ_CONFLICT";
353 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
354 wpa_msg(wpa_s->parent, MSG_INFO,
355 P2P_EVENT_GROUP_REMOVED "%s %s%s",
356 wpa_s->ifname, gtype, reason);
359 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
360 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
362 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
363 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
365 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
366 struct wpa_global *global;
368 enum wpa_driver_if_type type;
369 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
371 global = wpa_s->global;
372 ifname = os_strdup(wpa_s->ifname);
373 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
374 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
375 wpa_s = global->ifaces;
377 wpa_drv_if_remove(wpa_s, type, ifname);
382 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
383 if (ssid && (ssid->p2p_group ||
384 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
385 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
387 if (ssid == wpa_s->current_ssid) {
388 wpa_sm_set_config(wpa_s->wpa, NULL);
389 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
390 wpa_s->current_ssid = NULL;
393 * Networks objects created during any P2P activities are not
394 * exposed out as they might/will confuse certain non-P2P aware
395 * applications since these network objects won't behave like
398 * Likewise, we don't send out network removed signals for such
401 wpa_config_remove_network(wpa_s->conf, id);
402 wpa_supplicant_clear_status(wpa_s);
403 wpa_supplicant_cancel_sched_scan(wpa_s);
405 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
409 wpa_supplicant_ap_deinit(wpa_s);
411 wpa_drv_deinit_p2p_cli(wpa_s);
417 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
419 const u8 *ssid, size_t ssid_len)
427 if (wpa_s->go_params)
428 bssid = wpa_s->go_params->peer_interface_addr;
430 bssid = wpa_s->bssid;
432 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
434 u8 iface_addr[ETH_ALEN];
435 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
437 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
440 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
441 "group is persistent - BSS " MACSTR " not found",
446 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
448 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
449 "group is persistent - BSS " MACSTR
450 " did not include P2P IE", MAC2STR(bssid));
451 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
452 (u8 *) (bss + 1), bss->ie_len);
453 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
454 ((u8 *) bss + 1) + bss->ie_len,
459 group_capab = p2p_get_group_capab(p2p);
460 addr = p2p_get_go_dev_addr(p2p);
461 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
462 "group_capab=0x%x", group_capab);
464 os_memcpy(go_dev_addr, addr, ETH_ALEN);
465 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
468 os_memset(go_dev_addr, 0, ETH_ALEN);
471 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
472 "go_dev_addr=" MACSTR,
473 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
475 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
479 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
480 struct wpa_ssid *ssid,
481 const u8 *go_dev_addr)
486 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
487 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
488 for (s = wpa_s->conf->ssid; s; s = s->next) {
489 if (s->disabled == 2 &&
490 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
491 s->ssid_len == ssid->ssid_len &&
492 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
497 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
499 if (ssid->passphrase && !s->passphrase)
501 else if (ssid->passphrase && s->passphrase &&
502 os_strcmp(ssid->passphrase, s->passphrase) != 0)
505 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
508 s = wpa_config_add_network(wpa_s->conf);
513 * Instead of network_added we emit persistent_group_added
514 * notification. Also to keep the defense checks in
515 * persistent_group obj registration method, we set the
516 * relevant flags in s to designate it as a persistent group.
519 s->p2p_persistent_group = 1;
520 wpas_notify_persistent_group_added(wpa_s, s);
521 wpa_config_set_network_defaults(s);
525 s->p2p_persistent_group = 1;
528 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
529 s->mode = ssid->mode;
530 s->auth_alg = WPA_AUTH_ALG_OPEN;
531 s->key_mgmt = WPA_KEY_MGMT_PSK;
532 s->proto = WPA_PROTO_RSN;
533 s->pairwise_cipher = WPA_CIPHER_CCMP;
535 if (ssid->passphrase) {
536 os_free(s->passphrase);
537 s->passphrase = os_strdup(ssid->passphrase);
541 os_memcpy(s->psk, ssid->psk, 32);
543 if (s->passphrase && !s->psk_set)
544 wpa_config_update_psk(s);
545 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
547 s->ssid = os_malloc(ssid->ssid_len);
550 s->ssid_len = ssid->ssid_len;
551 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
554 #ifndef CONFIG_NO_CONFIG_WRITE
555 if (changed && wpa_s->conf->update_config &&
556 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
557 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
559 #endif /* CONFIG_NO_CONFIG_WRITE */
565 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
568 struct wpa_ssid *ssid, *s;
573 ssid = wpa_s->current_ssid;
574 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
575 !ssid->p2p_persistent_group)
578 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
579 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
582 if (s->ssid_len == ssid->ssid_len &&
583 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
590 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
591 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
595 if (i == s->num_p2p_clients - 1)
596 return; /* already the most recent entry */
598 /* move the entry to mark it most recent */
599 os_memmove(s->p2p_client_list + i * ETH_ALEN,
600 s->p2p_client_list + (i + 1) * ETH_ALEN,
601 (s->num_p2p_clients - i - 1) * ETH_ALEN);
602 os_memcpy(s->p2p_client_list +
603 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
608 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
609 n = os_realloc_array(s->p2p_client_list,
610 s->num_p2p_clients + 1, ETH_ALEN);
613 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
614 s->p2p_client_list = n;
615 s->num_p2p_clients++;
617 /* Not enough room for an additional entry - drop the oldest
619 os_memmove(s->p2p_client_list,
620 s->p2p_client_list + ETH_ALEN,
621 (s->num_p2p_clients - 1) * ETH_ALEN);
622 os_memcpy(s->p2p_client_list +
623 (s->num_p2p_clients - 1) * ETH_ALEN,
627 #ifndef CONFIG_NO_CONFIG_WRITE
628 if (wpa_s->parent->conf->update_config &&
629 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
630 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
631 #endif /* CONFIG_NO_CONFIG_WRITE */
635 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
638 struct wpa_ssid *ssid;
639 const char *ssid_txt;
642 u8 go_dev_addr[ETH_ALEN];
646 * This callback is likely called for the main interface. Update wpa_s
647 * to use the group interface if a new interface was created for the
650 if (wpa_s->global->p2p_group_formation)
651 wpa_s = wpa_s->global->p2p_group_formation;
652 wpa_s->global->p2p_group_formation = NULL;
653 wpa_s->p2p_in_provisioning = 0;
656 wpa_msg(wpa_s->parent, MSG_INFO,
657 P2P_EVENT_GROUP_FORMATION_FAILURE);
658 wpas_p2p_group_delete(wpa_s,
659 P2P_GROUP_REMOVAL_FORMATION_FAILED);
663 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
665 ssid = wpa_s->current_ssid;
666 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
667 ssid->mode = WPAS_MODE_P2P_GO;
668 p2p_group_notif_formation_done(wpa_s->p2p_group);
669 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
674 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
675 client = ssid->mode == WPAS_MODE_INFRA;
676 if (ssid->mode == WPAS_MODE_P2P_GO) {
677 persistent = ssid->p2p_persistent_group;
678 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
681 persistent = wpas_p2p_persistent_group(wpa_s,
687 client = wpa_s->p2p_group_interface ==
688 P2P_GROUP_INTERFACE_CLIENT;
689 os_memset(go_dev_addr, 0, ETH_ALEN);
692 wpa_s->show_group_started = 0;
695 * Indicate event only after successfully completed 4-way
696 * handshake, i.e., when the interface is ready for data
699 wpa_s->show_group_started = 1;
701 /* For client Second phase of Group formation (4-way handshake) can be still pending
702 * So we need to restore wpa_s->global->p2p_group_formation */
703 wpa_s->global->p2p_group_formation = wpa_s;
706 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
708 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
709 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
710 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
712 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
713 MAC2STR(go_dev_addr),
714 persistent ? " [PERSISTENT]" : "");
715 wpas_p2p_cross_connect_setup(wpa_s);
716 wpas_p2p_set_group_idle_timeout(wpa_s);
718 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
719 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
720 "go_dev_addr=" MACSTR "%s",
721 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
722 ssid && ssid->passphrase ? ssid->passphrase : "",
723 MAC2STR(go_dev_addr),
724 persistent ? " [PERSISTENT]" : "");
725 wpas_p2p_cross_connect_setup(wpa_s);
726 wpas_p2p_set_group_idle_timeout(wpa_s);
730 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
732 if (network_id < 0 && ssid)
733 network_id = ssid->id;
735 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
739 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
741 const u8 *dst, const u8 *src,
743 const u8 *data, size_t data_len,
744 enum offchannel_send_action_result
747 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
749 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
751 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
755 case OFFCHANNEL_SEND_ACTION_SUCCESS:
756 res = P2P_SEND_ACTION_SUCCESS;
758 case OFFCHANNEL_SEND_ACTION_NO_ACK:
759 res = P2P_SEND_ACTION_NO_ACK;
761 case OFFCHANNEL_SEND_ACTION_FAILED:
762 res = P2P_SEND_ACTION_FAILED;
766 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
768 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
769 wpa_s->pending_pd_before_join &&
770 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
771 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
772 wpa_s->pending_pd_before_join = 0;
773 if (wpa_s->p2p_fallback_to_go_neg) {
774 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
775 "during p2p_connect-auto");
776 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
780 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
781 "join-existing-group operation (no ACK for PD "
783 wpas_p2p_join_start(wpa_s);
788 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
789 const u8 *src, const u8 *bssid, const u8 *buf,
790 size_t len, unsigned int wait_time)
792 struct wpa_supplicant *wpa_s = ctx;
793 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
795 wpas_p2p_send_action_tx_status, 1);
799 static void wpas_send_action_done(void *ctx)
801 struct wpa_supplicant *wpa_s = ctx;
802 offchannel_send_action_done(wpa_s);
806 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
807 struct p2p_go_neg_results *params)
809 if (wpa_s->go_params == NULL) {
810 wpa_s->go_params = os_malloc(sizeof(*params));
811 if (wpa_s->go_params == NULL)
814 os_memcpy(wpa_s->go_params, params, sizeof(*params));
819 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
820 struct p2p_go_neg_results *res)
822 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
823 MAC2STR(res->peer_interface_addr));
824 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
825 res->ssid, res->ssid_len);
826 wpa_supplicant_ap_deinit(wpa_s);
827 wpas_copy_go_neg_results(wpa_s, res);
828 if (res->wps_method == WPS_PBC)
829 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
831 u16 dev_pw_id = DEV_PW_DEFAULT;
832 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
833 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
834 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
835 wpa_s->p2p_pin, 1, dev_pw_id);
840 static void p2p_go_configured(void *ctx, void *data)
842 struct wpa_supplicant *wpa_s = ctx;
843 struct p2p_go_neg_results *params = data;
844 struct wpa_ssid *ssid;
847 ssid = wpa_s->current_ssid;
848 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
849 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
850 if (wpa_s->global->p2p_group_formation == wpa_s)
851 wpa_s->global->p2p_group_formation = NULL;
852 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
853 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
854 "go_dev_addr=" MACSTR "%s",
856 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
858 params->passphrase ? params->passphrase : "",
859 MAC2STR(wpa_s->global->p2p_dev_addr),
860 params->persistent_group ? " [PERSISTENT]" : "");
862 if (params->persistent_group)
863 network_id = wpas_p2p_store_persistent_group(
865 wpa_s->global->p2p_dev_addr);
867 network_id = ssid->id;
868 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
869 wpas_p2p_cross_connect_setup(wpa_s);
870 wpas_p2p_set_group_idle_timeout(wpa_s);
874 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
875 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
876 params->peer_interface_addr)) {
877 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
881 if (params->wps_method == WPS_PBC)
882 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
883 params->peer_device_addr);
884 else if (wpa_s->p2p_pin[0])
885 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
886 wpa_s->p2p_pin, NULL, 0);
887 os_free(wpa_s->go_params);
888 wpa_s->go_params = NULL;
892 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
893 struct p2p_go_neg_results *params,
896 struct wpa_ssid *ssid;
898 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
901 ssid = wpa_config_add_network(wpa_s->conf);
905 wpa_s->show_group_started = 0;
907 wpa_config_set_network_defaults(ssid);
910 ssid->p2p_persistent_group = params->persistent_group;
911 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
913 ssid->frequency = params->freq;
914 ssid->ht40 = params->ht40;
915 ssid->ssid = os_zalloc(params->ssid_len + 1);
917 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
918 ssid->ssid_len = params->ssid_len;
920 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
921 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
922 ssid->proto = WPA_PROTO_RSN;
923 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
924 ssid->passphrase = os_strdup(params->passphrase);
925 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
927 wpa_s->ap_configured_cb = p2p_go_configured;
928 wpa_s->ap_configured_cb_ctx = wpa_s;
929 wpa_s->ap_configured_cb_data = wpa_s->go_params;
930 wpa_s->connect_without_scan = ssid;
931 wpa_s->reassociate = 1;
932 wpa_s->disconnected = 0;
933 wpa_supplicant_req_scan(wpa_s, 0, 0);
937 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
938 const struct wpa_supplicant *src)
940 struct wpa_config *d;
941 const struct wpa_config *s;
946 #define C(n) if (s->n) d->n = os_strdup(s->n)
955 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
956 os_memcpy(d->sec_device_type, s->sec_device_type,
957 sizeof(d->sec_device_type));
958 d->num_sec_device_types = s->num_sec_device_types;
960 d->p2p_group_idle = s->p2p_group_idle;
961 d->p2p_intra_bss = s->p2p_intra_bss;
962 d->persistent_reconnect = s->persistent_reconnect;
963 d->max_num_sta = s->max_num_sta;
964 d->pbc_in_m1 = s->pbc_in_m1;
968 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
969 enum wpa_driver_if_type type)
971 char ifname[120], force_ifname[120];
973 if (wpa_s->pending_interface_name[0]) {
974 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
975 "- skip creation of a new one");
976 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
977 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
978 "unknown?! ifname='%s'",
979 wpa_s->pending_interface_name);
985 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
986 wpa_s->p2p_group_idx);
987 if (os_strlen(ifname) >= IFNAMSIZ &&
988 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
989 /* Try to avoid going over the IFNAMSIZ length limit */
990 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
991 wpa_s->p2p_group_idx);
993 force_ifname[0] = '\0';
995 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
997 wpa_s->p2p_group_idx++;
999 wpa_s->pending_interface_type = type;
1000 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1001 wpa_s->pending_interface_addr, NULL) < 0) {
1002 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1007 if (force_ifname[0]) {
1008 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1010 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1011 sizeof(wpa_s->pending_interface_name));
1013 os_strlcpy(wpa_s->pending_interface_name, ifname,
1014 sizeof(wpa_s->pending_interface_name));
1015 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1016 MACSTR, wpa_s->pending_interface_name,
1017 MAC2STR(wpa_s->pending_interface_addr));
1023 static void wpas_p2p_remove_pending_group_interface(
1024 struct wpa_supplicant *wpa_s)
1026 if (!wpa_s->pending_interface_name[0] ||
1027 is_zero_ether_addr(wpa_s->pending_interface_addr))
1028 return; /* No pending virtual interface */
1030 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1031 wpa_s->pending_interface_name);
1032 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1033 wpa_s->pending_interface_name);
1034 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1035 wpa_s->pending_interface_name[0] = '\0';
1039 static struct wpa_supplicant *
1040 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1042 struct wpa_interface iface;
1043 struct wpa_supplicant *group_wpa_s;
1045 if (!wpa_s->pending_interface_name[0]) {
1046 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1047 if (!wpas_p2p_create_iface(wpa_s))
1050 * Something has forced us to remove the pending interface; try
1051 * to create a new one and hope for the best that we will get
1052 * the same local address.
1054 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1055 WPA_IF_P2P_CLIENT) < 0)
1059 os_memset(&iface, 0, sizeof(iface));
1060 iface.ifname = wpa_s->pending_interface_name;
1061 iface.driver = wpa_s->driver->name;
1062 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1063 iface.driver_param = wpa_s->conf->driver_param;
1064 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1065 if (group_wpa_s == NULL) {
1066 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1067 "wpa_supplicant interface");
1070 wpa_s->pending_interface_name[0] = '\0';
1071 group_wpa_s->parent = wpa_s;
1072 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1073 P2P_GROUP_INTERFACE_CLIENT;
1074 wpa_s->global->p2p_group_formation = group_wpa_s;
1076 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1082 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1085 struct wpa_supplicant *wpa_s = eloop_ctx;
1086 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1087 if (wpa_s->global->p2p)
1088 p2p_group_formation_failed(wpa_s->global->p2p);
1089 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1090 wpa_drv_p2p_group_formation_failed(wpa_s);
1091 wpas_group_formation_completed(wpa_s, 0);
1095 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1097 struct wpa_supplicant *wpa_s = ctx;
1099 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1100 wpa_drv_cancel_remain_on_channel(wpa_s);
1101 wpa_s->off_channel_freq = 0;
1102 wpa_s->roc_waiting_drv_freq = 0;
1106 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1108 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1109 wpas_p2p_remove_pending_group_interface(wpa_s);
1113 if (wpa_s->p2p_go_ht40)
1116 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1117 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1119 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1120 struct wpa_ssid *ssid;
1121 ssid = wpa_config_get_network(wpa_s->conf,
1122 wpa_s->p2p_persistent_id);
1123 if (ssid && ssid->disabled == 2 &&
1124 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1125 size_t len = os_strlen(ssid->passphrase);
1126 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1127 "on requested persistent group");
1128 os_memcpy(res->passphrase, ssid->passphrase, len);
1129 res->passphrase[len] = '\0';
1133 if (wpa_s->create_p2p_iface) {
1134 struct wpa_supplicant *group_wpa_s =
1135 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1136 if (group_wpa_s == NULL) {
1137 wpas_p2p_remove_pending_group_interface(wpa_s);
1140 if (group_wpa_s != wpa_s) {
1141 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1142 sizeof(group_wpa_s->p2p_pin));
1143 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1145 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1146 wpa_s->pending_interface_name[0] = '\0';
1147 group_wpa_s->p2p_in_provisioning = 1;
1150 wpas_start_wps_go(group_wpa_s, res, 1);
1152 wpas_start_wps_enrollee(group_wpa_s, res);
1154 wpa_s->p2p_in_provisioning = 1;
1155 wpa_s->global->p2p_group_formation = wpa_s;
1158 wpas_start_wps_go(wpa_s, res, 1);
1160 wpas_start_wps_enrollee(ctx, res);
1163 wpa_s->p2p_long_listen = 0;
1164 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1166 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1167 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1168 (res->peer_config_timeout % 100) * 10000,
1169 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1173 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1175 struct wpa_supplicant *wpa_s = ctx;
1176 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1177 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1179 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1183 void wpas_dev_found(void *ctx, const u8 *addr,
1184 const struct p2p_peer_info *info,
1187 #ifndef CONFIG_NO_STDOUT_DEBUG
1188 struct wpa_supplicant *wpa_s = ctx;
1189 char devtype[WPS_DEV_TYPE_BUFSIZE];
1191 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1192 " p2p_dev_addr=" MACSTR
1193 " pri_dev_type=%s name='%s' config_methods=0x%x "
1194 "dev_capab=0x%x group_capab=0x%x",
1195 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1196 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1198 info->device_name, info->config_methods,
1199 info->dev_capab, info->group_capab);
1200 #endif /* CONFIG_NO_STDOUT_DEBUG */
1202 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1206 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1208 struct wpa_supplicant *wpa_s = ctx;
1210 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1211 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1213 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1217 static int wpas_start_listen(void *ctx, unsigned int freq,
1218 unsigned int duration,
1219 const struct wpabuf *probe_resp_ie)
1221 struct wpa_supplicant *wpa_s = ctx;
1223 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1225 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1226 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1227 "report received Probe Request frames");
1231 wpa_s->pending_listen_freq = freq;
1232 wpa_s->pending_listen_duration = duration;
1234 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1235 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1236 "to remain on channel (%u MHz) for Listen "
1238 wpa_s->pending_listen_freq = 0;
1241 wpa_s->off_channel_freq = 0;
1242 wpa_s->roc_waiting_drv_freq = freq;
1248 static void wpas_stop_listen(void *ctx)
1250 struct wpa_supplicant *wpa_s = ctx;
1251 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1252 wpa_drv_cancel_remain_on_channel(wpa_s);
1253 wpa_s->off_channel_freq = 0;
1254 wpa_s->roc_waiting_drv_freq = 0;
1256 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1257 wpa_drv_probe_req_report(wpa_s, 0);
1261 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1263 struct wpa_supplicant *wpa_s = ctx;
1264 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1268 static struct p2p_srv_bonjour *
1269 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1270 const struct wpabuf *query)
1272 struct p2p_srv_bonjour *bsrv;
1275 len = wpabuf_len(query);
1276 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1277 struct p2p_srv_bonjour, list) {
1278 if (len == wpabuf_len(bsrv->query) &&
1279 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1287 static struct p2p_srv_upnp *
1288 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1289 const char *service)
1291 struct p2p_srv_upnp *usrv;
1293 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1294 struct p2p_srv_upnp, list) {
1295 if (version == usrv->version &&
1296 os_strcmp(service, usrv->service) == 0)
1303 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1308 if (wpabuf_tailroom(resp) < 5)
1311 /* Length (to be filled) */
1312 len_pos = wpabuf_put(resp, 2);
1313 wpabuf_put_u8(resp, srv_proto);
1314 wpabuf_put_u8(resp, srv_trans_id);
1316 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1317 /* Response Data: empty */
1318 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1322 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1323 struct wpabuf *resp, u8 srv_trans_id)
1325 struct p2p_srv_bonjour *bsrv;
1328 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1330 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1331 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1335 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1336 struct p2p_srv_bonjour, list) {
1337 if (wpabuf_tailroom(resp) <
1338 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1340 /* Length (to be filled) */
1341 len_pos = wpabuf_put(resp, 2);
1342 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1343 wpabuf_put_u8(resp, srv_trans_id);
1345 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1346 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1347 wpabuf_head(bsrv->resp),
1348 wpabuf_len(bsrv->resp));
1350 wpabuf_put_buf(resp, bsrv->query); /* Key */
1351 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1352 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1358 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1359 struct wpabuf *resp, u8 srv_trans_id,
1360 const u8 *query, size_t query_len)
1362 struct p2p_srv_bonjour *bsrv;
1366 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1368 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1369 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1370 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1375 if (query_len == 0) {
1376 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1380 if (wpabuf_tailroom(resp) < 5)
1382 /* Length (to be filled) */
1383 len_pos = wpabuf_put(resp, 2);
1384 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1385 wpabuf_put_u8(resp, srv_trans_id);
1387 wpabuf_set(&buf, query, query_len);
1388 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1390 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1394 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1395 /* Response Data: empty */
1396 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1402 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1403 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1404 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1406 if (wpabuf_tailroom(resp) >=
1407 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1409 wpabuf_put_buf(resp, bsrv->query); /* Key */
1410 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1412 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1416 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1417 struct wpabuf *resp, u8 srv_trans_id)
1419 struct p2p_srv_upnp *usrv;
1422 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1424 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1425 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1429 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1430 struct p2p_srv_upnp, list) {
1431 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1434 /* Length (to be filled) */
1435 len_pos = wpabuf_put(resp, 2);
1436 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1437 wpabuf_put_u8(resp, srv_trans_id);
1440 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1442 wpabuf_put_u8(resp, usrv->version);
1443 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1445 wpabuf_put_str(resp, usrv->service);
1446 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1452 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1453 struct wpabuf *resp, u8 srv_trans_id,
1454 const u8 *query, size_t query_len)
1456 struct p2p_srv_upnp *usrv;
1462 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1465 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1466 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1467 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1472 if (query_len == 0) {
1473 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1477 if (wpabuf_tailroom(resp) < 5)
1480 /* Length (to be filled) */
1481 len_pos = wpabuf_put(resp, 2);
1482 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1483 wpabuf_put_u8(resp, srv_trans_id);
1486 str = os_malloc(query_len);
1489 os_memcpy(str, query + 1, query_len - 1);
1490 str[query_len - 1] = '\0';
1492 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1493 struct p2p_srv_upnp, list) {
1494 if (version != usrv->version)
1497 if (os_strcmp(str, "ssdp:all") != 0 &&
1498 os_strstr(usrv->service, str) == NULL)
1501 if (wpabuf_tailroom(resp) < 2)
1505 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1507 wpabuf_put_u8(resp, version);
1509 wpabuf_put_u8(resp, ',');
1513 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1515 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1517 wpabuf_put_str(resp, usrv->service);
1522 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1525 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1526 /* Response Data: empty */
1529 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1533 #ifdef CONFIG_WIFI_DISPLAY
1534 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1535 struct wpabuf *resp, u8 srv_trans_id,
1536 const u8 *query, size_t query_len)
1542 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1544 if (!wpa_s->global->wifi_display) {
1545 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1546 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1551 if (query_len < 1) {
1552 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1557 if (wpabuf_tailroom(resp) < 5)
1562 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1564 /* TODO: role specific handling */
1566 /* Length (to be filled) */
1567 len_pos = wpabuf_put(resp, 2);
1568 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1569 wpabuf_put_u8(resp, srv_trans_id);
1570 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1572 while (pos < query + query_len) {
1573 if (*pos < MAX_WFD_SUBELEMS &&
1574 wpa_s->global->wfd_subelem[*pos] &&
1575 wpabuf_tailroom(resp) >=
1576 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1577 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1578 "subelement %u", *pos);
1579 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1584 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1586 #endif /* CONFIG_WIFI_DISPLAY */
1589 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1590 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1592 struct wpa_supplicant *wpa_s = ctx;
1593 const u8 *pos = tlvs;
1594 const u8 *end = tlvs + tlvs_len;
1597 struct wpabuf *resp;
1598 u8 srv_proto, srv_trans_id;
1602 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1604 buf_len = 2 * tlvs_len + 1;
1605 buf = os_malloc(buf_len);
1607 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1608 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1610 freq, MAC2STR(sa), dialog_token, update_indic,
1615 if (wpa_s->p2p_sd_over_ctrl_iface) {
1616 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1617 update_indic, tlvs, tlvs_len);
1618 return; /* to be processed by an external program */
1621 resp = wpabuf_alloc(10000);
1625 while (pos + 1 < end) {
1626 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1627 slen = WPA_GET_LE16(pos);
1629 if (pos + slen > end || slen < 2) {
1630 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1635 tlv_end = pos + slen;
1638 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1640 srv_trans_id = *pos++;
1641 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1644 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1645 pos, tlv_end - pos);
1648 if (wpa_s->force_long_sd) {
1649 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1651 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1652 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1656 switch (srv_proto) {
1657 case P2P_SERV_ALL_SERVICES:
1658 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1659 "for all services");
1660 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1661 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1662 wpa_printf(MSG_DEBUG, "P2P: No service "
1663 "discovery protocols available");
1664 wpas_sd_add_proto_not_avail(
1665 resp, P2P_SERV_ALL_SERVICES,
1669 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1670 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1672 case P2P_SERV_BONJOUR:
1673 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1674 pos, tlv_end - pos);
1677 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1678 pos, tlv_end - pos);
1680 #ifdef CONFIG_WIFI_DISPLAY
1681 case P2P_SERV_WIFI_DISPLAY:
1682 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1683 pos, tlv_end - pos);
1685 #endif /* CONFIG_WIFI_DISPLAY */
1687 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1688 "protocol %u", srv_proto);
1689 wpas_sd_add_proto_not_avail(resp, srv_proto,
1698 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1699 update_indic, tlvs, tlvs_len);
1701 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1707 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1708 const u8 *tlvs, size_t tlvs_len)
1710 struct wpa_supplicant *wpa_s = ctx;
1711 const u8 *pos = tlvs;
1712 const u8 *end = tlvs + tlvs_len;
1718 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1720 if (tlvs_len > 1500) {
1721 /* TODO: better way for handling this */
1722 wpa_msg_ctrl(wpa_s, MSG_INFO,
1723 P2P_EVENT_SERV_DISC_RESP MACSTR
1724 " %u <long response: %u bytes>",
1725 MAC2STR(sa), update_indic,
1726 (unsigned int) tlvs_len);
1728 buf_len = 2 * tlvs_len + 1;
1729 buf = os_malloc(buf_len);
1731 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1732 wpa_msg_ctrl(wpa_s, MSG_INFO,
1733 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1734 MAC2STR(sa), update_indic, buf);
1740 u8 srv_proto, srv_trans_id, status;
1742 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1743 slen = WPA_GET_LE16(pos);
1745 if (pos + slen > end || slen < 3) {
1746 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1750 tlv_end = pos + slen;
1753 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1755 srv_trans_id = *pos++;
1756 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1759 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1762 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1763 pos, tlv_end - pos);
1768 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1772 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1773 const struct wpabuf *tlvs)
1775 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1776 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1777 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1779 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1783 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1784 u8 version, const char *query)
1786 struct wpabuf *tlvs;
1789 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1792 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1793 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1794 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1795 wpabuf_put_u8(tlvs, version);
1796 wpabuf_put_str(tlvs, query);
1797 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1803 #ifdef CONFIG_WIFI_DISPLAY
1805 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1806 const struct wpabuf *tlvs)
1808 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1810 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1812 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1816 #define MAX_WFD_SD_SUBELEMS 20
1818 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1819 const char *subelems)
1826 len = wpabuf_put(tlvs, 2);
1827 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1828 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1830 wpabuf_put_u8(tlvs, role);
1835 if (val >= 0 && val < 256) {
1836 wpabuf_put_u8(tlvs, val);
1838 if (count == MAX_WFD_SD_SUBELEMS)
1841 pos = os_strchr(pos + 1, ',');
1847 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1851 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1852 const u8 *dst, const char *role)
1854 struct wpabuf *tlvs;
1856 const char *subelems;
1859 subelems = os_strchr(role, ' ');
1860 if (subelems == NULL)
1864 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1868 if (os_strstr(role, "[source]"))
1869 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1870 if (os_strstr(role, "[pri-sink]"))
1871 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1872 if (os_strstr(role, "[sec-sink]"))
1873 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1874 if (os_strstr(role, "[source+sink]"))
1875 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1877 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1882 #endif /* CONFIG_WIFI_DISPLAY */
1885 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
1887 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1888 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
1889 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1891 return p2p_sd_cancel_request(wpa_s->global->p2p,
1892 (void *) (uintptr_t) req);
1896 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1897 const u8 *dst, u8 dialog_token,
1898 const struct wpabuf *resp_tlvs)
1900 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1901 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1905 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1907 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1912 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1914 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1917 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1918 wpa_drv_p2p_service_update(wpa_s);
1921 if (wpa_s->global->p2p)
1923 p2p_sd_service_update(wpa_s->global->p2p, action);
1925 p2p_sd_service_update(wpa_s->global->p2p);
1930 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1932 dl_list_del(&bsrv->list);
1933 wpabuf_free(bsrv->query);
1934 wpabuf_free(bsrv->resp);
1939 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1941 dl_list_del(&usrv->list);
1942 os_free(usrv->service);
1947 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1949 struct p2p_srv_bonjour *bsrv, *bn;
1950 struct p2p_srv_upnp *usrv, *un;
1952 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1953 struct p2p_srv_bonjour, list)
1954 wpas_p2p_srv_bonjour_free(bsrv);
1956 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1957 struct p2p_srv_upnp, list)
1958 wpas_p2p_srv_upnp_free(usrv);
1961 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1963 wpas_p2p_sd_service_update(wpa_s);
1968 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1969 struct wpabuf *query, struct wpabuf *resp)
1971 struct p2p_srv_bonjour *bsrv;
1973 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1976 wpabuf_free(bsrv->resp);
1981 bsrv = os_zalloc(sizeof(*bsrv));
1984 bsrv->query = query;
1986 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1989 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
1991 wpas_p2p_sd_service_update(wpa_s);
1997 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1998 const struct wpabuf *query)
2000 struct p2p_srv_bonjour *bsrv;
2002 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2005 wpas_p2p_srv_bonjour_free(bsrv);
2007 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2009 wpas_p2p_sd_service_update(wpa_s);
2015 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2016 const char *service)
2018 struct p2p_srv_upnp *usrv;
2020 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2021 return 0; /* Already listed */
2022 usrv = os_zalloc(sizeof(*usrv));
2025 usrv->version = version;
2026 usrv->service = os_strdup(service);
2027 if (usrv->service == NULL) {
2031 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2034 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2036 wpas_p2p_sd_service_update(wpa_s);
2042 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2043 const char *service)
2045 struct p2p_srv_upnp *usrv;
2047 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2050 wpas_p2p_srv_upnp_free(usrv);
2052 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2054 wpas_p2p_sd_service_update(wpa_s);
2060 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2061 const u8 *peer, const char *params,
2062 unsigned int generated_pin)
2064 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2065 MAC2STR(peer), generated_pin, params);
2069 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2070 const u8 *peer, const char *params)
2072 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2073 MAC2STR(peer), params);
2077 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2078 const u8 *dev_addr, const u8 *pri_dev_type,
2079 const char *dev_name, u16 supp_config_methods,
2080 u8 dev_capab, u8 group_capab, const u8 *group_id,
2081 size_t group_id_len)
2083 struct wpa_supplicant *wpa_s = ctx;
2084 char devtype[WPS_DEV_TYPE_BUFSIZE];
2086 u8 empty_dev_type[8];
2087 unsigned int generated_pin = 0;
2088 struct wpa_supplicant *group = NULL;
2091 for (group = wpa_s->global->ifaces; group; group = group->next)
2093 struct wpa_ssid *s = group->current_ssid;
2095 s->mode == WPAS_MODE_P2P_GO &&
2096 group_id_len - ETH_ALEN == s->ssid_len &&
2097 os_memcmp(group_id + ETH_ALEN, s->ssid,
2103 if (pri_dev_type == NULL) {
2104 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2105 pri_dev_type = empty_dev_type;
2107 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2108 " pri_dev_type=%s name='%s' config_methods=0x%x "
2109 "dev_capab=0x%x group_capab=0x%x%s%s",
2111 wps_dev_type_bin2str(pri_dev_type, devtype,
2113 dev_name, supp_config_methods, dev_capab, group_capab,
2114 group ? " group=" : "",
2115 group ? group->ifname : "");
2116 params[sizeof(params) - 1] = '\0';
2118 if (config_methods & WPS_CONFIG_DISPLAY) {
2119 generated_pin = wps_generate_pin();
2120 wpas_prov_disc_local_display(wpa_s, peer, params,
2122 } else if (config_methods & WPS_CONFIG_KEYPAD)
2123 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2124 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2125 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2126 "%s", MAC2STR(peer), params);
2128 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2129 P2P_PROV_DISC_SUCCESS,
2130 config_methods, generated_pin);
2134 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2136 struct wpa_supplicant *wpa_s = ctx;
2137 unsigned int generated_pin = 0;
2140 if (wpa_s->pending_pd_before_join &&
2141 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2142 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2143 wpa_s->pending_pd_before_join = 0;
2144 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2145 "join-existing-group operation");
2146 wpas_p2p_join_start(wpa_s);
2150 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2151 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2152 os_snprintf(params, sizeof(params), " peer_go=%d",
2153 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2157 if (config_methods & WPS_CONFIG_DISPLAY)
2158 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2159 else if (config_methods & WPS_CONFIG_KEYPAD) {
2160 generated_pin = wps_generate_pin();
2161 wpas_prov_disc_local_display(wpa_s, peer, params,
2163 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2164 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2165 "%s", MAC2STR(peer), params);
2167 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2168 P2P_PROV_DISC_SUCCESS,
2169 config_methods, generated_pin);
2173 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2174 enum p2p_prov_disc_status status)
2176 struct wpa_supplicant *wpa_s = ctx;
2178 if (wpa_s->p2p_fallback_to_go_neg) {
2179 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2180 "failed - fall back to GO Negotiation");
2181 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2185 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2186 " p2p_dev_addr=" MACSTR " status=%d",
2187 MAC2STR(peer), status);
2189 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2194 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2195 const u8 *go_dev_addr, const u8 *ssid,
2196 size_t ssid_len, int *go, u8 *group_bssid,
2197 int *force_freq, int persistent_group)
2199 struct wpa_supplicant *wpa_s = ctx;
2201 u8 cur_bssid[ETH_ALEN];
2203 struct wpa_supplicant *grp;
2205 if (!persistent_group) {
2206 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2207 " to join an active group", MAC2STR(sa));
2208 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2209 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2211 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2212 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2213 "authorized invitation");
2217 * Do not accept the invitation automatically; notify user and
2220 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2223 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2225 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2226 "running persistent group");
2228 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2232 if (!wpa_s->conf->persistent_reconnect)
2233 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2235 for (s = wpa_s->conf->ssid; s; s = s->next) {
2236 if (s->disabled == 2 &&
2237 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2238 s->ssid_len == ssid_len &&
2239 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2244 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2245 " requested reinvocation of an unknown group",
2247 return P2P_SC_FAIL_UNKNOWN_GROUP;
2250 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2252 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2253 wpa_printf(MSG_DEBUG, "P2P: The only available "
2254 "interface is already in use - reject "
2256 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2258 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2259 } else if (s->mode == WPAS_MODE_P2P_GO) {
2261 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2263 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2264 "interface address for the group");
2265 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2267 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2272 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2273 wpa_s->assoc_freq) {
2274 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2275 "the channel we are already using");
2276 *force_freq = wpa_s->assoc_freq;
2279 res = wpa_drv_shared_freq(wpa_s);
2281 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2282 "with the channel we are already using on a "
2283 "shared interface");
2287 return P2P_SC_SUCCESS;
2291 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2292 const u8 *ssid, size_t ssid_len,
2293 const u8 *go_dev_addr, u8 status,
2296 struct wpa_supplicant *wpa_s = ctx;
2299 for (s = wpa_s->conf->ssid; s; s = s->next) {
2300 if (s->disabled == 2 &&
2301 s->ssid_len == ssid_len &&
2302 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2306 if (status == P2P_SC_SUCCESS) {
2307 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2308 " was accepted; op_freq=%d MHz",
2309 MAC2STR(sa), op_freq);
2311 wpas_p2p_group_add_persistent(
2312 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
2314 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2315 wpa_s->p2p_wps_method, 0);
2320 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2321 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2322 " was rejected (status %u)", MAC2STR(sa), status);
2328 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2329 "sa=" MACSTR " go_dev_addr=" MACSTR
2330 " bssid=" MACSTR " unknown-network",
2331 MAC2STR(sa), MAC2STR(go_dev_addr),
2334 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2335 "sa=" MACSTR " go_dev_addr=" MACSTR
2337 MAC2STR(sa), MAC2STR(go_dev_addr));
2342 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2343 " persistent=%d", MAC2STR(sa), s->id);
2347 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2349 struct wpa_supplicant *wpa_s = ctx;
2350 struct wpa_ssid *ssid;
2353 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2354 "status=%d " MACSTR,
2355 status, MAC2STR(bssid));
2357 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2358 "status=%d ", status);
2360 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2362 if (wpa_s->pending_invite_ssid_id == -1)
2363 return; /* Invitation to active group */
2365 if (status != P2P_SC_SUCCESS) {
2366 wpas_p2p_remove_pending_group_interface(wpa_s);
2370 ssid = wpa_config_get_network(wpa_s->conf,
2371 wpa_s->pending_invite_ssid_id);
2373 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2374 "data matching with invitation");
2378 wpas_p2p_group_add_persistent(wpa_s, ssid,
2379 ssid->mode == WPAS_MODE_P2P_GO,
2380 wpa_s->p2p_persistent_go_freq,
2381 wpa_s->p2p_go_ht40);
2385 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2390 if (global->p2p_disallow_freq == NULL)
2393 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2394 if (freq >= global->p2p_disallow_freq[i].min &&
2395 freq <= global->p2p_disallow_freq[i].max)
2403 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2405 reg->channel[reg->channels] = chan;
2410 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2411 struct p2p_channels *chan)
2415 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2418 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2419 chan->reg_class[cla].reg_class = 81;
2420 chan->reg_class[cla].channels = 0;
2421 for (i = 0; i < 11; i++) {
2422 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2423 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2425 if (chan->reg_class[cla].channels)
2428 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2431 /* Operating class 115 - 5 GHz, channels 36-48 */
2432 chan->reg_class[cla].reg_class = 115;
2433 chan->reg_class[cla].channels = 0;
2434 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2435 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2436 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2437 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2438 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2439 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2440 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2441 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2442 if (chan->reg_class[cla].channels)
2445 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2448 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2449 chan->reg_class[cla].reg_class = 124;
2450 chan->reg_class[cla].channels = 0;
2451 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2452 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2453 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2454 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2455 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2456 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2457 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2458 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2459 if (chan->reg_class[cla].channels)
2462 chan->reg_classes = cla;
2467 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2469 enum hostapd_hw_mode mode)
2473 for (i = 0; i < num_modes; i++) {
2474 if (modes[i].mode == mode)
2482 static int has_channel(struct wpa_global *global,
2483 struct hostapd_hw_modes *mode, u8 chan, int *flags)
2488 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2490 if (wpas_p2p_disallowed_freq(global, freq))
2493 for (i = 0; i < mode->num_channels; i++) {
2494 if (mode->channels[i].chan == chan) {
2496 *flags = mode->channels[i].flag;
2497 return !(mode->channels[i].flag &
2498 (HOSTAPD_CHAN_DISABLED |
2499 HOSTAPD_CHAN_PASSIVE_SCAN |
2500 HOSTAPD_CHAN_NO_IBSS |
2501 HOSTAPD_CHAN_RADAR));
2509 struct p2p_oper_class_map {
2510 enum hostapd_hw_mode mode;
2515 enum { BW20, BW40PLUS, BW40MINUS } bw;
2518 static struct p2p_oper_class_map op_class[] = {
2519 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2520 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2521 #if 0 /* Do not enable HT40 on 2 GHz for now */
2522 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2523 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2525 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2526 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2527 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2528 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2529 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2530 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2531 { -1, 0, 0, 0, 0, BW20 }
2535 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2536 struct hostapd_hw_modes *mode,
2541 if (!has_channel(wpa_s->global, mode, channel, &flag))
2543 if (bw == BW40MINUS &&
2544 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2545 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2547 if (bw == BW40PLUS &&
2548 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2549 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2555 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2556 struct p2p_channels *chan)
2558 struct hostapd_hw_modes *mode;
2561 if (wpa_s->hw.modes == NULL) {
2562 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2563 "of all supported channels; assume dualband "
2565 return wpas_p2p_default_channels(wpa_s, chan);
2570 for (op = 0; op_class[op].op_class; op++) {
2571 struct p2p_oper_class_map *o = &op_class[op];
2573 struct p2p_reg_class *reg = NULL;
2575 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2578 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2579 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2582 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2583 "class %u", o->op_class);
2584 reg = &chan->reg_class[cla];
2586 reg->reg_class = o->op_class;
2588 reg->channel[reg->channels] = ch;
2592 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2593 reg->channel, reg->channels);
2597 chan->reg_classes = cla;
2603 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2604 struct hostapd_hw_modes *mode, u8 channel)
2608 for (op = 0; op_class[op].op_class; op++) {
2609 struct p2p_oper_class_map *o = &op_class[op];
2612 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2613 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2614 o->bw == BW20 || ch != channel)
2616 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2620 return (o->bw == BW40MINUS) ? -1 : 1;
2629 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2632 struct wpa_supplicant *wpa_s = ctx;
2634 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2635 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2641 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2645 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2647 struct wpa_supplicant *wpa_s = ctx;
2649 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2650 struct wpa_ssid *ssid = wpa_s->current_ssid;
2653 if (ssid->mode != WPAS_MODE_INFRA)
2655 if (wpa_s->wpa_state != WPA_COMPLETED &&
2656 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2658 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2667 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2668 * @global: Pointer to global data from wpa_supplicant_init()
2669 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2670 * Returns: 0 on success, -1 on failure
2672 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2674 struct p2p_config p2p;
2678 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2684 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2685 struct p2p_params params;
2687 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2688 os_memset(¶ms, 0, sizeof(params));
2689 params.dev_name = wpa_s->conf->device_name;
2690 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2692 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2693 os_memcpy(params.sec_dev_type,
2694 wpa_s->conf->sec_device_type,
2695 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2697 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2703 os_memset(&p2p, 0, sizeof(p2p));
2704 p2p.msg_ctx = wpa_s;
2706 p2p.p2p_scan = wpas_p2p_scan;
2707 p2p.send_action = wpas_send_action;
2708 p2p.send_action_done = wpas_send_action_done;
2709 p2p.go_neg_completed = wpas_go_neg_completed;
2710 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2711 p2p.dev_found = wpas_dev_found;
2712 p2p.dev_lost = wpas_dev_lost;
2713 p2p.start_listen = wpas_start_listen;
2714 p2p.stop_listen = wpas_stop_listen;
2715 p2p.send_probe_resp = wpas_send_probe_resp;
2716 p2p.sd_request = wpas_sd_request;
2717 p2p.sd_response = wpas_sd_response;
2718 p2p.prov_disc_req = wpas_prov_disc_req;
2719 p2p.prov_disc_resp = wpas_prov_disc_resp;
2720 p2p.prov_disc_fail = wpas_prov_disc_fail;
2721 p2p.invitation_process = wpas_invitation_process;
2722 p2p.invitation_received = wpas_invitation_received;
2723 p2p.invitation_result = wpas_invitation_result;
2724 p2p.get_noa = wpas_get_noa;
2725 p2p.go_connected = wpas_go_connected;
2727 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2728 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2729 p2p.dev_name = wpa_s->conf->device_name;
2730 p2p.manufacturer = wpa_s->conf->manufacturer;
2731 p2p.model_name = wpa_s->conf->model_name;
2732 p2p.model_number = wpa_s->conf->model_number;
2733 p2p.serial_number = wpa_s->conf->serial_number;
2735 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2736 p2p.config_methods = wpa_s->wps->config_methods;
2739 if (wpa_s->conf->p2p_listen_reg_class &&
2740 wpa_s->conf->p2p_listen_channel) {
2741 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2742 p2p.channel = wpa_s->conf->p2p_listen_channel;
2746 * Pick one of the social channels randomly as the listen
2749 os_get_random((u8 *) &r, sizeof(r));
2750 p2p.channel = 1 + (r % 3) * 5;
2752 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2754 if (wpa_s->conf->p2p_oper_reg_class &&
2755 wpa_s->conf->p2p_oper_channel) {
2756 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2757 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2758 p2p.cfg_op_channel = 1;
2759 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2760 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2763 p2p.op_reg_class = 81;
2765 * Use random operation channel from (1, 6, 11) if no other
2766 * preference is indicated.
2768 os_get_random((u8 *) &r, sizeof(r));
2769 p2p.op_channel = 1 + (r % 3) * 5;
2770 p2p.cfg_op_channel = 0;
2771 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2772 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2774 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2775 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2776 p2p.country[2] = 0x04;
2778 os_memcpy(p2p.country, "XX\x04", 3);
2780 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2781 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2786 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2789 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2790 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2791 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2793 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2794 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2796 p2p.max_peers = 100;
2798 if (wpa_s->conf->p2p_ssid_postfix) {
2799 p2p.ssid_postfix_len =
2800 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2801 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2802 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2803 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2804 p2p.ssid_postfix_len);
2807 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2810 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2811 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
2812 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)
2813 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
2816 global->p2p = p2p_init(&p2p);
2817 if (global->p2p == NULL)
2819 global->p2p_init_wpa_s = wpa_s;
2821 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2822 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2824 p2p_add_wps_vendor_extension(
2825 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2833 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2834 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2836 * This function deinitialize per-interface P2P data.
2838 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2840 if (wpa_s->driver && wpa_s->drv_priv)
2841 wpa_drv_probe_req_report(wpa_s, 0);
2843 if (wpa_s->go_params) {
2844 /* Clear any stored provisioning info */
2845 p2p_clear_provisioning_info(
2847 wpa_s->go_params->peer_device_addr);
2850 os_free(wpa_s->go_params);
2851 wpa_s->go_params = NULL;
2852 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2853 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2854 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2855 wpa_s->p2p_long_listen = 0;
2856 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2857 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2858 wpas_p2p_remove_pending_group_interface(wpa_s);
2860 /* TODO: remove group interface from the driver if this wpa_s instance
2861 * is on top of a P2P group interface */
2866 * wpas_p2p_deinit_global - Deinitialize global P2P module
2867 * @global: Pointer to global data from wpa_supplicant_init()
2869 * This function deinitializes the global (per device) P2P module.
2871 void wpas_p2p_deinit_global(struct wpa_global *global)
2873 struct wpa_supplicant *wpa_s, *tmp;
2875 wpa_s = global->ifaces;
2877 wpas_p2p_service_flush(wpa_s);
2879 if (global->p2p == NULL)
2882 /* Remove remaining P2P group interfaces */
2883 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2884 wpa_s = wpa_s->next;
2886 tmp = global->ifaces;
2889 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2894 /* Disconnect from the P2P group and deinit the interface */
2895 wpas_p2p_disconnect(tmp);
2899 * Deinit GO data on any possibly remaining interface (if main
2900 * interface is used as GO).
2902 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2903 if (wpa_s->ap_iface)
2904 wpas_p2p_group_deinit(wpa_s);
2907 p2p_deinit(global->p2p);
2909 global->p2p_init_wpa_s = NULL;
2913 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2915 if (wpa_s->drv_flags &
2916 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2917 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2918 return 1; /* P2P group requires a new interface in every case
2920 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2921 return 0; /* driver does not support concurrent operations */
2922 if (wpa_s->global->ifaces->next)
2923 return 1; /* more that one interface already in use */
2924 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2925 return 1; /* this interface is already in use */
2930 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2931 const u8 *peer_addr,
2932 enum p2p_wps_method wps_method,
2933 int go_intent, const u8 *own_interface_addr,
2934 unsigned int force_freq, int persistent_group,
2935 struct wpa_ssid *ssid)
2937 if (persistent_group && wpa_s->conf->persistent_reconnect)
2938 persistent_group = 2;
2940 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2941 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2942 go_intent, own_interface_addr,
2943 force_freq, persistent_group);
2947 * Increase GO config timeout if HT40 is used since it takes some time
2948 * to scan channels for coex purposes before the BSS can be started.
2950 p2p_set_config_timeout(wpa_s->global->p2p,
2951 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2953 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2954 go_intent, own_interface_addr, force_freq,
2955 persistent_group, ssid ? ssid->ssid : NULL,
2956 ssid ? ssid->ssid_len : 0,
2957 wpa_s->p2p_pd_before_go_neg);
2961 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2962 const u8 *peer_addr,
2963 enum p2p_wps_method wps_method,
2964 int go_intent, const u8 *own_interface_addr,
2965 unsigned int force_freq, int persistent_group,
2966 struct wpa_ssid *ssid)
2968 if (persistent_group && wpa_s->conf->persistent_reconnect)
2969 persistent_group = 2;
2971 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2974 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2975 go_intent, own_interface_addr, force_freq,
2976 persistent_group, ssid ? ssid->ssid : NULL,
2977 ssid ? ssid->ssid_len : 0);
2981 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2983 wpa_s->p2p_join_scan_count++;
2984 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2985 wpa_s->p2p_join_scan_count);
2986 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2987 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2988 " for join operationg - stop join attempt",
2989 MAC2STR(wpa_s->pending_join_iface_addr));
2990 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2991 if (wpa_s->p2p_auto_pd) {
2992 wpa_s->p2p_auto_pd = 0;
2993 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2994 " p2p_dev_addr=" MACSTR " status=N/A",
2995 MAC2STR(wpa_s->pending_join_dev_addr));
2998 wpa_msg(wpa_s->parent, MSG_INFO,
2999 P2P_EVENT_GROUP_FORMATION_FAILURE);
3004 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
3006 struct wpa_supplicant *wpa_s = eloop_ctx;
3007 if (!wpa_s->pending_pd_before_join)
3010 * Provision Discovery Response may have been lost - try to connect
3011 * anyway since we do not need any information from this PD.
3013 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
3014 "try to connect anyway");
3015 wpas_p2p_join_start(wpa_s);
3019 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3021 struct wpa_supplicant *iface;
3025 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3028 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3029 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3031 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3033 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3034 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3035 shared_freq = iface->current_ssid->frequency;
3036 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3037 shared_freq = iface->assoc_freq;
3041 if (shared_freq && freq != shared_freq) {
3042 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3043 "connected on %d MHz - new connection on "
3044 "%d MHz", iface->ifname, shared_freq, freq);
3049 shared_freq = wpa_drv_shared_freq(wpa_s);
3050 if (shared_freq > 0 && shared_freq != freq) {
3051 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3052 "virtual interface connected on %d MHz - new "
3053 "connection on %d MHz", shared_freq, freq);
3061 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3062 const u8 *peer_dev_addr)
3064 struct wpa_bss *bss;
3067 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3070 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3071 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3076 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3077 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3078 "%ld.%06ld (%supdated in last scan)",
3079 bss->last_update.sec, bss->last_update.usec,
3080 updated ? "": "not ");
3086 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3087 struct wpa_scan_results *scan_res)
3089 struct wpa_bss *bss;
3091 u8 iface_addr[ETH_ALEN];
3093 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3095 if (wpa_s->global->p2p_disabled)
3098 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3099 scan_res ? (int) scan_res->num : -1,
3100 wpa_s->p2p_auto_join ? "auto_" : "");
3103 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3105 if (wpa_s->p2p_auto_pd) {
3106 int join = wpas_p2p_peer_go(wpa_s,
3107 wpa_s->pending_join_dev_addr);
3109 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3110 wpa_s->auto_pd_scan_retry++;
3111 bss = wpa_bss_get_bssid(wpa_s,
3112 wpa_s->pending_join_dev_addr);
3115 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3116 "the peer " MACSTR " at %d MHz",
3117 wpa_s->auto_pd_scan_retry,
3119 pending_join_dev_addr),
3121 wpas_p2p_join_scan_req(wpa_s, freq);
3129 wpa_s->p2p_auto_pd = 0;
3130 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3131 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3132 MAC2STR(wpa_s->pending_join_dev_addr), join);
3133 if (p2p_prov_disc_req(wpa_s->global->p2p,
3134 wpa_s->pending_join_dev_addr,
3135 wpa_s->pending_pd_config_methods, join,
3137 wpa_s->p2p_auto_pd = 0;
3138 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3139 " p2p_dev_addr=" MACSTR " status=N/A",
3140 MAC2STR(wpa_s->pending_join_dev_addr));
3145 if (wpa_s->p2p_auto_join) {
3146 int join = wpas_p2p_peer_go(wpa_s,
3147 wpa_s->pending_join_dev_addr);
3149 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3150 "running a GO -> use GO Negotiation");
3151 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3152 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3153 wpa_s->p2p_persistent_group, 0, 0, 0,
3154 wpa_s->p2p_go_intent,
3155 wpa_s->p2p_connect_freq,
3156 wpa_s->p2p_persistent_id,
3157 wpa_s->p2p_pd_before_go_neg,
3158 wpa_s->p2p_go_ht40);
3162 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3163 "try to join the group", join ? "" :
3166 wpa_s->p2p_fallback_to_go_neg = 1;
3169 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3170 wpa_s->pending_join_iface_addr);
3172 p2p_get_interface_addr(wpa_s->global->p2p,
3173 wpa_s->pending_join_dev_addr,
3175 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3177 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3178 "address for join from " MACSTR " to " MACSTR
3179 " based on newly discovered P2P peer entry",
3180 MAC2STR(wpa_s->pending_join_iface_addr),
3181 MAC2STR(iface_addr));
3182 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3185 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3186 wpa_s->pending_join_iface_addr);
3189 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3190 "from P2P peer table: %d MHz", freq);
3192 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3195 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3196 "from BSS table: %d MHz", freq);
3201 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3202 wpa_msg(wpa_s->parent, MSG_INFO,
3203 P2P_EVENT_GROUP_FORMATION_FAILURE
3204 "reason=FREQ_CONFLICT");
3208 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3209 "prior to joining an existing group (GO " MACSTR
3211 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3212 wpa_s->pending_pd_before_join = 1;
3214 switch (wpa_s->pending_join_wps_method) {
3215 case WPS_PIN_DISPLAY:
3216 method = WPS_CONFIG_KEYPAD;
3218 case WPS_PIN_KEYPAD:
3219 method = WPS_CONFIG_DISPLAY;
3222 method = WPS_CONFIG_PUSHBUTTON;
3229 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3230 wpa_s->pending_join_dev_addr) ==
3233 * We have already performed provision discovery for
3234 * joining the group. Proceed directly to join
3235 * operation without duplicated provision discovery. */
3236 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3237 "with " MACSTR " already done - proceed to "
3239 MAC2STR(wpa_s->pending_join_dev_addr));
3240 wpa_s->pending_pd_before_join = 0;
3244 if (p2p_prov_disc_req(wpa_s->global->p2p,
3245 wpa_s->pending_join_dev_addr, method, 1,
3247 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3248 "Discovery Request before joining an "
3250 wpa_s->pending_pd_before_join = 0;
3255 * Actual join operation will be started from the Action frame
3256 * TX status callback (if no ACK is received) or when the
3257 * Provision Discovery Response is received. Use a short
3258 * timeout as a backup mechanism should the Provision Discovery
3259 * Response be lost for any reason.
3261 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3263 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3268 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3269 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3270 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3271 wpas_p2p_check_join_scan_limit(wpa_s);
3275 /* Start join operation immediately */
3276 wpas_p2p_join_start(wpa_s);
3280 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3283 struct wpa_driver_scan_params params;
3284 struct wpabuf *wps_ie, *ies;
3286 int freqs[2] = { 0, 0 };
3290 /* If freq is not provided, check the operating freq of the GO and do a
3291 * a directed scan to save time
3294 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3295 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3298 os_memset(¶ms, 0, sizeof(params));
3300 /* P2P Wildcard SSID */
3301 params.num_ssids = 1;
3302 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3303 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3305 wpa_s->wps->dev.p2p = 1;
3306 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3307 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3309 if (wps_ie == NULL) {
3310 wpas_p2p_scan_res_join(wpa_s, NULL);
3314 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3315 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3317 wpabuf_free(wps_ie);
3318 wpas_p2p_scan_res_join(wpa_s, NULL);
3321 wpabuf_put_buf(ies, wps_ie);
3322 wpabuf_free(wps_ie);
3324 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3326 params.p2p_probe = 1;
3327 params.extra_ies = wpabuf_head(ies);
3328 params.extra_ies_len = wpabuf_len(ies);
3331 params.freqs = freqs;
3335 * Run a scan to update BSS table and start Provision Discovery once
3336 * the new scan results become available.
3338 ret = wpa_drv_scan(wpa_s, ¶ms);
3340 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3345 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3347 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3348 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3349 wpas_p2p_check_join_scan_limit(wpa_s);
3354 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3356 struct wpa_supplicant *wpa_s = eloop_ctx;
3357 wpas_p2p_join_scan_req(wpa_s, 0);
3361 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3362 const u8 *dev_addr, enum p2p_wps_method wps_method,
3365 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3366 MACSTR " dev " MACSTR ")%s",
3367 MAC2STR(iface_addr), MAC2STR(dev_addr),
3368 auto_join ? " (auto_join)" : "");
3370 wpa_s->p2p_auto_pd = 0;
3371 wpa_s->p2p_auto_join = !!auto_join;
3372 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3373 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3374 wpa_s->pending_join_wps_method = wps_method;
3376 /* Make sure we are not running find during connection establishment */
3377 wpas_p2p_stop_find(wpa_s);
3379 wpa_s->p2p_join_scan_count = 0;
3380 wpas_p2p_join_scan(wpa_s, NULL);
3385 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3387 struct wpa_supplicant *group;
3388 struct p2p_go_neg_results res;
3389 struct wpa_bss *bss;
3391 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
3392 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3395 if (group != wpa_s) {
3396 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3397 sizeof(group->p2p_pin));
3398 group->p2p_wps_method = wpa_s->p2p_wps_method;
3401 group->p2p_in_provisioning = 1;
3402 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3404 os_memset(&res, 0, sizeof(res));
3405 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3407 res.wps_method = wpa_s->pending_join_wps_method;
3408 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3410 res.freq = bss->freq;
3411 res.ssid_len = bss->ssid_len;
3412 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3415 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3416 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3418 wpa_drv_cancel_remain_on_channel(wpa_s);
3419 wpa_s->off_channel_freq = 0;
3420 wpa_s->roc_waiting_drv_freq = 0;
3422 wpas_start_wps_enrollee(group, &res);
3425 * Allow a longer timeout for join-a-running-group than normal 15
3426 * second group formation timeout since the GO may not have authorized
3427 * our connection yet.
3429 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3430 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3438 * wpas_p2p_connect - Request P2P Group Formation to be started
3439 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3440 * @peer_addr: Address of the peer P2P Device
3441 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3442 * @persistent_group: Whether to create a persistent group
3443 * @auto_join: Whether to select join vs. GO Negotiation automatically
3444 * @join: Whether to join an existing group (as a client) instead of starting
3445 * Group Owner negotiation; @peer_addr is BSSID in that case
3446 * @auth: Whether to only authorize the connection instead of doing that and
3447 * initiating Group Owner negotiation
3448 * @go_intent: GO Intent or -1 to use default
3449 * @freq: Frequency for the group or 0 for auto-selection
3450 * @persistent_id: Persistent group credentials to use for forcing GO
3451 * parameters or -1 to generate new values (SSID/passphrase)
3452 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3453 * interoperability workaround when initiating group formation
3454 * @ht40: Start GO with 40 MHz channel width
3455 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3456 * failure, -2 on failure due to channel not currently available,
3457 * -3 if forced channel is not supported
3459 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3460 const char *pin, enum p2p_wps_method wps_method,
3461 int persistent_group, int auto_join, int join, int auth,
3462 int go_intent, int freq, int persistent_id, int pd,
3465 int force_freq = 0, oper_freq = 0;
3468 enum wpa_driver_if_type iftype;
3470 struct wpa_ssid *ssid = NULL;
3472 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3475 if (persistent_id >= 0) {
3476 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3477 if (ssid == NULL || ssid->disabled != 2 ||
3478 ssid->mode != WPAS_MODE_P2P_GO)
3483 go_intent = wpa_s->conf->p2p_go_intent;
3486 wpa_s->p2p_long_listen = 0;
3488 wpa_s->p2p_wps_method = wps_method;
3489 wpa_s->p2p_persistent_group = !!persistent_group;
3490 wpa_s->p2p_persistent_id = persistent_id;
3491 wpa_s->p2p_go_intent = go_intent;
3492 wpa_s->p2p_connect_freq = freq;
3493 wpa_s->p2p_fallback_to_go_neg = 0;
3494 wpa_s->p2p_pd_before_go_neg = !!pd;
3495 wpa_s->p2p_go_ht40 = !!ht40;
3498 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3499 else if (wps_method == WPS_PIN_DISPLAY) {
3500 ret = wps_generate_pin();
3501 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3503 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3506 wpa_s->p2p_pin[0] = '\0';
3508 if (join || auto_join) {
3509 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3511 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3512 "connect a running group from " MACSTR,
3513 MAC2STR(peer_addr));
3514 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3517 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3518 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3520 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3521 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3525 os_get_time(&wpa_s->p2p_auto_started);
3526 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3528 wpa_s->p2p_auto_started.sec,
3529 wpa_s->p2p_auto_started.usec);
3531 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3537 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3539 oper_freq = wpa_s->assoc_freq;
3541 oper_freq = wpa_drv_shared_freq(wpa_s);
3547 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3548 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3549 "(%u MHz) is not supported for P2P uses",
3554 if (oper_freq > 0 && freq != oper_freq &&
3555 !(wpa_s->drv_flags &
3556 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3557 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3558 "on %u MHz while connected on another "
3559 "channel (%u MHz)", freq, oper_freq);
3562 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3563 "requested channel (%u MHz)", freq);
3565 } else if (oper_freq > 0 &&
3566 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3567 if (!(wpa_s->drv_flags &
3568 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3569 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3570 "while connected on non-P2P supported "
3571 "channel (%u MHz)", oper_freq);
3574 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3575 "(%u MHz) not available for P2P - try to use "
3576 "another channel", oper_freq);
3578 } else if (oper_freq > 0) {
3579 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3580 "channel we are already using (%u MHz) on another "
3581 "interface", oper_freq);
3582 force_freq = oper_freq;
3585 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3587 if (wpa_s->create_p2p_iface) {
3588 /* Prepare to add a new interface for the group */
3589 iftype = WPA_IF_P2P_GROUP;
3590 if (go_intent == 15)
3591 iftype = WPA_IF_P2P_GO;
3592 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3593 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3594 "interface for the group");
3598 if_addr = wpa_s->pending_interface_addr;
3600 if_addr = wpa_s->own_addr;
3603 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3605 force_freq, persistent_group, ssid) <
3611 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3612 go_intent, if_addr, force_freq,
3613 persistent_group, ssid) < 0) {
3614 if (wpa_s->create_p2p_iface)
3615 wpas_p2p_remove_pending_group_interface(wpa_s);
3623 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3624 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3625 * @freq: Frequency of the channel in MHz
3626 * @duration: Duration of the stay on the channel in milliseconds
3628 * This callback is called when the driver indicates that it has started the
3629 * requested remain-on-channel duration.
3631 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3632 unsigned int freq, unsigned int duration)
3634 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3636 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3637 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3638 wpa_s->pending_listen_duration);
3639 wpa_s->pending_listen_freq = 0;
3644 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3645 unsigned int timeout)
3647 /* Limit maximum Listen state time based on driver limitation. */
3648 if (timeout > wpa_s->max_remain_on_chan)
3649 timeout = wpa_s->max_remain_on_chan;
3651 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3652 return wpa_drv_p2p_listen(wpa_s, timeout);
3654 return p2p_listen(wpa_s->global->p2p, timeout);
3659 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3660 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3661 * @freq: Frequency of the channel in MHz
3663 * This callback is called when the driver indicates that a remain-on-channel
3664 * operation has been completed, i.e., the duration on the requested channel
3667 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3670 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3671 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3672 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3673 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3675 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3676 return; /* P2P module started a new operation */
3677 if (wpa_s->pending_action_tx)
3679 if (wpa_s->p2p_long_listen > 0)
3680 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3681 if (wpa_s->p2p_long_listen > 0) {
3682 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3683 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3689 * wpas_p2p_group_remove - Remove a P2P group
3690 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3691 * @ifname: Network interface name of the group interface or "*" to remove all
3693 * Returns: 0 on success, -1 on failure
3695 * This function is used to remove a P2P group. This can be used to disconnect
3696 * from a group in which the local end is a P2P Client or to end a P2P Group in
3697 * case the local end is the Group Owner. If a virtual network interface was
3698 * created for this group, that interface will be removed. Otherwise, only the
3699 * configured P2P group network will be removed from the interface.
3701 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3703 struct wpa_global *global = wpa_s->global;
3705 if (os_strcmp(ifname, "*") == 0) {
3706 struct wpa_supplicant *prev;
3707 wpa_s = global->ifaces;
3710 wpa_s = wpa_s->next;
3711 wpas_p2p_disconnect(prev);
3716 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3717 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3721 return wpas_p2p_disconnect(wpa_s);
3725 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3726 struct p2p_go_neg_results *params,
3732 os_memset(params, 0, sizeof(*params));
3733 params->role_go = 1;
3734 params->ht40 = ht40;
3736 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3737 "frequency %d MHz", freq);
3738 params->freq = freq;
3739 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3740 wpa_s->conf->p2p_oper_channel >= 1 &&
3741 wpa_s->conf->p2p_oper_channel <= 11) {
3742 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3743 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3744 "frequency %d MHz", params->freq);
3745 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3746 wpa_s->conf->p2p_oper_reg_class == 124) {
3747 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3748 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3749 "frequency %d MHz", params->freq);
3750 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3751 wpa_s->best_overall_freq > 0 &&
3752 p2p_supported_freq(wpa_s->global->p2p,
3753 wpa_s->best_overall_freq)) {
3754 params->freq = wpa_s->best_overall_freq;
3755 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3756 "channel %d MHz", params->freq);
3757 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3758 wpa_s->best_24_freq > 0 &&
3759 p2p_supported_freq(wpa_s->global->p2p,
3760 wpa_s->best_24_freq)) {
3761 params->freq = wpa_s->best_24_freq;
3762 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3763 "channel %d MHz", params->freq);
3764 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3765 wpa_s->best_5_freq > 0 &&
3766 p2p_supported_freq(wpa_s->global->p2p,
3767 wpa_s->best_5_freq)) {
3768 params->freq = wpa_s->best_5_freq;
3769 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3770 "channel %d MHz", params->freq);
3773 for (chan = 0; chan < 11; chan++) {
3774 params->freq = 2412 + chan * 5;
3775 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3780 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3784 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3785 "known)", params->freq);
3788 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3789 wpa_s->assoc_freq && !freq) {
3790 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3792 params->freq = wpa_s->assoc_freq;
3795 res = wpa_drv_shared_freq(wpa_s);
3796 if (res > 0 && !freq) {
3797 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3798 "already using on a shared interface");
3800 } else if (res > 0 && freq != res &&
3801 !(wpa_s->drv_flags &
3802 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3803 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3804 "while connected on another channel (%u MHz)",
3813 static struct wpa_supplicant *
3814 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3817 struct wpa_supplicant *group_wpa_s;
3819 if (!wpas_p2p_create_iface(wpa_s))
3822 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3823 WPA_IF_P2P_CLIENT) < 0)
3825 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3826 if (group_wpa_s == NULL) {
3827 wpas_p2p_remove_pending_group_interface(wpa_s);
3836 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3837 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3838 * @persistent_group: Whether to create a persistent group
3839 * @freq: Frequency for the group or 0 to indicate no hardcoding
3840 * Returns: 0 on success, -1 on failure
3842 * This function creates a new P2P group with the local end as the Group Owner,
3843 * i.e., without using Group Owner Negotiation.
3845 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3848 struct p2p_go_neg_results params;
3851 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3854 /* Make sure we are not running find during connection establishment */
3855 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3856 wpas_p2p_stop_find(wpa_s);
3859 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3861 if (wpa_s->best_24_freq > 0 &&
3862 p2p_supported_freq(wpa_s->global->p2p,
3863 wpa_s->best_24_freq)) {
3864 freq = wpa_s->best_24_freq;
3865 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3866 "channel: %d MHz", freq);
3868 os_get_random((u8 *) &r, sizeof(r));
3869 freq = 2412 + (r % 3) * 25;
3870 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3871 "channel: %d MHz", freq);
3876 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3878 if (wpa_s->best_5_freq > 0 &&
3879 p2p_supported_freq(wpa_s->global->p2p,
3880 wpa_s->best_5_freq)) {
3881 freq = wpa_s->best_5_freq;
3882 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3883 "channel: %d MHz", freq);
3885 os_get_random((u8 *) &r, sizeof(r));
3886 freq = 5180 + (r % 4) * 20;
3887 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3888 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3889 "5 GHz channel for P2P group");
3892 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3893 "channel: %d MHz", freq);
3897 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3898 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3899 "(%u MHz) is not supported for P2P uses",
3904 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40))
3907 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3908 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3909 "(%u MHz) is not supported for P2P uses",
3913 p2p_go_params(wpa_s->global->p2p, ¶ms);
3914 params.persistent_group = persistent_group;
3916 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3919 wpas_start_wps_go(wpa_s, ¶ms, 0);
3925 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3926 struct wpa_ssid *params, int addr_allocated)
3928 struct wpa_ssid *ssid;
3930 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3934 wpa_supplicant_ap_deinit(wpa_s);
3936 ssid = wpa_config_add_network(wpa_s->conf);
3939 wpa_config_set_network_defaults(ssid);
3940 ssid->temporary = 1;
3941 ssid->proto = WPA_PROTO_RSN;
3942 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3943 ssid->group_cipher = WPA_CIPHER_CCMP;
3944 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3945 ssid->ssid = os_malloc(params->ssid_len);
3946 if (ssid->ssid == NULL) {
3947 wpa_config_remove_network(wpa_s->conf, ssid->id);
3950 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3951 ssid->ssid_len = params->ssid_len;
3952 ssid->p2p_group = 1;
3953 ssid->export_keys = 1;
3954 if (params->psk_set) {
3955 os_memcpy(ssid->psk, params->psk, 32);
3958 if (params->passphrase)
3959 ssid->passphrase = os_strdup(params->passphrase);
3961 wpa_supplicant_select_network(wpa_s, ssid);
3963 wpa_s->show_group_started = 1;
3969 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3970 struct wpa_ssid *ssid, int addr_allocated,
3973 struct p2p_go_neg_results params;
3976 if (ssid->disabled != 2 || ssid->ssid == NULL)
3979 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3980 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3981 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3986 /* Make sure we are not running find during connection establishment */
3987 wpas_p2p_stop_find(wpa_s);
3989 wpa_s->p2p_fallback_to_go_neg = 0;
3991 if (ssid->mode == WPAS_MODE_INFRA)
3992 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3994 if (ssid->mode != WPAS_MODE_P2P_GO)
3997 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40))
4001 if (ssid->passphrase == NULL ||
4002 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4003 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4007 os_strlcpy(params.passphrase, ssid->passphrase,
4008 sizeof(params.passphrase));
4009 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4010 params.ssid_len = ssid->ssid_len;
4011 params.persistent_group = 1;
4013 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4017 wpas_start_wps_go(wpa_s, ¶ms, 0);
4023 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4024 struct wpabuf *proberesp_ies)
4026 struct wpa_supplicant *wpa_s = ctx;
4027 if (wpa_s->ap_iface) {
4028 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4029 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4030 wpabuf_free(beacon_ies);
4031 wpabuf_free(proberesp_ies);
4035 wpabuf_free(hapd->p2p_beacon_ie);
4036 hapd->p2p_beacon_ie = beacon_ies;
4038 wpabuf_free(hapd->p2p_probe_resp_ie);
4039 hapd->p2p_probe_resp_ie = proberesp_ies;
4041 wpabuf_free(beacon_ies);
4042 wpabuf_free(proberesp_ies);
4044 wpa_supplicant_ap_update_beacon(wpa_s);
4048 static void wpas_p2p_idle_update(void *ctx, int idle)
4050 struct wpa_supplicant *wpa_s = ctx;
4051 if (!wpa_s->ap_iface)
4053 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4055 wpas_p2p_set_group_idle_timeout(wpa_s);
4057 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4061 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4062 struct wpa_ssid *ssid)
4064 struct p2p_group *group;
4065 struct p2p_group_config *cfg;
4067 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4069 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4072 cfg = os_zalloc(sizeof(*cfg));
4076 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4077 cfg->persistent_group = 2;
4078 else if (ssid->p2p_persistent_group)
4079 cfg->persistent_group = 1;
4080 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4081 if (wpa_s->max_stations &&
4082 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4083 cfg->max_clients = wpa_s->max_stations;
4085 cfg->max_clients = wpa_s->conf->max_num_sta;
4086 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4087 cfg->ssid_len = ssid->ssid_len;
4088 cfg->cb_ctx = wpa_s;
4089 cfg->ie_update = wpas_p2p_ie_update;
4090 cfg->idle_update = wpas_p2p_idle_update;
4092 group = p2p_group_init(wpa_s->global->p2p, cfg);
4095 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4096 p2p_group_notif_formation_done(group);
4097 wpa_s->p2p_group = group;
4102 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4105 struct wpa_ssid *ssid = wpa_s->current_ssid;
4107 if (!wpa_s->p2p_in_provisioning) {
4108 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4109 "provisioning not in progress");
4113 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4114 u8 go_dev_addr[ETH_ALEN];
4115 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4116 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4118 /* Clear any stored provisioning info */
4119 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4122 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4124 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4126 * Use a separate timeout for initial data connection to
4127 * complete to allow the group to be removed automatically if
4128 * something goes wrong in this step before the P2P group idle
4129 * timeout mechanism is taken into use.
4131 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4132 wpas_p2p_group_formation_timeout,
4133 wpa_s->parent, NULL);
4135 if (wpa_s->global->p2p)
4136 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4137 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4138 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4139 wpas_group_formation_completed(wpa_s, 1);
4143 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4144 struct wps_event_fail *fail)
4146 if (!wpa_s->p2p_in_provisioning) {
4147 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4148 "provisioning not in progress");
4152 if (wpa_s->go_params) {
4153 p2p_clear_provisioning_info(
4155 wpa_s->go_params->peer_device_addr);
4158 wpas_notify_p2p_wps_failed(wpa_s, fail);
4162 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4163 const char *config_method,
4164 enum wpas_p2p_prov_disc_use use)
4168 wpa_s->p2p_fallback_to_go_neg = 0;
4169 wpa_s->pending_pd_use = NORMAL_PD;
4170 if (os_strncmp(config_method, "display", 7) == 0)
4171 config_methods = WPS_CONFIG_DISPLAY;
4172 else if (os_strncmp(config_method, "keypad", 6) == 0)
4173 config_methods = WPS_CONFIG_KEYPAD;
4174 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4175 os_strncmp(config_method, "pushbutton", 10) == 0)
4176 config_methods = WPS_CONFIG_PUSHBUTTON;
4178 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
4182 if (use == WPAS_P2P_PD_AUTO) {
4183 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4184 wpa_s->pending_pd_config_methods = config_methods;
4185 wpa_s->p2p_auto_pd = 1;
4186 wpa_s->p2p_auto_join = 0;
4187 wpa_s->pending_pd_before_join = 0;
4188 wpa_s->auto_pd_scan_retry = 0;
4189 wpas_p2p_stop_find(wpa_s);
4190 wpa_s->p2p_join_scan_count = 0;
4191 os_get_time(&wpa_s->p2p_auto_started);
4192 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4193 wpa_s->p2p_auto_started.sec,
4194 wpa_s->p2p_auto_started.usec);
4195 wpas_p2p_join_scan(wpa_s, NULL);
4199 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4200 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
4202 use == WPAS_P2P_PD_FOR_JOIN);
4205 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4208 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
4209 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4214 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4217 return p2p_scan_result_text(ies, ies_len, buf, end);
4221 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4223 if (!wpa_s->pending_action_tx)
4226 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4227 "operation request");
4228 wpabuf_free(wpa_s->pending_action_tx);
4229 wpa_s->pending_action_tx = NULL;
4233 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4234 enum p2p_discovery_type type,
4235 unsigned int num_req_dev_types, const u8 *req_dev_types,
4236 const u8 *dev_id, unsigned int search_delay)
4238 wpas_p2p_clear_pending_action_tx(wpa_s);
4239 wpa_s->p2p_long_listen = 0;
4241 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4242 return wpa_drv_p2p_find(wpa_s, timeout, type);
4244 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4245 wpa_s->p2p_in_provisioning)
4248 wpa_supplicant_cancel_sched_scan(wpa_s);
4250 return p2p_find(wpa_s->global->p2p, timeout, type,
4251 num_req_dev_types, req_dev_types, dev_id,
4256 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4258 wpas_p2p_clear_pending_action_tx(wpa_s);
4259 wpa_s->p2p_long_listen = 0;
4260 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4261 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4262 wpa_s->p2p_cb_on_scan_complete = 0;
4264 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4265 wpa_drv_p2p_stop_find(wpa_s);
4269 if (wpa_s->global->p2p)
4270 p2p_stop_find(wpa_s->global->p2p);
4272 wpas_p2p_remove_pending_group_interface(wpa_s);
4276 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4278 struct wpa_supplicant *wpa_s = eloop_ctx;
4279 wpa_s->p2p_long_listen = 0;
4283 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4287 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4290 wpa_supplicant_cancel_sched_scan(wpa_s);
4291 wpas_p2p_clear_pending_action_tx(wpa_s);
4295 * This is a request for unlimited Listen state. However, at
4296 * least for now, this is mapped to a Listen state for one
4301 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4302 wpa_s->p2p_long_listen = 0;
4305 * Stop previous find/listen operation to avoid trying to request a new
4306 * remain-on-channel operation while the driver is still running the
4309 if (wpa_s->global->p2p)
4310 p2p_stop_find(wpa_s->global->p2p);
4312 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4313 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4314 wpa_s->p2p_long_listen = timeout * 1000;
4315 eloop_register_timeout(timeout, 0,
4316 wpas_p2p_long_listen_timeout,
4324 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4325 u8 *buf, size_t len, int p2p_group)
4327 struct wpabuf *p2p_ie;
4330 if (wpa_s->global->p2p_disabled)
4332 if (wpa_s->global->p2p == NULL)
4337 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4338 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4340 wpabuf_free(p2p_ie);
4346 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4347 const u8 *dst, const u8 *bssid,
4348 const u8 *ie, size_t ie_len, int ssi_signal)
4350 if (wpa_s->global->p2p_disabled)
4352 if (wpa_s->global->p2p == NULL)
4355 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4357 case P2P_PREQ_NOT_P2P:
4358 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4361 case P2P_PREQ_MALFORMED:
4362 case P2P_PREQ_NOT_LISTEN:
4363 case P2P_PREQ_NOT_PROCESSED:
4364 default: /* make gcc happy */
4366 case P2P_PREQ_PROCESSED:
4372 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4373 const u8 *sa, const u8 *bssid,
4374 u8 category, const u8 *data, size_t len, int freq)
4376 if (wpa_s->global->p2p_disabled)
4378 if (wpa_s->global->p2p == NULL)
4381 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4386 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4388 if (wpa_s->global->p2p_disabled)
4390 if (wpa_s->global->p2p == NULL)
4393 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4397 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4399 p2p_group_deinit(wpa_s->p2p_group);
4400 wpa_s->p2p_group = NULL;
4402 wpa_s->ap_configured_cb = NULL;
4403 wpa_s->ap_configured_cb_ctx = NULL;
4404 wpa_s->ap_configured_cb_data = NULL;
4405 wpa_s->connect_without_scan = NULL;
4409 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4411 wpa_s->p2p_long_listen = 0;
4413 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4414 return wpa_drv_p2p_reject(wpa_s, addr);
4416 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4419 return p2p_reject(wpa_s->global->p2p, addr);
4423 /* Invite to reinvoke a persistent group */
4424 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4425 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4428 enum p2p_invite_role role;
4431 wpa_s->p2p_persistent_go_freq = freq;
4432 wpa_s->p2p_go_ht40 = !!ht40;
4433 if (ssid->mode == WPAS_MODE_P2P_GO) {
4434 role = P2P_INVITE_ROLE_GO;
4435 if (peer_addr == NULL) {
4436 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4437 "address in invitation command");
4440 if (wpas_p2p_create_iface(wpa_s)) {
4441 if (wpas_p2p_add_group_interface(wpa_s,
4442 WPA_IF_P2P_GO) < 0) {
4443 wpa_printf(MSG_ERROR, "P2P: Failed to "
4444 "allocate a new interface for the "
4448 bssid = wpa_s->pending_interface_addr;
4450 bssid = wpa_s->own_addr;
4452 role = P2P_INVITE_ROLE_CLIENT;
4453 peer_addr = ssid->bssid;
4455 wpa_s->pending_invite_ssid_id = ssid->id;
4457 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4458 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4459 ssid->ssid, ssid->ssid_len,
4462 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4465 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4466 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4470 /* Invite to join an active group */
4471 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4472 const u8 *peer_addr, const u8 *go_dev_addr)
4474 struct wpa_global *global = wpa_s->global;
4475 enum p2p_invite_role role;
4477 struct wpa_ssid *ssid;
4480 wpa_s->p2p_persistent_go_freq = 0;
4481 wpa_s->p2p_go_ht40 = 0;
4483 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4484 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4487 if (wpa_s == NULL) {
4488 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4492 ssid = wpa_s->current_ssid;
4494 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4499 persistent = ssid->p2p_persistent_group &&
4500 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4501 ssid->ssid, ssid->ssid_len);
4503 if (ssid->mode == WPAS_MODE_P2P_GO) {
4504 role = P2P_INVITE_ROLE_ACTIVE_GO;
4505 bssid = wpa_s->own_addr;
4506 if (go_dev_addr == NULL)
4507 go_dev_addr = wpa_s->global->p2p_dev_addr;
4509 role = P2P_INVITE_ROLE_CLIENT;
4510 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4511 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4512 "invite to current group");
4515 bssid = wpa_s->bssid;
4516 if (go_dev_addr == NULL &&
4517 !is_zero_ether_addr(wpa_s->go_dev_addr))
4518 go_dev_addr = wpa_s->go_dev_addr;
4520 wpa_s->parent->pending_invite_ssid_id = -1;
4522 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4523 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4524 ssid->ssid, ssid->ssid_len,
4525 go_dev_addr, persistent);
4527 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4530 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4531 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
4532 go_dev_addr, persistent);
4536 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4538 struct wpa_ssid *ssid = wpa_s->current_ssid;
4539 const char *ssid_txt;
4540 u8 go_dev_addr[ETH_ALEN];
4541 int network_id = -1;
4545 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4546 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4547 wpa_s->parent, NULL);
4550 if (!wpa_s->show_group_started || !ssid)
4553 wpa_s->show_group_started = 0;
4555 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4556 os_memset(go_dev_addr, 0, ETH_ALEN);
4557 if (ssid->bssid_set)
4558 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4559 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4561 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4563 if (wpa_s->global->p2p_group_formation == wpa_s)
4564 wpa_s->global->p2p_group_formation = NULL;
4566 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4567 (int) wpa_s->assoc_freq;
4568 if (ssid->passphrase == NULL && ssid->psk_set) {
4570 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4571 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4572 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4574 wpa_s->ifname, ssid_txt, freq, psk,
4575 MAC2STR(go_dev_addr),
4576 persistent ? " [PERSISTENT]" : "");
4578 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4579 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4580 "go_dev_addr=" MACSTR "%s",
4581 wpa_s->ifname, ssid_txt, freq,
4582 ssid->passphrase ? ssid->passphrase : "",
4583 MAC2STR(go_dev_addr),
4584 persistent ? " [PERSISTENT]" : "");
4588 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4591 network_id = ssid->id;
4592 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4595 if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4596 wpa_s->global->p2p != NULL) {
4597 wpa_s->p2p_cb_on_scan_complete = 0;
4598 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4599 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4600 "continued after successful connection");
4601 p2p_increase_search_delay(
4603 wpas_p2p_search_delay(wpa_s));
4609 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4610 u32 interval1, u32 duration2, u32 interval2)
4612 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4614 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4617 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4618 wpa_s->current_ssid == NULL ||
4619 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4622 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4623 wpa_s->own_addr, wpa_s->assoc_freq,
4624 duration1, interval1, duration2, interval2);
4628 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4629 unsigned int interval)
4631 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4634 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4637 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4641 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4643 return wpa_s->current_ssid != NULL &&
4644 wpa_s->current_ssid->p2p_group &&
4645 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4649 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4651 struct wpa_supplicant *wpa_s = eloop_ctx;
4653 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4654 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4659 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4661 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
4665 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4669 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4670 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4672 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4675 timeout = wpa_s->conf->p2p_group_idle;
4676 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4677 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4678 timeout = P2P_MAX_CLIENT_IDLE;
4684 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4685 timeout = 0; /* special client mode no-timeout */
4690 if (wpa_s->p2p_in_provisioning) {
4692 * Use the normal group formation timeout during the
4693 * provisioning phase to avoid terminating this process too
4694 * early due to group idle timeout.
4696 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4697 "during provisioning");
4701 if (wpa_s->show_group_started) {
4703 * Use the normal group formation timeout between the end of
4704 * the provisioning phase and completion of 4-way handshake to
4705 * avoid terminating this process too early due to group idle
4708 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4709 "while waiting for initial 4-way handshake to "
4715 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4717 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4722 /* Returns 1 if the interface was removed */
4723 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4724 u16 reason_code, const u8 *ie, size_t ie_len,
4725 int locally_generated)
4727 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4729 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4732 if (!locally_generated)
4733 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4736 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4737 wpa_s->current_ssid &&
4738 wpa_s->current_ssid->p2p_group &&
4739 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4740 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4741 "session is ending");
4742 if (wpas_p2p_group_delete(wpa_s,
4743 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4752 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4753 u16 reason_code, const u8 *ie, size_t ie_len,
4754 int locally_generated)
4756 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4758 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4761 if (!locally_generated)
4762 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4767 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4769 struct p2p_data *p2p = wpa_s->global->p2p;
4774 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4777 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4778 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4780 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4781 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4784 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4785 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4787 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4788 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4790 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4791 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4792 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4793 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4794 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4797 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4798 p2p_set_sec_dev_types(p2p,
4799 (void *) wpa_s->conf->sec_device_type,
4800 wpa_s->conf->num_sec_device_types);
4802 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4804 p2p_remove_wps_vendor_extensions(p2p);
4805 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4806 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4808 p2p_add_wps_vendor_extension(
4809 p2p, wpa_s->conf->wps_vendor_ext[i]);
4813 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4814 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4816 country[0] = wpa_s->conf->country[0];
4817 country[1] = wpa_s->conf->country[1];
4819 p2p_set_country(p2p, country);
4822 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4823 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4824 wpa_s->conf->p2p_ssid_postfix ?
4825 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4829 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4830 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4832 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4833 u8 reg_class, channel;
4836 if (wpa_s->conf->p2p_listen_reg_class &&
4837 wpa_s->conf->p2p_listen_channel) {
4838 reg_class = wpa_s->conf->p2p_listen_reg_class;
4839 channel = wpa_s->conf->p2p_listen_channel;
4843 * Pick one of the social channels randomly as the
4846 os_get_random((u8 *) &r, sizeof(r));
4847 channel = 1 + (r % 3) * 5;
4849 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4851 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4854 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4855 u8 op_reg_class, op_channel, cfg_op_channel;
4858 if (wpa_s->conf->p2p_oper_reg_class &&
4859 wpa_s->conf->p2p_oper_channel) {
4860 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4861 op_channel = wpa_s->conf->p2p_oper_channel;
4866 * Use random operation channel from (1, 6, 11)
4867 *if no other preference is indicated.
4869 os_get_random((u8 *) &r, sizeof(r));
4870 op_channel = 1 + (r % 3) * 5;
4873 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4876 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4880 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4881 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4882 wpa_s->conf->p2p_pref_chan) < 0) {
4883 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4890 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4893 if (!wpa_s->ap_iface)
4895 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4900 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4902 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4904 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4907 wpa_s->global->cross_connection = enabled;
4908 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4911 struct wpa_supplicant *iface;
4913 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4915 if (iface->cross_connect_enabled == 0)
4918 iface->cross_connect_enabled = 0;
4919 iface->cross_connect_in_use = 0;
4920 wpa_msg(iface->parent, MSG_INFO,
4921 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4922 iface->ifname, iface->cross_connect_uplink);
4930 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4932 struct wpa_supplicant *iface;
4934 if (!uplink->global->cross_connection)
4937 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4938 if (!iface->cross_connect_enabled)
4940 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4943 if (iface->ap_iface == NULL)
4945 if (iface->cross_connect_in_use)
4948 iface->cross_connect_in_use = 1;
4949 wpa_msg(iface->parent, MSG_INFO,
4950 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4951 iface->ifname, iface->cross_connect_uplink);
4956 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4958 struct wpa_supplicant *iface;
4960 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4961 if (!iface->cross_connect_enabled)
4963 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4966 if (!iface->cross_connect_in_use)
4969 wpa_msg(iface->parent, MSG_INFO,
4970 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4971 iface->ifname, iface->cross_connect_uplink);
4972 iface->cross_connect_in_use = 0;
4977 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4979 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4980 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4981 wpa_s->cross_connect_disallowed)
4982 wpas_p2p_disable_cross_connect(wpa_s);
4984 wpas_p2p_enable_cross_connect(wpa_s);
4985 if (!wpa_s->ap_iface &&
4986 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4987 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4991 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4993 wpas_p2p_disable_cross_connect(wpa_s);
4994 if (!wpa_s->ap_iface &&
4995 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4997 wpas_p2p_set_group_idle_timeout(wpa_s);
5001 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5003 struct wpa_supplicant *iface;
5005 if (!wpa_s->global->cross_connection)
5008 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5011 if (iface->drv_flags &
5012 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5014 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5017 wpa_s->cross_connect_enabled = 1;
5018 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5019 sizeof(wpa_s->cross_connect_uplink));
5020 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5021 "%s to %s whenever uplink is available",
5022 wpa_s->ifname, wpa_s->cross_connect_uplink);
5024 if (iface->ap_iface || iface->current_ssid == NULL ||
5025 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5026 iface->cross_connect_disallowed ||
5027 iface->wpa_state != WPA_COMPLETED)
5030 wpa_s->cross_connect_in_use = 1;
5031 wpa_msg(wpa_s->parent, MSG_INFO,
5032 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5033 wpa_s->ifname, wpa_s->cross_connect_uplink);
5039 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5041 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5042 !wpa_s->p2p_in_provisioning)
5043 return 0; /* not P2P client operation */
5045 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5047 if (wpa_s != wpa_s->parent)
5048 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5050 if (wpa_s->global->p2p)
5051 p2p_group_formation_failed(wpa_s->global->p2p);
5053 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5054 wpa_s->parent, NULL);
5056 wpas_group_formation_completed(wpa_s, 0);
5061 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5063 struct p2p_channels chan;
5065 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5068 os_memset(&chan, 0, sizeof(chan));
5069 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5070 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5075 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5079 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5081 struct wpa_global *global = wpa_s->global;
5085 if (global->p2p == NULL)
5088 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5090 if (wpa_s->pending_interface_name[0] &&
5091 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5094 peer = p2p_get_go_neg_peer(global->p2p);
5096 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5097 MACSTR, MAC2STR(peer));
5098 p2p_unauthorize(global->p2p, peer);
5102 wpas_p2p_stop_find(wpa_s);
5104 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5105 if (wpa_s == global->p2p_group_formation &&
5106 (wpa_s->p2p_in_provisioning ||
5107 wpa_s->parent->pending_interface_type ==
5108 WPA_IF_P2P_CLIENT)) {
5109 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5110 "formation found - cancelling",
5113 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5114 wpa_s->parent, NULL);
5115 wpas_p2p_group_delete(wpa_s,
5116 P2P_GROUP_REMOVAL_REQUESTED);
5122 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5130 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5132 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5135 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5136 "being available anymore");
5137 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
5141 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5142 int freq_24, int freq_5, int freq_overall)
5144 struct p2p_data *p2p = wpa_s->global->p2p;
5145 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5147 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5151 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5154 struct p2p_data *p2p = wpa_s->global->p2p;
5156 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5159 if (hwaddr_aton(addr, peer))
5162 return p2p_unauthorize(p2p, peer);
5167 * wpas_p2p_disconnect - Disconnect from a P2P Group
5168 * @wpa_s: Pointer to wpa_supplicant data
5169 * Returns: 0 on success, -1 on failure
5171 * This can be used to disconnect from a group in which the local end is a P2P
5172 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5173 * virtual network interface was created for this group, that interface will be
5174 * removed. Otherwise, only the configured P2P group network will be removed
5175 * from the interface.
5177 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5183 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5188 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5191 struct wpa_supplicant *group = wpa_s;
5194 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5198 while (group && (group->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)) {
5199 if(group->wpa_state == WPA_ASSOCIATED) {
5200 /* WPA_ASSOCIATED hasn't moved to WPA_COMPLETED. So it could be in WPS
5201 * or 4Way Hanshake phase. Avoid allowing scan during this time critical
5204 wpa_printf(MSG_ERROR, "P2P: WPS/4way handshake in Progress."
5208 group = group->next;
5212 return p2p_in_progress(wpa_s->global->p2p);
5216 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5217 struct wpa_ssid *ssid)
5219 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5220 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5221 wpa_s->parent, NULL) > 0) {
5223 * Remove the network by scheduling the group formation
5224 * timeout to happen immediately. The teardown code
5225 * needs to be scheduled to run asynch later so that we
5226 * don't delete data from under ourselves unexpectedly.
5227 * Calling wpas_p2p_group_formation_timeout directly
5228 * causes a series of crashes in WPS failure scenarios.
5230 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5231 "P2P group network getting removed");
5232 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5233 wpa_s->parent, NULL);
5238 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
5239 const u8 *addr, const u8 *ssid,
5245 for (s = wpa_s->conf->ssid; s; s = s->next) {
5246 if (s->disabled != 2)
5249 (ssid_len != s->ssid_len ||
5250 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5252 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5253 return s; /* peer is GO in the persistent group */
5254 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5256 for (i = 0; i < s->num_p2p_clients; i++) {
5257 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5258 addr, ETH_ALEN) == 0)
5259 return s; /* peer is P2P client in persistent
5268 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5273 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5277 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5280 struct wpa_supplicant *group = wpa_s;
5281 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5282 if (wpa_s->global->p2p_group_formation)
5283 group = wpa_s->global->p2p_group_formation;
5284 wpa_s = wpa_s->parent;
5285 offchannel_send_action_done(wpa_s);
5287 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
5288 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5289 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5290 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5291 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5292 wpa_s->p2p_persistent_id,
5293 wpa_s->p2p_pd_before_go_neg,
5294 wpa_s->p2p_go_ht40);
5298 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5300 if (!wpa_s->p2p_fallback_to_go_neg ||
5301 wpa_s->p2p_in_provisioning <= 5)
5304 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5305 return 0; /* peer operating as a GO */
5307 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5308 "fallback to GO Negotiation");
5309 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5315 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5317 const char *rn, *rn2;
5318 struct wpa_supplicant *ifs;
5320 if (wpa_s->wpa_state > WPA_SCANNING) {
5321 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5322 "concurrent operation",
5323 P2P_CONCURRENT_SEARCH_DELAY);
5324 return P2P_CONCURRENT_SEARCH_DELAY;
5327 if (!wpa_s->driver->get_radio_name)
5329 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5330 if (rn == NULL || rn[0] == '\0')
5333 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5334 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5337 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5338 if (!rn2 || os_strcmp(rn, rn2) != 0)
5340 if (ifs->wpa_state > WPA_SCANNING) {
5341 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5342 "delay due to concurrent operation on "
5344 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5345 return P2P_CONCURRENT_SEARCH_DELAY;
5353 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
5355 struct wpa_supplicant *iface = NULL;
5356 struct p2p_data *p2p = wpa_s->global->p2p;
5358 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5359 if((iface->p2p_group_interface) && (iface->current_ssid) &&
5360 (iface->current_ssid->frequency != freq)) {
5362 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
5363 /* Try to see whether we can move the GO. If it
5364 * is not possible, remove the GO interface
5366 if(wpa_drv_switch_channel(iface, freq) == 0) {
5367 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5368 iface->current_ssid->frequency = freq;
5373 /* If GO cannot be moved or if the conflicting interface is a
5374 * P2P Client, remove the interface depending up on the connection
5376 if(!wpas_is_p2p_prioritized(wpa_s)) {
5377 /* STA connection has priority over existing
5378 * P2P connection. So remove the interface */
5379 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5380 "concurrent mode frequency conflict");
5381 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
5383 /* Existing connection has the priority. Disable the newly
5384 * selected network and let the application know about it.