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);
48 struct hostapd_data *calling_hapd;
52 static int wps_for_each(struct hostapd_iface *iface, void *ctx)
54 struct wps_for_each_data *data = ctx;
59 for (j = 0; j < iface->num_bss; j++) {
60 struct hostapd_data *hapd = iface->bss[j];
63 if (hapd != data->calling_hapd &&
64 (hapd->conf->wps_independent ||
65 data->calling_hapd->conf->wps_independent))
68 ret = data->func(hapd, data->ctx);
77 static int hostapd_wps_for_each(struct hostapd_data *hapd,
78 int (*func)(struct hostapd_data *h, void *ctx),
81 struct hostapd_iface *iface = hapd->iface;
82 struct wps_for_each_data data;
85 data.calling_hapd = hapd;
86 if (iface->interfaces == NULL ||
87 iface->interfaces->for_each_interface == NULL)
88 return wps_for_each(iface, &data);
89 return iface->interfaces->for_each_interface(iface->interfaces,
94 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
95 const u8 *p2p_dev_addr, const u8 *psk,
98 struct hostapd_data *hapd = ctx;
99 struct hostapd_wpa_psk *p;
100 struct hostapd_ssid *ssid = &hapd->conf->ssid;
102 if (is_zero_ether_addr(p2p_dev_addr)) {
103 wpa_printf(MSG_DEBUG,
104 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
107 wpa_printf(MSG_DEBUG,
108 "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
109 " P2P Device Addr " MACSTR,
110 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
112 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
114 if (psk_len != PMK_LEN) {
115 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
116 (unsigned long) psk_len);
120 /* Add the new PSK to runtime PSK list */
121 p = os_zalloc(sizeof(*p));
124 os_memcpy(p->addr, mac_addr, ETH_ALEN);
125 os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
126 os_memcpy(p->psk, psk, PMK_LEN);
128 if (hapd->new_psk_cb) {
129 hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr,
133 p->next = ssid->wpa_psk;
136 if (ssid->wpa_psk_file) {
138 char hex[PMK_LEN * 2 + 1];
139 /* Add the new PSK to PSK list file */
140 f = fopen(ssid->wpa_psk_file, "a");
142 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
143 "'%s'", ssid->wpa_psk_file);
147 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
148 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
156 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
157 struct wpabuf *probe_resp_ie)
159 struct hostapd_data *hapd = ctx;
160 wpabuf_free(hapd->wps_beacon_ie);
161 hapd->wps_beacon_ie = beacon_ie;
162 wpabuf_free(hapd->wps_probe_resp_ie);
163 hapd->wps_probe_resp_ie = probe_resp_ie;
164 if (hapd->beacon_set_done)
165 ieee802_11_set_beacon(hapd);
166 return hostapd_set_ap_wps_ie(hapd);
170 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
171 const struct wps_device_data *dev)
173 struct hostapd_data *hapd = ctx;
174 char uuid[40], txt[400];
176 char devtype[WPS_DEV_TYPE_BUFSIZE];
177 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
179 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
180 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
181 "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
182 uuid, MAC2STR(dev->mac_addr), dev->device_name,
183 dev->manufacturer, dev->model_name,
184 dev->model_number, dev->serial_number,
185 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
187 if (len > 0 && len < (int) sizeof(txt))
188 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
190 if (hapd->conf->wps_pin_requests) {
193 f = fopen(hapd->conf->wps_pin_requests, "a");
197 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
199 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
200 dev->manufacturer, dev->model_name, dev->model_number,
202 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
209 struct wps_stop_reg_data {
210 struct hostapd_data *current_hapd;
216 static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
218 struct wps_stop_reg_data *data = ctx;
219 if (hapd != data->current_hapd && hapd->wps != NULL)
220 wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
221 data->dev_pw, data->dev_pw_len);
226 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
227 const u8 *uuid_e, const u8 *dev_pw,
230 struct hostapd_data *hapd = ctx;
232 struct wps_stop_reg_data data;
233 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
235 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
236 MAC2STR(mac_addr), uuid);
237 if (hapd->wps_reg_success_cb)
238 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
240 data.current_hapd = hapd;
241 data.uuid_e = uuid_e;
242 data.dev_pw = dev_pw;
243 data.dev_pw_len = dev_pw_len;
244 hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
248 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
250 const u8 *pri_dev_type,
252 u16 dev_password_id, u8 request_type,
253 const char *dev_name)
255 struct hostapd_data *hapd = ctx;
257 char devtype[WPS_DEV_TYPE_BUFSIZE];
258 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
260 if (dev_name == NULL)
262 wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
263 " %s %s 0x%x %u %u [%s]",
265 wps_dev_type_bin2str(pri_dev_type, devtype,
267 config_methods, dev_password_id, request_type, dev_name);
271 static int str_starts(const char *str, const char *start)
273 return os_strncmp(str, start, os_strlen(start)) == 0;
277 static void wps_reload_config(void *eloop_data, void *user_ctx)
279 struct hostapd_iface *iface = eloop_data;
281 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
282 if (iface->interfaces == NULL ||
283 iface->interfaces->reload_config(iface) < 0) {
284 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
290 void hostapd_wps_eap_completed(struct hostapd_data *hapd)
293 * Reduce race condition of the station trying to reconnect immediately
294 * after AP reconfiguration through WPS by rescheduling the reload
295 * timeout to happen after EAP completion rather than the originally
296 * scheduled 100 ms after new configuration became known.
298 if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) ==
300 wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload");
304 static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
307 size_t blen = attr_len * 2 + 1;
308 char *buf = os_malloc(blen);
310 wpa_snprintf_hex(buf, blen, attr, attr_len);
311 wpa_msg(hapd->msg_ctx, MSG_INFO,
312 WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
318 static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
319 const struct wps_credential *cred)
321 struct hostapd_bss_config *bss = hapd->conf;
323 wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
326 if (cred->ssid_len <= HOSTAPD_MAX_SSID_LEN) {
327 os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
328 bss->ssid.ssid_len = cred->ssid_len;
329 bss->ssid.ssid_set = 1;
332 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
333 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
335 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
337 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
343 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
344 bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
345 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
346 bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
348 bss->wpa_pairwise = 0;
349 if (cred->encr_type & WPS_ENCR_AES)
350 bss->wpa_pairwise |= WPA_CIPHER_CCMP;
351 if (cred->encr_type & WPS_ENCR_TKIP)
352 bss->wpa_pairwise |= WPA_CIPHER_TKIP;
353 bss->rsn_pairwise = bss->wpa_pairwise;
354 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
358 if (cred->key_len >= 8 && cred->key_len < 64) {
359 os_free(bss->ssid.wpa_passphrase);
360 bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
361 if (bss->ssid.wpa_passphrase)
362 os_memcpy(bss->ssid.wpa_passphrase, cred->key,
364 os_free(bss->ssid.wpa_psk);
365 bss->ssid.wpa_psk = NULL;
366 } else if (cred->key_len == 64) {
367 os_free(bss->ssid.wpa_psk);
369 os_zalloc(sizeof(struct hostapd_wpa_psk));
370 if (bss->ssid.wpa_psk &&
371 hexstr2bin((const char *) cred->key,
372 bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
373 bss->ssid.wpa_psk->group = 1;
374 os_free(bss->ssid.wpa_passphrase);
375 bss->ssid.wpa_passphrase = NULL;
380 if ((cred->auth_type & WPS_AUTH_OPEN) &&
381 (cred->auth_type & WPS_AUTH_SHARED))
383 else if (cred->auth_type & WPS_AUTH_SHARED)
387 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx > 0 &&
388 cred->key_idx <= 4) {
389 struct hostapd_wep_keys *wep = &bss->ssid.wep;
390 int idx = cred->key_idx;
394 if (cred->key_len == 10 || cred->key_len == 26) {
395 os_free(wep->key[idx]);
396 wep->key[idx] = os_malloc(cred->key_len / 2);
397 if (wep->key[idx] == NULL ||
398 hexstr2bin((const char *) cred->key,
402 wep->len[idx] = cred->key_len / 2;
404 os_free(wep->key[idx]);
405 wep->key[idx] = os_malloc(cred->key_len);
406 if (wep->key[idx] == NULL)
408 os_memcpy(wep->key[idx], cred->key,
410 wep->len[idx] = cred->key_len;
416 /* Schedule configuration reload after short period of time to allow
417 * EAP-WSC to be finished.
419 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
426 static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
428 const struct wps_credential *cred = ctx;
436 if (hapd->wps == NULL)
439 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
440 cred->cred_attr, cred->cred_attr_len);
442 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
443 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
444 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
446 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
447 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
448 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
449 cred->key, cred->key_len);
450 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
451 MAC2STR(cred->mac_addr));
453 if ((hapd->conf->wps_cred_processing == 1 ||
454 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
455 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
456 } else if (hapd->conf->wps_cred_processing == 1 ||
457 hapd->conf->wps_cred_processing == 2) {
459 attr = wpabuf_alloc(200);
460 if (attr && wps_build_credential_wrap(attr, cred) == 0)
461 hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
465 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
467 if (hapd->conf->wps_cred_processing == 1)
470 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
471 hapd->wps->ssid_len = cred->ssid_len;
472 hapd->wps->encr_types = cred->encr_type;
473 hapd->wps->auth_types = cred->auth_type;
474 if (cred->key_len == 0) {
475 os_free(hapd->wps->network_key);
476 hapd->wps->network_key = NULL;
477 hapd->wps->network_key_len = 0;
479 if (hapd->wps->network_key == NULL ||
480 hapd->wps->network_key_len < cred->key_len) {
481 hapd->wps->network_key_len = 0;
482 os_free(hapd->wps->network_key);
483 hapd->wps->network_key = os_malloc(cred->key_len);
484 if (hapd->wps->network_key == NULL)
487 hapd->wps->network_key_len = cred->key_len;
488 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
490 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
492 if (hapd->iface->config_fname == NULL)
493 return hapd_wps_reconfig_in_memory(hapd, cred);
494 len = os_strlen(hapd->iface->config_fname) + 5;
495 tmp_fname = os_malloc(len);
496 if (tmp_fname == NULL)
498 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
500 oconf = fopen(hapd->iface->config_fname, "r");
502 wpa_printf(MSG_WARNING, "WPS: Could not open current "
503 "configuration file");
508 nconf = fopen(tmp_fname, "w");
510 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
511 "configuration file");
517 fprintf(nconf, "# WPS configuration - START\n");
519 fprintf(nconf, "wps_state=2\n");
521 if (is_hex(cred->ssid, cred->ssid_len)) {
522 fprintf(nconf, "ssid2=");
523 for (i = 0; i < cred->ssid_len; i++)
524 fprintf(nconf, "%02x", cred->ssid[i]);
525 fprintf(nconf, "\n");
527 fprintf(nconf, "ssid=");
528 for (i = 0; i < cred->ssid_len; i++)
529 fputc(cred->ssid[i], nconf);
530 fprintf(nconf, "\n");
533 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
534 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
536 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
538 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
545 fprintf(nconf, "wpa=%d\n", wpa);
547 fprintf(nconf, "wpa_key_mgmt=");
549 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
550 fprintf(nconf, "WPA-EAP");
553 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
554 fprintf(nconf, "%sWPA-PSK", prefix);
555 fprintf(nconf, "\n");
557 fprintf(nconf, "wpa_pairwise=");
559 if (cred->encr_type & WPS_ENCR_AES) {
560 fprintf(nconf, "CCMP");
563 if (cred->encr_type & WPS_ENCR_TKIP) {
564 fprintf(nconf, "%sTKIP", prefix);
566 fprintf(nconf, "\n");
568 if (cred->key_len >= 8 && cred->key_len < 64) {
569 fprintf(nconf, "wpa_passphrase=");
570 for (i = 0; i < cred->key_len; i++)
571 fputc(cred->key[i], nconf);
572 fprintf(nconf, "\n");
573 } else if (cred->key_len == 64) {
574 fprintf(nconf, "wpa_psk=");
575 for (i = 0; i < cred->key_len; i++)
576 fputc(cred->key[i], nconf);
577 fprintf(nconf, "\n");
579 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
581 (unsigned long) cred->key_len);
584 fprintf(nconf, "auth_algs=1\n");
586 if ((cred->auth_type & WPS_AUTH_OPEN) &&
587 (cred->auth_type & WPS_AUTH_SHARED))
588 fprintf(nconf, "auth_algs=3\n");
589 else if (cred->auth_type & WPS_AUTH_SHARED)
590 fprintf(nconf, "auth_algs=2\n");
592 fprintf(nconf, "auth_algs=1\n");
594 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
595 int key_idx = cred->key_idx;
598 fprintf(nconf, "wep_default_key=%d\n", key_idx);
599 fprintf(nconf, "wep_key%d=", key_idx);
600 if (cred->key_len == 10 || cred->key_len == 26) {
601 /* WEP key as a hex string */
602 for (i = 0; i < cred->key_len; i++)
603 fputc(cred->key[i], nconf);
605 /* Raw WEP key; convert to hex */
606 for (i = 0; i < cred->key_len; i++)
607 fprintf(nconf, "%02x", cred->key[i]);
609 fprintf(nconf, "\n");
613 fprintf(nconf, "# WPS configuration - END\n");
616 while (fgets(buf, sizeof(buf), oconf)) {
617 if (os_strncmp(buf, "bss=", 4) == 0)
620 (str_starts(buf, "ssid=") ||
621 str_starts(buf, "ssid2=") ||
622 str_starts(buf, "auth_algs=") ||
623 str_starts(buf, "wep_default_key=") ||
624 str_starts(buf, "wep_key") ||
625 str_starts(buf, "wps_state=") ||
626 str_starts(buf, "wpa=") ||
627 str_starts(buf, "wpa_psk=") ||
628 str_starts(buf, "wpa_pairwise=") ||
629 str_starts(buf, "rsn_pairwise=") ||
630 str_starts(buf, "wpa_key_mgmt=") ||
631 str_starts(buf, "wpa_passphrase="))) {
632 fprintf(nconf, "#WPS# %s", buf);
634 fprintf(nconf, "%s", buf);
640 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
641 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
642 "configuration file: %s", strerror(errno));
649 /* Schedule configuration reload after short period of time to allow
650 * EAP-WSC to be finished.
652 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
655 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
661 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
663 struct hostapd_data *hapd = ctx;
664 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
668 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
670 struct hostapd_data *hapd = eloop_data;
672 if (hapd->conf->ap_setup_locked)
674 if (hapd->ap_pin_failures_consecutive >= 10)
677 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
678 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
679 hapd->wps->ap_setup_locked = 0;
680 wps_registrar_update_ie(hapd->wps->registrar);
684 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
686 struct wps_event_pwd_auth_fail *data = ctx;
688 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
692 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
693 * for some time if this happens multiple times to slow down brute
696 hapd->ap_pin_failures++;
697 hapd->ap_pin_failures_consecutive++;
698 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
700 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
701 if (hapd->ap_pin_failures < 3)
704 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
705 hapd->wps->ap_setup_locked = 1;
707 wps_registrar_update_ie(hapd->wps->registrar);
709 if (!hapd->conf->ap_setup_locked &&
710 hapd->ap_pin_failures_consecutive >= 10) {
712 * In indefinite lockdown - disable automatic AP PIN
715 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
716 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
717 } else if (!hapd->conf->ap_setup_locked) {
718 if (hapd->ap_pin_lockout_time == 0)
719 hapd->ap_pin_lockout_time = 60;
720 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
721 (hapd->ap_pin_failures % 3) == 0)
722 hapd->ap_pin_lockout_time *= 2;
724 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
725 hapd->ap_pin_lockout_time);
726 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
727 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
728 hostapd_wps_reenable_ap_pin, hapd,
736 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
737 struct wps_event_pwd_auth_fail *data)
739 /* Update WPS Status - Authentication Failure */
740 wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
741 hapd->wps_stats.status = WPS_STATUS_FAILURE;
742 hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
743 os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
745 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
749 static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
751 if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
754 if (hapd->ap_pin_failures_consecutive == 0)
757 wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
758 "- total validation failures %u (%u consecutive)",
759 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
760 hapd->ap_pin_failures_consecutive = 0;
766 static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
768 hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
772 static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
774 /* Update WPS Status - PBC Overlap */
775 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
779 static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
781 /* Update WPS PBC Status:PBC Timeout */
782 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
786 static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
788 /* Update WPS PBC status - Active */
789 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
793 static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
795 /* Update WPS PBC status - Active */
796 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
800 static void hostapd_wps_event_success(struct hostapd_data *hapd,
801 struct wps_event_success *success)
803 /* Update WPS status - Success */
804 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
805 hapd->wps_stats.status = WPS_STATUS_SUCCESS;
806 os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
810 static void hostapd_wps_event_fail(struct hostapd_data *hapd,
811 struct wps_event_fail *fail)
813 /* Update WPS status - Failure */
814 hapd->wps_stats.status = WPS_STATUS_FAILURE;
815 os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
817 hapd->wps_stats.failure_reason = fail->error_indication;
819 if (fail->error_indication > 0 &&
820 fail->error_indication < NUM_WPS_EI_VALUES) {
821 wpa_msg(hapd->msg_ctx, MSG_INFO,
822 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
823 fail->msg, fail->config_error, fail->error_indication,
824 wps_ei_str(fail->error_indication));
826 wpa_msg(hapd->msg_ctx, MSG_INFO,
827 WPS_EVENT_FAIL "msg=%d config_error=%d",
828 fail->msg, fail->config_error);
833 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
834 union wps_event_data *data)
836 struct hostapd_data *hapd = ctx;
840 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
843 hostapd_wps_event_fail(hapd, &data->fail);
846 hostapd_wps_event_success(hapd, &data->success);
847 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
849 case WPS_EV_PWD_AUTH_FAIL:
850 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
852 case WPS_EV_PBC_OVERLAP:
853 hostapd_wps_event_pbc_overlap(hapd);
854 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
856 case WPS_EV_PBC_TIMEOUT:
857 hostapd_wps_event_pbc_timeout(hapd);
858 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
860 case WPS_EV_PBC_ACTIVE:
861 hostapd_wps_event_pbc_active(hapd);
862 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
864 case WPS_EV_PBC_DISABLE:
865 hostapd_wps_event_pbc_disable(hapd);
866 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
868 case WPS_EV_ER_AP_ADD:
870 case WPS_EV_ER_AP_REMOVE:
872 case WPS_EV_ER_ENROLLEE_ADD:
874 case WPS_EV_ER_ENROLLEE_REMOVE:
876 case WPS_EV_ER_AP_SETTINGS:
878 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
880 case WPS_EV_AP_PIN_SUCCESS:
881 hostapd_wps_ap_pin_success(hapd);
884 if (hapd->wps_event_cb)
885 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
889 static int hostapd_wps_rf_band_cb(void *ctx)
891 struct hostapd_data *hapd = ctx;
893 return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
894 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
898 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
900 wpabuf_free(hapd->wps_beacon_ie);
901 hapd->wps_beacon_ie = NULL;
903 wpabuf_free(hapd->wps_probe_resp_ie);
904 hapd->wps_probe_resp_ie = NULL;
906 hostapd_set_ap_wps_ie(hapd);
910 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
912 const u8 **uuid = ctx;
917 for (j = 0; j < iface->num_bss; j++) {
918 struct hostapd_data *hapd = iface->bss[j];
919 if (hapd->wps && !hapd->conf->wps_independent &&
920 !is_nil_uuid(hapd->wps->uuid)) {
921 *uuid = hapd->wps->uuid;
930 static const u8 * get_own_uuid(struct hostapd_iface *iface)
933 if (iface->interfaces == NULL ||
934 iface->interfaces->for_each_interface == NULL)
937 iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
943 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
951 static int interface_count(struct hostapd_iface *iface)
954 if (iface->interfaces == NULL ||
955 iface->interfaces->for_each_interface == NULL)
957 iface->interfaces->for_each_interface(iface->interfaces,
958 count_interface_cb, &count);
963 static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
964 struct wps_context *wps)
968 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
969 wpabuf_free(wps->dev.vendor_ext[i]);
970 wps->dev.vendor_ext[i] = NULL;
972 if (hapd->conf->wps_vendor_ext[i] == NULL)
975 wps->dev.vendor_ext[i] =
976 wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
977 if (wps->dev.vendor_ext[i] == NULL) {
979 wpabuf_free(wps->dev.vendor_ext[i]);
988 int hostapd_init_wps(struct hostapd_data *hapd,
989 struct hostapd_bss_config *conf)
991 struct wps_context *wps;
992 struct wps_registrar_config cfg;
994 if (conf->wps_state == 0) {
995 hostapd_wps_clear_ies(hapd);
999 wps = os_zalloc(sizeof(*wps));
1003 wps->cred_cb = hostapd_wps_cred_cb;
1004 wps->event_cb = hostapd_wps_event_cb;
1005 wps->rf_band_cb = hostapd_wps_rf_band_cb;
1008 os_memset(&cfg, 0, sizeof(cfg));
1009 wps->wps_state = hapd->conf->wps_state;
1010 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
1011 if (is_nil_uuid(hapd->conf->uuid)) {
1013 uuid = get_own_uuid(hapd->iface);
1014 if (uuid && !conf->wps_independent) {
1015 os_memcpy(wps->uuid, uuid, UUID_LEN);
1016 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
1017 "interface", wps->uuid, UUID_LEN);
1019 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
1020 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1021 "address", wps->uuid, UUID_LEN);
1024 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
1025 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
1026 wps->uuid, UUID_LEN);
1028 wps->ssid_len = hapd->conf->ssid.ssid_len;
1029 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
1031 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
1032 wps->dev.device_name = hapd->conf->device_name ?
1033 os_strdup(hapd->conf->device_name) : NULL;
1034 wps->dev.manufacturer = hapd->conf->manufacturer ?
1035 os_strdup(hapd->conf->manufacturer) : NULL;
1036 wps->dev.model_name = hapd->conf->model_name ?
1037 os_strdup(hapd->conf->model_name) : NULL;
1038 wps->dev.model_number = hapd->conf->model_number ?
1039 os_strdup(hapd->conf->model_number) : NULL;
1040 wps->dev.serial_number = hapd->conf->serial_number ?
1041 os_strdup(hapd->conf->serial_number) : NULL;
1042 wps->config_methods =
1043 wps_config_methods_str2bin(hapd->conf->config_methods);
1045 if ((wps->config_methods &
1046 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1047 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1048 wpa_printf(MSG_INFO, "WPS: Converting display to "
1049 "virtual_display for WPS 2.0 compliance");
1050 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1052 if ((wps->config_methods &
1053 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1054 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1055 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1056 "virtual_push_button for WPS 2.0 compliance");
1057 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1059 #endif /* CONFIG_WPS2 */
1060 os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
1063 if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) {
1068 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
1070 if (conf->wps_rf_bands) {
1071 wps->dev.rf_bands = conf->wps_rf_bands;
1074 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
1075 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
1078 if (conf->wpa & WPA_PROTO_RSN) {
1079 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1080 wps->auth_types |= WPS_AUTH_WPA2PSK;
1081 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1082 wps->auth_types |= WPS_AUTH_WPA2;
1084 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
1085 wps->encr_types |= WPS_ENCR_AES;
1086 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
1087 wps->encr_types |= WPS_ENCR_TKIP;
1090 if (conf->wpa & WPA_PROTO_WPA) {
1091 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1092 wps->auth_types |= WPS_AUTH_WPAPSK;
1093 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1094 wps->auth_types |= WPS_AUTH_WPA;
1096 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
1097 wps->encr_types |= WPS_ENCR_AES;
1098 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
1099 wps->encr_types |= WPS_ENCR_TKIP;
1102 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
1103 wps->encr_types |= WPS_ENCR_NONE;
1104 wps->auth_types |= WPS_AUTH_OPEN;
1105 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
1106 wps->encr_types |= WPS_ENCR_WEP;
1107 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
1108 wps->auth_types |= WPS_AUTH_OPEN;
1109 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
1110 wps->auth_types |= WPS_AUTH_SHARED;
1111 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
1112 wps->auth_types |= WPS_AUTH_OPEN;
1113 if (conf->default_wep_key_len)
1114 wps->encr_types |= WPS_ENCR_WEP;
1116 wps->encr_types |= WPS_ENCR_NONE;
1119 if (conf->ssid.wpa_psk_file) {
1120 /* Use per-device PSKs */
1121 } else if (conf->ssid.wpa_passphrase) {
1122 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1123 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1124 } else if (conf->ssid.wpa_psk) {
1125 wps->network_key = os_malloc(2 * PMK_LEN + 1);
1126 if (wps->network_key == NULL) {
1130 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1131 conf->ssid.wpa_psk->psk, PMK_LEN);
1132 wps->network_key_len = 2 * PMK_LEN;
1133 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1134 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
1135 if (wps->network_key == NULL) {
1139 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1140 conf->ssid.wep.len[0]);
1141 wps->network_key_len = conf->ssid.wep.len[0];
1144 if (conf->ssid.wpa_psk) {
1145 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1149 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
1150 /* Override parameters to enable security by default */
1151 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1152 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1155 wps->ap_settings = conf->ap_settings;
1156 wps->ap_settings_len = conf->ap_settings_len;
1158 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
1159 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
1160 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
1161 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
1162 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
1164 cfg.skip_cred_build = conf->skip_cred_build;
1165 cfg.extra_cred = conf->extra_cred;
1166 cfg.extra_cred_len = conf->extra_cred_len;
1167 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
1168 conf->skip_cred_build;
1169 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
1170 cfg.static_wep_only = 1;
1171 cfg.dualband = interface_count(hapd->iface) > 1;
1172 if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
1173 (WPS_RF_50GHZ | WPS_RF_24GHZ))
1176 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
1177 cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
1179 wps->registrar = wps_registrar_init(wps, &cfg);
1180 if (wps->registrar == NULL) {
1181 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
1182 os_free(wps->network_key);
1187 #ifdef CONFIG_WPS_UPNP
1188 wps->friendly_name = hapd->conf->friendly_name;
1189 wps->manufacturer_url = hapd->conf->manufacturer_url;
1190 wps->model_description = hapd->conf->model_description;
1191 wps->model_url = hapd->conf->model_url;
1192 wps->upc = hapd->conf->upc;
1193 #endif /* CONFIG_WPS_UPNP */
1195 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
1203 int hostapd_init_wps_complete(struct hostapd_data *hapd)
1205 struct wps_context *wps = hapd->wps;
1210 #ifdef CONFIG_WPS_UPNP
1211 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
1212 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
1213 wps_registrar_deinit(wps->registrar);
1214 os_free(wps->network_key);
1219 #endif /* CONFIG_WPS_UPNP */
1225 static void hostapd_wps_nfc_clear(struct wps_context *wps)
1227 #ifdef CONFIG_WPS_NFC
1228 wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
1229 wps->ap_nfc_dev_pw_id = 0;
1230 wpabuf_free(wps->ap_nfc_dh_pubkey);
1231 wps->ap_nfc_dh_pubkey = NULL;
1232 wpabuf_free(wps->ap_nfc_dh_privkey);
1233 wps->ap_nfc_dh_privkey = NULL;
1234 wpabuf_free(wps->ap_nfc_dev_pw);
1235 wps->ap_nfc_dev_pw = NULL;
1236 #endif /* CONFIG_WPS_NFC */
1240 void hostapd_deinit_wps(struct hostapd_data *hapd)
1242 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1243 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1244 eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
1245 if (hapd->wps == NULL)
1247 #ifdef CONFIG_WPS_UPNP
1248 hostapd_wps_upnp_deinit(hapd);
1249 #endif /* CONFIG_WPS_UPNP */
1250 wps_registrar_deinit(hapd->wps->registrar);
1251 os_free(hapd->wps->network_key);
1252 wps_device_data_free(&hapd->wps->dev);
1253 wpabuf_free(hapd->wps->dh_pubkey);
1254 wpabuf_free(hapd->wps->dh_privkey);
1255 wps_free_pending_msgs(hapd->wps->upnp_msgs);
1256 hostapd_wps_nfc_clear(hapd->wps);
1259 hostapd_wps_clear_ies(hapd);
1263 void hostapd_update_wps(struct hostapd_data *hapd)
1265 if (hapd->wps == NULL)
1268 #ifdef CONFIG_WPS_UPNP
1269 hapd->wps->friendly_name = hapd->conf->friendly_name;
1270 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
1271 hapd->wps->model_description = hapd->conf->model_description;
1272 hapd->wps->model_url = hapd->conf->model_url;
1273 hapd->wps->upc = hapd->conf->upc;
1274 #endif /* CONFIG_WPS_UPNP */
1276 hostapd_wps_set_vendor_ext(hapd, hapd->wps);
1278 if (hapd->conf->wps_state)
1279 wps_registrar_update_ie(hapd->wps->registrar);
1281 hostapd_deinit_wps(hapd);
1285 struct wps_add_pin_data {
1295 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1297 struct wps_add_pin_data *data = ctx;
1300 if (hapd->wps == NULL)
1302 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1303 data->uuid, data->pin, data->pin_len,
1311 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1312 const char *uuid, const char *pin, int timeout)
1315 struct wps_add_pin_data data;
1319 data.pin = (const u8 *) pin;
1320 data.pin_len = os_strlen(pin);
1321 data.timeout = timeout;
1324 if (os_strcmp(uuid, "any") == 0)
1327 if (uuid_str2bin(uuid, u))
1331 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1333 return data.added ? 0 : -1;
1337 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1339 const u8 *p2p_dev_addr = ctx;
1340 if (hapd->wps == NULL)
1342 return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
1346 int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1347 const u8 *p2p_dev_addr)
1349 return hostapd_wps_for_each(hapd, wps_button_pushed,
1350 (void *) p2p_dev_addr);
1354 static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1356 if (hapd->wps == NULL)
1359 wps_registrar_wps_cancel(hapd->wps->registrar);
1360 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
1366 int hostapd_wps_cancel(struct hostapd_data *hapd)
1368 return hostapd_wps_for_each(hapd, wps_cancel, NULL);
1372 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1374 const u8 *ie, size_t ie_len,
1377 struct hostapd_data *hapd = ctx;
1378 struct wpabuf *wps_ie;
1379 struct ieee802_11_elems elems;
1381 if (hapd->wps == NULL)
1384 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1385 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1386 MACSTR, MAC2STR(addr));
1390 if (elems.ssid && elems.ssid_len > 0 &&
1391 (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1392 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1394 return 0; /* Not for us */
1396 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1399 if (wps_validate_probe_req(wps_ie, addr) < 0) {
1400 wpabuf_free(wps_ie);
1404 if (wpabuf_len(wps_ie) > 0) {
1405 int p2p_wildcard = 0;
1407 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1408 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1409 P2P_WILDCARD_SSID_LEN) == 0)
1411 #endif /* CONFIG_P2P */
1412 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1414 #ifdef CONFIG_WPS_UPNP
1415 /* FIX: what exactly should be included in the WLANEvent?
1416 * WPS attributes? Full ProbeReq frame? */
1418 upnp_wps_device_send_wlan_event(
1419 hapd->wps_upnp, addr,
1420 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1421 #endif /* CONFIG_WPS_UPNP */
1424 wpabuf_free(wps_ie);
1430 #ifdef CONFIG_WPS_UPNP
1432 static int hostapd_rx_req_put_wlan_response(
1433 void *priv, enum upnp_wps_wlanevent_type ev_type,
1434 const u8 *mac_addr, const struct wpabuf *msg,
1435 enum wps_msg_type msg_type)
1437 struct hostapd_data *hapd = priv;
1438 struct sta_info *sta;
1439 struct upnp_pending_message *p;
1441 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1442 MACSTR, ev_type, MAC2STR(mac_addr));
1443 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1444 wpabuf_head(msg), wpabuf_len(msg));
1445 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1446 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1447 "PutWLANResponse WLANEventType %d", ev_type);
1452 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1453 * server implementation for delivery to the peer.
1456 sta = ap_get_sta(hapd, mac_addr);
1457 #ifndef CONFIG_WPS_STRICT
1460 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1461 * Pick STA that is in an ongoing WPS registration without
1462 * checking the MAC address.
1464 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1465 "on NewWLANEventMAC; try wildcard match");
1466 for (sta = hapd->sta_list; sta; sta = sta->next) {
1467 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1471 #endif /* CONFIG_WPS_STRICT */
1473 if (!sta || !(sta->flags & WLAN_STA_WPS)) {
1474 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1478 p = os_zalloc(sizeof(*p));
1481 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1482 p->msg = wpabuf_dup(msg);
1484 p->next = hapd->wps->upnp_msgs;
1485 hapd->wps->upnp_msgs = p;
1487 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1491 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1492 struct wps_context *wps)
1494 struct upnp_wps_device_ctx *ctx;
1496 if (!hapd->conf->upnp_iface)
1498 ctx = os_zalloc(sizeof(*ctx));
1502 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1503 if (hapd->conf->ap_pin)
1504 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1506 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1507 hapd->conf->upnp_iface);
1508 if (hapd->wps_upnp == NULL)
1510 wps->wps_upnp = hapd->wps_upnp;
1516 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1518 upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1521 #endif /* CONFIG_WPS_UPNP */
1524 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1525 char *buf, size_t buflen)
1527 if (hapd->wps == NULL)
1529 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1533 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1535 struct hostapd_data *hapd = eloop_data;
1536 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1537 hostapd_wps_ap_pin_disable(hapd);
1538 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
1542 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1544 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1545 hapd->ap_pin_failures = 0;
1546 hapd->ap_pin_failures_consecutive = 0;
1547 hapd->conf->ap_setup_locked = 0;
1548 if (hapd->wps->ap_setup_locked) {
1549 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1550 hapd->wps->ap_setup_locked = 0;
1551 wps_registrar_update_ie(hapd->wps->registrar);
1553 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1555 eloop_register_timeout(timeout, 0,
1556 hostapd_wps_ap_pin_timeout, hapd, NULL);
1560 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1562 os_free(hapd->conf->ap_pin);
1563 hapd->conf->ap_pin = NULL;
1564 #ifdef CONFIG_WPS_UPNP
1565 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1566 #endif /* CONFIG_WPS_UPNP */
1567 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1572 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1574 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1575 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1579 struct wps_ap_pin_data {
1585 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1587 struct wps_ap_pin_data *data = ctx;
1588 os_free(hapd->conf->ap_pin);
1589 hapd->conf->ap_pin = os_strdup(data->pin_txt);
1590 #ifdef CONFIG_WPS_UPNP
1591 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1592 #endif /* CONFIG_WPS_UPNP */
1593 hostapd_wps_ap_pin_enable(hapd, data->timeout);
1598 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1601 struct wps_ap_pin_data data;
1603 pin = wps_generate_pin();
1604 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
1605 data.timeout = timeout;
1606 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1607 return hapd->conf->ap_pin;
1611 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1613 return hapd->conf->ap_pin;
1617 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1620 struct wps_ap_pin_data data;
1623 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1624 if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1626 data.timeout = timeout;
1627 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1631 static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1634 wps_registrar_update_ie(hapd->wps->registrar);
1639 void hostapd_wps_update_ie(struct hostapd_data *hapd)
1641 hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1645 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1646 const char *auth, const char *encr, const char *key)
1648 struct wps_credential cred;
1651 os_memset(&cred, 0, sizeof(cred));
1653 len = os_strlen(ssid);
1654 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1655 hexstr2bin(ssid, cred.ssid, len / 2))
1657 cred.ssid_len = len / 2;
1659 if (os_strncmp(auth, "OPEN", 4) == 0)
1660 cred.auth_type = WPS_AUTH_OPEN;
1661 else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1662 cred.auth_type = WPS_AUTH_WPAPSK;
1663 else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1664 cred.auth_type = WPS_AUTH_WPA2PSK;
1669 if (os_strncmp(encr, "NONE", 4) == 0)
1670 cred.encr_type = WPS_ENCR_NONE;
1671 else if (os_strncmp(encr, "WEP", 3) == 0)
1672 cred.encr_type = WPS_ENCR_WEP;
1673 else if (os_strncmp(encr, "TKIP", 4) == 0)
1674 cred.encr_type = WPS_ENCR_TKIP;
1675 else if (os_strncmp(encr, "CCMP", 4) == 0)
1676 cred.encr_type = WPS_ENCR_AES;
1680 cred.encr_type = WPS_ENCR_NONE;
1683 len = os_strlen(key);
1684 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1685 hexstr2bin(key, cred.key, len / 2))
1687 cred.key_len = len / 2;
1690 return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1694 #ifdef CONFIG_WPS_NFC
1696 struct wps_nfc_password_token_data {
1697 const u8 *oob_dev_pw;
1698 size_t oob_dev_pw_len;
1703 static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1705 struct wps_nfc_password_token_data *data = ctx;
1708 if (hapd->wps == NULL)
1710 ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1712 data->oob_dev_pw_len);
1719 static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
1720 struct wps_parse_attr *attr)
1722 struct wps_nfc_password_token_data data;
1724 data.oob_dev_pw = attr->oob_dev_password;
1725 data.oob_dev_pw_len = attr->oob_dev_password_len;
1727 if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
1729 return data.added ? 0 : -1;
1733 static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
1734 const struct wpabuf *wps)
1736 struct wps_parse_attr attr;
1738 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
1740 if (wps_parse_msg(wps, &attr)) {
1741 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
1745 if (attr.oob_dev_password)
1746 return hostapd_wps_add_nfc_password_token(hapd, &attr);
1748 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
1753 int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
1754 const struct wpabuf *data)
1756 const struct wpabuf *wps = data;
1757 struct wpabuf *tmp = NULL;
1760 if (wpabuf_len(data) < 4)
1763 if (*wpabuf_head_u8(data) != 0x10) {
1764 /* Assume this contains full NDEF record */
1765 tmp = ndef_parse_wifi(data);
1767 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
1773 ret = hostapd_wps_nfc_tag_process(hapd, wps);
1779 struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
1784 if (hapd->wps == NULL)
1787 ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
1788 hapd->iconf->channel);
1791 tmp = ndef_build_wifi(ret);
1802 struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
1806 if (hapd->wps == NULL)
1809 if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
1810 struct wps_context *wps = hapd->wps;
1811 if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
1812 &hapd->conf->wps_nfc_dh_privkey) < 0)
1814 hostapd_wps_nfc_clear(wps);
1815 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
1816 wps->ap_nfc_dh_pubkey =
1817 wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1818 wps->ap_nfc_dh_privkey =
1819 wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
1820 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
1821 hostapd_wps_nfc_clear(wps);
1826 ret = wps_build_nfc_handover_sel(hapd->wps,
1827 hapd->conf->wps_nfc_dh_pubkey,
1828 hapd->own_addr, hapd->iface->freq);
1832 tmp = ndef_build_wifi(ret);
1843 int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd,
1844 const struct wpabuf *req,
1845 const struct wpabuf *sel)
1852 struct wps_parse_attr attr;
1856 * Enrollee/station is always initiator of the NFC connection handover,
1857 * so use the request message here to find Enrollee public key hash.
1859 wps = ndef_parse_wifi(req);
1862 wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
1863 "payload from NFC connection handover");
1864 wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
1865 if (wpabuf_len(wps) < 2) {
1866 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
1870 pos = wpabuf_head(wps);
1871 wsc_len = WPA_GET_BE16(pos);
1872 if (wsc_len > wpabuf_len(wps) - 2) {
1873 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
1874 "in rt Wi-Fi Handover Request Message", wsc_len);
1879 wpa_hexdump(MSG_DEBUG,
1880 "WPS: WSC attributes in Wi-Fi Handover Request Message",
1882 if (wsc_len < wpabuf_len(wps) - 2) {
1883 wpa_hexdump(MSG_DEBUG,
1884 "WPS: Ignore extra data after WSC attributes",
1885 pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
1888 wpabuf_set(&msg, pos, wsc_len);
1889 ret = wps_parse_msg(&msg, &attr);
1891 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
1892 "Wi-Fi Handover Request Message");
1896 if (attr.oob_dev_password == NULL ||
1897 attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
1898 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
1899 "included in Wi-Fi Handover Request Message");
1904 if (attr.uuid_e == NULL) {
1905 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
1906 "Handover Request Message");
1911 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
1913 wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
1914 attr.oob_dev_password, attr.oob_dev_password_len);
1915 dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
1916 WPS_OOB_PUBKEY_HASH_LEN);
1917 if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
1918 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
1919 "%u in Wi-Fi Handover Request Message", dev_pw_id);
1923 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
1924 attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
1926 ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar,
1927 attr.oob_dev_password,
1928 DEV_PW_NFC_CONNECTION_HANDOVER,
1937 struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
1939 if (hapd->conf->wps_nfc_pw_from_config) {
1940 return wps_nfc_token_build(ndef,
1941 hapd->conf->wps_nfc_dev_pw_id,
1942 hapd->conf->wps_nfc_dh_pubkey,
1943 hapd->conf->wps_nfc_dev_pw);
1946 return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
1947 &hapd->conf->wps_nfc_dh_pubkey,
1948 &hapd->conf->wps_nfc_dh_privkey,
1949 &hapd->conf->wps_nfc_dev_pw);
1953 int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
1955 struct wps_context *wps = hapd->wps;
1961 if (!hapd->conf->wps_nfc_dh_pubkey ||
1962 !hapd->conf->wps_nfc_dh_privkey ||
1963 !hapd->conf->wps_nfc_dev_pw ||
1964 !hapd->conf->wps_nfc_dev_pw_id)
1967 hostapd_wps_nfc_clear(wps);
1968 wpa_printf(MSG_DEBUG,
1969 "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
1970 hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
1971 wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
1972 wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1973 wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
1974 pw = hapd->conf->wps_nfc_dev_pw;
1975 wps->ap_nfc_dev_pw = wpabuf_alloc(
1976 wpabuf_len(pw) * 2 + 1);
1977 if (wps->ap_nfc_dev_pw) {
1978 wpa_snprintf_hex_uppercase(
1979 (char *) wpabuf_put(wps->ap_nfc_dev_pw,
1980 wpabuf_len(pw) * 2),
1981 wpabuf_len(pw) * 2 + 1,
1982 wpabuf_head(pw), wpabuf_len(pw));
1985 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
1986 !wps->ap_nfc_dev_pw) {
1987 hostapd_wps_nfc_clear(wps);
1995 void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
1997 wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
1999 hostapd_wps_nfc_clear(hapd->wps);
2002 #endif /* CONFIG_WPS_NFC */