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
);
75 wpa_s
->reassociate
= 1;
76 wpa_supplicant_req_scan(wpa_s
, 0, 0);
80 if (wpa_s
->key_mgmt
== WPA_KEY_MGMT_WPS
&& wpa_s
->current_ssid
) {
81 wpa_printf(MSG_DEBUG
, "WPS: Registration completed - waiting "
82 "for external credential processing");
83 wpas_clear_wps(wpa_s
);
84 wpa_supplicant_deauthenticate(wpa_s
,
85 WLAN_REASON_DEAUTH_LEAVING
);
93 static void wpas_wps_security_workaround(struct wpa_supplicant
*wpa_s
,
94 struct wpa_ssid
*ssid
,
95 const struct wps_credential
*cred
)
97 struct wpa_driver_capa capa
;
100 struct wpa_ie_data adv
;
101 int wpa2
= 0, ccmp
= 0;
104 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
105 * case they are configured for mixed mode operation (WPA+WPA2 and
106 * TKIP+CCMP). Try to use scan results to figure out whether the AP
107 * actually supports stronger security and select that if the client
108 * has support for it, too.
111 if (wpa_drv_get_capa(wpa_s
, &capa
))
112 return; /* Unknown what driver supports */
114 bss
= wpa_bss_get(wpa_s
, cred
->mac_addr
, ssid
->ssid
, ssid
->ssid_len
);
116 wpa_printf(MSG_DEBUG
, "WPS: The AP was not found from BSS "
117 "table - use credential as-is");
121 wpa_printf(MSG_DEBUG
, "WPS: AP found from BSS table");
123 ie
= wpa_bss_get_ie(bss
, WLAN_EID_RSN
);
124 if (ie
&& wpa_parse_wpa_ie(ie
, 2 + ie
[1], &adv
) == 0) {
126 if (adv
.pairwise_cipher
& WPA_CIPHER_CCMP
)
129 ie
= wpa_bss_get_vendor_ie(bss
, WPA_IE_VENDOR_TYPE
);
130 if (ie
&& wpa_parse_wpa_ie(ie
, 2 + ie
[1], &adv
) == 0 &&
131 adv
.pairwise_cipher
& WPA_CIPHER_CCMP
)
135 if (ie
== NULL
&& (ssid
->proto
& WPA_PROTO_WPA
) &&
136 (ssid
->pairwise_cipher
& WPA_CIPHER_TKIP
)) {
138 * TODO: This could be the initial AP configuration and the
139 * Beacon contents could change shortly. Should request a new
140 * scan and delay addition of the network until the updated
141 * scan results are available.
143 wpa_printf(MSG_DEBUG
, "WPS: The AP did not yet advertise WPA "
144 "support - use credential as-is");
148 if (ccmp
&& !(ssid
->pairwise_cipher
& WPA_CIPHER_CCMP
) &&
149 (ssid
->pairwise_cipher
& WPA_CIPHER_TKIP
) &&
150 (capa
.key_mgmt
& WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
)) {
151 wpa_printf(MSG_DEBUG
, "WPS: Add CCMP into the credential "
152 "based on scan results");
153 if (wpa_s
->conf
->ap_scan
== 1)
154 ssid
->pairwise_cipher
|= WPA_CIPHER_CCMP
;
156 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
159 if (wpa2
&& !(ssid
->proto
& WPA_PROTO_RSN
) &&
160 (ssid
->proto
& WPA_PROTO_WPA
) &&
161 (capa
.enc
& WPA_DRIVER_CAPA_ENC_CCMP
)) {
162 wpa_printf(MSG_DEBUG
, "WPS: Add WPA2 into the credential "
163 "based on scan results");
164 if (wpa_s
->conf
->ap_scan
== 1)
165 ssid
->proto
|= WPA_PROTO_RSN
;
167 ssid
->proto
= WPA_PROTO_RSN
;
172 static int wpa_supplicant_wps_cred(void *ctx
,
173 const struct wps_credential
*cred
)
175 struct wpa_supplicant
*wpa_s
= ctx
;
176 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
180 if ((wpa_s
->conf
->wps_cred_processing
== 1 ||
181 wpa_s
->conf
->wps_cred_processing
== 2) && cred
->cred_attr
) {
182 size_t blen
= cred
->cred_attr_len
* 2 + 1;
183 char *buf
= os_malloc(blen
);
185 wpa_snprintf_hex(buf
, blen
,
186 cred
->cred_attr
, cred
->cred_attr_len
);
187 wpa_msg(wpa_s
, MSG_INFO
, "%s%s",
188 WPS_EVENT_CRED_RECEIVED
, buf
);
192 wpas_notify_wps_credential(wpa_s
, cred
);
194 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_CRED_RECEIVED
);
196 wpa_hexdump_key(MSG_DEBUG
, "WPS: Received Credential attribute",
197 cred
->cred_attr
, cred
->cred_attr_len
);
199 if (wpa_s
->conf
->wps_cred_processing
== 1)
202 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: SSID", cred
->ssid
, cred
->ssid_len
);
203 wpa_printf(MSG_DEBUG
, "WPS: Authentication Type 0x%x",
205 wpa_printf(MSG_DEBUG
, "WPS: Encryption Type 0x%x", cred
->encr_type
);
206 wpa_printf(MSG_DEBUG
, "WPS: Network Key Index %d", cred
->key_idx
);
207 wpa_hexdump_key(MSG_DEBUG
, "WPS: Network Key",
208 cred
->key
, cred
->key_len
);
209 wpa_printf(MSG_DEBUG
, "WPS: MAC Address " MACSTR
,
210 MAC2STR(cred
->mac_addr
));
212 auth_type
= cred
->auth_type
;
213 if (auth_type
== (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
)) {
214 wpa_printf(MSG_DEBUG
, "WPS: Workaround - convert mixed-mode "
215 "auth_type into WPA2PSK");
216 auth_type
= WPS_AUTH_WPA2PSK
;
219 if (auth_type
!= WPS_AUTH_OPEN
&&
220 auth_type
!= WPS_AUTH_SHARED
&&
221 auth_type
!= WPS_AUTH_WPAPSK
&&
222 auth_type
!= WPS_AUTH_WPA2PSK
) {
223 wpa_printf(MSG_DEBUG
, "WPS: Ignored credentials for "
224 "unsupported authentication type 0x%x",
229 if (ssid
&& (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)) {
230 wpa_printf(MSG_DEBUG
, "WPS: Replace WPS network block based "
231 "on the received credential");
232 os_free(ssid
->eap
.identity
);
233 ssid
->eap
.identity
= NULL
;
234 ssid
->eap
.identity_len
= 0;
235 os_free(ssid
->eap
.phase1
);
236 ssid
->eap
.phase1
= NULL
;
237 os_free(ssid
->eap
.eap_methods
);
238 ssid
->eap
.eap_methods
= NULL
;
240 wpa_printf(MSG_DEBUG
, "WPS: Create a new network based on the "
241 "received credential");
242 ssid
= wpa_config_add_network(wpa_s
->conf
);
245 wpas_notify_network_added(wpa_s
, ssid
);
248 wpa_config_set_network_defaults(ssid
);
251 ssid
->ssid
= os_malloc(cred
->ssid_len
);
253 os_memcpy(ssid
->ssid
, cred
->ssid
, cred
->ssid_len
);
254 ssid
->ssid_len
= cred
->ssid_len
;
257 switch (cred
->encr_type
) {
261 if (cred
->key_len
<= 0)
263 if (cred
->key_len
!= 5 && cred
->key_len
!= 13 &&
264 cred
->key_len
!= 10 && cred
->key_len
!= 26) {
265 wpa_printf(MSG_ERROR
, "WPS: Invalid WEP Key length "
266 "%lu", (unsigned long) cred
->key_len
);
269 if (cred
->key_idx
> NUM_WEP_KEYS
) {
270 wpa_printf(MSG_ERROR
, "WPS: Invalid WEP Key index %d",
275 key_idx
= cred
->key_idx
- 1;
276 if (cred
->key_len
== 10 || cred
->key_len
== 26) {
277 if (hexstr2bin((char *) cred
->key
,
278 ssid
->wep_key
[key_idx
],
279 cred
->key_len
/ 2) < 0) {
280 wpa_printf(MSG_ERROR
, "WPS: Invalid WEP Key "
284 ssid
->wep_key_len
[key_idx
] = cred
->key_len
/ 2;
286 os_memcpy(ssid
->wep_key
[key_idx
], cred
->key
,
288 ssid
->wep_key_len
[key_idx
] = cred
->key_len
;
290 ssid
->wep_tx_keyidx
= key_idx
;
293 ssid
->pairwise_cipher
= WPA_CIPHER_TKIP
;
296 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
302 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
303 ssid
->key_mgmt
= WPA_KEY_MGMT_NONE
;
306 case WPS_AUTH_SHARED
:
307 ssid
->auth_alg
= WPA_AUTH_ALG_SHARED
;
308 ssid
->key_mgmt
= WPA_KEY_MGMT_NONE
;
311 case WPS_AUTH_WPAPSK
:
312 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
313 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
314 ssid
->proto
= WPA_PROTO_WPA
;
317 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
318 ssid
->key_mgmt
= WPA_KEY_MGMT_IEEE8021X
;
319 ssid
->proto
= WPA_PROTO_WPA
;
322 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
323 ssid
->key_mgmt
= WPA_KEY_MGMT_IEEE8021X
;
324 ssid
->proto
= WPA_PROTO_RSN
;
326 case WPS_AUTH_WPA2PSK
:
327 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
328 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
329 ssid
->proto
= WPA_PROTO_RSN
;
333 if (ssid
->key_mgmt
== WPA_KEY_MGMT_PSK
) {
334 if (cred
->key_len
== 2 * PMK_LEN
) {
335 if (hexstr2bin((const char *) cred
->key
, ssid
->psk
,
337 wpa_printf(MSG_ERROR
, "WPS: Invalid Network "
342 } else if (cred
->key_len
>= 8 && cred
->key_len
< 2 * PMK_LEN
) {
343 os_free(ssid
->passphrase
);
344 ssid
->passphrase
= os_malloc(cred
->key_len
+ 1);
345 if (ssid
->passphrase
== NULL
)
347 os_memcpy(ssid
->passphrase
, cred
->key
, cred
->key_len
);
348 ssid
->passphrase
[cred
->key_len
] = '\0';
349 wpa_config_update_psk(ssid
);
351 wpa_printf(MSG_ERROR
, "WPS: Invalid Network Key "
353 (unsigned long) cred
->key_len
);
358 wpas_wps_security_workaround(wpa_s
, ssid
, cred
);
360 #ifndef CONFIG_NO_CONFIG_WRITE
361 if (wpa_s
->conf
->update_config
&&
362 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
363 wpa_printf(MSG_DEBUG
, "WPS: Failed to update configuration");
366 #endif /* CONFIG_NO_CONFIG_WRITE */
372 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant
*wpa_s
,
373 struct wps_event_m2d
*m2d
)
375 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_M2D
376 "dev_password_id=%d config_error=%d",
377 m2d
->dev_password_id
, m2d
->config_error
);
378 wpas_notify_wps_event_m2d(wpa_s
, m2d
);
382 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant
*wpa_s
,
383 struct wps_event_fail
*fail
)
385 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_FAIL
"msg=%d", fail
->msg
);
386 wpas_clear_wps(wpa_s
);
387 wpas_notify_wps_event_fail(wpa_s
, fail
);
391 static void wpa_supplicant_wps_event_success(struct wpa_supplicant
*wpa_s
)
393 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_SUCCESS
);
394 wpa_s
->wps_success
= 1;
395 wpas_notify_wps_event_success(wpa_s
);
399 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant
*wpa_s
,
400 struct wps_event_er_ap
*ap
)
403 char dev_type
[WPS_DEV_TYPE_BUFSIZE
];
405 uuid_bin2str(ap
->uuid
, uuid_str
, sizeof(uuid_str
));
406 if (ap
->pri_dev_type
)
407 wps_dev_type_bin2str(ap
->pri_dev_type
, dev_type
,
412 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_AP_ADD
"%s " MACSTR
413 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
414 uuid_str
, MAC2STR(ap
->mac_addr
), dev_type
, ap
->wps_state
,
415 ap
->friendly_name
? ap
->friendly_name
: "",
416 ap
->manufacturer
? ap
->manufacturer
: "",
417 ap
->model_description
? ap
->model_description
: "",
418 ap
->model_name
? ap
->model_name
: "",
419 ap
->manufacturer_url
? ap
->manufacturer_url
: "",
420 ap
->model_url
? ap
->model_url
: "");
424 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant
*wpa_s
,
425 struct wps_event_er_ap
*ap
)
428 uuid_bin2str(ap
->uuid
, uuid_str
, sizeof(uuid_str
));
429 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_AP_REMOVE
"%s", uuid_str
);
433 static void wpa_supplicant_wps_event_er_enrollee_add(
434 struct wpa_supplicant
*wpa_s
, struct wps_event_er_enrollee
*enrollee
)
437 char dev_type
[WPS_DEV_TYPE_BUFSIZE
];
439 uuid_bin2str(enrollee
->uuid
, uuid_str
, sizeof(uuid_str
));
440 if (enrollee
->pri_dev_type
)
441 wps_dev_type_bin2str(enrollee
->pri_dev_type
, dev_type
,
446 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_ENROLLEE_ADD
"%s " MACSTR
447 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
449 uuid_str
, MAC2STR(enrollee
->mac_addr
), enrollee
->m1_received
,
450 enrollee
->config_methods
, enrollee
->dev_passwd_id
, dev_type
,
451 enrollee
->dev_name
? enrollee
->dev_name
: "",
452 enrollee
->manufacturer
? enrollee
->manufacturer
: "",
453 enrollee
->model_name
? enrollee
->model_name
: "",
454 enrollee
->model_number
? enrollee
->model_number
: "",
455 enrollee
->serial_number
? enrollee
->serial_number
: "");
459 static void wpa_supplicant_wps_event_er_enrollee_remove(
460 struct wpa_supplicant
*wpa_s
, struct wps_event_er_enrollee
*enrollee
)
463 uuid_bin2str(enrollee
->uuid
, uuid_str
, sizeof(uuid_str
));
464 wpa_msg(wpa_s
, MSG_INFO
, WPS_EVENT_ER_ENROLLEE_REMOVE
"%s " MACSTR
,
465 uuid_str
, MAC2STR(enrollee
->mac_addr
));
469 static void wpa_supplicant_wps_event(void *ctx
, enum wps_event event
,
470 union wps_event_data
*data
)
472 struct wpa_supplicant
*wpa_s
= ctx
;
475 wpa_supplicant_wps_event_m2d(wpa_s
, &data
->m2d
);
478 wpa_supplicant_wps_event_fail(wpa_s
, &data
->fail
);
481 wpa_supplicant_wps_event_success(wpa_s
);
483 case WPS_EV_PWD_AUTH_FAIL
:
485 case WPS_EV_PBC_OVERLAP
:
487 case WPS_EV_PBC_TIMEOUT
:
489 case WPS_EV_ER_AP_ADD
:
490 wpa_supplicant_wps_event_er_ap_add(wpa_s
, &data
->ap
);
492 case WPS_EV_ER_AP_REMOVE
:
493 wpa_supplicant_wps_event_er_ap_remove(wpa_s
, &data
->ap
);
495 case WPS_EV_ER_ENROLLEE_ADD
:
496 wpa_supplicant_wps_event_er_enrollee_add(wpa_s
,
499 case WPS_EV_ER_ENROLLEE_REMOVE
:
500 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s
,
507 enum wps_request_type
wpas_wps_get_req_type(struct wpa_ssid
*ssid
)
509 if (eap_is_wps_pbc_enrollee(&ssid
->eap
) ||
510 eap_is_wps_pin_enrollee(&ssid
->eap
))
511 return WPS_REQ_ENROLLEE
;
513 return WPS_REQ_REGISTRAR
;
517 static void wpas_clear_wps(struct wpa_supplicant
*wpa_s
)
520 struct wpa_ssid
*ssid
, *remove_ssid
= NULL
;
522 eloop_cancel_timeout(wpas_wps_timeout
, wpa_s
, NULL
);
524 /* Remove any existing WPS network from configuration */
525 ssid
= wpa_s
->conf
->ssid
;
527 if (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
) {
528 if (ssid
== wpa_s
->current_ssid
) {
529 wpa_s
->current_ssid
= NULL
;
531 wpas_notify_network_changed(wpa_s
);
539 wpas_notify_network_removed(wpa_s
, remove_ssid
);
540 wpa_config_remove_network(wpa_s
->conf
, id
);
546 static void wpas_wps_timeout(void *eloop_ctx
, void *timeout_ctx
)
548 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
549 wpa_printf(MSG_INFO
, WPS_EVENT_TIMEOUT
"Requested operation timed "
551 wpas_clear_wps(wpa_s
);
555 static struct wpa_ssid
* wpas_wps_add_network(struct wpa_supplicant
*wpa_s
,
556 int registrar
, const u8
*bssid
)
558 struct wpa_ssid
*ssid
;
560 ssid
= wpa_config_add_network(wpa_s
->conf
);
563 wpas_notify_network_added(wpa_s
, ssid
);
564 wpa_config_set_network_defaults(ssid
);
565 if (wpa_config_set(ssid
, "key_mgmt", "WPS", 0) < 0 ||
566 wpa_config_set(ssid
, "eap", "WSC", 0) < 0 ||
567 wpa_config_set(ssid
, "identity", registrar
?
568 "\"" WSC_ID_REGISTRAR
"\"" :
569 "\"" WSC_ID_ENROLLEE
"\"", 0) < 0) {
570 wpas_notify_network_removed(wpa_s
, ssid
);
571 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
579 os_memcpy(ssid
->bssid
, bssid
, ETH_ALEN
);
582 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
583 if (os_memcmp(bssid
, bss
->bssid
, ETH_ALEN
) != 0)
587 ssid
->ssid
= os_malloc(bss
->ssid_len
);
588 if (ssid
->ssid
== NULL
)
590 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
591 ssid
->ssid_len
= bss
->ssid_len
;
592 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Picked SSID from "
594 ssid
->ssid
, ssid
->ssid_len
);
599 wpa_printf(MSG_DEBUG
, "WPS: More than one SSID found "
600 "for the AP; use wildcard");
611 static void wpas_wps_reassoc(struct wpa_supplicant
*wpa_s
,
612 struct wpa_ssid
*selected
)
614 struct wpa_ssid
*ssid
;
616 /* Mark all other networks disabled and trigger reassociation */
617 ssid
= wpa_s
->conf
->ssid
;
619 int was_disabled
= ssid
->disabled
;
620 ssid
->disabled
= ssid
!= selected
;
621 if (was_disabled
!= ssid
->disabled
)
622 wpas_notify_network_enabled_changed(wpa_s
, ssid
);
625 wpa_s
->disconnected
= 0;
626 wpa_s
->reassociate
= 1;
627 wpa_s
->scan_runs
= 0;
628 wpa_s
->wps_success
= 0;
629 wpa_s
->blacklist_cleared
= 0;
630 wpa_supplicant_req_scan(wpa_s
, 0, 0);
634 int wpas_wps_start_pbc(struct wpa_supplicant
*wpa_s
, const u8
*bssid
)
636 struct wpa_ssid
*ssid
;
637 wpas_clear_wps(wpa_s
);
638 ssid
= wpas_wps_add_network(wpa_s
, 0, bssid
);
641 wpa_config_set(ssid
, "phase1", "\"pbc=1\"", 0);
642 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0, wpas_wps_timeout
,
644 wpas_wps_reassoc(wpa_s
, ssid
);
649 int wpas_wps_start_pin(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
652 struct wpa_ssid
*ssid
;
654 unsigned int rpin
= 0;
656 wpas_clear_wps(wpa_s
);
657 ssid
= wpas_wps_add_network(wpa_s
, 0, bssid
);
661 os_snprintf(val
, sizeof(val
), "\"pin=%s\"", pin
);
663 rpin
= wps_generate_pin();
664 os_snprintf(val
, sizeof(val
), "\"pin=%08d\"", rpin
);
666 wpa_config_set(ssid
, "phase1", val
, 0);
667 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0, wpas_wps_timeout
,
669 wpas_wps_reassoc(wpa_s
, ssid
);
674 #ifdef CONFIG_WPS_OOB
675 int wpas_wps_start_oob(struct wpa_supplicant
*wpa_s
, char *device_type
,
676 char *path
, char *method
, char *name
)
678 struct wps_context
*wps
= wpa_s
->wps
;
679 struct oob_device_data
*oob_dev
;
681 oob_dev
= wps_get_oob_device(device_type
);
684 oob_dev
->device_path
= path
;
685 oob_dev
->device_name
= name
;
686 wps
->oob_conf
.oob_method
= wps_get_oob_method(method
);
688 if (wps
->oob_conf
.oob_method
== OOB_METHOD_DEV_PWD_E
) {
690 * Use pre-configured DH keys in order to be able to write the
691 * key hash into the OOB file.
693 wpabuf_free(wps
->dh_pubkey
);
694 wpabuf_free(wps
->dh_privkey
);
695 wps
->dh_privkey
= NULL
;
696 wps
->dh_pubkey
= NULL
;
697 dh5_free(wps
->dh_ctx
);
698 wps
->dh_ctx
= dh5_init(&wps
->dh_privkey
, &wps
->dh_pubkey
);
699 wps
->dh_pubkey
= wpabuf_zeropad(wps
->dh_pubkey
, 192);
700 if (wps
->dh_ctx
== NULL
|| wps
->dh_pubkey
== NULL
) {
701 wpa_printf(MSG_ERROR
, "WPS: Failed to initialize "
702 "Diffie-Hellman handshake");
707 if (wps
->oob_conf
.oob_method
== OOB_METHOD_CRED
)
708 wpas_clear_wps(wpa_s
);
710 if (wps_process_oob(wps
, oob_dev
, 0) < 0)
713 if ((wps
->oob_conf
.oob_method
== OOB_METHOD_DEV_PWD_E
||
714 wps
->oob_conf
.oob_method
== OOB_METHOD_DEV_PWD_R
) &&
715 wpas_wps_start_pin(wpa_s
, NULL
,
716 wpabuf_head(wps
->oob_conf
.dev_password
)) < 0)
721 #endif /* CONFIG_WPS_OOB */
724 int wpas_wps_start_reg(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
725 const char *pin
, struct wps_new_ap_settings
*settings
)
727 struct wpa_ssid
*ssid
;
734 wpas_clear_wps(wpa_s
);
735 ssid
= wpas_wps_add_network(wpa_s
, 1, bssid
);
739 end
= pos
+ sizeof(val
);
740 res
= os_snprintf(pos
, end
- pos
, "\"pin=%s", pin
);
741 if (res
< 0 || res
>= end
- pos
)
745 res
= os_snprintf(pos
, end
- pos
, " new_ssid=%s new_auth=%s "
746 "new_encr=%s new_key=%s",
747 settings
->ssid_hex
, settings
->auth
,
748 settings
->encr
, settings
->key_hex
);
749 if (res
< 0 || res
>= end
- pos
)
753 res
= os_snprintf(pos
, end
- pos
, "\"");
754 if (res
< 0 || res
>= end
- pos
)
756 wpa_config_set(ssid
, "phase1", val
, 0);
757 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0, wpas_wps_timeout
,
759 wpas_wps_reassoc(wpa_s
, ssid
);
764 static int wpas_wps_new_psk_cb(void *ctx
, const u8
*mac_addr
, const u8
*psk
,
767 wpa_printf(MSG_DEBUG
, "WPS: Received new WPA/WPA2-PSK from WPS for "
768 "STA " MACSTR
, MAC2STR(mac_addr
));
769 wpa_hexdump_key(MSG_DEBUG
, "Per-device PSK", psk
, psk_len
);
777 static void wpas_wps_pin_needed_cb(void *ctx
, const u8
*uuid_e
,
778 const struct wps_device_data
*dev
)
780 char uuid
[40], txt
[400];
782 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
783 if (uuid_bin2str(uuid_e
, uuid
, sizeof(uuid
)))
785 wpa_printf(MSG_DEBUG
, "WPS: PIN needed for UUID-E %s", uuid
);
786 len
= os_snprintf(txt
, sizeof(txt
), "WPS-EVENT-PIN-NEEDED %s " MACSTR
787 " [%s|%s|%s|%s|%s|%s]",
788 uuid
, MAC2STR(dev
->mac_addr
), dev
->device_name
,
789 dev
->manufacturer
, dev
->model_name
,
790 dev
->model_number
, dev
->serial_number
,
791 wps_dev_type_bin2str(dev
->pri_dev_type
, devtype
,
793 if (len
> 0 && len
< (int) sizeof(txt
))
794 wpa_printf(MSG_INFO
, "%s", txt
);
798 static void wpas_wps_set_sel_reg_cb(void *ctx
, int sel_reg
, u16 dev_passwd_id
,
799 u16 sel_reg_config_methods
)
802 struct wpa_supplicant
*wpa_s
= ctx
;
804 if (wpa_s
->wps_er
== NULL
)
806 wps_er_set_sel_reg(wpa_s
->wps_er
, sel_reg
, dev_passwd_id
,
807 sel_reg_config_methods
);
808 #endif /* CONFIG_WPS_ER */
812 int wpas_wps_init(struct wpa_supplicant
*wpa_s
)
814 struct wps_context
*wps
;
815 struct wps_registrar_config rcfg
;
817 wps
= os_zalloc(sizeof(*wps
));
821 wps
->cred_cb
= wpa_supplicant_wps_cred
;
822 wps
->event_cb
= wpa_supplicant_wps_event
;
825 wps
->dev
.device_name
= wpa_s
->conf
->device_name
;
826 wps
->dev
.manufacturer
= wpa_s
->conf
->manufacturer
;
827 wps
->dev
.model_name
= wpa_s
->conf
->model_name
;
828 wps
->dev
.model_number
= wpa_s
->conf
->model_number
;
829 wps
->dev
.serial_number
= wpa_s
->conf
->serial_number
;
830 wps
->config_methods
=
831 wps_config_methods_str2bin(wpa_s
->conf
->config_methods
);
832 if (wpa_s
->conf
->device_type
&&
833 wps_dev_type_str2bin(wpa_s
->conf
->device_type
,
834 wps
->dev
.pri_dev_type
) < 0) {
835 wpa_printf(MSG_ERROR
, "WPS: Invalid device_type");
839 wps
->dev
.os_version
= WPA_GET_BE32(wpa_s
->conf
->os_version
);
840 wps
->dev
.rf_bands
= WPS_RF_24GHZ
| WPS_RF_50GHZ
; /* TODO: config */
841 os_memcpy(wps
->dev
.mac_addr
, wpa_s
->own_addr
, ETH_ALEN
);
842 if (is_nil_uuid(wpa_s
->conf
->uuid
)) {
843 uuid_gen_mac_addr(wpa_s
->own_addr
, wps
->uuid
);
844 wpa_hexdump(MSG_DEBUG
, "WPS: UUID based on MAC address",
845 wps
->uuid
, WPS_UUID_LEN
);
847 os_memcpy(wps
->uuid
, wpa_s
->conf
->uuid
, WPS_UUID_LEN
);
849 wps
->auth_types
= WPS_AUTH_WPA2PSK
| WPS_AUTH_WPAPSK
;
850 wps
->encr_types
= WPS_ENCR_AES
| WPS_ENCR_TKIP
;
852 os_memset(&rcfg
, 0, sizeof(rcfg
));
853 rcfg
.new_psk_cb
= wpas_wps_new_psk_cb
;
854 rcfg
.pin_needed_cb
= wpas_wps_pin_needed_cb
;
855 rcfg
.set_sel_reg_cb
= wpas_wps_set_sel_reg_cb
;
858 wps
->registrar
= wps_registrar_init(wps
, &rcfg
);
859 if (wps
->registrar
== NULL
) {
860 wpa_printf(MSG_DEBUG
, "Failed to initialize WPS Registrar");
871 void wpas_wps_deinit(struct wpa_supplicant
*wpa_s
)
873 eloop_cancel_timeout(wpas_wps_timeout
, wpa_s
, NULL
);
875 if (wpa_s
->wps
== NULL
)
879 wps_er_deinit(wpa_s
->wps_er
, NULL
, NULL
);
880 wpa_s
->wps_er
= NULL
;
881 #endif /* CONFIG_WPS_ER */
883 wps_registrar_deinit(wpa_s
->wps
->registrar
);
884 wpabuf_free(wpa_s
->wps
->dh_pubkey
);
885 wpabuf_free(wpa_s
->wps
->dh_privkey
);
886 wpabuf_free(wpa_s
->wps
->oob_conf
.pubkey_hash
);
887 wpabuf_free(wpa_s
->wps
->oob_conf
.dev_password
);
888 os_free(wpa_s
->wps
->network_key
);
894 int wpas_wps_ssid_bss_match(struct wpa_supplicant
*wpa_s
,
895 struct wpa_ssid
*ssid
, struct wpa_scan_res
*bss
)
897 struct wpabuf
*wps_ie
;
899 if (!(ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))
902 wps_ie
= wpa_scan_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
903 if (eap_is_wps_pbc_enrollee(&ssid
->eap
)) {
905 wpa_printf(MSG_DEBUG
, " skip - non-WPS AP");
909 if (!wps_is_selected_pbc_registrar(wps_ie
)) {
910 wpa_printf(MSG_DEBUG
, " skip - WPS AP "
911 "without active PBC Registrar");
916 /* TODO: overlap detection */
917 wpa_printf(MSG_DEBUG
, " selected based on WPS IE "
923 if (eap_is_wps_pin_enrollee(&ssid
->eap
)) {
925 wpa_printf(MSG_DEBUG
, " skip - non-WPS AP");
930 * Start with WPS APs that advertise active PIN Registrar and
931 * allow any WPS AP after third scan since some APs do not set
932 * Selected Registrar attribute properly when using external
935 if (!wps_is_selected_pin_registrar(wps_ie
)) {
936 if (wpa_s
->scan_runs
< WPS_PIN_SCAN_IGNORE_SEL_REG
) {
937 wpa_printf(MSG_DEBUG
, " skip - WPS AP "
938 "without active PIN Registrar");
942 wpa_printf(MSG_DEBUG
, " selected based on WPS IE");
944 wpa_printf(MSG_DEBUG
, " selected based on WPS IE "
952 wpa_printf(MSG_DEBUG
, " selected based on WPS IE");
961 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant
*wpa_s
,
962 struct wpa_ssid
*ssid
,
963 struct wpa_scan_res
*bss
)
965 struct wpabuf
*wps_ie
= NULL
;
968 if (eap_is_wps_pbc_enrollee(&ssid
->eap
)) {
969 wps_ie
= wpa_scan_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
970 if (wps_ie
&& wps_is_selected_pbc_registrar(wps_ie
)) {
971 /* allow wildcard SSID for WPS PBC */
974 } else if (eap_is_wps_pin_enrollee(&ssid
->eap
)) {
975 wps_ie
= wpa_scan_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
977 (wps_is_selected_pin_registrar(wps_ie
) ||
978 wpa_s
->scan_runs
>= WPS_PIN_SCAN_IGNORE_SEL_REG
)) {
979 /* allow wildcard SSID for WPS PIN */
984 if (!ret
&& ssid
->bssid_set
&&
985 os_memcmp(ssid
->bssid
, bss
->bssid
, ETH_ALEN
) == 0) {
986 /* allow wildcard SSID due to hardcoded BSSID match */
996 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant
*wpa_s
,
997 struct wpa_bss
*selected
, struct wpa_ssid
*ssid
)
999 const u8
*sel_uuid
, *uuid
;
1000 struct wpabuf
*wps_ie
;
1002 struct wpa_bss
*bss
;
1004 if (!eap_is_wps_pbc_enrollee(&ssid
->eap
))
1007 /* Make sure that only one AP is in active PBC mode */
1008 wps_ie
= wpa_bss_get_vendor_ie_multi(selected
, WPS_IE_VENDOR_TYPE
);
1010 sel_uuid
= wps_get_uuid_e(wps_ie
);
1014 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1016 if (bss
== selected
)
1018 ie
= wpa_bss_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
1021 if (!wps_is_selected_pbc_registrar(ie
)) {
1025 uuid
= wps_get_uuid_e(ie
);
1026 if (sel_uuid
== NULL
|| uuid
== NULL
||
1027 os_memcmp(sel_uuid
, uuid
, 16) != 0) {
1028 ret
= 1; /* PBC overlap */
1033 /* TODO: verify that this is reasonable dual-band situation */
1038 wpabuf_free(wps_ie
);
1044 void wpas_wps_notify_scan_results(struct wpa_supplicant
*wpa_s
)
1046 struct wpa_bss
*bss
;
1048 if (wpa_s
->disconnected
|| wpa_s
->wpa_state
>= WPA_ASSOCIATED
)
1051 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1053 ie
= wpa_bss_get_vendor_ie_multi(bss
, WPS_IE_VENDOR_TYPE
);
1056 if (wps_is_selected_pbc_registrar(ie
))
1057 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1058 WPS_EVENT_AP_AVAILABLE_PBC
);
1059 else if (wps_is_selected_pin_registrar(ie
))
1060 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1061 WPS_EVENT_AP_AVAILABLE_PIN
);
1063 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1064 WPS_EVENT_AP_AVAILABLE
);
1071 int wpas_wps_searching(struct wpa_supplicant
*wpa_s
)
1073 struct wpa_ssid
*ssid
;
1075 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
1076 if ((ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
) && !ssid
->disabled
)
1084 int wpas_wps_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
1087 struct wpabuf
*wps_ie
;
1090 wps_ie
= ieee802_11_vendor_ie_concat(ies
, ies_len
, WPS_DEV_OUI_WFA
);
1094 ret
= wps_attr_text(wps_ie
, buf
, end
);
1095 wpabuf_free(wps_ie
);
1100 int wpas_wps_er_start(struct wpa_supplicant
*wpa_s
)
1102 #ifdef CONFIG_WPS_ER
1103 if (wpa_s
->wps_er
) {
1104 wps_er_refresh(wpa_s
->wps_er
);
1107 wpa_s
->wps_er
= wps_er_init(wpa_s
->wps
, wpa_s
->ifname
);
1108 if (wpa_s
->wps_er
== NULL
)
1111 #else /* CONFIG_WPS_ER */
1113 #endif /* CONFIG_WPS_ER */
1117 int wpas_wps_er_stop(struct wpa_supplicant
*wpa_s
)
1119 #ifdef CONFIG_WPS_ER
1120 wps_er_deinit(wpa_s
->wps_er
, NULL
, NULL
);
1121 wpa_s
->wps_er
= NULL
;
1122 #endif /* CONFIG_WPS_ER */
1127 #ifdef CONFIG_WPS_ER
1128 int wpas_wps_er_add_pin(struct wpa_supplicant
*wpa_s
, const char *uuid
,
1134 if (os_strcmp(uuid
, "any") == 0)
1136 else if (uuid_str2bin(uuid
, u
))
1138 return wps_registrar_add_pin(wpa_s
->wps
->registrar
, any
? NULL
: u
,
1139 (const u8
*) pin
, os_strlen(pin
), 300);
1143 int wpas_wps_er_pbc(struct wpa_supplicant
*wpa_s
, const char *uuid
)
1147 if (uuid_str2bin(uuid
, u
))
1149 return wps_er_pbc(wpa_s
->wps_er
, u
);
1153 int wpas_wps_er_learn(struct wpa_supplicant
*wpa_s
, const char *uuid
,
1158 if (uuid_str2bin(uuid
, u
))
1160 return wps_er_learn(wpa_s
->wps_er
, u
, (const u8
*) pin
,
1165 static void wpas_wps_terminate_cb(void *ctx
)
1167 wpa_printf(MSG_DEBUG
, "WPS ER: Terminated");
1170 #endif /* CONFIG_WPS_ER */
1173 int wpas_wps_terminate_pending(struct wpa_supplicant
*wpa_s
)
1175 #ifdef CONFIG_WPS_ER
1176 if (wpa_s
->wps_er
) {
1177 wps_er_deinit(wpa_s
->wps_er
, wpas_wps_terminate_cb
, wpa_s
);
1178 wpa_s
->wps_er
= NULL
;
1181 #endif /* CONFIG_WPS_ER */