blob: 640a169406f1d0539db0540d266750847d7c7b71 [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 Ravi99c035e2024-07-12 01:42:03 +0000196 hostapd_mld_add_link(hapd);
Sunil Ravi7f769292024-07-23 22:21:32 +0000197 wpa_printf(MSG_DEBUG,
198 "Setup of non first link (%d) BSS of MLD %s",
199 hapd->mld_link_id, hapd->conf->iface);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000200
201 goto setup_mld;
202 }
203#endif /* CONFIG_IEEE80211BE */
204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 /* Initialize the driver interface */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000206 if (is_zero_ether_addr(b))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207 b = NULL;
208
209 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800210 for (i = 0; wpa_drivers[i]; i++) {
211 if (wpa_drivers[i] != hapd->driver)
212 continue;
213
214 if (global.drv_priv[i] == NULL &&
215 wpa_drivers[i]->global_init) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700216 global.drv_priv[i] =
217 wpa_drivers[i]->global_init(iface->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800218 if (global.drv_priv[i] == NULL) {
219 wpa_printf(MSG_ERROR, "Failed to initialize "
220 "driver '%s'",
221 wpa_drivers[i]->name);
222 return -1;
223 }
224 }
225
226 params.global_priv = global.drv_priv[i];
227 break;
228 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229 params.bssid = b;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000230#ifdef CONFIG_IEEE80211BE
231 /*
232 * Use the configured MLD MAC address as the interface hardware address
233 * if this AP is a part of an AP MLD.
234 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000235 if (hapd->conf->mld_ap) {
236 if (!is_zero_ether_addr(hapd->conf->mld_addr))
237 params.bssid = hapd->conf->mld_addr;
238 else
239 params.bssid = NULL;
240 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000241#endif /* CONFIG_IEEE80211BE */
242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 params.ifname = hapd->conf->iface;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800244 params.driver_params = hapd->iconf->driver_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
246
247 params.num_bridge = hapd->iface->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700248 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249 if (params.bridge == NULL)
250 return -1;
251 for (i = 0; i < hapd->iface->num_bss; i++) {
252 struct hostapd_data *bss = hapd->iface->bss[i];
253 if (bss->conf->bridge[0])
254 params.bridge[i] = bss->conf->bridge;
255 }
256
257 params.own_addr = hapd->own_addr;
258
259 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
260 os_free(params.bridge);
261 if (hapd->drv_priv == NULL) {
262 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
263 hapd->driver->name);
264 hapd->driver = NULL;
265 return -1;
266 }
267
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000268#ifdef CONFIG_IEEE80211BE
269 /*
270 * This is the first interface added to the AP MLD, so have the
271 * interface hardware address be the MLD address, while the link address
272 * would be derived from the original interface address if BSSID is not
273 * configured, and otherwise it would be the configured BSSID.
274 */
275 if (hapd->conf->mld_ap) {
Sunil Ravi99c035e2024-07-12 01:42:03 +0000276 os_memcpy(hapd->mld->mld_addr, hapd->own_addr, ETH_ALEN);
277
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000278 if (!b)
279 random_mac_addr_keep_oui(hapd->own_addr);
280 else
281 os_memcpy(hapd->own_addr, b, ETH_ALEN);
Sunil Ravi99c035e2024-07-12 01:42:03 +0000282
283 hostapd_mld_add_link(hapd);
Sunil Ravi7f769292024-07-23 22:21:32 +0000284 wpa_printf(MSG_DEBUG, "Setup of first link (%d) BSS of MLD %s",
285 hapd->mld_link_id, hapd->conf->iface);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000286 }
287
288setup_mld:
289#endif /* CONFIG_IEEE80211BE */
290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 if (hapd->driver->get_capa &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800292 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700293 struct wowlan_triggers *triggs;
294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 iface->drv_flags = capa.flags;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700296 iface->drv_flags2 = capa.flags2;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000297 iface->drv_rrm_flags = capa.rrm_flags;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800298 iface->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700299 /*
300 * Use default extended capa values from per-radio information
301 */
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700302 iface->extended_capa = capa.extended_capa;
303 iface->extended_capa_mask = capa.extended_capa_mask;
304 iface->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700305 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700306
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700307 /*
308 * Override extended capa with per-interface type (AP), if
309 * available from the driver.
310 */
311 hostapd_get_ext_capa(iface);
312
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000313 hostapd_get_mld_capa(iface);
314
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700315 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
316 if (triggs && hapd->driver->set_wowlan) {
317 if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
318 wpa_printf(MSG_ERROR, "set_wowlan failed");
319 }
320 os_free(triggs);
Sunil Ravi77d572f2023-01-17 23:58:31 +0000321
322 iface->mbssid_max_interfaces = capa.mbssid_max_interfaces;
323 iface->ema_max_periodicity = capa.ema_max_periodicity;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800324 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000326#ifdef CONFIG_IEEE80211BE
327 if (hapd->conf->mld_ap) {
328 if (!(iface->drv_flags2 & WPA_DRIVER_FLAGS2_MLO)) {
329 wpa_printf(MSG_INFO,
330 "MLD: Not supported by the driver");
331 return -1;
332 }
333
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000334 /* Initialize the BSS parameter change to 1 */
335 hapd->eht_mld_bss_param_change = 1;
336
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000337 wpa_printf(MSG_DEBUG,
338 "MLD: Set link_id=%u, mld_addr=" MACSTR
339 ", own_addr=" MACSTR,
Sunil Ravi99c035e2024-07-12 01:42:03 +0000340 hapd->mld_link_id, MAC2STR(hapd->mld->mld_addr),
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000341 MAC2STR(hapd->own_addr));
342
343 hostapd_drv_link_add(hapd, hapd->mld_link_id,
344 hapd->own_addr);
345 }
346#endif /* CONFIG_IEEE80211BE */
347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 return 0;
349}
350
351
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800352/**
353 * hostapd_interface_init - Read configuration file and init BSS data
354 *
355 * This function is used to parse configuration file for a full interface (one
356 * or more BSSes sharing the same radio) and allocate memory for the BSS
Hai Shalom74f70d42019-02-11 14:42:39 -0800357 * interfaces. No actual driver operations are started.
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800358 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359static struct hostapd_iface *
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700360hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361 const char *config_fname, int debug)
362{
363 struct hostapd_iface *iface;
364 int k;
365
Hai Shalomfdcde762020-04-02 11:19:20 -0700366 wpa_printf(MSG_DEBUG, "Configuration file: %s", config_fname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800367 iface = hostapd_init(interfaces, config_fname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 if (!iface)
369 return NULL;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700370
371 if (if_name) {
372 os_strlcpy(iface->conf->bss[0]->iface, if_name,
373 sizeof(iface->conf->bss[0]->iface));
374 }
375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 iface->interfaces = interfaces;
377
378 for (k = 0; k < debug; k++) {
379 if (iface->bss[0]->conf->logger_stdout_level > 0)
380 iface->bss[0]->conf->logger_stdout_level--;
381 }
382
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800383 if (iface->conf->bss[0]->iface[0] == '\0' &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700384 !hostapd_drv_none(iface->bss[0])) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700385 wpa_printf(MSG_ERROR,
386 "Interface name not specified in %s, nor by '-i' parameter",
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700387 config_fname);
388 hostapd_interface_deinit_free(iface);
389 return NULL;
390 }
391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 return iface;
393}
394
395
396/**
397 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
398 */
399static void handle_term(int sig, void *signal_ctx)
400{
401 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
402 eloop_terminate();
403}
404
405
406#ifndef CONFIG_NATIVE_WINDOWS
407
408static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
409{
410 if (hostapd_reload_config(iface) < 0) {
411 wpa_printf(MSG_WARNING, "Failed to read new configuration "
412 "file - continuing with old.");
413 }
414 return 0;
415}
416
417
418/**
419 * handle_reload - SIGHUP handler to reload configuration
420 */
421static void handle_reload(int sig, void *signal_ctx)
422{
423 struct hapd_interfaces *interfaces = signal_ctx;
424 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
425 sig);
426 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
427}
428
429
430static void handle_dump_state(int sig, void *signal_ctx)
431{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800432 /* Not used anymore - ignore signal */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433}
434#endif /* CONFIG_NATIVE_WINDOWS */
435
436
Jouni Malinen75ecf522011-06-27 15:19:46 -0700437static int hostapd_global_init(struct hapd_interfaces *interfaces,
438 const char *entropy_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800440 int i;
441
442 os_memset(&global, 0, sizeof(global));
443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 hostapd_logger_register_cb(hostapd_logger_cb);
445
446 if (eap_server_register_methods()) {
447 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
448 return -1;
449 }
450
451 if (eloop_init()) {
452 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
453 return -1;
454 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700455 interfaces->eloop_initialized = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456
Jouni Malinen75ecf522011-06-27 15:19:46 -0700457 random_init(entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458
459#ifndef CONFIG_NATIVE_WINDOWS
460 eloop_register_signal(SIGHUP, handle_reload, interfaces);
461 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
462#endif /* CONFIG_NATIVE_WINDOWS */
463 eloop_register_signal_terminate(handle_term, interfaces);
464
465#ifndef CONFIG_NATIVE_WINDOWS
466 openlog("hostapd", 0, LOG_DAEMON);
467#endif /* CONFIG_NATIVE_WINDOWS */
468
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800469 for (i = 0; wpa_drivers[i]; i++)
470 global.drv_count++;
471 if (global.drv_count == 0) {
472 wpa_printf(MSG_ERROR, "No drivers enabled");
473 return -1;
474 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700475 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800476 if (global.drv_priv == NULL)
477 return -1;
478
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700479 return 0;
480}
481
482
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700483static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800485 int i;
486
487 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
488 if (!global.drv_priv[i])
489 continue;
490 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
491 }
492 os_free(global.drv_priv);
493 global.drv_priv = NULL;
494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495#ifdef EAP_SERVER_TNC
496 tncs_global_deinit();
497#endif /* EAP_SERVER_TNC */
498
499 random_deinit();
500
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700501 if (eloop_initialized)
502 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700503
504#ifndef CONFIG_NATIVE_WINDOWS
505 closelog();
506#endif /* CONFIG_NATIVE_WINDOWS */
507
508 eap_server_unregister_methods();
509
510 os_daemonize_terminate(pid_file);
511}
512
513
514static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
515 const char *pid_file)
516{
517#ifdef EAP_SERVER_TNC
518 int tnc = 0;
519 size_t i, k;
520
521 for (i = 0; !tnc && i < ifaces->count; i++) {
522 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
523 if (ifaces->iface[i]->bss[0]->conf->tnc) {
524 tnc++;
525 break;
526 }
527 }
528 }
529
530 if (tnc && tncs_global_init() < 0) {
531 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
532 return -1;
533 }
534#endif /* EAP_SERVER_TNC */
535
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800536 if (daemonize) {
537 if (os_daemonize(pid_file)) {
538 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
539 return -1;
540 }
541 if (eloop_sock_requeue()) {
542 wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
543 strerror(errno));
544 return -1;
545 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 }
547
Veerendranath Jakkam9d0d5212023-02-21 19:55:58 +0530548#ifdef CONFIG_CTRL_IFACE_AIDL
549 if (hostapd_aidl_init(ifaces)) {
550 wpa_printf(MSG_ERROR, "Failed to initialize AIDL interface");
551 return -1;
552 }
553#endif /* CONFIG_CTRL_IFACE_AIDL */
554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 eloop_run();
556
557 return 0;
558}
559
560
561static void show_version(void)
562{
563 fprintf(stderr,
Hai Shalomfdcde762020-04-02 11:19:20 -0700564 "hostapd v%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700565 "User space daemon for IEEE 802.11 AP management,\n"
566 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Sunil Ravi7f769292024-07-23 22:21:32 +0000567 "Copyright (c) 2002-2024, Jouni Malinen <j@w1.fi> "
Hai Shalomfdcde762020-04-02 11:19:20 -0700568 "and contributors\n",
569 VERSION_STR);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570}
571
572
573static void usage(void)
574{
575 show_version();
576 fprintf(stderr,
577 "\n"
Sunil8cd6f4d2022-06-28 18:40:46 +0000578 "usage: hostapd [-hdBKtvq] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700579 "\\\n"
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700580 " [-g <global ctrl_iface>] [-G <group>]\\\n"
581 " [-i <comma-separated list of interface names>]\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700582 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700583 "\n"
584 "options:\n"
585 " -h show this usage\n"
586 " -d show more debug messages (-dd for even more)\n"
587 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700588 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700589 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700590 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700591 " -P PID file\n"
592 " -K include key data in debug messages\n"
593#ifdef CONFIG_DEBUG_FILE
594 " -f log output to debug file instead of stdout\n"
595#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800596#ifdef CONFIG_DEBUG_LINUX_TRACING
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800597 " -T record to Linux tracing in addition to logging\n"
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800598 " (records all messages regardless of debug verbosity)\n"
599#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700600 " -i list of interface names to use\n"
Paul Stewart092955c2017-02-06 09:13:09 -0800601#ifdef CONFIG_DEBUG_SYSLOG
602 " -s log output to syslog instead of stdout\n"
603#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800604 " -S start all the interfaces synchronously\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605 " -t include timestamps in some debug messages\n"
Sunil Ravi89eba102022-09-13 21:04:37 -0700606 " -v show hostapd version\n"
607 " -q show less debug messages (-qq for even less)\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608
609 exit(1);
610}
611
612
613static const char * hostapd_msg_ifname_cb(void *ctx)
614{
615 struct hostapd_data *hapd = ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800616 if (hapd && hapd->conf)
617 return hapd->conf->iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 return NULL;
619}
620
621
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700622static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
623 const char *path)
624{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800625#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700626 char *pos;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800627#endif /* !CONFIG_CTRL_IFACE_UDP */
628
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700629 os_free(interfaces->global_iface_path);
630 interfaces->global_iface_path = os_strdup(path);
631 if (interfaces->global_iface_path == NULL)
632 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800633
634#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700635 pos = os_strrchr(interfaces->global_iface_path, '/');
636 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700637 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
638 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700639 os_free(interfaces->global_iface_path);
640 interfaces->global_iface_path = NULL;
641 return -1;
642 }
643
644 *pos = '\0';
645 interfaces->global_iface_name = pos + 1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800646#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700647
648 return 0;
649}
650
651
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700652static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
653 const char *group)
654{
655#ifndef CONFIG_NATIVE_WINDOWS
656 struct group *grp;
657 grp = getgrnam(group);
658 if (grp == NULL) {
659 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
660 return -1;
661 }
662 interfaces->ctrl_iface_group = grp->gr_gid;
663#endif /* CONFIG_NATIVE_WINDOWS */
664 return 0;
665}
666
667
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700668static int hostapd_get_interface_names(char ***if_names,
669 size_t *if_names_size,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800670 char *arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700671{
672 char *if_name, *tmp, **nnames;
673 size_t i;
674
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800675 if (!arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700676 return -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800677 if_name = strtok_r(arg, ",", &tmp);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700678
679 while (if_name) {
680 nnames = os_realloc_array(*if_names, 1 + *if_names_size,
681 sizeof(char *));
682 if (!nnames)
683 goto fail;
684 *if_names = nnames;
685
686 (*if_names)[*if_names_size] = os_strdup(if_name);
687 if (!(*if_names)[*if_names_size])
688 goto fail;
689 (*if_names_size)++;
690 if_name = strtok_r(NULL, ",", &tmp);
691 }
692
693 return 0;
694
695fail:
696 for (i = 0; i < *if_names_size; i++)
697 os_free((*if_names)[i]);
698 os_free(*if_names);
699 *if_names = NULL;
700 *if_names_size = 0;
701 return -1;
702}
703
704
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800705#ifdef CONFIG_WPS
706static int gen_uuid(const char *txt_addr)
707{
708 u8 addr[ETH_ALEN];
709 u8 uuid[UUID_LEN];
710 char buf[100];
711
712 if (hwaddr_aton(txt_addr, addr) < 0)
713 return -1;
714
715 uuid_gen_mac_addr(addr, uuid);
716 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
717 return -1;
718
719 printf("%s\n", buf);
720
721 return 0;
722}
723#endif /* CONFIG_WPS */
724
725
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800726#ifndef HOSTAPD_CLEANUP_INTERVAL
727#define HOSTAPD_CLEANUP_INTERVAL 10
728#endif /* HOSTAPD_CLEANUP_INTERVAL */
729
730static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
731{
732 hostapd_periodic_iface(iface);
733 return 0;
734}
735
736
737/* Periodic cleanup tasks */
738static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
739{
740 struct hapd_interfaces *interfaces = eloop_ctx;
741
742 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
743 hostapd_periodic, interfaces, NULL);
744 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
745}
746
747
Sunil Ravi99c035e2024-07-12 01:42:03 +0000748static void hostapd_global_cleanup_mld(struct hapd_interfaces *interfaces)
749{
750#ifdef CONFIG_IEEE80211BE
751 size_t i;
752
753 if (!interfaces || !interfaces->mld)
754 return;
755
756 for (i = 0; i < interfaces->mld_count; i++) {
757 if (!interfaces->mld[i])
758 continue;
759
760 os_free(interfaces->mld[i]);
761 interfaces->mld[i] = NULL;
762 }
763
764 os_free(interfaces->mld);
765 interfaces->mld = NULL;
766 interfaces->mld_count = 0;
767#endif /* CONFIG_IEEE80211BE */
768}
769
770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771int main(int argc, char *argv[])
772{
773 struct hapd_interfaces interfaces;
774 int ret = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800775 size_t i, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776 int c, debug = 0, daemonize = 0;
777 char *pid_file = NULL;
778 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700779 const char *entropy_file = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800780 char **bss_config = NULL, **tmp_bss;
781 size_t num_bss_configs = 0;
782#ifdef CONFIG_DEBUG_LINUX_TRACING
783 int enable_trace_dbg = 0;
784#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800785 int start_ifaces_in_sync = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700786 char **if_names = NULL;
787 size_t if_names_size = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700788#ifdef CONFIG_DPP
789 struct dpp_global_config dpp_conf;
790#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700791
792 if (os_program_init())
793 return -1;
794
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700795 os_memset(&interfaces, 0, sizeof(interfaces));
796 interfaces.reload_config = hostapd_reload_config;
797 interfaces.config_read_cb = hostapd_config_read;
798 interfaces.for_each_interface = hostapd_for_each_interface;
799 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
800 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
801 interfaces.driver_init = hostapd_driver_init;
802 interfaces.global_iface_path = NULL;
803 interfaces.global_iface_name = NULL;
804 interfaces.global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800805 dl_list_init(&interfaces.global_ctrl_dst);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700806#ifdef CONFIG_ETH_P_OUI
807 dl_list_init(&interfaces.eth_p_oui);
808#endif /* CONFIG_ETH_P_OUI */
Roshan Pius3a1667e2018-07-03 15:17:14 -0700809#ifdef CONFIG_DPP
Hai Shalom81f62d82019-07-22 12:10:00 -0700810 os_memset(&dpp_conf, 0, sizeof(dpp_conf));
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700811 dpp_conf.cb_ctx = &interfaces;
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700812#ifdef CONFIG_DPP2
813 dpp_conf.remove_bi = hostapd_dpp_remove_bi;
814#endif /* CONFIG_DPP2 */
Hai Shalom81f62d82019-07-22 12:10:00 -0700815 interfaces.dpp = dpp_global_init(&dpp_conf);
Hai Shalom021b0b52019-04-10 11:17:58 -0700816 if (!interfaces.dpp)
817 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700818#endif /* CONFIG_DPP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 for (;;) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000821 c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:q");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 if (c < 0)
823 break;
824 switch (c) {
825 case 'h':
826 usage();
827 break;
828 case 'd':
829 debug++;
830 if (wpa_debug_level > 0)
831 wpa_debug_level--;
832 break;
833 case 'B':
834 daemonize++;
835 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700836 case 'e':
837 entropy_file = optarg;
838 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839 case 'f':
840 log_file = optarg;
841 break;
842 case 'K':
843 wpa_debug_show_keys++;
844 break;
845 case 'P':
846 os_free(pid_file);
847 pid_file = os_rel2abs_path(optarg);
848 break;
849 case 't':
850 wpa_debug_timestamp++;
851 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800852#ifdef CONFIG_DEBUG_LINUX_TRACING
853 case 'T':
854 enable_trace_dbg = 1;
855 break;
856#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857 case 'v':
858 show_version();
859 exit(1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700860 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700861 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
862 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700863 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700864 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700865 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
866 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700867 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800868 case 'b':
869 tmp_bss = os_realloc_array(bss_config,
870 num_bss_configs + 1,
871 sizeof(char *));
872 if (tmp_bss == NULL)
873 goto out;
874 bss_config = tmp_bss;
875 bss_config[num_bss_configs++] = optarg;
876 break;
Paul Stewart092955c2017-02-06 09:13:09 -0800877#ifdef CONFIG_DEBUG_SYSLOG
878 case 's':
879 wpa_debug_syslog = 1;
880 break;
881#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800882 case 'S':
883 start_ifaces_in_sync = 1;
884 break;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800885#ifdef CONFIG_WPS
886 case 'u':
887 return gen_uuid(optarg);
888#endif /* CONFIG_WPS */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700889 case 'i':
890 if (hostapd_get_interface_names(&if_names,
891 &if_names_size, optarg))
892 goto out;
893 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000894 case 'q':
895 wpa_debug_level++;
896 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 default:
898 usage();
899 break;
900 }
901 }
902
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000903#ifndef CONFIG_CTRL_IFACE_AIDL
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800904 if (optind == argc && interfaces.global_iface_path == NULL &&
905 num_bss_configs == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 usage();
Roshan Piuscc817562017-12-22 14:45:05 -0800907#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908
909 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
910
911 if (log_file)
912 wpa_debug_open_file(log_file);
Hai Shalomfdcde762020-04-02 11:19:20 -0700913 if (!log_file && !wpa_debug_syslog)
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800914 wpa_debug_setup_stdout();
Paul Stewart092955c2017-02-06 09:13:09 -0800915#ifdef CONFIG_DEBUG_SYSLOG
916 if (wpa_debug_syslog)
917 wpa_debug_open_syslog();
918#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800919#ifdef CONFIG_DEBUG_LINUX_TRACING
920 if (enable_trace_dbg) {
921 int tret = wpa_debug_open_linux_tracing();
922 if (tret) {
923 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
924 return -1;
925 }
926 }
927#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928
929 interfaces.count = argc - optind;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800930 if (interfaces.count || num_bss_configs) {
931 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700932 sizeof(struct hostapd_iface *));
933 if (interfaces.iface == NULL) {
934 wpa_printf(MSG_ERROR, "malloc failed");
935 return -1;
936 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 }
938
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700939 if (hostapd_global_init(&interfaces, entropy_file)) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700940 wpa_printf(MSG_ERROR, "Failed to initialize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700942 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800944 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
945 hostapd_periodic, &interfaces, NULL);
946
947 if (fst_global_init()) {
948 wpa_printf(MSG_ERROR,
949 "Failed to initialize global FST context");
950 goto out;
951 }
952
953#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
954 if (!fst_global_add_ctrl(fst_ctrl_cli))
955 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
956#endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
957
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800958 /* Allocate and parse configuration for full interface files */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700960 char *if_name = NULL;
961
962 if (i < if_names_size)
963 if_name = if_names[i];
964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 interfaces.iface[i] = hostapd_interface_init(&interfaces,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700966 if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 argv[optind + i],
968 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700969 if (!interfaces.iface[i]) {
970 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700972 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800973 if (start_ifaces_in_sync)
974 interfaces.iface[i]->need_to_start_in_sync = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 }
976
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800977 /* Allocate and parse configuration for per-BSS files */
978 for (i = 0; i < num_bss_configs; i++) {
979 struct hostapd_iface *iface;
980 char *fname;
981
982 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
983 fname = os_strchr(bss_config[i], ':');
984 if (fname == NULL) {
985 wpa_printf(MSG_ERROR,
986 "Invalid BSS config identifier '%s'",
987 bss_config[i]);
988 goto out;
989 }
990 *fname++ = '\0';
991 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
992 fname, debug);
993 if (iface == NULL)
994 goto out;
995 for (j = 0; j < interfaces.count; j++) {
996 if (interfaces.iface[j] == iface)
997 break;
998 }
999 if (j == interfaces.count) {
1000 struct hostapd_iface **tmp;
1001 tmp = os_realloc_array(interfaces.iface,
1002 interfaces.count + 1,
1003 sizeof(struct hostapd_iface *));
1004 if (tmp == NULL) {
1005 hostapd_interface_deinit_free(iface);
1006 goto out;
1007 }
1008 interfaces.iface = tmp;
1009 interfaces.iface[interfaces.count++] = iface;
1010 }
1011 }
1012
1013 /*
1014 * Enable configured interfaces. Depending on channel configuration,
1015 * this may complete full initialization before returning or use a
1016 * callback mechanism to complete setup in case of operations like HT
1017 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
1018 * In such case, the interface will be enabled from eloop context within
1019 * hostapd_global_run().
1020 */
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001021 interfaces.terminate_on_error = interfaces.count;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001022 for (i = 0; i < interfaces.count; i++) {
Hai Shalom74f70d42019-02-11 14:42:39 -08001023 if (hostapd_driver_init(interfaces.iface[i]) ||
1024 hostapd_setup_interface(interfaces.iface[i]))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001025 goto out;
1026 }
1027
Daichi Ueuracaa74a82018-02-14 22:25:39 +09001028 hostapd_global_ctrl_iface_init(&interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001029
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001030 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
1031 wpa_printf(MSG_ERROR, "Failed to start eloop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001033 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034
1035 ret = 0;
1036
1037 out:
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001038#ifdef CONFIG_CTRL_IFACE_AIDL
1039 hostapd_aidl_deinit(&interfaces);
1040#endif /* CONFIG_CTRL_IFACE_AIDL */
Daichi Ueuracaa74a82018-02-14 22:25:39 +09001041 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 /* Deinitialize all interfaces */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001043 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001044 if (!interfaces.iface[i])
1045 continue;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001046 interfaces.iface[i]->driver_ap_teardown =
1047 !!(interfaces.iface[i]->drv_flags &
1048 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 hostapd_interface_deinit_free(interfaces.iface[i]);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001050 interfaces.iface[i] = NULL;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001051 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 os_free(interfaces.iface);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001053 interfaces.iface = NULL;
1054 interfaces.count = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055
Sunil Ravi99c035e2024-07-12 01:42:03 +00001056 hostapd_global_cleanup_mld(&interfaces);
1057
Roshan Pius3a1667e2018-07-03 15:17:14 -07001058#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07001059 dpp_global_deinit(interfaces.dpp);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001060#endif /* CONFIG_DPP */
1061
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001062 if (interfaces.eloop_initialized)
1063 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
1064 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 os_free(pid_file);
1066
Paul Stewart092955c2017-02-06 09:13:09 -08001067 wpa_debug_close_syslog();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 if (log_file)
1069 wpa_debug_close_file();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001070 wpa_debug_close_linux_tracing();
1071
1072 os_free(bss_config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001074 for (i = 0; i < if_names_size; i++)
1075 os_free(if_names[i]);
1076 os_free(if_names);
1077
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001078 fst_global_deinit();
1079
Sunil Ravia04bd252022-05-02 22:54:18 -07001080 crypto_unload();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 os_program_deinit();
1082
1083 return ret;
1084}