Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * hostapd / Initialization and configuration |
| 3 | * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Alternatively, this software may be distributed under the terms of BSD |
| 10 | * license. |
| 11 | * |
| 12 | * See README and COPYING for more details. |
| 13 | */ |
| 14 | |
| 15 | #include "utils/includes.h" |
| 16 | |
| 17 | #include "utils/common.h" |
| 18 | #include "utils/eloop.h" |
| 19 | #include "common/ieee802_11_defs.h" |
| 20 | #include "radius/radius_client.h" |
| 21 | #include "drivers/driver.h" |
| 22 | #include "hostapd.h" |
| 23 | #include "authsrv.h" |
| 24 | #include "sta_info.h" |
| 25 | #include "accounting.h" |
| 26 | #include "ap_list.h" |
| 27 | #include "beacon.h" |
| 28 | #include "iapp.h" |
| 29 | #include "ieee802_1x.h" |
| 30 | #include "ieee802_11_auth.h" |
| 31 | #include "vlan_init.h" |
| 32 | #include "wpa_auth.h" |
| 33 | #include "wps_hostapd.h" |
| 34 | #include "hw_features.h" |
| 35 | #include "wpa_auth_glue.h" |
| 36 | #include "ap_drv_ops.h" |
| 37 | #include "ap_config.h" |
| 38 | #include "p2p_hostapd.h" |
| 39 | |
| 40 | |
| 41 | static int hostapd_flush_old_stations(struct hostapd_data *hapd); |
| 42 | static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd); |
| 43 | |
| 44 | extern int wpa_debug_level; |
| 45 | |
| 46 | |
| 47 | static void hostapd_reload_bss(struct hostapd_data *hapd) |
| 48 | { |
| 49 | #ifndef CONFIG_NO_RADIUS |
| 50 | radius_client_reconfig(hapd->radius, hapd->conf->radius); |
| 51 | #endif /* CONFIG_NO_RADIUS */ |
| 52 | |
| 53 | if (hostapd_setup_wpa_psk(hapd->conf)) { |
| 54 | wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK " |
| 55 | "after reloading configuration"); |
| 56 | } |
| 57 | |
| 58 | if (hapd->conf->ieee802_1x || hapd->conf->wpa) |
| 59 | hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1); |
| 60 | else |
| 61 | hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0); |
| 62 | |
| 63 | if (hapd->conf->wpa && hapd->wpa_auth == NULL) |
| 64 | hostapd_setup_wpa(hapd); |
| 65 | else if (hapd->conf->wpa) { |
| 66 | const u8 *wpa_ie; |
| 67 | size_t wpa_ie_len; |
| 68 | hostapd_reconfig_wpa(hapd); |
| 69 | wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len); |
| 70 | if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) |
| 71 | wpa_printf(MSG_ERROR, "Failed to configure WPA IE for " |
| 72 | "the kernel driver."); |
| 73 | } else if (hapd->wpa_auth) { |
| 74 | wpa_deinit(hapd->wpa_auth); |
| 75 | hapd->wpa_auth = NULL; |
| 76 | hostapd_set_privacy(hapd, 0); |
| 77 | hostapd_setup_encryption(hapd->conf->iface, hapd); |
| 78 | hostapd_set_generic_elem(hapd, (u8 *) "", 0); |
| 79 | } |
| 80 | |
| 81 | ieee802_11_set_beacon(hapd); |
| 82 | hostapd_update_wps(hapd); |
| 83 | |
| 84 | if (hapd->conf->ssid.ssid_set && |
| 85 | hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid, |
| 86 | hapd->conf->ssid.ssid_len)) { |
| 87 | wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver"); |
| 88 | /* try to continue */ |
| 89 | } |
| 90 | wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | int hostapd_reload_config(struct hostapd_iface *iface) |
| 95 | { |
| 96 | struct hostapd_data *hapd = iface->bss[0]; |
| 97 | struct hostapd_config *newconf, *oldconf; |
| 98 | size_t j; |
| 99 | |
| 100 | if (iface->config_read_cb == NULL) |
| 101 | return -1; |
| 102 | newconf = iface->config_read_cb(iface->config_fname); |
| 103 | if (newconf == NULL) |
| 104 | return -1; |
| 105 | |
| 106 | /* |
| 107 | * Deauthenticate all stations since the new configuration may not |
| 108 | * allow them to use the BSS anymore. |
| 109 | */ |
| 110 | for (j = 0; j < iface->num_bss; j++) { |
| 111 | hostapd_flush_old_stations(iface->bss[j]); |
| 112 | |
| 113 | #ifndef CONFIG_NO_RADIUS |
| 114 | /* TODO: update dynamic data based on changed configuration |
| 115 | * items (e.g., open/close sockets, etc.) */ |
| 116 | radius_client_flush(iface->bss[j]->radius, 0); |
| 117 | #endif /* CONFIG_NO_RADIUS */ |
| 118 | } |
| 119 | |
| 120 | oldconf = hapd->iconf; |
| 121 | iface->conf = newconf; |
| 122 | |
| 123 | for (j = 0; j < iface->num_bss; j++) { |
| 124 | hapd = iface->bss[j]; |
| 125 | hapd->iconf = newconf; |
| 126 | hapd->conf = &newconf->bss[j]; |
| 127 | hostapd_reload_bss(hapd); |
| 128 | } |
| 129 | |
| 130 | hostapd_config_free(oldconf); |
| 131 | |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd, |
| 138 | char *ifname) |
| 139 | { |
| 140 | int i; |
| 141 | |
| 142 | for (i = 0; i < NUM_WEP_KEYS; i++) { |
| 143 | if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, |
| 144 | 0, NULL, 0, NULL, 0)) { |
| 145 | wpa_printf(MSG_DEBUG, "Failed to clear default " |
| 146 | "encryption keys (ifname=%s keyidx=%d)", |
| 147 | ifname, i); |
| 148 | } |
| 149 | } |
| 150 | #ifdef CONFIG_IEEE80211W |
| 151 | if (hapd->conf->ieee80211w) { |
| 152 | for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) { |
| 153 | if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, |
| 154 | NULL, i, 0, NULL, |
| 155 | 0, NULL, 0)) { |
| 156 | wpa_printf(MSG_DEBUG, "Failed to clear " |
| 157 | "default mgmt encryption keys " |
| 158 | "(ifname=%s keyidx=%d)", ifname, i); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | #endif /* CONFIG_IEEE80211W */ |
| 163 | } |
| 164 | |
| 165 | |
| 166 | static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd) |
| 167 | { |
| 168 | hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static int hostapd_broadcast_wep_set(struct hostapd_data *hapd) |
| 174 | { |
| 175 | int errors = 0, idx; |
| 176 | struct hostapd_ssid *ssid = &hapd->conf->ssid; |
| 177 | |
| 178 | idx = ssid->wep.idx; |
| 179 | if (ssid->wep.default_len && |
| 180 | hostapd_drv_set_key(hapd->conf->iface, |
| 181 | hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, |
| 182 | 1, NULL, 0, ssid->wep.key[idx], |
| 183 | ssid->wep.len[idx])) { |
| 184 | wpa_printf(MSG_WARNING, "Could not set WEP encryption."); |
| 185 | errors++; |
| 186 | } |
| 187 | |
| 188 | if (ssid->dyn_vlan_keys) { |
| 189 | size_t i; |
| 190 | for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) { |
| 191 | const char *ifname; |
| 192 | struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i]; |
| 193 | if (key == NULL) |
| 194 | continue; |
| 195 | ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, |
| 196 | i); |
| 197 | if (ifname == NULL) |
| 198 | continue; |
| 199 | |
| 200 | idx = key->idx; |
| 201 | if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_WEP, |
| 202 | broadcast_ether_addr, idx, 1, |
| 203 | NULL, 0, key->key[idx], |
| 204 | key->len[idx])) { |
| 205 | wpa_printf(MSG_WARNING, "Could not set " |
| 206 | "dynamic VLAN WEP encryption."); |
| 207 | errors++; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return errors; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * hostapd_cleanup - Per-BSS cleanup (deinitialization) |
| 217 | * @hapd: Pointer to BSS data |
| 218 | * |
| 219 | * This function is used to free all per-BSS data structures and resources. |
| 220 | * This gets called in a loop for each BSS between calls to |
| 221 | * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface |
| 222 | * is deinitialized. Most of the modules that are initialized in |
| 223 | * hostapd_setup_bss() are deinitialized here. |
| 224 | */ |
| 225 | static void hostapd_cleanup(struct hostapd_data *hapd) |
| 226 | { |
| 227 | if (hapd->iface->ctrl_iface_deinit) |
| 228 | hapd->iface->ctrl_iface_deinit(hapd); |
| 229 | |
| 230 | iapp_deinit(hapd->iapp); |
| 231 | hapd->iapp = NULL; |
| 232 | accounting_deinit(hapd); |
| 233 | hostapd_deinit_wpa(hapd); |
| 234 | vlan_deinit(hapd); |
| 235 | hostapd_acl_deinit(hapd); |
| 236 | #ifndef CONFIG_NO_RADIUS |
| 237 | radius_client_deinit(hapd->radius); |
| 238 | hapd->radius = NULL; |
| 239 | #endif /* CONFIG_NO_RADIUS */ |
| 240 | |
| 241 | hostapd_deinit_wps(hapd); |
| 242 | |
| 243 | authsrv_deinit(hapd); |
| 244 | |
| 245 | if (hapd->interface_added && |
| 246 | hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) { |
| 247 | wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s", |
| 248 | hapd->conf->iface); |
| 249 | } |
| 250 | |
| 251 | os_free(hapd->probereq_cb); |
| 252 | hapd->probereq_cb = NULL; |
| 253 | |
| 254 | #ifdef CONFIG_P2P |
| 255 | wpabuf_free(hapd->p2p_beacon_ie); |
| 256 | hapd->p2p_beacon_ie = NULL; |
| 257 | wpabuf_free(hapd->p2p_probe_resp_ie); |
| 258 | hapd->p2p_probe_resp_ie = NULL; |
| 259 | #endif /* CONFIG_P2P */ |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /** |
| 264 | * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup |
| 265 | * @iface: Pointer to interface data |
| 266 | * |
| 267 | * This function is called before per-BSS data structures are deinitialized |
| 268 | * with hostapd_cleanup(). |
| 269 | */ |
| 270 | static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface) |
| 271 | { |
| 272 | } |
| 273 | |
| 274 | |
| 275 | /** |
| 276 | * hostapd_cleanup_iface - Complete per-interface cleanup |
| 277 | * @iface: Pointer to interface data |
| 278 | * |
| 279 | * This function is called after per-BSS data structures are deinitialized |
| 280 | * with hostapd_cleanup(). |
| 281 | */ |
| 282 | static void hostapd_cleanup_iface(struct hostapd_iface *iface) |
| 283 | { |
| 284 | hostapd_free_hw_features(iface->hw_features, iface->num_hw_features); |
| 285 | iface->hw_features = NULL; |
| 286 | os_free(iface->current_rates); |
| 287 | iface->current_rates = NULL; |
| 288 | ap_list_deinit(iface); |
| 289 | hostapd_config_free(iface->conf); |
| 290 | iface->conf = NULL; |
| 291 | |
| 292 | os_free(iface->config_fname); |
| 293 | os_free(iface->bss); |
| 294 | os_free(iface); |
| 295 | } |
| 296 | |
| 297 | |
| 298 | static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd) |
| 299 | { |
| 300 | int i; |
| 301 | |
| 302 | hostapd_broadcast_wep_set(hapd); |
| 303 | |
| 304 | if (hapd->conf->ssid.wep.default_len) { |
| 305 | hostapd_set_privacy(hapd, 1); |
| 306 | return 0; |
| 307 | } |
| 308 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame^] | 309 | /* |
| 310 | * When IEEE 802.1X is not enabled, the driver may need to know how to |
| 311 | * set authentication algorithms for static WEP. |
| 312 | */ |
| 313 | hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs); |
| 314 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 315 | for (i = 0; i < 4; i++) { |
| 316 | if (hapd->conf->ssid.wep.key[i] && |
| 317 | hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, |
| 318 | i == hapd->conf->ssid.wep.idx, NULL, 0, |
| 319 | hapd->conf->ssid.wep.key[i], |
| 320 | hapd->conf->ssid.wep.len[i])) { |
| 321 | wpa_printf(MSG_WARNING, "Could not set WEP " |
| 322 | "encryption."); |
| 323 | return -1; |
| 324 | } |
| 325 | if (hapd->conf->ssid.wep.key[i] && |
| 326 | i == hapd->conf->ssid.wep.idx) |
| 327 | hostapd_set_privacy(hapd, 1); |
| 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | static int hostapd_flush_old_stations(struct hostapd_data *hapd) |
| 335 | { |
| 336 | int ret = 0; |
| 337 | u8 addr[ETH_ALEN]; |
| 338 | |
| 339 | if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL) |
| 340 | return 0; |
| 341 | |
| 342 | wpa_printf(MSG_DEBUG, "Flushing old station entries"); |
| 343 | if (hostapd_flush(hapd)) { |
| 344 | wpa_printf(MSG_WARNING, "Could not connect to kernel driver."); |
| 345 | ret = -1; |
| 346 | } |
| 347 | wpa_printf(MSG_DEBUG, "Deauthenticate all stations"); |
| 348 | os_memset(addr, 0xff, ETH_ALEN); |
| 349 | hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID); |
| 350 | hostapd_free_stas(hapd); |
| 351 | |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /** |
| 357 | * hostapd_validate_bssid_configuration - Validate BSSID configuration |
| 358 | * @iface: Pointer to interface data |
| 359 | * Returns: 0 on success, -1 on failure |
| 360 | * |
| 361 | * This function is used to validate that the configured BSSIDs are valid. |
| 362 | */ |
| 363 | static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface) |
| 364 | { |
| 365 | u8 mask[ETH_ALEN] = { 0 }; |
| 366 | struct hostapd_data *hapd = iface->bss[0]; |
| 367 | unsigned int i = iface->conf->num_bss, bits = 0, j; |
| 368 | int res; |
| 369 | int auto_addr = 0; |
| 370 | |
| 371 | if (hostapd_drv_none(hapd)) |
| 372 | return 0; |
| 373 | |
| 374 | /* Generate BSSID mask that is large enough to cover the BSSIDs. */ |
| 375 | |
| 376 | /* Determine the bits necessary to cover the number of BSSIDs. */ |
| 377 | for (i--; i; i >>= 1) |
| 378 | bits++; |
| 379 | |
| 380 | /* Determine the bits necessary to any configured BSSIDs, |
| 381 | if they are higher than the number of BSSIDs. */ |
| 382 | for (j = 0; j < iface->conf->num_bss; j++) { |
| 383 | if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) { |
| 384 | if (j) |
| 385 | auto_addr++; |
| 386 | continue; |
| 387 | } |
| 388 | |
| 389 | for (i = 0; i < ETH_ALEN; i++) { |
| 390 | mask[i] |= |
| 391 | iface->conf->bss[j].bssid[i] ^ |
| 392 | hapd->own_addr[i]; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | if (!auto_addr) |
| 397 | goto skip_mask_ext; |
| 398 | |
| 399 | for (i = 0; i < ETH_ALEN && mask[i] == 0; i++) |
| 400 | ; |
| 401 | j = 0; |
| 402 | if (i < ETH_ALEN) { |
| 403 | j = (5 - i) * 8; |
| 404 | |
| 405 | while (mask[i] != 0) { |
| 406 | mask[i] >>= 1; |
| 407 | j++; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (bits < j) |
| 412 | bits = j; |
| 413 | |
| 414 | if (bits > 40) { |
| 415 | wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)", |
| 416 | bits); |
| 417 | return -1; |
| 418 | } |
| 419 | |
| 420 | os_memset(mask, 0xff, ETH_ALEN); |
| 421 | j = bits / 8; |
| 422 | for (i = 5; i > 5 - j; i--) |
| 423 | mask[i] = 0; |
| 424 | j = bits % 8; |
| 425 | while (j--) |
| 426 | mask[i] <<= 1; |
| 427 | |
| 428 | skip_mask_ext: |
| 429 | wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)", |
| 430 | (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits); |
| 431 | |
| 432 | res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask); |
| 433 | if (res == 0) |
| 434 | return 0; |
| 435 | |
| 436 | if (res < 0) { |
| 437 | wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask " |
| 438 | MACSTR " for start address " MACSTR ".", |
| 439 | MAC2STR(mask), MAC2STR(hapd->own_addr)); |
| 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | if (!auto_addr) |
| 444 | return 0; |
| 445 | |
| 446 | for (i = 0; i < ETH_ALEN; i++) { |
| 447 | if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) { |
| 448 | wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR |
| 449 | " for start address " MACSTR ".", |
| 450 | MAC2STR(mask), MAC2STR(hapd->own_addr)); |
| 451 | wpa_printf(MSG_ERROR, "Start address must be the " |
| 452 | "first address in the block (i.e., addr " |
| 453 | "AND mask == addr)."); |
| 454 | return -1; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | static int mac_in_conf(struct hostapd_config *conf, const void *a) |
| 463 | { |
| 464 | size_t i; |
| 465 | |
| 466 | for (i = 0; i < conf->num_bss; i++) { |
| 467 | if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) { |
| 468 | return 1; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | /** |
| 479 | * hostapd_setup_bss - Per-BSS setup (initialization) |
| 480 | * @hapd: Pointer to BSS data |
| 481 | * @first: Whether this BSS is the first BSS of an interface |
| 482 | * |
| 483 | * This function is used to initialize all per-BSS data structures and |
| 484 | * resources. This gets called in a loop for each BSS when an interface is |
| 485 | * initialized. Most of the modules that are initialized here will be |
| 486 | * deinitialized in hostapd_cleanup(). |
| 487 | */ |
| 488 | static int hostapd_setup_bss(struct hostapd_data *hapd, int first) |
| 489 | { |
| 490 | struct hostapd_bss_config *conf = hapd->conf; |
| 491 | u8 ssid[HOSTAPD_MAX_SSID_LEN + 1]; |
| 492 | int ssid_len, set_ssid; |
| 493 | char force_ifname[IFNAMSIZ]; |
| 494 | u8 if_addr[ETH_ALEN]; |
| 495 | |
| 496 | if (!first) { |
| 497 | if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) { |
| 498 | /* Allocate the next available BSSID. */ |
| 499 | do { |
| 500 | inc_byte_array(hapd->own_addr, ETH_ALEN); |
| 501 | } while (mac_in_conf(hapd->iconf, hapd->own_addr)); |
| 502 | } else { |
| 503 | /* Allocate the configured BSSID. */ |
| 504 | os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN); |
| 505 | |
| 506 | if (hostapd_mac_comp(hapd->own_addr, |
| 507 | hapd->iface->bss[0]->own_addr) == |
| 508 | 0) { |
| 509 | wpa_printf(MSG_ERROR, "BSS '%s' may not have " |
| 510 | "BSSID set to the MAC address of " |
| 511 | "the radio", hapd->conf->iface); |
| 512 | return -1; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | hapd->interface_added = 1; |
| 517 | if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS, |
| 518 | hapd->conf->iface, hapd->own_addr, hapd, |
| 519 | &hapd->drv_priv, force_ifname, if_addr, |
| 520 | hapd->conf->bridge[0] ? hapd->conf->bridge : |
| 521 | NULL)) { |
| 522 | wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID=" |
| 523 | MACSTR ")", MAC2STR(hapd->own_addr)); |
| 524 | return -1; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | if (conf->wmm_enabled < 0) |
| 529 | conf->wmm_enabled = hapd->iconf->ieee80211n; |
| 530 | |
| 531 | hostapd_flush_old_stations(hapd); |
| 532 | hostapd_set_privacy(hapd, 0); |
| 533 | |
| 534 | hostapd_broadcast_wep_clear(hapd); |
| 535 | if (hostapd_setup_encryption(hapd->conf->iface, hapd)) |
| 536 | return -1; |
| 537 | |
| 538 | /* |
| 539 | * Fetch the SSID from the system and use it or, |
| 540 | * if one was specified in the config file, verify they |
| 541 | * match. |
| 542 | */ |
| 543 | ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid)); |
| 544 | if (ssid_len < 0) { |
| 545 | wpa_printf(MSG_ERROR, "Could not read SSID from system"); |
| 546 | return -1; |
| 547 | } |
| 548 | if (conf->ssid.ssid_set) { |
| 549 | /* |
| 550 | * If SSID is specified in the config file and it differs |
| 551 | * from what is being used then force installation of the |
| 552 | * new SSID. |
| 553 | */ |
| 554 | set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len || |
| 555 | os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0); |
| 556 | } else { |
| 557 | /* |
| 558 | * No SSID in the config file; just use the one we got |
| 559 | * from the system. |
| 560 | */ |
| 561 | set_ssid = 0; |
| 562 | conf->ssid.ssid_len = ssid_len; |
| 563 | os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len); |
| 564 | conf->ssid.ssid[conf->ssid.ssid_len] = '\0'; |
| 565 | } |
| 566 | |
| 567 | if (!hostapd_drv_none(hapd)) { |
| 568 | wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR |
| 569 | " and ssid '%s'", |
| 570 | hapd->conf->iface, MAC2STR(hapd->own_addr), |
| 571 | hapd->conf->ssid.ssid); |
| 572 | } |
| 573 | |
| 574 | if (hostapd_setup_wpa_psk(conf)) { |
| 575 | wpa_printf(MSG_ERROR, "WPA-PSK setup failed."); |
| 576 | return -1; |
| 577 | } |
| 578 | |
| 579 | /* Set SSID for the kernel driver (to be used in beacon and probe |
| 580 | * response frames) */ |
| 581 | if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid, |
| 582 | conf->ssid.ssid_len)) { |
| 583 | wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver"); |
| 584 | return -1; |
| 585 | } |
| 586 | |
| 587 | if (wpa_debug_level == MSG_MSGDUMP) |
| 588 | conf->radius->msg_dumps = 1; |
| 589 | #ifndef CONFIG_NO_RADIUS |
| 590 | hapd->radius = radius_client_init(hapd, conf->radius); |
| 591 | if (hapd->radius == NULL) { |
| 592 | wpa_printf(MSG_ERROR, "RADIUS client initialization failed."); |
| 593 | return -1; |
| 594 | } |
| 595 | #endif /* CONFIG_NO_RADIUS */ |
| 596 | |
| 597 | if (hostapd_acl_init(hapd)) { |
| 598 | wpa_printf(MSG_ERROR, "ACL initialization failed."); |
| 599 | return -1; |
| 600 | } |
| 601 | if (hostapd_init_wps(hapd, conf)) |
| 602 | return -1; |
| 603 | |
| 604 | if (authsrv_init(hapd) < 0) |
| 605 | return -1; |
| 606 | |
| 607 | if (ieee802_1x_init(hapd)) { |
| 608 | wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed."); |
| 609 | return -1; |
| 610 | } |
| 611 | |
| 612 | if (hapd->conf->wpa && hostapd_setup_wpa(hapd)) |
| 613 | return -1; |
| 614 | |
| 615 | if (accounting_init(hapd)) { |
| 616 | wpa_printf(MSG_ERROR, "Accounting initialization failed."); |
| 617 | return -1; |
| 618 | } |
| 619 | |
| 620 | if (hapd->conf->ieee802_11f && |
| 621 | (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) { |
| 622 | wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization " |
| 623 | "failed."); |
| 624 | return -1; |
| 625 | } |
| 626 | |
| 627 | if (hapd->iface->ctrl_iface_init && |
| 628 | hapd->iface->ctrl_iface_init(hapd)) { |
| 629 | wpa_printf(MSG_ERROR, "Failed to setup control interface"); |
| 630 | return -1; |
| 631 | } |
| 632 | |
| 633 | if (!hostapd_drv_none(hapd) && vlan_init(hapd)) { |
| 634 | wpa_printf(MSG_ERROR, "VLAN initialization failed."); |
| 635 | return -1; |
| 636 | } |
| 637 | |
| 638 | ieee802_11_set_beacon(hapd); |
| 639 | |
| 640 | if (hapd->driver && hapd->driver->set_operstate) |
| 641 | hapd->driver->set_operstate(hapd->drv_priv, 1); |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | |
| 647 | static void hostapd_tx_queue_params(struct hostapd_iface *iface) |
| 648 | { |
| 649 | struct hostapd_data *hapd = iface->bss[0]; |
| 650 | int i; |
| 651 | struct hostapd_tx_queue_params *p; |
| 652 | |
| 653 | for (i = 0; i < NUM_TX_QUEUES; i++) { |
| 654 | p = &iface->conf->tx_queue[i]; |
| 655 | |
| 656 | if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin, |
| 657 | p->cwmax, p->burst)) { |
| 658 | wpa_printf(MSG_DEBUG, "Failed to set TX queue " |
| 659 | "parameters for queue %d.", i); |
| 660 | /* Continue anyway */ |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | |
| 666 | static int setup_interface(struct hostapd_iface *iface) |
| 667 | { |
| 668 | struct hostapd_data *hapd = iface->bss[0]; |
| 669 | size_t i; |
| 670 | char country[4]; |
| 671 | |
| 672 | /* |
| 673 | * Make sure that all BSSes get configured with a pointer to the same |
| 674 | * driver interface. |
| 675 | */ |
| 676 | for (i = 1; i < iface->num_bss; i++) { |
| 677 | iface->bss[i]->driver = hapd->driver; |
| 678 | iface->bss[i]->drv_priv = hapd->drv_priv; |
| 679 | } |
| 680 | |
| 681 | if (hostapd_validate_bssid_configuration(iface)) |
| 682 | return -1; |
| 683 | |
| 684 | if (hapd->iconf->country[0] && hapd->iconf->country[1]) { |
| 685 | os_memcpy(country, hapd->iconf->country, 3); |
| 686 | country[3] = '\0'; |
| 687 | if (hostapd_set_country(hapd, country) < 0) { |
| 688 | wpa_printf(MSG_ERROR, "Failed to set country code"); |
| 689 | return -1; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (hostapd_get_hw_features(iface)) { |
| 694 | /* Not all drivers support this yet, so continue without hw |
| 695 | * feature data. */ |
| 696 | } else { |
| 697 | int ret = hostapd_select_hw_mode(iface); |
| 698 | if (ret < 0) { |
| 699 | wpa_printf(MSG_ERROR, "Could not select hw_mode and " |
| 700 | "channel. (%d)", ret); |
| 701 | return -1; |
| 702 | } |
| 703 | ret = hostapd_check_ht_capab(iface); |
| 704 | if (ret < 0) |
| 705 | return -1; |
| 706 | if (ret == 1) { |
| 707 | wpa_printf(MSG_DEBUG, "Interface initialization will " |
| 708 | "be completed in a callback"); |
| 709 | return 0; |
| 710 | } |
| 711 | } |
| 712 | return hostapd_setup_interface_complete(iface, 0); |
| 713 | } |
| 714 | |
| 715 | |
| 716 | int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err) |
| 717 | { |
| 718 | struct hostapd_data *hapd = iface->bss[0]; |
| 719 | size_t j; |
| 720 | u8 *prev_addr; |
| 721 | |
| 722 | if (err) { |
| 723 | wpa_printf(MSG_ERROR, "Interface initialization failed"); |
| 724 | eloop_terminate(); |
| 725 | return -1; |
| 726 | } |
| 727 | |
| 728 | wpa_printf(MSG_DEBUG, "Completing interface initialization"); |
| 729 | if (hapd->iconf->channel) { |
| 730 | iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel); |
| 731 | wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d " |
| 732 | "Frequency: %d MHz", |
| 733 | hostapd_hw_mode_txt(hapd->iconf->hw_mode), |
| 734 | hapd->iconf->channel, iface->freq); |
| 735 | |
| 736 | if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq, |
| 737 | hapd->iconf->channel, |
| 738 | hapd->iconf->ieee80211n, |
| 739 | hapd->iconf->secondary_channel)) { |
| 740 | wpa_printf(MSG_ERROR, "Could not set channel for " |
| 741 | "kernel driver"); |
| 742 | return -1; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | if (iface->current_mode) { |
| 747 | if (hostapd_prepare_rates(hapd, iface->current_mode)) { |
| 748 | wpa_printf(MSG_ERROR, "Failed to prepare rates " |
| 749 | "table."); |
| 750 | hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211, |
| 751 | HOSTAPD_LEVEL_WARNING, |
| 752 | "Failed to prepare rates table."); |
| 753 | return -1; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if (hapd->iconf->rts_threshold > -1 && |
| 758 | hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) { |
| 759 | wpa_printf(MSG_ERROR, "Could not set RTS threshold for " |
| 760 | "kernel driver"); |
| 761 | return -1; |
| 762 | } |
| 763 | |
| 764 | if (hapd->iconf->fragm_threshold > -1 && |
| 765 | hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) { |
| 766 | wpa_printf(MSG_ERROR, "Could not set fragmentation threshold " |
| 767 | "for kernel driver"); |
| 768 | return -1; |
| 769 | } |
| 770 | |
| 771 | prev_addr = hapd->own_addr; |
| 772 | |
| 773 | for (j = 0; j < iface->num_bss; j++) { |
| 774 | hapd = iface->bss[j]; |
| 775 | if (j) |
| 776 | os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN); |
| 777 | if (hostapd_setup_bss(hapd, j == 0)) |
| 778 | return -1; |
| 779 | if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) |
| 780 | prev_addr = hapd->own_addr; |
| 781 | } |
| 782 | |
| 783 | hostapd_tx_queue_params(iface); |
| 784 | |
| 785 | ap_list_init(iface); |
| 786 | |
| 787 | if (hostapd_driver_commit(hapd) < 0) { |
| 788 | wpa_printf(MSG_ERROR, "%s: Failed to commit driver " |
| 789 | "configuration", __func__); |
| 790 | return -1; |
| 791 | } |
| 792 | |
Jouni Malinen | 87fd279 | 2011-05-16 18:35:42 +0300 | [diff] [blame] | 793 | /* |
| 794 | * WPS UPnP module can be initialized only when the "upnp_iface" is up. |
| 795 | * If "interface" and "upnp_iface" are the same (e.g., non-bridge |
| 796 | * mode), the interface is up only after driver_commit, so initialize |
| 797 | * WPS after driver_commit. |
| 798 | */ |
| 799 | for (j = 0; j < iface->num_bss; j++) { |
| 800 | if (hostapd_init_wps_complete(iface->bss[j])) |
| 801 | return -1; |
| 802 | } |
| 803 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 804 | if (hapd->setup_complete_cb) |
| 805 | hapd->setup_complete_cb(hapd->setup_complete_cb_ctx); |
| 806 | |
| 807 | wpa_printf(MSG_DEBUG, "%s: Setup of interface done.", |
| 808 | iface->bss[0]->conf->iface); |
| 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | |
| 814 | /** |
| 815 | * hostapd_setup_interface - Setup of an interface |
| 816 | * @iface: Pointer to interface data. |
| 817 | * Returns: 0 on success, -1 on failure |
| 818 | * |
| 819 | * Initializes the driver interface, validates the configuration, |
| 820 | * and sets driver parameters based on the configuration. |
| 821 | * Flushes old stations, sets the channel, encryption, |
| 822 | * beacons, and WDS links based on the configuration. |
| 823 | */ |
| 824 | int hostapd_setup_interface(struct hostapd_iface *iface) |
| 825 | { |
| 826 | int ret; |
| 827 | |
| 828 | ret = setup_interface(iface); |
| 829 | if (ret) { |
| 830 | wpa_printf(MSG_ERROR, "%s: Unable to setup interface.", |
| 831 | iface->bss[0]->conf->iface); |
| 832 | return -1; |
| 833 | } |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | |
| 839 | /** |
| 840 | * hostapd_alloc_bss_data - Allocate and initialize per-BSS data |
| 841 | * @hapd_iface: Pointer to interface data |
| 842 | * @conf: Pointer to per-interface configuration |
| 843 | * @bss: Pointer to per-BSS configuration for this BSS |
| 844 | * Returns: Pointer to allocated BSS data |
| 845 | * |
| 846 | * This function is used to allocate per-BSS data structure. This data will be |
| 847 | * freed after hostapd_cleanup() is called for it during interface |
| 848 | * deinitialization. |
| 849 | */ |
| 850 | struct hostapd_data * |
| 851 | hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface, |
| 852 | struct hostapd_config *conf, |
| 853 | struct hostapd_bss_config *bss) |
| 854 | { |
| 855 | struct hostapd_data *hapd; |
| 856 | |
| 857 | hapd = os_zalloc(sizeof(*hapd)); |
| 858 | if (hapd == NULL) |
| 859 | return NULL; |
| 860 | |
| 861 | hapd->new_assoc_sta_cb = hostapd_new_assoc_sta; |
| 862 | hapd->iconf = conf; |
| 863 | hapd->conf = bss; |
| 864 | hapd->iface = hapd_iface; |
| 865 | hapd->driver = hapd->iconf->driver; |
| 866 | |
| 867 | return hapd; |
| 868 | } |
| 869 | |
| 870 | |
| 871 | void hostapd_interface_deinit(struct hostapd_iface *iface) |
| 872 | { |
| 873 | size_t j; |
| 874 | |
| 875 | if (iface == NULL) |
| 876 | return; |
| 877 | |
| 878 | hostapd_cleanup_iface_pre(iface); |
| 879 | for (j = 0; j < iface->num_bss; j++) { |
| 880 | struct hostapd_data *hapd = iface->bss[j]; |
| 881 | hostapd_free_stas(hapd); |
| 882 | hostapd_flush_old_stations(hapd); |
| 883 | hostapd_cleanup(hapd); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
| 888 | void hostapd_interface_free(struct hostapd_iface *iface) |
| 889 | { |
| 890 | size_t j; |
| 891 | for (j = 0; j < iface->num_bss; j++) |
| 892 | os_free(iface->bss[j]); |
| 893 | hostapd_cleanup_iface(iface); |
| 894 | } |
| 895 | |
| 896 | |
| 897 | /** |
| 898 | * hostapd_new_assoc_sta - Notify that a new station associated with the AP |
| 899 | * @hapd: Pointer to BSS data |
| 900 | * @sta: Pointer to the associated STA data |
| 901 | * @reassoc: 1 to indicate this was a re-association; 0 = first association |
| 902 | * |
| 903 | * This function will be called whenever a station associates with the AP. It |
| 904 | * can be called from ieee802_11.c for drivers that export MLME to hostapd and |
| 905 | * from drv_callbacks.c based on driver events for drivers that take care of |
| 906 | * management frames (IEEE 802.11 authentication and association) internally. |
| 907 | */ |
| 908 | void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta, |
| 909 | int reassoc) |
| 910 | { |
| 911 | if (hapd->tkip_countermeasures) { |
| 912 | hostapd_drv_sta_deauth(hapd, sta->addr, |
| 913 | WLAN_REASON_MICHAEL_MIC_FAILURE); |
| 914 | return; |
| 915 | } |
| 916 | |
| 917 | hostapd_prune_associations(hapd, sta->addr); |
| 918 | |
| 919 | /* IEEE 802.11F (IAPP) */ |
| 920 | if (hapd->conf->ieee802_11f) |
| 921 | iapp_new_station(hapd->iapp, sta); |
| 922 | |
| 923 | #ifdef CONFIG_P2P |
| 924 | if (sta->p2p_ie == NULL && !sta->no_p2p_set) { |
| 925 | sta->no_p2p_set = 1; |
| 926 | hapd->num_sta_no_p2p++; |
| 927 | if (hapd->num_sta_no_p2p == 1) |
| 928 | hostapd_p2p_non_p2p_sta_connected(hapd); |
| 929 | } |
| 930 | #endif /* CONFIG_P2P */ |
| 931 | |
| 932 | /* Start accounting here, if IEEE 802.1X and WPA are not used. |
| 933 | * IEEE 802.1X/WPA code will start accounting after the station has |
| 934 | * been authorized. */ |
| 935 | if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) |
| 936 | accounting_sta_start(hapd, sta); |
| 937 | |
| 938 | /* Start IEEE 802.1X authentication process for new stations */ |
| 939 | ieee802_1x_new_station(hapd, sta); |
| 940 | if (reassoc) { |
| 941 | if (sta->auth_alg != WLAN_AUTH_FT && |
| 942 | !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) |
| 943 | wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH); |
| 944 | } else |
| 945 | wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm); |
| 946 | } |