2 * wpa_supplicant / WPS integration
3 * Copyright (c) 2008-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 #include "crypto/random.h"
15 #include "crypto/dh_group5.h"
16 #include "common/ieee802_11_defs.h"
17 #include "common/ieee802_11_common.h"
18 #include "common/wpa_common.h"
19 #include "common/wpa_ctrl.h"
20 #include "eap_common/eap_wsc_common.h"
21 #include "eap_peer/eap.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "rsn_supp/wpa.h"
24 #include "wps/wps_attr_parse.h"
26 #include "wpa_supplicant_i.h"
29 #include "blacklist.h"
34 #include "p2p_supplicant.h"
35 #include "wps_supplicant.h"
38 #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
39 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
40 #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
42 static void wpas_wps_timeout(void *eloop_ctx
, void *timeout_ctx
);
43 static void wpas_clear_wps(struct wpa_supplicant
*wpa_s
);
46 static void wpas_wps_clear_ap_info(struct wpa_supplicant
*wpa_s
)
48 os_free(wpa_s
->wps_ap
);
50 wpa_s
->num_wps_ap
= 0;
51 wpa_s
->wps_ap_iter
= 0;
55 int wpas_wps_eapol_cb(struct wpa_supplicant
*wpa_s
)
58 if (wpas_p2p_wps_eapol_cb(wpa_s
) > 0)
60 #endif /* CONFIG_P2P */
62 if (!wpa_s
->wps_success
&&
63 wpa_s
->current_ssid
&&
64 eap_is_wps_pin_enrollee(&wpa_s
->current_ssid
->eap
)) {
65 const u8
*bssid
= wpa_s
->bssid
;
66 if (is_zero_ether_addr(bssid
))
67 bssid
= wpa_s
->pending_bssid
;
69 wpa_printf(MSG_DEBUG
, "WPS: PIN registration with " MACSTR
70 " did not succeed - continue trying to find "
71 "suitable AP", MAC2STR(bssid
));
72 wpa_blacklist_add(wpa_s
, bssid
);
74 wpa_supplicant_deauthenticate(wpa_s
,
75 WLAN_REASON_DEAUTH_LEAVING
);
76 wpa_s
->reassociate
= 1;
77 wpa_supplicant_req_scan(wpa_s
,
78 wpa_s
->blacklist_cleared
? 5 : 0, 0);
79 wpa_s
->blacklist_cleared
= 0;
83 wpas_wps_clear_ap_info(wpa_s
);
84 eloop_cancel_timeout(wpas_wps_timeout
, wpa_s
, NULL
);
85 if (wpa_s
->key_mgmt
== WPA_KEY_MGMT_WPS
&& !wpa_s
->wps_success
)
86 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_FAIL
);
88 if (wpa_s
->key_mgmt
== WPA_KEY_MGMT_WPS
&& wpa_s
->current_ssid
&&
89 !(wpa_s
->current_ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)) {
90 int disabled
= wpa_s
->current_ssid
->disabled
;
91 unsigned int freq
= wpa_s
->assoc_freq
;
93 struct wpa_ssid
*ssid
= NULL
;
94 int use_fast_assoc
= 0;
96 wpa_printf(MSG_DEBUG
, "WPS: Network configuration replaced - "
97 "try to associate with the received credential "
99 wpa_supplicant_deauthenticate(wpa_s
,
100 WLAN_REASON_DEAUTH_LEAVING
);
102 wpa_printf(MSG_DEBUG
, "WPS: Current network is "
103 "disabled - wait for user to enable");
106 wpa_s
->after_wps
= 5;
107 wpa_s
->wps_freq
= freq
;
108 wpa_s
->normal_scans
= 0;
109 wpa_s
->reassociate
= 1;
111 wpa_printf(MSG_DEBUG
, "WPS: Checking whether fast association "
112 "without a new scan can be used");
113 bss
= wpa_supplicant_pick_network(wpa_s
, &ssid
);
116 struct wps_parse_attr attr
;
118 wps
= wpa_bss_get_vendor_ie_multi(bss
,
120 if (wps
&& wps_parse_msg(wps
, &attr
) == 0 &&
122 *attr
.wps_state
== WPS_STATE_CONFIGURED
)
127 if (!use_fast_assoc
||
128 wpa_supplicant_fast_associate(wpa_s
) != 1)
129 wpa_supplicant_req_scan(wpa_s
, 0, 0);
133 if (wpa_s
->key_mgmt
== WPA_KEY_MGMT_WPS
&& wpa_s
->current_ssid
) {
134 wpa_printf(MSG_DEBUG
, "WPS: Registration completed - waiting "
135 "for external credential processing");
136 wpas_clear_wps(wpa_s
);
137 wpa_supplicant_deauthenticate(wpa_s
,
138 WLAN_REASON_DEAUTH_LEAVING
);
146 static void wpas_wps_security_workaround(struct wpa_supplicant
*wpa_s
,
147 struct wpa_ssid
*ssid
,
148 const struct wps_credential
*cred
)
150 struct wpa_driver_capa capa
;
153 struct wpa_ie_data adv
;
154 int wpa2
= 0, ccmp
= 0;
157 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
158 * case they are configured for mixed mode operation (WPA+WPA2 and
159 * TKIP+CCMP). Try to use scan results to figure out whether the AP
160 * actually supports stronger security and select that if the client
161 * has support for it, too.
164 if (wpa_drv_get_capa(wpa_s
, &capa
))
165 return; /* Unknown what driver supports */
167 if (ssid
->ssid
== NULL
)
169 bss
= wpa_bss_get(wpa_s
, cred
->mac_addr
, ssid
->ssid
, ssid
->ssid_len
);
171 wpa_printf(MSG_DEBUG
, "WPS: The AP was not found from BSS "
172 "table - use credential as-is");
176 wpa_printf(MSG_DEBUG
, "WPS: AP found from BSS table");
178 ie
= wpa_bss_get_ie(bss
, WLAN_EID_RSN
);
179 if (ie
&& wpa_parse_wpa_ie(ie
, 2 + ie
[1], &adv
) == 0) {
181 if (adv
.pairwise_cipher
& WPA_CIPHER_CCMP
)
184 ie
= wpa_bss_get_vendor_ie(bss
, WPA_IE_VENDOR_TYPE
);
185 if (ie
&& wpa_parse_wpa_ie(ie
, 2 + ie
[1], &adv
) == 0 &&
186 adv
.pairwise_cipher
& WPA_CIPHER_CCMP
)
190 if (ie
== NULL
&& (ssid
->proto
& WPA_PROTO_WPA
) &&
191 (ssid
->pairwise_cipher
& WPA_CIPHER_TKIP
)) {
193 * TODO: This could be the initial AP configuration and the
194 * Beacon contents could change shortly. Should request a new
195 * scan and delay addition of the network until the updated
196 * scan results are available.
198 wpa_printf(MSG_DEBUG
, "WPS: The AP did not yet advertise WPA "
199 "support - use credential as-is");
203 if (ccmp
&& !(ssid
->pairwise_cipher
& WPA_CIPHER_CCMP
) &&
204 (ssid
->pairwise_cipher
& WPA_CIPHER_TKIP
) &&
205 (capa
.key_mgmt
& WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
)) {
206 wpa_printf(MSG_DEBUG
, "WPS: Add CCMP into the credential "
207 "based on scan results");
208 if (wpa_s
->conf
->ap_scan
== 1)
209 ssid
->pairwise_cipher
|= WPA_CIPHER_CCMP
;
211 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
214 if (wpa2
&& !(ssid
->proto
& WPA_PROTO_RSN
) &&
215 (ssid
->proto
& WPA_PROTO_WPA
) &&
216 (capa
.enc
& WPA_DRIVER_CAPA_ENC_CCMP
)) {
217 wpa_printf(MSG_DEBUG
, "WPS: Add WPA2 into the credential "
218 "based on scan results");
219 if (wpa_s
->conf
->ap_scan
== 1)
220 ssid
->proto
|= WPA_PROTO_RSN
;
222 ssid
->proto
= WPA_PROTO_RSN
;
227 static void wpas_wps_remove_dup_network(struct wpa_supplicant
*wpa_s
,
228 struct wpa_ssid
*new_ssid
)
230 struct wpa_ssid
*ssid
, *next
;
232 for (ssid
= wpa_s
->conf
->ssid
, next
= ssid
? ssid
->next
: NULL
; ssid
;
233 ssid
= next
, next
= ssid
? ssid
->next
: NULL
) {
235 * new_ssid has already been added to the list in
236 * wpas_wps_add_network(), so skip it.
238 if (ssid
== new_ssid
)
241 if (ssid
->bssid_set
|| new_ssid
->bssid_set
) {
242 if (ssid
->bssid_set
!= new_ssid
->bssid_set
)
244 if (os_memcmp(ssid
->bssid
, new_ssid
->bssid
, ETH_ALEN
) !=
250 if (ssid
->ssid_len
== 0 || ssid
->ssid_len
!= new_ssid
->ssid_len
)
253 if (ssid
->ssid
&& new_ssid
->ssid
) {
254 if (os_memcmp(ssid
->ssid
, new_ssid
->ssid
,
255 ssid
->ssid_len
) != 0)
257 } else if (ssid
->ssid
|| new_ssid
->ssid
)
260 /* compare security parameters */
261 if (ssid
->auth_alg
!= new_ssid
->auth_alg
||
262 ssid
->key_mgmt
!= new_ssid
->key_mgmt
||
263 ssid
->proto
!= new_ssid
->proto
||
264 ssid
->pairwise_cipher
!= new_ssid
->pairwise_cipher
||
265 ssid
->group_cipher
!= new_ssid
->group_cipher
)
268 /* Remove the duplicated older network entry. */
269 wpa_printf(MSG_DEBUG
, "Remove duplicate network %d", ssid
->id
);
270 wpas_notify_network_removed(wpa_s
, ssid
);
271 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
276 static int wpa_supplicant_wps_cred(void *ctx
,
277 const struct wps_credential
*cred
)
279 struct wpa_supplicant
*wpa_s
= ctx
;
280 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
283 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
285 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
287 if ((wpa_s
->conf
->wps_cred_processing
== 1 ||
288 wpa_s
->conf
->wps_cred_processing
== 2) && cred
->cred_attr
) {
289 size_t blen
= cred
->cred_attr_len
* 2 + 1;
290 char *buf
= os_malloc(blen
);
292 wpa_snprintf_hex(buf
, blen
,
293 cred
->cred_attr
, cred
->cred_attr_len
);
294 wpa_msg(wpa_s
, MSG_INFO
, "%s%s",
295 WPS_EVENT_CRED_RECEIVED
, buf
);
299 wpas_notify_wps_credential(wpa_s
, cred
);
301 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_CRED_RECEIVED
);
303 wpa_hexdump_key(MSG_DEBUG
, "WPS: Received Credential attribute",
304 cred
->cred_attr
, cred
->cred_attr_len
);
306 if (wpa_s
->conf
->wps_cred_processing
== 1)
309 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: SSID", cred
->ssid
, cred
->ssid_len
);
310 wpa_printf(MSG_DEBUG
, "WPS: Authentication Type 0x%x",
312 wpa_printf(MSG_DEBUG
, "WPS: Encryption Type 0x%x", cred
->encr_type
);
313 wpa_printf(MSG_DEBUG
, "WPS: Network Key Index %d", cred
->key_idx
);
314 wpa_hexdump_key(MSG_DEBUG
, "WPS: Network Key",
315 cred
->key
, cred
->key_len
);
316 wpa_printf(MSG_DEBUG
, "WPS: MAC Address " MACSTR
,
317 MAC2STR(cred
->mac_addr
));
319 auth_type
= cred
->auth_type
;
320 if (auth_type
== (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
)) {
321 wpa_printf(MSG_DEBUG
, "WPS: Workaround - convert mixed-mode "
322 "auth_type into WPA2PSK");
323 auth_type
= WPS_AUTH_WPA2PSK
;
326 if (auth_type
!= WPS_AUTH_OPEN
&&
327 auth_type
!= WPS_AUTH_SHARED
&&
328 auth_type
!= WPS_AUTH_WPAPSK
&&
329 auth_type
!= WPS_AUTH_WPA2PSK
) {
330 wpa_printf(MSG_DEBUG
, "WPS: Ignored credentials for "
331 "unsupported authentication type 0x%x",
336 if (auth_type
== WPS_AUTH_WPAPSK
|| auth_type
== WPS_AUTH_WPA2PSK
) {
337 if (cred
->key_len
< 8 || cred
->key_len
> 2 * PMK_LEN
) {
338 wpa_printf(MSG_ERROR
, "WPS: Reject PSK credential with "
339 "invalid Network Key length %lu",
340 (unsigned long) cred
->key_len
);
345 if (ssid
&& (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)) {
346 wpa_printf(MSG_DEBUG
, "WPS: Replace WPS network block based "
347 "on the received credential");
348 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
349 if (ssid
->eap
.identity
&&
350 ssid
->eap
.identity_len
== WSC_ID_REGISTRAR_LEN
&&
351 os_memcmp(ssid
->eap
.identity
, WSC_ID_REGISTRAR
,
352 WSC_ID_REGISTRAR_LEN
) == 0)
354 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
355 os_free(ssid
->eap
.identity
);
356 ssid
->eap
.identity
= NULL
;
357 ssid
->eap
.identity_len
= 0;
358 os_free(ssid
->eap
.phase1
);
359 ssid
->eap
.phase1
= NULL
;
360 os_free(ssid
->eap
.eap_methods
);
361 ssid
->eap
.eap_methods
= NULL
;
362 if (!ssid
->p2p_group
) {
366 ssid
->disabled_until
.sec
= 0;
367 ssid
->disabled_until
.usec
= 0;
368 ssid
->auth_failures
= 0;
370 wpa_printf(MSG_DEBUG
, "WPS: Create a new network based on the "
371 "received credential");
372 ssid
= wpa_config_add_network(wpa_s
->conf
);
375 wpas_notify_network_added(wpa_s
, ssid
);
378 wpa_config_set_network_defaults(ssid
);
381 ssid
->ssid
= os_malloc(cred
->ssid_len
);
383 os_memcpy(ssid
->ssid
, cred
->ssid
, cred
->ssid_len
);
384 ssid
->ssid_len
= cred
->ssid_len
;
387 switch (cred
->encr_type
) {
391 if (cred
->key_len
<= 0)
393 if (cred
->key_len
!= 5 && cred
->key_len
!= 13 &&
394 cred
->key_len
!= 10 && cred
->key_len
!= 26) {
395 wpa_printf(MSG_ERROR
, "WPS: Invalid WEP Key length "
396 "%lu", (unsigned long) cred
->key_len
);
399 if (cred
->key_idx
> NUM_WEP_KEYS
) {
400 wpa_printf(MSG_ERROR
, "WPS: Invalid WEP Key index %d",
405 key_idx
= cred
->key_idx
- 1;
406 if (cred
->key_len
== 10 || cred
->key_len
== 26) {
407 if (hexstr2bin((char *) cred
->key
,
408 ssid
->wep_key
[key_idx
],
409 cred
->key_len
/ 2) < 0) {
410 wpa_printf(MSG_ERROR
, "WPS: Invalid WEP Key "
414 ssid
->wep_key_len
[key_idx
] = cred
->key_len
/ 2;
416 os_memcpy(ssid
->wep_key
[key_idx
], cred
->key
,
418 ssid
->wep_key_len
[key_idx
] = cred
->key_len
;
420 ssid
->wep_tx_keyidx
= key_idx
;
423 ssid
->pairwise_cipher
= WPA_CIPHER_TKIP
;
426 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
432 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
433 ssid
->key_mgmt
= WPA_KEY_MGMT_NONE
;
435 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
437 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_OPEN_NETWORK
438 "id=%d - Credentials for an open "
439 "network disabled by default - use "
440 "'select_network %d' to enable",
444 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
446 case WPS_AUTH_SHARED
:
447 ssid
->auth_alg
= WPA_AUTH_ALG_SHARED
;
448 ssid
->key_mgmt
= WPA_KEY_MGMT_NONE
;
451 case WPS_AUTH_WPAPSK
:
452 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
453 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
454 ssid
->proto
= WPA_PROTO_WPA
;
456 case WPS_AUTH_WPA2PSK
:
457 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
458 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
459 ssid
->proto
= WPA_PROTO_RSN
;
463 if (ssid
->key_mgmt
== WPA_KEY_MGMT_PSK
) {
464 if (cred
->key_len
== 2 * PMK_LEN
) {
465 if (hexstr2bin((const char *) cred
->key
, ssid
->psk
,
467 wpa_printf(MSG_ERROR
, "WPS: Invalid Network "
472 ssid
->export_keys
= 1;
473 } else if (cred
->key_len
>= 8 && cred
->key_len
< 2 * PMK_LEN
) {
474 os_free(ssid
->passphrase
);
475 ssid
->passphrase
= os_malloc(cred
->key_len
+ 1);
476 if (ssid
->passphrase
== NULL
)
478 os_memcpy(ssid
->passphrase
, cred
->key
, cred
->key_len
);
479 ssid
->passphrase
[cred
->key_len
] = '\0';
480 wpa_config_update_psk(ssid
);
481 ssid
->export_keys
= 1;
483 wpa_printf(MSG_ERROR
, "WPS: Invalid Network Key "
485 (unsigned long) cred
->key_len
);
490 wpas_wps_security_workaround(wpa_s
, ssid
, cred
);
492 if (cred
->ap_channel
)
493 wpa_s
->wps_ap_channel
= cred
->ap_channel
;
495 wpas_wps_remove_dup_network(wpa_s
, ssid
);
497 #ifndef CONFIG_NO_CONFIG_WRITE
498 if (wpa_s
->conf
->update_config
&&
499 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
500 wpa_printf(MSG_DEBUG
, "WPS: Failed to update configuration");
503 #endif /* CONFIG_NO_CONFIG_WRITE */
506 * Optimize the post-WPS scan based on the channel used during
507 * the provisioning in case EAP-Failure is not received.
509 wpa_s
->after_wps
= 5;
510 wpa_s
->wps_freq
= wpa_s
->assoc_freq
;
517 static void wpas_wps_pbc_overlap_cb(void *eloop_ctx
, void *timeout_ctx
)
519 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
520 wpas_p2p_notif_pbc_overlap(wpa_s
);
522 #endif /* CONFIG_P2P */
525 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant
*wpa_s
,
526 struct wps_event_m2d
*m2d
)
528 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_M2D
529 "dev_password_id=%d config_error=%d",
530 m2d
->dev_password_id
, m2d
->config_error
);
531 wpas_notify_wps_event_m2d(wpa_s
, m2d
);
533 if (wpa_s
->parent
&& wpa_s
->parent
!= wpa_s
) {
534 wpa_msg(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_M2D
535 "dev_password_id=%d config_error=%d",
536 m2d
->dev_password_id
, m2d
->config_error
);
538 if (m2d
->config_error
== WPS_CFG_MULTIPLE_PBC_DETECTED
) {
540 * Notify P2P from eloop timeout to avoid issues with the
541 * interface getting removed while processing a message.
543 eloop_register_timeout(0, 0, wpas_wps_pbc_overlap_cb
, wpa_s
,
546 #endif /* CONFIG_P2P */
550 static void wpas_wps_clear_timeout(void *eloop_ctx
, void *timeout_ctx
)
552 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
553 wpa_printf(MSG_DEBUG
, "WPS: Clear WPS network from timeout");
554 wpas_clear_wps(wpa_s
);
558 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant
*wpa_s
,
559 struct wps_event_fail
*fail
)
561 if (fail
->error_indication
> 0 &&
562 fail
->error_indication
< NUM_WPS_EI_VALUES
) {
563 wpa_msg(wpa_s
, MSG_INFO
,
564 WPS_EVENT_FAIL
"msg=%d config_error=%d reason=%d (%s)",
565 fail
->msg
, fail
->config_error
, fail
->error_indication
,
566 wps_ei_str(fail
->error_indication
));
567 if (wpa_s
->parent
&& wpa_s
->parent
!= wpa_s
)
568 wpa_msg(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_FAIL
569 "msg=%d config_error=%d reason=%d (%s)",
570 fail
->msg
, fail
->config_error
,
571 fail
->error_indication
,
572 wps_ei_str(fail
->error_indication
));
574 wpa_msg(wpa_s
, MSG_INFO
,
575 WPS_EVENT_FAIL
"msg=%d config_error=%d",
576 fail
->msg
, fail
->config_error
);
577 if (wpa_s
->parent
&& wpa_s
->parent
!= wpa_s
)
578 wpa_msg(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_FAIL
579 "msg=%d config_error=%d",
580 fail
->msg
, fail
->config_error
);
584 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
586 wpa_printf(MSG_DEBUG
, "WPS: Register timeout to clear WPS network");
587 eloop_cancel_timeout(wpas_wps_clear_timeout
, wpa_s
, NULL
);
588 eloop_register_timeout(0, 100000, wpas_wps_clear_timeout
, wpa_s
, NULL
);
590 wpas_notify_wps_event_fail(wpa_s
, fail
);
592 wpas_p2p_wps_failed(wpa_s
, fail
);
593 #endif /* CONFIG_P2P */
597 static void wpas_wps_reenable_networks_cb(void *eloop_ctx
, void *timeout_ctx
);
599 static void wpas_wps_reenable_networks(struct wpa_supplicant
*wpa_s
)
601 struct wpa_ssid
*ssid
;
604 eloop_cancel_timeout(wpas_wps_reenable_networks_cb
, wpa_s
, NULL
);
606 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
607 if (ssid
->disabled_for_connect
&& ssid
->disabled
) {
608 ssid
->disabled_for_connect
= 0;
610 wpas_notify_network_enabled_changed(wpa_s
, ssid
);
616 #ifndef CONFIG_NO_CONFIG_WRITE
617 if (wpa_s
->conf
->update_config
&&
618 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
619 wpa_printf(MSG_DEBUG
, "WPS: Failed to update "
622 #endif /* CONFIG_NO_CONFIG_WRITE */
627 static void wpas_wps_reenable_networks_cb(void *eloop_ctx
, void *timeout_ctx
)
629 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
630 /* Enable the networks disabled during wpas_wps_reassoc */
631 wpas_wps_reenable_networks(wpa_s
);
635 static void wpa_supplicant_wps_event_success(struct wpa_supplicant
*wpa_s
)
637 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_SUCCESS
);
638 wpa_s
->wps_success
= 1;
639 wpas_notify_wps_event_success(wpa_s
);
640 if (wpa_s
->current_ssid
)
641 wpas_clear_temp_disabled(wpa_s
, wpa_s
->current_ssid
, 1);
642 wpa_s
->extra_blacklist_count
= 0;
645 * Enable the networks disabled during wpas_wps_reassoc after 10
646 * seconds. The 10 seconds timer is to allow the data connection to be
647 * formed before allowing other networks to be selected.
649 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb
, wpa_s
,
653 wpas_p2p_wps_success(wpa_s
, wpa_s
->bssid
, 0);
654 #endif /* CONFIG_P2P */
658 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant
*wpa_s
,
659 struct wps_event_er_ap
*ap
)
662 char dev_type
[WPS_DEV_TYPE_BUFSIZE
];
664 uuid_bin2str(ap
->uuid
, uuid_str
, sizeof(uuid_str
));
665 if (ap
->pri_dev_type
)
666 wps_dev_type_bin2str(ap
->pri_dev_type
, dev_type
,
671 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_AP_ADD
"%s " MACSTR
672 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
673 uuid_str
, MAC2STR(ap
->mac_addr
), dev_type
, ap
->wps_state
,
674 ap
->friendly_name
? ap
->friendly_name
: "",
675 ap
->manufacturer
? ap
->manufacturer
: "",
676 ap
->model_description
? ap
->model_description
: "",
677 ap
->model_name
? ap
->model_name
: "",
678 ap
->manufacturer_url
? ap
->manufacturer_url
: "",
679 ap
->model_url
? ap
->model_url
: "");
683 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant
*wpa_s
,
684 struct wps_event_er_ap
*ap
)
687 uuid_bin2str(ap
->uuid
, uuid_str
, sizeof(uuid_str
));
688 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_AP_REMOVE
"%s", uuid_str
);
692 static void wpa_supplicant_wps_event_er_enrollee_add(
693 struct wpa_supplicant
*wpa_s
, struct wps_event_er_enrollee
*enrollee
)
696 char dev_type
[WPS_DEV_TYPE_BUFSIZE
];
698 uuid_bin2str(enrollee
->uuid
, uuid_str
, sizeof(uuid_str
));
699 if (enrollee
->pri_dev_type
)
700 wps_dev_type_bin2str(enrollee
->pri_dev_type
, dev_type
,
705 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_ENROLLEE_ADD
"%s " MACSTR
706 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
708 uuid_str
, MAC2STR(enrollee
->mac_addr
), enrollee
->m1_received
,
709 enrollee
->config_methods
, enrollee
->dev_passwd_id
, dev_type
,
710 enrollee
->dev_name
? enrollee
->dev_name
: "",
711 enrollee
->manufacturer
? enrollee
->manufacturer
: "",
712 enrollee
->model_name
? enrollee
->model_name
: "",
713 enrollee
->model_number
? enrollee
->model_number
: "",
714 enrollee
->serial_number
? enrollee
->serial_number
: "");
718 static void wpa_supplicant_wps_event_er_enrollee_remove(
719 struct wpa_supplicant
*wpa_s
, struct wps_event_er_enrollee
*enrollee
)
722 uuid_bin2str(enrollee
->uuid
, uuid_str
, sizeof(uuid_str
));
723 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_ENROLLEE_REMOVE
"%s " MACSTR
,
724 uuid_str
, MAC2STR(enrollee
->mac_addr
));
728 static void wpa_supplicant_wps_event_er_ap_settings(
729 struct wpa_supplicant
*wpa_s
,
730 struct wps_event_er_ap_settings
*ap_settings
)
734 const struct wps_credential
*cred
= ap_settings
->cred
;
737 if (cred
->auth_type
& (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
)) {
738 if (cred
->key_len
>= 8 && cred
->key_len
<= 64) {
739 os_memcpy(key_str
, cred
->key
, cred
->key_len
);
740 key_str
[cred
->key_len
] = '\0';
744 uuid_bin2str(ap_settings
->uuid
, uuid_str
, sizeof(uuid_str
));
745 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
746 wpa_msg_ctrl(wpa_s
, MSG_INFO
, WPS_EVENT_ER_AP_SETTINGS
747 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
749 uuid_str
, wpa_ssid_txt(cred
->ssid
, cred
->ssid_len
),
750 cred
->auth_type
, cred
->encr_type
, key_str
);
754 static void wpa_supplicant_wps_event_er_set_sel_reg(
755 struct wpa_supplicant
*wpa_s
,
756 struct wps_event_er_set_selected_registrar
*ev
)
760 uuid_bin2str(ev
->uuid
, uuid_str
, sizeof(uuid_str
));
762 case WPS_ER_SET_SEL_REG_START
:
763 wpa_msg(wpa_s
, MSG_DEBUG
, WPS_EVENT_ER_SET_SEL_REG
764 "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
765 "sel_reg_config_methods=0x%x",
766 uuid_str
, ev
->sel_reg
, ev
->dev_passwd_id
,
767 ev
->sel_reg_config_methods
);
769 case WPS_ER_SET_SEL_REG_DONE
:
770 wpa_msg(wpa_s
, MSG_DEBUG
, WPS_EVENT_ER_SET_SEL_REG
771 "uuid=%s state=DONE", uuid_str
);
773 case WPS_ER_SET_SEL_REG_FAILED
:
774 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_SET_SEL_REG
775 "uuid=%s state=FAILED", uuid_str
);
781 static void wpa_supplicant_wps_event(void *ctx
, enum wps_event event
,
782 union wps_event_data
*data
)
784 struct wpa_supplicant
*wpa_s
= ctx
;
787 wpa_supplicant_wps_event_m2d(wpa_s
, &data
->m2d
);
790 wpa_supplicant_wps_event_fail(wpa_s
, &data
->fail
);
793 wpa_supplicant_wps_event_success(wpa_s
);
795 case WPS_EV_PWD_AUTH_FAIL
:
797 if (wpa_s
->ap_iface
&& data
->pwd_auth_fail
.enrollee
)
798 wpa_supplicant_ap_pwd_auth_fail(wpa_s
);
799 #endif /* CONFIG_AP */
801 case WPS_EV_PBC_OVERLAP
:
803 case WPS_EV_PBC_TIMEOUT
:
805 case WPS_EV_PBC_ACTIVE
:
806 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ACTIVE
);
808 case WPS_EV_PBC_DISABLE
:
809 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_DISABLE
);
811 case WPS_EV_ER_AP_ADD
:
812 wpa_supplicant_wps_event_er_ap_add(wpa_s
, &data
->ap
);
814 case WPS_EV_ER_AP_REMOVE
:
815 wpa_supplicant_wps_event_er_ap_remove(wpa_s
, &data
->ap
);
817 case WPS_EV_ER_ENROLLEE_ADD
:
818 wpa_supplicant_wps_event_er_enrollee_add(wpa_s
,
821 case WPS_EV_ER_ENROLLEE_REMOVE
:
822 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s
,
825 case WPS_EV_ER_AP_SETTINGS
:
826 wpa_supplicant_wps_event_er_ap_settings(wpa_s
,
829 case WPS_EV_ER_SET_SELECTED_REGISTRAR
:
830 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s
,
833 case WPS_EV_AP_PIN_SUCCESS
:
839 static int wpa_supplicant_wps_rf_band(void *ctx
)
841 struct wpa_supplicant
*wpa_s
= ctx
;
843 if (!wpa_s
->current_ssid
|| !wpa_s
->assoc_freq
)
846 return (wpa_s
->assoc_freq
> 2484) ? WPS_RF_50GHZ
: WPS_RF_24GHZ
;
850 enum wps_request_type
wpas_wps_get_req_type(struct wpa_ssid
*ssid
)
852 if (eap_is_wps_pbc_enrollee(&ssid
->eap
) ||
853 eap_is_wps_pin_enrollee(&ssid
->eap
))
854 return WPS_REQ_ENROLLEE
;
856 return WPS_REQ_REGISTRAR
;
860 static void wpas_clear_wps(struct wpa_supplicant
*wpa_s
)
863 struct wpa_ssid
*ssid
, *remove_ssid
= NULL
, *prev_current
;
865 wpa_s
->after_wps
= 0;
866 wpa_s
->known_wps_freq
= 0;
868 prev_current
= wpa_s
->current_ssid
;
870 /* Enable the networks disabled during wpas_wps_reassoc */
871 wpas_wps_reenable_networks(wpa_s
);
873 eloop_cancel_timeout(wpas_wps_timeout
, wpa_s
, NULL
);
874 eloop_cancel_timeout(wpas_wps_clear_timeout
, wpa_s
, NULL
);
876 /* Remove any existing WPS network from configuration */
877 ssid
= wpa_s
->conf
->ssid
;
879 if (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
) {
880 if (ssid
== wpa_s
->current_ssid
) {
881 wpa_supplicant_deauthenticate(
882 wpa_s
, WLAN_REASON_DEAUTH_LEAVING
);
890 if (prev_current
== remove_ssid
) {
891 wpa_sm_set_config(wpa_s
->wpa
, NULL
);
892 eapol_sm_notify_config(wpa_s
->eapol
, NULL
,
895 wpas_notify_network_removed(wpa_s
, remove_ssid
);
896 wpa_config_remove_network(wpa_s
->conf
, id
);
900 wpas_wps_clear_ap_info(wpa_s
);
904 static void wpas_wps_timeout(void *eloop_ctx
, void *timeout_ctx
)
906 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
907 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_TIMEOUT
"Requested operation timed "
909 wpas_clear_wps(wpa_s
);
913 static struct wpa_ssid
* wpas_wps_add_network(struct wpa_supplicant
*wpa_s
,
914 int registrar
, const u8
*bssid
)
916 struct wpa_ssid
*ssid
;
918 ssid
= wpa_config_add_network(wpa_s
->conf
);
921 wpas_notify_network_added(wpa_s
, ssid
);
922 wpa_config_set_network_defaults(ssid
);
924 if (wpa_config_set(ssid
, "key_mgmt", "WPS", 0) < 0 ||
925 wpa_config_set(ssid
, "eap", "WSC", 0) < 0 ||
926 wpa_config_set(ssid
, "identity", registrar
?
927 "\"" WSC_ID_REGISTRAR
"\"" :
928 "\"" WSC_ID_ENROLLEE
"\"", 0) < 0) {
929 wpas_notify_network_removed(wpa_s
, ssid
);
930 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
938 #endif /* CONFIG_P2P */
940 os_memcpy(ssid
->bssid
, bssid
, ETH_ALEN
);
944 * Note: With P2P, the SSID may change at the time the WPS
945 * provisioning is started, so better not filter the AP based
946 * on the current SSID in the scan results.
949 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
950 if (os_memcmp(bssid
, bss
->bssid
, ETH_ALEN
) != 0)
954 ssid
->ssid
= os_malloc(bss
->ssid_len
);
955 if (ssid
->ssid
== NULL
)
957 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
958 ssid
->ssid_len
= bss
->ssid_len
;
959 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Picked SSID from "
961 ssid
->ssid
, ssid
->ssid_len
);
966 wpa_printf(MSG_DEBUG
, "WPS: More than one SSID found "
967 "for the AP; use wildcard");
972 #endif /* CONFIG_P2P */
979 static void wpas_wps_temp_disable(struct wpa_supplicant
*wpa_s
,
980 struct wpa_ssid
*selected
)
982 struct wpa_ssid
*ssid
;
984 if (wpa_s
->current_ssid
)
985 wpa_supplicant_deauthenticate(
986 wpa_s
, WLAN_REASON_DEAUTH_LEAVING
);
988 /* Mark all other networks disabled and trigger reassociation */
989 ssid
= wpa_s
->conf
->ssid
;
991 int was_disabled
= ssid
->disabled
;
992 ssid
->disabled_for_connect
= 0;
994 * In case the network object corresponds to a persistent group
995 * then do not send out network disabled signal. In addition,
996 * do not change disabled status of persistent network objects
997 * from 2 to 1 should we connect to another network.
999 if (was_disabled
!= 2) {
1000 ssid
->disabled
= ssid
!= selected
;
1001 if (was_disabled
!= ssid
->disabled
) {
1003 ssid
->disabled_for_connect
= 1;
1004 wpas_notify_network_enabled_changed(wpa_s
,
1013 static void wpas_wps_reassoc(struct wpa_supplicant
*wpa_s
,
1014 struct wpa_ssid
*selected
, const u8
*bssid
)
1016 struct wpa_bss
*bss
;
1018 wpa_s
->after_wps
= 0;
1019 wpa_s
->known_wps_freq
= 0;
1021 bss
= wpa_bss_get_bssid_latest(wpa_s
, bssid
);
1022 if (bss
&& bss
->freq
> 0) {
1023 wpa_s
->known_wps_freq
= 1;
1024 wpa_s
->wps_freq
= bss
->freq
;
1028 wpas_wps_temp_disable(wpa_s
, selected
);
1030 wpa_s
->disconnected
= 0;
1031 wpa_s
->reassociate
= 1;
1032 wpa_s
->scan_runs
= 0;
1033 wpa_s
->normal_scans
= 0;
1034 wpa_s
->wps_success
= 0;
1035 wpa_s
->blacklist_cleared
= 0;
1037 wpa_supplicant_cancel_sched_scan(wpa_s
);
1038 wpa_supplicant_req_scan(wpa_s
, 0, 0);
1042 int wpas_wps_start_pbc(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
1045 struct wpa_ssid
*ssid
;
1046 wpas_clear_wps(wpa_s
);
1047 ssid
= wpas_wps_add_network(wpa_s
, 0, bssid
);
1050 ssid
->temporary
= 1;
1051 ssid
->p2p_group
= p2p_group
;
1053 if (p2p_group
&& wpa_s
->go_params
&& wpa_s
->go_params
->ssid_len
) {
1054 ssid
->ssid
= os_zalloc(wpa_s
->go_params
->ssid_len
+ 1);
1056 ssid
->ssid_len
= wpa_s
->go_params
->ssid_len
;
1057 os_memcpy(ssid
->ssid
, wpa_s
->go_params
->ssid
,
1059 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Use specific AP "
1060 "SSID", ssid
->ssid
, ssid
->ssid_len
);
1063 #endif /* CONFIG_P2P */
1064 if (wpa_config_set(ssid
, "phase1", "\"pbc=1\"", 0) < 0)
1066 if (wpa_s
->wps_fragment_size
)
1067 ssid
->eap
.fragment_size
= wpa_s
->wps_fragment_size
;
1068 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0, wpas_wps_timeout
,
1070 wpas_wps_reassoc(wpa_s
, ssid
, bssid
);
1075 static int wpas_wps_start_dev_pw(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
1076 const char *pin
, int p2p_group
, u16 dev_pw_id
,
1077 const u8
*peer_pubkey_hash
,
1078 const u8
*ssid_val
, size_t ssid_len
)
1080 struct wpa_ssid
*ssid
;
1081 char val
[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN
];
1082 unsigned int rpin
= 0;
1083 char hash
[2 * WPS_OOB_PUBKEY_HASH_LEN
+ 10];
1085 wpas_clear_wps(wpa_s
);
1086 ssid
= wpas_wps_add_network(wpa_s
, 0, bssid
);
1088 wpa_printf(MSG_DEBUG
, "WPS: Could not add network");
1091 ssid
->temporary
= 1;
1092 ssid
->p2p_group
= p2p_group
;
1094 ssid
->ssid
= os_malloc(ssid_len
);
1096 os_memcpy(ssid
->ssid
, ssid_val
, ssid_len
);
1097 ssid
->ssid_len
= ssid_len
;
1100 if (peer_pubkey_hash
) {
1101 os_memcpy(hash
, " pkhash=", 8);
1102 wpa_snprintf_hex_uppercase(hash
+ 8, sizeof(hash
) - 8,
1104 WPS_OOB_PUBKEY_HASH_LEN
);
1109 if (p2p_group
&& wpa_s
->go_params
&& wpa_s
->go_params
->ssid_len
) {
1110 ssid
->ssid
= os_zalloc(wpa_s
->go_params
->ssid_len
+ 1);
1112 ssid
->ssid_len
= wpa_s
->go_params
->ssid_len
;
1113 os_memcpy(ssid
->ssid
, wpa_s
->go_params
->ssid
,
1115 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Use specific AP "
1116 "SSID", ssid
->ssid
, ssid
->ssid_len
);
1119 #endif /* CONFIG_P2P */
1121 os_snprintf(val
, sizeof(val
), "\"pin=%s dev_pw_id=%u%s\"",
1122 pin
, dev_pw_id
, hash
);
1124 rpin
= wps_generate_pin();
1125 os_snprintf(val
, sizeof(val
), "\"pin=%08d dev_pw_id=%u%s\"",
1126 rpin
, dev_pw_id
, hash
);
1128 if (wpa_config_set(ssid
, "phase1", val
, 0) < 0)
1130 if (wpa_s
->wps_fragment_size
)
1131 ssid
->eap
.fragment_size
= wpa_s
->wps_fragment_size
;
1132 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0, wpas_wps_timeout
,
1134 wpa_s
->wps_ap_iter
= 1;
1135 wpas_wps_reassoc(wpa_s
, ssid
, bssid
);
1140 int wpas_wps_start_pin(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
1141 const char *pin
, int p2p_group
, u16 dev_pw_id
)
1143 return wpas_wps_start_dev_pw(wpa_s
, bssid
, pin
, p2p_group
, dev_pw_id
,
1148 /* Cancel the wps pbc/pin requests */
1149 int wpas_wps_cancel(struct wpa_supplicant
*wpa_s
)
1152 if (wpa_s
->ap_iface
) {
1153 wpa_printf(MSG_DEBUG
, "WPS: Cancelling in AP mode");
1154 return wpa_supplicant_ap_wps_cancel(wpa_s
);
1156 #endif /* CONFIG_AP */
1158 if (wpa_s
->wpa_state
== WPA_SCANNING
||
1159 wpa_s
->wpa_state
== WPA_DISCONNECTED
) {
1160 wpa_printf(MSG_DEBUG
, "WPS: Cancel operation - cancel scan");
1161 wpa_supplicant_cancel_scan(wpa_s
);
1162 wpas_clear_wps(wpa_s
);
1163 } else if (wpa_s
->wpa_state
>= WPA_ASSOCIATED
) {
1164 wpa_printf(MSG_DEBUG
, "WPS: Cancel operation - "
1166 wpa_supplicant_deauthenticate(wpa_s
,
1167 WLAN_REASON_DEAUTH_LEAVING
);
1168 wpas_clear_wps(wpa_s
);
1170 wpas_wps_reenable_networks(wpa_s
);
1171 wpas_wps_clear_ap_info(wpa_s
);
1172 if (eloop_cancel_timeout(wpas_wps_clear_timeout
, wpa_s
, NULL
) >
1174 wpas_clear_wps(wpa_s
);
1177 wpa_s
->after_wps
= 0;
1183 int wpas_wps_start_reg(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
1184 const char *pin
, struct wps_new_ap_settings
*settings
)
1186 struct wpa_ssid
*ssid
;
1193 wpas_clear_wps(wpa_s
);
1194 ssid
= wpas_wps_add_network(wpa_s
, 1, bssid
);
1197 ssid
->temporary
= 1;
1199 end
= pos
+ sizeof(val
);
1200 res
= os_snprintf(pos
, end
- pos
, "\"pin=%s", pin
);
1201 if (res
< 0 || res
>= end
- pos
)
1205 res
= os_snprintf(pos
, end
- pos
, " new_ssid=%s new_auth=%s "
1206 "new_encr=%s new_key=%s",
1207 settings
->ssid_hex
, settings
->auth
,
1208 settings
->encr
, settings
->key_hex
);
1209 if (res
< 0 || res
>= end
- pos
)
1213 res
= os_snprintf(pos
, end
- pos
, "\"");
1214 if (res
< 0 || res
>= end
- pos
)
1216 if (wpa_config_set(ssid
, "phase1", val
, 0) < 0)
1218 if (wpa_s
->wps_fragment_size
)
1219 ssid
->eap
.fragment_size
= wpa_s
->wps_fragment_size
;
1220 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0, wpas_wps_timeout
,
1222 wpas_wps_reassoc(wpa_s
, ssid
, bssid
);
1227 static int wpas_wps_new_psk_cb(void *ctx
, const u8
*mac_addr
,
1228 const u8
*p2p_dev_addr
, const u8
*psk
,
1231 if (is_zero_ether_addr(p2p_dev_addr
)) {
1232 wpa_printf(MSG_DEBUG
,
1233 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
,
1236 wpa_printf(MSG_DEBUG
,
1237 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1238 " P2P Device Addr " MACSTR
,
1239 MAC2STR(mac_addr
), MAC2STR(p2p_dev_addr
));
1241 wpa_hexdump_key(MSG_DEBUG
, "Per-device PSK", psk
, psk_len
);
1249 static void wpas_wps_pin_needed_cb(void *ctx
, const u8
*uuid_e
,
1250 const struct wps_device_data
*dev
)
1252 char uuid
[40], txt
[400];
1254 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1255 if (uuid_bin2str(uuid_e
, uuid
, sizeof(uuid
)))
1257 wpa_printf(MSG_DEBUG
, "WPS: PIN needed for UUID-E %s", uuid
);
1258 len
= os_snprintf(txt
, sizeof(txt
), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1259 " [%s|%s|%s|%s|%s|%s]",
1260 uuid
, MAC2STR(dev
->mac_addr
), dev
->device_name
,
1261 dev
->manufacturer
, dev
->model_name
,
1262 dev
->model_number
, dev
->serial_number
,
1263 wps_dev_type_bin2str(dev
->pri_dev_type
, devtype
,
1265 if (len
> 0 && len
< (int) sizeof(txt
))
1266 wpa_printf(MSG_INFO
, "%s", txt
);
1270 static void wpas_wps_set_sel_reg_cb(void *ctx
, int sel_reg
, u16 dev_passwd_id
,
1271 u16 sel_reg_config_methods
)
1273 #ifdef CONFIG_WPS_ER
1274 struct wpa_supplicant
*wpa_s
= ctx
;
1276 if (wpa_s
->wps_er
== NULL
)
1278 wpa_printf(MSG_DEBUG
, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1279 "dev_password_id=%u sel_reg_config_methods=0x%x",
1280 sel_reg
, dev_passwd_id
, sel_reg_config_methods
);
1281 wps_er_set_sel_reg(wpa_s
->wps_er
, sel_reg
, dev_passwd_id
,
1282 sel_reg_config_methods
);
1283 #endif /* CONFIG_WPS_ER */
1287 static u16
wps_fix_config_methods(u16 config_methods
)
1290 if ((config_methods
&
1291 (WPS_CONFIG_DISPLAY
| WPS_CONFIG_VIRT_DISPLAY
|
1292 WPS_CONFIG_PHY_DISPLAY
)) == WPS_CONFIG_DISPLAY
) {
1293 wpa_printf(MSG_INFO
, "WPS: Converting display to "
1294 "virtual_display for WPS 2.0 compliance");
1295 config_methods
|= WPS_CONFIG_VIRT_DISPLAY
;
1297 if ((config_methods
&
1298 (WPS_CONFIG_PUSHBUTTON
| WPS_CONFIG_VIRT_PUSHBUTTON
|
1299 WPS_CONFIG_PHY_PUSHBUTTON
)) == WPS_CONFIG_PUSHBUTTON
) {
1300 wpa_printf(MSG_INFO
, "WPS: Converting push_button to "
1301 "virtual_push_button for WPS 2.0 compliance");
1302 config_methods
|= WPS_CONFIG_VIRT_PUSHBUTTON
;
1304 #endif /* CONFIG_WPS2 */
1306 return config_methods
;
1310 static void wpas_wps_set_uuid(struct wpa_supplicant
*wpa_s
,
1311 struct wps_context
*wps
)
1316 if (is_nil_uuid(wpa_s
->conf
->uuid
)) {
1317 struct wpa_supplicant
*first
;
1318 first
= wpa_s
->global
->ifaces
;
1319 while (first
&& first
->next
)
1320 first
= first
->next
;
1321 if (first
&& first
!= wpa_s
) {
1322 if (wps
!= wpa_s
->global
->ifaces
->wps
)
1323 os_memcpy(wps
->uuid
,
1324 wpa_s
->global
->ifaces
->wps
->uuid
,
1326 src
= "from the first interface";
1328 uuid_gen_mac_addr(wpa_s
->own_addr
, wps
->uuid
);
1329 src
= "based on MAC address";
1332 os_memcpy(wps
->uuid
, wpa_s
->conf
->uuid
, WPS_UUID_LEN
);
1333 src
= "based on configuration";
1336 uuid_bin2str(wps
->uuid
, buf
, sizeof(buf
));
1337 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: UUID %s: %s", src
, buf
);
1341 static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant
*wpa_s
,
1342 struct wps_context
*wps
)
1344 wpabuf_free(wps
->dev
.vendor_ext_m1
);
1345 wps
->dev
.vendor_ext_m1
= NULL
;
1347 if (wpa_s
->conf
->wps_vendor_ext_m1
) {
1348 wps
->dev
.vendor_ext_m1
=
1349 wpabuf_dup(wpa_s
->conf
->wps_vendor_ext_m1
);
1350 if (!wps
->dev
.vendor_ext_m1
) {
1351 wpa_printf(MSG_ERROR
, "WPS: Cannot "
1352 "allocate memory for vendor_ext_m1");
1358 int wpas_wps_init(struct wpa_supplicant
*wpa_s
)
1360 struct wps_context
*wps
;
1361 struct wps_registrar_config rcfg
;
1362 struct hostapd_hw_modes
*modes
;
1365 wps
= os_zalloc(sizeof(*wps
));
1369 wps
->cred_cb
= wpa_supplicant_wps_cred
;
1370 wps
->event_cb
= wpa_supplicant_wps_event
;
1371 wps
->rf_band_cb
= wpa_supplicant_wps_rf_band
;
1372 wps
->cb_ctx
= wpa_s
;
1374 wps
->dev
.device_name
= wpa_s
->conf
->device_name
;
1375 wps
->dev
.manufacturer
= wpa_s
->conf
->manufacturer
;
1376 wps
->dev
.model_name
= wpa_s
->conf
->model_name
;
1377 wps
->dev
.model_number
= wpa_s
->conf
->model_number
;
1378 wps
->dev
.serial_number
= wpa_s
->conf
->serial_number
;
1379 wps
->config_methods
=
1380 wps_config_methods_str2bin(wpa_s
->conf
->config_methods
);
1381 if ((wps
->config_methods
& (WPS_CONFIG_DISPLAY
| WPS_CONFIG_LABEL
)) ==
1382 (WPS_CONFIG_DISPLAY
| WPS_CONFIG_LABEL
)) {
1383 wpa_printf(MSG_ERROR
, "WPS: Both Label and Display config "
1384 "methods are not allowed at the same time");
1388 wps
->config_methods
= wps_fix_config_methods(wps
->config_methods
);
1389 wps
->dev
.config_methods
= wps
->config_methods
;
1390 os_memcpy(wps
->dev
.pri_dev_type
, wpa_s
->conf
->device_type
,
1393 wps
->dev
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
1394 os_memcpy(wps
->dev
.sec_dev_type
, wpa_s
->conf
->sec_device_type
,
1395 WPS_DEV_TYPE_LEN
* wps
->dev
.num_sec_dev_types
);
1397 wpas_wps_set_vendor_ext_m1(wpa_s
, wps
);
1399 wps
->dev
.os_version
= WPA_GET_BE32(wpa_s
->conf
->os_version
);
1400 modes
= wpa_s
->hw
.modes
;
1402 for (m
= 0; m
< wpa_s
->hw
.num_modes
; m
++) {
1403 if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211B
||
1404 modes
[m
].mode
== HOSTAPD_MODE_IEEE80211G
)
1405 wps
->dev
.rf_bands
|= WPS_RF_24GHZ
;
1406 else if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211A
)
1407 wps
->dev
.rf_bands
|= WPS_RF_50GHZ
;
1410 if (wps
->dev
.rf_bands
== 0) {
1412 * Default to claiming support for both bands if the driver
1413 * does not provide support for fetching supported bands.
1415 wps
->dev
.rf_bands
= WPS_RF_24GHZ
| WPS_RF_50GHZ
;
1417 os_memcpy(wps
->dev
.mac_addr
, wpa_s
->own_addr
, ETH_ALEN
);
1418 wpas_wps_set_uuid(wpa_s
, wps
);
1420 wps
->auth_types
= WPS_AUTH_WPA2PSK
| WPS_AUTH_WPAPSK
;
1421 wps
->encr_types
= WPS_ENCR_AES
| WPS_ENCR_TKIP
;
1423 os_memset(&rcfg
, 0, sizeof(rcfg
));
1424 rcfg
.new_psk_cb
= wpas_wps_new_psk_cb
;
1425 rcfg
.pin_needed_cb
= wpas_wps_pin_needed_cb
;
1426 rcfg
.set_sel_reg_cb
= wpas_wps_set_sel_reg_cb
;
1427 rcfg
.cb_ctx
= wpa_s
;
1429 wps
->registrar
= wps_registrar_init(wps
, &rcfg
);
1430 if (wps
->registrar
== NULL
) {
1431 wpa_printf(MSG_DEBUG
, "Failed to initialize WPS Registrar");
1442 void wpas_wps_deinit(struct wpa_supplicant
*wpa_s
)
1444 eloop_cancel_timeout(wpas_wps_timeout
, wpa_s
, NULL
);
1445 eloop_cancel_timeout(wpas_wps_clear_timeout
, wpa_s
, NULL
);
1446 eloop_cancel_timeout(wpas_wps_reenable_networks_cb
, wpa_s
, NULL
);
1447 wpas_wps_clear_ap_info(wpa_s
);
1449 if (wpa_s
->wps
== NULL
)
1452 #ifdef CONFIG_WPS_ER
1453 wps_er_deinit(wpa_s
->wps_er
, NULL
, NULL
);
1454 wpa_s
->wps_er
= NULL
;
1455 #endif /* CONFIG_WPS_ER */
1457 wps_registrar_deinit(wpa_s
->wps
->registrar
);
1458 wpabuf_free(wpa_s
->wps
->dh_pubkey
);
1459 wpabuf_free(wpa_s
->wps
->dh_privkey
);
1460 wpabuf_free(wpa_s
->wps
->dev
.vendor_ext_m1
);
1461 os_free(wpa_s
->wps
->network_key
);
1462 os_free(wpa_s
->wps
);
1467 int wpas_wps_ssid_bss_match(struct wpa_supplicant
*wpa_s
,
1468 struct wpa_ssid
*ssid
, struct wpa_bss
*bss
)
1470 struct wpabuf
*wps_ie
;
1472 if (!(ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))
1475 wps_ie
= wpa_bss_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
1476 if (eap_is_wps_pbc_enrollee(&ssid
->eap
)) {
1478 wpa_printf(MSG_DEBUG
, " skip - non-WPS AP");
1482 if (!wps_is_selected_pbc_registrar(wps_ie
)) {
1483 wpa_printf(MSG_DEBUG
, " skip - WPS AP "
1484 "without active PBC Registrar");
1485 wpabuf_free(wps_ie
);
1489 /* TODO: overlap detection */
1490 wpa_printf(MSG_DEBUG
, " selected based on WPS IE "
1492 wpabuf_free(wps_ie
);
1496 if (eap_is_wps_pin_enrollee(&ssid
->eap
)) {
1498 wpa_printf(MSG_DEBUG
, " skip - non-WPS AP");
1503 * Start with WPS APs that advertise our address as an
1504 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1505 * allow any WPS AP after couple of scans since some APs do not
1506 * set Selected Registrar attribute properly when using
1507 * external Registrar.
1509 if (!wps_is_addr_authorized(wps_ie
, wpa_s
->own_addr
, 1)) {
1510 if (wpa_s
->scan_runs
< WPS_PIN_SCAN_IGNORE_SEL_REG
) {
1511 wpa_printf(MSG_DEBUG
, " skip - WPS AP "
1512 "without active PIN Registrar");
1513 wpabuf_free(wps_ie
);
1516 wpa_printf(MSG_DEBUG
, " selected based on WPS IE");
1518 wpa_printf(MSG_DEBUG
, " selected based on WPS IE "
1519 "(Authorized MAC or Active PIN)");
1521 wpabuf_free(wps_ie
);
1526 wpa_printf(MSG_DEBUG
, " selected based on WPS IE");
1527 wpabuf_free(wps_ie
);
1535 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant
*wpa_s
,
1536 struct wpa_ssid
*ssid
,
1537 struct wpa_bss
*bss
)
1539 struct wpabuf
*wps_ie
= NULL
;
1542 if (eap_is_wps_pbc_enrollee(&ssid
->eap
)) {
1543 wps_ie
= wpa_bss_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
1544 if (wps_ie
&& wps_is_selected_pbc_registrar(wps_ie
)) {
1545 /* allow wildcard SSID for WPS PBC */
1548 } else if (eap_is_wps_pin_enrollee(&ssid
->eap
)) {
1549 wps_ie
= wpa_bss_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
1551 (wps_is_addr_authorized(wps_ie
, wpa_s
->own_addr
, 1) ||
1552 wpa_s
->scan_runs
>= WPS_PIN_SCAN_IGNORE_SEL_REG
)) {
1553 /* allow wildcard SSID for WPS PIN */
1558 if (!ret
&& ssid
->bssid_set
&&
1559 os_memcmp(ssid
->bssid
, bss
->bssid
, ETH_ALEN
) == 0) {
1560 /* allow wildcard SSID due to hardcoded BSSID match */
1564 #ifdef CONFIG_WPS_STRICT
1566 if (wps_validate_beacon_probe_resp(wps_ie
, bss
->beacon_ie_len
>
1569 if (bss
->beacon_ie_len
) {
1570 struct wpabuf
*bcn_wps
;
1571 bcn_wps
= wpa_bss_get_vendor_ie_multi_beacon(
1572 bss
, WPS_IE_VENDOR_TYPE
);
1573 if (bcn_wps
== NULL
) {
1574 wpa_printf(MSG_DEBUG
, "WPS: Mandatory WPS IE "
1575 "missing from AP Beacon");
1578 if (wps_validate_beacon(wps_ie
) < 0)
1580 wpabuf_free(bcn_wps
);
1584 #endif /* CONFIG_WPS_STRICT */
1586 wpabuf_free(wps_ie
);
1592 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant
*wpa_s
,
1593 struct wpa_bss
*selected
, struct wpa_ssid
*ssid
)
1595 const u8
*sel_uuid
, *uuid
;
1596 struct wpabuf
*wps_ie
;
1598 struct wpa_bss
*bss
;
1600 if (!eap_is_wps_pbc_enrollee(&ssid
->eap
))
1603 wpa_printf(MSG_DEBUG
, "WPS: Check whether PBC session overlap is "
1604 "present in scan results; selected BSSID " MACSTR
,
1605 MAC2STR(selected
->bssid
));
1607 /* Make sure that only one AP is in active PBC mode */
1608 wps_ie
= wpa_bss_get_vendor_ie_multi(selected
, WPS_IE_VENDOR_TYPE
);
1610 sel_uuid
= wps_get_uuid_e(wps_ie
);
1611 wpa_hexdump(MSG_DEBUG
, "WPS: UUID of the selected BSS",
1612 sel_uuid
, UUID_LEN
);
1614 wpa_printf(MSG_DEBUG
, "WPS: Selected BSS does not include "
1619 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1621 if (bss
== selected
)
1623 ie
= wpa_bss_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
1626 if (!wps_is_selected_pbc_registrar(ie
)) {
1630 wpa_printf(MSG_DEBUG
, "WPS: Another BSS in active PBC mode: "
1631 MACSTR
, MAC2STR(bss
->bssid
));
1632 uuid
= wps_get_uuid_e(ie
);
1633 wpa_hexdump(MSG_DEBUG
, "WPS: UUID of the other BSS",
1635 if (sel_uuid
== NULL
|| uuid
== NULL
||
1636 os_memcmp(sel_uuid
, uuid
, UUID_LEN
) != 0) {
1637 ret
= 1; /* PBC overlap */
1638 wpa_msg(wpa_s
, MSG_INFO
, "WPS: PBC overlap detected: "
1639 MACSTR
" and " MACSTR
,
1640 MAC2STR(selected
->bssid
),
1641 MAC2STR(bss
->bssid
));
1646 /* TODO: verify that this is reasonable dual-band situation */
1651 wpabuf_free(wps_ie
);
1657 void wpas_wps_notify_scan_results(struct wpa_supplicant
*wpa_s
)
1659 struct wpa_bss
*bss
;
1660 unsigned int pbc
= 0, auth
= 0, pin
= 0, wps
= 0;
1662 if (wpa_s
->disconnected
|| wpa_s
->wpa_state
>= WPA_ASSOCIATED
)
1665 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1667 ie
= wpa_bss_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
1670 if (wps_is_selected_pbc_registrar(ie
))
1672 else if (wps_is_addr_authorized(ie
, wpa_s
->own_addr
, 0))
1674 else if (wps_is_selected_pin_registrar(ie
))
1682 wpa_msg_ctrl(wpa_s
, MSG_INFO
, WPS_EVENT_AP_AVAILABLE_PBC
);
1684 wpa_msg_ctrl(wpa_s
, MSG_INFO
, WPS_EVENT_AP_AVAILABLE_AUTH
);
1686 wpa_msg_ctrl(wpa_s
, MSG_INFO
, WPS_EVENT_AP_AVAILABLE_PIN
);
1688 wpa_msg_ctrl(wpa_s
, MSG_INFO
, WPS_EVENT_AP_AVAILABLE
);
1692 int wpas_wps_searching(struct wpa_supplicant
*wpa_s
)
1694 struct wpa_ssid
*ssid
;
1696 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
1697 if ((ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
) && !ssid
->disabled
)
1705 int wpas_wps_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
1708 struct wpabuf
*wps_ie
;
1711 wps_ie
= ieee802_11_vendor_ie_concat(ies
, ies_len
, WPS_DEV_OUI_WFA
);
1715 ret
= wps_attr_text(wps_ie
, buf
, end
);
1716 wpabuf_free(wps_ie
);
1721 int wpas_wps_er_start(struct wpa_supplicant
*wpa_s
, const char *filter
)
1723 #ifdef CONFIG_WPS_ER
1724 if (wpa_s
->wps_er
) {
1725 wps_er_refresh(wpa_s
->wps_er
);
1728 wpa_s
->wps_er
= wps_er_init(wpa_s
->wps
, wpa_s
->ifname
, filter
);
1729 if (wpa_s
->wps_er
== NULL
)
1732 #else /* CONFIG_WPS_ER */
1734 #endif /* CONFIG_WPS_ER */
1738 int wpas_wps_er_stop(struct wpa_supplicant
*wpa_s
)
1740 #ifdef CONFIG_WPS_ER
1741 wps_er_deinit(wpa_s
->wps_er
, NULL
, NULL
);
1742 wpa_s
->wps_er
= NULL
;
1743 #endif /* CONFIG_WPS_ER */
1748 #ifdef CONFIG_WPS_ER
1749 int wpas_wps_er_add_pin(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
1750 const char *uuid
, const char *pin
)
1753 const u8
*use_uuid
= NULL
;
1754 u8 addr_buf
[ETH_ALEN
];
1756 if (os_strcmp(uuid
, "any") == 0) {
1757 } else if (uuid_str2bin(uuid
, u
) == 0) {
1759 } else if (hwaddr_aton(uuid
, addr_buf
) == 0) {
1760 use_uuid
= wps_er_get_sta_uuid(wpa_s
->wps_er
, addr_buf
);
1761 if (use_uuid
== NULL
)
1765 return wps_registrar_add_pin(wpa_s
->wps
->registrar
, addr
,
1767 (const u8
*) pin
, os_strlen(pin
), 300);
1771 int wpas_wps_er_pbc(struct wpa_supplicant
*wpa_s
, const char *uuid
)
1773 u8 u
[UUID_LEN
], *use_uuid
= NULL
;
1774 u8 addr
[ETH_ALEN
], *use_addr
= NULL
;
1776 if (uuid_str2bin(uuid
, u
) == 0)
1778 else if (hwaddr_aton(uuid
, addr
) == 0)
1782 return wps_er_pbc(wpa_s
->wps_er
, use_uuid
, use_addr
);
1786 int wpas_wps_er_learn(struct wpa_supplicant
*wpa_s
, const char *uuid
,
1789 u8 u
[UUID_LEN
], *use_uuid
= NULL
;
1790 u8 addr
[ETH_ALEN
], *use_addr
= NULL
;
1792 if (uuid_str2bin(uuid
, u
) == 0)
1794 else if (hwaddr_aton(uuid
, addr
) == 0)
1799 return wps_er_learn(wpa_s
->wps_er
, use_uuid
, use_addr
, (const u8
*) pin
,
1804 static int wpas_wps_network_to_cred(struct wpa_ssid
*ssid
,
1805 struct wps_credential
*cred
)
1807 os_memset(cred
, 0, sizeof(*cred
));
1808 if (ssid
->ssid_len
> 32)
1810 os_memcpy(cred
->ssid
, ssid
->ssid
, ssid
->ssid_len
);
1811 cred
->ssid_len
= ssid
->ssid_len
;
1812 if (ssid
->key_mgmt
& WPA_KEY_MGMT_PSK
) {
1813 cred
->auth_type
= (ssid
->proto
& WPA_PROTO_RSN
) ?
1814 WPS_AUTH_WPA2PSK
: WPS_AUTH_WPAPSK
;
1815 if (ssid
->pairwise_cipher
& WPA_CIPHER_CCMP
)
1816 cred
->encr_type
= WPS_ENCR_AES
;
1818 cred
->encr_type
= WPS_ENCR_TKIP
;
1819 if (ssid
->passphrase
) {
1820 cred
->key_len
= os_strlen(ssid
->passphrase
);
1821 if (cred
->key_len
>= 64)
1823 os_memcpy(cred
->key
, ssid
->passphrase
, cred
->key_len
);
1824 } else if (ssid
->psk_set
) {
1826 os_memcpy(cred
->key
, ssid
->psk
, 32);
1830 cred
->auth_type
= WPS_AUTH_OPEN
;
1831 cred
->encr_type
= WPS_ENCR_NONE
;
1838 int wpas_wps_er_set_config(struct wpa_supplicant
*wpa_s
, const char *uuid
,
1841 u8 u
[UUID_LEN
], *use_uuid
= NULL
;
1842 u8 addr
[ETH_ALEN
], *use_addr
= NULL
;
1843 struct wpa_ssid
*ssid
;
1844 struct wps_credential cred
;
1846 if (uuid_str2bin(uuid
, u
) == 0)
1848 else if (hwaddr_aton(uuid
, addr
) == 0)
1852 ssid
= wpa_config_get_network(wpa_s
->conf
, id
);
1853 if (ssid
== NULL
|| ssid
->ssid
== NULL
)
1856 if (wpas_wps_network_to_cred(ssid
, &cred
) < 0)
1858 return wps_er_set_config(wpa_s
->wps_er
, use_uuid
, use_addr
, &cred
);
1862 int wpas_wps_er_config(struct wpa_supplicant
*wpa_s
, const char *uuid
,
1863 const char *pin
, struct wps_new_ap_settings
*settings
)
1865 u8 u
[UUID_LEN
], *use_uuid
= NULL
;
1866 u8 addr
[ETH_ALEN
], *use_addr
= NULL
;
1867 struct wps_credential cred
;
1870 if (uuid_str2bin(uuid
, u
) == 0)
1872 else if (hwaddr_aton(uuid
, addr
) == 0)
1876 if (settings
->ssid_hex
== NULL
|| settings
->auth
== NULL
||
1877 settings
->encr
== NULL
|| settings
->key_hex
== NULL
)
1880 os_memset(&cred
, 0, sizeof(cred
));
1881 len
= os_strlen(settings
->ssid_hex
);
1882 if ((len
& 1) || len
> 2 * sizeof(cred
.ssid
) ||
1883 hexstr2bin(settings
->ssid_hex
, cred
.ssid
, len
/ 2))
1885 cred
.ssid_len
= len
/ 2;
1887 len
= os_strlen(settings
->key_hex
);
1888 if ((len
& 1) || len
> 2 * sizeof(cred
.key
) ||
1889 hexstr2bin(settings
->key_hex
, cred
.key
, len
/ 2))
1891 cred
.key_len
= len
/ 2;
1893 if (os_strcmp(settings
->auth
, "OPEN") == 0)
1894 cred
.auth_type
= WPS_AUTH_OPEN
;
1895 else if (os_strcmp(settings
->auth
, "WPAPSK") == 0)
1896 cred
.auth_type
= WPS_AUTH_WPAPSK
;
1897 else if (os_strcmp(settings
->auth
, "WPA2PSK") == 0)
1898 cred
.auth_type
= WPS_AUTH_WPA2PSK
;
1902 if (os_strcmp(settings
->encr
, "NONE") == 0)
1903 cred
.encr_type
= WPS_ENCR_NONE
;
1904 else if (os_strcmp(settings
->encr
, "WEP") == 0)
1905 cred
.encr_type
= WPS_ENCR_WEP
;
1906 else if (os_strcmp(settings
->encr
, "TKIP") == 0)
1907 cred
.encr_type
= WPS_ENCR_TKIP
;
1908 else if (os_strcmp(settings
->encr
, "CCMP") == 0)
1909 cred
.encr_type
= WPS_ENCR_AES
;
1913 return wps_er_config(wpa_s
->wps_er
, use_uuid
, use_addr
,
1914 (const u8
*) pin
, os_strlen(pin
), &cred
);
1918 #ifdef CONFIG_WPS_NFC
1919 struct wpabuf
* wpas_wps_er_nfc_config_token(struct wpa_supplicant
*wpa_s
,
1920 int ndef
, const char *uuid
)
1923 u8 u
[UUID_LEN
], *use_uuid
= NULL
;
1924 u8 addr
[ETH_ALEN
], *use_addr
= NULL
;
1929 if (uuid_str2bin(uuid
, u
) == 0)
1931 else if (hwaddr_aton(uuid
, addr
) == 0)
1936 ret
= wps_er_nfc_config_token(wpa_s
->wps_er
, use_uuid
, use_addr
);
1939 tmp
= ndef_build_wifi(ret
);
1948 #endif /* CONFIG_WPS_NFC */
1951 static int callbacks_pending
= 0;
1953 static void wpas_wps_terminate_cb(void *ctx
)
1955 wpa_printf(MSG_DEBUG
, "WPS ER: Terminated");
1956 if (--callbacks_pending
<= 0)
1959 #endif /* CONFIG_WPS_ER */
1962 int wpas_wps_terminate_pending(struct wpa_supplicant
*wpa_s
)
1964 #ifdef CONFIG_WPS_ER
1965 if (wpa_s
->wps_er
) {
1966 callbacks_pending
++;
1967 wps_er_deinit(wpa_s
->wps_er
, wpas_wps_terminate_cb
, wpa_s
);
1968 wpa_s
->wps_er
= NULL
;
1971 #endif /* CONFIG_WPS_ER */
1976 void wpas_wps_update_config(struct wpa_supplicant
*wpa_s
)
1978 struct wps_context
*wps
= wpa_s
->wps
;
1983 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_CONFIG_METHODS
) {
1984 wps
->config_methods
= wps_config_methods_str2bin(
1985 wpa_s
->conf
->config_methods
);
1986 if ((wps
->config_methods
&
1987 (WPS_CONFIG_DISPLAY
| WPS_CONFIG_LABEL
)) ==
1988 (WPS_CONFIG_DISPLAY
| WPS_CONFIG_LABEL
)) {
1989 wpa_printf(MSG_ERROR
, "WPS: Both Label and Display "
1990 "config methods are not allowed at the "
1992 wps
->config_methods
&= ~WPS_CONFIG_LABEL
;
1995 wps
->config_methods
= wps_fix_config_methods(wps
->config_methods
);
1996 wps
->dev
.config_methods
= wps
->config_methods
;
1998 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
)
1999 os_memcpy(wps
->dev
.pri_dev_type
, wpa_s
->conf
->device_type
,
2002 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
) {
2003 wps
->dev
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
2004 os_memcpy(wps
->dev
.sec_dev_type
, wpa_s
->conf
->sec_device_type
,
2005 wps
->dev
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
2008 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_VENDOR_EXTENSION
)
2009 wpas_wps_set_vendor_ext_m1(wpa_s
, wps
);
2011 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_OS_VERSION
)
2012 wps
->dev
.os_version
= WPA_GET_BE32(wpa_s
->conf
->os_version
);
2014 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_UUID
)
2015 wpas_wps_set_uuid(wpa_s
, wps
);
2017 if (wpa_s
->conf
->changed_parameters
&
2018 (CFG_CHANGED_DEVICE_NAME
| CFG_CHANGED_WPS_STRING
)) {
2019 /* Update pointers to make sure they refer current values */
2020 wps
->dev
.device_name
= wpa_s
->conf
->device_name
;
2021 wps
->dev
.manufacturer
= wpa_s
->conf
->manufacturer
;
2022 wps
->dev
.model_name
= wpa_s
->conf
->model_name
;
2023 wps
->dev
.model_number
= wpa_s
->conf
->model_number
;
2024 wps
->dev
.serial_number
= wpa_s
->conf
->serial_number
;
2029 #ifdef CONFIG_WPS_NFC
2031 #ifdef CONFIG_WPS_ER
2032 static struct wpabuf
*
2033 wpas_wps_network_config_token(struct wpa_supplicant
*wpa_s
, int ndef
,
2034 struct wpa_ssid
*ssid
)
2037 struct wps_credential cred
;
2039 if (wpas_wps_network_to_cred(ssid
, &cred
) < 0)
2042 ret
= wps_er_config_token_from_cred(wpa_s
->wps
, &cred
);
2046 tmp
= ndef_build_wifi(ret
);
2055 #endif /* CONFIG_WPS_ER */
2058 struct wpabuf
* wpas_wps_nfc_config_token(struct wpa_supplicant
*wpa_s
,
2059 int ndef
, const char *id_str
)
2061 #ifdef CONFIG_WPS_ER
2065 struct wpa_ssid
*ssid
;
2067 id
= strtol(id_str
, &end
, 10);
2071 ssid
= wpa_config_get_network(wpa_s
->conf
, id
);
2074 return wpas_wps_network_config_token(wpa_s
, ndef
, ssid
);
2076 #endif /* CONFIG_WPS_ER */
2078 if (wpa_s
->ap_iface
)
2079 return wpas_ap_wps_nfc_config_token(wpa_s
, ndef
);
2080 #endif /* CONFIG_AP */
2085 struct wpabuf
* wpas_wps_nfc_token(struct wpa_supplicant
*wpa_s
, int ndef
)
2087 if (wpa_s
->conf
->wps_nfc_pw_from_config
) {
2088 return wps_nfc_token_build(ndef
,
2089 wpa_s
->conf
->wps_nfc_dev_pw_id
,
2090 wpa_s
->conf
->wps_nfc_dh_pubkey
,
2091 wpa_s
->conf
->wps_nfc_dev_pw
);
2094 return wps_nfc_token_gen(ndef
, &wpa_s
->conf
->wps_nfc_dev_pw_id
,
2095 &wpa_s
->conf
->wps_nfc_dh_pubkey
,
2096 &wpa_s
->conf
->wps_nfc_dh_privkey
,
2097 &wpa_s
->conf
->wps_nfc_dev_pw
);
2101 int wpas_wps_start_nfc(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
2102 const struct wpabuf
*dev_pw
, u16 dev_pw_id
,
2103 int p2p_group
, const u8
*peer_pubkey_hash
,
2104 const u8
*ssid
, size_t ssid_len
)
2106 struct wps_context
*wps
= wpa_s
->wps
;
2107 char pw
[32 * 2 + 1];
2109 if (dev_pw
== NULL
) {
2110 dev_pw
= wpa_s
->conf
->wps_nfc_dev_pw
;
2111 dev_pw_id
= wpa_s
->conf
->wps_nfc_dev_pw_id
;
2114 if (wpa_s
->conf
->wps_nfc_dh_pubkey
== NULL
||
2115 wpa_s
->conf
->wps_nfc_dh_privkey
== NULL
||
2119 dh5_free(wps
->dh_ctx
);
2120 wpabuf_free(wps
->dh_pubkey
);
2121 wpabuf_free(wps
->dh_privkey
);
2122 wps
->dh_privkey
= wpabuf_dup(wpa_s
->conf
->wps_nfc_dh_privkey
);
2123 wps
->dh_pubkey
= wpabuf_dup(wpa_s
->conf
->wps_nfc_dh_pubkey
);
2124 if (wps
->dh_privkey
== NULL
|| wps
->dh_pubkey
== NULL
) {
2126 wpabuf_free(wps
->dh_pubkey
);
2127 wps
->dh_pubkey
= NULL
;
2128 wpabuf_free(wps
->dh_privkey
);
2129 wps
->dh_privkey
= NULL
;
2132 wps
->dh_ctx
= dh5_init_fixed(wps
->dh_privkey
, wps
->dh_pubkey
);
2133 if (wps
->dh_ctx
== NULL
) {
2134 wpabuf_free(wps
->dh_pubkey
);
2135 wps
->dh_pubkey
= NULL
;
2136 wpabuf_free(wps
->dh_privkey
);
2137 wps
->dh_privkey
= NULL
;
2141 wpa_snprintf_hex_uppercase(pw
, sizeof(pw
),
2142 wpabuf_head(dev_pw
), wpabuf_len(dev_pw
));
2143 return wpas_wps_start_dev_pw(wpa_s
, bssid
, pw
, p2p_group
, dev_pw_id
,
2144 peer_pubkey_hash
, ssid
, ssid_len
);
2148 static int wpas_wps_use_cred(struct wpa_supplicant
*wpa_s
,
2149 struct wps_parse_attr
*attr
)
2151 wpa_s
->wps_ap_channel
= 0;
2154 * Disable existing networks temporarily to allow the newly learned
2155 * credential to be preferred. Enable the temporarily disabled networks
2158 wpas_wps_temp_disable(wpa_s
, NULL
);
2159 eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb
, wpa_s
,
2162 if (wps_oob_use_cred(wpa_s
->wps
, attr
) < 0)
2165 if (wpa_s
->wpa_state
== WPA_INTERFACE_DISABLED
)
2168 wpa_printf(MSG_DEBUG
, "WPS: Request reconnection with new network "
2169 "based on the received credential added");
2170 wpa_s
->normal_scans
= 0;
2171 wpa_supplicant_reinit_autoscan(wpa_s
);
2172 if (wpa_s
->wps_ap_channel
) {
2173 u16 chan
= wpa_s
->wps_ap_channel
;
2176 if (chan
>= 1 && chan
<= 13)
2177 freq
= 2407 + 5 * chan
;
2178 else if (chan
== 14)
2180 else if (chan
>= 30)
2181 freq
= 5000 + 5 * chan
;
2184 wpa_printf(MSG_DEBUG
, "WPS: Credential indicated "
2185 "AP channel %u -> %u MHz", chan
, freq
);
2186 wpa_s
->after_wps
= 5;
2187 wpa_s
->wps_freq
= freq
;
2190 wpa_s
->disconnected
= 0;
2191 wpa_s
->reassociate
= 1;
2193 wpa_supplicant_cancel_sched_scan(wpa_s
);
2194 wpa_supplicant_req_scan(wpa_s
, 0, 0);
2200 #ifdef CONFIG_WPS_ER
2201 static int wpas_wps_add_nfc_password_token(struct wpa_supplicant
*wpa_s
,
2202 struct wps_parse_attr
*attr
)
2204 return wps_registrar_add_nfc_password_token(
2205 wpa_s
->wps
->registrar
, attr
->oob_dev_password
,
2206 attr
->oob_dev_password_len
);
2208 #endif /* CONFIG_WPS_ER */
2211 static int wpas_wps_nfc_tag_process(struct wpa_supplicant
*wpa_s
,
2212 const struct wpabuf
*wps
)
2214 struct wps_parse_attr attr
;
2216 wpa_hexdump_buf(MSG_DEBUG
, "WPS: Received NFC tag payload", wps
);
2218 if (wps_parse_msg(wps
, &attr
)) {
2219 wpa_printf(MSG_DEBUG
, "WPS: Ignore invalid data from NFC tag");
2224 return wpas_wps_use_cred(wpa_s
, &attr
);
2226 #ifdef CONFIG_WPS_ER
2227 if (attr
.oob_dev_password
)
2228 return wpas_wps_add_nfc_password_token(wpa_s
, &attr
);
2229 #endif /* CONFIG_WPS_ER */
2231 wpa_printf(MSG_DEBUG
, "WPS: Ignore unrecognized NFC tag");
2236 int wpas_wps_nfc_tag_read(struct wpa_supplicant
*wpa_s
,
2237 const struct wpabuf
*data
)
2239 const struct wpabuf
*wps
= data
;
2240 struct wpabuf
*tmp
= NULL
;
2243 if (wpabuf_len(data
) < 4)
2246 if (*wpabuf_head_u8(data
) != 0x10) {
2247 /* Assume this contains full NDEF record */
2248 tmp
= ndef_parse_wifi(data
);
2250 wpa_printf(MSG_DEBUG
, "WPS: Could not parse NDEF");
2256 ret
= wpas_wps_nfc_tag_process(wpa_s
, wps
);
2262 struct wpabuf
* wpas_wps_nfc_handover_req(struct wpa_supplicant
*wpa_s
, int cr
)
2265 return ndef_build_wifi_hc(1);
2266 return ndef_build_wifi_hr();
2270 #ifdef CONFIG_WPS_NFC
2271 static struct wpabuf
*
2272 wpas_wps_er_nfc_handover_sel(struct wpa_supplicant
*wpa_s
, int ndef
,
2275 #ifdef CONFIG_WPS_ER
2277 u8 u
[UUID_LEN
], *use_uuid
= NULL
;
2278 u8 addr
[ETH_ALEN
], *use_addr
= NULL
;
2285 if (uuid_str2bin(uuid
, u
) == 0)
2287 else if (hwaddr_aton(uuid
, addr
) == 0)
2293 * Handover Select carrier record for WPS uses the same format as
2294 * configuration token.
2296 ret
= wps_er_nfc_config_token(wpa_s
->wps_er
, use_uuid
, use_addr
);
2299 tmp
= ndef_build_wifi(ret
);
2307 #else /* CONFIG_WPS_ER */
2309 #endif /* CONFIG_WPS_ER */
2311 #endif /* CONFIG_WPS_NFC */
2314 struct wpabuf
* wpas_wps_nfc_handover_sel(struct wpa_supplicant
*wpa_s
,
2315 int ndef
, int cr
, const char *uuid
)
2320 ret
= wpas_ap_wps_nfc_handover_sel(wpa_s
, ndef
);
2323 return wpas_wps_er_nfc_handover_sel(wpa_s
, ndef
, uuid
);
2327 int wpas_wps_nfc_rx_handover_req(struct wpa_supplicant
*wpa_s
,
2328 const struct wpabuf
*data
)
2335 int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant
*wpa_s
,
2336 const struct wpabuf
*data
)
2341 wps
= ndef_parse_wifi(data
);
2344 wpa_printf(MSG_DEBUG
, "WPS: Received application/vnd.wfa.wsc "
2345 "payload from NFC connection handover");
2346 wpa_hexdump_buf_key(MSG_DEBUG
, "WPS: NFC payload", wps
);
2347 ret
= wpas_wps_nfc_tag_process(wpa_s
, wps
);
2354 int wpas_wps_nfc_report_handover(struct wpa_supplicant
*wpa_s
,
2355 const struct wpabuf
*req
,
2356 const struct wpabuf
*sel
)
2358 wpa_printf(MSG_DEBUG
, "NFC: WPS connection handover reported");
2359 wpa_hexdump_buf_key(MSG_DEBUG
, "WPS: Carrier record in request", req
);
2360 wpa_hexdump_buf_key(MSG_DEBUG
, "WPS: Carrier record in select", sel
);
2361 return wpas_wps_nfc_rx_handover_sel(wpa_s
, sel
);
2364 #endif /* CONFIG_WPS_NFC */
2367 static void wpas_wps_dump_ap_info(struct wpa_supplicant
*wpa_s
)
2370 struct os_reltime now
;
2372 if (wpa_debug_level
> MSG_DEBUG
)
2375 if (wpa_s
->wps_ap
== NULL
)
2378 os_get_reltime(&now
);
2380 for (i
= 0; i
< wpa_s
->num_wps_ap
; i
++) {
2381 struct wps_ap_info
*ap
= &wpa_s
->wps_ap
[i
];
2382 struct wpa_blacklist
*e
= wpa_blacklist_get(wpa_s
, ap
->bssid
);
2384 wpa_printf(MSG_DEBUG
, "WPS: AP[%d] " MACSTR
" type=%d "
2385 "tries=%d last_attempt=%d sec ago blacklist=%d",
2386 (int) i
, MAC2STR(ap
->bssid
), ap
->type
, ap
->tries
,
2387 ap
->last_attempt
.sec
> 0 ?
2388 (int) now
.sec
- (int) ap
->last_attempt
.sec
: -1,
2394 static struct wps_ap_info
* wpas_wps_get_ap_info(struct wpa_supplicant
*wpa_s
,
2399 if (wpa_s
->wps_ap
== NULL
)
2402 for (i
= 0; i
< wpa_s
->num_wps_ap
; i
++) {
2403 struct wps_ap_info
*ap
= &wpa_s
->wps_ap
[i
];
2404 if (os_memcmp(ap
->bssid
, bssid
, ETH_ALEN
) == 0)
2412 static void wpas_wps_update_ap_info_bss(struct wpa_supplicant
*wpa_s
,
2413 struct wpa_scan_res
*res
)
2416 enum wps_ap_info_type type
;
2417 struct wps_ap_info
*ap
;
2420 if (wpa_scan_get_vendor_ie(res
, WPS_IE_VENDOR_TYPE
) == NULL
)
2423 wps
= wpa_scan_get_vendor_ie_multi(res
, WPS_IE_VENDOR_TYPE
);
2427 r
= wps_is_addr_authorized(wps
, wpa_s
->own_addr
, 1);
2429 type
= WPS_AP_SEL_REG_OUR
;
2431 type
= WPS_AP_SEL_REG
;
2433 type
= WPS_AP_NOT_SEL_REG
;
2437 ap
= wpas_wps_get_ap_info(wpa_s
, res
->bssid
);
2439 if (ap
->type
!= type
) {
2440 wpa_printf(MSG_DEBUG
, "WPS: AP " MACSTR
2441 " changed type %d -> %d",
2442 MAC2STR(res
->bssid
), ap
->type
, type
);
2444 if (type
!= WPS_AP_NOT_SEL_REG
)
2445 wpa_blacklist_del(wpa_s
, ap
->bssid
);
2450 ap
= os_realloc_array(wpa_s
->wps_ap
, wpa_s
->num_wps_ap
+ 1,
2451 sizeof(struct wps_ap_info
));
2456 ap
= &wpa_s
->wps_ap
[wpa_s
->num_wps_ap
];
2457 wpa_s
->num_wps_ap
++;
2459 os_memset(ap
, 0, sizeof(*ap
));
2460 os_memcpy(ap
->bssid
, res
->bssid
, ETH_ALEN
);
2462 wpa_printf(MSG_DEBUG
, "WPS: AP " MACSTR
" type %d added",
2463 MAC2STR(ap
->bssid
), ap
->type
);
2467 void wpas_wps_update_ap_info(struct wpa_supplicant
*wpa_s
,
2468 struct wpa_scan_results
*scan_res
)
2472 for (i
= 0; i
< scan_res
->num
; i
++)
2473 wpas_wps_update_ap_info_bss(wpa_s
, scan_res
->res
[i
]);
2475 wpas_wps_dump_ap_info(wpa_s
);
2479 void wpas_wps_notify_assoc(struct wpa_supplicant
*wpa_s
, const u8
*bssid
)
2481 struct wps_ap_info
*ap
;
2483 wpa_s
->after_wps
= 0;
2485 if (!wpa_s
->wps_ap_iter
)
2487 ap
= wpas_wps_get_ap_info(wpa_s
, bssid
);
2491 os_get_reltime(&ap
->last_attempt
);