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