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 "utils/uuid.h"
18 #include "crypto/random.h"
19 #include "crypto/tls.h"
20 #include "common/version.h"
21 #include "drivers/driver.h"
22 #include "eap_server/eap.h"
23 #include "eap_server/tncs.h"
24 #include "ap/hostapd.h"
25 #include "ap/ap_config.h"
26 #include "ap/ap_drv_ops.h"
27 #include "config_file.h"
28 #include "eap_register.h"
29 #include "ctrl_iface.h"
37 static struct hapd_global global;
40 #ifndef CONFIG_NO_HOSTAPD_LOGGER
41 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
42 int level, const char *txt, size_t len)
44 struct hostapd_data *hapd = ctx;
45 char *format, *module_str;
47 int conf_syslog_level, conf_stdout_level;
48 unsigned int conf_syslog, conf_stdout;
51 format = os_malloc(maxlen);
55 if (hapd && hapd->conf) {
56 conf_syslog_level = hapd->conf->logger_syslog_level;
57 conf_stdout_level = hapd->conf->logger_stdout_level;
58 conf_syslog = hapd->conf->logger_syslog;
59 conf_stdout = hapd->conf->logger_stdout;
61 conf_syslog_level = conf_stdout_level = 0;
62 conf_syslog = conf_stdout = (unsigned int) -1;
66 case HOSTAPD_MODULE_IEEE80211:
67 module_str = "IEEE 802.11";
69 case HOSTAPD_MODULE_IEEE8021X:
70 module_str = "IEEE 802.1X";
72 case HOSTAPD_MODULE_RADIUS:
73 module_str = "RADIUS";
75 case HOSTAPD_MODULE_WPA:
78 case HOSTAPD_MODULE_DRIVER:
79 module_str = "DRIVER";
81 case HOSTAPD_MODULE_IAPP:
84 case HOSTAPD_MODULE_MLME:
92 if (hapd && hapd->conf && addr)
93 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
94 hapd->conf->iface, MAC2STR(addr),
95 module_str ? " " : "", module_str ? module_str : "",
97 else if (hapd && hapd->conf)
98 os_snprintf(format, maxlen, "%s:%s%s %s",
99 hapd->conf->iface, module_str ? " " : "",
102 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
103 MAC2STR(addr), module_str ? " " : "",
106 os_snprintf(format, maxlen, "%s%s%s",
107 module_str, module_str ? ": " : "", txt);
109 if ((conf_stdout & module) && level >= conf_stdout_level) {
110 wpa_debug_print_timestamp();
111 wpa_printf(MSG_INFO, "%s", format);
114 #ifndef CONFIG_NATIVE_WINDOWS
115 if ((conf_syslog & module) && level >= conf_syslog_level) {
118 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
119 case HOSTAPD_LEVEL_DEBUG:
120 priority = LOG_DEBUG;
122 case HOSTAPD_LEVEL_INFO:
125 case HOSTAPD_LEVEL_NOTICE:
126 priority = LOG_NOTICE;
128 case HOSTAPD_LEVEL_WARNING:
129 priority = LOG_WARNING;
135 syslog(priority, "%s", format);
137 #endif /* CONFIG_NATIVE_WINDOWS */
141 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
145 * hostapd_driver_init - Preparate driver interface
147 static int hostapd_driver_init(struct hostapd_iface *iface)
149 struct wpa_init_params params;
151 struct hostapd_data *hapd = iface->bss[0];
152 struct hostapd_bss_config *conf = hapd->conf;
154 struct wpa_driver_capa capa;
156 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
157 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
161 /* Initialize the driver interface */
162 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
165 os_memset(¶ms, 0, sizeof(params));
166 for (i = 0; wpa_drivers[i]; i++) {
167 if (wpa_drivers[i] != hapd->driver)
170 if (global.drv_priv[i] == NULL &&
171 wpa_drivers[i]->global_init) {
172 global.drv_priv[i] = wpa_drivers[i]->global_init();
173 if (global.drv_priv[i] == NULL) {
174 wpa_printf(MSG_ERROR, "Failed to initialize "
176 wpa_drivers[i]->name);
181 params.global_priv = global.drv_priv[i];
185 params.ifname = hapd->conf->iface;
186 params.ssid = hapd->conf->ssid.ssid;
187 params.ssid_len = hapd->conf->ssid.ssid_len;
188 params.test_socket = hapd->conf->test_socket;
189 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
191 params.num_bridge = hapd->iface->num_bss;
192 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
193 if (params.bridge == NULL)
195 for (i = 0; i < hapd->iface->num_bss; i++) {
196 struct hostapd_data *bss = hapd->iface->bss[i];
197 if (bss->conf->bridge[0])
198 params.bridge[i] = bss->conf->bridge;
201 params.own_addr = hapd->own_addr;
203 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
204 os_free(params.bridge);
205 if (hapd->drv_priv == NULL) {
206 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
212 if (hapd->driver->get_capa &&
213 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
214 iface->drv_flags = capa.flags;
215 iface->probe_resp_offloads = capa.probe_resp_offloads;
216 iface->extended_capa = capa.extended_capa;
217 iface->extended_capa_mask = capa.extended_capa_mask;
218 iface->extended_capa_len = capa.extended_capa_len;
219 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
227 * hostapd_interface_init - Read configuration file and init BSS data
229 * This function is used to parse configuration file for a full interface (one
230 * or more BSSes sharing the same radio) and allocate memory for the BSS
231 * interfaces. No actiual driver operations are started.
233 static struct hostapd_iface *
234 hostapd_interface_init(struct hapd_interfaces *interfaces,
235 const char *config_fname, int debug)
237 struct hostapd_iface *iface;
240 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
241 iface = hostapd_init(interfaces, config_fname);
244 iface->interfaces = interfaces;
246 for (k = 0; k < debug; k++) {
247 if (iface->bss[0]->conf->logger_stdout_level > 0)
248 iface->bss[0]->conf->logger_stdout_level--;
251 if (iface->conf->bss[0]->iface[0] == '\0' &&
252 !hostapd_drv_none(iface->bss[0])) {
253 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
255 hostapd_interface_deinit_free(iface);
264 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
266 static void handle_term(int sig, void *signal_ctx)
268 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
273 #ifndef CONFIG_NATIVE_WINDOWS
275 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
277 if (hostapd_reload_config(iface) < 0) {
278 wpa_printf(MSG_WARNING, "Failed to read new configuration "
279 "file - continuing with old.");
286 * handle_reload - SIGHUP handler to reload configuration
288 static void handle_reload(int sig, void *signal_ctx)
290 struct hapd_interfaces *interfaces = signal_ctx;
291 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
293 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
297 static void handle_dump_state(int sig, void *signal_ctx)
299 /* Not used anymore - ignore signal */
301 #endif /* CONFIG_NATIVE_WINDOWS */
304 static int hostapd_global_init(struct hapd_interfaces *interfaces,
305 const char *entropy_file)
309 os_memset(&global, 0, sizeof(global));
311 hostapd_logger_register_cb(hostapd_logger_cb);
313 if (eap_server_register_methods()) {
314 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
319 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
323 random_init(entropy_file);
325 #ifndef CONFIG_NATIVE_WINDOWS
326 eloop_register_signal(SIGHUP, handle_reload, interfaces);
327 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
328 #endif /* CONFIG_NATIVE_WINDOWS */
329 eloop_register_signal_terminate(handle_term, interfaces);
331 #ifndef CONFIG_NATIVE_WINDOWS
332 openlog("hostapd", 0, LOG_DAEMON);
333 #endif /* CONFIG_NATIVE_WINDOWS */
335 for (i = 0; wpa_drivers[i]; i++)
337 if (global.drv_count == 0) {
338 wpa_printf(MSG_ERROR, "No drivers enabled");
341 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
342 if (global.drv_priv == NULL)
349 static void hostapd_global_deinit(const char *pid_file)
353 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
354 if (!global.drv_priv[i])
356 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
358 os_free(global.drv_priv);
359 global.drv_priv = NULL;
361 #ifdef EAP_SERVER_TNC
362 tncs_global_deinit();
363 #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-2014, Jouni Malinen <j@w1.fi> "
419 "and contributors\n");
423 static void usage(void)
428 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
430 " [-g <global ctrl_iface>] [-G <group>] \\\n"
431 " <configuration file(s)>\n"
434 " -h show this usage\n"
435 " -d show more debug messages (-dd for even more)\n"
436 " -B run daemon in the background\n"
438 " -g global control interface path\n"
439 " -G group for control interfaces\n"
441 " -K include key data in debug messages\n"
442 #ifdef CONFIG_DEBUG_FILE
443 " -f log output to debug file instead of stdout\n"
444 #endif /* CONFIG_DEBUG_FILE */
445 #ifdef CONFIG_DEBUG_LINUX_TRACING
446 " -T = record to Linux tracing in addition to logging\n"
447 " (records all messages regardless of debug verbosity)\n"
448 #endif /* CONFIG_DEBUG_LINUX_TRACING */
449 " -t include timestamps in some debug messages\n"
450 " -v show hostapd version\n");
456 static const char * hostapd_msg_ifname_cb(void *ctx)
458 struct hostapd_data *hapd = ctx;
459 if (hapd && hapd->iconf && hapd->iconf->bss &&
460 hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
461 return hapd->iconf->bss[0]->iface;
466 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
470 os_free(interfaces->global_iface_path);
471 interfaces->global_iface_path = os_strdup(path);
472 if (interfaces->global_iface_path == NULL)
474 pos = os_strrchr(interfaces->global_iface_path, '/');
476 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
478 os_free(interfaces->global_iface_path);
479 interfaces->global_iface_path = NULL;
484 interfaces->global_iface_name = pos + 1;
490 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
493 #ifndef CONFIG_NATIVE_WINDOWS
495 grp = getgrnam(group);
497 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
500 interfaces->ctrl_iface_group = grp->gr_gid;
501 #endif /* CONFIG_NATIVE_WINDOWS */
507 static int gen_uuid(const char *txt_addr)
513 if (hwaddr_aton(txt_addr, addr) < 0)
516 uuid_gen_mac_addr(addr, uuid);
517 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
524 #endif /* CONFIG_WPS */
527 int main(int argc, char *argv[])
529 struct hapd_interfaces interfaces;
532 int c, debug = 0, daemonize = 0;
533 char *pid_file = NULL;
534 const char *log_file = NULL;
535 const char *entropy_file = NULL;
536 char **bss_config = NULL, **tmp_bss;
537 size_t num_bss_configs = 0;
538 #ifdef CONFIG_DEBUG_LINUX_TRACING
539 int enable_trace_dbg = 0;
540 #endif /* CONFIG_DEBUG_LINUX_TRACING */
542 if (os_program_init())
545 os_memset(&interfaces, 0, sizeof(interfaces));
546 interfaces.reload_config = hostapd_reload_config;
547 interfaces.config_read_cb = hostapd_config_read;
548 interfaces.for_each_interface = hostapd_for_each_interface;
549 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
550 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
551 interfaces.driver_init = hostapd_driver_init;
552 interfaces.global_iface_path = NULL;
553 interfaces.global_iface_name = NULL;
554 interfaces.global_ctrl_sock = -1;
557 c = getopt(argc, argv, "b:Bde:f:hKP:Ttu:vg:G:");
566 if (wpa_debug_level > 0)
573 entropy_file = optarg;
579 wpa_debug_show_keys++;
583 pid_file = os_rel2abs_path(optarg);
586 wpa_debug_timestamp++;
588 #ifdef CONFIG_DEBUG_LINUX_TRACING
590 enable_trace_dbg = 1;
592 #endif /* CONFIG_DEBUG_LINUX_TRACING */
598 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
602 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
606 tmp_bss = os_realloc_array(bss_config,
611 bss_config = tmp_bss;
612 bss_config[num_bss_configs++] = optarg;
616 return gen_uuid(optarg);
617 #endif /* CONFIG_WPS */
624 if (optind == argc && interfaces.global_iface_path == NULL &&
625 num_bss_configs == 0)
628 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
631 wpa_debug_open_file(log_file);
632 #ifdef CONFIG_DEBUG_LINUX_TRACING
633 if (enable_trace_dbg) {
634 int tret = wpa_debug_open_linux_tracing();
636 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
640 #endif /* CONFIG_DEBUG_LINUX_TRACING */
642 interfaces.count = argc - optind;
643 if (interfaces.count || num_bss_configs) {
644 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
645 sizeof(struct hostapd_iface *));
646 if (interfaces.iface == NULL) {
647 wpa_printf(MSG_ERROR, "malloc failed");
652 if (hostapd_global_init(&interfaces, entropy_file)) {
653 wpa_printf(MSG_ERROR, "Failed to initilize global context");
657 /* Allocate and parse configuration for full interface files */
658 for (i = 0; i < interfaces.count; i++) {
659 interfaces.iface[i] = hostapd_interface_init(&interfaces,
662 if (!interfaces.iface[i]) {
663 wpa_printf(MSG_ERROR, "Failed to initialize interface");
668 /* Allocate and parse configuration for per-BSS files */
669 for (i = 0; i < num_bss_configs; i++) {
670 struct hostapd_iface *iface;
673 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
674 fname = os_strchr(bss_config[i], ':');
676 wpa_printf(MSG_ERROR,
677 "Invalid BSS config identifier '%s'",
682 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
686 for (j = 0; j < interfaces.count; j++) {
687 if (interfaces.iface[j] == iface)
690 if (j == interfaces.count) {
691 struct hostapd_iface **tmp;
692 tmp = os_realloc_array(interfaces.iface,
693 interfaces.count + 1,
694 sizeof(struct hostapd_iface *));
696 hostapd_interface_deinit_free(iface);
699 interfaces.iface = tmp;
700 interfaces.iface[interfaces.count++] = iface;
705 * Enable configured interfaces. Depending on channel configuration,
706 * this may complete full initialization before returning or use a
707 * callback mechanism to complete setup in case of operations like HT
708 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
709 * In such case, the interface will be enabled from eloop context within
710 * hostapd_global_run().
712 interfaces.terminate_on_error = interfaces.count;
713 for (i = 0; i < interfaces.count; i++) {
714 if (hostapd_driver_init(interfaces.iface[i]) ||
715 hostapd_setup_interface(interfaces.iface[i]))
719 hostapd_global_ctrl_iface_init(&interfaces);
721 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
722 wpa_printf(MSG_ERROR, "Failed to start eloop");
729 hostapd_global_ctrl_iface_deinit(&interfaces);
730 /* Deinitialize all interfaces */
731 for (i = 0; i < interfaces.count; i++) {
732 if (!interfaces.iface[i])
734 interfaces.iface[i]->driver_ap_teardown =
735 !!(interfaces.iface[i]->drv_flags &
736 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
737 hostapd_interface_deinit_free(interfaces.iface[i]);
739 os_free(interfaces.iface);
741 hostapd_global_deinit(pid_file);
745 wpa_debug_close_file();
746 wpa_debug_close_linux_tracing();