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 "crypto/dh_groups.h"
15 #include "crypto/dh_group5.h"
16 #include "common/wpa_ctrl.h"
17 #include "common/ieee802_11_defs.h"
18 #include "common/ieee802_11_common.h"
19 #include "eapol_auth/eapol_auth_sm.h"
20 #include "eapol_auth/eapol_auth_sm_i.h"
22 #include "wps/wps_defs.h"
23 #include "wps/wps_dev_attr.h"
24 #include "wps/wps_attr_parse.h"
26 #include "ap_config.h"
27 #include "ap_drv_ops.h"
30 #include "wps_hostapd.h"
33 #ifdef CONFIG_WPS_UPNP
34 #include "wps/wps_upnp.h"
35 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
36 struct wps_context *wps);
37 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
38 #endif /* CONFIG_WPS_UPNP */
40 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
42 const u8 *ie, size_t ie_len,
44 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
47 struct wps_for_each_data {
48 int (*func)(struct hostapd_data *h, void *ctx);
53 static int wps_for_each(struct hostapd_iface *iface, void *ctx)
55 struct wps_for_each_data *data = ctx;
60 for (j = 0; j < iface->num_bss; j++) {
61 struct hostapd_data *hapd = iface->bss[j];
62 int ret = data->func(hapd, data->ctx);
71 static int hostapd_wps_for_each(struct hostapd_data *hapd,
72 int (*func)(struct hostapd_data *h, void *ctx),
75 struct hostapd_iface *iface = hapd->iface;
76 struct wps_for_each_data data;
79 if (iface->interfaces == NULL ||
80 iface->interfaces->for_each_interface == NULL)
81 return wps_for_each(iface, &data);
82 return iface->interfaces->for_each_interface(iface->interfaces,
87 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
90 struct hostapd_data *hapd = ctx;
91 struct hostapd_wpa_psk *p;
92 struct hostapd_ssid *ssid = &hapd->conf->ssid;
94 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
95 MACSTR, MAC2STR(mac_addr));
96 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
98 if (psk_len != PMK_LEN) {
99 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
100 (unsigned long) psk_len);
104 /* Add the new PSK to runtime PSK list */
105 p = os_zalloc(sizeof(*p));
108 os_memcpy(p->addr, mac_addr, ETH_ALEN);
109 os_memcpy(p->psk, psk, PMK_LEN);
111 p->next = ssid->wpa_psk;
114 if (ssid->wpa_psk_file) {
116 char hex[PMK_LEN * 2 + 1];
117 /* Add the new PSK to PSK list file */
118 f = fopen(ssid->wpa_psk_file, "a");
120 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
121 "'%s'", ssid->wpa_psk_file);
125 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
126 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
134 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
135 struct wpabuf *probe_resp_ie)
137 struct hostapd_data *hapd = ctx;
138 wpabuf_free(hapd->wps_beacon_ie);
139 hapd->wps_beacon_ie = beacon_ie;
140 wpabuf_free(hapd->wps_probe_resp_ie);
141 hapd->wps_probe_resp_ie = probe_resp_ie;
142 if (hapd->beacon_set_done)
143 ieee802_11_set_beacon(hapd);
144 return hostapd_set_ap_wps_ie(hapd);
148 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
149 const struct wps_device_data *dev)
151 struct hostapd_data *hapd = ctx;
152 char uuid[40], txt[400];
154 char devtype[WPS_DEV_TYPE_BUFSIZE];
155 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
157 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
158 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
159 "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
160 uuid, MAC2STR(dev->mac_addr), dev->device_name,
161 dev->manufacturer, dev->model_name,
162 dev->model_number, dev->serial_number,
163 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
165 if (len > 0 && len < (int) sizeof(txt))
166 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
168 if (hapd->conf->wps_pin_requests) {
171 f = fopen(hapd->conf->wps_pin_requests, "a");
175 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
177 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
178 dev->manufacturer, dev->model_name, dev->model_number,
180 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
187 struct wps_stop_reg_data {
188 struct hostapd_data *current_hapd;
194 static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
196 struct wps_stop_reg_data *data = ctx;
197 if (hapd != data->current_hapd && hapd->wps != NULL)
198 wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
199 data->dev_pw, data->dev_pw_len);
204 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
205 const u8 *uuid_e, const u8 *dev_pw,
208 struct hostapd_data *hapd = ctx;
210 struct wps_stop_reg_data data;
211 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
213 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
214 MAC2STR(mac_addr), uuid);
215 if (hapd->wps_reg_success_cb)
216 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
218 data.current_hapd = hapd;
219 data.uuid_e = uuid_e;
220 data.dev_pw = dev_pw;
221 data.dev_pw_len = dev_pw_len;
222 hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
226 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
228 const u8 *pri_dev_type,
230 u16 dev_password_id, u8 request_type,
231 const char *dev_name)
233 struct hostapd_data *hapd = ctx;
235 char devtype[WPS_DEV_TYPE_BUFSIZE];
236 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
238 if (dev_name == NULL)
240 wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
241 " %s %s 0x%x %u %u [%s]",
243 wps_dev_type_bin2str(pri_dev_type, devtype,
245 config_methods, dev_password_id, request_type, dev_name);
249 static int str_starts(const char *str, const char *start)
251 return os_strncmp(str, start, os_strlen(start)) == 0;
255 static void wps_reload_config(void *eloop_data, void *user_ctx)
257 struct hostapd_iface *iface = eloop_data;
259 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
260 if (iface->interfaces == NULL ||
261 iface->interfaces->reload_config(iface) < 0) {
262 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
268 static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
271 size_t blen = attr_len * 2 + 1;
272 char *buf = os_malloc(blen);
274 wpa_snprintf_hex(buf, blen, attr, attr_len);
275 wpa_msg(hapd->msg_ctx, MSG_INFO,
276 WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
282 static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
284 const struct wps_credential *cred = ctx;
292 if (hapd->wps == NULL)
295 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
296 cred->cred_attr, cred->cred_attr_len);
298 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
299 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
300 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
302 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
303 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
304 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
305 cred->key, cred->key_len);
306 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
307 MAC2STR(cred->mac_addr));
309 if ((hapd->conf->wps_cred_processing == 1 ||
310 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
311 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
312 } else if (hapd->conf->wps_cred_processing == 1 ||
313 hapd->conf->wps_cred_processing == 2) {
315 attr = wpabuf_alloc(200);
316 if (attr && wps_build_credential_wrap(attr, cred) == 0)
317 hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
321 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
323 if (hapd->conf->wps_cred_processing == 1)
326 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
327 hapd->wps->ssid_len = cred->ssid_len;
328 hapd->wps->encr_types = cred->encr_type;
329 hapd->wps->auth_types = cred->auth_type;
330 if (cred->key_len == 0) {
331 os_free(hapd->wps->network_key);
332 hapd->wps->network_key = NULL;
333 hapd->wps->network_key_len = 0;
335 if (hapd->wps->network_key == NULL ||
336 hapd->wps->network_key_len < cred->key_len) {
337 hapd->wps->network_key_len = 0;
338 os_free(hapd->wps->network_key);
339 hapd->wps->network_key = os_malloc(cred->key_len);
340 if (hapd->wps->network_key == NULL)
343 hapd->wps->network_key_len = cred->key_len;
344 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
346 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
348 len = os_strlen(hapd->iface->config_fname) + 5;
349 tmp_fname = os_malloc(len);
350 if (tmp_fname == NULL)
352 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
354 oconf = fopen(hapd->iface->config_fname, "r");
356 wpa_printf(MSG_WARNING, "WPS: Could not open current "
357 "configuration file");
362 nconf = fopen(tmp_fname, "w");
364 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
365 "configuration file");
371 fprintf(nconf, "# WPS configuration - START\n");
373 fprintf(nconf, "wps_state=2\n");
375 if (is_hex(cred->ssid, cred->ssid_len)) {
376 fprintf(nconf, "ssid2=");
377 for (i = 0; i < cred->ssid_len; i++)
378 fprintf(nconf, "%02x", cred->ssid[i]);
379 fprintf(nconf, "\n");
381 fprintf(nconf, "ssid=");
382 for (i = 0; i < cred->ssid_len; i++)
383 fputc(cred->ssid[i], nconf);
384 fprintf(nconf, "\n");
387 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
388 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
390 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
392 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
399 fprintf(nconf, "wpa=%d\n", wpa);
401 fprintf(nconf, "wpa_key_mgmt=");
403 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
404 fprintf(nconf, "WPA-EAP");
407 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
408 fprintf(nconf, "%sWPA-PSK", prefix);
409 fprintf(nconf, "\n");
411 fprintf(nconf, "wpa_pairwise=");
413 if (cred->encr_type & WPS_ENCR_AES) {
414 fprintf(nconf, "CCMP");
417 if (cred->encr_type & WPS_ENCR_TKIP) {
418 fprintf(nconf, "%sTKIP", prefix);
420 fprintf(nconf, "\n");
422 if (cred->key_len >= 8 && cred->key_len < 64) {
423 fprintf(nconf, "wpa_passphrase=");
424 for (i = 0; i < cred->key_len; i++)
425 fputc(cred->key[i], nconf);
426 fprintf(nconf, "\n");
427 } else if (cred->key_len == 64) {
428 fprintf(nconf, "wpa_psk=");
429 for (i = 0; i < cred->key_len; i++)
430 fputc(cred->key[i], nconf);
431 fprintf(nconf, "\n");
433 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
435 (unsigned long) cred->key_len);
438 fprintf(nconf, "auth_algs=1\n");
440 if ((cred->auth_type & WPS_AUTH_OPEN) &&
441 (cred->auth_type & WPS_AUTH_SHARED))
442 fprintf(nconf, "auth_algs=3\n");
443 else if (cred->auth_type & WPS_AUTH_SHARED)
444 fprintf(nconf, "auth_algs=2\n");
446 fprintf(nconf, "auth_algs=1\n");
448 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
449 int key_idx = cred->key_idx;
452 fprintf(nconf, "wep_default_key=%d\n", key_idx);
453 fprintf(nconf, "wep_key%d=", key_idx);
454 if (cred->key_len == 10 || cred->key_len == 26) {
455 /* WEP key as a hex string */
456 for (i = 0; i < cred->key_len; i++)
457 fputc(cred->key[i], nconf);
459 /* Raw WEP key; convert to hex */
460 for (i = 0; i < cred->key_len; i++)
461 fprintf(nconf, "%02x", cred->key[i]);
463 fprintf(nconf, "\n");
467 fprintf(nconf, "# WPS configuration - END\n");
470 while (fgets(buf, sizeof(buf), oconf)) {
471 if (os_strncmp(buf, "bss=", 4) == 0)
474 (str_starts(buf, "ssid=") ||
475 str_starts(buf, "ssid2=") ||
476 str_starts(buf, "auth_algs=") ||
477 str_starts(buf, "wep_default_key=") ||
478 str_starts(buf, "wep_key") ||
479 str_starts(buf, "wps_state=") ||
480 str_starts(buf, "wpa=") ||
481 str_starts(buf, "wpa_psk=") ||
482 str_starts(buf, "wpa_pairwise=") ||
483 str_starts(buf, "rsn_pairwise=") ||
484 str_starts(buf, "wpa_key_mgmt=") ||
485 str_starts(buf, "wpa_passphrase="))) {
486 fprintf(nconf, "#WPS# %s", buf);
488 fprintf(nconf, "%s", buf);
494 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
495 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
496 "configuration file: %s", strerror(errno));
503 /* Schedule configuration reload after short period of time to allow
504 * EAP-WSC to be finished.
506 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
509 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
515 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
517 struct hostapd_data *hapd = ctx;
518 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
522 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
524 struct hostapd_data *hapd = eloop_data;
526 if (hapd->conf->ap_setup_locked)
528 if (hapd->ap_pin_failures_consecutive >= 10)
531 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
532 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
533 hapd->wps->ap_setup_locked = 0;
534 wps_registrar_update_ie(hapd->wps->registrar);
538 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
540 struct wps_event_pwd_auth_fail *data = ctx;
542 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
546 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
547 * for some time if this happens multiple times to slow down brute
550 hapd->ap_pin_failures++;
551 hapd->ap_pin_failures_consecutive++;
552 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
554 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
555 if (hapd->ap_pin_failures < 3)
558 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
559 hapd->wps->ap_setup_locked = 1;
561 wps_registrar_update_ie(hapd->wps->registrar);
563 if (!hapd->conf->ap_setup_locked &&
564 hapd->ap_pin_failures_consecutive >= 10) {
566 * In indefinite lockdown - disable automatic AP PIN
569 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
570 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
571 } else if (!hapd->conf->ap_setup_locked) {
572 if (hapd->ap_pin_lockout_time == 0)
573 hapd->ap_pin_lockout_time = 60;
574 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
575 (hapd->ap_pin_failures % 3) == 0)
576 hapd->ap_pin_lockout_time *= 2;
578 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
579 hapd->ap_pin_lockout_time);
580 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
581 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
582 hostapd_wps_reenable_ap_pin, hapd,
590 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
591 struct wps_event_pwd_auth_fail *data)
593 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
597 static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
599 if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
602 if (hapd->ap_pin_failures_consecutive == 0)
605 wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
606 "- total validation failures %u (%u consecutive)",
607 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
608 hapd->ap_pin_failures_consecutive = 0;
614 static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
616 hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
620 static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = {
621 "No Error", /* WPS_EI_NO_ERROR */
622 "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */
623 "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */
626 static void hostapd_wps_event_fail(struct hostapd_data *hapd,
627 struct wps_event_fail *fail)
629 if (fail->error_indication > 0 &&
630 fail->error_indication < NUM_WPS_EI_VALUES) {
631 wpa_msg(hapd->msg_ctx, MSG_INFO,
632 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
633 fail->msg, fail->config_error, fail->error_indication,
634 wps_event_fail_reason[fail->error_indication]);
636 wpa_msg(hapd->msg_ctx, MSG_INFO,
637 WPS_EVENT_FAIL "msg=%d config_error=%d",
638 fail->msg, fail->config_error);
643 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
644 union wps_event_data *data)
646 struct hostapd_data *hapd = ctx;
650 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
653 hostapd_wps_event_fail(hapd, &data->fail);
656 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
658 case WPS_EV_PWD_AUTH_FAIL:
659 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
661 case WPS_EV_PBC_OVERLAP:
662 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
664 case WPS_EV_PBC_TIMEOUT:
665 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
667 case WPS_EV_ER_AP_ADD:
669 case WPS_EV_ER_AP_REMOVE:
671 case WPS_EV_ER_ENROLLEE_ADD:
673 case WPS_EV_ER_ENROLLEE_REMOVE:
675 case WPS_EV_ER_AP_SETTINGS:
677 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
679 case WPS_EV_AP_PIN_SUCCESS:
680 hostapd_wps_ap_pin_success(hapd);
683 if (hapd->wps_event_cb)
684 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
688 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
690 wpabuf_free(hapd->wps_beacon_ie);
691 hapd->wps_beacon_ie = NULL;
693 wpabuf_free(hapd->wps_probe_resp_ie);
694 hapd->wps_probe_resp_ie = NULL;
696 hostapd_set_ap_wps_ie(hapd);
700 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
702 const u8 **uuid = ctx;
707 for (j = 0; j < iface->num_bss; j++) {
708 struct hostapd_data *hapd = iface->bss[j];
709 if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) {
710 *uuid = hapd->wps->uuid;
719 static const u8 * get_own_uuid(struct hostapd_iface *iface)
722 if (iface->interfaces == NULL ||
723 iface->interfaces->for_each_interface == NULL)
726 iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
732 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
740 static int interface_count(struct hostapd_iface *iface)
743 if (iface->interfaces == NULL ||
744 iface->interfaces->for_each_interface == NULL)
746 iface->interfaces->for_each_interface(iface->interfaces,
747 count_interface_cb, &count);
752 static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
753 struct wps_context *wps)
757 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
758 wpabuf_free(wps->dev.vendor_ext[i]);
759 wps->dev.vendor_ext[i] = NULL;
761 if (hapd->conf->wps_vendor_ext[i] == NULL)
764 wps->dev.vendor_ext[i] =
765 wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
766 if (wps->dev.vendor_ext[i] == NULL) {
768 wpabuf_free(wps->dev.vendor_ext[i]);
777 int hostapd_init_wps(struct hostapd_data *hapd,
778 struct hostapd_bss_config *conf)
780 struct wps_context *wps;
781 struct wps_registrar_config cfg;
783 if (conf->wps_state == 0) {
784 hostapd_wps_clear_ies(hapd);
788 wps = os_zalloc(sizeof(*wps));
792 wps->cred_cb = hostapd_wps_cred_cb;
793 wps->event_cb = hostapd_wps_event_cb;
796 os_memset(&cfg, 0, sizeof(cfg));
797 wps->wps_state = hapd->conf->wps_state;
798 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
799 if (is_nil_uuid(hapd->conf->uuid)) {
801 uuid = get_own_uuid(hapd->iface);
803 os_memcpy(wps->uuid, uuid, UUID_LEN);
804 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
805 "interface", wps->uuid, UUID_LEN);
807 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
808 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
809 "address", wps->uuid, UUID_LEN);
812 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
813 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
814 wps->uuid, UUID_LEN);
816 wps->ssid_len = hapd->conf->ssid.ssid_len;
817 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
819 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
820 wps->dev.device_name = hapd->conf->device_name ?
821 os_strdup(hapd->conf->device_name) : NULL;
822 wps->dev.manufacturer = hapd->conf->manufacturer ?
823 os_strdup(hapd->conf->manufacturer) : NULL;
824 wps->dev.model_name = hapd->conf->model_name ?
825 os_strdup(hapd->conf->model_name) : NULL;
826 wps->dev.model_number = hapd->conf->model_number ?
827 os_strdup(hapd->conf->model_number) : NULL;
828 wps->dev.serial_number = hapd->conf->serial_number ?
829 os_strdup(hapd->conf->serial_number) : NULL;
830 wps->config_methods =
831 wps_config_methods_str2bin(hapd->conf->config_methods);
833 if ((wps->config_methods &
834 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
835 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
836 wpa_printf(MSG_INFO, "WPS: Converting display to "
837 "virtual_display for WPS 2.0 compliance");
838 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
840 if ((wps->config_methods &
841 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
842 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
843 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
844 "virtual_push_button for WPS 2.0 compliance");
845 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
847 #endif /* CONFIG_WPS2 */
848 os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
851 if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) {
856 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
858 if (conf->wps_rf_bands) {
859 wps->dev.rf_bands = conf->wps_rf_bands;
862 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
863 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
866 if (conf->wpa & WPA_PROTO_RSN) {
867 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
868 wps->auth_types |= WPS_AUTH_WPA2PSK;
869 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
870 wps->auth_types |= WPS_AUTH_WPA2;
872 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
873 wps->encr_types |= WPS_ENCR_AES;
874 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
875 wps->encr_types |= WPS_ENCR_TKIP;
878 if (conf->wpa & WPA_PROTO_WPA) {
879 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
880 wps->auth_types |= WPS_AUTH_WPAPSK;
881 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
882 wps->auth_types |= WPS_AUTH_WPA;
884 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
885 wps->encr_types |= WPS_ENCR_AES;
886 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
887 wps->encr_types |= WPS_ENCR_TKIP;
890 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
891 wps->encr_types |= WPS_ENCR_NONE;
892 wps->auth_types |= WPS_AUTH_OPEN;
893 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
894 wps->encr_types |= WPS_ENCR_WEP;
895 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
896 wps->auth_types |= WPS_AUTH_OPEN;
897 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
898 wps->auth_types |= WPS_AUTH_SHARED;
899 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
900 wps->auth_types |= WPS_AUTH_OPEN;
901 if (conf->default_wep_key_len)
902 wps->encr_types |= WPS_ENCR_WEP;
904 wps->encr_types |= WPS_ENCR_NONE;
907 if (conf->ssid.wpa_psk_file) {
908 /* Use per-device PSKs */
909 } else if (conf->ssid.wpa_passphrase) {
910 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
911 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
912 } else if (conf->ssid.wpa_psk) {
913 wps->network_key = os_malloc(2 * PMK_LEN + 1);
914 if (wps->network_key == NULL) {
918 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
919 conf->ssid.wpa_psk->psk, PMK_LEN);
920 wps->network_key_len = 2 * PMK_LEN;
921 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
922 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
923 if (wps->network_key == NULL) {
927 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
928 conf->ssid.wep.len[0]);
929 wps->network_key_len = conf->ssid.wep.len[0];
932 if (conf->ssid.wpa_psk) {
933 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
937 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
938 /* Override parameters to enable security by default */
939 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
940 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
943 wps->ap_settings = conf->ap_settings;
944 wps->ap_settings_len = conf->ap_settings_len;
946 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
947 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
948 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
949 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
950 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
952 cfg.skip_cred_build = conf->skip_cred_build;
953 cfg.extra_cred = conf->extra_cred;
954 cfg.extra_cred_len = conf->extra_cred_len;
955 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
956 conf->skip_cred_build;
957 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
958 cfg.static_wep_only = 1;
959 cfg.dualband = interface_count(hapd->iface) > 1;
961 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
963 wps->registrar = wps_registrar_init(wps, &cfg);
964 if (wps->registrar == NULL) {
965 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
966 os_free(wps->network_key);
971 #ifdef CONFIG_WPS_UPNP
972 wps->friendly_name = hapd->conf->friendly_name;
973 wps->manufacturer_url = hapd->conf->manufacturer_url;
974 wps->model_description = hapd->conf->model_description;
975 wps->model_url = hapd->conf->model_url;
976 wps->upc = hapd->conf->upc;
977 #endif /* CONFIG_WPS_UPNP */
979 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
987 int hostapd_init_wps_complete(struct hostapd_data *hapd)
989 struct wps_context *wps = hapd->wps;
994 #ifdef CONFIG_WPS_UPNP
995 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
996 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
997 wps_registrar_deinit(wps->registrar);
998 os_free(wps->network_key);
1003 #endif /* CONFIG_WPS_UPNP */
1009 static void hostapd_wps_nfc_clear(struct wps_context *wps)
1011 #ifdef CONFIG_WPS_NFC
1012 wps->ap_nfc_dev_pw_id = 0;
1013 wpabuf_free(wps->ap_nfc_dh_pubkey);
1014 wps->ap_nfc_dh_pubkey = NULL;
1015 wpabuf_free(wps->ap_nfc_dh_privkey);
1016 wps->ap_nfc_dh_privkey = NULL;
1017 wpabuf_free(wps->ap_nfc_dev_pw);
1018 wps->ap_nfc_dev_pw = NULL;
1019 #endif /* CONFIG_WPS_NFC */
1023 void hostapd_deinit_wps(struct hostapd_data *hapd)
1025 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1026 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1027 if (hapd->wps == NULL)
1029 #ifdef CONFIG_WPS_UPNP
1030 hostapd_wps_upnp_deinit(hapd);
1031 #endif /* CONFIG_WPS_UPNP */
1032 wps_registrar_deinit(hapd->wps->registrar);
1033 os_free(hapd->wps->network_key);
1034 wps_device_data_free(&hapd->wps->dev);
1035 wpabuf_free(hapd->wps->dh_pubkey);
1036 wpabuf_free(hapd->wps->dh_privkey);
1037 wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
1038 wpabuf_free(hapd->wps->oob_conf.dev_password);
1039 wps_free_pending_msgs(hapd->wps->upnp_msgs);
1040 hostapd_wps_nfc_clear(hapd->wps);
1043 hostapd_wps_clear_ies(hapd);
1047 void hostapd_update_wps(struct hostapd_data *hapd)
1049 if (hapd->wps == NULL)
1052 #ifdef CONFIG_WPS_UPNP
1053 hapd->wps->friendly_name = hapd->conf->friendly_name;
1054 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
1055 hapd->wps->model_description = hapd->conf->model_description;
1056 hapd->wps->model_url = hapd->conf->model_url;
1057 hapd->wps->upc = hapd->conf->upc;
1058 #endif /* CONFIG_WPS_UPNP */
1060 hostapd_wps_set_vendor_ext(hapd, hapd->wps);
1062 if (hapd->conf->wps_state)
1063 wps_registrar_update_ie(hapd->wps->registrar);
1065 hostapd_deinit_wps(hapd);
1069 struct wps_add_pin_data {
1079 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1081 struct wps_add_pin_data *data = ctx;
1084 if (hapd->wps == NULL)
1086 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1087 data->uuid, data->pin, data->pin_len,
1095 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1096 const char *uuid, const char *pin, int timeout)
1099 struct wps_add_pin_data data;
1103 data.pin = (const u8 *) pin;
1104 data.pin_len = os_strlen(pin);
1105 data.timeout = timeout;
1108 if (os_strcmp(uuid, "any") == 0)
1111 if (uuid_str2bin(uuid, u))
1115 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1117 return data.added ? 0 : -1;
1121 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1123 const u8 *p2p_dev_addr = ctx;
1124 if (hapd->wps == NULL)
1126 return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
1130 int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1131 const u8 *p2p_dev_addr)
1133 return hostapd_wps_for_each(hapd, wps_button_pushed,
1134 (void *) p2p_dev_addr);
1138 static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1140 if (hapd->wps == NULL)
1143 wps_registrar_wps_cancel(hapd->wps->registrar);
1144 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
1150 int hostapd_wps_cancel(struct hostapd_data *hapd)
1152 return hostapd_wps_for_each(hapd, wps_cancel, NULL);
1156 #ifdef CONFIG_WPS_OOB
1157 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
1158 char *path, char *method, char *name)
1160 struct wps_context *wps = hapd->wps;
1161 struct oob_device_data *oob_dev;
1163 oob_dev = wps_get_oob_device(device_type);
1164 if (oob_dev == NULL)
1166 oob_dev->device_path = path;
1167 oob_dev->device_name = name;
1168 wps->oob_conf.oob_method = wps_get_oob_method(method);
1170 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
1172 * Use pre-configured DH keys in order to be able to write the
1173 * key hash into the OOB file.
1175 wpabuf_free(wps->dh_pubkey);
1176 wpabuf_free(wps->dh_privkey);
1177 wps->dh_privkey = NULL;
1178 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
1180 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
1181 if (wps->dh_pubkey == NULL) {
1182 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
1183 "Diffie-Hellman handshake");
1188 if (wps_process_oob(wps, oob_dev, 1) < 0)
1191 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
1192 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
1193 hostapd_wps_add_pin(hapd, NULL, "any",
1194 wpabuf_head(wps->oob_conf.dev_password), 0) <
1201 wpabuf_free(wps->dh_pubkey);
1202 wps->dh_pubkey = NULL;
1203 wpabuf_free(wps->dh_privkey);
1204 wps->dh_privkey = NULL;
1207 #endif /* CONFIG_WPS_OOB */
1210 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1212 const u8 *ie, size_t ie_len,
1215 struct hostapd_data *hapd = ctx;
1216 struct wpabuf *wps_ie;
1217 struct ieee802_11_elems elems;
1219 if (hapd->wps == NULL)
1222 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1223 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1224 MACSTR, MAC2STR(addr));
1228 if (elems.ssid && elems.ssid_len > 0 &&
1229 (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1230 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1232 return 0; /* Not for us */
1234 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1237 if (wps_validate_probe_req(wps_ie, addr) < 0) {
1238 wpabuf_free(wps_ie);
1242 if (wpabuf_len(wps_ie) > 0) {
1243 int p2p_wildcard = 0;
1245 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1246 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1247 P2P_WILDCARD_SSID_LEN) == 0)
1249 #endif /* CONFIG_P2P */
1250 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1252 #ifdef CONFIG_WPS_UPNP
1253 /* FIX: what exactly should be included in the WLANEvent?
1254 * WPS attributes? Full ProbeReq frame? */
1256 upnp_wps_device_send_wlan_event(
1257 hapd->wps_upnp, addr,
1258 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1259 #endif /* CONFIG_WPS_UPNP */
1262 wpabuf_free(wps_ie);
1268 #ifdef CONFIG_WPS_UPNP
1270 static int hostapd_rx_req_put_wlan_response(
1271 void *priv, enum upnp_wps_wlanevent_type ev_type,
1272 const u8 *mac_addr, const struct wpabuf *msg,
1273 enum wps_msg_type msg_type)
1275 struct hostapd_data *hapd = priv;
1276 struct sta_info *sta;
1277 struct upnp_pending_message *p;
1279 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1280 MACSTR, ev_type, MAC2STR(mac_addr));
1281 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1282 wpabuf_head(msg), wpabuf_len(msg));
1283 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1284 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1285 "PutWLANResponse WLANEventType %d", ev_type);
1290 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1291 * server implementation for delivery to the peer.
1294 sta = ap_get_sta(hapd, mac_addr);
1295 #ifndef CONFIG_WPS_STRICT
1298 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1299 * Pick STA that is in an ongoing WPS registration without
1300 * checking the MAC address.
1302 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1303 "on NewWLANEventMAC; try wildcard match");
1304 for (sta = hapd->sta_list; sta; sta = sta->next) {
1305 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1309 #endif /* CONFIG_WPS_STRICT */
1311 if (!sta || !(sta->flags & WLAN_STA_WPS)) {
1312 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1316 p = os_zalloc(sizeof(*p));
1319 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1320 p->msg = wpabuf_dup(msg);
1322 p->next = hapd->wps->upnp_msgs;
1323 hapd->wps->upnp_msgs = p;
1325 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1329 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1330 struct wps_context *wps)
1332 struct upnp_wps_device_ctx *ctx;
1334 if (!hapd->conf->upnp_iface)
1336 ctx = os_zalloc(sizeof(*ctx));
1340 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1341 if (hapd->conf->ap_pin)
1342 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1344 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1345 hapd->conf->upnp_iface);
1346 if (hapd->wps_upnp == NULL)
1348 wps->wps_upnp = hapd->wps_upnp;
1354 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1356 upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1359 #endif /* CONFIG_WPS_UPNP */
1362 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1363 char *buf, size_t buflen)
1365 if (hapd->wps == NULL)
1367 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1371 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1373 struct hostapd_data *hapd = eloop_data;
1374 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1375 hostapd_wps_ap_pin_disable(hapd);
1376 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
1380 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1382 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1383 hapd->ap_pin_failures = 0;
1384 hapd->ap_pin_failures_consecutive = 0;
1385 hapd->conf->ap_setup_locked = 0;
1386 if (hapd->wps->ap_setup_locked) {
1387 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1388 hapd->wps->ap_setup_locked = 0;
1389 wps_registrar_update_ie(hapd->wps->registrar);
1391 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1393 eloop_register_timeout(timeout, 0,
1394 hostapd_wps_ap_pin_timeout, hapd, NULL);
1398 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1400 os_free(hapd->conf->ap_pin);
1401 hapd->conf->ap_pin = NULL;
1402 #ifdef CONFIG_WPS_UPNP
1403 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1404 #endif /* CONFIG_WPS_UPNP */
1405 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1410 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1412 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1413 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1417 struct wps_ap_pin_data {
1423 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1425 struct wps_ap_pin_data *data = ctx;
1426 os_free(hapd->conf->ap_pin);
1427 hapd->conf->ap_pin = os_strdup(data->pin_txt);
1428 #ifdef CONFIG_WPS_UPNP
1429 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1430 #endif /* CONFIG_WPS_UPNP */
1431 hostapd_wps_ap_pin_enable(hapd, data->timeout);
1436 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1439 struct wps_ap_pin_data data;
1441 pin = wps_generate_pin();
1442 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
1443 data.timeout = timeout;
1444 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1445 return hapd->conf->ap_pin;
1449 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1451 return hapd->conf->ap_pin;
1455 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1458 struct wps_ap_pin_data data;
1461 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1462 if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1464 data.timeout = timeout;
1465 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1469 static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1472 wps_registrar_update_ie(hapd->wps->registrar);
1477 void hostapd_wps_update_ie(struct hostapd_data *hapd)
1479 hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1483 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1484 const char *auth, const char *encr, const char *key)
1486 struct wps_credential cred;
1489 os_memset(&cred, 0, sizeof(cred));
1491 len = os_strlen(ssid);
1492 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1493 hexstr2bin(ssid, cred.ssid, len / 2))
1495 cred.ssid_len = len / 2;
1497 if (os_strncmp(auth, "OPEN", 4) == 0)
1498 cred.auth_type = WPS_AUTH_OPEN;
1499 else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1500 cred.auth_type = WPS_AUTH_WPAPSK;
1501 else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1502 cred.auth_type = WPS_AUTH_WPA2PSK;
1507 if (os_strncmp(encr, "NONE", 4) == 0)
1508 cred.encr_type = WPS_ENCR_NONE;
1509 else if (os_strncmp(encr, "WEP", 3) == 0)
1510 cred.encr_type = WPS_ENCR_WEP;
1511 else if (os_strncmp(encr, "TKIP", 4) == 0)
1512 cred.encr_type = WPS_ENCR_TKIP;
1513 else if (os_strncmp(encr, "CCMP", 4) == 0)
1514 cred.encr_type = WPS_ENCR_AES;
1518 cred.encr_type = WPS_ENCR_NONE;
1521 len = os_strlen(key);
1522 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1523 hexstr2bin(key, cred.key, len / 2))
1525 cred.key_len = len / 2;
1528 return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1532 #ifdef CONFIG_WPS_NFC
1534 struct wps_nfc_password_token_data {
1535 const u8 *oob_dev_pw;
1536 size_t oob_dev_pw_len;
1541 static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1543 struct wps_nfc_password_token_data *data = ctx;
1546 if (hapd->wps == NULL)
1548 ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1550 data->oob_dev_pw_len);
1557 static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
1558 struct wps_parse_attr *attr)
1560 struct wps_nfc_password_token_data data;
1562 data.oob_dev_pw = attr->oob_dev_password;
1563 data.oob_dev_pw_len = attr->oob_dev_password_len;
1565 if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
1567 return data.added ? 0 : -1;
1571 static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
1572 const struct wpabuf *wps)
1574 struct wps_parse_attr attr;
1576 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
1578 if (wps_parse_msg(wps, &attr)) {
1579 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
1583 if (attr.oob_dev_password)
1584 return hostapd_wps_add_nfc_password_token(hapd, &attr);
1586 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
1591 int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
1592 const struct wpabuf *data)
1594 const struct wpabuf *wps = data;
1595 struct wpabuf *tmp = NULL;
1598 if (wpabuf_len(data) < 4)
1601 if (*wpabuf_head_u8(data) != 0x10) {
1602 /* Assume this contains full NDEF record */
1603 tmp = ndef_parse_wifi(data);
1605 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
1611 ret = hostapd_wps_nfc_tag_process(hapd, wps);
1617 struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
1622 if (hapd->wps == NULL)
1625 ret = wps_get_oob_cred(hapd->wps);
1628 tmp = ndef_build_wifi(ret);
1639 struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
1641 return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
1642 &hapd->conf->wps_nfc_dh_pubkey,
1643 &hapd->conf->wps_nfc_dh_privkey,
1644 &hapd->conf->wps_nfc_dev_pw);
1648 int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
1650 struct wps_context *wps = hapd->wps;
1655 if (!hapd->conf->wps_nfc_dh_pubkey ||
1656 !hapd->conf->wps_nfc_dh_privkey ||
1657 !hapd->conf->wps_nfc_dev_pw ||
1658 !hapd->conf->wps_nfc_dev_pw_id)
1661 hostapd_wps_nfc_clear(wps);
1662 wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
1663 wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1664 wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
1665 wps->ap_nfc_dev_pw = wpabuf_dup(hapd->conf->wps_nfc_dev_pw);
1667 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
1668 !wps->ap_nfc_dev_pw) {
1669 hostapd_wps_nfc_clear(wps);
1677 void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
1679 hostapd_wps_nfc_clear(hapd->wps);
1682 #endif /* CONFIG_WPS_NFC */