blob: 6d7e248d46d3ec9480449831c6076fba6832c63b [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * hostapd / main()
qs.xiongff0dba92023-04-01 18:45:01 +08003 * Copyright (c) 2002-2022, Jouni Malinen <j@w1.fi>
xjb04a4022021-11-25 15:01:52 +08004 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "utils/includes.h"
10#ifndef CONFIG_NATIVE_WINDOWS
11#include <syslog.h>
12#include <grp.h>
13#endif /* CONFIG_NATIVE_WINDOWS */
14
15#include "utils/common.h"
16#include "utils/eloop.h"
17#include "utils/uuid.h"
18#include "crypto/random.h"
19#include "crypto/tls.h"
20#include "common/version.h"
21#include "common/dpp.h"
22#include "drivers/driver.h"
23#include "eap_server/eap.h"
24#include "eap_server/tncs.h"
25#include "ap/hostapd.h"
26#include "ap/ap_config.h"
27#include "ap/ap_drv_ops.h"
28#include "ap/dpp_hostapd.h"
29#include "fst/fst.h"
30#include "config_file.h"
31#include "eap_register.h"
32#include "ctrl_iface.h"
33#ifdef CONFIG_CTRL_IFACE_HIDL
34#include "hidl.h"
35#endif /* CONFIG_CTRL_IFACE_HIDL */
qs.xiongff0dba92023-04-01 18:45:01 +080036#ifdef CONFIG_CTRL_IFACE_AIDL
37#include "aidl.h"
38#endif /* CONFIG_CTRL_IFACE_AIDL */
39
xjb04a4022021-11-25 15:01:52 +080040
41struct hapd_global {
42 void **drv_priv;
43 size_t drv_count;
44};
45
46static struct hapd_global global;
47
48
49#ifndef CONFIG_NO_HOSTAPD_LOGGER
50static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
51 int level, const char *txt, size_t len)
52{
53 struct hostapd_data *hapd = ctx;
54 char *format, *module_str;
55 int maxlen;
56 int conf_syslog_level, conf_stdout_level;
57 unsigned int conf_syslog, conf_stdout;
58
59 maxlen = len + 100;
60 format = os_malloc(maxlen);
61 if (!format)
62 return;
63
64 if (hapd && hapd->conf) {
65 conf_syslog_level = hapd->conf->logger_syslog_level;
66 conf_stdout_level = hapd->conf->logger_stdout_level;
67 conf_syslog = hapd->conf->logger_syslog;
68 conf_stdout = hapd->conf->logger_stdout;
69 } else {
70 conf_syslog_level = conf_stdout_level = 0;
71 conf_syslog = conf_stdout = (unsigned int) -1;
72 }
73
74 switch (module) {
75 case HOSTAPD_MODULE_IEEE80211:
76 module_str = "IEEE 802.11";
77 break;
78 case HOSTAPD_MODULE_IEEE8021X:
79 module_str = "IEEE 802.1X";
80 break;
81 case HOSTAPD_MODULE_RADIUS:
82 module_str = "RADIUS";
83 break;
84 case HOSTAPD_MODULE_WPA:
85 module_str = "WPA";
86 break;
87 case HOSTAPD_MODULE_DRIVER:
88 module_str = "DRIVER";
89 break;
xjb04a4022021-11-25 15:01:52 +080090 case HOSTAPD_MODULE_MLME:
91 module_str = "MLME";
92 break;
93 default:
94 module_str = NULL;
95 break;
96 }
97
98 if (hapd && hapd->conf && addr)
99 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
100 hapd->conf->iface, MAC2STR(addr),
101 module_str ? " " : "", module_str ? module_str : "",
102 txt);
103 else if (hapd && hapd->conf)
104 os_snprintf(format, maxlen, "%s:%s%s %s",
105 hapd->conf->iface, module_str ? " " : "",
106 module_str ? module_str : "", txt);
107 else if (addr)
108 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
109 MAC2STR(addr), module_str ? " " : "",
110 module_str ? module_str : "", txt);
111 else
112 os_snprintf(format, maxlen, "%s%s%s",
113 module_str ? module_str : "",
114 module_str ? ": " : "", txt);
115
116#ifdef CONFIG_DEBUG_SYSLOG
117 if (wpa_debug_syslog)
118 conf_stdout = 0;
119#endif /* CONFIG_DEBUG_SYSLOG */
120 if ((conf_stdout & module) && level >= conf_stdout_level) {
121 wpa_debug_print_timestamp();
122 wpa_printf(MSG_INFO, "%s", format);
123 }
124
125#ifndef CONFIG_NATIVE_WINDOWS
126 if ((conf_syslog & module) && level >= conf_syslog_level) {
127 int priority;
128 switch (level) {
129 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
130 case HOSTAPD_LEVEL_DEBUG:
131 priority = LOG_DEBUG;
132 break;
133 case HOSTAPD_LEVEL_INFO:
134 priority = LOG_INFO;
135 break;
136 case HOSTAPD_LEVEL_NOTICE:
137 priority = LOG_NOTICE;
138 break;
139 case HOSTAPD_LEVEL_WARNING:
140 priority = LOG_WARNING;
141 break;
142 default:
143 priority = LOG_INFO;
144 break;
145 }
146 syslog(priority, "%s", format);
147 }
148#endif /* CONFIG_NATIVE_WINDOWS */
149
150 os_free(format);
151}
152#endif /* CONFIG_NO_HOSTAPD_LOGGER */
153
154
155/**
156 * hostapd_driver_init - Preparate driver interface
157 */
158static int hostapd_driver_init(struct hostapd_iface *iface)
159{
160 struct wpa_init_params params;
161 size_t i;
162 struct hostapd_data *hapd = iface->bss[0];
163 struct hostapd_bss_config *conf = hapd->conf;
164 u8 *b = conf->bssid;
165 struct wpa_driver_capa capa;
166
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
172 /* Initialize the driver interface */
173 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
174 b = NULL;
175
176 os_memset(&params, 0, sizeof(params));
177 for (i = 0; wpa_drivers[i]; i++) {
178 if (wpa_drivers[i] != hapd->driver)
179 continue;
180
181 if (global.drv_priv[i] == NULL &&
182 wpa_drivers[i]->global_init) {
183 global.drv_priv[i] =
184 wpa_drivers[i]->global_init(iface->interfaces);
185 if (global.drv_priv[i] == NULL) {
186 wpa_printf(MSG_ERROR, "Failed to initialize "
187 "driver '%s'",
188 wpa_drivers[i]->name);
189 return -1;
190 }
191 }
192
193 params.global_priv = global.drv_priv[i];
194 break;
195 }
196 params.bssid = b;
197 params.ifname = hapd->conf->iface;
198 params.driver_params = hapd->iconf->driver_params;
199 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
200
201 params.num_bridge = hapd->iface->num_bss;
202 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
203 if (params.bridge == NULL)
204 return -1;
205 for (i = 0; i < hapd->iface->num_bss; i++) {
206 struct hostapd_data *bss = hapd->iface->bss[i];
207 if (bss->conf->bridge[0])
208 params.bridge[i] = bss->conf->bridge;
209 }
210
211 params.own_addr = hapd->own_addr;
212
213 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
214 os_free(params.bridge);
215 if (hapd->drv_priv == NULL) {
216 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
217 hapd->driver->name);
218 hapd->driver = NULL;
219 return -1;
220 }
221
222#ifdef BRCM_SUPP_ACS
223 if (iface->conf->autochannel_enabled) {
224 char buf[64];
225 u8 chan = 0;
226 int ret = 0;
227 wpa_printf(MSG_INFO, "autochannel feature enabled");
228 ret = hapd->driver->driver_cmd(hapd->drv_priv, "HAPD_AUTO_CHANNEL band=2G", buf, sizeof(buf));
229 if (ret > 0) {
230 chan = atoi(buf);
231 /* For B/G mode, valid channel must be between 0 and 14 */
232 if (chan <= 14) {
233 iface->conf->channel = chan;
234 wpa_printf(MSG_INFO, "ACS channel: %d\n", iface->conf->channel);
235 iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
236 } else {
237 wpa_printf(MSG_ERROR, "%s Invalid chan = %d\n",
238 __FUNCTION__, chan);
239 return -1;
240 }
241 } else {
242 wpa_printf(MSG_ERROR, "%s autochannel get failed.",
243 __FUNCTION__);
244 return ret;
245 }
246 }
247#endif /* BRCM_SUPP_ACS */
xjb04a4022021-11-25 15:01:52 +0800248 if (hapd->driver->get_capa &&
249 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
250 struct wowlan_triggers *triggs;
251
252 iface->drv_flags = capa.flags;
qs.xiongff0dba92023-04-01 18:45:01 +0800253 iface->drv_flags2 = capa.flags2;
xjb04a4022021-11-25 15:01:52 +0800254 iface->probe_resp_offloads = capa.probe_resp_offloads;
255 /*
256 * Use default extended capa values from per-radio information
257 */
258 iface->extended_capa = capa.extended_capa;
259 iface->extended_capa_mask = capa.extended_capa_mask;
260 iface->extended_capa_len = capa.extended_capa_len;
261 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
262
263 /*
264 * Override extended capa with per-interface type (AP), if
265 * available from the driver.
266 */
267 hostapd_get_ext_capa(iface);
268
269 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
270 if (triggs && hapd->driver->set_wowlan) {
271 if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
272 wpa_printf(MSG_ERROR, "set_wowlan failed");
273 }
274 os_free(triggs);
275 }
276
277 return 0;
278}
279
280
281/**
282 * hostapd_interface_init - Read configuration file and init BSS data
283 *
284 * This function is used to parse configuration file for a full interface (one
285 * or more BSSes sharing the same radio) and allocate memory for the BSS
286 * interfaces. No actual driver operations are started.
287 */
288static struct hostapd_iface *
289hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
290 const char *config_fname, int debug)
291{
292 struct hostapd_iface *iface;
293 int k;
294
qs.xiongff0dba92023-04-01 18:45:01 +0800295 wpa_printf(MSG_DEBUG, "Configuration file: %s", config_fname);
xjb04a4022021-11-25 15:01:52 +0800296 iface = hostapd_init(interfaces, config_fname);
297 if (!iface)
298 return NULL;
299
300 if (if_name) {
301 os_strlcpy(iface->conf->bss[0]->iface, if_name,
302 sizeof(iface->conf->bss[0]->iface));
303 }
304
305 iface->interfaces = interfaces;
306
307 for (k = 0; k < debug; k++) {
308 if (iface->bss[0]->conf->logger_stdout_level > 0)
309 iface->bss[0]->conf->logger_stdout_level--;
310 }
311
312 if (iface->conf->bss[0]->iface[0] == '\0' &&
313 !hostapd_drv_none(iface->bss[0])) {
314 wpa_printf(MSG_ERROR,
315 "Interface name not specified in %s, nor by '-i' parameter",
316 config_fname);
317 hostapd_interface_deinit_free(iface);
318 return NULL;
319 }
320
321 return iface;
322}
323
324
325/**
326 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
327 */
328static void handle_term(int sig, void *signal_ctx)
329{
330 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
331 eloop_terminate();
332}
333
334
335#ifndef CONFIG_NATIVE_WINDOWS
336
337static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
338{
339 if (hostapd_reload_config(iface) < 0) {
340 wpa_printf(MSG_WARNING, "Failed to read new configuration "
341 "file - continuing with old.");
342 }
343 return 0;
344}
345
346
347/**
348 * handle_reload - SIGHUP handler to reload configuration
349 */
350static void handle_reload(int sig, void *signal_ctx)
351{
352 struct hapd_interfaces *interfaces = signal_ctx;
353 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
354 sig);
355 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
356}
357
358
359static void handle_dump_state(int sig, void *signal_ctx)
360{
361 /* Not used anymore - ignore signal */
362}
363#endif /* CONFIG_NATIVE_WINDOWS */
364
365
366static int hostapd_global_init(struct hapd_interfaces *interfaces,
367 const char *entropy_file)
368{
369 int i;
370
371 os_memset(&global, 0, sizeof(global));
372
373 hostapd_logger_register_cb(hostapd_logger_cb);
374
375 if (eap_server_register_methods()) {
376 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
377 return -1;
378 }
379
380 if (eloop_init()) {
381 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
382 return -1;
383 }
384 interfaces->eloop_initialized = 1;
385
386 random_init(entropy_file);
387
388#ifndef CONFIG_NATIVE_WINDOWS
389 eloop_register_signal(SIGHUP, handle_reload, interfaces);
390 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
391#endif /* CONFIG_NATIVE_WINDOWS */
392 eloop_register_signal_terminate(handle_term, interfaces);
393
394#ifndef CONFIG_NATIVE_WINDOWS
395 openlog("hostapd", 0, LOG_DAEMON);
396#endif /* CONFIG_NATIVE_WINDOWS */
397
398 for (i = 0; wpa_drivers[i]; i++)
399 global.drv_count++;
400 if (global.drv_count == 0) {
401 wpa_printf(MSG_ERROR, "No drivers enabled");
402 return -1;
403 }
404 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
405 if (global.drv_priv == NULL)
406 return -1;
407
408 return 0;
409}
410
411
412static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
413{
414 int i;
415
416 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
417 if (!global.drv_priv[i])
418 continue;
419 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
420 }
421 os_free(global.drv_priv);
422 global.drv_priv = NULL;
423
424#ifdef EAP_SERVER_TNC
425 tncs_global_deinit();
426#endif /* EAP_SERVER_TNC */
427
428 random_deinit();
429
430 if (eloop_initialized)
431 eloop_destroy();
432
433#ifndef CONFIG_NATIVE_WINDOWS
434 closelog();
435#endif /* CONFIG_NATIVE_WINDOWS */
436
437 eap_server_unregister_methods();
438
439 os_daemonize_terminate(pid_file);
440}
441
442
443static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
444 const char *pid_file)
445{
446#ifdef EAP_SERVER_TNC
447 int tnc = 0;
448 size_t i, k;
449
450 for (i = 0; !tnc && i < ifaces->count; i++) {
451 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
452 if (ifaces->iface[i]->bss[0]->conf->tnc) {
453 tnc++;
454 break;
455 }
456 }
457 }
458
459 if (tnc && tncs_global_init() < 0) {
460 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
461 return -1;
462 }
463#endif /* EAP_SERVER_TNC */
464
465 if (daemonize) {
466 if (os_daemonize(pid_file)) {
467 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
468 return -1;
469 }
470 if (eloop_sock_requeue()) {
471 wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
472 strerror(errno));
473 return -1;
474 }
475 }
476
477 eloop_run();
478
479 return 0;
480}
481
482
483static void show_version(void)
484{
485 fprintf(stderr,
qs.xiongff0dba92023-04-01 18:45:01 +0800486 "hostapd v%s\n"
xjb04a4022021-11-25 15:01:52 +0800487 "User space daemon for IEEE 802.11 AP management,\n"
488 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
qs.xiongff0dba92023-04-01 18:45:01 +0800489 "Copyright (c) 2002-2022, Jouni Malinen <j@w1.fi> "
490 "and contributors\n",
491 VERSION_STR);
xjb04a4022021-11-25 15:01:52 +0800492}
493
494
495static void usage(void)
496{
497 show_version();
498 fprintf(stderr,
499 "\n"
500 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
501 "\\\n"
502 " [-g <global ctrl_iface>] [-G <group>]\\\n"
503 " [-i <comma-separated list of interface names>]\\\n"
504 " <configuration file(s)>\n"
505 "\n"
506 "options:\n"
507 " -h show this usage\n"
508 " -d show more debug messages (-dd for even more)\n"
509 " -B run daemon in the background\n"
510 " -e entropy file\n"
511 " -g global control interface path\n"
512 " -G group for control interfaces\n"
513 " -P PID file\n"
514 " -K include key data in debug messages\n"
515#ifdef CONFIG_DEBUG_FILE
516 " -f log output to debug file instead of stdout\n"
517#endif /* CONFIG_DEBUG_FILE */
518#ifdef CONFIG_DEBUG_LINUX_TRACING
519 " -T record to Linux tracing in addition to logging\n"
520 " (records all messages regardless of debug verbosity)\n"
521#endif /* CONFIG_DEBUG_LINUX_TRACING */
522 " -i list of interface names to use\n"
523#ifdef CONFIG_DEBUG_SYSLOG
524 " -s log output to syslog instead of stdout\n"
525#endif /* CONFIG_DEBUG_SYSLOG */
526 " -S start all the interfaces synchronously\n"
527 " -t include timestamps in some debug messages\n"
528 " -v show hostapd version\n");
529
530 exit(1);
531}
532
533
534static const char * hostapd_msg_ifname_cb(void *ctx)
535{
536 struct hostapd_data *hapd = ctx;
537 if (hapd && hapd->conf)
538 return hapd->conf->iface;
539 return NULL;
540}
541
542
543static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
544 const char *path)
545{
546#ifndef CONFIG_CTRL_IFACE_UDP
547 char *pos;
548#endif /* !CONFIG_CTRL_IFACE_UDP */
549
550 os_free(interfaces->global_iface_path);
551 interfaces->global_iface_path = os_strdup(path);
552 if (interfaces->global_iface_path == NULL)
553 return -1;
554
555#ifndef CONFIG_CTRL_IFACE_UDP
556 pos = os_strrchr(interfaces->global_iface_path, '/');
557 if (pos == NULL) {
558 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
559 "file");
560 os_free(interfaces->global_iface_path);
561 interfaces->global_iface_path = NULL;
562 return -1;
563 }
564
565 *pos = '\0';
566 interfaces->global_iface_name = pos + 1;
567#endif /* !CONFIG_CTRL_IFACE_UDP */
568
569 return 0;
570}
571
572
573static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
574 const char *group)
575{
576#ifndef CONFIG_NATIVE_WINDOWS
577 struct group *grp;
578 grp = getgrnam(group);
579 if (grp == NULL) {
580 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
581 return -1;
582 }
583 interfaces->ctrl_iface_group = grp->gr_gid;
584#endif /* CONFIG_NATIVE_WINDOWS */
585 return 0;
586}
587
588
589static int hostapd_get_interface_names(char ***if_names,
590 size_t *if_names_size,
591 char *arg)
592{
593 char *if_name, *tmp, **nnames;
594 size_t i;
595
596 if (!arg)
597 return -1;
598 if_name = strtok_r(arg, ",", &tmp);
599
600 while (if_name) {
601 nnames = os_realloc_array(*if_names, 1 + *if_names_size,
602 sizeof(char *));
603 if (!nnames)
604 goto fail;
605 *if_names = nnames;
606
607 (*if_names)[*if_names_size] = os_strdup(if_name);
608 if (!(*if_names)[*if_names_size])
609 goto fail;
610 (*if_names_size)++;
611 if_name = strtok_r(NULL, ",", &tmp);
612 }
613
614 return 0;
615
616fail:
617 for (i = 0; i < *if_names_size; i++)
618 os_free((*if_names)[i]);
619 os_free(*if_names);
620 *if_names = NULL;
621 *if_names_size = 0;
622 return -1;
623}
624
625
626#ifdef CONFIG_WPS
627static int gen_uuid(const char *txt_addr)
628{
629 u8 addr[ETH_ALEN];
630 u8 uuid[UUID_LEN];
631 char buf[100];
632
633 if (hwaddr_aton(txt_addr, addr) < 0)
634 return -1;
635
636 uuid_gen_mac_addr(addr, uuid);
637 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
638 return -1;
639
640 printf("%s\n", buf);
641
642 return 0;
643}
644#endif /* CONFIG_WPS */
645
646
647#ifndef HOSTAPD_CLEANUP_INTERVAL
648#define HOSTAPD_CLEANUP_INTERVAL 10
649#endif /* HOSTAPD_CLEANUP_INTERVAL */
650
651static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
652{
653 hostapd_periodic_iface(iface);
654 return 0;
655}
656
657
658/* Periodic cleanup tasks */
659static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
660{
661 struct hapd_interfaces *interfaces = eloop_ctx;
662
663 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
664 hostapd_periodic, interfaces, NULL);
665 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
666}
667
668
669int main(int argc, char *argv[])
670{
671 struct hapd_interfaces interfaces;
672 int ret = 1;
673 size_t i, j;
674 int c, debug = 0, daemonize = 0;
675 char *pid_file = NULL;
676 const char *log_file = NULL;
677 const char *entropy_file = NULL;
678 char **bss_config = NULL, **tmp_bss;
679 size_t num_bss_configs = 0;
680#ifdef CONFIG_DEBUG_LINUX_TRACING
681 int enable_trace_dbg = 0;
682#endif /* CONFIG_DEBUG_LINUX_TRACING */
683 int start_ifaces_in_sync = 0;
684 char **if_names = NULL;
685 size_t if_names_size = 0;
686#ifdef CONFIG_DPP
687 struct dpp_global_config dpp_conf;
688#endif /* CONFIG_DPP */
689
690 if (os_program_init())
691 return -1;
692
693 os_memset(&interfaces, 0, sizeof(interfaces));
694 interfaces.reload_config = hostapd_reload_config;
695 interfaces.config_read_cb = hostapd_config_read;
696 interfaces.for_each_interface = hostapd_for_each_interface;
697 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
698 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
699 interfaces.driver_init = hostapd_driver_init;
700 interfaces.global_iface_path = NULL;
701 interfaces.global_iface_name = NULL;
702 interfaces.global_ctrl_sock = -1;
703 dl_list_init(&interfaces.global_ctrl_dst);
704#ifdef CONFIG_ETH_P_OUI
705 dl_list_init(&interfaces.eth_p_oui);
706#endif /* CONFIG_ETH_P_OUI */
707#ifdef CONFIG_DPP
708 os_memset(&dpp_conf, 0, sizeof(dpp_conf));
qs.xiongff0dba92023-04-01 18:45:01 +0800709 dpp_conf.cb_ctx = &interfaces;
710#ifdef CONFIG_DPP2
711 dpp_conf.remove_bi = hostapd_dpp_remove_bi;
712#endif /* CONFIG_DPP2 */
xjb04a4022021-11-25 15:01:52 +0800713 interfaces.dpp = dpp_global_init(&dpp_conf);
714 if (!interfaces.dpp)
715 return -1;
716#endif /* CONFIG_DPP */
717
718 for (;;) {
719 c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:");
720 if (c < 0)
721 break;
722 switch (c) {
723 case 'h':
724 usage();
725 break;
726 case 'd':
727 debug++;
728 if (wpa_debug_level > 0)
729 wpa_debug_level--;
730 break;
731 case 'B':
732 daemonize++;
733 break;
734 case 'e':
735 entropy_file = optarg;
736 break;
737 case 'f':
738 log_file = optarg;
739 break;
740 case 'K':
741 wpa_debug_show_keys++;
742 break;
743 case 'P':
744 os_free(pid_file);
745 pid_file = os_rel2abs_path(optarg);
746 break;
747 case 't':
748 wpa_debug_timestamp++;
749 break;
750#ifdef CONFIG_DEBUG_LINUX_TRACING
751 case 'T':
752 enable_trace_dbg = 1;
753 break;
754#endif /* CONFIG_DEBUG_LINUX_TRACING */
755 case 'v':
756 show_version();
757 exit(1);
758 break;
759 case 'g':
760 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
761 return -1;
762 break;
763 case 'G':
764 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
765 return -1;
766 break;
767 case 'b':
768 tmp_bss = os_realloc_array(bss_config,
769 num_bss_configs + 1,
770 sizeof(char *));
771 if (tmp_bss == NULL)
772 goto out;
773 bss_config = tmp_bss;
774 bss_config[num_bss_configs++] = optarg;
775 break;
776#ifdef CONFIG_DEBUG_SYSLOG
777 case 's':
778 wpa_debug_syslog = 1;
779 break;
780#endif /* CONFIG_DEBUG_SYSLOG */
781 case 'S':
782 start_ifaces_in_sync = 1;
783 break;
784#ifdef CONFIG_WPS
785 case 'u':
786 return gen_uuid(optarg);
787#endif /* CONFIG_WPS */
788 case 'i':
789 if (hostapd_get_interface_names(&if_names,
790 &if_names_size, optarg))
791 goto out;
792 break;
793 default:
794 usage();
795 break;
796 }
797 }
798
799#ifndef CONFIG_CTRL_IFACE_HIDL
qs.xiongff0dba92023-04-01 18:45:01 +0800800#ifndef CONFIG_CTRL_IFACE_AIDL
xjb04a4022021-11-25 15:01:52 +0800801 if (optind == argc && interfaces.global_iface_path == NULL &&
802 num_bss_configs == 0)
803 usage();
qs.xiongff0dba92023-04-01 18:45:01 +0800804#endif /* CONFIG_CTRL_IFACE_AIDL */
805#endif /* CONFIG_CTRL_IFACE_HIDL */
xjb04a4022021-11-25 15:01:52 +0800806
807 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
808
809 if (log_file)
810 wpa_debug_open_file(log_file);
qs.xiongff0dba92023-04-01 18:45:01 +0800811 if (!log_file && !wpa_debug_syslog)
xjb04a4022021-11-25 15:01:52 +0800812 wpa_debug_setup_stdout();
813#ifdef CONFIG_DEBUG_SYSLOG
814 if (wpa_debug_syslog)
815 wpa_debug_open_syslog();
816#endif /* CONFIG_DEBUG_SYSLOG */
817#ifdef CONFIG_DEBUG_LINUX_TRACING
818 if (enable_trace_dbg) {
819 int tret = wpa_debug_open_linux_tracing();
820 if (tret) {
821 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
822 return -1;
823 }
824 }
825#endif /* CONFIG_DEBUG_LINUX_TRACING */
826
827 interfaces.count = argc - optind;
828 if (interfaces.count || num_bss_configs) {
829 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
830 sizeof(struct hostapd_iface *));
831 if (interfaces.iface == NULL) {
832 wpa_printf(MSG_ERROR, "malloc failed");
833 return -1;
834 }
835 }
836
837 if (hostapd_global_init(&interfaces, entropy_file)) {
838 wpa_printf(MSG_ERROR, "Failed to initialize global context");
839 return -1;
840 }
841
842 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
843 hostapd_periodic, &interfaces, NULL);
844
845 if (fst_global_init()) {
846 wpa_printf(MSG_ERROR,
847 "Failed to initialize global FST context");
848 goto out;
849 }
850
851#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
852 if (!fst_global_add_ctrl(fst_ctrl_cli))
853 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
854#endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
855
856 /* Allocate and parse configuration for full interface files */
857 for (i = 0; i < interfaces.count; i++) {
858 char *if_name = NULL;
859
860 if (i < if_names_size)
861 if_name = if_names[i];
862
863 interfaces.iface[i] = hostapd_interface_init(&interfaces,
864 if_name,
865 argv[optind + i],
866 debug);
867 if (!interfaces.iface[i]) {
868 wpa_printf(MSG_ERROR, "Failed to initialize interface");
869 goto out;
870 }
871 if (start_ifaces_in_sync)
872 interfaces.iface[i]->need_to_start_in_sync = 1;
873 }
874
875 /* Allocate and parse configuration for per-BSS files */
876 for (i = 0; i < num_bss_configs; i++) {
877 struct hostapd_iface *iface;
878 char *fname;
879
880 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
881 fname = os_strchr(bss_config[i], ':');
882 if (fname == NULL) {
883 wpa_printf(MSG_ERROR,
884 "Invalid BSS config identifier '%s'",
885 bss_config[i]);
886 goto out;
887 }
888 *fname++ = '\0';
889 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
890 fname, debug);
891 if (iface == NULL)
892 goto out;
893 for (j = 0; j < interfaces.count; j++) {
894 if (interfaces.iface[j] == iface)
895 break;
896 }
897 if (j == interfaces.count) {
898 struct hostapd_iface **tmp;
899 tmp = os_realloc_array(interfaces.iface,
900 interfaces.count + 1,
901 sizeof(struct hostapd_iface *));
902 if (tmp == NULL) {
903 hostapd_interface_deinit_free(iface);
904 goto out;
905 }
906 interfaces.iface = tmp;
907 interfaces.iface[interfaces.count++] = iface;
908 }
909 }
910
911 /*
912 * Enable configured interfaces. Depending on channel configuration,
913 * this may complete full initialization before returning or use a
914 * callback mechanism to complete setup in case of operations like HT
915 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
916 * In such case, the interface will be enabled from eloop context within
917 * hostapd_global_run().
918 */
919 interfaces.terminate_on_error = interfaces.count;
920 for (i = 0; i < interfaces.count; i++) {
921 if (hostapd_driver_init(interfaces.iface[i]) ||
922 hostapd_setup_interface(interfaces.iface[i]))
923 goto out;
924 }
925
926#ifdef CONFIG_CTRL_IFACE_HIDL
927 if (hostapd_hidl_init(&interfaces)) {
928 wpa_printf(MSG_ERROR, "Failed to initialize HIDL interface");
929 goto out;
930 }
931#endif /* CONFIG_CTRL_IFACE_HIDL */
qs.xiongff0dba92023-04-01 18:45:01 +0800932#ifdef CONFIG_CTRL_IFACE_AIDL
933 if (hostapd_aidl_init(&interfaces)) {
934 wpa_printf(MSG_ERROR, "Failed to initialize AIDL interface");
935 goto out;
936 }
937#endif /* CONFIG_CTRL_IFACE_AIDL */
xjb04a4022021-11-25 15:01:52 +0800938 hostapd_global_ctrl_iface_init(&interfaces);
939
940 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
941 wpa_printf(MSG_ERROR, "Failed to start eloop");
942 goto out;
943 }
944
945 ret = 0;
946
947 out:
948#ifdef CONFIG_CTRL_IFACE_HIDL
949 hostapd_hidl_deinit(&interfaces);
950#endif /* CONFIG_CTRL_IFACE_HIDL */
qs.xiongff0dba92023-04-01 18:45:01 +0800951#ifdef CONFIG_CTRL_IFACE_AIDL
952 hostapd_aidl_deinit(&interfaces);
953#endif /* CONFIG_CTRL_IFACE_AIDL */
xjb04a4022021-11-25 15:01:52 +0800954 hostapd_global_ctrl_iface_deinit(&interfaces);
955 /* Deinitialize all interfaces */
956 for (i = 0; i < interfaces.count; i++) {
957 if (!interfaces.iface[i])
958 continue;
959 interfaces.iface[i]->driver_ap_teardown =
960 !!(interfaces.iface[i]->drv_flags &
961 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
962 hostapd_interface_deinit_free(interfaces.iface[i]);
qs.xiongff0dba92023-04-01 18:45:01 +0800963 interfaces.iface[i] = NULL;
xjb04a4022021-11-25 15:01:52 +0800964 }
965 os_free(interfaces.iface);
qs.xiongff0dba92023-04-01 18:45:01 +0800966 interfaces.iface = NULL;
967 interfaces.count = 0;
xjb04a4022021-11-25 15:01:52 +0800968
969#ifdef CONFIG_DPP
970 dpp_global_deinit(interfaces.dpp);
971#endif /* CONFIG_DPP */
972
973 if (interfaces.eloop_initialized)
974 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
975 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
976 os_free(pid_file);
977
978 wpa_debug_close_syslog();
979 if (log_file)
980 wpa_debug_close_file();
981 wpa_debug_close_linux_tracing();
982
983 os_free(bss_config);
984
985 for (i = 0; i < if_names_size; i++)
986 os_free(if_names[i]);
987 os_free(if_names);
988
989 fst_global_deinit();
990
991 os_program_deinit();
992
993 return ret;
994}