Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * hostapd / WPS integration |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 3 | * Copyright (c) 2008-2016, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "utils/includes.h" |
| 10 | |
| 11 | #include "utils/common.h" |
| 12 | #include "utils/eloop.h" |
| 13 | #include "utils/uuid.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 14 | #include "common/wpa_ctrl.h" |
| 15 | #include "common/ieee802_11_defs.h" |
| 16 | #include "common/ieee802_11_common.h" |
| 17 | #include "eapol_auth/eapol_auth_sm.h" |
| 18 | #include "eapol_auth/eapol_auth_sm_i.h" |
| 19 | #include "wps/wps.h" |
| 20 | #include "wps/wps_defs.h" |
| 21 | #include "wps/wps_dev_attr.h" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 22 | #include "wps/wps_attr_parse.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 23 | #include "hostapd.h" |
| 24 | #include "ap_config.h" |
| 25 | #include "ap_drv_ops.h" |
| 26 | #include "beacon.h" |
| 27 | #include "sta_info.h" |
| 28 | #include "wps_hostapd.h" |
| 29 | |
| 30 | |
| 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 */ |
| 37 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 38 | static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da, |
| 39 | const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 40 | const u8 *ie, size_t ie_len, |
| 41 | int ssi_signal); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 42 | static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 43 | static void hostapd_wps_nfc_clear(struct wps_context *wps); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 44 | |
| 45 | |
| 46 | struct wps_for_each_data { |
| 47 | int (*func)(struct hostapd_data *h, void *ctx); |
| 48 | void *ctx; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 49 | struct hostapd_data *calling_hapd; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | |
| 53 | static int wps_for_each(struct hostapd_iface *iface, void *ctx) |
| 54 | { |
| 55 | struct wps_for_each_data *data = ctx; |
| 56 | size_t j; |
| 57 | |
| 58 | if (iface == NULL) |
| 59 | return 0; |
| 60 | for (j = 0; j < iface->num_bss; j++) { |
| 61 | struct hostapd_data *hapd = iface->bss[j]; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 62 | int ret; |
| 63 | |
| 64 | if (hapd != data->calling_hapd && |
| 65 | (hapd->conf->wps_independent || |
| 66 | data->calling_hapd->conf->wps_independent)) |
| 67 | continue; |
| 68 | |
| 69 | ret = data->func(hapd, data->ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 70 | if (ret) |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | static int hostapd_wps_for_each(struct hostapd_data *hapd, |
| 79 | int (*func)(struct hostapd_data *h, void *ctx), |
| 80 | void *ctx) |
| 81 | { |
| 82 | struct hostapd_iface *iface = hapd->iface; |
| 83 | struct wps_for_each_data data; |
| 84 | data.func = func; |
| 85 | data.ctx = ctx; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 86 | data.calling_hapd = hapd; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 87 | if (iface->interfaces == NULL || |
| 88 | iface->interfaces->for_each_interface == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 89 | return wps_for_each(iface, &data); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 90 | return iface->interfaces->for_each_interface(iface->interfaces, |
| 91 | wps_for_each, &data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 95 | static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, |
| 96 | const u8 *p2p_dev_addr, const u8 *psk, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 97 | size_t psk_len) |
| 98 | { |
| 99 | struct hostapd_data *hapd = ctx; |
| 100 | struct hostapd_wpa_psk *p; |
| 101 | struct hostapd_ssid *ssid = &hapd->conf->ssid; |
| 102 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 103 | if (is_zero_ether_addr(p2p_dev_addr)) { |
| 104 | wpa_printf(MSG_DEBUG, |
| 105 | "Received new WPA/WPA2-PSK from WPS for STA " MACSTR, |
| 106 | MAC2STR(mac_addr)); |
| 107 | } else { |
| 108 | wpa_printf(MSG_DEBUG, |
| 109 | "Received new WPA/WPA2-PSK from WPS for STA " MACSTR |
| 110 | " P2P Device Addr " MACSTR, |
| 111 | MAC2STR(mac_addr), MAC2STR(p2p_dev_addr)); |
| 112 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 113 | wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len); |
| 114 | |
| 115 | if (psk_len != PMK_LEN) { |
| 116 | wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu", |
| 117 | (unsigned long) psk_len); |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | /* Add the new PSK to runtime PSK list */ |
| 122 | p = os_zalloc(sizeof(*p)); |
| 123 | if (p == NULL) |
| 124 | return -1; |
| 125 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 126 | os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 127 | os_memcpy(p->psk, psk, PMK_LEN); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 128 | p->wps = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 129 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 130 | if (hapd->new_psk_cb) { |
| 131 | hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr, |
| 132 | psk, psk_len); |
| 133 | } |
| 134 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 135 | p->next = ssid->wpa_psk; |
| 136 | ssid->wpa_psk = p; |
| 137 | |
| 138 | if (ssid->wpa_psk_file) { |
| 139 | FILE *f; |
| 140 | char hex[PMK_LEN * 2 + 1]; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 141 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 142 | /* Add the new PSK to PSK list file */ |
| 143 | f = fopen(ssid->wpa_psk_file, "a"); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 144 | if (!f) { |
| 145 | wpa_printf(MSG_DEBUG, "Failed to add the PSK to '%s'", |
| 146 | ssid->wpa_psk_file); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 151 | fprintf(f, "wps=1 " MACSTR " %s\n", MAC2STR(mac_addr), hex); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 152 | fclose(f); |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie, |
| 160 | struct wpabuf *probe_resp_ie) |
| 161 | { |
| 162 | struct hostapd_data *hapd = ctx; |
| 163 | wpabuf_free(hapd->wps_beacon_ie); |
| 164 | hapd->wps_beacon_ie = beacon_ie; |
| 165 | wpabuf_free(hapd->wps_probe_resp_ie); |
| 166 | hapd->wps_probe_resp_ie = probe_resp_ie; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 167 | if (hapd->beacon_set_done) |
| 168 | ieee802_11_set_beacon(hapd); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 169 | return hostapd_set_ap_wps_ie(hapd); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e, |
| 174 | const struct wps_device_data *dev) |
| 175 | { |
| 176 | struct hostapd_data *hapd = ctx; |
| 177 | char uuid[40], txt[400]; |
| 178 | int len; |
| 179 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
| 180 | if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) |
| 181 | return; |
| 182 | wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid); |
| 183 | len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED |
| 184 | "%s " MACSTR " [%s|%s|%s|%s|%s|%s]", |
| 185 | uuid, MAC2STR(dev->mac_addr), dev->device_name, |
| 186 | dev->manufacturer, dev->model_name, |
| 187 | dev->model_number, dev->serial_number, |
| 188 | wps_dev_type_bin2str(dev->pri_dev_type, devtype, |
| 189 | sizeof(devtype))); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 190 | if (!os_snprintf_error(sizeof(txt), len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 191 | wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt); |
| 192 | |
| 193 | if (hapd->conf->wps_pin_requests) { |
| 194 | FILE *f; |
| 195 | struct os_time t; |
| 196 | f = fopen(hapd->conf->wps_pin_requests, "a"); |
| 197 | if (f == NULL) |
| 198 | return; |
| 199 | os_get_time(&t); |
| 200 | fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s" |
| 201 | "\t%s\n", |
| 202 | t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name, |
| 203 | dev->manufacturer, dev->model_name, dev->model_number, |
| 204 | dev->serial_number, |
| 205 | wps_dev_type_bin2str(dev->pri_dev_type, devtype, |
| 206 | sizeof(devtype))); |
| 207 | fclose(f); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 212 | struct wps_stop_reg_data { |
| 213 | struct hostapd_data *current_hapd; |
| 214 | const u8 *uuid_e; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 215 | const u8 *dev_pw; |
| 216 | size_t dev_pw_len; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx) |
| 220 | { |
| 221 | struct wps_stop_reg_data *data = ctx; |
| 222 | if (hapd != data->current_hapd && hapd->wps != NULL) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 223 | wps_registrar_complete(hapd->wps->registrar, data->uuid_e, |
| 224 | data->dev_pw, data->dev_pw_len); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 229 | static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 230 | const u8 *uuid_e, const u8 *dev_pw, |
| 231 | size_t dev_pw_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 232 | { |
| 233 | struct hostapd_data *hapd = ctx; |
| 234 | char uuid[40]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 235 | struct wps_stop_reg_data data; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 236 | if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) |
| 237 | return; |
| 238 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s", |
| 239 | MAC2STR(mac_addr), uuid); |
| 240 | if (hapd->wps_reg_success_cb) |
| 241 | hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx, |
| 242 | mac_addr, uuid_e); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 243 | data.current_hapd = hapd; |
| 244 | data.uuid_e = uuid_e; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 245 | data.dev_pw = dev_pw; |
| 246 | data.dev_pw_len = dev_pw_len; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 247 | hostapd_wps_for_each(hapd, wps_stop_registrar, &data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | |
| 251 | static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr, |
| 252 | const u8 *uuid_e, |
| 253 | const u8 *pri_dev_type, |
| 254 | u16 config_methods, |
| 255 | u16 dev_password_id, u8 request_type, |
| 256 | const char *dev_name) |
| 257 | { |
| 258 | struct hostapd_data *hapd = ctx; |
| 259 | char uuid[40]; |
| 260 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
| 261 | if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) |
| 262 | return; |
| 263 | if (dev_name == NULL) |
| 264 | dev_name = ""; |
| 265 | wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR |
| 266 | " %s %s 0x%x %u %u [%s]", |
| 267 | MAC2STR(addr), uuid, |
| 268 | wps_dev_type_bin2str(pri_dev_type, devtype, |
| 269 | sizeof(devtype)), |
| 270 | config_methods, dev_password_id, request_type, dev_name); |
| 271 | } |
| 272 | |
| 273 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 274 | static int hostapd_wps_lookup_pskfile_cb(void *ctx, const u8 *mac_addr, |
| 275 | const u8 **psk) |
| 276 | { |
| 277 | const struct hostapd_data *hapd = ctx; |
| 278 | const struct hostapd_wpa_psk *wpa_psk; |
| 279 | const u8 *any_psk = NULL; |
| 280 | const u8 *dev_psk = NULL; |
| 281 | |
| 282 | for (wpa_psk = hapd->conf->ssid.wpa_psk; wpa_psk; |
| 283 | wpa_psk = wpa_psk->next) { |
| 284 | if (!wpa_psk->wps) |
| 285 | continue; |
| 286 | |
| 287 | if (!any_psk && is_zero_ether_addr(wpa_psk->addr)) |
| 288 | any_psk = wpa_psk->psk; |
| 289 | |
| 290 | if (mac_addr && !dev_psk && |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 291 | ether_addr_equal(mac_addr, wpa_psk->addr)) { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 292 | dev_psk = wpa_psk->psk; |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (dev_psk) { |
| 298 | *psk = dev_psk; |
| 299 | } else if (any_psk) { |
| 300 | *psk = any_psk; |
| 301 | } else { |
| 302 | *psk = NULL; |
| 303 | wpa_printf(MSG_DEBUG, |
| 304 | "WPS: No appropriate PSK in wpa_psk_file"); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | return 1; |
| 309 | } |
| 310 | |
| 311 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 312 | static void wps_reload_config(void *eloop_data, void *user_ctx) |
| 313 | { |
| 314 | struct hostapd_iface *iface = eloop_data; |
| 315 | |
| 316 | wpa_printf(MSG_DEBUG, "WPS: Reload configuration data"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 317 | if (iface->interfaces == NULL || |
| 318 | iface->interfaces->reload_config(iface) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 319 | wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated " |
| 320 | "configuration"); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 325 | void hostapd_wps_eap_completed(struct hostapd_data *hapd) |
| 326 | { |
| 327 | /* |
| 328 | * Reduce race condition of the station trying to reconnect immediately |
| 329 | * after AP reconfiguration through WPS by rescheduling the reload |
| 330 | * timeout to happen after EAP completion rather than the originally |
| 331 | * scheduled 100 ms after new configuration became known. |
| 332 | */ |
| 333 | if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) == |
| 334 | 1) |
| 335 | wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload"); |
| 336 | } |
| 337 | |
| 338 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 339 | static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr, |
| 340 | size_t attr_len) |
| 341 | { |
| 342 | size_t blen = attr_len * 2 + 1; |
| 343 | char *buf = os_malloc(blen); |
| 344 | if (buf) { |
| 345 | wpa_snprintf_hex(buf, blen, attr, attr_len); |
| 346 | wpa_msg(hapd->msg_ctx, MSG_INFO, |
| 347 | WPS_EVENT_NEW_AP_SETTINGS "%s", buf); |
| 348 | os_free(buf); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 353 | static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd, |
| 354 | const struct wps_credential *cred) |
| 355 | { |
| 356 | struct hostapd_bss_config *bss = hapd->conf; |
| 357 | |
| 358 | wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration"); |
| 359 | |
| 360 | bss->wps_state = 2; |
Dmitry Shmidt | 9d9e602 | 2015-04-23 10:34:55 -0700 | [diff] [blame] | 361 | if (cred->ssid_len <= SSID_MAX_LEN) { |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 362 | os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len); |
| 363 | bss->ssid.ssid_len = cred->ssid_len; |
| 364 | bss->ssid.ssid_set = 1; |
| 365 | } |
| 366 | |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 367 | #ifdef CONFIG_NO_TKIP |
| 368 | if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK | |
| 369 | WPS_AUTH_WPA | WPS_AUTH_WPAPSK)) |
| 370 | bss->wpa = 2; |
| 371 | else |
| 372 | bss->wpa = 0; |
| 373 | #else /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 374 | if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) && |
| 375 | (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))) |
| 376 | bss->wpa = 3; |
| 377 | else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) |
| 378 | bss->wpa = 2; |
| 379 | else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)) |
| 380 | bss->wpa = 1; |
| 381 | else |
| 382 | bss->wpa = 0; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 383 | #endif /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 384 | |
| 385 | if (bss->wpa) { |
| 386 | if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) |
| 387 | bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X; |
| 388 | if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) |
| 389 | bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK; |
| 390 | |
| 391 | bss->wpa_pairwise = 0; |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 392 | if (cred->encr_type & WPS_ENCR_AES) { |
| 393 | if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD) |
| 394 | bss->wpa_pairwise |= WPA_CIPHER_GCMP; |
| 395 | else |
| 396 | bss->wpa_pairwise |= WPA_CIPHER_CCMP; |
| 397 | } |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 398 | #ifndef CONFIG_NO_TKIP |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 399 | if (cred->encr_type & WPS_ENCR_TKIP) |
| 400 | bss->wpa_pairwise |= WPA_CIPHER_TKIP; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 401 | #endif /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 402 | bss->rsn_pairwise = bss->wpa_pairwise; |
| 403 | bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, |
| 404 | bss->wpa_pairwise, |
| 405 | bss->rsn_pairwise); |
| 406 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 407 | if (hapd->conf->wps_cred_add_sae && |
| 408 | (cred->auth_type & WPS_AUTH_WPA2PSK) && |
| 409 | cred->key_len != 2 * PMK_LEN) { |
| 410 | bss->wpa_key_mgmt |= WPA_KEY_MGMT_SAE; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 411 | if (bss->ieee80211w == NO_MGMT_FRAME_PROTECTION) |
| 412 | bss->ieee80211w = |
| 413 | MGMT_FRAME_PROTECTION_OPTIONAL; |
| 414 | bss->sae_require_mfp = 1; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 417 | if (cred->key_len >= 8 && cred->key_len < 64) { |
| 418 | os_free(bss->ssid.wpa_passphrase); |
| 419 | bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1); |
| 420 | if (bss->ssid.wpa_passphrase) |
| 421 | os_memcpy(bss->ssid.wpa_passphrase, cred->key, |
| 422 | cred->key_len); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 423 | hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 424 | } else if (cred->key_len == 64) { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 425 | hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 426 | bss->ssid.wpa_psk = |
| 427 | os_zalloc(sizeof(struct hostapd_wpa_psk)); |
| 428 | if (bss->ssid.wpa_psk && |
| 429 | hexstr2bin((const char *) cred->key, |
| 430 | bss->ssid.wpa_psk->psk, PMK_LEN) == 0) { |
| 431 | bss->ssid.wpa_psk->group = 1; |
| 432 | os_free(bss->ssid.wpa_passphrase); |
| 433 | bss->ssid.wpa_passphrase = NULL; |
| 434 | } |
| 435 | } |
| 436 | bss->auth_algs = 1; |
| 437 | } else { |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 438 | /* |
| 439 | * WPS 2.0 does not allow WEP to be configured, so no need to |
| 440 | * process that option here either. |
| 441 | */ |
| 442 | bss->auth_algs = 1; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* Schedule configuration reload after short period of time to allow |
| 446 | * EAP-WSC to be finished. |
| 447 | */ |
| 448 | eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface, |
| 449 | NULL); |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 455 | static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx) |
| 456 | { |
| 457 | const struct wps_credential *cred = ctx; |
| 458 | FILE *oconf, *nconf; |
| 459 | size_t len, i; |
| 460 | char *tmp_fname; |
| 461 | char buf[1024]; |
| 462 | int multi_bss; |
| 463 | int wpa; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 464 | int pmf_changed = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 465 | |
| 466 | if (hapd->wps == NULL) |
| 467 | return 0; |
| 468 | |
| 469 | wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute", |
| 470 | cred->cred_attr, cred->cred_attr_len); |
| 471 | |
| 472 | wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings"); |
| 473 | wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len); |
| 474 | wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x", |
| 475 | cred->auth_type); |
| 476 | wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type); |
| 477 | wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx); |
| 478 | wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key", |
| 479 | cred->key, cred->key_len); |
| 480 | wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR, |
| 481 | MAC2STR(cred->mac_addr)); |
| 482 | |
| 483 | if ((hapd->conf->wps_cred_processing == 1 || |
| 484 | hapd->conf->wps_cred_processing == 2) && cred->cred_attr) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 485 | hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len); |
| 486 | } else if (hapd->conf->wps_cred_processing == 1 || |
| 487 | hapd->conf->wps_cred_processing == 2) { |
| 488 | struct wpabuf *attr; |
| 489 | attr = wpabuf_alloc(200); |
| 490 | if (attr && wps_build_credential_wrap(attr, cred) == 0) |
| 491 | hapd_new_ap_event(hapd, wpabuf_head_u8(attr), |
| 492 | wpabuf_len(attr)); |
| 493 | wpabuf_free(attr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 494 | } else |
| 495 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS); |
| 496 | |
| 497 | if (hapd->conf->wps_cred_processing == 1) |
| 498 | return 0; |
| 499 | |
| 500 | os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len); |
| 501 | hapd->wps->ssid_len = cred->ssid_len; |
| 502 | hapd->wps->encr_types = cred->encr_type; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 503 | hapd->wps->encr_types_rsn = cred->encr_type; |
| 504 | hapd->wps->encr_types_wpa = cred->encr_type; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 505 | hapd->wps->auth_types = cred->auth_type; |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 506 | hapd->wps->ap_encr_type = cred->encr_type; |
| 507 | hapd->wps->ap_auth_type = cred->auth_type; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 508 | if (cred->key_len == 0) { |
| 509 | os_free(hapd->wps->network_key); |
| 510 | hapd->wps->network_key = NULL; |
| 511 | hapd->wps->network_key_len = 0; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 512 | } else if ((cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) && |
| 513 | (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN)) { |
| 514 | wpa_printf(MSG_INFO, "WPS: Invalid key length %lu for WPA/WPA2", |
| 515 | (unsigned long) cred->key_len); |
| 516 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 517 | } else { |
| 518 | if (hapd->wps->network_key == NULL || |
| 519 | hapd->wps->network_key_len < cred->key_len) { |
| 520 | hapd->wps->network_key_len = 0; |
| 521 | os_free(hapd->wps->network_key); |
| 522 | hapd->wps->network_key = os_malloc(cred->key_len); |
| 523 | if (hapd->wps->network_key == NULL) |
| 524 | return -1; |
| 525 | } |
| 526 | hapd->wps->network_key_len = cred->key_len; |
| 527 | os_memcpy(hapd->wps->network_key, cred->key, cred->key_len); |
| 528 | } |
| 529 | hapd->wps->wps_state = WPS_STATE_CONFIGURED; |
| 530 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 531 | if (hapd->iface->config_fname == NULL) |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 532 | return hapd_wps_reconfig_in_memory(hapd, cred); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 533 | len = os_strlen(hapd->iface->config_fname) + 5; |
| 534 | tmp_fname = os_malloc(len); |
| 535 | if (tmp_fname == NULL) |
| 536 | return -1; |
| 537 | os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname); |
| 538 | |
| 539 | oconf = fopen(hapd->iface->config_fname, "r"); |
| 540 | if (oconf == NULL) { |
| 541 | wpa_printf(MSG_WARNING, "WPS: Could not open current " |
| 542 | "configuration file"); |
| 543 | os_free(tmp_fname); |
| 544 | return -1; |
| 545 | } |
| 546 | |
| 547 | nconf = fopen(tmp_fname, "w"); |
| 548 | if (nconf == NULL) { |
| 549 | wpa_printf(MSG_WARNING, "WPS: Could not write updated " |
| 550 | "configuration file"); |
| 551 | os_free(tmp_fname); |
| 552 | fclose(oconf); |
| 553 | return -1; |
| 554 | } |
| 555 | |
| 556 | fprintf(nconf, "# WPS configuration - START\n"); |
| 557 | |
| 558 | fprintf(nconf, "wps_state=2\n"); |
| 559 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 560 | if (is_hex(cred->ssid, cred->ssid_len)) { |
| 561 | fprintf(nconf, "ssid2="); |
| 562 | for (i = 0; i < cred->ssid_len; i++) |
| 563 | fprintf(nconf, "%02x", cred->ssid[i]); |
| 564 | fprintf(nconf, "\n"); |
| 565 | } else { |
| 566 | fprintf(nconf, "ssid="); |
| 567 | for (i = 0; i < cred->ssid_len; i++) |
| 568 | fputc(cred->ssid[i], nconf); |
| 569 | fprintf(nconf, "\n"); |
| 570 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 571 | |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 572 | #ifdef CONFIG_NO_TKIP |
| 573 | if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK | |
| 574 | WPS_AUTH_WPA | WPS_AUTH_WPAPSK)) |
| 575 | wpa = 2; |
| 576 | else |
| 577 | wpa = 0; |
| 578 | #else /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 579 | if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) && |
| 580 | (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))) |
| 581 | wpa = 3; |
| 582 | else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) |
| 583 | wpa = 2; |
| 584 | else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)) |
| 585 | wpa = 1; |
| 586 | else |
| 587 | wpa = 0; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 588 | #endif /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 589 | |
| 590 | if (wpa) { |
| 591 | char *prefix; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 592 | int sae = 0; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 593 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 594 | fprintf(nconf, "wpa=%d\n", wpa); |
| 595 | |
| 596 | fprintf(nconf, "wpa_key_mgmt="); |
| 597 | prefix = ""; |
| 598 | if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) { |
| 599 | fprintf(nconf, "WPA-EAP"); |
| 600 | prefix = " "; |
| 601 | } |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 602 | if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 603 | fprintf(nconf, "%sWPA-PSK", prefix); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 604 | prefix = " "; |
| 605 | } |
| 606 | if (hapd->conf->wps_cred_add_sae && |
| 607 | (cred->auth_type & WPS_AUTH_WPA2PSK) && |
| 608 | cred->key_len != 2 * PMK_LEN) { |
| 609 | fprintf(nconf, "%sSAE", prefix); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 610 | sae = 1; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 611 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 612 | fprintf(nconf, "\n"); |
| 613 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 614 | if (sae && hapd->conf->ieee80211w == NO_MGMT_FRAME_PROTECTION) { |
| 615 | fprintf(nconf, "ieee80211w=%d\n", |
| 616 | MGMT_FRAME_PROTECTION_OPTIONAL); |
| 617 | pmf_changed = 1; |
| 618 | } |
| 619 | if (sae) |
| 620 | fprintf(nconf, "sae_require_mfp=1\n"); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 621 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 622 | fprintf(nconf, "wpa_pairwise="); |
| 623 | prefix = ""; |
| 624 | if (cred->encr_type & WPS_ENCR_AES) { |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 625 | if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD) |
| 626 | fprintf(nconf, "GCMP"); |
| 627 | else |
| 628 | fprintf(nconf, "CCMP"); |
| 629 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 630 | prefix = " "; |
| 631 | } |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 632 | #ifndef CONFIG_NO_TKIP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 633 | if (cred->encr_type & WPS_ENCR_TKIP) { |
| 634 | fprintf(nconf, "%sTKIP", prefix); |
| 635 | } |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 636 | #endif /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 637 | fprintf(nconf, "\n"); |
| 638 | |
| 639 | if (cred->key_len >= 8 && cred->key_len < 64) { |
| 640 | fprintf(nconf, "wpa_passphrase="); |
| 641 | for (i = 0; i < cred->key_len; i++) |
| 642 | fputc(cred->key[i], nconf); |
| 643 | fprintf(nconf, "\n"); |
| 644 | } else if (cred->key_len == 64) { |
| 645 | fprintf(nconf, "wpa_psk="); |
| 646 | for (i = 0; i < cred->key_len; i++) |
| 647 | fputc(cred->key[i], nconf); |
| 648 | fprintf(nconf, "\n"); |
| 649 | } else { |
| 650 | wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu " |
| 651 | "for WPA/WPA2", |
| 652 | (unsigned long) cred->key_len); |
| 653 | } |
| 654 | |
| 655 | fprintf(nconf, "auth_algs=1\n"); |
| 656 | } else { |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 657 | /* |
| 658 | * WPS 2.0 does not allow WEP to be configured, so no need to |
| 659 | * process that option here either. |
| 660 | */ |
| 661 | fprintf(nconf, "auth_algs=1\n"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | fprintf(nconf, "# WPS configuration - END\n"); |
| 665 | |
| 666 | multi_bss = 0; |
| 667 | while (fgets(buf, sizeof(buf), oconf)) { |
| 668 | if (os_strncmp(buf, "bss=", 4) == 0) |
| 669 | multi_bss = 1; |
| 670 | if (!multi_bss && |
| 671 | (str_starts(buf, "ssid=") || |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 672 | str_starts(buf, "ssid2=") || |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 673 | str_starts(buf, "auth_algs=") || |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 674 | #ifdef CONFIG_WEP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 675 | str_starts(buf, "wep_default_key=") || |
| 676 | str_starts(buf, "wep_key") || |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 677 | #endif /* CONFIG_WEP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 678 | str_starts(buf, "wps_state=") || |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 679 | (pmf_changed && str_starts(buf, "ieee80211w=")) || |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 680 | str_starts(buf, "wpa=") || |
| 681 | str_starts(buf, "wpa_psk=") || |
| 682 | str_starts(buf, "wpa_pairwise=") || |
| 683 | str_starts(buf, "rsn_pairwise=") || |
| 684 | str_starts(buf, "wpa_key_mgmt=") || |
| 685 | str_starts(buf, "wpa_passphrase="))) { |
| 686 | fprintf(nconf, "#WPS# %s", buf); |
| 687 | } else |
| 688 | fprintf(nconf, "%s", buf); |
| 689 | } |
| 690 | |
| 691 | fclose(nconf); |
| 692 | fclose(oconf); |
| 693 | |
| 694 | if (rename(tmp_fname, hapd->iface->config_fname) < 0) { |
| 695 | wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated " |
| 696 | "configuration file: %s", strerror(errno)); |
| 697 | os_free(tmp_fname); |
| 698 | return -1; |
| 699 | } |
| 700 | |
| 701 | os_free(tmp_fname); |
| 702 | |
| 703 | /* Schedule configuration reload after short period of time to allow |
| 704 | * EAP-WSC to be finished. |
| 705 | */ |
| 706 | eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface, |
| 707 | NULL); |
| 708 | |
| 709 | wpa_printf(MSG_DEBUG, "WPS: AP configuration updated"); |
| 710 | |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | |
| 715 | static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred) |
| 716 | { |
| 717 | struct hostapd_data *hapd = ctx; |
| 718 | return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred); |
| 719 | } |
| 720 | |
| 721 | |
| 722 | static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx) |
| 723 | { |
| 724 | struct hostapd_data *hapd = eloop_data; |
| 725 | |
| 726 | if (hapd->conf->ap_setup_locked) |
| 727 | return; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 728 | if (hapd->ap_pin_failures_consecutive >= 10) |
| 729 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 730 | |
| 731 | wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN"); |
| 732 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED); |
| 733 | hapd->wps->ap_setup_locked = 0; |
| 734 | wps_registrar_update_ie(hapd->wps->registrar); |
| 735 | } |
| 736 | |
| 737 | |
| 738 | static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx) |
| 739 | { |
| 740 | struct wps_event_pwd_auth_fail *data = ctx; |
| 741 | |
| 742 | if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL) |
| 743 | return 0; |
| 744 | |
| 745 | /* |
| 746 | * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup |
| 747 | * for some time if this happens multiple times to slow down brute |
| 748 | * force attacks. |
| 749 | */ |
| 750 | hapd->ap_pin_failures++; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 751 | hapd->ap_pin_failures_consecutive++; |
| 752 | wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u " |
| 753 | "(%u consecutive)", |
| 754 | hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 755 | if (hapd->ap_pin_failures < 3) |
| 756 | return 0; |
| 757 | |
| 758 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED); |
| 759 | hapd->wps->ap_setup_locked = 1; |
| 760 | |
| 761 | wps_registrar_update_ie(hapd->wps->registrar); |
| 762 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 763 | if (!hapd->conf->ap_setup_locked && |
| 764 | hapd->ap_pin_failures_consecutive >= 10) { |
| 765 | /* |
| 766 | * In indefinite lockdown - disable automatic AP PIN |
| 767 | * reenablement. |
| 768 | */ |
| 769 | eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); |
| 770 | wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely"); |
| 771 | } else if (!hapd->conf->ap_setup_locked) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 772 | if (hapd->ap_pin_lockout_time == 0) |
| 773 | hapd->ap_pin_lockout_time = 60; |
| 774 | else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 && |
| 775 | (hapd->ap_pin_failures % 3) == 0) |
| 776 | hapd->ap_pin_lockout_time *= 2; |
| 777 | |
| 778 | wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds", |
| 779 | hapd->ap_pin_lockout_time); |
| 780 | eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); |
| 781 | eloop_register_timeout(hapd->ap_pin_lockout_time, 0, |
| 782 | hostapd_wps_reenable_ap_pin, hapd, |
| 783 | NULL); |
| 784 | } |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | static void hostapd_pwd_auth_fail(struct hostapd_data *hapd, |
| 791 | struct wps_event_pwd_auth_fail *data) |
| 792 | { |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 793 | /* Update WPS Status - Authentication Failure */ |
| 794 | wpa_printf(MSG_DEBUG, "WPS: Authentication failure update"); |
| 795 | hapd->wps_stats.status = WPS_STATUS_FAILURE; |
| 796 | hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE; |
| 797 | os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN); |
| 798 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 799 | hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data); |
| 800 | } |
| 801 | |
| 802 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 803 | static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx) |
| 804 | { |
| 805 | if (hapd->conf->ap_pin == NULL || hapd->wps == NULL) |
| 806 | return 0; |
| 807 | |
| 808 | if (hapd->ap_pin_failures_consecutive == 0) |
| 809 | return 0; |
| 810 | |
| 811 | wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter " |
| 812 | "- total validation failures %u (%u consecutive)", |
| 813 | hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive); |
| 814 | hapd->ap_pin_failures_consecutive = 0; |
| 815 | |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | |
| 820 | static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd) |
| 821 | { |
| 822 | hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL); |
| 823 | } |
| 824 | |
| 825 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 826 | static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd) |
| 827 | { |
| 828 | /* Update WPS Status - PBC Overlap */ |
| 829 | hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP; |
| 830 | } |
| 831 | |
| 832 | |
| 833 | static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd) |
| 834 | { |
| 835 | /* Update WPS PBC Status:PBC Timeout */ |
| 836 | hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT; |
| 837 | } |
| 838 | |
| 839 | |
| 840 | static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd) |
| 841 | { |
| 842 | /* Update WPS PBC status - Active */ |
| 843 | hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE; |
| 844 | } |
| 845 | |
| 846 | |
| 847 | static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd) |
| 848 | { |
| 849 | /* Update WPS PBC status - Active */ |
| 850 | hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE; |
| 851 | } |
| 852 | |
| 853 | |
| 854 | static void hostapd_wps_event_success(struct hostapd_data *hapd, |
| 855 | struct wps_event_success *success) |
| 856 | { |
| 857 | /* Update WPS status - Success */ |
| 858 | hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE; |
| 859 | hapd->wps_stats.status = WPS_STATUS_SUCCESS; |
| 860 | os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN); |
| 861 | } |
| 862 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 863 | |
| 864 | static void hostapd_wps_event_fail(struct hostapd_data *hapd, |
| 865 | struct wps_event_fail *fail) |
| 866 | { |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 867 | /* Update WPS status - Failure */ |
| 868 | hapd->wps_stats.status = WPS_STATUS_FAILURE; |
| 869 | os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN); |
| 870 | |
| 871 | hapd->wps_stats.failure_reason = fail->error_indication; |
| 872 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 873 | if (fail->error_indication > 0 && |
| 874 | fail->error_indication < NUM_WPS_EI_VALUES) { |
| 875 | wpa_msg(hapd->msg_ctx, MSG_INFO, |
| 876 | WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)", |
| 877 | fail->msg, fail->config_error, fail->error_indication, |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 878 | wps_ei_str(fail->error_indication)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 879 | } else { |
| 880 | wpa_msg(hapd->msg_ctx, MSG_INFO, |
| 881 | WPS_EVENT_FAIL "msg=%d config_error=%d", |
| 882 | fail->msg, fail->config_error); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | |
| 887 | static void hostapd_wps_event_cb(void *ctx, enum wps_event event, |
| 888 | union wps_event_data *data) |
| 889 | { |
| 890 | struct hostapd_data *hapd = ctx; |
| 891 | |
| 892 | switch (event) { |
| 893 | case WPS_EV_M2D: |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 894 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 895 | break; |
| 896 | case WPS_EV_FAIL: |
| 897 | hostapd_wps_event_fail(hapd, &data->fail); |
| 898 | break; |
| 899 | case WPS_EV_SUCCESS: |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 900 | hostapd_wps_event_success(hapd, &data->success); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 901 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 902 | break; |
| 903 | case WPS_EV_PWD_AUTH_FAIL: |
| 904 | hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail); |
| 905 | break; |
| 906 | case WPS_EV_PBC_OVERLAP: |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 907 | hostapd_wps_event_pbc_overlap(hapd); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 908 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 909 | break; |
| 910 | case WPS_EV_PBC_TIMEOUT: |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 911 | hostapd_wps_event_pbc_timeout(hapd); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 912 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 913 | break; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 914 | case WPS_EV_PBC_ACTIVE: |
| 915 | hostapd_wps_event_pbc_active(hapd); |
| 916 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE); |
| 917 | break; |
| 918 | case WPS_EV_PBC_DISABLE: |
| 919 | hostapd_wps_event_pbc_disable(hapd); |
| 920 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE); |
| 921 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 922 | case WPS_EV_ER_AP_ADD: |
| 923 | break; |
| 924 | case WPS_EV_ER_AP_REMOVE: |
| 925 | break; |
| 926 | case WPS_EV_ER_ENROLLEE_ADD: |
| 927 | break; |
| 928 | case WPS_EV_ER_ENROLLEE_REMOVE: |
| 929 | break; |
| 930 | case WPS_EV_ER_AP_SETTINGS: |
| 931 | break; |
| 932 | case WPS_EV_ER_SET_SELECTED_REGISTRAR: |
| 933 | break; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 934 | case WPS_EV_AP_PIN_SUCCESS: |
| 935 | hostapd_wps_ap_pin_success(hapd); |
| 936 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 937 | } |
| 938 | if (hapd->wps_event_cb) |
| 939 | hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data); |
| 940 | } |
| 941 | |
| 942 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 943 | static int hostapd_wps_rf_band_cb(void *ctx) |
| 944 | { |
| 945 | struct hostapd_data *hapd = ctx; |
| 946 | |
| 947 | return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ? |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 948 | WPS_RF_50GHZ : |
| 949 | hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ? |
| 950 | WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */ |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 954 | static void hostapd_wps_clear_ies(struct hostapd_data *hapd, int deinit_only) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 955 | { |
| 956 | wpabuf_free(hapd->wps_beacon_ie); |
| 957 | hapd->wps_beacon_ie = NULL; |
| 958 | |
| 959 | wpabuf_free(hapd->wps_probe_resp_ie); |
| 960 | hapd->wps_probe_resp_ie = NULL; |
| 961 | |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 962 | if (deinit_only) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 963 | if (hapd->drv_priv) |
| 964 | hostapd_reset_ap_wps_ie(hapd); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 965 | return; |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 966 | } |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 967 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 968 | hostapd_set_ap_wps_ie(hapd); |
| 969 | } |
| 970 | |
| 971 | |
| 972 | static int get_uuid_cb(struct hostapd_iface *iface, void *ctx) |
| 973 | { |
| 974 | const u8 **uuid = ctx; |
| 975 | size_t j; |
| 976 | |
| 977 | if (iface == NULL) |
| 978 | return 0; |
| 979 | for (j = 0; j < iface->num_bss; j++) { |
| 980 | struct hostapd_data *hapd = iface->bss[j]; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 981 | if (hapd->wps && !hapd->conf->wps_independent && |
| 982 | !is_nil_uuid(hapd->wps->uuid)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 983 | *uuid = hapd->wps->uuid; |
| 984 | return 1; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | |
| 992 | static const u8 * get_own_uuid(struct hostapd_iface *iface) |
| 993 | { |
| 994 | const u8 *uuid; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 995 | if (iface->interfaces == NULL || |
| 996 | iface->interfaces->for_each_interface == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 997 | return NULL; |
| 998 | uuid = NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 999 | iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb, |
| 1000 | &uuid); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1001 | return uuid; |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | static int count_interface_cb(struct hostapd_iface *iface, void *ctx) |
| 1006 | { |
| 1007 | int *count= ctx; |
| 1008 | (*count)++; |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | static int interface_count(struct hostapd_iface *iface) |
| 1014 | { |
| 1015 | int count = 0; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1016 | if (iface->interfaces == NULL || |
| 1017 | iface->interfaces->for_each_interface == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1018 | return 0; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1019 | iface->interfaces->for_each_interface(iface->interfaces, |
| 1020 | count_interface_cb, &count); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1021 | return count; |
| 1022 | } |
| 1023 | |
| 1024 | |
| 1025 | static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd, |
| 1026 | struct wps_context *wps) |
| 1027 | { |
| 1028 | int i; |
| 1029 | |
| 1030 | for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) { |
| 1031 | wpabuf_free(wps->dev.vendor_ext[i]); |
| 1032 | wps->dev.vendor_ext[i] = NULL; |
| 1033 | |
| 1034 | if (hapd->conf->wps_vendor_ext[i] == NULL) |
| 1035 | continue; |
| 1036 | |
| 1037 | wps->dev.vendor_ext[i] = |
| 1038 | wpabuf_dup(hapd->conf->wps_vendor_ext[i]); |
| 1039 | if (wps->dev.vendor_ext[i] == NULL) { |
| 1040 | while (--i >= 0) |
| 1041 | wpabuf_free(wps->dev.vendor_ext[i]); |
| 1042 | return -1; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1050 | static int hostapd_wps_set_application_ext(struct hostapd_data *hapd, |
| 1051 | struct wps_context *wps) |
| 1052 | { |
| 1053 | wpabuf_free(wps->dev.application_ext); |
| 1054 | |
| 1055 | if (!hapd->conf->wps_application_ext) { |
| 1056 | wps->dev.application_ext = NULL; |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | wps->dev.application_ext = wpabuf_dup(hapd->conf->wps_application_ext); |
| 1061 | return wps->dev.application_ext ? 0 : -1; |
| 1062 | } |
| 1063 | |
| 1064 | |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1065 | static void hostapd_free_wps(struct wps_context *wps) |
| 1066 | { |
| 1067 | int i; |
| 1068 | |
| 1069 | for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) |
| 1070 | wpabuf_free(wps->dev.vendor_ext[i]); |
| 1071 | wps_device_data_free(&wps->dev); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1072 | bin_clear_free(wps->network_key, wps->network_key_len); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1073 | hostapd_wps_nfc_clear(wps); |
| 1074 | wpabuf_free(wps->dh_pubkey); |
| 1075 | wpabuf_free(wps->dh_privkey); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1076 | forced_memzero(wps->psk, sizeof(wps->psk)); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1077 | os_free(wps); |
| 1078 | } |
| 1079 | |
| 1080 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1081 | int hostapd_init_wps(struct hostapd_data *hapd, |
| 1082 | struct hostapd_bss_config *conf) |
| 1083 | { |
| 1084 | struct wps_context *wps; |
| 1085 | struct wps_registrar_config cfg; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1086 | u8 *multi_ap_netw_key = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1087 | |
| 1088 | if (conf->wps_state == 0) { |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1089 | hostapd_wps_clear_ies(hapd, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | wps = os_zalloc(sizeof(*wps)); |
| 1094 | if (wps == NULL) |
| 1095 | return -1; |
| 1096 | |
| 1097 | wps->cred_cb = hostapd_wps_cred_cb; |
| 1098 | wps->event_cb = hostapd_wps_event_cb; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 1099 | wps->rf_band_cb = hostapd_wps_rf_band_cb; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1100 | wps->cb_ctx = hapd; |
| 1101 | |
| 1102 | os_memset(&cfg, 0, sizeof(cfg)); |
| 1103 | wps->wps_state = hapd->conf->wps_state; |
| 1104 | wps->ap_setup_locked = hapd->conf->ap_setup_locked; |
| 1105 | if (is_nil_uuid(hapd->conf->uuid)) { |
| 1106 | const u8 *uuid; |
| 1107 | uuid = get_own_uuid(hapd->iface); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1108 | if (uuid && !conf->wps_independent) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1109 | os_memcpy(wps->uuid, uuid, UUID_LEN); |
| 1110 | wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another " |
| 1111 | "interface", wps->uuid, UUID_LEN); |
| 1112 | } else { |
| 1113 | uuid_gen_mac_addr(hapd->own_addr, wps->uuid); |
| 1114 | wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC " |
| 1115 | "address", wps->uuid, UUID_LEN); |
| 1116 | } |
| 1117 | } else { |
| 1118 | os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN); |
| 1119 | wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID", |
| 1120 | wps->uuid, UUID_LEN); |
| 1121 | } |
| 1122 | wps->ssid_len = hapd->conf->ssid.ssid_len; |
| 1123 | os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len); |
| 1124 | wps->ap = 1; |
| 1125 | os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN); |
| 1126 | wps->dev.device_name = hapd->conf->device_name ? |
| 1127 | os_strdup(hapd->conf->device_name) : NULL; |
| 1128 | wps->dev.manufacturer = hapd->conf->manufacturer ? |
| 1129 | os_strdup(hapd->conf->manufacturer) : NULL; |
| 1130 | wps->dev.model_name = hapd->conf->model_name ? |
| 1131 | os_strdup(hapd->conf->model_name) : NULL; |
| 1132 | wps->dev.model_number = hapd->conf->model_number ? |
| 1133 | os_strdup(hapd->conf->model_number) : NULL; |
| 1134 | wps->dev.serial_number = hapd->conf->serial_number ? |
| 1135 | os_strdup(hapd->conf->serial_number) : NULL; |
| 1136 | wps->config_methods = |
| 1137 | wps_config_methods_str2bin(hapd->conf->config_methods); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1138 | if ((wps->config_methods & |
| 1139 | (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY | |
| 1140 | WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) { |
| 1141 | wpa_printf(MSG_INFO, "WPS: Converting display to " |
| 1142 | "virtual_display for WPS 2.0 compliance"); |
| 1143 | wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY; |
| 1144 | } |
| 1145 | if ((wps->config_methods & |
| 1146 | (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON | |
| 1147 | WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) { |
| 1148 | wpa_printf(MSG_INFO, "WPS: Converting push_button to " |
| 1149 | "virtual_push_button for WPS 2.0 compliance"); |
| 1150 | wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON; |
| 1151 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1152 | os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type, |
| 1153 | WPS_DEV_TYPE_LEN); |
| 1154 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1155 | if (hostapd_wps_set_vendor_ext(hapd, wps) < 0 || |
| 1156 | hostapd_wps_set_application_ext(hapd, wps) < 0) |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1157 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1158 | |
| 1159 | wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1160 | |
| 1161 | if (conf->wps_rf_bands) { |
| 1162 | wps->dev.rf_bands = conf->wps_rf_bands; |
| 1163 | } else { |
| 1164 | wps->dev.rf_bands = |
| 1165 | hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ? |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 1166 | WPS_RF_50GHZ : |
| 1167 | hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ? |
| 1168 | WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1169 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1170 | |
| 1171 | if (conf->wpa & WPA_PROTO_RSN) { |
| 1172 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) |
| 1173 | wps->auth_types |= WPS_AUTH_WPA2PSK; |
| 1174 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) |
| 1175 | wps->auth_types |= WPS_AUTH_WPA2; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1176 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) |
| 1177 | wps->auth_types |= WPS_AUTH_WPA2PSK; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1178 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1179 | if (conf->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP | |
| 1180 | WPA_CIPHER_CCMP_256 | |
| 1181 | WPA_CIPHER_GCMP_256)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1182 | wps->encr_types |= WPS_ENCR_AES; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 1183 | wps->encr_types_rsn |= WPS_ENCR_AES; |
| 1184 | } |
| 1185 | if (conf->rsn_pairwise & WPA_CIPHER_TKIP) { |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1186 | #ifdef CONFIG_NO_TKIP |
| 1187 | wpa_printf(MSG_INFO, "WPS: TKIP not supported"); |
| 1188 | goto fail; |
| 1189 | #else /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1190 | wps->encr_types |= WPS_ENCR_TKIP; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 1191 | wps->encr_types_rsn |= WPS_ENCR_TKIP; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1192 | #endif /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 1193 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | if (conf->wpa & WPA_PROTO_WPA) { |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1197 | #ifdef CONFIG_NO_TKIP |
| 1198 | if (!(conf->wpa & WPA_PROTO_RSN)) { |
| 1199 | wpa_printf(MSG_INFO, "WPS: WPA(v1) not supported"); |
| 1200 | goto fail; |
| 1201 | } |
| 1202 | conf->wpa &= ~WPA_PROTO_WPA; |
| 1203 | #else /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1204 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) |
| 1205 | wps->auth_types |= WPS_AUTH_WPAPSK; |
| 1206 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) |
| 1207 | wps->auth_types |= WPS_AUTH_WPA; |
| 1208 | |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 1209 | if (conf->wpa_pairwise & WPA_CIPHER_CCMP) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1210 | wps->encr_types |= WPS_ENCR_AES; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 1211 | wps->encr_types_wpa |= WPS_ENCR_AES; |
| 1212 | } |
| 1213 | if (conf->wpa_pairwise & WPA_CIPHER_TKIP) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1214 | wps->encr_types |= WPS_ENCR_TKIP; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 1215 | wps->encr_types_wpa |= WPS_ENCR_TKIP; |
| 1216 | } |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1217 | #endif /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | if (conf->ssid.security_policy == SECURITY_PLAINTEXT) { |
| 1221 | wps->encr_types |= WPS_ENCR_NONE; |
| 1222 | wps->auth_types |= WPS_AUTH_OPEN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | if (conf->ssid.wpa_psk_file) { |
| 1226 | /* Use per-device PSKs */ |
| 1227 | } else if (conf->ssid.wpa_passphrase) { |
| 1228 | wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase); |
| 1229 | wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase); |
| 1230 | } else if (conf->ssid.wpa_psk) { |
| 1231 | wps->network_key = os_malloc(2 * PMK_LEN + 1); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1232 | if (wps->network_key == NULL) |
| 1233 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1234 | wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1, |
| 1235 | conf->ssid.wpa_psk->psk, PMK_LEN); |
| 1236 | wps->network_key_len = 2 * PMK_LEN; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1237 | #ifdef CONFIG_WEP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1238 | } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) { |
| 1239 | wps->network_key = os_malloc(conf->ssid.wep.len[0]); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1240 | if (wps->network_key == NULL) |
| 1241 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1242 | os_memcpy(wps->network_key, conf->ssid.wep.key[0], |
| 1243 | conf->ssid.wep.len[0]); |
| 1244 | wps->network_key_len = conf->ssid.wep.len[0]; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1245 | #endif /* CONFIG_WEP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | if (conf->ssid.wpa_psk) { |
| 1249 | os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN); |
| 1250 | wps->psk_set = 1; |
| 1251 | } |
| 1252 | |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 1253 | wps->ap_auth_type = wps->auth_types; |
| 1254 | wps->ap_encr_type = wps->encr_types; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1255 | if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) { |
| 1256 | /* Override parameters to enable security by default */ |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1257 | #ifdef CONFIG_NO_TKIP |
| 1258 | wps->auth_types = WPS_AUTH_WPA2PSK; |
| 1259 | wps->encr_types = WPS_ENCR_AES; |
| 1260 | wps->encr_types_rsn = WPS_ENCR_AES; |
| 1261 | wps->encr_types_wpa = WPS_ENCR_AES; |
| 1262 | #else /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1263 | wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK; |
| 1264 | wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 1265 | wps->encr_types_rsn = WPS_ENCR_AES | WPS_ENCR_TKIP; |
| 1266 | wps->encr_types_wpa = WPS_ENCR_AES | WPS_ENCR_TKIP; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1267 | #endif /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1270 | if ((hapd->conf->multi_ap & FRONTHAUL_BSS) && |
| 1271 | hapd->conf->multi_ap_backhaul_ssid.ssid_len) { |
| 1272 | cfg.multi_ap_backhaul_ssid_len = |
| 1273 | hapd->conf->multi_ap_backhaul_ssid.ssid_len; |
| 1274 | cfg.multi_ap_backhaul_ssid = |
| 1275 | hapd->conf->multi_ap_backhaul_ssid.ssid; |
| 1276 | |
| 1277 | if (conf->multi_ap_backhaul_ssid.wpa_passphrase) { |
| 1278 | cfg.multi_ap_backhaul_network_key = (const u8 *) |
| 1279 | conf->multi_ap_backhaul_ssid.wpa_passphrase; |
| 1280 | cfg.multi_ap_backhaul_network_key_len = |
| 1281 | os_strlen(conf->multi_ap_backhaul_ssid.wpa_passphrase); |
| 1282 | } else if (conf->multi_ap_backhaul_ssid.wpa_psk) { |
| 1283 | multi_ap_netw_key = os_malloc(2 * PMK_LEN + 1); |
| 1284 | if (!multi_ap_netw_key) |
| 1285 | goto fail; |
| 1286 | wpa_snprintf_hex((char *) multi_ap_netw_key, |
| 1287 | 2 * PMK_LEN + 1, |
| 1288 | conf->multi_ap_backhaul_ssid.wpa_psk->psk, |
| 1289 | PMK_LEN); |
| 1290 | cfg.multi_ap_backhaul_network_key = multi_ap_netw_key; |
| 1291 | cfg.multi_ap_backhaul_network_key_len = 2 * PMK_LEN; |
| 1292 | } |
| 1293 | } |
| 1294 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1295 | wps->ap_settings = conf->ap_settings; |
| 1296 | wps->ap_settings_len = conf->ap_settings_len; |
| 1297 | |
| 1298 | cfg.new_psk_cb = hostapd_wps_new_psk_cb; |
| 1299 | cfg.set_ie_cb = hostapd_wps_set_ie_cb; |
| 1300 | cfg.pin_needed_cb = hostapd_wps_pin_needed_cb; |
| 1301 | cfg.reg_success_cb = hostapd_wps_reg_success_cb; |
| 1302 | cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1303 | cfg.lookup_pskfile_cb = hostapd_wps_lookup_pskfile_cb; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1304 | cfg.cb_ctx = hapd; |
| 1305 | cfg.skip_cred_build = conf->skip_cred_build; |
| 1306 | cfg.extra_cred = conf->extra_cred; |
| 1307 | cfg.extra_cred_len = conf->extra_cred_len; |
| 1308 | cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) && |
| 1309 | conf->skip_cred_build; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1310 | cfg.dualband = interface_count(hapd->iface) > 1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1311 | if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) == |
| 1312 | (WPS_RF_50GHZ | WPS_RF_24GHZ)) |
| 1313 | cfg.dualband = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1314 | if (cfg.dualband) |
| 1315 | wpa_printf(MSG_DEBUG, "WPS: Dualband AP"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1316 | cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1317 | |
| 1318 | wps->registrar = wps_registrar_init(wps, &cfg); |
| 1319 | if (wps->registrar == NULL) { |
| 1320 | wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar"); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1321 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | #ifdef CONFIG_WPS_UPNP |
| 1325 | wps->friendly_name = hapd->conf->friendly_name; |
| 1326 | wps->manufacturer_url = hapd->conf->manufacturer_url; |
| 1327 | wps->model_description = hapd->conf->model_description; |
| 1328 | wps->model_url = hapd->conf->model_url; |
| 1329 | wps->upc = hapd->conf->upc; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1330 | #endif /* CONFIG_WPS_UPNP */ |
| 1331 | |
| 1332 | hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd); |
| 1333 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1334 | #ifdef CONFIG_P2P |
| 1335 | if ((hapd->conf->p2p & P2P_ENABLED) && |
| 1336 | is_6ghz_op_class(hapd->iconf->op_class)) |
| 1337 | wps->use_passphrase = true; |
| 1338 | #endif /* CONFIG_P2P */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1339 | hapd->wps = wps; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1340 | bin_clear_free(multi_ap_netw_key, 2 * PMK_LEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1341 | |
| 1342 | return 0; |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1343 | |
| 1344 | fail: |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1345 | bin_clear_free(multi_ap_netw_key, 2 * PMK_LEN); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1346 | hostapd_free_wps(wps); |
| 1347 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | |
Jouni Malinen | 87fd279 | 2011-05-16 18:35:42 +0300 | [diff] [blame] | 1351 | int hostapd_init_wps_complete(struct hostapd_data *hapd) |
| 1352 | { |
| 1353 | struct wps_context *wps = hapd->wps; |
| 1354 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1355 | if (wps == NULL) |
Jouni Malinen | 87fd279 | 2011-05-16 18:35:42 +0300 | [diff] [blame] | 1356 | return 0; |
| 1357 | |
| 1358 | #ifdef CONFIG_WPS_UPNP |
| 1359 | if (hostapd_wps_upnp_init(hapd, wps) < 0) { |
| 1360 | wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP"); |
| 1361 | wps_registrar_deinit(wps->registrar); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1362 | hostapd_free_wps(wps); |
Jouni Malinen | 87fd279 | 2011-05-16 18:35:42 +0300 | [diff] [blame] | 1363 | hapd->wps = NULL; |
| 1364 | return -1; |
| 1365 | } |
| 1366 | #endif /* CONFIG_WPS_UPNP */ |
| 1367 | |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1372 | static void hostapd_wps_nfc_clear(struct wps_context *wps) |
| 1373 | { |
| 1374 | #ifdef CONFIG_WPS_NFC |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 1375 | wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1376 | wps->ap_nfc_dev_pw_id = 0; |
| 1377 | wpabuf_free(wps->ap_nfc_dh_pubkey); |
| 1378 | wps->ap_nfc_dh_pubkey = NULL; |
| 1379 | wpabuf_free(wps->ap_nfc_dh_privkey); |
| 1380 | wps->ap_nfc_dh_privkey = NULL; |
| 1381 | wpabuf_free(wps->ap_nfc_dev_pw); |
| 1382 | wps->ap_nfc_dev_pw = NULL; |
| 1383 | #endif /* CONFIG_WPS_NFC */ |
| 1384 | } |
| 1385 | |
| 1386 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1387 | static int hostapd_wps_update_multi_ap(struct hostapd_data *hapd, |
| 1388 | struct wps_registrar *reg) |
| 1389 | { |
| 1390 | struct hostapd_bss_config *conf = hapd->conf; |
| 1391 | u8 *multi_ap_backhaul_network_key = NULL; |
| 1392 | size_t multi_ap_backhaul_network_key_len = 0; |
| 1393 | int ret; |
| 1394 | |
| 1395 | if (!(conf->multi_ap & FRONTHAUL_BSS) || |
| 1396 | !conf->multi_ap_backhaul_ssid.ssid_len) |
| 1397 | return 0; |
| 1398 | |
| 1399 | if (conf->multi_ap_backhaul_ssid.wpa_passphrase) { |
| 1400 | multi_ap_backhaul_network_key = |
| 1401 | (u8 *) os_strdup( |
| 1402 | conf->multi_ap_backhaul_ssid.wpa_passphrase); |
| 1403 | if (!multi_ap_backhaul_network_key) |
| 1404 | return -1; |
| 1405 | multi_ap_backhaul_network_key_len = |
| 1406 | os_strlen(conf->multi_ap_backhaul_ssid.wpa_passphrase); |
| 1407 | } else if (conf->multi_ap_backhaul_ssid.wpa_psk) { |
| 1408 | multi_ap_backhaul_network_key = os_malloc(2 * PMK_LEN + 1); |
| 1409 | if (!multi_ap_backhaul_network_key) |
| 1410 | return -1; |
| 1411 | wpa_snprintf_hex((char *) multi_ap_backhaul_network_key, |
| 1412 | 2 * PMK_LEN + 1, |
| 1413 | conf->multi_ap_backhaul_ssid.wpa_psk->psk, |
| 1414 | PMK_LEN); |
| 1415 | multi_ap_backhaul_network_key_len = 2 * PMK_LEN; |
| 1416 | } |
| 1417 | |
| 1418 | ret = wps_registrar_update_multi_ap( |
| 1419 | reg, conf->multi_ap_backhaul_ssid.ssid, |
| 1420 | conf->multi_ap_backhaul_ssid.ssid_len, |
| 1421 | multi_ap_backhaul_network_key, |
| 1422 | multi_ap_backhaul_network_key_len); |
| 1423 | os_free(multi_ap_backhaul_network_key); |
| 1424 | |
| 1425 | return ret; |
| 1426 | } |
| 1427 | |
| 1428 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1429 | void hostapd_deinit_wps(struct hostapd_data *hapd) |
| 1430 | { |
| 1431 | eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); |
| 1432 | eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1433 | eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1434 | if (hapd->wps == NULL) { |
| 1435 | hostapd_wps_clear_ies(hapd, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1436 | return; |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1437 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1438 | #ifdef CONFIG_WPS_UPNP |
| 1439 | hostapd_wps_upnp_deinit(hapd); |
| 1440 | #endif /* CONFIG_WPS_UPNP */ |
| 1441 | wps_registrar_deinit(hapd->wps->registrar); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1442 | wps_free_pending_msgs(hapd->wps->upnp_msgs); |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1443 | hostapd_free_wps(hapd->wps); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1444 | hapd->wps = NULL; |
Dmitry Shmidt | 6cb1f65 | 2014-03-21 10:54:03 -0700 | [diff] [blame] | 1445 | hostapd_wps_clear_ies(hapd, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | |
| 1449 | void hostapd_update_wps(struct hostapd_data *hapd) |
| 1450 | { |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1451 | struct wps_context *wps = hapd->wps; |
| 1452 | struct hostapd_bss_config *conf = hapd->conf; |
| 1453 | |
| 1454 | if (!wps) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1455 | return; |
| 1456 | |
| 1457 | #ifdef CONFIG_WPS_UPNP |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1458 | wps->friendly_name = conf->friendly_name; |
| 1459 | wps->manufacturer_url = conf->manufacturer_url; |
| 1460 | wps->model_description = conf->model_description; |
| 1461 | wps->model_url = conf->model_url; |
| 1462 | wps->upc = conf->upc; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1463 | #endif /* CONFIG_WPS_UPNP */ |
| 1464 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1465 | os_memcpy(wps->ssid, conf->ssid.ssid, conf->ssid.ssid_len); |
| 1466 | wps->ssid_len = conf->ssid.ssid_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1467 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1468 | /* Clear WPS settings, then fill them again */ |
| 1469 | os_free(wps->network_key); |
| 1470 | wps->network_key = NULL; |
| 1471 | wps->network_key_len = 0; |
| 1472 | wps->psk_set = 0; |
| 1473 | if (conf->ssid.wpa_psk_file) { |
| 1474 | /* Use per-device PSKs */ |
| 1475 | } else if (conf->ssid.wpa_passphrase) { |
| 1476 | wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase); |
| 1477 | if (!wps->network_key) |
| 1478 | return; |
| 1479 | wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase); |
| 1480 | } else if (conf->ssid.wpa_psk) { |
| 1481 | wps->network_key = os_malloc(2 * PMK_LEN + 1); |
| 1482 | if (!wps->network_key) |
| 1483 | return; |
| 1484 | wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1, |
| 1485 | conf->ssid.wpa_psk->psk, PMK_LEN); |
| 1486 | wps->network_key_len = 2 * PMK_LEN; |
| 1487 | #ifdef CONFIG_WEP |
| 1488 | } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) { |
| 1489 | wps->network_key = os_malloc(conf->ssid.wep.len[0]); |
| 1490 | if (!wps->network_key) |
| 1491 | return; |
| 1492 | os_memcpy(wps->network_key, conf->ssid.wep.key[0], |
| 1493 | conf->ssid.wep.len[0]); |
| 1494 | wps->network_key_len = conf->ssid.wep.len[0]; |
| 1495 | #endif /* CONFIG_WEP */ |
| 1496 | } |
| 1497 | |
| 1498 | if (conf->ssid.wpa_psk) { |
| 1499 | os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN); |
| 1500 | wps->psk_set = 1; |
| 1501 | } |
| 1502 | |
| 1503 | hostapd_wps_update_multi_ap(hapd, wps->registrar); |
| 1504 | |
| 1505 | hostapd_wps_set_vendor_ext(hapd, wps); |
| 1506 | hostapd_wps_set_application_ext(hapd, wps); |
| 1507 | |
| 1508 | if (conf->wps_state) |
| 1509 | wps_registrar_update_ie(wps->registrar); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1510 | else |
| 1511 | hostapd_deinit_wps(hapd); |
| 1512 | } |
| 1513 | |
| 1514 | |
| 1515 | struct wps_add_pin_data { |
| 1516 | const u8 *addr; |
| 1517 | const u8 *uuid; |
| 1518 | const u8 *pin; |
| 1519 | size_t pin_len; |
| 1520 | int timeout; |
| 1521 | int added; |
| 1522 | }; |
| 1523 | |
| 1524 | |
| 1525 | static int wps_add_pin(struct hostapd_data *hapd, void *ctx) |
| 1526 | { |
| 1527 | struct wps_add_pin_data *data = ctx; |
| 1528 | int ret; |
| 1529 | |
| 1530 | if (hapd->wps == NULL) |
| 1531 | return 0; |
| 1532 | ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr, |
| 1533 | data->uuid, data->pin, data->pin_len, |
| 1534 | data->timeout); |
| 1535 | if (ret == 0) |
| 1536 | data->added++; |
| 1537 | return ret; |
| 1538 | } |
| 1539 | |
| 1540 | |
| 1541 | int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr, |
| 1542 | const char *uuid, const char *pin, int timeout) |
| 1543 | { |
| 1544 | u8 u[UUID_LEN]; |
| 1545 | struct wps_add_pin_data data; |
| 1546 | |
| 1547 | data.addr = addr; |
| 1548 | data.uuid = u; |
| 1549 | data.pin = (const u8 *) pin; |
| 1550 | data.pin_len = os_strlen(pin); |
| 1551 | data.timeout = timeout; |
| 1552 | data.added = 0; |
| 1553 | |
| 1554 | if (os_strcmp(uuid, "any") == 0) |
| 1555 | data.uuid = NULL; |
| 1556 | else { |
| 1557 | if (uuid_str2bin(uuid, u)) |
| 1558 | return -1; |
| 1559 | data.uuid = u; |
| 1560 | } |
| 1561 | if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0) |
| 1562 | return -1; |
| 1563 | return data.added ? 0 : -1; |
| 1564 | } |
| 1565 | |
| 1566 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1567 | struct wps_button_pushed_ctx { |
| 1568 | const u8 *p2p_dev_addr; |
| 1569 | unsigned int count; |
| 1570 | }; |
| 1571 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1572 | static int wps_button_pushed(struct hostapd_data *hapd, void *ctx) |
| 1573 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1574 | struct wps_button_pushed_ctx *data = ctx; |
| 1575 | |
| 1576 | if (hapd->wps) { |
| 1577 | data->count++; |
| 1578 | return wps_registrar_button_pushed(hapd->wps->registrar, |
| 1579 | data->p2p_dev_addr); |
| 1580 | } |
| 1581 | |
| 1582 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | |
| 1586 | int hostapd_wps_button_pushed(struct hostapd_data *hapd, |
| 1587 | const u8 *p2p_dev_addr) |
| 1588 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1589 | struct wps_button_pushed_ctx ctx; |
| 1590 | int ret; |
| 1591 | |
| 1592 | os_memset(&ctx, 0, sizeof(ctx)); |
| 1593 | ctx.p2p_dev_addr = p2p_dev_addr; |
| 1594 | ret = hostapd_wps_for_each(hapd, wps_button_pushed, &ctx); |
| 1595 | if (ret == 0 && !ctx.count) |
| 1596 | ret = -1; |
| 1597 | return ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1601 | struct wps_cancel_ctx { |
| 1602 | unsigned int count; |
| 1603 | }; |
| 1604 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1605 | static int wps_cancel(struct hostapd_data *hapd, void *ctx) |
| 1606 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1607 | struct wps_cancel_ctx *data = ctx; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1608 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1609 | if (hapd->wps) { |
| 1610 | data->count++; |
| 1611 | wps_registrar_wps_cancel(hapd->wps->registrar); |
| 1612 | ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL); |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1613 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_CANCEL); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1614 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1615 | |
| 1616 | return 0; |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | int hostapd_wps_cancel(struct hostapd_data *hapd) |
| 1621 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1622 | struct wps_cancel_ctx ctx; |
| 1623 | int ret; |
| 1624 | |
| 1625 | os_memset(&ctx, 0, sizeof(ctx)); |
| 1626 | ret = hostapd_wps_for_each(hapd, wps_cancel, &ctx); |
| 1627 | if (ret == 0 && !ctx.count) |
| 1628 | ret = -1; |
| 1629 | return ret; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1633 | static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da, |
| 1634 | const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1635 | const u8 *ie, size_t ie_len, |
| 1636 | int ssi_signal) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1637 | { |
| 1638 | struct hostapd_data *hapd = ctx; |
| 1639 | struct wpabuf *wps_ie; |
| 1640 | struct ieee802_11_elems elems; |
| 1641 | |
| 1642 | if (hapd->wps == NULL) |
| 1643 | return 0; |
| 1644 | |
| 1645 | if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) { |
| 1646 | wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from " |
| 1647 | MACSTR, MAC2STR(addr)); |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
| 1651 | if (elems.ssid && elems.ssid_len > 0 && |
| 1652 | (elems.ssid_len != hapd->conf->ssid.ssid_len || |
| 1653 | os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != |
| 1654 | 0)) |
| 1655 | return 0; /* Not for us */ |
| 1656 | |
| 1657 | wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA); |
| 1658 | if (wps_ie == NULL) |
| 1659 | return 0; |
| 1660 | if (wps_validate_probe_req(wps_ie, addr) < 0) { |
| 1661 | wpabuf_free(wps_ie); |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | if (wpabuf_len(wps_ie) > 0) { |
| 1666 | int p2p_wildcard = 0; |
| 1667 | #ifdef CONFIG_P2P |
| 1668 | if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN && |
| 1669 | os_memcmp(elems.ssid, P2P_WILDCARD_SSID, |
| 1670 | P2P_WILDCARD_SSID_LEN) == 0) |
| 1671 | p2p_wildcard = 1; |
| 1672 | #endif /* CONFIG_P2P */ |
| 1673 | wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie, |
| 1674 | p2p_wildcard); |
| 1675 | #ifdef CONFIG_WPS_UPNP |
| 1676 | /* FIX: what exactly should be included in the WLANEvent? |
| 1677 | * WPS attributes? Full ProbeReq frame? */ |
| 1678 | if (!p2p_wildcard) |
| 1679 | upnp_wps_device_send_wlan_event( |
| 1680 | hapd->wps_upnp, addr, |
| 1681 | UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie); |
| 1682 | #endif /* CONFIG_WPS_UPNP */ |
| 1683 | } |
| 1684 | |
| 1685 | wpabuf_free(wps_ie); |
| 1686 | |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | |
| 1691 | #ifdef CONFIG_WPS_UPNP |
| 1692 | |
| 1693 | static int hostapd_rx_req_put_wlan_response( |
| 1694 | void *priv, enum upnp_wps_wlanevent_type ev_type, |
| 1695 | const u8 *mac_addr, const struct wpabuf *msg, |
| 1696 | enum wps_msg_type msg_type) |
| 1697 | { |
| 1698 | struct hostapd_data *hapd = priv; |
| 1699 | struct sta_info *sta; |
| 1700 | struct upnp_pending_message *p; |
| 1701 | |
| 1702 | wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr=" |
| 1703 | MACSTR, ev_type, MAC2STR(mac_addr)); |
| 1704 | wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage", |
| 1705 | wpabuf_head(msg), wpabuf_len(msg)); |
| 1706 | if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) { |
| 1707 | wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected " |
| 1708 | "PutWLANResponse WLANEventType %d", ev_type); |
| 1709 | return -1; |
| 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | * EAP response to ongoing to WPS Registration. Send it to EAP-WSC |
| 1714 | * server implementation for delivery to the peer. |
| 1715 | */ |
| 1716 | |
| 1717 | sta = ap_get_sta(hapd, mac_addr); |
| 1718 | #ifndef CONFIG_WPS_STRICT |
| 1719 | if (!sta) { |
| 1720 | /* |
| 1721 | * Workaround - Intel wsccmd uses bogus NewWLANEventMAC: |
| 1722 | * Pick STA that is in an ongoing WPS registration without |
| 1723 | * checking the MAC address. |
| 1724 | */ |
| 1725 | wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based " |
| 1726 | "on NewWLANEventMAC; try wildcard match"); |
| 1727 | for (sta = hapd->sta_list; sta; sta = sta->next) { |
| 1728 | if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS)) |
| 1729 | break; |
| 1730 | } |
| 1731 | } |
| 1732 | #endif /* CONFIG_WPS_STRICT */ |
| 1733 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1734 | if (!sta || !(sta->flags & WLAN_STA_WPS)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1735 | wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found"); |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
Dmitry Shmidt | 13ca8d8 | 2014-02-20 10:18:40 -0800 | [diff] [blame] | 1739 | if (!sta->eapol_sm) { |
| 1740 | /* |
| 1741 | * This can happen, e.g., if an ER sends an extra message after |
| 1742 | * the station has disassociated (but not fully |
| 1743 | * deauthenticated). |
| 1744 | */ |
| 1745 | wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized"); |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1749 | p = os_zalloc(sizeof(*p)); |
| 1750 | if (p == NULL) |
| 1751 | return -1; |
| 1752 | os_memcpy(p->addr, sta->addr, ETH_ALEN); |
| 1753 | p->msg = wpabuf_dup(msg); |
| 1754 | p->type = msg_type; |
| 1755 | p->next = hapd->wps->upnp_msgs; |
| 1756 | hapd->wps->upnp_msgs = p; |
| 1757 | |
| 1758 | return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap); |
| 1759 | } |
| 1760 | |
| 1761 | |
| 1762 | static int hostapd_wps_upnp_init(struct hostapd_data *hapd, |
| 1763 | struct wps_context *wps) |
| 1764 | { |
| 1765 | struct upnp_wps_device_ctx *ctx; |
| 1766 | |
| 1767 | if (!hapd->conf->upnp_iface) |
| 1768 | return 0; |
| 1769 | ctx = os_zalloc(sizeof(*ctx)); |
| 1770 | if (ctx == NULL) |
| 1771 | return -1; |
| 1772 | |
| 1773 | ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response; |
| 1774 | if (hapd->conf->ap_pin) |
| 1775 | ctx->ap_pin = os_strdup(hapd->conf->ap_pin); |
| 1776 | |
| 1777 | hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd, |
| 1778 | hapd->conf->upnp_iface); |
| 1779 | if (hapd->wps_upnp == NULL) |
| 1780 | return -1; |
| 1781 | wps->wps_upnp = hapd->wps_upnp; |
| 1782 | |
| 1783 | return 0; |
| 1784 | } |
| 1785 | |
| 1786 | |
| 1787 | static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd) |
| 1788 | { |
| 1789 | upnp_wps_device_deinit(hapd->wps_upnp, hapd); |
| 1790 | } |
| 1791 | |
| 1792 | #endif /* CONFIG_WPS_UPNP */ |
| 1793 | |
| 1794 | |
| 1795 | int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr, |
| 1796 | char *buf, size_t buflen) |
| 1797 | { |
| 1798 | if (hapd->wps == NULL) |
| 1799 | return 0; |
| 1800 | return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen); |
| 1801 | } |
| 1802 | |
| 1803 | |
| 1804 | static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx) |
| 1805 | { |
| 1806 | struct hostapd_data *hapd = eloop_data; |
| 1807 | wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out"); |
| 1808 | hostapd_wps_ap_pin_disable(hapd); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1809 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | |
| 1813 | static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout) |
| 1814 | { |
| 1815 | wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout); |
| 1816 | hapd->ap_pin_failures = 0; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1817 | hapd->ap_pin_failures_consecutive = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1818 | hapd->conf->ap_setup_locked = 0; |
| 1819 | if (hapd->wps->ap_setup_locked) { |
| 1820 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED); |
| 1821 | hapd->wps->ap_setup_locked = 0; |
| 1822 | wps_registrar_update_ie(hapd->wps->registrar); |
| 1823 | } |
| 1824 | eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); |
| 1825 | if (timeout > 0) |
| 1826 | eloop_register_timeout(timeout, 0, |
| 1827 | hostapd_wps_ap_pin_timeout, hapd, NULL); |
| 1828 | } |
| 1829 | |
| 1830 | |
| 1831 | static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx) |
| 1832 | { |
| 1833 | os_free(hapd->conf->ap_pin); |
| 1834 | hapd->conf->ap_pin = NULL; |
| 1835 | #ifdef CONFIG_WPS_UPNP |
| 1836 | upnp_wps_set_ap_pin(hapd->wps_upnp, NULL); |
| 1837 | #endif /* CONFIG_WPS_UPNP */ |
| 1838 | eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); |
| 1839 | return 0; |
| 1840 | } |
| 1841 | |
| 1842 | |
| 1843 | void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd) |
| 1844 | { |
| 1845 | wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN"); |
| 1846 | hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL); |
| 1847 | } |
| 1848 | |
| 1849 | |
| 1850 | struct wps_ap_pin_data { |
| 1851 | char pin_txt[9]; |
| 1852 | int timeout; |
| 1853 | }; |
| 1854 | |
| 1855 | |
| 1856 | static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx) |
| 1857 | { |
| 1858 | struct wps_ap_pin_data *data = ctx; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1859 | |
| 1860 | if (!hapd->wps) |
| 1861 | return 0; |
| 1862 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1863 | os_free(hapd->conf->ap_pin); |
| 1864 | hapd->conf->ap_pin = os_strdup(data->pin_txt); |
| 1865 | #ifdef CONFIG_WPS_UPNP |
| 1866 | upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt); |
| 1867 | #endif /* CONFIG_WPS_UPNP */ |
| 1868 | hostapd_wps_ap_pin_enable(hapd, data->timeout); |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
| 1872 | |
| 1873 | const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout) |
| 1874 | { |
| 1875 | unsigned int pin; |
| 1876 | struct wps_ap_pin_data data; |
| 1877 | |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1878 | if (wps_generate_pin(&pin) < 0) |
| 1879 | return NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1880 | os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1881 | data.timeout = timeout; |
| 1882 | hostapd_wps_for_each(hapd, wps_ap_pin_set, &data); |
| 1883 | return hapd->conf->ap_pin; |
| 1884 | } |
| 1885 | |
| 1886 | |
| 1887 | const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd) |
| 1888 | { |
| 1889 | return hapd->conf->ap_pin; |
| 1890 | } |
| 1891 | |
| 1892 | |
| 1893 | int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin, |
| 1894 | int timeout) |
| 1895 | { |
| 1896 | struct wps_ap_pin_data data; |
| 1897 | int ret; |
| 1898 | |
| 1899 | ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1900 | if (os_snprintf_error(sizeof(data.pin_txt), ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1901 | return -1; |
| 1902 | data.timeout = timeout; |
| 1903 | return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data); |
| 1904 | } |
| 1905 | |
| 1906 | |
| 1907 | static int wps_update_ie(struct hostapd_data *hapd, void *ctx) |
| 1908 | { |
| 1909 | if (hapd->wps) |
| 1910 | wps_registrar_update_ie(hapd->wps->registrar); |
| 1911 | return 0; |
| 1912 | } |
| 1913 | |
| 1914 | |
| 1915 | void hostapd_wps_update_ie(struct hostapd_data *hapd) |
| 1916 | { |
| 1917 | hostapd_wps_for_each(hapd, wps_update_ie, NULL); |
| 1918 | } |
| 1919 | |
| 1920 | |
| 1921 | int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid, |
| 1922 | const char *auth, const char *encr, const char *key) |
| 1923 | { |
| 1924 | struct wps_credential cred; |
| 1925 | size_t len; |
| 1926 | |
| 1927 | os_memset(&cred, 0, sizeof(cred)); |
| 1928 | |
| 1929 | len = os_strlen(ssid); |
| 1930 | if ((len & 1) || len > 2 * sizeof(cred.ssid) || |
| 1931 | hexstr2bin(ssid, cred.ssid, len / 2)) |
| 1932 | return -1; |
| 1933 | cred.ssid_len = len / 2; |
| 1934 | |
| 1935 | if (os_strncmp(auth, "OPEN", 4) == 0) |
| 1936 | cred.auth_type = WPS_AUTH_OPEN; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1937 | #ifndef CONFIG_NO_TKIP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1938 | else if (os_strncmp(auth, "WPAPSK", 6) == 0) |
| 1939 | cred.auth_type = WPS_AUTH_WPAPSK; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1940 | #endif /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1941 | else if (os_strncmp(auth, "WPA2PSK", 7) == 0) |
| 1942 | cred.auth_type = WPS_AUTH_WPA2PSK; |
| 1943 | else |
| 1944 | return -1; |
| 1945 | |
| 1946 | if (encr) { |
| 1947 | if (os_strncmp(encr, "NONE", 4) == 0) |
| 1948 | cred.encr_type = WPS_ENCR_NONE; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1949 | #ifndef CONFIG_NO_TKIP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1950 | else if (os_strncmp(encr, "TKIP", 4) == 0) |
| 1951 | cred.encr_type = WPS_ENCR_TKIP; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 1952 | #endif /* CONFIG_NO_TKIP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1953 | else if (os_strncmp(encr, "CCMP", 4) == 0) |
| 1954 | cred.encr_type = WPS_ENCR_AES; |
| 1955 | else |
| 1956 | return -1; |
| 1957 | } else |
| 1958 | cred.encr_type = WPS_ENCR_NONE; |
| 1959 | |
| 1960 | if (key) { |
| 1961 | len = os_strlen(key); |
| 1962 | if ((len & 1) || len > 2 * sizeof(cred.key) || |
| 1963 | hexstr2bin(key, cred.key, len / 2)) |
| 1964 | return -1; |
| 1965 | cred.key_len = len / 2; |
| 1966 | } |
| 1967 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1968 | if (!hapd->wps) { |
| 1969 | wpa_printf(MSG_ERROR, "WPS: WPS config does not exist"); |
| 1970 | return -1; |
| 1971 | } |
| 1972 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1973 | return wps_registrar_config_ap(hapd->wps->registrar, &cred); |
| 1974 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1975 | |
| 1976 | |
| 1977 | #ifdef CONFIG_WPS_NFC |
| 1978 | |
| 1979 | struct wps_nfc_password_token_data { |
| 1980 | const u8 *oob_dev_pw; |
| 1981 | size_t oob_dev_pw_len; |
| 1982 | int added; |
| 1983 | }; |
| 1984 | |
| 1985 | |
| 1986 | static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx) |
| 1987 | { |
| 1988 | struct wps_nfc_password_token_data *data = ctx; |
| 1989 | int ret; |
| 1990 | |
| 1991 | if (hapd->wps == NULL) |
| 1992 | return 0; |
| 1993 | ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar, |
| 1994 | data->oob_dev_pw, |
| 1995 | data->oob_dev_pw_len); |
| 1996 | if (ret == 0) |
| 1997 | data->added++; |
| 1998 | return ret; |
| 1999 | } |
| 2000 | |
| 2001 | |
| 2002 | static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd, |
| 2003 | struct wps_parse_attr *attr) |
| 2004 | { |
| 2005 | struct wps_nfc_password_token_data data; |
| 2006 | |
| 2007 | data.oob_dev_pw = attr->oob_dev_password; |
| 2008 | data.oob_dev_pw_len = attr->oob_dev_password_len; |
| 2009 | data.added = 0; |
| 2010 | if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0) |
| 2011 | return -1; |
| 2012 | return data.added ? 0 : -1; |
| 2013 | } |
| 2014 | |
| 2015 | |
| 2016 | static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd, |
| 2017 | const struct wpabuf *wps) |
| 2018 | { |
| 2019 | struct wps_parse_attr attr; |
| 2020 | |
| 2021 | wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps); |
| 2022 | |
| 2023 | if (wps_parse_msg(wps, &attr)) { |
| 2024 | wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag"); |
| 2025 | return -1; |
| 2026 | } |
| 2027 | |
| 2028 | if (attr.oob_dev_password) |
| 2029 | return hostapd_wps_add_nfc_password_token(hapd, &attr); |
| 2030 | |
| 2031 | wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag"); |
| 2032 | return -1; |
| 2033 | } |
| 2034 | |
| 2035 | |
| 2036 | int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd, |
| 2037 | const struct wpabuf *data) |
| 2038 | { |
| 2039 | const struct wpabuf *wps = data; |
| 2040 | struct wpabuf *tmp = NULL; |
| 2041 | int ret; |
| 2042 | |
| 2043 | if (wpabuf_len(data) < 4) |
| 2044 | return -1; |
| 2045 | |
| 2046 | if (*wpabuf_head_u8(data) != 0x10) { |
| 2047 | /* Assume this contains full NDEF record */ |
| 2048 | tmp = ndef_parse_wifi(data); |
| 2049 | if (tmp == NULL) { |
| 2050 | wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF"); |
| 2051 | return -1; |
| 2052 | } |
| 2053 | wps = tmp; |
| 2054 | } |
| 2055 | |
| 2056 | ret = hostapd_wps_nfc_tag_process(hapd, wps); |
| 2057 | wpabuf_free(tmp); |
| 2058 | return ret; |
| 2059 | } |
| 2060 | |
| 2061 | |
| 2062 | struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd, |
| 2063 | int ndef) |
| 2064 | { |
| 2065 | struct wpabuf *ret; |
| 2066 | |
| 2067 | if (hapd->wps == NULL) |
| 2068 | return NULL; |
| 2069 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2070 | ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd), |
| 2071 | hapd->iconf->channel); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2072 | if (ndef && ret) { |
| 2073 | struct wpabuf *tmp; |
| 2074 | tmp = ndef_build_wifi(ret); |
| 2075 | wpabuf_free(ret); |
| 2076 | if (tmp == NULL) |
| 2077 | return NULL; |
| 2078 | ret = tmp; |
| 2079 | } |
| 2080 | |
| 2081 | return ret; |
| 2082 | } |
| 2083 | |
| 2084 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 2085 | struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef) |
| 2086 | { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2087 | struct wpabuf *ret; |
| 2088 | |
| 2089 | if (hapd->wps == NULL) |
| 2090 | return NULL; |
| 2091 | |
| 2092 | if (hapd->conf->wps_nfc_dh_pubkey == NULL) { |
| 2093 | struct wps_context *wps = hapd->wps; |
| 2094 | if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey, |
| 2095 | &hapd->conf->wps_nfc_dh_privkey) < 0) |
| 2096 | return NULL; |
| 2097 | hostapd_wps_nfc_clear(wps); |
| 2098 | wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER; |
| 2099 | wps->ap_nfc_dh_pubkey = |
| 2100 | wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey); |
| 2101 | wps->ap_nfc_dh_privkey = |
| 2102 | wpabuf_dup(hapd->conf->wps_nfc_dh_privkey); |
| 2103 | if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) { |
| 2104 | hostapd_wps_nfc_clear(wps); |
| 2105 | return NULL; |
| 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | ret = wps_build_nfc_handover_sel(hapd->wps, |
| 2110 | hapd->conf->wps_nfc_dh_pubkey, |
| 2111 | hapd->own_addr, hapd->iface->freq); |
| 2112 | |
| 2113 | if (ndef && ret) { |
| 2114 | struct wpabuf *tmp; |
| 2115 | tmp = ndef_build_wifi(ret); |
| 2116 | wpabuf_free(ret); |
| 2117 | if (tmp == NULL) |
| 2118 | return NULL; |
| 2119 | ret = tmp; |
| 2120 | } |
| 2121 | |
| 2122 | return ret; |
| 2123 | } |
| 2124 | |
| 2125 | |
| 2126 | int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd, |
| 2127 | const struct wpabuf *req, |
| 2128 | const struct wpabuf *sel) |
| 2129 | { |
| 2130 | struct wpabuf *wps; |
| 2131 | int ret = -1; |
| 2132 | u16 wsc_len; |
| 2133 | const u8 *pos; |
| 2134 | struct wpabuf msg; |
| 2135 | struct wps_parse_attr attr; |
| 2136 | u16 dev_pw_id; |
| 2137 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 2138 | /* |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2139 | * Enrollee/station is always initiator of the NFC connection handover, |
| 2140 | * so use the request message here to find Enrollee public key hash. |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 2141 | */ |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2142 | wps = ndef_parse_wifi(req); |
| 2143 | if (wps == NULL) |
| 2144 | return -1; |
| 2145 | wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc " |
| 2146 | "payload from NFC connection handover"); |
| 2147 | wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps); |
| 2148 | if (wpabuf_len(wps) < 2) { |
| 2149 | wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request " |
| 2150 | "Message"); |
| 2151 | goto out; |
| 2152 | } |
| 2153 | pos = wpabuf_head(wps); |
| 2154 | wsc_len = WPA_GET_BE16(pos); |
| 2155 | if (wsc_len > wpabuf_len(wps) - 2) { |
| 2156 | wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) " |
| 2157 | "in rt Wi-Fi Handover Request Message", wsc_len); |
| 2158 | goto out; |
| 2159 | } |
| 2160 | pos += 2; |
| 2161 | |
| 2162 | wpa_hexdump(MSG_DEBUG, |
| 2163 | "WPS: WSC attributes in Wi-Fi Handover Request Message", |
| 2164 | pos, wsc_len); |
| 2165 | if (wsc_len < wpabuf_len(wps) - 2) { |
| 2166 | wpa_hexdump(MSG_DEBUG, |
| 2167 | "WPS: Ignore extra data after WSC attributes", |
| 2168 | pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len); |
| 2169 | } |
| 2170 | |
| 2171 | wpabuf_set(&msg, pos, wsc_len); |
| 2172 | ret = wps_parse_msg(&msg, &attr); |
| 2173 | if (ret < 0) { |
| 2174 | wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in " |
| 2175 | "Wi-Fi Handover Request Message"); |
| 2176 | goto out; |
| 2177 | } |
| 2178 | |
| 2179 | if (attr.oob_dev_password == NULL || |
| 2180 | attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) { |
| 2181 | wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password " |
| 2182 | "included in Wi-Fi Handover Request Message"); |
| 2183 | ret = -1; |
| 2184 | goto out; |
| 2185 | } |
| 2186 | |
| 2187 | if (attr.uuid_e == NULL) { |
| 2188 | wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi " |
| 2189 | "Handover Request Message"); |
| 2190 | ret = -1; |
| 2191 | goto out; |
| 2192 | } |
| 2193 | |
| 2194 | wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN); |
| 2195 | |
| 2196 | wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password", |
| 2197 | attr.oob_dev_password, attr.oob_dev_password_len); |
| 2198 | dev_pw_id = WPA_GET_BE16(attr.oob_dev_password + |
| 2199 | WPS_OOB_PUBKEY_HASH_LEN); |
| 2200 | if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) { |
| 2201 | wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID " |
| 2202 | "%u in Wi-Fi Handover Request Message", dev_pw_id); |
| 2203 | ret = -1; |
| 2204 | goto out; |
| 2205 | } |
| 2206 | wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash", |
| 2207 | attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN); |
| 2208 | |
| 2209 | ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar, |
| 2210 | attr.oob_dev_password, |
| 2211 | DEV_PW_NFC_CONNECTION_HANDOVER, |
| 2212 | NULL, 0, 1); |
| 2213 | |
| 2214 | out: |
| 2215 | wpabuf_free(wps); |
| 2216 | return ret; |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2220 | struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef) |
| 2221 | { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 2222 | if (hapd->conf->wps_nfc_pw_from_config) { |
| 2223 | return wps_nfc_token_build(ndef, |
| 2224 | hapd->conf->wps_nfc_dev_pw_id, |
| 2225 | hapd->conf->wps_nfc_dh_pubkey, |
| 2226 | hapd->conf->wps_nfc_dev_pw); |
| 2227 | } |
| 2228 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2229 | return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id, |
| 2230 | &hapd->conf->wps_nfc_dh_pubkey, |
| 2231 | &hapd->conf->wps_nfc_dh_privkey, |
| 2232 | &hapd->conf->wps_nfc_dev_pw); |
| 2233 | } |
| 2234 | |
| 2235 | |
| 2236 | int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd) |
| 2237 | { |
| 2238 | struct wps_context *wps = hapd->wps; |
Dmitry Shmidt | 33e38bf | 2013-02-27 12:56:00 -0800 | [diff] [blame] | 2239 | struct wpabuf *pw; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2240 | |
| 2241 | if (wps == NULL) |
| 2242 | return -1; |
| 2243 | |
| 2244 | if (!hapd->conf->wps_nfc_dh_pubkey || |
| 2245 | !hapd->conf->wps_nfc_dh_privkey || |
| 2246 | !hapd->conf->wps_nfc_dev_pw || |
| 2247 | !hapd->conf->wps_nfc_dev_pw_id) |
| 2248 | return -1; |
| 2249 | |
| 2250 | hostapd_wps_nfc_clear(wps); |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 2251 | wpa_printf(MSG_DEBUG, |
| 2252 | "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)", |
| 2253 | hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2254 | wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id; |
| 2255 | wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey); |
| 2256 | wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey); |
Dmitry Shmidt | 33e38bf | 2013-02-27 12:56:00 -0800 | [diff] [blame] | 2257 | pw = hapd->conf->wps_nfc_dev_pw; |
| 2258 | wps->ap_nfc_dev_pw = wpabuf_alloc( |
| 2259 | wpabuf_len(pw) * 2 + 1); |
| 2260 | if (wps->ap_nfc_dev_pw) { |
| 2261 | wpa_snprintf_hex_uppercase( |
| 2262 | (char *) wpabuf_put(wps->ap_nfc_dev_pw, |
| 2263 | wpabuf_len(pw) * 2), |
| 2264 | wpabuf_len(pw) * 2 + 1, |
| 2265 | wpabuf_head(pw), wpabuf_len(pw)); |
| 2266 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2267 | |
| 2268 | if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey || |
| 2269 | !wps->ap_nfc_dev_pw) { |
| 2270 | hostapd_wps_nfc_clear(wps); |
| 2271 | return -1; |
| 2272 | } |
| 2273 | |
| 2274 | return 0; |
| 2275 | } |
| 2276 | |
| 2277 | |
| 2278 | void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd) |
| 2279 | { |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 2280 | wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s", |
| 2281 | hapd->conf->iface); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2282 | hostapd_wps_nfc_clear(hapd->wps); |
| 2283 | } |
| 2284 | |
| 2285 | #endif /* CONFIG_WPS_NFC */ |