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