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 "crypto/random.h"
18 #include "crypto/tls.h"
19 #include "common/version.h"
20 #include "drivers/driver.h"
21 #include "eap_server/eap.h"
22 #include "eap_server/tncs.h"
23 #include "ap/hostapd.h"
24 #include "ap/ap_config.h"
25 #include "ap/ap_drv_ops.h"
26 #include "config_file.h"
27 #include "eap_register.h"
28 #include "dump_state.h"
29 #include "ctrl_iface.h"
32 extern int wpa_debug_level;
33 extern int wpa_debug_show_keys;
34 extern int wpa_debug_timestamp;
36 extern struct wpa_driver_ops *wpa_drivers[];
44 static struct hapd_global global;
47 #ifndef CONFIG_NO_HOSTAPD_LOGGER
48 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
49 int level, const char *txt, size_t len)
51 struct hostapd_data *hapd = ctx;
52 char *format, *module_str;
54 int conf_syslog_level, conf_stdout_level;
55 unsigned int conf_syslog, conf_stdout;
58 format = os_malloc(maxlen);
62 if (hapd && hapd->conf) {
63 conf_syslog_level = hapd->conf->logger_syslog_level;
64 conf_stdout_level = hapd->conf->logger_stdout_level;
65 conf_syslog = hapd->conf->logger_syslog;
66 conf_stdout = hapd->conf->logger_stdout;
68 conf_syslog_level = conf_stdout_level = 0;
69 conf_syslog = conf_stdout = (unsigned int) -1;
73 case HOSTAPD_MODULE_IEEE80211:
74 module_str = "IEEE 802.11";
76 case HOSTAPD_MODULE_IEEE8021X:
77 module_str = "IEEE 802.1X";
79 case HOSTAPD_MODULE_RADIUS:
80 module_str = "RADIUS";
82 case HOSTAPD_MODULE_WPA:
85 case HOSTAPD_MODULE_DRIVER:
86 module_str = "DRIVER";
88 case HOSTAPD_MODULE_IAPP:
91 case HOSTAPD_MODULE_MLME:
99 if (hapd && hapd->conf && addr)
100 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
101 hapd->conf->iface, MAC2STR(addr),
102 module_str ? " " : "", module_str, txt);
103 else if (hapd && hapd->conf)
104 os_snprintf(format, maxlen, "%s:%s%s %s",
105 hapd->conf->iface, module_str ? " " : "",
108 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
109 MAC2STR(addr), module_str ? " " : "",
112 os_snprintf(format, maxlen, "%s%s%s",
113 module_str, module_str ? ": " : "", txt);
115 if ((conf_stdout & module) && level >= conf_stdout_level) {
116 wpa_debug_print_timestamp();
117 wpa_printf(MSG_INFO, "%s", format);
120 #ifndef CONFIG_NATIVE_WINDOWS
121 if ((conf_syslog & module) && level >= conf_syslog_level) {
124 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
125 case HOSTAPD_LEVEL_DEBUG:
126 priority = LOG_DEBUG;
128 case HOSTAPD_LEVEL_INFO:
131 case HOSTAPD_LEVEL_NOTICE:
132 priority = LOG_NOTICE;
134 case HOSTAPD_LEVEL_WARNING:
135 priority = LOG_WARNING;
141 syslog(priority, "%s", format);
143 #endif /* CONFIG_NATIVE_WINDOWS */
147 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
150 static int hostapd_driver_init(struct hostapd_iface *iface)
152 struct wpa_init_params params;
154 struct hostapd_data *hapd = iface->bss[0];
155 struct hostapd_bss_config *conf = hapd->conf;
157 struct wpa_driver_capa capa;
159 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
160 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
164 /* Initialize the driver interface */
165 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
168 os_memset(¶ms, 0, sizeof(params));
169 for (i = 0; wpa_drivers[i]; i++) {
170 if (wpa_drivers[i] != hapd->driver)
173 if (global.drv_priv[i] == NULL &&
174 wpa_drivers[i]->global_init) {
175 global.drv_priv[i] = wpa_drivers[i]->global_init();
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.ssid = hapd->conf->ssid.ssid;
190 params.ssid_len = hapd->conf->ssid.ssid_len;
191 params.test_socket = hapd->conf->test_socket;
192 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
194 params.num_bridge = hapd->iface->num_bss;
195 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
196 if (params.bridge == NULL)
198 for (i = 0; i < hapd->iface->num_bss; i++) {
199 struct hostapd_data *bss = hapd->iface->bss[i];
200 if (bss->conf->bridge[0])
201 params.bridge[i] = bss->conf->bridge;
204 params.own_addr = hapd->own_addr;
206 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
207 os_free(params.bridge);
208 if (hapd->drv_priv == NULL) {
209 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
215 if (hapd->driver->get_capa &&
216 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
217 iface->drv_flags = capa.flags;
218 iface->probe_resp_offloads = capa.probe_resp_offloads;
219 iface->extended_capa = capa.extended_capa;
220 iface->extended_capa_mask = capa.extended_capa_mask;
221 iface->extended_capa_len = capa.extended_capa_len;
222 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
229 static struct hostapd_iface *
230 hostapd_interface_init(struct hapd_interfaces *interfaces,
231 const char *config_fname, int debug)
233 struct hostapd_iface *iface;
236 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
237 iface = hostapd_init(interfaces, config_fname);
240 iface->interfaces = interfaces;
242 for (k = 0; k < debug; k++) {
243 if (iface->bss[0]->conf->logger_stdout_level > 0)
244 iface->bss[0]->conf->logger_stdout_level--;
247 if (iface->conf->bss[0]->iface[0] == '\0' &&
248 !hostapd_drv_none(iface->bss[0])) {
249 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
251 hostapd_interface_deinit_free(iface);
260 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
262 static void handle_term(int sig, void *signal_ctx)
264 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
269 #ifndef CONFIG_NATIVE_WINDOWS
271 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
273 if (hostapd_reload_config(iface) < 0) {
274 wpa_printf(MSG_WARNING, "Failed to read new configuration "
275 "file - continuing with old.");
282 * handle_reload - SIGHUP handler to reload configuration
284 static void handle_reload(int sig, void *signal_ctx)
286 struct hapd_interfaces *interfaces = signal_ctx;
287 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
289 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
293 static void handle_dump_state(int sig, void *signal_ctx)
295 #ifdef HOSTAPD_DUMP_STATE
296 struct hapd_interfaces *interfaces = signal_ctx;
297 hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
298 #endif /* HOSTAPD_DUMP_STATE */
300 #endif /* CONFIG_NATIVE_WINDOWS */
303 static int hostapd_global_init(struct hapd_interfaces *interfaces,
304 const char *entropy_file)
308 os_memset(&global, 0, sizeof(global));
310 hostapd_logger_register_cb(hostapd_logger_cb);
312 if (eap_server_register_methods()) {
313 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
318 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
322 random_init(entropy_file);
324 #ifndef CONFIG_NATIVE_WINDOWS
325 eloop_register_signal(SIGHUP, handle_reload, interfaces);
326 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
327 #endif /* CONFIG_NATIVE_WINDOWS */
328 eloop_register_signal_terminate(handle_term, interfaces);
330 #ifndef CONFIG_NATIVE_WINDOWS
331 openlog("hostapd", 0, LOG_DAEMON);
332 #endif /* CONFIG_NATIVE_WINDOWS */
334 for (i = 0; wpa_drivers[i]; i++)
336 if (global.drv_count == 0) {
337 wpa_printf(MSG_ERROR, "No drivers enabled");
340 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
341 if (global.drv_priv == NULL)
348 static void hostapd_global_deinit(const char *pid_file)
352 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
353 if (!global.drv_priv[i])
355 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
357 os_free(global.drv_priv);
358 global.drv_priv = NULL;
360 #ifdef EAP_SERVER_TNC
361 tncs_global_deinit();
362 #endif /* EAP_SERVER_TNC */
368 #ifndef CONFIG_NATIVE_WINDOWS
370 #endif /* CONFIG_NATIVE_WINDOWS */
372 eap_server_unregister_methods();
374 os_daemonize_terminate(pid_file);
378 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
379 const char *pid_file)
381 #ifdef EAP_SERVER_TNC
385 for (i = 0; !tnc && i < ifaces->count; i++) {
386 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
387 if (ifaces->iface[i]->bss[0]->conf->tnc) {
394 if (tnc && tncs_global_init() < 0) {
395 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
398 #endif /* EAP_SERVER_TNC */
400 if (daemonize && os_daemonize(pid_file)) {
411 static void show_version(void)
414 "hostapd v" VERSION_STR "\n"
415 "User space daemon for IEEE 802.11 AP management,\n"
416 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
417 "Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> "
418 "and contributors\n");
422 static void usage(void)
427 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
429 " [-g <global ctrl_iface>] [-G <group>] \\\n"
430 " <configuration file(s)>\n"
433 " -h show this usage\n"
434 " -d show more debug messages (-dd for even more)\n"
435 " -B run daemon in the background\n"
437 " -g global control interface path\n"
438 " -G group for control interfaces\n"
440 " -K include key data in debug messages\n"
441 #ifdef CONFIG_DEBUG_FILE
442 " -f log output to debug file instead of stdout\n"
443 #endif /* CONFIG_DEBUG_FILE */
444 #ifdef CONFIG_DEBUG_LINUX_TRACING
445 " -T = record to Linux tracing in addition to logging\n"
446 " (records all messages regardless of debug verbosity)\n"
447 #endif /* CONFIG_DEBUG_LINUX_TRACING */
448 " -t include timestamps in some debug messages\n"
449 " -v show hostapd version\n");
455 static const char * hostapd_msg_ifname_cb(void *ctx)
457 struct hostapd_data *hapd = ctx;
458 if (hapd && hapd->iconf && hapd->iconf->bss &&
459 hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
460 return hapd->iconf->bss[0]->iface;
465 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
469 os_free(interfaces->global_iface_path);
470 interfaces->global_iface_path = os_strdup(path);
471 if (interfaces->global_iface_path == NULL)
473 pos = os_strrchr(interfaces->global_iface_path, '/');
475 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
477 os_free(interfaces->global_iface_path);
478 interfaces->global_iface_path = NULL;
483 interfaces->global_iface_name = pos + 1;
489 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
492 #ifndef CONFIG_NATIVE_WINDOWS
494 grp = getgrnam(group);
496 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
499 interfaces->ctrl_iface_group = grp->gr_gid;
500 #endif /* CONFIG_NATIVE_WINDOWS */
505 int main(int argc, char *argv[])
507 struct hapd_interfaces interfaces;
510 int c, debug = 0, daemonize = 0;
511 char *pid_file = NULL;
512 const char *log_file = NULL;
513 const char *entropy_file = NULL;
514 char **bss_config = NULL, **tmp_bss;
515 size_t num_bss_configs = 0;
516 #ifdef CONFIG_DEBUG_LINUX_TRACING
517 int enable_trace_dbg = 0;
518 #endif /* CONFIG_DEBUG_LINUX_TRACING */
520 if (os_program_init())
523 os_memset(&interfaces, 0, sizeof(interfaces));
524 interfaces.reload_config = hostapd_reload_config;
525 interfaces.config_read_cb = hostapd_config_read;
526 interfaces.for_each_interface = hostapd_for_each_interface;
527 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
528 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
529 interfaces.driver_init = hostapd_driver_init;
530 interfaces.global_iface_path = NULL;
531 interfaces.global_iface_name = NULL;
532 interfaces.global_ctrl_sock = -1;
535 c = getopt(argc, argv, "b:Bde:f:hKP:Ttvg:G:");
544 if (wpa_debug_level > 0)
551 entropy_file = optarg;
557 wpa_debug_show_keys++;
561 pid_file = os_rel2abs_path(optarg);
564 wpa_debug_timestamp++;
566 #ifdef CONFIG_DEBUG_LINUX_TRACING
568 enable_trace_dbg = 1;
570 #endif /* CONFIG_DEBUG_LINUX_TRACING */
576 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
580 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
584 tmp_bss = os_realloc_array(bss_config,
589 bss_config = tmp_bss;
590 bss_config[num_bss_configs++] = optarg;
598 if (optind == argc && interfaces.global_iface_path == NULL &&
599 num_bss_configs == 0)
602 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
605 wpa_debug_open_file(log_file);
606 #ifdef CONFIG_DEBUG_LINUX_TRACING
607 if (enable_trace_dbg) {
608 int tret = wpa_debug_open_linux_tracing();
610 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
614 #endif /* CONFIG_DEBUG_LINUX_TRACING */
616 interfaces.count = argc - optind;
617 if (interfaces.count || num_bss_configs) {
618 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
619 sizeof(struct hostapd_iface *));
620 if (interfaces.iface == NULL) {
621 wpa_printf(MSG_ERROR, "malloc failed");
626 if (hostapd_global_init(&interfaces, entropy_file)) {
627 wpa_printf(MSG_ERROR, "Failed to initilize global context");
631 /* Initialize interfaces */
632 for (i = 0; i < interfaces.count; i++) {
633 interfaces.iface[i] = hostapd_interface_init(&interfaces,
636 if (!interfaces.iface[i]) {
637 wpa_printf(MSG_ERROR, "Failed to initialize interface");
642 for (i = 0; i < num_bss_configs; i++) {
643 struct hostapd_iface *iface;
646 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
647 fname = os_strchr(bss_config[i], ':');
649 wpa_printf(MSG_ERROR,
650 "Invalid BSS config identifier '%s'",
655 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
659 for (j = 0; j < interfaces.count; j++) {
660 if (interfaces.iface[j] == iface)
663 if (j == interfaces.count) {
664 struct hostapd_iface **tmp;
665 tmp = os_realloc_array(interfaces.iface,
666 interfaces.count + 1,
667 sizeof(struct hostapd_iface *));
669 hostapd_interface_deinit_free(iface);
672 interfaces.iface = tmp;
673 interfaces.iface[interfaces.count++] = iface;
677 for (i = 0; i < interfaces.count; i++) {
678 if (hostapd_driver_init(interfaces.iface[i]) ||
679 hostapd_setup_interface(interfaces.iface[i]))
683 hostapd_global_ctrl_iface_init(&interfaces);
685 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
686 wpa_printf(MSG_ERROR, "Failed to start eloop");
693 hostapd_global_ctrl_iface_deinit(&interfaces);
694 /* Deinitialize all interfaces */
695 for (i = 0; i < interfaces.count; i++)
696 hostapd_interface_deinit_free(interfaces.iface[i]);
697 os_free(interfaces.iface);
699 hostapd_global_deinit(pid_file);
703 wpa_debug_close_file();
704 wpa_debug_close_linux_tracing();