3 * Copyright (c) 2009-2010, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "common/ieee802_11_common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/wpa_ctrl.h"
22 #include "wps/wps_i.h"
24 #include "ap/hostapd.h"
25 #include "ap/p2p_hostapd.h"
26 #include "wpa_supplicant_i.h"
29 #include "config_ssid.h"
35 #include "wps_supplicant.h"
36 #include "p2p_supplicant.h"
39 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
);
40 static struct wpa_supplicant
*
41 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
43 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
);
44 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
);
45 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
46 const u8
*dev_addr
, enum p2p_wps_method wps_method
);
47 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
);
48 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
);
49 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
);
50 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
);
53 static void wpas_p2p_scan_res_handler(struct wpa_supplicant
*wpa_s
,
54 struct wpa_scan_results
*scan_res
)
58 if (wpa_s
->global
->p2p_disabled
)
61 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS)",
64 for (i
= 0; i
< scan_res
->num
; i
++) {
65 struct wpa_scan_res
*bss
= scan_res
->res
[i
];
66 if (p2p_scan_res_handler(wpa_s
->global
->p2p
, bss
->bssid
,
67 bss
->freq
, bss
->level
,
68 (const u8
*) (bss
+ 1),
73 p2p_scan_res_handled(wpa_s
->global
->p2p
);
77 static int wpas_p2p_scan(void *ctx
, enum p2p_scan_type type
, int freq
)
79 struct wpa_supplicant
*wpa_s
= ctx
;
80 struct wpa_driver_scan_params params
;
82 struct wpabuf
*wps_ie
, *ies
;
83 int social_channels
[] = { 2412, 2437, 2462, 0, 0 };
85 os_memset(¶ms
, 0, sizeof(params
));
87 /* P2P Wildcard SSID */
89 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
90 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
92 wpa_s
->wps
->dev
.p2p
= 1;
93 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
98 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + 100);
103 wpabuf_put_buf(ies
, wps_ie
);
106 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
108 params
.extra_ies
= wpabuf_head(ies
);
109 params
.extra_ies_len
= wpabuf_len(ies
);
112 case P2P_SCAN_SOCIAL
:
113 params
.freqs
= social_channels
;
117 case P2P_SCAN_SPECIFIC
:
118 social_channels
[0] = freq
;
119 social_channels
[1] = 0;
120 params
.freqs
= social_channels
;
122 case P2P_SCAN_SOCIAL_PLUS_ONE
:
123 social_channels
[3] = freq
;
124 params
.freqs
= social_channels
;
128 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
129 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
130 ret
= ieee80211_sta_req_scan(wpa_s
, ¶ms
);
132 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
140 #ifdef CONFIG_CLIENT_MLME
141 static void p2p_rx_action_mlme(void *ctx
, const u8
*buf
, size_t len
, int freq
)
143 struct wpa_supplicant
*wpa_s
= ctx
;
144 const struct ieee80211_mgmt
*mgmt
;
147 if (wpa_s
->global
->p2p_disabled
)
149 mgmt
= (const struct ieee80211_mgmt
*) buf
;
150 hdr_len
= (const u8
*) &mgmt
->u
.action
.u
.vs_public_action
.action
- buf
;
153 p2p_rx_action(wpa_s
->global
->p2p
, mgmt
->da
, mgmt
->sa
, mgmt
->bssid
,
154 mgmt
->u
.action
.category
,
155 &mgmt
->u
.action
.u
.vs_public_action
.action
,
156 len
- hdr_len
, freq
);
158 #endif /* CONFIG_CLIENT_MLME */
161 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
163 switch (p2p_group_interface
) {
164 case P2P_GROUP_INTERFACE_PENDING
:
165 return WPA_IF_P2P_GROUP
;
166 case P2P_GROUP_INTERFACE_GO
:
167 return WPA_IF_P2P_GO
;
168 case P2P_GROUP_INTERFACE_CLIENT
:
169 return WPA_IF_P2P_CLIENT
;
172 return WPA_IF_P2P_GROUP
;
176 static void wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
)
178 struct wpa_ssid
*ssid
;
182 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
184 ssid
= wpa_s
->current_ssid
;
187 * The current SSID was not known, but there may still be a
188 * pending P2P group interface waiting for provisioning.
190 ssid
= wpa_s
->conf
->ssid
;
192 if (ssid
->p2p_group
&&
193 (ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
194 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)))
199 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
201 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
202 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
203 wpa_s
->reassociate
= 0;
204 wpa_s
->disconnected
= 1;
205 wpa_supplicant_deauthenticate(wpa_s
,
206 WLAN_REASON_DEAUTH_LEAVING
);
210 if (wpa_s
->cross_connect_in_use
) {
211 wpa_s
->cross_connect_in_use
= 0;
212 wpa_msg(wpa_s
->parent
, MSG_INFO
,
213 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
214 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
216 switch (wpa_s
->removal_reason
) {
217 case P2P_GROUP_REMOVAL_REQUESTED
:
218 reason
= " reason=REQUESTED";
220 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
221 reason
= " reason=IDLE";
227 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_REMOVED
"%s %s%s",
228 wpa_s
->ifname
, gtype
, reason
);
229 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
230 struct wpa_global
*global
;
232 enum wpa_driver_if_type type
;
233 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
235 global
= wpa_s
->global
;
236 ifname
= os_strdup(wpa_s
->ifname
);
237 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
238 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
);
239 wpa_s
= global
->ifaces
;
241 wpa_drv_if_remove(wpa_s
, type
, ifname
);
246 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
247 if (ssid
&& (ssid
->p2p_group
||
248 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
249 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
251 if (ssid
== wpa_s
->current_ssid
)
252 wpa_s
->current_ssid
= NULL
;
253 wpas_notify_network_removed(wpa_s
, ssid
);
254 wpa_config_remove_network(wpa_s
->conf
, id
);
255 wpa_supplicant_clear_status(wpa_s
);
257 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
260 wpa_supplicant_ap_deinit(wpa_s
);
264 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
266 const u8
*ssid
, size_t ssid_len
)
274 if (wpa_s
->go_params
)
275 bssid
= wpa_s
->go_params
->peer_interface_addr
;
277 bssid
= wpa_s
->bssid
;
279 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
281 u8 iface_addr
[ETH_ALEN
];
282 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
284 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
287 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
288 "group is persistent - BSS " MACSTR
" not found",
293 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
295 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
296 "group is persistent - BSS " MACSTR
297 " did not include P2P IE", MAC2STR(bssid
));
298 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
299 (u8
*) (bss
+ 1), bss
->ie_len
);
300 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
301 ((u8
*) bss
+ 1) + bss
->ie_len
,
306 group_capab
= p2p_get_group_capab(p2p
);
307 addr
= p2p_get_go_dev_addr(p2p
);
308 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
309 "group_capab=0x%x", group_capab
);
311 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
312 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
315 os_memset(go_dev_addr
, 0, ETH_ALEN
);
318 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
319 "go_dev_addr=" MACSTR
,
320 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
322 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
326 static void wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
327 struct wpa_ssid
*ssid
,
328 const u8
*go_dev_addr
)
333 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
334 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
335 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
336 if (s
->disabled
== 2 &&
337 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
338 s
->ssid_len
== ssid
->ssid_len
&&
339 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
344 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
346 if (ssid
->passphrase
&& !s
->passphrase
)
348 else if (ssid
->passphrase
&& s
->passphrase
&&
349 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
352 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
355 s
= wpa_config_add_network(wpa_s
->conf
);
358 wpa_config_set_network_defaults(s
);
362 s
->p2p_persistent_group
= 1;
365 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
366 s
->mode
= ssid
->mode
;
367 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
368 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
369 s
->proto
= WPA_PROTO_RSN
;
370 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
371 if (ssid
->passphrase
) {
372 os_free(s
->passphrase
);
373 s
->passphrase
= os_strdup(ssid
->passphrase
);
377 os_memcpy(s
->psk
, ssid
->psk
, 32);
379 if (s
->passphrase
&& !s
->psk_set
)
380 wpa_config_update_psk(s
);
381 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
383 s
->ssid
= os_malloc(ssid
->ssid_len
);
386 s
->ssid_len
= ssid
->ssid_len
;
387 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
390 #ifndef CONFIG_NO_CONFIG_WRITE
391 if (changed
&& wpa_s
->conf
->update_config
&&
392 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
393 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
395 #endif /* CONFIG_NO_CONFIG_WRITE */
399 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
402 struct wpa_ssid
*ssid
;
403 const char *ssid_txt
;
406 u8 go_dev_addr
[ETH_ALEN
];
409 * This callback is likely called for the main interface. Update wpa_s
410 * to use the group interface if a new interface was created for the
413 if (wpa_s
->global
->p2p_group_formation
)
414 wpa_s
= wpa_s
->global
->p2p_group_formation
;
415 wpa_s
->p2p_in_provisioning
= 0;
418 wpa_msg(wpa_s
->parent
, MSG_INFO
,
419 P2P_EVENT_GROUP_FORMATION_FAILURE
);
420 wpas_p2p_group_delete(wpa_s
);
424 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_FORMATION_SUCCESS
);
426 ssid
= wpa_s
->current_ssid
;
427 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
428 ssid
->mode
= WPAS_MODE_P2P_GO
;
429 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
430 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
435 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
436 client
= ssid
->mode
== WPAS_MODE_INFRA
;
437 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
438 persistent
= ssid
->p2p_persistent_group
;
439 os_memcpy(go_dev_addr
, wpa_s
->parent
->own_addr
,
442 persistent
= wpas_p2p_persistent_group(wpa_s
,
448 client
= wpa_s
->p2p_group_interface
==
449 P2P_GROUP_INTERFACE_CLIENT
;
452 wpa_s
->show_group_started
= 0;
455 * Indicate event only after successfully completed 4-way
456 * handshake, i.e., when the interface is ready for data
459 wpa_s
->show_group_started
= 1;
460 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
462 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
463 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
464 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
466 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
467 MAC2STR(go_dev_addr
),
468 persistent
? " [PERSISTENT]" : "");
469 wpas_p2p_cross_connect_setup(wpa_s
);
470 wpas_p2p_set_group_idle_timeout(wpa_s
);
472 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
473 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
474 "go_dev_addr=" MACSTR
"%s",
475 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
,
476 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
477 MAC2STR(go_dev_addr
),
478 persistent
? " [PERSISTENT]" : "");
479 wpas_p2p_cross_connect_setup(wpa_s
);
480 wpas_p2p_set_group_idle_timeout(wpa_s
);
484 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
489 static void wpas_send_action_cb(void *eloop_ctx
, void *timeout_ctx
)
491 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
492 struct wpa_supplicant
*iface
;
496 without_roc
= wpa_s
->pending_action_without_roc
;
497 wpa_s
->pending_action_without_roc
= 0;
498 wpa_printf(MSG_DEBUG
, "P2P: Send Action callback (without_roc=%d "
499 "pending_action_tx=%p)",
500 without_roc
, wpa_s
->pending_action_tx
);
502 if (wpa_s
->pending_action_tx
== NULL
)
505 if (wpa_s
->off_channel_freq
!= wpa_s
->pending_action_freq
&&
506 wpa_s
->pending_action_freq
!= 0) {
507 wpa_printf(MSG_DEBUG
, "P2P: Pending Action frame TX "
508 "waiting for another freq=%u (off_channel_freq=%u)",
509 wpa_s
->pending_action_freq
,
510 wpa_s
->off_channel_freq
);
511 if (without_roc
&& wpa_s
->off_channel_freq
== 0) {
513 * We may get here if wpas_send_action() found us to be
514 * on the correct channel, but remain-on-channel cancel
515 * event was received before getting here.
517 wpa_printf(MSG_DEBUG
, "P2P: Schedule "
518 "remain-on-channel to send Action frame");
519 if (wpa_drv_remain_on_channel(
520 wpa_s
, wpa_s
->pending_action_freq
, 200) <
522 wpa_printf(MSG_DEBUG
, "P2P: Failed to request "
523 "driver to remain on channel (%u "
524 "MHz) for Action Frame TX",
525 wpa_s
->pending_action_freq
);
527 wpa_s
->roc_waiting_drv_freq
=
528 wpa_s
->pending_action_freq
;
534 * This call is likely going to be on the P2P device instance if the
535 * driver uses a separate interface for that purpose. However, some
536 * Action frames are actually sent within a P2P Group and when that is
537 * the case, we need to follow power saving (e.g., GO buffering the
538 * frame for a client in PS mode or a client following the advertised
539 * NoA from its GO). To make that easier for the driver, select the
540 * correct group interface here.
542 if (os_memcmp(wpa_s
->pending_action_src
, wpa_s
->own_addr
, ETH_ALEN
) !=
545 * Try to find a group interface that matches with the source
548 iface
= wpa_s
->global
->ifaces
;
550 if (os_memcmp(wpa_s
->pending_action_src
,
551 iface
->own_addr
, ETH_ALEN
) == 0)
556 wpa_printf(MSG_DEBUG
, "P2P: Use group interface %s "
557 "instead of interface %s for Action TX",
558 iface
->ifname
, wpa_s
->ifname
);
564 wpa_printf(MSG_DEBUG
, "P2P: Sending pending Action frame to "
565 MACSTR
" using interface %s",
566 MAC2STR(wpa_s
->pending_action_dst
), iface
->ifname
);
567 res
= wpa_drv_send_action(iface
, wpa_s
->pending_action_freq
,
568 wpa_s
->pending_action_dst
,
569 wpa_s
->pending_action_src
,
570 wpa_s
->pending_action_bssid
,
571 wpabuf_head(wpa_s
->pending_action_tx
),
572 wpabuf_len(wpa_s
->pending_action_tx
));
574 wpa_printf(MSG_DEBUG
, "P2P: Failed to send the pending "
577 * Use fake TX status event to allow P2P state machine to
580 wpas_send_action_tx_status(
581 wpa_s
, wpa_s
->pending_action_dst
,
582 wpabuf_head(wpa_s
->pending_action_tx
),
583 wpabuf_len(wpa_s
->pending_action_tx
),
584 P2P_SEND_ACTION_FAILED
);
589 void wpas_send_action_tx_status(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
590 const u8
*data
, size_t data_len
,
591 enum p2p_send_action_result result
)
593 if (wpa_s
->global
->p2p_disabled
)
596 if (wpa_s
->pending_action_tx
== NULL
) {
597 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - no "
598 "pending operation");
602 if (os_memcmp(dst
, wpa_s
->pending_action_dst
, ETH_ALEN
) != 0) {
603 wpa_printf(MSG_DEBUG
, "P2P: Ignore Action TX status - unknown "
604 "destination address");
608 wpabuf_free(wpa_s
->pending_action_tx
);
609 wpa_s
->pending_action_tx
= NULL
;
611 p2p_send_action_cb(wpa_s
->global
->p2p
, wpa_s
->pending_action_freq
,
612 wpa_s
->pending_action_dst
,
613 wpa_s
->pending_action_src
,
614 wpa_s
->pending_action_bssid
,
617 if (wpa_s
->pending_pd_before_join
&&
618 (os_memcmp(wpa_s
->pending_action_dst
, wpa_s
->pending_join_dev_addr
,
620 os_memcmp(wpa_s
->pending_action_dst
,
621 wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
622 wpa_s
->pending_pd_before_join
= 0;
623 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
624 "join-existing-group operation");
625 wpas_p2p_join_start(wpa_s
);
630 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
631 const u8
*src
, const u8
*bssid
, const u8
*buf
,
632 size_t len
, unsigned int wait_time
)
634 struct wpa_supplicant
*wpa_s
= ctx
;
636 wpa_printf(MSG_DEBUG
, "P2P: Send action frame: freq=%d dst=" MACSTR
637 " src=" MACSTR
" bssid=" MACSTR
" len=%d",
638 freq
, MAC2STR(dst
), MAC2STR(src
), MAC2STR(bssid
),
641 if (wpa_s
->pending_action_tx
) {
642 wpa_printf(MSG_DEBUG
, "P2P: Dropped pending Action frame TX "
643 "to " MACSTR
, MAC2STR(wpa_s
->pending_action_dst
));
644 wpabuf_free(wpa_s
->pending_action_tx
);
646 wpa_s
->pending_action_tx
= wpabuf_alloc(len
);
647 if (wpa_s
->pending_action_tx
== NULL
) {
648 wpa_printf(MSG_DEBUG
, "P2P: Failed to allocate Action frame "
649 "TX buffer (len=%llu)", (unsigned long long) len
);
652 wpabuf_put_data(wpa_s
->pending_action_tx
, buf
, len
);
653 os_memcpy(wpa_s
->pending_action_src
, src
, ETH_ALEN
);
654 os_memcpy(wpa_s
->pending_action_dst
, dst
, ETH_ALEN
);
655 os_memcpy(wpa_s
->pending_action_bssid
, bssid
, ETH_ALEN
);
656 wpa_s
->pending_action_freq
= freq
;
658 if (wpa_s
->off_channel_freq
== freq
|| freq
== 0) {
659 wpa_printf(MSG_DEBUG
, "P2P: Already on requested channel; "
660 "send Action frame immediately");
661 /* TODO: Would there ever be need to extend the current
662 * duration on the channel? */
663 wpa_s
->pending_action_without_roc
= 1;
664 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
665 eloop_register_timeout(0, 0, wpas_send_action_cb
, wpa_s
, NULL
);
668 wpa_s
->pending_action_without_roc
= 0;
670 if (wpa_s
->roc_waiting_drv_freq
== freq
) {
671 wpa_printf(MSG_DEBUG
, "P2P: Already waiting for driver to get "
672 "to frequency %u MHz; continue waiting to send the "
673 "Action frame", freq
);
677 wpa_printf(MSG_DEBUG
, "P2P: Schedule Action frame to be transmitted "
678 "once the driver gets to the requested channel");
679 if (wait_time
> wpa_s
->max_remain_on_chan
)
680 wait_time
= wpa_s
->max_remain_on_chan
;
681 if (wpa_drv_remain_on_channel(wpa_s
, freq
, wait_time
) < 0) {
682 wpa_printf(MSG_DEBUG
, "P2P: Failed to request driver "
683 "to remain on channel (%u MHz) for Action "
687 wpa_s
->roc_waiting_drv_freq
= freq
;
693 static void wpas_send_action_done(void *ctx
)
695 struct wpa_supplicant
*wpa_s
= ctx
;
696 wpa_printf(MSG_DEBUG
, "P2P: Action frame sequence done notification");
697 wpabuf_free(wpa_s
->pending_action_tx
);
698 wpa_s
->pending_action_tx
= NULL
;
699 if (wpa_s
->off_channel_freq
) {
700 wpa_drv_cancel_remain_on_channel(wpa_s
);
701 wpa_s
->off_channel_freq
= 0;
702 wpa_s
->roc_waiting_drv_freq
= 0;
707 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
708 struct p2p_go_neg_results
*params
)
710 if (wpa_s
->go_params
== NULL
) {
711 wpa_s
->go_params
= os_malloc(sizeof(*params
));
712 if (wpa_s
->go_params
== NULL
)
715 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
720 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
721 struct p2p_go_neg_results
*res
)
723 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
724 MAC2STR(res
->peer_interface_addr
));
725 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
726 res
->ssid
, res
->ssid_len
);
727 wpa_supplicant_ap_deinit(wpa_s
);
728 wpas_copy_go_neg_results(wpa_s
, res
);
729 if (res
->wps_method
== WPS_PBC
)
730 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
732 u16 dev_pw_id
= DEV_PW_DEFAULT
;
733 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
734 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
735 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
736 wpa_s
->p2p_pin
, 1, dev_pw_id
);
741 static void p2p_go_configured(void *ctx
, void *data
)
743 struct wpa_supplicant
*wpa_s
= ctx
;
744 struct p2p_go_neg_results
*params
= data
;
745 struct wpa_ssid
*ssid
;
747 ssid
= wpa_s
->current_ssid
;
748 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
749 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
750 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
751 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
752 "go_dev_addr=" MACSTR
"%s",
754 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
756 params
->passphrase
? params
->passphrase
: "",
757 MAC2STR(wpa_s
->parent
->own_addr
),
758 params
->persistent_group
? " [PERSISTENT]" : "");
759 if (params
->persistent_group
)
760 wpas_p2p_store_persistent_group(
762 wpa_s
->parent
->own_addr
);
763 wpas_p2p_cross_connect_setup(wpa_s
);
764 wpas_p2p_set_group_idle_timeout(wpa_s
);
768 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
769 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
770 params
->peer_interface_addr
)) {
771 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
775 if (params
->wps_method
== WPS_PBC
)
776 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
);
777 else if (wpa_s
->p2p_pin
[0])
778 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
779 wpa_s
->p2p_pin
, NULL
, 0);
780 os_free(wpa_s
->go_params
);
781 wpa_s
->go_params
= NULL
;
785 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
786 struct p2p_go_neg_results
*params
,
789 struct wpa_ssid
*ssid
;
791 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0)
794 ssid
= wpa_config_add_network(wpa_s
->conf
);
798 wpa_config_set_network_defaults(ssid
);
801 ssid
->p2p_persistent_group
= params
->persistent_group
;
802 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
804 ssid
->frequency
= params
->freq
;
805 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
807 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
808 ssid
->ssid_len
= params
->ssid_len
;
810 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
811 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
812 ssid
->proto
= WPA_PROTO_RSN
;
813 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
814 ssid
->passphrase
= os_strdup(params
->passphrase
);
816 wpa_s
->ap_configured_cb
= p2p_go_configured
;
817 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
818 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
819 wpa_s
->connect_without_scan
= 1;
820 wpa_s
->reassociate
= 1;
821 wpa_s
->disconnected
= 0;
822 wpa_supplicant_req_scan(wpa_s
, 0, 0);
826 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
827 const struct wpa_supplicant
*src
)
829 struct wpa_config
*d
;
830 const struct wpa_config
*s
;
835 #define C(n) if (s->n) d->n = os_strdup(s->n)
845 d
->p2p_group_idle
= s
->p2p_group_idle
;
849 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
850 enum wpa_driver_if_type type
)
852 char ifname
[120], force_ifname
[120];
854 if (wpa_s
->pending_interface_name
[0]) {
855 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
856 "- skip creation of a new one");
857 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
858 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
859 "unknown?! ifname='%s'",
860 wpa_s
->pending_interface_name
);
866 os_snprintf(ifname
, sizeof(ifname
), "%s-p2p-%d", wpa_s
->ifname
,
867 wpa_s
->p2p_group_idx
);
868 force_ifname
[0] = '\0';
870 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
872 wpa_s
->p2p_group_idx
++;
874 wpa_s
->pending_interface_type
= type
;
875 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
876 wpa_s
->pending_interface_addr
) < 0) {
877 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
882 if (force_ifname
[0]) {
883 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
885 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
886 sizeof(wpa_s
->pending_interface_name
));
888 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
889 sizeof(wpa_s
->pending_interface_name
));
890 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
891 MACSTR
, wpa_s
->pending_interface_name
,
892 MAC2STR(wpa_s
->pending_interface_addr
));
898 static void wpas_p2p_remove_pending_group_interface(
899 struct wpa_supplicant
*wpa_s
)
901 if (!wpa_s
->pending_interface_name
[0] ||
902 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
903 return; /* No pending virtual interface */
905 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
906 wpa_s
->pending_interface_name
);
907 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
908 wpa_s
->pending_interface_name
);
909 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
910 wpa_s
->pending_interface_name
[0] = '\0';
914 static struct wpa_supplicant
*
915 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
917 struct wpa_interface iface
;
918 struct wpa_supplicant
*group_wpa_s
;
920 if (!wpa_s
->pending_interface_name
[0]) {
921 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
922 if (!wpas_p2p_create_iface(wpa_s
))
925 * Something has forced us to remove the pending interface; try
926 * to create a new one and hope for the best that we will get
927 * the same local address.
929 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
930 WPA_IF_P2P_CLIENT
) < 0)
934 os_memset(&iface
, 0, sizeof(iface
));
935 iface
.ifname
= wpa_s
->pending_interface_name
;
936 iface
.driver
= wpa_s
->driver
->name
;
937 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
938 iface
.driver_param
= wpa_s
->conf
->driver_param
;
939 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
940 if (group_wpa_s
== NULL
) {
941 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
942 "wpa_supplicant interface");
945 wpa_s
->pending_interface_name
[0] = '\0';
946 group_wpa_s
->parent
= wpa_s
;
947 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
948 P2P_GROUP_INTERFACE_CLIENT
;
949 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
951 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
957 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
960 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
961 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
962 if (wpa_s
->global
->p2p
)
963 p2p_group_formation_failed(wpa_s
->global
->p2p
);
964 wpas_group_formation_completed(wpa_s
, 0);
968 void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
970 struct wpa_supplicant
*wpa_s
= ctx
;
972 if (wpa_s
->off_channel_freq
) {
973 wpa_drv_cancel_remain_on_channel(wpa_s
);
974 wpa_s
->off_channel_freq
= 0;
975 wpa_s
->roc_waiting_drv_freq
= 0;
979 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_FAILURE
"status=%d",
981 wpas_p2p_remove_pending_group_interface(wpa_s
);
985 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
);
987 if (wpa_s
->create_p2p_iface
) {
988 struct wpa_supplicant
*group_wpa_s
=
989 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
990 if (group_wpa_s
== NULL
) {
991 wpas_p2p_remove_pending_group_interface(wpa_s
);
994 if (group_wpa_s
!= wpa_s
) {
995 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
996 sizeof(group_wpa_s
->p2p_pin
));
997 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
999 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1000 wpa_s
->pending_interface_name
[0] = '\0';
1001 group_wpa_s
->p2p_in_provisioning
= 1;
1004 wpas_start_wps_go(group_wpa_s
, res
, 1);
1006 wpas_start_wps_enrollee(group_wpa_s
, res
);
1008 wpa_s
->p2p_in_provisioning
= 1;
1009 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1012 wpas_start_wps_go(wpa_s
, res
, 1);
1014 wpas_start_wps_enrollee(ctx
, res
);
1017 wpa_s
->p2p_long_listen
= 0;
1018 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1020 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1021 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1022 (res
->peer_config_timeout
% 100) * 10000,
1023 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1027 void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1029 struct wpa_supplicant
*wpa_s
= ctx
;
1030 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1031 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1035 void wpas_dev_found(void *ctx
, const u8
*addr
, const u8
*dev_addr
,
1036 const u8
*pri_dev_type
, const char *dev_name
,
1037 u16 config_methods
, u8 dev_capab
, u8 group_capab
)
1039 struct wpa_supplicant
*wpa_s
= ctx
;
1040 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1041 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1042 " p2p_dev_addr=" MACSTR
1043 " pri_dev_type=%s name='%s' config_methods=0x%x "
1044 "dev_capab=0x%x group_capab=0x%x",
1045 MAC2STR(addr
), MAC2STR(dev_addr
),
1046 wps_dev_type_bin2str(pri_dev_type
, devtype
, sizeof(devtype
)),
1047 dev_name
, config_methods
, dev_capab
, group_capab
);
1051 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1052 unsigned int duration
,
1053 const struct wpabuf
*probe_resp_ie
)
1055 struct wpa_supplicant
*wpa_s
= ctx
;
1057 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, probe_resp_ie
, NULL
);
1059 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1060 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1061 "report received Probe Request frames");
1065 wpa_s
->pending_listen_freq
= freq
;
1066 wpa_s
->pending_listen_duration
= duration
;
1068 if (wpa_drv_remain_on_channel(wpa_s
, freq
, duration
) < 0) {
1069 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1070 "to remain on channel (%u MHz) for Listen "
1072 wpa_s
->pending_listen_freq
= 0;
1075 wpa_s
->roc_waiting_drv_freq
= freq
;
1081 static void wpas_stop_listen(void *ctx
)
1083 struct wpa_supplicant
*wpa_s
= ctx
;
1084 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1085 wpa_drv_cancel_remain_on_channel(wpa_s
);
1086 wpa_s
->off_channel_freq
= 0;
1087 wpa_s
->roc_waiting_drv_freq
= 0;
1089 wpa_drv_probe_req_report(wpa_s
, 0);
1093 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1095 struct wpa_supplicant
*wpa_s
= ctx
;
1096 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
));
1100 static struct p2p_srv_bonjour
*
1101 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1102 const struct wpabuf
*query
)
1104 struct p2p_srv_bonjour
*bsrv
;
1107 len
= wpabuf_len(query
);
1108 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1109 struct p2p_srv_bonjour
, list
) {
1110 if (len
== wpabuf_len(bsrv
->query
) &&
1111 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1119 static struct p2p_srv_upnp
*
1120 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1121 const char *service
)
1123 struct p2p_srv_upnp
*usrv
;
1125 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1126 struct p2p_srv_upnp
, list
) {
1127 if (version
== usrv
->version
&&
1128 os_strcmp(service
, usrv
->service
) == 0)
1135 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1140 if (wpabuf_tailroom(resp
) < 5)
1143 /* Length (to be filled) */
1144 len_pos
= wpabuf_put(resp
, 2);
1145 wpabuf_put_u8(resp
, srv_proto
);
1146 wpabuf_put_u8(resp
, srv_trans_id
);
1148 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1149 /* Response Data: empty */
1150 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1154 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1155 struct wpabuf
*resp
, u8 srv_trans_id
)
1157 struct p2p_srv_bonjour
*bsrv
;
1160 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1162 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1163 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1167 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1168 struct p2p_srv_bonjour
, list
) {
1169 if (wpabuf_tailroom(resp
) <
1170 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1172 /* Length (to be filled) */
1173 len_pos
= wpabuf_put(resp
, 2);
1174 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1175 wpabuf_put_u8(resp
, srv_trans_id
);
1177 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1178 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1179 wpabuf_head(bsrv
->resp
),
1180 wpabuf_len(bsrv
->resp
));
1182 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1183 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1184 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1190 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
1191 struct wpabuf
*resp
, u8 srv_trans_id
,
1192 const u8
*query
, size_t query_len
)
1194 struct p2p_srv_bonjour
*bsrv
;
1198 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
1200 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1201 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1202 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
1207 if (query_len
== 0) {
1208 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1212 if (wpabuf_tailroom(resp
) < 5)
1214 /* Length (to be filled) */
1215 len_pos
= wpabuf_put(resp
, 2);
1216 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1217 wpabuf_put_u8(resp
, srv_trans_id
);
1219 wpabuf_set(&buf
, query
, query_len
);
1220 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, &buf
);
1222 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
1226 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1227 /* Response Data: empty */
1228 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1234 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1235 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1236 wpabuf_head(bsrv
->resp
), wpabuf_len(bsrv
->resp
));
1238 if (wpabuf_tailroom(resp
) >=
1239 wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
)) {
1241 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1242 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1244 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1248 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
1249 struct wpabuf
*resp
, u8 srv_trans_id
)
1251 struct p2p_srv_upnp
*usrv
;
1254 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
1256 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1257 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1261 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1262 struct p2p_srv_upnp
, list
) {
1263 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
1266 /* Length (to be filled) */
1267 len_pos
= wpabuf_put(resp
, 2);
1268 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1269 wpabuf_put_u8(resp
, srv_trans_id
);
1272 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1274 wpabuf_put_u8(resp
, usrv
->version
);
1275 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1277 wpabuf_put_str(resp
, usrv
->service
);
1278 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1284 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
1285 struct wpabuf
*resp
, u8 srv_trans_id
,
1286 const u8
*query
, size_t query_len
)
1288 struct p2p_srv_upnp
*usrv
;
1294 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
1297 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1298 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1299 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
1304 if (query_len
== 0) {
1305 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1310 str
= os_malloc(query_len
);
1313 os_memcpy(str
, query
+ 1, query_len
- 1);
1314 str
[query_len
- 1] = '\0';
1316 if (wpabuf_tailroom(resp
) < 5)
1319 /* Length (to be filled) */
1320 len_pos
= wpabuf_put(resp
, 2);
1321 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1322 wpabuf_put_u8(resp
, srv_trans_id
);
1324 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1325 struct p2p_srv_upnp
, list
) {
1326 if (version
!= usrv
->version
)
1329 if (os_strcmp(str
, "ssdp:all") != 0 &&
1330 os_strstr(usrv
->service
, str
) == NULL
)
1333 if (wpabuf_tailroom(resp
) < 2)
1337 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1339 wpabuf_put_u8(resp
, version
);
1341 wpabuf_put_u8(resp
, ',');
1345 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1347 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
1349 wpabuf_put_str(resp
, usrv
->service
);
1353 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
1356 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1357 /* Response Data: empty */
1360 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1364 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
1365 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
1367 struct wpa_supplicant
*wpa_s
= ctx
;
1368 const u8
*pos
= tlvs
;
1369 const u8
*end
= tlvs
+ tlvs_len
;
1372 struct wpabuf
*resp
;
1373 u8 srv_proto
, srv_trans_id
;
1377 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
1379 buf_len
= 2 * tlvs_len
+ 1;
1380 buf
= os_malloc(buf_len
);
1382 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1383 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
1385 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
1390 if (wpa_s
->p2p_sd_over_ctrl_iface
)
1391 return; /* to be processed by an external program */
1393 resp
= wpabuf_alloc(10000);
1397 while (pos
+ 1 < end
) {
1398 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
1399 slen
= WPA_GET_LE16(pos
);
1401 if (pos
+ slen
> end
|| slen
< 2) {
1402 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
1407 tlv_end
= pos
+ slen
;
1410 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1412 srv_trans_id
= *pos
++;
1413 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1416 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
1417 pos
, tlv_end
- pos
);
1420 if (wpa_s
->force_long_sd
) {
1421 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
1423 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1424 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1428 switch (srv_proto
) {
1429 case P2P_SERV_ALL_SERVICES
:
1430 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
1431 "for all services");
1432 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
1433 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1434 wpa_printf(MSG_DEBUG
, "P2P: No service "
1435 "discovery protocols available");
1436 wpas_sd_add_proto_not_avail(
1437 resp
, P2P_SERV_ALL_SERVICES
,
1441 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1442 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1444 case P2P_SERV_BONJOUR
:
1445 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
1446 pos
, tlv_end
- pos
);
1449 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
1450 pos
, tlv_end
- pos
);
1453 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
1454 "protocol %u", srv_proto
);
1455 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
1464 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
1470 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
1471 const u8
*tlvs
, size_t tlvs_len
)
1473 struct wpa_supplicant
*wpa_s
= ctx
;
1474 const u8
*pos
= tlvs
;
1475 const u8
*end
= tlvs
+ tlvs_len
;
1481 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
1483 if (tlvs_len
> 1500) {
1484 /* TODO: better way for handling this */
1485 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1486 P2P_EVENT_SERV_DISC_RESP MACSTR
1487 " %u <long response: %u bytes>",
1488 MAC2STR(sa
), update_indic
,
1489 (unsigned int) tlvs_len
);
1491 buf_len
= 2 * tlvs_len
+ 1;
1492 buf
= os_malloc(buf_len
);
1494 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1495 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1496 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
1497 MAC2STR(sa
), update_indic
, buf
);
1503 u8 srv_proto
, srv_trans_id
, status
;
1505 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
1506 slen
= WPA_GET_LE16(pos
);
1508 if (pos
+ slen
> end
|| slen
< 3) {
1509 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
1513 tlv_end
= pos
+ slen
;
1516 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1518 srv_trans_id
= *pos
++;
1519 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1522 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
1525 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
1526 pos
, tlv_end
- pos
);
1533 void * wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1534 const struct wpabuf
*tlvs
)
1536 return p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
1540 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1541 u8 version
, const char *query
)
1543 struct wpabuf
*tlvs
;
1546 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
1549 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
1550 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
1551 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
1552 wpabuf_put_u8(tlvs
, version
);
1553 wpabuf_put_str(tlvs
, query
);
1554 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
1560 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, void *req
)
1562 return p2p_sd_cancel_request(wpa_s
->global
->p2p
, req
);
1566 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
1567 const u8
*dst
, u8 dialog_token
,
1568 const struct wpabuf
*resp_tlvs
)
1570 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
1575 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
1577 p2p_sd_service_update(wpa_s
->global
->p2p
);
1581 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
1583 dl_list_del(&bsrv
->list
);
1584 wpabuf_free(bsrv
->query
);
1585 wpabuf_free(bsrv
->resp
);
1590 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
1592 dl_list_del(&usrv
->list
);
1593 os_free(usrv
->service
);
1598 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
1600 struct p2p_srv_bonjour
*bsrv
, *bn
;
1601 struct p2p_srv_upnp
*usrv
, *un
;
1603 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
1604 struct p2p_srv_bonjour
, list
)
1605 wpas_p2p_srv_bonjour_free(bsrv
);
1607 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
1608 struct p2p_srv_upnp
, list
)
1609 wpas_p2p_srv_upnp_free(usrv
);
1611 wpas_p2p_sd_service_update(wpa_s
);
1615 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
1616 struct wpabuf
*query
, struct wpabuf
*resp
)
1618 struct p2p_srv_bonjour
*bsrv
;
1620 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1623 wpabuf_free(bsrv
->resp
);
1628 bsrv
= os_zalloc(sizeof(*bsrv
));
1631 bsrv
->query
= query
;
1633 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
1635 wpas_p2p_sd_service_update(wpa_s
);
1640 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
1641 const struct wpabuf
*query
)
1643 struct p2p_srv_bonjour
*bsrv
;
1645 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1648 wpas_p2p_srv_bonjour_free(bsrv
);
1649 wpas_p2p_sd_service_update(wpa_s
);
1654 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1655 const char *service
)
1657 struct p2p_srv_upnp
*usrv
;
1659 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
1660 return 0; /* Already listed */
1661 usrv
= os_zalloc(sizeof(*usrv
));
1664 usrv
->version
= version
;
1665 usrv
->service
= os_strdup(service
);
1666 if (usrv
->service
== NULL
) {
1670 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
1672 wpas_p2p_sd_service_update(wpa_s
);
1677 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1678 const char *service
)
1680 struct p2p_srv_upnp
*usrv
;
1682 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
1685 wpas_p2p_srv_upnp_free(usrv
);
1686 wpas_p2p_sd_service_update(wpa_s
);
1691 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
1692 const u8
*peer
, const char *params
)
1694 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
" %08d%s",
1695 MAC2STR(peer
), wps_generate_pin(), params
);
1699 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
1700 const u8
*peer
, const char *params
)
1702 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
"%s",
1703 MAC2STR(peer
), params
);
1707 void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
1708 const u8
*dev_addr
, const u8
*pri_dev_type
,
1709 const char *dev_name
, u16 supp_config_methods
,
1710 u8 dev_capab
, u8 group_capab
)
1712 struct wpa_supplicant
*wpa_s
= ctx
;
1713 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1715 u8 empty_dev_type
[8];
1717 if (pri_dev_type
== NULL
) {
1718 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
1719 pri_dev_type
= empty_dev_type
;
1721 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
1722 " pri_dev_type=%s name='%s' config_methods=0x%x "
1723 "dev_capab=0x%x group_capab=0x%x",
1725 wps_dev_type_bin2str(pri_dev_type
, devtype
,
1727 dev_name
, supp_config_methods
, dev_capab
, group_capab
);
1728 params
[sizeof(params
) - 1] = '\0';
1730 if (config_methods
& WPS_CONFIG_DISPLAY
)
1731 wpas_prov_disc_local_display(wpa_s
, peer
, params
);
1732 else if (config_methods
& WPS_CONFIG_KEYPAD
)
1733 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
1734 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1735 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1736 "%s", MAC2STR(peer
), params
);
1740 void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
1742 struct wpa_supplicant
*wpa_s
= ctx
;
1743 if (config_methods
& WPS_CONFIG_DISPLAY
)
1744 wpas_prov_disc_local_keypad(wpa_s
, peer
, "");
1745 else if (config_methods
& WPS_CONFIG_KEYPAD
)
1746 wpas_prov_disc_local_display(wpa_s
, peer
, "");
1747 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
1748 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
,
1751 if (wpa_s
->pending_pd_before_join
&&
1752 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
1753 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
1754 wpa_s
->pending_pd_before_join
= 0;
1755 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
1756 "join-existing-group operation");
1757 wpas_p2p_join_start(wpa_s
);
1762 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
1763 const u8
*go_dev_addr
, const u8
*ssid
,
1764 size_t ssid_len
, int *go
, u8
*group_bssid
,
1765 int *force_freq
, int persistent_group
)
1767 struct wpa_supplicant
*wpa_s
= ctx
;
1769 u8 cur_bssid
[ETH_ALEN
];
1772 if (!persistent_group
) {
1773 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1774 " to join an active group", MAC2STR(sa
));
1775 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
1776 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
1778 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
1779 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
1780 "authorized invitation");
1784 * Do not accept the invitation automatically; notify user and
1787 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1790 if (!wpa_s
->conf
->persistent_reconnect
)
1791 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
1793 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1794 if (s
->disabled
== 2 &&
1795 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
1796 s
->ssid_len
== ssid_len
&&
1797 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
1802 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
1803 " requested reinvocation of an unknown group",
1805 return P2P_SC_FAIL_UNKNOWN_GROUP
;
1808 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
1810 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
1811 wpa_printf(MSG_DEBUG
, "P2P: The only available "
1812 "interface is already in use - reject "
1814 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1816 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
1817 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
1819 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
1821 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
1822 "interface address for the group");
1823 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
1825 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
1830 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, cur_bssid
) == 0 &&
1831 wpa_s
->assoc_freq
) {
1832 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
1833 "the channel we are already using");
1834 *force_freq
= wpa_s
->assoc_freq
;
1837 res
= wpa_drv_shared_freq(wpa_s
);
1839 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
1840 "with the channel we are already using on a "
1841 "shared interface");
1845 return P2P_SC_SUCCESS
;
1849 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
1850 const u8
*ssid
, size_t ssid_len
,
1851 const u8
*go_dev_addr
, u8 status
,
1854 struct wpa_supplicant
*wpa_s
= ctx
;
1857 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
1858 if (s
->disabled
== 2 &&
1859 s
->ssid_len
== ssid_len
&&
1860 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
1864 if (status
== P2P_SC_SUCCESS
) {
1865 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
1866 " was accepted; op_freq=%d MHz",
1867 MAC2STR(sa
), op_freq
);
1869 wpas_p2p_group_add_persistent(
1870 wpa_s
, s
, s
->mode
== WPAS_MODE_P2P_GO
, 0);
1872 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
1873 wpa_s
->p2p_wps_method
);
1878 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
1879 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
1880 " was rejected (status %u)", MAC2STR(sa
), status
);
1886 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
1887 "sa=" MACSTR
" go_dev_addr=" MACSTR
1888 " bssid=" MACSTR
" unknown-network",
1889 MAC2STR(sa
), MAC2STR(go_dev_addr
),
1892 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
1893 "sa=" MACSTR
" go_dev_addr=" MACSTR
1895 MAC2STR(sa
), MAC2STR(go_dev_addr
));
1900 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
"sa=" MACSTR
1901 " persistent=%d", MAC2STR(sa
), s
->id
);
1905 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
)
1907 struct wpa_supplicant
*wpa_s
= ctx
;
1908 struct wpa_ssid
*ssid
;
1911 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
1912 "status=%d " MACSTR
,
1913 status
, MAC2STR(bssid
));
1915 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
1916 "status=%d ", status
);
1919 if (wpa_s
->pending_invite_ssid_id
== -1)
1920 return; /* Invitation to active group */
1922 if (status
!= P2P_SC_SUCCESS
) {
1923 wpas_p2p_remove_pending_group_interface(wpa_s
);
1927 ssid
= wpa_config_get_network(wpa_s
->conf
,
1928 wpa_s
->pending_invite_ssid_id
);
1930 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
1931 "data matching with invitation");
1935 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
1936 ssid
->mode
== WPAS_MODE_P2P_GO
, 0);
1940 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
1941 struct p2p_channels
*chan
)
1945 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
1948 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1949 chan
->reg_class
[cla
].reg_class
= 81;
1950 chan
->reg_class
[cla
].channels
= 11;
1951 for (i
= 0; i
< 11; i
++)
1952 chan
->reg_class
[cla
].channel
[i
] = i
+ 1;
1955 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
1958 /* Operating class 115 - 5 GHz, channels 36-48 */
1959 chan
->reg_class
[cla
].reg_class
= 115;
1960 chan
->reg_class
[cla
].channels
= 4;
1961 chan
->reg_class
[cla
].channel
[0] = 36;
1962 chan
->reg_class
[cla
].channel
[1] = 40;
1963 chan
->reg_class
[cla
].channel
[2] = 44;
1964 chan
->reg_class
[cla
].channel
[3] = 48;
1967 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
1970 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1971 chan
->reg_class
[cla
].reg_class
= 124;
1972 chan
->reg_class
[cla
].channels
= 4;
1973 chan
->reg_class
[cla
].channel
[0] = 149;
1974 chan
->reg_class
[cla
].channel
[1] = 153;
1975 chan
->reg_class
[cla
].channel
[2] = 157;
1976 chan
->reg_class
[cla
].channel
[3] = 161;
1979 chan
->reg_classes
= cla
;
1984 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
1986 enum hostapd_hw_mode mode
)
1990 for (i
= 0; i
< num_modes
; i
++) {
1991 if (modes
[i
].mode
== mode
)
1999 static int has_channel(struct hostapd_hw_modes
*mode
, u8 chan
)
2003 for (i
= 0; i
< mode
->num_channels
; i
++) {
2004 if (mode
->channels
[i
].chan
== chan
) {
2005 return !(mode
->channels
[i
].flag
&
2006 (HOSTAPD_CHAN_DISABLED
|
2007 HOSTAPD_CHAN_PASSIVE_SCAN
|
2008 HOSTAPD_CHAN_NO_IBSS
|
2009 HOSTAPD_CHAN_RADAR
));
2017 struct p2p_oper_class_map
{
2018 enum hostapd_hw_mode mode
;
2025 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
2026 struct p2p_channels
*chan
)
2028 struct hostapd_hw_modes
*modes
, *mode
;
2029 u16 num_modes
, flags
;
2031 struct p2p_oper_class_map op_class
[] = {
2032 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1 },
2033 { HOSTAPD_MODE_IEEE80211G
, 82, 14, 14, 1 },
2034 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4 },
2035 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4 },
2036 #if 0 /* TODO: 40 MHz channels */
2037 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1 },
2038 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1 },
2039 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8 },
2040 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8 },
2041 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8 },
2042 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8 },
2047 modes
= wpa_drv_get_hw_feature_data(wpa_s
, &num_modes
, &flags
);
2048 if (modes
== NULL
) {
2049 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
2050 "of all supported channels; assume dualband "
2052 return wpas_p2p_default_channels(wpa_s
, chan
);
2057 for (op
= 0; op_class
[op
].op_class
; op
++) {
2058 struct p2p_oper_class_map
*o
= &op_class
[op
];
2060 struct p2p_reg_class
*reg
= NULL
;
2062 mode
= get_mode(modes
, num_modes
, o
->mode
);
2065 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
2066 if (!has_channel(mode
, ch
))
2069 wpa_printf(MSG_DEBUG
, "P2P: Add operating "
2070 "class %u", o
->op_class
);
2071 reg
= &chan
->reg_class
[cla
];
2073 reg
->reg_class
= o
->op_class
;
2075 reg
->channel
[reg
->channels
] = ch
;
2079 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
2080 reg
->channel
, reg
->channels
);
2084 chan
->reg_classes
= cla
;
2086 ieee80211_sta_free_hw_features(modes
, num_modes
);
2092 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
2095 struct wpa_supplicant
*wpa_s
= ctx
;
2097 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2098 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
2104 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
2109 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2110 * @global: Pointer to global data from wpa_supplicant_init()
2111 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2112 * Returns: 0 on success, -1 on failure
2114 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
2116 struct p2p_config p2p
;
2120 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
2123 #ifdef CONFIG_CLIENT_MLME
2124 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)) {
2125 wpa_s
->mlme
.public_action_cb
= p2p_rx_action_mlme
;
2126 wpa_s
->mlme
.public_action_cb_ctx
= wpa_s
;
2128 #endif /* CONFIG_CLIENT_MLME */
2130 if (wpa_drv_disable_11b_rates(wpa_s
, 1) < 0) {
2131 wpa_printf(MSG_DEBUG
, "P2P: Failed to disable 11b rates");
2132 /* Continue anyway; this is not really a fatal error */
2138 os_memset(&p2p
, 0, sizeof(p2p
));
2139 p2p
.msg_ctx
= wpa_s
;
2141 p2p
.p2p_scan
= wpas_p2p_scan
;
2142 p2p
.send_action
= wpas_send_action
;
2143 p2p
.send_action_done
= wpas_send_action_done
;
2144 p2p
.go_neg_completed
= wpas_go_neg_completed
;
2145 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
2146 p2p
.dev_found
= wpas_dev_found
;
2147 p2p
.start_listen
= wpas_start_listen
;
2148 p2p
.stop_listen
= wpas_stop_listen
;
2149 p2p
.send_probe_resp
= wpas_send_probe_resp
;
2150 p2p
.sd_request
= wpas_sd_request
;
2151 p2p
.sd_response
= wpas_sd_response
;
2152 p2p
.prov_disc_req
= wpas_prov_disc_req
;
2153 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
2154 p2p
.invitation_process
= wpas_invitation_process
;
2155 p2p
.invitation_received
= wpas_invitation_received
;
2156 p2p
.invitation_result
= wpas_invitation_result
;
2157 p2p
.get_noa
= wpas_get_noa
;
2159 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2160 os_memcpy(p2p
.dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2161 p2p
.dev_name
= wpa_s
->conf
->device_name
;
2163 if (wpa_s
->conf
->p2p_listen_reg_class
&&
2164 wpa_s
->conf
->p2p_listen_channel
) {
2165 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
2166 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
2170 * Pick one of the social channels randomly as the listen
2173 os_get_random((u8
*) &r
, sizeof(r
));
2174 p2p
.channel
= 1 + (r
% 3) * 5;
2177 if (wpa_s
->conf
->p2p_oper_reg_class
&&
2178 wpa_s
->conf
->p2p_oper_channel
) {
2179 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
2180 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
2182 p2p
.op_reg_class
= 81;
2184 * For initial tests, pick the operation channel randomly.
2185 * TODO: Use scan results (etc.) to select the best channel.
2187 p2p
.op_channel
= 1 + r
% 11;
2189 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d "
2190 "Own preferred operation channel: %d",
2191 p2p
.channel
, p2p
.op_channel
);
2192 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
2193 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
2194 p2p
.country
[2] = 0x04;
2196 os_memcpy(p2p
.country
, "US\x04", 3);
2198 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
)) {
2199 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
2204 if (wpa_s
->conf
->device_type
&&
2205 wps_dev_type_str2bin(wpa_s
->conf
->device_type
, p2p
.pri_dev_type
) <
2207 wpa_printf(MSG_ERROR
, "P2P: Invalid device_type");
2211 for (i
= 0; i
< MAX_SEC_DEVICE_TYPES
; i
++) {
2212 if (wpa_s
->conf
->sec_device_type
[i
] == NULL
)
2214 if (wps_dev_type_str2bin(
2215 wpa_s
->conf
->sec_device_type
[i
],
2216 p2p
.sec_dev_type
[p2p
.num_sec_dev_types
]) < 0) {
2217 wpa_printf(MSG_ERROR
, "P2P: Invalid sec_device_type");
2220 p2p
.num_sec_dev_types
++;
2221 if (p2p
.num_sec_dev_types
== P2P_SEC_DEVICE_TYPES
)
2225 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
2226 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
2228 p2p
.max_peers
= 100;
2230 if (wpa_s
->conf
->p2p_ssid_postfix
) {
2231 p2p
.ssid_postfix_len
=
2232 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
2233 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
2234 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
2235 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
2236 p2p
.ssid_postfix_len
);
2239 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
2241 global
->p2p
= p2p_init(&p2p
);
2242 if (global
->p2p
== NULL
)
2250 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2251 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2253 * This function deinitialize per-interface P2P data.
2255 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
2257 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
2258 wpa_drv_probe_req_report(wpa_s
, 0);
2259 os_free(wpa_s
->go_params
);
2260 wpa_s
->go_params
= NULL
;
2261 wpabuf_free(wpa_s
->pending_action_tx
);
2262 wpa_s
->pending_action_tx
= NULL
;
2263 eloop_cancel_timeout(wpas_send_action_cb
, wpa_s
, NULL
);
2264 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2265 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2266 wpa_s
->p2p_long_listen
= 0;
2267 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
2268 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
2269 wpas_p2p_remove_pending_group_interface(wpa_s
);
2271 /* TODO: remove group interface from the driver if this wpa_s instance
2272 * is on top of a P2P group interface */
2277 * wpas_p2p_deinit_global - Deinitialize global P2P module
2278 * @global: Pointer to global data from wpa_supplicant_init()
2280 * This function deinitializes the global (per device) P2P module.
2282 void wpas_p2p_deinit_global(struct wpa_global
*global
)
2284 struct wpa_supplicant
*wpa_s
, *tmp
;
2287 if (global
->p2p
== NULL
)
2290 /* Remove remaining P2P group interfaces */
2291 wpa_s
= global
->ifaces
;
2293 wpas_p2p_service_flush(wpa_s
);
2294 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
2295 wpa_s
= wpa_s
->next
;
2297 enum wpa_driver_if_type type
;
2298 tmp
= global
->ifaces
;
2301 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
2306 ifname
= os_strdup(tmp
->ifname
);
2307 type
= wpas_p2p_if_type(tmp
->p2p_group_interface
);
2308 wpa_supplicant_remove_iface(global
, tmp
);
2310 wpa_drv_if_remove(wpa_s
, type
, ifname
);
2315 * Deinit GO data on any possibly remaining interface (if main
2316 * interface is used as GO).
2318 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2319 if (wpa_s
->ap_iface
)
2320 wpas_p2p_group_deinit(wpa_s
);
2323 p2p_deinit(global
->p2p
);
2328 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
2330 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
2331 return 1; /* P2P group requires a new interface in every case
2333 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
2334 return 0; /* driver does not support concurrent operations */
2335 if (wpa_s
->global
->ifaces
->next
)
2336 return 1; /* more that one interface already in use */
2337 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
2338 return 1; /* this interface is already in use */
2343 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
2344 const u8
*peer_addr
,
2345 enum p2p_wps_method wps_method
,
2346 int go_intent
, const u8
*own_interface_addr
,
2347 unsigned int force_freq
, int persistent_group
)
2349 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2350 go_intent
, own_interface_addr
, force_freq
,
2355 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
2356 const u8
*peer_addr
,
2357 enum p2p_wps_method wps_method
,
2358 int go_intent
, const u8
*own_interface_addr
,
2359 unsigned int force_freq
, int persistent_group
)
2361 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
2362 go_intent
, own_interface_addr
, force_freq
,
2367 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
2368 struct wpa_scan_results
*scan_res
)
2370 struct wpa_bss
*bss
;
2373 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2375 if (wpa_s
->global
->p2p_disabled
)
2378 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for join",
2379 scan_res
? (int) scan_res
->num
: -1);
2382 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
2384 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
2385 wpa_s
->pending_join_iface_addr
);
2387 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2388 "from P2P peer table: %d MHz", freq
);
2390 bss
= wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
);
2393 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
2394 "from BSS table: %d MHz", freq
);
2399 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
2400 "prior to joining an existing group (GO " MACSTR
2402 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
2403 wpa_s
->pending_pd_before_join
= 1;
2405 switch (wpa_s
->pending_join_wps_method
) {
2407 case WPS_PIN_DISPLAY
:
2408 method
= WPS_CONFIG_KEYPAD
;
2410 case WPS_PIN_KEYPAD
:
2411 method
= WPS_CONFIG_DISPLAY
;
2414 method
= WPS_CONFIG_PUSHBUTTON
;
2421 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
2422 wpa_s
->pending_join_dev_addr
, method
, 1)
2424 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
2425 "Discovery Request before joining an "
2427 wpa_s
->pending_pd_before_join
= 0;
2432 * Actual join operation will be started from the Action frame
2433 * TX status callback.
2438 wpa_printf(MSG_DEBUG
, "P2P: Target BSS/GO not yet in BSS table - "
2439 "cannot send Provision Discovery Request");
2442 /* Start join operation immediately */
2443 wpas_p2p_join_start(wpa_s
);
2447 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
2449 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
2451 struct wpa_driver_scan_params params
;
2452 struct wpabuf
*wps_ie
, *ies
;
2454 os_memset(¶ms
, 0, sizeof(params
));
2456 /* P2P Wildcard SSID */
2457 params
.num_ssids
= 1;
2458 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
2459 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
2461 wpa_s
->wps
->dev
.p2p
= 1;
2462 wps_ie
= wps_build_probe_req_ie(0, &wpa_s
->wps
->dev
, wpa_s
->wps
->uuid
,
2464 if (wps_ie
== NULL
) {
2465 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2469 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + 100);
2471 wpabuf_free(wps_ie
);
2472 wpas_p2p_scan_res_join(wpa_s
, NULL
);
2475 wpabuf_put_buf(ies
, wps_ie
);
2476 wpabuf_free(wps_ie
);
2478 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
2480 params
.extra_ies
= wpabuf_head(ies
);
2481 params
.extra_ies_len
= wpabuf_len(ies
);
2484 * Run a scan to update BSS table and start Provision Discovery once
2485 * the new scan results become available.
2487 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
2488 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_USER_SPACE_MLME
)
2489 ret
= ieee80211_sta_req_scan(wpa_s
, ¶ms
);
2491 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
2496 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
2498 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
2499 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
2504 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
2505 const u8
*dev_addr
, enum p2p_wps_method wps_method
)
2507 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
2508 MACSTR
" dev " MACSTR
")",
2509 MAC2STR(iface_addr
), MAC2STR(dev_addr
));
2511 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
2512 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
2513 wpa_s
->pending_join_wps_method
= wps_method
;
2515 /* Make sure we are not running find during connection establishment */
2516 wpas_p2p_stop_find(wpa_s
);
2518 wpas_p2p_join_scan(wpa_s
, NULL
);
2523 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
2525 struct wpa_supplicant
*group
;
2526 struct p2p_go_neg_results res
;
2528 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
2531 if (group
!= wpa_s
) {
2532 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
2533 sizeof(group
->p2p_pin
));
2534 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
2537 group
->p2p_in_provisioning
= 1;
2539 os_memset(&res
, 0, sizeof(res
));
2540 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
2542 res
.wps_method
= wpa_s
->pending_join_wps_method
;
2543 wpas_start_wps_enrollee(group
, &res
);
2546 * Allow a longer timeout for join-a-running-group than normal 15
2547 * second group formation timeout since the GO may not have authorized
2548 * our connection yet.
2550 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
2551 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
2559 * wpas_p2p_connect - Request P2P Group Formation to be started
2560 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2561 * @peer_addr: Address of the peer P2P Device
2562 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2563 * @persistent_group: Whether to create a persistent group
2564 * @join: Whether to join an existing group (as a client) instead of starting
2565 * Group Owner negotiation; @peer_addr is BSSID in that case
2566 * @auth: Whether to only authorize the connection instead of doing that and
2567 * initiating Group Owner negotiation
2568 * @go_intent: GO Intent or -1 to use default
2569 * @freq: Frequency for the group or 0 for auto-selection
2570 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2571 * failure, -2 on failure due to channel not currently available,
2572 * -3 if forced channel is not supported
2574 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
2575 const char *pin
, enum p2p_wps_method wps_method
,
2576 int persistent_group
, int join
, int auth
, int go_intent
,
2579 int force_freq
= 0, oper_freq
= 0;
2582 enum wpa_driver_if_type iftype
;
2585 go_intent
= wpa_s
->conf
->p2p_go_intent
;
2588 wpa_s
->p2p_long_listen
= 0;
2590 wpa_s
->p2p_wps_method
= wps_method
;
2593 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
2594 else if (wps_method
== WPS_PIN_DISPLAY
) {
2595 ret
= wps_generate_pin();
2596 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
2598 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
2601 wpa_s
->p2p_pin
[0] = '\0';
2604 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
2606 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
2607 "connect a running group from " MACSTR
,
2608 MAC2STR(peer_addr
));
2609 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
2612 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
2613 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
2615 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
2616 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
2619 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
) <
2625 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
2627 oper_freq
= wpa_s
->assoc_freq
;
2629 oper_freq
= wpa_drv_shared_freq(wpa_s
);
2635 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
2636 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
2637 "(%u MHz) is not supported for P2P uses",
2642 if (oper_freq
> 0 && freq
!= oper_freq
&&
2643 !(wpa_s
->drv_flags
&
2644 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2645 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2646 "on %u MHz while connected on another "
2647 "channel (%u MHz)", freq
, oper_freq
);
2650 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2651 "requested channel (%u MHz)", freq
);
2653 } else if (oper_freq
> 0 &&
2654 !p2p_supported_freq(wpa_s
->global
->p2p
, oper_freq
)) {
2655 if (!(wpa_s
->drv_flags
&
2656 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
2657 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
2658 "while connected on non-P2P supported "
2659 "channel (%u MHz)", oper_freq
);
2662 wpa_printf(MSG_DEBUG
, "P2P: Current operating channel "
2663 "(%u MHz) not available for P2P - try to use "
2664 "another channel", oper_freq
);
2666 } else if (oper_freq
> 0) {
2667 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
2668 "channel we are already using (%u MHz) on another "
2669 "interface", oper_freq
);
2670 force_freq
= oper_freq
;
2673 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
2675 if (!wpa_s
->create_p2p_iface
) {
2677 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2678 go_intent
, wpa_s
->own_addr
,
2679 force_freq
, persistent_group
)
2684 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
2685 go_intent
, wpa_s
->own_addr
,
2686 force_freq
, persistent_group
) < 0)
2691 /* Prepare to add a new interface for the group */
2692 iftype
= WPA_IF_P2P_GROUP
;
2694 iftype
= WPA_IF_P2P_CLIENT
;
2695 else if (go_intent
== 15)
2696 iftype
= WPA_IF_P2P_GO
;
2697 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
2698 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2699 "interface for the group");
2704 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
2706 wpa_s
->pending_interface_addr
,
2707 force_freq
, persistent_group
) < 0)
2711 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
, go_intent
,
2712 wpa_s
->pending_interface_addr
,
2713 force_freq
, persistent_group
) < 0) {
2714 wpas_p2p_remove_pending_group_interface(wpa_s
);
2722 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2723 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2724 * @freq: Frequency of the channel in MHz
2725 * @duration: Duration of the stay on the channel in milliseconds
2727 * This callback is called when the driver indicates that it has started the
2728 * requested remain-on-channel duration.
2730 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
2731 unsigned int freq
, unsigned int duration
)
2733 wpa_s
->roc_waiting_drv_freq
= 0;
2734 wpa_s
->off_channel_freq
= freq
;
2735 wpas_send_action_cb(wpa_s
, NULL
);
2736 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
2737 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
2738 wpa_s
->pending_listen_duration
);
2739 wpa_s
->pending_listen_freq
= 0;
2744 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
2745 unsigned int timeout
)
2747 /* Limit maximum Listen state time based on driver limitation. */
2748 if (timeout
> wpa_s
->max_remain_on_chan
)
2749 timeout
= wpa_s
->max_remain_on_chan
;
2751 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
2756 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2757 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2758 * @freq: Frequency of the channel in MHz
2760 * This callback is called when the driver indicates that a remain-on-channel
2761 * operation has been completed, i.e., the duration on the requested channel
2764 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
2767 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
2768 "(p2p_long_listen=%d pending_action_tx=%p)",
2769 wpa_s
->p2p_long_listen
, wpa_s
->pending_action_tx
);
2770 wpa_s
->off_channel_freq
= 0;
2771 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
2772 return; /* P2P module started a new operation */
2773 if (wpa_s
->pending_action_tx
)
2775 if (wpa_s
->p2p_long_listen
> 0)
2776 wpa_s
->p2p_long_listen
-= 5;
2777 if (wpa_s
->p2p_long_listen
> 0) {
2778 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
2779 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
* 1000);
2785 * wpas_p2p_group_remove - Remove a P2P group
2786 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2787 * @ifname: Network interface name of the group interface or "*" to remove all
2789 * Returns: 0 on success, -1 on failure
2791 * This function is used to remove a P2P group. This can be used to disconnect
2792 * from a group in which the local end is a P2P Client or to end a P2P Group in
2793 * case the local end is the Group Owner. If a virtual network interface was
2794 * created for this group, that interface will be removed. Otherwise, only the
2795 * configured P2P group network will be removed from the interface.
2797 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
2799 struct wpa_global
*global
= wpa_s
->global
;
2801 if (os_strcmp(ifname
, "*") == 0) {
2802 struct wpa_supplicant
*prev
;
2803 wpa_s
= global
->ifaces
;
2806 wpa_s
= wpa_s
->next
;
2807 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_REQUESTED
;
2808 wpas_p2p_group_delete(prev
);
2813 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2814 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
2821 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_REQUESTED
;
2822 wpas_p2p_group_delete(wpa_s
);
2828 static void wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
2829 struct p2p_go_neg_results
*params
,
2835 os_memset(params
, 0, sizeof(*params
));
2836 params
->role_go
= 1;
2837 params
->freq
= 2412;
2839 params
->freq
= freq
;
2840 else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
2841 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
2842 wpa_s
->conf
->p2p_oper_channel
<= 11)
2843 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
2844 else if (wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
2845 wpa_s
->conf
->p2p_oper_reg_class
== 118)
2846 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
2847 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
2848 wpa_s
->assoc_freq
&& !freq
) {
2849 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
2851 params
->freq
= wpa_s
->assoc_freq
;
2854 res
= wpa_drv_shared_freq(wpa_s
);
2855 if (res
> 0 && !freq
) {
2856 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
2857 "already using on a shared interface");
2863 static struct wpa_supplicant
*
2864 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
2867 struct wpa_supplicant
*group_wpa_s
;
2869 if (!wpas_p2p_create_iface(wpa_s
))
2872 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
2873 WPA_IF_P2P_CLIENT
) < 0)
2875 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
2876 if (group_wpa_s
== NULL
) {
2877 wpas_p2p_remove_pending_group_interface(wpa_s
);
2886 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
2887 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2888 * @persistent_group: Whether to create a persistent group
2889 * @freq: Frequency for the group or 0 to indicate no hardcoding
2890 * Returns: 0 on success, -1 on failure
2892 * This function creates a new P2P group with the local end as the Group Owner,
2893 * i.e., without using Group Owner Negotiation.
2895 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
2898 struct p2p_go_neg_results params
;
2900 if (freq
> 0 && !p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
2901 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
2902 "(%u MHz) is not supported for P2P uses",
2907 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
2908 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
2909 params
.persistent_group
= persistent_group
;
2911 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
2914 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
2920 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
2921 struct wpa_ssid
*params
, int addr_allocated
)
2923 struct wpa_ssid
*ssid
;
2925 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
2929 wpa_supplicant_ap_deinit(wpa_s
);
2931 ssid
= wpa_config_add_network(wpa_s
->conf
);
2934 wpas_notify_network_added(wpa_s
, ssid
);
2935 wpa_config_set_network_defaults(ssid
);
2936 ssid
->temporary
= 1;
2937 ssid
->proto
= WPA_PROTO_RSN
;
2938 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
2939 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
2940 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
2941 ssid
->ssid
= os_malloc(params
->ssid_len
);
2942 if (ssid
->ssid
== NULL
) {
2943 wpas_notify_network_removed(wpa_s
, ssid
);
2944 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
2947 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
2948 ssid
->ssid_len
= params
->ssid_len
;
2949 ssid
->p2p_group
= 1;
2950 if (params
->psk_set
) {
2951 os_memcpy(ssid
->psk
, params
->psk
, 32);
2954 if (params
->passphrase
)
2955 ssid
->passphrase
= os_strdup(params
->passphrase
);
2957 wpa_supplicant_select_network(wpa_s
, ssid
);
2959 wpa_s
->show_group_started
= 1;
2965 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
2966 struct wpa_ssid
*ssid
, int addr_allocated
,
2969 struct p2p_go_neg_results params
;
2971 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
2974 wpa_s
->p2p_long_listen
= 0;
2975 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
2977 if (ssid
->mode
== WPAS_MODE_INFRA
)
2978 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
2980 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
2983 wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
);
2986 if (ssid
->passphrase
== NULL
||
2987 os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
2988 wpa_printf(MSG_DEBUG
, "P2P: Invalid passphrase in persistent "
2992 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
2993 sizeof(params
.passphrase
));
2994 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
2995 params
.ssid_len
= ssid
->ssid_len
;
2996 params
.persistent_group
= 1;
2998 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
3002 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
3008 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
3009 struct wpabuf
*proberesp_ies
)
3011 struct wpa_supplicant
*wpa_s
= ctx
;
3012 if (wpa_s
->ap_iface
) {
3013 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
3015 wpabuf_free(hapd
->p2p_beacon_ie
);
3016 hapd
->p2p_beacon_ie
= beacon_ies
;
3018 wpabuf_free(hapd
->p2p_probe_resp_ie
);
3019 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
3021 wpabuf_free(beacon_ies
);
3022 wpabuf_free(proberesp_ies
);
3024 wpa_supplicant_ap_update_beacon(wpa_s
);
3028 static void wpas_p2p_idle_update(void *ctx
, int idle
)
3030 struct wpa_supplicant
*wpa_s
= ctx
;
3031 if (!wpa_s
->ap_iface
)
3033 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
3035 wpas_p2p_set_group_idle_timeout(wpa_s
);
3037 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3041 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
3042 int persistent_group
,
3043 int group_formation
)
3045 struct p2p_group
*group
;
3046 struct p2p_group_config
*cfg
;
3048 cfg
= os_zalloc(sizeof(*cfg
));
3052 cfg
->persistent_group
= persistent_group
;
3053 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
3054 if (wpa_s
->max_stations
&&
3055 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
3056 cfg
->max_clients
= wpa_s
->max_stations
;
3058 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
3059 cfg
->cb_ctx
= wpa_s
;
3060 cfg
->ie_update
= wpas_p2p_ie_update
;
3061 cfg
->idle_update
= wpas_p2p_idle_update
;
3063 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
3066 if (!group_formation
)
3067 p2p_group_notif_formation_done(group
);
3068 wpa_s
->p2p_group
= group
;
3073 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3076 if (!wpa_s
->p2p_in_provisioning
) {
3077 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
3078 "provisioning not in progress");
3082 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
3084 if (wpa_s
->global
->p2p
)
3085 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
3086 wpas_group_formation_completed(wpa_s
, 1);
3090 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3091 const char *config_method
)
3095 if (os_strcmp(config_method
, "display") == 0)
3096 config_methods
= WPS_CONFIG_DISPLAY
;
3097 else if (os_strcmp(config_method
, "keypad") == 0)
3098 config_methods
= WPS_CONFIG_KEYPAD
;
3099 else if (os_strcmp(config_method
, "pbc") == 0 ||
3100 os_strcmp(config_method
, "pushbutton") == 0)
3101 config_methods
= WPS_CONFIG_PUSHBUTTON
;
3105 if (wpa_s
->global
->p2p
== NULL
)
3108 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
3113 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
3116 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
3120 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
3122 if (!wpa_s
->pending_action_tx
)
3125 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
3126 "operation request");
3127 wpabuf_free(wpa_s
->pending_action_tx
);
3128 wpa_s
->pending_action_tx
= NULL
;
3132 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
3133 enum p2p_discovery_type type
)
3135 wpas_p2p_clear_pending_action_tx(wpa_s
);
3136 wpa_s
->p2p_long_listen
= 0;
3138 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
);
3142 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
3144 wpas_p2p_clear_pending_action_tx(wpa_s
);
3145 wpa_s
->p2p_long_listen
= 0;
3146 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3148 p2p_stop_find(wpa_s
->global
->p2p
);
3150 wpas_p2p_remove_pending_group_interface(wpa_s
);
3154 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
3156 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3157 wpa_s
->p2p_long_listen
= 0;
3161 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
3165 wpas_p2p_clear_pending_action_tx(wpa_s
);
3169 * This is a request for unlimited Listen state. However, at
3170 * least for now, this is mapped to a Listen state for one
3175 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3176 wpa_s
->p2p_long_listen
= 0;
3178 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
3179 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
3180 wpa_s
->p2p_long_listen
= timeout
;
3181 eloop_register_timeout(timeout
, 0,
3182 wpas_p2p_long_listen_timeout
,
3190 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
3191 u8
*buf
, size_t len
, int p2p_group
)
3193 struct wpabuf
*p2p_ie
;
3196 if (wpa_s
->global
->p2p_disabled
)
3198 if (wpa_s
->global
->p2p
== NULL
)
3203 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
3204 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
3206 wpabuf_free(p2p_ie
);
3212 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
3213 const u8
*ie
, size_t ie_len
)
3215 if (wpa_s
->global
->p2p_disabled
)
3217 if (wpa_s
->global
->p2p
== NULL
)
3220 return p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, ie
, ie_len
);
3224 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
3225 const u8
*sa
, const u8
*bssid
,
3226 u8 category
, const u8
*data
, size_t len
, int freq
)
3228 if (wpa_s
->global
->p2p_disabled
)
3230 if (wpa_s
->global
->p2p
== NULL
)
3233 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
3238 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
3240 if (wpa_s
->global
->p2p_disabled
)
3242 if (wpa_s
->global
->p2p
== NULL
)
3245 p2p_scan_ie(wpa_s
->global
->p2p
, ies
);
3249 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
3251 p2p_group_deinit(wpa_s
->p2p_group
);
3252 wpa_s
->p2p_group
= NULL
;
3256 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
3258 wpa_s
->p2p_long_listen
= 0;
3260 return p2p_reject(wpa_s
->global
->p2p
, addr
);
3264 /* Invite to reinvoke a persistent group */
3265 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3266 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
)
3268 enum p2p_invite_role role
;
3271 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3272 role
= P2P_INVITE_ROLE_GO
;
3273 if (peer_addr
== NULL
) {
3274 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
3275 "address in invitation command");
3278 if (wpas_p2p_create_iface(wpa_s
)) {
3279 if (wpas_p2p_add_group_interface(wpa_s
,
3280 WPA_IF_P2P_GO
) < 0) {
3281 wpa_printf(MSG_ERROR
, "P2P: Failed to "
3282 "allocate a new interface for the "
3286 bssid
= wpa_s
->pending_interface_addr
;
3288 bssid
= wpa_s
->own_addr
;
3290 role
= P2P_INVITE_ROLE_CLIENT
;
3291 peer_addr
= ssid
->bssid
;
3293 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
3295 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3296 ssid
->ssid
, ssid
->ssid_len
, 0, go_dev_addr
, 1);
3300 /* Invite to join an active group */
3301 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
3302 const u8
*peer_addr
, const u8
*go_dev_addr
)
3304 struct wpa_global
*global
= wpa_s
->global
;
3305 enum p2p_invite_role role
;
3307 struct wpa_ssid
*ssid
;
3309 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3310 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3313 if (wpa_s
== NULL
) {
3314 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
3318 ssid
= wpa_s
->current_ssid
;
3320 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
3325 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
3326 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
3327 bssid
= wpa_s
->own_addr
;
3328 if (go_dev_addr
== NULL
)
3329 go_dev_addr
= wpa_s
->parent
->own_addr
;
3331 role
= P2P_INVITE_ROLE_CLIENT
;
3332 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
3333 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
3334 "invite to current group");
3337 bssid
= wpa_s
->bssid
;
3338 if (go_dev_addr
== NULL
&&
3339 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
3340 go_dev_addr
= wpa_s
->go_dev_addr
;
3342 wpa_s
->pending_invite_ssid_id
= -1;
3344 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
3345 ssid
->ssid
, ssid
->ssid_len
, wpa_s
->assoc_freq
,
3350 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
3352 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
3353 const char *ssid_txt
;
3354 u8 go_dev_addr
[ETH_ALEN
];
3357 if (!wpa_s
->show_group_started
|| !ssid
)
3360 wpa_s
->show_group_started
= 0;
3362 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
3363 os_memset(go_dev_addr
, 0, ETH_ALEN
);
3364 if (ssid
->bssid_set
)
3365 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
3366 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
3368 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
3370 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
3372 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
3373 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3374 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3376 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
3377 MAC2STR(go_dev_addr
),
3378 persistent
? " [PERSISTENT]" : "");
3380 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
3381 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3382 "go_dev_addr=" MACSTR
"%s",
3383 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
,
3384 ssid
->passphrase
? ssid
->passphrase
: "",
3385 MAC2STR(go_dev_addr
),
3386 persistent
? " [PERSISTENT]" : "");
3390 wpas_p2p_store_persistent_group(wpa_s
->parent
, ssid
,
3395 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
3396 u32 interval1
, u32 duration2
, u32 interval2
)
3398 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
3399 wpa_s
->current_ssid
== NULL
||
3400 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
3403 return p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
3404 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
3405 duration1
, interval1
, duration2
, interval2
);
3409 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
3410 unsigned int interval
)
3412 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
3416 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
3418 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3420 if (wpa_s
->conf
->p2p_group_idle
== 0) {
3421 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
3426 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
3428 wpa_s
->removal_reason
= P2P_GROUP_REMOVAL_IDLE_TIMEOUT
;
3429 wpas_p2p_group_delete(wpa_s
);
3433 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
3435 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3436 if (wpa_s
->conf
->p2p_group_idle
== 0)
3439 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
3442 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
3443 wpa_s
->conf
->p2p_group_idle
);
3444 eloop_register_timeout(wpa_s
->conf
->p2p_group_idle
, 0,
3445 wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3449 void wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3450 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3452 if (wpa_s
->global
->p2p_disabled
)
3455 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3459 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
3460 u16 reason_code
, const u8
*ie
, size_t ie_len
)
3462 if (wpa_s
->global
->p2p_disabled
)
3465 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
, ie_len
);
3469 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
3471 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
3476 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
3479 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
3480 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
3482 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
) {
3484 if (wpa_s
->conf
->device_type
) {
3485 if (wps_dev_type_str2bin(wpa_s
->conf
->device_type
,
3486 pri_dev_type
) < 0) {
3487 wpa_printf(MSG_ERROR
, "P2P: Invalid "
3490 p2p_set_pri_dev_type(p2p
, pri_dev_type
);
3494 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
) {
3495 u8 sec_dev_type
[P2P_SEC_DEVICE_TYPES
][8];
3498 for (i
= 0; i
< MAX_SEC_DEVICE_TYPES
; i
++) {
3499 if (wpa_s
->conf
->sec_device_type
[i
] == NULL
)
3501 if (wps_dev_type_str2bin(
3502 wpa_s
->conf
->sec_device_type
[i
],
3503 sec_dev_type
[num
]) < 0) {
3504 wpa_printf(MSG_ERROR
, "P2P: Invalid "
3509 if (num
== P2P_SEC_DEVICE_TYPES
)
3512 p2p_set_sec_dev_types(p2p
, (void *) sec_dev_type
, num
);
3515 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
3516 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
3518 country
[0] = wpa_s
->conf
->country
[0];
3519 country
[1] = wpa_s
->conf
->country
[1];
3521 p2p_set_country(p2p
, country
);
3524 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
3525 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
3526 wpa_s
->conf
->p2p_ssid_postfix
?
3527 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
3531 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
3532 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
3536 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
3539 if (!wpa_s
->ap_iface
)
3541 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
3546 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
3548 if (wpa_s
->global
->p2p_disabled
)
3550 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3553 wpa_s
->global
->cross_connection
= enabled
;
3554 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
3557 struct wpa_supplicant
*iface
;
3559 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
3561 if (iface
->cross_connect_enabled
== 0)
3564 iface
->cross_connect_enabled
= 0;
3565 iface
->cross_connect_in_use
= 0;
3566 wpa_msg(iface
->parent
, MSG_INFO
,
3567 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
3568 iface
->ifname
, iface
->cross_connect_uplink
);
3576 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
3578 struct wpa_supplicant
*iface
;
3580 if (!uplink
->global
->cross_connection
)
3583 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
3584 if (!iface
->cross_connect_enabled
)
3586 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
3589 if (iface
->ap_iface
== NULL
)
3591 if (iface
->cross_connect_in_use
)
3594 iface
->cross_connect_in_use
= 1;
3595 wpa_msg(iface
->parent
, MSG_INFO
,
3596 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
3597 iface
->ifname
, iface
->cross_connect_uplink
);
3602 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
3604 struct wpa_supplicant
*iface
;
3606 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
3607 if (!iface
->cross_connect_enabled
)
3609 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
3612 if (!iface
->cross_connect_in_use
)
3615 wpa_msg(iface
->parent
, MSG_INFO
,
3616 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
3617 iface
->ifname
, iface
->cross_connect_uplink
);
3618 iface
->cross_connect_in_use
= 0;
3623 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
3625 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
3626 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
3627 wpa_s
->cross_connect_disallowed
)
3628 wpas_p2p_disable_cross_connect(wpa_s
);
3630 wpas_p2p_enable_cross_connect(wpa_s
);
3631 if (!wpa_s
->ap_iface
)
3632 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3636 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
3638 wpas_p2p_disable_cross_connect(wpa_s
);
3639 if (!wpa_s
->ap_iface
)
3640 wpas_p2p_set_group_idle_timeout(wpa_s
);
3644 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
3646 struct wpa_supplicant
*iface
;
3648 if (!wpa_s
->global
->cross_connection
)
3651 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
3654 if (iface
->drv_flags
&
3655 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
3657 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
3660 wpa_s
->cross_connect_enabled
= 1;
3661 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
3662 sizeof(wpa_s
->cross_connect_uplink
));
3663 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
3664 "%s to %s whenever uplink is available",
3665 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
3667 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
3668 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
3669 iface
->cross_connect_disallowed
||
3670 iface
->wpa_state
!= WPA_COMPLETED
)
3673 wpa_s
->cross_connect_in_use
= 1;
3674 wpa_msg(wpa_s
->parent
, MSG_INFO
,
3675 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
3676 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
3682 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
3684 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
3685 !wpa_s
->p2p_in_provisioning
)
3686 return 0; /* not P2P client operation */
3688 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
3690 wpas_group_formation_completed(wpa_s
, 0);
3695 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
3697 struct p2p_channels chan
;
3699 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
3702 os_memset(&chan
, 0, sizeof(chan
));
3703 if (wpas_p2p_setup_channels(wpa_s
, &chan
)) {
3704 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
3709 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
);
3713 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
3715 struct wpa_global
*global
= wpa_s
->global
;
3718 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
3720 if (wpa_s
->pending_interface_name
[0] &&
3721 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
3724 wpas_p2p_stop_find(wpa_s
);
3726 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3727 if (wpa_s
== global
->p2p_group_formation
&&
3728 wpa_s
->p2p_in_provisioning
) {
3729 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
3730 "formation found - cancelling",
3733 wpas_p2p_group_delete(wpa_s
);
3739 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");