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 "ctrl_iface.h"
36 static struct hapd_global global;
39 #ifndef CONFIG_NO_HOSTAPD_LOGGER
40 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
41 int level, const char *txt, size_t len)
43 struct hostapd_data *hapd = ctx;
44 char *format, *module_str;
46 int conf_syslog_level, conf_stdout_level;
47 unsigned int conf_syslog, conf_stdout;
50 format = os_malloc(maxlen);
54 if (hapd && hapd->conf) {
55 conf_syslog_level = hapd->conf->logger_syslog_level;
56 conf_stdout_level = hapd->conf->logger_stdout_level;
57 conf_syslog = hapd->conf->logger_syslog;
58 conf_stdout = hapd->conf->logger_stdout;
60 conf_syslog_level = conf_stdout_level = 0;
61 conf_syslog = conf_stdout = (unsigned int) -1;
65 case HOSTAPD_MODULE_IEEE80211:
66 module_str = "IEEE 802.11";
68 case HOSTAPD_MODULE_IEEE8021X:
69 module_str = "IEEE 802.1X";
71 case HOSTAPD_MODULE_RADIUS:
72 module_str = "RADIUS";
74 case HOSTAPD_MODULE_WPA:
77 case HOSTAPD_MODULE_DRIVER:
78 module_str = "DRIVER";
80 case HOSTAPD_MODULE_IAPP:
83 case HOSTAPD_MODULE_MLME:
91 if (hapd && hapd->conf && addr)
92 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
93 hapd->conf->iface, MAC2STR(addr),
94 module_str ? " " : "", module_str, txt);
95 else if (hapd && hapd->conf)
96 os_snprintf(format, maxlen, "%s:%s%s %s",
97 hapd->conf->iface, module_str ? " " : "",
100 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
101 MAC2STR(addr), module_str ? " " : "",
104 os_snprintf(format, maxlen, "%s%s%s",
105 module_str, module_str ? ": " : "", txt);
107 if ((conf_stdout & module) && level >= conf_stdout_level) {
108 wpa_debug_print_timestamp();
109 wpa_printf(MSG_INFO, "%s", format);
112 #ifndef CONFIG_NATIVE_WINDOWS
113 if ((conf_syslog & module) && level >= conf_syslog_level) {
116 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
117 case HOSTAPD_LEVEL_DEBUG:
118 priority = LOG_DEBUG;
120 case HOSTAPD_LEVEL_INFO:
123 case HOSTAPD_LEVEL_NOTICE:
124 priority = LOG_NOTICE;
126 case HOSTAPD_LEVEL_WARNING:
127 priority = LOG_WARNING;
133 syslog(priority, "%s", format);
135 #endif /* CONFIG_NATIVE_WINDOWS */
139 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
143 * hostapd_driver_init - Preparate driver interface
145 static int hostapd_driver_init(struct hostapd_iface *iface)
147 struct wpa_init_params params;
149 struct hostapd_data *hapd = iface->bss[0];
150 struct hostapd_bss_config *conf = hapd->conf;
152 struct wpa_driver_capa capa;
154 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
155 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
159 /* Initialize the driver interface */
160 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
163 os_memset(¶ms, 0, sizeof(params));
164 for (i = 0; wpa_drivers[i]; i++) {
165 if (wpa_drivers[i] != hapd->driver)
168 if (global.drv_priv[i] == NULL &&
169 wpa_drivers[i]->global_init) {
170 global.drv_priv[i] = wpa_drivers[i]->global_init();
171 if (global.drv_priv[i] == NULL) {
172 wpa_printf(MSG_ERROR, "Failed to initialize "
174 wpa_drivers[i]->name);
179 params.global_priv = global.drv_priv[i];
183 params.ifname = hapd->conf->iface;
184 params.ssid = hapd->conf->ssid.ssid;
185 params.ssid_len = hapd->conf->ssid.ssid_len;
186 params.test_socket = hapd->conf->test_socket;
187 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
189 params.num_bridge = hapd->iface->num_bss;
190 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
191 if (params.bridge == NULL)
193 for (i = 0; i < hapd->iface->num_bss; i++) {
194 struct hostapd_data *bss = hapd->iface->bss[i];
195 if (bss->conf->bridge[0])
196 params.bridge[i] = bss->conf->bridge;
199 params.own_addr = hapd->own_addr;
201 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
202 os_free(params.bridge);
203 if (hapd->drv_priv == NULL) {
204 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
210 if (hapd->driver->get_capa &&
211 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
212 iface->drv_flags = capa.flags;
213 iface->probe_resp_offloads = capa.probe_resp_offloads;
214 iface->extended_capa = capa.extended_capa;
215 iface->extended_capa_mask = capa.extended_capa_mask;
216 iface->extended_capa_len = capa.extended_capa_len;
217 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
225 * hostapd_interface_init - Read configuration file and init BSS data
227 * This function is used to parse configuration file for a full interface (one
228 * or more BSSes sharing the same radio) and allocate memory for the BSS
229 * interfaces. No actiual driver operations are started.
231 static struct hostapd_iface *
232 hostapd_interface_init(struct hapd_interfaces *interfaces,
233 const char *config_fname, int debug)
235 struct hostapd_iface *iface;
238 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
239 iface = hostapd_init(interfaces, config_fname);
242 iface->interfaces = interfaces;
244 for (k = 0; k < debug; k++) {
245 if (iface->bss[0]->conf->logger_stdout_level > 0)
246 iface->bss[0]->conf->logger_stdout_level--;
249 if (iface->conf->bss[0]->iface[0] == '\0' &&
250 !hostapd_drv_none(iface->bss[0])) {
251 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
253 hostapd_interface_deinit_free(iface);
262 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
264 static void handle_term(int sig, void *signal_ctx)
266 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
271 #ifndef CONFIG_NATIVE_WINDOWS
273 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
275 if (hostapd_reload_config(iface) < 0) {
276 wpa_printf(MSG_WARNING, "Failed to read new configuration "
277 "file - continuing with old.");
284 * handle_reload - SIGHUP handler to reload configuration
286 static void handle_reload(int sig, void *signal_ctx)
288 struct hapd_interfaces *interfaces = signal_ctx;
289 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
291 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
295 static void handle_dump_state(int sig, void *signal_ctx)
297 /* Not used anymore - ignore signal */
299 #endif /* CONFIG_NATIVE_WINDOWS */
302 static int hostapd_global_init(struct hapd_interfaces *interfaces,
303 const char *entropy_file)
307 os_memset(&global, 0, sizeof(global));
309 hostapd_logger_register_cb(hostapd_logger_cb);
311 if (eap_server_register_methods()) {
312 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
317 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
321 random_init(entropy_file);
323 #ifndef CONFIG_NATIVE_WINDOWS
324 eloop_register_signal(SIGHUP, handle_reload, interfaces);
325 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
326 #endif /* CONFIG_NATIVE_WINDOWS */
327 eloop_register_signal_terminate(handle_term, interfaces);
329 #ifndef CONFIG_NATIVE_WINDOWS
330 openlog("hostapd", 0, LOG_DAEMON);
331 #endif /* CONFIG_NATIVE_WINDOWS */
333 for (i = 0; wpa_drivers[i]; i++)
335 if (global.drv_count == 0) {
336 wpa_printf(MSG_ERROR, "No drivers enabled");
339 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
340 if (global.drv_priv == NULL)
347 static void hostapd_global_deinit(const char *pid_file)
351 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
352 if (!global.drv_priv[i])
354 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
356 os_free(global.drv_priv);
357 global.drv_priv = NULL;
359 #ifdef EAP_SERVER_TNC
360 tncs_global_deinit();
361 #endif /* EAP_SERVER_TNC */
367 #ifndef CONFIG_NATIVE_WINDOWS
369 #endif /* CONFIG_NATIVE_WINDOWS */
371 eap_server_unregister_methods();
373 os_daemonize_terminate(pid_file);
377 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
378 const char *pid_file)
380 #ifdef EAP_SERVER_TNC
384 for (i = 0; !tnc && i < ifaces->count; i++) {
385 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
386 if (ifaces->iface[i]->bss[0]->conf->tnc) {
393 if (tnc && tncs_global_init() < 0) {
394 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
397 #endif /* EAP_SERVER_TNC */
399 if (daemonize && os_daemonize(pid_file)) {
410 static void show_version(void)
413 "hostapd v" VERSION_STR "\n"
414 "User space daemon for IEEE 802.11 AP management,\n"
415 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
416 "Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi> "
417 "and contributors\n");
421 static void usage(void)
426 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
428 " [-g <global ctrl_iface>] [-G <group>] \\\n"
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 " -g global control interface path\n"
437 " -G group for control interfaces\n"
439 " -K include key data in debug messages\n"
440 #ifdef CONFIG_DEBUG_FILE
441 " -f log output to debug file instead of stdout\n"
442 #endif /* CONFIG_DEBUG_FILE */
443 #ifdef CONFIG_DEBUG_LINUX_TRACING
444 " -T = record to Linux tracing in addition to logging\n"
445 " (records all messages regardless of debug verbosity)\n"
446 #endif /* CONFIG_DEBUG_LINUX_TRACING */
447 " -t include timestamps in some debug messages\n"
448 " -v show hostapd version\n");
454 static const char * hostapd_msg_ifname_cb(void *ctx)
456 struct hostapd_data *hapd = ctx;
457 if (hapd && hapd->iconf && hapd->iconf->bss &&
458 hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
459 return hapd->iconf->bss[0]->iface;
464 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
468 os_free(interfaces->global_iface_path);
469 interfaces->global_iface_path = os_strdup(path);
470 if (interfaces->global_iface_path == NULL)
472 pos = os_strrchr(interfaces->global_iface_path, '/');
474 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
476 os_free(interfaces->global_iface_path);
477 interfaces->global_iface_path = NULL;
482 interfaces->global_iface_name = pos + 1;
488 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
491 #ifndef CONFIG_NATIVE_WINDOWS
493 grp = getgrnam(group);
495 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
498 interfaces->ctrl_iface_group = grp->gr_gid;
499 #endif /* CONFIG_NATIVE_WINDOWS */
504 int main(int argc, char *argv[])
506 struct hapd_interfaces interfaces;
509 int c, debug = 0, daemonize = 0;
510 char *pid_file = NULL;
511 const char *log_file = NULL;
512 const char *entropy_file = NULL;
513 char **bss_config = NULL, **tmp_bss;
514 size_t num_bss_configs = 0;
515 #ifdef CONFIG_DEBUG_LINUX_TRACING
516 int enable_trace_dbg = 0;
517 #endif /* CONFIG_DEBUG_LINUX_TRACING */
519 if (os_program_init())
522 os_memset(&interfaces, 0, sizeof(interfaces));
523 interfaces.reload_config = hostapd_reload_config;
524 interfaces.config_read_cb = hostapd_config_read;
525 interfaces.for_each_interface = hostapd_for_each_interface;
526 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
527 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
528 interfaces.driver_init = hostapd_driver_init;
529 interfaces.global_iface_path = NULL;
530 interfaces.global_iface_name = NULL;
531 interfaces.global_ctrl_sock = -1;
534 c = getopt(argc, argv, "b:Bde:f:hKP:Ttvg:G:");
543 if (wpa_debug_level > 0)
550 entropy_file = optarg;
556 wpa_debug_show_keys++;
560 pid_file = os_rel2abs_path(optarg);
563 wpa_debug_timestamp++;
565 #ifdef CONFIG_DEBUG_LINUX_TRACING
567 enable_trace_dbg = 1;
569 #endif /* CONFIG_DEBUG_LINUX_TRACING */
575 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
579 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
583 tmp_bss = os_realloc_array(bss_config,
588 bss_config = tmp_bss;
589 bss_config[num_bss_configs++] = optarg;
597 if (optind == argc && interfaces.global_iface_path == NULL &&
598 num_bss_configs == 0)
601 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
604 wpa_debug_open_file(log_file);
605 #ifdef CONFIG_DEBUG_LINUX_TRACING
606 if (enable_trace_dbg) {
607 int tret = wpa_debug_open_linux_tracing();
609 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
613 #endif /* CONFIG_DEBUG_LINUX_TRACING */
615 interfaces.count = argc - optind;
616 if (interfaces.count || num_bss_configs) {
617 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
618 sizeof(struct hostapd_iface *));
619 if (interfaces.iface == NULL) {
620 wpa_printf(MSG_ERROR, "malloc failed");
625 if (hostapd_global_init(&interfaces, entropy_file)) {
626 wpa_printf(MSG_ERROR, "Failed to initilize global context");
630 /* Allocate and parse configuration for full interface files */
631 for (i = 0; i < interfaces.count; i++) {
632 interfaces.iface[i] = hostapd_interface_init(&interfaces,
635 if (!interfaces.iface[i]) {
636 wpa_printf(MSG_ERROR, "Failed to initialize interface");
641 /* Allocate and parse configuration for per-BSS files */
642 for (i = 0; i < num_bss_configs; i++) {
643 struct hostapd_iface *iface;
646 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
647 fname = os_strchr(bss_config[i], ':');
649 wpa_printf(MSG_ERROR,
650 "Invalid BSS config identifier '%s'",
655 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
659 for (j = 0; j < interfaces.count; j++) {
660 if (interfaces.iface[j] == iface)
663 if (j == interfaces.count) {
664 struct hostapd_iface **tmp;
665 tmp = os_realloc_array(interfaces.iface,
666 interfaces.count + 1,
667 sizeof(struct hostapd_iface *));
669 hostapd_interface_deinit_free(iface);
672 interfaces.iface = tmp;
673 interfaces.iface[interfaces.count++] = iface;
678 * Enable configured interfaces. Depending on channel configuration,
679 * this may complete full initialization before returning or use a
680 * callback mechanism to complete setup in case of operations like HT
681 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
682 * In such case, the interface will be enabled from eloop context within
683 * hostapd_global_run().
685 interfaces.terminate_on_error = interfaces.count;
686 for (i = 0; i < interfaces.count; i++) {
687 if (hostapd_driver_init(interfaces.iface[i]) ||
688 hostapd_setup_interface(interfaces.iface[i]))
692 hostapd_global_ctrl_iface_init(&interfaces);
694 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
695 wpa_printf(MSG_ERROR, "Failed to start eloop");
702 hostapd_global_ctrl_iface_deinit(&interfaces);
703 /* Deinitialize all interfaces */
704 for (i = 0; i < interfaces.count; i++)
705 hostapd_interface_deinit_free(interfaces.iface[i]);
706 os_free(interfaces.iface);
708 hostapd_global_deinit(pid_file);
712 wpa_debug_close_file();
713 wpa_debug_close_linux_tracing();