2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2014, 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 "eap_server/tncs.h"
18 #include "eapol_auth/eapol_auth_sm.h"
19 #include "eapol_auth/eapol_auth_sm_i.h"
23 #include "accounting.h"
27 #include "ieee802_1x.h"
28 #include "ieee802_11_auth.h"
29 #include "vlan_init.h"
31 #include "wps_hostapd.h"
32 #include "hw_features.h"
33 #include "wpa_auth_glue.h"
34 #include "ap_drv_ops.h"
35 #include "ap_config.h"
36 #include "p2p_hostapd.h"
39 #include "ieee802_11.h"
42 #include "dhcp_snoop.h"
43 #include "ndisc_snoop.h"
46 static int hostapd_flush_old_stations(struct hostapd_data
*hapd
, u16 reason
);
47 static int hostapd_setup_encryption(char *iface
, struct hostapd_data
*hapd
);
48 static int hostapd_broadcast_wep_clear(struct hostapd_data
*hapd
);
49 static int setup_interface2(struct hostapd_iface
*iface
);
50 static void channel_list_update_timeout(void *eloop_ctx
, void *timeout_ctx
);
53 int hostapd_for_each_interface(struct hapd_interfaces
*interfaces
,
54 int (*cb
)(struct hostapd_iface
*iface
,
55 void *ctx
), void *ctx
)
60 for (i
= 0; i
< interfaces
->count
; i
++) {
61 ret
= cb(interfaces
->iface
[i
], ctx
);
70 static void hostapd_reload_bss(struct hostapd_data
*hapd
)
72 struct hostapd_ssid
*ssid
;
74 #ifndef CONFIG_NO_RADIUS
75 radius_client_reconfig(hapd
->radius
, hapd
->conf
->radius
);
76 #endif /* CONFIG_NO_RADIUS */
78 ssid
= &hapd
->conf
->ssid
;
79 if (!ssid
->wpa_psk_set
&& ssid
->wpa_psk
&& !ssid
->wpa_psk
->next
&&
80 ssid
->wpa_passphrase_set
&& ssid
->wpa_passphrase
) {
82 * Force PSK to be derived again since SSID or passphrase may
85 hostapd_config_clear_wpa_psk(&hapd
->conf
->ssid
.wpa_psk
);
87 if (hostapd_setup_wpa_psk(hapd
->conf
)) {
88 wpa_printf(MSG_ERROR
, "Failed to re-configure WPA PSK "
89 "after reloading configuration");
92 if (hapd
->conf
->ieee802_1x
|| hapd
->conf
->wpa
)
93 hostapd_set_drv_ieee8021x(hapd
, hapd
->conf
->iface
, 1);
95 hostapd_set_drv_ieee8021x(hapd
, hapd
->conf
->iface
, 0);
97 if ((hapd
->conf
->wpa
|| hapd
->conf
->osen
) && hapd
->wpa_auth
== NULL
) {
98 hostapd_setup_wpa(hapd
);
100 wpa_init_keys(hapd
->wpa_auth
);
101 } else if (hapd
->conf
->wpa
) {
104 hostapd_reconfig_wpa(hapd
);
105 wpa_ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &wpa_ie_len
);
106 if (hostapd_set_generic_elem(hapd
, wpa_ie
, wpa_ie_len
))
107 wpa_printf(MSG_ERROR
, "Failed to configure WPA IE for "
108 "the kernel driver.");
109 } else if (hapd
->wpa_auth
) {
110 wpa_deinit(hapd
->wpa_auth
);
111 hapd
->wpa_auth
= NULL
;
112 hostapd_set_privacy(hapd
, 0);
113 hostapd_setup_encryption(hapd
->conf
->iface
, hapd
);
114 hostapd_set_generic_elem(hapd
, (u8
*) "", 0);
117 ieee802_11_set_beacon(hapd
);
118 hostapd_update_wps(hapd
);
120 if (hapd
->conf
->ssid
.ssid_set
&&
121 hostapd_set_ssid(hapd
, hapd
->conf
->ssid
.ssid
,
122 hapd
->conf
->ssid
.ssid_len
)) {
123 wpa_printf(MSG_ERROR
, "Could not set SSID for kernel driver");
124 /* try to continue */
126 wpa_printf(MSG_DEBUG
, "Reconfigured interface %s", hapd
->conf
->iface
);
130 static void hostapd_clear_old(struct hostapd_iface
*iface
)
135 * Deauthenticate all stations since the new configuration may not
136 * allow them to use the BSS anymore.
138 for (j
= 0; j
< iface
->num_bss
; j
++) {
139 hostapd_flush_old_stations(iface
->bss
[j
],
140 WLAN_REASON_PREV_AUTH_NOT_VALID
);
141 hostapd_broadcast_wep_clear(iface
->bss
[j
]);
143 #ifndef CONFIG_NO_RADIUS
144 /* TODO: update dynamic data based on changed configuration
145 * items (e.g., open/close sockets, etc.) */
146 radius_client_flush(iface
->bss
[j
]->radius
, 0);
147 #endif /* CONFIG_NO_RADIUS */
152 int hostapd_reload_config(struct hostapd_iface
*iface
)
154 struct hostapd_data
*hapd
= iface
->bss
[0];
155 struct hostapd_config
*newconf
, *oldconf
;
158 if (iface
->config_fname
== NULL
) {
159 /* Only in-memory config in use - assume it has been updated */
160 hostapd_clear_old(iface
);
161 for (j
= 0; j
< iface
->num_bss
; j
++)
162 hostapd_reload_bss(iface
->bss
[j
]);
166 if (iface
->interfaces
== NULL
||
167 iface
->interfaces
->config_read_cb
== NULL
)
169 newconf
= iface
->interfaces
->config_read_cb(iface
->config_fname
);
173 hostapd_clear_old(iface
);
175 oldconf
= hapd
->iconf
;
176 iface
->conf
= newconf
;
178 for (j
= 0; j
< iface
->num_bss
; j
++) {
179 hapd
= iface
->bss
[j
];
180 hapd
->iconf
= newconf
;
181 hapd
->iconf
->channel
= oldconf
->channel
;
182 hapd
->iconf
->acs
= oldconf
->acs
;
183 hapd
->iconf
->secondary_channel
= oldconf
->secondary_channel
;
184 hapd
->iconf
->ieee80211n
= oldconf
->ieee80211n
;
185 hapd
->iconf
->ieee80211ac
= oldconf
->ieee80211ac
;
186 hapd
->iconf
->ht_capab
= oldconf
->ht_capab
;
187 hapd
->iconf
->vht_capab
= oldconf
->vht_capab
;
188 hapd
->iconf
->vht_oper_chwidth
= oldconf
->vht_oper_chwidth
;
189 hapd
->iconf
->vht_oper_centr_freq_seg0_idx
=
190 oldconf
->vht_oper_centr_freq_seg0_idx
;
191 hapd
->iconf
->vht_oper_centr_freq_seg1_idx
=
192 oldconf
->vht_oper_centr_freq_seg1_idx
;
193 hapd
->conf
= newconf
->bss
[j
];
194 hostapd_reload_bss(hapd
);
197 hostapd_config_free(oldconf
);
204 static void hostapd_broadcast_key_clear_iface(struct hostapd_data
*hapd
,
209 for (i
= 0; i
< NUM_WEP_KEYS
; i
++) {
210 if (hostapd_drv_set_key(ifname
, hapd
, WPA_ALG_NONE
, NULL
, i
,
211 0, NULL
, 0, NULL
, 0)) {
212 wpa_printf(MSG_DEBUG
, "Failed to clear default "
213 "encryption keys (ifname=%s keyidx=%d)",
217 #ifdef CONFIG_IEEE80211W
218 if (hapd
->conf
->ieee80211w
) {
219 for (i
= NUM_WEP_KEYS
; i
< NUM_WEP_KEYS
+ 2; i
++) {
220 if (hostapd_drv_set_key(ifname
, hapd
, WPA_ALG_NONE
,
223 wpa_printf(MSG_DEBUG
, "Failed to clear "
224 "default mgmt encryption keys "
225 "(ifname=%s keyidx=%d)", ifname
, i
);
229 #endif /* CONFIG_IEEE80211W */
233 static int hostapd_broadcast_wep_clear(struct hostapd_data
*hapd
)
235 hostapd_broadcast_key_clear_iface(hapd
, hapd
->conf
->iface
);
240 static int hostapd_broadcast_wep_set(struct hostapd_data
*hapd
)
243 struct hostapd_ssid
*ssid
= &hapd
->conf
->ssid
;
246 if (ssid
->wep
.default_len
&&
247 hostapd_drv_set_key(hapd
->conf
->iface
,
248 hapd
, WPA_ALG_WEP
, broadcast_ether_addr
, idx
,
249 1, NULL
, 0, ssid
->wep
.key
[idx
],
250 ssid
->wep
.len
[idx
])) {
251 wpa_printf(MSG_WARNING
, "Could not set WEP encryption.");
259 static void hostapd_free_hapd_data(struct hostapd_data
*hapd
)
261 os_free(hapd
->probereq_cb
);
262 hapd
->probereq_cb
= NULL
;
265 wpabuf_free(hapd
->p2p_beacon_ie
);
266 hapd
->p2p_beacon_ie
= NULL
;
267 wpabuf_free(hapd
->p2p_probe_resp_ie
);
268 hapd
->p2p_probe_resp_ie
= NULL
;
269 #endif /* CONFIG_P2P */
271 if (!hapd
->started
) {
272 wpa_printf(MSG_ERROR
, "%s: Interface %s wasn't started",
273 __func__
, hapd
->conf
->iface
);
278 wpa_printf(MSG_DEBUG
, "%s(%s)", __func__
, hapd
->conf
->iface
);
279 iapp_deinit(hapd
->iapp
);
281 accounting_deinit(hapd
);
282 hostapd_deinit_wpa(hapd
);
284 hostapd_acl_deinit(hapd
);
285 #ifndef CONFIG_NO_RADIUS
286 radius_client_deinit(hapd
->radius
);
288 radius_das_deinit(hapd
->radius_das
);
289 hapd
->radius_das
= NULL
;
290 #endif /* CONFIG_NO_RADIUS */
292 hostapd_deinit_wps(hapd
);
294 authsrv_deinit(hapd
);
296 if (hapd
->interface_added
) {
297 hapd
->interface_added
= 0;
298 if (hostapd_if_remove(hapd
, WPA_IF_AP_BSS
, hapd
->conf
->iface
)) {
299 wpa_printf(MSG_WARNING
,
300 "Failed to remove BSS interface %s",
302 hapd
->interface_added
= 1;
305 * Since this was a dynamically added interface, the
306 * driver wrapper may have removed its internal instance
307 * and hapd->drv_priv is not valid anymore.
309 hapd
->drv_priv
= NULL
;
313 wpabuf_free(hapd
->time_adv
);
315 #ifdef CONFIG_INTERWORKING
316 gas_serv_deinit(hapd
);
317 #endif /* CONFIG_INTERWORKING */
319 bss_load_update_deinit(hapd
);
320 ndisc_snoop_deinit(hapd
);
321 dhcp_snoop_deinit(hapd
);
322 x_snoop_deinit(hapd
);
325 bin_clear_free(hapd
->tmp_eap_user
.identity
,
326 hapd
->tmp_eap_user
.identity_len
);
327 bin_clear_free(hapd
->tmp_eap_user
.password
,
328 hapd
->tmp_eap_user
.password_len
);
329 #endif /* CONFIG_SQLITE */
332 wpabuf_free(hapd
->mesh_pending_auth
);
333 hapd
->mesh_pending_auth
= NULL
;
334 #endif /* CONFIG_MESH */
339 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
340 * @hapd: Pointer to BSS data
342 * This function is used to free all per-BSS data structures and resources.
343 * Most of the modules that are initialized in hostapd_setup_bss() are
344 * deinitialized here.
346 static void hostapd_cleanup(struct hostapd_data
*hapd
)
348 wpa_printf(MSG_DEBUG
, "%s(hapd=%p (%s))", __func__
, hapd
,
350 if (hapd
->iface
->interfaces
&&
351 hapd
->iface
->interfaces
->ctrl_iface_deinit
)
352 hapd
->iface
->interfaces
->ctrl_iface_deinit(hapd
);
353 hostapd_free_hapd_data(hapd
);
357 static void hostapd_cleanup_iface_partial(struct hostapd_iface
*iface
)
359 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
360 #ifdef CONFIG_IEEE80211N
362 hostapd_stop_setup_timers(iface
);
363 #endif /* NEED_AP_MLME */
364 #endif /* CONFIG_IEEE80211N */
365 hostapd_free_hw_features(iface
->hw_features
, iface
->num_hw_features
);
366 iface
->hw_features
= NULL
;
367 os_free(iface
->current_rates
);
368 iface
->current_rates
= NULL
;
369 os_free(iface
->basic_rates
);
370 iface
->basic_rates
= NULL
;
371 ap_list_deinit(iface
);
376 * hostapd_cleanup_iface - Complete per-interface cleanup
377 * @iface: Pointer to interface data
379 * This function is called after per-BSS data structures are deinitialized
380 * with hostapd_cleanup().
382 static void hostapd_cleanup_iface(struct hostapd_iface
*iface
)
384 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
385 eloop_cancel_timeout(channel_list_update_timeout
, iface
, NULL
);
387 hostapd_cleanup_iface_partial(iface
);
388 hostapd_config_free(iface
->conf
);
391 os_free(iface
->config_fname
);
393 wpa_printf(MSG_DEBUG
, "%s: free iface=%p", __func__
, iface
);
398 static void hostapd_clear_wep(struct hostapd_data
*hapd
)
400 if (hapd
->drv_priv
&& !hapd
->iface
->driver_ap_teardown
) {
401 hostapd_set_privacy(hapd
, 0);
402 hostapd_broadcast_wep_clear(hapd
);
407 static int hostapd_setup_encryption(char *iface
, struct hostapd_data
*hapd
)
411 hostapd_broadcast_wep_set(hapd
);
413 if (hapd
->conf
->ssid
.wep
.default_len
) {
414 hostapd_set_privacy(hapd
, 1);
419 * When IEEE 802.1X is not enabled, the driver may need to know how to
420 * set authentication algorithms for static WEP.
422 hostapd_drv_set_authmode(hapd
, hapd
->conf
->auth_algs
);
424 for (i
= 0; i
< 4; i
++) {
425 if (hapd
->conf
->ssid
.wep
.key
[i
] &&
426 hostapd_drv_set_key(iface
, hapd
, WPA_ALG_WEP
, NULL
, i
,
427 i
== hapd
->conf
->ssid
.wep
.idx
, NULL
, 0,
428 hapd
->conf
->ssid
.wep
.key
[i
],
429 hapd
->conf
->ssid
.wep
.len
[i
])) {
430 wpa_printf(MSG_WARNING
, "Could not set WEP "
434 if (hapd
->conf
->ssid
.wep
.key
[i
] &&
435 i
== hapd
->conf
->ssid
.wep
.idx
)
436 hostapd_set_privacy(hapd
, 1);
443 static int hostapd_flush_old_stations(struct hostapd_data
*hapd
, u16 reason
)
448 if (hostapd_drv_none(hapd
) || hapd
->drv_priv
== NULL
)
451 if (!hapd
->iface
->driver_ap_teardown
) {
452 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
,
453 "Flushing old station entries");
455 if (hostapd_flush(hapd
)) {
456 wpa_msg(hapd
->msg_ctx
, MSG_WARNING
,
457 "Could not connect to kernel driver");
461 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "Deauthenticate all stations");
462 os_memset(addr
, 0xff, ETH_ALEN
);
463 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
464 hostapd_free_stas(hapd
);
470 static void hostapd_bss_deinit_no_free(struct hostapd_data
*hapd
)
472 hostapd_free_stas(hapd
);
473 hostapd_flush_old_stations(hapd
, WLAN_REASON_DEAUTH_LEAVING
);
474 hostapd_clear_wep(hapd
);
479 * hostapd_validate_bssid_configuration - Validate BSSID configuration
480 * @iface: Pointer to interface data
481 * Returns: 0 on success, -1 on failure
483 * This function is used to validate that the configured BSSIDs are valid.
485 static int hostapd_validate_bssid_configuration(struct hostapd_iface
*iface
)
487 u8 mask
[ETH_ALEN
] = { 0 };
488 struct hostapd_data
*hapd
= iface
->bss
[0];
489 unsigned int i
= iface
->conf
->num_bss
, bits
= 0, j
;
492 if (hostapd_drv_none(hapd
))
495 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
497 /* Determine the bits necessary to cover the number of BSSIDs. */
498 for (i
--; i
; i
>>= 1)
501 /* Determine the bits necessary to any configured BSSIDs,
502 if they are higher than the number of BSSIDs. */
503 for (j
= 0; j
< iface
->conf
->num_bss
; j
++) {
504 if (hostapd_mac_comp_empty(iface
->conf
->bss
[j
]->bssid
) == 0) {
510 for (i
= 0; i
< ETH_ALEN
; i
++) {
512 iface
->conf
->bss
[j
]->bssid
[i
] ^
520 for (i
= 0; i
< ETH_ALEN
&& mask
[i
] == 0; i
++)
526 while (mask
[i
] != 0) {
536 wpa_printf(MSG_ERROR
, "Too many bits in the BSSID mask (%u)",
541 os_memset(mask
, 0xff, ETH_ALEN
);
543 for (i
= 5; i
> 5 - j
; i
--)
550 wpa_printf(MSG_DEBUG
, "BSS count %lu, BSSID mask " MACSTR
" (%d bits)",
551 (unsigned long) iface
->conf
->num_bss
, MAC2STR(mask
), bits
);
556 for (i
= 0; i
< ETH_ALEN
; i
++) {
557 if ((hapd
->own_addr
[i
] & mask
[i
]) != hapd
->own_addr
[i
]) {
558 wpa_printf(MSG_ERROR
, "Invalid BSSID mask " MACSTR
559 " for start address " MACSTR
".",
560 MAC2STR(mask
), MAC2STR(hapd
->own_addr
));
561 wpa_printf(MSG_ERROR
, "Start address must be the "
562 "first address in the block (i.e., addr "
563 "AND mask == addr).");
572 static int mac_in_conf(struct hostapd_config
*conf
, const void *a
)
576 for (i
= 0; i
< conf
->num_bss
; i
++) {
577 if (hostapd_mac_comp(conf
->bss
[i
]->bssid
, a
) == 0) {
586 #ifndef CONFIG_NO_RADIUS
588 static int hostapd_das_nas_mismatch(struct hostapd_data
*hapd
,
589 struct radius_das_attrs
*attr
)
591 if (attr
->nas_identifier
&&
592 (!hapd
->conf
->nas_identifier
||
593 os_strlen(hapd
->conf
->nas_identifier
) !=
594 attr
->nas_identifier_len
||
595 os_memcmp(hapd
->conf
->nas_identifier
, attr
->nas_identifier
,
596 attr
->nas_identifier_len
) != 0)) {
597 wpa_printf(MSG_DEBUG
, "RADIUS DAS: NAS-Identifier mismatch");
601 if (attr
->nas_ip_addr
&&
602 (hapd
->conf
->own_ip_addr
.af
!= AF_INET
||
603 os_memcmp(&hapd
->conf
->own_ip_addr
.u
.v4
, attr
->nas_ip_addr
, 4) !=
605 wpa_printf(MSG_DEBUG
, "RADIUS DAS: NAS-IP-Address mismatch");
610 if (attr
->nas_ipv6_addr
&&
611 (hapd
->conf
->own_ip_addr
.af
!= AF_INET6
||
612 os_memcmp(&hapd
->conf
->own_ip_addr
.u
.v6
, attr
->nas_ipv6_addr
, 16)
614 wpa_printf(MSG_DEBUG
, "RADIUS DAS: NAS-IPv6-Address mismatch");
617 #endif /* CONFIG_IPV6 */
623 static struct sta_info
* hostapd_das_find_sta(struct hostapd_data
*hapd
,
624 struct radius_das_attrs
*attr
,
627 struct sta_info
*selected
, *sta
;
634 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
)
635 sta
->radius_das_match
= 1;
637 if (attr
->sta_addr
) {
639 sta
= ap_get_sta(hapd
, attr
->sta_addr
);
641 wpa_printf(MSG_DEBUG
,
642 "RADIUS DAS: No Calling-Station-Id match");
647 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
649 sta
->radius_das_match
= 0;
651 wpa_printf(MSG_DEBUG
, "RADIUS DAS: Calling-Station-Id match");
654 if (attr
->acct_session_id
) {
656 if (attr
->acct_session_id_len
!= 17) {
657 wpa_printf(MSG_DEBUG
,
658 "RADIUS DAS: Acct-Session-Id cannot match");
663 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
664 if (!sta
->radius_das_match
)
666 os_snprintf(buf
, sizeof(buf
), "%08X-%08X",
667 sta
->acct_session_id_hi
,
668 sta
->acct_session_id_lo
);
669 if (os_memcmp(attr
->acct_session_id
, buf
, 17) != 0)
670 sta
->radius_das_match
= 0;
676 wpa_printf(MSG_DEBUG
,
677 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
680 wpa_printf(MSG_DEBUG
, "RADIUS DAS: Acct-Session-Id match");
683 if (attr
->acct_multi_session_id
) {
685 if (attr
->acct_multi_session_id_len
!= 17) {
686 wpa_printf(MSG_DEBUG
,
687 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
692 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
693 if (!sta
->radius_das_match
)
695 if (!sta
->eapol_sm
||
696 !sta
->eapol_sm
->acct_multi_session_id_hi
) {
697 sta
->radius_das_match
= 0;
700 os_snprintf(buf
, sizeof(buf
), "%08X+%08X",
701 sta
->eapol_sm
->acct_multi_session_id_hi
,
702 sta
->eapol_sm
->acct_multi_session_id_lo
);
703 if (os_memcmp(attr
->acct_multi_session_id
, buf
, 17) !=
705 sta
->radius_das_match
= 0;
711 wpa_printf(MSG_DEBUG
,
712 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
715 wpa_printf(MSG_DEBUG
,
716 "RADIUS DAS: Acct-Multi-Session-Id match");
723 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
726 if (!sta
->radius_das_match
)
728 cui
= ieee802_1x_get_radius_cui(sta
->eapol_sm
);
729 if (!cui
|| wpabuf_len(cui
) != attr
->cui_len
||
730 os_memcmp(wpabuf_head(cui
), attr
->cui
,
732 sta
->radius_das_match
= 0;
738 wpa_printf(MSG_DEBUG
,
739 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
742 wpa_printf(MSG_DEBUG
,
743 "RADIUS DAS: Chargeable-User-Identity match");
746 if (attr
->user_name
) {
750 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
754 if (!sta
->radius_das_match
)
756 identity
= ieee802_1x_get_identity(sta
->eapol_sm
,
759 identity_len
!= attr
->user_name_len
||
760 os_memcmp(identity
, attr
->user_name
, identity_len
)
762 sta
->radius_das_match
= 0;
768 wpa_printf(MSG_DEBUG
,
769 "RADIUS DAS: No matches remaining after User-Name check");
772 wpa_printf(MSG_DEBUG
,
773 "RADIUS DAS: User-Name match");
778 * In theory, we could match all current associations, but it
779 * seems safer to just reject requests that do not include any
780 * session identification attributes.
782 wpa_printf(MSG_DEBUG
,
783 "RADIUS DAS: No session identification attributes included");
788 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
789 if (sta
->radius_das_match
) {
802 static int hostapd_das_disconnect_pmksa(struct hostapd_data
*hapd
,
803 struct radius_das_attrs
*attr
)
807 return wpa_auth_radius_das_disconnect_pmksa(hapd
->wpa_auth
, attr
);
811 static enum radius_das_res
812 hostapd_das_disconnect(void *ctx
, struct radius_das_attrs
*attr
)
814 struct hostapd_data
*hapd
= ctx
;
815 struct sta_info
*sta
;
818 if (hostapd_das_nas_mismatch(hapd
, attr
))
819 return RADIUS_DAS_NAS_MISMATCH
;
821 sta
= hostapd_das_find_sta(hapd
, attr
, &multi
);
824 wpa_printf(MSG_DEBUG
,
825 "RADIUS DAS: Multiple sessions match - not supported");
826 return RADIUS_DAS_MULTI_SESSION_MATCH
;
828 if (hostapd_das_disconnect_pmksa(hapd
, attr
) == 0) {
829 wpa_printf(MSG_DEBUG
,
830 "RADIUS DAS: PMKSA cache entry matched");
831 return RADIUS_DAS_SUCCESS
;
833 wpa_printf(MSG_DEBUG
, "RADIUS DAS: No matching session found");
834 return RADIUS_DAS_SESSION_NOT_FOUND
;
837 wpa_printf(MSG_DEBUG
, "RADIUS DAS: Found a matching session " MACSTR
838 " - disconnecting", MAC2STR(sta
->addr
));
839 wpa_auth_pmksa_remove(hapd
->wpa_auth
, sta
->addr
);
841 hostapd_drv_sta_deauth(hapd
, sta
->addr
,
842 WLAN_REASON_PREV_AUTH_NOT_VALID
);
843 ap_sta_deauthenticate(hapd
, sta
, WLAN_REASON_PREV_AUTH_NOT_VALID
);
845 return RADIUS_DAS_SUCCESS
;
848 #endif /* CONFIG_NO_RADIUS */
852 * hostapd_setup_bss - Per-BSS setup (initialization)
853 * @hapd: Pointer to BSS data
854 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
855 * but interface may exist
857 * This function is used to initialize all per-BSS data structures and
858 * resources. This gets called in a loop for each BSS when an interface is
859 * initialized. Most of the modules that are initialized here will be
860 * deinitialized in hostapd_cleanup().
862 static int hostapd_setup_bss(struct hostapd_data
*hapd
, int first
)
864 struct hostapd_bss_config
*conf
= hapd
->conf
;
865 u8 ssid
[HOSTAPD_MAX_SSID_LEN
+ 1];
866 int ssid_len
, set_ssid
;
867 char force_ifname
[IFNAMSIZ
];
868 u8 if_addr
[ETH_ALEN
];
869 int flush_old_stations
= 1;
871 wpa_printf(MSG_DEBUG
, "%s(hapd=%p (%s), first=%d)",
872 __func__
, hapd
, conf
->iface
, first
);
874 #ifdef EAP_SERVER_TNC
875 if (conf
->tnc
&& tncs_global_init() < 0) {
876 wpa_printf(MSG_ERROR
, "Failed to initialize TNCS");
879 #endif /* EAP_SERVER_TNC */
882 wpa_printf(MSG_ERROR
, "%s: Interface %s was already started",
883 __func__
, conf
->iface
);
888 if (!first
|| first
== -1) {
889 if (hostapd_mac_comp_empty(conf
->bssid
) == 0) {
890 /* Allocate the next available BSSID. */
892 inc_byte_array(hapd
->own_addr
, ETH_ALEN
);
893 } while (mac_in_conf(hapd
->iconf
, hapd
->own_addr
));
895 /* Allocate the configured BSSID. */
896 os_memcpy(hapd
->own_addr
, conf
->bssid
, ETH_ALEN
);
898 if (hostapd_mac_comp(hapd
->own_addr
,
899 hapd
->iface
->bss
[0]->own_addr
) ==
901 wpa_printf(MSG_ERROR
, "BSS '%s' may not have "
902 "BSSID set to the MAC address of "
903 "the radio", conf
->iface
);
908 hapd
->interface_added
= 1;
909 if (hostapd_if_add(hapd
->iface
->bss
[0], WPA_IF_AP_BSS
,
910 conf
->iface
, hapd
->own_addr
, hapd
,
911 &hapd
->drv_priv
, force_ifname
, if_addr
,
912 conf
->bridge
[0] ? conf
->bridge
: NULL
,
914 wpa_printf(MSG_ERROR
, "Failed to add BSS (BSSID="
915 MACSTR
")", MAC2STR(hapd
->own_addr
));
916 hapd
->interface_added
= 0;
921 if (conf
->wmm_enabled
< 0)
922 conf
->wmm_enabled
= hapd
->iconf
->ieee80211n
;
925 if (hapd
->iface
->mconf
== NULL
)
926 flush_old_stations
= 0;
927 #endif /* CONFIG_MESH */
929 if (flush_old_stations
)
930 hostapd_flush_old_stations(hapd
,
931 WLAN_REASON_PREV_AUTH_NOT_VALID
);
932 hostapd_set_privacy(hapd
, 0);
934 hostapd_broadcast_wep_clear(hapd
);
935 if (hostapd_setup_encryption(conf
->iface
, hapd
))
939 * Fetch the SSID from the system and use it or,
940 * if one was specified in the config file, verify they
943 ssid_len
= hostapd_get_ssid(hapd
, ssid
, sizeof(ssid
));
945 wpa_printf(MSG_ERROR
, "Could not read SSID from system");
948 if (conf
->ssid
.ssid_set
) {
950 * If SSID is specified in the config file and it differs
951 * from what is being used then force installation of the
954 set_ssid
= (conf
->ssid
.ssid_len
!= (size_t) ssid_len
||
955 os_memcmp(conf
->ssid
.ssid
, ssid
, ssid_len
) != 0);
958 * No SSID in the config file; just use the one we got
962 conf
->ssid
.ssid_len
= ssid_len
;
963 os_memcpy(conf
->ssid
.ssid
, ssid
, conf
->ssid
.ssid_len
);
966 if (!hostapd_drv_none(hapd
)) {
967 wpa_printf(MSG_ERROR
, "Using interface %s with hwaddr " MACSTR
969 conf
->iface
, MAC2STR(hapd
->own_addr
),
970 wpa_ssid_txt(conf
->ssid
.ssid
, conf
->ssid
.ssid_len
));
973 if (hostapd_setup_wpa_psk(conf
)) {
974 wpa_printf(MSG_ERROR
, "WPA-PSK setup failed.");
978 /* Set SSID for the kernel driver (to be used in beacon and probe
979 * response frames) */
980 if (set_ssid
&& hostapd_set_ssid(hapd
, conf
->ssid
.ssid
,
981 conf
->ssid
.ssid_len
)) {
982 wpa_printf(MSG_ERROR
, "Could not set SSID for kernel driver");
986 if (wpa_debug_level
<= MSG_MSGDUMP
)
987 conf
->radius
->msg_dumps
= 1;
988 #ifndef CONFIG_NO_RADIUS
989 hapd
->radius
= radius_client_init(hapd
, conf
->radius
);
990 if (hapd
->radius
== NULL
) {
991 wpa_printf(MSG_ERROR
, "RADIUS client initialization failed.");
995 if (conf
->radius_das_port
) {
996 struct radius_das_conf das_conf
;
997 os_memset(&das_conf
, 0, sizeof(das_conf
));
998 das_conf
.port
= conf
->radius_das_port
;
999 das_conf
.shared_secret
= conf
->radius_das_shared_secret
;
1000 das_conf
.shared_secret_len
=
1001 conf
->radius_das_shared_secret_len
;
1002 das_conf
.client_addr
= &conf
->radius_das_client_addr
;
1003 das_conf
.time_window
= conf
->radius_das_time_window
;
1004 das_conf
.require_event_timestamp
=
1005 conf
->radius_das_require_event_timestamp
;
1006 das_conf
.ctx
= hapd
;
1007 das_conf
.disconnect
= hostapd_das_disconnect
;
1008 hapd
->radius_das
= radius_das_init(&das_conf
);
1009 if (hapd
->radius_das
== NULL
) {
1010 wpa_printf(MSG_ERROR
, "RADIUS DAS initialization "
1015 #endif /* CONFIG_NO_RADIUS */
1017 if (hostapd_acl_init(hapd
)) {
1018 wpa_printf(MSG_ERROR
, "ACL initialization failed.");
1021 if (hostapd_init_wps(hapd
, conf
))
1024 if (authsrv_init(hapd
) < 0)
1027 if (ieee802_1x_init(hapd
)) {
1028 wpa_printf(MSG_ERROR
, "IEEE 802.1X initialization failed.");
1032 if ((conf
->wpa
|| conf
->osen
) && hostapd_setup_wpa(hapd
))
1035 if (accounting_init(hapd
)) {
1036 wpa_printf(MSG_ERROR
, "Accounting initialization failed.");
1040 if (conf
->ieee802_11f
&&
1041 (hapd
->iapp
= iapp_init(hapd
, conf
->iapp_iface
)) == NULL
) {
1042 wpa_printf(MSG_ERROR
, "IEEE 802.11F (IAPP) initialization "
1047 #ifdef CONFIG_INTERWORKING
1048 if (gas_serv_init(hapd
)) {
1049 wpa_printf(MSG_ERROR
, "GAS server initialization failed");
1053 if (conf
->qos_map_set_len
&&
1054 hostapd_drv_set_qos_map(hapd
, conf
->qos_map_set
,
1055 conf
->qos_map_set_len
)) {
1056 wpa_printf(MSG_ERROR
, "Failed to initialize QoS Map");
1059 #endif /* CONFIG_INTERWORKING */
1061 if (conf
->bss_load_update_period
&& bss_load_update_init(hapd
)) {
1062 wpa_printf(MSG_ERROR
, "BSS Load initialization failed");
1066 if (conf
->proxy_arp
) {
1067 if (x_snoop_init(hapd
)) {
1068 wpa_printf(MSG_ERROR
,
1069 "Generic snooping infrastructure initialization failed");
1073 if (dhcp_snoop_init(hapd
)) {
1074 wpa_printf(MSG_ERROR
,
1075 "DHCP snooping initialization failed");
1079 if (ndisc_snoop_init(hapd
)) {
1080 wpa_printf(MSG_ERROR
,
1081 "Neighbor Discovery snooping initialization failed");
1086 if (!hostapd_drv_none(hapd
) && vlan_init(hapd
)) {
1087 wpa_printf(MSG_ERROR
, "VLAN initialization failed.");
1091 if (!conf
->start_disabled
&& ieee802_11_set_beacon(hapd
) < 0)
1094 if (hapd
->wpa_auth
&& wpa_init_keys(hapd
->wpa_auth
) < 0)
1097 if (hapd
->driver
&& hapd
->driver
->set_operstate
)
1098 hapd
->driver
->set_operstate(hapd
->drv_priv
, 1);
1104 static void hostapd_tx_queue_params(struct hostapd_iface
*iface
)
1106 struct hostapd_data
*hapd
= iface
->bss
[0];
1108 struct hostapd_tx_queue_params
*p
;
1111 if (iface
->mconf
== NULL
)
1113 #endif /* CONFIG_MESH */
1115 for (i
= 0; i
< NUM_TX_QUEUES
; i
++) {
1116 p
= &iface
->conf
->tx_queue
[i
];
1118 if (hostapd_set_tx_queue_params(hapd
, i
, p
->aifs
, p
->cwmin
,
1119 p
->cwmax
, p
->burst
)) {
1120 wpa_printf(MSG_DEBUG
, "Failed to set TX queue "
1121 "parameters for queue %d.", i
);
1122 /* Continue anyway */
1128 static int hostapd_set_acl_list(struct hostapd_data
*hapd
,
1129 struct mac_acl_entry
*mac_acl
,
1130 int n_entries
, u8 accept_acl
)
1132 struct hostapd_acl_params
*acl_params
;
1135 acl_params
= os_zalloc(sizeof(*acl_params
) +
1136 (n_entries
* sizeof(acl_params
->mac_acl
[0])));
1140 for (i
= 0; i
< n_entries
; i
++)
1141 os_memcpy(acl_params
->mac_acl
[i
].addr
, mac_acl
[i
].addr
,
1144 acl_params
->acl_policy
= accept_acl
;
1145 acl_params
->num_mac_acl
= n_entries
;
1147 err
= hostapd_drv_set_acl(hapd
, acl_params
);
1149 os_free(acl_params
);
1155 static void hostapd_set_acl(struct hostapd_data
*hapd
)
1157 struct hostapd_config
*conf
= hapd
->iconf
;
1161 if (hapd
->iface
->drv_max_acl_mac_addrs
== 0)
1164 if (conf
->bss
[0]->macaddr_acl
== DENY_UNLESS_ACCEPTED
) {
1166 err
= hostapd_set_acl_list(hapd
, conf
->bss
[0]->accept_mac
,
1167 conf
->bss
[0]->num_accept_mac
,
1170 wpa_printf(MSG_DEBUG
, "Failed to set accept acl");
1173 } else if (conf
->bss
[0]->macaddr_acl
== ACCEPT_UNLESS_DENIED
) {
1175 err
= hostapd_set_acl_list(hapd
, conf
->bss
[0]->deny_mac
,
1176 conf
->bss
[0]->num_deny_mac
,
1179 wpa_printf(MSG_DEBUG
, "Failed to set deny acl");
1186 static int start_ctrl_iface_bss(struct hostapd_data
*hapd
)
1188 if (!hapd
->iface
->interfaces
||
1189 !hapd
->iface
->interfaces
->ctrl_iface_init
)
1192 if (hapd
->iface
->interfaces
->ctrl_iface_init(hapd
)) {
1193 wpa_printf(MSG_ERROR
,
1194 "Failed to setup control interface for %s",
1203 static int start_ctrl_iface(struct hostapd_iface
*iface
)
1207 if (!iface
->interfaces
|| !iface
->interfaces
->ctrl_iface_init
)
1210 for (i
= 0; i
< iface
->num_bss
; i
++) {
1211 struct hostapd_data
*hapd
= iface
->bss
[i
];
1212 if (iface
->interfaces
->ctrl_iface_init(hapd
)) {
1213 wpa_printf(MSG_ERROR
,
1214 "Failed to setup control interface for %s",
1224 static void channel_list_update_timeout(void *eloop_ctx
, void *timeout_ctx
)
1226 struct hostapd_iface
*iface
= eloop_ctx
;
1228 if (!iface
->wait_channel_update
) {
1229 wpa_printf(MSG_INFO
, "Channel list update timeout, but interface was not waiting for it");
1234 * It is possible that the existing channel list is acceptable, so try
1237 wpa_printf(MSG_DEBUG
, "Channel list update timeout - try to continue anyway");
1238 setup_interface2(iface
);
1242 void hostapd_channel_list_updated(struct hostapd_iface
*iface
, int initiator
)
1244 if (!iface
->wait_channel_update
|| initiator
!= REGDOM_SET_BY_USER
)
1247 wpa_printf(MSG_DEBUG
, "Channel list updated - continue setup");
1248 eloop_cancel_timeout(channel_list_update_timeout
, iface
, NULL
);
1249 setup_interface2(iface
);
1253 static int setup_interface(struct hostapd_iface
*iface
)
1255 struct hostapd_data
*hapd
= iface
->bss
[0];
1259 * It is possible that setup_interface() is called after the interface
1260 * was disabled etc., in which case driver_ap_teardown is possibly set
1261 * to 1. Clear it here so any other key/station deletion, which is not
1262 * part of a teardown flow, would also call the relevant driver
1265 iface
->driver_ap_teardown
= 0;
1267 if (!iface
->phy
[0]) {
1268 const char *phy
= hostapd_drv_get_radio_name(hapd
);
1270 wpa_printf(MSG_DEBUG
, "phy: %s", phy
);
1271 os_strlcpy(iface
->phy
, phy
, sizeof(iface
->phy
));
1276 * Make sure that all BSSes get configured with a pointer to the same
1279 for (i
= 1; i
< iface
->num_bss
; i
++) {
1280 iface
->bss
[i
]->driver
= hapd
->driver
;
1281 iface
->bss
[i
]->drv_priv
= hapd
->drv_priv
;
1284 if (hostapd_validate_bssid_configuration(iface
))
1288 * Initialize control interfaces early to allow external monitoring of
1289 * channel setup operations that may take considerable amount of time
1290 * especially for DFS cases.
1292 if (start_ctrl_iface(iface
))
1295 if (hapd
->iconf
->country
[0] && hapd
->iconf
->country
[1]) {
1296 char country
[4], previous_country
[4];
1298 hostapd_set_state(iface
, HAPD_IFACE_COUNTRY_UPDATE
);
1299 if (hostapd_get_country(hapd
, previous_country
) < 0)
1300 previous_country
[0] = '\0';
1302 os_memcpy(country
, hapd
->iconf
->country
, 3);
1304 if (hostapd_set_country(hapd
, country
) < 0) {
1305 wpa_printf(MSG_ERROR
, "Failed to set country code");
1309 wpa_printf(MSG_DEBUG
, "Previous country code %s, new country code %s",
1310 previous_country
, country
);
1312 if (os_strncmp(previous_country
, country
, 2) != 0) {
1313 wpa_printf(MSG_DEBUG
, "Continue interface setup after channel list update");
1314 iface
->wait_channel_update
= 1;
1315 eloop_register_timeout(5, 0,
1316 channel_list_update_timeout
,
1322 return setup_interface2(iface
);
1326 static int setup_interface2(struct hostapd_iface
*iface
)
1328 iface
->wait_channel_update
= 0;
1330 if (hostapd_get_hw_features(iface
)) {
1331 /* Not all drivers support this yet, so continue without hw
1334 int ret
= hostapd_select_hw_mode(iface
);
1336 wpa_printf(MSG_ERROR
, "Could not select hw_mode and "
1337 "channel. (%d)", ret
);
1341 wpa_printf(MSG_DEBUG
, "Interface initialization will be completed in a callback (ACS)");
1344 ret
= hostapd_check_ht_capab(iface
);
1348 wpa_printf(MSG_DEBUG
, "Interface initialization will "
1349 "be completed in a callback");
1353 if (iface
->conf
->ieee80211h
)
1354 wpa_printf(MSG_DEBUG
, "DFS support is enabled");
1356 return hostapd_setup_interface_complete(iface
, 0);
1359 hostapd_set_state(iface
, HAPD_IFACE_DISABLED
);
1360 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, AP_EVENT_DISABLED
);
1361 if (iface
->interfaces
&& iface
->interfaces
->terminate_on_error
)
1368 * hostapd_setup_interface_complete - Complete interface setup
1370 * This function is called when previous steps in the interface setup has been
1371 * completed. This can also start operations, e.g., DFS, that will require
1372 * additional processing before interface is ready to be enabled. Such
1373 * operations will call this function from eloop callbacks when finished.
1375 int hostapd_setup_interface_complete(struct hostapd_iface
*iface
, int err
)
1377 struct hostapd_data
*hapd
= iface
->bss
[0];
1380 int delay_apply_cfg
= 0;
1381 int res_dfs_offload
= 0;
1386 wpa_printf(MSG_DEBUG
, "Completing interface initialization");
1387 if (iface
->conf
->channel
) {
1390 #endif /* NEED_AP_MLME */
1392 iface
->freq
= hostapd_hw_get_freq(hapd
, iface
->conf
->channel
);
1393 wpa_printf(MSG_DEBUG
, "Mode: %s Channel: %d "
1394 "Frequency: %d MHz",
1395 hostapd_hw_mode_txt(iface
->conf
->hw_mode
),
1396 iface
->conf
->channel
, iface
->freq
);
1399 /* Handle DFS only if it is not offloaded to the driver */
1400 if (!(iface
->drv_flags
& WPA_DRIVER_FLAGS_DFS_OFFLOAD
)) {
1402 res
= hostapd_handle_dfs(iface
);
1409 /* If DFS is offloaded to the driver */
1410 res_dfs_offload
= hostapd_handle_dfs_offload(iface
);
1411 if (res_dfs_offload
<= 0) {
1412 if (res_dfs_offload
< 0)
1415 wpa_printf(MSG_DEBUG
,
1416 "Proceed with AP/channel setup");
1418 * If this is a DFS channel, move to completing
1421 if (res_dfs_offload
== 1)
1423 /* Otherwise fall through. */
1426 #endif /* NEED_AP_MLME */
1429 if (iface
->mconf
!= NULL
) {
1430 wpa_printf(MSG_DEBUG
,
1431 "%s: Mesh configuration will be applied while joining the mesh network",
1432 iface
->bss
[0]->conf
->iface
);
1433 delay_apply_cfg
= 1;
1435 #endif /* CONFIG_MESH */
1437 if (!delay_apply_cfg
&&
1438 hostapd_set_freq(hapd
, hapd
->iconf
->hw_mode
, iface
->freq
,
1439 hapd
->iconf
->channel
,
1440 hapd
->iconf
->ieee80211n
,
1441 hapd
->iconf
->ieee80211ac
,
1442 hapd
->iconf
->secondary_channel
,
1443 hapd
->iconf
->vht_oper_chwidth
,
1444 hapd
->iconf
->vht_oper_centr_freq_seg0_idx
,
1445 hapd
->iconf
->vht_oper_centr_freq_seg1_idx
)) {
1446 wpa_printf(MSG_ERROR
, "Could not set channel for "
1452 if (iface
->current_mode
) {
1453 if (hostapd_prepare_rates(iface
, iface
->current_mode
)) {
1454 wpa_printf(MSG_ERROR
, "Failed to prepare rates "
1456 hostapd_logger(hapd
, NULL
, HOSTAPD_MODULE_IEEE80211
,
1457 HOSTAPD_LEVEL_WARNING
,
1458 "Failed to prepare rates table.");
1463 if (hapd
->iconf
->rts_threshold
> -1 &&
1464 hostapd_set_rts(hapd
, hapd
->iconf
->rts_threshold
)) {
1465 wpa_printf(MSG_ERROR
, "Could not set RTS threshold for "
1470 if (hapd
->iconf
->fragm_threshold
> -1 &&
1471 hostapd_set_frag(hapd
, hapd
->iconf
->fragm_threshold
)) {
1472 wpa_printf(MSG_ERROR
, "Could not set fragmentation threshold "
1473 "for kernel driver");
1477 prev_addr
= hapd
->own_addr
;
1479 for (j
= 0; j
< iface
->num_bss
; j
++) {
1480 hapd
= iface
->bss
[j
];
1482 os_memcpy(hapd
->own_addr
, prev_addr
, ETH_ALEN
);
1483 if (hostapd_setup_bss(hapd
, j
== 0)) {
1485 hapd
= iface
->bss
[j
];
1486 hostapd_bss_deinit_no_free(hapd
);
1487 hostapd_free_hapd_data(hapd
);
1491 if (hostapd_mac_comp_empty(hapd
->conf
->bssid
) == 0)
1492 prev_addr
= hapd
->own_addr
;
1494 hapd
= iface
->bss
[0];
1496 hostapd_tx_queue_params(iface
);
1498 ap_list_init(iface
);
1500 hostapd_set_acl(hapd
);
1502 if (hostapd_driver_commit(hapd
) < 0) {
1503 wpa_printf(MSG_ERROR
, "%s: Failed to commit driver "
1504 "configuration", __func__
);
1509 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1510 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1511 * mode), the interface is up only after driver_commit, so initialize
1512 * WPS after driver_commit.
1514 for (j
= 0; j
< iface
->num_bss
; j
++) {
1515 if (hostapd_init_wps_complete(iface
->bss
[j
]))
1519 if ((iface
->drv_flags
& WPA_DRIVER_FLAGS_DFS_OFFLOAD
) &&
1522 * If freq is DFS, and DFS is offloaded to the driver, then wait
1523 * for CAC to complete.
1525 wpa_printf(MSG_DEBUG
, "%s: Wait for CAC to complete", __func__
);
1526 return res_dfs_offload
;
1531 #endif /* NEED_AP_MLME */
1532 hostapd_set_state(iface
, HAPD_IFACE_ENABLED
);
1533 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, AP_EVENT_ENABLED
);
1534 if (hapd
->setup_complete_cb
)
1535 hapd
->setup_complete_cb(hapd
->setup_complete_cb_ctx
);
1537 wpa_printf(MSG_DEBUG
, "%s: Setup of interface done.",
1538 iface
->bss
[0]->conf
->iface
);
1539 if (iface
->interfaces
&& iface
->interfaces
->terminate_on_error
> 0)
1540 iface
->interfaces
->terminate_on_error
--;
1545 wpa_printf(MSG_ERROR
, "Interface initialization failed");
1546 hostapd_set_state(iface
, HAPD_IFACE_DISABLED
);
1547 wpa_msg(hapd
->msg_ctx
, MSG_INFO
, AP_EVENT_DISABLED
);
1548 if (iface
->interfaces
&& iface
->interfaces
->terminate_on_error
)
1555 * hostapd_setup_interface - Setup of an interface
1556 * @iface: Pointer to interface data.
1557 * Returns: 0 on success, -1 on failure
1559 * Initializes the driver interface, validates the configuration,
1560 * and sets driver parameters based on the configuration.
1561 * Flushes old stations, sets the channel, encryption,
1562 * beacons, and WDS links based on the configuration.
1564 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1565 * or DFS operations, this function returns 0 before such operations have been
1566 * completed. The pending operations are registered into eloop and will be
1567 * completed from eloop callbacks. Those callbacks end up calling
1568 * hostapd_setup_interface_complete() once setup has been completed.
1570 int hostapd_setup_interface(struct hostapd_iface
*iface
)
1574 ret
= setup_interface(iface
);
1576 wpa_printf(MSG_ERROR
, "%s: Unable to setup interface.",
1577 iface
->bss
[0]->conf
->iface
);
1586 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1587 * @hapd_iface: Pointer to interface data
1588 * @conf: Pointer to per-interface configuration
1589 * @bss: Pointer to per-BSS configuration for this BSS
1590 * Returns: Pointer to allocated BSS data
1592 * This function is used to allocate per-BSS data structure. This data will be
1593 * freed after hostapd_cleanup() is called for it during interface
1596 struct hostapd_data
*
1597 hostapd_alloc_bss_data(struct hostapd_iface
*hapd_iface
,
1598 struct hostapd_config
*conf
,
1599 struct hostapd_bss_config
*bss
)
1601 struct hostapd_data
*hapd
;
1603 hapd
= os_zalloc(sizeof(*hapd
));
1607 hapd
->new_assoc_sta_cb
= hostapd_new_assoc_sta
;
1610 hapd
->iface
= hapd_iface
;
1611 hapd
->driver
= hapd
->iconf
->driver
;
1612 hapd
->ctrl_sock
= -1;
1618 static void hostapd_bss_deinit(struct hostapd_data
*hapd
)
1620 wpa_printf(MSG_DEBUG
, "%s: deinit bss %s", __func__
,
1622 hostapd_bss_deinit_no_free(hapd
);
1623 wpa_msg(hapd
->msg_ctx
, MSG_INFO
, AP_EVENT_DISABLED
);
1624 hostapd_cleanup(hapd
);
1628 void hostapd_interface_deinit(struct hostapd_iface
*iface
)
1632 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
1636 hostapd_set_state(iface
, HAPD_IFACE_DISABLED
);
1638 #ifdef CONFIG_IEEE80211N
1640 hostapd_stop_setup_timers(iface
);
1641 eloop_cancel_timeout(ap_ht2040_timeout
, iface
, NULL
);
1642 #endif /* NEED_AP_MLME */
1643 #endif /* CONFIG_IEEE80211N */
1644 eloop_cancel_timeout(channel_list_update_timeout
, iface
, NULL
);
1645 iface
->wait_channel_update
= 0;
1647 for (j
= iface
->num_bss
- 1; j
>= 0; j
--)
1648 hostapd_bss_deinit(iface
->bss
[j
]);
1652 void hostapd_interface_free(struct hostapd_iface
*iface
)
1655 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
1656 for (j
= 0; j
< iface
->num_bss
; j
++) {
1657 wpa_printf(MSG_DEBUG
, "%s: free hapd %p",
1658 __func__
, iface
->bss
[j
]);
1659 os_free(iface
->bss
[j
]);
1661 hostapd_cleanup_iface(iface
);
1666 * hostapd_init - Allocate and initialize per-interface data
1667 * @config_file: Path to the configuration file
1668 * Returns: Pointer to the allocated interface data or %NULL on failure
1670 * This function is used to allocate main data structures for per-interface
1671 * data. The allocated data buffer will be freed by calling
1672 * hostapd_cleanup_iface().
1674 struct hostapd_iface
* hostapd_init(struct hapd_interfaces
*interfaces
,
1675 const char *config_file
)
1677 struct hostapd_iface
*hapd_iface
= NULL
;
1678 struct hostapd_config
*conf
= NULL
;
1679 struct hostapd_data
*hapd
;
1682 hapd_iface
= os_zalloc(sizeof(*hapd_iface
));
1683 if (hapd_iface
== NULL
)
1686 hapd_iface
->config_fname
= os_strdup(config_file
);
1687 if (hapd_iface
->config_fname
== NULL
)
1690 conf
= interfaces
->config_read_cb(hapd_iface
->config_fname
);
1693 hapd_iface
->conf
= conf
;
1695 hapd_iface
->num_bss
= conf
->num_bss
;
1696 hapd_iface
->bss
= os_calloc(conf
->num_bss
,
1697 sizeof(struct hostapd_data
*));
1698 if (hapd_iface
->bss
== NULL
)
1701 for (i
= 0; i
< conf
->num_bss
; i
++) {
1702 hapd
= hapd_iface
->bss
[i
] =
1703 hostapd_alloc_bss_data(hapd_iface
, conf
,
1707 hapd
->msg_ctx
= hapd
;
1713 wpa_printf(MSG_ERROR
, "Failed to set up interface with %s",
1716 hostapd_config_free(conf
);
1718 os_free(hapd_iface
->config_fname
);
1719 os_free(hapd_iface
->bss
);
1720 wpa_printf(MSG_DEBUG
, "%s: free iface %p",
1721 __func__
, hapd_iface
);
1722 os_free(hapd_iface
);
1728 static int ifname_in_use(struct hapd_interfaces
*interfaces
, const char *ifname
)
1732 for (i
= 0; i
< interfaces
->count
; i
++) {
1733 struct hostapd_iface
*iface
= interfaces
->iface
[i
];
1734 for (j
= 0; j
< iface
->num_bss
; j
++) {
1735 struct hostapd_data
*hapd
= iface
->bss
[j
];
1736 if (os_strcmp(ifname
, hapd
->conf
->iface
) == 0)
1746 * hostapd_interface_init_bss - Read configuration file and init BSS data
1748 * This function is used to parse configuration file for a BSS. This BSS is
1749 * added to an existing interface sharing the same radio (if any) or a new
1750 * interface is created if this is the first interface on a radio. This
1751 * allocate memory for the BSS. No actual driver operations are started.
1753 * This is similar to hostapd_interface_init(), but for a case where the
1754 * configuration is used to add a single BSS instead of all BSSes for a radio.
1756 struct hostapd_iface
*
1757 hostapd_interface_init_bss(struct hapd_interfaces
*interfaces
, const char *phy
,
1758 const char *config_fname
, int debug
)
1760 struct hostapd_iface
*new_iface
= NULL
, *iface
= NULL
;
1761 struct hostapd_data
*hapd
;
1768 for (i
= 0; i
< interfaces
->count
; i
++) {
1769 if (os_strcmp(interfaces
->iface
[i
]->phy
, phy
) == 0) {
1770 iface
= interfaces
->iface
[i
];
1775 wpa_printf(MSG_INFO
, "Configuration file: %s (phy %s)%s",
1776 config_fname
, phy
, iface
? "" : " --> new PHY");
1778 struct hostapd_config
*conf
;
1779 struct hostapd_bss_config
**tmp_conf
;
1780 struct hostapd_data
**tmp_bss
;
1781 struct hostapd_bss_config
*bss
;
1784 /* Add new BSS to existing iface */
1785 conf
= interfaces
->config_read_cb(config_fname
);
1788 if (conf
->num_bss
> 1) {
1789 wpa_printf(MSG_ERROR
, "Multiple BSSes specified in BSS-config");
1790 hostapd_config_free(conf
);
1794 ifname
= conf
->bss
[0]->iface
;
1795 if (ifname
[0] != '\0' && ifname_in_use(interfaces
, ifname
)) {
1796 wpa_printf(MSG_ERROR
,
1797 "Interface name %s already in use", ifname
);
1798 hostapd_config_free(conf
);
1802 tmp_conf
= os_realloc_array(
1803 iface
->conf
->bss
, iface
->conf
->num_bss
+ 1,
1804 sizeof(struct hostapd_bss_config
*));
1805 tmp_bss
= os_realloc_array(iface
->bss
, iface
->num_bss
+ 1,
1806 sizeof(struct hostapd_data
*));
1808 iface
->bss
= tmp_bss
;
1810 iface
->conf
->bss
= tmp_conf
;
1811 iface
->conf
->last_bss
= tmp_conf
[0];
1813 if (tmp_bss
== NULL
|| tmp_conf
== NULL
) {
1814 hostapd_config_free(conf
);
1817 bss
= iface
->conf
->bss
[iface
->conf
->num_bss
] = conf
->bss
[0];
1818 iface
->conf
->num_bss
++;
1820 hapd
= hostapd_alloc_bss_data(iface
, iface
->conf
, bss
);
1822 iface
->conf
->num_bss
--;
1823 hostapd_config_free(conf
);
1826 iface
->conf
->last_bss
= bss
;
1827 iface
->bss
[iface
->num_bss
] = hapd
;
1828 hapd
->msg_ctx
= hapd
;
1830 bss_idx
= iface
->num_bss
++;
1832 conf
->bss
[0] = NULL
;
1833 hostapd_config_free(conf
);
1835 /* Add a new iface with the first BSS */
1836 new_iface
= iface
= hostapd_init(interfaces
, config_fname
);
1839 os_strlcpy(iface
->phy
, phy
, sizeof(iface
->phy
));
1840 iface
->interfaces
= interfaces
;
1844 for (k
= 0; k
< debug
; k
++) {
1845 if (iface
->bss
[bss_idx
]->conf
->logger_stdout_level
> 0)
1846 iface
->bss
[bss_idx
]->conf
->logger_stdout_level
--;
1849 if (iface
->conf
->bss
[bss_idx
]->iface
[0] == '\0' &&
1850 !hostapd_drv_none(iface
->bss
[bss_idx
])) {
1851 wpa_printf(MSG_ERROR
, "Interface name not specified in %s",
1854 hostapd_interface_deinit_free(new_iface
);
1862 void hostapd_interface_deinit_free(struct hostapd_iface
*iface
)
1864 const struct wpa_driver_ops
*driver
;
1867 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
1870 wpa_printf(MSG_DEBUG
, "%s: num_bss=%u conf->num_bss=%u",
1871 __func__
, (unsigned int) iface
->num_bss
,
1872 (unsigned int) iface
->conf
->num_bss
);
1873 driver
= iface
->bss
[0]->driver
;
1874 drv_priv
= iface
->bss
[0]->drv_priv
;
1875 hostapd_interface_deinit(iface
);
1876 wpa_printf(MSG_DEBUG
, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1877 __func__
, driver
, drv_priv
);
1878 if (driver
&& driver
->hapd_deinit
&& drv_priv
) {
1879 driver
->hapd_deinit(drv_priv
);
1880 iface
->bss
[0]->drv_priv
= NULL
;
1882 hostapd_interface_free(iface
);
1886 static void hostapd_deinit_driver(const struct wpa_driver_ops
*driver
,
1888 struct hostapd_iface
*hapd_iface
)
1892 wpa_printf(MSG_DEBUG
, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1893 __func__
, driver
, drv_priv
);
1894 if (driver
&& driver
->hapd_deinit
&& drv_priv
) {
1895 driver
->hapd_deinit(drv_priv
);
1896 for (j
= 0; j
< hapd_iface
->num_bss
; j
++) {
1897 wpa_printf(MSG_DEBUG
, "%s:bss[%d]->drv_priv=%p",
1899 hapd_iface
->bss
[j
]->drv_priv
);
1900 if (hapd_iface
->bss
[j
]->drv_priv
== drv_priv
)
1901 hapd_iface
->bss
[j
]->drv_priv
= NULL
;
1907 int hostapd_enable_iface(struct hostapd_iface
*hapd_iface
)
1911 if (hapd_iface
->bss
[0]->drv_priv
!= NULL
) {
1912 wpa_printf(MSG_ERROR
, "Interface %s already enabled",
1913 hapd_iface
->conf
->bss
[0]->iface
);
1917 wpa_printf(MSG_DEBUG
, "Enable interface %s",
1918 hapd_iface
->conf
->bss
[0]->iface
);
1920 for (j
= 0; j
< hapd_iface
->num_bss
; j
++)
1921 hostapd_set_security_params(hapd_iface
->conf
->bss
[j
], 1);
1922 if (hostapd_config_check(hapd_iface
->conf
, 1) < 0) {
1923 wpa_printf(MSG_INFO
, "Invalid configuration - cannot enable");
1927 if (hapd_iface
->interfaces
== NULL
||
1928 hapd_iface
->interfaces
->driver_init
== NULL
||
1929 hapd_iface
->interfaces
->driver_init(hapd_iface
))
1932 if (hostapd_setup_interface(hapd_iface
)) {
1933 hostapd_deinit_driver(hapd_iface
->bss
[0]->driver
,
1934 hapd_iface
->bss
[0]->drv_priv
,
1943 int hostapd_reload_iface(struct hostapd_iface
*hapd_iface
)
1947 wpa_printf(MSG_DEBUG
, "Reload interface %s",
1948 hapd_iface
->conf
->bss
[0]->iface
);
1949 for (j
= 0; j
< hapd_iface
->num_bss
; j
++)
1950 hostapd_set_security_params(hapd_iface
->conf
->bss
[j
], 1);
1951 if (hostapd_config_check(hapd_iface
->conf
, 1) < 0) {
1952 wpa_printf(MSG_ERROR
, "Updated configuration is invalid");
1955 hostapd_clear_old(hapd_iface
);
1956 for (j
= 0; j
< hapd_iface
->num_bss
; j
++)
1957 hostapd_reload_bss(hapd_iface
->bss
[j
]);
1963 int hostapd_disable_iface(struct hostapd_iface
*hapd_iface
)
1966 const struct wpa_driver_ops
*driver
;
1969 if (hapd_iface
== NULL
)
1972 if (hapd_iface
->bss
[0]->drv_priv
== NULL
) {
1973 wpa_printf(MSG_INFO
, "Interface %s already disabled",
1974 hapd_iface
->conf
->bss
[0]->iface
);
1978 wpa_msg(hapd_iface
->bss
[0]->msg_ctx
, MSG_INFO
, AP_EVENT_DISABLED
);
1979 driver
= hapd_iface
->bss
[0]->driver
;
1980 drv_priv
= hapd_iface
->bss
[0]->drv_priv
;
1982 hapd_iface
->driver_ap_teardown
=
1983 !!(hapd_iface
->drv_flags
&
1984 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT
);
1986 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1987 for (j
= 0; j
< hapd_iface
->num_bss
; j
++) {
1988 struct hostapd_data
*hapd
= hapd_iface
->bss
[j
];
1989 hostapd_bss_deinit_no_free(hapd
);
1990 hostapd_free_hapd_data(hapd
);
1993 hostapd_deinit_driver(driver
, drv_priv
, hapd_iface
);
1995 /* From hostapd_cleanup_iface: These were initialized in
1996 * hostapd_setup_interface and hostapd_setup_interface_complete
1998 hostapd_cleanup_iface_partial(hapd_iface
);
2000 wpa_printf(MSG_DEBUG
, "Interface %s disabled",
2001 hapd_iface
->bss
[0]->conf
->iface
);
2002 hostapd_set_state(hapd_iface
, HAPD_IFACE_DISABLED
);
2007 static struct hostapd_iface
*
2008 hostapd_iface_alloc(struct hapd_interfaces
*interfaces
)
2010 struct hostapd_iface
**iface
, *hapd_iface
;
2012 iface
= os_realloc_array(interfaces
->iface
, interfaces
->count
+ 1,
2013 sizeof(struct hostapd_iface
*));
2016 interfaces
->iface
= iface
;
2017 hapd_iface
= interfaces
->iface
[interfaces
->count
] =
2018 os_zalloc(sizeof(*hapd_iface
));
2019 if (hapd_iface
== NULL
) {
2020 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory for "
2021 "the interface", __func__
);
2024 interfaces
->count
++;
2025 hapd_iface
->interfaces
= interfaces
;
2031 static struct hostapd_config
*
2032 hostapd_config_alloc(struct hapd_interfaces
*interfaces
, const char *ifname
,
2033 const char *ctrl_iface
)
2035 struct hostapd_bss_config
*bss
;
2036 struct hostapd_config
*conf
;
2038 /* Allocates memory for bss and conf */
2039 conf
= hostapd_config_defaults();
2041 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory for "
2042 "configuration", __func__
);
2046 conf
->driver
= wpa_drivers
[0];
2047 if (conf
->driver
== NULL
) {
2048 wpa_printf(MSG_ERROR
, "No driver wrappers registered!");
2049 hostapd_config_free(conf
);
2053 bss
= conf
->last_bss
= conf
->bss
[0];
2055 os_strlcpy(bss
->iface
, ifname
, sizeof(bss
->iface
));
2056 bss
->ctrl_interface
= os_strdup(ctrl_iface
);
2057 if (bss
->ctrl_interface
== NULL
) {
2058 hostapd_config_free(conf
);
2062 /* Reading configuration file skipped, will be done in SET!
2063 * From reading the configuration till the end has to be done in
2070 static int hostapd_data_alloc(struct hostapd_iface
*hapd_iface
,
2071 struct hostapd_config
*conf
)
2074 struct hostapd_data
*hapd
;
2076 hapd_iface
->bss
= os_calloc(conf
->num_bss
,
2077 sizeof(struct hostapd_data
*));
2078 if (hapd_iface
->bss
== NULL
)
2081 for (i
= 0; i
< conf
->num_bss
; i
++) {
2082 hapd
= hapd_iface
->bss
[i
] =
2083 hostapd_alloc_bss_data(hapd_iface
, conf
, conf
->bss
[i
]);
2087 os_free(hapd_iface
->bss
[i
]);
2088 hapd_iface
->bss
[i
] = NULL
;
2090 os_free(hapd_iface
->bss
);
2091 hapd_iface
->bss
= NULL
;
2094 hapd
->msg_ctx
= hapd
;
2097 hapd_iface
->conf
= conf
;
2098 hapd_iface
->num_bss
= conf
->num_bss
;
2104 int hostapd_add_iface(struct hapd_interfaces
*interfaces
, char *buf
)
2106 struct hostapd_config
*conf
= NULL
;
2107 struct hostapd_iface
*hapd_iface
= NULL
, *new_iface
= NULL
;
2108 struct hostapd_data
*hapd
;
2111 const char *conf_file
= NULL
, *phy_name
= NULL
;
2113 if (os_strncmp(buf
, "bss_config=", 11) == 0) {
2115 phy_name
= buf
+ 11;
2116 pos
= os_strchr(phy_name
, ':');
2121 if (!os_strlen(conf_file
))
2124 hapd_iface
= hostapd_interface_init_bss(interfaces
, phy_name
,
2128 for (j
= 0; j
< interfaces
->count
; j
++) {
2129 if (interfaces
->iface
[j
] == hapd_iface
)
2132 if (j
== interfaces
->count
) {
2133 struct hostapd_iface
**tmp
;
2134 tmp
= os_realloc_array(interfaces
->iface
,
2135 interfaces
->count
+ 1,
2136 sizeof(struct hostapd_iface
*));
2138 hostapd_interface_deinit_free(hapd_iface
);
2141 interfaces
->iface
= tmp
;
2142 interfaces
->iface
[interfaces
->count
++] = hapd_iface
;
2143 new_iface
= hapd_iface
;
2147 if (interfaces
->driver_init(hapd_iface
))
2150 if (hostapd_setup_interface(hapd_iface
)) {
2151 hostapd_deinit_driver(
2152 hapd_iface
->bss
[0]->driver
,
2153 hapd_iface
->bss
[0]->drv_priv
,
2158 /* Assign new BSS with bss[0]'s driver info */
2159 hapd
= hapd_iface
->bss
[hapd_iface
->num_bss
- 1];
2160 hapd
->driver
= hapd_iface
->bss
[0]->driver
;
2161 hapd
->drv_priv
= hapd_iface
->bss
[0]->drv_priv
;
2162 os_memcpy(hapd
->own_addr
, hapd_iface
->bss
[0]->own_addr
,
2165 if (start_ctrl_iface_bss(hapd
) < 0 ||
2166 (hapd_iface
->state
== HAPD_IFACE_ENABLED
&&
2167 hostapd_setup_bss(hapd
, -1))) {
2168 hostapd_cleanup(hapd
);
2169 hapd_iface
->bss
[hapd_iface
->num_bss
- 1] = NULL
;
2170 hapd_iface
->conf
->num_bss
--;
2171 hapd_iface
->num_bss
--;
2172 wpa_printf(MSG_DEBUG
, "%s: free hapd %p %s",
2173 __func__
, hapd
, hapd
->conf
->iface
);
2174 hostapd_config_free_bss(hapd
->conf
);
2183 ptr
= os_strchr(buf
, ' ');
2188 if (os_strncmp(ptr
, "config=", 7) == 0)
2189 conf_file
= ptr
+ 7;
2191 for (i
= 0; i
< interfaces
->count
; i
++) {
2192 if (!os_strcmp(interfaces
->iface
[i
]->conf
->bss
[0]->iface
,
2194 wpa_printf(MSG_INFO
, "Cannot add interface - it "
2200 hapd_iface
= hostapd_iface_alloc(interfaces
);
2201 if (hapd_iface
== NULL
) {
2202 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory "
2203 "for interface", __func__
);
2206 new_iface
= hapd_iface
;
2208 if (conf_file
&& interfaces
->config_read_cb
) {
2209 conf
= interfaces
->config_read_cb(conf_file
);
2210 if (conf
&& conf
->bss
)
2211 os_strlcpy(conf
->bss
[0]->iface
, buf
,
2212 sizeof(conf
->bss
[0]->iface
));
2214 conf
= hostapd_config_alloc(interfaces
, buf
, ptr
);
2215 if (conf
== NULL
|| conf
->bss
== NULL
) {
2216 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory "
2217 "for configuration", __func__
);
2221 if (hostapd_data_alloc(hapd_iface
, conf
) < 0) {
2222 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory "
2223 "for hostapd", __func__
);
2228 if (start_ctrl_iface(hapd_iface
) < 0)
2231 wpa_printf(MSG_INFO
, "Add interface '%s'",
2232 hapd_iface
->conf
->bss
[0]->iface
);
2238 hostapd_config_free(conf
);
2240 if (hapd_iface
->bss
) {
2241 for (i
= 0; i
< hapd_iface
->num_bss
; i
++) {
2242 hapd
= hapd_iface
->bss
[i
];
2245 if (hapd_iface
->interfaces
&&
2246 hapd_iface
->interfaces
->ctrl_iface_deinit
)
2247 hapd_iface
->interfaces
->
2248 ctrl_iface_deinit(hapd
);
2249 wpa_printf(MSG_DEBUG
, "%s: free hapd %p (%s)",
2250 __func__
, hapd_iface
->bss
[i
],
2252 hostapd_cleanup(hapd
);
2254 hapd_iface
->bss
[i
] = NULL
;
2256 os_free(hapd_iface
->bss
);
2257 hapd_iface
->bss
= NULL
;
2260 interfaces
->count
--;
2261 interfaces
->iface
[interfaces
->count
] = NULL
;
2263 hostapd_cleanup_iface(hapd_iface
);
2269 static int hostapd_remove_bss(struct hostapd_iface
*iface
, unsigned int idx
)
2273 wpa_printf(MSG_INFO
, "Remove BSS '%s'", iface
->conf
->bss
[idx
]->iface
);
2275 /* Remove hostapd_data only if it has already been initialized */
2276 if (idx
< iface
->num_bss
) {
2277 struct hostapd_data
*hapd
= iface
->bss
[idx
];
2279 hostapd_bss_deinit(hapd
);
2280 wpa_printf(MSG_DEBUG
, "%s: free hapd %p (%s)",
2281 __func__
, hapd
, hapd
->conf
->iface
);
2282 hostapd_config_free_bss(hapd
->conf
);
2288 for (i
= idx
; i
< iface
->num_bss
; i
++)
2289 iface
->bss
[i
] = iface
->bss
[i
+ 1];
2291 hostapd_config_free_bss(iface
->conf
->bss
[idx
]);
2292 iface
->conf
->bss
[idx
] = NULL
;
2295 iface
->conf
->num_bss
--;
2296 for (i
= idx
; i
< iface
->conf
->num_bss
; i
++)
2297 iface
->conf
->bss
[i
] = iface
->conf
->bss
[i
+ 1];
2303 int hostapd_remove_iface(struct hapd_interfaces
*interfaces
, char *buf
)
2305 struct hostapd_iface
*hapd_iface
;
2308 for (i
= 0; i
< interfaces
->count
; i
++) {
2309 hapd_iface
= interfaces
->iface
[i
];
2310 if (hapd_iface
== NULL
)
2312 if (!os_strcmp(hapd_iface
->conf
->bss
[0]->iface
, buf
)) {
2313 wpa_printf(MSG_INFO
, "Remove interface '%s'", buf
);
2314 hapd_iface
->driver_ap_teardown
=
2315 !!(hapd_iface
->drv_flags
&
2316 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT
);
2318 hostapd_interface_deinit_free(hapd_iface
);
2320 while (k
< (interfaces
->count
- 1)) {
2321 interfaces
->iface
[k
] =
2322 interfaces
->iface
[k
+ 1];
2325 interfaces
->count
--;
2329 for (j
= 0; j
< hapd_iface
->conf
->num_bss
; j
++) {
2330 if (!os_strcmp(hapd_iface
->conf
->bss
[j
]->iface
, buf
)) {
2331 hapd_iface
->driver_ap_teardown
=
2332 !(hapd_iface
->drv_flags
&
2333 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT
);
2334 return hostapd_remove_bss(hapd_iface
, j
);
2343 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2344 * @hapd: Pointer to BSS data
2345 * @sta: Pointer to the associated STA data
2346 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2348 * This function will be called whenever a station associates with the AP. It
2349 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2350 * from drv_callbacks.c based on driver events for drivers that take care of
2351 * management frames (IEEE 802.11 authentication and association) internally.
2353 void hostapd_new_assoc_sta(struct hostapd_data
*hapd
, struct sta_info
*sta
,
2356 if (hapd
->tkip_countermeasures
) {
2357 hostapd_drv_sta_deauth(hapd
, sta
->addr
,
2358 WLAN_REASON_MICHAEL_MIC_FAILURE
);
2362 hostapd_prune_associations(hapd
, sta
->addr
);
2364 /* IEEE 802.11F (IAPP) */
2365 if (hapd
->conf
->ieee802_11f
)
2366 iapp_new_station(hapd
->iapp
, sta
);
2369 if (sta
->p2p_ie
== NULL
&& !sta
->no_p2p_set
) {
2370 sta
->no_p2p_set
= 1;
2371 hapd
->num_sta_no_p2p
++;
2372 if (hapd
->num_sta_no_p2p
== 1)
2373 hostapd_p2p_non_p2p_sta_connected(hapd
);
2375 #endif /* CONFIG_P2P */
2377 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2378 * IEEE 802.1X/WPA code will start accounting after the station has
2379 * been authorized. */
2380 if (!hapd
->conf
->ieee802_1x
&& !hapd
->conf
->wpa
&& !hapd
->conf
->osen
) {
2381 ap_sta_set_authorized(hapd
, sta
, 1);
2382 os_get_reltime(&sta
->connected_time
);
2383 accounting_sta_start(hapd
, sta
);
2386 /* Start IEEE 802.1X authentication process for new stations */
2387 ieee802_1x_new_station(hapd
, sta
);
2389 if (sta
->auth_alg
!= WLAN_AUTH_FT
&&
2390 !(sta
->flags
& (WLAN_STA_WPS
| WLAN_STA_MAYBE_WPS
)))
2391 wpa_auth_sm_event(sta
->wpa_sm
, WPA_REAUTH
);
2393 wpa_auth_sta_associated(hapd
->wpa_auth
, sta
->wpa_sm
);
2395 if (!(hapd
->iface
->drv_flags
& WPA_DRIVER_FLAGS_INACTIVITY_TIMER
)) {
2396 wpa_printf(MSG_DEBUG
, "%s: reschedule ap_handle_timer timeout "
2397 "for " MACSTR
" (%d seconds - ap_max_inactivity)",
2398 __func__
, MAC2STR(sta
->addr
),
2399 hapd
->conf
->ap_max_inactivity
);
2400 eloop_cancel_timeout(ap_handle_timer
, hapd
, sta
);
2401 eloop_register_timeout(hapd
->conf
->ap_max_inactivity
, 0,
2402 ap_handle_timer
, hapd
, sta
);
2407 const char * hostapd_state_text(enum hostapd_iface_state s
)
2410 case HAPD_IFACE_UNINITIALIZED
:
2411 return "UNINITIALIZED";
2412 case HAPD_IFACE_DISABLED
:
2414 case HAPD_IFACE_COUNTRY_UPDATE
:
2415 return "COUNTRY_UPDATE";
2416 case HAPD_IFACE_ACS
:
2418 case HAPD_IFACE_HT_SCAN
:
2420 case HAPD_IFACE_DFS
:
2422 case HAPD_IFACE_ENABLED
:
2430 void hostapd_set_state(struct hostapd_iface
*iface
, enum hostapd_iface_state s
)
2432 wpa_printf(MSG_INFO
, "%s: interface state %s->%s",
2433 iface
->conf
->bss
[0]->iface
, hostapd_state_text(iface
->state
),
2434 hostapd_state_text(s
));
2441 static void free_beacon_data(struct beacon_data
*beacon
)
2443 os_free(beacon
->head
);
2444 beacon
->head
= NULL
;
2445 os_free(beacon
->tail
);
2446 beacon
->tail
= NULL
;
2447 os_free(beacon
->probe_resp
);
2448 beacon
->probe_resp
= NULL
;
2449 os_free(beacon
->beacon_ies
);
2450 beacon
->beacon_ies
= NULL
;
2451 os_free(beacon
->proberesp_ies
);
2452 beacon
->proberesp_ies
= NULL
;
2453 os_free(beacon
->assocresp_ies
);
2454 beacon
->assocresp_ies
= NULL
;
2458 static int hostapd_build_beacon_data(struct hostapd_data
*hapd
,
2459 struct beacon_data
*beacon
)
2461 struct wpabuf
*beacon_extra
, *proberesp_extra
, *assocresp_extra
;
2462 struct wpa_driver_ap_params params
;
2465 os_memset(beacon
, 0, sizeof(*beacon
));
2466 ret
= ieee802_11_build_ap_params(hapd
, ¶ms
);
2470 ret
= hostapd_build_ap_extra_ies(hapd
, &beacon_extra
,
2474 goto free_ap_params
;
2477 beacon
->head
= os_malloc(params
.head_len
);
2479 goto free_ap_extra_ies
;
2481 os_memcpy(beacon
->head
, params
.head
, params
.head_len
);
2482 beacon
->head_len
= params
.head_len
;
2484 beacon
->tail
= os_malloc(params
.tail_len
);
2488 os_memcpy(beacon
->tail
, params
.tail
, params
.tail_len
);
2489 beacon
->tail_len
= params
.tail_len
;
2491 if (params
.proberesp
!= NULL
) {
2492 beacon
->probe_resp
= os_malloc(params
.proberesp_len
);
2493 if (!beacon
->probe_resp
)
2496 os_memcpy(beacon
->probe_resp
, params
.proberesp
,
2497 params
.proberesp_len
);
2498 beacon
->probe_resp_len
= params
.proberesp_len
;
2501 /* copy the extra ies */
2503 beacon
->beacon_ies
= os_malloc(wpabuf_len(beacon_extra
));
2504 if (!beacon
->beacon_ies
)
2507 os_memcpy(beacon
->beacon_ies
,
2508 beacon_extra
->buf
, wpabuf_len(beacon_extra
));
2509 beacon
->beacon_ies_len
= wpabuf_len(beacon_extra
);
2512 if (proberesp_extra
) {
2513 beacon
->proberesp_ies
=
2514 os_malloc(wpabuf_len(proberesp_extra
));
2515 if (!beacon
->proberesp_ies
)
2518 os_memcpy(beacon
->proberesp_ies
, proberesp_extra
->buf
,
2519 wpabuf_len(proberesp_extra
));
2520 beacon
->proberesp_ies_len
= wpabuf_len(proberesp_extra
);
2523 if (assocresp_extra
) {
2524 beacon
->assocresp_ies
=
2525 os_malloc(wpabuf_len(assocresp_extra
));
2526 if (!beacon
->assocresp_ies
)
2529 os_memcpy(beacon
->assocresp_ies
, assocresp_extra
->buf
,
2530 wpabuf_len(assocresp_extra
));
2531 beacon
->assocresp_ies_len
= wpabuf_len(assocresp_extra
);
2536 /* if the function fails, the caller should not free beacon data */
2538 free_beacon_data(beacon
);
2541 hostapd_free_ap_extra_ies(hapd
, beacon_extra
, proberesp_extra
,
2544 ieee802_11_free_ap_params(¶ms
);
2550 * TODO: This flow currently supports only changing frequency within the
2551 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2552 * width) are not supported.
2554 static int hostapd_change_config_freq(struct hostapd_data
*hapd
,
2555 struct hostapd_config
*conf
,
2556 struct hostapd_freq_params
*params
,
2557 struct hostapd_freq_params
*old_params
)
2561 if (!params
->channel
) {
2562 /* check if the new channel is supported by hw */
2563 params
->channel
= hostapd_hw_get_channel(hapd
, params
->freq
);
2566 channel
= params
->channel
;
2570 /* if a pointer to old_params is provided we save previous state */
2572 old_params
->channel
= conf
->channel
;
2573 old_params
->ht_enabled
= conf
->ieee80211n
;
2574 old_params
->sec_channel_offset
= conf
->secondary_channel
;
2577 conf
->channel
= channel
;
2578 conf
->ieee80211n
= params
->ht_enabled
;
2579 conf
->secondary_channel
= params
->sec_channel_offset
;
2581 /* TODO: maybe call here hostapd_config_check here? */
2587 static int hostapd_fill_csa_settings(struct hostapd_data
*hapd
,
2588 struct csa_settings
*settings
)
2590 struct hostapd_iface
*iface
= hapd
->iface
;
2591 struct hostapd_freq_params old_freq
;
2594 os_memset(&old_freq
, 0, sizeof(old_freq
));
2595 if (!iface
|| !iface
->freq
|| hapd
->csa_in_progress
)
2598 ret
= hostapd_change_config_freq(iface
->bss
[0], iface
->conf
,
2599 &settings
->freq_params
,
2604 ret
= hostapd_build_beacon_data(hapd
, &settings
->beacon_after
);
2606 /* change back the configuration */
2607 hostapd_change_config_freq(iface
->bss
[0], iface
->conf
,
2613 /* set channel switch parameters for csa ie */
2614 hapd
->cs_freq_params
= settings
->freq_params
;
2615 hapd
->cs_count
= settings
->cs_count
;
2616 hapd
->cs_block_tx
= settings
->block_tx
;
2618 ret
= hostapd_build_beacon_data(hapd
, &settings
->beacon_csa
);
2620 free_beacon_data(&settings
->beacon_after
);
2624 settings
->counter_offset_beacon
= hapd
->cs_c_off_beacon
;
2625 settings
->counter_offset_presp
= hapd
->cs_c_off_proberesp
;
2631 void hostapd_cleanup_cs_params(struct hostapd_data
*hapd
)
2633 os_memset(&hapd
->cs_freq_params
, 0, sizeof(hapd
->cs_freq_params
));
2635 hapd
->cs_block_tx
= 0;
2636 hapd
->cs_c_off_beacon
= 0;
2637 hapd
->cs_c_off_proberesp
= 0;
2638 hapd
->csa_in_progress
= 0;
2642 int hostapd_switch_channel(struct hostapd_data
*hapd
,
2643 struct csa_settings
*settings
)
2647 if (!(hapd
->iface
->drv_flags
& WPA_DRIVER_FLAGS_AP_CSA
)) {
2648 wpa_printf(MSG_INFO
, "CSA is not supported");
2652 ret
= hostapd_fill_csa_settings(hapd
, settings
);
2656 ret
= hostapd_drv_switch_channel(hapd
, settings
);
2657 free_beacon_data(&settings
->beacon_csa
);
2658 free_beacon_data(&settings
->beacon_after
);
2661 /* if we failed, clean cs parameters */
2662 hostapd_cleanup_cs_params(hapd
);
2666 hapd
->csa_in_progress
= 1;
2672 hostapd_switch_channel_fallback(struct hostapd_iface
*iface
,
2673 const struct hostapd_freq_params
*freq_params
)
2675 int vht_seg0_idx
= 0, vht_seg1_idx
= 0, vht_bw
= VHT_CHANWIDTH_USE_HT
;
2678 wpa_printf(MSG_DEBUG
, "Restarting all CSA-related BSSes");
2680 if (freq_params
->center_freq1
)
2681 vht_seg0_idx
= 36 + (freq_params
->center_freq1
- 5180) / 5;
2682 if (freq_params
->center_freq2
)
2683 vht_seg1_idx
= 36 + (freq_params
->center_freq2
- 5180) / 5;
2685 switch (freq_params
->bandwidth
) {
2689 vht_bw
= VHT_CHANWIDTH_USE_HT
;
2692 if (freq_params
->center_freq2
)
2693 vht_bw
= VHT_CHANWIDTH_80P80MHZ
;
2695 vht_bw
= VHT_CHANWIDTH_80MHZ
;
2698 vht_bw
= VHT_CHANWIDTH_160MHZ
;
2701 wpa_printf(MSG_WARNING
, "Unknown CSA bandwidth: %d",
2702 freq_params
->bandwidth
);
2706 iface
->freq
= freq_params
->freq
;
2707 iface
->conf
->channel
= freq_params
->channel
;
2708 iface
->conf
->secondary_channel
= freq_params
->sec_channel_offset
;
2709 iface
->conf
->vht_oper_centr_freq_seg0_idx
= vht_seg0_idx
;
2710 iface
->conf
->vht_oper_centr_freq_seg1_idx
= vht_seg1_idx
;
2711 iface
->conf
->vht_oper_chwidth
= vht_bw
;
2712 iface
->conf
->ieee80211n
= freq_params
->ht_enabled
;
2713 iface
->conf
->ieee80211ac
= freq_params
->vht_enabled
;
2716 * cs_params must not be cleared earlier because the freq_params
2717 * argument may actually point to one of these.
2719 for (i
= 0; i
< iface
->num_bss
; i
++)
2720 hostapd_cleanup_cs_params(iface
->bss
[i
]);
2722 hostapd_disable_iface(iface
);
2723 hostapd_enable_iface(iface
);
2726 #endif /* NEED_AP_MLME */