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 "config_file.h"
25 #include "eap_register.h"
26 #include "dump_state.h"
27 #include "ctrl_iface.h"
30 extern int wpa_debug_level;
31 extern int wpa_debug_show_keys;
32 extern int wpa_debug_timestamp;
34 extern struct wpa_driver_ops *wpa_drivers[];
42 static struct hapd_global global;
45 struct hapd_interfaces {
47 struct hostapd_iface **iface;
51 static int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
52 int (*cb)(struct hostapd_iface *iface,
53 void *ctx), void *ctx)
58 for (i = 0; i < interfaces->count; i++) {
59 ret = cb(interfaces->iface[i], ctx);
68 #ifndef CONFIG_NO_HOSTAPD_LOGGER
69 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
70 int level, const char *txt, size_t len)
72 struct hostapd_data *hapd = ctx;
73 char *format, *module_str;
75 int conf_syslog_level, conf_stdout_level;
76 unsigned int conf_syslog, conf_stdout;
79 format = os_malloc(maxlen);
83 if (hapd && hapd->conf) {
84 conf_syslog_level = hapd->conf->logger_syslog_level;
85 conf_stdout_level = hapd->conf->logger_stdout_level;
86 conf_syslog = hapd->conf->logger_syslog;
87 conf_stdout = hapd->conf->logger_stdout;
89 conf_syslog_level = conf_stdout_level = 0;
90 conf_syslog = conf_stdout = (unsigned int) -1;
94 case HOSTAPD_MODULE_IEEE80211:
95 module_str = "IEEE 802.11";
97 case HOSTAPD_MODULE_IEEE8021X:
98 module_str = "IEEE 802.1X";
100 case HOSTAPD_MODULE_RADIUS:
101 module_str = "RADIUS";
103 case HOSTAPD_MODULE_WPA:
106 case HOSTAPD_MODULE_DRIVER:
107 module_str = "DRIVER";
109 case HOSTAPD_MODULE_IAPP:
112 case HOSTAPD_MODULE_MLME:
120 if (hapd && hapd->conf && addr)
121 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
122 hapd->conf->iface, MAC2STR(addr),
123 module_str ? " " : "", module_str, txt);
124 else if (hapd && hapd->conf)
125 os_snprintf(format, maxlen, "%s:%s%s %s",
126 hapd->conf->iface, module_str ? " " : "",
129 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
130 MAC2STR(addr), module_str ? " " : "",
133 os_snprintf(format, maxlen, "%s%s%s",
134 module_str, module_str ? ": " : "", txt);
136 if ((conf_stdout & module) && level >= conf_stdout_level) {
137 wpa_debug_print_timestamp();
138 printf("%s\n", format);
141 #ifndef CONFIG_NATIVE_WINDOWS
142 if ((conf_syslog & module) && level >= conf_syslog_level) {
145 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
146 case HOSTAPD_LEVEL_DEBUG:
147 priority = LOG_DEBUG;
149 case HOSTAPD_LEVEL_INFO:
152 case HOSTAPD_LEVEL_NOTICE:
153 priority = LOG_NOTICE;
155 case HOSTAPD_LEVEL_WARNING:
156 priority = LOG_WARNING;
162 syslog(priority, "%s", format);
164 #endif /* CONFIG_NATIVE_WINDOWS */
168 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
172 * hostapd_init - Allocate and initialize per-interface data
173 * @config_file: Path to the configuration file
174 * Returns: Pointer to the allocated interface data or %NULL on failure
176 * This function is used to allocate main data structures for per-interface
177 * data. The allocated data buffer will be freed by calling
178 * hostapd_cleanup_iface().
180 static struct hostapd_iface * hostapd_init(const char *config_file)
182 struct hostapd_iface *hapd_iface = NULL;
183 struct hostapd_config *conf = NULL;
184 struct hostapd_data *hapd;
187 hapd_iface = os_zalloc(sizeof(*hapd_iface));
188 if (hapd_iface == NULL)
191 hapd_iface->reload_config = hostapd_reload_config;
192 hapd_iface->config_read_cb = hostapd_config_read;
193 hapd_iface->config_fname = os_strdup(config_file);
194 if (hapd_iface->config_fname == NULL)
196 hapd_iface->ctrl_iface_init = hostapd_ctrl_iface_init;
197 hapd_iface->ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
198 hapd_iface->for_each_interface = hostapd_for_each_interface;
200 conf = hostapd_config_read(hapd_iface->config_fname);
203 hapd_iface->conf = conf;
205 hapd_iface->num_bss = conf->num_bss;
206 hapd_iface->bss = os_zalloc(conf->num_bss *
207 sizeof(struct hostapd_data *));
208 if (hapd_iface->bss == NULL)
211 for (i = 0; i < conf->num_bss; i++) {
212 hapd = hapd_iface->bss[i] =
213 hostapd_alloc_bss_data(hapd_iface, conf,
217 hapd->msg_ctx = hapd;
224 hostapd_config_free(conf);
226 os_free(hapd_iface->config_fname);
227 os_free(hapd_iface->bss);
234 static int hostapd_driver_init(struct hostapd_iface *iface)
236 struct wpa_init_params params;
238 struct hostapd_data *hapd = iface->bss[0];
239 struct hostapd_bss_config *conf = hapd->conf;
241 struct wpa_driver_capa capa;
243 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
244 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
248 /* Initialize the driver interface */
249 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
252 os_memset(¶ms, 0, sizeof(params));
253 for (i = 0; wpa_drivers[i]; i++) {
254 if (wpa_drivers[i] != hapd->driver)
257 if (global.drv_priv[i] == NULL &&
258 wpa_drivers[i]->global_init) {
259 global.drv_priv[i] = wpa_drivers[i]->global_init();
260 if (global.drv_priv[i] == NULL) {
261 wpa_printf(MSG_ERROR, "Failed to initialize "
263 wpa_drivers[i]->name);
268 params.global_priv = global.drv_priv[i];
272 params.ifname = hapd->conf->iface;
273 params.ssid = (const u8 *) hapd->conf->ssid.ssid;
274 params.ssid_len = hapd->conf->ssid.ssid_len;
275 params.test_socket = hapd->conf->test_socket;
276 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
278 params.num_bridge = hapd->iface->num_bss;
279 params.bridge = os_zalloc(hapd->iface->num_bss * sizeof(char *));
280 if (params.bridge == NULL)
282 for (i = 0; i < hapd->iface->num_bss; i++) {
283 struct hostapd_data *bss = hapd->iface->bss[i];
284 if (bss->conf->bridge[0])
285 params.bridge[i] = bss->conf->bridge;
288 params.own_addr = hapd->own_addr;
290 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
291 os_free(params.bridge);
292 if (hapd->drv_priv == NULL) {
293 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
299 if (hapd->driver->get_capa &&
300 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
301 iface->drv_flags = capa.flags;
302 iface->probe_resp_offloads = capa.probe_resp_offloads;
309 static void hostapd_interface_deinit_free(struct hostapd_iface *iface)
311 const struct wpa_driver_ops *driver;
315 driver = iface->bss[0]->driver;
316 drv_priv = iface->bss[0]->drv_priv;
317 hostapd_interface_deinit(iface);
318 if (driver && driver->hapd_deinit)
319 driver->hapd_deinit(drv_priv);
320 hostapd_interface_free(iface);
324 static struct hostapd_iface *
325 hostapd_interface_init(struct hapd_interfaces *interfaces,
326 const char *config_fname, int debug)
328 struct hostapd_iface *iface;
331 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
332 iface = hostapd_init(config_fname);
335 iface->interfaces = interfaces;
337 for (k = 0; k < debug; k++) {
338 if (iface->bss[0]->conf->logger_stdout_level > 0)
339 iface->bss[0]->conf->logger_stdout_level--;
342 if (hostapd_driver_init(iface) ||
343 hostapd_setup_interface(iface)) {
344 hostapd_interface_deinit_free(iface);
353 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
355 static void handle_term(int sig, void *signal_ctx)
357 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
362 #ifndef CONFIG_NATIVE_WINDOWS
364 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
366 if (hostapd_reload_config(iface) < 0) {
367 wpa_printf(MSG_WARNING, "Failed to read new configuration "
368 "file - continuing with old.");
375 * handle_reload - SIGHUP handler to reload configuration
377 static void handle_reload(int sig, void *signal_ctx)
379 struct hapd_interfaces *interfaces = signal_ctx;
380 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
382 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
386 static void handle_dump_state(int sig, void *signal_ctx)
388 #ifdef HOSTAPD_DUMP_STATE
389 struct hapd_interfaces *interfaces = signal_ctx;
390 hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
391 #endif /* HOSTAPD_DUMP_STATE */
393 #endif /* CONFIG_NATIVE_WINDOWS */
396 static int hostapd_global_init(struct hapd_interfaces *interfaces,
397 const char *entropy_file)
401 os_memset(&global, 0, sizeof(global));
403 hostapd_logger_register_cb(hostapd_logger_cb);
405 if (eap_server_register_methods()) {
406 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
411 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
415 random_init(entropy_file);
417 #ifndef CONFIG_NATIVE_WINDOWS
418 eloop_register_signal(SIGHUP, handle_reload, interfaces);
419 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
420 #endif /* CONFIG_NATIVE_WINDOWS */
421 eloop_register_signal_terminate(handle_term, interfaces);
423 #ifndef CONFIG_NATIVE_WINDOWS
424 openlog("hostapd", 0, LOG_DAEMON);
425 #endif /* CONFIG_NATIVE_WINDOWS */
427 for (i = 0; wpa_drivers[i]; i++)
429 if (global.drv_count == 0) {
430 wpa_printf(MSG_ERROR, "No drivers enabled");
433 global.drv_priv = os_zalloc(global.drv_count * sizeof(void *));
434 if (global.drv_priv == NULL)
441 static void hostapd_global_deinit(const char *pid_file)
445 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
446 if (!global.drv_priv[i])
448 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
450 os_free(global.drv_priv);
451 global.drv_priv = NULL;
453 #ifdef EAP_SERVER_TNC
454 tncs_global_deinit();
455 #endif /* EAP_SERVER_TNC */
461 #ifndef CONFIG_NATIVE_WINDOWS
463 #endif /* CONFIG_NATIVE_WINDOWS */
465 eap_server_unregister_methods();
467 os_daemonize_terminate(pid_file);
471 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
472 const char *pid_file)
474 #ifdef EAP_SERVER_TNC
478 for (i = 0; !tnc && i < ifaces->count; i++) {
479 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
480 if (ifaces->iface[i]->bss[0]->conf->tnc) {
487 if (tnc && tncs_global_init() < 0) {
488 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
491 #endif /* EAP_SERVER_TNC */
493 if (daemonize && os_daemonize(pid_file)) {
504 static void show_version(void)
507 "hostapd v" VERSION_STR "\n"
508 "User space daemon for IEEE 802.11 AP management,\n"
509 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
510 "Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi> "
511 "and contributors\n");
515 static void usage(void)
520 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
521 "<configuration file(s)>\n"
524 " -h show this usage\n"
525 " -d show more debug messages (-dd for even more)\n"
526 " -B run daemon in the background\n"
529 " -K include key data in debug messages\n"
530 #ifdef CONFIG_DEBUG_FILE
531 " -f log output to debug file instead of stdout\n"
532 #endif /* CONFIG_DEBUG_FILE */
533 " -t include timestamps in some debug messages\n"
534 " -v show hostapd version\n");
540 static const char * hostapd_msg_ifname_cb(void *ctx)
542 struct hostapd_data *hapd = ctx;
543 if (hapd && hapd->iconf && hapd->iconf->bss)
544 return hapd->iconf->bss->iface;
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())
563 c = getopt(argc, argv, "Bde:f:hKP:tv");
572 if (wpa_debug_level > 0)
579 entropy_file = optarg;
585 wpa_debug_show_keys++;
589 pid_file = os_rel2abs_path(optarg);
592 wpa_debug_timestamp++;
608 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
611 wpa_debug_open_file(log_file);
613 interfaces.count = argc - optind;
614 interfaces.iface = os_zalloc(interfaces.count *
615 sizeof(struct hostapd_iface *));
616 if (interfaces.iface == NULL) {
617 wpa_printf(MSG_ERROR, "malloc failed");
621 if (hostapd_global_init(&interfaces, entropy_file))
624 /* Initialize interfaces */
625 for (i = 0; i < interfaces.count; i++) {
626 interfaces.iface[i] = hostapd_interface_init(&interfaces,
629 if (!interfaces.iface[i])
633 if (hostapd_global_run(&interfaces, daemonize, pid_file))
639 /* Deinitialize all interfaces */
640 for (i = 0; i < interfaces.count; i++)
641 hostapd_interface_deinit_free(interfaces.iface[i]);
642 os_free(interfaces.iface);
644 hostapd_global_deinit(pid_file);
648 wpa_debug_close_file();