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
49 #define P2P_MAX_CLIENT_IDLE 10
50 #endif /* P2P_MAX_CLIENT_IDLE */
52 #ifndef P2P_MAX_INITIAL_CONN_WAIT
54 * How many seconds to wait for initial 4-way handshake to get completed after
55 * WPS provisioning step.
57 #define P2P_MAX_INITIAL_CONN_WAIT 10
58 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
60 #ifndef P2P_CONCURRENT_SEARCH_DELAY
61 #define P2P_CONCURRENT_SEARCH_DELAY 500
62 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
64 enum p2p_group_removal_reason {
65 P2P_GROUP_REMOVAL_UNKNOWN,
66 P2P_GROUP_REMOVAL_SILENT,
67 P2P_GROUP_REMOVAL_FORMATION_FAILED,
68 P2P_GROUP_REMOVAL_REQUESTED,
69 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
70 P2P_GROUP_REMOVAL_UNAVAILABLE,
71 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
75 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
76 static struct wpa_supplicant *
77 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
79 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
80 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
81 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
82 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
83 const u8 *dev_addr, enum p2p_wps_method wps_method,
85 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
86 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
87 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
88 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
89 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
91 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
94 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
95 struct wpa_scan_results *scan_res)
99 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
102 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
103 (int) scan_res->num);
105 for (i = 0; i < scan_res->num; i++) {
106 struct wpa_scan_res *bss = scan_res->res[i];
107 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
108 bss->freq, bss->level,
109 (const u8 *) (bss + 1),
114 p2p_scan_res_handled(wpa_s->global->p2p);
118 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
119 unsigned int num_req_dev_types,
120 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
122 struct wpa_supplicant *wpa_s = ctx;
123 struct wpa_supplicant *ifs;
124 struct wpa_driver_scan_params params;
126 struct wpabuf *wps_ie, *ies;
127 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
130 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
133 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
134 if (ifs->sta_scan_pending &&
135 wpas_p2p_in_progress(wpa_s) == 2) {
136 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
137 "pending station mode scan to be "
138 "completed on interface %s", ifs->ifname);
139 wpa_s->global->p2p_cb_on_scan_complete = 1;
140 wpa_supplicant_req_scan(ifs, 0, 0);
145 os_memset(¶ms, 0, sizeof(params));
147 /* P2P Wildcard SSID */
148 params.num_ssids = 1;
149 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
150 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
152 wpa_s->wps->dev.p2p = 1;
153 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
154 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
155 num_req_dev_types, req_dev_types);
159 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
160 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
165 wpabuf_put_buf(ies, wps_ie);
168 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
170 params.p2p_probe = 1;
171 params.extra_ies = wpabuf_head(ies);
172 params.extra_ies_len = wpabuf_len(ies);
175 case P2P_SCAN_SOCIAL:
176 params.freqs = social_channels;
180 case P2P_SCAN_SOCIAL_PLUS_ONE:
181 social_channels[3] = freq;
182 params.freqs = social_channels;
186 ret = wpa_drv_scan(wpa_s, ¶ms);
191 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
193 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
194 wpa_s->global->p2p_cb_on_scan_complete = 1;
200 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
206 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
208 switch (p2p_group_interface) {
209 case P2P_GROUP_INTERFACE_PENDING:
210 return WPA_IF_P2P_GROUP;
211 case P2P_GROUP_INTERFACE_GO:
212 return WPA_IF_P2P_GO;
213 case P2P_GROUP_INTERFACE_CLIENT:
214 return WPA_IF_P2P_CLIENT;
217 return WPA_IF_P2P_GROUP;
221 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
223 size_t ssid_len, int *go)
227 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
228 for (s = wpa_s->conf->ssid; s; s = s->next) {
229 if (s->disabled != 0 || !s->p2p_group ||
230 s->ssid_len != ssid_len ||
231 os_memcmp(ssid, s->ssid, ssid_len) != 0)
233 if (s->mode == WPAS_MODE_P2P_GO &&
234 s != wpa_s->current_ssid)
237 *go = s->mode == WPAS_MODE_P2P_GO;
246 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
247 enum p2p_group_removal_reason removal_reason)
249 struct wpa_ssid *ssid;
253 ssid = wpa_s->current_ssid;
256 * The current SSID was not known, but there may still be a
257 * pending P2P group interface waiting for provisioning or a
258 * P2P group that is trying to reconnect.
260 ssid = wpa_s->conf->ssid;
262 if (ssid->p2p_group && ssid->disabled != 2)
267 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
269 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
274 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
276 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
277 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
278 wpa_s->reassociate = 0;
279 wpa_s->disconnected = 1;
280 wpa_supplicant_deauthenticate(wpa_s,
281 WLAN_REASON_DEAUTH_LEAVING);
285 if (wpa_s->cross_connect_in_use) {
286 wpa_s->cross_connect_in_use = 0;
287 wpa_msg(wpa_s->parent, MSG_INFO,
288 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
289 wpa_s->ifname, wpa_s->cross_connect_uplink);
291 switch (removal_reason) {
292 case P2P_GROUP_REMOVAL_REQUESTED:
293 reason = " reason=REQUESTED";
295 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
296 reason = " reason=FORMATION_FAILED";
298 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
299 reason = " reason=IDLE";
301 case P2P_GROUP_REMOVAL_UNAVAILABLE:
302 reason = " reason=UNAVAILABLE";
304 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
305 reason = " reason=GO_ENDING_SESSION";
311 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
312 wpa_msg(wpa_s->parent, MSG_INFO,
313 P2P_EVENT_GROUP_REMOVED "%s %s%s",
314 wpa_s->ifname, gtype, reason);
317 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
318 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
320 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
321 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
323 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
324 struct wpa_global *global;
326 enum wpa_driver_if_type type;
327 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
329 global = wpa_s->global;
330 ifname = os_strdup(wpa_s->ifname);
331 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
332 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
333 wpa_s = global->ifaces;
335 wpa_drv_if_remove(wpa_s, type, ifname);
340 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
341 if (ssid && (ssid->p2p_group ||
342 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
343 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
345 if (ssid == wpa_s->current_ssid) {
346 wpa_sm_set_config(wpa_s->wpa, NULL);
347 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
348 wpa_s->current_ssid = NULL;
351 * Networks objects created during any P2P activities are not
352 * exposed out as they might/will confuse certain non-P2P aware
353 * applications since these network objects won't behave like
356 * Likewise, we don't send out network removed signals for such
359 wpa_config_remove_network(wpa_s->conf, id);
360 wpa_supplicant_clear_status(wpa_s);
361 wpa_supplicant_cancel_sched_scan(wpa_s);
362 wpa_s->sta_scan_pending = 0;
364 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
368 wpa_supplicant_ap_deinit(wpa_s);
370 wpa_drv_deinit_p2p_cli(wpa_s);
376 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
378 const u8 *ssid, size_t ssid_len)
386 if (wpa_s->go_params)
387 bssid = wpa_s->go_params->peer_interface_addr;
389 bssid = wpa_s->bssid;
391 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
393 u8 iface_addr[ETH_ALEN];
394 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
396 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
399 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
400 "group is persistent - BSS " MACSTR " not found",
405 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
407 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
408 "group is persistent - BSS " MACSTR
409 " did not include P2P IE", MAC2STR(bssid));
410 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
411 (u8 *) (bss + 1), bss->ie_len);
412 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
413 ((u8 *) bss + 1) + bss->ie_len,
418 group_capab = p2p_get_group_capab(p2p);
419 addr = p2p_get_go_dev_addr(p2p);
420 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
421 "group_capab=0x%x", group_capab);
423 os_memcpy(go_dev_addr, addr, ETH_ALEN);
424 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
427 os_memset(go_dev_addr, 0, ETH_ALEN);
430 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
431 "go_dev_addr=" MACSTR,
432 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
434 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
438 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
439 struct wpa_ssid *ssid,
440 const u8 *go_dev_addr)
445 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
446 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
447 for (s = wpa_s->conf->ssid; s; s = s->next) {
448 if (s->disabled == 2 &&
449 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
450 s->ssid_len == ssid->ssid_len &&
451 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
456 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
458 if (ssid->passphrase && !s->passphrase)
460 else if (ssid->passphrase && s->passphrase &&
461 os_strcmp(ssid->passphrase, s->passphrase) != 0)
464 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
467 s = wpa_config_add_network(wpa_s->conf);
472 * Instead of network_added we emit persistent_group_added
473 * notification. Also to keep the defense checks in
474 * persistent_group obj registration method, we set the
475 * relevant flags in s to designate it as a persistent group.
478 s->p2p_persistent_group = 1;
479 wpas_notify_persistent_group_added(wpa_s, s);
480 wpa_config_set_network_defaults(s);
484 s->p2p_persistent_group = 1;
487 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
488 s->mode = ssid->mode;
489 s->auth_alg = WPA_AUTH_ALG_OPEN;
490 s->key_mgmt = WPA_KEY_MGMT_PSK;
491 s->proto = WPA_PROTO_RSN;
492 s->pairwise_cipher = WPA_CIPHER_CCMP;
494 if (ssid->passphrase) {
495 os_free(s->passphrase);
496 s->passphrase = os_strdup(ssid->passphrase);
500 os_memcpy(s->psk, ssid->psk, 32);
502 if (s->passphrase && !s->psk_set)
503 wpa_config_update_psk(s);
504 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
506 s->ssid = os_malloc(ssid->ssid_len);
509 s->ssid_len = ssid->ssid_len;
510 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
513 #ifndef CONFIG_NO_CONFIG_WRITE
514 if (changed && wpa_s->conf->update_config &&
515 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
516 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
518 #endif /* CONFIG_NO_CONFIG_WRITE */
524 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
527 struct wpa_ssid *ssid, *s;
532 ssid = wpa_s->current_ssid;
533 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
534 !ssid->p2p_persistent_group)
537 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
538 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
541 if (s->ssid_len == ssid->ssid_len &&
542 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
549 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
550 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
554 if (i == s->num_p2p_clients - 1)
555 return; /* already the most recent entry */
557 /* move the entry to mark it most recent */
558 os_memmove(s->p2p_client_list + i * ETH_ALEN,
559 s->p2p_client_list + (i + 1) * ETH_ALEN,
560 (s->num_p2p_clients - i - 1) * ETH_ALEN);
561 os_memcpy(s->p2p_client_list +
562 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
567 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
568 n = os_realloc_array(s->p2p_client_list,
569 s->num_p2p_clients + 1, ETH_ALEN);
572 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
573 s->p2p_client_list = n;
574 s->num_p2p_clients++;
576 /* Not enough room for an additional entry - drop the oldest
578 os_memmove(s->p2p_client_list,
579 s->p2p_client_list + ETH_ALEN,
580 (s->num_p2p_clients - 1) * ETH_ALEN);
581 os_memcpy(s->p2p_client_list +
582 (s->num_p2p_clients - 1) * ETH_ALEN,
586 #ifndef CONFIG_NO_CONFIG_WRITE
587 if (wpa_s->parent->conf->update_config &&
588 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
589 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
590 #endif /* CONFIG_NO_CONFIG_WRITE */
594 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
597 struct wpa_ssid *ssid;
598 const char *ssid_txt;
601 u8 go_dev_addr[ETH_ALEN];
605 * This callback is likely called for the main interface. Update wpa_s
606 * to use the group interface if a new interface was created for the
609 if (wpa_s->global->p2p_group_formation)
610 wpa_s = wpa_s->global->p2p_group_formation;
611 wpa_s->global->p2p_group_formation = NULL;
612 wpa_s->p2p_in_provisioning = 0;
615 wpa_msg(wpa_s->parent, MSG_INFO,
616 P2P_EVENT_GROUP_FORMATION_FAILURE);
617 wpas_p2p_group_delete(wpa_s,
618 P2P_GROUP_REMOVAL_FORMATION_FAILED);
622 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
624 ssid = wpa_s->current_ssid;
625 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
626 ssid->mode = WPAS_MODE_P2P_GO;
627 p2p_group_notif_formation_done(wpa_s->p2p_group);
628 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
633 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
634 client = ssid->mode == WPAS_MODE_INFRA;
635 if (ssid->mode == WPAS_MODE_P2P_GO) {
636 persistent = ssid->p2p_persistent_group;
637 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
640 persistent = wpas_p2p_persistent_group(wpa_s,
646 client = wpa_s->p2p_group_interface ==
647 P2P_GROUP_INTERFACE_CLIENT;
648 os_memset(go_dev_addr, 0, ETH_ALEN);
651 wpa_s->show_group_started = 0;
654 * Indicate event only after successfully completed 4-way
655 * handshake, i.e., when the interface is ready for data
658 wpa_s->show_group_started = 1;
659 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
661 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
662 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
663 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
665 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
666 MAC2STR(go_dev_addr),
667 persistent ? " [PERSISTENT]" : "");
668 wpas_p2p_cross_connect_setup(wpa_s);
669 wpas_p2p_set_group_idle_timeout(wpa_s);
671 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
672 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
673 "go_dev_addr=" MACSTR "%s",
674 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
675 ssid && ssid->passphrase ? ssid->passphrase : "",
676 MAC2STR(go_dev_addr),
677 persistent ? " [PERSISTENT]" : "");
678 wpas_p2p_cross_connect_setup(wpa_s);
679 wpas_p2p_set_group_idle_timeout(wpa_s);
683 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
685 if (network_id < 0 && ssid)
686 network_id = ssid->id;
688 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
692 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
694 const u8 *dst, const u8 *src,
696 const u8 *data, size_t data_len,
697 enum offchannel_send_action_result
700 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
702 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
704 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
708 case OFFCHANNEL_SEND_ACTION_SUCCESS:
709 res = P2P_SEND_ACTION_SUCCESS;
711 case OFFCHANNEL_SEND_ACTION_NO_ACK:
712 res = P2P_SEND_ACTION_NO_ACK;
714 case OFFCHANNEL_SEND_ACTION_FAILED:
715 res = P2P_SEND_ACTION_FAILED;
719 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
721 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
722 wpa_s->pending_pd_before_join &&
723 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
724 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
725 wpa_s->p2p_fallback_to_go_neg) {
726 wpa_s->pending_pd_before_join = 0;
727 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
728 "during p2p_connect-auto");
729 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
735 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
736 const u8 *src, const u8 *bssid, const u8 *buf,
737 size_t len, unsigned int wait_time)
739 struct wpa_supplicant *wpa_s = ctx;
740 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
742 wpas_p2p_send_action_tx_status, 1);
746 static void wpas_send_action_done(void *ctx)
748 struct wpa_supplicant *wpa_s = ctx;
749 offchannel_send_action_done(wpa_s);
753 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
754 struct p2p_go_neg_results *params)
756 if (wpa_s->go_params == NULL) {
757 wpa_s->go_params = os_malloc(sizeof(*params));
758 if (wpa_s->go_params == NULL)
761 os_memcpy(wpa_s->go_params, params, sizeof(*params));
766 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
767 struct p2p_go_neg_results *res)
769 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
770 MAC2STR(res->peer_interface_addr));
771 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
772 res->ssid, res->ssid_len);
773 wpa_supplicant_ap_deinit(wpa_s);
774 wpas_copy_go_neg_results(wpa_s, res);
775 if (res->wps_method == WPS_PBC)
776 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
778 u16 dev_pw_id = DEV_PW_DEFAULT;
779 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
780 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
781 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
782 wpa_s->p2p_pin, 1, dev_pw_id);
787 static void p2p_go_configured(void *ctx, void *data)
789 struct wpa_supplicant *wpa_s = ctx;
790 struct p2p_go_neg_results *params = data;
791 struct wpa_ssid *ssid;
794 ssid = wpa_s->current_ssid;
795 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
796 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
797 if (wpa_s->global->p2p_group_formation == wpa_s)
798 wpa_s->global->p2p_group_formation = NULL;
799 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
800 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
801 "go_dev_addr=" MACSTR "%s",
803 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
805 params->passphrase ? params->passphrase : "",
806 MAC2STR(wpa_s->global->p2p_dev_addr),
807 params->persistent_group ? " [PERSISTENT]" : "");
809 if (params->persistent_group)
810 network_id = wpas_p2p_store_persistent_group(
812 wpa_s->global->p2p_dev_addr);
814 network_id = ssid->id;
815 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
816 wpas_p2p_cross_connect_setup(wpa_s);
817 wpas_p2p_set_group_idle_timeout(wpa_s);
821 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
822 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
823 params->peer_interface_addr)) {
824 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
828 if (params->wps_method == WPS_PBC)
829 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
830 params->peer_device_addr);
831 else if (wpa_s->p2p_pin[0])
832 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
833 wpa_s->p2p_pin, NULL, 0, 0);
834 os_free(wpa_s->go_params);
835 wpa_s->go_params = NULL;
839 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
840 struct p2p_go_neg_results *params,
843 struct wpa_ssid *ssid;
845 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
846 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
847 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
852 ssid = wpa_config_add_network(wpa_s->conf);
854 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
858 wpa_s->show_group_started = 0;
860 wpa_config_set_network_defaults(ssid);
863 ssid->p2p_persistent_group = params->persistent_group;
864 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
866 ssid->frequency = params->freq;
867 ssid->ht40 = params->ht40;
868 ssid->ssid = os_zalloc(params->ssid_len + 1);
870 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
871 ssid->ssid_len = params->ssid_len;
873 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
874 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
875 ssid->proto = WPA_PROTO_RSN;
876 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
877 ssid->passphrase = os_strdup(params->passphrase);
878 if (ssid->passphrase == NULL) {
879 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy passphrase for "
881 wpa_config_remove_network(wpa_s->conf, ssid->id);
884 ssid->psk_set = params->psk_set;
886 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
888 wpa_config_update_psk(ssid);
889 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
891 wpa_s->ap_configured_cb = p2p_go_configured;
892 wpa_s->ap_configured_cb_ctx = wpa_s;
893 wpa_s->ap_configured_cb_data = wpa_s->go_params;
894 wpa_s->connect_without_scan = ssid;
895 wpa_s->reassociate = 1;
896 wpa_s->disconnected = 0;
897 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
899 wpa_supplicant_req_scan(wpa_s, 0, 0);
903 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
904 const struct wpa_supplicant *src)
906 struct wpa_config *d;
907 const struct wpa_config *s;
912 #define C(n) if (s->n) d->n = os_strdup(s->n)
921 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
922 os_memcpy(d->sec_device_type, s->sec_device_type,
923 sizeof(d->sec_device_type));
924 d->num_sec_device_types = s->num_sec_device_types;
926 d->p2p_group_idle = s->p2p_group_idle;
927 d->p2p_intra_bss = s->p2p_intra_bss;
928 d->persistent_reconnect = s->persistent_reconnect;
929 d->max_num_sta = s->max_num_sta;
930 d->pbc_in_m1 = s->pbc_in_m1;
934 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
935 enum wpa_driver_if_type type)
937 char ifname[120], force_ifname[120];
939 if (wpa_s->pending_interface_name[0]) {
940 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
941 "- skip creation of a new one");
942 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
943 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
944 "unknown?! ifname='%s'",
945 wpa_s->pending_interface_name);
951 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
952 wpa_s->p2p_group_idx);
953 if (os_strlen(ifname) >= IFNAMSIZ &&
954 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
955 /* Try to avoid going over the IFNAMSIZ length limit */
956 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
957 wpa_s->p2p_group_idx);
959 force_ifname[0] = '\0';
961 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
963 wpa_s->p2p_group_idx++;
965 wpa_s->pending_interface_type = type;
966 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
967 wpa_s->pending_interface_addr, NULL) < 0) {
968 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
973 if (force_ifname[0]) {
974 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
976 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
977 sizeof(wpa_s->pending_interface_name));
979 os_strlcpy(wpa_s->pending_interface_name, ifname,
980 sizeof(wpa_s->pending_interface_name));
981 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
982 MACSTR, wpa_s->pending_interface_name,
983 MAC2STR(wpa_s->pending_interface_addr));
989 static void wpas_p2p_remove_pending_group_interface(
990 struct wpa_supplicant *wpa_s)
992 if (!wpa_s->pending_interface_name[0] ||
993 is_zero_ether_addr(wpa_s->pending_interface_addr))
994 return; /* No pending virtual interface */
996 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
997 wpa_s->pending_interface_name);
998 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
999 wpa_s->pending_interface_name);
1000 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1001 wpa_s->pending_interface_name[0] = '\0';
1005 static struct wpa_supplicant *
1006 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1008 struct wpa_interface iface;
1009 struct wpa_supplicant *group_wpa_s;
1011 if (!wpa_s->pending_interface_name[0]) {
1012 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1013 if (!wpas_p2p_create_iface(wpa_s))
1016 * Something has forced us to remove the pending interface; try
1017 * to create a new one and hope for the best that we will get
1018 * the same local address.
1020 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1021 WPA_IF_P2P_CLIENT) < 0)
1025 os_memset(&iface, 0, sizeof(iface));
1026 iface.ifname = wpa_s->pending_interface_name;
1027 iface.driver = wpa_s->driver->name;
1028 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1029 iface.driver_param = wpa_s->conf->driver_param;
1030 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1031 if (group_wpa_s == NULL) {
1032 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1033 "wpa_supplicant interface");
1036 wpa_s->pending_interface_name[0] = '\0';
1037 group_wpa_s->parent = wpa_s;
1038 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1039 P2P_GROUP_INTERFACE_CLIENT;
1040 wpa_s->global->p2p_group_formation = group_wpa_s;
1042 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1048 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1051 struct wpa_supplicant *wpa_s = eloop_ctx;
1052 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1053 if (wpa_s->global->p2p)
1054 p2p_group_formation_failed(wpa_s->global->p2p);
1055 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1056 wpa_drv_p2p_group_formation_failed(wpa_s);
1057 wpas_group_formation_completed(wpa_s, 0);
1061 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1063 struct wpa_supplicant *wpa_s = ctx;
1065 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1066 wpa_drv_cancel_remain_on_channel(wpa_s);
1067 wpa_s->off_channel_freq = 0;
1068 wpa_s->roc_waiting_drv_freq = 0;
1072 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1074 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1075 wpas_p2p_remove_pending_group_interface(wpa_s);
1079 if (wpa_s->p2p_go_ht40)
1082 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1083 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1085 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1086 struct wpa_ssid *ssid;
1087 ssid = wpa_config_get_network(wpa_s->conf,
1088 wpa_s->p2p_persistent_id);
1089 if (ssid && ssid->disabled == 2 &&
1090 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1091 size_t len = os_strlen(ssid->passphrase);
1092 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1093 "on requested persistent group");
1094 os_memcpy(res->passphrase, ssid->passphrase, len);
1095 res->passphrase[len] = '\0';
1099 if (wpa_s->create_p2p_iface) {
1100 struct wpa_supplicant *group_wpa_s =
1101 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1102 if (group_wpa_s == NULL) {
1103 wpas_p2p_remove_pending_group_interface(wpa_s);
1106 if (group_wpa_s != wpa_s) {
1107 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1108 sizeof(group_wpa_s->p2p_pin));
1109 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1111 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1112 wpa_s->pending_interface_name[0] = '\0';
1113 group_wpa_s->p2p_in_provisioning = 1;
1116 wpas_start_wps_go(group_wpa_s, res, 1);
1118 wpas_start_wps_enrollee(group_wpa_s, res);
1120 wpa_s->p2p_in_provisioning = 1;
1121 wpa_s->global->p2p_group_formation = wpa_s;
1124 wpas_start_wps_go(wpa_s, res, 1);
1126 wpas_start_wps_enrollee(ctx, res);
1129 wpa_s->p2p_long_listen = 0;
1130 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1132 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1133 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1134 (res->peer_config_timeout % 100) * 10000,
1135 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1139 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1141 struct wpa_supplicant *wpa_s = ctx;
1142 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1143 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1145 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1149 void wpas_dev_found(void *ctx, const u8 *addr,
1150 const struct p2p_peer_info *info,
1153 #ifndef CONFIG_NO_STDOUT_DEBUG
1154 struct wpa_supplicant *wpa_s = ctx;
1155 char devtype[WPS_DEV_TYPE_BUFSIZE];
1157 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1158 " p2p_dev_addr=" MACSTR
1159 " pri_dev_type=%s name='%s' config_methods=0x%x "
1160 "dev_capab=0x%x group_capab=0x%x",
1161 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1162 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1164 info->device_name, info->config_methods,
1165 info->dev_capab, info->group_capab);
1166 #endif /* CONFIG_NO_STDOUT_DEBUG */
1168 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1172 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1174 struct wpa_supplicant *wpa_s = ctx;
1176 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1177 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1179 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1183 static int wpas_start_listen(void *ctx, unsigned int freq,
1184 unsigned int duration,
1185 const struct wpabuf *probe_resp_ie)
1187 struct wpa_supplicant *wpa_s = ctx;
1189 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1191 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1192 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1193 "report received Probe Request frames");
1197 wpa_s->pending_listen_freq = freq;
1198 wpa_s->pending_listen_duration = duration;
1200 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1201 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1202 "to remain on channel (%u MHz) for Listen "
1204 wpa_s->pending_listen_freq = 0;
1207 wpa_s->off_channel_freq = 0;
1208 wpa_s->roc_waiting_drv_freq = freq;
1214 static void wpas_stop_listen(void *ctx)
1216 struct wpa_supplicant *wpa_s = ctx;
1217 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1218 wpa_drv_cancel_remain_on_channel(wpa_s);
1219 wpa_s->off_channel_freq = 0;
1220 wpa_s->roc_waiting_drv_freq = 0;
1222 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1223 wpa_drv_probe_req_report(wpa_s, 0);
1227 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1229 struct wpa_supplicant *wpa_s = ctx;
1230 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1235 * DNS Header section is used only to calculate compression pointers, so the
1236 * contents of this data does not matter, but the length needs to be reserved
1237 * in the virtual packet.
1239 #define DNS_HEADER_LEN 12
1242 * 27-octet in-memory packet from P2P specification containing two implied
1243 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1245 #define P2P_SD_IN_MEMORY_LEN 27
1247 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1248 u8 **spos, const u8 *end)
1250 while (*spos < end) {
1251 u8 val = ((*spos)[0] & 0xc0) >> 6;
1254 if (val == 1 || val == 2) {
1255 /* These are reserved values in RFC 1035 */
1256 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1257 "sequence starting with 0x%x", val);
1266 if (*spos + 2 > end) {
1267 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1268 "DNS offset field");
1272 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1273 if (offset >= *spos - start) {
1274 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1275 "pointer offset %u", offset);
1280 spos_tmp = start + offset;
1281 return p2p_sd_dns_uncompress_label(upos, uend, start,
1287 len = (*spos)[0] & 0x3f;
1292 if (*spos + len > end) {
1293 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1294 "sequence - no room for label with length "
1299 if (*upos + len + 2 > uend)
1302 os_memcpy(*upos, *spos, len);
1314 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1315 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1316 * not large enough */
1317 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1318 size_t msg_len, size_t offset)
1320 /* 27-octet in-memory packet from P2P specification */
1321 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1322 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1323 u8 *tmp, *end, *spos;
1329 if (offset > msg_len)
1332 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1335 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1336 end = spos + msg_len;
1339 os_memset(tmp, 0, DNS_HEADER_LEN);
1340 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1341 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1344 uend = buf + buf_len;
1346 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1355 } else if (upos[-1] == '.')
1363 static struct p2p_srv_bonjour *
1364 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1365 const struct wpabuf *query)
1367 struct p2p_srv_bonjour *bsrv;
1370 len = wpabuf_len(query);
1371 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1372 struct p2p_srv_bonjour, list) {
1373 if (len == wpabuf_len(bsrv->query) &&
1374 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1382 static struct p2p_srv_upnp *
1383 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1384 const char *service)
1386 struct p2p_srv_upnp *usrv;
1388 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1389 struct p2p_srv_upnp, list) {
1390 if (version == usrv->version &&
1391 os_strcmp(service, usrv->service) == 0)
1398 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1403 if (wpabuf_tailroom(resp) < 5)
1406 /* Length (to be filled) */
1407 len_pos = wpabuf_put(resp, 2);
1408 wpabuf_put_u8(resp, srv_proto);
1409 wpabuf_put_u8(resp, srv_trans_id);
1411 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1412 /* Response Data: empty */
1413 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1417 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1418 struct wpabuf *resp, u8 srv_trans_id)
1420 struct p2p_srv_bonjour *bsrv;
1423 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1425 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1426 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1430 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1431 struct p2p_srv_bonjour, list) {
1432 if (wpabuf_tailroom(resp) <
1433 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1435 /* Length (to be filled) */
1436 len_pos = wpabuf_put(resp, 2);
1437 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1438 wpabuf_put_u8(resp, srv_trans_id);
1440 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1441 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1442 wpabuf_head(bsrv->resp),
1443 wpabuf_len(bsrv->resp));
1445 wpabuf_put_buf(resp, bsrv->query); /* Key */
1446 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1447 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1453 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1456 char str_rx[256], str_srv[256];
1458 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1459 return 0; /* Too short to include DNS Type and Version */
1460 if (os_memcmp(query + query_len - 3,
1461 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1463 return 0; /* Mismatch in DNS Type or Version */
1464 if (query_len == wpabuf_len(bsrv->query) &&
1465 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1466 return 1; /* Binary match */
1468 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1470 return 0; /* Failed to uncompress query */
1471 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1472 wpabuf_head(bsrv->query),
1473 wpabuf_len(bsrv->query) - 3, 0))
1474 return 0; /* Failed to uncompress service */
1476 return os_strcmp(str_rx, str_srv) == 0;
1480 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1481 struct wpabuf *resp, u8 srv_trans_id,
1482 const u8 *query, size_t query_len)
1484 struct p2p_srv_bonjour *bsrv;
1488 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1490 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1491 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1492 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1497 if (query_len == 0) {
1498 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1502 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1503 struct p2p_srv_bonjour, list) {
1504 if (!match_bonjour_query(bsrv, query, query_len))
1507 if (wpabuf_tailroom(resp) <
1508 5 + query_len + wpabuf_len(bsrv->resp))
1513 /* Length (to be filled) */
1514 len_pos = wpabuf_put(resp, 2);
1515 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1516 wpabuf_put_u8(resp, srv_trans_id);
1519 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1520 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1521 wpabuf_head(bsrv->resp),
1522 wpabuf_len(bsrv->resp));
1525 wpabuf_put_data(resp, query, query_len); /* Key */
1526 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1528 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1532 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1534 if (wpabuf_tailroom(resp) < 5)
1537 /* Length (to be filled) */
1538 len_pos = wpabuf_put(resp, 2);
1539 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1540 wpabuf_put_u8(resp, srv_trans_id);
1543 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1544 /* Response Data: empty */
1545 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1551 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1552 struct wpabuf *resp, u8 srv_trans_id)
1554 struct p2p_srv_upnp *usrv;
1557 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1559 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1560 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1564 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1565 struct p2p_srv_upnp, list) {
1566 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1569 /* Length (to be filled) */
1570 len_pos = wpabuf_put(resp, 2);
1571 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1572 wpabuf_put_u8(resp, srv_trans_id);
1575 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1577 wpabuf_put_u8(resp, usrv->version);
1578 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1580 wpabuf_put_str(resp, usrv->service);
1581 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1587 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1588 struct wpabuf *resp, u8 srv_trans_id,
1589 const u8 *query, size_t query_len)
1591 struct p2p_srv_upnp *usrv;
1597 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1600 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1601 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1602 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1607 if (query_len == 0) {
1608 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1612 if (wpabuf_tailroom(resp) < 5)
1615 /* Length (to be filled) */
1616 len_pos = wpabuf_put(resp, 2);
1617 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1618 wpabuf_put_u8(resp, srv_trans_id);
1621 str = os_malloc(query_len);
1624 os_memcpy(str, query + 1, query_len - 1);
1625 str[query_len - 1] = '\0';
1627 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1628 struct p2p_srv_upnp, list) {
1629 if (version != usrv->version)
1632 if (os_strcmp(str, "ssdp:all") != 0 &&
1633 os_strstr(usrv->service, str) == NULL)
1636 if (wpabuf_tailroom(resp) < 2)
1640 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1642 wpabuf_put_u8(resp, version);
1644 wpabuf_put_u8(resp, ',');
1648 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1650 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1652 wpabuf_put_str(resp, usrv->service);
1657 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1660 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1661 /* Response Data: empty */
1664 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1668 #ifdef CONFIG_WIFI_DISPLAY
1669 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1670 struct wpabuf *resp, u8 srv_trans_id,
1671 const u8 *query, size_t query_len)
1677 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1679 if (!wpa_s->global->wifi_display) {
1680 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1681 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1686 if (query_len < 1) {
1687 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1692 if (wpabuf_tailroom(resp) < 5)
1697 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1699 /* TODO: role specific handling */
1701 /* Length (to be filled) */
1702 len_pos = wpabuf_put(resp, 2);
1703 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1704 wpabuf_put_u8(resp, srv_trans_id);
1705 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1707 while (pos < query + query_len) {
1708 if (*pos < MAX_WFD_SUBELEMS &&
1709 wpa_s->global->wfd_subelem[*pos] &&
1710 wpabuf_tailroom(resp) >=
1711 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1712 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1713 "subelement %u", *pos);
1714 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1719 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1721 #endif /* CONFIG_WIFI_DISPLAY */
1724 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1725 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1727 struct wpa_supplicant *wpa_s = ctx;
1728 const u8 *pos = tlvs;
1729 const u8 *end = tlvs + tlvs_len;
1732 struct wpabuf *resp;
1733 u8 srv_proto, srv_trans_id;
1737 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1739 buf_len = 2 * tlvs_len + 1;
1740 buf = os_malloc(buf_len);
1742 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1743 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1745 freq, MAC2STR(sa), dialog_token, update_indic,
1750 if (wpa_s->p2p_sd_over_ctrl_iface) {
1751 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1752 update_indic, tlvs, tlvs_len);
1753 return; /* to be processed by an external program */
1756 resp = wpabuf_alloc(10000);
1760 while (pos + 1 < end) {
1761 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1762 slen = WPA_GET_LE16(pos);
1764 if (pos + slen > end || slen < 2) {
1765 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1770 tlv_end = pos + slen;
1773 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1775 srv_trans_id = *pos++;
1776 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1779 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1780 pos, tlv_end - pos);
1783 if (wpa_s->force_long_sd) {
1784 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1786 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1787 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1791 switch (srv_proto) {
1792 case P2P_SERV_ALL_SERVICES:
1793 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1794 "for all services");
1795 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1796 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1797 wpa_printf(MSG_DEBUG, "P2P: No service "
1798 "discovery protocols available");
1799 wpas_sd_add_proto_not_avail(
1800 resp, P2P_SERV_ALL_SERVICES,
1804 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1805 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1807 case P2P_SERV_BONJOUR:
1808 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1809 pos, tlv_end - pos);
1812 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1813 pos, tlv_end - pos);
1815 #ifdef CONFIG_WIFI_DISPLAY
1816 case P2P_SERV_WIFI_DISPLAY:
1817 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1818 pos, tlv_end - pos);
1820 #endif /* CONFIG_WIFI_DISPLAY */
1822 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1823 "protocol %u", srv_proto);
1824 wpas_sd_add_proto_not_avail(resp, srv_proto,
1833 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1834 update_indic, tlvs, tlvs_len);
1836 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1842 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1843 const u8 *tlvs, size_t tlvs_len)
1845 struct wpa_supplicant *wpa_s = ctx;
1846 const u8 *pos = tlvs;
1847 const u8 *end = tlvs + tlvs_len;
1853 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1855 if (tlvs_len > 1500) {
1856 /* TODO: better way for handling this */
1857 wpa_msg_ctrl(wpa_s, MSG_INFO,
1858 P2P_EVENT_SERV_DISC_RESP MACSTR
1859 " %u <long response: %u bytes>",
1860 MAC2STR(sa), update_indic,
1861 (unsigned int) tlvs_len);
1863 buf_len = 2 * tlvs_len + 1;
1864 buf = os_malloc(buf_len);
1866 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1867 wpa_msg_ctrl(wpa_s, MSG_INFO,
1868 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1869 MAC2STR(sa), update_indic, buf);
1875 u8 srv_proto, srv_trans_id, status;
1877 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1878 slen = WPA_GET_LE16(pos);
1880 if (pos + slen > end || slen < 3) {
1881 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1885 tlv_end = pos + slen;
1888 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1890 srv_trans_id = *pos++;
1891 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1894 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1897 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1898 pos, tlv_end - pos);
1903 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1907 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1908 const struct wpabuf *tlvs)
1910 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1911 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1912 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1914 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1918 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1919 u8 version, const char *query)
1921 struct wpabuf *tlvs;
1924 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1927 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1928 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1929 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1930 wpabuf_put_u8(tlvs, version);
1931 wpabuf_put_str(tlvs, query);
1932 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1938 #ifdef CONFIG_WIFI_DISPLAY
1940 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1941 const struct wpabuf *tlvs)
1943 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1945 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1947 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1951 #define MAX_WFD_SD_SUBELEMS 20
1953 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1954 const char *subelems)
1961 len = wpabuf_put(tlvs, 2);
1962 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1963 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1965 wpabuf_put_u8(tlvs, role);
1970 if (val >= 0 && val < 256) {
1971 wpabuf_put_u8(tlvs, val);
1973 if (count == MAX_WFD_SD_SUBELEMS)
1976 pos = os_strchr(pos + 1, ',');
1982 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1986 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1987 const u8 *dst, const char *role)
1989 struct wpabuf *tlvs;
1991 const char *subelems;
1994 subelems = os_strchr(role, ' ');
1995 if (subelems == NULL)
1999 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2003 if (os_strstr(role, "[source]"))
2004 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2005 if (os_strstr(role, "[pri-sink]"))
2006 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2007 if (os_strstr(role, "[sec-sink]"))
2008 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2009 if (os_strstr(role, "[source+sink]"))
2010 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2012 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2017 #endif /* CONFIG_WIFI_DISPLAY */
2020 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2022 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2023 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
2024 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2026 return p2p_sd_cancel_request(wpa_s->global->p2p,
2027 (void *) (uintptr_t) req);
2031 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2032 const u8 *dst, u8 dialog_token,
2033 const struct wpabuf *resp_tlvs)
2035 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2036 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2040 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2042 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2047 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2049 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2050 wpa_drv_p2p_service_update(wpa_s);
2053 if (wpa_s->global->p2p)
2054 p2p_sd_service_update(wpa_s->global->p2p);
2058 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2060 dl_list_del(&bsrv->list);
2061 wpabuf_free(bsrv->query);
2062 wpabuf_free(bsrv->resp);
2067 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2069 dl_list_del(&usrv->list);
2070 os_free(usrv->service);
2075 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2077 struct p2p_srv_bonjour *bsrv, *bn;
2078 struct p2p_srv_upnp *usrv, *un;
2080 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2081 struct p2p_srv_bonjour, list)
2082 wpas_p2p_srv_bonjour_free(bsrv);
2084 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2085 struct p2p_srv_upnp, list)
2086 wpas_p2p_srv_upnp_free(usrv);
2088 wpas_p2p_sd_service_update(wpa_s);
2092 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2093 struct wpabuf *query, struct wpabuf *resp)
2095 struct p2p_srv_bonjour *bsrv;
2097 bsrv = os_zalloc(sizeof(*bsrv));
2100 bsrv->query = query;
2102 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2104 wpas_p2p_sd_service_update(wpa_s);
2109 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2110 const struct wpabuf *query)
2112 struct p2p_srv_bonjour *bsrv;
2114 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2117 wpas_p2p_srv_bonjour_free(bsrv);
2118 wpas_p2p_sd_service_update(wpa_s);
2123 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2124 const char *service)
2126 struct p2p_srv_upnp *usrv;
2128 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2129 return 0; /* Already listed */
2130 usrv = os_zalloc(sizeof(*usrv));
2133 usrv->version = version;
2134 usrv->service = os_strdup(service);
2135 if (usrv->service == NULL) {
2139 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2141 wpas_p2p_sd_service_update(wpa_s);
2146 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2147 const char *service)
2149 struct p2p_srv_upnp *usrv;
2151 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2154 wpas_p2p_srv_upnp_free(usrv);
2155 wpas_p2p_sd_service_update(wpa_s);
2160 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2161 const u8 *peer, const char *params,
2162 unsigned int generated_pin)
2164 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2165 MAC2STR(peer), generated_pin, params);
2169 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2170 const u8 *peer, const char *params)
2172 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2173 MAC2STR(peer), params);
2177 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2178 const u8 *dev_addr, const u8 *pri_dev_type,
2179 const char *dev_name, u16 supp_config_methods,
2180 u8 dev_capab, u8 group_capab, const u8 *group_id,
2181 size_t group_id_len)
2183 struct wpa_supplicant *wpa_s = ctx;
2184 char devtype[WPS_DEV_TYPE_BUFSIZE];
2186 u8 empty_dev_type[8];
2187 unsigned int generated_pin = 0;
2188 struct wpa_supplicant *group = NULL;
2191 for (group = wpa_s->global->ifaces; group; group = group->next)
2193 struct wpa_ssid *s = group->current_ssid;
2195 s->mode == WPAS_MODE_P2P_GO &&
2196 group_id_len - ETH_ALEN == s->ssid_len &&
2197 os_memcmp(group_id + ETH_ALEN, s->ssid,
2203 if (pri_dev_type == NULL) {
2204 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2205 pri_dev_type = empty_dev_type;
2207 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2208 " pri_dev_type=%s name='%s' config_methods=0x%x "
2209 "dev_capab=0x%x group_capab=0x%x%s%s",
2211 wps_dev_type_bin2str(pri_dev_type, devtype,
2213 dev_name, supp_config_methods, dev_capab, group_capab,
2214 group ? " group=" : "",
2215 group ? group->ifname : "");
2216 params[sizeof(params) - 1] = '\0';
2218 if (config_methods & WPS_CONFIG_DISPLAY) {
2219 generated_pin = wps_generate_pin();
2220 wpas_prov_disc_local_display(wpa_s, peer, params,
2222 } else if (config_methods & WPS_CONFIG_KEYPAD)
2223 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2224 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2225 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2226 "%s", MAC2STR(peer), params);
2228 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2229 P2P_PROV_DISC_SUCCESS,
2230 config_methods, generated_pin);
2234 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2236 struct wpa_supplicant *wpa_s = ctx;
2237 unsigned int generated_pin = 0;
2240 if (wpa_s->pending_pd_before_join &&
2241 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2242 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2243 wpa_s->pending_pd_before_join = 0;
2244 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2245 "join-existing-group operation");
2246 wpas_p2p_join_start(wpa_s);
2250 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2251 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2252 os_snprintf(params, sizeof(params), " peer_go=%d",
2253 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2257 if (config_methods & WPS_CONFIG_DISPLAY)
2258 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2259 else if (config_methods & WPS_CONFIG_KEYPAD) {
2260 generated_pin = wps_generate_pin();
2261 wpas_prov_disc_local_display(wpa_s, peer, params,
2263 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2264 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2265 "%s", MAC2STR(peer), params);
2267 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2268 P2P_PROV_DISC_SUCCESS,
2269 config_methods, generated_pin);
2273 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2274 enum p2p_prov_disc_status status)
2276 struct wpa_supplicant *wpa_s = ctx;
2278 if (wpa_s->p2p_fallback_to_go_neg) {
2279 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2280 "failed - fall back to GO Negotiation");
2281 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2285 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2286 wpa_s->pending_pd_before_join = 0;
2287 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2288 "join-existing-group operation (no ACK for PD "
2290 wpas_p2p_join_start(wpa_s);
2294 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2295 " p2p_dev_addr=" MACSTR " status=%d",
2296 MAC2STR(peer), status);
2298 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2303 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2304 const u8 *go_dev_addr, const u8 *ssid,
2305 size_t ssid_len, int *go, u8 *group_bssid,
2306 int *force_freq, int persistent_group)
2308 struct wpa_supplicant *wpa_s = ctx;
2310 u8 cur_bssid[ETH_ALEN];
2312 struct wpa_supplicant *grp;
2314 if (!persistent_group) {
2315 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2316 " to join an active group", MAC2STR(sa));
2317 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2318 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2320 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2321 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2322 "authorized invitation");
2326 * Do not accept the invitation automatically; notify user and
2329 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2332 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2334 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2335 "running persistent group");
2337 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2341 if (!wpa_s->conf->persistent_reconnect)
2342 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2344 for (s = wpa_s->conf->ssid; s; s = s->next) {
2345 if (s->disabled == 2 &&
2346 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2347 s->ssid_len == ssid_len &&
2348 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2353 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2354 " requested reinvocation of an unknown group",
2356 return P2P_SC_FAIL_UNKNOWN_GROUP;
2359 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2361 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2362 wpa_printf(MSG_DEBUG, "P2P: The only available "
2363 "interface is already in use - reject "
2365 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2367 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2368 } else if (s->mode == WPAS_MODE_P2P_GO) {
2370 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2372 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2373 "interface address for the group");
2374 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2376 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2381 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2382 wpa_s->assoc_freq) {
2383 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2384 "the channel we are already using");
2385 *force_freq = wpa_s->assoc_freq;
2388 res = wpa_drv_shared_freq(wpa_s);
2390 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2391 "with the channel we are already using on a "
2392 "shared interface");
2396 return P2P_SC_SUCCESS;
2400 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2401 const u8 *ssid, size_t ssid_len,
2402 const u8 *go_dev_addr, u8 status,
2405 struct wpa_supplicant *wpa_s = ctx;
2408 for (s = wpa_s->conf->ssid; s; s = s->next) {
2409 if (s->disabled == 2 &&
2410 s->ssid_len == ssid_len &&
2411 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2415 if (status == P2P_SC_SUCCESS) {
2416 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2417 " was accepted; op_freq=%d MHz",
2418 MAC2STR(sa), op_freq);
2420 int go = s->mode == WPAS_MODE_P2P_GO;
2421 wpas_p2p_group_add_persistent(
2422 wpa_s, s, go, go ? op_freq : 0, 0);
2424 wpa_s->user_initiated_pd = 0;
2425 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2426 wpa_s->p2p_wps_method, 0);
2431 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2432 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2433 " was rejected (status %u)", MAC2STR(sa), status);
2439 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2440 "sa=" MACSTR " go_dev_addr=" MACSTR
2441 " bssid=" MACSTR " unknown-network",
2442 MAC2STR(sa), MAC2STR(go_dev_addr),
2445 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2446 "sa=" MACSTR " go_dev_addr=" MACSTR
2448 MAC2STR(sa), MAC2STR(go_dev_addr));
2453 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2454 " persistent=%d", MAC2STR(sa), s->id);
2458 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2460 struct wpa_supplicant *wpa_s = ctx;
2461 struct wpa_ssid *ssid;
2464 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2465 "status=%d " MACSTR,
2466 status, MAC2STR(bssid));
2468 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2469 "status=%d ", status);
2471 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2473 if (wpa_s->pending_invite_ssid_id == -1)
2474 return; /* Invitation to active group */
2476 if (status != P2P_SC_SUCCESS) {
2477 wpas_p2p_remove_pending_group_interface(wpa_s);
2481 ssid = wpa_config_get_network(wpa_s->conf,
2482 wpa_s->pending_invite_ssid_id);
2484 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2485 "data matching with invitation");
2490 * The peer could have missed our ctrl::ack frame for Invitation
2491 * Response and continue retransmitting the frame. To reduce the
2492 * likelihood of the peer not getting successful TX status for the
2493 * Invitation Response frame, wait a short time here before starting
2494 * the persistent group so that we will remain on the current channel to
2495 * acknowledge any possible retransmission from the peer.
2497 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2498 "starting persistent group");
2501 wpas_p2p_group_add_persistent(wpa_s, ssid,
2502 ssid->mode == WPAS_MODE_P2P_GO,
2503 wpa_s->p2p_persistent_go_freq,
2504 wpa_s->p2p_go_ht40);
2508 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2513 if (global->p2p_disallow_freq == NULL)
2516 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2517 if (freq >= global->p2p_disallow_freq[i].min &&
2518 freq <= global->p2p_disallow_freq[i].max)
2526 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2528 reg->channel[reg->channels] = chan;
2533 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2534 struct p2p_channels *chan)
2538 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2541 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2542 chan->reg_class[cla].reg_class = 81;
2543 chan->reg_class[cla].channels = 0;
2544 for (i = 0; i < 11; i++) {
2545 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2546 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2548 if (chan->reg_class[cla].channels)
2551 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2554 /* Operating class 115 - 5 GHz, channels 36-48 */
2555 chan->reg_class[cla].reg_class = 115;
2556 chan->reg_class[cla].channels = 0;
2557 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2558 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2559 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2560 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2561 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2562 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2563 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2564 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2565 if (chan->reg_class[cla].channels)
2568 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2571 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2572 chan->reg_class[cla].reg_class = 124;
2573 chan->reg_class[cla].channels = 0;
2574 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2575 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2576 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2577 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2578 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2579 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2580 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2581 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2582 if (chan->reg_class[cla].channels)
2585 chan->reg_classes = cla;
2590 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2592 enum hostapd_hw_mode mode)
2596 for (i = 0; i < num_modes; i++) {
2597 if (modes[i].mode == mode)
2605 static int has_channel(struct wpa_global *global,
2606 struct hostapd_hw_modes *mode, u8 chan, int *flags)
2611 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2613 if (wpas_p2p_disallowed_freq(global, freq))
2616 for (i = 0; i < mode->num_channels; i++) {
2617 if (mode->channels[i].chan == chan) {
2619 *flags = mode->channels[i].flag;
2620 return !(mode->channels[i].flag &
2621 (HOSTAPD_CHAN_DISABLED |
2622 HOSTAPD_CHAN_PASSIVE_SCAN |
2623 HOSTAPD_CHAN_NO_IBSS |
2624 HOSTAPD_CHAN_RADAR));
2632 struct p2p_oper_class_map {
2633 enum hostapd_hw_mode mode;
2638 enum { BW20, BW40PLUS, BW40MINUS } bw;
2641 static struct p2p_oper_class_map op_class[] = {
2642 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2643 #if 0 /* Do not enable HT40 on 2 GHz for now */
2644 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2645 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2647 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2648 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2649 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2650 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2651 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2652 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2653 { -1, 0, 0, 0, 0, BW20 }
2657 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2658 struct hostapd_hw_modes *mode,
2663 if (!has_channel(wpa_s->global, mode, channel, &flag))
2665 if (bw == BW40MINUS &&
2666 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2667 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2669 if (bw == BW40PLUS &&
2670 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2671 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2677 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2678 struct p2p_channels *chan)
2680 struct hostapd_hw_modes *mode;
2683 if (wpa_s->hw.modes == NULL) {
2684 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2685 "of all supported channels; assume dualband "
2687 return wpas_p2p_default_channels(wpa_s, chan);
2692 for (op = 0; op_class[op].op_class; op++) {
2693 struct p2p_oper_class_map *o = &op_class[op];
2695 struct p2p_reg_class *reg = NULL;
2697 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2700 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2701 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2704 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2705 "class %u", o->op_class);
2706 reg = &chan->reg_class[cla];
2708 reg->reg_class = o->op_class;
2710 reg->channel[reg->channels] = ch;
2714 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2715 reg->channel, reg->channels);
2719 chan->reg_classes = cla;
2725 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2726 struct hostapd_hw_modes *mode, u8 channel)
2730 for (op = 0; op_class[op].op_class; op++) {
2731 struct p2p_oper_class_map *o = &op_class[op];
2734 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2735 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2736 o->bw == BW20 || ch != channel)
2738 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2742 return (o->bw == BW40MINUS) ? -1 : 1;
2751 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2754 struct wpa_supplicant *wpa_s = ctx;
2756 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2757 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2763 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2767 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2769 struct wpa_supplicant *wpa_s = ctx;
2771 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2772 struct wpa_ssid *ssid = wpa_s->current_ssid;
2775 if (ssid->mode != WPAS_MODE_INFRA)
2777 if (wpa_s->wpa_state != WPA_COMPLETED &&
2778 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2780 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2789 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2790 * @global: Pointer to global data from wpa_supplicant_init()
2791 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2792 * Returns: 0 on success, -1 on failure
2794 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2796 struct p2p_config p2p;
2800 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2806 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2807 struct p2p_params params;
2809 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2810 os_memset(¶ms, 0, sizeof(params));
2811 params.dev_name = wpa_s->conf->device_name;
2812 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2814 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2815 os_memcpy(params.sec_dev_type,
2816 wpa_s->conf->sec_device_type,
2817 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2819 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2825 os_memset(&p2p, 0, sizeof(p2p));
2826 p2p.msg_ctx = wpa_s;
2828 p2p.p2p_scan = wpas_p2p_scan;
2829 p2p.send_action = wpas_send_action;
2830 p2p.send_action_done = wpas_send_action_done;
2831 p2p.go_neg_completed = wpas_go_neg_completed;
2832 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2833 p2p.dev_found = wpas_dev_found;
2834 p2p.dev_lost = wpas_dev_lost;
2835 p2p.start_listen = wpas_start_listen;
2836 p2p.stop_listen = wpas_stop_listen;
2837 p2p.send_probe_resp = wpas_send_probe_resp;
2838 p2p.sd_request = wpas_sd_request;
2839 p2p.sd_response = wpas_sd_response;
2840 p2p.prov_disc_req = wpas_prov_disc_req;
2841 p2p.prov_disc_resp = wpas_prov_disc_resp;
2842 p2p.prov_disc_fail = wpas_prov_disc_fail;
2843 p2p.invitation_process = wpas_invitation_process;
2844 p2p.invitation_received = wpas_invitation_received;
2845 p2p.invitation_result = wpas_invitation_result;
2846 p2p.get_noa = wpas_get_noa;
2847 p2p.go_connected = wpas_go_connected;
2849 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2850 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2851 p2p.dev_name = wpa_s->conf->device_name;
2852 p2p.manufacturer = wpa_s->conf->manufacturer;
2853 p2p.model_name = wpa_s->conf->model_name;
2854 p2p.model_number = wpa_s->conf->model_number;
2855 p2p.serial_number = wpa_s->conf->serial_number;
2857 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2858 p2p.config_methods = wpa_s->wps->config_methods;
2861 if (wpa_s->conf->p2p_listen_reg_class &&
2862 wpa_s->conf->p2p_listen_channel) {
2863 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2864 p2p.channel = wpa_s->conf->p2p_listen_channel;
2868 * Pick one of the social channels randomly as the listen
2871 os_get_random((u8 *) &r, sizeof(r));
2872 p2p.channel = 1 + (r % 3) * 5;
2874 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2876 if (wpa_s->conf->p2p_oper_reg_class &&
2877 wpa_s->conf->p2p_oper_channel) {
2878 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2879 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2880 p2p.cfg_op_channel = 1;
2881 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2882 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2885 p2p.op_reg_class = 81;
2887 * Use random operation channel from (1, 6, 11) if no other
2888 * preference is indicated.
2890 os_get_random((u8 *) &r, sizeof(r));
2891 p2p.op_channel = 1 + (r % 3) * 5;
2892 p2p.cfg_op_channel = 0;
2893 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2894 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2896 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2897 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2898 p2p.country[2] = 0x04;
2900 os_memcpy(p2p.country, "XX\x04", 3);
2902 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2903 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2908 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2911 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2912 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2913 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2915 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2916 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2918 p2p.max_peers = 100;
2920 if (wpa_s->conf->p2p_ssid_postfix) {
2921 p2p.ssid_postfix_len =
2922 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2923 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2924 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2925 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2926 p2p.ssid_postfix_len);
2929 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2931 p2p.max_listen = wpa_s->max_remain_on_chan;
2933 global->p2p = p2p_init(&p2p);
2934 if (global->p2p == NULL)
2936 global->p2p_init_wpa_s = wpa_s;
2938 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2939 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2941 p2p_add_wps_vendor_extension(
2942 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2950 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2951 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2953 * This function deinitialize per-interface P2P data.
2955 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2957 if (wpa_s->driver && wpa_s->drv_priv)
2958 wpa_drv_probe_req_report(wpa_s, 0);
2960 if (wpa_s->go_params) {
2961 /* Clear any stored provisioning info */
2962 p2p_clear_provisioning_info(
2964 wpa_s->go_params->peer_device_addr);
2967 os_free(wpa_s->go_params);
2968 wpa_s->go_params = NULL;
2969 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2970 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2971 wpa_s->p2p_long_listen = 0;
2972 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2973 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2974 wpas_p2p_remove_pending_group_interface(wpa_s);
2976 /* TODO: remove group interface from the driver if this wpa_s instance
2977 * is on top of a P2P group interface */
2982 * wpas_p2p_deinit_global - Deinitialize global P2P module
2983 * @global: Pointer to global data from wpa_supplicant_init()
2985 * This function deinitializes the global (per device) P2P module.
2987 void wpas_p2p_deinit_global(struct wpa_global *global)
2989 struct wpa_supplicant *wpa_s, *tmp;
2991 wpa_s = global->ifaces;
2993 wpas_p2p_service_flush(wpa_s);
2995 if (global->p2p == NULL)
2998 /* Remove remaining P2P group interfaces */
2999 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3000 wpa_s = wpa_s->next;
3002 tmp = global->ifaces;
3005 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3010 /* Disconnect from the P2P group and deinit the interface */
3011 wpas_p2p_disconnect(tmp);
3015 * Deinit GO data on any possibly remaining interface (if main
3016 * interface is used as GO).
3018 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3019 if (wpa_s->ap_iface)
3020 wpas_p2p_group_deinit(wpa_s);
3023 p2p_deinit(global->p2p);
3025 global->p2p_init_wpa_s = NULL;
3029 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3031 if (wpa_s->conf->p2p_no_group_iface)
3032 return 0; /* separate interface disabled per configuration */
3033 if (wpa_s->drv_flags &
3034 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3035 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3036 return 1; /* P2P group requires a new interface in every case
3038 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3039 return 0; /* driver does not support concurrent operations */
3040 if (wpa_s->global->ifaces->next)
3041 return 1; /* more that one interface already in use */
3042 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3043 return 1; /* this interface is already in use */
3048 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3049 const u8 *peer_addr,
3050 enum p2p_wps_method wps_method,
3051 int go_intent, const u8 *own_interface_addr,
3052 unsigned int force_freq, int persistent_group,
3053 struct wpa_ssid *ssid, unsigned int pref_freq)
3055 if (persistent_group && wpa_s->conf->persistent_reconnect)
3056 persistent_group = 2;
3058 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3059 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3060 go_intent, own_interface_addr,
3061 force_freq, persistent_group);
3065 * Increase GO config timeout if HT40 is used since it takes some time
3066 * to scan channels for coex purposes before the BSS can be started.
3068 p2p_set_config_timeout(wpa_s->global->p2p,
3069 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3071 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3072 go_intent, own_interface_addr, force_freq,
3073 persistent_group, ssid ? ssid->ssid : NULL,
3074 ssid ? ssid->ssid_len : 0,
3075 wpa_s->p2p_pd_before_go_neg, pref_freq);
3079 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3080 const u8 *peer_addr,
3081 enum p2p_wps_method wps_method,
3082 int go_intent, const u8 *own_interface_addr,
3083 unsigned int force_freq, int persistent_group,
3084 struct wpa_ssid *ssid, unsigned int pref_freq)
3086 if (persistent_group && wpa_s->conf->persistent_reconnect)
3087 persistent_group = 2;
3089 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3092 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3093 go_intent, own_interface_addr, force_freq,
3094 persistent_group, ssid ? ssid->ssid : NULL,
3095 ssid ? ssid->ssid_len : 0, pref_freq);
3099 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3101 wpa_s->p2p_join_scan_count++;
3102 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3103 wpa_s->p2p_join_scan_count);
3104 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3105 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3106 " for join operationg - stop join attempt",
3107 MAC2STR(wpa_s->pending_join_iface_addr));
3108 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3109 if (wpa_s->p2p_auto_pd) {
3110 wpa_s->p2p_auto_pd = 0;
3111 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3112 " p2p_dev_addr=" MACSTR " status=N/A",
3113 MAC2STR(wpa_s->pending_join_dev_addr));
3116 wpa_msg(wpa_s->parent, MSG_INFO,
3117 P2P_EVENT_GROUP_FORMATION_FAILURE);
3122 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3124 struct wpa_supplicant *iface;
3128 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3131 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3132 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3134 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3136 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3137 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3138 shared_freq = iface->current_ssid->frequency;
3139 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3140 shared_freq = iface->assoc_freq;
3144 if (shared_freq && freq != shared_freq) {
3145 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3146 "connected on %d MHz - new connection on "
3147 "%d MHz", iface->ifname, shared_freq, freq);
3152 shared_freq = wpa_drv_shared_freq(wpa_s);
3153 if (shared_freq > 0 && shared_freq != freq) {
3154 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3155 "virtual interface connected on %d MHz - new "
3156 "connection on %d MHz", shared_freq, freq);
3164 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3165 const u8 *peer_dev_addr)
3167 struct wpa_bss *bss;
3170 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3173 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3174 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3179 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3180 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3181 "%ld.%06ld (%supdated in last scan)",
3182 bss->last_update.sec, bss->last_update.usec,
3183 updated ? "": "not ");
3189 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3190 struct wpa_scan_results *scan_res)
3192 struct wpa_bss *bss;
3194 u8 iface_addr[ETH_ALEN];
3196 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3198 if (wpa_s->global->p2p_disabled)
3201 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3202 scan_res ? (int) scan_res->num : -1,
3203 wpa_s->p2p_auto_join ? "auto_" : "");
3206 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3208 if (wpa_s->p2p_auto_pd) {
3209 int join = wpas_p2p_peer_go(wpa_s,
3210 wpa_s->pending_join_dev_addr);
3212 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3213 wpa_s->auto_pd_scan_retry++;
3214 bss = wpa_bss_get_bssid(wpa_s,
3215 wpa_s->pending_join_dev_addr);
3218 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3219 "the peer " MACSTR " at %d MHz",
3220 wpa_s->auto_pd_scan_retry,
3222 pending_join_dev_addr),
3224 wpas_p2p_join_scan_req(wpa_s, freq);
3232 wpa_s->p2p_auto_pd = 0;
3233 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3234 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3235 MAC2STR(wpa_s->pending_join_dev_addr), join);
3236 if (p2p_prov_disc_req(wpa_s->global->p2p,
3237 wpa_s->pending_join_dev_addr,
3238 wpa_s->pending_pd_config_methods, join,
3239 0, wpa_s->user_initiated_pd) < 0) {
3240 wpa_s->p2p_auto_pd = 0;
3241 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3242 " p2p_dev_addr=" MACSTR " status=N/A",
3243 MAC2STR(wpa_s->pending_join_dev_addr));
3248 if (wpa_s->p2p_auto_join) {
3249 int join = wpas_p2p_peer_go(wpa_s,
3250 wpa_s->pending_join_dev_addr);
3252 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3253 "running a GO -> use GO Negotiation");
3254 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3255 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3256 wpa_s->p2p_persistent_group, 0, 0, 0,
3257 wpa_s->p2p_go_intent,
3258 wpa_s->p2p_connect_freq,
3259 wpa_s->p2p_persistent_id,
3260 wpa_s->p2p_pd_before_go_neg,
3261 wpa_s->p2p_go_ht40);
3265 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3266 "try to join the group", join ? "" :
3269 wpa_s->p2p_fallback_to_go_neg = 1;
3272 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3273 wpa_s->pending_join_iface_addr);
3275 p2p_get_interface_addr(wpa_s->global->p2p,
3276 wpa_s->pending_join_dev_addr,
3278 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3280 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3281 "address for join from " MACSTR " to " MACSTR
3282 " based on newly discovered P2P peer entry",
3283 MAC2STR(wpa_s->pending_join_iface_addr),
3284 MAC2STR(iface_addr));
3285 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3288 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3289 wpa_s->pending_join_iface_addr);
3292 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3293 "from P2P peer table: %d MHz", freq);
3295 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3298 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3299 "from BSS table: %d MHz", freq);
3304 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3305 wpa_msg(wpa_s->parent, MSG_INFO,
3306 P2P_EVENT_GROUP_FORMATION_FAILURE
3307 "reason=FREQ_CONFLICT");
3311 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3312 "prior to joining an existing group (GO " MACSTR
3314 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3315 wpa_s->pending_pd_before_join = 1;
3317 switch (wpa_s->pending_join_wps_method) {
3318 case WPS_PIN_DISPLAY:
3319 method = WPS_CONFIG_KEYPAD;
3321 case WPS_PIN_KEYPAD:
3322 method = WPS_CONFIG_DISPLAY;
3325 method = WPS_CONFIG_PUSHBUTTON;
3332 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3333 wpa_s->pending_join_dev_addr) ==
3336 * We have already performed provision discovery for
3337 * joining the group. Proceed directly to join
3338 * operation without duplicated provision discovery. */
3339 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3340 "with " MACSTR " already done - proceed to "
3342 MAC2STR(wpa_s->pending_join_dev_addr));
3343 wpa_s->pending_pd_before_join = 0;
3347 if (p2p_prov_disc_req(wpa_s->global->p2p,
3348 wpa_s->pending_join_dev_addr, method, 1,
3349 freq, wpa_s->user_initiated_pd) < 0) {
3350 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3351 "Discovery Request before joining an "
3353 wpa_s->pending_pd_before_join = 0;
3359 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3360 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3361 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3362 wpas_p2p_check_join_scan_limit(wpa_s);
3366 /* Start join operation immediately */
3367 wpas_p2p_join_start(wpa_s);
3371 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3374 struct wpa_driver_scan_params params;
3375 struct wpabuf *wps_ie, *ies;
3377 int freqs[2] = { 0, 0 };
3379 os_memset(¶ms, 0, sizeof(params));
3381 /* P2P Wildcard SSID */
3382 params.num_ssids = 1;
3383 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3384 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3386 wpa_s->wps->dev.p2p = 1;
3387 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3388 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3390 if (wps_ie == NULL) {
3391 wpas_p2p_scan_res_join(wpa_s, NULL);
3395 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3396 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3398 wpabuf_free(wps_ie);
3399 wpas_p2p_scan_res_join(wpa_s, NULL);
3402 wpabuf_put_buf(ies, wps_ie);
3403 wpabuf_free(wps_ie);
3405 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3407 params.p2p_probe = 1;
3408 params.extra_ies = wpabuf_head(ies);
3409 params.extra_ies_len = wpabuf_len(ies);
3412 params.freqs = freqs;
3416 * Run a scan to update BSS table and start Provision Discovery once
3417 * the new scan results become available.
3419 ret = wpa_drv_scan(wpa_s, ¶ms);
3421 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3426 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3428 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3429 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3430 wpas_p2p_check_join_scan_limit(wpa_s);
3435 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3437 struct wpa_supplicant *wpa_s = eloop_ctx;
3438 wpas_p2p_join_scan_req(wpa_s, 0);
3442 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3443 const u8 *dev_addr, enum p2p_wps_method wps_method,
3446 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3447 MACSTR " dev " MACSTR ")%s",
3448 MAC2STR(iface_addr), MAC2STR(dev_addr),
3449 auto_join ? " (auto_join)" : "");
3451 wpa_s->p2p_auto_pd = 0;
3452 wpa_s->p2p_auto_join = !!auto_join;
3453 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3454 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3455 wpa_s->pending_join_wps_method = wps_method;
3457 /* Make sure we are not running find during connection establishment */
3458 wpas_p2p_stop_find(wpa_s);
3460 wpa_s->p2p_join_scan_count = 0;
3461 wpas_p2p_join_scan(wpa_s, NULL);
3466 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3468 struct wpa_supplicant *group;
3469 struct p2p_go_neg_results res;
3470 struct wpa_bss *bss;
3472 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3475 if (group != wpa_s) {
3476 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3477 sizeof(group->p2p_pin));
3478 group->p2p_wps_method = wpa_s->p2p_wps_method;
3481 group->p2p_in_provisioning = 1;
3482 wpa_s->global->p2p_group_formation = wpa_s;
3483 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3485 os_memset(&res, 0, sizeof(res));
3486 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3488 res.wps_method = wpa_s->pending_join_wps_method;
3489 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3491 res.freq = bss->freq;
3492 res.ssid_len = bss->ssid_len;
3493 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3496 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3497 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3499 wpa_drv_cancel_remain_on_channel(wpa_s);
3500 wpa_s->off_channel_freq = 0;
3501 wpa_s->roc_waiting_drv_freq = 0;
3503 wpas_start_wps_enrollee(group, &res);
3506 * Allow a longer timeout for join-a-running-group than normal 15
3507 * second group formation timeout since the GO may not have authorized
3508 * our connection yet.
3510 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3511 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3519 * wpas_p2p_connect - Request P2P Group Formation to be started
3520 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3521 * @peer_addr: Address of the peer P2P Device
3522 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3523 * @persistent_group: Whether to create a persistent group
3524 * @auto_join: Whether to select join vs. GO Negotiation automatically
3525 * @join: Whether to join an existing group (as a client) instead of starting
3526 * Group Owner negotiation; @peer_addr is BSSID in that case
3527 * @auth: Whether to only authorize the connection instead of doing that and
3528 * initiating Group Owner negotiation
3529 * @go_intent: GO Intent or -1 to use default
3530 * @freq: Frequency for the group or 0 for auto-selection
3531 * @persistent_id: Persistent group credentials to use for forcing GO
3532 * parameters or -1 to generate new values (SSID/passphrase)
3533 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3534 * interoperability workaround when initiating group formation
3535 * @ht40: Start GO with 40 MHz channel width
3536 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3537 * failure, -2 on failure due to channel not currently available,
3538 * -3 if forced channel is not supported
3540 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3541 const char *pin, enum p2p_wps_method wps_method,
3542 int persistent_group, int auto_join, int join, int auth,
3543 int go_intent, int freq, int persistent_id, int pd,
3546 int force_freq = 0, pref_freq = 0, oper_freq = 0;
3549 enum wpa_driver_if_type iftype;
3551 struct wpa_ssid *ssid = NULL;
3553 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3556 if (persistent_id >= 0) {
3557 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3558 if (ssid == NULL || ssid->disabled != 2 ||
3559 ssid->mode != WPAS_MODE_P2P_GO)
3564 go_intent = wpa_s->conf->p2p_go_intent;
3567 wpa_s->p2p_long_listen = 0;
3569 wpa_s->p2p_wps_method = wps_method;
3570 wpa_s->p2p_persistent_group = !!persistent_group;
3571 wpa_s->p2p_persistent_id = persistent_id;
3572 wpa_s->p2p_go_intent = go_intent;
3573 wpa_s->p2p_connect_freq = freq;
3574 wpa_s->p2p_fallback_to_go_neg = 0;
3575 wpa_s->p2p_pd_before_go_neg = !!pd;
3576 wpa_s->p2p_go_ht40 = !!ht40;
3579 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3580 else if (wps_method == WPS_PIN_DISPLAY) {
3581 ret = wps_generate_pin();
3582 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3584 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3587 wpa_s->p2p_pin[0] = '\0';
3589 if (join || auto_join) {
3590 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3592 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3593 "connect a running group from " MACSTR,
3594 MAC2STR(peer_addr));
3595 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3598 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3599 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3601 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3602 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3606 os_get_time(&wpa_s->p2p_auto_started);
3607 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3609 wpa_s->p2p_auto_started.sec,
3610 wpa_s->p2p_auto_started.usec);
3612 wpa_s->user_initiated_pd = 1;
3613 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3619 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3621 oper_freq = wpa_s->assoc_freq;
3623 oper_freq = wpa_drv_shared_freq(wpa_s);
3629 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3630 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3631 "(%u MHz) is not supported for P2P uses",
3636 if (oper_freq > 0 && freq != oper_freq &&
3637 !(wpa_s->drv_flags &
3638 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3639 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3640 "on %u MHz while connected on another "
3641 "channel (%u MHz)", freq, oper_freq);
3644 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3645 "requested channel (%u MHz)", freq);
3647 } else if (oper_freq > 0 &&
3648 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3649 if (!(wpa_s->drv_flags &
3650 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3651 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3652 "while connected on non-P2P supported "
3653 "channel (%u MHz)", oper_freq);
3656 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3657 "(%u MHz) not available for P2P - try to use "
3658 "another channel", oper_freq);
3660 } else if (oper_freq > 0 &&
3662 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3663 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3664 "are already using (%u MHz) on another interface",
3666 pref_freq = oper_freq;
3667 } else if (oper_freq > 0) {
3668 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3669 "channel we are already using (%u MHz) on another "
3670 "interface", oper_freq);
3671 force_freq = oper_freq;
3674 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3676 if (wpa_s->create_p2p_iface) {
3677 /* Prepare to add a new interface for the group */
3678 iftype = WPA_IF_P2P_GROUP;
3679 if (go_intent == 15)
3680 iftype = WPA_IF_P2P_GO;
3681 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3682 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3683 "interface for the group");
3687 if_addr = wpa_s->pending_interface_addr;
3689 if_addr = wpa_s->own_addr;
3692 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3694 force_freq, persistent_group, ssid,
3700 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3701 go_intent, if_addr, force_freq,
3702 persistent_group, ssid, pref_freq) < 0) {
3703 if (wpa_s->create_p2p_iface)
3704 wpas_p2p_remove_pending_group_interface(wpa_s);
3712 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3713 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3714 * @freq: Frequency of the channel in MHz
3715 * @duration: Duration of the stay on the channel in milliseconds
3717 * This callback is called when the driver indicates that it has started the
3718 * requested remain-on-channel duration.
3720 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3721 unsigned int freq, unsigned int duration)
3723 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3725 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3726 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3727 wpa_s->pending_listen_duration);
3728 wpa_s->pending_listen_freq = 0;
3733 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3734 unsigned int timeout)
3736 /* Limit maximum Listen state time based on driver limitation. */
3737 if (timeout > wpa_s->max_remain_on_chan)
3738 timeout = wpa_s->max_remain_on_chan;
3740 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3741 return wpa_drv_p2p_listen(wpa_s, timeout);
3743 return p2p_listen(wpa_s->global->p2p, timeout);
3748 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3749 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3750 * @freq: Frequency of the channel in MHz
3752 * This callback is called when the driver indicates that a remain-on-channel
3753 * operation has been completed, i.e., the duration on the requested channel
3756 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3759 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3760 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3761 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3762 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3764 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3765 return; /* P2P module started a new operation */
3766 if (wpa_s->pending_action_tx)
3768 if (wpa_s->p2p_long_listen > 0)
3769 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3770 if (wpa_s->p2p_long_listen > 0) {
3771 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3772 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3778 * wpas_p2p_group_remove - Remove a P2P group
3779 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3780 * @ifname: Network interface name of the group interface or "*" to remove all
3782 * Returns: 0 on success, -1 on failure
3784 * This function is used to remove a P2P group. This can be used to disconnect
3785 * from a group in which the local end is a P2P Client or to end a P2P Group in
3786 * case the local end is the Group Owner. If a virtual network interface was
3787 * created for this group, that interface will be removed. Otherwise, only the
3788 * configured P2P group network will be removed from the interface.
3790 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3792 struct wpa_global *global = wpa_s->global;
3794 if (os_strcmp(ifname, "*") == 0) {
3795 struct wpa_supplicant *prev;
3796 wpa_s = global->ifaces;
3799 wpa_s = wpa_s->next;
3800 wpas_p2p_disconnect(prev);
3805 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3806 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3810 return wpas_p2p_disconnect(wpa_s);
3814 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3815 struct p2p_go_neg_results *params,
3821 os_memset(params, 0, sizeof(*params));
3822 params->role_go = 1;
3823 params->ht40 = ht40;
3825 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3826 "frequency %d MHz", freq);
3827 params->freq = freq;
3828 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3829 wpa_s->conf->p2p_oper_channel >= 1 &&
3830 wpa_s->conf->p2p_oper_channel <= 11) {
3831 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3832 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3833 "frequency %d MHz", params->freq);
3834 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3835 wpa_s->conf->p2p_oper_reg_class == 124) {
3836 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3837 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3838 "frequency %d MHz", params->freq);
3839 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3840 wpa_s->best_overall_freq > 0 &&
3841 p2p_supported_freq(wpa_s->global->p2p,
3842 wpa_s->best_overall_freq)) {
3843 params->freq = wpa_s->best_overall_freq;
3844 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3845 "channel %d MHz", params->freq);
3846 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3847 wpa_s->best_24_freq > 0 &&
3848 p2p_supported_freq(wpa_s->global->p2p,
3849 wpa_s->best_24_freq)) {
3850 params->freq = wpa_s->best_24_freq;
3851 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3852 "channel %d MHz", params->freq);
3853 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3854 wpa_s->best_5_freq > 0 &&
3855 p2p_supported_freq(wpa_s->global->p2p,
3856 wpa_s->best_5_freq)) {
3857 params->freq = wpa_s->best_5_freq;
3858 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3859 "channel %d MHz", params->freq);
3862 for (chan = 0; chan < 11; chan++) {
3863 params->freq = 2412 + chan * 5;
3864 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3869 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3873 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3874 "known)", params->freq);
3877 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3878 wpa_s->assoc_freq && !freq) {
3879 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3881 params->freq = wpa_s->assoc_freq;
3884 res = wpa_drv_shared_freq(wpa_s);
3885 if (res > 0 && !freq) {
3886 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3887 "already using on a shared interface");
3889 } else if (res > 0 && freq != res &&
3890 !(wpa_s->drv_flags &
3891 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3892 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3893 "while connected on another channel (%u MHz)",
3902 static struct wpa_supplicant *
3903 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3906 struct wpa_supplicant *group_wpa_s;
3908 if (!wpas_p2p_create_iface(wpa_s)) {
3909 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
3914 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3915 WPA_IF_P2P_CLIENT) < 0) {
3916 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
3919 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3920 if (group_wpa_s == NULL) {
3921 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
3923 wpas_p2p_remove_pending_group_interface(wpa_s);
3927 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
3928 group_wpa_s->ifname);
3934 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3935 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3936 * @persistent_group: Whether to create a persistent group
3937 * @freq: Frequency for the group or 0 to indicate no hardcoding
3938 * Returns: 0 on success, -1 on failure
3940 * This function creates a new P2P group with the local end as the Group Owner,
3941 * i.e., without using Group Owner Negotiation.
3943 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3946 struct p2p_go_neg_results params;
3949 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3952 /* Make sure we are not running find during connection establishment */
3953 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3954 wpas_p2p_stop_find_oper(wpa_s);
3957 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3959 if (wpa_s->best_24_freq > 0 &&
3960 p2p_supported_freq(wpa_s->global->p2p,
3961 wpa_s->best_24_freq)) {
3962 freq = wpa_s->best_24_freq;
3963 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3964 "channel: %d MHz", freq);
3966 os_get_random((u8 *) &r, sizeof(r));
3967 freq = 2412 + (r % 3) * 25;
3968 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3969 "channel: %d MHz", freq);
3974 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3976 if (wpa_s->best_5_freq > 0 &&
3977 p2p_supported_freq(wpa_s->global->p2p,
3978 wpa_s->best_5_freq)) {
3979 freq = wpa_s->best_5_freq;
3980 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3981 "channel: %d MHz", freq);
3983 os_get_random((u8 *) &r, sizeof(r));
3984 freq = 5180 + (r % 4) * 20;
3985 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3986 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3987 "5 GHz channel for P2P group");
3990 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3991 "channel: %d MHz", freq);
3995 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3996 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3997 "(%u MHz) is not supported for P2P uses",
4002 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40))
4005 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4006 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4007 "(%u MHz) is not supported for P2P uses",
4011 p2p_go_params(wpa_s->global->p2p, ¶ms);
4012 params.persistent_group = persistent_group;
4014 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4017 wpas_start_wps_go(wpa_s, ¶ms, 0);
4023 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4024 struct wpa_ssid *params, int addr_allocated)
4026 struct wpa_ssid *ssid;
4028 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4032 wpa_supplicant_ap_deinit(wpa_s);
4034 ssid = wpa_config_add_network(wpa_s->conf);
4037 wpa_config_set_network_defaults(ssid);
4038 ssid->temporary = 1;
4039 ssid->proto = WPA_PROTO_RSN;
4040 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4041 ssid->group_cipher = WPA_CIPHER_CCMP;
4042 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4043 ssid->ssid = os_malloc(params->ssid_len);
4044 if (ssid->ssid == NULL) {
4045 wpa_config_remove_network(wpa_s->conf, ssid->id);
4048 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4049 ssid->ssid_len = params->ssid_len;
4050 ssid->p2p_group = 1;
4051 ssid->export_keys = 1;
4052 if (params->psk_set) {
4053 os_memcpy(ssid->psk, params->psk, 32);
4056 if (params->passphrase)
4057 ssid->passphrase = os_strdup(params->passphrase);
4059 wpa_supplicant_select_network(wpa_s, ssid);
4061 wpa_s->show_group_started = 1;
4067 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4068 struct wpa_ssid *ssid, int addr_allocated,
4071 struct p2p_go_neg_results params;
4074 if (ssid->disabled != 2 || ssid->ssid == NULL)
4077 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4078 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4079 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4084 /* Make sure we are not running find during connection establishment */
4085 wpas_p2p_stop_find_oper(wpa_s);
4087 wpa_s->p2p_fallback_to_go_neg = 0;
4089 if (ssid->mode == WPAS_MODE_INFRA)
4090 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4092 if (ssid->mode != WPAS_MODE_P2P_GO)
4095 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40))
4099 params.psk_set = ssid->psk_set;
4101 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
4102 if (ssid->passphrase == NULL ||
4103 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4104 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4108 os_strlcpy(params.passphrase, ssid->passphrase,
4109 sizeof(params.passphrase));
4110 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4111 params.ssid_len = ssid->ssid_len;
4112 params.persistent_group = 1;
4114 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4118 wpas_start_wps_go(wpa_s, ¶ms, 0);
4124 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4125 struct wpabuf *proberesp_ies)
4127 struct wpa_supplicant *wpa_s = ctx;
4128 if (wpa_s->ap_iface) {
4129 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4130 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4131 wpabuf_free(beacon_ies);
4132 wpabuf_free(proberesp_ies);
4136 wpabuf_free(hapd->p2p_beacon_ie);
4137 hapd->p2p_beacon_ie = beacon_ies;
4139 wpabuf_free(hapd->p2p_probe_resp_ie);
4140 hapd->p2p_probe_resp_ie = proberesp_ies;
4142 wpabuf_free(beacon_ies);
4143 wpabuf_free(proberesp_ies);
4145 wpa_supplicant_ap_update_beacon(wpa_s);
4149 static void wpas_p2p_idle_update(void *ctx, int idle)
4151 struct wpa_supplicant *wpa_s = ctx;
4152 if (!wpa_s->ap_iface)
4154 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4156 wpas_p2p_set_group_idle_timeout(wpa_s);
4158 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4162 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4163 struct wpa_ssid *ssid)
4165 struct p2p_group *group;
4166 struct p2p_group_config *cfg;
4168 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4170 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4173 cfg = os_zalloc(sizeof(*cfg));
4177 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4178 cfg->persistent_group = 2;
4179 else if (ssid->p2p_persistent_group)
4180 cfg->persistent_group = 1;
4181 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4182 if (wpa_s->max_stations &&
4183 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4184 cfg->max_clients = wpa_s->max_stations;
4186 cfg->max_clients = wpa_s->conf->max_num_sta;
4187 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4188 cfg->ssid_len = ssid->ssid_len;
4189 cfg->cb_ctx = wpa_s;
4190 cfg->ie_update = wpas_p2p_ie_update;
4191 cfg->idle_update = wpas_p2p_idle_update;
4193 group = p2p_group_init(wpa_s->global->p2p, cfg);
4196 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4197 p2p_group_notif_formation_done(group);
4198 wpa_s->p2p_group = group;
4203 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4206 struct wpa_ssid *ssid = wpa_s->current_ssid;
4208 if (!wpa_s->p2p_in_provisioning) {
4209 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4210 "provisioning not in progress");
4214 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4215 u8 go_dev_addr[ETH_ALEN];
4216 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4217 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4219 /* Clear any stored provisioning info */
4220 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4223 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4225 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4227 * Use a separate timeout for initial data connection to
4228 * complete to allow the group to be removed automatically if
4229 * something goes wrong in this step before the P2P group idle
4230 * timeout mechanism is taken into use.
4232 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4233 wpas_p2p_group_formation_timeout,
4234 wpa_s->parent, NULL);
4236 if (wpa_s->global->p2p)
4237 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4238 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4239 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4240 wpas_group_formation_completed(wpa_s, 1);
4244 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4245 struct wps_event_fail *fail)
4247 if (!wpa_s->p2p_in_provisioning) {
4248 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4249 "provisioning not in progress");
4253 if (wpa_s->go_params) {
4254 p2p_clear_provisioning_info(
4256 wpa_s->go_params->peer_device_addr);
4259 wpas_notify_p2p_wps_failed(wpa_s, fail);
4263 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4264 const char *config_method,
4265 enum wpas_p2p_prov_disc_use use)
4269 wpa_s->p2p_fallback_to_go_neg = 0;
4270 wpa_s->pending_pd_use = NORMAL_PD;
4271 if (os_strncmp(config_method, "display", 7) == 0)
4272 config_methods = WPS_CONFIG_DISPLAY;
4273 else if (os_strncmp(config_method, "keypad", 6) == 0)
4274 config_methods = WPS_CONFIG_KEYPAD;
4275 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4276 os_strncmp(config_method, "pushbutton", 10) == 0)
4277 config_methods = WPS_CONFIG_PUSHBUTTON;
4279 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
4283 if (use == WPAS_P2P_PD_AUTO) {
4284 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4285 wpa_s->pending_pd_config_methods = config_methods;
4286 wpa_s->p2p_auto_pd = 1;
4287 wpa_s->p2p_auto_join = 0;
4288 wpa_s->pending_pd_before_join = 0;
4289 wpa_s->auto_pd_scan_retry = 0;
4290 wpas_p2p_stop_find(wpa_s);
4291 wpa_s->p2p_join_scan_count = 0;
4292 os_get_time(&wpa_s->p2p_auto_started);
4293 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4294 wpa_s->p2p_auto_started.sec,
4295 wpa_s->p2p_auto_started.usec);
4296 wpas_p2p_join_scan(wpa_s, NULL);
4300 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4301 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
4303 use == WPAS_P2P_PD_FOR_JOIN);
4306 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4309 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
4310 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4315 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4318 return p2p_scan_result_text(ies, ies_len, buf, end);
4322 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4324 if (!wpa_s->pending_action_tx)
4327 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4328 "operation request");
4329 wpabuf_free(wpa_s->pending_action_tx);
4330 wpa_s->pending_action_tx = NULL;
4334 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4335 enum p2p_discovery_type type,
4336 unsigned int num_req_dev_types, const u8 *req_dev_types,
4337 const u8 *dev_id, unsigned int search_delay)
4339 wpas_p2p_clear_pending_action_tx(wpa_s);
4340 wpa_s->p2p_long_listen = 0;
4342 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4343 return wpa_drv_p2p_find(wpa_s, timeout, type);
4345 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4346 wpa_s->p2p_in_provisioning)
4349 wpa_supplicant_cancel_sched_scan(wpa_s);
4351 return p2p_find(wpa_s->global->p2p, timeout, type,
4352 num_req_dev_types, req_dev_types, dev_id,
4357 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
4359 wpas_p2p_clear_pending_action_tx(wpa_s);
4360 wpa_s->p2p_long_listen = 0;
4361 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4362 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4363 wpa_s->global->p2p_cb_on_scan_complete = 0;
4365 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4366 wpa_drv_p2p_stop_find(wpa_s);
4370 if (wpa_s->global->p2p)
4371 p2p_stop_find(wpa_s->global->p2p);
4377 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4379 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4381 wpas_p2p_remove_pending_group_interface(wpa_s);
4385 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4387 struct wpa_supplicant *wpa_s = eloop_ctx;
4388 wpa_s->p2p_long_listen = 0;
4392 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4396 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4399 wpa_supplicant_cancel_sched_scan(wpa_s);
4400 wpas_p2p_clear_pending_action_tx(wpa_s);
4404 * This is a request for unlimited Listen state. However, at
4405 * least for now, this is mapped to a Listen state for one
4410 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4411 wpa_s->p2p_long_listen = 0;
4414 * Stop previous find/listen operation to avoid trying to request a new
4415 * remain-on-channel operation while the driver is still running the
4418 if (wpa_s->global->p2p)
4419 p2p_stop_find(wpa_s->global->p2p);
4421 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4422 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4423 wpa_s->p2p_long_listen = timeout * 1000;
4424 eloop_register_timeout(timeout, 0,
4425 wpas_p2p_long_listen_timeout,
4433 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4434 u8 *buf, size_t len, int p2p_group)
4436 struct wpabuf *p2p_ie;
4439 if (wpa_s->global->p2p_disabled)
4441 if (wpa_s->global->p2p == NULL)
4446 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4447 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4449 wpabuf_free(p2p_ie);
4455 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4456 const u8 *dst, const u8 *bssid,
4457 const u8 *ie, size_t ie_len, int ssi_signal)
4459 if (wpa_s->global->p2p_disabled)
4461 if (wpa_s->global->p2p == NULL)
4464 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4466 case P2P_PREQ_NOT_P2P:
4467 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4470 case P2P_PREQ_MALFORMED:
4471 case P2P_PREQ_NOT_LISTEN:
4472 case P2P_PREQ_NOT_PROCESSED:
4473 default: /* make gcc happy */
4475 case P2P_PREQ_PROCESSED:
4481 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4482 const u8 *sa, const u8 *bssid,
4483 u8 category, const u8 *data, size_t len, int freq)
4485 if (wpa_s->global->p2p_disabled)
4487 if (wpa_s->global->p2p == NULL)
4490 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4495 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4497 if (wpa_s->global->p2p_disabled)
4499 if (wpa_s->global->p2p == NULL)
4502 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4506 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4508 p2p_group_deinit(wpa_s->p2p_group);
4509 wpa_s->p2p_group = NULL;
4511 wpa_s->ap_configured_cb = NULL;
4512 wpa_s->ap_configured_cb_ctx = NULL;
4513 wpa_s->ap_configured_cb_data = NULL;
4514 wpa_s->connect_without_scan = NULL;
4518 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4520 wpa_s->p2p_long_listen = 0;
4522 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4523 return wpa_drv_p2p_reject(wpa_s, addr);
4525 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4528 return p2p_reject(wpa_s->global->p2p, addr);
4532 /* Invite to reinvoke a persistent group */
4533 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4534 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4537 enum p2p_invite_role role;
4540 wpa_s->p2p_persistent_go_freq = freq;
4541 wpa_s->p2p_go_ht40 = !!ht40;
4542 if (ssid->mode == WPAS_MODE_P2P_GO) {
4543 role = P2P_INVITE_ROLE_GO;
4544 if (peer_addr == NULL) {
4545 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4546 "address in invitation command");
4549 if (wpas_p2p_create_iface(wpa_s)) {
4550 if (wpas_p2p_add_group_interface(wpa_s,
4551 WPA_IF_P2P_GO) < 0) {
4552 wpa_printf(MSG_ERROR, "P2P: Failed to "
4553 "allocate a new interface for the "
4557 bssid = wpa_s->pending_interface_addr;
4559 bssid = wpa_s->own_addr;
4561 role = P2P_INVITE_ROLE_CLIENT;
4562 peer_addr = ssid->bssid;
4564 wpa_s->pending_invite_ssid_id = ssid->id;
4566 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4567 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4568 ssid->ssid, ssid->ssid_len,
4571 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4574 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4575 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4579 /* Invite to join an active group */
4580 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4581 const u8 *peer_addr, const u8 *go_dev_addr)
4583 struct wpa_global *global = wpa_s->global;
4584 enum p2p_invite_role role;
4586 struct wpa_ssid *ssid;
4589 wpa_s->p2p_persistent_go_freq = 0;
4590 wpa_s->p2p_go_ht40 = 0;
4592 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4593 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4596 if (wpa_s == NULL) {
4597 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4601 ssid = wpa_s->current_ssid;
4603 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4608 persistent = ssid->p2p_persistent_group &&
4609 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4610 ssid->ssid, ssid->ssid_len);
4612 if (ssid->mode == WPAS_MODE_P2P_GO) {
4613 role = P2P_INVITE_ROLE_ACTIVE_GO;
4614 bssid = wpa_s->own_addr;
4615 if (go_dev_addr == NULL)
4616 go_dev_addr = wpa_s->global->p2p_dev_addr;
4618 role = P2P_INVITE_ROLE_CLIENT;
4619 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4620 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4621 "invite to current group");
4624 bssid = wpa_s->bssid;
4625 if (go_dev_addr == NULL &&
4626 !is_zero_ether_addr(wpa_s->go_dev_addr))
4627 go_dev_addr = wpa_s->go_dev_addr;
4629 wpa_s->parent->pending_invite_ssid_id = -1;
4631 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4632 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4633 ssid->ssid, ssid->ssid_len,
4634 go_dev_addr, persistent);
4636 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4639 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4640 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
4641 go_dev_addr, persistent);
4645 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4647 struct wpa_ssid *ssid = wpa_s->current_ssid;
4648 const char *ssid_txt;
4649 u8 go_dev_addr[ETH_ALEN];
4650 int network_id = -1;
4654 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4655 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4656 wpa_s->parent, NULL);
4659 if (!wpa_s->show_group_started || !ssid)
4662 wpa_s->show_group_started = 0;
4664 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4665 os_memset(go_dev_addr, 0, ETH_ALEN);
4666 if (ssid->bssid_set)
4667 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4668 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4670 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4672 if (wpa_s->global->p2p_group_formation == wpa_s)
4673 wpa_s->global->p2p_group_formation = NULL;
4675 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4676 (int) wpa_s->assoc_freq;
4677 if (ssid->passphrase == NULL && ssid->psk_set) {
4679 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4680 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4681 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4683 wpa_s->ifname, ssid_txt, freq, psk,
4684 MAC2STR(go_dev_addr),
4685 persistent ? " [PERSISTENT]" : "");
4687 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4688 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4689 "go_dev_addr=" MACSTR "%s",
4690 wpa_s->ifname, ssid_txt, freq,
4691 ssid->passphrase ? ssid->passphrase : "",
4692 MAC2STR(go_dev_addr),
4693 persistent ? " [PERSISTENT]" : "");
4697 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4700 network_id = ssid->id;
4701 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4704 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4705 wpa_s->global->p2p != NULL) {
4706 wpa_s->global->p2p_cb_on_scan_complete = 0;
4707 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4708 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4709 "continued after successful connection");
4710 p2p_increase_search_delay(
4712 wpas_p2p_search_delay(wpa_s));
4718 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4719 u32 interval1, u32 duration2, u32 interval2)
4721 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4723 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4726 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4727 wpa_s->current_ssid == NULL ||
4728 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4731 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4732 wpa_s->own_addr, wpa_s->assoc_freq,
4733 duration1, interval1, duration2, interval2);
4737 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4738 unsigned int interval)
4740 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4743 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4746 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4750 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4752 if (wpa_s->current_ssid == NULL) {
4754 * current_ssid can be cleared when P2P client interface gets
4755 * disconnected, so assume this interface was used as P2P
4760 return wpa_s->current_ssid->p2p_group &&
4761 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4765 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4767 struct wpa_supplicant *wpa_s = eloop_ctx;
4769 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4770 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4775 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4777 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
4781 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4785 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4786 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4788 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4791 timeout = wpa_s->conf->p2p_group_idle;
4792 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4793 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4794 timeout = P2P_MAX_CLIENT_IDLE;
4800 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4801 timeout = 0; /* special client mode no-timeout */
4806 if (wpa_s->p2p_in_provisioning) {
4808 * Use the normal group formation timeout during the
4809 * provisioning phase to avoid terminating this process too
4810 * early due to group idle timeout.
4812 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4813 "during provisioning");
4817 if (wpa_s->show_group_started) {
4819 * Use the normal group formation timeout between the end of
4820 * the provisioning phase and completion of 4-way handshake to
4821 * avoid terminating this process too early due to group idle
4824 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4825 "while waiting for initial 4-way handshake to "
4830 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4832 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4837 /* Returns 1 if the interface was removed */
4838 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4839 u16 reason_code, const u8 *ie, size_t ie_len,
4840 int locally_generated)
4842 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4844 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4847 if (!locally_generated)
4848 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4851 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4852 wpa_s->current_ssid &&
4853 wpa_s->current_ssid->p2p_group &&
4854 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4855 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4856 "session is ending");
4857 if (wpas_p2p_group_delete(wpa_s,
4858 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4867 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4868 u16 reason_code, const u8 *ie, size_t ie_len,
4869 int locally_generated)
4871 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4873 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4876 if (!locally_generated)
4877 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4882 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4884 struct p2p_data *p2p = wpa_s->global->p2p;
4889 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4892 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4893 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4895 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4896 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4899 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4900 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4902 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4903 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4905 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4906 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4907 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4908 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4909 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4912 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4913 p2p_set_sec_dev_types(p2p,
4914 (void *) wpa_s->conf->sec_device_type,
4915 wpa_s->conf->num_sec_device_types);
4917 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4919 p2p_remove_wps_vendor_extensions(p2p);
4920 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4921 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4923 p2p_add_wps_vendor_extension(
4924 p2p, wpa_s->conf->wps_vendor_ext[i]);
4928 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4929 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4931 country[0] = wpa_s->conf->country[0];
4932 country[1] = wpa_s->conf->country[1];
4934 p2p_set_country(p2p, country);
4937 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4938 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4939 wpa_s->conf->p2p_ssid_postfix ?
4940 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4944 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4945 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4947 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4948 u8 reg_class, channel;
4951 if (wpa_s->conf->p2p_listen_reg_class &&
4952 wpa_s->conf->p2p_listen_channel) {
4953 reg_class = wpa_s->conf->p2p_listen_reg_class;
4954 channel = wpa_s->conf->p2p_listen_channel;
4958 * Pick one of the social channels randomly as the
4961 os_get_random((u8 *) &r, sizeof(r));
4962 channel = 1 + (r % 3) * 5;
4964 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4966 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4969 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4970 u8 op_reg_class, op_channel, cfg_op_channel;
4973 if (wpa_s->conf->p2p_oper_reg_class &&
4974 wpa_s->conf->p2p_oper_channel) {
4975 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4976 op_channel = wpa_s->conf->p2p_oper_channel;
4981 * Use random operation channel from (1, 6, 11)
4982 *if no other preference is indicated.
4984 os_get_random((u8 *) &r, sizeof(r));
4985 op_channel = 1 + (r % 3) * 5;
4988 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4991 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4995 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4996 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4997 wpa_s->conf->p2p_pref_chan) < 0) {
4998 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5005 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5008 if (!wpa_s->ap_iface)
5010 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5015 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5017 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5019 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5022 wpa_s->global->cross_connection = enabled;
5023 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5026 struct wpa_supplicant *iface;
5028 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5030 if (iface->cross_connect_enabled == 0)
5033 iface->cross_connect_enabled = 0;
5034 iface->cross_connect_in_use = 0;
5035 wpa_msg(iface->parent, MSG_INFO,
5036 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5037 iface->ifname, iface->cross_connect_uplink);
5045 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5047 struct wpa_supplicant *iface;
5049 if (!uplink->global->cross_connection)
5052 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5053 if (!iface->cross_connect_enabled)
5055 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5058 if (iface->ap_iface == NULL)
5060 if (iface->cross_connect_in_use)
5063 iface->cross_connect_in_use = 1;
5064 wpa_msg(iface->parent, MSG_INFO,
5065 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5066 iface->ifname, iface->cross_connect_uplink);
5071 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5073 struct wpa_supplicant *iface;
5075 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5076 if (!iface->cross_connect_enabled)
5078 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5081 if (!iface->cross_connect_in_use)
5084 wpa_msg(iface->parent, MSG_INFO,
5085 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5086 iface->ifname, iface->cross_connect_uplink);
5087 iface->cross_connect_in_use = 0;
5092 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5094 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5095 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5096 wpa_s->cross_connect_disallowed)
5097 wpas_p2p_disable_cross_connect(wpa_s);
5099 wpas_p2p_enable_cross_connect(wpa_s);
5100 if (!wpa_s->ap_iface &&
5101 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5102 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5106 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5108 wpas_p2p_disable_cross_connect(wpa_s);
5109 if (!wpa_s->ap_iface &&
5110 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5112 wpas_p2p_set_group_idle_timeout(wpa_s);
5116 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5118 struct wpa_supplicant *iface;
5120 if (!wpa_s->global->cross_connection)
5123 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5126 if (iface->drv_flags &
5127 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5129 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5132 wpa_s->cross_connect_enabled = 1;
5133 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5134 sizeof(wpa_s->cross_connect_uplink));
5135 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5136 "%s to %s whenever uplink is available",
5137 wpa_s->ifname, wpa_s->cross_connect_uplink);
5139 if (iface->ap_iface || iface->current_ssid == NULL ||
5140 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5141 iface->cross_connect_disallowed ||
5142 iface->wpa_state != WPA_COMPLETED)
5145 wpa_s->cross_connect_in_use = 1;
5146 wpa_msg(wpa_s->parent, MSG_INFO,
5147 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5148 wpa_s->ifname, wpa_s->cross_connect_uplink);
5154 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5156 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5157 !wpa_s->p2p_in_provisioning)
5158 return 0; /* not P2P client operation */
5160 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5162 if (wpa_s != wpa_s->parent)
5163 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5165 if (wpa_s->global->p2p)
5166 p2p_group_formation_failed(wpa_s->global->p2p);
5168 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5169 wpa_s->parent, NULL);
5171 wpas_group_formation_completed(wpa_s, 0);
5176 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5178 struct p2p_channels chan;
5180 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5183 os_memset(&chan, 0, sizeof(chan));
5184 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5185 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5190 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5194 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5195 struct wpa_scan_results *scan_res)
5197 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5201 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5203 struct wpa_global *global = wpa_s->global;
5207 if (global->p2p == NULL)
5210 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5212 if (wpa_s->pending_interface_name[0] &&
5213 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5216 peer = p2p_get_go_neg_peer(global->p2p);
5218 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5219 MACSTR, MAC2STR(peer));
5220 p2p_unauthorize(global->p2p, peer);
5224 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5225 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5226 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5230 if (wpa_s->pending_pd_before_join) {
5231 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5232 wpa_s->pending_pd_before_join = 0;
5236 wpas_p2p_stop_find(wpa_s);
5238 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5239 if (wpa_s == global->p2p_group_formation &&
5240 (wpa_s->p2p_in_provisioning ||
5241 wpa_s->parent->pending_interface_type ==
5242 WPA_IF_P2P_CLIENT)) {
5243 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5244 "formation found - cancelling",
5247 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5248 wpa_s->parent, NULL);
5249 if (wpa_s->p2p_in_provisioning) {
5250 wpas_group_formation_completed(wpa_s, 0);
5253 wpas_p2p_group_delete(wpa_s,
5254 P2P_GROUP_REMOVAL_REQUESTED);
5260 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5268 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5270 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5273 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5274 "being available anymore");
5275 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
5279 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5280 int freq_24, int freq_5, int freq_overall)
5282 struct p2p_data *p2p = wpa_s->global->p2p;
5283 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5285 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5289 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5292 struct p2p_data *p2p = wpa_s->global->p2p;
5294 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5297 if (hwaddr_aton(addr, peer))
5300 return p2p_unauthorize(p2p, peer);
5305 * wpas_p2p_disconnect - Disconnect from a P2P Group
5306 * @wpa_s: Pointer to wpa_supplicant data
5307 * Returns: 0 on success, -1 on failure
5309 * This can be used to disconnect from a group in which the local end is a P2P
5310 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5311 * virtual network interface was created for this group, that interface will be
5312 * removed. Otherwise, only the configured P2P group network will be removed
5313 * from the interface.
5315 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5321 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5326 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5328 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5331 return p2p_in_progress(wpa_s->global->p2p);
5335 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5336 struct wpa_ssid *ssid)
5338 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5339 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5340 wpa_s->parent, NULL) > 0) {
5342 * Remove the network by scheduling the group formation
5343 * timeout to happen immediately. The teardown code
5344 * needs to be scheduled to run asynch later so that we
5345 * don't delete data from under ourselves unexpectedly.
5346 * Calling wpas_p2p_group_formation_timeout directly
5347 * causes a series of crashes in WPS failure scenarios.
5349 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5350 "P2P group network getting removed");
5351 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5352 wpa_s->parent, NULL);
5357 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
5358 const u8 *addr, const u8 *ssid,
5364 for (s = wpa_s->conf->ssid; s; s = s->next) {
5365 if (s->disabled != 2)
5368 (ssid_len != s->ssid_len ||
5369 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5371 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5372 return s; /* peer is GO in the persistent group */
5373 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5375 for (i = 0; i < s->num_p2p_clients; i++) {
5376 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5377 addr, ETH_ALEN) == 0)
5378 return s; /* peer is P2P client in persistent
5387 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5392 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5396 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5399 struct wpa_supplicant *group = wpa_s;
5400 if (wpa_s->global->p2p_group_formation)
5401 group = wpa_s->global->p2p_group_formation;
5402 wpa_s = wpa_s->parent;
5403 offchannel_send_action_done(wpa_s);
5405 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
5406 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5407 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5408 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5409 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5410 wpa_s->p2p_persistent_id,
5411 wpa_s->p2p_pd_before_go_neg,
5412 wpa_s->p2p_go_ht40);
5416 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5418 if (!wpa_s->p2p_fallback_to_go_neg ||
5419 wpa_s->p2p_in_provisioning <= 5)
5422 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5423 return 0; /* peer operating as a GO */
5425 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5426 "fallback to GO Negotiation");
5427 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5433 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5435 const char *rn, *rn2;
5436 struct wpa_supplicant *ifs;
5438 if (wpa_s->wpa_state > WPA_SCANNING) {
5439 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5440 "concurrent operation",
5441 P2P_CONCURRENT_SEARCH_DELAY);
5442 return P2P_CONCURRENT_SEARCH_DELAY;
5445 if (!wpa_s->driver->get_radio_name)
5447 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5448 if (rn == NULL || rn[0] == '\0')
5451 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5452 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5455 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5456 if (!rn2 || os_strcmp(rn, rn2) != 0)
5458 if (ifs->wpa_state > WPA_SCANNING) {
5459 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5460 "delay due to concurrent operation on "
5462 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5463 return P2P_CONCURRENT_SEARCH_DELAY;