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
)
86 struct wpa_supplicant
*wpa_s
= ctx
;
87 struct wpa_driver_scan_params params
;
89 struct wpabuf
*wps_ie
, *ies
;
90 int social_channels
[] = { 2412, 2437, 2462, 0, 0 };
92 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
95 os_memset(¶ms
, 0, sizeof(params
));
97 /* P2P Wildcard SSID */
99 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
100 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
102 wpa_s
->wps
->dev
.p2p
= 1;
103 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
108 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + 100);
113 wpabuf_put_buf(ies
, wps_ie
);
116 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
118 params
.extra_ies
= wpabuf_head(ies
);
119 params
.extra_ies_len
= wpabuf_len(ies
);
122 case P2P_SCAN_SOCIAL
:
123 params
.freqs
= social_channels
;
127 case P2P_SCAN_SPECIFIC
:
128 social_channels
[0] = freq
;
129 social_channels
[1] = 0;
130 params
.freqs
= social_channels
;
132 case P2P_SCAN_SOCIAL_PLUS_ONE
:
133 social_channels
[3] = freq
;
134 params
.freqs
= social_channels
;
138 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
139 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
140 ret
= ieee80211_sta_req_scan(wpa_s
, ¶ms
);
142 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
150 #ifdef CONFIG_CLIENT_MLME
151 static void p2p_rx_action_mlme(void *ctx
, const u8
*buf
, size_t len
, int freq
)
153 struct wpa_supplicant
*wpa_s
= ctx
;
154 const struct ieee80211_mgmt
*mgmt
;
157 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
159 mgmt
= (const struct ieee80211_mgmt
*) buf
;
160 hdr_len
= (const u8
*) &mgmt
->u
.action
.u
.vs_public_action
.action
- buf
;
163 p2p_rx_action(wpa_s
->global
->p2p
, mgmt
->da
, mgmt
->sa
, mgmt
->bssid
,
164 mgmt
->u
.action
.category
,
165 &mgmt
->u
.action
.u
.vs_public_action
.action
,
166 len
- hdr_len
, freq
);
168 #endif /* CONFIG_CLIENT_MLME */
171 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
173 switch (p2p_group_interface
) {
174 case P2P_GROUP_INTERFACE_PENDING
:
175 return WPA_IF_P2P_GROUP
;
176 case P2P_GROUP_INTERFACE_GO
:
177 return WPA_IF_P2P_GO
;
178 case P2P_GROUP_INTERFACE_CLIENT
:
179 return WPA_IF_P2P_CLIENT
;
182 return WPA_IF_P2P_GROUP
;
186 static struct wpa_supplicant
* wpas_get_p2p_group(struct wpa_supplicant
*wpa_s
,
188 size_t ssid_len
, int *go
)
192 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
193 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
194 if (s
->disabled
!= 0 || !s
->p2p_group
||
195 s
->ssid_len
!= ssid_len
||
196 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
198 if (s
->mode
== WPAS_MODE_P2P_GO
&&
199 s
!= wpa_s
->current_ssid
)
202 *go
= s
->mode
== WPAS_MODE_P2P_GO
;
211 static void wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
)
213 struct wpa_ssid
*ssid
;
217 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
219 ssid
= wpa_s
->current_ssid
;
222 * The current SSID was not known, but there may still be a
223 * pending P2P group interface waiting for provisioning.
225 ssid
= wpa_s
->conf
->ssid
;
227 if (ssid
->p2p_group
&&
228 (ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
229 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)))
234 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
236 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
237 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
238 wpa_s
->reassociate
= 0;
239 wpa_s
->disconnected
= 1;
240 wpa_supplicant_deauthenticate(wpa_s
,
241 WLAN_REASON_DEAUTH_LEAVING
);
245 if (wpa_s
->cross_connect_in_use
) {
246 wpa_s
->cross_connect_in_use
= 0;
247 wpa_msg(wpa_s
->parent
, MSG_INFO
,
248 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
249 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
251 switch (wpa_s
->removal_reason
) {
252 case P2P_GROUP_REMOVAL_REQUESTED
:
253 reason
= " reason=REQUESTED";
255 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
256 reason
= " reason=IDLE";
258 case P2P_GROUP_REMOVAL_UNAVAILABLE
:
259 reason
= " reason=UNAVAILABLE";
265 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_REMOVED
"%s %s%s",
266 wpa_s
->ifname
, gtype
, reason
);
269 wpas_notify_p2p_group_removed(wpa_s
, ssid
, gtype
);
271 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
272 struct wpa_global
*global
;
274 enum wpa_driver_if_type type
;
275 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
277 global
= wpa_s
->global
;
278 ifname
= os_strdup(wpa_s
->ifname
);
279 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
280 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
);
281 wpa_s
= global
->ifaces
;
283 wpa_drv_if_remove(wpa_s
, type
, ifname
);
288 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
289 if (ssid
&& (ssid
->p2p_group
||
290 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
291 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
293 if (ssid
== wpa_s
->current_ssid
)
294 wpa_s
->current_ssid
= NULL
;
295 wpas_notify_network_removed(wpa_s
, ssid
);
296 wpa_config_remove_network(wpa_s
->conf
, id
);
297 wpa_supplicant_clear_status(wpa_s
);
299 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
302 wpa_supplicant_ap_deinit(wpa_s
);
306 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
308 const u8
*ssid
, size_t ssid_len
)
316 if (wpa_s
->go_params
)
317 bssid
= wpa_s
->go_params
->peer_interface_addr
;
319 bssid
= wpa_s
->bssid
;
321 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
323 u8 iface_addr
[ETH_ALEN
];
324 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
326 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
329 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
330 "group is persistent - BSS " MACSTR
" not found",
335 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
337 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
338 "group is persistent - BSS " MACSTR
339 " did not include P2P IE", MAC2STR(bssid
));
340 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
341 (u8
*) (bss
+ 1), bss
->ie_len
);
342 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
343 ((u8
*) bss
+ 1) + bss
->ie_len
,
348 group_capab
= p2p_get_group_capab(p2p
);
349 addr
= p2p_get_go_dev_addr(p2p
);
350 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
351 "group_capab=0x%x", group_capab
);
353 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
354 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
357 os_memset(go_dev_addr
, 0, ETH_ALEN
);
360 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
361 "go_dev_addr=" MACSTR
,
362 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
364 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
368 static void wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
369 struct wpa_ssid
*ssid
,
370 const u8
*go_dev_addr
)
375 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
376 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
377 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
378 if (s
->disabled
== 2 &&
379 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
380 s
->ssid_len
== ssid
->ssid_len
&&
381 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
386 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
388 if (ssid
->passphrase
&& !s
->passphrase
)
390 else if (ssid
->passphrase
&& s
->passphrase
&&
391 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
394 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
397 s
= wpa_config_add_network(wpa_s
->conf
);
400 wpa_config_set_network_defaults(s
);
404 s
->p2p_persistent_group
= 1;
407 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
408 s
->mode
= ssid
->mode
;
409 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
410 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
411 s
->proto
= WPA_PROTO_RSN
;
412 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
414 if (ssid
->passphrase
) {
415 os_free(s
->passphrase
);
416 s
->passphrase
= os_strdup(ssid
->passphrase
);
420 os_memcpy(s
->psk
, ssid
->psk
, 32);
422 if (s
->passphrase
&& !s
->psk_set
)
423 wpa_config_update_psk(s
);
424 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
426 s
->ssid
= os_malloc(ssid
->ssid_len
);
429 s
->ssid_len
= ssid
->ssid_len
;
430 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
433 #ifndef CONFIG_NO_CONFIG_WRITE
434 if (changed
&& wpa_s
->conf
->update_config
&&
435 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
436 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
438 #endif /* CONFIG_NO_CONFIG_WRITE */
442 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
445 struct wpa_ssid
*ssid
;
446 const char *ssid_txt
;
449 u8 go_dev_addr
[ETH_ALEN
];
452 * This callback is likely called for the main interface. Update wpa_s
453 * to use the group interface if a new interface was created for the
456 if (wpa_s
->global
->p2p_group_formation
)
457 wpa_s
= wpa_s
->global
->p2p_group_formation
;
458 wpa_s
->global
->p2p_group_formation
= NULL
;
459 wpa_s
->p2p_in_provisioning
= 0;
462 wpa_msg(wpa_s
->parent
, MSG_INFO
,
463 P2P_EVENT_GROUP_FORMATION_FAILURE
);
464 wpas_p2p_group_delete(wpa_s
);
468 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_FORMATION_SUCCESS
);
470 ssid
= wpa_s
->current_ssid
;
471 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
472 ssid
->mode
= WPAS_MODE_P2P_GO
;
473 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
474 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
479 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
480 client
= ssid
->mode
== WPAS_MODE_INFRA
;
481 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
482 persistent
= ssid
->p2p_persistent_group
;
483 os_memcpy(go_dev_addr
, wpa_s
->parent
->own_addr
,
486 persistent
= wpas_p2p_persistent_group(wpa_s
,
492 client
= wpa_s
->p2p_group_interface
==
493 P2P_GROUP_INTERFACE_CLIENT
;
496 wpa_s
->show_group_started
= 0;
499 * Indicate event only after successfully completed 4-way
500 * handshake, i.e., when the interface is ready for data
503 wpa_s
->show_group_started
= 1;
504 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
506 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
507 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
508 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
510 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
511 MAC2STR(go_dev_addr
),
512 persistent
? " [PERSISTENT]" : "");
513 wpas_p2p_cross_connect_setup(wpa_s
);
514 wpas_p2p_set_group_idle_timeout(wpa_s
);
516 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
517 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
518 "go_dev_addr=" MACSTR
"%s",
519 wpa_s
->ifname
, ssid_txt
, ssid
? ssid
->frequency
: 0,
520 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
521 MAC2STR(go_dev_addr
),
522 persistent
? " [PERSISTENT]" : "");
523 wpas_p2p_cross_connect_setup(wpa_s
);
524 wpas_p2p_set_group_idle_timeout(wpa_s
);
528 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
533 static struct wpa_supplicant
*
534 wpas_get_tx_interface(struct wpa_supplicant
*wpa_s
, const u8
*src
)
536 struct wpa_supplicant
*iface
;
538 if (os_memcmp(src
, wpa_s
->own_addr
, ETH_ALEN
) == 0)
542 * Try to find a group interface that matches with the source address.
544 iface
= wpa_s
->global
->ifaces
;
546 if (os_memcmp(wpa_s
->pending_action_src
,
547 iface
->own_addr
, ETH_ALEN
) == 0)
552 wpa_printf(MSG_DEBUG
, "P2P: Use group interface %s "
553 "instead of interface %s for Action TX",
554 iface
->ifname
, wpa_s
->ifname
);
562 static void wpas_send_action_cb(void *eloop_ctx
, void *timeout_ctx
)
564 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
565 struct wpa_supplicant
*iface
;
569 without_roc
= wpa_s
->pending_action_without_roc
;
570 wpa_s
->pending_action_without_roc
= 0;
571 wpa_printf(MSG_DEBUG
, "P2P: Send Action callback (without_roc=%d "
572 "pending_action_tx=%p)",
573 without_roc
, wpa_s
->pending_action_tx
);
575 if (wpa_s
->pending_action_tx
== NULL
)
579 * This call is likely going to be on the P2P device instance if the
580 * driver uses a separate interface for that purpose. However, some
581 * Action frames are actually sent within a P2P Group and when that is
582 * the case, we need to follow power saving (e.g., GO buffering the
583 * frame for a client in PS mode or a client following the advertised
584 * NoA from its GO). To make that easier for the driver, select the
585 * correct group interface here.
587 iface
= wpas_get_tx_interface(wpa_s
, wpa_s
->pending_action_src
);
589 if (wpa_s
->off_channel_freq
!= wpa_s
->pending_action_freq
&&
590 wpa_s
->pending_action_freq
!= 0 &&
591 wpa_s
->pending_action_freq
!= iface
->assoc_freq
) {
592 wpa_printf(MSG_DEBUG
, "P2P: Pending Action frame TX "
593 "waiting for another freq=%u (off_channel_freq=%u "
595 wpa_s
->pending_action_freq
,
596 wpa_s
->off_channel_freq
,
598 if (without_roc
&& wpa_s
->off_channel_freq
== 0) {
600 * We may get here if wpas_send_action() found us to be
601 * on the correct channel, but remain-on-channel cancel
602 * event was received before getting here.
604 wpa_printf(MSG_DEBUG
, "P2P: Schedule "
605 "remain-on-channel to send Action frame");
606 if (wpa_drv_remain_on_channel(
607 wpa_s
, wpa_s
->pending_action_freq
, 200) <
609 wpa_printf(MSG_DEBUG
, "P2P: Failed to request "
610 "driver to remain on channel (%u "
611 "MHz) for Action Frame TX",
612 wpa_s
->pending_action_freq
);
614 wpa_s
->off_channel_freq
= 0;
615 wpa_s
->roc_waiting_drv_freq
=
616 wpa_s
->pending_action_freq
;
622 wpa_printf(MSG_DEBUG
, "P2P: Sending pending Action frame to "
623 MACSTR
" using interface %s",
624 MAC2STR(wpa_s
->pending_action_dst
), iface
->ifname
);
625 res
= wpa_drv_send_action(iface
, wpa_s
->pending_action_freq
, 0,
626 wpa_s
->pending_action_dst
,
627 wpa_s
->pending_action_src
,
628 wpa_s
->pending_action_bssid
,
629 wpabuf_head(wpa_s
->pending_action_tx
),
630 wpabuf_len(wpa_s
->pending_action_tx
));
632 wpa_printf(MSG_DEBUG
, "P2P: Failed to send the pending "
635 * Use fake TX status event to allow P2P state machine to
638 wpas_send_action_tx_status(
639 wpa_s
, wpa_s
->pending_action_dst
,
640 wpabuf_head(wpa_s
->pending_action_tx
),
641 wpabuf_len(wpa_s
->pending_action_tx
),
642 P2P_SEND_ACTION_FAILED
);
647 void wpas_send_action_tx_status(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
648 const u8
*data
, size_t data_len
,
649 enum p2p_send_action_result result
)
651 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
653 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
656 if (wpa_s
->pending_action_tx
== NULL
) {
657 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - no "
658 "pending operation");
662 if (os_memcmp(dst
, wpa_s
->pending_action_dst
, ETH_ALEN
) != 0) {
663 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - unknown "
664 "destination address");
668 wpabuf_free(wpa_s
->pending_action_tx
);
669 wpa_s
->pending_action_tx
= NULL
;
671 p2p_send_action_cb(wpa_s
->global
->p2p
, wpa_s
->pending_action_freq
,
672 wpa_s
->pending_action_dst
,
673 wpa_s
->pending_action_src
,
674 wpa_s
->pending_action_bssid
,
677 if (wpa_s
->pending_pd_before_join
&&
678 (os_memcmp(wpa_s
->pending_action_dst
, wpa_s
->pending_join_dev_addr
,
680 os_memcmp(wpa_s
->pending_action_dst
,
681 wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
682 wpa_s
->pending_pd_before_join
= 0;
683 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
684 "join-existing-group operation");
685 wpas_p2p_join_start(wpa_s
);
690 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
691 const u8
*src
, const u8
*bssid
, const u8
*buf
,
692 size_t len
, unsigned int wait_time
)
694 struct wpa_supplicant
*wpa_s
= ctx
;
696 wpa_printf(MSG_DEBUG
, "P2P: Send action frame: freq=%d dst=" MACSTR
697 " src=" MACSTR
" bssid=" MACSTR
" len=%d",
698 freq
, MAC2STR(dst
), MAC2STR(src
), MAC2STR(bssid
),
701 if (wpa_s
->pending_action_tx
) {
702 wpa_printf(MSG_DEBUG
, "P2P: Dropped pending Action frame TX "
703 "to " MACSTR
, MAC2STR(wpa_s
->pending_action_dst
));
704 wpabuf_free(wpa_s
->pending_action_tx
);
706 wpa_s
->pending_action_tx
= wpabuf_alloc(len
);
707 if (wpa_s
->pending_action_tx
== NULL
) {
708 wpa_printf(MSG_DEBUG
, "P2P: Failed to allocate Action frame "
709 "TX buffer (len=%llu)", (unsigned long long) len
);
712 wpabuf_put_data(wpa_s
->pending_action_tx
, buf
, len
);
713 os_memcpy(wpa_s
->pending_action_src
, src
, ETH_ALEN
);
714 os_memcpy(wpa_s
->pending_action_dst
, dst
, ETH_ALEN
);
715 os_memcpy(wpa_s
->pending_action_bssid
, bssid
, ETH_ALEN
);
716 wpa_s
->pending_action_freq
= freq
;
718 if (freq
!= 0 && wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_OFFCHANNEL_TX
) {
719 struct wpa_supplicant
*iface
;
721 iface
= wpas_get_tx_interface(wpa_s
, wpa_s
->pending_action_src
);
722 wpa_s
->action_tx_wait_time
= wait_time
;
724 return wpa_drv_send_action(iface
, wpa_s
->pending_action_freq
,
725 wait_time
, wpa_s
->pending_action_dst
,
726 wpa_s
->pending_action_src
,
727 wpa_s
->pending_action_bssid
,
728 wpabuf_head(wpa_s
->pending_action_tx
),
729 wpabuf_len(wpa_s
->pending_action_tx
));
733 struct wpa_supplicant
*tx_iface
;
734 tx_iface
= wpas_get_tx_interface(wpa_s
, src
);
735 if (tx_iface
->assoc_freq
== freq
) {
736 wpa_printf(MSG_DEBUG
, "P2P: Already on requested "
737 "channel (TX interface operating channel)");
742 if (wpa_s
->off_channel_freq
== freq
|| freq
== 0) {
743 wpa_printf(MSG_DEBUG
, "P2P: Already on requested channel; "
744 "send Action frame immediately");
745 /* TODO: Would there ever be need to extend the current
746 * duration on the channel? */
747 wpa_s
->pending_action_without_roc
= 1;
748 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
749 eloop_register_timeout(0, 0, wpas_send_action_cb
, wpa_s
, NULL
);
752 wpa_s
->pending_action_without_roc
= 0;
754 if (wpa_s
->roc_waiting_drv_freq
== freq
) {
755 wpa_printf(MSG_DEBUG
, "P2P: Already waiting for driver to get "
756 "to frequency %u MHz; continue waiting to send the "
757 "Action frame", freq
);
761 wpa_printf(MSG_DEBUG
, "P2P: Schedule Action frame to be transmitted "
762 "once the driver gets to the requested channel");
763 if (wait_time
> wpa_s
->max_remain_on_chan
)
764 wait_time
= wpa_s
->max_remain_on_chan
;
765 if (wpa_drv_remain_on_channel(wpa_s
, freq
, wait_time
) < 0) {
766 wpa_printf(MSG_DEBUG
, "P2P: Failed to request driver "
767 "to remain on channel (%u MHz) for Action "
771 wpa_s
->off_channel_freq
= 0;
772 wpa_s
->roc_waiting_drv_freq
= freq
;
778 static void wpas_send_action_done(void *ctx
)
780 struct wpa_supplicant
*wpa_s
= ctx
;
781 wpa_printf(MSG_DEBUG
, "P2P: Action frame sequence done notification");
782 wpabuf_free(wpa_s
->pending_action_tx
);
783 wpa_s
->pending_action_tx
= NULL
;
784 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_OFFCHANNEL_TX
) {
785 if (wpa_s
->action_tx_wait_time
)
786 wpa_drv_send_action_cancel_wait(wpa_s
);
787 wpa_s
->off_channel_freq
= 0;
788 } else if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
789 wpa_drv_cancel_remain_on_channel(wpa_s
);
790 wpa_s
->off_channel_freq
= 0;
791 wpa_s
->roc_waiting_drv_freq
= 0;
796 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
797 struct p2p_go_neg_results
*params
)
799 if (wpa_s
->go_params
== NULL
) {
800 wpa_s
->go_params
= os_malloc(sizeof(*params
));
801 if (wpa_s
->go_params
== NULL
)
804 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
809 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
810 struct p2p_go_neg_results
*res
)
812 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
813 MAC2STR(res
->peer_interface_addr
));
814 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
815 res
->ssid
, res
->ssid_len
);
816 wpa_supplicant_ap_deinit(wpa_s
);
817 wpas_copy_go_neg_results(wpa_s
, res
);
818 if (res
->wps_method
== WPS_PBC
)
819 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
821 u16 dev_pw_id
= DEV_PW_DEFAULT
;
822 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
823 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
824 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
825 wpa_s
->p2p_pin
, 1, dev_pw_id
);
830 static void p2p_go_configured(void *ctx
, void *data
)
832 struct wpa_supplicant
*wpa_s
= ctx
;
833 struct p2p_go_neg_results
*params
= data
;
834 struct wpa_ssid
*ssid
;
836 ssid
= wpa_s
->current_ssid
;
837 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
838 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
839 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
840 wpa_s
->global
->p2p_group_formation
= NULL
;
841 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
842 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
843 "go_dev_addr=" MACSTR
"%s",
845 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
847 params
->passphrase
? params
->passphrase
: "",
848 MAC2STR(wpa_s
->parent
->own_addr
),
849 params
->persistent_group
? " [PERSISTENT]" : "");
850 if (params
->persistent_group
)
851 wpas_p2p_store_persistent_group(
853 wpa_s
->parent
->own_addr
);
854 wpas_p2p_cross_connect_setup(wpa_s
);
855 wpas_p2p_set_group_idle_timeout(wpa_s
);
859 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
860 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
861 params
->peer_interface_addr
)) {
862 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
866 if (params
->wps_method
== WPS_PBC
)
867 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
,
869 else if (wpa_s
->p2p_pin
[0])
870 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
871 wpa_s
->p2p_pin
, NULL
, 0);
872 os_free(wpa_s
->go_params
);
873 wpa_s
->go_params
= NULL
;
877 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
878 struct p2p_go_neg_results
*params
,
881 struct wpa_ssid
*ssid
;
883 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0)
886 ssid
= wpa_config_add_network(wpa_s
->conf
);
890 wpas_notify_network_added(wpa_s
, ssid
);
891 wpa_config_set_network_defaults(ssid
);
894 ssid
->p2p_persistent_group
= params
->persistent_group
;
895 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
897 ssid
->frequency
= params
->freq
;
898 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
900 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
901 ssid
->ssid_len
= params
->ssid_len
;
903 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
904 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
905 ssid
->proto
= WPA_PROTO_RSN
;
906 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
907 ssid
->passphrase
= os_strdup(params
->passphrase
);
909 wpa_s
->ap_configured_cb
= p2p_go_configured
;
910 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
911 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
912 wpa_s
->connect_without_scan
= 1;
913 wpa_s
->reassociate
= 1;
914 wpa_s
->disconnected
= 0;
915 wpa_supplicant_req_scan(wpa_s
, 0, 0);
919 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
920 const struct wpa_supplicant
*src
)
922 struct wpa_config
*d
;
923 const struct wpa_config
*s
;
928 #define C(n) if (s->n) d->n = os_strdup(s->n)
938 d
->p2p_group_idle
= s
->p2p_group_idle
;
939 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
943 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
944 enum wpa_driver_if_type type
)
946 char ifname
[120], force_ifname
[120];
948 if (wpa_s
->pending_interface_name
[0]) {
949 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
950 "- skip creation of a new one");
951 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
952 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
953 "unknown?! ifname='%s'",
954 wpa_s
->pending_interface_name
);
960 os_snprintf(ifname
, sizeof(ifname
), "p2p-%s-%d", wpa_s
->ifname
,
961 wpa_s
->p2p_group_idx
);
962 if (os_strlen(ifname
) >= IFNAMSIZ
&&
963 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
964 /* Try to avoid going over the IFNAMSIZ length limit */
965 os_snprintf(ifname
, sizeof(ifname
), "p2p-%d",
966 wpa_s
->p2p_group_idx
);
968 force_ifname
[0] = '\0';
970 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
972 wpa_s
->p2p_group_idx
++;
974 wpa_s
->pending_interface_type
= type
;
975 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
976 wpa_s
->pending_interface_addr
, NULL
) < 0) {
977 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
982 if (force_ifname
[0]) {
983 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
985 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
986 sizeof(wpa_s
->pending_interface_name
));
988 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
989 sizeof(wpa_s
->pending_interface_name
));
990 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
991 MACSTR
, wpa_s
->pending_interface_name
,
992 MAC2STR(wpa_s
->pending_interface_addr
));
998 static void wpas_p2p_remove_pending_group_interface(
999 struct wpa_supplicant
*wpa_s
)
1001 if (!wpa_s
->pending_interface_name
[0] ||
1002 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
1003 return; /* No pending virtual interface */
1005 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
1006 wpa_s
->pending_interface_name
);
1007 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
1008 wpa_s
->pending_interface_name
);
1009 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1010 wpa_s
->pending_interface_name
[0] = '\0';
1014 static struct wpa_supplicant
*
1015 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
1017 struct wpa_interface iface
;
1018 struct wpa_supplicant
*group_wpa_s
;
1020 if (!wpa_s
->pending_interface_name
[0]) {
1021 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
1022 if (!wpas_p2p_create_iface(wpa_s
))
1025 * Something has forced us to remove the pending interface; try
1026 * to create a new one and hope for the best that we will get
1027 * the same local address.
1029 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
1030 WPA_IF_P2P_CLIENT
) < 0)
1034 os_memset(&iface
, 0, sizeof(iface
));
1035 iface
.ifname
= wpa_s
->pending_interface_name
;
1036 iface
.driver
= wpa_s
->driver
->name
;
1037 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
1038 iface
.driver_param
= wpa_s
->conf
->driver_param
;
1039 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
1040 if (group_wpa_s
== NULL
) {
1041 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
1042 "wpa_supplicant interface");
1045 wpa_s
->pending_interface_name
[0] = '\0';
1046 group_wpa_s
->parent
= wpa_s
;
1047 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
1048 P2P_GROUP_INTERFACE_CLIENT
;
1049 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
1051 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
1057 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
1060 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
1061 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
1062 if (wpa_s
->global
->p2p
)
1063 p2p_group_formation_failed(wpa_s
->global
->p2p
);
1064 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1065 wpa_drv_p2p_group_formation_failed(wpa_s
);
1066 wpas_group_formation_completed(wpa_s
, 0);
1070 void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
1072 struct wpa_supplicant
*wpa_s
= ctx
;
1074 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1075 wpa_drv_cancel_remain_on_channel(wpa_s
);
1076 wpa_s
->off_channel_freq
= 0;
1077 wpa_s
->roc_waiting_drv_freq
= 0;
1081 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_FAILURE
"status=%d",
1083 wpas_notify_p2p_go_neg_completed(wpa_s
, res
->status
);
1084 wpas_p2p_remove_pending_group_interface(wpa_s
);
1088 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
);
1089 wpas_notify_p2p_go_neg_completed(wpa_s
, P2P_SC_SUCCESS
);
1091 if (wpa_s
->create_p2p_iface
) {
1092 struct wpa_supplicant
*group_wpa_s
=
1093 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
1094 if (group_wpa_s
== NULL
) {
1095 wpas_p2p_remove_pending_group_interface(wpa_s
);
1098 if (group_wpa_s
!= wpa_s
) {
1099 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
1100 sizeof(group_wpa_s
->p2p_pin
));
1101 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
1103 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1104 wpa_s
->pending_interface_name
[0] = '\0';
1105 group_wpa_s
->p2p_in_provisioning
= 1;
1108 wpas_start_wps_go(group_wpa_s
, res
, 1);
1110 wpas_start_wps_enrollee(group_wpa_s
, res
);
1112 wpa_s
->p2p_in_provisioning
= 1;
1113 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1116 wpas_start_wps_go(wpa_s
, res
, 1);
1118 wpas_start_wps_enrollee(ctx
, res
);
1121 wpa_s
->p2p_long_listen
= 0;
1122 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1124 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1125 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1126 (res
->peer_config_timeout
% 100) * 10000,
1127 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1131 void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1133 struct wpa_supplicant
*wpa_s
= ctx
;
1134 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1135 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1137 wpas_notify_p2p_go_neg_req(wpa_s
, src
, dev_passwd_id
);
1141 void wpas_dev_found(void *ctx
, const u8
*addr
,
1142 const struct p2p_peer_info
*info
,
1145 struct wpa_supplicant
*wpa_s
= ctx
;
1146 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1148 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1149 " p2p_dev_addr=" MACSTR
1150 " pri_dev_type=%s name='%s' config_methods=0x%x "
1151 "dev_capab=0x%x group_capab=0x%x",
1152 MAC2STR(addr
), MAC2STR(info
->p2p_device_addr
),
1153 wps_dev_type_bin2str(info
->pri_dev_type
, devtype
,
1155 info
->device_name
, info
->config_methods
,
1156 info
->dev_capab
, info
->group_capab
);
1158 wpas_notify_p2p_device_found(ctx
, info
->p2p_device_addr
, new_device
);
1162 static void wpas_dev_lost(void *ctx
, const u8
*dev_addr
)
1164 struct wpa_supplicant
*wpa_s
= ctx
;
1166 wpas_notify_p2p_device_lost(wpa_s
, dev_addr
);
1170 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1171 unsigned int duration
,
1172 const struct wpabuf
*probe_resp_ie
)
1174 struct wpa_supplicant
*wpa_s
= ctx
;
1176 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, probe_resp_ie
, NULL
);
1178 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1179 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1180 "report received Probe Request frames");
1184 wpa_s
->pending_listen_freq
= freq
;
1185 wpa_s
->pending_listen_duration
= duration
;
1187 if (wpa_drv_remain_on_channel(wpa_s
, freq
, duration
) < 0) {
1188 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1189 "to remain on channel (%u MHz) for Listen "
1191 wpa_s
->pending_listen_freq
= 0;
1194 wpa_s
->off_channel_freq
= 0;
1195 wpa_s
->roc_waiting_drv_freq
= freq
;
1201 static void wpas_stop_listen(void *ctx
)
1203 struct wpa_supplicant
*wpa_s
= ctx
;
1204 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1205 wpa_drv_cancel_remain_on_channel(wpa_s
);
1206 wpa_s
->off_channel_freq
= 0;
1207 wpa_s
->roc_waiting_drv_freq
= 0;
1209 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, NULL
, NULL
);
1210 wpa_drv_probe_req_report(wpa_s
, 0);
1214 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1216 struct wpa_supplicant
*wpa_s
= ctx
;
1217 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
));
1221 static struct p2p_srv_bonjour
*
1222 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1223 const struct wpabuf
*query
)
1225 struct p2p_srv_bonjour
*bsrv
;
1228 len
= wpabuf_len(query
);
1229 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1230 struct p2p_srv_bonjour
, list
) {
1231 if (len
== wpabuf_len(bsrv
->query
) &&
1232 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1240 static struct p2p_srv_upnp
*
1241 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1242 const char *service
)
1244 struct p2p_srv_upnp
*usrv
;
1246 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1247 struct p2p_srv_upnp
, list
) {
1248 if (version
== usrv
->version
&&
1249 os_strcmp(service
, usrv
->service
) == 0)
1256 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1261 if (wpabuf_tailroom(resp
) < 5)
1264 /* Length (to be filled) */
1265 len_pos
= wpabuf_put(resp
, 2);
1266 wpabuf_put_u8(resp
, srv_proto
);
1267 wpabuf_put_u8(resp
, srv_trans_id
);
1269 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1270 /* Response Data: empty */
1271 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1275 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1276 struct wpabuf
*resp
, u8 srv_trans_id
)
1278 struct p2p_srv_bonjour
*bsrv
;
1281 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1283 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1284 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1288 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1289 struct p2p_srv_bonjour
, list
) {
1290 if (wpabuf_tailroom(resp
) <
1291 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1293 /* Length (to be filled) */
1294 len_pos
= wpabuf_put(resp
, 2);
1295 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1296 wpabuf_put_u8(resp
, srv_trans_id
);
1298 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1299 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1300 wpabuf_head(bsrv
->resp
),
1301 wpabuf_len(bsrv
->resp
));
1303 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1304 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1305 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1311 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
1312 struct wpabuf
*resp
, u8 srv_trans_id
,
1313 const u8
*query
, size_t query_len
)
1315 struct p2p_srv_bonjour
*bsrv
;
1319 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
1321 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1322 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1323 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
1328 if (query_len
== 0) {
1329 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1333 if (wpabuf_tailroom(resp
) < 5)
1335 /* Length (to be filled) */
1336 len_pos
= wpabuf_put(resp
, 2);
1337 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1338 wpabuf_put_u8(resp
, srv_trans_id
);
1340 wpabuf_set(&buf
, query
, query_len
);
1341 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, &buf
);
1343 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
1347 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1348 /* Response Data: empty */
1349 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1355 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1356 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1357 wpabuf_head(bsrv
->resp
), wpabuf_len(bsrv
->resp
));
1359 if (wpabuf_tailroom(resp
) >=
1360 wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
)) {
1362 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1363 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1365 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1369 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
1370 struct wpabuf
*resp
, u8 srv_trans_id
)
1372 struct p2p_srv_upnp
*usrv
;
1375 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
1377 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1378 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1382 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1383 struct p2p_srv_upnp
, list
) {
1384 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
1387 /* Length (to be filled) */
1388 len_pos
= wpabuf_put(resp
, 2);
1389 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1390 wpabuf_put_u8(resp
, srv_trans_id
);
1393 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1395 wpabuf_put_u8(resp
, usrv
->version
);
1396 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1398 wpabuf_put_str(resp
, usrv
->service
);
1399 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1405 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
1406 struct wpabuf
*resp
, u8 srv_trans_id
,
1407 const u8
*query
, size_t query_len
)
1409 struct p2p_srv_upnp
*usrv
;
1415 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
1418 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1419 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1420 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
1425 if (query_len
== 0) {
1426 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1430 if (wpabuf_tailroom(resp
) < 5)
1433 /* Length (to be filled) */
1434 len_pos
= wpabuf_put(resp
, 2);
1435 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1436 wpabuf_put_u8(resp
, srv_trans_id
);
1439 str
= os_malloc(query_len
);
1442 os_memcpy(str
, query
+ 1, query_len
- 1);
1443 str
[query_len
- 1] = '\0';
1445 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1446 struct p2p_srv_upnp
, list
) {
1447 if (version
!= usrv
->version
)
1450 if (os_strcmp(str
, "ssdp:all") != 0 &&
1451 os_strstr(usrv
->service
, str
) == NULL
)
1454 if (wpabuf_tailroom(resp
) < 2)
1458 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1460 wpabuf_put_u8(resp
, version
);
1462 wpabuf_put_u8(resp
, ',');
1466 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1468 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
1470 wpabuf_put_str(resp
, usrv
->service
);
1474 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
1477 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1478 /* Response Data: empty */
1481 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1485 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
1486 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
1488 struct wpa_supplicant
*wpa_s
= ctx
;
1489 const u8
*pos
= tlvs
;
1490 const u8
*end
= tlvs
+ tlvs_len
;
1493 struct wpabuf
*resp
;
1494 u8 srv_proto
, srv_trans_id
;
1498 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
1500 buf_len
= 2 * tlvs_len
+ 1;
1501 buf
= os_malloc(buf_len
);
1503 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1504 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
1506 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
1511 if (wpa_s
->p2p_sd_over_ctrl_iface
)
1512 return; /* to be processed by an external program */
1514 resp
= wpabuf_alloc(10000);
1518 while (pos
+ 1 < end
) {
1519 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
1520 slen
= WPA_GET_LE16(pos
);
1522 if (pos
+ slen
> end
|| slen
< 2) {
1523 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
1528 tlv_end
= pos
+ slen
;
1531 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1533 srv_trans_id
= *pos
++;
1534 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1537 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
1538 pos
, tlv_end
- pos
);
1541 if (wpa_s
->force_long_sd
) {
1542 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
1544 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1545 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1549 switch (srv_proto
) {
1550 case P2P_SERV_ALL_SERVICES
:
1551 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
1552 "for all services");
1553 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
1554 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1555 wpa_printf(MSG_DEBUG
, "P2P: No service "
1556 "discovery protocols available");
1557 wpas_sd_add_proto_not_avail(
1558 resp
, P2P_SERV_ALL_SERVICES
,
1562 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1563 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1565 case P2P_SERV_BONJOUR
:
1566 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
1567 pos
, tlv_end
- pos
);
1570 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
1571 pos
, tlv_end
- pos
);
1574 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
1575 "protocol %u", srv_proto
);
1576 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
1585 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
1586 update_indic
, tlvs
, tlvs_len
);
1588 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
1594 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
1595 const u8
*tlvs
, size_t tlvs_len
)
1597 struct wpa_supplicant
*wpa_s
= ctx
;
1598 const u8
*pos
= tlvs
;
1599 const u8
*end
= tlvs
+ tlvs_len
;
1605 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
1607 if (tlvs_len
> 1500) {
1608 /* TODO: better way for handling this */
1609 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1610 P2P_EVENT_SERV_DISC_RESP MACSTR
1611 " %u <long response: %u bytes>",
1612 MAC2STR(sa
), update_indic
,
1613 (unsigned int) tlvs_len
);
1615 buf_len
= 2 * tlvs_len
+ 1;
1616 buf
= os_malloc(buf_len
);
1618 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1619 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1620 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
1621 MAC2STR(sa
), update_indic
, buf
);
1627 u8 srv_proto
, srv_trans_id
, status
;
1629 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
1630 slen
= WPA_GET_LE16(pos
);
1632 if (pos
+ slen
> end
|| slen
< 3) {
1633 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
1637 tlv_end
= pos
+ slen
;
1640 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1642 srv_trans_id
= *pos
++;
1643 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1646 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
1649 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
1650 pos
, tlv_end
- pos
);
1655 wpas_notify_p2p_sd_response(wpa_s
, sa
, update_indic
, tlvs
, tlvs_len
);
1659 void * wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1660 const struct wpabuf
*tlvs
)
1662 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1663 return (void *) wpa_drv_p2p_sd_request(wpa_s
, dst
, tlvs
);
1664 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1666 return p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
1670 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1671 u8 version
, const char *query
)
1673 struct wpabuf
*tlvs
;
1676 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
1679 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
1680 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
1681 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
1682 wpabuf_put_u8(tlvs
, version
);
1683 wpabuf_put_str(tlvs
, query
);
1684 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
1690 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, void *req
)
1692 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1693 return wpa_drv_p2p_sd_cancel_request(wpa_s
, (u64
) req
);
1694 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1696 return p2p_sd_cancel_request(wpa_s
->global
->p2p
, req
);
1700 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
1701 const u8
*dst
, u8 dialog_token
,
1702 const struct wpabuf
*resp_tlvs
)
1704 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1705 wpa_drv_p2p_sd_response(wpa_s
, freq
, dst
, dialog_token
,
1709 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1711 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
1716 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
1718 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1719 wpa_drv_p2p_service_update(wpa_s
);
1722 if (wpa_s
->global
->p2p
)
1723 p2p_sd_service_update(wpa_s
->global
->p2p
);
1727 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
1729 dl_list_del(&bsrv
->list
);
1730 wpabuf_free(bsrv
->query
);
1731 wpabuf_free(bsrv
->resp
);
1736 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
1738 dl_list_del(&usrv
->list
);
1739 os_free(usrv
->service
);
1744 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
1746 struct p2p_srv_bonjour
*bsrv
, *bn
;
1747 struct p2p_srv_upnp
*usrv
, *un
;
1749 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
1750 struct p2p_srv_bonjour
, list
)
1751 wpas_p2p_srv_bonjour_free(bsrv
);
1753 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
1754 struct p2p_srv_upnp
, list
)
1755 wpas_p2p_srv_upnp_free(usrv
);
1757 wpas_p2p_sd_service_update(wpa_s
);
1761 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
1762 struct wpabuf
*query
, struct wpabuf
*resp
)
1764 struct p2p_srv_bonjour
*bsrv
;
1766 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1769 wpabuf_free(bsrv
->resp
);
1774 bsrv
= os_zalloc(sizeof(*bsrv
));
1777 bsrv
->query
= query
;
1779 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
1781 wpas_p2p_sd_service_update(wpa_s
);
1786 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
1787 const struct wpabuf
*query
)
1789 struct p2p_srv_bonjour
*bsrv
;
1791 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1794 wpas_p2p_srv_bonjour_free(bsrv
);
1795 wpas_p2p_sd_service_update(wpa_s
);
1800 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1801 const char *service
)
1803 struct p2p_srv_upnp
*usrv
;
1805 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
1806 return 0; /* Already listed */
1807 usrv
= os_zalloc(sizeof(*usrv
));
1810 usrv
->version
= version
;
1811 usrv
->service
= os_strdup(service
);
1812 if (usrv
->service
== NULL
) {
1816 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
1818 wpas_p2p_sd_service_update(wpa_s
);
1823 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1824 const char *service
)
1826 struct p2p_srv_upnp
*usrv
;
1828 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
1831 wpas_p2p_srv_upnp_free(usrv
);
1832 wpas_p2p_sd_service_update(wpa_s
);
1837 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
1838 const u8
*peer
, const char *params
,
1839 unsigned int generated_pin
)
1841 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
" %08d%s",
1842 MAC2STR(peer
), generated_pin
, params
);
1846 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
1847 const u8
*peer
, const char *params
)
1849 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
"%s",
1850 MAC2STR(peer
), params
);
1854 void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
1855 const u8
*dev_addr
, const u8
*pri_dev_type
,
1856 const char *dev_name
, u16 supp_config_methods
,
1857 u8 dev_capab
, u8 group_capab
)
1859 struct wpa_supplicant
*wpa_s
= ctx
;
1860 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1862 u8 empty_dev_type
[8];
1863 unsigned int generated_pin
= 0;
1865 if (pri_dev_type
== NULL
) {
1866 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
1867 pri_dev_type
= empty_dev_type
;
1869 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
1870 " pri_dev_type=%s name='%s' config_methods=0x%x "
1871 "dev_capab=0x%x group_capab=0x%x",
1873 wps_dev_type_bin2str(pri_dev_type
, devtype
,
1875 dev_name
, supp_config_methods
, dev_capab
, group_capab
);
1876 params
[sizeof(params
) - 1] = '\0';
1878 if (config_methods
& WPS_CONFIG_DISPLAY
) {
1879 generated_pin
= wps_generate_pin();
1880 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
1882 } else if (config_methods
& WPS_CONFIG_KEYPAD
)
1883 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
1884 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1885 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1886 "%s", MAC2STR(peer
), params
);
1890 void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
1892 struct wpa_supplicant
*wpa_s
= ctx
;
1893 unsigned int generated_pin
= 0;
1895 if (config_methods
& WPS_CONFIG_DISPLAY
)
1896 wpas_prov_disc_local_keypad(wpa_s
, peer
, "");
1897 else if (config_methods
& WPS_CONFIG_KEYPAD
) {
1898 generated_pin
= wps_generate_pin();
1899 wpas_prov_disc_local_display(wpa_s
, peer
, "", generated_pin
);
1900 } else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1901 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
,
1904 if (wpa_s
->pending_pd_before_join
&&
1905 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
1906 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
1907 wpa_s
->pending_pd_before_join
= 0;
1908 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
1909 "join-existing-group operation");
1910 wpas_p2p_join_start(wpa_s
);
1915 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
1916 const u8
*go_dev_addr
, const u8
*ssid
,
1917 size_t ssid_len
, int *go
, u8
*group_bssid
,
1918 int *force_freq
, int persistent_group
)
1920 struct wpa_supplicant
*wpa_s
= ctx
;
1922 u8 cur_bssid
[ETH_ALEN
];
1924 struct wpa_supplicant
*grp
;
1926 if (!persistent_group
) {
1927 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1928 " to join an active group", MAC2STR(sa
));
1929 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
1930 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
1932 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
1933 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
1934 "authorized invitation");
1938 * Do not accept the invitation automatically; notify user and
1941 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1944 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
1946 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
1947 "running persistent group");
1949 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
1953 if (!wpa_s
->conf
->persistent_reconnect
)
1954 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1956 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1957 if (s
->disabled
== 2 &&
1958 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
1959 s
->ssid_len
== ssid_len
&&
1960 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
1965 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1966 " requested reinvocation of an unknown group",
1968 return P2P_SC_FAIL_UNKNOWN_GROUP
;
1971 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
1973 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
1974 wpa_printf(MSG_DEBUG
, "P2P: The only available "
1975 "interface is already in use - reject "
1977 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1979 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
1980 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
1982 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
1984 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
1985 "interface address for the group");
1986 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1988 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
1993 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, cur_bssid
) == 0 &&
1994 wpa_s
->assoc_freq
) {
1995 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
1996 "the channel we are already using");
1997 *force_freq
= wpa_s
->assoc_freq
;
2000 res
= wpa_drv_shared_freq(wpa_s
);
2002 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
2003 "with the channel we are already using on a "
2004 "shared interface");
2008 return P2P_SC_SUCCESS
;
2012 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
2013 const u8
*ssid
, size_t ssid_len
,
2014 const u8
*go_dev_addr
, u8 status
,
2017 struct wpa_supplicant
*wpa_s
= ctx
;
2020 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2021 if (s
->disabled
== 2 &&
2022 s
->ssid_len
== ssid_len
&&
2023 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2027 if (status
== P2P_SC_SUCCESS
) {
2028 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2029 " was accepted; op_freq=%d MHz",
2030 MAC2STR(sa
), op_freq
);
2032 wpas_p2p_group_add_persistent(
2033 wpa_s
, s
, s
->mode
== WPAS_MODE_P2P_GO
, 0);
2035 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
2036 wpa_s
->p2p_wps_method
);
2041 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
2042 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2043 " was rejected (status %u)", MAC2STR(sa
), status
);
2049 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2050 "sa=" MACSTR
" go_dev_addr=" MACSTR
2051 " bssid=" MACSTR
" unknown-network",
2052 MAC2STR(sa
), MAC2STR(go_dev_addr
),
2055 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2056 "sa=" MACSTR
" go_dev_addr=" MACSTR
2058 MAC2STR(sa
), MAC2STR(go_dev_addr
));
2063 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
"sa=" MACSTR
2064 " persistent=%d", MAC2STR(sa
), s
->id
);
2068 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
)
2070 struct wpa_supplicant
*wpa_s
= ctx
;
2071 struct wpa_ssid
*ssid
;
2074 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2075 "status=%d " MACSTR
,
2076 status
, MAC2STR(bssid
));
2078 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2079 "status=%d ", status
);
2081 wpas_notify_p2p_invitation_result(wpa_s
, status
, bssid
);
2083 if (wpa_s
->pending_invite_ssid_id
== -1)
2084 return; /* Invitation to active group */
2086 if (status
!= P2P_SC_SUCCESS
) {
2087 wpas_p2p_remove_pending_group_interface(wpa_s
);
2091 ssid
= wpa_config_get_network(wpa_s
->conf
,
2092 wpa_s
->pending_invite_ssid_id
);
2094 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
2095 "data matching with invitation");
2099 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
2100 ssid
->mode
== WPAS_MODE_P2P_GO
, 0);
2104 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
2105 struct p2p_channels
*chan
)
2109 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
2112 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2113 chan
->reg_class
[cla
].reg_class
= 81;
2114 chan
->reg_class
[cla
].channels
= 11;
2115 for (i
= 0; i
< 11; i
++)
2116 chan
->reg_class
[cla
].channel
[i
] = i
+ 1;
2119 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
2122 /* Operating class 115 - 5 GHz, channels 36-48 */
2123 chan
->reg_class
[cla
].reg_class
= 115;
2124 chan
->reg_class
[cla
].channels
= 4;
2125 chan
->reg_class
[cla
].channel
[0] = 36;
2126 chan
->reg_class
[cla
].channel
[1] = 40;
2127 chan
->reg_class
[cla
].channel
[2] = 44;
2128 chan
->reg_class
[cla
].channel
[3] = 48;
2131 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
2134 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2135 chan
->reg_class
[cla
].reg_class
= 124;
2136 chan
->reg_class
[cla
].channels
= 4;
2137 chan
->reg_class
[cla
].channel
[0] = 149;
2138 chan
->reg_class
[cla
].channel
[1] = 153;
2139 chan
->reg_class
[cla
].channel
[2] = 157;
2140 chan
->reg_class
[cla
].channel
[3] = 161;
2143 chan
->reg_classes
= cla
;
2148 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
2150 enum hostapd_hw_mode mode
)
2154 for (i
= 0; i
< num_modes
; i
++) {
2155 if (modes
[i
].mode
== mode
)
2163 static int has_channel(struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
2167 for (i
= 0; i
< mode
->num_channels
; i
++) {
2168 if (mode
->channels
[i
].chan
== chan
) {
2170 *flags
= mode
->channels
[i
].flag
;
2171 return !(mode
->channels
[i
].flag
&
2172 (HOSTAPD_CHAN_DISABLED
|
2173 HOSTAPD_CHAN_PASSIVE_SCAN
|
2174 HOSTAPD_CHAN_NO_IBSS
|
2175 HOSTAPD_CHAN_RADAR
));
2183 struct p2p_oper_class_map
{
2184 enum hostapd_hw_mode mode
;
2189 enum { BW20
, BW40PLUS
, BW40MINUS
} bw
;
2192 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
2193 struct p2p_channels
*chan
)
2195 struct hostapd_hw_modes
*modes
, *mode
;
2196 u16 num_modes
, flags
;
2198 struct p2p_oper_class_map op_class
[] = {
2199 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
2200 { HOSTAPD_MODE_IEEE80211G
, 82, 14, 14, 1, BW20
},
2201 #if 0 /* Do not enable HT40 on 2 GHz for now */
2202 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
2203 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
2205 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
2206 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
2207 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
2208 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
2209 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
2210 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
2211 { -1, 0, 0, 0, 0, BW20
}
2214 modes
= wpa_drv_get_hw_feature_data(wpa_s
, &num_modes
, &flags
);
2215 if (modes
== NULL
) {
2216 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
2217 "of all supported channels; assume dualband "
2219 return wpas_p2p_default_channels(wpa_s
, chan
);
2224 for (op
= 0; op_class
[op
].op_class
; op
++) {
2225 struct p2p_oper_class_map
*o
= &op_class
[op
];
2227 struct p2p_reg_class
*reg
= NULL
;
2229 mode
= get_mode(modes
, num_modes
, o
->mode
);
2232 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
2234 if (!has_channel(mode
, ch
, &flag
))
2236 if (o
->bw
== BW40MINUS
&&
2237 (!(flag
& HOSTAPD_CHAN_HT40MINUS
) ||
2238 !has_channel(mode
, ch
- 4, NULL
)))
2240 if (o
->bw
== BW40PLUS
&&
2241 (!(flag
& HOSTAPD_CHAN_HT40PLUS
) ||
2242 !has_channel(mode
, ch
+ 4, NULL
)))
2245 wpa_printf(MSG_DEBUG
, "P2P: Add operating "
2246 "class %u", o
->op_class
);
2247 reg
= &chan
->reg_class
[cla
];
2249 reg
->reg_class
= o
->op_class
;
2251 reg
->channel
[reg
->channels
] = ch
;
2255 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
2256 reg
->channel
, reg
->channels
);
2260 chan
->reg_classes
= cla
;
2262 ieee80211_sta_free_hw_features(modes
, num_modes
);
2268 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
2271 struct wpa_supplicant
*wpa_s
= ctx
;
2273 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2274 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
2280 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
2285 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2286 * @global: Pointer to global data from wpa_supplicant_init()
2287 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2288 * Returns: 0 on success, -1 on failure
2290 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
2292 struct p2p_config p2p
;
2296 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
2299 #ifdef CONFIG_CLIENT_MLME
2300 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)) {
2301 wpa_s
->mlme
.public_action_cb
= p2p_rx_action_mlme
;
2302 wpa_s
->mlme
.public_action_cb_ctx
= wpa_s
;
2304 #endif /* CONFIG_CLIENT_MLME */
2306 if (wpa_drv_disable_11b_rates(wpa_s
, 1) < 0) {
2307 wpa_printf(MSG_DEBUG
, "P2P: Failed to disable 11b rates");
2308 /* Continue anyway; this is not really a fatal error */
2314 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2315 struct p2p_params params
;
2317 wpa_printf(MSG_DEBUG
, "P2P: Use driver-based P2P management");
2318 os_memset(¶ms
, 0, sizeof(params
));
2319 params
.dev_name
= wpa_s
->conf
->device_name
;
2320 if (wpa_s
->conf
->device_type
&&
2321 wps_dev_type_str2bin(wpa_s
->conf
->device_type
,
2322 params
.pri_dev_type
) < 0) {
2323 wpa_printf(MSG_ERROR
, "P2P: Invalid device_type");
2326 for (i
= 0; i
< MAX_SEC_DEVICE_TYPES
; i
++) {
2327 if (wpa_s
->conf
->sec_device_type
[i
] == NULL
)
2329 if (wps_dev_type_str2bin(
2330 wpa_s
->conf
->sec_device_type
[i
],
2331 params
.sec_dev_type
[
2332 params
.num_sec_dev_types
]) < 0) {
2333 wpa_printf(MSG_ERROR
, "P2P: Invalid "
2337 params
.num_sec_dev_types
++;
2338 if (params
.num_sec_dev_types
== DRV_MAX_SEC_DEV_TYPES
)
2341 if (wpa_drv_p2p_set_params(wpa_s
, ¶ms
) < 0)
2347 os_memset(&p2p
, 0, sizeof(p2p
));
2348 p2p
.msg_ctx
= wpa_s
;
2350 p2p
.p2p_scan
= wpas_p2p_scan
;
2351 p2p
.send_action
= wpas_send_action
;
2352 p2p
.send_action_done
= wpas_send_action_done
;
2353 p2p
.go_neg_completed
= wpas_go_neg_completed
;
2354 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
2355 p2p
.dev_found
= wpas_dev_found
;
2356 p2p
.dev_lost
= wpas_dev_lost
;
2357 p2p
.start_listen
= wpas_start_listen
;
2358 p2p
.stop_listen
= wpas_stop_listen
;
2359 p2p
.send_probe_resp
= wpas_send_probe_resp
;
2360 p2p
.sd_request
= wpas_sd_request
;
2361 p2p
.sd_response
= wpas_sd_response
;
2362 p2p
.prov_disc_req
= wpas_prov_disc_req
;
2363 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
2364 p2p
.invitation_process
= wpas_invitation_process
;
2365 p2p
.invitation_received
= wpas_invitation_received
;
2366 p2p
.invitation_result
= wpas_invitation_result
;
2367 p2p
.get_noa
= wpas_get_noa
;
2369 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2370 os_memcpy(p2p
.dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2371 p2p
.dev_name
= wpa_s
->conf
->device_name
;
2373 if (wpa_s
->conf
->p2p_listen_reg_class
&&
2374 wpa_s
->conf
->p2p_listen_channel
) {
2375 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
2376 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
2380 * Pick one of the social channels randomly as the listen
2383 os_get_random((u8
*) &r
, sizeof(r
));
2384 p2p
.channel
= 1 + (r
% 3) * 5;
2386 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d", p2p
.channel
);
2388 if (wpa_s
->conf
->p2p_oper_reg_class
&&
2389 wpa_s
->conf
->p2p_oper_channel
) {
2390 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
2391 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
2392 p2p
.cfg_op_channel
= 1;
2393 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
2394 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2397 p2p
.op_reg_class
= 81;
2399 * Use random operation channel from (1, 6, 11) if no other
2400 * preference is indicated.
2402 os_get_random((u8
*) &r
, sizeof(r
));
2403 p2p
.op_channel
= 1 + (r
% 3) * 5;
2404 p2p
.cfg_op_channel
= 0;
2405 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
2406 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2408 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
2409 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
2410 p2p
.country
[2] = 0x04;
2412 os_memcpy(p2p
.country
, "XX\x04", 3);
2414 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
)) {
2415 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
2420 if (wpa_s
->conf
->device_type
&&
2421 wps_dev_type_str2bin(wpa_s
->conf
->device_type
, p2p
.pri_dev_type
) <
2423 wpa_printf(MSG_ERROR
, "P2P: Invalid device_type");
2427 for (i
= 0; i
< MAX_SEC_DEVICE_TYPES
; i
++) {
2428 if (wpa_s
->conf
->sec_device_type
[i
] == NULL
)
2430 if (wps_dev_type_str2bin(
2431 wpa_s
->conf
->sec_device_type
[i
],
2432 p2p
.sec_dev_type
[p2p
.num_sec_dev_types
]) < 0) {
2433 wpa_printf(MSG_ERROR
, "P2P: Invalid sec_device_type");
2436 p2p
.num_sec_dev_types
++;
2437 if (p2p
.num_sec_dev_types
== P2P_SEC_DEVICE_TYPES
)
2441 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
2442 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
2444 p2p
.max_peers
= 100;
2446 if (wpa_s
->conf
->p2p_ssid_postfix
) {
2447 p2p
.ssid_postfix_len
=
2448 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
2449 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
2450 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
2451 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
2452 p2p
.ssid_postfix_len
);
2455 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
2457 global
->p2p
= p2p_init(&p2p
);
2458 if (global
->p2p
== NULL
)
2466 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2467 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2469 * This function deinitialize per-interface P2P data.
2471 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
2473 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
2474 wpa_drv_probe_req_report(wpa_s
, 0);
2475 os_free(wpa_s
->go_params
);
2476 wpa_s
->go_params
= NULL
;
2477 wpabuf_free(wpa_s
->pending_action_tx
);
2478 wpa_s
->pending_action_tx
= NULL
;
2479 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
2480 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2481 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2482 wpa_s
->p2p_long_listen
= 0;
2483 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
2484 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
2485 wpas_p2p_remove_pending_group_interface(wpa_s
);
2487 /* TODO: remove group interface from the driver if this wpa_s instance
2488 * is on top of a P2P group interface */
2493 * wpas_p2p_deinit_global - Deinitialize global P2P module
2494 * @global: Pointer to global data from wpa_supplicant_init()
2496 * This function deinitializes the global (per device) P2P module.
2498 void wpas_p2p_deinit_global(struct wpa_global
*global
)
2500 struct wpa_supplicant
*wpa_s
, *tmp
;
2503 if (global
->p2p
== NULL
)
2506 /* Remove remaining P2P group interfaces */
2507 wpa_s
= global
->ifaces
;
2509 wpas_p2p_service_flush(wpa_s
);
2510 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
2511 wpa_s
= wpa_s
->next
;
2513 enum wpa_driver_if_type type
;
2514 tmp
= global
->ifaces
;
2517 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
2522 ifname
= os_strdup(tmp
->ifname
);
2523 type
= wpas_p2p_if_type(tmp
->p2p_group_interface
);
2524 wpa_supplicant_remove_iface(global
, tmp
);
2526 wpa_drv_if_remove(wpa_s
, type
, ifname
);
2531 * Deinit GO data on any possibly remaining interface (if main
2532 * interface is used as GO).
2534 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2535 if (wpa_s
->ap_iface
)
2536 wpas_p2p_group_deinit(wpa_s
);
2539 p2p_deinit(global
->p2p
);
2544 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
2546 if (wpa_s
->drv_flags
&
2547 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
2548 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
2549 return 1; /* P2P group requires a new interface in every case
2551 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
2552 return 0; /* driver does not support concurrent operations */
2553 if (wpa_s
->global
->ifaces
->next
)
2554 return 1; /* more that one interface already in use */
2555 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
2556 return 1; /* this interface is already in use */
2561 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
2562 const u8
*peer_addr
,
2563 enum p2p_wps_method wps_method
,
2564 int go_intent
, const u8
*own_interface_addr
,
2565 unsigned int force_freq
, int persistent_group
)
2567 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2568 return wpa_drv_p2p_connect(wpa_s
, peer_addr
, wps_method
,
2569 go_intent
, own_interface_addr
,
2570 force_freq
, persistent_group
);
2573 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2574 go_intent
, own_interface_addr
, force_freq
,
2579 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
2580 const u8
*peer_addr
,
2581 enum p2p_wps_method wps_method
,
2582 int go_intent
, const u8
*own_interface_addr
,
2583 unsigned int force_freq
, int persistent_group
)
2585 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2588 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2589 go_intent
, own_interface_addr
, force_freq
,
2594 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
2596 wpa_s
->p2p_join_scan_count
++;
2597 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
2598 wpa_s
->p2p_join_scan_count
);
2599 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
2600 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
2601 " for join operationg - stop join attempt",
2602 MAC2STR(wpa_s
->pending_join_iface_addr
));
2603 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2604 wpa_msg(wpa_s
->parent
, MSG_INFO
,
2605 P2P_EVENT_GROUP_FORMATION_FAILURE
);
2610 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
2611 struct wpa_scan_results
*scan_res
)
2613 struct wpa_bss
*bss
;
2615 u8 iface_addr
[ETH_ALEN
];
2617 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2619 if (wpa_s
->global
->p2p_disabled
)
2622 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for join",
2623 scan_res
? (int) scan_res
->num
: -1);
2626 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
2628 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2629 wpa_s
->pending_join_iface_addr
);
2631 p2p_get_interface_addr(wpa_s
->global
->p2p
,
2632 wpa_s
->pending_join_dev_addr
,
2634 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0)
2636 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
2637 "address for join from " MACSTR
" to " MACSTR
2638 " based on newly discovered P2P peer entry",
2639 MAC2STR(wpa_s
->pending_join_iface_addr
),
2640 MAC2STR(iface_addr
));
2641 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
2644 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2645 wpa_s
->pending_join_iface_addr
);
2648 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2649 "from P2P peer table: %d MHz", freq
);
2651 bss
= wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
);
2654 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2655 "from BSS table: %d MHz", freq
);
2660 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
2661 "prior to joining an existing group (GO " MACSTR
2663 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
2664 wpa_s
->pending_pd_before_join
= 1;
2666 switch (wpa_s
->pending_join_wps_method
) {
2668 case WPS_PIN_DISPLAY
:
2669 method
= WPS_CONFIG_KEYPAD
;
2671 case WPS_PIN_KEYPAD
:
2672 method
= WPS_CONFIG_DISPLAY
;
2675 method
= WPS_CONFIG_PUSHBUTTON
;
2682 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
2683 wpa_s
->pending_join_dev_addr
, method
, 1)
2685 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
2686 "Discovery Request before joining an "
2688 wpa_s
->pending_pd_before_join
= 0;
2693 * Actual join operation will be started from the Action frame
2694 * TX status callback.
2699 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
2700 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2701 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2702 wpas_p2p_check_join_scan_limit(wpa_s
);
2706 /* Start join operation immediately */
2707 wpas_p2p_join_start(wpa_s
);
2711 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
2713 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
2715 struct wpa_driver_scan_params params
;
2716 struct wpabuf
*wps_ie
, *ies
;
2718 os_memset(¶ms
, 0, sizeof(params
));
2720 /* P2P Wildcard SSID */
2721 params
.num_ssids
= 1;
2722 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
2723 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
2725 wpa_s
->wps
->dev
.p2p
= 1;
2726 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
2728 if (wps_ie
== NULL
) {
2729 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2733 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + 100);
2735 wpabuf_free(wps_ie
);
2736 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2739 wpabuf_put_buf(ies
, wps_ie
);
2740 wpabuf_free(wps_ie
);
2742 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
2744 params
.extra_ies
= wpabuf_head(ies
);
2745 params
.extra_ies_len
= wpabuf_len(ies
);
2748 * Run a scan to update BSS table and start Provision Discovery once
2749 * the new scan results become available.
2751 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
2752 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
2753 ret
= ieee80211_sta_req_scan(wpa_s
, ¶ms
);
2755 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
2760 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
2762 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2763 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2764 wpas_p2p_check_join_scan_limit(wpa_s
);
2769 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
2770 const u8
*dev_addr
, enum p2p_wps_method wps_method
)
2772 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
2773 MACSTR
" dev " MACSTR
")",
2774 MAC2STR(iface_addr
), MAC2STR(dev_addr
));
2776 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
2777 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
2778 wpa_s
->pending_join_wps_method
= wps_method
;
2780 /* Make sure we are not running find during connection establishment */
2781 wpas_p2p_stop_find(wpa_s
);
2783 wpa_s
->p2p_join_scan_count
= 0;
2784 wpas_p2p_join_scan(wpa_s
, NULL
);
2789 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
2791 struct wpa_supplicant
*group
;
2792 struct p2p_go_neg_results res
;
2794 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
2797 if (group
!= wpa_s
) {
2798 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
2799 sizeof(group
->p2p_pin
));
2800 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
2803 group
->p2p_in_provisioning
= 1;
2805 os_memset(&res
, 0, sizeof(res
));
2806 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
2808 res
.wps_method
= wpa_s
->pending_join_wps_method
;
2809 wpas_start_wps_enrollee(group
, &res
);
2812 * Allow a longer timeout for join-a-running-group than normal 15
2813 * second group formation timeout since the GO may not have authorized
2814 * our connection yet.
2816 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2817 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
2825 * wpas_p2p_connect - Request P2P Group Formation to be started
2826 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2827 * @peer_addr: Address of the peer P2P Device
2828 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2829 * @persistent_group: Whether to create a persistent group
2830 * @join: Whether to join an existing group (as a client) instead of starting
2831 * Group Owner negotiation; @peer_addr is BSSID in that case
2832 * @auth: Whether to only authorize the connection instead of doing that and
2833 * initiating Group Owner negotiation
2834 * @go_intent: GO Intent or -1 to use default
2835 * @freq: Frequency for the group or 0 for auto-selection
2836 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2837 * failure, -2 on failure due to channel not currently available,
2838 * -3 if forced channel is not supported
2840 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
2841 const char *pin
, enum p2p_wps_method wps_method
,
2842 int persistent_group
, int join
, int auth
, int go_intent
,
2845 int force_freq
= 0, oper_freq
= 0;
2848 enum wpa_driver_if_type iftype
;
2850 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2854 go_intent
= wpa_s
->conf
->p2p_go_intent
;
2857 wpa_s
->p2p_long_listen
= 0;
2859 wpa_s
->p2p_wps_method
= wps_method
;
2862 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
2863 else if (wps_method
== WPS_PIN_DISPLAY
) {
2864 ret
= wps_generate_pin();
2865 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
2867 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
2870 wpa_s
->p2p_pin
[0] = '\0';
2873 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
2875 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
2876 "connect a running group from " MACSTR
,
2877 MAC2STR(peer_addr
));
2878 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
2881 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
2882 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
2884 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
2885 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
2888 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
) <
2894 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
2896 oper_freq
= wpa_s
->assoc_freq
;
2898 oper_freq
= wpa_drv_shared_freq(wpa_s
);
2904 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
2905 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
2906 "(%u MHz) is not supported for P2P uses",
2911 if (oper_freq
> 0 && freq
!= oper_freq
&&
2912 !(wpa_s
->drv_flags
&
2913 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2914 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2915 "on %u MHz while connected on another "
2916 "channel (%u MHz)", freq
, oper_freq
);
2919 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2920 "requested channel (%u MHz)", freq
);
2922 } else if (oper_freq
> 0 &&
2923 !p2p_supported_freq(wpa_s
->global
->p2p
, oper_freq
)) {
2924 if (!(wpa_s
->drv_flags
&
2925 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2926 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2927 "while connected on non-P2P supported "
2928 "channel (%u MHz)", oper_freq
);
2931 wpa_printf(MSG_DEBUG
, "P2P: Current operating channel "
2932 "(%u MHz) not available for P2P - try to use "
2933 "another channel", oper_freq
);
2935 } else if (oper_freq
> 0) {
2936 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2937 "channel we are already using (%u MHz) on another "
2938 "interface", oper_freq
);
2939 force_freq
= oper_freq
;
2942 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
2944 if (!wpa_s
->create_p2p_iface
) {
2946 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2947 go_intent
, wpa_s
->own_addr
,
2948 force_freq
, persistent_group
)
2953 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
2954 go_intent
, wpa_s
->own_addr
,
2955 force_freq
, persistent_group
) < 0)
2960 /* Prepare to add a new interface for the group */
2961 iftype
= WPA_IF_P2P_GROUP
;
2963 iftype
= WPA_IF_P2P_CLIENT
;
2964 else if (go_intent
== 15)
2965 iftype
= WPA_IF_P2P_GO
;
2966 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
2967 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2968 "interface for the group");
2973 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2975 wpa_s
->pending_interface_addr
,
2976 force_freq
, persistent_group
) < 0)
2980 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
, go_intent
,
2981 wpa_s
->pending_interface_addr
,
2982 force_freq
, persistent_group
) < 0) {
2983 wpas_p2p_remove_pending_group_interface(wpa_s
);
2991 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2992 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2993 * @freq: Frequency of the channel in MHz
2994 * @duration: Duration of the stay on the channel in milliseconds
2996 * This callback is called when the driver indicates that it has started the
2997 * requested remain-on-channel duration.
2999 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
3000 unsigned int freq
, unsigned int duration
)
3002 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3004 wpa_s
->roc_waiting_drv_freq
= 0;
3005 wpa_s
->off_channel_freq
= freq
;
3006 wpas_send_action_cb(wpa_s
, NULL
);
3007 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
3008 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
3009 wpa_s
->pending_listen_duration
);
3010 wpa_s
->pending_listen_freq
= 0;
3015 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
3016 unsigned int timeout
)
3018 /* Limit maximum Listen state time based on driver limitation. */
3019 if (timeout
> wpa_s
->max_remain_on_chan
)
3020 timeout
= wpa_s
->max_remain_on_chan
;
3022 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3023 return wpa_drv_p2p_listen(wpa_s
, timeout
);
3025 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
3030 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3031 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3032 * @freq: Frequency of the channel in MHz
3034 * This callback is called when the driver indicates that a remain-on-channel
3035 * operation has been completed, i.e., the duration on the requested channel
3038 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
3041 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
3042 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3043 wpa_s
->p2p_long_listen
, wpa_s
->pending_action_tx
);
3044 wpa_s
->off_channel_freq
= 0;
3045 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3047 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
3048 return; /* P2P module started a new operation */
3049 if (wpa_s
->pending_action_tx
)
3051 if (wpa_s
->p2p_long_listen
> 0)
3052 wpa_s
->p2p_long_listen
-= wpa_s
->max_remain_on_chan
;
3053 if (wpa_s
->p2p_long_listen
> 0) {
3054 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
3055 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
);
3061 * wpas_p2p_group_remove - Remove a P2P group
3062 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3063 * @ifname: Network interface name of the group interface or "*" to remove all
3065 * Returns: 0 on success, -1 on failure
3067 * This function is used to remove a P2P group. This can be used to disconnect
3068 * from a group in which the local end is a P2P Client or to end a P2P Group in
3069 * case the local end is the Group Owner. If a virtual network interface was
3070 * created for this group, that interface will be removed. Otherwise, only the
3071 * configured P2P group network will be removed from the interface.
3073 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
3075 struct wpa_global
*global
= wpa_s
->global
;
3077 if (os_strcmp(ifname
, "*") == 0) {
3078 struct wpa_supplicant
*prev
;
3079 wpa_s
= global
->ifaces
;
3082 wpa_s
= wpa_s
->next
;
3083 wpas_p2p_disconnect(prev
);
3088 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3089 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3093 return wpas_p2p_disconnect(wpa_s
);
3097 static void wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
3098 struct p2p_go_neg_results
*params
,
3104 os_memset(params
, 0, sizeof(*params
));
3105 params
->role_go
= 1;
3107 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
3108 "frequency %d MHz", freq
);
3109 params
->freq
= freq
;
3110 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
3111 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
3112 wpa_s
->conf
->p2p_oper_channel
<= 11) {
3113 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3114 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3115 "frequency %d MHz", params
->freq
);
3116 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
3117 wpa_s
->conf
->p2p_oper_reg_class
== 118) {
3118 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3119 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3120 "frequency %d MHz", params
->freq
);
3121 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3122 wpa_s
->best_overall_freq
> 0 &&
3123 p2p_supported_freq(wpa_s
->global
->p2p
,
3124 wpa_s
->best_overall_freq
)) {
3125 params
->freq
= wpa_s
->best_overall_freq
;
3126 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
3127 "channel %d MHz", params
->freq
);
3128 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3129 wpa_s
->best_24_freq
> 0 &&
3130 p2p_supported_freq(wpa_s
->global
->p2p
,
3131 wpa_s
->best_24_freq
)) {
3132 params
->freq
= wpa_s
->best_24_freq
;
3133 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
3134 "channel %d MHz", params
->freq
);
3135 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3136 wpa_s
->best_5_freq
> 0 &&
3137 p2p_supported_freq(wpa_s
->global
->p2p
,
3138 wpa_s
->best_5_freq
)) {
3139 params
->freq
= wpa_s
->best_5_freq
;
3140 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
3141 "channel %d MHz", params
->freq
);
3143 params
->freq
= 2412;
3144 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference "
3145 "known)", params
->freq
);
3148 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
3149 wpa_s
->assoc_freq
&& !freq
) {
3150 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3152 params
->freq
= wpa_s
->assoc_freq
;
3155 res
= wpa_drv_shared_freq(wpa_s
);
3156 if (res
> 0 && !freq
) {
3157 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3158 "already using on a shared interface");
3164 static struct wpa_supplicant
*
3165 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
3168 struct wpa_supplicant
*group_wpa_s
;
3170 if (!wpas_p2p_create_iface(wpa_s
))
3173 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
3174 WPA_IF_P2P_CLIENT
) < 0)
3176 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
3177 if (group_wpa_s
== NULL
) {
3178 wpas_p2p_remove_pending_group_interface(wpa_s
);
3187 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3188 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3189 * @persistent_group: Whether to create a persistent group
3190 * @freq: Frequency for the group or 0 to indicate no hardcoding
3191 * Returns: 0 on success, -1 on failure
3193 * This function creates a new P2P group with the local end as the Group Owner,
3194 * i.e., without using Group Owner Negotiation.
3196 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
3199 struct p2p_go_neg_results params
;
3202 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3206 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
3208 if (wpa_s
->best_24_freq
> 0 &&
3209 p2p_supported_freq(wpa_s
->global
->p2p
,
3210 wpa_s
->best_24_freq
)) {
3211 freq
= wpa_s
->best_24_freq
;
3212 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
3213 "channel: %d MHz", freq
);
3215 os_get_random((u8
*) &r
, sizeof(r
));
3216 freq
= 2412 + (r
% 3) * 25;
3217 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
3218 "channel: %d MHz", freq
);
3223 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
3225 if (wpa_s
->best_5_freq
> 0 &&
3226 p2p_supported_freq(wpa_s
->global
->p2p
,
3227 wpa_s
->best_5_freq
)) {
3228 freq
= wpa_s
->best_5_freq
;
3229 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
3230 "channel: %d MHz", freq
);
3232 os_get_random((u8
*) &r
, sizeof(r
));
3233 freq
= 5180 + (r
% 4) * 20;
3234 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3235 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
3236 "5 GHz channel for P2P group");
3239 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
3240 "channel: %d MHz", freq
);
3244 if (freq
> 0 && !p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3245 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
3246 "(%u MHz) is not supported for P2P uses",
3251 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
3252 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
3253 params
.persistent_group
= persistent_group
;
3255 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
3258 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3264 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
3265 struct wpa_ssid
*params
, int addr_allocated
)
3267 struct wpa_ssid
*ssid
;
3269 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
3273 wpa_supplicant_ap_deinit(wpa_s
);
3275 ssid
= wpa_config_add_network(wpa_s
->conf
);
3278 wpas_notify_network_added(wpa_s
, ssid
);
3279 wpa_config_set_network_defaults(ssid
);
3280 ssid
->temporary
= 1;
3281 ssid
->proto
= WPA_PROTO_RSN
;
3282 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
3283 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
3284 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
3285 ssid
->ssid
= os_malloc(params
->ssid_len
);
3286 if (ssid
->ssid
== NULL
) {
3287 wpas_notify_network_removed(wpa_s
, ssid
);
3288 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
3291 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
3292 ssid
->ssid_len
= params
->ssid_len
;
3293 ssid
->p2p_group
= 1;
3294 ssid
->export_keys
= 1;
3295 if (params
->psk_set
) {
3296 os_memcpy(ssid
->psk
, params
->psk
, 32);
3299 if (params
->passphrase
)
3300 ssid
->passphrase
= os_strdup(params
->passphrase
);
3302 wpa_supplicant_select_network(wpa_s
, ssid
);
3304 wpa_s
->show_group_started
= 1;
3310 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
3311 struct wpa_ssid
*ssid
, int addr_allocated
,
3314 struct p2p_go_neg_results params
;
3317 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
3320 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
3321 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
3322 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
3327 /* Make sure we are not running find during connection establishment */
3328 wpas_p2p_stop_find(wpa_s
);
3330 if (ssid
->mode
== WPAS_MODE_INFRA
)
3331 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
3333 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
3336 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
3339 if (ssid
->passphrase
== NULL
||
3340 os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
3341 wpa_printf(MSG_DEBUG
, "P2P: Invalid passphrase in persistent "
3345 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
3346 sizeof(params
.passphrase
));
3347 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
3348 params
.ssid_len
= ssid
->ssid_len
;
3349 params
.persistent_group
= 1;
3351 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
3355 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3361 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
3362 struct wpabuf
*proberesp_ies
)
3364 struct wpa_supplicant
*wpa_s
= ctx
;
3365 if (wpa_s
->ap_iface
) {
3366 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
3368 wpabuf_free(hapd
->p2p_beacon_ie
);
3369 hapd
->p2p_beacon_ie
= beacon_ies
;
3371 wpabuf_free(hapd
->p2p_probe_resp_ie
);
3372 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
3374 wpabuf_free(beacon_ies
);
3375 wpabuf_free(proberesp_ies
);
3377 wpa_supplicant_ap_update_beacon(wpa_s
);
3381 static void wpas_p2p_idle_update(void *ctx
, int idle
)
3383 struct wpa_supplicant
*wpa_s
= ctx
;
3384 if (!wpa_s
->ap_iface
)
3386 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
3388 wpas_p2p_set_group_idle_timeout(wpa_s
);
3390 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3394 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
3395 int persistent_group
,
3396 int group_formation
)
3398 struct p2p_group
*group
;
3399 struct p2p_group_config
*cfg
;
3401 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3403 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3406 cfg
= os_zalloc(sizeof(*cfg
));
3410 cfg
->persistent_group
= persistent_group
;
3411 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
3412 if (wpa_s
->max_stations
&&
3413 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
3414 cfg
->max_clients
= wpa_s
->max_stations
;
3416 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
3417 cfg
->cb_ctx
= wpa_s
;
3418 cfg
->ie_update
= wpas_p2p_ie_update
;
3419 cfg
->idle_update
= wpas_p2p_idle_update
;
3421 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
3424 if (!group_formation
)
3425 p2p_group_notif_formation_done(group
);
3426 wpa_s
->p2p_group
= group
;
3431 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3434 if (!wpa_s
->p2p_in_provisioning
) {
3435 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
3436 "provisioning not in progress");
3440 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
3442 if (wpa_s
->global
->p2p
)
3443 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
3444 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3445 wpa_drv_wps_success_cb(wpa_s
, peer_addr
);
3446 wpas_group_formation_completed(wpa_s
, 1);
3450 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3451 const char *config_method
)
3455 if (os_strcmp(config_method
, "display") == 0)
3456 config_methods
= WPS_CONFIG_DISPLAY
;
3457 else if (os_strcmp(config_method
, "keypad") == 0)
3458 config_methods
= WPS_CONFIG_KEYPAD
;
3459 else if (os_strcmp(config_method
, "pbc") == 0 ||
3460 os_strcmp(config_method
, "pushbutton") == 0)
3461 config_methods
= WPS_CONFIG_PUSHBUTTON
;
3465 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3466 return wpa_drv_p2p_prov_disc_req(wpa_s
, peer_addr
,
3470 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
3473 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
3478 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
3481 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
3485 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
3487 if (!wpa_s
->pending_action_tx
)
3490 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
3491 "operation request");
3492 wpabuf_free(wpa_s
->pending_action_tx
);
3493 wpa_s
->pending_action_tx
= NULL
;
3497 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
3498 enum p2p_discovery_type type
)
3500 wpas_p2p_clear_pending_action_tx(wpa_s
);
3501 wpa_s
->p2p_long_listen
= 0;
3503 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3504 return wpa_drv_p2p_find(wpa_s
, timeout
, type
);
3506 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3509 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
);
3513 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
3515 wpas_p2p_clear_pending_action_tx(wpa_s
);
3516 wpa_s
->p2p_long_listen
= 0;
3517 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3518 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3520 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3521 wpa_drv_p2p_stop_find(wpa_s
);
3525 if (wpa_s
->global
->p2p
)
3526 p2p_stop_find(wpa_s
->global
->p2p
);
3528 wpas_p2p_remove_pending_group_interface(wpa_s
);
3532 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
3534 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3535 wpa_s
->p2p_long_listen
= 0;
3539 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
3543 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3546 wpas_p2p_clear_pending_action_tx(wpa_s
);
3550 * This is a request for unlimited Listen state. However, at
3551 * least for now, this is mapped to a Listen state for one
3556 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3557 wpa_s
->p2p_long_listen
= 0;
3559 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
3560 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
3561 wpa_s
->p2p_long_listen
= timeout
* 1000;
3562 eloop_register_timeout(timeout
, 0,
3563 wpas_p2p_long_listen_timeout
,
3571 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
3572 u8
*buf
, size_t len
, int p2p_group
)
3574 struct wpabuf
*p2p_ie
;
3577 if (wpa_s
->global
->p2p_disabled
)
3579 if (wpa_s
->global
->p2p
== NULL
)
3584 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
3585 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
3587 wpabuf_free(p2p_ie
);
3593 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
3594 const u8
*ie
, size_t ie_len
)
3596 if (wpa_s
->global
->p2p_disabled
)
3598 if (wpa_s
->global
->p2p
== NULL
)
3601 return p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, ie
, ie_len
);
3605 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
3606 const u8
*sa
, const u8
*bssid
,
3607 u8 category
, const u8
*data
, size_t len
, int freq
)
3609 if (wpa_s
->global
->p2p_disabled
)
3611 if (wpa_s
->global
->p2p
== NULL
)
3614 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
3619 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
3621 if (wpa_s
->global
->p2p_disabled
)
3623 if (wpa_s
->global
->p2p
== NULL
)
3626 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
3630 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
3632 p2p_group_deinit(wpa_s
->p2p_group
);
3633 wpa_s
->p2p_group
= NULL
;
3637 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
3639 wpa_s
->p2p_long_listen
= 0;
3641 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3642 return wpa_drv_p2p_reject(wpa_s
, addr
);
3644 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3647 return p2p_reject(wpa_s
->global
->p2p
, addr
);
3651 /* Invite to reinvoke a persistent group */
3652 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3653 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
)
3655 enum p2p_invite_role role
;
3658 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3659 role
= P2P_INVITE_ROLE_GO
;
3660 if (peer_addr
== NULL
) {
3661 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
3662 "address in invitation command");
3665 if (wpas_p2p_create_iface(wpa_s
)) {
3666 if (wpas_p2p_add_group_interface(wpa_s
,
3667 WPA_IF_P2P_GO
) < 0) {
3668 wpa_printf(MSG_ERROR
, "P2P: Failed to "
3669 "allocate a new interface for the "
3673 bssid
= wpa_s
->pending_interface_addr
;
3675 bssid
= wpa_s
->own_addr
;
3677 role
= P2P_INVITE_ROLE_CLIENT
;
3678 peer_addr
= ssid
->bssid
;
3680 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
3682 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3683 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3684 ssid
->ssid
, ssid
->ssid_len
,
3687 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3690 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3691 ssid
->ssid
, ssid
->ssid_len
, 0, go_dev_addr
, 1);
3695 /* Invite to join an active group */
3696 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
3697 const u8
*peer_addr
, const u8
*go_dev_addr
)
3699 struct wpa_global
*global
= wpa_s
->global
;
3700 enum p2p_invite_role role
;
3702 struct wpa_ssid
*ssid
;
3704 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3705 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3708 if (wpa_s
== NULL
) {
3709 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
3713 ssid
= wpa_s
->current_ssid
;
3715 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
3720 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3721 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
3722 bssid
= wpa_s
->own_addr
;
3723 if (go_dev_addr
== NULL
)
3724 go_dev_addr
= wpa_s
->parent
->own_addr
;
3726 role
= P2P_INVITE_ROLE_CLIENT
;
3727 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
3728 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
3729 "invite to current group");
3732 bssid
= wpa_s
->bssid
;
3733 if (go_dev_addr
== NULL
&&
3734 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
3735 go_dev_addr
= wpa_s
->go_dev_addr
;
3737 wpa_s
->parent
->pending_invite_ssid_id
= -1;
3739 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3740 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3741 ssid
->ssid
, ssid
->ssid_len
,
3744 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3747 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3748 ssid
->ssid
, ssid
->ssid_len
, wpa_s
->assoc_freq
,
3753 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
3755 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
3756 const char *ssid_txt
;
3757 u8 go_dev_addr
[ETH_ALEN
];
3760 if (!wpa_s
->show_group_started
|| !ssid
)
3763 wpa_s
->show_group_started
= 0;
3765 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
3766 os_memset(go_dev_addr
, 0, ETH_ALEN
);
3767 if (ssid
->bssid_set
)
3768 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
3769 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
3771 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
3773 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
3774 wpa_s
->global
->p2p_group_formation
= NULL
;
3776 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
3778 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
3779 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3780 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3782 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
3783 MAC2STR(go_dev_addr
),
3784 persistent
? " [PERSISTENT]" : "");
3786 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3787 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3788 "go_dev_addr=" MACSTR
"%s",
3789 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
,
3790 ssid
->passphrase
? ssid
->passphrase
: "",
3791 MAC2STR(go_dev_addr
),
3792 persistent
? " [PERSISTENT]" : "");
3796 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
3801 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
3802 u32 interval1
, u32 duration2
, u32 interval2
)
3804 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3806 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3809 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
3810 wpa_s
->current_ssid
== NULL
||
3811 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
3814 return p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
3815 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
3816 duration1
, interval1
, duration2
, interval2
);
3820 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
3821 unsigned int interval
)
3823 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3826 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3829 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
3833 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
3835 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3837 if (wpa_s
->conf
->p2p_group_idle
== 0) {
3838 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
3843 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
3845 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_IDLE_TIMEOUT
;
3846 wpas_p2p_group_delete(wpa_s
);
3850 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
3852 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3853 if (wpa_s
->conf
->p2p_group_idle
== 0)
3856 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
3859 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
3860 wpa_s
->conf
->p2p_group_idle
);
3861 eloop_register_timeout(wpa_s
->conf
->p2p_group_idle
, 0,
3862 wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3866 void wpas_p2p_deauth_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_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3878 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3879 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3881 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3883 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3886 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3890 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
3892 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
3897 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
3900 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
3901 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
3903 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
) {
3905 if (wpa_s
->conf
->device_type
) {
3906 if (wps_dev_type_str2bin(wpa_s
->conf
->device_type
,
3907 pri_dev_type
) < 0) {
3908 wpa_printf(MSG_ERROR
, "P2P: Invalid "
3911 p2p_set_pri_dev_type(p2p
, pri_dev_type
);
3915 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
) {
3916 u8 sec_dev_type
[P2P_SEC_DEVICE_TYPES
][8];
3919 for (i
= 0; i
< MAX_SEC_DEVICE_TYPES
; i
++) {
3920 if (wpa_s
->conf
->sec_device_type
[i
] == NULL
)
3922 if (wps_dev_type_str2bin(
3923 wpa_s
->conf
->sec_device_type
[i
],
3924 sec_dev_type
[num
]) < 0) {
3925 wpa_printf(MSG_ERROR
, "P2P: Invalid "
3930 if (num
== P2P_SEC_DEVICE_TYPES
)
3933 p2p_set_sec_dev_types(p2p
, (void *) sec_dev_type
, num
);
3936 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
3937 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
3939 country
[0] = wpa_s
->conf
->country
[0];
3940 country
[1] = wpa_s
->conf
->country
[1];
3942 p2p_set_country(p2p
, country
);
3945 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
3946 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
3947 wpa_s
->conf
->p2p_ssid_postfix
?
3948 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
3952 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
3953 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
3957 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
3960 if (!wpa_s
->ap_iface
)
3962 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
3967 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
3969 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3971 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3974 wpa_s
->global
->cross_connection
= enabled
;
3975 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
3978 struct wpa_supplicant
*iface
;
3980 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
3982 if (iface
->cross_connect_enabled
== 0)
3985 iface
->cross_connect_enabled
= 0;
3986 iface
->cross_connect_in_use
= 0;
3987 wpa_msg(iface
->parent
, MSG_INFO
,
3988 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
3989 iface
->ifname
, iface
->cross_connect_uplink
);
3997 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
3999 struct wpa_supplicant
*iface
;
4001 if (!uplink
->global
->cross_connection
)
4004 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
4005 if (!iface
->cross_connect_enabled
)
4007 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
4010 if (iface
->ap_iface
== NULL
)
4012 if (iface
->cross_connect_in_use
)
4015 iface
->cross_connect_in_use
= 1;
4016 wpa_msg(iface
->parent
, MSG_INFO
,
4017 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
4018 iface
->ifname
, iface
->cross_connect_uplink
);
4023 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
4025 struct wpa_supplicant
*iface
;
4027 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
4028 if (!iface
->cross_connect_enabled
)
4030 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
4033 if (!iface
->cross_connect_in_use
)
4036 wpa_msg(iface
->parent
, MSG_INFO
,
4037 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
4038 iface
->ifname
, iface
->cross_connect_uplink
);
4039 iface
->cross_connect_in_use
= 0;
4044 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
4046 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
4047 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
4048 wpa_s
->cross_connect_disallowed
)
4049 wpas_p2p_disable_cross_connect(wpa_s
);
4051 wpas_p2p_enable_cross_connect(wpa_s
);
4052 if (!wpa_s
->ap_iface
)
4053 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
4057 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
4059 wpas_p2p_disable_cross_connect(wpa_s
);
4060 if (!wpa_s
->ap_iface
)
4061 wpas_p2p_set_group_idle_timeout(wpa_s
);
4065 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
4067 struct wpa_supplicant
*iface
;
4069 if (!wpa_s
->global
->cross_connection
)
4072 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
4075 if (iface
->drv_flags
&
4076 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
4078 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
4081 wpa_s
->cross_connect_enabled
= 1;
4082 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
4083 sizeof(wpa_s
->cross_connect_uplink
));
4084 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
4085 "%s to %s whenever uplink is available",
4086 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4088 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
4089 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
4090 iface
->cross_connect_disallowed
||
4091 iface
->wpa_state
!= WPA_COMPLETED
)
4094 wpa_s
->cross_connect_in_use
= 1;
4095 wpa_msg(wpa_s
->parent
, MSG_INFO
,
4096 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
4097 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4103 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
4105 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
4106 !wpa_s
->p2p_in_provisioning
)
4107 return 0; /* not P2P client operation */
4109 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
4111 if (wpa_s
!= wpa_s
->parent
)
4112 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
4114 if (wpa_s
->global
->p2p
)
4115 p2p_group_formation_failed(wpa_s
->global
->p2p
);
4117 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4118 wpa_s
->parent
, NULL
);
4120 wpas_group_formation_completed(wpa_s
, 0);
4125 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
4127 struct p2p_channels chan
;
4129 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
4132 os_memset(&chan
, 0, sizeof(chan
));
4133 if (wpas_p2p_setup_channels(wpa_s
, &chan
)) {
4134 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
4139 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
);
4143 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
4145 struct wpa_global
*global
= wpa_s
->global
;
4149 if (global
->p2p
== NULL
)
4152 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
4154 if (wpa_s
->pending_interface_name
[0] &&
4155 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
4158 peer
= p2p_get_go_neg_peer(global
->p2p
);
4160 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
4161 MACSTR
, MAC2STR(peer
));
4162 p2p_unauthorize(global
->p2p
, peer
);
4165 wpas_p2p_stop_find(wpa_s
);
4167 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4168 if (wpa_s
== global
->p2p_group_formation
&&
4169 (wpa_s
->p2p_in_provisioning
||
4170 wpa_s
->parent
->pending_interface_type
==
4171 WPA_IF_P2P_CLIENT
)) {
4172 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
4173 "formation found - cancelling",
4176 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4177 wpa_s
->parent
, NULL
);
4178 wpas_p2p_group_delete(wpa_s
);
4184 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
4192 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
4194 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
4197 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
4198 "being available anymore");
4199 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_UNAVAILABLE
;
4200 wpas_p2p_group_delete(wpa_s
);
4204 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
4205 int freq_24
, int freq_5
, int freq_overall
)
4207 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4208 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4210 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
4214 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
4217 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4219 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4222 if (hwaddr_aton(addr
, peer
))
4225 return p2p_unauthorize(p2p
, peer
);
4230 * wpas_p2p_disconnect - Disconnect from a P2P Group
4231 * @wpa_s: Pointer to wpa_supplicant data
4232 * Returns: 0 on success, -1 on failure
4234 * This can be used to disconnect from a group in which the local end is a P2P
4235 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4236 * virtual network interface was created for this group, that interface will be
4237 * removed. Otherwise, only the configured P2P group network will be removed
4238 * from the interface.
4240 int wpas_p2p_disconnect(struct wpa_supplicant
*wpa_s
)
4246 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_REQUESTED
;
4247 wpas_p2p_group_delete(wpa_s
);