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"
25 #include "eap_common/eap_wsc_common.h"
27 #include "wps/wps_defs.h"
28 #include "wps_supplicant.h"
31 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
32 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
35 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
37 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
39 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
40 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
41 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
42 "try to associate with the received credential");
43 wpa_supplicant_deauthenticate(wpa_s,
44 WLAN_REASON_DEAUTH_LEAVING);
45 wpa_s->reassociate = 1;
46 wpa_supplicant_req_scan(wpa_s, 0, 0);
54 static int wpa_supplicant_wps_cred(void *ctx,
55 const struct wps_credential *cred)
57 struct wpa_supplicant *wpa_s = ctx;
58 struct wpa_ssid *ssid = wpa_s->current_ssid;
60 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
62 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
63 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
64 "on the received credential");
65 os_free(ssid->eap.identity);
66 ssid->eap.identity = NULL;
67 ssid->eap.identity_len = 0;
68 os_free(ssid->eap.phase1);
69 ssid->eap.phase1 = NULL;
70 os_free(ssid->eap.eap_methods);
71 ssid->eap.eap_methods = NULL;
73 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
74 "received credential");
75 ssid = wpa_config_add_network(wpa_s->conf);
80 wpa_config_set_network_defaults(ssid);
83 ssid->ssid = os_malloc(cred->ssid_len);
85 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
86 ssid->ssid_len = cred->ssid_len;
89 switch (cred->encr_type) {
91 ssid->pairwise_cipher = ssid->group_cipher = WPA_CIPHER_NONE;
94 ssid->pairwise_cipher = ssid->group_cipher =
95 WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104;
96 if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
97 cred->key_idx < NUM_WEP_KEYS) {
98 os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
100 ssid->wep_key_len[cred->key_idx] = cred->key_len;
101 ssid->wep_tx_keyidx = cred->key_idx;
105 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
106 ssid->group_cipher = WPA_CIPHER_TKIP;
109 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
110 ssid->group_cipher = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
114 switch (cred->auth_type) {
116 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
117 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
120 case WPS_AUTH_SHARED:
121 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
122 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
125 case WPS_AUTH_WPAPSK:
126 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
127 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
128 ssid->proto = WPA_PROTO_WPA;
131 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
132 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
133 ssid->proto = WPA_PROTO_WPA;
136 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
137 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
138 ssid->proto = WPA_PROTO_RSN;
140 case WPS_AUTH_WPA2PSK:
141 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
142 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
143 ssid->proto = WPA_PROTO_RSN;
147 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
148 if (cred->key_len == 2 * PMK_LEN) {
149 if (hexstr2bin((const char *) cred->key, ssid->psk,
151 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
156 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
157 os_free(ssid->passphrase);
158 ssid->passphrase = os_malloc(cred->key_len + 1);
159 if (ssid->passphrase == NULL)
161 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
162 ssid->passphrase[cred->key_len] = '\0';
163 wpa_config_update_psk(ssid);
165 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
167 (unsigned long) cred->key_len);
172 #ifndef CONFIG_NO_CONFIG_WRITE
173 if (wpa_s->conf->update_config &&
174 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
175 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
178 #endif /* CONFIG_NO_CONFIG_WRITE */
184 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
185 struct wps_event_m2d *m2d)
187 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
188 "dev_password_id=%d config_error=%d",
189 m2d->dev_password_id, m2d->config_error);
193 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
194 struct wps_event_fail *fail)
196 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
197 wpas_clear_wps(wpa_s);
201 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
202 union wps_event_data *data)
204 struct wpa_supplicant *wpa_s = ctx;
207 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
210 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
216 u8 wpas_wps_get_req_type(struct wpa_ssid *ssid)
218 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
219 eap_is_wps_pin_enrollee(&ssid->eap))
220 return WPS_REQ_ENROLLEE;
222 return WPS_REQ_REGISTRAR;
226 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
229 struct wpa_ssid *ssid;
231 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
233 /* Remove any existing WPS network from configuration */
234 ssid = wpa_s->conf->ssid;
236 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
237 if (ssid == wpa_s->current_ssid)
238 wpa_s->current_ssid = NULL;
244 wpa_config_remove_network(wpa_s->conf, id);
249 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
251 struct wpa_supplicant *wpa_s = eloop_ctx;
252 wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
253 wpas_clear_wps(wpa_s);
257 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
258 int registrar, const u8 *bssid)
260 struct wpa_ssid *ssid;
262 ssid = wpa_config_add_network(wpa_s->conf);
265 wpa_config_set_network_defaults(ssid);
266 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
267 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
268 wpa_config_set(ssid, "identity", registrar ?
269 "\"" WSC_ID_REGISTRAR "\"" :
270 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
271 wpa_config_remove_network(wpa_s->conf, ssid->id);
277 struct wpa_scan_res *res;
279 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
281 /* Try to get SSID from scan results */
282 if (wpa_s->scan_res == NULL &&
283 wpa_supplicant_get_scan_results(wpa_s) < 0)
284 return ssid; /* Could not find any scan results */
286 for (i = 0; i < wpa_s->scan_res->num; i++) {
289 res = wpa_s->scan_res->res[i];
290 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
293 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
297 ssid->ssid = os_malloc(ie[1]);
298 if (ssid->ssid == NULL)
300 os_memcpy(ssid->ssid, ie + 2, ie[1]);
301 ssid->ssid_len = ie[1];
310 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
311 struct wpa_ssid *selected)
313 struct wpa_ssid *ssid;
315 /* Mark all other networks disabled and trigger reassociation */
316 ssid = wpa_s->conf->ssid;
318 ssid->disabled = ssid != selected;
321 wpa_s->disconnected = 0;
322 wpa_s->reassociate = 1;
323 wpa_supplicant_req_scan(wpa_s, 0, 0);
327 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
329 struct wpa_ssid *ssid;
330 wpas_clear_wps(wpa_s);
331 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
334 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
335 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
337 wpas_wps_reassoc(wpa_s, ssid);
342 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
345 struct wpa_ssid *ssid;
347 unsigned int rpin = 0;
349 wpas_clear_wps(wpa_s);
350 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
354 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
356 rpin = wps_generate_pin();
357 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
359 wpa_config_set(ssid, "phase1", val, 0);
360 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
362 wpas_wps_reassoc(wpa_s, ssid);
367 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
370 struct wpa_ssid *ssid;
375 wpas_clear_wps(wpa_s);
376 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
379 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
380 wpa_config_set(ssid, "phase1", val, 0);
381 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
383 wpas_wps_reassoc(wpa_s, ssid);
388 int wpas_wps_init(struct wpa_supplicant *wpa_s)
390 struct wps_context *wps;
392 wps = os_zalloc(sizeof(*wps));
396 wps->cred_cb = wpa_supplicant_wps_cred;
397 wps->event_cb = wpa_supplicant_wps_event;
400 wps->dev.device_name = wpa_s->conf->device_name;
401 wps->dev.manufacturer = wpa_s->conf->manufacturer;
402 wps->dev.model_name = wpa_s->conf->model_name;
403 wps->dev.model_number = wpa_s->conf->model_number;
404 wps->dev.serial_number = wpa_s->conf->serial_number;
405 if (wpa_s->conf->device_type) {
408 /* <categ>-<OUI>-<subcateg> */
409 wps->dev.categ = atoi(wpa_s->conf->device_type);
410 pos = os_strchr(wpa_s->conf->device_type, '-');
412 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
417 if (hexstr2bin(pos, oui, 4)) {
418 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
422 wps->dev.oui = WPA_GET_BE32(oui);
423 pos = os_strchr(pos, '-');
425 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
430 wps->dev.sub_categ = atoi(pos);
432 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
433 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
434 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
435 os_memcpy(wps->uuid, wpa_s->conf->uuid, 16);
443 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
445 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
447 if (wpa_s->wps == NULL)
450 os_free(wpa_s->wps->network_key);
456 int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
458 struct wpabuf *wps_ie;
460 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
463 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
464 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
466 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
470 if (!wps_is_selected_pbc_registrar(wps_ie)) {
471 wpa_printf(MSG_DEBUG, " skip - WPS AP "
472 "without active PBC Registrar");
477 /* TODO: overlap detection */
478 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
484 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
486 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
490 if (!wps_is_selected_pin_registrar(wps_ie)) {
491 wpa_printf(MSG_DEBUG, " skip - WPS AP "
492 "without active PIN Registrar");
496 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
503 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
512 int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
513 struct wpa_scan_res *bss)
515 struct wpabuf *wps_ie = NULL;
518 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
519 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
520 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
521 /* allow wildcard SSID for WPS PBC */
524 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
525 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
526 if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
527 /* allow wildcard SSID for WPS PIN */
538 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
539 struct wpa_scan_res *selected,
540 struct wpa_ssid *ssid)
542 const u8 *sel_uuid, *uuid;
544 struct wpabuf *wps_ie;
547 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
550 /* Make sure that only one AP is in active PBC mode */
551 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
553 sel_uuid = wps_get_uuid_e(wps_ie);
557 wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for PBC "
558 "overlap detection");
563 for (i = 0; i < wpa_s->scan_res->num; i++) {
564 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
568 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
571 if (!wps_is_selected_pbc_registrar(ie)) {
575 uuid = wps_get_uuid_e(ie);
577 wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for "
578 "PBC overlap detection (other BSS)");
583 if (os_memcmp(sel_uuid, uuid, 16) != 0) {
584 ret = 1; /* PBC overlap */
589 /* TODO: verify that this is reasonable dual-band situation */
600 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
604 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
607 for (i = 0; i < wpa_s->scan_res->num; i++) {
608 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
610 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
613 if (wps_is_selected_pbc_registrar(ie))
614 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
615 else if (wps_is_selected_pin_registrar(ie))
616 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
618 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);