]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/hostapd.c
FT: Replace inter-AP protocol with use of OUI Extended Ethertype
[thirdparty/hostap.git] / src / ap / hostapd.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / Initialization and configuration
08081ad8 3 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
6226e38d 9#include "utils/includes.h"
6fc6879b 10
6226e38d
JM
11#include "utils/common.h"
12#include "utils/eloop.h"
03da66bd 13#include "common/ieee802_11_defs.h"
7d6d7370 14#include "common/wpa_ctrl.h"
982896ff 15#include "common/hw_features_common.h"
03da66bd 16#include "radius/radius_client.h"
b031338c 17#include "radius/radius_das.h"
10b58b50 18#include "eap_server/tncs.h"
4e871ed1
JM
19#include "eapol_auth/eapol_auth_sm.h"
20#include "eapol_auth/eapol_auth_sm_i.h"
6959145b 21#include "fst/fst.h"
6226e38d
JM
22#include "hostapd.h"
23#include "authsrv.h"
24#include "sta_info.h"
25#include "accounting.h"
26#include "ap_list.h"
27#include "beacon.h"
28#include "iapp.h"
29#include "ieee802_1x.h"
30#include "ieee802_11_auth.h"
31#include "vlan_init.h"
32#include "wpa_auth.h"
33#include "wps_hostapd.h"
6fc6879b 34#include "hw_features.h"
c442055e 35#include "wpa_auth_glue.h"
a4f21109 36#include "ap_drv_ops.h"
8b06c1ed 37#include "ap_config.h"
aefb53bd 38#include "p2p_hostapd.h"
dca30c3f 39#include "gas_serv.h"
e76da505 40#include "dfs.h"
9c47f6a2 41#include "ieee802_11.h"
ec8f36af 42#include "bss_load.h"
1d783762 43#include "x_snoop.h"
7d597d46 44#include "dhcp_snoop.h"
bd00c431 45#include "ndisc_snoop.h"
9b4b2264 46#include "neighbor_db.h"
f4f185a2 47#include "rrm.h"
91d91abf 48#include "fils_hlp.h"
4c803dfc 49#include "acs.h"
6fc6879b
JM
50
51
52b20042 52static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
ad08c363 53static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
a3e685a0 54static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
f0793bf1
JM
55static int setup_interface2(struct hostapd_iface *iface);
56static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
6fc6879b 57
6fc6879b 58
07bcdbb1
JM
59int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
60 int (*cb)(struct hostapd_iface *iface,
61 void *ctx), void *ctx)
62{
63 size_t i;
64 int ret;
65
66 for (i = 0; i < interfaces->count; i++) {
67 ret = cb(interfaces->iface[i], ctx);
68 if (ret)
69 return ret;
70 }
71
72 return 0;
73}
74
75
c2aa25fb 76static void hostapd_reload_bss(struct hostapd_data *hapd)
ad08c363 77{
a781e211
JM
78 struct hostapd_ssid *ssid;
79
1dfd25a6
JM
80 if (!hapd->started)
81 return;
82
e3e52e36
JM
83#ifndef CONFIG_NO_RADIUS
84 radius_client_reconfig(hapd->radius, hapd->conf->radius);
85#endif /* CONFIG_NO_RADIUS */
86
a781e211
JM
87 ssid = &hapd->conf->ssid;
88 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
89 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
90 /*
91 * Force PSK to be derived again since SSID or passphrase may
92 * have changed.
93 */
891dfb33 94 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
a781e211 95 }
ad08c363
JM
96 if (hostapd_setup_wpa_psk(hapd->conf)) {
97 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
98 "after reloading configuration");
99 }
100
84b2f990 101 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
0e8a96a9 102 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
84b2f990 103 else
0e8a96a9 104 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
84b2f990 105
a14896e8 106 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
ad08c363 107 hostapd_setup_wpa(hapd);
bdffdc5d
JM
108 if (hapd->wpa_auth)
109 wpa_init_keys(hapd->wpa_auth);
110 } else if (hapd->conf->wpa) {
99f4ae67
AT
111 const u8 *wpa_ie;
112 size_t wpa_ie_len;
c442055e 113 hostapd_reconfig_wpa(hapd);
99f4ae67
AT
114 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
115 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
116 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
117 "the kernel driver.");
118 } else if (hapd->wpa_auth) {
ad08c363
JM
119 wpa_deinit(hapd->wpa_auth);
120 hapd->wpa_auth = NULL;
121 hostapd_set_privacy(hapd, 0);
122 hostapd_setup_encryption(hapd->conf->iface, hapd);
99f4ae67 123 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
ad08c363
JM
124 }
125
126 ieee802_11_set_beacon(hapd);
6deb41e7 127 hostapd_update_wps(hapd);
ad08c363 128
c813b695 129 if (hapd->conf->ssid.ssid_set &&
986de33d 130 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
c813b695
JM
131 hapd->conf->ssid.ssid_len)) {
132 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
133 /* try to continue */
134 }
c2aa25fb
FF
135 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
136}
137
138
ccdff940 139static void hostapd_clear_old(struct hostapd_iface *iface)
c2aa25fb 140{
c2aa25fb
FF
141 size_t j;
142
c2aa25fb
FF
143 /*
144 * Deauthenticate all stations since the new configuration may not
145 * allow them to use the BSS anymore.
146 */
e3e52e36 147 for (j = 0; j < iface->num_bss; j++) {
52b20042
JM
148 hostapd_flush_old_stations(iface->bss[j],
149 WLAN_REASON_PREV_AUTH_NOT_VALID);
a3e685a0 150 hostapd_broadcast_wep_clear(iface->bss[j]);
c2aa25fb
FF
151
152#ifndef CONFIG_NO_RADIUS
e3e52e36
JM
153 /* TODO: update dynamic data based on changed configuration
154 * items (e.g., open/close sockets, etc.) */
155 radius_client_flush(iface->bss[j]->radius, 0);
c2aa25fb 156#endif /* CONFIG_NO_RADIUS */
e3e52e36 157 }
ccdff940
JM
158}
159
160
161int hostapd_reload_config(struct hostapd_iface *iface)
162{
163 struct hostapd_data *hapd = iface->bss[0];
164 struct hostapd_config *newconf, *oldconf;
165 size_t j;
166
167 if (iface->config_fname == NULL) {
168 /* Only in-memory config in use - assume it has been updated */
169 hostapd_clear_old(iface);
170 for (j = 0; j < iface->num_bss; j++)
171 hostapd_reload_bss(iface->bss[j]);
172 return 0;
173 }
174
175 if (iface->interfaces == NULL ||
176 iface->interfaces->config_read_cb == NULL)
177 return -1;
178 newconf = iface->interfaces->config_read_cb(iface->config_fname);
179 if (newconf == NULL)
180 return -1;
181
182 hostapd_clear_old(iface);
c2aa25fb
FF
183
184 oldconf = hapd->iconf;
185 iface->conf = newconf;
186
187 for (j = 0; j < iface->num_bss; j++) {
188 hapd = iface->bss[j];
189 hapd->iconf = newconf;
513dcec6 190 hapd->iconf->channel = oldconf->channel;
857d9422 191 hapd->iconf->acs = oldconf->acs;
74a1319e 192 hapd->iconf->secondary_channel = oldconf->secondary_channel;
513dcec6
PK
193 hapd->iconf->ieee80211n = oldconf->ieee80211n;
194 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
195 hapd->iconf->ht_capab = oldconf->ht_capab;
196 hapd->iconf->vht_capab = oldconf->vht_capab;
197 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
198 hapd->iconf->vht_oper_centr_freq_seg0_idx =
199 oldconf->vht_oper_centr_freq_seg0_idx;
200 hapd->iconf->vht_oper_centr_freq_seg1_idx =
201 oldconf->vht_oper_centr_freq_seg1_idx;
ebd79f07 202 hapd->conf = newconf->bss[j];
c2aa25fb
FF
203 hostapd_reload_bss(hapd);
204 }
c813b695 205
ad08c363
JM
206 hostapd_config_free(oldconf);
207
ad08c363
JM
208
209 return 0;
210}
211
212
6fc6879b 213static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
9c10be3f 214 const char *ifname)
6fc6879b
JM
215{
216 int i;
217
1dfd25a6 218 if (!ifname || !hapd->drv_priv)
9c10be3f 219 return;
6fc6879b 220 for (i = 0; i < NUM_WEP_KEYS; i++) {
3acdf771 221 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
dff99f8e 222 0, NULL, 0, NULL, 0)) {
bb305cbd
JM
223 wpa_printf(MSG_DEBUG, "Failed to clear default "
224 "encryption keys (ifname=%s keyidx=%d)",
225 ifname, i);
6fc6879b
JM
226 }
227 }
1aa5c134
JM
228#ifdef CONFIG_IEEE80211W
229 if (hapd->conf->ieee80211w) {
230 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
3acdf771 231 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
dff99f8e 232 NULL, i, 0, NULL,
3acdf771 233 0, NULL, 0)) {
bb305cbd
JM
234 wpa_printf(MSG_DEBUG, "Failed to clear "
235 "default mgmt encryption keys "
236 "(ifname=%s keyidx=%d)", ifname, i);
1aa5c134
JM
237 }
238 }
239 }
240#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
241}
242
243
244static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
245{
246 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
247 return 0;
248}
249
250
251static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
252{
253 int errors = 0, idx;
254 struct hostapd_ssid *ssid = &hapd->conf->ssid;
255
256 idx = ssid->wep.idx;
257 if (ssid->wep.default_len &&
3acdf771 258 hostapd_drv_set_key(hapd->conf->iface,
0382097e
JM
259 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
260 1, NULL, 0, ssid->wep.key[idx],
3acdf771 261 ssid->wep.len[idx])) {
bb305cbd 262 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
6fc6879b
JM
263 errors++;
264 }
265
6fc6879b
JM
266 return errors;
267}
268
6fc6879b 269
ed53dec0
SP
270static void hostapd_free_hapd_data(struct hostapd_data *hapd)
271{
eae3df7e
JM
272 os_free(hapd->probereq_cb);
273 hapd->probereq_cb = NULL;
24fd2043 274 hapd->num_probereq_cb = 0;
eae3df7e
JM
275
276#ifdef CONFIG_P2P
277 wpabuf_free(hapd->p2p_beacon_ie);
278 hapd->p2p_beacon_ie = NULL;
279 wpabuf_free(hapd->p2p_probe_resp_ie);
280 hapd->p2p_probe_resp_ie = NULL;
281#endif /* CONFIG_P2P */
282
68d628ac
MK
283 if (!hapd->started) {
284 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
285 __func__, hapd->conf->iface);
286 return;
287 }
288 hapd->started = 0;
289
747c85f9 290 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
6fc6879b
JM
291 iapp_deinit(hapd->iapp);
292 hapd->iapp = NULL;
293 accounting_deinit(hapd);
c442055e 294 hostapd_deinit_wpa(hapd);
6fc6879b
JM
295 vlan_deinit(hapd);
296 hostapd_acl_deinit(hapd);
74784010 297#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
298 radius_client_deinit(hapd->radius);
299 hapd->radius = NULL;
b031338c
JM
300 radius_das_deinit(hapd->radius_das);
301 hapd->radius_das = NULL;
74784010 302#endif /* CONFIG_NO_RADIUS */
6fc6879b 303
ad08c363
JM
304 hostapd_deinit_wps(hapd);
305
2586bc64 306 authsrv_deinit(hapd);
6fc6879b 307
81c4fca1
MK
308 if (hapd->interface_added) {
309 hapd->interface_added = 0;
310 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
311 wpa_printf(MSG_WARNING,
312 "Failed to remove BSS interface %s",
313 hapd->conf->iface);
314 hapd->interface_added = 1;
d92bdf96
JM
315 } else {
316 /*
317 * Since this was a dynamically added interface, the
318 * driver wrapper may have removed its internal instance
319 * and hapd->drv_priv is not valid anymore.
320 */
321 hapd->drv_priv = NULL;
81c4fca1 322 }
6fc6879b 323 }
fa16028d 324
39b97072 325 wpabuf_free(hapd->time_adv);
dca30c3f
JK
326
327#ifdef CONFIG_INTERWORKING
328 gas_serv_deinit(hapd);
329#endif /* CONFIG_INTERWORKING */
ee431d77 330
ec8f36af 331 bss_load_update_deinit(hapd);
bd00c431 332 ndisc_snoop_deinit(hapd);
7d597d46 333 dhcp_snoop_deinit(hapd);
1d783762 334 x_snoop_deinit(hapd);
ec8f36af 335
ee431d77 336#ifdef CONFIG_SQLITE
b7175b4d
JM
337 bin_clear_free(hapd->tmp_eap_user.identity,
338 hapd->tmp_eap_user.identity_len);
339 bin_clear_free(hapd->tmp_eap_user.password,
340 hapd->tmp_eap_user.password_len);
ee431d77 341#endif /* CONFIG_SQLITE */
3a322496
JM
342
343#ifdef CONFIG_MESH
344 wpabuf_free(hapd->mesh_pending_auth);
345 hapd->mesh_pending_auth = NULL;
346#endif /* CONFIG_MESH */
9b4b2264 347
f4f185a2 348 hostapd_clean_rrm(hapd);
91d91abf 349 fils_hlp_deinit(hapd);
6fc6879b
JM
350}
351
352
ed53dec0
SP
353/**
354 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
355 * @hapd: Pointer to BSS data
356 *
357 * This function is used to free all per-BSS data structures and resources.
6d1ca81e
JM
358 * Most of the modules that are initialized in hostapd_setup_bss() are
359 * deinitialized here.
ed53dec0
SP
360 */
361static void hostapd_cleanup(struct hostapd_data *hapd)
362{
36501a22
JM
363 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
364 hapd->conf->iface);
3776ac73 365 if (hapd->iface->interfaces &&
655dc4a4
DN
366 hapd->iface->interfaces->ctrl_iface_deinit) {
367 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
3776ac73 368 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
655dc4a4 369 }
ed53dec0
SP
370 hostapd_free_hapd_data(hapd);
371}
372
373
a65a9b8d
JM
374static void sta_track_deinit(struct hostapd_iface *iface)
375{
376 struct hostapd_sta_info *info;
377
378 if (!iface->num_sta_seen)
379 return;
380
381 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
382 list))) {
383 dl_list_del(&info->list);
384 iface->num_sta_seen--;
44281940 385 sta_track_del(info);
a65a9b8d
JM
386 }
387}
388
389
4b8a59e4
SP
390static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
391{
747c85f9 392 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
4fada121
JM
393#ifdef CONFIG_IEEE80211N
394#ifdef NEED_AP_MLME
395 hostapd_stop_setup_timers(iface);
396#endif /* NEED_AP_MLME */
397#endif /* CONFIG_IEEE80211N */
4c803dfc
JM
398 if (iface->current_mode)
399 acs_cleanup(iface);
4b8a59e4
SP
400 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
401 iface->hw_features = NULL;
4c803dfc 402 iface->current_mode = NULL;
4b8a59e4
SP
403 os_free(iface->current_rates);
404 iface->current_rates = NULL;
405 os_free(iface->basic_rates);
406 iface->basic_rates = NULL;
407 ap_list_deinit(iface);
a65a9b8d 408 sta_track_deinit(iface);
4b8a59e4
SP
409}
410
411
6fc6879b
JM
412/**
413 * hostapd_cleanup_iface - Complete per-interface cleanup
414 * @iface: Pointer to interface data
415 *
416 * This function is called after per-BSS data structures are deinitialized
417 * with hostapd_cleanup().
418 */
419static void hostapd_cleanup_iface(struct hostapd_iface *iface)
420{
747c85f9 421 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
f0793bf1
JM
422 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
423
4b8a59e4 424 hostapd_cleanup_iface_partial(iface);
6fc6879b
JM
425 hostapd_config_free(iface->conf);
426 iface->conf = NULL;
427
428 os_free(iface->config_fname);
429 os_free(iface->bss);
747c85f9 430 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
6fc6879b
JM
431 os_free(iface);
432}
433
434
e03c3069
SP
435static void hostapd_clear_wep(struct hostapd_data *hapd)
436{
354c903f 437 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
e03c3069
SP
438 hostapd_set_privacy(hapd, 0);
439 hostapd_broadcast_wep_clear(hapd);
440 }
441}
442
443
6fc6879b
JM
444static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
445{
446 int i;
447
448 hostapd_broadcast_wep_set(hapd);
449
579bc0e6
JM
450 if (hapd->conf->ssid.wep.default_len) {
451 hostapd_set_privacy(hapd, 1);
6fc6879b 452 return 0;
579bc0e6 453 }
6fc6879b 454
41fd1d9e
KZ
455 /*
456 * When IEEE 802.1X is not enabled, the driver may need to know how to
457 * set authentication algorithms for static WEP.
458 */
459 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
460
6fc6879b
JM
461 for (i = 0; i < 4; i++) {
462 if (hapd->conf->ssid.wep.key[i] &&
3acdf771
JM
463 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
464 i == hapd->conf->ssid.wep.idx, NULL, 0,
465 hapd->conf->ssid.wep.key[i],
466 hapd->conf->ssid.wep.len[i])) {
bb305cbd
JM
467 wpa_printf(MSG_WARNING, "Could not set WEP "
468 "encryption.");
6fc6879b
JM
469 return -1;
470 }
471 if (hapd->conf->ssid.wep.key[i] &&
472 i == hapd->conf->ssid.wep.idx)
473 hostapd_set_privacy(hapd, 1);
474 }
475
476 return 0;
477}
478
479
52b20042 480static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
6fc6879b
JM
481{
482 int ret = 0;
4d379f12 483 u8 addr[ETH_ALEN];
6fc6879b 484
70a8419f 485 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
85141289
JM
486 return 0;
487
354c903f
MB
488 if (!hapd->iface->driver_ap_teardown) {
489 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
490 "Flushing old station entries");
491
492 if (hostapd_flush(hapd)) {
493 wpa_msg(hapd->msg_ctx, MSG_WARNING,
494 "Could not connect to kernel driver");
495 ret = -1;
496 }
6fc6879b 497 }
57a2aaca
JM
498 if (hapd->conf && hapd->conf->broadcast_deauth) {
499 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
500 "Deauthenticate all stations");
501 os_memset(addr, 0xff, ETH_ALEN);
502 hostapd_drv_sta_deauth(hapd, addr, reason);
503 }
6603a966 504 hostapd_free_stas(hapd);
6fc6879b
JM
505
506 return ret;
507}
508
509
438e1333
JM
510static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
511{
512 hostapd_free_stas(hapd);
513 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
514 hostapd_clear_wep(hapd);
515}
516
517
6fc6879b
JM
518/**
519 * hostapd_validate_bssid_configuration - Validate BSSID configuration
520 * @iface: Pointer to interface data
521 * Returns: 0 on success, -1 on failure
522 *
523 * This function is used to validate that the configured BSSIDs are valid.
524 */
525static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
526{
527 u8 mask[ETH_ALEN] = { 0 };
528 struct hostapd_data *hapd = iface->bss[0];
529 unsigned int i = iface->conf->num_bss, bits = 0, j;
90ac1f9f 530 int auto_addr = 0;
6fc6879b 531
85141289
JM
532 if (hostapd_drv_none(hapd))
533 return 0;
534
6448e064
EP
535 if (iface->conf->use_driver_iface_addr)
536 return 0;
537
6fc6879b
JM
538 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
539
540 /* Determine the bits necessary to cover the number of BSSIDs. */
541 for (i--; i; i >>= 1)
542 bits++;
543
544 /* Determine the bits necessary to any configured BSSIDs,
545 if they are higher than the number of BSSIDs. */
546 for (j = 0; j < iface->conf->num_bss; j++) {
902c07a7 547 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
90ac1f9f
JM
548 if (j)
549 auto_addr++;
6fc6879b 550 continue;
90ac1f9f 551 }
6fc6879b
JM
552
553 for (i = 0; i < ETH_ALEN; i++) {
554 mask[i] |=
ebd79f07 555 iface->conf->bss[j]->bssid[i] ^
6fc6879b
JM
556 hapd->own_addr[i];
557 }
558 }
559
90ac1f9f
JM
560 if (!auto_addr)
561 goto skip_mask_ext;
562
6fc6879b
JM
563 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
564 ;
565 j = 0;
566 if (i < ETH_ALEN) {
567 j = (5 - i) * 8;
568
569 while (mask[i] != 0) {
570 mask[i] >>= 1;
571 j++;
572 }
573 }
574
575 if (bits < j)
576 bits = j;
577
90ac1f9f
JM
578 if (bits > 40) {
579 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
580 bits);
6fc6879b 581 return -1;
90ac1f9f 582 }
6fc6879b
JM
583
584 os_memset(mask, 0xff, ETH_ALEN);
585 j = bits / 8;
586 for (i = 5; i > 5 - j; i--)
587 mask[i] = 0;
588 j = bits % 8;
589 while (j--)
590 mask[i] <<= 1;
591
90ac1f9f 592skip_mask_ext:
6fc6879b
JM
593 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
594 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
595
90ac1f9f
JM
596 if (!auto_addr)
597 return 0;
598
6fc6879b
JM
599 for (i = 0; i < ETH_ALEN; i++) {
600 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
bb305cbd
JM
601 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
602 " for start address " MACSTR ".",
603 MAC2STR(mask), MAC2STR(hapd->own_addr));
604 wpa_printf(MSG_ERROR, "Start address must be the "
605 "first address in the block (i.e., addr "
606 "AND mask == addr).");
6fc6879b
JM
607 return -1;
608 }
609 }
610
611 return 0;
612}
613
614
615static int mac_in_conf(struct hostapd_config *conf, const void *a)
616{
617 size_t i;
618
619 for (i = 0; i < conf->num_bss; i++) {
ebd79f07 620 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
6fc6879b
JM
621 return 1;
622 }
623 }
624
625 return 0;
626}
627
628
8047a958
JM
629#ifndef CONFIG_NO_RADIUS
630
631static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
632 struct radius_das_attrs *attr)
633{
cb10c7d1
JM
634 if (attr->nas_identifier &&
635 (!hapd->conf->nas_identifier ||
636 os_strlen(hapd->conf->nas_identifier) !=
637 attr->nas_identifier_len ||
638 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
639 attr->nas_identifier_len) != 0)) {
640 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
641 return 1;
642 }
643
644 if (attr->nas_ip_addr &&
645 (hapd->conf->own_ip_addr.af != AF_INET ||
646 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
647 0)) {
648 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
649 return 1;
650 }
651
652#ifdef CONFIG_IPV6
653 if (attr->nas_ipv6_addr &&
654 (hapd->conf->own_ip_addr.af != AF_INET6 ||
655 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
656 != 0)) {
657 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
658 return 1;
659 }
660#endif /* CONFIG_IPV6 */
661
8047a958
JM
662 return 0;
663}
664
665
666static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
861beb72
JM
667 struct radius_das_attrs *attr,
668 int *multi)
8047a958 669{
861beb72 670 struct sta_info *selected, *sta;
8047a958 671 char buf[128];
861beb72
JM
672 int num_attr = 0;
673 int count;
8047a958 674
861beb72
JM
675 *multi = 0;
676
677 for (sta = hapd->sta_list; sta; sta = sta->next)
678 sta->radius_das_match = 1;
679
680 if (attr->sta_addr) {
681 num_attr++;
8047a958 682 sta = ap_get_sta(hapd, attr->sta_addr);
861beb72
JM
683 if (!sta) {
684 wpa_printf(MSG_DEBUG,
685 "RADIUS DAS: No Calling-Station-Id match");
686 return NULL;
687 }
8047a958 688
861beb72 689 selected = sta;
8047a958 690 for (sta = hapd->sta_list; sta; sta = sta->next) {
861beb72
JM
691 if (sta != selected)
692 sta->radius_das_match = 0;
693 }
694 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
695 }
696
697 if (attr->acct_session_id) {
698 num_attr++;
d72a0053 699 if (attr->acct_session_id_len != 16) {
861beb72
JM
700 wpa_printf(MSG_DEBUG,
701 "RADIUS DAS: Acct-Session-Id cannot match");
702 return NULL;
703 }
704 count = 0;
705
706 for (sta = hapd->sta_list; sta; sta = sta->next) {
707 if (!sta->radius_das_match)
708 continue;
1492fbb9
NL
709 os_snprintf(buf, sizeof(buf), "%016llX",
710 (unsigned long long) sta->acct_session_id);
d72a0053 711 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
861beb72
JM
712 sta->radius_das_match = 0;
713 else
714 count++;
715 }
716
717 if (count == 0) {
718 wpa_printf(MSG_DEBUG,
719 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
720 return NULL;
8047a958 721 }
861beb72 722 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
8047a958
JM
723 }
724
4e871ed1
JM
725 if (attr->acct_multi_session_id) {
726 num_attr++;
d72a0053 727 if (attr->acct_multi_session_id_len != 16) {
4e871ed1
JM
728 wpa_printf(MSG_DEBUG,
729 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
730 return NULL;
731 }
732 count = 0;
733
734 for (sta = hapd->sta_list; sta; sta = sta->next) {
735 if (!sta->radius_das_match)
736 continue;
737 if (!sta->eapol_sm ||
d72a0053 738 !sta->eapol_sm->acct_multi_session_id) {
4e871ed1
JM
739 sta->radius_das_match = 0;
740 continue;
741 }
1492fbb9
NL
742 os_snprintf(buf, sizeof(buf), "%016llX",
743 (unsigned long long)
d72a0053
NL
744 sta->eapol_sm->acct_multi_session_id);
745 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
4e871ed1
JM
746 0)
747 sta->radius_das_match = 0;
748 else
749 count++;
750 }
751
752 if (count == 0) {
753 wpa_printf(MSG_DEBUG,
754 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
755 return NULL;
756 }
757 wpa_printf(MSG_DEBUG,
758 "RADIUS DAS: Acct-Multi-Session-Id match");
759 }
760
861beb72
JM
761 if (attr->cui) {
762 num_attr++;
763 count = 0;
764
302fc0a3
JM
765 for (sta = hapd->sta_list; sta; sta = sta->next) {
766 struct wpabuf *cui;
861beb72
JM
767
768 if (!sta->radius_das_match)
769 continue;
302fc0a3 770 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
861beb72 771 if (!cui || wpabuf_len(cui) != attr->cui_len ||
302fc0a3 772 os_memcmp(wpabuf_head(cui), attr->cui,
861beb72
JM
773 attr->cui_len) != 0)
774 sta->radius_das_match = 0;
775 else
776 count++;
777 }
778
779 if (count == 0) {
780 wpa_printf(MSG_DEBUG,
781 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
782 return NULL;
302fc0a3 783 }
861beb72
JM
784 wpa_printf(MSG_DEBUG,
785 "RADIUS DAS: Chargeable-User-Identity match");
302fc0a3
JM
786 }
787
861beb72
JM
788 if (attr->user_name) {
789 num_attr++;
790 count = 0;
791
8047a958
JM
792 for (sta = hapd->sta_list; sta; sta = sta->next) {
793 u8 *identity;
794 size_t identity_len;
861beb72
JM
795
796 if (!sta->radius_das_match)
797 continue;
8047a958
JM
798 identity = ieee802_1x_get_identity(sta->eapol_sm,
799 &identity_len);
861beb72
JM
800 if (!identity ||
801 identity_len != attr->user_name_len ||
8047a958 802 os_memcmp(identity, attr->user_name, identity_len)
861beb72
JM
803 != 0)
804 sta->radius_das_match = 0;
805 else
806 count++;
807 }
808
809 if (count == 0) {
810 wpa_printf(MSG_DEBUG,
811 "RADIUS DAS: No matches remaining after User-Name check");
812 return NULL;
813 }
814 wpa_printf(MSG_DEBUG,
815 "RADIUS DAS: User-Name match");
816 }
817
818 if (num_attr == 0) {
819 /*
820 * In theory, we could match all current associations, but it
821 * seems safer to just reject requests that do not include any
822 * session identification attributes.
823 */
824 wpa_printf(MSG_DEBUG,
825 "RADIUS DAS: No session identification attributes included");
826 return NULL;
827 }
828
829 selected = NULL;
830 for (sta = hapd->sta_list; sta; sta = sta->next) {
831 if (sta->radius_das_match) {
832 if (selected) {
833 *multi = 1;
834 return NULL;
835 }
836 selected = sta;
8047a958
JM
837 }
838 }
839
861beb72 840 return selected;
8047a958
JM
841}
842
843
cbc210de
JM
844static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
845 struct radius_das_attrs *attr)
846{
847 if (!hapd->wpa_auth)
848 return -1;
849 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
850}
851
852
8047a958
JM
853static enum radius_das_res
854hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
855{
856 struct hostapd_data *hapd = ctx;
857 struct sta_info *sta;
861beb72 858 int multi;
8047a958
JM
859
860 if (hostapd_das_nas_mismatch(hapd, attr))
861 return RADIUS_DAS_NAS_MISMATCH;
862
861beb72
JM
863 sta = hostapd_das_find_sta(hapd, attr, &multi);
864 if (sta == NULL) {
865 if (multi) {
866 wpa_printf(MSG_DEBUG,
867 "RADIUS DAS: Multiple sessions match - not supported");
868 return RADIUS_DAS_MULTI_SESSION_MATCH;
869 }
cbc210de
JM
870 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
871 wpa_printf(MSG_DEBUG,
872 "RADIUS DAS: PMKSA cache entry matched");
873 return RADIUS_DAS_SUCCESS;
874 }
861beb72 875 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
8047a958 876 return RADIUS_DAS_SESSION_NOT_FOUND;
861beb72 877 }
8047a958 878
861beb72
JM
879 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
880 " - disconnecting", MAC2STR(sta->addr));
0d7c5e1d
JM
881 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
882
8047a958
JM
883 hostapd_drv_sta_deauth(hapd, sta->addr,
884 WLAN_REASON_PREV_AUTH_NOT_VALID);
885 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
886
887 return RADIUS_DAS_SUCCESS;
888}
889
890#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
891
892
6fc6879b
JM
893/**
894 * hostapd_setup_bss - Per-BSS setup (initialization)
895 * @hapd: Pointer to BSS data
2aec4f3c
JM
896 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
897 * but interface may exist
6fc6879b
JM
898 *
899 * This function is used to initialize all per-BSS data structures and
900 * resources. This gets called in a loop for each BSS when an interface is
901 * initialized. Most of the modules that are initialized here will be
902 * deinitialized in hostapd_cleanup().
903 */
904static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
905{
906 struct hostapd_bss_config *conf = hapd->conf;
81847c22 907 u8 ssid[SSID_MAX_LEN + 1];
6fc6879b 908 int ssid_len, set_ssid;
f3585c8a
JM
909 char force_ifname[IFNAMSIZ];
910 u8 if_addr[ETH_ALEN];
01e2231f 911 int flush_old_stations = 1;
6fc6879b 912
36501a22 913 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
39323bc1 914 __func__, hapd, conf->iface, first);
36501a22 915
10b58b50 916#ifdef EAP_SERVER_TNC
39323bc1 917 if (conf->tnc && tncs_global_init() < 0) {
10b58b50
JM
918 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
919 return -1;
920 }
921#endif /* EAP_SERVER_TNC */
922
36501a22
JM
923 if (hapd->started) {
924 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
39323bc1 925 __func__, conf->iface);
36501a22
JM
926 return -1;
927 }
928 hapd->started = 1;
929
2aec4f3c 930 if (!first || first == -1) {
6448e064
EP
931 u8 *addr = hapd->own_addr;
932
933 if (!is_zero_ether_addr(conf->bssid)) {
6fc6879b 934 /* Allocate the configured BSSID. */
39323bc1 935 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
6fc6879b
JM
936
937 if (hostapd_mac_comp(hapd->own_addr,
938 hapd->iface->bss[0]->own_addr) ==
939 0) {
bb305cbd
JM
940 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
941 "BSSID set to the MAC address of "
39323bc1 942 "the radio", conf->iface);
6fc6879b
JM
943 return -1;
944 }
6448e064
EP
945 } else if (hapd->iconf->use_driver_iface_addr) {
946 addr = NULL;
947 } else {
948 /* Allocate the next available BSSID. */
949 do {
950 inc_byte_array(hapd->own_addr, ETH_ALEN);
951 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
6fc6879b
JM
952 }
953
6023a788 954 hapd->interface_added = 1;
22a7c9d7 955 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
6448e064 956 conf->iface, addr, hapd,
e17a2477 957 &hapd->drv_priv, force_ifname, if_addr,
39323bc1
KP
958 conf->bridge[0] ? conf->bridge : NULL,
959 first == -1)) {
bb305cbd
JM
960 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
961 MACSTR ")", MAC2STR(hapd->own_addr));
493ba877 962 hapd->interface_added = 0;
6fc6879b
JM
963 return -1;
964 }
6448e064
EP
965
966 if (!addr)
967 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
6fc6879b
JM
968 }
969
d2da2249
JB
970 if (conf->wmm_enabled < 0)
971 conf->wmm_enabled = hapd->iconf->ieee80211n;
972
4ec1fd8e 973#ifdef CONFIG_IEEE80211R_AP
d48d1b88
MB
974 if (is_zero_ether_addr(conf->r1_key_holder))
975 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
4ec1fd8e 976#endif /* CONFIG_IEEE80211R_AP */
d48d1b88 977
01e2231f 978#ifdef CONFIG_MESH
21ed24f5 979 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
01e2231f
JL
980 flush_old_stations = 0;
981#endif /* CONFIG_MESH */
982
983 if (flush_old_stations)
984 hostapd_flush_old_stations(hapd,
985 WLAN_REASON_PREV_AUTH_NOT_VALID);
c213cc04
JM
986 hostapd_set_privacy(hapd, 0);
987
988 hostapd_broadcast_wep_clear(hapd);
39323bc1 989 if (hostapd_setup_encryption(conf->iface, hapd))
c213cc04
JM
990 return -1;
991
6fc6879b
JM
992 /*
993 * Fetch the SSID from the system and use it or,
994 * if one was specified in the config file, verify they
995 * match.
996 */
997 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
998 if (ssid_len < 0) {
bb305cbd 999 wpa_printf(MSG_ERROR, "Could not read SSID from system");
6fc6879b
JM
1000 return -1;
1001 }
1002 if (conf->ssid.ssid_set) {
1003 /*
1004 * If SSID is specified in the config file and it differs
1005 * from what is being used then force installation of the
1006 * new SSID.
1007 */
1008 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1009 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1010 } else {
1011 /*
1012 * No SSID in the config file; just use the one we got
1013 * from the system.
1014 */
1015 set_ssid = 0;
1016 conf->ssid.ssid_len = ssid_len;
1017 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
6fc6879b
JM
1018 }
1019
85141289 1020 if (!hostapd_drv_none(hapd)) {
bb305cbd 1021 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
986de33d 1022 " and ssid \"%s\"",
39323bc1
KP
1023 conf->iface, MAC2STR(hapd->own_addr),
1024 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
85141289 1025 }
6fc6879b
JM
1026
1027 if (hostapd_setup_wpa_psk(conf)) {
bb305cbd 1028 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
6fc6879b
JM
1029 return -1;
1030 }
1031
6fc6879b
JM
1032 /* Set SSID for the kernel driver (to be used in beacon and probe
1033 * response frames) */
986de33d 1034 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
6fc6879b 1035 conf->ssid.ssid_len)) {
bb305cbd 1036 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
6fc6879b
JM
1037 return -1;
1038 }
1039
ec33bc67 1040 if (wpa_debug_level <= MSG_MSGDUMP)
6fc6879b 1041 conf->radius->msg_dumps = 1;
74784010 1042#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
1043 hapd->radius = radius_client_init(hapd, conf->radius);
1044 if (hapd->radius == NULL) {
bb305cbd 1045 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
6fc6879b
JM
1046 return -1;
1047 }
b031338c 1048
39323bc1 1049 if (conf->radius_das_port) {
b031338c
JM
1050 struct radius_das_conf das_conf;
1051 os_memset(&das_conf, 0, sizeof(das_conf));
39323bc1
KP
1052 das_conf.port = conf->radius_das_port;
1053 das_conf.shared_secret = conf->radius_das_shared_secret;
b031338c 1054 das_conf.shared_secret_len =
39323bc1
KP
1055 conf->radius_das_shared_secret_len;
1056 das_conf.client_addr = &conf->radius_das_client_addr;
1057 das_conf.time_window = conf->radius_das_time_window;
bde7ba6c 1058 das_conf.require_event_timestamp =
39323bc1 1059 conf->radius_das_require_event_timestamp;
42d30e9e
NL
1060 das_conf.require_message_authenticator =
1061 conf->radius_das_require_message_authenticator;
8047a958
JM
1062 das_conf.ctx = hapd;
1063 das_conf.disconnect = hostapd_das_disconnect;
b031338c
JM
1064 hapd->radius_das = radius_das_init(&das_conf);
1065 if (hapd->radius_das == NULL) {
1066 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1067 "failed.");
1068 return -1;
1069 }
1070 }
74784010 1071#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
1072
1073 if (hostapd_acl_init(hapd)) {
bb305cbd 1074 wpa_printf(MSG_ERROR, "ACL initialization failed.");
6fc6879b
JM
1075 return -1;
1076 }
ad08c363
JM
1077 if (hostapd_init_wps(hapd, conf))
1078 return -1;
6fc6879b 1079
43a7fe2e
CL
1080 if (authsrv_init(hapd) < 0)
1081 return -1;
1082
6fc6879b 1083 if (ieee802_1x_init(hapd)) {
bb305cbd 1084 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
6fc6879b
JM
1085 return -1;
1086 }
1087
39323bc1 1088 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
6fc6879b
JM
1089 return -1;
1090
1091 if (accounting_init(hapd)) {
bb305cbd 1092 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
6fc6879b
JM
1093 return -1;
1094 }
1095
39323bc1
KP
1096 if (conf->ieee802_11f &&
1097 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
bb305cbd
JM
1098 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1099 "failed.");
6fc6879b
JM
1100 return -1;
1101 }
1102
dca30c3f
JK
1103#ifdef CONFIG_INTERWORKING
1104 if (gas_serv_init(hapd)) {
1105 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1106 return -1;
1107 }
bf7f09bd
JM
1108
1109 if (conf->qos_map_set_len &&
1110 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1111 conf->qos_map_set_len)) {
1112 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1113 return -1;
1114 }
dca30c3f
JK
1115#endif /* CONFIG_INTERWORKING */
1116
ec8f36af
KP
1117 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1118 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1119 return -1;
1120 }
1121
1d783762
KP
1122 if (conf->proxy_arp) {
1123 if (x_snoop_init(hapd)) {
1124 wpa_printf(MSG_ERROR,
1125 "Generic snooping infrastructure initialization failed");
1126 return -1;
1127 }
1128
1129 if (dhcp_snoop_init(hapd)) {
1130 wpa_printf(MSG_ERROR,
1131 "DHCP snooping initialization failed");
1132 return -1;
1133 }
bd00c431
KP
1134
1135 if (ndisc_snoop_init(hapd)) {
1136 wpa_printf(MSG_ERROR,
1137 "Neighbor Discovery snooping initialization failed");
1138 return -1;
1139 }
7d597d46
KP
1140 }
1141
85141289 1142 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
bb305cbd 1143 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
6fc6879b
JM
1144 return -1;
1145 }
1146
39323bc1 1147 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
bad5cdf4 1148 return -1;
6fc6879b 1149
bdffdc5d
JM
1150 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1151 return -1;
1152
e11f5a2c
JM
1153 if (hapd->driver && hapd->driver->set_operstate)
1154 hapd->driver->set_operstate(hapd->drv_priv, 1);
1155
6fc6879b
JM
1156 return 0;
1157}
1158
1159
990ec378
JM
1160static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1161{
1162 struct hostapd_data *hapd = iface->bss[0];
1163 int i;
1164 struct hostapd_tx_queue_params *p;
1165
01e2231f 1166#ifdef CONFIG_MESH
21ed24f5 1167 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
01e2231f
JL
1168 return;
1169#endif /* CONFIG_MESH */
1170
990ec378
JM
1171 for (i = 0; i < NUM_TX_QUEUES; i++) {
1172 p = &iface->conf->tx_queue[i];
1173
990ec378
JM
1174 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1175 p->cwmax, p->burst)) {
bb305cbd
JM
1176 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1177 "parameters for queue %d.", i);
990ec378
JM
1178 /* Continue anyway */
1179 }
1180 }
1181}
1182
1183
3c4ca363
VN
1184static int hostapd_set_acl_list(struct hostapd_data *hapd,
1185 struct mac_acl_entry *mac_acl,
1186 int n_entries, u8 accept_acl)
1187{
1188 struct hostapd_acl_params *acl_params;
1189 int i, err;
1190
1191 acl_params = os_zalloc(sizeof(*acl_params) +
1192 (n_entries * sizeof(acl_params->mac_acl[0])));
1193 if (!acl_params)
1194 return -ENOMEM;
1195
1196 for (i = 0; i < n_entries; i++)
1197 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1198 ETH_ALEN);
1199
1200 acl_params->acl_policy = accept_acl;
1201 acl_params->num_mac_acl = n_entries;
1202
1203 err = hostapd_drv_set_acl(hapd, acl_params);
1204
1205 os_free(acl_params);
1206
1207 return err;
1208}
1209
1210
1211static void hostapd_set_acl(struct hostapd_data *hapd)
1212{
1213 struct hostapd_config *conf = hapd->iconf;
1214 int err;
1215 u8 accept_acl;
1216
3cb953e4
JM
1217 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1218 return;
3c4ca363 1219
ebd79f07 1220 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
cf1600ac
AHS
1221 accept_acl = 1;
1222 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1223 conf->bss[0]->num_accept_mac,
1224 accept_acl);
1225 if (err) {
1226 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1227 return;
3c4ca363 1228 }
ebd79f07 1229 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
cf1600ac
AHS
1230 accept_acl = 0;
1231 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1232 conf->bss[0]->num_deny_mac,
1233 accept_acl);
1234 if (err) {
1235 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1236 return;
3c4ca363
VN
1237 }
1238 }
1239}
1240
1241
ad08e141
JM
1242static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1243{
1244 if (!hapd->iface->interfaces ||
1245 !hapd->iface->interfaces->ctrl_iface_init)
1246 return 0;
1247
1248 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1249 wpa_printf(MSG_ERROR,
1250 "Failed to setup control interface for %s",
1251 hapd->conf->iface);
1252 return -1;
1253 }
1254
1255 return 0;
1256}
1257
1258
1259static int start_ctrl_iface(struct hostapd_iface *iface)
1260{
1261 size_t i;
1262
1263 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1264 return 0;
1265
1266 for (i = 0; i < iface->num_bss; i++) {
1267 struct hostapd_data *hapd = iface->bss[i];
1268 if (iface->interfaces->ctrl_iface_init(hapd)) {
1269 wpa_printf(MSG_ERROR,
1270 "Failed to setup control interface for %s",
1271 hapd->conf->iface);
1272 return -1;
1273 }
1274 }
1275
1276 return 0;
1277}
1278
1279
f0793bf1
JM
1280static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1281{
1282 struct hostapd_iface *iface = eloop_ctx;
1283
1284 if (!iface->wait_channel_update) {
1285 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1286 return;
1287 }
1288
1289 /*
1290 * It is possible that the existing channel list is acceptable, so try
1291 * to proceed.
1292 */
1293 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1294 setup_interface2(iface);
1295}
1296
1297
795baf77 1298void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
f0793bf1 1299{
795baf77 1300 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
f0793bf1
JM
1301 return;
1302
1303 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1304 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1305 setup_interface2(iface);
1306}
1307
1308
ddaa83eb 1309static int setup_interface(struct hostapd_iface *iface)
6fc6879b
JM
1310{
1311 struct hostapd_data *hapd = iface->bss[0];
6fc6879b 1312 size_t i;
6fc6879b 1313
354c903f
MB
1314 /*
1315 * It is possible that setup_interface() is called after the interface
1316 * was disabled etc., in which case driver_ap_teardown is possibly set
1317 * to 1. Clear it here so any other key/station deletion, which is not
1318 * part of a teardown flow, would also call the relevant driver
1319 * callbacks.
1320 */
1321 iface->driver_ap_teardown = 0;
1322
2db938e8
JM
1323 if (!iface->phy[0]) {
1324 const char *phy = hostapd_drv_get_radio_name(hapd);
1325 if (phy) {
1326 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1327 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1328 }
1329 }
1330
6fc6879b 1331 /*
e5f2b59c
JM
1332 * Make sure that all BSSes get configured with a pointer to the same
1333 * driver interface.
6fc6879b 1334 */
e5f2b59c 1335 for (i = 1; i < iface->num_bss; i++) {
6fc6879b
JM
1336 iface->bss[i]->driver = hapd->driver;
1337 iface->bss[i]->drv_priv = hapd->drv_priv;
1338 }
1339
1340 if (hostapd_validate_bssid_configuration(iface))
1341 return -1;
1342
ad08e141
JM
1343 /*
1344 * Initialize control interfaces early to allow external monitoring of
1345 * channel setup operations that may take considerable amount of time
1346 * especially for DFS cases.
1347 */
1348 if (start_ctrl_iface(iface))
1349 return -1;
1350
6f4071c0 1351 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
f0793bf1
JM
1352 char country[4], previous_country[4];
1353
e1c5faf0 1354 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
f0793bf1
JM
1355 if (hostapd_get_country(hapd, previous_country) < 0)
1356 previous_country[0] = '\0';
1357
6f4071c0
JM
1358 os_memcpy(country, hapd->iconf->country, 3);
1359 country[3] = '\0';
1360 if (hostapd_set_country(hapd, country) < 0) {
1361 wpa_printf(MSG_ERROR, "Failed to set country code");
1362 return -1;
1363 }
f0793bf1
JM
1364
1365 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1366 previous_country, country);
1367
1368 if (os_strncmp(previous_country, country, 2) != 0) {
1369 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1370 iface->wait_channel_update = 1;
fd924134 1371 eloop_register_timeout(5, 0,
f0793bf1
JM
1372 channel_list_update_timeout,
1373 iface, NULL);
1374 return 0;
1375 }
6fc6879b
JM
1376 }
1377
f0793bf1
JM
1378 return setup_interface2(iface);
1379}
1380
1381
1382static int setup_interface2(struct hostapd_iface *iface)
1383{
1384 iface->wait_channel_update = 0;
1385
6fc6879b
JM
1386 if (hostapd_get_hw_features(iface)) {
1387 /* Not all drivers support this yet, so continue without hw
1388 * feature data. */
1389 } else {
ddaa83eb
JM
1390 int ret = hostapd_select_hw_mode(iface);
1391 if (ret < 0) {
bb305cbd
JM
1392 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1393 "channel. (%d)", ret);
0f23a5e7 1394 goto fail;
ddaa83eb 1395 }
50f4f2a0
MK
1396 if (ret == 1) {
1397 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1398 return 0;
1399 }
ad1e68e6
JM
1400 ret = hostapd_check_ht_capab(iface);
1401 if (ret < 0)
0f23a5e7 1402 goto fail;
ad1e68e6
JM
1403 if (ret == 1) {
1404 wpa_printf(MSG_DEBUG, "Interface initialization will "
1405 "be completed in a callback");
1406 return 0;
1407 }
e76da505
JD
1408
1409 if (iface->conf->ieee80211h)
1410 wpa_printf(MSG_DEBUG, "DFS support is enabled");
ad1e68e6
JM
1411 }
1412 return hostapd_setup_interface_complete(iface, 0);
0f23a5e7
JM
1413
1414fail:
1415 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1416 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1417 if (iface->interfaces && iface->interfaces->terminate_on_error)
1418 eloop_terminate();
1419 return -1;
ad1e68e6
JM
1420}
1421
1422
6959145b
AN
1423#ifdef CONFIG_FST
1424
1425static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1426{
1427 struct hostapd_data *hapd = ctx;
1428
1429 return hapd->own_addr;
1430}
1431
1432
1433static void fst_hostapd_get_channel_info_cb(void *ctx,
1434 enum hostapd_hw_mode *hw_mode,
1435 u8 *channel)
1436{
1437 struct hostapd_data *hapd = ctx;
1438
1439 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1440}
1441
1442
84bcb4e7 1443static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
6959145b
AN
1444{
1445 struct hostapd_data *hapd = ctx;
1446
1447 if (hapd->iface->fst_ies != fst_ies) {
1448 hapd->iface->fst_ies = fst_ies;
1449 if (ieee802_11_set_beacon(hapd))
1450 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1451 }
1452}
1453
1454
1455static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1456 struct wpabuf *buf)
1457{
1458 struct hostapd_data *hapd = ctx;
1459
1460 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1461 wpabuf_head(buf), wpabuf_len(buf));
1462}
1463
1464
a0f04da5 1465static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
6959145b
AN
1466{
1467 struct hostapd_data *hapd = ctx;
1468 struct sta_info *sta = ap_get_sta(hapd, addr);
1469
1470 return sta ? sta->mb_ies : NULL;
1471}
1472
1473
1474static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1475 const u8 *buf, size_t size)
1476{
1477 struct hostapd_data *hapd = ctx;
1478 struct sta_info *sta = ap_get_sta(hapd, addr);
1479
1480 if (sta) {
1481 struct mb_ies_info info;
1482
1483 if (!mb_ies_info_by_ies(&info, buf, size)) {
1484 wpabuf_free(sta->mb_ies);
1485 sta->mb_ies = mb_ies_by_info(&info);
1486 }
1487 }
1488}
1489
1490
1491static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1492 Boolean mb_only)
1493{
1494 struct sta_info *s = (struct sta_info *) *get_ctx;
1495
1496 if (mb_only) {
1497 for (; s && !s->mb_ies; s = s->next)
1498 ;
1499 }
1500
1501 if (s) {
1502 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1503
1504 return s->addr;
1505 }
1506
1507 *get_ctx = NULL;
1508 return NULL;
1509}
1510
1511
1512static const u8 * fst_hostapd_get_peer_first(void *ctx,
1513 struct fst_get_peer_ctx **get_ctx,
1514 Boolean mb_only)
1515{
1516 struct hostapd_data *hapd = ctx;
1517
1518 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1519
1520 return fst_hostapd_get_sta(get_ctx, mb_only);
1521}
1522
1523
1524static const u8 * fst_hostapd_get_peer_next(void *ctx,
1525 struct fst_get_peer_ctx **get_ctx,
1526 Boolean mb_only)
1527{
1528 return fst_hostapd_get_sta(get_ctx, mb_only);
1529}
1530
1531
1532void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1533 struct fst_wpa_obj *iface_obj)
1534{
1535 iface_obj->ctx = hapd;
1536 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1537 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1538 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1539 iface_obj->send_action = fst_hostapd_send_action_cb;
1540 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1541 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1542 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1543 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1544}
1545
1546#endif /* CONFIG_FST */
1547
1548
061269b3
DS
1549#ifdef NEED_AP_MLME
1550static enum nr_chan_width hostapd_get_nr_chan_width(struct hostapd_data *hapd,
1551 int ht, int vht)
1552{
1553 if (!ht && !vht)
1554 return NR_CHAN_WIDTH_20;
1555 if (!hapd->iconf->secondary_channel)
1556 return NR_CHAN_WIDTH_20;
1557 if (!vht || hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_USE_HT)
1558 return NR_CHAN_WIDTH_40;
1559 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80MHZ)
1560 return NR_CHAN_WIDTH_80;
1561 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_160MHZ)
1562 return NR_CHAN_WIDTH_160;
1563 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80P80MHZ)
1564 return NR_CHAN_WIDTH_80P80;
1565 return NR_CHAN_WIDTH_20;
1566}
1567#endif /* NEED_AP_MLME */
1568
1569
1570static void hostapd_set_own_neighbor_report(struct hostapd_data *hapd)
1571{
1572#ifdef NEED_AP_MLME
1573 u16 capab = hostapd_own_capab_info(hapd);
1574 int ht = hapd->iconf->ieee80211n && !hapd->conf->disable_11n;
1575 int vht = hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac;
1576 struct wpa_ssid_value ssid;
1577 u8 channel, op_class;
f0259c3f 1578 u8 center_freq1_idx = 0, center_freq2_idx = 0;
061269b3
DS
1579 enum nr_chan_width width;
1580 u32 bssid_info;
1581 struct wpabuf *nr;
1582
1583 if (!(hapd->conf->radio_measurements[0] &
1584 WLAN_RRM_CAPS_NEIGHBOR_REPORT))
1585 return;
1586
1587 bssid_info = 3; /* AP is reachable */
1588 bssid_info |= NEI_REP_BSSID_INFO_SECURITY; /* "same as the AP" */
1589 bssid_info |= NEI_REP_BSSID_INFO_KEY_SCOPE; /* "same as the AP" */
1590
1591 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT)
1592 bssid_info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
1593
1594 bssid_info |= NEI_REP_BSSID_INFO_RM; /* RRM is supported */
1595
1596 if (hapd->conf->wmm_enabled) {
1597 bssid_info |= NEI_REP_BSSID_INFO_QOS;
1598
1599 if (hapd->conf->wmm_uapsd &&
1600 (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
1601 bssid_info |= NEI_REP_BSSID_INFO_APSD;
1602 }
1603
1604 if (ht) {
1605 bssid_info |= NEI_REP_BSSID_INFO_HT |
1606 NEI_REP_BSSID_INFO_DELAYED_BA;
1607
1608 /* VHT bit added in IEEE P802.11-REVmc/D4.3 */
1609 if (vht)
1610 bssid_info |= NEI_REP_BSSID_INFO_VHT;
1611 }
1612
1613 /* TODO: Set NEI_REP_BSSID_INFO_MOBILITY_DOMAIN if MDE is set */
1614
f5687741
JM
1615 if (ieee80211_freq_to_channel_ext(hapd->iface->freq,
1616 hapd->iconf->secondary_channel,
1617 hapd->iconf->vht_oper_chwidth,
1618 &op_class, &channel) ==
1619 NUM_HOSTAPD_MODES)
1620 return;
061269b3
DS
1621 width = hostapd_get_nr_chan_width(hapd, ht, vht);
1622 if (vht) {
f0259c3f 1623 center_freq1_idx = hapd->iconf->vht_oper_centr_freq_seg0_idx;
061269b3 1624 if (width == NR_CHAN_WIDTH_80P80)
f0259c3f
BL
1625 center_freq2_idx =
1626 hapd->iconf->vht_oper_centr_freq_seg1_idx;
061269b3 1627 } else if (ht) {
f0259c3f
BL
1628 ieee80211_freq_to_chan(hapd->iface->freq +
1629 10 * hapd->iconf->secondary_channel,
1630 &center_freq1_idx);
061269b3
DS
1631 }
1632
1633 ssid.ssid_len = hapd->conf->ssid.ssid_len;
1634 os_memcpy(ssid.ssid, hapd->conf->ssid.ssid, ssid.ssid_len);
1635
1636 /*
1637 * Neighbor Report element size = BSSID + BSSID info + op_class + chan +
1638 * phy type + wide bandwidth channel subelement.
1639 */
1640 nr = wpabuf_alloc(ETH_ALEN + 4 + 1 + 1 + 1 + 5);
1641 if (!nr)
1642 return;
1643
1644 wpabuf_put_data(nr, hapd->own_addr, ETH_ALEN);
1645 wpabuf_put_le32(nr, bssid_info);
1646 wpabuf_put_u8(nr, op_class);
1647 wpabuf_put_u8(nr, channel);
1648 wpabuf_put_u8(nr, ieee80211_get_phy_type(hapd->iface->freq, ht, vht));
1649
1650 /*
1651 * Wide Bandwidth Channel subelement may be needed to allow the
1652 * receiving STA to send packets to the AP. See IEEE P802.11-REVmc/D5.0
1653 * Figure 9-301.
1654 */
1655 wpabuf_put_u8(nr, WNM_NEIGHBOR_WIDE_BW_CHAN);
1656 wpabuf_put_u8(nr, 3);
1657 wpabuf_put_u8(nr, width);
f0259c3f
BL
1658 wpabuf_put_u8(nr, center_freq1_idx);
1659 wpabuf_put_u8(nr, center_freq2_idx);
061269b3
DS
1660
1661 hostapd_neighbor_set(hapd, hapd->own_addr, &ssid, nr, hapd->iconf->lci,
451a27b1 1662 hapd->iconf->civic, hapd->iconf->stationary_ap);
061269b3
DS
1663
1664 wpabuf_free(nr);
1665#endif /* NEED_AP_MLME */
1666}
1667
1668
053693d2
SD
1669static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1670 int err)
ad1e68e6
JM
1671{
1672 struct hostapd_data *hapd = iface->bss[0];
ad1e68e6
JM
1673 size_t j;
1674 u8 *prev_addr;
01e2231f 1675 int delay_apply_cfg = 0;
c13578c3 1676 int res_dfs_offload = 0;
ad1e68e6 1677
0f23a5e7
JM
1678 if (err)
1679 goto fail;
6fc6879b 1680
ad1e68e6 1681 wpa_printf(MSG_DEBUG, "Completing interface initialization");
dc036d9e 1682 if (iface->conf->channel) {
e76da505
JD
1683#ifdef NEED_AP_MLME
1684 int res;
1685#endif /* NEED_AP_MLME */
1686
dc036d9e 1687 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
bb305cbd
JM
1688 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1689 "Frequency: %d MHz",
dc036d9e
JM
1690 hostapd_hw_mode_txt(iface->conf->hw_mode),
1691 iface->conf->channel, iface->freq);
6fc6879b 1692
e76da505 1693#ifdef NEED_AP_MLME
c53a9bf8
SD
1694 /* Handle DFS only if it is not offloaded to the driver */
1695 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1696 /* Check DFS */
1697 res = hostapd_handle_dfs(iface);
1698 if (res <= 0) {
1699 if (res < 0)
1700 goto fail;
1701 return res;
1702 }
c13578c3
AK
1703 } else {
1704 /* If DFS is offloaded to the driver */
1705 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1706 if (res_dfs_offload <= 0) {
1707 if (res_dfs_offload < 0)
1708 goto fail;
1709 } else {
1710 wpa_printf(MSG_DEBUG,
1711 "Proceed with AP/channel setup");
1712 /*
1713 * If this is a DFS channel, move to completing
1714 * AP setup.
1715 */
1716 if (res_dfs_offload == 1)
1717 goto dfs_offload;
1718 /* Otherwise fall through. */
1719 }
0f23a5e7 1720 }
e76da505
JD
1721#endif /* NEED_AP_MLME */
1722
01e2231f
JL
1723#ifdef CONFIG_MESH
1724 if (iface->mconf != NULL) {
1725 wpa_printf(MSG_DEBUG,
1726 "%s: Mesh configuration will be applied while joining the mesh network",
1727 iface->bss[0]->conf->iface);
1728 delay_apply_cfg = 1;
1729 }
1730#endif /* CONFIG_MESH */
1731
1732 if (!delay_apply_cfg &&
1733 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
9c6d8e1d 1734 hapd->iconf->channel,
fe0f58fa 1735 hapd->iconf->ieee80211n,
fa476336
JB
1736 hapd->iconf->ieee80211ac,
1737 hapd->iconf->secondary_channel,
1738 hapd->iconf->vht_oper_chwidth,
1739 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1740 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
bb305cbd
JM
1741 wpa_printf(MSG_ERROR, "Could not set channel for "
1742 "kernel driver");
0f23a5e7 1743 goto fail;
ddaa83eb
JM
1744 }
1745 }
6fc6879b 1746
5a5009dc 1747 if (iface->current_mode) {
34445d12 1748 if (hostapd_prepare_rates(iface, iface->current_mode)) {
5a5009dc
FF
1749 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1750 "table.");
1751 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1752 HOSTAPD_LEVEL_WARNING,
1753 "Failed to prepare rates table.");
0f23a5e7 1754 goto fail;
5a5009dc
FF
1755 }
1756 }
1757
ddaa83eb
JM
1758 if (hapd->iconf->rts_threshold > -1 &&
1759 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
bb305cbd
JM
1760 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1761 "kernel driver");
0f23a5e7 1762 goto fail;
ddaa83eb
JM
1763 }
1764
1765 if (hapd->iconf->fragm_threshold > -1 &&
1766 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
bb305cbd
JM
1767 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1768 "for kernel driver");
0f23a5e7 1769 goto fail;
ddaa83eb 1770 }
6fc6879b 1771
ddaa83eb
JM
1772 prev_addr = hapd->own_addr;
1773
1774 for (j = 0; j < iface->num_bss; j++) {
1775 hapd = iface->bss[j];
1776 if (j)
1777 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
ac1a2240
MK
1778 if (hostapd_setup_bss(hapd, j == 0)) {
1779 do {
1780 hapd = iface->bss[j];
438e1333 1781 hostapd_bss_deinit_no_free(hapd);
ac1a2240
MK
1782 hostapd_free_hapd_data(hapd);
1783 } while (j-- > 0);
0f23a5e7 1784 goto fail;
ac1a2240 1785 }
902c07a7 1786 if (is_zero_ether_addr(hapd->conf->bssid))
ddaa83eb
JM
1787 prev_addr = hapd->own_addr;
1788 }
dc036d9e 1789 hapd = iface->bss[0];
ddaa83eb
JM
1790
1791 hostapd_tx_queue_params(iface);
1792
1793 ap_list_init(iface);
1794
3c4ca363
VN
1795 hostapd_set_acl(hapd);
1796
ddaa83eb
JM
1797 if (hostapd_driver_commit(hapd) < 0) {
1798 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1799 "configuration", __func__);
0f23a5e7 1800 goto fail;
ddaa83eb
JM
1801 }
1802
86795546
VA
1803 /*
1804 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1805 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1806 * mode), the interface is up only after driver_commit, so initialize
1807 * WPS after driver_commit.
1808 */
1809 for (j = 0; j < iface->num_bss; j++) {
1810 if (hostapd_init_wps_complete(iface->bss[j]))
0f23a5e7 1811 goto fail;
86795546
VA
1812 }
1813
c13578c3
AK
1814 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1815 !res_dfs_offload) {
1816 /*
1817 * If freq is DFS, and DFS is offloaded to the driver, then wait
1818 * for CAC to complete.
1819 */
1820 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1821 return res_dfs_offload;
1822 }
1823
1824#ifdef NEED_AP_MLME
1825dfs_offload:
1826#endif /* NEED_AP_MLME */
6959145b
AN
1827
1828#ifdef CONFIG_FST
1829 if (hapd->iconf->fst_cfg.group_id[0]) {
1830 struct fst_wpa_obj iface_obj;
1831
1832 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1833 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1834 &iface_obj, &hapd->iconf->fst_cfg);
1835 if (!iface->fst) {
1836 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1837 hapd->iconf->fst_cfg.group_id);
1838 goto fail;
1839 }
1840 }
1841#endif /* CONFIG_FST */
1842
e1c5faf0 1843 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
7d6d7370 1844 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
c76e5d7f
JB
1845 if (hapd->setup_complete_cb)
1846 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1847
ad1e68e6
JM
1848 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1849 iface->bss[0]->conf->iface);
2b6623ab
JM
1850 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1851 iface->interfaces->terminate_on_error--;
ad1e68e6 1852
061269b3
DS
1853 for (j = 0; j < iface->num_bss; j++)
1854 hostapd_set_own_neighbor_report(iface->bss[j]);
1855
21db94c5 1856 return 0;
0f23a5e7
JM
1857
1858fail:
1859 wpa_printf(MSG_ERROR, "Interface initialization failed");
1860 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1861 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
6959145b
AN
1862#ifdef CONFIG_FST
1863 if (iface->fst) {
1864 fst_detach(iface->fst);
1865 iface->fst = NULL;
1866 }
1867#endif /* CONFIG_FST */
0f23a5e7
JM
1868 if (iface->interfaces && iface->interfaces->terminate_on_error)
1869 eloop_terminate();
1870 return -1;
6fc6879b
JM
1871}
1872
1873
053693d2
SD
1874/**
1875 * hostapd_setup_interface_complete - Complete interface setup
1876 *
1877 * This function is called when previous steps in the interface setup has been
1878 * completed. This can also start operations, e.g., DFS, that will require
1879 * additional processing before interface is ready to be enabled. Such
1880 * operations will call this function from eloop callbacks when finished.
1881 */
1882int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1883{
1884 struct hapd_interfaces *interfaces = iface->interfaces;
1885 struct hostapd_data *hapd = iface->bss[0];
1886 unsigned int i;
1887 int not_ready_in_sync_ifaces = 0;
1888
1889 if (!iface->need_to_start_in_sync)
1890 return hostapd_setup_interface_complete_sync(iface, err);
1891
1892 if (err) {
1893 wpa_printf(MSG_ERROR, "Interface initialization failed");
1894 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1895 iface->need_to_start_in_sync = 0;
1896 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1897 if (interfaces && interfaces->terminate_on_error)
1898 eloop_terminate();
1899 return -1;
1900 }
1901
1902 if (iface->ready_to_start_in_sync) {
1903 /* Already in ready and waiting. should never happpen */
1904 return 0;
1905 }
1906
1907 for (i = 0; i < interfaces->count; i++) {
1908 if (interfaces->iface[i]->need_to_start_in_sync &&
1909 !interfaces->iface[i]->ready_to_start_in_sync)
1910 not_ready_in_sync_ifaces++;
1911 }
1912
1913 /*
1914 * Check if this is the last interface, if yes then start all the other
1915 * waiting interfaces. If not, add this interface to the waiting list.
1916 */
1917 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
1918 /*
1919 * If this interface went through CAC, do not synchronize, just
1920 * start immediately.
1921 */
1922 iface->need_to_start_in_sync = 0;
1923 wpa_printf(MSG_INFO,
1924 "%s: Finished CAC - bypass sync and start interface",
1925 iface->bss[0]->conf->iface);
1926 return hostapd_setup_interface_complete_sync(iface, err);
1927 }
1928
1929 if (not_ready_in_sync_ifaces > 1) {
1930 /* need to wait as there are other interfaces still coming up */
1931 iface->ready_to_start_in_sync = 1;
1932 wpa_printf(MSG_INFO,
1933 "%s: Interface waiting to sync with other interfaces",
1934 iface->bss[0]->conf->iface);
1935 return 0;
1936 }
1937
1938 wpa_printf(MSG_INFO,
1939 "%s: Last interface to sync - starting all interfaces",
1940 iface->bss[0]->conf->iface);
1941 iface->need_to_start_in_sync = 0;
1942 hostapd_setup_interface_complete_sync(iface, err);
1943 for (i = 0; i < interfaces->count; i++) {
1944 if (interfaces->iface[i]->need_to_start_in_sync &&
1945 interfaces->iface[i]->ready_to_start_in_sync) {
1946 hostapd_setup_interface_complete_sync(
1947 interfaces->iface[i], 0);
1948 /* Only once the interfaces are sync started */
1949 interfaces->iface[i]->need_to_start_in_sync = 0;
1950 }
1951 }
1952
1953 return 0;
1954}
1955
1956
6fc6879b 1957/**
ddaa83eb 1958 * hostapd_setup_interface - Setup of an interface
6fc6879b 1959 * @iface: Pointer to interface data.
ddaa83eb 1960 * Returns: 0 on success, -1 on failure
6fc6879b
JM
1961 *
1962 * Initializes the driver interface, validates the configuration,
1963 * and sets driver parameters based on the configuration.
ddaa83eb 1964 * Flushes old stations, sets the channel, encryption,
6fc6879b 1965 * beacons, and WDS links based on the configuration.
0dfd2c61
JM
1966 *
1967 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1968 * or DFS operations, this function returns 0 before such operations have been
1969 * completed. The pending operations are registered into eloop and will be
1970 * completed from eloop callbacks. Those callbacks end up calling
1971 * hostapd_setup_interface_complete() once setup has been completed.
6fc6879b 1972 */
5c333467 1973int hostapd_setup_interface(struct hostapd_iface *iface)
6fc6879b 1974{
ddaa83eb
JM
1975 int ret;
1976
1977 ret = setup_interface(iface);
1978 if (ret) {
bee07ce8 1979 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
6fc6879b
JM
1980 iface->bss[0]->conf->iface);
1981 return -1;
1982 }
1983
6fc6879b
JM
1984 return 0;
1985}
1986
1987
6fc6879b
JM
1988/**
1989 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1990 * @hapd_iface: Pointer to interface data
1991 * @conf: Pointer to per-interface configuration
1992 * @bss: Pointer to per-BSS configuration for this BSS
1993 * Returns: Pointer to allocated BSS data
1994 *
1995 * This function is used to allocate per-BSS data structure. This data will be
1996 * freed after hostapd_cleanup() is called for it during interface
1997 * deinitialization.
1998 */
b6a7859d 1999struct hostapd_data *
6fc6879b
JM
2000hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2001 struct hostapd_config *conf,
2002 struct hostapd_bss_config *bss)
2003{
2004 struct hostapd_data *hapd;
2005
2006 hapd = os_zalloc(sizeof(*hapd));
2007 if (hapd == NULL)
2008 return NULL;
2009
d24df7c3 2010 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
6fc6879b
JM
2011 hapd->iconf = conf;
2012 hapd->conf = bss;
2013 hapd->iface = hapd_iface;
db5e53cb
JM
2014 if (conf)
2015 hapd->driver = conf->driver;
9e7d033e 2016 hapd->ctrl_sock = -1;
56885eec 2017 dl_list_init(&hapd->ctrl_dst);
f2f8616e 2018 dl_list_init(&hapd->nr_db);
91d91abf 2019 hapd->dhcp_sock = -1;
c5fee160
MB
2020#ifdef CONFIG_IEEE80211R_AP
2021 dl_list_init(&hapd->l2_queue);
50bd8e0a 2022 dl_list_init(&hapd->l2_oui_queue);
c5fee160 2023#endif /* CONFIG_IEEE80211R_AP */
6fc6879b
JM
2024
2025 return hapd;
6fc6879b
JM
2026}
2027
2028
54246f8d
JM
2029static void hostapd_bss_deinit(struct hostapd_data *hapd)
2030{
9c10be3f
JM
2031 if (!hapd)
2032 return;
54246f8d
JM
2033 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
2034 hapd->conf->iface);
438e1333 2035 hostapd_bss_deinit_no_free(hapd);
39c3bfcd 2036 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
54246f8d
JM
2037 hostapd_cleanup(hapd);
2038}
2039
2040
5c333467 2041void hostapd_interface_deinit(struct hostapd_iface *iface)
5fa30f32 2042{
390e489c 2043 int j;
5fa30f32 2044
747c85f9 2045 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
5fa30f32
JM
2046 if (iface == NULL)
2047 return;
2048
39c3bfcd
JM
2049 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2050
9c47f6a2
PX
2051#ifdef CONFIG_IEEE80211N
2052#ifdef NEED_AP_MLME
5f0bca77 2053 hostapd_stop_setup_timers(iface);
9c47f6a2
PX
2054 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2055#endif /* NEED_AP_MLME */
2056#endif /* CONFIG_IEEE80211N */
f0793bf1
JM
2057 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2058 iface->wait_channel_update = 0;
2059
6959145b
AN
2060#ifdef CONFIG_FST
2061 if (iface->fst) {
2062 fst_detach(iface->fst);
2063 iface->fst = NULL;
2064 }
2065#endif /* CONFIG_FST */
2066
9c10be3f
JM
2067 for (j = iface->num_bss - 1; j >= 0; j--) {
2068 if (!iface->bss)
2069 break;
54246f8d 2070 hostapd_bss_deinit(iface->bss[j]);
9c10be3f 2071 }
f7c47833
JM
2072}
2073
2074
2075void hostapd_interface_free(struct hostapd_iface *iface)
2076{
2077 size_t j;
747c85f9
JM
2078 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2079 for (j = 0; j < iface->num_bss; j++) {
9c10be3f
JM
2080 if (!iface->bss)
2081 break;
747c85f9
JM
2082 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2083 __func__, iface->bss[j]);
5fa30f32 2084 os_free(iface->bss[j]);
747c85f9 2085 }
5fa30f32
JM
2086 hostapd_cleanup_iface(iface);
2087}
fa16028d
JM
2088
2089
5e993390
JM
2090struct hostapd_iface * hostapd_alloc_iface(void)
2091{
2092 struct hostapd_iface *hapd_iface;
2093
2094 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2095 if (!hapd_iface)
2096 return NULL;
2097
2098 dl_list_init(&hapd_iface->sta_seen);
2099
2100 return hapd_iface;
2101}
2102
2103
66936c6a
KP
2104/**
2105 * hostapd_init - Allocate and initialize per-interface data
2106 * @config_file: Path to the configuration file
2107 * Returns: Pointer to the allocated interface data or %NULL on failure
2108 *
2109 * This function is used to allocate main data structures for per-interface
2110 * data. The allocated data buffer will be freed by calling
2111 * hostapd_cleanup_iface().
2112 */
2113struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2114 const char *config_file)
2115{
2116 struct hostapd_iface *hapd_iface = NULL;
2117 struct hostapd_config *conf = NULL;
2118 struct hostapd_data *hapd;
2119 size_t i;
2120
5e993390 2121 hapd_iface = hostapd_alloc_iface();
66936c6a
KP
2122 if (hapd_iface == NULL)
2123 goto fail;
2124
2125 hapd_iface->config_fname = os_strdup(config_file);
2126 if (hapd_iface->config_fname == NULL)
2127 goto fail;
2128
2129 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2130 if (conf == NULL)
2131 goto fail;
2132 hapd_iface->conf = conf;
2133
2134 hapd_iface->num_bss = conf->num_bss;
2135 hapd_iface->bss = os_calloc(conf->num_bss,
2136 sizeof(struct hostapd_data *));
2137 if (hapd_iface->bss == NULL)
2138 goto fail;
2139
2140 for (i = 0; i < conf->num_bss; i++) {
2141 hapd = hapd_iface->bss[i] =
2142 hostapd_alloc_bss_data(hapd_iface, conf,
2143 conf->bss[i]);
2144 if (hapd == NULL)
2145 goto fail;
2146 hapd->msg_ctx = hapd;
2147 }
2148
2149 return hapd_iface;
2150
2151fail:
2152 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2153 config_file);
2154 if (conf)
2155 hostapd_config_free(conf);
2156 if (hapd_iface) {
2157 os_free(hapd_iface->config_fname);
2158 os_free(hapd_iface->bss);
747c85f9
JM
2159 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2160 __func__, hapd_iface);
66936c6a
KP
2161 os_free(hapd_iface);
2162 }
2163 return NULL;
2164}
2165
2166
2e2fff37
KP
2167static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2168{
2169 size_t i, j;
2170
2171 for (i = 0; i < interfaces->count; i++) {
2172 struct hostapd_iface *iface = interfaces->iface[i];
2173 for (j = 0; j < iface->num_bss; j++) {
2174 struct hostapd_data *hapd = iface->bss[j];
2175 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2176 return 1;
2177 }
2178 }
2179
2180 return 0;
2181}
2182
2183
0dfd2c61
JM
2184/**
2185 * hostapd_interface_init_bss - Read configuration file and init BSS data
2186 *
2187 * This function is used to parse configuration file for a BSS. This BSS is
2188 * added to an existing interface sharing the same radio (if any) or a new
2189 * interface is created if this is the first interface on a radio. This
2190 * allocate memory for the BSS. No actual driver operations are started.
2191 *
2192 * This is similar to hostapd_interface_init(), but for a case where the
2193 * configuration is used to add a single BSS instead of all BSSes for a radio.
2194 */
a1fb5692
KP
2195struct hostapd_iface *
2196hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2197 const char *config_fname, int debug)
2198{
2199 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2200 struct hostapd_data *hapd;
2201 int k;
2202 size_t i, bss_idx;
2203
2204 if (!phy || !*phy)
2205 return NULL;
2206
2207 for (i = 0; i < interfaces->count; i++) {
2208 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2209 iface = interfaces->iface[i];
2210 break;
2211 }
2212 }
2213
fee947bf 2214 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
a1fb5692
KP
2215 config_fname, phy, iface ? "" : " --> new PHY");
2216 if (iface) {
2217 struct hostapd_config *conf;
2218 struct hostapd_bss_config **tmp_conf;
2219 struct hostapd_data **tmp_bss;
2220 struct hostapd_bss_config *bss;
2e2fff37 2221 const char *ifname;
a1fb5692
KP
2222
2223 /* Add new BSS to existing iface */
2224 conf = interfaces->config_read_cb(config_fname);
2225 if (conf == NULL)
2226 return NULL;
2227 if (conf->num_bss > 1) {
2228 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2229 hostapd_config_free(conf);
2230 return NULL;
2231 }
2232
2e2fff37
KP
2233 ifname = conf->bss[0]->iface;
2234 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2235 wpa_printf(MSG_ERROR,
2236 "Interface name %s already in use", ifname);
2237 hostapd_config_free(conf);
2238 return NULL;
2239 }
2240
a1fb5692
KP
2241 tmp_conf = os_realloc_array(
2242 iface->conf->bss, iface->conf->num_bss + 1,
2243 sizeof(struct hostapd_bss_config *));
2244 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2245 sizeof(struct hostapd_data *));
2246 if (tmp_bss)
2247 iface->bss = tmp_bss;
2248 if (tmp_conf) {
2249 iface->conf->bss = tmp_conf;
2250 iface->conf->last_bss = tmp_conf[0];
2251 }
2252 if (tmp_bss == NULL || tmp_conf == NULL) {
2253 hostapd_config_free(conf);
2254 return NULL;
2255 }
2256 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2257 iface->conf->num_bss++;
2258
2259 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2260 if (hapd == NULL) {
2261 iface->conf->num_bss--;
2262 hostapd_config_free(conf);
2263 return NULL;
2264 }
2265 iface->conf->last_bss = bss;
2266 iface->bss[iface->num_bss] = hapd;
2267 hapd->msg_ctx = hapd;
2268
2269 bss_idx = iface->num_bss++;
2270 conf->num_bss--;
2271 conf->bss[0] = NULL;
2272 hostapd_config_free(conf);
2273 } else {
2274 /* Add a new iface with the first BSS */
2275 new_iface = iface = hostapd_init(interfaces, config_fname);
2276 if (!iface)
2277 return NULL;
2278 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2279 iface->interfaces = interfaces;
2280 bss_idx = 0;
2281 }
2282
2283 for (k = 0; k < debug; k++) {
2284 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2285 iface->bss[bss_idx]->conf->logger_stdout_level--;
2286 }
2287
2288 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2289 !hostapd_drv_none(iface->bss[bss_idx])) {
2290 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2291 config_fname);
2292 if (new_iface)
2293 hostapd_interface_deinit_free(new_iface);
2294 return NULL;
2295 }
2296
2297 return iface;
2298}
2299
2300
75545652
SP
2301void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2302{
2303 const struct wpa_driver_ops *driver;
2304 void *drv_priv;
747c85f9
JM
2305
2306 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
75545652
SP
2307 if (iface == NULL)
2308 return;
747c85f9
JM
2309 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2310 __func__, (unsigned int) iface->num_bss,
2311 (unsigned int) iface->conf->num_bss);
75545652
SP
2312 driver = iface->bss[0]->driver;
2313 drv_priv = iface->bss[0]->drv_priv;
2314 hostapd_interface_deinit(iface);
747c85f9
JM
2315 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2316 __func__, driver, drv_priv);
d92bdf96 2317 if (driver && driver->hapd_deinit && drv_priv) {
75545652 2318 driver->hapd_deinit(drv_priv);
d92bdf96
JM
2319 iface->bss[0]->drv_priv = NULL;
2320 }
75545652
SP
2321 hostapd_interface_free(iface);
2322}
2323
2324
4d1e38be
JM
2325static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2326 void *drv_priv,
2327 struct hostapd_iface *hapd_iface)
2328{
2329 size_t j;
2330
2331 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2332 __func__, driver, drv_priv);
2333 if (driver && driver->hapd_deinit && drv_priv) {
2334 driver->hapd_deinit(drv_priv);
2335 for (j = 0; j < hapd_iface->num_bss; j++) {
2336 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2337 __func__, (int) j,
2338 hapd_iface->bss[j]->drv_priv);
2339 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2340 hapd_iface->bss[j]->drv_priv = NULL;
2341 }
2342 }
2343}
2344
2345
75545652
SP
2346int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2347{
5d67bf15
JM
2348 size_t j;
2349
75545652
SP
2350 if (hapd_iface->bss[0]->drv_priv != NULL) {
2351 wpa_printf(MSG_ERROR, "Interface %s already enabled",
ebd79f07 2352 hapd_iface->conf->bss[0]->iface);
75545652
SP
2353 return -1;
2354 }
2355
2356 wpa_printf(MSG_DEBUG, "Enable interface %s",
ebd79f07 2357 hapd_iface->conf->bss[0]->iface);
75545652 2358
5d67bf15
JM
2359 for (j = 0; j < hapd_iface->num_bss; j++)
2360 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
08081ad8
JM
2361 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2362 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2363 return -1;
2364 }
2365
75545652
SP
2366 if (hapd_iface->interfaces == NULL ||
2367 hapd_iface->interfaces->driver_init == NULL ||
71cdf6b6
JM
2368 hapd_iface->interfaces->driver_init(hapd_iface))
2369 return -1;
2370
2371 if (hostapd_setup_interface(hapd_iface)) {
4d1e38be
JM
2372 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2373 hapd_iface->bss[0]->drv_priv,
2374 hapd_iface);
75545652
SP
2375 return -1;
2376 }
71cdf6b6 2377
75545652
SP
2378 return 0;
2379}
2380
2381
2382int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2383{
2384 size_t j;
2385
2386 wpa_printf(MSG_DEBUG, "Reload interface %s",
ebd79f07 2387 hapd_iface->conf->bss[0]->iface);
6f2db2fb 2388 for (j = 0; j < hapd_iface->num_bss; j++)
5d67bf15 2389 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
08081ad8 2390 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
6f2db2fb
JM
2391 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2392 return -1;
2393 }
9f104b03
JM
2394 hostapd_clear_old(hapd_iface);
2395 for (j = 0; j < hapd_iface->num_bss; j++)
75545652 2396 hostapd_reload_bss(hapd_iface->bss[j]);
9f104b03 2397
75545652
SP
2398 return 0;
2399}
2400
2401
2402int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2403{
2404 size_t j;
75545652
SP
2405 const struct wpa_driver_ops *driver;
2406 void *drv_priv;
2407
2408 if (hapd_iface == NULL)
2409 return -1;
3fbd036e
MK
2410
2411 if (hapd_iface->bss[0]->drv_priv == NULL) {
2412 wpa_printf(MSG_INFO, "Interface %s already disabled",
2413 hapd_iface->conf->bss[0]->iface);
2414 return -1;
2415 }
2416
7d6d7370 2417 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
75545652
SP
2418 driver = hapd_iface->bss[0]->driver;
2419 drv_priv = hapd_iface->bss[0]->drv_priv;
2420
354c903f
MB
2421 hapd_iface->driver_ap_teardown =
2422 !!(hapd_iface->drv_flags &
2423 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2424
2425 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
75545652
SP
2426 for (j = 0; j < hapd_iface->num_bss; j++) {
2427 struct hostapd_data *hapd = hapd_iface->bss[j];
438e1333 2428 hostapd_bss_deinit_no_free(hapd);
75545652
SP
2429 hostapd_free_hapd_data(hapd);
2430 }
2431
4d1e38be 2432 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
75545652
SP
2433
2434 /* From hostapd_cleanup_iface: These were initialized in
2435 * hostapd_setup_interface and hostapd_setup_interface_complete
2436 */
2437 hostapd_cleanup_iface_partial(hapd_iface);
75545652 2438
0249c125
JM
2439 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2440 hapd_iface->bss[0]->conf->iface);
e1c5faf0 2441 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
75545652
SP
2442 return 0;
2443}
2444
06bb8c62
SP
2445
2446static struct hostapd_iface *
2447hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2448{
2449 struct hostapd_iface **iface, *hapd_iface;
2450
2451 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2452 sizeof(struct hostapd_iface *));
2453 if (iface == NULL)
2454 return NULL;
2455 interfaces->iface = iface;
2456 hapd_iface = interfaces->iface[interfaces->count] =
5e993390 2457 hostapd_alloc_iface();
06bb8c62
SP
2458 if (hapd_iface == NULL) {
2459 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2460 "the interface", __func__);
2461 return NULL;
2462 }
2463 interfaces->count++;
2464 hapd_iface->interfaces = interfaces;
2465
2466 return hapd_iface;
2467}
2468
2469
2470static struct hostapd_config *
2471hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
fab51186 2472 const char *ctrl_iface, const char *driver)
06bb8c62
SP
2473{
2474 struct hostapd_bss_config *bss;
2475 struct hostapd_config *conf;
2476
2477 /* Allocates memory for bss and conf */
2478 conf = hostapd_config_defaults();
2479 if (conf == NULL) {
2480 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2481 "configuration", __func__);
2482 return NULL;
2483 }
2484
fab51186
P
2485 if (driver) {
2486 int j;
2487
2488 for (j = 0; wpa_drivers[j]; j++) {
2489 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2490 conf->driver = wpa_drivers[j];
2491 goto skip;
2492 }
2493 }
2494
2495 wpa_printf(MSG_ERROR,
2496 "Invalid/unknown driver '%s' - registering the default driver",
2497 driver);
2498 }
2499
06bb8c62
SP
2500 conf->driver = wpa_drivers[0];
2501 if (conf->driver == NULL) {
2502 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2503 hostapd_config_free(conf);
2504 return NULL;
2505 }
2506
fab51186 2507skip:
ebd79f07 2508 bss = conf->last_bss = conf->bss[0];
06bb8c62
SP
2509
2510 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2511 bss->ctrl_interface = os_strdup(ctrl_iface);
2512 if (bss->ctrl_interface == NULL) {
2513 hostapd_config_free(conf);
2514 return NULL;
2515 }
2516
2517 /* Reading configuration file skipped, will be done in SET!
2518 * From reading the configuration till the end has to be done in
2519 * SET
2520 */
2521 return conf;
2522}
2523
2524
28016592
JM
2525static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2526 struct hostapd_config *conf)
06bb8c62
SP
2527{
2528 size_t i;
06bb8c62
SP
2529 struct hostapd_data *hapd;
2530
faebdeaa 2531 hapd_iface->bss = os_calloc(conf->num_bss,
06bb8c62
SP
2532 sizeof(struct hostapd_data *));
2533 if (hapd_iface->bss == NULL)
28016592 2534 return -1;
06bb8c62
SP
2535
2536 for (i = 0; i < conf->num_bss; i++) {
2537 hapd = hapd_iface->bss[i] =
ebd79f07 2538 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
28016592
JM
2539 if (hapd == NULL) {
2540 while (i > 0) {
2541 i--;
2542 os_free(hapd_iface->bss[i]);
2543 hapd_iface->bss[i] = NULL;
2544 }
2545 os_free(hapd_iface->bss);
2546 hapd_iface->bss = NULL;
2547 return -1;
2548 }
06bb8c62
SP
2549 hapd->msg_ctx = hapd;
2550 }
2551
28016592
JM
2552 hapd_iface->conf = conf;
2553 hapd_iface->num_bss = conf->num_bss;
06bb8c62 2554
28016592 2555 return 0;
06bb8c62
SP
2556}
2557
2558
2559int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2560{
2561 struct hostapd_config *conf = NULL;
2e2fff37
KP
2562 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2563 struct hostapd_data *hapd;
06bb8c62 2564 char *ptr;
2e2fff37
KP
2565 size_t i, j;
2566 const char *conf_file = NULL, *phy_name = NULL;
2567
2568 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2569 char *pos;
2570 phy_name = buf + 11;
2571 pos = os_strchr(phy_name, ':');
2572 if (!pos)
2573 return -1;
2574 *pos++ = '\0';
2575 conf_file = pos;
2576 if (!os_strlen(conf_file))
2577 return -1;
2578
2579 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2580 conf_file, 0);
2581 if (!hapd_iface)
2582 return -1;
2583 for (j = 0; j < interfaces->count; j++) {
2584 if (interfaces->iface[j] == hapd_iface)
2585 break;
2586 }
2587 if (j == interfaces->count) {
2588 struct hostapd_iface **tmp;
2589 tmp = os_realloc_array(interfaces->iface,
2590 interfaces->count + 1,
2591 sizeof(struct hostapd_iface *));
2592 if (!tmp) {
2593 hostapd_interface_deinit_free(hapd_iface);
2594 return -1;
2595 }
2596 interfaces->iface = tmp;
2597 interfaces->iface[interfaces->count++] = hapd_iface;
2598 new_iface = hapd_iface;
2599 }
2600
2601 if (new_iface) {
28016592 2602 if (interfaces->driver_init(hapd_iface))
2e2fff37 2603 goto fail;
71f1d1e5
JM
2604
2605 if (hostapd_setup_interface(hapd_iface)) {
71f1d1e5
JM
2606 hostapd_deinit_driver(
2607 hapd_iface->bss[0]->driver,
2608 hapd_iface->bss[0]->drv_priv,
2609 hapd_iface);
2610 goto fail;
2611 }
2e2fff37
KP
2612 } else {
2613 /* Assign new BSS with bss[0]'s driver info */
2614 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2615 hapd->driver = hapd_iface->bss[0]->driver;
2616 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2617 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2618 ETH_ALEN);
2619
ad08e141 2620 if (start_ctrl_iface_bss(hapd) < 0 ||
5e1a4565
JM
2621 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2622 hostapd_setup_bss(hapd, -1))) {
c9d9ee94 2623 hostapd_cleanup(hapd);
b908c50a 2624 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2e2fff37
KP
2625 hapd_iface->conf->num_bss--;
2626 hapd_iface->num_bss--;
747c85f9
JM
2627 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2628 __func__, hapd, hapd->conf->iface);
e10422c0
JM
2629 hostapd_config_free_bss(hapd->conf);
2630 hapd->conf = NULL;
2e2fff37
KP
2631 os_free(hapd);
2632 return -1;
2633 }
2634 }
2635 return 0;
2636 }
06bb8c62
SP
2637
2638 ptr = os_strchr(buf, ' ');
2639 if (ptr == NULL)
2640 return -1;
2641 *ptr++ = '\0';
2642
ed1bf011
JM
2643 if (os_strncmp(ptr, "config=", 7) == 0)
2644 conf_file = ptr + 7;
2645
06bb8c62 2646 for (i = 0; i < interfaces->count; i++) {
ebd79f07 2647 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
06bb8c62
SP
2648 buf)) {
2649 wpa_printf(MSG_INFO, "Cannot add interface - it "
2650 "already exists");
2651 return -1;
2652 }
2653 }
2654
2655 hapd_iface = hostapd_iface_alloc(interfaces);
2656 if (hapd_iface == NULL) {
2657 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2658 "for interface", __func__);
2659 goto fail;
2660 }
28016592 2661 new_iface = hapd_iface;
06bb8c62 2662
ed1bf011
JM
2663 if (conf_file && interfaces->config_read_cb) {
2664 conf = interfaces->config_read_cb(conf_file);
2665 if (conf && conf->bss)
ebd79f07
JM
2666 os_strlcpy(conf->bss[0]->iface, buf,
2667 sizeof(conf->bss[0]->iface));
fab51186
P
2668 } else {
2669 char *driver = os_strchr(ptr, ' ');
2670
2671 if (driver)
2672 *driver++ = '\0';
2673 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2674 }
2675
ed1bf011 2676 if (conf == NULL || conf->bss == NULL) {
06bb8c62
SP
2677 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2678 "for configuration", __func__);
2679 goto fail;
2680 }
2681
28016592 2682 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
06bb8c62
SP
2683 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2684 "for hostapd", __func__);
2685 goto fail;
2686 }
c1c07dcb 2687 conf = NULL;
06bb8c62 2688
e4ba0315 2689 if (start_ctrl_iface(hapd_iface) < 0)
06bb8c62 2690 goto fail;
e4ba0315 2691
c1c07dcb
JM
2692 wpa_printf(MSG_INFO, "Add interface '%s'",
2693 hapd_iface->conf->bss[0]->iface);
06bb8c62
SP
2694
2695 return 0;
2696
2697fail:
2698 if (conf)
2699 hostapd_config_free(conf);
2700 if (hapd_iface) {
33b0b330 2701 if (hapd_iface->bss) {
486d2ff0
JM
2702 for (i = 0; i < hapd_iface->num_bss; i++) {
2703 hapd = hapd_iface->bss[i];
7b6e8157
JM
2704 if (!hapd)
2705 continue;
2706 if (hapd_iface->interfaces &&
486d2ff0
JM
2707 hapd_iface->interfaces->ctrl_iface_deinit)
2708 hapd_iface->interfaces->
2709 ctrl_iface_deinit(hapd);
747c85f9
JM
2710 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2711 __func__, hapd_iface->bss[i],
7b6e8157 2712 hapd->conf->iface);
71f1d1e5 2713 hostapd_cleanup(hapd);
7b6e8157
JM
2714 os_free(hapd);
2715 hapd_iface->bss[i] = NULL;
486d2ff0 2716 }
33b0b330 2717 os_free(hapd_iface->bss);
71f1d1e5 2718 hapd_iface->bss = NULL;
33b0b330 2719 }
28016592
JM
2720 if (new_iface) {
2721 interfaces->count--;
2722 interfaces->iface[interfaces->count] = NULL;
2723 }
71f1d1e5 2724 hostapd_cleanup_iface(hapd_iface);
06bb8c62
SP
2725 }
2726 return -1;
2727}
2728
2729
55920658
JM
2730static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2731{
55920658
JM
2732 size_t i;
2733
cdf3fb1f 2734 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
55920658 2735
cdf3fb1f
JM
2736 /* Remove hostapd_data only if it has already been initialized */
2737 if (idx < iface->num_bss) {
2738 struct hostapd_data *hapd = iface->bss[idx];
55920658 2739
54246f8d 2740 hostapd_bss_deinit(hapd);
747c85f9
JM
2741 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2742 __func__, hapd, hapd->conf->iface);
cdf3fb1f 2743 hostapd_config_free_bss(hapd->conf);
28016592 2744 hapd->conf = NULL;
cdf3fb1f
JM
2745 os_free(hapd);
2746
2747 iface->num_bss--;
2748
2749 for (i = idx; i < iface->num_bss; i++)
2750 iface->bss[i] = iface->bss[i + 1];
2751 } else {
2752 hostapd_config_free_bss(iface->conf->bss[idx]);
2753 iface->conf->bss[idx] = NULL;
2754 }
55920658
JM
2755
2756 iface->conf->num_bss--;
cdf3fb1f 2757 for (i = idx; i < iface->conf->num_bss; i++)
55920658
JM
2758 iface->conf->bss[i] = iface->conf->bss[i + 1];
2759
2760 return 0;
2761}
2762
2763
06bb8c62
SP
2764int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2765{
2766 struct hostapd_iface *hapd_iface;
55920658 2767 size_t i, j, k = 0;
06bb8c62
SP
2768
2769 for (i = 0; i < interfaces->count; i++) {
2770 hapd_iface = interfaces->iface[i];
2771 if (hapd_iface == NULL)
2772 return -1;
2f99d907 2773 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
06bb8c62 2774 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
354c903f
MB
2775 hapd_iface->driver_ap_teardown =
2776 !!(hapd_iface->drv_flags &
2777 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2778
06bb8c62
SP
2779 hostapd_interface_deinit_free(hapd_iface);
2780 k = i;
2781 while (k < (interfaces->count - 1)) {
2782 interfaces->iface[k] =
2783 interfaces->iface[k + 1];
2784 k++;
2785 }
2786 interfaces->count--;
2787 return 0;
2788 }
55920658
JM
2789
2790 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
354c903f
MB
2791 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2792 hapd_iface->driver_ap_teardown =
2793 !(hapd_iface->drv_flags &
2794 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
55920658 2795 return hostapd_remove_bss(hapd_iface, j);
354c903f 2796 }
55920658 2797 }
06bb8c62
SP
2798 }
2799 return -1;
2800}
2801
75545652 2802
a2de634d
JM
2803/**
2804 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2805 * @hapd: Pointer to BSS data
2806 * @sta: Pointer to the associated STA data
2807 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2808 *
2809 * This function will be called whenever a station associates with the AP. It
2810 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2811 * from drv_callbacks.c based on driver events for drivers that take care of
2812 * management frames (IEEE 802.11 authentication and association) internally.
2813 */
2814void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2815 int reassoc)
2816{
2817 if (hapd->tkip_countermeasures) {
51e2a27a
JM
2818 hostapd_drv_sta_deauth(hapd, sta->addr,
2819 WLAN_REASON_MICHAEL_MIC_FAILURE);
a2de634d
JM
2820 return;
2821 }
2822
0aef3ec8 2823 hostapd_prune_associations(hapd, sta->addr);
9e8fde21 2824 ap_sta_clear_disconnect_timeouts(hapd, sta);
a2de634d
JM
2825
2826 /* IEEE 802.11F (IAPP) */
2827 if (hapd->conf->ieee802_11f)
2828 iapp_new_station(hapd->iapp, sta);
2829
aefb53bd
JM
2830#ifdef CONFIG_P2P
2831 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2832 sta->no_p2p_set = 1;
2833 hapd->num_sta_no_p2p++;
2834 if (hapd->num_sta_no_p2p == 1)
2835 hostapd_p2p_non_p2p_sta_connected(hapd);
2836 }
2837#endif /* CONFIG_P2P */
2838
a2de634d
JM
2839 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2840 * IEEE 802.1X/WPA code will start accounting after the station has
2841 * been authorized. */
95faa36a 2842 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
113318ad 2843 ap_sta_set_authorized(hapd, sta, 1);
b3493fa1 2844 os_get_reltime(&sta->connected_time);
a2de634d 2845 accounting_sta_start(hapd, sta);
39b1572c 2846 }
a2de634d
JM
2847
2848 /* Start IEEE 802.1X authentication process for new stations */
2849 ieee802_1x_new_station(hapd, sta);
2850 if (reassoc) {
2851 if (sta->auth_alg != WLAN_AUTH_FT &&
2852 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2853 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2854 } else
2855 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
a625ff60 2856
08032c74
ST
2857 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
2858 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
2859 wpa_printf(MSG_DEBUG,
2860 "%s: %s: canceled wired ap_handle_timer timeout for "
2861 MACSTR,
2862 hapd->conf->iface, __func__,
2863 MAC2STR(sta->addr));
2864 }
2865 } else if (!(hapd->iface->drv_flags &
2866 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
03269d55
JM
2867 wpa_printf(MSG_DEBUG,
2868 "%s: %s: reschedule ap_handle_timer timeout for "
2869 MACSTR " (%d seconds - ap_max_inactivity)",
2870 hapd->conf->iface, __func__, MAC2STR(sta->addr),
336167c8
MSS
2871 hapd->conf->ap_max_inactivity);
2872 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2873 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2874 ap_handle_timer, hapd, sta);
2875 }
a2de634d 2876}
e1c5faf0
JM
2877
2878
5ae6449c 2879const char * hostapd_state_text(enum hostapd_iface_state s)
e1c5faf0
JM
2880{
2881 switch (s) {
2882 case HAPD_IFACE_UNINITIALIZED:
2883 return "UNINITIALIZED";
2884 case HAPD_IFACE_DISABLED:
2885 return "DISABLED";
2886 case HAPD_IFACE_COUNTRY_UPDATE:
2887 return "COUNTRY_UPDATE";
2888 case HAPD_IFACE_ACS:
2889 return "ACS";
2890 case HAPD_IFACE_HT_SCAN:
2891 return "HT_SCAN";
2892 case HAPD_IFACE_DFS:
2893 return "DFS";
2894 case HAPD_IFACE_ENABLED:
2895 return "ENABLED";
2896 }
2897
2898 return "UNKNOWN";
2899}
2900
2901
2902void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2903{
2904 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
9c10be3f
JM
2905 iface->conf ? iface->conf->bss[0]->iface : "N/A",
2906 hostapd_state_text(iface->state), hostapd_state_text(s));
e1c5faf0
JM
2907 iface->state = s;
2908}
bf281c12
AO
2909
2910
4e0ab656
IP
2911int hostapd_csa_in_progress(struct hostapd_iface *iface)
2912{
2913 unsigned int i;
2914
2915 for (i = 0; i < iface->num_bss; i++)
2916 if (iface->bss[i]->csa_in_progress)
2917 return 1;
2918 return 0;
2919}
2920
2921
bf281c12
AO
2922#ifdef NEED_AP_MLME
2923
2924static void free_beacon_data(struct beacon_data *beacon)
2925{
2926 os_free(beacon->head);
2927 beacon->head = NULL;
2928 os_free(beacon->tail);
2929 beacon->tail = NULL;
2930 os_free(beacon->probe_resp);
2931 beacon->probe_resp = NULL;
2932 os_free(beacon->beacon_ies);
2933 beacon->beacon_ies = NULL;
2934 os_free(beacon->proberesp_ies);
2935 beacon->proberesp_ies = NULL;
2936 os_free(beacon->assocresp_ies);
2937 beacon->assocresp_ies = NULL;
2938}
2939
2940
6782b684 2941static int hostapd_build_beacon_data(struct hostapd_data *hapd,
bf281c12
AO
2942 struct beacon_data *beacon)
2943{
2944 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2945 struct wpa_driver_ap_params params;
2946 int ret;
bf281c12 2947
80ed037f 2948 os_memset(beacon, 0, sizeof(*beacon));
bf281c12
AO
2949 ret = ieee802_11_build_ap_params(hapd, &params);
2950 if (ret < 0)
2951 return ret;
2952
2953 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2954 &proberesp_extra,
2955 &assocresp_extra);
2956 if (ret)
2957 goto free_ap_params;
2958
2959 ret = -1;
a1f11e34 2960 beacon->head = os_memdup(params.head, params.head_len);
bf281c12
AO
2961 if (!beacon->head)
2962 goto free_ap_extra_ies;
2963
bf281c12
AO
2964 beacon->head_len = params.head_len;
2965
a1f11e34 2966 beacon->tail = os_memdup(params.tail, params.tail_len);
bf281c12
AO
2967 if (!beacon->tail)
2968 goto free_beacon;
2969
bf281c12
AO
2970 beacon->tail_len = params.tail_len;
2971
2972 if (params.proberesp != NULL) {
a1f11e34
JB
2973 beacon->probe_resp = os_memdup(params.proberesp,
2974 params.proberesp_len);
bf281c12
AO
2975 if (!beacon->probe_resp)
2976 goto free_beacon;
2977
bf281c12
AO
2978 beacon->probe_resp_len = params.proberesp_len;
2979 }
2980
2981 /* copy the extra ies */
2982 if (beacon_extra) {
a1f11e34
JB
2983 beacon->beacon_ies = os_memdup(beacon_extra->buf,
2984 wpabuf_len(beacon_extra));
bf281c12
AO
2985 if (!beacon->beacon_ies)
2986 goto free_beacon;
2987
bf281c12
AO
2988 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2989 }
2990
2991 if (proberesp_extra) {
a1f11e34
JB
2992 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
2993 wpabuf_len(proberesp_extra));
bf281c12
AO
2994 if (!beacon->proberesp_ies)
2995 goto free_beacon;
2996
bf281c12
AO
2997 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2998 }
2999
3000 if (assocresp_extra) {
a1f11e34
JB
3001 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3002 wpabuf_len(assocresp_extra));
bf281c12
AO
3003 if (!beacon->assocresp_ies)
3004 goto free_beacon;
3005
bf281c12
AO
3006 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3007 }
3008
3009 ret = 0;
3010free_beacon:
3011 /* if the function fails, the caller should not free beacon data */
3012 if (ret)
3013 free_beacon_data(beacon);
3014
3015free_ap_extra_ies:
3016 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3017 assocresp_extra);
3018free_ap_params:
3019 ieee802_11_free_ap_params(&params);
3020 return ret;
3021}
3022
3023
3024/*
982896ff
AO
3025 * TODO: This flow currently supports only changing channel and width within
3026 * the same hw_mode. Any other changes to MAC parameters or provided settings
3027 * are not supported.
bf281c12
AO
3028 */
3029static int hostapd_change_config_freq(struct hostapd_data *hapd,
3030 struct hostapd_config *conf,
3031 struct hostapd_freq_params *params,
3032 struct hostapd_freq_params *old_params)
3033{
3034 int channel;
3035
3036 if (!params->channel) {
3037 /* check if the new channel is supported by hw */
5841958f 3038 params->channel = hostapd_hw_get_channel(hapd, params->freq);
bf281c12
AO
3039 }
3040
5841958f
MK
3041 channel = params->channel;
3042 if (!channel)
3043 return -1;
3044
bf281c12 3045 /* if a pointer to old_params is provided we save previous state */
982896ff
AO
3046 if (old_params &&
3047 hostapd_set_freq_params(old_params, conf->hw_mode,
3048 hostapd_hw_get_freq(hapd, conf->channel),
3049 conf->channel, conf->ieee80211n,
3050 conf->ieee80211ac,
3051 conf->secondary_channel,
3052 conf->vht_oper_chwidth,
3053 conf->vht_oper_centr_freq_seg0_idx,
3054 conf->vht_oper_centr_freq_seg1_idx,
3055 conf->vht_capab))
3056 return -1;
3057
3058 switch (params->bandwidth) {
3059 case 0:
3060 case 20:
3061 case 40:
3062 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
3063 break;
3064 case 80:
3065 if (params->center_freq2)
3066 conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
3067 else
3068 conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
3069 break;
3070 case 160:
3071 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
3072 break;
3073 default:
3074 return -1;
bf281c12
AO
3075 }
3076
3077 conf->channel = channel;
3078 conf->ieee80211n = params->ht_enabled;
3079 conf->secondary_channel = params->sec_channel_offset;
d308a44f
LC
3080 ieee80211_freq_to_chan(params->center_freq1,
3081 &conf->vht_oper_centr_freq_seg0_idx);
3082 ieee80211_freq_to_chan(params->center_freq2,
3083 &conf->vht_oper_centr_freq_seg1_idx);
bf281c12
AO
3084
3085 /* TODO: maybe call here hostapd_config_check here? */
3086
3087 return 0;
3088}
3089
3090
6782b684 3091static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
bf281c12
AO
3092 struct csa_settings *settings)
3093{
6782b684 3094 struct hostapd_iface *iface = hapd->iface;
bf281c12
AO
3095 struct hostapd_freq_params old_freq;
3096 int ret;
fa53d74c 3097 u8 chan, vht_bandwidth;
bf281c12
AO
3098
3099 os_memset(&old_freq, 0, sizeof(old_freq));
6782b684 3100 if (!iface || !iface->freq || hapd->csa_in_progress)
bf281c12
AO
3101 return -1;
3102
fa53d74c
AO
3103 switch (settings->freq_params.bandwidth) {
3104 case 80:
3105 if (settings->freq_params.center_freq2)
3106 vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
3107 else
3108 vht_bandwidth = VHT_CHANWIDTH_80MHZ;
3109 break;
3110 case 160:
3111 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
3112 break;
3113 default:
3114 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
3115 break;
3116 }
3117
7d82170a
LC
3118 if (ieee80211_freq_to_channel_ext(
3119 settings->freq_params.freq,
3120 settings->freq_params.sec_channel_offset,
fa53d74c 3121 vht_bandwidth,
7d82170a
LC
3122 &hapd->iface->cs_oper_class,
3123 &chan) == NUM_HOSTAPD_MODES) {
3124 wpa_printf(MSG_DEBUG,
3125 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
3126 settings->freq_params.freq,
3127 settings->freq_params.sec_channel_offset,
3128 settings->freq_params.vht_enabled);
3129 return -1;
3130 }
3131
3132 settings->freq_params.channel = chan;
3133
bf281c12
AO
3134 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3135 &settings->freq_params,
3136 &old_freq);
3137 if (ret)
3138 return ret;
3139
6782b684 3140 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
bf281c12
AO
3141
3142 /* change back the configuration */
3143 hostapd_change_config_freq(iface->bss[0], iface->conf,
3144 &old_freq, NULL);
3145
3146 if (ret)
3147 return ret;
3148
3149 /* set channel switch parameters for csa ie */
6782b684
MK
3150 hapd->cs_freq_params = settings->freq_params;
3151 hapd->cs_count = settings->cs_count;
3152 hapd->cs_block_tx = settings->block_tx;
bf281c12 3153
6782b684 3154 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
bf281c12
AO
3155 if (ret) {
3156 free_beacon_data(&settings->beacon_after);
3157 return ret;
3158 }
3159
366179d2
AO
3160 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3161 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
6315bfdb
AO
3162 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3163 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
bf281c12
AO
3164
3165 return 0;
3166}
3167
3168
3169void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3170{
6782b684
MK
3171 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3172 hapd->cs_count = 0;
3173 hapd->cs_block_tx = 0;
3174 hapd->cs_c_off_beacon = 0;
3175 hapd->cs_c_off_proberesp = 0;
3176 hapd->csa_in_progress = 0;
6315bfdb
AO
3177 hapd->cs_c_off_ecsa_beacon = 0;
3178 hapd->cs_c_off_ecsa_proberesp = 0;
bf281c12
AO
3179}
3180
3181
3182int hostapd_switch_channel(struct hostapd_data *hapd,
3183 struct csa_settings *settings)
3184{
3185 int ret;
d66873f5
AO
3186
3187 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3188 wpa_printf(MSG_INFO, "CSA is not supported");
3189 return -1;
3190 }
3191
6782b684 3192 ret = hostapd_fill_csa_settings(hapd, settings);
bf281c12
AO
3193 if (ret)
3194 return ret;
3195
3196 ret = hostapd_drv_switch_channel(hapd, settings);
3197 free_beacon_data(&settings->beacon_csa);
3198 free_beacon_data(&settings->beacon_after);
3199
3200 if (ret) {
3201 /* if we failed, clean cs parameters */
3202 hostapd_cleanup_cs_params(hapd);
3203 return ret;
3204 }
3205
6782b684 3206 hapd->csa_in_progress = 1;
bf281c12
AO
3207 return 0;
3208}
3209
5841958f
MK
3210
3211void
3212hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3213 const struct hostapd_freq_params *freq_params)
3214{
3215 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3216 unsigned int i;
3217
3218 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3219
3220 if (freq_params->center_freq1)
3221 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3222 if (freq_params->center_freq2)
3223 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3224
3225 switch (freq_params->bandwidth) {
3226 case 0:
3227 case 20:
3228 case 40:
3229 vht_bw = VHT_CHANWIDTH_USE_HT;
3230 break;
3231 case 80:
3232 if (freq_params->center_freq2)
3233 vht_bw = VHT_CHANWIDTH_80P80MHZ;
3234 else
3235 vht_bw = VHT_CHANWIDTH_80MHZ;
3236 break;
3237 case 160:
3238 vht_bw = VHT_CHANWIDTH_160MHZ;
3239 break;
3240 default:
3241 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3242 freq_params->bandwidth);
3243 break;
3244 }
3245
3246 iface->freq = freq_params->freq;
3247 iface->conf->channel = freq_params->channel;
3248 iface->conf->secondary_channel = freq_params->sec_channel_offset;
3249 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3250 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3251 iface->conf->vht_oper_chwidth = vht_bw;
3252 iface->conf->ieee80211n = freq_params->ht_enabled;
3253 iface->conf->ieee80211ac = freq_params->vht_enabled;
3254
3255 /*
3256 * cs_params must not be cleared earlier because the freq_params
3257 * argument may actually point to one of these.
3258 */
3259 for (i = 0; i < iface->num_bss; i++)
3260 hostapd_cleanup_cs_params(iface->bss[i]);
3261
3262 hostapd_disable_iface(iface);
3263 hostapd_enable_iface(iface);
3264}
3265
45e3fc72
RM
3266#endif /* NEED_AP_MLME */
3267
6959145b
AN
3268
3269struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3270 const char *ifname)
3271{
3272 size_t i, j;
3273
3274 for (i = 0; i < interfaces->count; i++) {
3275 struct hostapd_iface *iface = interfaces->iface[i];
3276
3277 for (j = 0; j < iface->num_bss; j++) {
3278 struct hostapd_data *hapd = iface->bss[j];
3279
3280 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3281 return hapd;
3282 }
3283 }
3284
3285 return NULL;
3286}
3287
3188aaba
JM
3288
3289void hostapd_periodic_iface(struct hostapd_iface *iface)
3290{
22fd2822
JM
3291 size_t i;
3292
de744892
JM
3293 ap_list_timer(iface);
3294
22fd2822
JM
3295 for (i = 0; i < iface->num_bss; i++) {
3296 struct hostapd_data *hapd = iface->bss[i];
3297
3298 if (!hapd->started)
3299 continue;
3300
3301#ifndef CONFIG_NO_RADIUS
3302 hostapd_acl_expire(hapd);
3303#endif /* CONFIG_NO_RADIUS */
3304 }
3188aaba 3305}