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 len = os_strlen(hapd->iface->config_fname) + 5;
237 tmp_fname = os_malloc(len);
238 if (tmp_fname == NULL)
240 os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
242 oconf = fopen(hapd->iface->config_fname, "r");
244 wpa_printf(MSG_WARNING, "WPS: Could not open current "
245 "configuration file");
250 nconf = fopen(tmp_fname, "w");
252 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
253 "configuration file");
259 fprintf(nconf, "# WPS configuration - START\n");
261 fprintf(nconf, "wps_state=2\n");
263 fprintf(nconf, "ssid=");
264 for (i = 0; i < cred->ssid_len; i++)
265 fputc(cred->ssid[i], nconf);
266 fprintf(nconf, "\n");
268 if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
269 (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
271 else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
273 else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
280 fprintf(nconf, "wpa=%d\n", wpa);
282 fprintf(nconf, "wpa_key_mgmt=");
284 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
285 fprintf(nconf, "WPA-EAP");
288 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
289 fprintf(nconf, "%sWPA-PSK", prefix);
290 fprintf(nconf, "\n");
292 fprintf(nconf, "wpa_pairwise=");
294 if (cred->encr_type & WPS_ENCR_AES) {
295 fprintf(nconf, "CCMP");
298 if (cred->encr_type & WPS_ENCR_TKIP) {
299 fprintf(nconf, "%sTKIP", prefix);
301 fprintf(nconf, "\n");
303 if (cred->key_len >= 8 && cred->key_len < 64) {
304 fprintf(nconf, "wpa_passphrase=");
305 for (i = 0; i < cred->key_len; i++)
306 fputc(cred->key[i], nconf);
307 fprintf(nconf, "\n");
308 } else if (cred->key_len == 64) {
309 fprintf(nconf, "wpa_psk=");
310 for (i = 0; i < cred->key_len; i++)
311 fputc(cred->key[i], nconf);
312 fprintf(nconf, "\n");
314 wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
316 (unsigned long) cred->key_len);
319 fprintf(nconf, "auth_algs=1\n");
321 if ((cred->auth_type & WPS_AUTH_OPEN) &&
322 (cred->auth_type & WPS_AUTH_SHARED))
323 fprintf(nconf, "auth_algs=3\n");
324 else if (cred->auth_type & WPS_AUTH_SHARED)
325 fprintf(nconf, "auth_algs=2\n");
327 fprintf(nconf, "auth_algs=1\n");
329 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx < 4) {
330 fprintf(nconf, "wep_default_key=%d\n", cred->key_idx);
331 fprintf(nconf, "wep_key%d=", cred->key_idx);
332 if (cred->key_len != 10 && cred->key_len != 26)
334 for (i = 0; i < cred->key_len; i++)
335 fputc(cred->key[i], nconf);
336 if (cred->key_len != 10 && cred->key_len != 26)
338 fprintf(nconf, "\n");
342 fprintf(nconf, "# WPS configuration - END\n");
345 while (fgets(buf, sizeof(buf), oconf)) {
346 if (os_strncmp(buf, "bss=", 4) == 0)
349 (str_starts(buf, "ssid=") ||
350 str_starts(buf, "auth_algs=") ||
351 str_starts(buf, "wps_state=") ||
352 str_starts(buf, "wpa=") ||
353 str_starts(buf, "wpa_psk=") ||
354 str_starts(buf, "wpa_pairwise=") ||
355 str_starts(buf, "rsn_pairwise=") ||
356 str_starts(buf, "wpa_key_mgmt=") ||
357 str_starts(buf, "wpa_passphrase="))) {
358 fprintf(nconf, "#WPS# %s", buf);
360 fprintf(nconf, "%s", buf);
366 if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
367 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
368 "configuration file: %s", strerror(errno));
375 /* Schedule configuration reload after short period of time to allow
376 * EAP-WSC to be finished.
378 eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
381 /* TODO: dualband AP may need to update multiple configuration files */
383 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
389 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
390 struct wps_event_pwd_auth_fail *data)
398 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
399 * if this happens multiple times.
401 hapd->ap_pin_failures++;
402 if (hapd->ap_pin_failures < 4)
405 wpa_msg(hapd, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
406 hapd->wps->ap_setup_locked = 1;
408 wps_registrar_update_ie(hapd->wps->registrar);
410 if (hapd->conf->wps_cred_processing == 1)
413 f = fopen(hapd->iface->config_fname, "a");
415 wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
416 "configuration file");
420 fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
421 fprintf(f, "ap_setup_locked=1\n");
424 /* TODO: dualband AP may need to update multiple configuration files */
426 wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
430 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
431 union wps_event_data *data)
433 struct hostapd_data *hapd = ctx;
435 if (event == WPS_EV_PWD_AUTH_FAIL)
436 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
440 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
442 os_free(hapd->wps_beacon_ie);
443 hapd->wps_beacon_ie = NULL;
444 hapd->wps_beacon_ie_len = 0;
445 hostapd_set_wps_beacon_ie(hapd, NULL, 0);
447 os_free(hapd->wps_probe_resp_ie);
448 hapd->wps_probe_resp_ie = NULL;
449 hapd->wps_probe_resp_ie_len = 0;
450 hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
454 int hostapd_init_wps(struct hostapd_data *hapd,
455 struct hostapd_bss_config *conf)
457 struct wps_context *wps;
458 struct wps_registrar_config cfg;
460 if (conf->wps_state == 0) {
461 hostapd_wps_clear_ies(hapd);
465 wps = os_zalloc(sizeof(*wps));
469 wps->cred_cb = hostapd_wps_cred_cb;
470 wps->event_cb = hostapd_wps_event_cb;
473 os_memset(&cfg, 0, sizeof(cfg));
474 wps->wps_state = hapd->conf->wps_state;
475 wps->ap_setup_locked = hapd->conf->ap_setup_locked;
476 if (is_nil_uuid(hapd->conf->uuid)) {
477 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
478 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
479 wps->uuid, UUID_LEN);
481 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
482 wps->ssid_len = hapd->conf->ssid.ssid_len;
483 os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
485 os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
486 wps->dev.device_name = hapd->conf->device_name ?
487 os_strdup(hapd->conf->device_name) : NULL;
488 wps->dev.manufacturer = hapd->conf->manufacturer ?
489 os_strdup(hapd->conf->manufacturer) : NULL;
490 wps->dev.model_name = hapd->conf->model_name ?
491 os_strdup(hapd->conf->model_name) : NULL;
492 wps->dev.model_number = hapd->conf->model_number ?
493 os_strdup(hapd->conf->model_number) : NULL;
494 wps->dev.serial_number = hapd->conf->serial_number ?
495 os_strdup(hapd->conf->serial_number) : NULL;
496 if (hapd->conf->config_methods) {
497 char *m = hapd->conf->config_methods;
498 if (os_strstr(m, "label"))
499 wps->config_methods |= WPS_CONFIG_LABEL;
500 if (os_strstr(m, "display"))
501 wps->config_methods |= WPS_CONFIG_DISPLAY;
502 if (os_strstr(m, "push_button"))
503 wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
504 if (os_strstr(m, "keypad"))
505 wps->config_methods |= WPS_CONFIG_KEYPAD;
507 if (hapd->conf->device_type) {
510 /* <categ>-<OUI>-<subcateg> */
511 wps->dev.categ = atoi(hapd->conf->device_type);
512 pos = os_strchr(hapd->conf->device_type, '-');
514 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
519 if (hexstr2bin(pos, oui, 4)) {
520 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
524 wps->dev.oui = WPA_GET_BE32(oui);
525 pos = os_strchr(pos, '-');
527 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
532 wps->dev.sub_categ = atoi(pos);
534 wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
535 wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
536 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
538 if (conf->wpa & WPA_PROTO_RSN) {
539 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
540 wps->auth_types |= WPS_AUTH_WPA2PSK;
541 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
542 wps->auth_types |= WPS_AUTH_WPA2;
544 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
545 wps->encr_types |= WPS_ENCR_AES;
546 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
547 wps->encr_types |= WPS_ENCR_TKIP;
550 if (conf->wpa & WPA_PROTO_WPA) {
551 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
552 wps->auth_types |= WPS_AUTH_WPAPSK;
553 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
554 wps->auth_types |= WPS_AUTH_WPA;
556 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
557 wps->encr_types |= WPS_ENCR_AES;
558 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
559 wps->encr_types |= WPS_ENCR_TKIP;
562 if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
563 wps->encr_types |= WPS_ENCR_NONE;
564 wps->auth_types |= WPS_AUTH_OPEN;
565 } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
566 wps->encr_types |= WPS_ENCR_WEP;
567 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
568 wps->auth_types |= WPS_AUTH_OPEN;
569 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
570 wps->auth_types |= WPS_AUTH_SHARED;
571 } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
572 wps->auth_types |= WPS_AUTH_OPEN;
573 if (conf->default_wep_key_len)
574 wps->encr_types |= WPS_ENCR_WEP;
576 wps->encr_types |= WPS_ENCR_NONE;
579 if (conf->ssid.wpa_psk_file) {
580 /* Use per-device PSKs */
581 } else if (conf->ssid.wpa_passphrase) {
582 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
583 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
584 } else if (conf->ssid.wpa_psk) {
585 wps->network_key = os_malloc(2 * PMK_LEN + 1);
586 if (wps->network_key == NULL) {
590 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
591 conf->ssid.wpa_psk->psk, PMK_LEN);
592 wps->network_key_len = 2 * PMK_LEN;
593 } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
594 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
595 if (wps->network_key == NULL) {
599 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
600 conf->ssid.wep.len[0]);
601 wps->network_key_len = conf->ssid.wep.len[0];
604 if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
605 /* Override parameters to enable security by default */
606 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
607 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
610 wps->ap_settings = conf->ap_settings;
611 wps->ap_settings_len = conf->ap_settings_len;
613 cfg.new_psk_cb = hostapd_wps_new_psk_cb;
614 cfg.set_ie_cb = hostapd_wps_set_ie_cb;
615 cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
616 cfg.reg_success_cb = hostapd_wps_reg_success_cb;
618 cfg.skip_cred_build = conf->skip_cred_build;
619 cfg.extra_cred = conf->extra_cred;
620 cfg.extra_cred_len = conf->extra_cred_len;
621 cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
622 conf->skip_cred_build;
624 wps->registrar = wps_registrar_init(wps, &cfg);
625 if (wps->registrar == NULL) {
626 printf("Failed to initialize WPS Registrar\n");
627 os_free(wps->network_key);
632 #ifdef CONFIG_WPS_UPNP
633 wps->friendly_name = hapd->conf->friendly_name;
634 wps->manufacturer_url = hapd->conf->manufacturer_url;
635 wps->model_description = hapd->conf->model_description;
636 wps->model_url = hapd->conf->model_url;
637 wps->upc = hapd->conf->upc;
639 if (hostapd_wps_upnp_init(hapd, wps) < 0) {
640 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
641 wps_registrar_deinit(wps->registrar);
642 os_free(wps->network_key);
646 #endif /* CONFIG_WPS_UPNP */
654 void hostapd_deinit_wps(struct hostapd_data *hapd)
656 if (hapd->wps == NULL)
658 #ifdef CONFIG_WPS_UPNP
659 hostapd_wps_upnp_deinit(hapd);
660 #endif /* CONFIG_WPS_UPNP */
661 wps_registrar_deinit(hapd->wps->registrar);
662 os_free(hapd->wps->network_key);
663 wps_device_data_free(&hapd->wps->dev);
664 wps_free_pending_msgs(hapd->wps->upnp_msgs);
667 hostapd_wps_clear_ies(hapd);
671 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
677 if (hapd->wps == NULL)
679 if (os_strcmp(uuid, "any") == 0)
681 else if (uuid_str2bin(uuid, u))
683 return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
684 (const u8 *) pin, os_strlen(pin));
688 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
690 if (hapd->wps == NULL)
692 return wps_registrar_button_pushed(hapd->wps->registrar);
696 void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
697 const u8 *ie, size_t ie_len)
699 struct wpabuf *wps_ie;
700 const u8 *end, *pos, *wps;
702 if (hapd->wps == NULL)
709 while (pos + 1 < end) {
710 if (pos + 2 + pos[1] > end)
712 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
713 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
721 return; /* No WPS IE in Probe Request */
723 wps_ie = wpabuf_alloc(ie_len);
727 /* There may be multiple WPS IEs in the message, so need to concatenate
728 * their WPS Data fields */
729 while (pos + 1 < end) {
730 if (pos + 2 + pos[1] > end)
732 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
733 WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
734 wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
738 if (wpabuf_len(wps_ie) > 0) {
739 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
740 #ifdef CONFIG_WPS_UPNP
741 /* FIX: what exactly should be included in the WLANEvent?
742 * WPS attributes? Full ProbeReq frame? */
743 upnp_wps_device_send_wlan_event(hapd->wps_upnp, addr,
744 UPNP_WPS_WLANEVENT_TYPE_PROBE,
746 #endif /* CONFIG_WPS_UPNP */
753 #ifdef CONFIG_WPS_UPNP
755 static struct wpabuf *
756 hostapd_rx_req_get_device_info(void *priv, struct upnp_wps_peer *peer)
758 struct hostapd_data *hapd = priv;
759 struct wps_config cfg;
760 struct wps_data *wps;
761 enum wsc_op_code op_code;
765 * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
766 * registration over UPnP with the AP acting as an Enrollee. It should
767 * be noted that this is frequently used just to get the device data,
768 * i.e., there may not be any intent to actually complete the
773 wps_deinit(peer->wps);
775 os_memset(&cfg, 0, sizeof(cfg));
777 cfg.pin = (u8 *) hapd->conf->ap_pin;
778 cfg.pin_len = os_strlen(hapd->conf->ap_pin);
779 wps = wps_init(&cfg);
783 m1 = wps_get_msg(wps, &op_code);
795 static struct wpabuf *
796 hostapd_rx_req_put_message(void *priv, struct upnp_wps_peer *peer,
797 const struct wpabuf *msg)
799 enum wps_process_res res;
800 enum wsc_op_code op_code;
802 /* PutMessage: msg = InMessage, return OutMessage */
803 res = wps_process_msg(peer->wps, WSC_UPnP, msg);
804 if (res == WPS_FAILURE)
806 return wps_get_msg(peer->wps, &op_code);
810 static struct wpabuf *
811 hostapd_rx_req_get_ap_settings(void *priv, const struct wpabuf *msg)
813 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
818 static int hostapd_rx_req_set_ap_settings(void *priv, const struct wpabuf *msg)
820 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
825 static int hostapd_rx_req_del_ap_settings(void *priv, const struct wpabuf *msg)
827 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
832 static struct wpabuf *
833 hostapd_rx_req_get_sta_settings(void *priv, const struct wpabuf *msg)
835 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
840 static int hostapd_rx_req_set_sta_settings(void *priv,
841 const struct wpabuf *msg)
843 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
848 static int hostapd_rx_req_del_sta_settings(void *priv,
849 const struct wpabuf *msg)
851 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
856 static int hostapd_rx_req_put_wlan_response(
857 void *priv, enum upnp_wps_wlanevent_type ev_type,
858 const u8 *mac_addr, const struct wpabuf *msg,
859 enum wps_msg_type msg_type)
861 struct hostapd_data *hapd = priv;
862 struct sta_info *sta;
863 struct upnp_pending_message *p;
865 wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
866 MACSTR, ev_type, MAC2STR(mac_addr));
867 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
868 wpabuf_head(msg), wpabuf_len(msg));
869 if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
870 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
871 "PutWLANResponse WLANEventType %d", ev_type);
876 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
877 * server implementation for delivery to the peer.
880 sta = ap_get_sta(hapd, mac_addr);
883 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
884 * Pick STA that is in an ongoing WPS registration without
885 * checking the MAC address.
887 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
888 "on NewWLANEventMAC; try wildcard match");
889 for (sta = hapd->sta_list; sta; sta = sta->next) {
890 if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
896 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
900 p = os_zalloc(sizeof(*p));
903 os_memcpy(p->addr, sta->addr, ETH_ALEN);
904 p->msg = wpabuf_dup(msg);
906 p->next = hapd->wps->upnp_msgs;
907 hapd->wps->upnp_msgs = p;
909 return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
913 static int hostapd_rx_req_set_selected_registrar(void *priv,
914 const struct wpabuf *msg)
916 struct hostapd_data *hapd = priv;
917 return wps_registrar_set_selected_registrar(hapd->wps->registrar, msg);
921 static int hostapd_rx_req_reboot_ap(void *priv, const struct wpabuf *msg)
923 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
928 static int hostapd_rx_req_reset_ap(void *priv, const struct wpabuf *msg)
930 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
935 static int hostapd_rx_req_reboot_sta(void *priv, const struct wpabuf *msg)
937 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
942 static int hostapd_rx_req_reset_sta(void *priv, const struct wpabuf *msg)
944 wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
949 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
950 struct wps_context *wps)
952 struct upnp_wps_device_ctx *ctx;
954 if (!hapd->conf->upnp_iface)
956 ctx = os_zalloc(sizeof(*ctx));
960 ctx->rx_req_get_device_info = hostapd_rx_req_get_device_info;
961 ctx->rx_req_put_message = hostapd_rx_req_put_message;
962 ctx->rx_req_get_ap_settings = hostapd_rx_req_get_ap_settings;
963 ctx->rx_req_set_ap_settings = hostapd_rx_req_set_ap_settings;
964 ctx->rx_req_del_ap_settings = hostapd_rx_req_del_ap_settings;
965 ctx->rx_req_get_sta_settings = hostapd_rx_req_get_sta_settings;
966 ctx->rx_req_set_sta_settings = hostapd_rx_req_set_sta_settings;
967 ctx->rx_req_del_sta_settings = hostapd_rx_req_del_sta_settings;
968 ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
969 ctx->rx_req_set_selected_registrar =
970 hostapd_rx_req_set_selected_registrar;
971 ctx->rx_req_reboot_ap = hostapd_rx_req_reboot_ap;
972 ctx->rx_req_reset_ap = hostapd_rx_req_reset_ap;
973 ctx->rx_req_reboot_sta = hostapd_rx_req_reboot_sta;
974 ctx->rx_req_reset_sta = hostapd_rx_req_reset_sta;
976 hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
977 if (hapd->wps_upnp == NULL) {
981 wps->wps_upnp = hapd->wps_upnp;
983 if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
984 upnp_wps_device_deinit(hapd->wps_upnp);
985 hapd->wps_upnp = NULL;
993 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
995 upnp_wps_device_deinit(hapd->wps_upnp);
998 #endif /* CONFIG_WPS_UPNP */