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 "wpa_supplicant_i.h"
29 #include "config_ssid.h"
35 #include "wps_supplicant.h"
36 #include "p2p_supplicant.h"
40 * How many times to try to scan to find the GO before giving up on join
43 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
46 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
);
47 static struct wpa_supplicant
*
48 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
50 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
);
51 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
);
52 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
53 const u8
*dev_addr
, enum p2p_wps_method wps_method
);
54 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
);
55 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
);
56 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
);
57 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
);
60 static void wpas_p2p_scan_res_handler(struct wpa_supplicant
*wpa_s
,
61 struct wpa_scan_results
*scan_res
)
65 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
68 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS)",
71 for (i
= 0; i
< scan_res
->num
; i
++) {
72 struct wpa_scan_res
*bss
= scan_res
->res
[i
];
73 if (p2p_scan_res_handler(wpa_s
->global
->p2p
, bss
->bssid
,
74 bss
->freq
, bss
->level
,
75 (const u8
*) (bss
+ 1),
80 p2p_scan_res_handled(wpa_s
->global
->p2p
);
84 static int wpas_p2p_scan(void *ctx
, enum p2p_scan_type type
, int freq
,
85 unsigned int num_req_dev_types
,
86 const u8
*req_dev_types
)
88 struct wpa_supplicant
*wpa_s
= ctx
;
89 struct wpa_driver_scan_params params
;
91 struct wpabuf
*wps_ie
, *ies
;
92 int social_channels
[] = { 2412, 2437, 2462, 0, 0 };
94 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
97 os_memset(¶ms
, 0, sizeof(params
));
99 /* P2P Wildcard SSID */
100 params
.num_ssids
= 1;
101 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
102 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
104 wpa_s
->wps
->dev
.p2p
= 1;
105 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
107 num_req_dev_types
, req_dev_types
);
111 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + 100);
116 wpabuf_put_buf(ies
, wps_ie
);
119 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
121 params
.extra_ies
= wpabuf_head(ies
);
122 params
.extra_ies_len
= wpabuf_len(ies
);
125 case P2P_SCAN_SOCIAL
:
126 params
.freqs
= social_channels
;
130 case P2P_SCAN_SPECIFIC
:
131 social_channels
[0] = freq
;
132 social_channels
[1] = 0;
133 params
.freqs
= social_channels
;
135 case P2P_SCAN_SOCIAL_PLUS_ONE
:
136 social_channels
[3] = freq
;
137 params
.freqs
= social_channels
;
141 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
142 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
143 ret
= ieee80211_sta_req_scan(wpa_s
, ¶ms
);
145 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
153 #ifdef CONFIG_CLIENT_MLME
154 static void p2p_rx_action_mlme(void *ctx
, const u8
*buf
, size_t len
, int freq
)
156 struct wpa_supplicant
*wpa_s
= ctx
;
157 const struct ieee80211_mgmt
*mgmt
;
160 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
162 mgmt
= (const struct ieee80211_mgmt
*) buf
;
163 hdr_len
= (const u8
*) &mgmt
->u
.action
.u
.vs_public_action
.action
- buf
;
166 p2p_rx_action(wpa_s
->global
->p2p
, mgmt
->da
, mgmt
->sa
, mgmt
->bssid
,
167 mgmt
->u
.action
.category
,
168 &mgmt
->u
.action
.u
.vs_public_action
.action
,
169 len
- hdr_len
, freq
);
171 #endif /* CONFIG_CLIENT_MLME */
174 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
176 switch (p2p_group_interface
) {
177 case P2P_GROUP_INTERFACE_PENDING
:
178 return WPA_IF_P2P_GROUP
;
179 case P2P_GROUP_INTERFACE_GO
:
180 return WPA_IF_P2P_GO
;
181 case P2P_GROUP_INTERFACE_CLIENT
:
182 return WPA_IF_P2P_CLIENT
;
185 return WPA_IF_P2P_GROUP
;
189 static struct wpa_supplicant
* wpas_get_p2p_group(struct wpa_supplicant
*wpa_s
,
191 size_t ssid_len
, int *go
)
195 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
196 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
197 if (s
->disabled
!= 0 || !s
->p2p_group
||
198 s
->ssid_len
!= ssid_len
||
199 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
201 if (s
->mode
== WPAS_MODE_P2P_GO
&&
202 s
!= wpa_s
->current_ssid
)
205 *go
= s
->mode
== WPAS_MODE_P2P_GO
;
214 static void wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
)
216 struct wpa_ssid
*ssid
;
220 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
222 ssid
= wpa_s
->current_ssid
;
225 * The current SSID was not known, but there may still be a
226 * pending P2P group interface waiting for provisioning.
228 ssid
= wpa_s
->conf
->ssid
;
230 if (ssid
->p2p_group
&&
231 (ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
232 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)))
237 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
239 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
240 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
241 wpa_s
->reassociate
= 0;
242 wpa_s
->disconnected
= 1;
243 wpa_supplicant_deauthenticate(wpa_s
,
244 WLAN_REASON_DEAUTH_LEAVING
);
248 if (wpa_s
->cross_connect_in_use
) {
249 wpa_s
->cross_connect_in_use
= 0;
250 wpa_msg(wpa_s
->parent
, MSG_INFO
,
251 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
252 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
254 switch (wpa_s
->removal_reason
) {
255 case P2P_GROUP_REMOVAL_REQUESTED
:
256 reason
= " reason=REQUESTED";
258 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
259 reason
= " reason=IDLE";
261 case P2P_GROUP_REMOVAL_UNAVAILABLE
:
262 reason
= " reason=UNAVAILABLE";
268 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_REMOVED
"%s %s%s",
269 wpa_s
->ifname
, gtype
, reason
);
272 wpas_notify_p2p_group_removed(wpa_s
, ssid
, gtype
);
274 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
275 struct wpa_global
*global
;
277 enum wpa_driver_if_type type
;
278 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
280 global
= wpa_s
->global
;
281 ifname
= os_strdup(wpa_s
->ifname
);
282 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
283 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
);
284 wpa_s
= global
->ifaces
;
286 wpa_drv_if_remove(wpa_s
, type
, ifname
);
291 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
292 if (ssid
&& (ssid
->p2p_group
||
293 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
294 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
296 if (ssid
== wpa_s
->current_ssid
)
297 wpa_s
->current_ssid
= NULL
;
298 wpas_notify_network_removed(wpa_s
, ssid
);
299 wpa_config_remove_network(wpa_s
->conf
, id
);
300 wpa_supplicant_clear_status(wpa_s
);
302 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
305 wpa_supplicant_ap_deinit(wpa_s
);
309 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
311 const u8
*ssid
, size_t ssid_len
)
319 if (wpa_s
->go_params
)
320 bssid
= wpa_s
->go_params
->peer_interface_addr
;
322 bssid
= wpa_s
->bssid
;
324 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
326 u8 iface_addr
[ETH_ALEN
];
327 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
329 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
332 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
333 "group is persistent - BSS " MACSTR
" not found",
338 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
340 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
341 "group is persistent - BSS " MACSTR
342 " did not include P2P IE", MAC2STR(bssid
));
343 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
344 (u8
*) (bss
+ 1), bss
->ie_len
);
345 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
346 ((u8
*) bss
+ 1) + bss
->ie_len
,
351 group_capab
= p2p_get_group_capab(p2p
);
352 addr
= p2p_get_go_dev_addr(p2p
);
353 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
354 "group_capab=0x%x", group_capab
);
356 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
357 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
360 os_memset(go_dev_addr
, 0, ETH_ALEN
);
363 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
364 "go_dev_addr=" MACSTR
,
365 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
367 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
371 static void wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
372 struct wpa_ssid
*ssid
,
373 const u8
*go_dev_addr
)
378 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
379 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
380 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
381 if (s
->disabled
== 2 &&
382 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
383 s
->ssid_len
== ssid
->ssid_len
&&
384 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
389 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
391 if (ssid
->passphrase
&& !s
->passphrase
)
393 else if (ssid
->passphrase
&& s
->passphrase
&&
394 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
397 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
400 s
= wpa_config_add_network(wpa_s
->conf
);
403 wpa_config_set_network_defaults(s
);
407 s
->p2p_persistent_group
= 1;
410 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
411 s
->mode
= ssid
->mode
;
412 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
413 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
414 s
->proto
= WPA_PROTO_RSN
;
415 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
417 if (ssid
->passphrase
) {
418 os_free(s
->passphrase
);
419 s
->passphrase
= os_strdup(ssid
->passphrase
);
423 os_memcpy(s
->psk
, ssid
->psk
, 32);
425 if (s
->passphrase
&& !s
->psk_set
)
426 wpa_config_update_psk(s
);
427 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
429 s
->ssid
= os_malloc(ssid
->ssid_len
);
432 s
->ssid_len
= ssid
->ssid_len
;
433 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
436 #ifndef CONFIG_NO_CONFIG_WRITE
437 if (changed
&& wpa_s
->conf
->update_config
&&
438 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
439 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
441 #endif /* CONFIG_NO_CONFIG_WRITE */
445 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
448 struct wpa_ssid
*ssid
;
449 const char *ssid_txt
;
452 u8 go_dev_addr
[ETH_ALEN
];
455 * This callback is likely called for the main interface. Update wpa_s
456 * to use the group interface if a new interface was created for the
459 if (wpa_s
->global
->p2p_group_formation
)
460 wpa_s
= wpa_s
->global
->p2p_group_formation
;
461 wpa_s
->global
->p2p_group_formation
= NULL
;
462 wpa_s
->p2p_in_provisioning
= 0;
465 wpa_msg(wpa_s
->parent
, MSG_INFO
,
466 P2P_EVENT_GROUP_FORMATION_FAILURE
);
467 wpas_p2p_group_delete(wpa_s
);
471 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_FORMATION_SUCCESS
);
473 ssid
= wpa_s
->current_ssid
;
474 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
475 ssid
->mode
= WPAS_MODE_P2P_GO
;
476 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
477 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
482 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
483 client
= ssid
->mode
== WPAS_MODE_INFRA
;
484 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
485 persistent
= ssid
->p2p_persistent_group
;
486 os_memcpy(go_dev_addr
, wpa_s
->parent
->own_addr
,
489 persistent
= wpas_p2p_persistent_group(wpa_s
,
495 client
= wpa_s
->p2p_group_interface
==
496 P2P_GROUP_INTERFACE_CLIENT
;
499 wpa_s
->show_group_started
= 0;
502 * Indicate event only after successfully completed 4-way
503 * handshake, i.e., when the interface is ready for data
506 wpa_s
->show_group_started
= 1;
507 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
509 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
510 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
511 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
513 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
514 MAC2STR(go_dev_addr
),
515 persistent
? " [PERSISTENT]" : "");
516 wpas_p2p_cross_connect_setup(wpa_s
);
517 wpas_p2p_set_group_idle_timeout(wpa_s
);
519 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
520 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
521 "go_dev_addr=" MACSTR
"%s",
522 wpa_s
->ifname
, ssid_txt
, ssid
? ssid
->frequency
: 0,
523 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
524 MAC2STR(go_dev_addr
),
525 persistent
? " [PERSISTENT]" : "");
526 wpas_p2p_cross_connect_setup(wpa_s
);
527 wpas_p2p_set_group_idle_timeout(wpa_s
);
531 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
536 static struct wpa_supplicant
*
537 wpas_get_tx_interface(struct wpa_supplicant
*wpa_s
, const u8
*src
)
539 struct wpa_supplicant
*iface
;
541 if (os_memcmp(src
, wpa_s
->own_addr
, ETH_ALEN
) == 0)
545 * Try to find a group interface that matches with the source address.
547 iface
= wpa_s
->global
->ifaces
;
549 if (os_memcmp(wpa_s
->pending_action_src
,
550 iface
->own_addr
, ETH_ALEN
) == 0)
555 wpa_printf(MSG_DEBUG
, "P2P: Use group interface %s "
556 "instead of interface %s for Action TX",
557 iface
->ifname
, wpa_s
->ifname
);
565 static void wpas_send_action_cb(void *eloop_ctx
, void *timeout_ctx
)
567 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
568 struct wpa_supplicant
*iface
;
572 without_roc
= wpa_s
->pending_action_without_roc
;
573 wpa_s
->pending_action_without_roc
= 0;
574 wpa_printf(MSG_DEBUG
, "P2P: Send Action callback (without_roc=%d "
575 "pending_action_tx=%p)",
576 without_roc
, wpa_s
->pending_action_tx
);
578 if (wpa_s
->pending_action_tx
== NULL
)
582 * This call is likely going to be on the P2P device instance if the
583 * driver uses a separate interface for that purpose. However, some
584 * Action frames are actually sent within a P2P Group and when that is
585 * the case, we need to follow power saving (e.g., GO buffering the
586 * frame for a client in PS mode or a client following the advertised
587 * NoA from its GO). To make that easier for the driver, select the
588 * correct group interface here.
590 iface
= wpas_get_tx_interface(wpa_s
, wpa_s
->pending_action_src
);
592 if (wpa_s
->off_channel_freq
!= wpa_s
->pending_action_freq
&&
593 wpa_s
->pending_action_freq
!= 0 &&
594 wpa_s
->pending_action_freq
!= iface
->assoc_freq
) {
595 wpa_printf(MSG_DEBUG
, "P2P: Pending Action frame TX "
596 "waiting for another freq=%u (off_channel_freq=%u "
598 wpa_s
->pending_action_freq
,
599 wpa_s
->off_channel_freq
,
601 if (without_roc
&& wpa_s
->off_channel_freq
== 0) {
603 * We may get here if wpas_send_action() found us to be
604 * on the correct channel, but remain-on-channel cancel
605 * event was received before getting here.
607 wpa_printf(MSG_DEBUG
, "P2P: Schedule "
608 "remain-on-channel to send Action frame");
609 if (wpa_drv_remain_on_channel(
610 wpa_s
, wpa_s
->pending_action_freq
, 200) <
612 wpa_printf(MSG_DEBUG
, "P2P: Failed to request "
613 "driver to remain on channel (%u "
614 "MHz) for Action Frame TX",
615 wpa_s
->pending_action_freq
);
617 wpa_s
->off_channel_freq
= 0;
618 wpa_s
->roc_waiting_drv_freq
=
619 wpa_s
->pending_action_freq
;
625 wpa_printf(MSG_DEBUG
, "P2P: Sending pending Action frame to "
626 MACSTR
" using interface %s",
627 MAC2STR(wpa_s
->pending_action_dst
), iface
->ifname
);
628 res
= wpa_drv_send_action(iface
, wpa_s
->pending_action_freq
, 0,
629 wpa_s
->pending_action_dst
,
630 wpa_s
->pending_action_src
,
631 wpa_s
->pending_action_bssid
,
632 wpabuf_head(wpa_s
->pending_action_tx
),
633 wpabuf_len(wpa_s
->pending_action_tx
));
635 wpa_printf(MSG_DEBUG
, "P2P: Failed to send the pending "
638 * Use fake TX status event to allow P2P state machine to
641 wpas_send_action_tx_status(
642 wpa_s
, wpa_s
->pending_action_dst
,
643 wpabuf_head(wpa_s
->pending_action_tx
),
644 wpabuf_len(wpa_s
->pending_action_tx
),
645 P2P_SEND_ACTION_FAILED
);
650 void wpas_send_action_tx_status(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
651 const u8
*data
, size_t data_len
,
652 enum p2p_send_action_result result
)
654 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
656 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
659 if (wpa_s
->pending_action_tx
== NULL
) {
660 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - no "
661 "pending operation");
665 if (os_memcmp(dst
, wpa_s
->pending_action_dst
, ETH_ALEN
) != 0) {
666 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - unknown "
667 "destination address");
671 wpabuf_free(wpa_s
->pending_action_tx
);
672 wpa_s
->pending_action_tx
= NULL
;
674 p2p_send_action_cb(wpa_s
->global
->p2p
, wpa_s
->pending_action_freq
,
675 wpa_s
->pending_action_dst
,
676 wpa_s
->pending_action_src
,
677 wpa_s
->pending_action_bssid
,
680 if (wpa_s
->pending_pd_before_join
&&
681 (os_memcmp(wpa_s
->pending_action_dst
, wpa_s
->pending_join_dev_addr
,
683 os_memcmp(wpa_s
->pending_action_dst
,
684 wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
685 wpa_s
->pending_pd_before_join
= 0;
686 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
687 "join-existing-group operation");
688 wpas_p2p_join_start(wpa_s
);
693 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
694 const u8
*src
, const u8
*bssid
, const u8
*buf
,
695 size_t len
, unsigned int wait_time
)
697 struct wpa_supplicant
*wpa_s
= ctx
;
699 wpa_printf(MSG_DEBUG
, "P2P: Send action frame: freq=%d dst=" MACSTR
700 " src=" MACSTR
" bssid=" MACSTR
" len=%d",
701 freq
, MAC2STR(dst
), MAC2STR(src
), MAC2STR(bssid
),
704 if (wpa_s
->pending_action_tx
) {
705 wpa_printf(MSG_DEBUG
, "P2P: Dropped pending Action frame TX "
706 "to " MACSTR
, MAC2STR(wpa_s
->pending_action_dst
));
707 wpabuf_free(wpa_s
->pending_action_tx
);
709 wpa_s
->pending_action_tx
= wpabuf_alloc(len
);
710 if (wpa_s
->pending_action_tx
== NULL
) {
711 wpa_printf(MSG_DEBUG
, "P2P: Failed to allocate Action frame "
712 "TX buffer (len=%llu)", (unsigned long long) len
);
715 wpabuf_put_data(wpa_s
->pending_action_tx
, buf
, len
);
716 os_memcpy(wpa_s
->pending_action_src
, src
, ETH_ALEN
);
717 os_memcpy(wpa_s
->pending_action_dst
, dst
, ETH_ALEN
);
718 os_memcpy(wpa_s
->pending_action_bssid
, bssid
, ETH_ALEN
);
719 wpa_s
->pending_action_freq
= freq
;
721 if (freq
!= 0 && wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_OFFCHANNEL_TX
) {
722 struct wpa_supplicant
*iface
;
724 iface
= wpas_get_tx_interface(wpa_s
, wpa_s
->pending_action_src
);
725 wpa_s
->action_tx_wait_time
= wait_time
;
727 return wpa_drv_send_action(iface
, wpa_s
->pending_action_freq
,
728 wait_time
, wpa_s
->pending_action_dst
,
729 wpa_s
->pending_action_src
,
730 wpa_s
->pending_action_bssid
,
731 wpabuf_head(wpa_s
->pending_action_tx
),
732 wpabuf_len(wpa_s
->pending_action_tx
));
736 struct wpa_supplicant
*tx_iface
;
737 tx_iface
= wpas_get_tx_interface(wpa_s
, src
);
738 if (tx_iface
->assoc_freq
== freq
) {
739 wpa_printf(MSG_DEBUG
, "P2P: Already on requested "
740 "channel (TX interface operating channel)");
745 if (wpa_s
->off_channel_freq
== freq
|| freq
== 0) {
746 wpa_printf(MSG_DEBUG
, "P2P: Already on requested channel; "
747 "send Action frame immediately");
748 /* TODO: Would there ever be need to extend the current
749 * duration on the channel? */
750 wpa_s
->pending_action_without_roc
= 1;
751 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
752 eloop_register_timeout(0, 0, wpas_send_action_cb
, wpa_s
, NULL
);
755 wpa_s
->pending_action_without_roc
= 0;
757 if (wpa_s
->roc_waiting_drv_freq
== freq
) {
758 wpa_printf(MSG_DEBUG
, "P2P: Already waiting for driver to get "
759 "to frequency %u MHz; continue waiting to send the "
760 "Action frame", freq
);
764 wpa_printf(MSG_DEBUG
, "P2P: Schedule Action frame to be transmitted "
765 "once the driver gets to the requested channel");
766 if (wait_time
> wpa_s
->max_remain_on_chan
)
767 wait_time
= wpa_s
->max_remain_on_chan
;
768 if (wpa_drv_remain_on_channel(wpa_s
, freq
, wait_time
) < 0) {
769 wpa_printf(MSG_DEBUG
, "P2P: Failed to request driver "
770 "to remain on channel (%u MHz) for Action "
774 wpa_s
->off_channel_freq
= 0;
775 wpa_s
->roc_waiting_drv_freq
= freq
;
781 static void wpas_send_action_done(void *ctx
)
783 struct wpa_supplicant
*wpa_s
= ctx
;
784 wpa_printf(MSG_DEBUG
, "P2P: Action frame sequence done notification");
785 wpabuf_free(wpa_s
->pending_action_tx
);
786 wpa_s
->pending_action_tx
= NULL
;
787 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_OFFCHANNEL_TX
) {
788 if (wpa_s
->action_tx_wait_time
)
789 wpa_drv_send_action_cancel_wait(wpa_s
);
790 wpa_s
->off_channel_freq
= 0;
791 } else if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
792 wpa_drv_cancel_remain_on_channel(wpa_s
);
793 wpa_s
->off_channel_freq
= 0;
794 wpa_s
->roc_waiting_drv_freq
= 0;
799 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
800 struct p2p_go_neg_results
*params
)
802 if (wpa_s
->go_params
== NULL
) {
803 wpa_s
->go_params
= os_malloc(sizeof(*params
));
804 if (wpa_s
->go_params
== NULL
)
807 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
812 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
813 struct p2p_go_neg_results
*res
)
815 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
816 MAC2STR(res
->peer_interface_addr
));
817 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
818 res
->ssid
, res
->ssid_len
);
819 wpa_supplicant_ap_deinit(wpa_s
);
820 wpas_copy_go_neg_results(wpa_s
, res
);
821 if (res
->wps_method
== WPS_PBC
)
822 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
824 u16 dev_pw_id
= DEV_PW_DEFAULT
;
825 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
826 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
827 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
828 wpa_s
->p2p_pin
, 1, dev_pw_id
);
833 static void p2p_go_configured(void *ctx
, void *data
)
835 struct wpa_supplicant
*wpa_s
= ctx
;
836 struct p2p_go_neg_results
*params
= data
;
837 struct wpa_ssid
*ssid
;
839 ssid
= wpa_s
->current_ssid
;
840 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
841 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
842 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
843 wpa_s
->global
->p2p_group_formation
= NULL
;
844 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
845 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
846 "go_dev_addr=" MACSTR
"%s",
848 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
850 params
->passphrase
? params
->passphrase
: "",
851 MAC2STR(wpa_s
->parent
->own_addr
),
852 params
->persistent_group
? " [PERSISTENT]" : "");
853 if (params
->persistent_group
)
854 wpas_p2p_store_persistent_group(
856 wpa_s
->parent
->own_addr
);
857 wpas_p2p_cross_connect_setup(wpa_s
);
858 wpas_p2p_set_group_idle_timeout(wpa_s
);
862 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
863 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
864 params
->peer_interface_addr
)) {
865 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
869 if (params
->wps_method
== WPS_PBC
)
870 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
,
872 else if (wpa_s
->p2p_pin
[0])
873 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
874 wpa_s
->p2p_pin
, NULL
, 0);
875 os_free(wpa_s
->go_params
);
876 wpa_s
->go_params
= NULL
;
880 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
881 struct p2p_go_neg_results
*params
,
884 struct wpa_ssid
*ssid
;
886 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0)
889 ssid
= wpa_config_add_network(wpa_s
->conf
);
893 wpas_notify_network_added(wpa_s
, ssid
);
894 wpa_config_set_network_defaults(ssid
);
897 ssid
->p2p_persistent_group
= params
->persistent_group
;
898 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
900 ssid
->frequency
= params
->freq
;
901 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
903 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
904 ssid
->ssid_len
= params
->ssid_len
;
906 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
907 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
908 ssid
->proto
= WPA_PROTO_RSN
;
909 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
910 ssid
->passphrase
= os_strdup(params
->passphrase
);
912 wpa_s
->ap_configured_cb
= p2p_go_configured
;
913 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
914 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
915 wpa_s
->connect_without_scan
= 1;
916 wpa_s
->reassociate
= 1;
917 wpa_s
->disconnected
= 0;
918 wpa_supplicant_req_scan(wpa_s
, 0, 0);
922 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
923 const struct wpa_supplicant
*src
)
925 struct wpa_config
*d
;
926 const struct wpa_config
*s
;
931 #define C(n) if (s->n) d->n = os_strdup(s->n)
940 os_memcpy(d
->device_type
, s
->device_type
, WPS_DEV_TYPE_LEN
);
941 os_memcpy(d
->sec_device_type
, s
->sec_device_type
,
942 sizeof(d
->sec_device_type
));
943 d
->num_sec_device_types
= s
->num_sec_device_types
;
945 d
->p2p_group_idle
= s
->p2p_group_idle
;
946 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
950 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
951 enum wpa_driver_if_type type
)
953 char ifname
[120], force_ifname
[120];
955 if (wpa_s
->pending_interface_name
[0]) {
956 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
957 "- skip creation of a new one");
958 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
959 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
960 "unknown?! ifname='%s'",
961 wpa_s
->pending_interface_name
);
967 os_snprintf(ifname
, sizeof(ifname
), "p2p-%s-%d", wpa_s
->ifname
,
968 wpa_s
->p2p_group_idx
);
969 if (os_strlen(ifname
) >= IFNAMSIZ
&&
970 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
971 /* Try to avoid going over the IFNAMSIZ length limit */
972 os_snprintf(ifname
, sizeof(ifname
), "p2p-%d",
973 wpa_s
->p2p_group_idx
);
975 force_ifname
[0] = '\0';
977 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
979 wpa_s
->p2p_group_idx
++;
981 wpa_s
->pending_interface_type
= type
;
982 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
983 wpa_s
->pending_interface_addr
, NULL
) < 0) {
984 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
989 if (force_ifname
[0]) {
990 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
992 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
993 sizeof(wpa_s
->pending_interface_name
));
995 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
996 sizeof(wpa_s
->pending_interface_name
));
997 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
998 MACSTR
, wpa_s
->pending_interface_name
,
999 MAC2STR(wpa_s
->pending_interface_addr
));
1005 static void wpas_p2p_remove_pending_group_interface(
1006 struct wpa_supplicant
*wpa_s
)
1008 if (!wpa_s
->pending_interface_name
[0] ||
1009 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
1010 return; /* No pending virtual interface */
1012 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
1013 wpa_s
->pending_interface_name
);
1014 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
1015 wpa_s
->pending_interface_name
);
1016 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1017 wpa_s
->pending_interface_name
[0] = '\0';
1021 static struct wpa_supplicant
*
1022 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
1024 struct wpa_interface iface
;
1025 struct wpa_supplicant
*group_wpa_s
;
1027 if (!wpa_s
->pending_interface_name
[0]) {
1028 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
1029 if (!wpas_p2p_create_iface(wpa_s
))
1032 * Something has forced us to remove the pending interface; try
1033 * to create a new one and hope for the best that we will get
1034 * the same local address.
1036 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
1037 WPA_IF_P2P_CLIENT
) < 0)
1041 os_memset(&iface
, 0, sizeof(iface
));
1042 iface
.ifname
= wpa_s
->pending_interface_name
;
1043 iface
.driver
= wpa_s
->driver
->name
;
1044 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
1045 iface
.driver_param
= wpa_s
->conf
->driver_param
;
1046 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
1047 if (group_wpa_s
== NULL
) {
1048 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
1049 "wpa_supplicant interface");
1052 wpa_s
->pending_interface_name
[0] = '\0';
1053 group_wpa_s
->parent
= wpa_s
;
1054 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
1055 P2P_GROUP_INTERFACE_CLIENT
;
1056 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
1058 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
1064 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
1067 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
1068 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
1069 if (wpa_s
->global
->p2p
)
1070 p2p_group_formation_failed(wpa_s
->global
->p2p
);
1071 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1072 wpa_drv_p2p_group_formation_failed(wpa_s
);
1073 wpas_group_formation_completed(wpa_s
, 0);
1077 void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
1079 struct wpa_supplicant
*wpa_s
= ctx
;
1081 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1082 wpa_drv_cancel_remain_on_channel(wpa_s
);
1083 wpa_s
->off_channel_freq
= 0;
1084 wpa_s
->roc_waiting_drv_freq
= 0;
1088 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_FAILURE
"status=%d",
1090 wpas_notify_p2p_go_neg_completed(wpa_s
, res
->status
);
1091 wpas_p2p_remove_pending_group_interface(wpa_s
);
1095 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
);
1096 wpas_notify_p2p_go_neg_completed(wpa_s
, P2P_SC_SUCCESS
);
1098 if (wpa_s
->create_p2p_iface
) {
1099 struct wpa_supplicant
*group_wpa_s
=
1100 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
1101 if (group_wpa_s
== NULL
) {
1102 wpas_p2p_remove_pending_group_interface(wpa_s
);
1105 if (group_wpa_s
!= wpa_s
) {
1106 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
1107 sizeof(group_wpa_s
->p2p_pin
));
1108 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
1110 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1111 wpa_s
->pending_interface_name
[0] = '\0';
1112 group_wpa_s
->p2p_in_provisioning
= 1;
1115 wpas_start_wps_go(group_wpa_s
, res
, 1);
1117 wpas_start_wps_enrollee(group_wpa_s
, res
);
1119 wpa_s
->p2p_in_provisioning
= 1;
1120 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1123 wpas_start_wps_go(wpa_s
, res
, 1);
1125 wpas_start_wps_enrollee(ctx
, res
);
1128 wpa_s
->p2p_long_listen
= 0;
1129 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1131 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1132 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1133 (res
->peer_config_timeout
% 100) * 10000,
1134 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1138 void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1140 struct wpa_supplicant
*wpa_s
= ctx
;
1141 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1142 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1144 wpas_notify_p2p_go_neg_req(wpa_s
, src
, dev_passwd_id
);
1148 void wpas_dev_found(void *ctx
, const u8
*addr
,
1149 const struct p2p_peer_info
*info
,
1152 struct wpa_supplicant
*wpa_s
= ctx
;
1153 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1155 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1156 " p2p_dev_addr=" MACSTR
1157 " pri_dev_type=%s name='%s' config_methods=0x%x "
1158 "dev_capab=0x%x group_capab=0x%x",
1159 MAC2STR(addr
), MAC2STR(info
->p2p_device_addr
),
1160 wps_dev_type_bin2str(info
->pri_dev_type
, devtype
,
1162 info
->device_name
, info
->config_methods
,
1163 info
->dev_capab
, info
->group_capab
);
1165 wpas_notify_p2p_device_found(ctx
, info
->p2p_device_addr
, new_device
);
1169 static void wpas_dev_lost(void *ctx
, const u8
*dev_addr
)
1171 struct wpa_supplicant
*wpa_s
= ctx
;
1173 wpas_notify_p2p_device_lost(wpa_s
, dev_addr
);
1177 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1178 unsigned int duration
,
1179 const struct wpabuf
*probe_resp_ie
)
1181 struct wpa_supplicant
*wpa_s
= ctx
;
1183 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, probe_resp_ie
, NULL
);
1185 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1186 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1187 "report received Probe Request frames");
1191 wpa_s
->pending_listen_freq
= freq
;
1192 wpa_s
->pending_listen_duration
= duration
;
1194 if (wpa_drv_remain_on_channel(wpa_s
, freq
, duration
) < 0) {
1195 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1196 "to remain on channel (%u MHz) for Listen "
1198 wpa_s
->pending_listen_freq
= 0;
1201 wpa_s
->off_channel_freq
= 0;
1202 wpa_s
->roc_waiting_drv_freq
= freq
;
1208 static void wpas_stop_listen(void *ctx
)
1210 struct wpa_supplicant
*wpa_s
= ctx
;
1211 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1212 wpa_drv_cancel_remain_on_channel(wpa_s
);
1213 wpa_s
->off_channel_freq
= 0;
1214 wpa_s
->roc_waiting_drv_freq
= 0;
1216 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, NULL
, NULL
);
1217 wpa_drv_probe_req_report(wpa_s
, 0);
1221 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1223 struct wpa_supplicant
*wpa_s
= ctx
;
1224 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
));
1228 static struct p2p_srv_bonjour
*
1229 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1230 const struct wpabuf
*query
)
1232 struct p2p_srv_bonjour
*bsrv
;
1235 len
= wpabuf_len(query
);
1236 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1237 struct p2p_srv_bonjour
, list
) {
1238 if (len
== wpabuf_len(bsrv
->query
) &&
1239 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1247 static struct p2p_srv_upnp
*
1248 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1249 const char *service
)
1251 struct p2p_srv_upnp
*usrv
;
1253 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1254 struct p2p_srv_upnp
, list
) {
1255 if (version
== usrv
->version
&&
1256 os_strcmp(service
, usrv
->service
) == 0)
1263 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1268 if (wpabuf_tailroom(resp
) < 5)
1271 /* Length (to be filled) */
1272 len_pos
= wpabuf_put(resp
, 2);
1273 wpabuf_put_u8(resp
, srv_proto
);
1274 wpabuf_put_u8(resp
, srv_trans_id
);
1276 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1277 /* Response Data: empty */
1278 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1282 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1283 struct wpabuf
*resp
, u8 srv_trans_id
)
1285 struct p2p_srv_bonjour
*bsrv
;
1288 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1290 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1291 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1295 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1296 struct p2p_srv_bonjour
, list
) {
1297 if (wpabuf_tailroom(resp
) <
1298 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1300 /* Length (to be filled) */
1301 len_pos
= wpabuf_put(resp
, 2);
1302 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1303 wpabuf_put_u8(resp
, srv_trans_id
);
1305 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1306 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1307 wpabuf_head(bsrv
->resp
),
1308 wpabuf_len(bsrv
->resp
));
1310 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1311 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1312 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1318 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
1319 struct wpabuf
*resp
, u8 srv_trans_id
,
1320 const u8
*query
, size_t query_len
)
1322 struct p2p_srv_bonjour
*bsrv
;
1326 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
1328 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1329 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1330 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
1335 if (query_len
== 0) {
1336 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1340 if (wpabuf_tailroom(resp
) < 5)
1342 /* Length (to be filled) */
1343 len_pos
= wpabuf_put(resp
, 2);
1344 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1345 wpabuf_put_u8(resp
, srv_trans_id
);
1347 wpabuf_set(&buf
, query
, query_len
);
1348 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, &buf
);
1350 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
1354 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1355 /* Response Data: empty */
1356 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1362 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1363 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1364 wpabuf_head(bsrv
->resp
), wpabuf_len(bsrv
->resp
));
1366 if (wpabuf_tailroom(resp
) >=
1367 wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
)) {
1369 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1370 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1372 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1376 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
1377 struct wpabuf
*resp
, u8 srv_trans_id
)
1379 struct p2p_srv_upnp
*usrv
;
1382 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
1384 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1385 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1389 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1390 struct p2p_srv_upnp
, list
) {
1391 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
1394 /* Length (to be filled) */
1395 len_pos
= wpabuf_put(resp
, 2);
1396 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1397 wpabuf_put_u8(resp
, srv_trans_id
);
1400 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1402 wpabuf_put_u8(resp
, usrv
->version
);
1403 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1405 wpabuf_put_str(resp
, usrv
->service
);
1406 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1412 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
1413 struct wpabuf
*resp
, u8 srv_trans_id
,
1414 const u8
*query
, size_t query_len
)
1416 struct p2p_srv_upnp
*usrv
;
1422 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
1425 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1426 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1427 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
1432 if (query_len
== 0) {
1433 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1437 if (wpabuf_tailroom(resp
) < 5)
1440 /* Length (to be filled) */
1441 len_pos
= wpabuf_put(resp
, 2);
1442 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1443 wpabuf_put_u8(resp
, srv_trans_id
);
1446 str
= os_malloc(query_len
);
1449 os_memcpy(str
, query
+ 1, query_len
- 1);
1450 str
[query_len
- 1] = '\0';
1452 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1453 struct p2p_srv_upnp
, list
) {
1454 if (version
!= usrv
->version
)
1457 if (os_strcmp(str
, "ssdp:all") != 0 &&
1458 os_strstr(usrv
->service
, str
) == NULL
)
1461 if (wpabuf_tailroom(resp
) < 2)
1465 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1467 wpabuf_put_u8(resp
, version
);
1469 wpabuf_put_u8(resp
, ',');
1473 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1475 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
1477 wpabuf_put_str(resp
, usrv
->service
);
1481 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
1484 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1485 /* Response Data: empty */
1488 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1492 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
1493 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
1495 struct wpa_supplicant
*wpa_s
= ctx
;
1496 const u8
*pos
= tlvs
;
1497 const u8
*end
= tlvs
+ tlvs_len
;
1500 struct wpabuf
*resp
;
1501 u8 srv_proto
, srv_trans_id
;
1505 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
1507 buf_len
= 2 * tlvs_len
+ 1;
1508 buf
= os_malloc(buf_len
);
1510 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1511 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
1513 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
1518 if (wpa_s
->p2p_sd_over_ctrl_iface
)
1519 return; /* to be processed by an external program */
1521 resp
= wpabuf_alloc(10000);
1525 while (pos
+ 1 < end
) {
1526 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
1527 slen
= WPA_GET_LE16(pos
);
1529 if (pos
+ slen
> end
|| slen
< 2) {
1530 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
1535 tlv_end
= pos
+ slen
;
1538 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1540 srv_trans_id
= *pos
++;
1541 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1544 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
1545 pos
, tlv_end
- pos
);
1548 if (wpa_s
->force_long_sd
) {
1549 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
1551 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1552 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1556 switch (srv_proto
) {
1557 case P2P_SERV_ALL_SERVICES
:
1558 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
1559 "for all services");
1560 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
1561 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1562 wpa_printf(MSG_DEBUG
, "P2P: No service "
1563 "discovery protocols available");
1564 wpas_sd_add_proto_not_avail(
1565 resp
, P2P_SERV_ALL_SERVICES
,
1569 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1570 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1572 case P2P_SERV_BONJOUR
:
1573 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
1574 pos
, tlv_end
- pos
);
1577 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
1578 pos
, tlv_end
- pos
);
1581 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
1582 "protocol %u", srv_proto
);
1583 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
1592 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
1593 update_indic
, tlvs
, tlvs_len
);
1595 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
1601 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
1602 const u8
*tlvs
, size_t tlvs_len
)
1604 struct wpa_supplicant
*wpa_s
= ctx
;
1605 const u8
*pos
= tlvs
;
1606 const u8
*end
= tlvs
+ tlvs_len
;
1612 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
1614 if (tlvs_len
> 1500) {
1615 /* TODO: better way for handling this */
1616 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1617 P2P_EVENT_SERV_DISC_RESP MACSTR
1618 " %u <long response: %u bytes>",
1619 MAC2STR(sa
), update_indic
,
1620 (unsigned int) tlvs_len
);
1622 buf_len
= 2 * tlvs_len
+ 1;
1623 buf
= os_malloc(buf_len
);
1625 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1626 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1627 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
1628 MAC2STR(sa
), update_indic
, buf
);
1634 u8 srv_proto
, srv_trans_id
, status
;
1636 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
1637 slen
= WPA_GET_LE16(pos
);
1639 if (pos
+ slen
> end
|| slen
< 3) {
1640 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
1644 tlv_end
= pos
+ slen
;
1647 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1649 srv_trans_id
= *pos
++;
1650 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1653 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
1656 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
1657 pos
, tlv_end
- pos
);
1662 wpas_notify_p2p_sd_response(wpa_s
, sa
, update_indic
, tlvs
, tlvs_len
);
1666 void * wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1667 const struct wpabuf
*tlvs
)
1669 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1670 return (void *) wpa_drv_p2p_sd_request(wpa_s
, dst
, tlvs
);
1671 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1673 return p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
1677 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1678 u8 version
, const char *query
)
1680 struct wpabuf
*tlvs
;
1683 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
1686 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
1687 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
1688 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
1689 wpabuf_put_u8(tlvs
, version
);
1690 wpabuf_put_str(tlvs
, query
);
1691 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
1697 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, void *req
)
1699 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1700 return wpa_drv_p2p_sd_cancel_request(wpa_s
, (u64
) req
);
1701 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1703 return p2p_sd_cancel_request(wpa_s
->global
->p2p
, req
);
1707 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
1708 const u8
*dst
, u8 dialog_token
,
1709 const struct wpabuf
*resp_tlvs
)
1711 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1712 wpa_drv_p2p_sd_response(wpa_s
, freq
, dst
, dialog_token
,
1716 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1718 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
1723 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
1725 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1726 wpa_drv_p2p_service_update(wpa_s
);
1729 if (wpa_s
->global
->p2p
)
1730 p2p_sd_service_update(wpa_s
->global
->p2p
);
1734 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
1736 dl_list_del(&bsrv
->list
);
1737 wpabuf_free(bsrv
->query
);
1738 wpabuf_free(bsrv
->resp
);
1743 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
1745 dl_list_del(&usrv
->list
);
1746 os_free(usrv
->service
);
1751 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
1753 struct p2p_srv_bonjour
*bsrv
, *bn
;
1754 struct p2p_srv_upnp
*usrv
, *un
;
1756 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
1757 struct p2p_srv_bonjour
, list
)
1758 wpas_p2p_srv_bonjour_free(bsrv
);
1760 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
1761 struct p2p_srv_upnp
, list
)
1762 wpas_p2p_srv_upnp_free(usrv
);
1764 wpas_p2p_sd_service_update(wpa_s
);
1768 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
1769 struct wpabuf
*query
, struct wpabuf
*resp
)
1771 struct p2p_srv_bonjour
*bsrv
;
1773 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1776 wpabuf_free(bsrv
->resp
);
1781 bsrv
= os_zalloc(sizeof(*bsrv
));
1784 bsrv
->query
= query
;
1786 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
1788 wpas_p2p_sd_service_update(wpa_s
);
1793 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
1794 const struct wpabuf
*query
)
1796 struct p2p_srv_bonjour
*bsrv
;
1798 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1801 wpas_p2p_srv_bonjour_free(bsrv
);
1802 wpas_p2p_sd_service_update(wpa_s
);
1807 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1808 const char *service
)
1810 struct p2p_srv_upnp
*usrv
;
1812 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
1813 return 0; /* Already listed */
1814 usrv
= os_zalloc(sizeof(*usrv
));
1817 usrv
->version
= version
;
1818 usrv
->service
= os_strdup(service
);
1819 if (usrv
->service
== NULL
) {
1823 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
1825 wpas_p2p_sd_service_update(wpa_s
);
1830 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1831 const char *service
)
1833 struct p2p_srv_upnp
*usrv
;
1835 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
1838 wpas_p2p_srv_upnp_free(usrv
);
1839 wpas_p2p_sd_service_update(wpa_s
);
1844 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
1845 const u8
*peer
, const char *params
,
1846 unsigned int generated_pin
)
1848 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
" %08d%s",
1849 MAC2STR(peer
), generated_pin
, params
);
1853 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
1854 const u8
*peer
, const char *params
)
1856 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
"%s",
1857 MAC2STR(peer
), params
);
1861 void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
1862 const u8
*dev_addr
, const u8
*pri_dev_type
,
1863 const char *dev_name
, u16 supp_config_methods
,
1864 u8 dev_capab
, u8 group_capab
)
1866 struct wpa_supplicant
*wpa_s
= ctx
;
1867 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1869 u8 empty_dev_type
[8];
1870 unsigned int generated_pin
= 0;
1872 if (pri_dev_type
== NULL
) {
1873 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
1874 pri_dev_type
= empty_dev_type
;
1876 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
1877 " pri_dev_type=%s name='%s' config_methods=0x%x "
1878 "dev_capab=0x%x group_capab=0x%x",
1880 wps_dev_type_bin2str(pri_dev_type
, devtype
,
1882 dev_name
, supp_config_methods
, dev_capab
, group_capab
);
1883 params
[sizeof(params
) - 1] = '\0';
1885 if (config_methods
& WPS_CONFIG_DISPLAY
) {
1886 generated_pin
= wps_generate_pin();
1887 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
1889 } else if (config_methods
& WPS_CONFIG_KEYPAD
)
1890 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
1891 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1892 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1893 "%s", MAC2STR(peer
), params
);
1897 void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
1899 struct wpa_supplicant
*wpa_s
= ctx
;
1900 unsigned int generated_pin
= 0;
1902 if (config_methods
& WPS_CONFIG_DISPLAY
)
1903 wpas_prov_disc_local_keypad(wpa_s
, peer
, "");
1904 else if (config_methods
& WPS_CONFIG_KEYPAD
) {
1905 generated_pin
= wps_generate_pin();
1906 wpas_prov_disc_local_display(wpa_s
, peer
, "", generated_pin
);
1907 } else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1908 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
,
1911 if (wpa_s
->pending_pd_before_join
&&
1912 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
1913 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
1914 wpa_s
->pending_pd_before_join
= 0;
1915 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
1916 "join-existing-group operation");
1917 wpas_p2p_join_start(wpa_s
);
1922 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
1923 const u8
*go_dev_addr
, const u8
*ssid
,
1924 size_t ssid_len
, int *go
, u8
*group_bssid
,
1925 int *force_freq
, int persistent_group
)
1927 struct wpa_supplicant
*wpa_s
= ctx
;
1929 u8 cur_bssid
[ETH_ALEN
];
1931 struct wpa_supplicant
*grp
;
1933 if (!persistent_group
) {
1934 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1935 " to join an active group", MAC2STR(sa
));
1936 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
1937 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
1939 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
1940 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
1941 "authorized invitation");
1945 * Do not accept the invitation automatically; notify user and
1948 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1951 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
1953 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
1954 "running persistent group");
1956 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
1960 if (!wpa_s
->conf
->persistent_reconnect
)
1961 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1963 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1964 if (s
->disabled
== 2 &&
1965 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
1966 s
->ssid_len
== ssid_len
&&
1967 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
1972 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1973 " requested reinvocation of an unknown group",
1975 return P2P_SC_FAIL_UNKNOWN_GROUP
;
1978 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
1980 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
1981 wpa_printf(MSG_DEBUG
, "P2P: The only available "
1982 "interface is already in use - reject "
1984 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1986 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
1987 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
1989 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
1991 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
1992 "interface address for the group");
1993 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1995 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
2000 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, cur_bssid
) == 0 &&
2001 wpa_s
->assoc_freq
) {
2002 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
2003 "the channel we are already using");
2004 *force_freq
= wpa_s
->assoc_freq
;
2007 res
= wpa_drv_shared_freq(wpa_s
);
2009 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
2010 "with the channel we are already using on a "
2011 "shared interface");
2015 return P2P_SC_SUCCESS
;
2019 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
2020 const u8
*ssid
, size_t ssid_len
,
2021 const u8
*go_dev_addr
, u8 status
,
2024 struct wpa_supplicant
*wpa_s
= ctx
;
2027 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2028 if (s
->disabled
== 2 &&
2029 s
->ssid_len
== ssid_len
&&
2030 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2034 if (status
== P2P_SC_SUCCESS
) {
2035 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2036 " was accepted; op_freq=%d MHz",
2037 MAC2STR(sa
), op_freq
);
2039 wpas_p2p_group_add_persistent(
2040 wpa_s
, s
, s
->mode
== WPAS_MODE_P2P_GO
, 0);
2042 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
2043 wpa_s
->p2p_wps_method
);
2048 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
2049 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2050 " was rejected (status %u)", MAC2STR(sa
), status
);
2056 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2057 "sa=" MACSTR
" go_dev_addr=" MACSTR
2058 " bssid=" MACSTR
" unknown-network",
2059 MAC2STR(sa
), MAC2STR(go_dev_addr
),
2062 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2063 "sa=" MACSTR
" go_dev_addr=" MACSTR
2065 MAC2STR(sa
), MAC2STR(go_dev_addr
));
2070 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
"sa=" MACSTR
2071 " persistent=%d", MAC2STR(sa
), s
->id
);
2075 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
)
2077 struct wpa_supplicant
*wpa_s
= ctx
;
2078 struct wpa_ssid
*ssid
;
2081 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2082 "status=%d " MACSTR
,
2083 status
, MAC2STR(bssid
));
2085 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2086 "status=%d ", status
);
2088 wpas_notify_p2p_invitation_result(wpa_s
, status
, bssid
);
2090 if (wpa_s
->pending_invite_ssid_id
== -1)
2091 return; /* Invitation to active group */
2093 if (status
!= P2P_SC_SUCCESS
) {
2094 wpas_p2p_remove_pending_group_interface(wpa_s
);
2098 ssid
= wpa_config_get_network(wpa_s
->conf
,
2099 wpa_s
->pending_invite_ssid_id
);
2101 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
2102 "data matching with invitation");
2106 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
2107 ssid
->mode
== WPAS_MODE_P2P_GO
, 0);
2111 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
2112 struct p2p_channels
*chan
)
2116 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
2119 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2120 chan
->reg_class
[cla
].reg_class
= 81;
2121 chan
->reg_class
[cla
].channels
= 11;
2122 for (i
= 0; i
< 11; i
++)
2123 chan
->reg_class
[cla
].channel
[i
] = i
+ 1;
2126 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
2129 /* Operating class 115 - 5 GHz, channels 36-48 */
2130 chan
->reg_class
[cla
].reg_class
= 115;
2131 chan
->reg_class
[cla
].channels
= 4;
2132 chan
->reg_class
[cla
].channel
[0] = 36;
2133 chan
->reg_class
[cla
].channel
[1] = 40;
2134 chan
->reg_class
[cla
].channel
[2] = 44;
2135 chan
->reg_class
[cla
].channel
[3] = 48;
2138 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
2141 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2142 chan
->reg_class
[cla
].reg_class
= 124;
2143 chan
->reg_class
[cla
].channels
= 4;
2144 chan
->reg_class
[cla
].channel
[0] = 149;
2145 chan
->reg_class
[cla
].channel
[1] = 153;
2146 chan
->reg_class
[cla
].channel
[2] = 157;
2147 chan
->reg_class
[cla
].channel
[3] = 161;
2150 chan
->reg_classes
= cla
;
2155 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
2157 enum hostapd_hw_mode mode
)
2161 for (i
= 0; i
< num_modes
; i
++) {
2162 if (modes
[i
].mode
== mode
)
2170 static int has_channel(struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
2174 for (i
= 0; i
< mode
->num_channels
; i
++) {
2175 if (mode
->channels
[i
].chan
== chan
) {
2177 *flags
= mode
->channels
[i
].flag
;
2178 return !(mode
->channels
[i
].flag
&
2179 (HOSTAPD_CHAN_DISABLED
|
2180 HOSTAPD_CHAN_PASSIVE_SCAN
|
2181 HOSTAPD_CHAN_NO_IBSS
|
2182 HOSTAPD_CHAN_RADAR
));
2190 struct p2p_oper_class_map
{
2191 enum hostapd_hw_mode mode
;
2196 enum { BW20
, BW40PLUS
, BW40MINUS
} bw
;
2199 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
2200 struct p2p_channels
*chan
)
2202 struct hostapd_hw_modes
*modes
, *mode
;
2203 u16 num_modes
, flags
;
2205 struct p2p_oper_class_map op_class
[] = {
2206 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
2207 { HOSTAPD_MODE_IEEE80211G
, 82, 14, 14, 1, BW20
},
2208 #if 0 /* Do not enable HT40 on 2 GHz for now */
2209 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
2210 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
2212 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
2213 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
2214 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
2215 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
2216 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
2217 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
2218 { -1, 0, 0, 0, 0, BW20
}
2221 modes
= wpa_drv_get_hw_feature_data(wpa_s
, &num_modes
, &flags
);
2222 if (modes
== NULL
) {
2223 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
2224 "of all supported channels; assume dualband "
2226 return wpas_p2p_default_channels(wpa_s
, chan
);
2231 for (op
= 0; op_class
[op
].op_class
; op
++) {
2232 struct p2p_oper_class_map
*o
= &op_class
[op
];
2234 struct p2p_reg_class
*reg
= NULL
;
2236 mode
= get_mode(modes
, num_modes
, o
->mode
);
2239 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
2241 if (!has_channel(mode
, ch
, &flag
))
2243 if (o
->bw
== BW40MINUS
&&
2244 (!(flag
& HOSTAPD_CHAN_HT40MINUS
) ||
2245 !has_channel(mode
, ch
- 4, NULL
)))
2247 if (o
->bw
== BW40PLUS
&&
2248 (!(flag
& HOSTAPD_CHAN_HT40PLUS
) ||
2249 !has_channel(mode
, ch
+ 4, NULL
)))
2252 wpa_printf(MSG_DEBUG
, "P2P: Add operating "
2253 "class %u", o
->op_class
);
2254 reg
= &chan
->reg_class
[cla
];
2256 reg
->reg_class
= o
->op_class
;
2258 reg
->channel
[reg
->channels
] = ch
;
2262 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
2263 reg
->channel
, reg
->channels
);
2267 chan
->reg_classes
= cla
;
2269 ieee80211_sta_free_hw_features(modes
, num_modes
);
2275 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
2278 struct wpa_supplicant
*wpa_s
= ctx
;
2280 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2281 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
2287 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
2292 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2293 * @global: Pointer to global data from wpa_supplicant_init()
2294 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2295 * Returns: 0 on success, -1 on failure
2297 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
2299 struct p2p_config p2p
;
2303 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
2306 #ifdef CONFIG_CLIENT_MLME
2307 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)) {
2308 wpa_s
->mlme
.public_action_cb
= p2p_rx_action_mlme
;
2309 wpa_s
->mlme
.public_action_cb_ctx
= wpa_s
;
2311 #endif /* CONFIG_CLIENT_MLME */
2313 if (wpa_drv_disable_11b_rates(wpa_s
, 1) < 0) {
2314 wpa_printf(MSG_DEBUG
, "P2P: Failed to disable 11b rates");
2315 /* Continue anyway; this is not really a fatal error */
2321 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2322 struct p2p_params params
;
2324 wpa_printf(MSG_DEBUG
, "P2P: Use driver-based P2P management");
2325 os_memset(¶ms
, 0, sizeof(params
));
2326 params
.dev_name
= wpa_s
->conf
->device_name
;
2327 os_memcpy(params
.pri_dev_type
, wpa_s
->conf
->device_type
,
2329 params
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
2330 os_memcpy(params
.sec_dev_type
,
2331 wpa_s
->conf
->sec_device_type
,
2332 params
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
2334 if (wpa_drv_p2p_set_params(wpa_s
, ¶ms
) < 0)
2340 os_memset(&p2p
, 0, sizeof(p2p
));
2341 p2p
.msg_ctx
= wpa_s
;
2343 p2p
.p2p_scan
= wpas_p2p_scan
;
2344 p2p
.send_action
= wpas_send_action
;
2345 p2p
.send_action_done
= wpas_send_action_done
;
2346 p2p
.go_neg_completed
= wpas_go_neg_completed
;
2347 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
2348 p2p
.dev_found
= wpas_dev_found
;
2349 p2p
.dev_lost
= wpas_dev_lost
;
2350 p2p
.start_listen
= wpas_start_listen
;
2351 p2p
.stop_listen
= wpas_stop_listen
;
2352 p2p
.send_probe_resp
= wpas_send_probe_resp
;
2353 p2p
.sd_request
= wpas_sd_request
;
2354 p2p
.sd_response
= wpas_sd_response
;
2355 p2p
.prov_disc_req
= wpas_prov_disc_req
;
2356 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
2357 p2p
.invitation_process
= wpas_invitation_process
;
2358 p2p
.invitation_received
= wpas_invitation_received
;
2359 p2p
.invitation_result
= wpas_invitation_result
;
2360 p2p
.get_noa
= wpas_get_noa
;
2362 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2363 os_memcpy(p2p
.dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2364 p2p
.dev_name
= wpa_s
->conf
->device_name
;
2366 if (wpa_s
->conf
->p2p_listen_reg_class
&&
2367 wpa_s
->conf
->p2p_listen_channel
) {
2368 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
2369 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
2373 * Pick one of the social channels randomly as the listen
2376 os_get_random((u8
*) &r
, sizeof(r
));
2377 p2p
.channel
= 1 + (r
% 3) * 5;
2379 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d", p2p
.channel
);
2381 if (wpa_s
->conf
->p2p_oper_reg_class
&&
2382 wpa_s
->conf
->p2p_oper_channel
) {
2383 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
2384 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
2385 p2p
.cfg_op_channel
= 1;
2386 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
2387 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2390 p2p
.op_reg_class
= 81;
2392 * Use random operation channel from (1, 6, 11) if no other
2393 * preference is indicated.
2395 os_get_random((u8
*) &r
, sizeof(r
));
2396 p2p
.op_channel
= 1 + (r
% 3) * 5;
2397 p2p
.cfg_op_channel
= 0;
2398 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
2399 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2401 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
2402 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
2403 p2p
.country
[2] = 0x04;
2405 os_memcpy(p2p
.country
, "XX\x04", 3);
2407 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
)) {
2408 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
2413 os_memcpy(p2p
.pri_dev_type
, wpa_s
->conf
->device_type
,
2416 p2p
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
2417 os_memcpy(p2p
.sec_dev_type
, wpa_s
->conf
->sec_device_type
,
2418 p2p
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
2420 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
2421 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
2423 p2p
.max_peers
= 100;
2425 if (wpa_s
->conf
->p2p_ssid_postfix
) {
2426 p2p
.ssid_postfix_len
=
2427 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
2428 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
2429 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
2430 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
2431 p2p
.ssid_postfix_len
);
2434 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
2436 global
->p2p
= p2p_init(&p2p
);
2437 if (global
->p2p
== NULL
)
2440 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
2441 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
2443 p2p_add_wps_vendor_extension(
2444 global
->p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
2452 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2453 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2455 * This function deinitialize per-interface P2P data.
2457 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
2459 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
2460 wpa_drv_probe_req_report(wpa_s
, 0);
2461 os_free(wpa_s
->go_params
);
2462 wpa_s
->go_params
= NULL
;
2463 wpabuf_free(wpa_s
->pending_action_tx
);
2464 wpa_s
->pending_action_tx
= NULL
;
2465 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
2466 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2467 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2468 wpa_s
->p2p_long_listen
= 0;
2469 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
2470 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
2471 wpas_p2p_remove_pending_group_interface(wpa_s
);
2473 /* TODO: remove group interface from the driver if this wpa_s instance
2474 * is on top of a P2P group interface */
2479 * wpas_p2p_deinit_global - Deinitialize global P2P module
2480 * @global: Pointer to global data from wpa_supplicant_init()
2482 * This function deinitializes the global (per device) P2P module.
2484 void wpas_p2p_deinit_global(struct wpa_global
*global
)
2486 struct wpa_supplicant
*wpa_s
, *tmp
;
2489 if (global
->p2p
== NULL
)
2492 /* Remove remaining P2P group interfaces */
2493 wpa_s
= global
->ifaces
;
2495 wpas_p2p_service_flush(wpa_s
);
2496 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
2497 wpa_s
= wpa_s
->next
;
2499 enum wpa_driver_if_type type
;
2500 tmp
= global
->ifaces
;
2503 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
2508 ifname
= os_strdup(tmp
->ifname
);
2509 type
= wpas_p2p_if_type(tmp
->p2p_group_interface
);
2510 wpa_supplicant_remove_iface(global
, tmp
);
2512 wpa_drv_if_remove(wpa_s
, type
, ifname
);
2517 * Deinit GO data on any possibly remaining interface (if main
2518 * interface is used as GO).
2520 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2521 if (wpa_s
->ap_iface
)
2522 wpas_p2p_group_deinit(wpa_s
);
2525 p2p_deinit(global
->p2p
);
2530 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
2532 if (wpa_s
->drv_flags
&
2533 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
2534 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
2535 return 1; /* P2P group requires a new interface in every case
2537 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
2538 return 0; /* driver does not support concurrent operations */
2539 if (wpa_s
->global
->ifaces
->next
)
2540 return 1; /* more that one interface already in use */
2541 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
2542 return 1; /* this interface is already in use */
2547 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
2548 const u8
*peer_addr
,
2549 enum p2p_wps_method wps_method
,
2550 int go_intent
, const u8
*own_interface_addr
,
2551 unsigned int force_freq
, int persistent_group
)
2553 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2554 return wpa_drv_p2p_connect(wpa_s
, peer_addr
, wps_method
,
2555 go_intent
, own_interface_addr
,
2556 force_freq
, persistent_group
);
2559 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2560 go_intent
, own_interface_addr
, force_freq
,
2565 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
2566 const u8
*peer_addr
,
2567 enum p2p_wps_method wps_method
,
2568 int go_intent
, const u8
*own_interface_addr
,
2569 unsigned int force_freq
, int persistent_group
)
2571 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2574 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2575 go_intent
, own_interface_addr
, force_freq
,
2580 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
2582 wpa_s
->p2p_join_scan_count
++;
2583 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
2584 wpa_s
->p2p_join_scan_count
);
2585 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
2586 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
2587 " for join operationg - stop join attempt",
2588 MAC2STR(wpa_s
->pending_join_iface_addr
));
2589 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2590 wpa_msg(wpa_s
->parent
, MSG_INFO
,
2591 P2P_EVENT_GROUP_FORMATION_FAILURE
);
2596 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
2597 struct wpa_scan_results
*scan_res
)
2599 struct wpa_bss
*bss
;
2601 u8 iface_addr
[ETH_ALEN
];
2603 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2605 if (wpa_s
->global
->p2p_disabled
)
2608 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for join",
2609 scan_res
? (int) scan_res
->num
: -1);
2612 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
2614 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2615 wpa_s
->pending_join_iface_addr
);
2617 p2p_get_interface_addr(wpa_s
->global
->p2p
,
2618 wpa_s
->pending_join_dev_addr
,
2620 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0)
2622 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
2623 "address for join from " MACSTR
" to " MACSTR
2624 " based on newly discovered P2P peer entry",
2625 MAC2STR(wpa_s
->pending_join_iface_addr
),
2626 MAC2STR(iface_addr
));
2627 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
2630 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2631 wpa_s
->pending_join_iface_addr
);
2634 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2635 "from P2P peer table: %d MHz", freq
);
2637 bss
= wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
);
2640 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2641 "from BSS table: %d MHz", freq
);
2646 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
2647 "prior to joining an existing group (GO " MACSTR
2649 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
2650 wpa_s
->pending_pd_before_join
= 1;
2652 switch (wpa_s
->pending_join_wps_method
) {
2654 case WPS_PIN_DISPLAY
:
2655 method
= WPS_CONFIG_KEYPAD
;
2657 case WPS_PIN_KEYPAD
:
2658 method
= WPS_CONFIG_DISPLAY
;
2661 method
= WPS_CONFIG_PUSHBUTTON
;
2668 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
2669 wpa_s
->pending_join_dev_addr
, method
, 1)
2671 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
2672 "Discovery Request before joining an "
2674 wpa_s
->pending_pd_before_join
= 0;
2679 * Actual join operation will be started from the Action frame
2680 * TX status callback.
2685 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
2686 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2687 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2688 wpas_p2p_check_join_scan_limit(wpa_s
);
2692 /* Start join operation immediately */
2693 wpas_p2p_join_start(wpa_s
);
2697 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
2699 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
2701 struct wpa_driver_scan_params params
;
2702 struct wpabuf
*wps_ie
, *ies
;
2704 os_memset(¶ms
, 0, sizeof(params
));
2706 /* P2P Wildcard SSID */
2707 params
.num_ssids
= 1;
2708 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
2709 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
2711 wpa_s
->wps
->dev
.p2p
= 1;
2712 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
2713 WPS_REQ_ENROLLEE
, 0, NULL
);
2714 if (wps_ie
== NULL
) {
2715 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2719 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + 100);
2721 wpabuf_free(wps_ie
);
2722 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2725 wpabuf_put_buf(ies
, wps_ie
);
2726 wpabuf_free(wps_ie
);
2728 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
2730 params
.extra_ies
= wpabuf_head(ies
);
2731 params
.extra_ies_len
= wpabuf_len(ies
);
2734 * Run a scan to update BSS table and start Provision Discovery once
2735 * the new scan results become available.
2737 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
2738 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
2739 ret
= ieee80211_sta_req_scan(wpa_s
, ¶ms
);
2741 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
2746 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
2748 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2749 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2750 wpas_p2p_check_join_scan_limit(wpa_s
);
2755 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
2756 const u8
*dev_addr
, enum p2p_wps_method wps_method
)
2758 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
2759 MACSTR
" dev " MACSTR
")",
2760 MAC2STR(iface_addr
), MAC2STR(dev_addr
));
2762 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
2763 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
2764 wpa_s
->pending_join_wps_method
= wps_method
;
2766 /* Make sure we are not running find during connection establishment */
2767 wpas_p2p_stop_find(wpa_s
);
2769 wpa_s
->p2p_join_scan_count
= 0;
2770 wpas_p2p_join_scan(wpa_s
, NULL
);
2775 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
2777 struct wpa_supplicant
*group
;
2778 struct p2p_go_neg_results res
;
2780 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
2783 if (group
!= wpa_s
) {
2784 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
2785 sizeof(group
->p2p_pin
));
2786 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
2789 group
->p2p_in_provisioning
= 1;
2791 os_memset(&res
, 0, sizeof(res
));
2792 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
2794 res
.wps_method
= wpa_s
->pending_join_wps_method
;
2795 wpas_start_wps_enrollee(group
, &res
);
2798 * Allow a longer timeout for join-a-running-group than normal 15
2799 * second group formation timeout since the GO may not have authorized
2800 * our connection yet.
2802 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2803 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
2811 * wpas_p2p_connect - Request P2P Group Formation to be started
2812 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2813 * @peer_addr: Address of the peer P2P Device
2814 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2815 * @persistent_group: Whether to create a persistent group
2816 * @join: Whether to join an existing group (as a client) instead of starting
2817 * Group Owner negotiation; @peer_addr is BSSID in that case
2818 * @auth: Whether to only authorize the connection instead of doing that and
2819 * initiating Group Owner negotiation
2820 * @go_intent: GO Intent or -1 to use default
2821 * @freq: Frequency for the group or 0 for auto-selection
2822 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2823 * failure, -2 on failure due to channel not currently available,
2824 * -3 if forced channel is not supported
2826 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
2827 const char *pin
, enum p2p_wps_method wps_method
,
2828 int persistent_group
, int join
, int auth
, int go_intent
,
2831 int force_freq
= 0, oper_freq
= 0;
2834 enum wpa_driver_if_type iftype
;
2836 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2840 go_intent
= wpa_s
->conf
->p2p_go_intent
;
2843 wpa_s
->p2p_long_listen
= 0;
2845 wpa_s
->p2p_wps_method
= wps_method
;
2848 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
2849 else if (wps_method
== WPS_PIN_DISPLAY
) {
2850 ret
= wps_generate_pin();
2851 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
2853 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
2856 wpa_s
->p2p_pin
[0] = '\0';
2859 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
2861 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
2862 "connect a running group from " MACSTR
,
2863 MAC2STR(peer_addr
));
2864 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
2867 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
2868 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
2870 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
2871 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
2874 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
) <
2880 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
2882 oper_freq
= wpa_s
->assoc_freq
;
2884 oper_freq
= wpa_drv_shared_freq(wpa_s
);
2890 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
2891 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
2892 "(%u MHz) is not supported for P2P uses",
2897 if (oper_freq
> 0 && freq
!= oper_freq
&&
2898 !(wpa_s
->drv_flags
&
2899 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2900 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2901 "on %u MHz while connected on another "
2902 "channel (%u MHz)", freq
, oper_freq
);
2905 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2906 "requested channel (%u MHz)", freq
);
2908 } else if (oper_freq
> 0 &&
2909 !p2p_supported_freq(wpa_s
->global
->p2p
, oper_freq
)) {
2910 if (!(wpa_s
->drv_flags
&
2911 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2912 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2913 "while connected on non-P2P supported "
2914 "channel (%u MHz)", oper_freq
);
2917 wpa_printf(MSG_DEBUG
, "P2P: Current operating channel "
2918 "(%u MHz) not available for P2P - try to use "
2919 "another channel", oper_freq
);
2921 } else if (oper_freq
> 0) {
2922 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2923 "channel we are already using (%u MHz) on another "
2924 "interface", oper_freq
);
2925 force_freq
= oper_freq
;
2928 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
2930 if (!wpa_s
->create_p2p_iface
) {
2932 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2933 go_intent
, wpa_s
->own_addr
,
2934 force_freq
, persistent_group
)
2939 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
2940 go_intent
, wpa_s
->own_addr
,
2941 force_freq
, persistent_group
) < 0)
2946 /* Prepare to add a new interface for the group */
2947 iftype
= WPA_IF_P2P_GROUP
;
2949 iftype
= WPA_IF_P2P_CLIENT
;
2950 else if (go_intent
== 15)
2951 iftype
= WPA_IF_P2P_GO
;
2952 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
2953 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2954 "interface for the group");
2959 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2961 wpa_s
->pending_interface_addr
,
2962 force_freq
, persistent_group
) < 0)
2966 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
, go_intent
,
2967 wpa_s
->pending_interface_addr
,
2968 force_freq
, persistent_group
) < 0) {
2969 wpas_p2p_remove_pending_group_interface(wpa_s
);
2977 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2978 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2979 * @freq: Frequency of the channel in MHz
2980 * @duration: Duration of the stay on the channel in milliseconds
2982 * This callback is called when the driver indicates that it has started the
2983 * requested remain-on-channel duration.
2985 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
2986 unsigned int freq
, unsigned int duration
)
2988 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2990 wpa_s
->roc_waiting_drv_freq
= 0;
2991 wpa_s
->off_channel_freq
= freq
;
2992 wpas_send_action_cb(wpa_s
, NULL
);
2993 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
2994 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
2995 wpa_s
->pending_listen_duration
);
2996 wpa_s
->pending_listen_freq
= 0;
3001 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
3002 unsigned int timeout
)
3004 /* Limit maximum Listen state time based on driver limitation. */
3005 if (timeout
> wpa_s
->max_remain_on_chan
)
3006 timeout
= wpa_s
->max_remain_on_chan
;
3008 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3009 return wpa_drv_p2p_listen(wpa_s
, timeout
);
3011 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
3016 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3017 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3018 * @freq: Frequency of the channel in MHz
3020 * This callback is called when the driver indicates that a remain-on-channel
3021 * operation has been completed, i.e., the duration on the requested channel
3024 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
3027 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
3028 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3029 wpa_s
->p2p_long_listen
, wpa_s
->pending_action_tx
);
3030 wpa_s
->off_channel_freq
= 0;
3031 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3033 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
3034 return; /* P2P module started a new operation */
3035 if (wpa_s
->pending_action_tx
)
3037 if (wpa_s
->p2p_long_listen
> 0)
3038 wpa_s
->p2p_long_listen
-= wpa_s
->max_remain_on_chan
;
3039 if (wpa_s
->p2p_long_listen
> 0) {
3040 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
3041 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
);
3047 * wpas_p2p_group_remove - Remove a P2P group
3048 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3049 * @ifname: Network interface name of the group interface or "*" to remove all
3051 * Returns: 0 on success, -1 on failure
3053 * This function is used to remove a P2P group. This can be used to disconnect
3054 * from a group in which the local end is a P2P Client or to end a P2P Group in
3055 * case the local end is the Group Owner. If a virtual network interface was
3056 * created for this group, that interface will be removed. Otherwise, only the
3057 * configured P2P group network will be removed from the interface.
3059 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
3061 struct wpa_global
*global
= wpa_s
->global
;
3063 if (os_strcmp(ifname
, "*") == 0) {
3064 struct wpa_supplicant
*prev
;
3065 wpa_s
= global
->ifaces
;
3068 wpa_s
= wpa_s
->next
;
3069 wpas_p2p_disconnect(prev
);
3074 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3075 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3079 return wpas_p2p_disconnect(wpa_s
);
3083 static void wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
3084 struct p2p_go_neg_results
*params
,
3090 os_memset(params
, 0, sizeof(*params
));
3091 params
->role_go
= 1;
3093 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
3094 "frequency %d MHz", freq
);
3095 params
->freq
= freq
;
3096 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
3097 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
3098 wpa_s
->conf
->p2p_oper_channel
<= 11) {
3099 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3100 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3101 "frequency %d MHz", params
->freq
);
3102 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
3103 wpa_s
->conf
->p2p_oper_reg_class
== 118) {
3104 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3105 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3106 "frequency %d MHz", params
->freq
);
3107 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3108 wpa_s
->best_overall_freq
> 0 &&
3109 p2p_supported_freq(wpa_s
->global
->p2p
,
3110 wpa_s
->best_overall_freq
)) {
3111 params
->freq
= wpa_s
->best_overall_freq
;
3112 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
3113 "channel %d MHz", params
->freq
);
3114 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3115 wpa_s
->best_24_freq
> 0 &&
3116 p2p_supported_freq(wpa_s
->global
->p2p
,
3117 wpa_s
->best_24_freq
)) {
3118 params
->freq
= wpa_s
->best_24_freq
;
3119 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
3120 "channel %d MHz", params
->freq
);
3121 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3122 wpa_s
->best_5_freq
> 0 &&
3123 p2p_supported_freq(wpa_s
->global
->p2p
,
3124 wpa_s
->best_5_freq
)) {
3125 params
->freq
= wpa_s
->best_5_freq
;
3126 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
3127 "channel %d MHz", params
->freq
);
3129 params
->freq
= 2412;
3130 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference "
3131 "known)", params
->freq
);
3134 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
3135 wpa_s
->assoc_freq
&& !freq
) {
3136 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3138 params
->freq
= wpa_s
->assoc_freq
;
3141 res
= wpa_drv_shared_freq(wpa_s
);
3142 if (res
> 0 && !freq
) {
3143 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3144 "already using on a shared interface");
3150 static struct wpa_supplicant
*
3151 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
3154 struct wpa_supplicant
*group_wpa_s
;
3156 if (!wpas_p2p_create_iface(wpa_s
))
3159 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
3160 WPA_IF_P2P_CLIENT
) < 0)
3162 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
3163 if (group_wpa_s
== NULL
) {
3164 wpas_p2p_remove_pending_group_interface(wpa_s
);
3173 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3174 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3175 * @persistent_group: Whether to create a persistent group
3176 * @freq: Frequency for the group or 0 to indicate no hardcoding
3177 * Returns: 0 on success, -1 on failure
3179 * This function creates a new P2P group with the local end as the Group Owner,
3180 * i.e., without using Group Owner Negotiation.
3182 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
3185 struct p2p_go_neg_results params
;
3188 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3192 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
3194 if (wpa_s
->best_24_freq
> 0 &&
3195 p2p_supported_freq(wpa_s
->global
->p2p
,
3196 wpa_s
->best_24_freq
)) {
3197 freq
= wpa_s
->best_24_freq
;
3198 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
3199 "channel: %d MHz", freq
);
3201 os_get_random((u8
*) &r
, sizeof(r
));
3202 freq
= 2412 + (r
% 3) * 25;
3203 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
3204 "channel: %d MHz", freq
);
3209 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
3211 if (wpa_s
->best_5_freq
> 0 &&
3212 p2p_supported_freq(wpa_s
->global
->p2p
,
3213 wpa_s
->best_5_freq
)) {
3214 freq
= wpa_s
->best_5_freq
;
3215 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
3216 "channel: %d MHz", freq
);
3218 os_get_random((u8
*) &r
, sizeof(r
));
3219 freq
= 5180 + (r
% 4) * 20;
3220 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3221 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
3222 "5 GHz channel for P2P group");
3225 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
3226 "channel: %d MHz", freq
);
3230 if (freq
> 0 && !p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3231 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
3232 "(%u MHz) is not supported for P2P uses",
3237 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
3238 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
3239 params
.persistent_group
= persistent_group
;
3241 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
3244 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3250 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
3251 struct wpa_ssid
*params
, int addr_allocated
)
3253 struct wpa_ssid
*ssid
;
3255 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
3259 wpa_supplicant_ap_deinit(wpa_s
);
3261 ssid
= wpa_config_add_network(wpa_s
->conf
);
3264 wpas_notify_network_added(wpa_s
, ssid
);
3265 wpa_config_set_network_defaults(ssid
);
3266 ssid
->temporary
= 1;
3267 ssid
->proto
= WPA_PROTO_RSN
;
3268 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
3269 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
3270 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
3271 ssid
->ssid
= os_malloc(params
->ssid_len
);
3272 if (ssid
->ssid
== NULL
) {
3273 wpas_notify_network_removed(wpa_s
, ssid
);
3274 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
3277 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
3278 ssid
->ssid_len
= params
->ssid_len
;
3279 ssid
->p2p_group
= 1;
3280 ssid
->export_keys
= 1;
3281 if (params
->psk_set
) {
3282 os_memcpy(ssid
->psk
, params
->psk
, 32);
3285 if (params
->passphrase
)
3286 ssid
->passphrase
= os_strdup(params
->passphrase
);
3288 wpa_supplicant_select_network(wpa_s
, ssid
);
3290 wpa_s
->show_group_started
= 1;
3296 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
3297 struct wpa_ssid
*ssid
, int addr_allocated
,
3300 struct p2p_go_neg_results params
;
3303 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
3306 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
3307 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
3308 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
3313 /* Make sure we are not running find during connection establishment */
3314 wpas_p2p_stop_find(wpa_s
);
3316 if (ssid
->mode
== WPAS_MODE_INFRA
)
3317 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
3319 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
3322 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
3325 if (ssid
->passphrase
== NULL
||
3326 os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
3327 wpa_printf(MSG_DEBUG
, "P2P: Invalid passphrase in persistent "
3331 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
3332 sizeof(params
.passphrase
));
3333 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
3334 params
.ssid_len
= ssid
->ssid_len
;
3335 params
.persistent_group
= 1;
3337 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
3341 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3347 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
3348 struct wpabuf
*proberesp_ies
)
3350 struct wpa_supplicant
*wpa_s
= ctx
;
3351 if (wpa_s
->ap_iface
) {
3352 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
3354 wpabuf_free(hapd
->p2p_beacon_ie
);
3355 hapd
->p2p_beacon_ie
= beacon_ies
;
3357 wpabuf_free(hapd
->p2p_probe_resp_ie
);
3358 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
3360 wpabuf_free(beacon_ies
);
3361 wpabuf_free(proberesp_ies
);
3363 wpa_supplicant_ap_update_beacon(wpa_s
);
3367 static void wpas_p2p_idle_update(void *ctx
, int idle
)
3369 struct wpa_supplicant
*wpa_s
= ctx
;
3370 if (!wpa_s
->ap_iface
)
3372 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
3374 wpas_p2p_set_group_idle_timeout(wpa_s
);
3376 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3380 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
3381 int persistent_group
,
3382 int group_formation
)
3384 struct p2p_group
*group
;
3385 struct p2p_group_config
*cfg
;
3387 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3389 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3392 cfg
= os_zalloc(sizeof(*cfg
));
3396 cfg
->persistent_group
= persistent_group
;
3397 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
3398 if (wpa_s
->max_stations
&&
3399 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
3400 cfg
->max_clients
= wpa_s
->max_stations
;
3402 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
3403 cfg
->cb_ctx
= wpa_s
;
3404 cfg
->ie_update
= wpas_p2p_ie_update
;
3405 cfg
->idle_update
= wpas_p2p_idle_update
;
3407 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
3410 if (!group_formation
)
3411 p2p_group_notif_formation_done(group
);
3412 wpa_s
->p2p_group
= group
;
3417 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3420 if (!wpa_s
->p2p_in_provisioning
) {
3421 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
3422 "provisioning not in progress");
3426 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
3428 if (wpa_s
->global
->p2p
)
3429 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
3430 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3431 wpa_drv_wps_success_cb(wpa_s
, peer_addr
);
3432 wpas_group_formation_completed(wpa_s
, 1);
3436 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3437 const char *config_method
)
3441 if (os_strcmp(config_method
, "display") == 0)
3442 config_methods
= WPS_CONFIG_DISPLAY
;
3443 else if (os_strcmp(config_method
, "keypad") == 0)
3444 config_methods
= WPS_CONFIG_KEYPAD
;
3445 else if (os_strcmp(config_method
, "pbc") == 0 ||
3446 os_strcmp(config_method
, "pushbutton") == 0)
3447 config_methods
= WPS_CONFIG_PUSHBUTTON
;
3451 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3452 return wpa_drv_p2p_prov_disc_req(wpa_s
, peer_addr
,
3456 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
3459 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
3464 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
3467 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
3471 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
3473 if (!wpa_s
->pending_action_tx
)
3476 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
3477 "operation request");
3478 wpabuf_free(wpa_s
->pending_action_tx
);
3479 wpa_s
->pending_action_tx
= NULL
;
3483 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
3484 enum p2p_discovery_type type
,
3485 unsigned int num_req_dev_types
, const u8
*req_dev_types
)
3487 wpas_p2p_clear_pending_action_tx(wpa_s
);
3488 wpa_s
->p2p_long_listen
= 0;
3490 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3491 return wpa_drv_p2p_find(wpa_s
, timeout
, type
);
3493 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3496 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
,
3497 num_req_dev_types
, req_dev_types
);
3501 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
3503 wpas_p2p_clear_pending_action_tx(wpa_s
);
3504 wpa_s
->p2p_long_listen
= 0;
3505 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3506 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3508 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3509 wpa_drv_p2p_stop_find(wpa_s
);
3513 if (wpa_s
->global
->p2p
)
3514 p2p_stop_find(wpa_s
->global
->p2p
);
3516 wpas_p2p_remove_pending_group_interface(wpa_s
);
3520 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
3522 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3523 wpa_s
->p2p_long_listen
= 0;
3527 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
3531 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3534 wpas_p2p_clear_pending_action_tx(wpa_s
);
3538 * This is a request for unlimited Listen state. However, at
3539 * least for now, this is mapped to a Listen state for one
3544 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3545 wpa_s
->p2p_long_listen
= 0;
3547 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
3548 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
3549 wpa_s
->p2p_long_listen
= timeout
* 1000;
3550 eloop_register_timeout(timeout
, 0,
3551 wpas_p2p_long_listen_timeout
,
3559 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
3560 u8
*buf
, size_t len
, int p2p_group
)
3562 struct wpabuf
*p2p_ie
;
3565 if (wpa_s
->global
->p2p_disabled
)
3567 if (wpa_s
->global
->p2p
== NULL
)
3572 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
3573 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
3575 wpabuf_free(p2p_ie
);
3581 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
3582 const u8
*ie
, size_t ie_len
)
3584 if (wpa_s
->global
->p2p_disabled
)
3586 if (wpa_s
->global
->p2p
== NULL
)
3589 return p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, ie
, ie_len
);
3593 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
3594 const u8
*sa
, const u8
*bssid
,
3595 u8 category
, const u8
*data
, size_t len
, int freq
)
3597 if (wpa_s
->global
->p2p_disabled
)
3599 if (wpa_s
->global
->p2p
== NULL
)
3602 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
3607 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
3609 if (wpa_s
->global
->p2p_disabled
)
3611 if (wpa_s
->global
->p2p
== NULL
)
3614 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
3618 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
3620 p2p_group_deinit(wpa_s
->p2p_group
);
3621 wpa_s
->p2p_group
= NULL
;
3625 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
3627 wpa_s
->p2p_long_listen
= 0;
3629 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3630 return wpa_drv_p2p_reject(wpa_s
, addr
);
3632 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3635 return p2p_reject(wpa_s
->global
->p2p
, addr
);
3639 /* Invite to reinvoke a persistent group */
3640 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3641 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
)
3643 enum p2p_invite_role role
;
3646 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3647 role
= P2P_INVITE_ROLE_GO
;
3648 if (peer_addr
== NULL
) {
3649 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
3650 "address in invitation command");
3653 if (wpas_p2p_create_iface(wpa_s
)) {
3654 if (wpas_p2p_add_group_interface(wpa_s
,
3655 WPA_IF_P2P_GO
) < 0) {
3656 wpa_printf(MSG_ERROR
, "P2P: Failed to "
3657 "allocate a new interface for the "
3661 bssid
= wpa_s
->pending_interface_addr
;
3663 bssid
= wpa_s
->own_addr
;
3665 role
= P2P_INVITE_ROLE_CLIENT
;
3666 peer_addr
= ssid
->bssid
;
3668 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
3670 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3671 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3672 ssid
->ssid
, ssid
->ssid_len
,
3675 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3678 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3679 ssid
->ssid
, ssid
->ssid_len
, 0, go_dev_addr
, 1);
3683 /* Invite to join an active group */
3684 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
3685 const u8
*peer_addr
, const u8
*go_dev_addr
)
3687 struct wpa_global
*global
= wpa_s
->global
;
3688 enum p2p_invite_role role
;
3690 struct wpa_ssid
*ssid
;
3692 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3693 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3696 if (wpa_s
== NULL
) {
3697 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
3701 ssid
= wpa_s
->current_ssid
;
3703 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
3708 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3709 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
3710 bssid
= wpa_s
->own_addr
;
3711 if (go_dev_addr
== NULL
)
3712 go_dev_addr
= wpa_s
->parent
->own_addr
;
3714 role
= P2P_INVITE_ROLE_CLIENT
;
3715 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
3716 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
3717 "invite to current group");
3720 bssid
= wpa_s
->bssid
;
3721 if (go_dev_addr
== NULL
&&
3722 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
3723 go_dev_addr
= wpa_s
->go_dev_addr
;
3725 wpa_s
->parent
->pending_invite_ssid_id
= -1;
3727 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3728 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3729 ssid
->ssid
, ssid
->ssid_len
,
3732 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3735 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3736 ssid
->ssid
, ssid
->ssid_len
, wpa_s
->assoc_freq
,
3741 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
3743 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
3744 const char *ssid_txt
;
3745 u8 go_dev_addr
[ETH_ALEN
];
3748 if (!wpa_s
->show_group_started
|| !ssid
)
3751 wpa_s
->show_group_started
= 0;
3753 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
3754 os_memset(go_dev_addr
, 0, ETH_ALEN
);
3755 if (ssid
->bssid_set
)
3756 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
3757 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
3759 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
3761 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
3762 wpa_s
->global
->p2p_group_formation
= NULL
;
3764 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
3766 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
3767 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3768 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3770 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
3771 MAC2STR(go_dev_addr
),
3772 persistent
? " [PERSISTENT]" : "");
3774 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3775 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3776 "go_dev_addr=" MACSTR
"%s",
3777 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
,
3778 ssid
->passphrase
? ssid
->passphrase
: "",
3779 MAC2STR(go_dev_addr
),
3780 persistent
? " [PERSISTENT]" : "");
3784 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
3789 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
3790 u32 interval1
, u32 duration2
, u32 interval2
)
3792 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3794 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3797 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
3798 wpa_s
->current_ssid
== NULL
||
3799 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
3802 return p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
3803 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
3804 duration1
, interval1
, duration2
, interval2
);
3808 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
3809 unsigned int interval
)
3811 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3814 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3817 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
3821 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
3823 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3825 if (wpa_s
->conf
->p2p_group_idle
== 0) {
3826 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
3831 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
3833 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_IDLE_TIMEOUT
;
3834 wpas_p2p_group_delete(wpa_s
);
3838 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
3840 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3841 if (wpa_s
->conf
->p2p_group_idle
== 0)
3844 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
3847 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
3848 wpa_s
->conf
->p2p_group_idle
);
3849 eloop_register_timeout(wpa_s
->conf
->p2p_group_idle
, 0,
3850 wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3854 void wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3855 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3857 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3859 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3862 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3866 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3867 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3869 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3871 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3874 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3878 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
3880 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
3885 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
3888 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
3889 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
3891 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
)
3892 p2p_set_pri_dev_type(p2p
, wpa_s
->conf
->device_type
);
3894 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
)
3895 p2p_set_sec_dev_types(p2p
,
3896 (void *) wpa_s
->conf
->sec_device_type
,
3897 wpa_s
->conf
->num_sec_device_types
);
3899 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_VENDOR_EXTENSION
) {
3901 p2p_remove_wps_vendor_extensions(p2p
);
3902 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
3903 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
3905 p2p_add_wps_vendor_extension(
3906 p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
3910 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
3911 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
3913 country
[0] = wpa_s
->conf
->country
[0];
3914 country
[1] = wpa_s
->conf
->country
[1];
3916 p2p_set_country(p2p
, country
);
3919 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
3920 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
3921 wpa_s
->conf
->p2p_ssid_postfix
?
3922 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
3926 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
3927 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
3931 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
3934 if (!wpa_s
->ap_iface
)
3936 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
3941 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
3943 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3945 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3948 wpa_s
->global
->cross_connection
= enabled
;
3949 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
3952 struct wpa_supplicant
*iface
;
3954 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
3956 if (iface
->cross_connect_enabled
== 0)
3959 iface
->cross_connect_enabled
= 0;
3960 iface
->cross_connect_in_use
= 0;
3961 wpa_msg(iface
->parent
, MSG_INFO
,
3962 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
3963 iface
->ifname
, iface
->cross_connect_uplink
);
3971 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
3973 struct wpa_supplicant
*iface
;
3975 if (!uplink
->global
->cross_connection
)
3978 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
3979 if (!iface
->cross_connect_enabled
)
3981 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
3984 if (iface
->ap_iface
== NULL
)
3986 if (iface
->cross_connect_in_use
)
3989 iface
->cross_connect_in_use
= 1;
3990 wpa_msg(iface
->parent
, MSG_INFO
,
3991 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
3992 iface
->ifname
, iface
->cross_connect_uplink
);
3997 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
3999 struct wpa_supplicant
*iface
;
4001 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
4002 if (!iface
->cross_connect_enabled
)
4004 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
4007 if (!iface
->cross_connect_in_use
)
4010 wpa_msg(iface
->parent
, MSG_INFO
,
4011 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
4012 iface
->ifname
, iface
->cross_connect_uplink
);
4013 iface
->cross_connect_in_use
= 0;
4018 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
4020 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
4021 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
4022 wpa_s
->cross_connect_disallowed
)
4023 wpas_p2p_disable_cross_connect(wpa_s
);
4025 wpas_p2p_enable_cross_connect(wpa_s
);
4026 if (!wpa_s
->ap_iface
)
4027 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
4031 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
4033 wpas_p2p_disable_cross_connect(wpa_s
);
4034 if (!wpa_s
->ap_iface
)
4035 wpas_p2p_set_group_idle_timeout(wpa_s
);
4039 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
4041 struct wpa_supplicant
*iface
;
4043 if (!wpa_s
->global
->cross_connection
)
4046 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
4049 if (iface
->drv_flags
&
4050 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
4052 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
4055 wpa_s
->cross_connect_enabled
= 1;
4056 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
4057 sizeof(wpa_s
->cross_connect_uplink
));
4058 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
4059 "%s to %s whenever uplink is available",
4060 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4062 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
4063 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
4064 iface
->cross_connect_disallowed
||
4065 iface
->wpa_state
!= WPA_COMPLETED
)
4068 wpa_s
->cross_connect_in_use
= 1;
4069 wpa_msg(wpa_s
->parent
, MSG_INFO
,
4070 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
4071 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4077 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
4079 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
4080 !wpa_s
->p2p_in_provisioning
)
4081 return 0; /* not P2P client operation */
4083 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
4085 if (wpa_s
!= wpa_s
->parent
)
4086 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
4088 if (wpa_s
->global
->p2p
)
4089 p2p_group_formation_failed(wpa_s
->global
->p2p
);
4091 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4092 wpa_s
->parent
, NULL
);
4094 wpas_group_formation_completed(wpa_s
, 0);
4099 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
4101 struct p2p_channels chan
;
4103 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
4106 os_memset(&chan
, 0, sizeof(chan
));
4107 if (wpas_p2p_setup_channels(wpa_s
, &chan
)) {
4108 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
4113 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
);
4117 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
4119 struct wpa_global
*global
= wpa_s
->global
;
4123 if (global
->p2p
== NULL
)
4126 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
4128 if (wpa_s
->pending_interface_name
[0] &&
4129 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
4132 peer
= p2p_get_go_neg_peer(global
->p2p
);
4134 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
4135 MACSTR
, MAC2STR(peer
));
4136 p2p_unauthorize(global
->p2p
, peer
);
4139 wpas_p2p_stop_find(wpa_s
);
4141 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4142 if (wpa_s
== global
->p2p_group_formation
&&
4143 (wpa_s
->p2p_in_provisioning
||
4144 wpa_s
->parent
->pending_interface_type
==
4145 WPA_IF_P2P_CLIENT
)) {
4146 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
4147 "formation found - cancelling",
4150 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4151 wpa_s
->parent
, NULL
);
4152 wpas_p2p_group_delete(wpa_s
);
4158 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
4166 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
4168 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
4171 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
4172 "being available anymore");
4173 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_UNAVAILABLE
;
4174 wpas_p2p_group_delete(wpa_s
);
4178 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
4179 int freq_24
, int freq_5
, int freq_overall
)
4181 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4182 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4184 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
4188 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
4191 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4193 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4196 if (hwaddr_aton(addr
, peer
))
4199 return p2p_unauthorize(p2p
, peer
);
4204 * wpas_p2p_disconnect - Disconnect from a P2P Group
4205 * @wpa_s: Pointer to wpa_supplicant data
4206 * Returns: 0 on success, -1 on failure
4208 * This can be used to disconnect from a group in which the local end is a P2P
4209 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4210 * virtual network interface was created for this group, that interface will be
4211 * removed. Otherwise, only the configured P2P group network will be removed
4212 * from the interface.
4214 int wpas_p2p_disconnect(struct wpa_supplicant
*wpa_s
)
4220 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_REQUESTED
;
4221 wpas_p2p_group_delete(wpa_s
);