2 * wpa_supplicant / WPS integration
3 * Copyright (c) 2008, 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.
18 #include "ieee802_11_defs.h"
19 #include "wpa_common.h"
21 #include "eap_peer/eap.h"
22 #include "wpa_supplicant_i.h"
26 #include "eap_common/eap_wsc_common.h"
27 #include "wps_supplicant.h"
30 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
31 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
34 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
36 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
38 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
39 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
40 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
41 "try to associate with the received credential");
42 wpa_supplicant_deauthenticate(wpa_s,
43 WLAN_REASON_DEAUTH_LEAVING);
44 wpa_s->reassociate = 1;
45 wpa_supplicant_req_scan(wpa_s, 0, 0);
53 static int wpa_supplicant_wps_cred(void *ctx,
54 const struct wps_credential *cred)
56 struct wpa_supplicant *wpa_s = ctx;
57 struct wpa_ssid *ssid = wpa_s->current_ssid;
59 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
61 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
62 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
63 "on the received credential");
64 os_free(ssid->eap.identity);
65 ssid->eap.identity = NULL;
66 ssid->eap.identity_len = 0;
67 os_free(ssid->eap.phase1);
68 ssid->eap.phase1 = NULL;
69 os_free(ssid->eap.eap_methods);
70 ssid->eap.eap_methods = NULL;
72 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
73 "received credential");
74 ssid = wpa_config_add_network(wpa_s->conf);
79 wpa_config_set_network_defaults(ssid);
82 ssid->ssid = os_malloc(cred->ssid_len);
84 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
85 ssid->ssid_len = cred->ssid_len;
88 switch (cred->encr_type) {
90 ssid->pairwise_cipher = ssid->group_cipher = WPA_CIPHER_NONE;
93 ssid->pairwise_cipher = ssid->group_cipher =
94 WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104;
95 if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
96 cred->key_idx < NUM_WEP_KEYS) {
97 os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
99 ssid->wep_key_len[cred->key_idx] = cred->key_len;
100 ssid->wep_tx_keyidx = cred->key_idx;
104 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
105 ssid->group_cipher = WPA_CIPHER_TKIP;
108 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
109 ssid->group_cipher = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
113 switch (cred->auth_type) {
115 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
116 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
119 case WPS_AUTH_SHARED:
120 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
121 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
124 case WPS_AUTH_WPAPSK:
125 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
126 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
127 ssid->proto = WPA_PROTO_WPA;
130 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
131 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
132 ssid->proto = WPA_PROTO_WPA;
135 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
136 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
137 ssid->proto = WPA_PROTO_RSN;
139 case WPS_AUTH_WPA2PSK:
140 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
141 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
142 ssid->proto = WPA_PROTO_RSN;
146 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
147 if (cred->key_len == 2 * PMK_LEN) {
148 if (hexstr2bin((const char *) cred->key, ssid->psk,
150 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
155 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
156 os_free(ssid->passphrase);
157 ssid->passphrase = os_malloc(cred->key_len + 1);
158 if (ssid->passphrase == NULL)
160 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
161 ssid->passphrase[cred->key_len] = '\0';
162 wpa_config_update_psk(ssid);
164 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
166 (unsigned long) cred->key_len);
171 #ifndef CONFIG_NO_CONFIG_WRITE
172 if (wpa_s->conf->update_config &&
173 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
174 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
177 #endif /* CONFIG_NO_CONFIG_WRITE */
183 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
184 struct wps_event_m2d *m2d)
186 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
187 "dev_password_id=%d config_error=%d",
188 m2d->dev_password_id, m2d->config_error);
192 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
193 struct wps_event_fail *fail)
195 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
196 wpas_clear_wps(wpa_s);
200 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
202 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
206 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
207 union wps_event_data *data)
209 struct wpa_supplicant *wpa_s = ctx;
212 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
215 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
218 wpa_supplicant_wps_event_success(wpa_s);
224 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
226 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
227 eap_is_wps_pin_enrollee(&ssid->eap))
228 return WPS_REQ_ENROLLEE;
230 return WPS_REQ_REGISTRAR;
234 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
237 struct wpa_ssid *ssid;
239 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
241 /* Remove any existing WPS network from configuration */
242 ssid = wpa_s->conf->ssid;
244 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
245 if (ssid == wpa_s->current_ssid)
246 wpa_s->current_ssid = NULL;
252 wpa_config_remove_network(wpa_s->conf, id);
257 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
259 struct wpa_supplicant *wpa_s = eloop_ctx;
260 wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
261 wpas_clear_wps(wpa_s);
265 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
266 int registrar, const u8 *bssid)
268 struct wpa_ssid *ssid;
270 ssid = wpa_config_add_network(wpa_s->conf);
273 wpa_config_set_network_defaults(ssid);
274 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
275 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
276 wpa_config_set(ssid, "identity", registrar ?
277 "\"" WSC_ID_REGISTRAR "\"" :
278 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
279 wpa_config_remove_network(wpa_s->conf, ssid->id);
285 struct wpa_scan_res *res;
287 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
290 /* Try to get SSID from scan results */
291 if (wpa_s->scan_res == NULL &&
292 wpa_supplicant_get_scan_results(wpa_s) < 0)
293 return ssid; /* Could not find any scan results */
295 for (i = 0; i < wpa_s->scan_res->num; i++) {
298 res = wpa_s->scan_res->res[i];
299 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
302 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
306 ssid->ssid = os_malloc(ie[1]);
307 if (ssid->ssid == NULL)
309 os_memcpy(ssid->ssid, ie + 2, ie[1]);
310 ssid->ssid_len = ie[1];
319 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
320 struct wpa_ssid *selected)
322 struct wpa_ssid *ssid;
324 /* Mark all other networks disabled and trigger reassociation */
325 ssid = wpa_s->conf->ssid;
327 ssid->disabled = ssid != selected;
330 wpa_s->disconnected = 0;
331 wpa_s->reassociate = 1;
332 wpa_supplicant_req_scan(wpa_s, 0, 0);
336 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
338 struct wpa_ssid *ssid;
339 wpas_clear_wps(wpa_s);
340 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
343 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
344 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
346 wpas_wps_reassoc(wpa_s, ssid);
351 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
354 struct wpa_ssid *ssid;
356 unsigned int rpin = 0;
358 wpas_clear_wps(wpa_s);
359 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
363 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
365 rpin = wps_generate_pin();
366 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
368 wpa_config_set(ssid, "phase1", val, 0);
369 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
371 wpas_wps_reassoc(wpa_s, ssid);
376 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
379 struct wpa_ssid *ssid;
384 wpas_clear_wps(wpa_s);
385 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
388 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
389 wpa_config_set(ssid, "phase1", val, 0);
390 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
392 wpas_wps_reassoc(wpa_s, ssid);
397 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
400 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
401 "STA " MACSTR, MAC2STR(mac_addr));
402 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
410 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
411 const struct wps_device_data *dev)
413 char uuid[40], txt[400];
415 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
417 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
418 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
419 " [%s|%s|%s|%s|%s|%d-%08X-%d]",
420 uuid, MAC2STR(dev->mac_addr), dev->device_name,
421 dev->manufacturer, dev->model_name,
422 dev->model_number, dev->serial_number,
423 dev->categ, dev->oui, dev->sub_categ);
424 if (len > 0 && len < (int) sizeof(txt))
425 wpa_printf(MSG_INFO, "%s", txt);
429 int wpas_wps_init(struct wpa_supplicant *wpa_s)
431 struct wps_context *wps;
432 struct wps_registrar_config rcfg;
434 wps = os_zalloc(sizeof(*wps));
438 wps->cred_cb = wpa_supplicant_wps_cred;
439 wps->event_cb = wpa_supplicant_wps_event;
442 wps->dev.device_name = wpa_s->conf->device_name;
443 wps->dev.manufacturer = wpa_s->conf->manufacturer;
444 wps->dev.model_name = wpa_s->conf->model_name;
445 wps->dev.model_number = wpa_s->conf->model_number;
446 wps->dev.serial_number = wpa_s->conf->serial_number;
447 if (wpa_s->conf->device_type) {
450 /* <categ>-<OUI>-<subcateg> */
451 wps->dev.categ = atoi(wpa_s->conf->device_type);
452 pos = os_strchr(wpa_s->conf->device_type, '-');
454 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
459 if (hexstr2bin(pos, oui, 4)) {
460 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
464 wps->dev.oui = WPA_GET_BE32(oui);
465 pos = os_strchr(pos, '-');
467 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
472 wps->dev.sub_categ = atoi(pos);
474 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
475 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
476 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
477 if (is_nil_uuid(wpa_s->conf->uuid)) {
478 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
479 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
480 wps->uuid, WPS_UUID_LEN);
482 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
484 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
485 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
487 os_memset(&rcfg, 0, sizeof(rcfg));
488 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
489 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
492 wps->registrar = wps_registrar_init(wps, &rcfg);
493 if (wps->registrar == NULL) {
494 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
505 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
507 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
509 if (wpa_s->wps == NULL)
512 wps_registrar_deinit(wpa_s->wps->registrar);
513 os_free(wpa_s->wps->network_key);
519 int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
521 struct wpabuf *wps_ie;
523 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
526 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
527 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
529 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
533 if (!wps_is_selected_pbc_registrar(wps_ie)) {
534 wpa_printf(MSG_DEBUG, " skip - WPS AP "
535 "without active PBC Registrar");
540 /* TODO: overlap detection */
541 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
547 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
549 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
553 if (!wps_is_selected_pin_registrar(wps_ie)) {
554 wpa_printf(MSG_DEBUG, " skip - WPS AP "
555 "without active PIN Registrar");
559 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
566 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
575 int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
576 struct wpa_scan_res *bss)
578 struct wpabuf *wps_ie = NULL;
581 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
582 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
583 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
584 /* allow wildcard SSID for WPS PBC */
587 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
588 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
589 if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
590 /* allow wildcard SSID for WPS PIN */
595 if (!ret && ssid->bssid_set &&
596 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
597 /* allow wildcard SSID due to hardcoded BSSID match */
607 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
608 struct wpa_scan_res *selected,
609 struct wpa_ssid *ssid)
611 const u8 *sel_uuid, *uuid;
613 struct wpabuf *wps_ie;
616 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
619 /* Make sure that only one AP is in active PBC mode */
620 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
622 sel_uuid = wps_get_uuid_e(wps_ie);
626 for (i = 0; i < wpa_s->scan_res->num; i++) {
627 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
631 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
634 if (!wps_is_selected_pbc_registrar(ie)) {
638 uuid = wps_get_uuid_e(ie);
639 if (sel_uuid == NULL || uuid == NULL ||
640 os_memcmp(sel_uuid, uuid, 16) != 0) {
641 ret = 1; /* PBC overlap */
646 /* TODO: verify that this is reasonable dual-band situation */
657 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
661 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
664 for (i = 0; i < wpa_s->scan_res->num; i++) {
665 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
667 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
670 if (wps_is_selected_pbc_registrar(ie))
671 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
672 else if (wps_is_selected_pin_registrar(ie))
673 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
675 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
682 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
684 struct wpa_ssid *ssid;
686 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
687 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)