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 if (hapd->iface->config_fname == NULL)
350 len = os_strlen(hapd->iface->config_fname) + 5;
351 tmp_fname = os_malloc(len);
352 if (tmp_fname == NULL)
354 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
356 oconf = fopen(hapd->iface->config_fname, "r");
358 wpa_printf(MSG_WARNING, "WPS: Could not open current "
359 "configuration file");
364 nconf = fopen(tmp_fname, "w");
366 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
367 "configuration file");
373 fprintf(nconf, "# WPS configuration - START\n");
375 fprintf(nconf, "wps_state=2\n");
377 if (is_hex(cred->ssid, cred->ssid_len)) {
378 fprintf(nconf, "ssid2=");
379 for (i = 0; i < cred->ssid_len; i++)
380 fprintf(nconf, "%02x", cred->ssid[i]);
381 fprintf(nconf, "\n");
383 fprintf(nconf, "ssid=");
384 for (i = 0; i < cred->ssid_len; i++)
385 fputc(cred->ssid[i], nconf);
386 fprintf(nconf, "\n");
389 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
390 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
392 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
394 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
401 fprintf(nconf, "wpa=%d\n", wpa);
403 fprintf(nconf, "wpa_key_mgmt=");
405 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
406 fprintf(nconf, "WPA-EAP");
409 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
410 fprintf(nconf, "%sWPA-PSK", prefix);
411 fprintf(nconf, "\n");
413 fprintf(nconf, "wpa_pairwise=");
415 if (cred->encr_type & WPS_ENCR_AES) {
416 fprintf(nconf, "CCMP");
419 if (cred->encr_type & WPS_ENCR_TKIP) {
420 fprintf(nconf, "%sTKIP", prefix);
422 fprintf(nconf, "\n");
424 if (cred->key_len >= 8 && cred->key_len < 64) {
425 fprintf(nconf, "wpa_passphrase=");
426 for (i = 0; i < cred->key_len; i++)
427 fputc(cred->key[i], nconf);
428 fprintf(nconf, "\n");
429 } else if (cred->key_len == 64) {
430 fprintf(nconf, "wpa_psk=");
431 for (i = 0; i < cred->key_len; i++)
432 fputc(cred->key[i], nconf);
433 fprintf(nconf, "\n");
435 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
437 (unsigned long) cred->key_len);
440 fprintf(nconf, "auth_algs=1\n");
442 if ((cred->auth_type & WPS_AUTH_OPEN) &&
443 (cred->auth_type & WPS_AUTH_SHARED))
444 fprintf(nconf, "auth_algs=3\n");
445 else if (cred->auth_type & WPS_AUTH_SHARED)
446 fprintf(nconf, "auth_algs=2\n");
448 fprintf(nconf, "auth_algs=1\n");
450 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
451 int key_idx = cred->key_idx;
454 fprintf(nconf, "wep_default_key=%d\n", key_idx);
455 fprintf(nconf, "wep_key%d=", key_idx);
456 if (cred->key_len == 10 || cred->key_len == 26) {
457 /* WEP key as a hex string */
458 for (i = 0; i < cred->key_len; i++)
459 fputc(cred->key[i], nconf);
461 /* Raw WEP key; convert to hex */
462 for (i = 0; i < cred->key_len; i++)
463 fprintf(nconf, "%02x", cred->key[i]);
465 fprintf(nconf, "\n");
469 fprintf(nconf, "# WPS configuration - END\n");
472 while (fgets(buf, sizeof(buf), oconf)) {
473 if (os_strncmp(buf, "bss=", 4) == 0)
476 (str_starts(buf, "ssid=") ||
477 str_starts(buf, "ssid2=") ||
478 str_starts(buf, "auth_algs=") ||
479 str_starts(buf, "wep_default_key=") ||
480 str_starts(buf, "wep_key") ||
481 str_starts(buf, "wps_state=") ||
482 str_starts(buf, "wpa=") ||
483 str_starts(buf, "wpa_psk=") ||
484 str_starts(buf, "wpa_pairwise=") ||
485 str_starts(buf, "rsn_pairwise=") ||
486 str_starts(buf, "wpa_key_mgmt=") ||
487 str_starts(buf, "wpa_passphrase="))) {
488 fprintf(nconf, "#WPS# %s", buf);
490 fprintf(nconf, "%s", buf);
496 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
497 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
498 "configuration file: %s", strerror(errno));
505 /* Schedule configuration reload after short period of time to allow
506 * EAP-WSC to be finished.
508 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
511 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
517 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
519 struct hostapd_data *hapd = ctx;
520 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
524 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
526 struct hostapd_data *hapd = eloop_data;
528 if (hapd->conf->ap_setup_locked)
530 if (hapd->ap_pin_failures_consecutive >= 10)
533 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
534 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
535 hapd->wps->ap_setup_locked = 0;
536 wps_registrar_update_ie(hapd->wps->registrar);
540 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
542 struct wps_event_pwd_auth_fail *data = ctx;
544 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
548 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
549 * for some time if this happens multiple times to slow down brute
552 hapd->ap_pin_failures++;
553 hapd->ap_pin_failures_consecutive++;
554 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
556 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
557 if (hapd->ap_pin_failures < 3)
560 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
561 hapd->wps->ap_setup_locked = 1;
563 wps_registrar_update_ie(hapd->wps->registrar);
565 if (!hapd->conf->ap_setup_locked &&
566 hapd->ap_pin_failures_consecutive >= 10) {
568 * In indefinite lockdown - disable automatic AP PIN
571 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
572 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
573 } else if (!hapd->conf->ap_setup_locked) {
574 if (hapd->ap_pin_lockout_time == 0)
575 hapd->ap_pin_lockout_time = 60;
576 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
577 (hapd->ap_pin_failures % 3) == 0)
578 hapd->ap_pin_lockout_time *= 2;
580 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
581 hapd->ap_pin_lockout_time);
582 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
583 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
584 hostapd_wps_reenable_ap_pin, hapd,
592 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
593 struct wps_event_pwd_auth_fail *data)
595 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
599 static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
601 if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
604 if (hapd->ap_pin_failures_consecutive == 0)
607 wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
608 "- total validation failures %u (%u consecutive)",
609 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
610 hapd->ap_pin_failures_consecutive = 0;
616 static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
618 hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
622 static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = {
623 "No Error", /* WPS_EI_NO_ERROR */
624 "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */
625 "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */
628 static void hostapd_wps_event_fail(struct hostapd_data *hapd,
629 struct wps_event_fail *fail)
631 if (fail->error_indication > 0 &&
632 fail->error_indication < NUM_WPS_EI_VALUES) {
633 wpa_msg(hapd->msg_ctx, MSG_INFO,
634 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
635 fail->msg, fail->config_error, fail->error_indication,
636 wps_event_fail_reason[fail->error_indication]);
638 wpa_msg(hapd->msg_ctx, MSG_INFO,
639 WPS_EVENT_FAIL "msg=%d config_error=%d",
640 fail->msg, fail->config_error);
645 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
646 union wps_event_data *data)
648 struct hostapd_data *hapd = ctx;
652 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
655 hostapd_wps_event_fail(hapd, &data->fail);
658 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
660 case WPS_EV_PWD_AUTH_FAIL:
661 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
663 case WPS_EV_PBC_OVERLAP:
664 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
666 case WPS_EV_PBC_TIMEOUT:
667 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
669 case WPS_EV_ER_AP_ADD:
671 case WPS_EV_ER_AP_REMOVE:
673 case WPS_EV_ER_ENROLLEE_ADD:
675 case WPS_EV_ER_ENROLLEE_REMOVE:
677 case WPS_EV_ER_AP_SETTINGS:
679 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
681 case WPS_EV_AP_PIN_SUCCESS:
682 hostapd_wps_ap_pin_success(hapd);
685 if (hapd->wps_event_cb)
686 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
690 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
692 wpabuf_free(hapd->wps_beacon_ie);
693 hapd->wps_beacon_ie = NULL;
695 wpabuf_free(hapd->wps_probe_resp_ie);
696 hapd->wps_probe_resp_ie = NULL;
698 hostapd_set_ap_wps_ie(hapd);
702 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
704 const u8 **uuid = ctx;
709 for (j = 0; j < iface->num_bss; j++) {
710 struct hostapd_data *hapd = iface->bss[j];
711 if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) {
712 *uuid = hapd->wps->uuid;
721 static const u8 * get_own_uuid(struct hostapd_iface *iface)
724 if (iface->interfaces == NULL ||
725 iface->interfaces->for_each_interface == NULL)
728 iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
734 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
742 static int interface_count(struct hostapd_iface *iface)
745 if (iface->interfaces == NULL ||
746 iface->interfaces->for_each_interface == NULL)
748 iface->interfaces->for_each_interface(iface->interfaces,
749 count_interface_cb, &count);
754 static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
755 struct wps_context *wps)
759 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
760 wpabuf_free(wps->dev.vendor_ext[i]);
761 wps->dev.vendor_ext[i] = NULL;
763 if (hapd->conf->wps_vendor_ext[i] == NULL)
766 wps->dev.vendor_ext[i] =
767 wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
768 if (wps->dev.vendor_ext[i] == NULL) {
770 wpabuf_free(wps->dev.vendor_ext[i]);
779 int hostapd_init_wps(struct hostapd_data *hapd,
780 struct hostapd_bss_config *conf)
782 struct wps_context *wps;
783 struct wps_registrar_config cfg;
785 if (conf->wps_state == 0) {
786 hostapd_wps_clear_ies(hapd);
790 wps = os_zalloc(sizeof(*wps));
794 wps->cred_cb = hostapd_wps_cred_cb;
795 wps->event_cb = hostapd_wps_event_cb;
798 os_memset(&cfg, 0, sizeof(cfg));
799 wps->wps_state = hapd->conf->wps_state;
800 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
801 if (is_nil_uuid(hapd->conf->uuid)) {
803 uuid = get_own_uuid(hapd->iface);
805 os_memcpy(wps->uuid, uuid, UUID_LEN);
806 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
807 "interface", wps->uuid, UUID_LEN);
809 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
810 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
811 "address", wps->uuid, UUID_LEN);
814 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
815 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
816 wps->uuid, UUID_LEN);
818 wps->ssid_len = hapd->conf->ssid.ssid_len;
819 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
821 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
822 wps->dev.device_name = hapd->conf->device_name ?
823 os_strdup(hapd->conf->device_name) : NULL;
824 wps->dev.manufacturer = hapd->conf->manufacturer ?
825 os_strdup(hapd->conf->manufacturer) : NULL;
826 wps->dev.model_name = hapd->conf->model_name ?
827 os_strdup(hapd->conf->model_name) : NULL;
828 wps->dev.model_number = hapd->conf->model_number ?
829 os_strdup(hapd->conf->model_number) : NULL;
830 wps->dev.serial_number = hapd->conf->serial_number ?
831 os_strdup(hapd->conf->serial_number) : NULL;
832 wps->config_methods =
833 wps_config_methods_str2bin(hapd->conf->config_methods);
835 if ((wps->config_methods &
836 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
837 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
838 wpa_printf(MSG_INFO, "WPS: Converting display to "
839 "virtual_display for WPS 2.0 compliance");
840 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
842 if ((wps->config_methods &
843 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
844 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
845 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
846 "virtual_push_button for WPS 2.0 compliance");
847 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
849 #endif /* CONFIG_WPS2 */
850 os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
853 if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) {
858 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
860 if (conf->wps_rf_bands) {
861 wps->dev.rf_bands = conf->wps_rf_bands;
864 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
865 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
868 if (conf->wpa & WPA_PROTO_RSN) {
869 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
870 wps->auth_types |= WPS_AUTH_WPA2PSK;
871 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
872 wps->auth_types |= WPS_AUTH_WPA2;
874 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
875 wps->encr_types |= WPS_ENCR_AES;
876 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
877 wps->encr_types |= WPS_ENCR_TKIP;
880 if (conf->wpa & WPA_PROTO_WPA) {
881 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
882 wps->auth_types |= WPS_AUTH_WPAPSK;
883 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
884 wps->auth_types |= WPS_AUTH_WPA;
886 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
887 wps->encr_types |= WPS_ENCR_AES;
888 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
889 wps->encr_types |= WPS_ENCR_TKIP;
892 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
893 wps->encr_types |= WPS_ENCR_NONE;
894 wps->auth_types |= WPS_AUTH_OPEN;
895 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
896 wps->encr_types |= WPS_ENCR_WEP;
897 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
898 wps->auth_types |= WPS_AUTH_OPEN;
899 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
900 wps->auth_types |= WPS_AUTH_SHARED;
901 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
902 wps->auth_types |= WPS_AUTH_OPEN;
903 if (conf->default_wep_key_len)
904 wps->encr_types |= WPS_ENCR_WEP;
906 wps->encr_types |= WPS_ENCR_NONE;
909 if (conf->ssid.wpa_psk_file) {
910 /* Use per-device PSKs */
911 } else if (conf->ssid.wpa_passphrase) {
912 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
913 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
914 } else if (conf->ssid.wpa_psk) {
915 wps->network_key = os_malloc(2 * PMK_LEN + 1);
916 if (wps->network_key == NULL) {
920 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
921 conf->ssid.wpa_psk->psk, PMK_LEN);
922 wps->network_key_len = 2 * PMK_LEN;
923 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
924 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
925 if (wps->network_key == NULL) {
929 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
930 conf->ssid.wep.len[0]);
931 wps->network_key_len = conf->ssid.wep.len[0];
934 if (conf->ssid.wpa_psk) {
935 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
939 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
940 /* Override parameters to enable security by default */
941 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
942 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
945 wps->ap_settings = conf->ap_settings;
946 wps->ap_settings_len = conf->ap_settings_len;
948 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
949 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
950 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
951 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
952 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
954 cfg.skip_cred_build = conf->skip_cred_build;
955 cfg.extra_cred = conf->extra_cred;
956 cfg.extra_cred_len = conf->extra_cred_len;
957 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
958 conf->skip_cred_build;
959 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
960 cfg.static_wep_only = 1;
961 cfg.dualband = interface_count(hapd->iface) > 1;
963 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
965 wps->registrar = wps_registrar_init(wps, &cfg);
966 if (wps->registrar == NULL) {
967 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
968 os_free(wps->network_key);
973 #ifdef CONFIG_WPS_UPNP
974 wps->friendly_name = hapd->conf->friendly_name;
975 wps->manufacturer_url = hapd->conf->manufacturer_url;
976 wps->model_description = hapd->conf->model_description;
977 wps->model_url = hapd->conf->model_url;
978 wps->upc = hapd->conf->upc;
979 #endif /* CONFIG_WPS_UPNP */
981 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
989 int hostapd_init_wps_complete(struct hostapd_data *hapd)
991 struct wps_context *wps = hapd->wps;
996 #ifdef CONFIG_WPS_UPNP
997 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
998 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
999 wps_registrar_deinit(wps->registrar);
1000 os_free(wps->network_key);
1005 #endif /* CONFIG_WPS_UPNP */
1011 static void hostapd_wps_nfc_clear(struct wps_context *wps)
1013 #ifdef CONFIG_WPS_NFC
1014 wps->ap_nfc_dev_pw_id = 0;
1015 wpabuf_free(wps->ap_nfc_dh_pubkey);
1016 wps->ap_nfc_dh_pubkey = NULL;
1017 wpabuf_free(wps->ap_nfc_dh_privkey);
1018 wps->ap_nfc_dh_privkey = NULL;
1019 wpabuf_free(wps->ap_nfc_dev_pw);
1020 wps->ap_nfc_dev_pw = NULL;
1021 #endif /* CONFIG_WPS_NFC */
1025 void hostapd_deinit_wps(struct hostapd_data *hapd)
1027 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1028 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1029 if (hapd->wps == NULL)
1031 #ifdef CONFIG_WPS_UPNP
1032 hostapd_wps_upnp_deinit(hapd);
1033 #endif /* CONFIG_WPS_UPNP */
1034 wps_registrar_deinit(hapd->wps->registrar);
1035 os_free(hapd->wps->network_key);
1036 wps_device_data_free(&hapd->wps->dev);
1037 wpabuf_free(hapd->wps->dh_pubkey);
1038 wpabuf_free(hapd->wps->dh_privkey);
1039 wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
1040 wpabuf_free(hapd->wps->oob_conf.dev_password);
1041 wps_free_pending_msgs(hapd->wps->upnp_msgs);
1042 hostapd_wps_nfc_clear(hapd->wps);
1045 hostapd_wps_clear_ies(hapd);
1049 void hostapd_update_wps(struct hostapd_data *hapd)
1051 if (hapd->wps == NULL)
1054 #ifdef CONFIG_WPS_UPNP
1055 hapd->wps->friendly_name = hapd->conf->friendly_name;
1056 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
1057 hapd->wps->model_description = hapd->conf->model_description;
1058 hapd->wps->model_url = hapd->conf->model_url;
1059 hapd->wps->upc = hapd->conf->upc;
1060 #endif /* CONFIG_WPS_UPNP */
1062 hostapd_wps_set_vendor_ext(hapd, hapd->wps);
1064 if (hapd->conf->wps_state)
1065 wps_registrar_update_ie(hapd->wps->registrar);
1067 hostapd_deinit_wps(hapd);
1071 struct wps_add_pin_data {
1081 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1083 struct wps_add_pin_data *data = ctx;
1086 if (hapd->wps == NULL)
1088 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1089 data->uuid, data->pin, data->pin_len,
1097 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1098 const char *uuid, const char *pin, int timeout)
1101 struct wps_add_pin_data data;
1105 data.pin = (const u8 *) pin;
1106 data.pin_len = os_strlen(pin);
1107 data.timeout = timeout;
1110 if (os_strcmp(uuid, "any") == 0)
1113 if (uuid_str2bin(uuid, u))
1117 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1119 return data.added ? 0 : -1;
1123 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1125 const u8 *p2p_dev_addr = ctx;
1126 if (hapd->wps == NULL)
1128 return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
1132 int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1133 const u8 *p2p_dev_addr)
1135 return hostapd_wps_for_each(hapd, wps_button_pushed,
1136 (void *) p2p_dev_addr);
1140 static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1142 if (hapd->wps == NULL)
1145 wps_registrar_wps_cancel(hapd->wps->registrar);
1146 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
1152 int hostapd_wps_cancel(struct hostapd_data *hapd)
1154 return hostapd_wps_for_each(hapd, wps_cancel, NULL);
1158 #ifdef CONFIG_WPS_OOB
1159 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
1160 char *path, char *method)
1162 struct wps_context *wps = hapd->wps;
1163 struct oob_device_data *oob_dev;
1165 oob_dev = wps_get_oob_device(device_type);
1166 if (oob_dev == NULL)
1168 oob_dev->device_path = path;
1169 wps->oob_conf.oob_method = wps_get_oob_method(method);
1171 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
1173 * Use pre-configured DH keys in order to be able to write the
1174 * key hash into the OOB file.
1176 wpabuf_free(wps->dh_pubkey);
1177 wpabuf_free(wps->dh_privkey);
1178 wps->dh_privkey = NULL;
1179 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
1181 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
1182 if (wps->dh_pubkey == NULL) {
1183 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
1184 "Diffie-Hellman handshake");
1189 if (wps_process_oob(wps, oob_dev, 1) < 0)
1192 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
1193 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
1194 hostapd_wps_add_pin(hapd, NULL, "any",
1195 wpabuf_head(wps->oob_conf.dev_password), 0) <
1202 wpabuf_free(wps->dh_pubkey);
1203 wps->dh_pubkey = NULL;
1204 wpabuf_free(wps->dh_privkey);
1205 wps->dh_privkey = NULL;
1208 #endif /* CONFIG_WPS_OOB */
1211 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1213 const u8 *ie, size_t ie_len,
1216 struct hostapd_data *hapd = ctx;
1217 struct wpabuf *wps_ie;
1218 struct ieee802_11_elems elems;
1220 if (hapd->wps == NULL)
1223 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1224 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1225 MACSTR, MAC2STR(addr));
1229 if (elems.ssid && elems.ssid_len > 0 &&
1230 (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1231 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1233 return 0; /* Not for us */
1235 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1238 if (wps_validate_probe_req(wps_ie, addr) < 0) {
1239 wpabuf_free(wps_ie);
1243 if (wpabuf_len(wps_ie) > 0) {
1244 int p2p_wildcard = 0;
1246 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1247 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1248 P2P_WILDCARD_SSID_LEN) == 0)
1250 #endif /* CONFIG_P2P */
1251 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1253 #ifdef CONFIG_WPS_UPNP
1254 /* FIX: what exactly should be included in the WLANEvent?
1255 * WPS attributes? Full ProbeReq frame? */
1257 upnp_wps_device_send_wlan_event(
1258 hapd->wps_upnp, addr,
1259 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1260 #endif /* CONFIG_WPS_UPNP */
1263 wpabuf_free(wps_ie);
1269 #ifdef CONFIG_WPS_UPNP
1271 static int hostapd_rx_req_put_wlan_response(
1272 void *priv, enum upnp_wps_wlanevent_type ev_type,
1273 const u8 *mac_addr, const struct wpabuf *msg,
1274 enum wps_msg_type msg_type)
1276 struct hostapd_data *hapd = priv;
1277 struct sta_info *sta;
1278 struct upnp_pending_message *p;
1280 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1281 MACSTR, ev_type, MAC2STR(mac_addr));
1282 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1283 wpabuf_head(msg), wpabuf_len(msg));
1284 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1285 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1286 "PutWLANResponse WLANEventType %d", ev_type);
1291 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1292 * server implementation for delivery to the peer.
1295 sta = ap_get_sta(hapd, mac_addr);
1296 #ifndef CONFIG_WPS_STRICT
1299 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1300 * Pick STA that is in an ongoing WPS registration without
1301 * checking the MAC address.
1303 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1304 "on NewWLANEventMAC; try wildcard match");
1305 for (sta = hapd->sta_list; sta; sta = sta->next) {
1306 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1310 #endif /* CONFIG_WPS_STRICT */
1312 if (!sta || !(sta->flags & WLAN_STA_WPS)) {
1313 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1317 p = os_zalloc(sizeof(*p));
1320 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1321 p->msg = wpabuf_dup(msg);
1323 p->next = hapd->wps->upnp_msgs;
1324 hapd->wps->upnp_msgs = p;
1326 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1330 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1331 struct wps_context *wps)
1333 struct upnp_wps_device_ctx *ctx;
1335 if (!hapd->conf->upnp_iface)
1337 ctx = os_zalloc(sizeof(*ctx));
1341 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1342 if (hapd->conf->ap_pin)
1343 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1345 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1346 hapd->conf->upnp_iface);
1347 if (hapd->wps_upnp == NULL)
1349 wps->wps_upnp = hapd->wps_upnp;
1355 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1357 upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1360 #endif /* CONFIG_WPS_UPNP */
1363 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1364 char *buf, size_t buflen)
1366 if (hapd->wps == NULL)
1368 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1372 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1374 struct hostapd_data *hapd = eloop_data;
1375 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1376 hostapd_wps_ap_pin_disable(hapd);
1377 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
1381 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1383 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1384 hapd->ap_pin_failures = 0;
1385 hapd->ap_pin_failures_consecutive = 0;
1386 hapd->conf->ap_setup_locked = 0;
1387 if (hapd->wps->ap_setup_locked) {
1388 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1389 hapd->wps->ap_setup_locked = 0;
1390 wps_registrar_update_ie(hapd->wps->registrar);
1392 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1394 eloop_register_timeout(timeout, 0,
1395 hostapd_wps_ap_pin_timeout, hapd, NULL);
1399 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1401 os_free(hapd->conf->ap_pin);
1402 hapd->conf->ap_pin = NULL;
1403 #ifdef CONFIG_WPS_UPNP
1404 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1405 #endif /* CONFIG_WPS_UPNP */
1406 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1411 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1413 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1414 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1418 struct wps_ap_pin_data {
1424 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1426 struct wps_ap_pin_data *data = ctx;
1427 os_free(hapd->conf->ap_pin);
1428 hapd->conf->ap_pin = os_strdup(data->pin_txt);
1429 #ifdef CONFIG_WPS_UPNP
1430 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1431 #endif /* CONFIG_WPS_UPNP */
1432 hostapd_wps_ap_pin_enable(hapd, data->timeout);
1437 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1440 struct wps_ap_pin_data data;
1442 pin = wps_generate_pin();
1443 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
1444 data.timeout = timeout;
1445 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1446 return hapd->conf->ap_pin;
1450 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1452 return hapd->conf->ap_pin;
1456 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1459 struct wps_ap_pin_data data;
1462 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1463 if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1465 data.timeout = timeout;
1466 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1470 static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1473 wps_registrar_update_ie(hapd->wps->registrar);
1478 void hostapd_wps_update_ie(struct hostapd_data *hapd)
1480 hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1484 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1485 const char *auth, const char *encr, const char *key)
1487 struct wps_credential cred;
1490 os_memset(&cred, 0, sizeof(cred));
1492 len = os_strlen(ssid);
1493 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1494 hexstr2bin(ssid, cred.ssid, len / 2))
1496 cred.ssid_len = len / 2;
1498 if (os_strncmp(auth, "OPEN", 4) == 0)
1499 cred.auth_type = WPS_AUTH_OPEN;
1500 else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1501 cred.auth_type = WPS_AUTH_WPAPSK;
1502 else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1503 cred.auth_type = WPS_AUTH_WPA2PSK;
1508 if (os_strncmp(encr, "NONE", 4) == 0)
1509 cred.encr_type = WPS_ENCR_NONE;
1510 else if (os_strncmp(encr, "WEP", 3) == 0)
1511 cred.encr_type = WPS_ENCR_WEP;
1512 else if (os_strncmp(encr, "TKIP", 4) == 0)
1513 cred.encr_type = WPS_ENCR_TKIP;
1514 else if (os_strncmp(encr, "CCMP", 4) == 0)
1515 cred.encr_type = WPS_ENCR_AES;
1519 cred.encr_type = WPS_ENCR_NONE;
1522 len = os_strlen(key);
1523 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1524 hexstr2bin(key, cred.key, len / 2))
1526 cred.key_len = len / 2;
1529 return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1533 #ifdef CONFIG_WPS_NFC
1535 struct wps_nfc_password_token_data {
1536 const u8 *oob_dev_pw;
1537 size_t oob_dev_pw_len;
1542 static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1544 struct wps_nfc_password_token_data *data = ctx;
1547 if (hapd->wps == NULL)
1549 ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1551 data->oob_dev_pw_len);
1558 static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
1559 struct wps_parse_attr *attr)
1561 struct wps_nfc_password_token_data data;
1563 data.oob_dev_pw = attr->oob_dev_password;
1564 data.oob_dev_pw_len = attr->oob_dev_password_len;
1566 if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
1568 return data.added ? 0 : -1;
1572 static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
1573 const struct wpabuf *wps)
1575 struct wps_parse_attr attr;
1577 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
1579 if (wps_parse_msg(wps, &attr)) {
1580 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
1584 if (attr.oob_dev_password)
1585 return hostapd_wps_add_nfc_password_token(hapd, &attr);
1587 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
1592 int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
1593 const struct wpabuf *data)
1595 const struct wpabuf *wps = data;
1596 struct wpabuf *tmp = NULL;
1599 if (wpabuf_len(data) < 4)
1602 if (*wpabuf_head_u8(data) != 0x10) {
1603 /* Assume this contains full NDEF record */
1604 tmp = ndef_parse_wifi(data);
1606 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
1612 ret = hostapd_wps_nfc_tag_process(hapd, wps);
1618 struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
1623 if (hapd->wps == NULL)
1626 ret = wps_get_oob_cred(hapd->wps);
1629 tmp = ndef_build_wifi(ret);
1640 struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
1642 return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
1643 &hapd->conf->wps_nfc_dh_pubkey,
1644 &hapd->conf->wps_nfc_dh_privkey,
1645 &hapd->conf->wps_nfc_dev_pw);
1649 int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
1651 struct wps_context *wps = hapd->wps;
1656 if (!hapd->conf->wps_nfc_dh_pubkey ||
1657 !hapd->conf->wps_nfc_dh_privkey ||
1658 !hapd->conf->wps_nfc_dev_pw ||
1659 !hapd->conf->wps_nfc_dev_pw_id)
1662 hostapd_wps_nfc_clear(wps);
1663 wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
1664 wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1665 wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
1666 wps->ap_nfc_dev_pw = wpabuf_dup(hapd->conf->wps_nfc_dev_pw);
1668 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
1669 !wps->ap_nfc_dev_pw) {
1670 hostapd_wps_nfc_clear(wps);
1678 void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
1680 hostapd_wps_nfc_clear(hapd->wps);
1683 #endif /* CONFIG_WPS_NFC */