• 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 #ifdef CONFIG_IEEE80211BE
170 	for (i = 0; conf->mld_ap && i < iface->interfaces->count; i++) {
171 		struct hostapd_iface *h = iface->interfaces->iface[i];
172 		struct hostapd_data *h_hapd = h->bss[0];
173 		struct hostapd_bss_config *hconf = h_hapd->conf;
174 
175 		if (h == iface) {
176 			wpa_printf(MSG_DEBUG, "MLD: Skip own interface");
177 			continue;
178 		}
179 
180 		if (!hconf->mld_ap || hconf->mld_id != conf->mld_id) {
181 			wpa_printf(MSG_DEBUG,
182 				   "MLD: Skip non matching mld_id");
183 			continue;
184 		}
185 
186 		wpa_printf(MSG_DEBUG, "MLD: Found matching MLD interface");
187 		if (!h_hapd->drv_priv) {
188 			wpa_printf(MSG_DEBUG,
189 				   "MLD: Matching MLD BSS not initialized yet");
190 			continue;
191 		}
192 
193 		hapd->drv_priv = h_hapd->drv_priv;
194 
195 		/*
196 		 * All interfaces participating in the AP MLD would have
197 		 * the same MLD address, which is the interface hardware
198 		 * address, while the interface address would be
199 		 * derived from the original interface address if BSSID
200 		 * is not configured, and otherwise it would be the
201 		 * configured BSSID.
202 		 */
203 		os_memcpy(hapd->mld_addr, h_hapd->mld_addr, ETH_ALEN);
204 		if (is_zero_ether_addr(b)) {
205 			os_memcpy(hapd->own_addr, h_hapd->mld_addr, ETH_ALEN);
206 			random_mac_addr_keep_oui(hapd->own_addr);
207 		} else {
208 			os_memcpy(hapd->own_addr, b, ETH_ALEN);
209 		}
210 
211 		/*
212 		 * Mark the interface as a secondary interface, as this
213 		 * is needed for the de-initialization flow
214 		 */
215 		hapd->mld_first_bss = h_hapd;
216 		hapd->mld_link_id = hapd->mld_first_bss->mld_next_link_id++;
217 
218 		goto setup_mld;
219 	}
220 #endif /* CONFIG_IEEE80211BE */
221 
222 	/* Initialize the driver interface */
223 	if (is_zero_ether_addr(b))
224 		b = NULL;
225 
226 	os_memset(&params, 0, sizeof(params));
227 	for (i = 0; wpa_drivers[i]; i++) {
228 		if (wpa_drivers[i] != hapd->driver)
229 			continue;
230 
231 		if (global.drv_priv[i] == NULL &&
232 		    wpa_drivers[i]->global_init) {
233 			global.drv_priv[i] =
234 				wpa_drivers[i]->global_init(iface->interfaces);
235 			if (global.drv_priv[i] == NULL) {
236 				wpa_printf(MSG_ERROR, "Failed to initialize "
237 					   "driver '%s'",
238 					   wpa_drivers[i]->name);
239 				return -1;
240 			}
241 		}
242 
243 		params.global_priv = global.drv_priv[i];
244 		break;
245 	}
246 	params.bssid = b;
247 #ifdef CONFIG_IEEE80211BE
248 	/*
249 	 * Use the configured MLD MAC address as the interface hardware address
250 	 * if this AP is a part of an AP MLD.
251 	 */
252 	if (!is_zero_ether_addr(hapd->conf->mld_addr) && hapd->conf->mld_ap)
253 		params.bssid = hapd->conf->mld_addr;
254 #endif /* CONFIG_IEEE80211BE */
255 
256 	params.ifname = hapd->conf->iface;
257 	params.driver_params = hapd->iconf->driver_params;
258 	params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
259 
260 	params.num_bridge = hapd->iface->num_bss;
261 	params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
262 	if (params.bridge == NULL)
263 		return -1;
264 	for (i = 0; i < hapd->iface->num_bss; i++) {
265 		struct hostapd_data *bss = hapd->iface->bss[i];
266 		if (bss->conf->bridge[0])
267 			params.bridge[i] = bss->conf->bridge;
268 	}
269 
270 	params.own_addr = hapd->own_addr;
271 
272 	hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
273 	os_free(params.bridge);
274 	if (hapd->drv_priv == NULL) {
275 		wpa_printf(MSG_ERROR, "%s driver initialization failed.",
276 			   hapd->driver->name);
277 		hapd->driver = NULL;
278 		return -1;
279 	}
280 
281 #ifdef CONFIG_IEEE80211BE
282 	/*
283 	 * This is the first interface added to the AP MLD, so have the
284 	 * interface hardware address be the MLD address, while the link address
285 	 * would be derived from the original interface address if BSSID is not
286 	 * configured, and otherwise it would be the configured BSSID.
287 	 */
288 	if (hapd->conf->mld_ap) {
289 		os_memcpy(hapd->mld_addr, hapd->own_addr, ETH_ALEN);
290 		hapd->mld_next_link_id = 0;
291 		hapd->mld_link_id = hapd->mld_next_link_id++;
292 		if (!b)
293 			random_mac_addr_keep_oui(hapd->own_addr);
294 		else
295 			os_memcpy(hapd->own_addr, b, ETH_ALEN);
296 	}
297 
298 setup_mld:
299 #endif /* CONFIG_IEEE80211BE */
300 
301 	if (hapd->driver->get_capa &&
302 	    hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
303 		struct wowlan_triggers *triggs;
304 
305 		iface->drv_flags = capa.flags;
306 		iface->drv_flags2 = capa.flags2;
307 		iface->probe_resp_offloads = capa.probe_resp_offloads;
308 		/*
309 		 * Use default extended capa values from per-radio information
310 		 */
311 		iface->extended_capa = capa.extended_capa;
312 		iface->extended_capa_mask = capa.extended_capa_mask;
313 		iface->extended_capa_len = capa.extended_capa_len;
314 		iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
315 
316 		/*
317 		 * Override extended capa with per-interface type (AP), if
318 		 * available from the driver.
319 		 */
320 		hostapd_get_ext_capa(iface);
321 
322 		hostapd_get_mld_capa(iface);
323 
324 		triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
325 		if (triggs && hapd->driver->set_wowlan) {
326 			if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
327 				wpa_printf(MSG_ERROR, "set_wowlan failed");
328 		}
329 		os_free(triggs);
330 
331 		iface->mbssid_max_interfaces = capa.mbssid_max_interfaces;
332 		iface->ema_max_periodicity = capa.ema_max_periodicity;
333 	}
334 
335 #ifdef CONFIG_IEEE80211BE
336 	if (hapd->conf->mld_ap) {
337 		if (!(iface->drv_flags2 & WPA_DRIVER_FLAGS2_MLO)) {
338 			wpa_printf(MSG_INFO,
339 				   "MLD: Not supported by the driver");
340 			return -1;
341 		}
342 
343 		wpa_printf(MSG_DEBUG,
344 			   "MLD: Set link_id=%u, mld_addr=" MACSTR
345 			   ", own_addr=" MACSTR,
346 			   hapd->mld_link_id, MAC2STR(hapd->mld_addr),
347 			   MAC2STR(hapd->own_addr));
348 
349 		hostapd_drv_link_add(hapd, hapd->mld_link_id,
350 				     hapd->own_addr);
351 	}
352 #endif /* CONFIG_IEEE80211BE */
353 
354 	return 0;
355 }
356 
357 
358 /**
359  * hostapd_interface_init - Read configuration file and init BSS data
360  *
361  * This function is used to parse configuration file for a full interface (one
362  * or more BSSes sharing the same radio) and allocate memory for the BSS
363  * interfaces. No actual driver operations are started.
364  */
365 static struct hostapd_iface *
hostapd_interface_init(struct hapd_interfaces * interfaces,const char * if_name,const char * config_fname,int debug)366 hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
367 		       const char *config_fname, int debug)
368 {
369 	struct hostapd_iface *iface;
370 	int k;
371 
372 	wpa_printf(MSG_DEBUG, "Configuration file: %s", config_fname);
373 	iface = hostapd_init(interfaces, config_fname);
374 	if (!iface)
375 		return NULL;
376 
377 	if (if_name) {
378 		os_strlcpy(iface->conf->bss[0]->iface, if_name,
379 			   sizeof(iface->conf->bss[0]->iface));
380 	}
381 
382 	iface->interfaces = interfaces;
383 
384 	for (k = 0; k < debug; k++) {
385 		if (iface->bss[0]->conf->logger_stdout_level > 0)
386 			iface->bss[0]->conf->logger_stdout_level--;
387 	}
388 
389 	if (iface->conf->bss[0]->iface[0] == '\0' &&
390 	    !hostapd_drv_none(iface->bss[0])) {
391 		wpa_printf(MSG_ERROR,
392 			   "Interface name not specified in %s, nor by '-i' parameter",
393 			   config_fname);
394 		hostapd_interface_deinit_free(iface);
395 		return NULL;
396 	}
397 
398 	return iface;
399 }
400 
401 
402 /**
403  * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
404  */
handle_term(int sig,void * signal_ctx)405 static void handle_term(int sig, void *signal_ctx)
406 {
407 	wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
408 	eloop_terminate();
409 }
410 
411 
412 #ifndef CONFIG_NATIVE_WINDOWS
413 
handle_reload_iface(struct hostapd_iface * iface,void * ctx)414 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
415 {
416 	if (hostapd_reload_config(iface) < 0) {
417 		wpa_printf(MSG_WARNING, "Failed to read new configuration "
418 			   "file - continuing with old.");
419 	}
420 	return 0;
421 }
422 
423 
424 /**
425  * handle_reload - SIGHUP handler to reload configuration
426  */
handle_reload(int sig,void * signal_ctx)427 static void handle_reload(int sig, void *signal_ctx)
428 {
429 	struct hapd_interfaces *interfaces = signal_ctx;
430 	wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
431 		   sig);
432 	hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
433 }
434 
435 
handle_dump_state(int sig,void * signal_ctx)436 static void handle_dump_state(int sig, void *signal_ctx)
437 {
438 	/* Not used anymore - ignore signal */
439 }
440 #endif /* CONFIG_NATIVE_WINDOWS */
441 
442 
hostapd_global_init(struct hapd_interfaces * interfaces,const char * entropy_file)443 static int hostapd_global_init(struct hapd_interfaces *interfaces,
444 			       const char *entropy_file)
445 {
446 	int i;
447 
448 	os_memset(&global, 0, sizeof(global));
449 
450 	hostapd_logger_register_cb(hostapd_logger_cb);
451 
452 	if (eap_server_register_methods()) {
453 		wpa_printf(MSG_ERROR, "Failed to register EAP methods");
454 		return -1;
455 	}
456 
457 	if (eloop_init()) {
458 		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
459 		return -1;
460 	}
461 	interfaces->eloop_initialized = 1;
462 
463 	random_init(entropy_file);
464 
465 #ifndef CONFIG_NATIVE_WINDOWS
466 	eloop_register_signal(SIGHUP, handle_reload, interfaces);
467 	eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
468 #endif /* CONFIG_NATIVE_WINDOWS */
469 	eloop_register_signal_terminate(handle_term, interfaces);
470 
471 #ifndef CONFIG_NATIVE_WINDOWS
472 	openlog("hostapd", 0, LOG_DAEMON);
473 #endif /* CONFIG_NATIVE_WINDOWS */
474 
475 	for (i = 0; wpa_drivers[i]; i++)
476 		global.drv_count++;
477 	if (global.drv_count == 0) {
478 		wpa_printf(MSG_ERROR, "No drivers enabled");
479 		return -1;
480 	}
481 	global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
482 	if (global.drv_priv == NULL)
483 		return -1;
484 
485 	return 0;
486 }
487 
488 
hostapd_global_deinit(const char * pid_file,int eloop_initialized)489 static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
490 {
491 	int i;
492 
493 	for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
494 		if (!global.drv_priv[i])
495 			continue;
496 		wpa_drivers[i]->global_deinit(global.drv_priv[i]);
497 	}
498 	os_free(global.drv_priv);
499 	global.drv_priv = NULL;
500 
501 #ifdef EAP_SERVER_TNC
502 	tncs_global_deinit();
503 #endif /* EAP_SERVER_TNC */
504 
505 	random_deinit();
506 
507 	if (eloop_initialized)
508 		eloop_destroy();
509 
510 #ifndef CONFIG_NATIVE_WINDOWS
511 	closelog();
512 #endif /* CONFIG_NATIVE_WINDOWS */
513 
514 	eap_server_unregister_methods();
515 
516 	os_daemonize_terminate(pid_file);
517 }
518 
519 
hostapd_global_run(struct hapd_interfaces * ifaces,int daemonize,const char * pid_file)520 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
521 			      const char *pid_file)
522 {
523 #ifdef EAP_SERVER_TNC
524 	int tnc = 0;
525 	size_t i, k;
526 
527 	for (i = 0; !tnc && i < ifaces->count; i++) {
528 		for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
529 			if (ifaces->iface[i]->bss[0]->conf->tnc) {
530 				tnc++;
531 				break;
532 			}
533 		}
534 	}
535 
536 	if (tnc && tncs_global_init() < 0) {
537 		wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
538 		return -1;
539 	}
540 #endif /* EAP_SERVER_TNC */
541 
542 	if (daemonize) {
543 		if (os_daemonize(pid_file)) {
544 			wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
545 			return -1;
546 		}
547 		if (eloop_sock_requeue()) {
548 			wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
549 				   strerror(errno));
550 			return -1;
551 		}
552 	}
553 
554 #ifdef CONFIG_CTRL_IFACE_AIDL
555 	if (hostapd_aidl_init(ifaces)) {
556 		wpa_printf(MSG_ERROR, "Failed to initialize AIDL interface");
557 		return -1;
558 	}
559 #endif /* CONFIG_CTRL_IFACE_AIDL */
560 
561 	eloop_run();
562 
563 	return 0;
564 }
565 
566 
show_version(void)567 static void show_version(void)
568 {
569 	fprintf(stderr,
570 		"hostapd v%s\n"
571 		"User space daemon for IEEE 802.11 AP management,\n"
572 		"IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
573 		"Copyright (c) 2002-2022, Jouni Malinen <j@w1.fi> "
574 		"and contributors\n",
575 		VERSION_STR);
576 }
577 
578 
usage(void)579 static void usage(void)
580 {
581 	show_version();
582 	fprintf(stderr,
583 		"\n"
584 		"usage: hostapd [-hdBKtvq] [-P <PID file>] [-e <entropy file>] "
585 		"\\\n"
586 		"         [-g <global ctrl_iface>] [-G <group>]\\\n"
587 		"         [-i <comma-separated list of interface names>]\\\n"
588 		"         <configuration file(s)>\n"
589 		"\n"
590 		"options:\n"
591 		"   -h   show this usage\n"
592 		"   -d   show more debug messages (-dd for even more)\n"
593 		"   -B   run daemon in the background\n"
594 		"   -e   entropy file\n"
595 		"   -g   global control interface path\n"
596 		"   -G   group for control interfaces\n"
597 		"   -P   PID file\n"
598 		"   -K   include key data in debug messages\n"
599 #ifdef CONFIG_DEBUG_FILE
600 		"   -f   log output to debug file instead of stdout\n"
601 #endif /* CONFIG_DEBUG_FILE */
602 #ifdef CONFIG_DEBUG_LINUX_TRACING
603 		"   -T   record to Linux tracing in addition to logging\n"
604 		"        (records all messages regardless of debug verbosity)\n"
605 #endif /* CONFIG_DEBUG_LINUX_TRACING */
606 		"   -i   list of interface names to use\n"
607 #ifdef CONFIG_DEBUG_SYSLOG
608 		"   -s   log output to syslog instead of stdout\n"
609 #endif /* CONFIG_DEBUG_SYSLOG */
610 		"   -S   start all the interfaces synchronously\n"
611 		"   -t   include timestamps in some debug messages\n"
612 		"   -v   show hostapd version\n"
613 		"   -q   show less debug messages (-qq for even less)\n");
614 
615 	exit(1);
616 }
617 
618 
hostapd_msg_ifname_cb(void * ctx)619 static const char * hostapd_msg_ifname_cb(void *ctx)
620 {
621 	struct hostapd_data *hapd = ctx;
622 	if (hapd && hapd->conf)
623 		return hapd->conf->iface;
624 	return NULL;
625 }
626 
627 
hostapd_get_global_ctrl_iface(struct hapd_interfaces * interfaces,const char * path)628 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
629 					 const char *path)
630 {
631 #ifndef CONFIG_CTRL_IFACE_UDP
632 	char *pos;
633 #endif /* !CONFIG_CTRL_IFACE_UDP */
634 
635 	os_free(interfaces->global_iface_path);
636 	interfaces->global_iface_path = os_strdup(path);
637 	if (interfaces->global_iface_path == NULL)
638 		return -1;
639 
640 #ifndef CONFIG_CTRL_IFACE_UDP
641 	pos = os_strrchr(interfaces->global_iface_path, '/');
642 	if (pos == NULL) {
643 		wpa_printf(MSG_ERROR, "No '/' in the global control interface "
644 			   "file");
645 		os_free(interfaces->global_iface_path);
646 		interfaces->global_iface_path = NULL;
647 		return -1;
648 	}
649 
650 	*pos = '\0';
651 	interfaces->global_iface_name = pos + 1;
652 #endif /* !CONFIG_CTRL_IFACE_UDP */
653 
654 	return 0;
655 }
656 
657 
hostapd_get_ctrl_iface_group(struct hapd_interfaces * interfaces,const char * group)658 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
659 					const char *group)
660 {
661 #ifndef CONFIG_NATIVE_WINDOWS
662 	struct group *grp;
663 	grp = getgrnam(group);
664 	if (grp == NULL) {
665 		wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
666 		return -1;
667 	}
668 	interfaces->ctrl_iface_group = grp->gr_gid;
669 #endif /* CONFIG_NATIVE_WINDOWS */
670 	return 0;
671 }
672 
673 
hostapd_get_interface_names(char *** if_names,size_t * if_names_size,char * arg)674 static int hostapd_get_interface_names(char ***if_names,
675 				       size_t *if_names_size,
676 				       char *arg)
677 {
678 	char *if_name, *tmp, **nnames;
679 	size_t i;
680 
681 	if (!arg)
682 		return -1;
683 	if_name = strtok_r(arg, ",", &tmp);
684 
685 	while (if_name) {
686 		nnames = os_realloc_array(*if_names, 1 + *if_names_size,
687 					  sizeof(char *));
688 		if (!nnames)
689 			goto fail;
690 		*if_names = nnames;
691 
692 		(*if_names)[*if_names_size] = os_strdup(if_name);
693 		if (!(*if_names)[*if_names_size])
694 			goto fail;
695 		(*if_names_size)++;
696 		if_name = strtok_r(NULL, ",", &tmp);
697 	}
698 
699 	return 0;
700 
701 fail:
702 	for (i = 0; i < *if_names_size; i++)
703 		os_free((*if_names)[i]);
704 	os_free(*if_names);
705 	*if_names = NULL;
706 	*if_names_size = 0;
707 	return -1;
708 }
709 
710 
711 #ifdef CONFIG_WPS
gen_uuid(const char * txt_addr)712 static int gen_uuid(const char *txt_addr)
713 {
714 	u8 addr[ETH_ALEN];
715 	u8 uuid[UUID_LEN];
716 	char buf[100];
717 
718 	if (hwaddr_aton(txt_addr, addr) < 0)
719 		return -1;
720 
721 	uuid_gen_mac_addr(addr, uuid);
722 	if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
723 		return -1;
724 
725 	printf("%s\n", buf);
726 
727 	return 0;
728 }
729 #endif /* CONFIG_WPS */
730 
731 
732 #ifndef HOSTAPD_CLEANUP_INTERVAL
733 #define HOSTAPD_CLEANUP_INTERVAL 10
734 #endif /* HOSTAPD_CLEANUP_INTERVAL */
735 
hostapd_periodic_call(struct hostapd_iface * iface,void * ctx)736 static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
737 {
738 	hostapd_periodic_iface(iface);
739 	return 0;
740 }
741 
742 
743 /* Periodic cleanup tasks */
hostapd_periodic(void * eloop_ctx,void * timeout_ctx)744 static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
745 {
746 	struct hapd_interfaces *interfaces = eloop_ctx;
747 
748 	eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
749 			       hostapd_periodic, interfaces, NULL);
750 	hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
751 }
752 
753 
main(int argc,char * argv[])754 int main(int argc, char *argv[])
755 {
756 	struct hapd_interfaces interfaces;
757 	int ret = 1;
758 	size_t i, j;
759 	int c, debug = 0, daemonize = 0;
760 	char *pid_file = NULL;
761 	const char *log_file = NULL;
762 	const char *entropy_file = NULL;
763 	char **bss_config = NULL, **tmp_bss;
764 	size_t num_bss_configs = 0;
765 #ifdef CONFIG_DEBUG_LINUX_TRACING
766 	int enable_trace_dbg = 0;
767 #endif /* CONFIG_DEBUG_LINUX_TRACING */
768 	int start_ifaces_in_sync = 0;
769 	char **if_names = NULL;
770 	size_t if_names_size = 0;
771 #ifdef CONFIG_DPP
772 	struct dpp_global_config dpp_conf;
773 #endif /* CONFIG_DPP */
774 
775 	if (os_program_init())
776 		return -1;
777 
778 	os_memset(&interfaces, 0, sizeof(interfaces));
779 	interfaces.reload_config = hostapd_reload_config;
780 	interfaces.config_read_cb = hostapd_config_read;
781 	interfaces.for_each_interface = hostapd_for_each_interface;
782 	interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
783 	interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
784 	interfaces.driver_init = hostapd_driver_init;
785 	interfaces.global_iface_path = NULL;
786 	interfaces.global_iface_name = NULL;
787 	interfaces.global_ctrl_sock = -1;
788 	dl_list_init(&interfaces.global_ctrl_dst);
789 #ifdef CONFIG_ETH_P_OUI
790 	dl_list_init(&interfaces.eth_p_oui);
791 #endif /* CONFIG_ETH_P_OUI */
792 #ifdef CONFIG_DPP
793 	os_memset(&dpp_conf, 0, sizeof(dpp_conf));
794 	dpp_conf.cb_ctx = &interfaces;
795 #ifdef CONFIG_DPP2
796 	dpp_conf.remove_bi = hostapd_dpp_remove_bi;
797 #endif /* CONFIG_DPP2 */
798 	interfaces.dpp = dpp_global_init(&dpp_conf);
799 	if (!interfaces.dpp)
800 		return -1;
801 #endif /* CONFIG_DPP */
802 
803 	for (;;) {
804 		c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:q");
805 		if (c < 0)
806 			break;
807 		switch (c) {
808 		case 'h':
809 			usage();
810 			break;
811 		case 'd':
812 			debug++;
813 			if (wpa_debug_level > 0)
814 				wpa_debug_level--;
815 			break;
816 		case 'B':
817 			daemonize++;
818 			break;
819 		case 'e':
820 			entropy_file = optarg;
821 			break;
822 		case 'f':
823 			log_file = optarg;
824 			break;
825 		case 'K':
826 			wpa_debug_show_keys++;
827 			break;
828 		case 'P':
829 			os_free(pid_file);
830 			pid_file = os_rel2abs_path(optarg);
831 			break;
832 		case 't':
833 			wpa_debug_timestamp++;
834 			break;
835 #ifdef CONFIG_DEBUG_LINUX_TRACING
836 		case 'T':
837 			enable_trace_dbg = 1;
838 			break;
839 #endif /* CONFIG_DEBUG_LINUX_TRACING */
840 		case 'v':
841 			show_version();
842 			exit(1);
843 		case 'g':
844 			if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
845 				return -1;
846 			break;
847 		case 'G':
848 			if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
849 				return -1;
850 			break;
851 		case 'b':
852 			tmp_bss = os_realloc_array(bss_config,
853 						   num_bss_configs + 1,
854 						   sizeof(char *));
855 			if (tmp_bss == NULL)
856 				goto out;
857 			bss_config = tmp_bss;
858 			bss_config[num_bss_configs++] = optarg;
859 			break;
860 #ifdef CONFIG_DEBUG_SYSLOG
861 		case 's':
862 			wpa_debug_syslog = 1;
863 			break;
864 #endif /* CONFIG_DEBUG_SYSLOG */
865 		case 'S':
866 			start_ifaces_in_sync = 1;
867 			break;
868 #ifdef CONFIG_WPS
869 		case 'u':
870 			return gen_uuid(optarg);
871 #endif /* CONFIG_WPS */
872 		case 'i':
873 			if (hostapd_get_interface_names(&if_names,
874 							&if_names_size, optarg))
875 				goto out;
876 			break;
877 		case 'q':
878 			wpa_debug_level++;
879 			break;
880 		default:
881 			usage();
882 			break;
883 		}
884 	}
885 
886 #ifndef CONFIG_CTRL_IFACE_AIDL
887 	if (optind == argc && interfaces.global_iface_path == NULL &&
888 	    num_bss_configs == 0)
889 		usage();
890 #endif
891 
892 	wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
893 
894 	if (log_file)
895 		wpa_debug_open_file(log_file);
896 	if (!log_file && !wpa_debug_syslog)
897 		wpa_debug_setup_stdout();
898 #ifdef CONFIG_DEBUG_SYSLOG
899 	if (wpa_debug_syslog)
900 		wpa_debug_open_syslog();
901 #endif /* CONFIG_DEBUG_SYSLOG */
902 #ifdef CONFIG_DEBUG_LINUX_TRACING
903 	if (enable_trace_dbg) {
904 		int tret = wpa_debug_open_linux_tracing();
905 		if (tret) {
906 			wpa_printf(MSG_ERROR, "Failed to enable trace logging");
907 			return -1;
908 		}
909 	}
910 #endif /* CONFIG_DEBUG_LINUX_TRACING */
911 
912 	interfaces.count = argc - optind;
913 	if (interfaces.count || num_bss_configs) {
914 		interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
915 					     sizeof(struct hostapd_iface *));
916 		if (interfaces.iface == NULL) {
917 			wpa_printf(MSG_ERROR, "malloc failed");
918 			return -1;
919 		}
920 	}
921 
922 	if (hostapd_global_init(&interfaces, entropy_file)) {
923 		wpa_printf(MSG_ERROR, "Failed to initialize global context");
924 		return -1;
925 	}
926 
927 	eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
928 			       hostapd_periodic, &interfaces, NULL);
929 
930 	if (fst_global_init()) {
931 		wpa_printf(MSG_ERROR,
932 			   "Failed to initialize global FST context");
933 		goto out;
934 	}
935 
936 #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
937 	if (!fst_global_add_ctrl(fst_ctrl_cli))
938 		wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
939 #endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
940 
941 	/* Allocate and parse configuration for full interface files */
942 	for (i = 0; i < interfaces.count; i++) {
943 		char *if_name = NULL;
944 
945 		if (i < if_names_size)
946 			if_name = if_names[i];
947 
948 		interfaces.iface[i] = hostapd_interface_init(&interfaces,
949 							     if_name,
950 							     argv[optind + i],
951 							     debug);
952 		if (!interfaces.iface[i]) {
953 			wpa_printf(MSG_ERROR, "Failed to initialize interface");
954 			goto out;
955 		}
956 		if (start_ifaces_in_sync)
957 			interfaces.iface[i]->need_to_start_in_sync = 1;
958 	}
959 
960 	/* Allocate and parse configuration for per-BSS files */
961 	for (i = 0; i < num_bss_configs; i++) {
962 		struct hostapd_iface *iface;
963 		char *fname;
964 
965 		wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
966 		fname = os_strchr(bss_config[i], ':');
967 		if (fname == NULL) {
968 			wpa_printf(MSG_ERROR,
969 				   "Invalid BSS config identifier '%s'",
970 				   bss_config[i]);
971 			goto out;
972 		}
973 		*fname++ = '\0';
974 		iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
975 						   fname, debug);
976 		if (iface == NULL)
977 			goto out;
978 		for (j = 0; j < interfaces.count; j++) {
979 			if (interfaces.iface[j] == iface)
980 				break;
981 		}
982 		if (j == interfaces.count) {
983 			struct hostapd_iface **tmp;
984 			tmp = os_realloc_array(interfaces.iface,
985 					       interfaces.count + 1,
986 					       sizeof(struct hostapd_iface *));
987 			if (tmp == NULL) {
988 				hostapd_interface_deinit_free(iface);
989 				goto out;
990 			}
991 			interfaces.iface = tmp;
992 			interfaces.iface[interfaces.count++] = iface;
993 		}
994 	}
995 
996 	/*
997 	 * Enable configured interfaces. Depending on channel configuration,
998 	 * this may complete full initialization before returning or use a
999 	 * callback mechanism to complete setup in case of operations like HT
1000 	 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
1001 	 * In such case, the interface will be enabled from eloop context within
1002 	 * hostapd_global_run().
1003 	 */
1004 	interfaces.terminate_on_error = interfaces.count;
1005 	for (i = 0; i < interfaces.count; i++) {
1006 		if (hostapd_driver_init(interfaces.iface[i]) ||
1007 		    hostapd_setup_interface(interfaces.iface[i]))
1008 			goto out;
1009 	}
1010 
1011 	hostapd_global_ctrl_iface_init(&interfaces);
1012 
1013 	if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
1014 		wpa_printf(MSG_ERROR, "Failed to start eloop");
1015 		goto out;
1016 	}
1017 
1018 	ret = 0;
1019 
1020  out:
1021 #ifdef CONFIG_CTRL_IFACE_AIDL
1022 	hostapd_aidl_deinit(&interfaces);
1023 #endif /* CONFIG_CTRL_IFACE_AIDL */
1024 	hostapd_global_ctrl_iface_deinit(&interfaces);
1025 	/* Deinitialize all interfaces */
1026 	for (i = 0; i < interfaces.count; i++) {
1027 		if (!interfaces.iface[i])
1028 			continue;
1029 		interfaces.iface[i]->driver_ap_teardown =
1030 			!!(interfaces.iface[i]->drv_flags &
1031 			   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1032 		hostapd_interface_deinit_free(interfaces.iface[i]);
1033 		interfaces.iface[i] = NULL;
1034 	}
1035 	os_free(interfaces.iface);
1036 	interfaces.iface = NULL;
1037 	interfaces.count = 0;
1038 
1039 #ifdef CONFIG_DPP
1040 	dpp_global_deinit(interfaces.dpp);
1041 #endif /* CONFIG_DPP */
1042 
1043 	if (interfaces.eloop_initialized)
1044 		eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
1045 	hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
1046 	os_free(pid_file);
1047 
1048 	wpa_debug_close_syslog();
1049 	if (log_file)
1050 		wpa_debug_close_file();
1051 	wpa_debug_close_linux_tracing();
1052 
1053 	os_free(bss_config);
1054 
1055 	for (i = 0; i < if_names_size; i++)
1056 		os_free(if_names[i]);
1057 	os_free(if_names);
1058 
1059 	fst_global_deinit();
1060 
1061 	crypto_unload();
1062 	os_program_deinit();
1063 
1064 	return ret;
1065 }
1066