3 * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
16 #ifndef CONFIG_NATIVE_WINDOWS
18 #endif /* CONFIG_NATIVE_WINDOWS */
20 #include "utils/common.h"
21 #include "utils/eloop.h"
22 #include "crypto/random.h"
23 #include "crypto/tls.h"
24 #include "common/version.h"
25 #include "drivers/driver.h"
26 #include "eap_server/eap.h"
27 #include "eap_server/tncs.h"
28 #include "ap/hostapd.h"
29 #include "ap/ap_config.h"
30 #include "config_file.h"
31 #include "eap_register.h"
32 #include "dump_state.h"
33 #include "ctrl_iface.h"
36 extern int wpa_debug_level;
37 extern int wpa_debug_show_keys;
38 extern int wpa_debug_timestamp;
40 extern struct wpa_driver_ops *wpa_drivers[];
48 static struct hapd_global global;
51 struct hapd_interfaces {
53 struct hostapd_iface **iface;
57 static int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
58 int (*cb)(struct hostapd_iface *iface,
59 void *ctx), void *ctx)
64 for (i = 0; i < interfaces->count; i++) {
65 ret = cb(interfaces->iface[i], ctx);
74 #ifndef CONFIG_NO_HOSTAPD_LOGGER
75 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
76 int level, const char *txt, size_t len)
78 struct hostapd_data *hapd = ctx;
79 char *format, *module_str;
81 int conf_syslog_level, conf_stdout_level;
82 unsigned int conf_syslog, conf_stdout;
85 format = os_malloc(maxlen);
89 if (hapd && hapd->conf) {
90 conf_syslog_level = hapd->conf->logger_syslog_level;
91 conf_stdout_level = hapd->conf->logger_stdout_level;
92 conf_syslog = hapd->conf->logger_syslog;
93 conf_stdout = hapd->conf->logger_stdout;
95 conf_syslog_level = conf_stdout_level = 0;
96 conf_syslog = conf_stdout = (unsigned int) -1;
100 case HOSTAPD_MODULE_IEEE80211:
101 module_str = "IEEE 802.11";
103 case HOSTAPD_MODULE_IEEE8021X:
104 module_str = "IEEE 802.1X";
106 case HOSTAPD_MODULE_RADIUS:
107 module_str = "RADIUS";
109 case HOSTAPD_MODULE_WPA:
112 case HOSTAPD_MODULE_DRIVER:
113 module_str = "DRIVER";
115 case HOSTAPD_MODULE_IAPP:
118 case HOSTAPD_MODULE_MLME:
126 if (hapd && hapd->conf && addr)
127 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
128 hapd->conf->iface, MAC2STR(addr),
129 module_str ? " " : "", module_str, txt);
130 else if (hapd && hapd->conf)
131 os_snprintf(format, maxlen, "%s:%s%s %s",
132 hapd->conf->iface, module_str ? " " : "",
135 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
136 MAC2STR(addr), module_str ? " " : "",
139 os_snprintf(format, maxlen, "%s%s%s",
140 module_str, module_str ? ": " : "", txt);
142 if ((conf_stdout & module) && level >= conf_stdout_level) {
143 wpa_debug_print_timestamp();
144 printf("%s\n", format);
147 #ifndef CONFIG_NATIVE_WINDOWS
148 if ((conf_syslog & module) && level >= conf_syslog_level) {
151 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
152 case HOSTAPD_LEVEL_DEBUG:
153 priority = LOG_DEBUG;
155 case HOSTAPD_LEVEL_INFO:
158 case HOSTAPD_LEVEL_NOTICE:
159 priority = LOG_NOTICE;
161 case HOSTAPD_LEVEL_WARNING:
162 priority = LOG_WARNING;
168 syslog(priority, "%s", format);
170 #endif /* CONFIG_NATIVE_WINDOWS */
174 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
178 * hostapd_init - Allocate and initialize per-interface data
179 * @config_file: Path to the configuration file
180 * Returns: Pointer to the allocated interface data or %NULL on failure
182 * This function is used to allocate main data structures for per-interface
183 * data. The allocated data buffer will be freed by calling
184 * hostapd_cleanup_iface().
186 static struct hostapd_iface * hostapd_init(const char *config_file)
188 struct hostapd_iface *hapd_iface = NULL;
189 struct hostapd_config *conf = NULL;
190 struct hostapd_data *hapd;
193 hapd_iface = os_zalloc(sizeof(*hapd_iface));
194 if (hapd_iface == NULL)
197 hapd_iface->reload_config = hostapd_reload_config;
198 hapd_iface->config_read_cb = hostapd_config_read;
199 hapd_iface->config_fname = os_strdup(config_file);
200 if (hapd_iface->config_fname == NULL)
202 hapd_iface->ctrl_iface_init = hostapd_ctrl_iface_init;
203 hapd_iface->ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
204 hapd_iface->for_each_interface = hostapd_for_each_interface;
206 conf = hostapd_config_read(hapd_iface->config_fname);
209 hapd_iface->conf = conf;
211 hapd_iface->num_bss = conf->num_bss;
212 hapd_iface->bss = os_zalloc(conf->num_bss *
213 sizeof(struct hostapd_data *));
214 if (hapd_iface->bss == NULL)
217 for (i = 0; i < conf->num_bss; i++) {
218 hapd = hapd_iface->bss[i] =
219 hostapd_alloc_bss_data(hapd_iface, conf,
223 hapd->msg_ctx = hapd;
230 hostapd_config_free(conf);
232 os_free(hapd_iface->config_fname);
233 os_free(hapd_iface->bss);
240 static int hostapd_driver_init(struct hostapd_iface *iface)
242 struct wpa_init_params params;
244 struct hostapd_data *hapd = iface->bss[0];
245 struct hostapd_bss_config *conf = hapd->conf;
247 struct wpa_driver_capa capa;
249 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
250 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
254 /* Initialize the driver interface */
255 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
258 os_memset(¶ms, 0, sizeof(params));
259 for (i = 0; wpa_drivers[i]; i++) {
260 if (wpa_drivers[i] != hapd->driver)
263 if (global.drv_priv[i] == NULL &&
264 wpa_drivers[i]->global_init) {
265 global.drv_priv[i] = wpa_drivers[i]->global_init();
266 if (global.drv_priv[i] == NULL) {
267 wpa_printf(MSG_ERROR, "Failed to initialize "
269 wpa_drivers[i]->name);
274 params.global_priv = global.drv_priv[i];
278 params.ifname = hapd->conf->iface;
279 params.ssid = (const u8 *) hapd->conf->ssid.ssid;
280 params.ssid_len = hapd->conf->ssid.ssid_len;
281 params.test_socket = hapd->conf->test_socket;
282 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
284 params.num_bridge = hapd->iface->num_bss;
285 params.bridge = os_zalloc(hapd->iface->num_bss * sizeof(char *));
286 if (params.bridge == NULL)
288 for (i = 0; i < hapd->iface->num_bss; i++) {
289 struct hostapd_data *bss = hapd->iface->bss[i];
290 if (bss->conf->bridge[0])
291 params.bridge[i] = bss->conf->bridge;
294 params.own_addr = hapd->own_addr;
296 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
297 os_free(params.bridge);
298 if (hapd->drv_priv == NULL) {
299 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
305 if (hapd->driver->get_capa &&
306 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0)
307 iface->drv_flags = capa.flags;
313 static void hostapd_interface_deinit_free(struct hostapd_iface *iface)
315 const struct wpa_driver_ops *driver;
319 driver = iface->bss[0]->driver;
320 drv_priv = iface->bss[0]->drv_priv;
321 hostapd_interface_deinit(iface);
322 if (driver && driver->hapd_deinit)
323 driver->hapd_deinit(drv_priv);
324 hostapd_interface_free(iface);
328 static struct hostapd_iface *
329 hostapd_interface_init(struct hapd_interfaces *interfaces,
330 const char *config_fname, int debug)
332 struct hostapd_iface *iface;
335 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
336 iface = hostapd_init(config_fname);
339 iface->interfaces = interfaces;
341 for (k = 0; k < debug; k++) {
342 if (iface->bss[0]->conf->logger_stdout_level > 0)
343 iface->bss[0]->conf->logger_stdout_level--;
346 if (hostapd_driver_init(iface) ||
347 hostapd_setup_interface(iface)) {
348 hostapd_interface_deinit_free(iface);
357 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
359 static void handle_term(int sig, void *signal_ctx)
361 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
366 #ifndef CONFIG_NATIVE_WINDOWS
368 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
370 if (hostapd_reload_config(iface) < 0) {
371 wpa_printf(MSG_WARNING, "Failed to read new configuration "
372 "file - continuing with old.");
379 * handle_reload - SIGHUP handler to reload configuration
381 static void handle_reload(int sig, void *signal_ctx)
383 struct hapd_interfaces *interfaces = signal_ctx;
384 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
386 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
390 static void handle_dump_state(int sig, void *signal_ctx)
392 #ifdef HOSTAPD_DUMP_STATE
393 struct hapd_interfaces *interfaces = signal_ctx;
394 hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
395 #endif /* HOSTAPD_DUMP_STATE */
397 #endif /* CONFIG_NATIVE_WINDOWS */
400 static int hostapd_global_init(struct hapd_interfaces *interfaces,
401 const char *entropy_file)
405 os_memset(&global, 0, sizeof(global));
407 hostapd_logger_register_cb(hostapd_logger_cb);
409 if (eap_server_register_methods()) {
410 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
415 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
419 random_init(entropy_file);
421 #ifndef CONFIG_NATIVE_WINDOWS
422 eloop_register_signal(SIGHUP, handle_reload, interfaces);
423 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
424 #endif /* CONFIG_NATIVE_WINDOWS */
425 eloop_register_signal_terminate(handle_term, interfaces);
427 #ifndef CONFIG_NATIVE_WINDOWS
428 openlog("hostapd", 0, LOG_DAEMON);
429 #endif /* CONFIG_NATIVE_WINDOWS */
431 for (i = 0; wpa_drivers[i]; i++)
433 if (global.drv_count == 0) {
434 wpa_printf(MSG_ERROR, "No drivers enabled");
437 global.drv_priv = os_zalloc(global.drv_count * sizeof(void *));
438 if (global.drv_priv == NULL)
445 static void hostapd_global_deinit(const char *pid_file)
449 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
450 if (!global.drv_priv[i])
452 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
454 os_free(global.drv_priv);
455 global.drv_priv = NULL;
457 #ifdef EAP_SERVER_TNC
458 tncs_global_deinit();
459 #endif /* EAP_SERVER_TNC */
465 #ifndef CONFIG_NATIVE_WINDOWS
467 #endif /* CONFIG_NATIVE_WINDOWS */
469 eap_server_unregister_methods();
471 os_daemonize_terminate(pid_file);
475 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
476 const char *pid_file)
478 #ifdef EAP_SERVER_TNC
482 for (i = 0; !tnc && i < ifaces->count; i++) {
483 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
484 if (ifaces->iface[i]->bss[0]->conf->tnc) {
491 if (tnc && tncs_global_init() < 0) {
492 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
495 #endif /* EAP_SERVER_TNC */
497 if (daemonize && os_daemonize(pid_file)) {
508 static void show_version(void)
511 "hostapd v" VERSION_STR "\n"
512 "User space daemon for IEEE 802.11 AP management,\n"
513 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
514 "Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi> "
515 "and contributors\n");
519 static void usage(void)
524 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
525 "<configuration file(s)>\n"
528 " -h show this usage\n"
529 " -d show more debug messages (-dd for even more)\n"
530 " -B run daemon in the background\n"
533 " -K include key data in debug messages\n"
534 #ifdef CONFIG_DEBUG_FILE
535 " -f log output to debug file instead of stdout\n"
536 #endif /* CONFIG_DEBUG_FILE */
537 " -t include timestamps in some debug messages\n"
538 " -v show hostapd version\n");
544 static const char * hostapd_msg_ifname_cb(void *ctx)
546 struct hostapd_data *hapd = ctx;
547 if (hapd && hapd->iconf && hapd->iconf->bss)
548 return hapd->iconf->bss->iface;
553 int main(int argc, char *argv[])
555 struct hapd_interfaces interfaces;
558 int c, debug = 0, daemonize = 0;
559 char *pid_file = NULL;
560 const char *log_file = NULL;
561 const char *entropy_file = NULL;
563 if (os_program_init())
567 c = getopt(argc, argv, "Bde:f:hKP:tv");
576 if (wpa_debug_level > 0)
583 entropy_file = optarg;
589 wpa_debug_show_keys++;
593 pid_file = os_rel2abs_path(optarg);
596 wpa_debug_timestamp++;
612 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
615 wpa_debug_open_file(log_file);
617 interfaces.count = argc - optind;
618 interfaces.iface = os_zalloc(interfaces.count *
619 sizeof(struct hostapd_iface *));
620 if (interfaces.iface == NULL) {
621 wpa_printf(MSG_ERROR, "malloc failed");
625 if (hostapd_global_init(&interfaces, entropy_file))
628 /* Initialize interfaces */
629 for (i = 0; i < interfaces.count; i++) {
630 interfaces.iface[i] = hostapd_interface_init(&interfaces,
633 if (!interfaces.iface[i])
637 if (hostapd_global_run(&interfaces, daemonize, pid_file))
643 /* Deinitialize all interfaces */
644 for (i = 0; i < interfaces.count; i++)
645 hostapd_interface_deinit_free(interfaces.iface[i]);
646 os_free(interfaces.iface);
648 hostapd_global_deinit(pid_file);
652 wpa_debug_close_file();