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