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 */
23 #include "common/version.h"
26 #include "eap_server/eap.h"
27 #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(int (*cb)(struct hostapd_iface *iface,
42 void *ctx), void *ctx)
44 struct hapd_interfaces *interfaces = eloop_get_user_data();
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 * hostapd_interface_init(const char *config_fname,
227 struct hostapd_iface *iface;
230 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
231 iface = hostapd_init(config_fname);
235 for (k = 0; k < debug; k++) {
236 if (iface->bss[0]->conf->logger_stdout_level > 0)
237 iface->bss[0]->conf->logger_stdout_level--;
240 if (hostapd_setup_interface(iface)) {
241 hostapd_interface_deinit(iface);
250 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
252 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
254 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
259 #ifndef CONFIG_NATIVE_WINDOWS
261 * handle_reload - SIGHUP handler to reload configuration
263 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
265 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
267 hostapd_for_each_interface(handle_reload_iface, NULL);
271 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
273 #ifdef HOSTAPD_DUMP_STATE
274 hostapd_for_each_interface(handle_dump_state_iface, NULL);
275 #endif /* HOSTAPD_DUMP_STATE */
277 #endif /* CONFIG_NATIVE_WINDOWS */
280 static int hostapd_global_init(struct hapd_interfaces *interfaces)
282 hostapd_logger_register_cb(hostapd_logger_cb);
284 if (eap_server_register_methods()) {
285 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
289 if (eloop_init(interfaces)) {
290 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
294 #ifndef CONFIG_NATIVE_WINDOWS
295 eloop_register_signal(SIGHUP, handle_reload, NULL);
296 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
297 #endif /* CONFIG_NATIVE_WINDOWS */
298 eloop_register_signal_terminate(handle_term, NULL);
300 #ifndef CONFIG_NATIVE_WINDOWS
301 openlog("hostapd", 0, LOG_DAEMON);
302 #endif /* CONFIG_NATIVE_WINDOWS */
308 static void hostapd_global_deinit(const char *pid_file)
310 #ifdef EAP_SERVER_TNC
311 tncs_global_deinit();
312 #endif /* EAP_SERVER_TNC */
316 #ifndef CONFIG_NATIVE_WINDOWS
318 #endif /* CONFIG_NATIVE_WINDOWS */
320 eap_server_unregister_methods();
322 os_daemonize_terminate(pid_file);
326 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
327 const char *pid_file)
329 #ifdef EAP_SERVER_TNC
333 for (i = 0; !tnc && i < ifaces->count; i++) {
334 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
335 if (ifaces->iface[i]->bss[0]->conf->tnc) {
342 if (tnc && tncs_global_init() < 0) {
343 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
346 #endif /* EAP_SERVER_TNC */
348 if (daemonize && os_daemonize(pid_file)) {
359 static void show_version(void)
362 "hostapd v" VERSION_STR "\n"
363 "User space daemon for IEEE 802.11 AP management,\n"
364 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
365 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
366 "and contributors\n");
370 static void usage(void)
375 "usage: hostapd [-hdBKtv] [-P <PID file>] "
376 "<configuration file(s)>\n"
379 " -h show this usage\n"
380 " -d show more debug messages (-dd for even more)\n"
381 " -B run daemon in the background\n"
383 " -K include key data in debug messages\n"
384 " -t include timestamps in some debug messages\n"
385 " -v show hostapd version\n");
391 int main(int argc, char *argv[])
393 struct hapd_interfaces interfaces;
396 int c, debug = 0, daemonize = 0;
397 char *pid_file = NULL;
400 c = getopt(argc, argv, "BdhKP:tv");
409 if (wpa_debug_level > 0)
416 wpa_debug_show_keys++;
420 pid_file = os_rel2abs_path(optarg);
423 wpa_debug_timestamp++;
439 interfaces.count = argc - optind;
440 interfaces.iface = os_malloc(interfaces.count *
441 sizeof(struct hostapd_iface *));
442 if (interfaces.iface == NULL) {
443 wpa_printf(MSG_ERROR, "malloc failed\n");
447 if (hostapd_global_init(&interfaces))
450 /* Initialize interfaces */
451 for (i = 0; i < interfaces.count; i++) {
452 interfaces.iface[i] = hostapd_interface_init(argv[optind + i],
454 if (!interfaces.iface[i])
458 if (hostapd_global_run(&interfaces, daemonize, pid_file))
464 /* Deinitialize all interfaces */
465 for (i = 0; i < interfaces.count; i++)
466 hostapd_interface_deinit(interfaces.iface[i]);
467 os_free(interfaces.iface);
469 hostapd_global_deinit(pid_file);