3 * Copyright (c) 2002-2009, 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.
16 #ifndef CONFIG_NATIVE_WINDOWS
18 #endif /* CONFIG_NATIVE_WINDOWS */
22 #include "crypto/tls.h"
23 #include "common/version.h"
24 #include "eap_server/eap.h"
25 #include "eap_server/tncs.h"
30 extern int wpa_debug_level;
31 extern int wpa_debug_show_keys;
32 extern int wpa_debug_timestamp;
35 struct hapd_interfaces {
37 struct hostapd_iface **iface;
41 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
42 int (*cb)(struct hostapd_iface *iface,
43 void *ctx), void *ctx)
48 for (i = 0; i < interfaces->count; i++) {
49 ret = cb(interfaces->iface[i], ctx);
58 #ifndef CONFIG_NO_HOSTAPD_LOGGER
59 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
60 int level, const char *txt, size_t len)
62 struct hostapd_data *hapd = ctx;
63 char *format, *module_str;
65 int conf_syslog_level, conf_stdout_level;
66 unsigned int conf_syslog, conf_stdout;
69 format = os_malloc(maxlen);
73 if (hapd && hapd->conf) {
74 conf_syslog_level = hapd->conf->logger_syslog_level;
75 conf_stdout_level = hapd->conf->logger_stdout_level;
76 conf_syslog = hapd->conf->logger_syslog;
77 conf_stdout = hapd->conf->logger_stdout;
79 conf_syslog_level = conf_stdout_level = 0;
80 conf_syslog = conf_stdout = (unsigned int) -1;
84 case HOSTAPD_MODULE_IEEE80211:
85 module_str = "IEEE 802.11";
87 case HOSTAPD_MODULE_IEEE8021X:
88 module_str = "IEEE 802.1X";
90 case HOSTAPD_MODULE_RADIUS:
91 module_str = "RADIUS";
93 case HOSTAPD_MODULE_WPA:
96 case HOSTAPD_MODULE_DRIVER:
97 module_str = "DRIVER";
99 case HOSTAPD_MODULE_IAPP:
102 case HOSTAPD_MODULE_MLME:
110 if (hapd && hapd->conf && addr)
111 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
112 hapd->conf->iface, MAC2STR(addr),
113 module_str ? " " : "", module_str, txt);
114 else if (hapd && hapd->conf)
115 os_snprintf(format, maxlen, "%s:%s%s %s",
116 hapd->conf->iface, module_str ? " " : "",
119 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
120 MAC2STR(addr), module_str ? " " : "",
123 os_snprintf(format, maxlen, "%s%s%s",
124 module_str, module_str ? ": " : "", txt);
126 if ((conf_stdout & module) && level >= conf_stdout_level) {
127 wpa_debug_print_timestamp();
128 printf("%s\n", format);
131 #ifndef CONFIG_NATIVE_WINDOWS
132 if ((conf_syslog & module) && level >= conf_syslog_level) {
135 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
136 case HOSTAPD_LEVEL_DEBUG:
137 priority = LOG_DEBUG;
139 case HOSTAPD_LEVEL_INFO:
142 case HOSTAPD_LEVEL_NOTICE:
143 priority = LOG_NOTICE;
145 case HOSTAPD_LEVEL_WARNING:
146 priority = LOG_WARNING;
152 syslog(priority, "%s", format);
154 #endif /* CONFIG_NATIVE_WINDOWS */
158 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
162 * hostapd_init - Allocate and initialize per-interface data
163 * @config_file: Path to the configuration file
164 * Returns: Pointer to the allocated interface data or %NULL on failure
166 * This function is used to allocate main data structures for per-interface
167 * data. The allocated data buffer will be freed by calling
168 * hostapd_cleanup_iface().
170 static struct hostapd_iface * hostapd_init(const char *config_file)
172 struct hostapd_iface *hapd_iface = NULL;
173 struct hostapd_config *conf = NULL;
174 struct hostapd_data *hapd;
177 hapd_iface = os_zalloc(sizeof(*hapd_iface));
178 if (hapd_iface == NULL)
181 hapd_iface->config_fname = os_strdup(config_file);
182 if (hapd_iface->config_fname == NULL)
185 conf = hostapd_config_read(hapd_iface->config_fname);
188 hapd_iface->conf = conf;
190 hapd_iface->num_bss = conf->num_bss;
191 hapd_iface->bss = os_zalloc(conf->num_bss *
192 sizeof(struct hostapd_data *));
193 if (hapd_iface->bss == NULL)
196 for (i = 0; i < conf->num_bss; i++) {
197 hapd = hapd_iface->bss[i] =
198 hostapd_alloc_bss_data(hapd_iface, conf,
208 hostapd_config_free(conf);
210 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
211 hapd = hapd_iface->bss[i];
212 if (hapd && hapd->ssl_ctx)
213 tls_deinit(hapd->ssl_ctx);
216 os_free(hapd_iface->config_fname);
217 os_free(hapd_iface->bss);
224 static struct hostapd_iface *
225 hostapd_interface_init(struct hapd_interfaces *interfaces,
226 const char *config_fname, int debug)
228 struct hostapd_iface *iface;
231 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
232 iface = hostapd_init(config_fname);
235 iface->interfaces = interfaces;
237 for (k = 0; k < debug; k++) {
238 if (iface->bss[0]->conf->logger_stdout_level > 0)
239 iface->bss[0]->conf->logger_stdout_level--;
242 if (hostapd_setup_interface(iface)) {
243 hostapd_interface_deinit(iface);
252 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
254 static void handle_term(int sig, void *signal_ctx)
256 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
261 #ifndef CONFIG_NATIVE_WINDOWS
263 * handle_reload - SIGHUP handler to reload configuration
265 static void handle_reload(int sig, void *signal_ctx)
267 struct hapd_interfaces *interfaces = signal_ctx;
268 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
270 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
274 static void handle_dump_state(int sig, void *signal_ctx)
276 #ifdef HOSTAPD_DUMP_STATE
277 struct hapd_interfaces *interfaces = signal_ctx;
278 hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
279 #endif /* HOSTAPD_DUMP_STATE */
281 #endif /* CONFIG_NATIVE_WINDOWS */
284 static int hostapd_global_init(struct hapd_interfaces *interfaces)
286 hostapd_logger_register_cb(hostapd_logger_cb);
288 if (eap_server_register_methods()) {
289 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
294 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
298 #ifndef CONFIG_NATIVE_WINDOWS
299 eloop_register_signal(SIGHUP, handle_reload, interfaces);
300 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
301 #endif /* CONFIG_NATIVE_WINDOWS */
302 eloop_register_signal_terminate(handle_term, interfaces);
304 #ifndef CONFIG_NATIVE_WINDOWS
305 openlog("hostapd", 0, LOG_DAEMON);
306 #endif /* CONFIG_NATIVE_WINDOWS */
312 static void hostapd_global_deinit(const char *pid_file)
314 #ifdef EAP_SERVER_TNC
315 tncs_global_deinit();
316 #endif /* EAP_SERVER_TNC */
320 #ifndef CONFIG_NATIVE_WINDOWS
322 #endif /* CONFIG_NATIVE_WINDOWS */
324 eap_server_unregister_methods();
326 os_daemonize_terminate(pid_file);
330 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
331 const char *pid_file)
333 #ifdef EAP_SERVER_TNC
337 for (i = 0; !tnc && i < ifaces->count; i++) {
338 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
339 if (ifaces->iface[i]->bss[0]->conf->tnc) {
346 if (tnc && tncs_global_init() < 0) {
347 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
350 #endif /* EAP_SERVER_TNC */
352 if (daemonize && os_daemonize(pid_file)) {
363 static void show_version(void)
366 "hostapd v" VERSION_STR "\n"
367 "User space daemon for IEEE 802.11 AP management,\n"
368 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
369 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
370 "and contributors\n");
374 static void usage(void)
379 "usage: hostapd [-hdBKtv] [-P <PID file>] "
380 "<configuration file(s)>\n"
383 " -h show this usage\n"
384 " -d show more debug messages (-dd for even more)\n"
385 " -B run daemon in the background\n"
387 " -K include key data in debug messages\n"
388 " -t include timestamps in some debug messages\n"
389 " -v show hostapd version\n");
395 int main(int argc, char *argv[])
397 struct hapd_interfaces interfaces;
400 int c, debug = 0, daemonize = 0;
401 char *pid_file = NULL;
403 if (os_program_init())
407 c = getopt(argc, argv, "BdhKP:tv");
416 if (wpa_debug_level > 0)
423 wpa_debug_show_keys++;
427 pid_file = os_rel2abs_path(optarg);
430 wpa_debug_timestamp++;
446 interfaces.count = argc - optind;
447 interfaces.iface = os_malloc(interfaces.count *
448 sizeof(struct hostapd_iface *));
449 if (interfaces.iface == NULL) {
450 wpa_printf(MSG_ERROR, "malloc failed\n");
454 if (hostapd_global_init(&interfaces))
457 /* Initialize interfaces */
458 for (i = 0; i < interfaces.count; i++) {
459 interfaces.iface[i] = hostapd_interface_init(&interfaces,
462 if (!interfaces.iface[i])
466 if (hostapd_global_run(&interfaces, daemonize, pid_file))
472 /* Deinitialize all interfaces */
473 for (i = 0; i < interfaces.count; i++)
474 hostapd_interface_deinit(interfaces.iface[i]);
475 os_free(interfaces.iface);
477 hostapd_global_deinit(pid_file);