2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "radius/radius_client.h"
15 #include "radius/radius_das.h"
16 #include "drivers/driver.h"
20 #include "accounting.h"
24 #include "ieee802_1x.h"
25 #include "ieee802_11_auth.h"
26 #include "vlan_init.h"
28 #include "wps_hostapd.h"
29 #include "hw_features.h"
30 #include "wpa_auth_glue.h"
31 #include "ap_drv_ops.h"
32 #include "ap_config.h"
33 #include "p2p_hostapd.h"
37 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
38 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
39 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
41 extern int wpa_debug_level;
44 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
45 int (*cb)(struct hostapd_iface *iface,
46 void *ctx), void *ctx)
51 for (i = 0; i < interfaces->count; i++) {
52 ret = cb(interfaces->iface[i], ctx);
61 static void hostapd_reload_bss(struct hostapd_data *hapd)
63 #ifndef CONFIG_NO_RADIUS
64 radius_client_reconfig(hapd->radius, hapd->conf->radius);
65 #endif /* CONFIG_NO_RADIUS */
67 if (hostapd_setup_wpa_psk(hapd->conf)) {
68 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
69 "after reloading configuration");
72 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
73 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
75 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
77 if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
78 hostapd_setup_wpa(hapd);
80 wpa_init_keys(hapd->wpa_auth);
81 } else if (hapd->conf->wpa) {
84 hostapd_reconfig_wpa(hapd);
85 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
86 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
87 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
88 "the kernel driver.");
89 } else if (hapd->wpa_auth) {
90 wpa_deinit(hapd->wpa_auth);
91 hapd->wpa_auth = NULL;
92 hostapd_set_privacy(hapd, 0);
93 hostapd_setup_encryption(hapd->conf->iface, hapd);
94 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
97 ieee802_11_set_beacon(hapd);
98 hostapd_update_wps(hapd);
100 if (hapd->conf->ssid.ssid_set &&
101 hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid,
102 hapd->conf->ssid.ssid_len)) {
103 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
104 /* try to continue */
106 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
110 int hostapd_reload_config(struct hostapd_iface *iface)
112 struct hostapd_data *hapd = iface->bss[0];
113 struct hostapd_config *newconf, *oldconf;
116 if (iface->config_read_cb == NULL)
118 newconf = iface->config_read_cb(iface->config_fname);
123 * Deauthenticate all stations since the new configuration may not
124 * allow them to use the BSS anymore.
126 for (j = 0; j < iface->num_bss; j++) {
127 hostapd_flush_old_stations(iface->bss[j],
128 WLAN_REASON_PREV_AUTH_NOT_VALID);
129 hostapd_broadcast_wep_clear(iface->bss[j]);
131 #ifndef CONFIG_NO_RADIUS
132 /* TODO: update dynamic data based on changed configuration
133 * items (e.g., open/close sockets, etc.) */
134 radius_client_flush(iface->bss[j]->radius, 0);
135 #endif /* CONFIG_NO_RADIUS */
138 oldconf = hapd->iconf;
139 iface->conf = newconf;
141 for (j = 0; j < iface->num_bss; j++) {
142 hapd = iface->bss[j];
143 hapd->iconf = newconf;
144 hapd->conf = &newconf->bss[j];
145 hostapd_reload_bss(hapd);
148 hostapd_config_free(oldconf);
155 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
160 for (i = 0; i < NUM_WEP_KEYS; i++) {
161 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
162 0, NULL, 0, NULL, 0)) {
163 wpa_printf(MSG_DEBUG, "Failed to clear default "
164 "encryption keys (ifname=%s keyidx=%d)",
168 #ifdef CONFIG_IEEE80211W
169 if (hapd->conf->ieee80211w) {
170 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
171 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
174 wpa_printf(MSG_DEBUG, "Failed to clear "
175 "default mgmt encryption keys "
176 "(ifname=%s keyidx=%d)", ifname, i);
180 #endif /* CONFIG_IEEE80211W */
184 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
186 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
191 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
194 struct hostapd_ssid *ssid = &hapd->conf->ssid;
197 if (ssid->wep.default_len &&
198 hostapd_drv_set_key(hapd->conf->iface,
199 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
200 1, NULL, 0, ssid->wep.key[idx],
201 ssid->wep.len[idx])) {
202 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
206 if (ssid->dyn_vlan_keys) {
208 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
210 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
213 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
219 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_WEP,
220 broadcast_ether_addr, idx, 1,
221 NULL, 0, key->key[idx],
223 wpa_printf(MSG_WARNING, "Could not set "
224 "dynamic VLAN WEP encryption.");
234 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
236 iapp_deinit(hapd->iapp);
238 accounting_deinit(hapd);
239 hostapd_deinit_wpa(hapd);
241 hostapd_acl_deinit(hapd);
242 #ifndef CONFIG_NO_RADIUS
243 radius_client_deinit(hapd->radius);
245 radius_das_deinit(hapd->radius_das);
246 hapd->radius_das = NULL;
247 #endif /* CONFIG_NO_RADIUS */
249 hostapd_deinit_wps(hapd);
251 authsrv_deinit(hapd);
253 if (hapd->interface_added &&
254 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
255 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
259 os_free(hapd->probereq_cb);
260 hapd->probereq_cb = NULL;
263 wpabuf_free(hapd->p2p_beacon_ie);
264 hapd->p2p_beacon_ie = NULL;
265 wpabuf_free(hapd->p2p_probe_resp_ie);
266 hapd->p2p_probe_resp_ie = NULL;
267 #endif /* CONFIG_P2P */
269 wpabuf_free(hapd->time_adv);
271 #ifdef CONFIG_INTERWORKING
272 gas_serv_deinit(hapd);
273 #endif /* CONFIG_INTERWORKING */
278 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
279 * @hapd: Pointer to BSS data
281 * This function is used to free all per-BSS data structures and resources.
282 * This gets called in a loop for each BSS between calls to
283 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
284 * is deinitialized. Most of the modules that are initialized in
285 * hostapd_setup_bss() are deinitialized here.
287 static void hostapd_cleanup(struct hostapd_data *hapd)
289 if (hapd->iface->ctrl_iface_deinit)
290 hapd->iface->ctrl_iface_deinit(hapd);
291 hostapd_free_hapd_data(hapd);
296 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
297 * @iface: Pointer to interface data
299 * This function is called before per-BSS data structures are deinitialized
300 * with hostapd_cleanup().
302 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
307 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
309 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
310 iface->hw_features = NULL;
311 os_free(iface->current_rates);
312 iface->current_rates = NULL;
313 os_free(iface->basic_rates);
314 iface->basic_rates = NULL;
315 ap_list_deinit(iface);
320 * hostapd_cleanup_iface - Complete per-interface cleanup
321 * @iface: Pointer to interface data
323 * This function is called after per-BSS data structures are deinitialized
324 * with hostapd_cleanup().
326 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
328 hostapd_cleanup_iface_partial(iface);
329 hostapd_config_free(iface->conf);
332 os_free(iface->config_fname);
338 static void hostapd_clear_wep(struct hostapd_data *hapd)
340 if (hapd->drv_priv) {
341 hostapd_set_privacy(hapd, 0);
342 hostapd_broadcast_wep_clear(hapd);
347 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
351 hostapd_broadcast_wep_set(hapd);
353 if (hapd->conf->ssid.wep.default_len) {
354 hostapd_set_privacy(hapd, 1);
359 * When IEEE 802.1X is not enabled, the driver may need to know how to
360 * set authentication algorithms for static WEP.
362 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
364 for (i = 0; i < 4; i++) {
365 if (hapd->conf->ssid.wep.key[i] &&
366 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
367 i == hapd->conf->ssid.wep.idx, NULL, 0,
368 hapd->conf->ssid.wep.key[i],
369 hapd->conf->ssid.wep.len[i])) {
370 wpa_printf(MSG_WARNING, "Could not set WEP "
374 if (hapd->conf->ssid.wep.key[i] &&
375 i == hapd->conf->ssid.wep.idx)
376 hostapd_set_privacy(hapd, 1);
383 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
388 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
391 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
392 if (hostapd_flush(hapd)) {
393 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
397 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
398 os_memset(addr, 0xff, ETH_ALEN);
399 hostapd_drv_sta_deauth(hapd, addr, reason);
400 hostapd_free_stas(hapd);
407 * hostapd_validate_bssid_configuration - Validate BSSID configuration
408 * @iface: Pointer to interface data
409 * Returns: 0 on success, -1 on failure
411 * This function is used to validate that the configured BSSIDs are valid.
413 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
415 u8 mask[ETH_ALEN] = { 0 };
416 struct hostapd_data *hapd = iface->bss[0];
417 unsigned int i = iface->conf->num_bss, bits = 0, j;
420 if (hostapd_drv_none(hapd))
423 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
425 /* Determine the bits necessary to cover the number of BSSIDs. */
426 for (i--; i; i >>= 1)
429 /* Determine the bits necessary to any configured BSSIDs,
430 if they are higher than the number of BSSIDs. */
431 for (j = 0; j < iface->conf->num_bss; j++) {
432 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
438 for (i = 0; i < ETH_ALEN; i++) {
440 iface->conf->bss[j].bssid[i] ^
448 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
454 while (mask[i] != 0) {
464 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
469 os_memset(mask, 0xff, ETH_ALEN);
471 for (i = 5; i > 5 - j; i--)
478 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
479 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
484 for (i = 0; i < ETH_ALEN; i++) {
485 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
486 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
487 " for start address " MACSTR ".",
488 MAC2STR(mask), MAC2STR(hapd->own_addr));
489 wpa_printf(MSG_ERROR, "Start address must be the "
490 "first address in the block (i.e., addr "
491 "AND mask == addr).");
500 static int mac_in_conf(struct hostapd_config *conf, const void *a)
504 for (i = 0; i < conf->num_bss; i++) {
505 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
517 * hostapd_setup_bss - Per-BSS setup (initialization)
518 * @hapd: Pointer to BSS data
519 * @first: Whether this BSS is the first BSS of an interface
521 * This function is used to initialize all per-BSS data structures and
522 * resources. This gets called in a loop for each BSS when an interface is
523 * initialized. Most of the modules that are initialized here will be
524 * deinitialized in hostapd_cleanup().
526 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
528 struct hostapd_bss_config *conf = hapd->conf;
529 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
530 int ssid_len, set_ssid;
531 char force_ifname[IFNAMSIZ];
532 u8 if_addr[ETH_ALEN];
535 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
536 /* Allocate the next available BSSID. */
538 inc_byte_array(hapd->own_addr, ETH_ALEN);
539 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
541 /* Allocate the configured BSSID. */
542 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
544 if (hostapd_mac_comp(hapd->own_addr,
545 hapd->iface->bss[0]->own_addr) ==
547 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
548 "BSSID set to the MAC address of "
549 "the radio", hapd->conf->iface);
554 hapd->interface_added = 1;
555 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
556 hapd->conf->iface, hapd->own_addr, hapd,
557 &hapd->drv_priv, force_ifname, if_addr,
558 hapd->conf->bridge[0] ? hapd->conf->bridge :
560 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
561 MACSTR ")", MAC2STR(hapd->own_addr));
566 if (conf->wmm_enabled < 0)
567 conf->wmm_enabled = hapd->iconf->ieee80211n;
569 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
570 hostapd_set_privacy(hapd, 0);
572 hostapd_broadcast_wep_clear(hapd);
573 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
577 * Fetch the SSID from the system and use it or,
578 * if one was specified in the config file, verify they
581 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
583 wpa_printf(MSG_ERROR, "Could not read SSID from system");
586 if (conf->ssid.ssid_set) {
588 * If SSID is specified in the config file and it differs
589 * from what is being used then force installation of the
592 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
593 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
596 * No SSID in the config file; just use the one we got
600 conf->ssid.ssid_len = ssid_len;
601 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
602 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
605 if (!hostapd_drv_none(hapd)) {
606 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
608 hapd->conf->iface, MAC2STR(hapd->own_addr),
609 hapd->conf->ssid.ssid);
612 if (hostapd_setup_wpa_psk(conf)) {
613 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
617 /* Set SSID for the kernel driver (to be used in beacon and probe
618 * response frames) */
619 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
620 conf->ssid.ssid_len)) {
621 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
625 if (wpa_debug_level == MSG_MSGDUMP)
626 conf->radius->msg_dumps = 1;
627 #ifndef CONFIG_NO_RADIUS
628 hapd->radius = radius_client_init(hapd, conf->radius);
629 if (hapd->radius == NULL) {
630 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
634 if (hapd->conf->radius_das_port) {
635 struct radius_das_conf das_conf;
636 os_memset(&das_conf, 0, sizeof(das_conf));
637 das_conf.port = hapd->conf->radius_das_port;
638 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
639 das_conf.shared_secret_len =
640 hapd->conf->radius_das_shared_secret_len;
641 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
642 das_conf.time_window = hapd->conf->radius_das_time_window;
643 das_conf.require_event_timestamp =
644 hapd->conf->radius_das_require_event_timestamp;
645 hapd->radius_das = radius_das_init(&das_conf);
646 if (hapd->radius_das == NULL) {
647 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
652 #endif /* CONFIG_NO_RADIUS */
654 if (hostapd_acl_init(hapd)) {
655 wpa_printf(MSG_ERROR, "ACL initialization failed.");
658 if (hostapd_init_wps(hapd, conf))
661 if (authsrv_init(hapd) < 0)
664 if (ieee802_1x_init(hapd)) {
665 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
669 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
672 if (accounting_init(hapd)) {
673 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
677 if (hapd->conf->ieee802_11f &&
678 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
679 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
684 #ifdef CONFIG_INTERWORKING
685 if (gas_serv_init(hapd)) {
686 wpa_printf(MSG_ERROR, "GAS server initialization failed");
689 #endif /* CONFIG_INTERWORKING */
691 if (hapd->iface->ctrl_iface_init &&
692 hapd->iface->ctrl_iface_init(hapd)) {
693 wpa_printf(MSG_ERROR, "Failed to setup control interface");
697 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
698 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
702 ieee802_11_set_beacon(hapd);
704 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
707 if (hapd->driver && hapd->driver->set_operstate)
708 hapd->driver->set_operstate(hapd->drv_priv, 1);
714 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
716 struct hostapd_data *hapd = iface->bss[0];
718 struct hostapd_tx_queue_params *p;
720 for (i = 0; i < NUM_TX_QUEUES; i++) {
721 p = &iface->conf->tx_queue[i];
723 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
724 p->cwmax, p->burst)) {
725 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
726 "parameters for queue %d.", i);
727 /* Continue anyway */
733 static int setup_interface(struct hostapd_iface *iface)
735 struct hostapd_data *hapd = iface->bss[0];
740 * Make sure that all BSSes get configured with a pointer to the same
743 for (i = 1; i < iface->num_bss; i++) {
744 iface->bss[i]->driver = hapd->driver;
745 iface->bss[i]->drv_priv = hapd->drv_priv;
748 if (hostapd_validate_bssid_configuration(iface))
751 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
752 os_memcpy(country, hapd->iconf->country, 3);
754 if (hostapd_set_country(hapd, country) < 0) {
755 wpa_printf(MSG_ERROR, "Failed to set country code");
760 if (hostapd_get_hw_features(iface)) {
761 /* Not all drivers support this yet, so continue without hw
764 int ret = hostapd_select_hw_mode(iface);
766 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
767 "channel. (%d)", ret);
770 ret = hostapd_check_ht_capab(iface);
774 wpa_printf(MSG_DEBUG, "Interface initialization will "
775 "be completed in a callback");
779 return hostapd_setup_interface_complete(iface, 0);
783 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
785 struct hostapd_data *hapd = iface->bss[0];
790 wpa_printf(MSG_ERROR, "Interface initialization failed");
795 wpa_printf(MSG_DEBUG, "Completing interface initialization");
796 if (hapd->iconf->channel) {
797 iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
798 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
800 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
801 hapd->iconf->channel, iface->freq);
803 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
804 hapd->iconf->channel,
805 hapd->iconf->ieee80211n,
806 hapd->iconf->secondary_channel)) {
807 wpa_printf(MSG_ERROR, "Could not set channel for "
813 if (iface->current_mode) {
814 if (hostapd_prepare_rates(iface, iface->current_mode)) {
815 wpa_printf(MSG_ERROR, "Failed to prepare rates "
817 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
818 HOSTAPD_LEVEL_WARNING,
819 "Failed to prepare rates table.");
824 if (hapd->iconf->rts_threshold > -1 &&
825 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
826 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
831 if (hapd->iconf->fragm_threshold > -1 &&
832 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
833 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
834 "for kernel driver");
838 prev_addr = hapd->own_addr;
840 for (j = 0; j < iface->num_bss; j++) {
841 hapd = iface->bss[j];
843 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
844 if (hostapd_setup_bss(hapd, j == 0))
846 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
847 prev_addr = hapd->own_addr;
850 hostapd_tx_queue_params(iface);
854 if (hostapd_driver_commit(hapd) < 0) {
855 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
856 "configuration", __func__);
861 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
862 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
863 * mode), the interface is up only after driver_commit, so initialize
864 * WPS after driver_commit.
866 for (j = 0; j < iface->num_bss; j++) {
867 if (hostapd_init_wps_complete(iface->bss[j]))
871 if (hapd->setup_complete_cb)
872 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
874 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
875 iface->bss[0]->conf->iface);
882 * hostapd_setup_interface - Setup of an interface
883 * @iface: Pointer to interface data.
884 * Returns: 0 on success, -1 on failure
886 * Initializes the driver interface, validates the configuration,
887 * and sets driver parameters based on the configuration.
888 * Flushes old stations, sets the channel, encryption,
889 * beacons, and WDS links based on the configuration.
891 int hostapd_setup_interface(struct hostapd_iface *iface)
895 ret = setup_interface(iface);
897 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
898 iface->bss[0]->conf->iface);
907 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
908 * @hapd_iface: Pointer to interface data
909 * @conf: Pointer to per-interface configuration
910 * @bss: Pointer to per-BSS configuration for this BSS
911 * Returns: Pointer to allocated BSS data
913 * This function is used to allocate per-BSS data structure. This data will be
914 * freed after hostapd_cleanup() is called for it during interface
917 struct hostapd_data *
918 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
919 struct hostapd_config *conf,
920 struct hostapd_bss_config *bss)
922 struct hostapd_data *hapd;
924 hapd = os_zalloc(sizeof(*hapd));
928 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
931 hapd->iface = hapd_iface;
932 hapd->driver = hapd->iconf->driver;
933 hapd->ctrl_sock = -1;
939 void hostapd_interface_deinit(struct hostapd_iface *iface)
946 hostapd_cleanup_iface_pre(iface);
947 for (j = 0; j < iface->num_bss; j++) {
948 struct hostapd_data *hapd = iface->bss[j];
949 hostapd_free_stas(hapd);
950 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
951 hostapd_clear_wep(hapd);
952 hostapd_cleanup(hapd);
957 void hostapd_interface_free(struct hostapd_iface *iface)
960 for (j = 0; j < iface->num_bss; j++)
961 os_free(iface->bss[j]);
962 hostapd_cleanup_iface(iface);
967 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
968 * @hapd: Pointer to BSS data
969 * @sta: Pointer to the associated STA data
970 * @reassoc: 1 to indicate this was a re-association; 0 = first association
972 * This function will be called whenever a station associates with the AP. It
973 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
974 * from drv_callbacks.c based on driver events for drivers that take care of
975 * management frames (IEEE 802.11 authentication and association) internally.
977 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
980 if (hapd->tkip_countermeasures) {
981 hostapd_drv_sta_deauth(hapd, sta->addr,
982 WLAN_REASON_MICHAEL_MIC_FAILURE);
986 hostapd_prune_associations(hapd, sta->addr);
988 /* IEEE 802.11F (IAPP) */
989 if (hapd->conf->ieee802_11f)
990 iapp_new_station(hapd->iapp, sta);
993 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
995 hapd->num_sta_no_p2p++;
996 if (hapd->num_sta_no_p2p == 1)
997 hostapd_p2p_non_p2p_sta_connected(hapd);
999 #endif /* CONFIG_P2P */
1001 /* Start accounting here, if IEEE 802.1X and WPA are not used.
1002 * IEEE 802.1X/WPA code will start accounting after the station has
1003 * been authorized. */
1004 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
1005 accounting_sta_start(hapd, sta);
1007 /* Start IEEE 802.1X authentication process for new stations */
1008 ieee802_1x_new_station(hapd, sta);
1010 if (sta->auth_alg != WLAN_AUTH_FT &&
1011 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1012 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1014 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
1016 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1017 "for " MACSTR " (%d seconds - ap_max_inactivity)",
1018 __func__, MAC2STR(sta->addr),
1019 hapd->conf->ap_max_inactivity);
1020 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1021 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1022 ap_handle_timer, hapd, sta);