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