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
65 #define P2P_CONCURRENT_SEARCH_DELAY 500
66 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
68 enum p2p_group_removal_reason {
69 P2P_GROUP_REMOVAL_UNKNOWN,
70 P2P_GROUP_REMOVAL_SILENT,
71 P2P_GROUP_REMOVAL_FORMATION_FAILED,
72 P2P_GROUP_REMOVAL_REQUESTED,
73 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
74 P2P_GROUP_REMOVAL_UNAVAILABLE,
75 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
77 P2P_GROUP_REMOVAL_FREQ_CONFLICT
82 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
83 static struct wpa_supplicant *
84 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
86 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
87 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
88 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
89 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
90 const u8 *dev_addr, enum p2p_wps_method wps_method,
92 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
93 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
94 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
95 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
96 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
98 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
101 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
102 struct wpa_scan_results *scan_res)
106 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
109 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
110 (int) scan_res->num);
112 for (i = 0; i < scan_res->num; i++) {
113 struct wpa_scan_res *bss = scan_res->res[i];
114 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
115 bss->freq, bss->age, bss->level,
116 (const u8 *) (bss + 1),
121 p2p_scan_res_handled(wpa_s->global->p2p);
125 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
126 unsigned int num_req_dev_types,
127 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
129 struct wpa_supplicant *wpa_s = ctx;
130 struct wpa_supplicant *ifs;
131 struct wpa_driver_scan_params params;
133 struct wpabuf *wps_ie, *ies;
134 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
137 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
140 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
141 if (ifs->sta_scan_pending &&
142 wpas_p2p_in_progress(wpa_s) == 2) {
143 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
144 "pending station mode scan to be "
145 "completed on interface %s", ifs->ifname);
146 wpa_s->global->p2p_cb_on_scan_complete = 1;
147 wpa_supplicant_req_scan(ifs, 0, 0);
152 os_memset(¶ms, 0, sizeof(params));
154 /* P2P Wildcard SSID */
155 params.num_ssids = 1;
156 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
157 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
159 wpa_s->wps->dev.p2p = 1;
160 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
161 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
162 num_req_dev_types, req_dev_types);
166 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
167 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
172 wpabuf_put_buf(ies, wps_ie);
175 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
177 params.p2p_probe = 1;
178 params.extra_ies = wpabuf_head(ies);
179 params.extra_ies_len = wpabuf_len(ies);
182 case P2P_SCAN_SOCIAL:
183 params.freqs = social_channels;
187 case P2P_SCAN_SPECIFIC:
188 social_channels[0] = freq;
189 social_channels[1] = 0;
190 params.freqs = social_channels;
192 case P2P_SCAN_SOCIAL_PLUS_ONE:
193 social_channels[3] = freq;
194 params.freqs = social_channels;
198 ret = wpa_drv_scan(wpa_s, ¶ms);
203 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
205 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
206 wpa_s->global->p2p_cb_on_scan_complete = 1;
212 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
218 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
220 switch (p2p_group_interface) {
221 case P2P_GROUP_INTERFACE_PENDING:
222 return WPA_IF_P2P_GROUP;
223 case P2P_GROUP_INTERFACE_GO:
224 return WPA_IF_P2P_GO;
225 case P2P_GROUP_INTERFACE_CLIENT:
226 return WPA_IF_P2P_CLIENT;
229 return WPA_IF_P2P_GROUP;
233 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
235 size_t ssid_len, int *go)
239 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
240 for (s = wpa_s->conf->ssid; s; s = s->next) {
241 if (s->disabled != 0 || !s->p2p_group ||
242 s->ssid_len != ssid_len ||
243 os_memcmp(ssid, s->ssid, ssid_len) != 0)
245 if (s->mode == WPAS_MODE_P2P_GO &&
246 s != wpa_s->current_ssid)
249 *go = s->mode == WPAS_MODE_P2P_GO;
258 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
259 enum p2p_group_removal_reason removal_reason)
261 struct wpa_ssid *ssid;
265 ssid = wpa_s->current_ssid;
268 * The current SSID was not known, but there may still be a
269 * pending P2P group interface waiting for provisioning or a
270 * P2P group that is trying to reconnect.
272 ssid = wpa_s->conf->ssid;
274 if (ssid->p2p_group && ssid->disabled != 2)
279 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
281 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
286 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
288 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
289 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
290 wpa_s->reassociate = 0;
291 wpa_s->disconnected = 1;
292 wpa_supplicant_deauthenticate(wpa_s,
293 WLAN_REASON_DEAUTH_LEAVING);
297 if (wpa_s->cross_connect_in_use) {
298 wpa_s->cross_connect_in_use = 0;
299 wpa_msg(wpa_s->parent, MSG_INFO,
300 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
301 wpa_s->ifname, wpa_s->cross_connect_uplink);
303 switch (removal_reason) {
304 case P2P_GROUP_REMOVAL_REQUESTED:
305 reason = " reason=REQUESTED";
307 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
308 reason = " reason=FORMATION_FAILED";
310 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
311 reason = " reason=IDLE";
313 case P2P_GROUP_REMOVAL_UNAVAILABLE:
314 reason = " reason=UNAVAILABLE";
316 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
317 reason = " reason=GO_ENDING_SESSION";
320 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
321 reason = " reason=FREQ_CONFLICT";
328 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
329 wpa_msg(wpa_s->parent, MSG_INFO,
330 P2P_EVENT_GROUP_REMOVED "%s %s%s",
331 wpa_s->ifname, gtype, reason);
334 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
335 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
337 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
338 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
340 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
341 struct wpa_global *global;
343 enum wpa_driver_if_type type;
344 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
346 global = wpa_s->global;
347 ifname = os_strdup(wpa_s->ifname);
348 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
349 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
350 wpa_s = global->ifaces;
352 wpa_drv_if_remove(wpa_s, type, ifname);
357 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
358 if (ssid && (ssid->p2p_group ||
359 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
360 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
362 if (ssid == wpa_s->current_ssid) {
363 wpa_sm_set_config(wpa_s->wpa, NULL);
364 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
365 wpa_s->current_ssid = NULL;
368 * Networks objects created during any P2P activities are not
369 * exposed out as they might/will confuse certain non-P2P aware
370 * applications since these network objects won't behave like
373 * Likewise, we don't send out network removed signals for such
376 wpa_config_remove_network(wpa_s->conf, id);
377 wpa_supplicant_clear_status(wpa_s);
378 wpa_supplicant_cancel_sched_scan(wpa_s);
379 wpa_s->sta_scan_pending = 0;
381 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
385 wpa_supplicant_ap_deinit(wpa_s);
387 wpa_drv_deinit_p2p_cli(wpa_s);
393 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
395 const u8 *ssid, size_t ssid_len)
403 if (wpa_s->go_params)
404 bssid = wpa_s->go_params->peer_interface_addr;
406 bssid = wpa_s->bssid;
408 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
410 u8 iface_addr[ETH_ALEN];
411 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
413 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
416 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
417 "group is persistent - BSS " MACSTR " not found",
422 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
424 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
425 "group is persistent - BSS " MACSTR
426 " did not include P2P IE", MAC2STR(bssid));
427 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
428 (u8 *) (bss + 1), bss->ie_len);
429 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
430 ((u8 *) bss + 1) + bss->ie_len,
435 group_capab = p2p_get_group_capab(p2p);
436 addr = p2p_get_go_dev_addr(p2p);
437 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
438 "group_capab=0x%x", group_capab);
440 os_memcpy(go_dev_addr, addr, ETH_ALEN);
441 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
444 os_memset(go_dev_addr, 0, ETH_ALEN);
447 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
448 "go_dev_addr=" MACSTR,
449 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
451 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
455 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
456 struct wpa_ssid *ssid,
457 const u8 *go_dev_addr)
462 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
463 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
464 for (s = wpa_s->conf->ssid; s; s = s->next) {
465 if (s->disabled == 2 &&
466 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
467 s->ssid_len == ssid->ssid_len &&
468 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
473 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
475 if (ssid->passphrase && !s->passphrase)
477 else if (ssid->passphrase && s->passphrase &&
478 os_strcmp(ssid->passphrase, s->passphrase) != 0)
481 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
484 s = wpa_config_add_network(wpa_s->conf);
489 * Instead of network_added we emit persistent_group_added
490 * notification. Also to keep the defense checks in
491 * persistent_group obj registration method, we set the
492 * relevant flags in s to designate it as a persistent group.
495 s->p2p_persistent_group = 1;
496 wpas_notify_persistent_group_added(wpa_s, s);
497 wpa_config_set_network_defaults(s);
501 s->p2p_persistent_group = 1;
504 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
505 s->mode = ssid->mode;
506 s->auth_alg = WPA_AUTH_ALG_OPEN;
507 s->key_mgmt = WPA_KEY_MGMT_PSK;
508 s->proto = WPA_PROTO_RSN;
509 s->pairwise_cipher = WPA_CIPHER_CCMP;
511 if (ssid->passphrase) {
512 os_free(s->passphrase);
513 s->passphrase = os_strdup(ssid->passphrase);
517 os_memcpy(s->psk, ssid->psk, 32);
519 if (s->passphrase && !s->psk_set)
520 wpa_config_update_psk(s);
521 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
523 s->ssid = os_malloc(ssid->ssid_len);
526 s->ssid_len = ssid->ssid_len;
527 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
530 #ifndef CONFIG_NO_CONFIG_WRITE
531 if (changed && wpa_s->conf->update_config &&
532 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
533 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
535 #endif /* CONFIG_NO_CONFIG_WRITE */
541 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
544 struct wpa_ssid *ssid, *s;
549 ssid = wpa_s->current_ssid;
550 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
551 !ssid->p2p_persistent_group)
554 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
555 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
558 if (s->ssid_len == ssid->ssid_len &&
559 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
566 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
567 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
571 if (i == s->num_p2p_clients - 1)
572 return; /* already the most recent entry */
574 /* move the entry to mark it most recent */
575 os_memmove(s->p2p_client_list + i * ETH_ALEN,
576 s->p2p_client_list + (i + 1) * ETH_ALEN,
577 (s->num_p2p_clients - i - 1) * ETH_ALEN);
578 os_memcpy(s->p2p_client_list +
579 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
584 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
585 n = os_realloc_array(s->p2p_client_list,
586 s->num_p2p_clients + 1, ETH_ALEN);
589 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
590 s->p2p_client_list = n;
591 s->num_p2p_clients++;
593 /* Not enough room for an additional entry - drop the oldest
595 os_memmove(s->p2p_client_list,
596 s->p2p_client_list + ETH_ALEN,
597 (s->num_p2p_clients - 1) * ETH_ALEN);
598 os_memcpy(s->p2p_client_list +
599 (s->num_p2p_clients - 1) * ETH_ALEN,
603 #ifndef CONFIG_NO_CONFIG_WRITE
604 if (wpa_s->parent->conf->update_config &&
605 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
606 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
607 #endif /* CONFIG_NO_CONFIG_WRITE */
611 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
614 struct wpa_ssid *ssid;
615 const char *ssid_txt;
618 u8 go_dev_addr[ETH_ALEN];
622 * This callback is likely called for the main interface. Update wpa_s
623 * to use the group interface if a new interface was created for the
626 if (wpa_s->global->p2p_group_formation)
627 wpa_s = wpa_s->global->p2p_group_formation;
628 wpa_s->global->p2p_group_formation = NULL;
629 wpa_s->p2p_in_provisioning = 0;
632 wpa_msg(wpa_s->parent, MSG_INFO,
633 P2P_EVENT_GROUP_FORMATION_FAILURE);
634 wpas_p2p_group_delete(wpa_s,
635 P2P_GROUP_REMOVAL_FORMATION_FAILED);
639 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
641 ssid = wpa_s->current_ssid;
642 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
643 ssid->mode = WPAS_MODE_P2P_GO;
644 p2p_group_notif_formation_done(wpa_s->p2p_group);
645 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
650 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
651 client = ssid->mode == WPAS_MODE_INFRA;
652 if (ssid->mode == WPAS_MODE_P2P_GO) {
653 persistent = ssid->p2p_persistent_group;
654 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
657 persistent = wpas_p2p_persistent_group(wpa_s,
663 client = wpa_s->p2p_group_interface ==
664 P2P_GROUP_INTERFACE_CLIENT;
665 os_memset(go_dev_addr, 0, ETH_ALEN);
668 wpa_s->show_group_started = 0;
671 * Indicate event only after successfully completed 4-way
672 * handshake, i.e., when the interface is ready for data
675 wpa_s->show_group_started = 1;
677 /* For client Second phase of Group formation (4-way handshake) can be still pending
678 * So we need to restore wpa_s->global->p2p_group_formation */
679 wpa_printf(MSG_INFO, "Restoring back wpa_s->global->p2p_group_formation to wpa_s %p\n", wpa_s);
680 wpa_s->global->p2p_group_formation = wpa_s;
683 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
685 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
686 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
687 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
689 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
690 MAC2STR(go_dev_addr),
691 persistent ? " [PERSISTENT]" : "");
692 wpas_p2p_cross_connect_setup(wpa_s);
693 wpas_p2p_set_group_idle_timeout(wpa_s);
695 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
696 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
697 "go_dev_addr=" MACSTR "%s",
698 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
699 ssid && ssid->passphrase ? ssid->passphrase : "",
700 MAC2STR(go_dev_addr),
701 persistent ? " [PERSISTENT]" : "");
702 wpas_p2p_cross_connect_setup(wpa_s);
703 wpas_p2p_set_group_idle_timeout(wpa_s);
707 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
709 if (network_id < 0 && ssid)
710 network_id = ssid->id;
712 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
716 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
718 const u8 *dst, const u8 *src,
720 const u8 *data, size_t data_len,
721 enum offchannel_send_action_result
724 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
726 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
728 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
732 case OFFCHANNEL_SEND_ACTION_SUCCESS:
733 res = P2P_SEND_ACTION_SUCCESS;
735 case OFFCHANNEL_SEND_ACTION_NO_ACK:
736 res = P2P_SEND_ACTION_NO_ACK;
738 case OFFCHANNEL_SEND_ACTION_FAILED:
739 res = P2P_SEND_ACTION_FAILED;
743 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
745 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
746 wpa_s->pending_pd_before_join &&
747 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
748 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
749 wpa_s->p2p_fallback_to_go_neg) {
750 wpa_s->pending_pd_before_join = 0;
751 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
752 "during p2p_connect-auto");
753 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
759 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
760 const u8 *src, const u8 *bssid, const u8 *buf,
761 size_t len, unsigned int wait_time)
763 struct wpa_supplicant *wpa_s = ctx;
764 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
766 wpas_p2p_send_action_tx_status, 1);
770 static void wpas_send_action_done(void *ctx)
772 struct wpa_supplicant *wpa_s = ctx;
773 offchannel_send_action_done(wpa_s);
777 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
778 struct p2p_go_neg_results *params)
780 if (wpa_s->go_params == NULL) {
781 wpa_s->go_params = os_malloc(sizeof(*params));
782 if (wpa_s->go_params == NULL)
785 os_memcpy(wpa_s->go_params, params, sizeof(*params));
790 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
791 struct p2p_go_neg_results *res)
793 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
794 MAC2STR(res->peer_interface_addr));
795 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
796 res->ssid, res->ssid_len);
797 wpa_supplicant_ap_deinit(wpa_s);
798 wpas_copy_go_neg_results(wpa_s, res);
799 if (res->wps_method == WPS_PBC)
800 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
802 u16 dev_pw_id = DEV_PW_DEFAULT;
803 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
804 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
805 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
806 wpa_s->p2p_pin, 1, dev_pw_id);
811 static void p2p_go_configured(void *ctx, void *data)
813 struct wpa_supplicant *wpa_s = ctx;
814 struct p2p_go_neg_results *params = data;
815 struct wpa_ssid *ssid;
818 ssid = wpa_s->current_ssid;
819 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
820 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
821 if (wpa_s->global->p2p_group_formation == wpa_s)
822 wpa_s->global->p2p_group_formation = NULL;
823 if (os_strlen(params->passphrase) > 0) {
824 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
825 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
826 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
827 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
828 ssid->frequency, params->passphrase,
829 MAC2STR(wpa_s->global->p2p_dev_addr),
830 params->persistent_group ? " [PERSISTENT]" :
834 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
835 sizeof(params->psk));
836 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
837 "%s GO ssid=\"%s\" freq=%d psk=%s "
838 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
839 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
840 ssid->frequency, psk,
841 MAC2STR(wpa_s->global->p2p_dev_addr),
842 params->persistent_group ? " [PERSISTENT]" :
846 if (params->persistent_group)
847 network_id = wpas_p2p_store_persistent_group(
849 wpa_s->global->p2p_dev_addr);
851 network_id = ssid->id;
852 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
853 wpas_p2p_cross_connect_setup(wpa_s);
854 wpas_p2p_set_group_idle_timeout(wpa_s);
858 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
859 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
860 params->peer_interface_addr)) {
861 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
865 if (params->wps_method == WPS_PBC)
866 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
867 params->peer_device_addr);
868 else if (wpa_s->p2p_pin[0])
869 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
870 wpa_s->p2p_pin, NULL, 0, 0);
871 os_free(wpa_s->go_params);
872 wpa_s->go_params = NULL;
876 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
877 struct p2p_go_neg_results *params,
880 struct wpa_ssid *ssid;
882 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
883 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
884 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
889 ssid = wpa_config_add_network(wpa_s->conf);
891 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
895 wpa_s->show_group_started = 0;
897 wpa_config_set_network_defaults(ssid);
900 ssid->p2p_persistent_group = params->persistent_group;
901 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
903 ssid->frequency = params->freq;
904 ssid->ht40 = params->ht40;
905 ssid->ssid = os_zalloc(params->ssid_len + 1);
907 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
908 ssid->ssid_len = params->ssid_len;
910 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
911 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
912 ssid->proto = WPA_PROTO_RSN;
913 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
914 if (os_strlen(params->passphrase) > 0) {
915 ssid->passphrase = os_strdup(params->passphrase);
916 if (ssid->passphrase == NULL) {
917 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
918 "passphrase for GO");
919 wpa_config_remove_network(wpa_s->conf, ssid->id);
923 ssid->passphrase = NULL;
924 ssid->psk_set = params->psk_set;
926 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
927 else if (ssid->passphrase)
928 wpa_config_update_psk(ssid);
929 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
931 wpa_s->ap_configured_cb = p2p_go_configured;
932 wpa_s->ap_configured_cb_ctx = wpa_s;
933 wpa_s->ap_configured_cb_data = wpa_s->go_params;
934 wpa_s->connect_without_scan = ssid;
935 wpa_s->reassociate = 1;
936 wpa_s->disconnected = 0;
937 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
939 wpa_supplicant_req_scan(wpa_s, 0, 0);
943 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
944 const struct wpa_supplicant *src)
946 struct wpa_config *d;
947 const struct wpa_config *s;
952 #define C(n) if (s->n) d->n = os_strdup(s->n)
961 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
962 os_memcpy(d->sec_device_type, s->sec_device_type,
963 sizeof(d->sec_device_type));
964 d->num_sec_device_types = s->num_sec_device_types;
966 d->p2p_group_idle = s->p2p_group_idle;
967 d->p2p_intra_bss = s->p2p_intra_bss;
968 d->persistent_reconnect = s->persistent_reconnect;
969 d->max_num_sta = s->max_num_sta;
970 d->pbc_in_m1 = s->pbc_in_m1;
974 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
975 enum wpa_driver_if_type type)
977 char ifname[120], force_ifname[120];
979 if (wpa_s->pending_interface_name[0]) {
980 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
981 "- skip creation of a new one");
982 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
983 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
984 "unknown?! ifname='%s'",
985 wpa_s->pending_interface_name);
991 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
992 wpa_s->p2p_group_idx);
993 if (os_strlen(ifname) >= IFNAMSIZ &&
994 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
995 /* Try to avoid going over the IFNAMSIZ length limit */
996 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
997 wpa_s->p2p_group_idx);
999 force_ifname[0] = '\0';
1001 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1003 wpa_s->p2p_group_idx++;
1005 wpa_s->pending_interface_type = type;
1006 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1007 wpa_s->pending_interface_addr, NULL) < 0) {
1008 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1013 if (force_ifname[0]) {
1014 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1016 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1017 sizeof(wpa_s->pending_interface_name));
1019 os_strlcpy(wpa_s->pending_interface_name, ifname,
1020 sizeof(wpa_s->pending_interface_name));
1021 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1022 MACSTR, wpa_s->pending_interface_name,
1023 MAC2STR(wpa_s->pending_interface_addr));
1029 static void wpas_p2p_remove_pending_group_interface(
1030 struct wpa_supplicant *wpa_s)
1032 if (!wpa_s->pending_interface_name[0] ||
1033 is_zero_ether_addr(wpa_s->pending_interface_addr))
1034 return; /* No pending virtual interface */
1036 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1037 wpa_s->pending_interface_name);
1038 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1039 wpa_s->pending_interface_name);
1040 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1041 wpa_s->pending_interface_name[0] = '\0';
1045 static struct wpa_supplicant *
1046 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1048 struct wpa_interface iface;
1049 struct wpa_supplicant *group_wpa_s;
1051 if (!wpa_s->pending_interface_name[0]) {
1052 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1053 if (!wpas_p2p_create_iface(wpa_s))
1056 * Something has forced us to remove the pending interface; try
1057 * to create a new one and hope for the best that we will get
1058 * the same local address.
1060 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1061 WPA_IF_P2P_CLIENT) < 0)
1065 os_memset(&iface, 0, sizeof(iface));
1066 iface.ifname = wpa_s->pending_interface_name;
1067 iface.driver = wpa_s->driver->name;
1068 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1069 iface.driver_param = wpa_s->conf->driver_param;
1070 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1071 if (group_wpa_s == NULL) {
1072 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1073 "wpa_supplicant interface");
1076 wpa_s->pending_interface_name[0] = '\0';
1077 group_wpa_s->parent = wpa_s;
1078 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1079 P2P_GROUP_INTERFACE_CLIENT;
1080 wpa_s->global->p2p_group_formation = group_wpa_s;
1082 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1088 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1091 struct wpa_supplicant *wpa_s = eloop_ctx;
1092 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1093 if (wpa_s->global->p2p)
1094 p2p_group_formation_failed(wpa_s->global->p2p);
1095 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1096 wpa_drv_p2p_group_formation_failed(wpa_s);
1097 wpas_group_formation_completed(wpa_s, 0);
1101 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1103 struct wpa_supplicant *wpa_s = ctx;
1105 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1106 wpa_drv_cancel_remain_on_channel(wpa_s);
1107 wpa_s->off_channel_freq = 0;
1108 wpa_s->roc_waiting_drv_freq = 0;
1112 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1114 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1115 wpas_p2p_remove_pending_group_interface(wpa_s);
1119 if (wpa_s->p2p_go_ht40)
1122 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1123 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1125 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1126 struct wpa_ssid *ssid;
1127 ssid = wpa_config_get_network(wpa_s->conf,
1128 wpa_s->p2p_persistent_id);
1129 if (ssid && ssid->disabled == 2 &&
1130 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1131 size_t len = os_strlen(ssid->passphrase);
1132 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1133 "on requested persistent group");
1134 os_memcpy(res->passphrase, ssid->passphrase, len);
1135 res->passphrase[len] = '\0';
1139 if (wpa_s->create_p2p_iface) {
1140 struct wpa_supplicant *group_wpa_s =
1141 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1142 if (group_wpa_s == NULL) {
1143 wpas_p2p_remove_pending_group_interface(wpa_s);
1146 if (group_wpa_s != wpa_s) {
1147 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1148 sizeof(group_wpa_s->p2p_pin));
1149 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1151 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1152 wpa_s->pending_interface_name[0] = '\0';
1153 group_wpa_s->p2p_in_provisioning = 1;
1156 wpas_start_wps_go(group_wpa_s, res, 1);
1158 wpas_start_wps_enrollee(group_wpa_s, res);
1160 wpa_s->p2p_in_provisioning = 1;
1161 wpa_s->global->p2p_group_formation = wpa_s;
1164 wpas_start_wps_go(wpa_s, res, 1);
1166 wpas_start_wps_enrollee(ctx, res);
1169 wpa_s->p2p_long_listen = 0;
1170 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1172 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1173 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1174 (res->peer_config_timeout % 100) * 10000,
1175 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1179 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1181 struct wpa_supplicant *wpa_s = ctx;
1182 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1183 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1185 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1189 void wpas_dev_found(void *ctx, const u8 *addr,
1190 const struct p2p_peer_info *info,
1193 #ifndef CONFIG_NO_STDOUT_DEBUG
1194 struct wpa_supplicant *wpa_s = ctx;
1195 char devtype[WPS_DEV_TYPE_BUFSIZE];
1196 #define WFD_DEV_INFO_SIZE 9
1197 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
1198 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
1199 #ifdef CONFIG_WIFI_DISPLAY
1200 if (info->wfd_subelems) {
1201 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1202 wpabuf_head(info->wfd_subelems),
1205 #endif /* CONFIG_WIFI_DISPLAY */
1206 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1207 " p2p_dev_addr=" MACSTR
1208 " pri_dev_type=%s name='%s' config_methods=0x%x "
1209 "dev_capab=0x%x group_capab=0x%x%s%s",
1210 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1211 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1213 info->device_name, info->config_methods,
1214 info->dev_capab, info->group_capab,
1215 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
1216 #endif /* CONFIG_NO_STDOUT_DEBUG */
1218 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1222 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1224 struct wpa_supplicant *wpa_s = ctx;
1226 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1227 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1229 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1233 static int wpas_start_listen(void *ctx, unsigned int freq,
1234 unsigned int duration,
1235 const struct wpabuf *probe_resp_ie)
1237 struct wpa_supplicant *wpa_s = ctx;
1239 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1241 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1242 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1243 "report received Probe Request frames");
1247 wpa_s->pending_listen_freq = freq;
1248 wpa_s->pending_listen_duration = duration;
1250 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1251 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1252 "to remain on channel (%u MHz) for Listen "
1254 wpa_s->pending_listen_freq = 0;
1257 wpa_s->off_channel_freq = 0;
1258 wpa_s->roc_waiting_drv_freq = freq;
1264 static void wpas_stop_listen(void *ctx)
1266 struct wpa_supplicant *wpa_s = ctx;
1267 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1268 wpa_drv_cancel_remain_on_channel(wpa_s);
1269 wpa_s->off_channel_freq = 0;
1270 wpa_s->roc_waiting_drv_freq = 0;
1272 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1273 wpa_drv_probe_req_report(wpa_s, 0);
1277 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1279 struct wpa_supplicant *wpa_s = ctx;
1280 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1285 * DNS Header section is used only to calculate compression pointers, so the
1286 * contents of this data does not matter, but the length needs to be reserved
1287 * in the virtual packet.
1289 #define DNS_HEADER_LEN 12
1292 * 27-octet in-memory packet from P2P specification containing two implied
1293 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1295 #define P2P_SD_IN_MEMORY_LEN 27
1297 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1298 u8 **spos, const u8 *end)
1300 while (*spos < end) {
1301 u8 val = ((*spos)[0] & 0xc0) >> 6;
1304 if (val == 1 || val == 2) {
1305 /* These are reserved values in RFC 1035 */
1306 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1307 "sequence starting with 0x%x", val);
1316 if (*spos + 2 > end) {
1317 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1318 "DNS offset field");
1322 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1323 if (offset >= *spos - start) {
1324 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1325 "pointer offset %u", offset);
1330 spos_tmp = start + offset;
1331 return p2p_sd_dns_uncompress_label(upos, uend, start,
1337 len = (*spos)[0] & 0x3f;
1342 if (*spos + len > end) {
1343 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1344 "sequence - no room for label with length "
1349 if (*upos + len + 2 > uend)
1352 os_memcpy(*upos, *spos, len);
1364 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1365 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1366 * not large enough */
1367 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1368 size_t msg_len, size_t offset)
1370 /* 27-octet in-memory packet from P2P specification */
1371 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1372 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1373 u8 *tmp, *end, *spos;
1379 if (offset > msg_len)
1382 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1385 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1386 end = spos + msg_len;
1389 os_memset(tmp, 0, DNS_HEADER_LEN);
1390 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1391 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1394 uend = buf + buf_len;
1396 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1405 } else if (upos[-1] == '.')
1413 static struct p2p_srv_bonjour *
1414 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1415 const struct wpabuf *query)
1417 struct p2p_srv_bonjour *bsrv;
1420 len = wpabuf_len(query);
1421 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1422 struct p2p_srv_bonjour, list) {
1423 if (len == wpabuf_len(bsrv->query) &&
1424 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1432 static struct p2p_srv_upnp *
1433 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1434 const char *service)
1436 struct p2p_srv_upnp *usrv;
1438 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1439 struct p2p_srv_upnp, list) {
1440 if (version == usrv->version &&
1441 os_strcmp(service, usrv->service) == 0)
1448 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1453 if (wpabuf_tailroom(resp) < 5)
1456 /* Length (to be filled) */
1457 len_pos = wpabuf_put(resp, 2);
1458 wpabuf_put_u8(resp, srv_proto);
1459 wpabuf_put_u8(resp, srv_trans_id);
1461 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1462 /* Response Data: empty */
1463 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1467 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1468 struct wpabuf *resp, u8 srv_trans_id)
1470 struct p2p_srv_bonjour *bsrv;
1473 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1475 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1476 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1480 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1481 struct p2p_srv_bonjour, list) {
1482 if (wpabuf_tailroom(resp) <
1483 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1485 /* Length (to be filled) */
1486 len_pos = wpabuf_put(resp, 2);
1487 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1488 wpabuf_put_u8(resp, srv_trans_id);
1490 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1491 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1492 wpabuf_head(bsrv->resp),
1493 wpabuf_len(bsrv->resp));
1495 wpabuf_put_buf(resp, bsrv->query); /* Key */
1496 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1497 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1503 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1506 char str_rx[256], str_srv[256];
1508 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1509 return 0; /* Too short to include DNS Type and Version */
1510 if (os_memcmp(query + query_len - 3,
1511 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1513 return 0; /* Mismatch in DNS Type or Version */
1514 if (query_len == wpabuf_len(bsrv->query) &&
1515 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1516 return 1; /* Binary match */
1518 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1520 return 0; /* Failed to uncompress query */
1521 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1522 wpabuf_head(bsrv->query),
1523 wpabuf_len(bsrv->query) - 3, 0))
1524 return 0; /* Failed to uncompress service */
1526 return os_strcmp(str_rx, str_srv) == 0;
1530 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1531 struct wpabuf *resp, u8 srv_trans_id,
1532 const u8 *query, size_t query_len)
1534 struct p2p_srv_bonjour *bsrv;
1538 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1540 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1541 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1542 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1547 if (query_len == 0) {
1548 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1552 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1553 struct p2p_srv_bonjour, list) {
1554 if (!match_bonjour_query(bsrv, query, query_len))
1557 if (wpabuf_tailroom(resp) <
1558 5 + query_len + wpabuf_len(bsrv->resp))
1563 /* Length (to be filled) */
1564 len_pos = wpabuf_put(resp, 2);
1565 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1566 wpabuf_put_u8(resp, srv_trans_id);
1569 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1570 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1571 wpabuf_head(bsrv->resp),
1572 wpabuf_len(bsrv->resp));
1575 wpabuf_put_data(resp, query, query_len); /* Key */
1576 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1578 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1582 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1584 if (wpabuf_tailroom(resp) < 5)
1587 /* Length (to be filled) */
1588 len_pos = wpabuf_put(resp, 2);
1589 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1590 wpabuf_put_u8(resp, srv_trans_id);
1593 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1594 /* Response Data: empty */
1595 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1601 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1602 struct wpabuf *resp, u8 srv_trans_id)
1604 struct p2p_srv_upnp *usrv;
1607 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1609 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1610 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1614 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1615 struct p2p_srv_upnp, list) {
1616 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1619 /* Length (to be filled) */
1620 len_pos = wpabuf_put(resp, 2);
1621 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1622 wpabuf_put_u8(resp, srv_trans_id);
1625 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1627 wpabuf_put_u8(resp, usrv->version);
1628 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1630 wpabuf_put_str(resp, usrv->service);
1631 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1637 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1638 struct wpabuf *resp, u8 srv_trans_id,
1639 const u8 *query, size_t query_len)
1641 struct p2p_srv_upnp *usrv;
1647 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1650 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1651 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1652 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1657 if (query_len == 0) {
1658 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1662 if (wpabuf_tailroom(resp) < 5)
1665 /* Length (to be filled) */
1666 len_pos = wpabuf_put(resp, 2);
1667 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1668 wpabuf_put_u8(resp, srv_trans_id);
1671 str = os_malloc(query_len);
1674 os_memcpy(str, query + 1, query_len - 1);
1675 str[query_len - 1] = '\0';
1677 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1678 struct p2p_srv_upnp, list) {
1679 if (version != usrv->version)
1682 if (os_strcmp(str, "ssdp:all") != 0 &&
1683 os_strstr(usrv->service, str) == NULL)
1686 if (wpabuf_tailroom(resp) < 2)
1690 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1692 wpabuf_put_u8(resp, version);
1694 wpabuf_put_u8(resp, ',');
1698 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1700 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1702 wpabuf_put_str(resp, usrv->service);
1707 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1710 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1711 /* Response Data: empty */
1714 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1718 #ifdef CONFIG_WIFI_DISPLAY
1719 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1720 struct wpabuf *resp, u8 srv_trans_id,
1721 const u8 *query, size_t query_len)
1727 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1729 if (!wpa_s->global->wifi_display) {
1730 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1731 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1736 if (query_len < 1) {
1737 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1742 if (wpabuf_tailroom(resp) < 5)
1747 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1749 /* TODO: role specific handling */
1751 /* Length (to be filled) */
1752 len_pos = wpabuf_put(resp, 2);
1753 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1754 wpabuf_put_u8(resp, srv_trans_id);
1755 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1757 while (pos < query + query_len) {
1758 if (*pos < MAX_WFD_SUBELEMS &&
1759 wpa_s->global->wfd_subelem[*pos] &&
1760 wpabuf_tailroom(resp) >=
1761 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1762 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1763 "subelement %u", *pos);
1764 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1769 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1771 #endif /* CONFIG_WIFI_DISPLAY */
1774 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1775 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1777 struct wpa_supplicant *wpa_s = ctx;
1778 const u8 *pos = tlvs;
1779 const u8 *end = tlvs + tlvs_len;
1782 struct wpabuf *resp;
1783 u8 srv_proto, srv_trans_id;
1787 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1789 buf_len = 2 * tlvs_len + 1;
1790 buf = os_malloc(buf_len);
1792 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1793 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1795 freq, MAC2STR(sa), dialog_token, update_indic,
1800 if (wpa_s->p2p_sd_over_ctrl_iface) {
1801 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1802 update_indic, tlvs, tlvs_len);
1803 return; /* to be processed by an external program */
1806 resp = wpabuf_alloc(10000);
1810 while (pos + 1 < end) {
1811 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1812 slen = WPA_GET_LE16(pos);
1814 if (pos + slen > end || slen < 2) {
1815 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1820 tlv_end = pos + slen;
1823 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1825 srv_trans_id = *pos++;
1826 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1829 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1830 pos, tlv_end - pos);
1833 if (wpa_s->force_long_sd) {
1834 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1836 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1837 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1841 switch (srv_proto) {
1842 case P2P_SERV_ALL_SERVICES:
1843 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1844 "for all services");
1845 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1846 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1847 wpa_printf(MSG_DEBUG, "P2P: No service "
1848 "discovery protocols available");
1849 wpas_sd_add_proto_not_avail(
1850 resp, P2P_SERV_ALL_SERVICES,
1854 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1855 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1857 case P2P_SERV_BONJOUR:
1858 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1859 pos, tlv_end - pos);
1862 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1863 pos, tlv_end - pos);
1865 #ifdef CONFIG_WIFI_DISPLAY
1866 case P2P_SERV_WIFI_DISPLAY:
1867 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1868 pos, tlv_end - pos);
1870 #endif /* CONFIG_WIFI_DISPLAY */
1872 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1873 "protocol %u", srv_proto);
1874 wpas_sd_add_proto_not_avail(resp, srv_proto,
1883 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1884 update_indic, tlvs, tlvs_len);
1886 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1892 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1893 const u8 *tlvs, size_t tlvs_len)
1895 struct wpa_supplicant *wpa_s = ctx;
1896 const u8 *pos = tlvs;
1897 const u8 *end = tlvs + tlvs_len;
1903 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1905 if (tlvs_len > 1500) {
1906 /* TODO: better way for handling this */
1907 wpa_msg_ctrl(wpa_s, MSG_INFO,
1908 P2P_EVENT_SERV_DISC_RESP MACSTR
1909 " %u <long response: %u bytes>",
1910 MAC2STR(sa), update_indic,
1911 (unsigned int) tlvs_len);
1913 buf_len = 2 * tlvs_len + 1;
1914 buf = os_malloc(buf_len);
1916 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1917 wpa_msg_ctrl(wpa_s, MSG_INFO,
1918 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1919 MAC2STR(sa), update_indic, buf);
1925 u8 srv_proto, srv_trans_id, status;
1927 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1928 slen = WPA_GET_LE16(pos);
1930 if (pos + slen > end || slen < 3) {
1931 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1935 tlv_end = pos + slen;
1938 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1940 srv_trans_id = *pos++;
1941 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1944 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1947 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1948 pos, tlv_end - pos);
1953 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1957 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1958 const struct wpabuf *tlvs)
1960 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1961 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1962 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1964 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1968 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1969 u8 version, const char *query)
1971 struct wpabuf *tlvs;
1974 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1977 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1978 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1979 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1980 wpabuf_put_u8(tlvs, version);
1981 wpabuf_put_str(tlvs, query);
1982 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1988 #ifdef CONFIG_WIFI_DISPLAY
1990 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1991 const struct wpabuf *tlvs)
1993 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1995 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1997 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2001 #define MAX_WFD_SD_SUBELEMS 20
2003 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2004 const char *subelems)
2011 len = wpabuf_put(tlvs, 2);
2012 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2013 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2015 wpabuf_put_u8(tlvs, role);
2020 if (val >= 0 && val < 256) {
2021 wpabuf_put_u8(tlvs, val);
2023 if (count == MAX_WFD_SD_SUBELEMS)
2026 pos = os_strchr(pos + 1, ',');
2032 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2036 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2037 const u8 *dst, const char *role)
2039 struct wpabuf *tlvs;
2041 const char *subelems;
2044 subelems = os_strchr(role, ' ');
2045 if (subelems == NULL)
2049 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2053 if (os_strstr(role, "[source]"))
2054 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2055 if (os_strstr(role, "[pri-sink]"))
2056 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2057 if (os_strstr(role, "[sec-sink]"))
2058 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2059 if (os_strstr(role, "[source+sink]"))
2060 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2062 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2067 #endif /* CONFIG_WIFI_DISPLAY */
2070 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2072 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2073 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
2074 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2076 return p2p_sd_cancel_request(wpa_s->global->p2p,
2077 (void *) (uintptr_t) req);
2081 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2082 const u8 *dst, u8 dialog_token,
2083 const struct wpabuf *resp_tlvs)
2085 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2086 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2090 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2092 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2097 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2099 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2102 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2103 wpa_drv_p2p_service_update(wpa_s);
2106 if (wpa_s->global->p2p)
2108 p2p_sd_service_update(wpa_s->global->p2p, action);
2110 p2p_sd_service_update(wpa_s->global->p2p);
2115 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2117 dl_list_del(&bsrv->list);
2118 wpabuf_free(bsrv->query);
2119 wpabuf_free(bsrv->resp);
2124 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2126 dl_list_del(&usrv->list);
2127 os_free(usrv->service);
2132 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2134 struct p2p_srv_bonjour *bsrv, *bn;
2135 struct p2p_srv_upnp *usrv, *un;
2137 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2138 struct p2p_srv_bonjour, list)
2139 wpas_p2p_srv_bonjour_free(bsrv);
2141 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2142 struct p2p_srv_upnp, list)
2143 wpas_p2p_srv_upnp_free(usrv);
2146 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2148 wpas_p2p_sd_service_update(wpa_s);
2153 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2154 struct wpabuf *query, struct wpabuf *resp)
2156 struct p2p_srv_bonjour *bsrv;
2158 bsrv = os_zalloc(sizeof(*bsrv));
2161 bsrv->query = query;
2163 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2166 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2168 wpas_p2p_sd_service_update(wpa_s);
2174 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2175 const struct wpabuf *query)
2177 struct p2p_srv_bonjour *bsrv;
2179 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2182 wpas_p2p_srv_bonjour_free(bsrv);
2184 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2186 wpas_p2p_sd_service_update(wpa_s);
2192 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2193 const char *service)
2195 struct p2p_srv_upnp *usrv;
2197 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2198 return 0; /* Already listed */
2199 usrv = os_zalloc(sizeof(*usrv));
2202 usrv->version = version;
2203 usrv->service = os_strdup(service);
2204 if (usrv->service == NULL) {
2208 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2211 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2213 wpas_p2p_sd_service_update(wpa_s);
2219 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2220 const char *service)
2222 struct p2p_srv_upnp *usrv;
2224 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2227 wpas_p2p_srv_upnp_free(usrv);
2229 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2231 wpas_p2p_sd_service_update(wpa_s);
2237 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2238 const u8 *peer, const char *params,
2239 unsigned int generated_pin)
2241 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2242 MAC2STR(peer), generated_pin, params);
2246 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2247 const u8 *peer, const char *params)
2249 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2250 MAC2STR(peer), params);
2254 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2255 const u8 *dev_addr, const u8 *pri_dev_type,
2256 const char *dev_name, u16 supp_config_methods,
2257 u8 dev_capab, u8 group_capab, const u8 *group_id,
2258 size_t group_id_len)
2260 struct wpa_supplicant *wpa_s = ctx;
2261 char devtype[WPS_DEV_TYPE_BUFSIZE];
2263 u8 empty_dev_type[8];
2264 unsigned int generated_pin = 0;
2265 struct wpa_supplicant *group = NULL;
2268 for (group = wpa_s->global->ifaces; group; group = group->next)
2270 struct wpa_ssid *s = group->current_ssid;
2272 s->mode == WPAS_MODE_P2P_GO &&
2273 group_id_len - ETH_ALEN == s->ssid_len &&
2274 os_memcmp(group_id + ETH_ALEN, s->ssid,
2280 if (pri_dev_type == NULL) {
2281 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2282 pri_dev_type = empty_dev_type;
2284 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2285 " pri_dev_type=%s name='%s' config_methods=0x%x "
2286 "dev_capab=0x%x group_capab=0x%x%s%s",
2288 wps_dev_type_bin2str(pri_dev_type, devtype,
2290 dev_name, supp_config_methods, dev_capab, group_capab,
2291 group ? " group=" : "",
2292 group ? group->ifname : "");
2293 params[sizeof(params) - 1] = '\0';
2295 if (config_methods & WPS_CONFIG_DISPLAY) {
2296 generated_pin = wps_generate_pin();
2297 wpas_prov_disc_local_display(wpa_s, peer, params,
2299 } else if (config_methods & WPS_CONFIG_KEYPAD)
2300 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2301 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2302 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2303 "%s", MAC2STR(peer), params);
2305 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2306 P2P_PROV_DISC_SUCCESS,
2307 config_methods, generated_pin);
2311 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2313 struct wpa_supplicant *wpa_s = ctx;
2314 unsigned int generated_pin = 0;
2317 if (wpa_s->pending_pd_before_join &&
2318 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2319 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2320 wpa_s->pending_pd_before_join = 0;
2321 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2322 "join-existing-group operation");
2323 wpas_p2p_join_start(wpa_s);
2327 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2328 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2329 os_snprintf(params, sizeof(params), " peer_go=%d",
2330 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2334 if (config_methods & WPS_CONFIG_DISPLAY)
2335 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2336 else if (config_methods & WPS_CONFIG_KEYPAD) {
2337 generated_pin = wps_generate_pin();
2338 wpas_prov_disc_local_display(wpa_s, peer, params,
2340 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2341 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2342 "%s", MAC2STR(peer), params);
2344 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2345 P2P_PROV_DISC_SUCCESS,
2346 config_methods, generated_pin);
2350 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2351 enum p2p_prov_disc_status status)
2353 struct wpa_supplicant *wpa_s = ctx;
2355 if (wpa_s->p2p_fallback_to_go_neg) {
2356 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2357 "failed - fall back to GO Negotiation");
2358 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2362 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2363 wpa_s->pending_pd_before_join = 0;
2364 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2365 "join-existing-group operation (no ACK for PD "
2367 wpas_p2p_join_start(wpa_s);
2371 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2372 " p2p_dev_addr=" MACSTR " status=%d",
2373 MAC2STR(peer), status);
2375 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2380 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2381 const u8 *go_dev_addr, const u8 *ssid,
2382 size_t ssid_len, int *go, u8 *group_bssid,
2383 int *force_freq, int persistent_group)
2385 struct wpa_supplicant *wpa_s = ctx;
2387 u8 cur_bssid[ETH_ALEN];
2389 struct wpa_supplicant *grp;
2391 if (!persistent_group) {
2392 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2393 " to join an active group", MAC2STR(sa));
2394 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2395 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2397 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2398 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2399 "authorized invitation");
2403 * Do not accept the invitation automatically; notify user and
2406 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2409 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2411 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2412 "running persistent group");
2414 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2418 if (!wpa_s->conf->persistent_reconnect)
2419 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2421 for (s = wpa_s->conf->ssid; s; s = s->next) {
2422 if (s->disabled == 2 &&
2423 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2424 s->ssid_len == ssid_len &&
2425 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2430 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2431 " requested reinvocation of an unknown group",
2433 return P2P_SC_FAIL_UNKNOWN_GROUP;
2436 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2438 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2439 wpa_printf(MSG_DEBUG, "P2P: The only available "
2440 "interface is already in use - reject "
2442 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2444 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2445 } else if (s->mode == WPAS_MODE_P2P_GO) {
2447 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2449 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2450 "interface address for the group");
2451 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2453 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2458 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2459 wpa_s->assoc_freq) {
2460 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2461 "the channel we are already using");
2462 *force_freq = wpa_s->assoc_freq;
2465 res = wpa_drv_shared_freq(wpa_s);
2467 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2468 "with the channel we are already using on a "
2469 "shared interface");
2473 return P2P_SC_SUCCESS;
2477 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2478 const u8 *ssid, size_t ssid_len,
2479 const u8 *go_dev_addr, u8 status,
2482 struct wpa_supplicant *wpa_s = ctx;
2485 for (s = wpa_s->conf->ssid; s; s = s->next) {
2486 if (s->disabled == 2 &&
2487 s->ssid_len == ssid_len &&
2488 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2492 if (status == P2P_SC_SUCCESS) {
2493 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2494 " was accepted; op_freq=%d MHz",
2495 MAC2STR(sa), op_freq);
2497 int go = s->mode == WPAS_MODE_P2P_GO;
2498 wpas_p2p_group_add_persistent(
2499 wpa_s, s, go, go ? op_freq : 0, 0);
2501 wpa_s->user_initiated_pd = 0;
2502 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2503 wpa_s->p2p_wps_method, 0);
2508 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2509 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2510 " was rejected (status %u)", MAC2STR(sa), status);
2516 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2517 "sa=" MACSTR " go_dev_addr=" MACSTR
2518 " bssid=" MACSTR " unknown-network",
2519 MAC2STR(sa), MAC2STR(go_dev_addr),
2522 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2523 "sa=" MACSTR " go_dev_addr=" MACSTR
2525 MAC2STR(sa), MAC2STR(go_dev_addr));
2530 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2531 " persistent=%d", MAC2STR(sa), s->id);
2535 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2537 struct wpa_supplicant *wpa_s = ctx;
2538 struct wpa_ssid *ssid;
2541 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2542 "status=%d " MACSTR,
2543 status, MAC2STR(bssid));
2545 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2546 "status=%d ", status);
2548 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2550 if (wpa_s->pending_invite_ssid_id == -1)
2551 return; /* Invitation to active group */
2553 if (status != P2P_SC_SUCCESS) {
2554 wpas_p2p_remove_pending_group_interface(wpa_s);
2558 ssid = wpa_config_get_network(wpa_s->conf,
2559 wpa_s->pending_invite_ssid_id);
2561 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2562 "data matching with invitation");
2567 * The peer could have missed our ctrl::ack frame for Invitation
2568 * Response and continue retransmitting the frame. To reduce the
2569 * likelihood of the peer not getting successful TX status for the
2570 * Invitation Response frame, wait a short time here before starting
2571 * the persistent group so that we will remain on the current channel to
2572 * acknowledge any possible retransmission from the peer.
2575 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2576 "starting persistent group");
2579 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2580 "starting persistent group");
2581 os_sleep(0, 100000);
2584 wpas_p2p_group_add_persistent(wpa_s, ssid,
2585 ssid->mode == WPAS_MODE_P2P_GO,
2586 wpa_s->p2p_persistent_go_freq,
2587 wpa_s->p2p_go_ht40);
2591 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2596 if (global->p2p_disallow_freq == NULL)
2599 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2600 if (freq >= global->p2p_disallow_freq[i].min &&
2601 freq <= global->p2p_disallow_freq[i].max)
2609 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2611 reg->channel[reg->channels] = chan;
2616 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2617 struct p2p_channels *chan)
2621 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2624 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2625 chan->reg_class[cla].reg_class = 81;
2626 chan->reg_class[cla].channels = 0;
2627 for (i = 0; i < 11; i++) {
2628 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2629 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2631 if (chan->reg_class[cla].channels)
2634 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2637 /* Operating class 115 - 5 GHz, channels 36-48 */
2638 chan->reg_class[cla].reg_class = 115;
2639 chan->reg_class[cla].channels = 0;
2640 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2641 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2642 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2643 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2644 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2645 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2646 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2647 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2648 if (chan->reg_class[cla].channels)
2651 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2654 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2655 chan->reg_class[cla].reg_class = 124;
2656 chan->reg_class[cla].channels = 0;
2657 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2658 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2659 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2660 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2661 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2662 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2663 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2664 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2665 if (chan->reg_class[cla].channels)
2668 chan->reg_classes = cla;
2673 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2675 enum hostapd_hw_mode mode)
2679 for (i = 0; i < num_modes; i++) {
2680 if (modes[i].mode == mode)
2688 static int has_channel(struct wpa_global *global,
2689 struct hostapd_hw_modes *mode, u8 chan, int *flags)
2694 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2696 if (wpas_p2p_disallowed_freq(global, freq))
2699 for (i = 0; i < mode->num_channels; i++) {
2700 if (mode->channels[i].chan == chan) {
2702 *flags = mode->channels[i].flag;
2703 return !(mode->channels[i].flag &
2704 (HOSTAPD_CHAN_DISABLED |
2705 HOSTAPD_CHAN_PASSIVE_SCAN |
2706 HOSTAPD_CHAN_NO_IBSS |
2707 HOSTAPD_CHAN_RADAR));
2715 struct p2p_oper_class_map {
2716 enum hostapd_hw_mode mode;
2721 enum { BW20, BW40PLUS, BW40MINUS } bw;
2724 static struct p2p_oper_class_map op_class[] = {
2725 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2726 #if 0 /* Do not enable HT40 on 2 GHz for now */
2727 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2728 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2730 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2731 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2732 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2733 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2734 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2735 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2736 { -1, 0, 0, 0, 0, BW20 }
2740 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2741 struct hostapd_hw_modes *mode,
2746 if (!has_channel(wpa_s->global, mode, channel, &flag))
2748 if (bw == BW40MINUS &&
2749 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2750 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2752 if (bw == BW40PLUS &&
2753 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2754 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2760 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2761 struct p2p_channels *chan)
2763 struct hostapd_hw_modes *mode;
2766 if (wpa_s->hw.modes == NULL) {
2767 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2768 "of all supported channels; assume dualband "
2770 return wpas_p2p_default_channels(wpa_s, chan);
2775 for (op = 0; op_class[op].op_class; op++) {
2776 struct p2p_oper_class_map *o = &op_class[op];
2778 struct p2p_reg_class *reg = NULL;
2780 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2783 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2784 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2787 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2788 "class %u", o->op_class);
2789 reg = &chan->reg_class[cla];
2791 reg->reg_class = o->op_class;
2793 reg->channel[reg->channels] = ch;
2797 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2798 reg->channel, reg->channels);
2802 chan->reg_classes = cla;
2808 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2809 struct hostapd_hw_modes *mode, u8 channel)
2813 for (op = 0; op_class[op].op_class; op++) {
2814 struct p2p_oper_class_map *o = &op_class[op];
2817 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2818 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2819 o->bw == BW20 || ch != channel)
2821 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2825 return (o->bw == BW40MINUS) ? -1 : 1;
2834 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2837 struct wpa_supplicant *wpa_s = ctx;
2839 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2840 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2846 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2850 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2852 struct wpa_supplicant *wpa_s = ctx;
2854 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2855 struct wpa_ssid *ssid = wpa_s->current_ssid;
2858 if (ssid->mode != WPAS_MODE_INFRA)
2860 if (wpa_s->wpa_state != WPA_COMPLETED &&
2861 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2863 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2872 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2873 * @global: Pointer to global data from wpa_supplicant_init()
2874 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2875 * Returns: 0 on success, -1 on failure
2877 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2879 struct p2p_config p2p;
2883 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2889 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2890 struct p2p_params params;
2892 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2893 os_memset(¶ms, 0, sizeof(params));
2894 params.dev_name = wpa_s->conf->device_name;
2895 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2897 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2898 os_memcpy(params.sec_dev_type,
2899 wpa_s->conf->sec_device_type,
2900 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2902 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2908 os_memset(&p2p, 0, sizeof(p2p));
2909 p2p.msg_ctx = wpa_s;
2911 p2p.p2p_scan = wpas_p2p_scan;
2912 p2p.send_action = wpas_send_action;
2913 p2p.send_action_done = wpas_send_action_done;
2914 p2p.go_neg_completed = wpas_go_neg_completed;
2915 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2916 p2p.dev_found = wpas_dev_found;
2917 p2p.dev_lost = wpas_dev_lost;
2918 p2p.start_listen = wpas_start_listen;
2919 p2p.stop_listen = wpas_stop_listen;
2920 p2p.send_probe_resp = wpas_send_probe_resp;
2921 p2p.sd_request = wpas_sd_request;
2922 p2p.sd_response = wpas_sd_response;
2923 p2p.prov_disc_req = wpas_prov_disc_req;
2924 p2p.prov_disc_resp = wpas_prov_disc_resp;
2925 p2p.prov_disc_fail = wpas_prov_disc_fail;
2926 p2p.invitation_process = wpas_invitation_process;
2927 p2p.invitation_received = wpas_invitation_received;
2928 p2p.invitation_result = wpas_invitation_result;
2929 p2p.get_noa = wpas_get_noa;
2930 p2p.go_connected = wpas_go_connected;
2932 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2933 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2934 p2p.dev_name = wpa_s->conf->device_name;
2935 p2p.manufacturer = wpa_s->conf->manufacturer;
2936 p2p.model_name = wpa_s->conf->model_name;
2937 p2p.model_number = wpa_s->conf->model_number;
2938 p2p.serial_number = wpa_s->conf->serial_number;
2940 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2941 p2p.config_methods = wpa_s->wps->config_methods;
2944 if (wpa_s->conf->p2p_listen_reg_class &&
2945 wpa_s->conf->p2p_listen_channel) {
2946 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2947 p2p.channel = wpa_s->conf->p2p_listen_channel;
2951 * Pick one of the social channels randomly as the listen
2954 os_get_random((u8 *) &r, sizeof(r));
2955 p2p.channel = 1 + (r % 3) * 5;
2957 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2959 if (wpa_s->conf->p2p_oper_reg_class &&
2960 wpa_s->conf->p2p_oper_channel) {
2961 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2962 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2963 p2p.cfg_op_channel = 1;
2964 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2965 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2968 p2p.op_reg_class = 81;
2970 * Use random operation channel from (1, 6, 11) if no other
2971 * preference is indicated.
2973 os_get_random((u8 *) &r, sizeof(r));
2974 p2p.op_channel = 1 + (r % 3) * 5;
2975 p2p.cfg_op_channel = 0;
2976 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2977 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2979 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2980 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2981 p2p.country[2] = 0x04;
2983 os_memcpy(p2p.country, "XX\x04", 3);
2985 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2986 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2991 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2994 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2995 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2996 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2998 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2999 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3001 p2p.max_peers = 100;
3003 if (wpa_s->conf->p2p_ssid_postfix) {
3004 p2p.ssid_postfix_len =
3005 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3006 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3007 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3008 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3009 p2p.ssid_postfix_len);
3012 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3014 p2p.max_listen = wpa_s->max_remain_on_chan;
3017 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
3018 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
3019 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3021 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
3022 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
3023 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3027 global->p2p = p2p_init(&p2p);
3028 if (global->p2p == NULL)
3030 global->p2p_init_wpa_s = wpa_s;
3032 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3033 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3035 p2p_add_wps_vendor_extension(
3036 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3044 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3045 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3047 * This function deinitialize per-interface P2P data.
3049 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3051 if (wpa_s->driver && wpa_s->drv_priv)
3052 wpa_drv_probe_req_report(wpa_s, 0);
3054 if (wpa_s->go_params) {
3055 /* Clear any stored provisioning info */
3056 p2p_clear_provisioning_info(
3058 wpa_s->go_params->peer_device_addr);
3061 os_free(wpa_s->go_params);
3062 wpa_s->go_params = NULL;
3063 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3064 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3065 wpa_s->p2p_long_listen = 0;
3066 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3067 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3068 wpas_p2p_remove_pending_group_interface(wpa_s);
3070 /* TODO: remove group interface from the driver if this wpa_s instance
3071 * is on top of a P2P group interface */
3076 * wpas_p2p_deinit_global - Deinitialize global P2P module
3077 * @global: Pointer to global data from wpa_supplicant_init()
3079 * This function deinitializes the global (per device) P2P module.
3081 void wpas_p2p_deinit_global(struct wpa_global *global)
3083 struct wpa_supplicant *wpa_s, *tmp;
3085 wpa_s = global->ifaces;
3087 wpas_p2p_service_flush(wpa_s);
3089 if (global->p2p == NULL)
3092 /* Remove remaining P2P group interfaces */
3093 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3094 wpa_s = wpa_s->next;
3096 tmp = global->ifaces;
3099 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3104 /* Disconnect from the P2P group and deinit the interface */
3105 wpas_p2p_disconnect(tmp);
3109 * Deinit GO data on any possibly remaining interface (if main
3110 * interface is used as GO).
3112 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3113 if (wpa_s->ap_iface)
3114 wpas_p2p_group_deinit(wpa_s);
3117 p2p_deinit(global->p2p);
3119 global->p2p_init_wpa_s = NULL;
3123 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3125 if (wpa_s->conf->p2p_no_group_iface)
3126 return 0; /* separate interface disabled per configuration */
3127 if (wpa_s->drv_flags &
3128 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3129 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3130 return 1; /* P2P group requires a new interface in every case
3132 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3133 return 0; /* driver does not support concurrent operations */
3134 if (wpa_s->global->ifaces->next)
3135 return 1; /* more that one interface already in use */
3136 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3137 return 1; /* this interface is already in use */
3142 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3143 const u8 *peer_addr,
3144 enum p2p_wps_method wps_method,
3145 int go_intent, const u8 *own_interface_addr,
3146 unsigned int force_freq, int persistent_group,
3147 struct wpa_ssid *ssid, unsigned int pref_freq)
3149 if (persistent_group && wpa_s->conf->persistent_reconnect)
3150 persistent_group = 2;
3152 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3153 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3154 go_intent, own_interface_addr,
3155 force_freq, persistent_group);
3159 * Increase GO config timeout if HT40 is used since it takes some time
3160 * to scan channels for coex purposes before the BSS can be started.
3162 p2p_set_config_timeout(wpa_s->global->p2p,
3163 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3165 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3166 go_intent, own_interface_addr, force_freq,
3167 persistent_group, ssid ? ssid->ssid : NULL,
3168 ssid ? ssid->ssid_len : 0,
3169 wpa_s->p2p_pd_before_go_neg, pref_freq);
3173 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3174 const u8 *peer_addr,
3175 enum p2p_wps_method wps_method,
3176 int go_intent, const u8 *own_interface_addr,
3177 unsigned int force_freq, int persistent_group,
3178 struct wpa_ssid *ssid, unsigned int pref_freq)
3180 if (persistent_group && wpa_s->conf->persistent_reconnect)
3181 persistent_group = 2;
3183 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3186 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3187 go_intent, own_interface_addr, force_freq,
3188 persistent_group, ssid ? ssid->ssid : NULL,
3189 ssid ? ssid->ssid_len : 0, pref_freq);
3193 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3195 wpa_s->p2p_join_scan_count++;
3196 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3197 wpa_s->p2p_join_scan_count);
3198 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3199 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3200 " for join operationg - stop join attempt",
3201 MAC2STR(wpa_s->pending_join_iface_addr));
3202 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3203 if (wpa_s->p2p_auto_pd) {
3204 wpa_s->p2p_auto_pd = 0;
3205 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3206 " p2p_dev_addr=" MACSTR " status=N/A",
3207 MAC2STR(wpa_s->pending_join_dev_addr));
3210 wpa_msg(wpa_s->parent, MSG_INFO,
3211 P2P_EVENT_GROUP_FORMATION_FAILURE);
3216 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3218 struct wpa_supplicant *iface;
3222 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3225 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3226 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3228 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3230 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3231 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3232 shared_freq = iface->current_ssid->frequency;
3233 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3234 shared_freq = iface->assoc_freq;
3238 if (shared_freq && freq != shared_freq) {
3239 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3240 "connected on %d MHz - new connection on "
3241 "%d MHz", iface->ifname, shared_freq, freq);
3246 shared_freq = wpa_drv_shared_freq(wpa_s);
3247 if (shared_freq > 0 && shared_freq != freq) {
3248 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3249 "virtual interface connected on %d MHz - new "
3250 "connection on %d MHz", shared_freq, freq);
3258 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3259 const u8 *peer_dev_addr)
3261 struct wpa_bss *bss;
3264 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3267 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3268 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3273 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3274 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3275 "%ld.%06ld (%supdated in last scan)",
3276 bss->last_update.sec, bss->last_update.usec,
3277 updated ? "": "not ");
3283 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3284 struct wpa_scan_results *scan_res)
3286 struct wpa_bss *bss;
3288 u8 iface_addr[ETH_ALEN];
3290 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3292 if (wpa_s->global->p2p_disabled)
3295 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3296 scan_res ? (int) scan_res->num : -1,
3297 wpa_s->p2p_auto_join ? "auto_" : "");
3300 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3302 if (wpa_s->p2p_auto_pd) {
3303 int join = wpas_p2p_peer_go(wpa_s,
3304 wpa_s->pending_join_dev_addr);
3306 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3307 wpa_s->auto_pd_scan_retry++;
3308 bss = wpa_bss_get_bssid(wpa_s,
3309 wpa_s->pending_join_dev_addr);
3312 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3313 "the peer " MACSTR " at %d MHz",
3314 wpa_s->auto_pd_scan_retry,
3316 pending_join_dev_addr),
3318 wpas_p2p_join_scan_req(wpa_s, freq);
3326 wpa_s->p2p_auto_pd = 0;
3327 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3328 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3329 MAC2STR(wpa_s->pending_join_dev_addr), join);
3330 if (p2p_prov_disc_req(wpa_s->global->p2p,
3331 wpa_s->pending_join_dev_addr,
3332 wpa_s->pending_pd_config_methods, join,
3333 0, wpa_s->user_initiated_pd) < 0) {
3334 wpa_s->p2p_auto_pd = 0;
3335 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3336 " p2p_dev_addr=" MACSTR " status=N/A",
3337 MAC2STR(wpa_s->pending_join_dev_addr));
3342 if (wpa_s->p2p_auto_join) {
3343 int join = wpas_p2p_peer_go(wpa_s,
3344 wpa_s->pending_join_dev_addr);
3346 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3347 "running a GO -> use GO Negotiation");
3348 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3349 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3350 wpa_s->p2p_persistent_group, 0, 0, 0,
3351 wpa_s->p2p_go_intent,
3352 wpa_s->p2p_connect_freq,
3353 wpa_s->p2p_persistent_id,
3354 wpa_s->p2p_pd_before_go_neg,
3355 wpa_s->p2p_go_ht40);
3359 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3360 "try to join the group", join ? "" :
3363 wpa_s->p2p_fallback_to_go_neg = 1;
3366 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3367 wpa_s->pending_join_iface_addr);
3369 p2p_get_interface_addr(wpa_s->global->p2p,
3370 wpa_s->pending_join_dev_addr,
3372 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3374 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3375 "address for join from " MACSTR " to " MACSTR
3376 " based on newly discovered P2P peer entry",
3377 MAC2STR(wpa_s->pending_join_iface_addr),
3378 MAC2STR(iface_addr));
3379 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3382 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3383 wpa_s->pending_join_iface_addr);
3386 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3387 "from P2P peer table: %d MHz", freq);
3389 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3392 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3393 "from BSS table: %d MHz", freq);
3398 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3399 wpa_msg(wpa_s->parent, MSG_INFO,
3400 P2P_EVENT_GROUP_FORMATION_FAILURE
3401 "reason=FREQ_CONFLICT");
3405 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3406 "prior to joining an existing group (GO " MACSTR
3408 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3409 wpa_s->pending_pd_before_join = 1;
3411 switch (wpa_s->pending_join_wps_method) {
3412 case WPS_PIN_DISPLAY:
3413 method = WPS_CONFIG_KEYPAD;
3415 case WPS_PIN_KEYPAD:
3416 method = WPS_CONFIG_DISPLAY;
3419 method = WPS_CONFIG_PUSHBUTTON;
3426 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3427 wpa_s->pending_join_dev_addr) ==
3430 * We have already performed provision discovery for
3431 * joining the group. Proceed directly to join
3432 * operation without duplicated provision discovery. */
3433 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3434 "with " MACSTR " already done - proceed to "
3436 MAC2STR(wpa_s->pending_join_dev_addr));
3437 wpa_s->pending_pd_before_join = 0;
3441 if (p2p_prov_disc_req(wpa_s->global->p2p,
3442 wpa_s->pending_join_dev_addr, method, 1,
3443 freq, wpa_s->user_initiated_pd) < 0) {
3444 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3445 "Discovery Request before joining an "
3447 wpa_s->pending_pd_before_join = 0;
3453 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3454 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3455 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3456 wpas_p2p_check_join_scan_limit(wpa_s);
3460 /* Start join operation immediately */
3461 wpas_p2p_join_start(wpa_s);
3465 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3468 struct wpa_driver_scan_params params;
3469 struct wpabuf *wps_ie, *ies;
3471 int freqs[2] = { 0, 0 };
3475 /* If freq is not provided, check the operating freq of the GO and do a
3476 * a directed scan to save time
3479 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3480 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3483 os_memset(¶ms, 0, sizeof(params));
3485 /* P2P Wildcard SSID */
3486 params.num_ssids = 1;
3487 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3488 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3490 wpa_s->wps->dev.p2p = 1;
3491 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3492 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3494 if (wps_ie == NULL) {
3495 wpas_p2p_scan_res_join(wpa_s, NULL);
3499 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3500 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3502 wpabuf_free(wps_ie);
3503 wpas_p2p_scan_res_join(wpa_s, NULL);
3506 wpabuf_put_buf(ies, wps_ie);
3507 wpabuf_free(wps_ie);
3509 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3511 params.p2p_probe = 1;
3512 params.extra_ies = wpabuf_head(ies);
3513 params.extra_ies_len = wpabuf_len(ies);
3516 params.freqs = freqs;
3520 * Run a scan to update BSS table and start Provision Discovery once
3521 * the new scan results become available.
3523 ret = wpa_drv_scan(wpa_s, ¶ms);
3525 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3530 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3532 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3533 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3534 wpas_p2p_check_join_scan_limit(wpa_s);
3539 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3541 struct wpa_supplicant *wpa_s = eloop_ctx;
3542 wpas_p2p_join_scan_req(wpa_s, 0);
3546 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3547 const u8 *dev_addr, enum p2p_wps_method wps_method,
3550 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3551 MACSTR " dev " MACSTR ")%s",
3552 MAC2STR(iface_addr), MAC2STR(dev_addr),
3553 auto_join ? " (auto_join)" : "");
3555 wpa_s->p2p_auto_pd = 0;
3556 wpa_s->p2p_auto_join = !!auto_join;
3557 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3558 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3559 wpa_s->pending_join_wps_method = wps_method;
3561 /* Make sure we are not running find during connection establishment */
3562 wpas_p2p_stop_find(wpa_s);
3564 wpa_s->p2p_join_scan_count = 0;
3565 wpas_p2p_join_scan(wpa_s, NULL);
3570 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3572 struct wpa_supplicant *group;
3573 struct p2p_go_neg_results res;
3574 struct wpa_bss *bss;
3576 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3579 if (group != wpa_s) {
3580 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3581 sizeof(group->p2p_pin));
3582 group->p2p_wps_method = wpa_s->p2p_wps_method;
3585 group->p2p_in_provisioning = 1;
3586 wpa_s->global->p2p_group_formation = wpa_s;
3587 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3589 os_memset(&res, 0, sizeof(res));
3590 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3592 res.wps_method = wpa_s->pending_join_wps_method;
3593 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3595 res.freq = bss->freq;
3596 res.ssid_len = bss->ssid_len;
3597 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3600 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3601 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3603 wpa_drv_cancel_remain_on_channel(wpa_s);
3604 wpa_s->off_channel_freq = 0;
3605 wpa_s->roc_waiting_drv_freq = 0;
3607 wpas_start_wps_enrollee(group, &res);
3610 * Allow a longer timeout for join-a-running-group than normal 15
3611 * second group formation timeout since the GO may not have authorized
3612 * our connection yet.
3614 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3615 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3623 * wpas_p2p_connect - Request P2P Group Formation to be started
3624 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3625 * @peer_addr: Address of the peer P2P Device
3626 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3627 * @persistent_group: Whether to create a persistent group
3628 * @auto_join: Whether to select join vs. GO Negotiation automatically
3629 * @join: Whether to join an existing group (as a client) instead of starting
3630 * Group Owner negotiation; @peer_addr is BSSID in that case
3631 * @auth: Whether to only authorize the connection instead of doing that and
3632 * initiating Group Owner negotiation
3633 * @go_intent: GO Intent or -1 to use default
3634 * @freq: Frequency for the group or 0 for auto-selection
3635 * @persistent_id: Persistent group credentials to use for forcing GO
3636 * parameters or -1 to generate new values (SSID/passphrase)
3637 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3638 * interoperability workaround when initiating group formation
3639 * @ht40: Start GO with 40 MHz channel width
3640 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3641 * failure, -2 on failure due to channel not currently available,
3642 * -3 if forced channel is not supported
3644 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3645 const char *pin, enum p2p_wps_method wps_method,
3646 int persistent_group, int auto_join, int join, int auth,
3647 int go_intent, int freq, int persistent_id, int pd,
3650 int force_freq = 0, pref_freq = 0, oper_freq = 0;
3653 enum wpa_driver_if_type iftype;
3655 struct wpa_ssid *ssid = NULL;
3657 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3660 if (persistent_id >= 0) {
3661 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3662 if (ssid == NULL || ssid->disabled != 2 ||
3663 ssid->mode != WPAS_MODE_P2P_GO)
3668 go_intent = wpa_s->conf->p2p_go_intent;
3671 wpa_s->p2p_long_listen = 0;
3673 wpa_s->p2p_wps_method = wps_method;
3674 wpa_s->p2p_persistent_group = !!persistent_group;
3675 wpa_s->p2p_persistent_id = persistent_id;
3676 wpa_s->p2p_go_intent = go_intent;
3677 wpa_s->p2p_connect_freq = freq;
3678 wpa_s->p2p_fallback_to_go_neg = 0;
3679 wpa_s->p2p_pd_before_go_neg = !!pd;
3680 wpa_s->p2p_go_ht40 = !!ht40;
3683 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3684 else if (wps_method == WPS_PIN_DISPLAY) {
3685 ret = wps_generate_pin();
3686 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3688 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3691 wpa_s->p2p_pin[0] = '\0';
3693 if (join || auto_join) {
3694 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3696 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3697 "connect a running group from " MACSTR,
3698 MAC2STR(peer_addr));
3699 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3702 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3703 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3705 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3706 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3710 os_get_time(&wpa_s->p2p_auto_started);
3711 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3713 wpa_s->p2p_auto_started.sec,
3714 wpa_s->p2p_auto_started.usec);
3716 wpa_s->user_initiated_pd = 1;
3717 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3723 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3725 oper_freq = wpa_s->assoc_freq;
3727 oper_freq = wpa_drv_shared_freq(wpa_s);
3733 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3734 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3735 "(%u MHz) is not supported for P2P uses",
3740 if (oper_freq > 0 && freq != oper_freq &&
3741 !(wpa_s->drv_flags &
3742 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3743 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3744 "on %u MHz while connected on another "
3745 "channel (%u MHz)", freq, oper_freq);
3748 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3749 "requested channel (%u MHz)", freq);
3751 } else if (oper_freq > 0 &&
3752 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3753 if (!(wpa_s->drv_flags &
3754 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3755 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3756 "while connected on non-P2P supported "
3757 "channel (%u MHz)", oper_freq);
3760 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3761 "(%u MHz) not available for P2P - try to use "
3762 "another channel", oper_freq);
3764 } else if (oper_freq > 0 &&
3766 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3767 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3768 "are already using (%u MHz) on another interface",
3770 pref_freq = oper_freq;
3771 } else if (oper_freq > 0) {
3772 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3773 "channel we are already using (%u MHz) on another "
3774 "interface", oper_freq);
3775 force_freq = oper_freq;
3778 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3780 if (wpa_s->create_p2p_iface) {
3781 /* Prepare to add a new interface for the group */
3782 iftype = WPA_IF_P2P_GROUP;
3783 if (go_intent == 15)
3784 iftype = WPA_IF_P2P_GO;
3785 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3786 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3787 "interface for the group");
3791 if_addr = wpa_s->pending_interface_addr;
3793 if_addr = wpa_s->own_addr;
3796 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3798 force_freq, persistent_group, ssid,
3804 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3805 go_intent, if_addr, force_freq,
3806 persistent_group, ssid, pref_freq) < 0) {
3807 if (wpa_s->create_p2p_iface)
3808 wpas_p2p_remove_pending_group_interface(wpa_s);
3816 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3817 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3818 * @freq: Frequency of the channel in MHz
3819 * @duration: Duration of the stay on the channel in milliseconds
3821 * This callback is called when the driver indicates that it has started the
3822 * requested remain-on-channel duration.
3824 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3825 unsigned int freq, unsigned int duration)
3827 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3829 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3830 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3831 wpa_s->pending_listen_duration);
3832 wpa_s->pending_listen_freq = 0;
3837 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3838 unsigned int timeout)
3840 /* Limit maximum Listen state time based on driver limitation. */
3841 if (timeout > wpa_s->max_remain_on_chan)
3842 timeout = wpa_s->max_remain_on_chan;
3844 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3845 return wpa_drv_p2p_listen(wpa_s, timeout);
3847 return p2p_listen(wpa_s->global->p2p, timeout);
3852 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3853 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3854 * @freq: Frequency of the channel in MHz
3856 * This callback is called when the driver indicates that a remain-on-channel
3857 * operation has been completed, i.e., the duration on the requested channel
3860 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3863 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3864 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3865 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
3866 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3868 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3869 return; /* P2P module started a new operation */
3870 if (offchannel_pending_action_tx(wpa_s))
3872 if (wpa_s->p2p_long_listen > 0)
3873 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3874 if (wpa_s->p2p_long_listen > 0) {
3875 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3876 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3882 * wpas_p2p_group_remove - Remove a P2P group
3883 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3884 * @ifname: Network interface name of the group interface or "*" to remove all
3886 * Returns: 0 on success, -1 on failure
3888 * This function is used to remove a P2P group. This can be used to disconnect
3889 * from a group in which the local end is a P2P Client or to end a P2P Group in
3890 * case the local end is the Group Owner. If a virtual network interface was
3891 * created for this group, that interface will be removed. Otherwise, only the
3892 * configured P2P group network will be removed from the interface.
3894 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3896 struct wpa_global *global = wpa_s->global;
3898 if (os_strcmp(ifname, "*") == 0) {
3899 struct wpa_supplicant *prev;
3900 wpa_s = global->ifaces;
3903 wpa_s = wpa_s->next;
3904 wpas_p2p_disconnect(prev);
3909 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3910 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3914 return wpas_p2p_disconnect(wpa_s);
3918 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3919 struct p2p_go_neg_results *params,
3925 os_memset(params, 0, sizeof(*params));
3926 params->role_go = 1;
3927 params->ht40 = ht40;
3929 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3930 "frequency %d MHz", freq);
3931 params->freq = freq;
3932 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3933 wpa_s->conf->p2p_oper_channel >= 1 &&
3934 wpa_s->conf->p2p_oper_channel <= 11) {
3935 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3936 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3937 "frequency %d MHz", params->freq);
3938 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3939 wpa_s->conf->p2p_oper_reg_class == 124) {
3940 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3941 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3942 "frequency %d MHz", params->freq);
3943 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3944 wpa_s->best_overall_freq > 0 &&
3945 p2p_supported_freq(wpa_s->global->p2p,
3946 wpa_s->best_overall_freq)) {
3947 params->freq = wpa_s->best_overall_freq;
3948 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3949 "channel %d MHz", params->freq);
3950 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3951 wpa_s->best_24_freq > 0 &&
3952 p2p_supported_freq(wpa_s->global->p2p,
3953 wpa_s->best_24_freq)) {
3954 params->freq = wpa_s->best_24_freq;
3955 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3956 "channel %d MHz", params->freq);
3957 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3958 wpa_s->best_5_freq > 0 &&
3959 p2p_supported_freq(wpa_s->global->p2p,
3960 wpa_s->best_5_freq)) {
3961 params->freq = wpa_s->best_5_freq;
3962 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3963 "channel %d MHz", params->freq);
3966 for (chan = 0; chan < 11; chan++) {
3967 params->freq = 2412 + chan * 5;
3968 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3973 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3977 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3978 "known)", params->freq);
3981 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3982 wpa_s->assoc_freq && !freq) {
3983 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3985 params->freq = wpa_s->assoc_freq;
3988 res = wpa_drv_shared_freq(wpa_s);
3989 if (res > 0 && !freq) {
3990 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3991 "already using on a shared interface");
3993 } else if (res > 0 && freq != res &&
3994 !(wpa_s->drv_flags &
3995 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3996 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3997 "while connected on another channel (%u MHz)",
4006 static struct wpa_supplicant *
4007 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4010 struct wpa_supplicant *group_wpa_s;
4012 if (!wpas_p2p_create_iface(wpa_s)) {
4013 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4018 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
4019 WPA_IF_P2P_CLIENT) < 0) {
4020 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
4023 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4024 if (group_wpa_s == NULL) {
4025 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4027 wpas_p2p_remove_pending_group_interface(wpa_s);
4031 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4032 group_wpa_s->ifname);
4038 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4039 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4040 * @persistent_group: Whether to create a persistent group
4041 * @freq: Frequency for the group or 0 to indicate no hardcoding
4042 * Returns: 0 on success, -1 on failure
4044 * This function creates a new P2P group with the local end as the Group Owner,
4045 * i.e., without using Group Owner Negotiation.
4047 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
4050 struct p2p_go_neg_results params;
4053 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4056 /* Make sure we are not running find during connection establishment */
4057 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
4058 wpas_p2p_stop_find_oper(wpa_s);
4061 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4063 if (wpa_s->best_24_freq > 0 &&
4064 p2p_supported_freq(wpa_s->global->p2p,
4065 wpa_s->best_24_freq)) {
4066 freq = wpa_s->best_24_freq;
4067 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4068 "channel: %d MHz", freq);
4070 os_get_random((u8 *) &r, sizeof(r));
4071 freq = 2412 + (r % 3) * 25;
4072 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4073 "channel: %d MHz", freq);
4078 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4080 if (wpa_s->best_5_freq > 0 &&
4081 p2p_supported_freq(wpa_s->global->p2p,
4082 wpa_s->best_5_freq)) {
4083 freq = wpa_s->best_5_freq;
4084 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4085 "channel: %d MHz", freq);
4087 os_get_random((u8 *) &r, sizeof(r));
4088 freq = 5180 + (r % 4) * 20;
4089 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4090 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4091 "5 GHz channel for P2P group");
4094 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4095 "channel: %d MHz", freq);
4099 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4100 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4101 "(%u MHz) is not supported for P2P uses",
4106 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40))
4109 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4110 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4111 "(%u MHz) is not supported for P2P uses",
4115 p2p_go_params(wpa_s->global->p2p, ¶ms);
4116 params.persistent_group = persistent_group;
4118 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4121 wpas_start_wps_go(wpa_s, ¶ms, 0);
4127 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4128 struct wpa_ssid *params, int addr_allocated)
4130 struct wpa_ssid *ssid;
4132 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4136 wpa_supplicant_ap_deinit(wpa_s);
4138 ssid = wpa_config_add_network(wpa_s->conf);
4141 wpa_config_set_network_defaults(ssid);
4142 ssid->temporary = 1;
4143 ssid->proto = WPA_PROTO_RSN;
4144 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4145 ssid->group_cipher = WPA_CIPHER_CCMP;
4146 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4147 ssid->ssid = os_malloc(params->ssid_len);
4148 if (ssid->ssid == NULL) {
4149 wpa_config_remove_network(wpa_s->conf, ssid->id);
4152 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4153 ssid->ssid_len = params->ssid_len;
4154 ssid->p2p_group = 1;
4155 ssid->export_keys = 1;
4156 if (params->psk_set) {
4157 os_memcpy(ssid->psk, params->psk, 32);
4160 if (params->passphrase)
4161 ssid->passphrase = os_strdup(params->passphrase);
4163 wpa_supplicant_select_network(wpa_s, ssid);
4165 wpa_s->show_group_started = 1;
4171 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4172 struct wpa_ssid *ssid, int addr_allocated,
4175 struct p2p_go_neg_results params;
4178 if (ssid->disabled != 2 || ssid->ssid == NULL)
4181 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4182 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4183 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4188 /* Make sure we are not running find during connection establishment */
4189 wpas_p2p_stop_find_oper(wpa_s);
4191 wpa_s->p2p_fallback_to_go_neg = 0;
4193 if (ssid->mode == WPAS_MODE_INFRA)
4194 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4196 if (ssid->mode != WPAS_MODE_P2P_GO)
4199 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40))
4203 params.psk_set = ssid->psk_set;
4205 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
4206 if (ssid->passphrase) {
4207 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4208 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4209 "persistent group");
4212 os_strlcpy(params.passphrase, ssid->passphrase,
4213 sizeof(params.passphrase));
4215 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4216 params.ssid_len = ssid->ssid_len;
4217 params.persistent_group = 1;
4219 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4223 wpas_start_wps_go(wpa_s, ¶ms, 0);
4229 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4230 struct wpabuf *proberesp_ies)
4232 struct wpa_supplicant *wpa_s = ctx;
4233 if (wpa_s->ap_iface) {
4234 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4235 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4236 wpabuf_free(beacon_ies);
4237 wpabuf_free(proberesp_ies);
4241 wpabuf_free(hapd->p2p_beacon_ie);
4242 hapd->p2p_beacon_ie = beacon_ies;
4244 wpabuf_free(hapd->p2p_probe_resp_ie);
4245 hapd->p2p_probe_resp_ie = proberesp_ies;
4247 wpabuf_free(beacon_ies);
4248 wpabuf_free(proberesp_ies);
4250 wpa_supplicant_ap_update_beacon(wpa_s);
4254 static void wpas_p2p_idle_update(void *ctx, int idle)
4256 struct wpa_supplicant *wpa_s = ctx;
4257 if (!wpa_s->ap_iface)
4259 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4261 wpas_p2p_set_group_idle_timeout(wpa_s);
4263 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4267 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4268 struct wpa_ssid *ssid)
4270 struct p2p_group *group;
4271 struct p2p_group_config *cfg;
4273 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4275 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4278 cfg = os_zalloc(sizeof(*cfg));
4282 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4283 cfg->persistent_group = 2;
4284 else if (ssid->p2p_persistent_group)
4285 cfg->persistent_group = 1;
4286 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4287 if (wpa_s->max_stations &&
4288 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4289 cfg->max_clients = wpa_s->max_stations;
4291 cfg->max_clients = wpa_s->conf->max_num_sta;
4292 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4293 cfg->ssid_len = ssid->ssid_len;
4294 cfg->cb_ctx = wpa_s;
4295 cfg->ie_update = wpas_p2p_ie_update;
4296 cfg->idle_update = wpas_p2p_idle_update;
4298 group = p2p_group_init(wpa_s->global->p2p, cfg);
4301 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4302 p2p_group_notif_formation_done(group);
4303 wpa_s->p2p_group = group;
4308 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4311 struct wpa_ssid *ssid = wpa_s->current_ssid;
4313 if (!wpa_s->p2p_in_provisioning) {
4314 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4315 "provisioning not in progress");
4319 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4320 u8 go_dev_addr[ETH_ALEN];
4321 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4322 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4324 /* Clear any stored provisioning info */
4325 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4328 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4330 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4332 * Use a separate timeout for initial data connection to
4333 * complete to allow the group to be removed automatically if
4334 * something goes wrong in this step before the P2P group idle
4335 * timeout mechanism is taken into use.
4337 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4338 wpas_p2p_group_formation_timeout,
4339 wpa_s->parent, NULL);
4341 if (wpa_s->global->p2p)
4342 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4343 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4344 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4345 wpas_group_formation_completed(wpa_s, 1);
4349 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4350 struct wps_event_fail *fail)
4352 if (!wpa_s->p2p_in_provisioning) {
4353 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4354 "provisioning not in progress");
4358 if (wpa_s->go_params) {
4359 p2p_clear_provisioning_info(
4361 wpa_s->go_params->peer_device_addr);
4364 wpas_notify_p2p_wps_failed(wpa_s, fail);
4368 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4369 const char *config_method,
4370 enum wpas_p2p_prov_disc_use use)
4374 wpa_s->p2p_fallback_to_go_neg = 0;
4375 wpa_s->pending_pd_use = NORMAL_PD;
4376 if (os_strncmp(config_method, "display", 7) == 0)
4377 config_methods = WPS_CONFIG_DISPLAY;
4378 else if (os_strncmp(config_method, "keypad", 6) == 0)
4379 config_methods = WPS_CONFIG_KEYPAD;
4380 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4381 os_strncmp(config_method, "pushbutton", 10) == 0)
4382 config_methods = WPS_CONFIG_PUSHBUTTON;
4384 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
4388 if (use == WPAS_P2P_PD_AUTO) {
4389 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4390 wpa_s->pending_pd_config_methods = config_methods;
4391 wpa_s->p2p_auto_pd = 1;
4392 wpa_s->p2p_auto_join = 0;
4393 wpa_s->pending_pd_before_join = 0;
4394 wpa_s->auto_pd_scan_retry = 0;
4395 wpas_p2p_stop_find(wpa_s);
4396 wpa_s->p2p_join_scan_count = 0;
4397 os_get_time(&wpa_s->p2p_auto_started);
4398 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4399 wpa_s->p2p_auto_started.sec,
4400 wpa_s->p2p_auto_started.usec);
4401 wpas_p2p_join_scan(wpa_s, NULL);
4405 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4406 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
4408 use == WPAS_P2P_PD_FOR_JOIN);
4411 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4414 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
4415 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4420 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4423 return p2p_scan_result_text(ies, ies_len, buf, end);
4427 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4429 if (!offchannel_pending_action_tx(wpa_s))
4432 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4433 "operation request");
4434 offchannel_clear_pending_action_tx(wpa_s);
4438 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4439 enum p2p_discovery_type type,
4440 unsigned int num_req_dev_types, const u8 *req_dev_types,
4441 const u8 *dev_id, unsigned int search_delay)
4443 wpas_p2p_clear_pending_action_tx(wpa_s);
4444 wpa_s->p2p_long_listen = 0;
4446 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4447 return wpa_drv_p2p_find(wpa_s, timeout, type);
4449 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4450 wpa_s->p2p_in_provisioning)
4453 wpa_supplicant_cancel_sched_scan(wpa_s);
4455 return p2p_find(wpa_s->global->p2p, timeout, type,
4456 num_req_dev_types, req_dev_types, dev_id,
4461 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
4463 wpas_p2p_clear_pending_action_tx(wpa_s);
4464 wpa_s->p2p_long_listen = 0;
4465 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4466 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4467 wpa_s->global->p2p_cb_on_scan_complete = 0;
4469 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4470 wpa_drv_p2p_stop_find(wpa_s);
4474 if (wpa_s->global->p2p)
4475 p2p_stop_find(wpa_s->global->p2p);
4481 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4483 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4485 wpas_p2p_remove_pending_group_interface(wpa_s);
4489 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4491 struct wpa_supplicant *wpa_s = eloop_ctx;
4492 wpa_s->p2p_long_listen = 0;
4496 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4500 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4503 wpa_supplicant_cancel_sched_scan(wpa_s);
4504 wpas_p2p_clear_pending_action_tx(wpa_s);
4508 * This is a request for unlimited Listen state. However, at
4509 * least for now, this is mapped to a Listen state for one
4514 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4515 wpa_s->p2p_long_listen = 0;
4518 * Stop previous find/listen operation to avoid trying to request a new
4519 * remain-on-channel operation while the driver is still running the
4522 if (wpa_s->global->p2p)
4523 p2p_stop_find(wpa_s->global->p2p);
4525 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4526 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4527 wpa_s->p2p_long_listen = timeout * 1000;
4528 eloop_register_timeout(timeout, 0,
4529 wpas_p2p_long_listen_timeout,
4537 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4538 u8 *buf, size_t len, int p2p_group)
4540 struct wpabuf *p2p_ie;
4543 if (wpa_s->global->p2p_disabled)
4545 if (wpa_s->global->p2p == NULL)
4550 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4551 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4553 wpabuf_free(p2p_ie);
4559 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4560 const u8 *dst, const u8 *bssid,
4561 const u8 *ie, size_t ie_len, int ssi_signal)
4563 if (wpa_s->global->p2p_disabled)
4565 if (wpa_s->global->p2p == NULL)
4568 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4570 case P2P_PREQ_NOT_P2P:
4571 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4574 case P2P_PREQ_MALFORMED:
4575 case P2P_PREQ_NOT_LISTEN:
4576 case P2P_PREQ_NOT_PROCESSED:
4577 default: /* make gcc happy */
4579 case P2P_PREQ_PROCESSED:
4585 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4586 const u8 *sa, const u8 *bssid,
4587 u8 category, const u8 *data, size_t len, int freq)
4589 if (wpa_s->global->p2p_disabled)
4591 if (wpa_s->global->p2p == NULL)
4594 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4599 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4601 if (wpa_s->global->p2p_disabled)
4603 if (wpa_s->global->p2p == NULL)
4606 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4610 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4612 p2p_group_deinit(wpa_s->p2p_group);
4613 wpa_s->p2p_group = NULL;
4615 wpa_s->ap_configured_cb = NULL;
4616 wpa_s->ap_configured_cb_ctx = NULL;
4617 wpa_s->ap_configured_cb_data = NULL;
4618 wpa_s->connect_without_scan = NULL;
4622 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4624 wpa_s->p2p_long_listen = 0;
4626 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4627 return wpa_drv_p2p_reject(wpa_s, addr);
4629 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4632 return p2p_reject(wpa_s->global->p2p, addr);
4636 /* Invite to reinvoke a persistent group */
4637 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4638 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4641 enum p2p_invite_role role;
4644 int force_freq = 0, oper_freq = 0;
4647 wpa_s->p2p_persistent_go_freq = freq;
4648 wpa_s->p2p_go_ht40 = !!ht40;
4649 if (ssid->mode == WPAS_MODE_P2P_GO) {
4650 role = P2P_INVITE_ROLE_GO;
4651 if (peer_addr == NULL) {
4652 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4653 "address in invitation command");
4656 if (wpas_p2p_create_iface(wpa_s)) {
4657 if (wpas_p2p_add_group_interface(wpa_s,
4658 WPA_IF_P2P_GO) < 0) {
4659 wpa_printf(MSG_ERROR, "P2P: Failed to "
4660 "allocate a new interface for the "
4664 bssid = wpa_s->pending_interface_addr;
4666 bssid = wpa_s->own_addr;
4668 role = P2P_INVITE_ROLE_CLIENT;
4669 peer_addr = ssid->bssid;
4671 wpa_s->pending_invite_ssid_id = ssid->id;
4674 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4676 oper_freq = wpa_s->assoc_freq;
4678 oper_freq = wpa_drv_shared_freq(wpa_s);
4684 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4685 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4686 "(%u MHz) is not supported for P2P uses",
4691 if (oper_freq > 0 && freq != oper_freq &&
4692 !(wpa_s->drv_flags &
4693 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4694 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4695 "on %u MHz while connected on another "
4696 "channel (%u MHz)", freq, oper_freq);
4699 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4700 "requested channel (%u MHz)", freq);
4702 } else if (oper_freq > 0 &&
4703 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
4704 if (!(wpa_s->drv_flags &
4705 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4706 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4707 "while connected on non-P2P supported "
4708 "channel (%u MHz)", oper_freq);
4711 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
4712 "(%u MHz) not available for P2P - try to use "
4713 "another channel", oper_freq);
4715 } else if (oper_freq > 0) {
4716 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4717 "channel we are already using (%u MHz) on another "
4718 "interface", oper_freq);
4719 force_freq = oper_freq;
4722 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4723 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4724 ssid->ssid, ssid->ssid_len,
4727 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4731 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4732 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, 1);
4734 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4735 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4740 /* Invite to join an active group */
4741 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4742 const u8 *peer_addr, const u8 *go_dev_addr)
4744 struct wpa_global *global = wpa_s->global;
4745 enum p2p_invite_role role;
4747 struct wpa_ssid *ssid;
4750 wpa_s->p2p_persistent_go_freq = 0;
4751 wpa_s->p2p_go_ht40 = 0;
4753 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4754 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4757 if (wpa_s == NULL) {
4758 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4762 ssid = wpa_s->current_ssid;
4764 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4769 persistent = ssid->p2p_persistent_group &&
4770 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4771 ssid->ssid, ssid->ssid_len);
4773 if (ssid->mode == WPAS_MODE_P2P_GO) {
4774 role = P2P_INVITE_ROLE_ACTIVE_GO;
4775 bssid = wpa_s->own_addr;
4776 if (go_dev_addr == NULL)
4777 go_dev_addr = wpa_s->global->p2p_dev_addr;
4779 role = P2P_INVITE_ROLE_CLIENT;
4780 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4781 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4782 "invite to current group");
4785 bssid = wpa_s->bssid;
4786 if (go_dev_addr == NULL &&
4787 !is_zero_ether_addr(wpa_s->go_dev_addr))
4788 go_dev_addr = wpa_s->go_dev_addr;
4790 wpa_s->parent->pending_invite_ssid_id = -1;
4792 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4793 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4794 ssid->ssid, ssid->ssid_len,
4795 go_dev_addr, persistent);
4797 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4800 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4801 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
4802 go_dev_addr, persistent);
4806 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4808 struct wpa_ssid *ssid = wpa_s->current_ssid;
4809 const char *ssid_txt;
4810 u8 go_dev_addr[ETH_ALEN];
4811 int network_id = -1;
4815 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4816 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4817 wpa_s->parent, NULL);
4820 if (!wpa_s->show_group_started || !ssid)
4823 wpa_s->show_group_started = 0;
4825 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4826 os_memset(go_dev_addr, 0, ETH_ALEN);
4827 if (ssid->bssid_set)
4828 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4829 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4831 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4833 if (wpa_s->global->p2p_group_formation == wpa_s)
4834 wpa_s->global->p2p_group_formation = NULL;
4836 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4837 (int) wpa_s->assoc_freq;
4838 if (ssid->passphrase == NULL && ssid->psk_set) {
4840 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4841 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4842 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4844 wpa_s->ifname, ssid_txt, freq, psk,
4845 MAC2STR(go_dev_addr),
4846 persistent ? " [PERSISTENT]" : "");
4848 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4849 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4850 "go_dev_addr=" MACSTR "%s",
4851 wpa_s->ifname, ssid_txt, freq,
4852 ssid->passphrase ? ssid->passphrase : "",
4853 MAC2STR(go_dev_addr),
4854 persistent ? " [PERSISTENT]" : "");
4858 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4861 network_id = ssid->id;
4862 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4865 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4866 wpa_s->global->p2p != NULL) {
4867 wpa_s->global->p2p_cb_on_scan_complete = 0;
4868 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4869 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4870 "continued after successful connection");
4871 p2p_increase_search_delay(
4873 wpas_p2p_search_delay(wpa_s));
4879 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4880 u32 interval1, u32 duration2, u32 interval2)
4882 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4884 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4887 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4888 wpa_s->current_ssid == NULL ||
4889 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4892 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4893 wpa_s->own_addr, wpa_s->assoc_freq,
4894 duration1, interval1, duration2, interval2);
4898 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4899 unsigned int interval)
4901 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4904 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4907 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4911 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4913 if (wpa_s->current_ssid == NULL) {
4915 * current_ssid can be cleared when P2P client interface gets
4916 * disconnected, so assume this interface was used as P2P
4921 return wpa_s->current_ssid->p2p_group &&
4922 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4926 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4928 struct wpa_supplicant *wpa_s = eloop_ctx;
4930 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4931 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4936 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4938 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
4942 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4946 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4947 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4949 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4952 timeout = wpa_s->conf->p2p_group_idle;
4953 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4954 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4955 timeout = P2P_MAX_CLIENT_IDLE;
4961 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4962 timeout = 0; /* special client mode no-timeout */
4967 if (wpa_s->p2p_in_provisioning) {
4969 * Use the normal group formation timeout during the
4970 * provisioning phase to avoid terminating this process too
4971 * early due to group idle timeout.
4973 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4974 "during provisioning");
4978 if (wpa_s->show_group_started) {
4980 * Use the normal group formation timeout between the end of
4981 * the provisioning phase and completion of 4-way handshake to
4982 * avoid terminating this process too early due to group idle
4985 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4986 "while waiting for initial 4-way handshake to "
4992 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4994 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4999 /* Returns 1 if the interface was removed */
5000 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5001 u16 reason_code, const u8 *ie, size_t ie_len,
5002 int locally_generated)
5004 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5006 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5009 if (!locally_generated)
5010 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5013 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5014 wpa_s->current_ssid &&
5015 wpa_s->current_ssid->p2p_group &&
5016 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5017 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5018 "session is ending");
5019 if (wpas_p2p_group_delete(wpa_s,
5020 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5029 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5030 u16 reason_code, const u8 *ie, size_t ie_len,
5031 int locally_generated)
5033 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5035 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5038 if (!locally_generated)
5039 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5044 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5046 struct p2p_data *p2p = wpa_s->global->p2p;
5051 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5054 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5055 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5057 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5058 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5061 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5062 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5064 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5065 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5067 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5068 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5069 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5070 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5071 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5074 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5075 p2p_set_sec_dev_types(p2p,
5076 (void *) wpa_s->conf->sec_device_type,
5077 wpa_s->conf->num_sec_device_types);
5079 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5081 p2p_remove_wps_vendor_extensions(p2p);
5082 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5083 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5085 p2p_add_wps_vendor_extension(
5086 p2p, wpa_s->conf->wps_vendor_ext[i]);
5090 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5091 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5093 country[0] = wpa_s->conf->country[0];
5094 country[1] = wpa_s->conf->country[1];
5096 p2p_set_country(p2p, country);
5099 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5100 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5101 wpa_s->conf->p2p_ssid_postfix ?
5102 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5106 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5107 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
5109 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5110 u8 reg_class, channel;
5113 if (wpa_s->conf->p2p_listen_reg_class &&
5114 wpa_s->conf->p2p_listen_channel) {
5115 reg_class = wpa_s->conf->p2p_listen_reg_class;
5116 channel = wpa_s->conf->p2p_listen_channel;
5120 * Pick one of the social channels randomly as the
5123 os_get_random((u8 *) &r, sizeof(r));
5124 channel = 1 + (r % 3) * 5;
5126 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5128 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5131 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5132 u8 op_reg_class, op_channel, cfg_op_channel;
5135 if (wpa_s->conf->p2p_oper_reg_class &&
5136 wpa_s->conf->p2p_oper_channel) {
5137 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5138 op_channel = wpa_s->conf->p2p_oper_channel;
5143 * Use random operation channel from (1, 6, 11)
5144 *if no other preference is indicated.
5146 os_get_random((u8 *) &r, sizeof(r));
5147 op_channel = 1 + (r % 3) * 5;
5150 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5153 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5157 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5158 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5159 wpa_s->conf->p2p_pref_chan) < 0) {
5160 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5167 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5170 if (!wpa_s->ap_iface)
5172 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5177 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5179 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5181 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5184 wpa_s->global->cross_connection = enabled;
5185 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5188 struct wpa_supplicant *iface;
5190 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5192 if (iface->cross_connect_enabled == 0)
5195 iface->cross_connect_enabled = 0;
5196 iface->cross_connect_in_use = 0;
5197 wpa_msg(iface->parent, MSG_INFO,
5198 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5199 iface->ifname, iface->cross_connect_uplink);
5207 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5209 struct wpa_supplicant *iface;
5211 if (!uplink->global->cross_connection)
5214 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5215 if (!iface->cross_connect_enabled)
5217 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5220 if (iface->ap_iface == NULL)
5222 if (iface->cross_connect_in_use)
5225 iface->cross_connect_in_use = 1;
5226 wpa_msg(iface->parent, MSG_INFO,
5227 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5228 iface->ifname, iface->cross_connect_uplink);
5233 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5235 struct wpa_supplicant *iface;
5237 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5238 if (!iface->cross_connect_enabled)
5240 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5243 if (!iface->cross_connect_in_use)
5246 wpa_msg(iface->parent, MSG_INFO,
5247 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5248 iface->ifname, iface->cross_connect_uplink);
5249 iface->cross_connect_in_use = 0;
5254 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5256 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5257 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5258 wpa_s->cross_connect_disallowed)
5259 wpas_p2p_disable_cross_connect(wpa_s);
5261 wpas_p2p_enable_cross_connect(wpa_s);
5262 if (!wpa_s->ap_iface &&
5263 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5264 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5268 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5270 wpas_p2p_disable_cross_connect(wpa_s);
5271 if (!wpa_s->ap_iface &&
5272 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5274 wpas_p2p_set_group_idle_timeout(wpa_s);
5278 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5280 struct wpa_supplicant *iface;
5282 if (!wpa_s->global->cross_connection)
5285 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5288 if (iface->drv_flags &
5289 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5291 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5294 wpa_s->cross_connect_enabled = 1;
5295 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5296 sizeof(wpa_s->cross_connect_uplink));
5297 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5298 "%s to %s whenever uplink is available",
5299 wpa_s->ifname, wpa_s->cross_connect_uplink);
5301 if (iface->ap_iface || iface->current_ssid == NULL ||
5302 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5303 iface->cross_connect_disallowed ||
5304 iface->wpa_state != WPA_COMPLETED)
5307 wpa_s->cross_connect_in_use = 1;
5308 wpa_msg(wpa_s->parent, MSG_INFO,
5309 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5310 wpa_s->ifname, wpa_s->cross_connect_uplink);
5316 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5318 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5319 !wpa_s->p2p_in_provisioning)
5320 return 0; /* not P2P client operation */
5322 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5324 if (wpa_s != wpa_s->parent)
5325 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5327 if (wpa_s->global->p2p)
5328 p2p_group_formation_failed(wpa_s->global->p2p);
5330 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5331 wpa_s->parent, NULL);
5333 wpas_group_formation_completed(wpa_s, 0);
5338 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5340 struct p2p_channels chan;
5342 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5345 os_memset(&chan, 0, sizeof(chan));
5346 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5347 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5352 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5356 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5357 struct wpa_scan_results *scan_res)
5359 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5363 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5365 struct wpa_global *global = wpa_s->global;
5369 if (global->p2p == NULL)
5372 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5374 if (wpa_s->pending_interface_name[0] &&
5375 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5378 peer = p2p_get_go_neg_peer(global->p2p);
5380 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5381 MACSTR, MAC2STR(peer));
5382 p2p_unauthorize(global->p2p, peer);
5386 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5387 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5388 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5392 if (wpa_s->pending_pd_before_join) {
5393 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5394 wpa_s->pending_pd_before_join = 0;
5398 wpas_p2p_stop_find(wpa_s);
5400 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5401 if (wpa_s == global->p2p_group_formation &&
5402 (wpa_s->p2p_in_provisioning ||
5403 wpa_s->parent->pending_interface_type ==
5404 WPA_IF_P2P_CLIENT)) {
5405 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5406 "formation found - cancelling",
5409 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5410 wpa_s->parent, NULL);
5411 if (wpa_s->p2p_in_provisioning) {
5412 wpas_group_formation_completed(wpa_s, 0);
5415 wpas_p2p_group_delete(wpa_s,
5416 P2P_GROUP_REMOVAL_REQUESTED);
5422 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5430 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5432 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5435 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5436 "being available anymore");
5437 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
5441 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5442 int freq_24, int freq_5, int freq_overall)
5444 struct p2p_data *p2p = wpa_s->global->p2p;
5445 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5447 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5451 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5454 struct p2p_data *p2p = wpa_s->global->p2p;
5456 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5459 if (hwaddr_aton(addr, peer))
5462 return p2p_unauthorize(p2p, peer);
5467 * wpas_p2p_disconnect - Disconnect from a P2P Group
5468 * @wpa_s: Pointer to wpa_supplicant data
5469 * Returns: 0 on success, -1 on failure
5471 * This can be used to disconnect from a group in which the local end is a P2P
5472 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5473 * virtual network interface was created for this group, that interface will be
5474 * removed. Otherwise, only the configured P2P group network will be removed
5475 * from the interface.
5477 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5483 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5488 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5490 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5493 return p2p_in_progress(wpa_s->global->p2p);
5497 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5498 struct wpa_ssid *ssid)
5500 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5501 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5502 wpa_s->parent, NULL) > 0) {
5504 * Remove the network by scheduling the group formation
5505 * timeout to happen immediately. The teardown code
5506 * needs to be scheduled to run asynch later so that we
5507 * don't delete data from under ourselves unexpectedly.
5508 * Calling wpas_p2p_group_formation_timeout directly
5509 * causes a series of crashes in WPS failure scenarios.
5511 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5512 "P2P group network getting removed");
5513 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5514 wpa_s->parent, NULL);
5519 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
5520 const u8 *addr, const u8 *ssid,
5526 for (s = wpa_s->conf->ssid; s; s = s->next) {
5527 if (s->disabled != 2)
5530 (ssid_len != s->ssid_len ||
5531 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5533 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5534 return s; /* peer is GO in the persistent group */
5535 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5537 for (i = 0; i < s->num_p2p_clients; i++) {
5538 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5539 addr, ETH_ALEN) == 0)
5540 return s; /* peer is P2P client in persistent
5549 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5554 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5558 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5561 struct wpa_supplicant *group = wpa_s;
5562 if (wpa_s->global->p2p_group_formation)
5563 group = wpa_s->global->p2p_group_formation;
5564 wpa_s = wpa_s->parent;
5565 offchannel_send_action_done(wpa_s);
5567 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
5568 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5569 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5570 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5571 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5572 wpa_s->p2p_persistent_id,
5573 wpa_s->p2p_pd_before_go_neg,
5574 wpa_s->p2p_go_ht40);
5578 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5580 if (!wpa_s->p2p_fallback_to_go_neg ||
5581 wpa_s->p2p_in_provisioning <= 5)
5584 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5585 return 0; /* peer operating as a GO */
5587 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5588 "fallback to GO Negotiation");
5589 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5595 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5597 const char *rn, *rn2;
5598 struct wpa_supplicant *ifs;
5600 if (wpa_s->wpa_state > WPA_SCANNING) {
5601 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5602 "concurrent operation",
5603 P2P_CONCURRENT_SEARCH_DELAY);
5604 return P2P_CONCURRENT_SEARCH_DELAY;
5607 if (!wpa_s->driver->get_radio_name)
5609 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5610 if (rn == NULL || rn[0] == '\0')
5613 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5614 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5617 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5618 if (!rn2 || os_strcmp(rn, rn2) != 0)
5620 if (ifs->wpa_state > WPA_SCANNING) {
5621 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5622 "delay due to concurrent operation on "
5624 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5625 return P2P_CONCURRENT_SEARCH_DELAY;
5633 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5634 struct wpa_ssid *ssid)
5636 struct wpa_supplicant *iface = NULL;
5637 struct p2p_data *p2p = wpa_s->global->p2p;
5639 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5640 if ((iface->current_ssid) &&
5641 (iface->current_ssid->frequency != freq) &&
5642 ((iface->p2p_group_interface) ||
5643 (iface->current_ssid->p2p_group))) {
5645 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5646 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5647 /* Try to see whether we can move the GO. If it
5648 * is not possible, remove the GO interface
5650 if (wpa_drv_switch_channel(iface, freq) == 0) {
5651 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5652 iface->current_ssid->frequency = freq;
5657 /* If GO cannot be moved or if the conflicting interface is a
5658 * P2P Client, remove the interface depending up on the connection
5660 if(!wpas_is_p2p_prioritized(iface)) {
5661 /* STA connection has priority over existing
5662 * P2P connection. So remove the interface */
5663 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5664 "concurrent mode frequency conflict");
5665 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
5666 /* If connection in progress is p2p connection, do not proceed for the connection */
5670 /* If connection in progress is STA connection, proceed for the connection */
5673 /* P2p connection has priority, disable the STA network*/
5674 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5675 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5676 " id=%d", ssid->id);
5677 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5678 if (wpa_s == iface) {
5679 /* p2p connection is in progress, continue connecting...*/
5683 /* STA connection is in progress, do not allow to continue */