3 * Copyright (c) 2002-2011, 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"
27 #include "config_file.h"
28 #include "eap_register.h"
29 #include "ctrl_iface.h"
37 static struct hapd_global global;
40 #ifndef CONFIG_NO_HOSTAPD_LOGGER
41 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
42 int level, const char *txt, size_t len)
44 struct hostapd_data *hapd = ctx;
45 char *format, *module_str;
47 int conf_syslog_level, conf_stdout_level;
48 unsigned int conf_syslog, conf_stdout;
51 format = os_malloc(maxlen);
55 if (hapd && hapd->conf) {
56 conf_syslog_level = hapd->conf->logger_syslog_level;
57 conf_stdout_level = hapd->conf->logger_stdout_level;
58 conf_syslog = hapd->conf->logger_syslog;
59 conf_stdout = hapd->conf->logger_stdout;
61 conf_syslog_level = conf_stdout_level = 0;
62 conf_syslog = conf_stdout = (unsigned int) -1;
66 case HOSTAPD_MODULE_IEEE80211:
67 module_str = "IEEE 802.11";
69 case HOSTAPD_MODULE_IEEE8021X:
70 module_str = "IEEE 802.1X";
72 case HOSTAPD_MODULE_RADIUS:
73 module_str = "RADIUS";
75 case HOSTAPD_MODULE_WPA:
78 case HOSTAPD_MODULE_DRIVER:
79 module_str = "DRIVER";
81 case HOSTAPD_MODULE_IAPP:
84 case HOSTAPD_MODULE_MLME:
92 if (hapd && hapd->conf && addr)
93 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
94 hapd->conf->iface, MAC2STR(addr),
95 module_str ? " " : "", module_str ? module_str : "",
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);
102 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
103 MAC2STR(addr), module_str ? " " : "",
104 module_str ? module_str : "", txt);
106 os_snprintf(format, maxlen, "%s%s%s",
107 module_str ? module_str : "",
108 module_str ? ": " : "", txt);
110 if ((conf_stdout & module) && level >= conf_stdout_level) {
111 wpa_debug_print_timestamp();
112 wpa_printf(MSG_INFO, "%s", format);
115 #ifndef CONFIG_NATIVE_WINDOWS
116 if ((conf_syslog & module) && level >= conf_syslog_level) {
119 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
120 case HOSTAPD_LEVEL_DEBUG:
121 priority = LOG_DEBUG;
123 case HOSTAPD_LEVEL_INFO:
126 case HOSTAPD_LEVEL_NOTICE:
127 priority = LOG_NOTICE;
129 case HOSTAPD_LEVEL_WARNING:
130 priority = LOG_WARNING;
136 syslog(priority, "%s", format);
138 #endif /* CONFIG_NATIVE_WINDOWS */
142 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
146 * hostapd_driver_init - Preparate driver interface
148 static int hostapd_driver_init(struct hostapd_iface *iface)
150 struct wpa_init_params params;
152 struct hostapd_data *hapd = iface->bss[0];
153 struct hostapd_bss_config *conf = hapd->conf;
155 struct wpa_driver_capa capa;
157 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
158 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
162 /* Initialize the driver interface */
163 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
166 os_memset(¶ms, 0, sizeof(params));
167 for (i = 0; wpa_drivers[i]; i++) {
168 if (wpa_drivers[i] != hapd->driver)
171 if (global.drv_priv[i] == NULL &&
172 wpa_drivers[i]->global_init) {
173 global.drv_priv[i] = wpa_drivers[i]->global_init();
174 if (global.drv_priv[i] == NULL) {
175 wpa_printf(MSG_ERROR, "Failed to initialize "
177 wpa_drivers[i]->name);
182 params.global_priv = global.drv_priv[i];
186 params.ifname = hapd->conf->iface;
187 params.ssid = hapd->conf->ssid.ssid;
188 params.ssid_len = hapd->conf->ssid.ssid_len;
189 params.test_socket = hapd->conf->test_socket;
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 iface->drv_flags = capa.flags;
216 iface->probe_resp_offloads = capa.probe_resp_offloads;
217 iface->extended_capa = capa.extended_capa;
218 iface->extended_capa_mask = capa.extended_capa_mask;
219 iface->extended_capa_len = capa.extended_capa_len;
220 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
228 * hostapd_interface_init - Read configuration file and init BSS data
230 * This function is used to parse configuration file for a full interface (one
231 * or more BSSes sharing the same radio) and allocate memory for the BSS
232 * interfaces. No actiual driver operations are started.
234 static struct hostapd_iface *
235 hostapd_interface_init(struct hapd_interfaces *interfaces,
236 const char *config_fname, int debug)
238 struct hostapd_iface *iface;
241 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
242 iface = hostapd_init(interfaces, config_fname);
245 iface->interfaces = interfaces;
247 for (k = 0; k < debug; k++) {
248 if (iface->bss[0]->conf->logger_stdout_level > 0)
249 iface->bss[0]->conf->logger_stdout_level--;
252 if (iface->conf->bss[0]->iface[0] == '\0' &&
253 !hostapd_drv_none(iface->bss[0])) {
254 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
256 hostapd_interface_deinit_free(iface);
265 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
267 static void handle_term(int sig, void *signal_ctx)
269 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
274 #ifndef CONFIG_NATIVE_WINDOWS
276 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
278 if (hostapd_reload_config(iface) < 0) {
279 wpa_printf(MSG_WARNING, "Failed to read new configuration "
280 "file - continuing with old.");
287 * handle_reload - SIGHUP handler to reload configuration
289 static void handle_reload(int sig, void *signal_ctx)
291 struct hapd_interfaces *interfaces = signal_ctx;
292 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
294 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
298 static void handle_dump_state(int sig, void *signal_ctx)
300 /* Not used anymore - ignore signal */
302 #endif /* CONFIG_NATIVE_WINDOWS */
305 static int hostapd_global_init(struct hapd_interfaces *interfaces,
306 const char *entropy_file)
310 os_memset(&global, 0, sizeof(global));
312 hostapd_logger_register_cb(hostapd_logger_cb);
314 if (eap_server_register_methods()) {
315 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
320 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
324 random_init(entropy_file);
326 #ifndef CONFIG_NATIVE_WINDOWS
327 eloop_register_signal(SIGHUP, handle_reload, interfaces);
328 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
329 #endif /* CONFIG_NATIVE_WINDOWS */
330 eloop_register_signal_terminate(handle_term, interfaces);
332 #ifndef CONFIG_NATIVE_WINDOWS
333 openlog("hostapd", 0, LOG_DAEMON);
334 #endif /* CONFIG_NATIVE_WINDOWS */
336 for (i = 0; wpa_drivers[i]; i++)
338 if (global.drv_count == 0) {
339 wpa_printf(MSG_ERROR, "No drivers enabled");
342 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
343 if (global.drv_priv == NULL)
350 static void hostapd_global_deinit(const char *pid_file)
354 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
355 if (!global.drv_priv[i])
357 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
359 os_free(global.drv_priv);
360 global.drv_priv = NULL;
362 #ifdef EAP_SERVER_TNC
363 tncs_global_deinit();
364 #endif /* EAP_SERVER_TNC */
370 #ifndef CONFIG_NATIVE_WINDOWS
372 #endif /* CONFIG_NATIVE_WINDOWS */
374 eap_server_unregister_methods();
376 os_daemonize_terminate(pid_file);
380 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
381 const char *pid_file)
383 #ifdef EAP_SERVER_TNC
387 for (i = 0; !tnc && i < ifaces->count; i++) {
388 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
389 if (ifaces->iface[i]->bss[0]->conf->tnc) {
396 if (tnc && tncs_global_init() < 0) {
397 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
400 #endif /* EAP_SERVER_TNC */
402 if (daemonize && os_daemonize(pid_file)) {
413 static void show_version(void)
416 "hostapd v" VERSION_STR "\n"
417 "User space daemon for IEEE 802.11 AP management,\n"
418 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
419 "Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi> "
420 "and contributors\n");
424 static void usage(void)
429 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
431 " [-g <global ctrl_iface>] [-G <group>] \\\n"
432 " <configuration file(s)>\n"
435 " -h show this usage\n"
436 " -d show more debug messages (-dd for even more)\n"
437 " -B run daemon in the background\n"
439 " -g global control interface path\n"
440 " -G group for control interfaces\n"
442 " -K include key data in debug messages\n"
443 #ifdef CONFIG_DEBUG_FILE
444 " -f log output to debug file instead of stdout\n"
445 #endif /* CONFIG_DEBUG_FILE */
446 #ifdef CONFIG_DEBUG_LINUX_TRACING
447 " -T = record to Linux tracing in addition to logging\n"
448 " (records all messages regardless of debug verbosity)\n"
449 #endif /* CONFIG_DEBUG_LINUX_TRACING */
450 " -t include timestamps in some debug messages\n"
451 " -v show hostapd version\n");
457 static const char * hostapd_msg_ifname_cb(void *ctx)
459 struct hostapd_data *hapd = ctx;
460 if (hapd && hapd->iconf && hapd->iconf->bss &&
461 hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
462 return hapd->iconf->bss[0]->iface;
467 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
471 os_free(interfaces->global_iface_path);
472 interfaces->global_iface_path = os_strdup(path);
473 if (interfaces->global_iface_path == NULL)
475 pos = os_strrchr(interfaces->global_iface_path, '/');
477 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
479 os_free(interfaces->global_iface_path);
480 interfaces->global_iface_path = NULL;
485 interfaces->global_iface_name = pos + 1;
491 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
494 #ifndef CONFIG_NATIVE_WINDOWS
496 grp = getgrnam(group);
498 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
501 interfaces->ctrl_iface_group = grp->gr_gid;
502 #endif /* CONFIG_NATIVE_WINDOWS */
508 static int gen_uuid(const char *txt_addr)
514 if (hwaddr_aton(txt_addr, addr) < 0)
517 uuid_gen_mac_addr(addr, uuid);
518 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
525 #endif /* CONFIG_WPS */
528 int main(int argc, char *argv[])
530 struct hapd_interfaces interfaces;
533 int c, debug = 0, daemonize = 0;
534 char *pid_file = NULL;
535 const char *log_file = NULL;
536 const char *entropy_file = NULL;
537 char **bss_config = NULL, **tmp_bss;
538 size_t num_bss_configs = 0;
539 #ifdef CONFIG_DEBUG_LINUX_TRACING
540 int enable_trace_dbg = 0;
541 #endif /* CONFIG_DEBUG_LINUX_TRACING */
543 if (os_program_init())
546 os_memset(&interfaces, 0, sizeof(interfaces));
547 interfaces.reload_config = hostapd_reload_config;
548 interfaces.config_read_cb = hostapd_config_read;
549 interfaces.for_each_interface = hostapd_for_each_interface;
550 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
551 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
552 interfaces.driver_init = hostapd_driver_init;
553 interfaces.global_iface_path = NULL;
554 interfaces.global_iface_name = NULL;
555 interfaces.global_ctrl_sock = -1;
558 c = getopt(argc, argv, "b:Bde:f:hKP:Ttu:vg:G:");
567 if (wpa_debug_level > 0)
574 entropy_file = optarg;
580 wpa_debug_show_keys++;
584 pid_file = os_rel2abs_path(optarg);
587 wpa_debug_timestamp++;
589 #ifdef CONFIG_DEBUG_LINUX_TRACING
591 enable_trace_dbg = 1;
593 #endif /* CONFIG_DEBUG_LINUX_TRACING */
599 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
603 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
607 tmp_bss = os_realloc_array(bss_config,
612 bss_config = tmp_bss;
613 bss_config[num_bss_configs++] = optarg;
617 return gen_uuid(optarg);
618 #endif /* CONFIG_WPS */
625 if (optind == argc && interfaces.global_iface_path == NULL &&
626 num_bss_configs == 0)
629 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
632 wpa_debug_open_file(log_file);
633 #ifdef CONFIG_DEBUG_LINUX_TRACING
634 if (enable_trace_dbg) {
635 int tret = wpa_debug_open_linux_tracing();
637 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
641 #endif /* CONFIG_DEBUG_LINUX_TRACING */
643 interfaces.count = argc - optind;
644 if (interfaces.count || num_bss_configs) {
645 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
646 sizeof(struct hostapd_iface *));
647 if (interfaces.iface == NULL) {
648 wpa_printf(MSG_ERROR, "malloc failed");
653 if (hostapd_global_init(&interfaces, entropy_file)) {
654 wpa_printf(MSG_ERROR, "Failed to initilize global context");
658 /* Allocate and parse configuration for full interface files */
659 for (i = 0; i < interfaces.count; i++) {
660 interfaces.iface[i] = hostapd_interface_init(&interfaces,
663 if (!interfaces.iface[i]) {
664 wpa_printf(MSG_ERROR, "Failed to initialize interface");
669 /* Allocate and parse configuration for per-BSS files */
670 for (i = 0; i < num_bss_configs; i++) {
671 struct hostapd_iface *iface;
674 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
675 fname = os_strchr(bss_config[i], ':');
677 wpa_printf(MSG_ERROR,
678 "Invalid BSS config identifier '%s'",
683 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
687 for (j = 0; j < interfaces.count; j++) {
688 if (interfaces.iface[j] == iface)
691 if (j == interfaces.count) {
692 struct hostapd_iface **tmp;
693 tmp = os_realloc_array(interfaces.iface,
694 interfaces.count + 1,
695 sizeof(struct hostapd_iface *));
697 hostapd_interface_deinit_free(iface);
700 interfaces.iface = tmp;
701 interfaces.iface[interfaces.count++] = iface;
706 * Enable configured interfaces. Depending on channel configuration,
707 * this may complete full initialization before returning or use a
708 * callback mechanism to complete setup in case of operations like HT
709 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
710 * In such case, the interface will be enabled from eloop context within
711 * hostapd_global_run().
713 interfaces.terminate_on_error = interfaces.count;
714 for (i = 0; i < interfaces.count; i++) {
715 if (hostapd_driver_init(interfaces.iface[i]) ||
716 hostapd_setup_interface(interfaces.iface[i]))
720 hostapd_global_ctrl_iface_init(&interfaces);
722 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
723 wpa_printf(MSG_ERROR, "Failed to start eloop");
730 hostapd_global_ctrl_iface_deinit(&interfaces);
731 /* Deinitialize all interfaces */
732 for (i = 0; i < interfaces.count; i++) {
733 if (!interfaces.iface[i])
735 interfaces.iface[i]->driver_ap_teardown =
736 !!(interfaces.iface[i]->drv_flags &
737 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
738 hostapd_interface_deinit_free(interfaces.iface[i]);
740 os_free(interfaces.iface);
742 hostapd_global_deinit(pid_file);
746 wpa_debug_close_file();
747 wpa_debug_close_linux_tracing();