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"
26 #include "wps/wps_defs.h"
27 #include "wps/wps_dev_attr.h"
28 #include "wps_hostapd.h"
29 #include "dh_groups.h"
32 #ifdef CONFIG_WPS_UPNP
33 #include "wps/wps_upnp.h"
34 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
35 struct wps_context *wps);
36 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
37 #endif /* CONFIG_WPS_UPNP */
39 static void hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
40 const u8 *ie, size_t ie_len);
43 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
46 struct hostapd_data *hapd = ctx;
47 struct hostapd_wpa_psk *p;
48 struct hostapd_ssid *ssid = &hapd->conf->ssid;
50 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
51 MACSTR, MAC2STR(mac_addr));
52 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
54 if (psk_len != PMK_LEN) {
55 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
56 (unsigned long) psk_len);
60 /* Add the new PSK to runtime PSK list */
61 p = os_zalloc(sizeof(*p));
64 os_memcpy(p->addr, mac_addr, ETH_ALEN);
65 os_memcpy(p->psk, psk, PMK_LEN);
67 p->next = ssid->wpa_psk;
70 if (ssid->wpa_psk_file) {
72 char hex[PMK_LEN * 2 + 1];
73 /* Add the new PSK to PSK list file */
74 f = fopen(ssid->wpa_psk_file, "a");
76 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
77 "'%s'", ssid->wpa_psk_file);
81 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
82 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
90 static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
91 size_t beacon_ie_len, const u8 *probe_resp_ie,
92 size_t probe_resp_ie_len)
94 struct hostapd_data *hapd = ctx;
96 os_free(hapd->wps_beacon_ie);
97 if (beacon_ie_len == 0) {
98 hapd->wps_beacon_ie = NULL;
99 hapd->wps_beacon_ie_len = 0;
101 hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
102 if (hapd->wps_beacon_ie == NULL) {
103 hapd->wps_beacon_ie_len = 0;
106 os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
107 hapd->wps_beacon_ie_len = beacon_ie_len;
109 hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
110 hapd->wps_beacon_ie_len);
112 os_free(hapd->wps_probe_resp_ie);
113 if (probe_resp_ie_len == 0) {
114 hapd->wps_probe_resp_ie = NULL;
115 hapd->wps_probe_resp_ie_len = 0;
117 hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
118 if (hapd->wps_probe_resp_ie == NULL) {
119 hapd->wps_probe_resp_ie_len = 0;
122 os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
124 hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
126 hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
127 hapd->wps_probe_resp_ie_len);
133 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
134 const struct wps_device_data *dev)
136 struct hostapd_data *hapd = ctx;
137 char uuid[40], txt[400];
139 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
141 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
142 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
143 "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
144 uuid, MAC2STR(dev->mac_addr), dev->device_name,
145 dev->manufacturer, dev->model_name,
146 dev->model_number, dev->serial_number,
147 dev->categ, dev->oui, dev->sub_categ);
148 if (len > 0 && len < (int) sizeof(txt))
149 wpa_msg(hapd, MSG_INFO, "%s", txt);
151 if (hapd->conf->wps_pin_requests) {
154 f = fopen(hapd->conf->wps_pin_requests, "a");
158 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
160 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
161 dev->manufacturer, dev->model_name, dev->model_number,
163 dev->categ, dev->oui, dev->sub_categ);
169 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
172 struct hostapd_data *hapd = ctx;
174 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
176 wpa_msg(hapd, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
177 MAC2STR(mac_addr), uuid);
181 static int str_starts(const char *str, const char *start)
183 return os_strncmp(str, start, os_strlen(start)) == 0;
187 static void wps_reload_config(void *eloop_data, void *user_ctx)
189 struct hostapd_iface *iface = eloop_data;
191 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
192 if (hostapd_reload_config(iface) < 0) {
193 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
199 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
201 struct hostapd_data *hapd = ctx;
209 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
210 cred->cred_attr, cred->cred_attr_len);
212 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
213 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
214 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
216 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
217 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
218 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
219 cred->key, cred->key_len);
220 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
221 MAC2STR(cred->mac_addr));
223 if ((hapd->conf->wps_cred_processing == 1 ||
224 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
225 size_t blen = cred->cred_attr_len * 2 + 1;
226 char *_buf = os_malloc(blen);
228 wpa_snprintf_hex(_buf, blen,
229 cred->cred_attr, cred->cred_attr_len);
230 wpa_msg(hapd, MSG_INFO, "%s%s",
231 WPS_EVENT_NEW_AP_SETTINGS, _buf);
235 wpa_msg(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
237 if (hapd->conf->wps_cred_processing == 1)
240 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
241 hapd->wps->ssid_len = cred->ssid_len;
242 hapd->wps->encr_types = cred->encr_type;
243 hapd->wps->auth_types = cred->auth_type;
244 if (cred->key == NULL) {
245 os_free(hapd->wps->network_key);
246 hapd->wps->network_key = NULL;
247 hapd->wps->network_key_len = 0;
249 if (hapd->wps->network_key == NULL ||
250 hapd->wps->network_key_len < cred->key_len) {
251 hapd->wps->network_key_len = 0;
252 os_free(hapd->wps->network_key);
253 hapd->wps->network_key = os_malloc(cred->key_len);
254 if (hapd->wps->network_key == NULL)
257 hapd->wps->network_key_len = cred->key_len;
258 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
260 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
262 len = os_strlen(hapd->iface->config_fname) + 5;
263 tmp_fname = os_malloc(len);
264 if (tmp_fname == NULL)
266 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
268 oconf = fopen(hapd->iface->config_fname, "r");
270 wpa_printf(MSG_WARNING, "WPS: Could not open current "
271 "configuration file");
276 nconf = fopen(tmp_fname, "w");
278 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
279 "configuration file");
285 fprintf(nconf, "# WPS configuration - START\n");
287 fprintf(nconf, "wps_state=2\n");
289 fprintf(nconf, "ssid=");
290 for (i = 0; i < cred->ssid_len; i++)
291 fputc(cred->ssid[i], nconf);
292 fprintf(nconf, "\n");
294 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
295 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
297 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
299 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
306 fprintf(nconf, "wpa=%d\n", wpa);
308 fprintf(nconf, "wpa_key_mgmt=");
310 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
311 fprintf(nconf, "WPA-EAP");
314 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
315 fprintf(nconf, "%sWPA-PSK", prefix);
316 fprintf(nconf, "\n");
318 fprintf(nconf, "wpa_pairwise=");
320 if (cred->encr_type & WPS_ENCR_AES) {
321 fprintf(nconf, "CCMP");
324 if (cred->encr_type & WPS_ENCR_TKIP) {
325 fprintf(nconf, "%sTKIP", prefix);
327 fprintf(nconf, "\n");
329 if (cred->key_len >= 8 && cred->key_len < 64) {
330 fprintf(nconf, "wpa_passphrase=");
331 for (i = 0; i < cred->key_len; i++)
332 fputc(cred->key[i], nconf);
333 fprintf(nconf, "\n");
334 } else if (cred->key_len == 64) {
335 fprintf(nconf, "wpa_psk=");
336 for (i = 0; i < cred->key_len; i++)
337 fputc(cred->key[i], nconf);
338 fprintf(nconf, "\n");
340 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
342 (unsigned long) cred->key_len);
345 fprintf(nconf, "auth_algs=1\n");
347 if ((cred->auth_type & WPS_AUTH_OPEN) &&
348 (cred->auth_type & WPS_AUTH_SHARED))
349 fprintf(nconf, "auth_algs=3\n");
350 else if (cred->auth_type & WPS_AUTH_SHARED)
351 fprintf(nconf, "auth_algs=2\n");
353 fprintf(nconf, "auth_algs=1\n");
355 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
356 int key_idx = cred->key_idx;
359 fprintf(nconf, "wep_default_key=%d\n", key_idx);
360 fprintf(nconf, "wep_key%d=", key_idx);
361 if (cred->key_len == 10 || cred->key_len == 26) {
362 /* WEP key as a hex string */
363 for (i = 0; i < cred->key_len; i++)
364 fputc(cred->key[i], nconf);
366 /* Raw WEP key; convert to hex */
367 for (i = 0; i < cred->key_len; i++)
368 fprintf(nconf, "%02x", cred->key[i]);
370 fprintf(nconf, "\n");
374 fprintf(nconf, "# WPS configuration - END\n");
377 while (fgets(buf, sizeof(buf), oconf)) {
378 if (os_strncmp(buf, "bss=", 4) == 0)
381 (str_starts(buf, "ssid=") ||
382 str_starts(buf, "auth_algs=") ||
383 str_starts(buf, "wps_state=") ||
384 str_starts(buf, "wpa=") ||
385 str_starts(buf, "wpa_psk=") ||
386 str_starts(buf, "wpa_pairwise=") ||
387 str_starts(buf, "rsn_pairwise=") ||
388 str_starts(buf, "wpa_key_mgmt=") ||
389 str_starts(buf, "wpa_passphrase="))) {
390 fprintf(nconf, "#WPS# %s", buf);
392 fprintf(nconf, "%s", buf);
398 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
399 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
400 "configuration file: %s", strerror(errno));
407 /* Schedule configuration reload after short period of time to allow
408 * EAP-WSC to be finished.
410 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
413 /* TODO: dualband AP may need to update multiple configuration files */
415 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
421 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
422 struct wps_event_pwd_auth_fail *data)
430 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
431 * if this happens multiple times.
433 hapd->ap_pin_failures++;
434 if (hapd->ap_pin_failures < 4)
437 wpa_msg(hapd, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
438 hapd->wps->ap_setup_locked = 1;
440 wps_registrar_update_ie(hapd->wps->registrar);
442 if (hapd->conf->wps_cred_processing == 1)
445 f = fopen(hapd->iface->config_fname, "a");
447 wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
448 "configuration file");
452 fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
453 fprintf(f, "ap_setup_locked=1\n");
456 /* TODO: dualband AP may need to update multiple configuration files */
458 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
462 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
463 union wps_event_data *data)
465 struct hostapd_data *hapd = ctx;
467 if (event == WPS_EV_PWD_AUTH_FAIL)
468 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
472 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
474 os_free(hapd->wps_beacon_ie);
475 hapd->wps_beacon_ie = NULL;
476 hapd->wps_beacon_ie_len = 0;
477 hostapd_set_wps_beacon_ie(hapd, NULL, 0);
479 os_free(hapd->wps_probe_resp_ie);
480 hapd->wps_probe_resp_ie = NULL;
481 hapd->wps_probe_resp_ie_len = 0;
482 hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
486 int hostapd_init_wps(struct hostapd_data *hapd,
487 struct hostapd_bss_config *conf)
489 struct wps_context *wps;
490 struct wps_registrar_config cfg;
492 if (conf->wps_state == 0) {
493 hostapd_wps_clear_ies(hapd);
497 wps = os_zalloc(sizeof(*wps));
501 wps->cred_cb = hostapd_wps_cred_cb;
502 wps->event_cb = hostapd_wps_event_cb;
505 os_memset(&cfg, 0, sizeof(cfg));
506 wps->wps_state = hapd->conf->wps_state;
507 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
508 if (is_nil_uuid(hapd->conf->uuid)) {
509 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
510 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
511 wps->uuid, UUID_LEN);
513 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
514 wps->ssid_len = hapd->conf->ssid.ssid_len;
515 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
517 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
518 wps->dev.device_name = hapd->conf->device_name ?
519 os_strdup(hapd->conf->device_name) : NULL;
520 wps->dev.manufacturer = hapd->conf->manufacturer ?
521 os_strdup(hapd->conf->manufacturer) : NULL;
522 wps->dev.model_name = hapd->conf->model_name ?
523 os_strdup(hapd->conf->model_name) : NULL;
524 wps->dev.model_number = hapd->conf->model_number ?
525 os_strdup(hapd->conf->model_number) : NULL;
526 wps->dev.serial_number = hapd->conf->serial_number ?
527 os_strdup(hapd->conf->serial_number) : NULL;
528 if (hapd->conf->config_methods) {
529 char *m = hapd->conf->config_methods;
530 if (os_strstr(m, "label"))
531 wps->config_methods |= WPS_CONFIG_LABEL;
532 if (os_strstr(m, "display"))
533 wps->config_methods |= WPS_CONFIG_DISPLAY;
534 if (os_strstr(m, "push_button"))
535 wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
536 if (os_strstr(m, "keypad"))
537 wps->config_methods |= WPS_CONFIG_KEYPAD;
539 if (hapd->conf->device_type) {
542 /* <categ>-<OUI>-<subcateg> */
543 wps->dev.categ = atoi(hapd->conf->device_type);
544 pos = os_strchr(hapd->conf->device_type, '-');
546 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
551 if (hexstr2bin(pos, oui, 4)) {
552 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
556 wps->dev.oui = WPA_GET_BE32(oui);
557 pos = os_strchr(pos, '-');
559 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
564 wps->dev.sub_categ = atoi(pos);
566 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
567 wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
568 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
570 if (conf->wpa & WPA_PROTO_RSN) {
571 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
572 wps->auth_types |= WPS_AUTH_WPA2PSK;
573 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
574 wps->auth_types |= WPS_AUTH_WPA2;
576 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
577 wps->encr_types |= WPS_ENCR_AES;
578 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
579 wps->encr_types |= WPS_ENCR_TKIP;
582 if (conf->wpa & WPA_PROTO_WPA) {
583 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
584 wps->auth_types |= WPS_AUTH_WPAPSK;
585 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
586 wps->auth_types |= WPS_AUTH_WPA;
588 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
589 wps->encr_types |= WPS_ENCR_AES;
590 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
591 wps->encr_types |= WPS_ENCR_TKIP;
594 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
595 wps->encr_types |= WPS_ENCR_NONE;
596 wps->auth_types |= WPS_AUTH_OPEN;
597 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
598 wps->encr_types |= WPS_ENCR_WEP;
599 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
600 wps->auth_types |= WPS_AUTH_OPEN;
601 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
602 wps->auth_types |= WPS_AUTH_SHARED;
603 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
604 wps->auth_types |= WPS_AUTH_OPEN;
605 if (conf->default_wep_key_len)
606 wps->encr_types |= WPS_ENCR_WEP;
608 wps->encr_types |= WPS_ENCR_NONE;
611 if (conf->ssid.wpa_psk_file) {
612 /* Use per-device PSKs */
613 } else if (conf->ssid.wpa_passphrase) {
614 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
615 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
616 } else if (conf->ssid.wpa_psk) {
617 wps->network_key = os_malloc(2 * PMK_LEN + 1);
618 if (wps->network_key == NULL) {
622 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
623 conf->ssid.wpa_psk->psk, PMK_LEN);
624 wps->network_key_len = 2 * PMK_LEN;
625 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
626 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
627 if (wps->network_key == NULL) {
631 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
632 conf->ssid.wep.len[0]);
633 wps->network_key_len = conf->ssid.wep.len[0];
636 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
637 /* Override parameters to enable security by default */
638 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
639 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
642 wps->ap_settings = conf->ap_settings;
643 wps->ap_settings_len = conf->ap_settings_len;
645 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
646 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
647 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
648 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
650 cfg.skip_cred_build = conf->skip_cred_build;
651 cfg.extra_cred = conf->extra_cred;
652 cfg.extra_cred_len = conf->extra_cred_len;
653 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
654 conf->skip_cred_build;
655 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
656 cfg.static_wep_only = 1;
658 wps->registrar = wps_registrar_init(wps, &cfg);
659 if (wps->registrar == NULL) {
660 printf("Failed to initialize WPS Registrar\n");
661 os_free(wps->network_key);
666 #ifdef CONFIG_WPS_UPNP
667 wps->friendly_name = hapd->conf->friendly_name;
668 wps->manufacturer_url = hapd->conf->manufacturer_url;
669 wps->model_description = hapd->conf->model_description;
670 wps->model_url = hapd->conf->model_url;
671 wps->upc = hapd->conf->upc;
673 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
674 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
675 wps_registrar_deinit(wps->registrar);
676 os_free(wps->network_key);
680 #endif /* CONFIG_WPS_UPNP */
682 hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
690 void hostapd_deinit_wps(struct hostapd_data *hapd)
692 if (hapd->wps == NULL)
694 #ifdef CONFIG_WPS_UPNP
695 hostapd_wps_upnp_deinit(hapd);
696 #endif /* CONFIG_WPS_UPNP */
697 wps_registrar_deinit(hapd->wps->registrar);
698 os_free(hapd->wps->network_key);
699 wps_device_data_free(&hapd->wps->dev);
700 wpabuf_free(hapd->wps->dh_pubkey);
701 wpabuf_free(hapd->wps->dh_privkey);
702 wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
703 wpabuf_free(hapd->wps->oob_conf.dev_password);
704 wps_free_pending_msgs(hapd->wps->upnp_msgs);
707 hostapd_wps_clear_ies(hapd);
711 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
712 const char *pin, int timeout)
717 if (hapd->wps == NULL)
719 if (os_strcmp(uuid, "any") == 0)
721 else if (uuid_str2bin(uuid, u))
723 return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
724 (const u8 *) pin, os_strlen(pin),
729 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
731 if (hapd->wps == NULL)
733 return wps_registrar_button_pushed(hapd->wps->registrar);
737 #ifdef CONFIG_WPS_OOB
738 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
739 char *path, char *method, char *name)
741 struct wps_context *wps = hapd->wps;
742 struct oob_device_data *oob_dev;
744 oob_dev = wps_get_oob_device(device_type);
747 oob_dev->device_path = path;
748 oob_dev->device_name = name;
749 wps->oob_conf.oob_method = wps_get_oob_method(method);
751 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
753 * Use pre-configured DH keys in order to be able to write the
754 * key hash into the OOB file.
756 wpabuf_free(wps->dh_pubkey);
757 wpabuf_free(wps->dh_privkey);
758 wps->dh_privkey = NULL;
759 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
761 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
762 if (wps->dh_pubkey == NULL) {
763 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
764 "Diffie-Hellman handshake");
769 if (wps_process_oob(wps, oob_dev, 1) < 0)
772 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
773 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
774 hostapd_wps_add_pin(hapd, "any",
775 wpabuf_head(wps->oob_conf.dev_password), 0) <
782 wpabuf_free(wps->dh_pubkey);
783 wps->dh_pubkey = NULL;
784 wpabuf_free(wps->dh_privkey);
785 wps->dh_privkey = NULL;
788 #endif /* CONFIG_WPS_OOB */
791 static void hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
792 const u8 *ie, size_t ie_len)
794 struct hostapd_data *hapd = ctx;
795 struct wpabuf *wps_ie;
796 const u8 *end, *pos, *wps;
798 if (hapd->wps == NULL)
805 while (pos + 1 < end) {
806 if (pos + 2 + pos[1] > end)
808 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
809 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
817 return; /* No WPS IE in Probe Request */
819 wps_ie = wpabuf_alloc(ie_len);
823 /* There may be multiple WPS IEs in the message, so need to concatenate
824 * their WPS Data fields */
825 while (pos + 1 < end) {
826 if (pos + 2 + pos[1] > end)
828 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
829 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
830 wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
834 if (wpabuf_len(wps_ie) > 0) {
835 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
836 #ifdef CONFIG_WPS_UPNP
837 /* FIX: what exactly should be included in the WLANEvent?
838 * WPS attributes? Full ProbeReq frame? */
839 upnp_wps_device_send_wlan_event(hapd->wps_upnp, addr,
840 UPNP_WPS_WLANEVENT_TYPE_PROBE,
842 #endif /* CONFIG_WPS_UPNP */
849 #ifdef CONFIG_WPS_UPNP
851 static struct wpabuf *
852 hostapd_rx_req_get_device_info(void *priv, struct upnp_wps_peer *peer)
854 struct hostapd_data *hapd = priv;
855 struct wps_config cfg;
856 struct wps_data *wps;
857 enum wsc_op_code op_code;
861 * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
862 * registration over UPnP with the AP acting as an Enrollee. It should
863 * be noted that this is frequently used just to get the device data,
864 * i.e., there may not be any intent to actually complete the
869 wps_deinit(peer->wps);
871 os_memset(&cfg, 0, sizeof(cfg));
873 cfg.pin = (u8 *) hapd->conf->ap_pin;
874 cfg.pin_len = os_strlen(hapd->conf->ap_pin);
875 wps = wps_init(&cfg);
879 m1 = wps_get_msg(wps, &op_code);
891 static struct wpabuf *
892 hostapd_rx_req_put_message(void *priv, struct upnp_wps_peer *peer,
893 const struct wpabuf *msg)
895 enum wps_process_res res;
896 enum wsc_op_code op_code;
898 /* PutMessage: msg = InMessage, return OutMessage */
899 res = wps_process_msg(peer->wps, WSC_UPnP, msg);
900 if (res == WPS_FAILURE)
902 return wps_get_msg(peer->wps, &op_code);
906 static struct wpabuf *
907 hostapd_rx_req_get_ap_settings(void *priv, const struct wpabuf *msg)
909 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
914 static int hostapd_rx_req_set_ap_settings(void *priv, const struct wpabuf *msg)
916 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
921 static int hostapd_rx_req_del_ap_settings(void *priv, const struct wpabuf *msg)
923 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
928 static struct wpabuf *
929 hostapd_rx_req_get_sta_settings(void *priv, const struct wpabuf *msg)
931 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
936 static int hostapd_rx_req_set_sta_settings(void *priv,
937 const struct wpabuf *msg)
939 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
944 static int hostapd_rx_req_del_sta_settings(void *priv,
945 const struct wpabuf *msg)
947 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
952 static int hostapd_rx_req_put_wlan_response(
953 void *priv, enum upnp_wps_wlanevent_type ev_type,
954 const u8 *mac_addr, const struct wpabuf *msg,
955 enum wps_msg_type msg_type)
957 struct hostapd_data *hapd = priv;
958 struct sta_info *sta;
959 struct upnp_pending_message *p;
961 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
962 MACSTR, ev_type, MAC2STR(mac_addr));
963 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
964 wpabuf_head(msg), wpabuf_len(msg));
965 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
966 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
967 "PutWLANResponse WLANEventType %d", ev_type);
972 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
973 * server implementation for delivery to the peer.
976 sta = ap_get_sta(hapd, mac_addr);
979 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
980 * Pick STA that is in an ongoing WPS registration without
981 * checking the MAC address.
983 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
984 "on NewWLANEventMAC; try wildcard match");
985 for (sta = hapd->sta_list; sta; sta = sta->next) {
986 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
992 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
996 p = os_zalloc(sizeof(*p));
999 os_memcpy(p->addr, sta->addr, ETH_ALEN);
1000 p->msg = wpabuf_dup(msg);
1002 p->next = hapd->wps->upnp_msgs;
1003 hapd->wps->upnp_msgs = p;
1005 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1009 static int hostapd_rx_req_set_selected_registrar(void *priv,
1010 const struct wpabuf *msg)
1012 struct hostapd_data *hapd = priv;
1013 return wps_registrar_set_selected_registrar(hapd->wps->registrar, msg);
1017 static int hostapd_rx_req_reboot_ap(void *priv, const struct wpabuf *msg)
1019 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1024 static int hostapd_rx_req_reset_ap(void *priv, const struct wpabuf *msg)
1026 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1031 static int hostapd_rx_req_reboot_sta(void *priv, const struct wpabuf *msg)
1033 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1038 static int hostapd_rx_req_reset_sta(void *priv, const struct wpabuf *msg)
1040 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1045 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1046 struct wps_context *wps)
1048 struct upnp_wps_device_ctx *ctx;
1050 if (!hapd->conf->upnp_iface)
1052 ctx = os_zalloc(sizeof(*ctx));
1056 ctx->rx_req_get_device_info = hostapd_rx_req_get_device_info;
1057 ctx->rx_req_put_message = hostapd_rx_req_put_message;
1058 ctx->rx_req_get_ap_settings = hostapd_rx_req_get_ap_settings;
1059 ctx->rx_req_set_ap_settings = hostapd_rx_req_set_ap_settings;
1060 ctx->rx_req_del_ap_settings = hostapd_rx_req_del_ap_settings;
1061 ctx->rx_req_get_sta_settings = hostapd_rx_req_get_sta_settings;
1062 ctx->rx_req_set_sta_settings = hostapd_rx_req_set_sta_settings;
1063 ctx->rx_req_del_sta_settings = hostapd_rx_req_del_sta_settings;
1064 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1065 ctx->rx_req_set_selected_registrar =
1066 hostapd_rx_req_set_selected_registrar;
1067 ctx->rx_req_reboot_ap = hostapd_rx_req_reboot_ap;
1068 ctx->rx_req_reset_ap = hostapd_rx_req_reset_ap;
1069 ctx->rx_req_reboot_sta = hostapd_rx_req_reboot_sta;
1070 ctx->rx_req_reset_sta = hostapd_rx_req_reset_sta;
1072 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
1073 if (hapd->wps_upnp == NULL) {
1077 wps->wps_upnp = hapd->wps_upnp;
1079 if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
1080 upnp_wps_device_deinit(hapd->wps_upnp);
1081 hapd->wps_upnp = NULL;
1089 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1091 upnp_wps_device_deinit(hapd->wps_upnp);
1094 #endif /* CONFIG_WPS_UPNP */