]> git.ipfire.org Git - thirdparty/hostap.git/blob - hostapd/main.c
hostapd: Allow per-BSS (vif) configuration files
[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 /**
151 * hostapd_init - Allocate and initialize per-interface data
152 * @config_file: Path to the configuration file
153 * Returns: Pointer to the allocated interface data or %NULL on failure
154 *
155 * This function is used to allocate main data structures for per-interface
156 * data. The allocated data buffer will be freed by calling
157 * hostapd_cleanup_iface().
158 */
159 static struct hostapd_iface * hostapd_init(const char *config_file)
160 {
161 struct hostapd_iface *hapd_iface = NULL;
162 struct hostapd_config *conf = NULL;
163 struct hostapd_data *hapd;
164 size_t i;
165
166 hapd_iface = os_zalloc(sizeof(*hapd_iface));
167 if (hapd_iface == NULL)
168 goto fail;
169
170 hapd_iface->config_fname = os_strdup(config_file);
171 if (hapd_iface->config_fname == NULL)
172 goto fail;
173
174 conf = hostapd_config_read(hapd_iface->config_fname);
175 if (conf == NULL)
176 goto fail;
177 hapd_iface->conf = conf;
178
179 hapd_iface->num_bss = conf->num_bss;
180 hapd_iface->bss = os_calloc(conf->num_bss,
181 sizeof(struct hostapd_data *));
182 if (hapd_iface->bss == NULL)
183 goto fail;
184
185 for (i = 0; i < conf->num_bss; i++) {
186 hapd = hapd_iface->bss[i] =
187 hostapd_alloc_bss_data(hapd_iface, conf,
188 conf->bss[i]);
189 if (hapd == NULL)
190 goto fail;
191 hapd->msg_ctx = hapd;
192 }
193
194 return hapd_iface;
195
196 fail:
197 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
198 config_file);
199 if (conf)
200 hostapd_config_free(conf);
201 if (hapd_iface) {
202 os_free(hapd_iface->config_fname);
203 os_free(hapd_iface->bss);
204 os_free(hapd_iface);
205 }
206 return NULL;
207 }
208
209
210 static int hostapd_driver_init(struct hostapd_iface *iface)
211 {
212 struct wpa_init_params params;
213 size_t i;
214 struct hostapd_data *hapd = iface->bss[0];
215 struct hostapd_bss_config *conf = hapd->conf;
216 u8 *b = conf->bssid;
217 struct wpa_driver_capa capa;
218
219 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
220 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
221 return -1;
222 }
223
224 /* Initialize the driver interface */
225 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
226 b = NULL;
227
228 os_memset(&params, 0, sizeof(params));
229 for (i = 0; wpa_drivers[i]; i++) {
230 if (wpa_drivers[i] != hapd->driver)
231 continue;
232
233 if (global.drv_priv[i] == NULL &&
234 wpa_drivers[i]->global_init) {
235 global.drv_priv[i] = wpa_drivers[i]->global_init();
236 if (global.drv_priv[i] == NULL) {
237 wpa_printf(MSG_ERROR, "Failed to initialize "
238 "driver '%s'",
239 wpa_drivers[i]->name);
240 return -1;
241 }
242 }
243
244 params.global_priv = global.drv_priv[i];
245 break;
246 }
247 params.bssid = b;
248 params.ifname = hapd->conf->iface;
249 params.ssid = hapd->conf->ssid.ssid;
250 params.ssid_len = hapd->conf->ssid.ssid_len;
251 params.test_socket = hapd->conf->test_socket;
252 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
253
254 params.num_bridge = hapd->iface->num_bss;
255 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
256 if (params.bridge == NULL)
257 return -1;
258 for (i = 0; i < hapd->iface->num_bss; i++) {
259 struct hostapd_data *bss = hapd->iface->bss[i];
260 if (bss->conf->bridge[0])
261 params.bridge[i] = bss->conf->bridge;
262 }
263
264 params.own_addr = hapd->own_addr;
265
266 hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
267 os_free(params.bridge);
268 if (hapd->drv_priv == NULL) {
269 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
270 hapd->driver->name);
271 hapd->driver = NULL;
272 return -1;
273 }
274
275 if (hapd->driver->get_capa &&
276 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
277 iface->drv_flags = capa.flags;
278 iface->probe_resp_offloads = capa.probe_resp_offloads;
279 iface->extended_capa = capa.extended_capa;
280 iface->extended_capa_mask = capa.extended_capa_mask;
281 iface->extended_capa_len = capa.extended_capa_len;
282 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
283 }
284
285 #ifdef CONFIG_INTERWORKING
286 if (hapd->driver->set_qos_map && conf->qos_map_set_len &&
287 hapd->driver->set_qos_map(hapd->drv_priv, conf->qos_map_set,
288 conf->qos_map_set_len)) {
289 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map.");
290 return -1;
291 }
292 #endif /* CONFIG_INTERWORKING */
293
294 return 0;
295 }
296
297
298 static struct hostapd_iface *
299 hostapd_interface_init(struct hapd_interfaces *interfaces,
300 const char *config_fname, int debug)
301 {
302 struct hostapd_iface *iface;
303 int k;
304
305 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
306 iface = hostapd_init(config_fname);
307 if (!iface)
308 return NULL;
309 iface->interfaces = interfaces;
310
311 for (k = 0; k < debug; k++) {
312 if (iface->bss[0]->conf->logger_stdout_level > 0)
313 iface->bss[0]->conf->logger_stdout_level--;
314 }
315
316 if (iface->conf->bss[0]->iface[0] == '\0' &&
317 !hostapd_drv_none(iface->bss[0])) {
318 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
319 config_fname);
320 hostapd_interface_deinit_free(iface);
321 return NULL;
322 }
323
324 if (hostapd_driver_init(iface) ||
325 hostapd_setup_interface(iface)) {
326 hostapd_interface_deinit_free(iface);
327 return NULL;
328 }
329
330 iface->init_done = 1;
331
332 return iface;
333 }
334
335
336 static struct hostapd_iface *
337 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
338 const char *config_fname, int debug)
339 {
340 struct hostapd_iface *new_iface = NULL, *iface = NULL;
341 struct hostapd_data *hapd;
342 int k;
343 size_t i, bss_idx;
344
345 if (!phy || !*phy)
346 return NULL;
347
348 for (i = 0; i < interfaces->count; i++) {
349 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
350 iface = interfaces->iface[i];
351 break;
352 }
353 }
354
355 wpa_printf(MSG_ERROR, "Configuration file: %s (phy %s)%s",
356 config_fname, phy, iface ? "" : " --> new PHY");
357 if (iface) {
358 struct hostapd_config *conf;
359 struct hostapd_bss_config **tmp_conf;
360 struct hostapd_data **tmp_bss;
361 struct hostapd_bss_config *bss;
362
363 /* Add new BSS to existing iface */
364 conf = hostapd_config_read(config_fname);
365 if (conf == NULL)
366 return NULL;
367 if (conf->num_bss > 1) {
368 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
369 hostapd_config_free(conf);
370 return NULL;
371 }
372
373 tmp_conf = os_realloc_array(
374 iface->conf->bss, iface->conf->num_bss + 1,
375 sizeof(struct hostapd_bss_config *));
376 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
377 sizeof(struct hostapd_data *));
378 if (tmp_bss)
379 iface->bss = tmp_bss;
380 if (tmp_conf) {
381 iface->conf->bss = tmp_conf;
382 iface->conf->last_bss = tmp_conf[0];
383 }
384 if (tmp_bss == NULL || tmp_conf == NULL) {
385 hostapd_config_free(conf);
386 return NULL;
387 }
388 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
389 iface->conf->num_bss++;
390
391 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
392 if (hapd == NULL) {
393 iface->conf->num_bss--;
394 hostapd_config_free(conf);
395 return NULL;
396 }
397 iface->conf->last_bss = bss;
398 iface->bss[iface->num_bss] = hapd;
399 hapd->msg_ctx = hapd;
400
401 bss_idx = iface->num_bss++;
402 conf->num_bss--;
403 conf->bss[0] = NULL;
404 hostapd_config_free(conf);
405 } else {
406 /* Add a new iface with the first BSS */
407 new_iface = iface = hostapd_init(config_fname);
408 if (!iface)
409 return NULL;
410 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
411 iface->interfaces = interfaces;
412 bss_idx = 0;
413 }
414
415 for (k = 0; k < debug; k++) {
416 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
417 iface->bss[bss_idx]->conf->logger_stdout_level--;
418 }
419
420 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
421 !hostapd_drv_none(iface->bss[bss_idx])) {
422 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
423 config_fname);
424 if (new_iface)
425 hostapd_interface_deinit_free(new_iface);
426 return NULL;
427 }
428
429 return iface;
430 }
431
432
433 static int hostapd_interface_init2(struct hostapd_iface *iface)
434 {
435 if (iface->init_done)
436 return 0;
437
438 if (hostapd_driver_init(iface) ||
439 hostapd_setup_interface(iface))
440 return -1;
441 iface->init_done = 1;
442
443 return 0;
444 }
445
446
447 /**
448 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
449 */
450 static void handle_term(int sig, void *signal_ctx)
451 {
452 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
453 eloop_terminate();
454 }
455
456
457 #ifndef CONFIG_NATIVE_WINDOWS
458
459 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
460 {
461 if (hostapd_reload_config(iface) < 0) {
462 wpa_printf(MSG_WARNING, "Failed to read new configuration "
463 "file - continuing with old.");
464 }
465 return 0;
466 }
467
468
469 /**
470 * handle_reload - SIGHUP handler to reload configuration
471 */
472 static void handle_reload(int sig, void *signal_ctx)
473 {
474 struct hapd_interfaces *interfaces = signal_ctx;
475 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
476 sig);
477 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
478 }
479
480
481 static void handle_dump_state(int sig, void *signal_ctx)
482 {
483 #ifdef HOSTAPD_DUMP_STATE
484 struct hapd_interfaces *interfaces = signal_ctx;
485 hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
486 #endif /* HOSTAPD_DUMP_STATE */
487 }
488 #endif /* CONFIG_NATIVE_WINDOWS */
489
490
491 static int hostapd_global_init(struct hapd_interfaces *interfaces,
492 const char *entropy_file)
493 {
494 int i;
495
496 os_memset(&global, 0, sizeof(global));
497
498 hostapd_logger_register_cb(hostapd_logger_cb);
499
500 if (eap_server_register_methods()) {
501 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
502 return -1;
503 }
504
505 if (eloop_init()) {
506 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
507 return -1;
508 }
509
510 random_init(entropy_file);
511
512 #ifndef CONFIG_NATIVE_WINDOWS
513 eloop_register_signal(SIGHUP, handle_reload, interfaces);
514 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
515 #endif /* CONFIG_NATIVE_WINDOWS */
516 eloop_register_signal_terminate(handle_term, interfaces);
517
518 #ifndef CONFIG_NATIVE_WINDOWS
519 openlog("hostapd", 0, LOG_DAEMON);
520 #endif /* CONFIG_NATIVE_WINDOWS */
521
522 for (i = 0; wpa_drivers[i]; i++)
523 global.drv_count++;
524 if (global.drv_count == 0) {
525 wpa_printf(MSG_ERROR, "No drivers enabled");
526 return -1;
527 }
528 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
529 if (global.drv_priv == NULL)
530 return -1;
531
532 return 0;
533 }
534
535
536 static void hostapd_global_deinit(const char *pid_file)
537 {
538 int i;
539
540 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
541 if (!global.drv_priv[i])
542 continue;
543 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
544 }
545 os_free(global.drv_priv);
546 global.drv_priv = NULL;
547
548 #ifdef EAP_SERVER_TNC
549 tncs_global_deinit();
550 #endif /* EAP_SERVER_TNC */
551
552 random_deinit();
553
554 eloop_destroy();
555
556 #ifndef CONFIG_NATIVE_WINDOWS
557 closelog();
558 #endif /* CONFIG_NATIVE_WINDOWS */
559
560 eap_server_unregister_methods();
561
562 os_daemonize_terminate(pid_file);
563 }
564
565
566 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
567 const char *pid_file)
568 {
569 #ifdef EAP_SERVER_TNC
570 int tnc = 0;
571 size_t i, k;
572
573 for (i = 0; !tnc && i < ifaces->count; i++) {
574 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
575 if (ifaces->iface[i]->bss[0]->conf->tnc) {
576 tnc++;
577 break;
578 }
579 }
580 }
581
582 if (tnc && tncs_global_init() < 0) {
583 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
584 return -1;
585 }
586 #endif /* EAP_SERVER_TNC */
587
588 if (daemonize && os_daemonize(pid_file)) {
589 perror("daemon");
590 return -1;
591 }
592
593 eloop_run();
594
595 return 0;
596 }
597
598
599 static void show_version(void)
600 {
601 fprintf(stderr,
602 "hostapd v" VERSION_STR "\n"
603 "User space daemon for IEEE 802.11 AP management,\n"
604 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
605 "Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> "
606 "and contributors\n");
607 }
608
609
610 static void usage(void)
611 {
612 show_version();
613 fprintf(stderr,
614 "\n"
615 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
616 "\\\n"
617 " [-g <global ctrl_iface>] [-G <group>] \\\n"
618 " <configuration file(s)>\n"
619 "\n"
620 "options:\n"
621 " -h show this usage\n"
622 " -d show more debug messages (-dd for even more)\n"
623 " -B run daemon in the background\n"
624 " -e entropy file\n"
625 " -g global control interface path\n"
626 " -G group for control interfaces\n"
627 " -P PID file\n"
628 " -K include key data in debug messages\n"
629 #ifdef CONFIG_DEBUG_FILE
630 " -f log output to debug file instead of stdout\n"
631 #endif /* CONFIG_DEBUG_FILE */
632 " -t include timestamps in some debug messages\n"
633 " -v show hostapd version\n");
634
635 exit(1);
636 }
637
638
639 static const char * hostapd_msg_ifname_cb(void *ctx)
640 {
641 struct hostapd_data *hapd = ctx;
642 if (hapd && hapd->iconf && hapd->iconf->bss &&
643 hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
644 return hapd->iconf->bss[0]->iface;
645 return NULL;
646 }
647
648
649 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
650 const char *path)
651 {
652 char *pos;
653 os_free(interfaces->global_iface_path);
654 interfaces->global_iface_path = os_strdup(path);
655 if (interfaces->global_iface_path == NULL)
656 return -1;
657 pos = os_strrchr(interfaces->global_iface_path, '/');
658 if (pos == NULL) {
659 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
660 "file");
661 os_free(interfaces->global_iface_path);
662 interfaces->global_iface_path = NULL;
663 return -1;
664 }
665
666 *pos = '\0';
667 interfaces->global_iface_name = pos + 1;
668
669 return 0;
670 }
671
672
673 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
674 const char *group)
675 {
676 #ifndef CONFIG_NATIVE_WINDOWS
677 struct group *grp;
678 grp = getgrnam(group);
679 if (grp == NULL) {
680 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
681 return -1;
682 }
683 interfaces->ctrl_iface_group = grp->gr_gid;
684 #endif /* CONFIG_NATIVE_WINDOWS */
685 return 0;
686 }
687
688
689 int main(int argc, char *argv[])
690 {
691 struct hapd_interfaces interfaces;
692 int ret = 1;
693 size_t i, j;
694 int c, debug = 0, daemonize = 0;
695 char *pid_file = NULL;
696 const char *log_file = NULL;
697 const char *entropy_file = NULL;
698 char **bss_config = NULL, **tmp_bss;
699 size_t num_bss_configs = 0;
700
701 if (os_program_init())
702 return -1;
703
704 os_memset(&interfaces, 0, sizeof(interfaces));
705 interfaces.reload_config = hostapd_reload_config;
706 interfaces.config_read_cb = hostapd_config_read;
707 interfaces.for_each_interface = hostapd_for_each_interface;
708 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
709 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
710 interfaces.driver_init = hostapd_driver_init;
711 interfaces.global_iface_path = NULL;
712 interfaces.global_iface_name = NULL;
713 interfaces.global_ctrl_sock = -1;
714
715 for (;;) {
716 c = getopt(argc, argv, "b:Bde:f:hKP:tvg:G:");
717 if (c < 0)
718 break;
719 switch (c) {
720 case 'h':
721 usage();
722 break;
723 case 'd':
724 debug++;
725 if (wpa_debug_level > 0)
726 wpa_debug_level--;
727 break;
728 case 'B':
729 daemonize++;
730 break;
731 case 'e':
732 entropy_file = optarg;
733 break;
734 case 'f':
735 log_file = optarg;
736 break;
737 case 'K':
738 wpa_debug_show_keys++;
739 break;
740 case 'P':
741 os_free(pid_file);
742 pid_file = os_rel2abs_path(optarg);
743 break;
744 case 't':
745 wpa_debug_timestamp++;
746 break;
747 case 'v':
748 show_version();
749 exit(1);
750 break;
751 case 'g':
752 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
753 return -1;
754 break;
755 case 'G':
756 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
757 return -1;
758 break;
759 case 'b':
760 tmp_bss = os_realloc_array(bss_config,
761 num_bss_configs + 1,
762 sizeof(char *));
763 if (tmp_bss == NULL)
764 goto out;
765 bss_config = tmp_bss;
766 bss_config[num_bss_configs++] = optarg;
767 break;
768 default:
769 usage();
770 break;
771 }
772 }
773
774 if (optind == argc && interfaces.global_iface_path == NULL &&
775 num_bss_configs == 0)
776 usage();
777
778 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
779
780 if (log_file)
781 wpa_debug_open_file(log_file);
782
783 interfaces.count = argc - optind;
784 if (interfaces.count || num_bss_configs) {
785 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
786 sizeof(struct hostapd_iface *));
787 if (interfaces.iface == NULL) {
788 wpa_printf(MSG_ERROR, "malloc failed");
789 return -1;
790 }
791 }
792
793 if (hostapd_global_init(&interfaces, entropy_file)) {
794 wpa_printf(MSG_ERROR, "Failed to initilize global context");
795 return -1;
796 }
797
798 /* Initialize interfaces */
799 for (i = 0; i < interfaces.count; i++) {
800 interfaces.iface[i] = hostapd_interface_init(&interfaces,
801 argv[optind + i],
802 debug);
803 if (!interfaces.iface[i]) {
804 wpa_printf(MSG_ERROR, "Failed to initialize interface");
805 goto out;
806 }
807 }
808
809 for (i = 0; i < num_bss_configs; i++) {
810 struct hostapd_iface *iface;
811 char *fname;
812
813 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
814 fname = os_strchr(bss_config[i], ':');
815 if (fname == NULL) {
816 wpa_printf(MSG_ERROR,
817 "Invalid BSS config identifier '%s'",
818 bss_config[i]);
819 goto out;
820 }
821 *fname++ = '\0';
822 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
823 fname, debug);
824 if (iface == NULL)
825 goto out;
826 for (j = 0; j < interfaces.count; j++) {
827 if (interfaces.iface[j] == iface)
828 break;
829 }
830 if (j == interfaces.count) {
831 struct hostapd_iface **tmp;
832 tmp = os_realloc_array(interfaces.iface,
833 interfaces.count + 1,
834 sizeof(struct hostapd_iface *));
835 if (tmp == NULL) {
836 hostapd_interface_deinit_free(iface);
837 goto out;
838 }
839 interfaces.iface = tmp;
840 interfaces.iface[interfaces.count++] = iface;
841 }
842 }
843
844 for (i = 0; i < interfaces.count; i++) {
845 if (hostapd_interface_init2(interfaces.iface[i]) < 0)
846 goto out;
847 }
848
849 hostapd_global_ctrl_iface_init(&interfaces);
850
851 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
852 wpa_printf(MSG_ERROR, "Failed to start eloop");
853 goto out;
854 }
855
856 ret = 0;
857
858 out:
859 hostapd_global_ctrl_iface_deinit(&interfaces);
860 /* Deinitialize all interfaces */
861 for (i = 0; i < interfaces.count; i++)
862 hostapd_interface_deinit_free(interfaces.iface[i]);
863 os_free(interfaces.iface);
864
865 hostapd_global_deinit(pid_file);
866 os_free(pid_file);
867
868 if (log_file)
869 wpa_debug_close_file();
870
871 os_free(bss_config);
872
873 os_program_deinit();
874
875 return ret;
876 }