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"
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 */
39 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
42 struct hostapd_data *hapd = ctx;
43 struct hostapd_wpa_psk *p;
44 struct hostapd_ssid *ssid = &hapd->conf->ssid;
46 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
47 MACSTR, MAC2STR(mac_addr));
48 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
50 if (psk_len != PMK_LEN) {
51 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
52 (unsigned long) psk_len);
56 /* Add the new PSK to runtime PSK list */
57 p = os_zalloc(sizeof(*p));
60 os_memcpy(p->addr, mac_addr, ETH_ALEN);
61 os_memcpy(p->psk, psk, PMK_LEN);
63 p->next = ssid->wpa_psk;
66 if (ssid->wpa_psk_file) {
68 char hex[PMK_LEN * 2 + 1];
69 /* Add the new PSK to PSK list file */
70 f = fopen(ssid->wpa_psk_file, "a");
72 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
73 "'%s'", ssid->wpa_psk_file);
77 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
78 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
86 static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
87 size_t beacon_ie_len, const u8 *probe_resp_ie,
88 size_t probe_resp_ie_len)
90 struct hostapd_data *hapd = ctx;
92 os_free(hapd->wps_beacon_ie);
93 if (beacon_ie_len == 0) {
94 hapd->wps_beacon_ie = NULL;
95 hapd->wps_beacon_ie_len = 0;
97 hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
98 if (hapd->wps_beacon_ie == NULL) {
99 hapd->wps_beacon_ie_len = 0;
102 os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
103 hapd->wps_beacon_ie_len = beacon_ie_len;
105 hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
106 hapd->wps_beacon_ie_len);
108 os_free(hapd->wps_probe_resp_ie);
109 if (probe_resp_ie_len == 0) {
110 hapd->wps_probe_resp_ie = NULL;
111 hapd->wps_probe_resp_ie_len = 0;
113 hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
114 if (hapd->wps_probe_resp_ie == NULL) {
115 hapd->wps_probe_resp_ie_len = 0;
118 os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
120 hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
122 hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
123 hapd->wps_probe_resp_ie_len);
129 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
130 const struct wps_device_data *dev)
132 struct hostapd_data *hapd = ctx;
133 char uuid[40], txt[400];
135 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
137 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
138 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
139 "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
140 uuid, MAC2STR(dev->mac_addr), dev->device_name,
141 dev->manufacturer, dev->model_name,
142 dev->model_number, dev->serial_number,
143 dev->categ, dev->oui, dev->sub_categ);
144 if (len > 0 && len < (int) sizeof(txt))
145 wpa_msg(hapd, MSG_INFO, "%s", txt);
147 if (hapd->conf->wps_pin_requests) {
150 f = fopen(hapd->conf->wps_pin_requests, "a");
154 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
156 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
157 dev->manufacturer, dev->model_name, dev->model_number,
159 dev->categ, dev->oui, dev->sub_categ);
165 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
168 struct hostapd_data *hapd = ctx;
170 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
172 wpa_msg(hapd, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
173 MAC2STR(mac_addr), uuid);
177 static int str_starts(const char *str, const char *start)
179 return os_strncmp(str, start, os_strlen(start)) == 0;
183 static void wps_reload_config(void *eloop_data, void *user_ctx)
185 struct hostapd_iface *iface = eloop_data;
187 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
188 if (hostapd_reload_config(iface) < 0) {
189 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
195 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
197 struct hostapd_data *hapd = ctx;
205 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
206 cred->cred_attr, cred->cred_attr_len);
208 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
209 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
210 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
212 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
213 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
214 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
215 cred->key, cred->key_len);
216 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
217 MAC2STR(cred->mac_addr));
219 if ((hapd->conf->wps_cred_processing == 1 ||
220 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
221 size_t blen = cred->cred_attr_len * 2 + 1;
222 char *_buf = os_malloc(blen);
224 wpa_snprintf_hex(_buf, blen,
225 cred->cred_attr, cred->cred_attr_len);
226 wpa_msg(hapd, MSG_INFO, "%s%s",
227 WPS_EVENT_NEW_AP_SETTINGS, _buf);
231 wpa_msg(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
233 if (hapd->conf->wps_cred_processing == 1)
236 os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
237 hapd->wps->ssid_len = cred->ssid_len;
238 hapd->wps->encr_types = cred->encr_type;
239 hapd->wps->auth_types = cred->auth_type;
240 if (cred->key_len == 0) {
241 os_free(hapd->wps->network_key);
242 hapd->wps->network_key = NULL;
243 hapd->wps->network_key_len = 0;
245 if (hapd->wps->network_key == NULL ||
246 hapd->wps->network_key_len < cred->key_len) {
247 hapd->wps->network_key_len = 0;
248 os_free(hapd->wps->network_key);
249 hapd->wps->network_key = os_malloc(cred->key_len);
250 if (hapd->wps->network_key == NULL)
253 hapd->wps->network_key_len = cred->key_len;
254 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
256 hapd->wps->wps_state = WPS_STATE_CONFIGURED;
258 len = os_strlen(hapd->iface->config_fname) + 5;
259 tmp_fname = os_malloc(len);
260 if (tmp_fname == NULL)
262 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
264 oconf = fopen(hapd->iface->config_fname, "r");
266 wpa_printf(MSG_WARNING, "WPS: Could not open current "
267 "configuration file");
272 nconf = fopen(tmp_fname, "w");
274 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
275 "configuration file");
281 fprintf(nconf, "# WPS configuration - START\n");
283 fprintf(nconf, "wps_state=2\n");
285 fprintf(nconf, "ssid=");
286 for (i = 0; i < cred->ssid_len; i++)
287 fputc(cred->ssid[i], nconf);
288 fprintf(nconf, "\n");
290 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
291 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
293 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
295 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
302 fprintf(nconf, "wpa=%d\n", wpa);
304 fprintf(nconf, "wpa_key_mgmt=");
306 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
307 fprintf(nconf, "WPA-EAP");
310 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
311 fprintf(nconf, "%sWPA-PSK", prefix);
312 fprintf(nconf, "\n");
314 fprintf(nconf, "wpa_pairwise=");
316 if (cred->encr_type & WPS_ENCR_AES) {
317 fprintf(nconf, "CCMP");
320 if (cred->encr_type & WPS_ENCR_TKIP) {
321 fprintf(nconf, "%sTKIP", prefix);
323 fprintf(nconf, "\n");
325 if (cred->key_len >= 8 && cred->key_len < 64) {
326 fprintf(nconf, "wpa_passphrase=");
327 for (i = 0; i < cred->key_len; i++)
328 fputc(cred->key[i], nconf);
329 fprintf(nconf, "\n");
330 } else if (cred->key_len == 64) {
331 fprintf(nconf, "wpa_psk=");
332 for (i = 0; i < cred->key_len; i++)
333 fputc(cred->key[i], nconf);
334 fprintf(nconf, "\n");
336 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
338 (unsigned long) cred->key_len);
341 fprintf(nconf, "auth_algs=1\n");
343 if ((cred->auth_type & WPS_AUTH_OPEN) &&
344 (cred->auth_type & WPS_AUTH_SHARED))
345 fprintf(nconf, "auth_algs=3\n");
346 else if (cred->auth_type & WPS_AUTH_SHARED)
347 fprintf(nconf, "auth_algs=2\n");
349 fprintf(nconf, "auth_algs=1\n");
351 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
352 int key_idx = cred->key_idx;
355 fprintf(nconf, "wep_default_key=%d\n", key_idx);
356 fprintf(nconf, "wep_key%d=", key_idx);
357 if (cred->key_len == 10 || cred->key_len == 26) {
358 /* WEP key as a hex string */
359 for (i = 0; i < cred->key_len; i++)
360 fputc(cred->key[i], nconf);
362 /* Raw WEP key; convert to hex */
363 for (i = 0; i < cred->key_len; i++)
364 fprintf(nconf, "%02x", cred->key[i]);
366 fprintf(nconf, "\n");
370 fprintf(nconf, "# WPS configuration - END\n");
373 while (fgets(buf, sizeof(buf), oconf)) {
374 if (os_strncmp(buf, "bss=", 4) == 0)
377 (str_starts(buf, "ssid=") ||
378 str_starts(buf, "auth_algs=") ||
379 str_starts(buf, "wps_state=") ||
380 str_starts(buf, "wpa=") ||
381 str_starts(buf, "wpa_psk=") ||
382 str_starts(buf, "wpa_pairwise=") ||
383 str_starts(buf, "rsn_pairwise=") ||
384 str_starts(buf, "wpa_key_mgmt=") ||
385 str_starts(buf, "wpa_passphrase="))) {
386 fprintf(nconf, "#WPS# %s", buf);
388 fprintf(nconf, "%s", buf);
394 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
395 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
396 "configuration file: %s", strerror(errno));
403 /* Schedule configuration reload after short period of time to allow
404 * EAP-WSC to be finished.
406 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
409 /* TODO: dualband AP may need to update multiple configuration files */
411 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
417 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
418 struct wps_event_pwd_auth_fail *data)
426 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
427 * if this happens multiple times.
429 hapd->ap_pin_failures++;
430 if (hapd->ap_pin_failures < 4)
433 wpa_msg(hapd, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
434 hapd->wps->ap_setup_locked = 1;
436 wps_registrar_update_ie(hapd->wps->registrar);
438 if (hapd->conf->wps_cred_processing == 1)
441 f = fopen(hapd->iface->config_fname, "a");
443 wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
444 "configuration file");
448 fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
449 fprintf(f, "ap_setup_locked=1\n");
452 /* TODO: dualband AP may need to update multiple configuration files */
454 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
458 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
459 union wps_event_data *data)
461 struct hostapd_data *hapd = ctx;
463 if (event == WPS_EV_PWD_AUTH_FAIL)
464 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
468 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
470 os_free(hapd->wps_beacon_ie);
471 hapd->wps_beacon_ie = NULL;
472 hapd->wps_beacon_ie_len = 0;
473 hostapd_set_wps_beacon_ie(hapd, NULL, 0);
475 os_free(hapd->wps_probe_resp_ie);
476 hapd->wps_probe_resp_ie = NULL;
477 hapd->wps_probe_resp_ie_len = 0;
478 hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
482 int hostapd_init_wps(struct hostapd_data *hapd,
483 struct hostapd_bss_config *conf)
485 struct wps_context *wps;
486 struct wps_registrar_config cfg;
488 if (conf->wps_state == 0) {
489 hostapd_wps_clear_ies(hapd);
493 wps = os_zalloc(sizeof(*wps));
497 wps->cred_cb = hostapd_wps_cred_cb;
498 wps->event_cb = hostapd_wps_event_cb;
501 os_memset(&cfg, 0, sizeof(cfg));
502 wps->wps_state = hapd->conf->wps_state;
503 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
504 if (is_nil_uuid(hapd->conf->uuid)) {
505 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
506 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
507 wps->uuid, UUID_LEN);
509 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
510 wps->ssid_len = hapd->conf->ssid.ssid_len;
511 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
513 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
514 wps->dev.device_name = hapd->conf->device_name ?
515 os_strdup(hapd->conf->device_name) : NULL;
516 wps->dev.manufacturer = hapd->conf->manufacturer ?
517 os_strdup(hapd->conf->manufacturer) : NULL;
518 wps->dev.model_name = hapd->conf->model_name ?
519 os_strdup(hapd->conf->model_name) : NULL;
520 wps->dev.model_number = hapd->conf->model_number ?
521 os_strdup(hapd->conf->model_number) : NULL;
522 wps->dev.serial_number = hapd->conf->serial_number ?
523 os_strdup(hapd->conf->serial_number) : NULL;
524 if (hapd->conf->config_methods) {
525 char *m = hapd->conf->config_methods;
526 if (os_strstr(m, "label"))
527 wps->config_methods |= WPS_CONFIG_LABEL;
528 if (os_strstr(m, "display"))
529 wps->config_methods |= WPS_CONFIG_DISPLAY;
530 if (os_strstr(m, "push_button"))
531 wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
532 if (os_strstr(m, "keypad"))
533 wps->config_methods |= WPS_CONFIG_KEYPAD;
535 if (hapd->conf->device_type) {
538 /* <categ>-<OUI>-<subcateg> */
539 wps->dev.categ = atoi(hapd->conf->device_type);
540 pos = os_strchr(hapd->conf->device_type, '-');
542 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
547 if (hexstr2bin(pos, oui, 4)) {
548 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
552 wps->dev.oui = WPA_GET_BE32(oui);
553 pos = os_strchr(pos, '-');
555 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
560 wps->dev.sub_categ = atoi(pos);
562 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
563 wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
564 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
566 if (conf->wpa & WPA_PROTO_RSN) {
567 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
568 wps->auth_types |= WPS_AUTH_WPA2PSK;
569 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
570 wps->auth_types |= WPS_AUTH_WPA2;
572 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
573 wps->encr_types |= WPS_ENCR_AES;
574 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
575 wps->encr_types |= WPS_ENCR_TKIP;
578 if (conf->wpa & WPA_PROTO_WPA) {
579 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
580 wps->auth_types |= WPS_AUTH_WPAPSK;
581 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
582 wps->auth_types |= WPS_AUTH_WPA;
584 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
585 wps->encr_types |= WPS_ENCR_AES;
586 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
587 wps->encr_types |= WPS_ENCR_TKIP;
590 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
591 wps->encr_types |= WPS_ENCR_NONE;
592 wps->auth_types |= WPS_AUTH_OPEN;
593 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
594 wps->encr_types |= WPS_ENCR_WEP;
595 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
596 wps->auth_types |= WPS_AUTH_OPEN;
597 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
598 wps->auth_types |= WPS_AUTH_SHARED;
599 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
600 wps->auth_types |= WPS_AUTH_OPEN;
601 if (conf->default_wep_key_len)
602 wps->encr_types |= WPS_ENCR_WEP;
604 wps->encr_types |= WPS_ENCR_NONE;
607 if (conf->ssid.wpa_psk_file) {
608 /* Use per-device PSKs */
609 } else if (conf->ssid.wpa_passphrase) {
610 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
611 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
612 } else if (conf->ssid.wpa_psk) {
613 wps->network_key = os_malloc(2 * PMK_LEN + 1);
614 if (wps->network_key == NULL) {
618 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
619 conf->ssid.wpa_psk->psk, PMK_LEN);
620 wps->network_key_len = 2 * PMK_LEN;
621 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
622 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
623 if (wps->network_key == NULL) {
627 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
628 conf->ssid.wep.len[0]);
629 wps->network_key_len = conf->ssid.wep.len[0];
632 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
633 /* Override parameters to enable security by default */
634 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
635 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
638 wps->ap_settings = conf->ap_settings;
639 wps->ap_settings_len = conf->ap_settings_len;
641 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
642 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
643 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
644 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
646 cfg.skip_cred_build = conf->skip_cred_build;
647 cfg.extra_cred = conf->extra_cred;
648 cfg.extra_cred_len = conf->extra_cred_len;
649 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
650 conf->skip_cred_build;
651 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
652 cfg.static_wep_only = 1;
654 wps->registrar = wps_registrar_init(wps, &cfg);
655 if (wps->registrar == NULL) {
656 printf("Failed to initialize WPS Registrar\n");
657 os_free(wps->network_key);
662 #ifdef CONFIG_WPS_UPNP
663 wps->friendly_name = hapd->conf->friendly_name;
664 wps->manufacturer_url = hapd->conf->manufacturer_url;
665 wps->model_description = hapd->conf->model_description;
666 wps->model_url = hapd->conf->model_url;
667 wps->upc = hapd->conf->upc;
669 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
670 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
671 wps_registrar_deinit(wps->registrar);
672 os_free(wps->network_key);
676 #endif /* CONFIG_WPS_UPNP */
684 void hostapd_deinit_wps(struct hostapd_data *hapd)
686 if (hapd->wps == NULL)
688 #ifdef CONFIG_WPS_UPNP
689 hostapd_wps_upnp_deinit(hapd);
690 #endif /* CONFIG_WPS_UPNP */
691 wps_registrar_deinit(hapd->wps->registrar);
692 os_free(hapd->wps->network_key);
693 wps_device_data_free(&hapd->wps->dev);
694 wps_free_pending_msgs(hapd->wps->upnp_msgs);
697 hostapd_wps_clear_ies(hapd);
701 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
702 const char *pin, int timeout)
707 if (hapd->wps == NULL)
709 if (os_strcmp(uuid, "any") == 0)
711 else if (uuid_str2bin(uuid, u))
713 return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
714 (const u8 *) pin, os_strlen(pin),
719 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
721 if (hapd->wps == NULL)
723 return wps_registrar_button_pushed(hapd->wps->registrar);
727 void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
728 const u8 *ie, size_t ie_len)
730 struct wpabuf *wps_ie;
731 const u8 *end, *pos, *wps;
733 if (hapd->wps == NULL)
740 while (pos + 1 < end) {
741 if (pos + 2 + pos[1] > end)
743 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
744 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
752 return; /* No WPS IE in Probe Request */
754 wps_ie = wpabuf_alloc(ie_len);
758 /* There may be multiple WPS IEs in the message, so need to concatenate
759 * their WPS Data fields */
760 while (pos + 1 < end) {
761 if (pos + 2 + pos[1] > end)
763 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
764 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
765 wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
769 if (wpabuf_len(wps_ie) > 0) {
770 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
771 #ifdef CONFIG_WPS_UPNP
772 /* FIX: what exactly should be included in the WLANEvent?
773 * WPS attributes? Full ProbeReq frame? */
774 upnp_wps_device_send_wlan_event(hapd->wps_upnp, addr,
775 UPNP_WPS_WLANEVENT_TYPE_PROBE,
777 #endif /* CONFIG_WPS_UPNP */
784 #ifdef CONFIG_WPS_UPNP
786 static struct wpabuf *
787 hostapd_rx_req_get_device_info(void *priv, struct upnp_wps_peer *peer)
789 struct hostapd_data *hapd = priv;
790 struct wps_config cfg;
791 struct wps_data *wps;
792 enum wsc_op_code op_code;
796 * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
797 * registration over UPnP with the AP acting as an Enrollee. It should
798 * be noted that this is frequently used just to get the device data,
799 * i.e., there may not be any intent to actually complete the
804 wps_deinit(peer->wps);
806 os_memset(&cfg, 0, sizeof(cfg));
808 cfg.pin = (u8 *) hapd->conf->ap_pin;
809 cfg.pin_len = os_strlen(hapd->conf->ap_pin);
810 wps = wps_init(&cfg);
814 m1 = wps_get_msg(wps, &op_code);
826 static struct wpabuf *
827 hostapd_rx_req_put_message(void *priv, struct upnp_wps_peer *peer,
828 const struct wpabuf *msg)
830 enum wps_process_res res;
831 enum wsc_op_code op_code;
833 /* PutMessage: msg = InMessage, return OutMessage */
834 res = wps_process_msg(peer->wps, WSC_UPnP, msg);
835 if (res == WPS_FAILURE)
837 return wps_get_msg(peer->wps, &op_code);
841 static struct wpabuf *
842 hostapd_rx_req_get_ap_settings(void *priv, const struct wpabuf *msg)
844 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
849 static int hostapd_rx_req_set_ap_settings(void *priv, const struct wpabuf *msg)
851 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
856 static int hostapd_rx_req_del_ap_settings(void *priv, const struct wpabuf *msg)
858 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
863 static struct wpabuf *
864 hostapd_rx_req_get_sta_settings(void *priv, const struct wpabuf *msg)
866 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
871 static int hostapd_rx_req_set_sta_settings(void *priv,
872 const struct wpabuf *msg)
874 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
879 static int hostapd_rx_req_del_sta_settings(void *priv,
880 const struct wpabuf *msg)
882 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
887 static int hostapd_rx_req_put_wlan_response(
888 void *priv, enum upnp_wps_wlanevent_type ev_type,
889 const u8 *mac_addr, const struct wpabuf *msg,
890 enum wps_msg_type msg_type)
892 struct hostapd_data *hapd = priv;
893 struct sta_info *sta;
894 struct upnp_pending_message *p;
896 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
897 MACSTR, ev_type, MAC2STR(mac_addr));
898 wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
899 wpabuf_head(msg), wpabuf_len(msg));
900 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
901 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
902 "PutWLANResponse WLANEventType %d", ev_type);
907 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
908 * server implementation for delivery to the peer.
911 sta = ap_get_sta(hapd, mac_addr);
914 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
915 * Pick STA that is in an ongoing WPS registration without
916 * checking the MAC address.
918 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
919 "on NewWLANEventMAC; try wildcard match");
920 for (sta = hapd->sta_list; sta; sta = sta->next) {
921 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
927 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
931 p = os_zalloc(sizeof(*p));
934 os_memcpy(p->addr, sta->addr, ETH_ALEN);
935 p->msg = wpabuf_dup(msg);
937 p->next = hapd->wps->upnp_msgs;
938 hapd->wps->upnp_msgs = p;
940 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
944 static int hostapd_rx_req_set_selected_registrar(void *priv,
945 const struct wpabuf *msg)
947 struct hostapd_data *hapd = priv;
948 return wps_registrar_set_selected_registrar(hapd->wps->registrar, msg);
952 static int hostapd_rx_req_reboot_ap(void *priv, const struct wpabuf *msg)
954 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
959 static int hostapd_rx_req_reset_ap(void *priv, const struct wpabuf *msg)
961 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
966 static int hostapd_rx_req_reboot_sta(void *priv, const struct wpabuf *msg)
968 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
973 static int hostapd_rx_req_reset_sta(void *priv, const struct wpabuf *msg)
975 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
980 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
981 struct wps_context *wps)
983 struct upnp_wps_device_ctx *ctx;
985 if (!hapd->conf->upnp_iface)
987 ctx = os_zalloc(sizeof(*ctx));
991 ctx->rx_req_get_device_info = hostapd_rx_req_get_device_info;
992 ctx->rx_req_put_message = hostapd_rx_req_put_message;
993 ctx->rx_req_get_ap_settings = hostapd_rx_req_get_ap_settings;
994 ctx->rx_req_set_ap_settings = hostapd_rx_req_set_ap_settings;
995 ctx->rx_req_del_ap_settings = hostapd_rx_req_del_ap_settings;
996 ctx->rx_req_get_sta_settings = hostapd_rx_req_get_sta_settings;
997 ctx->rx_req_set_sta_settings = hostapd_rx_req_set_sta_settings;
998 ctx->rx_req_del_sta_settings = hostapd_rx_req_del_sta_settings;
999 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1000 ctx->rx_req_set_selected_registrar =
1001 hostapd_rx_req_set_selected_registrar;
1002 ctx->rx_req_reboot_ap = hostapd_rx_req_reboot_ap;
1003 ctx->rx_req_reset_ap = hostapd_rx_req_reset_ap;
1004 ctx->rx_req_reboot_sta = hostapd_rx_req_reboot_sta;
1005 ctx->rx_req_reset_sta = hostapd_rx_req_reset_sta;
1007 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
1008 if (hapd->wps_upnp == NULL) {
1012 wps->wps_upnp = hapd->wps_upnp;
1014 if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
1015 upnp_wps_device_deinit(hapd->wps_upnp);
1016 hapd->wps_upnp = NULL;
1024 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1026 upnp_wps_device_deinit(hapd->wps_upnp);
1029 #endif /* CONFIG_WPS_UPNP */