xref: /freebsd/contrib/wpa/hostapd/main.c (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
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
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  */
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(&params, 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, &params);
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 *
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  */
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 
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  */
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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
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 
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 */
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 
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 			break;
727 		case 'g':
728 			if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
729 				return -1;
730 			break;
731 		case 'G':
732 			if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
733 				return -1;
734 			break;
735 		case 'b':
736 			tmp_bss = os_realloc_array(bss_config,
737 						   num_bss_configs + 1,
738 						   sizeof(char *));
739 			if (tmp_bss == NULL)
740 				goto out;
741 			bss_config = tmp_bss;
742 			bss_config[num_bss_configs++] = optarg;
743 			break;
744 #ifdef CONFIG_DEBUG_SYSLOG
745 		case 's':
746 			wpa_debug_syslog = 1;
747 			break;
748 #endif /* CONFIG_DEBUG_SYSLOG */
749 		case 'S':
750 			start_ifaces_in_sync = 1;
751 			break;
752 #ifdef CONFIG_WPS
753 		case 'u':
754 			return gen_uuid(optarg);
755 #endif /* CONFIG_WPS */
756 		case 'i':
757 			if (hostapd_get_interface_names(&if_names,
758 							&if_names_size, optarg))
759 				goto out;
760 			break;
761 		default:
762 			usage();
763 			break;
764 		}
765 	}
766 
767 	if (optind == argc && interfaces.global_iface_path == NULL &&
768 	    num_bss_configs == 0)
769 		usage();
770 
771 	wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
772 
773 	if (log_file)
774 		wpa_debug_open_file(log_file);
775 	if (!log_file && !wpa_debug_syslog)
776 		wpa_debug_setup_stdout();
777 #ifdef CONFIG_DEBUG_SYSLOG
778 	if (wpa_debug_syslog)
779 		wpa_debug_open_syslog();
780 #endif /* CONFIG_DEBUG_SYSLOG */
781 #ifdef CONFIG_DEBUG_LINUX_TRACING
782 	if (enable_trace_dbg) {
783 		int tret = wpa_debug_open_linux_tracing();
784 		if (tret) {
785 			wpa_printf(MSG_ERROR, "Failed to enable trace logging");
786 			return -1;
787 		}
788 	}
789 #endif /* CONFIG_DEBUG_LINUX_TRACING */
790 
791 	interfaces.count = argc - optind;
792 	if (interfaces.count || num_bss_configs) {
793 		interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
794 					     sizeof(struct hostapd_iface *));
795 		if (interfaces.iface == NULL) {
796 			wpa_printf(MSG_ERROR, "malloc failed");
797 			return -1;
798 		}
799 	}
800 
801 	if (hostapd_global_init(&interfaces, entropy_file)) {
802 		wpa_printf(MSG_ERROR, "Failed to initialize global context");
803 		return -1;
804 	}
805 
806 	eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
807 			       hostapd_periodic, &interfaces, NULL);
808 
809 	if (fst_global_init()) {
810 		wpa_printf(MSG_ERROR,
811 			   "Failed to initialize global FST context");
812 		goto out;
813 	}
814 
815 #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
816 	if (!fst_global_add_ctrl(fst_ctrl_cli))
817 		wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
818 #endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
819 
820 	/* Allocate and parse configuration for full interface files */
821 	for (i = 0; i < interfaces.count; i++) {
822 		char *if_name = NULL;
823 
824 		if (i < if_names_size)
825 			if_name = if_names[i];
826 
827 		interfaces.iface[i] = hostapd_interface_init(&interfaces,
828 							     if_name,
829 							     argv[optind + i],
830 							     debug);
831 		if (!interfaces.iface[i]) {
832 			wpa_printf(MSG_ERROR, "Failed to initialize interface");
833 			goto out;
834 		}
835 		if (start_ifaces_in_sync)
836 			interfaces.iface[i]->need_to_start_in_sync = 1;
837 	}
838 
839 	/* Allocate and parse configuration for per-BSS files */
840 	for (i = 0; i < num_bss_configs; i++) {
841 		struct hostapd_iface *iface;
842 		char *fname;
843 
844 		wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
845 		fname = os_strchr(bss_config[i], ':');
846 		if (fname == NULL) {
847 			wpa_printf(MSG_ERROR,
848 				   "Invalid BSS config identifier '%s'",
849 				   bss_config[i]);
850 			goto out;
851 		}
852 		*fname++ = '\0';
853 		iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
854 						   fname, debug);
855 		if (iface == NULL)
856 			goto out;
857 		for (j = 0; j < interfaces.count; j++) {
858 			if (interfaces.iface[j] == iface)
859 				break;
860 		}
861 		if (j == interfaces.count) {
862 			struct hostapd_iface **tmp;
863 			tmp = os_realloc_array(interfaces.iface,
864 					       interfaces.count + 1,
865 					       sizeof(struct hostapd_iface *));
866 			if (tmp == NULL) {
867 				hostapd_interface_deinit_free(iface);
868 				goto out;
869 			}
870 			interfaces.iface = tmp;
871 			interfaces.iface[interfaces.count++] = iface;
872 		}
873 	}
874 
875 	/*
876 	 * Enable configured interfaces. Depending on channel configuration,
877 	 * this may complete full initialization before returning or use a
878 	 * callback mechanism to complete setup in case of operations like HT
879 	 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
880 	 * In such case, the interface will be enabled from eloop context within
881 	 * hostapd_global_run().
882 	 */
883 	interfaces.terminate_on_error = interfaces.count;
884 	for (i = 0; i < interfaces.count; i++) {
885 		if (hostapd_driver_init(interfaces.iface[i]) ||
886 		    hostapd_setup_interface(interfaces.iface[i]))
887 			goto out;
888 	}
889 
890 	hostapd_global_ctrl_iface_init(&interfaces);
891 
892 	if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
893 		wpa_printf(MSG_ERROR, "Failed to start eloop");
894 		goto out;
895 	}
896 
897 	ret = 0;
898 
899  out:
900 	hostapd_global_ctrl_iface_deinit(&interfaces);
901 	/* Deinitialize all interfaces */
902 	for (i = 0; i < interfaces.count; i++) {
903 		if (!interfaces.iface[i])
904 			continue;
905 		interfaces.iface[i]->driver_ap_teardown =
906 			!!(interfaces.iface[i]->drv_flags &
907 			   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
908 		hostapd_interface_deinit_free(interfaces.iface[i]);
909 		interfaces.iface[i] = NULL;
910 	}
911 	os_free(interfaces.iface);
912 	interfaces.iface = NULL;
913 	interfaces.count = 0;
914 
915 #ifdef CONFIG_DPP
916 	dpp_global_deinit(interfaces.dpp);
917 #endif /* CONFIG_DPP */
918 
919 	if (interfaces.eloop_initialized)
920 		eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
921 	hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
922 	os_free(pid_file);
923 
924 	wpa_debug_close_syslog();
925 	if (log_file)
926 		wpa_debug_close_file();
927 	wpa_debug_close_linux_tracing();
928 
929 	os_free(bss_config);
930 
931 	for (i = 0; i < if_names_size; i++)
932 		os_free(if_names[i]);
933 	os_free(if_names);
934 
935 	fst_global_deinit();
936 
937 	os_program_deinit();
938 
939 	return ret;
940 }
941