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 */
40 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
43 struct hostapd_data *hapd = ctx;
44 struct hostapd_wpa_psk *p;
45 struct hostapd_ssid *ssid = &hapd->conf->ssid;
47 wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
48 MACSTR, MAC2STR(mac_addr));
49 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
51 if (psk_len != PMK_LEN) {
52 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
53 (unsigned long) psk_len);
57 /* Add the new PSK to runtime PSK list */
58 p = os_zalloc(sizeof(*p));
61 os_memcpy(p->addr, mac_addr, ETH_ALEN);
62 os_memcpy(p->psk, psk, PMK_LEN);
64 p->next = ssid->wpa_psk;
67 if (ssid->wpa_psk_file) {
69 char hex[PMK_LEN * 2 + 1];
70 /* Add the new PSK to PSK list file */
71 f = fopen(ssid->wpa_psk_file, "a");
73 wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
74 "'%s'", ssid->wpa_psk_file);
78 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
79 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
87 static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
88 size_t beacon_ie_len, const u8 *probe_resp_ie,
89 size_t probe_resp_ie_len)
91 struct hostapd_data *hapd = ctx;
93 os_free(hapd->wps_beacon_ie);
94 if (beacon_ie_len == 0) {
95 hapd->wps_beacon_ie = NULL;
96 hapd->wps_beacon_ie_len = 0;
98 hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
99 if (hapd->wps_beacon_ie == NULL) {
100 hapd->wps_beacon_ie_len = 0;
103 os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
104 hapd->wps_beacon_ie_len = beacon_ie_len;
106 hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
107 hapd->wps_beacon_ie_len);
109 os_free(hapd->wps_probe_resp_ie);
110 if (probe_resp_ie_len == 0) {
111 hapd->wps_probe_resp_ie = NULL;
112 hapd->wps_probe_resp_ie_len = 0;
114 hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
115 if (hapd->wps_probe_resp_ie == NULL) {
116 hapd->wps_probe_resp_ie_len = 0;
119 os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
121 hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
123 hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
124 hapd->wps_probe_resp_ie_len);
130 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
131 const struct wps_device_data *dev)
133 struct hostapd_data *hapd = ctx;
134 char uuid[40], txt[400];
136 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
138 wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
139 len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
140 "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
141 uuid, MAC2STR(dev->mac_addr), dev->device_name,
142 dev->manufacturer, dev->model_name,
143 dev->model_number, dev->serial_number,
144 dev->categ, dev->oui, dev->sub_categ);
145 if (len > 0 && len < (int) sizeof(txt))
146 wpa_msg(hapd, MSG_INFO, "%s", txt);
148 if (hapd->conf->wps_pin_requests) {
151 f = fopen(hapd->conf->wps_pin_requests, "a");
155 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
157 t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
158 dev->manufacturer, dev->model_name, dev->model_number,
160 dev->categ, dev->oui, dev->sub_categ);
166 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
169 struct hostapd_data *hapd = ctx;
171 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
173 wpa_msg(hapd, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
174 MAC2STR(mac_addr), uuid);
178 static int str_starts(const char *str, const char *start)
180 return os_strncmp(str, start, os_strlen(start)) == 0;
184 static void wps_reload_config(void *eloop_data, void *user_ctx)
186 struct hostapd_iface *iface = eloop_data;
188 wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
189 if (hostapd_reload_config(iface) < 0) {
190 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
196 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
198 struct hostapd_data *hapd = ctx;
206 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
207 cred->cred_attr, cred->cred_attr_len);
209 wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
210 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
211 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
213 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
214 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
215 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
216 cred->key, cred->key_len);
217 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
218 MAC2STR(cred->mac_addr));
220 if ((hapd->conf->wps_cred_processing == 1 ||
221 hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
222 size_t blen = cred->cred_attr_len * 2 + 1;
223 char *_buf = os_malloc(blen);
225 wpa_snprintf_hex(_buf, blen,
226 cred->cred_attr, cred->cred_attr_len);
227 wpa_msg(hapd, MSG_INFO, "%s%s",
228 WPS_EVENT_NEW_AP_SETTINGS, _buf);
232 wpa_msg(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
234 if (hapd->conf->wps_cred_processing == 1)
237 len = os_strlen(hapd->iface->config_fname) + 5;
238 tmp_fname = os_malloc(len);
239 if (tmp_fname == NULL)
241 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
243 oconf = fopen(hapd->iface->config_fname, "r");
245 wpa_printf(MSG_WARNING, "WPS: Could not open current "
246 "configuration file");
251 nconf = fopen(tmp_fname, "w");
253 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
254 "configuration file");
260 fprintf(nconf, "# WPS configuration - START\n");
262 fprintf(nconf, "wps_state=2\n");
264 fprintf(nconf, "ssid=");
265 for (i = 0; i < cred->ssid_len; i++)
266 fputc(cred->ssid[i], nconf);
267 fprintf(nconf, "\n");
269 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
270 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
272 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
274 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
281 fprintf(nconf, "wpa=%d\n", wpa);
283 fprintf(nconf, "wpa_key_mgmt=");
285 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
286 fprintf(nconf, "WPA-EAP");
289 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
290 fprintf(nconf, "%sWPA-PSK", prefix);
291 fprintf(nconf, "\n");
293 fprintf(nconf, "wpa_pairwise=");
295 if (cred->encr_type & WPS_ENCR_AES) {
296 fprintf(nconf, "CCMP");
299 if (cred->encr_type & WPS_ENCR_TKIP) {
300 fprintf(nconf, "%sTKIP", prefix);
302 fprintf(nconf, "\n");
304 if (cred->key_len >= 8 && cred->key_len < 64) {
305 fprintf(nconf, "wpa_passphrase=");
306 for (i = 0; i < cred->key_len; i++)
307 fputc(cred->key[i], nconf);
308 fprintf(nconf, "\n");
309 } else if (cred->key_len == 64) {
310 fprintf(nconf, "wpa_psk=");
311 for (i = 0; i < cred->key_len; i++)
312 fputc(cred->key[i], nconf);
313 fprintf(nconf, "\n");
315 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
317 (unsigned long) cred->key_len);
320 fprintf(nconf, "auth_algs=1\n");
322 if ((cred->auth_type & WPS_AUTH_OPEN) &&
323 (cred->auth_type & WPS_AUTH_SHARED))
324 fprintf(nconf, "auth_algs=3\n");
325 else if (cred->auth_type & WPS_AUTH_SHARED)
326 fprintf(nconf, "auth_algs=2\n");
328 fprintf(nconf, "auth_algs=1\n");
330 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
331 int key_idx = cred->key_idx;
334 fprintf(nconf, "wep_default_key=%d\n", key_idx);
335 fprintf(nconf, "wep_key%d=", key_idx);
336 if (cred->key_len == 10 || cred->key_len == 26) {
337 /* WEP key as a hex string */
338 for (i = 0; i < cred->key_len; i++)
339 fputc(cred->key[i], nconf);
341 /* Raw WEP key; convert to hex */
342 for (i = 0; i < cred->key_len; i++)
343 fprintf(nconf, "%02x", cred->key[i]);
345 fprintf(nconf, "\n");
349 fprintf(nconf, "# WPS configuration - END\n");
352 while (fgets(buf, sizeof(buf), oconf)) {
353 if (os_strncmp(buf, "bss=", 4) == 0)
356 (str_starts(buf, "ssid=") ||
357 str_starts(buf, "auth_algs=") ||
358 str_starts(buf, "wps_state=") ||
359 str_starts(buf, "wpa=") ||
360 str_starts(buf, "wpa_psk=") ||
361 str_starts(buf, "wpa_pairwise=") ||
362 str_starts(buf, "rsn_pairwise=") ||
363 str_starts(buf, "wpa_key_mgmt=") ||
364 str_starts(buf, "wpa_passphrase="))) {
365 fprintf(nconf, "#WPS# %s", buf);
367 fprintf(nconf, "%s", buf);
373 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
374 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
375 "configuration file: %s", strerror(errno));
382 /* Schedule configuration reload after short period of time to allow
383 * EAP-WSC to be finished.
385 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
388 /* TODO: dualband AP may need to update multiple configuration files */
390 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
396 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
397 struct wps_event_pwd_auth_fail *data)
405 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
406 * if this happens multiple times.
408 hapd->ap_pin_failures++;
409 if (hapd->ap_pin_failures < 4)
412 wpa_msg(hapd, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
413 hapd->wps->ap_setup_locked = 1;
415 wps_registrar_update_ie(hapd->wps->registrar);
417 if (hapd->conf->wps_cred_processing == 1)
420 f = fopen(hapd->iface->config_fname, "a");
422 wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
423 "configuration file");
427 fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
428 fprintf(f, "ap_setup_locked=1\n");
431 /* TODO: dualband AP may need to update multiple configuration files */
433 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
437 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
438 union wps_event_data *data)
440 struct hostapd_data *hapd = ctx;
442 if (event == WPS_EV_PWD_AUTH_FAIL)
443 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
447 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
449 os_free(hapd->wps_beacon_ie);
450 hapd->wps_beacon_ie = NULL;
451 hapd->wps_beacon_ie_len = 0;
452 hostapd_set_wps_beacon_ie(hapd, NULL, 0);
454 os_free(hapd->wps_probe_resp_ie);
455 hapd->wps_probe_resp_ie = NULL;
456 hapd->wps_probe_resp_ie_len = 0;
457 hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
461 int hostapd_init_wps(struct hostapd_data *hapd,
462 struct hostapd_bss_config *conf)
464 struct wps_context *wps;
465 struct wps_registrar_config cfg;
467 if (conf->wps_state == 0) {
468 hostapd_wps_clear_ies(hapd);
472 wps = os_zalloc(sizeof(*wps));
476 wps->cred_cb = hostapd_wps_cred_cb;
477 wps->event_cb = hostapd_wps_event_cb;
480 os_memset(&cfg, 0, sizeof(cfg));
481 wps->wps_state = hapd->conf->wps_state;
482 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
483 if (is_nil_uuid(hapd->conf->uuid)) {
484 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
485 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
486 wps->uuid, UUID_LEN);
488 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
489 wps->ssid_len = hapd->conf->ssid.ssid_len;
490 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
492 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
493 wps->dev.device_name = hapd->conf->device_name ?
494 os_strdup(hapd->conf->device_name) : NULL;
495 wps->dev.manufacturer = hapd->conf->manufacturer ?
496 os_strdup(hapd->conf->manufacturer) : NULL;
497 wps->dev.model_name = hapd->conf->model_name ?
498 os_strdup(hapd->conf->model_name) : NULL;
499 wps->dev.model_number = hapd->conf->model_number ?
500 os_strdup(hapd->conf->model_number) : NULL;
501 wps->dev.serial_number = hapd->conf->serial_number ?
502 os_strdup(hapd->conf->serial_number) : NULL;
503 if (hapd->conf->config_methods) {
504 char *m = hapd->conf->config_methods;
505 if (os_strstr(m, "label"))
506 wps->config_methods |= WPS_CONFIG_LABEL;
507 if (os_strstr(m, "display"))
508 wps->config_methods |= WPS_CONFIG_DISPLAY;
509 if (os_strstr(m, "push_button"))
510 wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
511 if (os_strstr(m, "keypad"))
512 wps->config_methods |= WPS_CONFIG_KEYPAD;
514 if (hapd->conf->device_type) {
517 /* <categ>-<OUI>-<subcateg> */
518 wps->dev.categ = atoi(hapd->conf->device_type);
519 pos = os_strchr(hapd->conf->device_type, '-');
521 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
526 if (hexstr2bin(pos, oui, 4)) {
527 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
531 wps->dev.oui = WPA_GET_BE32(oui);
532 pos = os_strchr(pos, '-');
534 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
539 wps->dev.sub_categ = atoi(pos);
541 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
542 wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
543 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
545 if (conf->wpa & WPA_PROTO_RSN) {
546 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
547 wps->auth_types |= WPS_AUTH_WPA2PSK;
548 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
549 wps->auth_types |= WPS_AUTH_WPA2;
551 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
552 wps->encr_types |= WPS_ENCR_AES;
553 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
554 wps->encr_types |= WPS_ENCR_TKIP;
557 if (conf->wpa & WPA_PROTO_WPA) {
558 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
559 wps->auth_types |= WPS_AUTH_WPAPSK;
560 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
561 wps->auth_types |= WPS_AUTH_WPA;
563 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
564 wps->encr_types |= WPS_ENCR_AES;
565 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
566 wps->encr_types |= WPS_ENCR_TKIP;
569 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
570 wps->encr_types |= WPS_ENCR_NONE;
571 wps->auth_types |= WPS_AUTH_OPEN;
572 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
573 wps->encr_types |= WPS_ENCR_WEP;
574 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
575 wps->auth_types |= WPS_AUTH_OPEN;
576 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
577 wps->auth_types |= WPS_AUTH_SHARED;
578 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
579 wps->auth_types |= WPS_AUTH_OPEN;
580 if (conf->default_wep_key_len)
581 wps->encr_types |= WPS_ENCR_WEP;
583 wps->encr_types |= WPS_ENCR_NONE;
586 if (conf->ssid.wpa_psk_file) {
587 /* Use per-device PSKs */
588 } else if (conf->ssid.wpa_passphrase) {
589 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
590 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
591 } else if (conf->ssid.wpa_psk) {
592 wps->network_key = os_malloc(2 * PMK_LEN + 1);
593 if (wps->network_key == NULL) {
597 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
598 conf->ssid.wpa_psk->psk, PMK_LEN);
599 wps->network_key_len = 2 * PMK_LEN;
600 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
601 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
602 if (wps->network_key == NULL) {
606 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
607 conf->ssid.wep.len[0]);
608 wps->network_key_len = conf->ssid.wep.len[0];
611 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
612 /* Override parameters to enable security by default */
613 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
614 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
617 wps->ap_settings = conf->ap_settings;
618 wps->ap_settings_len = conf->ap_settings_len;
620 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
621 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
622 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
623 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
625 cfg.skip_cred_build = conf->skip_cred_build;
626 cfg.extra_cred = conf->extra_cred;
627 cfg.extra_cred_len = conf->extra_cred_len;
628 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
629 conf->skip_cred_build;
630 if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
631 cfg.static_wep_only = 1;
633 wps->registrar = wps_registrar_init(wps, &cfg);
634 if (wps->registrar == NULL) {
635 printf("Failed to initialize WPS Registrar\n");
636 os_free(wps->network_key);
641 #ifdef CONFIG_WPS_UPNP
642 wps->friendly_name = hapd->conf->friendly_name;
643 wps->manufacturer_url = hapd->conf->manufacturer_url;
644 wps->model_description = hapd->conf->model_description;
645 wps->model_url = hapd->conf->model_url;
646 wps->upc = hapd->conf->upc;
648 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
649 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
650 wps_registrar_deinit(wps->registrar);
651 os_free(wps->network_key);
655 #endif /* CONFIG_WPS_UPNP */
663 void hostapd_deinit_wps(struct hostapd_data *hapd)
665 if (hapd->wps == NULL)
667 #ifdef CONFIG_WPS_UPNP
668 hostapd_wps_upnp_deinit(hapd);
669 #endif /* CONFIG_WPS_UPNP */
670 wps_registrar_deinit(hapd->wps->registrar);
671 os_free(hapd->wps->network_key);
672 wps_device_data_free(&hapd->wps->dev);
673 wpabuf_free(hapd->wps->dh_pubkey);
674 wpabuf_free(hapd->wps->dh_privkey);
675 wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
676 wpabuf_free(hapd->wps->oob_conf.dev_password);
677 wps_free_pending_msgs(hapd->wps->upnp_msgs);
680 hostapd_wps_clear_ies(hapd);
684 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
690 if (hapd->wps == NULL)
692 if (os_strcmp(uuid, "any") == 0)
694 else if (uuid_str2bin(uuid, u))
696 return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
697 (const u8 *) pin, os_strlen(pin));
701 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
703 if (hapd->wps == NULL)
705 return wps_registrar_button_pushed(hapd->wps->registrar);
709 #ifdef CONFIG_WPS_OOB
710 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
711 char *path, char *method, char *name)
713 struct wps_context *wps = hapd->wps;
714 struct oob_device_data *oob_dev;
716 oob_dev = wps_get_oob_device(device_type);
719 oob_dev->device_path = path;
720 oob_dev->device_name = name;
721 wps->oob_conf.oob_method = wps_get_oob_method(method);
723 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
725 * Use pre-configured DH keys in order to be able to write the
726 * key hash into the OOB file.
728 wpabuf_free(wps->dh_pubkey);
729 wpabuf_free(wps->dh_privkey);
730 wps->dh_privkey = NULL;
731 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
733 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
734 if (wps->dh_pubkey == NULL) {
735 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
736 "Diffie-Hellman handshake");
741 if (wps_process_oob(wps, oob_dev, 1) < 0)
744 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
745 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
746 hostapd_wps_add_pin(hapd, "any",
747 wpabuf_head(wps->oob_conf.dev_password)) < 0)
753 wpabuf_free(wps->dh_pubkey);
754 wps->dh_pubkey = NULL;
755 wpabuf_free(wps->dh_privkey);
756 wps->dh_privkey = NULL;
759 #endif /* CONFIG_WPS_OOB */
762 void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
763 const u8 *ie, size_t ie_len)
765 struct wpabuf *wps_ie;
766 const u8 *end, *pos, *wps;
768 if (hapd->wps == NULL)
775 while (pos + 1 < end) {
776 if (pos + 2 + pos[1] > end)
778 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
779 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
787 return; /* No WPS IE in Probe Request */
789 wps_ie = wpabuf_alloc(ie_len);
793 /* There may be multiple WPS IEs in the message, so need to concatenate
794 * their WPS Data fields */
795 while (pos + 1 < end) {
796 if (pos + 2 + pos[1] > end)
798 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
799 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
800 wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
804 if (wpabuf_len(wps_ie) > 0) {
805 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
806 #ifdef CONFIG_WPS_UPNP
807 /* FIX: what exactly should be included in the WLANEvent?
808 * WPS attributes? Full ProbeReq frame? */
809 upnp_wps_device_send_wlan_event(hapd->wps_upnp, addr,
810 UPNP_WPS_WLANEVENT_TYPE_PROBE,
812 #endif /* CONFIG_WPS_UPNP */
819 #ifdef CONFIG_WPS_UPNP
821 static struct wpabuf *
822 hostapd_rx_req_get_device_info(void *priv, struct upnp_wps_peer *peer)
824 struct hostapd_data *hapd = priv;
825 struct wps_config cfg;
826 struct wps_data *wps;
827 enum wsc_op_code op_code;
831 * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
832 * registration over UPnP with the AP acting as an Enrollee. It should
833 * be noted that this is frequently used just to get the device data,
834 * i.e., there may not be any intent to actually complete the
839 wps_deinit(peer->wps);
841 os_memset(&cfg, 0, sizeof(cfg));
843 cfg.pin = (u8 *) hapd->conf->ap_pin;
844 cfg.pin_len = os_strlen(hapd->conf->ap_pin);
845 wps = wps_init(&cfg);
849 m1 = wps_get_msg(wps, &op_code);
861 static struct wpabuf *
862 hostapd_rx_req_put_message(void *priv, struct upnp_wps_peer *peer,
863 const struct wpabuf *msg)
865 enum wps_process_res res;
866 enum wsc_op_code op_code;
868 /* PutMessage: msg = InMessage, return OutMessage */
869 res = wps_process_msg(peer->wps, WSC_UPnP, msg);
870 if (res == WPS_FAILURE)
872 return wps_get_msg(peer->wps, &op_code);
876 static struct wpabuf *
877 hostapd_rx_req_get_ap_settings(void *priv, const struct wpabuf *msg)
879 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
884 static int hostapd_rx_req_set_ap_settings(void *priv, const struct wpabuf *msg)
886 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
891 static int hostapd_rx_req_del_ap_settings(void *priv, const struct wpabuf *msg)
893 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
898 static struct wpabuf *
899 hostapd_rx_req_get_sta_settings(void *priv, const struct wpabuf *msg)
901 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
906 static int hostapd_rx_req_set_sta_settings(void *priv,
907 const struct wpabuf *msg)
909 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
914 static int hostapd_rx_req_del_sta_settings(void *priv,
915 const struct wpabuf *msg)
917 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
922 static int hostapd_rx_req_put_wlan_response(
923 void *priv, enum upnp_wps_wlanevent_type ev_type,
924 const u8 *mac_addr, const struct wpabuf *msg,
925 enum wps_msg_type msg_type)
927 struct hostapd_data *hapd = priv;
928 struct sta_info *sta;
929 struct upnp_pending_message *p;
931 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
932 MACSTR, ev_type, MAC2STR(mac_addr));
933 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
934 wpabuf_head(msg), wpabuf_len(msg));
935 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
936 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
937 "PutWLANResponse WLANEventType %d", ev_type);
942 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
943 * server implementation for delivery to the peer.
946 sta = ap_get_sta(hapd, mac_addr);
949 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
950 * Pick STA that is in an ongoing WPS registration without
951 * checking the MAC address.
953 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
954 "on NewWLANEventMAC; try wildcard match");
955 for (sta = hapd->sta_list; sta; sta = sta->next) {
956 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
962 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
966 p = os_zalloc(sizeof(*p));
969 os_memcpy(p->addr, sta->addr, ETH_ALEN);
970 p->msg = wpabuf_dup(msg);
972 p->next = hapd->wps->upnp_msgs;
973 hapd->wps->upnp_msgs = p;
975 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
979 static int hostapd_rx_req_set_selected_registrar(void *priv,
980 const struct wpabuf *msg)
982 struct hostapd_data *hapd = priv;
983 return wps_registrar_set_selected_registrar(hapd->wps->registrar, msg);
987 static int hostapd_rx_req_reboot_ap(void *priv, const struct wpabuf *msg)
989 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
994 static int hostapd_rx_req_reset_ap(void *priv, const struct wpabuf *msg)
996 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1001 static int hostapd_rx_req_reboot_sta(void *priv, const struct wpabuf *msg)
1003 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1008 static int hostapd_rx_req_reset_sta(void *priv, const struct wpabuf *msg)
1010 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1015 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1016 struct wps_context *wps)
1018 struct upnp_wps_device_ctx *ctx;
1020 if (!hapd->conf->upnp_iface)
1022 ctx = os_zalloc(sizeof(*ctx));
1026 ctx->rx_req_get_device_info = hostapd_rx_req_get_device_info;
1027 ctx->rx_req_put_message = hostapd_rx_req_put_message;
1028 ctx->rx_req_get_ap_settings = hostapd_rx_req_get_ap_settings;
1029 ctx->rx_req_set_ap_settings = hostapd_rx_req_set_ap_settings;
1030 ctx->rx_req_del_ap_settings = hostapd_rx_req_del_ap_settings;
1031 ctx->rx_req_get_sta_settings = hostapd_rx_req_get_sta_settings;
1032 ctx->rx_req_set_sta_settings = hostapd_rx_req_set_sta_settings;
1033 ctx->rx_req_del_sta_settings = hostapd_rx_req_del_sta_settings;
1034 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1035 ctx->rx_req_set_selected_registrar =
1036 hostapd_rx_req_set_selected_registrar;
1037 ctx->rx_req_reboot_ap = hostapd_rx_req_reboot_ap;
1038 ctx->rx_req_reset_ap = hostapd_rx_req_reset_ap;
1039 ctx->rx_req_reboot_sta = hostapd_rx_req_reboot_sta;
1040 ctx->rx_req_reset_sta = hostapd_rx_req_reset_sta;
1042 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
1043 if (hapd->wps_upnp == NULL) {
1047 wps->wps_upnp = hapd->wps_upnp;
1049 if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
1050 upnp_wps_device_deinit(hapd->wps_upnp);
1051 hapd->wps_upnp = NULL;
1059 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1061 upnp_wps_device_deinit(hapd->wps_upnp);
1064 #endif /* CONFIG_WPS_UPNP */