3 * Copyright (c) 2002-2009, 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/tls.h"
23 #include "common/version.h"
24 #include "drivers/driver.h"
25 #include "eap_server/eap.h"
26 #include "eap_server/tncs.h"
27 #include "ap/hostapd.h"
28 #include "ap/ap_config.h"
29 #include "config_file.h"
30 #include "eap_register.h"
31 #include "dump_state.h"
32 #include "ctrl_iface.h"
35 extern int wpa_debug_level
;
36 extern int wpa_debug_show_keys
;
37 extern int wpa_debug_timestamp
;
40 struct hapd_interfaces
{
42 struct hostapd_iface
**iface
;
46 static int hostapd_for_each_interface(struct hapd_interfaces
*interfaces
,
47 int (*cb
)(struct hostapd_iface
*iface
,
48 void *ctx
), void *ctx
)
53 for (i
= 0; i
< interfaces
->count
; i
++) {
54 ret
= cb(interfaces
->iface
[i
], ctx
);
63 #ifndef CONFIG_NO_HOSTAPD_LOGGER
64 static void hostapd_logger_cb(void *ctx
, const u8
*addr
, unsigned int module
,
65 int level
, const char *txt
, size_t len
)
67 struct hostapd_data
*hapd
= ctx
;
68 char *format
, *module_str
;
70 int conf_syslog_level
, conf_stdout_level
;
71 unsigned int conf_syslog
, conf_stdout
;
74 format
= os_malloc(maxlen
);
78 if (hapd
&& hapd
->conf
) {
79 conf_syslog_level
= hapd
->conf
->logger_syslog_level
;
80 conf_stdout_level
= hapd
->conf
->logger_stdout_level
;
81 conf_syslog
= hapd
->conf
->logger_syslog
;
82 conf_stdout
= hapd
->conf
->logger_stdout
;
84 conf_syslog_level
= conf_stdout_level
= 0;
85 conf_syslog
= conf_stdout
= (unsigned int) -1;
89 case HOSTAPD_MODULE_IEEE80211
:
90 module_str
= "IEEE 802.11";
92 case HOSTAPD_MODULE_IEEE8021X
:
93 module_str
= "IEEE 802.1X";
95 case HOSTAPD_MODULE_RADIUS
:
96 module_str
= "RADIUS";
98 case HOSTAPD_MODULE_WPA
:
101 case HOSTAPD_MODULE_DRIVER
:
102 module_str
= "DRIVER";
104 case HOSTAPD_MODULE_IAPP
:
107 case HOSTAPD_MODULE_MLME
:
115 if (hapd
&& hapd
->conf
&& addr
)
116 os_snprintf(format
, maxlen
, "%s: STA " MACSTR
"%s%s: %s",
117 hapd
->conf
->iface
, MAC2STR(addr
),
118 module_str
? " " : "", module_str
, txt
);
119 else if (hapd
&& hapd
->conf
)
120 os_snprintf(format
, maxlen
, "%s:%s%s %s",
121 hapd
->conf
->iface
, module_str
? " " : "",
124 os_snprintf(format
, maxlen
, "STA " MACSTR
"%s%s: %s",
125 MAC2STR(addr
), module_str
? " " : "",
128 os_snprintf(format
, maxlen
, "%s%s%s",
129 module_str
, module_str
? ": " : "", txt
);
131 if ((conf_stdout
& module
) && level
>= conf_stdout_level
) {
132 wpa_debug_print_timestamp();
133 printf("%s\n", format
);
136 #ifndef CONFIG_NATIVE_WINDOWS
137 if ((conf_syslog
& module
) && level
>= conf_syslog_level
) {
140 case HOSTAPD_LEVEL_DEBUG_VERBOSE
:
141 case HOSTAPD_LEVEL_DEBUG
:
142 priority
= LOG_DEBUG
;
144 case HOSTAPD_LEVEL_INFO
:
147 case HOSTAPD_LEVEL_NOTICE
:
148 priority
= LOG_NOTICE
;
150 case HOSTAPD_LEVEL_WARNING
:
151 priority
= LOG_WARNING
;
157 syslog(priority
, "%s", format
);
159 #endif /* CONFIG_NATIVE_WINDOWS */
163 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
167 * hostapd_init - Allocate and initialize per-interface data
168 * @config_file: Path to the configuration file
169 * Returns: Pointer to the allocated interface data or %NULL on failure
171 * This function is used to allocate main data structures for per-interface
172 * data. The allocated data buffer will be freed by calling
173 * hostapd_cleanup_iface().
175 static struct hostapd_iface
* hostapd_init(const char *config_file
)
177 struct hostapd_iface
*hapd_iface
= NULL
;
178 struct hostapd_config
*conf
= NULL
;
179 struct hostapd_data
*hapd
;
182 hapd_iface
= os_zalloc(sizeof(*hapd_iface
));
183 if (hapd_iface
== NULL
)
186 hapd_iface
->reload_config
= hostapd_reload_config
;
187 hapd_iface
->config_read_cb
= hostapd_config_read
;
188 hapd_iface
->config_fname
= os_strdup(config_file
);
189 if (hapd_iface
->config_fname
== NULL
)
191 hapd_iface
->ctrl_iface_init
= hostapd_ctrl_iface_init
;
192 hapd_iface
->ctrl_iface_deinit
= hostapd_ctrl_iface_deinit
;
193 hapd_iface
->for_each_interface
= hostapd_for_each_interface
;
195 conf
= hostapd_config_read(hapd_iface
->config_fname
);
198 hapd_iface
->conf
= conf
;
200 hapd_iface
->num_bss
= conf
->num_bss
;
201 hapd_iface
->bss
= os_zalloc(conf
->num_bss
*
202 sizeof(struct hostapd_data
*));
203 if (hapd_iface
->bss
== NULL
)
206 for (i
= 0; i
< conf
->num_bss
; i
++) {
207 hapd
= hapd_iface
->bss
[i
] =
208 hostapd_alloc_bss_data(hapd_iface
, conf
,
218 hostapd_config_free(conf
);
220 os_free(hapd_iface
->config_fname
);
221 os_free(hapd_iface
->bss
);
228 static int hostapd_driver_init(struct hostapd_iface
*iface
)
230 struct wpa_init_params params
;
232 struct hostapd_data
*hapd
= iface
->bss
[0];
233 struct hostapd_bss_config
*conf
= hapd
->conf
;
236 if (hapd
->driver
== NULL
|| hapd
->driver
->hapd_init
== NULL
) {
237 wpa_printf(MSG_ERROR
, "No hostapd driver wrapper available");
241 /* Initialize the driver interface */
242 if (!(b
[0] | b
[1] | b
[2] | b
[3] | b
[4] | b
[5]))
245 os_memset(¶ms
, 0, sizeof(params
));
247 params
.ifname
= hapd
->conf
->iface
;
248 params
.ssid
= (const u8
*) hapd
->conf
->ssid
.ssid
;
249 params
.ssid_len
= hapd
->conf
->ssid
.ssid_len
;
250 params
.test_socket
= hapd
->conf
->test_socket
;
251 params
.use_pae_group_addr
= hapd
->conf
->use_pae_group_addr
;
253 params
.num_bridge
= hapd
->iface
->num_bss
;
254 params
.bridge
= os_zalloc(hapd
->iface
->num_bss
* sizeof(char *));
255 if (params
.bridge
== NULL
)
257 for (i
= 0; i
< hapd
->iface
->num_bss
; i
++) {
258 struct hostapd_data
*bss
= hapd
->iface
->bss
[i
];
259 if (bss
->conf
->bridge
[0])
260 params
.bridge
[i
] = bss
->conf
->bridge
;
263 params
.own_addr
= hapd
->own_addr
;
265 hapd
->drv_priv
= hapd
->driver
->hapd_init(hapd
, ¶ms
);
266 os_free(params
.bridge
);
267 if (hapd
->drv_priv
== NULL
) {
268 wpa_printf(MSG_ERROR
, "%s driver initialization failed.",
278 static void hostapd_interface_deinit_free(struct hostapd_iface
*iface
)
280 const struct wpa_driver_ops
*driver
;
284 driver
= iface
->bss
[0]->driver
;
285 drv_priv
= iface
->bss
[0]->drv_priv
;
286 hostapd_interface_deinit(iface
);
287 if (driver
&& driver
->hapd_deinit
)
288 driver
->hapd_deinit(drv_priv
);
289 hostapd_interface_free(iface
);
293 static struct hostapd_iface
*
294 hostapd_interface_init(struct hapd_interfaces
*interfaces
,
295 const char *config_fname
, int debug
)
297 struct hostapd_iface
*iface
;
300 wpa_printf(MSG_ERROR
, "Configuration file: %s", config_fname
);
301 iface
= hostapd_init(config_fname
);
304 iface
->interfaces
= interfaces
;
306 for (k
= 0; k
< debug
; k
++) {
307 if (iface
->bss
[0]->conf
->logger_stdout_level
> 0)
308 iface
->bss
[0]->conf
->logger_stdout_level
--;
311 if (hostapd_driver_init(iface
) ||
312 hostapd_setup_interface(iface
)) {
313 hostapd_interface_deinit_free(iface
);
322 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
324 static void handle_term(int sig
, void *signal_ctx
)
326 wpa_printf(MSG_DEBUG
, "Signal %d received - terminating", sig
);
331 #ifndef CONFIG_NATIVE_WINDOWS
333 static int handle_reload_iface(struct hostapd_iface
*iface
, void *ctx
)
335 if (hostapd_reload_config(iface
) < 0) {
336 wpa_printf(MSG_WARNING
, "Failed to read new configuration "
337 "file - continuing with old.");
344 * handle_reload - SIGHUP handler to reload configuration
346 static void handle_reload(int sig
, void *signal_ctx
)
348 struct hapd_interfaces
*interfaces
= signal_ctx
;
349 wpa_printf(MSG_DEBUG
, "Signal %d received - reloading configuration",
351 hostapd_for_each_interface(interfaces
, handle_reload_iface
, NULL
);
355 static void handle_dump_state(int sig
, void *signal_ctx
)
357 #ifdef HOSTAPD_DUMP_STATE
358 struct hapd_interfaces
*interfaces
= signal_ctx
;
359 hostapd_for_each_interface(interfaces
, handle_dump_state_iface
, NULL
);
360 #endif /* HOSTAPD_DUMP_STATE */
362 #endif /* CONFIG_NATIVE_WINDOWS */
365 static int hostapd_global_init(struct hapd_interfaces
*interfaces
)
367 hostapd_logger_register_cb(hostapd_logger_cb
);
369 if (eap_server_register_methods()) {
370 wpa_printf(MSG_ERROR
, "Failed to register EAP methods");
375 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
379 #ifndef CONFIG_NATIVE_WINDOWS
380 eloop_register_signal(SIGHUP
, handle_reload
, interfaces
);
381 eloop_register_signal(SIGUSR1
, handle_dump_state
, interfaces
);
382 #endif /* CONFIG_NATIVE_WINDOWS */
383 eloop_register_signal_terminate(handle_term
, interfaces
);
385 #ifndef CONFIG_NATIVE_WINDOWS
386 openlog("hostapd", 0, LOG_DAEMON
);
387 #endif /* CONFIG_NATIVE_WINDOWS */
393 static void hostapd_global_deinit(const char *pid_file
)
395 #ifdef EAP_SERVER_TNC
396 tncs_global_deinit();
397 #endif /* EAP_SERVER_TNC */
401 #ifndef CONFIG_NATIVE_WINDOWS
403 #endif /* CONFIG_NATIVE_WINDOWS */
405 eap_server_unregister_methods();
407 os_daemonize_terminate(pid_file
);
411 static int hostapd_global_run(struct hapd_interfaces
*ifaces
, int daemonize
,
412 const char *pid_file
)
414 #ifdef EAP_SERVER_TNC
418 for (i
= 0; !tnc
&& i
< ifaces
->count
; i
++) {
419 for (k
= 0; k
< ifaces
->iface
[i
]->num_bss
; k
++) {
420 if (ifaces
->iface
[i
]->bss
[0]->conf
->tnc
) {
427 if (tnc
&& tncs_global_init() < 0) {
428 wpa_printf(MSG_ERROR
, "Failed to initialize TNCS");
431 #endif /* EAP_SERVER_TNC */
433 if (daemonize
&& os_daemonize(pid_file
)) {
444 static void show_version(void)
447 "hostapd v" VERSION_STR
"\n"
448 "User space daemon for IEEE 802.11 AP management,\n"
449 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
450 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
451 "and contributors\n");
455 static void usage(void)
460 "usage: hostapd [-hdBKtv] [-P <PID file>] "
461 "<configuration file(s)>\n"
464 " -h show this usage\n"
465 " -d show more debug messages (-dd for even more)\n"
466 " -B run daemon in the background\n"
468 " -K include key data in debug messages\n"
469 " -t include timestamps in some debug messages\n"
470 " -v show hostapd version\n");
476 int main(int argc
, char *argv
[])
478 struct hapd_interfaces interfaces
;
481 int c
, debug
= 0, daemonize
= 0;
482 char *pid_file
= NULL
;
484 if (os_program_init())
488 c
= getopt(argc
, argv
, "BdhKP:tv");
497 if (wpa_debug_level
> 0)
504 wpa_debug_show_keys
++;
508 pid_file
= os_rel2abs_path(optarg
);
511 wpa_debug_timestamp
++;
527 interfaces
.count
= argc
- optind
;
528 interfaces
.iface
= os_malloc(interfaces
.count
*
529 sizeof(struct hostapd_iface
*));
530 if (interfaces
.iface
== NULL
) {
531 wpa_printf(MSG_ERROR
, "malloc failed\n");
535 if (hostapd_global_init(&interfaces
))
538 /* Initialize interfaces */
539 for (i
= 0; i
< interfaces
.count
; i
++) {
540 interfaces
.iface
[i
] = hostapd_interface_init(&interfaces
,
543 if (!interfaces
.iface
[i
])
547 if (hostapd_global_run(&interfaces
, daemonize
, pid_file
))
553 /* Deinitialize all interfaces */
554 for (i
= 0; i
< interfaces
.count
; i
++)
555 hostapd_interface_deinit_free(interfaces
.iface
[i
]);
556 os_free(interfaces
.iface
);
558 hostapd_global_deinit(pid_file
);