3 * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
16 #ifndef CONFIG_NATIVE_WINDOWS
18 #endif /* CONFIG_NATIVE_WINDOWS */
20 #include "utils/common.h"
21 #include "utils/eloop.h"
22 #include "crypto/random.h"
23 #include "crypto/tls.h"
24 #include "common/version.h"
25 #include "drivers/driver.h"
26 #include "eap_server/eap.h"
27 #include "eap_server/tncs.h"
28 #include "ap/hostapd.h"
29 #include "ap/ap_config.h"
30 #include "config_file.h"
31 #include "eap_register.h"
32 #include "dump_state.h"
33 #include "ctrl_iface.h"
36 extern int wpa_debug_level
;
37 extern int wpa_debug_show_keys
;
38 extern int wpa_debug_timestamp
;
40 extern struct wpa_driver_ops
*wpa_drivers
[];
48 static struct hapd_global global
;
51 struct hapd_interfaces
{
53 struct hostapd_iface
**iface
;
57 static int hostapd_for_each_interface(struct hapd_interfaces
*interfaces
,
58 int (*cb
)(struct hostapd_iface
*iface
,
59 void *ctx
), void *ctx
)
64 for (i
= 0; i
< interfaces
->count
; i
++) {
65 ret
= cb(interfaces
->iface
[i
], ctx
);
74 #ifndef CONFIG_NO_HOSTAPD_LOGGER
75 static void hostapd_logger_cb(void *ctx
, const u8
*addr
, unsigned int module
,
76 int level
, const char *txt
, size_t len
)
78 struct hostapd_data
*hapd
= ctx
;
79 char *format
, *module_str
;
81 int conf_syslog_level
, conf_stdout_level
;
82 unsigned int conf_syslog
, conf_stdout
;
85 format
= os_malloc(maxlen
);
89 if (hapd
&& hapd
->conf
) {
90 conf_syslog_level
= hapd
->conf
->logger_syslog_level
;
91 conf_stdout_level
= hapd
->conf
->logger_stdout_level
;
92 conf_syslog
= hapd
->conf
->logger_syslog
;
93 conf_stdout
= hapd
->conf
->logger_stdout
;
95 conf_syslog_level
= conf_stdout_level
= 0;
96 conf_syslog
= conf_stdout
= (unsigned int) -1;
100 case HOSTAPD_MODULE_IEEE80211
:
101 module_str
= "IEEE 802.11";
103 case HOSTAPD_MODULE_IEEE8021X
:
104 module_str
= "IEEE 802.1X";
106 case HOSTAPD_MODULE_RADIUS
:
107 module_str
= "RADIUS";
109 case HOSTAPD_MODULE_WPA
:
112 case HOSTAPD_MODULE_DRIVER
:
113 module_str
= "DRIVER";
115 case HOSTAPD_MODULE_IAPP
:
118 case HOSTAPD_MODULE_MLME
:
126 if (hapd
&& hapd
->conf
&& addr
)
127 os_snprintf(format
, maxlen
, "%s: STA " MACSTR
"%s%s: %s",
128 hapd
->conf
->iface
, MAC2STR(addr
),
129 module_str
? " " : "", module_str
, txt
);
130 else if (hapd
&& hapd
->conf
)
131 os_snprintf(format
, maxlen
, "%s:%s%s %s",
132 hapd
->conf
->iface
, module_str
? " " : "",
135 os_snprintf(format
, maxlen
, "STA " MACSTR
"%s%s: %s",
136 MAC2STR(addr
), module_str
? " " : "",
139 os_snprintf(format
, maxlen
, "%s%s%s",
140 module_str
, module_str
? ": " : "", txt
);
142 if ((conf_stdout
& module
) && level
>= conf_stdout_level
) {
143 wpa_debug_print_timestamp();
144 printf("%s\n", format
);
147 #ifndef CONFIG_NATIVE_WINDOWS
148 if ((conf_syslog
& module
) && level
>= conf_syslog_level
) {
151 case HOSTAPD_LEVEL_DEBUG_VERBOSE
:
152 case HOSTAPD_LEVEL_DEBUG
:
153 priority
= LOG_DEBUG
;
155 case HOSTAPD_LEVEL_INFO
:
158 case HOSTAPD_LEVEL_NOTICE
:
159 priority
= LOG_NOTICE
;
161 case HOSTAPD_LEVEL_WARNING
:
162 priority
= LOG_WARNING
;
168 syslog(priority
, "%s", format
);
170 #endif /* CONFIG_NATIVE_WINDOWS */
174 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
178 * hostapd_init - Allocate and initialize per-interface data
179 * @config_file: Path to the configuration file
180 * Returns: Pointer to the allocated interface data or %NULL on failure
182 * This function is used to allocate main data structures for per-interface
183 * data. The allocated data buffer will be freed by calling
184 * hostapd_cleanup_iface().
186 static struct hostapd_iface
* hostapd_init(const char *config_file
)
188 struct hostapd_iface
*hapd_iface
= NULL
;
189 struct hostapd_config
*conf
= NULL
;
190 struct hostapd_data
*hapd
;
193 hapd_iface
= os_zalloc(sizeof(*hapd_iface
));
194 if (hapd_iface
== NULL
)
197 hapd_iface
->reload_config
= hostapd_reload_config
;
198 hapd_iface
->config_read_cb
= hostapd_config_read
;
199 hapd_iface
->config_fname
= os_strdup(config_file
);
200 if (hapd_iface
->config_fname
== NULL
)
202 hapd_iface
->ctrl_iface_init
= hostapd_ctrl_iface_init
;
203 hapd_iface
->ctrl_iface_deinit
= hostapd_ctrl_iface_deinit
;
204 hapd_iface
->for_each_interface
= hostapd_for_each_interface
;
206 conf
= hostapd_config_read(hapd_iface
->config_fname
);
209 hapd_iface
->conf
= conf
;
211 hapd_iface
->num_bss
= conf
->num_bss
;
212 hapd_iface
->bss
= os_zalloc(conf
->num_bss
*
213 sizeof(struct hostapd_data
*));
214 if (hapd_iface
->bss
== NULL
)
217 for (i
= 0; i
< conf
->num_bss
; i
++) {
218 hapd
= hapd_iface
->bss
[i
] =
219 hostapd_alloc_bss_data(hapd_iface
, conf
,
223 hapd
->msg_ctx
= hapd
;
230 hostapd_config_free(conf
);
232 os_free(hapd_iface
->config_fname
);
233 os_free(hapd_iface
->bss
);
240 static int hostapd_driver_init(struct hostapd_iface
*iface
)
242 struct wpa_init_params params
;
244 struct hostapd_data
*hapd
= iface
->bss
[0];
245 struct hostapd_bss_config
*conf
= hapd
->conf
;
247 struct wpa_driver_capa capa
;
249 if (hapd
->driver
== NULL
|| hapd
->driver
->hapd_init
== NULL
) {
250 wpa_printf(MSG_ERROR
, "No hostapd driver wrapper available");
254 /* Initialize the driver interface */
255 if (!(b
[0] | b
[1] | b
[2] | b
[3] | b
[4] | b
[5]))
258 os_memset(¶ms
, 0, sizeof(params
));
259 for (i
= 0; wpa_drivers
[i
]; i
++) {
260 if (wpa_drivers
[i
] != hapd
->driver
)
263 if (global
.drv_priv
[i
] == NULL
&&
264 wpa_drivers
[i
]->global_init
) {
265 global
.drv_priv
[i
] = wpa_drivers
[i
]->global_init();
266 if (global
.drv_priv
[i
] == NULL
) {
267 wpa_printf(MSG_ERROR
, "Failed to initialize "
269 wpa_drivers
[i
]->name
);
274 params
.global_priv
= global
.drv_priv
[i
];
278 params
.ifname
= hapd
->conf
->iface
;
279 params
.ssid
= (const u8
*) hapd
->conf
->ssid
.ssid
;
280 params
.ssid_len
= hapd
->conf
->ssid
.ssid_len
;
281 params
.test_socket
= hapd
->conf
->test_socket
;
282 params
.use_pae_group_addr
= hapd
->conf
->use_pae_group_addr
;
284 params
.num_bridge
= hapd
->iface
->num_bss
;
285 params
.bridge
= os_zalloc(hapd
->iface
->num_bss
* sizeof(char *));
286 if (params
.bridge
== NULL
)
288 for (i
= 0; i
< hapd
->iface
->num_bss
; i
++) {
289 struct hostapd_data
*bss
= hapd
->iface
->bss
[i
];
290 if (bss
->conf
->bridge
[0])
291 params
.bridge
[i
] = bss
->conf
->bridge
;
294 params
.own_addr
= hapd
->own_addr
;
296 hapd
->drv_priv
= hapd
->driver
->hapd_init(hapd
, ¶ms
);
297 os_free(params
.bridge
);
298 if (hapd
->drv_priv
== NULL
) {
299 wpa_printf(MSG_ERROR
, "%s driver initialization failed.",
305 if (hapd
->driver
->get_capa
&&
306 hapd
->driver
->get_capa(hapd
->drv_priv
, &capa
) == 0) {
307 iface
->drv_flags
= capa
.flags
;
308 iface
->probe_resp_offloads
= capa
.probe_resp_offloads
;
315 static void hostapd_interface_deinit_free(struct hostapd_iface
*iface
)
317 const struct wpa_driver_ops
*driver
;
321 driver
= iface
->bss
[0]->driver
;
322 drv_priv
= iface
->bss
[0]->drv_priv
;
323 hostapd_interface_deinit(iface
);
324 if (driver
&& driver
->hapd_deinit
)
325 driver
->hapd_deinit(drv_priv
);
326 hostapd_interface_free(iface
);
330 static struct hostapd_iface
*
331 hostapd_interface_init(struct hapd_interfaces
*interfaces
,
332 const char *config_fname
, int debug
)
334 struct hostapd_iface
*iface
;
337 wpa_printf(MSG_ERROR
, "Configuration file: %s", config_fname
);
338 iface
= hostapd_init(config_fname
);
341 iface
->interfaces
= interfaces
;
343 for (k
= 0; k
< debug
; k
++) {
344 if (iface
->bss
[0]->conf
->logger_stdout_level
> 0)
345 iface
->bss
[0]->conf
->logger_stdout_level
--;
348 if (hostapd_driver_init(iface
) ||
349 hostapd_setup_interface(iface
)) {
350 hostapd_interface_deinit_free(iface
);
359 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
361 static void handle_term(int sig
, void *signal_ctx
)
363 wpa_printf(MSG_DEBUG
, "Signal %d received - terminating", sig
);
368 #ifndef CONFIG_NATIVE_WINDOWS
370 static int handle_reload_iface(struct hostapd_iface
*iface
, void *ctx
)
372 if (hostapd_reload_config(iface
) < 0) {
373 wpa_printf(MSG_WARNING
, "Failed to read new configuration "
374 "file - continuing with old.");
381 * handle_reload - SIGHUP handler to reload configuration
383 static void handle_reload(int sig
, void *signal_ctx
)
385 struct hapd_interfaces
*interfaces
= signal_ctx
;
386 wpa_printf(MSG_DEBUG
, "Signal %d received - reloading configuration",
388 hostapd_for_each_interface(interfaces
, handle_reload_iface
, NULL
);
392 static void handle_dump_state(int sig
, void *signal_ctx
)
394 #ifdef HOSTAPD_DUMP_STATE
395 struct hapd_interfaces
*interfaces
= signal_ctx
;
396 hostapd_for_each_interface(interfaces
, handle_dump_state_iface
, NULL
);
397 #endif /* HOSTAPD_DUMP_STATE */
399 #endif /* CONFIG_NATIVE_WINDOWS */
402 static int hostapd_global_init(struct hapd_interfaces
*interfaces
,
403 const char *entropy_file
)
407 os_memset(&global
, 0, sizeof(global
));
409 hostapd_logger_register_cb(hostapd_logger_cb
);
411 if (eap_server_register_methods()) {
412 wpa_printf(MSG_ERROR
, "Failed to register EAP methods");
417 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
421 random_init(entropy_file
);
423 #ifndef CONFIG_NATIVE_WINDOWS
424 eloop_register_signal(SIGHUP
, handle_reload
, interfaces
);
425 eloop_register_signal(SIGUSR1
, handle_dump_state
, interfaces
);
426 #endif /* CONFIG_NATIVE_WINDOWS */
427 eloop_register_signal_terminate(handle_term
, interfaces
);
429 #ifndef CONFIG_NATIVE_WINDOWS
430 openlog("hostapd", 0, LOG_DAEMON
);
431 #endif /* CONFIG_NATIVE_WINDOWS */
433 for (i
= 0; wpa_drivers
[i
]; i
++)
435 if (global
.drv_count
== 0) {
436 wpa_printf(MSG_ERROR
, "No drivers enabled");
439 global
.drv_priv
= os_zalloc(global
.drv_count
* sizeof(void *));
440 if (global
.drv_priv
== NULL
)
447 static void hostapd_global_deinit(const char *pid_file
)
451 for (i
= 0; wpa_drivers
[i
] && global
.drv_priv
; i
++) {
452 if (!global
.drv_priv
[i
])
454 wpa_drivers
[i
]->global_deinit(global
.drv_priv
[i
]);
456 os_free(global
.drv_priv
);
457 global
.drv_priv
= NULL
;
459 #ifdef EAP_SERVER_TNC
460 tncs_global_deinit();
461 #endif /* EAP_SERVER_TNC */
467 #ifndef CONFIG_NATIVE_WINDOWS
469 #endif /* CONFIG_NATIVE_WINDOWS */
471 eap_server_unregister_methods();
473 os_daemonize_terminate(pid_file
);
477 static int hostapd_global_run(struct hapd_interfaces
*ifaces
, int daemonize
,
478 const char *pid_file
)
480 #ifdef EAP_SERVER_TNC
484 for (i
= 0; !tnc
&& i
< ifaces
->count
; i
++) {
485 for (k
= 0; k
< ifaces
->iface
[i
]->num_bss
; k
++) {
486 if (ifaces
->iface
[i
]->bss
[0]->conf
->tnc
) {
493 if (tnc
&& tncs_global_init() < 0) {
494 wpa_printf(MSG_ERROR
, "Failed to initialize TNCS");
497 #endif /* EAP_SERVER_TNC */
499 if (daemonize
&& os_daemonize(pid_file
)) {
510 static void show_version(void)
513 "hostapd v" VERSION_STR
"\n"
514 "User space daemon for IEEE 802.11 AP management,\n"
515 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
516 "Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi> "
517 "and contributors\n");
521 static void usage(void)
526 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
527 "<configuration file(s)>\n"
530 " -h show this usage\n"
531 " -d show more debug messages (-dd for even more)\n"
532 " -B run daemon in the background\n"
535 " -K include key data in debug messages\n"
536 #ifdef CONFIG_DEBUG_FILE
537 " -f log output to debug file instead of stdout\n"
538 #endif /* CONFIG_DEBUG_FILE */
539 " -t include timestamps in some debug messages\n"
540 " -v show hostapd version\n");
546 static const char * hostapd_msg_ifname_cb(void *ctx
)
548 struct hostapd_data
*hapd
= ctx
;
549 if (hapd
&& hapd
->iconf
&& hapd
->iconf
->bss
)
550 return hapd
->iconf
->bss
->iface
;
555 int main(int argc
, char *argv
[])
557 struct hapd_interfaces interfaces
;
560 int c
, debug
= 0, daemonize
= 0;
561 char *pid_file
= NULL
;
562 const char *log_file
= NULL
;
563 const char *entropy_file
= NULL
;
565 if (os_program_init())
569 c
= getopt(argc
, argv
, "Bde:f:hKP:tv");
578 if (wpa_debug_level
> 0)
585 entropy_file
= optarg
;
591 wpa_debug_show_keys
++;
595 pid_file
= os_rel2abs_path(optarg
);
598 wpa_debug_timestamp
++;
614 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb
);
617 wpa_debug_open_file(log_file
);
619 interfaces
.count
= argc
- optind
;
620 interfaces
.iface
= os_zalloc(interfaces
.count
*
621 sizeof(struct hostapd_iface
*));
622 if (interfaces
.iface
== NULL
) {
623 wpa_printf(MSG_ERROR
, "malloc failed");
627 if (hostapd_global_init(&interfaces
, entropy_file
))
630 /* Initialize interfaces */
631 for (i
= 0; i
< interfaces
.count
; i
++) {
632 interfaces
.iface
[i
] = hostapd_interface_init(&interfaces
,
635 if (!interfaces
.iface
[i
])
639 if (hostapd_global_run(&interfaces
, daemonize
, pid_file
))
645 /* Deinitialize all interfaces */
646 for (i
= 0; i
< interfaces
.count
; i
++)
647 hostapd_interface_deinit_free(interfaces
.iface
[i
]);
648 os_free(interfaces
.iface
);
650 hostapd_global_deinit(pid_file
);
654 wpa_debug_close_file();