2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2013, 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 "common/wpa_ctrl.h"
15 #include "radius/radius_client.h"
16 #include "radius/radius_das.h"
17 #include "drivers/driver.h"
21 #include "accounting.h"
25 #include "ieee802_1x.h"
26 #include "ieee802_11_auth.h"
27 #include "vlan_init.h"
29 #include "wps_hostapd.h"
30 #include "hw_features.h"
31 #include "wpa_auth_glue.h"
32 #include "ap_drv_ops.h"
33 #include "ap_config.h"
34 #include "p2p_hostapd.h"
39 static int hostapd_flush_old_stations(struct hostapd_data
*hapd
, u16 reason
);
40 static int hostapd_setup_encryption(char *iface
, struct hostapd_data
*hapd
);
41 static int hostapd_broadcast_wep_clear(struct hostapd_data
*hapd
);
42 static int setup_interface2(struct hostapd_iface
*iface
);
43 static void channel_list_update_timeout(void *eloop_ctx
, void *timeout_ctx
);
45 extern int wpa_debug_level
;
46 extern struct wpa_driver_ops
*wpa_drivers
[];
49 int hostapd_for_each_interface(struct hapd_interfaces
*interfaces
,
50 int (*cb
)(struct hostapd_iface
*iface
,
51 void *ctx
), void *ctx
)
56 for (i
= 0; i
< interfaces
->count
; i
++) {
57 ret
= cb(interfaces
->iface
[i
], ctx
);
66 static void hostapd_reload_bss(struct hostapd_data
*hapd
)
68 struct hostapd_ssid
*ssid
;
70 #ifndef CONFIG_NO_RADIUS
71 radius_client_reconfig(hapd
->radius
, hapd
->conf
->radius
);
72 #endif /* CONFIG_NO_RADIUS */
74 ssid
= &hapd
->conf
->ssid
;
75 if (!ssid
->wpa_psk_set
&& ssid
->wpa_psk
&& !ssid
->wpa_psk
->next
&&
76 ssid
->wpa_passphrase_set
&& ssid
->wpa_passphrase
) {
78 * Force PSK to be derived again since SSID or passphrase may
81 os_free(ssid
->wpa_psk
);
84 if (hostapd_setup_wpa_psk(hapd
->conf
)) {
85 wpa_printf(MSG_ERROR
, "Failed to re-configure WPA PSK "
86 "after reloading configuration");
89 if (hapd
->conf
->ieee802_1x
|| hapd
->conf
->wpa
)
90 hostapd_set_drv_ieee8021x(hapd
, hapd
->conf
->iface
, 1);
92 hostapd_set_drv_ieee8021x(hapd
, hapd
->conf
->iface
, 0);
94 if (hapd
->conf
->wpa
&& hapd
->wpa_auth
== NULL
) {
95 hostapd_setup_wpa(hapd
);
97 wpa_init_keys(hapd
->wpa_auth
);
98 } else if (hapd
->conf
->wpa
) {
101 hostapd_reconfig_wpa(hapd
);
102 wpa_ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &wpa_ie_len
);
103 if (hostapd_set_generic_elem(hapd
, wpa_ie
, wpa_ie_len
))
104 wpa_printf(MSG_ERROR
, "Failed to configure WPA IE for "
105 "the kernel driver.");
106 } else if (hapd
->wpa_auth
) {
107 wpa_deinit(hapd
->wpa_auth
);
108 hapd
->wpa_auth
= NULL
;
109 hostapd_set_privacy(hapd
, 0);
110 hostapd_setup_encryption(hapd
->conf
->iface
, hapd
);
111 hostapd_set_generic_elem(hapd
, (u8
*) "", 0);
114 ieee802_11_set_beacon(hapd
);
115 hostapd_update_wps(hapd
);
117 if (hapd
->conf
->ssid
.ssid_set
&&
118 hostapd_set_ssid(hapd
, hapd
->conf
->ssid
.ssid
,
119 hapd
->conf
->ssid
.ssid_len
)) {
120 wpa_printf(MSG_ERROR
, "Could not set SSID for kernel driver");
121 /* try to continue */
123 wpa_printf(MSG_DEBUG
, "Reconfigured interface %s", hapd
->conf
->iface
);
127 static void hostapd_clear_old(struct hostapd_iface
*iface
)
132 * Deauthenticate all stations since the new configuration may not
133 * allow them to use the BSS anymore.
135 for (j
= 0; j
< iface
->num_bss
; j
++) {
136 hostapd_flush_old_stations(iface
->bss
[j
],
137 WLAN_REASON_PREV_AUTH_NOT_VALID
);
138 hostapd_broadcast_wep_clear(iface
->bss
[j
]);
140 #ifndef CONFIG_NO_RADIUS
141 /* TODO: update dynamic data based on changed configuration
142 * items (e.g., open/close sockets, etc.) */
143 radius_client_flush(iface
->bss
[j
]->radius
, 0);
144 #endif /* CONFIG_NO_RADIUS */
149 int hostapd_reload_config(struct hostapd_iface
*iface
)
151 struct hostapd_data
*hapd
= iface
->bss
[0];
152 struct hostapd_config
*newconf
, *oldconf
;
155 if (iface
->config_fname
== NULL
) {
156 /* Only in-memory config in use - assume it has been updated */
157 hostapd_clear_old(iface
);
158 for (j
= 0; j
< iface
->num_bss
; j
++)
159 hostapd_reload_bss(iface
->bss
[j
]);
163 if (iface
->interfaces
== NULL
||
164 iface
->interfaces
->config_read_cb
== NULL
)
166 newconf
= iface
->interfaces
->config_read_cb(iface
->config_fname
);
170 hostapd_clear_old(iface
);
172 oldconf
= hapd
->iconf
;
173 iface
->conf
= newconf
;
175 for (j
= 0; j
< iface
->num_bss
; j
++) {
176 hapd
= iface
->bss
[j
];
177 hapd
->iconf
= newconf
;
178 hapd
->conf
= newconf
->bss
[j
];
179 hostapd_reload_bss(hapd
);
182 hostapd_config_free(oldconf
);
189 static void hostapd_broadcast_key_clear_iface(struct hostapd_data
*hapd
,
194 for (i
= 0; i
< NUM_WEP_KEYS
; i
++) {
195 if (hostapd_drv_set_key(ifname
, hapd
, WPA_ALG_NONE
, NULL
, i
,
196 0, NULL
, 0, NULL
, 0)) {
197 wpa_printf(MSG_DEBUG
, "Failed to clear default "
198 "encryption keys (ifname=%s keyidx=%d)",
202 #ifdef CONFIG_IEEE80211W
203 if (hapd
->conf
->ieee80211w
) {
204 for (i
= NUM_WEP_KEYS
; i
< NUM_WEP_KEYS
+ 2; i
++) {
205 if (hostapd_drv_set_key(ifname
, hapd
, WPA_ALG_NONE
,
208 wpa_printf(MSG_DEBUG
, "Failed to clear "
209 "default mgmt encryption keys "
210 "(ifname=%s keyidx=%d)", ifname
, i
);
214 #endif /* CONFIG_IEEE80211W */
218 static int hostapd_broadcast_wep_clear(struct hostapd_data
*hapd
)
220 hostapd_broadcast_key_clear_iface(hapd
, hapd
->conf
->iface
);
225 static int hostapd_broadcast_wep_set(struct hostapd_data
*hapd
)
228 struct hostapd_ssid
*ssid
= &hapd
->conf
->ssid
;
231 if (ssid
->wep
.default_len
&&
232 hostapd_drv_set_key(hapd
->conf
->iface
,
233 hapd
, WPA_ALG_WEP
, broadcast_ether_addr
, idx
,
234 1, NULL
, 0, ssid
->wep
.key
[idx
],
235 ssid
->wep
.len
[idx
])) {
236 wpa_printf(MSG_WARNING
, "Could not set WEP encryption.");
244 static void hostapd_free_hapd_data(struct hostapd_data
*hapd
)
246 wpa_printf(MSG_DEBUG
, "%s(%s)", __func__
, hapd
->conf
->iface
);
247 iapp_deinit(hapd
->iapp
);
249 accounting_deinit(hapd
);
250 hostapd_deinit_wpa(hapd
);
252 hostapd_acl_deinit(hapd
);
253 #ifndef CONFIG_NO_RADIUS
254 radius_client_deinit(hapd
->radius
);
256 radius_das_deinit(hapd
->radius_das
);
257 hapd
->radius_das
= NULL
;
258 #endif /* CONFIG_NO_RADIUS */
260 hostapd_deinit_wps(hapd
);
262 authsrv_deinit(hapd
);
264 if (hapd
->interface_added
&&
265 hostapd_if_remove(hapd
, WPA_IF_AP_BSS
, hapd
->conf
->iface
)) {
266 wpa_printf(MSG_WARNING
, "Failed to remove BSS interface %s",
270 os_free(hapd
->probereq_cb
);
271 hapd
->probereq_cb
= NULL
;
274 wpabuf_free(hapd
->p2p_beacon_ie
);
275 hapd
->p2p_beacon_ie
= NULL
;
276 wpabuf_free(hapd
->p2p_probe_resp_ie
);
277 hapd
->p2p_probe_resp_ie
= NULL
;
278 #endif /* CONFIG_P2P */
280 wpabuf_free(hapd
->time_adv
);
282 #ifdef CONFIG_INTERWORKING
283 gas_serv_deinit(hapd
);
284 #endif /* CONFIG_INTERWORKING */
287 os_free(hapd
->tmp_eap_user
.identity
);
288 os_free(hapd
->tmp_eap_user
.password
);
289 #endif /* CONFIG_SQLITE */
294 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
295 * @hapd: Pointer to BSS data
297 * This function is used to free all per-BSS data structures and resources.
298 * Most of the modules that are initialized in hostapd_setup_bss() are
299 * deinitialized here.
301 static void hostapd_cleanup(struct hostapd_data
*hapd
)
303 wpa_printf(MSG_DEBUG
, "%s(%s)", __func__
, hapd
->conf
->iface
);
304 if (hapd
->iface
->interfaces
&&
305 hapd
->iface
->interfaces
->ctrl_iface_deinit
)
306 hapd
->iface
->interfaces
->ctrl_iface_deinit(hapd
);
307 hostapd_free_hapd_data(hapd
);
311 static void hostapd_cleanup_iface_partial(struct hostapd_iface
*iface
)
313 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
314 hostapd_free_hw_features(iface
->hw_features
, iface
->num_hw_features
);
315 iface
->hw_features
= NULL
;
316 os_free(iface
->current_rates
);
317 iface
->current_rates
= NULL
;
318 os_free(iface
->basic_rates
);
319 iface
->basic_rates
= NULL
;
320 ap_list_deinit(iface
);
325 * hostapd_cleanup_iface - Complete per-interface cleanup
326 * @iface: Pointer to interface data
328 * This function is called after per-BSS data structures are deinitialized
329 * with hostapd_cleanup().
331 static void hostapd_cleanup_iface(struct hostapd_iface
*iface
)
333 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
334 eloop_cancel_timeout(channel_list_update_timeout
, iface
, NULL
);
336 hostapd_cleanup_iface_partial(iface
);
337 hostapd_config_free(iface
->conf
);
340 os_free(iface
->config_fname
);
342 wpa_printf(MSG_DEBUG
, "%s: free iface=%p", __func__
, iface
);
347 static void hostapd_clear_wep(struct hostapd_data
*hapd
)
349 if (hapd
->drv_priv
) {
350 hostapd_set_privacy(hapd
, 0);
351 hostapd_broadcast_wep_clear(hapd
);
356 static int hostapd_setup_encryption(char *iface
, struct hostapd_data
*hapd
)
360 hostapd_broadcast_wep_set(hapd
);
362 if (hapd
->conf
->ssid
.wep
.default_len
) {
363 hostapd_set_privacy(hapd
, 1);
368 * When IEEE 802.1X is not enabled, the driver may need to know how to
369 * set authentication algorithms for static WEP.
371 hostapd_drv_set_authmode(hapd
, hapd
->conf
->auth_algs
);
373 for (i
= 0; i
< 4; i
++) {
374 if (hapd
->conf
->ssid
.wep
.key
[i
] &&
375 hostapd_drv_set_key(iface
, hapd
, WPA_ALG_WEP
, NULL
, i
,
376 i
== hapd
->conf
->ssid
.wep
.idx
, NULL
, 0,
377 hapd
->conf
->ssid
.wep
.key
[i
],
378 hapd
->conf
->ssid
.wep
.len
[i
])) {
379 wpa_printf(MSG_WARNING
, "Could not set WEP "
383 if (hapd
->conf
->ssid
.wep
.key
[i
] &&
384 i
== hapd
->conf
->ssid
.wep
.idx
)
385 hostapd_set_privacy(hapd
, 1);
392 static int hostapd_flush_old_stations(struct hostapd_data
*hapd
, u16 reason
)
397 if (hostapd_drv_none(hapd
) || hapd
->drv_priv
== NULL
)
400 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "Flushing old station entries");
401 if (hostapd_flush(hapd
)) {
402 wpa_msg(hapd
->msg_ctx
, MSG_WARNING
, "Could not connect to "
406 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "Deauthenticate all stations");
407 os_memset(addr
, 0xff, ETH_ALEN
);
408 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
409 hostapd_free_stas(hapd
);
416 * hostapd_validate_bssid_configuration - Validate BSSID configuration
417 * @iface: Pointer to interface data
418 * Returns: 0 on success, -1 on failure
420 * This function is used to validate that the configured BSSIDs are valid.
422 static int hostapd_validate_bssid_configuration(struct hostapd_iface
*iface
)
424 u8 mask
[ETH_ALEN
] = { 0 };
425 struct hostapd_data
*hapd
= iface
->bss
[0];
426 unsigned int i
= iface
->conf
->num_bss
, bits
= 0, j
;
429 if (hostapd_drv_none(hapd
))
432 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
434 /* Determine the bits necessary to cover the number of BSSIDs. */
435 for (i
--; i
; i
>>= 1)
438 /* Determine the bits necessary to any configured BSSIDs,
439 if they are higher than the number of BSSIDs. */
440 for (j
= 0; j
< iface
->conf
->num_bss
; j
++) {
441 if (hostapd_mac_comp_empty(iface
->conf
->bss
[j
]->bssid
) == 0) {
447 for (i
= 0; i
< ETH_ALEN
; i
++) {
449 iface
->conf
->bss
[j
]->bssid
[i
] ^
457 for (i
= 0; i
< ETH_ALEN
&& mask
[i
] == 0; i
++)
463 while (mask
[i
] != 0) {
473 wpa_printf(MSG_ERROR
, "Too many bits in the BSSID mask (%u)",
478 os_memset(mask
, 0xff, ETH_ALEN
);
480 for (i
= 5; i
> 5 - j
; i
--)
487 wpa_printf(MSG_DEBUG
, "BSS count %lu, BSSID mask " MACSTR
" (%d bits)",
488 (unsigned long) iface
->conf
->num_bss
, MAC2STR(mask
), bits
);
493 for (i
= 0; i
< ETH_ALEN
; i
++) {
494 if ((hapd
->own_addr
[i
] & mask
[i
]) != hapd
->own_addr
[i
]) {
495 wpa_printf(MSG_ERROR
, "Invalid BSSID mask " MACSTR
496 " for start address " MACSTR
".",
497 MAC2STR(mask
), MAC2STR(hapd
->own_addr
));
498 wpa_printf(MSG_ERROR
, "Start address must be the "
499 "first address in the block (i.e., addr "
500 "AND mask == addr).");
509 static int mac_in_conf(struct hostapd_config
*conf
, const void *a
)
513 for (i
= 0; i
< conf
->num_bss
; i
++) {
514 if (hostapd_mac_comp(conf
->bss
[i
]->bssid
, a
) == 0) {
523 #ifndef CONFIG_NO_RADIUS
525 static int hostapd_das_nas_mismatch(struct hostapd_data
*hapd
,
526 struct radius_das_attrs
*attr
)
533 static struct sta_info
* hostapd_das_find_sta(struct hostapd_data
*hapd
,
534 struct radius_das_attrs
*attr
)
536 struct sta_info
*sta
= NULL
;
540 sta
= ap_get_sta(hapd
, attr
->sta_addr
);
542 if (sta
== NULL
&& attr
->acct_session_id
&&
543 attr
->acct_session_id_len
== 17) {
544 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
545 os_snprintf(buf
, sizeof(buf
), "%08X-%08X",
546 sta
->acct_session_id_hi
,
547 sta
->acct_session_id_lo
);
548 if (os_memcmp(attr
->acct_session_id
, buf
, 17) == 0)
553 if (sta
== NULL
&& attr
->cui
) {
554 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
556 cui
= ieee802_1x_get_radius_cui(sta
->eapol_sm
);
557 if (cui
&& wpabuf_len(cui
) == attr
->cui_len
&&
558 os_memcmp(wpabuf_head(cui
), attr
->cui
,
564 if (sta
== NULL
&& attr
->user_name
) {
565 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
568 identity
= ieee802_1x_get_identity(sta
->eapol_sm
,
571 identity_len
== attr
->user_name_len
&&
572 os_memcmp(identity
, attr
->user_name
, identity_len
)
582 static enum radius_das_res
583 hostapd_das_disconnect(void *ctx
, struct radius_das_attrs
*attr
)
585 struct hostapd_data
*hapd
= ctx
;
586 struct sta_info
*sta
;
588 if (hostapd_das_nas_mismatch(hapd
, attr
))
589 return RADIUS_DAS_NAS_MISMATCH
;
591 sta
= hostapd_das_find_sta(hapd
, attr
);
593 return RADIUS_DAS_SESSION_NOT_FOUND
;
595 hostapd_drv_sta_deauth(hapd
, sta
->addr
,
596 WLAN_REASON_PREV_AUTH_NOT_VALID
);
597 ap_sta_deauthenticate(hapd
, sta
, WLAN_REASON_PREV_AUTH_NOT_VALID
);
599 return RADIUS_DAS_SUCCESS
;
602 #endif /* CONFIG_NO_RADIUS */
606 * hostapd_setup_bss - Per-BSS setup (initialization)
607 * @hapd: Pointer to BSS data
608 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
609 * but interface may exist
611 * This function is used to initialize all per-BSS data structures and
612 * resources. This gets called in a loop for each BSS when an interface is
613 * initialized. Most of the modules that are initialized here will be
614 * deinitialized in hostapd_cleanup().
616 static int hostapd_setup_bss(struct hostapd_data
*hapd
, int first
)
618 struct hostapd_bss_config
*conf
= hapd
->conf
;
619 u8 ssid
[HOSTAPD_MAX_SSID_LEN
+ 1];
620 int ssid_len
, set_ssid
;
621 char force_ifname
[IFNAMSIZ
];
622 u8 if_addr
[ETH_ALEN
];
624 if (!first
|| first
== -1) {
625 if (hostapd_mac_comp_empty(hapd
->conf
->bssid
) == 0) {
626 /* Allocate the next available BSSID. */
628 inc_byte_array(hapd
->own_addr
, ETH_ALEN
);
629 } while (mac_in_conf(hapd
->iconf
, hapd
->own_addr
));
631 /* Allocate the configured BSSID. */
632 os_memcpy(hapd
->own_addr
, hapd
->conf
->bssid
, ETH_ALEN
);
634 if (hostapd_mac_comp(hapd
->own_addr
,
635 hapd
->iface
->bss
[0]->own_addr
) ==
637 wpa_printf(MSG_ERROR
, "BSS '%s' may not have "
638 "BSSID set to the MAC address of "
639 "the radio", hapd
->conf
->iface
);
644 hapd
->interface_added
= 1;
645 if (hostapd_if_add(hapd
->iface
->bss
[0], WPA_IF_AP_BSS
,
646 hapd
->conf
->iface
, hapd
->own_addr
, hapd
,
647 &hapd
->drv_priv
, force_ifname
, if_addr
,
648 hapd
->conf
->bridge
[0] ? hapd
->conf
->bridge
:
649 NULL
, first
== -1)) {
650 wpa_printf(MSG_ERROR
, "Failed to add BSS (BSSID="
651 MACSTR
")", MAC2STR(hapd
->own_addr
));
656 if (conf
->wmm_enabled
< 0)
657 conf
->wmm_enabled
= hapd
->iconf
->ieee80211n
;
659 hostapd_flush_old_stations(hapd
, WLAN_REASON_PREV_AUTH_NOT_VALID
);
660 hostapd_set_privacy(hapd
, 0);
662 hostapd_broadcast_wep_clear(hapd
);
663 if (hostapd_setup_encryption(hapd
->conf
->iface
, hapd
))
667 * Fetch the SSID from the system and use it or,
668 * if one was specified in the config file, verify they
671 ssid_len
= hostapd_get_ssid(hapd
, ssid
, sizeof(ssid
));
673 wpa_printf(MSG_ERROR
, "Could not read SSID from system");
676 if (conf
->ssid
.ssid_set
) {
678 * If SSID is specified in the config file and it differs
679 * from what is being used then force installation of the
682 set_ssid
= (conf
->ssid
.ssid_len
!= (size_t) ssid_len
||
683 os_memcmp(conf
->ssid
.ssid
, ssid
, ssid_len
) != 0);
686 * No SSID in the config file; just use the one we got
690 conf
->ssid
.ssid_len
= ssid_len
;
691 os_memcpy(conf
->ssid
.ssid
, ssid
, conf
->ssid
.ssid_len
);
694 if (!hostapd_drv_none(hapd
)) {
695 wpa_printf(MSG_ERROR
, "Using interface %s with hwaddr " MACSTR
697 hapd
->conf
->iface
, MAC2STR(hapd
->own_addr
),
698 wpa_ssid_txt(hapd
->conf
->ssid
.ssid
,
699 hapd
->conf
->ssid
.ssid_len
));
702 if (hostapd_setup_wpa_psk(conf
)) {
703 wpa_printf(MSG_ERROR
, "WPA-PSK setup failed.");
707 /* Set SSID for the kernel driver (to be used in beacon and probe
708 * response frames) */
709 if (set_ssid
&& hostapd_set_ssid(hapd
, conf
->ssid
.ssid
,
710 conf
->ssid
.ssid_len
)) {
711 wpa_printf(MSG_ERROR
, "Could not set SSID for kernel driver");
715 if (wpa_debug_level
== MSG_MSGDUMP
)
716 conf
->radius
->msg_dumps
= 1;
717 #ifndef CONFIG_NO_RADIUS
718 hapd
->radius
= radius_client_init(hapd
, conf
->radius
);
719 if (hapd
->radius
== NULL
) {
720 wpa_printf(MSG_ERROR
, "RADIUS client initialization failed.");
724 if (hapd
->conf
->radius_das_port
) {
725 struct radius_das_conf das_conf
;
726 os_memset(&das_conf
, 0, sizeof(das_conf
));
727 das_conf
.port
= hapd
->conf
->radius_das_port
;
728 das_conf
.shared_secret
= hapd
->conf
->radius_das_shared_secret
;
729 das_conf
.shared_secret_len
=
730 hapd
->conf
->radius_das_shared_secret_len
;
731 das_conf
.client_addr
= &hapd
->conf
->radius_das_client_addr
;
732 das_conf
.time_window
= hapd
->conf
->radius_das_time_window
;
733 das_conf
.require_event_timestamp
=
734 hapd
->conf
->radius_das_require_event_timestamp
;
736 das_conf
.disconnect
= hostapd_das_disconnect
;
737 hapd
->radius_das
= radius_das_init(&das_conf
);
738 if (hapd
->radius_das
== NULL
) {
739 wpa_printf(MSG_ERROR
, "RADIUS DAS initialization "
744 #endif /* CONFIG_NO_RADIUS */
746 if (hostapd_acl_init(hapd
)) {
747 wpa_printf(MSG_ERROR
, "ACL initialization failed.");
750 if (hostapd_init_wps(hapd
, conf
))
753 if (authsrv_init(hapd
) < 0)
756 if (ieee802_1x_init(hapd
)) {
757 wpa_printf(MSG_ERROR
, "IEEE 802.1X initialization failed.");
761 if (hapd
->conf
->wpa
&& hostapd_setup_wpa(hapd
))
764 if (accounting_init(hapd
)) {
765 wpa_printf(MSG_ERROR
, "Accounting initialization failed.");
769 if (hapd
->conf
->ieee802_11f
&&
770 (hapd
->iapp
= iapp_init(hapd
, hapd
->conf
->iapp_iface
)) == NULL
) {
771 wpa_printf(MSG_ERROR
, "IEEE 802.11F (IAPP) initialization "
776 #ifdef CONFIG_INTERWORKING
777 if (gas_serv_init(hapd
)) {
778 wpa_printf(MSG_ERROR
, "GAS server initialization failed");
782 if (conf
->qos_map_set_len
&&
783 hostapd_drv_set_qos_map(hapd
, conf
->qos_map_set
,
784 conf
->qos_map_set_len
)) {
785 wpa_printf(MSG_ERROR
, "Failed to initialize QoS Map");
788 #endif /* CONFIG_INTERWORKING */
790 if (!hostapd_drv_none(hapd
) && vlan_init(hapd
)) {
791 wpa_printf(MSG_ERROR
, "VLAN initialization failed.");
795 if (!hapd
->conf
->start_disabled
)
796 ieee802_11_set_beacon(hapd
);
798 if (hapd
->wpa_auth
&& wpa_init_keys(hapd
->wpa_auth
) < 0)
801 if (hapd
->driver
&& hapd
->driver
->set_operstate
)
802 hapd
->driver
->set_operstate(hapd
->drv_priv
, 1);
808 static void hostapd_tx_queue_params(struct hostapd_iface
*iface
)
810 struct hostapd_data
*hapd
= iface
->bss
[0];
812 struct hostapd_tx_queue_params
*p
;
814 for (i
= 0; i
< NUM_TX_QUEUES
; i
++) {
815 p
= &iface
->conf
->tx_queue
[i
];
817 if (hostapd_set_tx_queue_params(hapd
, i
, p
->aifs
, p
->cwmin
,
818 p
->cwmax
, p
->burst
)) {
819 wpa_printf(MSG_DEBUG
, "Failed to set TX queue "
820 "parameters for queue %d.", i
);
821 /* Continue anyway */
827 static int hostapd_set_acl_list(struct hostapd_data
*hapd
,
828 struct mac_acl_entry
*mac_acl
,
829 int n_entries
, u8 accept_acl
)
831 struct hostapd_acl_params
*acl_params
;
834 acl_params
= os_zalloc(sizeof(*acl_params
) +
835 (n_entries
* sizeof(acl_params
->mac_acl
[0])));
839 for (i
= 0; i
< n_entries
; i
++)
840 os_memcpy(acl_params
->mac_acl
[i
].addr
, mac_acl
[i
].addr
,
843 acl_params
->acl_policy
= accept_acl
;
844 acl_params
->num_mac_acl
= n_entries
;
846 err
= hostapd_drv_set_acl(hapd
, acl_params
);
854 static void hostapd_set_acl(struct hostapd_data
*hapd
)
856 struct hostapd_config
*conf
= hapd
->iconf
;
860 if (hapd
->iface
->drv_max_acl_mac_addrs
== 0)
862 if (!(conf
->bss
[0]->num_accept_mac
|| conf
->bss
[0]->num_deny_mac
))
865 if (conf
->bss
[0]->macaddr_acl
== DENY_UNLESS_ACCEPTED
) {
866 if (conf
->bss
[0]->num_accept_mac
) {
868 err
= hostapd_set_acl_list(hapd
,
869 conf
->bss
[0]->accept_mac
,
870 conf
->bss
[0]->num_accept_mac
,
873 wpa_printf(MSG_DEBUG
, "Failed to set accept acl");
877 wpa_printf(MSG_DEBUG
, "Mismatch between ACL Policy & Accept/deny lists file");
879 } else if (conf
->bss
[0]->macaddr_acl
== ACCEPT_UNLESS_DENIED
) {
880 if (conf
->bss
[0]->num_deny_mac
) {
882 err
= hostapd_set_acl_list(hapd
, conf
->bss
[0]->deny_mac
,
883 conf
->bss
[0]->num_deny_mac
,
886 wpa_printf(MSG_DEBUG
, "Failed to set deny acl");
890 wpa_printf(MSG_DEBUG
, "Mismatch between ACL Policy & Accept/deny lists file");
896 static int start_ctrl_iface_bss(struct hostapd_data
*hapd
)
898 if (!hapd
->iface
->interfaces
||
899 !hapd
->iface
->interfaces
->ctrl_iface_init
)
902 if (hapd
->iface
->interfaces
->ctrl_iface_init(hapd
)) {
903 wpa_printf(MSG_ERROR
,
904 "Failed to setup control interface for %s",
913 static int start_ctrl_iface(struct hostapd_iface
*iface
)
917 if (!iface
->interfaces
|| !iface
->interfaces
->ctrl_iface_init
)
920 for (i
= 0; i
< iface
->num_bss
; i
++) {
921 struct hostapd_data
*hapd
= iface
->bss
[i
];
922 if (iface
->interfaces
->ctrl_iface_init(hapd
)) {
923 wpa_printf(MSG_ERROR
,
924 "Failed to setup control interface for %s",
934 static void channel_list_update_timeout(void *eloop_ctx
, void *timeout_ctx
)
936 struct hostapd_iface
*iface
= eloop_ctx
;
938 if (!iface
->wait_channel_update
) {
939 wpa_printf(MSG_INFO
, "Channel list update timeout, but interface was not waiting for it");
944 * It is possible that the existing channel list is acceptable, so try
947 wpa_printf(MSG_DEBUG
, "Channel list update timeout - try to continue anyway");
948 setup_interface2(iface
);
952 void hostapd_channel_list_updated(struct hostapd_iface
*iface
)
954 if (!iface
->wait_channel_update
)
957 wpa_printf(MSG_DEBUG
, "Channel list updated - continue setup");
958 eloop_cancel_timeout(channel_list_update_timeout
, iface
, NULL
);
959 setup_interface2(iface
);
963 static int setup_interface(struct hostapd_iface
*iface
)
965 struct hostapd_data
*hapd
= iface
->bss
[0];
968 if (!iface
->phy
[0]) {
969 const char *phy
= hostapd_drv_get_radio_name(hapd
);
971 wpa_printf(MSG_DEBUG
, "phy: %s", phy
);
972 os_strlcpy(iface
->phy
, phy
, sizeof(iface
->phy
));
977 * Make sure that all BSSes get configured with a pointer to the same
980 for (i
= 1; i
< iface
->num_bss
; i
++) {
981 iface
->bss
[i
]->driver
= hapd
->driver
;
982 iface
->bss
[i
]->drv_priv
= hapd
->drv_priv
;
985 if (hostapd_validate_bssid_configuration(iface
))
989 * Initialize control interfaces early to allow external monitoring of
990 * channel setup operations that may take considerable amount of time
991 * especially for DFS cases.
993 if (start_ctrl_iface(iface
))
996 if (hapd
->iconf
->country
[0] && hapd
->iconf
->country
[1]) {
997 char country
[4], previous_country
[4];
999 hostapd_set_state(iface
, HAPD_IFACE_COUNTRY_UPDATE
);
1000 if (hostapd_get_country(hapd
, previous_country
) < 0)
1001 previous_country
[0] = '\0';
1003 os_memcpy(country
, hapd
->iconf
->country
, 3);
1005 if (hostapd_set_country(hapd
, country
) < 0) {
1006 wpa_printf(MSG_ERROR
, "Failed to set country code");
1010 wpa_printf(MSG_DEBUG
, "Previous country code %s, new country code %s",
1011 previous_country
, country
);
1013 if (os_strncmp(previous_country
, country
, 2) != 0) {
1014 wpa_printf(MSG_DEBUG
, "Continue interface setup after channel list update");
1015 iface
->wait_channel_update
= 1;
1016 eloop_register_timeout(1, 0,
1017 channel_list_update_timeout
,
1023 return setup_interface2(iface
);
1027 static int setup_interface2(struct hostapd_iface
*iface
)
1029 iface
->wait_channel_update
= 0;
1031 if (hostapd_get_hw_features(iface
)) {
1032 /* Not all drivers support this yet, so continue without hw
1035 int ret
= hostapd_select_hw_mode(iface
);
1037 wpa_printf(MSG_ERROR
, "Could not select hw_mode and "
1038 "channel. (%d)", ret
);
1042 wpa_printf(MSG_DEBUG
, "Interface initialization will be completed in a callback (ACS)");
1045 ret
= hostapd_check_ht_capab(iface
);
1049 wpa_printf(MSG_DEBUG
, "Interface initialization will "
1050 "be completed in a callback");
1054 if (iface
->conf
->ieee80211h
)
1055 wpa_printf(MSG_DEBUG
, "DFS support is enabled");
1057 return hostapd_setup_interface_complete(iface
, 0);
1062 * hostapd_setup_interface_complete - Complete interface setup
1064 * This function is called when previous steps in the interface setup has been
1065 * completed. This can also start operations, e.g., DFS, that will require
1066 * additional processing before interface is ready to be enabled. Such
1067 * operations will call this function from eloop callbacks when finished.
1069 int hostapd_setup_interface_complete(struct hostapd_iface
*iface
, int err
)
1071 struct hostapd_data
*hapd
= iface
->bss
[0];
1076 wpa_printf(MSG_ERROR
, "Interface initialization failed");
1077 hostapd_set_state(iface
, HAPD_IFACE_DISABLED
);
1078 if (iface
->interfaces
&& iface
->interfaces
->terminate_on_error
)
1083 wpa_printf(MSG_DEBUG
, "Completing interface initialization");
1084 if (iface
->conf
->channel
) {
1087 #endif /* NEED_AP_MLME */
1089 iface
->freq
= hostapd_hw_get_freq(hapd
, iface
->conf
->channel
);
1090 wpa_printf(MSG_DEBUG
, "Mode: %s Channel: %d "
1091 "Frequency: %d MHz",
1092 hostapd_hw_mode_txt(iface
->conf
->hw_mode
),
1093 iface
->conf
->channel
, iface
->freq
);
1097 res
= hostapd_handle_dfs(iface
);
1100 #endif /* NEED_AP_MLME */
1102 if (hostapd_set_freq(hapd
, hapd
->iconf
->hw_mode
, iface
->freq
,
1103 hapd
->iconf
->channel
,
1104 hapd
->iconf
->ieee80211n
,
1105 hapd
->iconf
->ieee80211ac
,
1106 hapd
->iconf
->secondary_channel
,
1107 hapd
->iconf
->vht_oper_chwidth
,
1108 hapd
->iconf
->vht_oper_centr_freq_seg0_idx
,
1109 hapd
->iconf
->vht_oper_centr_freq_seg1_idx
)) {
1110 wpa_printf(MSG_ERROR
, "Could not set channel for "
1116 if (iface
->current_mode
) {
1117 if (hostapd_prepare_rates(iface
, iface
->current_mode
)) {
1118 wpa_printf(MSG_ERROR
, "Failed to prepare rates "
1120 hostapd_logger(hapd
, NULL
, HOSTAPD_MODULE_IEEE80211
,
1121 HOSTAPD_LEVEL_WARNING
,
1122 "Failed to prepare rates table.");
1127 if (hapd
->iconf
->rts_threshold
> -1 &&
1128 hostapd_set_rts(hapd
, hapd
->iconf
->rts_threshold
)) {
1129 wpa_printf(MSG_ERROR
, "Could not set RTS threshold for "
1134 if (hapd
->iconf
->fragm_threshold
> -1 &&
1135 hostapd_set_frag(hapd
, hapd
->iconf
->fragm_threshold
)) {
1136 wpa_printf(MSG_ERROR
, "Could not set fragmentation threshold "
1137 "for kernel driver");
1141 prev_addr
= hapd
->own_addr
;
1143 for (j
= 0; j
< iface
->num_bss
; j
++) {
1144 hapd
= iface
->bss
[j
];
1146 os_memcpy(hapd
->own_addr
, prev_addr
, ETH_ALEN
);
1147 if (hostapd_setup_bss(hapd
, j
== 0))
1149 if (hostapd_mac_comp_empty(hapd
->conf
->bssid
) == 0)
1150 prev_addr
= hapd
->own_addr
;
1152 hapd
= iface
->bss
[0];
1154 hostapd_tx_queue_params(iface
);
1156 ap_list_init(iface
);
1158 hostapd_set_acl(hapd
);
1160 if (hostapd_driver_commit(hapd
) < 0) {
1161 wpa_printf(MSG_ERROR
, "%s: Failed to commit driver "
1162 "configuration", __func__
);
1167 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1168 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1169 * mode), the interface is up only after driver_commit, so initialize
1170 * WPS after driver_commit.
1172 for (j
= 0; j
< iface
->num_bss
; j
++) {
1173 if (hostapd_init_wps_complete(iface
->bss
[j
]))
1177 hostapd_set_state(iface
, HAPD_IFACE_ENABLED
);
1178 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, AP_EVENT_ENABLED
);
1179 if (hapd
->setup_complete_cb
)
1180 hapd
->setup_complete_cb(hapd
->setup_complete_cb_ctx
);
1182 wpa_printf(MSG_DEBUG
, "%s: Setup of interface done.",
1183 iface
->bss
[0]->conf
->iface
);
1184 if (iface
->interfaces
&& iface
->interfaces
->terminate_on_error
> 0)
1185 iface
->interfaces
->terminate_on_error
--;
1192 * hostapd_setup_interface - Setup of an interface
1193 * @iface: Pointer to interface data.
1194 * Returns: 0 on success, -1 on failure
1196 * Initializes the driver interface, validates the configuration,
1197 * and sets driver parameters based on the configuration.
1198 * Flushes old stations, sets the channel, encryption,
1199 * beacons, and WDS links based on the configuration.
1201 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1202 * or DFS operations, this function returns 0 before such operations have been
1203 * completed. The pending operations are registered into eloop and will be
1204 * completed from eloop callbacks. Those callbacks end up calling
1205 * hostapd_setup_interface_complete() once setup has been completed.
1207 int hostapd_setup_interface(struct hostapd_iface
*iface
)
1211 ret
= setup_interface(iface
);
1213 wpa_printf(MSG_ERROR
, "%s: Unable to setup interface.",
1214 iface
->bss
[0]->conf
->iface
);
1223 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1224 * @hapd_iface: Pointer to interface data
1225 * @conf: Pointer to per-interface configuration
1226 * @bss: Pointer to per-BSS configuration for this BSS
1227 * Returns: Pointer to allocated BSS data
1229 * This function is used to allocate per-BSS data structure. This data will be
1230 * freed after hostapd_cleanup() is called for it during interface
1233 struct hostapd_data
*
1234 hostapd_alloc_bss_data(struct hostapd_iface
*hapd_iface
,
1235 struct hostapd_config
*conf
,
1236 struct hostapd_bss_config
*bss
)
1238 struct hostapd_data
*hapd
;
1240 hapd
= os_zalloc(sizeof(*hapd
));
1244 hapd
->new_assoc_sta_cb
= hostapd_new_assoc_sta
;
1247 hapd
->iface
= hapd_iface
;
1248 hapd
->driver
= hapd
->iconf
->driver
;
1249 hapd
->ctrl_sock
= -1;
1255 void hostapd_interface_deinit(struct hostapd_iface
*iface
)
1259 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
1263 eloop_cancel_timeout(channel_list_update_timeout
, iface
, NULL
);
1264 iface
->wait_channel_update
= 0;
1266 for (j
= iface
->num_bss
- 1; j
>= 0; j
--) {
1267 struct hostapd_data
*hapd
= iface
->bss
[j
];
1268 wpa_printf(MSG_DEBUG
, "%s: deinit bss %s", __func__
,
1270 hostapd_free_stas(hapd
);
1271 hostapd_flush_old_stations(hapd
, WLAN_REASON_DEAUTH_LEAVING
);
1272 hostapd_clear_wep(hapd
);
1273 hostapd_cleanup(hapd
);
1278 void hostapd_interface_free(struct hostapd_iface
*iface
)
1281 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
1282 for (j
= 0; j
< iface
->num_bss
; j
++) {
1283 wpa_printf(MSG_DEBUG
, "%s: free hapd %p",
1284 __func__
, iface
->bss
[j
]);
1285 os_free(iface
->bss
[j
]);
1287 hostapd_cleanup_iface(iface
);
1292 * hostapd_init - Allocate and initialize per-interface data
1293 * @config_file: Path to the configuration file
1294 * Returns: Pointer to the allocated interface data or %NULL on failure
1296 * This function is used to allocate main data structures for per-interface
1297 * data. The allocated data buffer will be freed by calling
1298 * hostapd_cleanup_iface().
1300 struct hostapd_iface
* hostapd_init(struct hapd_interfaces
*interfaces
,
1301 const char *config_file
)
1303 struct hostapd_iface
*hapd_iface
= NULL
;
1304 struct hostapd_config
*conf
= NULL
;
1305 struct hostapd_data
*hapd
;
1308 hapd_iface
= os_zalloc(sizeof(*hapd_iface
));
1309 if (hapd_iface
== NULL
)
1312 hapd_iface
->config_fname
= os_strdup(config_file
);
1313 if (hapd_iface
->config_fname
== NULL
)
1316 conf
= interfaces
->config_read_cb(hapd_iface
->config_fname
);
1319 hapd_iface
->conf
= conf
;
1321 hapd_iface
->num_bss
= conf
->num_bss
;
1322 hapd_iface
->bss
= os_calloc(conf
->num_bss
,
1323 sizeof(struct hostapd_data
*));
1324 if (hapd_iface
->bss
== NULL
)
1327 for (i
= 0; i
< conf
->num_bss
; i
++) {
1328 hapd
= hapd_iface
->bss
[i
] =
1329 hostapd_alloc_bss_data(hapd_iface
, conf
,
1333 hapd
->msg_ctx
= hapd
;
1339 wpa_printf(MSG_ERROR
, "Failed to set up interface with %s",
1342 hostapd_config_free(conf
);
1344 os_free(hapd_iface
->config_fname
);
1345 os_free(hapd_iface
->bss
);
1346 wpa_printf(MSG_DEBUG
, "%s: free iface %p",
1347 __func__
, hapd_iface
);
1348 os_free(hapd_iface
);
1354 static int ifname_in_use(struct hapd_interfaces
*interfaces
, const char *ifname
)
1358 for (i
= 0; i
< interfaces
->count
; i
++) {
1359 struct hostapd_iface
*iface
= interfaces
->iface
[i
];
1360 for (j
= 0; j
< iface
->num_bss
; j
++) {
1361 struct hostapd_data
*hapd
= iface
->bss
[j
];
1362 if (os_strcmp(ifname
, hapd
->conf
->iface
) == 0)
1372 * hostapd_interface_init_bss - Read configuration file and init BSS data
1374 * This function is used to parse configuration file for a BSS. This BSS is
1375 * added to an existing interface sharing the same radio (if any) or a new
1376 * interface is created if this is the first interface on a radio. This
1377 * allocate memory for the BSS. No actual driver operations are started.
1379 * This is similar to hostapd_interface_init(), but for a case where the
1380 * configuration is used to add a single BSS instead of all BSSes for a radio.
1382 struct hostapd_iface
*
1383 hostapd_interface_init_bss(struct hapd_interfaces
*interfaces
, const char *phy
,
1384 const char *config_fname
, int debug
)
1386 struct hostapd_iface
*new_iface
= NULL
, *iface
= NULL
;
1387 struct hostapd_data
*hapd
;
1394 for (i
= 0; i
< interfaces
->count
; i
++) {
1395 if (os_strcmp(interfaces
->iface
[i
]->phy
, phy
) == 0) {
1396 iface
= interfaces
->iface
[i
];
1401 wpa_printf(MSG_INFO
, "Configuration file: %s (phy %s)%s",
1402 config_fname
, phy
, iface
? "" : " --> new PHY");
1404 struct hostapd_config
*conf
;
1405 struct hostapd_bss_config
**tmp_conf
;
1406 struct hostapd_data
**tmp_bss
;
1407 struct hostapd_bss_config
*bss
;
1410 /* Add new BSS to existing iface */
1411 conf
= interfaces
->config_read_cb(config_fname
);
1414 if (conf
->num_bss
> 1) {
1415 wpa_printf(MSG_ERROR
, "Multiple BSSes specified in BSS-config");
1416 hostapd_config_free(conf
);
1420 ifname
= conf
->bss
[0]->iface
;
1421 if (ifname
[0] != '\0' && ifname_in_use(interfaces
, ifname
)) {
1422 wpa_printf(MSG_ERROR
,
1423 "Interface name %s already in use", ifname
);
1424 hostapd_config_free(conf
);
1428 tmp_conf
= os_realloc_array(
1429 iface
->conf
->bss
, iface
->conf
->num_bss
+ 1,
1430 sizeof(struct hostapd_bss_config
*));
1431 tmp_bss
= os_realloc_array(iface
->bss
, iface
->num_bss
+ 1,
1432 sizeof(struct hostapd_data
*));
1434 iface
->bss
= tmp_bss
;
1436 iface
->conf
->bss
= tmp_conf
;
1437 iface
->conf
->last_bss
= tmp_conf
[0];
1439 if (tmp_bss
== NULL
|| tmp_conf
== NULL
) {
1440 hostapd_config_free(conf
);
1443 bss
= iface
->conf
->bss
[iface
->conf
->num_bss
] = conf
->bss
[0];
1444 iface
->conf
->num_bss
++;
1446 hapd
= hostapd_alloc_bss_data(iface
, iface
->conf
, bss
);
1448 iface
->conf
->num_bss
--;
1449 hostapd_config_free(conf
);
1452 iface
->conf
->last_bss
= bss
;
1453 iface
->bss
[iface
->num_bss
] = hapd
;
1454 hapd
->msg_ctx
= hapd
;
1456 bss_idx
= iface
->num_bss
++;
1458 conf
->bss
[0] = NULL
;
1459 hostapd_config_free(conf
);
1461 /* Add a new iface with the first BSS */
1462 new_iface
= iface
= hostapd_init(interfaces
, config_fname
);
1465 os_strlcpy(iface
->phy
, phy
, sizeof(iface
->phy
));
1466 iface
->interfaces
= interfaces
;
1470 for (k
= 0; k
< debug
; k
++) {
1471 if (iface
->bss
[bss_idx
]->conf
->logger_stdout_level
> 0)
1472 iface
->bss
[bss_idx
]->conf
->logger_stdout_level
--;
1475 if (iface
->conf
->bss
[bss_idx
]->iface
[0] == '\0' &&
1476 !hostapd_drv_none(iface
->bss
[bss_idx
])) {
1477 wpa_printf(MSG_ERROR
, "Interface name not specified in %s",
1480 hostapd_interface_deinit_free(new_iface
);
1488 void hostapd_interface_deinit_free(struct hostapd_iface
*iface
)
1490 const struct wpa_driver_ops
*driver
;
1493 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
1496 wpa_printf(MSG_DEBUG
, "%s: num_bss=%u conf->num_bss=%u",
1497 __func__
, (unsigned int) iface
->num_bss
,
1498 (unsigned int) iface
->conf
->num_bss
);
1499 driver
= iface
->bss
[0]->driver
;
1500 drv_priv
= iface
->bss
[0]->drv_priv
;
1501 hostapd_interface_deinit(iface
);
1502 wpa_printf(MSG_DEBUG
, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1503 __func__
, driver
, drv_priv
);
1504 if (driver
&& driver
->hapd_deinit
&& drv_priv
)
1505 driver
->hapd_deinit(drv_priv
);
1506 hostapd_interface_free(iface
);
1510 int hostapd_enable_iface(struct hostapd_iface
*hapd_iface
)
1512 if (hapd_iface
->bss
[0]->drv_priv
!= NULL
) {
1513 wpa_printf(MSG_ERROR
, "Interface %s already enabled",
1514 hapd_iface
->conf
->bss
[0]->iface
);
1518 wpa_printf(MSG_DEBUG
, "Enable interface %s",
1519 hapd_iface
->conf
->bss
[0]->iface
);
1521 if (hapd_iface
->interfaces
== NULL
||
1522 hapd_iface
->interfaces
->driver_init
== NULL
||
1523 hapd_iface
->interfaces
->driver_init(hapd_iface
))
1526 if (hostapd_setup_interface(hapd_iface
)) {
1527 const struct wpa_driver_ops
*driver
;
1530 driver
= hapd_iface
->bss
[0]->driver
;
1531 drv_priv
= hapd_iface
->bss
[0]->drv_priv
;
1532 wpa_printf(MSG_DEBUG
, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1533 __func__
, driver
, drv_priv
);
1534 if (driver
&& driver
->hapd_deinit
&& drv_priv
) {
1535 driver
->hapd_deinit(drv_priv
);
1536 hapd_iface
->bss
[0]->drv_priv
= NULL
;
1545 int hostapd_reload_iface(struct hostapd_iface
*hapd_iface
)
1549 wpa_printf(MSG_DEBUG
, "Reload interface %s",
1550 hapd_iface
->conf
->bss
[0]->iface
);
1551 for (j
= 0; j
< hapd_iface
->num_bss
; j
++)
1552 hostapd_set_security_params(hapd_iface
->conf
->bss
[j
]);
1553 if (hostapd_config_check(hapd_iface
->conf
) < 0) {
1554 wpa_printf(MSG_ERROR
, "Updated configuration is invalid");
1557 hostapd_clear_old(hapd_iface
);
1558 for (j
= 0; j
< hapd_iface
->num_bss
; j
++)
1559 hostapd_reload_bss(hapd_iface
->bss
[j
]);
1565 int hostapd_disable_iface(struct hostapd_iface
*hapd_iface
)
1568 const struct wpa_driver_ops
*driver
;
1571 if (hapd_iface
== NULL
)
1573 wpa_msg(hapd_iface
->bss
[0]->msg_ctx
, MSG_INFO
, AP_EVENT_DISABLED
);
1574 driver
= hapd_iface
->bss
[0]->driver
;
1575 drv_priv
= hapd_iface
->bss
[0]->drv_priv
;
1577 /* whatever hostapd_interface_deinit does */
1578 for (j
= 0; j
< hapd_iface
->num_bss
; j
++) {
1579 struct hostapd_data
*hapd
= hapd_iface
->bss
[j
];
1580 hostapd_free_stas(hapd
);
1581 hostapd_flush_old_stations(hapd
, WLAN_REASON_DEAUTH_LEAVING
);
1582 hostapd_clear_wep(hapd
);
1583 hostapd_free_hapd_data(hapd
);
1586 wpa_printf(MSG_DEBUG
, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1587 __func__
, driver
, drv_priv
);
1588 if (driver
&& driver
->hapd_deinit
&& drv_priv
) {
1589 driver
->hapd_deinit(drv_priv
);
1590 hapd_iface
->bss
[0]->drv_priv
= NULL
;
1593 /* From hostapd_cleanup_iface: These were initialized in
1594 * hostapd_setup_interface and hostapd_setup_interface_complete
1596 hostapd_cleanup_iface_partial(hapd_iface
);
1598 wpa_printf(MSG_DEBUG
, "Interface %s disabled",
1599 hapd_iface
->bss
[0]->conf
->iface
);
1600 hostapd_set_state(hapd_iface
, HAPD_IFACE_DISABLED
);
1605 static struct hostapd_iface
*
1606 hostapd_iface_alloc(struct hapd_interfaces
*interfaces
)
1608 struct hostapd_iface
**iface
, *hapd_iface
;
1610 iface
= os_realloc_array(interfaces
->iface
, interfaces
->count
+ 1,
1611 sizeof(struct hostapd_iface
*));
1614 interfaces
->iface
= iface
;
1615 hapd_iface
= interfaces
->iface
[interfaces
->count
] =
1616 os_zalloc(sizeof(*hapd_iface
));
1617 if (hapd_iface
== NULL
) {
1618 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory for "
1619 "the interface", __func__
);
1622 interfaces
->count
++;
1623 hapd_iface
->interfaces
= interfaces
;
1629 static struct hostapd_config
*
1630 hostapd_config_alloc(struct hapd_interfaces
*interfaces
, const char *ifname
,
1631 const char *ctrl_iface
)
1633 struct hostapd_bss_config
*bss
;
1634 struct hostapd_config
*conf
;
1636 /* Allocates memory for bss and conf */
1637 conf
= hostapd_config_defaults();
1639 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory for "
1640 "configuration", __func__
);
1644 conf
->driver
= wpa_drivers
[0];
1645 if (conf
->driver
== NULL
) {
1646 wpa_printf(MSG_ERROR
, "No driver wrappers registered!");
1647 hostapd_config_free(conf
);
1651 bss
= conf
->last_bss
= conf
->bss
[0];
1653 os_strlcpy(bss
->iface
, ifname
, sizeof(bss
->iface
));
1654 bss
->ctrl_interface
= os_strdup(ctrl_iface
);
1655 if (bss
->ctrl_interface
== NULL
) {
1656 hostapd_config_free(conf
);
1660 /* Reading configuration file skipped, will be done in SET!
1661 * From reading the configuration till the end has to be done in
1668 static struct hostapd_iface
* hostapd_data_alloc(
1669 struct hapd_interfaces
*interfaces
, struct hostapd_config
*conf
)
1672 struct hostapd_iface
*hapd_iface
=
1673 interfaces
->iface
[interfaces
->count
- 1];
1674 struct hostapd_data
*hapd
;
1676 hapd_iface
->conf
= conf
;
1677 hapd_iface
->num_bss
= conf
->num_bss
;
1679 hapd_iface
->bss
= os_zalloc(conf
->num_bss
*
1680 sizeof(struct hostapd_data
*));
1681 if (hapd_iface
->bss
== NULL
)
1684 for (i
= 0; i
< conf
->num_bss
; i
++) {
1685 hapd
= hapd_iface
->bss
[i
] =
1686 hostapd_alloc_bss_data(hapd_iface
, conf
, conf
->bss
[i
]);
1689 hapd
->msg_ctx
= hapd
;
1692 hapd_iface
->interfaces
= interfaces
;
1698 int hostapd_add_iface(struct hapd_interfaces
*interfaces
, char *buf
)
1700 struct hostapd_config
*conf
= NULL
;
1701 struct hostapd_iface
*hapd_iface
= NULL
, *new_iface
= NULL
;
1702 struct hostapd_data
*hapd
;
1705 const char *conf_file
= NULL
, *phy_name
= NULL
;
1707 if (os_strncmp(buf
, "bss_config=", 11) == 0) {
1709 phy_name
= buf
+ 11;
1710 pos
= os_strchr(phy_name
, ':');
1715 if (!os_strlen(conf_file
))
1718 hapd_iface
= hostapd_interface_init_bss(interfaces
, phy_name
,
1722 for (j
= 0; j
< interfaces
->count
; j
++) {
1723 if (interfaces
->iface
[j
] == hapd_iface
)
1726 if (j
== interfaces
->count
) {
1727 struct hostapd_iface
**tmp
;
1728 tmp
= os_realloc_array(interfaces
->iface
,
1729 interfaces
->count
+ 1,
1730 sizeof(struct hostapd_iface
*));
1732 hostapd_interface_deinit_free(hapd_iface
);
1735 interfaces
->iface
= tmp
;
1736 interfaces
->iface
[interfaces
->count
++] = hapd_iface
;
1737 new_iface
= hapd_iface
;
1741 if (interfaces
->driver_init(hapd_iface
) ||
1742 hostapd_setup_interface(hapd_iface
)) {
1743 interfaces
->count
--;
1747 /* Assign new BSS with bss[0]'s driver info */
1748 hapd
= hapd_iface
->bss
[hapd_iface
->num_bss
- 1];
1749 hapd
->driver
= hapd_iface
->bss
[0]->driver
;
1750 hapd
->drv_priv
= hapd_iface
->bss
[0]->drv_priv
;
1751 os_memcpy(hapd
->own_addr
, hapd_iface
->bss
[0]->own_addr
,
1754 if (start_ctrl_iface_bss(hapd
) < 0 ||
1755 hostapd_setup_bss(hapd
, -1)) {
1756 hapd_iface
->conf
->num_bss
--;
1757 hapd_iface
->num_bss
--;
1758 wpa_printf(MSG_DEBUG
, "%s: free hapd %p %s",
1759 __func__
, hapd
, hapd
->conf
->iface
);
1767 ptr
= os_strchr(buf
, ' ');
1772 if (os_strncmp(ptr
, "config=", 7) == 0)
1773 conf_file
= ptr
+ 7;
1775 for (i
= 0; i
< interfaces
->count
; i
++) {
1776 if (!os_strcmp(interfaces
->iface
[i
]->conf
->bss
[0]->iface
,
1778 wpa_printf(MSG_INFO
, "Cannot add interface - it "
1784 hapd_iface
= hostapd_iface_alloc(interfaces
);
1785 if (hapd_iface
== NULL
) {
1786 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory "
1787 "for interface", __func__
);
1791 if (conf_file
&& interfaces
->config_read_cb
) {
1792 conf
= interfaces
->config_read_cb(conf_file
);
1793 if (conf
&& conf
->bss
)
1794 os_strlcpy(conf
->bss
[0]->iface
, buf
,
1795 sizeof(conf
->bss
[0]->iface
));
1797 conf
= hostapd_config_alloc(interfaces
, buf
, ptr
);
1798 if (conf
== NULL
|| conf
->bss
== NULL
) {
1799 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory "
1800 "for configuration", __func__
);
1804 hapd_iface
= hostapd_data_alloc(interfaces
, conf
);
1805 if (hapd_iface
== NULL
) {
1806 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory "
1807 "for hostapd", __func__
);
1811 if (start_ctrl_iface(hapd_iface
) < 0)
1814 wpa_printf(MSG_INFO
, "Add interface '%s'", conf
->bss
[0]->iface
);
1820 hostapd_config_free(conf
);
1822 if (hapd_iface
->bss
) {
1823 for (i
= 0; i
< hapd_iface
->num_bss
; i
++) {
1824 hapd
= hapd_iface
->bss
[i
];
1825 if (hapd
&& hapd_iface
->interfaces
&&
1826 hapd_iface
->interfaces
->ctrl_iface_deinit
)
1827 hapd_iface
->interfaces
->
1828 ctrl_iface_deinit(hapd
);
1829 wpa_printf(MSG_DEBUG
, "%s: free hapd %p (%s)",
1830 __func__
, hapd_iface
->bss
[i
],
1831 hapd_iface
->bss
[i
]->conf
->iface
);
1832 os_free(hapd_iface
->bss
[i
]);
1834 os_free(hapd_iface
->bss
);
1836 wpa_printf(MSG_DEBUG
, "%s: free iface %p",
1837 __func__
, hapd_iface
);
1838 os_free(hapd_iface
);
1844 static int hostapd_remove_bss(struct hostapd_iface
*iface
, unsigned int idx
)
1848 wpa_printf(MSG_INFO
, "Remove BSS '%s'", iface
->conf
->bss
[idx
]->iface
);
1850 /* Remove hostapd_data only if it has already been initialized */
1851 if (idx
< iface
->num_bss
) {
1852 struct hostapd_data
*hapd
= iface
->bss
[idx
];
1854 hostapd_free_stas(hapd
);
1855 hostapd_flush_old_stations(hapd
, WLAN_REASON_DEAUTH_LEAVING
);
1856 hostapd_clear_wep(hapd
);
1857 hostapd_cleanup(hapd
);
1858 wpa_printf(MSG_DEBUG
, "%s: free hapd %p (%s)",
1859 __func__
, hapd
, hapd
->conf
->iface
);
1860 hostapd_config_free_bss(hapd
->conf
);
1865 for (i
= idx
; i
< iface
->num_bss
; i
++)
1866 iface
->bss
[i
] = iface
->bss
[i
+ 1];
1868 hostapd_config_free_bss(iface
->conf
->bss
[idx
]);
1869 iface
->conf
->bss
[idx
] = NULL
;
1872 iface
->conf
->num_bss
--;
1873 for (i
= idx
; i
< iface
->conf
->num_bss
; i
++)
1874 iface
->conf
->bss
[i
] = iface
->conf
->bss
[i
+ 1];
1880 int hostapd_remove_iface(struct hapd_interfaces
*interfaces
, char *buf
)
1882 struct hostapd_iface
*hapd_iface
;
1885 for (i
= 0; i
< interfaces
->count
; i
++) {
1886 hapd_iface
= interfaces
->iface
[i
];
1887 if (hapd_iface
== NULL
)
1889 if (!os_strcmp(hapd_iface
->conf
->bss
[0]->iface
, buf
)) {
1890 wpa_printf(MSG_INFO
, "Remove interface '%s'", buf
);
1891 hostapd_interface_deinit_free(hapd_iface
);
1893 while (k
< (interfaces
->count
- 1)) {
1894 interfaces
->iface
[k
] =
1895 interfaces
->iface
[k
+ 1];
1898 interfaces
->count
--;
1902 for (j
= 0; j
< hapd_iface
->conf
->num_bss
; j
++) {
1903 if (!os_strcmp(hapd_iface
->conf
->bss
[j
]->iface
, buf
))
1904 return hostapd_remove_bss(hapd_iface
, j
);
1912 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1913 * @hapd: Pointer to BSS data
1914 * @sta: Pointer to the associated STA data
1915 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1917 * This function will be called whenever a station associates with the AP. It
1918 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1919 * from drv_callbacks.c based on driver events for drivers that take care of
1920 * management frames (IEEE 802.11 authentication and association) internally.
1922 void hostapd_new_assoc_sta(struct hostapd_data
*hapd
, struct sta_info
*sta
,
1925 if (hapd
->tkip_countermeasures
) {
1926 hostapd_drv_sta_deauth(hapd
, sta
->addr
,
1927 WLAN_REASON_MICHAEL_MIC_FAILURE
);
1931 hostapd_prune_associations(hapd
, sta
->addr
);
1933 /* IEEE 802.11F (IAPP) */
1934 if (hapd
->conf
->ieee802_11f
)
1935 iapp_new_station(hapd
->iapp
, sta
);
1938 if (sta
->p2p_ie
== NULL
&& !sta
->no_p2p_set
) {
1939 sta
->no_p2p_set
= 1;
1940 hapd
->num_sta_no_p2p
++;
1941 if (hapd
->num_sta_no_p2p
== 1)
1942 hostapd_p2p_non_p2p_sta_connected(hapd
);
1944 #endif /* CONFIG_P2P */
1946 /* Start accounting here, if IEEE 802.1X and WPA are not used.
1947 * IEEE 802.1X/WPA code will start accounting after the station has
1948 * been authorized. */
1949 if (!hapd
->conf
->ieee802_1x
&& !hapd
->conf
->wpa
) {
1950 os_get_time(&sta
->connected_time
);
1951 accounting_sta_start(hapd
, sta
);
1954 /* Start IEEE 802.1X authentication process for new stations */
1955 ieee802_1x_new_station(hapd
, sta
);
1957 if (sta
->auth_alg
!= WLAN_AUTH_FT
&&
1958 !(sta
->flags
& (WLAN_STA_WPS
| WLAN_STA_MAYBE_WPS
)))
1959 wpa_auth_sm_event(sta
->wpa_sm
, WPA_REAUTH
);
1961 wpa_auth_sta_associated(hapd
->wpa_auth
, sta
->wpa_sm
);
1963 wpa_printf(MSG_DEBUG
, "%s: reschedule ap_handle_timer timeout "
1964 "for " MACSTR
" (%d seconds - ap_max_inactivity)",
1965 __func__
, MAC2STR(sta
->addr
),
1966 hapd
->conf
->ap_max_inactivity
);
1967 eloop_cancel_timeout(ap_handle_timer
, hapd
, sta
);
1968 eloop_register_timeout(hapd
->conf
->ap_max_inactivity
, 0,
1969 ap_handle_timer
, hapd
, sta
);
1973 const char * hostapd_state_text(enum hostapd_iface_state s
)
1976 case HAPD_IFACE_UNINITIALIZED
:
1977 return "UNINITIALIZED";
1978 case HAPD_IFACE_DISABLED
:
1980 case HAPD_IFACE_COUNTRY_UPDATE
:
1981 return "COUNTRY_UPDATE";
1982 case HAPD_IFACE_ACS
:
1984 case HAPD_IFACE_HT_SCAN
:
1986 case HAPD_IFACE_DFS
:
1988 case HAPD_IFACE_ENABLED
:
1996 void hostapd_set_state(struct hostapd_iface
*iface
, enum hostapd_iface_state s
)
1998 wpa_printf(MSG_INFO
, "%s: interface state %s->%s",
1999 iface
->conf
->bss
[0]->iface
, hostapd_state_text(iface
->state
),
2000 hostapd_state_text(s
));