3 * Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
10 #ifndef CONFIG_NATIVE_WINDOWS
13 #endif /* CONFIG_NATIVE_WINDOWS */
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"
30 #include "config_file.h"
31 #include "eap_register.h"
32 #include "ctrl_iface.h"
40 static struct hapd_global global;
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)
47 struct hostapd_data *hapd = ctx;
48 char *format, *module_str;
50 int conf_syslog_level, conf_stdout_level;
51 unsigned int conf_syslog, conf_stdout;
54 format = os_malloc(maxlen);
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;
64 conf_syslog_level = conf_stdout_level = 0;
65 conf_syslog = conf_stdout = (unsigned int) -1;
69 case HOSTAPD_MODULE_IEEE80211:
70 module_str = "IEEE 802.11";
72 case HOSTAPD_MODULE_IEEE8021X:
73 module_str = "IEEE 802.1X";
75 case HOSTAPD_MODULE_RADIUS:
76 module_str = "RADIUS";
78 case HOSTAPD_MODULE_WPA:
81 case HOSTAPD_MODULE_DRIVER:
82 module_str = "DRIVER";
84 case HOSTAPD_MODULE_IAPP:
87 case HOSTAPD_MODULE_MLME:
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 : "",
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);
105 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
106 MAC2STR(addr), module_str ? " " : "",
107 module_str ? module_str : "", txt);
109 os_snprintf(format, maxlen, "%s%s%s",
110 module_str ? module_str : "",
111 module_str ? ": " : "", txt);
113 #ifdef CONFIG_DEBUG_SYSLOG
114 if (wpa_debug_syslog)
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);
122 #ifndef CONFIG_NATIVE_WINDOWS
123 if ((conf_syslog & module) && level >= conf_syslog_level) {
126 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
127 case HOSTAPD_LEVEL_DEBUG:
128 priority = LOG_DEBUG;
130 case HOSTAPD_LEVEL_INFO:
133 case HOSTAPD_LEVEL_NOTICE:
134 priority = LOG_NOTICE;
136 case HOSTAPD_LEVEL_WARNING:
137 priority = LOG_WARNING;
143 syslog(priority, "%s", format);
145 #endif /* CONFIG_NATIVE_WINDOWS */
149 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
153 * hostapd_driver_init - Preparate driver interface
155 static int hostapd_driver_init(struct hostapd_iface *iface)
157 struct wpa_init_params params;
159 struct hostapd_data *hapd = iface->bss[0];
160 struct hostapd_bss_config *conf = hapd->conf;
162 struct wpa_driver_capa capa;
164 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
165 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
169 /* Initialize the driver interface */
170 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
173 os_memset(¶ms, 0, sizeof(params));
174 for (i = 0; wpa_drivers[i]; i++) {
175 if (wpa_drivers[i] != hapd->driver)
178 if (global.drv_priv[i] == NULL &&
179 wpa_drivers[i]->global_init) {
181 wpa_drivers[i]->global_init(iface->interfaces);
182 if (global.drv_priv[i] == NULL) {
183 wpa_printf(MSG_ERROR, "Failed to initialize "
185 wpa_drivers[i]->name);
190 params.global_priv = global.drv_priv[i];
194 params.ifname = hapd->conf->iface;
195 params.driver_params = hapd->iconf->driver_params;
196 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
198 params.num_bridge = hapd->iface->num_bss;
199 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
200 if (params.bridge == NULL)
202 for (i = 0; i < hapd->iface->num_bss; i++) {
203 struct hostapd_data *bss = hapd->iface->bss[i];
204 if (bss->conf->bridge[0])
205 params.bridge[i] = bss->conf->bridge;
208 params.own_addr = hapd->own_addr;
210 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
211 os_free(params.bridge);
212 if (hapd->drv_priv == NULL) {
213 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
219 if (hapd->driver->get_capa &&
220 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
221 struct wowlan_triggers *triggs;
223 iface->drv_flags = capa.flags;
224 iface->smps_modes = capa.smps_modes;
225 iface->probe_resp_offloads = capa.probe_resp_offloads;
227 * Use default extended capa values from per-radio information
229 iface->extended_capa = capa.extended_capa;
230 iface->extended_capa_mask = capa.extended_capa_mask;
231 iface->extended_capa_len = capa.extended_capa_len;
232 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
235 * Override extended capa with per-interface type (AP), if
236 * available from the driver.
238 hostapd_get_ext_capa(iface);
240 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
241 if (triggs && hapd->driver->set_wowlan) {
242 if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
243 wpa_printf(MSG_ERROR, "set_wowlan failed");
253 * hostapd_interface_init - Read configuration file and init BSS data
255 * This function is used to parse configuration file for a full interface (one
256 * or more BSSes sharing the same radio) and allocate memory for the BSS
257 * interfaces. No actual driver operations are started.
259 static struct hostapd_iface *
260 hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
261 const char *config_fname, int debug)
263 struct hostapd_iface *iface;
266 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
267 iface = hostapd_init(interfaces, config_fname);
272 os_strlcpy(iface->conf->bss[0]->iface, if_name,
273 sizeof(iface->conf->bss[0]->iface));
276 iface->interfaces = interfaces;
278 for (k = 0; k < debug; k++) {
279 if (iface->bss[0]->conf->logger_stdout_level > 0)
280 iface->bss[0]->conf->logger_stdout_level--;
283 if (iface->conf->bss[0]->iface[0] == '\0' &&
284 !hostapd_drv_none(iface->bss[0])) {
285 wpa_printf(MSG_ERROR,
286 "Interface name not specified in %s, nor by '-i' parameter",
288 hostapd_interface_deinit_free(iface);
297 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
299 static void handle_term(int sig, void *signal_ctx)
301 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
306 #ifndef CONFIG_NATIVE_WINDOWS
308 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
310 if (hostapd_reload_config(iface) < 0) {
311 wpa_printf(MSG_WARNING, "Failed to read new configuration "
312 "file - continuing with old.");
319 * handle_reload - SIGHUP handler to reload configuration
321 static void handle_reload(int sig, void *signal_ctx)
323 struct hapd_interfaces *interfaces = signal_ctx;
324 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
326 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
330 static void handle_dump_state(int sig, void *signal_ctx)
332 /* Not used anymore - ignore signal */
334 #endif /* CONFIG_NATIVE_WINDOWS */
337 static int hostapd_global_init(struct hapd_interfaces *interfaces,
338 const char *entropy_file)
342 os_memset(&global, 0, sizeof(global));
344 hostapd_logger_register_cb(hostapd_logger_cb);
346 if (eap_server_register_methods()) {
347 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
352 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
355 interfaces->eloop_initialized = 1;
357 random_init(entropy_file);
359 #ifndef CONFIG_NATIVE_WINDOWS
360 eloop_register_signal(SIGHUP, handle_reload, interfaces);
361 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
362 #endif /* CONFIG_NATIVE_WINDOWS */
363 eloop_register_signal_terminate(handle_term, interfaces);
365 #ifndef CONFIG_NATIVE_WINDOWS
366 openlog("hostapd", 0, LOG_DAEMON);
367 #endif /* CONFIG_NATIVE_WINDOWS */
369 for (i = 0; wpa_drivers[i]; i++)
371 if (global.drv_count == 0) {
372 wpa_printf(MSG_ERROR, "No drivers enabled");
375 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
376 if (global.drv_priv == NULL)
383 static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
387 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
388 if (!global.drv_priv[i])
390 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
392 os_free(global.drv_priv);
393 global.drv_priv = NULL;
395 #ifdef EAP_SERVER_TNC
396 tncs_global_deinit();
397 #endif /* EAP_SERVER_TNC */
401 if (eloop_initialized)
404 #ifndef CONFIG_NATIVE_WINDOWS
406 #endif /* CONFIG_NATIVE_WINDOWS */
408 eap_server_unregister_methods();
410 os_daemonize_terminate(pid_file);
414 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
415 const char *pid_file)
417 #ifdef EAP_SERVER_TNC
421 for (i = 0; !tnc && i < ifaces->count; i++) {
422 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
423 if (ifaces->iface[i]->bss[0]->conf->tnc) {
430 if (tnc && tncs_global_init() < 0) {
431 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
434 #endif /* EAP_SERVER_TNC */
437 if (os_daemonize(pid_file)) {
438 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
441 if (eloop_sock_requeue()) {
442 wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
454 static void show_version(void)
457 "hostapd v" VERSION_STR "\n"
458 "User space daemon for IEEE 802.11 AP management,\n"
459 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
460 "Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi> "
461 "and contributors\n");
465 static void usage(void)
470 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
472 " [-g <global ctrl_iface>] [-G <group>]\\\n"
473 " [-i <comma-separated list of interface names>]\\\n"
474 " <configuration file(s)>\n"
477 " -h show this usage\n"
478 " -d show more debug messages (-dd for even more)\n"
479 " -B run daemon in the background\n"
481 " -g global control interface path\n"
482 " -G group for control interfaces\n"
484 " -K include key data in debug messages\n"
485 #ifdef CONFIG_DEBUG_FILE
486 " -f log output to debug file instead of stdout\n"
487 #endif /* CONFIG_DEBUG_FILE */
488 #ifdef CONFIG_DEBUG_LINUX_TRACING
489 " -T record to Linux tracing in addition to logging\n"
490 " (records all messages regardless of debug verbosity)\n"
491 #endif /* CONFIG_DEBUG_LINUX_TRACING */
492 " -i list of interface names to use\n"
493 #ifdef CONFIG_DEBUG_SYSLOG
494 " -s log output to syslog instead of stdout\n"
495 #endif /* CONFIG_DEBUG_SYSLOG */
496 " -S start all the interfaces synchronously\n"
497 " -t include timestamps in some debug messages\n"
498 " -v show hostapd version\n");
504 static const char * hostapd_msg_ifname_cb(void *ctx)
506 struct hostapd_data *hapd = ctx;
507 if (hapd && hapd->conf)
508 return hapd->conf->iface;
513 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
516 #ifndef CONFIG_CTRL_IFACE_UDP
518 #endif /* !CONFIG_CTRL_IFACE_UDP */
520 os_free(interfaces->global_iface_path);
521 interfaces->global_iface_path = os_strdup(path);
522 if (interfaces->global_iface_path == NULL)
525 #ifndef CONFIG_CTRL_IFACE_UDP
526 pos = os_strrchr(interfaces->global_iface_path, '/');
528 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
530 os_free(interfaces->global_iface_path);
531 interfaces->global_iface_path = NULL;
536 interfaces->global_iface_name = pos + 1;
537 #endif /* !CONFIG_CTRL_IFACE_UDP */
543 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
546 #ifndef CONFIG_NATIVE_WINDOWS
548 grp = getgrnam(group);
550 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
553 interfaces->ctrl_iface_group = grp->gr_gid;
554 #endif /* CONFIG_NATIVE_WINDOWS */
559 static int hostapd_get_interface_names(char ***if_names,
560 size_t *if_names_size,
563 char *if_name, *tmp, **nnames;
568 if_name = strtok_r(arg, ",", &tmp);
571 nnames = os_realloc_array(*if_names, 1 + *if_names_size,
577 (*if_names)[*if_names_size] = os_strdup(if_name);
578 if (!(*if_names)[*if_names_size])
581 if_name = strtok_r(NULL, ",", &tmp);
587 for (i = 0; i < *if_names_size; i++)
588 os_free((*if_names)[i]);
597 static int gen_uuid(const char *txt_addr)
603 if (hwaddr_aton(txt_addr, addr) < 0)
606 uuid_gen_mac_addr(addr, uuid);
607 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
614 #endif /* CONFIG_WPS */
617 #ifndef HOSTAPD_CLEANUP_INTERVAL
618 #define HOSTAPD_CLEANUP_INTERVAL 10
619 #endif /* HOSTAPD_CLEANUP_INTERVAL */
621 static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
623 hostapd_periodic_iface(iface);
628 /* Periodic cleanup tasks */
629 static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
631 struct hapd_interfaces *interfaces = eloop_ctx;
633 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
634 hostapd_periodic, interfaces, NULL);
635 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
639 int main(int argc, char *argv[])
641 struct hapd_interfaces interfaces;
644 int c, debug = 0, daemonize = 0;
645 char *pid_file = NULL;
646 const char *log_file = NULL;
647 const char *entropy_file = NULL;
648 char **bss_config = NULL, **tmp_bss;
649 size_t num_bss_configs = 0;
650 #ifdef CONFIG_DEBUG_LINUX_TRACING
651 int enable_trace_dbg = 0;
652 #endif /* CONFIG_DEBUG_LINUX_TRACING */
653 int start_ifaces_in_sync = 0;
654 char **if_names = NULL;
655 size_t if_names_size = 0;
657 if (os_program_init())
660 os_memset(&interfaces, 0, sizeof(interfaces));
661 interfaces.reload_config = hostapd_reload_config;
662 interfaces.config_read_cb = hostapd_config_read;
663 interfaces.for_each_interface = hostapd_for_each_interface;
664 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
665 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
666 interfaces.driver_init = hostapd_driver_init;
667 interfaces.global_iface_path = NULL;
668 interfaces.global_iface_name = NULL;
669 interfaces.global_ctrl_sock = -1;
670 dl_list_init(&interfaces.global_ctrl_dst);
671 #ifdef CONFIG_ETH_P_OUI
672 dl_list_init(&interfaces.eth_p_oui);
673 #endif /* CONFIG_ETH_P_OUI */
675 interfaces.dpp = dpp_global_init();
678 #endif /* CONFIG_DPP */
681 c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:");
690 if (wpa_debug_level > 0)
697 entropy_file = optarg;
703 wpa_debug_show_keys++;
707 pid_file = os_rel2abs_path(optarg);
710 wpa_debug_timestamp++;
712 #ifdef CONFIG_DEBUG_LINUX_TRACING
714 enable_trace_dbg = 1;
716 #endif /* CONFIG_DEBUG_LINUX_TRACING */
722 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
726 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
730 tmp_bss = os_realloc_array(bss_config,
735 bss_config = tmp_bss;
736 bss_config[num_bss_configs++] = optarg;
738 #ifdef CONFIG_DEBUG_SYSLOG
740 wpa_debug_syslog = 1;
742 #endif /* CONFIG_DEBUG_SYSLOG */
744 start_ifaces_in_sync = 1;
748 return gen_uuid(optarg);
749 #endif /* CONFIG_WPS */
751 if (hostapd_get_interface_names(&if_names,
752 &if_names_size, optarg))
761 if (optind == argc && interfaces.global_iface_path == NULL &&
762 num_bss_configs == 0)
765 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
768 wpa_debug_open_file(log_file);
770 wpa_debug_setup_stdout();
771 #ifdef CONFIG_DEBUG_SYSLOG
772 if (wpa_debug_syslog)
773 wpa_debug_open_syslog();
774 #endif /* CONFIG_DEBUG_SYSLOG */
775 #ifdef CONFIG_DEBUG_LINUX_TRACING
776 if (enable_trace_dbg) {
777 int tret = wpa_debug_open_linux_tracing();
779 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
783 #endif /* CONFIG_DEBUG_LINUX_TRACING */
785 interfaces.count = argc - optind;
786 if (interfaces.count || num_bss_configs) {
787 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
788 sizeof(struct hostapd_iface *));
789 if (interfaces.iface == NULL) {
790 wpa_printf(MSG_ERROR, "malloc failed");
795 if (hostapd_global_init(&interfaces, entropy_file)) {
796 wpa_printf(MSG_ERROR, "Failed to initialize global context");
800 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
801 hostapd_periodic, &interfaces, NULL);
803 if (fst_global_init()) {
804 wpa_printf(MSG_ERROR,
805 "Failed to initialize global FST context");
809 #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
810 if (!fst_global_add_ctrl(fst_ctrl_cli))
811 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
812 #endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
814 /* Allocate and parse configuration for full interface files */
815 for (i = 0; i < interfaces.count; i++) {
816 char *if_name = NULL;
818 if (i < if_names_size)
819 if_name = if_names[i];
821 interfaces.iface[i] = hostapd_interface_init(&interfaces,
825 if (!interfaces.iface[i]) {
826 wpa_printf(MSG_ERROR, "Failed to initialize interface");
829 if (start_ifaces_in_sync)
830 interfaces.iface[i]->need_to_start_in_sync = 1;
833 /* Allocate and parse configuration for per-BSS files */
834 for (i = 0; i < num_bss_configs; i++) {
835 struct hostapd_iface *iface;
838 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
839 fname = os_strchr(bss_config[i], ':');
841 wpa_printf(MSG_ERROR,
842 "Invalid BSS config identifier '%s'",
847 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
851 for (j = 0; j < interfaces.count; j++) {
852 if (interfaces.iface[j] == iface)
855 if (j == interfaces.count) {
856 struct hostapd_iface **tmp;
857 tmp = os_realloc_array(interfaces.iface,
858 interfaces.count + 1,
859 sizeof(struct hostapd_iface *));
861 hostapd_interface_deinit_free(iface);
864 interfaces.iface = tmp;
865 interfaces.iface[interfaces.count++] = iface;
870 * Enable configured interfaces. Depending on channel configuration,
871 * this may complete full initialization before returning or use a
872 * callback mechanism to complete setup in case of operations like HT
873 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
874 * In such case, the interface will be enabled from eloop context within
875 * hostapd_global_run().
877 interfaces.terminate_on_error = interfaces.count;
878 for (i = 0; i < interfaces.count; i++) {
879 if (hostapd_driver_init(interfaces.iface[i]) ||
880 hostapd_setup_interface(interfaces.iface[i]))
884 hostapd_global_ctrl_iface_init(&interfaces);
886 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
887 wpa_printf(MSG_ERROR, "Failed to start eloop");
894 hostapd_global_ctrl_iface_deinit(&interfaces);
895 /* Deinitialize all interfaces */
896 for (i = 0; i < interfaces.count; i++) {
897 if (!interfaces.iface[i])
899 interfaces.iface[i]->driver_ap_teardown =
900 !!(interfaces.iface[i]->drv_flags &
901 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
902 hostapd_interface_deinit_free(interfaces.iface[i]);
904 os_free(interfaces.iface);
907 dpp_global_deinit(interfaces.dpp);
908 #endif /* CONFIG_DPP */
910 if (interfaces.eloop_initialized)
911 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
912 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
915 wpa_debug_close_syslog();
917 wpa_debug_close_file();
918 wpa_debug_close_linux_tracing();
922 for (i = 0; i < if_names_size; i++)
923 os_free(if_names[i]);