hostapd: Fix early init failure path
[mech_eap.git] / hostapd / main.c
1 /*
2  * hostapd / main()
3  * Copyright (c) 2002-2016, 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 "utils/uuid.h"
18 #include "crypto/random.h"
19 #include "crypto/tls.h"
20 #include "common/version.h"
21 #include "drivers/driver.h"
22 #include "eap_server/eap.h"
23 #include "eap_server/tncs.h"
24 #include "ap/hostapd.h"
25 #include "ap/ap_config.h"
26 #include "ap/ap_drv_ops.h"
27 #include "fst/fst.h"
28 #include "config_file.h"
29 #include "eap_register.h"
30 #include "ctrl_iface.h"
31
32
33 struct hapd_global {
34         void **drv_priv;
35         size_t drv_count;
36 };
37
38 static struct hapd_global global;
39
40
41 #ifndef CONFIG_NO_HOSTAPD_LOGGER
42 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
43                               int level, const char *txt, size_t len)
44 {
45         struct hostapd_data *hapd = ctx;
46         char *format, *module_str;
47         int maxlen;
48         int conf_syslog_level, conf_stdout_level;
49         unsigned int conf_syslog, conf_stdout;
50
51         maxlen = len + 100;
52         format = os_malloc(maxlen);
53         if (!format)
54                 return;
55
56         if (hapd && hapd->conf) {
57                 conf_syslog_level = hapd->conf->logger_syslog_level;
58                 conf_stdout_level = hapd->conf->logger_stdout_level;
59                 conf_syslog = hapd->conf->logger_syslog;
60                 conf_stdout = hapd->conf->logger_stdout;
61         } else {
62                 conf_syslog_level = conf_stdout_level = 0;
63                 conf_syslog = conf_stdout = (unsigned int) -1;
64         }
65
66         switch (module) {
67         case HOSTAPD_MODULE_IEEE80211:
68                 module_str = "IEEE 802.11";
69                 break;
70         case HOSTAPD_MODULE_IEEE8021X:
71                 module_str = "IEEE 802.1X";
72                 break;
73         case HOSTAPD_MODULE_RADIUS:
74                 module_str = "RADIUS";
75                 break;
76         case HOSTAPD_MODULE_WPA:
77                 module_str = "WPA";
78                 break;
79         case HOSTAPD_MODULE_DRIVER:
80                 module_str = "DRIVER";
81                 break;
82         case HOSTAPD_MODULE_IAPP:
83                 module_str = "IAPP";
84                 break;
85         case HOSTAPD_MODULE_MLME:
86                 module_str = "MLME";
87                 break;
88         default:
89                 module_str = NULL;
90                 break;
91         }
92
93         if (hapd && hapd->conf && addr)
94                 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
95                             hapd->conf->iface, MAC2STR(addr),
96                             module_str ? " " : "", module_str ? module_str : "",
97                             txt);
98         else if (hapd && hapd->conf)
99                 os_snprintf(format, maxlen, "%s:%s%s %s",
100                             hapd->conf->iface, module_str ? " " : "",
101                             module_str ? module_str : "", txt);
102         else if (addr)
103                 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
104                             MAC2STR(addr), module_str ? " " : "",
105                             module_str ? module_str : "", txt);
106         else
107                 os_snprintf(format, maxlen, "%s%s%s",
108                             module_str ? module_str : "",
109                             module_str ? ": " : "", txt);
110
111         if ((conf_stdout & module) && level >= conf_stdout_level) {
112                 wpa_debug_print_timestamp();
113                 wpa_printf(MSG_INFO, "%s", format);
114         }
115
116 #ifndef CONFIG_NATIVE_WINDOWS
117         if ((conf_syslog & module) && level >= conf_syslog_level) {
118                 int priority;
119                 switch (level) {
120                 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
121                 case HOSTAPD_LEVEL_DEBUG:
122                         priority = LOG_DEBUG;
123                         break;
124                 case HOSTAPD_LEVEL_INFO:
125                         priority = LOG_INFO;
126                         break;
127                 case HOSTAPD_LEVEL_NOTICE:
128                         priority = LOG_NOTICE;
129                         break;
130                 case HOSTAPD_LEVEL_WARNING:
131                         priority = LOG_WARNING;
132                         break;
133                 default:
134                         priority = LOG_INFO;
135                         break;
136                 }
137                 syslog(priority, "%s", format);
138         }
139 #endif /* CONFIG_NATIVE_WINDOWS */
140
141         os_free(format);
142 }
143 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
144
145
146 /**
147  * hostapd_driver_init - Preparate driver interface
148  */
149 static int hostapd_driver_init(struct hostapd_iface *iface)
150 {
151         struct wpa_init_params params;
152         size_t i;
153         struct hostapd_data *hapd = iface->bss[0];
154         struct hostapd_bss_config *conf = hapd->conf;
155         u8 *b = conf->bssid;
156         struct wpa_driver_capa capa;
157
158         if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
159                 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
160                 return -1;
161         }
162
163         /* Initialize the driver interface */
164         if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
165                 b = NULL;
166
167         os_memset(&params, 0, sizeof(params));
168         for (i = 0; wpa_drivers[i]; i++) {
169                 if (wpa_drivers[i] != hapd->driver)
170                         continue;
171
172                 if (global.drv_priv[i] == NULL &&
173                     wpa_drivers[i]->global_init) {
174                         global.drv_priv[i] =
175                                 wpa_drivers[i]->global_init(iface->interfaces);
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.driver_params = hapd->iconf->driver_params;
190         params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
191
192         params.num_bridge = hapd->iface->num_bss;
193         params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
194         if (params.bridge == NULL)
195                 return -1;
196         for (i = 0; i < hapd->iface->num_bss; i++) {
197                 struct hostapd_data *bss = hapd->iface->bss[i];
198                 if (bss->conf->bridge[0])
199                         params.bridge[i] = bss->conf->bridge;
200         }
201
202         params.own_addr = hapd->own_addr;
203
204         hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
205         os_free(params.bridge);
206         if (hapd->drv_priv == NULL) {
207                 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
208                            hapd->driver->name);
209                 hapd->driver = NULL;
210                 return -1;
211         }
212
213         if (hapd->driver->get_capa &&
214             hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
215                 struct wowlan_triggers *triggs;
216
217                 iface->drv_flags = capa.flags;
218                 iface->smps_modes = capa.smps_modes;
219                 iface->probe_resp_offloads = capa.probe_resp_offloads;
220                 /*
221                  * Use default extended capa values from per-radio information
222                  */
223                 iface->extended_capa = capa.extended_capa;
224                 iface->extended_capa_mask = capa.extended_capa_mask;
225                 iface->extended_capa_len = capa.extended_capa_len;
226                 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
227
228                 /*
229                  * Override extended capa with per-interface type (AP), if
230                  * available from the driver.
231                  */
232                 hostapd_get_ext_capa(iface);
233
234                 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
235                 if (triggs && hapd->driver->set_wowlan) {
236                         if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
237                                 wpa_printf(MSG_ERROR, "set_wowlan failed");
238                 }
239                 os_free(triggs);
240         }
241
242         return 0;
243 }
244
245
246 /**
247  * hostapd_interface_init - Read configuration file and init BSS data
248  *
249  * This function is used to parse configuration file for a full interface (one
250  * or more BSSes sharing the same radio) and allocate memory for the BSS
251  * interfaces. No actiual driver operations are started.
252  */
253 static struct hostapd_iface *
254 hostapd_interface_init(struct hapd_interfaces *interfaces,
255                        const char *config_fname, int debug)
256 {
257         struct hostapd_iface *iface;
258         int k;
259
260         wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
261         iface = hostapd_init(interfaces, config_fname);
262         if (!iface)
263                 return NULL;
264         iface->interfaces = interfaces;
265
266         for (k = 0; k < debug; k++) {
267                 if (iface->bss[0]->conf->logger_stdout_level > 0)
268                         iface->bss[0]->conf->logger_stdout_level--;
269         }
270
271         if (iface->conf->bss[0]->iface[0] == '\0' &&
272             !hostapd_drv_none(iface->bss[0])) {
273                 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
274                            config_fname);
275                 hostapd_interface_deinit_free(iface);
276                 return NULL;
277         }
278
279         return iface;
280 }
281
282
283 /**
284  * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
285  */
286 static void handle_term(int sig, void *signal_ctx)
287 {
288         wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
289         eloop_terminate();
290 }
291
292
293 #ifndef CONFIG_NATIVE_WINDOWS
294
295 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
296 {
297         if (hostapd_reload_config(iface) < 0) {
298                 wpa_printf(MSG_WARNING, "Failed to read new configuration "
299                            "file - continuing with old.");
300         }
301         return 0;
302 }
303
304
305 /**
306  * handle_reload - SIGHUP handler to reload configuration
307  */
308 static void handle_reload(int sig, void *signal_ctx)
309 {
310         struct hapd_interfaces *interfaces = signal_ctx;
311         wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
312                    sig);
313         hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
314 }
315
316
317 static void handle_dump_state(int sig, void *signal_ctx)
318 {
319         /* Not used anymore - ignore signal */
320 }
321 #endif /* CONFIG_NATIVE_WINDOWS */
322
323
324 static int hostapd_global_init(struct hapd_interfaces *interfaces,
325                                const char *entropy_file)
326 {
327         int i;
328
329         os_memset(&global, 0, sizeof(global));
330
331         hostapd_logger_register_cb(hostapd_logger_cb);
332
333         if (eap_server_register_methods()) {
334                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
335                 return -1;
336         }
337
338         if (eloop_init()) {
339                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
340                 return -1;
341         }
342         interfaces->eloop_initialized = 1;
343
344         random_init(entropy_file);
345
346 #ifndef CONFIG_NATIVE_WINDOWS
347         eloop_register_signal(SIGHUP, handle_reload, interfaces);
348         eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
349 #endif /* CONFIG_NATIVE_WINDOWS */
350         eloop_register_signal_terminate(handle_term, interfaces);
351
352 #ifndef CONFIG_NATIVE_WINDOWS
353         openlog("hostapd", 0, LOG_DAEMON);
354 #endif /* CONFIG_NATIVE_WINDOWS */
355
356         for (i = 0; wpa_drivers[i]; i++)
357                 global.drv_count++;
358         if (global.drv_count == 0) {
359                 wpa_printf(MSG_ERROR, "No drivers enabled");
360                 return -1;
361         }
362         global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
363         if (global.drv_priv == NULL)
364                 return -1;
365
366         return 0;
367 }
368
369
370 static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
371 {
372         int i;
373
374         for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
375                 if (!global.drv_priv[i])
376                         continue;
377                 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
378         }
379         os_free(global.drv_priv);
380         global.drv_priv = NULL;
381
382 #ifdef EAP_SERVER_TNC
383         tncs_global_deinit();
384 #endif /* EAP_SERVER_TNC */
385
386         random_deinit();
387
388         if (eloop_initialized)
389                 eloop_destroy();
390
391 #ifndef CONFIG_NATIVE_WINDOWS
392         closelog();
393 #endif /* CONFIG_NATIVE_WINDOWS */
394
395         eap_server_unregister_methods();
396
397         os_daemonize_terminate(pid_file);
398 }
399
400
401 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
402                               const char *pid_file)
403 {
404 #ifdef EAP_SERVER_TNC
405         int tnc = 0;
406         size_t i, k;
407
408         for (i = 0; !tnc && i < ifaces->count; i++) {
409                 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
410                         if (ifaces->iface[i]->bss[0]->conf->tnc) {
411                                 tnc++;
412                                 break;
413                         }
414                 }
415         }
416
417         if (tnc && tncs_global_init() < 0) {
418                 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
419                 return -1;
420         }
421 #endif /* EAP_SERVER_TNC */
422
423         if (daemonize) {
424                 if (os_daemonize(pid_file)) {
425                         wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
426                         return -1;
427                 }
428                 if (eloop_sock_requeue()) {
429                         wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
430                                    strerror(errno));
431                         return -1;
432                 }
433         }
434
435         eloop_run();
436
437         return 0;
438 }
439
440
441 static void show_version(void)
442 {
443         fprintf(stderr,
444                 "hostapd v" VERSION_STR "\n"
445                 "User space daemon for IEEE 802.11 AP management,\n"
446                 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
447                 "Copyright (c) 2002-2016, Jouni Malinen <j@w1.fi> "
448                 "and contributors\n");
449 }
450
451
452 static void usage(void)
453 {
454         show_version();
455         fprintf(stderr,
456                 "\n"
457                 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
458                 "\\\n"
459                 "         [-g <global ctrl_iface>] [-G <group>] \\\n"
460                 "         <configuration file(s)>\n"
461                 "\n"
462                 "options:\n"
463                 "   -h   show this usage\n"
464                 "   -d   show more debug messages (-dd for even more)\n"
465                 "   -B   run daemon in the background\n"
466                 "   -e   entropy file\n"
467                 "   -g   global control interface path\n"
468                 "   -G   group for control interfaces\n"
469                 "   -P   PID file\n"
470                 "   -K   include key data in debug messages\n"
471 #ifdef CONFIG_DEBUG_FILE
472                 "   -f   log output to debug file instead of stdout\n"
473 #endif /* CONFIG_DEBUG_FILE */
474 #ifdef CONFIG_DEBUG_LINUX_TRACING
475                 "   -T = record to Linux tracing in addition to logging\n"
476                 "        (records all messages regardless of debug verbosity)\n"
477 #endif /* CONFIG_DEBUG_LINUX_TRACING */
478                 "   -S   start all the interfaces synchronously\n"
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->conf)
490                 return hapd->conf->iface;
491         return NULL;
492 }
493
494
495 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
496                                          const char *path)
497 {
498 #ifndef CONFIG_CTRL_IFACE_UDP
499         char *pos;
500 #endif /* !CONFIG_CTRL_IFACE_UDP */
501
502         os_free(interfaces->global_iface_path);
503         interfaces->global_iface_path = os_strdup(path);
504         if (interfaces->global_iface_path == NULL)
505                 return -1;
506
507 #ifndef CONFIG_CTRL_IFACE_UDP
508         pos = os_strrchr(interfaces->global_iface_path, '/');
509         if (pos == NULL) {
510                 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
511                            "file");
512                 os_free(interfaces->global_iface_path);
513                 interfaces->global_iface_path = NULL;
514                 return -1;
515         }
516
517         *pos = '\0';
518         interfaces->global_iface_name = pos + 1;
519 #endif /* !CONFIG_CTRL_IFACE_UDP */
520
521         return 0;
522 }
523
524
525 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
526                                         const char *group)
527 {
528 #ifndef CONFIG_NATIVE_WINDOWS
529         struct group *grp;
530         grp = getgrnam(group);
531         if (grp == NULL) {
532                 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
533                 return -1;
534         }
535         interfaces->ctrl_iface_group = grp->gr_gid;
536 #endif /* CONFIG_NATIVE_WINDOWS */
537         return 0;
538 }
539
540
541 #ifdef CONFIG_WPS
542 static int gen_uuid(const char *txt_addr)
543 {
544         u8 addr[ETH_ALEN];
545         u8 uuid[UUID_LEN];
546         char buf[100];
547
548         if (hwaddr_aton(txt_addr, addr) < 0)
549                 return -1;
550
551         uuid_gen_mac_addr(addr, uuid);
552         if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
553                 return -1;
554
555         printf("%s\n", buf);
556
557         return 0;
558 }
559 #endif /* CONFIG_WPS */
560
561
562 #ifndef HOSTAPD_CLEANUP_INTERVAL
563 #define HOSTAPD_CLEANUP_INTERVAL 10
564 #endif /* HOSTAPD_CLEANUP_INTERVAL */
565
566 static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
567 {
568         hostapd_periodic_iface(iface);
569         return 0;
570 }
571
572
573 /* Periodic cleanup tasks */
574 static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
575 {
576         struct hapd_interfaces *interfaces = eloop_ctx;
577
578         eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
579                                hostapd_periodic, interfaces, NULL);
580         hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
581 }
582
583
584 int main(int argc, char *argv[])
585 {
586         struct hapd_interfaces interfaces;
587         int ret = 1;
588         size_t i, j;
589         int c, debug = 0, daemonize = 0;
590         char *pid_file = NULL;
591         const char *log_file = NULL;
592         const char *entropy_file = NULL;
593         char **bss_config = NULL, **tmp_bss;
594         size_t num_bss_configs = 0;
595 #ifdef CONFIG_DEBUG_LINUX_TRACING
596         int enable_trace_dbg = 0;
597 #endif /* CONFIG_DEBUG_LINUX_TRACING */
598         int start_ifaces_in_sync = 0;
599
600         if (os_program_init())
601                 return -1;
602
603         os_memset(&interfaces, 0, sizeof(interfaces));
604         interfaces.reload_config = hostapd_reload_config;
605         interfaces.config_read_cb = hostapd_config_read;
606         interfaces.for_each_interface = hostapd_for_each_interface;
607         interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
608         interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
609         interfaces.driver_init = hostapd_driver_init;
610         interfaces.global_iface_path = NULL;
611         interfaces.global_iface_name = NULL;
612         interfaces.global_ctrl_sock = -1;
613         dl_list_init(&interfaces.global_ctrl_dst);
614
615         for (;;) {
616                 c = getopt(argc, argv, "b:Bde:f:hKP:STtu:vg:G:");
617                 if (c < 0)
618                         break;
619                 switch (c) {
620                 case 'h':
621                         usage();
622                         break;
623                 case 'd':
624                         debug++;
625                         if (wpa_debug_level > 0)
626                                 wpa_debug_level--;
627                         break;
628                 case 'B':
629                         daemonize++;
630                         break;
631                 case 'e':
632                         entropy_file = optarg;
633                         break;
634                 case 'f':
635                         log_file = optarg;
636                         break;
637                 case 'K':
638                         wpa_debug_show_keys++;
639                         break;
640                 case 'P':
641                         os_free(pid_file);
642                         pid_file = os_rel2abs_path(optarg);
643                         break;
644                 case 't':
645                         wpa_debug_timestamp++;
646                         break;
647 #ifdef CONFIG_DEBUG_LINUX_TRACING
648                 case 'T':
649                         enable_trace_dbg = 1;
650                         break;
651 #endif /* CONFIG_DEBUG_LINUX_TRACING */
652                 case 'v':
653                         show_version();
654                         exit(1);
655                         break;
656                 case 'g':
657                         if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
658                                 return -1;
659                         break;
660                 case 'G':
661                         if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
662                                 return -1;
663                         break;
664                 case 'b':
665                         tmp_bss = os_realloc_array(bss_config,
666                                                    num_bss_configs + 1,
667                                                    sizeof(char *));
668                         if (tmp_bss == NULL)
669                                 goto out;
670                         bss_config = tmp_bss;
671                         bss_config[num_bss_configs++] = optarg;
672                         break;
673                 case 'S':
674                         start_ifaces_in_sync = 1;
675                         break;
676 #ifdef CONFIG_WPS
677                 case 'u':
678                         return gen_uuid(optarg);
679 #endif /* CONFIG_WPS */
680                 default:
681                         usage();
682                         break;
683                 }
684         }
685
686         if (optind == argc && interfaces.global_iface_path == NULL &&
687             num_bss_configs == 0)
688                 usage();
689
690         wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
691
692         if (log_file)
693                 wpa_debug_open_file(log_file);
694         else
695                 wpa_debug_setup_stdout();
696 #ifdef CONFIG_DEBUG_LINUX_TRACING
697         if (enable_trace_dbg) {
698                 int tret = wpa_debug_open_linux_tracing();
699                 if (tret) {
700                         wpa_printf(MSG_ERROR, "Failed to enable trace logging");
701                         return -1;
702                 }
703         }
704 #endif /* CONFIG_DEBUG_LINUX_TRACING */
705
706         interfaces.count = argc - optind;
707         if (interfaces.count || num_bss_configs) {
708                 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
709                                              sizeof(struct hostapd_iface *));
710                 if (interfaces.iface == NULL) {
711                         wpa_printf(MSG_ERROR, "malloc failed");
712                         return -1;
713                 }
714         }
715
716         if (hostapd_global_init(&interfaces, entropy_file)) {
717                 wpa_printf(MSG_ERROR, "Failed to initialize global context");
718                 return -1;
719         }
720
721         eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
722                                hostapd_periodic, &interfaces, NULL);
723
724         if (fst_global_init()) {
725                 wpa_printf(MSG_ERROR,
726                            "Failed to initialize global FST context");
727                 goto out;
728         }
729
730 #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
731         if (!fst_global_add_ctrl(fst_ctrl_cli))
732                 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
733 #endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
734
735         /* Allocate and parse configuration for full interface files */
736         for (i = 0; i < interfaces.count; i++) {
737                 interfaces.iface[i] = hostapd_interface_init(&interfaces,
738                                                              argv[optind + i],
739                                                              debug);
740                 if (!interfaces.iface[i]) {
741                         wpa_printf(MSG_ERROR, "Failed to initialize interface");
742                         goto out;
743                 }
744                 if (start_ifaces_in_sync)
745                         interfaces.iface[i]->need_to_start_in_sync = 1;
746         }
747
748         /* Allocate and parse configuration for per-BSS files */
749         for (i = 0; i < num_bss_configs; i++) {
750                 struct hostapd_iface *iface;
751                 char *fname;
752
753                 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
754                 fname = os_strchr(bss_config[i], ':');
755                 if (fname == NULL) {
756                         wpa_printf(MSG_ERROR,
757                                    "Invalid BSS config identifier '%s'",
758                                    bss_config[i]);
759                         goto out;
760                 }
761                 *fname++ = '\0';
762                 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
763                                                    fname, debug);
764                 if (iface == NULL)
765                         goto out;
766                 for (j = 0; j < interfaces.count; j++) {
767                         if (interfaces.iface[j] == iface)
768                                 break;
769                 }
770                 if (j == interfaces.count) {
771                         struct hostapd_iface **tmp;
772                         tmp = os_realloc_array(interfaces.iface,
773                                                interfaces.count + 1,
774                                                sizeof(struct hostapd_iface *));
775                         if (tmp == NULL) {
776                                 hostapd_interface_deinit_free(iface);
777                                 goto out;
778                         }
779                         interfaces.iface = tmp;
780                         interfaces.iface[interfaces.count++] = iface;
781                 }
782         }
783
784         /*
785          * Enable configured interfaces. Depending on channel configuration,
786          * this may complete full initialization before returning or use a
787          * callback mechanism to complete setup in case of operations like HT
788          * co-ex scans, ACS, or DFS are needed to determine channel parameters.
789          * In such case, the interface will be enabled from eloop context within
790          * hostapd_global_run().
791          */
792         interfaces.terminate_on_error = interfaces.count;
793         for (i = 0; i < interfaces.count; i++) {
794                 if (hostapd_driver_init(interfaces.iface[i]) ||
795                     hostapd_setup_interface(interfaces.iface[i]))
796                         goto out;
797         }
798
799         hostapd_global_ctrl_iface_init(&interfaces);
800
801         if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
802                 wpa_printf(MSG_ERROR, "Failed to start eloop");
803                 goto out;
804         }
805
806         ret = 0;
807
808  out:
809         hostapd_global_ctrl_iface_deinit(&interfaces);
810         /* Deinitialize all interfaces */
811         for (i = 0; i < interfaces.count; i++) {
812                 if (!interfaces.iface[i])
813                         continue;
814                 interfaces.iface[i]->driver_ap_teardown =
815                         !!(interfaces.iface[i]->drv_flags &
816                            WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
817                 hostapd_interface_deinit_free(interfaces.iface[i]);
818         }
819         os_free(interfaces.iface);
820
821         if (interfaces.eloop_initialized)
822                 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
823         hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
824         os_free(pid_file);
825
826         if (log_file)
827                 wpa_debug_close_file();
828         wpa_debug_close_linux_tracing();
829
830         os_free(bss_config);
831
832         fst_global_deinit();
833
834         os_program_deinit();
835
836         return ret;
837 }