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