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
);
267 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
268 struct wpa_global
*global
;
270 enum wpa_driver_if_type type
;
271 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
273 global
= wpa_s
->global
;
274 ifname
= os_strdup(wpa_s
->ifname
);
275 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
276 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
);
277 wpa_s
= global
->ifaces
;
279 wpa_drv_if_remove(wpa_s
, type
, ifname
);
284 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
285 if (ssid
&& (ssid
->p2p_group
||
286 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
287 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
289 if (ssid
== wpa_s
->current_ssid
)
290 wpa_s
->current_ssid
= NULL
;
291 wpas_notify_network_removed(wpa_s
, ssid
);
292 wpa_config_remove_network(wpa_s
->conf
, id
);
293 wpa_supplicant_clear_status(wpa_s
);
295 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
298 wpa_supplicant_ap_deinit(wpa_s
);
302 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
304 const u8
*ssid
, size_t ssid_len
)
312 if (wpa_s
->go_params
)
313 bssid
= wpa_s
->go_params
->peer_interface_addr
;
315 bssid
= wpa_s
->bssid
;
317 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
319 u8 iface_addr
[ETH_ALEN
];
320 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
322 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
325 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
326 "group is persistent - BSS " MACSTR
" not found",
331 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
333 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
334 "group is persistent - BSS " MACSTR
335 " did not include P2P IE", MAC2STR(bssid
));
336 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
337 (u8
*) (bss
+ 1), bss
->ie_len
);
338 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
339 ((u8
*) bss
+ 1) + bss
->ie_len
,
344 group_capab
= p2p_get_group_capab(p2p
);
345 addr
= p2p_get_go_dev_addr(p2p
);
346 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
347 "group_capab=0x%x", group_capab
);
349 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
350 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
353 os_memset(go_dev_addr
, 0, ETH_ALEN
);
356 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
357 "go_dev_addr=" MACSTR
,
358 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
360 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
364 static void wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
365 struct wpa_ssid
*ssid
,
366 const u8
*go_dev_addr
)
371 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
372 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
373 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
374 if (s
->disabled
== 2 &&
375 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
376 s
->ssid_len
== ssid
->ssid_len
&&
377 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
382 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
384 if (ssid
->passphrase
&& !s
->passphrase
)
386 else if (ssid
->passphrase
&& s
->passphrase
&&
387 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
390 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
393 s
= wpa_config_add_network(wpa_s
->conf
);
396 wpa_config_set_network_defaults(s
);
400 s
->p2p_persistent_group
= 1;
403 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
404 s
->mode
= ssid
->mode
;
405 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
406 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
407 s
->proto
= WPA_PROTO_RSN
;
408 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
410 if (ssid
->passphrase
) {
411 os_free(s
->passphrase
);
412 s
->passphrase
= os_strdup(ssid
->passphrase
);
416 os_memcpy(s
->psk
, ssid
->psk
, 32);
418 if (s
->passphrase
&& !s
->psk_set
)
419 wpa_config_update_psk(s
);
420 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
422 s
->ssid
= os_malloc(ssid
->ssid_len
);
425 s
->ssid_len
= ssid
->ssid_len
;
426 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
429 #ifndef CONFIG_NO_CONFIG_WRITE
430 if (changed
&& wpa_s
->conf
->update_config
&&
431 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
432 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
434 #endif /* CONFIG_NO_CONFIG_WRITE */
438 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
441 struct wpa_ssid
*ssid
;
442 const char *ssid_txt
;
445 u8 go_dev_addr
[ETH_ALEN
];
448 * This callback is likely called for the main interface. Update wpa_s
449 * to use the group interface if a new interface was created for the
452 if (wpa_s
->global
->p2p_group_formation
)
453 wpa_s
= wpa_s
->global
->p2p_group_formation
;
454 wpa_s
->global
->p2p_group_formation
= NULL
;
455 wpa_s
->p2p_in_provisioning
= 0;
458 wpa_msg(wpa_s
->parent
, MSG_INFO
,
459 P2P_EVENT_GROUP_FORMATION_FAILURE
);
460 wpas_p2p_group_delete(wpa_s
);
464 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_FORMATION_SUCCESS
);
466 ssid
= wpa_s
->current_ssid
;
467 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
468 ssid
->mode
= WPAS_MODE_P2P_GO
;
469 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
470 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
475 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
476 client
= ssid
->mode
== WPAS_MODE_INFRA
;
477 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
478 persistent
= ssid
->p2p_persistent_group
;
479 os_memcpy(go_dev_addr
, wpa_s
->parent
->own_addr
,
482 persistent
= wpas_p2p_persistent_group(wpa_s
,
488 client
= wpa_s
->p2p_group_interface
==
489 P2P_GROUP_INTERFACE_CLIENT
;
492 wpa_s
->show_group_started
= 0;
495 * Indicate event only after successfully completed 4-way
496 * handshake, i.e., when the interface is ready for data
499 wpa_s
->show_group_started
= 1;
500 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
502 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
503 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
504 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
506 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
507 MAC2STR(go_dev_addr
),
508 persistent
? " [PERSISTENT]" : "");
509 wpas_p2p_cross_connect_setup(wpa_s
);
510 wpas_p2p_set_group_idle_timeout(wpa_s
);
512 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
513 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
514 "go_dev_addr=" MACSTR
"%s",
515 wpa_s
->ifname
, ssid_txt
, ssid
? ssid
->frequency
: 0,
516 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
517 MAC2STR(go_dev_addr
),
518 persistent
? " [PERSISTENT]" : "");
519 wpas_p2p_cross_connect_setup(wpa_s
);
520 wpas_p2p_set_group_idle_timeout(wpa_s
);
524 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
529 static struct wpa_supplicant
*
530 wpas_get_tx_interface(struct wpa_supplicant
*wpa_s
, const u8
*src
)
532 struct wpa_supplicant
*iface
;
534 if (os_memcmp(src
, wpa_s
->own_addr
, ETH_ALEN
) == 0)
538 * Try to find a group interface that matches with the source address.
540 iface
= wpa_s
->global
->ifaces
;
542 if (os_memcmp(wpa_s
->pending_action_src
,
543 iface
->own_addr
, ETH_ALEN
) == 0)
548 wpa_printf(MSG_DEBUG
, "P2P: Use group interface %s "
549 "instead of interface %s for Action TX",
550 iface
->ifname
, wpa_s
->ifname
);
558 static void wpas_send_action_cb(void *eloop_ctx
, void *timeout_ctx
)
560 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
561 struct wpa_supplicant
*iface
;
565 without_roc
= wpa_s
->pending_action_without_roc
;
566 wpa_s
->pending_action_without_roc
= 0;
567 wpa_printf(MSG_DEBUG
, "P2P: Send Action callback (without_roc=%d "
568 "pending_action_tx=%p)",
569 without_roc
, wpa_s
->pending_action_tx
);
571 if (wpa_s
->pending_action_tx
== NULL
)
575 * This call is likely going to be on the P2P device instance if the
576 * driver uses a separate interface for that purpose. However, some
577 * Action frames are actually sent within a P2P Group and when that is
578 * the case, we need to follow power saving (e.g., GO buffering the
579 * frame for a client in PS mode or a client following the advertised
580 * NoA from its GO). To make that easier for the driver, select the
581 * correct group interface here.
583 iface
= wpas_get_tx_interface(wpa_s
, wpa_s
->pending_action_src
);
585 if (wpa_s
->off_channel_freq
!= wpa_s
->pending_action_freq
&&
586 wpa_s
->pending_action_freq
!= 0 &&
587 wpa_s
->pending_action_freq
!= iface
->assoc_freq
) {
588 wpa_printf(MSG_DEBUG
, "P2P: Pending Action frame TX "
589 "waiting for another freq=%u (off_channel_freq=%u "
591 wpa_s
->pending_action_freq
,
592 wpa_s
->off_channel_freq
,
594 if (without_roc
&& wpa_s
->off_channel_freq
== 0) {
596 * We may get here if wpas_send_action() found us to be
597 * on the correct channel, but remain-on-channel cancel
598 * event was received before getting here.
600 wpa_printf(MSG_DEBUG
, "P2P: Schedule "
601 "remain-on-channel to send Action frame");
602 if (wpa_drv_remain_on_channel(
603 wpa_s
, wpa_s
->pending_action_freq
, 200) <
605 wpa_printf(MSG_DEBUG
, "P2P: Failed to request "
606 "driver to remain on channel (%u "
607 "MHz) for Action Frame TX",
608 wpa_s
->pending_action_freq
);
610 wpa_s
->off_channel_freq
= 0;
611 wpa_s
->roc_waiting_drv_freq
=
612 wpa_s
->pending_action_freq
;
618 wpa_printf(MSG_DEBUG
, "P2P: Sending pending Action frame to "
619 MACSTR
" using interface %s",
620 MAC2STR(wpa_s
->pending_action_dst
), iface
->ifname
);
621 res
= wpa_drv_send_action(iface
, wpa_s
->pending_action_freq
, 0,
622 wpa_s
->pending_action_dst
,
623 wpa_s
->pending_action_src
,
624 wpa_s
->pending_action_bssid
,
625 wpabuf_head(wpa_s
->pending_action_tx
),
626 wpabuf_len(wpa_s
->pending_action_tx
));
628 wpa_printf(MSG_DEBUG
, "P2P: Failed to send the pending "
631 * Use fake TX status event to allow P2P state machine to
634 wpas_send_action_tx_status(
635 wpa_s
, wpa_s
->pending_action_dst
,
636 wpabuf_head(wpa_s
->pending_action_tx
),
637 wpabuf_len(wpa_s
->pending_action_tx
),
638 P2P_SEND_ACTION_FAILED
);
643 void wpas_send_action_tx_status(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
644 const u8
*data
, size_t data_len
,
645 enum p2p_send_action_result result
)
647 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
649 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
652 if (wpa_s
->pending_action_tx
== NULL
) {
653 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - no "
654 "pending operation");
658 if (os_memcmp(dst
, wpa_s
->pending_action_dst
, ETH_ALEN
) != 0) {
659 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - unknown "
660 "destination address");
664 wpabuf_free(wpa_s
->pending_action_tx
);
665 wpa_s
->pending_action_tx
= NULL
;
667 p2p_send_action_cb(wpa_s
->global
->p2p
, wpa_s
->pending_action_freq
,
668 wpa_s
->pending_action_dst
,
669 wpa_s
->pending_action_src
,
670 wpa_s
->pending_action_bssid
,
673 if (wpa_s
->pending_pd_before_join
&&
674 (os_memcmp(wpa_s
->pending_action_dst
, wpa_s
->pending_join_dev_addr
,
676 os_memcmp(wpa_s
->pending_action_dst
,
677 wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
678 wpa_s
->pending_pd_before_join
= 0;
679 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
680 "join-existing-group operation");
681 wpas_p2p_join_start(wpa_s
);
686 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
687 const u8
*src
, const u8
*bssid
, const u8
*buf
,
688 size_t len
, unsigned int wait_time
)
690 struct wpa_supplicant
*wpa_s
= ctx
;
692 wpa_printf(MSG_DEBUG
, "P2P: Send action frame: freq=%d dst=" MACSTR
693 " src=" MACSTR
" bssid=" MACSTR
" len=%d",
694 freq
, MAC2STR(dst
), MAC2STR(src
), MAC2STR(bssid
),
697 if (wpa_s
->pending_action_tx
) {
698 wpa_printf(MSG_DEBUG
, "P2P: Dropped pending Action frame TX "
699 "to " MACSTR
, MAC2STR(wpa_s
->pending_action_dst
));
700 wpabuf_free(wpa_s
->pending_action_tx
);
702 wpa_s
->pending_action_tx
= wpabuf_alloc(len
);
703 if (wpa_s
->pending_action_tx
== NULL
) {
704 wpa_printf(MSG_DEBUG
, "P2P: Failed to allocate Action frame "
705 "TX buffer (len=%llu)", (unsigned long long) len
);
708 wpabuf_put_data(wpa_s
->pending_action_tx
, buf
, len
);
709 os_memcpy(wpa_s
->pending_action_src
, src
, ETH_ALEN
);
710 os_memcpy(wpa_s
->pending_action_dst
, dst
, ETH_ALEN
);
711 os_memcpy(wpa_s
->pending_action_bssid
, bssid
, ETH_ALEN
);
712 wpa_s
->pending_action_freq
= freq
;
714 if (freq
!= 0 && wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_OFFCHANNEL_TX
) {
715 struct wpa_supplicant
*iface
;
717 iface
= wpas_get_tx_interface(wpa_s
, wpa_s
->pending_action_src
);
718 wpa_s
->action_tx_wait_time
= wait_time
;
720 return wpa_drv_send_action(iface
, wpa_s
->pending_action_freq
,
721 wait_time
, wpa_s
->pending_action_dst
,
722 wpa_s
->pending_action_src
,
723 wpa_s
->pending_action_bssid
,
724 wpabuf_head(wpa_s
->pending_action_tx
),
725 wpabuf_len(wpa_s
->pending_action_tx
));
729 struct wpa_supplicant
*tx_iface
;
730 tx_iface
= wpas_get_tx_interface(wpa_s
, src
);
731 if (tx_iface
->assoc_freq
== freq
) {
732 wpa_printf(MSG_DEBUG
, "P2P: Already on requested "
733 "channel (TX interface operating channel)");
738 if (wpa_s
->off_channel_freq
== freq
|| freq
== 0) {
739 wpa_printf(MSG_DEBUG
, "P2P: Already on requested channel; "
740 "send Action frame immediately");
741 /* TODO: Would there ever be need to extend the current
742 * duration on the channel? */
743 wpa_s
->pending_action_without_roc
= 1;
744 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
745 eloop_register_timeout(0, 0, wpas_send_action_cb
, wpa_s
, NULL
);
748 wpa_s
->pending_action_without_roc
= 0;
750 if (wpa_s
->roc_waiting_drv_freq
== freq
) {
751 wpa_printf(MSG_DEBUG
, "P2P: Already waiting for driver to get "
752 "to frequency %u MHz; continue waiting to send the "
753 "Action frame", freq
);
757 wpa_printf(MSG_DEBUG
, "P2P: Schedule Action frame to be transmitted "
758 "once the driver gets to the requested channel");
759 if (wait_time
> wpa_s
->max_remain_on_chan
)
760 wait_time
= wpa_s
->max_remain_on_chan
;
761 if (wpa_drv_remain_on_channel(wpa_s
, freq
, wait_time
) < 0) {
762 wpa_printf(MSG_DEBUG
, "P2P: Failed to request driver "
763 "to remain on channel (%u MHz) for Action "
767 wpa_s
->off_channel_freq
= 0;
768 wpa_s
->roc_waiting_drv_freq
= freq
;
774 static void wpas_send_action_done(void *ctx
)
776 struct wpa_supplicant
*wpa_s
= ctx
;
777 wpa_printf(MSG_DEBUG
, "P2P: Action frame sequence done notification");
778 wpabuf_free(wpa_s
->pending_action_tx
);
779 wpa_s
->pending_action_tx
= NULL
;
780 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_OFFCHANNEL_TX
) {
781 if (wpa_s
->action_tx_wait_time
)
782 wpa_drv_send_action_cancel_wait(wpa_s
);
783 wpa_s
->off_channel_freq
= 0;
784 } else if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
785 wpa_drv_cancel_remain_on_channel(wpa_s
);
786 wpa_s
->off_channel_freq
= 0;
787 wpa_s
->roc_waiting_drv_freq
= 0;
792 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
793 struct p2p_go_neg_results
*params
)
795 if (wpa_s
->go_params
== NULL
) {
796 wpa_s
->go_params
= os_malloc(sizeof(*params
));
797 if (wpa_s
->go_params
== NULL
)
800 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
805 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
806 struct p2p_go_neg_results
*res
)
808 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
809 MAC2STR(res
->peer_interface_addr
));
810 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
811 res
->ssid
, res
->ssid_len
);
812 wpa_supplicant_ap_deinit(wpa_s
);
813 wpas_copy_go_neg_results(wpa_s
, res
);
814 if (res
->wps_method
== WPS_PBC
)
815 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
817 u16 dev_pw_id
= DEV_PW_DEFAULT
;
818 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
819 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
820 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
821 wpa_s
->p2p_pin
, 1, dev_pw_id
);
826 static void p2p_go_configured(void *ctx
, void *data
)
828 struct wpa_supplicant
*wpa_s
= ctx
;
829 struct p2p_go_neg_results
*params
= data
;
830 struct wpa_ssid
*ssid
;
832 ssid
= wpa_s
->current_ssid
;
833 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
834 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
835 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
836 wpa_s
->global
->p2p_group_formation
= NULL
;
837 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
838 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
839 "go_dev_addr=" MACSTR
"%s",
841 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
843 params
->passphrase
? params
->passphrase
: "",
844 MAC2STR(wpa_s
->parent
->own_addr
),
845 params
->persistent_group
? " [PERSISTENT]" : "");
846 if (params
->persistent_group
)
847 wpas_p2p_store_persistent_group(
849 wpa_s
->parent
->own_addr
);
850 wpas_p2p_cross_connect_setup(wpa_s
);
851 wpas_p2p_set_group_idle_timeout(wpa_s
);
855 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
856 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
857 params
->peer_interface_addr
)) {
858 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
862 if (params
->wps_method
== WPS_PBC
)
863 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
,
865 else if (wpa_s
->p2p_pin
[0])
866 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
867 wpa_s
->p2p_pin
, NULL
, 0);
868 os_free(wpa_s
->go_params
);
869 wpa_s
->go_params
= NULL
;
873 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
874 struct p2p_go_neg_results
*params
,
877 struct wpa_ssid
*ssid
;
879 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0)
882 ssid
= wpa_config_add_network(wpa_s
->conf
);
886 wpas_notify_network_added(wpa_s
, ssid
);
887 wpa_config_set_network_defaults(ssid
);
890 ssid
->p2p_persistent_group
= params
->persistent_group
;
891 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
893 ssid
->frequency
= params
->freq
;
894 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
896 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
897 ssid
->ssid_len
= params
->ssid_len
;
899 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
900 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
901 ssid
->proto
= WPA_PROTO_RSN
;
902 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
903 ssid
->passphrase
= os_strdup(params
->passphrase
);
905 wpa_s
->ap_configured_cb
= p2p_go_configured
;
906 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
907 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
908 wpa_s
->connect_without_scan
= 1;
909 wpa_s
->reassociate
= 1;
910 wpa_s
->disconnected
= 0;
911 wpa_supplicant_req_scan(wpa_s
, 0, 0);
915 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
916 const struct wpa_supplicant
*src
)
918 struct wpa_config
*d
;
919 const struct wpa_config
*s
;
924 #define C(n) if (s->n) d->n = os_strdup(s->n)
934 d
->p2p_group_idle
= s
->p2p_group_idle
;
935 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
939 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
940 enum wpa_driver_if_type type
)
942 char ifname
[120], force_ifname
[120];
944 if (wpa_s
->pending_interface_name
[0]) {
945 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
946 "- skip creation of a new one");
947 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
948 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
949 "unknown?! ifname='%s'",
950 wpa_s
->pending_interface_name
);
956 os_snprintf(ifname
, sizeof(ifname
), "p2p-%s-%d", wpa_s
->ifname
,
957 wpa_s
->p2p_group_idx
);
958 if (os_strlen(ifname
) >= IFNAMSIZ
&&
959 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
960 /* Try to avoid going over the IFNAMSIZ length limit */
961 os_snprintf(ifname
, sizeof(ifname
), "p2p-%d",
962 wpa_s
->p2p_group_idx
);
964 force_ifname
[0] = '\0';
966 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
968 wpa_s
->p2p_group_idx
++;
970 wpa_s
->pending_interface_type
= type
;
971 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
972 wpa_s
->pending_interface_addr
) < 0) {
973 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
978 if (force_ifname
[0]) {
979 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
981 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
982 sizeof(wpa_s
->pending_interface_name
));
984 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
985 sizeof(wpa_s
->pending_interface_name
));
986 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
987 MACSTR
, wpa_s
->pending_interface_name
,
988 MAC2STR(wpa_s
->pending_interface_addr
));
994 static void wpas_p2p_remove_pending_group_interface(
995 struct wpa_supplicant
*wpa_s
)
997 if (!wpa_s
->pending_interface_name
[0] ||
998 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
999 return; /* No pending virtual interface */
1001 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
1002 wpa_s
->pending_interface_name
);
1003 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
1004 wpa_s
->pending_interface_name
);
1005 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1006 wpa_s
->pending_interface_name
[0] = '\0';
1010 static struct wpa_supplicant
*
1011 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
1013 struct wpa_interface iface
;
1014 struct wpa_supplicant
*group_wpa_s
;
1016 if (!wpa_s
->pending_interface_name
[0]) {
1017 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
1018 if (!wpas_p2p_create_iface(wpa_s
))
1021 * Something has forced us to remove the pending interface; try
1022 * to create a new one and hope for the best that we will get
1023 * the same local address.
1025 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
1026 WPA_IF_P2P_CLIENT
) < 0)
1030 os_memset(&iface
, 0, sizeof(iface
));
1031 iface
.ifname
= wpa_s
->pending_interface_name
;
1032 iface
.driver
= wpa_s
->driver
->name
;
1033 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
1034 iface
.driver_param
= wpa_s
->conf
->driver_param
;
1035 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
1036 if (group_wpa_s
== NULL
) {
1037 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
1038 "wpa_supplicant interface");
1041 wpa_s
->pending_interface_name
[0] = '\0';
1042 group_wpa_s
->parent
= wpa_s
;
1043 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
1044 P2P_GROUP_INTERFACE_CLIENT
;
1045 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
1047 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
1053 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
1056 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
1057 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
1058 if (wpa_s
->global
->p2p
)
1059 p2p_group_formation_failed(wpa_s
->global
->p2p
);
1060 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1061 wpa_drv_p2p_group_formation_failed(wpa_s
);
1062 wpas_group_formation_completed(wpa_s
, 0);
1066 void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
1068 struct wpa_supplicant
*wpa_s
= ctx
;
1070 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1071 wpa_drv_cancel_remain_on_channel(wpa_s
);
1072 wpa_s
->off_channel_freq
= 0;
1073 wpa_s
->roc_waiting_drv_freq
= 0;
1077 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_FAILURE
"status=%d",
1079 wpas_p2p_remove_pending_group_interface(wpa_s
);
1083 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
);
1085 if (wpa_s
->create_p2p_iface
) {
1086 struct wpa_supplicant
*group_wpa_s
=
1087 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
1088 if (group_wpa_s
== NULL
) {
1089 wpas_p2p_remove_pending_group_interface(wpa_s
);
1092 if (group_wpa_s
!= wpa_s
) {
1093 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
1094 sizeof(group_wpa_s
->p2p_pin
));
1095 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
1097 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1098 wpa_s
->pending_interface_name
[0] = '\0';
1099 group_wpa_s
->p2p_in_provisioning
= 1;
1102 wpas_start_wps_go(group_wpa_s
, res
, 1);
1104 wpas_start_wps_enrollee(group_wpa_s
, res
);
1106 wpa_s
->p2p_in_provisioning
= 1;
1107 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1110 wpas_start_wps_go(wpa_s
, res
, 1);
1112 wpas_start_wps_enrollee(ctx
, res
);
1115 wpa_s
->p2p_long_listen
= 0;
1116 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1118 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1119 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1120 (res
->peer_config_timeout
% 100) * 10000,
1121 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1125 void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1127 struct wpa_supplicant
*wpa_s
= ctx
;
1128 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1129 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1133 void wpas_dev_found(void *ctx
, const u8
*addr
,
1134 const struct p2p_peer_info
*info
)
1136 struct wpa_supplicant
*wpa_s
= ctx
;
1137 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1139 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1140 " p2p_dev_addr=" MACSTR
1141 " pri_dev_type=%s name='%s' config_methods=0x%x "
1142 "dev_capab=0x%x group_capab=0x%x",
1143 MAC2STR(addr
), MAC2STR(info
->p2p_device_addr
),
1144 wps_dev_type_bin2str(info
->pri_dev_type
, devtype
,
1146 info
->device_name
, info
->config_methods
,
1147 info
->dev_capab
, info
->group_capab
);
1151 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1152 unsigned int duration
,
1153 const struct wpabuf
*probe_resp_ie
)
1155 struct wpa_supplicant
*wpa_s
= ctx
;
1157 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, probe_resp_ie
, NULL
);
1159 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1160 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1161 "report received Probe Request frames");
1165 wpa_s
->pending_listen_freq
= freq
;
1166 wpa_s
->pending_listen_duration
= duration
;
1168 if (wpa_drv_remain_on_channel(wpa_s
, freq
, duration
) < 0) {
1169 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1170 "to remain on channel (%u MHz) for Listen "
1172 wpa_s
->pending_listen_freq
= 0;
1175 wpa_s
->off_channel_freq
= 0;
1176 wpa_s
->roc_waiting_drv_freq
= freq
;
1182 static void wpas_stop_listen(void *ctx
)
1184 struct wpa_supplicant
*wpa_s
= ctx
;
1185 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1186 wpa_drv_cancel_remain_on_channel(wpa_s
);
1187 wpa_s
->off_channel_freq
= 0;
1188 wpa_s
->roc_waiting_drv_freq
= 0;
1190 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, NULL
, NULL
);
1191 wpa_drv_probe_req_report(wpa_s
, 0);
1195 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1197 struct wpa_supplicant
*wpa_s
= ctx
;
1198 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
));
1202 static struct p2p_srv_bonjour
*
1203 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1204 const struct wpabuf
*query
)
1206 struct p2p_srv_bonjour
*bsrv
;
1209 len
= wpabuf_len(query
);
1210 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1211 struct p2p_srv_bonjour
, list
) {
1212 if (len
== wpabuf_len(bsrv
->query
) &&
1213 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1221 static struct p2p_srv_upnp
*
1222 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1223 const char *service
)
1225 struct p2p_srv_upnp
*usrv
;
1227 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1228 struct p2p_srv_upnp
, list
) {
1229 if (version
== usrv
->version
&&
1230 os_strcmp(service
, usrv
->service
) == 0)
1237 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1242 if (wpabuf_tailroom(resp
) < 5)
1245 /* Length (to be filled) */
1246 len_pos
= wpabuf_put(resp
, 2);
1247 wpabuf_put_u8(resp
, srv_proto
);
1248 wpabuf_put_u8(resp
, srv_trans_id
);
1250 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1251 /* Response Data: empty */
1252 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1256 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1257 struct wpabuf
*resp
, u8 srv_trans_id
)
1259 struct p2p_srv_bonjour
*bsrv
;
1262 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1264 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1265 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1269 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1270 struct p2p_srv_bonjour
, list
) {
1271 if (wpabuf_tailroom(resp
) <
1272 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1274 /* Length (to be filled) */
1275 len_pos
= wpabuf_put(resp
, 2);
1276 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1277 wpabuf_put_u8(resp
, srv_trans_id
);
1279 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1280 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1281 wpabuf_head(bsrv
->resp
),
1282 wpabuf_len(bsrv
->resp
));
1284 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1285 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1286 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1292 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
1293 struct wpabuf
*resp
, u8 srv_trans_id
,
1294 const u8
*query
, size_t query_len
)
1296 struct p2p_srv_bonjour
*bsrv
;
1300 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
1302 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1303 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1304 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
1309 if (query_len
== 0) {
1310 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1314 if (wpabuf_tailroom(resp
) < 5)
1316 /* Length (to be filled) */
1317 len_pos
= wpabuf_put(resp
, 2);
1318 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1319 wpabuf_put_u8(resp
, srv_trans_id
);
1321 wpabuf_set(&buf
, query
, query_len
);
1322 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, &buf
);
1324 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
1328 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1329 /* Response Data: empty */
1330 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1336 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1337 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1338 wpabuf_head(bsrv
->resp
), wpabuf_len(bsrv
->resp
));
1340 if (wpabuf_tailroom(resp
) >=
1341 wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
)) {
1343 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1344 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1346 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1350 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
1351 struct wpabuf
*resp
, u8 srv_trans_id
)
1353 struct p2p_srv_upnp
*usrv
;
1356 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
1358 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1359 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1363 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1364 struct p2p_srv_upnp
, list
) {
1365 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
1368 /* Length (to be filled) */
1369 len_pos
= wpabuf_put(resp
, 2);
1370 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1371 wpabuf_put_u8(resp
, srv_trans_id
);
1374 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1376 wpabuf_put_u8(resp
, usrv
->version
);
1377 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1379 wpabuf_put_str(resp
, usrv
->service
);
1380 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1386 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
1387 struct wpabuf
*resp
, u8 srv_trans_id
,
1388 const u8
*query
, size_t query_len
)
1390 struct p2p_srv_upnp
*usrv
;
1396 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
1399 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1400 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1401 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
1406 if (query_len
== 0) {
1407 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1411 if (wpabuf_tailroom(resp
) < 5)
1414 /* Length (to be filled) */
1415 len_pos
= wpabuf_put(resp
, 2);
1416 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1417 wpabuf_put_u8(resp
, srv_trans_id
);
1420 str
= os_malloc(query_len
);
1423 os_memcpy(str
, query
+ 1, query_len
- 1);
1424 str
[query_len
- 1] = '\0';
1426 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1427 struct p2p_srv_upnp
, list
) {
1428 if (version
!= usrv
->version
)
1431 if (os_strcmp(str
, "ssdp:all") != 0 &&
1432 os_strstr(usrv
->service
, str
) == NULL
)
1435 if (wpabuf_tailroom(resp
) < 2)
1439 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1441 wpabuf_put_u8(resp
, version
);
1443 wpabuf_put_u8(resp
, ',');
1447 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1449 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
1451 wpabuf_put_str(resp
, usrv
->service
);
1455 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
1458 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1459 /* Response Data: empty */
1462 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1466 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
1467 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
1469 struct wpa_supplicant
*wpa_s
= ctx
;
1470 const u8
*pos
= tlvs
;
1471 const u8
*end
= tlvs
+ tlvs_len
;
1474 struct wpabuf
*resp
;
1475 u8 srv_proto
, srv_trans_id
;
1479 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
1481 buf_len
= 2 * tlvs_len
+ 1;
1482 buf
= os_malloc(buf_len
);
1484 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1485 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
1487 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
1492 if (wpa_s
->p2p_sd_over_ctrl_iface
)
1493 return; /* to be processed by an external program */
1495 resp
= wpabuf_alloc(10000);
1499 while (pos
+ 1 < end
) {
1500 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
1501 slen
= WPA_GET_LE16(pos
);
1503 if (pos
+ slen
> end
|| slen
< 2) {
1504 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
1509 tlv_end
= pos
+ slen
;
1512 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1514 srv_trans_id
= *pos
++;
1515 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1518 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
1519 pos
, tlv_end
- pos
);
1522 if (wpa_s
->force_long_sd
) {
1523 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
1525 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1526 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1530 switch (srv_proto
) {
1531 case P2P_SERV_ALL_SERVICES
:
1532 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
1533 "for all services");
1534 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
1535 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1536 wpa_printf(MSG_DEBUG
, "P2P: No service "
1537 "discovery protocols available");
1538 wpas_sd_add_proto_not_avail(
1539 resp
, P2P_SERV_ALL_SERVICES
,
1543 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1544 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1546 case P2P_SERV_BONJOUR
:
1547 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
1548 pos
, tlv_end
- pos
);
1551 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
1552 pos
, tlv_end
- pos
);
1555 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
1556 "protocol %u", srv_proto
);
1557 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
1566 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
1572 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
1573 const u8
*tlvs
, size_t tlvs_len
)
1575 struct wpa_supplicant
*wpa_s
= ctx
;
1576 const u8
*pos
= tlvs
;
1577 const u8
*end
= tlvs
+ tlvs_len
;
1583 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
1585 if (tlvs_len
> 1500) {
1586 /* TODO: better way for handling this */
1587 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1588 P2P_EVENT_SERV_DISC_RESP MACSTR
1589 " %u <long response: %u bytes>",
1590 MAC2STR(sa
), update_indic
,
1591 (unsigned int) tlvs_len
);
1593 buf_len
= 2 * tlvs_len
+ 1;
1594 buf
= os_malloc(buf_len
);
1596 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1597 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1598 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
1599 MAC2STR(sa
), update_indic
, buf
);
1605 u8 srv_proto
, srv_trans_id
, status
;
1607 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
1608 slen
= WPA_GET_LE16(pos
);
1610 if (pos
+ slen
> end
|| slen
< 3) {
1611 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
1615 tlv_end
= pos
+ slen
;
1618 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1620 srv_trans_id
= *pos
++;
1621 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1624 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
1627 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
1628 pos
, tlv_end
- pos
);
1635 void * wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1636 const struct wpabuf
*tlvs
)
1638 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1639 return (void *) wpa_drv_p2p_sd_request(wpa_s
, dst
, tlvs
);
1640 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1642 return p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
1646 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1647 u8 version
, const char *query
)
1649 struct wpabuf
*tlvs
;
1652 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
1655 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
1656 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
1657 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
1658 wpabuf_put_u8(tlvs
, version
);
1659 wpabuf_put_str(tlvs
, query
);
1660 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
1666 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, void *req
)
1668 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1669 return wpa_drv_p2p_sd_cancel_request(wpa_s
, (u64
) req
);
1670 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1672 return p2p_sd_cancel_request(wpa_s
->global
->p2p
, req
);
1676 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
1677 const u8
*dst
, u8 dialog_token
,
1678 const struct wpabuf
*resp_tlvs
)
1680 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1681 wpa_drv_p2p_sd_response(wpa_s
, freq
, dst
, dialog_token
,
1685 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1687 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
1692 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
1694 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1695 wpa_drv_p2p_service_update(wpa_s
);
1698 if (wpa_s
->global
->p2p
)
1699 p2p_sd_service_update(wpa_s
->global
->p2p
);
1703 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
1705 dl_list_del(&bsrv
->list
);
1706 wpabuf_free(bsrv
->query
);
1707 wpabuf_free(bsrv
->resp
);
1712 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
1714 dl_list_del(&usrv
->list
);
1715 os_free(usrv
->service
);
1720 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
1722 struct p2p_srv_bonjour
*bsrv
, *bn
;
1723 struct p2p_srv_upnp
*usrv
, *un
;
1725 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
1726 struct p2p_srv_bonjour
, list
)
1727 wpas_p2p_srv_bonjour_free(bsrv
);
1729 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
1730 struct p2p_srv_upnp
, list
)
1731 wpas_p2p_srv_upnp_free(usrv
);
1733 wpas_p2p_sd_service_update(wpa_s
);
1737 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
1738 struct wpabuf
*query
, struct wpabuf
*resp
)
1740 struct p2p_srv_bonjour
*bsrv
;
1742 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1745 wpabuf_free(bsrv
->resp
);
1750 bsrv
= os_zalloc(sizeof(*bsrv
));
1753 bsrv
->query
= query
;
1755 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
1757 wpas_p2p_sd_service_update(wpa_s
);
1762 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
1763 const struct wpabuf
*query
)
1765 struct p2p_srv_bonjour
*bsrv
;
1767 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1770 wpas_p2p_srv_bonjour_free(bsrv
);
1771 wpas_p2p_sd_service_update(wpa_s
);
1776 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1777 const char *service
)
1779 struct p2p_srv_upnp
*usrv
;
1781 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
1782 return 0; /* Already listed */
1783 usrv
= os_zalloc(sizeof(*usrv
));
1786 usrv
->version
= version
;
1787 usrv
->service
= os_strdup(service
);
1788 if (usrv
->service
== NULL
) {
1792 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
1794 wpas_p2p_sd_service_update(wpa_s
);
1799 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1800 const char *service
)
1802 struct p2p_srv_upnp
*usrv
;
1804 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
1807 wpas_p2p_srv_upnp_free(usrv
);
1808 wpas_p2p_sd_service_update(wpa_s
);
1813 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
1814 const u8
*peer
, const char *params
,
1815 unsigned int generated_pin
)
1817 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
" %08d%s",
1818 MAC2STR(peer
), generated_pin
, params
);
1822 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
1823 const u8
*peer
, const char *params
)
1825 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
"%s",
1826 MAC2STR(peer
), params
);
1830 void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
1831 const u8
*dev_addr
, const u8
*pri_dev_type
,
1832 const char *dev_name
, u16 supp_config_methods
,
1833 u8 dev_capab
, u8 group_capab
)
1835 struct wpa_supplicant
*wpa_s
= ctx
;
1836 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1838 u8 empty_dev_type
[8];
1839 unsigned int generated_pin
= 0;
1841 if (pri_dev_type
== NULL
) {
1842 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
1843 pri_dev_type
= empty_dev_type
;
1845 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
1846 " pri_dev_type=%s name='%s' config_methods=0x%x "
1847 "dev_capab=0x%x group_capab=0x%x",
1849 wps_dev_type_bin2str(pri_dev_type
, devtype
,
1851 dev_name
, supp_config_methods
, dev_capab
, group_capab
);
1852 params
[sizeof(params
) - 1] = '\0';
1854 if (config_methods
& WPS_CONFIG_DISPLAY
) {
1855 generated_pin
= wps_generate_pin();
1856 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
1858 } else if (config_methods
& WPS_CONFIG_KEYPAD
)
1859 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
1860 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1861 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1862 "%s", MAC2STR(peer
), params
);
1866 void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
1868 struct wpa_supplicant
*wpa_s
= ctx
;
1869 unsigned int generated_pin
= 0;
1871 if (config_methods
& WPS_CONFIG_DISPLAY
)
1872 wpas_prov_disc_local_keypad(wpa_s
, peer
, "");
1873 else if (config_methods
& WPS_CONFIG_KEYPAD
) {
1874 generated_pin
= wps_generate_pin();
1875 wpas_prov_disc_local_display(wpa_s
, peer
, "", generated_pin
);
1876 } else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1877 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
,
1880 if (wpa_s
->pending_pd_before_join
&&
1881 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
1882 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
1883 wpa_s
->pending_pd_before_join
= 0;
1884 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
1885 "join-existing-group operation");
1886 wpas_p2p_join_start(wpa_s
);
1891 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
1892 const u8
*go_dev_addr
, const u8
*ssid
,
1893 size_t ssid_len
, int *go
, u8
*group_bssid
,
1894 int *force_freq
, int persistent_group
)
1896 struct wpa_supplicant
*wpa_s
= ctx
;
1898 u8 cur_bssid
[ETH_ALEN
];
1900 struct wpa_supplicant
*grp
;
1902 if (!persistent_group
) {
1903 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1904 " to join an active group", MAC2STR(sa
));
1905 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
1906 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
1908 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
1909 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
1910 "authorized invitation");
1914 * Do not accept the invitation automatically; notify user and
1917 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1920 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
1922 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
1923 "running persistent group");
1925 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
1929 if (!wpa_s
->conf
->persistent_reconnect
)
1930 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1932 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1933 if (s
->disabled
== 2 &&
1934 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
1935 s
->ssid_len
== ssid_len
&&
1936 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
1941 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1942 " requested reinvocation of an unknown group",
1944 return P2P_SC_FAIL_UNKNOWN_GROUP
;
1947 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
1949 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
1950 wpa_printf(MSG_DEBUG
, "P2P: The only available "
1951 "interface is already in use - reject "
1953 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1955 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
1956 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
1958 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
1960 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
1961 "interface address for the group");
1962 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1964 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
1969 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, cur_bssid
) == 0 &&
1970 wpa_s
->assoc_freq
) {
1971 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
1972 "the channel we are already using");
1973 *force_freq
= wpa_s
->assoc_freq
;
1976 res
= wpa_drv_shared_freq(wpa_s
);
1978 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
1979 "with the channel we are already using on a "
1980 "shared interface");
1984 return P2P_SC_SUCCESS
;
1988 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
1989 const u8
*ssid
, size_t ssid_len
,
1990 const u8
*go_dev_addr
, u8 status
,
1993 struct wpa_supplicant
*wpa_s
= ctx
;
1996 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1997 if (s
->disabled
== 2 &&
1998 s
->ssid_len
== ssid_len
&&
1999 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2003 if (status
== P2P_SC_SUCCESS
) {
2004 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2005 " was accepted; op_freq=%d MHz",
2006 MAC2STR(sa
), op_freq
);
2008 wpas_p2p_group_add_persistent(
2009 wpa_s
, s
, s
->mode
== WPAS_MODE_P2P_GO
, 0);
2011 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
2012 wpa_s
->p2p_wps_method
);
2017 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
2018 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2019 " was rejected (status %u)", MAC2STR(sa
), status
);
2025 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2026 "sa=" MACSTR
" go_dev_addr=" MACSTR
2027 " bssid=" MACSTR
" unknown-network",
2028 MAC2STR(sa
), MAC2STR(go_dev_addr
),
2031 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2032 "sa=" MACSTR
" go_dev_addr=" MACSTR
2034 MAC2STR(sa
), MAC2STR(go_dev_addr
));
2039 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
"sa=" MACSTR
2040 " persistent=%d", MAC2STR(sa
), s
->id
);
2044 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
)
2046 struct wpa_supplicant
*wpa_s
= ctx
;
2047 struct wpa_ssid
*ssid
;
2050 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2051 "status=%d " MACSTR
,
2052 status
, MAC2STR(bssid
));
2054 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2055 "status=%d ", status
);
2058 if (wpa_s
->pending_invite_ssid_id
== -1)
2059 return; /* Invitation to active group */
2061 if (status
!= P2P_SC_SUCCESS
) {
2062 wpas_p2p_remove_pending_group_interface(wpa_s
);
2066 ssid
= wpa_config_get_network(wpa_s
->conf
,
2067 wpa_s
->pending_invite_ssid_id
);
2069 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
2070 "data matching with invitation");
2074 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
2075 ssid
->mode
== WPAS_MODE_P2P_GO
, 0);
2079 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
2080 struct p2p_channels
*chan
)
2084 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
2087 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2088 chan
->reg_class
[cla
].reg_class
= 81;
2089 chan
->reg_class
[cla
].channels
= 11;
2090 for (i
= 0; i
< 11; i
++)
2091 chan
->reg_class
[cla
].channel
[i
] = i
+ 1;
2094 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
2097 /* Operating class 115 - 5 GHz, channels 36-48 */
2098 chan
->reg_class
[cla
].reg_class
= 115;
2099 chan
->reg_class
[cla
].channels
= 4;
2100 chan
->reg_class
[cla
].channel
[0] = 36;
2101 chan
->reg_class
[cla
].channel
[1] = 40;
2102 chan
->reg_class
[cla
].channel
[2] = 44;
2103 chan
->reg_class
[cla
].channel
[3] = 48;
2106 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
2109 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2110 chan
->reg_class
[cla
].reg_class
= 124;
2111 chan
->reg_class
[cla
].channels
= 4;
2112 chan
->reg_class
[cla
].channel
[0] = 149;
2113 chan
->reg_class
[cla
].channel
[1] = 153;
2114 chan
->reg_class
[cla
].channel
[2] = 157;
2115 chan
->reg_class
[cla
].channel
[3] = 161;
2118 chan
->reg_classes
= cla
;
2123 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
2125 enum hostapd_hw_mode mode
)
2129 for (i
= 0; i
< num_modes
; i
++) {
2130 if (modes
[i
].mode
== mode
)
2138 static int has_channel(struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
2142 for (i
= 0; i
< mode
->num_channels
; i
++) {
2143 if (mode
->channels
[i
].chan
== chan
) {
2145 *flags
= mode
->channels
[i
].flag
;
2146 return !(mode
->channels
[i
].flag
&
2147 (HOSTAPD_CHAN_DISABLED
|
2148 HOSTAPD_CHAN_PASSIVE_SCAN
|
2149 HOSTAPD_CHAN_NO_IBSS
|
2150 HOSTAPD_CHAN_RADAR
));
2158 struct p2p_oper_class_map
{
2159 enum hostapd_hw_mode mode
;
2164 enum { BW20
, BW40PLUS
, BW40MINUS
} bw
;
2167 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
2168 struct p2p_channels
*chan
)
2170 struct hostapd_hw_modes
*modes
, *mode
;
2171 u16 num_modes
, flags
;
2173 struct p2p_oper_class_map op_class
[] = {
2174 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
2175 { HOSTAPD_MODE_IEEE80211G
, 82, 14, 14, 1, BW20
},
2176 #if 0 /* Do not enable HT40 on 2 GHz for now */
2177 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
2178 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
2180 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
2181 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
2182 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
2183 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
2184 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
2185 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
2186 { -1, 0, 0, 0, 0, BW20
}
2189 modes
= wpa_drv_get_hw_feature_data(wpa_s
, &num_modes
, &flags
);
2190 if (modes
== NULL
) {
2191 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
2192 "of all supported channels; assume dualband "
2194 return wpas_p2p_default_channels(wpa_s
, chan
);
2199 for (op
= 0; op_class
[op
].op_class
; op
++) {
2200 struct p2p_oper_class_map
*o
= &op_class
[op
];
2202 struct p2p_reg_class
*reg
= NULL
;
2204 mode
= get_mode(modes
, num_modes
, o
->mode
);
2207 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
2209 if (!has_channel(mode
, ch
, &flag
))
2211 if (o
->bw
== BW40MINUS
&&
2212 (!(flag
& HOSTAPD_CHAN_HT40MINUS
) ||
2213 !has_channel(mode
, ch
- 4, NULL
)))
2215 if (o
->bw
== BW40PLUS
&&
2216 (!(flag
& HOSTAPD_CHAN_HT40PLUS
) ||
2217 !has_channel(mode
, ch
+ 4, NULL
)))
2220 wpa_printf(MSG_DEBUG
, "P2P: Add operating "
2221 "class %u", o
->op_class
);
2222 reg
= &chan
->reg_class
[cla
];
2224 reg
->reg_class
= o
->op_class
;
2226 reg
->channel
[reg
->channels
] = ch
;
2230 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
2231 reg
->channel
, reg
->channels
);
2235 chan
->reg_classes
= cla
;
2237 ieee80211_sta_free_hw_features(modes
, num_modes
);
2243 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
2246 struct wpa_supplicant
*wpa_s
= ctx
;
2248 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2249 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
2255 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
2260 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2261 * @global: Pointer to global data from wpa_supplicant_init()
2262 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2263 * Returns: 0 on success, -1 on failure
2265 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
2267 struct p2p_config p2p
;
2271 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
2274 #ifdef CONFIG_CLIENT_MLME
2275 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)) {
2276 wpa_s
->mlme
.public_action_cb
= p2p_rx_action_mlme
;
2277 wpa_s
->mlme
.public_action_cb_ctx
= wpa_s
;
2279 #endif /* CONFIG_CLIENT_MLME */
2281 if (wpa_drv_disable_11b_rates(wpa_s
, 1) < 0) {
2282 wpa_printf(MSG_DEBUG
, "P2P: Failed to disable 11b rates");
2283 /* Continue anyway; this is not really a fatal error */
2289 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2290 struct p2p_params params
;
2292 wpa_printf(MSG_DEBUG
, "P2P: Use driver-based P2P management");
2293 os_memset(¶ms
, 0, sizeof(params
));
2294 params
.dev_name
= wpa_s
->conf
->device_name
;
2295 if (wpa_s
->conf
->device_type
&&
2296 wps_dev_type_str2bin(wpa_s
->conf
->device_type
,
2297 params
.pri_dev_type
) < 0) {
2298 wpa_printf(MSG_ERROR
, "P2P: Invalid device_type");
2301 for (i
= 0; i
< MAX_SEC_DEVICE_TYPES
; i
++) {
2302 if (wpa_s
->conf
->sec_device_type
[i
] == NULL
)
2304 if (wps_dev_type_str2bin(
2305 wpa_s
->conf
->sec_device_type
[i
],
2306 params
.sec_dev_type
[
2307 params
.num_sec_dev_types
]) < 0) {
2308 wpa_printf(MSG_ERROR
, "P2P: Invalid "
2312 params
.num_sec_dev_types
++;
2313 if (params
.num_sec_dev_types
== DRV_MAX_SEC_DEV_TYPES
)
2316 if (wpa_drv_p2p_set_params(wpa_s
, ¶ms
) < 0)
2322 os_memset(&p2p
, 0, sizeof(p2p
));
2323 p2p
.msg_ctx
= wpa_s
;
2325 p2p
.p2p_scan
= wpas_p2p_scan
;
2326 p2p
.send_action
= wpas_send_action
;
2327 p2p
.send_action_done
= wpas_send_action_done
;
2328 p2p
.go_neg_completed
= wpas_go_neg_completed
;
2329 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
2330 p2p
.dev_found
= wpas_dev_found
;
2331 p2p
.start_listen
= wpas_start_listen
;
2332 p2p
.stop_listen
= wpas_stop_listen
;
2333 p2p
.send_probe_resp
= wpas_send_probe_resp
;
2334 p2p
.sd_request
= wpas_sd_request
;
2335 p2p
.sd_response
= wpas_sd_response
;
2336 p2p
.prov_disc_req
= wpas_prov_disc_req
;
2337 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
2338 p2p
.invitation_process
= wpas_invitation_process
;
2339 p2p
.invitation_received
= wpas_invitation_received
;
2340 p2p
.invitation_result
= wpas_invitation_result
;
2341 p2p
.get_noa
= wpas_get_noa
;
2343 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2344 os_memcpy(p2p
.dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2345 p2p
.dev_name
= wpa_s
->conf
->device_name
;
2347 if (wpa_s
->conf
->p2p_listen_reg_class
&&
2348 wpa_s
->conf
->p2p_listen_channel
) {
2349 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
2350 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
2354 * Pick one of the social channels randomly as the listen
2357 os_get_random((u8
*) &r
, sizeof(r
));
2358 p2p
.channel
= 1 + (r
% 3) * 5;
2360 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d", p2p
.channel
);
2362 if (wpa_s
->conf
->p2p_oper_reg_class
&&
2363 wpa_s
->conf
->p2p_oper_channel
) {
2364 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
2365 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
2366 p2p
.cfg_op_channel
= 1;
2367 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
2368 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2371 p2p
.op_reg_class
= 81;
2373 * Use random operation channel from (1, 6, 11) if no other
2374 * preference is indicated.
2376 os_get_random((u8
*) &r
, sizeof(r
));
2377 p2p
.op_channel
= 1 + (r
% 3) * 5;
2378 p2p
.cfg_op_channel
= 0;
2379 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
2380 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2382 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
2383 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
2384 p2p
.country
[2] = 0x04;
2386 os_memcpy(p2p
.country
, "XX\x04", 3);
2388 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
)) {
2389 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
2394 if (wpa_s
->conf
->device_type
&&
2395 wps_dev_type_str2bin(wpa_s
->conf
->device_type
, p2p
.pri_dev_type
) <
2397 wpa_printf(MSG_ERROR
, "P2P: Invalid device_type");
2401 for (i
= 0; i
< MAX_SEC_DEVICE_TYPES
; i
++) {
2402 if (wpa_s
->conf
->sec_device_type
[i
] == NULL
)
2404 if (wps_dev_type_str2bin(
2405 wpa_s
->conf
->sec_device_type
[i
],
2406 p2p
.sec_dev_type
[p2p
.num_sec_dev_types
]) < 0) {
2407 wpa_printf(MSG_ERROR
, "P2P: Invalid sec_device_type");
2410 p2p
.num_sec_dev_types
++;
2411 if (p2p
.num_sec_dev_types
== P2P_SEC_DEVICE_TYPES
)
2415 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
2416 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
2418 p2p
.max_peers
= 100;
2420 if (wpa_s
->conf
->p2p_ssid_postfix
) {
2421 p2p
.ssid_postfix_len
=
2422 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
2423 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
2424 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
2425 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
2426 p2p
.ssid_postfix_len
);
2429 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
2431 global
->p2p
= p2p_init(&p2p
);
2432 if (global
->p2p
== NULL
)
2440 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2441 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2443 * This function deinitialize per-interface P2P data.
2445 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
2447 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
2448 wpa_drv_probe_req_report(wpa_s
, 0);
2449 os_free(wpa_s
->go_params
);
2450 wpa_s
->go_params
= NULL
;
2451 wpabuf_free(wpa_s
->pending_action_tx
);
2452 wpa_s
->pending_action_tx
= NULL
;
2453 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
2454 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2455 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2456 wpa_s
->p2p_long_listen
= 0;
2457 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
2458 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
2459 wpas_p2p_remove_pending_group_interface(wpa_s
);
2461 /* TODO: remove group interface from the driver if this wpa_s instance
2462 * is on top of a P2P group interface */
2467 * wpas_p2p_deinit_global - Deinitialize global P2P module
2468 * @global: Pointer to global data from wpa_supplicant_init()
2470 * This function deinitializes the global (per device) P2P module.
2472 void wpas_p2p_deinit_global(struct wpa_global
*global
)
2474 struct wpa_supplicant
*wpa_s
, *tmp
;
2477 if (global
->p2p
== NULL
)
2480 /* Remove remaining P2P group interfaces */
2481 wpa_s
= global
->ifaces
;
2483 wpas_p2p_service_flush(wpa_s
);
2484 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
2485 wpa_s
= wpa_s
->next
;
2487 enum wpa_driver_if_type type
;
2488 tmp
= global
->ifaces
;
2491 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
2496 ifname
= os_strdup(tmp
->ifname
);
2497 type
= wpas_p2p_if_type(tmp
->p2p_group_interface
);
2498 wpa_supplicant_remove_iface(global
, tmp
);
2500 wpa_drv_if_remove(wpa_s
, type
, ifname
);
2505 * Deinit GO data on any possibly remaining interface (if main
2506 * interface is used as GO).
2508 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2509 if (wpa_s
->ap_iface
)
2510 wpas_p2p_group_deinit(wpa_s
);
2513 p2p_deinit(global
->p2p
);
2518 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
2520 if (wpa_s
->drv_flags
&
2521 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
2522 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
2523 return 1; /* P2P group requires a new interface in every case
2525 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
2526 return 0; /* driver does not support concurrent operations */
2527 if (wpa_s
->global
->ifaces
->next
)
2528 return 1; /* more that one interface already in use */
2529 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
2530 return 1; /* this interface is already in use */
2535 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
2536 const u8
*peer_addr
,
2537 enum p2p_wps_method wps_method
,
2538 int go_intent
, const u8
*own_interface_addr
,
2539 unsigned int force_freq
, int persistent_group
)
2541 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2542 return wpa_drv_p2p_connect(wpa_s
, peer_addr
, wps_method
,
2543 go_intent
, own_interface_addr
,
2544 force_freq
, persistent_group
);
2547 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2548 go_intent
, own_interface_addr
, force_freq
,
2553 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
2554 const u8
*peer_addr
,
2555 enum p2p_wps_method wps_method
,
2556 int go_intent
, const u8
*own_interface_addr
,
2557 unsigned int force_freq
, int persistent_group
)
2559 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2562 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2563 go_intent
, own_interface_addr
, force_freq
,
2568 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
2570 wpa_s
->p2p_join_scan_count
++;
2571 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
2572 wpa_s
->p2p_join_scan_count
);
2573 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
2574 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
2575 " for join operationg - stop join attempt",
2576 MAC2STR(wpa_s
->pending_join_iface_addr
));
2577 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2578 wpa_msg(wpa_s
->parent
, MSG_INFO
,
2579 P2P_EVENT_GROUP_FORMATION_FAILURE
);
2584 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
2585 struct wpa_scan_results
*scan_res
)
2587 struct wpa_bss
*bss
;
2589 u8 iface_addr
[ETH_ALEN
];
2591 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2593 if (wpa_s
->global
->p2p_disabled
)
2596 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for join",
2597 scan_res
? (int) scan_res
->num
: -1);
2600 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
2602 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2603 wpa_s
->pending_join_iface_addr
);
2605 p2p_get_interface_addr(wpa_s
->global
->p2p
,
2606 wpa_s
->pending_join_dev_addr
,
2608 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0)
2610 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
2611 "address for join from " MACSTR
" to " MACSTR
2612 " based on newly discovered P2P peer entry",
2613 MAC2STR(wpa_s
->pending_join_iface_addr
),
2614 MAC2STR(iface_addr
));
2615 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
2618 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2619 wpa_s
->pending_join_iface_addr
);
2622 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2623 "from P2P peer table: %d MHz", freq
);
2625 bss
= wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
);
2628 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2629 "from BSS table: %d MHz", freq
);
2634 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
2635 "prior to joining an existing group (GO " MACSTR
2637 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
2638 wpa_s
->pending_pd_before_join
= 1;
2640 switch (wpa_s
->pending_join_wps_method
) {
2642 case WPS_PIN_DISPLAY
:
2643 method
= WPS_CONFIG_KEYPAD
;
2645 case WPS_PIN_KEYPAD
:
2646 method
= WPS_CONFIG_DISPLAY
;
2649 method
= WPS_CONFIG_PUSHBUTTON
;
2656 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
2657 wpa_s
->pending_join_dev_addr
, method
, 1)
2659 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
2660 "Discovery Request before joining an "
2662 wpa_s
->pending_pd_before_join
= 0;
2667 * Actual join operation will be started from the Action frame
2668 * TX status callback.
2673 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
2674 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2675 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2676 wpas_p2p_check_join_scan_limit(wpa_s
);
2680 /* Start join operation immediately */
2681 wpas_p2p_join_start(wpa_s
);
2685 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
2687 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
2689 struct wpa_driver_scan_params params
;
2690 struct wpabuf
*wps_ie
, *ies
;
2692 os_memset(¶ms
, 0, sizeof(params
));
2694 /* P2P Wildcard SSID */
2695 params
.num_ssids
= 1;
2696 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
2697 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
2699 wpa_s
->wps
->dev
.p2p
= 1;
2700 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
2702 if (wps_ie
== NULL
) {
2703 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2707 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + 100);
2709 wpabuf_free(wps_ie
);
2710 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2713 wpabuf_put_buf(ies
, wps_ie
);
2714 wpabuf_free(wps_ie
);
2716 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
2718 params
.extra_ies
= wpabuf_head(ies
);
2719 params
.extra_ies_len
= wpabuf_len(ies
);
2722 * Run a scan to update BSS table and start Provision Discovery once
2723 * the new scan results become available.
2725 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
2726 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
2727 ret
= ieee80211_sta_req_scan(wpa_s
, ¶ms
);
2729 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
2734 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
2736 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2737 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2738 wpas_p2p_check_join_scan_limit(wpa_s
);
2743 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
2744 const u8
*dev_addr
, enum p2p_wps_method wps_method
)
2746 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
2747 MACSTR
" dev " MACSTR
")",
2748 MAC2STR(iface_addr
), MAC2STR(dev_addr
));
2750 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
2751 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
2752 wpa_s
->pending_join_wps_method
= wps_method
;
2754 /* Make sure we are not running find during connection establishment */
2755 wpas_p2p_stop_find(wpa_s
);
2757 wpa_s
->p2p_join_scan_count
= 0;
2758 wpas_p2p_join_scan(wpa_s
, NULL
);
2763 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
2765 struct wpa_supplicant
*group
;
2766 struct p2p_go_neg_results res
;
2768 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
2771 if (group
!= wpa_s
) {
2772 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
2773 sizeof(group
->p2p_pin
));
2774 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
2777 group
->p2p_in_provisioning
= 1;
2779 os_memset(&res
, 0, sizeof(res
));
2780 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
2782 res
.wps_method
= wpa_s
->pending_join_wps_method
;
2783 wpas_start_wps_enrollee(group
, &res
);
2786 * Allow a longer timeout for join-a-running-group than normal 15
2787 * second group formation timeout since the GO may not have authorized
2788 * our connection yet.
2790 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2791 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
2799 * wpas_p2p_connect - Request P2P Group Formation to be started
2800 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2801 * @peer_addr: Address of the peer P2P Device
2802 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2803 * @persistent_group: Whether to create a persistent group
2804 * @join: Whether to join an existing group (as a client) instead of starting
2805 * Group Owner negotiation; @peer_addr is BSSID in that case
2806 * @auth: Whether to only authorize the connection instead of doing that and
2807 * initiating Group Owner negotiation
2808 * @go_intent: GO Intent or -1 to use default
2809 * @freq: Frequency for the group or 0 for auto-selection
2810 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2811 * failure, -2 on failure due to channel not currently available,
2812 * -3 if forced channel is not supported
2814 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
2815 const char *pin
, enum p2p_wps_method wps_method
,
2816 int persistent_group
, int join
, int auth
, int go_intent
,
2819 int force_freq
= 0, oper_freq
= 0;
2822 enum wpa_driver_if_type iftype
;
2824 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2828 go_intent
= wpa_s
->conf
->p2p_go_intent
;
2831 wpa_s
->p2p_long_listen
= 0;
2833 wpa_s
->p2p_wps_method
= wps_method
;
2836 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
2837 else if (wps_method
== WPS_PIN_DISPLAY
) {
2838 ret
= wps_generate_pin();
2839 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
2841 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
2844 wpa_s
->p2p_pin
[0] = '\0';
2847 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
2849 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
2850 "connect a running group from " MACSTR
,
2851 MAC2STR(peer_addr
));
2852 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
2855 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
2856 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
2858 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
2859 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
2862 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
) <
2868 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
2870 oper_freq
= wpa_s
->assoc_freq
;
2872 oper_freq
= wpa_drv_shared_freq(wpa_s
);
2878 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
2879 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
2880 "(%u MHz) is not supported for P2P uses",
2885 if (oper_freq
> 0 && freq
!= oper_freq
&&
2886 !(wpa_s
->drv_flags
&
2887 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2888 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2889 "on %u MHz while connected on another "
2890 "channel (%u MHz)", freq
, oper_freq
);
2893 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2894 "requested channel (%u MHz)", freq
);
2896 } else if (oper_freq
> 0 &&
2897 !p2p_supported_freq(wpa_s
->global
->p2p
, oper_freq
)) {
2898 if (!(wpa_s
->drv_flags
&
2899 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2900 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2901 "while connected on non-P2P supported "
2902 "channel (%u MHz)", oper_freq
);
2905 wpa_printf(MSG_DEBUG
, "P2P: Current operating channel "
2906 "(%u MHz) not available for P2P - try to use "
2907 "another channel", oper_freq
);
2909 } else if (oper_freq
> 0) {
2910 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2911 "channel we are already using (%u MHz) on another "
2912 "interface", oper_freq
);
2913 force_freq
= oper_freq
;
2916 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
2918 if (!wpa_s
->create_p2p_iface
) {
2920 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2921 go_intent
, wpa_s
->own_addr
,
2922 force_freq
, persistent_group
)
2927 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
2928 go_intent
, wpa_s
->own_addr
,
2929 force_freq
, persistent_group
) < 0)
2934 /* Prepare to add a new interface for the group */
2935 iftype
= WPA_IF_P2P_GROUP
;
2937 iftype
= WPA_IF_P2P_CLIENT
;
2938 else if (go_intent
== 15)
2939 iftype
= WPA_IF_P2P_GO
;
2940 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
2941 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2942 "interface for the group");
2947 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2949 wpa_s
->pending_interface_addr
,
2950 force_freq
, persistent_group
) < 0)
2954 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
, go_intent
,
2955 wpa_s
->pending_interface_addr
,
2956 force_freq
, persistent_group
) < 0) {
2957 wpas_p2p_remove_pending_group_interface(wpa_s
);
2965 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2966 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2967 * @freq: Frequency of the channel in MHz
2968 * @duration: Duration of the stay on the channel in milliseconds
2970 * This callback is called when the driver indicates that it has started the
2971 * requested remain-on-channel duration.
2973 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
2974 unsigned int freq
, unsigned int duration
)
2976 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2978 wpa_s
->roc_waiting_drv_freq
= 0;
2979 wpa_s
->off_channel_freq
= freq
;
2980 wpas_send_action_cb(wpa_s
, NULL
);
2981 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
2982 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
2983 wpa_s
->pending_listen_duration
);
2984 wpa_s
->pending_listen_freq
= 0;
2989 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
2990 unsigned int timeout
)
2992 /* Limit maximum Listen state time based on driver limitation. */
2993 if (timeout
> wpa_s
->max_remain_on_chan
)
2994 timeout
= wpa_s
->max_remain_on_chan
;
2996 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2997 return wpa_drv_p2p_listen(wpa_s
, timeout
);
2999 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
3004 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3005 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3006 * @freq: Frequency of the channel in MHz
3008 * This callback is called when the driver indicates that a remain-on-channel
3009 * operation has been completed, i.e., the duration on the requested channel
3012 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
3015 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
3016 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3017 wpa_s
->p2p_long_listen
, wpa_s
->pending_action_tx
);
3018 wpa_s
->off_channel_freq
= 0;
3019 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3021 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
3022 return; /* P2P module started a new operation */
3023 if (wpa_s
->pending_action_tx
)
3025 if (wpa_s
->p2p_long_listen
> 0)
3026 wpa_s
->p2p_long_listen
-= wpa_s
->max_remain_on_chan
;
3027 if (wpa_s
->p2p_long_listen
> 0) {
3028 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
3029 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
);
3035 * wpas_p2p_group_remove - Remove a P2P group
3036 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3037 * @ifname: Network interface name of the group interface or "*" to remove all
3039 * Returns: 0 on success, -1 on failure
3041 * This function is used to remove a P2P group. This can be used to disconnect
3042 * from a group in which the local end is a P2P Client or to end a P2P Group in
3043 * case the local end is the Group Owner. If a virtual network interface was
3044 * created for this group, that interface will be removed. Otherwise, only the
3045 * configured P2P group network will be removed from the interface.
3047 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
3049 struct wpa_global
*global
= wpa_s
->global
;
3051 if (os_strcmp(ifname
, "*") == 0) {
3052 struct wpa_supplicant
*prev
;
3053 wpa_s
= global
->ifaces
;
3056 wpa_s
= wpa_s
->next
;
3057 wpas_p2p_disconnect(prev
);
3062 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3063 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3067 return wpas_p2p_disconnect(wpa_s
);
3071 static void wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
3072 struct p2p_go_neg_results
*params
,
3078 os_memset(params
, 0, sizeof(*params
));
3079 params
->role_go
= 1;
3081 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
3082 "frequency %d MHz", freq
);
3083 params
->freq
= freq
;
3084 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
3085 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
3086 wpa_s
->conf
->p2p_oper_channel
<= 11) {
3087 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3088 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3089 "frequency %d MHz", params
->freq
);
3090 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
3091 wpa_s
->conf
->p2p_oper_reg_class
== 118) {
3092 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3093 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3094 "frequency %d MHz", params
->freq
);
3095 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3096 wpa_s
->best_overall_freq
> 0 &&
3097 p2p_supported_freq(wpa_s
->global
->p2p
,
3098 wpa_s
->best_overall_freq
)) {
3099 params
->freq
= wpa_s
->best_overall_freq
;
3100 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
3101 "channel %d MHz", params
->freq
);
3102 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3103 wpa_s
->best_24_freq
> 0 &&
3104 p2p_supported_freq(wpa_s
->global
->p2p
,
3105 wpa_s
->best_24_freq
)) {
3106 params
->freq
= wpa_s
->best_24_freq
;
3107 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
3108 "channel %d MHz", params
->freq
);
3109 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3110 wpa_s
->best_5_freq
> 0 &&
3111 p2p_supported_freq(wpa_s
->global
->p2p
,
3112 wpa_s
->best_5_freq
)) {
3113 params
->freq
= wpa_s
->best_5_freq
;
3114 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
3115 "channel %d MHz", params
->freq
);
3117 params
->freq
= 2412;
3118 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference "
3119 "known)", params
->freq
);
3122 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
3123 wpa_s
->assoc_freq
&& !freq
) {
3124 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3126 params
->freq
= wpa_s
->assoc_freq
;
3129 res
= wpa_drv_shared_freq(wpa_s
);
3130 if (res
> 0 && !freq
) {
3131 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3132 "already using on a shared interface");
3138 static struct wpa_supplicant
*
3139 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
3142 struct wpa_supplicant
*group_wpa_s
;
3144 if (!wpas_p2p_create_iface(wpa_s
))
3147 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
3148 WPA_IF_P2P_CLIENT
) < 0)
3150 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
3151 if (group_wpa_s
== NULL
) {
3152 wpas_p2p_remove_pending_group_interface(wpa_s
);
3161 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3162 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3163 * @persistent_group: Whether to create a persistent group
3164 * @freq: Frequency for the group or 0 to indicate no hardcoding
3165 * Returns: 0 on success, -1 on failure
3167 * This function creates a new P2P group with the local end as the Group Owner,
3168 * i.e., without using Group Owner Negotiation.
3170 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
3173 struct p2p_go_neg_results params
;
3176 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3180 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
3182 if (wpa_s
->best_24_freq
> 0 &&
3183 p2p_supported_freq(wpa_s
->global
->p2p
,
3184 wpa_s
->best_24_freq
)) {
3185 freq
= wpa_s
->best_24_freq
;
3186 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
3187 "channel: %d MHz", freq
);
3189 os_get_random((u8
*) &r
, sizeof(r
));
3190 freq
= 2412 + (r
% 3) * 25;
3191 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
3192 "channel: %d MHz", freq
);
3197 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
3199 if (wpa_s
->best_5_freq
> 0 &&
3200 p2p_supported_freq(wpa_s
->global
->p2p
,
3201 wpa_s
->best_5_freq
)) {
3202 freq
= wpa_s
->best_5_freq
;
3203 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
3204 "channel: %d MHz", freq
);
3206 os_get_random((u8
*) &r
, sizeof(r
));
3207 freq
= 5180 + (r
% 4) * 20;
3208 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3209 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
3210 "5 GHz channel for P2P group");
3213 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
3214 "channel: %d MHz", freq
);
3218 if (freq
> 0 && !p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3219 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
3220 "(%u MHz) is not supported for P2P uses",
3225 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
3226 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
3227 params
.persistent_group
= persistent_group
;
3229 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
3232 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3238 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
3239 struct wpa_ssid
*params
, int addr_allocated
)
3241 struct wpa_ssid
*ssid
;
3243 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
3247 wpa_supplicant_ap_deinit(wpa_s
);
3249 ssid
= wpa_config_add_network(wpa_s
->conf
);
3252 wpas_notify_network_added(wpa_s
, ssid
);
3253 wpa_config_set_network_defaults(ssid
);
3254 ssid
->temporary
= 1;
3255 ssid
->proto
= WPA_PROTO_RSN
;
3256 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
3257 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
3258 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
3259 ssid
->ssid
= os_malloc(params
->ssid_len
);
3260 if (ssid
->ssid
== NULL
) {
3261 wpas_notify_network_removed(wpa_s
, ssid
);
3262 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
3265 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
3266 ssid
->ssid_len
= params
->ssid_len
;
3267 ssid
->p2p_group
= 1;
3268 ssid
->export_keys
= 1;
3269 if (params
->psk_set
) {
3270 os_memcpy(ssid
->psk
, params
->psk
, 32);
3273 if (params
->passphrase
)
3274 ssid
->passphrase
= os_strdup(params
->passphrase
);
3276 wpa_supplicant_select_network(wpa_s
, ssid
);
3278 wpa_s
->show_group_started
= 1;
3284 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
3285 struct wpa_ssid
*ssid
, int addr_allocated
,
3288 struct p2p_go_neg_results params
;
3291 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
3294 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
3295 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
3296 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
3301 /* Make sure we are not running find during connection establishment */
3302 wpas_p2p_stop_find(wpa_s
);
3304 if (ssid
->mode
== WPAS_MODE_INFRA
)
3305 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
3307 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
3310 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
3313 if (ssid
->passphrase
== NULL
||
3314 os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
3315 wpa_printf(MSG_DEBUG
, "P2P: Invalid passphrase in persistent "
3319 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
3320 sizeof(params
.passphrase
));
3321 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
3322 params
.ssid_len
= ssid
->ssid_len
;
3323 params
.persistent_group
= 1;
3325 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
3329 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3335 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
3336 struct wpabuf
*proberesp_ies
)
3338 struct wpa_supplicant
*wpa_s
= ctx
;
3339 if (wpa_s
->ap_iface
) {
3340 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
3342 wpabuf_free(hapd
->p2p_beacon_ie
);
3343 hapd
->p2p_beacon_ie
= beacon_ies
;
3345 wpabuf_free(hapd
->p2p_probe_resp_ie
);
3346 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
3348 wpabuf_free(beacon_ies
);
3349 wpabuf_free(proberesp_ies
);
3351 wpa_supplicant_ap_update_beacon(wpa_s
);
3355 static void wpas_p2p_idle_update(void *ctx
, int idle
)
3357 struct wpa_supplicant
*wpa_s
= ctx
;
3358 if (!wpa_s
->ap_iface
)
3360 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
3362 wpas_p2p_set_group_idle_timeout(wpa_s
);
3364 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3368 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
3369 int persistent_group
,
3370 int group_formation
)
3372 struct p2p_group
*group
;
3373 struct p2p_group_config
*cfg
;
3375 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3377 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3380 cfg
= os_zalloc(sizeof(*cfg
));
3384 cfg
->persistent_group
= persistent_group
;
3385 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
3386 if (wpa_s
->max_stations
&&
3387 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
3388 cfg
->max_clients
= wpa_s
->max_stations
;
3390 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
3391 cfg
->cb_ctx
= wpa_s
;
3392 cfg
->ie_update
= wpas_p2p_ie_update
;
3393 cfg
->idle_update
= wpas_p2p_idle_update
;
3395 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
3398 if (!group_formation
)
3399 p2p_group_notif_formation_done(group
);
3400 wpa_s
->p2p_group
= group
;
3405 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3408 if (!wpa_s
->p2p_in_provisioning
) {
3409 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
3410 "provisioning not in progress");
3414 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
3416 if (wpa_s
->global
->p2p
)
3417 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
3418 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3419 wpa_drv_wps_success_cb(wpa_s
, peer_addr
);
3420 wpas_group_formation_completed(wpa_s
, 1);
3424 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3425 const char *config_method
)
3429 if (os_strcmp(config_method
, "display") == 0)
3430 config_methods
= WPS_CONFIG_DISPLAY
;
3431 else if (os_strcmp(config_method
, "keypad") == 0)
3432 config_methods
= WPS_CONFIG_KEYPAD
;
3433 else if (os_strcmp(config_method
, "pbc") == 0 ||
3434 os_strcmp(config_method
, "pushbutton") == 0)
3435 config_methods
= WPS_CONFIG_PUSHBUTTON
;
3439 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3440 return wpa_drv_p2p_prov_disc_req(wpa_s
, peer_addr
,
3444 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
3447 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
3452 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
3455 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
3459 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
3461 if (!wpa_s
->pending_action_tx
)
3464 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
3465 "operation request");
3466 wpabuf_free(wpa_s
->pending_action_tx
);
3467 wpa_s
->pending_action_tx
= NULL
;
3471 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
3472 enum p2p_discovery_type type
)
3474 wpas_p2p_clear_pending_action_tx(wpa_s
);
3475 wpa_s
->p2p_long_listen
= 0;
3477 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3478 return wpa_drv_p2p_find(wpa_s
, timeout
, type
);
3480 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3483 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
);
3487 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
3489 wpas_p2p_clear_pending_action_tx(wpa_s
);
3490 wpa_s
->p2p_long_listen
= 0;
3491 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3492 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3494 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3495 wpa_drv_p2p_stop_find(wpa_s
);
3499 if (wpa_s
->global
->p2p
)
3500 p2p_stop_find(wpa_s
->global
->p2p
);
3502 wpas_p2p_remove_pending_group_interface(wpa_s
);
3506 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
3508 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3509 wpa_s
->p2p_long_listen
= 0;
3513 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
3517 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3520 wpas_p2p_clear_pending_action_tx(wpa_s
);
3524 * This is a request for unlimited Listen state. However, at
3525 * least for now, this is mapped to a Listen state for one
3530 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3531 wpa_s
->p2p_long_listen
= 0;
3533 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
3534 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
3535 wpa_s
->p2p_long_listen
= timeout
* 1000;
3536 eloop_register_timeout(timeout
, 0,
3537 wpas_p2p_long_listen_timeout
,
3545 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
3546 u8
*buf
, size_t len
, int p2p_group
)
3548 struct wpabuf
*p2p_ie
;
3551 if (wpa_s
->global
->p2p_disabled
)
3553 if (wpa_s
->global
->p2p
== NULL
)
3558 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
3559 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
3561 wpabuf_free(p2p_ie
);
3567 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
3568 const u8
*ie
, size_t ie_len
)
3570 if (wpa_s
->global
->p2p_disabled
)
3572 if (wpa_s
->global
->p2p
== NULL
)
3575 return p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, ie
, ie_len
);
3579 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
3580 const u8
*sa
, const u8
*bssid
,
3581 u8 category
, const u8
*data
, size_t len
, int freq
)
3583 if (wpa_s
->global
->p2p_disabled
)
3585 if (wpa_s
->global
->p2p
== NULL
)
3588 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
3593 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
3595 if (wpa_s
->global
->p2p_disabled
)
3597 if (wpa_s
->global
->p2p
== NULL
)
3600 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
3604 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
3606 p2p_group_deinit(wpa_s
->p2p_group
);
3607 wpa_s
->p2p_group
= NULL
;
3611 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
3613 wpa_s
->p2p_long_listen
= 0;
3615 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3616 return wpa_drv_p2p_reject(wpa_s
, addr
);
3618 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3621 return p2p_reject(wpa_s
->global
->p2p
, addr
);
3625 /* Invite to reinvoke a persistent group */
3626 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3627 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
)
3629 enum p2p_invite_role role
;
3632 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3633 role
= P2P_INVITE_ROLE_GO
;
3634 if (peer_addr
== NULL
) {
3635 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
3636 "address in invitation command");
3639 if (wpas_p2p_create_iface(wpa_s
)) {
3640 if (wpas_p2p_add_group_interface(wpa_s
,
3641 WPA_IF_P2P_GO
) < 0) {
3642 wpa_printf(MSG_ERROR
, "P2P: Failed to "
3643 "allocate a new interface for the "
3647 bssid
= wpa_s
->pending_interface_addr
;
3649 bssid
= wpa_s
->own_addr
;
3651 role
= P2P_INVITE_ROLE_CLIENT
;
3652 peer_addr
= ssid
->bssid
;
3654 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
3656 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3657 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3658 ssid
->ssid
, ssid
->ssid_len
,
3661 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3664 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3665 ssid
->ssid
, ssid
->ssid_len
, 0, go_dev_addr
, 1);
3669 /* Invite to join an active group */
3670 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
3671 const u8
*peer_addr
, const u8
*go_dev_addr
)
3673 struct wpa_global
*global
= wpa_s
->global
;
3674 enum p2p_invite_role role
;
3676 struct wpa_ssid
*ssid
;
3678 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3679 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3682 if (wpa_s
== NULL
) {
3683 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
3687 ssid
= wpa_s
->current_ssid
;
3689 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
3694 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3695 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
3696 bssid
= wpa_s
->own_addr
;
3697 if (go_dev_addr
== NULL
)
3698 go_dev_addr
= wpa_s
->parent
->own_addr
;
3700 role
= P2P_INVITE_ROLE_CLIENT
;
3701 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
3702 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
3703 "invite to current group");
3706 bssid
= wpa_s
->bssid
;
3707 if (go_dev_addr
== NULL
&&
3708 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
3709 go_dev_addr
= wpa_s
->go_dev_addr
;
3711 wpa_s
->parent
->pending_invite_ssid_id
= -1;
3713 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3714 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3715 ssid
->ssid
, ssid
->ssid_len
,
3718 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3721 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3722 ssid
->ssid
, ssid
->ssid_len
, wpa_s
->assoc_freq
,
3727 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
3729 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
3730 const char *ssid_txt
;
3731 u8 go_dev_addr
[ETH_ALEN
];
3734 if (!wpa_s
->show_group_started
|| !ssid
)
3737 wpa_s
->show_group_started
= 0;
3739 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
3740 os_memset(go_dev_addr
, 0, ETH_ALEN
);
3741 if (ssid
->bssid_set
)
3742 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
3743 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
3745 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
3747 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
3748 wpa_s
->global
->p2p_group_formation
= NULL
;
3750 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
3752 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
3753 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3754 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3756 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
3757 MAC2STR(go_dev_addr
),
3758 persistent
? " [PERSISTENT]" : "");
3760 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3761 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3762 "go_dev_addr=" MACSTR
"%s",
3763 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
,
3764 ssid
->passphrase
? ssid
->passphrase
: "",
3765 MAC2STR(go_dev_addr
),
3766 persistent
? " [PERSISTENT]" : "");
3770 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
3775 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
3776 u32 interval1
, u32 duration2
, u32 interval2
)
3778 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3780 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3783 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
3784 wpa_s
->current_ssid
== NULL
||
3785 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
3788 return p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
3789 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
3790 duration1
, interval1
, duration2
, interval2
);
3794 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
3795 unsigned int interval
)
3797 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3800 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3803 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
3807 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
3809 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3811 if (wpa_s
->conf
->p2p_group_idle
== 0) {
3812 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
3817 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
3819 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_IDLE_TIMEOUT
;
3820 wpas_p2p_group_delete(wpa_s
);
3824 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
3826 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3827 if (wpa_s
->conf
->p2p_group_idle
== 0)
3830 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
3833 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
3834 wpa_s
->conf
->p2p_group_idle
);
3835 eloop_register_timeout(wpa_s
->conf
->p2p_group_idle
, 0,
3836 wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3840 void wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3841 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3843 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3845 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3848 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3852 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3853 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3855 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3857 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3860 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3864 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
3866 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
3871 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
3874 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
3875 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
3877 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
) {
3879 if (wpa_s
->conf
->device_type
) {
3880 if (wps_dev_type_str2bin(wpa_s
->conf
->device_type
,
3881 pri_dev_type
) < 0) {
3882 wpa_printf(MSG_ERROR
, "P2P: Invalid "
3885 p2p_set_pri_dev_type(p2p
, pri_dev_type
);
3889 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
) {
3890 u8 sec_dev_type
[P2P_SEC_DEVICE_TYPES
][8];
3893 for (i
= 0; i
< MAX_SEC_DEVICE_TYPES
; i
++) {
3894 if (wpa_s
->conf
->sec_device_type
[i
] == NULL
)
3896 if (wps_dev_type_str2bin(
3897 wpa_s
->conf
->sec_device_type
[i
],
3898 sec_dev_type
[num
]) < 0) {
3899 wpa_printf(MSG_ERROR
, "P2P: Invalid "
3904 if (num
== P2P_SEC_DEVICE_TYPES
)
3907 p2p_set_sec_dev_types(p2p
, (void *) sec_dev_type
, num
);
3910 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
3911 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
3913 country
[0] = wpa_s
->conf
->country
[0];
3914 country
[1] = wpa_s
->conf
->country
[1];
3916 p2p_set_country(p2p
, country
);
3919 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
3920 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
3921 wpa_s
->conf
->p2p_ssid_postfix
?
3922 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
3926 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
3927 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
3931 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
3934 if (!wpa_s
->ap_iface
)
3936 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
3941 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
3943 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3945 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3948 wpa_s
->global
->cross_connection
= enabled
;
3949 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
3952 struct wpa_supplicant
*iface
;
3954 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
3956 if (iface
->cross_connect_enabled
== 0)
3959 iface
->cross_connect_enabled
= 0;
3960 iface
->cross_connect_in_use
= 0;
3961 wpa_msg(iface
->parent
, MSG_INFO
,
3962 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
3963 iface
->ifname
, iface
->cross_connect_uplink
);
3971 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
3973 struct wpa_supplicant
*iface
;
3975 if (!uplink
->global
->cross_connection
)
3978 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
3979 if (!iface
->cross_connect_enabled
)
3981 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
3984 if (iface
->ap_iface
== NULL
)
3986 if (iface
->cross_connect_in_use
)
3989 iface
->cross_connect_in_use
= 1;
3990 wpa_msg(iface
->parent
, MSG_INFO
,
3991 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
3992 iface
->ifname
, iface
->cross_connect_uplink
);
3997 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
3999 struct wpa_supplicant
*iface
;
4001 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
4002 if (!iface
->cross_connect_enabled
)
4004 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
4007 if (!iface
->cross_connect_in_use
)
4010 wpa_msg(iface
->parent
, MSG_INFO
,
4011 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
4012 iface
->ifname
, iface
->cross_connect_uplink
);
4013 iface
->cross_connect_in_use
= 0;
4018 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
4020 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
4021 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
4022 wpa_s
->cross_connect_disallowed
)
4023 wpas_p2p_disable_cross_connect(wpa_s
);
4025 wpas_p2p_enable_cross_connect(wpa_s
);
4026 if (!wpa_s
->ap_iface
)
4027 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
4031 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
4033 wpas_p2p_disable_cross_connect(wpa_s
);
4034 if (!wpa_s
->ap_iface
)
4035 wpas_p2p_set_group_idle_timeout(wpa_s
);
4039 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
4041 struct wpa_supplicant
*iface
;
4043 if (!wpa_s
->global
->cross_connection
)
4046 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
4049 if (iface
->drv_flags
&
4050 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
4052 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
4055 wpa_s
->cross_connect_enabled
= 1;
4056 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
4057 sizeof(wpa_s
->cross_connect_uplink
));
4058 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
4059 "%s to %s whenever uplink is available",
4060 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4062 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
4063 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
4064 iface
->cross_connect_disallowed
||
4065 iface
->wpa_state
!= WPA_COMPLETED
)
4068 wpa_s
->cross_connect_in_use
= 1;
4069 wpa_msg(wpa_s
->parent
, MSG_INFO
,
4070 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
4071 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4077 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
4079 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
4080 !wpa_s
->p2p_in_provisioning
)
4081 return 0; /* not P2P client operation */
4083 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
4085 if (wpa_s
!= wpa_s
->parent
)
4086 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
4088 if (wpa_s
->global
->p2p
)
4089 p2p_group_formation_failed(wpa_s
->global
->p2p
);
4091 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4092 wpa_s
->parent
, NULL
);
4094 wpas_group_formation_completed(wpa_s
, 0);
4099 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
4101 struct p2p_channels chan
;
4103 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
4106 os_memset(&chan
, 0, sizeof(chan
));
4107 if (wpas_p2p_setup_channels(wpa_s
, &chan
)) {
4108 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
4113 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
);
4117 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
4119 struct wpa_global
*global
= wpa_s
->global
;
4123 if (global
->p2p
== NULL
)
4126 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
4128 if (wpa_s
->pending_interface_name
[0] &&
4129 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
4132 peer
= p2p_get_go_neg_peer(global
->p2p
);
4134 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
4135 MACSTR
, MAC2STR(peer
));
4136 p2p_unauthorize(global
->p2p
, peer
);
4139 wpas_p2p_stop_find(wpa_s
);
4141 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4142 if (wpa_s
== global
->p2p_group_formation
&&
4143 (wpa_s
->p2p_in_provisioning
||
4144 wpa_s
->parent
->pending_interface_type
==
4145 WPA_IF_P2P_CLIENT
)) {
4146 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
4147 "formation found - cancelling",
4150 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4151 wpa_s
->parent
, NULL
);
4152 wpas_p2p_group_delete(wpa_s
);
4158 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
4166 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
4168 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
4171 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
4172 "being available anymore");
4173 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_UNAVAILABLE
;
4174 wpas_p2p_group_delete(wpa_s
);
4178 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
4179 int freq_24
, int freq_5
, int freq_overall
)
4181 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4182 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4184 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
4188 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
4191 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4193 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4196 if (hwaddr_aton(addr
, peer
))
4199 return p2p_unauthorize(p2p
, peer
);
4204 * wpas_p2p_disconnect - Disconnect from a P2P Group
4205 * @wpa_s: Pointer to wpa_supplicant data
4206 * Returns: 0 on success, -1 on failure
4208 * This can be used to disconnect from a group in which the local end is a P2P
4209 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4210 * virtual network interface was created for this group, that interface will be
4211 * removed. Otherwise, only the configured P2P group network will be removed
4212 * from the interface.
4214 int wpas_p2p_disconnect(struct wpa_supplicant
*wpa_s
)
4220 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_REQUESTED
;
4221 wpas_p2p_group_delete(wpa_s
);