2 * hostapd / 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.
22 #include "ctrl_iface.h"
23 #include "ieee802_11_defs.h"
25 #include "wps/wps_defs.h"
26 #include "wps/wps_dev_attr.h"
27 #include "wps_hostapd.h"
30 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
33 struct hostapd_data *hapd = ctx;
34 struct hostapd_wpa_psk *p;
35 struct hostapd_ssid *ssid = &hapd->conf->ssid;
37 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
38 MACSTR, MAC2STR(mac_addr));
39 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
41 if (psk_len != PMK_LEN) {
42 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
43 (unsigned long) psk_len);
47 /* Add the new PSK to runtime PSK list */
48 p = os_zalloc(sizeof(*p));
51 os_memcpy(p->addr, mac_addr, ETH_ALEN);
52 os_memcpy(p->psk, psk, PMK_LEN);
54 p->next = ssid->wpa_psk;
57 if (ssid->wpa_psk_file) {
59 char hex[PMK_LEN * 2 + 1];
60 /* Add the new PSK to PSK list file */
61 f = fopen(ssid->wpa_psk_file, "a");
63 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
64 "'%s'", ssid->wpa_psk_file);
68 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
69 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
77 static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
78 size_t beacon_ie_len, const u8 *probe_resp_ie,
79 size_t probe_resp_ie_len)
81 struct hostapd_data *hapd = ctx;
83 os_free(hapd->wps_beacon_ie);
84 if (beacon_ie_len == 0) {
85 hapd->wps_beacon_ie = NULL;
86 hapd->wps_beacon_ie_len = 0;
88 hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
89 if (hapd->wps_beacon_ie == NULL) {
90 hapd->wps_beacon_ie_len = 0;
93 os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
94 hapd->wps_beacon_ie_len = beacon_ie_len;
96 hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
97 hapd->wps_beacon_ie_len);
99 os_free(hapd->wps_probe_resp_ie);
100 if (probe_resp_ie_len == 0) {
101 hapd->wps_probe_resp_ie = NULL;
102 hapd->wps_probe_resp_ie_len = 0;
104 hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
105 if (hapd->wps_probe_resp_ie == NULL) {
106 hapd->wps_probe_resp_ie_len = 0;
109 os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
111 hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
113 hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
114 hapd->wps_probe_resp_ie_len);
120 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
121 const struct wps_device_data *dev)
123 struct hostapd_data *hapd = ctx;
124 char uuid[40], txt[400];
126 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
128 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
129 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
130 "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
131 uuid, MAC2STR(dev->mac_addr), dev->device_name,
132 dev->manufacturer, dev->model_name,
133 dev->model_number, dev->serial_number,
134 dev->categ, dev->oui, dev->sub_categ);
135 if (len > 0 && len < (int) sizeof(txt))
136 hostapd_ctrl_iface_send(hapd, MSG_INFO, txt, len);
138 if (hapd->conf->wps_pin_requests) {
141 f = fopen(hapd->conf->wps_pin_requests, "a");
145 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
147 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
148 dev->manufacturer, dev->model_name, dev->model_number,
150 dev->categ, dev->oui, dev->sub_categ);
156 static int str_starts(const char *str, const char *start)
158 return os_strncmp(str, start, os_strlen(start)) == 0;
162 static void wps_reload_config(void *eloop_data, void *user_ctx)
164 struct hostapd_iface *iface = eloop_data;
166 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
167 if (hostapd_reload_config(iface) < 0) {
168 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
174 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
176 struct hostapd_data *hapd = ctx;
184 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
185 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
186 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
188 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
189 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
190 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
191 cred->key, cred->key_len);
192 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
193 MAC2STR(cred->mac_addr));
195 hostapd_ctrl_iface_send(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS,
196 os_strlen(WPS_EVENT_NEW_AP_SETTINGS));
198 len = os_strlen(hapd->iface->config_fname) + 5;
199 tmp_fname = os_malloc(len);
200 if (tmp_fname == NULL)
202 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
204 oconf = fopen(hapd->iface->config_fname, "r");
206 wpa_printf(MSG_WARNING, "WPS: Could not open current "
207 "configuration file");
212 nconf = fopen(tmp_fname, "w");
214 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
215 "configuration file");
221 fprintf(nconf, "# WPS configuration - START\n");
223 fprintf(nconf, "wps_state=2\n");
225 fprintf(nconf, "ssid=");
226 for (i = 0; i < cred->ssid_len; i++)
227 fputc(cred->ssid[i], nconf);
228 fprintf(nconf, "\n");
230 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
231 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
233 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
235 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
242 fprintf(nconf, "wpa=%d\n", wpa);
244 fprintf(nconf, "wpa_key_mgmt=");
246 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
247 fprintf(nconf, "WPA-EAP");
250 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
251 fprintf(nconf, "%sWPA-PSK", prefix);
252 fprintf(nconf, "\n");
254 fprintf(nconf, "wpa_pairwise=");
256 if (cred->encr_type & WPS_ENCR_AES) {
257 fprintf(nconf, "CCMP");
260 if (cred->encr_type & WPS_ENCR_TKIP) {
261 fprintf(nconf, "%sTKIP", prefix);
263 fprintf(nconf, "\n");
265 if (cred->key_len >= 8 && cred->key_len < 64) {
266 fprintf(nconf, "wpa_passphrase=");
267 for (i = 0; i < cred->key_len; i++)
268 fputc(cred->key[i], nconf);
269 fprintf(nconf, "\n");
270 } else if (cred->key_len == 64) {
271 fprintf(nconf, "wpa_psk=");
272 for (i = 0; i < cred->key_len; i++)
273 fputc(cred->key[i], nconf);
274 fprintf(nconf, "\n");
276 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
278 (unsigned long) cred->key_len);
281 fprintf(nconf, "auth_algs=1\n");
283 if ((cred->auth_type & WPS_AUTH_OPEN) &&
284 (cred->auth_type & WPS_AUTH_SHARED))
285 fprintf(nconf, "auth_algs=3\n");
286 else if (cred->auth_type & WPS_AUTH_SHARED)
287 fprintf(nconf, "auth_algs=2\n");
289 fprintf(nconf, "auth_algs=1\n");
291 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx < 4) {
292 fprintf(nconf, "wep_default_key=%d\n", cred->key_idx);
293 fprintf(nconf, "wep_key%d=", cred->key_idx);
294 if (cred->key_len != 10 && cred->key_len != 26)
296 for (i = 0; i < cred->key_len; i++)
297 fputc(cred->key[i], nconf);
298 if (cred->key_len != 10 && cred->key_len != 26)
300 fprintf(nconf, "\n");
304 fprintf(nconf, "# WPS configuration - END\n");
307 while (fgets(buf, sizeof(buf), oconf)) {
308 if (os_strncmp(buf, "bss=", 4) == 0)
311 (str_starts(buf, "ssid=") ||
312 str_starts(buf, "auth_algs=") ||
313 str_starts(buf, "wps_state=") ||
314 str_starts(buf, "wpa=") ||
315 str_starts(buf, "wpa_psk=") ||
316 str_starts(buf, "wpa_pairwise=") ||
317 str_starts(buf, "rsn_pairwise=") ||
318 str_starts(buf, "wpa_key_mgmt=") ||
319 str_starts(buf, "wpa_passphrase="))) {
320 fprintf(nconf, "#WPS# %s", buf);
322 fprintf(nconf, "%s", buf);
328 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
329 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
330 "configuration file: %s", strerror(errno));
337 /* Schedule configuration reload after short period of time to allow
338 * EAP-WSC to be finished.
340 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
343 /* TODO: dualband AP may need to update multiple configuration files */
345 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
351 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
353 os_free(hapd->wps_beacon_ie);
354 hapd->wps_beacon_ie = NULL;
355 hapd->wps_beacon_ie_len = 0;
356 hostapd_set_wps_beacon_ie(hapd, NULL, 0);
358 os_free(hapd->wps_probe_resp_ie);
359 hapd->wps_probe_resp_ie = NULL;
360 hapd->wps_probe_resp_ie_len = 0;
361 hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
365 int hostapd_init_wps(struct hostapd_data *hapd,
366 struct hostapd_bss_config *conf)
368 struct wps_context *wps;
369 struct wps_registrar_config cfg;
371 if (conf->wps_state == 0) {
372 hostapd_wps_clear_ies(hapd);
376 wps = os_zalloc(sizeof(*wps));
380 wps->cred_cb = hostapd_wps_cred_cb;
383 os_memset(&cfg, 0, sizeof(cfg));
384 wps->wps_state = hapd->conf->wps_state;
385 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
386 if (is_nil_uuid(hapd->conf->uuid)) {
387 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
388 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
389 wps->uuid, UUID_LEN);
391 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
392 wps->ssid_len = hapd->conf->ssid.ssid_len;
393 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
395 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
396 wps->dev.device_name = os_strdup(hapd->conf->device_name);
397 wps->dev.manufacturer = os_strdup(hapd->conf->manufacturer);
398 wps->dev.model_name = os_strdup(hapd->conf->model_name);
399 wps->dev.model_number = os_strdup(hapd->conf->model_number);
400 wps->dev.serial_number = os_strdup(hapd->conf->serial_number);
401 if (hapd->conf->config_methods) {
402 char *m = hapd->conf->config_methods;
403 if (os_strstr(m, "label"))
404 wps->config_methods |= WPS_CONFIG_LABEL;
405 if (os_strstr(m, "display"))
406 wps->config_methods |= WPS_CONFIG_DISPLAY;
407 if (os_strstr(m, "push_button"))
408 wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
409 if (os_strstr(m, "keypad"))
410 wps->config_methods |= WPS_CONFIG_KEYPAD;
412 if (hapd->conf->device_type) {
415 /* <categ>-<OUI>-<subcateg> */
416 wps->dev.categ = atoi(hapd->conf->device_type);
417 pos = os_strchr(hapd->conf->device_type, '-');
419 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
424 if (hexstr2bin(pos, oui, 4)) {
425 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
429 wps->dev.oui = WPA_GET_BE32(oui);
430 pos = os_strchr(pos, '-');
432 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
437 wps->dev.sub_categ = atoi(pos);
439 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
440 wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
441 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
443 if (conf->wpa & WPA_PROTO_RSN) {
444 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
445 wps->auth_types |= WPS_AUTH_WPA2PSK;
446 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
447 wps->auth_types |= WPS_AUTH_WPA2;
449 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
450 wps->encr_types |= WPS_ENCR_AES;
451 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
452 wps->encr_types |= WPS_ENCR_TKIP;
455 if (conf->wpa & WPA_PROTO_WPA) {
456 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
457 wps->auth_types |= WPS_AUTH_WPAPSK;
458 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
459 wps->auth_types |= WPS_AUTH_WPA;
461 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
462 wps->encr_types |= WPS_ENCR_AES;
463 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
464 wps->encr_types |= WPS_ENCR_TKIP;
467 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
468 wps->encr_types |= WPS_ENCR_NONE;
469 wps->auth_types |= WPS_AUTH_OPEN;
470 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
471 wps->encr_types |= WPS_ENCR_WEP;
472 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
473 wps->auth_types |= WPS_AUTH_OPEN;
474 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
475 wps->auth_types |= WPS_AUTH_SHARED;
476 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
477 wps->auth_types |= WPS_AUTH_OPEN;
478 if (conf->default_wep_key_len)
479 wps->encr_types |= WPS_ENCR_WEP;
481 wps->encr_types |= WPS_ENCR_NONE;
484 if (conf->ssid.wpa_psk_file) {
485 /* Use per-device PSKs */
486 } else if (conf->ssid.wpa_passphrase) {
487 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
488 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
489 } else if (conf->ssid.wpa_psk) {
490 wps->network_key = os_malloc(2 * PMK_LEN + 1);
491 if (wps->network_key == NULL) {
495 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
496 conf->ssid.wpa_psk->psk, PMK_LEN);
497 wps->network_key_len = 2 * PMK_LEN;
498 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
499 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
500 if (wps->network_key == NULL) {
504 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
505 conf->ssid.wep.len[0]);
506 wps->network_key_len = conf->ssid.wep.len[0];
509 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
510 /* Override parameters to enable security by default */
511 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
512 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
515 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
516 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
517 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
520 wps->registrar = wps_registrar_init(wps, &cfg);
521 if (wps->registrar == NULL) {
522 printf("Failed to initialize WPS Registrar\n");
523 os_free(wps->network_key);
534 void hostapd_deinit_wps(struct hostapd_data *hapd)
536 if (hapd->wps == NULL)
538 wps_registrar_deinit(hapd->wps->registrar);
539 os_free(hapd->wps->network_key);
540 wps_device_data_free(&hapd->wps->dev);
543 hostapd_wps_clear_ies(hapd);
547 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
553 if (hapd->wps == NULL)
555 if (os_strcmp(uuid, "any") == 0)
557 else if (uuid_str2bin(uuid, u))
559 return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
560 (const u8 *) pin, os_strlen(pin));
564 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
566 if (hapd->wps == NULL)
568 return wps_registrar_button_pushed(hapd->wps->registrar);
572 void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
573 const u8 *ie, size_t ie_len)
575 struct wpabuf *wps_ie;
576 const u8 *end, *pos, *wps;
578 if (hapd->wps == NULL)
585 while (pos + 1 < end) {
586 if (pos + 2 + pos[1] > end)
588 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
589 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
597 return; /* No WPS IE in Probe Request */
599 wps_ie = wpabuf_alloc(ie_len);
603 /* There may be multiple WPS IEs in the message, so need to concatenate
604 * their WPS Data fields */
605 while (pos + 1 < end) {
606 if (pos + 2 + pos[1] > end)
608 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
609 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
610 wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
614 if (wpabuf_len(wps_ie) > 0)
615 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);