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, const u8 *psk,
97 struct hostapd_data *hapd = ctx;
98 struct hostapd_wpa_psk *p;
99 struct hostapd_ssid *ssid = &hapd->conf->ssid;
101 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
102 MACSTR, MAC2STR(mac_addr));
103 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
105 if (psk_len != PMK_LEN) {
106 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
107 (unsigned long) psk_len);
111 /* Add the new PSK to runtime PSK list */
112 p = os_zalloc(sizeof(*p));
115 os_memcpy(p->addr, mac_addr, ETH_ALEN);
116 os_memcpy(p->psk, psk, PMK_LEN);
118 p->next = ssid->wpa_psk;
121 if (ssid->wpa_psk_file) {
123 char hex[PMK_LEN * 2 + 1];
124 /* Add the new PSK to PSK list file */
125 f = fopen(ssid->wpa_psk_file, "a");
127 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
128 "'%s'", ssid->wpa_psk_file);
132 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
133 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
141 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
142 struct wpabuf *probe_resp_ie)
144 struct hostapd_data *hapd = ctx;
145 wpabuf_free(hapd->wps_beacon_ie);
146 hapd->wps_beacon_ie = beacon_ie;
147 wpabuf_free(hapd->wps_probe_resp_ie);
148 hapd->wps_probe_resp_ie = probe_resp_ie;
149 if (hapd->beacon_set_done)
150 ieee802_11_set_beacon(hapd);
151 return hostapd_set_ap_wps_ie(hapd);
155 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
156 const struct wps_device_data *dev)
158 struct hostapd_data *hapd = ctx;
159 char uuid[40], txt[400];
161 char devtype[WPS_DEV_TYPE_BUFSIZE];
162 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
164 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
165 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
166 "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
167 uuid, MAC2STR(dev->mac_addr), dev->device_name,
168 dev->manufacturer, dev->model_name,
169 dev->model_number, dev->serial_number,
170 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
172 if (len > 0 && len < (int) sizeof(txt))
173 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
175 if (hapd->conf->wps_pin_requests) {
178 f = fopen(hapd->conf->wps_pin_requests, "a");
182 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
184 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
185 dev->manufacturer, dev->model_name, dev->model_number,
187 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
194 struct wps_stop_reg_data {
195 struct hostapd_data *current_hapd;
201 static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
203 struct wps_stop_reg_data *data = ctx;
204 if (hapd != data->current_hapd && hapd->wps != NULL)
205 wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
206 data->dev_pw, data->dev_pw_len);
211 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
212 const u8 *uuid_e, const u8 *dev_pw,
215 struct hostapd_data *hapd = ctx;
217 struct wps_stop_reg_data data;
218 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
220 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
221 MAC2STR(mac_addr), uuid);
222 if (hapd->wps_reg_success_cb)
223 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
225 data.current_hapd = hapd;
226 data.uuid_e = uuid_e;
227 data.dev_pw = dev_pw;
228 data.dev_pw_len = dev_pw_len;
229 hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
233 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
235 const u8 *pri_dev_type,
237 u16 dev_password_id, u8 request_type,
238 const char *dev_name)
240 struct hostapd_data *hapd = ctx;
242 char devtype[WPS_DEV_TYPE_BUFSIZE];
243 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
245 if (dev_name == NULL)
247 wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
248 " %s %s 0x%x %u %u [%s]",
250 wps_dev_type_bin2str(pri_dev_type, devtype,
252 config_methods, dev_password_id, request_type, dev_name);
256 static int str_starts(const char *str, const char *start)
258 return os_strncmp(str, start, os_strlen(start)) == 0;
262 static void wps_reload_config(void *eloop_data, void *user_ctx)
264 struct hostapd_iface *iface = eloop_data;
266 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
267 if (iface->interfaces == NULL ||
268 iface->interfaces->reload_config(iface) < 0) {
269 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
275 static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
278 size_t blen = attr_len * 2 + 1;
279 char *buf = os_malloc(blen);
281 wpa_snprintf_hex(buf, blen, attr, attr_len);
282 wpa_msg(hapd->msg_ctx, MSG_INFO,
283 WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
289 static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
290 const struct wps_credential *cred)
292 struct hostapd_bss_config *bss = hapd->conf;
294 wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
297 if (cred->ssid_len <= HOSTAPD_MAX_SSID_LEN) {
298 os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
299 bss->ssid.ssid_len = cred->ssid_len;
300 bss->ssid.ssid_set = 1;
303 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
304 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
306 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
308 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
314 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
315 bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
316 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
317 bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
319 bss->wpa_pairwise = 0;
320 if (cred->encr_type & WPS_ENCR_AES)
321 bss->wpa_pairwise |= WPA_CIPHER_CCMP;
322 if (cred->encr_type & WPS_ENCR_TKIP)
323 bss->wpa_pairwise |= WPA_CIPHER_TKIP;
324 bss->rsn_pairwise = bss->wpa_pairwise;
325 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
329 if (cred->key_len >= 8 && cred->key_len < 64) {
330 os_free(bss->ssid.wpa_passphrase);
331 bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
332 if (bss->ssid.wpa_passphrase)
333 os_memcpy(bss->ssid.wpa_passphrase, cred->key,
335 os_free(bss->ssid.wpa_psk);
336 bss->ssid.wpa_psk = NULL;
337 } else if (cred->key_len == 64) {
338 os_free(bss->ssid.wpa_psk);
340 os_zalloc(sizeof(struct hostapd_wpa_psk));
341 if (bss->ssid.wpa_psk &&
342 hexstr2bin((const char *) cred->key,
343 bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
344 bss->ssid.wpa_psk->group = 1;
345 os_free(bss->ssid.wpa_passphrase);
346 bss->ssid.wpa_passphrase = NULL;
351 if ((cred->auth_type & WPS_AUTH_OPEN) &&
352 (cred->auth_type & WPS_AUTH_SHARED))
354 else if (cred->auth_type & WPS_AUTH_SHARED)
358 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx > 0 &&
359 cred->key_idx <= 4) {
360 struct hostapd_wep_keys *wep = &bss->ssid.wep;
361 int idx = cred->key_idx;
365 if (cred->key_len == 10 || cred->key_len == 26) {
366 os_free(wep->key[idx]);
367 wep->key[idx] = os_malloc(cred->key_len / 2);
368 if (wep->key[idx] == NULL ||
369 hexstr2bin((const char *) cred->key,
373 wep->len[idx] = cred->key_len / 2;
375 os_free(wep->key[idx]);
376 wep->key[idx] = os_malloc(cred->key_len);
377 if (wep->key[idx] == NULL)
379 os_memcpy(wep->key[idx], cred->key,
381 wep->len[idx] = cred->key_len;
387 /* Schedule configuration reload after short period of time to allow
388 * EAP-WSC to be finished.
390 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
397 static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
399 const struct wps_credential *cred = ctx;
407 if (hapd->wps == NULL)
410 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
411 cred->cred_attr, cred->cred_attr_len);
413 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
414 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
415 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
417 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
418 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
419 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
420 cred->key, cred->key_len);
421 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
422 MAC2STR(cred->mac_addr));
424 if ((hapd->conf->wps_cred_processing == 1 ||
425 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
426 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
427 } else if (hapd->conf->wps_cred_processing == 1 ||
428 hapd->conf->wps_cred_processing == 2) {
430 attr = wpabuf_alloc(200);
431 if (attr && wps_build_credential_wrap(attr, cred) == 0)
432 hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
436 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
438 if (hapd->conf->wps_cred_processing == 1)
441 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
442 hapd->wps->ssid_len = cred->ssid_len;
443 hapd->wps->encr_types = cred->encr_type;
444 hapd->wps->auth_types = cred->auth_type;
445 if (cred->key_len == 0) {
446 os_free(hapd->wps->network_key);
447 hapd->wps->network_key = NULL;
448 hapd->wps->network_key_len = 0;
450 if (hapd->wps->network_key == NULL ||
451 hapd->wps->network_key_len < cred->key_len) {
452 hapd->wps->network_key_len = 0;
453 os_free(hapd->wps->network_key);
454 hapd->wps->network_key = os_malloc(cred->key_len);
455 if (hapd->wps->network_key == NULL)
458 hapd->wps->network_key_len = cred->key_len;
459 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
461 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
463 if (hapd->iface->config_fname == NULL)
464 return hapd_wps_reconfig_in_memory(hapd, cred);
465 len = os_strlen(hapd->iface->config_fname) + 5;
466 tmp_fname = os_malloc(len);
467 if (tmp_fname == NULL)
469 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
471 oconf = fopen(hapd->iface->config_fname, "r");
473 wpa_printf(MSG_WARNING, "WPS: Could not open current "
474 "configuration file");
479 nconf = fopen(tmp_fname, "w");
481 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
482 "configuration file");
488 fprintf(nconf, "# WPS configuration - START\n");
490 fprintf(nconf, "wps_state=2\n");
492 if (is_hex(cred->ssid, cred->ssid_len)) {
493 fprintf(nconf, "ssid2=");
494 for (i = 0; i < cred->ssid_len; i++)
495 fprintf(nconf, "%02x", cred->ssid[i]);
496 fprintf(nconf, "\n");
498 fprintf(nconf, "ssid=");
499 for (i = 0; i < cred->ssid_len; i++)
500 fputc(cred->ssid[i], nconf);
501 fprintf(nconf, "\n");
504 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
505 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
507 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
509 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
516 fprintf(nconf, "wpa=%d\n", wpa);
518 fprintf(nconf, "wpa_key_mgmt=");
520 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
521 fprintf(nconf, "WPA-EAP");
524 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
525 fprintf(nconf, "%sWPA-PSK", prefix);
526 fprintf(nconf, "\n");
528 fprintf(nconf, "wpa_pairwise=");
530 if (cred->encr_type & WPS_ENCR_AES) {
531 fprintf(nconf, "CCMP");
534 if (cred->encr_type & WPS_ENCR_TKIP) {
535 fprintf(nconf, "%sTKIP", prefix);
537 fprintf(nconf, "\n");
539 if (cred->key_len >= 8 && cred->key_len < 64) {
540 fprintf(nconf, "wpa_passphrase=");
541 for (i = 0; i < cred->key_len; i++)
542 fputc(cred->key[i], nconf);
543 fprintf(nconf, "\n");
544 } else if (cred->key_len == 64) {
545 fprintf(nconf, "wpa_psk=");
546 for (i = 0; i < cred->key_len; i++)
547 fputc(cred->key[i], nconf);
548 fprintf(nconf, "\n");
550 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
552 (unsigned long) cred->key_len);
555 fprintf(nconf, "auth_algs=1\n");
557 if ((cred->auth_type & WPS_AUTH_OPEN) &&
558 (cred->auth_type & WPS_AUTH_SHARED))
559 fprintf(nconf, "auth_algs=3\n");
560 else if (cred->auth_type & WPS_AUTH_SHARED)
561 fprintf(nconf, "auth_algs=2\n");
563 fprintf(nconf, "auth_algs=1\n");
565 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
566 int key_idx = cred->key_idx;
569 fprintf(nconf, "wep_default_key=%d\n", key_idx);
570 fprintf(nconf, "wep_key%d=", key_idx);
571 if (cred->key_len == 10 || cred->key_len == 26) {
572 /* WEP key as a hex string */
573 for (i = 0; i < cred->key_len; i++)
574 fputc(cred->key[i], nconf);
576 /* Raw WEP key; convert to hex */
577 for (i = 0; i < cred->key_len; i++)
578 fprintf(nconf, "%02x", cred->key[i]);
580 fprintf(nconf, "\n");
584 fprintf(nconf, "# WPS configuration - END\n");
587 while (fgets(buf, sizeof(buf), oconf)) {
588 if (os_strncmp(buf, "bss=", 4) == 0)
591 (str_starts(buf, "ssid=") ||
592 str_starts(buf, "ssid2=") ||
593 str_starts(buf, "auth_algs=") ||
594 str_starts(buf, "wep_default_key=") ||
595 str_starts(buf, "wep_key") ||
596 str_starts(buf, "wps_state=") ||
597 str_starts(buf, "wpa=") ||
598 str_starts(buf, "wpa_psk=") ||
599 str_starts(buf, "wpa_pairwise=") ||
600 str_starts(buf, "rsn_pairwise=") ||
601 str_starts(buf, "wpa_key_mgmt=") ||
602 str_starts(buf, "wpa_passphrase="))) {
603 fprintf(nconf, "#WPS# %s", buf);
605 fprintf(nconf, "%s", buf);
611 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
612 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
613 "configuration file: %s", strerror(errno));
620 /* Schedule configuration reload after short period of time to allow
621 * EAP-WSC to be finished.
623 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
626 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
632 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
634 struct hostapd_data *hapd = ctx;
635 return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
639 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
641 struct hostapd_data *hapd = eloop_data;
643 if (hapd->conf->ap_setup_locked)
645 if (hapd->ap_pin_failures_consecutive >= 10)
648 wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
649 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
650 hapd->wps->ap_setup_locked = 0;
651 wps_registrar_update_ie(hapd->wps->registrar);
655 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
657 struct wps_event_pwd_auth_fail *data = ctx;
659 if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
663 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
664 * for some time if this happens multiple times to slow down brute
667 hapd->ap_pin_failures++;
668 hapd->ap_pin_failures_consecutive++;
669 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
671 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
672 if (hapd->ap_pin_failures < 3)
675 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
676 hapd->wps->ap_setup_locked = 1;
678 wps_registrar_update_ie(hapd->wps->registrar);
680 if (!hapd->conf->ap_setup_locked &&
681 hapd->ap_pin_failures_consecutive >= 10) {
683 * In indefinite lockdown - disable automatic AP PIN
686 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
687 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
688 } else if (!hapd->conf->ap_setup_locked) {
689 if (hapd->ap_pin_lockout_time == 0)
690 hapd->ap_pin_lockout_time = 60;
691 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
692 (hapd->ap_pin_failures % 3) == 0)
693 hapd->ap_pin_lockout_time *= 2;
695 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
696 hapd->ap_pin_lockout_time);
697 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
698 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
699 hostapd_wps_reenable_ap_pin, hapd,
707 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
708 struct wps_event_pwd_auth_fail *data)
710 /* Update WPS Status - Authentication Failure */
711 wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
712 hapd->wps_stats.status = WPS_STATUS_FAILURE;
713 hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
714 os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
716 hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
720 static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
722 if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
725 if (hapd->ap_pin_failures_consecutive == 0)
728 wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
729 "- total validation failures %u (%u consecutive)",
730 hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
731 hapd->ap_pin_failures_consecutive = 0;
737 static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
739 hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
743 static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
745 /* Update WPS Status - PBC Overlap */
746 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
750 static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
752 /* Update WPS PBC Status:PBC Timeout */
753 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
757 static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
759 /* Update WPS PBC status - Active */
760 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
764 static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
766 /* Update WPS PBC status - Active */
767 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
771 static void hostapd_wps_event_success(struct hostapd_data *hapd,
772 struct wps_event_success *success)
774 /* Update WPS status - Success */
775 hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
776 hapd->wps_stats.status = WPS_STATUS_SUCCESS;
777 os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
781 static void hostapd_wps_event_fail(struct hostapd_data *hapd,
782 struct wps_event_fail *fail)
784 /* Update WPS status - Failure */
785 hapd->wps_stats.status = WPS_STATUS_FAILURE;
786 os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
788 hapd->wps_stats.failure_reason = fail->error_indication;
790 if (fail->error_indication > 0 &&
791 fail->error_indication < NUM_WPS_EI_VALUES) {
792 wpa_msg(hapd->msg_ctx, MSG_INFO,
793 WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
794 fail->msg, fail->config_error, fail->error_indication,
795 wps_ei_str(fail->error_indication));
797 wpa_msg(hapd->msg_ctx, MSG_INFO,
798 WPS_EVENT_FAIL "msg=%d config_error=%d",
799 fail->msg, fail->config_error);
804 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
805 union wps_event_data *data)
807 struct hostapd_data *hapd = ctx;
811 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
814 hostapd_wps_event_fail(hapd, &data->fail);
817 hostapd_wps_event_success(hapd, &data->success);
818 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
820 case WPS_EV_PWD_AUTH_FAIL:
821 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
823 case WPS_EV_PBC_OVERLAP:
824 hostapd_wps_event_pbc_overlap(hapd);
825 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
827 case WPS_EV_PBC_TIMEOUT:
828 hostapd_wps_event_pbc_timeout(hapd);
829 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
831 case WPS_EV_PBC_ACTIVE:
832 hostapd_wps_event_pbc_active(hapd);
833 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
835 case WPS_EV_PBC_DISABLE:
836 hostapd_wps_event_pbc_disable(hapd);
837 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
839 case WPS_EV_ER_AP_ADD:
841 case WPS_EV_ER_AP_REMOVE:
843 case WPS_EV_ER_ENROLLEE_ADD:
845 case WPS_EV_ER_ENROLLEE_REMOVE:
847 case WPS_EV_ER_AP_SETTINGS:
849 case WPS_EV_ER_SET_SELECTED_REGISTRAR:
851 case WPS_EV_AP_PIN_SUCCESS:
852 hostapd_wps_ap_pin_success(hapd);
855 if (hapd->wps_event_cb)
856 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
860 static int hostapd_wps_rf_band_cb(void *ctx)
862 struct hostapd_data *hapd = ctx;
864 return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
865 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
869 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
871 wpabuf_free(hapd->wps_beacon_ie);
872 hapd->wps_beacon_ie = NULL;
874 wpabuf_free(hapd->wps_probe_resp_ie);
875 hapd->wps_probe_resp_ie = NULL;
877 hostapd_set_ap_wps_ie(hapd);
881 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
883 const u8 **uuid = ctx;
888 for (j = 0; j < iface->num_bss; j++) {
889 struct hostapd_data *hapd = iface->bss[j];
890 if (hapd->wps && !hapd->conf->wps_independent &&
891 !is_nil_uuid(hapd->wps->uuid)) {
892 *uuid = hapd->wps->uuid;
901 static const u8 * get_own_uuid(struct hostapd_iface *iface)
904 if (iface->interfaces == NULL ||
905 iface->interfaces->for_each_interface == NULL)
908 iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
914 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
922 static int interface_count(struct hostapd_iface *iface)
925 if (iface->interfaces == NULL ||
926 iface->interfaces->for_each_interface == NULL)
928 iface->interfaces->for_each_interface(iface->interfaces,
929 count_interface_cb, &count);
934 static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
935 struct wps_context *wps)
939 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
940 wpabuf_free(wps->dev.vendor_ext[i]);
941 wps->dev.vendor_ext[i] = NULL;
943 if (hapd->conf->wps_vendor_ext[i] == NULL)
946 wps->dev.vendor_ext[i] =
947 wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
948 if (wps->dev.vendor_ext[i] == NULL) {
950 wpabuf_free(wps->dev.vendor_ext[i]);
959 int hostapd_init_wps(struct hostapd_data *hapd,
960 struct hostapd_bss_config *conf)
962 struct wps_context *wps;
963 struct wps_registrar_config cfg;
965 if (conf->wps_state == 0) {
966 hostapd_wps_clear_ies(hapd);
970 wps = os_zalloc(sizeof(*wps));
974 wps->cred_cb = hostapd_wps_cred_cb;
975 wps->event_cb = hostapd_wps_event_cb;
976 wps->rf_band_cb = hostapd_wps_rf_band_cb;
979 os_memset(&cfg, 0, sizeof(cfg));
980 wps->wps_state = hapd->conf->wps_state;
981 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
982 if (is_nil_uuid(hapd->conf->uuid)) {
984 uuid = get_own_uuid(hapd->iface);
985 if (uuid && !conf->wps_independent) {
986 os_memcpy(wps->uuid, uuid, UUID_LEN);
987 wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
988 "interface", wps->uuid, UUID_LEN);
990 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
991 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
992 "address", wps->uuid, UUID_LEN);
995 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
996 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
997 wps->uuid, UUID_LEN);
999 wps->ssid_len = hapd->conf->ssid.ssid_len;
1000 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
1002 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
1003 wps->dev.device_name = hapd->conf->device_name ?
1004 os_strdup(hapd->conf->device_name) : NULL;
1005 wps->dev.manufacturer = hapd->conf->manufacturer ?
1006 os_strdup(hapd->conf->manufacturer) : NULL;
1007 wps->dev.model_name = hapd->conf->model_name ?
1008 os_strdup(hapd->conf->model_name) : NULL;
1009 wps->dev.model_number = hapd->conf->model_number ?
1010 os_strdup(hapd->conf->model_number) : NULL;
1011 wps->dev.serial_number = hapd->conf->serial_number ?
1012 os_strdup(hapd->conf->serial_number) : NULL;
1013 wps->config_methods =
1014 wps_config_methods_str2bin(hapd->conf->config_methods);
1016 if ((wps->config_methods &
1017 (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1018 WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1019 wpa_printf(MSG_INFO, "WPS: Converting display to "
1020 "virtual_display for WPS 2.0 compliance");
1021 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1023 if ((wps->config_methods &
1024 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1025 WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1026 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1027 "virtual_push_button for WPS 2.0 compliance");
1028 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1030 #endif /* CONFIG_WPS2 */
1031 os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
1034 if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) {
1039 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
1041 if (conf->wps_rf_bands) {
1042 wps->dev.rf_bands = conf->wps_rf_bands;
1045 hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
1046 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
1049 if (conf->wpa & WPA_PROTO_RSN) {
1050 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1051 wps->auth_types |= WPS_AUTH_WPA2PSK;
1052 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1053 wps->auth_types |= WPS_AUTH_WPA2;
1055 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
1056 wps->encr_types |= WPS_ENCR_AES;
1057 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
1058 wps->encr_types |= WPS_ENCR_TKIP;
1061 if (conf->wpa & WPA_PROTO_WPA) {
1062 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1063 wps->auth_types |= WPS_AUTH_WPAPSK;
1064 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1065 wps->auth_types |= WPS_AUTH_WPA;
1067 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
1068 wps->encr_types |= WPS_ENCR_AES;
1069 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
1070 wps->encr_types |= WPS_ENCR_TKIP;
1073 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
1074 wps->encr_types |= WPS_ENCR_NONE;
1075 wps->auth_types |= WPS_AUTH_OPEN;
1076 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
1077 wps->encr_types |= WPS_ENCR_WEP;
1078 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
1079 wps->auth_types |= WPS_AUTH_OPEN;
1080 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
1081 wps->auth_types |= WPS_AUTH_SHARED;
1082 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
1083 wps->auth_types |= WPS_AUTH_OPEN;
1084 if (conf->default_wep_key_len)
1085 wps->encr_types |= WPS_ENCR_WEP;
1087 wps->encr_types |= WPS_ENCR_NONE;
1090 if (conf->ssid.wpa_psk_file) {
1091 /* Use per-device PSKs */
1092 } else if (conf->ssid.wpa_passphrase) {
1093 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1094 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1095 } else if (conf->ssid.wpa_psk) {
1096 wps->network_key = os_malloc(2 * PMK_LEN + 1);
1097 if (wps->network_key == NULL) {
1101 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1102 conf->ssid.wpa_psk->psk, PMK_LEN);
1103 wps->network_key_len = 2 * PMK_LEN;
1104 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1105 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
1106 if (wps->network_key == NULL) {
1110 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1111 conf->ssid.wep.len[0]);
1112 wps->network_key_len = conf->ssid.wep.len[0];
1115 if (conf->ssid.wpa_psk) {
1116 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1120 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
1121 /* Override parameters to enable security by default */
1122 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1123 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1126 wps->ap_settings = conf->ap_settings;
1127 wps->ap_settings_len = conf->ap_settings_len;
1129 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
1130 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
1131 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
1132 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
1133 cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
1135 cfg.skip_cred_build = conf->skip_cred_build;
1136 cfg.extra_cred = conf->extra_cred;
1137 cfg.extra_cred_len = conf->extra_cred_len;
1138 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
1139 conf->skip_cred_build;
1140 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
1141 cfg.static_wep_only = 1;
1142 cfg.dualband = interface_count(hapd->iface) > 1;
1143 if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
1144 (WPS_RF_50GHZ | WPS_RF_24GHZ))
1147 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
1149 wps->registrar = wps_registrar_init(wps, &cfg);
1150 if (wps->registrar == NULL) {
1151 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
1152 os_free(wps->network_key);
1157 #ifdef CONFIG_WPS_UPNP
1158 wps->friendly_name = hapd->conf->friendly_name;
1159 wps->manufacturer_url = hapd->conf->manufacturer_url;
1160 wps->model_description = hapd->conf->model_description;
1161 wps->model_url = hapd->conf->model_url;
1162 wps->upc = hapd->conf->upc;
1163 #endif /* CONFIG_WPS_UPNP */
1165 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
1173 int hostapd_init_wps_complete(struct hostapd_data *hapd)
1175 struct wps_context *wps = hapd->wps;
1180 #ifdef CONFIG_WPS_UPNP
1181 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
1182 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
1183 wps_registrar_deinit(wps->registrar);
1184 os_free(wps->network_key);
1189 #endif /* CONFIG_WPS_UPNP */
1195 static void hostapd_wps_nfc_clear(struct wps_context *wps)
1197 #ifdef CONFIG_WPS_NFC
1198 wps->ap_nfc_dev_pw_id = 0;
1199 wpabuf_free(wps->ap_nfc_dh_pubkey);
1200 wps->ap_nfc_dh_pubkey = NULL;
1201 wpabuf_free(wps->ap_nfc_dh_privkey);
1202 wps->ap_nfc_dh_privkey = NULL;
1203 wpabuf_free(wps->ap_nfc_dev_pw);
1204 wps->ap_nfc_dev_pw = NULL;
1205 #endif /* CONFIG_WPS_NFC */
1209 void hostapd_deinit_wps(struct hostapd_data *hapd)
1211 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1212 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1213 if (hapd->wps == NULL)
1215 #ifdef CONFIG_WPS_UPNP
1216 hostapd_wps_upnp_deinit(hapd);
1217 #endif /* CONFIG_WPS_UPNP */
1218 wps_registrar_deinit(hapd->wps->registrar);
1219 os_free(hapd->wps->network_key);
1220 wps_device_data_free(&hapd->wps->dev);
1221 wpabuf_free(hapd->wps->dh_pubkey);
1222 wpabuf_free(hapd->wps->dh_privkey);
1223 wps_free_pending_msgs(hapd->wps->upnp_msgs);
1224 hostapd_wps_nfc_clear(hapd->wps);
1227 hostapd_wps_clear_ies(hapd);
1231 void hostapd_update_wps(struct hostapd_data *hapd)
1233 if (hapd->wps == NULL)
1236 #ifdef CONFIG_WPS_UPNP
1237 hapd->wps->friendly_name = hapd->conf->friendly_name;
1238 hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
1239 hapd->wps->model_description = hapd->conf->model_description;
1240 hapd->wps->model_url = hapd->conf->model_url;
1241 hapd->wps->upc = hapd->conf->upc;
1242 #endif /* CONFIG_WPS_UPNP */
1244 hostapd_wps_set_vendor_ext(hapd, hapd->wps);
1246 if (hapd->conf->wps_state)
1247 wps_registrar_update_ie(hapd->wps->registrar);
1249 hostapd_deinit_wps(hapd);
1253 struct wps_add_pin_data {
1263 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1265 struct wps_add_pin_data *data = ctx;
1268 if (hapd->wps == NULL)
1270 ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1271 data->uuid, data->pin, data->pin_len,
1279 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1280 const char *uuid, const char *pin, int timeout)
1283 struct wps_add_pin_data data;
1287 data.pin = (const u8 *) pin;
1288 data.pin_len = os_strlen(pin);
1289 data.timeout = timeout;
1292 if (os_strcmp(uuid, "any") == 0)
1295 if (uuid_str2bin(uuid, u))
1299 if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1301 return data.added ? 0 : -1;
1305 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1307 const u8 *p2p_dev_addr = ctx;
1308 if (hapd->wps == NULL)
1310 return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
1314 int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1315 const u8 *p2p_dev_addr)
1317 return hostapd_wps_for_each(hapd, wps_button_pushed,
1318 (void *) p2p_dev_addr);
1322 static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1324 if (hapd->wps == NULL)
1327 wps_registrar_wps_cancel(hapd->wps->registrar);
1328 ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
1334 int hostapd_wps_cancel(struct hostapd_data *hapd)
1336 return hostapd_wps_for_each(hapd, wps_cancel, NULL);
1340 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1342 const u8 *ie, size_t ie_len,
1345 struct hostapd_data *hapd = ctx;
1346 struct wpabuf *wps_ie;
1347 struct ieee802_11_elems elems;
1349 if (hapd->wps == NULL)
1352 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1353 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1354 MACSTR, MAC2STR(addr));
1358 if (elems.ssid && elems.ssid_len > 0 &&
1359 (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1360 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1362 return 0; /* Not for us */
1364 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1367 if (wps_validate_probe_req(wps_ie, addr) < 0) {
1368 wpabuf_free(wps_ie);
1372 if (wpabuf_len(wps_ie) > 0) {
1373 int p2p_wildcard = 0;
1375 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1376 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1377 P2P_WILDCARD_SSID_LEN) == 0)
1379 #endif /* CONFIG_P2P */
1380 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1382 #ifdef CONFIG_WPS_UPNP
1383 /* FIX: what exactly should be included in the WLANEvent?
1384 * WPS attributes? Full ProbeReq frame? */
1386 upnp_wps_device_send_wlan_event(
1387 hapd->wps_upnp, addr,
1388 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1389 #endif /* CONFIG_WPS_UPNP */
1392 wpabuf_free(wps_ie);
1398 #ifdef CONFIG_WPS_UPNP
1400 static int hostapd_rx_req_put_wlan_response(
1401 void *priv, enum upnp_wps_wlanevent_type ev_type,
1402 const u8 *mac_addr, const struct wpabuf *msg,
1403 enum wps_msg_type msg_type)
1405 struct hostapd_data *hapd = priv;
1406 struct sta_info *sta;
1407 struct upnp_pending_message *p;
1409 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1410 MACSTR, ev_type, MAC2STR(mac_addr));
1411 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1412 wpabuf_head(msg), wpabuf_len(msg));
1413 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1414 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1415 "PutWLANResponse WLANEventType %d", ev_type);
1420 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1421 * server implementation for delivery to the peer.
1424 sta = ap_get_sta(hapd, mac_addr);
1425 #ifndef CONFIG_WPS_STRICT
1428 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1429 * Pick STA that is in an ongoing WPS registration without
1430 * checking the MAC address.
1432 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1433 "on NewWLANEventMAC; try wildcard match");
1434 for (sta = hapd->sta_list; sta; sta = sta->next) {
1435 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1439 #endif /* CONFIG_WPS_STRICT */
1441 if (!sta || !(sta->flags & WLAN_STA_WPS)) {
1442 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1446 p = os_zalloc(sizeof(*p));
1449 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1450 p->msg = wpabuf_dup(msg);
1452 p->next = hapd->wps->upnp_msgs;
1453 hapd->wps->upnp_msgs = p;
1455 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1459 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1460 struct wps_context *wps)
1462 struct upnp_wps_device_ctx *ctx;
1464 if (!hapd->conf->upnp_iface)
1466 ctx = os_zalloc(sizeof(*ctx));
1470 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1471 if (hapd->conf->ap_pin)
1472 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1474 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1475 hapd->conf->upnp_iface);
1476 if (hapd->wps_upnp == NULL)
1478 wps->wps_upnp = hapd->wps_upnp;
1484 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1486 upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1489 #endif /* CONFIG_WPS_UPNP */
1492 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1493 char *buf, size_t buflen)
1495 if (hapd->wps == NULL)
1497 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1501 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1503 struct hostapd_data *hapd = eloop_data;
1504 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1505 hostapd_wps_ap_pin_disable(hapd);
1506 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
1510 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1512 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1513 hapd->ap_pin_failures = 0;
1514 hapd->ap_pin_failures_consecutive = 0;
1515 hapd->conf->ap_setup_locked = 0;
1516 if (hapd->wps->ap_setup_locked) {
1517 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1518 hapd->wps->ap_setup_locked = 0;
1519 wps_registrar_update_ie(hapd->wps->registrar);
1521 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1523 eloop_register_timeout(timeout, 0,
1524 hostapd_wps_ap_pin_timeout, hapd, NULL);
1528 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1530 os_free(hapd->conf->ap_pin);
1531 hapd->conf->ap_pin = NULL;
1532 #ifdef CONFIG_WPS_UPNP
1533 upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1534 #endif /* CONFIG_WPS_UPNP */
1535 eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1540 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1542 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1543 hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1547 struct wps_ap_pin_data {
1553 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1555 struct wps_ap_pin_data *data = ctx;
1556 os_free(hapd->conf->ap_pin);
1557 hapd->conf->ap_pin = os_strdup(data->pin_txt);
1558 #ifdef CONFIG_WPS_UPNP
1559 upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1560 #endif /* CONFIG_WPS_UPNP */
1561 hostapd_wps_ap_pin_enable(hapd, data->timeout);
1566 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1569 struct wps_ap_pin_data data;
1571 pin = wps_generate_pin();
1572 os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
1573 data.timeout = timeout;
1574 hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1575 return hapd->conf->ap_pin;
1579 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1581 return hapd->conf->ap_pin;
1585 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1588 struct wps_ap_pin_data data;
1591 ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1592 if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
1594 data.timeout = timeout;
1595 return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1599 static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1602 wps_registrar_update_ie(hapd->wps->registrar);
1607 void hostapd_wps_update_ie(struct hostapd_data *hapd)
1609 hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1613 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1614 const char *auth, const char *encr, const char *key)
1616 struct wps_credential cred;
1619 os_memset(&cred, 0, sizeof(cred));
1621 len = os_strlen(ssid);
1622 if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1623 hexstr2bin(ssid, cred.ssid, len / 2))
1625 cred.ssid_len = len / 2;
1627 if (os_strncmp(auth, "OPEN", 4) == 0)
1628 cred.auth_type = WPS_AUTH_OPEN;
1629 else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1630 cred.auth_type = WPS_AUTH_WPAPSK;
1631 else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1632 cred.auth_type = WPS_AUTH_WPA2PSK;
1637 if (os_strncmp(encr, "NONE", 4) == 0)
1638 cred.encr_type = WPS_ENCR_NONE;
1639 else if (os_strncmp(encr, "WEP", 3) == 0)
1640 cred.encr_type = WPS_ENCR_WEP;
1641 else if (os_strncmp(encr, "TKIP", 4) == 0)
1642 cred.encr_type = WPS_ENCR_TKIP;
1643 else if (os_strncmp(encr, "CCMP", 4) == 0)
1644 cred.encr_type = WPS_ENCR_AES;
1648 cred.encr_type = WPS_ENCR_NONE;
1651 len = os_strlen(key);
1652 if ((len & 1) || len > 2 * sizeof(cred.key) ||
1653 hexstr2bin(key, cred.key, len / 2))
1655 cred.key_len = len / 2;
1658 return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1662 #ifdef CONFIG_WPS_NFC
1664 struct wps_nfc_password_token_data {
1665 const u8 *oob_dev_pw;
1666 size_t oob_dev_pw_len;
1671 static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1673 struct wps_nfc_password_token_data *data = ctx;
1676 if (hapd->wps == NULL)
1678 ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1680 data->oob_dev_pw_len);
1687 static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
1688 struct wps_parse_attr *attr)
1690 struct wps_nfc_password_token_data data;
1692 data.oob_dev_pw = attr->oob_dev_password;
1693 data.oob_dev_pw_len = attr->oob_dev_password_len;
1695 if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
1697 return data.added ? 0 : -1;
1701 static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
1702 const struct wpabuf *wps)
1704 struct wps_parse_attr attr;
1706 wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
1708 if (wps_parse_msg(wps, &attr)) {
1709 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
1713 if (attr.oob_dev_password)
1714 return hostapd_wps_add_nfc_password_token(hapd, &attr);
1716 wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
1721 int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
1722 const struct wpabuf *data)
1724 const struct wpabuf *wps = data;
1725 struct wpabuf *tmp = NULL;
1728 if (wpabuf_len(data) < 4)
1731 if (*wpabuf_head_u8(data) != 0x10) {
1732 /* Assume this contains full NDEF record */
1733 tmp = ndef_parse_wifi(data);
1735 wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
1741 ret = hostapd_wps_nfc_tag_process(hapd, wps);
1747 struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
1752 if (hapd->wps == NULL)
1755 ret = wps_get_oob_cred(hapd->wps);
1758 tmp = ndef_build_wifi(ret);
1769 struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
1772 * Handover Select carrier record for WPS uses the same format as
1773 * configuration token.
1775 return hostapd_wps_nfc_config_token(hapd, ndef);
1779 struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
1781 if (hapd->conf->wps_nfc_pw_from_config) {
1782 return wps_nfc_token_build(ndef,
1783 hapd->conf->wps_nfc_dev_pw_id,
1784 hapd->conf->wps_nfc_dh_pubkey,
1785 hapd->conf->wps_nfc_dev_pw);
1788 return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
1789 &hapd->conf->wps_nfc_dh_pubkey,
1790 &hapd->conf->wps_nfc_dh_privkey,
1791 &hapd->conf->wps_nfc_dev_pw);
1795 int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
1797 struct wps_context *wps = hapd->wps;
1803 if (!hapd->conf->wps_nfc_dh_pubkey ||
1804 !hapd->conf->wps_nfc_dh_privkey ||
1805 !hapd->conf->wps_nfc_dev_pw ||
1806 !hapd->conf->wps_nfc_dev_pw_id)
1809 hostapd_wps_nfc_clear(wps);
1810 wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
1811 wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
1812 wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
1813 pw = hapd->conf->wps_nfc_dev_pw;
1814 wps->ap_nfc_dev_pw = wpabuf_alloc(
1815 wpabuf_len(pw) * 2 + 1);
1816 if (wps->ap_nfc_dev_pw) {
1817 wpa_snprintf_hex_uppercase(
1818 (char *) wpabuf_put(wps->ap_nfc_dev_pw,
1819 wpabuf_len(pw) * 2),
1820 wpabuf_len(pw) * 2 + 1,
1821 wpabuf_head(pw), wpabuf_len(pw));
1824 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
1825 !wps->ap_nfc_dev_pw) {
1826 hostapd_wps_nfc_clear(wps);
1834 void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
1836 hostapd_wps_nfc_clear(hapd->wps);
1839 #endif /* CONFIG_WPS_NFC */