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"
31 #include "ap_drv_ops.h"
34 #include "wps_hostapd.h"
37 #ifdef CONFIG_WPS_UPNP
38 #include "wps/wps_upnp.h"
39 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
40 struct wps_context *wps);
41 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
42 #endif /* CONFIG_WPS_UPNP */
44 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
46 const u8 *ie, size_t ie_len);
47 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
50 struct wps_for_each_data {
51 int (*func)(struct hostapd_data *h, void *ctx);
56 static int wps_for_each(struct hostapd_iface *iface, void *ctx)
58 struct wps_for_each_data *data = ctx;
63 for (j = 0; j < iface->num_bss; j++) {
64 struct hostapd_data *hapd = iface->bss[j];
65 int ret = data->func(hapd, data->ctx);
74 static int hostapd_wps_for_each(struct hostapd_data *hapd,
75 int (*func)(struct hostapd_data *h, void *ctx),
78 struct hostapd_iface *iface = hapd->iface;
79 struct wps_for_each_data data;
82 if (iface->for_each_interface == NULL)
83 return wps_for_each(iface, &data);
84 return iface->for_each_interface(iface->interfaces, wps_for_each,
89 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
92 struct hostapd_data *hapd = ctx;
93 struct hostapd_wpa_psk *p;
94 struct hostapd_ssid *ssid = &hapd->conf->ssid;
96 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
97 MACSTR, MAC2STR(mac_addr));
98 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
100 if (psk_len != PMK_LEN) {
101 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
102 (unsigned long) psk_len);
106 /* Add the new PSK to runtime PSK list */
107 p = os_zalloc(sizeof(*p));
110 os_memcpy(p->addr, mac_addr, ETH_ALEN);
111 os_memcpy(p->psk, psk, PMK_LEN);
113 p->next = ssid->wpa_psk;
116 if (ssid->wpa_psk_file) {
118 char hex[PMK_LEN * 2 + 1];
119 /* Add the new PSK to PSK list file */
120 f = fopen(ssid->wpa_psk_file, "a");
122 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
123 "'%s'", ssid->wpa_psk_file);
127 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
128 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
136 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
137 struct wpabuf *probe_resp_ie)
139 struct hostapd_data *hapd = ctx;
140 wpabuf_free(hapd->wps_beacon_ie);
141 hapd->wps_beacon_ie = beacon_ie;
142 wpabuf_free(hapd->wps_probe_resp_ie);
143 hapd->wps_probe_resp_ie = probe_resp_ie;
144 ieee802_11_set_beacon(hapd);
145 return hostapd_set_ap_wps_ie(hapd);
149 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
150 const struct wps_device_data *dev)
152 struct hostapd_data *hapd = ctx;
153 char uuid[40], txt[400];
155 char devtype[WPS_DEV_TYPE_BUFSIZE];
156 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
158 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
159 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
160 "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
161 uuid, MAC2STR(dev->mac_addr), dev->device_name,
162 dev->manufacturer, dev->model_name,
163 dev->model_number, dev->serial_number,
164 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
166 if (len > 0 && len < (int) sizeof(txt))
167 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
169 if (hapd->conf->wps_pin_requests) {
172 f = fopen(hapd->conf->wps_pin_requests, "a");
176 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
178 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
179 dev->manufacturer, dev->model_name, dev->model_number,
181 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
188 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
191 struct hostapd_data *hapd = ctx;
193 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
195 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
196 MAC2STR(mac_addr), uuid);
197 if (hapd->wps_reg_success_cb)
198 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
203 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
205 const u8 *pri_dev_type,
207 u16 dev_password_id, u8 request_type,
208 const char *dev_name)
210 struct hostapd_data *hapd = ctx;
212 char devtype[WPS_DEV_TYPE_BUFSIZE];
213 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
215 if (dev_name == NULL)
217 wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
218 " %s %s 0x%x %u %u [%s]",
220 wps_dev_type_bin2str(pri_dev_type, devtype,
222 config_methods, dev_password_id, request_type, dev_name);
226 static int str_starts(const char *str, const char *start)
228 return os_strncmp(str, start, os_strlen(start)) == 0;
232 static void wps_reload_config(void *eloop_data, void *user_ctx)
234 struct hostapd_iface *iface = eloop_data;
236 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
237 if (iface->reload_config(iface) < 0) {
238 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
244 static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
246 const struct wps_credential *cred = ctx;
254 if (hapd->wps == NULL)
257 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
258 cred->cred_attr, cred->cred_attr_len);
260 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
261 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
262 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
264 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
265 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
266 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
267 cred->key, cred->key_len);
268 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
269 MAC2STR(cred->mac_addr));
271 if ((hapd->conf->wps_cred_processing == 1 ||
272 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
273 size_t blen = cred->cred_attr_len * 2 + 1;
274 char *_buf = os_malloc(blen);
276 wpa_snprintf_hex(_buf, blen,
277 cred->cred_attr, cred->cred_attr_len);
278 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
279 WPS_EVENT_NEW_AP_SETTINGS, _buf);
283 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
285 if (hapd->conf->wps_cred_processing == 1)
288 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
289 hapd->wps->ssid_len = cred->ssid_len;
290 hapd->wps->encr_types = cred->encr_type;
291 hapd->wps->auth_types = cred->auth_type;
292 if (cred->key_len == 0) {
293 os_free(hapd->wps->network_key);
294 hapd->wps->network_key = NULL;
295 hapd->wps->network_key_len = 0;
297 if (hapd->wps->network_key == NULL ||
298 hapd->wps->network_key_len < cred->key_len) {
299 hapd->wps->network_key_len = 0;
300 os_free(hapd->wps->network_key);
301 hapd->wps->network_key = os_malloc(cred->key_len);
302 if (hapd->wps->network_key == NULL)
305 hapd->wps->network_key_len = cred->key_len;
306 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
308 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
310 len = os_strlen(hapd->iface->config_fname) + 5;
311 tmp_fname = os_malloc(len);
312 if (tmp_fname == NULL)
314 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
316 oconf = fopen(hapd->iface->config_fname, "r");
318 wpa_printf(MSG_WARNING, "WPS: Could not open current "
319 "configuration file");
324 nconf = fopen(tmp_fname, "w");
326 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
327 "configuration file");
333 fprintf(nconf, "# WPS configuration - START\n");
335 fprintf(nconf, "wps_state=2\n");
337 fprintf(nconf, "ssid=");
338 for (i = 0; i < cred->ssid_len; i++)
339 fputc(cred->ssid[i], nconf);
340 fprintf(nconf, "\n");
342 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
343 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
345 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
347 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
354 fprintf(nconf, "wpa=%d\n", wpa);
356 fprintf(nconf, "wpa_key_mgmt=");
358 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
359 fprintf(nconf, "WPA-EAP");
362 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
363 fprintf(nconf, "%sWPA-PSK", prefix);
364 fprintf(nconf, "\n");
366 fprintf(nconf, "wpa_pairwise=");
368 if (cred->encr_type & WPS_ENCR_AES) {
369 fprintf(nconf, "CCMP");
372 if (cred->encr_type & WPS_ENCR_TKIP) {
373 fprintf(nconf, "%sTKIP", prefix);
375 fprintf(nconf, "\n");
377 if (cred->key_len >= 8 && cred->key_len < 64) {
378 fprintf(nconf, "wpa_passphrase=");
379 for (i = 0; i < cred->key_len; i++)
380 fputc(cred->key[i], nconf);
381 fprintf(nconf, "\n");
382 } else if (cred->key_len == 64) {
383 fprintf(nconf, "wpa_psk=");
384 for (i = 0; i < cred->key_len; i++)
385 fputc(cred->key[i], nconf);
386 fprintf(nconf, "\n");
388 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
390 (unsigned long) cred->key_len);
393 fprintf(nconf, "auth_algs=1\n");
395 if ((cred->auth_type & WPS_AUTH_OPEN) &&
396 (cred->auth_type & WPS_AUTH_SHARED))
397 fprintf(nconf, "auth_algs=3\n");
398 else if (cred->auth_type & WPS_AUTH_SHARED)
399 fprintf(nconf, "auth_algs=2\n");
401 fprintf(nconf, "auth_algs=1\n");
403 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
404 int key_idx = cred->key_idx;
407 fprintf(nconf, "wep_default_key=%d\n", key_idx);
408 fprintf(nconf, "wep_key%d=", key_idx);
409 if (cred->key_len == 10 || cred->key_len == 26) {
410 /* WEP key as a hex string */
411 for (i = 0; i < cred->key_len; i++)
412 fputc(cred->key[i], nconf);
414 /* Raw WEP key; convert to hex */
415 for (i = 0; i < cred->key_len; i++)
416 fprintf(nconf, "%02x", cred->key[i]);
418 fprintf(nconf, "\n");
422 fprintf(nconf, "# WPS configuration - END\n");
425 while (fgets(buf, sizeof(buf), oconf)) {
426 if (os_strncmp(buf, "bss=", 4) == 0)
429 (str_starts(buf, "ssid=") ||
430 str_starts(buf, "auth_algs=") ||
431 str_starts(buf, "wep_default_key=") ||
432 str_starts(buf, "wep_key") ||
433 str_starts(buf, "wps_state=") ||
434 str_starts(buf, "wpa=") ||
435 str_starts(buf, "wpa_psk=") ||
436 str_starts(buf, "wpa_pairwise=") ||
437 str_starts(buf, "rsn_pairwise=") ||
438 str_starts(buf, "wpa_key_mgmt=") ||
439 str_starts(buf, "wpa_passphrase="))) {
440 fprintf(nconf, "#WPS# %s", buf);
442 fprintf(nconf, "%s", buf);
448 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
449 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
450 "configuration file: %s", strerror(errno));
457 /* Schedule configuration reload after short period of time to allow
458 * EAP-WSC to be finished.
460 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
463 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
469 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
471 struct hostapd_data *hapd = ctx;
472 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
476 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
478 struct hostapd_data *hapd = eloop_data;
480 if (hapd->conf->ap_setup_locked)
483 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
484 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
485 hapd->wps->ap_setup_locked = 0;
486 wps_registrar_update_ie(hapd->wps->registrar);
490 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
492 struct wps_event_pwd_auth_fail *data = ctx;
494 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
498 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
499 * for some time if this happens multiple times to slow down brute
502 hapd->ap_pin_failures++;
503 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
504 hapd->ap_pin_failures);
505 if (hapd->ap_pin_failures < 3)
508 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
509 hapd->wps->ap_setup_locked = 1;
511 wps_registrar_update_ie(hapd->wps->registrar);
513 if (!hapd->conf->ap_setup_locked) {
514 if (hapd->ap_pin_lockout_time == 0)
515 hapd->ap_pin_lockout_time = 60;
516 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
517 (hapd->ap_pin_failures % 3) == 0)
518 hapd->ap_pin_lockout_time *= 2;
520 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
521 hapd->ap_pin_lockout_time);
522 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
523 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
524 hostapd_wps_reenable_ap_pin, hapd,
532 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
533 struct wps_event_pwd_auth_fail *data)
535 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
539 static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = {
540 "No Error", /* WPS_EI_NO_ERROR */
541 "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */
542 "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */
545 static void hostapd_wps_event_fail(struct hostapd_data *hapd,
546 struct wps_event_fail *fail)
548 if (fail->error_indication > 0 &&
549 fail->error_indication < NUM_WPS_EI_VALUES) {
550 wpa_msg(hapd->msg_ctx, MSG_INFO,
551 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
552 fail->msg, fail->config_error, fail->error_indication,
553 wps_event_fail_reason[fail->error_indication]);
555 wpa_msg(hapd->msg_ctx, MSG_INFO,
556 WPS_EVENT_FAIL "msg=%d config_error=%d",
557 fail->msg, fail->config_error);
562 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
563 union wps_event_data *data)
565 struct hostapd_data *hapd = ctx;
571 hostapd_wps_event_fail(hapd, &data->fail);
575 case WPS_EV_PWD_AUTH_FAIL:
576 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
578 case WPS_EV_PBC_OVERLAP:
580 case WPS_EV_PBC_TIMEOUT:
582 case WPS_EV_ER_AP_ADD:
584 case WPS_EV_ER_AP_REMOVE:
586 case WPS_EV_ER_ENROLLEE_ADD:
588 case WPS_EV_ER_ENROLLEE_REMOVE:
590 case WPS_EV_ER_AP_SETTINGS:
592 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
595 if (hapd->wps_event_cb)
596 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
600 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
602 wpabuf_free(hapd->wps_beacon_ie);
603 hapd->wps_beacon_ie = NULL;
605 wpabuf_free(hapd->wps_probe_resp_ie);
606 hapd->wps_probe_resp_ie = NULL;
608 hostapd_set_ap_wps_ie(hapd);
612 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
614 const u8 **uuid = ctx;
619 for (j = 0; j < iface->num_bss; j++) {
620 struct hostapd_data *hapd = iface->bss[j];
621 if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) {
622 *uuid = hapd->wps->uuid;
631 static const u8 * get_own_uuid(struct hostapd_iface *iface)
634 if (iface->for_each_interface == NULL)
637 iface->for_each_interface(iface->interfaces, get_uuid_cb, &uuid);
642 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
650 static int interface_count(struct hostapd_iface *iface)
653 if (iface->for_each_interface == NULL)
655 iface->for_each_interface(iface->interfaces, count_interface_cb,
661 static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
662 struct wps_context *wps)
666 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
667 wpabuf_free(wps->dev.vendor_ext[i]);
668 wps->dev.vendor_ext[i] = NULL;
670 if (hapd->conf->wps_vendor_ext[i] == NULL)
673 wps->dev.vendor_ext[i] =
674 wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
675 if (wps->dev.vendor_ext[i] == NULL) {
677 wpabuf_free(wps->dev.vendor_ext[i]);
686 int hostapd_init_wps(struct hostapd_data *hapd,
687 struct hostapd_bss_config *conf)
689 struct wps_context *wps;
690 struct wps_registrar_config cfg;
692 if (conf->wps_state == 0) {
693 hostapd_wps_clear_ies(hapd);
697 wps = os_zalloc(sizeof(*wps));
701 wps->cred_cb = hostapd_wps_cred_cb;
702 wps->event_cb = hostapd_wps_event_cb;
705 os_memset(&cfg, 0, sizeof(cfg));
706 wps->wps_state = hapd->conf->wps_state;
707 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
708 if (is_nil_uuid(hapd->conf->uuid)) {
710 uuid = get_own_uuid(hapd->iface);
712 os_memcpy(wps->uuid, uuid, UUID_LEN);
713 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
714 "interface", wps->uuid, UUID_LEN);
716 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
717 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
718 "address", wps->uuid, UUID_LEN);
721 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
722 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
723 wps->uuid, UUID_LEN);
725 wps->ssid_len = hapd->conf->ssid.ssid_len;
726 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
728 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
729 wps->dev.device_name = hapd->conf->device_name ?
730 os_strdup(hapd->conf->device_name) : NULL;
731 wps->dev.manufacturer = hapd->conf->manufacturer ?
732 os_strdup(hapd->conf->manufacturer) : NULL;
733 wps->dev.model_name = hapd->conf->model_name ?
734 os_strdup(hapd->conf->model_name) : NULL;
735 wps->dev.model_number = hapd->conf->model_number ?
736 os_strdup(hapd->conf->model_number) : NULL;
737 wps->dev.serial_number = hapd->conf->serial_number ?
738 os_strdup(hapd->conf->serial_number) : NULL;
739 wps->config_methods =
740 wps_config_methods_str2bin(hapd->conf->config_methods);
742 if ((wps->config_methods &
743 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
744 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
745 wpa_printf(MSG_INFO, "WPS: Converting display to "
746 "virtual_display for WPS 2.0 compliance");
747 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
749 if ((wps->config_methods &
750 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
751 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
752 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
753 "virtual_push_button for WPS 2.0 compliance");
754 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
756 #endif /* CONFIG_WPS2 */
757 os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
760 if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) {
765 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
766 wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
767 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
769 if (conf->wpa & WPA_PROTO_RSN) {
770 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
771 wps->auth_types |= WPS_AUTH_WPA2PSK;
772 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
773 wps->auth_types |= WPS_AUTH_WPA2;
775 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
776 wps->encr_types |= WPS_ENCR_AES;
777 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
778 wps->encr_types |= WPS_ENCR_TKIP;
781 if (conf->wpa & WPA_PROTO_WPA) {
782 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
783 wps->auth_types |= WPS_AUTH_WPAPSK;
784 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
785 wps->auth_types |= WPS_AUTH_WPA;
787 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
788 wps->encr_types |= WPS_ENCR_AES;
789 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
790 wps->encr_types |= WPS_ENCR_TKIP;
793 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
794 wps->encr_types |= WPS_ENCR_NONE;
795 wps->auth_types |= WPS_AUTH_OPEN;
796 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
797 wps->encr_types |= WPS_ENCR_WEP;
798 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
799 wps->auth_types |= WPS_AUTH_OPEN;
800 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
801 wps->auth_types |= WPS_AUTH_SHARED;
802 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
803 wps->auth_types |= WPS_AUTH_OPEN;
804 if (conf->default_wep_key_len)
805 wps->encr_types |= WPS_ENCR_WEP;
807 wps->encr_types |= WPS_ENCR_NONE;
810 if (conf->ssid.wpa_psk_file) {
811 /* Use per-device PSKs */
812 } else if (conf->ssid.wpa_passphrase) {
813 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
814 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
815 } else if (conf->ssid.wpa_psk) {
816 wps->network_key = os_malloc(2 * PMK_LEN + 1);
817 if (wps->network_key == NULL) {
821 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
822 conf->ssid.wpa_psk->psk, PMK_LEN);
823 wps->network_key_len = 2 * PMK_LEN;
824 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
825 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
826 if (wps->network_key == NULL) {
830 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
831 conf->ssid.wep.len[0]);
832 wps->network_key_len = conf->ssid.wep.len[0];
835 if (conf->ssid.wpa_psk) {
836 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
840 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
841 /* Override parameters to enable security by default */
842 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
843 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
846 wps->ap_settings = conf->ap_settings;
847 wps->ap_settings_len = conf->ap_settings_len;
849 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
850 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
851 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
852 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
853 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
855 cfg.skip_cred_build = conf->skip_cred_build;
856 cfg.extra_cred = conf->extra_cred;
857 cfg.extra_cred_len = conf->extra_cred_len;
858 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
859 conf->skip_cred_build;
860 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
861 cfg.static_wep_only = 1;
862 cfg.dualband = interface_count(hapd->iface) > 1;
864 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
866 wps->registrar = wps_registrar_init(wps, &cfg);
867 if (wps->registrar == NULL) {
868 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
869 os_free(wps->network_key);
874 #ifdef CONFIG_WPS_UPNP
875 wps->friendly_name = hapd->conf->friendly_name;
876 wps->manufacturer_url = hapd->conf->manufacturer_url;
877 wps->model_description = hapd->conf->model_description;
878 wps->model_url = hapd->conf->model_url;
879 wps->upc = hapd->conf->upc;
880 #endif /* CONFIG_WPS_UPNP */
882 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
890 int hostapd_init_wps_complete(struct hostapd_data *hapd)
892 struct wps_context *wps = hapd->wps;
897 #ifdef CONFIG_WPS_UPNP
898 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
899 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
900 wps_registrar_deinit(wps->registrar);
901 os_free(wps->network_key);
906 #endif /* CONFIG_WPS_UPNP */
912 void hostapd_deinit_wps(struct hostapd_data *hapd)
914 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
915 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
916 if (hapd->wps == NULL)
918 #ifdef CONFIG_WPS_UPNP
919 hostapd_wps_upnp_deinit(hapd);
920 #endif /* CONFIG_WPS_UPNP */
921 wps_registrar_deinit(hapd->wps->registrar);
922 os_free(hapd->wps->network_key);
923 wps_device_data_free(&hapd->wps->dev);
924 wpabuf_free(hapd->wps->dh_pubkey);
925 wpabuf_free(hapd->wps->dh_privkey);
926 wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
927 wpabuf_free(hapd->wps->oob_conf.dev_password);
928 wps_free_pending_msgs(hapd->wps->upnp_msgs);
931 hostapd_wps_clear_ies(hapd);
935 void hostapd_update_wps(struct hostapd_data *hapd)
937 if (hapd->wps == NULL)
940 #ifdef CONFIG_WPS_UPNP
941 hapd->wps->friendly_name = hapd->conf->friendly_name;
942 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
943 hapd->wps->model_description = hapd->conf->model_description;
944 hapd->wps->model_url = hapd->conf->model_url;
945 hapd->wps->upc = hapd->conf->upc;
946 #endif /* CONFIG_WPS_UPNP */
948 hostapd_wps_set_vendor_ext(hapd, hapd->wps);
950 if (hapd->conf->wps_state)
951 wps_registrar_update_ie(hapd->wps->registrar);
953 hostapd_deinit_wps(hapd);
957 struct wps_add_pin_data {
967 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
969 struct wps_add_pin_data *data = ctx;
972 if (hapd->wps == NULL)
974 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
975 data->uuid, data->pin, data->pin_len,
983 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
984 const char *uuid, const char *pin, int timeout)
987 struct wps_add_pin_data data;
991 data.pin = (const u8 *) pin;
992 data.pin_len = os_strlen(pin);
993 data.timeout = timeout;
996 if (os_strcmp(uuid, "any") == 0)
999 if (uuid_str2bin(uuid, u))
1003 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1005 return data.added ? 0 : -1;
1009 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1011 const u8 *p2p_dev_addr = ctx;
1012 if (hapd->wps == NULL)
1014 return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
1018 int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1019 const u8 *p2p_dev_addr)
1021 return hostapd_wps_for_each(hapd, wps_button_pushed,
1022 (void *) p2p_dev_addr);
1026 #ifdef CONFIG_WPS_OOB
1027 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
1028 char *path, char *method, char *name)
1030 struct wps_context *wps = hapd->wps;
1031 struct oob_device_data *oob_dev;
1033 oob_dev = wps_get_oob_device(device_type);
1034 if (oob_dev == NULL)
1036 oob_dev->device_path = path;
1037 oob_dev->device_name = name;
1038 wps->oob_conf.oob_method = wps_get_oob_method(method);
1040 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
1042 * Use pre-configured DH keys in order to be able to write the
1043 * key hash into the OOB file.
1045 wpabuf_free(wps->dh_pubkey);
1046 wpabuf_free(wps->dh_privkey);
1047 wps->dh_privkey = NULL;
1048 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
1050 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
1051 if (wps->dh_pubkey == NULL) {
1052 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
1053 "Diffie-Hellman handshake");
1058 if (wps_process_oob(wps, oob_dev, 1) < 0)
1061 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
1062 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
1063 hostapd_wps_add_pin(hapd, NULL, "any",
1064 wpabuf_head(wps->oob_conf.dev_password), 0) <
1071 wpabuf_free(wps->dh_pubkey);
1072 wps->dh_pubkey = NULL;
1073 wpabuf_free(wps->dh_privkey);
1074 wps->dh_privkey = NULL;
1077 #endif /* CONFIG_WPS_OOB */
1080 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1082 const u8 *ie, size_t ie_len)
1084 struct hostapd_data *hapd = ctx;
1085 struct wpabuf *wps_ie;
1086 struct ieee802_11_elems elems;
1088 if (hapd->wps == NULL)
1091 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1092 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1093 MACSTR, MAC2STR(addr));
1097 if (elems.ssid && elems.ssid_len > 0 &&
1098 (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1099 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1101 return 0; /* Not for us */
1103 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1106 if (wps_validate_probe_req(wps_ie, addr) < 0) {
1107 wpabuf_free(wps_ie);
1111 if (wpabuf_len(wps_ie) > 0) {
1112 int p2p_wildcard = 0;
1114 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1115 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1116 P2P_WILDCARD_SSID_LEN) == 0)
1118 #endif /* CONFIG_P2P */
1119 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1121 #ifdef CONFIG_WPS_UPNP
1122 /* FIX: what exactly should be included in the WLANEvent?
1123 * WPS attributes? Full ProbeReq frame? */
1125 upnp_wps_device_send_wlan_event(
1126 hapd->wps_upnp, addr,
1127 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1128 #endif /* CONFIG_WPS_UPNP */
1131 wpabuf_free(wps_ie);
1137 #ifdef CONFIG_WPS_UPNP
1139 static int hostapd_rx_req_put_wlan_response(
1140 void *priv, enum upnp_wps_wlanevent_type ev_type,
1141 const u8 *mac_addr, const struct wpabuf *msg,
1142 enum wps_msg_type msg_type)
1144 struct hostapd_data *hapd = priv;
1145 struct sta_info *sta;
1146 struct upnp_pending_message *p;
1148 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1149 MACSTR, ev_type, MAC2STR(mac_addr));
1150 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1151 wpabuf_head(msg), wpabuf_len(msg));
1152 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1153 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1154 "PutWLANResponse WLANEventType %d", ev_type);
1159 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1160 * server implementation for delivery to the peer.
1163 sta = ap_get_sta(hapd, mac_addr);
1164 #ifndef CONFIG_WPS_STRICT
1167 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1168 * Pick STA that is in an ongoing WPS registration without
1169 * checking the MAC address.
1171 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1172 "on NewWLANEventMAC; try wildcard match");
1173 for (sta = hapd->sta_list; sta; sta = sta->next) {
1174 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1178 #endif /* CONFIG_WPS_STRICT */
1181 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1185 p = os_zalloc(sizeof(*p));
1188 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1189 p->msg = wpabuf_dup(msg);
1191 p->next = hapd->wps->upnp_msgs;
1192 hapd->wps->upnp_msgs = p;
1194 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1198 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1199 struct wps_context *wps)
1201 struct upnp_wps_device_ctx *ctx;
1203 if (!hapd->conf->upnp_iface)
1205 ctx = os_zalloc(sizeof(*ctx));
1209 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1210 if (hapd->conf->ap_pin)
1211 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1213 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1214 hapd->conf->upnp_iface);
1215 if (hapd->wps_upnp == NULL)
1217 wps->wps_upnp = hapd->wps_upnp;
1223 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1225 upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1228 #endif /* CONFIG_WPS_UPNP */
1231 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1232 char *buf, size_t buflen)
1234 if (hapd->wps == NULL)
1236 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1240 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1242 struct hostapd_data *hapd = eloop_data;
1243 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1244 hostapd_wps_ap_pin_disable(hapd);
1248 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1250 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1251 hapd->ap_pin_failures = 0;
1252 hapd->conf->ap_setup_locked = 0;
1253 if (hapd->wps->ap_setup_locked) {
1254 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1255 hapd->wps->ap_setup_locked = 0;
1256 wps_registrar_update_ie(hapd->wps->registrar);
1258 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1260 eloop_register_timeout(timeout, 0,
1261 hostapd_wps_ap_pin_timeout, hapd, NULL);
1265 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1267 os_free(hapd->conf->ap_pin);
1268 hapd->conf->ap_pin = NULL;
1269 #ifdef CONFIG_WPS_UPNP
1270 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1271 #endif /* CONFIG_WPS_UPNP */
1272 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1277 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1279 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1280 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1284 struct wps_ap_pin_data {
1290 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1292 struct wps_ap_pin_data *data = ctx;
1293 os_free(hapd->conf->ap_pin);
1294 hapd->conf->ap_pin = os_strdup(data->pin_txt);
1295 #ifdef CONFIG_WPS_UPNP
1296 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1297 #endif /* CONFIG_WPS_UPNP */
1298 hostapd_wps_ap_pin_enable(hapd, data->timeout);
1303 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1306 struct wps_ap_pin_data data;
1308 pin = wps_generate_pin();
1309 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%u", pin);
1310 data.timeout = timeout;
1311 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1312 return hapd->conf->ap_pin;
1316 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1318 return hapd->conf->ap_pin;
1322 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1325 struct wps_ap_pin_data data;
1328 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1329 if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1331 data.timeout = timeout;
1332 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1336 static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1339 wps_registrar_update_ie(hapd->wps->registrar);
1344 void hostapd_wps_update_ie(struct hostapd_data *hapd)
1346 hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1350 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1351 const char *auth, const char *encr, const char *key)
1353 struct wps_credential cred;
1356 os_memset(&cred, 0, sizeof(cred));
1358 len = os_strlen(ssid);
1359 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1360 hexstr2bin(ssid, cred.ssid, len / 2))
1362 cred.ssid_len = len / 2;
1364 if (os_strncmp(auth, "OPEN", 4) == 0)
1365 cred.auth_type = WPS_AUTH_OPEN;
1366 else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1367 cred.auth_type = WPS_AUTH_WPAPSK;
1368 else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1369 cred.auth_type = WPS_AUTH_WPA2PSK;
1374 if (os_strncmp(encr, "NONE", 4) == 0)
1375 cred.encr_type = WPS_ENCR_NONE;
1376 else if (os_strncmp(encr, "WEP", 3) == 0)
1377 cred.encr_type = WPS_ENCR_WEP;
1378 else if (os_strncmp(encr, "TKIP", 4) == 0)
1379 cred.encr_type = WPS_ENCR_TKIP;
1380 else if (os_strncmp(encr, "CCMP", 4) == 0)
1381 cred.encr_type = WPS_ENCR_AES;
1385 cred.encr_type = WPS_ENCR_NONE;
1388 len = os_strlen(key);
1389 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1390 hexstr2bin(key, cred.key, len / 2))
1392 cred.key_len = len / 2;
1395 return wps_registrar_config_ap(hapd->wps->registrar, &cred);