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;
41 struct hapd_interfaces {
43 struct hostapd_iface **iface;
47 static int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
48 int (*cb)(struct hostapd_iface *iface,
49 void *ctx), void *ctx)
54 for (i = 0; i < interfaces->count; i++) {
55 ret = cb(interfaces->iface[i], ctx);
64 #ifndef CONFIG_NO_HOSTAPD_LOGGER
65 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
66 int level, const char *txt, size_t len)
68 struct hostapd_data *hapd = ctx;
69 char *format, *module_str;
71 int conf_syslog_level, conf_stdout_level;
72 unsigned int conf_syslog, conf_stdout;
75 format = os_malloc(maxlen);
79 if (hapd && hapd->conf) {
80 conf_syslog_level = hapd->conf->logger_syslog_level;
81 conf_stdout_level = hapd->conf->logger_stdout_level;
82 conf_syslog = hapd->conf->logger_syslog;
83 conf_stdout = hapd->conf->logger_stdout;
85 conf_syslog_level = conf_stdout_level = 0;
86 conf_syslog = conf_stdout = (unsigned int) -1;
90 case HOSTAPD_MODULE_IEEE80211:
91 module_str = "IEEE 802.11";
93 case HOSTAPD_MODULE_IEEE8021X:
94 module_str = "IEEE 802.1X";
96 case HOSTAPD_MODULE_RADIUS:
97 module_str = "RADIUS";
99 case HOSTAPD_MODULE_WPA:
102 case HOSTAPD_MODULE_DRIVER:
103 module_str = "DRIVER";
105 case HOSTAPD_MODULE_IAPP:
108 case HOSTAPD_MODULE_MLME:
116 if (hapd && hapd->conf && addr)
117 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
118 hapd->conf->iface, MAC2STR(addr),
119 module_str ? " " : "", module_str, txt);
120 else if (hapd && hapd->conf)
121 os_snprintf(format, maxlen, "%s:%s%s %s",
122 hapd->conf->iface, module_str ? " " : "",
125 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
126 MAC2STR(addr), module_str ? " " : "",
129 os_snprintf(format, maxlen, "%s%s%s",
130 module_str, module_str ? ": " : "", txt);
132 if ((conf_stdout & module) && level >= conf_stdout_level) {
133 wpa_debug_print_timestamp();
134 printf("%s\n", format);
137 #ifndef CONFIG_NATIVE_WINDOWS
138 if ((conf_syslog & module) && level >= conf_syslog_level) {
141 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
142 case HOSTAPD_LEVEL_DEBUG:
143 priority = LOG_DEBUG;
145 case HOSTAPD_LEVEL_INFO:
148 case HOSTAPD_LEVEL_NOTICE:
149 priority = LOG_NOTICE;
151 case HOSTAPD_LEVEL_WARNING:
152 priority = LOG_WARNING;
158 syslog(priority, "%s", format);
160 #endif /* CONFIG_NATIVE_WINDOWS */
164 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
168 * hostapd_init - Allocate and initialize per-interface data
169 * @config_file: Path to the configuration file
170 * Returns: Pointer to the allocated interface data or %NULL on failure
172 * This function is used to allocate main data structures for per-interface
173 * data. The allocated data buffer will be freed by calling
174 * hostapd_cleanup_iface().
176 static struct hostapd_iface * hostapd_init(const char *config_file)
178 struct hostapd_iface *hapd_iface = NULL;
179 struct hostapd_config *conf = NULL;
180 struct hostapd_data *hapd;
183 hapd_iface = os_zalloc(sizeof(*hapd_iface));
184 if (hapd_iface == NULL)
187 hapd_iface->reload_config = hostapd_reload_config;
188 hapd_iface->config_read_cb = hostapd_config_read;
189 hapd_iface->config_fname = os_strdup(config_file);
190 if (hapd_iface->config_fname == NULL)
192 hapd_iface->ctrl_iface_init = hostapd_ctrl_iface_init;
193 hapd_iface->ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
194 hapd_iface->for_each_interface = hostapd_for_each_interface;
196 conf = hostapd_config_read(hapd_iface->config_fname);
199 hapd_iface->conf = conf;
201 hapd_iface->num_bss = conf->num_bss;
202 hapd_iface->bss = os_zalloc(conf->num_bss *
203 sizeof(struct hostapd_data *));
204 if (hapd_iface->bss == NULL)
207 for (i = 0; i < conf->num_bss; i++) {
208 hapd = hapd_iface->bss[i] =
209 hostapd_alloc_bss_data(hapd_iface, conf,
213 hapd->msg_ctx = hapd;
220 hostapd_config_free(conf);
222 os_free(hapd_iface->config_fname);
223 os_free(hapd_iface->bss);
230 static int hostapd_driver_init(struct hostapd_iface *iface)
232 struct wpa_init_params params;
234 struct hostapd_data *hapd = iface->bss[0];
235 struct hostapd_bss_config *conf = hapd->conf;
237 struct wpa_driver_capa capa;
239 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
240 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
244 /* Initialize the driver interface */
245 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
248 os_memset(¶ms, 0, sizeof(params));
250 params.ifname = hapd->conf->iface;
251 params.ssid = (const u8 *) hapd->conf->ssid.ssid;
252 params.ssid_len = hapd->conf->ssid.ssid_len;
253 params.test_socket = hapd->conf->test_socket;
254 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
256 params.num_bridge = hapd->iface->num_bss;
257 params.bridge = os_zalloc(hapd->iface->num_bss * sizeof(char *));
258 if (params.bridge == NULL)
260 for (i = 0; i < hapd->iface->num_bss; i++) {
261 struct hostapd_data *bss = hapd->iface->bss[i];
262 if (bss->conf->bridge[0])
263 params.bridge[i] = bss->conf->bridge;
266 params.own_addr = hapd->own_addr;
268 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
269 os_free(params.bridge);
270 if (hapd->drv_priv == NULL) {
271 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
277 if (hapd->driver->get_capa &&
278 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0)
279 iface->drv_flags = capa.flags;
285 static void hostapd_interface_deinit_free(struct hostapd_iface *iface)
287 const struct wpa_driver_ops *driver;
291 driver = iface->bss[0]->driver;
292 drv_priv = iface->bss[0]->drv_priv;
293 hostapd_interface_deinit(iface);
294 if (driver && driver->hapd_deinit)
295 driver->hapd_deinit(drv_priv);
296 hostapd_interface_free(iface);
300 static struct hostapd_iface *
301 hostapd_interface_init(struct hapd_interfaces *interfaces,
302 const char *config_fname, int debug)
304 struct hostapd_iface *iface;
307 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
308 iface = hostapd_init(config_fname);
311 iface->interfaces = interfaces;
313 for (k = 0; k < debug; k++) {
314 if (iface->bss[0]->conf->logger_stdout_level > 0)
315 iface->bss[0]->conf->logger_stdout_level--;
318 if (hostapd_driver_init(iface) ||
319 hostapd_setup_interface(iface)) {
320 hostapd_interface_deinit_free(iface);
329 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
331 static void handle_term(int sig, void *signal_ctx)
333 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
338 #ifndef CONFIG_NATIVE_WINDOWS
340 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
342 if (hostapd_reload_config(iface) < 0) {
343 wpa_printf(MSG_WARNING, "Failed to read new configuration "
344 "file - continuing with old.");
351 * handle_reload - SIGHUP handler to reload configuration
353 static void handle_reload(int sig, void *signal_ctx)
355 struct hapd_interfaces *interfaces = signal_ctx;
356 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
358 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
362 static void handle_dump_state(int sig, void *signal_ctx)
364 #ifdef HOSTAPD_DUMP_STATE
365 struct hapd_interfaces *interfaces = signal_ctx;
366 hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
367 #endif /* HOSTAPD_DUMP_STATE */
369 #endif /* CONFIG_NATIVE_WINDOWS */
372 static int hostapd_global_init(struct hapd_interfaces *interfaces,
373 const char *entropy_file)
375 hostapd_logger_register_cb(hostapd_logger_cb);
377 if (eap_server_register_methods()) {
378 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
383 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
387 random_init(entropy_file);
389 #ifndef CONFIG_NATIVE_WINDOWS
390 eloop_register_signal(SIGHUP, handle_reload, interfaces);
391 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
392 #endif /* CONFIG_NATIVE_WINDOWS */
393 eloop_register_signal_terminate(handle_term, interfaces);
395 #ifndef CONFIG_NATIVE_WINDOWS
396 openlog("hostapd", 0, LOG_DAEMON);
397 #endif /* CONFIG_NATIVE_WINDOWS */
403 static void hostapd_global_deinit(const char *pid_file)
405 #ifdef EAP_SERVER_TNC
406 tncs_global_deinit();
407 #endif /* EAP_SERVER_TNC */
413 #ifndef CONFIG_NATIVE_WINDOWS
415 #endif /* CONFIG_NATIVE_WINDOWS */
417 eap_server_unregister_methods();
419 os_daemonize_terminate(pid_file);
423 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
424 const char *pid_file)
426 #ifdef EAP_SERVER_TNC
430 for (i = 0; !tnc && i < ifaces->count; i++) {
431 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
432 if (ifaces->iface[i]->bss[0]->conf->tnc) {
439 if (tnc && tncs_global_init() < 0) {
440 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
443 #endif /* EAP_SERVER_TNC */
445 if (daemonize && os_daemonize(pid_file)) {
456 static void show_version(void)
459 "hostapd v" VERSION_STR "\n"
460 "User space daemon for IEEE 802.11 AP management,\n"
461 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
462 "Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi> "
463 "and contributors\n");
467 static void usage(void)
472 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
473 "<configuration file(s)>\n"
476 " -h show this usage\n"
477 " -d show more debug messages (-dd for even more)\n"
478 " -B run daemon in the background\n"
481 " -K include key data in debug messages\n"
482 #ifdef CONFIG_DEBUG_FILE
483 " -f log output to debug file instead of stdout\n"
484 #endif /* CONFIG_DEBUG_FILE */
485 " -t include timestamps in some debug messages\n"
486 " -v show hostapd version\n");
492 static const char * hostapd_msg_ifname_cb(void *ctx)
494 struct hostapd_data *hapd = ctx;
495 if (hapd && hapd->iconf && hapd->iconf->bss)
496 return hapd->iconf->bss->iface;
501 int main(int argc, char *argv[])
503 struct hapd_interfaces interfaces;
506 int c, debug = 0, daemonize = 0;
507 char *pid_file = NULL;
508 const char *log_file = NULL;
509 const char *entropy_file = NULL;
511 if (os_program_init())
515 c = getopt(argc, argv, "Bde:f:hKP:tv");
524 if (wpa_debug_level > 0)
531 entropy_file = optarg;
537 wpa_debug_show_keys++;
541 pid_file = os_rel2abs_path(optarg);
544 wpa_debug_timestamp++;
560 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
563 wpa_debug_open_file(log_file);
565 interfaces.count = argc - optind;
566 interfaces.iface = os_zalloc(interfaces.count *
567 sizeof(struct hostapd_iface *));
568 if (interfaces.iface == NULL) {
569 wpa_printf(MSG_ERROR, "malloc failed");
573 if (hostapd_global_init(&interfaces, entropy_file))
576 /* Initialize interfaces */
577 for (i = 0; i < interfaces.count; i++) {
578 interfaces.iface[i] = hostapd_interface_init(&interfaces,
581 if (!interfaces.iface[i])
585 if (hostapd_global_run(&interfaces, daemonize, pid_file))
591 /* Deinitialize all interfaces */
592 for (i = 0; i < interfaces.count; i++)
593 hostapd_interface_deinit_free(interfaces.iface[i]);
594 os_free(interfaces.iface);
596 hostapd_global_deinit(pid_file);
600 wpa_debug_close_file();