2 * hostapd / WPS integration
3 * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/uuid.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ieee802_11_common.h"
17 #include "eapol_auth/eapol_auth_sm.h"
18 #include "eapol_auth/eapol_auth_sm_i.h"
20 #include "wps/wps_defs.h"
21 #include "wps/wps_dev_attr.h"
22 #include "wps/wps_attr_parse.h"
24 #include "ap_config.h"
25 #include "ap_drv_ops.h"
28 #include "wps_hostapd.h"
31 #ifdef CONFIG_WPS_UPNP
32 #include "wps/wps_upnp.h"
33 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
34 struct wps_context *wps);
35 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
36 #endif /* CONFIG_WPS_UPNP */
38 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
40 const u8 *ie, size_t ie_len,
42 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
45 struct wps_for_each_data {
46 int (*func)(struct hostapd_data *h, void *ctx);
51 static int wps_for_each(struct hostapd_iface *iface, void *ctx)
53 struct wps_for_each_data *data = ctx;
58 for (j = 0; j < iface->num_bss; j++) {
59 struct hostapd_data *hapd = iface->bss[j];
60 int ret = data->func(hapd, data->ctx);
69 static int hostapd_wps_for_each(struct hostapd_data *hapd,
70 int (*func)(struct hostapd_data *h, void *ctx),
73 struct hostapd_iface *iface = hapd->iface;
74 struct wps_for_each_data data;
77 if (iface->interfaces == NULL ||
78 iface->interfaces->for_each_interface == NULL)
79 return wps_for_each(iface, &data);
80 return iface->interfaces->for_each_interface(iface->interfaces,
85 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
88 struct hostapd_data *hapd = ctx;
89 struct hostapd_wpa_psk *p;
90 struct hostapd_ssid *ssid = &hapd->conf->ssid;
92 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
93 MACSTR, MAC2STR(mac_addr));
94 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
96 if (psk_len != PMK_LEN) {
97 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
98 (unsigned long) psk_len);
102 /* Add the new PSK to runtime PSK list */
103 p = os_zalloc(sizeof(*p));
106 os_memcpy(p->addr, mac_addr, ETH_ALEN);
107 os_memcpy(p->psk, psk, PMK_LEN);
109 p->next = ssid->wpa_psk;
112 if (ssid->wpa_psk_file) {
114 char hex[PMK_LEN * 2 + 1];
115 /* Add the new PSK to PSK list file */
116 f = fopen(ssid->wpa_psk_file, "a");
118 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
119 "'%s'", ssid->wpa_psk_file);
123 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
124 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
132 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
133 struct wpabuf *probe_resp_ie)
135 struct hostapd_data *hapd = ctx;
136 wpabuf_free(hapd->wps_beacon_ie);
137 hapd->wps_beacon_ie = beacon_ie;
138 wpabuf_free(hapd->wps_probe_resp_ie);
139 hapd->wps_probe_resp_ie = probe_resp_ie;
140 if (hapd->beacon_set_done)
141 ieee802_11_set_beacon(hapd);
142 return hostapd_set_ap_wps_ie(hapd);
146 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
147 const struct wps_device_data *dev)
149 struct hostapd_data *hapd = ctx;
150 char uuid[40], txt[400];
152 char devtype[WPS_DEV_TYPE_BUFSIZE];
153 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
155 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
156 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
157 "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
158 uuid, MAC2STR(dev->mac_addr), dev->device_name,
159 dev->manufacturer, dev->model_name,
160 dev->model_number, dev->serial_number,
161 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
163 if (len > 0 && len < (int) sizeof(txt))
164 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
166 if (hapd->conf->wps_pin_requests) {
169 f = fopen(hapd->conf->wps_pin_requests, "a");
173 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
175 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
176 dev->manufacturer, dev->model_name, dev->model_number,
178 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
185 struct wps_stop_reg_data {
186 struct hostapd_data *current_hapd;
192 static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
194 struct wps_stop_reg_data *data = ctx;
195 if (hapd != data->current_hapd && hapd->wps != NULL)
196 wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
197 data->dev_pw, data->dev_pw_len);
202 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
203 const u8 *uuid_e, const u8 *dev_pw,
206 struct hostapd_data *hapd = ctx;
208 struct wps_stop_reg_data data;
209 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
211 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
212 MAC2STR(mac_addr), uuid);
213 if (hapd->wps_reg_success_cb)
214 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
216 data.current_hapd = hapd;
217 data.uuid_e = uuid_e;
218 data.dev_pw = dev_pw;
219 data.dev_pw_len = dev_pw_len;
220 hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
224 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
226 const u8 *pri_dev_type,
228 u16 dev_password_id, u8 request_type,
229 const char *dev_name)
231 struct hostapd_data *hapd = ctx;
233 char devtype[WPS_DEV_TYPE_BUFSIZE];
234 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
236 if (dev_name == NULL)
238 wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
239 " %s %s 0x%x %u %u [%s]",
241 wps_dev_type_bin2str(pri_dev_type, devtype,
243 config_methods, dev_password_id, request_type, dev_name);
247 static int str_starts(const char *str, const char *start)
249 return os_strncmp(str, start, os_strlen(start)) == 0;
253 static void wps_reload_config(void *eloop_data, void *user_ctx)
255 struct hostapd_iface *iface = eloop_data;
257 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
258 if (iface->interfaces == NULL ||
259 iface->interfaces->reload_config(iface) < 0) {
260 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
266 static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
269 size_t blen = attr_len * 2 + 1;
270 char *buf = os_malloc(blen);
272 wpa_snprintf_hex(buf, blen, attr, attr_len);
273 wpa_msg(hapd->msg_ctx, MSG_INFO,
274 WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
280 static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
281 const struct wps_credential *cred)
283 struct hostapd_bss_config *bss = hapd->conf;
285 wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
288 if (cred->ssid_len <= HOSTAPD_MAX_SSID_LEN) {
289 os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
290 bss->ssid.ssid_len = cred->ssid_len;
291 bss->ssid.ssid_set = 1;
294 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
295 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
297 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
299 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
305 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
306 bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
307 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
308 bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
310 bss->wpa_pairwise = 0;
311 if (cred->encr_type & WPS_ENCR_AES)
312 bss->wpa_pairwise |= WPA_CIPHER_CCMP;
313 if (cred->encr_type & WPS_ENCR_TKIP)
314 bss->wpa_pairwise |= WPA_CIPHER_TKIP;
315 bss->rsn_pairwise = bss->wpa_pairwise;
316 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
320 if (cred->key_len >= 8 && cred->key_len < 64) {
321 os_free(bss->ssid.wpa_passphrase);
322 bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
323 if (bss->ssid.wpa_passphrase)
324 os_memcpy(bss->ssid.wpa_passphrase, cred->key,
326 os_free(bss->ssid.wpa_psk);
327 bss->ssid.wpa_psk = NULL;
328 } else if (cred->key_len == 64) {
329 os_free(bss->ssid.wpa_psk);
331 os_zalloc(sizeof(struct hostapd_wpa_psk));
332 if (bss->ssid.wpa_psk &&
333 hexstr2bin((const char *) cred->key,
334 bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
335 bss->ssid.wpa_psk->group = 1;
336 os_free(bss->ssid.wpa_passphrase);
337 bss->ssid.wpa_passphrase = NULL;
342 if ((cred->auth_type & WPS_AUTH_OPEN) &&
343 (cred->auth_type & WPS_AUTH_SHARED))
345 else if (cred->auth_type & WPS_AUTH_SHARED)
349 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx > 0 &&
350 cred->key_idx <= 4) {
351 struct hostapd_wep_keys *wep = &bss->ssid.wep;
352 int idx = cred->key_idx;
356 if (cred->key_len == 10 || cred->key_len == 26) {
357 os_free(wep->key[idx]);
358 wep->key[idx] = os_malloc(cred->key_len / 2);
359 if (wep->key[idx] == NULL ||
360 hexstr2bin((const char *) cred->key,
364 wep->len[idx] = cred->key_len / 2;
366 os_free(wep->key[idx]);
367 wep->key[idx] = os_malloc(cred->key_len);
368 if (wep->key[idx] == NULL)
370 os_memcpy(wep->key[idx], cred->key,
372 wep->len[idx] = cred->key_len;
378 /* Schedule configuration reload after short period of time to allow
379 * EAP-WSC to be finished.
381 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
388 static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
390 const struct wps_credential *cred = ctx;
398 if (hapd->wps == NULL)
401 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
402 cred->cred_attr, cred->cred_attr_len);
404 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
405 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
406 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
408 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
409 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
410 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
411 cred->key, cred->key_len);
412 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
413 MAC2STR(cred->mac_addr));
415 if ((hapd->conf->wps_cred_processing == 1 ||
416 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
417 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
418 } else if (hapd->conf->wps_cred_processing == 1 ||
419 hapd->conf->wps_cred_processing == 2) {
421 attr = wpabuf_alloc(200);
422 if (attr && wps_build_credential_wrap(attr, cred) == 0)
423 hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
427 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
429 if (hapd->conf->wps_cred_processing == 1)
432 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
433 hapd->wps->ssid_len = cred->ssid_len;
434 hapd->wps->encr_types = cred->encr_type;
435 hapd->wps->auth_types = cred->auth_type;
436 if (cred->key_len == 0) {
437 os_free(hapd->wps->network_key);
438 hapd->wps->network_key = NULL;
439 hapd->wps->network_key_len = 0;
441 if (hapd->wps->network_key == NULL ||
442 hapd->wps->network_key_len < cred->key_len) {
443 hapd->wps->network_key_len = 0;
444 os_free(hapd->wps->network_key);
445 hapd->wps->network_key = os_malloc(cred->key_len);
446 if (hapd->wps->network_key == NULL)
449 hapd->wps->network_key_len = cred->key_len;
450 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
452 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
454 if (hapd->iface->config_fname == NULL)
455 return hapd_wps_reconfig_in_memory(hapd, cred);
456 len = os_strlen(hapd->iface->config_fname) + 5;
457 tmp_fname = os_malloc(len);
458 if (tmp_fname == NULL)
460 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
462 oconf = fopen(hapd->iface->config_fname, "r");
464 wpa_printf(MSG_WARNING, "WPS: Could not open current "
465 "configuration file");
470 nconf = fopen(tmp_fname, "w");
472 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
473 "configuration file");
479 fprintf(nconf, "# WPS configuration - START\n");
481 fprintf(nconf, "wps_state=2\n");
483 if (is_hex(cred->ssid, cred->ssid_len)) {
484 fprintf(nconf, "ssid2=");
485 for (i = 0; i < cred->ssid_len; i++)
486 fprintf(nconf, "%02x", cred->ssid[i]);
487 fprintf(nconf, "\n");
489 fprintf(nconf, "ssid=");
490 for (i = 0; i < cred->ssid_len; i++)
491 fputc(cred->ssid[i], nconf);
492 fprintf(nconf, "\n");
495 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
496 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
498 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
500 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
507 fprintf(nconf, "wpa=%d\n", wpa);
509 fprintf(nconf, "wpa_key_mgmt=");
511 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
512 fprintf(nconf, "WPA-EAP");
515 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
516 fprintf(nconf, "%sWPA-PSK", prefix);
517 fprintf(nconf, "\n");
519 fprintf(nconf, "wpa_pairwise=");
521 if (cred->encr_type & WPS_ENCR_AES) {
522 fprintf(nconf, "CCMP");
525 if (cred->encr_type & WPS_ENCR_TKIP) {
526 fprintf(nconf, "%sTKIP", prefix);
528 fprintf(nconf, "\n");
530 if (cred->key_len >= 8 && cred->key_len < 64) {
531 fprintf(nconf, "wpa_passphrase=");
532 for (i = 0; i < cred->key_len; i++)
533 fputc(cred->key[i], nconf);
534 fprintf(nconf, "\n");
535 } else if (cred->key_len == 64) {
536 fprintf(nconf, "wpa_psk=");
537 for (i = 0; i < cred->key_len; i++)
538 fputc(cred->key[i], nconf);
539 fprintf(nconf, "\n");
541 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
543 (unsigned long) cred->key_len);
546 fprintf(nconf, "auth_algs=1\n");
548 if ((cred->auth_type & WPS_AUTH_OPEN) &&
549 (cred->auth_type & WPS_AUTH_SHARED))
550 fprintf(nconf, "auth_algs=3\n");
551 else if (cred->auth_type & WPS_AUTH_SHARED)
552 fprintf(nconf, "auth_algs=2\n");
554 fprintf(nconf, "auth_algs=1\n");
556 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
557 int key_idx = cred->key_idx;
560 fprintf(nconf, "wep_default_key=%d\n", key_idx);
561 fprintf(nconf, "wep_key%d=", key_idx);
562 if (cred->key_len == 10 || cred->key_len == 26) {
563 /* WEP key as a hex string */
564 for (i = 0; i < cred->key_len; i++)
565 fputc(cred->key[i], nconf);
567 /* Raw WEP key; convert to hex */
568 for (i = 0; i < cred->key_len; i++)
569 fprintf(nconf, "%02x", cred->key[i]);
571 fprintf(nconf, "\n");
575 fprintf(nconf, "# WPS configuration - END\n");
578 while (fgets(buf, sizeof(buf), oconf)) {
579 if (os_strncmp(buf, "bss=", 4) == 0)
582 (str_starts(buf, "ssid=") ||
583 str_starts(buf, "ssid2=") ||
584 str_starts(buf, "auth_algs=") ||
585 str_starts(buf, "wep_default_key=") ||
586 str_starts(buf, "wep_key") ||
587 str_starts(buf, "wps_state=") ||
588 str_starts(buf, "wpa=") ||
589 str_starts(buf, "wpa_psk=") ||
590 str_starts(buf, "wpa_pairwise=") ||
591 str_starts(buf, "rsn_pairwise=") ||
592 str_starts(buf, "wpa_key_mgmt=") ||
593 str_starts(buf, "wpa_passphrase="))) {
594 fprintf(nconf, "#WPS# %s", buf);
596 fprintf(nconf, "%s", buf);
602 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
603 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
604 "configuration file: %s", strerror(errno));
611 /* Schedule configuration reload after short period of time to allow
612 * EAP-WSC to be finished.
614 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
617 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
623 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
625 struct hostapd_data *hapd = ctx;
626 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
630 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
632 struct hostapd_data *hapd = eloop_data;
634 if (hapd->conf->ap_setup_locked)
636 if (hapd->ap_pin_failures_consecutive >= 10)
639 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
640 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
641 hapd->wps->ap_setup_locked = 0;
642 wps_registrar_update_ie(hapd->wps->registrar);
646 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
648 struct wps_event_pwd_auth_fail *data = ctx;
650 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
654 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
655 * for some time if this happens multiple times to slow down brute
658 hapd->ap_pin_failures++;
659 hapd->ap_pin_failures_consecutive++;
660 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
662 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
663 if (hapd->ap_pin_failures < 3)
666 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
667 hapd->wps->ap_setup_locked = 1;
669 wps_registrar_update_ie(hapd->wps->registrar);
671 if (!hapd->conf->ap_setup_locked &&
672 hapd->ap_pin_failures_consecutive >= 10) {
674 * In indefinite lockdown - disable automatic AP PIN
677 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
678 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
679 } else if (!hapd->conf->ap_setup_locked) {
680 if (hapd->ap_pin_lockout_time == 0)
681 hapd->ap_pin_lockout_time = 60;
682 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
683 (hapd->ap_pin_failures % 3) == 0)
684 hapd->ap_pin_lockout_time *= 2;
686 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
687 hapd->ap_pin_lockout_time);
688 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
689 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
690 hostapd_wps_reenable_ap_pin, hapd,
698 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
699 struct wps_event_pwd_auth_fail *data)
701 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
705 static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
707 if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
710 if (hapd->ap_pin_failures_consecutive == 0)
713 wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
714 "- total validation failures %u (%u consecutive)",
715 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
716 hapd->ap_pin_failures_consecutive = 0;
722 static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
724 hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
728 static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = {
729 "No Error", /* WPS_EI_NO_ERROR */
730 "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */
731 "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */
734 static void hostapd_wps_event_fail(struct hostapd_data *hapd,
735 struct wps_event_fail *fail)
737 if (fail->error_indication > 0 &&
738 fail->error_indication < NUM_WPS_EI_VALUES) {
739 wpa_msg(hapd->msg_ctx, MSG_INFO,
740 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
741 fail->msg, fail->config_error, fail->error_indication,
742 wps_event_fail_reason[fail->error_indication]);
744 wpa_msg(hapd->msg_ctx, MSG_INFO,
745 WPS_EVENT_FAIL "msg=%d config_error=%d",
746 fail->msg, fail->config_error);
751 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
752 union wps_event_data *data)
754 struct hostapd_data *hapd = ctx;
758 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
761 hostapd_wps_event_fail(hapd, &data->fail);
764 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
766 case WPS_EV_PWD_AUTH_FAIL:
767 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
769 case WPS_EV_PBC_OVERLAP:
770 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
772 case WPS_EV_PBC_TIMEOUT:
773 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
775 case WPS_EV_ER_AP_ADD:
777 case WPS_EV_ER_AP_REMOVE:
779 case WPS_EV_ER_ENROLLEE_ADD:
781 case WPS_EV_ER_ENROLLEE_REMOVE:
783 case WPS_EV_ER_AP_SETTINGS:
785 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
787 case WPS_EV_AP_PIN_SUCCESS:
788 hostapd_wps_ap_pin_success(hapd);
791 if (hapd->wps_event_cb)
792 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
796 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
798 wpabuf_free(hapd->wps_beacon_ie);
799 hapd->wps_beacon_ie = NULL;
801 wpabuf_free(hapd->wps_probe_resp_ie);
802 hapd->wps_probe_resp_ie = NULL;
804 hostapd_set_ap_wps_ie(hapd);
808 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
810 const u8 **uuid = ctx;
815 for (j = 0; j < iface->num_bss; j++) {
816 struct hostapd_data *hapd = iface->bss[j];
817 if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) {
818 *uuid = hapd->wps->uuid;
827 static const u8 * get_own_uuid(struct hostapd_iface *iface)
830 if (iface->interfaces == NULL ||
831 iface->interfaces->for_each_interface == NULL)
834 iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
840 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
848 static int interface_count(struct hostapd_iface *iface)
851 if (iface->interfaces == NULL ||
852 iface->interfaces->for_each_interface == NULL)
854 iface->interfaces->for_each_interface(iface->interfaces,
855 count_interface_cb, &count);
860 static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
861 struct wps_context *wps)
865 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
866 wpabuf_free(wps->dev.vendor_ext[i]);
867 wps->dev.vendor_ext[i] = NULL;
869 if (hapd->conf->wps_vendor_ext[i] == NULL)
872 wps->dev.vendor_ext[i] =
873 wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
874 if (wps->dev.vendor_ext[i] == NULL) {
876 wpabuf_free(wps->dev.vendor_ext[i]);
885 int hostapd_init_wps(struct hostapd_data *hapd,
886 struct hostapd_bss_config *conf)
888 struct wps_context *wps;
889 struct wps_registrar_config cfg;
891 if (conf->wps_state == 0) {
892 hostapd_wps_clear_ies(hapd);
896 wps = os_zalloc(sizeof(*wps));
900 wps->cred_cb = hostapd_wps_cred_cb;
901 wps->event_cb = hostapd_wps_event_cb;
904 os_memset(&cfg, 0, sizeof(cfg));
905 wps->wps_state = hapd->conf->wps_state;
906 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
907 if (is_nil_uuid(hapd->conf->uuid)) {
909 uuid = get_own_uuid(hapd->iface);
911 os_memcpy(wps->uuid, uuid, UUID_LEN);
912 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
913 "interface", wps->uuid, UUID_LEN);
915 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
916 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
917 "address", wps->uuid, UUID_LEN);
920 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
921 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
922 wps->uuid, UUID_LEN);
924 wps->ssid_len = hapd->conf->ssid.ssid_len;
925 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
927 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
928 wps->dev.device_name = hapd->conf->device_name ?
929 os_strdup(hapd->conf->device_name) : NULL;
930 wps->dev.manufacturer = hapd->conf->manufacturer ?
931 os_strdup(hapd->conf->manufacturer) : NULL;
932 wps->dev.model_name = hapd->conf->model_name ?
933 os_strdup(hapd->conf->model_name) : NULL;
934 wps->dev.model_number = hapd->conf->model_number ?
935 os_strdup(hapd->conf->model_number) : NULL;
936 wps->dev.serial_number = hapd->conf->serial_number ?
937 os_strdup(hapd->conf->serial_number) : NULL;
938 wps->config_methods =
939 wps_config_methods_str2bin(hapd->conf->config_methods);
941 if ((wps->config_methods &
942 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
943 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
944 wpa_printf(MSG_INFO, "WPS: Converting display to "
945 "virtual_display for WPS 2.0 compliance");
946 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
948 if ((wps->config_methods &
949 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
950 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
951 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
952 "virtual_push_button for WPS 2.0 compliance");
953 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
955 #endif /* CONFIG_WPS2 */
956 os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
959 if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) {
964 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
966 if (conf->wps_rf_bands) {
967 wps->dev.rf_bands = conf->wps_rf_bands;
970 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
971 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
974 if (conf->wpa & WPA_PROTO_RSN) {
975 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
976 wps->auth_types |= WPS_AUTH_WPA2PSK;
977 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
978 wps->auth_types |= WPS_AUTH_WPA2;
980 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
981 wps->encr_types |= WPS_ENCR_AES;
982 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
983 wps->encr_types |= WPS_ENCR_TKIP;
986 if (conf->wpa & WPA_PROTO_WPA) {
987 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
988 wps->auth_types |= WPS_AUTH_WPAPSK;
989 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
990 wps->auth_types |= WPS_AUTH_WPA;
992 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
993 wps->encr_types |= WPS_ENCR_AES;
994 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
995 wps->encr_types |= WPS_ENCR_TKIP;
998 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
999 wps->encr_types |= WPS_ENCR_NONE;
1000 wps->auth_types |= WPS_AUTH_OPEN;
1001 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
1002 wps->encr_types |= WPS_ENCR_WEP;
1003 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
1004 wps->auth_types |= WPS_AUTH_OPEN;
1005 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
1006 wps->auth_types |= WPS_AUTH_SHARED;
1007 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
1008 wps->auth_types |= WPS_AUTH_OPEN;
1009 if (conf->default_wep_key_len)
1010 wps->encr_types |= WPS_ENCR_WEP;
1012 wps->encr_types |= WPS_ENCR_NONE;
1015 if (conf->ssid.wpa_psk_file) {
1016 /* Use per-device PSKs */
1017 } else if (conf->ssid.wpa_passphrase) {
1018 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1019 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1020 } else if (conf->ssid.wpa_psk) {
1021 wps->network_key = os_malloc(2 * PMK_LEN + 1);
1022 if (wps->network_key == NULL) {
1026 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1027 conf->ssid.wpa_psk->psk, PMK_LEN);
1028 wps->network_key_len = 2 * PMK_LEN;
1029 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1030 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
1031 if (wps->network_key == NULL) {
1035 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1036 conf->ssid.wep.len[0]);
1037 wps->network_key_len = conf->ssid.wep.len[0];
1040 if (conf->ssid.wpa_psk) {
1041 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1045 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
1046 /* Override parameters to enable security by default */
1047 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1048 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1051 wps->ap_settings = conf->ap_settings;
1052 wps->ap_settings_len = conf->ap_settings_len;
1054 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
1055 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
1056 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
1057 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
1058 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
1060 cfg.skip_cred_build = conf->skip_cred_build;
1061 cfg.extra_cred = conf->extra_cred;
1062 cfg.extra_cred_len = conf->extra_cred_len;
1063 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
1064 conf->skip_cred_build;
1065 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
1066 cfg.static_wep_only = 1;
1067 cfg.dualband = interface_count(hapd->iface) > 1;
1068 if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
1069 (WPS_RF_50GHZ | WPS_RF_24GHZ))
1072 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
1074 wps->registrar = wps_registrar_init(wps, &cfg);
1075 if (wps->registrar == NULL) {
1076 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
1077 os_free(wps->network_key);
1082 #ifdef CONFIG_WPS_UPNP
1083 wps->friendly_name = hapd->conf->friendly_name;
1084 wps->manufacturer_url = hapd->conf->manufacturer_url;
1085 wps->model_description = hapd->conf->model_description;
1086 wps->model_url = hapd->conf->model_url;
1087 wps->upc = hapd->conf->upc;
1088 #endif /* CONFIG_WPS_UPNP */
1090 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
1098 int hostapd_init_wps_complete(struct hostapd_data *hapd)
1100 struct wps_context *wps = hapd->wps;
1105 #ifdef CONFIG_WPS_UPNP
1106 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
1107 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
1108 wps_registrar_deinit(wps->registrar);
1109 os_free(wps->network_key);
1114 #endif /* CONFIG_WPS_UPNP */
1120 static void hostapd_wps_nfc_clear(struct wps_context *wps)
1122 #ifdef CONFIG_WPS_NFC
1123 wps->ap_nfc_dev_pw_id = 0;
1124 wpabuf_free(wps->ap_nfc_dh_pubkey);
1125 wps->ap_nfc_dh_pubkey = NULL;
1126 wpabuf_free(wps->ap_nfc_dh_privkey);
1127 wps->ap_nfc_dh_privkey = NULL;
1128 wpabuf_free(wps->ap_nfc_dev_pw);
1129 wps->ap_nfc_dev_pw = NULL;
1130 #endif /* CONFIG_WPS_NFC */
1134 void hostapd_deinit_wps(struct hostapd_data *hapd)
1136 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1137 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1138 if (hapd->wps == NULL)
1140 #ifdef CONFIG_WPS_UPNP
1141 hostapd_wps_upnp_deinit(hapd);
1142 #endif /* CONFIG_WPS_UPNP */
1143 wps_registrar_deinit(hapd->wps->registrar);
1144 os_free(hapd->wps->network_key);
1145 wps_device_data_free(&hapd->wps->dev);
1146 wpabuf_free(hapd->wps->dh_pubkey);
1147 wpabuf_free(hapd->wps->dh_privkey);
1148 wps_free_pending_msgs(hapd->wps->upnp_msgs);
1149 hostapd_wps_nfc_clear(hapd->wps);
1152 hostapd_wps_clear_ies(hapd);
1156 void hostapd_update_wps(struct hostapd_data *hapd)
1158 if (hapd->wps == NULL)
1161 #ifdef CONFIG_WPS_UPNP
1162 hapd->wps->friendly_name = hapd->conf->friendly_name;
1163 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
1164 hapd->wps->model_description = hapd->conf->model_description;
1165 hapd->wps->model_url = hapd->conf->model_url;
1166 hapd->wps->upc = hapd->conf->upc;
1167 #endif /* CONFIG_WPS_UPNP */
1169 hostapd_wps_set_vendor_ext(hapd, hapd->wps);
1171 if (hapd->conf->wps_state)
1172 wps_registrar_update_ie(hapd->wps->registrar);
1174 hostapd_deinit_wps(hapd);
1178 struct wps_add_pin_data {
1188 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1190 struct wps_add_pin_data *data = ctx;
1193 if (hapd->wps == NULL)
1195 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1196 data->uuid, data->pin, data->pin_len,
1204 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1205 const char *uuid, const char *pin, int timeout)
1208 struct wps_add_pin_data data;
1212 data.pin = (const u8 *) pin;
1213 data.pin_len = os_strlen(pin);
1214 data.timeout = timeout;
1217 if (os_strcmp(uuid, "any") == 0)
1220 if (uuid_str2bin(uuid, u))
1224 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1226 return data.added ? 0 : -1;
1230 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1232 const u8 *p2p_dev_addr = ctx;
1233 if (hapd->wps == NULL)
1235 return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
1239 int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1240 const u8 *p2p_dev_addr)
1242 return hostapd_wps_for_each(hapd, wps_button_pushed,
1243 (void *) p2p_dev_addr);
1247 static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1249 if (hapd->wps == NULL)
1252 wps_registrar_wps_cancel(hapd->wps->registrar);
1253 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
1259 int hostapd_wps_cancel(struct hostapd_data *hapd)
1261 return hostapd_wps_for_each(hapd, wps_cancel, NULL);
1265 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1267 const u8 *ie, size_t ie_len,
1270 struct hostapd_data *hapd = ctx;
1271 struct wpabuf *wps_ie;
1272 struct ieee802_11_elems elems;
1274 if (hapd->wps == NULL)
1277 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1278 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1279 MACSTR, MAC2STR(addr));
1283 if (elems.ssid && elems.ssid_len > 0 &&
1284 (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1285 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1287 return 0; /* Not for us */
1289 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1292 if (wps_validate_probe_req(wps_ie, addr) < 0) {
1293 wpabuf_free(wps_ie);
1297 if (wpabuf_len(wps_ie) > 0) {
1298 int p2p_wildcard = 0;
1300 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1301 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1302 P2P_WILDCARD_SSID_LEN) == 0)
1304 #endif /* CONFIG_P2P */
1305 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1307 #ifdef CONFIG_WPS_UPNP
1308 /* FIX: what exactly should be included in the WLANEvent?
1309 * WPS attributes? Full ProbeReq frame? */
1311 upnp_wps_device_send_wlan_event(
1312 hapd->wps_upnp, addr,
1313 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1314 #endif /* CONFIG_WPS_UPNP */
1317 wpabuf_free(wps_ie);
1323 #ifdef CONFIG_WPS_UPNP
1325 static int hostapd_rx_req_put_wlan_response(
1326 void *priv, enum upnp_wps_wlanevent_type ev_type,
1327 const u8 *mac_addr, const struct wpabuf *msg,
1328 enum wps_msg_type msg_type)
1330 struct hostapd_data *hapd = priv;
1331 struct sta_info *sta;
1332 struct upnp_pending_message *p;
1334 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1335 MACSTR, ev_type, MAC2STR(mac_addr));
1336 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1337 wpabuf_head(msg), wpabuf_len(msg));
1338 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1339 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1340 "PutWLANResponse WLANEventType %d", ev_type);
1345 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1346 * server implementation for delivery to the peer.
1349 sta = ap_get_sta(hapd, mac_addr);
1350 #ifndef CONFIG_WPS_STRICT
1353 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1354 * Pick STA that is in an ongoing WPS registration without
1355 * checking the MAC address.
1357 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1358 "on NewWLANEventMAC; try wildcard match");
1359 for (sta = hapd->sta_list; sta; sta = sta->next) {
1360 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1364 #endif /* CONFIG_WPS_STRICT */
1366 if (!sta || !(sta->flags & WLAN_STA_WPS)) {
1367 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1371 p = os_zalloc(sizeof(*p));
1374 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1375 p->msg = wpabuf_dup(msg);
1377 p->next = hapd->wps->upnp_msgs;
1378 hapd->wps->upnp_msgs = p;
1380 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1384 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1385 struct wps_context *wps)
1387 struct upnp_wps_device_ctx *ctx;
1389 if (!hapd->conf->upnp_iface)
1391 ctx = os_zalloc(sizeof(*ctx));
1395 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1396 if (hapd->conf->ap_pin)
1397 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1399 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1400 hapd->conf->upnp_iface);
1401 if (hapd->wps_upnp == NULL)
1403 wps->wps_upnp = hapd->wps_upnp;
1409 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1411 upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1414 #endif /* CONFIG_WPS_UPNP */
1417 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1418 char *buf, size_t buflen)
1420 if (hapd->wps == NULL)
1422 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1426 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1428 struct hostapd_data *hapd = eloop_data;
1429 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1430 hostapd_wps_ap_pin_disable(hapd);
1431 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
1435 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1437 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1438 hapd->ap_pin_failures = 0;
1439 hapd->ap_pin_failures_consecutive = 0;
1440 hapd->conf->ap_setup_locked = 0;
1441 if (hapd->wps->ap_setup_locked) {
1442 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1443 hapd->wps->ap_setup_locked = 0;
1444 wps_registrar_update_ie(hapd->wps->registrar);
1446 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1448 eloop_register_timeout(timeout, 0,
1449 hostapd_wps_ap_pin_timeout, hapd, NULL);
1453 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1455 os_free(hapd->conf->ap_pin);
1456 hapd->conf->ap_pin = NULL;
1457 #ifdef CONFIG_WPS_UPNP
1458 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1459 #endif /* CONFIG_WPS_UPNP */
1460 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1465 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1467 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1468 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1472 struct wps_ap_pin_data {
1478 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1480 struct wps_ap_pin_data *data = ctx;
1481 os_free(hapd->conf->ap_pin);
1482 hapd->conf->ap_pin = os_strdup(data->pin_txt);
1483 #ifdef CONFIG_WPS_UPNP
1484 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1485 #endif /* CONFIG_WPS_UPNP */
1486 hostapd_wps_ap_pin_enable(hapd, data->timeout);
1491 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1494 struct wps_ap_pin_data data;
1496 pin = wps_generate_pin();
1497 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
1498 data.timeout = timeout;
1499 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1500 return hapd->conf->ap_pin;
1504 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1506 return hapd->conf->ap_pin;
1510 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1513 struct wps_ap_pin_data data;
1516 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1517 if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1519 data.timeout = timeout;
1520 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1524 static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1527 wps_registrar_update_ie(hapd->wps->registrar);
1532 void hostapd_wps_update_ie(struct hostapd_data *hapd)
1534 hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1538 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1539 const char *auth, const char *encr, const char *key)
1541 struct wps_credential cred;
1544 os_memset(&cred, 0, sizeof(cred));
1546 len = os_strlen(ssid);
1547 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1548 hexstr2bin(ssid, cred.ssid, len / 2))
1550 cred.ssid_len = len / 2;
1552 if (os_strncmp(auth, "OPEN", 4) == 0)
1553 cred.auth_type = WPS_AUTH_OPEN;
1554 else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1555 cred.auth_type = WPS_AUTH_WPAPSK;
1556 else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1557 cred.auth_type = WPS_AUTH_WPA2PSK;
1562 if (os_strncmp(encr, "NONE", 4) == 0)
1563 cred.encr_type = WPS_ENCR_NONE;
1564 else if (os_strncmp(encr, "WEP", 3) == 0)
1565 cred.encr_type = WPS_ENCR_WEP;
1566 else if (os_strncmp(encr, "TKIP", 4) == 0)
1567 cred.encr_type = WPS_ENCR_TKIP;
1568 else if (os_strncmp(encr, "CCMP", 4) == 0)
1569 cred.encr_type = WPS_ENCR_AES;
1573 cred.encr_type = WPS_ENCR_NONE;
1576 len = os_strlen(key);
1577 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1578 hexstr2bin(key, cred.key, len / 2))
1580 cred.key_len = len / 2;
1583 return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1587 #ifdef CONFIG_WPS_NFC
1589 struct wps_nfc_password_token_data {
1590 const u8 *oob_dev_pw;
1591 size_t oob_dev_pw_len;
1596 static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1598 struct wps_nfc_password_token_data *data = ctx;
1601 if (hapd->wps == NULL)
1603 ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1605 data->oob_dev_pw_len);
1612 static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
1613 struct wps_parse_attr *attr)
1615 struct wps_nfc_password_token_data data;
1617 data.oob_dev_pw = attr->oob_dev_password;
1618 data.oob_dev_pw_len = attr->oob_dev_password_len;
1620 if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
1622 return data.added ? 0 : -1;
1626 static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
1627 const struct wpabuf *wps)
1629 struct wps_parse_attr attr;
1631 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
1633 if (wps_parse_msg(wps, &attr)) {
1634 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
1638 if (attr.oob_dev_password)
1639 return hostapd_wps_add_nfc_password_token(hapd, &attr);
1641 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
1646 int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
1647 const struct wpabuf *data)
1649 const struct wpabuf *wps = data;
1650 struct wpabuf *tmp = NULL;
1653 if (wpabuf_len(data) < 4)
1656 if (*wpabuf_head_u8(data) != 0x10) {
1657 /* Assume this contains full NDEF record */
1658 tmp = ndef_parse_wifi(data);
1660 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
1666 ret = hostapd_wps_nfc_tag_process(hapd, wps);
1672 struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
1677 if (hapd->wps == NULL)
1680 ret = wps_get_oob_cred(hapd->wps);
1683 tmp = ndef_build_wifi(ret);
1694 struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
1697 * Handover Select carrier record for WPS uses the same format as
1698 * configuration token.
1700 return hostapd_wps_nfc_config_token(hapd, ndef);
1704 struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
1706 if (hapd->conf->wps_nfc_pw_from_config) {
1707 return wps_nfc_token_build(ndef,
1708 hapd->conf->wps_nfc_dev_pw_id,
1709 hapd->conf->wps_nfc_dh_pubkey,
1710 hapd->conf->wps_nfc_dev_pw);
1713 return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
1714 &hapd->conf->wps_nfc_dh_pubkey,
1715 &hapd->conf->wps_nfc_dh_privkey,
1716 &hapd->conf->wps_nfc_dev_pw);
1720 int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
1722 struct wps_context *wps = hapd->wps;
1728 if (!hapd->conf->wps_nfc_dh_pubkey ||
1729 !hapd->conf->wps_nfc_dh_privkey ||
1730 !hapd->conf->wps_nfc_dev_pw ||
1731 !hapd->conf->wps_nfc_dev_pw_id)
1734 hostapd_wps_nfc_clear(wps);
1735 wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
1736 wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1737 wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
1738 pw = hapd->conf->wps_nfc_dev_pw;
1739 wps->ap_nfc_dev_pw = wpabuf_alloc(
1740 wpabuf_len(pw) * 2 + 1);
1741 if (wps->ap_nfc_dev_pw) {
1742 wpa_snprintf_hex_uppercase(
1743 (char *) wpabuf_put(wps->ap_nfc_dev_pw,
1744 wpabuf_len(pw) * 2),
1745 wpabuf_len(pw) * 2 + 1,
1746 wpabuf_head(pw), wpabuf_len(pw));
1749 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
1750 !wps->ap_nfc_dev_pw) {
1751 hostapd_wps_nfc_clear(wps);
1759 void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
1761 hostapd_wps_nfc_clear(hapd->wps);
1764 #endif /* CONFIG_WPS_NFC */