blob: 50b9f04f1f00bc998d2229f932beafdd1eb04bda [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / main()
Hai Shaloma20dcd72022-02-04 13:43:00 -08003 * Copyright (c) 2002-2022, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10#ifndef CONFIG_NATIVE_WINDOWS
11#include <syslog.h>
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -070012#include <grp.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013#endif /* CONFIG_NATIVE_WINDOWS */
14
15#include "utils/common.h"
16#include "utils/eloop.h"
Dmitry Shmidt96be6222014-02-13 10:16:51 -080017#include "utils/uuid.h"
Sunil Ravia04bd252022-05-02 22:54:18 -070018#include "crypto/crypto.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include "crypto/random.h"
20#include "crypto/tls.h"
21#include "common/version.h"
Hai Shalom021b0b52019-04-10 11:17:58 -070022#include "common/dpp.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "drivers/driver.h"
24#include "eap_server/eap.h"
25#include "eap_server/tncs.h"
26#include "ap/hostapd.h"
27#include "ap/ap_config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070028#include "ap/ap_drv_ops.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070029#include "ap/dpp_hostapd.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080030#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031#include "config_file.h"
32#include "eap_register.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "ctrl_iface.h"
Gabriel Biren72cf9a52021-06-25 23:29:26 +000034#ifdef CONFIG_CTRL_IFACE_AIDL
35#include "aidl.h"
36#endif /* CONFIG_CTRL_IFACE_AIDL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080038struct hapd_global {
39 void **drv_priv;
40 size_t drv_count;
41};
42
43static struct hapd_global global;
44
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070046#ifndef CONFIG_NO_HOSTAPD_LOGGER
47static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
48 int level, const char *txt, size_t len)
49{
50 struct hostapd_data *hapd = ctx;
51 char *format, *module_str;
52 int maxlen;
53 int conf_syslog_level, conf_stdout_level;
54 unsigned int conf_syslog, conf_stdout;
55
56 maxlen = len + 100;
57 format = os_malloc(maxlen);
58 if (!format)
59 return;
60
61 if (hapd && hapd->conf) {
62 conf_syslog_level = hapd->conf->logger_syslog_level;
63 conf_stdout_level = hapd->conf->logger_stdout_level;
64 conf_syslog = hapd->conf->logger_syslog;
65 conf_stdout = hapd->conf->logger_stdout;
66 } else {
67 conf_syslog_level = conf_stdout_level = 0;
68 conf_syslog = conf_stdout = (unsigned int) -1;
69 }
70
71 switch (module) {
72 case HOSTAPD_MODULE_IEEE80211:
73 module_str = "IEEE 802.11";
74 break;
75 case HOSTAPD_MODULE_IEEE8021X:
76 module_str = "IEEE 802.1X";
77 break;
78 case HOSTAPD_MODULE_RADIUS:
79 module_str = "RADIUS";
80 break;
81 case HOSTAPD_MODULE_WPA:
82 module_str = "WPA";
83 break;
84 case HOSTAPD_MODULE_DRIVER:
85 module_str = "DRIVER";
86 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087 case HOSTAPD_MODULE_MLME:
88 module_str = "MLME";
89 break;
90 default:
91 module_str = NULL;
92 break;
93 }
94
95 if (hapd && hapd->conf && addr)
96 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
97 hapd->conf->iface, MAC2STR(addr),
Dmitry Shmidt96be6222014-02-13 10:16:51 -080098 module_str ? " " : "", module_str ? module_str : "",
99 txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100 else if (hapd && hapd->conf)
101 os_snprintf(format, maxlen, "%s:%s%s %s",
102 hapd->conf->iface, module_str ? " " : "",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700103 module_str ? module_str : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 else if (addr)
105 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
106 MAC2STR(addr), module_str ? " " : "",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700107 module_str ? module_str : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108 else
109 os_snprintf(format, maxlen, "%s%s%s",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700110 module_str ? module_str : "",
111 module_str ? ": " : "", txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112
Paul Stewart092955c2017-02-06 09:13:09 -0800113#ifdef CONFIG_DEBUG_SYSLOG
114 if (wpa_debug_syslog)
115 conf_stdout = 0;
116#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117 if ((conf_stdout & module) && level >= conf_stdout_level) {
118 wpa_debug_print_timestamp();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800119 wpa_printf(MSG_INFO, "%s", format);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700120 }
121
122#ifndef CONFIG_NATIVE_WINDOWS
123 if ((conf_syslog & module) && level >= conf_syslog_level) {
124 int priority;
125 switch (level) {
126 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
127 case HOSTAPD_LEVEL_DEBUG:
128 priority = LOG_DEBUG;
129 break;
130 case HOSTAPD_LEVEL_INFO:
131 priority = LOG_INFO;
132 break;
133 case HOSTAPD_LEVEL_NOTICE:
134 priority = LOG_NOTICE;
135 break;
136 case HOSTAPD_LEVEL_WARNING:
137 priority = LOG_WARNING;
138 break;
139 default:
140 priority = LOG_INFO;
141 break;
142 }
143 syslog(priority, "%s", format);
144 }
145#endif /* CONFIG_NATIVE_WINDOWS */
146
147 os_free(format);
148}
149#endif /* CONFIG_NO_HOSTAPD_LOGGER */
150
151
152/**
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800153 * hostapd_driver_init - Preparate driver interface
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155static int hostapd_driver_init(struct hostapd_iface *iface)
156{
157 struct wpa_init_params params;
158 size_t i;
159 struct hostapd_data *hapd = iface->bss[0];
160 struct hostapd_bss_config *conf = hapd->conf;
161 u8 *b = conf->bssid;
162 struct wpa_driver_capa capa;
Sunil Ravi7f769292024-07-23 22:21:32 +0000163#ifdef CONFIG_IEEE80211BE
164 struct hostapd_data *h_hapd = NULL;
165#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166
167 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
168 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
169 return -1;
170 }
171
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000172#ifdef CONFIG_IEEE80211BE
Sunil Ravi7f769292024-07-23 22:21:32 +0000173 if (conf->mld_ap)
174 h_hapd = hostapd_mld_get_first_bss(hapd);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000175
Sunil Ravi7f769292024-07-23 22:21:32 +0000176 if (h_hapd) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000177 hapd->drv_priv = h_hapd->drv_priv;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000178 hapd->interface_added = h_hapd->interface_added;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000179
180 /*
181 * All interfaces participating in the AP MLD would have
182 * the same MLD address, which is the interface hardware
183 * address, while the interface address would be
184 * derived from the original interface address if BSSID
185 * is not configured, and otherwise it would be the
186 * configured BSSID.
187 */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000188 if (is_zero_ether_addr(b)) {
Sunil Ravi99c035e2024-07-12 01:42:03 +0000189 os_memcpy(hapd->own_addr, h_hapd->mld->mld_addr,
190 ETH_ALEN);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000191 random_mac_addr_keep_oui(hapd->own_addr);
192 } else {
193 os_memcpy(hapd->own_addr, b, ETH_ALEN);
194 }
195
Sunil Ravi7f769292024-07-23 22:21:32 +0000196 wpa_printf(MSG_DEBUG,
197 "Setup of non first link (%d) BSS of MLD %s",
198 hapd->mld_link_id, hapd->conf->iface);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000199
200 goto setup_mld;
201 }
202#endif /* CONFIG_IEEE80211BE */
203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204 /* Initialize the driver interface */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000205 if (is_zero_ether_addr(b))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 b = NULL;
207
208 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800209 for (i = 0; wpa_drivers[i]; i++) {
210 if (wpa_drivers[i] != hapd->driver)
211 continue;
212
213 if (global.drv_priv[i] == NULL &&
214 wpa_drivers[i]->global_init) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700215 global.drv_priv[i] =
216 wpa_drivers[i]->global_init(iface->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800217 if (global.drv_priv[i] == NULL) {
218 wpa_printf(MSG_ERROR, "Failed to initialize "
219 "driver '%s'",
220 wpa_drivers[i]->name);
221 return -1;
222 }
223 }
224
225 params.global_priv = global.drv_priv[i];
226 break;
227 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 params.bssid = b;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000229#ifdef CONFIG_IEEE80211BE
230 /*
231 * Use the configured MLD MAC address as the interface hardware address
232 * if this AP is a part of an AP MLD.
233 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000234 if (hapd->conf->mld_ap) {
235 if (!is_zero_ether_addr(hapd->conf->mld_addr))
236 params.bssid = hapd->conf->mld_addr;
237 else
238 params.bssid = NULL;
239 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000240#endif /* CONFIG_IEEE80211BE */
241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 params.ifname = hapd->conf->iface;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800243 params.driver_params = hapd->iconf->driver_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
245
246 params.num_bridge = hapd->iface->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700247 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 if (params.bridge == NULL)
249 return -1;
250 for (i = 0; i < hapd->iface->num_bss; i++) {
251 struct hostapd_data *bss = hapd->iface->bss[i];
252 if (bss->conf->bridge[0])
253 params.bridge[i] = bss->conf->bridge;
254 }
255
256 params.own_addr = hapd->own_addr;
257
258 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
259 os_free(params.bridge);
260 if (hapd->drv_priv == NULL) {
261 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
262 hapd->driver->name);
263 hapd->driver = NULL;
264 return -1;
265 }
266
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000267#ifdef CONFIG_IEEE80211BE
268 /*
269 * This is the first interface added to the AP MLD, so have the
270 * interface hardware address be the MLD address, while the link address
271 * would be derived from the original interface address if BSSID is not
272 * configured, and otherwise it would be the configured BSSID.
273 */
274 if (hapd->conf->mld_ap) {
Sunil Ravi99c035e2024-07-12 01:42:03 +0000275 os_memcpy(hapd->mld->mld_addr, hapd->own_addr, ETH_ALEN);
276
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000277 if (!b)
278 random_mac_addr_keep_oui(hapd->own_addr);
279 else
280 os_memcpy(hapd->own_addr, b, ETH_ALEN);
Sunil Ravi99c035e2024-07-12 01:42:03 +0000281
Sunil Ravi7f769292024-07-23 22:21:32 +0000282 wpa_printf(MSG_DEBUG, "Setup of first link (%d) BSS of MLD %s",
283 hapd->mld_link_id, hapd->conf->iface);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000284 }
285
286setup_mld:
287#endif /* CONFIG_IEEE80211BE */
288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (hapd->driver->get_capa &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800290 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700291 struct wowlan_triggers *triggs;
292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 iface->drv_flags = capa.flags;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700294 iface->drv_flags2 = capa.flags2;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000295 iface->drv_rrm_flags = capa.rrm_flags;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800296 iface->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700297 /*
298 * Use default extended capa values from per-radio information
299 */
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700300 iface->extended_capa = capa.extended_capa;
301 iface->extended_capa_mask = capa.extended_capa_mask;
302 iface->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700303 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700304
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700305 /*
306 * Override extended capa with per-interface type (AP), if
307 * available from the driver.
308 */
309 hostapd_get_ext_capa(iface);
310
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000311 hostapd_get_mld_capa(iface);
312
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700313 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
314 if (triggs && hapd->driver->set_wowlan) {
315 if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
316 wpa_printf(MSG_ERROR, "set_wowlan failed");
317 }
318 os_free(triggs);
Sunil Ravi77d572f2023-01-17 23:58:31 +0000319
320 iface->mbssid_max_interfaces = capa.mbssid_max_interfaces;
321 iface->ema_max_periodicity = capa.ema_max_periodicity;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000324#ifdef CONFIG_IEEE80211BE
325 if (hapd->conf->mld_ap) {
326 if (!(iface->drv_flags2 & WPA_DRIVER_FLAGS2_MLO)) {
327 wpa_printf(MSG_INFO,
328 "MLD: Not supported by the driver");
329 return -1;
330 }
331
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000332 /* Initialize the BSS parameter change to 1 */
333 hapd->eht_mld_bss_param_change = 1;
334
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000335 wpa_printf(MSG_DEBUG,
336 "MLD: Set link_id=%u, mld_addr=" MACSTR
337 ", own_addr=" MACSTR,
Sunil Ravi99c035e2024-07-12 01:42:03 +0000338 hapd->mld_link_id, MAC2STR(hapd->mld->mld_addr),
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000339 MAC2STR(hapd->own_addr));
340
Sunil Ravic0f5d412024-09-11 22:12:49 +0000341 if (hostapd_drv_link_add(hapd, hapd->mld_link_id,
342 hapd->own_addr)) {
343 wpa_printf(MSG_ERROR,
344 "MLD: Failed to add link %d in MLD %s",
345 hapd->mld_link_id, hapd->conf->iface);
346 return -1;
347 }
348 hostapd_mld_add_link(hapd);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000349 }
350#endif /* CONFIG_IEEE80211BE */
351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 return 0;
353}
354
355
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800356/**
357 * hostapd_interface_init - Read configuration file and init BSS data
358 *
359 * This function is used to parse configuration file for a full interface (one
360 * or more BSSes sharing the same radio) and allocate memory for the BSS
Hai Shalom74f70d42019-02-11 14:42:39 -0800361 * interfaces. No actual driver operations are started.
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800362 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363static struct hostapd_iface *
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700364hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365 const char *config_fname, int debug)
366{
367 struct hostapd_iface *iface;
368 int k;
369
Hai Shalomfdcde762020-04-02 11:19:20 -0700370 wpa_printf(MSG_DEBUG, "Configuration file: %s", config_fname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800371 iface = hostapd_init(interfaces, config_fname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 if (!iface)
373 return NULL;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700374
375 if (if_name) {
376 os_strlcpy(iface->conf->bss[0]->iface, if_name,
377 sizeof(iface->conf->bss[0]->iface));
378 }
379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 iface->interfaces = interfaces;
381
382 for (k = 0; k < debug; k++) {
383 if (iface->bss[0]->conf->logger_stdout_level > 0)
384 iface->bss[0]->conf->logger_stdout_level--;
385 }
386
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800387 if (iface->conf->bss[0]->iface[0] == '\0' &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700388 !hostapd_drv_none(iface->bss[0])) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700389 wpa_printf(MSG_ERROR,
390 "Interface name not specified in %s, nor by '-i' parameter",
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700391 config_fname);
392 hostapd_interface_deinit_free(iface);
393 return NULL;
394 }
395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396 return iface;
397}
398
399
400/**
401 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
402 */
403static void handle_term(int sig, void *signal_ctx)
404{
405 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
406 eloop_terminate();
407}
408
409
410#ifndef CONFIG_NATIVE_WINDOWS
411
412static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
413{
414 if (hostapd_reload_config(iface) < 0) {
415 wpa_printf(MSG_WARNING, "Failed to read new configuration "
416 "file - continuing with old.");
417 }
418 return 0;
419}
420
421
422/**
423 * handle_reload - SIGHUP handler to reload configuration
424 */
425static void handle_reload(int sig, void *signal_ctx)
426{
427 struct hapd_interfaces *interfaces = signal_ctx;
428 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
429 sig);
430 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
431}
432
433
434static void handle_dump_state(int sig, void *signal_ctx)
435{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800436 /* Not used anymore - ignore signal */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437}
438#endif /* CONFIG_NATIVE_WINDOWS */
439
440
Jouni Malinen75ecf522011-06-27 15:19:46 -0700441static int hostapd_global_init(struct hapd_interfaces *interfaces,
442 const char *entropy_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800444 int i;
445
446 os_memset(&global, 0, sizeof(global));
447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448 hostapd_logger_register_cb(hostapd_logger_cb);
449
450 if (eap_server_register_methods()) {
451 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
452 return -1;
453 }
454
455 if (eloop_init()) {
456 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
457 return -1;
458 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700459 interfaces->eloop_initialized = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460
Jouni Malinen75ecf522011-06-27 15:19:46 -0700461 random_init(entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462
463#ifndef CONFIG_NATIVE_WINDOWS
464 eloop_register_signal(SIGHUP, handle_reload, interfaces);
465 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
466#endif /* CONFIG_NATIVE_WINDOWS */
467 eloop_register_signal_terminate(handle_term, interfaces);
468
469#ifndef CONFIG_NATIVE_WINDOWS
470 openlog("hostapd", 0, LOG_DAEMON);
471#endif /* CONFIG_NATIVE_WINDOWS */
472
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800473 for (i = 0; wpa_drivers[i]; i++)
474 global.drv_count++;
475 if (global.drv_count == 0) {
476 wpa_printf(MSG_ERROR, "No drivers enabled");
477 return -1;
478 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700479 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800480 if (global.drv_priv == NULL)
481 return -1;
482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 return 0;
484}
485
486
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700487static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800489 int i;
490
491 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
492 if (!global.drv_priv[i])
493 continue;
494 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
495 }
496 os_free(global.drv_priv);
497 global.drv_priv = NULL;
498
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700499#ifdef EAP_SERVER_TNC
500 tncs_global_deinit();
501#endif /* EAP_SERVER_TNC */
502
503 random_deinit();
504
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700505 if (eloop_initialized)
506 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507
508#ifndef CONFIG_NATIVE_WINDOWS
509 closelog();
510#endif /* CONFIG_NATIVE_WINDOWS */
511
512 eap_server_unregister_methods();
513
514 os_daemonize_terminate(pid_file);
515}
516
517
518static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
519 const char *pid_file)
520{
521#ifdef EAP_SERVER_TNC
522 int tnc = 0;
523 size_t i, k;
524
525 for (i = 0; !tnc && i < ifaces->count; i++) {
526 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
527 if (ifaces->iface[i]->bss[0]->conf->tnc) {
528 tnc++;
529 break;
530 }
531 }
532 }
533
534 if (tnc && tncs_global_init() < 0) {
535 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
536 return -1;
537 }
538#endif /* EAP_SERVER_TNC */
539
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800540 if (daemonize) {
541 if (os_daemonize(pid_file)) {
542 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
543 return -1;
544 }
545 if (eloop_sock_requeue()) {
546 wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
547 strerror(errno));
548 return -1;
549 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550 }
551
Veerendranath Jakkam9d0d5212023-02-21 19:55:58 +0530552#ifdef CONFIG_CTRL_IFACE_AIDL
553 if (hostapd_aidl_init(ifaces)) {
554 wpa_printf(MSG_ERROR, "Failed to initialize AIDL interface");
555 return -1;
556 }
557#endif /* CONFIG_CTRL_IFACE_AIDL */
558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559 eloop_run();
560
561 return 0;
562}
563
564
565static void show_version(void)
566{
567 fprintf(stderr,
Hai Shalomfdcde762020-04-02 11:19:20 -0700568 "hostapd v%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569 "User space daemon for IEEE 802.11 AP management,\n"
570 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Sunil Ravi7f769292024-07-23 22:21:32 +0000571 "Copyright (c) 2002-2024, Jouni Malinen <j@w1.fi> "
Hai Shalomfdcde762020-04-02 11:19:20 -0700572 "and contributors\n",
573 VERSION_STR);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574}
575
576
577static void usage(void)
578{
579 show_version();
580 fprintf(stderr,
581 "\n"
Sunil8cd6f4d2022-06-28 18:40:46 +0000582 "usage: hostapd [-hdBKtvq] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700583 "\\\n"
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700584 " [-g <global ctrl_iface>] [-G <group>]\\\n"
585 " [-i <comma-separated list of interface names>]\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700586 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587 "\n"
588 "options:\n"
589 " -h show this usage\n"
590 " -d show more debug messages (-dd for even more)\n"
591 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700592 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700593 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700594 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 " -P PID file\n"
596 " -K include key data in debug messages\n"
597#ifdef CONFIG_DEBUG_FILE
598 " -f log output to debug file instead of stdout\n"
599#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800600#ifdef CONFIG_DEBUG_LINUX_TRACING
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800601 " -T record to Linux tracing in addition to logging\n"
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800602 " (records all messages regardless of debug verbosity)\n"
603#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700604 " -i list of interface names to use\n"
Paul Stewart092955c2017-02-06 09:13:09 -0800605#ifdef CONFIG_DEBUG_SYSLOG
606 " -s log output to syslog instead of stdout\n"
607#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800608 " -S start all the interfaces synchronously\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609 " -t include timestamps in some debug messages\n"
Sunil Ravi89eba102022-09-13 21:04:37 -0700610 " -v show hostapd version\n"
611 " -q show less debug messages (-qq for even less)\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612
613 exit(1);
614}
615
616
617static const char * hostapd_msg_ifname_cb(void *ctx)
618{
619 struct hostapd_data *hapd = ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800620 if (hapd && hapd->conf)
621 return hapd->conf->iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622 return NULL;
623}
624
625
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700626static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
627 const char *path)
628{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800629#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700630 char *pos;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800631#endif /* !CONFIG_CTRL_IFACE_UDP */
632
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700633 os_free(interfaces->global_iface_path);
634 interfaces->global_iface_path = os_strdup(path);
635 if (interfaces->global_iface_path == NULL)
636 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800637
638#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700639 pos = os_strrchr(interfaces->global_iface_path, '/');
640 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700641 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
642 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700643 os_free(interfaces->global_iface_path);
644 interfaces->global_iface_path = NULL;
645 return -1;
646 }
647
648 *pos = '\0';
649 interfaces->global_iface_name = pos + 1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800650#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700651
652 return 0;
653}
654
655
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700656static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
657 const char *group)
658{
659#ifndef CONFIG_NATIVE_WINDOWS
660 struct group *grp;
661 grp = getgrnam(group);
662 if (grp == NULL) {
663 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
664 return -1;
665 }
666 interfaces->ctrl_iface_group = grp->gr_gid;
667#endif /* CONFIG_NATIVE_WINDOWS */
668 return 0;
669}
670
671
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700672static int hostapd_get_interface_names(char ***if_names,
673 size_t *if_names_size,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800674 char *arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700675{
676 char *if_name, *tmp, **nnames;
677 size_t i;
678
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800679 if (!arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700680 return -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800681 if_name = strtok_r(arg, ",", &tmp);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700682
683 while (if_name) {
684 nnames = os_realloc_array(*if_names, 1 + *if_names_size,
685 sizeof(char *));
686 if (!nnames)
687 goto fail;
688 *if_names = nnames;
689
690 (*if_names)[*if_names_size] = os_strdup(if_name);
691 if (!(*if_names)[*if_names_size])
692 goto fail;
693 (*if_names_size)++;
694 if_name = strtok_r(NULL, ",", &tmp);
695 }
696
697 return 0;
698
699fail:
700 for (i = 0; i < *if_names_size; i++)
701 os_free((*if_names)[i]);
702 os_free(*if_names);
703 *if_names = NULL;
704 *if_names_size = 0;
705 return -1;
706}
707
708
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800709#ifdef CONFIG_WPS
710static int gen_uuid(const char *txt_addr)
711{
712 u8 addr[ETH_ALEN];
713 u8 uuid[UUID_LEN];
714 char buf[100];
715
716 if (hwaddr_aton(txt_addr, addr) < 0)
717 return -1;
718
719 uuid_gen_mac_addr(addr, uuid);
720 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
721 return -1;
722
723 printf("%s\n", buf);
724
725 return 0;
726}
727#endif /* CONFIG_WPS */
728
729
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800730#ifndef HOSTAPD_CLEANUP_INTERVAL
731#define HOSTAPD_CLEANUP_INTERVAL 10
732#endif /* HOSTAPD_CLEANUP_INTERVAL */
733
734static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
735{
736 hostapd_periodic_iface(iface);
737 return 0;
738}
739
740
741/* Periodic cleanup tasks */
742static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
743{
744 struct hapd_interfaces *interfaces = eloop_ctx;
745
746 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
747 hostapd_periodic, interfaces, NULL);
748 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
749}
750
751
Sunil Ravi99c035e2024-07-12 01:42:03 +0000752static void hostapd_global_cleanup_mld(struct hapd_interfaces *interfaces)
753{
754#ifdef CONFIG_IEEE80211BE
755 size_t i;
756
757 if (!interfaces || !interfaces->mld)
758 return;
759
760 for (i = 0; i < interfaces->mld_count; i++) {
761 if (!interfaces->mld[i])
762 continue;
763
Sunil Ravic0f5d412024-09-11 22:12:49 +0000764 interfaces->mld_ctrl_iface_deinit(interfaces->mld[i]);
Sunil Ravi99c035e2024-07-12 01:42:03 +0000765 os_free(interfaces->mld[i]);
766 interfaces->mld[i] = NULL;
767 }
768
769 os_free(interfaces->mld);
770 interfaces->mld = NULL;
771 interfaces->mld_count = 0;
772#endif /* CONFIG_IEEE80211BE */
773}
774
775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776int main(int argc, char *argv[])
777{
778 struct hapd_interfaces interfaces;
779 int ret = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800780 size_t i, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700781 int c, debug = 0, daemonize = 0;
782 char *pid_file = NULL;
783 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700784 const char *entropy_file = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800785 char **bss_config = NULL, **tmp_bss;
786 size_t num_bss_configs = 0;
787#ifdef CONFIG_DEBUG_LINUX_TRACING
788 int enable_trace_dbg = 0;
789#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800790 int start_ifaces_in_sync = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700791 char **if_names = NULL;
792 size_t if_names_size = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700793#ifdef CONFIG_DPP
794 struct dpp_global_config dpp_conf;
795#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796
797 if (os_program_init())
798 return -1;
799
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700800 os_memset(&interfaces, 0, sizeof(interfaces));
801 interfaces.reload_config = hostapd_reload_config;
802 interfaces.config_read_cb = hostapd_config_read;
803 interfaces.for_each_interface = hostapd_for_each_interface;
804 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
805 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
806 interfaces.driver_init = hostapd_driver_init;
807 interfaces.global_iface_path = NULL;
808 interfaces.global_iface_name = NULL;
809 interfaces.global_ctrl_sock = -1;
Sunil Ravic0f5d412024-09-11 22:12:49 +0000810#ifdef CONFIG_IEEE80211BE
811 interfaces.mld_ctrl_iface_init = hostapd_mld_ctrl_iface_init;
812 interfaces.mld_ctrl_iface_deinit = hostapd_mld_ctrl_iface_deinit;
813#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800814 dl_list_init(&interfaces.global_ctrl_dst);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700815#ifdef CONFIG_ETH_P_OUI
816 dl_list_init(&interfaces.eth_p_oui);
817#endif /* CONFIG_ETH_P_OUI */
Roshan Pius3a1667e2018-07-03 15:17:14 -0700818#ifdef CONFIG_DPP
Hai Shalom81f62d82019-07-22 12:10:00 -0700819 os_memset(&dpp_conf, 0, sizeof(dpp_conf));
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700820 dpp_conf.cb_ctx = &interfaces;
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700821#ifdef CONFIG_DPP2
822 dpp_conf.remove_bi = hostapd_dpp_remove_bi;
823#endif /* CONFIG_DPP2 */
Hai Shalom81f62d82019-07-22 12:10:00 -0700824 interfaces.dpp = dpp_global_init(&dpp_conf);
Hai Shalom021b0b52019-04-10 11:17:58 -0700825 if (!interfaces.dpp)
826 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700827#endif /* CONFIG_DPP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829 for (;;) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000830 c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:q");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 if (c < 0)
832 break;
833 switch (c) {
834 case 'h':
835 usage();
836 break;
837 case 'd':
838 debug++;
839 if (wpa_debug_level > 0)
840 wpa_debug_level--;
841 break;
842 case 'B':
843 daemonize++;
844 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700845 case 'e':
846 entropy_file = optarg;
847 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700848 case 'f':
849 log_file = optarg;
850 break;
851 case 'K':
852 wpa_debug_show_keys++;
853 break;
854 case 'P':
855 os_free(pid_file);
856 pid_file = os_rel2abs_path(optarg);
857 break;
858 case 't':
859 wpa_debug_timestamp++;
860 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800861#ifdef CONFIG_DEBUG_LINUX_TRACING
862 case 'T':
863 enable_trace_dbg = 1;
864 break;
865#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 case 'v':
867 show_version();
868 exit(1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700869 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700870 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
871 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700872 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700873 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700874 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
875 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700876 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800877 case 'b':
878 tmp_bss = os_realloc_array(bss_config,
879 num_bss_configs + 1,
880 sizeof(char *));
881 if (tmp_bss == NULL)
882 goto out;
883 bss_config = tmp_bss;
884 bss_config[num_bss_configs++] = optarg;
885 break;
Paul Stewart092955c2017-02-06 09:13:09 -0800886#ifdef CONFIG_DEBUG_SYSLOG
887 case 's':
888 wpa_debug_syslog = 1;
889 break;
890#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800891 case 'S':
892 start_ifaces_in_sync = 1;
893 break;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800894#ifdef CONFIG_WPS
895 case 'u':
896 return gen_uuid(optarg);
897#endif /* CONFIG_WPS */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700898 case 'i':
899 if (hostapd_get_interface_names(&if_names,
900 &if_names_size, optarg))
901 goto out;
902 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000903 case 'q':
904 wpa_debug_level++;
905 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 default:
907 usage();
908 break;
909 }
910 }
911
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000912#ifndef CONFIG_CTRL_IFACE_AIDL
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800913 if (optind == argc && interfaces.global_iface_path == NULL &&
914 num_bss_configs == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 usage();
Roshan Piuscc817562017-12-22 14:45:05 -0800916#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917
918 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
919
920 if (log_file)
921 wpa_debug_open_file(log_file);
Hai Shalomfdcde762020-04-02 11:19:20 -0700922 if (!log_file && !wpa_debug_syslog)
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800923 wpa_debug_setup_stdout();
Paul Stewart092955c2017-02-06 09:13:09 -0800924#ifdef CONFIG_DEBUG_SYSLOG
925 if (wpa_debug_syslog)
926 wpa_debug_open_syslog();
927#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800928#ifdef CONFIG_DEBUG_LINUX_TRACING
929 if (enable_trace_dbg) {
930 int tret = wpa_debug_open_linux_tracing();
931 if (tret) {
932 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
933 return -1;
934 }
935 }
936#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937
938 interfaces.count = argc - optind;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800939 if (interfaces.count || num_bss_configs) {
940 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700941 sizeof(struct hostapd_iface *));
942 if (interfaces.iface == NULL) {
943 wpa_printf(MSG_ERROR, "malloc failed");
944 return -1;
945 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 }
947
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700948 if (hostapd_global_init(&interfaces, entropy_file)) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700949 wpa_printf(MSG_ERROR, "Failed to initialize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700951 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700952
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800953 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
954 hostapd_periodic, &interfaces, NULL);
955
956 if (fst_global_init()) {
957 wpa_printf(MSG_ERROR,
958 "Failed to initialize global FST context");
959 goto out;
960 }
961
962#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
963 if (!fst_global_add_ctrl(fst_ctrl_cli))
964 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
965#endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
966
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800967 /* Allocate and parse configuration for full interface files */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700969 char *if_name = NULL;
970
971 if (i < if_names_size)
972 if_name = if_names[i];
973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 interfaces.iface[i] = hostapd_interface_init(&interfaces,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700975 if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 argv[optind + i],
977 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700978 if (!interfaces.iface[i]) {
979 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700981 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800982 if (start_ifaces_in_sync)
983 interfaces.iface[i]->need_to_start_in_sync = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984 }
985
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800986 /* Allocate and parse configuration for per-BSS files */
987 for (i = 0; i < num_bss_configs; i++) {
988 struct hostapd_iface *iface;
989 char *fname;
990
991 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
992 fname = os_strchr(bss_config[i], ':');
993 if (fname == NULL) {
994 wpa_printf(MSG_ERROR,
995 "Invalid BSS config identifier '%s'",
996 bss_config[i]);
997 goto out;
998 }
999 *fname++ = '\0';
1000 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
1001 fname, debug);
1002 if (iface == NULL)
1003 goto out;
1004 for (j = 0; j < interfaces.count; j++) {
1005 if (interfaces.iface[j] == iface)
1006 break;
1007 }
1008 if (j == interfaces.count) {
1009 struct hostapd_iface **tmp;
1010 tmp = os_realloc_array(interfaces.iface,
1011 interfaces.count + 1,
1012 sizeof(struct hostapd_iface *));
1013 if (tmp == NULL) {
1014 hostapd_interface_deinit_free(iface);
1015 goto out;
1016 }
1017 interfaces.iface = tmp;
1018 interfaces.iface[interfaces.count++] = iface;
1019 }
1020 }
1021
1022 /*
1023 * Enable configured interfaces. Depending on channel configuration,
1024 * this may complete full initialization before returning or use a
1025 * callback mechanism to complete setup in case of operations like HT
1026 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
1027 * In such case, the interface will be enabled from eloop context within
1028 * hostapd_global_run().
1029 */
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001030 interfaces.terminate_on_error = interfaces.count;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001031 for (i = 0; i < interfaces.count; i++) {
Hai Shalom74f70d42019-02-11 14:42:39 -08001032 if (hostapd_driver_init(interfaces.iface[i]) ||
1033 hostapd_setup_interface(interfaces.iface[i]))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001034 goto out;
1035 }
1036
Daichi Ueuracaa74a82018-02-14 22:25:39 +09001037 hostapd_global_ctrl_iface_init(&interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001038
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001039 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
1040 wpa_printf(MSG_ERROR, "Failed to start eloop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001042 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043
1044 ret = 0;
1045
1046 out:
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001047#ifdef CONFIG_CTRL_IFACE_AIDL
1048 hostapd_aidl_deinit(&interfaces);
1049#endif /* CONFIG_CTRL_IFACE_AIDL */
Daichi Ueuracaa74a82018-02-14 22:25:39 +09001050 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051 /* Deinitialize all interfaces */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001052 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001053 if (!interfaces.iface[i])
1054 continue;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001055 interfaces.iface[i]->driver_ap_teardown =
1056 !!(interfaces.iface[i]->drv_flags &
1057 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058 hostapd_interface_deinit_free(interfaces.iface[i]);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001059 interfaces.iface[i] = NULL;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001060 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 os_free(interfaces.iface);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001062 interfaces.iface = NULL;
1063 interfaces.count = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064
Sunil Ravi99c035e2024-07-12 01:42:03 +00001065 hostapd_global_cleanup_mld(&interfaces);
1066
Roshan Pius3a1667e2018-07-03 15:17:14 -07001067#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07001068 dpp_global_deinit(interfaces.dpp);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001069#endif /* CONFIG_DPP */
1070
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001071 if (interfaces.eloop_initialized)
1072 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
1073 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 os_free(pid_file);
1075
Paul Stewart092955c2017-02-06 09:13:09 -08001076 wpa_debug_close_syslog();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001077 if (log_file)
1078 wpa_debug_close_file();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001079 wpa_debug_close_linux_tracing();
1080
1081 os_free(bss_config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001083 for (i = 0; i < if_names_size; i++)
1084 os_free(if_names[i]);
1085 os_free(if_names);
1086
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001087 fst_global_deinit();
1088
Sunil Ravia04bd252022-05-02 22:54:18 -07001089 crypto_unload();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 os_program_deinit();
1091
1092 return ret;
1093}