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"
24 #include "eap_common/eap_wsc_common.h"
26 #include "wps/wps_defs.h"
27 #include "wps_supplicant.h"
30 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
33 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
35 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
37 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
38 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
39 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
40 "try to associate with the received credential");
41 wpa_supplicant_deauthenticate(wpa_s,
42 WLAN_REASON_DEAUTH_LEAVING);
43 wpa_s->reassociate = 1;
44 wpa_supplicant_req_scan(wpa_s, 0, 0);
52 static int wpa_supplicant_wps_cred(void *ctx,
53 const struct wps_credential *cred)
55 struct wpa_supplicant *wpa_s = ctx;
56 struct wpa_ssid *ssid = wpa_s->current_ssid;
58 wpa_msg(wpa_s, MSG_INFO, "WPS: New credential received");
60 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
61 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
62 "on the received credential");
63 os_free(ssid->eap.identity);
64 ssid->eap.identity = NULL;
65 ssid->eap.identity_len = 0;
66 os_free(ssid->eap.phase1);
67 ssid->eap.phase1 = NULL;
68 os_free(ssid->eap.eap_methods);
69 ssid->eap.eap_methods = NULL;
71 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
72 "received credential");
73 ssid = wpa_config_add_network(wpa_s->conf);
78 wpa_config_set_network_defaults(ssid);
81 ssid->ssid = os_malloc(cred->ssid_len);
83 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
84 ssid->ssid_len = cred->ssid_len;
87 switch (cred->encr_type) {
89 ssid->pairwise_cipher = ssid->group_cipher = WPA_CIPHER_NONE;
92 ssid->pairwise_cipher = ssid->group_cipher =
93 WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104;
94 if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
95 cred->key_idx < NUM_WEP_KEYS) {
96 os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
98 ssid->wep_key_len[cred->key_idx] = cred->key_len;
99 ssid->wep_tx_keyidx = cred->key_idx;
103 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
104 ssid->group_cipher = WPA_CIPHER_TKIP;
107 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
108 ssid->group_cipher = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
112 switch (cred->auth_type) {
114 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
115 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
118 case WPS_AUTH_SHARED:
119 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
120 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
123 case WPS_AUTH_WPAPSK:
124 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
125 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
126 ssid->proto = WPA_PROTO_WPA;
129 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
130 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
131 ssid->proto = WPA_PROTO_WPA;
134 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
135 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
136 ssid->proto = WPA_PROTO_RSN;
138 case WPS_AUTH_WPA2PSK:
139 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
140 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
141 ssid->proto = WPA_PROTO_RSN;
145 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
146 if (cred->key_len == 2 * PMK_LEN) {
147 if (hexstr2bin((const char *) cred->key, ssid->psk,
149 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
154 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
155 os_free(ssid->passphrase);
156 ssid->passphrase = os_malloc(cred->key_len + 1);
157 if (ssid->passphrase == NULL)
159 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
160 ssid->passphrase[cred->key_len] = '\0';
161 wpa_config_update_psk(ssid);
163 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
165 (unsigned long) cred->key_len);
170 #ifndef CONFIG_NO_CONFIG_WRITE
171 if (wpa_s->conf->update_config &&
172 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
173 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
176 #endif /* CONFIG_NO_CONFIG_WRITE */
182 u8 wpas_wps_get_req_type(struct wpa_ssid *ssid)
184 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
185 eap_is_wps_pin_enrollee(&ssid->eap))
186 return WPS_REQ_ENROLLEE;
188 return WPS_REQ_REGISTRAR;
192 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
195 struct wpa_ssid *ssid;
197 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
199 /* Remove any existing WPS network from configuration */
200 ssid = wpa_s->conf->ssid;
202 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
208 wpa_config_remove_network(wpa_s->conf, id);
213 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
215 struct wpa_supplicant *wpa_s = eloop_ctx;
216 wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
217 wpas_clear_wps(wpa_s);
221 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
222 int registrar, const u8 *bssid)
224 struct wpa_ssid *ssid;
226 ssid = wpa_config_add_network(wpa_s->conf);
229 wpa_config_set_network_defaults(ssid);
230 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
231 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
232 wpa_config_set(ssid, "identity", registrar ?
233 "\"" WSC_ID_REGISTRAR "\"" :
234 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
235 wpa_config_remove_network(wpa_s->conf, ssid->id);
241 struct wpa_scan_res *res;
243 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
245 /* Try to get SSID from scan results */
246 if (wpa_s->scan_res == NULL &&
247 wpa_supplicant_get_scan_results(wpa_s) < 0)
248 return ssid; /* Could not find any scan results */
250 for (i = 0; i < wpa_s->scan_res->num; i++) {
253 res = wpa_s->scan_res->res[i];
254 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
257 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
261 ssid->ssid = os_malloc(ie[1]);
262 if (ssid->ssid == NULL)
264 os_memcpy(ssid->ssid, ie + 2, ie[1]);
265 ssid->ssid_len = ie[1];
274 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
275 struct wpa_ssid *selected)
277 struct wpa_ssid *ssid;
279 /* Mark all other networks disabled and trigger reassociation */
280 ssid = wpa_s->conf->ssid;
282 ssid->disabled = ssid != selected;
285 wpa_s->disconnected = 0;
286 wpa_s->reassociate = 1;
287 wpa_supplicant_req_scan(wpa_s, 0, 0);
291 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
293 struct wpa_ssid *ssid;
294 wpas_clear_wps(wpa_s);
295 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
298 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
299 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
301 wpas_wps_reassoc(wpa_s, ssid);
306 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
309 struct wpa_ssid *ssid;
311 unsigned int rpin = 0;
313 wpas_clear_wps(wpa_s);
314 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
318 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
320 rpin = wps_generate_pin();
321 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
323 wpa_config_set(ssid, "phase1", val, 0);
324 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
326 wpas_wps_reassoc(wpa_s, ssid);
331 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
334 struct wpa_ssid *ssid;
339 wpas_clear_wps(wpa_s);
340 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
343 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
344 wpa_config_set(ssid, "phase1", val, 0);
345 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
347 wpas_wps_reassoc(wpa_s, ssid);
352 int wpas_wps_init(struct wpa_supplicant *wpa_s)
354 struct wps_context *wps;
356 wps = os_zalloc(sizeof(*wps));
360 wps->cred_cb = wpa_supplicant_wps_cred;
363 /* TODO: make the device data configurable */
364 wps->dev.device_name = "dev name";
365 wps->dev.manufacturer = "manuf";
366 wps->dev.model_name = "model name";
367 wps->dev.model_number = "model number";
368 wps->dev.serial_number = "12345";
369 wps->dev.categ = WPS_DEV_COMPUTER;
370 wps->dev.oui = WPS_DEV_OUI_WFA;
371 wps->dev.sub_categ = WPS_DEV_COMPUTER_PC;
372 wps->dev.os_version = 0;
373 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
374 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
375 os_memcpy(wps->uuid, wpa_s->conf->uuid, 16);
383 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
385 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
387 if (wpa_s->wps == NULL)
390 os_free(wpa_s->wps->network_key);
396 int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
398 struct wpabuf *wps_ie;
400 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
403 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
404 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
406 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
410 if (!wps_is_selected_pbc_registrar(wps_ie)) {
411 wpa_printf(MSG_DEBUG, " skip - WPS AP "
412 "without active PBC Registrar");
417 /* TODO: overlap detection */
418 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
424 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
426 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
430 if (!wps_is_selected_pin_registrar(wps_ie)) {
431 wpa_printf(MSG_DEBUG, " skip - WPS AP "
432 "without active PIN Registrar");
436 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
443 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
452 int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
453 struct wpa_scan_res *bss)
455 struct wpabuf *wps_ie = NULL;
458 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
459 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
460 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
461 /* allow wildcard SSID for WPS PBC */
464 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
465 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
466 if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
467 /* allow wildcard SSID for WPS PIN */
478 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
479 struct wpa_scan_res *selected,
480 struct wpa_ssid *ssid)
482 const u8 *sel_uuid, *uuid;
484 struct wpabuf *wps_ie;
487 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
490 /* Make sure that only one AP is in active PBC mode */
491 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
493 sel_uuid = wps_get_uuid_e(wps_ie);
497 wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for PBC "
498 "overlap detection");
503 for (i = 0; i < wpa_s->scan_res->num; i++) {
504 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
508 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
511 if (!wps_is_selected_pbc_registrar(ie)) {
515 uuid = wps_get_uuid_e(ie);
517 wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for "
518 "PBC overlap detection (other BSS)");
523 if (os_memcmp(sel_uuid, uuid, 16) != 0) {
524 ret = 1; /* PBC overlap */
529 /* TODO: verify that this is reasonable dual-band situation */