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.
16 #ifndef CONFIG_NATIVE_WINDOWS
18 #endif /* CONFIG_NATIVE_WINDOWS */
22 #include "common/version.h"
25 #include "eap_server/eap.h"
26 #include "eap_server/tncs.h"
29 extern int wpa_debug_level
;
30 extern int wpa_debug_show_keys
;
31 extern int wpa_debug_timestamp
;
34 struct hapd_interfaces
{
36 struct hostapd_iface
**iface
;
40 int hostapd_for_each_interface(int (*cb
)(struct hostapd_iface
*iface
,
41 void *ctx
), void *ctx
)
43 struct hapd_interfaces
*interfaces
= eloop_get_user_data();
47 for (i
= 0; i
< interfaces
->count
; i
++) {
48 ret
= cb(interfaces
->iface
[i
], ctx
);
57 #ifndef CONFIG_NO_HOSTAPD_LOGGER
58 static void hostapd_logger_cb(void *ctx
, const u8
*addr
, unsigned int module
,
59 int level
, const char *txt
, size_t len
)
61 struct hostapd_data
*hapd
= ctx
;
62 char *format
, *module_str
;
64 int conf_syslog_level
, conf_stdout_level
;
65 unsigned int conf_syslog
, conf_stdout
;
68 format
= os_malloc(maxlen
);
72 if (hapd
&& hapd
->conf
) {
73 conf_syslog_level
= hapd
->conf
->logger_syslog_level
;
74 conf_stdout_level
= hapd
->conf
->logger_stdout_level
;
75 conf_syslog
= hapd
->conf
->logger_syslog
;
76 conf_stdout
= hapd
->conf
->logger_stdout
;
78 conf_syslog_level
= conf_stdout_level
= 0;
79 conf_syslog
= conf_stdout
= (unsigned int) -1;
83 case HOSTAPD_MODULE_IEEE80211
:
84 module_str
= "IEEE 802.11";
86 case HOSTAPD_MODULE_IEEE8021X
:
87 module_str
= "IEEE 802.1X";
89 case HOSTAPD_MODULE_RADIUS
:
90 module_str
= "RADIUS";
92 case HOSTAPD_MODULE_WPA
:
95 case HOSTAPD_MODULE_DRIVER
:
96 module_str
= "DRIVER";
98 case HOSTAPD_MODULE_IAPP
:
101 case HOSTAPD_MODULE_MLME
:
109 if (hapd
&& hapd
->conf
&& addr
)
110 os_snprintf(format
, maxlen
, "%s: STA " MACSTR
"%s%s: %s",
111 hapd
->conf
->iface
, MAC2STR(addr
),
112 module_str
? " " : "", module_str
, txt
);
113 else if (hapd
&& hapd
->conf
)
114 os_snprintf(format
, maxlen
, "%s:%s%s %s",
115 hapd
->conf
->iface
, module_str
? " " : "",
118 os_snprintf(format
, maxlen
, "STA " MACSTR
"%s%s: %s",
119 MAC2STR(addr
), module_str
? " " : "",
122 os_snprintf(format
, maxlen
, "%s%s%s",
123 module_str
, module_str
? ": " : "", txt
);
125 if ((conf_stdout
& module
) && level
>= conf_stdout_level
) {
126 wpa_debug_print_timestamp();
127 printf("%s\n", format
);
130 #ifndef CONFIG_NATIVE_WINDOWS
131 if ((conf_syslog
& module
) && level
>= conf_syslog_level
) {
134 case HOSTAPD_LEVEL_DEBUG_VERBOSE
:
135 case HOSTAPD_LEVEL_DEBUG
:
136 priority
= LOG_DEBUG
;
138 case HOSTAPD_LEVEL_INFO
:
141 case HOSTAPD_LEVEL_NOTICE
:
142 priority
= LOG_NOTICE
;
144 case HOSTAPD_LEVEL_WARNING
:
145 priority
= LOG_WARNING
;
151 syslog(priority
, "%s", format
);
153 #endif /* CONFIG_NATIVE_WINDOWS */
157 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
161 * hostapd_init - Allocate and initialize per-interface data
162 * @config_file: Path to the configuration file
163 * Returns: Pointer to the allocated interface data or %NULL on failure
165 * This function is used to allocate main data structures for per-interface
166 * data. The allocated data buffer will be freed by calling
167 * hostapd_cleanup_iface().
169 static struct hostapd_iface
* hostapd_init(const char *config_file
)
171 struct hostapd_iface
*hapd_iface
= NULL
;
172 struct hostapd_config
*conf
= NULL
;
173 struct hostapd_data
*hapd
;
176 hapd_iface
= os_zalloc(sizeof(*hapd_iface
));
177 if (hapd_iface
== NULL
)
180 hapd_iface
->config_fname
= os_strdup(config_file
);
181 if (hapd_iface
->config_fname
== NULL
)
184 conf
= hostapd_config_read(hapd_iface
->config_fname
);
187 hapd_iface
->conf
= conf
;
189 hapd_iface
->num_bss
= conf
->num_bss
;
190 hapd_iface
->bss
= os_zalloc(conf
->num_bss
*
191 sizeof(struct hostapd_data
*));
192 if (hapd_iface
->bss
== NULL
)
195 for (i
= 0; i
< conf
->num_bss
; i
++) {
196 hapd
= hapd_iface
->bss
[i
] =
197 hostapd_alloc_bss_data(hapd_iface
, conf
,
207 hostapd_config_free(conf
);
209 for (i
= 0; hapd_iface
->bss
&& i
< hapd_iface
->num_bss
; i
++) {
210 hapd
= hapd_iface
->bss
[i
];
211 if (hapd
&& hapd
->ssl_ctx
)
212 tls_deinit(hapd
->ssl_ctx
);
215 os_free(hapd_iface
->config_fname
);
216 os_free(hapd_iface
->bss
);
223 static struct hostapd_iface
* hostapd_interface_init(const char *config_fname
,
226 struct hostapd_iface
*iface
;
229 wpa_printf(MSG_ERROR
, "Configuration file: %s", config_fname
);
230 iface
= hostapd_init(config_fname
);
234 for (k
= 0; k
< debug
; k
++) {
235 if (iface
->bss
[0]->conf
->logger_stdout_level
> 0)
236 iface
->bss
[0]->conf
->logger_stdout_level
--;
239 if (hostapd_setup_interface(iface
)) {
240 hostapd_interface_deinit(iface
);
249 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
251 static void handle_term(int sig
, void *eloop_ctx
, void *signal_ctx
)
253 wpa_printf(MSG_DEBUG
, "Signal %d received - terminating", sig
);
258 #ifndef CONFIG_NATIVE_WINDOWS
260 * handle_reload - SIGHUP handler to reload configuration
262 static void handle_reload(int sig
, void *eloop_ctx
, void *signal_ctx
)
264 wpa_printf(MSG_DEBUG
, "Signal %d received - reloading configuration",
266 hostapd_for_each_interface(handle_reload_iface
, NULL
);
270 static void handle_dump_state(int sig
, void *eloop_ctx
, void *signal_ctx
)
272 #ifdef HOSTAPD_DUMP_STATE
273 hostapd_for_each_interface(handle_dump_state_iface
, NULL
);
274 #endif /* HOSTAPD_DUMP_STATE */
276 #endif /* CONFIG_NATIVE_WINDOWS */
279 static int hostapd_global_init(struct hapd_interfaces
*interfaces
)
281 hostapd_logger_register_cb(hostapd_logger_cb
);
283 if (eap_server_register_methods()) {
284 wpa_printf(MSG_ERROR
, "Failed to register EAP methods");
288 if (eloop_init(interfaces
)) {
289 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
293 #ifndef CONFIG_NATIVE_WINDOWS
294 eloop_register_signal(SIGHUP
, handle_reload
, NULL
);
295 eloop_register_signal(SIGUSR1
, handle_dump_state
, NULL
);
296 #endif /* CONFIG_NATIVE_WINDOWS */
297 eloop_register_signal_terminate(handle_term
, NULL
);
299 #ifndef CONFIG_NATIVE_WINDOWS
300 openlog("hostapd", 0, LOG_DAEMON
);
301 #endif /* CONFIG_NATIVE_WINDOWS */
307 static void hostapd_global_deinit(const char *pid_file
)
309 #ifdef EAP_SERVER_TNC
310 tncs_global_deinit();
311 #endif /* EAP_SERVER_TNC */
315 #ifndef CONFIG_NATIVE_WINDOWS
317 #endif /* CONFIG_NATIVE_WINDOWS */
319 eap_server_unregister_methods();
321 os_daemonize_terminate(pid_file
);
325 static int hostapd_global_run(struct hapd_interfaces
*ifaces
, int daemonize
,
326 const char *pid_file
)
328 #ifdef EAP_SERVER_TNC
332 for (i
= 0; !tnc
&& i
< ifaces
->count
; i
++) {
333 for (k
= 0; k
< ifaces
->iface
[i
]->num_bss
; k
++) {
334 if (ifaces
->iface
[i
]->bss
[0]->conf
->tnc
) {
341 if (tnc
&& tncs_global_init() < 0) {
342 wpa_printf(MSG_ERROR
, "Failed to initialize TNCS");
345 #endif /* EAP_SERVER_TNC */
347 if (daemonize
&& os_daemonize(pid_file
)) {
358 static void show_version(void)
361 "hostapd v" VERSION_STR
"\n"
362 "User space daemon for IEEE 802.11 AP management,\n"
363 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
364 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
365 "and contributors\n");
369 static void usage(void)
374 "usage: hostapd [-hdBKtv] [-P <PID file>] "
375 "<configuration file(s)>\n"
378 " -h show this usage\n"
379 " -d show more debug messages (-dd for even more)\n"
380 " -B run daemon in the background\n"
382 " -K include key data in debug messages\n"
383 " -t include timestamps in some debug messages\n"
384 " -v show hostapd version\n");
390 int main(int argc
, char *argv
[])
392 struct hapd_interfaces interfaces
;
395 int c
, debug
= 0, daemonize
= 0;
396 char *pid_file
= NULL
;
399 c
= getopt(argc
, argv
, "BdhKP:tv");
408 if (wpa_debug_level
> 0)
415 wpa_debug_show_keys
++;
419 pid_file
= os_rel2abs_path(optarg
);
422 wpa_debug_timestamp
++;
438 interfaces
.count
= argc
- optind
;
439 interfaces
.iface
= os_malloc(interfaces
.count
*
440 sizeof(struct hostapd_iface
*));
441 if (interfaces
.iface
== NULL
) {
442 wpa_printf(MSG_ERROR
, "malloc failed\n");
446 if (hostapd_global_init(&interfaces
))
449 /* Initialize interfaces */
450 for (i
= 0; i
< interfaces
.count
; i
++) {
451 interfaces
.iface
[i
] = hostapd_interface_init(argv
[optind
+ i
],
453 if (!interfaces
.iface
[i
])
457 if (hostapd_global_run(&interfaces
, daemonize
, pid_file
))
463 /* Deinitialize all interfaces */
464 for (i
= 0; i
< interfaces
.count
; i
++)
465 hostapd_interface_deinit(interfaces
.iface
[i
]);
466 os_free(interfaces
.iface
);
468 hostapd_global_deinit(pid_file
);