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_set_own_freq_preference(struct wpa_supplicant *wpa_s,
99 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
102 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
103 wpa_s->parent->conf->p2p_ignore_shared_freq)
105 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
109 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
110 struct wpa_scan_results *scan_res)
114 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
117 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
118 (int) scan_res->num);
120 for (i = 0; i < scan_res->num; i++) {
121 struct wpa_scan_res *bss = scan_res->res[i];
122 struct os_time time_tmp_age, entry_ts;
123 time_tmp_age.sec = bss->age / 1000;
124 time_tmp_age.usec = (bss->age % 1000) * 1000;
125 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
126 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
127 bss->freq, &entry_ts, bss->level,
128 (const u8 *) (bss + 1),
133 p2p_scan_res_handled(wpa_s->global->p2p);
137 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
138 unsigned int num_req_dev_types,
139 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
141 struct wpa_supplicant *wpa_s = ctx;
142 struct wpa_supplicant *ifs;
143 struct wpa_driver_scan_params params;
145 struct wpabuf *wps_ie, *ies;
146 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
149 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
152 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
153 if (ifs->sta_scan_pending &&
154 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
155 wpas_p2p_in_progress(wpa_s) == 2) {
156 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
157 "pending station mode scan to be "
158 "completed on interface %s", ifs->ifname);
159 wpa_s->global->p2p_cb_on_scan_complete = 1;
160 wpa_supplicant_req_scan(ifs, 0, 0);
165 os_memset(¶ms, 0, sizeof(params));
167 /* P2P Wildcard SSID */
168 params.num_ssids = 1;
169 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
170 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
172 wpa_s->wps->dev.p2p = 1;
173 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
174 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
175 num_req_dev_types, req_dev_types);
179 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
180 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
185 wpabuf_put_buf(ies, wps_ie);
188 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
190 params.p2p_probe = 1;
191 params.extra_ies = wpabuf_head(ies);
192 params.extra_ies_len = wpabuf_len(ies);
195 case P2P_SCAN_SOCIAL:
196 params.freqs = social_channels;
200 case P2P_SCAN_SOCIAL_PLUS_ONE:
201 social_channels[3] = freq;
202 params.freqs = social_channels;
206 ret = wpa_drv_scan(wpa_s, ¶ms);
211 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
213 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
214 wpa_s->global->p2p_cb_on_scan_complete = 1;
220 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
226 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
228 switch (p2p_group_interface) {
229 case P2P_GROUP_INTERFACE_PENDING:
230 return WPA_IF_P2P_GROUP;
231 case P2P_GROUP_INTERFACE_GO:
232 return WPA_IF_P2P_GO;
233 case P2P_GROUP_INTERFACE_CLIENT:
234 return WPA_IF_P2P_CLIENT;
237 return WPA_IF_P2P_GROUP;
241 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
243 size_t ssid_len, int *go)
247 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
248 for (s = wpa_s->conf->ssid; s; s = s->next) {
249 if (s->disabled != 0 || !s->p2p_group ||
250 s->ssid_len != ssid_len ||
251 os_memcmp(ssid, s->ssid, ssid_len) != 0)
253 if (s->mode == WPAS_MODE_P2P_GO &&
254 s != wpa_s->current_ssid)
257 *go = s->mode == WPAS_MODE_P2P_GO;
266 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
267 enum p2p_group_removal_reason removal_reason)
269 struct wpa_ssid *ssid;
273 ssid = wpa_s->current_ssid;
276 * The current SSID was not known, but there may still be a
277 * pending P2P group interface waiting for provisioning or a
278 * P2P group that is trying to reconnect.
280 ssid = wpa_s->conf->ssid;
282 if (ssid->p2p_group && ssid->disabled != 2)
287 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
289 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
294 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
296 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
297 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
298 wpa_s->reassociate = 0;
299 wpa_s->disconnected = 1;
300 wpa_supplicant_deauthenticate(wpa_s,
301 WLAN_REASON_DEAUTH_LEAVING);
305 if (wpa_s->cross_connect_in_use) {
306 wpa_s->cross_connect_in_use = 0;
307 wpa_msg(wpa_s->parent, MSG_INFO,
308 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
309 wpa_s->ifname, wpa_s->cross_connect_uplink);
311 switch (removal_reason) {
312 case P2P_GROUP_REMOVAL_REQUESTED:
313 reason = " reason=REQUESTED";
315 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
316 reason = " reason=FORMATION_FAILED";
318 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
319 reason = " reason=IDLE";
321 case P2P_GROUP_REMOVAL_UNAVAILABLE:
322 reason = " reason=UNAVAILABLE";
324 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
325 reason = " reason=GO_ENDING_SESSION";
331 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
332 wpa_msg(wpa_s->parent, MSG_INFO,
333 P2P_EVENT_GROUP_REMOVED "%s %s%s",
334 wpa_s->ifname, gtype, reason);
337 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
338 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
339 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
340 wpa_s->parent, NULL) > 0) {
341 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
343 wpa_s->p2p_in_provisioning = 0;
346 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
347 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
349 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
350 struct wpa_global *global;
352 enum wpa_driver_if_type type;
353 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
355 global = wpa_s->global;
356 ifname = os_strdup(wpa_s->ifname);
357 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
358 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
359 wpa_s = global->ifaces;
361 wpa_drv_if_remove(wpa_s, type, ifname);
366 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
367 if (ssid && (ssid->p2p_group ||
368 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
369 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
371 if (ssid == wpa_s->current_ssid) {
372 wpa_sm_set_config(wpa_s->wpa, NULL);
373 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
374 wpa_s->current_ssid = NULL;
377 * Networks objects created during any P2P activities are not
378 * exposed out as they might/will confuse certain non-P2P aware
379 * applications since these network objects won't behave like
382 * Likewise, we don't send out network removed signals for such
385 wpa_config_remove_network(wpa_s->conf, id);
386 wpa_supplicant_clear_status(wpa_s);
387 wpa_supplicant_cancel_sched_scan(wpa_s);
388 wpa_s->sta_scan_pending = 0;
390 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
394 wpa_supplicant_ap_deinit(wpa_s);
396 wpa_drv_deinit_p2p_cli(wpa_s);
402 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
404 const u8 *ssid, size_t ssid_len)
412 if (wpa_s->go_params)
413 bssid = wpa_s->go_params->peer_interface_addr;
415 bssid = wpa_s->bssid;
417 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
419 u8 iface_addr[ETH_ALEN];
420 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
422 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
425 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
426 "group is persistent - BSS " MACSTR " not found",
431 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
433 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
434 "group is persistent - BSS " MACSTR
435 " did not include P2P IE", MAC2STR(bssid));
436 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
437 (u8 *) (bss + 1), bss->ie_len);
438 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
439 ((u8 *) bss + 1) + bss->ie_len,
444 group_capab = p2p_get_group_capab(p2p);
445 addr = p2p_get_go_dev_addr(p2p);
446 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
447 "group_capab=0x%x", group_capab);
449 os_memcpy(go_dev_addr, addr, ETH_ALEN);
450 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
453 os_memset(go_dev_addr, 0, ETH_ALEN);
456 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
457 "go_dev_addr=" MACSTR,
458 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
460 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
464 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
465 struct wpa_ssid *ssid,
466 const u8 *go_dev_addr)
471 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
472 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
473 for (s = wpa_s->conf->ssid; s; s = s->next) {
474 if (s->disabled == 2 &&
475 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
476 s->ssid_len == ssid->ssid_len &&
477 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
482 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
484 if (ssid->passphrase && !s->passphrase)
486 else if (ssid->passphrase && s->passphrase &&
487 os_strcmp(ssid->passphrase, s->passphrase) != 0)
490 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
493 s = wpa_config_add_network(wpa_s->conf);
498 * Instead of network_added we emit persistent_group_added
499 * notification. Also to keep the defense checks in
500 * persistent_group obj registration method, we set the
501 * relevant flags in s to designate it as a persistent group.
504 s->p2p_persistent_group = 1;
505 wpas_notify_persistent_group_added(wpa_s, s);
506 wpa_config_set_network_defaults(s);
510 s->p2p_persistent_group = 1;
513 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
514 s->mode = ssid->mode;
515 s->auth_alg = WPA_AUTH_ALG_OPEN;
516 s->key_mgmt = WPA_KEY_MGMT_PSK;
517 s->proto = WPA_PROTO_RSN;
518 s->pairwise_cipher = WPA_CIPHER_CCMP;
520 if (ssid->passphrase) {
521 os_free(s->passphrase);
522 s->passphrase = os_strdup(ssid->passphrase);
526 os_memcpy(s->psk, ssid->psk, 32);
528 if (s->passphrase && !s->psk_set)
529 wpa_config_update_psk(s);
530 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
532 s->ssid = os_malloc(ssid->ssid_len);
535 s->ssid_len = ssid->ssid_len;
536 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
539 #ifndef CONFIG_NO_CONFIG_WRITE
540 if (changed && wpa_s->conf->update_config &&
541 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
542 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
544 #endif /* CONFIG_NO_CONFIG_WRITE */
550 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
553 struct wpa_ssid *ssid, *s;
558 ssid = wpa_s->current_ssid;
559 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
560 !ssid->p2p_persistent_group)
563 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
564 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
567 if (s->ssid_len == ssid->ssid_len &&
568 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
575 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
576 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
580 if (i == s->num_p2p_clients - 1)
581 return; /* already the most recent entry */
583 /* move the entry to mark it most recent */
584 os_memmove(s->p2p_client_list + i * ETH_ALEN,
585 s->p2p_client_list + (i + 1) * ETH_ALEN,
586 (s->num_p2p_clients - i - 1) * ETH_ALEN);
587 os_memcpy(s->p2p_client_list +
588 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
593 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
594 n = os_realloc_array(s->p2p_client_list,
595 s->num_p2p_clients + 1, ETH_ALEN);
598 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
599 s->p2p_client_list = n;
600 s->num_p2p_clients++;
602 /* Not enough room for an additional entry - drop the oldest
604 os_memmove(s->p2p_client_list,
605 s->p2p_client_list + ETH_ALEN,
606 (s->num_p2p_clients - 1) * ETH_ALEN);
607 os_memcpy(s->p2p_client_list +
608 (s->num_p2p_clients - 1) * ETH_ALEN,
612 #ifndef CONFIG_NO_CONFIG_WRITE
613 if (wpa_s->parent->conf->update_config &&
614 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
615 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
616 #endif /* CONFIG_NO_CONFIG_WRITE */
620 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
623 struct wpa_ssid *ssid;
624 const char *ssid_txt;
627 u8 go_dev_addr[ETH_ALEN];
631 * This callback is likely called for the main interface. Update wpa_s
632 * to use the group interface if a new interface was created for the
635 if (wpa_s->global->p2p_group_formation)
636 wpa_s = wpa_s->global->p2p_group_formation;
637 wpa_s->global->p2p_group_formation = NULL;
638 wpa_s->p2p_in_provisioning = 0;
641 wpa_msg(wpa_s->parent, MSG_INFO,
642 P2P_EVENT_GROUP_FORMATION_FAILURE);
643 wpas_p2p_group_delete(wpa_s,
644 P2P_GROUP_REMOVAL_FORMATION_FAILED);
648 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
650 ssid = wpa_s->current_ssid;
651 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
652 ssid->mode = WPAS_MODE_P2P_GO;
653 p2p_group_notif_formation_done(wpa_s->p2p_group);
654 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
659 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
660 client = ssid->mode == WPAS_MODE_INFRA;
661 if (ssid->mode == WPAS_MODE_P2P_GO) {
662 persistent = ssid->p2p_persistent_group;
663 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
666 persistent = wpas_p2p_persistent_group(wpa_s,
672 client = wpa_s->p2p_group_interface ==
673 P2P_GROUP_INTERFACE_CLIENT;
674 os_memset(go_dev_addr, 0, ETH_ALEN);
677 wpa_s->show_group_started = 0;
680 * Indicate event only after successfully completed 4-way
681 * handshake, i.e., when the interface is ready for data
684 wpa_s->show_group_started = 1;
685 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
687 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
688 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
689 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
691 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
692 MAC2STR(go_dev_addr),
693 persistent ? " [PERSISTENT]" : "");
694 wpas_p2p_cross_connect_setup(wpa_s);
695 wpas_p2p_set_group_idle_timeout(wpa_s);
697 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
698 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
699 "go_dev_addr=" MACSTR "%s",
700 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
701 ssid && ssid->passphrase ? ssid->passphrase : "",
702 MAC2STR(go_dev_addr),
703 persistent ? " [PERSISTENT]" : "");
704 wpas_p2p_cross_connect_setup(wpa_s);
705 wpas_p2p_set_group_idle_timeout(wpa_s);
709 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
711 if (network_id < 0 && ssid)
712 network_id = ssid->id;
714 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
718 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
720 const u8 *dst, const u8 *src,
722 const u8 *data, size_t data_len,
723 enum offchannel_send_action_result
726 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
728 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
730 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
734 case OFFCHANNEL_SEND_ACTION_SUCCESS:
735 res = P2P_SEND_ACTION_SUCCESS;
737 case OFFCHANNEL_SEND_ACTION_NO_ACK:
738 res = P2P_SEND_ACTION_NO_ACK;
740 case OFFCHANNEL_SEND_ACTION_FAILED:
741 res = P2P_SEND_ACTION_FAILED;
745 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
747 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
748 wpa_s->pending_pd_before_join &&
749 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
750 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
751 wpa_s->p2p_fallback_to_go_neg) {
752 wpa_s->pending_pd_before_join = 0;
753 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
754 "during p2p_connect-auto");
755 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
761 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
762 const u8 *src, const u8 *bssid, const u8 *buf,
763 size_t len, unsigned int wait_time)
765 struct wpa_supplicant *wpa_s = ctx;
766 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
768 wpas_p2p_send_action_tx_status, 1);
772 static void wpas_send_action_done(void *ctx)
774 struct wpa_supplicant *wpa_s = ctx;
775 offchannel_send_action_done(wpa_s);
779 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
780 struct p2p_go_neg_results *params)
782 if (wpa_s->go_params == NULL) {
783 wpa_s->go_params = os_malloc(sizeof(*params));
784 if (wpa_s->go_params == NULL)
787 os_memcpy(wpa_s->go_params, params, sizeof(*params));
792 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
793 struct p2p_go_neg_results *res)
795 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
796 MAC2STR(res->peer_interface_addr));
797 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
798 res->ssid, res->ssid_len);
799 wpa_supplicant_ap_deinit(wpa_s);
800 wpas_copy_go_neg_results(wpa_s, res);
801 if (res->wps_method == WPS_PBC)
802 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
804 u16 dev_pw_id = DEV_PW_DEFAULT;
805 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
806 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
807 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
808 wpa_s->p2p_pin, 1, dev_pw_id);
813 static void p2p_go_configured(void *ctx, void *data)
815 struct wpa_supplicant *wpa_s = ctx;
816 struct p2p_go_neg_results *params = data;
817 struct wpa_ssid *ssid;
820 ssid = wpa_s->current_ssid;
821 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
822 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
823 if (wpa_s->global->p2p_group_formation == wpa_s)
824 wpa_s->global->p2p_group_formation = NULL;
825 if (os_strlen(params->passphrase) > 0) {
826 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
827 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
828 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
829 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
830 ssid->frequency, params->passphrase,
831 MAC2STR(wpa_s->global->p2p_dev_addr),
832 params->persistent_group ? " [PERSISTENT]" :
836 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
837 sizeof(params->psk));
838 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
839 "%s GO ssid=\"%s\" freq=%d psk=%s "
840 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
841 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
842 ssid->frequency, psk,
843 MAC2STR(wpa_s->global->p2p_dev_addr),
844 params->persistent_group ? " [PERSISTENT]" :
848 if (params->persistent_group)
849 network_id = wpas_p2p_store_persistent_group(
851 wpa_s->global->p2p_dev_addr);
853 network_id = ssid->id;
854 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
855 wpas_p2p_cross_connect_setup(wpa_s);
856 wpas_p2p_set_group_idle_timeout(wpa_s);
860 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
861 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
862 params->peer_interface_addr)) {
863 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
867 if (params->wps_method == WPS_PBC)
868 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
869 params->peer_device_addr);
870 else if (wpa_s->p2p_pin[0])
871 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
872 wpa_s->p2p_pin, NULL, 0, 0);
873 os_free(wpa_s->go_params);
874 wpa_s->go_params = NULL;
878 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
879 struct p2p_go_neg_results *params,
882 struct wpa_ssid *ssid;
884 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
885 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
886 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
891 ssid = wpa_config_add_network(wpa_s->conf);
893 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
897 wpa_s->show_group_started = 0;
899 wpa_config_set_network_defaults(ssid);
902 ssid->p2p_persistent_group = params->persistent_group;
903 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
905 ssid->frequency = params->freq;
906 ssid->ht40 = params->ht40;
907 ssid->ssid = os_zalloc(params->ssid_len + 1);
909 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
910 ssid->ssid_len = params->ssid_len;
912 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
913 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
914 ssid->proto = WPA_PROTO_RSN;
915 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
916 if (os_strlen(params->passphrase) > 0) {
917 ssid->passphrase = os_strdup(params->passphrase);
918 if (ssid->passphrase == NULL) {
919 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
920 "passphrase for GO");
921 wpa_config_remove_network(wpa_s->conf, ssid->id);
925 ssid->passphrase = NULL;
926 ssid->psk_set = params->psk_set;
928 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
929 else if (ssid->passphrase)
930 wpa_config_update_psk(ssid);
931 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
933 wpa_s->ap_configured_cb = p2p_go_configured;
934 wpa_s->ap_configured_cb_ctx = wpa_s;
935 wpa_s->ap_configured_cb_data = wpa_s->go_params;
936 wpa_s->connect_without_scan = ssid;
937 wpa_s->reassociate = 1;
938 wpa_s->disconnected = 0;
939 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
941 wpa_supplicant_req_scan(wpa_s, 0, 0);
945 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
946 const struct wpa_supplicant *src)
948 struct wpa_config *d;
949 const struct wpa_config *s;
954 #define C(n) if (s->n) d->n = os_strdup(s->n)
963 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
964 os_memcpy(d->sec_device_type, s->sec_device_type,
965 sizeof(d->sec_device_type));
966 d->num_sec_device_types = s->num_sec_device_types;
968 d->p2p_group_idle = s->p2p_group_idle;
969 d->p2p_intra_bss = s->p2p_intra_bss;
970 d->persistent_reconnect = s->persistent_reconnect;
971 d->max_num_sta = s->max_num_sta;
972 d->pbc_in_m1 = s->pbc_in_m1;
976 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
977 enum wpa_driver_if_type type)
979 char ifname[120], force_ifname[120];
981 if (wpa_s->pending_interface_name[0]) {
982 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
983 "- skip creation of a new one");
984 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
985 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
986 "unknown?! ifname='%s'",
987 wpa_s->pending_interface_name);
993 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
994 wpa_s->p2p_group_idx);
995 if (os_strlen(ifname) >= IFNAMSIZ &&
996 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
997 /* Try to avoid going over the IFNAMSIZ length limit */
998 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
999 wpa_s->p2p_group_idx);
1001 force_ifname[0] = '\0';
1003 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1005 wpa_s->p2p_group_idx++;
1007 wpa_s->pending_interface_type = type;
1008 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1009 wpa_s->pending_interface_addr, NULL) < 0) {
1010 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1015 if (force_ifname[0]) {
1016 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1018 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1019 sizeof(wpa_s->pending_interface_name));
1021 os_strlcpy(wpa_s->pending_interface_name, ifname,
1022 sizeof(wpa_s->pending_interface_name));
1023 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1024 MACSTR, wpa_s->pending_interface_name,
1025 MAC2STR(wpa_s->pending_interface_addr));
1031 static void wpas_p2p_remove_pending_group_interface(
1032 struct wpa_supplicant *wpa_s)
1034 if (!wpa_s->pending_interface_name[0] ||
1035 is_zero_ether_addr(wpa_s->pending_interface_addr))
1036 return; /* No pending virtual interface */
1038 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1039 wpa_s->pending_interface_name);
1040 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1041 wpa_s->pending_interface_name);
1042 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1043 wpa_s->pending_interface_name[0] = '\0';
1047 static struct wpa_supplicant *
1048 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1050 struct wpa_interface iface;
1051 struct wpa_supplicant *group_wpa_s;
1053 if (!wpa_s->pending_interface_name[0]) {
1054 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1055 if (!wpas_p2p_create_iface(wpa_s))
1058 * Something has forced us to remove the pending interface; try
1059 * to create a new one and hope for the best that we will get
1060 * the same local address.
1062 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1063 WPA_IF_P2P_CLIENT) < 0)
1067 os_memset(&iface, 0, sizeof(iface));
1068 iface.ifname = wpa_s->pending_interface_name;
1069 iface.driver = wpa_s->driver->name;
1070 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1071 iface.driver_param = wpa_s->conf->driver_param;
1072 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1073 if (group_wpa_s == NULL) {
1074 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1075 "wpa_supplicant interface");
1078 wpa_s->pending_interface_name[0] = '\0';
1079 group_wpa_s->parent = wpa_s;
1080 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1081 P2P_GROUP_INTERFACE_CLIENT;
1082 wpa_s->global->p2p_group_formation = group_wpa_s;
1084 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1090 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1093 struct wpa_supplicant *wpa_s = eloop_ctx;
1094 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1095 if (wpa_s->global->p2p)
1096 p2p_group_formation_failed(wpa_s->global->p2p);
1097 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1098 wpa_drv_p2p_group_formation_failed(wpa_s);
1099 wpas_group_formation_completed(wpa_s, 0);
1103 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1105 struct wpa_supplicant *wpa_s = ctx;
1107 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1108 wpa_drv_cancel_remain_on_channel(wpa_s);
1109 wpa_s->off_channel_freq = 0;
1110 wpa_s->roc_waiting_drv_freq = 0;
1114 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1116 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1117 wpas_p2p_remove_pending_group_interface(wpa_s);
1121 if (wpa_s->p2p_go_ht40)
1124 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1125 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1127 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1128 struct wpa_ssid *ssid;
1129 ssid = wpa_config_get_network(wpa_s->conf,
1130 wpa_s->p2p_persistent_id);
1131 if (ssid && ssid->disabled == 2 &&
1132 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1133 size_t len = os_strlen(ssid->passphrase);
1134 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1135 "on requested persistent group");
1136 os_memcpy(res->passphrase, ssid->passphrase, len);
1137 res->passphrase[len] = '\0';
1141 if (wpa_s->create_p2p_iface) {
1142 struct wpa_supplicant *group_wpa_s =
1143 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1144 if (group_wpa_s == NULL) {
1145 wpas_p2p_remove_pending_group_interface(wpa_s);
1148 if (group_wpa_s != wpa_s) {
1149 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1150 sizeof(group_wpa_s->p2p_pin));
1151 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1153 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1154 wpa_s->pending_interface_name[0] = '\0';
1155 group_wpa_s->p2p_in_provisioning = 1;
1158 wpas_start_wps_go(group_wpa_s, res, 1);
1160 wpas_start_wps_enrollee(group_wpa_s, res);
1162 wpa_s->p2p_in_provisioning = 1;
1163 wpa_s->global->p2p_group_formation = wpa_s;
1166 wpas_start_wps_go(wpa_s, res, 1);
1168 wpas_start_wps_enrollee(ctx, res);
1171 wpa_s->p2p_long_listen = 0;
1172 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1174 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1175 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1176 (res->peer_config_timeout % 100) * 10000,
1177 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1181 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1183 struct wpa_supplicant *wpa_s = ctx;
1184 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1185 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1187 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1191 void wpas_dev_found(void *ctx, const u8 *addr,
1192 const struct p2p_peer_info *info,
1195 #ifndef CONFIG_NO_STDOUT_DEBUG
1196 struct wpa_supplicant *wpa_s = ctx;
1197 char devtype[WPS_DEV_TYPE_BUFSIZE];
1199 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1200 " p2p_dev_addr=" MACSTR
1201 " pri_dev_type=%s name='%s' config_methods=0x%x "
1202 "dev_capab=0x%x group_capab=0x%x",
1203 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1204 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1206 info->device_name, info->config_methods,
1207 info->dev_capab, info->group_capab);
1208 #endif /* CONFIG_NO_STDOUT_DEBUG */
1210 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1214 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1216 struct wpa_supplicant *wpa_s = ctx;
1218 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1219 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1221 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1225 static int wpas_start_listen(void *ctx, unsigned int freq,
1226 unsigned int duration,
1227 const struct wpabuf *probe_resp_ie)
1229 struct wpa_supplicant *wpa_s = ctx;
1231 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1233 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1234 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1235 "report received Probe Request frames");
1239 wpa_s->pending_listen_freq = freq;
1240 wpa_s->pending_listen_duration = duration;
1242 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1243 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1244 "to remain on channel (%u MHz) for Listen "
1246 wpa_s->pending_listen_freq = 0;
1249 wpa_s->off_channel_freq = 0;
1250 wpa_s->roc_waiting_drv_freq = freq;
1256 static void wpas_stop_listen(void *ctx)
1258 struct wpa_supplicant *wpa_s = ctx;
1259 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1260 wpa_drv_cancel_remain_on_channel(wpa_s);
1261 wpa_s->off_channel_freq = 0;
1262 wpa_s->roc_waiting_drv_freq = 0;
1264 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1265 wpa_drv_probe_req_report(wpa_s, 0);
1269 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1271 struct wpa_supplicant *wpa_s = ctx;
1272 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1277 * DNS Header section is used only to calculate compression pointers, so the
1278 * contents of this data does not matter, but the length needs to be reserved
1279 * in the virtual packet.
1281 #define DNS_HEADER_LEN 12
1284 * 27-octet in-memory packet from P2P specification containing two implied
1285 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1287 #define P2P_SD_IN_MEMORY_LEN 27
1289 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1290 u8 **spos, const u8 *end)
1292 while (*spos < end) {
1293 u8 val = ((*spos)[0] & 0xc0) >> 6;
1296 if (val == 1 || val == 2) {
1297 /* These are reserved values in RFC 1035 */
1298 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1299 "sequence starting with 0x%x", val);
1308 if (*spos + 2 > end) {
1309 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1310 "DNS offset field");
1314 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1315 if (offset >= *spos - start) {
1316 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1317 "pointer offset %u", offset);
1322 spos_tmp = start + offset;
1323 return p2p_sd_dns_uncompress_label(upos, uend, start,
1329 len = (*spos)[0] & 0x3f;
1334 if (*spos + len > end) {
1335 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1336 "sequence - no room for label with length "
1341 if (*upos + len + 2 > uend)
1344 os_memcpy(*upos, *spos, len);
1356 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1357 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1358 * not large enough */
1359 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1360 size_t msg_len, size_t offset)
1362 /* 27-octet in-memory packet from P2P specification */
1363 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1364 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1365 u8 *tmp, *end, *spos;
1371 if (offset > msg_len)
1374 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1377 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1378 end = spos + msg_len;
1381 os_memset(tmp, 0, DNS_HEADER_LEN);
1382 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1383 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1386 uend = buf + buf_len;
1388 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1397 } else if (upos[-1] == '.')
1405 static struct p2p_srv_bonjour *
1406 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1407 const struct wpabuf *query)
1409 struct p2p_srv_bonjour *bsrv;
1412 len = wpabuf_len(query);
1413 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1414 struct p2p_srv_bonjour, list) {
1415 if (len == wpabuf_len(bsrv->query) &&
1416 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1424 static struct p2p_srv_upnp *
1425 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1426 const char *service)
1428 struct p2p_srv_upnp *usrv;
1430 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1431 struct p2p_srv_upnp, list) {
1432 if (version == usrv->version &&
1433 os_strcmp(service, usrv->service) == 0)
1440 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1445 if (wpabuf_tailroom(resp) < 5)
1448 /* Length (to be filled) */
1449 len_pos = wpabuf_put(resp, 2);
1450 wpabuf_put_u8(resp, srv_proto);
1451 wpabuf_put_u8(resp, srv_trans_id);
1453 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1454 /* Response Data: empty */
1455 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1459 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1460 struct wpabuf *resp, u8 srv_trans_id)
1462 struct p2p_srv_bonjour *bsrv;
1465 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1467 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1468 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1472 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1473 struct p2p_srv_bonjour, list) {
1474 if (wpabuf_tailroom(resp) <
1475 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1477 /* Length (to be filled) */
1478 len_pos = wpabuf_put(resp, 2);
1479 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1480 wpabuf_put_u8(resp, srv_trans_id);
1482 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1483 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1484 wpabuf_head(bsrv->resp),
1485 wpabuf_len(bsrv->resp));
1487 wpabuf_put_buf(resp, bsrv->query); /* Key */
1488 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1489 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1495 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1498 char str_rx[256], str_srv[256];
1500 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1501 return 0; /* Too short to include DNS Type and Version */
1502 if (os_memcmp(query + query_len - 3,
1503 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1505 return 0; /* Mismatch in DNS Type or Version */
1506 if (query_len == wpabuf_len(bsrv->query) &&
1507 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1508 return 1; /* Binary match */
1510 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1512 return 0; /* Failed to uncompress query */
1513 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1514 wpabuf_head(bsrv->query),
1515 wpabuf_len(bsrv->query) - 3, 0))
1516 return 0; /* Failed to uncompress service */
1518 return os_strcmp(str_rx, str_srv) == 0;
1522 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1523 struct wpabuf *resp, u8 srv_trans_id,
1524 const u8 *query, size_t query_len)
1526 struct p2p_srv_bonjour *bsrv;
1530 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1532 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1533 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1534 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1539 if (query_len == 0) {
1540 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1544 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1545 struct p2p_srv_bonjour, list) {
1546 if (!match_bonjour_query(bsrv, query, query_len))
1549 if (wpabuf_tailroom(resp) <
1550 5 + query_len + wpabuf_len(bsrv->resp))
1555 /* Length (to be filled) */
1556 len_pos = wpabuf_put(resp, 2);
1557 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1558 wpabuf_put_u8(resp, srv_trans_id);
1561 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1562 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1563 wpabuf_head(bsrv->resp),
1564 wpabuf_len(bsrv->resp));
1567 wpabuf_put_data(resp, query, query_len); /* Key */
1568 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1570 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1574 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1576 if (wpabuf_tailroom(resp) < 5)
1579 /* Length (to be filled) */
1580 len_pos = wpabuf_put(resp, 2);
1581 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1582 wpabuf_put_u8(resp, srv_trans_id);
1585 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1586 /* Response Data: empty */
1587 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1593 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1594 struct wpabuf *resp, u8 srv_trans_id)
1596 struct p2p_srv_upnp *usrv;
1599 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1601 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1602 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1606 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1607 struct p2p_srv_upnp, list) {
1608 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1611 /* Length (to be filled) */
1612 len_pos = wpabuf_put(resp, 2);
1613 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1614 wpabuf_put_u8(resp, srv_trans_id);
1617 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1619 wpabuf_put_u8(resp, usrv->version);
1620 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1622 wpabuf_put_str(resp, usrv->service);
1623 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1629 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1630 struct wpabuf *resp, u8 srv_trans_id,
1631 const u8 *query, size_t query_len)
1633 struct p2p_srv_upnp *usrv;
1639 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1642 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1643 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1644 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1649 if (query_len == 0) {
1650 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1654 if (wpabuf_tailroom(resp) < 5)
1657 /* Length (to be filled) */
1658 len_pos = wpabuf_put(resp, 2);
1659 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1660 wpabuf_put_u8(resp, srv_trans_id);
1663 str = os_malloc(query_len);
1666 os_memcpy(str, query + 1, query_len - 1);
1667 str[query_len - 1] = '\0';
1669 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1670 struct p2p_srv_upnp, list) {
1671 if (version != usrv->version)
1674 if (os_strcmp(str, "ssdp:all") != 0 &&
1675 os_strstr(usrv->service, str) == NULL)
1678 if (wpabuf_tailroom(resp) < 2)
1682 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1684 wpabuf_put_u8(resp, version);
1686 wpabuf_put_u8(resp, ',');
1690 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1692 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1694 wpabuf_put_str(resp, usrv->service);
1699 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1702 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1703 /* Response Data: empty */
1706 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1710 #ifdef CONFIG_WIFI_DISPLAY
1711 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1712 struct wpabuf *resp, u8 srv_trans_id,
1713 const u8 *query, size_t query_len)
1719 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1721 if (!wpa_s->global->wifi_display) {
1722 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1723 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1728 if (query_len < 1) {
1729 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1734 if (wpabuf_tailroom(resp) < 5)
1739 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1741 /* TODO: role specific handling */
1743 /* Length (to be filled) */
1744 len_pos = wpabuf_put(resp, 2);
1745 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1746 wpabuf_put_u8(resp, srv_trans_id);
1747 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1749 while (pos < query + query_len) {
1750 if (*pos < MAX_WFD_SUBELEMS &&
1751 wpa_s->global->wfd_subelem[*pos] &&
1752 wpabuf_tailroom(resp) >=
1753 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1754 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1755 "subelement %u", *pos);
1756 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1761 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1763 #endif /* CONFIG_WIFI_DISPLAY */
1766 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1767 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1769 struct wpa_supplicant *wpa_s = ctx;
1770 const u8 *pos = tlvs;
1771 const u8 *end = tlvs + tlvs_len;
1774 struct wpabuf *resp;
1775 u8 srv_proto, srv_trans_id;
1779 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1781 buf_len = 2 * tlvs_len + 1;
1782 buf = os_malloc(buf_len);
1784 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1785 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1787 freq, MAC2STR(sa), dialog_token, update_indic,
1792 if (wpa_s->p2p_sd_over_ctrl_iface) {
1793 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1794 update_indic, tlvs, tlvs_len);
1795 return; /* to be processed by an external program */
1798 resp = wpabuf_alloc(10000);
1802 while (pos + 1 < end) {
1803 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1804 slen = WPA_GET_LE16(pos);
1806 if (pos + slen > end || slen < 2) {
1807 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1812 tlv_end = pos + slen;
1815 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1817 srv_trans_id = *pos++;
1818 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1821 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1822 pos, tlv_end - pos);
1825 if (wpa_s->force_long_sd) {
1826 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1828 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1829 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1833 switch (srv_proto) {
1834 case P2P_SERV_ALL_SERVICES:
1835 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1836 "for all services");
1837 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1838 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1839 wpa_printf(MSG_DEBUG, "P2P: No service "
1840 "discovery protocols available");
1841 wpas_sd_add_proto_not_avail(
1842 resp, P2P_SERV_ALL_SERVICES,
1846 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1847 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1849 case P2P_SERV_BONJOUR:
1850 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1851 pos, tlv_end - pos);
1854 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1855 pos, tlv_end - pos);
1857 #ifdef CONFIG_WIFI_DISPLAY
1858 case P2P_SERV_WIFI_DISPLAY:
1859 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1860 pos, tlv_end - pos);
1862 #endif /* CONFIG_WIFI_DISPLAY */
1864 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1865 "protocol %u", srv_proto);
1866 wpas_sd_add_proto_not_avail(resp, srv_proto,
1875 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1876 update_indic, tlvs, tlvs_len);
1878 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1884 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1885 const u8 *tlvs, size_t tlvs_len)
1887 struct wpa_supplicant *wpa_s = ctx;
1888 const u8 *pos = tlvs;
1889 const u8 *end = tlvs + tlvs_len;
1895 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1897 if (tlvs_len > 1500) {
1898 /* TODO: better way for handling this */
1899 wpa_msg_ctrl(wpa_s, MSG_INFO,
1900 P2P_EVENT_SERV_DISC_RESP MACSTR
1901 " %u <long response: %u bytes>",
1902 MAC2STR(sa), update_indic,
1903 (unsigned int) tlvs_len);
1905 buf_len = 2 * tlvs_len + 1;
1906 buf = os_malloc(buf_len);
1908 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1909 wpa_msg_ctrl(wpa_s, MSG_INFO,
1910 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1911 MAC2STR(sa), update_indic, buf);
1917 u8 srv_proto, srv_trans_id, status;
1919 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1920 slen = WPA_GET_LE16(pos);
1922 if (pos + slen > end || slen < 3) {
1923 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1927 tlv_end = pos + slen;
1930 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1932 srv_trans_id = *pos++;
1933 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1936 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1939 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1940 pos, tlv_end - pos);
1945 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1949 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1950 const struct wpabuf *tlvs)
1952 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1953 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1954 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1956 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1960 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1961 u8 version, const char *query)
1963 struct wpabuf *tlvs;
1966 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1969 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1970 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1971 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1972 wpabuf_put_u8(tlvs, version);
1973 wpabuf_put_str(tlvs, query);
1974 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1980 #ifdef CONFIG_WIFI_DISPLAY
1982 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1983 const struct wpabuf *tlvs)
1985 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1987 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1989 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1993 #define MAX_WFD_SD_SUBELEMS 20
1995 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1996 const char *subelems)
2003 len = wpabuf_put(tlvs, 2);
2004 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2005 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2007 wpabuf_put_u8(tlvs, role);
2012 if (val >= 0 && val < 256) {
2013 wpabuf_put_u8(tlvs, val);
2015 if (count == MAX_WFD_SD_SUBELEMS)
2018 pos = os_strchr(pos + 1, ',');
2024 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2028 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2029 const u8 *dst, const char *role)
2031 struct wpabuf *tlvs;
2033 const char *subelems;
2036 subelems = os_strchr(role, ' ');
2037 if (subelems == NULL)
2041 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2045 if (os_strstr(role, "[source]"))
2046 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2047 if (os_strstr(role, "[pri-sink]"))
2048 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2049 if (os_strstr(role, "[sec-sink]"))
2050 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2051 if (os_strstr(role, "[source+sink]"))
2052 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2054 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2059 #endif /* CONFIG_WIFI_DISPLAY */
2062 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2064 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2065 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
2066 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2068 return p2p_sd_cancel_request(wpa_s->global->p2p,
2069 (void *) (uintptr_t) req);
2073 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2074 const u8 *dst, u8 dialog_token,
2075 const struct wpabuf *resp_tlvs)
2077 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2078 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2082 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2084 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2089 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2091 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2092 wpa_drv_p2p_service_update(wpa_s);
2095 if (wpa_s->global->p2p)
2096 p2p_sd_service_update(wpa_s->global->p2p);
2100 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2102 dl_list_del(&bsrv->list);
2103 wpabuf_free(bsrv->query);
2104 wpabuf_free(bsrv->resp);
2109 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2111 dl_list_del(&usrv->list);
2112 os_free(usrv->service);
2117 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2119 struct p2p_srv_bonjour *bsrv, *bn;
2120 struct p2p_srv_upnp *usrv, *un;
2122 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2123 struct p2p_srv_bonjour, list)
2124 wpas_p2p_srv_bonjour_free(bsrv);
2126 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2127 struct p2p_srv_upnp, list)
2128 wpas_p2p_srv_upnp_free(usrv);
2130 wpas_p2p_sd_service_update(wpa_s);
2134 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2135 struct wpabuf *query, struct wpabuf *resp)
2137 struct p2p_srv_bonjour *bsrv;
2139 bsrv = os_zalloc(sizeof(*bsrv));
2142 bsrv->query = query;
2144 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2146 wpas_p2p_sd_service_update(wpa_s);
2151 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2152 const struct wpabuf *query)
2154 struct p2p_srv_bonjour *bsrv;
2156 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2159 wpas_p2p_srv_bonjour_free(bsrv);
2160 wpas_p2p_sd_service_update(wpa_s);
2165 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2166 const char *service)
2168 struct p2p_srv_upnp *usrv;
2170 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2171 return 0; /* Already listed */
2172 usrv = os_zalloc(sizeof(*usrv));
2175 usrv->version = version;
2176 usrv->service = os_strdup(service);
2177 if (usrv->service == NULL) {
2181 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2183 wpas_p2p_sd_service_update(wpa_s);
2188 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2189 const char *service)
2191 struct p2p_srv_upnp *usrv;
2193 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2196 wpas_p2p_srv_upnp_free(usrv);
2197 wpas_p2p_sd_service_update(wpa_s);
2202 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2203 const u8 *peer, const char *params,
2204 unsigned int generated_pin)
2206 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2207 MAC2STR(peer), generated_pin, params);
2211 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2212 const u8 *peer, const char *params)
2214 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2215 MAC2STR(peer), params);
2219 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2220 const u8 *dev_addr, const u8 *pri_dev_type,
2221 const char *dev_name, u16 supp_config_methods,
2222 u8 dev_capab, u8 group_capab, const u8 *group_id,
2223 size_t group_id_len)
2225 struct wpa_supplicant *wpa_s = ctx;
2226 char devtype[WPS_DEV_TYPE_BUFSIZE];
2228 u8 empty_dev_type[8];
2229 unsigned int generated_pin = 0;
2230 struct wpa_supplicant *group = NULL;
2233 for (group = wpa_s->global->ifaces; group; group = group->next)
2235 struct wpa_ssid *s = group->current_ssid;
2237 s->mode == WPAS_MODE_P2P_GO &&
2238 group_id_len - ETH_ALEN == s->ssid_len &&
2239 os_memcmp(group_id + ETH_ALEN, s->ssid,
2245 if (pri_dev_type == NULL) {
2246 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2247 pri_dev_type = empty_dev_type;
2249 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2250 " pri_dev_type=%s name='%s' config_methods=0x%x "
2251 "dev_capab=0x%x group_capab=0x%x%s%s",
2253 wps_dev_type_bin2str(pri_dev_type, devtype,
2255 dev_name, supp_config_methods, dev_capab, group_capab,
2256 group ? " group=" : "",
2257 group ? group->ifname : "");
2258 params[sizeof(params) - 1] = '\0';
2260 if (config_methods & WPS_CONFIG_DISPLAY) {
2261 generated_pin = wps_generate_pin();
2262 wpas_prov_disc_local_display(wpa_s, peer, params,
2264 } else if (config_methods & WPS_CONFIG_KEYPAD)
2265 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2266 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2267 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2268 "%s", MAC2STR(peer), params);
2270 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2271 P2P_PROV_DISC_SUCCESS,
2272 config_methods, generated_pin);
2276 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2278 struct wpa_supplicant *wpa_s = ctx;
2279 unsigned int generated_pin = 0;
2282 if (wpa_s->pending_pd_before_join &&
2283 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2284 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2285 wpa_s->pending_pd_before_join = 0;
2286 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2287 "join-existing-group operation");
2288 wpas_p2p_join_start(wpa_s);
2292 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2293 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2294 os_snprintf(params, sizeof(params), " peer_go=%d",
2295 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2299 if (config_methods & WPS_CONFIG_DISPLAY)
2300 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2301 else if (config_methods & WPS_CONFIG_KEYPAD) {
2302 generated_pin = wps_generate_pin();
2303 wpas_prov_disc_local_display(wpa_s, peer, params,
2305 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2306 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2307 "%s", MAC2STR(peer), params);
2309 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2310 P2P_PROV_DISC_SUCCESS,
2311 config_methods, generated_pin);
2315 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2316 enum p2p_prov_disc_status status)
2318 struct wpa_supplicant *wpa_s = ctx;
2320 if (wpa_s->p2p_fallback_to_go_neg) {
2321 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2322 "failed - fall back to GO Negotiation");
2323 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2327 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2328 wpa_s->pending_pd_before_join = 0;
2329 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2330 "join-existing-group operation (no ACK for PD "
2332 wpas_p2p_join_start(wpa_s);
2336 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2337 " p2p_dev_addr=" MACSTR " status=%d",
2338 MAC2STR(peer), status);
2340 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2345 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2346 const u8 *go_dev_addr, const u8 *ssid,
2347 size_t ssid_len, int *go, u8 *group_bssid,
2348 int *force_freq, int persistent_group)
2350 struct wpa_supplicant *wpa_s = ctx;
2352 u8 cur_bssid[ETH_ALEN];
2354 struct wpa_supplicant *grp;
2356 if (!persistent_group) {
2357 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2358 " to join an active group", MAC2STR(sa));
2359 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2360 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2362 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2363 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2364 "authorized invitation");
2368 * Do not accept the invitation automatically; notify user and
2371 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2374 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2376 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2377 "running persistent group");
2379 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2383 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2384 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2385 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2386 "invitation to re-invoke a persistent group");
2387 } else if (!wpa_s->conf->persistent_reconnect)
2388 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2390 for (s = wpa_s->conf->ssid; s; s = s->next) {
2391 if (s->disabled == 2 &&
2392 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2393 s->ssid_len == ssid_len &&
2394 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2399 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2400 " requested reinvocation of an unknown group",
2402 return P2P_SC_FAIL_UNKNOWN_GROUP;
2405 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2407 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2408 wpa_printf(MSG_DEBUG, "P2P: The only available "
2409 "interface is already in use - reject "
2411 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2413 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2414 } else if (s->mode == WPAS_MODE_P2P_GO) {
2416 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2418 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2419 "interface address for the group");
2420 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2422 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2427 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2429 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2430 wpa_s->assoc_freq) {
2431 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2432 "the channel we are already using");
2433 *force_freq = wpa_s->assoc_freq;
2434 wpas_p2p_set_own_freq_preference(wpa_s, wpa_s->assoc_freq);
2437 res = wpa_drv_shared_freq(wpa_s);
2439 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2440 "with the channel we are already using on a "
2441 "shared interface");
2443 wpas_p2p_set_own_freq_preference(wpa_s, res);
2446 return P2P_SC_SUCCESS;
2450 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2451 const u8 *ssid, size_t ssid_len,
2452 const u8 *go_dev_addr, u8 status,
2455 struct wpa_supplicant *wpa_s = ctx;
2458 for (s = wpa_s->conf->ssid; s; s = s->next) {
2459 if (s->disabled == 2 &&
2460 s->ssid_len == ssid_len &&
2461 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2465 if (status == P2P_SC_SUCCESS) {
2466 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2467 " was accepted; op_freq=%d MHz",
2468 MAC2STR(sa), op_freq);
2470 int go = s->mode == WPAS_MODE_P2P_GO;
2471 wpas_p2p_group_add_persistent(
2472 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
2474 wpa_s->user_initiated_pd = 0;
2475 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2476 wpa_s->p2p_wps_method, 0);
2481 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2482 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2483 " was rejected (status %u)", MAC2STR(sa), status);
2489 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2490 "sa=" MACSTR " go_dev_addr=" MACSTR
2491 " bssid=" MACSTR " unknown-network",
2492 MAC2STR(sa), MAC2STR(go_dev_addr),
2495 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2496 "sa=" MACSTR " go_dev_addr=" MACSTR
2498 MAC2STR(sa), MAC2STR(go_dev_addr));
2503 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2504 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2505 MACSTR " persistent=%d freq=%d",
2506 MAC2STR(sa), s->id, op_freq);
2508 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2509 MACSTR " persistent=%d", MAC2STR(sa), s->id);
2514 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2515 struct wpa_ssid *ssid,
2523 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2524 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2528 if (i >= ssid->num_p2p_clients) {
2529 if (ssid->mode != WPAS_MODE_P2P_GO &&
2530 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2531 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2532 "due to invitation result", ssid->id);
2533 wpas_notify_network_removed(wpa_s, ssid);
2534 wpa_config_remove_network(wpa_s->conf, ssid->id);
2537 return; /* Peer not found in client list */
2540 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2541 "group %d client list due to invitation result",
2542 MAC2STR(peer), ssid->id);
2543 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2544 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2545 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2546 ssid->num_p2p_clients--;
2547 #ifndef CONFIG_NO_CONFIG_WRITE
2548 if (wpa_s->parent->conf->update_config &&
2549 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2550 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2551 #endif /* CONFIG_NO_CONFIG_WRITE */
2555 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2558 struct wpa_ssid *ssid;
2560 wpa_s = wpa_s->global->p2p_invite_group;
2562 return; /* No known invitation group */
2563 ssid = wpa_s->current_ssid;
2564 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2565 !ssid->p2p_persistent_group)
2566 return; /* Not operating as a GO in persistent group */
2567 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2568 ssid->ssid, ssid->ssid_len);
2569 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2573 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2574 const struct p2p_channels *channels,
2577 struct wpa_supplicant *wpa_s = ctx;
2578 struct wpa_ssid *ssid;
2581 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2582 "status=%d " MACSTR,
2583 status, MAC2STR(bssid));
2585 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2586 "status=%d ", status);
2588 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2590 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2591 status, MAC2STR(peer));
2592 if (wpa_s->pending_invite_ssid_id == -1) {
2593 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2594 wpas_remove_persistent_client(wpa_s, peer);
2595 return; /* Invitation to active group */
2598 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2599 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2600 "invitation exchange to indicate readiness for "
2604 if (status != P2P_SC_SUCCESS) {
2605 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2606 ssid = wpa_config_get_network(
2607 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2608 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2610 wpas_p2p_remove_pending_group_interface(wpa_s);
2614 ssid = wpa_config_get_network(wpa_s->conf,
2615 wpa_s->pending_invite_ssid_id);
2617 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2618 "data matching with invitation");
2623 * The peer could have missed our ctrl::ack frame for Invitation
2624 * Response and continue retransmitting the frame. To reduce the
2625 * likelihood of the peer not getting successful TX status for the
2626 * Invitation Response frame, wait a short time here before starting
2627 * the persistent group so that we will remain on the current channel to
2628 * acknowledge any possible retransmission from the peer.
2630 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2631 "starting persistent group");
2634 wpas_p2p_group_add_persistent(wpa_s, ssid,
2635 ssid->mode == WPAS_MODE_P2P_GO,
2636 wpa_s->p2p_persistent_go_freq,
2637 wpa_s->p2p_go_ht40, channels);
2641 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2646 if (global->p2p_disallow_freq == NULL)
2649 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2650 if (freq >= global->p2p_disallow_freq[i].min &&
2651 freq <= global->p2p_disallow_freq[i].max)
2659 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2661 reg->channel[reg->channels] = chan;
2666 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2667 struct p2p_channels *chan)
2671 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2674 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2675 chan->reg_class[cla].reg_class = 81;
2676 chan->reg_class[cla].channels = 0;
2677 for (i = 0; i < 11; i++) {
2678 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2679 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2681 if (chan->reg_class[cla].channels)
2684 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2687 /* Operating class 115 - 5 GHz, channels 36-48 */
2688 chan->reg_class[cla].reg_class = 115;
2689 chan->reg_class[cla].channels = 0;
2690 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2691 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2692 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2693 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2694 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2695 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2696 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2697 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2698 if (chan->reg_class[cla].channels)
2701 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2704 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2705 chan->reg_class[cla].reg_class = 124;
2706 chan->reg_class[cla].channels = 0;
2707 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2708 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2709 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2710 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2711 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2712 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2713 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2714 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2715 if (chan->reg_class[cla].channels)
2718 chan->reg_classes = cla;
2723 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2725 enum hostapd_hw_mode mode)
2729 for (i = 0; i < num_modes; i++) {
2730 if (modes[i].mode == mode)
2738 static int has_channel(struct wpa_global *global,
2739 struct hostapd_hw_modes *mode, u8 chan, int *flags)
2744 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2746 if (wpas_p2p_disallowed_freq(global, freq))
2749 for (i = 0; i < mode->num_channels; i++) {
2750 if (mode->channels[i].chan == chan) {
2752 *flags = mode->channels[i].flag;
2753 return !(mode->channels[i].flag &
2754 (HOSTAPD_CHAN_DISABLED |
2755 HOSTAPD_CHAN_PASSIVE_SCAN |
2756 HOSTAPD_CHAN_NO_IBSS |
2757 HOSTAPD_CHAN_RADAR));
2765 struct p2p_oper_class_map {
2766 enum hostapd_hw_mode mode;
2771 enum { BW20, BW40PLUS, BW40MINUS } bw;
2774 static struct p2p_oper_class_map op_class[] = {
2775 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2776 #if 0 /* Do not enable HT40 on 2 GHz for now */
2777 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2778 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2780 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2781 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2782 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2783 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2784 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2785 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2786 { -1, 0, 0, 0, 0, BW20 }
2790 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2791 struct hostapd_hw_modes *mode,
2796 if (!has_channel(wpa_s->global, mode, channel, &flag))
2798 if (bw == BW40MINUS &&
2799 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2800 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2802 if (bw == BW40PLUS &&
2803 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2804 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2810 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2811 struct p2p_channels *chan)
2813 struct hostapd_hw_modes *mode;
2816 if (wpa_s->hw.modes == NULL) {
2817 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2818 "of all supported channels; assume dualband "
2820 return wpas_p2p_default_channels(wpa_s, chan);
2825 for (op = 0; op_class[op].op_class; op++) {
2826 struct p2p_oper_class_map *o = &op_class[op];
2828 struct p2p_reg_class *reg = NULL;
2830 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2833 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2834 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2837 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2838 "class %u", o->op_class);
2839 reg = &chan->reg_class[cla];
2841 reg->reg_class = o->op_class;
2843 reg->channel[reg->channels] = ch;
2847 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2848 reg->channel, reg->channels);
2852 chan->reg_classes = cla;
2858 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2859 struct hostapd_hw_modes *mode, u8 channel)
2863 for (op = 0; op_class[op].op_class; op++) {
2864 struct p2p_oper_class_map *o = &op_class[op];
2867 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2868 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2869 o->bw == BW20 || ch != channel)
2871 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2875 return (o->bw == BW40MINUS) ? -1 : 1;
2884 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2887 struct wpa_supplicant *wpa_s = ctx;
2889 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2890 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2896 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2900 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2902 struct wpa_supplicant *wpa_s = ctx;
2904 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2905 struct wpa_ssid *ssid = wpa_s->current_ssid;
2908 if (ssid->mode != WPAS_MODE_INFRA)
2910 if (wpa_s->wpa_state != WPA_COMPLETED &&
2911 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2913 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2922 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2923 * @global: Pointer to global data from wpa_supplicant_init()
2924 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2925 * Returns: 0 on success, -1 on failure
2927 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2929 struct p2p_config p2p;
2933 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2939 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2940 struct p2p_params params;
2942 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2943 os_memset(¶ms, 0, sizeof(params));
2944 params.dev_name = wpa_s->conf->device_name;
2945 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2947 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2948 os_memcpy(params.sec_dev_type,
2949 wpa_s->conf->sec_device_type,
2950 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2952 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2958 os_memset(&p2p, 0, sizeof(p2p));
2959 p2p.msg_ctx = wpa_s;
2961 p2p.p2p_scan = wpas_p2p_scan;
2962 p2p.send_action = wpas_send_action;
2963 p2p.send_action_done = wpas_send_action_done;
2964 p2p.go_neg_completed = wpas_go_neg_completed;
2965 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2966 p2p.dev_found = wpas_dev_found;
2967 p2p.dev_lost = wpas_dev_lost;
2968 p2p.start_listen = wpas_start_listen;
2969 p2p.stop_listen = wpas_stop_listen;
2970 p2p.send_probe_resp = wpas_send_probe_resp;
2971 p2p.sd_request = wpas_sd_request;
2972 p2p.sd_response = wpas_sd_response;
2973 p2p.prov_disc_req = wpas_prov_disc_req;
2974 p2p.prov_disc_resp = wpas_prov_disc_resp;
2975 p2p.prov_disc_fail = wpas_prov_disc_fail;
2976 p2p.invitation_process = wpas_invitation_process;
2977 p2p.invitation_received = wpas_invitation_received;
2978 p2p.invitation_result = wpas_invitation_result;
2979 p2p.get_noa = wpas_get_noa;
2980 p2p.go_connected = wpas_go_connected;
2982 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2983 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2984 p2p.dev_name = wpa_s->conf->device_name;
2985 p2p.manufacturer = wpa_s->conf->manufacturer;
2986 p2p.model_name = wpa_s->conf->model_name;
2987 p2p.model_number = wpa_s->conf->model_number;
2988 p2p.serial_number = wpa_s->conf->serial_number;
2990 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2991 p2p.config_methods = wpa_s->wps->config_methods;
2994 if (wpa_s->conf->p2p_listen_reg_class &&
2995 wpa_s->conf->p2p_listen_channel) {
2996 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2997 p2p.channel = wpa_s->conf->p2p_listen_channel;
3001 * Pick one of the social channels randomly as the listen
3004 os_get_random((u8 *) &r, sizeof(r));
3005 p2p.channel = 1 + (r % 3) * 5;
3007 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3009 if (wpa_s->conf->p2p_oper_reg_class &&
3010 wpa_s->conf->p2p_oper_channel) {
3011 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3012 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3013 p2p.cfg_op_channel = 1;
3014 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3015 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3018 p2p.op_reg_class = 81;
3020 * Use random operation channel from (1, 6, 11) if no other
3021 * preference is indicated.
3023 os_get_random((u8 *) &r, sizeof(r));
3024 p2p.op_channel = 1 + (r % 3) * 5;
3025 p2p.cfg_op_channel = 0;
3026 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3027 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3029 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3030 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3031 p2p.country[2] = 0x04;
3033 os_memcpy(p2p.country, "XX\x04", 3);
3035 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3036 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3041 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3044 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3045 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3046 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3048 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3049 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3051 p2p.max_peers = 100;
3053 if (wpa_s->conf->p2p_ssid_postfix) {
3054 p2p.ssid_postfix_len =
3055 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3056 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3057 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3058 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3059 p2p.ssid_postfix_len);
3062 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3064 p2p.max_listen = wpa_s->max_remain_on_chan;
3066 global->p2p = p2p_init(&p2p);
3067 if (global->p2p == NULL)
3069 global->p2p_init_wpa_s = wpa_s;
3071 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3072 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3074 p2p_add_wps_vendor_extension(
3075 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3083 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3084 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3086 * This function deinitialize per-interface P2P data.
3088 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3090 if (wpa_s->driver && wpa_s->drv_priv)
3091 wpa_drv_probe_req_report(wpa_s, 0);
3093 if (wpa_s->go_params) {
3094 /* Clear any stored provisioning info */
3095 p2p_clear_provisioning_info(
3097 wpa_s->go_params->peer_device_addr);
3100 os_free(wpa_s->go_params);
3101 wpa_s->go_params = NULL;
3102 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3103 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3104 wpa_s->p2p_long_listen = 0;
3105 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3106 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3107 wpas_p2p_remove_pending_group_interface(wpa_s);
3109 /* TODO: remove group interface from the driver if this wpa_s instance
3110 * is on top of a P2P group interface */
3115 * wpas_p2p_deinit_global - Deinitialize global P2P module
3116 * @global: Pointer to global data from wpa_supplicant_init()
3118 * This function deinitializes the global (per device) P2P module.
3120 void wpas_p2p_deinit_global(struct wpa_global *global)
3122 struct wpa_supplicant *wpa_s, *tmp;
3124 wpa_s = global->ifaces;
3126 wpas_p2p_service_flush(wpa_s);
3128 if (global->p2p == NULL)
3131 /* Remove remaining P2P group interfaces */
3132 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3133 wpa_s = wpa_s->next;
3135 tmp = global->ifaces;
3138 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3143 /* Disconnect from the P2P group and deinit the interface */
3144 wpas_p2p_disconnect(tmp);
3148 * Deinit GO data on any possibly remaining interface (if main
3149 * interface is used as GO).
3151 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3152 if (wpa_s->ap_iface)
3153 wpas_p2p_group_deinit(wpa_s);
3156 p2p_deinit(global->p2p);
3158 global->p2p_init_wpa_s = NULL;
3162 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3164 if (wpa_s->conf->p2p_no_group_iface)
3165 return 0; /* separate interface disabled per configuration */
3166 if (wpa_s->drv_flags &
3167 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3168 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3169 return 1; /* P2P group requires a new interface in every case
3171 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3172 return 0; /* driver does not support concurrent operations */
3173 if (wpa_s->global->ifaces->next)
3174 return 1; /* more that one interface already in use */
3175 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3176 return 1; /* this interface is already in use */
3181 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3182 const u8 *peer_addr,
3183 enum p2p_wps_method wps_method,
3184 int go_intent, const u8 *own_interface_addr,
3185 unsigned int force_freq, int persistent_group,
3186 struct wpa_ssid *ssid, unsigned int pref_freq)
3188 if (persistent_group && wpa_s->conf->persistent_reconnect)
3189 persistent_group = 2;
3191 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3192 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3193 go_intent, own_interface_addr,
3194 force_freq, persistent_group);
3198 * Increase GO config timeout if HT40 is used since it takes some time
3199 * to scan channels for coex purposes before the BSS can be started.
3201 p2p_set_config_timeout(wpa_s->global->p2p,
3202 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3204 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3205 go_intent, own_interface_addr, force_freq,
3206 persistent_group, ssid ? ssid->ssid : NULL,
3207 ssid ? ssid->ssid_len : 0,
3208 wpa_s->p2p_pd_before_go_neg, pref_freq);
3212 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3213 const u8 *peer_addr,
3214 enum p2p_wps_method wps_method,
3215 int go_intent, const u8 *own_interface_addr,
3216 unsigned int force_freq, int persistent_group,
3217 struct wpa_ssid *ssid, unsigned int pref_freq)
3219 if (persistent_group && wpa_s->conf->persistent_reconnect)
3220 persistent_group = 2;
3222 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3225 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3226 go_intent, own_interface_addr, force_freq,
3227 persistent_group, ssid ? ssid->ssid : NULL,
3228 ssid ? ssid->ssid_len : 0, pref_freq);
3232 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3234 wpa_s->p2p_join_scan_count++;
3235 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3236 wpa_s->p2p_join_scan_count);
3237 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3238 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3239 " for join operationg - stop join attempt",
3240 MAC2STR(wpa_s->pending_join_iface_addr));
3241 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3242 if (wpa_s->p2p_auto_pd) {
3243 wpa_s->p2p_auto_pd = 0;
3244 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3245 " p2p_dev_addr=" MACSTR " status=N/A",
3246 MAC2STR(wpa_s->pending_join_dev_addr));
3249 wpa_msg(wpa_s->parent, MSG_INFO,
3250 P2P_EVENT_GROUP_FORMATION_FAILURE);
3255 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3257 struct wpa_supplicant *iface;
3261 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3264 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3265 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3267 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3269 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3270 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3271 shared_freq = iface->current_ssid->frequency;
3272 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3273 shared_freq = iface->assoc_freq;
3277 if (shared_freq && freq != shared_freq) {
3278 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3279 "connected on %d MHz - new connection on "
3280 "%d MHz", iface->ifname, shared_freq, freq);
3285 shared_freq = wpa_drv_shared_freq(wpa_s);
3286 if (shared_freq > 0 && shared_freq != freq) {
3287 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3288 "virtual interface connected on %d MHz - new "
3289 "connection on %d MHz", shared_freq, freq);
3297 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3298 const u8 *peer_dev_addr)
3300 struct wpa_bss *bss;
3303 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3306 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3307 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3312 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3313 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3314 "%ld.%06ld (%supdated in last scan)",
3315 bss->last_update.sec, bss->last_update.usec,
3316 updated ? "": "not ");
3322 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3323 struct wpa_scan_results *scan_res)
3325 struct wpa_bss *bss;
3327 u8 iface_addr[ETH_ALEN];
3329 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3331 if (wpa_s->global->p2p_disabled)
3334 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3335 scan_res ? (int) scan_res->num : -1,
3336 wpa_s->p2p_auto_join ? "auto_" : "");
3339 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3341 if (wpa_s->p2p_auto_pd) {
3342 int join = wpas_p2p_peer_go(wpa_s,
3343 wpa_s->pending_join_dev_addr);
3345 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3346 wpa_s->auto_pd_scan_retry++;
3347 bss = wpa_bss_get_bssid_latest(
3348 wpa_s, wpa_s->pending_join_dev_addr);
3351 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3352 "the peer " MACSTR " at %d MHz",
3353 wpa_s->auto_pd_scan_retry,
3355 pending_join_dev_addr),
3357 wpas_p2p_join_scan_req(wpa_s, freq);
3365 wpa_s->p2p_auto_pd = 0;
3366 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3367 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3368 MAC2STR(wpa_s->pending_join_dev_addr), join);
3369 if (p2p_prov_disc_req(wpa_s->global->p2p,
3370 wpa_s->pending_join_dev_addr,
3371 wpa_s->pending_pd_config_methods, join,
3372 0, wpa_s->user_initiated_pd) < 0) {
3373 wpa_s->p2p_auto_pd = 0;
3374 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3375 " p2p_dev_addr=" MACSTR " status=N/A",
3376 MAC2STR(wpa_s->pending_join_dev_addr));
3381 if (wpa_s->p2p_auto_join) {
3382 int join = wpas_p2p_peer_go(wpa_s,
3383 wpa_s->pending_join_dev_addr);
3385 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3386 "running a GO -> use GO Negotiation");
3387 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3388 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3389 wpa_s->p2p_persistent_group, 0, 0, 0,
3390 wpa_s->p2p_go_intent,
3391 wpa_s->p2p_connect_freq,
3392 wpa_s->p2p_persistent_id,
3393 wpa_s->p2p_pd_before_go_neg,
3394 wpa_s->p2p_go_ht40);
3398 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3399 "try to join the group", join ? "" :
3402 wpa_s->p2p_fallback_to_go_neg = 1;
3405 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3406 wpa_s->pending_join_iface_addr);
3408 p2p_get_interface_addr(wpa_s->global->p2p,
3409 wpa_s->pending_join_dev_addr,
3411 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3413 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3414 "address for join from " MACSTR " to " MACSTR
3415 " based on newly discovered P2P peer entry",
3416 MAC2STR(wpa_s->pending_join_iface_addr),
3417 MAC2STR(iface_addr));
3418 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3421 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3422 wpa_s->pending_join_iface_addr);
3425 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3426 "from P2P peer table: %d MHz", freq);
3428 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
3431 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3432 "from BSS table: %d MHz", freq);
3437 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3438 wpa_msg(wpa_s->parent, MSG_INFO,
3439 P2P_EVENT_GROUP_FORMATION_FAILURE
3440 "reason=FREQ_CONFLICT");
3444 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3445 "prior to joining an existing group (GO " MACSTR
3447 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3448 wpa_s->pending_pd_before_join = 1;
3450 switch (wpa_s->pending_join_wps_method) {
3451 case WPS_PIN_DISPLAY:
3452 method = WPS_CONFIG_KEYPAD;
3454 case WPS_PIN_KEYPAD:
3455 method = WPS_CONFIG_DISPLAY;
3458 method = WPS_CONFIG_PUSHBUTTON;
3465 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3466 wpa_s->pending_join_dev_addr) ==
3469 * We have already performed provision discovery for
3470 * joining the group. Proceed directly to join
3471 * operation without duplicated provision discovery. */
3472 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3473 "with " MACSTR " already done - proceed to "
3475 MAC2STR(wpa_s->pending_join_dev_addr));
3476 wpa_s->pending_pd_before_join = 0;
3480 if (p2p_prov_disc_req(wpa_s->global->p2p,
3481 wpa_s->pending_join_dev_addr, method, 1,
3482 freq, wpa_s->user_initiated_pd) < 0) {
3483 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3484 "Discovery Request before joining an "
3486 wpa_s->pending_pd_before_join = 0;
3492 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3493 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3494 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3495 wpas_p2p_check_join_scan_limit(wpa_s);
3499 /* Start join operation immediately */
3500 wpas_p2p_join_start(wpa_s);
3504 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3507 struct wpa_driver_scan_params params;
3508 struct wpabuf *wps_ie, *ies;
3510 int freqs[2] = { 0, 0 };
3512 os_memset(¶ms, 0, sizeof(params));
3514 /* P2P Wildcard SSID */
3515 params.num_ssids = 1;
3516 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3517 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3519 wpa_s->wps->dev.p2p = 1;
3520 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3521 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3523 if (wps_ie == NULL) {
3524 wpas_p2p_scan_res_join(wpa_s, NULL);
3528 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3529 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3531 wpabuf_free(wps_ie);
3532 wpas_p2p_scan_res_join(wpa_s, NULL);
3535 wpabuf_put_buf(ies, wps_ie);
3536 wpabuf_free(wps_ie);
3538 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3540 params.p2p_probe = 1;
3541 params.extra_ies = wpabuf_head(ies);
3542 params.extra_ies_len = wpabuf_len(ies);
3545 params.freqs = freqs;
3549 * Run a scan to update BSS table and start Provision Discovery once
3550 * the new scan results become available.
3552 ret = wpa_drv_scan(wpa_s, ¶ms);
3554 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3559 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3561 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3562 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3563 wpas_p2p_check_join_scan_limit(wpa_s);
3568 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3570 struct wpa_supplicant *wpa_s = eloop_ctx;
3571 wpas_p2p_join_scan_req(wpa_s, 0);
3575 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3576 const u8 *dev_addr, enum p2p_wps_method wps_method,
3579 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3580 MACSTR " dev " MACSTR ")%s",
3581 MAC2STR(iface_addr), MAC2STR(dev_addr),
3582 auto_join ? " (auto_join)" : "");
3584 wpa_s->p2p_auto_pd = 0;
3585 wpa_s->p2p_auto_join = !!auto_join;
3586 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3587 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3588 wpa_s->pending_join_wps_method = wps_method;
3590 /* Make sure we are not running find during connection establishment */
3591 wpas_p2p_stop_find(wpa_s);
3593 wpa_s->p2p_join_scan_count = 0;
3594 wpas_p2p_join_scan(wpa_s, NULL);
3599 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3601 struct wpa_supplicant *group;
3602 struct p2p_go_neg_results res;
3603 struct wpa_bss *bss;
3605 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3608 if (group != wpa_s) {
3609 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3610 sizeof(group->p2p_pin));
3611 group->p2p_wps_method = wpa_s->p2p_wps_method;
3614 * Need to mark the current interface for p2p_group_formation
3615 * when a separate group interface is not used. This is needed
3616 * to allow p2p_cancel stop a pending p2p_connect-join.
3617 * wpas_p2p_init_group_interface() addresses this for the case
3618 * where a separate group interface is used.
3620 wpa_s->global->p2p_group_formation = wpa_s;
3623 group->p2p_in_provisioning = 1;
3624 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3626 os_memset(&res, 0, sizeof(res));
3627 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3629 res.wps_method = wpa_s->pending_join_wps_method;
3630 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
3632 res.freq = bss->freq;
3633 res.ssid_len = bss->ssid_len;
3634 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3637 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3638 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3640 wpa_drv_cancel_remain_on_channel(wpa_s);
3641 wpa_s->off_channel_freq = 0;
3642 wpa_s->roc_waiting_drv_freq = 0;
3644 wpas_start_wps_enrollee(group, &res);
3647 * Allow a longer timeout for join-a-running-group than normal 15
3648 * second group formation timeout since the GO may not have authorized
3649 * our connection yet.
3651 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3652 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3659 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3660 int *force_freq, int *pref_freq,
3664 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3665 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3666 "(%u MHz) is not supported for P2P uses",
3671 if (*oper_freq > 0 && freq != *oper_freq &&
3672 !(wpa_s->drv_flags &
3673 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3674 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3675 "on %u MHz while connected on another "
3676 "channel (%u MHz)", freq, *oper_freq);
3679 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3680 "requested channel (%u MHz)", freq);
3682 } else if (*oper_freq > 0 &&
3683 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3684 if (!(wpa_s->drv_flags &
3685 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3686 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3687 "while connected on non-P2P supported "
3688 "channel (%u MHz)", *oper_freq);
3691 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3692 "(%u MHz) not available for P2P - try to use "
3693 "another channel", *oper_freq);
3695 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3697 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3698 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3699 "are already using (%u MHz) on another interface",
3701 *pref_freq = *oper_freq;
3702 } else if (*oper_freq > 0) {
3703 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3704 "channel we are already using (%u MHz) on another "
3705 "interface", *oper_freq);
3706 *force_freq = *oper_freq;
3714 * wpas_p2p_connect - Request P2P Group Formation to be started
3715 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3716 * @peer_addr: Address of the peer P2P Device
3717 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3718 * @persistent_group: Whether to create a persistent group
3719 * @auto_join: Whether to select join vs. GO Negotiation automatically
3720 * @join: Whether to join an existing group (as a client) instead of starting
3721 * Group Owner negotiation; @peer_addr is BSSID in that case
3722 * @auth: Whether to only authorize the connection instead of doing that and
3723 * initiating Group Owner negotiation
3724 * @go_intent: GO Intent or -1 to use default
3725 * @freq: Frequency for the group or 0 for auto-selection
3726 * @persistent_id: Persistent group credentials to use for forcing GO
3727 * parameters or -1 to generate new values (SSID/passphrase)
3728 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3729 * interoperability workaround when initiating group formation
3730 * @ht40: Start GO with 40 MHz channel width
3731 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3732 * failure, -2 on failure due to channel not currently available,
3733 * -3 if forced channel is not supported
3735 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3736 const char *pin, enum p2p_wps_method wps_method,
3737 int persistent_group, int auto_join, int join, int auth,
3738 int go_intent, int freq, int persistent_id, int pd,
3741 int force_freq = 0, pref_freq = 0, oper_freq = 0;
3744 enum wpa_driver_if_type iftype;
3746 struct wpa_ssid *ssid = NULL;
3748 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3751 if (persistent_id >= 0) {
3752 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3753 if (ssid == NULL || ssid->disabled != 2 ||
3754 ssid->mode != WPAS_MODE_P2P_GO)
3759 go_intent = wpa_s->conf->p2p_go_intent;
3762 wpa_s->p2p_long_listen = 0;
3764 wpa_s->p2p_wps_method = wps_method;
3765 wpa_s->p2p_persistent_group = !!persistent_group;
3766 wpa_s->p2p_persistent_id = persistent_id;
3767 wpa_s->p2p_go_intent = go_intent;
3768 wpa_s->p2p_connect_freq = freq;
3769 wpa_s->p2p_fallback_to_go_neg = 0;
3770 wpa_s->p2p_pd_before_go_neg = !!pd;
3771 wpa_s->p2p_go_ht40 = !!ht40;
3774 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3775 else if (wps_method == WPS_PIN_DISPLAY) {
3776 ret = wps_generate_pin();
3777 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3779 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3782 wpa_s->p2p_pin[0] = '\0';
3784 if (join || auto_join) {
3785 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3787 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3788 "connect a running group from " MACSTR,
3789 MAC2STR(peer_addr));
3790 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3793 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3794 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3796 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3797 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3801 os_get_time(&wpa_s->p2p_auto_started);
3802 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3804 wpa_s->p2p_auto_started.sec,
3805 wpa_s->p2p_auto_started.usec);
3807 wpa_s->user_initiated_pd = 1;
3808 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3814 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3815 wpa_s->assoc_freq) {
3816 oper_freq = wpa_s->assoc_freq;
3818 oper_freq = wpa_drv_shared_freq(wpa_s);
3823 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
3827 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
3829 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3831 if (wpa_s->create_p2p_iface) {
3832 /* Prepare to add a new interface for the group */
3833 iftype = WPA_IF_P2P_GROUP;
3834 if (go_intent == 15)
3835 iftype = WPA_IF_P2P_GO;
3836 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3837 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3838 "interface for the group");
3842 if_addr = wpa_s->pending_interface_addr;
3844 if_addr = wpa_s->own_addr;
3847 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3849 force_freq, persistent_group, ssid,
3855 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3856 go_intent, if_addr, force_freq,
3857 persistent_group, ssid, pref_freq) < 0) {
3858 if (wpa_s->create_p2p_iface)
3859 wpas_p2p_remove_pending_group_interface(wpa_s);
3867 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3868 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3869 * @freq: Frequency of the channel in MHz
3870 * @duration: Duration of the stay on the channel in milliseconds
3872 * This callback is called when the driver indicates that it has started the
3873 * requested remain-on-channel duration.
3875 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3876 unsigned int freq, unsigned int duration)
3878 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3880 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3881 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3882 wpa_s->pending_listen_duration);
3883 wpa_s->pending_listen_freq = 0;
3888 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3889 unsigned int timeout)
3891 /* Limit maximum Listen state time based on driver limitation. */
3892 if (timeout > wpa_s->max_remain_on_chan)
3893 timeout = wpa_s->max_remain_on_chan;
3895 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3896 return wpa_drv_p2p_listen(wpa_s, timeout);
3898 return p2p_listen(wpa_s->global->p2p, timeout);
3903 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3904 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3905 * @freq: Frequency of the channel in MHz
3907 * This callback is called when the driver indicates that a remain-on-channel
3908 * operation has been completed, i.e., the duration on the requested channel
3911 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3914 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3915 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3916 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
3917 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3919 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3920 return; /* P2P module started a new operation */
3921 if (offchannel_pending_action_tx(wpa_s))
3923 if (wpa_s->p2p_long_listen > 0)
3924 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3925 if (wpa_s->p2p_long_listen > 0) {
3926 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3927 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3933 * wpas_p2p_group_remove - Remove a P2P group
3934 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3935 * @ifname: Network interface name of the group interface or "*" to remove all
3937 * Returns: 0 on success, -1 on failure
3939 * This function is used to remove a P2P group. This can be used to disconnect
3940 * from a group in which the local end is a P2P Client or to end a P2P Group in
3941 * case the local end is the Group Owner. If a virtual network interface was
3942 * created for this group, that interface will be removed. Otherwise, only the
3943 * configured P2P group network will be removed from the interface.
3945 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3947 struct wpa_global *global = wpa_s->global;
3949 if (os_strcmp(ifname, "*") == 0) {
3950 struct wpa_supplicant *prev;
3951 wpa_s = global->ifaces;
3954 wpa_s = wpa_s->next;
3955 wpas_p2p_disconnect(prev);
3960 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3961 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3965 return wpas_p2p_disconnect(wpa_s);
3969 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3971 if (channels == NULL)
3972 return 1; /* Assume no restrictions */
3973 return p2p_channels_includes_freq(channels, freq);
3977 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3978 struct p2p_go_neg_results *params,
3980 const struct p2p_channels *channels)
3985 os_memset(params, 0, sizeof(*params));
3986 params->role_go = 1;
3987 params->ht40 = ht40;
3989 if (!freq_included(channels, freq)) {
3990 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3994 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3995 "frequency %d MHz", freq);
3996 params->freq = freq;
3997 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3998 wpa_s->conf->p2p_oper_channel >= 1 &&
3999 wpa_s->conf->p2p_oper_channel <= 11 &&
4000 freq_included(channels,
4001 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
4002 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4003 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4004 "frequency %d MHz", params->freq);
4005 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4006 wpa_s->conf->p2p_oper_reg_class == 116 ||
4007 wpa_s->conf->p2p_oper_reg_class == 117 ||
4008 wpa_s->conf->p2p_oper_reg_class == 124 ||
4009 wpa_s->conf->p2p_oper_reg_class == 126 ||
4010 wpa_s->conf->p2p_oper_reg_class == 127) &&
4011 freq_included(channels,
4012 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
4013 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4014 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4015 "frequency %d MHz", params->freq);
4016 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4017 wpa_s->best_overall_freq > 0 &&
4018 p2p_supported_freq(wpa_s->global->p2p,
4019 wpa_s->best_overall_freq) &&
4020 freq_included(channels, wpa_s->best_overall_freq)) {
4021 params->freq = wpa_s->best_overall_freq;
4022 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4023 "channel %d MHz", params->freq);
4024 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4025 wpa_s->best_24_freq > 0 &&
4026 p2p_supported_freq(wpa_s->global->p2p,
4027 wpa_s->best_24_freq) &&
4028 freq_included(channels, wpa_s->best_24_freq)) {
4029 params->freq = wpa_s->best_24_freq;
4030 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4031 "channel %d MHz", params->freq);
4032 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4033 wpa_s->best_5_freq > 0 &&
4034 p2p_supported_freq(wpa_s->global->p2p,
4035 wpa_s->best_5_freq) &&
4036 freq_included(channels, wpa_s->best_5_freq)) {
4037 params->freq = wpa_s->best_5_freq;
4038 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4039 "channel %d MHz", params->freq);
4042 for (chan = 0; chan < 11; chan++) {
4043 params->freq = 2412 + chan * 5;
4044 if (!wpas_p2p_disallowed_freq(wpa_s->global,
4046 freq_included(channels, params->freq))
4050 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4054 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4055 "known)", params->freq);
4058 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4059 wpa_s->assoc_freq && !freq) {
4060 if (!p2p_supported_freq(wpa_s->global->p2p, wpa_s->assoc_freq)
4061 || !freq_included(channels, wpa_s->assoc_freq)) {
4062 if (wpa_s->drv_flags &
4063 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4064 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4065 "the channel we are already using "
4066 "(%u MHz) - allow multi-channel "
4067 "concurrency", wpa_s->assoc_freq);
4069 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4070 "the channel we are already using "
4071 "(%u MHz)", wpa_s->assoc_freq);
4075 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we "
4076 "are already using (%u MHz)",
4078 params->freq = wpa_s->assoc_freq;
4082 res = wpa_drv_shared_freq(wpa_s);
4083 if (res > 0 && !freq &&
4084 (!p2p_supported_freq(wpa_s->global->p2p, res) ||
4085 !freq_included(channels, res))) {
4086 if (wpa_s->drv_flags &
4087 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4088 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4089 "channel we are already using on a shared "
4090 "interface (%u MHz) - allow multi-channel "
4091 "concurrency", res);
4093 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4094 "channel we are already using on a shared "
4095 "interface (%u MHz)", res);
4098 } else if (res > 0 && !freq) {
4099 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4100 "already using on a shared interface");
4102 if (!freq_included(channels, params->freq)) {
4103 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4104 "accepted", params->freq);
4107 } else if (res > 0 && freq != res &&
4108 !(wpa_s->drv_flags &
4109 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4110 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4111 "while connected on another channel (%u MHz)",
4120 static struct wpa_supplicant *
4121 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4124 struct wpa_supplicant *group_wpa_s;
4126 if (!wpas_p2p_create_iface(wpa_s)) {
4127 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4132 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
4133 WPA_IF_P2P_CLIENT) < 0) {
4134 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
4137 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4138 if (group_wpa_s == NULL) {
4139 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4141 wpas_p2p_remove_pending_group_interface(wpa_s);
4145 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4146 group_wpa_s->ifname);
4152 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4153 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4154 * @persistent_group: Whether to create a persistent group
4155 * @freq: Frequency for the group or 0 to indicate no hardcoding
4156 * Returns: 0 on success, -1 on failure
4158 * This function creates a new P2P group with the local end as the Group Owner,
4159 * i.e., without using Group Owner Negotiation.
4161 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
4164 struct p2p_go_neg_results params;
4167 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4170 /* Make sure we are not running find during connection establishment */
4171 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
4172 wpas_p2p_stop_find_oper(wpa_s);
4175 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4177 if (wpa_s->best_24_freq > 0 &&
4178 p2p_supported_freq(wpa_s->global->p2p,
4179 wpa_s->best_24_freq)) {
4180 freq = wpa_s->best_24_freq;
4181 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4182 "channel: %d MHz", freq);
4184 os_get_random((u8 *) &r, sizeof(r));
4185 freq = 2412 + (r % 3) * 25;
4186 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4187 "channel: %d MHz", freq);
4192 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4194 if (wpa_s->best_5_freq > 0 &&
4195 p2p_supported_freq(wpa_s->global->p2p,
4196 wpa_s->best_5_freq)) {
4197 freq = wpa_s->best_5_freq;
4198 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4199 "channel: %d MHz", freq);
4201 os_get_random((u8 *) &r, sizeof(r));
4202 freq = 5180 + (r % 4) * 20;
4203 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4204 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4205 "5 GHz channel for P2P group");
4208 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4209 "channel: %d MHz", freq);
4213 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4214 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4215 "(%u MHz) is not supported for P2P uses",
4220 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, NULL))
4223 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4224 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4225 "(%u MHz) is not supported for P2P uses",
4229 p2p_go_params(wpa_s->global->p2p, ¶ms);
4230 params.persistent_group = persistent_group;
4232 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4235 wpas_start_wps_go(wpa_s, ¶ms, 0);
4241 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4242 struct wpa_ssid *params, int addr_allocated)
4244 struct wpa_ssid *ssid;
4246 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4250 wpa_supplicant_ap_deinit(wpa_s);
4252 ssid = wpa_config_add_network(wpa_s->conf);
4255 wpa_config_set_network_defaults(ssid);
4256 ssid->temporary = 1;
4257 ssid->proto = WPA_PROTO_RSN;
4258 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4259 ssid->group_cipher = WPA_CIPHER_CCMP;
4260 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4261 ssid->ssid = os_malloc(params->ssid_len);
4262 if (ssid->ssid == NULL) {
4263 wpa_config_remove_network(wpa_s->conf, ssid->id);
4266 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4267 ssid->ssid_len = params->ssid_len;
4268 ssid->p2p_group = 1;
4269 ssid->export_keys = 1;
4270 if (params->psk_set) {
4271 os_memcpy(ssid->psk, params->psk, 32);
4274 if (params->passphrase)
4275 ssid->passphrase = os_strdup(params->passphrase);
4277 wpa_supplicant_select_network(wpa_s, ssid);
4279 wpa_s->show_group_started = 1;
4285 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4286 struct wpa_ssid *ssid, int addr_allocated,
4288 const struct p2p_channels *channels)
4290 struct p2p_go_neg_results params;
4293 if (ssid->disabled != 2 || ssid->ssid == NULL)
4296 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4297 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4298 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4303 /* Make sure we are not running find during connection establishment */
4304 wpas_p2p_stop_find_oper(wpa_s);
4306 wpa_s->p2p_fallback_to_go_neg = 0;
4308 if (ssid->mode == WPAS_MODE_INFRA)
4309 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4311 if (ssid->mode != WPAS_MODE_P2P_GO)
4314 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, channels))
4318 params.psk_set = ssid->psk_set;
4320 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
4321 if (ssid->passphrase) {
4322 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4323 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4324 "persistent group");
4327 os_strlcpy(params.passphrase, ssid->passphrase,
4328 sizeof(params.passphrase));
4330 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4331 params.ssid_len = ssid->ssid_len;
4332 params.persistent_group = 1;
4334 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4338 wpas_start_wps_go(wpa_s, ¶ms, 0);
4344 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4345 struct wpabuf *proberesp_ies)
4347 struct wpa_supplicant *wpa_s = ctx;
4348 if (wpa_s->ap_iface) {
4349 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4350 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4351 wpabuf_free(beacon_ies);
4352 wpabuf_free(proberesp_ies);
4356 wpabuf_free(hapd->p2p_beacon_ie);
4357 hapd->p2p_beacon_ie = beacon_ies;
4359 wpabuf_free(hapd->p2p_probe_resp_ie);
4360 hapd->p2p_probe_resp_ie = proberesp_ies;
4362 wpabuf_free(beacon_ies);
4363 wpabuf_free(proberesp_ies);
4365 wpa_supplicant_ap_update_beacon(wpa_s);
4369 static void wpas_p2p_idle_update(void *ctx, int idle)
4371 struct wpa_supplicant *wpa_s = ctx;
4372 if (!wpa_s->ap_iface)
4374 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4376 wpas_p2p_set_group_idle_timeout(wpa_s);
4378 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4382 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4383 struct wpa_ssid *ssid)
4385 struct p2p_group *group;
4386 struct p2p_group_config *cfg;
4388 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4390 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4393 cfg = os_zalloc(sizeof(*cfg));
4397 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4398 cfg->persistent_group = 2;
4399 else if (ssid->p2p_persistent_group)
4400 cfg->persistent_group = 1;
4401 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4402 if (wpa_s->max_stations &&
4403 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4404 cfg->max_clients = wpa_s->max_stations;
4406 cfg->max_clients = wpa_s->conf->max_num_sta;
4407 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4408 cfg->ssid_len = ssid->ssid_len;
4409 cfg->cb_ctx = wpa_s;
4410 cfg->ie_update = wpas_p2p_ie_update;
4411 cfg->idle_update = wpas_p2p_idle_update;
4413 group = p2p_group_init(wpa_s->global->p2p, cfg);
4416 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4417 p2p_group_notif_formation_done(group);
4418 wpa_s->p2p_group = group;
4423 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4426 struct wpa_ssid *ssid = wpa_s->current_ssid;
4428 if (!wpa_s->p2p_in_provisioning) {
4429 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4430 "provisioning not in progress");
4434 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4435 u8 go_dev_addr[ETH_ALEN];
4436 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4437 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4439 /* Clear any stored provisioning info */
4440 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4443 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4445 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4447 * Use a separate timeout for initial data connection to
4448 * complete to allow the group to be removed automatically if
4449 * something goes wrong in this step before the P2P group idle
4450 * timeout mechanism is taken into use.
4452 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4453 wpas_p2p_group_formation_timeout,
4454 wpa_s->parent, NULL);
4456 if (wpa_s->global->p2p)
4457 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4458 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4459 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4460 wpas_group_formation_completed(wpa_s, 1);
4464 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4465 struct wps_event_fail *fail)
4467 if (!wpa_s->p2p_in_provisioning) {
4468 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4469 "provisioning not in progress");
4473 if (wpa_s->go_params) {
4474 p2p_clear_provisioning_info(
4476 wpa_s->go_params->peer_device_addr);
4479 wpas_notify_p2p_wps_failed(wpa_s, fail);
4483 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4484 const char *config_method,
4485 enum wpas_p2p_prov_disc_use use)
4489 wpa_s->p2p_fallback_to_go_neg = 0;
4490 wpa_s->pending_pd_use = NORMAL_PD;
4491 if (os_strncmp(config_method, "display", 7) == 0)
4492 config_methods = WPS_CONFIG_DISPLAY;
4493 else if (os_strncmp(config_method, "keypad", 6) == 0)
4494 config_methods = WPS_CONFIG_KEYPAD;
4495 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4496 os_strncmp(config_method, "pushbutton", 10) == 0)
4497 config_methods = WPS_CONFIG_PUSHBUTTON;
4499 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
4503 if (use == WPAS_P2P_PD_AUTO) {
4504 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4505 wpa_s->pending_pd_config_methods = config_methods;
4506 wpa_s->p2p_auto_pd = 1;
4507 wpa_s->p2p_auto_join = 0;
4508 wpa_s->pending_pd_before_join = 0;
4509 wpa_s->auto_pd_scan_retry = 0;
4510 wpas_p2p_stop_find(wpa_s);
4511 wpa_s->p2p_join_scan_count = 0;
4512 os_get_time(&wpa_s->p2p_auto_started);
4513 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4514 wpa_s->p2p_auto_started.sec,
4515 wpa_s->p2p_auto_started.usec);
4516 wpas_p2p_join_scan(wpa_s, NULL);
4520 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4521 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
4523 use == WPAS_P2P_PD_FOR_JOIN);
4526 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4529 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
4530 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4535 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4538 return p2p_scan_result_text(ies, ies_len, buf, end);
4542 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4544 if (!offchannel_pending_action_tx(wpa_s))
4547 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4548 "operation request");
4549 offchannel_clear_pending_action_tx(wpa_s);
4553 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4554 enum p2p_discovery_type type,
4555 unsigned int num_req_dev_types, const u8 *req_dev_types,
4556 const u8 *dev_id, unsigned int search_delay)
4558 wpas_p2p_clear_pending_action_tx(wpa_s);
4559 wpa_s->p2p_long_listen = 0;
4561 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4562 return wpa_drv_p2p_find(wpa_s, timeout, type);
4564 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4565 wpa_s->p2p_in_provisioning)
4568 wpa_supplicant_cancel_sched_scan(wpa_s);
4570 return p2p_find(wpa_s->global->p2p, timeout, type,
4571 num_req_dev_types, req_dev_types, dev_id,
4576 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
4578 wpas_p2p_clear_pending_action_tx(wpa_s);
4579 wpa_s->p2p_long_listen = 0;
4580 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4581 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4582 wpa_s->global->p2p_cb_on_scan_complete = 0;
4584 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4585 wpa_drv_p2p_stop_find(wpa_s);
4589 if (wpa_s->global->p2p)
4590 p2p_stop_find(wpa_s->global->p2p);
4596 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4598 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4600 wpas_p2p_remove_pending_group_interface(wpa_s);
4604 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4606 struct wpa_supplicant *wpa_s = eloop_ctx;
4607 wpa_s->p2p_long_listen = 0;
4611 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4615 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4618 wpa_supplicant_cancel_sched_scan(wpa_s);
4619 wpas_p2p_clear_pending_action_tx(wpa_s);
4623 * This is a request for unlimited Listen state. However, at
4624 * least for now, this is mapped to a Listen state for one
4629 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4630 wpa_s->p2p_long_listen = 0;
4633 * Stop previous find/listen operation to avoid trying to request a new
4634 * remain-on-channel operation while the driver is still running the
4637 if (wpa_s->global->p2p)
4638 p2p_stop_find(wpa_s->global->p2p);
4640 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4641 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4642 wpa_s->p2p_long_listen = timeout * 1000;
4643 eloop_register_timeout(timeout, 0,
4644 wpas_p2p_long_listen_timeout,
4652 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4653 u8 *buf, size_t len, int p2p_group)
4655 struct wpabuf *p2p_ie;
4658 if (wpa_s->global->p2p_disabled)
4660 if (wpa_s->global->p2p == NULL)
4665 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4666 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4668 wpabuf_free(p2p_ie);
4674 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4675 const u8 *dst, const u8 *bssid,
4676 const u8 *ie, size_t ie_len, int ssi_signal)
4678 if (wpa_s->global->p2p_disabled)
4680 if (wpa_s->global->p2p == NULL)
4683 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4685 case P2P_PREQ_NOT_P2P:
4686 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4689 case P2P_PREQ_MALFORMED:
4690 case P2P_PREQ_NOT_LISTEN:
4691 case P2P_PREQ_NOT_PROCESSED:
4692 default: /* make gcc happy */
4694 case P2P_PREQ_PROCESSED:
4700 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4701 const u8 *sa, const u8 *bssid,
4702 u8 category, const u8 *data, size_t len, int freq)
4704 if (wpa_s->global->p2p_disabled)
4706 if (wpa_s->global->p2p == NULL)
4709 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4714 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4716 if (wpa_s->global->p2p_disabled)
4718 if (wpa_s->global->p2p == NULL)
4721 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4725 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4727 p2p_group_deinit(wpa_s->p2p_group);
4728 wpa_s->p2p_group = NULL;
4730 wpa_s->ap_configured_cb = NULL;
4731 wpa_s->ap_configured_cb_ctx = NULL;
4732 wpa_s->ap_configured_cb_data = NULL;
4733 wpa_s->connect_without_scan = NULL;
4737 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4739 wpa_s->p2p_long_listen = 0;
4741 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4742 return wpa_drv_p2p_reject(wpa_s, addr);
4744 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4747 return p2p_reject(wpa_s->global->p2p, addr);
4751 /* Invite to reinvoke a persistent group */
4752 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4753 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4754 int ht40, int pref_freq)
4756 enum p2p_invite_role role;
4757 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
4758 int force_freq = 0, oper_freq = 0;
4761 wpa_s->global->p2p_invite_group = NULL;
4763 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4765 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4767 wpa_s->p2p_persistent_go_freq = freq;
4768 wpa_s->p2p_go_ht40 = !!ht40;
4769 if (ssid->mode == WPAS_MODE_P2P_GO) {
4770 role = P2P_INVITE_ROLE_GO;
4771 if (peer_addr == NULL) {
4772 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4773 "address in invitation command");
4776 if (wpas_p2p_create_iface(wpa_s)) {
4777 if (wpas_p2p_add_group_interface(wpa_s,
4778 WPA_IF_P2P_GO) < 0) {
4779 wpa_printf(MSG_ERROR, "P2P: Failed to "
4780 "allocate a new interface for the "
4784 bssid = wpa_s->pending_interface_addr;
4786 bssid = wpa_s->own_addr;
4788 role = P2P_INVITE_ROLE_CLIENT;
4789 peer_addr = ssid->bssid;
4791 wpa_s->pending_invite_ssid_id = ssid->id;
4793 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4794 wpa_s->assoc_freq) {
4795 oper_freq = wpa_s->assoc_freq;
4799 oper_freq = wpa_drv_shared_freq(wpa_s);
4804 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4809 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4810 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4811 ssid->ssid, ssid->ssid_len,
4814 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4817 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4818 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4823 /* Invite to join an active group */
4824 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4825 const u8 *peer_addr, const u8 *go_dev_addr)
4827 struct wpa_global *global = wpa_s->global;
4828 enum p2p_invite_role role;
4829 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
4830 struct wpa_ssid *ssid;
4832 int force_freq = 0, oper_freq = 0, pref_freq = 0;
4835 wpa_s->p2p_persistent_go_freq = 0;
4836 wpa_s->p2p_go_ht40 = 0;
4838 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4839 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4842 if (wpa_s == NULL) {
4843 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4847 ssid = wpa_s->current_ssid;
4849 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4854 wpa_s->global->p2p_invite_group = wpa_s;
4855 persistent = ssid->p2p_persistent_group &&
4856 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4857 ssid->ssid, ssid->ssid_len);
4859 if (ssid->mode == WPAS_MODE_P2P_GO) {
4860 role = P2P_INVITE_ROLE_ACTIVE_GO;
4861 bssid = wpa_s->own_addr;
4862 if (go_dev_addr == NULL)
4863 go_dev_addr = wpa_s->global->p2p_dev_addr;
4865 role = P2P_INVITE_ROLE_CLIENT;
4866 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4867 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4868 "invite to current group");
4871 bssid = wpa_s->bssid;
4872 if (go_dev_addr == NULL &&
4873 !is_zero_ether_addr(wpa_s->go_dev_addr))
4874 go_dev_addr = wpa_s->go_dev_addr;
4876 wpa_s->parent->pending_invite_ssid_id = -1;
4878 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4879 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4880 ssid->ssid, ssid->ssid_len,
4881 go_dev_addr, persistent);
4883 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4886 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4887 wpa_s->assoc_freq) {
4888 oper_freq = wpa_s->assoc_freq;
4892 oper_freq = wpa_drv_shared_freq(wpa_s);
4897 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
4901 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
4903 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4904 ssid->ssid, ssid->ssid_len, force_freq,
4905 go_dev_addr, persistent, pref_freq);
4909 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4911 struct wpa_ssid *ssid = wpa_s->current_ssid;
4912 const char *ssid_txt;
4913 u8 go_dev_addr[ETH_ALEN];
4914 int network_id = -1;
4918 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4919 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4920 wpa_s->parent, NULL);
4923 if (!wpa_s->show_group_started || !ssid)
4926 wpa_s->show_group_started = 0;
4928 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4929 os_memset(go_dev_addr, 0, ETH_ALEN);
4930 if (ssid->bssid_set)
4931 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4932 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4934 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4936 if (wpa_s->global->p2p_group_formation == wpa_s)
4937 wpa_s->global->p2p_group_formation = NULL;
4939 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4940 (int) wpa_s->assoc_freq;
4941 if (ssid->passphrase == NULL && ssid->psk_set) {
4943 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4944 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4945 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4947 wpa_s->ifname, ssid_txt, freq, psk,
4948 MAC2STR(go_dev_addr),
4949 persistent ? " [PERSISTENT]" : "");
4951 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4952 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4953 "go_dev_addr=" MACSTR "%s",
4954 wpa_s->ifname, ssid_txt, freq,
4955 ssid->passphrase ? ssid->passphrase : "",
4956 MAC2STR(go_dev_addr),
4957 persistent ? " [PERSISTENT]" : "");
4961 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4964 network_id = ssid->id;
4965 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4968 wpas_p2p_continue_after_scan(wpa_s);
4972 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4973 u32 interval1, u32 duration2, u32 interval2)
4975 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4977 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4980 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4981 wpa_s->current_ssid == NULL ||
4982 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4985 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4986 wpa_s->own_addr, wpa_s->assoc_freq,
4987 duration1, interval1, duration2, interval2);
4991 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4992 unsigned int interval)
4994 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4997 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5000 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5004 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5006 if (wpa_s->current_ssid == NULL) {
5008 * current_ssid can be cleared when P2P client interface gets
5009 * disconnected, so assume this interface was used as P2P
5014 return wpa_s->current_ssid->p2p_group &&
5015 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5019 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5021 struct wpa_supplicant *wpa_s = eloop_ctx;
5023 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
5024 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5029 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5031 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
5035 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5039 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5040 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5042 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5045 timeout = wpa_s->conf->p2p_group_idle;
5046 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5047 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5048 timeout = P2P_MAX_CLIENT_IDLE;
5054 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5055 timeout = 0; /* special client mode no-timeout */
5060 if (wpa_s->p2p_in_provisioning) {
5062 * Use the normal group formation timeout during the
5063 * provisioning phase to avoid terminating this process too
5064 * early due to group idle timeout.
5066 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5067 "during provisioning");
5071 if (wpa_s->show_group_started) {
5073 * Use the normal group formation timeout between the end of
5074 * the provisioning phase and completion of 4-way handshake to
5075 * avoid terminating this process too early due to group idle
5078 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5079 "while waiting for initial 4-way handshake to "
5084 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
5086 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5091 /* Returns 1 if the interface was removed */
5092 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5093 u16 reason_code, const u8 *ie, size_t ie_len,
5094 int locally_generated)
5096 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5098 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5101 if (!locally_generated)
5102 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5105 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5106 wpa_s->current_ssid &&
5107 wpa_s->current_ssid->p2p_group &&
5108 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5109 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5110 "session is ending");
5111 if (wpas_p2p_group_delete(wpa_s,
5112 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5121 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5122 u16 reason_code, const u8 *ie, size_t ie_len,
5123 int locally_generated)
5125 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5127 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5130 if (!locally_generated)
5131 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5136 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5138 struct p2p_data *p2p = wpa_s->global->p2p;
5143 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5146 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5147 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5149 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5150 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5153 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5154 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5156 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5157 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5159 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5160 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5161 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5162 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5163 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5166 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5167 p2p_set_sec_dev_types(p2p,
5168 (void *) wpa_s->conf->sec_device_type,
5169 wpa_s->conf->num_sec_device_types);
5171 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5173 p2p_remove_wps_vendor_extensions(p2p);
5174 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5175 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5177 p2p_add_wps_vendor_extension(
5178 p2p, wpa_s->conf->wps_vendor_ext[i]);
5182 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5183 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5185 country[0] = wpa_s->conf->country[0];
5186 country[1] = wpa_s->conf->country[1];
5188 p2p_set_country(p2p, country);
5191 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5192 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5193 wpa_s->conf->p2p_ssid_postfix ?
5194 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5198 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5199 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
5201 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5202 u8 reg_class, channel;
5205 if (wpa_s->conf->p2p_listen_reg_class &&
5206 wpa_s->conf->p2p_listen_channel) {
5207 reg_class = wpa_s->conf->p2p_listen_reg_class;
5208 channel = wpa_s->conf->p2p_listen_channel;
5212 * Pick one of the social channels randomly as the
5215 os_get_random((u8 *) &r, sizeof(r));
5216 channel = 1 + (r % 3) * 5;
5218 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5220 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5223 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5224 u8 op_reg_class, op_channel, cfg_op_channel;
5227 if (wpa_s->conf->p2p_oper_reg_class &&
5228 wpa_s->conf->p2p_oper_channel) {
5229 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5230 op_channel = wpa_s->conf->p2p_oper_channel;
5235 * Use random operation channel from (1, 6, 11)
5236 *if no other preference is indicated.
5238 os_get_random((u8 *) &r, sizeof(r));
5239 op_channel = 1 + (r % 3) * 5;
5242 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5245 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5249 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5250 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5251 wpa_s->conf->p2p_pref_chan) < 0) {
5252 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5259 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5262 if (!wpa_s->ap_iface)
5264 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5269 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5271 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5273 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5276 wpa_s->global->cross_connection = enabled;
5277 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5280 struct wpa_supplicant *iface;
5282 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5284 if (iface->cross_connect_enabled == 0)
5287 iface->cross_connect_enabled = 0;
5288 iface->cross_connect_in_use = 0;
5289 wpa_msg(iface->parent, MSG_INFO,
5290 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5291 iface->ifname, iface->cross_connect_uplink);
5299 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5301 struct wpa_supplicant *iface;
5303 if (!uplink->global->cross_connection)
5306 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5307 if (!iface->cross_connect_enabled)
5309 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5312 if (iface->ap_iface == NULL)
5314 if (iface->cross_connect_in_use)
5317 iface->cross_connect_in_use = 1;
5318 wpa_msg(iface->parent, MSG_INFO,
5319 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5320 iface->ifname, iface->cross_connect_uplink);
5325 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5327 struct wpa_supplicant *iface;
5329 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5330 if (!iface->cross_connect_enabled)
5332 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5335 if (!iface->cross_connect_in_use)
5338 wpa_msg(iface->parent, MSG_INFO,
5339 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5340 iface->ifname, iface->cross_connect_uplink);
5341 iface->cross_connect_in_use = 0;
5346 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5348 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5349 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5350 wpa_s->cross_connect_disallowed)
5351 wpas_p2p_disable_cross_connect(wpa_s);
5353 wpas_p2p_enable_cross_connect(wpa_s);
5354 if (!wpa_s->ap_iface &&
5355 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5356 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5360 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5362 wpas_p2p_disable_cross_connect(wpa_s);
5363 if (!wpa_s->ap_iface &&
5364 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5366 wpas_p2p_set_group_idle_timeout(wpa_s);
5370 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5372 struct wpa_supplicant *iface;
5374 if (!wpa_s->global->cross_connection)
5377 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5380 if (iface->drv_flags &
5381 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5383 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5386 wpa_s->cross_connect_enabled = 1;
5387 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5388 sizeof(wpa_s->cross_connect_uplink));
5389 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5390 "%s to %s whenever uplink is available",
5391 wpa_s->ifname, wpa_s->cross_connect_uplink);
5393 if (iface->ap_iface || iface->current_ssid == NULL ||
5394 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5395 iface->cross_connect_disallowed ||
5396 iface->wpa_state != WPA_COMPLETED)
5399 wpa_s->cross_connect_in_use = 1;
5400 wpa_msg(wpa_s->parent, MSG_INFO,
5401 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5402 wpa_s->ifname, wpa_s->cross_connect_uplink);
5408 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5410 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5411 !wpa_s->p2p_in_provisioning)
5412 return 0; /* not P2P client operation */
5414 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5416 if (wpa_s != wpa_s->parent)
5417 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5419 if (wpa_s->global->p2p)
5420 p2p_group_formation_failed(wpa_s->global->p2p);
5422 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5423 wpa_s->parent, NULL);
5425 wpas_group_formation_completed(wpa_s, 0);
5430 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5432 struct p2p_channels chan;
5434 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5437 os_memset(&chan, 0, sizeof(chan));
5438 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5439 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5444 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5448 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5449 struct wpa_scan_results *scan_res)
5451 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5455 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5457 struct wpa_global *global = wpa_s->global;
5461 if (global->p2p == NULL)
5464 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5466 if (wpa_s->pending_interface_name[0] &&
5467 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5470 peer = p2p_get_go_neg_peer(global->p2p);
5472 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5473 MACSTR, MAC2STR(peer));
5474 p2p_unauthorize(global->p2p, peer);
5478 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5479 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5480 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5484 if (wpa_s->pending_pd_before_join) {
5485 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5486 wpa_s->pending_pd_before_join = 0;
5490 wpas_p2p_stop_find(wpa_s);
5492 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5493 if (wpa_s == global->p2p_group_formation &&
5494 (wpa_s->p2p_in_provisioning ||
5495 wpa_s->parent->pending_interface_type ==
5496 WPA_IF_P2P_CLIENT)) {
5497 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5498 "formation found - cancelling",
5501 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5502 wpa_s->parent, NULL);
5503 if (wpa_s->p2p_in_provisioning) {
5504 wpas_group_formation_completed(wpa_s, 0);
5507 wpas_p2p_group_delete(wpa_s,
5508 P2P_GROUP_REMOVAL_REQUESTED);
5514 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5522 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5524 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5527 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5528 "being available anymore");
5529 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
5533 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5534 int freq_24, int freq_5, int freq_overall)
5536 struct p2p_data *p2p = wpa_s->global->p2p;
5537 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5539 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5543 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5546 struct p2p_data *p2p = wpa_s->global->p2p;
5548 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5551 if (hwaddr_aton(addr, peer))
5554 return p2p_unauthorize(p2p, peer);
5559 * wpas_p2p_disconnect - Disconnect from a P2P Group
5560 * @wpa_s: Pointer to wpa_supplicant data
5561 * Returns: 0 on success, -1 on failure
5563 * This can be used to disconnect from a group in which the local end is a P2P
5564 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5565 * virtual network interface was created for this group, that interface will be
5566 * removed. Otherwise, only the configured P2P group network will be removed
5567 * from the interface.
5569 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5575 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5580 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5584 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5587 ret = p2p_in_progress(wpa_s->global->p2p);
5590 * Check whether there is an ongoing WPS provisioning step (or
5591 * other parts of group formation) on another interface since
5592 * p2p_in_progress() does not report this to avoid issues for
5593 * scans during such provisioning step.
5595 if (wpa_s->global->p2p_group_formation &&
5596 wpa_s->global->p2p_group_formation != wpa_s) {
5597 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5598 "in group formation",
5599 wpa_s->global->p2p_group_formation->ifname);
5608 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5609 struct wpa_ssid *ssid)
5611 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5612 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5613 wpa_s->parent, NULL) > 0) {
5615 * Remove the network by scheduling the group formation
5616 * timeout to happen immediately. The teardown code
5617 * needs to be scheduled to run asynch later so that we
5618 * don't delete data from under ourselves unexpectedly.
5619 * Calling wpas_p2p_group_formation_timeout directly
5620 * causes a series of crashes in WPS failure scenarios.
5622 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5623 "P2P group network getting removed");
5624 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5625 wpa_s->parent, NULL);
5630 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
5631 const u8 *addr, const u8 *ssid,
5637 for (s = wpa_s->conf->ssid; s; s = s->next) {
5638 if (s->disabled != 2)
5641 (ssid_len != s->ssid_len ||
5642 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5644 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5645 return s; /* peer is GO in the persistent group */
5646 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5648 for (i = 0; i < s->num_p2p_clients; i++) {
5649 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5650 addr, ETH_ALEN) == 0)
5651 return s; /* peer is P2P client in persistent
5660 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5665 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5669 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5672 struct wpa_supplicant *group = wpa_s;
5673 if (wpa_s->global->p2p_group_formation)
5674 group = wpa_s->global->p2p_group_formation;
5675 wpa_s = wpa_s->parent;
5676 offchannel_send_action_done(wpa_s);
5678 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
5679 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5680 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5681 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5682 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5683 wpa_s->p2p_persistent_id,
5684 wpa_s->p2p_pd_before_go_neg,
5685 wpa_s->p2p_go_ht40);
5689 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5691 if (!wpa_s->p2p_fallback_to_go_neg ||
5692 wpa_s->p2p_in_provisioning <= 5)
5695 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5696 return 0; /* peer operating as a GO */
5698 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5699 "fallback to GO Negotiation");
5700 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5706 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5708 const char *rn, *rn2;
5709 struct wpa_supplicant *ifs;
5711 if (wpa_s->wpa_state > WPA_SCANNING) {
5712 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5713 "concurrent operation",
5714 P2P_CONCURRENT_SEARCH_DELAY);
5715 return P2P_CONCURRENT_SEARCH_DELAY;
5718 if (!wpa_s->driver->get_radio_name)
5720 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5721 if (rn == NULL || rn[0] == '\0')
5724 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5725 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5728 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5729 if (!rn2 || os_strcmp(rn, rn2) != 0)
5731 if (ifs->wpa_state > WPA_SCANNING) {
5732 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5733 "delay due to concurrent operation on "
5735 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5736 return P2P_CONCURRENT_SEARCH_DELAY;
5744 void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5746 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5747 "pending anymore (sta_scan_pending=%d "
5748 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5749 wpa_s->global->p2p_cb_on_scan_complete);
5750 wpa_s->sta_scan_pending = 0;
5752 if (!wpa_s->global->p2p_cb_on_scan_complete)
5754 wpa_s->global->p2p_cb_on_scan_complete = 0;
5756 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5759 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
5760 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
5761 "continued after successful connection");
5762 p2p_increase_search_delay(wpa_s->global->p2p,
5763 wpas_p2p_search_delay(wpa_s));