d69e1e637289927342141d9a10cb7a3b370c3d1f
[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 #include "eap_register.h"
31 #include "dump_state.h"
32 #include "ctrl_iface.h"
33
34
35 extern int wpa_debug_level;
36 extern int wpa_debug_show_keys;
37 extern int wpa_debug_timestamp;
38
39
40 struct hapd_interfaces {
41         size_t count;
42         struct hostapd_iface **iface;
43 };
44
45
46 static int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
47                                       int (*cb)(struct hostapd_iface *iface,
48                                                 void *ctx), void *ctx)
49 {
50         size_t i;
51         int ret;
52
53         for (i = 0; i < interfaces->count; i++) {
54                 ret = cb(interfaces->iface[i], ctx);
55                 if (ret)
56                         return ret;
57         }
58
59         return 0;
60 }
61
62
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)
66 {
67         struct hostapd_data *hapd = ctx;
68         char *format, *module_str;
69         int maxlen;
70         int conf_syslog_level, conf_stdout_level;
71         unsigned int conf_syslog, conf_stdout;
72
73         maxlen = len + 100;
74         format = os_malloc(maxlen);
75         if (!format)
76                 return;
77
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;
83         } else {
84                 conf_syslog_level = conf_stdout_level = 0;
85                 conf_syslog = conf_stdout = (unsigned int) -1;
86         }
87
88         switch (module) {
89         case HOSTAPD_MODULE_IEEE80211:
90                 module_str = "IEEE 802.11";
91                 break;
92         case HOSTAPD_MODULE_IEEE8021X:
93                 module_str = "IEEE 802.1X";
94                 break;
95         case HOSTAPD_MODULE_RADIUS:
96                 module_str = "RADIUS";
97                 break;
98         case HOSTAPD_MODULE_WPA:
99                 module_str = "WPA";
100                 break;
101         case HOSTAPD_MODULE_DRIVER:
102                 module_str = "DRIVER";
103                 break;
104         case HOSTAPD_MODULE_IAPP:
105                 module_str = "IAPP";
106                 break;
107         case HOSTAPD_MODULE_MLME:
108                 module_str = "MLME";
109                 break;
110         default:
111                 module_str = NULL;
112                 break;
113         }
114
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 ? " " : "",
122                             module_str, txt);
123         else if (addr)
124                 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
125                             MAC2STR(addr), module_str ? " " : "",
126                             module_str, txt);
127         else
128                 os_snprintf(format, maxlen, "%s%s%s",
129                             module_str, module_str ? ": " : "", txt);
130
131         if ((conf_stdout & module) && level >= conf_stdout_level) {
132                 wpa_debug_print_timestamp();
133                 printf("%s\n", format);
134         }
135
136 #ifndef CONFIG_NATIVE_WINDOWS
137         if ((conf_syslog & module) && level >= conf_syslog_level) {
138                 int priority;
139                 switch (level) {
140                 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
141                 case HOSTAPD_LEVEL_DEBUG:
142                         priority = LOG_DEBUG;
143                         break;
144                 case HOSTAPD_LEVEL_INFO:
145                         priority = LOG_INFO;
146                         break;
147                 case HOSTAPD_LEVEL_NOTICE:
148                         priority = LOG_NOTICE;
149                         break;
150                 case HOSTAPD_LEVEL_WARNING:
151                         priority = LOG_WARNING;
152                         break;
153                 default:
154                         priority = LOG_INFO;
155                         break;
156                 }
157                 syslog(priority, "%s", format);
158         }
159 #endif /* CONFIG_NATIVE_WINDOWS */
160
161         os_free(format);
162 }
163 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
164
165
166 /**
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
170  *
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().
174  */
175 static struct hostapd_iface * hostapd_init(const char *config_file)
176 {
177         struct hostapd_iface *hapd_iface = NULL;
178         struct hostapd_config *conf = NULL;
179         struct hostapd_data *hapd;
180         size_t i;
181
182         hapd_iface = os_zalloc(sizeof(*hapd_iface));
183         if (hapd_iface == NULL)
184                 goto fail;
185
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)
190                 goto fail;
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;
194
195         conf = hostapd_config_read(hapd_iface->config_fname);
196         if (conf == NULL)
197                 goto fail;
198         hapd_iface->conf = conf;
199
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)
204                 goto fail;
205
206         for (i = 0; i < conf->num_bss; i++) {
207                 hapd = hapd_iface->bss[i] =
208                         hostapd_alloc_bss_data(hapd_iface, conf,
209                                                &conf->bss[i]);
210                 if (hapd == NULL)
211                         goto fail;
212         }
213
214         return hapd_iface;
215
216 fail:
217         if (conf)
218                 hostapd_config_free(conf);
219         if (hapd_iface) {
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                 const struct wpa_driver_ops *driver;
299                 void *drv_priv;
300                 driver = iface->bss[0]->driver;
301                 drv_priv = iface->bss[0]->drv_priv;
302                 hostapd_interface_deinit(iface);
303                 if (driver && driver->hapd_deinit)
304                         driver->hapd_deinit(drv_priv);
305                 return NULL;
306         }
307
308         return iface;
309 }
310
311
312 /**
313  * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
314  */
315 static void handle_term(int sig, void *signal_ctx)
316 {
317         wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
318         eloop_terminate();
319 }
320
321
322 #ifndef CONFIG_NATIVE_WINDOWS
323
324 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
325 {
326         if (hostapd_reload_config(iface) < 0) {
327                 wpa_printf(MSG_WARNING, "Failed to read new configuration "
328                            "file - continuing with old.");
329         }
330         return 0;
331 }
332
333
334 /**
335  * handle_reload - SIGHUP handler to reload configuration
336  */
337 static void handle_reload(int sig, void *signal_ctx)
338 {
339         struct hapd_interfaces *interfaces = signal_ctx;
340         wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
341                    sig);
342         hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
343 }
344
345
346 static void handle_dump_state(int sig, void *signal_ctx)
347 {
348 #ifdef HOSTAPD_DUMP_STATE
349         struct hapd_interfaces *interfaces = signal_ctx;
350         hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
351 #endif /* HOSTAPD_DUMP_STATE */
352 }
353 #endif /* CONFIG_NATIVE_WINDOWS */
354
355
356 static int hostapd_global_init(struct hapd_interfaces *interfaces)
357 {
358         hostapd_logger_register_cb(hostapd_logger_cb);
359
360         if (eap_server_register_methods()) {
361                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
362                 return -1;
363         }
364
365         if (eloop_init()) {
366                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
367                 return -1;
368         }
369
370 #ifndef CONFIG_NATIVE_WINDOWS
371         eloop_register_signal(SIGHUP, handle_reload, interfaces);
372         eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
373 #endif /* CONFIG_NATIVE_WINDOWS */
374         eloop_register_signal_terminate(handle_term, interfaces);
375
376 #ifndef CONFIG_NATIVE_WINDOWS
377         openlog("hostapd", 0, LOG_DAEMON);
378 #endif /* CONFIG_NATIVE_WINDOWS */
379
380         return 0;
381 }
382
383
384 static void hostapd_global_deinit(const char *pid_file)
385 {
386 #ifdef EAP_SERVER_TNC
387         tncs_global_deinit();
388 #endif /* EAP_SERVER_TNC */
389
390         eloop_destroy();
391
392 #ifndef CONFIG_NATIVE_WINDOWS
393         closelog();
394 #endif /* CONFIG_NATIVE_WINDOWS */
395
396         eap_server_unregister_methods();
397
398         os_daemonize_terminate(pid_file);
399 }
400
401
402 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
403                               const char *pid_file)
404 {
405 #ifdef EAP_SERVER_TNC
406         int tnc = 0;
407         size_t i, k;
408
409         for (i = 0; !tnc && i < ifaces->count; i++) {
410                 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
411                         if (ifaces->iface[i]->bss[0]->conf->tnc) {
412                                 tnc++;
413                                 break;
414                         }
415                 }
416         }
417
418         if (tnc && tncs_global_init() < 0) {
419                 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
420                 return -1;
421         }
422 #endif /* EAP_SERVER_TNC */
423
424         if (daemonize && os_daemonize(pid_file)) {
425                 perror("daemon");
426                 return -1;
427         }
428
429         eloop_run();
430
431         return 0;
432 }
433
434
435 static void show_version(void)
436 {
437         fprintf(stderr,
438                 "hostapd v" VERSION_STR "\n"
439                 "User space daemon for IEEE 802.11 AP management,\n"
440                 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
441                 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
442                 "and contributors\n");
443 }
444
445
446 static void usage(void)
447 {
448         show_version();
449         fprintf(stderr,
450                 "\n"
451                 "usage: hostapd [-hdBKtv] [-P <PID file>] "
452                 "<configuration file(s)>\n"
453                 "\n"
454                 "options:\n"
455                 "   -h   show this usage\n"
456                 "   -d   show more debug messages (-dd for even more)\n"
457                 "   -B   run daemon in the background\n"
458                 "   -P   PID file\n"
459                 "   -K   include key data in debug messages\n"
460                 "   -t   include timestamps in some debug messages\n"
461                 "   -v   show hostapd version\n");
462
463         exit(1);
464 }
465
466
467 int main(int argc, char *argv[])
468 {
469         struct hapd_interfaces interfaces;
470         int ret = 1;
471         size_t i;
472         int c, debug = 0, daemonize = 0;
473         char *pid_file = NULL;
474
475         if (os_program_init())
476                 return -1;
477
478         for (;;) {
479                 c = getopt(argc, argv, "BdhKP:tv");
480                 if (c < 0)
481                         break;
482                 switch (c) {
483                 case 'h':
484                         usage();
485                         break;
486                 case 'd':
487                         debug++;
488                         if (wpa_debug_level > 0)
489                                 wpa_debug_level--;
490                         break;
491                 case 'B':
492                         daemonize++;
493                         break;
494                 case 'K':
495                         wpa_debug_show_keys++;
496                         break;
497                 case 'P':
498                         os_free(pid_file);
499                         pid_file = os_rel2abs_path(optarg);
500                         break;
501                 case 't':
502                         wpa_debug_timestamp++;
503                         break;
504                 case 'v':
505                         show_version();
506                         exit(1);
507                         break;
508
509                 default:
510                         usage();
511                         break;
512                 }
513         }
514
515         if (optind == argc)
516                 usage();
517
518         interfaces.count = argc - optind;
519         interfaces.iface = os_malloc(interfaces.count *
520                                      sizeof(struct hostapd_iface *));
521         if (interfaces.iface == NULL) {
522                 wpa_printf(MSG_ERROR, "malloc failed\n");
523                 return -1;
524         }
525
526         if (hostapd_global_init(&interfaces))
527                 return -1;
528
529         /* Initialize interfaces */
530         for (i = 0; i < interfaces.count; i++) {
531                 interfaces.iface[i] = hostapd_interface_init(&interfaces,
532                                                              argv[optind + i],
533                                                              debug);
534                 if (!interfaces.iface[i])
535                         goto out;
536         }
537
538         if (hostapd_global_run(&interfaces, daemonize, pid_file))
539                 goto out;
540
541         ret = 0;
542
543  out:
544         /* Deinitialize all interfaces */
545         for (i = 0; i < interfaces.count; i++) {
546                 struct hostapd_iface *iface = interfaces.iface[i];
547                 const struct wpa_driver_ops *driver;
548                 void *drv_priv;
549                 driver = iface->bss[0]->driver;
550                 drv_priv = iface->bss[0]->drv_priv;
551                 hostapd_interface_deinit(iface);
552                 if (driver && driver->hapd_deinit)
553                         driver->hapd_deinit(drv_priv);
554         }
555         os_free(interfaces.iface);
556
557         hostapd_global_deinit(pid_file);
558         os_free(pid_file);
559
560         os_program_deinit();
561
562         return ret;
563 }