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 "drivers/driver.h"
25 #include "eap_server/eap.h"
26 #include "eap_server/tncs.h"
27 #include "ap/hostapd.h"
28 #include "ap/config.h"
29 #include "config_file.h"
32 extern int wpa_debug_level;
33 extern int wpa_debug_show_keys;
34 extern int wpa_debug_timestamp;
37 struct hapd_interfaces {
39 struct hostapd_iface **iface;
43 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
44 int (*cb)(struct hostapd_iface *iface,
45 void *ctx), void *ctx)
50 for (i = 0; i < interfaces->count; i++) {
51 ret = cb(interfaces->iface[i], ctx);
60 #ifndef CONFIG_NO_HOSTAPD_LOGGER
61 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
62 int level, const char *txt, size_t len)
64 struct hostapd_data *hapd = ctx;
65 char *format, *module_str;
67 int conf_syslog_level, conf_stdout_level;
68 unsigned int conf_syslog, conf_stdout;
71 format = os_malloc(maxlen);
75 if (hapd && hapd->conf) {
76 conf_syslog_level = hapd->conf->logger_syslog_level;
77 conf_stdout_level = hapd->conf->logger_stdout_level;
78 conf_syslog = hapd->conf->logger_syslog;
79 conf_stdout = hapd->conf->logger_stdout;
81 conf_syslog_level = conf_stdout_level = 0;
82 conf_syslog = conf_stdout = (unsigned int) -1;
86 case HOSTAPD_MODULE_IEEE80211:
87 module_str = "IEEE 802.11";
89 case HOSTAPD_MODULE_IEEE8021X:
90 module_str = "IEEE 802.1X";
92 case HOSTAPD_MODULE_RADIUS:
93 module_str = "RADIUS";
95 case HOSTAPD_MODULE_WPA:
98 case HOSTAPD_MODULE_DRIVER:
99 module_str = "DRIVER";
101 case HOSTAPD_MODULE_IAPP:
104 case HOSTAPD_MODULE_MLME:
112 if (hapd && hapd->conf && addr)
113 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
114 hapd->conf->iface, MAC2STR(addr),
115 module_str ? " " : "", module_str, txt);
116 else if (hapd && hapd->conf)
117 os_snprintf(format, maxlen, "%s:%s%s %s",
118 hapd->conf->iface, module_str ? " " : "",
121 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
122 MAC2STR(addr), module_str ? " " : "",
125 os_snprintf(format, maxlen, "%s%s%s",
126 module_str, module_str ? ": " : "", txt);
128 if ((conf_stdout & module) && level >= conf_stdout_level) {
129 wpa_debug_print_timestamp();
130 printf("%s\n", format);
133 #ifndef CONFIG_NATIVE_WINDOWS
134 if ((conf_syslog & module) && level >= conf_syslog_level) {
137 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
138 case HOSTAPD_LEVEL_DEBUG:
139 priority = LOG_DEBUG;
141 case HOSTAPD_LEVEL_INFO:
144 case HOSTAPD_LEVEL_NOTICE:
145 priority = LOG_NOTICE;
147 case HOSTAPD_LEVEL_WARNING:
148 priority = LOG_WARNING;
154 syslog(priority, "%s", format);
156 #endif /* CONFIG_NATIVE_WINDOWS */
160 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
164 * hostapd_init - Allocate and initialize per-interface data
165 * @config_file: Path to the configuration file
166 * Returns: Pointer to the allocated interface data or %NULL on failure
168 * This function is used to allocate main data structures for per-interface
169 * data. The allocated data buffer will be freed by calling
170 * hostapd_cleanup_iface().
172 static struct hostapd_iface * hostapd_init(const char *config_file)
174 struct hostapd_iface *hapd_iface = NULL;
175 struct hostapd_config *conf = NULL;
176 struct hostapd_data *hapd;
179 hapd_iface = os_zalloc(sizeof(*hapd_iface));
180 if (hapd_iface == NULL)
183 hapd_iface->reload_config = hostapd_reload_config;
184 hapd_iface->config_read_cb = hostapd_config_read;
185 hapd_iface->config_fname = os_strdup(config_file);
186 if (hapd_iface->config_fname == NULL)
189 conf = hostapd_config_read(hapd_iface->config_fname);
192 hapd_iface->conf = conf;
194 hapd_iface->num_bss = conf->num_bss;
195 hapd_iface->bss = os_zalloc(conf->num_bss *
196 sizeof(struct hostapd_data *));
197 if (hapd_iface->bss == NULL)
200 for (i = 0; i < conf->num_bss; i++) {
201 hapd = hapd_iface->bss[i] =
202 hostapd_alloc_bss_data(hapd_iface, conf,
212 hostapd_config_free(conf);
214 os_free(hapd_iface->config_fname);
215 os_free(hapd_iface->bss);
222 static int hostapd_driver_init(struct hostapd_iface *iface)
224 struct wpa_init_params params;
226 struct hostapd_data *hapd = iface->bss[0];
227 struct hostapd_bss_config *conf = hapd->conf;
230 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
231 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
235 /* Initialize the driver interface */
236 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
239 os_memset(¶ms, 0, sizeof(params));
241 params.ifname = hapd->conf->iface;
242 params.ssid = (const u8 *) hapd->conf->ssid.ssid;
243 params.ssid_len = hapd->conf->ssid.ssid_len;
244 params.test_socket = hapd->conf->test_socket;
245 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
247 params.num_bridge = hapd->iface->num_bss;
248 params.bridge = os_zalloc(hapd->iface->num_bss * sizeof(char *));
249 if (params.bridge == NULL)
251 for (i = 0; i < hapd->iface->num_bss; i++) {
252 struct hostapd_data *bss = hapd->iface->bss[i];
253 if (bss->conf->bridge[0])
254 params.bridge[i] = bss->conf->bridge;
257 params.own_addr = hapd->own_addr;
259 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
260 os_free(params.bridge);
261 if (hapd->drv_priv == NULL) {
262 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
272 static struct hostapd_iface *
273 hostapd_interface_init(struct hapd_interfaces *interfaces,
274 const char *config_fname, int debug)
276 struct hostapd_iface *iface;
279 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
280 iface = hostapd_init(config_fname);
283 iface->interfaces = interfaces;
285 for (k = 0; k < debug; k++) {
286 if (iface->bss[0]->conf->logger_stdout_level > 0)
287 iface->bss[0]->conf->logger_stdout_level--;
290 if (hostapd_driver_init(iface) ||
291 hostapd_setup_interface(iface)) {
292 hostapd_interface_deinit(iface);
301 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
303 static void handle_term(int sig, void *signal_ctx)
305 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
310 #ifndef CONFIG_NATIVE_WINDOWS
312 * handle_reload - SIGHUP handler to reload configuration
314 static void handle_reload(int sig, void *signal_ctx)
316 struct hapd_interfaces *interfaces = signal_ctx;
317 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
319 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
323 static void handle_dump_state(int sig, void *signal_ctx)
325 #ifdef HOSTAPD_DUMP_STATE
326 struct hapd_interfaces *interfaces = signal_ctx;
327 hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
328 #endif /* HOSTAPD_DUMP_STATE */
330 #endif /* CONFIG_NATIVE_WINDOWS */
333 static int hostapd_global_init(struct hapd_interfaces *interfaces)
335 hostapd_logger_register_cb(hostapd_logger_cb);
337 if (eap_server_register_methods()) {
338 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
343 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
347 #ifndef CONFIG_NATIVE_WINDOWS
348 eloop_register_signal(SIGHUP, handle_reload, interfaces);
349 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
350 #endif /* CONFIG_NATIVE_WINDOWS */
351 eloop_register_signal_terminate(handle_term, interfaces);
353 #ifndef CONFIG_NATIVE_WINDOWS
354 openlog("hostapd", 0, LOG_DAEMON);
355 #endif /* CONFIG_NATIVE_WINDOWS */
361 static void hostapd_global_deinit(const char *pid_file)
363 #ifdef EAP_SERVER_TNC
364 tncs_global_deinit();
365 #endif /* EAP_SERVER_TNC */
369 #ifndef CONFIG_NATIVE_WINDOWS
371 #endif /* CONFIG_NATIVE_WINDOWS */
373 eap_server_unregister_methods();
375 os_daemonize_terminate(pid_file);
379 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
380 const char *pid_file)
382 #ifdef EAP_SERVER_TNC
386 for (i = 0; !tnc && i < ifaces->count; i++) {
387 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
388 if (ifaces->iface[i]->bss[0]->conf->tnc) {
395 if (tnc && tncs_global_init() < 0) {
396 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
399 #endif /* EAP_SERVER_TNC */
401 if (daemonize && os_daemonize(pid_file)) {
412 static void show_version(void)
415 "hostapd v" VERSION_STR "\n"
416 "User space daemon for IEEE 802.11 AP management,\n"
417 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
418 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
419 "and contributors\n");
423 static void usage(void)
428 "usage: hostapd [-hdBKtv] [-P <PID file>] "
429 "<configuration file(s)>\n"
432 " -h show this usage\n"
433 " -d show more debug messages (-dd for even more)\n"
434 " -B run daemon in the background\n"
436 " -K include key data in debug messages\n"
437 " -t include timestamps in some debug messages\n"
438 " -v show hostapd version\n");
444 int main(int argc, char *argv[])
446 struct hapd_interfaces interfaces;
449 int c, debug = 0, daemonize = 0;
450 char *pid_file = NULL;
452 if (os_program_init())
456 c = getopt(argc, argv, "BdhKP:tv");
465 if (wpa_debug_level > 0)
472 wpa_debug_show_keys++;
476 pid_file = os_rel2abs_path(optarg);
479 wpa_debug_timestamp++;
495 interfaces.count = argc - optind;
496 interfaces.iface = os_malloc(interfaces.count *
497 sizeof(struct hostapd_iface *));
498 if (interfaces.iface == NULL) {
499 wpa_printf(MSG_ERROR, "malloc failed\n");
503 if (hostapd_global_init(&interfaces))
506 /* Initialize interfaces */
507 for (i = 0; i < interfaces.count; i++) {
508 interfaces.iface[i] = hostapd_interface_init(&interfaces,
511 if (!interfaces.iface[i])
515 if (hostapd_global_run(&interfaces, daemonize, pid_file))
521 /* Deinitialize all interfaces */
522 for (i = 0; i < interfaces.count; i++)
523 hostapd_interface_deinit(interfaces.iface[i]);
524 os_free(interfaces.iface);
526 hostapd_global_deinit(pid_file);