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;
244 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
245 "received credential");
246 ssid = wpa_config_add_network(wpa_s->conf);
249 wpas_notify_network_added(wpa_s, ssid);
252 wpa_config_set_network_defaults(ssid);
255 ssid->ssid = os_malloc(cred->ssid_len);
257 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
258 ssid->ssid_len = cred->ssid_len;
261 switch (cred->encr_type) {
265 if (cred->key_len <= 0)
267 if (cred->key_len != 5 && cred->key_len != 13 &&
268 cred->key_len != 10 && cred->key_len != 26) {
269 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
270 "%lu", (unsigned long) cred->key_len);
273 if (cred->key_idx > NUM_WEP_KEYS) {
274 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
279 key_idx = cred->key_idx - 1;
280 if (cred->key_len == 10 || cred->key_len == 26) {
281 if (hexstr2bin((char *) cred->key,
282 ssid->wep_key[key_idx],
283 cred->key_len / 2) < 0) {
284 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
288 ssid->wep_key_len[key_idx] = cred->key_len / 2;
290 os_memcpy(ssid->wep_key[key_idx], cred->key,
292 ssid->wep_key_len[key_idx] = cred->key_len;
294 ssid->wep_tx_keyidx = key_idx;
297 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
300 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
306 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
307 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
310 case WPS_AUTH_SHARED:
311 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
312 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
315 case WPS_AUTH_WPAPSK:
316 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
317 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
318 ssid->proto = WPA_PROTO_WPA;
321 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
322 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
323 ssid->proto = WPA_PROTO_WPA;
326 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
327 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
328 ssid->proto = WPA_PROTO_RSN;
330 case WPS_AUTH_WPA2PSK:
331 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
332 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
333 ssid->proto = WPA_PROTO_RSN;
337 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
338 if (cred->key_len == 2 * PMK_LEN) {
339 if (hexstr2bin((const char *) cred->key, ssid->psk,
341 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
346 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
347 os_free(ssid->passphrase);
348 ssid->passphrase = os_malloc(cred->key_len + 1);
349 if (ssid->passphrase == NULL)
351 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
352 ssid->passphrase[cred->key_len] = '\0';
353 wpa_config_update_psk(ssid);
355 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
357 (unsigned long) cred->key_len);
362 wpas_wps_security_workaround(wpa_s, ssid, cred);
364 #ifndef CONFIG_NO_CONFIG_WRITE
365 if (wpa_s->conf->update_config &&
366 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
367 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
370 #endif /* CONFIG_NO_CONFIG_WRITE */
376 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
377 struct wps_event_m2d *m2d)
379 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
380 "dev_password_id=%d config_error=%d",
381 m2d->dev_password_id, m2d->config_error);
382 wpas_notify_wps_event_m2d(wpa_s, m2d);
386 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
387 struct wps_event_fail *fail)
389 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
390 wpas_clear_wps(wpa_s);
391 wpas_notify_wps_event_fail(wpa_s, fail);
395 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
397 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
398 wpa_s->wps_success = 1;
399 wpas_notify_wps_event_success(wpa_s);
403 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
404 struct wps_event_er_ap *ap)
407 char dev_type[WPS_DEV_TYPE_BUFSIZE];
409 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
410 if (ap->pri_dev_type)
411 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
416 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
417 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
418 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
419 ap->friendly_name ? ap->friendly_name : "",
420 ap->manufacturer ? ap->manufacturer : "",
421 ap->model_description ? ap->model_description : "",
422 ap->model_name ? ap->model_name : "",
423 ap->manufacturer_url ? ap->manufacturer_url : "",
424 ap->model_url ? ap->model_url : "");
428 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
429 struct wps_event_er_ap *ap)
432 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
433 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
437 static void wpa_supplicant_wps_event_er_enrollee_add(
438 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
441 char dev_type[WPS_DEV_TYPE_BUFSIZE];
443 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
444 if (enrollee->pri_dev_type)
445 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
450 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
451 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
453 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
454 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
455 enrollee->dev_name ? enrollee->dev_name : "",
456 enrollee->manufacturer ? enrollee->manufacturer : "",
457 enrollee->model_name ? enrollee->model_name : "",
458 enrollee->model_number ? enrollee->model_number : "",
459 enrollee->serial_number ? enrollee->serial_number : "");
463 static void wpa_supplicant_wps_event_er_enrollee_remove(
464 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
467 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
468 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
469 uuid_str, MAC2STR(enrollee->mac_addr));
473 static void wpa_supplicant_wps_event_er_ap_settings(
474 struct wpa_supplicant *wpa_s,
475 struct wps_event_er_ap_settings *ap_settings)
479 const struct wps_credential *cred = ap_settings->cred;
482 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
483 if (cred->key_len >= 8 && cred->key_len <= 64) {
484 os_memcpy(key_str, cred->key, cred->key_len);
485 key_str[cred->key_len] = '\0';
489 uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
490 /* Use wpa_msg_ctrl to avoid showing the key in debug log */
491 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
492 "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
494 uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
495 cred->auth_type, cred->encr_type, key_str);
499 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
500 union wps_event_data *data)
502 struct wpa_supplicant *wpa_s = ctx;
505 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
508 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
511 wpa_supplicant_wps_event_success(wpa_s);
513 case WPS_EV_PWD_AUTH_FAIL:
515 case WPS_EV_PBC_OVERLAP:
517 case WPS_EV_PBC_TIMEOUT:
519 case WPS_EV_ER_AP_ADD:
520 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
522 case WPS_EV_ER_AP_REMOVE:
523 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
525 case WPS_EV_ER_ENROLLEE_ADD:
526 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
529 case WPS_EV_ER_ENROLLEE_REMOVE:
530 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
533 case WPS_EV_ER_AP_SETTINGS:
534 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
541 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
543 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
544 eap_is_wps_pin_enrollee(&ssid->eap))
545 return WPS_REQ_ENROLLEE;
547 return WPS_REQ_REGISTRAR;
551 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
554 struct wpa_ssid *ssid, *remove_ssid = NULL;
556 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
558 /* Remove any existing WPS network from configuration */
559 ssid = wpa_s->conf->ssid;
561 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
562 if (ssid == wpa_s->current_ssid) {
563 wpa_s->current_ssid = NULL;
565 wpas_notify_network_changed(wpa_s);
573 wpas_notify_network_removed(wpa_s, remove_ssid);
574 wpa_config_remove_network(wpa_s->conf, id);
580 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
582 struct wpa_supplicant *wpa_s = eloop_ctx;
583 wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
585 wpas_clear_wps(wpa_s);
589 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
590 int registrar, const u8 *bssid)
592 struct wpa_ssid *ssid;
594 ssid = wpa_config_add_network(wpa_s->conf);
597 wpas_notify_network_added(wpa_s, ssid);
598 wpa_config_set_network_defaults(ssid);
599 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
600 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
601 wpa_config_set(ssid, "identity", registrar ?
602 "\"" WSC_ID_REGISTRAR "\"" :
603 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
604 wpas_notify_network_removed(wpa_s, ssid);
605 wpa_config_remove_network(wpa_s->conf, ssid->id);
613 #endif /* CONFIG_P2P */
615 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
619 * Note: With P2P, the SSID may change at the time the WPS
620 * provisioning is started, so better not filter the AP based
621 * on the current SSID in the scan results.
624 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
625 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
629 ssid->ssid = os_malloc(bss->ssid_len);
630 if (ssid->ssid == NULL)
632 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
633 ssid->ssid_len = bss->ssid_len;
634 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
636 ssid->ssid, ssid->ssid_len);
641 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
642 "for the AP; use wildcard");
647 #endif /* CONFIG_P2P */
654 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
655 struct wpa_ssid *selected)
657 struct wpa_ssid *ssid;
659 /* Mark all other networks disabled and trigger reassociation */
660 ssid = wpa_s->conf->ssid;
662 int was_disabled = ssid->disabled;
663 ssid->disabled = ssid != selected;
664 if (was_disabled != ssid->disabled)
665 wpas_notify_network_enabled_changed(wpa_s, ssid);
668 wpa_s->disconnected = 0;
669 wpa_s->reassociate = 1;
670 wpa_s->scan_runs = 0;
671 wpa_s->wps_success = 0;
672 wpa_s->blacklist_cleared = 0;
673 wpa_supplicant_req_scan(wpa_s, 0, 0);
677 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
680 struct wpa_ssid *ssid;
681 wpas_clear_wps(wpa_s);
682 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
686 ssid->p2p_group = p2p_group;
687 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
688 if (wpa_s->wps_fragment_size)
689 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
690 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
692 wpas_wps_reassoc(wpa_s, ssid);
697 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
698 const char *pin, int p2p_group)
700 struct wpa_ssid *ssid;
702 unsigned int rpin = 0;
704 wpas_clear_wps(wpa_s);
705 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
709 ssid->p2p_group = p2p_group;
711 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
713 rpin = wps_generate_pin();
714 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
716 wpa_config_set(ssid, "phase1", val, 0);
717 if (wpa_s->wps_fragment_size)
718 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
719 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
721 wpas_wps_reassoc(wpa_s, ssid);
726 #ifdef CONFIG_WPS_OOB
727 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
728 char *path, char *method, char *name)
730 struct wps_context *wps = wpa_s->wps;
731 struct oob_device_data *oob_dev;
733 oob_dev = wps_get_oob_device(device_type);
736 oob_dev->device_path = path;
737 oob_dev->device_name = name;
738 wps->oob_conf.oob_method = wps_get_oob_method(method);
740 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
742 * Use pre-configured DH keys in order to be able to write the
743 * key hash into the OOB file.
745 wpabuf_free(wps->dh_pubkey);
746 wpabuf_free(wps->dh_privkey);
747 wps->dh_privkey = NULL;
748 wps->dh_pubkey = NULL;
749 dh5_free(wps->dh_ctx);
750 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
751 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
752 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
753 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
754 "Diffie-Hellman handshake");
759 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
760 wpas_clear_wps(wpa_s);
762 if (wps_process_oob(wps, oob_dev, 0) < 0)
765 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
766 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
767 wpas_wps_start_pin(wpa_s, NULL,
768 wpabuf_head(wps->oob_conf.dev_password), 0) < 0)
773 #endif /* CONFIG_WPS_OOB */
776 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
777 const char *pin, struct wps_new_ap_settings *settings)
779 struct wpa_ssid *ssid;
786 wpas_clear_wps(wpa_s);
787 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
791 end = pos + sizeof(val);
792 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
793 if (res < 0 || res >= end - pos)
797 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
798 "new_encr=%s new_key=%s",
799 settings->ssid_hex, settings->auth,
800 settings->encr, settings->key_hex);
801 if (res < 0 || res >= end - pos)
805 res = os_snprintf(pos, end - pos, "\"");
806 if (res < 0 || res >= end - pos)
808 wpa_config_set(ssid, "phase1", val, 0);
809 if (wpa_s->wps_fragment_size)
810 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
811 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
813 wpas_wps_reassoc(wpa_s, ssid);
818 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
821 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
822 "STA " MACSTR, MAC2STR(mac_addr));
823 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
831 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
832 const struct wps_device_data *dev)
834 char uuid[40], txt[400];
836 char devtype[WPS_DEV_TYPE_BUFSIZE];
837 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
839 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
840 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
841 " [%s|%s|%s|%s|%s|%s]",
842 uuid, MAC2STR(dev->mac_addr), dev->device_name,
843 dev->manufacturer, dev->model_name,
844 dev->model_number, dev->serial_number,
845 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
847 if (len > 0 && len < (int) sizeof(txt))
848 wpa_printf(MSG_INFO, "%s", txt);
852 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
853 u16 sel_reg_config_methods)
856 struct wpa_supplicant *wpa_s = ctx;
858 if (wpa_s->wps_er == NULL)
860 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
861 "dev_password_id=%u sel_reg_config_methods=0x%x",
862 sel_reg, dev_passwd_id, sel_reg_config_methods);
863 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
864 sel_reg_config_methods);
865 #endif /* CONFIG_WPS_ER */
869 int wpas_wps_init(struct wpa_supplicant *wpa_s)
871 struct wps_context *wps;
872 struct wps_registrar_config rcfg;
874 wps = os_zalloc(sizeof(*wps));
878 wps->cred_cb = wpa_supplicant_wps_cred;
879 wps->event_cb = wpa_supplicant_wps_event;
882 wps->dev.device_name = wpa_s->conf->device_name;
883 wps->dev.manufacturer = wpa_s->conf->manufacturer;
884 wps->dev.model_name = wpa_s->conf->model_name;
885 wps->dev.model_number = wpa_s->conf->model_number;
886 wps->dev.serial_number = wpa_s->conf->serial_number;
887 wps->config_methods =
888 wps_config_methods_str2bin(wpa_s->conf->config_methods);
889 if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
890 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
891 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
892 "methods are not allowed at the same time");
896 if (wpa_s->conf->device_type &&
897 wps_dev_type_str2bin(wpa_s->conf->device_type,
898 wps->dev.pri_dev_type) < 0) {
899 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
903 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
904 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
905 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
906 if (is_nil_uuid(wpa_s->conf->uuid)) {
907 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
908 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
909 wps->uuid, WPS_UUID_LEN);
911 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
913 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
914 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
916 os_memset(&rcfg, 0, sizeof(rcfg));
917 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
918 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
919 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
922 wps->registrar = wps_registrar_init(wps, &rcfg);
923 if (wps->registrar == NULL) {
924 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
935 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
937 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
939 if (wpa_s->wps == NULL)
943 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
944 wpa_s->wps_er = NULL;
945 #endif /* CONFIG_WPS_ER */
947 wps_registrar_deinit(wpa_s->wps->registrar);
948 wpabuf_free(wpa_s->wps->dh_pubkey);
949 wpabuf_free(wpa_s->wps->dh_privkey);
950 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
951 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
952 os_free(wpa_s->wps->network_key);
958 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
959 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
961 struct wpabuf *wps_ie;
963 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
966 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
967 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
969 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
973 if (!wps_is_selected_pbc_registrar(wps_ie)) {
974 wpa_printf(MSG_DEBUG, " skip - WPS AP "
975 "without active PBC Registrar");
980 /* TODO: overlap detection */
981 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
987 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
989 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
994 * Start with WPS APs that advertise our address as an
995 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
996 * allow any WPS AP after couple of scans since some APs do not
997 * set Selected Registrar attribute properly when using
998 * external Registrar.
1000 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1001 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1002 wpa_printf(MSG_DEBUG, " skip - WPS AP "
1003 "without active PIN Registrar");
1004 wpabuf_free(wps_ie);
1007 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1009 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
1010 "(Authorized MAC or Active PIN)");
1012 wpabuf_free(wps_ie);
1017 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
1018 wpabuf_free(wps_ie);
1026 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1027 struct wpa_ssid *ssid,
1028 struct wpa_scan_res *bss)
1030 struct wpabuf *wps_ie = NULL;
1033 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1034 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1035 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1036 /* allow wildcard SSID for WPS PBC */
1039 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1040 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1042 (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1043 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1044 /* allow wildcard SSID for WPS PIN */
1049 if (!ret && ssid->bssid_set &&
1050 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1051 /* allow wildcard SSID due to hardcoded BSSID match */
1055 #ifdef CONFIG_WPS_STRICT
1057 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1060 if (bss->beacon_ie_len) {
1061 struct wpabuf *bcn_wps;
1062 bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1063 bss, WPS_IE_VENDOR_TYPE);
1064 if (bcn_wps == NULL) {
1065 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1066 "missing from AP Beacon");
1069 if (wps_validate_beacon(wps_ie) < 0)
1071 wpabuf_free(bcn_wps);
1075 #endif /* CONFIG_WPS_STRICT */
1077 wpabuf_free(wps_ie);
1083 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1084 struct wpa_bss *selected, struct wpa_ssid *ssid)
1086 const u8 *sel_uuid, *uuid;
1087 struct wpabuf *wps_ie;
1089 struct wpa_bss *bss;
1091 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1094 /* Make sure that only one AP is in active PBC mode */
1095 wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1097 sel_uuid = wps_get_uuid_e(wps_ie);
1101 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1103 if (bss == selected)
1105 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1108 if (!wps_is_selected_pbc_registrar(ie)) {
1112 uuid = wps_get_uuid_e(ie);
1113 if (sel_uuid == NULL || uuid == NULL ||
1114 os_memcmp(sel_uuid, uuid, 16) != 0) {
1115 ret = 1; /* PBC overlap */
1120 /* TODO: verify that this is reasonable dual-band situation */
1125 wpabuf_free(wps_ie);
1131 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1133 struct wpa_bss *bss;
1135 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1138 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1140 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1143 if (wps_is_selected_pbc_registrar(ie))
1144 wpa_msg_ctrl(wpa_s, MSG_INFO,
1145 WPS_EVENT_AP_AVAILABLE_PBC);
1146 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1147 wpa_msg_ctrl(wpa_s, MSG_INFO,
1148 WPS_EVENT_AP_AVAILABLE_AUTH);
1149 else if (wps_is_selected_pin_registrar(ie))
1150 wpa_msg_ctrl(wpa_s, MSG_INFO,
1151 WPS_EVENT_AP_AVAILABLE_PIN);
1153 wpa_msg_ctrl(wpa_s, MSG_INFO,
1154 WPS_EVENT_AP_AVAILABLE);
1161 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1163 struct wpa_ssid *ssid;
1165 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1166 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1174 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1177 struct wpabuf *wps_ie;
1180 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1184 ret = wps_attr_text(wps_ie, buf, end);
1185 wpabuf_free(wps_ie);
1190 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1192 #ifdef CONFIG_WPS_ER
1193 if (wpa_s->wps_er) {
1194 wps_er_refresh(wpa_s->wps_er);
1197 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1198 if (wpa_s->wps_er == NULL)
1201 #else /* CONFIG_WPS_ER */
1203 #endif /* CONFIG_WPS_ER */
1207 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1209 #ifdef CONFIG_WPS_ER
1210 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1211 wpa_s->wps_er = NULL;
1212 #endif /* CONFIG_WPS_ER */
1217 #ifdef CONFIG_WPS_ER
1218 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1219 const char *uuid, const char *pin)
1224 if (os_strcmp(uuid, "any") == 0)
1226 else if (uuid_str2bin(uuid, u))
1228 return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1230 (const u8 *) pin, os_strlen(pin), 300);
1234 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1238 if (uuid_str2bin(uuid, u))
1240 return wps_er_pbc(wpa_s->wps_er, u);
1244 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1249 if (uuid_str2bin(uuid, u))
1251 return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1256 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1257 const char *pin, struct wps_new_ap_settings *settings)
1260 struct wps_credential cred;
1263 if (uuid_str2bin(uuid, u))
1265 if (settings->ssid_hex == NULL || settings->auth == NULL ||
1266 settings->encr == NULL || settings->key_hex == NULL)
1269 os_memset(&cred, 0, sizeof(cred));
1270 len = os_strlen(settings->ssid_hex);
1271 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1272 hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1274 cred.ssid_len = len / 2;
1276 len = os_strlen(settings->key_hex);
1277 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1278 hexstr2bin(settings->key_hex, cred.key, len / 2))
1280 cred.key_len = len / 2;
1282 if (os_strcmp(settings->auth, "OPEN") == 0)
1283 cred.auth_type = WPS_AUTH_OPEN;
1284 else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1285 cred.auth_type = WPS_AUTH_WPAPSK;
1286 else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1287 cred.auth_type = WPS_AUTH_WPA2PSK;
1291 if (os_strcmp(settings->encr, "NONE") == 0)
1292 cred.encr_type = WPS_ENCR_NONE;
1293 else if (os_strcmp(settings->encr, "WEP") == 0)
1294 cred.encr_type = WPS_ENCR_WEP;
1295 else if (os_strcmp(settings->encr, "TKIP") == 0)
1296 cred.encr_type = WPS_ENCR_TKIP;
1297 else if (os_strcmp(settings->encr, "CCMP") == 0)
1298 cred.encr_type = WPS_ENCR_AES;
1302 return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1303 os_strlen(pin), &cred);
1307 static void wpas_wps_terminate_cb(void *ctx)
1309 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1312 #endif /* CONFIG_WPS_ER */
1315 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1317 #ifdef CONFIG_WPS_ER
1318 if (wpa_s->wps_er) {
1319 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1320 wpa_s->wps_er = NULL;
1323 #endif /* CONFIG_WPS_ER */
1328 int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1330 struct wpa_ssid *ssid;
1332 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1333 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1341 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1343 struct wps_context *wps = wpa_s->wps;
1348 if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1349 wps->config_methods = wps_config_methods_str2bin(
1350 wpa_s->conf->config_methods);
1351 if ((wps->config_methods &
1352 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1353 (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1354 wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1355 "config methods are not allowed at the "
1357 wps->config_methods &= ~WPS_CONFIG_LABEL;
1361 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
1362 if (wpa_s->conf->device_type &&
1363 wps_dev_type_str2bin(wpa_s->conf->device_type,
1364 wps->dev.pri_dev_type) < 0)
1365 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
1368 if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1369 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1371 if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID) {
1372 if (is_nil_uuid(wpa_s->conf->uuid)) {
1373 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1374 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1375 "address", wps->uuid, WPS_UUID_LEN);
1377 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);