Move hostapd driver initialization away from hostapd.c
[libeap.git] / hostapd / main.c
1 /*
2  * hostapd / main()
3  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16 #ifndef CONFIG_NATIVE_WINDOWS
17 #include <syslog.h>
18 #endif /* CONFIG_NATIVE_WINDOWS */
19
20 #include "common.h"
21 #include "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/config.h"
29 #include "config_file.h"
30
31
32 extern int wpa_debug_level;
33 extern int wpa_debug_show_keys;
34 extern int wpa_debug_timestamp;
35
36
37 struct hapd_interfaces {
38         size_t count;
39         struct hostapd_iface **iface;
40 };
41
42
43 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
44                                int (*cb)(struct hostapd_iface *iface,
45                                          void *ctx), void *ctx)
46 {
47         size_t i;
48         int ret;
49
50         for (i = 0; i < interfaces->count; i++) {
51                 ret = cb(interfaces->iface[i], ctx);
52                 if (ret)
53                         return ret;
54         }
55
56         return 0;
57 }
58
59
60 #ifndef CONFIG_NO_HOSTAPD_LOGGER
61 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
62                               int level, const char *txt, size_t len)
63 {
64         struct hostapd_data *hapd = ctx;
65         char *format, *module_str;
66         int maxlen;
67         int conf_syslog_level, conf_stdout_level;
68         unsigned int conf_syslog, conf_stdout;
69
70         maxlen = len + 100;
71         format = os_malloc(maxlen);
72         if (!format)
73                 return;
74
75         if (hapd && hapd->conf) {
76                 conf_syslog_level = hapd->conf->logger_syslog_level;
77                 conf_stdout_level = hapd->conf->logger_stdout_level;
78                 conf_syslog = hapd->conf->logger_syslog;
79                 conf_stdout = hapd->conf->logger_stdout;
80         } else {
81                 conf_syslog_level = conf_stdout_level = 0;
82                 conf_syslog = conf_stdout = (unsigned int) -1;
83         }
84
85         switch (module) {
86         case HOSTAPD_MODULE_IEEE80211:
87                 module_str = "IEEE 802.11";
88                 break;
89         case HOSTAPD_MODULE_IEEE8021X:
90                 module_str = "IEEE 802.1X";
91                 break;
92         case HOSTAPD_MODULE_RADIUS:
93                 module_str = "RADIUS";
94                 break;
95         case HOSTAPD_MODULE_WPA:
96                 module_str = "WPA";
97                 break;
98         case HOSTAPD_MODULE_DRIVER:
99                 module_str = "DRIVER";
100                 break;
101         case HOSTAPD_MODULE_IAPP:
102                 module_str = "IAPP";
103                 break;
104         case HOSTAPD_MODULE_MLME:
105                 module_str = "MLME";
106                 break;
107         default:
108                 module_str = NULL;
109                 break;
110         }
111
112         if (hapd && hapd->conf && addr)
113                 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
114                             hapd->conf->iface, MAC2STR(addr),
115                             module_str ? " " : "", module_str, txt);
116         else if (hapd && hapd->conf)
117                 os_snprintf(format, maxlen, "%s:%s%s %s",
118                             hapd->conf->iface, module_str ? " " : "",
119                             module_str, txt);
120         else if (addr)
121                 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
122                             MAC2STR(addr), module_str ? " " : "",
123                             module_str, txt);
124         else
125                 os_snprintf(format, maxlen, "%s%s%s",
126                             module_str, module_str ? ": " : "", txt);
127
128         if ((conf_stdout & module) && level >= conf_stdout_level) {
129                 wpa_debug_print_timestamp();
130                 printf("%s\n", format);
131         }
132
133 #ifndef CONFIG_NATIVE_WINDOWS
134         if ((conf_syslog & module) && level >= conf_syslog_level) {
135                 int priority;
136                 switch (level) {
137                 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
138                 case HOSTAPD_LEVEL_DEBUG:
139                         priority = LOG_DEBUG;
140                         break;
141                 case HOSTAPD_LEVEL_INFO:
142                         priority = LOG_INFO;
143                         break;
144                 case HOSTAPD_LEVEL_NOTICE:
145                         priority = LOG_NOTICE;
146                         break;
147                 case HOSTAPD_LEVEL_WARNING:
148                         priority = LOG_WARNING;
149                         break;
150                 default:
151                         priority = LOG_INFO;
152                         break;
153                 }
154                 syslog(priority, "%s", format);
155         }
156 #endif /* CONFIG_NATIVE_WINDOWS */
157
158         os_free(format);
159 }
160 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
161
162
163 /**
164  * hostapd_init - Allocate and initialize per-interface data
165  * @config_file: Path to the configuration file
166  * Returns: Pointer to the allocated interface data or %NULL on failure
167  *
168  * This function is used to allocate main data structures for per-interface
169  * data. The allocated data buffer will be freed by calling
170  * hostapd_cleanup_iface().
171  */
172 static struct hostapd_iface * hostapd_init(const char *config_file)
173 {
174         struct hostapd_iface *hapd_iface = NULL;
175         struct hostapd_config *conf = NULL;
176         struct hostapd_data *hapd;
177         size_t i;
178
179         hapd_iface = os_zalloc(sizeof(*hapd_iface));
180         if (hapd_iface == NULL)
181                 goto fail;
182
183         hapd_iface->reload_config = hostapd_reload_config;
184         hapd_iface->config_read_cb = hostapd_config_read;
185         hapd_iface->config_fname = os_strdup(config_file);
186         if (hapd_iface->config_fname == NULL)
187                 goto fail;
188
189         conf = hostapd_config_read(hapd_iface->config_fname);
190         if (conf == NULL)
191                 goto fail;
192         hapd_iface->conf = conf;
193
194         hapd_iface->num_bss = conf->num_bss;
195         hapd_iface->bss = os_zalloc(conf->num_bss *
196                                     sizeof(struct hostapd_data *));
197         if (hapd_iface->bss == NULL)
198                 goto fail;
199
200         for (i = 0; i < conf->num_bss; i++) {
201                 hapd = hapd_iface->bss[i] =
202                         hostapd_alloc_bss_data(hapd_iface, conf,
203                                                &conf->bss[i]);
204                 if (hapd == NULL)
205                         goto fail;
206         }
207
208         return hapd_iface;
209
210 fail:
211         if (conf)
212                 hostapd_config_free(conf);
213         if (hapd_iface) {
214                 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
215                         hapd = hapd_iface->bss[i];
216                         if (hapd && hapd->ssl_ctx)
217                                 tls_deinit(hapd->ssl_ctx);
218                 }
219
220                 os_free(hapd_iface->config_fname);
221                 os_free(hapd_iface->bss);
222                 os_free(hapd_iface);
223         }
224         return NULL;
225 }
226
227
228 static int hostapd_driver_init(struct hostapd_iface *iface)
229 {
230         struct wpa_init_params params;
231         size_t i;
232         struct hostapd_data *hapd = iface->bss[0];
233         struct hostapd_bss_config *conf = hapd->conf;
234         u8 *b = conf->bssid;
235
236         if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
237                 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
238                 return -1;
239         }
240
241         /* Initialize the driver interface */
242         if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
243                 b = NULL;
244
245         os_memset(&params, 0, sizeof(params));
246         params.bssid = b;
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;
252
253         params.num_bridge = hapd->iface->num_bss;
254         params.bridge = os_zalloc(hapd->iface->num_bss * sizeof(char *));
255         if (params.bridge == NULL)
256                 return -1;
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;
261         }
262
263         params.own_addr = hapd->own_addr;
264
265         hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
266         os_free(params.bridge);
267         if (hapd->drv_priv == NULL) {
268                 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
269                            hapd->driver->name);
270                 hapd->driver = NULL;
271                 return -1;
272         }
273
274         return 0;
275 }
276
277
278 static struct hostapd_iface *
279 hostapd_interface_init(struct hapd_interfaces *interfaces,
280                        const char *config_fname, int debug)
281 {
282         struct hostapd_iface *iface;
283         int k;
284
285         wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
286         iface = hostapd_init(config_fname);
287         if (!iface)
288                 return NULL;
289         iface->interfaces = interfaces;
290
291         for (k = 0; k < debug; k++) {
292                 if (iface->bss[0]->conf->logger_stdout_level > 0)
293                         iface->bss[0]->conf->logger_stdout_level--;
294         }
295
296         if (hostapd_driver_init(iface) ||
297             hostapd_setup_interface(iface)) {
298                 hostapd_interface_deinit(iface);
299                 return NULL;
300         }
301
302         return iface;
303 }
304
305
306 /**
307  * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
308  */
309 static void handle_term(int sig, void *signal_ctx)
310 {
311         wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
312         eloop_terminate();
313 }
314
315
316 #ifndef CONFIG_NATIVE_WINDOWS
317 /**
318  * handle_reload - SIGHUP handler to reload configuration
319  */
320 static void handle_reload(int sig, void *signal_ctx)
321 {
322         struct hapd_interfaces *interfaces = signal_ctx;
323         wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
324                    sig);
325         hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
326 }
327
328
329 static void handle_dump_state(int sig, void *signal_ctx)
330 {
331 #ifdef HOSTAPD_DUMP_STATE
332         struct hapd_interfaces *interfaces = signal_ctx;
333         hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
334 #endif /* HOSTAPD_DUMP_STATE */
335 }
336 #endif /* CONFIG_NATIVE_WINDOWS */
337
338
339 static int hostapd_global_init(struct hapd_interfaces *interfaces)
340 {
341         hostapd_logger_register_cb(hostapd_logger_cb);
342
343         if (eap_server_register_methods()) {
344                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
345                 return -1;
346         }
347
348         if (eloop_init()) {
349                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
350                 return -1;
351         }
352
353 #ifndef CONFIG_NATIVE_WINDOWS
354         eloop_register_signal(SIGHUP, handle_reload, interfaces);
355         eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
356 #endif /* CONFIG_NATIVE_WINDOWS */
357         eloop_register_signal_terminate(handle_term, interfaces);
358
359 #ifndef CONFIG_NATIVE_WINDOWS
360         openlog("hostapd", 0, LOG_DAEMON);
361 #endif /* CONFIG_NATIVE_WINDOWS */
362
363         return 0;
364 }
365
366
367 static void hostapd_global_deinit(const char *pid_file)
368 {
369 #ifdef EAP_SERVER_TNC
370         tncs_global_deinit();
371 #endif /* EAP_SERVER_TNC */
372
373         eloop_destroy();
374
375 #ifndef CONFIG_NATIVE_WINDOWS
376         closelog();
377 #endif /* CONFIG_NATIVE_WINDOWS */
378
379         eap_server_unregister_methods();
380
381         os_daemonize_terminate(pid_file);
382 }
383
384
385 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
386                               const char *pid_file)
387 {
388 #ifdef EAP_SERVER_TNC
389         int tnc = 0;
390         size_t i, k;
391
392         for (i = 0; !tnc && i < ifaces->count; i++) {
393                 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
394                         if (ifaces->iface[i]->bss[0]->conf->tnc) {
395                                 tnc++;
396                                 break;
397                         }
398                 }
399         }
400
401         if (tnc && tncs_global_init() < 0) {
402                 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
403                 return -1;
404         }
405 #endif /* EAP_SERVER_TNC */
406
407         if (daemonize && os_daemonize(pid_file)) {
408                 perror("daemon");
409                 return -1;
410         }
411
412         eloop_run();
413
414         return 0;
415 }
416
417
418 static void show_version(void)
419 {
420         fprintf(stderr,
421                 "hostapd v" VERSION_STR "\n"
422                 "User space daemon for IEEE 802.11 AP management,\n"
423                 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
424                 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
425                 "and contributors\n");
426 }
427
428
429 static void usage(void)
430 {
431         show_version();
432         fprintf(stderr,
433                 "\n"
434                 "usage: hostapd [-hdBKtv] [-P <PID file>] "
435                 "<configuration file(s)>\n"
436                 "\n"
437                 "options:\n"
438                 "   -h   show this usage\n"
439                 "   -d   show more debug messages (-dd for even more)\n"
440                 "   -B   run daemon in the background\n"
441                 "   -P   PID file\n"
442                 "   -K   include key data in debug messages\n"
443                 "   -t   include timestamps in some debug messages\n"
444                 "   -v   show hostapd version\n");
445
446         exit(1);
447 }
448
449
450 int main(int argc, char *argv[])
451 {
452         struct hapd_interfaces interfaces;
453         int ret = 1;
454         size_t i;
455         int c, debug = 0, daemonize = 0;
456         char *pid_file = NULL;
457
458         if (os_program_init())
459                 return -1;
460
461         for (;;) {
462                 c = getopt(argc, argv, "BdhKP:tv");
463                 if (c < 0)
464                         break;
465                 switch (c) {
466                 case 'h':
467                         usage();
468                         break;
469                 case 'd':
470                         debug++;
471                         if (wpa_debug_level > 0)
472                                 wpa_debug_level--;
473                         break;
474                 case 'B':
475                         daemonize++;
476                         break;
477                 case 'K':
478                         wpa_debug_show_keys++;
479                         break;
480                 case 'P':
481                         os_free(pid_file);
482                         pid_file = os_rel2abs_path(optarg);
483                         break;
484                 case 't':
485                         wpa_debug_timestamp++;
486                         break;
487                 case 'v':
488                         show_version();
489                         exit(1);
490                         break;
491
492                 default:
493                         usage();
494                         break;
495                 }
496         }
497
498         if (optind == argc)
499                 usage();
500
501         interfaces.count = argc - optind;
502         interfaces.iface = os_malloc(interfaces.count *
503                                      sizeof(struct hostapd_iface *));
504         if (interfaces.iface == NULL) {
505                 wpa_printf(MSG_ERROR, "malloc failed\n");
506                 return -1;
507         }
508
509         if (hostapd_global_init(&interfaces))
510                 return -1;
511
512         /* Initialize interfaces */
513         for (i = 0; i < interfaces.count; i++) {
514                 interfaces.iface[i] = hostapd_interface_init(&interfaces,
515                                                              argv[optind + i],
516                                                              debug);
517                 if (!interfaces.iface[i])
518                         goto out;
519         }
520
521         if (hostapd_global_run(&interfaces, daemonize, pid_file))
522                 goto out;
523
524         ret = 0;
525
526  out:
527         /* Deinitialize all interfaces */
528         for (i = 0; i < interfaces.count; i++)
529                 hostapd_interface_deinit(interfaces.iface[i]);
530         os_free(interfaces.iface);
531
532         hostapd_global_deinit(pid_file);
533         os_free(pid_file);
534
535         os_program_deinit();
536
537         return ret;
538 }