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];
1190 #define WFD_DEV_INFO_SIZE 9
1191 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
1192 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
1194 #ifdef CONFIG_WIFI_DISPLAY
1195 if (info->wfd_subelems) {
1196 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1197 wpabuf_head(info->wfd_subelems),
1200 #endif /* CONFIG_WIFI_DISPLAY */
1202 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1203 " p2p_dev_addr=" MACSTR
1204 " pri_dev_type=%s name='%s' config_methods=0x%x "
1205 "dev_capab=0x%x group_capab=0x%x%s%s",
1206 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1207 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1209 info->device_name, info->config_methods,
1210 info->dev_capab, info->group_capab,
1211 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
1212 #endif /* CONFIG_NO_STDOUT_DEBUG */
1214 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1218 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1220 struct wpa_supplicant *wpa_s = ctx;
1222 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1223 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1225 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1229 static int wpas_start_listen(void *ctx, unsigned int freq,
1230 unsigned int duration,
1231 const struct wpabuf *probe_resp_ie)
1233 struct wpa_supplicant *wpa_s = ctx;
1235 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1237 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1238 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1239 "report received Probe Request frames");
1243 wpa_s->pending_listen_freq = freq;
1244 wpa_s->pending_listen_duration = duration;
1246 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1247 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1248 "to remain on channel (%u MHz) for Listen "
1250 wpa_s->pending_listen_freq = 0;
1253 wpa_s->off_channel_freq = 0;
1254 wpa_s->roc_waiting_drv_freq = freq;
1260 static void wpas_stop_listen(void *ctx)
1262 struct wpa_supplicant *wpa_s = ctx;
1263 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1264 wpa_drv_cancel_remain_on_channel(wpa_s);
1265 wpa_s->off_channel_freq = 0;
1266 wpa_s->roc_waiting_drv_freq = 0;
1268 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1269 wpa_drv_probe_req_report(wpa_s, 0);
1273 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1275 struct wpa_supplicant *wpa_s = ctx;
1276 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1280 static struct p2p_srv_bonjour *
1281 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1282 const struct wpabuf *query)
1284 struct p2p_srv_bonjour *bsrv;
1287 len = wpabuf_len(query);
1288 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1289 struct p2p_srv_bonjour, list) {
1290 if (len == wpabuf_len(bsrv->query) &&
1291 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1299 static struct p2p_srv_upnp *
1300 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1301 const char *service)
1303 struct p2p_srv_upnp *usrv;
1305 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1306 struct p2p_srv_upnp, list) {
1307 if (version == usrv->version &&
1308 os_strcmp(service, usrv->service) == 0)
1315 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1320 if (wpabuf_tailroom(resp) < 5)
1323 /* Length (to be filled) */
1324 len_pos = wpabuf_put(resp, 2);
1325 wpabuf_put_u8(resp, srv_proto);
1326 wpabuf_put_u8(resp, srv_trans_id);
1328 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1329 /* Response Data: empty */
1330 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1334 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1335 struct wpabuf *resp, u8 srv_trans_id)
1337 struct p2p_srv_bonjour *bsrv;
1340 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1342 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1343 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1347 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1348 struct p2p_srv_bonjour, list) {
1349 if (wpabuf_tailroom(resp) <
1350 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1352 /* Length (to be filled) */
1353 len_pos = wpabuf_put(resp, 2);
1354 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1355 wpabuf_put_u8(resp, srv_trans_id);
1357 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1358 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1359 wpabuf_head(bsrv->resp),
1360 wpabuf_len(bsrv->resp));
1362 wpabuf_put_buf(resp, bsrv->query); /* Key */
1363 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1364 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1370 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1371 struct wpabuf *resp, u8 srv_trans_id,
1372 const u8 *query, size_t query_len)
1374 struct p2p_srv_bonjour *bsrv;
1378 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1380 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1381 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1382 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1387 if (query_len == 0) {
1388 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1392 if (wpabuf_tailroom(resp) < 5)
1394 /* Length (to be filled) */
1395 len_pos = wpabuf_put(resp, 2);
1396 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1397 wpabuf_put_u8(resp, srv_trans_id);
1399 wpabuf_set(&buf, query, query_len);
1400 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1402 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1406 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1407 /* Response Data: empty */
1408 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1414 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1415 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1416 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1418 if (wpabuf_tailroom(resp) >=
1419 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1421 wpabuf_put_buf(resp, bsrv->query); /* Key */
1422 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1424 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1428 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1429 struct wpabuf *resp, u8 srv_trans_id)
1431 struct p2p_srv_upnp *usrv;
1434 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1436 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1437 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1441 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1442 struct p2p_srv_upnp, list) {
1443 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1446 /* Length (to be filled) */
1447 len_pos = wpabuf_put(resp, 2);
1448 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1449 wpabuf_put_u8(resp, srv_trans_id);
1452 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1454 wpabuf_put_u8(resp, usrv->version);
1455 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1457 wpabuf_put_str(resp, usrv->service);
1458 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1464 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1465 struct wpabuf *resp, u8 srv_trans_id,
1466 const u8 *query, size_t query_len)
1468 struct p2p_srv_upnp *usrv;
1474 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1477 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1478 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1479 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1484 if (query_len == 0) {
1485 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1489 if (wpabuf_tailroom(resp) < 5)
1492 /* Length (to be filled) */
1493 len_pos = wpabuf_put(resp, 2);
1494 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1495 wpabuf_put_u8(resp, srv_trans_id);
1498 str = os_malloc(query_len);
1501 os_memcpy(str, query + 1, query_len - 1);
1502 str[query_len - 1] = '\0';
1504 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1505 struct p2p_srv_upnp, list) {
1506 if (version != usrv->version)
1509 if (os_strcmp(str, "ssdp:all") != 0 &&
1510 os_strstr(usrv->service, str) == NULL)
1513 if (wpabuf_tailroom(resp) < 2)
1517 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1519 wpabuf_put_u8(resp, version);
1521 wpabuf_put_u8(resp, ',');
1525 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1527 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1529 wpabuf_put_str(resp, usrv->service);
1534 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1537 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1538 /* Response Data: empty */
1541 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1545 #ifdef CONFIG_WIFI_DISPLAY
1546 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1547 struct wpabuf *resp, u8 srv_trans_id,
1548 const u8 *query, size_t query_len)
1554 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1556 if (!wpa_s->global->wifi_display) {
1557 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1558 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1563 if (query_len < 1) {
1564 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1569 if (wpabuf_tailroom(resp) < 5)
1574 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1576 /* TODO: role specific handling */
1578 /* Length (to be filled) */
1579 len_pos = wpabuf_put(resp, 2);
1580 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1581 wpabuf_put_u8(resp, srv_trans_id);
1582 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1584 while (pos < query + query_len) {
1585 if (*pos < MAX_WFD_SUBELEMS &&
1586 wpa_s->global->wfd_subelem[*pos] &&
1587 wpabuf_tailroom(resp) >=
1588 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1589 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1590 "subelement %u", *pos);
1591 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1596 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1598 #endif /* CONFIG_WIFI_DISPLAY */
1601 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1602 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1604 struct wpa_supplicant *wpa_s = ctx;
1605 const u8 *pos = tlvs;
1606 const u8 *end = tlvs + tlvs_len;
1609 struct wpabuf *resp;
1610 u8 srv_proto, srv_trans_id;
1614 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1616 buf_len = 2 * tlvs_len + 1;
1617 buf = os_malloc(buf_len);
1619 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1620 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1622 freq, MAC2STR(sa), dialog_token, update_indic,
1627 if (wpa_s->p2p_sd_over_ctrl_iface) {
1628 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1629 update_indic, tlvs, tlvs_len);
1630 return; /* to be processed by an external program */
1633 resp = wpabuf_alloc(10000);
1637 while (pos + 1 < end) {
1638 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1639 slen = WPA_GET_LE16(pos);
1641 if (pos + slen > end || slen < 2) {
1642 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1647 tlv_end = pos + slen;
1650 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1652 srv_trans_id = *pos++;
1653 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1656 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1657 pos, tlv_end - pos);
1660 if (wpa_s->force_long_sd) {
1661 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1663 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1664 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1668 switch (srv_proto) {
1669 case P2P_SERV_ALL_SERVICES:
1670 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1671 "for all services");
1672 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1673 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1674 wpa_printf(MSG_DEBUG, "P2P: No service "
1675 "discovery protocols available");
1676 wpas_sd_add_proto_not_avail(
1677 resp, P2P_SERV_ALL_SERVICES,
1681 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1682 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1684 case P2P_SERV_BONJOUR:
1685 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1686 pos, tlv_end - pos);
1689 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1690 pos, tlv_end - pos);
1692 #ifdef CONFIG_WIFI_DISPLAY
1693 case P2P_SERV_WIFI_DISPLAY:
1694 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1695 pos, tlv_end - pos);
1697 #endif /* CONFIG_WIFI_DISPLAY */
1699 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1700 "protocol %u", srv_proto);
1701 wpas_sd_add_proto_not_avail(resp, srv_proto,
1710 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1711 update_indic, tlvs, tlvs_len);
1713 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1719 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1720 const u8 *tlvs, size_t tlvs_len)
1722 struct wpa_supplicant *wpa_s = ctx;
1723 const u8 *pos = tlvs;
1724 const u8 *end = tlvs + tlvs_len;
1730 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1732 if (tlvs_len > 1500) {
1733 /* TODO: better way for handling this */
1734 wpa_msg_ctrl(wpa_s, MSG_INFO,
1735 P2P_EVENT_SERV_DISC_RESP MACSTR
1736 " %u <long response: %u bytes>",
1737 MAC2STR(sa), update_indic,
1738 (unsigned int) tlvs_len);
1740 buf_len = 2 * tlvs_len + 1;
1741 buf = os_malloc(buf_len);
1743 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1744 wpa_msg_ctrl(wpa_s, MSG_INFO,
1745 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1746 MAC2STR(sa), update_indic, buf);
1752 u8 srv_proto, srv_trans_id, status;
1754 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1755 slen = WPA_GET_LE16(pos);
1757 if (pos + slen > end || slen < 3) {
1758 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1762 tlv_end = pos + slen;
1765 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1767 srv_trans_id = *pos++;
1768 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1771 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1774 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1775 pos, tlv_end - pos);
1780 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1784 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1785 const struct wpabuf *tlvs)
1787 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1788 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1789 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1791 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1795 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1796 u8 version, const char *query)
1798 struct wpabuf *tlvs;
1801 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1804 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1805 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1806 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1807 wpabuf_put_u8(tlvs, version);
1808 wpabuf_put_str(tlvs, query);
1809 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1815 #ifdef CONFIG_WIFI_DISPLAY
1817 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1818 const struct wpabuf *tlvs)
1820 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1822 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1824 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1828 #define MAX_WFD_SD_SUBELEMS 20
1830 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1831 const char *subelems)
1838 len = wpabuf_put(tlvs, 2);
1839 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1840 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1842 wpabuf_put_u8(tlvs, role);
1847 if (val >= 0 && val < 256) {
1848 wpabuf_put_u8(tlvs, val);
1850 if (count == MAX_WFD_SD_SUBELEMS)
1853 pos = os_strchr(pos + 1, ',');
1859 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1863 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1864 const u8 *dst, const char *role)
1866 struct wpabuf *tlvs;
1868 const char *subelems;
1871 subelems = os_strchr(role, ' ');
1872 if (subelems == NULL)
1876 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1880 if (os_strstr(role, "[source]"))
1881 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1882 if (os_strstr(role, "[pri-sink]"))
1883 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1884 if (os_strstr(role, "[sec-sink]"))
1885 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1886 if (os_strstr(role, "[source+sink]"))
1887 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1889 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1894 #endif /* CONFIG_WIFI_DISPLAY */
1897 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
1899 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1900 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
1901 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1903 return p2p_sd_cancel_request(wpa_s->global->p2p,
1904 (void *) (uintptr_t) req);
1908 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1909 const u8 *dst, u8 dialog_token,
1910 const struct wpabuf *resp_tlvs)
1912 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1913 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1917 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1919 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1924 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1926 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1929 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1930 wpa_drv_p2p_service_update(wpa_s);
1933 if (wpa_s->global->p2p)
1935 p2p_sd_service_update(wpa_s->global->p2p, action);
1937 p2p_sd_service_update(wpa_s->global->p2p);
1942 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1944 dl_list_del(&bsrv->list);
1945 wpabuf_free(bsrv->query);
1946 wpabuf_free(bsrv->resp);
1951 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1953 dl_list_del(&usrv->list);
1954 os_free(usrv->service);
1959 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1961 struct p2p_srv_bonjour *bsrv, *bn;
1962 struct p2p_srv_upnp *usrv, *un;
1964 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1965 struct p2p_srv_bonjour, list)
1966 wpas_p2p_srv_bonjour_free(bsrv);
1968 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1969 struct p2p_srv_upnp, list)
1970 wpas_p2p_srv_upnp_free(usrv);
1973 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1975 wpas_p2p_sd_service_update(wpa_s);
1980 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1981 struct wpabuf *query, struct wpabuf *resp)
1983 struct p2p_srv_bonjour *bsrv;
1985 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1988 wpabuf_free(bsrv->resp);
1993 bsrv = os_zalloc(sizeof(*bsrv));
1996 bsrv->query = query;
1998 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2001 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2003 wpas_p2p_sd_service_update(wpa_s);
2009 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2010 const struct wpabuf *query)
2012 struct p2p_srv_bonjour *bsrv;
2014 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2017 wpas_p2p_srv_bonjour_free(bsrv);
2019 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2021 wpas_p2p_sd_service_update(wpa_s);
2027 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2028 const char *service)
2030 struct p2p_srv_upnp *usrv;
2032 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2033 return 0; /* Already listed */
2034 usrv = os_zalloc(sizeof(*usrv));
2037 usrv->version = version;
2038 usrv->service = os_strdup(service);
2039 if (usrv->service == NULL) {
2043 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2046 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2048 wpas_p2p_sd_service_update(wpa_s);
2054 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2055 const char *service)
2057 struct p2p_srv_upnp *usrv;
2059 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2062 wpas_p2p_srv_upnp_free(usrv);
2064 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2066 wpas_p2p_sd_service_update(wpa_s);
2072 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2073 const u8 *peer, const char *params,
2074 unsigned int generated_pin)
2076 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2077 MAC2STR(peer), generated_pin, params);
2081 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2082 const u8 *peer, const char *params)
2084 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2085 MAC2STR(peer), params);
2089 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2090 const u8 *dev_addr, const u8 *pri_dev_type,
2091 const char *dev_name, u16 supp_config_methods,
2092 u8 dev_capab, u8 group_capab, const u8 *group_id,
2093 size_t group_id_len)
2095 struct wpa_supplicant *wpa_s = ctx;
2096 char devtype[WPS_DEV_TYPE_BUFSIZE];
2098 u8 empty_dev_type[8];
2099 unsigned int generated_pin = 0;
2100 struct wpa_supplicant *group = NULL;
2103 for (group = wpa_s->global->ifaces; group; group = group->next)
2105 struct wpa_ssid *s = group->current_ssid;
2107 s->mode == WPAS_MODE_P2P_GO &&
2108 group_id_len - ETH_ALEN == s->ssid_len &&
2109 os_memcmp(group_id + ETH_ALEN, s->ssid,
2115 if (pri_dev_type == NULL) {
2116 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2117 pri_dev_type = empty_dev_type;
2119 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2120 " pri_dev_type=%s name='%s' config_methods=0x%x "
2121 "dev_capab=0x%x group_capab=0x%x%s%s",
2123 wps_dev_type_bin2str(pri_dev_type, devtype,
2125 dev_name, supp_config_methods, dev_capab, group_capab,
2126 group ? " group=" : "",
2127 group ? group->ifname : "");
2128 params[sizeof(params) - 1] = '\0';
2130 if (config_methods & WPS_CONFIG_DISPLAY) {
2131 generated_pin = wps_generate_pin();
2132 wpas_prov_disc_local_display(wpa_s, peer, params,
2134 } else if (config_methods & WPS_CONFIG_KEYPAD)
2135 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2136 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2137 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2138 "%s", MAC2STR(peer), params);
2140 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2141 P2P_PROV_DISC_SUCCESS,
2142 config_methods, generated_pin);
2146 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2148 struct wpa_supplicant *wpa_s = ctx;
2149 unsigned int generated_pin = 0;
2152 if (wpa_s->pending_pd_before_join &&
2153 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2154 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2155 wpa_s->pending_pd_before_join = 0;
2156 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2157 "join-existing-group operation");
2158 wpas_p2p_join_start(wpa_s);
2162 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2163 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2164 os_snprintf(params, sizeof(params), " peer_go=%d",
2165 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2169 if (config_methods & WPS_CONFIG_DISPLAY)
2170 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2171 else if (config_methods & WPS_CONFIG_KEYPAD) {
2172 generated_pin = wps_generate_pin();
2173 wpas_prov_disc_local_display(wpa_s, peer, params,
2175 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2176 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2177 "%s", MAC2STR(peer), params);
2179 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2180 P2P_PROV_DISC_SUCCESS,
2181 config_methods, generated_pin);
2185 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2186 enum p2p_prov_disc_status status)
2188 struct wpa_supplicant *wpa_s = ctx;
2190 if (wpa_s->p2p_fallback_to_go_neg) {
2191 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2192 "failed - fall back to GO Negotiation");
2193 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2197 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2198 " p2p_dev_addr=" MACSTR " status=%d",
2199 MAC2STR(peer), status);
2201 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2206 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2207 const u8 *go_dev_addr, const u8 *ssid,
2208 size_t ssid_len, int *go, u8 *group_bssid,
2209 int *force_freq, int persistent_group)
2211 struct wpa_supplicant *wpa_s = ctx;
2213 u8 cur_bssid[ETH_ALEN];
2215 struct wpa_supplicant *grp;
2217 if (!persistent_group) {
2218 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2219 " to join an active group", MAC2STR(sa));
2220 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2221 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2223 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2224 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2225 "authorized invitation");
2229 * Do not accept the invitation automatically; notify user and
2232 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2235 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2237 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2238 "running persistent group");
2240 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2244 if (!wpa_s->conf->persistent_reconnect)
2245 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2247 for (s = wpa_s->conf->ssid; s; s = s->next) {
2248 if (s->disabled == 2 &&
2249 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2250 s->ssid_len == ssid_len &&
2251 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2256 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2257 " requested reinvocation of an unknown group",
2259 return P2P_SC_FAIL_UNKNOWN_GROUP;
2262 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2264 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2265 wpa_printf(MSG_DEBUG, "P2P: The only available "
2266 "interface is already in use - reject "
2268 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2270 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2271 } else if (s->mode == WPAS_MODE_P2P_GO) {
2273 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2275 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2276 "interface address for the group");
2277 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2279 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2284 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2285 wpa_s->assoc_freq) {
2286 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2287 "the channel we are already using");
2288 *force_freq = wpa_s->assoc_freq;
2291 res = wpa_drv_shared_freq(wpa_s);
2293 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2294 "with the channel we are already using on a "
2295 "shared interface");
2299 return P2P_SC_SUCCESS;
2303 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2304 const u8 *ssid, size_t ssid_len,
2305 const u8 *go_dev_addr, u8 status,
2308 struct wpa_supplicant *wpa_s = ctx;
2311 for (s = wpa_s->conf->ssid; s; s = s->next) {
2312 if (s->disabled == 2 &&
2313 s->ssid_len == ssid_len &&
2314 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2318 if (status == P2P_SC_SUCCESS) {
2319 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2320 " was accepted; op_freq=%d MHz",
2321 MAC2STR(sa), op_freq);
2323 wpas_p2p_group_add_persistent(
2324 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
2326 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2327 wpa_s->p2p_wps_method, 0);
2332 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2333 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2334 " was rejected (status %u)", MAC2STR(sa), status);
2340 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2341 "sa=" MACSTR " go_dev_addr=" MACSTR
2342 " bssid=" MACSTR " unknown-network",
2343 MAC2STR(sa), MAC2STR(go_dev_addr),
2346 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2347 "sa=" MACSTR " go_dev_addr=" MACSTR
2349 MAC2STR(sa), MAC2STR(go_dev_addr));
2354 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2355 " persistent=%d", MAC2STR(sa), s->id);
2359 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2361 struct wpa_supplicant *wpa_s = ctx;
2362 struct wpa_ssid *ssid;
2365 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2366 "status=%d " MACSTR,
2367 status, MAC2STR(bssid));
2369 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2370 "status=%d ", status);
2372 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2374 if (wpa_s->pending_invite_ssid_id == -1)
2375 return; /* Invitation to active group */
2377 if (status != P2P_SC_SUCCESS) {
2378 wpas_p2p_remove_pending_group_interface(wpa_s);
2382 ssid = wpa_config_get_network(wpa_s->conf,
2383 wpa_s->pending_invite_ssid_id);
2385 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2386 "data matching with invitation");
2390 wpas_p2p_group_add_persistent(wpa_s, ssid,
2391 ssid->mode == WPAS_MODE_P2P_GO,
2392 wpa_s->p2p_persistent_go_freq,
2393 wpa_s->p2p_go_ht40);
2397 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2402 if (global->p2p_disallow_freq == NULL)
2405 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2406 if (freq >= global->p2p_disallow_freq[i].min &&
2407 freq <= global->p2p_disallow_freq[i].max)
2415 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2417 reg->channel[reg->channels] = chan;
2422 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2423 struct p2p_channels *chan)
2427 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2430 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2431 chan->reg_class[cla].reg_class = 81;
2432 chan->reg_class[cla].channels = 0;
2433 for (i = 0; i < 11; i++) {
2434 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2435 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2437 if (chan->reg_class[cla].channels)
2440 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2443 /* Operating class 115 - 5 GHz, channels 36-48 */
2444 chan->reg_class[cla].reg_class = 115;
2445 chan->reg_class[cla].channels = 0;
2446 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2447 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2448 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2449 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2450 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2451 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2452 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2453 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2454 if (chan->reg_class[cla].channels)
2457 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2460 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2461 chan->reg_class[cla].reg_class = 124;
2462 chan->reg_class[cla].channels = 0;
2463 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2464 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2465 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2466 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2467 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2468 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2469 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2470 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2471 if (chan->reg_class[cla].channels)
2474 chan->reg_classes = cla;
2479 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2481 enum hostapd_hw_mode mode)
2485 for (i = 0; i < num_modes; i++) {
2486 if (modes[i].mode == mode)
2494 static int has_channel(struct wpa_global *global,
2495 struct hostapd_hw_modes *mode, u8 chan, int *flags)
2500 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2502 if (wpas_p2p_disallowed_freq(global, freq))
2505 for (i = 0; i < mode->num_channels; i++) {
2506 if (mode->channels[i].chan == chan) {
2508 *flags = mode->channels[i].flag;
2509 return !(mode->channels[i].flag &
2510 (HOSTAPD_CHAN_DISABLED |
2511 HOSTAPD_CHAN_PASSIVE_SCAN |
2512 HOSTAPD_CHAN_NO_IBSS |
2513 HOSTAPD_CHAN_RADAR));
2521 struct p2p_oper_class_map {
2522 enum hostapd_hw_mode mode;
2527 enum { BW20, BW40PLUS, BW40MINUS } bw;
2530 static struct p2p_oper_class_map op_class[] = {
2531 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2532 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2533 #if 0 /* Do not enable HT40 on 2 GHz for now */
2534 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2535 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2537 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2538 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2539 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2540 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2541 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2542 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2543 { -1, 0, 0, 0, 0, BW20 }
2547 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2548 struct hostapd_hw_modes *mode,
2553 if (!has_channel(wpa_s->global, mode, channel, &flag))
2555 if (bw == BW40MINUS &&
2556 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2557 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2559 if (bw == BW40PLUS &&
2560 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2561 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2567 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2568 struct p2p_channels *chan)
2570 struct hostapd_hw_modes *mode;
2573 if (wpa_s->hw.modes == NULL) {
2574 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2575 "of all supported channels; assume dualband "
2577 return wpas_p2p_default_channels(wpa_s, chan);
2582 for (op = 0; op_class[op].op_class; op++) {
2583 struct p2p_oper_class_map *o = &op_class[op];
2585 struct p2p_reg_class *reg = NULL;
2587 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2590 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2591 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2594 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2595 "class %u", o->op_class);
2596 reg = &chan->reg_class[cla];
2598 reg->reg_class = o->op_class;
2600 reg->channel[reg->channels] = ch;
2604 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2605 reg->channel, reg->channels);
2609 chan->reg_classes = cla;
2615 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2616 struct hostapd_hw_modes *mode, u8 channel)
2620 for (op = 0; op_class[op].op_class; op++) {
2621 struct p2p_oper_class_map *o = &op_class[op];
2624 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2625 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2626 o->bw == BW20 || ch != channel)
2628 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2632 return (o->bw == BW40MINUS) ? -1 : 1;
2641 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2644 struct wpa_supplicant *wpa_s = ctx;
2646 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2647 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2653 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2657 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2659 struct wpa_supplicant *wpa_s = ctx;
2661 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2662 struct wpa_ssid *ssid = wpa_s->current_ssid;
2665 if (ssid->mode != WPAS_MODE_INFRA)
2667 if (wpa_s->wpa_state != WPA_COMPLETED &&
2668 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2670 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2679 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2680 * @global: Pointer to global data from wpa_supplicant_init()
2681 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2682 * Returns: 0 on success, -1 on failure
2684 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2686 struct p2p_config p2p;
2690 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2696 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2697 struct p2p_params params;
2699 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2700 os_memset(¶ms, 0, sizeof(params));
2701 params.dev_name = wpa_s->conf->device_name;
2702 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2704 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2705 os_memcpy(params.sec_dev_type,
2706 wpa_s->conf->sec_device_type,
2707 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2709 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2715 os_memset(&p2p, 0, sizeof(p2p));
2716 p2p.msg_ctx = wpa_s;
2718 p2p.p2p_scan = wpas_p2p_scan;
2719 p2p.send_action = wpas_send_action;
2720 p2p.send_action_done = wpas_send_action_done;
2721 p2p.go_neg_completed = wpas_go_neg_completed;
2722 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2723 p2p.dev_found = wpas_dev_found;
2724 p2p.dev_lost = wpas_dev_lost;
2725 p2p.start_listen = wpas_start_listen;
2726 p2p.stop_listen = wpas_stop_listen;
2727 p2p.send_probe_resp = wpas_send_probe_resp;
2728 p2p.sd_request = wpas_sd_request;
2729 p2p.sd_response = wpas_sd_response;
2730 p2p.prov_disc_req = wpas_prov_disc_req;
2731 p2p.prov_disc_resp = wpas_prov_disc_resp;
2732 p2p.prov_disc_fail = wpas_prov_disc_fail;
2733 p2p.invitation_process = wpas_invitation_process;
2734 p2p.invitation_received = wpas_invitation_received;
2735 p2p.invitation_result = wpas_invitation_result;
2736 p2p.get_noa = wpas_get_noa;
2737 p2p.go_connected = wpas_go_connected;
2739 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2740 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2741 p2p.dev_name = wpa_s->conf->device_name;
2742 p2p.manufacturer = wpa_s->conf->manufacturer;
2743 p2p.model_name = wpa_s->conf->model_name;
2744 p2p.model_number = wpa_s->conf->model_number;
2745 p2p.serial_number = wpa_s->conf->serial_number;
2747 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2748 p2p.config_methods = wpa_s->wps->config_methods;
2751 if (wpa_s->conf->p2p_listen_reg_class &&
2752 wpa_s->conf->p2p_listen_channel) {
2753 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2754 p2p.channel = wpa_s->conf->p2p_listen_channel;
2758 * Pick one of the social channels randomly as the listen
2761 os_get_random((u8 *) &r, sizeof(r));
2762 p2p.channel = 1 + (r % 3) * 5;
2764 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2766 if (wpa_s->conf->p2p_oper_reg_class &&
2767 wpa_s->conf->p2p_oper_channel) {
2768 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2769 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2770 p2p.cfg_op_channel = 1;
2771 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2772 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2775 p2p.op_reg_class = 81;
2777 * Use random operation channel from (1, 6, 11) if no other
2778 * preference is indicated.
2780 os_get_random((u8 *) &r, sizeof(r));
2781 p2p.op_channel = 1 + (r % 3) * 5;
2782 p2p.cfg_op_channel = 0;
2783 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2784 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2786 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2787 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2788 p2p.country[2] = 0x04;
2790 os_memcpy(p2p.country, "XX\x04", 3);
2792 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2793 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2798 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2801 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2802 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2803 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2805 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2806 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2808 p2p.max_peers = 100;
2810 if (wpa_s->conf->p2p_ssid_postfix) {
2811 p2p.ssid_postfix_len =
2812 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2813 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2814 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2815 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2816 p2p.ssid_postfix_len);
2819 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2822 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2823 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
2824 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)
2825 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
2828 global->p2p = p2p_init(&p2p);
2829 if (global->p2p == NULL)
2831 global->p2p_init_wpa_s = wpa_s;
2833 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2834 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2836 p2p_add_wps_vendor_extension(
2837 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2845 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2846 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2848 * This function deinitialize per-interface P2P data.
2850 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2852 if (wpa_s->driver && wpa_s->drv_priv)
2853 wpa_drv_probe_req_report(wpa_s, 0);
2855 if (wpa_s->go_params) {
2856 /* Clear any stored provisioning info */
2857 p2p_clear_provisioning_info(
2859 wpa_s->go_params->peer_device_addr);
2862 os_free(wpa_s->go_params);
2863 wpa_s->go_params = NULL;
2864 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2865 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2866 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2867 wpa_s->p2p_long_listen = 0;
2868 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2869 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2870 wpas_p2p_remove_pending_group_interface(wpa_s);
2872 /* TODO: remove group interface from the driver if this wpa_s instance
2873 * is on top of a P2P group interface */
2878 * wpas_p2p_deinit_global - Deinitialize global P2P module
2879 * @global: Pointer to global data from wpa_supplicant_init()
2881 * This function deinitializes the global (per device) P2P module.
2883 void wpas_p2p_deinit_global(struct wpa_global *global)
2885 struct wpa_supplicant *wpa_s, *tmp;
2887 wpa_s = global->ifaces;
2889 wpas_p2p_service_flush(wpa_s);
2891 if (global->p2p == NULL)
2894 /* Remove remaining P2P group interfaces */
2895 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2896 wpa_s = wpa_s->next;
2898 tmp = global->ifaces;
2901 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2906 /* Disconnect from the P2P group and deinit the interface */
2907 wpas_p2p_disconnect(tmp);
2911 * Deinit GO data on any possibly remaining interface (if main
2912 * interface is used as GO).
2914 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2915 if (wpa_s->ap_iface)
2916 wpas_p2p_group_deinit(wpa_s);
2919 p2p_deinit(global->p2p);
2921 global->p2p_init_wpa_s = NULL;
2925 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2927 if (wpa_s->drv_flags &
2928 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2929 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2930 return 1; /* P2P group requires a new interface in every case
2932 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2933 return 0; /* driver does not support concurrent operations */
2934 if (wpa_s->global->ifaces->next)
2935 return 1; /* more that one interface already in use */
2936 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2937 return 1; /* this interface is already in use */
2942 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2943 const u8 *peer_addr,
2944 enum p2p_wps_method wps_method,
2945 int go_intent, const u8 *own_interface_addr,
2946 unsigned int force_freq, int persistent_group,
2947 struct wpa_ssid *ssid)
2949 if (persistent_group && wpa_s->conf->persistent_reconnect)
2950 persistent_group = 2;
2952 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2953 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2954 go_intent, own_interface_addr,
2955 force_freq, persistent_group);
2959 * Increase GO config timeout if HT40 is used since it takes some time
2960 * to scan channels for coex purposes before the BSS can be started.
2962 p2p_set_config_timeout(wpa_s->global->p2p,
2963 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2965 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2966 go_intent, own_interface_addr, force_freq,
2967 persistent_group, ssid ? ssid->ssid : NULL,
2968 ssid ? ssid->ssid_len : 0,
2969 wpa_s->p2p_pd_before_go_neg);
2973 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2974 const u8 *peer_addr,
2975 enum p2p_wps_method wps_method,
2976 int go_intent, const u8 *own_interface_addr,
2977 unsigned int force_freq, int persistent_group,
2978 struct wpa_ssid *ssid)
2980 if (persistent_group && wpa_s->conf->persistent_reconnect)
2981 persistent_group = 2;
2983 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2986 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2987 go_intent, own_interface_addr, force_freq,
2988 persistent_group, ssid ? ssid->ssid : NULL,
2989 ssid ? ssid->ssid_len : 0);
2993 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2995 wpa_s->p2p_join_scan_count++;
2996 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2997 wpa_s->p2p_join_scan_count);
2998 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2999 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3000 " for join operationg - stop join attempt",
3001 MAC2STR(wpa_s->pending_join_iface_addr));
3002 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3003 if (wpa_s->p2p_auto_pd) {
3004 wpa_s->p2p_auto_pd = 0;
3005 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3006 " p2p_dev_addr=" MACSTR " status=N/A",
3007 MAC2STR(wpa_s->pending_join_dev_addr));
3010 wpa_msg(wpa_s->parent, MSG_INFO,
3011 P2P_EVENT_GROUP_FORMATION_FAILURE);
3016 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
3018 struct wpa_supplicant *wpa_s = eloop_ctx;
3019 if (!wpa_s->pending_pd_before_join)
3022 * Provision Discovery Response may have been lost - try to connect
3023 * anyway since we do not need any information from this PD.
3025 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
3026 "try to connect anyway");
3027 wpas_p2p_join_start(wpa_s);
3031 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3033 struct wpa_supplicant *iface;
3037 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3040 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3041 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3043 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3045 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3046 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3047 shared_freq = iface->current_ssid->frequency;
3048 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3049 shared_freq = iface->assoc_freq;
3053 if (shared_freq && freq != shared_freq) {
3054 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3055 "connected on %d MHz - new connection on "
3056 "%d MHz", iface->ifname, shared_freq, freq);
3061 shared_freq = wpa_drv_shared_freq(wpa_s);
3062 if (shared_freq > 0 && shared_freq != freq) {
3063 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3064 "virtual interface connected on %d MHz - new "
3065 "connection on %d MHz", shared_freq, freq);
3073 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3074 const u8 *peer_dev_addr)
3076 struct wpa_bss *bss;
3079 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3082 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3083 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3088 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3089 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3090 "%ld.%06ld (%supdated in last scan)",
3091 bss->last_update.sec, bss->last_update.usec,
3092 updated ? "": "not ");
3098 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3099 struct wpa_scan_results *scan_res)
3101 struct wpa_bss *bss;
3103 u8 iface_addr[ETH_ALEN];
3105 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3107 if (wpa_s->global->p2p_disabled)
3110 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3111 scan_res ? (int) scan_res->num : -1,
3112 wpa_s->p2p_auto_join ? "auto_" : "");
3115 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3117 if (wpa_s->p2p_auto_pd) {
3118 int join = wpas_p2p_peer_go(wpa_s,
3119 wpa_s->pending_join_dev_addr);
3121 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3122 wpa_s->auto_pd_scan_retry++;
3123 bss = wpa_bss_get_bssid(wpa_s,
3124 wpa_s->pending_join_dev_addr);
3127 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3128 "the peer " MACSTR " at %d MHz",
3129 wpa_s->auto_pd_scan_retry,
3131 pending_join_dev_addr),
3133 wpas_p2p_join_scan_req(wpa_s, freq);
3141 wpa_s->p2p_auto_pd = 0;
3142 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3143 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3144 MAC2STR(wpa_s->pending_join_dev_addr), join);
3145 if (p2p_prov_disc_req(wpa_s->global->p2p,
3146 wpa_s->pending_join_dev_addr,
3147 wpa_s->pending_pd_config_methods, join,
3149 wpa_s->p2p_auto_pd = 0;
3150 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3151 " p2p_dev_addr=" MACSTR " status=N/A",
3152 MAC2STR(wpa_s->pending_join_dev_addr));
3157 if (wpa_s->p2p_auto_join) {
3158 int join = wpas_p2p_peer_go(wpa_s,
3159 wpa_s->pending_join_dev_addr);
3161 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3162 "running a GO -> use GO Negotiation");
3163 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3164 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3165 wpa_s->p2p_persistent_group, 0, 0, 0,
3166 wpa_s->p2p_go_intent,
3167 wpa_s->p2p_connect_freq,
3168 wpa_s->p2p_persistent_id,
3169 wpa_s->p2p_pd_before_go_neg,
3170 wpa_s->p2p_go_ht40);
3174 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3175 "try to join the group", join ? "" :
3178 wpa_s->p2p_fallback_to_go_neg = 1;
3181 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3182 wpa_s->pending_join_iface_addr);
3184 p2p_get_interface_addr(wpa_s->global->p2p,
3185 wpa_s->pending_join_dev_addr,
3187 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3189 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3190 "address for join from " MACSTR " to " MACSTR
3191 " based on newly discovered P2P peer entry",
3192 MAC2STR(wpa_s->pending_join_iface_addr),
3193 MAC2STR(iface_addr));
3194 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3197 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3198 wpa_s->pending_join_iface_addr);
3201 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3202 "from P2P peer table: %d MHz", freq);
3204 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3207 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3208 "from BSS table: %d MHz", freq);
3213 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3214 wpa_msg(wpa_s->parent, MSG_INFO,
3215 P2P_EVENT_GROUP_FORMATION_FAILURE
3216 "reason=FREQ_CONFLICT");
3220 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3221 "prior to joining an existing group (GO " MACSTR
3223 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3224 wpa_s->pending_pd_before_join = 1;
3226 switch (wpa_s->pending_join_wps_method) {
3227 case WPS_PIN_DISPLAY:
3228 method = WPS_CONFIG_KEYPAD;
3230 case WPS_PIN_KEYPAD:
3231 method = WPS_CONFIG_DISPLAY;
3234 method = WPS_CONFIG_PUSHBUTTON;
3241 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3242 wpa_s->pending_join_dev_addr) ==
3245 * We have already performed provision discovery for
3246 * joining the group. Proceed directly to join
3247 * operation without duplicated provision discovery. */
3248 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3249 "with " MACSTR " already done - proceed to "
3251 MAC2STR(wpa_s->pending_join_dev_addr));
3252 wpa_s->pending_pd_before_join = 0;
3256 if (p2p_prov_disc_req(wpa_s->global->p2p,
3257 wpa_s->pending_join_dev_addr, method, 1,
3259 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3260 "Discovery Request before joining an "
3262 wpa_s->pending_pd_before_join = 0;
3267 * Actual join operation will be started from the Action frame
3268 * TX status callback (if no ACK is received) or when the
3269 * Provision Discovery Response is received. Use a short
3270 * timeout as a backup mechanism should the Provision Discovery
3271 * Response be lost for any reason.
3273 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3275 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3280 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3281 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3282 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3283 wpas_p2p_check_join_scan_limit(wpa_s);
3287 /* Start join operation immediately */
3288 wpas_p2p_join_start(wpa_s);
3292 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3295 struct wpa_driver_scan_params params;
3296 struct wpabuf *wps_ie, *ies;
3298 int freqs[2] = { 0, 0 };
3302 /* If freq is not provided, check the operating freq of the GO and do a
3303 * a directed scan to save time
3306 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3307 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3310 os_memset(¶ms, 0, sizeof(params));
3312 /* P2P Wildcard SSID */
3313 params.num_ssids = 1;
3314 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3315 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3317 wpa_s->wps->dev.p2p = 1;
3318 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3319 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3321 if (wps_ie == NULL) {
3322 wpas_p2p_scan_res_join(wpa_s, NULL);
3326 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3327 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3329 wpabuf_free(wps_ie);
3330 wpas_p2p_scan_res_join(wpa_s, NULL);
3333 wpabuf_put_buf(ies, wps_ie);
3334 wpabuf_free(wps_ie);
3336 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3338 params.p2p_probe = 1;
3339 params.extra_ies = wpabuf_head(ies);
3340 params.extra_ies_len = wpabuf_len(ies);
3343 params.freqs = freqs;
3347 * Run a scan to update BSS table and start Provision Discovery once
3348 * the new scan results become available.
3350 ret = wpa_drv_scan(wpa_s, ¶ms);
3352 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3357 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3359 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3360 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3361 wpas_p2p_check_join_scan_limit(wpa_s);
3366 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3368 struct wpa_supplicant *wpa_s = eloop_ctx;
3369 wpas_p2p_join_scan_req(wpa_s, 0);
3373 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3374 const u8 *dev_addr, enum p2p_wps_method wps_method,
3377 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3378 MACSTR " dev " MACSTR ")%s",
3379 MAC2STR(iface_addr), MAC2STR(dev_addr),
3380 auto_join ? " (auto_join)" : "");
3382 wpa_s->p2p_auto_pd = 0;
3383 wpa_s->p2p_auto_join = !!auto_join;
3384 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3385 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3386 wpa_s->pending_join_wps_method = wps_method;
3388 /* Make sure we are not running find during connection establishment */
3389 wpas_p2p_stop_find(wpa_s);
3391 wpa_s->p2p_join_scan_count = 0;
3392 wpas_p2p_join_scan(wpa_s, NULL);
3397 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3399 struct wpa_supplicant *group;
3400 struct p2p_go_neg_results res;
3401 struct wpa_bss *bss;
3403 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
3404 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3407 if (group != wpa_s) {
3408 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3409 sizeof(group->p2p_pin));
3410 group->p2p_wps_method = wpa_s->p2p_wps_method;
3413 group->p2p_in_provisioning = 1;
3414 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3416 os_memset(&res, 0, sizeof(res));
3417 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3419 res.wps_method = wpa_s->pending_join_wps_method;
3420 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3422 res.freq = bss->freq;
3423 res.ssid_len = bss->ssid_len;
3424 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3427 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3428 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3430 wpa_drv_cancel_remain_on_channel(wpa_s);
3431 wpa_s->off_channel_freq = 0;
3432 wpa_s->roc_waiting_drv_freq = 0;
3434 wpas_start_wps_enrollee(group, &res);
3437 * Allow a longer timeout for join-a-running-group than normal 15
3438 * second group formation timeout since the GO may not have authorized
3439 * our connection yet.
3441 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3442 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3450 * wpas_p2p_connect - Request P2P Group Formation to be started
3451 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3452 * @peer_addr: Address of the peer P2P Device
3453 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3454 * @persistent_group: Whether to create a persistent group
3455 * @auto_join: Whether to select join vs. GO Negotiation automatically
3456 * @join: Whether to join an existing group (as a client) instead of starting
3457 * Group Owner negotiation; @peer_addr is BSSID in that case
3458 * @auth: Whether to only authorize the connection instead of doing that and
3459 * initiating Group Owner negotiation
3460 * @go_intent: GO Intent or -1 to use default
3461 * @freq: Frequency for the group or 0 for auto-selection
3462 * @persistent_id: Persistent group credentials to use for forcing GO
3463 * parameters or -1 to generate new values (SSID/passphrase)
3464 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3465 * interoperability workaround when initiating group formation
3466 * @ht40: Start GO with 40 MHz channel width
3467 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3468 * failure, -2 on failure due to channel not currently available,
3469 * -3 if forced channel is not supported
3471 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3472 const char *pin, enum p2p_wps_method wps_method,
3473 int persistent_group, int auto_join, int join, int auth,
3474 int go_intent, int freq, int persistent_id, int pd,
3477 int force_freq = 0, oper_freq = 0;
3480 enum wpa_driver_if_type iftype;
3482 struct wpa_ssid *ssid = NULL;
3484 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3487 if (persistent_id >= 0) {
3488 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3489 if (ssid == NULL || ssid->disabled != 2 ||
3490 ssid->mode != WPAS_MODE_P2P_GO)
3495 go_intent = wpa_s->conf->p2p_go_intent;
3498 wpa_s->p2p_long_listen = 0;
3500 wpa_s->p2p_wps_method = wps_method;
3501 wpa_s->p2p_persistent_group = !!persistent_group;
3502 wpa_s->p2p_persistent_id = persistent_id;
3503 wpa_s->p2p_go_intent = go_intent;
3504 wpa_s->p2p_connect_freq = freq;
3505 wpa_s->p2p_fallback_to_go_neg = 0;
3506 wpa_s->p2p_pd_before_go_neg = !!pd;
3507 wpa_s->p2p_go_ht40 = !!ht40;
3510 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3511 else if (wps_method == WPS_PIN_DISPLAY) {
3512 ret = wps_generate_pin();
3513 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3515 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3518 wpa_s->p2p_pin[0] = '\0';
3520 if (join || auto_join) {
3521 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3523 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3524 "connect a running group from " MACSTR,
3525 MAC2STR(peer_addr));
3526 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3529 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3530 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3532 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3533 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3537 os_get_time(&wpa_s->p2p_auto_started);
3538 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3540 wpa_s->p2p_auto_started.sec,
3541 wpa_s->p2p_auto_started.usec);
3543 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3549 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3551 oper_freq = wpa_s->assoc_freq;
3553 oper_freq = wpa_drv_shared_freq(wpa_s);
3559 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3560 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3561 "(%u MHz) is not supported for P2P uses",
3566 if (oper_freq > 0 && freq != oper_freq &&
3567 !(wpa_s->drv_flags &
3568 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3569 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3570 "on %u MHz while connected on another "
3571 "channel (%u MHz)", freq, oper_freq);
3574 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3575 "requested channel (%u MHz)", freq);
3577 } else if (oper_freq > 0 &&
3578 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3579 if (!(wpa_s->drv_flags &
3580 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3581 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3582 "while connected on non-P2P supported "
3583 "channel (%u MHz)", oper_freq);
3586 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3587 "(%u MHz) not available for P2P - try to use "
3588 "another channel", oper_freq);
3590 } else if (oper_freq > 0) {
3591 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3592 "channel we are already using (%u MHz) on another "
3593 "interface", oper_freq);
3594 force_freq = oper_freq;
3597 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3599 if (wpa_s->create_p2p_iface) {
3600 /* Prepare to add a new interface for the group */
3601 iftype = WPA_IF_P2P_GROUP;
3602 if (go_intent == 15)
3603 iftype = WPA_IF_P2P_GO;
3604 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3605 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3606 "interface for the group");
3610 if_addr = wpa_s->pending_interface_addr;
3612 if_addr = wpa_s->own_addr;
3615 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3617 force_freq, persistent_group, ssid) <
3623 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3624 go_intent, if_addr, force_freq,
3625 persistent_group, ssid) < 0) {
3626 if (wpa_s->create_p2p_iface)
3627 wpas_p2p_remove_pending_group_interface(wpa_s);
3635 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3636 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3637 * @freq: Frequency of the channel in MHz
3638 * @duration: Duration of the stay on the channel in milliseconds
3640 * This callback is called when the driver indicates that it has started the
3641 * requested remain-on-channel duration.
3643 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3644 unsigned int freq, unsigned int duration)
3646 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3648 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3649 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3650 wpa_s->pending_listen_duration);
3651 wpa_s->pending_listen_freq = 0;
3656 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3657 unsigned int timeout)
3659 /* Limit maximum Listen state time based on driver limitation. */
3660 if (timeout > wpa_s->max_remain_on_chan)
3661 timeout = wpa_s->max_remain_on_chan;
3663 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3664 return wpa_drv_p2p_listen(wpa_s, timeout);
3666 return p2p_listen(wpa_s->global->p2p, timeout);
3671 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3672 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3673 * @freq: Frequency of the channel in MHz
3675 * This callback is called when the driver indicates that a remain-on-channel
3676 * operation has been completed, i.e., the duration on the requested channel
3679 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3682 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3683 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3684 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3685 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3687 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3688 return; /* P2P module started a new operation */
3689 if (wpa_s->pending_action_tx)
3691 if (wpa_s->p2p_long_listen > 0)
3692 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3693 if (wpa_s->p2p_long_listen > 0) {
3694 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3695 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3701 * wpas_p2p_group_remove - Remove a P2P group
3702 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3703 * @ifname: Network interface name of the group interface or "*" to remove all
3705 * Returns: 0 on success, -1 on failure
3707 * This function is used to remove a P2P group. This can be used to disconnect
3708 * from a group in which the local end is a P2P Client or to end a P2P Group in
3709 * case the local end is the Group Owner. If a virtual network interface was
3710 * created for this group, that interface will be removed. Otherwise, only the
3711 * configured P2P group network will be removed from the interface.
3713 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3715 struct wpa_global *global = wpa_s->global;
3717 if (os_strcmp(ifname, "*") == 0) {
3718 struct wpa_supplicant *prev;
3719 wpa_s = global->ifaces;
3722 wpa_s = wpa_s->next;
3723 wpas_p2p_disconnect(prev);
3728 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3729 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3733 return wpas_p2p_disconnect(wpa_s);
3737 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3738 struct p2p_go_neg_results *params,
3744 os_memset(params, 0, sizeof(*params));
3745 params->role_go = 1;
3746 params->ht40 = ht40;
3748 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3749 "frequency %d MHz", freq);
3750 params->freq = freq;
3751 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3752 wpa_s->conf->p2p_oper_channel >= 1 &&
3753 wpa_s->conf->p2p_oper_channel <= 11) {
3754 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3755 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3756 "frequency %d MHz", params->freq);
3757 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3758 wpa_s->conf->p2p_oper_reg_class == 124) {
3759 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3760 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3761 "frequency %d MHz", params->freq);
3762 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3763 wpa_s->best_overall_freq > 0 &&
3764 p2p_supported_freq(wpa_s->global->p2p,
3765 wpa_s->best_overall_freq)) {
3766 params->freq = wpa_s->best_overall_freq;
3767 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3768 "channel %d MHz", params->freq);
3769 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3770 wpa_s->best_24_freq > 0 &&
3771 p2p_supported_freq(wpa_s->global->p2p,
3772 wpa_s->best_24_freq)) {
3773 params->freq = wpa_s->best_24_freq;
3774 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3775 "channel %d MHz", params->freq);
3776 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3777 wpa_s->best_5_freq > 0 &&
3778 p2p_supported_freq(wpa_s->global->p2p,
3779 wpa_s->best_5_freq)) {
3780 params->freq = wpa_s->best_5_freq;
3781 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3782 "channel %d MHz", params->freq);
3785 for (chan = 0; chan < 11; chan++) {
3786 params->freq = 2412 + chan * 5;
3787 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3792 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3796 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3797 "known)", params->freq);
3800 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3801 wpa_s->assoc_freq && !freq) {
3802 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3804 params->freq = wpa_s->assoc_freq;
3807 res = wpa_drv_shared_freq(wpa_s);
3808 if (res > 0 && !freq) {
3809 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3810 "already using on a shared interface");
3812 } else if (res > 0 && freq != res &&
3813 !(wpa_s->drv_flags &
3814 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3815 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3816 "while connected on another channel (%u MHz)",
3825 static struct wpa_supplicant *
3826 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3829 struct wpa_supplicant *group_wpa_s;
3831 if (!wpas_p2p_create_iface(wpa_s))
3834 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3835 WPA_IF_P2P_CLIENT) < 0)
3837 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3838 if (group_wpa_s == NULL) {
3839 wpas_p2p_remove_pending_group_interface(wpa_s);
3848 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3849 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3850 * @persistent_group: Whether to create a persistent group
3851 * @freq: Frequency for the group or 0 to indicate no hardcoding
3852 * Returns: 0 on success, -1 on failure
3854 * This function creates a new P2P group with the local end as the Group Owner,
3855 * i.e., without using Group Owner Negotiation.
3857 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3860 struct p2p_go_neg_results params;
3863 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3866 /* Make sure we are not running find during connection establishment */
3867 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3868 wpas_p2p_stop_find(wpa_s);
3871 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3873 if (wpa_s->best_24_freq > 0 &&
3874 p2p_supported_freq(wpa_s->global->p2p,
3875 wpa_s->best_24_freq)) {
3876 freq = wpa_s->best_24_freq;
3877 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3878 "channel: %d MHz", freq);
3880 os_get_random((u8 *) &r, sizeof(r));
3881 freq = 2412 + (r % 3) * 25;
3882 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3883 "channel: %d MHz", freq);
3888 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3890 if (wpa_s->best_5_freq > 0 &&
3891 p2p_supported_freq(wpa_s->global->p2p,
3892 wpa_s->best_5_freq)) {
3893 freq = wpa_s->best_5_freq;
3894 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3895 "channel: %d MHz", freq);
3897 os_get_random((u8 *) &r, sizeof(r));
3898 freq = 5180 + (r % 4) * 20;
3899 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3900 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3901 "5 GHz channel for P2P group");
3904 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3905 "channel: %d MHz", freq);
3909 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3910 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3911 "(%u MHz) is not supported for P2P uses",
3916 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40))
3919 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3920 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3921 "(%u MHz) is not supported for P2P uses",
3925 p2p_go_params(wpa_s->global->p2p, ¶ms);
3926 params.persistent_group = persistent_group;
3928 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3931 wpas_start_wps_go(wpa_s, ¶ms, 0);
3937 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3938 struct wpa_ssid *params, int addr_allocated)
3940 struct wpa_ssid *ssid;
3942 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3946 wpa_supplicant_ap_deinit(wpa_s);
3948 ssid = wpa_config_add_network(wpa_s->conf);
3951 wpa_config_set_network_defaults(ssid);
3952 ssid->temporary = 1;
3953 ssid->proto = WPA_PROTO_RSN;
3954 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3955 ssid->group_cipher = WPA_CIPHER_CCMP;
3956 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3957 ssid->ssid = os_malloc(params->ssid_len);
3958 if (ssid->ssid == NULL) {
3959 wpa_config_remove_network(wpa_s->conf, ssid->id);
3962 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3963 ssid->ssid_len = params->ssid_len;
3964 ssid->p2p_group = 1;
3965 ssid->export_keys = 1;
3966 if (params->psk_set) {
3967 os_memcpy(ssid->psk, params->psk, 32);
3970 if (params->passphrase)
3971 ssid->passphrase = os_strdup(params->passphrase);
3973 wpa_supplicant_select_network(wpa_s, ssid);
3975 wpa_s->show_group_started = 1;
3981 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3982 struct wpa_ssid *ssid, int addr_allocated,
3985 struct p2p_go_neg_results params;
3988 if (ssid->disabled != 2 || ssid->ssid == NULL)
3991 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3992 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3993 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3998 /* Make sure we are not running find during connection establishment */
3999 wpas_p2p_stop_find(wpa_s);
4001 wpa_s->p2p_fallback_to_go_neg = 0;
4003 if (ssid->mode == WPAS_MODE_INFRA)
4004 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4006 if (ssid->mode != WPAS_MODE_P2P_GO)
4009 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40))
4013 if (ssid->passphrase == NULL ||
4014 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4015 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4019 os_strlcpy(params.passphrase, ssid->passphrase,
4020 sizeof(params.passphrase));
4021 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4022 params.ssid_len = ssid->ssid_len;
4023 params.persistent_group = 1;
4025 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4029 wpas_start_wps_go(wpa_s, ¶ms, 0);
4035 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4036 struct wpabuf *proberesp_ies)
4038 struct wpa_supplicant *wpa_s = ctx;
4039 if (wpa_s->ap_iface) {
4040 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4041 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4042 wpabuf_free(beacon_ies);
4043 wpabuf_free(proberesp_ies);
4047 wpabuf_free(hapd->p2p_beacon_ie);
4048 hapd->p2p_beacon_ie = beacon_ies;
4050 wpabuf_free(hapd->p2p_probe_resp_ie);
4051 hapd->p2p_probe_resp_ie = proberesp_ies;
4053 wpabuf_free(beacon_ies);
4054 wpabuf_free(proberesp_ies);
4056 wpa_supplicant_ap_update_beacon(wpa_s);
4060 static void wpas_p2p_idle_update(void *ctx, int idle)
4062 struct wpa_supplicant *wpa_s = ctx;
4063 if (!wpa_s->ap_iface)
4065 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4067 wpas_p2p_set_group_idle_timeout(wpa_s);
4069 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4073 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4074 struct wpa_ssid *ssid)
4076 struct p2p_group *group;
4077 struct p2p_group_config *cfg;
4079 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4081 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4084 cfg = os_zalloc(sizeof(*cfg));
4088 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4089 cfg->persistent_group = 2;
4090 else if (ssid->p2p_persistent_group)
4091 cfg->persistent_group = 1;
4092 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4093 if (wpa_s->max_stations &&
4094 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4095 cfg->max_clients = wpa_s->max_stations;
4097 cfg->max_clients = wpa_s->conf->max_num_sta;
4098 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4099 cfg->ssid_len = ssid->ssid_len;
4100 cfg->cb_ctx = wpa_s;
4101 cfg->ie_update = wpas_p2p_ie_update;
4102 cfg->idle_update = wpas_p2p_idle_update;
4104 group = p2p_group_init(wpa_s->global->p2p, cfg);
4107 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4108 p2p_group_notif_formation_done(group);
4109 wpa_s->p2p_group = group;
4114 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4117 struct wpa_ssid *ssid = wpa_s->current_ssid;
4119 if (!wpa_s->p2p_in_provisioning) {
4120 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4121 "provisioning not in progress");
4125 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4126 u8 go_dev_addr[ETH_ALEN];
4127 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4128 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4130 /* Clear any stored provisioning info */
4131 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4134 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4136 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4138 * Use a separate timeout for initial data connection to
4139 * complete to allow the group to be removed automatically if
4140 * something goes wrong in this step before the P2P group idle
4141 * timeout mechanism is taken into use.
4143 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4144 wpas_p2p_group_formation_timeout,
4145 wpa_s->parent, NULL);
4147 if (wpa_s->global->p2p)
4148 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4149 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4150 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4151 wpas_group_formation_completed(wpa_s, 1);
4155 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4156 struct wps_event_fail *fail)
4158 if (!wpa_s->p2p_in_provisioning) {
4159 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4160 "provisioning not in progress");
4164 if (wpa_s->go_params) {
4165 p2p_clear_provisioning_info(
4167 wpa_s->go_params->peer_device_addr);
4170 wpas_notify_p2p_wps_failed(wpa_s, fail);
4174 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4175 const char *config_method,
4176 enum wpas_p2p_prov_disc_use use)
4180 wpa_s->p2p_fallback_to_go_neg = 0;
4181 wpa_s->pending_pd_use = NORMAL_PD;
4182 if (os_strncmp(config_method, "display", 7) == 0)
4183 config_methods = WPS_CONFIG_DISPLAY;
4184 else if (os_strncmp(config_method, "keypad", 6) == 0)
4185 config_methods = WPS_CONFIG_KEYPAD;
4186 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4187 os_strncmp(config_method, "pushbutton", 10) == 0)
4188 config_methods = WPS_CONFIG_PUSHBUTTON;
4190 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
4194 if (use == WPAS_P2P_PD_AUTO) {
4195 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4196 wpa_s->pending_pd_config_methods = config_methods;
4197 wpa_s->p2p_auto_pd = 1;
4198 wpa_s->p2p_auto_join = 0;
4199 wpa_s->pending_pd_before_join = 0;
4200 wpa_s->auto_pd_scan_retry = 0;
4201 wpas_p2p_stop_find(wpa_s);
4202 wpa_s->p2p_join_scan_count = 0;
4203 os_get_time(&wpa_s->p2p_auto_started);
4204 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4205 wpa_s->p2p_auto_started.sec,
4206 wpa_s->p2p_auto_started.usec);
4207 wpas_p2p_join_scan(wpa_s, NULL);
4211 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4212 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
4214 use == WPAS_P2P_PD_FOR_JOIN);
4217 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4220 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
4221 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4226 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4229 return p2p_scan_result_text(ies, ies_len, buf, end);
4233 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4235 if (!wpa_s->pending_action_tx)
4238 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4239 "operation request");
4240 wpabuf_free(wpa_s->pending_action_tx);
4241 wpa_s->pending_action_tx = NULL;
4245 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4246 enum p2p_discovery_type type,
4247 unsigned int num_req_dev_types, const u8 *req_dev_types,
4248 const u8 *dev_id, unsigned int search_delay)
4250 wpas_p2p_clear_pending_action_tx(wpa_s);
4251 wpa_s->p2p_long_listen = 0;
4253 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4254 return wpa_drv_p2p_find(wpa_s, timeout, type);
4256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4257 wpa_s->p2p_in_provisioning)
4260 wpa_supplicant_cancel_sched_scan(wpa_s);
4262 return p2p_find(wpa_s->global->p2p, timeout, type,
4263 num_req_dev_types, req_dev_types, dev_id,
4268 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4270 wpas_p2p_clear_pending_action_tx(wpa_s);
4271 wpa_s->p2p_long_listen = 0;
4272 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4273 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4274 wpa_s->p2p_cb_on_scan_complete = 0;
4276 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4277 wpa_drv_p2p_stop_find(wpa_s);
4281 if (wpa_s->global->p2p)
4282 p2p_stop_find(wpa_s->global->p2p);
4284 wpas_p2p_remove_pending_group_interface(wpa_s);
4288 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4290 struct wpa_supplicant *wpa_s = eloop_ctx;
4291 wpa_s->p2p_long_listen = 0;
4295 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4299 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4302 wpa_supplicant_cancel_sched_scan(wpa_s);
4303 wpas_p2p_clear_pending_action_tx(wpa_s);
4307 * This is a request for unlimited Listen state. However, at
4308 * least for now, this is mapped to a Listen state for one
4313 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4314 wpa_s->p2p_long_listen = 0;
4317 * Stop previous find/listen operation to avoid trying to request a new
4318 * remain-on-channel operation while the driver is still running the
4321 if (wpa_s->global->p2p)
4322 p2p_stop_find(wpa_s->global->p2p);
4324 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4325 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4326 wpa_s->p2p_long_listen = timeout * 1000;
4327 eloop_register_timeout(timeout, 0,
4328 wpas_p2p_long_listen_timeout,
4336 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4337 u8 *buf, size_t len, int p2p_group)
4339 struct wpabuf *p2p_ie;
4342 if (wpa_s->global->p2p_disabled)
4344 if (wpa_s->global->p2p == NULL)
4349 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4350 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4352 wpabuf_free(p2p_ie);
4358 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4359 const u8 *dst, const u8 *bssid,
4360 const u8 *ie, size_t ie_len, int ssi_signal)
4362 if (wpa_s->global->p2p_disabled)
4364 if (wpa_s->global->p2p == NULL)
4367 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4369 case P2P_PREQ_NOT_P2P:
4370 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4373 case P2P_PREQ_MALFORMED:
4374 case P2P_PREQ_NOT_LISTEN:
4375 case P2P_PREQ_NOT_PROCESSED:
4376 default: /* make gcc happy */
4378 case P2P_PREQ_PROCESSED:
4384 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4385 const u8 *sa, const u8 *bssid,
4386 u8 category, const u8 *data, size_t len, int freq)
4388 if (wpa_s->global->p2p_disabled)
4390 if (wpa_s->global->p2p == NULL)
4393 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4398 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4400 if (wpa_s->global->p2p_disabled)
4402 if (wpa_s->global->p2p == NULL)
4405 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4409 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4411 p2p_group_deinit(wpa_s->p2p_group);
4412 wpa_s->p2p_group = NULL;
4414 wpa_s->ap_configured_cb = NULL;
4415 wpa_s->ap_configured_cb_ctx = NULL;
4416 wpa_s->ap_configured_cb_data = NULL;
4417 wpa_s->connect_without_scan = NULL;
4421 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4423 wpa_s->p2p_long_listen = 0;
4425 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4426 return wpa_drv_p2p_reject(wpa_s, addr);
4428 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4431 return p2p_reject(wpa_s->global->p2p, addr);
4435 /* Invite to reinvoke a persistent group */
4436 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4437 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4440 enum p2p_invite_role role;
4443 wpa_s->p2p_persistent_go_freq = freq;
4444 wpa_s->p2p_go_ht40 = !!ht40;
4445 if (ssid->mode == WPAS_MODE_P2P_GO) {
4446 role = P2P_INVITE_ROLE_GO;
4447 if (peer_addr == NULL) {
4448 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4449 "address in invitation command");
4452 if (wpas_p2p_create_iface(wpa_s)) {
4453 if (wpas_p2p_add_group_interface(wpa_s,
4454 WPA_IF_P2P_GO) < 0) {
4455 wpa_printf(MSG_ERROR, "P2P: Failed to "
4456 "allocate a new interface for the "
4460 bssid = wpa_s->pending_interface_addr;
4462 bssid = wpa_s->own_addr;
4464 role = P2P_INVITE_ROLE_CLIENT;
4465 peer_addr = ssid->bssid;
4467 wpa_s->pending_invite_ssid_id = ssid->id;
4469 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4470 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4471 ssid->ssid, ssid->ssid_len,
4474 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4477 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4478 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4482 /* Invite to join an active group */
4483 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4484 const u8 *peer_addr, const u8 *go_dev_addr)
4486 struct wpa_global *global = wpa_s->global;
4487 enum p2p_invite_role role;
4489 struct wpa_ssid *ssid;
4492 wpa_s->p2p_persistent_go_freq = 0;
4493 wpa_s->p2p_go_ht40 = 0;
4495 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4496 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4499 if (wpa_s == NULL) {
4500 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4504 ssid = wpa_s->current_ssid;
4506 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4511 persistent = ssid->p2p_persistent_group &&
4512 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4513 ssid->ssid, ssid->ssid_len);
4515 if (ssid->mode == WPAS_MODE_P2P_GO) {
4516 role = P2P_INVITE_ROLE_ACTIVE_GO;
4517 bssid = wpa_s->own_addr;
4518 if (go_dev_addr == NULL)
4519 go_dev_addr = wpa_s->global->p2p_dev_addr;
4521 role = P2P_INVITE_ROLE_CLIENT;
4522 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4523 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4524 "invite to current group");
4527 bssid = wpa_s->bssid;
4528 if (go_dev_addr == NULL &&
4529 !is_zero_ether_addr(wpa_s->go_dev_addr))
4530 go_dev_addr = wpa_s->go_dev_addr;
4532 wpa_s->parent->pending_invite_ssid_id = -1;
4534 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4535 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4536 ssid->ssid, ssid->ssid_len,
4537 go_dev_addr, persistent);
4539 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4542 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4543 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
4544 go_dev_addr, persistent);
4548 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4550 struct wpa_ssid *ssid = wpa_s->current_ssid;
4551 const char *ssid_txt;
4552 u8 go_dev_addr[ETH_ALEN];
4553 int network_id = -1;
4557 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4558 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4559 wpa_s->parent, NULL);
4562 if (!wpa_s->show_group_started || !ssid)
4565 wpa_s->show_group_started = 0;
4567 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4568 os_memset(go_dev_addr, 0, ETH_ALEN);
4569 if (ssid->bssid_set)
4570 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4571 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4573 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4575 if (wpa_s->global->p2p_group_formation == wpa_s)
4576 wpa_s->global->p2p_group_formation = NULL;
4578 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4579 (int) wpa_s->assoc_freq;
4580 if (ssid->passphrase == NULL && ssid->psk_set) {
4582 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4583 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4584 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4586 wpa_s->ifname, ssid_txt, freq, psk,
4587 MAC2STR(go_dev_addr),
4588 persistent ? " [PERSISTENT]" : "");
4590 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4591 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4592 "go_dev_addr=" MACSTR "%s",
4593 wpa_s->ifname, ssid_txt, freq,
4594 ssid->passphrase ? ssid->passphrase : "",
4595 MAC2STR(go_dev_addr),
4596 persistent ? " [PERSISTENT]" : "");
4600 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4603 network_id = ssid->id;
4604 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4607 if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4608 wpa_s->global->p2p != NULL) {
4609 wpa_s->p2p_cb_on_scan_complete = 0;
4610 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4611 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4612 "continued after successful connection");
4613 p2p_increase_search_delay(
4615 wpas_p2p_search_delay(wpa_s));
4621 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4622 u32 interval1, u32 duration2, u32 interval2)
4624 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4626 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4629 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4630 wpa_s->current_ssid == NULL ||
4631 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4634 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4635 wpa_s->own_addr, wpa_s->assoc_freq,
4636 duration1, interval1, duration2, interval2);
4640 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4641 unsigned int interval)
4643 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4646 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4649 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4653 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4655 return wpa_s->current_ssid != NULL &&
4656 wpa_s->current_ssid->p2p_group &&
4657 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4661 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4663 struct wpa_supplicant *wpa_s = eloop_ctx;
4665 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4666 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4671 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4673 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
4677 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4681 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4682 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4684 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4687 timeout = wpa_s->conf->p2p_group_idle;
4688 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4689 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4690 timeout = P2P_MAX_CLIENT_IDLE;
4696 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4697 timeout = 0; /* special client mode no-timeout */
4702 if (wpa_s->p2p_in_provisioning) {
4704 * Use the normal group formation timeout during the
4705 * provisioning phase to avoid terminating this process too
4706 * early due to group idle timeout.
4708 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4709 "during provisioning");
4713 if (wpa_s->show_group_started) {
4715 * Use the normal group formation timeout between the end of
4716 * the provisioning phase and completion of 4-way handshake to
4717 * avoid terminating this process too early due to group idle
4720 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4721 "while waiting for initial 4-way handshake to "
4727 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4729 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4734 /* Returns 1 if the interface was removed */
4735 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4736 u16 reason_code, const u8 *ie, size_t ie_len,
4737 int locally_generated)
4739 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4741 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4744 if (!locally_generated)
4745 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4748 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4749 wpa_s->current_ssid &&
4750 wpa_s->current_ssid->p2p_group &&
4751 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4752 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4753 "session is ending");
4754 if (wpas_p2p_group_delete(wpa_s,
4755 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4764 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4765 u16 reason_code, const u8 *ie, size_t ie_len,
4766 int locally_generated)
4768 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4770 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4773 if (!locally_generated)
4774 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4779 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4781 struct p2p_data *p2p = wpa_s->global->p2p;
4786 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4789 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4790 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4792 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4793 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4796 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4797 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4799 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4800 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4802 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4803 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4804 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4805 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4806 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4809 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4810 p2p_set_sec_dev_types(p2p,
4811 (void *) wpa_s->conf->sec_device_type,
4812 wpa_s->conf->num_sec_device_types);
4814 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4816 p2p_remove_wps_vendor_extensions(p2p);
4817 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4818 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4820 p2p_add_wps_vendor_extension(
4821 p2p, wpa_s->conf->wps_vendor_ext[i]);
4825 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4826 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4828 country[0] = wpa_s->conf->country[0];
4829 country[1] = wpa_s->conf->country[1];
4831 p2p_set_country(p2p, country);
4834 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4835 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4836 wpa_s->conf->p2p_ssid_postfix ?
4837 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4841 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4842 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4844 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4845 u8 reg_class, channel;
4848 if (wpa_s->conf->p2p_listen_reg_class &&
4849 wpa_s->conf->p2p_listen_channel) {
4850 reg_class = wpa_s->conf->p2p_listen_reg_class;
4851 channel = wpa_s->conf->p2p_listen_channel;
4855 * Pick one of the social channels randomly as the
4858 os_get_random((u8 *) &r, sizeof(r));
4859 channel = 1 + (r % 3) * 5;
4861 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4863 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4866 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4867 u8 op_reg_class, op_channel, cfg_op_channel;
4870 if (wpa_s->conf->p2p_oper_reg_class &&
4871 wpa_s->conf->p2p_oper_channel) {
4872 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4873 op_channel = wpa_s->conf->p2p_oper_channel;
4878 * Use random operation channel from (1, 6, 11)
4879 *if no other preference is indicated.
4881 os_get_random((u8 *) &r, sizeof(r));
4882 op_channel = 1 + (r % 3) * 5;
4885 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4888 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4892 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4893 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4894 wpa_s->conf->p2p_pref_chan) < 0) {
4895 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4902 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4905 if (!wpa_s->ap_iface)
4907 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4912 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4914 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4916 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4919 wpa_s->global->cross_connection = enabled;
4920 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4923 struct wpa_supplicant *iface;
4925 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4927 if (iface->cross_connect_enabled == 0)
4930 iface->cross_connect_enabled = 0;
4931 iface->cross_connect_in_use = 0;
4932 wpa_msg(iface->parent, MSG_INFO,
4933 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4934 iface->ifname, iface->cross_connect_uplink);
4942 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4944 struct wpa_supplicant *iface;
4946 if (!uplink->global->cross_connection)
4949 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4950 if (!iface->cross_connect_enabled)
4952 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4955 if (iface->ap_iface == NULL)
4957 if (iface->cross_connect_in_use)
4960 iface->cross_connect_in_use = 1;
4961 wpa_msg(iface->parent, MSG_INFO,
4962 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4963 iface->ifname, iface->cross_connect_uplink);
4968 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4970 struct wpa_supplicant *iface;
4972 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4973 if (!iface->cross_connect_enabled)
4975 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4978 if (!iface->cross_connect_in_use)
4981 wpa_msg(iface->parent, MSG_INFO,
4982 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4983 iface->ifname, iface->cross_connect_uplink);
4984 iface->cross_connect_in_use = 0;
4989 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4991 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4992 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4993 wpa_s->cross_connect_disallowed)
4994 wpas_p2p_disable_cross_connect(wpa_s);
4996 wpas_p2p_enable_cross_connect(wpa_s);
4997 if (!wpa_s->ap_iface &&
4998 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4999 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5003 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5005 wpas_p2p_disable_cross_connect(wpa_s);
5006 if (!wpa_s->ap_iface &&
5007 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5009 wpas_p2p_set_group_idle_timeout(wpa_s);
5013 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5015 struct wpa_supplicant *iface;
5017 if (!wpa_s->global->cross_connection)
5020 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5023 if (iface->drv_flags &
5024 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5026 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5029 wpa_s->cross_connect_enabled = 1;
5030 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5031 sizeof(wpa_s->cross_connect_uplink));
5032 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5033 "%s to %s whenever uplink is available",
5034 wpa_s->ifname, wpa_s->cross_connect_uplink);
5036 if (iface->ap_iface || iface->current_ssid == NULL ||
5037 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5038 iface->cross_connect_disallowed ||
5039 iface->wpa_state != WPA_COMPLETED)
5042 wpa_s->cross_connect_in_use = 1;
5043 wpa_msg(wpa_s->parent, MSG_INFO,
5044 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5045 wpa_s->ifname, wpa_s->cross_connect_uplink);
5051 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5053 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5054 !wpa_s->p2p_in_provisioning)
5055 return 0; /* not P2P client operation */
5057 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5059 if (wpa_s != wpa_s->parent)
5060 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5062 if (wpa_s->global->p2p)
5063 p2p_group_formation_failed(wpa_s->global->p2p);
5065 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5066 wpa_s->parent, NULL);
5068 wpas_group_formation_completed(wpa_s, 0);
5073 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5075 struct p2p_channels chan;
5077 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5080 os_memset(&chan, 0, sizeof(chan));
5081 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5082 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5087 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5091 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5093 struct wpa_global *global = wpa_s->global;
5097 if (global->p2p == NULL)
5100 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5102 if (wpa_s->pending_interface_name[0] &&
5103 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5106 peer = p2p_get_go_neg_peer(global->p2p);
5108 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5109 MACSTR, MAC2STR(peer));
5110 p2p_unauthorize(global->p2p, peer);
5114 wpas_p2p_stop_find(wpa_s);
5116 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5117 if (wpa_s == global->p2p_group_formation &&
5118 (wpa_s->p2p_in_provisioning ||
5119 wpa_s->parent->pending_interface_type ==
5120 WPA_IF_P2P_CLIENT)) {
5121 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5122 "formation found - cancelling",
5125 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5126 wpa_s->parent, NULL);
5127 wpas_p2p_group_delete(wpa_s,
5128 P2P_GROUP_REMOVAL_REQUESTED);
5134 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5142 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5144 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5147 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5148 "being available anymore");
5149 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
5153 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5154 int freq_24, int freq_5, int freq_overall)
5156 struct p2p_data *p2p = wpa_s->global->p2p;
5157 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5159 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5163 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5166 struct p2p_data *p2p = wpa_s->global->p2p;
5168 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5171 if (hwaddr_aton(addr, peer))
5174 return p2p_unauthorize(p2p, peer);
5179 * wpas_p2p_disconnect - Disconnect from a P2P Group
5180 * @wpa_s: Pointer to wpa_supplicant data
5181 * Returns: 0 on success, -1 on failure
5183 * This can be used to disconnect from a group in which the local end is a P2P
5184 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5185 * virtual network interface was created for this group, that interface will be
5186 * removed. Otherwise, only the configured P2P group network will be removed
5187 * from the interface.
5189 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5195 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5200 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5203 struct wpa_supplicant *group = wpa_s;
5206 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5210 while (group && (group->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)) {
5211 if(group->wpa_state == WPA_ASSOCIATED) {
5212 /* WPA_ASSOCIATED hasn't moved to WPA_COMPLETED. So it could be in WPS
5213 * or 4Way Hanshake phase. Avoid allowing scan during this time critical
5216 wpa_printf(MSG_ERROR, "P2P: WPS/4way handshake in Progress."
5220 group = group->next;
5224 return p2p_in_progress(wpa_s->global->p2p);
5228 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5229 struct wpa_ssid *ssid)
5231 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5232 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5233 wpa_s->parent, NULL) > 0) {
5235 * Remove the network by scheduling the group formation
5236 * timeout to happen immediately. The teardown code
5237 * needs to be scheduled to run asynch later so that we
5238 * don't delete data from under ourselves unexpectedly.
5239 * Calling wpas_p2p_group_formation_timeout directly
5240 * causes a series of crashes in WPS failure scenarios.
5242 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5243 "P2P group network getting removed");
5244 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5245 wpa_s->parent, NULL);
5250 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
5251 const u8 *addr, const u8 *ssid,
5257 for (s = wpa_s->conf->ssid; s; s = s->next) {
5258 if (s->disabled != 2)
5261 (ssid_len != s->ssid_len ||
5262 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5264 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5265 return s; /* peer is GO in the persistent group */
5266 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5268 for (i = 0; i < s->num_p2p_clients; i++) {
5269 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5270 addr, ETH_ALEN) == 0)
5271 return s; /* peer is P2P client in persistent
5280 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5285 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5289 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5292 struct wpa_supplicant *group = wpa_s;
5293 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5294 if (wpa_s->global->p2p_group_formation)
5295 group = wpa_s->global->p2p_group_formation;
5296 wpa_s = wpa_s->parent;
5297 offchannel_send_action_done(wpa_s);
5299 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
5300 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5301 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5302 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5303 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5304 wpa_s->p2p_persistent_id,
5305 wpa_s->p2p_pd_before_go_neg,
5306 wpa_s->p2p_go_ht40);
5310 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5312 if (!wpa_s->p2p_fallback_to_go_neg ||
5313 wpa_s->p2p_in_provisioning <= 5)
5316 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5317 return 0; /* peer operating as a GO */
5319 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5320 "fallback to GO Negotiation");
5321 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5327 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5329 const char *rn, *rn2;
5330 struct wpa_supplicant *ifs;
5332 if (wpa_s->wpa_state > WPA_SCANNING) {
5333 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5334 "concurrent operation",
5335 P2P_CONCURRENT_SEARCH_DELAY);
5336 return P2P_CONCURRENT_SEARCH_DELAY;
5339 if (!wpa_s->driver->get_radio_name)
5341 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5342 if (rn == NULL || rn[0] == '\0')
5345 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5346 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5349 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5350 if (!rn2 || os_strcmp(rn, rn2) != 0)
5352 if (ifs->wpa_state > WPA_SCANNING) {
5353 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5354 "delay due to concurrent operation on "
5356 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5357 return P2P_CONCURRENT_SEARCH_DELAY;
5365 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
5367 struct wpa_supplicant *iface = NULL;
5368 struct p2p_data *p2p = wpa_s->global->p2p;
5370 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5371 if((iface->p2p_group_interface) && (iface->current_ssid) &&
5372 (iface->current_ssid->frequency != freq)) {
5374 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
5375 /* Try to see whether we can move the GO. If it
5376 * is not possible, remove the GO interface
5378 if(wpa_drv_switch_channel(iface, freq) == 0) {
5379 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5380 iface->current_ssid->frequency = freq;
5385 /* If GO cannot be moved or if the conflicting interface is a
5386 * P2P Client, remove the interface depending up on the connection
5388 if(!wpas_is_p2p_prioritized(wpa_s)) {
5389 /* STA connection has priority over existing
5390 * P2P connection. So remove the interface */
5391 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5392 "concurrent mode frequency conflict");
5393 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
5395 /* Existing connection has the priority. Disable the newly
5396 * selected network and let the application know about it.