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