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
13 #endif /* CONFIG_NATIVE_WINDOWS */
15 #include "utils/common.h"
16 #include "utils/eloop.h"
17 #include "crypto/random.h"
18 #include "crypto/tls.h"
19 #include "common/version.h"
20 #include "drivers/driver.h"
21 #include "eap_server/eap.h"
22 #include "eap_server/tncs.h"
23 #include "ap/hostapd.h"
24 #include "ap/ap_config.h"
25 #include "ap/ap_drv_ops.h"
26 #include "config_file.h"
27 #include "eap_register.h"
28 #include "dump_state.h"
29 #include "ctrl_iface.h"
32 extern int wpa_debug_level;
33 extern int wpa_debug_show_keys;
34 extern int wpa_debug_timestamp;
36 extern struct wpa_driver_ops *wpa_drivers[];
44 static struct hapd_global global;
47 #ifndef CONFIG_NO_HOSTAPD_LOGGER
48 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
49 int level, const char *txt, size_t len)
51 struct hostapd_data *hapd = ctx;
52 char *format, *module_str;
54 int conf_syslog_level, conf_stdout_level;
55 unsigned int conf_syslog, conf_stdout;
58 format = os_malloc(maxlen);
62 if (hapd && hapd->conf) {
63 conf_syslog_level = hapd->conf->logger_syslog_level;
64 conf_stdout_level = hapd->conf->logger_stdout_level;
65 conf_syslog = hapd->conf->logger_syslog;
66 conf_stdout = hapd->conf->logger_stdout;
68 conf_syslog_level = conf_stdout_level = 0;
69 conf_syslog = conf_stdout = (unsigned int) -1;
73 case HOSTAPD_MODULE_IEEE80211:
74 module_str = "IEEE 802.11";
76 case HOSTAPD_MODULE_IEEE8021X:
77 module_str = "IEEE 802.1X";
79 case HOSTAPD_MODULE_RADIUS:
80 module_str = "RADIUS";
82 case HOSTAPD_MODULE_WPA:
85 case HOSTAPD_MODULE_DRIVER:
86 module_str = "DRIVER";
88 case HOSTAPD_MODULE_IAPP:
91 case HOSTAPD_MODULE_MLME:
99 if (hapd && hapd->conf && addr)
100 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
101 hapd->conf->iface, MAC2STR(addr),
102 module_str ? " " : "", module_str, txt);
103 else if (hapd && hapd->conf)
104 os_snprintf(format, maxlen, "%s:%s%s %s",
105 hapd->conf->iface, module_str ? " " : "",
108 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
109 MAC2STR(addr), module_str ? " " : "",
112 os_snprintf(format, maxlen, "%s%s%s",
113 module_str, module_str ? ": " : "", txt);
115 if ((conf_stdout & module) && level >= conf_stdout_level) {
116 wpa_debug_print_timestamp();
117 printf("%s\n", format);
120 #ifndef CONFIG_NATIVE_WINDOWS
121 if ((conf_syslog & module) && level >= conf_syslog_level) {
124 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
125 case HOSTAPD_LEVEL_DEBUG:
126 priority = LOG_DEBUG;
128 case HOSTAPD_LEVEL_INFO:
131 case HOSTAPD_LEVEL_NOTICE:
132 priority = LOG_NOTICE;
134 case HOSTAPD_LEVEL_WARNING:
135 priority = LOG_WARNING;
141 syslog(priority, "%s", format);
143 #endif /* CONFIG_NATIVE_WINDOWS */
147 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
151 * hostapd_init - Allocate and initialize per-interface data
152 * @config_file: Path to the configuration file
153 * Returns: Pointer to the allocated interface data or %NULL on failure
155 * This function is used to allocate main data structures for per-interface
156 * data. The allocated data buffer will be freed by calling
157 * hostapd_cleanup_iface().
159 static struct hostapd_iface * hostapd_init(const char *config_file)
161 struct hostapd_iface *hapd_iface = NULL;
162 struct hostapd_config *conf = NULL;
163 struct hostapd_data *hapd;
166 hapd_iface = os_zalloc(sizeof(*hapd_iface));
167 if (hapd_iface == NULL)
170 hapd_iface->config_fname = os_strdup(config_file);
171 if (hapd_iface->config_fname == NULL)
174 conf = hostapd_config_read(hapd_iface->config_fname);
177 hapd_iface->conf = conf;
179 hapd_iface->num_bss = conf->num_bss;
180 hapd_iface->bss = os_calloc(conf->num_bss,
181 sizeof(struct hostapd_data *));
182 if (hapd_iface->bss == NULL)
185 for (i = 0; i < conf->num_bss; i++) {
186 hapd = hapd_iface->bss[i] =
187 hostapd_alloc_bss_data(hapd_iface, conf,
191 hapd->msg_ctx = hapd;
197 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
200 hostapd_config_free(conf);
202 os_free(hapd_iface->config_fname);
203 os_free(hapd_iface->bss);
210 static int hostapd_driver_init(struct hostapd_iface *iface)
212 struct wpa_init_params params;
214 struct hostapd_data *hapd = iface->bss[0];
215 struct hostapd_bss_config *conf = hapd->conf;
217 struct wpa_driver_capa capa;
219 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
220 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
224 /* Initialize the driver interface */
225 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
228 os_memset(¶ms, 0, sizeof(params));
229 for (i = 0; wpa_drivers[i]; i++) {
230 if (wpa_drivers[i] != hapd->driver)
233 if (global.drv_priv[i] == NULL &&
234 wpa_drivers[i]->global_init) {
235 global.drv_priv[i] = wpa_drivers[i]->global_init();
236 if (global.drv_priv[i] == NULL) {
237 wpa_printf(MSG_ERROR, "Failed to initialize "
239 wpa_drivers[i]->name);
244 params.global_priv = global.drv_priv[i];
248 params.ifname = hapd->conf->iface;
249 params.ssid = hapd->conf->ssid.ssid;
250 params.ssid_len = hapd->conf->ssid.ssid_len;
251 params.test_socket = hapd->conf->test_socket;
252 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
254 params.num_bridge = hapd->iface->num_bss;
255 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
256 if (params.bridge == NULL)
258 for (i = 0; i < hapd->iface->num_bss; i++) {
259 struct hostapd_data *bss = hapd->iface->bss[i];
260 if (bss->conf->bridge[0])
261 params.bridge[i] = bss->conf->bridge;
264 params.own_addr = hapd->own_addr;
266 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
267 os_free(params.bridge);
268 if (hapd->drv_priv == NULL) {
269 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
275 if (hapd->driver->get_capa &&
276 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
277 iface->drv_flags = capa.flags;
278 iface->probe_resp_offloads = capa.probe_resp_offloads;
279 iface->extended_capa = capa.extended_capa;
280 iface->extended_capa_mask = capa.extended_capa_mask;
281 iface->extended_capa_len = capa.extended_capa_len;
282 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
285 #ifdef CONFIG_INTERWORKING
286 if (hapd->driver->set_qos_map && conf->qos_map_set_len &&
287 hapd->driver->set_qos_map(hapd->drv_priv, conf->qos_map_set,
288 conf->qos_map_set_len)) {
289 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map.");
292 #endif /* CONFIG_INTERWORKING */
298 static struct hostapd_iface *
299 hostapd_interface_init(struct hapd_interfaces *interfaces,
300 const char *config_fname, int debug)
302 struct hostapd_iface *iface;
305 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
306 iface = hostapd_init(config_fname);
309 iface->interfaces = interfaces;
311 for (k = 0; k < debug; k++) {
312 if (iface->bss[0]->conf->logger_stdout_level > 0)
313 iface->bss[0]->conf->logger_stdout_level--;
316 if (iface->conf->bss[0].iface[0] == '\0' &&
317 !hostapd_drv_none(iface->bss[0])) {
318 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
320 hostapd_interface_deinit_free(iface);
324 if (hostapd_driver_init(iface) ||
325 hostapd_setup_interface(iface)) {
326 hostapd_interface_deinit_free(iface);
335 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
337 static void handle_term(int sig, void *signal_ctx)
339 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
344 #ifndef CONFIG_NATIVE_WINDOWS
346 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
348 if (hostapd_reload_config(iface) < 0) {
349 wpa_printf(MSG_WARNING, "Failed to read new configuration "
350 "file - continuing with old.");
357 * handle_reload - SIGHUP handler to reload configuration
359 static void handle_reload(int sig, void *signal_ctx)
361 struct hapd_interfaces *interfaces = signal_ctx;
362 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
364 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
368 static void handle_dump_state(int sig, void *signal_ctx)
370 #ifdef HOSTAPD_DUMP_STATE
371 struct hapd_interfaces *interfaces = signal_ctx;
372 hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
373 #endif /* HOSTAPD_DUMP_STATE */
375 #endif /* CONFIG_NATIVE_WINDOWS */
378 static int hostapd_global_init(struct hapd_interfaces *interfaces,
379 const char *entropy_file)
383 os_memset(&global, 0, sizeof(global));
385 hostapd_logger_register_cb(hostapd_logger_cb);
387 if (eap_server_register_methods()) {
388 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
393 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
397 random_init(entropy_file);
399 #ifndef CONFIG_NATIVE_WINDOWS
400 eloop_register_signal(SIGHUP, handle_reload, interfaces);
401 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
402 #endif /* CONFIG_NATIVE_WINDOWS */
403 eloop_register_signal_terminate(handle_term, interfaces);
405 #ifndef CONFIG_NATIVE_WINDOWS
406 openlog("hostapd", 0, LOG_DAEMON);
407 #endif /* CONFIG_NATIVE_WINDOWS */
409 for (i = 0; wpa_drivers[i]; i++)
411 if (global.drv_count == 0) {
412 wpa_printf(MSG_ERROR, "No drivers enabled");
415 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
416 if (global.drv_priv == NULL)
423 static void hostapd_global_deinit(const char *pid_file)
427 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
428 if (!global.drv_priv[i])
430 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
432 os_free(global.drv_priv);
433 global.drv_priv = NULL;
435 #ifdef EAP_SERVER_TNC
436 tncs_global_deinit();
437 #endif /* EAP_SERVER_TNC */
443 #ifndef CONFIG_NATIVE_WINDOWS
445 #endif /* CONFIG_NATIVE_WINDOWS */
447 eap_server_unregister_methods();
449 os_daemonize_terminate(pid_file);
453 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
454 const char *pid_file)
456 #ifdef EAP_SERVER_TNC
460 for (i = 0; !tnc && i < ifaces->count; i++) {
461 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
462 if (ifaces->iface[i]->bss[0]->conf->tnc) {
469 if (tnc && tncs_global_init() < 0) {
470 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
473 #endif /* EAP_SERVER_TNC */
475 if (daemonize && os_daemonize(pid_file)) {
486 static void show_version(void)
489 "hostapd v" VERSION_STR "\n"
490 "User space daemon for IEEE 802.11 AP management,\n"
491 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
492 "Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> "
493 "and contributors\n");
497 static void usage(void)
502 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
504 " [-g <global ctrl_iface>] [-G <group>] \\\n"
505 " <configuration file(s)>\n"
508 " -h show this usage\n"
509 " -d show more debug messages (-dd for even more)\n"
510 " -B run daemon in the background\n"
512 " -g global control interface path\n"
513 " -G group for control interfaces\n"
515 " -K include key data in debug messages\n"
516 #ifdef CONFIG_DEBUG_FILE
517 " -f log output to debug file instead of stdout\n"
518 #endif /* CONFIG_DEBUG_FILE */
519 " -t include timestamps in some debug messages\n"
520 " -v show hostapd version\n");
526 static const char * hostapd_msg_ifname_cb(void *ctx)
528 struct hostapd_data *hapd = ctx;
529 if (hapd && hapd->iconf && hapd->iconf->bss)
530 return hapd->iconf->bss->iface;
535 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
539 os_free(interfaces->global_iface_path);
540 interfaces->global_iface_path = os_strdup(path);
541 if (interfaces->global_iface_path == NULL)
543 pos = os_strrchr(interfaces->global_iface_path, '/');
545 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
547 os_free(interfaces->global_iface_path);
548 interfaces->global_iface_path = NULL;
553 interfaces->global_iface_name = pos + 1;
559 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
562 #ifndef CONFIG_NATIVE_WINDOWS
564 grp = getgrnam(group);
566 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
569 interfaces->ctrl_iface_group = grp->gr_gid;
570 #endif /* CONFIG_NATIVE_WINDOWS */
575 int main(int argc, char *argv[])
577 struct hapd_interfaces interfaces;
580 int c, debug = 0, daemonize = 0;
581 char *pid_file = NULL;
582 const char *log_file = NULL;
583 const char *entropy_file = NULL;
585 if (os_program_init())
588 os_memset(&interfaces, 0, sizeof(interfaces));
589 interfaces.reload_config = hostapd_reload_config;
590 interfaces.config_read_cb = hostapd_config_read;
591 interfaces.for_each_interface = hostapd_for_each_interface;
592 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
593 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
594 interfaces.driver_init = hostapd_driver_init;
595 interfaces.global_iface_path = NULL;
596 interfaces.global_iface_name = NULL;
597 interfaces.global_ctrl_sock = -1;
600 c = getopt(argc, argv, "Bde:f:hKP:tvg:G:");
609 if (wpa_debug_level > 0)
616 entropy_file = optarg;
622 wpa_debug_show_keys++;
626 pid_file = os_rel2abs_path(optarg);
629 wpa_debug_timestamp++;
636 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
640 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
649 if (optind == argc && interfaces.global_iface_path == NULL)
652 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
655 wpa_debug_open_file(log_file);
657 interfaces.count = argc - optind;
658 if (interfaces.count) {
659 interfaces.iface = os_calloc(interfaces.count,
660 sizeof(struct hostapd_iface *));
661 if (interfaces.iface == NULL) {
662 wpa_printf(MSG_ERROR, "malloc failed");
667 if (hostapd_global_init(&interfaces, entropy_file)) {
668 wpa_printf(MSG_ERROR, "Failed to initilize global context");
672 /* Initialize interfaces */
673 for (i = 0; i < interfaces.count; i++) {
674 interfaces.iface[i] = hostapd_interface_init(&interfaces,
677 if (!interfaces.iface[i]) {
678 wpa_printf(MSG_ERROR, "Failed to initialize interface");
683 hostapd_global_ctrl_iface_init(&interfaces);
685 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
686 wpa_printf(MSG_ERROR, "Failed to start eloop");
693 hostapd_global_ctrl_iface_deinit(&interfaces);
694 /* Deinitialize all interfaces */
695 for (i = 0; i < interfaces.count; i++)
696 hostapd_interface_deinit_free(interfaces.iface[i]);
697 os_free(interfaces.iface);
699 hostapd_global_deinit(pid_file);
703 wpa_debug_close_file();