blob: 3a14381806b75303047ac1d0755add0f4719485d [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;
163
164 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
165 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
166 return -1;
167 }
168
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000169#ifdef CONFIG_IEEE80211BE
170 for (i = 0; conf->mld_ap && i < iface->interfaces->count; i++) {
171 struct hostapd_iface *h = iface->interfaces->iface[i];
172 struct hostapd_data *h_hapd = h->bss[0];
173 struct hostapd_bss_config *hconf = h_hapd->conf;
174
175 if (h == iface) {
176 wpa_printf(MSG_DEBUG, "MLD: Skip own interface");
177 continue;
178 }
179
Sunil Ravi99c035e2024-07-12 01:42:03 +0000180 if (!hconf->mld_ap) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000181 wpa_printf(MSG_DEBUG,
Sunil Ravi99c035e2024-07-12 01:42:03 +0000182 "MLD: Skip non-MLD");
183 continue;
184 }
185
186 if (!hostapd_is_ml_partner(hapd, h_hapd)) {
187 wpa_printf(MSG_DEBUG,
188 "MLD: Skip non matching MLD vif name");
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000189 continue;
190 }
191
192 wpa_printf(MSG_DEBUG, "MLD: Found matching MLD interface");
193 if (!h_hapd->drv_priv) {
194 wpa_printf(MSG_DEBUG,
195 "MLD: Matching MLD BSS not initialized yet");
196 continue;
197 }
198
199 hapd->drv_priv = h_hapd->drv_priv;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000200 hapd->interface_added = h_hapd->interface_added;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000201
202 /*
203 * All interfaces participating in the AP MLD would have
204 * the same MLD address, which is the interface hardware
205 * address, while the interface address would be
206 * derived from the original interface address if BSSID
207 * is not configured, and otherwise it would be the
208 * configured BSSID.
209 */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000210 if (is_zero_ether_addr(b)) {
Sunil Ravi99c035e2024-07-12 01:42:03 +0000211 os_memcpy(hapd->own_addr, h_hapd->mld->mld_addr,
212 ETH_ALEN);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000213 random_mac_addr_keep_oui(hapd->own_addr);
214 } else {
215 os_memcpy(hapd->own_addr, b, ETH_ALEN);
216 }
217
Sunil Ravi99c035e2024-07-12 01:42:03 +0000218 hostapd_mld_add_link(hapd);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000219
220 goto setup_mld;
221 }
222#endif /* CONFIG_IEEE80211BE */
223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224 /* Initialize the driver interface */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000225 if (is_zero_ether_addr(b))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 b = NULL;
227
228 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800229 for (i = 0; wpa_drivers[i]; i++) {
230 if (wpa_drivers[i] != hapd->driver)
231 continue;
232
233 if (global.drv_priv[i] == NULL &&
234 wpa_drivers[i]->global_init) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700235 global.drv_priv[i] =
236 wpa_drivers[i]->global_init(iface->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800237 if (global.drv_priv[i] == NULL) {
238 wpa_printf(MSG_ERROR, "Failed to initialize "
239 "driver '%s'",
240 wpa_drivers[i]->name);
241 return -1;
242 }
243 }
244
245 params.global_priv = global.drv_priv[i];
246 break;
247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 params.bssid = b;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000249#ifdef CONFIG_IEEE80211BE
250 /*
251 * Use the configured MLD MAC address as the interface hardware address
252 * if this AP is a part of an AP MLD.
253 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000254 if (hapd->conf->mld_ap) {
255 if (!is_zero_ether_addr(hapd->conf->mld_addr))
256 params.bssid = hapd->conf->mld_addr;
257 else
258 params.bssid = NULL;
259 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000260#endif /* CONFIG_IEEE80211BE */
261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 params.ifname = hapd->conf->iface;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800263 params.driver_params = hapd->iconf->driver_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
265
266 params.num_bridge = hapd->iface->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700267 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268 if (params.bridge == NULL)
269 return -1;
270 for (i = 0; i < hapd->iface->num_bss; i++) {
271 struct hostapd_data *bss = hapd->iface->bss[i];
272 if (bss->conf->bridge[0])
273 params.bridge[i] = bss->conf->bridge;
274 }
275
276 params.own_addr = hapd->own_addr;
277
278 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
279 os_free(params.bridge);
280 if (hapd->drv_priv == NULL) {
281 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
282 hapd->driver->name);
283 hapd->driver = NULL;
284 return -1;
285 }
286
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000287#ifdef CONFIG_IEEE80211BE
288 /*
289 * This is the first interface added to the AP MLD, so have the
290 * interface hardware address be the MLD address, while the link address
291 * would be derived from the original interface address if BSSID is not
292 * configured, and otherwise it would be the configured BSSID.
293 */
294 if (hapd->conf->mld_ap) {
Sunil Ravi99c035e2024-07-12 01:42:03 +0000295 os_memcpy(hapd->mld->mld_addr, hapd->own_addr, ETH_ALEN);
296
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000297 if (!b)
298 random_mac_addr_keep_oui(hapd->own_addr);
299 else
300 os_memcpy(hapd->own_addr, b, ETH_ALEN);
Sunil Ravi99c035e2024-07-12 01:42:03 +0000301
302 hostapd_mld_add_link(hapd);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000303 }
304
305setup_mld:
306#endif /* CONFIG_IEEE80211BE */
307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 if (hapd->driver->get_capa &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800309 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700310 struct wowlan_triggers *triggs;
311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312 iface->drv_flags = capa.flags;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700313 iface->drv_flags2 = capa.flags2;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000314 iface->drv_rrm_flags = capa.rrm_flags;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800315 iface->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700316 /*
317 * Use default extended capa values from per-radio information
318 */
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700319 iface->extended_capa = capa.extended_capa;
320 iface->extended_capa_mask = capa.extended_capa_mask;
321 iface->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700322 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700323
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700324 /*
325 * Override extended capa with per-interface type (AP), if
326 * available from the driver.
327 */
328 hostapd_get_ext_capa(iface);
329
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000330 hostapd_get_mld_capa(iface);
331
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700332 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
333 if (triggs && hapd->driver->set_wowlan) {
334 if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
335 wpa_printf(MSG_ERROR, "set_wowlan failed");
336 }
337 os_free(triggs);
Sunil Ravi77d572f2023-01-17 23:58:31 +0000338
339 iface->mbssid_max_interfaces = capa.mbssid_max_interfaces;
340 iface->ema_max_periodicity = capa.ema_max_periodicity;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800341 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000343#ifdef CONFIG_IEEE80211BE
344 if (hapd->conf->mld_ap) {
345 if (!(iface->drv_flags2 & WPA_DRIVER_FLAGS2_MLO)) {
346 wpa_printf(MSG_INFO,
347 "MLD: Not supported by the driver");
348 return -1;
349 }
350
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000351 /* Initialize the BSS parameter change to 1 */
352 hapd->eht_mld_bss_param_change = 1;
353
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000354 wpa_printf(MSG_DEBUG,
355 "MLD: Set link_id=%u, mld_addr=" MACSTR
356 ", own_addr=" MACSTR,
Sunil Ravi99c035e2024-07-12 01:42:03 +0000357 hapd->mld_link_id, MAC2STR(hapd->mld->mld_addr),
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000358 MAC2STR(hapd->own_addr));
359
360 hostapd_drv_link_add(hapd, hapd->mld_link_id,
361 hapd->own_addr);
362 }
363#endif /* CONFIG_IEEE80211BE */
364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365 return 0;
366}
367
368
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800369/**
370 * hostapd_interface_init - Read configuration file and init BSS data
371 *
372 * This function is used to parse configuration file for a full interface (one
373 * or more BSSes sharing the same radio) and allocate memory for the BSS
Hai Shalom74f70d42019-02-11 14:42:39 -0800374 * interfaces. No actual driver operations are started.
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800375 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376static struct hostapd_iface *
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700377hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378 const char *config_fname, int debug)
379{
380 struct hostapd_iface *iface;
381 int k;
382
Hai Shalomfdcde762020-04-02 11:19:20 -0700383 wpa_printf(MSG_DEBUG, "Configuration file: %s", config_fname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800384 iface = hostapd_init(interfaces, config_fname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 if (!iface)
386 return NULL;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700387
388 if (if_name) {
389 os_strlcpy(iface->conf->bss[0]->iface, if_name,
390 sizeof(iface->conf->bss[0]->iface));
391 }
392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393 iface->interfaces = interfaces;
394
395 for (k = 0; k < debug; k++) {
396 if (iface->bss[0]->conf->logger_stdout_level > 0)
397 iface->bss[0]->conf->logger_stdout_level--;
398 }
399
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800400 if (iface->conf->bss[0]->iface[0] == '\0' &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700401 !hostapd_drv_none(iface->bss[0])) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700402 wpa_printf(MSG_ERROR,
403 "Interface name not specified in %s, nor by '-i' parameter",
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700404 config_fname);
405 hostapd_interface_deinit_free(iface);
406 return NULL;
407 }
408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 return iface;
410}
411
412
413/**
414 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
415 */
416static void handle_term(int sig, void *signal_ctx)
417{
418 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
419 eloop_terminate();
420}
421
422
423#ifndef CONFIG_NATIVE_WINDOWS
424
425static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
426{
427 if (hostapd_reload_config(iface) < 0) {
428 wpa_printf(MSG_WARNING, "Failed to read new configuration "
429 "file - continuing with old.");
430 }
431 return 0;
432}
433
434
435/**
436 * handle_reload - SIGHUP handler to reload configuration
437 */
438static void handle_reload(int sig, void *signal_ctx)
439{
440 struct hapd_interfaces *interfaces = signal_ctx;
441 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
442 sig);
443 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
444}
445
446
447static void handle_dump_state(int sig, void *signal_ctx)
448{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800449 /* Not used anymore - ignore signal */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450}
451#endif /* CONFIG_NATIVE_WINDOWS */
452
453
Jouni Malinen75ecf522011-06-27 15:19:46 -0700454static int hostapd_global_init(struct hapd_interfaces *interfaces,
455 const char *entropy_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800457 int i;
458
459 os_memset(&global, 0, sizeof(global));
460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700461 hostapd_logger_register_cb(hostapd_logger_cb);
462
463 if (eap_server_register_methods()) {
464 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
465 return -1;
466 }
467
468 if (eloop_init()) {
469 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
470 return -1;
471 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700472 interfaces->eloop_initialized = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473
Jouni Malinen75ecf522011-06-27 15:19:46 -0700474 random_init(entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475
476#ifndef CONFIG_NATIVE_WINDOWS
477 eloop_register_signal(SIGHUP, handle_reload, interfaces);
478 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
479#endif /* CONFIG_NATIVE_WINDOWS */
480 eloop_register_signal_terminate(handle_term, interfaces);
481
482#ifndef CONFIG_NATIVE_WINDOWS
483 openlog("hostapd", 0, LOG_DAEMON);
484#endif /* CONFIG_NATIVE_WINDOWS */
485
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800486 for (i = 0; wpa_drivers[i]; i++)
487 global.drv_count++;
488 if (global.drv_count == 0) {
489 wpa_printf(MSG_ERROR, "No drivers enabled");
490 return -1;
491 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700492 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800493 if (global.drv_priv == NULL)
494 return -1;
495
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496 return 0;
497}
498
499
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700500static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800502 int i;
503
504 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
505 if (!global.drv_priv[i])
506 continue;
507 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
508 }
509 os_free(global.drv_priv);
510 global.drv_priv = NULL;
511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700512#ifdef EAP_SERVER_TNC
513 tncs_global_deinit();
514#endif /* EAP_SERVER_TNC */
515
516 random_deinit();
517
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700518 if (eloop_initialized)
519 eloop_destroy();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520
521#ifndef CONFIG_NATIVE_WINDOWS
522 closelog();
523#endif /* CONFIG_NATIVE_WINDOWS */
524
525 eap_server_unregister_methods();
526
527 os_daemonize_terminate(pid_file);
528}
529
530
531static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
532 const char *pid_file)
533{
534#ifdef EAP_SERVER_TNC
535 int tnc = 0;
536 size_t i, k;
537
538 for (i = 0; !tnc && i < ifaces->count; i++) {
539 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
540 if (ifaces->iface[i]->bss[0]->conf->tnc) {
541 tnc++;
542 break;
543 }
544 }
545 }
546
547 if (tnc && tncs_global_init() < 0) {
548 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
549 return -1;
550 }
551#endif /* EAP_SERVER_TNC */
552
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800553 if (daemonize) {
554 if (os_daemonize(pid_file)) {
555 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
556 return -1;
557 }
558 if (eloop_sock_requeue()) {
559 wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
560 strerror(errno));
561 return -1;
562 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 }
564
Veerendranath Jakkam9d0d5212023-02-21 19:55:58 +0530565#ifdef CONFIG_CTRL_IFACE_AIDL
566 if (hostapd_aidl_init(ifaces)) {
567 wpa_printf(MSG_ERROR, "Failed to initialize AIDL interface");
568 return -1;
569 }
570#endif /* CONFIG_CTRL_IFACE_AIDL */
571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572 eloop_run();
573
574 return 0;
575}
576
577
578static void show_version(void)
579{
580 fprintf(stderr,
Hai Shalomfdcde762020-04-02 11:19:20 -0700581 "hostapd v%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 "User space daemon for IEEE 802.11 AP management,\n"
583 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
Hai Shaloma20dcd72022-02-04 13:43:00 -0800584 "Copyright (c) 2002-2022, Jouni Malinen <j@w1.fi> "
Hai Shalomfdcde762020-04-02 11:19:20 -0700585 "and contributors\n",
586 VERSION_STR);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587}
588
589
590static void usage(void)
591{
592 show_version();
593 fprintf(stderr,
594 "\n"
Sunil8cd6f4d2022-06-28 18:40:46 +0000595 "usage: hostapd [-hdBKtvq] [-P <PID file>] [-e <entropy file>] "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700596 "\\\n"
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700597 " [-g <global ctrl_iface>] [-G <group>]\\\n"
598 " [-i <comma-separated list of interface names>]\\\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700599 " <configuration file(s)>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600 "\n"
601 "options:\n"
602 " -h show this usage\n"
603 " -d show more debug messages (-dd for even more)\n"
604 " -B run daemon in the background\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -0700605 " -e entropy file\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700606 " -g global control interface path\n"
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700607 " -G group for control interfaces\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 " -P PID file\n"
609 " -K include key data in debug messages\n"
610#ifdef CONFIG_DEBUG_FILE
611 " -f log output to debug file instead of stdout\n"
612#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800613#ifdef CONFIG_DEBUG_LINUX_TRACING
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800614 " -T record to Linux tracing in addition to logging\n"
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800615 " (records all messages regardless of debug verbosity)\n"
616#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700617 " -i list of interface names to use\n"
Paul Stewart092955c2017-02-06 09:13:09 -0800618#ifdef CONFIG_DEBUG_SYSLOG
619 " -s log output to syslog instead of stdout\n"
620#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800621 " -S start all the interfaces synchronously\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622 " -t include timestamps in some debug messages\n"
Sunil Ravi89eba102022-09-13 21:04:37 -0700623 " -v show hostapd version\n"
624 " -q show less debug messages (-qq for even less)\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625
626 exit(1);
627}
628
629
630static const char * hostapd_msg_ifname_cb(void *ctx)
631{
632 struct hostapd_data *hapd = ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800633 if (hapd && hapd->conf)
634 return hapd->conf->iface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 return NULL;
636}
637
638
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700639static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
640 const char *path)
641{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800642#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700643 char *pos;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800644#endif /* !CONFIG_CTRL_IFACE_UDP */
645
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700646 os_free(interfaces->global_iface_path);
647 interfaces->global_iface_path = os_strdup(path);
648 if (interfaces->global_iface_path == NULL)
649 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800650
651#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700652 pos = os_strrchr(interfaces->global_iface_path, '/');
653 if (pos == NULL) {
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700654 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
655 "file");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700656 os_free(interfaces->global_iface_path);
657 interfaces->global_iface_path = NULL;
658 return -1;
659 }
660
661 *pos = '\0';
662 interfaces->global_iface_name = pos + 1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800663#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700664
665 return 0;
666}
667
668
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700669static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
670 const char *group)
671{
672#ifndef CONFIG_NATIVE_WINDOWS
673 struct group *grp;
674 grp = getgrnam(group);
675 if (grp == NULL) {
676 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
677 return -1;
678 }
679 interfaces->ctrl_iface_group = grp->gr_gid;
680#endif /* CONFIG_NATIVE_WINDOWS */
681 return 0;
682}
683
684
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700685static int hostapd_get_interface_names(char ***if_names,
686 size_t *if_names_size,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800687 char *arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700688{
689 char *if_name, *tmp, **nnames;
690 size_t i;
691
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800692 if (!arg)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700693 return -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800694 if_name = strtok_r(arg, ",", &tmp);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700695
696 while (if_name) {
697 nnames = os_realloc_array(*if_names, 1 + *if_names_size,
698 sizeof(char *));
699 if (!nnames)
700 goto fail;
701 *if_names = nnames;
702
703 (*if_names)[*if_names_size] = os_strdup(if_name);
704 if (!(*if_names)[*if_names_size])
705 goto fail;
706 (*if_names_size)++;
707 if_name = strtok_r(NULL, ",", &tmp);
708 }
709
710 return 0;
711
712fail:
713 for (i = 0; i < *if_names_size; i++)
714 os_free((*if_names)[i]);
715 os_free(*if_names);
716 *if_names = NULL;
717 *if_names_size = 0;
718 return -1;
719}
720
721
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800722#ifdef CONFIG_WPS
723static int gen_uuid(const char *txt_addr)
724{
725 u8 addr[ETH_ALEN];
726 u8 uuid[UUID_LEN];
727 char buf[100];
728
729 if (hwaddr_aton(txt_addr, addr) < 0)
730 return -1;
731
732 uuid_gen_mac_addr(addr, uuid);
733 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
734 return -1;
735
736 printf("%s\n", buf);
737
738 return 0;
739}
740#endif /* CONFIG_WPS */
741
742
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800743#ifndef HOSTAPD_CLEANUP_INTERVAL
744#define HOSTAPD_CLEANUP_INTERVAL 10
745#endif /* HOSTAPD_CLEANUP_INTERVAL */
746
747static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
748{
749 hostapd_periodic_iface(iface);
750 return 0;
751}
752
753
754/* Periodic cleanup tasks */
755static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
756{
757 struct hapd_interfaces *interfaces = eloop_ctx;
758
759 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
760 hostapd_periodic, interfaces, NULL);
761 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
762}
763
764
Sunil Ravi99c035e2024-07-12 01:42:03 +0000765static void hostapd_global_cleanup_mld(struct hapd_interfaces *interfaces)
766{
767#ifdef CONFIG_IEEE80211BE
768 size_t i;
769
770 if (!interfaces || !interfaces->mld)
771 return;
772
773 for (i = 0; i < interfaces->mld_count; i++) {
774 if (!interfaces->mld[i])
775 continue;
776
777 os_free(interfaces->mld[i]);
778 interfaces->mld[i] = NULL;
779 }
780
781 os_free(interfaces->mld);
782 interfaces->mld = NULL;
783 interfaces->mld_count = 0;
784#endif /* CONFIG_IEEE80211BE */
785}
786
787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788int main(int argc, char *argv[])
789{
790 struct hapd_interfaces interfaces;
791 int ret = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800792 size_t i, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793 int c, debug = 0, daemonize = 0;
794 char *pid_file = NULL;
795 const char *log_file = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700796 const char *entropy_file = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800797 char **bss_config = NULL, **tmp_bss;
798 size_t num_bss_configs = 0;
799#ifdef CONFIG_DEBUG_LINUX_TRACING
800 int enable_trace_dbg = 0;
801#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800802 int start_ifaces_in_sync = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700803 char **if_names = NULL;
804 size_t if_names_size = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700805#ifdef CONFIG_DPP
806 struct dpp_global_config dpp_conf;
807#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808
809 if (os_program_init())
810 return -1;
811
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700812 os_memset(&interfaces, 0, sizeof(interfaces));
813 interfaces.reload_config = hostapd_reload_config;
814 interfaces.config_read_cb = hostapd_config_read;
815 interfaces.for_each_interface = hostapd_for_each_interface;
816 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
817 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
818 interfaces.driver_init = hostapd_driver_init;
819 interfaces.global_iface_path = NULL;
820 interfaces.global_iface_name = NULL;
821 interfaces.global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800822 dl_list_init(&interfaces.global_ctrl_dst);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700823#ifdef CONFIG_ETH_P_OUI
824 dl_list_init(&interfaces.eth_p_oui);
825#endif /* CONFIG_ETH_P_OUI */
Roshan Pius3a1667e2018-07-03 15:17:14 -0700826#ifdef CONFIG_DPP
Hai Shalom81f62d82019-07-22 12:10:00 -0700827 os_memset(&dpp_conf, 0, sizeof(dpp_conf));
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700828 dpp_conf.cb_ctx = &interfaces;
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700829#ifdef CONFIG_DPP2
830 dpp_conf.remove_bi = hostapd_dpp_remove_bi;
831#endif /* CONFIG_DPP2 */
Hai Shalom81f62d82019-07-22 12:10:00 -0700832 interfaces.dpp = dpp_global_init(&dpp_conf);
Hai Shalom021b0b52019-04-10 11:17:58 -0700833 if (!interfaces.dpp)
834 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700835#endif /* CONFIG_DPP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837 for (;;) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000838 c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:q");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839 if (c < 0)
840 break;
841 switch (c) {
842 case 'h':
843 usage();
844 break;
845 case 'd':
846 debug++;
847 if (wpa_debug_level > 0)
848 wpa_debug_level--;
849 break;
850 case 'B':
851 daemonize++;
852 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700853 case 'e':
854 entropy_file = optarg;
855 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700856 case 'f':
857 log_file = optarg;
858 break;
859 case 'K':
860 wpa_debug_show_keys++;
861 break;
862 case 'P':
863 os_free(pid_file);
864 pid_file = os_rel2abs_path(optarg);
865 break;
866 case 't':
867 wpa_debug_timestamp++;
868 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800869#ifdef CONFIG_DEBUG_LINUX_TRACING
870 case 'T':
871 enable_trace_dbg = 1;
872 break;
873#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 case 'v':
875 show_version();
876 exit(1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700877 case 'g':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700878 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
879 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700880 break;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700881 case 'G':
Dmitry Shmidt1e78e762013-04-02 11:05:36 -0700882 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
883 return -1;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700884 break;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800885 case 'b':
886 tmp_bss = os_realloc_array(bss_config,
887 num_bss_configs + 1,
888 sizeof(char *));
889 if (tmp_bss == NULL)
890 goto out;
891 bss_config = tmp_bss;
892 bss_config[num_bss_configs++] = optarg;
893 break;
Paul Stewart092955c2017-02-06 09:13:09 -0800894#ifdef CONFIG_DEBUG_SYSLOG
895 case 's':
896 wpa_debug_syslog = 1;
897 break;
898#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800899 case 'S':
900 start_ifaces_in_sync = 1;
901 break;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800902#ifdef CONFIG_WPS
903 case 'u':
904 return gen_uuid(optarg);
905#endif /* CONFIG_WPS */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700906 case 'i':
907 if (hostapd_get_interface_names(&if_names,
908 &if_names_size, optarg))
909 goto out;
910 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000911 case 'q':
912 wpa_debug_level++;
913 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 default:
915 usage();
916 break;
917 }
918 }
919
Gabriel Biren72cf9a52021-06-25 23:29:26 +0000920#ifndef CONFIG_CTRL_IFACE_AIDL
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800921 if (optind == argc && interfaces.global_iface_path == NULL &&
922 num_bss_configs == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 usage();
Roshan Piuscc817562017-12-22 14:45:05 -0800924#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925
926 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
927
928 if (log_file)
929 wpa_debug_open_file(log_file);
Hai Shalomfdcde762020-04-02 11:19:20 -0700930 if (!log_file && !wpa_debug_syslog)
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800931 wpa_debug_setup_stdout();
Paul Stewart092955c2017-02-06 09:13:09 -0800932#ifdef CONFIG_DEBUG_SYSLOG
933 if (wpa_debug_syslog)
934 wpa_debug_open_syslog();
935#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800936#ifdef CONFIG_DEBUG_LINUX_TRACING
937 if (enable_trace_dbg) {
938 int tret = wpa_debug_open_linux_tracing();
939 if (tret) {
940 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
941 return -1;
942 }
943 }
944#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945
946 interfaces.count = argc - optind;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800947 if (interfaces.count || num_bss_configs) {
948 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700949 sizeof(struct hostapd_iface *));
950 if (interfaces.iface == NULL) {
951 wpa_printf(MSG_ERROR, "malloc failed");
952 return -1;
953 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 }
955
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700956 if (hostapd_global_init(&interfaces, entropy_file)) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700957 wpa_printf(MSG_ERROR, "Failed to initialize global context");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 return -1;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800961 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
962 hostapd_periodic, &interfaces, NULL);
963
964 if (fst_global_init()) {
965 wpa_printf(MSG_ERROR,
966 "Failed to initialize global FST context");
967 goto out;
968 }
969
970#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
971 if (!fst_global_add_ctrl(fst_ctrl_cli))
972 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
973#endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
974
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800975 /* Allocate and parse configuration for full interface files */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700977 char *if_name = NULL;
978
979 if (i < if_names_size)
980 if_name = if_names[i];
981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982 interfaces.iface[i] = hostapd_interface_init(&interfaces,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700983 if_name,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984 argv[optind + i],
985 debug);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700986 if (!interfaces.iface[i]) {
987 wpa_printf(MSG_ERROR, "Failed to initialize interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700989 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800990 if (start_ifaces_in_sync)
991 interfaces.iface[i]->need_to_start_in_sync = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 }
993
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800994 /* Allocate and parse configuration for per-BSS files */
995 for (i = 0; i < num_bss_configs; i++) {
996 struct hostapd_iface *iface;
997 char *fname;
998
999 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
1000 fname = os_strchr(bss_config[i], ':');
1001 if (fname == NULL) {
1002 wpa_printf(MSG_ERROR,
1003 "Invalid BSS config identifier '%s'",
1004 bss_config[i]);
1005 goto out;
1006 }
1007 *fname++ = '\0';
1008 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
1009 fname, debug);
1010 if (iface == NULL)
1011 goto out;
1012 for (j = 0; j < interfaces.count; j++) {
1013 if (interfaces.iface[j] == iface)
1014 break;
1015 }
1016 if (j == interfaces.count) {
1017 struct hostapd_iface **tmp;
1018 tmp = os_realloc_array(interfaces.iface,
1019 interfaces.count + 1,
1020 sizeof(struct hostapd_iface *));
1021 if (tmp == NULL) {
1022 hostapd_interface_deinit_free(iface);
1023 goto out;
1024 }
1025 interfaces.iface = tmp;
1026 interfaces.iface[interfaces.count++] = iface;
1027 }
1028 }
1029
1030 /*
1031 * Enable configured interfaces. Depending on channel configuration,
1032 * this may complete full initialization before returning or use a
1033 * callback mechanism to complete setup in case of operations like HT
1034 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
1035 * In such case, the interface will be enabled from eloop context within
1036 * hostapd_global_run().
1037 */
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001038 interfaces.terminate_on_error = interfaces.count;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001039 for (i = 0; i < interfaces.count; i++) {
Hai Shalom74f70d42019-02-11 14:42:39 -08001040 if (hostapd_driver_init(interfaces.iface[i]) ||
1041 hostapd_setup_interface(interfaces.iface[i]))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001042 goto out;
1043 }
1044
Daichi Ueuracaa74a82018-02-14 22:25:39 +09001045 hostapd_global_ctrl_iface_init(&interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001046
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001047 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
1048 wpa_printf(MSG_ERROR, "Failed to start eloop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 goto out;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001050 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051
1052 ret = 0;
1053
1054 out:
Gabriel Biren72cf9a52021-06-25 23:29:26 +00001055#ifdef CONFIG_CTRL_IFACE_AIDL
1056 hostapd_aidl_deinit(&interfaces);
1057#endif /* CONFIG_CTRL_IFACE_AIDL */
Daichi Ueuracaa74a82018-02-14 22:25:39 +09001058 hostapd_global_ctrl_iface_deinit(&interfaces);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 /* Deinitialize all interfaces */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001060 for (i = 0; i < interfaces.count; i++) {
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001061 if (!interfaces.iface[i])
1062 continue;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001063 interfaces.iface[i]->driver_ap_teardown =
1064 !!(interfaces.iface[i]->drv_flags &
1065 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066 hostapd_interface_deinit_free(interfaces.iface[i]);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001067 interfaces.iface[i] = NULL;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001068 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 os_free(interfaces.iface);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001070 interfaces.iface = NULL;
1071 interfaces.count = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072
Sunil Ravi99c035e2024-07-12 01:42:03 +00001073 hostapd_global_cleanup_mld(&interfaces);
1074
Roshan Pius3a1667e2018-07-03 15:17:14 -07001075#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07001076 dpp_global_deinit(interfaces.dpp);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001077#endif /* CONFIG_DPP */
1078
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001079 if (interfaces.eloop_initialized)
1080 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
1081 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082 os_free(pid_file);
1083
Paul Stewart092955c2017-02-06 09:13:09 -08001084 wpa_debug_close_syslog();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 if (log_file)
1086 wpa_debug_close_file();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001087 wpa_debug_close_linux_tracing();
1088
1089 os_free(bss_config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001091 for (i = 0; i < if_names_size; i++)
1092 os_free(if_names[i]);
1093 os_free(if_names);
1094
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001095 fst_global_deinit();
1096
Sunil Ravia04bd252022-05-02 22:54:18 -07001097 crypto_unload();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 os_program_deinit();
1099
1100 return ret;
1101}