Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * hostapd / WPS integration |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3 | * Copyright (c) 2008-2012, 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" |
| 14 | #include "crypto/dh_groups.h" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 15 | #include "crypto/dh_group5.h" |
| 16 | #include "crypto/random.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 17 | #include "common/wpa_ctrl.h" |
| 18 | #include "common/ieee802_11_defs.h" |
| 19 | #include "common/ieee802_11_common.h" |
| 20 | #include "eapol_auth/eapol_auth_sm.h" |
| 21 | #include "eapol_auth/eapol_auth_sm_i.h" |
| 22 | #include "wps/wps.h" |
| 23 | #include "wps/wps_defs.h" |
| 24 | #include "wps/wps_dev_attr.h" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 25 | #include "wps/wps_attr_parse.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 26 | #include "hostapd.h" |
| 27 | #include "ap_config.h" |
| 28 | #include "ap_drv_ops.h" |
| 29 | #include "beacon.h" |
| 30 | #include "sta_info.h" |
| 31 | #include "wps_hostapd.h" |
| 32 | |
| 33 | |
| 34 | #ifdef CONFIG_WPS_UPNP |
| 35 | #include "wps/wps_upnp.h" |
| 36 | static int hostapd_wps_upnp_init(struct hostapd_data *hapd, |
| 37 | struct wps_context *wps); |
| 38 | static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd); |
| 39 | #endif /* CONFIG_WPS_UPNP */ |
| 40 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 41 | static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da, |
| 42 | const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 43 | const u8 *ie, size_t ie_len, |
| 44 | int ssi_signal); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 45 | static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx); |
| 46 | |
| 47 | |
| 48 | struct wps_for_each_data { |
| 49 | int (*func)(struct hostapd_data *h, void *ctx); |
| 50 | void *ctx; |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | static int wps_for_each(struct hostapd_iface *iface, void *ctx) |
| 55 | { |
| 56 | struct wps_for_each_data *data = ctx; |
| 57 | size_t j; |
| 58 | |
| 59 | if (iface == NULL) |
| 60 | return 0; |
| 61 | for (j = 0; j < iface->num_bss; j++) { |
| 62 | struct hostapd_data *hapd = iface->bss[j]; |
| 63 | int ret = data->func(hapd, data->ctx); |
| 64 | if (ret) |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | static int hostapd_wps_for_each(struct hostapd_data *hapd, |
| 73 | int (*func)(struct hostapd_data *h, void *ctx), |
| 74 | void *ctx) |
| 75 | { |
| 76 | struct hostapd_iface *iface = hapd->iface; |
| 77 | struct wps_for_each_data data; |
| 78 | data.func = func; |
| 79 | data.ctx = ctx; |
| 80 | if (iface->for_each_interface == NULL) |
| 81 | return wps_for_each(iface, &data); |
| 82 | return iface->for_each_interface(iface->interfaces, wps_for_each, |
| 83 | &data); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk, |
| 88 | size_t psk_len) |
| 89 | { |
| 90 | struct hostapd_data *hapd = ctx; |
| 91 | struct hostapd_wpa_psk *p; |
| 92 | struct hostapd_ssid *ssid = &hapd->conf->ssid; |
| 93 | |
| 94 | wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA " |
| 95 | MACSTR, MAC2STR(mac_addr)); |
| 96 | wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len); |
| 97 | |
| 98 | if (psk_len != PMK_LEN) { |
| 99 | wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu", |
| 100 | (unsigned long) psk_len); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | /* Add the new PSK to runtime PSK list */ |
| 105 | p = os_zalloc(sizeof(*p)); |
| 106 | if (p == NULL) |
| 107 | return -1; |
| 108 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
| 109 | os_memcpy(p->psk, psk, PMK_LEN); |
| 110 | |
| 111 | p->next = ssid->wpa_psk; |
| 112 | ssid->wpa_psk = p; |
| 113 | |
| 114 | if (ssid->wpa_psk_file) { |
| 115 | FILE *f; |
| 116 | char hex[PMK_LEN * 2 + 1]; |
| 117 | /* Add the new PSK to PSK list file */ |
| 118 | f = fopen(ssid->wpa_psk_file, "a"); |
| 119 | if (f == NULL) { |
| 120 | wpa_printf(MSG_DEBUG, "Failed to add the PSK to " |
| 121 | "'%s'", ssid->wpa_psk_file); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len); |
| 126 | fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex); |
| 127 | fclose(f); |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie, |
| 135 | struct wpabuf *probe_resp_ie) |
| 136 | { |
| 137 | struct hostapd_data *hapd = ctx; |
| 138 | wpabuf_free(hapd->wps_beacon_ie); |
| 139 | hapd->wps_beacon_ie = beacon_ie; |
| 140 | wpabuf_free(hapd->wps_probe_resp_ie); |
| 141 | hapd->wps_probe_resp_ie = probe_resp_ie; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 142 | if (hapd->beacon_set_done) |
| 143 | ieee802_11_set_beacon(hapd); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 144 | return hostapd_set_ap_wps_ie(hapd); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e, |
| 149 | const struct wps_device_data *dev) |
| 150 | { |
| 151 | struct hostapd_data *hapd = ctx; |
| 152 | char uuid[40], txt[400]; |
| 153 | int len; |
| 154 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
| 155 | if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) |
| 156 | return; |
| 157 | wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid); |
| 158 | len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED |
| 159 | "%s " MACSTR " [%s|%s|%s|%s|%s|%s]", |
| 160 | uuid, MAC2STR(dev->mac_addr), dev->device_name, |
| 161 | dev->manufacturer, dev->model_name, |
| 162 | dev->model_number, dev->serial_number, |
| 163 | wps_dev_type_bin2str(dev->pri_dev_type, devtype, |
| 164 | sizeof(devtype))); |
| 165 | if (len > 0 && len < (int) sizeof(txt)) |
| 166 | wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt); |
| 167 | |
| 168 | if (hapd->conf->wps_pin_requests) { |
| 169 | FILE *f; |
| 170 | struct os_time t; |
| 171 | f = fopen(hapd->conf->wps_pin_requests, "a"); |
| 172 | if (f == NULL) |
| 173 | return; |
| 174 | os_get_time(&t); |
| 175 | fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s" |
| 176 | "\t%s\n", |
| 177 | t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name, |
| 178 | dev->manufacturer, dev->model_name, dev->model_number, |
| 179 | dev->serial_number, |
| 180 | wps_dev_type_bin2str(dev->pri_dev_type, devtype, |
| 181 | sizeof(devtype))); |
| 182 | fclose(f); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 187 | struct wps_stop_reg_data { |
| 188 | struct hostapd_data *current_hapd; |
| 189 | const u8 *uuid_e; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 190 | const u8 *dev_pw; |
| 191 | size_t dev_pw_len; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx) |
| 195 | { |
| 196 | struct wps_stop_reg_data *data = ctx; |
| 197 | if (hapd != data->current_hapd && hapd->wps != NULL) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 198 | wps_registrar_complete(hapd->wps->registrar, data->uuid_e, |
| 199 | data->dev_pw, data->dev_pw_len); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 204 | 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] | 205 | const u8 *uuid_e, const u8 *dev_pw, |
| 206 | size_t dev_pw_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 207 | { |
| 208 | struct hostapd_data *hapd = ctx; |
| 209 | char uuid[40]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 210 | struct wps_stop_reg_data data; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 211 | if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) |
| 212 | return; |
| 213 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s", |
| 214 | MAC2STR(mac_addr), uuid); |
| 215 | if (hapd->wps_reg_success_cb) |
| 216 | hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx, |
| 217 | mac_addr, uuid_e); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 218 | data.current_hapd = hapd; |
| 219 | data.uuid_e = uuid_e; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 220 | data.dev_pw = dev_pw; |
| 221 | data.dev_pw_len = dev_pw_len; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 222 | hostapd_wps_for_each(hapd, wps_stop_registrar, &data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | |
| 226 | static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr, |
| 227 | const u8 *uuid_e, |
| 228 | const u8 *pri_dev_type, |
| 229 | u16 config_methods, |
| 230 | u16 dev_password_id, u8 request_type, |
| 231 | const char *dev_name) |
| 232 | { |
| 233 | struct hostapd_data *hapd = ctx; |
| 234 | char uuid[40]; |
| 235 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
| 236 | if (uuid_bin2str(uuid_e, uuid, sizeof(uuid))) |
| 237 | return; |
| 238 | if (dev_name == NULL) |
| 239 | dev_name = ""; |
| 240 | wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR |
| 241 | " %s %s 0x%x %u %u [%s]", |
| 242 | MAC2STR(addr), uuid, |
| 243 | wps_dev_type_bin2str(pri_dev_type, devtype, |
| 244 | sizeof(devtype)), |
| 245 | config_methods, dev_password_id, request_type, dev_name); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | static int str_starts(const char *str, const char *start) |
| 250 | { |
| 251 | return os_strncmp(str, start, os_strlen(start)) == 0; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | static void wps_reload_config(void *eloop_data, void *user_ctx) |
| 256 | { |
| 257 | struct hostapd_iface *iface = eloop_data; |
| 258 | |
| 259 | wpa_printf(MSG_DEBUG, "WPS: Reload configuration data"); |
| 260 | if (iface->reload_config(iface) < 0) { |
| 261 | wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated " |
| 262 | "configuration"); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 267 | static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr, |
| 268 | size_t attr_len) |
| 269 | { |
| 270 | size_t blen = attr_len * 2 + 1; |
| 271 | char *buf = os_malloc(blen); |
| 272 | if (buf) { |
| 273 | wpa_snprintf_hex(buf, blen, attr, attr_len); |
| 274 | wpa_msg(hapd->msg_ctx, MSG_INFO, |
| 275 | WPS_EVENT_NEW_AP_SETTINGS "%s", buf); |
| 276 | os_free(buf); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 281 | static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx) |
| 282 | { |
| 283 | const struct wps_credential *cred = ctx; |
| 284 | FILE *oconf, *nconf; |
| 285 | size_t len, i; |
| 286 | char *tmp_fname; |
| 287 | char buf[1024]; |
| 288 | int multi_bss; |
| 289 | int wpa; |
| 290 | |
| 291 | if (hapd->wps == NULL) |
| 292 | return 0; |
| 293 | |
| 294 | wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute", |
| 295 | cred->cred_attr, cred->cred_attr_len); |
| 296 | |
| 297 | wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings"); |
| 298 | wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len); |
| 299 | wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x", |
| 300 | cred->auth_type); |
| 301 | wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type); |
| 302 | wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx); |
| 303 | wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key", |
| 304 | cred->key, cred->key_len); |
| 305 | wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR, |
| 306 | MAC2STR(cred->mac_addr)); |
| 307 | |
| 308 | if ((hapd->conf->wps_cred_processing == 1 || |
| 309 | hapd->conf->wps_cred_processing == 2) && cred->cred_attr) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 310 | hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len); |
| 311 | } else if (hapd->conf->wps_cred_processing == 1 || |
| 312 | hapd->conf->wps_cred_processing == 2) { |
| 313 | struct wpabuf *attr; |
| 314 | attr = wpabuf_alloc(200); |
| 315 | if (attr && wps_build_credential_wrap(attr, cred) == 0) |
| 316 | hapd_new_ap_event(hapd, wpabuf_head_u8(attr), |
| 317 | wpabuf_len(attr)); |
| 318 | wpabuf_free(attr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 319 | } else |
| 320 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS); |
| 321 | |
| 322 | if (hapd->conf->wps_cred_processing == 1) |
| 323 | return 0; |
| 324 | |
| 325 | os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len); |
| 326 | hapd->wps->ssid_len = cred->ssid_len; |
| 327 | hapd->wps->encr_types = cred->encr_type; |
| 328 | hapd->wps->auth_types = cred->auth_type; |
| 329 | if (cred->key_len == 0) { |
| 330 | os_free(hapd->wps->network_key); |
| 331 | hapd->wps->network_key = NULL; |
| 332 | hapd->wps->network_key_len = 0; |
| 333 | } else { |
| 334 | if (hapd->wps->network_key == NULL || |
| 335 | hapd->wps->network_key_len < cred->key_len) { |
| 336 | hapd->wps->network_key_len = 0; |
| 337 | os_free(hapd->wps->network_key); |
| 338 | hapd->wps->network_key = os_malloc(cred->key_len); |
| 339 | if (hapd->wps->network_key == NULL) |
| 340 | return -1; |
| 341 | } |
| 342 | hapd->wps->network_key_len = cred->key_len; |
| 343 | os_memcpy(hapd->wps->network_key, cred->key, cred->key_len); |
| 344 | } |
| 345 | hapd->wps->wps_state = WPS_STATE_CONFIGURED; |
| 346 | |
| 347 | len = os_strlen(hapd->iface->config_fname) + 5; |
| 348 | tmp_fname = os_malloc(len); |
| 349 | if (tmp_fname == NULL) |
| 350 | return -1; |
| 351 | os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname); |
| 352 | |
| 353 | oconf = fopen(hapd->iface->config_fname, "r"); |
| 354 | if (oconf == NULL) { |
| 355 | wpa_printf(MSG_WARNING, "WPS: Could not open current " |
| 356 | "configuration file"); |
| 357 | os_free(tmp_fname); |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | nconf = fopen(tmp_fname, "w"); |
| 362 | if (nconf == NULL) { |
| 363 | wpa_printf(MSG_WARNING, "WPS: Could not write updated " |
| 364 | "configuration file"); |
| 365 | os_free(tmp_fname); |
| 366 | fclose(oconf); |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | fprintf(nconf, "# WPS configuration - START\n"); |
| 371 | |
| 372 | fprintf(nconf, "wps_state=2\n"); |
| 373 | |
| 374 | fprintf(nconf, "ssid="); |
| 375 | for (i = 0; i < cred->ssid_len; i++) |
| 376 | fputc(cred->ssid[i], nconf); |
| 377 | fprintf(nconf, "\n"); |
| 378 | |
| 379 | if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) && |
| 380 | (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))) |
| 381 | wpa = 3; |
| 382 | else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) |
| 383 | wpa = 2; |
| 384 | else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)) |
| 385 | wpa = 1; |
| 386 | else |
| 387 | wpa = 0; |
| 388 | |
| 389 | if (wpa) { |
| 390 | char *prefix; |
| 391 | fprintf(nconf, "wpa=%d\n", wpa); |
| 392 | |
| 393 | fprintf(nconf, "wpa_key_mgmt="); |
| 394 | prefix = ""; |
| 395 | if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) { |
| 396 | fprintf(nconf, "WPA-EAP"); |
| 397 | prefix = " "; |
| 398 | } |
| 399 | if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) |
| 400 | fprintf(nconf, "%sWPA-PSK", prefix); |
| 401 | fprintf(nconf, "\n"); |
| 402 | |
| 403 | fprintf(nconf, "wpa_pairwise="); |
| 404 | prefix = ""; |
| 405 | if (cred->encr_type & WPS_ENCR_AES) { |
| 406 | fprintf(nconf, "CCMP"); |
| 407 | prefix = " "; |
| 408 | } |
| 409 | if (cred->encr_type & WPS_ENCR_TKIP) { |
| 410 | fprintf(nconf, "%sTKIP", prefix); |
| 411 | } |
| 412 | fprintf(nconf, "\n"); |
| 413 | |
| 414 | if (cred->key_len >= 8 && cred->key_len < 64) { |
| 415 | fprintf(nconf, "wpa_passphrase="); |
| 416 | for (i = 0; i < cred->key_len; i++) |
| 417 | fputc(cred->key[i], nconf); |
| 418 | fprintf(nconf, "\n"); |
| 419 | } else if (cred->key_len == 64) { |
| 420 | fprintf(nconf, "wpa_psk="); |
| 421 | for (i = 0; i < cred->key_len; i++) |
| 422 | fputc(cred->key[i], nconf); |
| 423 | fprintf(nconf, "\n"); |
| 424 | } else { |
| 425 | wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu " |
| 426 | "for WPA/WPA2", |
| 427 | (unsigned long) cred->key_len); |
| 428 | } |
| 429 | |
| 430 | fprintf(nconf, "auth_algs=1\n"); |
| 431 | } else { |
| 432 | if ((cred->auth_type & WPS_AUTH_OPEN) && |
| 433 | (cred->auth_type & WPS_AUTH_SHARED)) |
| 434 | fprintf(nconf, "auth_algs=3\n"); |
| 435 | else if (cred->auth_type & WPS_AUTH_SHARED) |
| 436 | fprintf(nconf, "auth_algs=2\n"); |
| 437 | else |
| 438 | fprintf(nconf, "auth_algs=1\n"); |
| 439 | |
| 440 | if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) { |
| 441 | int key_idx = cred->key_idx; |
| 442 | if (key_idx) |
| 443 | key_idx--; |
| 444 | fprintf(nconf, "wep_default_key=%d\n", key_idx); |
| 445 | fprintf(nconf, "wep_key%d=", key_idx); |
| 446 | if (cred->key_len == 10 || cred->key_len == 26) { |
| 447 | /* WEP key as a hex string */ |
| 448 | for (i = 0; i < cred->key_len; i++) |
| 449 | fputc(cred->key[i], nconf); |
| 450 | } else { |
| 451 | /* Raw WEP key; convert to hex */ |
| 452 | for (i = 0; i < cred->key_len; i++) |
| 453 | fprintf(nconf, "%02x", cred->key[i]); |
| 454 | } |
| 455 | fprintf(nconf, "\n"); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | fprintf(nconf, "# WPS configuration - END\n"); |
| 460 | |
| 461 | multi_bss = 0; |
| 462 | while (fgets(buf, sizeof(buf), oconf)) { |
| 463 | if (os_strncmp(buf, "bss=", 4) == 0) |
| 464 | multi_bss = 1; |
| 465 | if (!multi_bss && |
| 466 | (str_starts(buf, "ssid=") || |
| 467 | str_starts(buf, "auth_algs=") || |
| 468 | str_starts(buf, "wep_default_key=") || |
| 469 | str_starts(buf, "wep_key") || |
| 470 | str_starts(buf, "wps_state=") || |
| 471 | str_starts(buf, "wpa=") || |
| 472 | str_starts(buf, "wpa_psk=") || |
| 473 | str_starts(buf, "wpa_pairwise=") || |
| 474 | str_starts(buf, "rsn_pairwise=") || |
| 475 | str_starts(buf, "wpa_key_mgmt=") || |
| 476 | str_starts(buf, "wpa_passphrase="))) { |
| 477 | fprintf(nconf, "#WPS# %s", buf); |
| 478 | } else |
| 479 | fprintf(nconf, "%s", buf); |
| 480 | } |
| 481 | |
| 482 | fclose(nconf); |
| 483 | fclose(oconf); |
| 484 | |
| 485 | if (rename(tmp_fname, hapd->iface->config_fname) < 0) { |
| 486 | wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated " |
| 487 | "configuration file: %s", strerror(errno)); |
| 488 | os_free(tmp_fname); |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | os_free(tmp_fname); |
| 493 | |
| 494 | /* Schedule configuration reload after short period of time to allow |
| 495 | * EAP-WSC to be finished. |
| 496 | */ |
| 497 | eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface, |
| 498 | NULL); |
| 499 | |
| 500 | wpa_printf(MSG_DEBUG, "WPS: AP configuration updated"); |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | |
| 506 | static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred) |
| 507 | { |
| 508 | struct hostapd_data *hapd = ctx; |
| 509 | return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred); |
| 510 | } |
| 511 | |
| 512 | |
| 513 | static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx) |
| 514 | { |
| 515 | struct hostapd_data *hapd = eloop_data; |
| 516 | |
| 517 | if (hapd->conf->ap_setup_locked) |
| 518 | return; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 519 | if (hapd->ap_pin_failures_consecutive >= 10) |
| 520 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 521 | |
| 522 | wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN"); |
| 523 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED); |
| 524 | hapd->wps->ap_setup_locked = 0; |
| 525 | wps_registrar_update_ie(hapd->wps->registrar); |
| 526 | } |
| 527 | |
| 528 | |
| 529 | static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx) |
| 530 | { |
| 531 | struct wps_event_pwd_auth_fail *data = ctx; |
| 532 | |
| 533 | if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL) |
| 534 | return 0; |
| 535 | |
| 536 | /* |
| 537 | * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup |
| 538 | * for some time if this happens multiple times to slow down brute |
| 539 | * force attacks. |
| 540 | */ |
| 541 | hapd->ap_pin_failures++; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 542 | hapd->ap_pin_failures_consecutive++; |
| 543 | wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u " |
| 544 | "(%u consecutive)", |
| 545 | hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 546 | if (hapd->ap_pin_failures < 3) |
| 547 | return 0; |
| 548 | |
| 549 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED); |
| 550 | hapd->wps->ap_setup_locked = 1; |
| 551 | |
| 552 | wps_registrar_update_ie(hapd->wps->registrar); |
| 553 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 554 | if (!hapd->conf->ap_setup_locked && |
| 555 | hapd->ap_pin_failures_consecutive >= 10) { |
| 556 | /* |
| 557 | * In indefinite lockdown - disable automatic AP PIN |
| 558 | * reenablement. |
| 559 | */ |
| 560 | eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); |
| 561 | wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely"); |
| 562 | } else if (!hapd->conf->ap_setup_locked) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 563 | if (hapd->ap_pin_lockout_time == 0) |
| 564 | hapd->ap_pin_lockout_time = 60; |
| 565 | else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 && |
| 566 | (hapd->ap_pin_failures % 3) == 0) |
| 567 | hapd->ap_pin_lockout_time *= 2; |
| 568 | |
| 569 | wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds", |
| 570 | hapd->ap_pin_lockout_time); |
| 571 | eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); |
| 572 | eloop_register_timeout(hapd->ap_pin_lockout_time, 0, |
| 573 | hostapd_wps_reenable_ap_pin, hapd, |
| 574 | NULL); |
| 575 | } |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | |
| 581 | static void hostapd_pwd_auth_fail(struct hostapd_data *hapd, |
| 582 | struct wps_event_pwd_auth_fail *data) |
| 583 | { |
| 584 | hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data); |
| 585 | } |
| 586 | |
| 587 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 588 | static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx) |
| 589 | { |
| 590 | if (hapd->conf->ap_pin == NULL || hapd->wps == NULL) |
| 591 | return 0; |
| 592 | |
| 593 | if (hapd->ap_pin_failures_consecutive == 0) |
| 594 | return 0; |
| 595 | |
| 596 | wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter " |
| 597 | "- total validation failures %u (%u consecutive)", |
| 598 | hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive); |
| 599 | hapd->ap_pin_failures_consecutive = 0; |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | |
| 605 | static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd) |
| 606 | { |
| 607 | hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL); |
| 608 | } |
| 609 | |
| 610 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 611 | static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = { |
| 612 | "No Error", /* WPS_EI_NO_ERROR */ |
| 613 | "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */ |
| 614 | "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */ |
| 615 | }; |
| 616 | |
| 617 | static void hostapd_wps_event_fail(struct hostapd_data *hapd, |
| 618 | struct wps_event_fail *fail) |
| 619 | { |
| 620 | if (fail->error_indication > 0 && |
| 621 | fail->error_indication < NUM_WPS_EI_VALUES) { |
| 622 | wpa_msg(hapd->msg_ctx, MSG_INFO, |
| 623 | WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)", |
| 624 | fail->msg, fail->config_error, fail->error_indication, |
| 625 | wps_event_fail_reason[fail->error_indication]); |
| 626 | } else { |
| 627 | wpa_msg(hapd->msg_ctx, MSG_INFO, |
| 628 | WPS_EVENT_FAIL "msg=%d config_error=%d", |
| 629 | fail->msg, fail->config_error); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | |
| 634 | static void hostapd_wps_event_cb(void *ctx, enum wps_event event, |
| 635 | union wps_event_data *data) |
| 636 | { |
| 637 | struct hostapd_data *hapd = ctx; |
| 638 | |
| 639 | switch (event) { |
| 640 | case WPS_EV_M2D: |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 641 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 642 | break; |
| 643 | case WPS_EV_FAIL: |
| 644 | hostapd_wps_event_fail(hapd, &data->fail); |
| 645 | break; |
| 646 | case WPS_EV_SUCCESS: |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 647 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 648 | break; |
| 649 | case WPS_EV_PWD_AUTH_FAIL: |
| 650 | hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail); |
| 651 | break; |
| 652 | case WPS_EV_PBC_OVERLAP: |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 653 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 654 | break; |
| 655 | case WPS_EV_PBC_TIMEOUT: |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 656 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 657 | break; |
| 658 | case WPS_EV_ER_AP_ADD: |
| 659 | break; |
| 660 | case WPS_EV_ER_AP_REMOVE: |
| 661 | break; |
| 662 | case WPS_EV_ER_ENROLLEE_ADD: |
| 663 | break; |
| 664 | case WPS_EV_ER_ENROLLEE_REMOVE: |
| 665 | break; |
| 666 | case WPS_EV_ER_AP_SETTINGS: |
| 667 | break; |
| 668 | case WPS_EV_ER_SET_SELECTED_REGISTRAR: |
| 669 | break; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 670 | case WPS_EV_AP_PIN_SUCCESS: |
| 671 | hostapd_wps_ap_pin_success(hapd); |
| 672 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 673 | } |
| 674 | if (hapd->wps_event_cb) |
| 675 | hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data); |
| 676 | } |
| 677 | |
| 678 | |
| 679 | static void hostapd_wps_clear_ies(struct hostapd_data *hapd) |
| 680 | { |
| 681 | wpabuf_free(hapd->wps_beacon_ie); |
| 682 | hapd->wps_beacon_ie = NULL; |
| 683 | |
| 684 | wpabuf_free(hapd->wps_probe_resp_ie); |
| 685 | hapd->wps_probe_resp_ie = NULL; |
| 686 | |
| 687 | hostapd_set_ap_wps_ie(hapd); |
| 688 | } |
| 689 | |
| 690 | |
| 691 | static int get_uuid_cb(struct hostapd_iface *iface, void *ctx) |
| 692 | { |
| 693 | const u8 **uuid = ctx; |
| 694 | size_t j; |
| 695 | |
| 696 | if (iface == NULL) |
| 697 | return 0; |
| 698 | for (j = 0; j < iface->num_bss; j++) { |
| 699 | struct hostapd_data *hapd = iface->bss[j]; |
| 700 | if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) { |
| 701 | *uuid = hapd->wps->uuid; |
| 702 | return 1; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | |
| 710 | static const u8 * get_own_uuid(struct hostapd_iface *iface) |
| 711 | { |
| 712 | const u8 *uuid; |
| 713 | if (iface->for_each_interface == NULL) |
| 714 | return NULL; |
| 715 | uuid = NULL; |
| 716 | iface->for_each_interface(iface->interfaces, get_uuid_cb, &uuid); |
| 717 | return uuid; |
| 718 | } |
| 719 | |
| 720 | |
| 721 | static int count_interface_cb(struct hostapd_iface *iface, void *ctx) |
| 722 | { |
| 723 | int *count= ctx; |
| 724 | (*count)++; |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | |
| 729 | static int interface_count(struct hostapd_iface *iface) |
| 730 | { |
| 731 | int count = 0; |
| 732 | if (iface->for_each_interface == NULL) |
| 733 | return 0; |
| 734 | iface->for_each_interface(iface->interfaces, count_interface_cb, |
| 735 | &count); |
| 736 | return count; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd, |
| 741 | struct wps_context *wps) |
| 742 | { |
| 743 | int i; |
| 744 | |
| 745 | for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) { |
| 746 | wpabuf_free(wps->dev.vendor_ext[i]); |
| 747 | wps->dev.vendor_ext[i] = NULL; |
| 748 | |
| 749 | if (hapd->conf->wps_vendor_ext[i] == NULL) |
| 750 | continue; |
| 751 | |
| 752 | wps->dev.vendor_ext[i] = |
| 753 | wpabuf_dup(hapd->conf->wps_vendor_ext[i]); |
| 754 | if (wps->dev.vendor_ext[i] == NULL) { |
| 755 | while (--i >= 0) |
| 756 | wpabuf_free(wps->dev.vendor_ext[i]); |
| 757 | return -1; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | |
| 765 | int hostapd_init_wps(struct hostapd_data *hapd, |
| 766 | struct hostapd_bss_config *conf) |
| 767 | { |
| 768 | struct wps_context *wps; |
| 769 | struct wps_registrar_config cfg; |
| 770 | |
| 771 | if (conf->wps_state == 0) { |
| 772 | hostapd_wps_clear_ies(hapd); |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | wps = os_zalloc(sizeof(*wps)); |
| 777 | if (wps == NULL) |
| 778 | return -1; |
| 779 | |
| 780 | wps->cred_cb = hostapd_wps_cred_cb; |
| 781 | wps->event_cb = hostapd_wps_event_cb; |
| 782 | wps->cb_ctx = hapd; |
| 783 | |
| 784 | os_memset(&cfg, 0, sizeof(cfg)); |
| 785 | wps->wps_state = hapd->conf->wps_state; |
| 786 | wps->ap_setup_locked = hapd->conf->ap_setup_locked; |
| 787 | if (is_nil_uuid(hapd->conf->uuid)) { |
| 788 | const u8 *uuid; |
| 789 | uuid = get_own_uuid(hapd->iface); |
| 790 | if (uuid) { |
| 791 | os_memcpy(wps->uuid, uuid, UUID_LEN); |
| 792 | wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another " |
| 793 | "interface", wps->uuid, UUID_LEN); |
| 794 | } else { |
| 795 | uuid_gen_mac_addr(hapd->own_addr, wps->uuid); |
| 796 | wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC " |
| 797 | "address", wps->uuid, UUID_LEN); |
| 798 | } |
| 799 | } else { |
| 800 | os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN); |
| 801 | wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID", |
| 802 | wps->uuid, UUID_LEN); |
| 803 | } |
| 804 | wps->ssid_len = hapd->conf->ssid.ssid_len; |
| 805 | os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len); |
| 806 | wps->ap = 1; |
| 807 | os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN); |
| 808 | wps->dev.device_name = hapd->conf->device_name ? |
| 809 | os_strdup(hapd->conf->device_name) : NULL; |
| 810 | wps->dev.manufacturer = hapd->conf->manufacturer ? |
| 811 | os_strdup(hapd->conf->manufacturer) : NULL; |
| 812 | wps->dev.model_name = hapd->conf->model_name ? |
| 813 | os_strdup(hapd->conf->model_name) : NULL; |
| 814 | wps->dev.model_number = hapd->conf->model_number ? |
| 815 | os_strdup(hapd->conf->model_number) : NULL; |
| 816 | wps->dev.serial_number = hapd->conf->serial_number ? |
| 817 | os_strdup(hapd->conf->serial_number) : NULL; |
| 818 | wps->config_methods = |
| 819 | wps_config_methods_str2bin(hapd->conf->config_methods); |
| 820 | #ifdef CONFIG_WPS2 |
| 821 | if ((wps->config_methods & |
| 822 | (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY | |
| 823 | WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) { |
| 824 | wpa_printf(MSG_INFO, "WPS: Converting display to " |
| 825 | "virtual_display for WPS 2.0 compliance"); |
| 826 | wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY; |
| 827 | } |
| 828 | if ((wps->config_methods & |
| 829 | (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON | |
| 830 | WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) { |
| 831 | wpa_printf(MSG_INFO, "WPS: Converting push_button to " |
| 832 | "virtual_push_button for WPS 2.0 compliance"); |
| 833 | wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON; |
| 834 | } |
| 835 | #endif /* CONFIG_WPS2 */ |
| 836 | os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type, |
| 837 | WPS_DEV_TYPE_LEN); |
| 838 | |
| 839 | if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) { |
| 840 | os_free(wps); |
| 841 | return -1; |
| 842 | } |
| 843 | |
| 844 | wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 845 | |
| 846 | if (conf->wps_rf_bands) { |
| 847 | wps->dev.rf_bands = conf->wps_rf_bands; |
| 848 | } else { |
| 849 | wps->dev.rf_bands = |
| 850 | hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ? |
| 851 | WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */ |
| 852 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 853 | |
| 854 | if (conf->wpa & WPA_PROTO_RSN) { |
| 855 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) |
| 856 | wps->auth_types |= WPS_AUTH_WPA2PSK; |
| 857 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) |
| 858 | wps->auth_types |= WPS_AUTH_WPA2; |
| 859 | |
| 860 | if (conf->rsn_pairwise & WPA_CIPHER_CCMP) |
| 861 | wps->encr_types |= WPS_ENCR_AES; |
| 862 | if (conf->rsn_pairwise & WPA_CIPHER_TKIP) |
| 863 | wps->encr_types |= WPS_ENCR_TKIP; |
| 864 | } |
| 865 | |
| 866 | if (conf->wpa & WPA_PROTO_WPA) { |
| 867 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) |
| 868 | wps->auth_types |= WPS_AUTH_WPAPSK; |
| 869 | if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) |
| 870 | wps->auth_types |= WPS_AUTH_WPA; |
| 871 | |
| 872 | if (conf->wpa_pairwise & WPA_CIPHER_CCMP) |
| 873 | wps->encr_types |= WPS_ENCR_AES; |
| 874 | if (conf->wpa_pairwise & WPA_CIPHER_TKIP) |
| 875 | wps->encr_types |= WPS_ENCR_TKIP; |
| 876 | } |
| 877 | |
| 878 | if (conf->ssid.security_policy == SECURITY_PLAINTEXT) { |
| 879 | wps->encr_types |= WPS_ENCR_NONE; |
| 880 | wps->auth_types |= WPS_AUTH_OPEN; |
| 881 | } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) { |
| 882 | wps->encr_types |= WPS_ENCR_WEP; |
| 883 | if (conf->auth_algs & WPA_AUTH_ALG_OPEN) |
| 884 | wps->auth_types |= WPS_AUTH_OPEN; |
| 885 | if (conf->auth_algs & WPA_AUTH_ALG_SHARED) |
| 886 | wps->auth_types |= WPS_AUTH_SHARED; |
| 887 | } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) { |
| 888 | wps->auth_types |= WPS_AUTH_OPEN; |
| 889 | if (conf->default_wep_key_len) |
| 890 | wps->encr_types |= WPS_ENCR_WEP; |
| 891 | else |
| 892 | wps->encr_types |= WPS_ENCR_NONE; |
| 893 | } |
| 894 | |
| 895 | if (conf->ssid.wpa_psk_file) { |
| 896 | /* Use per-device PSKs */ |
| 897 | } else if (conf->ssid.wpa_passphrase) { |
| 898 | wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase); |
| 899 | wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase); |
| 900 | } else if (conf->ssid.wpa_psk) { |
| 901 | wps->network_key = os_malloc(2 * PMK_LEN + 1); |
| 902 | if (wps->network_key == NULL) { |
| 903 | os_free(wps); |
| 904 | return -1; |
| 905 | } |
| 906 | wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1, |
| 907 | conf->ssid.wpa_psk->psk, PMK_LEN); |
| 908 | wps->network_key_len = 2 * PMK_LEN; |
| 909 | } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) { |
| 910 | wps->network_key = os_malloc(conf->ssid.wep.len[0]); |
| 911 | if (wps->network_key == NULL) { |
| 912 | os_free(wps); |
| 913 | return -1; |
| 914 | } |
| 915 | os_memcpy(wps->network_key, conf->ssid.wep.key[0], |
| 916 | conf->ssid.wep.len[0]); |
| 917 | wps->network_key_len = conf->ssid.wep.len[0]; |
| 918 | } |
| 919 | |
| 920 | if (conf->ssid.wpa_psk) { |
| 921 | os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN); |
| 922 | wps->psk_set = 1; |
| 923 | } |
| 924 | |
| 925 | if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) { |
| 926 | /* Override parameters to enable security by default */ |
| 927 | wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK; |
| 928 | wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP; |
| 929 | } |
| 930 | |
| 931 | wps->ap_settings = conf->ap_settings; |
| 932 | wps->ap_settings_len = conf->ap_settings_len; |
| 933 | |
| 934 | cfg.new_psk_cb = hostapd_wps_new_psk_cb; |
| 935 | cfg.set_ie_cb = hostapd_wps_set_ie_cb; |
| 936 | cfg.pin_needed_cb = hostapd_wps_pin_needed_cb; |
| 937 | cfg.reg_success_cb = hostapd_wps_reg_success_cb; |
| 938 | cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb; |
| 939 | cfg.cb_ctx = hapd; |
| 940 | cfg.skip_cred_build = conf->skip_cred_build; |
| 941 | cfg.extra_cred = conf->extra_cred; |
| 942 | cfg.extra_cred_len = conf->extra_cred_len; |
| 943 | cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) && |
| 944 | conf->skip_cred_build; |
| 945 | if (conf->ssid.security_policy == SECURITY_STATIC_WEP) |
| 946 | cfg.static_wep_only = 1; |
| 947 | cfg.dualband = interface_count(hapd->iface) > 1; |
| 948 | if (cfg.dualband) |
| 949 | wpa_printf(MSG_DEBUG, "WPS: Dualband AP"); |
| 950 | |
| 951 | wps->registrar = wps_registrar_init(wps, &cfg); |
| 952 | if (wps->registrar == NULL) { |
| 953 | wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar"); |
| 954 | os_free(wps->network_key); |
| 955 | os_free(wps); |
| 956 | return -1; |
| 957 | } |
| 958 | |
| 959 | #ifdef CONFIG_WPS_UPNP |
| 960 | wps->friendly_name = hapd->conf->friendly_name; |
| 961 | wps->manufacturer_url = hapd->conf->manufacturer_url; |
| 962 | wps->model_description = hapd->conf->model_description; |
| 963 | wps->model_url = hapd->conf->model_url; |
| 964 | wps->upc = hapd->conf->upc; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 965 | #endif /* CONFIG_WPS_UPNP */ |
| 966 | |
| 967 | hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd); |
| 968 | |
| 969 | hapd->wps = wps; |
| 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | |
Jouni Malinen | 87fd279 | 2011-05-16 18:35:42 +0300 | [diff] [blame] | 975 | int hostapd_init_wps_complete(struct hostapd_data *hapd) |
| 976 | { |
| 977 | struct wps_context *wps = hapd->wps; |
| 978 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 979 | if (wps == NULL) |
Jouni Malinen | 87fd279 | 2011-05-16 18:35:42 +0300 | [diff] [blame] | 980 | return 0; |
| 981 | |
| 982 | #ifdef CONFIG_WPS_UPNP |
| 983 | if (hostapd_wps_upnp_init(hapd, wps) < 0) { |
| 984 | wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP"); |
| 985 | wps_registrar_deinit(wps->registrar); |
| 986 | os_free(wps->network_key); |
| 987 | os_free(wps); |
| 988 | hapd->wps = NULL; |
| 989 | return -1; |
| 990 | } |
| 991 | #endif /* CONFIG_WPS_UPNP */ |
| 992 | |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 997 | static void hostapd_wps_nfc_clear(struct wps_context *wps) |
| 998 | { |
| 999 | #ifdef CONFIG_WPS_NFC |
| 1000 | wps->ap_nfc_dev_pw_id = 0; |
| 1001 | wpabuf_free(wps->ap_nfc_dh_pubkey); |
| 1002 | wps->ap_nfc_dh_pubkey = NULL; |
| 1003 | wpabuf_free(wps->ap_nfc_dh_privkey); |
| 1004 | wps->ap_nfc_dh_privkey = NULL; |
| 1005 | wpabuf_free(wps->ap_nfc_dev_pw); |
| 1006 | wps->ap_nfc_dev_pw = NULL; |
| 1007 | #endif /* CONFIG_WPS_NFC */ |
| 1008 | } |
| 1009 | |
| 1010 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1011 | void hostapd_deinit_wps(struct hostapd_data *hapd) |
| 1012 | { |
| 1013 | eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL); |
| 1014 | eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); |
| 1015 | if (hapd->wps == NULL) |
| 1016 | return; |
| 1017 | #ifdef CONFIG_WPS_UPNP |
| 1018 | hostapd_wps_upnp_deinit(hapd); |
| 1019 | #endif /* CONFIG_WPS_UPNP */ |
| 1020 | wps_registrar_deinit(hapd->wps->registrar); |
| 1021 | os_free(hapd->wps->network_key); |
| 1022 | wps_device_data_free(&hapd->wps->dev); |
| 1023 | wpabuf_free(hapd->wps->dh_pubkey); |
| 1024 | wpabuf_free(hapd->wps->dh_privkey); |
| 1025 | wpabuf_free(hapd->wps->oob_conf.pubkey_hash); |
| 1026 | wpabuf_free(hapd->wps->oob_conf.dev_password); |
| 1027 | wps_free_pending_msgs(hapd->wps->upnp_msgs); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1028 | hostapd_wps_nfc_clear(hapd->wps); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1029 | os_free(hapd->wps); |
| 1030 | hapd->wps = NULL; |
| 1031 | hostapd_wps_clear_ies(hapd); |
| 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | void hostapd_update_wps(struct hostapd_data *hapd) |
| 1036 | { |
| 1037 | if (hapd->wps == NULL) |
| 1038 | return; |
| 1039 | |
| 1040 | #ifdef CONFIG_WPS_UPNP |
| 1041 | hapd->wps->friendly_name = hapd->conf->friendly_name; |
| 1042 | hapd->wps->manufacturer_url = hapd->conf->manufacturer_url; |
| 1043 | hapd->wps->model_description = hapd->conf->model_description; |
| 1044 | hapd->wps->model_url = hapd->conf->model_url; |
| 1045 | hapd->wps->upc = hapd->conf->upc; |
| 1046 | #endif /* CONFIG_WPS_UPNP */ |
| 1047 | |
| 1048 | hostapd_wps_set_vendor_ext(hapd, hapd->wps); |
| 1049 | |
| 1050 | if (hapd->conf->wps_state) |
| 1051 | wps_registrar_update_ie(hapd->wps->registrar); |
| 1052 | else |
| 1053 | hostapd_deinit_wps(hapd); |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | struct wps_add_pin_data { |
| 1058 | const u8 *addr; |
| 1059 | const u8 *uuid; |
| 1060 | const u8 *pin; |
| 1061 | size_t pin_len; |
| 1062 | int timeout; |
| 1063 | int added; |
| 1064 | }; |
| 1065 | |
| 1066 | |
| 1067 | static int wps_add_pin(struct hostapd_data *hapd, void *ctx) |
| 1068 | { |
| 1069 | struct wps_add_pin_data *data = ctx; |
| 1070 | int ret; |
| 1071 | |
| 1072 | if (hapd->wps == NULL) |
| 1073 | return 0; |
| 1074 | ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr, |
| 1075 | data->uuid, data->pin, data->pin_len, |
| 1076 | data->timeout); |
| 1077 | if (ret == 0) |
| 1078 | data->added++; |
| 1079 | return ret; |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr, |
| 1084 | const char *uuid, const char *pin, int timeout) |
| 1085 | { |
| 1086 | u8 u[UUID_LEN]; |
| 1087 | struct wps_add_pin_data data; |
| 1088 | |
| 1089 | data.addr = addr; |
| 1090 | data.uuid = u; |
| 1091 | data.pin = (const u8 *) pin; |
| 1092 | data.pin_len = os_strlen(pin); |
| 1093 | data.timeout = timeout; |
| 1094 | data.added = 0; |
| 1095 | |
| 1096 | if (os_strcmp(uuid, "any") == 0) |
| 1097 | data.uuid = NULL; |
| 1098 | else { |
| 1099 | if (uuid_str2bin(uuid, u)) |
| 1100 | return -1; |
| 1101 | data.uuid = u; |
| 1102 | } |
| 1103 | if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0) |
| 1104 | return -1; |
| 1105 | return data.added ? 0 : -1; |
| 1106 | } |
| 1107 | |
| 1108 | |
| 1109 | static int wps_button_pushed(struct hostapd_data *hapd, void *ctx) |
| 1110 | { |
| 1111 | const u8 *p2p_dev_addr = ctx; |
| 1112 | if (hapd->wps == NULL) |
| 1113 | return 0; |
| 1114 | return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr); |
| 1115 | } |
| 1116 | |
| 1117 | |
| 1118 | int hostapd_wps_button_pushed(struct hostapd_data *hapd, |
| 1119 | const u8 *p2p_dev_addr) |
| 1120 | { |
| 1121 | return hostapd_wps_for_each(hapd, wps_button_pushed, |
| 1122 | (void *) p2p_dev_addr); |
| 1123 | } |
| 1124 | |
| 1125 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1126 | static int wps_cancel(struct hostapd_data *hapd, void *ctx) |
| 1127 | { |
| 1128 | if (hapd->wps == NULL) |
| 1129 | return 0; |
| 1130 | |
| 1131 | wps_registrar_wps_cancel(hapd->wps->registrar); |
| 1132 | ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL); |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | int hostapd_wps_cancel(struct hostapd_data *hapd) |
| 1139 | { |
| 1140 | return hostapd_wps_for_each(hapd, wps_cancel, NULL); |
| 1141 | } |
| 1142 | |
| 1143 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1144 | #ifdef CONFIG_WPS_OOB |
| 1145 | int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type, |
| 1146 | char *path, char *method, char *name) |
| 1147 | { |
| 1148 | struct wps_context *wps = hapd->wps; |
| 1149 | struct oob_device_data *oob_dev; |
| 1150 | |
| 1151 | oob_dev = wps_get_oob_device(device_type); |
| 1152 | if (oob_dev == NULL) |
| 1153 | return -1; |
| 1154 | oob_dev->device_path = path; |
| 1155 | oob_dev->device_name = name; |
| 1156 | wps->oob_conf.oob_method = wps_get_oob_method(method); |
| 1157 | |
| 1158 | if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) { |
| 1159 | /* |
| 1160 | * Use pre-configured DH keys in order to be able to write the |
| 1161 | * key hash into the OOB file. |
| 1162 | */ |
| 1163 | wpabuf_free(wps->dh_pubkey); |
| 1164 | wpabuf_free(wps->dh_privkey); |
| 1165 | wps->dh_privkey = NULL; |
| 1166 | wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP), |
| 1167 | &wps->dh_privkey); |
| 1168 | wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192); |
| 1169 | if (wps->dh_pubkey == NULL) { |
| 1170 | wpa_printf(MSG_ERROR, "WPS: Failed to initialize " |
| 1171 | "Diffie-Hellman handshake"); |
| 1172 | return -1; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | if (wps_process_oob(wps, oob_dev, 1) < 0) |
| 1177 | goto error; |
| 1178 | |
| 1179 | if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E || |
| 1180 | wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) && |
| 1181 | hostapd_wps_add_pin(hapd, NULL, "any", |
| 1182 | wpabuf_head(wps->oob_conf.dev_password), 0) < |
| 1183 | 0) |
| 1184 | goto error; |
| 1185 | |
| 1186 | return 0; |
| 1187 | |
| 1188 | error: |
| 1189 | wpabuf_free(wps->dh_pubkey); |
| 1190 | wps->dh_pubkey = NULL; |
| 1191 | wpabuf_free(wps->dh_privkey); |
| 1192 | wps->dh_privkey = NULL; |
| 1193 | return -1; |
| 1194 | } |
| 1195 | #endif /* CONFIG_WPS_OOB */ |
| 1196 | |
| 1197 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1198 | static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da, |
| 1199 | const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1200 | const u8 *ie, size_t ie_len, |
| 1201 | int ssi_signal) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1202 | { |
| 1203 | struct hostapd_data *hapd = ctx; |
| 1204 | struct wpabuf *wps_ie; |
| 1205 | struct ieee802_11_elems elems; |
| 1206 | |
| 1207 | if (hapd->wps == NULL) |
| 1208 | return 0; |
| 1209 | |
| 1210 | if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) { |
| 1211 | wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from " |
| 1212 | MACSTR, MAC2STR(addr)); |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | if (elems.ssid && elems.ssid_len > 0 && |
| 1217 | (elems.ssid_len != hapd->conf->ssid.ssid_len || |
| 1218 | os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != |
| 1219 | 0)) |
| 1220 | return 0; /* Not for us */ |
| 1221 | |
| 1222 | wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA); |
| 1223 | if (wps_ie == NULL) |
| 1224 | return 0; |
| 1225 | if (wps_validate_probe_req(wps_ie, addr) < 0) { |
| 1226 | wpabuf_free(wps_ie); |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
| 1230 | if (wpabuf_len(wps_ie) > 0) { |
| 1231 | int p2p_wildcard = 0; |
| 1232 | #ifdef CONFIG_P2P |
| 1233 | if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN && |
| 1234 | os_memcmp(elems.ssid, P2P_WILDCARD_SSID, |
| 1235 | P2P_WILDCARD_SSID_LEN) == 0) |
| 1236 | p2p_wildcard = 1; |
| 1237 | #endif /* CONFIG_P2P */ |
| 1238 | wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie, |
| 1239 | p2p_wildcard); |
| 1240 | #ifdef CONFIG_WPS_UPNP |
| 1241 | /* FIX: what exactly should be included in the WLANEvent? |
| 1242 | * WPS attributes? Full ProbeReq frame? */ |
| 1243 | if (!p2p_wildcard) |
| 1244 | upnp_wps_device_send_wlan_event( |
| 1245 | hapd->wps_upnp, addr, |
| 1246 | UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie); |
| 1247 | #endif /* CONFIG_WPS_UPNP */ |
| 1248 | } |
| 1249 | |
| 1250 | wpabuf_free(wps_ie); |
| 1251 | |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | #ifdef CONFIG_WPS_UPNP |
| 1257 | |
| 1258 | static int hostapd_rx_req_put_wlan_response( |
| 1259 | void *priv, enum upnp_wps_wlanevent_type ev_type, |
| 1260 | const u8 *mac_addr, const struct wpabuf *msg, |
| 1261 | enum wps_msg_type msg_type) |
| 1262 | { |
| 1263 | struct hostapd_data *hapd = priv; |
| 1264 | struct sta_info *sta; |
| 1265 | struct upnp_pending_message *p; |
| 1266 | |
| 1267 | wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr=" |
| 1268 | MACSTR, ev_type, MAC2STR(mac_addr)); |
| 1269 | wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage", |
| 1270 | wpabuf_head(msg), wpabuf_len(msg)); |
| 1271 | if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) { |
| 1272 | wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected " |
| 1273 | "PutWLANResponse WLANEventType %d", ev_type); |
| 1274 | return -1; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * EAP response to ongoing to WPS Registration. Send it to EAP-WSC |
| 1279 | * server implementation for delivery to the peer. |
| 1280 | */ |
| 1281 | |
| 1282 | sta = ap_get_sta(hapd, mac_addr); |
| 1283 | #ifndef CONFIG_WPS_STRICT |
| 1284 | if (!sta) { |
| 1285 | /* |
| 1286 | * Workaround - Intel wsccmd uses bogus NewWLANEventMAC: |
| 1287 | * Pick STA that is in an ongoing WPS registration without |
| 1288 | * checking the MAC address. |
| 1289 | */ |
| 1290 | wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based " |
| 1291 | "on NewWLANEventMAC; try wildcard match"); |
| 1292 | for (sta = hapd->sta_list; sta; sta = sta->next) { |
| 1293 | if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS)) |
| 1294 | break; |
| 1295 | } |
| 1296 | } |
| 1297 | #endif /* CONFIG_WPS_STRICT */ |
| 1298 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1299 | if (!sta || !(sta->flags & WLAN_STA_WPS)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1300 | wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found"); |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | p = os_zalloc(sizeof(*p)); |
| 1305 | if (p == NULL) |
| 1306 | return -1; |
| 1307 | os_memcpy(p->addr, sta->addr, ETH_ALEN); |
| 1308 | p->msg = wpabuf_dup(msg); |
| 1309 | p->type = msg_type; |
| 1310 | p->next = hapd->wps->upnp_msgs; |
| 1311 | hapd->wps->upnp_msgs = p; |
| 1312 | |
| 1313 | return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap); |
| 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | static int hostapd_wps_upnp_init(struct hostapd_data *hapd, |
| 1318 | struct wps_context *wps) |
| 1319 | { |
| 1320 | struct upnp_wps_device_ctx *ctx; |
| 1321 | |
| 1322 | if (!hapd->conf->upnp_iface) |
| 1323 | return 0; |
| 1324 | ctx = os_zalloc(sizeof(*ctx)); |
| 1325 | if (ctx == NULL) |
| 1326 | return -1; |
| 1327 | |
| 1328 | ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response; |
| 1329 | if (hapd->conf->ap_pin) |
| 1330 | ctx->ap_pin = os_strdup(hapd->conf->ap_pin); |
| 1331 | |
| 1332 | hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd, |
| 1333 | hapd->conf->upnp_iface); |
| 1334 | if (hapd->wps_upnp == NULL) |
| 1335 | return -1; |
| 1336 | wps->wps_upnp = hapd->wps_upnp; |
| 1337 | |
| 1338 | return 0; |
| 1339 | } |
| 1340 | |
| 1341 | |
| 1342 | static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd) |
| 1343 | { |
| 1344 | upnp_wps_device_deinit(hapd->wps_upnp, hapd); |
| 1345 | } |
| 1346 | |
| 1347 | #endif /* CONFIG_WPS_UPNP */ |
| 1348 | |
| 1349 | |
| 1350 | int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr, |
| 1351 | char *buf, size_t buflen) |
| 1352 | { |
| 1353 | if (hapd->wps == NULL) |
| 1354 | return 0; |
| 1355 | return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen); |
| 1356 | } |
| 1357 | |
| 1358 | |
| 1359 | static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx) |
| 1360 | { |
| 1361 | struct hostapd_data *hapd = eloop_data; |
| 1362 | wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out"); |
| 1363 | hostapd_wps_ap_pin_disable(hapd); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1364 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout) |
| 1369 | { |
| 1370 | wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout); |
| 1371 | hapd->ap_pin_failures = 0; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1372 | hapd->ap_pin_failures_consecutive = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1373 | hapd->conf->ap_setup_locked = 0; |
| 1374 | if (hapd->wps->ap_setup_locked) { |
| 1375 | wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED); |
| 1376 | hapd->wps->ap_setup_locked = 0; |
| 1377 | wps_registrar_update_ie(hapd->wps->registrar); |
| 1378 | } |
| 1379 | eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); |
| 1380 | if (timeout > 0) |
| 1381 | eloop_register_timeout(timeout, 0, |
| 1382 | hostapd_wps_ap_pin_timeout, hapd, NULL); |
| 1383 | } |
| 1384 | |
| 1385 | |
| 1386 | static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx) |
| 1387 | { |
| 1388 | os_free(hapd->conf->ap_pin); |
| 1389 | hapd->conf->ap_pin = NULL; |
| 1390 | #ifdef CONFIG_WPS_UPNP |
| 1391 | upnp_wps_set_ap_pin(hapd->wps_upnp, NULL); |
| 1392 | #endif /* CONFIG_WPS_UPNP */ |
| 1393 | eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL); |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd) |
| 1399 | { |
| 1400 | wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN"); |
| 1401 | hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL); |
| 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | struct wps_ap_pin_data { |
| 1406 | char pin_txt[9]; |
| 1407 | int timeout; |
| 1408 | }; |
| 1409 | |
| 1410 | |
| 1411 | static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx) |
| 1412 | { |
| 1413 | struct wps_ap_pin_data *data = ctx; |
| 1414 | os_free(hapd->conf->ap_pin); |
| 1415 | hapd->conf->ap_pin = os_strdup(data->pin_txt); |
| 1416 | #ifdef CONFIG_WPS_UPNP |
| 1417 | upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt); |
| 1418 | #endif /* CONFIG_WPS_UPNP */ |
| 1419 | hostapd_wps_ap_pin_enable(hapd, data->timeout); |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | |
| 1424 | const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout) |
| 1425 | { |
| 1426 | unsigned int pin; |
| 1427 | struct wps_ap_pin_data data; |
| 1428 | |
| 1429 | pin = wps_generate_pin(); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1430 | os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1431 | data.timeout = timeout; |
| 1432 | hostapd_wps_for_each(hapd, wps_ap_pin_set, &data); |
| 1433 | return hapd->conf->ap_pin; |
| 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd) |
| 1438 | { |
| 1439 | return hapd->conf->ap_pin; |
| 1440 | } |
| 1441 | |
| 1442 | |
| 1443 | int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin, |
| 1444 | int timeout) |
| 1445 | { |
| 1446 | struct wps_ap_pin_data data; |
| 1447 | int ret; |
| 1448 | |
| 1449 | ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin); |
| 1450 | if (ret < 0 || ret >= (int) sizeof(data.pin_txt)) |
| 1451 | return -1; |
| 1452 | data.timeout = timeout; |
| 1453 | return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data); |
| 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | static int wps_update_ie(struct hostapd_data *hapd, void *ctx) |
| 1458 | { |
| 1459 | if (hapd->wps) |
| 1460 | wps_registrar_update_ie(hapd->wps->registrar); |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | |
| 1465 | void hostapd_wps_update_ie(struct hostapd_data *hapd) |
| 1466 | { |
| 1467 | hostapd_wps_for_each(hapd, wps_update_ie, NULL); |
| 1468 | } |
| 1469 | |
| 1470 | |
| 1471 | int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid, |
| 1472 | const char *auth, const char *encr, const char *key) |
| 1473 | { |
| 1474 | struct wps_credential cred; |
| 1475 | size_t len; |
| 1476 | |
| 1477 | os_memset(&cred, 0, sizeof(cred)); |
| 1478 | |
| 1479 | len = os_strlen(ssid); |
| 1480 | if ((len & 1) || len > 2 * sizeof(cred.ssid) || |
| 1481 | hexstr2bin(ssid, cred.ssid, len / 2)) |
| 1482 | return -1; |
| 1483 | cred.ssid_len = len / 2; |
| 1484 | |
| 1485 | if (os_strncmp(auth, "OPEN", 4) == 0) |
| 1486 | cred.auth_type = WPS_AUTH_OPEN; |
| 1487 | else if (os_strncmp(auth, "WPAPSK", 6) == 0) |
| 1488 | cred.auth_type = WPS_AUTH_WPAPSK; |
| 1489 | else if (os_strncmp(auth, "WPA2PSK", 7) == 0) |
| 1490 | cred.auth_type = WPS_AUTH_WPA2PSK; |
| 1491 | else |
| 1492 | return -1; |
| 1493 | |
| 1494 | if (encr) { |
| 1495 | if (os_strncmp(encr, "NONE", 4) == 0) |
| 1496 | cred.encr_type = WPS_ENCR_NONE; |
| 1497 | else if (os_strncmp(encr, "WEP", 3) == 0) |
| 1498 | cred.encr_type = WPS_ENCR_WEP; |
| 1499 | else if (os_strncmp(encr, "TKIP", 4) == 0) |
| 1500 | cred.encr_type = WPS_ENCR_TKIP; |
| 1501 | else if (os_strncmp(encr, "CCMP", 4) == 0) |
| 1502 | cred.encr_type = WPS_ENCR_AES; |
| 1503 | else |
| 1504 | return -1; |
| 1505 | } else |
| 1506 | cred.encr_type = WPS_ENCR_NONE; |
| 1507 | |
| 1508 | if (key) { |
| 1509 | len = os_strlen(key); |
| 1510 | if ((len & 1) || len > 2 * sizeof(cred.key) || |
| 1511 | hexstr2bin(key, cred.key, len / 2)) |
| 1512 | return -1; |
| 1513 | cred.key_len = len / 2; |
| 1514 | } |
| 1515 | |
| 1516 | return wps_registrar_config_ap(hapd->wps->registrar, &cred); |
| 1517 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1518 | |
| 1519 | |
| 1520 | #ifdef CONFIG_WPS_NFC |
| 1521 | |
| 1522 | struct wps_nfc_password_token_data { |
| 1523 | const u8 *oob_dev_pw; |
| 1524 | size_t oob_dev_pw_len; |
| 1525 | int added; |
| 1526 | }; |
| 1527 | |
| 1528 | |
| 1529 | static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx) |
| 1530 | { |
| 1531 | struct wps_nfc_password_token_data *data = ctx; |
| 1532 | int ret; |
| 1533 | |
| 1534 | if (hapd->wps == NULL) |
| 1535 | return 0; |
| 1536 | ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar, |
| 1537 | data->oob_dev_pw, |
| 1538 | data->oob_dev_pw_len); |
| 1539 | if (ret == 0) |
| 1540 | data->added++; |
| 1541 | return ret; |
| 1542 | } |
| 1543 | |
| 1544 | |
| 1545 | static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd, |
| 1546 | struct wps_parse_attr *attr) |
| 1547 | { |
| 1548 | struct wps_nfc_password_token_data data; |
| 1549 | |
| 1550 | data.oob_dev_pw = attr->oob_dev_password; |
| 1551 | data.oob_dev_pw_len = attr->oob_dev_password_len; |
| 1552 | data.added = 0; |
| 1553 | if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0) |
| 1554 | return -1; |
| 1555 | return data.added ? 0 : -1; |
| 1556 | } |
| 1557 | |
| 1558 | |
| 1559 | static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd, |
| 1560 | const struct wpabuf *wps) |
| 1561 | { |
| 1562 | struct wps_parse_attr attr; |
| 1563 | |
| 1564 | wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps); |
| 1565 | |
| 1566 | if (wps_parse_msg(wps, &attr)) { |
| 1567 | wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag"); |
| 1568 | return -1; |
| 1569 | } |
| 1570 | |
| 1571 | if (attr.oob_dev_password) |
| 1572 | return hostapd_wps_add_nfc_password_token(hapd, &attr); |
| 1573 | |
| 1574 | wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag"); |
| 1575 | return -1; |
| 1576 | } |
| 1577 | |
| 1578 | |
| 1579 | int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd, |
| 1580 | const struct wpabuf *data) |
| 1581 | { |
| 1582 | const struct wpabuf *wps = data; |
| 1583 | struct wpabuf *tmp = NULL; |
| 1584 | int ret; |
| 1585 | |
| 1586 | if (wpabuf_len(data) < 4) |
| 1587 | return -1; |
| 1588 | |
| 1589 | if (*wpabuf_head_u8(data) != 0x10) { |
| 1590 | /* Assume this contains full NDEF record */ |
| 1591 | tmp = ndef_parse_wifi(data); |
| 1592 | if (tmp == NULL) { |
| 1593 | wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF"); |
| 1594 | return -1; |
| 1595 | } |
| 1596 | wps = tmp; |
| 1597 | } |
| 1598 | |
| 1599 | ret = hostapd_wps_nfc_tag_process(hapd, wps); |
| 1600 | wpabuf_free(tmp); |
| 1601 | return ret; |
| 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd, |
| 1606 | int ndef) |
| 1607 | { |
| 1608 | struct wpabuf *ret; |
| 1609 | |
| 1610 | if (hapd->wps == NULL) |
| 1611 | return NULL; |
| 1612 | |
| 1613 | ret = wps_get_oob_cred(hapd->wps); |
| 1614 | if (ndef && ret) { |
| 1615 | struct wpabuf *tmp; |
| 1616 | tmp = ndef_build_wifi(ret); |
| 1617 | wpabuf_free(ret); |
| 1618 | if (tmp == NULL) |
| 1619 | return NULL; |
| 1620 | ret = tmp; |
| 1621 | } |
| 1622 | |
| 1623 | return ret; |
| 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef) |
| 1628 | { |
| 1629 | return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id, |
| 1630 | &hapd->conf->wps_nfc_dh_pubkey, |
| 1631 | &hapd->conf->wps_nfc_dh_privkey, |
| 1632 | &hapd->conf->wps_nfc_dev_pw); |
| 1633 | } |
| 1634 | |
| 1635 | |
| 1636 | int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd) |
| 1637 | { |
| 1638 | struct wps_context *wps = hapd->wps; |
| 1639 | |
| 1640 | if (wps == NULL) |
| 1641 | return -1; |
| 1642 | |
| 1643 | if (!hapd->conf->wps_nfc_dh_pubkey || |
| 1644 | !hapd->conf->wps_nfc_dh_privkey || |
| 1645 | !hapd->conf->wps_nfc_dev_pw || |
| 1646 | !hapd->conf->wps_nfc_dev_pw_id) |
| 1647 | return -1; |
| 1648 | |
| 1649 | hostapd_wps_nfc_clear(wps); |
| 1650 | wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id; |
| 1651 | wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey); |
| 1652 | wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey); |
| 1653 | wps->ap_nfc_dev_pw = wpabuf_dup(hapd->conf->wps_nfc_dev_pw); |
| 1654 | |
| 1655 | if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey || |
| 1656 | !wps->ap_nfc_dev_pw) { |
| 1657 | hostapd_wps_nfc_clear(wps); |
| 1658 | return -1; |
| 1659 | } |
| 1660 | |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | |
| 1665 | void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd) |
| 1666 | { |
| 1667 | hostapd_wps_nfc_clear(hapd->wps); |
| 1668 | } |
| 1669 | |
| 1670 | #endif /* CONFIG_WPS_NFC */ |