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/ap_config.h"
26 #include "ap/p2p_hostapd.h"
27 #include "eapol_supp/eapol_supp_sm.h"
28 #include "rsn_supp/wpa.h"
29 #include "wpa_supplicant_i.h"
32 #include "config_ssid.h"
37 #include "offchannel.h"
38 #include "wps_supplicant.h"
39 #include "p2p_supplicant.h"
43 * How many times to try to scan to find the GO before giving up on join
46 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
48 #ifndef P2P_MAX_CLIENT_IDLE
50 * How many seconds to try to reconnect to the GO when connection in P2P client
53 #define P2P_MAX_CLIENT_IDLE 10
54 #endif /* P2P_MAX_CLIENT_IDLE */
57 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
);
58 static struct wpa_supplicant
*
59 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
61 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
);
62 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
);
63 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
64 const u8
*dev_addr
, enum p2p_wps_method wps_method
);
65 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
);
66 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
);
67 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
);
68 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
);
71 static void wpas_p2p_scan_res_handler(struct wpa_supplicant
*wpa_s
,
72 struct wpa_scan_results
*scan_res
)
76 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
79 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS)",
82 for (i
= 0; i
< scan_res
->num
; i
++) {
83 struct wpa_scan_res
*bss
= scan_res
->res
[i
];
84 if (p2p_scan_res_handler(wpa_s
->global
->p2p
, bss
->bssid
,
85 bss
->freq
, bss
->level
,
86 (const u8
*) (bss
+ 1),
91 p2p_scan_res_handled(wpa_s
->global
->p2p
);
95 static int wpas_p2p_scan(void *ctx
, enum p2p_scan_type type
, int freq
,
96 unsigned int num_req_dev_types
,
97 const u8
*req_dev_types
, const u8
*dev_id
)
99 struct wpa_supplicant
*wpa_s
= ctx
;
100 struct wpa_driver_scan_params params
;
102 struct wpabuf
*wps_ie
, *ies
;
103 int social_channels
[] = { 2412, 2437, 2462, 0, 0 };
107 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
110 os_memset(¶ms
, 0, sizeof(params
));
112 /* P2P Wildcard SSID */
113 params
.num_ssids
= 1;
114 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
115 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
117 wpa_s
->wps
->dev
.p2p
= 1;
118 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
120 num_req_dev_types
, req_dev_types
);
124 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
125 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
130 wpabuf_put_buf(ies
, wps_ie
);
133 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, dev_id
);
135 params
.p2p_probe
= 1;
136 params
.extra_ies
= wpabuf_head(ies
);
137 params
.extra_ies_len
= wpabuf_len(ies
);
140 case P2P_SCAN_SOCIAL
:
141 params
.freqs
= social_channels
;
145 case P2P_SCAN_SPECIFIC
:
146 social_channels
[0] = freq
;
147 social_channels
[1] = 0;
148 params
.freqs
= social_channels
;
150 case P2P_SCAN_SOCIAL_PLUS_ONE
:
151 social_channels
[3] = freq
;
152 params
.freqs
= social_channels
;
156 was_in_p2p_scan
= wpa_s
->scan_res_handler
== wpas_p2p_scan_res_handler
;
157 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
158 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
163 wpa_s
->scan_res_handler
= NULL
;
164 if (wpa_s
->scanning
|| was_in_p2p_scan
) {
165 wpa_s
->p2p_cb_on_scan_complete
= 1;
174 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
176 switch (p2p_group_interface
) {
177 case P2P_GROUP_INTERFACE_PENDING
:
178 return WPA_IF_P2P_GROUP
;
179 case P2P_GROUP_INTERFACE_GO
:
180 return WPA_IF_P2P_GO
;
181 case P2P_GROUP_INTERFACE_CLIENT
:
182 return WPA_IF_P2P_CLIENT
;
185 return WPA_IF_P2P_GROUP
;
189 static struct wpa_supplicant
* wpas_get_p2p_group(struct wpa_supplicant
*wpa_s
,
191 size_t ssid_len
, int *go
)
195 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
196 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
197 if (s
->disabled
!= 0 || !s
->p2p_group
||
198 s
->ssid_len
!= ssid_len
||
199 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
201 if (s
->mode
== WPAS_MODE_P2P_GO
&&
202 s
!= wpa_s
->current_ssid
)
205 *go
= s
->mode
== WPAS_MODE_P2P_GO
;
214 static void wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
)
216 struct wpa_ssid
*ssid
;
220 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
222 ssid
= wpa_s
->current_ssid
;
225 * The current SSID was not known, but there may still be a
226 * pending P2P group interface waiting for provisioning.
228 ssid
= wpa_s
->conf
->ssid
;
230 if (ssid
->p2p_group
&&
231 (ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
232 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)))
237 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
239 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
240 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
241 wpa_s
->reassociate
= 0;
242 wpa_s
->disconnected
= 1;
243 wpa_supplicant_deauthenticate(wpa_s
,
244 WLAN_REASON_DEAUTH_LEAVING
);
248 if (wpa_s
->cross_connect_in_use
) {
249 wpa_s
->cross_connect_in_use
= 0;
250 wpa_msg(wpa_s
->parent
, MSG_INFO
,
251 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
252 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
254 switch (wpa_s
->removal_reason
) {
255 case P2P_GROUP_REMOVAL_REQUESTED
:
256 reason
= " reason=REQUESTED";
258 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
259 reason
= " reason=IDLE";
261 case P2P_GROUP_REMOVAL_UNAVAILABLE
:
262 reason
= " reason=UNAVAILABLE";
268 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_REMOVED
"%s %s%s",
269 wpa_s
->ifname
, gtype
, reason
);
272 wpas_notify_p2p_group_removed(wpa_s
, ssid
, gtype
);
274 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
275 struct wpa_global
*global
;
277 enum wpa_driver_if_type type
;
278 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
280 global
= wpa_s
->global
;
281 ifname
= os_strdup(wpa_s
->ifname
);
282 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
283 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
, 0);
284 wpa_s
= global
->ifaces
;
286 wpa_drv_if_remove(wpa_s
, type
, ifname
);
291 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
292 if (ssid
&& (ssid
->p2p_group
||
293 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
294 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
296 if (ssid
== wpa_s
->current_ssid
) {
297 wpa_sm_set_config(wpa_s
->wpa
, NULL
);
298 eapol_sm_notify_config(wpa_s
->eapol
, NULL
, NULL
);
299 wpa_s
->current_ssid
= NULL
;
302 * Networks objects created during any P2P activities are not
303 * exposed out as they might/will confuse certain non-P2P aware
304 * applications since these network objects won't behave like
307 * Likewise, we don't send out network removed signals for such
310 wpa_config_remove_network(wpa_s
->conf
, id
);
311 wpa_supplicant_clear_status(wpa_s
);
312 wpa_supplicant_cancel_sched_scan(wpa_s
);
314 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
317 wpa_supplicant_ap_deinit(wpa_s
);
321 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
323 const u8
*ssid
, size_t ssid_len
)
331 if (wpa_s
->go_params
)
332 bssid
= wpa_s
->go_params
->peer_interface_addr
;
334 bssid
= wpa_s
->bssid
;
336 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
338 u8 iface_addr
[ETH_ALEN
];
339 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
341 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
344 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
345 "group is persistent - BSS " MACSTR
" not found",
350 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
352 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
353 "group is persistent - BSS " MACSTR
354 " did not include P2P IE", MAC2STR(bssid
));
355 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
356 (u8
*) (bss
+ 1), bss
->ie_len
);
357 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
358 ((u8
*) bss
+ 1) + bss
->ie_len
,
363 group_capab
= p2p_get_group_capab(p2p
);
364 addr
= p2p_get_go_dev_addr(p2p
);
365 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
366 "group_capab=0x%x", group_capab
);
368 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
369 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
372 os_memset(go_dev_addr
, 0, ETH_ALEN
);
375 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
376 "go_dev_addr=" MACSTR
,
377 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
379 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
383 static int wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
384 struct wpa_ssid
*ssid
,
385 const u8
*go_dev_addr
)
390 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
391 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
392 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
393 if (s
->disabled
== 2 &&
394 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
395 s
->ssid_len
== ssid
->ssid_len
&&
396 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
401 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
403 if (ssid
->passphrase
&& !s
->passphrase
)
405 else if (ssid
->passphrase
&& s
->passphrase
&&
406 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
409 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
412 s
= wpa_config_add_network(wpa_s
->conf
);
417 * Instead of network_added we emit persistent_group_added
418 * notification. Also to keep the defense checks in
419 * persistent_group obj registration method, we set the
420 * relevant flags in s to designate it as a persistent group.
423 s
->p2p_persistent_group
= 1;
424 wpas_notify_persistent_group_added(wpa_s
, s
);
425 wpa_config_set_network_defaults(s
);
429 s
->p2p_persistent_group
= 1;
432 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
433 s
->mode
= ssid
->mode
;
434 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
435 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
436 s
->proto
= WPA_PROTO_RSN
;
437 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
439 if (ssid
->passphrase
) {
440 os_free(s
->passphrase
);
441 s
->passphrase
= os_strdup(ssid
->passphrase
);
445 os_memcpy(s
->psk
, ssid
->psk
, 32);
447 if (s
->passphrase
&& !s
->psk_set
)
448 wpa_config_update_psk(s
);
449 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
451 s
->ssid
= os_malloc(ssid
->ssid_len
);
454 s
->ssid_len
= ssid
->ssid_len
;
455 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
458 #ifndef CONFIG_NO_CONFIG_WRITE
459 if (changed
&& wpa_s
->conf
->update_config
&&
460 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
461 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
463 #endif /* CONFIG_NO_CONFIG_WRITE */
469 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant
*wpa_s
,
472 struct wpa_ssid
*ssid
, *s
;
476 ssid
= wpa_s
->current_ssid
;
477 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GO
||
478 !ssid
->p2p_persistent_group
)
481 for (s
= wpa_s
->parent
->conf
->ssid
; s
; s
= s
->next
) {
482 if (s
->disabled
!= 2 || s
->mode
!= WPAS_MODE_P2P_GO
)
485 if (s
->ssid_len
== ssid
->ssid_len
&&
486 os_memcmp(s
->ssid
, ssid
->ssid
, s
->ssid_len
) == 0)
493 for (i
= 0; s
->p2p_client_list
&& i
< s
->num_p2p_clients
; i
++) {
494 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
, addr
,
496 return; /* already in list */
499 n
= os_realloc(s
->p2p_client_list
,
500 (s
->num_p2p_clients
+ 1) * ETH_ALEN
);
503 os_memcpy(n
+ s
->num_p2p_clients
* ETH_ALEN
, addr
, ETH_ALEN
);
504 s
->p2p_client_list
= n
;
505 s
->num_p2p_clients
++;
507 #ifndef CONFIG_NO_CONFIG_WRITE
508 if (wpa_s
->parent
->conf
->update_config
&&
509 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
510 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
511 #endif /* CONFIG_NO_CONFIG_WRITE */
515 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
518 struct wpa_ssid
*ssid
;
519 const char *ssid_txt
;
522 u8 go_dev_addr
[ETH_ALEN
];
526 * This callback is likely called for the main interface. Update wpa_s
527 * to use the group interface if a new interface was created for the
530 if (wpa_s
->global
->p2p_group_formation
)
531 wpa_s
= wpa_s
->global
->p2p_group_formation
;
532 wpa_s
->global
->p2p_group_formation
= NULL
;
533 wpa_s
->p2p_in_provisioning
= 0;
536 wpa_msg(wpa_s
->parent
, MSG_INFO
,
537 P2P_EVENT_GROUP_FORMATION_FAILURE
);
538 wpas_p2p_group_delete(wpa_s
);
542 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_FORMATION_SUCCESS
);
544 ssid
= wpa_s
->current_ssid
;
545 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
546 ssid
->mode
= WPAS_MODE_P2P_GO
;
547 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
548 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
553 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
554 client
= ssid
->mode
== WPAS_MODE_INFRA
;
555 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
556 persistent
= ssid
->p2p_persistent_group
;
557 os_memcpy(go_dev_addr
, wpa_s
->global
->p2p_dev_addr
,
560 persistent
= wpas_p2p_persistent_group(wpa_s
,
566 client
= wpa_s
->p2p_group_interface
==
567 P2P_GROUP_INTERFACE_CLIENT
;
568 os_memset(go_dev_addr
, 0, ETH_ALEN
);
571 wpa_s
->show_group_started
= 0;
574 * Indicate event only after successfully completed 4-way
575 * handshake, i.e., when the interface is ready for data
578 wpa_s
->show_group_started
= 1;
579 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
581 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
582 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
583 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
585 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
586 MAC2STR(go_dev_addr
),
587 persistent
? " [PERSISTENT]" : "");
588 wpas_p2p_cross_connect_setup(wpa_s
);
589 wpas_p2p_set_group_idle_timeout(wpa_s
);
591 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
592 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
593 "go_dev_addr=" MACSTR
"%s",
594 wpa_s
->ifname
, ssid_txt
, ssid
? ssid
->frequency
: 0,
595 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
596 MAC2STR(go_dev_addr
),
597 persistent
? " [PERSISTENT]" : "");
598 wpas_p2p_cross_connect_setup(wpa_s
);
599 wpas_p2p_set_group_idle_timeout(wpa_s
);
603 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
605 if (network_id
< 0 && ssid
)
606 network_id
= ssid
->id
;
608 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
612 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant
*wpa_s
,
614 const u8
*dst
, const u8
*src
,
616 const u8
*data
, size_t data_len
,
617 enum offchannel_send_action_result
620 enum p2p_send_action_result res
= P2P_SEND_ACTION_SUCCESS
;
622 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
624 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
628 case OFFCHANNEL_SEND_ACTION_SUCCESS
:
629 res
= P2P_SEND_ACTION_SUCCESS
;
631 case OFFCHANNEL_SEND_ACTION_NO_ACK
:
632 res
= P2P_SEND_ACTION_NO_ACK
;
634 case OFFCHANNEL_SEND_ACTION_FAILED
:
635 res
= P2P_SEND_ACTION_FAILED
;
639 p2p_send_action_cb(wpa_s
->global
->p2p
, freq
, dst
, src
, bssid
, res
);
641 if (wpa_s
->pending_pd_before_join
&&
642 (os_memcmp(dst
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
643 os_memcmp(dst
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
644 wpa_s
->pending_pd_before_join
= 0;
645 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
646 "join-existing-group operation");
647 wpas_p2p_join_start(wpa_s
);
652 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
653 const u8
*src
, const u8
*bssid
, const u8
*buf
,
654 size_t len
, unsigned int wait_time
)
656 struct wpa_supplicant
*wpa_s
= ctx
;
657 return offchannel_send_action(wpa_s
, freq
, dst
, src
, bssid
, buf
, len
,
659 wpas_p2p_send_action_tx_status
, 1);
663 static void wpas_send_action_done(void *ctx
)
665 struct wpa_supplicant
*wpa_s
= ctx
;
666 offchannel_send_action_done(wpa_s
);
670 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
671 struct p2p_go_neg_results
*params
)
673 if (wpa_s
->go_params
== NULL
) {
674 wpa_s
->go_params
= os_malloc(sizeof(*params
));
675 if (wpa_s
->go_params
== NULL
)
678 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
683 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
684 struct p2p_go_neg_results
*res
)
686 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
687 MAC2STR(res
->peer_interface_addr
));
688 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
689 res
->ssid
, res
->ssid_len
);
690 wpa_supplicant_ap_deinit(wpa_s
);
691 wpas_copy_go_neg_results(wpa_s
, res
);
692 if (res
->wps_method
== WPS_PBC
)
693 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
695 u16 dev_pw_id
= DEV_PW_DEFAULT
;
696 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
697 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
698 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
699 wpa_s
->p2p_pin
, 1, dev_pw_id
);
704 static void p2p_go_configured(void *ctx
, void *data
)
706 struct wpa_supplicant
*wpa_s
= ctx
;
707 struct p2p_go_neg_results
*params
= data
;
708 struct wpa_ssid
*ssid
;
711 ssid
= wpa_s
->current_ssid
;
712 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
713 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
714 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
715 wpa_s
->global
->p2p_group_formation
= NULL
;
716 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
717 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
718 "go_dev_addr=" MACSTR
"%s",
720 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
722 params
->passphrase
? params
->passphrase
: "",
723 MAC2STR(wpa_s
->global
->p2p_dev_addr
),
724 params
->persistent_group
? " [PERSISTENT]" : "");
726 if (params
->persistent_group
)
727 network_id
= wpas_p2p_store_persistent_group(
729 wpa_s
->global
->p2p_dev_addr
);
731 network_id
= ssid
->id
;
732 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
733 wpas_p2p_cross_connect_setup(wpa_s
);
734 wpas_p2p_set_group_idle_timeout(wpa_s
);
738 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
739 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
740 params
->peer_interface_addr
)) {
741 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
745 if (params
->wps_method
== WPS_PBC
)
746 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
,
747 params
->peer_device_addr
);
748 else if (wpa_s
->p2p_pin
[0])
749 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
750 wpa_s
->p2p_pin
, NULL
, 0);
751 os_free(wpa_s
->go_params
);
752 wpa_s
->go_params
= NULL
;
756 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
757 struct p2p_go_neg_results
*params
,
760 struct wpa_ssid
*ssid
;
762 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0)
765 ssid
= wpa_config_add_network(wpa_s
->conf
);
769 wpa_s
->show_group_started
= 0;
771 wpa_config_set_network_defaults(ssid
);
774 ssid
->p2p_persistent_group
= params
->persistent_group
;
775 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
777 ssid
->frequency
= params
->freq
;
778 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
780 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
781 ssid
->ssid_len
= params
->ssid_len
;
783 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
784 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
785 ssid
->proto
= WPA_PROTO_RSN
;
786 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
787 ssid
->passphrase
= os_strdup(params
->passphrase
);
789 wpa_s
->ap_configured_cb
= p2p_go_configured
;
790 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
791 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
792 wpa_s
->connect_without_scan
= ssid
;
793 wpa_s
->reassociate
= 1;
794 wpa_s
->disconnected
= 0;
795 wpa_supplicant_req_scan(wpa_s
, 0, 0);
799 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
800 const struct wpa_supplicant
*src
)
802 struct wpa_config
*d
;
803 const struct wpa_config
*s
;
808 #define C(n) if (s->n) d->n = os_strdup(s->n)
817 os_memcpy(d
->device_type
, s
->device_type
, WPS_DEV_TYPE_LEN
);
818 os_memcpy(d
->sec_device_type
, s
->sec_device_type
,
819 sizeof(d
->sec_device_type
));
820 d
->num_sec_device_types
= s
->num_sec_device_types
;
822 d
->p2p_group_idle
= s
->p2p_group_idle
;
823 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
824 d
->persistent_reconnect
= s
->persistent_reconnect
;
828 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
829 enum wpa_driver_if_type type
)
831 char ifname
[120], force_ifname
[120];
833 if (wpa_s
->pending_interface_name
[0]) {
834 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
835 "- skip creation of a new one");
836 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
837 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
838 "unknown?! ifname='%s'",
839 wpa_s
->pending_interface_name
);
845 os_snprintf(ifname
, sizeof(ifname
), "p2p-%s-%d", wpa_s
->ifname
,
846 wpa_s
->p2p_group_idx
);
847 if (os_strlen(ifname
) >= IFNAMSIZ
&&
848 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
849 /* Try to avoid going over the IFNAMSIZ length limit */
850 os_snprintf(ifname
, sizeof(ifname
), "p2p-%d",
851 wpa_s
->p2p_group_idx
);
853 force_ifname
[0] = '\0';
855 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
857 wpa_s
->p2p_group_idx
++;
859 wpa_s
->pending_interface_type
= type
;
860 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
861 wpa_s
->pending_interface_addr
, NULL
) < 0) {
862 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
867 if (force_ifname
[0]) {
868 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
870 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
871 sizeof(wpa_s
->pending_interface_name
));
873 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
874 sizeof(wpa_s
->pending_interface_name
));
875 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
876 MACSTR
, wpa_s
->pending_interface_name
,
877 MAC2STR(wpa_s
->pending_interface_addr
));
883 static void wpas_p2p_remove_pending_group_interface(
884 struct wpa_supplicant
*wpa_s
)
886 if (!wpa_s
->pending_interface_name
[0] ||
887 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
888 return; /* No pending virtual interface */
890 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
891 wpa_s
->pending_interface_name
);
892 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
893 wpa_s
->pending_interface_name
);
894 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
895 wpa_s
->pending_interface_name
[0] = '\0';
899 static struct wpa_supplicant
*
900 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
902 struct wpa_interface iface
;
903 struct wpa_supplicant
*group_wpa_s
;
905 if (!wpa_s
->pending_interface_name
[0]) {
906 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
907 if (!wpas_p2p_create_iface(wpa_s
))
910 * Something has forced us to remove the pending interface; try
911 * to create a new one and hope for the best that we will get
912 * the same local address.
914 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
915 WPA_IF_P2P_CLIENT
) < 0)
919 os_memset(&iface
, 0, sizeof(iface
));
920 iface
.ifname
= wpa_s
->pending_interface_name
;
921 iface
.driver
= wpa_s
->driver
->name
;
922 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
923 iface
.driver_param
= wpa_s
->conf
->driver_param
;
924 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
925 if (group_wpa_s
== NULL
) {
926 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
927 "wpa_supplicant interface");
930 wpa_s
->pending_interface_name
[0] = '\0';
931 group_wpa_s
->parent
= wpa_s
;
932 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
933 P2P_GROUP_INTERFACE_CLIENT
;
934 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
936 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
942 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
945 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
946 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
947 if (wpa_s
->global
->p2p
)
948 p2p_group_formation_failed(wpa_s
->global
->p2p
);
949 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
950 wpa_drv_p2p_group_formation_failed(wpa_s
);
951 wpas_group_formation_completed(wpa_s
, 0);
955 void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
957 struct wpa_supplicant
*wpa_s
= ctx
;
959 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
960 wpa_drv_cancel_remain_on_channel(wpa_s
);
961 wpa_s
->off_channel_freq
= 0;
962 wpa_s
->roc_waiting_drv_freq
= 0;
966 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_FAILURE
"status=%d",
968 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
969 wpas_p2p_remove_pending_group_interface(wpa_s
);
973 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
);
974 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
976 if (wpa_s
->create_p2p_iface
) {
977 struct wpa_supplicant
*group_wpa_s
=
978 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
979 if (group_wpa_s
== NULL
) {
980 wpas_p2p_remove_pending_group_interface(wpa_s
);
983 if (group_wpa_s
!= wpa_s
) {
984 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
985 sizeof(group_wpa_s
->p2p_pin
));
986 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
988 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
989 wpa_s
->pending_interface_name
[0] = '\0';
990 group_wpa_s
->p2p_in_provisioning
= 1;
993 wpas_start_wps_go(group_wpa_s
, res
, 1);
995 wpas_start_wps_enrollee(group_wpa_s
, res
);
997 wpa_s
->p2p_in_provisioning
= 1;
998 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1001 wpas_start_wps_go(wpa_s
, res
, 1);
1003 wpas_start_wps_enrollee(ctx
, res
);
1006 wpa_s
->p2p_long_listen
= 0;
1007 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1009 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1010 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1011 (res
->peer_config_timeout
% 100) * 10000,
1012 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1016 void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1018 struct wpa_supplicant
*wpa_s
= ctx
;
1019 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1020 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1022 wpas_notify_p2p_go_neg_req(wpa_s
, src
, dev_passwd_id
);
1026 void wpas_dev_found(void *ctx
, const u8
*addr
,
1027 const struct p2p_peer_info
*info
,
1030 #ifndef CONFIG_NO_STDOUT_DEBUG
1031 struct wpa_supplicant
*wpa_s
= ctx
;
1032 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1034 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1035 " p2p_dev_addr=" MACSTR
1036 " pri_dev_type=%s name='%s' config_methods=0x%x "
1037 "dev_capab=0x%x group_capab=0x%x",
1038 MAC2STR(addr
), MAC2STR(info
->p2p_device_addr
),
1039 wps_dev_type_bin2str(info
->pri_dev_type
, devtype
,
1041 info
->device_name
, info
->config_methods
,
1042 info
->dev_capab
, info
->group_capab
);
1043 #endif /* CONFIG_NO_STDOUT_DEBUG */
1045 wpas_notify_p2p_device_found(ctx
, info
->p2p_device_addr
, new_device
);
1049 static void wpas_dev_lost(void *ctx
, const u8
*dev_addr
)
1051 struct wpa_supplicant
*wpa_s
= ctx
;
1053 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_LOST
1054 "p2p_dev_addr=" MACSTR
, MAC2STR(dev_addr
));
1056 wpas_notify_p2p_device_lost(wpa_s
, dev_addr
);
1060 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1061 unsigned int duration
,
1062 const struct wpabuf
*probe_resp_ie
)
1064 struct wpa_supplicant
*wpa_s
= ctx
;
1066 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, probe_resp_ie
, NULL
);
1068 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1069 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1070 "report received Probe Request frames");
1074 wpa_s
->pending_listen_freq
= freq
;
1075 wpa_s
->pending_listen_duration
= duration
;
1077 if (wpa_drv_remain_on_channel(wpa_s
, freq
, duration
) < 0) {
1078 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1079 "to remain on channel (%u MHz) for Listen "
1081 wpa_s
->pending_listen_freq
= 0;
1084 wpa_s
->off_channel_freq
= 0;
1085 wpa_s
->roc_waiting_drv_freq
= freq
;
1091 static void wpas_stop_listen(void *ctx
)
1093 struct wpa_supplicant
*wpa_s
= ctx
;
1094 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1095 wpa_drv_cancel_remain_on_channel(wpa_s
);
1096 wpa_s
->off_channel_freq
= 0;
1097 wpa_s
->roc_waiting_drv_freq
= 0;
1099 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, NULL
, NULL
);
1100 wpa_drv_probe_req_report(wpa_s
, 0);
1104 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1106 struct wpa_supplicant
*wpa_s
= ctx
;
1107 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
), 1);
1111 static struct p2p_srv_bonjour
*
1112 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1113 const struct wpabuf
*query
)
1115 struct p2p_srv_bonjour
*bsrv
;
1118 len
= wpabuf_len(query
);
1119 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1120 struct p2p_srv_bonjour
, list
) {
1121 if (len
== wpabuf_len(bsrv
->query
) &&
1122 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1130 static struct p2p_srv_upnp
*
1131 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1132 const char *service
)
1134 struct p2p_srv_upnp
*usrv
;
1136 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1137 struct p2p_srv_upnp
, list
) {
1138 if (version
== usrv
->version
&&
1139 os_strcmp(service
, usrv
->service
) == 0)
1146 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1151 if (wpabuf_tailroom(resp
) < 5)
1154 /* Length (to be filled) */
1155 len_pos
= wpabuf_put(resp
, 2);
1156 wpabuf_put_u8(resp
, srv_proto
);
1157 wpabuf_put_u8(resp
, srv_trans_id
);
1159 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1160 /* Response Data: empty */
1161 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1165 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1166 struct wpabuf
*resp
, u8 srv_trans_id
)
1168 struct p2p_srv_bonjour
*bsrv
;
1171 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1173 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1174 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1178 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1179 struct p2p_srv_bonjour
, list
) {
1180 if (wpabuf_tailroom(resp
) <
1181 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1183 /* Length (to be filled) */
1184 len_pos
= wpabuf_put(resp
, 2);
1185 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1186 wpabuf_put_u8(resp
, srv_trans_id
);
1188 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1189 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1190 wpabuf_head(bsrv
->resp
),
1191 wpabuf_len(bsrv
->resp
));
1193 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1194 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1195 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1201 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
1202 struct wpabuf
*resp
, u8 srv_trans_id
,
1203 const u8
*query
, size_t query_len
)
1205 struct p2p_srv_bonjour
*bsrv
;
1209 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
1211 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1212 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1213 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
1218 if (query_len
== 0) {
1219 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1223 if (wpabuf_tailroom(resp
) < 5)
1225 /* Length (to be filled) */
1226 len_pos
= wpabuf_put(resp
, 2);
1227 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1228 wpabuf_put_u8(resp
, srv_trans_id
);
1230 wpabuf_set(&buf
, query
, query_len
);
1231 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, &buf
);
1233 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
1237 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1238 /* Response Data: empty */
1239 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1245 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1246 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1247 wpabuf_head(bsrv
->resp
), wpabuf_len(bsrv
->resp
));
1249 if (wpabuf_tailroom(resp
) >=
1250 wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
)) {
1252 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1253 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1255 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1259 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
1260 struct wpabuf
*resp
, u8 srv_trans_id
)
1262 struct p2p_srv_upnp
*usrv
;
1265 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
1267 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1268 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1272 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1273 struct p2p_srv_upnp
, list
) {
1274 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
1277 /* Length (to be filled) */
1278 len_pos
= wpabuf_put(resp
, 2);
1279 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1280 wpabuf_put_u8(resp
, srv_trans_id
);
1283 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1285 wpabuf_put_u8(resp
, usrv
->version
);
1286 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1288 wpabuf_put_str(resp
, usrv
->service
);
1289 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1295 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
1296 struct wpabuf
*resp
, u8 srv_trans_id
,
1297 const u8
*query
, size_t query_len
)
1299 struct p2p_srv_upnp
*usrv
;
1305 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
1308 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1309 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1310 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
1315 if (query_len
== 0) {
1316 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1320 if (wpabuf_tailroom(resp
) < 5)
1323 /* Length (to be filled) */
1324 len_pos
= wpabuf_put(resp
, 2);
1325 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1326 wpabuf_put_u8(resp
, srv_trans_id
);
1329 str
= os_malloc(query_len
);
1332 os_memcpy(str
, query
+ 1, query_len
- 1);
1333 str
[query_len
- 1] = '\0';
1335 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1336 struct p2p_srv_upnp
, list
) {
1337 if (version
!= usrv
->version
)
1340 if (os_strcmp(str
, "ssdp:all") != 0 &&
1341 os_strstr(usrv
->service
, str
) == NULL
)
1344 if (wpabuf_tailroom(resp
) < 2)
1348 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1350 wpabuf_put_u8(resp
, version
);
1352 wpabuf_put_u8(resp
, ',');
1356 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1358 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
1360 wpabuf_put_str(resp
, usrv
->service
);
1365 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
1368 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1369 /* Response Data: empty */
1372 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1376 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
1377 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
1379 struct wpa_supplicant
*wpa_s
= ctx
;
1380 const u8
*pos
= tlvs
;
1381 const u8
*end
= tlvs
+ tlvs_len
;
1384 struct wpabuf
*resp
;
1385 u8 srv_proto
, srv_trans_id
;
1389 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
1391 buf_len
= 2 * tlvs_len
+ 1;
1392 buf
= os_malloc(buf_len
);
1394 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1395 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
1397 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
1402 if (wpa_s
->p2p_sd_over_ctrl_iface
) {
1403 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
1404 update_indic
, tlvs
, tlvs_len
);
1405 return; /* to be processed by an external program */
1408 resp
= wpabuf_alloc(10000);
1412 while (pos
+ 1 < end
) {
1413 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
1414 slen
= WPA_GET_LE16(pos
);
1416 if (pos
+ slen
> end
|| slen
< 2) {
1417 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
1422 tlv_end
= pos
+ slen
;
1425 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1427 srv_trans_id
= *pos
++;
1428 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1431 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
1432 pos
, tlv_end
- pos
);
1435 if (wpa_s
->force_long_sd
) {
1436 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
1438 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1439 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1443 switch (srv_proto
) {
1444 case P2P_SERV_ALL_SERVICES
:
1445 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
1446 "for all services");
1447 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
1448 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1449 wpa_printf(MSG_DEBUG
, "P2P: No service "
1450 "discovery protocols available");
1451 wpas_sd_add_proto_not_avail(
1452 resp
, P2P_SERV_ALL_SERVICES
,
1456 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1457 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1459 case P2P_SERV_BONJOUR
:
1460 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
1461 pos
, tlv_end
- pos
);
1464 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
1465 pos
, tlv_end
- pos
);
1468 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
1469 "protocol %u", srv_proto
);
1470 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
1479 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
1480 update_indic
, tlvs
, tlvs_len
);
1482 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
1488 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
1489 const u8
*tlvs
, size_t tlvs_len
)
1491 struct wpa_supplicant
*wpa_s
= ctx
;
1492 const u8
*pos
= tlvs
;
1493 const u8
*end
= tlvs
+ tlvs_len
;
1499 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
1501 if (tlvs_len
> 1500) {
1502 /* TODO: better way for handling this */
1503 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1504 P2P_EVENT_SERV_DISC_RESP MACSTR
1505 " %u <long response: %u bytes>",
1506 MAC2STR(sa
), update_indic
,
1507 (unsigned int) tlvs_len
);
1509 buf_len
= 2 * tlvs_len
+ 1;
1510 buf
= os_malloc(buf_len
);
1512 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1513 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1514 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
1515 MAC2STR(sa
), update_indic
, buf
);
1521 u8 srv_proto
, srv_trans_id
, status
;
1523 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
1524 slen
= WPA_GET_LE16(pos
);
1526 if (pos
+ slen
> end
|| slen
< 3) {
1527 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
1531 tlv_end
= pos
+ slen
;
1534 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1536 srv_trans_id
= *pos
++;
1537 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1540 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
1543 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
1544 pos
, tlv_end
- pos
);
1549 wpas_notify_p2p_sd_response(wpa_s
, sa
, update_indic
, tlvs
, tlvs_len
);
1553 u64
wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1554 const struct wpabuf
*tlvs
)
1556 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1557 return wpa_drv_p2p_sd_request(wpa_s
, dst
, tlvs
);
1558 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1560 return (uintptr_t) p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
1564 u64
wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1565 u8 version
, const char *query
)
1567 struct wpabuf
*tlvs
;
1570 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
1573 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
1574 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
1575 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
1576 wpabuf_put_u8(tlvs
, version
);
1577 wpabuf_put_str(tlvs
, query
);
1578 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
1584 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, u64 req
)
1586 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1587 return wpa_drv_p2p_sd_cancel_request(wpa_s
, req
);
1588 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1590 return p2p_sd_cancel_request(wpa_s
->global
->p2p
,
1591 (void *) (uintptr_t) req
);
1595 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
1596 const u8
*dst
, u8 dialog_token
,
1597 const struct wpabuf
*resp_tlvs
)
1599 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1600 wpa_drv_p2p_sd_response(wpa_s
, freq
, dst
, dialog_token
,
1604 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1606 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
1611 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
1613 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
1614 wpa_drv_p2p_service_update(wpa_s
);
1617 if (wpa_s
->global
->p2p
)
1618 p2p_sd_service_update(wpa_s
->global
->p2p
);
1622 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
1624 dl_list_del(&bsrv
->list
);
1625 wpabuf_free(bsrv
->query
);
1626 wpabuf_free(bsrv
->resp
);
1631 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
1633 dl_list_del(&usrv
->list
);
1634 os_free(usrv
->service
);
1639 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
1641 struct p2p_srv_bonjour
*bsrv
, *bn
;
1642 struct p2p_srv_upnp
*usrv
, *un
;
1644 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
1645 struct p2p_srv_bonjour
, list
)
1646 wpas_p2p_srv_bonjour_free(bsrv
);
1648 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
1649 struct p2p_srv_upnp
, list
)
1650 wpas_p2p_srv_upnp_free(usrv
);
1652 wpas_p2p_sd_service_update(wpa_s
);
1656 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
1657 struct wpabuf
*query
, struct wpabuf
*resp
)
1659 struct p2p_srv_bonjour
*bsrv
;
1661 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1664 wpabuf_free(bsrv
->resp
);
1669 bsrv
= os_zalloc(sizeof(*bsrv
));
1672 bsrv
->query
= query
;
1674 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
1676 wpas_p2p_sd_service_update(wpa_s
);
1681 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
1682 const struct wpabuf
*query
)
1684 struct p2p_srv_bonjour
*bsrv
;
1686 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1689 wpas_p2p_srv_bonjour_free(bsrv
);
1690 wpas_p2p_sd_service_update(wpa_s
);
1695 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1696 const char *service
)
1698 struct p2p_srv_upnp
*usrv
;
1700 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
1701 return 0; /* Already listed */
1702 usrv
= os_zalloc(sizeof(*usrv
));
1705 usrv
->version
= version
;
1706 usrv
->service
= os_strdup(service
);
1707 if (usrv
->service
== NULL
) {
1711 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
1713 wpas_p2p_sd_service_update(wpa_s
);
1718 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1719 const char *service
)
1721 struct p2p_srv_upnp
*usrv
;
1723 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
1726 wpas_p2p_srv_upnp_free(usrv
);
1727 wpas_p2p_sd_service_update(wpa_s
);
1732 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
1733 const u8
*peer
, const char *params
,
1734 unsigned int generated_pin
)
1736 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
" %08d%s",
1737 MAC2STR(peer
), generated_pin
, params
);
1741 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
1742 const u8
*peer
, const char *params
)
1744 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
"%s",
1745 MAC2STR(peer
), params
);
1749 void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
1750 const u8
*dev_addr
, const u8
*pri_dev_type
,
1751 const char *dev_name
, u16 supp_config_methods
,
1752 u8 dev_capab
, u8 group_capab
, const u8
*group_id
,
1753 size_t group_id_len
)
1755 struct wpa_supplicant
*wpa_s
= ctx
;
1756 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1758 u8 empty_dev_type
[8];
1759 unsigned int generated_pin
= 0;
1760 struct wpa_supplicant
*group
= NULL
;
1763 for (group
= wpa_s
->global
->ifaces
; group
; group
= group
->next
)
1765 struct wpa_ssid
*s
= group
->current_ssid
;
1767 s
->mode
== WPAS_MODE_P2P_GO
&&
1768 group_id_len
- ETH_ALEN
== s
->ssid_len
&&
1769 os_memcmp(group_id
+ ETH_ALEN
, s
->ssid
,
1775 if (pri_dev_type
== NULL
) {
1776 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
1777 pri_dev_type
= empty_dev_type
;
1779 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
1780 " pri_dev_type=%s name='%s' config_methods=0x%x "
1781 "dev_capab=0x%x group_capab=0x%x%s%s",
1783 wps_dev_type_bin2str(pri_dev_type
, devtype
,
1785 dev_name
, supp_config_methods
, dev_capab
, group_capab
,
1786 group
? " group=" : "",
1787 group
? group
->ifname
: "");
1788 params
[sizeof(params
) - 1] = '\0';
1790 if (config_methods
& WPS_CONFIG_DISPLAY
) {
1791 generated_pin
= wps_generate_pin();
1792 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
1794 } else if (config_methods
& WPS_CONFIG_KEYPAD
)
1795 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
1796 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1797 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1798 "%s", MAC2STR(peer
), params
);
1800 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 1 /* request */,
1801 P2P_PROV_DISC_SUCCESS
,
1802 config_methods
, generated_pin
);
1806 void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
1808 struct wpa_supplicant
*wpa_s
= ctx
;
1809 unsigned int generated_pin
= 0;
1811 if (config_methods
& WPS_CONFIG_DISPLAY
)
1812 wpas_prov_disc_local_keypad(wpa_s
, peer
, "");
1813 else if (config_methods
& WPS_CONFIG_KEYPAD
) {
1814 generated_pin
= wps_generate_pin();
1815 wpas_prov_disc_local_display(wpa_s
, peer
, "", generated_pin
);
1816 } else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1817 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
,
1820 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
1821 P2P_PROV_DISC_SUCCESS
,
1822 config_methods
, generated_pin
);
1824 if (wpa_s
->pending_pd_before_join
&&
1825 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
1826 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
1827 wpa_s
->pending_pd_before_join
= 0;
1828 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
1829 "join-existing-group operation");
1830 wpas_p2p_join_start(wpa_s
);
1835 static void wpas_prov_disc_fail(void *ctx
, const u8
*peer
,
1836 enum p2p_prov_disc_status status
)
1838 struct wpa_supplicant
*wpa_s
= ctx
;
1840 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
1845 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
1846 const u8
*go_dev_addr
, const u8
*ssid
,
1847 size_t ssid_len
, int *go
, u8
*group_bssid
,
1848 int *force_freq
, int persistent_group
)
1850 struct wpa_supplicant
*wpa_s
= ctx
;
1852 u8 cur_bssid
[ETH_ALEN
];
1854 struct wpa_supplicant
*grp
;
1856 if (!persistent_group
) {
1857 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1858 " to join an active group", MAC2STR(sa
));
1859 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
1860 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
1862 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
1863 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
1864 "authorized invitation");
1868 * Do not accept the invitation automatically; notify user and
1871 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1874 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
1876 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
1877 "running persistent group");
1879 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
1883 if (!wpa_s
->conf
->persistent_reconnect
)
1884 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1886 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1887 if (s
->disabled
== 2 &&
1888 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
1889 s
->ssid_len
== ssid_len
&&
1890 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
1895 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1896 " requested reinvocation of an unknown group",
1898 return P2P_SC_FAIL_UNKNOWN_GROUP
;
1901 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
1903 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
1904 wpa_printf(MSG_DEBUG
, "P2P: The only available "
1905 "interface is already in use - reject "
1907 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1909 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
1910 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
1912 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
1914 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
1915 "interface address for the group");
1916 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1918 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
1923 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, cur_bssid
) == 0 &&
1924 wpa_s
->assoc_freq
) {
1925 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
1926 "the channel we are already using");
1927 *force_freq
= wpa_s
->assoc_freq
;
1930 res
= wpa_drv_shared_freq(wpa_s
);
1932 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
1933 "with the channel we are already using on a "
1934 "shared interface");
1938 return P2P_SC_SUCCESS
;
1942 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
1943 const u8
*ssid
, size_t ssid_len
,
1944 const u8
*go_dev_addr
, u8 status
,
1947 struct wpa_supplicant
*wpa_s
= ctx
;
1950 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1951 if (s
->disabled
== 2 &&
1952 s
->ssid_len
== ssid_len
&&
1953 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
1957 if (status
== P2P_SC_SUCCESS
) {
1958 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
1959 " was accepted; op_freq=%d MHz",
1960 MAC2STR(sa
), op_freq
);
1962 wpas_p2p_group_add_persistent(
1963 wpa_s
, s
, s
->mode
== WPAS_MODE_P2P_GO
, 0);
1965 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
1966 wpa_s
->p2p_wps_method
);
1971 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
1972 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
1973 " was rejected (status %u)", MAC2STR(sa
), status
);
1979 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
1980 "sa=" MACSTR
" go_dev_addr=" MACSTR
1981 " bssid=" MACSTR
" unknown-network",
1982 MAC2STR(sa
), MAC2STR(go_dev_addr
),
1985 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
1986 "sa=" MACSTR
" go_dev_addr=" MACSTR
1988 MAC2STR(sa
), MAC2STR(go_dev_addr
));
1993 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
"sa=" MACSTR
1994 " persistent=%d", MAC2STR(sa
), s
->id
);
1998 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
)
2000 struct wpa_supplicant
*wpa_s
= ctx
;
2001 struct wpa_ssid
*ssid
;
2004 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2005 "status=%d " MACSTR
,
2006 status
, MAC2STR(bssid
));
2008 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2009 "status=%d ", status
);
2011 wpas_notify_p2p_invitation_result(wpa_s
, status
, bssid
);
2013 if (wpa_s
->pending_invite_ssid_id
== -1)
2014 return; /* Invitation to active group */
2016 if (status
!= P2P_SC_SUCCESS
) {
2017 wpas_p2p_remove_pending_group_interface(wpa_s
);
2021 ssid
= wpa_config_get_network(wpa_s
->conf
,
2022 wpa_s
->pending_invite_ssid_id
);
2024 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
2025 "data matching with invitation");
2029 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
2030 ssid
->mode
== WPAS_MODE_P2P_GO
, 0);
2034 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
2035 struct p2p_channels
*chan
)
2039 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
2042 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2043 chan
->reg_class
[cla
].reg_class
= 81;
2044 chan
->reg_class
[cla
].channels
= 11;
2045 for (i
= 0; i
< 11; i
++)
2046 chan
->reg_class
[cla
].channel
[i
] = i
+ 1;
2049 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
2052 /* Operating class 115 - 5 GHz, channels 36-48 */
2053 chan
->reg_class
[cla
].reg_class
= 115;
2054 chan
->reg_class
[cla
].channels
= 4;
2055 chan
->reg_class
[cla
].channel
[0] = 36;
2056 chan
->reg_class
[cla
].channel
[1] = 40;
2057 chan
->reg_class
[cla
].channel
[2] = 44;
2058 chan
->reg_class
[cla
].channel
[3] = 48;
2061 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
2064 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2065 chan
->reg_class
[cla
].reg_class
= 124;
2066 chan
->reg_class
[cla
].channels
= 4;
2067 chan
->reg_class
[cla
].channel
[0] = 149;
2068 chan
->reg_class
[cla
].channel
[1] = 153;
2069 chan
->reg_class
[cla
].channel
[2] = 157;
2070 chan
->reg_class
[cla
].channel
[3] = 161;
2073 chan
->reg_classes
= cla
;
2078 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
2080 enum hostapd_hw_mode mode
)
2084 for (i
= 0; i
< num_modes
; i
++) {
2085 if (modes
[i
].mode
== mode
)
2093 static int has_channel(struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
2097 for (i
= 0; i
< mode
->num_channels
; i
++) {
2098 if (mode
->channels
[i
].chan
== chan
) {
2100 *flags
= mode
->channels
[i
].flag
;
2101 return !(mode
->channels
[i
].flag
&
2102 (HOSTAPD_CHAN_DISABLED
|
2103 HOSTAPD_CHAN_PASSIVE_SCAN
|
2104 HOSTAPD_CHAN_NO_IBSS
|
2105 HOSTAPD_CHAN_RADAR
));
2113 struct p2p_oper_class_map
{
2114 enum hostapd_hw_mode mode
;
2119 enum { BW20
, BW40PLUS
, BW40MINUS
} bw
;
2122 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
2123 struct p2p_channels
*chan
)
2125 struct hostapd_hw_modes
*mode
;
2127 struct p2p_oper_class_map op_class
[] = {
2128 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
2129 { HOSTAPD_MODE_IEEE80211G
, 82, 14, 14, 1, BW20
},
2130 #if 0 /* Do not enable HT40 on 2 GHz for now */
2131 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
2132 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
2134 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
2135 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
2136 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
2137 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
2138 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
2139 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
2140 { -1, 0, 0, 0, 0, BW20
}
2143 if (wpa_s
->hw
.modes
== NULL
) {
2144 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
2145 "of all supported channels; assume dualband "
2147 return wpas_p2p_default_channels(wpa_s
, chan
);
2152 for (op
= 0; op_class
[op
].op_class
; op
++) {
2153 struct p2p_oper_class_map
*o
= &op_class
[op
];
2155 struct p2p_reg_class
*reg
= NULL
;
2157 mode
= get_mode(wpa_s
->hw
.modes
, wpa_s
->hw
.num_modes
, o
->mode
);
2160 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
2162 if (!has_channel(mode
, ch
, &flag
))
2164 if (o
->bw
== BW40MINUS
&&
2165 (!(flag
& HOSTAPD_CHAN_HT40MINUS
) ||
2166 !has_channel(mode
, ch
- 4, NULL
)))
2168 if (o
->bw
== BW40PLUS
&&
2169 (!(flag
& HOSTAPD_CHAN_HT40PLUS
) ||
2170 !has_channel(mode
, ch
+ 4, NULL
)))
2173 wpa_printf(MSG_DEBUG
, "P2P: Add operating "
2174 "class %u", o
->op_class
);
2175 reg
= &chan
->reg_class
[cla
];
2177 reg
->reg_class
= o
->op_class
;
2179 reg
->channel
[reg
->channels
] = ch
;
2183 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
2184 reg
->channel
, reg
->channels
);
2188 chan
->reg_classes
= cla
;
2194 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
2197 struct wpa_supplicant
*wpa_s
= ctx
;
2199 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2200 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
2206 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
2210 static int wpas_go_connected(void *ctx
, const u8
*dev_addr
)
2212 struct wpa_supplicant
*wpa_s
= ctx
;
2214 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2215 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
2218 if (ssid
->mode
!= WPAS_MODE_INFRA
)
2220 if (wpa_s
->wpa_state
!= WPA_COMPLETED
)
2222 if (os_memcmp(wpa_s
->go_dev_addr
, dev_addr
, ETH_ALEN
) == 0)
2231 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2232 * @global: Pointer to global data from wpa_supplicant_init()
2233 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2234 * Returns: 0 on success, -1 on failure
2236 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
2238 struct p2p_config p2p
;
2242 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
2248 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2249 struct p2p_params params
;
2251 wpa_printf(MSG_DEBUG
, "P2P: Use driver-based P2P management");
2252 os_memset(¶ms
, 0, sizeof(params
));
2253 params
.dev_name
= wpa_s
->conf
->device_name
;
2254 os_memcpy(params
.pri_dev_type
, wpa_s
->conf
->device_type
,
2256 params
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
2257 os_memcpy(params
.sec_dev_type
,
2258 wpa_s
->conf
->sec_device_type
,
2259 params
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
2261 if (wpa_drv_p2p_set_params(wpa_s
, ¶ms
) < 0)
2267 os_memset(&p2p
, 0, sizeof(p2p
));
2268 p2p
.msg_ctx
= wpa_s
;
2270 p2p
.p2p_scan
= wpas_p2p_scan
;
2271 p2p
.send_action
= wpas_send_action
;
2272 p2p
.send_action_done
= wpas_send_action_done
;
2273 p2p
.go_neg_completed
= wpas_go_neg_completed
;
2274 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
2275 p2p
.dev_found
= wpas_dev_found
;
2276 p2p
.dev_lost
= wpas_dev_lost
;
2277 p2p
.start_listen
= wpas_start_listen
;
2278 p2p
.stop_listen
= wpas_stop_listen
;
2279 p2p
.send_probe_resp
= wpas_send_probe_resp
;
2280 p2p
.sd_request
= wpas_sd_request
;
2281 p2p
.sd_response
= wpas_sd_response
;
2282 p2p
.prov_disc_req
= wpas_prov_disc_req
;
2283 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
2284 p2p
.prov_disc_fail
= wpas_prov_disc_fail
;
2285 p2p
.invitation_process
= wpas_invitation_process
;
2286 p2p
.invitation_received
= wpas_invitation_received
;
2287 p2p
.invitation_result
= wpas_invitation_result
;
2288 p2p
.get_noa
= wpas_get_noa
;
2289 p2p
.go_connected
= wpas_go_connected
;
2291 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2292 os_memcpy(p2p
.dev_addr
, wpa_s
->global
->p2p_dev_addr
, ETH_ALEN
);
2293 p2p
.dev_name
= wpa_s
->conf
->device_name
;
2294 p2p
.manufacturer
= wpa_s
->conf
->manufacturer
;
2295 p2p
.model_name
= wpa_s
->conf
->model_name
;
2296 p2p
.model_number
= wpa_s
->conf
->model_number
;
2297 p2p
.serial_number
= wpa_s
->conf
->serial_number
;
2299 os_memcpy(p2p
.uuid
, wpa_s
->wps
->uuid
, 16);
2300 p2p
.config_methods
= wpa_s
->wps
->config_methods
;
2303 if (wpa_s
->conf
->p2p_listen_reg_class
&&
2304 wpa_s
->conf
->p2p_listen_channel
) {
2305 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
2306 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
2310 * Pick one of the social channels randomly as the listen
2313 os_get_random((u8
*) &r
, sizeof(r
));
2314 p2p
.channel
= 1 + (r
% 3) * 5;
2316 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d", p2p
.channel
);
2318 if (wpa_s
->conf
->p2p_oper_reg_class
&&
2319 wpa_s
->conf
->p2p_oper_channel
) {
2320 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
2321 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
2322 p2p
.cfg_op_channel
= 1;
2323 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
2324 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2327 p2p
.op_reg_class
= 81;
2329 * Use random operation channel from (1, 6, 11) if no other
2330 * preference is indicated.
2332 os_get_random((u8
*) &r
, sizeof(r
));
2333 p2p
.op_channel
= 1 + (r
% 3) * 5;
2334 p2p
.cfg_op_channel
= 0;
2335 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
2336 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2338 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
2339 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
2340 p2p
.country
[2] = 0x04;
2342 os_memcpy(p2p
.country
, "XX\x04", 3);
2344 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
)) {
2345 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
2350 os_memcpy(p2p
.pri_dev_type
, wpa_s
->conf
->device_type
,
2353 p2p
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
2354 os_memcpy(p2p
.sec_dev_type
, wpa_s
->conf
->sec_device_type
,
2355 p2p
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
2357 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
2358 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
2360 p2p
.max_peers
= 100;
2362 if (wpa_s
->conf
->p2p_ssid_postfix
) {
2363 p2p
.ssid_postfix_len
=
2364 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
2365 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
2366 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
2367 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
2368 p2p
.ssid_postfix_len
);
2371 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
2373 global
->p2p
= p2p_init(&p2p
);
2374 if (global
->p2p
== NULL
)
2377 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
2378 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
2380 p2p_add_wps_vendor_extension(
2381 global
->p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
2389 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2390 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2392 * This function deinitialize per-interface P2P data.
2394 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
2396 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
2397 wpa_drv_probe_req_report(wpa_s
, 0);
2399 if (wpa_s
->go_params
) {
2400 /* Clear any stored provisioning info */
2401 p2p_clear_provisioning_info(
2403 wpa_s
->go_params
->peer_interface_addr
);
2406 os_free(wpa_s
->go_params
);
2407 wpa_s
->go_params
= NULL
;
2408 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2409 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2410 wpa_s
->p2p_long_listen
= 0;
2411 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
2412 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
2413 wpas_p2p_remove_pending_group_interface(wpa_s
);
2415 /* TODO: remove group interface from the driver if this wpa_s instance
2416 * is on top of a P2P group interface */
2421 * wpas_p2p_deinit_global - Deinitialize global P2P module
2422 * @global: Pointer to global data from wpa_supplicant_init()
2424 * This function deinitializes the global (per device) P2P module.
2426 void wpas_p2p_deinit_global(struct wpa_global
*global
)
2428 struct wpa_supplicant
*wpa_s
, *tmp
;
2431 if (global
->p2p
== NULL
)
2434 /* Remove remaining P2P group interfaces */
2435 wpa_s
= global
->ifaces
;
2437 wpas_p2p_service_flush(wpa_s
);
2438 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
2439 wpa_s
= wpa_s
->next
;
2441 enum wpa_driver_if_type type
;
2442 tmp
= global
->ifaces
;
2445 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
2450 ifname
= os_strdup(tmp
->ifname
);
2451 type
= wpas_p2p_if_type(tmp
->p2p_group_interface
);
2452 wpa_supplicant_remove_iface(global
, tmp
, 0);
2454 wpa_drv_if_remove(wpa_s
, type
, ifname
);
2459 * Deinit GO data on any possibly remaining interface (if main
2460 * interface is used as GO).
2462 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2463 if (wpa_s
->ap_iface
)
2464 wpas_p2p_group_deinit(wpa_s
);
2467 p2p_deinit(global
->p2p
);
2472 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
2474 if (wpa_s
->drv_flags
&
2475 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
2476 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
2477 return 1; /* P2P group requires a new interface in every case
2479 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
2480 return 0; /* driver does not support concurrent operations */
2481 if (wpa_s
->global
->ifaces
->next
)
2482 return 1; /* more that one interface already in use */
2483 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
2484 return 1; /* this interface is already in use */
2489 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
2490 const u8
*peer_addr
,
2491 enum p2p_wps_method wps_method
,
2492 int go_intent
, const u8
*own_interface_addr
,
2493 unsigned int force_freq
, int persistent_group
)
2495 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
2496 persistent_group
= 2;
2498 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2499 return wpa_drv_p2p_connect(wpa_s
, peer_addr
, wps_method
,
2500 go_intent
, own_interface_addr
,
2501 force_freq
, persistent_group
);
2504 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2505 go_intent
, own_interface_addr
, force_freq
,
2510 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
2511 const u8
*peer_addr
,
2512 enum p2p_wps_method wps_method
,
2513 int go_intent
, const u8
*own_interface_addr
,
2514 unsigned int force_freq
, int persistent_group
)
2516 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
2517 persistent_group
= 2;
2519 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2522 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2523 go_intent
, own_interface_addr
, force_freq
,
2528 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
2530 wpa_s
->p2p_join_scan_count
++;
2531 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
2532 wpa_s
->p2p_join_scan_count
);
2533 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
2534 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
2535 " for join operationg - stop join attempt",
2536 MAC2STR(wpa_s
->pending_join_iface_addr
));
2537 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2538 wpa_msg(wpa_s
->parent
, MSG_INFO
,
2539 P2P_EVENT_GROUP_FORMATION_FAILURE
);
2544 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
2545 struct wpa_scan_results
*scan_res
)
2547 struct wpa_bss
*bss
;
2549 u8 iface_addr
[ETH_ALEN
];
2551 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2553 if (wpa_s
->global
->p2p_disabled
)
2556 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for join",
2557 scan_res
? (int) scan_res
->num
: -1);
2560 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
2562 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2563 wpa_s
->pending_join_iface_addr
);
2565 p2p_get_interface_addr(wpa_s
->global
->p2p
,
2566 wpa_s
->pending_join_dev_addr
,
2568 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0)
2570 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
2571 "address for join from " MACSTR
" to " MACSTR
2572 " based on newly discovered P2P peer entry",
2573 MAC2STR(wpa_s
->pending_join_iface_addr
),
2574 MAC2STR(iface_addr
));
2575 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
2578 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2579 wpa_s
->pending_join_iface_addr
);
2582 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2583 "from P2P peer table: %d MHz", freq
);
2585 bss
= wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
);
2588 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2589 "from BSS table: %d MHz", freq
);
2594 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
2595 "prior to joining an existing group (GO " MACSTR
2597 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
2598 wpa_s
->pending_pd_before_join
= 1;
2600 switch (wpa_s
->pending_join_wps_method
) {
2601 case WPS_PIN_DISPLAY
:
2602 method
= WPS_CONFIG_KEYPAD
;
2604 case WPS_PIN_KEYPAD
:
2605 method
= WPS_CONFIG_DISPLAY
;
2608 method
= WPS_CONFIG_PUSHBUTTON
;
2615 if ((p2p_get_provisioning_info(wpa_s
->global
->p2p
,
2616 wpa_s
->pending_join_dev_addr
) ==
2619 * We have already performed provision discovery for
2620 * joining the group. Proceed directly to join
2621 * operation without duplicated provision discovery. */
2622 wpa_printf(MSG_DEBUG
, "P2P: Provision discovery "
2623 "with " MACSTR
" already done - proceed to "
2625 MAC2STR(wpa_s
->pending_join_dev_addr
));
2626 wpa_s
->pending_pd_before_join
= 0;
2630 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
2631 wpa_s
->pending_join_dev_addr
, method
, 1,
2633 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
2634 "Discovery Request before joining an "
2636 wpa_s
->pending_pd_before_join
= 0;
2641 * Actual join operation will be started from the Action frame
2642 * TX status callback.
2647 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
2648 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2649 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2650 wpas_p2p_check_join_scan_limit(wpa_s
);
2654 /* Start join operation immediately */
2655 wpas_p2p_join_start(wpa_s
);
2659 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
2661 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
2663 struct wpa_driver_scan_params params
;
2664 struct wpabuf
*wps_ie
, *ies
;
2667 os_memset(¶ms
, 0, sizeof(params
));
2669 /* P2P Wildcard SSID */
2670 params
.num_ssids
= 1;
2671 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
2672 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
2674 wpa_s
->wps
->dev
.p2p
= 1;
2675 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
2676 WPS_REQ_ENROLLEE
, 0, NULL
);
2677 if (wps_ie
== NULL
) {
2678 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2682 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
2683 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
2685 wpabuf_free(wps_ie
);
2686 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2689 wpabuf_put_buf(ies
, wps_ie
);
2690 wpabuf_free(wps_ie
);
2692 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
2694 params
.p2p_probe
= 1;
2695 params
.extra_ies
= wpabuf_head(ies
);
2696 params
.extra_ies_len
= wpabuf_len(ies
);
2699 * Run a scan to update BSS table and start Provision Discovery once
2700 * the new scan results become available.
2702 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
2703 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
2708 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
2710 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2711 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2712 wpas_p2p_check_join_scan_limit(wpa_s
);
2717 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
2718 const u8
*dev_addr
, enum p2p_wps_method wps_method
)
2720 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
2721 MACSTR
" dev " MACSTR
")",
2722 MAC2STR(iface_addr
), MAC2STR(dev_addr
));
2724 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
2725 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
2726 wpa_s
->pending_join_wps_method
= wps_method
;
2728 /* Make sure we are not running find during connection establishment */
2729 wpas_p2p_stop_find(wpa_s
);
2731 wpa_s
->p2p_join_scan_count
= 0;
2732 wpas_p2p_join_scan(wpa_s
, NULL
);
2737 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
2739 struct wpa_supplicant
*group
;
2740 struct p2p_go_neg_results res
;
2742 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
2745 if (group
!= wpa_s
) {
2746 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
2747 sizeof(group
->p2p_pin
));
2748 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
2751 group
->p2p_in_provisioning
= 1;
2753 os_memset(&res
, 0, sizeof(res
));
2754 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
2756 res
.wps_method
= wpa_s
->pending_join_wps_method
;
2757 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
2758 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel prior to "
2760 wpa_drv_cancel_remain_on_channel(wpa_s
);
2761 wpa_s
->off_channel_freq
= 0;
2762 wpa_s
->roc_waiting_drv_freq
= 0;
2764 wpas_start_wps_enrollee(group
, &res
);
2767 * Allow a longer timeout for join-a-running-group than normal 15
2768 * second group formation timeout since the GO may not have authorized
2769 * our connection yet.
2771 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2772 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
2780 * wpas_p2p_connect - Request P2P Group Formation to be started
2781 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2782 * @peer_addr: Address of the peer P2P Device
2783 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2784 * @persistent_group: Whether to create a persistent group
2785 * @join: Whether to join an existing group (as a client) instead of starting
2786 * Group Owner negotiation; @peer_addr is BSSID in that case
2787 * @auth: Whether to only authorize the connection instead of doing that and
2788 * initiating Group Owner negotiation
2789 * @go_intent: GO Intent or -1 to use default
2790 * @freq: Frequency for the group or 0 for auto-selection
2791 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2792 * failure, -2 on failure due to channel not currently available,
2793 * -3 if forced channel is not supported
2795 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
2796 const char *pin
, enum p2p_wps_method wps_method
,
2797 int persistent_group
, int join
, int auth
, int go_intent
,
2800 int force_freq
= 0, oper_freq
= 0;
2803 enum wpa_driver_if_type iftype
;
2806 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2810 go_intent
= wpa_s
->conf
->p2p_go_intent
;
2813 wpa_s
->p2p_long_listen
= 0;
2815 wpa_s
->p2p_wps_method
= wps_method
;
2818 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
2819 else if (wps_method
== WPS_PIN_DISPLAY
) {
2820 ret
= wps_generate_pin();
2821 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
2823 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
2826 wpa_s
->p2p_pin
[0] = '\0';
2829 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
2831 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
2832 "connect a running group from " MACSTR
,
2833 MAC2STR(peer_addr
));
2834 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
2837 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
2838 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
2840 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
2841 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
2844 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
) <
2850 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
2852 oper_freq
= wpa_s
->assoc_freq
;
2854 oper_freq
= wpa_drv_shared_freq(wpa_s
);
2860 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
2861 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
2862 "(%u MHz) is not supported for P2P uses",
2867 if (oper_freq
> 0 && freq
!= oper_freq
&&
2868 !(wpa_s
->drv_flags
&
2869 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2870 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2871 "on %u MHz while connected on another "
2872 "channel (%u MHz)", freq
, oper_freq
);
2875 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2876 "requested channel (%u MHz)", freq
);
2878 } else if (oper_freq
> 0 &&
2879 !p2p_supported_freq(wpa_s
->global
->p2p
, oper_freq
)) {
2880 if (!(wpa_s
->drv_flags
&
2881 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2882 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2883 "while connected on non-P2P supported "
2884 "channel (%u MHz)", oper_freq
);
2887 wpa_printf(MSG_DEBUG
, "P2P: Current operating channel "
2888 "(%u MHz) not available for P2P - try to use "
2889 "another channel", oper_freq
);
2891 } else if (oper_freq
> 0) {
2892 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2893 "channel we are already using (%u MHz) on another "
2894 "interface", oper_freq
);
2895 force_freq
= oper_freq
;
2898 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
2900 if (wpa_s
->create_p2p_iface
) {
2901 /* Prepare to add a new interface for the group */
2902 iftype
= WPA_IF_P2P_GROUP
;
2903 if (go_intent
== 15)
2904 iftype
= WPA_IF_P2P_GO
;
2905 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
2906 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2907 "interface for the group");
2911 if_addr
= wpa_s
->pending_interface_addr
;
2913 if_addr
= wpa_s
->own_addr
;
2916 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2918 force_freq
, persistent_group
) < 0)
2923 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
2924 go_intent
, if_addr
, force_freq
,
2925 persistent_group
) < 0) {
2926 if (wpa_s
->create_p2p_iface
)
2927 wpas_p2p_remove_pending_group_interface(wpa_s
);
2935 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2936 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2937 * @freq: Frequency of the channel in MHz
2938 * @duration: Duration of the stay on the channel in milliseconds
2940 * This callback is called when the driver indicates that it has started the
2941 * requested remain-on-channel duration.
2943 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
2944 unsigned int freq
, unsigned int duration
)
2946 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2948 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
2949 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
2950 wpa_s
->pending_listen_duration
);
2951 wpa_s
->pending_listen_freq
= 0;
2956 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
2957 unsigned int timeout
)
2959 /* Limit maximum Listen state time based on driver limitation. */
2960 if (timeout
> wpa_s
->max_remain_on_chan
)
2961 timeout
= wpa_s
->max_remain_on_chan
;
2963 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2964 return wpa_drv_p2p_listen(wpa_s
, timeout
);
2966 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
2971 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2972 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2973 * @freq: Frequency of the channel in MHz
2975 * This callback is called when the driver indicates that a remain-on-channel
2976 * operation has been completed, i.e., the duration on the requested channel
2979 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
2982 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
2983 "(p2p_long_listen=%d ms pending_action_tx=%p)",
2984 wpa_s
->p2p_long_listen
, wpa_s
->pending_action_tx
);
2985 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2987 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
2988 return; /* P2P module started a new operation */
2989 if (wpa_s
->pending_action_tx
)
2991 if (wpa_s
->p2p_long_listen
> 0)
2992 wpa_s
->p2p_long_listen
-= wpa_s
->max_remain_on_chan
;
2993 if (wpa_s
->p2p_long_listen
> 0) {
2994 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
2995 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
);
3001 * wpas_p2p_group_remove - Remove a P2P group
3002 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3003 * @ifname: Network interface name of the group interface or "*" to remove all
3005 * Returns: 0 on success, -1 on failure
3007 * This function is used to remove a P2P group. This can be used to disconnect
3008 * from a group in which the local end is a P2P Client or to end a P2P Group in
3009 * case the local end is the Group Owner. If a virtual network interface was
3010 * created for this group, that interface will be removed. Otherwise, only the
3011 * configured P2P group network will be removed from the interface.
3013 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
3015 struct wpa_global
*global
= wpa_s
->global
;
3017 if (os_strcmp(ifname
, "*") == 0) {
3018 struct wpa_supplicant
*prev
;
3019 wpa_s
= global
->ifaces
;
3022 wpa_s
= wpa_s
->next
;
3023 wpas_p2p_disconnect(prev
);
3028 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3029 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3033 return wpas_p2p_disconnect(wpa_s
);
3037 static int wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
3038 struct p2p_go_neg_results
*params
,
3044 os_memset(params
, 0, sizeof(*params
));
3045 params
->role_go
= 1;
3047 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
3048 "frequency %d MHz", freq
);
3049 params
->freq
= freq
;
3050 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
3051 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
3052 wpa_s
->conf
->p2p_oper_channel
<= 11) {
3053 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3054 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3055 "frequency %d MHz", params
->freq
);
3056 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
3057 wpa_s
->conf
->p2p_oper_reg_class
== 124) {
3058 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3059 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3060 "frequency %d MHz", params
->freq
);
3061 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3062 wpa_s
->best_overall_freq
> 0 &&
3063 p2p_supported_freq(wpa_s
->global
->p2p
,
3064 wpa_s
->best_overall_freq
)) {
3065 params
->freq
= wpa_s
->best_overall_freq
;
3066 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
3067 "channel %d MHz", params
->freq
);
3068 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3069 wpa_s
->best_24_freq
> 0 &&
3070 p2p_supported_freq(wpa_s
->global
->p2p
,
3071 wpa_s
->best_24_freq
)) {
3072 params
->freq
= wpa_s
->best_24_freq
;
3073 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
3074 "channel %d MHz", params
->freq
);
3075 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3076 wpa_s
->best_5_freq
> 0 &&
3077 p2p_supported_freq(wpa_s
->global
->p2p
,
3078 wpa_s
->best_5_freq
)) {
3079 params
->freq
= wpa_s
->best_5_freq
;
3080 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
3081 "channel %d MHz", params
->freq
);
3083 params
->freq
= 2412;
3084 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference "
3085 "known)", params
->freq
);
3088 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
3089 wpa_s
->assoc_freq
&& !freq
) {
3090 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3092 params
->freq
= wpa_s
->assoc_freq
;
3095 res
= wpa_drv_shared_freq(wpa_s
);
3096 if (res
> 0 && !freq
) {
3097 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
3098 "already using on a shared interface");
3100 } else if (res
> 0 && freq
!= res
&&
3101 !(wpa_s
->drv_flags
&
3102 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
3103 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group on %u MHz "
3104 "while connected on another channel (%u MHz)",
3113 static struct wpa_supplicant
*
3114 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
3117 struct wpa_supplicant
*group_wpa_s
;
3119 if (!wpas_p2p_create_iface(wpa_s
))
3122 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
3123 WPA_IF_P2P_CLIENT
) < 0)
3125 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
3126 if (group_wpa_s
== NULL
) {
3127 wpas_p2p_remove_pending_group_interface(wpa_s
);
3136 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3137 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3138 * @persistent_group: Whether to create a persistent group
3139 * @freq: Frequency for the group or 0 to indicate no hardcoding
3140 * Returns: 0 on success, -1 on failure
3142 * This function creates a new P2P group with the local end as the Group Owner,
3143 * i.e., without using Group Owner Negotiation.
3145 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
3148 struct p2p_go_neg_results params
;
3151 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3154 /* Make sure we are not running find during connection establishment */
3155 wpa_printf(MSG_DEBUG
, "P2P: Stop any on-going P2P FIND");
3156 wpas_p2p_stop_find(wpa_s
);
3159 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
3161 if (wpa_s
->best_24_freq
> 0 &&
3162 p2p_supported_freq(wpa_s
->global
->p2p
,
3163 wpa_s
->best_24_freq
)) {
3164 freq
= wpa_s
->best_24_freq
;
3165 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
3166 "channel: %d MHz", freq
);
3168 os_get_random((u8
*) &r
, sizeof(r
));
3169 freq
= 2412 + (r
% 3) * 25;
3170 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
3171 "channel: %d MHz", freq
);
3176 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
3178 if (wpa_s
->best_5_freq
> 0 &&
3179 p2p_supported_freq(wpa_s
->global
->p2p
,
3180 wpa_s
->best_5_freq
)) {
3181 freq
= wpa_s
->best_5_freq
;
3182 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
3183 "channel: %d MHz", freq
);
3185 os_get_random((u8
*) &r
, sizeof(r
));
3186 freq
= 5180 + (r
% 4) * 20;
3187 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3188 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
3189 "5 GHz channel for P2P group");
3192 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
3193 "channel: %d MHz", freq
);
3197 if (freq
> 0 && !p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3198 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
3199 "(%u MHz) is not supported for P2P uses",
3204 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
))
3206 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
3207 params
.persistent_group
= persistent_group
;
3209 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
3212 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3218 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
3219 struct wpa_ssid
*params
, int addr_allocated
)
3221 struct wpa_ssid
*ssid
;
3223 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
3227 wpa_supplicant_ap_deinit(wpa_s
);
3229 ssid
= wpa_config_add_network(wpa_s
->conf
);
3232 wpa_config_set_network_defaults(ssid
);
3233 ssid
->temporary
= 1;
3234 ssid
->proto
= WPA_PROTO_RSN
;
3235 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
3236 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
3237 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
3238 ssid
->ssid
= os_malloc(params
->ssid_len
);
3239 if (ssid
->ssid
== NULL
) {
3240 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
3243 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
3244 ssid
->ssid_len
= params
->ssid_len
;
3245 ssid
->p2p_group
= 1;
3246 ssid
->export_keys
= 1;
3247 if (params
->psk_set
) {
3248 os_memcpy(ssid
->psk
, params
->psk
, 32);
3251 if (params
->passphrase
)
3252 ssid
->passphrase
= os_strdup(params
->passphrase
);
3254 wpa_supplicant_select_network(wpa_s
, ssid
);
3256 wpa_s
->show_group_started
= 1;
3262 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
3263 struct wpa_ssid
*ssid
, int addr_allocated
,
3266 struct p2p_go_neg_results params
;
3269 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
3272 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
3273 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
3274 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
3279 /* Make sure we are not running find during connection establishment */
3280 wpas_p2p_stop_find(wpa_s
);
3282 if (ssid
->mode
== WPAS_MODE_INFRA
)
3283 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
3285 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
3288 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
))
3292 if (ssid
->passphrase
== NULL
||
3293 os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
3294 wpa_printf(MSG_DEBUG
, "P2P: Invalid passphrase in persistent "
3298 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
3299 sizeof(params
.passphrase
));
3300 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
3301 params
.ssid_len
= ssid
->ssid_len
;
3302 params
.persistent_group
= 1;
3304 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
3308 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3314 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
3315 struct wpabuf
*proberesp_ies
)
3317 struct wpa_supplicant
*wpa_s
= ctx
;
3318 if (wpa_s
->ap_iface
) {
3319 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
3320 if (!(hapd
->conf
->p2p
& P2P_GROUP_OWNER
)) {
3321 wpabuf_free(beacon_ies
);
3322 wpabuf_free(proberesp_ies
);
3326 wpabuf_free(hapd
->p2p_beacon_ie
);
3327 hapd
->p2p_beacon_ie
= beacon_ies
;
3329 wpabuf_free(hapd
->p2p_probe_resp_ie
);
3330 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
3332 wpabuf_free(beacon_ies
);
3333 wpabuf_free(proberesp_ies
);
3335 wpa_supplicant_ap_update_beacon(wpa_s
);
3339 static void wpas_p2p_idle_update(void *ctx
, int idle
)
3341 struct wpa_supplicant
*wpa_s
= ctx
;
3342 if (!wpa_s
->ap_iface
)
3344 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
3346 wpas_p2p_set_group_idle_timeout(wpa_s
);
3348 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3352 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
3353 int persistent_group
,
3354 int group_formation
)
3356 struct p2p_group
*group
;
3357 struct p2p_group_config
*cfg
;
3359 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3361 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3364 cfg
= os_zalloc(sizeof(*cfg
));
3368 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
3369 cfg
->persistent_group
= 2;
3370 else if (persistent_group
)
3371 cfg
->persistent_group
= 1;
3372 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
3373 if (wpa_s
->max_stations
&&
3374 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
3375 cfg
->max_clients
= wpa_s
->max_stations
;
3377 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
3378 cfg
->cb_ctx
= wpa_s
;
3379 cfg
->ie_update
= wpas_p2p_ie_update
;
3380 cfg
->idle_update
= wpas_p2p_idle_update
;
3382 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
3385 if (!group_formation
)
3386 p2p_group_notif_formation_done(group
);
3387 wpa_s
->p2p_group
= group
;
3392 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3395 if (!wpa_s
->p2p_in_provisioning
) {
3396 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
3397 "provisioning not in progress");
3401 /* Clear any stored provisioning info */
3402 p2p_clear_provisioning_info(wpa_s
->global
->p2p
, peer_addr
);
3404 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
3406 if (wpa_s
->global
->p2p
)
3407 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
3408 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3409 wpa_drv_wps_success_cb(wpa_s
, peer_addr
);
3410 wpas_group_formation_completed(wpa_s
, 1);
3414 void wpas_p2p_wps_failed(struct wpa_supplicant
*wpa_s
,
3415 struct wps_event_fail
*fail
)
3417 if (!wpa_s
->p2p_in_provisioning
) {
3418 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS fail event - P2P "
3419 "provisioning not in progress");
3423 if (wpa_s
->go_params
) {
3424 p2p_clear_provisioning_info(
3426 wpa_s
->go_params
->peer_interface_addr
);
3429 wpas_notify_p2p_wps_failed(wpa_s
, fail
);
3433 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3434 const char *config_method
, int join
)
3438 if (os_strncmp(config_method
, "display", 7) == 0)
3439 config_methods
= WPS_CONFIG_DISPLAY
;
3440 else if (os_strncmp(config_method
, "keypad", 6) == 0)
3441 config_methods
= WPS_CONFIG_KEYPAD
;
3442 else if (os_strncmp(config_method
, "pbc", 3) == 0 ||
3443 os_strncmp(config_method
, "pushbutton", 10) == 0)
3444 config_methods
= WPS_CONFIG_PUSHBUTTON
;
3446 wpa_printf(MSG_DEBUG
, "P2P: Unknown config method");
3450 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3451 return wpa_drv_p2p_prov_disc_req(wpa_s
, peer_addr
,
3452 config_methods
, join
);
3455 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
3458 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
3459 config_methods
, join
, 0);
3463 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
3466 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
3470 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
3472 if (!wpa_s
->pending_action_tx
)
3475 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
3476 "operation request");
3477 wpabuf_free(wpa_s
->pending_action_tx
);
3478 wpa_s
->pending_action_tx
= NULL
;
3482 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
3483 enum p2p_discovery_type type
,
3484 unsigned int num_req_dev_types
, const u8
*req_dev_types
,
3487 wpas_p2p_clear_pending_action_tx(wpa_s
);
3488 wpa_s
->p2p_long_listen
= 0;
3490 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3491 return wpa_drv_p2p_find(wpa_s
, timeout
, type
);
3493 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3496 wpa_supplicant_cancel_sched_scan(wpa_s
);
3498 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
,
3499 num_req_dev_types
, req_dev_types
, dev_id
);
3503 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
3505 wpas_p2p_clear_pending_action_tx(wpa_s
);
3506 wpa_s
->p2p_long_listen
= 0;
3507 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3508 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3509 wpa_s
->p2p_cb_on_scan_complete
= 0;
3511 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3512 wpa_drv_p2p_stop_find(wpa_s
);
3516 if (wpa_s
->global
->p2p
)
3517 p2p_stop_find(wpa_s
->global
->p2p
);
3519 wpas_p2p_remove_pending_group_interface(wpa_s
);
3523 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
3525 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3526 wpa_s
->p2p_long_listen
= 0;
3530 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
3534 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3537 wpa_supplicant_cancel_sched_scan(wpa_s
);
3538 wpas_p2p_clear_pending_action_tx(wpa_s
);
3542 * This is a request for unlimited Listen state. However, at
3543 * least for now, this is mapped to a Listen state for one
3548 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3549 wpa_s
->p2p_long_listen
= 0;
3552 * Stop previous find/listen operation to avoid trying to request a new
3553 * remain-on-channel operation while the driver is still running the
3556 if (wpa_s
->global
->p2p
)
3557 p2p_stop_find(wpa_s
->global
->p2p
);
3559 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
3560 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
3561 wpa_s
->p2p_long_listen
= timeout
* 1000;
3562 eloop_register_timeout(timeout
, 0,
3563 wpas_p2p_long_listen_timeout
,
3571 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
3572 u8
*buf
, size_t len
, int p2p_group
)
3574 struct wpabuf
*p2p_ie
;
3577 if (wpa_s
->global
->p2p_disabled
)
3579 if (wpa_s
->global
->p2p
== NULL
)
3584 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
3585 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
3587 wpabuf_free(p2p_ie
);
3593 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
3594 const u8
*dst
, const u8
*bssid
,
3595 const u8
*ie
, size_t ie_len
)
3597 if (wpa_s
->global
->p2p_disabled
)
3599 if (wpa_s
->global
->p2p
== NULL
)
3602 return p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, dst
, bssid
,
3607 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
3608 const u8
*sa
, const u8
*bssid
,
3609 u8 category
, const u8
*data
, size_t len
, int freq
)
3611 if (wpa_s
->global
->p2p_disabled
)
3613 if (wpa_s
->global
->p2p
== NULL
)
3616 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
3621 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
3623 if (wpa_s
->global
->p2p_disabled
)
3625 if (wpa_s
->global
->p2p
== NULL
)
3628 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
3632 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
3634 p2p_group_deinit(wpa_s
->p2p_group
);
3635 wpa_s
->p2p_group
= NULL
;
3637 wpa_s
->ap_configured_cb
= NULL
;
3638 wpa_s
->ap_configured_cb_ctx
= NULL
;
3639 wpa_s
->ap_configured_cb_data
= NULL
;
3640 wpa_s
->connect_without_scan
= NULL
;
3644 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
3646 wpa_s
->p2p_long_listen
= 0;
3648 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3649 return wpa_drv_p2p_reject(wpa_s
, addr
);
3651 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3654 return p2p_reject(wpa_s
->global
->p2p
, addr
);
3658 /* Invite to reinvoke a persistent group */
3659 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3660 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
)
3662 enum p2p_invite_role role
;
3665 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3666 role
= P2P_INVITE_ROLE_GO
;
3667 if (peer_addr
== NULL
) {
3668 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
3669 "address in invitation command");
3672 if (wpas_p2p_create_iface(wpa_s
)) {
3673 if (wpas_p2p_add_group_interface(wpa_s
,
3674 WPA_IF_P2P_GO
) < 0) {
3675 wpa_printf(MSG_ERROR
, "P2P: Failed to "
3676 "allocate a new interface for the "
3680 bssid
= wpa_s
->pending_interface_addr
;
3682 bssid
= wpa_s
->own_addr
;
3684 role
= P2P_INVITE_ROLE_CLIENT
;
3685 peer_addr
= ssid
->bssid
;
3687 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
3689 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3690 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3691 ssid
->ssid
, ssid
->ssid_len
,
3694 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3697 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3698 ssid
->ssid
, ssid
->ssid_len
, 0, go_dev_addr
, 1);
3702 /* Invite to join an active group */
3703 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
3704 const u8
*peer_addr
, const u8
*go_dev_addr
)
3706 struct wpa_global
*global
= wpa_s
->global
;
3707 enum p2p_invite_role role
;
3709 struct wpa_ssid
*ssid
;
3711 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3712 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3715 if (wpa_s
== NULL
) {
3716 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
3720 ssid
= wpa_s
->current_ssid
;
3722 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
3727 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3728 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
3729 bssid
= wpa_s
->own_addr
;
3730 if (go_dev_addr
== NULL
)
3731 go_dev_addr
= wpa_s
->global
->p2p_dev_addr
;
3733 role
= P2P_INVITE_ROLE_CLIENT
;
3734 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
3735 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
3736 "invite to current group");
3739 bssid
= wpa_s
->bssid
;
3740 if (go_dev_addr
== NULL
&&
3741 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
3742 go_dev_addr
= wpa_s
->go_dev_addr
;
3744 wpa_s
->parent
->pending_invite_ssid_id
= -1;
3746 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3747 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
3748 ssid
->ssid
, ssid
->ssid_len
,
3751 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3754 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3755 ssid
->ssid
, ssid
->ssid_len
, wpa_s
->assoc_freq
,
3760 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
3762 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
3763 const char *ssid_txt
;
3764 u8 go_dev_addr
[ETH_ALEN
];
3765 int network_id
= -1;
3769 if (!wpa_s
->show_group_started
|| !ssid
)
3772 wpa_s
->show_group_started
= 0;
3774 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
3775 os_memset(go_dev_addr
, 0, ETH_ALEN
);
3776 if (ssid
->bssid_set
)
3777 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
3778 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
3780 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
3782 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
3783 wpa_s
->global
->p2p_group_formation
= NULL
;
3785 freq
= wpa_s
->current_bss
? wpa_s
->current_bss
->freq
:
3786 (int) wpa_s
->assoc_freq
;
3787 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
3789 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
3790 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3791 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3793 wpa_s
->ifname
, ssid_txt
, freq
, psk
,
3794 MAC2STR(go_dev_addr
),
3795 persistent
? " [PERSISTENT]" : "");
3797 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3798 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3799 "go_dev_addr=" MACSTR
"%s",
3800 wpa_s
->ifname
, ssid_txt
, freq
,
3801 ssid
->passphrase
? ssid
->passphrase
: "",
3802 MAC2STR(go_dev_addr
),
3803 persistent
? " [PERSISTENT]" : "");
3807 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
3810 network_id
= ssid
->id
;
3811 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 1);
3815 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
3816 u32 interval1
, u32 duration2
, u32 interval2
)
3818 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3820 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3823 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
3824 wpa_s
->current_ssid
== NULL
||
3825 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
3828 return p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
3829 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
3830 duration1
, interval1
, duration2
, interval2
);
3834 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
3835 unsigned int interval
)
3837 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3840 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3843 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
3847 static int wpas_p2p_is_client(struct wpa_supplicant
*wpa_s
)
3849 return wpa_s
->current_ssid
!= NULL
&&
3850 wpa_s
->current_ssid
->p2p_group
&&
3851 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
;
3855 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
3857 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3859 if (wpa_s
->conf
->p2p_group_idle
== 0 && !wpas_p2p_is_client(wpa_s
)) {
3860 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
3865 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
3867 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_IDLE_TIMEOUT
;
3868 wpas_p2p_group_delete(wpa_s
);
3872 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
3874 unsigned int timeout
;
3876 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3877 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
3880 timeout
= wpa_s
->conf
->p2p_group_idle
;
3881 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
&&
3882 (timeout
== 0 || timeout
> P2P_MAX_CLIENT_IDLE
))
3883 timeout
= P2P_MAX_CLIENT_IDLE
;
3888 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
3890 eloop_register_timeout(timeout
, 0, wpas_p2p_group_idle_timeout
,
3895 void wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3896 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3898 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3900 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3903 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3907 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3908 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3910 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3912 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3915 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3919 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
3921 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
3926 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
3929 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
3930 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
3932 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
)
3933 p2p_set_pri_dev_type(p2p
, wpa_s
->conf
->device_type
);
3936 (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_CONFIG_METHODS
))
3937 p2p_set_config_methods(p2p
, wpa_s
->wps
->config_methods
);
3939 if (wpa_s
->wps
&& (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_UUID
))
3940 p2p_set_uuid(p2p
, wpa_s
->wps
->uuid
);
3942 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_WPS_STRING
) {
3943 p2p_set_manufacturer(p2p
, wpa_s
->conf
->manufacturer
);
3944 p2p_set_model_name(p2p
, wpa_s
->conf
->model_name
);
3945 p2p_set_model_number(p2p
, wpa_s
->conf
->model_number
);
3946 p2p_set_serial_number(p2p
, wpa_s
->conf
->serial_number
);
3949 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
)
3950 p2p_set_sec_dev_types(p2p
,
3951 (void *) wpa_s
->conf
->sec_device_type
,
3952 wpa_s
->conf
->num_sec_device_types
);
3954 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_VENDOR_EXTENSION
) {
3956 p2p_remove_wps_vendor_extensions(p2p
);
3957 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
3958 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
3960 p2p_add_wps_vendor_extension(
3961 p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
3965 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
3966 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
3968 country
[0] = wpa_s
->conf
->country
[0];
3969 country
[1] = wpa_s
->conf
->country
[1];
3971 p2p_set_country(p2p
, country
);
3974 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
3975 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
3976 wpa_s
->conf
->p2p_ssid_postfix
?
3977 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
3981 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
3982 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
3984 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_LISTEN_CHANNEL
) {
3985 u8 reg_class
, channel
;
3988 if (wpa_s
->conf
->p2p_listen_reg_class
&&
3989 wpa_s
->conf
->p2p_listen_channel
) {
3990 reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
3991 channel
= wpa_s
->conf
->p2p_listen_channel
;
3995 * Pick one of the social channels randomly as the
3998 os_get_random((u8
*) &r
, sizeof(r
));
3999 channel
= 1 + (r
% 3) * 5;
4001 ret
= p2p_set_listen_channel(p2p
, reg_class
, channel
);
4003 wpa_printf(MSG_ERROR
, "P2P: Own listen channel update "
4006 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_OPER_CHANNEL
) {
4007 u8 op_reg_class
, op_channel
, cfg_op_channel
;
4010 if (wpa_s
->conf
->p2p_oper_reg_class
&&
4011 wpa_s
->conf
->p2p_oper_channel
) {
4012 op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
4013 op_channel
= wpa_s
->conf
->p2p_oper_channel
;
4018 * Use random operation channel from (1, 6, 11)
4019 *if no other preference is indicated.
4021 os_get_random((u8
*) &r
, sizeof(r
));
4022 op_channel
= 1 + (r
% 3) * 5;
4025 ret
= p2p_set_oper_channel(p2p
, op_reg_class
, op_channel
,
4028 wpa_printf(MSG_ERROR
, "P2P: Own oper channel update "
4034 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
4037 if (!wpa_s
->ap_iface
)
4039 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
4044 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
4046 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4048 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4051 wpa_s
->global
->cross_connection
= enabled
;
4052 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
4055 struct wpa_supplicant
*iface
;
4057 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
4059 if (iface
->cross_connect_enabled
== 0)
4062 iface
->cross_connect_enabled
= 0;
4063 iface
->cross_connect_in_use
= 0;
4064 wpa_msg(iface
->parent
, MSG_INFO
,
4065 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
4066 iface
->ifname
, iface
->cross_connect_uplink
);
4074 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
4076 struct wpa_supplicant
*iface
;
4078 if (!uplink
->global
->cross_connection
)
4081 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
4082 if (!iface
->cross_connect_enabled
)
4084 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
4087 if (iface
->ap_iface
== NULL
)
4089 if (iface
->cross_connect_in_use
)
4092 iface
->cross_connect_in_use
= 1;
4093 wpa_msg(iface
->parent
, MSG_INFO
,
4094 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
4095 iface
->ifname
, iface
->cross_connect_uplink
);
4100 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
4102 struct wpa_supplicant
*iface
;
4104 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
4105 if (!iface
->cross_connect_enabled
)
4107 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
4110 if (!iface
->cross_connect_in_use
)
4113 wpa_msg(iface
->parent
, MSG_INFO
,
4114 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
4115 iface
->ifname
, iface
->cross_connect_uplink
);
4116 iface
->cross_connect_in_use
= 0;
4121 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
4123 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
4124 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
4125 wpa_s
->cross_connect_disallowed
)
4126 wpas_p2p_disable_cross_connect(wpa_s
);
4128 wpas_p2p_enable_cross_connect(wpa_s
);
4129 if (!wpa_s
->ap_iface
)
4130 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
4134 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
4136 wpas_p2p_disable_cross_connect(wpa_s
);
4137 if (!wpa_s
->ap_iface
&&
4138 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout
,
4140 wpas_p2p_set_group_idle_timeout(wpa_s
);
4144 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
4146 struct wpa_supplicant
*iface
;
4148 if (!wpa_s
->global
->cross_connection
)
4151 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
4154 if (iface
->drv_flags
&
4155 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
4157 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
4160 wpa_s
->cross_connect_enabled
= 1;
4161 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
4162 sizeof(wpa_s
->cross_connect_uplink
));
4163 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
4164 "%s to %s whenever uplink is available",
4165 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4167 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
4168 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
4169 iface
->cross_connect_disallowed
||
4170 iface
->wpa_state
!= WPA_COMPLETED
)
4173 wpa_s
->cross_connect_in_use
= 1;
4174 wpa_msg(wpa_s
->parent
, MSG_INFO
,
4175 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
4176 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
4182 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
4184 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
4185 !wpa_s
->p2p_in_provisioning
)
4186 return 0; /* not P2P client operation */
4188 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
4190 if (wpa_s
!= wpa_s
->parent
)
4191 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
4193 if (wpa_s
->global
->p2p
)
4194 p2p_group_formation_failed(wpa_s
->global
->p2p
);
4196 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4197 wpa_s
->parent
, NULL
);
4199 wpas_group_formation_completed(wpa_s
, 0);
4204 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
4206 struct p2p_channels chan
;
4208 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
4211 os_memset(&chan
, 0, sizeof(chan
));
4212 if (wpas_p2p_setup_channels(wpa_s
, &chan
)) {
4213 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
4218 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
);
4222 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
4224 struct wpa_global
*global
= wpa_s
->global
;
4228 if (global
->p2p
== NULL
)
4231 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
4233 if (wpa_s
->pending_interface_name
[0] &&
4234 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
4237 peer
= p2p_get_go_neg_peer(global
->p2p
);
4239 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
4240 MACSTR
, MAC2STR(peer
));
4241 p2p_unauthorize(global
->p2p
, peer
);
4244 wpas_p2p_stop_find(wpa_s
);
4246 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4247 if (wpa_s
== global
->p2p_group_formation
&&
4248 (wpa_s
->p2p_in_provisioning
||
4249 wpa_s
->parent
->pending_interface_type
==
4250 WPA_IF_P2P_CLIENT
)) {
4251 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
4252 "formation found - cancelling",
4255 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4256 wpa_s
->parent
, NULL
);
4257 wpas_p2p_group_delete(wpa_s
);
4263 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
4271 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
4273 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
4276 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
4277 "being available anymore");
4278 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_UNAVAILABLE
;
4279 wpas_p2p_group_delete(wpa_s
);
4283 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
4284 int freq_24
, int freq_5
, int freq_overall
)
4286 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4287 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4289 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
4293 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
4296 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
4298 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
4301 if (hwaddr_aton(addr
, peer
))
4304 return p2p_unauthorize(p2p
, peer
);
4309 * wpas_p2p_disconnect - Disconnect from a P2P Group
4310 * @wpa_s: Pointer to wpa_supplicant data
4311 * Returns: 0 on success, -1 on failure
4313 * This can be used to disconnect from a group in which the local end is a P2P
4314 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4315 * virtual network interface was created for this group, that interface will be
4316 * removed. Otherwise, only the configured P2P group network will be removed
4317 * from the interface.
4319 int wpas_p2p_disconnect(struct wpa_supplicant
*wpa_s
)
4325 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_REQUESTED
;
4326 wpas_p2p_group_delete(wpa_s
);
4332 int wpas_p2p_in_progress(struct wpa_supplicant
*wpa_s
)
4334 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4337 return p2p_in_progress(wpa_s
->global
->p2p
);
4341 void wpas_p2p_network_removed(struct wpa_supplicant
*wpa_s
,
4342 struct wpa_ssid
*ssid
)
4345 if (wpa_s
->p2p_in_provisioning
&& ssid
->p2p_group
&&
4346 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4347 wpa_s
->parent
, NULL
) > 0) {
4348 wpa_printf(MSG_DEBUG
, "P2P: Canceled group formation due to "
4349 "P2P group network getting removed");
4350 wpas_p2p_group_formation_timeout(wpa_s
->parent
, NULL
);
4355 struct wpa_ssid
* wpas_p2p_get_persistent(struct wpa_supplicant
*wpa_s
,
4361 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
4362 if (s
->disabled
!= 2)
4364 if (os_memcmp(s
->bssid
, addr
, ETH_ALEN
) == 0)
4365 return s
; /* peer is GO in the persistent group */
4366 if (s
->mode
!= WPAS_MODE_P2P_GO
|| s
->p2p_client_list
== NULL
)
4368 for (i
= 0; i
< s
->num_p2p_clients
; i
++) {
4369 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
,
4370 addr
, ETH_ALEN
) == 0)
4371 return s
; /* peer is P2P client in persistent
4380 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant
*wpa_s
,
4385 wpas_p2p_add_persistent_group_client(wpa_s
, addr
);