3 * Copyright (c) 2002-2016, 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 "drivers/driver.h"
22 #include "eap_server/eap.h"
23 #include "eap_server/tncs.h"
24 #include "ap/hostapd.h"
25 #include "ap/ap_config.h"
26 #include "ap/ap_drv_ops.h"
28 #include "config_file.h"
29 #include "eap_register.h"
30 #include "ctrl_iface.h"
38 static struct hapd_global global;
41 #ifndef CONFIG_NO_HOSTAPD_LOGGER
42 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
43 int level, const char *txt, size_t len)
45 struct hostapd_data *hapd = ctx;
46 char *format, *module_str;
48 int conf_syslog_level, conf_stdout_level;
49 unsigned int conf_syslog, conf_stdout;
52 format = os_malloc(maxlen);
56 if (hapd && hapd->conf) {
57 conf_syslog_level = hapd->conf->logger_syslog_level;
58 conf_stdout_level = hapd->conf->logger_stdout_level;
59 conf_syslog = hapd->conf->logger_syslog;
60 conf_stdout = hapd->conf->logger_stdout;
62 conf_syslog_level = conf_stdout_level = 0;
63 conf_syslog = conf_stdout = (unsigned int) -1;
67 case HOSTAPD_MODULE_IEEE80211:
68 module_str = "IEEE 802.11";
70 case HOSTAPD_MODULE_IEEE8021X:
71 module_str = "IEEE 802.1X";
73 case HOSTAPD_MODULE_RADIUS:
74 module_str = "RADIUS";
76 case HOSTAPD_MODULE_WPA:
79 case HOSTAPD_MODULE_DRIVER:
80 module_str = "DRIVER";
82 case HOSTAPD_MODULE_IAPP:
85 case HOSTAPD_MODULE_MLME:
93 if (hapd && hapd->conf && addr)
94 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
95 hapd->conf->iface, MAC2STR(addr),
96 module_str ? " " : "", module_str ? module_str : "",
98 else if (hapd && hapd->conf)
99 os_snprintf(format, maxlen, "%s:%s%s %s",
100 hapd->conf->iface, module_str ? " " : "",
101 module_str ? module_str : "", txt);
103 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
104 MAC2STR(addr), module_str ? " " : "",
105 module_str ? module_str : "", txt);
107 os_snprintf(format, maxlen, "%s%s%s",
108 module_str ? module_str : "",
109 module_str ? ": " : "", txt);
111 if ((conf_stdout & module) && level >= conf_stdout_level) {
112 wpa_debug_print_timestamp();
113 wpa_printf(MSG_INFO, "%s", format);
116 #ifndef CONFIG_NATIVE_WINDOWS
117 if ((conf_syslog & module) && level >= conf_syslog_level) {
120 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
121 case HOSTAPD_LEVEL_DEBUG:
122 priority = LOG_DEBUG;
124 case HOSTAPD_LEVEL_INFO:
127 case HOSTAPD_LEVEL_NOTICE:
128 priority = LOG_NOTICE;
130 case HOSTAPD_LEVEL_WARNING:
131 priority = LOG_WARNING;
137 syslog(priority, "%s", format);
139 #endif /* CONFIG_NATIVE_WINDOWS */
143 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
147 * hostapd_driver_init - Preparate driver interface
149 static int hostapd_driver_init(struct hostapd_iface *iface)
151 struct wpa_init_params params;
153 struct hostapd_data *hapd = iface->bss[0];
154 struct hostapd_bss_config *conf = hapd->conf;
156 struct wpa_driver_capa capa;
158 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
159 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
163 /* Initialize the driver interface */
164 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
167 os_memset(¶ms, 0, sizeof(params));
168 for (i = 0; wpa_drivers[i]; i++) {
169 if (wpa_drivers[i] != hapd->driver)
172 if (global.drv_priv[i] == NULL &&
173 wpa_drivers[i]->global_init) {
175 wpa_drivers[i]->global_init(iface->interfaces);
176 if (global.drv_priv[i] == NULL) {
177 wpa_printf(MSG_ERROR, "Failed to initialize "
179 wpa_drivers[i]->name);
184 params.global_priv = global.drv_priv[i];
188 params.ifname = hapd->conf->iface;
189 params.driver_params = hapd->iconf->driver_params;
190 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
192 params.num_bridge = hapd->iface->num_bss;
193 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
194 if (params.bridge == NULL)
196 for (i = 0; i < hapd->iface->num_bss; i++) {
197 struct hostapd_data *bss = hapd->iface->bss[i];
198 if (bss->conf->bridge[0])
199 params.bridge[i] = bss->conf->bridge;
202 params.own_addr = hapd->own_addr;
204 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
205 os_free(params.bridge);
206 if (hapd->drv_priv == NULL) {
207 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
213 if (hapd->driver->get_capa &&
214 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
215 struct wowlan_triggers *triggs;
217 iface->drv_flags = capa.flags;
218 iface->smps_modes = capa.smps_modes;
219 iface->probe_resp_offloads = capa.probe_resp_offloads;
220 iface->extended_capa = capa.extended_capa;
221 iface->extended_capa_mask = capa.extended_capa_mask;
222 iface->extended_capa_len = capa.extended_capa_len;
223 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
225 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
226 if (triggs && hapd->driver->set_wowlan) {
227 if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
228 wpa_printf(MSG_ERROR, "set_wowlan failed");
238 * hostapd_interface_init - Read configuration file and init BSS data
240 * This function is used to parse configuration file for a full interface (one
241 * or more BSSes sharing the same radio) and allocate memory for the BSS
242 * interfaces. No actiual driver operations are started.
244 static struct hostapd_iface *
245 hostapd_interface_init(struct hapd_interfaces *interfaces,
246 const char *config_fname, int debug)
248 struct hostapd_iface *iface;
251 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
252 iface = hostapd_init(interfaces, config_fname);
255 iface->interfaces = interfaces;
257 for (k = 0; k < debug; k++) {
258 if (iface->bss[0]->conf->logger_stdout_level > 0)
259 iface->bss[0]->conf->logger_stdout_level--;
262 if (iface->conf->bss[0]->iface[0] == '\0' &&
263 !hostapd_drv_none(iface->bss[0])) {
264 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
266 hostapd_interface_deinit_free(iface);
275 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
277 static void handle_term(int sig, void *signal_ctx)
279 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
284 #ifndef CONFIG_NATIVE_WINDOWS
286 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
288 if (hostapd_reload_config(iface) < 0) {
289 wpa_printf(MSG_WARNING, "Failed to read new configuration "
290 "file - continuing with old.");
297 * handle_reload - SIGHUP handler to reload configuration
299 static void handle_reload(int sig, void *signal_ctx)
301 struct hapd_interfaces *interfaces = signal_ctx;
302 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
304 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
308 static void handle_dump_state(int sig, void *signal_ctx)
310 /* Not used anymore - ignore signal */
312 #endif /* CONFIG_NATIVE_WINDOWS */
315 static int hostapd_global_init(struct hapd_interfaces *interfaces,
316 const char *entropy_file)
320 os_memset(&global, 0, sizeof(global));
322 hostapd_logger_register_cb(hostapd_logger_cb);
324 if (eap_server_register_methods()) {
325 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
330 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
334 random_init(entropy_file);
336 #ifndef CONFIG_NATIVE_WINDOWS
337 eloop_register_signal(SIGHUP, handle_reload, interfaces);
338 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
339 #endif /* CONFIG_NATIVE_WINDOWS */
340 eloop_register_signal_terminate(handle_term, interfaces);
342 #ifndef CONFIG_NATIVE_WINDOWS
343 openlog("hostapd", 0, LOG_DAEMON);
344 #endif /* CONFIG_NATIVE_WINDOWS */
346 for (i = 0; wpa_drivers[i]; i++)
348 if (global.drv_count == 0) {
349 wpa_printf(MSG_ERROR, "No drivers enabled");
352 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
353 if (global.drv_priv == NULL)
360 static void hostapd_global_deinit(const char *pid_file)
364 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
365 if (!global.drv_priv[i])
367 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
369 os_free(global.drv_priv);
370 global.drv_priv = NULL;
372 #ifdef EAP_SERVER_TNC
373 tncs_global_deinit();
374 #endif /* EAP_SERVER_TNC */
380 #ifndef CONFIG_NATIVE_WINDOWS
382 #endif /* CONFIG_NATIVE_WINDOWS */
384 eap_server_unregister_methods();
386 os_daemonize_terminate(pid_file);
390 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
391 const char *pid_file)
393 #ifdef EAP_SERVER_TNC
397 for (i = 0; !tnc && i < ifaces->count; i++) {
398 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
399 if (ifaces->iface[i]->bss[0]->conf->tnc) {
406 if (tnc && tncs_global_init() < 0) {
407 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
410 #endif /* EAP_SERVER_TNC */
413 if (os_daemonize(pid_file)) {
414 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
417 if (eloop_sock_requeue()) {
418 wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
430 static void show_version(void)
433 "hostapd v" VERSION_STR "\n"
434 "User space daemon for IEEE 802.11 AP management,\n"
435 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
436 "Copyright (c) 2002-2016, Jouni Malinen <j@w1.fi> "
437 "and contributors\n");
441 static void usage(void)
446 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
448 " [-g <global ctrl_iface>] [-G <group>] \\\n"
449 " <configuration file(s)>\n"
452 " -h show this usage\n"
453 " -d show more debug messages (-dd for even more)\n"
454 " -B run daemon in the background\n"
456 " -g global control interface path\n"
457 " -G group for control interfaces\n"
459 " -K include key data in debug messages\n"
460 #ifdef CONFIG_DEBUG_FILE
461 " -f log output to debug file instead of stdout\n"
462 #endif /* CONFIG_DEBUG_FILE */
463 #ifdef CONFIG_DEBUG_LINUX_TRACING
464 " -T = record to Linux tracing in addition to logging\n"
465 " (records all messages regardless of debug verbosity)\n"
466 #endif /* CONFIG_DEBUG_LINUX_TRACING */
467 " -S start all the interfaces synchronously\n"
468 " -t include timestamps in some debug messages\n"
469 " -v show hostapd version\n");
475 static const char * hostapd_msg_ifname_cb(void *ctx)
477 struct hostapd_data *hapd = ctx;
478 if (hapd && hapd->conf)
479 return hapd->conf->iface;
484 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
487 #ifndef CONFIG_CTRL_IFACE_UDP
489 #endif /* !CONFIG_CTRL_IFACE_UDP */
491 os_free(interfaces->global_iface_path);
492 interfaces->global_iface_path = os_strdup(path);
493 if (interfaces->global_iface_path == NULL)
496 #ifndef CONFIG_CTRL_IFACE_UDP
497 pos = os_strrchr(interfaces->global_iface_path, '/');
499 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
501 os_free(interfaces->global_iface_path);
502 interfaces->global_iface_path = NULL;
507 interfaces->global_iface_name = pos + 1;
508 #endif /* !CONFIG_CTRL_IFACE_UDP */
514 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
517 #ifndef CONFIG_NATIVE_WINDOWS
519 grp = getgrnam(group);
521 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
524 interfaces->ctrl_iface_group = grp->gr_gid;
525 #endif /* CONFIG_NATIVE_WINDOWS */
531 static int gen_uuid(const char *txt_addr)
537 if (hwaddr_aton(txt_addr, addr) < 0)
540 uuid_gen_mac_addr(addr, uuid);
541 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
548 #endif /* CONFIG_WPS */
551 #ifndef HOSTAPD_CLEANUP_INTERVAL
552 #define HOSTAPD_CLEANUP_INTERVAL 10
553 #endif /* HOSTAPD_CLEANUP_INTERVAL */
555 static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
557 hostapd_periodic_iface(iface);
562 /* Periodic cleanup tasks */
563 static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
565 struct hapd_interfaces *interfaces = eloop_ctx;
567 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
568 hostapd_periodic, interfaces, NULL);
569 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
573 int main(int argc, char *argv[])
575 struct hapd_interfaces interfaces;
578 int c, debug = 0, daemonize = 0;
579 char *pid_file = NULL;
580 const char *log_file = NULL;
581 const char *entropy_file = NULL;
582 char **bss_config = NULL, **tmp_bss;
583 size_t num_bss_configs = 0;
584 #ifdef CONFIG_DEBUG_LINUX_TRACING
585 int enable_trace_dbg = 0;
586 #endif /* CONFIG_DEBUG_LINUX_TRACING */
587 int start_ifaces_in_sync = 0;
589 if (os_program_init())
592 os_memset(&interfaces, 0, sizeof(interfaces));
593 interfaces.reload_config = hostapd_reload_config;
594 interfaces.config_read_cb = hostapd_config_read;
595 interfaces.for_each_interface = hostapd_for_each_interface;
596 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
597 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
598 interfaces.driver_init = hostapd_driver_init;
599 interfaces.global_iface_path = NULL;
600 interfaces.global_iface_name = NULL;
601 interfaces.global_ctrl_sock = -1;
602 dl_list_init(&interfaces.global_ctrl_dst);
605 c = getopt(argc, argv, "b:Bde:f:hKP:STtu:vg:G:");
614 if (wpa_debug_level > 0)
621 entropy_file = optarg;
627 wpa_debug_show_keys++;
631 pid_file = os_rel2abs_path(optarg);
634 wpa_debug_timestamp++;
636 #ifdef CONFIG_DEBUG_LINUX_TRACING
638 enable_trace_dbg = 1;
640 #endif /* CONFIG_DEBUG_LINUX_TRACING */
646 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
650 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
654 tmp_bss = os_realloc_array(bss_config,
659 bss_config = tmp_bss;
660 bss_config[num_bss_configs++] = optarg;
663 start_ifaces_in_sync = 1;
667 return gen_uuid(optarg);
668 #endif /* CONFIG_WPS */
675 if (optind == argc && interfaces.global_iface_path == NULL &&
676 num_bss_configs == 0)
679 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
682 wpa_debug_open_file(log_file);
684 wpa_debug_setup_stdout();
685 #ifdef CONFIG_DEBUG_LINUX_TRACING
686 if (enable_trace_dbg) {
687 int tret = wpa_debug_open_linux_tracing();
689 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
693 #endif /* CONFIG_DEBUG_LINUX_TRACING */
695 interfaces.count = argc - optind;
696 if (interfaces.count || num_bss_configs) {
697 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
698 sizeof(struct hostapd_iface *));
699 if (interfaces.iface == NULL) {
700 wpa_printf(MSG_ERROR, "malloc failed");
705 if (hostapd_global_init(&interfaces, entropy_file)) {
706 wpa_printf(MSG_ERROR, "Failed to initialize global context");
710 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
711 hostapd_periodic, &interfaces, NULL);
713 if (fst_global_init()) {
714 wpa_printf(MSG_ERROR,
715 "Failed to initialize global FST context");
719 #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
720 if (!fst_global_add_ctrl(fst_ctrl_cli))
721 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
722 #endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
724 /* Allocate and parse configuration for full interface files */
725 for (i = 0; i < interfaces.count; i++) {
726 interfaces.iface[i] = hostapd_interface_init(&interfaces,
729 if (!interfaces.iface[i]) {
730 wpa_printf(MSG_ERROR, "Failed to initialize interface");
733 if (start_ifaces_in_sync)
734 interfaces.iface[i]->need_to_start_in_sync = 1;
737 /* Allocate and parse configuration for per-BSS files */
738 for (i = 0; i < num_bss_configs; i++) {
739 struct hostapd_iface *iface;
742 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
743 fname = os_strchr(bss_config[i], ':');
745 wpa_printf(MSG_ERROR,
746 "Invalid BSS config identifier '%s'",
751 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
755 for (j = 0; j < interfaces.count; j++) {
756 if (interfaces.iface[j] == iface)
759 if (j == interfaces.count) {
760 struct hostapd_iface **tmp;
761 tmp = os_realloc_array(interfaces.iface,
762 interfaces.count + 1,
763 sizeof(struct hostapd_iface *));
765 hostapd_interface_deinit_free(iface);
768 interfaces.iface = tmp;
769 interfaces.iface[interfaces.count++] = iface;
774 * Enable configured interfaces. Depending on channel configuration,
775 * this may complete full initialization before returning or use a
776 * callback mechanism to complete setup in case of operations like HT
777 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
778 * In such case, the interface will be enabled from eloop context within
779 * hostapd_global_run().
781 interfaces.terminate_on_error = interfaces.count;
782 for (i = 0; i < interfaces.count; i++) {
783 if (hostapd_driver_init(interfaces.iface[i]) ||
784 hostapd_setup_interface(interfaces.iface[i]))
788 hostapd_global_ctrl_iface_init(&interfaces);
790 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
791 wpa_printf(MSG_ERROR, "Failed to start eloop");
798 hostapd_global_ctrl_iface_deinit(&interfaces);
799 /* Deinitialize all interfaces */
800 for (i = 0; i < interfaces.count; i++) {
801 if (!interfaces.iface[i])
803 interfaces.iface[i]->driver_ap_teardown =
804 !!(interfaces.iface[i]->drv_flags &
805 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
806 hostapd_interface_deinit_free(interfaces.iface[i]);
808 os_free(interfaces.iface);
810 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
811 hostapd_global_deinit(pid_file);
815 wpa_debug_close_file();
816 wpa_debug_close_linux_tracing();