2 * hostapd / WPS integration
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
22 #include "ieee802_11_defs.h"
23 #include "ieee802_11_common.h"
27 #include "wps/wps_defs.h"
28 #include "wps/wps_dev_attr.h"
29 #include "wps_hostapd.h"
30 #include "dh_groups.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 void hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
41 const u8 *ie, size_t ie_len);
44 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
47 struct hostapd_data *hapd = ctx;
48 struct hostapd_wpa_psk *p;
49 struct hostapd_ssid *ssid = &hapd->conf->ssid;
51 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
52 MACSTR, MAC2STR(mac_addr));
53 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
55 if (psk_len != PMK_LEN) {
56 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
57 (unsigned long) psk_len);
61 /* Add the new PSK to runtime PSK list */
62 p = os_zalloc(sizeof(*p));
65 os_memcpy(p->addr, mac_addr, ETH_ALEN);
66 os_memcpy(p->psk, psk, PMK_LEN);
68 p->next = ssid->wpa_psk;
71 if (ssid->wpa_psk_file) {
73 char hex[PMK_LEN * 2 + 1];
74 /* Add the new PSK to PSK list file */
75 f = fopen(ssid->wpa_psk_file, "a");
77 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
78 "'%s'", ssid->wpa_psk_file);
82 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
83 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
91 static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
92 size_t beacon_ie_len, const u8 *probe_resp_ie,
93 size_t probe_resp_ie_len)
95 struct hostapd_data *hapd = ctx;
97 os_free(hapd->wps_beacon_ie);
98 if (beacon_ie_len == 0) {
99 hapd->wps_beacon_ie = NULL;
100 hapd->wps_beacon_ie_len = 0;
102 hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
103 if (hapd->wps_beacon_ie == NULL) {
104 hapd->wps_beacon_ie_len = 0;
107 os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
108 hapd->wps_beacon_ie_len = beacon_ie_len;
110 hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
111 hapd->wps_beacon_ie_len);
113 os_free(hapd->wps_probe_resp_ie);
114 if (probe_resp_ie_len == 0) {
115 hapd->wps_probe_resp_ie = NULL;
116 hapd->wps_probe_resp_ie_len = 0;
118 hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
119 if (hapd->wps_probe_resp_ie == NULL) {
120 hapd->wps_probe_resp_ie_len = 0;
123 os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
125 hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
127 hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
128 hapd->wps_probe_resp_ie_len);
134 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
135 const struct wps_device_data *dev)
137 struct hostapd_data *hapd = ctx;
138 char uuid[40], txt[400];
140 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
142 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
143 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
144 "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
145 uuid, MAC2STR(dev->mac_addr), dev->device_name,
146 dev->manufacturer, dev->model_name,
147 dev->model_number, dev->serial_number,
148 dev->categ, dev->oui, dev->sub_categ);
149 if (len > 0 && len < (int) sizeof(txt))
150 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
152 if (hapd->conf->wps_pin_requests) {
155 f = fopen(hapd->conf->wps_pin_requests, "a");
159 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
161 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
162 dev->manufacturer, dev->model_name, dev->model_number,
164 dev->categ, dev->oui, dev->sub_categ);
170 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
173 struct hostapd_data *hapd = ctx;
175 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
177 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
178 MAC2STR(mac_addr), uuid);
182 static int str_starts(const char *str, const char *start)
184 return os_strncmp(str, start, os_strlen(start)) == 0;
188 static void wps_reload_config(void *eloop_data, void *user_ctx)
190 struct hostapd_iface *iface = eloop_data;
192 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
193 if (hostapd_reload_config(iface) < 0) {
194 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
200 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
202 struct hostapd_data *hapd = ctx;
210 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
211 cred->cred_attr, cred->cred_attr_len);
213 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
214 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
215 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
217 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
218 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
219 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
220 cred->key, cred->key_len);
221 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
222 MAC2STR(cred->mac_addr));
224 if ((hapd->conf->wps_cred_processing == 1 ||
225 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
226 size_t blen = cred->cred_attr_len * 2 + 1;
227 char *_buf = os_malloc(blen);
229 wpa_snprintf_hex(_buf, blen,
230 cred->cred_attr, cred->cred_attr_len);
231 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
232 WPS_EVENT_NEW_AP_SETTINGS, _buf);
236 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
238 if (hapd->conf->wps_cred_processing == 1)
241 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
242 hapd->wps->ssid_len = cred->ssid_len;
243 hapd->wps->encr_types = cred->encr_type;
244 hapd->wps->auth_types = cred->auth_type;
245 if (cred->key == NULL) {
246 os_free(hapd->wps->network_key);
247 hapd->wps->network_key = NULL;
248 hapd->wps->network_key_len = 0;
250 if (hapd->wps->network_key == NULL ||
251 hapd->wps->network_key_len < cred->key_len) {
252 hapd->wps->network_key_len = 0;
253 os_free(hapd->wps->network_key);
254 hapd->wps->network_key = os_malloc(cred->key_len);
255 if (hapd->wps->network_key == NULL)
258 hapd->wps->network_key_len = cred->key_len;
259 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
261 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
263 len = os_strlen(hapd->iface->config_fname) + 5;
264 tmp_fname = os_malloc(len);
265 if (tmp_fname == NULL)
267 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
269 oconf = fopen(hapd->iface->config_fname, "r");
271 wpa_printf(MSG_WARNING, "WPS: Could not open current "
272 "configuration file");
277 nconf = fopen(tmp_fname, "w");
279 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
280 "configuration file");
286 fprintf(nconf, "# WPS configuration - START\n");
288 fprintf(nconf, "wps_state=2\n");
290 fprintf(nconf, "ssid=");
291 for (i = 0; i < cred->ssid_len; i++)
292 fputc(cred->ssid[i], nconf);
293 fprintf(nconf, "\n");
295 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
296 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
298 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
300 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
307 fprintf(nconf, "wpa=%d\n", wpa);
309 fprintf(nconf, "wpa_key_mgmt=");
311 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
312 fprintf(nconf, "WPA-EAP");
315 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
316 fprintf(nconf, "%sWPA-PSK", prefix);
317 fprintf(nconf, "\n");
319 fprintf(nconf, "wpa_pairwise=");
321 if (cred->encr_type & WPS_ENCR_AES) {
322 fprintf(nconf, "CCMP");
325 if (cred->encr_type & WPS_ENCR_TKIP) {
326 fprintf(nconf, "%sTKIP", prefix);
328 fprintf(nconf, "\n");
330 if (cred->key_len >= 8 && cred->key_len < 64) {
331 fprintf(nconf, "wpa_passphrase=");
332 for (i = 0; i < cred->key_len; i++)
333 fputc(cred->key[i], nconf);
334 fprintf(nconf, "\n");
335 } else if (cred->key_len == 64) {
336 fprintf(nconf, "wpa_psk=");
337 for (i = 0; i < cred->key_len; i++)
338 fputc(cred->key[i], nconf);
339 fprintf(nconf, "\n");
341 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
343 (unsigned long) cred->key_len);
346 fprintf(nconf, "auth_algs=1\n");
348 if ((cred->auth_type & WPS_AUTH_OPEN) &&
349 (cred->auth_type & WPS_AUTH_SHARED))
350 fprintf(nconf, "auth_algs=3\n");
351 else if (cred->auth_type & WPS_AUTH_SHARED)
352 fprintf(nconf, "auth_algs=2\n");
354 fprintf(nconf, "auth_algs=1\n");
356 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
357 int key_idx = cred->key_idx;
360 fprintf(nconf, "wep_default_key=%d\n", key_idx);
361 fprintf(nconf, "wep_key%d=", key_idx);
362 if (cred->key_len == 10 || cred->key_len == 26) {
363 /* WEP key as a hex string */
364 for (i = 0; i < cred->key_len; i++)
365 fputc(cred->key[i], nconf);
367 /* Raw WEP key; convert to hex */
368 for (i = 0; i < cred->key_len; i++)
369 fprintf(nconf, "%02x", cred->key[i]);
371 fprintf(nconf, "\n");
375 fprintf(nconf, "# WPS configuration - END\n");
378 while (fgets(buf, sizeof(buf), oconf)) {
379 if (os_strncmp(buf, "bss=", 4) == 0)
382 (str_starts(buf, "ssid=") ||
383 str_starts(buf, "auth_algs=") ||
384 str_starts(buf, "wps_state=") ||
385 str_starts(buf, "wpa=") ||
386 str_starts(buf, "wpa_psk=") ||
387 str_starts(buf, "wpa_pairwise=") ||
388 str_starts(buf, "rsn_pairwise=") ||
389 str_starts(buf, "wpa_key_mgmt=") ||
390 str_starts(buf, "wpa_passphrase="))) {
391 fprintf(nconf, "#WPS# %s", buf);
393 fprintf(nconf, "%s", buf);
399 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
400 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
401 "configuration file: %s", strerror(errno));
408 /* Schedule configuration reload after short period of time to allow
409 * EAP-WSC to be finished.
411 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
414 /* TODO: dualband AP may need to update multiple configuration files */
416 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
422 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
423 struct wps_event_pwd_auth_fail *data)
431 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
432 * if this happens multiple times.
434 hapd->ap_pin_failures++;
435 if (hapd->ap_pin_failures < 4)
438 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
439 hapd->wps->ap_setup_locked = 1;
441 wps_registrar_update_ie(hapd->wps->registrar);
443 if (hapd->conf->wps_cred_processing == 1)
446 f = fopen(hapd->iface->config_fname, "a");
448 wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
449 "configuration file");
453 fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
454 fprintf(f, "ap_setup_locked=1\n");
457 /* TODO: dualband AP may need to update multiple configuration files */
459 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
463 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
464 union wps_event_data *data)
466 struct hostapd_data *hapd = ctx;
468 if (event == WPS_EV_PWD_AUTH_FAIL)
469 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
473 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
475 os_free(hapd->wps_beacon_ie);
476 hapd->wps_beacon_ie = NULL;
477 hapd->wps_beacon_ie_len = 0;
478 hostapd_set_wps_beacon_ie(hapd, NULL, 0);
480 os_free(hapd->wps_probe_resp_ie);
481 hapd->wps_probe_resp_ie = NULL;
482 hapd->wps_probe_resp_ie_len = 0;
483 hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
487 int hostapd_init_wps(struct hostapd_data *hapd,
488 struct hostapd_bss_config *conf)
490 struct wps_context *wps;
491 struct wps_registrar_config cfg;
493 if (conf->wps_state == 0) {
494 hostapd_wps_clear_ies(hapd);
498 wps = os_zalloc(sizeof(*wps));
502 wps->cred_cb = hostapd_wps_cred_cb;
503 wps->event_cb = hostapd_wps_event_cb;
506 os_memset(&cfg, 0, sizeof(cfg));
507 wps->wps_state = hapd->conf->wps_state;
508 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
509 if (is_nil_uuid(hapd->conf->uuid)) {
510 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
511 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
512 wps->uuid, UUID_LEN);
514 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
515 wps->ssid_len = hapd->conf->ssid.ssid_len;
516 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
518 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
519 wps->dev.device_name = hapd->conf->device_name ?
520 os_strdup(hapd->conf->device_name) : NULL;
521 wps->dev.manufacturer = hapd->conf->manufacturer ?
522 os_strdup(hapd->conf->manufacturer) : NULL;
523 wps->dev.model_name = hapd->conf->model_name ?
524 os_strdup(hapd->conf->model_name) : NULL;
525 wps->dev.model_number = hapd->conf->model_number ?
526 os_strdup(hapd->conf->model_number) : NULL;
527 wps->dev.serial_number = hapd->conf->serial_number ?
528 os_strdup(hapd->conf->serial_number) : NULL;
529 if (hapd->conf->config_methods) {
530 char *m = hapd->conf->config_methods;
531 if (os_strstr(m, "label"))
532 wps->config_methods |= WPS_CONFIG_LABEL;
533 if (os_strstr(m, "display"))
534 wps->config_methods |= WPS_CONFIG_DISPLAY;
535 if (os_strstr(m, "push_button"))
536 wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
537 if (os_strstr(m, "keypad"))
538 wps->config_methods |= WPS_CONFIG_KEYPAD;
540 if (hapd->conf->device_type) {
543 /* <categ>-<OUI>-<subcateg> */
544 wps->dev.categ = atoi(hapd->conf->device_type);
545 pos = os_strchr(hapd->conf->device_type, '-');
547 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
552 if (hexstr2bin(pos, oui, 4)) {
553 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
557 wps->dev.oui = WPA_GET_BE32(oui);
558 pos = os_strchr(pos, '-');
560 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
565 wps->dev.sub_categ = atoi(pos);
567 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
568 wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
569 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
571 if (conf->wpa & WPA_PROTO_RSN) {
572 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
573 wps->auth_types |= WPS_AUTH_WPA2PSK;
574 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
575 wps->auth_types |= WPS_AUTH_WPA2;
577 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
578 wps->encr_types |= WPS_ENCR_AES;
579 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
580 wps->encr_types |= WPS_ENCR_TKIP;
583 if (conf->wpa & WPA_PROTO_WPA) {
584 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
585 wps->auth_types |= WPS_AUTH_WPAPSK;
586 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
587 wps->auth_types |= WPS_AUTH_WPA;
589 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
590 wps->encr_types |= WPS_ENCR_AES;
591 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
592 wps->encr_types |= WPS_ENCR_TKIP;
595 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
596 wps->encr_types |= WPS_ENCR_NONE;
597 wps->auth_types |= WPS_AUTH_OPEN;
598 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
599 wps->encr_types |= WPS_ENCR_WEP;
600 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
601 wps->auth_types |= WPS_AUTH_OPEN;
602 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
603 wps->auth_types |= WPS_AUTH_SHARED;
604 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
605 wps->auth_types |= WPS_AUTH_OPEN;
606 if (conf->default_wep_key_len)
607 wps->encr_types |= WPS_ENCR_WEP;
609 wps->encr_types |= WPS_ENCR_NONE;
612 if (conf->ssid.wpa_psk_file) {
613 /* Use per-device PSKs */
614 } else if (conf->ssid.wpa_passphrase) {
615 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
616 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
617 } else if (conf->ssid.wpa_psk) {
618 wps->network_key = os_malloc(2 * PMK_LEN + 1);
619 if (wps->network_key == NULL) {
623 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
624 conf->ssid.wpa_psk->psk, PMK_LEN);
625 wps->network_key_len = 2 * PMK_LEN;
626 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
627 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
628 if (wps->network_key == NULL) {
632 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
633 conf->ssid.wep.len[0]);
634 wps->network_key_len = conf->ssid.wep.len[0];
637 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
638 /* Override parameters to enable security by default */
639 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
640 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
643 wps->ap_settings = conf->ap_settings;
644 wps->ap_settings_len = conf->ap_settings_len;
646 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
647 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
648 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
649 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
651 cfg.skip_cred_build = conf->skip_cred_build;
652 cfg.extra_cred = conf->extra_cred;
653 cfg.extra_cred_len = conf->extra_cred_len;
654 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
655 conf->skip_cred_build;
656 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
657 cfg.static_wep_only = 1;
659 wps->registrar = wps_registrar_init(wps, &cfg);
660 if (wps->registrar == NULL) {
661 printf("Failed to initialize WPS Registrar\n");
662 os_free(wps->network_key);
667 #ifdef CONFIG_WPS_UPNP
668 wps->friendly_name = hapd->conf->friendly_name;
669 wps->manufacturer_url = hapd->conf->manufacturer_url;
670 wps->model_description = hapd->conf->model_description;
671 wps->model_url = hapd->conf->model_url;
672 wps->upc = hapd->conf->upc;
674 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
675 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
676 wps_registrar_deinit(wps->registrar);
677 os_free(wps->network_key);
681 #endif /* CONFIG_WPS_UPNP */
683 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
691 void hostapd_deinit_wps(struct hostapd_data *hapd)
693 if (hapd->wps == NULL)
695 #ifdef CONFIG_WPS_UPNP
696 hostapd_wps_upnp_deinit(hapd);
697 #endif /* CONFIG_WPS_UPNP */
698 wps_registrar_deinit(hapd->wps->registrar);
699 os_free(hapd->wps->network_key);
700 wps_device_data_free(&hapd->wps->dev);
701 wpabuf_free(hapd->wps->dh_pubkey);
702 wpabuf_free(hapd->wps->dh_privkey);
703 wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
704 wpabuf_free(hapd->wps->oob_conf.dev_password);
705 wps_free_pending_msgs(hapd->wps->upnp_msgs);
708 hostapd_wps_clear_ies(hapd);
712 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
713 const char *pin, int timeout)
718 if (hapd->wps == NULL)
720 if (os_strcmp(uuid, "any") == 0)
722 else if (uuid_str2bin(uuid, u))
724 return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
725 (const u8 *) pin, os_strlen(pin),
730 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
732 if (hapd->wps == NULL)
734 return wps_registrar_button_pushed(hapd->wps->registrar);
738 #ifdef CONFIG_WPS_OOB
739 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
740 char *path, char *method, char *name)
742 struct wps_context *wps = hapd->wps;
743 struct oob_device_data *oob_dev;
745 oob_dev = wps_get_oob_device(device_type);
748 oob_dev->device_path = path;
749 oob_dev->device_name = name;
750 wps->oob_conf.oob_method = wps_get_oob_method(method);
752 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
754 * Use pre-configured DH keys in order to be able to write the
755 * key hash into the OOB file.
757 wpabuf_free(wps->dh_pubkey);
758 wpabuf_free(wps->dh_privkey);
759 wps->dh_privkey = NULL;
760 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
762 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
763 if (wps->dh_pubkey == NULL) {
764 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
765 "Diffie-Hellman handshake");
770 if (wps_process_oob(wps, oob_dev, 1) < 0)
773 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
774 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
775 hostapd_wps_add_pin(hapd, "any",
776 wpabuf_head(wps->oob_conf.dev_password), 0) <
783 wpabuf_free(wps->dh_pubkey);
784 wps->dh_pubkey = NULL;
785 wpabuf_free(wps->dh_privkey);
786 wps->dh_privkey = NULL;
789 #endif /* CONFIG_WPS_OOB */
792 static void hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
793 const u8 *ie, size_t ie_len)
795 struct hostapd_data *hapd = ctx;
796 struct wpabuf *wps_ie;
798 if (hapd->wps == NULL)
801 wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
805 if (wpabuf_len(wps_ie) > 0) {
806 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
807 #ifdef CONFIG_WPS_UPNP
808 /* FIX: what exactly should be included in the WLANEvent?
809 * WPS attributes? Full ProbeReq frame? */
810 upnp_wps_device_send_wlan_event(hapd->wps_upnp, addr,
811 UPNP_WPS_WLANEVENT_TYPE_PROBE,
813 #endif /* CONFIG_WPS_UPNP */
820 #ifdef CONFIG_WPS_UPNP
822 static struct wpabuf *
823 hostapd_rx_req_get_device_info(void *priv, struct upnp_wps_peer *peer)
825 struct hostapd_data *hapd = priv;
826 struct wps_config cfg;
827 struct wps_data *wps;
828 enum wsc_op_code op_code;
832 * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
833 * registration over UPnP with the AP acting as an Enrollee. It should
834 * be noted that this is frequently used just to get the device data,
835 * i.e., there may not be any intent to actually complete the
840 wps_deinit(peer->wps);
842 os_memset(&cfg, 0, sizeof(cfg));
844 cfg.pin = (u8 *) hapd->conf->ap_pin;
845 cfg.pin_len = os_strlen(hapd->conf->ap_pin);
846 wps = wps_init(&cfg);
850 m1 = wps_get_msg(wps, &op_code);
862 static struct wpabuf *
863 hostapd_rx_req_put_message(void *priv, struct upnp_wps_peer *peer,
864 const struct wpabuf *msg)
866 enum wps_process_res res;
867 enum wsc_op_code op_code;
869 /* PutMessage: msg = InMessage, return OutMessage */
870 res = wps_process_msg(peer->wps, WSC_UPnP, msg);
871 if (res == WPS_FAILURE)
873 return wps_get_msg(peer->wps, &op_code);
877 static struct wpabuf *
878 hostapd_rx_req_get_ap_settings(void *priv, const struct wpabuf *msg)
880 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
885 static int hostapd_rx_req_set_ap_settings(void *priv, const struct wpabuf *msg)
887 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
892 static int hostapd_rx_req_del_ap_settings(void *priv, const struct wpabuf *msg)
894 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
899 static struct wpabuf *
900 hostapd_rx_req_get_sta_settings(void *priv, const struct wpabuf *msg)
902 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
907 static int hostapd_rx_req_set_sta_settings(void *priv,
908 const struct wpabuf *msg)
910 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
915 static int hostapd_rx_req_del_sta_settings(void *priv,
916 const struct wpabuf *msg)
918 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
923 static int hostapd_rx_req_put_wlan_response(
924 void *priv, enum upnp_wps_wlanevent_type ev_type,
925 const u8 *mac_addr, const struct wpabuf *msg,
926 enum wps_msg_type msg_type)
928 struct hostapd_data *hapd = priv;
929 struct sta_info *sta;
930 struct upnp_pending_message *p;
932 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
933 MACSTR, ev_type, MAC2STR(mac_addr));
934 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
935 wpabuf_head(msg), wpabuf_len(msg));
936 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
937 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
938 "PutWLANResponse WLANEventType %d", ev_type);
943 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
944 * server implementation for delivery to the peer.
947 sta = ap_get_sta(hapd, mac_addr);
950 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
951 * Pick STA that is in an ongoing WPS registration without
952 * checking the MAC address.
954 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
955 "on NewWLANEventMAC; try wildcard match");
956 for (sta = hapd->sta_list; sta; sta = sta->next) {
957 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
963 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
967 p = os_zalloc(sizeof(*p));
970 os_memcpy(p->addr, sta->addr, ETH_ALEN);
971 p->msg = wpabuf_dup(msg);
973 p->next = hapd->wps->upnp_msgs;
974 hapd->wps->upnp_msgs = p;
976 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
980 static int hostapd_rx_req_set_selected_registrar(void *priv,
981 const struct wpabuf *msg)
983 struct hostapd_data *hapd = priv;
984 return wps_registrar_set_selected_registrar(hapd->wps->registrar, msg);
988 static int hostapd_rx_req_reboot_ap(void *priv, const struct wpabuf *msg)
990 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
995 static int hostapd_rx_req_reset_ap(void *priv, const struct wpabuf *msg)
997 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1002 static int hostapd_rx_req_reboot_sta(void *priv, const struct wpabuf *msg)
1004 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1009 static int hostapd_rx_req_reset_sta(void *priv, const struct wpabuf *msg)
1011 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1016 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1017 struct wps_context *wps)
1019 struct upnp_wps_device_ctx *ctx;
1021 if (!hapd->conf->upnp_iface)
1023 ctx = os_zalloc(sizeof(*ctx));
1027 ctx->rx_req_get_device_info = hostapd_rx_req_get_device_info;
1028 ctx->rx_req_put_message = hostapd_rx_req_put_message;
1029 ctx->rx_req_get_ap_settings = hostapd_rx_req_get_ap_settings;
1030 ctx->rx_req_set_ap_settings = hostapd_rx_req_set_ap_settings;
1031 ctx->rx_req_del_ap_settings = hostapd_rx_req_del_ap_settings;
1032 ctx->rx_req_get_sta_settings = hostapd_rx_req_get_sta_settings;
1033 ctx->rx_req_set_sta_settings = hostapd_rx_req_set_sta_settings;
1034 ctx->rx_req_del_sta_settings = hostapd_rx_req_del_sta_settings;
1035 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1036 ctx->rx_req_set_selected_registrar =
1037 hostapd_rx_req_set_selected_registrar;
1038 ctx->rx_req_reboot_ap = hostapd_rx_req_reboot_ap;
1039 ctx->rx_req_reset_ap = hostapd_rx_req_reset_ap;
1040 ctx->rx_req_reboot_sta = hostapd_rx_req_reboot_sta;
1041 ctx->rx_req_reset_sta = hostapd_rx_req_reset_sta;
1043 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
1044 if (hapd->wps_upnp == NULL) {
1048 wps->wps_upnp = hapd->wps_upnp;
1050 if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
1051 upnp_wps_device_deinit(hapd->wps_upnp);
1052 hapd->wps_upnp = NULL;
1060 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1062 upnp_wps_device_deinit(hapd->wps_upnp);
1065 #endif /* CONFIG_WPS_UPNP */
1068 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1069 char *buf, size_t buflen)
1071 return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);