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_group_formation_timeout(void *eloop_ctx,
91 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
93 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
96 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
97 struct wpa_scan_results *scan_res)
101 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
104 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
105 (int) scan_res->num);
107 for (i = 0; i < scan_res->num; i++) {
108 struct wpa_scan_res *bss = scan_res->res[i];
109 struct os_time time_tmp_age, entry_ts;
110 time_tmp_age.sec = bss->age / 1000;
111 time_tmp_age.usec = (bss->age % 1000) * 1000;
112 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
113 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
114 bss->freq, &entry_ts, bss->level,
115 (const u8 *) (bss + 1),
120 p2p_scan_res_handled(wpa_s->global->p2p);
124 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
125 unsigned int num_req_dev_types,
126 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
128 struct wpa_supplicant *wpa_s = ctx;
129 struct wpa_supplicant *ifs;
130 struct wpa_driver_scan_params params;
132 struct wpabuf *wps_ie, *ies;
133 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
136 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
139 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
140 if (ifs->sta_scan_pending &&
141 wpas_p2p_in_progress(wpa_s) == 2) {
142 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
143 "pending station mode scan to be "
144 "completed on interface %s", ifs->ifname);
145 wpa_s->global->p2p_cb_on_scan_complete = 1;
146 wpa_supplicant_req_scan(ifs, 0, 0);
151 os_memset(¶ms, 0, sizeof(params));
153 /* P2P Wildcard SSID */
154 params.num_ssids = 1;
155 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
156 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
158 wpa_s->wps->dev.p2p = 1;
159 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
160 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
161 num_req_dev_types, req_dev_types);
165 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
166 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
171 wpabuf_put_buf(ies, wps_ie);
174 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
176 params.p2p_probe = 1;
177 params.extra_ies = wpabuf_head(ies);
178 params.extra_ies_len = wpabuf_len(ies);
181 case P2P_SCAN_SOCIAL:
182 params.freqs = social_channels;
186 case P2P_SCAN_SOCIAL_PLUS_ONE:
187 social_channels[3] = freq;
188 params.freqs = social_channels;
192 ret = wpa_drv_scan(wpa_s, ¶ms);
197 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
199 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
200 wpa_s->global->p2p_cb_on_scan_complete = 1;
206 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
212 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
214 switch (p2p_group_interface) {
215 case P2P_GROUP_INTERFACE_PENDING:
216 return WPA_IF_P2P_GROUP;
217 case P2P_GROUP_INTERFACE_GO:
218 return WPA_IF_P2P_GO;
219 case P2P_GROUP_INTERFACE_CLIENT:
220 return WPA_IF_P2P_CLIENT;
223 return WPA_IF_P2P_GROUP;
227 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
229 size_t ssid_len, int *go)
233 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
234 for (s = wpa_s->conf->ssid; s; s = s->next) {
235 if (s->disabled != 0 || !s->p2p_group ||
236 s->ssid_len != ssid_len ||
237 os_memcmp(ssid, s->ssid, ssid_len) != 0)
239 if (s->mode == WPAS_MODE_P2P_GO &&
240 s != wpa_s->current_ssid)
243 *go = s->mode == WPAS_MODE_P2P_GO;
252 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
253 enum p2p_group_removal_reason removal_reason)
255 struct wpa_ssid *ssid;
259 ssid = wpa_s->current_ssid;
262 * The current SSID was not known, but there may still be a
263 * pending P2P group interface waiting for provisioning or a
264 * P2P group that is trying to reconnect.
266 ssid = wpa_s->conf->ssid;
268 if (ssid->p2p_group && ssid->disabled != 2)
273 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
275 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
280 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
282 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
283 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
284 wpa_s->reassociate = 0;
285 wpa_s->disconnected = 1;
286 wpa_supplicant_deauthenticate(wpa_s,
287 WLAN_REASON_DEAUTH_LEAVING);
291 if (wpa_s->cross_connect_in_use) {
292 wpa_s->cross_connect_in_use = 0;
293 wpa_msg(wpa_s->parent, MSG_INFO,
294 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
295 wpa_s->ifname, wpa_s->cross_connect_uplink);
297 switch (removal_reason) {
298 case P2P_GROUP_REMOVAL_REQUESTED:
299 reason = " reason=REQUESTED";
301 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
302 reason = " reason=FORMATION_FAILED";
304 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
305 reason = " reason=IDLE";
307 case P2P_GROUP_REMOVAL_UNAVAILABLE:
308 reason = " reason=UNAVAILABLE";
310 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
311 reason = " reason=GO_ENDING_SESSION";
317 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
318 wpa_msg(wpa_s->parent, MSG_INFO,
319 P2P_EVENT_GROUP_REMOVED "%s %s%s",
320 wpa_s->ifname, gtype, reason);
323 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
324 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
325 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
326 wpa_s->parent, NULL) > 0)
327 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
330 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
331 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
333 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
334 struct wpa_global *global;
336 enum wpa_driver_if_type type;
337 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
339 global = wpa_s->global;
340 ifname = os_strdup(wpa_s->ifname);
341 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
342 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
343 wpa_s = global->ifaces;
345 wpa_drv_if_remove(wpa_s, type, ifname);
350 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
351 if (ssid && (ssid->p2p_group ||
352 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
353 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
355 if (ssid == wpa_s->current_ssid) {
356 wpa_sm_set_config(wpa_s->wpa, NULL);
357 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
358 wpa_s->current_ssid = NULL;
361 * Networks objects created during any P2P activities are not
362 * exposed out as they might/will confuse certain non-P2P aware
363 * applications since these network objects won't behave like
366 * Likewise, we don't send out network removed signals for such
369 wpa_config_remove_network(wpa_s->conf, id);
370 wpa_supplicant_clear_status(wpa_s);
371 wpa_supplicant_cancel_sched_scan(wpa_s);
372 wpa_s->sta_scan_pending = 0;
374 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
378 wpa_supplicant_ap_deinit(wpa_s);
380 wpa_drv_deinit_p2p_cli(wpa_s);
386 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
388 const u8 *ssid, size_t ssid_len)
396 if (wpa_s->go_params)
397 bssid = wpa_s->go_params->peer_interface_addr;
399 bssid = wpa_s->bssid;
401 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
403 u8 iface_addr[ETH_ALEN];
404 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
406 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
409 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
410 "group is persistent - BSS " MACSTR " not found",
415 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
417 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
418 "group is persistent - BSS " MACSTR
419 " did not include P2P IE", MAC2STR(bssid));
420 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
421 (u8 *) (bss + 1), bss->ie_len);
422 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
423 ((u8 *) bss + 1) + bss->ie_len,
428 group_capab = p2p_get_group_capab(p2p);
429 addr = p2p_get_go_dev_addr(p2p);
430 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
431 "group_capab=0x%x", group_capab);
433 os_memcpy(go_dev_addr, addr, ETH_ALEN);
434 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
437 os_memset(go_dev_addr, 0, ETH_ALEN);
440 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
441 "go_dev_addr=" MACSTR,
442 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
444 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
448 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
449 struct wpa_ssid *ssid,
450 const u8 *go_dev_addr)
455 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
456 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
457 for (s = wpa_s->conf->ssid; s; s = s->next) {
458 if (s->disabled == 2 &&
459 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
460 s->ssid_len == ssid->ssid_len &&
461 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
466 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
468 if (ssid->passphrase && !s->passphrase)
470 else if (ssid->passphrase && s->passphrase &&
471 os_strcmp(ssid->passphrase, s->passphrase) != 0)
474 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
477 s = wpa_config_add_network(wpa_s->conf);
482 * Instead of network_added we emit persistent_group_added
483 * notification. Also to keep the defense checks in
484 * persistent_group obj registration method, we set the
485 * relevant flags in s to designate it as a persistent group.
488 s->p2p_persistent_group = 1;
489 wpas_notify_persistent_group_added(wpa_s, s);
490 wpa_config_set_network_defaults(s);
494 s->p2p_persistent_group = 1;
497 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
498 s->mode = ssid->mode;
499 s->auth_alg = WPA_AUTH_ALG_OPEN;
500 s->key_mgmt = WPA_KEY_MGMT_PSK;
501 s->proto = WPA_PROTO_RSN;
502 s->pairwise_cipher = WPA_CIPHER_CCMP;
504 if (ssid->passphrase) {
505 os_free(s->passphrase);
506 s->passphrase = os_strdup(ssid->passphrase);
510 os_memcpy(s->psk, ssid->psk, 32);
512 if (s->passphrase && !s->psk_set)
513 wpa_config_update_psk(s);
514 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
516 s->ssid = os_malloc(ssid->ssid_len);
519 s->ssid_len = ssid->ssid_len;
520 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
523 #ifndef CONFIG_NO_CONFIG_WRITE
524 if (changed && wpa_s->conf->update_config &&
525 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
526 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
528 #endif /* CONFIG_NO_CONFIG_WRITE */
534 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
537 struct wpa_ssid *ssid, *s;
542 ssid = wpa_s->current_ssid;
543 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
544 !ssid->p2p_persistent_group)
547 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
548 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
551 if (s->ssid_len == ssid->ssid_len &&
552 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
559 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
560 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
564 if (i == s->num_p2p_clients - 1)
565 return; /* already the most recent entry */
567 /* move the entry to mark it most recent */
568 os_memmove(s->p2p_client_list + i * ETH_ALEN,
569 s->p2p_client_list + (i + 1) * ETH_ALEN,
570 (s->num_p2p_clients - i - 1) * ETH_ALEN);
571 os_memcpy(s->p2p_client_list +
572 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
577 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
578 n = os_realloc_array(s->p2p_client_list,
579 s->num_p2p_clients + 1, ETH_ALEN);
582 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
583 s->p2p_client_list = n;
584 s->num_p2p_clients++;
586 /* Not enough room for an additional entry - drop the oldest
588 os_memmove(s->p2p_client_list,
589 s->p2p_client_list + ETH_ALEN,
590 (s->num_p2p_clients - 1) * ETH_ALEN);
591 os_memcpy(s->p2p_client_list +
592 (s->num_p2p_clients - 1) * ETH_ALEN,
596 #ifndef CONFIG_NO_CONFIG_WRITE
597 if (wpa_s->parent->conf->update_config &&
598 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
599 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
600 #endif /* CONFIG_NO_CONFIG_WRITE */
604 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
607 struct wpa_ssid *ssid;
608 const char *ssid_txt;
611 u8 go_dev_addr[ETH_ALEN];
615 * This callback is likely called for the main interface. Update wpa_s
616 * to use the group interface if a new interface was created for the
619 if (wpa_s->global->p2p_group_formation)
620 wpa_s = wpa_s->global->p2p_group_formation;
621 wpa_s->global->p2p_group_formation = NULL;
622 wpa_s->p2p_in_provisioning = 0;
625 wpa_msg(wpa_s->parent, MSG_INFO,
626 P2P_EVENT_GROUP_FORMATION_FAILURE);
627 wpas_p2p_group_delete(wpa_s,
628 P2P_GROUP_REMOVAL_FORMATION_FAILED);
632 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
634 ssid = wpa_s->current_ssid;
635 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
636 ssid->mode = WPAS_MODE_P2P_GO;
637 p2p_group_notif_formation_done(wpa_s->p2p_group);
638 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
643 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
644 client = ssid->mode == WPAS_MODE_INFRA;
645 if (ssid->mode == WPAS_MODE_P2P_GO) {
646 persistent = ssid->p2p_persistent_group;
647 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
650 persistent = wpas_p2p_persistent_group(wpa_s,
656 client = wpa_s->p2p_group_interface ==
657 P2P_GROUP_INTERFACE_CLIENT;
658 os_memset(go_dev_addr, 0, ETH_ALEN);
661 wpa_s->show_group_started = 0;
664 * Indicate event only after successfully completed 4-way
665 * handshake, i.e., when the interface is ready for data
668 wpa_s->show_group_started = 1;
669 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
671 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
672 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
673 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
675 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
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);
681 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
682 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
683 "go_dev_addr=" MACSTR "%s",
684 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
685 ssid && ssid->passphrase ? ssid->passphrase : "",
686 MAC2STR(go_dev_addr),
687 persistent ? " [PERSISTENT]" : "");
688 wpas_p2p_cross_connect_setup(wpa_s);
689 wpas_p2p_set_group_idle_timeout(wpa_s);
693 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
695 if (network_id < 0 && ssid)
696 network_id = ssid->id;
698 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
702 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
704 const u8 *dst, const u8 *src,
706 const u8 *data, size_t data_len,
707 enum offchannel_send_action_result
710 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
712 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
714 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
718 case OFFCHANNEL_SEND_ACTION_SUCCESS:
719 res = P2P_SEND_ACTION_SUCCESS;
721 case OFFCHANNEL_SEND_ACTION_NO_ACK:
722 res = P2P_SEND_ACTION_NO_ACK;
724 case OFFCHANNEL_SEND_ACTION_FAILED:
725 res = P2P_SEND_ACTION_FAILED;
729 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
731 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
732 wpa_s->pending_pd_before_join &&
733 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
734 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
735 wpa_s->p2p_fallback_to_go_neg) {
736 wpa_s->pending_pd_before_join = 0;
737 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
738 "during p2p_connect-auto");
739 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
745 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
746 const u8 *src, const u8 *bssid, const u8 *buf,
747 size_t len, unsigned int wait_time)
749 struct wpa_supplicant *wpa_s = ctx;
750 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
752 wpas_p2p_send_action_tx_status, 1);
756 static void wpas_send_action_done(void *ctx)
758 struct wpa_supplicant *wpa_s = ctx;
759 offchannel_send_action_done(wpa_s);
763 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
764 struct p2p_go_neg_results *params)
766 if (wpa_s->go_params == NULL) {
767 wpa_s->go_params = os_malloc(sizeof(*params));
768 if (wpa_s->go_params == NULL)
771 os_memcpy(wpa_s->go_params, params, sizeof(*params));
776 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
777 struct p2p_go_neg_results *res)
779 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
780 MAC2STR(res->peer_interface_addr));
781 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
782 res->ssid, res->ssid_len);
783 wpa_supplicant_ap_deinit(wpa_s);
784 wpas_copy_go_neg_results(wpa_s, res);
785 if (res->wps_method == WPS_PBC)
786 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
788 u16 dev_pw_id = DEV_PW_DEFAULT;
789 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
790 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
791 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
792 wpa_s->p2p_pin, 1, dev_pw_id);
797 static void p2p_go_configured(void *ctx, void *data)
799 struct wpa_supplicant *wpa_s = ctx;
800 struct p2p_go_neg_results *params = data;
801 struct wpa_ssid *ssid;
804 ssid = wpa_s->current_ssid;
805 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
806 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
807 if (wpa_s->global->p2p_group_formation == wpa_s)
808 wpa_s->global->p2p_group_formation = NULL;
809 if (os_strlen(params->passphrase) > 0) {
810 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
811 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
812 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
813 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
814 ssid->frequency, params->passphrase,
815 MAC2STR(wpa_s->global->p2p_dev_addr),
816 params->persistent_group ? " [PERSISTENT]" :
820 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
821 sizeof(params->psk));
822 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
823 "%s GO ssid=\"%s\" freq=%d psk=%s "
824 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
825 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
826 ssid->frequency, psk,
827 MAC2STR(wpa_s->global->p2p_dev_addr),
828 params->persistent_group ? " [PERSISTENT]" :
832 if (params->persistent_group)
833 network_id = wpas_p2p_store_persistent_group(
835 wpa_s->global->p2p_dev_addr);
837 network_id = ssid->id;
838 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
839 wpas_p2p_cross_connect_setup(wpa_s);
840 wpas_p2p_set_group_idle_timeout(wpa_s);
844 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
845 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
846 params->peer_interface_addr)) {
847 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
851 if (params->wps_method == WPS_PBC)
852 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
853 params->peer_device_addr);
854 else if (wpa_s->p2p_pin[0])
855 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
856 wpa_s->p2p_pin, NULL, 0, 0);
857 os_free(wpa_s->go_params);
858 wpa_s->go_params = NULL;
862 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
863 struct p2p_go_neg_results *params,
866 struct wpa_ssid *ssid;
868 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
869 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
870 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
875 ssid = wpa_config_add_network(wpa_s->conf);
877 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
881 wpa_s->show_group_started = 0;
883 wpa_config_set_network_defaults(ssid);
886 ssid->p2p_persistent_group = params->persistent_group;
887 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
889 ssid->frequency = params->freq;
890 ssid->ht40 = params->ht40;
891 ssid->ssid = os_zalloc(params->ssid_len + 1);
893 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
894 ssid->ssid_len = params->ssid_len;
896 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
897 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
898 ssid->proto = WPA_PROTO_RSN;
899 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
900 if (os_strlen(params->passphrase) > 0) {
901 ssid->passphrase = os_strdup(params->passphrase);
902 if (ssid->passphrase == NULL) {
903 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
904 "passphrase for GO");
905 wpa_config_remove_network(wpa_s->conf, ssid->id);
909 ssid->passphrase = NULL;
910 ssid->psk_set = params->psk_set;
912 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
913 else if (ssid->passphrase)
914 wpa_config_update_psk(ssid);
915 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
917 wpa_s->ap_configured_cb = p2p_go_configured;
918 wpa_s->ap_configured_cb_ctx = wpa_s;
919 wpa_s->ap_configured_cb_data = wpa_s->go_params;
920 wpa_s->connect_without_scan = ssid;
921 wpa_s->reassociate = 1;
922 wpa_s->disconnected = 0;
923 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
925 wpa_supplicant_req_scan(wpa_s, 0, 0);
929 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
930 const struct wpa_supplicant *src)
932 struct wpa_config *d;
933 const struct wpa_config *s;
938 #define C(n) if (s->n) d->n = os_strdup(s->n)
947 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
948 os_memcpy(d->sec_device_type, s->sec_device_type,
949 sizeof(d->sec_device_type));
950 d->num_sec_device_types = s->num_sec_device_types;
952 d->p2p_group_idle = s->p2p_group_idle;
953 d->p2p_intra_bss = s->p2p_intra_bss;
954 d->persistent_reconnect = s->persistent_reconnect;
955 d->max_num_sta = s->max_num_sta;
956 d->pbc_in_m1 = s->pbc_in_m1;
960 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
961 enum wpa_driver_if_type type)
963 char ifname[120], force_ifname[120];
965 if (wpa_s->pending_interface_name[0]) {
966 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
967 "- skip creation of a new one");
968 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
969 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
970 "unknown?! ifname='%s'",
971 wpa_s->pending_interface_name);
977 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
978 wpa_s->p2p_group_idx);
979 if (os_strlen(ifname) >= IFNAMSIZ &&
980 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
981 /* Try to avoid going over the IFNAMSIZ length limit */
982 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
983 wpa_s->p2p_group_idx);
985 force_ifname[0] = '\0';
987 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
989 wpa_s->p2p_group_idx++;
991 wpa_s->pending_interface_type = type;
992 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
993 wpa_s->pending_interface_addr, NULL) < 0) {
994 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
999 if (force_ifname[0]) {
1000 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1002 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1003 sizeof(wpa_s->pending_interface_name));
1005 os_strlcpy(wpa_s->pending_interface_name, ifname,
1006 sizeof(wpa_s->pending_interface_name));
1007 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1008 MACSTR, wpa_s->pending_interface_name,
1009 MAC2STR(wpa_s->pending_interface_addr));
1015 static void wpas_p2p_remove_pending_group_interface(
1016 struct wpa_supplicant *wpa_s)
1018 if (!wpa_s->pending_interface_name[0] ||
1019 is_zero_ether_addr(wpa_s->pending_interface_addr))
1020 return; /* No pending virtual interface */
1022 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1023 wpa_s->pending_interface_name);
1024 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1025 wpa_s->pending_interface_name);
1026 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1027 wpa_s->pending_interface_name[0] = '\0';
1031 static struct wpa_supplicant *
1032 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1034 struct wpa_interface iface;
1035 struct wpa_supplicant *group_wpa_s;
1037 if (!wpa_s->pending_interface_name[0]) {
1038 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1039 if (!wpas_p2p_create_iface(wpa_s))
1042 * Something has forced us to remove the pending interface; try
1043 * to create a new one and hope for the best that we will get
1044 * the same local address.
1046 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1047 WPA_IF_P2P_CLIENT) < 0)
1051 os_memset(&iface, 0, sizeof(iface));
1052 iface.ifname = wpa_s->pending_interface_name;
1053 iface.driver = wpa_s->driver->name;
1054 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1055 iface.driver_param = wpa_s->conf->driver_param;
1056 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1057 if (group_wpa_s == NULL) {
1058 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1059 "wpa_supplicant interface");
1062 wpa_s->pending_interface_name[0] = '\0';
1063 group_wpa_s->parent = wpa_s;
1064 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1065 P2P_GROUP_INTERFACE_CLIENT;
1066 wpa_s->global->p2p_group_formation = group_wpa_s;
1068 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1074 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1077 struct wpa_supplicant *wpa_s = eloop_ctx;
1078 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1079 if (wpa_s->global->p2p)
1080 p2p_group_formation_failed(wpa_s->global->p2p);
1081 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1082 wpa_drv_p2p_group_formation_failed(wpa_s);
1083 wpas_group_formation_completed(wpa_s, 0);
1087 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1089 struct wpa_supplicant *wpa_s = ctx;
1091 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1092 wpa_drv_cancel_remain_on_channel(wpa_s);
1093 wpa_s->off_channel_freq = 0;
1094 wpa_s->roc_waiting_drv_freq = 0;
1098 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1100 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1101 wpas_p2p_remove_pending_group_interface(wpa_s);
1105 if (wpa_s->p2p_go_ht40)
1108 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1109 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1111 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1112 struct wpa_ssid *ssid;
1113 ssid = wpa_config_get_network(wpa_s->conf,
1114 wpa_s->p2p_persistent_id);
1115 if (ssid && ssid->disabled == 2 &&
1116 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1117 size_t len = os_strlen(ssid->passphrase);
1118 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1119 "on requested persistent group");
1120 os_memcpy(res->passphrase, ssid->passphrase, len);
1121 res->passphrase[len] = '\0';
1125 if (wpa_s->create_p2p_iface) {
1126 struct wpa_supplicant *group_wpa_s =
1127 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1128 if (group_wpa_s == NULL) {
1129 wpas_p2p_remove_pending_group_interface(wpa_s);
1132 if (group_wpa_s != wpa_s) {
1133 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1134 sizeof(group_wpa_s->p2p_pin));
1135 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1137 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1138 wpa_s->pending_interface_name[0] = '\0';
1139 group_wpa_s->p2p_in_provisioning = 1;
1142 wpas_start_wps_go(group_wpa_s, res, 1);
1144 wpas_start_wps_enrollee(group_wpa_s, res);
1146 wpa_s->p2p_in_provisioning = 1;
1147 wpa_s->global->p2p_group_formation = wpa_s;
1150 wpas_start_wps_go(wpa_s, res, 1);
1152 wpas_start_wps_enrollee(ctx, res);
1155 wpa_s->p2p_long_listen = 0;
1156 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1158 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1159 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1160 (res->peer_config_timeout % 100) * 10000,
1161 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1165 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1167 struct wpa_supplicant *wpa_s = ctx;
1168 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1169 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1171 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1175 void wpas_dev_found(void *ctx, const u8 *addr,
1176 const struct p2p_peer_info *info,
1179 #ifndef CONFIG_NO_STDOUT_DEBUG
1180 struct wpa_supplicant *wpa_s = ctx;
1181 char devtype[WPS_DEV_TYPE_BUFSIZE];
1183 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1184 " p2p_dev_addr=" MACSTR
1185 " pri_dev_type=%s name='%s' config_methods=0x%x "
1186 "dev_capab=0x%x group_capab=0x%x",
1187 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1188 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1190 info->device_name, info->config_methods,
1191 info->dev_capab, info->group_capab);
1192 #endif /* CONFIG_NO_STDOUT_DEBUG */
1194 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1198 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1200 struct wpa_supplicant *wpa_s = ctx;
1202 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1203 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1205 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1209 static int wpas_start_listen(void *ctx, unsigned int freq,
1210 unsigned int duration,
1211 const struct wpabuf *probe_resp_ie)
1213 struct wpa_supplicant *wpa_s = ctx;
1215 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1217 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1218 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1219 "report received Probe Request frames");
1223 wpa_s->pending_listen_freq = freq;
1224 wpa_s->pending_listen_duration = duration;
1226 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1227 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1228 "to remain on channel (%u MHz) for Listen "
1230 wpa_s->pending_listen_freq = 0;
1233 wpa_s->off_channel_freq = 0;
1234 wpa_s->roc_waiting_drv_freq = freq;
1240 static void wpas_stop_listen(void *ctx)
1242 struct wpa_supplicant *wpa_s = ctx;
1243 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1244 wpa_drv_cancel_remain_on_channel(wpa_s);
1245 wpa_s->off_channel_freq = 0;
1246 wpa_s->roc_waiting_drv_freq = 0;
1248 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1249 wpa_drv_probe_req_report(wpa_s, 0);
1253 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1255 struct wpa_supplicant *wpa_s = ctx;
1256 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1261 * DNS Header section is used only to calculate compression pointers, so the
1262 * contents of this data does not matter, but the length needs to be reserved
1263 * in the virtual packet.
1265 #define DNS_HEADER_LEN 12
1268 * 27-octet in-memory packet from P2P specification containing two implied
1269 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1271 #define P2P_SD_IN_MEMORY_LEN 27
1273 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1274 u8 **spos, const u8 *end)
1276 while (*spos < end) {
1277 u8 val = ((*spos)[0] & 0xc0) >> 6;
1280 if (val == 1 || val == 2) {
1281 /* These are reserved values in RFC 1035 */
1282 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1283 "sequence starting with 0x%x", val);
1292 if (*spos + 2 > end) {
1293 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1294 "DNS offset field");
1298 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1299 if (offset >= *spos - start) {
1300 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1301 "pointer offset %u", offset);
1306 spos_tmp = start + offset;
1307 return p2p_sd_dns_uncompress_label(upos, uend, start,
1313 len = (*spos)[0] & 0x3f;
1318 if (*spos + len > end) {
1319 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1320 "sequence - no room for label with length "
1325 if (*upos + len + 2 > uend)
1328 os_memcpy(*upos, *spos, len);
1340 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1341 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1342 * not large enough */
1343 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1344 size_t msg_len, size_t offset)
1346 /* 27-octet in-memory packet from P2P specification */
1347 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1348 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1349 u8 *tmp, *end, *spos;
1355 if (offset > msg_len)
1358 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1361 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1362 end = spos + msg_len;
1365 os_memset(tmp, 0, DNS_HEADER_LEN);
1366 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1367 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1370 uend = buf + buf_len;
1372 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1381 } else if (upos[-1] == '.')
1389 static struct p2p_srv_bonjour *
1390 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1391 const struct wpabuf *query)
1393 struct p2p_srv_bonjour *bsrv;
1396 len = wpabuf_len(query);
1397 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1398 struct p2p_srv_bonjour, list) {
1399 if (len == wpabuf_len(bsrv->query) &&
1400 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1408 static struct p2p_srv_upnp *
1409 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1410 const char *service)
1412 struct p2p_srv_upnp *usrv;
1414 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1415 struct p2p_srv_upnp, list) {
1416 if (version == usrv->version &&
1417 os_strcmp(service, usrv->service) == 0)
1424 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1429 if (wpabuf_tailroom(resp) < 5)
1432 /* Length (to be filled) */
1433 len_pos = wpabuf_put(resp, 2);
1434 wpabuf_put_u8(resp, srv_proto);
1435 wpabuf_put_u8(resp, srv_trans_id);
1437 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1438 /* Response Data: empty */
1439 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1443 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1444 struct wpabuf *resp, u8 srv_trans_id)
1446 struct p2p_srv_bonjour *bsrv;
1449 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1451 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1452 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1456 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1457 struct p2p_srv_bonjour, list) {
1458 if (wpabuf_tailroom(resp) <
1459 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1461 /* Length (to be filled) */
1462 len_pos = wpabuf_put(resp, 2);
1463 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1464 wpabuf_put_u8(resp, srv_trans_id);
1466 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1467 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1468 wpabuf_head(bsrv->resp),
1469 wpabuf_len(bsrv->resp));
1471 wpabuf_put_buf(resp, bsrv->query); /* Key */
1472 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1473 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1479 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1482 char str_rx[256], str_srv[256];
1484 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1485 return 0; /* Too short to include DNS Type and Version */
1486 if (os_memcmp(query + query_len - 3,
1487 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1489 return 0; /* Mismatch in DNS Type or Version */
1490 if (query_len == wpabuf_len(bsrv->query) &&
1491 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1492 return 1; /* Binary match */
1494 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1496 return 0; /* Failed to uncompress query */
1497 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1498 wpabuf_head(bsrv->query),
1499 wpabuf_len(bsrv->query) - 3, 0))
1500 return 0; /* Failed to uncompress service */
1502 return os_strcmp(str_rx, str_srv) == 0;
1506 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1507 struct wpabuf *resp, u8 srv_trans_id,
1508 const u8 *query, size_t query_len)
1510 struct p2p_srv_bonjour *bsrv;
1514 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1516 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1517 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1518 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1523 if (query_len == 0) {
1524 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1528 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1529 struct p2p_srv_bonjour, list) {
1530 if (!match_bonjour_query(bsrv, query, query_len))
1533 if (wpabuf_tailroom(resp) <
1534 5 + query_len + wpabuf_len(bsrv->resp))
1539 /* Length (to be filled) */
1540 len_pos = wpabuf_put(resp, 2);
1541 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1542 wpabuf_put_u8(resp, srv_trans_id);
1545 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1546 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1547 wpabuf_head(bsrv->resp),
1548 wpabuf_len(bsrv->resp));
1551 wpabuf_put_data(resp, query, query_len); /* Key */
1552 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1554 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1558 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1560 if (wpabuf_tailroom(resp) < 5)
1563 /* Length (to be filled) */
1564 len_pos = wpabuf_put(resp, 2);
1565 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1566 wpabuf_put_u8(resp, srv_trans_id);
1569 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1570 /* Response Data: empty */
1571 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1577 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1578 struct wpabuf *resp, u8 srv_trans_id)
1580 struct p2p_srv_upnp *usrv;
1583 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1585 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1586 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1590 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1591 struct p2p_srv_upnp, list) {
1592 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1595 /* Length (to be filled) */
1596 len_pos = wpabuf_put(resp, 2);
1597 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1598 wpabuf_put_u8(resp, srv_trans_id);
1601 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1603 wpabuf_put_u8(resp, usrv->version);
1604 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1606 wpabuf_put_str(resp, usrv->service);
1607 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1613 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1614 struct wpabuf *resp, u8 srv_trans_id,
1615 const u8 *query, size_t query_len)
1617 struct p2p_srv_upnp *usrv;
1623 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1626 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1627 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1628 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1633 if (query_len == 0) {
1634 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1638 if (wpabuf_tailroom(resp) < 5)
1641 /* Length (to be filled) */
1642 len_pos = wpabuf_put(resp, 2);
1643 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1644 wpabuf_put_u8(resp, srv_trans_id);
1647 str = os_malloc(query_len);
1650 os_memcpy(str, query + 1, query_len - 1);
1651 str[query_len - 1] = '\0';
1653 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1654 struct p2p_srv_upnp, list) {
1655 if (version != usrv->version)
1658 if (os_strcmp(str, "ssdp:all") != 0 &&
1659 os_strstr(usrv->service, str) == NULL)
1662 if (wpabuf_tailroom(resp) < 2)
1666 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1668 wpabuf_put_u8(resp, version);
1670 wpabuf_put_u8(resp, ',');
1674 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1676 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1678 wpabuf_put_str(resp, usrv->service);
1683 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1686 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1687 /* Response Data: empty */
1690 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1694 #ifdef CONFIG_WIFI_DISPLAY
1695 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1696 struct wpabuf *resp, u8 srv_trans_id,
1697 const u8 *query, size_t query_len)
1703 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1705 if (!wpa_s->global->wifi_display) {
1706 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1707 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1712 if (query_len < 1) {
1713 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1718 if (wpabuf_tailroom(resp) < 5)
1723 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1725 /* TODO: role specific handling */
1727 /* Length (to be filled) */
1728 len_pos = wpabuf_put(resp, 2);
1729 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1730 wpabuf_put_u8(resp, srv_trans_id);
1731 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1733 while (pos < query + query_len) {
1734 if (*pos < MAX_WFD_SUBELEMS &&
1735 wpa_s->global->wfd_subelem[*pos] &&
1736 wpabuf_tailroom(resp) >=
1737 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1738 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1739 "subelement %u", *pos);
1740 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1745 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1747 #endif /* CONFIG_WIFI_DISPLAY */
1750 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1751 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1753 struct wpa_supplicant *wpa_s = ctx;
1754 const u8 *pos = tlvs;
1755 const u8 *end = tlvs + tlvs_len;
1758 struct wpabuf *resp;
1759 u8 srv_proto, srv_trans_id;
1763 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1765 buf_len = 2 * tlvs_len + 1;
1766 buf = os_malloc(buf_len);
1768 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1769 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1771 freq, MAC2STR(sa), dialog_token, update_indic,
1776 if (wpa_s->p2p_sd_over_ctrl_iface) {
1777 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1778 update_indic, tlvs, tlvs_len);
1779 return; /* to be processed by an external program */
1782 resp = wpabuf_alloc(10000);
1786 while (pos + 1 < end) {
1787 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1788 slen = WPA_GET_LE16(pos);
1790 if (pos + slen > end || slen < 2) {
1791 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1796 tlv_end = pos + slen;
1799 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1801 srv_trans_id = *pos++;
1802 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1805 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1806 pos, tlv_end - pos);
1809 if (wpa_s->force_long_sd) {
1810 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1812 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1813 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1817 switch (srv_proto) {
1818 case P2P_SERV_ALL_SERVICES:
1819 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1820 "for all services");
1821 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1822 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1823 wpa_printf(MSG_DEBUG, "P2P: No service "
1824 "discovery protocols available");
1825 wpas_sd_add_proto_not_avail(
1826 resp, P2P_SERV_ALL_SERVICES,
1830 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1831 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1833 case P2P_SERV_BONJOUR:
1834 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1835 pos, tlv_end - pos);
1838 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1839 pos, tlv_end - pos);
1841 #ifdef CONFIG_WIFI_DISPLAY
1842 case P2P_SERV_WIFI_DISPLAY:
1843 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1844 pos, tlv_end - pos);
1846 #endif /* CONFIG_WIFI_DISPLAY */
1848 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1849 "protocol %u", srv_proto);
1850 wpas_sd_add_proto_not_avail(resp, srv_proto,
1859 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1860 update_indic, tlvs, tlvs_len);
1862 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1868 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1869 const u8 *tlvs, size_t tlvs_len)
1871 struct wpa_supplicant *wpa_s = ctx;
1872 const u8 *pos = tlvs;
1873 const u8 *end = tlvs + tlvs_len;
1879 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1881 if (tlvs_len > 1500) {
1882 /* TODO: better way for handling this */
1883 wpa_msg_ctrl(wpa_s, MSG_INFO,
1884 P2P_EVENT_SERV_DISC_RESP MACSTR
1885 " %u <long response: %u bytes>",
1886 MAC2STR(sa), update_indic,
1887 (unsigned int) tlvs_len);
1889 buf_len = 2 * tlvs_len + 1;
1890 buf = os_malloc(buf_len);
1892 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1893 wpa_msg_ctrl(wpa_s, MSG_INFO,
1894 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1895 MAC2STR(sa), update_indic, buf);
1901 u8 srv_proto, srv_trans_id, status;
1903 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1904 slen = WPA_GET_LE16(pos);
1906 if (pos + slen > end || slen < 3) {
1907 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1911 tlv_end = pos + slen;
1914 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1916 srv_trans_id = *pos++;
1917 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1920 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1923 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1924 pos, tlv_end - pos);
1929 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1933 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1934 const struct wpabuf *tlvs)
1936 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1937 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1938 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1940 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1944 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1945 u8 version, const char *query)
1947 struct wpabuf *tlvs;
1950 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1953 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1954 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1955 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1956 wpabuf_put_u8(tlvs, version);
1957 wpabuf_put_str(tlvs, query);
1958 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1964 #ifdef CONFIG_WIFI_DISPLAY
1966 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1967 const struct wpabuf *tlvs)
1969 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1971 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1973 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1977 #define MAX_WFD_SD_SUBELEMS 20
1979 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1980 const char *subelems)
1987 len = wpabuf_put(tlvs, 2);
1988 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1989 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1991 wpabuf_put_u8(tlvs, role);
1996 if (val >= 0 && val < 256) {
1997 wpabuf_put_u8(tlvs, val);
1999 if (count == MAX_WFD_SD_SUBELEMS)
2002 pos = os_strchr(pos + 1, ',');
2008 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2012 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2013 const u8 *dst, const char *role)
2015 struct wpabuf *tlvs;
2017 const char *subelems;
2020 subelems = os_strchr(role, ' ');
2021 if (subelems == NULL)
2025 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2029 if (os_strstr(role, "[source]"))
2030 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2031 if (os_strstr(role, "[pri-sink]"))
2032 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2033 if (os_strstr(role, "[sec-sink]"))
2034 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2035 if (os_strstr(role, "[source+sink]"))
2036 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2038 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2043 #endif /* CONFIG_WIFI_DISPLAY */
2046 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2048 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2049 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
2050 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2052 return p2p_sd_cancel_request(wpa_s->global->p2p,
2053 (void *) (uintptr_t) req);
2057 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2058 const u8 *dst, u8 dialog_token,
2059 const struct wpabuf *resp_tlvs)
2061 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2062 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2066 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2068 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2073 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2075 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2076 wpa_drv_p2p_service_update(wpa_s);
2079 if (wpa_s->global->p2p)
2080 p2p_sd_service_update(wpa_s->global->p2p);
2084 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2086 dl_list_del(&bsrv->list);
2087 wpabuf_free(bsrv->query);
2088 wpabuf_free(bsrv->resp);
2093 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2095 dl_list_del(&usrv->list);
2096 os_free(usrv->service);
2101 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2103 struct p2p_srv_bonjour *bsrv, *bn;
2104 struct p2p_srv_upnp *usrv, *un;
2106 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2107 struct p2p_srv_bonjour, list)
2108 wpas_p2p_srv_bonjour_free(bsrv);
2110 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2111 struct p2p_srv_upnp, list)
2112 wpas_p2p_srv_upnp_free(usrv);
2114 wpas_p2p_sd_service_update(wpa_s);
2118 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2119 struct wpabuf *query, struct wpabuf *resp)
2121 struct p2p_srv_bonjour *bsrv;
2123 bsrv = os_zalloc(sizeof(*bsrv));
2126 bsrv->query = query;
2128 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2130 wpas_p2p_sd_service_update(wpa_s);
2135 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2136 const struct wpabuf *query)
2138 struct p2p_srv_bonjour *bsrv;
2140 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2143 wpas_p2p_srv_bonjour_free(bsrv);
2144 wpas_p2p_sd_service_update(wpa_s);
2149 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2150 const char *service)
2152 struct p2p_srv_upnp *usrv;
2154 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2155 return 0; /* Already listed */
2156 usrv = os_zalloc(sizeof(*usrv));
2159 usrv->version = version;
2160 usrv->service = os_strdup(service);
2161 if (usrv->service == NULL) {
2165 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2167 wpas_p2p_sd_service_update(wpa_s);
2172 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2173 const char *service)
2175 struct p2p_srv_upnp *usrv;
2177 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2180 wpas_p2p_srv_upnp_free(usrv);
2181 wpas_p2p_sd_service_update(wpa_s);
2186 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2187 const u8 *peer, const char *params,
2188 unsigned int generated_pin)
2190 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2191 MAC2STR(peer), generated_pin, params);
2195 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2196 const u8 *peer, const char *params)
2198 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2199 MAC2STR(peer), params);
2203 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2204 const u8 *dev_addr, const u8 *pri_dev_type,
2205 const char *dev_name, u16 supp_config_methods,
2206 u8 dev_capab, u8 group_capab, const u8 *group_id,
2207 size_t group_id_len)
2209 struct wpa_supplicant *wpa_s = ctx;
2210 char devtype[WPS_DEV_TYPE_BUFSIZE];
2212 u8 empty_dev_type[8];
2213 unsigned int generated_pin = 0;
2214 struct wpa_supplicant *group = NULL;
2217 for (group = wpa_s->global->ifaces; group; group = group->next)
2219 struct wpa_ssid *s = group->current_ssid;
2221 s->mode == WPAS_MODE_P2P_GO &&
2222 group_id_len - ETH_ALEN == s->ssid_len &&
2223 os_memcmp(group_id + ETH_ALEN, s->ssid,
2229 if (pri_dev_type == NULL) {
2230 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2231 pri_dev_type = empty_dev_type;
2233 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2234 " pri_dev_type=%s name='%s' config_methods=0x%x "
2235 "dev_capab=0x%x group_capab=0x%x%s%s",
2237 wps_dev_type_bin2str(pri_dev_type, devtype,
2239 dev_name, supp_config_methods, dev_capab, group_capab,
2240 group ? " group=" : "",
2241 group ? group->ifname : "");
2242 params[sizeof(params) - 1] = '\0';
2244 if (config_methods & WPS_CONFIG_DISPLAY) {
2245 generated_pin = wps_generate_pin();
2246 wpas_prov_disc_local_display(wpa_s, peer, params,
2248 } else if (config_methods & WPS_CONFIG_KEYPAD)
2249 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2250 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2251 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2252 "%s", MAC2STR(peer), params);
2254 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2255 P2P_PROV_DISC_SUCCESS,
2256 config_methods, generated_pin);
2260 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2262 struct wpa_supplicant *wpa_s = ctx;
2263 unsigned int generated_pin = 0;
2266 if (wpa_s->pending_pd_before_join &&
2267 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2268 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2269 wpa_s->pending_pd_before_join = 0;
2270 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2271 "join-existing-group operation");
2272 wpas_p2p_join_start(wpa_s);
2276 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2277 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2278 os_snprintf(params, sizeof(params), " peer_go=%d",
2279 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2283 if (config_methods & WPS_CONFIG_DISPLAY)
2284 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2285 else if (config_methods & WPS_CONFIG_KEYPAD) {
2286 generated_pin = wps_generate_pin();
2287 wpas_prov_disc_local_display(wpa_s, peer, params,
2289 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2290 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2291 "%s", MAC2STR(peer), params);
2293 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2294 P2P_PROV_DISC_SUCCESS,
2295 config_methods, generated_pin);
2299 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2300 enum p2p_prov_disc_status status)
2302 struct wpa_supplicant *wpa_s = ctx;
2304 if (wpa_s->p2p_fallback_to_go_neg) {
2305 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2306 "failed - fall back to GO Negotiation");
2307 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2311 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2312 wpa_s->pending_pd_before_join = 0;
2313 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2314 "join-existing-group operation (no ACK for PD "
2316 wpas_p2p_join_start(wpa_s);
2320 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2321 " p2p_dev_addr=" MACSTR " status=%d",
2322 MAC2STR(peer), status);
2324 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2329 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2330 const u8 *go_dev_addr, const u8 *ssid,
2331 size_t ssid_len, int *go, u8 *group_bssid,
2332 int *force_freq, int persistent_group)
2334 struct wpa_supplicant *wpa_s = ctx;
2336 u8 cur_bssid[ETH_ALEN];
2338 struct wpa_supplicant *grp;
2340 if (!persistent_group) {
2341 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2342 " to join an active group", MAC2STR(sa));
2343 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2344 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2346 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2347 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2348 "authorized invitation");
2352 * Do not accept the invitation automatically; notify user and
2355 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2358 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2360 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2361 "running persistent group");
2363 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2367 if (!wpa_s->conf->persistent_reconnect)
2368 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2370 for (s = wpa_s->conf->ssid; s; s = s->next) {
2371 if (s->disabled == 2 &&
2372 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2373 s->ssid_len == ssid_len &&
2374 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2379 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2380 " requested reinvocation of an unknown group",
2382 return P2P_SC_FAIL_UNKNOWN_GROUP;
2385 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2387 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2388 wpa_printf(MSG_DEBUG, "P2P: The only available "
2389 "interface is already in use - reject "
2391 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2393 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2394 } else if (s->mode == WPAS_MODE_P2P_GO) {
2396 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2398 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2399 "interface address for the group");
2400 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2402 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2407 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2408 wpa_s->assoc_freq) {
2409 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2410 "the channel we are already using");
2411 *force_freq = wpa_s->assoc_freq;
2414 res = wpa_drv_shared_freq(wpa_s);
2416 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2417 "with the channel we are already using on a "
2418 "shared interface");
2422 return P2P_SC_SUCCESS;
2426 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2427 const u8 *ssid, size_t ssid_len,
2428 const u8 *go_dev_addr, u8 status,
2431 struct wpa_supplicant *wpa_s = ctx;
2434 for (s = wpa_s->conf->ssid; s; s = s->next) {
2435 if (s->disabled == 2 &&
2436 s->ssid_len == ssid_len &&
2437 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2441 if (status == P2P_SC_SUCCESS) {
2442 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2443 " was accepted; op_freq=%d MHz",
2444 MAC2STR(sa), op_freq);
2446 int go = s->mode == WPAS_MODE_P2P_GO;
2447 wpas_p2p_group_add_persistent(
2448 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
2450 wpa_s->user_initiated_pd = 0;
2451 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2452 wpa_s->p2p_wps_method, 0);
2457 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2458 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2459 " was rejected (status %u)", MAC2STR(sa), status);
2465 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2466 "sa=" MACSTR " go_dev_addr=" MACSTR
2467 " bssid=" MACSTR " unknown-network",
2468 MAC2STR(sa), MAC2STR(go_dev_addr),
2471 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2472 "sa=" MACSTR " go_dev_addr=" MACSTR
2474 MAC2STR(sa), MAC2STR(go_dev_addr));
2479 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2480 " persistent=%d", MAC2STR(sa), s->id);
2484 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2485 const struct p2p_channels *channels)
2487 struct wpa_supplicant *wpa_s = ctx;
2488 struct wpa_ssid *ssid;
2491 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2492 "status=%d " MACSTR,
2493 status, MAC2STR(bssid));
2495 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2496 "status=%d ", status);
2498 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2500 if (wpa_s->pending_invite_ssid_id == -1)
2501 return; /* Invitation to active group */
2503 if (status != P2P_SC_SUCCESS) {
2504 wpas_p2p_remove_pending_group_interface(wpa_s);
2508 ssid = wpa_config_get_network(wpa_s->conf,
2509 wpa_s->pending_invite_ssid_id);
2511 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2512 "data matching with invitation");
2517 * The peer could have missed our ctrl::ack frame for Invitation
2518 * Response and continue retransmitting the frame. To reduce the
2519 * likelihood of the peer not getting successful TX status for the
2520 * Invitation Response frame, wait a short time here before starting
2521 * the persistent group so that we will remain on the current channel to
2522 * acknowledge any possible retransmission from the peer.
2524 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2525 "starting persistent group");
2528 wpas_p2p_group_add_persistent(wpa_s, ssid,
2529 ssid->mode == WPAS_MODE_P2P_GO,
2530 wpa_s->p2p_persistent_go_freq,
2531 wpa_s->p2p_go_ht40, channels);
2535 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2540 if (global->p2p_disallow_freq == NULL)
2543 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2544 if (freq >= global->p2p_disallow_freq[i].min &&
2545 freq <= global->p2p_disallow_freq[i].max)
2553 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2555 reg->channel[reg->channels] = chan;
2560 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2561 struct p2p_channels *chan)
2565 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2568 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2569 chan->reg_class[cla].reg_class = 81;
2570 chan->reg_class[cla].channels = 0;
2571 for (i = 0; i < 11; i++) {
2572 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2573 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2575 if (chan->reg_class[cla].channels)
2578 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2581 /* Operating class 115 - 5 GHz, channels 36-48 */
2582 chan->reg_class[cla].reg_class = 115;
2583 chan->reg_class[cla].channels = 0;
2584 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2585 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2586 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2587 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2588 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2589 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2590 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2591 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2592 if (chan->reg_class[cla].channels)
2595 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2598 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2599 chan->reg_class[cla].reg_class = 124;
2600 chan->reg_class[cla].channels = 0;
2601 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2602 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2603 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2604 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2605 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2606 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2607 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2608 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2609 if (chan->reg_class[cla].channels)
2612 chan->reg_classes = cla;
2617 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2619 enum hostapd_hw_mode mode)
2623 for (i = 0; i < num_modes; i++) {
2624 if (modes[i].mode == mode)
2632 static int has_channel(struct wpa_global *global,
2633 struct hostapd_hw_modes *mode, u8 chan, int *flags)
2638 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2640 if (wpas_p2p_disallowed_freq(global, freq))
2643 for (i = 0; i < mode->num_channels; i++) {
2644 if (mode->channels[i].chan == chan) {
2646 *flags = mode->channels[i].flag;
2647 return !(mode->channels[i].flag &
2648 (HOSTAPD_CHAN_DISABLED |
2649 HOSTAPD_CHAN_PASSIVE_SCAN |
2650 HOSTAPD_CHAN_NO_IBSS |
2651 HOSTAPD_CHAN_RADAR));
2659 struct p2p_oper_class_map {
2660 enum hostapd_hw_mode mode;
2665 enum { BW20, BW40PLUS, BW40MINUS } bw;
2668 static struct p2p_oper_class_map op_class[] = {
2669 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2670 #if 0 /* Do not enable HT40 on 2 GHz for now */
2671 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2672 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2674 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2675 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2676 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2677 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2678 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2679 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2680 { -1, 0, 0, 0, 0, BW20 }
2684 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2685 struct hostapd_hw_modes *mode,
2690 if (!has_channel(wpa_s->global, mode, channel, &flag))
2692 if (bw == BW40MINUS &&
2693 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2694 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2696 if (bw == BW40PLUS &&
2697 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2698 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2704 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2705 struct p2p_channels *chan)
2707 struct hostapd_hw_modes *mode;
2710 if (wpa_s->hw.modes == NULL) {
2711 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2712 "of all supported channels; assume dualband "
2714 return wpas_p2p_default_channels(wpa_s, chan);
2719 for (op = 0; op_class[op].op_class; op++) {
2720 struct p2p_oper_class_map *o = &op_class[op];
2722 struct p2p_reg_class *reg = NULL;
2724 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2727 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2728 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2731 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2732 "class %u", o->op_class);
2733 reg = &chan->reg_class[cla];
2735 reg->reg_class = o->op_class;
2737 reg->channel[reg->channels] = ch;
2741 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2742 reg->channel, reg->channels);
2746 chan->reg_classes = cla;
2752 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2753 struct hostapd_hw_modes *mode, u8 channel)
2757 for (op = 0; op_class[op].op_class; op++) {
2758 struct p2p_oper_class_map *o = &op_class[op];
2761 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2762 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2763 o->bw == BW20 || ch != channel)
2765 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2769 return (o->bw == BW40MINUS) ? -1 : 1;
2778 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2781 struct wpa_supplicant *wpa_s = ctx;
2783 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2784 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2790 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2794 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2796 struct wpa_supplicant *wpa_s = ctx;
2798 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2799 struct wpa_ssid *ssid = wpa_s->current_ssid;
2802 if (ssid->mode != WPAS_MODE_INFRA)
2804 if (wpa_s->wpa_state != WPA_COMPLETED &&
2805 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2807 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2816 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2817 * @global: Pointer to global data from wpa_supplicant_init()
2818 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2819 * Returns: 0 on success, -1 on failure
2821 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2823 struct p2p_config p2p;
2827 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2833 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2834 struct p2p_params params;
2836 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2837 os_memset(¶ms, 0, sizeof(params));
2838 params.dev_name = wpa_s->conf->device_name;
2839 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2841 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2842 os_memcpy(params.sec_dev_type,
2843 wpa_s->conf->sec_device_type,
2844 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2846 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2852 os_memset(&p2p, 0, sizeof(p2p));
2853 p2p.msg_ctx = wpa_s;
2855 p2p.p2p_scan = wpas_p2p_scan;
2856 p2p.send_action = wpas_send_action;
2857 p2p.send_action_done = wpas_send_action_done;
2858 p2p.go_neg_completed = wpas_go_neg_completed;
2859 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2860 p2p.dev_found = wpas_dev_found;
2861 p2p.dev_lost = wpas_dev_lost;
2862 p2p.start_listen = wpas_start_listen;
2863 p2p.stop_listen = wpas_stop_listen;
2864 p2p.send_probe_resp = wpas_send_probe_resp;
2865 p2p.sd_request = wpas_sd_request;
2866 p2p.sd_response = wpas_sd_response;
2867 p2p.prov_disc_req = wpas_prov_disc_req;
2868 p2p.prov_disc_resp = wpas_prov_disc_resp;
2869 p2p.prov_disc_fail = wpas_prov_disc_fail;
2870 p2p.invitation_process = wpas_invitation_process;
2871 p2p.invitation_received = wpas_invitation_received;
2872 p2p.invitation_result = wpas_invitation_result;
2873 p2p.get_noa = wpas_get_noa;
2874 p2p.go_connected = wpas_go_connected;
2876 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2877 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2878 p2p.dev_name = wpa_s->conf->device_name;
2879 p2p.manufacturer = wpa_s->conf->manufacturer;
2880 p2p.model_name = wpa_s->conf->model_name;
2881 p2p.model_number = wpa_s->conf->model_number;
2882 p2p.serial_number = wpa_s->conf->serial_number;
2884 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2885 p2p.config_methods = wpa_s->wps->config_methods;
2888 if (wpa_s->conf->p2p_listen_reg_class &&
2889 wpa_s->conf->p2p_listen_channel) {
2890 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2891 p2p.channel = wpa_s->conf->p2p_listen_channel;
2895 * Pick one of the social channels randomly as the listen
2898 os_get_random((u8 *) &r, sizeof(r));
2899 p2p.channel = 1 + (r % 3) * 5;
2901 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2903 if (wpa_s->conf->p2p_oper_reg_class &&
2904 wpa_s->conf->p2p_oper_channel) {
2905 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2906 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2907 p2p.cfg_op_channel = 1;
2908 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2909 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2912 p2p.op_reg_class = 81;
2914 * Use random operation channel from (1, 6, 11) if no other
2915 * preference is indicated.
2917 os_get_random((u8 *) &r, sizeof(r));
2918 p2p.op_channel = 1 + (r % 3) * 5;
2919 p2p.cfg_op_channel = 0;
2920 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2921 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2923 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2924 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2925 p2p.country[2] = 0x04;
2927 os_memcpy(p2p.country, "XX\x04", 3);
2929 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2930 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2935 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2938 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2939 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2940 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2942 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2943 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2945 p2p.max_peers = 100;
2947 if (wpa_s->conf->p2p_ssid_postfix) {
2948 p2p.ssid_postfix_len =
2949 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2950 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2951 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2952 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2953 p2p.ssid_postfix_len);
2956 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2958 p2p.max_listen = wpa_s->max_remain_on_chan;
2960 global->p2p = p2p_init(&p2p);
2961 if (global->p2p == NULL)
2963 global->p2p_init_wpa_s = wpa_s;
2965 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2966 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2968 p2p_add_wps_vendor_extension(
2969 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2977 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2978 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2980 * This function deinitialize per-interface P2P data.
2982 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2984 if (wpa_s->driver && wpa_s->drv_priv)
2985 wpa_drv_probe_req_report(wpa_s, 0);
2987 if (wpa_s->go_params) {
2988 /* Clear any stored provisioning info */
2989 p2p_clear_provisioning_info(
2991 wpa_s->go_params->peer_device_addr);
2994 os_free(wpa_s->go_params);
2995 wpa_s->go_params = NULL;
2996 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2997 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2998 wpa_s->p2p_long_listen = 0;
2999 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3000 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3001 wpas_p2p_remove_pending_group_interface(wpa_s);
3003 /* TODO: remove group interface from the driver if this wpa_s instance
3004 * is on top of a P2P group interface */
3009 * wpas_p2p_deinit_global - Deinitialize global P2P module
3010 * @global: Pointer to global data from wpa_supplicant_init()
3012 * This function deinitializes the global (per device) P2P module.
3014 void wpas_p2p_deinit_global(struct wpa_global *global)
3016 struct wpa_supplicant *wpa_s, *tmp;
3018 wpa_s = global->ifaces;
3020 wpas_p2p_service_flush(wpa_s);
3022 if (global->p2p == NULL)
3025 /* Remove remaining P2P group interfaces */
3026 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3027 wpa_s = wpa_s->next;
3029 tmp = global->ifaces;
3032 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3037 /* Disconnect from the P2P group and deinit the interface */
3038 wpas_p2p_disconnect(tmp);
3042 * Deinit GO data on any possibly remaining interface (if main
3043 * interface is used as GO).
3045 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3046 if (wpa_s->ap_iface)
3047 wpas_p2p_group_deinit(wpa_s);
3050 p2p_deinit(global->p2p);
3052 global->p2p_init_wpa_s = NULL;
3056 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3058 if (wpa_s->conf->p2p_no_group_iface)
3059 return 0; /* separate interface disabled per configuration */
3060 if (wpa_s->drv_flags &
3061 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3062 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3063 return 1; /* P2P group requires a new interface in every case
3065 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3066 return 0; /* driver does not support concurrent operations */
3067 if (wpa_s->global->ifaces->next)
3068 return 1; /* more that one interface already in use */
3069 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3070 return 1; /* this interface is already in use */
3075 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3076 const u8 *peer_addr,
3077 enum p2p_wps_method wps_method,
3078 int go_intent, const u8 *own_interface_addr,
3079 unsigned int force_freq, int persistent_group,
3080 struct wpa_ssid *ssid, unsigned int pref_freq)
3082 if (persistent_group && wpa_s->conf->persistent_reconnect)
3083 persistent_group = 2;
3085 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3086 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3087 go_intent, own_interface_addr,
3088 force_freq, persistent_group);
3092 * Increase GO config timeout if HT40 is used since it takes some time
3093 * to scan channels for coex purposes before the BSS can be started.
3095 p2p_set_config_timeout(wpa_s->global->p2p,
3096 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3098 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3099 go_intent, own_interface_addr, force_freq,
3100 persistent_group, ssid ? ssid->ssid : NULL,
3101 ssid ? ssid->ssid_len : 0,
3102 wpa_s->p2p_pd_before_go_neg, pref_freq);
3106 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3107 const u8 *peer_addr,
3108 enum p2p_wps_method wps_method,
3109 int go_intent, const u8 *own_interface_addr,
3110 unsigned int force_freq, int persistent_group,
3111 struct wpa_ssid *ssid, unsigned int pref_freq)
3113 if (persistent_group && wpa_s->conf->persistent_reconnect)
3114 persistent_group = 2;
3116 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3119 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3120 go_intent, own_interface_addr, force_freq,
3121 persistent_group, ssid ? ssid->ssid : NULL,
3122 ssid ? ssid->ssid_len : 0, pref_freq);
3126 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3128 wpa_s->p2p_join_scan_count++;
3129 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3130 wpa_s->p2p_join_scan_count);
3131 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3132 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3133 " for join operationg - stop join attempt",
3134 MAC2STR(wpa_s->pending_join_iface_addr));
3135 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3136 if (wpa_s->p2p_auto_pd) {
3137 wpa_s->p2p_auto_pd = 0;
3138 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3139 " p2p_dev_addr=" MACSTR " status=N/A",
3140 MAC2STR(wpa_s->pending_join_dev_addr));
3143 wpa_msg(wpa_s->parent, MSG_INFO,
3144 P2P_EVENT_GROUP_FORMATION_FAILURE);
3149 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3151 struct wpa_supplicant *iface;
3155 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3158 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3159 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3161 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3163 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3164 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3165 shared_freq = iface->current_ssid->frequency;
3166 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3167 shared_freq = iface->assoc_freq;
3171 if (shared_freq && freq != shared_freq) {
3172 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3173 "connected on %d MHz - new connection on "
3174 "%d MHz", iface->ifname, shared_freq, freq);
3179 shared_freq = wpa_drv_shared_freq(wpa_s);
3180 if (shared_freq > 0 && shared_freq != freq) {
3181 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3182 "virtual interface connected on %d MHz - new "
3183 "connection on %d MHz", shared_freq, freq);
3191 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3192 const u8 *peer_dev_addr)
3194 struct wpa_bss *bss;
3197 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3200 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3201 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3206 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3207 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3208 "%ld.%06ld (%supdated in last scan)",
3209 bss->last_update.sec, bss->last_update.usec,
3210 updated ? "": "not ");
3216 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3217 struct wpa_scan_results *scan_res)
3219 struct wpa_bss *bss;
3221 u8 iface_addr[ETH_ALEN];
3223 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3225 if (wpa_s->global->p2p_disabled)
3228 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3229 scan_res ? (int) scan_res->num : -1,
3230 wpa_s->p2p_auto_join ? "auto_" : "");
3233 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3235 if (wpa_s->p2p_auto_pd) {
3236 int join = wpas_p2p_peer_go(wpa_s,
3237 wpa_s->pending_join_dev_addr);
3239 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3240 wpa_s->auto_pd_scan_retry++;
3241 bss = wpa_bss_get_bssid(wpa_s,
3242 wpa_s->pending_join_dev_addr);
3245 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3246 "the peer " MACSTR " at %d MHz",
3247 wpa_s->auto_pd_scan_retry,
3249 pending_join_dev_addr),
3251 wpas_p2p_join_scan_req(wpa_s, freq);
3259 wpa_s->p2p_auto_pd = 0;
3260 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3261 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3262 MAC2STR(wpa_s->pending_join_dev_addr), join);
3263 if (p2p_prov_disc_req(wpa_s->global->p2p,
3264 wpa_s->pending_join_dev_addr,
3265 wpa_s->pending_pd_config_methods, join,
3266 0, wpa_s->user_initiated_pd) < 0) {
3267 wpa_s->p2p_auto_pd = 0;
3268 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3269 " p2p_dev_addr=" MACSTR " status=N/A",
3270 MAC2STR(wpa_s->pending_join_dev_addr));
3275 if (wpa_s->p2p_auto_join) {
3276 int join = wpas_p2p_peer_go(wpa_s,
3277 wpa_s->pending_join_dev_addr);
3279 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3280 "running a GO -> use GO Negotiation");
3281 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3282 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3283 wpa_s->p2p_persistent_group, 0, 0, 0,
3284 wpa_s->p2p_go_intent,
3285 wpa_s->p2p_connect_freq,
3286 wpa_s->p2p_persistent_id,
3287 wpa_s->p2p_pd_before_go_neg,
3288 wpa_s->p2p_go_ht40);
3292 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3293 "try to join the group", join ? "" :
3296 wpa_s->p2p_fallback_to_go_neg = 1;
3299 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3300 wpa_s->pending_join_iface_addr);
3302 p2p_get_interface_addr(wpa_s->global->p2p,
3303 wpa_s->pending_join_dev_addr,
3305 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3307 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3308 "address for join from " MACSTR " to " MACSTR
3309 " based on newly discovered P2P peer entry",
3310 MAC2STR(wpa_s->pending_join_iface_addr),
3311 MAC2STR(iface_addr));
3312 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3315 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3316 wpa_s->pending_join_iface_addr);
3319 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3320 "from P2P peer table: %d MHz", freq);
3322 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3325 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3326 "from BSS table: %d MHz", freq);
3331 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3332 wpa_msg(wpa_s->parent, MSG_INFO,
3333 P2P_EVENT_GROUP_FORMATION_FAILURE
3334 "reason=FREQ_CONFLICT");
3338 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3339 "prior to joining an existing group (GO " MACSTR
3341 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3342 wpa_s->pending_pd_before_join = 1;
3344 switch (wpa_s->pending_join_wps_method) {
3345 case WPS_PIN_DISPLAY:
3346 method = WPS_CONFIG_KEYPAD;
3348 case WPS_PIN_KEYPAD:
3349 method = WPS_CONFIG_DISPLAY;
3352 method = WPS_CONFIG_PUSHBUTTON;
3359 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3360 wpa_s->pending_join_dev_addr) ==
3363 * We have already performed provision discovery for
3364 * joining the group. Proceed directly to join
3365 * operation without duplicated provision discovery. */
3366 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3367 "with " MACSTR " already done - proceed to "
3369 MAC2STR(wpa_s->pending_join_dev_addr));
3370 wpa_s->pending_pd_before_join = 0;
3374 if (p2p_prov_disc_req(wpa_s->global->p2p,
3375 wpa_s->pending_join_dev_addr, method, 1,
3376 freq, wpa_s->user_initiated_pd) < 0) {
3377 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3378 "Discovery Request before joining an "
3380 wpa_s->pending_pd_before_join = 0;
3386 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3387 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3388 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3389 wpas_p2p_check_join_scan_limit(wpa_s);
3393 /* Start join operation immediately */
3394 wpas_p2p_join_start(wpa_s);
3398 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3401 struct wpa_driver_scan_params params;
3402 struct wpabuf *wps_ie, *ies;
3404 int freqs[2] = { 0, 0 };
3406 os_memset(¶ms, 0, sizeof(params));
3408 /* P2P Wildcard SSID */
3409 params.num_ssids = 1;
3410 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3411 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3413 wpa_s->wps->dev.p2p = 1;
3414 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3415 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3417 if (wps_ie == NULL) {
3418 wpas_p2p_scan_res_join(wpa_s, NULL);
3422 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3423 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3425 wpabuf_free(wps_ie);
3426 wpas_p2p_scan_res_join(wpa_s, NULL);
3429 wpabuf_put_buf(ies, wps_ie);
3430 wpabuf_free(wps_ie);
3432 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3434 params.p2p_probe = 1;
3435 params.extra_ies = wpabuf_head(ies);
3436 params.extra_ies_len = wpabuf_len(ies);
3439 params.freqs = freqs;
3443 * Run a scan to update BSS table and start Provision Discovery once
3444 * the new scan results become available.
3446 ret = wpa_drv_scan(wpa_s, ¶ms);
3448 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3453 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3455 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3456 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3457 wpas_p2p_check_join_scan_limit(wpa_s);
3462 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3464 struct wpa_supplicant *wpa_s = eloop_ctx;
3465 wpas_p2p_join_scan_req(wpa_s, 0);
3469 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3470 const u8 *dev_addr, enum p2p_wps_method wps_method,
3473 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3474 MACSTR " dev " MACSTR ")%s",
3475 MAC2STR(iface_addr), MAC2STR(dev_addr),
3476 auto_join ? " (auto_join)" : "");
3478 wpa_s->p2p_auto_pd = 0;
3479 wpa_s->p2p_auto_join = !!auto_join;
3480 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3481 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3482 wpa_s->pending_join_wps_method = wps_method;
3484 /* Make sure we are not running find during connection establishment */
3485 wpas_p2p_stop_find(wpa_s);
3487 wpa_s->p2p_join_scan_count = 0;
3488 wpas_p2p_join_scan(wpa_s, NULL);
3493 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3495 struct wpa_supplicant *group;
3496 struct p2p_go_neg_results res;
3497 struct wpa_bss *bss;
3499 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3502 if (group != wpa_s) {
3503 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3504 sizeof(group->p2p_pin));
3505 group->p2p_wps_method = wpa_s->p2p_wps_method;
3508 * Need to mark the current interface for p2p_group_formation
3509 * when a separate group interface is not used. This is needed
3510 * to allow p2p_cancel stop a pending p2p_connect-join.
3511 * wpas_p2p_init_group_interface() addresses this for the case
3512 * where a separate group interface is used.
3514 wpa_s->global->p2p_group_formation = wpa_s;
3517 group->p2p_in_provisioning = 1;
3518 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3520 os_memset(&res, 0, sizeof(res));
3521 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3523 res.wps_method = wpa_s->pending_join_wps_method;
3524 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3526 res.freq = bss->freq;
3527 res.ssid_len = bss->ssid_len;
3528 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3531 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3532 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3534 wpa_drv_cancel_remain_on_channel(wpa_s);
3535 wpa_s->off_channel_freq = 0;
3536 wpa_s->roc_waiting_drv_freq = 0;
3538 wpas_start_wps_enrollee(group, &res);
3541 * Allow a longer timeout for join-a-running-group than normal 15
3542 * second group formation timeout since the GO may not have authorized
3543 * our connection yet.
3545 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3546 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3553 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3554 int *force_freq, int *pref_freq,
3558 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3559 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3560 "(%u MHz) is not supported for P2P uses",
3565 if (*oper_freq > 0 && freq != *oper_freq &&
3566 !(wpa_s->drv_flags &
3567 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3568 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3569 "on %u MHz while connected on another "
3570 "channel (%u MHz)", freq, *oper_freq);
3573 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3574 "requested channel (%u MHz)", freq);
3576 } else if (*oper_freq > 0 &&
3577 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3578 if (!(wpa_s->drv_flags &
3579 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3580 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3581 "while connected on non-P2P supported "
3582 "channel (%u MHz)", *oper_freq);
3585 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3586 "(%u MHz) not available for P2P - try to use "
3587 "another channel", *oper_freq);
3589 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3591 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3592 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3593 "are already using (%u MHz) on another interface",
3595 *pref_freq = *oper_freq;
3596 } else if (*oper_freq > 0) {
3597 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3598 "channel we are already using (%u MHz) on another "
3599 "interface", *oper_freq);
3600 *force_freq = *oper_freq;
3608 * wpas_p2p_connect - Request P2P Group Formation to be started
3609 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3610 * @peer_addr: Address of the peer P2P Device
3611 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3612 * @persistent_group: Whether to create a persistent group
3613 * @auto_join: Whether to select join vs. GO Negotiation automatically
3614 * @join: Whether to join an existing group (as a client) instead of starting
3615 * Group Owner negotiation; @peer_addr is BSSID in that case
3616 * @auth: Whether to only authorize the connection instead of doing that and
3617 * initiating Group Owner negotiation
3618 * @go_intent: GO Intent or -1 to use default
3619 * @freq: Frequency for the group or 0 for auto-selection
3620 * @persistent_id: Persistent group credentials to use for forcing GO
3621 * parameters or -1 to generate new values (SSID/passphrase)
3622 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3623 * interoperability workaround when initiating group formation
3624 * @ht40: Start GO with 40 MHz channel width
3625 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3626 * failure, -2 on failure due to channel not currently available,
3627 * -3 if forced channel is not supported
3629 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3630 const char *pin, enum p2p_wps_method wps_method,
3631 int persistent_group, int auto_join, int join, int auth,
3632 int go_intent, int freq, int persistent_id, int pd,
3635 int force_freq = 0, pref_freq = 0, oper_freq = 0;
3638 enum wpa_driver_if_type iftype;
3640 struct wpa_ssid *ssid = NULL;
3642 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3645 if (persistent_id >= 0) {
3646 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3647 if (ssid == NULL || ssid->disabled != 2 ||
3648 ssid->mode != WPAS_MODE_P2P_GO)
3653 go_intent = wpa_s->conf->p2p_go_intent;
3656 wpa_s->p2p_long_listen = 0;
3658 wpa_s->p2p_wps_method = wps_method;
3659 wpa_s->p2p_persistent_group = !!persistent_group;
3660 wpa_s->p2p_persistent_id = persistent_id;
3661 wpa_s->p2p_go_intent = go_intent;
3662 wpa_s->p2p_connect_freq = freq;
3663 wpa_s->p2p_fallback_to_go_neg = 0;
3664 wpa_s->p2p_pd_before_go_neg = !!pd;
3665 wpa_s->p2p_go_ht40 = !!ht40;
3668 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3669 else if (wps_method == WPS_PIN_DISPLAY) {
3670 ret = wps_generate_pin();
3671 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3673 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3676 wpa_s->p2p_pin[0] = '\0';
3678 if (join || auto_join) {
3679 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3681 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3682 "connect a running group from " MACSTR,
3683 MAC2STR(peer_addr));
3684 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3687 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3688 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3690 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3691 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3695 os_get_time(&wpa_s->p2p_auto_started);
3696 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3698 wpa_s->p2p_auto_started.sec,
3699 wpa_s->p2p_auto_started.usec);
3701 wpa_s->user_initiated_pd = 1;
3702 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3708 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3709 wpa_s->assoc_freq) {
3710 oper_freq = wpa_s->assoc_freq;
3712 oper_freq = wpa_drv_shared_freq(wpa_s);
3717 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
3722 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3724 if (wpa_s->create_p2p_iface) {
3725 /* Prepare to add a new interface for the group */
3726 iftype = WPA_IF_P2P_GROUP;
3727 if (go_intent == 15)
3728 iftype = WPA_IF_P2P_GO;
3729 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3730 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3731 "interface for the group");
3735 if_addr = wpa_s->pending_interface_addr;
3737 if_addr = wpa_s->own_addr;
3740 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3742 force_freq, persistent_group, ssid,
3748 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3749 go_intent, if_addr, force_freq,
3750 persistent_group, ssid, pref_freq) < 0) {
3751 if (wpa_s->create_p2p_iface)
3752 wpas_p2p_remove_pending_group_interface(wpa_s);
3760 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3761 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3762 * @freq: Frequency of the channel in MHz
3763 * @duration: Duration of the stay on the channel in milliseconds
3765 * This callback is called when the driver indicates that it has started the
3766 * requested remain-on-channel duration.
3768 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3769 unsigned int freq, unsigned int duration)
3771 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3773 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3774 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3775 wpa_s->pending_listen_duration);
3776 wpa_s->pending_listen_freq = 0;
3781 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3782 unsigned int timeout)
3784 /* Limit maximum Listen state time based on driver limitation. */
3785 if (timeout > wpa_s->max_remain_on_chan)
3786 timeout = wpa_s->max_remain_on_chan;
3788 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3789 return wpa_drv_p2p_listen(wpa_s, timeout);
3791 return p2p_listen(wpa_s->global->p2p, timeout);
3796 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3797 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3798 * @freq: Frequency of the channel in MHz
3800 * This callback is called when the driver indicates that a remain-on-channel
3801 * operation has been completed, i.e., the duration on the requested channel
3804 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3807 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3808 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3809 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
3810 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3812 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3813 return; /* P2P module started a new operation */
3814 if (offchannel_pending_action_tx(wpa_s))
3816 if (wpa_s->p2p_long_listen > 0)
3817 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3818 if (wpa_s->p2p_long_listen > 0) {
3819 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3820 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3826 * wpas_p2p_group_remove - Remove a P2P group
3827 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3828 * @ifname: Network interface name of the group interface or "*" to remove all
3830 * Returns: 0 on success, -1 on failure
3832 * This function is used to remove a P2P group. This can be used to disconnect
3833 * from a group in which the local end is a P2P Client or to end a P2P Group in
3834 * case the local end is the Group Owner. If a virtual network interface was
3835 * created for this group, that interface will be removed. Otherwise, only the
3836 * configured P2P group network will be removed from the interface.
3838 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3840 struct wpa_global *global = wpa_s->global;
3842 if (os_strcmp(ifname, "*") == 0) {
3843 struct wpa_supplicant *prev;
3844 wpa_s = global->ifaces;
3847 wpa_s = wpa_s->next;
3848 wpas_p2p_disconnect(prev);
3853 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3854 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3858 return wpas_p2p_disconnect(wpa_s);
3862 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3864 if (channels == NULL)
3865 return 1; /* Assume no restrictions */
3866 return p2p_channels_includes_freq(channels, freq);
3870 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3871 struct p2p_go_neg_results *params,
3873 const struct p2p_channels *channels)
3878 os_memset(params, 0, sizeof(*params));
3879 params->role_go = 1;
3880 params->ht40 = ht40;
3882 if (!freq_included(channels, freq)) {
3883 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3887 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3888 "frequency %d MHz", freq);
3889 params->freq = freq;
3890 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3891 wpa_s->conf->p2p_oper_channel >= 1 &&
3892 wpa_s->conf->p2p_oper_channel <= 11 &&
3893 freq_included(channels,
3894 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
3895 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3896 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3897 "frequency %d MHz", params->freq);
3898 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
3899 wpa_s->conf->p2p_oper_reg_class == 116 ||
3900 wpa_s->conf->p2p_oper_reg_class == 117 ||
3901 wpa_s->conf->p2p_oper_reg_class == 124 ||
3902 wpa_s->conf->p2p_oper_reg_class == 126 ||
3903 wpa_s->conf->p2p_oper_reg_class == 127) &&
3904 freq_included(channels,
3905 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
3906 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3907 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3908 "frequency %d MHz", params->freq);
3909 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3910 wpa_s->best_overall_freq > 0 &&
3911 p2p_supported_freq(wpa_s->global->p2p,
3912 wpa_s->best_overall_freq) &&
3913 freq_included(channels, wpa_s->best_overall_freq)) {
3914 params->freq = wpa_s->best_overall_freq;
3915 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3916 "channel %d MHz", params->freq);
3917 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3918 wpa_s->best_24_freq > 0 &&
3919 p2p_supported_freq(wpa_s->global->p2p,
3920 wpa_s->best_24_freq) &&
3921 freq_included(channels, wpa_s->best_24_freq)) {
3922 params->freq = wpa_s->best_24_freq;
3923 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3924 "channel %d MHz", params->freq);
3925 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3926 wpa_s->best_5_freq > 0 &&
3927 p2p_supported_freq(wpa_s->global->p2p,
3928 wpa_s->best_5_freq) &&
3929 freq_included(channels, wpa_s->best_5_freq)) {
3930 params->freq = wpa_s->best_5_freq;
3931 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3932 "channel %d MHz", params->freq);
3935 for (chan = 0; chan < 11; chan++) {
3936 params->freq = 2412 + chan * 5;
3937 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3939 freq_included(channels, params->freq))
3943 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3947 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3948 "known)", params->freq);
3951 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3952 wpa_s->assoc_freq && !freq) {
3953 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3955 params->freq = wpa_s->assoc_freq;
3956 if (!freq_included(channels, params->freq)) {
3957 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3958 "accepted", params->freq);
3963 res = wpa_drv_shared_freq(wpa_s);
3964 if (res > 0 && !freq) {
3965 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3966 "already using on a shared interface");
3968 if (!freq_included(channels, params->freq)) {
3969 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3970 "accepted", params->freq);
3973 } else if (res > 0 && freq != res &&
3974 !(wpa_s->drv_flags &
3975 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3976 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3977 "while connected on another channel (%u MHz)",
3986 static struct wpa_supplicant *
3987 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3990 struct wpa_supplicant *group_wpa_s;
3992 if (!wpas_p2p_create_iface(wpa_s)) {
3993 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
3998 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3999 WPA_IF_P2P_CLIENT) < 0) {
4000 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
4003 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4004 if (group_wpa_s == NULL) {
4005 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4007 wpas_p2p_remove_pending_group_interface(wpa_s);
4011 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4012 group_wpa_s->ifname);
4018 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4019 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4020 * @persistent_group: Whether to create a persistent group
4021 * @freq: Frequency for the group or 0 to indicate no hardcoding
4022 * Returns: 0 on success, -1 on failure
4024 * This function creates a new P2P group with the local end as the Group Owner,
4025 * i.e., without using Group Owner Negotiation.
4027 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
4030 struct p2p_go_neg_results params;
4033 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4036 /* Make sure we are not running find during connection establishment */
4037 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
4038 wpas_p2p_stop_find_oper(wpa_s);
4041 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4043 if (wpa_s->best_24_freq > 0 &&
4044 p2p_supported_freq(wpa_s->global->p2p,
4045 wpa_s->best_24_freq)) {
4046 freq = wpa_s->best_24_freq;
4047 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4048 "channel: %d MHz", freq);
4050 os_get_random((u8 *) &r, sizeof(r));
4051 freq = 2412 + (r % 3) * 25;
4052 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4053 "channel: %d MHz", freq);
4058 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4060 if (wpa_s->best_5_freq > 0 &&
4061 p2p_supported_freq(wpa_s->global->p2p,
4062 wpa_s->best_5_freq)) {
4063 freq = wpa_s->best_5_freq;
4064 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4065 "channel: %d MHz", freq);
4067 os_get_random((u8 *) &r, sizeof(r));
4068 freq = 5180 + (r % 4) * 20;
4069 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4070 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4071 "5 GHz channel for P2P group");
4074 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4075 "channel: %d MHz", freq);
4079 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4080 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4081 "(%u MHz) is not supported for P2P uses",
4086 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, NULL))
4089 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4090 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4091 "(%u MHz) is not supported for P2P uses",
4095 p2p_go_params(wpa_s->global->p2p, ¶ms);
4096 params.persistent_group = persistent_group;
4098 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4101 wpas_start_wps_go(wpa_s, ¶ms, 0);
4107 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4108 struct wpa_ssid *params, int addr_allocated)
4110 struct wpa_ssid *ssid;
4112 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4116 wpa_supplicant_ap_deinit(wpa_s);
4118 ssid = wpa_config_add_network(wpa_s->conf);
4121 wpa_config_set_network_defaults(ssid);
4122 ssid->temporary = 1;
4123 ssid->proto = WPA_PROTO_RSN;
4124 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4125 ssid->group_cipher = WPA_CIPHER_CCMP;
4126 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4127 ssid->ssid = os_malloc(params->ssid_len);
4128 if (ssid->ssid == NULL) {
4129 wpa_config_remove_network(wpa_s->conf, ssid->id);
4132 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4133 ssid->ssid_len = params->ssid_len;
4134 ssid->p2p_group = 1;
4135 ssid->export_keys = 1;
4136 if (params->psk_set) {
4137 os_memcpy(ssid->psk, params->psk, 32);
4140 if (params->passphrase)
4141 ssid->passphrase = os_strdup(params->passphrase);
4143 wpa_supplicant_select_network(wpa_s, ssid);
4145 wpa_s->show_group_started = 1;
4151 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4152 struct wpa_ssid *ssid, int addr_allocated,
4154 const struct p2p_channels *channels)
4156 struct p2p_go_neg_results params;
4159 if (ssid->disabled != 2 || ssid->ssid == NULL)
4162 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4163 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4164 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4169 /* Make sure we are not running find during connection establishment */
4170 wpas_p2p_stop_find_oper(wpa_s);
4172 wpa_s->p2p_fallback_to_go_neg = 0;
4174 if (ssid->mode == WPAS_MODE_INFRA)
4175 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4177 if (ssid->mode != WPAS_MODE_P2P_GO)
4180 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, channels))
4184 params.psk_set = ssid->psk_set;
4186 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
4187 if (ssid->passphrase) {
4188 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4189 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4190 "persistent group");
4193 os_strlcpy(params.passphrase, ssid->passphrase,
4194 sizeof(params.passphrase));
4196 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4197 params.ssid_len = ssid->ssid_len;
4198 params.persistent_group = 1;
4200 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4204 wpas_start_wps_go(wpa_s, ¶ms, 0);
4210 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4211 struct wpabuf *proberesp_ies)
4213 struct wpa_supplicant *wpa_s = ctx;
4214 if (wpa_s->ap_iface) {
4215 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4216 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4217 wpabuf_free(beacon_ies);
4218 wpabuf_free(proberesp_ies);
4222 wpabuf_free(hapd->p2p_beacon_ie);
4223 hapd->p2p_beacon_ie = beacon_ies;
4225 wpabuf_free(hapd->p2p_probe_resp_ie);
4226 hapd->p2p_probe_resp_ie = proberesp_ies;
4228 wpabuf_free(beacon_ies);
4229 wpabuf_free(proberesp_ies);
4231 wpa_supplicant_ap_update_beacon(wpa_s);
4235 static void wpas_p2p_idle_update(void *ctx, int idle)
4237 struct wpa_supplicant *wpa_s = ctx;
4238 if (!wpa_s->ap_iface)
4240 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4242 wpas_p2p_set_group_idle_timeout(wpa_s);
4244 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4248 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4249 struct wpa_ssid *ssid)
4251 struct p2p_group *group;
4252 struct p2p_group_config *cfg;
4254 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4259 cfg = os_zalloc(sizeof(*cfg));
4263 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4264 cfg->persistent_group = 2;
4265 else if (ssid->p2p_persistent_group)
4266 cfg->persistent_group = 1;
4267 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4268 if (wpa_s->max_stations &&
4269 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4270 cfg->max_clients = wpa_s->max_stations;
4272 cfg->max_clients = wpa_s->conf->max_num_sta;
4273 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4274 cfg->ssid_len = ssid->ssid_len;
4275 cfg->cb_ctx = wpa_s;
4276 cfg->ie_update = wpas_p2p_ie_update;
4277 cfg->idle_update = wpas_p2p_idle_update;
4279 group = p2p_group_init(wpa_s->global->p2p, cfg);
4282 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4283 p2p_group_notif_formation_done(group);
4284 wpa_s->p2p_group = group;
4289 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4292 struct wpa_ssid *ssid = wpa_s->current_ssid;
4294 if (!wpa_s->p2p_in_provisioning) {
4295 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4296 "provisioning not in progress");
4300 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4301 u8 go_dev_addr[ETH_ALEN];
4302 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4303 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4305 /* Clear any stored provisioning info */
4306 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4309 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4311 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4313 * Use a separate timeout for initial data connection to
4314 * complete to allow the group to be removed automatically if
4315 * something goes wrong in this step before the P2P group idle
4316 * timeout mechanism is taken into use.
4318 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4319 wpas_p2p_group_formation_timeout,
4320 wpa_s->parent, NULL);
4322 if (wpa_s->global->p2p)
4323 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4324 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4325 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4326 wpas_group_formation_completed(wpa_s, 1);
4330 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4331 struct wps_event_fail *fail)
4333 if (!wpa_s->p2p_in_provisioning) {
4334 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4335 "provisioning not in progress");
4339 if (wpa_s->go_params) {
4340 p2p_clear_provisioning_info(
4342 wpa_s->go_params->peer_device_addr);
4345 wpas_notify_p2p_wps_failed(wpa_s, fail);
4349 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4350 const char *config_method,
4351 enum wpas_p2p_prov_disc_use use)
4355 wpa_s->p2p_fallback_to_go_neg = 0;
4356 wpa_s->pending_pd_use = NORMAL_PD;
4357 if (os_strncmp(config_method, "display", 7) == 0)
4358 config_methods = WPS_CONFIG_DISPLAY;
4359 else if (os_strncmp(config_method, "keypad", 6) == 0)
4360 config_methods = WPS_CONFIG_KEYPAD;
4361 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4362 os_strncmp(config_method, "pushbutton", 10) == 0)
4363 config_methods = WPS_CONFIG_PUSHBUTTON;
4365 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
4369 if (use == WPAS_P2P_PD_AUTO) {
4370 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4371 wpa_s->pending_pd_config_methods = config_methods;
4372 wpa_s->p2p_auto_pd = 1;
4373 wpa_s->p2p_auto_join = 0;
4374 wpa_s->pending_pd_before_join = 0;
4375 wpa_s->auto_pd_scan_retry = 0;
4376 wpas_p2p_stop_find(wpa_s);
4377 wpa_s->p2p_join_scan_count = 0;
4378 os_get_time(&wpa_s->p2p_auto_started);
4379 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4380 wpa_s->p2p_auto_started.sec,
4381 wpa_s->p2p_auto_started.usec);
4382 wpas_p2p_join_scan(wpa_s, NULL);
4386 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4387 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
4389 use == WPAS_P2P_PD_FOR_JOIN);
4392 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4395 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
4396 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4401 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4404 return p2p_scan_result_text(ies, ies_len, buf, end);
4408 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4410 if (!offchannel_pending_action_tx(wpa_s))
4413 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4414 "operation request");
4415 offchannel_clear_pending_action_tx(wpa_s);
4419 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4420 enum p2p_discovery_type type,
4421 unsigned int num_req_dev_types, const u8 *req_dev_types,
4422 const u8 *dev_id, unsigned int search_delay)
4424 wpas_p2p_clear_pending_action_tx(wpa_s);
4425 wpa_s->p2p_long_listen = 0;
4427 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4428 return wpa_drv_p2p_find(wpa_s, timeout, type);
4430 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4431 wpa_s->p2p_in_provisioning)
4434 wpa_supplicant_cancel_sched_scan(wpa_s);
4436 return p2p_find(wpa_s->global->p2p, timeout, type,
4437 num_req_dev_types, req_dev_types, dev_id,
4442 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
4444 wpas_p2p_clear_pending_action_tx(wpa_s);
4445 wpa_s->p2p_long_listen = 0;
4446 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4447 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4448 wpa_s->global->p2p_cb_on_scan_complete = 0;
4450 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4451 wpa_drv_p2p_stop_find(wpa_s);
4455 if (wpa_s->global->p2p)
4456 p2p_stop_find(wpa_s->global->p2p);
4462 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4464 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4466 wpas_p2p_remove_pending_group_interface(wpa_s);
4470 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4472 struct wpa_supplicant *wpa_s = eloop_ctx;
4473 wpa_s->p2p_long_listen = 0;
4477 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4481 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4484 wpa_supplicant_cancel_sched_scan(wpa_s);
4485 wpas_p2p_clear_pending_action_tx(wpa_s);
4489 * This is a request for unlimited Listen state. However, at
4490 * least for now, this is mapped to a Listen state for one
4495 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4496 wpa_s->p2p_long_listen = 0;
4499 * Stop previous find/listen operation to avoid trying to request a new
4500 * remain-on-channel operation while the driver is still running the
4503 if (wpa_s->global->p2p)
4504 p2p_stop_find(wpa_s->global->p2p);
4506 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4507 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4508 wpa_s->p2p_long_listen = timeout * 1000;
4509 eloop_register_timeout(timeout, 0,
4510 wpas_p2p_long_listen_timeout,
4518 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4519 u8 *buf, size_t len, int p2p_group)
4521 struct wpabuf *p2p_ie;
4524 if (wpa_s->global->p2p_disabled)
4526 if (wpa_s->global->p2p == NULL)
4531 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4532 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4534 wpabuf_free(p2p_ie);
4540 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4541 const u8 *dst, const u8 *bssid,
4542 const u8 *ie, size_t ie_len, int ssi_signal)
4544 if (wpa_s->global->p2p_disabled)
4546 if (wpa_s->global->p2p == NULL)
4549 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4551 case P2P_PREQ_NOT_P2P:
4552 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4555 case P2P_PREQ_MALFORMED:
4556 case P2P_PREQ_NOT_LISTEN:
4557 case P2P_PREQ_NOT_PROCESSED:
4558 default: /* make gcc happy */
4560 case P2P_PREQ_PROCESSED:
4566 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4567 const u8 *sa, const u8 *bssid,
4568 u8 category, const u8 *data, size_t len, int freq)
4570 if (wpa_s->global->p2p_disabled)
4572 if (wpa_s->global->p2p == NULL)
4575 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4580 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4582 if (wpa_s->global->p2p_disabled)
4584 if (wpa_s->global->p2p == NULL)
4587 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4591 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4593 p2p_group_deinit(wpa_s->p2p_group);
4594 wpa_s->p2p_group = NULL;
4596 wpa_s->ap_configured_cb = NULL;
4597 wpa_s->ap_configured_cb_ctx = NULL;
4598 wpa_s->ap_configured_cb_data = NULL;
4599 wpa_s->connect_without_scan = NULL;
4603 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4605 wpa_s->p2p_long_listen = 0;
4607 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4608 return wpa_drv_p2p_reject(wpa_s, addr);
4610 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4613 return p2p_reject(wpa_s->global->p2p, addr);
4617 /* Invite to reinvoke a persistent group */
4618 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4619 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4620 int ht40, int pref_freq)
4622 enum p2p_invite_role role;
4623 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
4624 int force_freq = 0, oper_freq = 0;
4627 wpa_s->p2p_persistent_go_freq = freq;
4628 wpa_s->p2p_go_ht40 = !!ht40;
4629 if (ssid->mode == WPAS_MODE_P2P_GO) {
4630 role = P2P_INVITE_ROLE_GO;
4631 if (peer_addr == NULL) {
4632 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4633 "address in invitation command");
4636 if (wpas_p2p_create_iface(wpa_s)) {
4637 if (wpas_p2p_add_group_interface(wpa_s,
4638 WPA_IF_P2P_GO) < 0) {
4639 wpa_printf(MSG_ERROR, "P2P: Failed to "
4640 "allocate a new interface for the "
4644 bssid = wpa_s->pending_interface_addr;
4646 bssid = wpa_s->own_addr;
4648 role = P2P_INVITE_ROLE_CLIENT;
4649 peer_addr = ssid->bssid;
4651 wpa_s->pending_invite_ssid_id = ssid->id;
4653 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4654 wpa_s->assoc_freq) {
4655 oper_freq = wpa_s->assoc_freq;
4659 oper_freq = wpa_drv_shared_freq(wpa_s);
4664 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4669 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4670 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4671 ssid->ssid, ssid->ssid_len,
4674 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4677 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4678 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4683 /* Invite to join an active group */
4684 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4685 const u8 *peer_addr, const u8 *go_dev_addr)
4687 struct wpa_global *global = wpa_s->global;
4688 enum p2p_invite_role role;
4689 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
4690 struct wpa_ssid *ssid;
4692 int force_freq = 0, oper_freq = 0, pref_freq = 0;
4695 wpa_s->p2p_persistent_go_freq = 0;
4696 wpa_s->p2p_go_ht40 = 0;
4698 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4699 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4702 if (wpa_s == NULL) {
4703 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4707 ssid = wpa_s->current_ssid;
4709 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4714 persistent = ssid->p2p_persistent_group &&
4715 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4716 ssid->ssid, ssid->ssid_len);
4718 if (ssid->mode == WPAS_MODE_P2P_GO) {
4719 role = P2P_INVITE_ROLE_ACTIVE_GO;
4720 bssid = wpa_s->own_addr;
4721 if (go_dev_addr == NULL)
4722 go_dev_addr = wpa_s->global->p2p_dev_addr;
4724 role = P2P_INVITE_ROLE_CLIENT;
4725 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4726 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4727 "invite to current group");
4730 bssid = wpa_s->bssid;
4731 if (go_dev_addr == NULL &&
4732 !is_zero_ether_addr(wpa_s->go_dev_addr))
4733 go_dev_addr = wpa_s->go_dev_addr;
4735 wpa_s->parent->pending_invite_ssid_id = -1;
4737 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4738 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4739 ssid->ssid, ssid->ssid_len,
4740 go_dev_addr, persistent);
4742 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4745 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4746 wpa_s->assoc_freq) {
4747 oper_freq = wpa_s->assoc_freq;
4751 oper_freq = wpa_drv_shared_freq(wpa_s);
4756 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
4761 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4762 ssid->ssid, ssid->ssid_len, force_freq,
4763 go_dev_addr, persistent, pref_freq);
4767 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4769 struct wpa_ssid *ssid = wpa_s->current_ssid;
4770 const char *ssid_txt;
4771 u8 go_dev_addr[ETH_ALEN];
4772 int network_id = -1;
4776 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4777 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4778 wpa_s->parent, NULL);
4781 if (!wpa_s->show_group_started || !ssid)
4784 wpa_s->show_group_started = 0;
4786 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4787 os_memset(go_dev_addr, 0, ETH_ALEN);
4788 if (ssid->bssid_set)
4789 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4790 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4792 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4794 if (wpa_s->global->p2p_group_formation == wpa_s)
4795 wpa_s->global->p2p_group_formation = NULL;
4797 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4798 (int) wpa_s->assoc_freq;
4799 if (ssid->passphrase == NULL && ssid->psk_set) {
4801 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4802 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4803 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4805 wpa_s->ifname, ssid_txt, freq, psk,
4806 MAC2STR(go_dev_addr),
4807 persistent ? " [PERSISTENT]" : "");
4809 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4810 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4811 "go_dev_addr=" MACSTR "%s",
4812 wpa_s->ifname, ssid_txt, freq,
4813 ssid->passphrase ? ssid->passphrase : "",
4814 MAC2STR(go_dev_addr),
4815 persistent ? " [PERSISTENT]" : "");
4819 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4822 network_id = ssid->id;
4823 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4826 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4827 wpa_s->global->p2p != NULL) {
4828 wpa_s->global->p2p_cb_on_scan_complete = 0;
4829 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4830 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4831 "continued after successful connection");
4832 p2p_increase_search_delay(
4834 wpas_p2p_search_delay(wpa_s));
4840 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4841 u32 interval1, u32 duration2, u32 interval2)
4843 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4845 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4848 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4849 wpa_s->current_ssid == NULL ||
4850 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4853 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4854 wpa_s->own_addr, wpa_s->assoc_freq,
4855 duration1, interval1, duration2, interval2);
4859 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4860 unsigned int interval)
4862 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4865 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4868 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4872 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4874 if (wpa_s->current_ssid == NULL) {
4876 * current_ssid can be cleared when P2P client interface gets
4877 * disconnected, so assume this interface was used as P2P
4882 return wpa_s->current_ssid->p2p_group &&
4883 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4887 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4889 struct wpa_supplicant *wpa_s = eloop_ctx;
4891 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4892 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4897 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4899 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
4903 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4907 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4908 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4910 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4913 timeout = wpa_s->conf->p2p_group_idle;
4914 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4915 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4916 timeout = P2P_MAX_CLIENT_IDLE;
4922 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4923 timeout = 0; /* special client mode no-timeout */
4928 if (wpa_s->p2p_in_provisioning) {
4930 * Use the normal group formation timeout during the
4931 * provisioning phase to avoid terminating this process too
4932 * early due to group idle timeout.
4934 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4935 "during provisioning");
4939 if (wpa_s->show_group_started) {
4941 * Use the normal group formation timeout between the end of
4942 * the provisioning phase and completion of 4-way handshake to
4943 * avoid terminating this process too early due to group idle
4946 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4947 "while waiting for initial 4-way handshake to "
4952 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4954 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4959 /* Returns 1 if the interface was removed */
4960 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4961 u16 reason_code, const u8 *ie, size_t ie_len,
4962 int locally_generated)
4964 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4966 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4969 if (!locally_generated)
4970 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4973 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4974 wpa_s->current_ssid &&
4975 wpa_s->current_ssid->p2p_group &&
4976 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4977 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4978 "session is ending");
4979 if (wpas_p2p_group_delete(wpa_s,
4980 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4989 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4990 u16 reason_code, const u8 *ie, size_t ie_len,
4991 int locally_generated)
4993 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4995 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4998 if (!locally_generated)
4999 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5004 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5006 struct p2p_data *p2p = wpa_s->global->p2p;
5011 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5014 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5015 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5017 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5018 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5021 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5022 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5024 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5025 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5027 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5028 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5029 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5030 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5031 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5034 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5035 p2p_set_sec_dev_types(p2p,
5036 (void *) wpa_s->conf->sec_device_type,
5037 wpa_s->conf->num_sec_device_types);
5039 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5041 p2p_remove_wps_vendor_extensions(p2p);
5042 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5043 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5045 p2p_add_wps_vendor_extension(
5046 p2p, wpa_s->conf->wps_vendor_ext[i]);
5050 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5051 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5053 country[0] = wpa_s->conf->country[0];
5054 country[1] = wpa_s->conf->country[1];
5056 p2p_set_country(p2p, country);
5059 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5060 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5061 wpa_s->conf->p2p_ssid_postfix ?
5062 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5066 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5067 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
5069 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5070 u8 reg_class, channel;
5073 if (wpa_s->conf->p2p_listen_reg_class &&
5074 wpa_s->conf->p2p_listen_channel) {
5075 reg_class = wpa_s->conf->p2p_listen_reg_class;
5076 channel = wpa_s->conf->p2p_listen_channel;
5080 * Pick one of the social channels randomly as the
5083 os_get_random((u8 *) &r, sizeof(r));
5084 channel = 1 + (r % 3) * 5;
5086 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5088 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5091 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5092 u8 op_reg_class, op_channel, cfg_op_channel;
5095 if (wpa_s->conf->p2p_oper_reg_class &&
5096 wpa_s->conf->p2p_oper_channel) {
5097 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5098 op_channel = wpa_s->conf->p2p_oper_channel;
5103 * Use random operation channel from (1, 6, 11)
5104 *if no other preference is indicated.
5106 os_get_random((u8 *) &r, sizeof(r));
5107 op_channel = 1 + (r % 3) * 5;
5110 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5113 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5117 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5118 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5119 wpa_s->conf->p2p_pref_chan) < 0) {
5120 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5127 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5130 if (!wpa_s->ap_iface)
5132 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5137 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5139 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5141 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5144 wpa_s->global->cross_connection = enabled;
5145 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5148 struct wpa_supplicant *iface;
5150 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5152 if (iface->cross_connect_enabled == 0)
5155 iface->cross_connect_enabled = 0;
5156 iface->cross_connect_in_use = 0;
5157 wpa_msg(iface->parent, MSG_INFO,
5158 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5159 iface->ifname, iface->cross_connect_uplink);
5167 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5169 struct wpa_supplicant *iface;
5171 if (!uplink->global->cross_connection)
5174 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5175 if (!iface->cross_connect_enabled)
5177 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5180 if (iface->ap_iface == NULL)
5182 if (iface->cross_connect_in_use)
5185 iface->cross_connect_in_use = 1;
5186 wpa_msg(iface->parent, MSG_INFO,
5187 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5188 iface->ifname, iface->cross_connect_uplink);
5193 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5195 struct wpa_supplicant *iface;
5197 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5198 if (!iface->cross_connect_enabled)
5200 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5203 if (!iface->cross_connect_in_use)
5206 wpa_msg(iface->parent, MSG_INFO,
5207 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5208 iface->ifname, iface->cross_connect_uplink);
5209 iface->cross_connect_in_use = 0;
5214 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5216 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5217 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5218 wpa_s->cross_connect_disallowed)
5219 wpas_p2p_disable_cross_connect(wpa_s);
5221 wpas_p2p_enable_cross_connect(wpa_s);
5222 if (!wpa_s->ap_iface &&
5223 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5224 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5228 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5230 wpas_p2p_disable_cross_connect(wpa_s);
5231 if (!wpa_s->ap_iface &&
5232 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5234 wpas_p2p_set_group_idle_timeout(wpa_s);
5238 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5240 struct wpa_supplicant *iface;
5242 if (!wpa_s->global->cross_connection)
5245 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5248 if (iface->drv_flags &
5249 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5251 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5254 wpa_s->cross_connect_enabled = 1;
5255 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5256 sizeof(wpa_s->cross_connect_uplink));
5257 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5258 "%s to %s whenever uplink is available",
5259 wpa_s->ifname, wpa_s->cross_connect_uplink);
5261 if (iface->ap_iface || iface->current_ssid == NULL ||
5262 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5263 iface->cross_connect_disallowed ||
5264 iface->wpa_state != WPA_COMPLETED)
5267 wpa_s->cross_connect_in_use = 1;
5268 wpa_msg(wpa_s->parent, MSG_INFO,
5269 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5270 wpa_s->ifname, wpa_s->cross_connect_uplink);
5276 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5278 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5279 !wpa_s->p2p_in_provisioning)
5280 return 0; /* not P2P client operation */
5282 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5284 if (wpa_s != wpa_s->parent)
5285 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5287 if (wpa_s->global->p2p)
5288 p2p_group_formation_failed(wpa_s->global->p2p);
5290 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5291 wpa_s->parent, NULL);
5293 wpas_group_formation_completed(wpa_s, 0);
5298 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5300 struct p2p_channels chan;
5302 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5305 os_memset(&chan, 0, sizeof(chan));
5306 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5307 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5312 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5316 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5317 struct wpa_scan_results *scan_res)
5319 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5323 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5325 struct wpa_global *global = wpa_s->global;
5329 if (global->p2p == NULL)
5332 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5334 if (wpa_s->pending_interface_name[0] &&
5335 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5338 peer = p2p_get_go_neg_peer(global->p2p);
5340 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5341 MACSTR, MAC2STR(peer));
5342 p2p_unauthorize(global->p2p, peer);
5346 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5347 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5348 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5352 if (wpa_s->pending_pd_before_join) {
5353 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5354 wpa_s->pending_pd_before_join = 0;
5358 wpas_p2p_stop_find(wpa_s);
5360 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5361 if (wpa_s == global->p2p_group_formation &&
5362 (wpa_s->p2p_in_provisioning ||
5363 wpa_s->parent->pending_interface_type ==
5364 WPA_IF_P2P_CLIENT)) {
5365 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5366 "formation found - cancelling",
5369 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5370 wpa_s->parent, NULL);
5371 if (wpa_s->p2p_in_provisioning) {
5372 wpas_group_formation_completed(wpa_s, 0);
5375 wpas_p2p_group_delete(wpa_s,
5376 P2P_GROUP_REMOVAL_REQUESTED);
5382 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5390 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5392 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5395 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5396 "being available anymore");
5397 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
5401 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5402 int freq_24, int freq_5, int freq_overall)
5404 struct p2p_data *p2p = wpa_s->global->p2p;
5405 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5407 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5411 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5414 struct p2p_data *p2p = wpa_s->global->p2p;
5416 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5419 if (hwaddr_aton(addr, peer))
5422 return p2p_unauthorize(p2p, peer);
5427 * wpas_p2p_disconnect - Disconnect from a P2P Group
5428 * @wpa_s: Pointer to wpa_supplicant data
5429 * Returns: 0 on success, -1 on failure
5431 * This can be used to disconnect from a group in which the local end is a P2P
5432 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5433 * virtual network interface was created for this group, that interface will be
5434 * removed. Otherwise, only the configured P2P group network will be removed
5435 * from the interface.
5437 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5443 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5448 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5452 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5455 ret = p2p_in_progress(wpa_s->global->p2p);
5458 * Check whether there is an ongoing WPS provisioning step (or
5459 * other parts of group formation) on another interface since
5460 * p2p_in_progress() does not report this to avoid issues for
5461 * scans during such provisioning step.
5463 if (wpa_s->global->p2p_group_formation &&
5464 wpa_s->global->p2p_group_formation != wpa_s) {
5465 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5466 "in group formation",
5467 wpa_s->global->p2p_group_formation->ifname);
5476 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5477 struct wpa_ssid *ssid)
5479 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5480 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5481 wpa_s->parent, NULL) > 0) {
5483 * Remove the network by scheduling the group formation
5484 * timeout to happen immediately. The teardown code
5485 * needs to be scheduled to run asynch later so that we
5486 * don't delete data from under ourselves unexpectedly.
5487 * Calling wpas_p2p_group_formation_timeout directly
5488 * causes a series of crashes in WPS failure scenarios.
5490 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5491 "P2P group network getting removed");
5492 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5493 wpa_s->parent, NULL);
5498 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
5499 const u8 *addr, const u8 *ssid,
5505 for (s = wpa_s->conf->ssid; s; s = s->next) {
5506 if (s->disabled != 2)
5509 (ssid_len != s->ssid_len ||
5510 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5512 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5513 return s; /* peer is GO in the persistent group */
5514 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5516 for (i = 0; i < s->num_p2p_clients; i++) {
5517 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5518 addr, ETH_ALEN) == 0)
5519 return s; /* peer is P2P client in persistent
5528 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5533 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5537 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5540 struct wpa_supplicant *group = wpa_s;
5541 if (wpa_s->global->p2p_group_formation)
5542 group = wpa_s->global->p2p_group_formation;
5543 wpa_s = wpa_s->parent;
5544 offchannel_send_action_done(wpa_s);
5546 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
5547 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5548 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5549 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5550 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5551 wpa_s->p2p_persistent_id,
5552 wpa_s->p2p_pd_before_go_neg,
5553 wpa_s->p2p_go_ht40);
5557 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5559 if (!wpa_s->p2p_fallback_to_go_neg ||
5560 wpa_s->p2p_in_provisioning <= 5)
5563 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5564 return 0; /* peer operating as a GO */
5566 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5567 "fallback to GO Negotiation");
5568 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5574 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5576 const char *rn, *rn2;
5577 struct wpa_supplicant *ifs;
5579 if (wpa_s->wpa_state > WPA_SCANNING) {
5580 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5581 "concurrent operation",
5582 P2P_CONCURRENT_SEARCH_DELAY);
5583 return P2P_CONCURRENT_SEARCH_DELAY;
5586 if (!wpa_s->driver->get_radio_name)
5588 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5589 if (rn == NULL || rn[0] == '\0')
5592 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5593 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5596 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5597 if (!rn2 || os_strcmp(rn, rn2) != 0)
5599 if (ifs->wpa_state > WPA_SCANNING) {
5600 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5601 "delay due to concurrent operation on "
5603 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5604 return P2P_CONCURRENT_SEARCH_DELAY;