• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd / main()
3  * Copyright (c) 2002-2017, 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 "fst/fst.h"
28 #include "config_file.h"
29 #include "eap_register.h"
30 #include "ctrl_iface.h"
31 
32 
33 struct hapd_global {
34 	void **drv_priv;
35 	size_t drv_count;
36 };
37 
38 static struct hapd_global global;
39 
40 
41 #ifndef CONFIG_NO_HOSTAPD_LOGGER
hostapd_logger_cb(void * ctx,const u8 * addr,unsigned int module,int level,const char * txt,size_t len)42 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
43 			      int level, const char *txt, size_t len)
44 {
45 	struct hostapd_data *hapd = ctx;
46 	char *format, *module_str;
47 	int maxlen;
48 	int conf_syslog_level, conf_stdout_level;
49 	unsigned int conf_syslog, conf_stdout;
50 
51 	maxlen = len + 100;
52 	format = os_malloc(maxlen);
53 	if (!format)
54 		return;
55 
56 	if (hapd && hapd->conf) {
57 		conf_syslog_level = hapd->conf->logger_syslog_level;
58 		conf_stdout_level = hapd->conf->logger_stdout_level;
59 		conf_syslog = hapd->conf->logger_syslog;
60 		conf_stdout = hapd->conf->logger_stdout;
61 	} else {
62 		conf_syslog_level = conf_stdout_level = 0;
63 		conf_syslog = conf_stdout = (unsigned int) -1;
64 	}
65 
66 	switch (module) {
67 	case HOSTAPD_MODULE_IEEE80211:
68 		module_str = "IEEE 802.11";
69 		break;
70 	case HOSTAPD_MODULE_IEEE8021X:
71 		module_str = "IEEE 802.1X";
72 		break;
73 	case HOSTAPD_MODULE_RADIUS:
74 		module_str = "RADIUS";
75 		break;
76 	case HOSTAPD_MODULE_WPA:
77 		module_str = "WPA";
78 		break;
79 	case HOSTAPD_MODULE_DRIVER:
80 		module_str = "DRIVER";
81 		break;
82 	case HOSTAPD_MODULE_IAPP:
83 		module_str = "IAPP";
84 		break;
85 	case HOSTAPD_MODULE_MLME:
86 		module_str = "MLME";
87 		break;
88 	default:
89 		module_str = NULL;
90 		break;
91 	}
92 
93 	if (hapd && hapd->conf && addr)
94 		os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
95 			    hapd->conf->iface, MAC2STR(addr),
96 			    module_str ? " " : "", module_str ? module_str : "",
97 			    txt);
98 	else if (hapd && hapd->conf)
99 		os_snprintf(format, maxlen, "%s:%s%s %s",
100 			    hapd->conf->iface, module_str ? " " : "",
101 			    module_str ? module_str : "", txt);
102 	else if (addr)
103 		os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
104 			    MAC2STR(addr), module_str ? " " : "",
105 			    module_str ? module_str : "", txt);
106 	else
107 		os_snprintf(format, maxlen, "%s%s%s",
108 			    module_str ? module_str : "",
109 			    module_str ? ": " : "", txt);
110 
111 #ifdef CONFIG_DEBUG_SYSLOG
112 	if (wpa_debug_syslog)
113 		conf_stdout = 0;
114 #endif /* CONFIG_DEBUG_SYSLOG */
115 	if ((conf_stdout & module) && level >= conf_stdout_level) {
116 		wpa_debug_print_timestamp();
117 		wpa_printf(MSG_INFO, "%s", format);
118 	}
119 
120 #ifndef CONFIG_NATIVE_WINDOWS
121 	if ((conf_syslog & module) && level >= conf_syslog_level) {
122 		int priority;
123 		switch (level) {
124 		case HOSTAPD_LEVEL_DEBUG_VERBOSE:
125 		case HOSTAPD_LEVEL_DEBUG:
126 			priority = LOG_DEBUG;
127 			break;
128 		case HOSTAPD_LEVEL_INFO:
129 			priority = LOG_INFO;
130 			break;
131 		case HOSTAPD_LEVEL_NOTICE:
132 			priority = LOG_NOTICE;
133 			break;
134 		case HOSTAPD_LEVEL_WARNING:
135 			priority = LOG_WARNING;
136 			break;
137 		default:
138 			priority = LOG_INFO;
139 			break;
140 		}
141 		syslog(priority, "%s", format);
142 	}
143 #endif /* CONFIG_NATIVE_WINDOWS */
144 
145 	os_free(format);
146 }
147 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
148 
149 
150 /**
151  * hostapd_driver_init - Preparate driver interface
152  */
hostapd_driver_init(struct hostapd_iface * iface)153 static int hostapd_driver_init(struct hostapd_iface *iface)
154 {
155 	struct wpa_init_params params;
156 	size_t i;
157 	struct hostapd_data *hapd = iface->bss[0];
158 	struct hostapd_bss_config *conf = hapd->conf;
159 	u8 *b = conf->bssid;
160 	struct wpa_driver_capa capa;
161 
162 	if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
163 		wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
164 		return -1;
165 	}
166 
167 	/* Initialize the driver interface */
168 	if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
169 		b = NULL;
170 
171 	os_memset(&params, 0, sizeof(params));
172 	for (i = 0; wpa_drivers[i]; i++) {
173 		if (wpa_drivers[i] != hapd->driver)
174 			continue;
175 
176 		if (global.drv_priv[i] == NULL &&
177 		    wpa_drivers[i]->global_init) {
178 			global.drv_priv[i] =
179 				wpa_drivers[i]->global_init(iface->interfaces);
180 			if (global.drv_priv[i] == NULL) {
181 				wpa_printf(MSG_ERROR, "Failed to initialize "
182 					   "driver '%s'",
183 					   wpa_drivers[i]->name);
184 				return -1;
185 			}
186 		}
187 
188 		params.global_priv = global.drv_priv[i];
189 		break;
190 	}
191 	params.bssid = b;
192 	params.ifname = hapd->conf->iface;
193 	params.driver_params = hapd->iconf->driver_params;
194 	params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
195 
196 	params.num_bridge = hapd->iface->num_bss;
197 	params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
198 	if (params.bridge == NULL)
199 		return -1;
200 	for (i = 0; i < hapd->iface->num_bss; i++) {
201 		struct hostapd_data *bss = hapd->iface->bss[i];
202 		if (bss->conf->bridge[0])
203 			params.bridge[i] = bss->conf->bridge;
204 	}
205 
206 	params.own_addr = hapd->own_addr;
207 
208 	hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
209 	os_free(params.bridge);
210 	if (hapd->drv_priv == NULL) {
211 		wpa_printf(MSG_ERROR, "%s driver initialization failed.",
212 			   hapd->driver->name);
213 		hapd->driver = NULL;
214 		return -1;
215 	}
216 
217 	if (hapd->driver->get_capa &&
218 	    hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
219 		struct wowlan_triggers *triggs;
220 
221 		iface->drv_flags = capa.flags;
222 		iface->smps_modes = capa.smps_modes;
223 		iface->probe_resp_offloads = capa.probe_resp_offloads;
224 		/*
225 		 * Use default extended capa values from per-radio information
226 		 */
227 		iface->extended_capa = capa.extended_capa;
228 		iface->extended_capa_mask = capa.extended_capa_mask;
229 		iface->extended_capa_len = capa.extended_capa_len;
230 		iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
231 
232 		/*
233 		 * Override extended capa with per-interface type (AP), if
234 		 * available from the driver.
235 		 */
236 		hostapd_get_ext_capa(iface);
237 
238 		triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
239 		if (triggs && hapd->driver->set_wowlan) {
240 			if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
241 				wpa_printf(MSG_ERROR, "set_wowlan failed");
242 		}
243 		os_free(triggs);
244 	}
245 
246 	return 0;
247 }
248 
249 
250 /**
251  * hostapd_interface_init - Read configuration file and init BSS data
252  *
253  * This function is used to parse configuration file for a full interface (one
254  * or more BSSes sharing the same radio) and allocate memory for the BSS
255  * interfaces. No actiual driver operations are started.
256  */
257 static struct hostapd_iface *
hostapd_interface_init(struct hapd_interfaces * interfaces,const char * if_name,const char * config_fname,int debug)258 hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
259 		       const char *config_fname, int debug)
260 {
261 	struct hostapd_iface *iface;
262 	int k;
263 
264 	wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
265 	iface = hostapd_init(interfaces, config_fname);
266 	if (!iface)
267 		return NULL;
268 
269 	if (if_name) {
270 		os_strlcpy(iface->conf->bss[0]->iface, if_name,
271 			   sizeof(iface->conf->bss[0]->iface));
272 	}
273 
274 	iface->interfaces = interfaces;
275 
276 	for (k = 0; k < debug; k++) {
277 		if (iface->bss[0]->conf->logger_stdout_level > 0)
278 			iface->bss[0]->conf->logger_stdout_level--;
279 	}
280 
281 	if (iface->conf->bss[0]->iface[0] == '\0' &&
282 	    !hostapd_drv_none(iface->bss[0])) {
283 		wpa_printf(MSG_ERROR,
284 			   "Interface name not specified in %s, nor by '-i' parameter",
285 			   config_fname);
286 		hostapd_interface_deinit_free(iface);
287 		return NULL;
288 	}
289 
290 	return iface;
291 }
292 
293 
294 /**
295  * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
296  */
handle_term(int sig,void * signal_ctx)297 static void handle_term(int sig, void *signal_ctx)
298 {
299 	wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
300 	eloop_terminate();
301 }
302 
303 
304 #ifndef CONFIG_NATIVE_WINDOWS
305 
handle_reload_iface(struct hostapd_iface * iface,void * ctx)306 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
307 {
308 	if (hostapd_reload_config(iface) < 0) {
309 		wpa_printf(MSG_WARNING, "Failed to read new configuration "
310 			   "file - continuing with old.");
311 	}
312 	return 0;
313 }
314 
315 
316 /**
317  * handle_reload - SIGHUP handler to reload configuration
318  */
handle_reload(int sig,void * signal_ctx)319 static void handle_reload(int sig, void *signal_ctx)
320 {
321 	struct hapd_interfaces *interfaces = signal_ctx;
322 	wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
323 		   sig);
324 	hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
325 }
326 
327 
handle_dump_state(int sig,void * signal_ctx)328 static void handle_dump_state(int sig, void *signal_ctx)
329 {
330 	/* Not used anymore - ignore signal */
331 }
332 #endif /* CONFIG_NATIVE_WINDOWS */
333 
334 
hostapd_global_init(struct hapd_interfaces * interfaces,const char * entropy_file)335 static int hostapd_global_init(struct hapd_interfaces *interfaces,
336 			       const char *entropy_file)
337 {
338 	int i;
339 
340 	os_memset(&global, 0, sizeof(global));
341 
342 	hostapd_logger_register_cb(hostapd_logger_cb);
343 
344 	if (eap_server_register_methods()) {
345 		wpa_printf(MSG_ERROR, "Failed to register EAP methods");
346 		return -1;
347 	}
348 
349 	if (eloop_init()) {
350 		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
351 		return -1;
352 	}
353 	interfaces->eloop_initialized = 1;
354 
355 	random_init(entropy_file);
356 
357 #ifndef CONFIG_NATIVE_WINDOWS
358 	eloop_register_signal(SIGHUP, handle_reload, interfaces);
359 	eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
360 #endif /* CONFIG_NATIVE_WINDOWS */
361 	eloop_register_signal_terminate(handle_term, interfaces);
362 
363 #ifndef CONFIG_NATIVE_WINDOWS
364 	openlog("hostapd", 0, LOG_DAEMON);
365 #endif /* CONFIG_NATIVE_WINDOWS */
366 
367 	for (i = 0; wpa_drivers[i]; i++)
368 		global.drv_count++;
369 	if (global.drv_count == 0) {
370 		wpa_printf(MSG_ERROR, "No drivers enabled");
371 		return -1;
372 	}
373 	global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
374 	if (global.drv_priv == NULL)
375 		return -1;
376 
377 	return 0;
378 }
379 
380 
hostapd_global_deinit(const char * pid_file,int eloop_initialized)381 static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
382 {
383 	int i;
384 
385 	for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
386 		if (!global.drv_priv[i])
387 			continue;
388 		wpa_drivers[i]->global_deinit(global.drv_priv[i]);
389 	}
390 	os_free(global.drv_priv);
391 	global.drv_priv = NULL;
392 
393 #ifdef EAP_SERVER_TNC
394 	tncs_global_deinit();
395 #endif /* EAP_SERVER_TNC */
396 
397 	random_deinit();
398 
399 	if (eloop_initialized)
400 		eloop_destroy();
401 
402 #ifndef CONFIG_NATIVE_WINDOWS
403 	closelog();
404 #endif /* CONFIG_NATIVE_WINDOWS */
405 
406 	eap_server_unregister_methods();
407 
408 	os_daemonize_terminate(pid_file);
409 }
410 
411 
hostapd_global_run(struct hapd_interfaces * ifaces,int daemonize,const char * pid_file)412 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
413 			      const char *pid_file)
414 {
415 #ifdef EAP_SERVER_TNC
416 	int tnc = 0;
417 	size_t i, k;
418 
419 	for (i = 0; !tnc && i < ifaces->count; i++) {
420 		for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
421 			if (ifaces->iface[i]->bss[0]->conf->tnc) {
422 				tnc++;
423 				break;
424 			}
425 		}
426 	}
427 
428 	if (tnc && tncs_global_init() < 0) {
429 		wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
430 		return -1;
431 	}
432 #endif /* EAP_SERVER_TNC */
433 
434 	if (daemonize) {
435 		if (os_daemonize(pid_file)) {
436 			wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
437 			return -1;
438 		}
439 		if (eloop_sock_requeue()) {
440 			wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
441 				   strerror(errno));
442 			return -1;
443 		}
444 	}
445 
446 	eloop_run();
447 
448 	return 0;
449 }
450 
451 
show_version(void)452 static void show_version(void)
453 {
454 	fprintf(stderr,
455 		"hostapd v" VERSION_STR "\n"
456 		"User space daemon for IEEE 802.11 AP management,\n"
457 		"IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
458 		"Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi> "
459 		"and contributors\n");
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 
655 	if (os_program_init())
656 		return -1;
657 
658 	os_memset(&interfaces, 0, sizeof(interfaces));
659 	interfaces.reload_config = hostapd_reload_config;
660 	interfaces.config_read_cb = hostapd_config_read;
661 	interfaces.for_each_interface = hostapd_for_each_interface;
662 	interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
663 	interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
664 	interfaces.driver_init = hostapd_driver_init;
665 	interfaces.global_iface_path = NULL;
666 	interfaces.global_iface_name = NULL;
667 	interfaces.global_ctrl_sock = -1;
668 	dl_list_init(&interfaces.global_ctrl_dst);
669 
670 	for (;;) {
671 		c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:");
672 		if (c < 0)
673 			break;
674 		switch (c) {
675 		case 'h':
676 			usage();
677 			break;
678 		case 'd':
679 			debug++;
680 			if (wpa_debug_level > 0)
681 				wpa_debug_level--;
682 			break;
683 		case 'B':
684 			daemonize++;
685 			break;
686 		case 'e':
687 			entropy_file = optarg;
688 			break;
689 		case 'f':
690 			log_file = optarg;
691 			break;
692 		case 'K':
693 			wpa_debug_show_keys++;
694 			break;
695 		case 'P':
696 			os_free(pid_file);
697 			pid_file = os_rel2abs_path(optarg);
698 			break;
699 		case 't':
700 			wpa_debug_timestamp++;
701 			break;
702 #ifdef CONFIG_DEBUG_LINUX_TRACING
703 		case 'T':
704 			enable_trace_dbg = 1;
705 			break;
706 #endif /* CONFIG_DEBUG_LINUX_TRACING */
707 		case 'v':
708 			show_version();
709 			exit(1);
710 			break;
711 		case 'g':
712 			if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
713 				return -1;
714 			break;
715 		case 'G':
716 			if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
717 				return -1;
718 			break;
719 		case 'b':
720 			tmp_bss = os_realloc_array(bss_config,
721 						   num_bss_configs + 1,
722 						   sizeof(char *));
723 			if (tmp_bss == NULL)
724 				goto out;
725 			bss_config = tmp_bss;
726 			bss_config[num_bss_configs++] = optarg;
727 			break;
728 #ifdef CONFIG_DEBUG_SYSLOG
729 		case 's':
730 			wpa_debug_syslog = 1;
731 			break;
732 #endif /* CONFIG_DEBUG_SYSLOG */
733 		case 'S':
734 			start_ifaces_in_sync = 1;
735 			break;
736 #ifdef CONFIG_WPS
737 		case 'u':
738 			return gen_uuid(optarg);
739 #endif /* CONFIG_WPS */
740 		case 'i':
741 			if (hostapd_get_interface_names(&if_names,
742 							&if_names_size, optarg))
743 				goto out;
744 			break;
745 		default:
746 			usage();
747 			break;
748 		}
749 	}
750 
751 	if (optind == argc && interfaces.global_iface_path == NULL &&
752 	    num_bss_configs == 0)
753 		usage();
754 
755 	wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
756 
757 	if (log_file)
758 		wpa_debug_open_file(log_file);
759 	else
760 		wpa_debug_setup_stdout();
761 #ifdef CONFIG_DEBUG_SYSLOG
762 	if (wpa_debug_syslog)
763 		wpa_debug_open_syslog();
764 #endif /* CONFIG_DEBUG_SYSLOG */
765 #ifdef CONFIG_DEBUG_LINUX_TRACING
766 	if (enable_trace_dbg) {
767 		int tret = wpa_debug_open_linux_tracing();
768 		if (tret) {
769 			wpa_printf(MSG_ERROR, "Failed to enable trace logging");
770 			return -1;
771 		}
772 	}
773 #endif /* CONFIG_DEBUG_LINUX_TRACING */
774 
775 	interfaces.count = argc - optind;
776 	if (interfaces.count || num_bss_configs) {
777 		interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
778 					     sizeof(struct hostapd_iface *));
779 		if (interfaces.iface == NULL) {
780 			wpa_printf(MSG_ERROR, "malloc failed");
781 			return -1;
782 		}
783 	}
784 
785 	if (hostapd_global_init(&interfaces, entropy_file)) {
786 		wpa_printf(MSG_ERROR, "Failed to initialize global context");
787 		return -1;
788 	}
789 
790 	eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
791 			       hostapd_periodic, &interfaces, NULL);
792 
793 	if (fst_global_init()) {
794 		wpa_printf(MSG_ERROR,
795 			   "Failed to initialize global FST context");
796 		goto out;
797 	}
798 
799 #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
800 	if (!fst_global_add_ctrl(fst_ctrl_cli))
801 		wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
802 #endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
803 
804 	/* Allocate and parse configuration for full interface files */
805 	for (i = 0; i < interfaces.count; i++) {
806 		char *if_name = NULL;
807 
808 		if (i < if_names_size)
809 			if_name = if_names[i];
810 
811 		interfaces.iface[i] = hostapd_interface_init(&interfaces,
812 							     if_name,
813 							     argv[optind + i],
814 							     debug);
815 		if (!interfaces.iface[i]) {
816 			wpa_printf(MSG_ERROR, "Failed to initialize interface");
817 			goto out;
818 		}
819 		if (start_ifaces_in_sync)
820 			interfaces.iface[i]->need_to_start_in_sync = 1;
821 	}
822 
823 	/* Allocate and parse configuration for per-BSS files */
824 	for (i = 0; i < num_bss_configs; i++) {
825 		struct hostapd_iface *iface;
826 		char *fname;
827 
828 		wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
829 		fname = os_strchr(bss_config[i], ':');
830 		if (fname == NULL) {
831 			wpa_printf(MSG_ERROR,
832 				   "Invalid BSS config identifier '%s'",
833 				   bss_config[i]);
834 			goto out;
835 		}
836 		*fname++ = '\0';
837 		iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
838 						   fname, debug);
839 		if (iface == NULL)
840 			goto out;
841 		for (j = 0; j < interfaces.count; j++) {
842 			if (interfaces.iface[j] == iface)
843 				break;
844 		}
845 		if (j == interfaces.count) {
846 			struct hostapd_iface **tmp;
847 			tmp = os_realloc_array(interfaces.iface,
848 					       interfaces.count + 1,
849 					       sizeof(struct hostapd_iface *));
850 			if (tmp == NULL) {
851 				hostapd_interface_deinit_free(iface);
852 				goto out;
853 			}
854 			interfaces.iface = tmp;
855 			interfaces.iface[interfaces.count++] = iface;
856 		}
857 	}
858 
859 	/*
860 	 * Enable configured interfaces. Depending on channel configuration,
861 	 * this may complete full initialization before returning or use a
862 	 * callback mechanism to complete setup in case of operations like HT
863 	 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
864 	 * In such case, the interface will be enabled from eloop context within
865 	 * hostapd_global_run().
866 	 */
867 	interfaces.terminate_on_error = interfaces.count;
868 	for (i = 0; i < interfaces.count; i++) {
869 		if (hostapd_driver_init(interfaces.iface[i]) ||
870 		    hostapd_setup_interface(interfaces.iface[i]))
871 			goto out;
872 	}
873 
874 	hostapd_global_ctrl_iface_init(&interfaces);
875 
876 	if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
877 		wpa_printf(MSG_ERROR, "Failed to start eloop");
878 		goto out;
879 	}
880 
881 	ret = 0;
882 
883  out:
884 	hostapd_global_ctrl_iface_deinit(&interfaces);
885 	/* Deinitialize all interfaces */
886 	for (i = 0; i < interfaces.count; i++) {
887 		if (!interfaces.iface[i])
888 			continue;
889 		interfaces.iface[i]->driver_ap_teardown =
890 			!!(interfaces.iface[i]->drv_flags &
891 			   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
892 		hostapd_interface_deinit_free(interfaces.iface[i]);
893 	}
894 	os_free(interfaces.iface);
895 
896 	if (interfaces.eloop_initialized)
897 		eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
898 	hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
899 	os_free(pid_file);
900 
901 	wpa_debug_close_syslog();
902 	if (log_file)
903 		wpa_debug_close_file();
904 	wpa_debug_close_linux_tracing();
905 
906 	os_free(bss_config);
907 
908 	for (i = 0; i < if_names_size; i++)
909 		os_free(if_names[i]);
910 	os_free(if_names);
911 
912 	fst_global_deinit();
913 
914 	os_program_deinit();
915 
916 	return ret;
917 }
918