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 "ieee802_11_defs.h"
24 #include "wps/wps_defs.h"
25 #include "wps/wps_dev_attr.h"
26 #include "wps_hostapd.h"
29 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
32 struct hostapd_data *hapd = ctx;
33 struct hostapd_wpa_psk *p;
34 struct hostapd_ssid *ssid = &hapd->conf->ssid;
36 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
37 MACSTR, MAC2STR(mac_addr));
38 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
40 if (psk_len != PMK_LEN) {
41 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
42 (unsigned long) psk_len);
46 /* Add the new PSK to runtime PSK list */
47 p = os_zalloc(sizeof(*p));
50 os_memcpy(p->addr, mac_addr, ETH_ALEN);
51 os_memcpy(p->psk, psk, PMK_LEN);
53 p->next = ssid->wpa_psk;
56 if (ssid->wpa_psk_file) {
58 char hex[PMK_LEN * 2 + 1];
59 /* Add the new PSK to PSK list file */
60 f = fopen(ssid->wpa_psk_file, "a");
62 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
63 "'%s'", ssid->wpa_psk_file);
67 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
68 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
76 static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
77 size_t beacon_ie_len, const u8 *probe_resp_ie,
78 size_t probe_resp_ie_len)
80 struct hostapd_data *hapd = ctx;
82 os_free(hapd->wps_beacon_ie);
83 if (beacon_ie_len == 0) {
84 hapd->wps_beacon_ie = NULL;
85 hapd->wps_beacon_ie_len = 0;
87 hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
88 if (hapd->wps_beacon_ie == NULL) {
89 hapd->wps_beacon_ie_len = 0;
92 os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
93 hapd->wps_beacon_ie_len = beacon_ie_len;
95 hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
96 hapd->wps_beacon_ie_len);
98 os_free(hapd->wps_probe_resp_ie);
99 if (probe_resp_ie_len == 0) {
100 hapd->wps_probe_resp_ie = NULL;
101 hapd->wps_probe_resp_ie_len = 0;
103 hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
104 if (hapd->wps_probe_resp_ie == NULL) {
105 hapd->wps_probe_resp_ie_len = 0;
108 os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
110 hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
112 hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
113 hapd->wps_probe_resp_ie_len);
119 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
120 const struct wps_device_data *dev)
122 struct hostapd_data *hapd = ctx;
123 char uuid[40], txt[400];
125 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
127 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
128 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
129 "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
130 uuid, MAC2STR(dev->mac_addr), dev->device_name,
131 dev->manufacturer, dev->model_name,
132 dev->model_number, dev->serial_number,
133 dev->categ, dev->oui, dev->sub_categ);
134 if (len > 0 && len < (int) sizeof(txt))
135 wpa_msg(hapd, MSG_INFO, "%s", txt);
137 if (hapd->conf->wps_pin_requests) {
140 f = fopen(hapd->conf->wps_pin_requests, "a");
144 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
146 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
147 dev->manufacturer, dev->model_name, dev->model_number,
149 dev->categ, dev->oui, dev->sub_categ);
155 static int str_starts(const char *str, const char *start)
157 return os_strncmp(str, start, os_strlen(start)) == 0;
161 static void wps_reload_config(void *eloop_data, void *user_ctx)
163 struct hostapd_iface *iface = eloop_data;
165 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
166 if (hostapd_reload_config(iface) < 0) {
167 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
173 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
175 struct hostapd_data *hapd = ctx;
183 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
184 cred->cred_attr, cred->cred_attr_len);
186 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
187 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
188 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
190 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
191 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
192 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
193 cred->key, cred->key_len);
194 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
195 MAC2STR(cred->mac_addr));
197 if ((hapd->conf->wps_cred_processing == 1 ||
198 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
199 size_t blen = cred->cred_attr_len * 2 + 1;
200 char *buf = os_malloc(blen);
202 wpa_snprintf_hex(buf, blen,
203 cred->cred_attr, cred->cred_attr_len);
204 wpa_msg(hapd, MSG_INFO, "%s%s",
205 WPS_EVENT_NEW_AP_SETTINGS, buf);
209 wpa_msg(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
211 if (hapd->conf->wps_cred_processing == 1)
214 len = os_strlen(hapd->iface->config_fname) + 5;
215 tmp_fname = os_malloc(len);
216 if (tmp_fname == NULL)
218 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
220 oconf = fopen(hapd->iface->config_fname, "r");
222 wpa_printf(MSG_WARNING, "WPS: Could not open current "
223 "configuration file");
228 nconf = fopen(tmp_fname, "w");
230 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
231 "configuration file");
237 fprintf(nconf, "# WPS configuration - START\n");
239 fprintf(nconf, "wps_state=2\n");
241 fprintf(nconf, "ssid=");
242 for (i = 0; i < cred->ssid_len; i++)
243 fputc(cred->ssid[i], nconf);
244 fprintf(nconf, "\n");
246 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
247 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
249 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
251 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
258 fprintf(nconf, "wpa=%d\n", wpa);
260 fprintf(nconf, "wpa_key_mgmt=");
262 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
263 fprintf(nconf, "WPA-EAP");
266 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
267 fprintf(nconf, "%sWPA-PSK", prefix);
268 fprintf(nconf, "\n");
270 fprintf(nconf, "wpa_pairwise=");
272 if (cred->encr_type & WPS_ENCR_AES) {
273 fprintf(nconf, "CCMP");
276 if (cred->encr_type & WPS_ENCR_TKIP) {
277 fprintf(nconf, "%sTKIP", prefix);
279 fprintf(nconf, "\n");
281 if (cred->key_len >= 8 && cred->key_len < 64) {
282 fprintf(nconf, "wpa_passphrase=");
283 for (i = 0; i < cred->key_len; i++)
284 fputc(cred->key[i], nconf);
285 fprintf(nconf, "\n");
286 } else if (cred->key_len == 64) {
287 fprintf(nconf, "wpa_psk=");
288 for (i = 0; i < cred->key_len; i++)
289 fputc(cred->key[i], nconf);
290 fprintf(nconf, "\n");
292 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
294 (unsigned long) cred->key_len);
297 fprintf(nconf, "auth_algs=1\n");
299 if ((cred->auth_type & WPS_AUTH_OPEN) &&
300 (cred->auth_type & WPS_AUTH_SHARED))
301 fprintf(nconf, "auth_algs=3\n");
302 else if (cred->auth_type & WPS_AUTH_SHARED)
303 fprintf(nconf, "auth_algs=2\n");
305 fprintf(nconf, "auth_algs=1\n");
307 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx < 4) {
308 fprintf(nconf, "wep_default_key=%d\n", cred->key_idx);
309 fprintf(nconf, "wep_key%d=", cred->key_idx);
310 if (cred->key_len != 10 && cred->key_len != 26)
312 for (i = 0; i < cred->key_len; i++)
313 fputc(cred->key[i], nconf);
314 if (cred->key_len != 10 && cred->key_len != 26)
316 fprintf(nconf, "\n");
320 fprintf(nconf, "# WPS configuration - END\n");
323 while (fgets(buf, sizeof(buf), oconf)) {
324 if (os_strncmp(buf, "bss=", 4) == 0)
327 (str_starts(buf, "ssid=") ||
328 str_starts(buf, "auth_algs=") ||
329 str_starts(buf, "wps_state=") ||
330 str_starts(buf, "wpa=") ||
331 str_starts(buf, "wpa_psk=") ||
332 str_starts(buf, "wpa_pairwise=") ||
333 str_starts(buf, "rsn_pairwise=") ||
334 str_starts(buf, "wpa_key_mgmt=") ||
335 str_starts(buf, "wpa_passphrase="))) {
336 fprintf(nconf, "#WPS# %s", buf);
338 fprintf(nconf, "%s", buf);
344 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
345 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
346 "configuration file: %s", strerror(errno));
353 /* Schedule configuration reload after short period of time to allow
354 * EAP-WSC to be finished.
356 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
359 /* TODO: dualband AP may need to update multiple configuration files */
361 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
367 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
369 os_free(hapd->wps_beacon_ie);
370 hapd->wps_beacon_ie = NULL;
371 hapd->wps_beacon_ie_len = 0;
372 hostapd_set_wps_beacon_ie(hapd, NULL, 0);
374 os_free(hapd->wps_probe_resp_ie);
375 hapd->wps_probe_resp_ie = NULL;
376 hapd->wps_probe_resp_ie_len = 0;
377 hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
381 int hostapd_init_wps(struct hostapd_data *hapd,
382 struct hostapd_bss_config *conf)
384 struct wps_context *wps;
385 struct wps_registrar_config cfg;
387 if (conf->wps_state == 0) {
388 hostapd_wps_clear_ies(hapd);
392 wps = os_zalloc(sizeof(*wps));
396 wps->cred_cb = hostapd_wps_cred_cb;
399 os_memset(&cfg, 0, sizeof(cfg));
400 wps->wps_state = hapd->conf->wps_state;
401 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
402 if (is_nil_uuid(hapd->conf->uuid)) {
403 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
404 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
405 wps->uuid, UUID_LEN);
407 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
408 wps->ssid_len = hapd->conf->ssid.ssid_len;
409 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
411 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
412 wps->dev.device_name = os_strdup(hapd->conf->device_name);
413 wps->dev.manufacturer = os_strdup(hapd->conf->manufacturer);
414 wps->dev.model_name = os_strdup(hapd->conf->model_name);
415 wps->dev.model_number = os_strdup(hapd->conf->model_number);
416 wps->dev.serial_number = os_strdup(hapd->conf->serial_number);
417 if (hapd->conf->config_methods) {
418 char *m = hapd->conf->config_methods;
419 if (os_strstr(m, "label"))
420 wps->config_methods |= WPS_CONFIG_LABEL;
421 if (os_strstr(m, "display"))
422 wps->config_methods |= WPS_CONFIG_DISPLAY;
423 if (os_strstr(m, "push_button"))
424 wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
425 if (os_strstr(m, "keypad"))
426 wps->config_methods |= WPS_CONFIG_KEYPAD;
428 if (hapd->conf->device_type) {
431 /* <categ>-<OUI>-<subcateg> */
432 wps->dev.categ = atoi(hapd->conf->device_type);
433 pos = os_strchr(hapd->conf->device_type, '-');
435 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
440 if (hexstr2bin(pos, oui, 4)) {
441 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
445 wps->dev.oui = WPA_GET_BE32(oui);
446 pos = os_strchr(pos, '-');
448 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
453 wps->dev.sub_categ = atoi(pos);
455 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
456 wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
457 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
459 if (conf->wpa & WPA_PROTO_RSN) {
460 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
461 wps->auth_types |= WPS_AUTH_WPA2PSK;
462 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
463 wps->auth_types |= WPS_AUTH_WPA2;
465 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
466 wps->encr_types |= WPS_ENCR_AES;
467 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
468 wps->encr_types |= WPS_ENCR_TKIP;
471 if (conf->wpa & WPA_PROTO_WPA) {
472 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
473 wps->auth_types |= WPS_AUTH_WPAPSK;
474 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
475 wps->auth_types |= WPS_AUTH_WPA;
477 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
478 wps->encr_types |= WPS_ENCR_AES;
479 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
480 wps->encr_types |= WPS_ENCR_TKIP;
483 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
484 wps->encr_types |= WPS_ENCR_NONE;
485 wps->auth_types |= WPS_AUTH_OPEN;
486 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
487 wps->encr_types |= WPS_ENCR_WEP;
488 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
489 wps->auth_types |= WPS_AUTH_OPEN;
490 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
491 wps->auth_types |= WPS_AUTH_SHARED;
492 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
493 wps->auth_types |= WPS_AUTH_OPEN;
494 if (conf->default_wep_key_len)
495 wps->encr_types |= WPS_ENCR_WEP;
497 wps->encr_types |= WPS_ENCR_NONE;
500 if (conf->ssid.wpa_psk_file) {
501 /* Use per-device PSKs */
502 } else if (conf->ssid.wpa_passphrase) {
503 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
504 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
505 } else if (conf->ssid.wpa_psk) {
506 wps->network_key = os_malloc(2 * PMK_LEN + 1);
507 if (wps->network_key == NULL) {
511 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
512 conf->ssid.wpa_psk->psk, PMK_LEN);
513 wps->network_key_len = 2 * PMK_LEN;
514 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
515 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
516 if (wps->network_key == NULL) {
520 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
521 conf->ssid.wep.len[0]);
522 wps->network_key_len = conf->ssid.wep.len[0];
525 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
526 /* Override parameters to enable security by default */
527 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
528 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
531 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
532 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
533 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
535 cfg.skip_cred_build = conf->skip_cred_build;
536 cfg.extra_cred = conf->extra_cred;
537 cfg.extra_cred_len = conf->extra_cred_len;
539 wps->registrar = wps_registrar_init(wps, &cfg);
540 if (wps->registrar == NULL) {
541 printf("Failed to initialize WPS Registrar\n");
542 os_free(wps->network_key);
553 void hostapd_deinit_wps(struct hostapd_data *hapd)
555 if (hapd->wps == NULL)
557 wps_registrar_deinit(hapd->wps->registrar);
558 os_free(hapd->wps->network_key);
559 wps_device_data_free(&hapd->wps->dev);
562 hostapd_wps_clear_ies(hapd);
566 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
572 if (hapd->wps == NULL)
574 if (os_strcmp(uuid, "any") == 0)
576 else if (uuid_str2bin(uuid, u))
578 return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
579 (const u8 *) pin, os_strlen(pin));
583 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
585 if (hapd->wps == NULL)
587 return wps_registrar_button_pushed(hapd->wps->registrar);
591 void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
592 const u8 *ie, size_t ie_len)
594 struct wpabuf *wps_ie;
595 const u8 *end, *pos, *wps;
597 if (hapd->wps == NULL)
604 while (pos + 1 < end) {
605 if (pos + 2 + pos[1] > end)
607 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
608 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
616 return; /* No WPS IE in Probe Request */
618 wps_ie = wpabuf_alloc(ie_len);
622 /* There may be multiple WPS IEs in the message, so need to concatenate
623 * their WPS Data fields */
624 while (pos + 1 < end) {
625 if (pos + 2 + pos[1] > end)
627 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
628 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
629 wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
633 if (wpabuf_len(wps_ie) > 0)
634 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);