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 #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 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
48 if (!wpa_s->wps_success &&
49 wpa_s->current_ssid &&
50 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
51 const u8 *bssid = wpa_s->bssid;
52 if (is_zero_ether_addr(bssid))
53 bssid = wpa_s->pending_bssid;
55 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
56 " did not succeed - continue trying to find "
57 "suitable AP", MAC2STR(bssid));
58 wpa_blacklist_add(wpa_s, bssid);
60 wpa_supplicant_deauthenticate(wpa_s,
61 WLAN_REASON_DEAUTH_LEAVING);
62 wpa_s->reassociate = 1;
63 wpa_supplicant_req_scan(wpa_s,
64 wpa_s->blacklist_cleared ? 5 : 0, 0);
65 wpa_s->blacklist_cleared = 0;
69 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
71 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
72 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
73 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
74 "try to associate with the received credential");
75 wpa_supplicant_deauthenticate(wpa_s,
76 WLAN_REASON_DEAUTH_LEAVING);
78 wpa_s->wps_freq = wpa_s->assoc_freq;
79 wpa_s->reassociate = 1;
80 wpa_supplicant_req_scan(wpa_s, 0, 0);
84 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
85 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
86 "for external credential processing");
87 wpas_clear_wps(wpa_s);
88 wpa_supplicant_deauthenticate(wpa_s,
89 WLAN_REASON_DEAUTH_LEAVING);
97 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
98 struct wpa_ssid *ssid,
99 const struct wps_credential *cred)
101 struct wpa_driver_capa capa;
104 struct wpa_ie_data adv;
105 int wpa2 = 0, ccmp = 0;
108 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
109 * case they are configured for mixed mode operation (WPA+WPA2 and
110 * TKIP+CCMP). Try to use scan results to figure out whether the AP
111 * actually supports stronger security and select that if the client
112 * has support for it, too.
115 if (wpa_drv_get_capa(wpa_s, &capa))
116 return; /* Unknown what driver supports */
118 bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
120 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
121 "table - use credential as-is");
125 wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
127 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
128 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
130 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
133 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
134 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
135 adv.pairwise_cipher & WPA_CIPHER_CCMP)
139 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
140 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
142 * TODO: This could be the initial AP configuration and the
143 * Beacon contents could change shortly. Should request a new
144 * scan and delay addition of the network until the updated
145 * scan results are available.
147 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
148 "support - use credential as-is");
152 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
153 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
154 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
155 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
156 "based on scan results");
157 if (wpa_s->conf->ap_scan == 1)
158 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
160 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
163 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
164 (ssid->proto & WPA_PROTO_WPA) &&
165 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
166 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
167 "based on scan results");
168 if (wpa_s->conf->ap_scan == 1)
169 ssid->proto |= WPA_PROTO_RSN;
171 ssid->proto = WPA_PROTO_RSN;
176 static int wpa_supplicant_wps_cred(void *ctx,
177 const struct wps_credential *cred)
179 struct wpa_supplicant *wpa_s = ctx;
180 struct wpa_ssid *ssid = wpa_s->current_ssid;
184 if ((wpa_s->conf->wps_cred_processing == 1 ||
185 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
186 size_t blen = cred->cred_attr_len * 2 + 1;
187 char *buf = os_malloc(blen);
189 wpa_snprintf_hex(buf, blen,
190 cred->cred_attr, cred->cred_attr_len);
191 wpa_msg(wpa_s, MSG_INFO, "%s%s",
192 WPS_EVENT_CRED_RECEIVED, buf);
196 wpas_notify_wps_credential(wpa_s, cred);
198 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
200 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
201 cred->cred_attr, cred->cred_attr_len);
203 if (wpa_s->conf->wps_cred_processing == 1)
206 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
207 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
209 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
210 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
211 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
212 cred->key, cred->key_len);
213 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
214 MAC2STR(cred->mac_addr));
216 auth_type = cred->auth_type;
217 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
218 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
219 "auth_type into WPA2PSK");
220 auth_type = WPS_AUTH_WPA2PSK;
223 if (auth_type != WPS_AUTH_OPEN &&
224 auth_type != WPS_AUTH_SHARED &&
225 auth_type != WPS_AUTH_WPAPSK &&
226 auth_type != WPS_AUTH_WPA2PSK) {
227 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
228 "unsupported authentication type 0x%x",
233 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
234 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
235 "on the received credential");
236 os_free(ssid->eap.identity);
237 ssid->eap.identity = NULL;
238 ssid->eap.identity_len = 0;
239 os_free(ssid->eap.phase1);
240 ssid->eap.phase1 = NULL;
241 os_free(ssid->eap.eap_methods);
242 ssid->eap.eap_methods = NULL;
243 if (!ssid->p2p_group)
246 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
247 "received credential");
248 ssid = wpa_config_add_network(wpa_s->conf);
251 wpas_notify_network_added(wpa_s, ssid);
254 wpa_config_set_network_defaults(ssid);
257 ssid->ssid = os_malloc(cred->ssid_len);
259 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
260 ssid->ssid_len = cred->ssid_len;
263 switch (cred->encr_type) {
267 if (cred->key_len <= 0)
269 if (cred->key_len != 5 && cred->key_len != 13 &&
270 cred->key_len != 10 && cred->key_len != 26) {
271 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
272 "%lu", (unsigned long) cred->key_len);
275 if (cred->key_idx > NUM_WEP_KEYS) {
276 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
281 key_idx = cred->key_idx - 1;
282 if (cred->key_len == 10 || cred->key_len == 26) {
283 if (hexstr2bin((char *) cred->key,
284 ssid->wep_key[key_idx],
285 cred->key_len / 2) < 0) {
286 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
290 ssid->wep_key_len[key_idx] = cred->key_len / 2;
292 os_memcpy(ssid->wep_key[key_idx], cred->key,
294 ssid->wep_key_len[key_idx] = cred->key_len;
296 ssid->wep_tx_keyidx = key_idx;
299 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
302 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
308 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
309 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
312 case WPS_AUTH_SHARED:
313 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
314 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
317 case WPS_AUTH_WPAPSK:
318 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
319 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
320 ssid->proto = WPA_PROTO_WPA;
323 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
324 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
325 ssid->proto = WPA_PROTO_WPA;
328 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
329 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
330 ssid->proto = WPA_PROTO_RSN;
332 case WPS_AUTH_WPA2PSK:
333 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
334 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
335 ssid->proto = WPA_PROTO_RSN;
339 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
340 if (cred->key_len == 2 * PMK_LEN) {
341 if (hexstr2bin((const char *) cred->key, ssid->psk,
343 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
348 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
349 os_free(ssid->passphrase);
350 ssid->passphrase = os_malloc(cred->key_len + 1);
351 if (ssid->passphrase == NULL)
353 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
354 ssid->passphrase[cred->key_len] = '\0';
355 wpa_config_update_psk(ssid);
357 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
359 (unsigned long) cred->key_len);
364 wpas_wps_security_workaround(wpa_s, ssid, cred);
366 #ifndef CONFIG_NO_CONFIG_WRITE
367 if (wpa_s->conf->update_config &&
368 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
369 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
372 #endif /* CONFIG_NO_CONFIG_WRITE */
378 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
379 struct wps_event_m2d *m2d)
381 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
382 "dev_password_id=%d config_error=%d",
383 m2d->dev_password_id, m2d->config_error);
384 wpas_notify_wps_event_m2d(wpa_s, m2d);
388 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
389 struct wps_event_fail *fail)
391 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
392 wpas_clear_wps(wpa_s);
393 wpas_notify_wps_event_fail(wpa_s, fail);
397 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
399 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
400 wpa_s->wps_success = 1;
401 wpas_notify_wps_event_success(wpa_s);
405 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
406 struct wps_event_er_ap *ap)
409 char dev_type[WPS_DEV_TYPE_BUFSIZE];
411 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
412 if (ap->pri_dev_type)
413 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
418 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
419 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
420 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
421 ap->friendly_name ? ap->friendly_name : "",
422 ap->manufacturer ? ap->manufacturer : "",
423 ap->model_description ? ap->model_description : "",
424 ap->model_name ? ap->model_name : "",
425 ap->manufacturer_url ? ap->manufacturer_url : "",
426 ap->model_url ? ap->model_url : "");
430 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
431 struct wps_event_er_ap *ap)
434 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
435 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
439 static void wpa_supplicant_wps_event_er_enrollee_add(
440 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
443 char dev_type[WPS_DEV_TYPE_BUFSIZE];
445 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
446 if (enrollee->pri_dev_type)
447 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
452 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
453 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
455 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
456 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
457 enrollee->dev_name ? enrollee->dev_name : "",
458 enrollee->manufacturer ? enrollee->manufacturer : "",
459 enrollee->model_name ? enrollee->model_name : "",
460 enrollee->model_number ? enrollee->model_number : "",
461 enrollee->serial_number ? enrollee->serial_number : "");
465 static void wpa_supplicant_wps_event_er_enrollee_remove(
466 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
469 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
470 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
471 uuid_str, MAC2STR(enrollee->mac_addr));
475 static void wpa_supplicant_wps_event_er_ap_settings(
476 struct wpa_supplicant *wpa_s,
477 struct wps_event_er_ap_settings *ap_settings)
481 const struct wps_credential *cred = ap_settings->cred;
484 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
485 if (cred->key_len >= 8 && cred->key_len <= 64) {
486 os_memcpy(key_str, cred->key, cred->key_len);
487 key_str[cred->key_len] = '\0';
491 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
492 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
493 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
494 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
496 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
497 cred->auth_type, cred->encr_type, key_str);
501 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
502 union wps_event_data *data)
504 struct wpa_supplicant *wpa_s = ctx;
507 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
510 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
513 wpa_supplicant_wps_event_success(wpa_s);
515 case WPS_EV_PWD_AUTH_FAIL:
517 case WPS_EV_PBC_OVERLAP:
519 case WPS_EV_PBC_TIMEOUT:
521 case WPS_EV_ER_AP_ADD:
522 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
524 case WPS_EV_ER_AP_REMOVE:
525 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
527 case WPS_EV_ER_ENROLLEE_ADD:
528 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
531 case WPS_EV_ER_ENROLLEE_REMOVE:
532 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
535 case WPS_EV_ER_AP_SETTINGS:
536 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
543 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
545 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
546 eap_is_wps_pin_enrollee(&ssid->eap))
547 return WPS_REQ_ENROLLEE;
549 return WPS_REQ_REGISTRAR;
553 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
556 struct wpa_ssid *ssid, *remove_ssid = NULL;
558 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
560 /* Remove any existing WPS network from configuration */
561 ssid = wpa_s->conf->ssid;
563 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
564 if (ssid == wpa_s->current_ssid) {
565 wpa_s->current_ssid = NULL;
567 wpas_notify_network_changed(wpa_s);
575 wpas_notify_network_removed(wpa_s, remove_ssid);
576 wpa_config_remove_network(wpa_s->conf, id);
582 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
584 struct wpa_supplicant *wpa_s = eloop_ctx;
585 wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
587 wpas_clear_wps(wpa_s);
591 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
592 int registrar, const u8 *bssid)
594 struct wpa_ssid *ssid;
596 ssid = wpa_config_add_network(wpa_s->conf);
599 wpas_notify_network_added(wpa_s, ssid);
600 wpa_config_set_network_defaults(ssid);
602 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
603 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
604 wpa_config_set(ssid, "identity", registrar ?
605 "\"" WSC_ID_REGISTRAR "\"" :
606 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
607 wpas_notify_network_removed(wpa_s, ssid);
608 wpa_config_remove_network(wpa_s->conf, ssid->id);
616 #endif /* CONFIG_P2P */
618 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
622 * Note: With P2P, the SSID may change at the time the WPS
623 * provisioning is started, so better not filter the AP based
624 * on the current SSID in the scan results.
627 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
628 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
632 ssid->ssid = os_malloc(bss->ssid_len);
633 if (ssid->ssid == NULL)
635 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
636 ssid->ssid_len = bss->ssid_len;
637 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
639 ssid->ssid, ssid->ssid_len);
644 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
645 "for the AP; use wildcard");
650 #endif /* CONFIG_P2P */
657 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
658 struct wpa_ssid *selected)
660 struct wpa_ssid *ssid;
662 /* Mark all other networks disabled and trigger reassociation */
663 ssid = wpa_s->conf->ssid;
665 int was_disabled = ssid->disabled;
666 ssid->disabled = ssid != selected;
667 if (was_disabled != ssid->disabled)
668 wpas_notify_network_enabled_changed(wpa_s, ssid);
671 wpa_s->disconnected = 0;
672 wpa_s->reassociate = 1;
673 wpa_s->scan_runs = 0;
674 wpa_s->wps_success = 0;
675 wpa_s->blacklist_cleared = 0;
676 wpa_supplicant_req_scan(wpa_s, 0, 0);
680 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
683 struct wpa_ssid *ssid;
684 wpas_clear_wps(wpa_s);
685 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
689 ssid->p2p_group = p2p_group;
690 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
691 if (wpa_s->wps_fragment_size)
692 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
693 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
695 wpas_wps_reassoc(wpa_s, ssid);
700 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
701 const char *pin, int p2p_group)
703 struct wpa_ssid *ssid;
705 unsigned int rpin = 0;
707 wpas_clear_wps(wpa_s);
708 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
712 ssid->p2p_group = p2p_group;
714 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
716 rpin = wps_generate_pin();
717 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
719 wpa_config_set(ssid, "phase1", val, 0);
720 if (wpa_s->wps_fragment_size)
721 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
722 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
724 wpas_wps_reassoc(wpa_s, ssid);
729 #ifdef CONFIG_WPS_OOB
730 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
731 char *path, char *method, char *name)
733 struct wps_context *wps = wpa_s->wps;
734 struct oob_device_data *oob_dev;
736 oob_dev = wps_get_oob_device(device_type);
739 oob_dev->device_path = path;
740 oob_dev->device_name = name;
741 wps->oob_conf.oob_method = wps_get_oob_method(method);
743 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
745 * Use pre-configured DH keys in order to be able to write the
746 * key hash into the OOB file.
748 wpabuf_free(wps->dh_pubkey);
749 wpabuf_free(wps->dh_privkey);
750 wps->dh_privkey = NULL;
751 wps->dh_pubkey = NULL;
752 dh5_free(wps->dh_ctx);
753 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
754 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
755 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
756 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
757 "Diffie-Hellman handshake");
762 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
763 wpas_clear_wps(wpa_s);
765 if (wps_process_oob(wps, oob_dev, 0) < 0)
768 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
769 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
770 wpas_wps_start_pin(wpa_s, NULL,
771 wpabuf_head(wps->oob_conf.dev_password), 0) < 0)
776 #endif /* CONFIG_WPS_OOB */
779 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
780 const char *pin, struct wps_new_ap_settings *settings)
782 struct wpa_ssid *ssid;
789 wpas_clear_wps(wpa_s);
790 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
795 end = pos + sizeof(val);
796 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
797 if (res < 0 || res >= end - pos)
801 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
802 "new_encr=%s new_key=%s",
803 settings->ssid_hex, settings->auth,
804 settings->encr, settings->key_hex);
805 if (res < 0 || res >= end - pos)
809 res = os_snprintf(pos, end - pos, "\"");
810 if (res < 0 || res >= end - pos)
812 wpa_config_set(ssid, "phase1", val, 0);
813 if (wpa_s->wps_fragment_size)
814 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
815 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
817 wpas_wps_reassoc(wpa_s, ssid);
822 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
825 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
826 "STA " MACSTR, MAC2STR(mac_addr));
827 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
835 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
836 const struct wps_device_data *dev)
838 char uuid[40], txt[400];
840 char devtype[WPS_DEV_TYPE_BUFSIZE];
841 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
843 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
844 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
845 " [%s|%s|%s|%s|%s|%s]",
846 uuid, MAC2STR(dev->mac_addr), dev->device_name,
847 dev->manufacturer, dev->model_name,
848 dev->model_number, dev->serial_number,
849 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
851 if (len > 0 && len < (int) sizeof(txt))
852 wpa_printf(MSG_INFO, "%s", txt);
856 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
857 u16 sel_reg_config_methods)
860 struct wpa_supplicant *wpa_s = ctx;
862 if (wpa_s->wps_er == NULL)
864 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
865 "dev_password_id=%u sel_reg_config_methods=0x%x",
866 sel_reg, dev_passwd_id, sel_reg_config_methods);
867 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
868 sel_reg_config_methods);
869 #endif /* CONFIG_WPS_ER */
873 int wpas_wps_init(struct wpa_supplicant *wpa_s)
875 struct wps_context *wps;
876 struct wps_registrar_config rcfg;
878 wps = os_zalloc(sizeof(*wps));
882 wps->cred_cb = wpa_supplicant_wps_cred;
883 wps->event_cb = wpa_supplicant_wps_event;
886 wps->dev.device_name = wpa_s->conf->device_name;
887 wps->dev.manufacturer = wpa_s->conf->manufacturer;
888 wps->dev.model_name = wpa_s->conf->model_name;
889 wps->dev.model_number = wpa_s->conf->model_number;
890 wps->dev.serial_number = wpa_s->conf->serial_number;
891 wps->config_methods =
892 wps_config_methods_str2bin(wpa_s->conf->config_methods);
893 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
894 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
895 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
896 "methods are not allowed at the same time");
900 if (wpa_s->conf->device_type &&
901 wps_dev_type_str2bin(wpa_s->conf->device_type,
902 wps->dev.pri_dev_type) < 0) {
903 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
907 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
908 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
909 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
910 if (is_nil_uuid(wpa_s->conf->uuid)) {
911 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
912 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
913 wps->uuid, WPS_UUID_LEN);
915 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
917 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
918 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
920 os_memset(&rcfg, 0, sizeof(rcfg));
921 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
922 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
923 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
926 wps->registrar = wps_registrar_init(wps, &rcfg);
927 if (wps->registrar == NULL) {
928 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
939 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
941 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
943 if (wpa_s->wps == NULL)
947 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
948 wpa_s->wps_er = NULL;
949 #endif /* CONFIG_WPS_ER */
951 wps_registrar_deinit(wpa_s->wps->registrar);
952 wpabuf_free(wpa_s->wps->dh_pubkey);
953 wpabuf_free(wpa_s->wps->dh_privkey);
954 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
955 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
956 os_free(wpa_s->wps->network_key);
962 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
963 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
965 struct wpabuf *wps_ie;
967 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
970 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
971 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
973 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
977 if (!wps_is_selected_pbc_registrar(wps_ie)) {
978 wpa_printf(MSG_DEBUG, " skip - WPS AP "
979 "without active PBC Registrar");
984 /* TODO: overlap detection */
985 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
991 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
993 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
998 * Start with WPS APs that advertise our address as an
999 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1000 * allow any WPS AP after couple of scans since some APs do not
1001 * set Selected Registrar attribute properly when using
1002 * external Registrar.
1004 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1005 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1006 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1007 "without active PIN Registrar");
1008 wpabuf_free(wps_ie);
1011 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1013 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1014 "(Authorized MAC or Active PIN)");
1016 wpabuf_free(wps_ie);
1021 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1022 wpabuf_free(wps_ie);
1030 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1031 struct wpa_ssid *ssid,
1032 struct wpa_scan_res *bss)
1034 struct wpabuf *wps_ie = NULL;
1037 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1038 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1039 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1040 /* allow wildcard SSID for WPS PBC */
1043 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1044 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1046 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1047 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1048 /* allow wildcard SSID for WPS PIN */
1053 if (!ret && ssid->bssid_set &&
1054 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1055 /* allow wildcard SSID due to hardcoded BSSID match */
1059 #ifdef CONFIG_WPS_STRICT
1061 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1064 if (bss->beacon_ie_len) {
1065 struct wpabuf *bcn_wps;
1066 bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1067 bss, WPS_IE_VENDOR_TYPE);
1068 if (bcn_wps == NULL) {
1069 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1070 "missing from AP Beacon");
1073 if (wps_validate_beacon(wps_ie) < 0)
1075 wpabuf_free(bcn_wps);
1079 #endif /* CONFIG_WPS_STRICT */
1081 wpabuf_free(wps_ie);
1087 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1088 struct wpa_bss *selected, struct wpa_ssid *ssid)
1090 const u8 *sel_uuid, *uuid;
1091 struct wpabuf *wps_ie;
1093 struct wpa_bss *bss;
1095 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1098 /* Make sure that only one AP is in active PBC mode */
1099 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1101 sel_uuid = wps_get_uuid_e(wps_ie);
1105 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1107 if (bss == selected)
1109 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1112 if (!wps_is_selected_pbc_registrar(ie)) {
1116 uuid = wps_get_uuid_e(ie);
1117 if (sel_uuid == NULL || uuid == NULL ||
1118 os_memcmp(sel_uuid, uuid, 16) != 0) {
1119 ret = 1; /* PBC overlap */
1124 /* TODO: verify that this is reasonable dual-band situation */
1129 wpabuf_free(wps_ie);
1135 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1137 struct wpa_bss *bss;
1139 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1142 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1144 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1147 if (wps_is_selected_pbc_registrar(ie))
1148 wpa_msg_ctrl(wpa_s, MSG_INFO,
1149 WPS_EVENT_AP_AVAILABLE_PBC);
1150 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1151 wpa_msg_ctrl(wpa_s, MSG_INFO,
1152 WPS_EVENT_AP_AVAILABLE_AUTH);
1153 else if (wps_is_selected_pin_registrar(ie))
1154 wpa_msg_ctrl(wpa_s, MSG_INFO,
1155 WPS_EVENT_AP_AVAILABLE_PIN);
1157 wpa_msg_ctrl(wpa_s, MSG_INFO,
1158 WPS_EVENT_AP_AVAILABLE);
1165 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1167 struct wpa_ssid *ssid;
1169 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1170 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1178 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1181 struct wpabuf *wps_ie;
1184 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1188 ret = wps_attr_text(wps_ie, buf, end);
1189 wpabuf_free(wps_ie);
1194 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1196 #ifdef CONFIG_WPS_ER
1197 if (wpa_s->wps_er) {
1198 wps_er_refresh(wpa_s->wps_er);
1201 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1202 if (wpa_s->wps_er == NULL)
1205 #else /* CONFIG_WPS_ER */
1207 #endif /* CONFIG_WPS_ER */
1211 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1213 #ifdef CONFIG_WPS_ER
1214 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1215 wpa_s->wps_er = NULL;
1216 #endif /* CONFIG_WPS_ER */
1221 #ifdef CONFIG_WPS_ER
1222 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1223 const char *uuid, const char *pin)
1228 if (os_strcmp(uuid, "any") == 0)
1230 else if (uuid_str2bin(uuid, u))
1232 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1234 (const u8 *) pin, os_strlen(pin), 300);
1238 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1242 if (uuid_str2bin(uuid, u))
1244 return wps_er_pbc(wpa_s->wps_er, u);
1248 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1253 if (uuid_str2bin(uuid, u))
1255 return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1260 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1261 const char *pin, struct wps_new_ap_settings *settings)
1264 struct wps_credential cred;
1267 if (uuid_str2bin(uuid, u))
1269 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1270 settings->encr == NULL || settings->key_hex == NULL)
1273 os_memset(&cred, 0, sizeof(cred));
1274 len = os_strlen(settings->ssid_hex);
1275 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1276 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1278 cred.ssid_len = len / 2;
1280 len = os_strlen(settings->key_hex);
1281 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1282 hexstr2bin(settings->key_hex, cred.key, len / 2))
1284 cred.key_len = len / 2;
1286 if (os_strcmp(settings->auth, "OPEN") == 0)
1287 cred.auth_type = WPS_AUTH_OPEN;
1288 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1289 cred.auth_type = WPS_AUTH_WPAPSK;
1290 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1291 cred.auth_type = WPS_AUTH_WPA2PSK;
1295 if (os_strcmp(settings->encr, "NONE") == 0)
1296 cred.encr_type = WPS_ENCR_NONE;
1297 else if (os_strcmp(settings->encr, "WEP") == 0)
1298 cred.encr_type = WPS_ENCR_WEP;
1299 else if (os_strcmp(settings->encr, "TKIP") == 0)
1300 cred.encr_type = WPS_ENCR_TKIP;
1301 else if (os_strcmp(settings->encr, "CCMP") == 0)
1302 cred.encr_type = WPS_ENCR_AES;
1306 return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1307 os_strlen(pin), &cred);
1311 static void wpas_wps_terminate_cb(void *ctx)
1313 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1316 #endif /* CONFIG_WPS_ER */
1319 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1321 #ifdef CONFIG_WPS_ER
1322 if (wpa_s->wps_er) {
1323 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1324 wpa_s->wps_er = NULL;
1327 #endif /* CONFIG_WPS_ER */
1332 int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1334 struct wpa_ssid *ssid;
1336 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1337 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1345 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1347 struct wps_context *wps = wpa_s->wps;
1352 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1353 wps->config_methods = wps_config_methods_str2bin(
1354 wpa_s->conf->config_methods);
1355 if ((wps->config_methods &
1356 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1357 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1358 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1359 "config methods are not allowed at the "
1361 wps->config_methods &= ~WPS_CONFIG_LABEL;
1365 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
1366 if (wpa_s->conf->device_type &&
1367 wps_dev_type_str2bin(wpa_s->conf->device_type,
1368 wps->dev.pri_dev_type) < 0)
1369 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
1372 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1373 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1375 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID) {
1376 if (is_nil_uuid(wpa_s->conf->uuid)) {
1377 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1378 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1379 "address", wps->uuid, WPS_UUID_LEN);
1381 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);