2 * hostapd / WPS integration
3 * Copyright (c) 2008-2010, 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.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "utils/uuid.h"
20 #include "crypto/dh_groups.h"
21 #include "common/wpa_ctrl.h"
22 #include "common/ieee802_11_defs.h"
23 #include "common/ieee802_11_common.h"
24 #include "eapol_auth/eapol_auth_sm.h"
25 #include "eapol_auth/eapol_auth_sm_i.h"
27 #include "wps/wps_defs.h"
28 #include "wps/wps_dev_attr.h"
30 #include "ap_config.h"
33 #include "wps_hostapd.h"
36 #ifdef CONFIG_WPS_UPNP
37 #include "wps/wps_upnp.h"
38 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
39 struct wps_context *wps);
40 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
41 #endif /* CONFIG_WPS_UPNP */
43 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
44 const u8 *ie, size_t ie_len);
45 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
48 struct wps_for_each_data {
49 int (*func)(struct hostapd_data *h, void *ctx);
54 static int wps_for_each(struct hostapd_iface *iface, void *ctx)
56 struct wps_for_each_data *data = ctx;
61 for (j = 0; j < iface->num_bss; j++) {
62 struct hostapd_data *hapd = iface->bss[j];
63 int ret = data->func(hapd, data->ctx);
72 static int hostapd_wps_for_each(struct hostapd_data *hapd,
73 int (*func)(struct hostapd_data *h, void *ctx),
76 struct hostapd_iface *iface = hapd->iface;
77 struct wps_for_each_data data;
80 if (iface->for_each_interface == NULL)
81 return wps_for_each(iface, &data);
82 return iface->for_each_interface(iface->interfaces, wps_for_each,
87 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
90 struct hostapd_data *hapd = ctx;
91 struct hostapd_wpa_psk *p;
92 struct hostapd_ssid *ssid = &hapd->conf->ssid;
94 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
95 MACSTR, MAC2STR(mac_addr));
96 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
98 if (psk_len != PMK_LEN) {
99 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
100 (unsigned long) psk_len);
104 /* Add the new PSK to runtime PSK list */
105 p = os_zalloc(sizeof(*p));
108 os_memcpy(p->addr, mac_addr, ETH_ALEN);
109 os_memcpy(p->psk, psk, PMK_LEN);
111 p->next = ssid->wpa_psk;
114 if (ssid->wpa_psk_file) {
116 char hex[PMK_LEN * 2 + 1];
117 /* Add the new PSK to PSK list file */
118 f = fopen(ssid->wpa_psk_file, "a");
120 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
121 "'%s'", ssid->wpa_psk_file);
125 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
126 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
134 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
135 struct wpabuf *probe_resp_ie)
137 struct hostapd_data *hapd = ctx;
138 wpabuf_free(hapd->wps_beacon_ie);
139 hapd->wps_beacon_ie = beacon_ie;
140 wpabuf_free(hapd->wps_probe_resp_ie);
141 hapd->wps_probe_resp_ie = probe_resp_ie;
142 ieee802_11_set_beacon(hapd);
143 return hapd->drv.set_ap_wps_ie(hapd);
147 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
148 const struct wps_device_data *dev)
150 struct hostapd_data *hapd = ctx;
151 char uuid[40], txt[400];
153 char devtype[WPS_DEV_TYPE_BUFSIZE];
154 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
156 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
157 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
158 "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
159 uuid, MAC2STR(dev->mac_addr), dev->device_name,
160 dev->manufacturer, dev->model_name,
161 dev->model_number, dev->serial_number,
162 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
164 if (len > 0 && len < (int) sizeof(txt))
165 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
167 if (hapd->conf->wps_pin_requests) {
170 f = fopen(hapd->conf->wps_pin_requests, "a");
174 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
176 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
177 dev->manufacturer, dev->model_name, dev->model_number,
179 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
186 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
189 struct hostapd_data *hapd = ctx;
191 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
193 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
194 MAC2STR(mac_addr), uuid);
195 if (hapd->wps_reg_success_cb)
196 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
201 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
203 const u8 *pri_dev_type,
205 u16 dev_password_id, u8 request_type,
206 const char *dev_name)
208 struct hostapd_data *hapd = ctx;
210 char devtype[WPS_DEV_TYPE_BUFSIZE];
211 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
213 if (dev_name == NULL)
215 wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
216 " %s %s 0x%x %u %u [%s]",
218 wps_dev_type_bin2str(pri_dev_type, devtype,
220 config_methods, dev_password_id, request_type, dev_name);
224 static int str_starts(const char *str, const char *start)
226 return os_strncmp(str, start, os_strlen(start)) == 0;
230 static void wps_reload_config(void *eloop_data, void *user_ctx)
232 struct hostapd_iface *iface = eloop_data;
234 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
235 if (iface->reload_config(iface) < 0) {
236 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
242 static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
244 const struct wps_credential *cred = ctx;
252 if (hapd->wps == NULL)
255 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
256 cred->cred_attr, cred->cred_attr_len);
258 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
259 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
260 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
262 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
263 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
264 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
265 cred->key, cred->key_len);
266 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
267 MAC2STR(cred->mac_addr));
269 if ((hapd->conf->wps_cred_processing == 1 ||
270 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
271 size_t blen = cred->cred_attr_len * 2 + 1;
272 char *_buf = os_malloc(blen);
274 wpa_snprintf_hex(_buf, blen,
275 cred->cred_attr, cred->cred_attr_len);
276 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
277 WPS_EVENT_NEW_AP_SETTINGS, _buf);
281 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
283 if (hapd->conf->wps_cred_processing == 1)
286 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
287 hapd->wps->ssid_len = cred->ssid_len;
288 hapd->wps->encr_types = cred->encr_type;
289 hapd->wps->auth_types = cred->auth_type;
290 if (cred->key_len == 0) {
291 os_free(hapd->wps->network_key);
292 hapd->wps->network_key = NULL;
293 hapd->wps->network_key_len = 0;
295 if (hapd->wps->network_key == NULL ||
296 hapd->wps->network_key_len < cred->key_len) {
297 hapd->wps->network_key_len = 0;
298 os_free(hapd->wps->network_key);
299 hapd->wps->network_key = os_malloc(cred->key_len);
300 if (hapd->wps->network_key == NULL)
303 hapd->wps->network_key_len = cred->key_len;
304 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
306 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
308 len = os_strlen(hapd->iface->config_fname) + 5;
309 tmp_fname = os_malloc(len);
310 if (tmp_fname == NULL)
312 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
314 oconf = fopen(hapd->iface->config_fname, "r");
316 wpa_printf(MSG_WARNING, "WPS: Could not open current "
317 "configuration file");
322 nconf = fopen(tmp_fname, "w");
324 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
325 "configuration file");
331 fprintf(nconf, "# WPS configuration - START\n");
333 fprintf(nconf, "wps_state=2\n");
335 fprintf(nconf, "ssid=");
336 for (i = 0; i < cred->ssid_len; i++)
337 fputc(cred->ssid[i], nconf);
338 fprintf(nconf, "\n");
340 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
341 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
343 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
345 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
352 fprintf(nconf, "wpa=%d\n", wpa);
354 fprintf(nconf, "wpa_key_mgmt=");
356 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
357 fprintf(nconf, "WPA-EAP");
360 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
361 fprintf(nconf, "%sWPA-PSK", prefix);
362 fprintf(nconf, "\n");
364 fprintf(nconf, "wpa_pairwise=");
366 if (cred->encr_type & WPS_ENCR_AES) {
367 fprintf(nconf, "CCMP");
370 if (cred->encr_type & WPS_ENCR_TKIP) {
371 fprintf(nconf, "%sTKIP", prefix);
373 fprintf(nconf, "\n");
375 if (cred->key_len >= 8 && cred->key_len < 64) {
376 fprintf(nconf, "wpa_passphrase=");
377 for (i = 0; i < cred->key_len; i++)
378 fputc(cred->key[i], nconf);
379 fprintf(nconf, "\n");
380 } else if (cred->key_len == 64) {
381 fprintf(nconf, "wpa_psk=");
382 for (i = 0; i < cred->key_len; i++)
383 fputc(cred->key[i], nconf);
384 fprintf(nconf, "\n");
386 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
388 (unsigned long) cred->key_len);
391 fprintf(nconf, "auth_algs=1\n");
393 if ((cred->auth_type & WPS_AUTH_OPEN) &&
394 (cred->auth_type & WPS_AUTH_SHARED))
395 fprintf(nconf, "auth_algs=3\n");
396 else if (cred->auth_type & WPS_AUTH_SHARED)
397 fprintf(nconf, "auth_algs=2\n");
399 fprintf(nconf, "auth_algs=1\n");
401 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
402 int key_idx = cred->key_idx;
405 fprintf(nconf, "wep_default_key=%d\n", key_idx);
406 fprintf(nconf, "wep_key%d=", key_idx);
407 if (cred->key_len == 10 || cred->key_len == 26) {
408 /* WEP key as a hex string */
409 for (i = 0; i < cred->key_len; i++)
410 fputc(cred->key[i], nconf);
412 /* Raw WEP key; convert to hex */
413 for (i = 0; i < cred->key_len; i++)
414 fprintf(nconf, "%02x", cred->key[i]);
416 fprintf(nconf, "\n");
420 fprintf(nconf, "# WPS configuration - END\n");
423 while (fgets(buf, sizeof(buf), oconf)) {
424 if (os_strncmp(buf, "bss=", 4) == 0)
427 (str_starts(buf, "ssid=") ||
428 str_starts(buf, "auth_algs=") ||
429 str_starts(buf, "wps_state=") ||
430 str_starts(buf, "wpa=") ||
431 str_starts(buf, "wpa_psk=") ||
432 str_starts(buf, "wpa_pairwise=") ||
433 str_starts(buf, "rsn_pairwise=") ||
434 str_starts(buf, "wpa_key_mgmt=") ||
435 str_starts(buf, "wpa_passphrase="))) {
436 fprintf(nconf, "#WPS# %s", buf);
438 fprintf(nconf, "%s", buf);
444 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
445 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
446 "configuration file: %s", strerror(errno));
453 /* Schedule configuration reload after short period of time to allow
454 * EAP-WSC to be finished.
456 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
459 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
465 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
467 struct hostapd_data *hapd = ctx;
468 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
472 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
474 struct hostapd_data *hapd = eloop_data;
476 if (hapd->conf->ap_setup_locked)
479 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
480 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
481 hapd->wps->ap_setup_locked = 0;
482 wps_registrar_update_ie(hapd->wps->registrar);
486 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
488 struct wps_event_pwd_auth_fail *data = ctx;
490 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
494 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
495 * for some time if this happens multiple times to slow down brute
498 hapd->ap_pin_failures++;
499 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
500 hapd->ap_pin_failures);
501 if (hapd->ap_pin_failures < 3)
504 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
505 hapd->wps->ap_setup_locked = 1;
507 wps_registrar_update_ie(hapd->wps->registrar);
509 if (!hapd->conf->ap_setup_locked) {
510 if (hapd->ap_pin_lockout_time == 0)
511 hapd->ap_pin_lockout_time = 60;
512 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
513 (hapd->ap_pin_failures % 3) == 0)
514 hapd->ap_pin_lockout_time *= 2;
516 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
517 hapd->ap_pin_lockout_time);
518 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
519 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
520 hostapd_wps_reenable_ap_pin, hapd,
528 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
529 struct wps_event_pwd_auth_fail *data)
531 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
535 static void hostapd_wps_event_fail(struct hostapd_data *hapd,
536 struct wps_event_fail *fail)
538 wpa_msg(hapd->msg_ctx, MSG_INFO,
539 WPS_EVENT_FAIL "msg=%d config_error=%d",
540 fail->msg, fail->config_error);
544 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
545 union wps_event_data *data)
547 struct hostapd_data *hapd = ctx;
553 hostapd_wps_event_fail(hapd, &data->fail);
557 case WPS_EV_PWD_AUTH_FAIL:
558 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
560 case WPS_EV_PBC_OVERLAP:
562 case WPS_EV_PBC_TIMEOUT:
564 case WPS_EV_ER_AP_ADD:
566 case WPS_EV_ER_AP_REMOVE:
568 case WPS_EV_ER_ENROLLEE_ADD:
570 case WPS_EV_ER_ENROLLEE_REMOVE:
572 case WPS_EV_ER_AP_SETTINGS:
578 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
580 wpabuf_free(hapd->wps_beacon_ie);
581 hapd->wps_beacon_ie = NULL;
583 wpabuf_free(hapd->wps_probe_resp_ie);
584 hapd->wps_probe_resp_ie = NULL;
586 hapd->drv.set_ap_wps_ie(hapd);
590 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
592 const u8 **uuid = ctx;
597 for (j = 0; j < iface->num_bss; j++) {
598 struct hostapd_data *hapd = iface->bss[j];
599 if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) {
600 *uuid = hapd->wps->uuid;
609 static const u8 * get_own_uuid(struct hostapd_iface *iface)
612 if (iface->for_each_interface == NULL)
615 iface->for_each_interface(iface->interfaces, get_uuid_cb, &uuid);
620 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
628 static int interface_count(struct hostapd_iface *iface)
631 if (iface->for_each_interface == NULL)
633 iface->for_each_interface(iface->interfaces, count_interface_cb,
639 int hostapd_init_wps(struct hostapd_data *hapd,
640 struct hostapd_bss_config *conf)
642 struct wps_context *wps;
643 struct wps_registrar_config cfg;
645 if (conf->wps_state == 0) {
646 hostapd_wps_clear_ies(hapd);
650 wps = os_zalloc(sizeof(*wps));
654 wps->cred_cb = hostapd_wps_cred_cb;
655 wps->event_cb = hostapd_wps_event_cb;
658 os_memset(&cfg, 0, sizeof(cfg));
659 wps->wps_state = hapd->conf->wps_state;
660 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
661 if (is_nil_uuid(hapd->conf->uuid)) {
663 uuid = get_own_uuid(hapd->iface);
665 os_memcpy(wps->uuid, uuid, UUID_LEN);
666 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
667 "interface", wps->uuid, UUID_LEN);
669 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
670 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
671 "address", wps->uuid, UUID_LEN);
674 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
675 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
676 wps->uuid, UUID_LEN);
678 wps->ssid_len = hapd->conf->ssid.ssid_len;
679 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
681 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
682 wps->dev.device_name = hapd->conf->device_name ?
683 os_strdup(hapd->conf->device_name) : NULL;
684 wps->dev.manufacturer = hapd->conf->manufacturer ?
685 os_strdup(hapd->conf->manufacturer) : NULL;
686 wps->dev.model_name = hapd->conf->model_name ?
687 os_strdup(hapd->conf->model_name) : NULL;
688 wps->dev.model_number = hapd->conf->model_number ?
689 os_strdup(hapd->conf->model_number) : NULL;
690 wps->dev.serial_number = hapd->conf->serial_number ?
691 os_strdup(hapd->conf->serial_number) : NULL;
692 wps->config_methods =
693 wps_config_methods_str2bin(hapd->conf->config_methods);
695 if ((wps->config_methods &
696 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
697 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
698 wpa_printf(MSG_INFO, "WPS: Converting display to "
699 "virtual_display for WPS 2.0 compliance");
700 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
702 if ((wps->config_methods &
703 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
704 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
705 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
706 "virtual_push_button for WPS 2.0 compliance");
707 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
709 #endif /* CONFIG_WPS2 */
710 if (hapd->conf->device_type &&
711 wps_dev_type_str2bin(hapd->conf->device_type,
712 wps->dev.pri_dev_type) < 0) {
713 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
717 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
718 wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
719 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
721 if (conf->wpa & WPA_PROTO_RSN) {
722 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
723 wps->auth_types |= WPS_AUTH_WPA2PSK;
724 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
725 wps->auth_types |= WPS_AUTH_WPA2;
727 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
728 wps->encr_types |= WPS_ENCR_AES;
729 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
730 wps->encr_types |= WPS_ENCR_TKIP;
733 if (conf->wpa & WPA_PROTO_WPA) {
734 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
735 wps->auth_types |= WPS_AUTH_WPAPSK;
736 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
737 wps->auth_types |= WPS_AUTH_WPA;
739 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
740 wps->encr_types |= WPS_ENCR_AES;
741 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
742 wps->encr_types |= WPS_ENCR_TKIP;
745 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
746 wps->encr_types |= WPS_ENCR_NONE;
747 wps->auth_types |= WPS_AUTH_OPEN;
748 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
749 wps->encr_types |= WPS_ENCR_WEP;
750 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
751 wps->auth_types |= WPS_AUTH_OPEN;
752 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
753 wps->auth_types |= WPS_AUTH_SHARED;
754 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
755 wps->auth_types |= WPS_AUTH_OPEN;
756 if (conf->default_wep_key_len)
757 wps->encr_types |= WPS_ENCR_WEP;
759 wps->encr_types |= WPS_ENCR_NONE;
762 if (conf->ssid.wpa_psk_file) {
763 /* Use per-device PSKs */
764 } else if (conf->ssid.wpa_passphrase) {
765 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
766 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
767 } else if (conf->ssid.wpa_psk) {
768 wps->network_key = os_malloc(2 * PMK_LEN + 1);
769 if (wps->network_key == NULL) {
773 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
774 conf->ssid.wpa_psk->psk, PMK_LEN);
775 wps->network_key_len = 2 * PMK_LEN;
776 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
777 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
778 if (wps->network_key == NULL) {
782 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
783 conf->ssid.wep.len[0]);
784 wps->network_key_len = conf->ssid.wep.len[0];
787 if (conf->ssid.wpa_psk) {
788 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
792 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
793 /* Override parameters to enable security by default */
794 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
795 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
798 wps->ap_settings = conf->ap_settings;
799 wps->ap_settings_len = conf->ap_settings_len;
801 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
802 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
803 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
804 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
805 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
807 cfg.skip_cred_build = conf->skip_cred_build;
808 cfg.extra_cred = conf->extra_cred;
809 cfg.extra_cred_len = conf->extra_cred_len;
810 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
811 conf->skip_cred_build;
812 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
813 cfg.static_wep_only = 1;
814 cfg.dualband = interface_count(hapd->iface) > 1;
816 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
818 wps->registrar = wps_registrar_init(wps, &cfg);
819 if (wps->registrar == NULL) {
820 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
821 os_free(wps->network_key);
826 #ifdef CONFIG_WPS_UPNP
827 wps->friendly_name = hapd->conf->friendly_name;
828 wps->manufacturer_url = hapd->conf->manufacturer_url;
829 wps->model_description = hapd->conf->model_description;
830 wps->model_url = hapd->conf->model_url;
831 wps->upc = hapd->conf->upc;
833 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
834 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
835 wps_registrar_deinit(wps->registrar);
836 os_free(wps->network_key);
840 #endif /* CONFIG_WPS_UPNP */
842 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
850 void hostapd_deinit_wps(struct hostapd_data *hapd)
852 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
853 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
854 if (hapd->wps == NULL)
856 #ifdef CONFIG_WPS_UPNP
857 hostapd_wps_upnp_deinit(hapd);
858 #endif /* CONFIG_WPS_UPNP */
859 wps_registrar_deinit(hapd->wps->registrar);
860 os_free(hapd->wps->network_key);
861 wps_device_data_free(&hapd->wps->dev);
862 wpabuf_free(hapd->wps->dh_pubkey);
863 wpabuf_free(hapd->wps->dh_privkey);
864 wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
865 wpabuf_free(hapd->wps->oob_conf.dev_password);
866 wps_free_pending_msgs(hapd->wps->upnp_msgs);
869 hostapd_wps_clear_ies(hapd);
873 void hostapd_update_wps(struct hostapd_data *hapd)
875 if (hapd->wps == NULL)
878 #ifdef CONFIG_WPS_UPNP
879 hapd->wps->friendly_name = hapd->conf->friendly_name;
880 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
881 hapd->wps->model_description = hapd->conf->model_description;
882 hapd->wps->model_url = hapd->conf->model_url;
883 hapd->wps->upc = hapd->conf->upc;
884 #endif /* CONFIG_WPS_UPNP */
886 if (hapd->conf->wps_state)
887 wps_registrar_update_ie(hapd->wps->registrar);
889 hostapd_deinit_wps(hapd);
893 struct wps_add_pin_data {
903 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
905 struct wps_add_pin_data *data = ctx;
908 if (hapd->wps == NULL)
910 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
911 data->uuid, data->pin, data->pin_len,
919 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
920 const char *uuid, const char *pin, int timeout)
923 struct wps_add_pin_data data;
927 data.pin = (const u8 *) pin;
928 data.pin_len = os_strlen(pin);
929 data.timeout = timeout;
932 if (os_strcmp(uuid, "any") == 0)
935 if (uuid_str2bin(uuid, u))
939 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
941 return data.added ? 0 : -1;
945 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
947 if (hapd->wps == NULL)
949 return wps_registrar_button_pushed(hapd->wps->registrar);
953 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
955 return hostapd_wps_for_each(hapd, wps_button_pushed, NULL);
959 #ifdef CONFIG_WPS_OOB
960 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
961 char *path, char *method, char *name)
963 struct wps_context *wps = hapd->wps;
964 struct oob_device_data *oob_dev;
966 oob_dev = wps_get_oob_device(device_type);
969 oob_dev->device_path = path;
970 oob_dev->device_name = name;
971 wps->oob_conf.oob_method = wps_get_oob_method(method);
973 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
975 * Use pre-configured DH keys in order to be able to write the
976 * key hash into the OOB file.
978 wpabuf_free(wps->dh_pubkey);
979 wpabuf_free(wps->dh_privkey);
980 wps->dh_privkey = NULL;
981 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
983 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
984 if (wps->dh_pubkey == NULL) {
985 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
986 "Diffie-Hellman handshake");
991 if (wps_process_oob(wps, oob_dev, 1) < 0)
994 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
995 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
996 hostapd_wps_add_pin(hapd, NULL, "any",
997 wpabuf_head(wps->oob_conf.dev_password), 0) <
1004 wpabuf_free(wps->dh_pubkey);
1005 wps->dh_pubkey = NULL;
1006 wpabuf_free(wps->dh_privkey);
1007 wps->dh_privkey = NULL;
1010 #endif /* CONFIG_WPS_OOB */
1013 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
1014 const u8 *ie, size_t ie_len)
1016 struct hostapd_data *hapd = ctx;
1017 struct wpabuf *wps_ie;
1018 struct ieee802_11_elems elems;
1020 if (hapd->wps == NULL)
1023 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1024 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1025 MACSTR, MAC2STR(addr));
1029 if (elems.ssid && elems.ssid_len > 0 &&
1030 (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1031 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1033 return 0; /* Not for us */
1035 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1038 if (wps_validate_probe_req(wps_ie, addr) < 0) {
1039 wpabuf_free(wps_ie);
1043 if (wpabuf_len(wps_ie) > 0) {
1044 int p2p_wildcard = 0;
1046 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1047 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1048 P2P_WILDCARD_SSID_LEN) == 0)
1050 #endif /* CONFIG_P2P */
1051 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1053 #ifdef CONFIG_WPS_UPNP
1054 /* FIX: what exactly should be included in the WLANEvent?
1055 * WPS attributes? Full ProbeReq frame? */
1057 upnp_wps_device_send_wlan_event(
1058 hapd->wps_upnp, addr,
1059 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1060 #endif /* CONFIG_WPS_UPNP */
1063 wpabuf_free(wps_ie);
1069 #ifdef CONFIG_WPS_UPNP
1071 static int hostapd_rx_req_put_wlan_response(
1072 void *priv, enum upnp_wps_wlanevent_type ev_type,
1073 const u8 *mac_addr, const struct wpabuf *msg,
1074 enum wps_msg_type msg_type)
1076 struct hostapd_data *hapd = priv;
1077 struct sta_info *sta;
1078 struct upnp_pending_message *p;
1080 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1081 MACSTR, ev_type, MAC2STR(mac_addr));
1082 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1083 wpabuf_head(msg), wpabuf_len(msg));
1084 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1085 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1086 "PutWLANResponse WLANEventType %d", ev_type);
1091 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1092 * server implementation for delivery to the peer.
1095 sta = ap_get_sta(hapd, mac_addr);
1096 #ifndef CONFIG_WPS_STRICT
1099 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1100 * Pick STA that is in an ongoing WPS registration without
1101 * checking the MAC address.
1103 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1104 "on NewWLANEventMAC; try wildcard match");
1105 for (sta = hapd->sta_list; sta; sta = sta->next) {
1106 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1110 #endif /* CONFIG_WPS_STRICT */
1113 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1117 p = os_zalloc(sizeof(*p));
1120 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1121 p->msg = wpabuf_dup(msg);
1123 p->next = hapd->wps->upnp_msgs;
1124 hapd->wps->upnp_msgs = p;
1126 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1130 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1131 struct wps_context *wps)
1133 struct upnp_wps_device_ctx *ctx;
1135 if (!hapd->conf->upnp_iface)
1137 ctx = os_zalloc(sizeof(*ctx));
1141 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1142 if (hapd->conf->ap_pin)
1143 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1145 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
1146 if (hapd->wps_upnp == NULL) {
1150 wps->wps_upnp = hapd->wps_upnp;
1152 if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
1153 upnp_wps_device_deinit(hapd->wps_upnp);
1154 hapd->wps_upnp = NULL;
1162 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1164 upnp_wps_device_deinit(hapd->wps_upnp);
1167 #endif /* CONFIG_WPS_UPNP */
1170 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1171 char *buf, size_t buflen)
1173 if (hapd->wps == NULL)
1175 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1179 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1181 struct hostapd_data *hapd = eloop_data;
1182 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1183 hostapd_wps_ap_pin_disable(hapd);
1187 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1189 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1190 hapd->ap_pin_failures = 0;
1191 hapd->conf->ap_setup_locked = 0;
1192 if (hapd->wps->ap_setup_locked) {
1193 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1194 hapd->wps->ap_setup_locked = 0;
1195 wps_registrar_update_ie(hapd->wps->registrar);
1197 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1199 eloop_register_timeout(timeout, 0,
1200 hostapd_wps_ap_pin_timeout, hapd, NULL);
1204 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1206 os_free(hapd->conf->ap_pin);
1207 hapd->conf->ap_pin = NULL;
1208 #ifdef CONFIG_WPS_UPNP
1209 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1210 #endif /* CONFIG_WPS_UPNP */
1211 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1216 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1218 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1219 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1223 struct wps_ap_pin_data {
1229 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1231 struct wps_ap_pin_data *data = ctx;
1232 os_free(hapd->conf->ap_pin);
1233 hapd->conf->ap_pin = os_strdup(data->pin_txt);
1234 #ifdef CONFIG_WPS_UPNP
1235 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1236 #endif /* CONFIG_WPS_UPNP */
1237 hostapd_wps_ap_pin_enable(hapd, data->timeout);
1242 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1245 struct wps_ap_pin_data data;
1247 pin = wps_generate_pin();
1248 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%u", pin);
1249 data.timeout = timeout;
1250 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1251 return hapd->conf->ap_pin;
1255 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1257 return hapd->conf->ap_pin;
1261 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1264 struct wps_ap_pin_data data;
1267 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1268 if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1270 data.timeout = timeout;
1271 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);