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