]> git.ipfire.org Git - thirdparty/hostap.git/blob - hostapd/main.c
HT: Remove SMPS in AP mode
[thirdparty/hostap.git] / hostapd / main.c
1 /*
2 * hostapd / main()
3 * Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10 #ifndef CONFIG_NATIVE_WINDOWS
11 #include <syslog.h>
12 #include <grp.h>
13 #endif /* CONFIG_NATIVE_WINDOWS */
14
15 #include "utils/common.h"
16 #include "utils/eloop.h"
17 #include "utils/uuid.h"
18 #include "crypto/random.h"
19 #include "crypto/tls.h"
20 #include "common/version.h"
21 #include "common/dpp.h"
22 #include "drivers/driver.h"
23 #include "eap_server/eap.h"
24 #include "eap_server/tncs.h"
25 #include "ap/hostapd.h"
26 #include "ap/ap_config.h"
27 #include "ap/ap_drv_ops.h"
28 #include "ap/dpp_hostapd.h"
29 #include "fst/fst.h"
30 #include "config_file.h"
31 #include "eap_register.h"
32 #include "ctrl_iface.h"
33
34
35 struct hapd_global {
36 void **drv_priv;
37 size_t drv_count;
38 };
39
40 static struct hapd_global global;
41
42
43 #ifndef CONFIG_NO_HOSTAPD_LOGGER
44 static 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;
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),
95 module_str ? " " : "", module_str ? module_str : "",
96 txt);
97 else if (hapd && hapd->conf)
98 os_snprintf(format, maxlen, "%s:%s%s %s",
99 hapd->conf->iface, module_str ? " " : "",
100 module_str ? module_str : "", txt);
101 else if (addr)
102 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
103 MAC2STR(addr), module_str ? " " : "",
104 module_str ? module_str : "", txt);
105 else
106 os_snprintf(format, maxlen, "%s%s%s",
107 module_str ? module_str : "",
108 module_str ? ": " : "", txt);
109
110 #ifdef CONFIG_DEBUG_SYSLOG
111 if (wpa_debug_syslog)
112 conf_stdout = 0;
113 #endif /* CONFIG_DEBUG_SYSLOG */
114 if ((conf_stdout & module) && level >= conf_stdout_level) {
115 wpa_debug_print_timestamp();
116 wpa_printf(MSG_INFO, "%s", format);
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
149 /**
150 * hostapd_driver_init - Preparate driver interface
151 */
152 static 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;
159 struct wpa_driver_capa capa;
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));
171 for (i = 0; wpa_drivers[i]; i++) {
172 if (wpa_drivers[i] != hapd->driver)
173 continue;
174
175 if (global.drv_priv[i] == NULL &&
176 wpa_drivers[i]->global_init) {
177 global.drv_priv[i] =
178 wpa_drivers[i]->global_init(iface->interfaces);
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 }
185 }
186
187 params.global_priv = global.drv_priv[i];
188 break;
189 }
190 params.bssid = b;
191 params.ifname = hapd->conf->iface;
192 params.driver_params = hapd->iconf->driver_params;
193 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
194
195 params.num_bridge = hapd->iface->num_bss;
196 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
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
216 if (hapd->driver->get_capa &&
217 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
218 struct wowlan_triggers *triggs;
219
220 iface->drv_flags = capa.flags;
221 iface->probe_resp_offloads = capa.probe_resp_offloads;
222 /*
223 * Use default extended capa values from per-radio information
224 */
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;
228 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
229
230 /*
231 * Override extended capa with per-interface type (AP), if
232 * available from the driver.
233 */
234 hostapd_get_ext_capa(iface);
235
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);
242 }
243
244 return 0;
245 }
246
247
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
253 * interfaces. No actual driver operations are started.
254 */
255 static struct hostapd_iface *
256 hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
257 const char *config_fname, int debug)
258 {
259 struct hostapd_iface *iface;
260 int k;
261
262 wpa_printf(MSG_DEBUG, "Configuration file: %s", config_fname);
263 iface = hostapd_init(interfaces, config_fname);
264 if (!iface)
265 return NULL;
266
267 if (if_name) {
268 os_strlcpy(iface->conf->bss[0]->iface, if_name,
269 sizeof(iface->conf->bss[0]->iface));
270 }
271
272 iface->interfaces = interfaces;
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
279 if (iface->conf->bss[0]->iface[0] == '\0' &&
280 !hostapd_drv_none(iface->bss[0])) {
281 wpa_printf(MSG_ERROR,
282 "Interface name not specified in %s, nor by '-i' parameter",
283 config_fname);
284 hostapd_interface_deinit_free(iface);
285 return NULL;
286 }
287
288 return iface;
289 }
290
291
292 /**
293 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
294 */
295 static void handle_term(int sig, void *signal_ctx)
296 {
297 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
298 eloop_terminate();
299 }
300
301
302 #ifndef CONFIG_NATIVE_WINDOWS
303
304 static 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
314 /**
315 * handle_reload - SIGHUP handler to reload configuration
316 */
317 static void handle_reload(int sig, void *signal_ctx)
318 {
319 struct hapd_interfaces *interfaces = signal_ctx;
320 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
321 sig);
322 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
323 }
324
325
326 static void handle_dump_state(int sig, void *signal_ctx)
327 {
328 /* Not used anymore - ignore signal */
329 }
330 #endif /* CONFIG_NATIVE_WINDOWS */
331
332
333 static int hostapd_global_init(struct hapd_interfaces *interfaces,
334 const char *entropy_file)
335 {
336 int i;
337
338 os_memset(&global, 0, sizeof(global));
339
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
347 if (eloop_init()) {
348 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
349 return -1;
350 }
351 interfaces->eloop_initialized = 1;
352
353 random_init(entropy_file);
354
355 #ifndef CONFIG_NATIVE_WINDOWS
356 eloop_register_signal(SIGHUP, handle_reload, interfaces);
357 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
358 #endif /* CONFIG_NATIVE_WINDOWS */
359 eloop_register_signal_terminate(handle_term, interfaces);
360
361 #ifndef CONFIG_NATIVE_WINDOWS
362 openlog("hostapd", 0, LOG_DAEMON);
363 #endif /* CONFIG_NATIVE_WINDOWS */
364
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 }
371 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
372 if (global.drv_priv == NULL)
373 return -1;
374
375 return 0;
376 }
377
378
379 static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
380 {
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
391 #ifdef EAP_SERVER_TNC
392 tncs_global_deinit();
393 #endif /* EAP_SERVER_TNC */
394
395 random_deinit();
396
397 if (eloop_initialized)
398 eloop_destroy();
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
410 static 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
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 }
442 }
443
444 eloop_run();
445
446 return 0;
447 }
448
449
450 static 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"
456 "Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi> "
457 "and contributors\n");
458 }
459
460
461 static void usage(void)
462 {
463 show_version();
464 fprintf(stderr,
465 "\n"
466 "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
467 "\\\n"
468 " [-g <global ctrl_iface>] [-G <group>]\\\n"
469 " [-i <comma-separated list of interface names>]\\\n"
470 " <configuration file(s)>\n"
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"
476 " -e entropy file\n"
477 " -g global control interface path\n"
478 " -G group for control interfaces\n"
479 " -P PID file\n"
480 " -K include key data in debug messages\n"
481 #ifdef CONFIG_DEBUG_FILE
482 " -f log output to debug file instead of stdout\n"
483 #endif /* CONFIG_DEBUG_FILE */
484 #ifdef CONFIG_DEBUG_LINUX_TRACING
485 " -T record to Linux tracing in addition to logging\n"
486 " (records all messages regardless of debug verbosity)\n"
487 #endif /* CONFIG_DEBUG_LINUX_TRACING */
488 " -i list of interface names to use\n"
489 #ifdef CONFIG_DEBUG_SYSLOG
490 " -s log output to syslog instead of stdout\n"
491 #endif /* CONFIG_DEBUG_SYSLOG */
492 " -S start all the interfaces synchronously\n"
493 " -t include timestamps in some debug messages\n"
494 " -v show hostapd version\n");
495
496 exit(1);
497 }
498
499
500 static const char * hostapd_msg_ifname_cb(void *ctx)
501 {
502 struct hostapd_data *hapd = ctx;
503 if (hapd && hapd->conf)
504 return hapd->conf->iface;
505 return NULL;
506 }
507
508
509 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
510 const char *path)
511 {
512 #ifndef CONFIG_CTRL_IFACE_UDP
513 char *pos;
514 #endif /* !CONFIG_CTRL_IFACE_UDP */
515
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;
520
521 #ifndef CONFIG_CTRL_IFACE_UDP
522 pos = os_strrchr(interfaces->global_iface_path, '/');
523 if (pos == NULL) {
524 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
525 "file");
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;
533 #endif /* !CONFIG_CTRL_IFACE_UDP */
534
535 return 0;
536 }
537
538
539 static 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
555 static int hostapd_get_interface_names(char ***if_names,
556 size_t *if_names_size,
557 char *arg)
558 {
559 char *if_name, *tmp, **nnames;
560 size_t i;
561
562 if (!arg)
563 return -1;
564 if_name = strtok_r(arg, ",", &tmp);
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
582 fail:
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
592 #ifdef CONFIG_WPS
593 static 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
613 #ifndef HOSTAPD_CLEANUP_INTERVAL
614 #define HOSTAPD_CLEANUP_INTERVAL 10
615 #endif /* HOSTAPD_CLEANUP_INTERVAL */
616
617 static 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 */
625 static 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
635 int main(int argc, char *argv[])
636 {
637 struct hapd_interfaces interfaces;
638 int ret = 1;
639 size_t i, j;
640 int c, debug = 0, daemonize = 0;
641 char *pid_file = NULL;
642 const char *log_file = NULL;
643 const char *entropy_file = NULL;
644 char **bss_config = NULL, **tmp_bss;
645 size_t num_bss_configs = 0;
646 #ifdef CONFIG_DEBUG_LINUX_TRACING
647 int enable_trace_dbg = 0;
648 #endif /* CONFIG_DEBUG_LINUX_TRACING */
649 int start_ifaces_in_sync = 0;
650 char **if_names = NULL;
651 size_t if_names_size = 0;
652 #ifdef CONFIG_DPP
653 struct dpp_global_config dpp_conf;
654 #endif /* CONFIG_DPP */
655
656 if (os_program_init())
657 return -1;
658
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;
665 interfaces.driver_init = hostapd_driver_init;
666 interfaces.global_iface_path = NULL;
667 interfaces.global_iface_name = NULL;
668 interfaces.global_ctrl_sock = -1;
669 dl_list_init(&interfaces.global_ctrl_dst);
670 #ifdef CONFIG_ETH_P_OUI
671 dl_list_init(&interfaces.eth_p_oui);
672 #endif /* CONFIG_ETH_P_OUI */
673 #ifdef CONFIG_DPP
674 os_memset(&dpp_conf, 0, sizeof(dpp_conf));
675 /* TODO: dpp_conf.msg_ctx? */
676 interfaces.dpp = dpp_global_init(&dpp_conf);
677 if (!interfaces.dpp)
678 return -1;
679 #endif /* CONFIG_DPP */
680
681 for (;;) {
682 c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:");
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;
697 case 'e':
698 entropy_file = optarg;
699 break;
700 case 'f':
701 log_file = optarg;
702 break;
703 case 'K':
704 wpa_debug_show_keys++;
705 break;
706 case 'P':
707 os_free(pid_file);
708 pid_file = os_rel2abs_path(optarg);
709 break;
710 case 't':
711 wpa_debug_timestamp++;
712 break;
713 #ifdef CONFIG_DEBUG_LINUX_TRACING
714 case 'T':
715 enable_trace_dbg = 1;
716 break;
717 #endif /* CONFIG_DEBUG_LINUX_TRACING */
718 case 'v':
719 show_version();
720 exit(1);
721 break;
722 case 'g':
723 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
724 return -1;
725 break;
726 case 'G':
727 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
728 return -1;
729 break;
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;
739 #ifdef CONFIG_DEBUG_SYSLOG
740 case 's':
741 wpa_debug_syslog = 1;
742 break;
743 #endif /* CONFIG_DEBUG_SYSLOG */
744 case 'S':
745 start_ifaces_in_sync = 1;
746 break;
747 #ifdef CONFIG_WPS
748 case 'u':
749 return gen_uuid(optarg);
750 #endif /* CONFIG_WPS */
751 case 'i':
752 if (hostapd_get_interface_names(&if_names,
753 &if_names_size, optarg))
754 goto out;
755 break;
756 default:
757 usage();
758 break;
759 }
760 }
761
762 if (optind == argc && interfaces.global_iface_path == NULL &&
763 num_bss_configs == 0)
764 usage();
765
766 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
767
768 if (log_file)
769 wpa_debug_open_file(log_file);
770 if (!log_file && !wpa_debug_syslog)
771 wpa_debug_setup_stdout();
772 #ifdef CONFIG_DEBUG_SYSLOG
773 if (wpa_debug_syslog)
774 wpa_debug_open_syslog();
775 #endif /* CONFIG_DEBUG_SYSLOG */
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 */
785
786 interfaces.count = argc - optind;
787 if (interfaces.count || num_bss_configs) {
788 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
789 sizeof(struct hostapd_iface *));
790 if (interfaces.iface == NULL) {
791 wpa_printf(MSG_ERROR, "malloc failed");
792 return -1;
793 }
794 }
795
796 if (hostapd_global_init(&interfaces, entropy_file)) {
797 wpa_printf(MSG_ERROR, "Failed to initialize global context");
798 return -1;
799 }
800
801 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
802 hostapd_periodic, &interfaces, NULL);
803
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
815 /* Allocate and parse configuration for full interface files */
816 for (i = 0; i < interfaces.count; i++) {
817 char *if_name = NULL;
818
819 if (i < if_names_size)
820 if_name = if_names[i];
821
822 interfaces.iface[i] = hostapd_interface_init(&interfaces,
823 if_name,
824 argv[optind + i],
825 debug);
826 if (!interfaces.iface[i]) {
827 wpa_printf(MSG_ERROR, "Failed to initialize interface");
828 goto out;
829 }
830 if (start_ifaces_in_sync)
831 interfaces.iface[i]->need_to_start_in_sync = 1;
832 }
833
834 /* Allocate and parse configuration for per-BSS files */
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
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 */
878 interfaces.terminate_on_error = interfaces.count;
879 for (i = 0; i < interfaces.count; i++) {
880 if (hostapd_driver_init(interfaces.iface[i]) ||
881 hostapd_setup_interface(interfaces.iface[i]))
882 goto out;
883 }
884
885 hostapd_global_ctrl_iface_init(&interfaces);
886
887 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
888 wpa_printf(MSG_ERROR, "Failed to start eloop");
889 goto out;
890 }
891
892 ret = 0;
893
894 out:
895 hostapd_global_ctrl_iface_deinit(&interfaces);
896 /* Deinitialize all interfaces */
897 for (i = 0; i < interfaces.count; i++) {
898 if (!interfaces.iface[i])
899 continue;
900 interfaces.iface[i]->driver_ap_teardown =
901 !!(interfaces.iface[i]->drv_flags &
902 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
903 hostapd_interface_deinit_free(interfaces.iface[i]);
904 }
905 os_free(interfaces.iface);
906
907 #ifdef CONFIG_DPP
908 dpp_global_deinit(interfaces.dpp);
909 #endif /* CONFIG_DPP */
910
911 if (interfaces.eloop_initialized)
912 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
913 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
914 os_free(pid_file);
915
916 wpa_debug_close_syslog();
917 if (log_file)
918 wpa_debug_close_file();
919 wpa_debug_close_linux_tracing();
920
921 os_free(bss_config);
922
923 for (i = 0; i < if_names_size; i++)
924 os_free(if_names[i]);
925 os_free(if_names);
926
927 fst_global_deinit();
928
929 os_program_deinit();
930
931 return ret;
932 }