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