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"
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"
37 #include "ieee802_11.h"
41 static int hostapd_flush_old_stations(struct hostapd_data
*hapd
, u16 reason
);
42 static int hostapd_setup_encryption(char *iface
, struct hostapd_data
*hapd
);
43 static int hostapd_broadcast_wep_clear(struct hostapd_data
*hapd
);
44 static int setup_interface2(struct hostapd_iface
*iface
);
45 static void channel_list_update_timeout(void *eloop_ctx
, void *timeout_ctx
);
48 int hostapd_for_each_interface(struct hapd_interfaces
*interfaces
,
49 int (*cb
)(struct hostapd_iface
*iface
,
50 void *ctx
), void *ctx
)
55 for (i
= 0; i
< interfaces
->count
; i
++) {
56 ret
= cb(interfaces
->iface
[i
], ctx
);
65 static void hostapd_reload_bss(struct hostapd_data
*hapd
)
67 struct hostapd_ssid
*ssid
;
69 #ifndef CONFIG_NO_RADIUS
70 radius_client_reconfig(hapd
->radius
, hapd
->conf
->radius
);
71 #endif /* CONFIG_NO_RADIUS */
73 ssid
= &hapd
->conf
->ssid
;
74 if (!ssid
->wpa_psk_set
&& ssid
->wpa_psk
&& !ssid
->wpa_psk
->next
&&
75 ssid
->wpa_passphrase_set
&& ssid
->wpa_passphrase
) {
77 * Force PSK to be derived again since SSID or passphrase may
80 os_free(ssid
->wpa_psk
);
83 if (hostapd_setup_wpa_psk(hapd
->conf
)) {
84 wpa_printf(MSG_ERROR
, "Failed to re-configure WPA PSK "
85 "after reloading configuration");
88 if (hapd
->conf
->ieee802_1x
|| hapd
->conf
->wpa
)
89 hostapd_set_drv_ieee8021x(hapd
, hapd
->conf
->iface
, 1);
91 hostapd_set_drv_ieee8021x(hapd
, hapd
->conf
->iface
, 0);
93 if ((hapd
->conf
->wpa
|| hapd
->conf
->osen
) && hapd
->wpa_auth
== NULL
) {
94 hostapd_setup_wpa(hapd
);
96 wpa_init_keys(hapd
->wpa_auth
);
97 } else if (hapd
->conf
->wpa
) {
100 hostapd_reconfig_wpa(hapd
);
101 wpa_ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &wpa_ie_len
);
102 if (hostapd_set_generic_elem(hapd
, wpa_ie
, wpa_ie_len
))
103 wpa_printf(MSG_ERROR
, "Failed to configure WPA IE for "
104 "the kernel driver.");
105 } else if (hapd
->wpa_auth
) {
106 wpa_deinit(hapd
->wpa_auth
);
107 hapd
->wpa_auth
= NULL
;
108 hostapd_set_privacy(hapd
, 0);
109 hostapd_setup_encryption(hapd
->conf
->iface
, hapd
);
110 hostapd_set_generic_elem(hapd
, (u8
*) "", 0);
113 ieee802_11_set_beacon(hapd
);
114 hostapd_update_wps(hapd
);
116 if (hapd
->conf
->ssid
.ssid_set
&&
117 hostapd_set_ssid(hapd
, hapd
->conf
->ssid
.ssid
,
118 hapd
->conf
->ssid
.ssid_len
)) {
119 wpa_printf(MSG_ERROR
, "Could not set SSID for kernel driver");
120 /* try to continue */
122 wpa_printf(MSG_DEBUG
, "Reconfigured interface %s", hapd
->conf
->iface
);
126 static void hostapd_clear_old(struct hostapd_iface
*iface
)
131 * Deauthenticate all stations since the new configuration may not
132 * allow them to use the BSS anymore.
134 for (j
= 0; j
< iface
->num_bss
; j
++) {
135 hostapd_flush_old_stations(iface
->bss
[j
],
136 WLAN_REASON_PREV_AUTH_NOT_VALID
);
137 hostapd_broadcast_wep_clear(iface
->bss
[j
]);
139 #ifndef CONFIG_NO_RADIUS
140 /* TODO: update dynamic data based on changed configuration
141 * items (e.g., open/close sockets, etc.) */
142 radius_client_flush(iface
->bss
[j
]->radius
, 0);
143 #endif /* CONFIG_NO_RADIUS */
148 int hostapd_reload_config(struct hostapd_iface
*iface
)
150 struct hostapd_data
*hapd
= iface
->bss
[0];
151 struct hostapd_config
*newconf
, *oldconf
;
154 if (iface
->config_fname
== NULL
) {
155 /* Only in-memory config in use - assume it has been updated */
156 hostapd_clear_old(iface
);
157 for (j
= 0; j
< iface
->num_bss
; j
++)
158 hostapd_reload_bss(iface
->bss
[j
]);
162 if (iface
->interfaces
== NULL
||
163 iface
->interfaces
->config_read_cb
== NULL
)
165 newconf
= iface
->interfaces
->config_read_cb(iface
->config_fname
);
169 hostapd_clear_old(iface
);
171 oldconf
= hapd
->iconf
;
172 iface
->conf
= newconf
;
174 for (j
= 0; j
< iface
->num_bss
; j
++) {
175 hapd
= iface
->bss
[j
];
176 hapd
->iconf
= newconf
;
177 hapd
->iconf
->channel
= oldconf
->channel
;
178 hapd
->iconf
->secondary_channel
= oldconf
->secondary_channel
;
179 hapd
->iconf
->ieee80211n
= oldconf
->ieee80211n
;
180 hapd
->iconf
->ieee80211ac
= oldconf
->ieee80211ac
;
181 hapd
->iconf
->ht_capab
= oldconf
->ht_capab
;
182 hapd
->iconf
->vht_capab
= oldconf
->vht_capab
;
183 hapd
->iconf
->vht_oper_chwidth
= oldconf
->vht_oper_chwidth
;
184 hapd
->iconf
->vht_oper_centr_freq_seg0_idx
=
185 oldconf
->vht_oper_centr_freq_seg0_idx
;
186 hapd
->iconf
->vht_oper_centr_freq_seg1_idx
=
187 oldconf
->vht_oper_centr_freq_seg1_idx
;
188 hapd
->conf
= newconf
->bss
[j
];
189 hostapd_reload_bss(hapd
);
192 hostapd_config_free(oldconf
);
199 static void hostapd_broadcast_key_clear_iface(struct hostapd_data
*hapd
,
204 for (i
= 0; i
< NUM_WEP_KEYS
; i
++) {
205 if (hostapd_drv_set_key(ifname
, hapd
, WPA_ALG_NONE
, NULL
, i
,
206 0, NULL
, 0, NULL
, 0)) {
207 wpa_printf(MSG_DEBUG
, "Failed to clear default "
208 "encryption keys (ifname=%s keyidx=%d)",
212 #ifdef CONFIG_IEEE80211W
213 if (hapd
->conf
->ieee80211w
) {
214 for (i
= NUM_WEP_KEYS
; i
< NUM_WEP_KEYS
+ 2; i
++) {
215 if (hostapd_drv_set_key(ifname
, hapd
, WPA_ALG_NONE
,
218 wpa_printf(MSG_DEBUG
, "Failed to clear "
219 "default mgmt encryption keys "
220 "(ifname=%s keyidx=%d)", ifname
, i
);
224 #endif /* CONFIG_IEEE80211W */
228 static int hostapd_broadcast_wep_clear(struct hostapd_data
*hapd
)
230 hostapd_broadcast_key_clear_iface(hapd
, hapd
->conf
->iface
);
235 static int hostapd_broadcast_wep_set(struct hostapd_data
*hapd
)
238 struct hostapd_ssid
*ssid
= &hapd
->conf
->ssid
;
241 if (ssid
->wep
.default_len
&&
242 hostapd_drv_set_key(hapd
->conf
->iface
,
243 hapd
, WPA_ALG_WEP
, broadcast_ether_addr
, idx
,
244 1, NULL
, 0, ssid
->wep
.key
[idx
],
245 ssid
->wep
.len
[idx
])) {
246 wpa_printf(MSG_WARNING
, "Could not set WEP encryption.");
254 static void hostapd_free_hapd_data(struct hostapd_data
*hapd
)
256 if (!hapd
->started
) {
257 wpa_printf(MSG_ERROR
, "%s: Interface %s wasn't started",
258 __func__
, hapd
->conf
->iface
);
263 wpa_printf(MSG_DEBUG
, "%s(%s)", __func__
, hapd
->conf
->iface
);
264 iapp_deinit(hapd
->iapp
);
266 accounting_deinit(hapd
);
267 hostapd_deinit_wpa(hapd
);
269 hostapd_acl_deinit(hapd
);
270 #ifndef CONFIG_NO_RADIUS
271 radius_client_deinit(hapd
->radius
);
273 radius_das_deinit(hapd
->radius_das
);
274 hapd
->radius_das
= NULL
;
275 #endif /* CONFIG_NO_RADIUS */
277 hostapd_deinit_wps(hapd
);
279 authsrv_deinit(hapd
);
281 if (hapd
->interface_added
) {
282 hapd
->interface_added
= 0;
283 if (hostapd_if_remove(hapd
, WPA_IF_AP_BSS
, hapd
->conf
->iface
)) {
284 wpa_printf(MSG_WARNING
,
285 "Failed to remove BSS interface %s",
287 hapd
->interface_added
= 1;
290 * Since this was a dynamically added interface, the
291 * driver wrapper may have removed its internal instance
292 * and hapd->drv_priv is not valid anymore.
294 hapd
->drv_priv
= NULL
;
298 os_free(hapd
->probereq_cb
);
299 hapd
->probereq_cb
= NULL
;
302 wpabuf_free(hapd
->p2p_beacon_ie
);
303 hapd
->p2p_beacon_ie
= NULL
;
304 wpabuf_free(hapd
->p2p_probe_resp_ie
);
305 hapd
->p2p_probe_resp_ie
= NULL
;
306 #endif /* CONFIG_P2P */
308 wpabuf_free(hapd
->time_adv
);
310 #ifdef CONFIG_INTERWORKING
311 gas_serv_deinit(hapd
);
312 #endif /* CONFIG_INTERWORKING */
314 bss_load_update_deinit(hapd
);
317 bin_clear_free(hapd
->tmp_eap_user
.identity
,
318 hapd
->tmp_eap_user
.identity_len
);
319 bin_clear_free(hapd
->tmp_eap_user
.password
,
320 hapd
->tmp_eap_user
.password_len
);
321 #endif /* CONFIG_SQLITE */
326 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
327 * @hapd: Pointer to BSS data
329 * This function is used to free all per-BSS data structures and resources.
330 * Most of the modules that are initialized in hostapd_setup_bss() are
331 * deinitialized here.
333 static void hostapd_cleanup(struct hostapd_data
*hapd
)
335 wpa_printf(MSG_DEBUG
, "%s(hapd=%p (%s))", __func__
, hapd
,
337 if (hapd
->iface
->interfaces
&&
338 hapd
->iface
->interfaces
->ctrl_iface_deinit
)
339 hapd
->iface
->interfaces
->ctrl_iface_deinit(hapd
);
340 hostapd_free_hapd_data(hapd
);
344 static void hostapd_cleanup_iface_partial(struct hostapd_iface
*iface
)
346 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
347 hostapd_free_hw_features(iface
->hw_features
, iface
->num_hw_features
);
348 iface
->hw_features
= NULL
;
349 os_free(iface
->current_rates
);
350 iface
->current_rates
= NULL
;
351 os_free(iface
->basic_rates
);
352 iface
->basic_rates
= NULL
;
353 ap_list_deinit(iface
);
358 * hostapd_cleanup_iface - Complete per-interface cleanup
359 * @iface: Pointer to interface data
361 * This function is called after per-BSS data structures are deinitialized
362 * with hostapd_cleanup().
364 static void hostapd_cleanup_iface(struct hostapd_iface
*iface
)
366 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
367 eloop_cancel_timeout(channel_list_update_timeout
, iface
, NULL
);
369 hostapd_cleanup_iface_partial(iface
);
370 hostapd_config_free(iface
->conf
);
373 os_free(iface
->config_fname
);
375 wpa_printf(MSG_DEBUG
, "%s: free iface=%p", __func__
, iface
);
380 static void hostapd_clear_wep(struct hostapd_data
*hapd
)
382 if (hapd
->drv_priv
&& !hapd
->iface
->driver_ap_teardown
) {
383 hostapd_set_privacy(hapd
, 0);
384 hostapd_broadcast_wep_clear(hapd
);
389 static int hostapd_setup_encryption(char *iface
, struct hostapd_data
*hapd
)
393 hostapd_broadcast_wep_set(hapd
);
395 if (hapd
->conf
->ssid
.wep
.default_len
) {
396 hostapd_set_privacy(hapd
, 1);
401 * When IEEE 802.1X is not enabled, the driver may need to know how to
402 * set authentication algorithms for static WEP.
404 hostapd_drv_set_authmode(hapd
, hapd
->conf
->auth_algs
);
406 for (i
= 0; i
< 4; i
++) {
407 if (hapd
->conf
->ssid
.wep
.key
[i
] &&
408 hostapd_drv_set_key(iface
, hapd
, WPA_ALG_WEP
, NULL
, i
,
409 i
== hapd
->conf
->ssid
.wep
.idx
, NULL
, 0,
410 hapd
->conf
->ssid
.wep
.key
[i
],
411 hapd
->conf
->ssid
.wep
.len
[i
])) {
412 wpa_printf(MSG_WARNING
, "Could not set WEP "
416 if (hapd
->conf
->ssid
.wep
.key
[i
] &&
417 i
== hapd
->conf
->ssid
.wep
.idx
)
418 hostapd_set_privacy(hapd
, 1);
425 static int hostapd_flush_old_stations(struct hostapd_data
*hapd
, u16 reason
)
430 if (hostapd_drv_none(hapd
) || hapd
->drv_priv
== NULL
)
433 if (!hapd
->iface
->driver_ap_teardown
) {
434 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
,
435 "Flushing old station entries");
437 if (hostapd_flush(hapd
)) {
438 wpa_msg(hapd
->msg_ctx
, MSG_WARNING
,
439 "Could not connect to kernel driver");
443 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "Deauthenticate all stations");
444 os_memset(addr
, 0xff, ETH_ALEN
);
445 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
446 hostapd_free_stas(hapd
);
452 static void hostapd_bss_deinit_no_free(struct hostapd_data
*hapd
)
454 hostapd_free_stas(hapd
);
455 hostapd_flush_old_stations(hapd
, WLAN_REASON_DEAUTH_LEAVING
);
456 hostapd_clear_wep(hapd
);
461 * hostapd_validate_bssid_configuration - Validate BSSID configuration
462 * @iface: Pointer to interface data
463 * Returns: 0 on success, -1 on failure
465 * This function is used to validate that the configured BSSIDs are valid.
467 static int hostapd_validate_bssid_configuration(struct hostapd_iface
*iface
)
469 u8 mask
[ETH_ALEN
] = { 0 };
470 struct hostapd_data
*hapd
= iface
->bss
[0];
471 unsigned int i
= iface
->conf
->num_bss
, bits
= 0, j
;
474 if (hostapd_drv_none(hapd
))
477 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
479 /* Determine the bits necessary to cover the number of BSSIDs. */
480 for (i
--; i
; i
>>= 1)
483 /* Determine the bits necessary to any configured BSSIDs,
484 if they are higher than the number of BSSIDs. */
485 for (j
= 0; j
< iface
->conf
->num_bss
; j
++) {
486 if (hostapd_mac_comp_empty(iface
->conf
->bss
[j
]->bssid
) == 0) {
492 for (i
= 0; i
< ETH_ALEN
; i
++) {
494 iface
->conf
->bss
[j
]->bssid
[i
] ^
502 for (i
= 0; i
< ETH_ALEN
&& mask
[i
] == 0; i
++)
508 while (mask
[i
] != 0) {
518 wpa_printf(MSG_ERROR
, "Too many bits in the BSSID mask (%u)",
523 os_memset(mask
, 0xff, ETH_ALEN
);
525 for (i
= 5; i
> 5 - j
; i
--)
532 wpa_printf(MSG_DEBUG
, "BSS count %lu, BSSID mask " MACSTR
" (%d bits)",
533 (unsigned long) iface
->conf
->num_bss
, MAC2STR(mask
), bits
);
538 for (i
= 0; i
< ETH_ALEN
; i
++) {
539 if ((hapd
->own_addr
[i
] & mask
[i
]) != hapd
->own_addr
[i
]) {
540 wpa_printf(MSG_ERROR
, "Invalid BSSID mask " MACSTR
541 " for start address " MACSTR
".",
542 MAC2STR(mask
), MAC2STR(hapd
->own_addr
));
543 wpa_printf(MSG_ERROR
, "Start address must be the "
544 "first address in the block (i.e., addr "
545 "AND mask == addr).");
554 static int mac_in_conf(struct hostapd_config
*conf
, const void *a
)
558 for (i
= 0; i
< conf
->num_bss
; i
++) {
559 if (hostapd_mac_comp(conf
->bss
[i
]->bssid
, a
) == 0) {
568 #ifndef CONFIG_NO_RADIUS
570 static int hostapd_das_nas_mismatch(struct hostapd_data
*hapd
,
571 struct radius_das_attrs
*attr
)
573 if (attr
->nas_identifier
&&
574 (!hapd
->conf
->nas_identifier
||
575 os_strlen(hapd
->conf
->nas_identifier
) !=
576 attr
->nas_identifier_len
||
577 os_memcmp(hapd
->conf
->nas_identifier
, attr
->nas_identifier
,
578 attr
->nas_identifier_len
) != 0)) {
579 wpa_printf(MSG_DEBUG
, "RADIUS DAS: NAS-Identifier mismatch");
583 if (attr
->nas_ip_addr
&&
584 (hapd
->conf
->own_ip_addr
.af
!= AF_INET
||
585 os_memcmp(&hapd
->conf
->own_ip_addr
.u
.v4
, attr
->nas_ip_addr
, 4) !=
587 wpa_printf(MSG_DEBUG
, "RADIUS DAS: NAS-IP-Address mismatch");
592 if (attr
->nas_ipv6_addr
&&
593 (hapd
->conf
->own_ip_addr
.af
!= AF_INET6
||
594 os_memcmp(&hapd
->conf
->own_ip_addr
.u
.v6
, attr
->nas_ipv6_addr
, 16)
596 wpa_printf(MSG_DEBUG
, "RADIUS DAS: NAS-IPv6-Address mismatch");
599 #endif /* CONFIG_IPV6 */
605 static struct sta_info
* hostapd_das_find_sta(struct hostapd_data
*hapd
,
606 struct radius_das_attrs
*attr
)
608 struct sta_info
*sta
= NULL
;
612 sta
= ap_get_sta(hapd
, attr
->sta_addr
);
614 if (sta
== NULL
&& attr
->acct_session_id
&&
615 attr
->acct_session_id_len
== 17) {
616 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
617 os_snprintf(buf
, sizeof(buf
), "%08X-%08X",
618 sta
->acct_session_id_hi
,
619 sta
->acct_session_id_lo
);
620 if (os_memcmp(attr
->acct_session_id
, buf
, 17) == 0)
625 if (sta
== NULL
&& attr
->cui
) {
626 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
628 cui
= ieee802_1x_get_radius_cui(sta
->eapol_sm
);
629 if (cui
&& wpabuf_len(cui
) == attr
->cui_len
&&
630 os_memcmp(wpabuf_head(cui
), attr
->cui
,
636 if (sta
== NULL
&& attr
->user_name
) {
637 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
640 identity
= ieee802_1x_get_identity(sta
->eapol_sm
,
643 identity_len
== attr
->user_name_len
&&
644 os_memcmp(identity
, attr
->user_name
, identity_len
)
654 static enum radius_das_res
655 hostapd_das_disconnect(void *ctx
, struct radius_das_attrs
*attr
)
657 struct hostapd_data
*hapd
= ctx
;
658 struct sta_info
*sta
;
660 if (hostapd_das_nas_mismatch(hapd
, attr
))
661 return RADIUS_DAS_NAS_MISMATCH
;
663 sta
= hostapd_das_find_sta(hapd
, attr
);
665 return RADIUS_DAS_SESSION_NOT_FOUND
;
667 wpa_auth_pmksa_remove(hapd
->wpa_auth
, sta
->addr
);
669 hostapd_drv_sta_deauth(hapd
, sta
->addr
,
670 WLAN_REASON_PREV_AUTH_NOT_VALID
);
671 ap_sta_deauthenticate(hapd
, sta
, WLAN_REASON_PREV_AUTH_NOT_VALID
);
673 return RADIUS_DAS_SUCCESS
;
676 #endif /* CONFIG_NO_RADIUS */
680 * hostapd_setup_bss - Per-BSS setup (initialization)
681 * @hapd: Pointer to BSS data
682 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
683 * but interface may exist
685 * This function is used to initialize all per-BSS data structures and
686 * resources. This gets called in a loop for each BSS when an interface is
687 * initialized. Most of the modules that are initialized here will be
688 * deinitialized in hostapd_cleanup().
690 static int hostapd_setup_bss(struct hostapd_data
*hapd
, int first
)
692 struct hostapd_bss_config
*conf
= hapd
->conf
;
693 u8 ssid
[HOSTAPD_MAX_SSID_LEN
+ 1];
694 int ssid_len
, set_ssid
;
695 char force_ifname
[IFNAMSIZ
];
696 u8 if_addr
[ETH_ALEN
];
697 int flush_old_stations
= 1;
699 wpa_printf(MSG_DEBUG
, "%s(hapd=%p (%s), first=%d)",
700 __func__
, hapd
, conf
->iface
, first
);
702 #ifdef EAP_SERVER_TNC
703 if (conf
->tnc
&& tncs_global_init() < 0) {
704 wpa_printf(MSG_ERROR
, "Failed to initialize TNCS");
707 #endif /* EAP_SERVER_TNC */
710 wpa_printf(MSG_ERROR
, "%s: Interface %s was already started",
711 __func__
, conf
->iface
);
716 if (!first
|| first
== -1) {
717 if (hostapd_mac_comp_empty(conf
->bssid
) == 0) {
718 /* Allocate the next available BSSID. */
720 inc_byte_array(hapd
->own_addr
, ETH_ALEN
);
721 } while (mac_in_conf(hapd
->iconf
, hapd
->own_addr
));
723 /* Allocate the configured BSSID. */
724 os_memcpy(hapd
->own_addr
, conf
->bssid
, ETH_ALEN
);
726 if (hostapd_mac_comp(hapd
->own_addr
,
727 hapd
->iface
->bss
[0]->own_addr
) ==
729 wpa_printf(MSG_ERROR
, "BSS '%s' may not have "
730 "BSSID set to the MAC address of "
731 "the radio", conf
->iface
);
736 hapd
->interface_added
= 1;
737 if (hostapd_if_add(hapd
->iface
->bss
[0], WPA_IF_AP_BSS
,
738 conf
->iface
, hapd
->own_addr
, hapd
,
739 &hapd
->drv_priv
, force_ifname
, if_addr
,
740 conf
->bridge
[0] ? conf
->bridge
: NULL
,
742 wpa_printf(MSG_ERROR
, "Failed to add BSS (BSSID="
743 MACSTR
")", MAC2STR(hapd
->own_addr
));
744 hapd
->interface_added
= 0;
749 if (conf
->wmm_enabled
< 0)
750 conf
->wmm_enabled
= hapd
->iconf
->ieee80211n
;
753 if (hapd
->iface
->mconf
== NULL
)
754 flush_old_stations
= 0;
755 #endif /* CONFIG_MESH */
757 if (flush_old_stations
)
758 hostapd_flush_old_stations(hapd
,
759 WLAN_REASON_PREV_AUTH_NOT_VALID
);
760 hostapd_set_privacy(hapd
, 0);
762 hostapd_broadcast_wep_clear(hapd
);
763 if (hostapd_setup_encryption(conf
->iface
, hapd
))
767 * Fetch the SSID from the system and use it or,
768 * if one was specified in the config file, verify they
771 ssid_len
= hostapd_get_ssid(hapd
, ssid
, sizeof(ssid
));
773 wpa_printf(MSG_ERROR
, "Could not read SSID from system");
776 if (conf
->ssid
.ssid_set
) {
778 * If SSID is specified in the config file and it differs
779 * from what is being used then force installation of the
782 set_ssid
= (conf
->ssid
.ssid_len
!= (size_t) ssid_len
||
783 os_memcmp(conf
->ssid
.ssid
, ssid
, ssid_len
) != 0);
786 * No SSID in the config file; just use the one we got
790 conf
->ssid
.ssid_len
= ssid_len
;
791 os_memcpy(conf
->ssid
.ssid
, ssid
, conf
->ssid
.ssid_len
);
794 if (!hostapd_drv_none(hapd
)) {
795 wpa_printf(MSG_ERROR
, "Using interface %s with hwaddr " MACSTR
797 conf
->iface
, MAC2STR(hapd
->own_addr
),
798 wpa_ssid_txt(conf
->ssid
.ssid
, conf
->ssid
.ssid_len
));
801 if (hostapd_setup_wpa_psk(conf
)) {
802 wpa_printf(MSG_ERROR
, "WPA-PSK setup failed.");
806 /* Set SSID for the kernel driver (to be used in beacon and probe
807 * response frames) */
808 if (set_ssid
&& hostapd_set_ssid(hapd
, conf
->ssid
.ssid
,
809 conf
->ssid
.ssid_len
)) {
810 wpa_printf(MSG_ERROR
, "Could not set SSID for kernel driver");
814 if (wpa_debug_level
<= MSG_MSGDUMP
)
815 conf
->radius
->msg_dumps
= 1;
816 #ifndef CONFIG_NO_RADIUS
817 hapd
->radius
= radius_client_init(hapd
, conf
->radius
);
818 if (hapd
->radius
== NULL
) {
819 wpa_printf(MSG_ERROR
, "RADIUS client initialization failed.");
823 if (conf
->radius_das_port
) {
824 struct radius_das_conf das_conf
;
825 os_memset(&das_conf
, 0, sizeof(das_conf
));
826 das_conf
.port
= conf
->radius_das_port
;
827 das_conf
.shared_secret
= conf
->radius_das_shared_secret
;
828 das_conf
.shared_secret_len
=
829 conf
->radius_das_shared_secret_len
;
830 das_conf
.client_addr
= &conf
->radius_das_client_addr
;
831 das_conf
.time_window
= conf
->radius_das_time_window
;
832 das_conf
.require_event_timestamp
=
833 conf
->radius_das_require_event_timestamp
;
835 das_conf
.disconnect
= hostapd_das_disconnect
;
836 hapd
->radius_das
= radius_das_init(&das_conf
);
837 if (hapd
->radius_das
== NULL
) {
838 wpa_printf(MSG_ERROR
, "RADIUS DAS initialization "
843 #endif /* CONFIG_NO_RADIUS */
845 if (hostapd_acl_init(hapd
)) {
846 wpa_printf(MSG_ERROR
, "ACL initialization failed.");
849 if (hostapd_init_wps(hapd
, conf
))
852 if (authsrv_init(hapd
) < 0)
855 if (ieee802_1x_init(hapd
)) {
856 wpa_printf(MSG_ERROR
, "IEEE 802.1X initialization failed.");
860 if ((conf
->wpa
|| conf
->osen
) && hostapd_setup_wpa(hapd
))
863 if (accounting_init(hapd
)) {
864 wpa_printf(MSG_ERROR
, "Accounting initialization failed.");
868 if (conf
->ieee802_11f
&&
869 (hapd
->iapp
= iapp_init(hapd
, conf
->iapp_iface
)) == NULL
) {
870 wpa_printf(MSG_ERROR
, "IEEE 802.11F (IAPP) initialization "
875 #ifdef CONFIG_INTERWORKING
876 if (gas_serv_init(hapd
)) {
877 wpa_printf(MSG_ERROR
, "GAS server initialization failed");
881 if (conf
->qos_map_set_len
&&
882 hostapd_drv_set_qos_map(hapd
, conf
->qos_map_set
,
883 conf
->qos_map_set_len
)) {
884 wpa_printf(MSG_ERROR
, "Failed to initialize QoS Map");
887 #endif /* CONFIG_INTERWORKING */
889 if (conf
->bss_load_update_period
&& bss_load_update_init(hapd
)) {
890 wpa_printf(MSG_ERROR
, "BSS Load initialization failed");
894 if (!hostapd_drv_none(hapd
) && vlan_init(hapd
)) {
895 wpa_printf(MSG_ERROR
, "VLAN initialization failed.");
899 if (!conf
->start_disabled
&& ieee802_11_set_beacon(hapd
) < 0)
902 if (hapd
->wpa_auth
&& wpa_init_keys(hapd
->wpa_auth
) < 0)
905 if (hapd
->driver
&& hapd
->driver
->set_operstate
)
906 hapd
->driver
->set_operstate(hapd
->drv_priv
, 1);
912 static void hostapd_tx_queue_params(struct hostapd_iface
*iface
)
914 struct hostapd_data
*hapd
= iface
->bss
[0];
916 struct hostapd_tx_queue_params
*p
;
919 if (iface
->mconf
== NULL
)
921 #endif /* CONFIG_MESH */
923 for (i
= 0; i
< NUM_TX_QUEUES
; i
++) {
924 p
= &iface
->conf
->tx_queue
[i
];
926 if (hostapd_set_tx_queue_params(hapd
, i
, p
->aifs
, p
->cwmin
,
927 p
->cwmax
, p
->burst
)) {
928 wpa_printf(MSG_DEBUG
, "Failed to set TX queue "
929 "parameters for queue %d.", i
);
930 /* Continue anyway */
936 static int hostapd_set_acl_list(struct hostapd_data
*hapd
,
937 struct mac_acl_entry
*mac_acl
,
938 int n_entries
, u8 accept_acl
)
940 struct hostapd_acl_params
*acl_params
;
943 acl_params
= os_zalloc(sizeof(*acl_params
) +
944 (n_entries
* sizeof(acl_params
->mac_acl
[0])));
948 for (i
= 0; i
< n_entries
; i
++)
949 os_memcpy(acl_params
->mac_acl
[i
].addr
, mac_acl
[i
].addr
,
952 acl_params
->acl_policy
= accept_acl
;
953 acl_params
->num_mac_acl
= n_entries
;
955 err
= hostapd_drv_set_acl(hapd
, acl_params
);
963 static void hostapd_set_acl(struct hostapd_data
*hapd
)
965 struct hostapd_config
*conf
= hapd
->iconf
;
969 if (hapd
->iface
->drv_max_acl_mac_addrs
== 0)
972 if (conf
->bss
[0]->macaddr_acl
== DENY_UNLESS_ACCEPTED
) {
974 err
= hostapd_set_acl_list(hapd
, conf
->bss
[0]->accept_mac
,
975 conf
->bss
[0]->num_accept_mac
,
978 wpa_printf(MSG_DEBUG
, "Failed to set accept acl");
981 } else if (conf
->bss
[0]->macaddr_acl
== ACCEPT_UNLESS_DENIED
) {
983 err
= hostapd_set_acl_list(hapd
, conf
->bss
[0]->deny_mac
,
984 conf
->bss
[0]->num_deny_mac
,
987 wpa_printf(MSG_DEBUG
, "Failed to set deny acl");
994 static int start_ctrl_iface_bss(struct hostapd_data
*hapd
)
996 if (!hapd
->iface
->interfaces
||
997 !hapd
->iface
->interfaces
->ctrl_iface_init
)
1000 if (hapd
->iface
->interfaces
->ctrl_iface_init(hapd
)) {
1001 wpa_printf(MSG_ERROR
,
1002 "Failed to setup control interface for %s",
1011 static int start_ctrl_iface(struct hostapd_iface
*iface
)
1015 if (!iface
->interfaces
|| !iface
->interfaces
->ctrl_iface_init
)
1018 for (i
= 0; i
< iface
->num_bss
; i
++) {
1019 struct hostapd_data
*hapd
= iface
->bss
[i
];
1020 if (iface
->interfaces
->ctrl_iface_init(hapd
)) {
1021 wpa_printf(MSG_ERROR
,
1022 "Failed to setup control interface for %s",
1032 static void channel_list_update_timeout(void *eloop_ctx
, void *timeout_ctx
)
1034 struct hostapd_iface
*iface
= eloop_ctx
;
1036 if (!iface
->wait_channel_update
) {
1037 wpa_printf(MSG_INFO
, "Channel list update timeout, but interface was not waiting for it");
1042 * It is possible that the existing channel list is acceptable, so try
1045 wpa_printf(MSG_DEBUG
, "Channel list update timeout - try to continue anyway");
1046 setup_interface2(iface
);
1050 void hostapd_channel_list_updated(struct hostapd_iface
*iface
, int initiator
)
1052 if (!iface
->wait_channel_update
|| initiator
!= REGDOM_SET_BY_USER
)
1055 wpa_printf(MSG_DEBUG
, "Channel list updated - continue setup");
1056 eloop_cancel_timeout(channel_list_update_timeout
, iface
, NULL
);
1057 setup_interface2(iface
);
1061 static int setup_interface(struct hostapd_iface
*iface
)
1063 struct hostapd_data
*hapd
= iface
->bss
[0];
1067 * It is possible that setup_interface() is called after the interface
1068 * was disabled etc., in which case driver_ap_teardown is possibly set
1069 * to 1. Clear it here so any other key/station deletion, which is not
1070 * part of a teardown flow, would also call the relevant driver
1073 iface
->driver_ap_teardown
= 0;
1075 if (!iface
->phy
[0]) {
1076 const char *phy
= hostapd_drv_get_radio_name(hapd
);
1078 wpa_printf(MSG_DEBUG
, "phy: %s", phy
);
1079 os_strlcpy(iface
->phy
, phy
, sizeof(iface
->phy
));
1084 * Make sure that all BSSes get configured with a pointer to the same
1087 for (i
= 1; i
< iface
->num_bss
; i
++) {
1088 iface
->bss
[i
]->driver
= hapd
->driver
;
1089 iface
->bss
[i
]->drv_priv
= hapd
->drv_priv
;
1092 if (hostapd_validate_bssid_configuration(iface
))
1096 * Initialize control interfaces early to allow external monitoring of
1097 * channel setup operations that may take considerable amount of time
1098 * especially for DFS cases.
1100 if (start_ctrl_iface(iface
))
1103 if (hapd
->iconf
->country
[0] && hapd
->iconf
->country
[1]) {
1104 char country
[4], previous_country
[4];
1106 hostapd_set_state(iface
, HAPD_IFACE_COUNTRY_UPDATE
);
1107 if (hostapd_get_country(hapd
, previous_country
) < 0)
1108 previous_country
[0] = '\0';
1110 os_memcpy(country
, hapd
->iconf
->country
, 3);
1112 if (hostapd_set_country(hapd
, country
) < 0) {
1113 wpa_printf(MSG_ERROR
, "Failed to set country code");
1117 wpa_printf(MSG_DEBUG
, "Previous country code %s, new country code %s",
1118 previous_country
, country
);
1120 if (os_strncmp(previous_country
, country
, 2) != 0) {
1121 wpa_printf(MSG_DEBUG
, "Continue interface setup after channel list update");
1122 iface
->wait_channel_update
= 1;
1123 eloop_register_timeout(5, 0,
1124 channel_list_update_timeout
,
1130 return setup_interface2(iface
);
1134 static int setup_interface2(struct hostapd_iface
*iface
)
1136 iface
->wait_channel_update
= 0;
1138 if (hostapd_get_hw_features(iface
)) {
1139 /* Not all drivers support this yet, so continue without hw
1142 int ret
= hostapd_select_hw_mode(iface
);
1144 wpa_printf(MSG_ERROR
, "Could not select hw_mode and "
1145 "channel. (%d)", ret
);
1149 wpa_printf(MSG_DEBUG
, "Interface initialization will be completed in a callback (ACS)");
1152 ret
= hostapd_check_ht_capab(iface
);
1156 wpa_printf(MSG_DEBUG
, "Interface initialization will "
1157 "be completed in a callback");
1161 if (iface
->conf
->ieee80211h
)
1162 wpa_printf(MSG_DEBUG
, "DFS support is enabled");
1164 return hostapd_setup_interface_complete(iface
, 0);
1167 hostapd_set_state(iface
, HAPD_IFACE_DISABLED
);
1168 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, AP_EVENT_DISABLED
);
1169 if (iface
->interfaces
&& iface
->interfaces
->terminate_on_error
)
1176 * hostapd_setup_interface_complete - Complete interface setup
1178 * This function is called when previous steps in the interface setup has been
1179 * completed. This can also start operations, e.g., DFS, that will require
1180 * additional processing before interface is ready to be enabled. Such
1181 * operations will call this function from eloop callbacks when finished.
1183 int hostapd_setup_interface_complete(struct hostapd_iface
*iface
, int err
)
1185 struct hostapd_data
*hapd
= iface
->bss
[0];
1188 int delay_apply_cfg
= 0;
1193 wpa_printf(MSG_DEBUG
, "Completing interface initialization");
1194 if (iface
->conf
->channel
) {
1197 #endif /* NEED_AP_MLME */
1199 iface
->freq
= hostapd_hw_get_freq(hapd
, iface
->conf
->channel
);
1200 wpa_printf(MSG_DEBUG
, "Mode: %s Channel: %d "
1201 "Frequency: %d MHz",
1202 hostapd_hw_mode_txt(iface
->conf
->hw_mode
),
1203 iface
->conf
->channel
, iface
->freq
);
1206 /* Handle DFS only if it is not offloaded to the driver */
1207 if (!(iface
->drv_flags
& WPA_DRIVER_FLAGS_DFS_OFFLOAD
)) {
1209 res
= hostapd_handle_dfs(iface
);
1216 #endif /* NEED_AP_MLME */
1219 if (iface
->mconf
!= NULL
) {
1220 wpa_printf(MSG_DEBUG
,
1221 "%s: Mesh configuration will be applied while joining the mesh network",
1222 iface
->bss
[0]->conf
->iface
);
1223 delay_apply_cfg
= 1;
1225 #endif /* CONFIG_MESH */
1227 if (!delay_apply_cfg
&&
1228 hostapd_set_freq(hapd
, hapd
->iconf
->hw_mode
, iface
->freq
,
1229 hapd
->iconf
->channel
,
1230 hapd
->iconf
->ieee80211n
,
1231 hapd
->iconf
->ieee80211ac
,
1232 hapd
->iconf
->secondary_channel
,
1233 hapd
->iconf
->vht_oper_chwidth
,
1234 hapd
->iconf
->vht_oper_centr_freq_seg0_idx
,
1235 hapd
->iconf
->vht_oper_centr_freq_seg1_idx
)) {
1236 wpa_printf(MSG_ERROR
, "Could not set channel for "
1242 if (iface
->current_mode
) {
1243 if (hostapd_prepare_rates(iface
, iface
->current_mode
)) {
1244 wpa_printf(MSG_ERROR
, "Failed to prepare rates "
1246 hostapd_logger(hapd
, NULL
, HOSTAPD_MODULE_IEEE80211
,
1247 HOSTAPD_LEVEL_WARNING
,
1248 "Failed to prepare rates table.");
1253 if (hapd
->iconf
->rts_threshold
> -1 &&
1254 hostapd_set_rts(hapd
, hapd
->iconf
->rts_threshold
)) {
1255 wpa_printf(MSG_ERROR
, "Could not set RTS threshold for "
1260 if (hapd
->iconf
->fragm_threshold
> -1 &&
1261 hostapd_set_frag(hapd
, hapd
->iconf
->fragm_threshold
)) {
1262 wpa_printf(MSG_ERROR
, "Could not set fragmentation threshold "
1263 "for kernel driver");
1267 prev_addr
= hapd
->own_addr
;
1269 for (j
= 0; j
< iface
->num_bss
; j
++) {
1270 hapd
= iface
->bss
[j
];
1272 os_memcpy(hapd
->own_addr
, prev_addr
, ETH_ALEN
);
1273 if (hostapd_setup_bss(hapd
, j
== 0)) {
1275 hapd
= iface
->bss
[j
];
1276 hostapd_bss_deinit_no_free(hapd
);
1277 hostapd_free_hapd_data(hapd
);
1281 if (hostapd_mac_comp_empty(hapd
->conf
->bssid
) == 0)
1282 prev_addr
= hapd
->own_addr
;
1284 hapd
= iface
->bss
[0];
1286 hostapd_tx_queue_params(iface
);
1288 ap_list_init(iface
);
1290 hostapd_set_acl(hapd
);
1292 if (hostapd_driver_commit(hapd
) < 0) {
1293 wpa_printf(MSG_ERROR
, "%s: Failed to commit driver "
1294 "configuration", __func__
);
1299 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1300 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1301 * mode), the interface is up only after driver_commit, so initialize
1302 * WPS after driver_commit.
1304 for (j
= 0; j
< iface
->num_bss
; j
++) {
1305 if (hostapd_init_wps_complete(iface
->bss
[j
]))
1309 hostapd_set_state(iface
, HAPD_IFACE_ENABLED
);
1310 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, AP_EVENT_ENABLED
);
1311 if (hapd
->setup_complete_cb
)
1312 hapd
->setup_complete_cb(hapd
->setup_complete_cb_ctx
);
1314 wpa_printf(MSG_DEBUG
, "%s: Setup of interface done.",
1315 iface
->bss
[0]->conf
->iface
);
1316 if (iface
->interfaces
&& iface
->interfaces
->terminate_on_error
> 0)
1317 iface
->interfaces
->terminate_on_error
--;
1322 wpa_printf(MSG_ERROR
, "Interface initialization failed");
1323 hostapd_set_state(iface
, HAPD_IFACE_DISABLED
);
1324 wpa_msg(hapd
->msg_ctx
, MSG_INFO
, AP_EVENT_DISABLED
);
1325 if (iface
->interfaces
&& iface
->interfaces
->terminate_on_error
)
1332 * hostapd_setup_interface - Setup of an interface
1333 * @iface: Pointer to interface data.
1334 * Returns: 0 on success, -1 on failure
1336 * Initializes the driver interface, validates the configuration,
1337 * and sets driver parameters based on the configuration.
1338 * Flushes old stations, sets the channel, encryption,
1339 * beacons, and WDS links based on the configuration.
1341 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1342 * or DFS operations, this function returns 0 before such operations have been
1343 * completed. The pending operations are registered into eloop and will be
1344 * completed from eloop callbacks. Those callbacks end up calling
1345 * hostapd_setup_interface_complete() once setup has been completed.
1347 int hostapd_setup_interface(struct hostapd_iface
*iface
)
1351 ret
= setup_interface(iface
);
1353 wpa_printf(MSG_ERROR
, "%s: Unable to setup interface.",
1354 iface
->bss
[0]->conf
->iface
);
1363 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1364 * @hapd_iface: Pointer to interface data
1365 * @conf: Pointer to per-interface configuration
1366 * @bss: Pointer to per-BSS configuration for this BSS
1367 * Returns: Pointer to allocated BSS data
1369 * This function is used to allocate per-BSS data structure. This data will be
1370 * freed after hostapd_cleanup() is called for it during interface
1373 struct hostapd_data
*
1374 hostapd_alloc_bss_data(struct hostapd_iface
*hapd_iface
,
1375 struct hostapd_config
*conf
,
1376 struct hostapd_bss_config
*bss
)
1378 struct hostapd_data
*hapd
;
1380 hapd
= os_zalloc(sizeof(*hapd
));
1384 hapd
->new_assoc_sta_cb
= hostapd_new_assoc_sta
;
1387 hapd
->iface
= hapd_iface
;
1388 hapd
->driver
= hapd
->iconf
->driver
;
1389 hapd
->ctrl_sock
= -1;
1395 static void hostapd_bss_deinit(struct hostapd_data
*hapd
)
1397 wpa_printf(MSG_DEBUG
, "%s: deinit bss %s", __func__
,
1399 hostapd_bss_deinit_no_free(hapd
);
1400 hostapd_cleanup(hapd
);
1404 void hostapd_interface_deinit(struct hostapd_iface
*iface
)
1408 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
1412 #ifdef CONFIG_IEEE80211N
1414 hostapd_stop_setup_timers(iface
);
1415 eloop_cancel_timeout(ap_ht2040_timeout
, iface
, NULL
);
1416 #endif /* NEED_AP_MLME */
1417 #endif /* CONFIG_IEEE80211N */
1418 eloop_cancel_timeout(channel_list_update_timeout
, iface
, NULL
);
1419 iface
->wait_channel_update
= 0;
1421 for (j
= iface
->num_bss
- 1; j
>= 0; j
--)
1422 hostapd_bss_deinit(iface
->bss
[j
]);
1426 void hostapd_interface_free(struct hostapd_iface
*iface
)
1429 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
1430 for (j
= 0; j
< iface
->num_bss
; j
++) {
1431 wpa_printf(MSG_DEBUG
, "%s: free hapd %p",
1432 __func__
, iface
->bss
[j
]);
1433 os_free(iface
->bss
[j
]);
1435 hostapd_cleanup_iface(iface
);
1440 * hostapd_init - Allocate and initialize per-interface data
1441 * @config_file: Path to the configuration file
1442 * Returns: Pointer to the allocated interface data or %NULL on failure
1444 * This function is used to allocate main data structures for per-interface
1445 * data. The allocated data buffer will be freed by calling
1446 * hostapd_cleanup_iface().
1448 struct hostapd_iface
* hostapd_init(struct hapd_interfaces
*interfaces
,
1449 const char *config_file
)
1451 struct hostapd_iface
*hapd_iface
= NULL
;
1452 struct hostapd_config
*conf
= NULL
;
1453 struct hostapd_data
*hapd
;
1456 hapd_iface
= os_zalloc(sizeof(*hapd_iface
));
1457 if (hapd_iface
== NULL
)
1460 hapd_iface
->config_fname
= os_strdup(config_file
);
1461 if (hapd_iface
->config_fname
== NULL
)
1464 conf
= interfaces
->config_read_cb(hapd_iface
->config_fname
);
1467 hapd_iface
->conf
= conf
;
1469 hapd_iface
->num_bss
= conf
->num_bss
;
1470 hapd_iface
->bss
= os_calloc(conf
->num_bss
,
1471 sizeof(struct hostapd_data
*));
1472 if (hapd_iface
->bss
== NULL
)
1475 for (i
= 0; i
< conf
->num_bss
; i
++) {
1476 hapd
= hapd_iface
->bss
[i
] =
1477 hostapd_alloc_bss_data(hapd_iface
, conf
,
1481 hapd
->msg_ctx
= hapd
;
1487 wpa_printf(MSG_ERROR
, "Failed to set up interface with %s",
1490 hostapd_config_free(conf
);
1492 os_free(hapd_iface
->config_fname
);
1493 os_free(hapd_iface
->bss
);
1494 wpa_printf(MSG_DEBUG
, "%s: free iface %p",
1495 __func__
, hapd_iface
);
1496 os_free(hapd_iface
);
1502 static int ifname_in_use(struct hapd_interfaces
*interfaces
, const char *ifname
)
1506 for (i
= 0; i
< interfaces
->count
; i
++) {
1507 struct hostapd_iface
*iface
= interfaces
->iface
[i
];
1508 for (j
= 0; j
< iface
->num_bss
; j
++) {
1509 struct hostapd_data
*hapd
= iface
->bss
[j
];
1510 if (os_strcmp(ifname
, hapd
->conf
->iface
) == 0)
1520 * hostapd_interface_init_bss - Read configuration file and init BSS data
1522 * This function is used to parse configuration file for a BSS. This BSS is
1523 * added to an existing interface sharing the same radio (if any) or a new
1524 * interface is created if this is the first interface on a radio. This
1525 * allocate memory for the BSS. No actual driver operations are started.
1527 * This is similar to hostapd_interface_init(), but for a case where the
1528 * configuration is used to add a single BSS instead of all BSSes for a radio.
1530 struct hostapd_iface
*
1531 hostapd_interface_init_bss(struct hapd_interfaces
*interfaces
, const char *phy
,
1532 const char *config_fname
, int debug
)
1534 struct hostapd_iface
*new_iface
= NULL
, *iface
= NULL
;
1535 struct hostapd_data
*hapd
;
1542 for (i
= 0; i
< interfaces
->count
; i
++) {
1543 if (os_strcmp(interfaces
->iface
[i
]->phy
, phy
) == 0) {
1544 iface
= interfaces
->iface
[i
];
1549 wpa_printf(MSG_INFO
, "Configuration file: %s (phy %s)%s",
1550 config_fname
, phy
, iface
? "" : " --> new PHY");
1552 struct hostapd_config
*conf
;
1553 struct hostapd_bss_config
**tmp_conf
;
1554 struct hostapd_data
**tmp_bss
;
1555 struct hostapd_bss_config
*bss
;
1558 /* Add new BSS to existing iface */
1559 conf
= interfaces
->config_read_cb(config_fname
);
1562 if (conf
->num_bss
> 1) {
1563 wpa_printf(MSG_ERROR
, "Multiple BSSes specified in BSS-config");
1564 hostapd_config_free(conf
);
1568 ifname
= conf
->bss
[0]->iface
;
1569 if (ifname
[0] != '\0' && ifname_in_use(interfaces
, ifname
)) {
1570 wpa_printf(MSG_ERROR
,
1571 "Interface name %s already in use", ifname
);
1572 hostapd_config_free(conf
);
1576 tmp_conf
= os_realloc_array(
1577 iface
->conf
->bss
, iface
->conf
->num_bss
+ 1,
1578 sizeof(struct hostapd_bss_config
*));
1579 tmp_bss
= os_realloc_array(iface
->bss
, iface
->num_bss
+ 1,
1580 sizeof(struct hostapd_data
*));
1582 iface
->bss
= tmp_bss
;
1584 iface
->conf
->bss
= tmp_conf
;
1585 iface
->conf
->last_bss
= tmp_conf
[0];
1587 if (tmp_bss
== NULL
|| tmp_conf
== NULL
) {
1588 hostapd_config_free(conf
);
1591 bss
= iface
->conf
->bss
[iface
->conf
->num_bss
] = conf
->bss
[0];
1592 iface
->conf
->num_bss
++;
1594 hapd
= hostapd_alloc_bss_data(iface
, iface
->conf
, bss
);
1596 iface
->conf
->num_bss
--;
1597 hostapd_config_free(conf
);
1600 iface
->conf
->last_bss
= bss
;
1601 iface
->bss
[iface
->num_bss
] = hapd
;
1602 hapd
->msg_ctx
= hapd
;
1604 bss_idx
= iface
->num_bss
++;
1606 conf
->bss
[0] = NULL
;
1607 hostapd_config_free(conf
);
1609 /* Add a new iface with the first BSS */
1610 new_iface
= iface
= hostapd_init(interfaces
, config_fname
);
1613 os_strlcpy(iface
->phy
, phy
, sizeof(iface
->phy
));
1614 iface
->interfaces
= interfaces
;
1618 for (k
= 0; k
< debug
; k
++) {
1619 if (iface
->bss
[bss_idx
]->conf
->logger_stdout_level
> 0)
1620 iface
->bss
[bss_idx
]->conf
->logger_stdout_level
--;
1623 if (iface
->conf
->bss
[bss_idx
]->iface
[0] == '\0' &&
1624 !hostapd_drv_none(iface
->bss
[bss_idx
])) {
1625 wpa_printf(MSG_ERROR
, "Interface name not specified in %s",
1628 hostapd_interface_deinit_free(new_iface
);
1636 void hostapd_interface_deinit_free(struct hostapd_iface
*iface
)
1638 const struct wpa_driver_ops
*driver
;
1641 wpa_printf(MSG_DEBUG
, "%s(%p)", __func__
, iface
);
1644 wpa_printf(MSG_DEBUG
, "%s: num_bss=%u conf->num_bss=%u",
1645 __func__
, (unsigned int) iface
->num_bss
,
1646 (unsigned int) iface
->conf
->num_bss
);
1647 driver
= iface
->bss
[0]->driver
;
1648 drv_priv
= iface
->bss
[0]->drv_priv
;
1649 hostapd_interface_deinit(iface
);
1650 wpa_printf(MSG_DEBUG
, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1651 __func__
, driver
, drv_priv
);
1652 if (driver
&& driver
->hapd_deinit
&& drv_priv
) {
1653 driver
->hapd_deinit(drv_priv
);
1654 iface
->bss
[0]->drv_priv
= NULL
;
1656 hostapd_interface_free(iface
);
1660 static void hostapd_deinit_driver(const struct wpa_driver_ops
*driver
,
1662 struct hostapd_iface
*hapd_iface
)
1666 wpa_printf(MSG_DEBUG
, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1667 __func__
, driver
, drv_priv
);
1668 if (driver
&& driver
->hapd_deinit
&& drv_priv
) {
1669 driver
->hapd_deinit(drv_priv
);
1670 for (j
= 0; j
< hapd_iface
->num_bss
; j
++) {
1671 wpa_printf(MSG_DEBUG
, "%s:bss[%d]->drv_priv=%p",
1673 hapd_iface
->bss
[j
]->drv_priv
);
1674 if (hapd_iface
->bss
[j
]->drv_priv
== drv_priv
)
1675 hapd_iface
->bss
[j
]->drv_priv
= NULL
;
1681 int hostapd_enable_iface(struct hostapd_iface
*hapd_iface
)
1685 if (hapd_iface
->bss
[0]->drv_priv
!= NULL
) {
1686 wpa_printf(MSG_ERROR
, "Interface %s already enabled",
1687 hapd_iface
->conf
->bss
[0]->iface
);
1691 wpa_printf(MSG_DEBUG
, "Enable interface %s",
1692 hapd_iface
->conf
->bss
[0]->iface
);
1694 for (j
= 0; j
< hapd_iface
->num_bss
; j
++)
1695 hostapd_set_security_params(hapd_iface
->conf
->bss
[j
], 1);
1696 if (hostapd_config_check(hapd_iface
->conf
, 1) < 0) {
1697 wpa_printf(MSG_INFO
, "Invalid configuration - cannot enable");
1701 if (hapd_iface
->interfaces
== NULL
||
1702 hapd_iface
->interfaces
->driver_init
== NULL
||
1703 hapd_iface
->interfaces
->driver_init(hapd_iface
))
1706 if (hostapd_setup_interface(hapd_iface
)) {
1707 hostapd_deinit_driver(hapd_iface
->bss
[0]->driver
,
1708 hapd_iface
->bss
[0]->drv_priv
,
1717 int hostapd_reload_iface(struct hostapd_iface
*hapd_iface
)
1721 wpa_printf(MSG_DEBUG
, "Reload interface %s",
1722 hapd_iface
->conf
->bss
[0]->iface
);
1723 for (j
= 0; j
< hapd_iface
->num_bss
; j
++)
1724 hostapd_set_security_params(hapd_iface
->conf
->bss
[j
], 1);
1725 if (hostapd_config_check(hapd_iface
->conf
, 1) < 0) {
1726 wpa_printf(MSG_ERROR
, "Updated configuration is invalid");
1729 hostapd_clear_old(hapd_iface
);
1730 for (j
= 0; j
< hapd_iface
->num_bss
; j
++)
1731 hostapd_reload_bss(hapd_iface
->bss
[j
]);
1737 int hostapd_disable_iface(struct hostapd_iface
*hapd_iface
)
1740 const struct wpa_driver_ops
*driver
;
1743 if (hapd_iface
== NULL
)
1746 if (hapd_iface
->bss
[0]->drv_priv
== NULL
) {
1747 wpa_printf(MSG_INFO
, "Interface %s already disabled",
1748 hapd_iface
->conf
->bss
[0]->iface
);
1752 wpa_msg(hapd_iface
->bss
[0]->msg_ctx
, MSG_INFO
, AP_EVENT_DISABLED
);
1753 driver
= hapd_iface
->bss
[0]->driver
;
1754 drv_priv
= hapd_iface
->bss
[0]->drv_priv
;
1756 hapd_iface
->driver_ap_teardown
=
1757 !!(hapd_iface
->drv_flags
&
1758 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT
);
1760 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1761 for (j
= 0; j
< hapd_iface
->num_bss
; j
++) {
1762 struct hostapd_data
*hapd
= hapd_iface
->bss
[j
];
1763 hostapd_bss_deinit_no_free(hapd
);
1764 hostapd_free_hapd_data(hapd
);
1767 hostapd_deinit_driver(driver
, drv_priv
, hapd_iface
);
1769 /* From hostapd_cleanup_iface: These were initialized in
1770 * hostapd_setup_interface and hostapd_setup_interface_complete
1772 hostapd_cleanup_iface_partial(hapd_iface
);
1774 wpa_printf(MSG_DEBUG
, "Interface %s disabled",
1775 hapd_iface
->bss
[0]->conf
->iface
);
1776 hostapd_set_state(hapd_iface
, HAPD_IFACE_DISABLED
);
1781 static struct hostapd_iface
*
1782 hostapd_iface_alloc(struct hapd_interfaces
*interfaces
)
1784 struct hostapd_iface
**iface
, *hapd_iface
;
1786 iface
= os_realloc_array(interfaces
->iface
, interfaces
->count
+ 1,
1787 sizeof(struct hostapd_iface
*));
1790 interfaces
->iface
= iface
;
1791 hapd_iface
= interfaces
->iface
[interfaces
->count
] =
1792 os_zalloc(sizeof(*hapd_iface
));
1793 if (hapd_iface
== NULL
) {
1794 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory for "
1795 "the interface", __func__
);
1798 interfaces
->count
++;
1799 hapd_iface
->interfaces
= interfaces
;
1805 static struct hostapd_config
*
1806 hostapd_config_alloc(struct hapd_interfaces
*interfaces
, const char *ifname
,
1807 const char *ctrl_iface
)
1809 struct hostapd_bss_config
*bss
;
1810 struct hostapd_config
*conf
;
1812 /* Allocates memory for bss and conf */
1813 conf
= hostapd_config_defaults();
1815 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory for "
1816 "configuration", __func__
);
1820 conf
->driver
= wpa_drivers
[0];
1821 if (conf
->driver
== NULL
) {
1822 wpa_printf(MSG_ERROR
, "No driver wrappers registered!");
1823 hostapd_config_free(conf
);
1827 bss
= conf
->last_bss
= conf
->bss
[0];
1829 os_strlcpy(bss
->iface
, ifname
, sizeof(bss
->iface
));
1830 bss
->ctrl_interface
= os_strdup(ctrl_iface
);
1831 if (bss
->ctrl_interface
== NULL
) {
1832 hostapd_config_free(conf
);
1836 /* Reading configuration file skipped, will be done in SET!
1837 * From reading the configuration till the end has to be done in
1844 static struct hostapd_iface
* hostapd_data_alloc(
1845 struct hapd_interfaces
*interfaces
, struct hostapd_config
*conf
)
1848 struct hostapd_iface
*hapd_iface
=
1849 interfaces
->iface
[interfaces
->count
- 1];
1850 struct hostapd_data
*hapd
;
1852 hapd_iface
->conf
= conf
;
1853 hapd_iface
->num_bss
= conf
->num_bss
;
1855 hapd_iface
->bss
= os_zalloc(conf
->num_bss
*
1856 sizeof(struct hostapd_data
*));
1857 if (hapd_iface
->bss
== NULL
)
1860 for (i
= 0; i
< conf
->num_bss
; i
++) {
1861 hapd
= hapd_iface
->bss
[i
] =
1862 hostapd_alloc_bss_data(hapd_iface
, conf
, conf
->bss
[i
]);
1865 hapd
->msg_ctx
= hapd
;
1868 hapd_iface
->interfaces
= interfaces
;
1874 int hostapd_add_iface(struct hapd_interfaces
*interfaces
, char *buf
)
1876 struct hostapd_config
*conf
= NULL
;
1877 struct hostapd_iface
*hapd_iface
= NULL
, *new_iface
= NULL
;
1878 struct hostapd_data
*hapd
;
1881 const char *conf_file
= NULL
, *phy_name
= NULL
;
1883 if (os_strncmp(buf
, "bss_config=", 11) == 0) {
1885 phy_name
= buf
+ 11;
1886 pos
= os_strchr(phy_name
, ':');
1891 if (!os_strlen(conf_file
))
1894 hapd_iface
= hostapd_interface_init_bss(interfaces
, phy_name
,
1898 for (j
= 0; j
< interfaces
->count
; j
++) {
1899 if (interfaces
->iface
[j
] == hapd_iface
)
1902 if (j
== interfaces
->count
) {
1903 struct hostapd_iface
**tmp
;
1904 tmp
= os_realloc_array(interfaces
->iface
,
1905 interfaces
->count
+ 1,
1906 sizeof(struct hostapd_iface
*));
1908 hostapd_interface_deinit_free(hapd_iface
);
1911 interfaces
->iface
= tmp
;
1912 interfaces
->iface
[interfaces
->count
++] = hapd_iface
;
1913 new_iface
= hapd_iface
;
1917 if (interfaces
->driver_init(hapd_iface
) ||
1918 hostapd_setup_interface(hapd_iface
)) {
1919 interfaces
->count
--;
1923 /* Assign new BSS with bss[0]'s driver info */
1924 hapd
= hapd_iface
->bss
[hapd_iface
->num_bss
- 1];
1925 hapd
->driver
= hapd_iface
->bss
[0]->driver
;
1926 hapd
->drv_priv
= hapd_iface
->bss
[0]->drv_priv
;
1927 os_memcpy(hapd
->own_addr
, hapd_iface
->bss
[0]->own_addr
,
1930 if (start_ctrl_iface_bss(hapd
) < 0 ||
1931 (hapd_iface
->state
== HAPD_IFACE_ENABLED
&&
1932 hostapd_setup_bss(hapd
, -1))) {
1933 hostapd_cleanup(hapd
);
1934 hapd_iface
->bss
[hapd_iface
->num_bss
- 1] = NULL
;
1935 hapd_iface
->conf
->num_bss
--;
1936 hapd_iface
->num_bss
--;
1937 wpa_printf(MSG_DEBUG
, "%s: free hapd %p %s",
1938 __func__
, hapd
, hapd
->conf
->iface
);
1946 ptr
= os_strchr(buf
, ' ');
1951 if (os_strncmp(ptr
, "config=", 7) == 0)
1952 conf_file
= ptr
+ 7;
1954 for (i
= 0; i
< interfaces
->count
; i
++) {
1955 if (!os_strcmp(interfaces
->iface
[i
]->conf
->bss
[0]->iface
,
1957 wpa_printf(MSG_INFO
, "Cannot add interface - it "
1963 hapd_iface
= hostapd_iface_alloc(interfaces
);
1964 if (hapd_iface
== NULL
) {
1965 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory "
1966 "for interface", __func__
);
1970 if (conf_file
&& interfaces
->config_read_cb
) {
1971 conf
= interfaces
->config_read_cb(conf_file
);
1972 if (conf
&& conf
->bss
)
1973 os_strlcpy(conf
->bss
[0]->iface
, buf
,
1974 sizeof(conf
->bss
[0]->iface
));
1976 conf
= hostapd_config_alloc(interfaces
, buf
, ptr
);
1977 if (conf
== NULL
|| conf
->bss
== NULL
) {
1978 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory "
1979 "for configuration", __func__
);
1983 hapd_iface
= hostapd_data_alloc(interfaces
, conf
);
1984 if (hapd_iface
== NULL
) {
1985 wpa_printf(MSG_ERROR
, "%s: Failed to allocate memory "
1986 "for hostapd", __func__
);
1990 if (start_ctrl_iface(hapd_iface
) < 0)
1993 wpa_printf(MSG_INFO
, "Add interface '%s'", conf
->bss
[0]->iface
);
1999 hostapd_config_free(conf
);
2001 if (hapd_iface
->bss
) {
2002 for (i
= 0; i
< hapd_iface
->num_bss
; i
++) {
2003 hapd
= hapd_iface
->bss
[i
];
2006 if (hapd_iface
->interfaces
&&
2007 hapd_iface
->interfaces
->ctrl_iface_deinit
)
2008 hapd_iface
->interfaces
->
2009 ctrl_iface_deinit(hapd
);
2010 wpa_printf(MSG_DEBUG
, "%s: free hapd %p (%s)",
2011 __func__
, hapd_iface
->bss
[i
],
2014 hapd_iface
->bss
[i
] = NULL
;
2016 os_free(hapd_iface
->bss
);
2018 wpa_printf(MSG_DEBUG
, "%s: free iface %p",
2019 __func__
, hapd_iface
);
2020 os_free(hapd_iface
);
2026 static int hostapd_remove_bss(struct hostapd_iface
*iface
, unsigned int idx
)
2030 wpa_printf(MSG_INFO
, "Remove BSS '%s'", iface
->conf
->bss
[idx
]->iface
);
2032 /* Remove hostapd_data only if it has already been initialized */
2033 if (idx
< iface
->num_bss
) {
2034 struct hostapd_data
*hapd
= iface
->bss
[idx
];
2036 hostapd_bss_deinit(hapd
);
2037 wpa_printf(MSG_DEBUG
, "%s: free hapd %p (%s)",
2038 __func__
, hapd
, hapd
->conf
->iface
);
2039 hostapd_config_free_bss(hapd
->conf
);
2044 for (i
= idx
; i
< iface
->num_bss
; i
++)
2045 iface
->bss
[i
] = iface
->bss
[i
+ 1];
2047 hostapd_config_free_bss(iface
->conf
->bss
[idx
]);
2048 iface
->conf
->bss
[idx
] = NULL
;
2051 iface
->conf
->num_bss
--;
2052 for (i
= idx
; i
< iface
->conf
->num_bss
; i
++)
2053 iface
->conf
->bss
[i
] = iface
->conf
->bss
[i
+ 1];
2059 int hostapd_remove_iface(struct hapd_interfaces
*interfaces
, char *buf
)
2061 struct hostapd_iface
*hapd_iface
;
2064 for (i
= 0; i
< interfaces
->count
; i
++) {
2065 hapd_iface
= interfaces
->iface
[i
];
2066 if (hapd_iface
== NULL
)
2068 if (!os_strcmp(hapd_iface
->conf
->bss
[0]->iface
, buf
)) {
2069 wpa_printf(MSG_INFO
, "Remove interface '%s'", buf
);
2070 hapd_iface
->driver_ap_teardown
=
2071 !!(hapd_iface
->drv_flags
&
2072 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT
);
2074 hostapd_interface_deinit_free(hapd_iface
);
2076 while (k
< (interfaces
->count
- 1)) {
2077 interfaces
->iface
[k
] =
2078 interfaces
->iface
[k
+ 1];
2081 interfaces
->count
--;
2085 for (j
= 0; j
< hapd_iface
->conf
->num_bss
; j
++) {
2086 if (!os_strcmp(hapd_iface
->conf
->bss
[j
]->iface
, buf
)) {
2087 hapd_iface
->driver_ap_teardown
=
2088 !(hapd_iface
->drv_flags
&
2089 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT
);
2090 return hostapd_remove_bss(hapd_iface
, j
);
2099 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2100 * @hapd: Pointer to BSS data
2101 * @sta: Pointer to the associated STA data
2102 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2104 * This function will be called whenever a station associates with the AP. It
2105 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2106 * from drv_callbacks.c based on driver events for drivers that take care of
2107 * management frames (IEEE 802.11 authentication and association) internally.
2109 void hostapd_new_assoc_sta(struct hostapd_data
*hapd
, struct sta_info
*sta
,
2112 if (hapd
->tkip_countermeasures
) {
2113 hostapd_drv_sta_deauth(hapd
, sta
->addr
,
2114 WLAN_REASON_MICHAEL_MIC_FAILURE
);
2118 hostapd_prune_associations(hapd
, sta
->addr
);
2120 /* IEEE 802.11F (IAPP) */
2121 if (hapd
->conf
->ieee802_11f
)
2122 iapp_new_station(hapd
->iapp
, sta
);
2125 if (sta
->p2p_ie
== NULL
&& !sta
->no_p2p_set
) {
2126 sta
->no_p2p_set
= 1;
2127 hapd
->num_sta_no_p2p
++;
2128 if (hapd
->num_sta_no_p2p
== 1)
2129 hostapd_p2p_non_p2p_sta_connected(hapd
);
2131 #endif /* CONFIG_P2P */
2133 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2134 * IEEE 802.1X/WPA code will start accounting after the station has
2135 * been authorized. */
2136 if (!hapd
->conf
->ieee802_1x
&& !hapd
->conf
->wpa
&& !hapd
->conf
->osen
) {
2137 ap_sta_set_authorized(hapd
, sta
, 1);
2138 os_get_reltime(&sta
->connected_time
);
2139 accounting_sta_start(hapd
, sta
);
2142 /* Start IEEE 802.1X authentication process for new stations */
2143 ieee802_1x_new_station(hapd
, sta
);
2145 if (sta
->auth_alg
!= WLAN_AUTH_FT
&&
2146 !(sta
->flags
& (WLAN_STA_WPS
| WLAN_STA_MAYBE_WPS
)))
2147 wpa_auth_sm_event(sta
->wpa_sm
, WPA_REAUTH
);
2149 wpa_auth_sta_associated(hapd
->wpa_auth
, sta
->wpa_sm
);
2151 if (!(hapd
->iface
->drv_flags
& WPA_DRIVER_FLAGS_INACTIVITY_TIMER
)) {
2152 wpa_printf(MSG_DEBUG
, "%s: reschedule ap_handle_timer timeout "
2153 "for " MACSTR
" (%d seconds - ap_max_inactivity)",
2154 __func__
, MAC2STR(sta
->addr
),
2155 hapd
->conf
->ap_max_inactivity
);
2156 eloop_cancel_timeout(ap_handle_timer
, hapd
, sta
);
2157 eloop_register_timeout(hapd
->conf
->ap_max_inactivity
, 0,
2158 ap_handle_timer
, hapd
, sta
);
2163 const char * hostapd_state_text(enum hostapd_iface_state s
)
2166 case HAPD_IFACE_UNINITIALIZED
:
2167 return "UNINITIALIZED";
2168 case HAPD_IFACE_DISABLED
:
2170 case HAPD_IFACE_COUNTRY_UPDATE
:
2171 return "COUNTRY_UPDATE";
2172 case HAPD_IFACE_ACS
:
2174 case HAPD_IFACE_HT_SCAN
:
2176 case HAPD_IFACE_DFS
:
2178 case HAPD_IFACE_ENABLED
:
2186 void hostapd_set_state(struct hostapd_iface
*iface
, enum hostapd_iface_state s
)
2188 wpa_printf(MSG_INFO
, "%s: interface state %s->%s",
2189 iface
->conf
->bss
[0]->iface
, hostapd_state_text(iface
->state
),
2190 hostapd_state_text(s
));
2197 static void free_beacon_data(struct beacon_data
*beacon
)
2199 os_free(beacon
->head
);
2200 beacon
->head
= NULL
;
2201 os_free(beacon
->tail
);
2202 beacon
->tail
= NULL
;
2203 os_free(beacon
->probe_resp
);
2204 beacon
->probe_resp
= NULL
;
2205 os_free(beacon
->beacon_ies
);
2206 beacon
->beacon_ies
= NULL
;
2207 os_free(beacon
->proberesp_ies
);
2208 beacon
->proberesp_ies
= NULL
;
2209 os_free(beacon
->assocresp_ies
);
2210 beacon
->assocresp_ies
= NULL
;
2214 static int hostapd_build_beacon_data(struct hostapd_data
*hapd
,
2215 struct beacon_data
*beacon
)
2217 struct wpabuf
*beacon_extra
, *proberesp_extra
, *assocresp_extra
;
2218 struct wpa_driver_ap_params params
;
2221 os_memset(beacon
, 0, sizeof(*beacon
));
2222 ret
= ieee802_11_build_ap_params(hapd
, ¶ms
);
2226 ret
= hostapd_build_ap_extra_ies(hapd
, &beacon_extra
,
2230 goto free_ap_params
;
2233 beacon
->head
= os_malloc(params
.head_len
);
2235 goto free_ap_extra_ies
;
2237 os_memcpy(beacon
->head
, params
.head
, params
.head_len
);
2238 beacon
->head_len
= params
.head_len
;
2240 beacon
->tail
= os_malloc(params
.tail_len
);
2244 os_memcpy(beacon
->tail
, params
.tail
, params
.tail_len
);
2245 beacon
->tail_len
= params
.tail_len
;
2247 if (params
.proberesp
!= NULL
) {
2248 beacon
->probe_resp
= os_malloc(params
.proberesp_len
);
2249 if (!beacon
->probe_resp
)
2252 os_memcpy(beacon
->probe_resp
, params
.proberesp
,
2253 params
.proberesp_len
);
2254 beacon
->probe_resp_len
= params
.proberesp_len
;
2257 /* copy the extra ies */
2259 beacon
->beacon_ies
= os_malloc(wpabuf_len(beacon_extra
));
2260 if (!beacon
->beacon_ies
)
2263 os_memcpy(beacon
->beacon_ies
,
2264 beacon_extra
->buf
, wpabuf_len(beacon_extra
));
2265 beacon
->beacon_ies_len
= wpabuf_len(beacon_extra
);
2268 if (proberesp_extra
) {
2269 beacon
->proberesp_ies
=
2270 os_malloc(wpabuf_len(proberesp_extra
));
2271 if (!beacon
->proberesp_ies
)
2274 os_memcpy(beacon
->proberesp_ies
, proberesp_extra
->buf
,
2275 wpabuf_len(proberesp_extra
));
2276 beacon
->proberesp_ies_len
= wpabuf_len(proberesp_extra
);
2279 if (assocresp_extra
) {
2280 beacon
->assocresp_ies
=
2281 os_malloc(wpabuf_len(assocresp_extra
));
2282 if (!beacon
->assocresp_ies
)
2285 os_memcpy(beacon
->assocresp_ies
, assocresp_extra
->buf
,
2286 wpabuf_len(assocresp_extra
));
2287 beacon
->assocresp_ies_len
= wpabuf_len(assocresp_extra
);
2292 /* if the function fails, the caller should not free beacon data */
2294 free_beacon_data(beacon
);
2297 hostapd_free_ap_extra_ies(hapd
, beacon_extra
, proberesp_extra
,
2300 ieee802_11_free_ap_params(¶ms
);
2306 * TODO: This flow currently supports only changing frequency within the
2307 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2308 * width) are not supported.
2310 static int hostapd_change_config_freq(struct hostapd_data
*hapd
,
2311 struct hostapd_config
*conf
,
2312 struct hostapd_freq_params
*params
,
2313 struct hostapd_freq_params
*old_params
)
2317 if (!params
->channel
) {
2318 /* check if the new channel is supported by hw */
2319 params
->channel
= hostapd_hw_get_channel(hapd
, params
->freq
);
2322 channel
= params
->channel
;
2326 /* if a pointer to old_params is provided we save previous state */
2328 old_params
->channel
= conf
->channel
;
2329 old_params
->ht_enabled
= conf
->ieee80211n
;
2330 old_params
->sec_channel_offset
= conf
->secondary_channel
;
2333 conf
->channel
= channel
;
2334 conf
->ieee80211n
= params
->ht_enabled
;
2335 conf
->secondary_channel
= params
->sec_channel_offset
;
2337 /* TODO: maybe call here hostapd_config_check here? */
2343 static int hostapd_fill_csa_settings(struct hostapd_data
*hapd
,
2344 struct csa_settings
*settings
)
2346 struct hostapd_iface
*iface
= hapd
->iface
;
2347 struct hostapd_freq_params old_freq
;
2350 os_memset(&old_freq
, 0, sizeof(old_freq
));
2351 if (!iface
|| !iface
->freq
|| hapd
->csa_in_progress
)
2354 ret
= hostapd_change_config_freq(iface
->bss
[0], iface
->conf
,
2355 &settings
->freq_params
,
2360 ret
= hostapd_build_beacon_data(hapd
, &settings
->beacon_after
);
2362 /* change back the configuration */
2363 hostapd_change_config_freq(iface
->bss
[0], iface
->conf
,
2369 /* set channel switch parameters for csa ie */
2370 hapd
->cs_freq_params
= settings
->freq_params
;
2371 hapd
->cs_count
= settings
->cs_count
;
2372 hapd
->cs_block_tx
= settings
->block_tx
;
2374 ret
= hostapd_build_beacon_data(hapd
, &settings
->beacon_csa
);
2376 free_beacon_data(&settings
->beacon_after
);
2380 settings
->counter_offset_beacon
= hapd
->cs_c_off_beacon
;
2381 settings
->counter_offset_presp
= hapd
->cs_c_off_proberesp
;
2387 void hostapd_cleanup_cs_params(struct hostapd_data
*hapd
)
2389 os_memset(&hapd
->cs_freq_params
, 0, sizeof(hapd
->cs_freq_params
));
2391 hapd
->cs_block_tx
= 0;
2392 hapd
->cs_c_off_beacon
= 0;
2393 hapd
->cs_c_off_proberesp
= 0;
2394 hapd
->csa_in_progress
= 0;
2398 int hostapd_switch_channel(struct hostapd_data
*hapd
,
2399 struct csa_settings
*settings
)
2403 if (!(hapd
->iface
->drv_flags
& WPA_DRIVER_FLAGS_AP_CSA
)) {
2404 wpa_printf(MSG_INFO
, "CSA is not supported");
2408 ret
= hostapd_fill_csa_settings(hapd
, settings
);
2412 ret
= hostapd_drv_switch_channel(hapd
, settings
);
2413 free_beacon_data(&settings
->beacon_csa
);
2414 free_beacon_data(&settings
->beacon_after
);
2417 /* if we failed, clean cs parameters */
2418 hostapd_cleanup_cs_params(hapd
);
2422 hapd
->csa_in_progress
= 1;
2428 hostapd_switch_channel_fallback(struct hostapd_iface
*iface
,
2429 const struct hostapd_freq_params
*freq_params
)
2431 int vht_seg0_idx
= 0, vht_seg1_idx
= 0, vht_bw
= VHT_CHANWIDTH_USE_HT
;
2434 wpa_printf(MSG_DEBUG
, "Restarting all CSA-related BSSes");
2436 if (freq_params
->center_freq1
)
2437 vht_seg0_idx
= 36 + (freq_params
->center_freq1
- 5180) / 5;
2438 if (freq_params
->center_freq2
)
2439 vht_seg1_idx
= 36 + (freq_params
->center_freq2
- 5180) / 5;
2441 switch (freq_params
->bandwidth
) {
2445 vht_bw
= VHT_CHANWIDTH_USE_HT
;
2448 if (freq_params
->center_freq2
)
2449 vht_bw
= VHT_CHANWIDTH_80P80MHZ
;
2451 vht_bw
= VHT_CHANWIDTH_80MHZ
;
2454 vht_bw
= VHT_CHANWIDTH_160MHZ
;
2457 wpa_printf(MSG_WARNING
, "Unknown CSA bandwidth: %d",
2458 freq_params
->bandwidth
);
2462 iface
->freq
= freq_params
->freq
;
2463 iface
->conf
->channel
= freq_params
->channel
;
2464 iface
->conf
->secondary_channel
= freq_params
->sec_channel_offset
;
2465 iface
->conf
->vht_oper_centr_freq_seg0_idx
= vht_seg0_idx
;
2466 iface
->conf
->vht_oper_centr_freq_seg1_idx
= vht_seg1_idx
;
2467 iface
->conf
->vht_oper_chwidth
= vht_bw
;
2468 iface
->conf
->ieee80211n
= freq_params
->ht_enabled
;
2469 iface
->conf
->ieee80211ac
= freq_params
->vht_enabled
;
2472 * cs_params must not be cleared earlier because the freq_params
2473 * argument may actually point to one of these.
2475 for (i
= 0; i
< iface
->num_bss
; i
++)
2476 hostapd_cleanup_cs_params(iface
->bss
[i
]);
2478 hostapd_disable_iface(iface
);
2479 hostapd_enable_iface(iface
);
2482 #endif /* NEED_AP_MLME */