blob: 8f8a0ce579e11c98095ccd35d234f0a2f7d10477 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * hostapd / main()
3 * Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "utils/includes.h"
10#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 */
36
37struct hapd_global {
38 void **drv_priv;
39 size_t drv_count;
40};
41
42static struct hapd_global global;
43
44
45#ifndef CONFIG_NO_HOSTAPD_LOGGER
46static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
47 int level, const char *txt, size_t len)
48{
49 struct hostapd_data *hapd = ctx;
50 char *format, *module_str;
51 int maxlen;
52 int conf_syslog_level, conf_stdout_level;
53 unsigned int conf_syslog, conf_stdout;
54
55 maxlen = len + 100;
56 format = os_malloc(maxlen);
57 if (!format)
58 return;
59
60 if (hapd && hapd->conf) {
61 conf_syslog_level = hapd->conf->logger_syslog_level;
62 conf_stdout_level = hapd->conf->logger_stdout_level;
63 conf_syslog = hapd->conf->logger_syslog;
64 conf_stdout = hapd->conf->logger_stdout;
65 } else {
66 conf_syslog_level = conf_stdout_level = 0;
67 conf_syslog = conf_stdout = (unsigned int) -1;
68 }
69
70 switch (module) {
71 case HOSTAPD_MODULE_IEEE80211:
72 module_str = "IEEE 802.11";
73 break;
74 case HOSTAPD_MODULE_IEEE8021X:
75 module_str = "IEEE 802.1X";
76 break;
77 case HOSTAPD_MODULE_RADIUS:
78 module_str = "RADIUS";
79 break;
80 case HOSTAPD_MODULE_WPA:
81 module_str = "WPA";
82 break;
83 case HOSTAPD_MODULE_DRIVER:
84 module_str = "DRIVER";
85 break;
86 case HOSTAPD_MODULE_IAPP:
87 module_str = "IAPP";
88 break;
89 case HOSTAPD_MODULE_MLME:
90 module_str = "MLME";
91 break;
92 default:
93 module_str = NULL;
94 break;
95 }
96
97 if (hapd && hapd->conf && addr)
98 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
99 hapd->conf->iface, MAC2STR(addr),
100 module_str ? " " : "", module_str ? module_str : "",
101 txt);
102 else if (hapd && hapd->conf)
103 os_snprintf(format, maxlen, "%s:%s%s %s",
104 hapd->conf->iface, module_str ? " " : "",
105 module_str ? module_str : "", txt);
106 else if (addr)
107 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
108 MAC2STR(addr), module_str ? " " : "",
109 module_str ? module_str : "", txt);
110 else
111 os_snprintf(format, maxlen, "%s%s%s",
112 module_str ? module_str : "",
113 module_str ? ": " : "", txt);
114
115#ifdef CONFIG_DEBUG_SYSLOG
116 if (wpa_debug_syslog)
117 conf_stdout = 0;
118#endif /* CONFIG_DEBUG_SYSLOG */
119 if ((conf_stdout & module) && level >= conf_stdout_level) {
120 wpa_debug_print_timestamp();
121 wpa_printf(MSG_INFO, "%s", format);
122 }
123
124#ifndef CONFIG_NATIVE_WINDOWS
125 if ((conf_syslog & module) && level >= conf_syslog_level) {
126 int priority;
127 switch (level) {
128 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
129 case HOSTAPD_LEVEL_DEBUG:
130 priority = LOG_DEBUG;
131 break;
132 case HOSTAPD_LEVEL_INFO:
133 priority = LOG_INFO;
134 break;
135 case HOSTAPD_LEVEL_NOTICE:
136 priority = LOG_NOTICE;
137 break;
138 case HOSTAPD_LEVEL_WARNING:
139 priority = LOG_WARNING;
140 break;
141 default:
142 priority = LOG_INFO;
143 break;
144 }
145 syslog(priority, "%s", format);
146 }
147#endif /* CONFIG_NATIVE_WINDOWS */
148
149 os_free(format);
150}
151#endif /* CONFIG_NO_HOSTAPD_LOGGER */
152
153
154/**
155 * hostapd_driver_init - Preparate driver interface
156 */
157static int hostapd_driver_init(struct hostapd_iface *iface)
158{
159 struct wpa_init_params params;
160 size_t i;
161 struct hostapd_data *hapd = iface->bss[0];
162 struct hostapd_bss_config *conf = hapd->conf;
163 u8 *b = conf->bssid;
164 struct wpa_driver_capa capa;
165
166 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
167 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
168 return -1;
169 }
170
171 /* Initialize the driver interface */
172 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
173 b = NULL;
174
175 os_memset(&params, 0, sizeof(params));
176 for (i = 0; wpa_drivers[i]; i++) {
177 if (wpa_drivers[i] != hapd->driver)
178 continue;
179
180 if (global.drv_priv[i] == NULL &&
181 wpa_drivers[i]->global_init) {
182 global.drv_priv[i] =
183 wpa_drivers[i]->global_init(iface->interfaces);
184 if (global.drv_priv[i] == NULL) {
185 wpa_printf(MSG_ERROR, "Failed to initialize "
186 "driver '%s'",
187 wpa_drivers[i]->name);
188 return -1;
189 }
190 }
191
192 params.global_priv = global.drv_priv[i];
193 break;
194 }
195 params.bssid = b;
196 params.ifname = hapd->conf->iface;
197 params.driver_params = hapd->iconf->driver_params;
198 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
199
200 params.num_bridge = hapd->iface->num_bss;
201 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
202 if (params.bridge == NULL)
203 return -1;
204 for (i = 0; i < hapd->iface->num_bss; i++) {
205 struct hostapd_data *bss = hapd->iface->bss[i];
206 if (bss->conf->bridge[0])
207 params.bridge[i] = bss->conf->bridge;
208 }
209
210 params.own_addr = hapd->own_addr;
211
212 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
213 os_free(params.bridge);
214 if (hapd->drv_priv == NULL) {
215 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
216 hapd->driver->name);
217 hapd->driver = NULL;
218 return -1;
219 }
220
221#ifdef BRCM_SUPP_ACS
222 if (iface->conf->autochannel_enabled) {
223 char buf[64];
224 u8 chan = 0;
225 int ret = 0;
226 wpa_printf(MSG_INFO, "autochannel feature enabled");
227 ret = hapd->driver->driver_cmd(hapd->drv_priv, "HAPD_AUTO_CHANNEL band=2G", buf, sizeof(buf));
228 if (ret > 0) {
229 chan = atoi(buf);
230 /* For B/G mode, valid channel must be between 0 and 14 */
231 if (chan <= 14) {
232 iface->conf->channel = chan;
233 wpa_printf(MSG_INFO, "ACS channel: %d\n", iface->conf->channel);
234 iface->conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
235 } else {
236 wpa_printf(MSG_ERROR, "%s Invalid chan = %d\n",
237 __FUNCTION__, chan);
238 return -1;
239 }
240 } else {
241 wpa_printf(MSG_ERROR, "%s autochannel get failed.",
242 __FUNCTION__);
243 return ret;
244 }
245 }
246#endif /* BRCM_SUPP_ACS */
247
248 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;
253 iface->smps_modes = capa.smps_modes;
254 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
295 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
296 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,
486 "hostapd v" VERSION_STR "\n"
487 "User space daemon for IEEE 802.11 AP management,\n"
488 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
489 "Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi> "
490 "and contributors\n");
491}
492
493
494static void usage(void)
495{
496 show_version();
497 fprintf(stderr,
498 "\n"
499 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
500 "\\\n"
501 " [-g <global ctrl_iface>] [-G <group>]\\\n"
502 " [-i <comma-separated list of interface names>]\\\n"
503 " <configuration file(s)>\n"
504 "\n"
505 "options:\n"
506 " -h show this usage\n"
507 " -d show more debug messages (-dd for even more)\n"
508 " -B run daemon in the background\n"
509 " -e entropy file\n"
510 " -g global control interface path\n"
511 " -G group for control interfaces\n"
512 " -P PID file\n"
513 " -K include key data in debug messages\n"
514#ifdef CONFIG_DEBUG_FILE
515 " -f log output to debug file instead of stdout\n"
516#endif /* CONFIG_DEBUG_FILE */
517#ifdef CONFIG_DEBUG_LINUX_TRACING
518 " -T record to Linux tracing in addition to logging\n"
519 " (records all messages regardless of debug verbosity)\n"
520#endif /* CONFIG_DEBUG_LINUX_TRACING */
521 " -i list of interface names to use\n"
522#ifdef CONFIG_DEBUG_SYSLOG
523 " -s log output to syslog instead of stdout\n"
524#endif /* CONFIG_DEBUG_SYSLOG */
525 " -S start all the interfaces synchronously\n"
526 " -t include timestamps in some debug messages\n"
527 " -v show hostapd version\n");
528
529 exit(1);
530}
531
532
533static const char * hostapd_msg_ifname_cb(void *ctx)
534{
535 struct hostapd_data *hapd = ctx;
536 if (hapd && hapd->conf)
537 return hapd->conf->iface;
538 return NULL;
539}
540
541
542static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
543 const char *path)
544{
545#ifndef CONFIG_CTRL_IFACE_UDP
546 char *pos;
547#endif /* !CONFIG_CTRL_IFACE_UDP */
548
549 os_free(interfaces->global_iface_path);
550 interfaces->global_iface_path = os_strdup(path);
551 if (interfaces->global_iface_path == NULL)
552 return -1;
553
554#ifndef CONFIG_CTRL_IFACE_UDP
555 pos = os_strrchr(interfaces->global_iface_path, '/');
556 if (pos == NULL) {
557 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
558 "file");
559 os_free(interfaces->global_iface_path);
560 interfaces->global_iface_path = NULL;
561 return -1;
562 }
563
564 *pos = '\0';
565 interfaces->global_iface_name = pos + 1;
566#endif /* !CONFIG_CTRL_IFACE_UDP */
567
568 return 0;
569}
570
571
572static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
573 const char *group)
574{
575#ifndef CONFIG_NATIVE_WINDOWS
576 struct group *grp;
577 grp = getgrnam(group);
578 if (grp == NULL) {
579 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
580 return -1;
581 }
582 interfaces->ctrl_iface_group = grp->gr_gid;
583#endif /* CONFIG_NATIVE_WINDOWS */
584 return 0;
585}
586
587
588static int hostapd_get_interface_names(char ***if_names,
589 size_t *if_names_size,
590 char *arg)
591{
592 char *if_name, *tmp, **nnames;
593 size_t i;
594
595 if (!arg)
596 return -1;
597 if_name = strtok_r(arg, ",", &tmp);
598
599 while (if_name) {
600 nnames = os_realloc_array(*if_names, 1 + *if_names_size,
601 sizeof(char *));
602 if (!nnames)
603 goto fail;
604 *if_names = nnames;
605
606 (*if_names)[*if_names_size] = os_strdup(if_name);
607 if (!(*if_names)[*if_names_size])
608 goto fail;
609 (*if_names_size)++;
610 if_name = strtok_r(NULL, ",", &tmp);
611 }
612
613 return 0;
614
615fail:
616 for (i = 0; i < *if_names_size; i++)
617 os_free((*if_names)[i]);
618 os_free(*if_names);
619 *if_names = NULL;
620 *if_names_size = 0;
621 return -1;
622}
623
624
625#ifdef CONFIG_WPS
626static int gen_uuid(const char *txt_addr)
627{
628 u8 addr[ETH_ALEN];
629 u8 uuid[UUID_LEN];
630 char buf[100];
631
632 if (hwaddr_aton(txt_addr, addr) < 0)
633 return -1;
634
635 uuid_gen_mac_addr(addr, uuid);
636 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
637 return -1;
638
639 printf("%s\n", buf);
640
641 return 0;
642}
643#endif /* CONFIG_WPS */
644
645
646#ifndef HOSTAPD_CLEANUP_INTERVAL
647#define HOSTAPD_CLEANUP_INTERVAL 10
648#endif /* HOSTAPD_CLEANUP_INTERVAL */
649
650static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
651{
652 hostapd_periodic_iface(iface);
653 return 0;
654}
655
656
657/* Periodic cleanup tasks */
658static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
659{
660 struct hapd_interfaces *interfaces = eloop_ctx;
661
662 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
663 hostapd_periodic, interfaces, NULL);
664 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
665}
666
667
668int main(int argc, char *argv[])
669{
670 struct hapd_interfaces interfaces;
671 int ret = 1;
672 size_t i, j;
673 int c, debug = 0, daemonize = 0;
674 char *pid_file = NULL;
675 const char *log_file = NULL;
676 const char *entropy_file = NULL;
677 char **bss_config = NULL, **tmp_bss;
678 size_t num_bss_configs = 0;
679#ifdef CONFIG_DEBUG_LINUX_TRACING
680 int enable_trace_dbg = 0;
681#endif /* CONFIG_DEBUG_LINUX_TRACING */
682 int start_ifaces_in_sync = 0;
683 char **if_names = NULL;
684 size_t if_names_size = 0;
685#ifdef CONFIG_DPP
686 struct dpp_global_config dpp_conf;
687#endif /* CONFIG_DPP */
688
689 if (os_program_init())
690 return -1;
691
692 os_memset(&interfaces, 0, sizeof(interfaces));
693 interfaces.reload_config = hostapd_reload_config;
694 interfaces.config_read_cb = hostapd_config_read;
695 interfaces.for_each_interface = hostapd_for_each_interface;
696 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
697 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
698 interfaces.driver_init = hostapd_driver_init;
699 interfaces.global_iface_path = NULL;
700 interfaces.global_iface_name = NULL;
701 interfaces.global_ctrl_sock = -1;
702 dl_list_init(&interfaces.global_ctrl_dst);
703#ifdef CONFIG_ETH_P_OUI
704 dl_list_init(&interfaces.eth_p_oui);
705#endif /* CONFIG_ETH_P_OUI */
706#ifdef CONFIG_DPP
707 os_memset(&dpp_conf, 0, sizeof(dpp_conf));
708 /* TODO: dpp_conf.msg_ctx? */
709 interfaces.dpp = dpp_global_init(&dpp_conf);
710 if (!interfaces.dpp)
711 return -1;
712#endif /* CONFIG_DPP */
713
714 for (;;) {
715 c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:");
716 if (c < 0)
717 break;
718 switch (c) {
719 case 'h':
720 usage();
721 break;
722 case 'd':
723 debug++;
724 if (wpa_debug_level > 0)
725 wpa_debug_level--;
726 break;
727 case 'B':
728 daemonize++;
729 break;
730 case 'e':
731 entropy_file = optarg;
732 break;
733 case 'f':
734 log_file = optarg;
735 break;
736 case 'K':
737 wpa_debug_show_keys++;
738 break;
739 case 'P':
740 os_free(pid_file);
741 pid_file = os_rel2abs_path(optarg);
742 break;
743 case 't':
744 wpa_debug_timestamp++;
745 break;
746#ifdef CONFIG_DEBUG_LINUX_TRACING
747 case 'T':
748 enable_trace_dbg = 1;
749 break;
750#endif /* CONFIG_DEBUG_LINUX_TRACING */
751 case 'v':
752 show_version();
753 exit(1);
754 break;
755 case 'g':
756 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
757 return -1;
758 break;
759 case 'G':
760 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
761 return -1;
762 break;
763 case 'b':
764 tmp_bss = os_realloc_array(bss_config,
765 num_bss_configs + 1,
766 sizeof(char *));
767 if (tmp_bss == NULL)
768 goto out;
769 bss_config = tmp_bss;
770 bss_config[num_bss_configs++] = optarg;
771 break;
772#ifdef CONFIG_DEBUG_SYSLOG
773 case 's':
774 wpa_debug_syslog = 1;
775 break;
776#endif /* CONFIG_DEBUG_SYSLOG */
777 case 'S':
778 start_ifaces_in_sync = 1;
779 break;
780#ifdef CONFIG_WPS
781 case 'u':
782 return gen_uuid(optarg);
783#endif /* CONFIG_WPS */
784 case 'i':
785 if (hostapd_get_interface_names(&if_names,
786 &if_names_size, optarg))
787 goto out;
788 break;
789 default:
790 usage();
791 break;
792 }
793 }
794
795#ifndef CONFIG_CTRL_IFACE_HIDL
796 if (optind == argc && interfaces.global_iface_path == NULL &&
797 num_bss_configs == 0)
798 usage();
799#endif /*CONFIG_CTRL_IFACE_HIDL*/
800
801 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
802
803 if (log_file)
804 wpa_debug_open_file(log_file);
805 else
806 wpa_debug_setup_stdout();
807#ifdef CONFIG_DEBUG_SYSLOG
808 if (wpa_debug_syslog)
809 wpa_debug_open_syslog();
810#endif /* CONFIG_DEBUG_SYSLOG */
811#ifdef CONFIG_DEBUG_LINUX_TRACING
812 if (enable_trace_dbg) {
813 int tret = wpa_debug_open_linux_tracing();
814 if (tret) {
815 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
816 return -1;
817 }
818 }
819#endif /* CONFIG_DEBUG_LINUX_TRACING */
820
821 interfaces.count = argc - optind;
822 if (interfaces.count || num_bss_configs) {
823 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
824 sizeof(struct hostapd_iface *));
825 if (interfaces.iface == NULL) {
826 wpa_printf(MSG_ERROR, "malloc failed");
827 return -1;
828 }
829 }
830
831 if (hostapd_global_init(&interfaces, entropy_file)) {
832 wpa_printf(MSG_ERROR, "Failed to initialize global context");
833 return -1;
834 }
835
836 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
837 hostapd_periodic, &interfaces, NULL);
838
839 if (fst_global_init()) {
840 wpa_printf(MSG_ERROR,
841 "Failed to initialize global FST context");
842 goto out;
843 }
844
845#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
846 if (!fst_global_add_ctrl(fst_ctrl_cli))
847 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
848#endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
849
850 /* Allocate and parse configuration for full interface files */
851 for (i = 0; i < interfaces.count; i++) {
852 char *if_name = NULL;
853
854 if (i < if_names_size)
855 if_name = if_names[i];
856
857 interfaces.iface[i] = hostapd_interface_init(&interfaces,
858 if_name,
859 argv[optind + i],
860 debug);
861 if (!interfaces.iface[i]) {
862 wpa_printf(MSG_ERROR, "Failed to initialize interface");
863 goto out;
864 }
865 if (start_ifaces_in_sync)
866 interfaces.iface[i]->need_to_start_in_sync = 1;
867 }
868
869 /* Allocate and parse configuration for per-BSS files */
870 for (i = 0; i < num_bss_configs; i++) {
871 struct hostapd_iface *iface;
872 char *fname;
873
874 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
875 fname = os_strchr(bss_config[i], ':');
876 if (fname == NULL) {
877 wpa_printf(MSG_ERROR,
878 "Invalid BSS config identifier '%s'",
879 bss_config[i]);
880 goto out;
881 }
882 *fname++ = '\0';
883 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
884 fname, debug);
885 if (iface == NULL)
886 goto out;
887 for (j = 0; j < interfaces.count; j++) {
888 if (interfaces.iface[j] == iface)
889 break;
890 }
891 if (j == interfaces.count) {
892 struct hostapd_iface **tmp;
893 tmp = os_realloc_array(interfaces.iface,
894 interfaces.count + 1,
895 sizeof(struct hostapd_iface *));
896 if (tmp == NULL) {
897 hostapd_interface_deinit_free(iface);
898 goto out;
899 }
900 interfaces.iface = tmp;
901 interfaces.iface[interfaces.count++] = iface;
902 }
903 }
904
905 /*
906 * Enable configured interfaces. Depending on channel configuration,
907 * this may complete full initialization before returning or use a
908 * callback mechanism to complete setup in case of operations like HT
909 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
910 * In such case, the interface will be enabled from eloop context within
911 * hostapd_global_run().
912 */
913 interfaces.terminate_on_error = interfaces.count;
914 for (i = 0; i < interfaces.count; i++) {
915 if (hostapd_driver_init(interfaces.iface[i]) ||
916 hostapd_setup_interface(interfaces.iface[i]))
917 goto out;
918 }
919
920#ifdef CONFIG_CTRL_IFACE_HIDL
921 if (hostapd_hidl_init(&interfaces)) {
922 wpa_printf(MSG_ERROR, "Failed to initialize HIDL interface");
923 goto out;
924 }
925#endif /* CONFIG_CTRL_IFACE_HIDL */
926 hostapd_global_ctrl_iface_init(&interfaces);
927
928 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
929 wpa_printf(MSG_ERROR, "Failed to start eloop");
930 goto out;
931 }
932
933 ret = 0;
934
935 out:
936#ifdef CONFIG_CTRL_IFACE_HIDL
937 hostapd_hidl_deinit(&interfaces);
938#endif /* CONFIG_CTRL_IFACE_HIDL */
939 hostapd_global_ctrl_iface_deinit(&interfaces);
940 /* Deinitialize all interfaces */
941 for (i = 0; i < interfaces.count; i++) {
942 if (!interfaces.iface[i])
943 continue;
944 interfaces.iface[i]->driver_ap_teardown =
945 !!(interfaces.iface[i]->drv_flags &
946 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
947 hostapd_interface_deinit_free(interfaces.iface[i]);
948 }
949 os_free(interfaces.iface);
950
951#ifdef CONFIG_DPP
952 dpp_global_deinit(interfaces.dpp);
953#endif /* CONFIG_DPP */
954
955 if (interfaces.eloop_initialized)
956 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
957 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
958 os_free(pid_file);
959
960 wpa_debug_close_syslog();
961 if (log_file)
962 wpa_debug_close_file();
963 wpa_debug_close_linux_tracing();
964
965 os_free(bss_config);
966
967 for (i = 0; i < if_names_size; i++)
968 os_free(if_names[i]);
969 os_free(if_names);
970
971 fst_global_deinit();
972
973 os_program_deinit();
974
975 return ret;
976}