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