2 * wpa_supplicant / WPS integration
3 * Copyright (c) 2008-2010, Jouni Malinen <j@w1.fi>
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.
20 #include "crypto/dh_group5.h"
21 #include "common/ieee802_11_defs.h"
22 #include "common/ieee802_11_common.h"
23 #include "common/wpa_common.h"
24 #include "common/wpa_ctrl.h"
25 #include "eap_common/eap_wsc_common.h"
26 #include "eap_peer/eap.h"
27 #include "rsn_supp/wpa.h"
29 #include "wpa_supplicant_i.h"
32 #include "blacklist.h"
35 #include "wps_supplicant.h"
38 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
40 static void wpas_wps_timeout(void *eloop_ctx
, void *timeout_ctx
);
41 static void wpas_clear_wps(struct wpa_supplicant
*wpa_s
);
44 int wpas_wps_eapol_cb(struct wpa_supplicant
*wpa_s
)
46 if (!wpa_s
->wps_success
&&
47 wpa_s
->current_ssid
&&
48 eap_is_wps_pin_enrollee(&wpa_s
->current_ssid
->eap
)) {
49 const u8
*bssid
= wpa_s
->bssid
;
50 if (is_zero_ether_addr(bssid
))
51 bssid
= wpa_s
->pending_bssid
;
53 wpa_printf(MSG_DEBUG
, "WPS: PIN registration with " MACSTR
54 " did not succeed - continue trying to find "
55 "suitable AP", MAC2STR(bssid
));
56 wpa_blacklist_add(wpa_s
, bssid
);
58 wpa_supplicant_deauthenticate(wpa_s
,
59 WLAN_REASON_DEAUTH_LEAVING
);
60 wpa_s
->reassociate
= 1;
61 wpa_supplicant_req_scan(wpa_s
,
62 wpa_s
->blacklist_cleared
? 5 : 0, 0);
63 wpa_s
->blacklist_cleared
= 0;
67 eloop_cancel_timeout(wpas_wps_timeout
, wpa_s
, NULL
);
69 if (wpa_s
->key_mgmt
== WPA_KEY_MGMT_WPS
&& wpa_s
->current_ssid
&&
70 !(wpa_s
->current_ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)) {
71 wpa_printf(MSG_DEBUG
, "WPS: Network configuration replaced - "
72 "try to associate with the received credential");
73 wpa_supplicant_deauthenticate(wpa_s
,
74 WLAN_REASON_DEAUTH_LEAVING
);
76 wpa_s
->wps_freq
= wpa_s
->assoc_freq
;
77 wpa_s
->reassociate
= 1;
78 wpa_supplicant_req_scan(wpa_s
, 0, 0);
82 if (wpa_s
->key_mgmt
== WPA_KEY_MGMT_WPS
&& wpa_s
->current_ssid
) {
83 wpa_printf(MSG_DEBUG
, "WPS: Registration completed - waiting "
84 "for external credential processing");
85 wpas_clear_wps(wpa_s
);
86 wpa_supplicant_deauthenticate(wpa_s
,
87 WLAN_REASON_DEAUTH_LEAVING
);
95 static void wpas_wps_security_workaround(struct wpa_supplicant
*wpa_s
,
96 struct wpa_ssid
*ssid
,
97 const struct wps_credential
*cred
)
99 struct wpa_driver_capa capa
;
102 struct wpa_ie_data adv
;
103 int wpa2
= 0, ccmp
= 0;
106 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
107 * case they are configured for mixed mode operation (WPA+WPA2 and
108 * TKIP+CCMP). Try to use scan results to figure out whether the AP
109 * actually supports stronger security and select that if the client
110 * has support for it, too.
113 if (wpa_drv_get_capa(wpa_s
, &capa
))
114 return; /* Unknown what driver supports */
116 bss
= wpa_bss_get(wpa_s
, cred
->mac_addr
, ssid
->ssid
, ssid
->ssid_len
);
118 wpa_printf(MSG_DEBUG
, "WPS: The AP was not found from BSS "
119 "table - use credential as-is");
123 wpa_printf(MSG_DEBUG
, "WPS: AP found from BSS table");
125 ie
= wpa_bss_get_ie(bss
, WLAN_EID_RSN
);
126 if (ie
&& wpa_parse_wpa_ie(ie
, 2 + ie
[1], &adv
) == 0) {
128 if (adv
.pairwise_cipher
& WPA_CIPHER_CCMP
)
131 ie
= wpa_bss_get_vendor_ie(bss
, WPA_IE_VENDOR_TYPE
);
132 if (ie
&& wpa_parse_wpa_ie(ie
, 2 + ie
[1], &adv
) == 0 &&
133 adv
.pairwise_cipher
& WPA_CIPHER_CCMP
)
137 if (ie
== NULL
&& (ssid
->proto
& WPA_PROTO_WPA
) &&
138 (ssid
->pairwise_cipher
& WPA_CIPHER_TKIP
)) {
140 * TODO: This could be the initial AP configuration and the
141 * Beacon contents could change shortly. Should request a new
142 * scan and delay addition of the network until the updated
143 * scan results are available.
145 wpa_printf(MSG_DEBUG
, "WPS: The AP did not yet advertise WPA "
146 "support - use credential as-is");
150 if (ccmp
&& !(ssid
->pairwise_cipher
& WPA_CIPHER_CCMP
) &&
151 (ssid
->pairwise_cipher
& WPA_CIPHER_TKIP
) &&
152 (capa
.key_mgmt
& WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
)) {
153 wpa_printf(MSG_DEBUG
, "WPS: Add CCMP into the credential "
154 "based on scan results");
155 if (wpa_s
->conf
->ap_scan
== 1)
156 ssid
->pairwise_cipher
|= WPA_CIPHER_CCMP
;
158 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
161 if (wpa2
&& !(ssid
->proto
& WPA_PROTO_RSN
) &&
162 (ssid
->proto
& WPA_PROTO_WPA
) &&
163 (capa
.enc
& WPA_DRIVER_CAPA_ENC_CCMP
)) {
164 wpa_printf(MSG_DEBUG
, "WPS: Add WPA2 into the credential "
165 "based on scan results");
166 if (wpa_s
->conf
->ap_scan
== 1)
167 ssid
->proto
|= WPA_PROTO_RSN
;
169 ssid
->proto
= WPA_PROTO_RSN
;
174 static int wpa_supplicant_wps_cred(void *ctx
,
175 const struct wps_credential
*cred
)
177 struct wpa_supplicant
*wpa_s
= ctx
;
178 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
182 if ((wpa_s
->conf
->wps_cred_processing
== 1 ||
183 wpa_s
->conf
->wps_cred_processing
== 2) && cred
->cred_attr
) {
184 size_t blen
= cred
->cred_attr_len
* 2 + 1;
185 char *buf
= os_malloc(blen
);
187 wpa_snprintf_hex(buf
, blen
,
188 cred
->cred_attr
, cred
->cred_attr_len
);
189 wpa_msg(wpa_s
, MSG_INFO
, "%s%s",
190 WPS_EVENT_CRED_RECEIVED
, buf
);
194 wpas_notify_wps_credential(wpa_s
, cred
);
196 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_CRED_RECEIVED
);
198 wpa_hexdump_key(MSG_DEBUG
, "WPS: Received Credential attribute",
199 cred
->cred_attr
, cred
->cred_attr_len
);
201 if (wpa_s
->conf
->wps_cred_processing
== 1)
204 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: SSID", cred
->ssid
, cred
->ssid_len
);
205 wpa_printf(MSG_DEBUG
, "WPS: Authentication Type 0x%x",
207 wpa_printf(MSG_DEBUG
, "WPS: Encryption Type 0x%x", cred
->encr_type
);
208 wpa_printf(MSG_DEBUG
, "WPS: Network Key Index %d", cred
->key_idx
);
209 wpa_hexdump_key(MSG_DEBUG
, "WPS: Network Key",
210 cred
->key
, cred
->key_len
);
211 wpa_printf(MSG_DEBUG
, "WPS: MAC Address " MACSTR
,
212 MAC2STR(cred
->mac_addr
));
214 auth_type
= cred
->auth_type
;
215 if (auth_type
== (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
)) {
216 wpa_printf(MSG_DEBUG
, "WPS: Workaround - convert mixed-mode "
217 "auth_type into WPA2PSK");
218 auth_type
= WPS_AUTH_WPA2PSK
;
221 if (auth_type
!= WPS_AUTH_OPEN
&&
222 auth_type
!= WPS_AUTH_SHARED
&&
223 auth_type
!= WPS_AUTH_WPAPSK
&&
224 auth_type
!= WPS_AUTH_WPA2PSK
) {
225 wpa_printf(MSG_DEBUG
, "WPS: Ignored credentials for "
226 "unsupported authentication type 0x%x",
231 if (ssid
&& (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)) {
232 wpa_printf(MSG_DEBUG
, "WPS: Replace WPS network block based "
233 "on the received credential");
234 os_free(ssid
->eap
.identity
);
235 ssid
->eap
.identity
= NULL
;
236 ssid
->eap
.identity_len
= 0;
237 os_free(ssid
->eap
.phase1
);
238 ssid
->eap
.phase1
= NULL
;
239 os_free(ssid
->eap
.eap_methods
);
240 ssid
->eap
.eap_methods
= NULL
;
242 wpa_printf(MSG_DEBUG
, "WPS: Create a new network based on the "
243 "received credential");
244 ssid
= wpa_config_add_network(wpa_s
->conf
);
247 wpas_notify_network_added(wpa_s
, ssid
);
250 wpa_config_set_network_defaults(ssid
);
253 ssid
->ssid
= os_malloc(cred
->ssid_len
);
255 os_memcpy(ssid
->ssid
, cred
->ssid
, cred
->ssid_len
);
256 ssid
->ssid_len
= cred
->ssid_len
;
259 switch (cred
->encr_type
) {
263 if (cred
->key_len
<= 0)
265 if (cred
->key_len
!= 5 && cred
->key_len
!= 13 &&
266 cred
->key_len
!= 10 && cred
->key_len
!= 26) {
267 wpa_printf(MSG_ERROR
, "WPS: Invalid WEP Key length "
268 "%lu", (unsigned long) cred
->key_len
);
271 if (cred
->key_idx
> NUM_WEP_KEYS
) {
272 wpa_printf(MSG_ERROR
, "WPS: Invalid WEP Key index %d",
277 key_idx
= cred
->key_idx
- 1;
278 if (cred
->key_len
== 10 || cred
->key_len
== 26) {
279 if (hexstr2bin((char *) cred
->key
,
280 ssid
->wep_key
[key_idx
],
281 cred
->key_len
/ 2) < 0) {
282 wpa_printf(MSG_ERROR
, "WPS: Invalid WEP Key "
286 ssid
->wep_key_len
[key_idx
] = cred
->key_len
/ 2;
288 os_memcpy(ssid
->wep_key
[key_idx
], cred
->key
,
290 ssid
->wep_key_len
[key_idx
] = cred
->key_len
;
292 ssid
->wep_tx_keyidx
= key_idx
;
295 ssid
->pairwise_cipher
= WPA_CIPHER_TKIP
;
298 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
304 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
305 ssid
->key_mgmt
= WPA_KEY_MGMT_NONE
;
308 case WPS_AUTH_SHARED
:
309 ssid
->auth_alg
= WPA_AUTH_ALG_SHARED
;
310 ssid
->key_mgmt
= WPA_KEY_MGMT_NONE
;
313 case WPS_AUTH_WPAPSK
:
314 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
315 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
316 ssid
->proto
= WPA_PROTO_WPA
;
319 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
320 ssid
->key_mgmt
= WPA_KEY_MGMT_IEEE8021X
;
321 ssid
->proto
= WPA_PROTO_WPA
;
324 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
325 ssid
->key_mgmt
= WPA_KEY_MGMT_IEEE8021X
;
326 ssid
->proto
= WPA_PROTO_RSN
;
328 case WPS_AUTH_WPA2PSK
:
329 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
330 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
331 ssid
->proto
= WPA_PROTO_RSN
;
335 if (ssid
->key_mgmt
== WPA_KEY_MGMT_PSK
) {
336 if (cred
->key_len
== 2 * PMK_LEN
) {
337 if (hexstr2bin((const char *) cred
->key
, ssid
->psk
,
339 wpa_printf(MSG_ERROR
, "WPS: Invalid Network "
344 } else if (cred
->key_len
>= 8 && cred
->key_len
< 2 * PMK_LEN
) {
345 os_free(ssid
->passphrase
);
346 ssid
->passphrase
= os_malloc(cred
->key_len
+ 1);
347 if (ssid
->passphrase
== NULL
)
349 os_memcpy(ssid
->passphrase
, cred
->key
, cred
->key_len
);
350 ssid
->passphrase
[cred
->key_len
] = '\0';
351 wpa_config_update_psk(ssid
);
353 wpa_printf(MSG_ERROR
, "WPS: Invalid Network Key "
355 (unsigned long) cred
->key_len
);
360 wpas_wps_security_workaround(wpa_s
, ssid
, cred
);
362 #ifndef CONFIG_NO_CONFIG_WRITE
363 if (wpa_s
->conf
->update_config
&&
364 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
365 wpa_printf(MSG_DEBUG
, "WPS: Failed to update configuration");
368 #endif /* CONFIG_NO_CONFIG_WRITE */
374 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant
*wpa_s
,
375 struct wps_event_m2d
*m2d
)
377 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_M2D
378 "dev_password_id=%d config_error=%d",
379 m2d
->dev_password_id
, m2d
->config_error
);
380 wpas_notify_wps_event_m2d(wpa_s
, m2d
);
384 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant
*wpa_s
,
385 struct wps_event_fail
*fail
)
387 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_FAIL
"msg=%d", fail
->msg
);
388 wpas_clear_wps(wpa_s
);
389 wpas_notify_wps_event_fail(wpa_s
, fail
);
393 static void wpa_supplicant_wps_event_success(struct wpa_supplicant
*wpa_s
)
395 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_SUCCESS
);
396 wpa_s
->wps_success
= 1;
397 wpas_notify_wps_event_success(wpa_s
);
401 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant
*wpa_s
,
402 struct wps_event_er_ap
*ap
)
405 char dev_type
[WPS_DEV_TYPE_BUFSIZE
];
407 uuid_bin2str(ap
->uuid
, uuid_str
, sizeof(uuid_str
));
408 if (ap
->pri_dev_type
)
409 wps_dev_type_bin2str(ap
->pri_dev_type
, dev_type
,
414 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_AP_ADD
"%s " MACSTR
415 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
416 uuid_str
, MAC2STR(ap
->mac_addr
), dev_type
, ap
->wps_state
,
417 ap
->friendly_name
? ap
->friendly_name
: "",
418 ap
->manufacturer
? ap
->manufacturer
: "",
419 ap
->model_description
? ap
->model_description
: "",
420 ap
->model_name
? ap
->model_name
: "",
421 ap
->manufacturer_url
? ap
->manufacturer_url
: "",
422 ap
->model_url
? ap
->model_url
: "");
426 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant
*wpa_s
,
427 struct wps_event_er_ap
*ap
)
430 uuid_bin2str(ap
->uuid
, uuid_str
, sizeof(uuid_str
));
431 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_AP_REMOVE
"%s", uuid_str
);
435 static void wpa_supplicant_wps_event_er_enrollee_add(
436 struct wpa_supplicant
*wpa_s
, struct wps_event_er_enrollee
*enrollee
)
439 char dev_type
[WPS_DEV_TYPE_BUFSIZE
];
441 uuid_bin2str(enrollee
->uuid
, uuid_str
, sizeof(uuid_str
));
442 if (enrollee
->pri_dev_type
)
443 wps_dev_type_bin2str(enrollee
->pri_dev_type
, dev_type
,
448 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_ENROLLEE_ADD
"%s " MACSTR
449 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
451 uuid_str
, MAC2STR(enrollee
->mac_addr
), enrollee
->m1_received
,
452 enrollee
->config_methods
, enrollee
->dev_passwd_id
, dev_type
,
453 enrollee
->dev_name
? enrollee
->dev_name
: "",
454 enrollee
->manufacturer
? enrollee
->manufacturer
: "",
455 enrollee
->model_name
? enrollee
->model_name
: "",
456 enrollee
->model_number
? enrollee
->model_number
: "",
457 enrollee
->serial_number
? enrollee
->serial_number
: "");
461 static void wpa_supplicant_wps_event_er_enrollee_remove(
462 struct wpa_supplicant
*wpa_s
, struct wps_event_er_enrollee
*enrollee
)
465 uuid_bin2str(enrollee
->uuid
, uuid_str
, sizeof(uuid_str
));
466 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_ENROLLEE_REMOVE
"%s " MACSTR
,
467 uuid_str
, MAC2STR(enrollee
->mac_addr
));
471 static void wpa_supplicant_wps_event(void *ctx
, enum wps_event event
,
472 union wps_event_data
*data
)
474 struct wpa_supplicant
*wpa_s
= ctx
;
477 wpa_supplicant_wps_event_m2d(wpa_s
, &data
->m2d
);
480 wpa_supplicant_wps_event_fail(wpa_s
, &data
->fail
);
483 wpa_supplicant_wps_event_success(wpa_s
);
485 case WPS_EV_PWD_AUTH_FAIL
:
487 case WPS_EV_PBC_OVERLAP
:
489 case WPS_EV_PBC_TIMEOUT
:
491 case WPS_EV_ER_AP_ADD
:
492 wpa_supplicant_wps_event_er_ap_add(wpa_s
, &data
->ap
);
494 case WPS_EV_ER_AP_REMOVE
:
495 wpa_supplicant_wps_event_er_ap_remove(wpa_s
, &data
->ap
);
497 case WPS_EV_ER_ENROLLEE_ADD
:
498 wpa_supplicant_wps_event_er_enrollee_add(wpa_s
,
501 case WPS_EV_ER_ENROLLEE_REMOVE
:
502 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s
,
509 enum wps_request_type
wpas_wps_get_req_type(struct wpa_ssid
*ssid
)
511 if (eap_is_wps_pbc_enrollee(&ssid
->eap
) ||
512 eap_is_wps_pin_enrollee(&ssid
->eap
))
513 return WPS_REQ_ENROLLEE
;
515 return WPS_REQ_REGISTRAR
;
519 static void wpas_clear_wps(struct wpa_supplicant
*wpa_s
)
522 struct wpa_ssid
*ssid
, *remove_ssid
= NULL
;
524 eloop_cancel_timeout(wpas_wps_timeout
, wpa_s
, NULL
);
526 /* Remove any existing WPS network from configuration */
527 ssid
= wpa_s
->conf
->ssid
;
529 if (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
) {
530 if (ssid
== wpa_s
->current_ssid
) {
531 wpa_s
->current_ssid
= NULL
;
533 wpas_notify_network_changed(wpa_s
);
541 wpas_notify_network_removed(wpa_s
, remove_ssid
);
542 wpa_config_remove_network(wpa_s
->conf
, id
);
548 static void wpas_wps_timeout(void *eloop_ctx
, void *timeout_ctx
)
550 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
551 wpa_printf(MSG_INFO
, WPS_EVENT_TIMEOUT
"Requested operation timed "
553 wpas_clear_wps(wpa_s
);
557 static struct wpa_ssid
* wpas_wps_add_network(struct wpa_supplicant
*wpa_s
,
558 int registrar
, const u8
*bssid
)
560 struct wpa_ssid
*ssid
;
562 ssid
= wpa_config_add_network(wpa_s
->conf
);
565 wpas_notify_network_added(wpa_s
, ssid
);
566 wpa_config_set_network_defaults(ssid
);
567 if (wpa_config_set(ssid
, "key_mgmt", "WPS", 0) < 0 ||
568 wpa_config_set(ssid
, "eap", "WSC", 0) < 0 ||
569 wpa_config_set(ssid
, "identity", registrar
?
570 "\"" WSC_ID_REGISTRAR
"\"" :
571 "\"" WSC_ID_ENROLLEE
"\"", 0) < 0) {
572 wpas_notify_network_removed(wpa_s
, ssid
);
573 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
581 os_memcpy(ssid
->bssid
, bssid
, ETH_ALEN
);
584 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
585 if (os_memcmp(bssid
, bss
->bssid
, ETH_ALEN
) != 0)
589 ssid
->ssid
= os_malloc(bss
->ssid_len
);
590 if (ssid
->ssid
== NULL
)
592 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
593 ssid
->ssid_len
= bss
->ssid_len
;
594 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Picked SSID from "
596 ssid
->ssid
, ssid
->ssid_len
);
601 wpa_printf(MSG_DEBUG
, "WPS: More than one SSID found "
602 "for the AP; use wildcard");
613 static void wpas_wps_reassoc(struct wpa_supplicant
*wpa_s
,
614 struct wpa_ssid
*selected
)
616 struct wpa_ssid
*ssid
;
618 /* Mark all other networks disabled and trigger reassociation */
619 ssid
= wpa_s
->conf
->ssid
;
621 int was_disabled
= ssid
->disabled
;
622 ssid
->disabled
= ssid
!= selected
;
623 if (was_disabled
!= ssid
->disabled
)
624 wpas_notify_network_enabled_changed(wpa_s
, ssid
);
627 wpa_s
->disconnected
= 0;
628 wpa_s
->reassociate
= 1;
629 wpa_s
->scan_runs
= 0;
630 wpa_s
->wps_success
= 0;
631 wpa_s
->blacklist_cleared
= 0;
632 wpa_supplicant_req_scan(wpa_s
, 0, 0);
636 int wpas_wps_start_pbc(struct wpa_supplicant
*wpa_s
, const u8
*bssid
)
638 struct wpa_ssid
*ssid
;
639 wpas_clear_wps(wpa_s
);
640 ssid
= wpas_wps_add_network(wpa_s
, 0, bssid
);
643 wpa_config_set(ssid
, "phase1", "\"pbc=1\"", 0);
644 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0, wpas_wps_timeout
,
646 wpas_wps_reassoc(wpa_s
, ssid
);
651 int wpas_wps_start_pin(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
654 struct wpa_ssid
*ssid
;
656 unsigned int rpin
= 0;
658 wpas_clear_wps(wpa_s
);
659 ssid
= wpas_wps_add_network(wpa_s
, 0, bssid
);
663 os_snprintf(val
, sizeof(val
), "\"pin=%s\"", pin
);
665 rpin
= wps_generate_pin();
666 os_snprintf(val
, sizeof(val
), "\"pin=%08d\"", rpin
);
668 wpa_config_set(ssid
, "phase1", val
, 0);
669 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0, wpas_wps_timeout
,
671 wpas_wps_reassoc(wpa_s
, ssid
);
676 #ifdef CONFIG_WPS_OOB
677 int wpas_wps_start_oob(struct wpa_supplicant
*wpa_s
, char *device_type
,
678 char *path
, char *method
, char *name
)
680 struct wps_context
*wps
= wpa_s
->wps
;
681 struct oob_device_data
*oob_dev
;
683 oob_dev
= wps_get_oob_device(device_type
);
686 oob_dev
->device_path
= path
;
687 oob_dev
->device_name
= name
;
688 wps
->oob_conf
.oob_method
= wps_get_oob_method(method
);
690 if (wps
->oob_conf
.oob_method
== OOB_METHOD_DEV_PWD_E
) {
692 * Use pre-configured DH keys in order to be able to write the
693 * key hash into the OOB file.
695 wpabuf_free(wps
->dh_pubkey
);
696 wpabuf_free(wps
->dh_privkey
);
697 wps
->dh_privkey
= NULL
;
698 wps
->dh_pubkey
= NULL
;
699 dh5_free(wps
->dh_ctx
);
700 wps
->dh_ctx
= dh5_init(&wps
->dh_privkey
, &wps
->dh_pubkey
);
701 wps
->dh_pubkey
= wpabuf_zeropad(wps
->dh_pubkey
, 192);
702 if (wps
->dh_ctx
== NULL
|| wps
->dh_pubkey
== NULL
) {
703 wpa_printf(MSG_ERROR
, "WPS: Failed to initialize "
704 "Diffie-Hellman handshake");
709 if (wps
->oob_conf
.oob_method
== OOB_METHOD_CRED
)
710 wpas_clear_wps(wpa_s
);
712 if (wps_process_oob(wps
, oob_dev
, 0) < 0)
715 if ((wps
->oob_conf
.oob_method
== OOB_METHOD_DEV_PWD_E
||
716 wps
->oob_conf
.oob_method
== OOB_METHOD_DEV_PWD_R
) &&
717 wpas_wps_start_pin(wpa_s
, NULL
,
718 wpabuf_head(wps
->oob_conf
.dev_password
)) < 0)
723 #endif /* CONFIG_WPS_OOB */
726 int wpas_wps_start_reg(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
727 const char *pin
, struct wps_new_ap_settings
*settings
)
729 struct wpa_ssid
*ssid
;
736 wpas_clear_wps(wpa_s
);
737 ssid
= wpas_wps_add_network(wpa_s
, 1, bssid
);
741 end
= pos
+ sizeof(val
);
742 res
= os_snprintf(pos
, end
- pos
, "\"pin=%s", pin
);
743 if (res
< 0 || res
>= end
- pos
)
747 res
= os_snprintf(pos
, end
- pos
, " new_ssid=%s new_auth=%s "
748 "new_encr=%s new_key=%s",
749 settings
->ssid_hex
, settings
->auth
,
750 settings
->encr
, settings
->key_hex
);
751 if (res
< 0 || res
>= end
- pos
)
755 res
= os_snprintf(pos
, end
- pos
, "\"");
756 if (res
< 0 || res
>= end
- pos
)
758 wpa_config_set(ssid
, "phase1", val
, 0);
759 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0, wpas_wps_timeout
,
761 wpas_wps_reassoc(wpa_s
, ssid
);
766 static int wpas_wps_new_psk_cb(void *ctx
, const u8
*mac_addr
, const u8
*psk
,
769 wpa_printf(MSG_DEBUG
, "WPS: Received new WPA/WPA2-PSK from WPS for "
770 "STA " MACSTR
, MAC2STR(mac_addr
));
771 wpa_hexdump_key(MSG_DEBUG
, "Per-device PSK", psk
, psk_len
);
779 static void wpas_wps_pin_needed_cb(void *ctx
, const u8
*uuid_e
,
780 const struct wps_device_data
*dev
)
782 char uuid
[40], txt
[400];
784 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
785 if (uuid_bin2str(uuid_e
, uuid
, sizeof(uuid
)))
787 wpa_printf(MSG_DEBUG
, "WPS: PIN needed for UUID-E %s", uuid
);
788 len
= os_snprintf(txt
, sizeof(txt
), "WPS-EVENT-PIN-NEEDED %s " MACSTR
789 " [%s|%s|%s|%s|%s|%s]",
790 uuid
, MAC2STR(dev
->mac_addr
), dev
->device_name
,
791 dev
->manufacturer
, dev
->model_name
,
792 dev
->model_number
, dev
->serial_number
,
793 wps_dev_type_bin2str(dev
->pri_dev_type
, devtype
,
795 if (len
> 0 && len
< (int) sizeof(txt
))
796 wpa_printf(MSG_INFO
, "%s", txt
);
800 static void wpas_wps_set_sel_reg_cb(void *ctx
, int sel_reg
, u16 dev_passwd_id
,
801 u16 sel_reg_config_methods
)
804 struct wpa_supplicant
*wpa_s
= ctx
;
806 if (wpa_s
->wps_er
== NULL
)
808 wps_er_set_sel_reg(wpa_s
->wps_er
, sel_reg
, dev_passwd_id
,
809 sel_reg_config_methods
);
810 #endif /* CONFIG_WPS_ER */
814 int wpas_wps_init(struct wpa_supplicant
*wpa_s
)
816 struct wps_context
*wps
;
817 struct wps_registrar_config rcfg
;
819 wps
= os_zalloc(sizeof(*wps
));
823 wps
->cred_cb
= wpa_supplicant_wps_cred
;
824 wps
->event_cb
= wpa_supplicant_wps_event
;
827 wps
->dev
.device_name
= wpa_s
->conf
->device_name
;
828 wps
->dev
.manufacturer
= wpa_s
->conf
->manufacturer
;
829 wps
->dev
.model_name
= wpa_s
->conf
->model_name
;
830 wps
->dev
.model_number
= wpa_s
->conf
->model_number
;
831 wps
->dev
.serial_number
= wpa_s
->conf
->serial_number
;
832 wps
->config_methods
=
833 wps_config_methods_str2bin(wpa_s
->conf
->config_methods
);
834 if (wpa_s
->conf
->device_type
&&
835 wps_dev_type_str2bin(wpa_s
->conf
->device_type
,
836 wps
->dev
.pri_dev_type
) < 0) {
837 wpa_printf(MSG_ERROR
, "WPS: Invalid device_type");
841 wps
->dev
.os_version
= WPA_GET_BE32(wpa_s
->conf
->os_version
);
842 wps
->dev
.rf_bands
= WPS_RF_24GHZ
| WPS_RF_50GHZ
; /* TODO: config */
843 os_memcpy(wps
->dev
.mac_addr
, wpa_s
->own_addr
, ETH_ALEN
);
844 if (is_nil_uuid(wpa_s
->conf
->uuid
)) {
845 uuid_gen_mac_addr(wpa_s
->own_addr
, wps
->uuid
);
846 wpa_hexdump(MSG_DEBUG
, "WPS: UUID based on MAC address",
847 wps
->uuid
, WPS_UUID_LEN
);
849 os_memcpy(wps
->uuid
, wpa_s
->conf
->uuid
, WPS_UUID_LEN
);
851 wps
->auth_types
= WPS_AUTH_WPA2PSK
| WPS_AUTH_WPAPSK
;
852 wps
->encr_types
= WPS_ENCR_AES
| WPS_ENCR_TKIP
;
854 os_memset(&rcfg
, 0, sizeof(rcfg
));
855 rcfg
.new_psk_cb
= wpas_wps_new_psk_cb
;
856 rcfg
.pin_needed_cb
= wpas_wps_pin_needed_cb
;
857 rcfg
.set_sel_reg_cb
= wpas_wps_set_sel_reg_cb
;
860 wps
->registrar
= wps_registrar_init(wps
, &rcfg
);
861 if (wps
->registrar
== NULL
) {
862 wpa_printf(MSG_DEBUG
, "Failed to initialize WPS Registrar");
873 void wpas_wps_deinit(struct wpa_supplicant
*wpa_s
)
875 eloop_cancel_timeout(wpas_wps_timeout
, wpa_s
, NULL
);
877 if (wpa_s
->wps
== NULL
)
881 wps_er_deinit(wpa_s
->wps_er
, NULL
, NULL
);
882 wpa_s
->wps_er
= NULL
;
883 #endif /* CONFIG_WPS_ER */
885 wps_registrar_deinit(wpa_s
->wps
->registrar
);
886 wpabuf_free(wpa_s
->wps
->dh_pubkey
);
887 wpabuf_free(wpa_s
->wps
->dh_privkey
);
888 wpabuf_free(wpa_s
->wps
->oob_conf
.pubkey_hash
);
889 wpabuf_free(wpa_s
->wps
->oob_conf
.dev_password
);
890 os_free(wpa_s
->wps
->network_key
);
896 int wpas_wps_ssid_bss_match(struct wpa_supplicant
*wpa_s
,
897 struct wpa_ssid
*ssid
, struct wpa_scan_res
*bss
)
899 struct wpabuf
*wps_ie
;
901 if (!(ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))
904 wps_ie
= wpa_scan_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
905 if (eap_is_wps_pbc_enrollee(&ssid
->eap
)) {
907 wpa_printf(MSG_DEBUG
, " skip - non-WPS AP");
911 if (!wps_is_selected_pbc_registrar(wps_ie
)) {
912 wpa_printf(MSG_DEBUG
, " skip - WPS AP "
913 "without active PBC Registrar");
918 /* TODO: overlap detection */
919 wpa_printf(MSG_DEBUG
, " selected based on WPS IE "
925 if (eap_is_wps_pin_enrollee(&ssid
->eap
)) {
927 wpa_printf(MSG_DEBUG
, " skip - non-WPS AP");
932 * Start with WPS APs that advertise active PIN Registrar and
933 * allow any WPS AP after third scan since some APs do not set
934 * Selected Registrar attribute properly when using external
937 if (!wps_is_selected_pin_registrar(wps_ie
)) {
938 if (wpa_s
->scan_runs
< WPS_PIN_SCAN_IGNORE_SEL_REG
) {
939 wpa_printf(MSG_DEBUG
, " skip - WPS AP "
940 "without active PIN Registrar");
944 wpa_printf(MSG_DEBUG
, " selected based on WPS IE");
946 wpa_printf(MSG_DEBUG
, " selected based on WPS IE "
954 wpa_printf(MSG_DEBUG
, " selected based on WPS IE");
963 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant
*wpa_s
,
964 struct wpa_ssid
*ssid
,
965 struct wpa_scan_res
*bss
)
967 struct wpabuf
*wps_ie
= NULL
;
970 if (eap_is_wps_pbc_enrollee(&ssid
->eap
)) {
971 wps_ie
= wpa_scan_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
972 if (wps_ie
&& wps_is_selected_pbc_registrar(wps_ie
)) {
973 /* allow wildcard SSID for WPS PBC */
976 } else if (eap_is_wps_pin_enrollee(&ssid
->eap
)) {
977 wps_ie
= wpa_scan_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
979 (wps_is_selected_pin_registrar(wps_ie
) ||
980 wpa_s
->scan_runs
>= WPS_PIN_SCAN_IGNORE_SEL_REG
)) {
981 /* allow wildcard SSID for WPS PIN */
986 if (!ret
&& ssid
->bssid_set
&&
987 os_memcmp(ssid
->bssid
, bss
->bssid
, ETH_ALEN
) == 0) {
988 /* allow wildcard SSID due to hardcoded BSSID match */
998 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant
*wpa_s
,
999 struct wpa_bss
*selected
, struct wpa_ssid
*ssid
)
1001 const u8
*sel_uuid
, *uuid
;
1002 struct wpabuf
*wps_ie
;
1004 struct wpa_bss
*bss
;
1006 if (!eap_is_wps_pbc_enrollee(&ssid
->eap
))
1009 /* Make sure that only one AP is in active PBC mode */
1010 wps_ie
= wpa_bss_get_vendor_ie_multi(selected
, WPS_IE_VENDOR_TYPE
);
1012 sel_uuid
= wps_get_uuid_e(wps_ie
);
1016 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1018 if (bss
== selected
)
1020 ie
= wpa_bss_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
1023 if (!wps_is_selected_pbc_registrar(ie
)) {
1027 uuid
= wps_get_uuid_e(ie
);
1028 if (sel_uuid
== NULL
|| uuid
== NULL
||
1029 os_memcmp(sel_uuid
, uuid
, 16) != 0) {
1030 ret
= 1; /* PBC overlap */
1035 /* TODO: verify that this is reasonable dual-band situation */
1040 wpabuf_free(wps_ie
);
1046 void wpas_wps_notify_scan_results(struct wpa_supplicant
*wpa_s
)
1048 struct wpa_bss
*bss
;
1050 if (wpa_s
->disconnected
|| wpa_s
->wpa_state
>= WPA_ASSOCIATED
)
1053 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1055 ie
= wpa_bss_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
1058 if (wps_is_selected_pbc_registrar(ie
))
1059 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1060 WPS_EVENT_AP_AVAILABLE_PBC
);
1061 else if (wps_is_selected_pin_registrar(ie
))
1062 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1063 WPS_EVENT_AP_AVAILABLE_PIN
);
1065 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1066 WPS_EVENT_AP_AVAILABLE
);
1073 int wpas_wps_searching(struct wpa_supplicant
*wpa_s
)
1075 struct wpa_ssid
*ssid
;
1077 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
1078 if ((ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
) && !ssid
->disabled
)
1086 int wpas_wps_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
1089 struct wpabuf
*wps_ie
;
1092 wps_ie
= ieee802_11_vendor_ie_concat(ies
, ies_len
, WPS_DEV_OUI_WFA
);
1096 ret
= wps_attr_text(wps_ie
, buf
, end
);
1097 wpabuf_free(wps_ie
);
1102 int wpas_wps_er_start(struct wpa_supplicant
*wpa_s
)
1104 #ifdef CONFIG_WPS_ER
1105 if (wpa_s
->wps_er
) {
1106 wps_er_refresh(wpa_s
->wps_er
);
1109 wpa_s
->wps_er
= wps_er_init(wpa_s
->wps
, wpa_s
->ifname
);
1110 if (wpa_s
->wps_er
== NULL
)
1113 #else /* CONFIG_WPS_ER */
1115 #endif /* CONFIG_WPS_ER */
1119 int wpas_wps_er_stop(struct wpa_supplicant
*wpa_s
)
1121 #ifdef CONFIG_WPS_ER
1122 wps_er_deinit(wpa_s
->wps_er
, NULL
, NULL
);
1123 wpa_s
->wps_er
= NULL
;
1124 #endif /* CONFIG_WPS_ER */
1129 #ifdef CONFIG_WPS_ER
1130 int wpas_wps_er_add_pin(struct wpa_supplicant
*wpa_s
, const char *uuid
,
1136 if (os_strcmp(uuid
, "any") == 0)
1138 else if (uuid_str2bin(uuid
, u
))
1140 return wps_registrar_add_pin(wpa_s
->wps
->registrar
, any
? NULL
: u
,
1141 (const u8
*) pin
, os_strlen(pin
), 300);
1145 int wpas_wps_er_pbc(struct wpa_supplicant
*wpa_s
, const char *uuid
)
1149 if (uuid_str2bin(uuid
, u
))
1151 return wps_er_pbc(wpa_s
->wps_er
, u
);
1155 int wpas_wps_er_learn(struct wpa_supplicant
*wpa_s
, const char *uuid
,
1160 if (uuid_str2bin(uuid
, u
))
1162 return wps_er_learn(wpa_s
->wps_er
, u
, (const u8
*) pin
,
1167 static void wpas_wps_terminate_cb(void *ctx
)
1169 wpa_printf(MSG_DEBUG
, "WPS ER: Terminated");
1172 #endif /* CONFIG_WPS_ER */
1175 int wpas_wps_terminate_pending(struct wpa_supplicant
*wpa_s
)
1177 #ifdef CONFIG_WPS_ER
1178 if (wpa_s
->wps_er
) {
1179 wps_er_deinit(wpa_s
->wps_er
, wpas_wps_terminate_cb
, wpa_s
);
1180 wpa_s
->wps_er
= NULL
;
1183 #endif /* CONFIG_WPS_ER */