blob: 53162032bcdef8db71c03c0b856423aa5b6d3356 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2021, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "utils/includes.h"
10#ifdef CONFIG_SQLITE
11#include <sqlite3.h>
12#endif /* CONFIG_SQLITE */
13
14#include "utils/common.h"
15#include "utils/eloop.h"
16#include "utils/crc32.h"
17#include "common/ieee802_11_defs.h"
18#include "common/wpa_ctrl.h"
19#include "common/hw_features_common.h"
20#include "radius/radius_client.h"
21#include "radius/radius_das.h"
22#include "eap_server/tncs.h"
23#include "eapol_auth/eapol_auth_sm.h"
24#include "eapol_auth/eapol_auth_sm_i.h"
25#include "fst/fst.h"
26#include "hostapd.h"
27#include "authsrv.h"
28#include "sta_info.h"
29#include "accounting.h"
30#include "ap_list.h"
31#include "beacon.h"
32#include "ieee802_1x.h"
33#include "ieee802_11_auth.h"
34#include "vlan_init.h"
35#include "wpa_auth.h"
36#include "wps_hostapd.h"
37#include "dpp_hostapd.h"
38#include "gas_query_ap.h"
39#include "hw_features.h"
40#include "wpa_auth_glue.h"
41#include "ap_drv_ops.h"
42#include "ap_config.h"
43#include "p2p_hostapd.h"
44#include "gas_serv.h"
45#include "dfs.h"
46#include "ieee802_11.h"
47#include "bss_load.h"
48#include "x_snoop.h"
49#include "dhcp_snoop.h"
50#include "ndisc_snoop.h"
51#include "neighbor_db.h"
52#include "rrm.h"
53#include "fils_hlp.h"
54#include "acs.h"
55#include "hs20.h"
56#include "airtime_policy.h"
57#include "wpa_auth_kay.h"
58#include "hw_features.h"
59
60
61static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
62#ifdef CONFIG_WEP
63static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
64static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
65#endif /* CONFIG_WEP */
66static int setup_interface2(struct hostapd_iface *iface);
67static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
68static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
69 void *timeout_ctx);
70#ifdef CONFIG_IEEE80211AX
71static void hostapd_switch_color_timeout_handler(void *eloop_data,
72 void *user_ctx);
73#endif /* CONFIG_IEEE80211AX */
74
75
76int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
77 int (*cb)(struct hostapd_iface *iface,
78 void *ctx), void *ctx)
79{
80 size_t i;
81 int ret;
82
83 for (i = 0; i < interfaces->count; i++) {
84 if (!interfaces->iface[i])
85 continue;
86 ret = cb(interfaces->iface[i], ctx);
87 if (ret)
88 return ret;
89 }
90
91 return 0;
92}
93
94
95struct hostapd_data * hostapd_mbssid_get_tx_bss(struct hostapd_data *hapd)
96{
97 if (hapd->iconf->mbssid)
98 return hapd->iface->bss[0];
99
100 return hapd;
101}
102
103
104int hostapd_mbssid_get_bss_index(struct hostapd_data *hapd)
105{
106 if (hapd->iconf->mbssid) {
107 size_t i;
108
109 for (i = 1; i < hapd->iface->num_bss; i++)
110 if (hapd->iface->bss[i] == hapd)
111 return i;
112 }
113
114 return 0;
115}
116
117
118void hostapd_reconfig_encryption(struct hostapd_data *hapd)
119{
120 if (hapd->wpa_auth)
121 return;
122
123 hostapd_set_privacy(hapd, 0);
124#ifdef CONFIG_WEP
125 hostapd_setup_encryption(hapd->conf->iface, hapd);
126#endif /* CONFIG_WEP */
127}
128
129
130static void hostapd_reload_bss(struct hostapd_data *hapd)
131{
132 struct hostapd_ssid *ssid;
133
134 if (!hapd->started)
135 return;
136
137 if (hapd->conf->wmm_enabled < 0)
138 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n |
139 hapd->iconf->ieee80211ax;
140
141#ifndef CONFIG_NO_RADIUS
142 radius_client_reconfig(hapd->radius, hapd->conf->radius);
143#endif /* CONFIG_NO_RADIUS */
144
145 ssid = &hapd->conf->ssid;
146
147 hostapd_set_freq(hapd, hapd->iconf->hw_mode, hapd->iface->freq,
148 hapd->iconf->channel,
149 hapd->iconf->enable_edmg,
150 hapd->iconf->edmg_channel,
151 hapd->iconf->ieee80211n,
152 hapd->iconf->ieee80211ac,
153 hapd->iconf->ieee80211ax,
154 hapd->iconf->ieee80211be,
155 hapd->iconf->secondary_channel,
156 hostapd_get_oper_chwidth(hapd->iconf),
157 hostapd_get_oper_centr_freq_seg0_idx(hapd->iconf),
158 hostapd_get_oper_centr_freq_seg1_idx(hapd->iconf));
159
160 if (hapd->iface->current_mode) {
161 if (hostapd_prepare_rates(hapd->iface, hapd->iface->current_mode)) {
162 wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
163 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
164 HOSTAPD_LEVEL_WARNING,
165 "Failed to prepare rates table.");
166 }
167 }
168
169 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
170 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
171 /*
172 * Force PSK to be derived again since SSID or passphrase may
173 * have changed.
174 */
175 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
176 }
177 if (hostapd_setup_wpa_psk(hapd->conf)) {
178 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
179 "after reloading configuration");
180 }
181
182 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
183 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
184 else
185 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
186
187 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
188 hostapd_setup_wpa(hapd);
189 if (hapd->wpa_auth)
190 wpa_init_keys(hapd->wpa_auth);
191 } else if (hapd->conf->wpa) {
192 const u8 *wpa_ie;
193 size_t wpa_ie_len;
194 hostapd_reconfig_wpa(hapd);
195 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
196 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
197 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
198 "the kernel driver.");
199 } else if (hapd->wpa_auth) {
200 wpa_deinit(hapd->wpa_auth);
201 hapd->wpa_auth = NULL;
202 hostapd_set_privacy(hapd, 0);
203#ifdef CONFIG_WEP
204 hostapd_setup_encryption(hapd->conf->iface, hapd);
205#endif /* CONFIG_WEP */
206 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
207 }
208
209 ieee802_11_set_beacon(hapd);
210 hostapd_update_wps(hapd);
211
212 if (hapd->conf->ssid.ssid_set &&
213 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
214 hapd->conf->ssid.ssid_len)) {
215 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
216 /* try to continue */
217 }
218 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
219}
220
221
222static void hostapd_clear_old_bss(struct hostapd_data *bss)
223{
224 wpa_printf(MSG_DEBUG, "BSS %s changed - clear old state",
225 bss->conf->iface);
226
227 /*
228 * Deauthenticate all stations since the new configuration may not
229 * allow them to use the BSS anymore.
230 */
231 hostapd_flush_old_stations(bss, WLAN_REASON_PREV_AUTH_NOT_VALID);
232#ifdef CONFIG_WEP
233 hostapd_broadcast_wep_clear(bss);
234#endif /* CONFIG_WEP */
235
236#ifndef CONFIG_NO_RADIUS
237 /* TODO: update dynamic data based on changed configuration
238 * items (e.g., open/close sockets, etc.) */
239 radius_client_flush(bss->radius, 0);
240#endif /* CONFIG_NO_RADIUS */
241}
242
243
244static void hostapd_clear_old(struct hostapd_iface *iface)
245{
246 size_t j;
247
248 for (j = 0; j < iface->num_bss; j++)
249 hostapd_clear_old_bss(iface->bss[j]);
250}
251
252
253static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
254 struct hostapd_config *oldconf)
255{
256 size_t i;
257
258 if (newconf->config_id != oldconf->config_id)
259 if (strcmp(newconf->config_id, oldconf->config_id))
260 return 1;
261
262 if (newconf->num_bss != oldconf->num_bss)
263 return 1;
264
265 for (i = 0; i < newconf->num_bss; i++) {
266 if (os_strcmp(newconf->bss[i]->iface,
267 oldconf->bss[i]->iface) != 0)
268 return 1;
269 }
270
271 return 0;
272}
273
274
275static inline int hostapd_iface_num_sta(struct hostapd_iface *iface)
276{
277 int num_sta = 0;
278 int i;
279
280 for (i = 0; i < iface->num_bss; i++)
281 num_sta += iface->bss[i]->num_sta;
282
283 return num_sta;
284}
285
286
287int hostapd_check_max_sta(struct hostapd_data *hapd)
288{
289 if (hapd->num_sta >= hapd->conf->max_num_sta)
290 return 1;
291
292 if (hapd->iconf->max_num_sta &&
293 hostapd_iface_num_sta(hapd->iface) >= hapd->iconf->max_num_sta)
294 return 1;
295
296 return 0;
297}
298
299static void hostapd_iface_reload_acs(struct hostapd_config *newconf, struct hostapd_config *oldconf)
300{
301 newconf->channel = oldconf->channel;
302 newconf->secondary_channel = oldconf->secondary_channel;
303
304#ifdef CONFIG_IEEE80211BE
305 if (newconf->ieee80211be && oldconf->ieee80211be)
306 {
307 newconf->eht_oper_chwidth = oldconf->eht_oper_chwidth;
308 newconf->eht_oper_centr_freq_seg0_idx = oldconf->eht_oper_centr_freq_seg0_idx;
309 }
310#endif /* CONFIG_IEEE80211BE */
311
312#ifdef CONFIG_IEEE80211AX
313 if (newconf->ieee80211ax && oldconf->ieee80211ax)
314 {
315 newconf->he_oper_chwidth = oldconf->he_oper_chwidth;
316 newconf->he_oper_centr_freq_seg0_idx = oldconf->he_oper_centr_freq_seg0_idx;
317 newconf->he_oper_centr_freq_seg1_idx = oldconf->he_oper_centr_freq_seg1_idx;
318 }
319#endif /* CONFIG_IEEE80211AX */
320
321 newconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
322 newconf->vht_oper_centr_freq_seg0_idx = oldconf->vht_oper_centr_freq_seg0_idx;
323 newconf->vht_oper_centr_freq_seg1_idx = oldconf->vht_oper_centr_freq_seg1_idx;
324 return;
325}
326
327int hostapd_reload_config(struct hostapd_iface *iface, int reconf)
328{
329 struct hapd_interfaces *interfaces = iface->interfaces;
330 struct hostapd_data *hapd = iface->bss[0];
331 struct hostapd_config *newconf, *oldconf;
332 size_t j;
333 int i;
334
335 if (iface->config_fname == NULL) {
336 /* Only in-memory config in use - assume it has been updated */
337 hostapd_clear_old(iface);
338 for (j = 0; j < iface->num_bss; j++)
339 hostapd_reload_bss(iface->bss[j]);
340 return 0;
341 }
342
343 if (iface->interfaces == NULL ||
344 iface->interfaces->config_read_cb == NULL)
345 return -1;
346 newconf = iface->interfaces->config_read_cb(iface->config_fname);
347 if (newconf == NULL)
348 return -1;
349 wpa_printf(MSG_INFO, "hostapd_reload_config, newconf, channel:%d, secondary_channel:%d, bw: %d, centr_freq_seg0_idx:%d, centr_freq_seg1_idx:%d",
350 newconf->channel, newconf->secondary_channel,
351 hostapd_get_oper_chwidth(newconf),
352 hostapd_get_oper_centr_freq_seg0_idx(newconf),
353 hostapd_get_oper_centr_freq_seg1_idx(newconf));
354
355 oldconf = hapd->iconf;
356 wpa_printf(MSG_INFO, "hostapd_reload_config, oldconf, channel:%d, secondary_channel:%d, bw: %d, centr_freq_seg0_idx:%d, centr_freq_seg1_idx:%d",
357 oldconf->channel, oldconf->secondary_channel,
358 hostapd_get_oper_chwidth(oldconf),
359 hostapd_get_oper_centr_freq_seg0_idx(oldconf),
360 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
361
362 if (hostapd_iface_conf_changed(newconf, oldconf)) {
363 char *fname;
364 int res;
365
366 if (reconf)
367 return -1;
368
369 hostapd_clear_old(iface);
370
371 wpa_printf(MSG_DEBUG,
372 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
373 fname = os_strdup(iface->config_fname);
374 if (!fname) {
375 hostapd_config_free(newconf);
376 return -1;
377 }
378 hostapd_remove_iface(interfaces, hapd->conf->iface);
379 iface = hostapd_init(interfaces, fname);
380 os_free(fname);
381 hostapd_config_free(newconf);
382 if (!iface) {
383 wpa_printf(MSG_ERROR,
384 "Failed to initialize interface on config reload");
385 return -1;
386 }
387 iface->interfaces = interfaces;
388 interfaces->iface[interfaces->count] = iface;
389 interfaces->count++;
390 res = hostapd_enable_iface(iface);
391 if (res < 0)
392 wpa_printf(MSG_ERROR,
393 "Failed to enable interface on config reload");
394 return res;
395 } else {
396 for (j = 0; j < iface->num_bss; j++) {
397 hapd = iface->bss[j];
398 if (!hapd->config_id || strcmp(hapd->config_id, newconf->bss[j]->config_id)) {
399 hostapd_flush_old_stations(iface->bss[j],
400 WLAN_REASON_PREV_AUTH_NOT_VALID);
401#ifdef CONFIG_WEP
402 hostapd_broadcast_wep_clear(iface->bss[j]);
403#endif
404
405#ifndef CONFIG_NO_RADIUS
406 /* TODO: update dynamic data based on changed configuration
407 * items (e.g., open/close sockets, etc.) */
408 radius_client_flush(iface->bss[j]->radius, 0);
409#endif /* CONFIG_NO_RADIUS */
410 wpa_printf(MSG_INFO, "bss %zu changed", j);
411 }
412 }
413 }
414
415 // Avoid channel=0 when reload config using ACS
416 if ((newconf->channel == 0) && (newconf->channel != oldconf->channel))
417 {
418 hostapd_iface_reload_acs(newconf, oldconf);
419 }
420 iface->conf = newconf;
421 wpa_printf(MSG_INFO, "hostapd_reload_config, iface->conf, channel:%d, secondary_channel:%d, bw: %d, centr_freq_seg0_idx:%d, centr_freq_seg1_idx:%d",
422 iface->conf->channel, iface->conf->secondary_channel,
423 hostapd_get_oper_chwidth(iface->conf),
424 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
425 hostapd_get_oper_centr_freq_seg1_idx(iface->conf));
426
427 for (i = 0; i < iface->num_hw_features; i++) {
428 struct hostapd_hw_modes *mode = &iface->hw_features[i];
429 if (mode->mode == iface->conf->hw_mode) {
430 iface->current_mode = mode;
431 break;
432 }
433 }
434
435 if (iface->conf->channel)
436 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
437
438 for (j = 0; j < iface->num_bss; j++) {
439 hapd = iface->bss[j];
440 if (hapd->config_id) {
441 os_free(hapd->config_id);
442 hapd->config_id = NULL;
443 }
444 if (newconf->bss[j]->config_id)
445 hapd->config_id = strdup(newconf->bss[j]->config_id);
446 if (!hapd->conf->config_id || !newconf->bss[j]->config_id ||
447 os_strcmp(hapd->conf->config_id,
448 newconf->bss[j]->config_id) != 0)
449 hostapd_clear_old_bss(hapd);
450 hapd->iconf = newconf;
451 hapd->conf = newconf->bss[j];
452 hostapd_reload_bss(hapd);
453 }
454
455 hostapd_config_free(oldconf);
456
457
458 return 0;
459}
460
461
462#ifdef CONFIG_WEP
463
464static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
465 const char *ifname)
466{
467 int i;
468
469 if (!ifname || !hapd->drv_priv)
470 return;
471 for (i = 0; i < NUM_WEP_KEYS; i++) {
472 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
473 0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
474 wpa_printf(MSG_DEBUG, "Failed to clear default "
475 "encryption keys (ifname=%s keyidx=%d)",
476 ifname, i);
477 }
478 }
479 if (hapd->conf->ieee80211w) {
480 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
481 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
482 NULL, i, 0, 0, NULL,
483 0, NULL, 0, KEY_FLAG_GROUP)) {
484 wpa_printf(MSG_DEBUG, "Failed to clear "
485 "default mgmt encryption keys "
486 "(ifname=%s keyidx=%d)", ifname, i);
487 }
488 }
489 }
490}
491
492
493static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
494{
495 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
496 return 0;
497}
498
499
500static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
501{
502 int errors = 0, idx;
503 struct hostapd_ssid *ssid = &hapd->conf->ssid;
504
505 idx = ssid->wep.idx;
506 if (ssid->wep.default_len && ssid->wep.key[idx] &&
507 hostapd_drv_set_key(hapd->conf->iface,
508 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
509 1, NULL, 0, ssid->wep.key[idx],
510 ssid->wep.len[idx],
511 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
512 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
513 errors++;
514 }
515
516 return errors;
517}
518
519#endif /* CONFIG_WEP */
520
521
522static void hostapd_clear_drv_priv(struct hostapd_data *hapd)
523{
524 unsigned int i;
525
526 for (i = 0; i < hapd->iface->interfaces->count; i++) {
527 struct hostapd_iface *iface = hapd->iface->interfaces->iface[i];
528
529 if (hapd->iface == iface)
530 continue;
531
532 if (iface && iface->bss && iface->bss[0] && iface->bss[0]->mld_first_bss && /* Avoid null pointer by Liangyu Chu*/
533 iface->bss[0]->mld_first_bss == hapd)
534 iface->bss[0]->drv_priv = NULL;
535 }
536
537 hapd->drv_priv = NULL;
538}
539
540
541void hostapd_free_hapd_data(struct hostapd_data *hapd)
542{
543 os_free(hapd->probereq_cb);
544 hapd->probereq_cb = NULL;
545 hapd->num_probereq_cb = 0;
546
547#ifdef CONFIG_P2P
548 wpabuf_free(hapd->p2p_beacon_ie);
549 hapd->p2p_beacon_ie = NULL;
550 wpabuf_free(hapd->p2p_probe_resp_ie);
551 hapd->p2p_probe_resp_ie = NULL;
552#endif /* CONFIG_P2P */
553
554 if (!hapd->started) {
555 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
556 __func__, hapd->conf ? hapd->conf->iface : "N/A");
557 return;
558 }
559 hapd->started = 0;
560 hapd->beacon_set_done = 0;
561
562 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
563 hostapd_ubus_free_bss(hapd);
564 accounting_deinit(hapd);
565 hostapd_deinit_wpa(hapd);
566 vlan_deinit(hapd);
567 hostapd_acl_deinit(hapd);
568#ifndef CONFIG_NO_RADIUS
569 if (!hapd->mld_first_bss) {
570 radius_client_deinit(hapd->radius);
571 radius_das_deinit(hapd->radius_das);
572 }
573 hapd->radius = NULL;
574 hapd->radius_das = NULL;
575#endif /* CONFIG_NO_RADIUS */
576
577 hostapd_deinit_wps(hapd);
578 ieee802_1x_dealloc_kay_sm_hapd(hapd);
579#ifdef CONFIG_DPP
580 hostapd_dpp_deinit(hapd);
581 gas_query_ap_deinit(hapd->gas);
582 hapd->gas = NULL;
583#endif /* CONFIG_DPP */
584
585 authsrv_deinit(hapd);
586
587 if (hapd->interface_added) {
588 hapd->interface_added = 0;
589 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
590 wpa_printf(MSG_WARNING,
591 "Failed to remove BSS interface %s",
592 hapd->conf->iface);
593 hapd->interface_added = 1;
594 } else {
595 /*
596 * Since this was a dynamically added interface, the
597 * driver wrapper may have removed its internal instance
598 * and hapd->drv_priv is not valid anymore.
599 */
600 hostapd_clear_drv_priv(hapd);
601 }
602 }
603
604 wpabuf_free(hapd->time_adv);
605 hapd->time_adv = NULL;
606
607#ifdef CONFIG_INTERWORKING
608 gas_serv_deinit(hapd);
609#endif /* CONFIG_INTERWORKING */
610
611 bss_load_update_deinit(hapd);
612 ndisc_snoop_deinit(hapd);
613 dhcp_snoop_deinit(hapd);
614 x_snoop_deinit(hapd);
615
616#ifdef CONFIG_SQLITE
617 bin_clear_free(hapd->tmp_eap_user.identity,
618 hapd->tmp_eap_user.identity_len);
619 bin_clear_free(hapd->tmp_eap_user.password,
620 hapd->tmp_eap_user.password_len);
621 os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
622#endif /* CONFIG_SQLITE */
623
624#ifdef CONFIG_MESH
625 wpabuf_free(hapd->mesh_pending_auth);
626 hapd->mesh_pending_auth = NULL;
627 /* handling setup failure is already done */
628 hapd->setup_complete_cb = NULL;
629#endif /* CONFIG_MESH */
630
631 hostapd_clean_rrm(hapd);
632 fils_hlp_deinit(hapd);
633
634#ifdef CONFIG_OCV
635 eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
636#endif /* CONFIG_OCV */
637
638#ifdef CONFIG_SAE
639 {
640 struct hostapd_sae_commit_queue *q;
641
642 while ((q = dl_list_first(&hapd->sae_commit_queue,
643 struct hostapd_sae_commit_queue,
644 list))) {
645 dl_list_del(&q->list);
646 os_free(q);
647 }
648 }
649 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
650#endif /* CONFIG_SAE */
651
652#ifdef CONFIG_IEEE80211AX
653 eloop_cancel_timeout(hostapd_switch_color_timeout_handler, hapd, NULL);
654#endif /* CONFIG_IEEE80211AX */
655}
656
657
658/**
659 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
660 * @hapd: Pointer to BSS data
661 *
662 * This function is used to free all per-BSS data structures and resources.
663 * Most of the modules that are initialized in hostapd_setup_bss() are
664 * deinitialized here.
665 */
666static void hostapd_cleanup(struct hostapd_data *hapd)
667{
668 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
669 hapd->conf ? hapd->conf->iface : "N/A");
670 if (hapd->iface->interfaces &&
671 hapd->iface->interfaces->ctrl_iface_deinit) {
672 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
673 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
674 }
675 hostapd_free_hapd_data(hapd);
676}
677
678
679static void sta_track_deinit(struct hostapd_iface *iface)
680{
681 struct hostapd_sta_info *info;
682
683 if (!iface->num_sta_seen)
684 return;
685
686 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
687 list))) {
688 dl_list_del(&info->list);
689 iface->num_sta_seen--;
690 sta_track_del(info);
691 }
692}
693
694
695void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
696{
697 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
698#ifdef NEED_AP_MLME
699 hostapd_stop_setup_timers(iface);
700#endif /* NEED_AP_MLME */
701 if (iface->current_mode)
702 acs_cleanup(iface);
703 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
704 iface->hw_features = NULL;
705 iface->current_mode = NULL;
706 os_free(iface->current_rates);
707 iface->current_rates = NULL;
708 os_free(iface->basic_rates);
709 iface->basic_rates = NULL;
710 iface->cac_started = 0;
711 ap_list_deinit(iface);
712 sta_track_deinit(iface);
713 airtime_policy_update_deinit(iface);
714}
715
716
717/**
718 * hostapd_cleanup_iface - Complete per-interface cleanup
719 * @iface: Pointer to interface data
720 *
721 * This function is called after per-BSS data structures are deinitialized
722 * with hostapd_cleanup().
723 */
724static void hostapd_cleanup_iface(struct hostapd_iface *iface)
725{
726 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
727 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
728 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
729 NULL);
730
731 hostapd_cleanup_iface_partial(iface);
732 hostapd_config_free(iface->conf);
733 iface->conf = NULL;
734
735 os_free(iface->config_fname);
736 os_free(iface->bss);
737 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
738 os_free(iface);
739}
740
741
742#ifdef CONFIG_WEP
743
744static void hostapd_clear_wep(struct hostapd_data *hapd)
745{
746 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
747 hostapd_set_privacy(hapd, 0);
748 hostapd_broadcast_wep_clear(hapd);
749 }
750}
751
752
753static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
754{
755 int i;
756
757 hostapd_broadcast_wep_set(hapd);
758
759 if (hapd->conf->ssid.wep.default_len) {
760 hostapd_set_privacy(hapd, 1);
761 return 0;
762 }
763
764 /*
765 * When IEEE 802.1X is not enabled, the driver may need to know how to
766 * set authentication algorithms for static WEP.
767 */
768 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
769
770 for (i = 0; i < 4; i++) {
771 if (hapd->conf->ssid.wep.key[i] &&
772 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
773 i == hapd->conf->ssid.wep.idx, NULL, 0,
774 hapd->conf->ssid.wep.key[i],
775 hapd->conf->ssid.wep.len[i],
776 i == hapd->conf->ssid.wep.idx ?
777 KEY_FLAG_GROUP_RX_TX_DEFAULT :
778 KEY_FLAG_GROUP_RX_TX)) {
779 wpa_printf(MSG_WARNING, "Could not set WEP "
780 "encryption.");
781 return -1;
782 }
783 if (hapd->conf->ssid.wep.key[i] &&
784 i == hapd->conf->ssid.wep.idx)
785 hostapd_set_privacy(hapd, 1);
786 }
787
788 return 0;
789}
790
791#endif /* CONFIG_WEP */
792
793
794static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
795{
796 int ret = 0;
797 u8 addr[ETH_ALEN];
798
799 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
800 return 0;
801
802 if (!hapd->iface->driver_ap_teardown) {
803 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
804 "Flushing old station entries");
805
806 if (hostapd_flush(hapd)) {
807 wpa_msg(hapd->msg_ctx, MSG_WARNING,
808 "Could not connect to kernel driver");
809 ret = -1;
810 }
811 }
812 if (hapd->conf && hapd->conf->broadcast_deauth) {
813 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
814 "Deauthenticate all stations");
815 os_memset(addr, 0xff, ETH_ALEN);
816 hostapd_drv_sta_deauth(hapd, addr, reason);
817 }
818 hostapd_free_stas(hapd);
819
820 return ret;
821}
822
823
824void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
825{
826 hostapd_free_stas(hapd);
827 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
828#ifdef CONFIG_WEP
829 hostapd_clear_wep(hapd);
830#endif /* CONFIG_WEP */
831}
832
833
834/**
835 * hostapd_validate_bssid_configuration - Validate BSSID configuration
836 * @iface: Pointer to interface data
837 * Returns: 0 on success, -1 on failure
838 *
839 * This function is used to validate that the configured BSSIDs are valid.
840 */
841static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
842{
843 u8 mask[ETH_ALEN] = { 0 };
844 struct hostapd_data *hapd = iface->bss[0];
845 unsigned int i = iface->conf->num_bss, bits = 0, j;
846 int auto_addr = 0;
847
848 if (hostapd_drv_none(hapd))
849 return 0;
850
851 if (iface->conf->use_driver_iface_addr)
852 return 0;
853
854 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
855
856 /* Determine the bits necessary to cover the number of BSSIDs. */
857 for (i--; i; i >>= 1)
858 bits++;
859
860 /* Determine the bits necessary to any configured BSSIDs,
861 if they are higher than the number of BSSIDs. */
862 for (j = 0; j < iface->conf->num_bss; j++) {
863 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
864 if (j)
865 auto_addr++;
866 continue;
867 }
868
869 for (i = 0; i < ETH_ALEN; i++) {
870 mask[i] |=
871 iface->conf->bss[j]->bssid[i] ^
872 hapd->own_addr[i];
873 }
874 }
875
876 if (!auto_addr)
877 goto skip_mask_ext;
878
879 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
880 ;
881 j = 0;
882 if (i < ETH_ALEN) {
883 j = (5 - i) * 8;
884
885 while (mask[i] != 0) {
886 mask[i] >>= 1;
887 j++;
888 }
889 }
890
891 if (bits < j)
892 bits = j;
893
894 if (bits > 40) {
895 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
896 bits);
897 return -1;
898 }
899
900 os_memset(mask, 0xff, ETH_ALEN);
901 j = bits / 8;
902 for (i = 5; i > 5 - j; i--)
903 mask[i] = 0;
904 j = bits % 8;
905 while (j) {
906 j--;
907 mask[i] <<= 1;
908 }
909
910skip_mask_ext:
911 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
912 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
913
914 if (!auto_addr)
915 return 0;
916
917 for (i = 0; i < ETH_ALEN; i++) {
918 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
919 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
920 " for start address " MACSTR ".",
921 MAC2STR(mask), MAC2STR(hapd->own_addr));
922 wpa_printf(MSG_ERROR, "Start address must be the "
923 "first address in the block (i.e., addr "
924 "AND mask == addr).");
925 return -1;
926 }
927 }
928
929 return 0;
930}
931
932
933static int mac_in_conf(struct hostapd_config *conf, const void *a)
934{
935 size_t i;
936
937 for (i = 0; i < conf->num_bss; i++) {
938 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
939 return 1;
940 }
941 }
942
943 return 0;
944}
945
946
947#ifndef CONFIG_NO_RADIUS
948
949static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
950 struct radius_das_attrs *attr)
951{
952 if (attr->nas_identifier &&
953 (!hapd->conf->nas_identifier ||
954 os_strlen(hapd->conf->nas_identifier) !=
955 attr->nas_identifier_len ||
956 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
957 attr->nas_identifier_len) != 0)) {
958 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
959 return 1;
960 }
961
962 if (attr->nas_ip_addr &&
963 (hapd->conf->own_ip_addr.af != AF_INET ||
964 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
965 0)) {
966 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
967 return 1;
968 }
969
970#ifdef CONFIG_IPV6
971 if (attr->nas_ipv6_addr &&
972 (hapd->conf->own_ip_addr.af != AF_INET6 ||
973 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
974 != 0)) {
975 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
976 return 1;
977 }
978#endif /* CONFIG_IPV6 */
979
980 return 0;
981}
982
983
984static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
985 struct radius_das_attrs *attr,
986 int *multi)
987{
988 struct sta_info *selected, *sta;
989 char buf[128];
990 int num_attr = 0;
991 int count;
992
993 *multi = 0;
994
995 for (sta = hapd->sta_list; sta; sta = sta->next)
996 sta->radius_das_match = 1;
997
998 if (attr->sta_addr) {
999 num_attr++;
1000 sta = ap_get_sta(hapd, attr->sta_addr);
1001 if (!sta) {
1002 wpa_printf(MSG_DEBUG,
1003 "RADIUS DAS: No Calling-Station-Id match");
1004 return NULL;
1005 }
1006
1007 selected = sta;
1008 for (sta = hapd->sta_list; sta; sta = sta->next) {
1009 if (sta != selected)
1010 sta->radius_das_match = 0;
1011 }
1012 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
1013 }
1014
1015 if (attr->acct_session_id) {
1016 num_attr++;
1017 if (attr->acct_session_id_len != 16) {
1018 wpa_printf(MSG_DEBUG,
1019 "RADIUS DAS: Acct-Session-Id cannot match");
1020 return NULL;
1021 }
1022 count = 0;
1023
1024 for (sta = hapd->sta_list; sta; sta = sta->next) {
1025 if (!sta->radius_das_match)
1026 continue;
1027 os_snprintf(buf, sizeof(buf), "%016llX",
1028 (unsigned long long) sta->acct_session_id);
1029 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
1030 sta->radius_das_match = 0;
1031 else
1032 count++;
1033 }
1034
1035 if (count == 0) {
1036 wpa_printf(MSG_DEBUG,
1037 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
1038 return NULL;
1039 }
1040 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
1041 }
1042
1043 if (attr->acct_multi_session_id) {
1044 num_attr++;
1045 if (attr->acct_multi_session_id_len != 16) {
1046 wpa_printf(MSG_DEBUG,
1047 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
1048 return NULL;
1049 }
1050 count = 0;
1051
1052 for (sta = hapd->sta_list; sta; sta = sta->next) {
1053 if (!sta->radius_das_match)
1054 continue;
1055 if (!sta->eapol_sm ||
1056 !sta->eapol_sm->acct_multi_session_id) {
1057 sta->radius_das_match = 0;
1058 continue;
1059 }
1060 os_snprintf(buf, sizeof(buf), "%016llX",
1061 (unsigned long long)
1062 sta->eapol_sm->acct_multi_session_id);
1063 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
1064 0)
1065 sta->radius_das_match = 0;
1066 else
1067 count++;
1068 }
1069
1070 if (count == 0) {
1071 wpa_printf(MSG_DEBUG,
1072 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
1073 return NULL;
1074 }
1075 wpa_printf(MSG_DEBUG,
1076 "RADIUS DAS: Acct-Multi-Session-Id match");
1077 }
1078
1079 if (attr->cui) {
1080 num_attr++;
1081 count = 0;
1082
1083 for (sta = hapd->sta_list; sta; sta = sta->next) {
1084 struct wpabuf *cui;
1085
1086 if (!sta->radius_das_match)
1087 continue;
1088 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
1089 if (!cui || wpabuf_len(cui) != attr->cui_len ||
1090 os_memcmp(wpabuf_head(cui), attr->cui,
1091 attr->cui_len) != 0)
1092 sta->radius_das_match = 0;
1093 else
1094 count++;
1095 }
1096
1097 if (count == 0) {
1098 wpa_printf(MSG_DEBUG,
1099 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
1100 return NULL;
1101 }
1102 wpa_printf(MSG_DEBUG,
1103 "RADIUS DAS: Chargeable-User-Identity match");
1104 }
1105
1106 if (attr->user_name) {
1107 num_attr++;
1108 count = 0;
1109
1110 for (sta = hapd->sta_list; sta; sta = sta->next) {
1111 u8 *identity;
1112 size_t identity_len;
1113
1114 if (!sta->radius_das_match)
1115 continue;
1116 identity = ieee802_1x_get_identity(sta->eapol_sm,
1117 &identity_len);
1118 if (!identity ||
1119 identity_len != attr->user_name_len ||
1120 os_memcmp(identity, attr->user_name, identity_len)
1121 != 0)
1122 sta->radius_das_match = 0;
1123 else
1124 count++;
1125 }
1126
1127 if (count == 0) {
1128 wpa_printf(MSG_DEBUG,
1129 "RADIUS DAS: No matches remaining after User-Name check");
1130 return NULL;
1131 }
1132 wpa_printf(MSG_DEBUG,
1133 "RADIUS DAS: User-Name match");
1134 }
1135
1136 if (num_attr == 0) {
1137 /*
1138 * In theory, we could match all current associations, but it
1139 * seems safer to just reject requests that do not include any
1140 * session identification attributes.
1141 */
1142 wpa_printf(MSG_DEBUG,
1143 "RADIUS DAS: No session identification attributes included");
1144 return NULL;
1145 }
1146
1147 selected = NULL;
1148 for (sta = hapd->sta_list; sta; sta = sta->next) {
1149 if (sta->radius_das_match) {
1150 if (selected) {
1151 *multi = 1;
1152 return NULL;
1153 }
1154 selected = sta;
1155 }
1156 }
1157
1158 return selected;
1159}
1160
1161
1162static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
1163 struct radius_das_attrs *attr)
1164{
1165 if (!hapd->wpa_auth)
1166 return -1;
1167 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
1168}
1169
1170
1171static enum radius_das_res
1172hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
1173{
1174 struct hostapd_data *hapd = ctx;
1175 struct sta_info *sta;
1176 int multi;
1177
1178 if (hostapd_das_nas_mismatch(hapd, attr))
1179 return RADIUS_DAS_NAS_MISMATCH;
1180
1181 sta = hostapd_das_find_sta(hapd, attr, &multi);
1182 if (sta == NULL) {
1183 if (multi) {
1184 wpa_printf(MSG_DEBUG,
1185 "RADIUS DAS: Multiple sessions match - not supported");
1186 return RADIUS_DAS_MULTI_SESSION_MATCH;
1187 }
1188 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
1189 wpa_printf(MSG_DEBUG,
1190 "RADIUS DAS: PMKSA cache entry matched");
1191 return RADIUS_DAS_SUCCESS;
1192 }
1193 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1194 return RADIUS_DAS_SESSION_NOT_FOUND;
1195 }
1196
1197 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1198 " - disconnecting", MAC2STR(sta->addr));
1199 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1200
1201 hostapd_drv_sta_deauth(hapd, sta->addr,
1202 WLAN_REASON_PREV_AUTH_NOT_VALID);
1203 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1204
1205 return RADIUS_DAS_SUCCESS;
1206}
1207
1208
1209#ifdef CONFIG_HS20
1210static enum radius_das_res
1211hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1212{
1213 struct hostapd_data *hapd = ctx;
1214 struct sta_info *sta;
1215 int multi;
1216
1217 if (hostapd_das_nas_mismatch(hapd, attr))
1218 return RADIUS_DAS_NAS_MISMATCH;
1219
1220 sta = hostapd_das_find_sta(hapd, attr, &multi);
1221 if (!sta) {
1222 if (multi) {
1223 wpa_printf(MSG_DEBUG,
1224 "RADIUS DAS: Multiple sessions match - not supported");
1225 return RADIUS_DAS_MULTI_SESSION_MATCH;
1226 }
1227 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1228 return RADIUS_DAS_SESSION_NOT_FOUND;
1229 }
1230
1231 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1232 " - CoA", MAC2STR(sta->addr));
1233
1234 if (attr->hs20_t_c_filtering) {
1235 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1236 wpa_printf(MSG_DEBUG,
1237 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1238 return RADIUS_DAS_COA_FAILED;
1239 }
1240
1241 hs20_t_c_filtering(hapd, sta, 0);
1242 }
1243
1244 return RADIUS_DAS_SUCCESS;
1245}
1246#else /* CONFIG_HS20 */
1247#define hostapd_das_coa NULL
1248#endif /* CONFIG_HS20 */
1249
1250
1251#ifdef CONFIG_SQLITE
1252
1253static int db_table_exists(sqlite3 *db, const char *name)
1254{
1255 char cmd[128];
1256
1257 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1258 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1259}
1260
1261
1262static int db_table_create_radius_attributes(sqlite3 *db)
1263{
1264 char *err = NULL;
1265 const char *sql =
1266 "CREATE TABLE radius_attributes("
1267 " id INTEGER PRIMARY KEY,"
1268 " sta TEXT,"
1269 " reqtype TEXT,"
1270 " attr TEXT"
1271 ");"
1272 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1273
1274 wpa_printf(MSG_DEBUG,
1275 "Adding database table for RADIUS attribute information");
1276 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1277 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1278 sqlite3_free(err);
1279 return -1;
1280 }
1281
1282 return 0;
1283}
1284
1285#endif /* CONFIG_SQLITE */
1286
1287#endif /* CONFIG_NO_RADIUS */
1288
1289
1290static int hostapd_start_beacon(struct hostapd_data *hapd,
1291 bool flush_old_stations)
1292{
1293 struct hostapd_bss_config *conf = hapd->conf;
1294
1295 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1296 return -1;
1297
1298 if (flush_old_stations && !conf->start_disabled &&
1299 conf->broadcast_deauth) {
1300 u8 addr[ETH_ALEN];
1301
1302 /* Should any previously associated STA not have noticed that
1303 * the AP had stopped and restarted, send one more
1304 * deauthentication notification now that the AP is ready to
1305 * operate. */
1306 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1307 "Deauthenticate all stations at BSS start");
1308 os_memset(addr, 0xff, ETH_ALEN);
1309 hostapd_drv_sta_deauth(hapd, addr,
1310 WLAN_REASON_PREV_AUTH_NOT_VALID);
1311 }
1312
1313 if (hapd->driver && hapd->driver->set_operstate)
1314 hapd->driver->set_operstate(hapd->drv_priv, 1);
1315
1316 hostapd_ubus_add_bss(hapd);
1317
1318 return 0;
1319}
1320
1321
1322/**
1323 * hostapd_setup_bss - Per-BSS setup (initialization)
1324 * @hapd: Pointer to BSS data
1325 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1326 * but interface may exist
1327 * @start_beacon: Whether Beacon frame template should be configured and
1328 * transmission of Beaconf rames started at this time. This is used when
1329 * MBSSID element is enabled where the information regarding all BSSes
1330 * should be retrieved before configuring the Beacon frame template. The
1331 * calling functions are responsible for configuring the Beacon frame
1332 * explicitly if this is set to false.
1333 *
1334 * This function is used to initialize all per-BSS data structures and
1335 * resources. This gets called in a loop for each BSS when an interface is
1336 * initialized. Most of the modules that are initialized here will be
1337 * deinitialized in hostapd_cleanup().
1338 */
1339static int hostapd_setup_bss(struct hostapd_data *hapd, int first,
1340 bool start_beacon)
1341{
1342 struct hostapd_bss_config *conf = hapd->conf;
1343 u8 ssid[SSID_MAX_LEN + 1];
1344 int ssid_len, set_ssid;
1345 char force_ifname[IFNAMSIZ];
1346 u8 if_addr[ETH_ALEN];
1347 int flush_old_stations = 1;
1348
1349 if (hapd->mld_first_bss)
1350 wpa_printf(MSG_DEBUG,
1351 "MLD: %s: Setting non-first BSS", __func__);
1352
1353 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
1354 __func__, hapd, conf->iface, first);
1355
1356#ifdef EAP_SERVER_TNC
1357 if (conf->tnc && tncs_global_init() < 0) {
1358 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1359 return -1;
1360 }
1361#endif /* EAP_SERVER_TNC */
1362
1363 if (hapd->started) {
1364 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
1365 __func__, conf->iface);
1366 return -1;
1367 }
1368 hapd->started = 1;
1369
1370 if (!first || first == -1) {
1371 u8 *addr = hapd->own_addr;
1372
1373 if (!is_zero_ether_addr(conf->bssid)) {
1374 /* Allocate the configured BSSID. */
1375 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
1376
1377 if (hostapd_mac_comp(hapd->own_addr,
1378 hapd->iface->bss[0]->own_addr) ==
1379 0) {
1380 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1381 "BSSID set to the MAC address of "
1382 "the radio", conf->iface);
1383 return -1;
1384 }
1385 } else if (hapd->iconf->use_driver_iface_addr) {
1386 addr = NULL;
1387 } else {
1388 /* Allocate the next available BSSID. */
1389 do {
1390 inc_byte_array(hapd->own_addr, ETH_ALEN);
1391 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1392 }
1393
1394 hapd->interface_added = 1;
1395 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
1396 conf->iface, addr, hapd,
1397 &hapd->drv_priv, force_ifname, if_addr,
1398 conf->bridge[0] ? conf->bridge : NULL,
1399 first == -1)) {
1400 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1401 MACSTR ")", MAC2STR(hapd->own_addr));
1402 hapd->interface_added = 0;
1403 return -1;
1404 }
1405
1406 if (!addr)
1407 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
1408 }
1409
1410 if (conf->wmm_enabled < 0)
1411 conf->wmm_enabled = hapd->iconf->ieee80211n |
1412 hapd->iconf->ieee80211ax;
1413
1414#ifdef CONFIG_IEEE80211R_AP
1415 if (is_zero_ether_addr(conf->r1_key_holder))
1416 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
1417#endif /* CONFIG_IEEE80211R_AP */
1418
1419#ifdef CONFIG_MESH
1420 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
1421 flush_old_stations = 0;
1422#endif /* CONFIG_MESH */
1423
1424 if (flush_old_stations)
1425 hostapd_flush(hapd);
1426 hostapd_set_privacy(hapd, 0);
1427
1428#ifdef CONFIG_WEP
1429 if (!hostapd_drv_nl80211(hapd))
1430 hostapd_broadcast_wep_clear(hapd);
1431 if (hostapd_setup_encryption(conf->iface, hapd))
1432 return -1;
1433#endif /* CONFIG_WEP */
1434
1435 /*
1436 * Fetch the SSID from the system and use it or,
1437 * if one was specified in the config file, verify they
1438 * match.
1439 */
1440 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1441 if (ssid_len < 0) {
1442 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1443 return -1;
1444 }
1445 if (conf->ssid.ssid_set) {
1446 /*
1447 * If SSID is specified in the config file and it differs
1448 * from what is being used then force installation of the
1449 * new SSID.
1450 */
1451 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1452 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1453 } else {
1454 /*
1455 * No SSID in the config file; just use the one we got
1456 * from the system.
1457 */
1458 set_ssid = 0;
1459 conf->ssid.ssid_len = ssid_len;
1460 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1461 }
1462
1463 /*
1464 * Short SSID calculation is identical to FCS and it is defined in
1465 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1466 */
1467 conf->ssid.short_ssid = ieee80211_crc32(conf->ssid.ssid,
1468 conf->ssid.ssid_len);
1469
1470 if (!hostapd_drv_none(hapd)) {
1471 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
1472 " and ssid \"%s\"",
1473 conf->iface, MAC2STR(hapd->own_addr),
1474 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
1475 }
1476
1477 if (hostapd_setup_wpa_psk(conf)) {
1478 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1479 return -1;
1480 }
1481
1482 /* Set SSID for the kernel driver (to be used in beacon and probe
1483 * response frames) */
1484 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
1485 conf->ssid.ssid_len)) {
1486 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1487 return -1;
1488 }
1489
1490 if (wpa_debug_level <= MSG_MSGDUMP)
1491 conf->radius->msg_dumps = 1;
1492#ifndef CONFIG_NO_RADIUS
1493
1494#ifdef CONFIG_SQLITE
1495 if (conf->radius_req_attr_sqlite) {
1496 if (sqlite3_open(conf->radius_req_attr_sqlite,
1497 &hapd->rad_attr_db)) {
1498 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1499 conf->radius_req_attr_sqlite);
1500 return -1;
1501 }
1502
1503 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1504 conf->radius_req_attr_sqlite);
1505 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1506 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1507 return -1;
1508 }
1509#endif /* CONFIG_SQLITE */
1510
1511 if (!hapd->mld_first_bss) {
1512 hapd->radius = radius_client_init(hapd, conf->radius);
1513 if (!hapd->radius) {
1514 wpa_printf(MSG_ERROR,
1515 "RADIUS client initialization failed.");
1516 return -1;
1517 }
1518
1519 if (conf->radius_das_port) {
1520 struct radius_das_conf das_conf;
1521
1522 os_memset(&das_conf, 0, sizeof(das_conf));
1523 das_conf.port = conf->radius_das_port;
1524 das_conf.nas_identifier = conf->nas_identifier;
1525 das_conf.shared_secret = conf->radius_das_shared_secret;
1526 das_conf.shared_secret_len =
1527 conf->radius_das_shared_secret_len;
1528 das_conf.client_addr = &conf->radius_das_client_addr;
1529 das_conf.time_window = conf->radius_das_time_window;
1530 das_conf.require_event_timestamp =
1531 conf->radius_das_require_event_timestamp;
1532 das_conf.require_message_authenticator =
1533 conf->radius_das_require_message_authenticator;
1534 das_conf.ctx = hapd;
1535 das_conf.disconnect = hostapd_das_disconnect;
1536 das_conf.coa = hostapd_das_coa;
1537 hapd->radius_das = radius_das_init(&das_conf);
1538 if (!hapd->radius_das) {
1539 wpa_printf(MSG_ERROR,
1540 "RADIUS DAS initialization failed.");
1541 return -1;
1542 }
1543 }
1544 } else {
1545 wpa_printf(MSG_DEBUG,
1546 "MLD: Using RADIUS client of the first BSS");
1547 hapd->radius = hapd->mld_first_bss->radius;
1548 hapd->radius_das = hapd->mld_first_bss->radius_das;
1549 }
1550#endif /* CONFIG_NO_RADIUS */
1551
1552 if (hostapd_acl_init(hapd)) {
1553 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1554 return -1;
1555 }
1556 if (hostapd_init_wps(hapd, conf))
1557 return -1;
1558
1559#ifdef CONFIG_DPP
1560 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1561 if (!hapd->gas)
1562 return -1;
1563 if (hostapd_dpp_init(hapd))
1564 return -1;
1565#endif /* CONFIG_DPP */
1566
1567 if (authsrv_init(hapd) < 0)
1568 return -1;
1569
1570 if (ieee802_1x_init(hapd)) {
1571 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1572 return -1;
1573 }
1574
1575 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1576 return -1;
1577
1578 if (accounting_init(hapd)) {
1579 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1580 return -1;
1581 }
1582
1583#ifdef CONFIG_INTERWORKING
1584 if (gas_serv_init(hapd)) {
1585 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1586 return -1;
1587 }
1588#endif /* CONFIG_INTERWORKING */
1589
1590 if (conf->qos_map_set_len &&
1591 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1592 conf->qos_map_set_len)) {
1593 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1594 return -1;
1595 }
1596
1597 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1598 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1599 return -1;
1600 }
1601
1602 if (conf->bridge[0]) {
1603 /* Set explicitly configured bridge parameters that might have
1604 * been lost if the interface has been removed out of the
1605 * bridge. */
1606
1607 /* multicast to unicast on bridge ports */
1608 if (conf->bridge_multicast_to_unicast)
1609 hostapd_drv_br_port_set_attr(
1610 hapd, DRV_BR_PORT_ATTR_MCAST2UCAST, 1);
1611
1612 /* hairpin mode */
1613 if (conf->bridge_hairpin)
1614 hostapd_drv_br_port_set_attr(
1615 hapd, DRV_BR_PORT_ATTR_HAIRPIN_MODE, 1);
1616 }
1617
1618 if (conf->proxy_arp) {
1619 if (x_snoop_init(hapd)) {
1620 wpa_printf(MSG_ERROR,
1621 "Generic snooping infrastructure initialization failed");
1622 return -1;
1623 }
1624
1625 if (dhcp_snoop_init(hapd)) {
1626 wpa_printf(MSG_ERROR,
1627 "DHCP snooping initialization failed");
1628 return -1;
1629 }
1630
1631 if (ndisc_snoop_init(hapd)) {
1632 wpa_printf(MSG_ERROR,
1633 "Neighbor Discovery snooping initialization failed");
1634 return -1;
1635 }
1636 }
1637
1638 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1639 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1640 return -1;
1641 }
1642
1643 if (start_beacon && hostapd_start_beacon(hapd, flush_old_stations) < 0)
1644 return -1;
1645
1646 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1647 return -1;
1648
1649 return 0;
1650}
1651
1652
1653static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1654{
1655 struct hostapd_data *hapd = iface->bss[0];
1656 int i;
1657 struct hostapd_tx_queue_params *p;
1658
1659#ifdef CONFIG_MESH
1660 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
1661 return;
1662#endif /* CONFIG_MESH */
1663
1664 for (i = 0; i < NUM_TX_QUEUES; i++) {
1665 p = &iface->conf->tx_queue[i];
1666
1667 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1668 p->cwmax, p->burst)) {
1669 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1670 "parameters for queue %d.", i);
1671 /* Continue anyway */
1672 }
1673 }
1674}
1675
1676
1677static int hostapd_set_acl_list(struct hostapd_data *hapd,
1678 struct mac_acl_entry *mac_acl,
1679 int n_entries, u8 accept_acl)
1680{
1681 struct hostapd_acl_params *acl_params;
1682 int i, err;
1683
1684 acl_params = os_zalloc(sizeof(*acl_params) +
1685 (n_entries * sizeof(acl_params->mac_acl[0])));
1686 if (!acl_params)
1687 return -ENOMEM;
1688
1689 for (i = 0; i < n_entries; i++)
1690 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1691 ETH_ALEN);
1692
1693 acl_params->acl_policy = accept_acl;
1694 acl_params->num_mac_acl = n_entries;
1695
1696 err = hostapd_drv_set_acl(hapd, acl_params);
1697
1698 os_free(acl_params);
1699
1700 return err;
1701}
1702
1703
1704int hostapd_set_acl(struct hostapd_data *hapd)
1705{
1706 struct hostapd_config *conf = hapd->iconf;
1707 int err = 0;
1708 u8 accept_acl;
1709
1710 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1711 return 0;
1712
1713 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1714 accept_acl = 1;
1715 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1716 conf->bss[0]->num_accept_mac,
1717 accept_acl);
1718 if (err) {
1719 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1720 return -1;
1721 }
1722 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1723 accept_acl = 0;
1724 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1725 conf->bss[0]->num_deny_mac,
1726 accept_acl);
1727 if (err) {
1728 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1729 return -1;
1730 }
1731 }
1732 return err;
1733}
1734
1735
1736static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1737{
1738 if (!hapd->iface->interfaces ||
1739 !hapd->iface->interfaces->ctrl_iface_init)
1740 return 0;
1741
1742 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1743 wpa_printf(MSG_ERROR,
1744 "Failed to setup control interface for %s",
1745 hapd->conf->iface);
1746 return -1;
1747 }
1748
1749 return 0;
1750}
1751
1752
1753static int start_ctrl_iface(struct hostapd_iface *iface)
1754{
1755 size_t i;
1756
1757 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1758 return 0;
1759
1760 for (i = 0; i < iface->num_bss; i++) {
1761 struct hostapd_data *hapd = iface->bss[i];
1762 if (iface->interfaces->ctrl_iface_init(hapd)) {
1763 wpa_printf(MSG_ERROR,
1764 "Failed to setup control interface for %s",
1765 hapd->conf->iface);
1766 return -1;
1767 }
1768 }
1769
1770 return 0;
1771}
1772
1773
1774/* When NO_IR flag is set and AP is stopped, clean up BSS parameters without
1775 * deinitializing the driver and the control interfaces. A subsequent
1776 * REG_CHANGE event can bring the AP back up.
1777 */
1778static void hostapd_no_ir_cleanup(struct hostapd_data *bss)
1779{
1780 hostapd_bss_deinit_no_free(bss);
1781 hostapd_free_hapd_data(bss);
1782 hostapd_cleanup_iface_partial(bss->iface);
1783}
1784
1785
1786static int hostapd_no_ir_channel_list_updated(struct hostapd_iface *iface,
1787 void *ctx)
1788{
1789 bool all_no_ir, is_6ghz;
1790 int i, j;
1791 struct hostapd_hw_modes *mode = NULL;
1792
1793 if (hostapd_get_hw_features(iface))
1794 return 0;
1795
1796 all_no_ir = true;
1797 is_6ghz = false;
1798
1799 for (i = 0; i < iface->num_hw_features; i++) {
1800 mode = &iface->hw_features[i];
1801
1802 if (mode->mode == iface->conf->hw_mode) {
1803 if (iface->freq > 0 &&
1804 !hw_mode_get_channel(mode, iface->freq, NULL)) {
1805 mode = NULL;
1806 continue;
1807 }
1808
1809 for (j = 0; j < mode->num_channels; j++) {
1810 if (!(mode->channels[j].flag &
1811 HOSTAPD_CHAN_NO_IR))
1812 all_no_ir = false;
1813
1814 if (is_6ghz_freq(mode->channels[j].freq))
1815 is_6ghz = true;
1816 }
1817 break;
1818 }
1819 }
1820
1821 if (!mode || !is_6ghz)
1822 return 0;
1823 iface->current_mode = mode;
1824
1825 if (iface->state == HAPD_IFACE_ENABLED) {
1826 if (!all_no_ir) {
1827 struct hostapd_channel_data *chan;
1828
1829 chan = hw_get_channel_freq(iface->current_mode->mode,
1830 iface->freq, NULL,
1831 iface->hw_features,
1832 iface->num_hw_features);
1833
1834 if (!chan) {
1835 wpa_printf(MSG_ERROR,
1836 "NO_IR: Could not derive chan from freq");
1837 return 0;
1838 }
1839
1840 if (!(chan->flag & HOSTAPD_CHAN_NO_IR))
1841 return 0;
1842 wpa_printf(MSG_DEBUG,
1843 "NO_IR: The current channel has NO_IR flag now, stop AP.");
1844 } else {
1845 wpa_printf(MSG_DEBUG,
1846 "NO_IR: All chan in new chanlist are NO_IR, stop AP.");
1847 }
1848
1849 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
1850 iface->is_no_ir = true;
1851 hostapd_drv_stop_ap(iface->bss[0]);
1852 hostapd_no_ir_cleanup(iface->bss[0]);
1853 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
1854 } else if (iface->state == HAPD_IFACE_NO_IR) {
1855 if (all_no_ir) {
1856 wpa_printf(MSG_DEBUG,
1857 "NO_IR: AP in NO_IR and all chan in the new chanlist are NO_IR. Ignore");
1858 return 0;
1859 }
1860
1861 if (!iface->conf->acs) {
1862 struct hostapd_channel_data *chan;
1863
1864 chan = hw_get_channel_freq(iface->current_mode->mode,
1865 iface->freq, NULL,
1866 iface->hw_features,
1867 iface->num_hw_features);
1868 if (!chan) {
1869 wpa_printf(MSG_ERROR,
1870 "NO_IR: Could not derive chan from freq");
1871 return 0;
1872 }
1873
1874 /* If the last operating channel is NO_IR, trigger ACS.
1875 */
1876 if (chan->flag & HOSTAPD_CHAN_NO_IR) {
1877 iface->freq = 0;
1878 iface->conf->channel = 0;
1879 if (acs_init(iface) != HOSTAPD_CHAN_ACS)
1880 wpa_printf(MSG_ERROR,
1881 "NO_IR: Could not start ACS");
1882 return 0;
1883 }
1884 }
1885
1886 setup_interface2(iface);
1887 }
1888
1889 return 0;
1890}
1891
1892
1893static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1894{
1895 struct hostapd_iface *iface = eloop_ctx;
1896
1897 if (!iface->wait_channel_update) {
1898 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1899 return;
1900 }
1901
1902 /*
1903 * It is possible that the existing channel list is acceptable, so try
1904 * to proceed.
1905 */
1906 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1907 setup_interface2(iface);
1908}
1909
1910
1911void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1912{
1913 if (initiator == REGDOM_SET_BY_DRIVER) {
1914 hostapd_for_each_interface(iface->interfaces,
1915 hostapd_no_ir_channel_list_updated,
1916 NULL);
1917 return;
1918 }
1919
1920 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1921 return;
1922
1923 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1924 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1925 setup_interface2(iface);
1926}
1927
1928
1929static int setup_interface(struct hostapd_iface *iface)
1930{
1931 struct hostapd_data *hapd = iface->bss[0];
1932 size_t i;
1933
1934 /*
1935 * It is possible that setup_interface() is called after the interface
1936 * was disabled etc., in which case driver_ap_teardown is possibly set
1937 * to 1. Clear it here so any other key/station deletion, which is not
1938 * part of a teardown flow, would also call the relevant driver
1939 * callbacks.
1940 */
1941 iface->driver_ap_teardown = 0;
1942
1943 if (!iface->phy[0]) {
1944 const char *phy = hostapd_drv_get_radio_name(hapd);
1945 if (phy) {
1946 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1947 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1948 }
1949 }
1950
1951 /*
1952 * Make sure that all BSSes get configured with a pointer to the same
1953 * driver interface.
1954 */
1955 for (i = 1; i < iface->num_bss; i++) {
1956 iface->bss[i]->driver = hapd->driver;
1957 iface->bss[i]->drv_priv = hapd->drv_priv;
1958 }
1959
1960 if (hostapd_validate_bssid_configuration(iface))
1961 return -1;
1962
1963 /*
1964 * Initialize control interfaces early to allow external monitoring of
1965 * channel setup operations that may take considerable amount of time
1966 * especially for DFS cases.
1967 */
1968 if (start_ctrl_iface(iface))
1969 return -1;
1970
1971 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1972 char country[4], previous_country[4];
1973
1974 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1975 if (hostapd_get_country(hapd, previous_country) < 0)
1976 previous_country[0] = '\0';
1977
1978 os_memcpy(country, hapd->iconf->country, 3);
1979 country[3] = '\0';
1980 if (hostapd_set_country(hapd, country) < 0) {
1981 wpa_printf(MSG_ERROR, "Failed to set country code");
1982 return -1;
1983 }
1984
1985 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1986 previous_country, country);
1987
1988 if (os_strncmp(previous_country, country, 2) != 0) {
1989 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1990 iface->wait_channel_update = 1;
1991 eloop_register_timeout(5, 0,
1992 channel_list_update_timeout,
1993 iface, NULL);
1994 return 0;
1995 }
1996 }
1997
1998 return setup_interface2(iface);
1999}
2000
2001
2002static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
2003{
2004 int freq, i, j;
2005
2006 if (!iface->conf->channel)
2007 return 0;
2008 if (iface->conf->op_class) {
2009 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
2010 iface->conf->channel);
2011 if (freq < 0) {
2012 wpa_printf(MSG_INFO,
2013 "Could not convert op_class %u channel %u to operating frequency",
2014 iface->conf->op_class, iface->conf->channel);
2015 return -1;
2016 }
2017 iface->freq = freq;
2018 return 0;
2019 }
2020
2021 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
2022 * op_class parameter. Select a matching channel from the configured
2023 * mode using the channel parameter for these cases.
2024 */
2025 for (j = 0; j < iface->num_hw_features; j++) {
2026 struct hostapd_hw_modes *mode = &iface->hw_features[j];
2027
2028 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
2029 iface->conf->hw_mode != mode->mode)
2030 continue;
2031 for (i = 0; i < mode->num_channels; i++) {
2032 struct hostapd_channel_data *chan = &mode->channels[i];
2033
2034 if (chan->chan == iface->conf->channel &&
2035 !is_6ghz_freq(chan->freq)) {
2036 iface->freq = chan->freq;
2037 return 0;
2038 }
2039 }
2040 }
2041
2042 wpa_printf(MSG_INFO, "Could not determine operating frequency");
2043 return -1;
2044}
2045
2046
2047static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
2048{
2049 int bw;
2050
2051 if (!is_6ghz_op_class(iface->conf->op_class))
2052 return;
2053
2054 bw = op_class_to_bandwidth(iface->conf->op_class);
2055 /* Assign the secondary channel if absent in config for
2056 * bandwidths > 20 MHz */
2057 if (bw >= 40 && !iface->conf->secondary_channel) {
2058 if (((iface->conf->channel - 1) / 4) % 2)
2059 iface->conf->secondary_channel = -1;
2060 else
2061 iface->conf->secondary_channel = 1;
2062 }
2063}
2064
2065
2066static int setup_interface2(struct hostapd_iface *iface)
2067{
2068 iface->wait_channel_update = 0;
2069 iface->is_no_ir = false;
2070
2071 if (hostapd_get_hw_features(iface)) {
2072 /* Not all drivers support this yet, so continue without hw
2073 * feature data. */
2074 } else {
2075 int ret;
2076
2077 if (iface->conf->acs) {
2078 iface->freq = 0;
2079 iface->conf->channel = 0;
2080 }
2081
2082 ret = configured_fixed_chan_to_freq(iface);
2083 if (ret < 0)
2084 goto fail;
2085
2086 if (iface->conf->op_class) {
2087 enum oper_chan_width ch_width;
2088
2089 ch_width = op_class_to_ch_width(iface->conf->op_class);
2090 hostapd_set_oper_chwidth(iface->conf, ch_width);
2091 hostapd_set_6ghz_sec_chan(iface);
2092 }
2093
2094 ret = hostapd_select_hw_mode(iface);
2095 if (ret < 0) {
2096 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
2097 "channel. (%d)", ret);
2098 goto fail;
2099 }
2100 if (ret == 1) {
2101 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
2102 return 0;
2103 }
2104 ret = hostapd_check_edmg_capab(iface);
2105 if (ret < 0)
2106 goto fail;
2107 ret = hostapd_check_he_6ghz_capab(iface);
2108 if (ret < 0)
2109 goto fail;
2110 ret = hostapd_check_ht_capab(iface);
2111 if (ret < 0)
2112 goto fail;
2113 if (ret == 1) {
2114 wpa_printf(MSG_DEBUG, "Interface initialization will "
2115 "be completed in a callback");
2116 return 0;
2117 }
2118
2119 if (iface->conf->ieee80211h)
2120 wpa_printf(MSG_DEBUG, "DFS support is enabled");
2121 }
2122 return hostapd_setup_interface_complete(iface, 0);
2123
2124fail:
2125 if (iface->is_no_ir) {
2126 /* If AP is in NO_IR state, it can be reenabled by the driver
2127 * regulatory update and EVENT_CHANNEL_LIST_CHANGED. */
2128 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2129 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2130 return 0;
2131 }
2132
2133 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2134 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2135 if (iface->interfaces && iface->interfaces->terminate_on_error)
2136 eloop_terminate();
2137 return -1;
2138}
2139
2140
2141#ifdef CONFIG_FST
2142
2143static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
2144{
2145 struct hostapd_data *hapd = ctx;
2146
2147 return hapd->own_addr;
2148}
2149
2150
2151static void fst_hostapd_get_channel_info_cb(void *ctx,
2152 enum hostapd_hw_mode *hw_mode,
2153 u8 *channel)
2154{
2155 struct hostapd_data *hapd = ctx;
2156
2157 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
2158}
2159
2160
2161static int fst_hostapd_get_hw_modes_cb(void *ctx,
2162 struct hostapd_hw_modes **modes)
2163{
2164 struct hostapd_data *hapd = ctx;
2165
2166 *modes = hapd->iface->hw_features;
2167 return hapd->iface->num_hw_features;
2168}
2169
2170
2171static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
2172{
2173 struct hostapd_data *hapd = ctx;
2174
2175 if (hapd->iface->fst_ies != fst_ies) {
2176 hapd->iface->fst_ies = fst_ies;
2177 if (ieee802_11_set_beacon(hapd))
2178 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
2179 }
2180}
2181
2182
2183static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
2184 struct wpabuf *buf)
2185{
2186 struct hostapd_data *hapd = ctx;
2187
2188 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
2189 wpabuf_head(buf), wpabuf_len(buf));
2190}
2191
2192
2193static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
2194{
2195 struct hostapd_data *hapd = ctx;
2196 struct sta_info *sta = ap_get_sta(hapd, addr);
2197
2198 return sta ? sta->mb_ies : NULL;
2199}
2200
2201
2202static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
2203 const u8 *buf, size_t size)
2204{
2205 struct hostapd_data *hapd = ctx;
2206 struct sta_info *sta = ap_get_sta(hapd, addr);
2207
2208 if (sta) {
2209 struct mb_ies_info info;
2210
2211 if (!mb_ies_info_by_ies(&info, buf, size)) {
2212 wpabuf_free(sta->mb_ies);
2213 sta->mb_ies = mb_ies_by_info(&info);
2214 }
2215 }
2216}
2217
2218
2219static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
2220 bool mb_only)
2221{
2222 struct sta_info *s = (struct sta_info *) *get_ctx;
2223
2224 if (mb_only) {
2225 for (; s && !s->mb_ies; s = s->next)
2226 ;
2227 }
2228
2229 if (s) {
2230 *get_ctx = (struct fst_get_peer_ctx *) s->next;
2231
2232 return s->addr;
2233 }
2234
2235 *get_ctx = NULL;
2236 return NULL;
2237}
2238
2239
2240static const u8 * fst_hostapd_get_peer_first(void *ctx,
2241 struct fst_get_peer_ctx **get_ctx,
2242 bool mb_only)
2243{
2244 struct hostapd_data *hapd = ctx;
2245
2246 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
2247
2248 return fst_hostapd_get_sta(get_ctx, mb_only);
2249}
2250
2251
2252static const u8 * fst_hostapd_get_peer_next(void *ctx,
2253 struct fst_get_peer_ctx **get_ctx,
2254 bool mb_only)
2255{
2256 return fst_hostapd_get_sta(get_ctx, mb_only);
2257}
2258
2259
2260void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
2261 struct fst_wpa_obj *iface_obj)
2262{
2263 os_memset(iface_obj, 0, sizeof(*iface_obj));
2264 iface_obj->ctx = hapd;
2265 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
2266 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
2267 iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
2268 iface_obj->set_ies = fst_hostapd_set_ies_cb;
2269 iface_obj->send_action = fst_hostapd_send_action_cb;
2270 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
2271 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
2272 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
2273 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
2274}
2275
2276#endif /* CONFIG_FST */
2277
2278#ifdef CONFIG_OWE
2279
2280static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
2281{
2282 struct hostapd_data *hapd = ctx;
2283 size_t i;
2284
2285 for (i = 0; i < iface->num_bss; i++) {
2286 struct hostapd_data *bss = iface->bss[i];
2287
2288 if (os_strcmp(hapd->conf->owe_transition_ifname,
2289 bss->conf->iface) != 0)
2290 continue;
2291
2292 wpa_printf(MSG_DEBUG,
2293 "OWE: ifname=%s found transition mode ifname=%s BSSID "
2294 MACSTR " SSID %s",
2295 hapd->conf->iface, bss->conf->iface,
2296 MAC2STR(bss->own_addr),
2297 wpa_ssid_txt(bss->conf->ssid.ssid,
2298 bss->conf->ssid.ssid_len));
2299 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
2300 is_zero_ether_addr(bss->own_addr))
2301 continue;
2302
2303 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
2304 ETH_ALEN);
2305 os_memcpy(hapd->conf->owe_transition_ssid,
2306 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
2307 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
2308 wpa_printf(MSG_DEBUG,
2309 "OWE: Copied transition mode information");
2310 return 1;
2311 }
2312
2313 return 0;
2314}
2315
2316
2317int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
2318{
2319 if (hapd->conf->owe_transition_ssid_len > 0 &&
2320 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
2321 return 0;
2322
2323 /* Find transition mode SSID/BSSID information from a BSS operated by
2324 * this hostapd instance. */
2325 if (!hapd->iface->interfaces ||
2326 !hapd->iface->interfaces->for_each_interface)
2327 return hostapd_owe_iface_iter(hapd->iface, hapd);
2328 else
2329 return hapd->iface->interfaces->for_each_interface(
2330 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
2331}
2332
2333
2334static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
2335{
2336 size_t i;
2337
2338 for (i = 0; i < iface->num_bss; i++) {
2339 struct hostapd_data *bss = iface->bss[i];
2340 int res;
2341
2342 if (!bss->conf->owe_transition_ifname[0])
2343 continue;
2344 if (bss->iface->state != HAPD_IFACE_ENABLED) {
2345 wpa_printf(MSG_DEBUG,
2346 "OWE: Interface %s state %s - defer beacon update",
2347 bss->conf->iface,
2348 hostapd_state_text(bss->iface->state));
2349 continue;
2350 }
2351 res = hostapd_owe_trans_get_info(bss);
2352 if (res == 0)
2353 continue;
2354 wpa_printf(MSG_DEBUG,
2355 "OWE: Matching transition mode interface enabled - update beacon data for %s",
2356 bss->conf->iface);
2357 ieee802_11_set_beacon(bss);
2358 }
2359
2360 return 0;
2361}
2362
2363#endif /* CONFIG_OWE */
2364
2365
2366static void hostapd_owe_update_trans(struct hostapd_iface *iface)
2367{
2368#ifdef CONFIG_OWE
2369 /* Check whether the enabled BSS can complete OWE transition mode
2370 * configuration for any pending interface. */
2371 if (!iface->interfaces ||
2372 !iface->interfaces->for_each_interface)
2373 hostapd_owe_iface_iter2(iface, NULL);
2374 else
2375 iface->interfaces->for_each_interface(
2376 iface->interfaces, hostapd_owe_iface_iter2, NULL);
2377#endif /* CONFIG_OWE */
2378}
2379
2380
2381static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2382 void *timeout_ctx)
2383{
2384 struct hostapd_iface *iface = eloop_ctx;
2385 struct hostapd_data *hapd;
2386
2387 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2388 return;
2389 hapd = iface->bss[0];
2390 if (hapd->setup_complete_cb)
2391 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2392}
2393
2394
2395static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2396 int err)
2397{
2398 struct hostapd_data *hapd = iface->bss[0];
2399 size_t j;
2400 u8 *prev_addr;
2401 int delay_apply_cfg = 0;
2402 int res_dfs_offload = 0;
2403
2404 if (err)
2405 goto fail;
2406
2407 hostapd_ubus_add_iface(iface);
2408 wpa_printf(MSG_DEBUG, "Completing interface initialization");
2409 if (iface->freq) {
2410#ifdef NEED_AP_MLME
2411 int res;
2412#endif /* NEED_AP_MLME */
2413
2414 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2415 "Frequency: %d MHz",
2416 hostapd_hw_mode_txt(iface->conf->hw_mode),
2417 iface->conf->channel, iface->freq);
2418
2419#ifdef NEED_AP_MLME
2420 /* Handle DFS only if it is not offloaded to the driver */
2421 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2422 /* Check DFS */
2423 res = hostapd_handle_dfs(iface);
2424 if (res <= 0) {
2425 if (res < 0)
2426 goto fail;
2427 return res;
2428 }
2429 } else {
2430 /* If DFS is offloaded to the driver */
2431 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2432 if (res_dfs_offload <= 0) {
2433 if (res_dfs_offload < 0)
2434 goto fail;
2435 } else {
2436 wpa_printf(MSG_DEBUG,
2437 "Proceed with AP/channel setup");
2438 /*
2439 * If this is a DFS channel, move to completing
2440 * AP setup.
2441 */
2442 if (res_dfs_offload == 1)
2443 goto dfs_offload;
2444 /* Otherwise fall through. */
2445 }
2446 }
2447#endif /* NEED_AP_MLME */
2448
2449#ifdef CONFIG_MESH
2450 if (iface->mconf != NULL) {
2451 wpa_printf(MSG_DEBUG,
2452 "%s: Mesh configuration will be applied while joining the mesh network",
2453 iface->bss[0]->conf->iface);
2454 delay_apply_cfg = 1;
2455 }
2456#endif /* CONFIG_MESH */
2457
2458 if (!delay_apply_cfg &&
2459 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
2460 hapd->iconf->channel,
2461 hapd->iconf->enable_edmg,
2462 hapd->iconf->edmg_channel,
2463 hapd->iconf->ieee80211n,
2464 hapd->iconf->ieee80211ac,
2465 hapd->iconf->ieee80211ax,
2466 hapd->iconf->ieee80211be,
2467 hapd->iconf->secondary_channel,
2468 hostapd_get_oper_chwidth(hapd->iconf),
2469 hostapd_get_oper_centr_freq_seg0_idx(
2470 hapd->iconf),
2471 hostapd_get_oper_centr_freq_seg1_idx(
2472 hapd->iconf))) {
2473 wpa_printf(MSG_ERROR, "Could not set channel for "
2474 "kernel driver");
2475 goto fail;
2476 }
2477 }
2478
2479 if (iface->current_mode) {
2480 if (hostapd_prepare_rates(iface, iface->current_mode)) {
2481 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2482 "table.");
2483 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2484 HOSTAPD_LEVEL_WARNING,
2485 "Failed to prepare rates table.");
2486 goto fail;
2487 }
2488 }
2489
2490 if (hapd->iconf->rts_threshold >= -1 &&
2491 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2492 hapd->iconf->rts_threshold >= -1) {
2493 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2494 "kernel driver");
2495 goto fail;
2496 }
2497
2498 if (hapd->iconf->fragm_threshold >= -1 &&
2499 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2500 hapd->iconf->fragm_threshold != -1) {
2501 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2502 "for kernel driver");
2503 goto fail;
2504 }
2505
2506 prev_addr = hapd->own_addr;
2507
2508 for (j = 0; j < iface->num_bss; j++) {
2509 hapd = iface->bss[j];
2510 if (j)
2511 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
2512 if (hostapd_setup_bss(hapd, j == 0, !iface->conf->mbssid)) {
2513 for (;;) {
2514 hapd = iface->bss[j];
2515 hostapd_bss_deinit_no_free(hapd);
2516 hostapd_free_hapd_data(hapd);
2517 if (j == 0)
2518 break;
2519 j--;
2520 }
2521 goto fail;
2522 }
2523 if (is_zero_ether_addr(hapd->conf->bssid))
2524 prev_addr = hapd->own_addr;
2525 }
2526
2527 if (hapd->iconf->mbssid) {
2528 for (j = 0; hapd->iconf->mbssid && j < iface->num_bss; j++) {
2529 hapd = iface->bss[j];
2530 if (hostapd_start_beacon(hapd, true)) {
2531 for (;;) {
2532 hapd = iface->bss[j];
2533 hostapd_bss_deinit_no_free(hapd);
2534 hostapd_free_hapd_data(hapd);
2535 if (j == 0)
2536 break;
2537 j--;
2538 }
2539 goto fail;
2540 }
2541 }
2542 }
2543
2544 hapd = iface->bss[0];
2545
2546 hostapd_tx_queue_params(iface);
2547
2548 ap_list_init(iface);
2549
2550 hostapd_set_acl(hapd);
2551
2552 if (hostapd_driver_commit(hapd) < 0) {
2553 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2554 "configuration", __func__);
2555 goto fail;
2556 }
2557
2558 /*
2559 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2560 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2561 * mode), the interface is up only after driver_commit, so initialize
2562 * WPS after driver_commit.
2563 */
2564 for (j = 0; j < iface->num_bss; j++) {
2565 if (hostapd_init_wps_complete(iface->bss[j]))
2566 goto fail;
2567 }
2568
2569 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2570 !res_dfs_offload) {
2571 /*
2572 * If freq is DFS, and DFS is offloaded to the driver, then wait
2573 * for CAC to complete.
2574 */
2575 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2576 return res_dfs_offload;
2577 }
2578
2579#ifdef NEED_AP_MLME
2580dfs_offload:
2581#endif /* NEED_AP_MLME */
2582
2583#ifdef CONFIG_FST
2584 if (hapd->iconf->fst_cfg.group_id[0]) {
2585 struct fst_wpa_obj iface_obj;
2586
2587 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2588 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2589 &iface_obj, &hapd->iconf->fst_cfg);
2590 if (!iface->fst) {
2591 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2592 hapd->iconf->fst_cfg.group_id);
2593 goto fail;
2594 }
2595 }
2596#endif /* CONFIG_FST */
2597
2598 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
2599 hostapd_owe_update_trans(iface);
2600 airtime_policy_update_init(iface);
2601 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
2602 if (hapd->setup_complete_cb)
2603 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2604
2605#ifdef CONFIG_MESH
2606 if (delay_apply_cfg && !iface->mconf) {
2607 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2608 goto fail;
2609 }
2610#endif /* CONFIG_MESH */
2611
2612 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2613 iface->bss[0]->conf->iface);
2614 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2615 iface->interfaces->terminate_on_error--;
2616
2617 for (j = 0; j < iface->num_bss; j++)
2618 hostapd_neighbor_set_own_report(iface->bss[j]);
2619
2620 if (iface->interfaces && iface->interfaces->count > 1)
2621 ieee802_11_set_beacons(iface);
2622
2623 return 0;
2624
2625fail:
2626 wpa_printf(MSG_ERROR, "Interface initialization failed");
2627 hostapd_ubus_free_iface(iface);
2628
2629 if (iface->is_no_ir) {
2630 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2631 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2632 return 0;
2633 }
2634
2635 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2636 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2637#ifdef CONFIG_FST
2638 if (iface->fst) {
2639 fst_detach(iface->fst);
2640 iface->fst = NULL;
2641 }
2642#endif /* CONFIG_FST */
2643
2644 if (iface->interfaces && iface->interfaces->terminate_on_error) {
2645 eloop_terminate();
2646 } else if (hapd->setup_complete_cb) {
2647 /*
2648 * Calling hapd->setup_complete_cb directly may cause iface
2649 * deinitialization which may be accessed later by the caller.
2650 */
2651 eloop_register_timeout(0, 0,
2652 hostapd_interface_setup_failure_handler,
2653 iface, NULL);
2654 }
2655
2656 return -1;
2657}
2658
2659
2660/**
2661 * hostapd_setup_interface_complete - Complete interface setup
2662 *
2663 * This function is called when previous steps in the interface setup has been
2664 * completed. This can also start operations, e.g., DFS, that will require
2665 * additional processing before interface is ready to be enabled. Such
2666 * operations will call this function from eloop callbacks when finished.
2667 */
2668int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2669{
2670 struct hapd_interfaces *interfaces = iface->interfaces;
2671 struct hostapd_data *hapd = iface->bss[0];
2672 unsigned int i;
2673 int not_ready_in_sync_ifaces = 0;
2674
2675 if (!iface->need_to_start_in_sync)
2676 return hostapd_setup_interface_complete_sync(iface, err);
2677
2678 if (err) {
2679 wpa_printf(MSG_ERROR, "Interface initialization failed");
2680 iface->need_to_start_in_sync = 0;
2681
2682 if (iface->is_no_ir) {
2683 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2684 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2685 return 0;
2686 }
2687
2688 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2689 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2690 if (interfaces && interfaces->terminate_on_error)
2691 eloop_terminate();
2692 return -1;
2693 }
2694
2695 if (iface->ready_to_start_in_sync) {
2696 /* Already in ready and waiting. should never happpen */
2697 return 0;
2698 }
2699
2700 for (i = 0; i < interfaces->count; i++) {
2701 if (interfaces->iface[i]->need_to_start_in_sync &&
2702 !interfaces->iface[i]->ready_to_start_in_sync)
2703 not_ready_in_sync_ifaces++;
2704 }
2705
2706 /*
2707 * Check if this is the last interface, if yes then start all the other
2708 * waiting interfaces. If not, add this interface to the waiting list.
2709 */
2710 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2711 /*
2712 * If this interface went through CAC, do not synchronize, just
2713 * start immediately.
2714 */
2715 iface->need_to_start_in_sync = 0;
2716 wpa_printf(MSG_INFO,
2717 "%s: Finished CAC - bypass sync and start interface",
2718 iface->bss[0]->conf->iface);
2719 return hostapd_setup_interface_complete_sync(iface, err);
2720 }
2721
2722 if (not_ready_in_sync_ifaces > 1) {
2723 /* need to wait as there are other interfaces still coming up */
2724 iface->ready_to_start_in_sync = 1;
2725 wpa_printf(MSG_INFO,
2726 "%s: Interface waiting to sync with other interfaces",
2727 iface->bss[0]->conf->iface);
2728 return 0;
2729 }
2730
2731 wpa_printf(MSG_INFO,
2732 "%s: Last interface to sync - starting all interfaces",
2733 iface->bss[0]->conf->iface);
2734 iface->need_to_start_in_sync = 0;
2735 hostapd_setup_interface_complete_sync(iface, err);
2736 for (i = 0; i < interfaces->count; i++) {
2737 if (interfaces->iface[i]->need_to_start_in_sync &&
2738 interfaces->iface[i]->ready_to_start_in_sync) {
2739 hostapd_setup_interface_complete_sync(
2740 interfaces->iface[i], 0);
2741 /* Only once the interfaces are sync started */
2742 interfaces->iface[i]->need_to_start_in_sync = 0;
2743 }
2744 }
2745
2746 return 0;
2747}
2748
2749
2750/**
2751 * hostapd_setup_interface - Setup of an interface
2752 * @iface: Pointer to interface data.
2753 * Returns: 0 on success, -1 on failure
2754 *
2755 * Initializes the driver interface, validates the configuration,
2756 * and sets driver parameters based on the configuration.
2757 * Flushes old stations, sets the channel, encryption,
2758 * beacons, and WDS links based on the configuration.
2759 *
2760 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2761 * or DFS operations, this function returns 0 before such operations have been
2762 * completed. The pending operations are registered into eloop and will be
2763 * completed from eloop callbacks. Those callbacks end up calling
2764 * hostapd_setup_interface_complete() once setup has been completed.
2765 */
2766int hostapd_setup_interface(struct hostapd_iface *iface)
2767{
2768 int ret;
2769
2770 if (!iface->conf)
2771 return -1;
2772 ret = setup_interface(iface);
2773 if (ret) {
2774 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2775 iface->conf->bss[0]->iface);
2776 return -1;
2777 }
2778
2779 return 0;
2780}
2781
2782
2783/**
2784 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2785 * @hapd_iface: Pointer to interface data
2786 * @conf: Pointer to per-interface configuration
2787 * @bss: Pointer to per-BSS configuration for this BSS
2788 * Returns: Pointer to allocated BSS data
2789 *
2790 * This function is used to allocate per-BSS data structure. This data will be
2791 * freed after hostapd_cleanup() is called for it during interface
2792 * deinitialization.
2793 */
2794struct hostapd_data *
2795hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2796 struct hostapd_config *conf,
2797 struct hostapd_bss_config *bss)
2798{
2799 struct hostapd_data *hapd;
2800
2801 hapd = os_zalloc(sizeof(*hapd));
2802 if (hapd == NULL)
2803 return NULL;
2804
2805 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2806 hapd->iconf = conf;
2807 hapd->conf = bss;
2808 hapd->iface = hapd_iface;
2809 if (bss && bss->config_id)
2810 hapd->config_id = strdup(bss->config_id);
2811 else
2812 hapd->config_id = NULL;
2813 if (conf)
2814 hapd->driver = conf->driver;
2815 hapd->ctrl_sock = -1;
2816 dl_list_init(&hapd->ctrl_dst);
2817 dl_list_init(&hapd->nr_db);
2818 hapd->dhcp_sock = -1;
2819#ifdef CONFIG_IEEE80211R_AP
2820 dl_list_init(&hapd->l2_queue);
2821 dl_list_init(&hapd->l2_oui_queue);
2822#endif /* CONFIG_IEEE80211R_AP */
2823#ifdef CONFIG_SAE
2824 dl_list_init(&hapd->sae_commit_queue);
2825#endif /* CONFIG_SAE */
2826
2827 return hapd;
2828}
2829
2830
2831static void hostapd_bss_deinit(struct hostapd_data *hapd)
2832{
2833 if (!hapd)
2834 return;
2835 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
2836 hapd->conf ? hapd->conf->iface : "N/A");
2837 hostapd_bss_deinit_no_free(hapd);
2838 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2839#ifdef CONFIG_SQLITE
2840 if (hapd->rad_attr_db) {
2841 sqlite3_close(hapd->rad_attr_db);
2842 hapd->rad_attr_db = NULL;
2843 }
2844#endif /* CONFIG_SQLITE */
2845 hostapd_cleanup(hapd);
2846}
2847
2848
2849void hostapd_interface_deinit(struct hostapd_iface *iface)
2850{
2851 int j;
2852
2853 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2854 if (iface == NULL)
2855 return;
2856
2857 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2858
2859 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2860 iface->wait_channel_update = 0;
2861 iface->is_no_ir = false;
2862
2863#ifdef CONFIG_FST
2864 if (iface->fst) {
2865 fst_detach(iface->fst);
2866 iface->fst = NULL;
2867 }
2868#endif /* CONFIG_FST */
2869
2870 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
2871 if (!iface->bss)
2872 break;
2873 hostapd_bss_deinit(iface->bss[j]);
2874 }
2875
2876#ifdef NEED_AP_MLME
2877 hostapd_stop_setup_timers(iface);
2878 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2879#endif /* NEED_AP_MLME */
2880}
2881
2882
2883void hostapd_interface_free(struct hostapd_iface *iface)
2884{
2885 size_t j;
2886 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2887 for (j = 0; j < iface->num_bss; j++) {
2888 if (!iface->bss)
2889 break;
2890 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2891 __func__, iface->bss[j]);
2892 os_free(iface->bss[j]);
2893 }
2894 hostapd_cleanup_iface(iface);
2895}
2896
2897
2898struct hostapd_iface * hostapd_alloc_iface(void)
2899{
2900 struct hostapd_iface *hapd_iface;
2901
2902 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2903 if (!hapd_iface)
2904 return NULL;
2905
2906 dl_list_init(&hapd_iface->sta_seen);
2907
2908 return hapd_iface;
2909}
2910
2911
2912/**
2913 * hostapd_init - Allocate and initialize per-interface data
2914 * @config_file: Path to the configuration file
2915 * Returns: Pointer to the allocated interface data or %NULL on failure
2916 *
2917 * This function is used to allocate main data structures for per-interface
2918 * data. The allocated data buffer will be freed by calling
2919 * hostapd_cleanup_iface().
2920 */
2921struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2922 const char *config_file)
2923{
2924 struct hostapd_iface *hapd_iface = NULL;
2925 struct hostapd_config *conf = NULL;
2926 struct hostapd_data *hapd;
2927 size_t i;
2928
2929 hapd_iface = hostapd_alloc_iface();
2930 if (hapd_iface == NULL)
2931 goto fail;
2932
2933 hapd_iface->config_fname = os_strdup(config_file);
2934 if (hapd_iface->config_fname == NULL)
2935 goto fail;
2936
2937 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2938 if (conf == NULL)
2939 goto fail;
2940 hapd_iface->conf = conf;
2941
2942 hapd_iface->num_bss = conf->num_bss;
2943 hapd_iface->bss = os_calloc(conf->num_bss,
2944 sizeof(struct hostapd_data *));
2945 if (hapd_iface->bss == NULL)
2946 goto fail;
2947
2948 for (i = 0; i < conf->num_bss; i++) {
2949 hapd = hapd_iface->bss[i] =
2950 hostapd_alloc_bss_data(hapd_iface, conf,
2951 conf->bss[i]);
2952 if (hapd == NULL)
2953 goto fail;
2954 hapd->msg_ctx = hapd;
2955 }
2956
2957 return hapd_iface;
2958
2959fail:
2960 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2961 config_file);
2962 if (conf)
2963 hostapd_config_free(conf);
2964 if (hapd_iface) {
2965 os_free(hapd_iface->config_fname);
2966 os_free(hapd_iface->bss);
2967 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2968 __func__, hapd_iface);
2969 os_free(hapd_iface);
2970 }
2971 return NULL;
2972}
2973
2974
2975static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2976{
2977 size_t i, j;
2978
2979 for (i = 0; i < interfaces->count; i++) {
2980 struct hostapd_iface *iface = interfaces->iface[i];
2981 for (j = 0; j < iface->num_bss; j++) {
2982 struct hostapd_data *hapd = iface->bss[j];
2983 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2984 return 1;
2985 }
2986 }
2987
2988 return 0;
2989}
2990
2991
2992/**
2993 * hostapd_interface_init_bss - Read configuration file and init BSS data
2994 *
2995 * This function is used to parse configuration file for a BSS. This BSS is
2996 * added to an existing interface sharing the same radio (if any) or a new
2997 * interface is created if this is the first interface on a radio. This
2998 * allocate memory for the BSS. No actual driver operations are started.
2999 *
3000 * This is similar to hostapd_interface_init(), but for a case where the
3001 * configuration is used to add a single BSS instead of all BSSes for a radio.
3002 */
3003struct hostapd_iface *
3004hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
3005 const char *config_fname, int debug)
3006{
3007 struct hostapd_iface *new_iface = NULL, *iface = NULL;
3008 struct hostapd_data *hapd;
3009 int k;
3010 size_t i, bss_idx;
3011
3012 if (!phy || !*phy)
3013 return NULL;
3014
3015 for (i = 0; i < interfaces->count; i++) {
3016 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
3017 iface = interfaces->iface[i];
3018 break;
3019 }
3020 }
3021
3022 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
3023 config_fname, phy, iface ? "" : " --> new PHY");
3024 if (iface) {
3025 struct hostapd_config *conf;
3026 struct hostapd_bss_config **tmp_conf;
3027 struct hostapd_data **tmp_bss;
3028 struct hostapd_bss_config *bss;
3029 const char *ifname;
3030
3031 /* Add new BSS to existing iface */
3032 conf = interfaces->config_read_cb(config_fname);
3033 if (conf == NULL)
3034 return NULL;
3035 if (conf->num_bss > 1) {
3036 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
3037 hostapd_config_free(conf);
3038 return NULL;
3039 }
3040
3041 ifname = conf->bss[0]->iface;
3042 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
3043 wpa_printf(MSG_ERROR,
3044 "Interface name %s already in use", ifname);
3045 hostapd_config_free(conf);
3046 return NULL;
3047 }
3048
3049 tmp_conf = os_realloc_array(
3050 iface->conf->bss, iface->conf->num_bss + 1,
3051 sizeof(struct hostapd_bss_config *));
3052 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
3053 sizeof(struct hostapd_data *));
3054 if (tmp_bss)
3055 iface->bss = tmp_bss;
3056 if (tmp_conf) {
3057 iface->conf->bss = tmp_conf;
3058 iface->conf->last_bss = tmp_conf[0];
3059 }
3060 if (tmp_bss == NULL || tmp_conf == NULL) {
3061 hostapd_config_free(conf);
3062 return NULL;
3063 }
3064 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
3065 iface->conf->num_bss++;
3066
3067 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
3068 if (hapd == NULL) {
3069 iface->conf->num_bss--;
3070 hostapd_config_free(conf);
3071 return NULL;
3072 }
3073 iface->conf->last_bss = bss;
3074 iface->bss[iface->num_bss] = hapd;
3075 hapd->msg_ctx = hapd;
3076
3077 bss_idx = iface->num_bss++;
3078 conf->num_bss--;
3079 conf->bss[0] = NULL;
3080 hostapd_config_free(conf);
3081 } else {
3082 /* Add a new iface with the first BSS */
3083 new_iface = iface = hostapd_init(interfaces, config_fname);
3084 if (!iface)
3085 return NULL;
3086 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
3087 iface->interfaces = interfaces;
3088 bss_idx = 0;
3089 }
3090
3091 for (k = 0; k < debug; k++) {
3092 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
3093 iface->bss[bss_idx]->conf->logger_stdout_level--;
3094 }
3095
3096 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
3097 !hostapd_drv_none(iface->bss[bss_idx])) {
3098 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
3099 config_fname);
3100 if (new_iface)
3101 hostapd_interface_deinit_free(new_iface);
3102 return NULL;
3103 }
3104
3105 return iface;
3106}
3107
3108
3109void hostapd_interface_deinit_free(struct hostapd_iface *iface)
3110{
3111 const struct wpa_driver_ops *driver;
3112 void *drv_priv;
3113
3114 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
3115 if (iface == NULL)
3116 return;
3117 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
3118 __func__, (unsigned int) iface->num_bss,
3119 (unsigned int) iface->conf->num_bss);
3120 driver = iface->bss[0]->driver;
3121 drv_priv = iface->bss[0]->drv_priv;
3122 hostapd_ubus_free_iface(iface);
3123 hostapd_interface_deinit(iface);
3124 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3125 __func__, driver, drv_priv);
3126 if (driver && driver->hapd_deinit && drv_priv) {
3127 if (!iface->bss[0]->mld_first_bss)
3128 driver->hapd_deinit(drv_priv);
3129 hostapd_clear_drv_priv(iface->bss[0]);
3130 }
3131 hostapd_interface_free(iface);
3132}
3133
3134
3135static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
3136 void *drv_priv,
3137 struct hostapd_iface *hapd_iface)
3138{
3139 size_t j;
3140
3141 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3142 __func__, driver, drv_priv);
3143 if (driver && driver->hapd_deinit && drv_priv) {
3144 if (!hapd_iface->bss[0]->mld_first_bss)
3145 driver->hapd_deinit(drv_priv);
3146 for (j = 0; j < hapd_iface->num_bss; j++) {
3147 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
3148 __func__, (int) j,
3149 hapd_iface->bss[j]->drv_priv);
3150 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
3151 hostapd_clear_drv_priv(hapd_iface->bss[j]);
3152 hapd_iface->extended_capa = NULL;
3153 hapd_iface->extended_capa_mask = NULL;
3154 hapd_iface->extended_capa_len = 0;
3155 }
3156 }
3157 }
3158}
3159
3160
3161int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
3162{
3163 size_t j;
3164
3165 if (!hapd_iface)
3166 return -1;
3167
3168 if (hapd_iface->enable_iface_cb)
3169 return hapd_iface->enable_iface_cb(hapd_iface);
3170
3171 if (hapd_iface->bss[0]->drv_priv != NULL) {
3172 wpa_printf(MSG_ERROR, "Interface %s already enabled",
3173 hapd_iface->conf->bss[0]->iface);
3174 return -1;
3175 }
3176
3177 wpa_printf(MSG_DEBUG, "Enable interface %s",
3178 hapd_iface->conf->bss[0]->iface);
3179
3180 for (j = 0; j < hapd_iface->num_bss; j++)
3181 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
3182 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
3183 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
3184 return -1;
3185 }
3186
3187 if (hapd_iface->interfaces == NULL ||
3188 hapd_iface->interfaces->driver_init == NULL ||
3189 hapd_iface->interfaces->driver_init(hapd_iface))
3190 return -1;
3191
3192 if (hostapd_setup_interface(hapd_iface)) {
3193 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
3194 hapd_iface->bss[0]->drv_priv,
3195 hapd_iface);
3196 return -1;
3197 }
3198
3199 return 0;
3200}
3201
3202
3203int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
3204{
3205 size_t j;
3206
3207 wpa_printf(MSG_DEBUG, "Reload interface %s",
3208 hapd_iface->conf->bss[0]->iface);
3209 for (j = 0; j < hapd_iface->num_bss; j++)
3210 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
3211 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
3212 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
3213 return -1;
3214 }
3215 hostapd_clear_old(hapd_iface);
3216 for (j = 0; j < hapd_iface->num_bss; j++)
3217 hostapd_reload_bss(hapd_iface->bss[j]);
3218
3219 return 0;
3220}
3221
3222
3223int hostapd_reload_bss_only(struct hostapd_data *bss)
3224{
3225
3226 wpa_printf(MSG_DEBUG, "Reload BSS %s", bss->conf->iface);
3227 hostapd_set_security_params(bss->conf, 1);
3228 if (hostapd_config_check(bss->iconf, 1) < 0) {
3229 wpa_printf(MSG_ERROR, "Updated BSS configuration is invalid");
3230 return -1;
3231 }
3232 hostapd_clear_old_bss(bss);
3233 hostapd_reload_bss(bss);
3234 return 0;
3235}
3236
3237
3238int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
3239{
3240 size_t j;
3241 const struct wpa_driver_ops *driver;
3242 void *drv_priv;
3243
3244 if (hapd_iface == NULL)
3245 return -1;
3246
3247 if (hapd_iface->disable_iface_cb)
3248 return hapd_iface->disable_iface_cb(hapd_iface);
3249
3250 if (hapd_iface->bss[0]->drv_priv == NULL) {
3251 wpa_printf(MSG_INFO, "Interface %s already disabled",
3252 hapd_iface->conf->bss[0]->iface);
3253 return -1;
3254 }
3255
3256 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
3257 driver = hapd_iface->bss[0]->driver;
3258 drv_priv = hapd_iface->bss[0]->drv_priv;
3259
3260 hapd_iface->driver_ap_teardown =
3261 !!(hapd_iface->drv_flags &
3262 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3263
3264#ifdef NEED_AP_MLME
3265 for (j = 0; j < hapd_iface->num_bss; j++)
3266 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
3267#endif /* NEED_AP_MLME */
3268
3269 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
3270 for (j = 0; j < hapd_iface->num_bss; j++) {
3271 struct hostapd_data *hapd = hapd_iface->bss[j];
3272 hostapd_bss_deinit_no_free(hapd);
3273 hostapd_free_hapd_data(hapd);
3274 }
3275
3276 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
3277
3278 /* From hostapd_cleanup_iface: These were initialized in
3279 * hostapd_setup_interface and hostapd_setup_interface_complete
3280 */
3281 hostapd_cleanup_iface_partial(hapd_iface);
3282
3283 wpa_printf(MSG_DEBUG, "Interface %s disabled",
3284 hapd_iface->bss[0]->conf->iface);
3285 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
3286 return 0;
3287}
3288
3289
3290static struct hostapd_iface *
3291hostapd_iface_alloc(struct hapd_interfaces *interfaces)
3292{
3293 struct hostapd_iface **iface, *hapd_iface;
3294
3295 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
3296 sizeof(struct hostapd_iface *));
3297 if (iface == NULL)
3298 return NULL;
3299 interfaces->iface = iface;
3300 hapd_iface = interfaces->iface[interfaces->count] =
3301 hostapd_alloc_iface();
3302 if (hapd_iface == NULL) {
3303 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3304 "the interface", __func__);
3305 return NULL;
3306 }
3307 interfaces->count++;
3308 hapd_iface->interfaces = interfaces;
3309
3310 return hapd_iface;
3311}
3312
3313
3314static struct hostapd_config *
3315hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
3316 const char *ctrl_iface, const char *driver)
3317{
3318 struct hostapd_bss_config *bss;
3319 struct hostapd_config *conf;
3320
3321 /* Allocates memory for bss and conf */
3322 conf = hostapd_config_defaults();
3323 if (conf == NULL) {
3324 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3325 "configuration", __func__);
3326 return NULL;
3327 }
3328
3329 if (driver) {
3330 int j;
3331
3332 for (j = 0; wpa_drivers[j]; j++) {
3333 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
3334 conf->driver = wpa_drivers[j];
3335 goto skip;
3336 }
3337 }
3338
3339 wpa_printf(MSG_ERROR,
3340 "Invalid/unknown driver '%s' - registering the default driver",
3341 driver);
3342 }
3343
3344 conf->driver = wpa_drivers[0];
3345 if (conf->driver == NULL) {
3346 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3347 hostapd_config_free(conf);
3348 return NULL;
3349 }
3350
3351skip:
3352 bss = conf->last_bss = conf->bss[0];
3353
3354 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
3355 bss->ctrl_interface = os_strdup(ctrl_iface);
3356 if (bss->ctrl_interface == NULL) {
3357 hostapd_config_free(conf);
3358 return NULL;
3359 }
3360
3361 /* Reading configuration file skipped, will be done in SET!
3362 * From reading the configuration till the end has to be done in
3363 * SET
3364 */
3365 return conf;
3366}
3367
3368
3369static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
3370 struct hostapd_config *conf)
3371{
3372 size_t i;
3373 struct hostapd_data *hapd;
3374
3375 hapd_iface->bss = os_calloc(conf->num_bss,
3376 sizeof(struct hostapd_data *));
3377 if (hapd_iface->bss == NULL)
3378 return -1;
3379
3380 for (i = 0; i < conf->num_bss; i++) {
3381 hapd = hapd_iface->bss[i] =
3382 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
3383 if (hapd == NULL) {
3384 while (i > 0) {
3385 i--;
3386 os_free(hapd_iface->bss[i]);
3387 hapd_iface->bss[i] = NULL;
3388 }
3389 os_free(hapd_iface->bss);
3390 hapd_iface->bss = NULL;
3391 return -1;
3392 }
3393 hapd->msg_ctx = hapd;
3394 }
3395
3396 hapd_iface->conf = conf;
3397 hapd_iface->num_bss = conf->num_bss;
3398
3399 return 0;
3400}
3401
3402
3403int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
3404{
3405 struct hostapd_config *conf = NULL;
3406 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
3407 struct hostapd_data *hapd;
3408 char *ptr;
3409 size_t i, j;
3410 const char *conf_file = NULL, *phy_name = NULL;
3411
3412 if (os_strncmp(buf, "bss_config=", 11) == 0) {
3413 char *pos;
3414 phy_name = buf + 11;
3415 pos = os_strchr(phy_name, ':');
3416 if (!pos)
3417 return -1;
3418 *pos++ = '\0';
3419 conf_file = pos;
3420 if (!os_strlen(conf_file))
3421 return -1;
3422
3423 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
3424 conf_file, 0);
3425 if (!hapd_iface)
3426 return -1;
3427 for (j = 0; j < interfaces->count; j++) {
3428 if (interfaces->iface[j] == hapd_iface)
3429 break;
3430 }
3431 if (j == interfaces->count) {
3432 struct hostapd_iface **tmp;
3433 tmp = os_realloc_array(interfaces->iface,
3434 interfaces->count + 1,
3435 sizeof(struct hostapd_iface *));
3436 if (!tmp) {
3437 hostapd_interface_deinit_free(hapd_iface);
3438 return -1;
3439 }
3440 interfaces->iface = tmp;
3441 interfaces->iface[interfaces->count++] = hapd_iface;
3442 new_iface = hapd_iface;
3443 }
3444
3445 if (new_iface) {
3446 if (interfaces->driver_init(hapd_iface))
3447 goto fail;
3448
3449 if (hostapd_setup_interface(hapd_iface)) {
3450 hostapd_deinit_driver(
3451 hapd_iface->bss[0]->driver,
3452 hapd_iface->bss[0]->drv_priv,
3453 hapd_iface);
3454 goto fail;
3455 }
3456 } else {
3457 /* Assign new BSS with bss[0]'s driver info */
3458 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3459 hapd->driver = hapd_iface->bss[0]->driver;
3460 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3461 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3462 ETH_ALEN);
3463
3464 if (start_ctrl_iface_bss(hapd) < 0 ||
3465 (hapd_iface->state == HAPD_IFACE_ENABLED &&
3466 hostapd_setup_bss(hapd, -1, true))) {
3467 hostapd_cleanup(hapd);
3468 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
3469 hapd_iface->conf->num_bss--;
3470 hapd_iface->num_bss--;
3471 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3472 __func__, hapd, hapd->conf->iface);
3473 hostapd_config_free_bss(hapd->conf);
3474 hapd->conf = NULL;
3475 os_free(hapd);
3476 return -1;
3477 }
3478 }
3479 hostapd_owe_update_trans(hapd_iface);
3480 return 0;
3481 }
3482
3483 ptr = os_strchr(buf, ' ');
3484 if (ptr == NULL)
3485 return -1;
3486 *ptr++ = '\0';
3487
3488 if (os_strncmp(ptr, "config=", 7) == 0)
3489 conf_file = ptr + 7;
3490
3491 for (i = 0; i < interfaces->count; i++) {
3492 bool mld_ap = false;
3493
3494#ifdef CONFIG_IEEE80211BE
3495 mld_ap = interfaces->iface[i]->conf->bss[0]->mld_ap;
3496#endif /* CONFIG_IEEE80211BE */
3497
3498 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
3499 buf) && !mld_ap) {
3500 wpa_printf(MSG_INFO, "Cannot add interface - it "
3501 "already exists");
3502 return -1;
3503 }
3504 }
3505
3506 hapd_iface = hostapd_iface_alloc(interfaces);
3507 if (hapd_iface == NULL) {
3508 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3509 "for interface", __func__);
3510 goto fail;
3511 }
3512 new_iface = hapd_iface;
3513
3514 if (conf_file && interfaces->config_read_cb) {
3515 conf = interfaces->config_read_cb(conf_file);
3516 if (conf && conf->bss)
3517 os_strlcpy(conf->bss[0]->iface, buf,
3518 sizeof(conf->bss[0]->iface));
3519 } else {
3520 char *driver = os_strchr(ptr, ' ');
3521
3522 if (driver)
3523 *driver++ = '\0';
3524 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3525 }
3526
3527 if (conf == NULL || conf->bss == NULL) {
3528 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3529 "for configuration", __func__);
3530 goto fail;
3531 }
3532
3533 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
3534 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3535 "for hostapd", __func__);
3536 goto fail;
3537 }
3538 conf = NULL;
3539
3540 if (start_ctrl_iface(hapd_iface) < 0)
3541 goto fail;
3542
3543 wpa_printf(MSG_INFO, "Add interface '%s'",
3544 hapd_iface->conf->bss[0]->iface);
3545
3546 return 0;
3547
3548fail:
3549 if (conf)
3550 hostapd_config_free(conf);
3551 if (hapd_iface) {
3552 if (hapd_iface->bss) {
3553 for (i = 0; i < hapd_iface->num_bss; i++) {
3554 hapd = hapd_iface->bss[i];
3555 if (!hapd)
3556 continue;
3557 if (hapd_iface->interfaces &&
3558 hapd_iface->interfaces->ctrl_iface_deinit)
3559 hapd_iface->interfaces->
3560 ctrl_iface_deinit(hapd);
3561 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3562 __func__, hapd_iface->bss[i],
3563 hapd->conf->iface);
3564 hostapd_cleanup(hapd);
3565 os_free(hapd);
3566 hapd_iface->bss[i] = NULL;
3567 }
3568 os_free(hapd_iface->bss);
3569 hapd_iface->bss = NULL;
3570 }
3571 if (new_iface) {
3572 interfaces->count--;
3573 interfaces->iface[interfaces->count] = NULL;
3574 }
3575 hostapd_cleanup_iface(hapd_iface);
3576 }
3577 return -1;
3578}
3579
3580
3581static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3582{
3583 size_t i;
3584
3585 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
3586
3587 /* Remove hostapd_data only if it has already been initialized */
3588 if (idx < iface->num_bss) {
3589 struct hostapd_data *hapd = iface->bss[idx];
3590
3591 hostapd_bss_deinit(hapd);
3592 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3593 __func__, hapd, hapd->conf->iface);
3594 hostapd_config_free_bss(hapd->conf);
3595 hapd->conf = NULL;
3596 os_free(hapd);
3597
3598 iface->num_bss--;
3599
3600 for (i = idx; i < iface->num_bss; i++)
3601 iface->bss[i] = iface->bss[i + 1];
3602 } else {
3603 hostapd_config_free_bss(iface->conf->bss[idx]);
3604 iface->conf->bss[idx] = NULL;
3605 }
3606
3607 iface->conf->num_bss--;
3608 for (i = idx; i < iface->conf->num_bss; i++)
3609 iface->conf->bss[i] = iface->conf->bss[i + 1];
3610
3611 return 0;
3612}
3613
3614
3615int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3616{
3617 struct hostapd_iface *hapd_iface;
3618 size_t i, j, k = 0;
3619
3620 for (i = 0; i < interfaces->count; i++) {
3621 hapd_iface = interfaces->iface[i];
3622 if (hapd_iface == NULL)
3623 return -1;
3624 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
3625 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
3626 hapd_iface->driver_ap_teardown =
3627 !!(hapd_iface->drv_flags &
3628 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3629
3630 hostapd_interface_deinit_free(hapd_iface);
3631 k = i;
3632 while (k < (interfaces->count - 1)) {
3633 interfaces->iface[k] =
3634 interfaces->iface[k + 1];
3635 k++;
3636 }
3637 interfaces->count--;
3638 return 0;
3639 }
3640
3641 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
3642 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3643 hapd_iface->driver_ap_teardown =
3644 !(hapd_iface->drv_flags &
3645 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3646 return hostapd_remove_bss(hapd_iface, j);
3647 }
3648 }
3649 }
3650 return -1;
3651}
3652
3653
3654/**
3655 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3656 * @hapd: Pointer to BSS data
3657 * @sta: Pointer to the associated STA data
3658 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3659 *
3660 * This function will be called whenever a station associates with the AP. It
3661 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3662 * from drv_callbacks.c based on driver events for drivers that take care of
3663 * management frames (IEEE 802.11 authentication and association) internally.
3664 */
3665void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3666 int reassoc)
3667{
3668 if (hapd->tkip_countermeasures) {
3669 hostapd_drv_sta_deauth(hapd, sta->addr,
3670 WLAN_REASON_MICHAEL_MIC_FAILURE);
3671 return;
3672 }
3673
3674#ifdef CONFIG_IEEE80211BE
3675 if (hapd->conf->mld_ap && sta->mld_info.mld_sta &&
3676 sta->mld_assoc_link_id != hapd->mld_link_id)
3677 return;
3678#endif /* CONFIG_IEEE80211BE */
3679
3680 ap_sta_clear_disconnect_timeouts(hapd, sta);
3681 sta->post_csa_sa_query = 0;
3682
3683#ifdef CONFIG_P2P
3684 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3685 sta->no_p2p_set = 1;
3686 hapd->num_sta_no_p2p++;
3687 if (hapd->num_sta_no_p2p == 1)
3688 hostapd_p2p_non_p2p_sta_connected(hapd);
3689 }
3690#endif /* CONFIG_P2P */
3691
3692 airtime_policy_new_sta(hapd, sta);
3693
3694 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3695 * IEEE 802.1X/WPA code will start accounting after the station has
3696 * been authorized. */
3697 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3698 ap_sta_set_authorized(hapd, sta, 1);
3699 os_get_reltime(&sta->connected_time);
3700 accounting_sta_start(hapd, sta);
3701 }
3702
3703 /* Start IEEE 802.1X authentication process for new stations */
3704 ieee802_1x_new_station(hapd, sta);
3705 if (reassoc) {
3706 if (sta->auth_alg != WLAN_AUTH_FT &&
3707 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3708 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3709 sta->auth_alg != WLAN_AUTH_FILS_PK &&
3710 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3711 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3712 } else
3713 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
3714
3715 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3716 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3717 wpa_printf(MSG_DEBUG,
3718 "%s: %s: canceled wired ap_handle_timer timeout for "
3719 MACSTR,
3720 hapd->conf->iface, __func__,
3721 MAC2STR(sta->addr));
3722 }
3723 } else if (!(hapd->iface->drv_flags &
3724 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
3725 wpa_printf(MSG_DEBUG,
3726 "%s: %s: reschedule ap_handle_timer timeout for "
3727 MACSTR " (%d seconds - ap_max_inactivity)",
3728 hapd->conf->iface, __func__, MAC2STR(sta->addr),
3729 hapd->conf->ap_max_inactivity);
3730 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3731 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3732 ap_handle_timer, hapd, sta);
3733 }
3734
3735#ifdef CONFIG_MACSEC
3736 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3737 hapd->conf->mka_psk_set)
3738 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3739 else
3740 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3741#endif /* CONFIG_MACSEC */
3742}
3743
3744
3745const char * hostapd_state_text(enum hostapd_iface_state s)
3746{
3747 switch (s) {
3748 case HAPD_IFACE_UNINITIALIZED:
3749 return "UNINITIALIZED";
3750 case HAPD_IFACE_DISABLED:
3751 return "DISABLED";
3752 case HAPD_IFACE_COUNTRY_UPDATE:
3753 return "COUNTRY_UPDATE";
3754 case HAPD_IFACE_ACS:
3755 return "ACS";
3756 case HAPD_IFACE_HT_SCAN:
3757 return "HT_SCAN";
3758 case HAPD_IFACE_DFS:
3759 return "DFS";
3760 case HAPD_IFACE_ENABLED:
3761 return "ENABLED";
3762 case HAPD_IFACE_NO_IR:
3763 return "NO_IR";
3764 }
3765
3766 return "UNKNOWN";
3767}
3768
3769
3770void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3771{
3772 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
3773 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3774 hostapd_state_text(iface->state), hostapd_state_text(s));
3775 iface->state = s;
3776}
3777
3778
3779int hostapd_csa_in_progress(struct hostapd_iface *iface)
3780{
3781 unsigned int i;
3782
3783 for (i = 0; i < iface->num_bss; i++)
3784 if (iface->bss[i]->csa_in_progress)
3785 return 1;
3786 return 0;
3787}
3788
3789
3790#ifdef NEED_AP_MLME
3791
3792static void free_beacon_data(struct beacon_data *beacon)
3793{
3794 os_free(beacon->head);
3795 beacon->head = NULL;
3796 os_free(beacon->tail);
3797 beacon->tail = NULL;
3798 os_free(beacon->probe_resp);
3799 beacon->probe_resp = NULL;
3800 os_free(beacon->beacon_ies);
3801 beacon->beacon_ies = NULL;
3802 os_free(beacon->proberesp_ies);
3803 beacon->proberesp_ies = NULL;
3804 os_free(beacon->assocresp_ies);
3805 beacon->assocresp_ies = NULL;
3806}
3807
3808
3809static int hostapd_build_beacon_data(struct hostapd_data *hapd,
3810 struct beacon_data *beacon)
3811{
3812 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3813 struct wpa_driver_ap_params params;
3814 int ret;
3815
3816 os_memset(beacon, 0, sizeof(*beacon));
3817 ret = ieee802_11_build_ap_params(hapd, &params);
3818 if (ret < 0)
3819 return ret;
3820
3821 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3822 &proberesp_extra,
3823 &assocresp_extra);
3824 if (ret)
3825 goto free_ap_params;
3826
3827 ret = -1;
3828 beacon->head = os_memdup(params.head, params.head_len);
3829 if (!beacon->head)
3830 goto free_ap_extra_ies;
3831
3832 beacon->head_len = params.head_len;
3833
3834 beacon->tail = os_memdup(params.tail, params.tail_len);
3835 if (!beacon->tail)
3836 goto free_beacon;
3837
3838 beacon->tail_len = params.tail_len;
3839
3840 if (params.proberesp != NULL) {
3841 beacon->probe_resp = os_memdup(params.proberesp,
3842 params.proberesp_len);
3843 if (!beacon->probe_resp)
3844 goto free_beacon;
3845
3846 beacon->probe_resp_len = params.proberesp_len;
3847 }
3848
3849 /* copy the extra ies */
3850 if (beacon_extra) {
3851 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3852 wpabuf_len(beacon_extra));
3853 if (!beacon->beacon_ies)
3854 goto free_beacon;
3855
3856 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3857 }
3858
3859 if (proberesp_extra) {
3860 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3861 wpabuf_len(proberesp_extra));
3862 if (!beacon->proberesp_ies)
3863 goto free_beacon;
3864
3865 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3866 }
3867
3868 if (assocresp_extra) {
3869 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3870 wpabuf_len(assocresp_extra));
3871 if (!beacon->assocresp_ies)
3872 goto free_beacon;
3873
3874 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3875 }
3876
3877 ret = 0;
3878free_beacon:
3879 /* if the function fails, the caller should not free beacon data */
3880 if (ret)
3881 free_beacon_data(beacon);
3882
3883free_ap_extra_ies:
3884 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3885 assocresp_extra);
3886free_ap_params:
3887 ieee802_11_free_ap_params(&params);
3888 return ret;
3889}
3890
3891
3892/*
3893 * TODO: This flow currently supports only changing channel and width within
3894 * the same hw_mode. Any other changes to MAC parameters or provided settings
3895 * are not supported.
3896 */
3897static int hostapd_change_config_freq(struct hostapd_data *hapd,
3898 struct hostapd_config *conf,
3899 struct hostapd_freq_params *params,
3900 struct hostapd_freq_params *old_params)
3901{
3902 int channel;
3903 u8 seg0 = 0, seg1 = 0;
3904 struct hostapd_hw_modes *mode;
3905
3906 if (!params->channel) {
3907 /* check if the new channel is supported by hw */
3908 params->channel = hostapd_hw_get_channel(hapd, params->freq);
3909 }
3910
3911 channel = params->channel;
3912 if (!channel)
3913 return -1;
3914
3915 hostapd_determine_mode(hapd->iface);
3916 mode = hapd->iface->current_mode;
3917
3918 /* if a pointer to old_params is provided we save previous state */
3919 if (old_params &&
3920 hostapd_set_freq_params(old_params, conf->hw_mode,
3921 hostapd_hw_get_freq(hapd, conf->channel),
3922 conf->channel, conf->enable_edmg,
3923 conf->edmg_channel, conf->ieee80211n,
3924 conf->ieee80211ac, conf->ieee80211ax,
3925 conf->ieee80211be, conf->secondary_channel,
3926 hostapd_get_oper_chwidth(conf),
3927 hostapd_get_oper_centr_freq_seg0_idx(conf),
3928 hostapd_get_oper_centr_freq_seg1_idx(conf),
3929 conf->vht_capab,
3930 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
3931 NULL,
3932 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
3933 NULL))
3934 return -1;
3935
3936 switch (params->bandwidth) {
3937 case 0:
3938 case 20:
3939 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3940 break;
3941 case 40:
3942 case 80:
3943 case 160:
3944 case 320:
3945 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3946 break;
3947 default:
3948 return -1;
3949 }
3950
3951 switch (params->bandwidth) {
3952 case 0:
3953 case 20:
3954 case 40:
3955 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
3956 break;
3957 case 80:
3958 if (params->center_freq2)
3959 hostapd_set_oper_chwidth(conf,
3960 CONF_OPER_CHWIDTH_80P80MHZ);
3961 else
3962 hostapd_set_oper_chwidth(conf,
3963 CONF_OPER_CHWIDTH_80MHZ);
3964 break;
3965 case 160:
3966 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
3967 break;
3968 case 320:
3969 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
3970 break;
3971 default:
3972 return -1;
3973 }
3974
3975 conf->channel = channel;
3976 conf->ieee80211n = params->ht_enabled;
3977 conf->ieee80211ac = params->vht_enabled;
3978 conf->secondary_channel = params->sec_channel_offset;
3979 ieee80211_freq_to_chan(params->center_freq1,
3980 &seg0);
3981 ieee80211_freq_to_chan(params->center_freq2,
3982 &seg1);
3983 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3984 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
3985
3986 /* TODO: maybe call here hostapd_config_check here? */
3987
3988 return 0;
3989}
3990
3991
3992static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
3993 struct csa_settings *settings)
3994{
3995 struct hostapd_iface *iface = hapd->iface;
3996 struct hostapd_freq_params old_freq;
3997 int ret;
3998#ifdef CONFIG_IEEE80211BE
3999 u16 old_punct_bitmap;
4000#endif /* CONFIG_IEEE80211BE */
4001 u8 chan, bandwidth;
4002
4003 os_memset(&old_freq, 0, sizeof(old_freq));
4004 if (!iface || !iface->freq || hapd->csa_in_progress)
4005 return -1;
4006
4007 switch (settings->freq_params.bandwidth) {
4008 case 80:
4009 if (settings->freq_params.center_freq2)
4010 bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
4011 else
4012 bandwidth = CONF_OPER_CHWIDTH_80MHZ;
4013 break;
4014 case 160:
4015 bandwidth = CONF_OPER_CHWIDTH_160MHZ;
4016 break;
4017 case 320:
4018 bandwidth = CONF_OPER_CHWIDTH_320MHZ;
4019 break;
4020 default:
4021 bandwidth = CONF_OPER_CHWIDTH_USE_HT;
4022 break;
4023 }
4024
4025 if (ieee80211_freq_to_channel_ext(
4026 settings->freq_params.freq,
4027 settings->freq_params.sec_channel_offset,
4028 bandwidth,
4029 &hapd->iface->cs_oper_class,
4030 &chan) == NUM_HOSTAPD_MODES) {
4031 wpa_printf(MSG_DEBUG,
4032 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d, eht_enabled=%d)",
4033 settings->freq_params.freq,
4034 settings->freq_params.sec_channel_offset,
4035 settings->freq_params.vht_enabled,
4036 settings->freq_params.he_enabled,
4037 settings->freq_params.eht_enabled);
4038 return -1;
4039 }
4040
4041 settings->freq_params.channel = chan;
4042
4043 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
4044 &settings->freq_params,
4045 &old_freq);
4046 if (ret)
4047 return ret;
4048
4049#ifdef CONFIG_IEEE80211BE
4050 old_punct_bitmap = iface->conf->punct_bitmap;
4051 iface->conf->punct_bitmap = settings->punct_bitmap;
4052#endif /* CONFIG_IEEE80211BE */
4053 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
4054
4055 /* change back the configuration */
4056#ifdef CONFIG_IEEE80211BE
4057 iface->conf->punct_bitmap = old_punct_bitmap;
4058#endif /* CONFIG_IEEE80211BE */
4059 hostapd_change_config_freq(iface->bss[0], iface->conf,
4060 &old_freq, NULL);
4061
4062 if (ret)
4063 return ret;
4064
4065 /* set channel switch parameters for csa ie */
4066 hapd->cs_freq_params = settings->freq_params;
4067 hapd->cs_count = settings->cs_count;
4068 hapd->cs_block_tx = settings->block_tx;
4069
4070 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
4071 if (ret) {
4072 free_beacon_data(&settings->beacon_after);
4073 return ret;
4074 }
4075
4076 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
4077 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
4078 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
4079 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
4080
4081 return 0;
4082}
4083
4084
4085void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
4086{
4087 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
4088 hapd->cs_count = 0;
4089 hapd->cs_block_tx = 0;
4090 hapd->cs_c_off_beacon = 0;
4091 hapd->cs_c_off_proberesp = 0;
4092 hapd->csa_in_progress = 0;
4093 hapd->cs_c_off_ecsa_beacon = 0;
4094 hapd->cs_c_off_ecsa_proberesp = 0;
4095}
4096
4097
4098void hostapd_chan_switch_config(struct hostapd_data *hapd,
4099 struct hostapd_freq_params *freq_params)
4100{
4101 if (freq_params->eht_enabled)
4102 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
4103 else
4104 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
4105
4106 if (freq_params->he_enabled)
4107 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
4108 else
4109 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
4110
4111 if (freq_params->vht_enabled)
4112 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
4113 else
4114 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
4115
4116 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
4117 HOSTAPD_LEVEL_INFO,
4118 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
4119 hapd->iconf->ch_switch_eht_config,
4120 hapd->iconf->ch_switch_he_config,
4121 hapd->iconf->ch_switch_vht_config);
4122}
4123
4124
4125int hostapd_switch_channel(struct hostapd_data *hapd,
4126 struct csa_settings *settings)
4127{
4128 int ret;
4129
4130 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
4131 wpa_printf(MSG_INFO, "CSA is not supported");
4132 return -1;
4133 }
4134
4135 ret = hostapd_fill_csa_settings(hapd, settings);
4136 if (ret)
4137 return ret;
4138
4139 ret = hostapd_drv_switch_channel(hapd, settings);
4140 free_beacon_data(&settings->beacon_csa);
4141 free_beacon_data(&settings->beacon_after);
4142
4143 if (ret) {
4144 /* if we failed, clean cs parameters */
4145 hostapd_cleanup_cs_params(hapd);
4146 return ret;
4147 }
4148
4149 hapd->csa_in_progress = 1;
4150 return 0;
4151}
4152
4153
4154void
4155hostapd_switch_channel_fallback(struct hostapd_iface *iface,
4156 const struct hostapd_freq_params *freq_params)
4157{
4158 int seg0_idx = 0, seg1_idx = 0;
4159 enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
4160
4161 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
4162
4163 if (freq_params->center_freq1)
4164 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
4165 if (freq_params->center_freq2)
4166 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
4167
4168 switch (freq_params->bandwidth) {
4169 case 0:
4170 case 20:
4171 case 40:
4172 bw = CONF_OPER_CHWIDTH_USE_HT;
4173 break;
4174 case 80:
4175 if (freq_params->center_freq2)
4176 bw = CONF_OPER_CHWIDTH_80P80MHZ;
4177 else
4178 bw = CONF_OPER_CHWIDTH_80MHZ;
4179 break;
4180 case 160:
4181 bw = CONF_OPER_CHWIDTH_160MHZ;
4182 break;
4183 case 320:
4184 bw = CONF_OPER_CHWIDTH_320MHZ;
4185 break;
4186 default:
4187 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
4188 freq_params->bandwidth);
4189 break;
4190 }
4191
4192 iface->freq = freq_params->freq;
4193 iface->conf->channel = freq_params->channel;
4194 iface->conf->secondary_channel = freq_params->sec_channel_offset;
4195 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
4196 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
4197 hostapd_set_oper_chwidth(iface->conf, bw);
4198 iface->conf->ieee80211n = freq_params->ht_enabled;
4199 iface->conf->ieee80211ac = freq_params->vht_enabled;
4200 iface->conf->ieee80211ax = freq_params->he_enabled;
4201 iface->conf->ieee80211be = freq_params->eht_enabled;
4202
4203 /*
4204 * cs_params must not be cleared earlier because the freq_params
4205 * argument may actually point to one of these.
4206 * These params will be cleared during interface disable below.
4207 */
4208 hostapd_disable_iface(iface);
4209 hostapd_enable_iface(iface);
4210}
4211
4212
4213#ifdef CONFIG_IEEE80211AX
4214
4215void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
4216{
4217 hapd->cca_count = 0;
4218 hapd->cca_color = 0;
4219 hapd->cca_c_off_beacon = 0;
4220 hapd->cca_c_off_proberesp = 0;
4221 hapd->cca_in_progress = false;
4222}
4223
4224
4225static int hostapd_fill_cca_settings(struct hostapd_data *hapd,
4226 struct cca_settings *settings)
4227{
4228 struct hostapd_iface *iface = hapd->iface;
4229 u8 old_color;
4230 int ret;
4231
4232 if (!iface || iface->conf->he_op.he_bss_color_disabled)
4233 return -1;
4234
4235 old_color = iface->conf->he_op.he_bss_color;
4236 iface->conf->he_op.he_bss_color = hapd->cca_color;
4237 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
4238 if (ret)
4239 return ret;
4240
4241 iface->conf->he_op.he_bss_color = old_color;
4242
4243 settings->cca_count = hapd->cca_count;
4244 settings->cca_color = hapd->cca_color,
4245 hapd->cca_in_progress = true;
4246
4247 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
4248 if (ret) {
4249 free_beacon_data(&settings->beacon_after);
4250 return ret;
4251 }
4252
4253 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
4254 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
4255
4256 return 0;
4257}
4258
4259
4260static void hostapd_switch_color_timeout_handler(void *eloop_data,
4261 void *user_ctx)
4262{
4263 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
4264 os_time_t delta_t;
4265 unsigned int b;
4266 int i, r;
4267
4268 /* CCA can be triggered once the handler constantly receives
4269 * color collision events to for at least
4270 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
4271 delta_t = hapd->last_color_collision.sec -
4272 hapd->first_color_collision.sec;
4273 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
4274 return;
4275
4276 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
4277 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
4278 if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
4279 break;
4280
4281 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
4282 }
4283
4284 if (i == HE_OPERATION_BSS_COLOR_MAX) {
4285 /* There are no free colors so turn BSS coloring off */
4286 wpa_printf(MSG_INFO,
4287 "No free colors left, turning off BSS coloring");
4288 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
4289 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
4290 for (b = 0; b < hapd->iface->num_bss; b++)
4291 ieee802_11_set_beacon(hapd->iface->bss[b]);
4292 return;
4293 }
4294
4295 for (b = 0; b < hapd->iface->num_bss; b++) {
4296 struct hostapd_data *bss = hapd->iface->bss[b];
4297 struct cca_settings settings;
4298 int ret;
4299
4300 hostapd_cleanup_cca_params(bss);
4301 bss->cca_color = r;
4302 bss->cca_count = 10;
4303
4304 if (hostapd_fill_cca_settings(bss, &settings)) {
4305 hostapd_cleanup_cca_params(bss);
4306 continue;
4307 }
4308
4309 ret = hostapd_drv_switch_color(bss, &settings);
4310 if (ret)
4311 hostapd_cleanup_cca_params(bss);
4312
4313 free_beacon_data(&settings.beacon_cca);
4314 free_beacon_data(&settings.beacon_after);
4315 }
4316}
4317
4318
4319void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
4320{
4321 struct os_reltime now;
4322
4323 if (hapd->cca_in_progress)
4324 return;
4325
4326 if (os_get_reltime(&now))
4327 return;
4328
4329 hapd->color_collision_bitmap = bitmap;
4330 hapd->last_color_collision = now;
4331
4332 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
4333 hapd, NULL))
4334 return;
4335
4336 hapd->first_color_collision = now;
4337 /* 10 s window as margin for persistent color collision reporting */
4338 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
4339 hostapd_switch_color_timeout_handler,
4340 hapd, NULL);
4341}
4342
4343#endif /* CONFIG_IEEE80211AX */
4344
4345#endif /* NEED_AP_MLME */
4346
4347
4348struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
4349 const char *ifname)
4350{
4351 size_t i, j;
4352
4353 for (i = 0; i < interfaces->count; i++) {
4354 struct hostapd_iface *iface = interfaces->iface[i];
4355
4356 for (j = 0; j < iface->num_bss; j++) {
4357 struct hostapd_data *hapd = iface->bss[j];
4358
4359 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4360 return hapd;
4361 }
4362 }
4363
4364 return NULL;
4365}
4366
4367
4368#ifdef CONFIG_BSS_TRANS_IMPL
4369// Periodic check of STA info and trigger BSS transition if necessary
4370static void hostapd_sta_bss_transition_check(struct hostapd_data *hapd)
4371{
4372 struct sta_info *sta;
4373 char shellBuf[100];
4374 char *s;
4375
4376 for (sta = hapd->sta_list; sta; sta = sta->next)
4377 {
4378 if (sta)
4379 {
4380 s = shellBuf;
4381 s += sprintf(s, "./etc/log/bss_tm_trigger.sh ");
4382 s += sprintf(s, "%s " MACSTR, hapd->iface->phy, MAC2STR(sta->addr));
4383 wpa_printf(MSG_DEBUG,
4384 "hostapd_sta_bss_transition_check, shell command: %s", shellBuf);
4385 system(shellBuf);
4386 memset(shellBuf, 0, sizeof(shellBuf));
4387 }
4388 }
4389}
4390#endif /* CONFIG_BSS_TRANS_IMPL */
4391
4392
4393void hostapd_periodic_iface(struct hostapd_iface *iface)
4394{
4395 size_t i;
4396
4397 ap_list_timer(iface);
4398
4399 for (i = 0; i < iface->num_bss; i++) {
4400 struct hostapd_data *hapd = iface->bss[i];
4401
4402 if (!hapd->started)
4403 continue;
4404
4405#ifndef CONFIG_NO_RADIUS
4406 hostapd_acl_expire(hapd);
4407#endif /* CONFIG_NO_RADIUS */
4408
4409#ifdef CONFIG_BSS_TRANS_IMPL
4410 // Add periodic BSS transition check for connected STAs by Liangyu Chu
4411 if (hapd->conf->bss_transition)
4412 hostapd_sta_bss_transition_check(hapd);
4413#endif /* CONFIG_BSS_TRANS_IMPL */
4414 }
4415}
4416
4417
4418#ifdef CONFIG_OCV
4419void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
4420{
4421 struct hostapd_data *hapd = eloop_ctx;
4422 struct sta_info *sta;
4423
4424 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
4425
4426 for (sta = hapd->sta_list; sta; sta = sta->next) {
4427 if (!sta->post_csa_sa_query)
4428 continue;
4429
4430 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
4431 " did not start SA Query after CSA - disconnect",
4432 MAC2STR(sta->addr));
4433 ap_sta_disconnect(hapd, sta, sta->addr,
4434 WLAN_REASON_PREV_AUTH_NOT_VALID);
4435 }
4436}
4437#endif /* CONFIG_OCV */
4438
4439
4440#ifdef CONFIG_IEEE80211BE
4441struct hostapd_data * hostapd_mld_get_link_bss(struct hostapd_data *hapd,
4442 u8 link_id)
4443{
4444 unsigned int i;
4445
4446 for (i = 0; i < hapd->iface->interfaces->count; i++) {
4447 struct hostapd_iface *h = hapd->iface->interfaces->iface[i];
4448 struct hostapd_data *h_hapd = h->bss[0];
4449 struct hostapd_bss_config *hconf = h_hapd->conf;
4450
4451 if (!hconf->mld_ap || hconf->mld_id != hapd->conf->mld_id)
4452 continue;
4453
4454 if (h_hapd->mld_link_id == link_id)
4455 return h_hapd;
4456 }
4457
4458 return NULL;
4459}
4460#endif /* CONFIG_IEEE80211BE */