3 * Copyright (c) 2009-2010, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "common/ieee802_11_common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/wpa_ctrl.h"
22 #include "wps/wps_i.h"
24 #include "ap/hostapd.h"
25 #include "ap/p2p_hostapd.h"
26 #include "eapol_supp/eapol_supp_sm.h"
27 #include "rsn_supp/wpa.h"
28 #include "wpa_supplicant_i.h"
31 #include "config_ssid.h"
37 #include "wps_supplicant.h"
38 #include "p2p_supplicant.h"
42 * How many times to try to scan to find the GO before giving up on join
45 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
48 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
49 static struct wpa_supplicant *
50 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
52 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
53 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
54 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
55 const u8 *dev_addr, enum p2p_wps_method wps_method);
56 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
57 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
58 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
59 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
62 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
63 struct wpa_scan_results *scan_res)
67 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
70 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
73 for (i = 0; i < scan_res->num; i++) {
74 struct wpa_scan_res *bss = scan_res->res[i];
75 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
76 bss->freq, bss->level,
77 (const u8 *) (bss + 1),
82 p2p_scan_res_handled(wpa_s->global->p2p);
86 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
87 unsigned int num_req_dev_types,
88 const u8 *req_dev_types)
90 struct wpa_supplicant *wpa_s = ctx;
91 struct wpa_driver_scan_params params;
93 struct wpabuf *wps_ie, *ies;
94 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
96 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
99 os_memset(¶ms, 0, sizeof(params));
101 /* P2P Wildcard SSID */
102 params.num_ssids = 1;
103 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
104 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
106 wpa_s->wps->dev.p2p = 1;
107 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
109 num_req_dev_types, req_dev_types);
113 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
118 wpabuf_put_buf(ies, wps_ie);
121 p2p_scan_ie(wpa_s->global->p2p, ies);
123 params.p2p_probe = 1;
124 params.extra_ies = wpabuf_head(ies);
125 params.extra_ies_len = wpabuf_len(ies);
128 case P2P_SCAN_SOCIAL:
129 params.freqs = social_channels;
133 case P2P_SCAN_SPECIFIC:
134 social_channels[0] = freq;
135 social_channels[1] = 0;
136 params.freqs = social_channels;
138 case P2P_SCAN_SOCIAL_PLUS_ONE:
139 social_channels[3] = freq;
140 params.freqs = social_channels;
144 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
145 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
146 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
148 ret = wpa_drv_scan(wpa_s, ¶ms);
153 wpa_s->scan_res_handler = NULL;
159 #ifdef CONFIG_CLIENT_MLME
160 static void p2p_rx_action_mlme(void *ctx, const u8 *buf, size_t len, int freq)
162 struct wpa_supplicant *wpa_s = ctx;
163 const struct ieee80211_mgmt *mgmt;
166 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
168 mgmt = (const struct ieee80211_mgmt *) buf;
169 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
172 p2p_rx_action(wpa_s->global->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
173 mgmt->u.action.category,
174 &mgmt->u.action.u.vs_public_action.action,
175 len - hdr_len, freq);
177 #endif /* CONFIG_CLIENT_MLME */
180 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
182 switch (p2p_group_interface) {
183 case P2P_GROUP_INTERFACE_PENDING:
184 return WPA_IF_P2P_GROUP;
185 case P2P_GROUP_INTERFACE_GO:
186 return WPA_IF_P2P_GO;
187 case P2P_GROUP_INTERFACE_CLIENT:
188 return WPA_IF_P2P_CLIENT;
191 return WPA_IF_P2P_GROUP;
195 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
197 size_t ssid_len, int *go)
201 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
202 for (s = wpa_s->conf->ssid; s; s = s->next) {
203 if (s->disabled != 0 || !s->p2p_group ||
204 s->ssid_len != ssid_len ||
205 os_memcmp(ssid, s->ssid, ssid_len) != 0)
207 if (s->mode == WPAS_MODE_P2P_GO &&
208 s != wpa_s->current_ssid)
211 *go = s->mode == WPAS_MODE_P2P_GO;
220 static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
222 struct wpa_ssid *ssid;
226 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
228 ssid = wpa_s->current_ssid;
231 * The current SSID was not known, but there may still be a
232 * pending P2P group interface waiting for provisioning.
234 ssid = wpa_s->conf->ssid;
236 if (ssid->p2p_group &&
237 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
238 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
243 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
245 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
246 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
247 wpa_s->reassociate = 0;
248 wpa_s->disconnected = 1;
249 wpa_supplicant_deauthenticate(wpa_s,
250 WLAN_REASON_DEAUTH_LEAVING);
254 if (wpa_s->cross_connect_in_use) {
255 wpa_s->cross_connect_in_use = 0;
256 wpa_msg(wpa_s->parent, MSG_INFO,
257 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
258 wpa_s->ifname, wpa_s->cross_connect_uplink);
260 switch (wpa_s->removal_reason) {
261 case P2P_GROUP_REMOVAL_REQUESTED:
262 reason = " reason=REQUESTED";
264 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
265 reason = " reason=IDLE";
267 case P2P_GROUP_REMOVAL_UNAVAILABLE:
268 reason = " reason=UNAVAILABLE";
274 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
275 wpa_s->ifname, gtype, reason);
278 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
280 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
281 struct wpa_global *global;
283 enum wpa_driver_if_type type;
284 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
286 global = wpa_s->global;
287 ifname = os_strdup(wpa_s->ifname);
288 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
289 wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
290 wpa_s = global->ifaces;
292 wpa_drv_if_remove(wpa_s, type, ifname);
297 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
298 if (ssid && (ssid->p2p_group ||
299 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
300 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
302 if (ssid == wpa_s->current_ssid) {
303 wpa_sm_set_config(wpa_s->wpa, NULL);
304 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
305 wpa_s->current_ssid = NULL;
308 * Networks objects created during any P2P activities are not
309 * exposed out as they might/will confuse certain non-P2P aware
310 * applications since these network objects won't behave like
313 * Likewise, we don't send out network removed signals for such
316 wpa_config_remove_network(wpa_s->conf, id);
317 wpa_supplicant_clear_status(wpa_s);
319 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
322 wpa_supplicant_ap_deinit(wpa_s);
326 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
328 const u8 *ssid, size_t ssid_len)
336 if (wpa_s->go_params)
337 bssid = wpa_s->go_params->peer_interface_addr;
339 bssid = wpa_s->bssid;
341 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
343 u8 iface_addr[ETH_ALEN];
344 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
346 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
349 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
350 "group is persistent - BSS " MACSTR " not found",
355 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
357 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
358 "group is persistent - BSS " MACSTR
359 " did not include P2P IE", MAC2STR(bssid));
360 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
361 (u8 *) (bss + 1), bss->ie_len);
362 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
363 ((u8 *) bss + 1) + bss->ie_len,
368 group_capab = p2p_get_group_capab(p2p);
369 addr = p2p_get_go_dev_addr(p2p);
370 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
371 "group_capab=0x%x", group_capab);
373 os_memcpy(go_dev_addr, addr, ETH_ALEN);
374 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
377 os_memset(go_dev_addr, 0, ETH_ALEN);
380 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
381 "go_dev_addr=" MACSTR,
382 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
384 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
388 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
389 struct wpa_ssid *ssid,
390 const u8 *go_dev_addr)
395 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
396 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
397 for (s = wpa_s->conf->ssid; s; s = s->next) {
398 if (s->disabled == 2 &&
399 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
400 s->ssid_len == ssid->ssid_len &&
401 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
406 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
408 if (ssid->passphrase && !s->passphrase)
410 else if (ssid->passphrase && s->passphrase &&
411 os_strcmp(ssid->passphrase, s->passphrase) != 0)
414 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
417 s = wpa_config_add_network(wpa_s->conf);
422 * Instead of network_added we emit persistent_group_added
423 * notification. Also to keep the defense checks in
424 * persistent_group obj registration method, we set the
425 * relevant flags in s to designate it as a persistent group.
428 s->p2p_persistent_group = 1;
429 wpas_notify_persistent_group_added(wpa_s, s);
430 wpa_config_set_network_defaults(s);
434 s->p2p_persistent_group = 1;
437 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
438 s->mode = ssid->mode;
439 s->auth_alg = WPA_AUTH_ALG_OPEN;
440 s->key_mgmt = WPA_KEY_MGMT_PSK;
441 s->proto = WPA_PROTO_RSN;
442 s->pairwise_cipher = WPA_CIPHER_CCMP;
444 if (ssid->passphrase) {
445 os_free(s->passphrase);
446 s->passphrase = os_strdup(ssid->passphrase);
450 os_memcpy(s->psk, ssid->psk, 32);
452 if (s->passphrase && !s->psk_set)
453 wpa_config_update_psk(s);
454 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
456 s->ssid = os_malloc(ssid->ssid_len);
459 s->ssid_len = ssid->ssid_len;
460 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
463 #ifndef CONFIG_NO_CONFIG_WRITE
464 if (changed && wpa_s->conf->update_config &&
465 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
466 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
468 #endif /* CONFIG_NO_CONFIG_WRITE */
474 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
477 struct wpa_ssid *ssid;
478 const char *ssid_txt;
481 u8 go_dev_addr[ETH_ALEN];
485 * This callback is likely called for the main interface. Update wpa_s
486 * to use the group interface if a new interface was created for the
489 if (wpa_s->global->p2p_group_formation)
490 wpa_s = wpa_s->global->p2p_group_formation;
491 wpa_s->global->p2p_group_formation = NULL;
492 wpa_s->p2p_in_provisioning = 0;
495 wpa_msg(wpa_s->parent, MSG_INFO,
496 P2P_EVENT_GROUP_FORMATION_FAILURE);
497 wpas_p2p_group_delete(wpa_s);
501 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
503 ssid = wpa_s->current_ssid;
504 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
505 ssid->mode = WPAS_MODE_P2P_GO;
506 p2p_group_notif_formation_done(wpa_s->p2p_group);
507 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
512 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
513 client = ssid->mode == WPAS_MODE_INFRA;
514 if (ssid->mode == WPAS_MODE_P2P_GO) {
515 persistent = ssid->p2p_persistent_group;
516 os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
519 persistent = wpas_p2p_persistent_group(wpa_s,
525 client = wpa_s->p2p_group_interface ==
526 P2P_GROUP_INTERFACE_CLIENT;
527 os_memset(go_dev_addr, 0, ETH_ALEN);
530 wpa_s->show_group_started = 0;
533 * Indicate event only after successfully completed 4-way
534 * handshake, i.e., when the interface is ready for data
537 wpa_s->show_group_started = 1;
538 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
540 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
541 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
542 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
544 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
545 MAC2STR(go_dev_addr),
546 persistent ? " [PERSISTENT]" : "");
547 wpas_p2p_cross_connect_setup(wpa_s);
548 wpas_p2p_set_group_idle_timeout(wpa_s);
550 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
551 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
552 "go_dev_addr=" MACSTR "%s",
553 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
554 ssid && ssid->passphrase ? ssid->passphrase : "",
555 MAC2STR(go_dev_addr),
556 persistent ? " [PERSISTENT]" : "");
557 wpas_p2p_cross_connect_setup(wpa_s);
558 wpas_p2p_set_group_idle_timeout(wpa_s);
562 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
564 if (network_id < 0 && ssid)
565 network_id = ssid->id;
567 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
571 static struct wpa_supplicant *
572 wpas_get_tx_interface(struct wpa_supplicant *wpa_s, const u8 *src)
574 struct wpa_supplicant *iface;
576 if (os_memcmp(src, wpa_s->own_addr, ETH_ALEN) == 0)
580 * Try to find a group interface that matches with the source address.
582 iface = wpa_s->global->ifaces;
584 if (os_memcmp(wpa_s->pending_action_src,
585 iface->own_addr, ETH_ALEN) == 0)
590 wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
591 "instead of interface %s for Action TX",
592 iface->ifname, wpa_s->ifname);
600 static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
602 struct wpa_supplicant *wpa_s = eloop_ctx;
603 struct wpa_supplicant *iface;
607 without_roc = wpa_s->pending_action_without_roc;
608 wpa_s->pending_action_without_roc = 0;
609 wpa_printf(MSG_DEBUG, "P2P: Send Action callback (without_roc=%d "
610 "pending_action_tx=%p)",
611 without_roc, wpa_s->pending_action_tx);
613 if (wpa_s->pending_action_tx == NULL)
617 * This call is likely going to be on the P2P device instance if the
618 * driver uses a separate interface for that purpose. However, some
619 * Action frames are actually sent within a P2P Group and when that is
620 * the case, we need to follow power saving (e.g., GO buffering the
621 * frame for a client in PS mode or a client following the advertised
622 * NoA from its GO). To make that easier for the driver, select the
623 * correct group interface here.
625 iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
627 if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
628 wpa_s->pending_action_freq != 0 &&
629 wpa_s->pending_action_freq != iface->assoc_freq) {
630 wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
631 "waiting for another freq=%u (off_channel_freq=%u "
633 wpa_s->pending_action_freq,
634 wpa_s->off_channel_freq,
636 if (without_roc && wpa_s->off_channel_freq == 0) {
638 * We may get here if wpas_send_action() found us to be
639 * on the correct channel, but remain-on-channel cancel
640 * event was received before getting here.
642 wpa_printf(MSG_DEBUG, "P2P: Schedule "
643 "remain-on-channel to send Action frame");
644 if (wpa_drv_remain_on_channel(
645 wpa_s, wpa_s->pending_action_freq, 200) <
647 wpa_printf(MSG_DEBUG, "P2P: Failed to request "
648 "driver to remain on channel (%u "
649 "MHz) for Action Frame TX",
650 wpa_s->pending_action_freq);
652 wpa_s->off_channel_freq = 0;
653 wpa_s->roc_waiting_drv_freq =
654 wpa_s->pending_action_freq;
660 wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
661 MACSTR " using interface %s",
662 MAC2STR(wpa_s->pending_action_dst), iface->ifname);
663 res = wpa_drv_send_action(iface, wpa_s->pending_action_freq, 0,
664 wpa_s->pending_action_dst,
665 wpa_s->pending_action_src,
666 wpa_s->pending_action_bssid,
667 wpabuf_head(wpa_s->pending_action_tx),
668 wpabuf_len(wpa_s->pending_action_tx));
670 wpa_printf(MSG_DEBUG, "P2P: Failed to send the pending "
673 * Use fake TX status event to allow P2P state machine to
676 wpas_send_action_tx_status(
677 wpa_s, wpa_s->pending_action_dst,
678 wpabuf_head(wpa_s->pending_action_tx),
679 wpabuf_len(wpa_s->pending_action_tx),
680 P2P_SEND_ACTION_FAILED);
685 void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
686 const u8 *data, size_t data_len,
687 enum p2p_send_action_result result)
689 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
691 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
694 if (wpa_s->pending_action_tx == NULL) {
695 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - no "
696 "pending operation");
700 if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
701 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - unknown "
702 "destination address");
706 wpabuf_free(wpa_s->pending_action_tx);
707 wpa_s->pending_action_tx = NULL;
709 p2p_send_action_cb(wpa_s->global->p2p, wpa_s->pending_action_freq,
710 wpa_s->pending_action_dst,
711 wpa_s->pending_action_src,
712 wpa_s->pending_action_bssid,
715 if (wpa_s->pending_pd_before_join &&
716 (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr,
718 os_memcmp(wpa_s->pending_action_dst,
719 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
720 wpa_s->pending_pd_before_join = 0;
721 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
722 "join-existing-group operation");
723 wpas_p2p_join_start(wpa_s);
728 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
729 const u8 *src, const u8 *bssid, const u8 *buf,
730 size_t len, unsigned int wait_time)
732 struct wpa_supplicant *wpa_s = ctx;
734 wpa_printf(MSG_DEBUG, "P2P: Send action frame: freq=%d dst=" MACSTR
735 " src=" MACSTR " bssid=" MACSTR " len=%d",
736 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
739 if (wpa_s->pending_action_tx) {
740 wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
741 "to " MACSTR, MAC2STR(wpa_s->pending_action_dst));
742 wpabuf_free(wpa_s->pending_action_tx);
744 wpa_s->pending_action_tx = wpabuf_alloc(len);
745 if (wpa_s->pending_action_tx == NULL) {
746 wpa_printf(MSG_DEBUG, "P2P: Failed to allocate Action frame "
747 "TX buffer (len=%llu)", (unsigned long long) len);
750 wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
751 os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
752 os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
753 os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
754 wpa_s->pending_action_freq = freq;
756 if (freq != 0 && wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
757 struct wpa_supplicant *iface;
759 iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
760 wpa_s->action_tx_wait_time = wait_time;
762 return wpa_drv_send_action(iface, wpa_s->pending_action_freq,
763 wait_time, wpa_s->pending_action_dst,
764 wpa_s->pending_action_src,
765 wpa_s->pending_action_bssid,
766 wpabuf_head(wpa_s->pending_action_tx),
767 wpabuf_len(wpa_s->pending_action_tx));
771 struct wpa_supplicant *tx_iface;
772 tx_iface = wpas_get_tx_interface(wpa_s, src);
773 if (tx_iface->assoc_freq == freq) {
774 wpa_printf(MSG_DEBUG, "P2P: Already on requested "
775 "channel (TX interface operating channel)");
780 if (wpa_s->off_channel_freq == freq || freq == 0) {
781 wpa_printf(MSG_DEBUG, "P2P: Already on requested channel; "
782 "send Action frame immediately");
783 /* TODO: Would there ever be need to extend the current
784 * duration on the channel? */
785 wpa_s->pending_action_without_roc = 1;
786 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
787 eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
790 wpa_s->pending_action_without_roc = 0;
792 if (wpa_s->roc_waiting_drv_freq == freq) {
793 wpa_printf(MSG_DEBUG, "P2P: Already waiting for driver to get "
794 "to frequency %u MHz; continue waiting to send the "
795 "Action frame", freq);
799 wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
800 "once the driver gets to the requested channel");
801 if (wait_time > wpa_s->max_remain_on_chan)
802 wait_time = wpa_s->max_remain_on_chan;
803 if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
804 wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
805 "to remain on channel (%u MHz) for Action "
809 wpa_s->off_channel_freq = 0;
810 wpa_s->roc_waiting_drv_freq = freq;
816 static void wpas_send_action_done(void *ctx)
818 struct wpa_supplicant *wpa_s = ctx;
819 wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification");
820 wpabuf_free(wpa_s->pending_action_tx);
821 wpa_s->pending_action_tx = NULL;
822 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX &&
823 wpa_s->action_tx_wait_time)
824 wpa_drv_send_action_cancel_wait(wpa_s);
826 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
827 wpa_drv_cancel_remain_on_channel(wpa_s);
828 wpa_s->off_channel_freq = 0;
829 wpa_s->roc_waiting_drv_freq = 0;
834 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
835 struct p2p_go_neg_results *params)
837 if (wpa_s->go_params == NULL) {
838 wpa_s->go_params = os_malloc(sizeof(*params));
839 if (wpa_s->go_params == NULL)
842 os_memcpy(wpa_s->go_params, params, sizeof(*params));
847 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
848 struct p2p_go_neg_results *res)
850 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
851 MAC2STR(res->peer_interface_addr));
852 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
853 res->ssid, res->ssid_len);
854 wpa_supplicant_ap_deinit(wpa_s);
855 wpas_copy_go_neg_results(wpa_s, res);
856 if (res->wps_method == WPS_PBC)
857 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
859 u16 dev_pw_id = DEV_PW_DEFAULT;
860 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
861 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
862 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
863 wpa_s->p2p_pin, 1, dev_pw_id);
868 static void p2p_go_configured(void *ctx, void *data)
870 struct wpa_supplicant *wpa_s = ctx;
871 struct p2p_go_neg_results *params = data;
872 struct wpa_ssid *ssid;
875 ssid = wpa_s->current_ssid;
876 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
877 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
878 if (wpa_s->global->p2p_group_formation == wpa_s)
879 wpa_s->global->p2p_group_formation = NULL;
880 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
881 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
882 "go_dev_addr=" MACSTR "%s",
884 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
886 params->passphrase ? params->passphrase : "",
887 MAC2STR(wpa_s->parent->own_addr),
888 params->persistent_group ? " [PERSISTENT]" : "");
890 if (params->persistent_group)
891 network_id = wpas_p2p_store_persistent_group(
893 wpa_s->parent->own_addr);
895 network_id = ssid->id;
896 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
897 wpas_p2p_cross_connect_setup(wpa_s);
898 wpas_p2p_set_group_idle_timeout(wpa_s);
902 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
903 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
904 params->peer_interface_addr)) {
905 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
909 if (params->wps_method == WPS_PBC)
910 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
912 else if (wpa_s->p2p_pin[0])
913 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
914 wpa_s->p2p_pin, NULL, 0);
915 os_free(wpa_s->go_params);
916 wpa_s->go_params = NULL;
920 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
921 struct p2p_go_neg_results *params,
924 struct wpa_ssid *ssid;
926 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
929 ssid = wpa_config_add_network(wpa_s->conf);
933 wpa_config_set_network_defaults(ssid);
936 ssid->p2p_persistent_group = params->persistent_group;
937 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
939 ssid->frequency = params->freq;
940 ssid->ssid = os_zalloc(params->ssid_len + 1);
942 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
943 ssid->ssid_len = params->ssid_len;
945 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
946 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
947 ssid->proto = WPA_PROTO_RSN;
948 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
949 ssid->passphrase = os_strdup(params->passphrase);
951 wpa_s->ap_configured_cb = p2p_go_configured;
952 wpa_s->ap_configured_cb_ctx = wpa_s;
953 wpa_s->ap_configured_cb_data = wpa_s->go_params;
954 wpa_s->connect_without_scan = ssid;
955 wpa_s->reassociate = 1;
956 wpa_s->disconnected = 0;
957 wpa_supplicant_req_scan(wpa_s, 0, 0);
961 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
962 const struct wpa_supplicant *src)
964 struct wpa_config *d;
965 const struct wpa_config *s;
970 #define C(n) if (s->n) d->n = os_strdup(s->n)
979 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
980 os_memcpy(d->sec_device_type, s->sec_device_type,
981 sizeof(d->sec_device_type));
982 d->num_sec_device_types = s->num_sec_device_types;
984 d->p2p_group_idle = s->p2p_group_idle;
985 d->p2p_intra_bss = s->p2p_intra_bss;
989 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
990 enum wpa_driver_if_type type)
992 char ifname[120], force_ifname[120];
994 if (wpa_s->pending_interface_name[0]) {
995 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
996 "- skip creation of a new one");
997 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
998 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
999 "unknown?! ifname='%s'",
1000 wpa_s->pending_interface_name);
1006 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
1007 wpa_s->p2p_group_idx);
1008 if (os_strlen(ifname) >= IFNAMSIZ &&
1009 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1010 /* Try to avoid going over the IFNAMSIZ length limit */
1011 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1012 wpa_s->p2p_group_idx);
1014 force_ifname[0] = '\0';
1016 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1018 wpa_s->p2p_group_idx++;
1020 wpa_s->pending_interface_type = type;
1021 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1022 wpa_s->pending_interface_addr, NULL) < 0) {
1023 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1028 if (force_ifname[0]) {
1029 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1031 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1032 sizeof(wpa_s->pending_interface_name));
1034 os_strlcpy(wpa_s->pending_interface_name, ifname,
1035 sizeof(wpa_s->pending_interface_name));
1036 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1037 MACSTR, wpa_s->pending_interface_name,
1038 MAC2STR(wpa_s->pending_interface_addr));
1044 static void wpas_p2p_remove_pending_group_interface(
1045 struct wpa_supplicant *wpa_s)
1047 if (!wpa_s->pending_interface_name[0] ||
1048 is_zero_ether_addr(wpa_s->pending_interface_addr))
1049 return; /* No pending virtual interface */
1051 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1052 wpa_s->pending_interface_name);
1053 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1054 wpa_s->pending_interface_name);
1055 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1056 wpa_s->pending_interface_name[0] = '\0';
1060 static struct wpa_supplicant *
1061 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1063 struct wpa_interface iface;
1064 struct wpa_supplicant *group_wpa_s;
1066 if (!wpa_s->pending_interface_name[0]) {
1067 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1068 if (!wpas_p2p_create_iface(wpa_s))
1071 * Something has forced us to remove the pending interface; try
1072 * to create a new one and hope for the best that we will get
1073 * the same local address.
1075 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1076 WPA_IF_P2P_CLIENT) < 0)
1080 os_memset(&iface, 0, sizeof(iface));
1081 iface.ifname = wpa_s->pending_interface_name;
1082 iface.driver = wpa_s->driver->name;
1083 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1084 iface.driver_param = wpa_s->conf->driver_param;
1085 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1086 if (group_wpa_s == NULL) {
1087 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1088 "wpa_supplicant interface");
1091 wpa_s->pending_interface_name[0] = '\0';
1092 group_wpa_s->parent = wpa_s;
1093 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1094 P2P_GROUP_INTERFACE_CLIENT;
1095 wpa_s->global->p2p_group_formation = group_wpa_s;
1097 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1103 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1106 struct wpa_supplicant *wpa_s = eloop_ctx;
1107 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1108 if (wpa_s->global->p2p)
1109 p2p_group_formation_failed(wpa_s->global->p2p);
1110 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1111 wpa_drv_p2p_group_formation_failed(wpa_s);
1112 wpas_group_formation_completed(wpa_s, 0);
1116 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1118 struct wpa_supplicant *wpa_s = ctx;
1120 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1121 wpa_drv_cancel_remain_on_channel(wpa_s);
1122 wpa_s->off_channel_freq = 0;
1123 wpa_s->roc_waiting_drv_freq = 0;
1127 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1129 wpas_notify_p2p_go_neg_completed(wpa_s, res->status);
1130 wpas_p2p_remove_pending_group_interface(wpa_s);
1134 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1135 wpas_notify_p2p_go_neg_completed(wpa_s, P2P_SC_SUCCESS);
1137 if (wpa_s->create_p2p_iface) {
1138 struct wpa_supplicant *group_wpa_s =
1139 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1140 if (group_wpa_s == NULL) {
1141 wpas_p2p_remove_pending_group_interface(wpa_s);
1144 if (group_wpa_s != wpa_s) {
1145 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1146 sizeof(group_wpa_s->p2p_pin));
1147 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1149 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1150 wpa_s->pending_interface_name[0] = '\0';
1151 group_wpa_s->p2p_in_provisioning = 1;
1154 wpas_start_wps_go(group_wpa_s, res, 1);
1156 wpas_start_wps_enrollee(group_wpa_s, res);
1158 wpa_s->p2p_in_provisioning = 1;
1159 wpa_s->global->p2p_group_formation = wpa_s;
1162 wpas_start_wps_go(wpa_s, res, 1);
1164 wpas_start_wps_enrollee(ctx, res);
1167 wpa_s->p2p_long_listen = 0;
1168 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1170 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1171 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1172 (res->peer_config_timeout % 100) * 10000,
1173 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1177 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1179 struct wpa_supplicant *wpa_s = ctx;
1180 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1181 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1183 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1187 void wpas_dev_found(void *ctx, const u8 *addr,
1188 const struct p2p_peer_info *info,
1191 struct wpa_supplicant *wpa_s = ctx;
1192 char devtype[WPS_DEV_TYPE_BUFSIZE];
1194 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1195 " p2p_dev_addr=" MACSTR
1196 " pri_dev_type=%s name='%s' config_methods=0x%x "
1197 "dev_capab=0x%x group_capab=0x%x",
1198 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1199 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1201 info->device_name, info->config_methods,
1202 info->dev_capab, info->group_capab);
1204 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1208 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1210 struct wpa_supplicant *wpa_s = ctx;
1212 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1216 static int wpas_start_listen(void *ctx, unsigned int freq,
1217 unsigned int duration,
1218 const struct wpabuf *probe_resp_ie)
1220 struct wpa_supplicant *wpa_s = ctx;
1222 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1224 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1225 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1226 "report received Probe Request frames");
1230 wpa_s->pending_listen_freq = freq;
1231 wpa_s->pending_listen_duration = duration;
1233 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1234 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1235 "to remain on channel (%u MHz) for Listen "
1237 wpa_s->pending_listen_freq = 0;
1240 wpa_s->off_channel_freq = 0;
1241 wpa_s->roc_waiting_drv_freq = freq;
1247 static void wpas_stop_listen(void *ctx)
1249 struct wpa_supplicant *wpa_s = ctx;
1250 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1251 wpa_drv_cancel_remain_on_channel(wpa_s);
1252 wpa_s->off_channel_freq = 0;
1253 wpa_s->roc_waiting_drv_freq = 0;
1255 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1256 wpa_drv_probe_req_report(wpa_s, 0);
1260 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1262 struct wpa_supplicant *wpa_s = ctx;
1263 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1267 static struct p2p_srv_bonjour *
1268 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1269 const struct wpabuf *query)
1271 struct p2p_srv_bonjour *bsrv;
1274 len = wpabuf_len(query);
1275 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1276 struct p2p_srv_bonjour, list) {
1277 if (len == wpabuf_len(bsrv->query) &&
1278 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1286 static struct p2p_srv_upnp *
1287 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1288 const char *service)
1290 struct p2p_srv_upnp *usrv;
1292 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1293 struct p2p_srv_upnp, list) {
1294 if (version == usrv->version &&
1295 os_strcmp(service, usrv->service) == 0)
1302 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1307 if (wpabuf_tailroom(resp) < 5)
1310 /* Length (to be filled) */
1311 len_pos = wpabuf_put(resp, 2);
1312 wpabuf_put_u8(resp, srv_proto);
1313 wpabuf_put_u8(resp, srv_trans_id);
1315 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1316 /* Response Data: empty */
1317 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1321 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1322 struct wpabuf *resp, u8 srv_trans_id)
1324 struct p2p_srv_bonjour *bsrv;
1327 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1329 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1330 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1334 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1335 struct p2p_srv_bonjour, list) {
1336 if (wpabuf_tailroom(resp) <
1337 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1339 /* Length (to be filled) */
1340 len_pos = wpabuf_put(resp, 2);
1341 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1342 wpabuf_put_u8(resp, srv_trans_id);
1344 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1345 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1346 wpabuf_head(bsrv->resp),
1347 wpabuf_len(bsrv->resp));
1349 wpabuf_put_buf(resp, bsrv->query); /* Key */
1350 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1351 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1357 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1358 struct wpabuf *resp, u8 srv_trans_id,
1359 const u8 *query, size_t query_len)
1361 struct p2p_srv_bonjour *bsrv;
1365 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1367 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1368 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1369 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1374 if (query_len == 0) {
1375 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1379 if (wpabuf_tailroom(resp) < 5)
1381 /* Length (to be filled) */
1382 len_pos = wpabuf_put(resp, 2);
1383 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1384 wpabuf_put_u8(resp, srv_trans_id);
1386 wpabuf_set(&buf, query, query_len);
1387 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1389 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1393 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1394 /* Response Data: empty */
1395 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1401 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1402 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1403 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1405 if (wpabuf_tailroom(resp) >=
1406 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1408 wpabuf_put_buf(resp, bsrv->query); /* Key */
1409 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1411 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1415 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1416 struct wpabuf *resp, u8 srv_trans_id)
1418 struct p2p_srv_upnp *usrv;
1421 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1423 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1424 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1428 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1429 struct p2p_srv_upnp, list) {
1430 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1433 /* Length (to be filled) */
1434 len_pos = wpabuf_put(resp, 2);
1435 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1436 wpabuf_put_u8(resp, srv_trans_id);
1439 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1441 wpabuf_put_u8(resp, usrv->version);
1442 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1444 wpabuf_put_str(resp, usrv->service);
1445 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1451 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1452 struct wpabuf *resp, u8 srv_trans_id,
1453 const u8 *query, size_t query_len)
1455 struct p2p_srv_upnp *usrv;
1461 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1464 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1465 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1466 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1471 if (query_len == 0) {
1472 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1476 if (wpabuf_tailroom(resp) < 5)
1479 /* Length (to be filled) */
1480 len_pos = wpabuf_put(resp, 2);
1481 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1482 wpabuf_put_u8(resp, srv_trans_id);
1485 str = os_malloc(query_len);
1488 os_memcpy(str, query + 1, query_len - 1);
1489 str[query_len - 1] = '\0';
1491 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1492 struct p2p_srv_upnp, list) {
1493 if (version != usrv->version)
1496 if (os_strcmp(str, "ssdp:all") != 0 &&
1497 os_strstr(usrv->service, str) == NULL)
1500 if (wpabuf_tailroom(resp) < 2)
1504 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1506 wpabuf_put_u8(resp, version);
1508 wpabuf_put_u8(resp, ',');
1512 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1514 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1516 wpabuf_put_str(resp, usrv->service);
1521 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1524 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1525 /* Response Data: empty */
1528 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1532 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1533 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1535 struct wpa_supplicant *wpa_s = ctx;
1536 const u8 *pos = tlvs;
1537 const u8 *end = tlvs + tlvs_len;
1540 struct wpabuf *resp;
1541 u8 srv_proto, srv_trans_id;
1545 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1547 buf_len = 2 * tlvs_len + 1;
1548 buf = os_malloc(buf_len);
1550 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1551 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1553 freq, MAC2STR(sa), dialog_token, update_indic,
1558 if (wpa_s->p2p_sd_over_ctrl_iface)
1559 return; /* to be processed by an external program */
1561 resp = wpabuf_alloc(10000);
1565 while (pos + 1 < end) {
1566 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1567 slen = WPA_GET_LE16(pos);
1569 if (pos + slen > end || slen < 2) {
1570 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1575 tlv_end = pos + slen;
1578 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1580 srv_trans_id = *pos++;
1581 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1584 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1585 pos, tlv_end - pos);
1588 if (wpa_s->force_long_sd) {
1589 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1591 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1592 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1596 switch (srv_proto) {
1597 case P2P_SERV_ALL_SERVICES:
1598 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1599 "for all services");
1600 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1601 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1602 wpa_printf(MSG_DEBUG, "P2P: No service "
1603 "discovery protocols available");
1604 wpas_sd_add_proto_not_avail(
1605 resp, P2P_SERV_ALL_SERVICES,
1609 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1610 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1612 case P2P_SERV_BONJOUR:
1613 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1614 pos, tlv_end - pos);
1617 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1618 pos, tlv_end - pos);
1621 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1622 "protocol %u", srv_proto);
1623 wpas_sd_add_proto_not_avail(resp, srv_proto,
1632 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1633 update_indic, tlvs, tlvs_len);
1635 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1641 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1642 const u8 *tlvs, size_t tlvs_len)
1644 struct wpa_supplicant *wpa_s = ctx;
1645 const u8 *pos = tlvs;
1646 const u8 *end = tlvs + tlvs_len;
1652 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1654 if (tlvs_len > 1500) {
1655 /* TODO: better way for handling this */
1656 wpa_msg_ctrl(wpa_s, MSG_INFO,
1657 P2P_EVENT_SERV_DISC_RESP MACSTR
1658 " %u <long response: %u bytes>",
1659 MAC2STR(sa), update_indic,
1660 (unsigned int) tlvs_len);
1662 buf_len = 2 * tlvs_len + 1;
1663 buf = os_malloc(buf_len);
1665 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1666 wpa_msg_ctrl(wpa_s, MSG_INFO,
1667 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1668 MAC2STR(sa), update_indic, buf);
1674 u8 srv_proto, srv_trans_id, status;
1676 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1677 slen = WPA_GET_LE16(pos);
1679 if (pos + slen > end || slen < 3) {
1680 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1684 tlv_end = pos + slen;
1687 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1689 srv_trans_id = *pos++;
1690 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1693 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1696 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1697 pos, tlv_end - pos);
1702 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1706 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1707 const struct wpabuf *tlvs)
1709 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1710 return (void *) wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1711 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1713 return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1717 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1718 u8 version, const char *query)
1720 struct wpabuf *tlvs;
1723 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1726 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1727 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1728 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1729 wpabuf_put_u8(tlvs, version);
1730 wpabuf_put_str(tlvs, query);
1731 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1737 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1739 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1740 return wpa_drv_p2p_sd_cancel_request(wpa_s, (u64) req);
1741 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1743 return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1747 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1748 const u8 *dst, u8 dialog_token,
1749 const struct wpabuf *resp_tlvs)
1751 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1752 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1756 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1758 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1763 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1765 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1766 wpa_drv_p2p_service_update(wpa_s);
1769 if (wpa_s->global->p2p)
1770 p2p_sd_service_update(wpa_s->global->p2p);
1774 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1776 dl_list_del(&bsrv->list);
1777 wpabuf_free(bsrv->query);
1778 wpabuf_free(bsrv->resp);
1783 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1785 dl_list_del(&usrv->list);
1786 os_free(usrv->service);
1791 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1793 struct p2p_srv_bonjour *bsrv, *bn;
1794 struct p2p_srv_upnp *usrv, *un;
1796 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1797 struct p2p_srv_bonjour, list)
1798 wpas_p2p_srv_bonjour_free(bsrv);
1800 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1801 struct p2p_srv_upnp, list)
1802 wpas_p2p_srv_upnp_free(usrv);
1804 wpas_p2p_sd_service_update(wpa_s);
1808 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1809 struct wpabuf *query, struct wpabuf *resp)
1811 struct p2p_srv_bonjour *bsrv;
1813 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1816 wpabuf_free(bsrv->resp);
1821 bsrv = os_zalloc(sizeof(*bsrv));
1824 bsrv->query = query;
1826 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1828 wpas_p2p_sd_service_update(wpa_s);
1833 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1834 const struct wpabuf *query)
1836 struct p2p_srv_bonjour *bsrv;
1838 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1841 wpas_p2p_srv_bonjour_free(bsrv);
1842 wpas_p2p_sd_service_update(wpa_s);
1847 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1848 const char *service)
1850 struct p2p_srv_upnp *usrv;
1852 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1853 return 0; /* Already listed */
1854 usrv = os_zalloc(sizeof(*usrv));
1857 usrv->version = version;
1858 usrv->service = os_strdup(service);
1859 if (usrv->service == NULL) {
1863 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1865 wpas_p2p_sd_service_update(wpa_s);
1870 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1871 const char *service)
1873 struct p2p_srv_upnp *usrv;
1875 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1878 wpas_p2p_srv_upnp_free(usrv);
1879 wpas_p2p_sd_service_update(wpa_s);
1884 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1885 const u8 *peer, const char *params,
1886 unsigned int generated_pin)
1888 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1889 MAC2STR(peer), generated_pin, params);
1893 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1894 const u8 *peer, const char *params)
1896 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1897 MAC2STR(peer), params);
1901 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1902 const u8 *dev_addr, const u8 *pri_dev_type,
1903 const char *dev_name, u16 supp_config_methods,
1904 u8 dev_capab, u8 group_capab)
1906 struct wpa_supplicant *wpa_s = ctx;
1907 char devtype[WPS_DEV_TYPE_BUFSIZE];
1909 u8 empty_dev_type[8];
1910 unsigned int generated_pin = 0;
1912 if (pri_dev_type == NULL) {
1913 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1914 pri_dev_type = empty_dev_type;
1916 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1917 " pri_dev_type=%s name='%s' config_methods=0x%x "
1918 "dev_capab=0x%x group_capab=0x%x",
1920 wps_dev_type_bin2str(pri_dev_type, devtype,
1922 dev_name, supp_config_methods, dev_capab, group_capab);
1923 params[sizeof(params) - 1] = '\0';
1925 if (config_methods & WPS_CONFIG_DISPLAY) {
1926 generated_pin = wps_generate_pin();
1927 wpas_prov_disc_local_display(wpa_s, peer, params,
1929 } else if (config_methods & WPS_CONFIG_KEYPAD)
1930 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1931 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1932 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1933 "%s", MAC2STR(peer), params);
1935 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1936 P2P_PROV_DISC_SUCCESS,
1937 config_methods, generated_pin);
1941 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1943 struct wpa_supplicant *wpa_s = ctx;
1944 unsigned int generated_pin = 0;
1946 if (config_methods & WPS_CONFIG_DISPLAY)
1947 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1948 else if (config_methods & WPS_CONFIG_KEYPAD) {
1949 generated_pin = wps_generate_pin();
1950 wpas_prov_disc_local_display(wpa_s, peer, "", generated_pin);
1951 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1952 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1955 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1956 P2P_PROV_DISC_SUCCESS,
1957 config_methods, generated_pin);
1959 if (wpa_s->pending_pd_before_join &&
1960 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1961 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1962 wpa_s->pending_pd_before_join = 0;
1963 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1964 "join-existing-group operation");
1965 wpas_p2p_join_start(wpa_s);
1970 void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1971 enum p2p_prov_disc_status status)
1973 struct wpa_supplicant *wpa_s = ctx;
1975 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1980 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1981 const u8 *go_dev_addr, const u8 *ssid,
1982 size_t ssid_len, int *go, u8 *group_bssid,
1983 int *force_freq, int persistent_group)
1985 struct wpa_supplicant *wpa_s = ctx;
1987 u8 cur_bssid[ETH_ALEN];
1989 struct wpa_supplicant *grp;
1991 if (!persistent_group) {
1992 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1993 " to join an active group", MAC2STR(sa));
1994 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1995 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1997 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1998 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1999 "authorized invitation");
2003 * Do not accept the invitation automatically; notify user and
2006 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2009 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2011 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2012 "running persistent group");
2014 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2018 if (!wpa_s->conf->persistent_reconnect)
2019 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2021 for (s = wpa_s->conf->ssid; s; s = s->next) {
2022 if (s->disabled == 2 &&
2023 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2024 s->ssid_len == ssid_len &&
2025 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2030 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2031 " requested reinvocation of an unknown group",
2033 return P2P_SC_FAIL_UNKNOWN_GROUP;
2036 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2038 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2039 wpa_printf(MSG_DEBUG, "P2P: The only available "
2040 "interface is already in use - reject "
2042 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2044 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2045 } else if (s->mode == WPAS_MODE_P2P_GO) {
2047 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2049 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2050 "interface address for the group");
2051 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2053 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2058 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2059 wpa_s->assoc_freq) {
2060 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2061 "the channel we are already using");
2062 *force_freq = wpa_s->assoc_freq;
2065 res = wpa_drv_shared_freq(wpa_s);
2067 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2068 "with the channel we are already using on a "
2069 "shared interface");
2073 return P2P_SC_SUCCESS;
2077 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2078 const u8 *ssid, size_t ssid_len,
2079 const u8 *go_dev_addr, u8 status,
2082 struct wpa_supplicant *wpa_s = ctx;
2085 for (s = wpa_s->conf->ssid; s; s = s->next) {
2086 if (s->disabled == 2 &&
2087 s->ssid_len == ssid_len &&
2088 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2092 if (status == P2P_SC_SUCCESS) {
2093 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2094 " was accepted; op_freq=%d MHz",
2095 MAC2STR(sa), op_freq);
2097 wpas_p2p_group_add_persistent(
2098 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
2100 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2101 wpa_s->p2p_wps_method);
2106 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2107 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2108 " was rejected (status %u)", MAC2STR(sa), status);
2114 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2115 "sa=" MACSTR " go_dev_addr=" MACSTR
2116 " bssid=" MACSTR " unknown-network",
2117 MAC2STR(sa), MAC2STR(go_dev_addr),
2120 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2121 "sa=" MACSTR " go_dev_addr=" MACSTR
2123 MAC2STR(sa), MAC2STR(go_dev_addr));
2128 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2129 " persistent=%d", MAC2STR(sa), s->id);
2133 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2135 struct wpa_supplicant *wpa_s = ctx;
2136 struct wpa_ssid *ssid;
2139 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2140 "status=%d " MACSTR,
2141 status, MAC2STR(bssid));
2143 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2144 "status=%d ", status);
2146 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2148 if (wpa_s->pending_invite_ssid_id == -1)
2149 return; /* Invitation to active group */
2151 if (status != P2P_SC_SUCCESS) {
2152 wpas_p2p_remove_pending_group_interface(wpa_s);
2156 ssid = wpa_config_get_network(wpa_s->conf,
2157 wpa_s->pending_invite_ssid_id);
2159 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2160 "data matching with invitation");
2164 wpas_p2p_group_add_persistent(wpa_s, ssid,
2165 ssid->mode == WPAS_MODE_P2P_GO, 0);
2169 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2170 struct p2p_channels *chan)
2174 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2177 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2178 chan->reg_class[cla].reg_class = 81;
2179 chan->reg_class[cla].channels = 11;
2180 for (i = 0; i < 11; i++)
2181 chan->reg_class[cla].channel[i] = i + 1;
2184 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2187 /* Operating class 115 - 5 GHz, channels 36-48 */
2188 chan->reg_class[cla].reg_class = 115;
2189 chan->reg_class[cla].channels = 4;
2190 chan->reg_class[cla].channel[0] = 36;
2191 chan->reg_class[cla].channel[1] = 40;
2192 chan->reg_class[cla].channel[2] = 44;
2193 chan->reg_class[cla].channel[3] = 48;
2196 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2199 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2200 chan->reg_class[cla].reg_class = 124;
2201 chan->reg_class[cla].channels = 4;
2202 chan->reg_class[cla].channel[0] = 149;
2203 chan->reg_class[cla].channel[1] = 153;
2204 chan->reg_class[cla].channel[2] = 157;
2205 chan->reg_class[cla].channel[3] = 161;
2208 chan->reg_classes = cla;
2213 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2215 enum hostapd_hw_mode mode)
2219 for (i = 0; i < num_modes; i++) {
2220 if (modes[i].mode == mode)
2228 static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
2232 for (i = 0; i < mode->num_channels; i++) {
2233 if (mode->channels[i].chan == chan) {
2235 *flags = mode->channels[i].flag;
2236 return !(mode->channels[i].flag &
2237 (HOSTAPD_CHAN_DISABLED |
2238 HOSTAPD_CHAN_PASSIVE_SCAN |
2239 HOSTAPD_CHAN_NO_IBSS |
2240 HOSTAPD_CHAN_RADAR));
2248 struct p2p_oper_class_map {
2249 enum hostapd_hw_mode mode;
2254 enum { BW20, BW40PLUS, BW40MINUS } bw;
2257 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2258 struct p2p_channels *chan)
2260 struct hostapd_hw_modes *modes, *mode;
2261 u16 num_modes, flags;
2263 struct p2p_oper_class_map op_class[] = {
2264 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2265 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2266 #if 0 /* Do not enable HT40 on 2 GHz for now */
2267 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2268 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2270 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2271 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2272 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2273 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2274 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2275 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2276 { -1, 0, 0, 0, 0, BW20 }
2279 modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
2280 if (modes == NULL) {
2281 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2282 "of all supported channels; assume dualband "
2284 return wpas_p2p_default_channels(wpa_s, chan);
2289 for (op = 0; op_class[op].op_class; op++) {
2290 struct p2p_oper_class_map *o = &op_class[op];
2292 struct p2p_reg_class *reg = NULL;
2294 mode = get_mode(modes, num_modes, o->mode);
2297 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2299 if (!has_channel(mode, ch, &flag))
2301 if (o->bw == BW40MINUS &&
2302 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2303 !has_channel(mode, ch - 4, NULL)))
2305 if (o->bw == BW40PLUS &&
2306 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2307 !has_channel(mode, ch + 4, NULL)))
2310 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2311 "class %u", o->op_class);
2312 reg = &chan->reg_class[cla];
2314 reg->reg_class = o->op_class;
2316 reg->channel[reg->channels] = ch;
2320 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2321 reg->channel, reg->channels);
2325 chan->reg_classes = cla;
2327 ieee80211_sta_free_hw_features(modes, num_modes);
2333 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2336 struct wpa_supplicant *wpa_s = ctx;
2338 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2339 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2345 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2350 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2351 * @global: Pointer to global data from wpa_supplicant_init()
2352 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2353 * Returns: 0 on success, -1 on failure
2355 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2357 struct p2p_config p2p;
2361 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2364 #ifdef CONFIG_CLIENT_MLME
2365 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
2366 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
2367 wpa_s->mlme.public_action_cb_ctx = wpa_s;
2369 #endif /* CONFIG_CLIENT_MLME */
2371 if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2372 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2373 /* Continue anyway; this is not really a fatal error */
2379 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2380 struct p2p_params params;
2382 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2383 os_memset(¶ms, 0, sizeof(params));
2384 params.dev_name = wpa_s->conf->device_name;
2385 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2387 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2388 os_memcpy(params.sec_dev_type,
2389 wpa_s->conf->sec_device_type,
2390 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2392 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2398 os_memset(&p2p, 0, sizeof(p2p));
2399 p2p.msg_ctx = wpa_s;
2401 p2p.p2p_scan = wpas_p2p_scan;
2402 p2p.send_action = wpas_send_action;
2403 p2p.send_action_done = wpas_send_action_done;
2404 p2p.go_neg_completed = wpas_go_neg_completed;
2405 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2406 p2p.dev_found = wpas_dev_found;
2407 p2p.dev_lost = wpas_dev_lost;
2408 p2p.start_listen = wpas_start_listen;
2409 p2p.stop_listen = wpas_stop_listen;
2410 p2p.send_probe_resp = wpas_send_probe_resp;
2411 p2p.sd_request = wpas_sd_request;
2412 p2p.sd_response = wpas_sd_response;
2413 p2p.prov_disc_req = wpas_prov_disc_req;
2414 p2p.prov_disc_resp = wpas_prov_disc_resp;
2415 p2p.prov_disc_fail = wpas_prov_disc_fail;
2416 p2p.invitation_process = wpas_invitation_process;
2417 p2p.invitation_received = wpas_invitation_received;
2418 p2p.invitation_result = wpas_invitation_result;
2419 p2p.get_noa = wpas_get_noa;
2421 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2422 os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2423 p2p.dev_name = wpa_s->conf->device_name;
2424 p2p.manufacturer = wpa_s->conf->manufacturer;
2425 p2p.model_name = wpa_s->conf->model_name;
2426 p2p.model_number = wpa_s->conf->model_number;
2427 p2p.serial_number = wpa_s->conf->serial_number;
2429 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2430 p2p.config_methods = wpa_s->wps->config_methods;
2433 if (wpa_s->conf->p2p_listen_reg_class &&
2434 wpa_s->conf->p2p_listen_channel) {
2435 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2436 p2p.channel = wpa_s->conf->p2p_listen_channel;
2440 * Pick one of the social channels randomly as the listen
2443 os_get_random((u8 *) &r, sizeof(r));
2444 p2p.channel = 1 + (r % 3) * 5;
2446 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2448 if (wpa_s->conf->p2p_oper_reg_class &&
2449 wpa_s->conf->p2p_oper_channel) {
2450 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2451 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2452 p2p.cfg_op_channel = 1;
2453 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2454 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2457 p2p.op_reg_class = 81;
2459 * Use random operation channel from (1, 6, 11) if no other
2460 * preference is indicated.
2462 os_get_random((u8 *) &r, sizeof(r));
2463 p2p.op_channel = 1 + (r % 3) * 5;
2464 p2p.cfg_op_channel = 0;
2465 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2466 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2468 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2469 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2470 p2p.country[2] = 0x04;
2472 os_memcpy(p2p.country, "XX\x04", 3);
2474 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2475 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2480 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2483 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2484 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2485 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2487 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2488 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2490 p2p.max_peers = 100;
2492 if (wpa_s->conf->p2p_ssid_postfix) {
2493 p2p.ssid_postfix_len =
2494 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2495 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2496 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2497 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2498 p2p.ssid_postfix_len);
2501 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2503 global->p2p = p2p_init(&p2p);
2504 if (global->p2p == NULL)
2507 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2508 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2510 p2p_add_wps_vendor_extension(
2511 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2519 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2520 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2522 * This function deinitialize per-interface P2P data.
2524 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2526 if (wpa_s->driver && wpa_s->drv_priv)
2527 wpa_drv_probe_req_report(wpa_s, 0);
2528 os_free(wpa_s->go_params);
2529 wpa_s->go_params = NULL;
2530 wpabuf_free(wpa_s->pending_action_tx);
2531 wpa_s->pending_action_tx = NULL;
2532 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2533 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2534 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2535 wpa_s->p2p_long_listen = 0;
2536 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2537 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2538 wpas_p2p_remove_pending_group_interface(wpa_s);
2540 /* TODO: remove group interface from the driver if this wpa_s instance
2541 * is on top of a P2P group interface */
2546 * wpas_p2p_deinit_global - Deinitialize global P2P module
2547 * @global: Pointer to global data from wpa_supplicant_init()
2549 * This function deinitializes the global (per device) P2P module.
2551 void wpas_p2p_deinit_global(struct wpa_global *global)
2553 struct wpa_supplicant *wpa_s, *tmp;
2556 if (global->p2p == NULL)
2559 /* Remove remaining P2P group interfaces */
2560 wpa_s = global->ifaces;
2562 wpas_p2p_service_flush(wpa_s);
2563 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2564 wpa_s = wpa_s->next;
2566 enum wpa_driver_if_type type;
2567 tmp = global->ifaces;
2570 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2575 ifname = os_strdup(tmp->ifname);
2576 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2577 wpa_supplicant_remove_iface(global, tmp);
2579 wpa_drv_if_remove(wpa_s, type, ifname);
2584 * Deinit GO data on any possibly remaining interface (if main
2585 * interface is used as GO).
2587 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2588 if (wpa_s->ap_iface)
2589 wpas_p2p_group_deinit(wpa_s);
2592 p2p_deinit(global->p2p);
2597 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2599 if (wpa_s->drv_flags &
2600 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2601 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2602 return 1; /* P2P group requires a new interface in every case
2604 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2605 return 0; /* driver does not support concurrent operations */
2606 if (wpa_s->global->ifaces->next)
2607 return 1; /* more that one interface already in use */
2608 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2609 return 1; /* this interface is already in use */
2614 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2615 const u8 *peer_addr,
2616 enum p2p_wps_method wps_method,
2617 int go_intent, const u8 *own_interface_addr,
2618 unsigned int force_freq, int persistent_group)
2620 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2621 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2622 go_intent, own_interface_addr,
2623 force_freq, persistent_group);
2626 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2627 go_intent, own_interface_addr, force_freq,
2632 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2633 const u8 *peer_addr,
2634 enum p2p_wps_method wps_method,
2635 int go_intent, const u8 *own_interface_addr,
2636 unsigned int force_freq, int persistent_group)
2638 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2641 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2642 go_intent, own_interface_addr, force_freq,
2647 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2649 wpa_s->p2p_join_scan_count++;
2650 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2651 wpa_s->p2p_join_scan_count);
2652 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2653 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2654 " for join operationg - stop join attempt",
2655 MAC2STR(wpa_s->pending_join_iface_addr));
2656 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2657 wpa_msg(wpa_s->parent, MSG_INFO,
2658 P2P_EVENT_GROUP_FORMATION_FAILURE);
2663 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2664 struct wpa_scan_results *scan_res)
2666 struct wpa_bss *bss;
2668 u8 iface_addr[ETH_ALEN];
2670 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2672 if (wpa_s->global->p2p_disabled)
2675 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2676 scan_res ? (int) scan_res->num : -1);
2679 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2681 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2682 wpa_s->pending_join_iface_addr);
2684 p2p_get_interface_addr(wpa_s->global->p2p,
2685 wpa_s->pending_join_dev_addr,
2687 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2689 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2690 "address for join from " MACSTR " to " MACSTR
2691 " based on newly discovered P2P peer entry",
2692 MAC2STR(wpa_s->pending_join_iface_addr),
2693 MAC2STR(iface_addr));
2694 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2697 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2698 wpa_s->pending_join_iface_addr);
2701 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2702 "from P2P peer table: %d MHz", freq);
2704 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2707 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2708 "from BSS table: %d MHz", freq);
2713 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2714 "prior to joining an existing group (GO " MACSTR
2716 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2717 wpa_s->pending_pd_before_join = 1;
2719 switch (wpa_s->pending_join_wps_method) {
2720 case WPS_PIN_DISPLAY:
2721 method = WPS_CONFIG_KEYPAD;
2723 case WPS_PIN_KEYPAD:
2724 method = WPS_CONFIG_DISPLAY;
2727 method = WPS_CONFIG_PUSHBUTTON;
2734 if (p2p_prov_disc_req(wpa_s->global->p2p,
2735 wpa_s->pending_join_dev_addr, method, 1)
2737 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2738 "Discovery Request before joining an "
2740 wpa_s->pending_pd_before_join = 0;
2745 * Actual join operation will be started from the Action frame
2746 * TX status callback.
2751 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2752 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2753 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2754 wpas_p2p_check_join_scan_limit(wpa_s);
2758 /* Start join operation immediately */
2759 wpas_p2p_join_start(wpa_s);
2763 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2765 struct wpa_supplicant *wpa_s = eloop_ctx;
2767 struct wpa_driver_scan_params params;
2768 struct wpabuf *wps_ie, *ies;
2770 os_memset(¶ms, 0, sizeof(params));
2772 /* P2P Wildcard SSID */
2773 params.num_ssids = 1;
2774 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2775 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2777 wpa_s->wps->dev.p2p = 1;
2778 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2779 WPS_REQ_ENROLLEE, 0, NULL);
2780 if (wps_ie == NULL) {
2781 wpas_p2p_scan_res_join(wpa_s, NULL);
2785 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
2787 wpabuf_free(wps_ie);
2788 wpas_p2p_scan_res_join(wpa_s, NULL);
2791 wpabuf_put_buf(ies, wps_ie);
2792 wpabuf_free(wps_ie);
2794 p2p_scan_ie(wpa_s->global->p2p, ies);
2796 params.p2p_probe = 1;
2797 params.extra_ies = wpabuf_head(ies);
2798 params.extra_ies_len = wpabuf_len(ies);
2801 * Run a scan to update BSS table and start Provision Discovery once
2802 * the new scan results become available.
2804 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2805 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
2806 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
2808 ret = wpa_drv_scan(wpa_s, ¶ms);
2813 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2815 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2816 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2817 wpas_p2p_check_join_scan_limit(wpa_s);
2822 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2823 const u8 *dev_addr, enum p2p_wps_method wps_method)
2825 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2826 MACSTR " dev " MACSTR ")",
2827 MAC2STR(iface_addr), MAC2STR(dev_addr));
2829 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2830 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2831 wpa_s->pending_join_wps_method = wps_method;
2833 /* Make sure we are not running find during connection establishment */
2834 wpas_p2p_stop_find(wpa_s);
2836 wpa_s->p2p_join_scan_count = 0;
2837 wpas_p2p_join_scan(wpa_s, NULL);
2842 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2844 struct wpa_supplicant *group;
2845 struct p2p_go_neg_results res;
2847 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2850 if (group != wpa_s) {
2851 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2852 sizeof(group->p2p_pin));
2853 group->p2p_wps_method = wpa_s->p2p_wps_method;
2856 group->p2p_in_provisioning = 1;
2858 os_memset(&res, 0, sizeof(res));
2859 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2861 res.wps_method = wpa_s->pending_join_wps_method;
2862 wpas_start_wps_enrollee(group, &res);
2865 * Allow a longer timeout for join-a-running-group than normal 15
2866 * second group formation timeout since the GO may not have authorized
2867 * our connection yet.
2869 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2870 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
2878 * wpas_p2p_connect - Request P2P Group Formation to be started
2879 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2880 * @peer_addr: Address of the peer P2P Device
2881 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2882 * @persistent_group: Whether to create a persistent group
2883 * @join: Whether to join an existing group (as a client) instead of starting
2884 * Group Owner negotiation; @peer_addr is BSSID in that case
2885 * @auth: Whether to only authorize the connection instead of doing that and
2886 * initiating Group Owner negotiation
2887 * @go_intent: GO Intent or -1 to use default
2888 * @freq: Frequency for the group or 0 for auto-selection
2889 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2890 * failure, -2 on failure due to channel not currently available,
2891 * -3 if forced channel is not supported
2893 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2894 const char *pin, enum p2p_wps_method wps_method,
2895 int persistent_group, int join, int auth, int go_intent,
2898 int force_freq = 0, oper_freq = 0;
2901 enum wpa_driver_if_type iftype;
2903 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2907 go_intent = wpa_s->conf->p2p_go_intent;
2910 wpa_s->p2p_long_listen = 0;
2912 wpa_s->p2p_wps_method = wps_method;
2915 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2916 else if (wps_method == WPS_PIN_DISPLAY) {
2917 ret = wps_generate_pin();
2918 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2920 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2923 wpa_s->p2p_pin[0] = '\0';
2926 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
2928 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2929 "connect a running group from " MACSTR,
2930 MAC2STR(peer_addr));
2931 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2934 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
2935 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2937 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2938 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
2941 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
2947 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2949 oper_freq = wpa_s->assoc_freq;
2951 oper_freq = wpa_drv_shared_freq(wpa_s);
2957 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
2958 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
2959 "(%u MHz) is not supported for P2P uses",
2964 if (oper_freq > 0 && freq != oper_freq &&
2965 !(wpa_s->drv_flags &
2966 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2967 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2968 "on %u MHz while connected on another "
2969 "channel (%u MHz)", freq, oper_freq);
2972 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2973 "requested channel (%u MHz)", freq);
2975 } else if (oper_freq > 0 &&
2976 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
2977 if (!(wpa_s->drv_flags &
2978 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2979 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2980 "while connected on non-P2P supported "
2981 "channel (%u MHz)", oper_freq);
2984 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
2985 "(%u MHz) not available for P2P - try to use "
2986 "another channel", oper_freq);
2988 } else if (oper_freq > 0) {
2989 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2990 "channel we are already using (%u MHz) on another "
2991 "interface", oper_freq);
2992 force_freq = oper_freq;
2995 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2997 if (!wpa_s->create_p2p_iface) {
2999 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3000 go_intent, wpa_s->own_addr,
3001 force_freq, persistent_group)
3006 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3007 go_intent, wpa_s->own_addr,
3008 force_freq, persistent_group) < 0)
3013 /* Prepare to add a new interface for the group */
3014 iftype = WPA_IF_P2P_GROUP;
3015 if (go_intent == 15)
3016 iftype = WPA_IF_P2P_GO;
3017 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3018 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3019 "interface for the group");
3024 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3026 wpa_s->pending_interface_addr,
3027 force_freq, persistent_group) < 0)
3031 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
3032 wpa_s->pending_interface_addr,
3033 force_freq, persistent_group) < 0) {
3034 wpas_p2p_remove_pending_group_interface(wpa_s);
3042 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3043 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3044 * @freq: Frequency of the channel in MHz
3045 * @duration: Duration of the stay on the channel in milliseconds
3047 * This callback is called when the driver indicates that it has started the
3048 * requested remain-on-channel duration.
3050 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3051 unsigned int freq, unsigned int duration)
3053 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3055 wpa_s->roc_waiting_drv_freq = 0;
3056 wpa_s->off_channel_freq = freq;
3057 wpas_send_action_cb(wpa_s, NULL);
3058 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3059 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3060 wpa_s->pending_listen_duration);
3061 wpa_s->pending_listen_freq = 0;
3066 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3067 unsigned int timeout)
3069 /* Limit maximum Listen state time based on driver limitation. */
3070 if (timeout > wpa_s->max_remain_on_chan)
3071 timeout = wpa_s->max_remain_on_chan;
3073 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3074 return wpa_drv_p2p_listen(wpa_s, timeout);
3076 return p2p_listen(wpa_s->global->p2p, timeout);
3081 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3082 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3083 * @freq: Frequency of the channel in MHz
3085 * This callback is called when the driver indicates that a remain-on-channel
3086 * operation has been completed, i.e., the duration on the requested channel
3089 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3092 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3093 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3094 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3095 wpa_s->off_channel_freq = 0;
3096 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3098 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3099 return; /* P2P module started a new operation */
3100 if (wpa_s->pending_action_tx)
3102 if (wpa_s->p2p_long_listen > 0)
3103 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3104 if (wpa_s->p2p_long_listen > 0) {
3105 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3106 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3112 * wpas_p2p_group_remove - Remove a P2P group
3113 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3114 * @ifname: Network interface name of the group interface or "*" to remove all
3116 * Returns: 0 on success, -1 on failure
3118 * This function is used to remove a P2P group. This can be used to disconnect
3119 * from a group in which the local end is a P2P Client or to end a P2P Group in
3120 * case the local end is the Group Owner. If a virtual network interface was
3121 * created for this group, that interface will be removed. Otherwise, only the
3122 * configured P2P group network will be removed from the interface.
3124 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3126 struct wpa_global *global = wpa_s->global;
3128 if (os_strcmp(ifname, "*") == 0) {
3129 struct wpa_supplicant *prev;
3130 wpa_s = global->ifaces;
3133 wpa_s = wpa_s->next;
3134 wpas_p2p_disconnect(prev);
3139 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3140 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3144 return wpas_p2p_disconnect(wpa_s);
3148 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3149 struct p2p_go_neg_results *params,
3155 os_memset(params, 0, sizeof(*params));
3156 params->role_go = 1;
3158 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3159 "frequency %d MHz", freq);
3160 params->freq = freq;
3161 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3162 wpa_s->conf->p2p_oper_channel >= 1 &&
3163 wpa_s->conf->p2p_oper_channel <= 11) {
3164 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3165 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3166 "frequency %d MHz", params->freq);
3167 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3168 wpa_s->conf->p2p_oper_reg_class == 124) {
3169 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3170 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3171 "frequency %d MHz", params->freq);
3172 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3173 wpa_s->best_overall_freq > 0 &&
3174 p2p_supported_freq(wpa_s->global->p2p,
3175 wpa_s->best_overall_freq)) {
3176 params->freq = wpa_s->best_overall_freq;
3177 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3178 "channel %d MHz", params->freq);
3179 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3180 wpa_s->best_24_freq > 0 &&
3181 p2p_supported_freq(wpa_s->global->p2p,
3182 wpa_s->best_24_freq)) {
3183 params->freq = wpa_s->best_24_freq;
3184 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3185 "channel %d MHz", params->freq);
3186 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3187 wpa_s->best_5_freq > 0 &&
3188 p2p_supported_freq(wpa_s->global->p2p,
3189 wpa_s->best_5_freq)) {
3190 params->freq = wpa_s->best_5_freq;
3191 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3192 "channel %d MHz", params->freq);
3194 params->freq = 2412;
3195 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3196 "known)", params->freq);
3199 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3200 wpa_s->assoc_freq && !freq) {
3201 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3203 params->freq = wpa_s->assoc_freq;
3206 res = wpa_drv_shared_freq(wpa_s);
3207 if (res > 0 && !freq) {
3208 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3209 "already using on a shared interface");
3215 static struct wpa_supplicant *
3216 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3219 struct wpa_supplicant *group_wpa_s;
3221 if (!wpas_p2p_create_iface(wpa_s))
3224 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3225 WPA_IF_P2P_CLIENT) < 0)
3227 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3228 if (group_wpa_s == NULL) {
3229 wpas_p2p_remove_pending_group_interface(wpa_s);
3238 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3239 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3240 * @persistent_group: Whether to create a persistent group
3241 * @freq: Frequency for the group or 0 to indicate no hardcoding
3242 * Returns: 0 on success, -1 on failure
3244 * This function creates a new P2P group with the local end as the Group Owner,
3245 * i.e., without using Group Owner Negotiation.
3247 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3250 struct p2p_go_neg_results params;
3253 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3257 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3259 if (wpa_s->best_24_freq > 0 &&
3260 p2p_supported_freq(wpa_s->global->p2p,
3261 wpa_s->best_24_freq)) {
3262 freq = wpa_s->best_24_freq;
3263 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3264 "channel: %d MHz", freq);
3266 os_get_random((u8 *) &r, sizeof(r));
3267 freq = 2412 + (r % 3) * 25;
3268 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3269 "channel: %d MHz", freq);
3274 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3276 if (wpa_s->best_5_freq > 0 &&
3277 p2p_supported_freq(wpa_s->global->p2p,
3278 wpa_s->best_5_freq)) {
3279 freq = wpa_s->best_5_freq;
3280 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3281 "channel: %d MHz", freq);
3283 os_get_random((u8 *) &r, sizeof(r));
3284 freq = 5180 + (r % 4) * 20;
3285 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3286 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3287 "5 GHz channel for P2P group");
3290 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3291 "channel: %d MHz", freq);
3295 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3296 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3297 "(%u MHz) is not supported for P2P uses",
3302 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3303 p2p_go_params(wpa_s->global->p2p, ¶ms);
3304 params.persistent_group = persistent_group;
3306 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3309 wpas_start_wps_go(wpa_s, ¶ms, 0);
3315 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3316 struct wpa_ssid *params, int addr_allocated)
3318 struct wpa_ssid *ssid;
3320 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3324 wpa_supplicant_ap_deinit(wpa_s);
3326 ssid = wpa_config_add_network(wpa_s->conf);
3329 wpa_config_set_network_defaults(ssid);
3330 ssid->temporary = 1;
3331 ssid->proto = WPA_PROTO_RSN;
3332 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3333 ssid->group_cipher = WPA_CIPHER_CCMP;
3334 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3335 ssid->ssid = os_malloc(params->ssid_len);
3336 if (ssid->ssid == NULL) {
3337 wpa_config_remove_network(wpa_s->conf, ssid->id);
3340 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3341 ssid->ssid_len = params->ssid_len;
3342 ssid->p2p_group = 1;
3343 ssid->export_keys = 1;
3344 if (params->psk_set) {
3345 os_memcpy(ssid->psk, params->psk, 32);
3348 if (params->passphrase)
3349 ssid->passphrase = os_strdup(params->passphrase);
3351 wpa_supplicant_select_network(wpa_s, ssid);
3353 wpa_s->show_group_started = 1;
3359 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3360 struct wpa_ssid *ssid, int addr_allocated,
3363 struct p2p_go_neg_results params;
3366 if (ssid->disabled != 2 || ssid->ssid == NULL)
3369 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3370 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3371 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3376 /* Make sure we are not running find during connection establishment */
3377 wpas_p2p_stop_find(wpa_s);
3379 if (ssid->mode == WPAS_MODE_INFRA)
3380 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3382 if (ssid->mode != WPAS_MODE_P2P_GO)
3385 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3388 if (ssid->passphrase == NULL ||
3389 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3390 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3394 os_strlcpy(params.passphrase, ssid->passphrase,
3395 sizeof(params.passphrase));
3396 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3397 params.ssid_len = ssid->ssid_len;
3398 params.persistent_group = 1;
3400 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3404 wpas_start_wps_go(wpa_s, ¶ms, 0);
3410 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3411 struct wpabuf *proberesp_ies)
3413 struct wpa_supplicant *wpa_s = ctx;
3414 if (wpa_s->ap_iface) {
3415 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3417 wpabuf_free(hapd->p2p_beacon_ie);
3418 hapd->p2p_beacon_ie = beacon_ies;
3420 wpabuf_free(hapd->p2p_probe_resp_ie);
3421 hapd->p2p_probe_resp_ie = proberesp_ies;
3423 wpabuf_free(beacon_ies);
3424 wpabuf_free(proberesp_ies);
3426 wpa_supplicant_ap_update_beacon(wpa_s);
3430 static void wpas_p2p_idle_update(void *ctx, int idle)
3432 struct wpa_supplicant *wpa_s = ctx;
3433 if (!wpa_s->ap_iface)
3435 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3437 wpas_p2p_set_group_idle_timeout(wpa_s);
3439 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3443 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3444 int persistent_group,
3445 int group_formation)
3447 struct p2p_group *group;
3448 struct p2p_group_config *cfg;
3450 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3452 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3455 cfg = os_zalloc(sizeof(*cfg));
3459 cfg->persistent_group = persistent_group;
3460 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3461 if (wpa_s->max_stations &&
3462 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3463 cfg->max_clients = wpa_s->max_stations;
3465 cfg->max_clients = wpa_s->conf->max_num_sta;
3466 cfg->cb_ctx = wpa_s;
3467 cfg->ie_update = wpas_p2p_ie_update;
3468 cfg->idle_update = wpas_p2p_idle_update;
3470 group = p2p_group_init(wpa_s->global->p2p, cfg);
3473 if (!group_formation)
3474 p2p_group_notif_formation_done(group);
3475 wpa_s->p2p_group = group;
3480 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3483 if (!wpa_s->p2p_in_provisioning) {
3484 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3485 "provisioning not in progress");
3489 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3491 if (wpa_s->global->p2p)
3492 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3493 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3494 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3495 wpas_group_formation_completed(wpa_s, 1);
3499 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3500 struct wps_event_fail *fail)
3502 if (!wpa_s->p2p_in_provisioning) {
3503 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3504 "provisioning not in progress");
3507 wpas_notify_p2p_wps_failed(wpa_s, fail);
3511 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3512 const char *config_method)
3516 if (os_strcmp(config_method, "display") == 0)
3517 config_methods = WPS_CONFIG_DISPLAY;
3518 else if (os_strcmp(config_method, "keypad") == 0)
3519 config_methods = WPS_CONFIG_KEYPAD;
3520 else if (os_strcmp(config_method, "pbc") == 0 ||
3521 os_strcmp(config_method, "pushbutton") == 0)
3522 config_methods = WPS_CONFIG_PUSHBUTTON;
3526 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3527 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
3531 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3534 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3539 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3542 return p2p_scan_result_text(ies, ies_len, buf, end);
3546 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3548 if (!wpa_s->pending_action_tx)
3551 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3552 "operation request");
3553 wpabuf_free(wpa_s->pending_action_tx);
3554 wpa_s->pending_action_tx = NULL;
3558 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3559 enum p2p_discovery_type type,
3560 unsigned int num_req_dev_types, const u8 *req_dev_types)
3562 wpas_p2p_clear_pending_action_tx(wpa_s);
3563 wpa_s->p2p_long_listen = 0;
3565 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3566 return wpa_drv_p2p_find(wpa_s, timeout, type);
3568 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3571 return p2p_find(wpa_s->global->p2p, timeout, type,
3572 num_req_dev_types, req_dev_types);
3576 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3578 wpas_p2p_clear_pending_action_tx(wpa_s);
3579 wpa_s->p2p_long_listen = 0;
3580 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3581 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3583 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3584 wpa_drv_p2p_stop_find(wpa_s);
3588 if (wpa_s->global->p2p)
3589 p2p_stop_find(wpa_s->global->p2p);
3591 wpas_p2p_remove_pending_group_interface(wpa_s);
3595 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3597 struct wpa_supplicant *wpa_s = eloop_ctx;
3598 wpa_s->p2p_long_listen = 0;
3602 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3606 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3609 wpas_p2p_clear_pending_action_tx(wpa_s);
3613 * This is a request for unlimited Listen state. However, at
3614 * least for now, this is mapped to a Listen state for one
3619 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3620 wpa_s->p2p_long_listen = 0;
3622 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3623 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
3624 wpa_s->p2p_long_listen = timeout * 1000;
3625 eloop_register_timeout(timeout, 0,
3626 wpas_p2p_long_listen_timeout,
3634 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3635 u8 *buf, size_t len, int p2p_group)
3637 struct wpabuf *p2p_ie;
3640 if (wpa_s->global->p2p_disabled)
3642 if (wpa_s->global->p2p == NULL)
3647 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3648 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3650 wpabuf_free(p2p_ie);
3656 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3657 const u8 *dst, const u8 *bssid,
3658 const u8 *ie, size_t ie_len)
3660 if (wpa_s->global->p2p_disabled)
3662 if (wpa_s->global->p2p == NULL)
3665 return p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
3670 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3671 const u8 *sa, const u8 *bssid,
3672 u8 category, const u8 *data, size_t len, int freq)
3674 if (wpa_s->global->p2p_disabled)
3676 if (wpa_s->global->p2p == NULL)
3679 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3684 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3686 if (wpa_s->global->p2p_disabled)
3688 if (wpa_s->global->p2p == NULL)
3691 p2p_scan_ie(wpa_s->global->p2p, ies);
3695 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3697 p2p_group_deinit(wpa_s->p2p_group);
3698 wpa_s->p2p_group = NULL;
3702 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3704 wpa_s->p2p_long_listen = 0;
3706 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3707 return wpa_drv_p2p_reject(wpa_s, addr);
3709 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3712 return p2p_reject(wpa_s->global->p2p, addr);
3716 /* Invite to reinvoke a persistent group */
3717 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3718 struct wpa_ssid *ssid, const u8 *go_dev_addr)
3720 enum p2p_invite_role role;
3723 if (ssid->mode == WPAS_MODE_P2P_GO) {
3724 role = P2P_INVITE_ROLE_GO;
3725 if (peer_addr == NULL) {
3726 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3727 "address in invitation command");
3730 if (wpas_p2p_create_iface(wpa_s)) {
3731 if (wpas_p2p_add_group_interface(wpa_s,
3732 WPA_IF_P2P_GO) < 0) {
3733 wpa_printf(MSG_ERROR, "P2P: Failed to "
3734 "allocate a new interface for the "
3738 bssid = wpa_s->pending_interface_addr;
3740 bssid = wpa_s->own_addr;
3742 role = P2P_INVITE_ROLE_CLIENT;
3743 peer_addr = ssid->bssid;
3745 wpa_s->pending_invite_ssid_id = ssid->id;
3747 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3748 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3749 ssid->ssid, ssid->ssid_len,
3752 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3755 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3756 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3760 /* Invite to join an active group */
3761 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3762 const u8 *peer_addr, const u8 *go_dev_addr)
3764 struct wpa_global *global = wpa_s->global;
3765 enum p2p_invite_role role;
3767 struct wpa_ssid *ssid;
3769 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3770 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3773 if (wpa_s == NULL) {
3774 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3778 ssid = wpa_s->current_ssid;
3780 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3785 if (ssid->mode == WPAS_MODE_P2P_GO) {
3786 role = P2P_INVITE_ROLE_ACTIVE_GO;
3787 bssid = wpa_s->own_addr;
3788 if (go_dev_addr == NULL)
3789 go_dev_addr = wpa_s->parent->own_addr;
3791 role = P2P_INVITE_ROLE_CLIENT;
3792 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3793 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3794 "invite to current group");
3797 bssid = wpa_s->bssid;
3798 if (go_dev_addr == NULL &&
3799 !is_zero_ether_addr(wpa_s->go_dev_addr))
3800 go_dev_addr = wpa_s->go_dev_addr;
3802 wpa_s->parent->pending_invite_ssid_id = -1;
3804 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3805 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3806 ssid->ssid, ssid->ssid_len,
3809 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3812 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3813 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
3818 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3820 struct wpa_ssid *ssid = wpa_s->current_ssid;
3821 const char *ssid_txt;
3822 u8 go_dev_addr[ETH_ALEN];
3823 int network_id = -1;
3826 if (!wpa_s->show_group_started || !ssid)
3829 wpa_s->show_group_started = 0;
3831 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3832 os_memset(go_dev_addr, 0, ETH_ALEN);
3833 if (ssid->bssid_set)
3834 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3835 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3837 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3839 if (wpa_s->global->p2p_group_formation == wpa_s)
3840 wpa_s->global->p2p_group_formation = NULL;
3842 if (ssid->passphrase == NULL && ssid->psk_set) {
3844 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3845 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3846 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3848 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
3849 MAC2STR(go_dev_addr),
3850 persistent ? " [PERSISTENT]" : "");
3852 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3853 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3854 "go_dev_addr=" MACSTR "%s",
3855 wpa_s->ifname, ssid_txt, ssid->frequency,
3856 ssid->passphrase ? ssid->passphrase : "",
3857 MAC2STR(go_dev_addr),
3858 persistent ? " [PERSISTENT]" : "");
3862 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
3865 network_id = ssid->id;
3866 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
3870 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3871 u32 interval1, u32 duration2, u32 interval2)
3873 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3875 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3878 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3879 wpa_s->current_ssid == NULL ||
3880 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3883 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3884 wpa_s->own_addr, wpa_s->assoc_freq,
3885 duration1, interval1, duration2, interval2);
3889 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3890 unsigned int interval)
3892 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3895 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3898 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3902 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
3904 struct wpa_supplicant *wpa_s = eloop_ctx;
3906 if (wpa_s->conf->p2p_group_idle == 0) {
3907 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
3912 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
3914 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
3915 wpas_p2p_group_delete(wpa_s);
3919 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
3921 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3922 if (wpa_s->conf->p2p_group_idle == 0)
3925 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3928 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
3929 wpa_s->conf->p2p_group_idle);
3930 eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
3931 wpas_p2p_group_idle_timeout, wpa_s, NULL);
3935 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3936 u16 reason_code, const u8 *ie, size_t ie_len)
3938 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3940 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3943 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3947 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3948 u16 reason_code, const u8 *ie, size_t ie_len)
3950 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3952 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3955 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3959 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3961 struct p2p_data *p2p = wpa_s->global->p2p;
3966 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3969 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3970 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3972 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
3973 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
3976 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
3977 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
3979 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
3980 p2p_set_uuid(p2p, wpa_s->wps->uuid);
3982 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
3983 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
3984 p2p_set_model_name(p2p, wpa_s->conf->model_name);
3985 p2p_set_model_number(p2p, wpa_s->conf->model_number);
3986 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
3989 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
3990 p2p_set_sec_dev_types(p2p,
3991 (void *) wpa_s->conf->sec_device_type,
3992 wpa_s->conf->num_sec_device_types);
3994 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
3996 p2p_remove_wps_vendor_extensions(p2p);
3997 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3998 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4000 p2p_add_wps_vendor_extension(
4001 p2p, wpa_s->conf->wps_vendor_ext[i]);
4005 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4006 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4008 country[0] = wpa_s->conf->country[0];
4009 country[1] = wpa_s->conf->country[1];
4011 p2p_set_country(p2p, country);
4014 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4015 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4016 wpa_s->conf->p2p_ssid_postfix ?
4017 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4021 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4022 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4024 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4025 u8 reg_class, channel;
4028 if (wpa_s->conf->p2p_listen_reg_class &&
4029 wpa_s->conf->p2p_listen_channel) {
4030 reg_class = wpa_s->conf->p2p_listen_reg_class;
4031 channel = wpa_s->conf->p2p_listen_channel;
4035 * Pick one of the social channels randomly as the
4038 os_get_random((u8 *) &r, sizeof(r));
4039 channel = 1 + (r % 3) * 5;
4041 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4043 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4046 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4047 u8 op_reg_class, op_channel, cfg_op_channel;
4050 if (wpa_s->conf->p2p_oper_reg_class &&
4051 wpa_s->conf->p2p_oper_channel) {
4052 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4053 op_channel = wpa_s->conf->p2p_oper_channel;
4058 * Use random operation channel from (1, 6, 11)
4059 *if no other preference is indicated.
4061 os_get_random((u8 *) &r, sizeof(r));
4062 op_channel = 1 + (r % 3) * 5;
4065 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4068 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4074 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4077 if (!wpa_s->ap_iface)
4079 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4084 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4086 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4088 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4091 wpa_s->global->cross_connection = enabled;
4092 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4095 struct wpa_supplicant *iface;
4097 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4099 if (iface->cross_connect_enabled == 0)
4102 iface->cross_connect_enabled = 0;
4103 iface->cross_connect_in_use = 0;
4104 wpa_msg(iface->parent, MSG_INFO,
4105 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4106 iface->ifname, iface->cross_connect_uplink);
4114 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4116 struct wpa_supplicant *iface;
4118 if (!uplink->global->cross_connection)
4121 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4122 if (!iface->cross_connect_enabled)
4124 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4127 if (iface->ap_iface == NULL)
4129 if (iface->cross_connect_in_use)
4132 iface->cross_connect_in_use = 1;
4133 wpa_msg(iface->parent, MSG_INFO,
4134 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4135 iface->ifname, iface->cross_connect_uplink);
4140 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4142 struct wpa_supplicant *iface;
4144 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4145 if (!iface->cross_connect_enabled)
4147 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4150 if (!iface->cross_connect_in_use)
4153 wpa_msg(iface->parent, MSG_INFO,
4154 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4155 iface->ifname, iface->cross_connect_uplink);
4156 iface->cross_connect_in_use = 0;
4161 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4163 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4164 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4165 wpa_s->cross_connect_disallowed)
4166 wpas_p2p_disable_cross_connect(wpa_s);
4168 wpas_p2p_enable_cross_connect(wpa_s);
4169 if (!wpa_s->ap_iface)
4170 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4174 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4176 wpas_p2p_disable_cross_connect(wpa_s);
4177 if (!wpa_s->ap_iface)
4178 wpas_p2p_set_group_idle_timeout(wpa_s);
4182 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4184 struct wpa_supplicant *iface;
4186 if (!wpa_s->global->cross_connection)
4189 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4192 if (iface->drv_flags &
4193 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4195 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4198 wpa_s->cross_connect_enabled = 1;
4199 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4200 sizeof(wpa_s->cross_connect_uplink));
4201 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4202 "%s to %s whenever uplink is available",
4203 wpa_s->ifname, wpa_s->cross_connect_uplink);
4205 if (iface->ap_iface || iface->current_ssid == NULL ||
4206 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4207 iface->cross_connect_disallowed ||
4208 iface->wpa_state != WPA_COMPLETED)
4211 wpa_s->cross_connect_in_use = 1;
4212 wpa_msg(wpa_s->parent, MSG_INFO,
4213 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4214 wpa_s->ifname, wpa_s->cross_connect_uplink);
4220 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4222 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4223 !wpa_s->p2p_in_provisioning)
4224 return 0; /* not P2P client operation */
4226 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4228 if (wpa_s != wpa_s->parent)
4229 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4231 if (wpa_s->global->p2p)
4232 p2p_group_formation_failed(wpa_s->global->p2p);
4234 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4235 wpa_s->parent, NULL);
4237 wpas_group_formation_completed(wpa_s, 0);
4242 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4244 struct p2p_channels chan;
4246 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4249 os_memset(&chan, 0, sizeof(chan));
4250 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4251 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4256 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4260 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4262 struct wpa_global *global = wpa_s->global;
4266 if (global->p2p == NULL)
4269 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4271 if (wpa_s->pending_interface_name[0] &&
4272 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4275 peer = p2p_get_go_neg_peer(global->p2p);
4277 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4278 MACSTR, MAC2STR(peer));
4279 p2p_unauthorize(global->p2p, peer);
4282 wpas_p2p_stop_find(wpa_s);
4284 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4285 if (wpa_s == global->p2p_group_formation &&
4286 (wpa_s->p2p_in_provisioning ||
4287 wpa_s->parent->pending_interface_type ==
4288 WPA_IF_P2P_CLIENT)) {
4289 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4290 "formation found - cancelling",
4293 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4294 wpa_s->parent, NULL);
4295 wpas_p2p_group_delete(wpa_s);
4301 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4309 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4311 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4314 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4315 "being available anymore");
4316 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
4317 wpas_p2p_group_delete(wpa_s);
4321 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4322 int freq_24, int freq_5, int freq_overall)
4324 struct p2p_data *p2p = wpa_s->global->p2p;
4325 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4327 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4331 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4334 struct p2p_data *p2p = wpa_s->global->p2p;
4336 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4339 if (hwaddr_aton(addr, peer))
4342 return p2p_unauthorize(p2p, peer);
4347 * wpas_p2p_disconnect - Disconnect from a P2P Group
4348 * @wpa_s: Pointer to wpa_supplicant data
4349 * Returns: 0 on success, -1 on failure
4351 * This can be used to disconnect from a group in which the local end is a P2P
4352 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4353 * virtual network interface was created for this group, that interface will be
4354 * removed. Otherwise, only the configured P2P group network will be removed
4355 * from the interface.
4357 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4363 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
4364 wpas_p2p_group_delete(wpa_s);
4370 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
4372 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4375 return p2p_in_progress(wpa_s->global->p2p);