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