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