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