3 * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
10 #ifndef CONFIG_NATIVE_WINDOWS
12 #endif /* CONFIG_NATIVE_WINDOWS */
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "crypto/random.h"
17 #include "crypto/tls.h"
18 #include "common/version.h"
19 #include "drivers/driver.h"
20 #include "eap_server/eap.h"
21 #include "eap_server/tncs.h"
22 #include "ap/hostapd.h"
23 #include "ap/ap_config.h"
24 #include "ap/ap_drv_ops.h"
25 #include "config_file.h"
26 #include "eap_register.h"
27 #include "dump_state.h"
28 #include "ctrl_iface.h"
31 extern int wpa_debug_level;
32 extern int wpa_debug_show_keys;
33 extern int wpa_debug_timestamp;
35 extern struct wpa_driver_ops *wpa_drivers[];
43 static struct hapd_global global;
46 #ifndef CONFIG_NO_HOSTAPD_LOGGER
47 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
48 int level, const char *txt, size_t len)
50 struct hostapd_data *hapd = ctx;
51 char *format, *module_str;
53 int conf_syslog_level, conf_stdout_level;
54 unsigned int conf_syslog, conf_stdout;
57 format = os_malloc(maxlen);
61 if (hapd && hapd->conf) {
62 conf_syslog_level = hapd->conf->logger_syslog_level;
63 conf_stdout_level = hapd->conf->logger_stdout_level;
64 conf_syslog = hapd->conf->logger_syslog;
65 conf_stdout = hapd->conf->logger_stdout;
67 conf_syslog_level = conf_stdout_level = 0;
68 conf_syslog = conf_stdout = (unsigned int) -1;
72 case HOSTAPD_MODULE_IEEE80211:
73 module_str = "IEEE 802.11";
75 case HOSTAPD_MODULE_IEEE8021X:
76 module_str = "IEEE 802.1X";
78 case HOSTAPD_MODULE_RADIUS:
79 module_str = "RADIUS";
81 case HOSTAPD_MODULE_WPA:
84 case HOSTAPD_MODULE_DRIVER:
85 module_str = "DRIVER";
87 case HOSTAPD_MODULE_IAPP:
90 case HOSTAPD_MODULE_MLME:
98 if (hapd && hapd->conf && addr)
99 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
100 hapd->conf->iface, MAC2STR(addr),
101 module_str ? " " : "", module_str, txt);
102 else if (hapd && hapd->conf)
103 os_snprintf(format, maxlen, "%s:%s%s %s",
104 hapd->conf->iface, module_str ? " " : "",
107 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
108 MAC2STR(addr), module_str ? " " : "",
111 os_snprintf(format, maxlen, "%s%s%s",
112 module_str, module_str ? ": " : "", txt);
114 if ((conf_stdout & module) && level >= conf_stdout_level) {
115 wpa_debug_print_timestamp();
116 printf("%s\n", format);
119 #ifndef CONFIG_NATIVE_WINDOWS
120 if ((conf_syslog & module) && level >= conf_syslog_level) {
123 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
124 case HOSTAPD_LEVEL_DEBUG:
125 priority = LOG_DEBUG;
127 case HOSTAPD_LEVEL_INFO:
130 case HOSTAPD_LEVEL_NOTICE:
131 priority = LOG_NOTICE;
133 case HOSTAPD_LEVEL_WARNING:
134 priority = LOG_WARNING;
140 syslog(priority, "%s", format);
142 #endif /* CONFIG_NATIVE_WINDOWS */
146 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
150 * hostapd_init - Allocate and initialize per-interface data
151 * @config_file: Path to the configuration file
152 * Returns: Pointer to the allocated interface data or %NULL on failure
154 * This function is used to allocate main data structures for per-interface
155 * data. The allocated data buffer will be freed by calling
156 * hostapd_cleanup_iface().
158 static struct hostapd_iface * hostapd_init(const char *config_file)
160 struct hostapd_iface *hapd_iface = NULL;
161 struct hostapd_config *conf = NULL;
162 struct hostapd_data *hapd;
165 hapd_iface = os_zalloc(sizeof(*hapd_iface));
166 if (hapd_iface == NULL)
169 hapd_iface->config_fname = os_strdup(config_file);
170 if (hapd_iface->config_fname == NULL)
173 conf = hostapd_config_read(hapd_iface->config_fname);
176 hapd_iface->conf = conf;
178 hapd_iface->num_bss = conf->num_bss;
179 hapd_iface->bss = os_calloc(conf->num_bss,
180 sizeof(struct hostapd_data *));
181 if (hapd_iface->bss == NULL)
184 for (i = 0; i < conf->num_bss; i++) {
185 hapd = hapd_iface->bss[i] =
186 hostapd_alloc_bss_data(hapd_iface, conf,
190 hapd->msg_ctx = hapd;
197 hostapd_config_free(conf);
199 os_free(hapd_iface->config_fname);
200 os_free(hapd_iface->bss);
207 static int hostapd_driver_init(struct hostapd_iface *iface)
209 struct wpa_init_params params;
211 struct hostapd_data *hapd = iface->bss[0];
212 struct hostapd_bss_config *conf = hapd->conf;
214 struct wpa_driver_capa capa;
216 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
217 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
221 /* Initialize the driver interface */
222 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
225 os_memset(¶ms, 0, sizeof(params));
226 for (i = 0; wpa_drivers[i]; i++) {
227 if (wpa_drivers[i] != hapd->driver)
230 if (global.drv_priv[i] == NULL &&
231 wpa_drivers[i]->global_init) {
232 global.drv_priv[i] = wpa_drivers[i]->global_init();
233 if (global.drv_priv[i] == NULL) {
234 wpa_printf(MSG_ERROR, "Failed to initialize "
236 wpa_drivers[i]->name);
241 params.global_priv = global.drv_priv[i];
245 params.ifname = hapd->conf->iface;
246 params.ssid = hapd->conf->ssid.ssid;
247 params.ssid_len = hapd->conf->ssid.ssid_len;
248 params.test_socket = hapd->conf->test_socket;
249 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
251 params.num_bridge = hapd->iface->num_bss;
252 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
253 if (params.bridge == NULL)
255 for (i = 0; i < hapd->iface->num_bss; i++) {
256 struct hostapd_data *bss = hapd->iface->bss[i];
257 if (bss->conf->bridge[0])
258 params.bridge[i] = bss->conf->bridge;
261 params.own_addr = hapd->own_addr;
263 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
264 os_free(params.bridge);
265 if (hapd->drv_priv == NULL) {
266 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
272 if (hapd->driver->get_capa &&
273 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
274 iface->drv_flags = capa.flags;
275 iface->probe_resp_offloads = capa.probe_resp_offloads;
282 static void hostapd_interface_deinit_free(struct hostapd_iface *iface)
284 const struct wpa_driver_ops *driver;
288 driver = iface->bss[0]->driver;
289 drv_priv = iface->bss[0]->drv_priv;
290 hostapd_interface_deinit(iface);
291 if (driver && driver->hapd_deinit && drv_priv)
292 driver->hapd_deinit(drv_priv);
293 hostapd_interface_free(iface);
297 static struct hostapd_iface *
298 hostapd_interface_init(struct hapd_interfaces *interfaces,
299 const char *config_fname, int debug)
301 struct hostapd_iface *iface;
304 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
305 iface = hostapd_init(config_fname);
308 iface->interfaces = interfaces;
310 for (k = 0; k < debug; k++) {
311 if (iface->bss[0]->conf->logger_stdout_level > 0)
312 iface->bss[0]->conf->logger_stdout_level--;
315 if (iface->conf->bss[0].iface[0] != 0 ||
316 hostapd_drv_none(iface->bss[0])) {
317 if (hostapd_driver_init(iface) ||
318 hostapd_setup_interface(iface)) {
319 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)
377 os_memset(&global, 0, sizeof(global));
379 hostapd_logger_register_cb(hostapd_logger_cb);
381 if (eap_server_register_methods()) {
382 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
387 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
391 random_init(entropy_file);
393 #ifndef CONFIG_NATIVE_WINDOWS
394 eloop_register_signal(SIGHUP, handle_reload, interfaces);
395 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
396 #endif /* CONFIG_NATIVE_WINDOWS */
397 eloop_register_signal_terminate(handle_term, interfaces);
399 #ifndef CONFIG_NATIVE_WINDOWS
400 openlog("hostapd", 0, LOG_DAEMON);
401 #endif /* CONFIG_NATIVE_WINDOWS */
403 for (i = 0; wpa_drivers[i]; i++)
405 if (global.drv_count == 0) {
406 wpa_printf(MSG_ERROR, "No drivers enabled");
409 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
410 if (global.drv_priv == NULL)
417 static void hostapd_global_deinit(const char *pid_file)
421 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
422 if (!global.drv_priv[i])
424 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
426 os_free(global.drv_priv);
427 global.drv_priv = NULL;
429 #ifdef EAP_SERVER_TNC
430 tncs_global_deinit();
431 #endif /* EAP_SERVER_TNC */
437 #ifndef CONFIG_NATIVE_WINDOWS
439 #endif /* CONFIG_NATIVE_WINDOWS */
441 eap_server_unregister_methods();
443 os_daemonize_terminate(pid_file);
447 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
448 const char *pid_file)
450 #ifdef EAP_SERVER_TNC
454 for (i = 0; !tnc && i < ifaces->count; i++) {
455 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
456 if (ifaces->iface[i]->bss[0]->conf->tnc) {
463 if (tnc && tncs_global_init() < 0) {
464 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
467 #endif /* EAP_SERVER_TNC */
469 if (daemonize && os_daemonize(pid_file)) {
480 static void show_version(void)
483 "hostapd v" VERSION_STR "\n"
484 "User space daemon for IEEE 802.11 AP management,\n"
485 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
486 "Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi> "
487 "and contributors\n");
491 static void usage(void)
496 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
498 " [-g <global ctrl_iface>] <configuration file(s)>\n"
501 " -h show this usage\n"
502 " -d show more debug messages (-dd for even more)\n"
503 " -B run daemon in the background\n"
505 " -g global control interface path\n"
507 " -K include key data in debug messages\n"
508 #ifdef CONFIG_DEBUG_FILE
509 " -f log output to debug file instead of stdout\n"
510 #endif /* CONFIG_DEBUG_FILE */
511 " -t include timestamps in some debug messages\n"
512 " -v show hostapd version\n");
518 static const char * hostapd_msg_ifname_cb(void *ctx)
520 struct hostapd_data *hapd = ctx;
521 if (hapd && hapd->iconf && hapd->iconf->bss)
522 return hapd->iconf->bss->iface;
527 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
531 os_free(interfaces->global_iface_path);
532 interfaces->global_iface_path = os_strdup(path);
533 if (interfaces->global_iface_path == NULL)
535 pos = os_strrchr(interfaces->global_iface_path, '/');
537 os_free(interfaces->global_iface_path);
538 interfaces->global_iface_path = NULL;
543 interfaces->global_iface_name = pos + 1;
549 int main(int argc, char *argv[])
551 struct hapd_interfaces interfaces;
554 int c, debug = 0, daemonize = 0;
555 char *pid_file = NULL;
556 const char *log_file = NULL;
557 const char *entropy_file = NULL;
559 if (os_program_init())
562 os_memset(&interfaces, 0, sizeof(interfaces));
563 interfaces.reload_config = hostapd_reload_config;
564 interfaces.config_read_cb = hostapd_config_read;
565 interfaces.for_each_interface = hostapd_for_each_interface;
566 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
567 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
568 interfaces.global_iface_path = NULL;
569 interfaces.global_iface_name = NULL;
570 interfaces.global_ctrl_sock = -1;
573 c = getopt(argc, argv, "Bde:f:hKP:tvg:");
582 if (wpa_debug_level > 0)
589 entropy_file = optarg;
595 wpa_debug_show_keys++;
599 pid_file = os_rel2abs_path(optarg);
602 wpa_debug_timestamp++;
609 hostapd_get_global_ctrl_iface(&interfaces, optarg);
621 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
624 wpa_debug_open_file(log_file);
626 interfaces.count = argc - optind;
627 interfaces.iface = os_calloc(interfaces.count,
628 sizeof(struct hostapd_iface *));
629 if (interfaces.iface == NULL) {
630 wpa_printf(MSG_ERROR, "malloc failed");
634 if (hostapd_global_init(&interfaces, entropy_file))
637 /* Initialize interfaces */
638 for (i = 0; i < interfaces.count; i++) {
639 interfaces.iface[i] = hostapd_interface_init(&interfaces,
642 if (!interfaces.iface[i])
646 hostapd_global_ctrl_iface_init(&interfaces);
648 if (hostapd_global_run(&interfaces, daemonize, pid_file))
654 hostapd_global_ctrl_iface_deinit(&interfaces);
655 /* Deinitialize all interfaces */
656 for (i = 0; i < interfaces.count; i++)
657 hostapd_interface_deinit_free(interfaces.iface[i]);
658 os_free(interfaces.iface);
660 hostapd_global_deinit(pid_file);
664 wpa_debug_close_file();