]> git.ipfire.org Git - thirdparty/hostap.git/blob - hostapd/main.c
hostapd: Make hostapd_interface_init_bss() available externally
[thirdparty/hostap.git] / hostapd / main.c
1 /*
2 * hostapd / main()
3 * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10 #ifndef CONFIG_NATIVE_WINDOWS
11 #include <syslog.h>
12 #include <grp.h>
13 #endif /* CONFIG_NATIVE_WINDOWS */
14
15 #include "utils/common.h"
16 #include "utils/eloop.h"
17 #include "crypto/random.h"
18 #include "crypto/tls.h"
19 #include "common/version.h"
20 #include "drivers/driver.h"
21 #include "eap_server/eap.h"
22 #include "eap_server/tncs.h"
23 #include "ap/hostapd.h"
24 #include "ap/ap_config.h"
25 #include "ap/ap_drv_ops.h"
26 #include "config_file.h"
27 #include "eap_register.h"
28 #include "dump_state.h"
29 #include "ctrl_iface.h"
30
31
32 extern int wpa_debug_level;
33 extern int wpa_debug_show_keys;
34 extern int wpa_debug_timestamp;
35
36 extern struct wpa_driver_ops *wpa_drivers[];
37
38
39 struct hapd_global {
40 void **drv_priv;
41 size_t drv_count;
42 };
43
44 static struct hapd_global global;
45
46
47 #ifndef CONFIG_NO_HOSTAPD_LOGGER
48 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
49 int level, const char *txt, size_t len)
50 {
51 struct hostapd_data *hapd = ctx;
52 char *format, *module_str;
53 int maxlen;
54 int conf_syslog_level, conf_stdout_level;
55 unsigned int conf_syslog, conf_stdout;
56
57 maxlen = len + 100;
58 format = os_malloc(maxlen);
59 if (!format)
60 return;
61
62 if (hapd && hapd->conf) {
63 conf_syslog_level = hapd->conf->logger_syslog_level;
64 conf_stdout_level = hapd->conf->logger_stdout_level;
65 conf_syslog = hapd->conf->logger_syslog;
66 conf_stdout = hapd->conf->logger_stdout;
67 } else {
68 conf_syslog_level = conf_stdout_level = 0;
69 conf_syslog = conf_stdout = (unsigned int) -1;
70 }
71
72 switch (module) {
73 case HOSTAPD_MODULE_IEEE80211:
74 module_str = "IEEE 802.11";
75 break;
76 case HOSTAPD_MODULE_IEEE8021X:
77 module_str = "IEEE 802.1X";
78 break;
79 case HOSTAPD_MODULE_RADIUS:
80 module_str = "RADIUS";
81 break;
82 case HOSTAPD_MODULE_WPA:
83 module_str = "WPA";
84 break;
85 case HOSTAPD_MODULE_DRIVER:
86 module_str = "DRIVER";
87 break;
88 case HOSTAPD_MODULE_IAPP:
89 module_str = "IAPP";
90 break;
91 case HOSTAPD_MODULE_MLME:
92 module_str = "MLME";
93 break;
94 default:
95 module_str = NULL;
96 break;
97 }
98
99 if (hapd && hapd->conf && addr)
100 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
101 hapd->conf->iface, MAC2STR(addr),
102 module_str ? " " : "", module_str, txt);
103 else if (hapd && hapd->conf)
104 os_snprintf(format, maxlen, "%s:%s%s %s",
105 hapd->conf->iface, module_str ? " " : "",
106 module_str, txt);
107 else if (addr)
108 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
109 MAC2STR(addr), module_str ? " " : "",
110 module_str, txt);
111 else
112 os_snprintf(format, maxlen, "%s%s%s",
113 module_str, module_str ? ": " : "", txt);
114
115 if ((conf_stdout & module) && level >= conf_stdout_level) {
116 wpa_debug_print_timestamp();
117 printf("%s\n", format);
118 }
119
120 #ifndef CONFIG_NATIVE_WINDOWS
121 if ((conf_syslog & module) && level >= conf_syslog_level) {
122 int priority;
123 switch (level) {
124 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
125 case HOSTAPD_LEVEL_DEBUG:
126 priority = LOG_DEBUG;
127 break;
128 case HOSTAPD_LEVEL_INFO:
129 priority = LOG_INFO;
130 break;
131 case HOSTAPD_LEVEL_NOTICE:
132 priority = LOG_NOTICE;
133 break;
134 case HOSTAPD_LEVEL_WARNING:
135 priority = LOG_WARNING;
136 break;
137 default:
138 priority = LOG_INFO;
139 break;
140 }
141 syslog(priority, "%s", format);
142 }
143 #endif /* CONFIG_NATIVE_WINDOWS */
144
145 os_free(format);
146 }
147 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
148
149
150 static int hostapd_driver_init(struct hostapd_iface *iface)
151 {
152 struct wpa_init_params params;
153 size_t i;
154 struct hostapd_data *hapd = iface->bss[0];
155 struct hostapd_bss_config *conf = hapd->conf;
156 u8 *b = conf->bssid;
157 struct wpa_driver_capa capa;
158
159 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
160 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
161 return -1;
162 }
163
164 /* Initialize the driver interface */
165 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
166 b = NULL;
167
168 os_memset(&params, 0, sizeof(params));
169 for (i = 0; wpa_drivers[i]; i++) {
170 if (wpa_drivers[i] != hapd->driver)
171 continue;
172
173 if (global.drv_priv[i] == NULL &&
174 wpa_drivers[i]->global_init) {
175 global.drv_priv[i] = wpa_drivers[i]->global_init();
176 if (global.drv_priv[i] == NULL) {
177 wpa_printf(MSG_ERROR, "Failed to initialize "
178 "driver '%s'",
179 wpa_drivers[i]->name);
180 return -1;
181 }
182 }
183
184 params.global_priv = global.drv_priv[i];
185 break;
186 }
187 params.bssid = b;
188 params.ifname = hapd->conf->iface;
189 params.ssid = hapd->conf->ssid.ssid;
190 params.ssid_len = hapd->conf->ssid.ssid_len;
191 params.test_socket = hapd->conf->test_socket;
192 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
193
194 params.num_bridge = hapd->iface->num_bss;
195 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
196 if (params.bridge == NULL)
197 return -1;
198 for (i = 0; i < hapd->iface->num_bss; i++) {
199 struct hostapd_data *bss = hapd->iface->bss[i];
200 if (bss->conf->bridge[0])
201 params.bridge[i] = bss->conf->bridge;
202 }
203
204 params.own_addr = hapd->own_addr;
205
206 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
207 os_free(params.bridge);
208 if (hapd->drv_priv == NULL) {
209 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
210 hapd->driver->name);
211 hapd->driver = NULL;
212 return -1;
213 }
214
215 if (hapd->driver->get_capa &&
216 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
217 iface->drv_flags = capa.flags;
218 iface->probe_resp_offloads = capa.probe_resp_offloads;
219 iface->extended_capa = capa.extended_capa;
220 iface->extended_capa_mask = capa.extended_capa_mask;
221 iface->extended_capa_len = capa.extended_capa_len;
222 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
223 }
224
225 #ifdef CONFIG_INTERWORKING
226 if (hapd->driver->set_qos_map && conf->qos_map_set_len &&
227 hapd->driver->set_qos_map(hapd->drv_priv, conf->qos_map_set,
228 conf->qos_map_set_len)) {
229 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map.");
230 return -1;
231 }
232 #endif /* CONFIG_INTERWORKING */
233
234 return 0;
235 }
236
237
238 static struct hostapd_iface *
239 hostapd_interface_init(struct hapd_interfaces *interfaces,
240 const char *config_fname, int debug)
241 {
242 struct hostapd_iface *iface;
243 int k;
244
245 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
246 iface = hostapd_init(interfaces, config_fname);
247 if (!iface)
248 return NULL;
249 iface->interfaces = interfaces;
250
251 for (k = 0; k < debug; k++) {
252 if (iface->bss[0]->conf->logger_stdout_level > 0)
253 iface->bss[0]->conf->logger_stdout_level--;
254 }
255
256 if (iface->conf->bss[0]->iface[0] == '\0' &&
257 !hostapd_drv_none(iface->bss[0])) {
258 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
259 config_fname);
260 hostapd_interface_deinit_free(iface);
261 return NULL;
262 }
263
264 if (hostapd_driver_init(iface) ||
265 hostapd_setup_interface(iface)) {
266 hostapd_interface_deinit_free(iface);
267 return NULL;
268 }
269
270 iface->init_done = 1;
271
272 return iface;
273 }
274
275
276 static int hostapd_interface_init2(struct hostapd_iface *iface)
277 {
278 if (iface->init_done)
279 return 0;
280
281 if (hostapd_driver_init(iface) ||
282 hostapd_setup_interface(iface))
283 return -1;
284 iface->init_done = 1;
285
286 return 0;
287 }
288
289
290 /**
291 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
292 */
293 static void handle_term(int sig, void *signal_ctx)
294 {
295 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
296 eloop_terminate();
297 }
298
299
300 #ifndef CONFIG_NATIVE_WINDOWS
301
302 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
303 {
304 if (hostapd_reload_config(iface) < 0) {
305 wpa_printf(MSG_WARNING, "Failed to read new configuration "
306 "file - continuing with old.");
307 }
308 return 0;
309 }
310
311
312 /**
313 * handle_reload - SIGHUP handler to reload configuration
314 */
315 static void handle_reload(int sig, void *signal_ctx)
316 {
317 struct hapd_interfaces *interfaces = signal_ctx;
318 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
319 sig);
320 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
321 }
322
323
324 static void handle_dump_state(int sig, void *signal_ctx)
325 {
326 #ifdef HOSTAPD_DUMP_STATE
327 struct hapd_interfaces *interfaces = signal_ctx;
328 hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
329 #endif /* HOSTAPD_DUMP_STATE */
330 }
331 #endif /* CONFIG_NATIVE_WINDOWS */
332
333
334 static int hostapd_global_init(struct hapd_interfaces *interfaces,
335 const char *entropy_file)
336 {
337 int i;
338
339 os_memset(&global, 0, sizeof(global));
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 random_init(entropy_file);
354
355 #ifndef CONFIG_NATIVE_WINDOWS
356 eloop_register_signal(SIGHUP, handle_reload, interfaces);
357 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
358 #endif /* CONFIG_NATIVE_WINDOWS */
359 eloop_register_signal_terminate(handle_term, interfaces);
360
361 #ifndef CONFIG_NATIVE_WINDOWS
362 openlog("hostapd", 0, LOG_DAEMON);
363 #endif /* CONFIG_NATIVE_WINDOWS */
364
365 for (i = 0; wpa_drivers[i]; i++)
366 global.drv_count++;
367 if (global.drv_count == 0) {
368 wpa_printf(MSG_ERROR, "No drivers enabled");
369 return -1;
370 }
371 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
372 if (global.drv_priv == NULL)
373 return -1;
374
375 return 0;
376 }
377
378
379 static void hostapd_global_deinit(const char *pid_file)
380 {
381 int i;
382
383 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
384 if (!global.drv_priv[i])
385 continue;
386 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
387 }
388 os_free(global.drv_priv);
389 global.drv_priv = NULL;
390
391 #ifdef EAP_SERVER_TNC
392 tncs_global_deinit();
393 #endif /* EAP_SERVER_TNC */
394
395 random_deinit();
396
397 eloop_destroy();
398
399 #ifndef CONFIG_NATIVE_WINDOWS
400 closelog();
401 #endif /* CONFIG_NATIVE_WINDOWS */
402
403 eap_server_unregister_methods();
404
405 os_daemonize_terminate(pid_file);
406 }
407
408
409 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
410 const char *pid_file)
411 {
412 #ifdef EAP_SERVER_TNC
413 int tnc = 0;
414 size_t i, k;
415
416 for (i = 0; !tnc && i < ifaces->count; i++) {
417 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
418 if (ifaces->iface[i]->bss[0]->conf->tnc) {
419 tnc++;
420 break;
421 }
422 }
423 }
424
425 if (tnc && tncs_global_init() < 0) {
426 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
427 return -1;
428 }
429 #endif /* EAP_SERVER_TNC */
430
431 if (daemonize && os_daemonize(pid_file)) {
432 perror("daemon");
433 return -1;
434 }
435
436 eloop_run();
437
438 return 0;
439 }
440
441
442 static void show_version(void)
443 {
444 fprintf(stderr,
445 "hostapd v" VERSION_STR "\n"
446 "User space daemon for IEEE 802.11 AP management,\n"
447 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
448 "Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> "
449 "and contributors\n");
450 }
451
452
453 static void usage(void)
454 {
455 show_version();
456 fprintf(stderr,
457 "\n"
458 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
459 "\\\n"
460 " [-g <global ctrl_iface>] [-G <group>] \\\n"
461 " <configuration file(s)>\n"
462 "\n"
463 "options:\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"
467 " -e entropy file\n"
468 " -g global control interface path\n"
469 " -G group for control interfaces\n"
470 " -P PID file\n"
471 " -K include key data in debug messages\n"
472 #ifdef CONFIG_DEBUG_FILE
473 " -f log output to debug file instead of stdout\n"
474 #endif /* CONFIG_DEBUG_FILE */
475 #ifdef CONFIG_DEBUG_LINUX_TRACING
476 " -T = record to Linux tracing in addition to logging\n"
477 " (records all messages regardless of debug verbosity)\n"
478 #endif /* CONFIG_DEBUG_LINUX_TRACING */
479 " -t include timestamps in some debug messages\n"
480 " -v show hostapd version\n");
481
482 exit(1);
483 }
484
485
486 static const char * hostapd_msg_ifname_cb(void *ctx)
487 {
488 struct hostapd_data *hapd = ctx;
489 if (hapd && hapd->iconf && hapd->iconf->bss &&
490 hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
491 return hapd->iconf->bss[0]->iface;
492 return NULL;
493 }
494
495
496 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
497 const char *path)
498 {
499 char *pos;
500 os_free(interfaces->global_iface_path);
501 interfaces->global_iface_path = os_strdup(path);
502 if (interfaces->global_iface_path == NULL)
503 return -1;
504 pos = os_strrchr(interfaces->global_iface_path, '/');
505 if (pos == NULL) {
506 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
507 "file");
508 os_free(interfaces->global_iface_path);
509 interfaces->global_iface_path = NULL;
510 return -1;
511 }
512
513 *pos = '\0';
514 interfaces->global_iface_name = pos + 1;
515
516 return 0;
517 }
518
519
520 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
521 const char *group)
522 {
523 #ifndef CONFIG_NATIVE_WINDOWS
524 struct group *grp;
525 grp = getgrnam(group);
526 if (grp == NULL) {
527 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
528 return -1;
529 }
530 interfaces->ctrl_iface_group = grp->gr_gid;
531 #endif /* CONFIG_NATIVE_WINDOWS */
532 return 0;
533 }
534
535
536 int main(int argc, char *argv[])
537 {
538 struct hapd_interfaces interfaces;
539 int ret = 1;
540 size_t i, j;
541 int c, debug = 0, daemonize = 0;
542 char *pid_file = NULL;
543 const char *log_file = NULL;
544 const char *entropy_file = NULL;
545 char **bss_config = NULL, **tmp_bss;
546 size_t num_bss_configs = 0;
547 #ifdef CONFIG_DEBUG_LINUX_TRACING
548 int enable_trace_dbg = 0;
549 #endif /* CONFIG_DEBUG_LINUX_TRACING */
550
551 if (os_program_init())
552 return -1;
553
554 os_memset(&interfaces, 0, sizeof(interfaces));
555 interfaces.reload_config = hostapd_reload_config;
556 interfaces.config_read_cb = hostapd_config_read;
557 interfaces.for_each_interface = hostapd_for_each_interface;
558 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
559 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
560 interfaces.driver_init = hostapd_driver_init;
561 interfaces.global_iface_path = NULL;
562 interfaces.global_iface_name = NULL;
563 interfaces.global_ctrl_sock = -1;
564
565 for (;;) {
566 c = getopt(argc, argv, "b:Bde:f:hKP:Ttvg:G:");
567 if (c < 0)
568 break;
569 switch (c) {
570 case 'h':
571 usage();
572 break;
573 case 'd':
574 debug++;
575 if (wpa_debug_level > 0)
576 wpa_debug_level--;
577 break;
578 case 'B':
579 daemonize++;
580 break;
581 case 'e':
582 entropy_file = optarg;
583 break;
584 case 'f':
585 log_file = optarg;
586 break;
587 case 'K':
588 wpa_debug_show_keys++;
589 break;
590 case 'P':
591 os_free(pid_file);
592 pid_file = os_rel2abs_path(optarg);
593 break;
594 case 't':
595 wpa_debug_timestamp++;
596 break;
597 #ifdef CONFIG_DEBUG_LINUX_TRACING
598 case 'T':
599 enable_trace_dbg = 1;
600 break;
601 #endif /* CONFIG_DEBUG_LINUX_TRACING */
602 case 'v':
603 show_version();
604 exit(1);
605 break;
606 case 'g':
607 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
608 return -1;
609 break;
610 case 'G':
611 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
612 return -1;
613 break;
614 case 'b':
615 tmp_bss = os_realloc_array(bss_config,
616 num_bss_configs + 1,
617 sizeof(char *));
618 if (tmp_bss == NULL)
619 goto out;
620 bss_config = tmp_bss;
621 bss_config[num_bss_configs++] = optarg;
622 break;
623 default:
624 usage();
625 break;
626 }
627 }
628
629 if (optind == argc && interfaces.global_iface_path == NULL &&
630 num_bss_configs == 0)
631 usage();
632
633 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
634
635 if (log_file)
636 wpa_debug_open_file(log_file);
637 #ifdef CONFIG_DEBUG_LINUX_TRACING
638 if (enable_trace_dbg) {
639 int tret = wpa_debug_open_linux_tracing();
640 if (tret) {
641 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
642 return -1;
643 }
644 }
645 #endif /* CONFIG_DEBUG_LINUX_TRACING */
646
647 interfaces.count = argc - optind;
648 if (interfaces.count || num_bss_configs) {
649 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
650 sizeof(struct hostapd_iface *));
651 if (interfaces.iface == NULL) {
652 wpa_printf(MSG_ERROR, "malloc failed");
653 return -1;
654 }
655 }
656
657 if (hostapd_global_init(&interfaces, entropy_file)) {
658 wpa_printf(MSG_ERROR, "Failed to initilize global context");
659 return -1;
660 }
661
662 /* Initialize interfaces */
663 for (i = 0; i < interfaces.count; i++) {
664 interfaces.iface[i] = hostapd_interface_init(&interfaces,
665 argv[optind + i],
666 debug);
667 if (!interfaces.iface[i]) {
668 wpa_printf(MSG_ERROR, "Failed to initialize interface");
669 goto out;
670 }
671 }
672
673 for (i = 0; i < num_bss_configs; i++) {
674 struct hostapd_iface *iface;
675 char *fname;
676
677 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
678 fname = os_strchr(bss_config[i], ':');
679 if (fname == NULL) {
680 wpa_printf(MSG_ERROR,
681 "Invalid BSS config identifier '%s'",
682 bss_config[i]);
683 goto out;
684 }
685 *fname++ = '\0';
686 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
687 fname, debug);
688 if (iface == NULL)
689 goto out;
690 for (j = 0; j < interfaces.count; j++) {
691 if (interfaces.iface[j] == iface)
692 break;
693 }
694 if (j == interfaces.count) {
695 struct hostapd_iface **tmp;
696 tmp = os_realloc_array(interfaces.iface,
697 interfaces.count + 1,
698 sizeof(struct hostapd_iface *));
699 if (tmp == NULL) {
700 hostapd_interface_deinit_free(iface);
701 goto out;
702 }
703 interfaces.iface = tmp;
704 interfaces.iface[interfaces.count++] = iface;
705 }
706 }
707
708 for (i = 0; i < interfaces.count; i++) {
709 if (hostapd_interface_init2(interfaces.iface[i]) < 0)
710 goto out;
711 }
712
713 hostapd_global_ctrl_iface_init(&interfaces);
714
715 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
716 wpa_printf(MSG_ERROR, "Failed to start eloop");
717 goto out;
718 }
719
720 ret = 0;
721
722 out:
723 hostapd_global_ctrl_iface_deinit(&interfaces);
724 /* Deinitialize all interfaces */
725 for (i = 0; i < interfaces.count; i++)
726 hostapd_interface_deinit_free(interfaces.iface[i]);
727 os_free(interfaces.iface);
728
729 hostapd_global_deinit(pid_file);
730 os_free(pid_file);
731
732 if (log_file)
733 wpa_debug_close_file();
734 wpa_debug_close_linux_tracing();
735
736 os_free(bss_config);
737
738 os_program_deinit();
739
740 return ret;
741 }