1 /*
2 * hostapd / main()
3 * Copyright (c) 2002-2022, Jouni Malinen <[email protected]>
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/crypto.h"
19 #include "crypto/random.h"
20 #include "crypto/tls.h"
21 #include "common/version.h"
22 #include "common/dpp.h"
23 #include "drivers/driver.h"
24 #include "eap_server/eap.h"
25 #include "eap_server/tncs.h"
26 #include "ap/hostapd.h"
27 #include "ap/ap_config.h"
28 #include "ap/ap_drv_ops.h"
29 #include "ap/dpp_hostapd.h"
30 #include "fst/fst.h"
31 #include "config_file.h"
32 #include "eap_register.h"
33 #include "ctrl_iface.h"
34 #ifdef CONFIG_CTRL_IFACE_AIDL
35 #include "aidl.h"
36 #endif /* CONFIG_CTRL_IFACE_AIDL */
37
38 struct hapd_global {
39 void **drv_priv;
40 size_t drv_count;
41 };
42
43 static struct hapd_global global;
44
45
46 #ifndef CONFIG_NO_HOSTAPD_LOGGER
hostapd_logger_cb(void * ctx,const u8 * addr,unsigned int module,int level,const char * txt,size_t len)47 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
48 int level, const char *txt, size_t len)
49 {
50 struct hostapd_data *hapd = ctx;
51 char *format, *module_str;
52 int maxlen;
53 int conf_syslog_level, conf_stdout_level;
54 unsigned int conf_syslog, conf_stdout;
55
56 maxlen = len + 100;
57 format = os_malloc(maxlen);
58 if (!format)
59 return;
60
61 if (hapd && hapd->conf) {
62 conf_syslog_level = hapd->conf->logger_syslog_level;
63 conf_stdout_level = hapd->conf->logger_stdout_level;
64 conf_syslog = hapd->conf->logger_syslog;
65 conf_stdout = hapd->conf->logger_stdout;
66 } else {
67 conf_syslog_level = conf_stdout_level = 0;
68 conf_syslog = conf_stdout = (unsigned int) -1;
69 }
70
71 switch (module) {
72 case HOSTAPD_MODULE_IEEE80211:
73 module_str = "IEEE 802.11";
74 break;
75 case HOSTAPD_MODULE_IEEE8021X:
76 module_str = "IEEE 802.1X";
77 break;
78 case HOSTAPD_MODULE_RADIUS:
79 module_str = "RADIUS";
80 break;
81 case HOSTAPD_MODULE_WPA:
82 module_str = "WPA";
83 break;
84 case HOSTAPD_MODULE_DRIVER:
85 module_str = "DRIVER";
86 break;
87 case HOSTAPD_MODULE_MLME:
88 module_str = "MLME";
89 break;
90 default:
91 module_str = NULL;
92 break;
93 }
94
95 if (hapd && hapd->conf && addr)
96 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
97 hapd->conf->iface, MAC2STR(addr),
98 module_str ? " " : "", module_str ? module_str : "",
99 txt);
100 else if (hapd && hapd->conf)
101 os_snprintf(format, maxlen, "%s:%s%s %s",
102 hapd->conf->iface, module_str ? " " : "",
103 module_str ? module_str : "", txt);
104 else if (addr)
105 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
106 MAC2STR(addr), module_str ? " " : "",
107 module_str ? module_str : "", txt);
108 else
109 os_snprintf(format, maxlen, "%s%s%s",
110 module_str ? module_str : "",
111 module_str ? ": " : "", txt);
112
113 #ifdef CONFIG_DEBUG_SYSLOG
114 if (wpa_debug_syslog)
115 conf_stdout = 0;
116 #endif /* CONFIG_DEBUG_SYSLOG */
117 if ((conf_stdout & module) && level >= conf_stdout_level) {
118 wpa_debug_print_timestamp();
119 wpa_printf(MSG_INFO, "%s", format);
120 }
121
122 #ifndef CONFIG_NATIVE_WINDOWS
123 if ((conf_syslog & module) && level >= conf_syslog_level) {
124 int priority;
125 switch (level) {
126 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
127 case HOSTAPD_LEVEL_DEBUG:
128 priority = LOG_DEBUG;
129 break;
130 case HOSTAPD_LEVEL_INFO:
131 priority = LOG_INFO;
132 break;
133 case HOSTAPD_LEVEL_NOTICE:
134 priority = LOG_NOTICE;
135 break;
136 case HOSTAPD_LEVEL_WARNING:
137 priority = LOG_WARNING;
138 break;
139 default:
140 priority = LOG_INFO;
141 break;
142 }
143 syslog(priority, "%s", format);
144 }
145 #endif /* CONFIG_NATIVE_WINDOWS */
146
147 os_free(format);
148 }
149 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
150
151
152 /**
153 * hostapd_driver_init - Preparate driver interface
154 */
hostapd_driver_init(struct hostapd_iface * iface)155 static int hostapd_driver_init(struct hostapd_iface *iface)
156 {
157 struct wpa_init_params params;
158 size_t i;
159 struct hostapd_data *hapd = iface->bss[0];
160 struct hostapd_bss_config *conf = hapd->conf;
161 u8 *b = conf->bssid;
162 struct wpa_driver_capa capa;
163 #ifdef CONFIG_IEEE80211BE
164 struct hostapd_data *h_hapd = NULL;
165 #endif /* CONFIG_IEEE80211BE */
166
167 if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
168 wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
169 return -1;
170 }
171
172 #ifdef CONFIG_IEEE80211BE
173 if (conf->mld_ap)
174 h_hapd = hostapd_mld_get_first_bss(hapd);
175
176 if (h_hapd) {
177 hapd->drv_priv = h_hapd->drv_priv;
178 hapd->interface_added = h_hapd->interface_added;
179
180 /*
181 * All interfaces participating in the AP MLD would have
182 * the same MLD address, which is the interface hardware
183 * address, while the interface address would be
184 * derived from the original interface address if BSSID
185 * is not configured, and otherwise it would be the
186 * configured BSSID.
187 */
188 if (is_zero_ether_addr(b)) {
189 os_memcpy(hapd->own_addr, h_hapd->mld->mld_addr,
190 ETH_ALEN);
191 random_mac_addr_keep_oui(hapd->own_addr);
192 } else {
193 os_memcpy(hapd->own_addr, b, ETH_ALEN);
194 }
195
196 wpa_printf(MSG_DEBUG,
197 "Setup of non first link (%d) BSS of MLD %s",
198 hapd->mld_link_id, hapd->conf->iface);
199
200 goto setup_mld;
201 }
202 #endif /* CONFIG_IEEE80211BE */
203
204 /* Initialize the driver interface */
205 if (is_zero_ether_addr(b))
206 b = NULL;
207
208 os_memset(¶ms, 0, sizeof(params));
209 for (i = 0; wpa_drivers[i]; i++) {
210 if (wpa_drivers[i] != hapd->driver)
211 continue;
212
213 if (global.drv_priv[i] == NULL &&
214 wpa_drivers[i]->global_init) {
215 global.drv_priv[i] =
216 wpa_drivers[i]->global_init(iface->interfaces);
217 if (global.drv_priv[i] == NULL) {
218 wpa_printf(MSG_ERROR, "Failed to initialize "
219 "driver '%s'",
220 wpa_drivers[i]->name);
221 return -1;
222 }
223 }
224
225 params.global_priv = global.drv_priv[i];
226 break;
227 }
228 params.bssid = b;
229 #ifdef CONFIG_IEEE80211BE
230 /*
231 * Use the configured MLD MAC address as the interface hardware address
232 * if this AP is a part of an AP MLD.
233 */
234 if (hapd->conf->mld_ap) {
235 if (!is_zero_ether_addr(hapd->conf->mld_addr))
236 params.bssid = hapd->conf->mld_addr;
237 else
238 params.bssid = NULL;
239 }
240 #endif /* CONFIG_IEEE80211BE */
241
242 params.ifname = hapd->conf->iface;
243 params.driver_params = hapd->iconf->driver_params;
244 params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
245
246 params.num_bridge = hapd->iface->num_bss;
247 params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
248 if (params.bridge == NULL)
249 return -1;
250 for (i = 0; i < hapd->iface->num_bss; i++) {
251 struct hostapd_data *bss = hapd->iface->bss[i];
252 if (bss->conf->bridge[0])
253 params.bridge[i] = bss->conf->bridge;
254 }
255
256 params.own_addr = hapd->own_addr;
257
258 hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms);
259 os_free(params.bridge);
260 if (hapd->drv_priv == NULL) {
261 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
262 hapd->driver->name);
263 hapd->driver = NULL;
264 return -1;
265 }
266
267 #ifdef CONFIG_IEEE80211BE
268 /*
269 * This is the first interface added to the AP MLD, so have the
270 * interface hardware address be the MLD address, while the link address
271 * would be derived from the original interface address if BSSID is not
272 * configured, and otherwise it would be the configured BSSID.
273 */
274 if (hapd->conf->mld_ap) {
275 os_memcpy(hapd->mld->mld_addr, hapd->own_addr, ETH_ALEN);
276
277 if (!b)
278 random_mac_addr_keep_oui(hapd->own_addr);
279 else
280 os_memcpy(hapd->own_addr, b, ETH_ALEN);
281
282 wpa_printf(MSG_DEBUG, "Setup of first link (%d) BSS of MLD %s",
283 hapd->mld_link_id, hapd->conf->iface);
284 }
285
286 setup_mld:
287 #endif /* CONFIG_IEEE80211BE */
288
289 if (hapd->driver->get_capa &&
290 hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
291 struct wowlan_triggers *triggs;
292
293 iface->drv_flags = capa.flags;
294 iface->drv_flags2 = capa.flags2;
295 iface->drv_rrm_flags = capa.rrm_flags;
296 iface->probe_resp_offloads = capa.probe_resp_offloads;
297 /*
298 * Use default extended capa values from per-radio information
299 */
300 iface->extended_capa = capa.extended_capa;
301 iface->extended_capa_mask = capa.extended_capa_mask;
302 iface->extended_capa_len = capa.extended_capa_len;
303 iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
304
305 /*
306 * Override extended capa with per-interface type (AP), if
307 * available from the driver.
308 */
309 hostapd_get_ext_capa(iface);
310
311 hostapd_get_mld_capa(iface);
312
313 triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
314 if (triggs && hapd->driver->set_wowlan) {
315 if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
316 wpa_printf(MSG_ERROR, "set_wowlan failed");
317 }
318 os_free(triggs);
319
320 iface->mbssid_max_interfaces = capa.mbssid_max_interfaces;
321 iface->ema_max_periodicity = capa.ema_max_periodicity;
322 }
323
324 #ifdef CONFIG_IEEE80211BE
325 if (hapd->conf->mld_ap) {
326 if (!(iface->drv_flags2 & WPA_DRIVER_FLAGS2_MLO)) {
327 wpa_printf(MSG_INFO,
328 "MLD: Not supported by the driver");
329 return -1;
330 }
331
332 /* Initialize the BSS parameter change to 1 */
333 hapd->eht_mld_bss_param_change = 1;
334
335 wpa_printf(MSG_DEBUG,
336 "MLD: Set link_id=%u, mld_addr=" MACSTR
337 ", own_addr=" MACSTR,
338 hapd->mld_link_id, MAC2STR(hapd->mld->mld_addr),
339 MAC2STR(hapd->own_addr));
340
341 if (hostapd_drv_link_add(hapd, hapd->mld_link_id,
342 hapd->own_addr)) {
343 wpa_printf(MSG_ERROR,
344 "MLD: Failed to add link %d in MLD %s",
345 hapd->mld_link_id, hapd->conf->iface);
346 return -1;
347 }
348 hostapd_mld_add_link(hapd);
349 }
350 #endif /* CONFIG_IEEE80211BE */
351
352 return 0;
353 }
354
355
356 /**
357 * hostapd_interface_init - Read configuration file and init BSS data
358 *
359 * This function is used to parse configuration file for a full interface (one
360 * or more BSSes sharing the same radio) and allocate memory for the BSS
361 * interfaces. No actual driver operations are started.
362 */
363 static struct hostapd_iface *
hostapd_interface_init(struct hapd_interfaces * interfaces,const char * if_name,const char * config_fname,int debug)364 hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name,
365 const char *config_fname, int debug)
366 {
367 struct hostapd_iface *iface;
368 int k;
369
370 wpa_printf(MSG_DEBUG, "Configuration file: %s", config_fname);
371 iface = hostapd_init(interfaces, config_fname);
372 if (!iface)
373 return NULL;
374
375 if (if_name) {
376 os_strlcpy(iface->conf->bss[0]->iface, if_name,
377 sizeof(iface->conf->bss[0]->iface));
378 }
379
380 iface->interfaces = interfaces;
381
382 for (k = 0; k < debug; k++) {
383 if (iface->bss[0]->conf->logger_stdout_level > 0)
384 iface->bss[0]->conf->logger_stdout_level--;
385 }
386
387 if (iface->conf->bss[0]->iface[0] == '\0' &&
388 !hostapd_drv_none(iface->bss[0])) {
389 wpa_printf(MSG_ERROR,
390 "Interface name not specified in %s, nor by '-i' parameter",
391 config_fname);
392 hostapd_interface_deinit_free(iface);
393 return NULL;
394 }
395
396 return iface;
397 }
398
399
400 /**
401 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
402 */
handle_term(int sig,void * signal_ctx)403 static void handle_term(int sig, void *signal_ctx)
404 {
405 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
406 eloop_terminate();
407 }
408
409
410 #ifndef CONFIG_NATIVE_WINDOWS
411
handle_reload_iface(struct hostapd_iface * iface,void * ctx)412 static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
413 {
414 if (hostapd_reload_config(iface) < 0) {
415 wpa_printf(MSG_WARNING, "Failed to read new configuration "
416 "file - continuing with old.");
417 }
418 return 0;
419 }
420
421
422 /**
423 * handle_reload - SIGHUP handler to reload configuration
424 */
handle_reload(int sig,void * signal_ctx)425 static void handle_reload(int sig, void *signal_ctx)
426 {
427 struct hapd_interfaces *interfaces = signal_ctx;
428 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
429 sig);
430 hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
431 }
432
433
handle_dump_state(int sig,void * signal_ctx)434 static void handle_dump_state(int sig, void *signal_ctx)
435 {
436 /* Not used anymore - ignore signal */
437 }
438 #endif /* CONFIG_NATIVE_WINDOWS */
439
440
hostapd_global_init(struct hapd_interfaces * interfaces,const char * entropy_file)441 static int hostapd_global_init(struct hapd_interfaces *interfaces,
442 const char *entropy_file)
443 {
444 int i;
445
446 os_memset(&global, 0, sizeof(global));
447
448 hostapd_logger_register_cb(hostapd_logger_cb);
449
450 if (eap_server_register_methods()) {
451 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
452 return -1;
453 }
454
455 if (eloop_init()) {
456 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
457 return -1;
458 }
459 interfaces->eloop_initialized = 1;
460
461 random_init(entropy_file);
462
463 #ifndef CONFIG_NATIVE_WINDOWS
464 eloop_register_signal(SIGHUP, handle_reload, interfaces);
465 eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
466 #endif /* CONFIG_NATIVE_WINDOWS */
467 eloop_register_signal_terminate(handle_term, interfaces);
468
469 #ifndef CONFIG_NATIVE_WINDOWS
470 openlog("hostapd", 0, LOG_DAEMON);
471 #endif /* CONFIG_NATIVE_WINDOWS */
472
473 for (i = 0; wpa_drivers[i]; i++)
474 global.drv_count++;
475 if (global.drv_count == 0) {
476 wpa_printf(MSG_ERROR, "No drivers enabled");
477 return -1;
478 }
479 global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
480 if (global.drv_priv == NULL)
481 return -1;
482
483 return 0;
484 }
485
486
hostapd_global_deinit(const char * pid_file,int eloop_initialized)487 static void hostapd_global_deinit(const char *pid_file, int eloop_initialized)
488 {
489 int i;
490
491 for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
492 if (!global.drv_priv[i])
493 continue;
494 wpa_drivers[i]->global_deinit(global.drv_priv[i]);
495 }
496 os_free(global.drv_priv);
497 global.drv_priv = NULL;
498
499 #ifdef EAP_SERVER_TNC
500 tncs_global_deinit();
501 #endif /* EAP_SERVER_TNC */
502
503 random_deinit();
504
505 if (eloop_initialized)
506 eloop_destroy();
507
508 #ifndef CONFIG_NATIVE_WINDOWS
509 closelog();
510 #endif /* CONFIG_NATIVE_WINDOWS */
511
512 eap_server_unregister_methods();
513
514 os_daemonize_terminate(pid_file);
515 }
516
517
hostapd_global_run(struct hapd_interfaces * ifaces,int daemonize,const char * pid_file)518 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
519 const char *pid_file)
520 {
521 #ifdef EAP_SERVER_TNC
522 int tnc = 0;
523 size_t i, k;
524
525 for (i = 0; !tnc && i < ifaces->count; i++) {
526 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
527 if (ifaces->iface[i]->bss[0]->conf->tnc) {
528 tnc++;
529 break;
530 }
531 }
532 }
533
534 if (tnc && tncs_global_init() < 0) {
535 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
536 return -1;
537 }
538 #endif /* EAP_SERVER_TNC */
539
540 if (daemonize) {
541 if (os_daemonize(pid_file)) {
542 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
543 return -1;
544 }
545 if (eloop_sock_requeue()) {
546 wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
547 strerror(errno));
548 return -1;
549 }
550 }
551
552 #ifdef CONFIG_CTRL_IFACE_AIDL
553 if (hostapd_aidl_init(ifaces)) {
554 wpa_printf(MSG_ERROR, "Failed to initialize AIDL interface");
555 return -1;
556 }
557 #endif /* CONFIG_CTRL_IFACE_AIDL */
558
559 eloop_run();
560
561 return 0;
562 }
563
564
show_version(void)565 static void show_version(void)
566 {
567 fprintf(stderr,
568 "hostapd v%s\n"
569 "User space daemon for IEEE 802.11 AP management,\n"
570 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
571 "Copyright (c) 2002-2024, Jouni Malinen <[email protected]> "
572 "and contributors\n",
573 VERSION_STR);
574 }
575
576
usage(void)577 static void usage(void)
578 {
579 show_version();
580 fprintf(stderr,
581 "\n"
582 "usage: hostapd [-hdBKtvq] [-P <PID file>] [-e <entropy file>] "
583 "\\\n"
584 " [-g <global ctrl_iface>] [-G <group>]\\\n"
585 " [-i <comma-separated list of interface names>]\\\n"
586 " <configuration file(s)>\n"
587 "\n"
588 "options:\n"
589 " -h show this usage\n"
590 " -d show more debug messages (-dd for even more)\n"
591 " -B run daemon in the background\n"
592 " -e entropy file\n"
593 " -g global control interface path\n"
594 " -G group for control interfaces\n"
595 " -P PID file\n"
596 " -K include key data in debug messages\n"
597 #ifdef CONFIG_DEBUG_FILE
598 " -f log output to debug file instead of stdout\n"
599 #endif /* CONFIG_DEBUG_FILE */
600 #ifdef CONFIG_DEBUG_LINUX_TRACING
601 " -T record to Linux tracing in addition to logging\n"
602 " (records all messages regardless of debug verbosity)\n"
603 #endif /* CONFIG_DEBUG_LINUX_TRACING */
604 " -i list of interface names to use\n"
605 #ifdef CONFIG_DEBUG_SYSLOG
606 " -s log output to syslog instead of stdout\n"
607 #endif /* CONFIG_DEBUG_SYSLOG */
608 " -S start all the interfaces synchronously\n"
609 " -t include timestamps in some debug messages\n"
610 " -v show hostapd version\n"
611 " -q show less debug messages (-qq for even less)\n");
612
613 exit(1);
614 }
615
616
hostapd_msg_ifname_cb(void * ctx)617 static const char * hostapd_msg_ifname_cb(void *ctx)
618 {
619 struct hostapd_data *hapd = ctx;
620 if (hapd && hapd->conf)
621 return hapd->conf->iface;
622 return NULL;
623 }
624
625
hostapd_get_global_ctrl_iface(struct hapd_interfaces * interfaces,const char * path)626 static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
627 const char *path)
628 {
629 #ifndef CONFIG_CTRL_IFACE_UDP
630 char *pos;
631 #endif /* !CONFIG_CTRL_IFACE_UDP */
632
633 os_free(interfaces->global_iface_path);
634 interfaces->global_iface_path = os_strdup(path);
635 if (interfaces->global_iface_path == NULL)
636 return -1;
637
638 #ifndef CONFIG_CTRL_IFACE_UDP
639 pos = os_strrchr(interfaces->global_iface_path, '/');
640 if (pos == NULL) {
641 wpa_printf(MSG_ERROR, "No '/' in the global control interface "
642 "file");
643 os_free(interfaces->global_iface_path);
644 interfaces->global_iface_path = NULL;
645 return -1;
646 }
647
648 *pos = '\0';
649 interfaces->global_iface_name = pos + 1;
650 #endif /* !CONFIG_CTRL_IFACE_UDP */
651
652 return 0;
653 }
654
655
hostapd_get_ctrl_iface_group(struct hapd_interfaces * interfaces,const char * group)656 static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
657 const char *group)
658 {
659 #ifndef CONFIG_NATIVE_WINDOWS
660 struct group *grp;
661 grp = getgrnam(group);
662 if (grp == NULL) {
663 wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
664 return -1;
665 }
666 interfaces->ctrl_iface_group = grp->gr_gid;
667 #endif /* CONFIG_NATIVE_WINDOWS */
668 return 0;
669 }
670
671
hostapd_get_interface_names(char *** if_names,size_t * if_names_size,char * arg)672 static int hostapd_get_interface_names(char ***if_names,
673 size_t *if_names_size,
674 char *arg)
675 {
676 char *if_name, *tmp, **nnames;
677 size_t i;
678
679 if (!arg)
680 return -1;
681 if_name = strtok_r(arg, ",", &tmp);
682
683 while (if_name) {
684 nnames = os_realloc_array(*if_names, 1 + *if_names_size,
685 sizeof(char *));
686 if (!nnames)
687 goto fail;
688 *if_names = nnames;
689
690 (*if_names)[*if_names_size] = os_strdup(if_name);
691 if (!(*if_names)[*if_names_size])
692 goto fail;
693 (*if_names_size)++;
694 if_name = strtok_r(NULL, ",", &tmp);
695 }
696
697 return 0;
698
699 fail:
700 for (i = 0; i < *if_names_size; i++)
701 os_free((*if_names)[i]);
702 os_free(*if_names);
703 *if_names = NULL;
704 *if_names_size = 0;
705 return -1;
706 }
707
708
709 #ifdef CONFIG_WPS
gen_uuid(const char * txt_addr)710 static int gen_uuid(const char *txt_addr)
711 {
712 u8 addr[ETH_ALEN];
713 u8 uuid[UUID_LEN];
714 char buf[100];
715
716 if (hwaddr_aton(txt_addr, addr) < 0)
717 return -1;
718
719 uuid_gen_mac_addr(addr, uuid);
720 if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
721 return -1;
722
723 printf("%s\n", buf);
724
725 return 0;
726 }
727 #endif /* CONFIG_WPS */
728
729
730 #ifndef HOSTAPD_CLEANUP_INTERVAL
731 #define HOSTAPD_CLEANUP_INTERVAL 10
732 #endif /* HOSTAPD_CLEANUP_INTERVAL */
733
hostapd_periodic_call(struct hostapd_iface * iface,void * ctx)734 static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
735 {
736 hostapd_periodic_iface(iface);
737 return 0;
738 }
739
740
741 /* Periodic cleanup tasks */
hostapd_periodic(void * eloop_ctx,void * timeout_ctx)742 static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
743 {
744 struct hapd_interfaces *interfaces = eloop_ctx;
745
746 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
747 hostapd_periodic, interfaces, NULL);
748 hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
749 }
750
751
hostapd_global_cleanup_mld(struct hapd_interfaces * interfaces)752 static void hostapd_global_cleanup_mld(struct hapd_interfaces *interfaces)
753 {
754 #ifdef CONFIG_IEEE80211BE
755 size_t i;
756
757 if (!interfaces || !interfaces->mld)
758 return;
759
760 for (i = 0; i < interfaces->mld_count; i++) {
761 if (!interfaces->mld[i])
762 continue;
763
764 interfaces->mld_ctrl_iface_deinit(interfaces->mld[i]);
765 os_free(interfaces->mld[i]);
766 interfaces->mld[i] = NULL;
767 }
768
769 os_free(interfaces->mld);
770 interfaces->mld = NULL;
771 interfaces->mld_count = 0;
772 #endif /* CONFIG_IEEE80211BE */
773 }
774
775
main(int argc,char * argv[])776 int main(int argc, char *argv[])
777 {
778 struct hapd_interfaces interfaces;
779 int ret = 1;
780 size_t i, j;
781 int c, debug = 0, daemonize = 0;
782 char *pid_file = NULL;
783 const char *log_file = NULL;
784 const char *entropy_file = NULL;
785 char **bss_config = NULL, **tmp_bss;
786 size_t num_bss_configs = 0;
787 #ifdef CONFIG_DEBUG_LINUX_TRACING
788 int enable_trace_dbg = 0;
789 #endif /* CONFIG_DEBUG_LINUX_TRACING */
790 int start_ifaces_in_sync = 0;
791 char **if_names = NULL;
792 size_t if_names_size = 0;
793 #ifdef CONFIG_DPP
794 struct dpp_global_config dpp_conf;
795 #endif /* CONFIG_DPP */
796
797 if (os_program_init())
798 return -1;
799
800 os_memset(&interfaces, 0, sizeof(interfaces));
801 interfaces.reload_config = hostapd_reload_config;
802 interfaces.config_read_cb = hostapd_config_read;
803 interfaces.for_each_interface = hostapd_for_each_interface;
804 interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
805 interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
806 interfaces.driver_init = hostapd_driver_init;
807 interfaces.global_iface_path = NULL;
808 interfaces.global_iface_name = NULL;
809 interfaces.global_ctrl_sock = -1;
810 #ifdef CONFIG_IEEE80211BE
811 interfaces.mld_ctrl_iface_init = hostapd_mld_ctrl_iface_init;
812 interfaces.mld_ctrl_iface_deinit = hostapd_mld_ctrl_iface_deinit;
813 #endif /* CONFIG_IEEE80211BE */
814 dl_list_init(&interfaces.global_ctrl_dst);
815 #ifdef CONFIG_ETH_P_OUI
816 dl_list_init(&interfaces.eth_p_oui);
817 #endif /* CONFIG_ETH_P_OUI */
818 #ifdef CONFIG_DPP
819 os_memset(&dpp_conf, 0, sizeof(dpp_conf));
820 dpp_conf.cb_ctx = &interfaces;
821 #ifdef CONFIG_DPP2
822 dpp_conf.remove_bi = hostapd_dpp_remove_bi;
823 #endif /* CONFIG_DPP2 */
824 interfaces.dpp = dpp_global_init(&dpp_conf);
825 if (!interfaces.dpp)
826 return -1;
827 #endif /* CONFIG_DPP */
828
829 for (;;) {
830 c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:q");
831 if (c < 0)
832 break;
833 switch (c) {
834 case 'h':
835 usage();
836 break;
837 case 'd':
838 debug++;
839 if (wpa_debug_level > 0)
840 wpa_debug_level--;
841 break;
842 case 'B':
843 daemonize++;
844 break;
845 case 'e':
846 entropy_file = optarg;
847 break;
848 case 'f':
849 log_file = optarg;
850 break;
851 case 'K':
852 wpa_debug_show_keys++;
853 break;
854 case 'P':
855 os_free(pid_file);
856 pid_file = os_rel2abs_path(optarg);
857 break;
858 case 't':
859 wpa_debug_timestamp++;
860 break;
861 #ifdef CONFIG_DEBUG_LINUX_TRACING
862 case 'T':
863 enable_trace_dbg = 1;
864 break;
865 #endif /* CONFIG_DEBUG_LINUX_TRACING */
866 case 'v':
867 show_version();
868 exit(1);
869 case 'g':
870 if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
871 return -1;
872 break;
873 case 'G':
874 if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
875 return -1;
876 break;
877 case 'b':
878 tmp_bss = os_realloc_array(bss_config,
879 num_bss_configs + 1,
880 sizeof(char *));
881 if (tmp_bss == NULL)
882 goto out;
883 bss_config = tmp_bss;
884 bss_config[num_bss_configs++] = optarg;
885 break;
886 #ifdef CONFIG_DEBUG_SYSLOG
887 case 's':
888 wpa_debug_syslog = 1;
889 break;
890 #endif /* CONFIG_DEBUG_SYSLOG */
891 case 'S':
892 start_ifaces_in_sync = 1;
893 break;
894 #ifdef CONFIG_WPS
895 case 'u':
896 return gen_uuid(optarg);
897 #endif /* CONFIG_WPS */
898 case 'i':
899 if (hostapd_get_interface_names(&if_names,
900 &if_names_size, optarg))
901 goto out;
902 break;
903 case 'q':
904 wpa_debug_level++;
905 break;
906 default:
907 usage();
908 break;
909 }
910 }
911
912 #ifndef CONFIG_CTRL_IFACE_AIDL
913 if (optind == argc && interfaces.global_iface_path == NULL &&
914 num_bss_configs == 0)
915 usage();
916 #endif
917
918 wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
919
920 if (log_file)
921 wpa_debug_open_file(log_file);
922 if (!log_file && !wpa_debug_syslog)
923 wpa_debug_setup_stdout();
924 #ifdef CONFIG_DEBUG_SYSLOG
925 if (wpa_debug_syslog)
926 wpa_debug_open_syslog();
927 #endif /* CONFIG_DEBUG_SYSLOG */
928 #ifdef CONFIG_DEBUG_LINUX_TRACING
929 if (enable_trace_dbg) {
930 int tret = wpa_debug_open_linux_tracing();
931 if (tret) {
932 wpa_printf(MSG_ERROR, "Failed to enable trace logging");
933 return -1;
934 }
935 }
936 #endif /* CONFIG_DEBUG_LINUX_TRACING */
937
938 interfaces.count = argc - optind;
939 if (interfaces.count || num_bss_configs) {
940 interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
941 sizeof(struct hostapd_iface *));
942 if (interfaces.iface == NULL) {
943 wpa_printf(MSG_ERROR, "malloc failed");
944 return -1;
945 }
946 }
947
948 if (hostapd_global_init(&interfaces, entropy_file)) {
949 wpa_printf(MSG_ERROR, "Failed to initialize global context");
950 return -1;
951 }
952
953 eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
954 hostapd_periodic, &interfaces, NULL);
955
956 if (fst_global_init()) {
957 wpa_printf(MSG_ERROR,
958 "Failed to initialize global FST context");
959 goto out;
960 }
961
962 #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
963 if (!fst_global_add_ctrl(fst_ctrl_cli))
964 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
965 #endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
966
967 /* Allocate and parse configuration for full interface files */
968 for (i = 0; i < interfaces.count; i++) {
969 char *if_name = NULL;
970
971 if (i < if_names_size)
972 if_name = if_names[i];
973
974 interfaces.iface[i] = hostapd_interface_init(&interfaces,
975 if_name,
976 argv[optind + i],
977 debug);
978 if (!interfaces.iface[i]) {
979 wpa_printf(MSG_ERROR, "Failed to initialize interface");
980 goto out;
981 }
982 if (start_ifaces_in_sync)
983 interfaces.iface[i]->need_to_start_in_sync = 1;
984 }
985
986 /* Allocate and parse configuration for per-BSS files */
987 for (i = 0; i < num_bss_configs; i++) {
988 struct hostapd_iface *iface;
989 char *fname;
990
991 wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
992 fname = os_strchr(bss_config[i], ':');
993 if (fname == NULL) {
994 wpa_printf(MSG_ERROR,
995 "Invalid BSS config identifier '%s'",
996 bss_config[i]);
997 goto out;
998 }
999 *fname++ = '\0';
1000 iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
1001 fname, debug);
1002 if (iface == NULL)
1003 goto out;
1004 for (j = 0; j < interfaces.count; j++) {
1005 if (interfaces.iface[j] == iface)
1006 break;
1007 }
1008 if (j == interfaces.count) {
1009 struct hostapd_iface **tmp;
1010 tmp = os_realloc_array(interfaces.iface,
1011 interfaces.count + 1,
1012 sizeof(struct hostapd_iface *));
1013 if (tmp == NULL) {
1014 hostapd_interface_deinit_free(iface);
1015 goto out;
1016 }
1017 interfaces.iface = tmp;
1018 interfaces.iface[interfaces.count++] = iface;
1019 }
1020 }
1021
1022 /*
1023 * Enable configured interfaces. Depending on channel configuration,
1024 * this may complete full initialization before returning or use a
1025 * callback mechanism to complete setup in case of operations like HT
1026 * co-ex scans, ACS, or DFS are needed to determine channel parameters.
1027 * In such case, the interface will be enabled from eloop context within
1028 * hostapd_global_run().
1029 */
1030 interfaces.terminate_on_error = interfaces.count;
1031 for (i = 0; i < interfaces.count; i++) {
1032 if (hostapd_driver_init(interfaces.iface[i]) ||
1033 hostapd_setup_interface(interfaces.iface[i]))
1034 goto out;
1035 }
1036
1037 hostapd_global_ctrl_iface_init(&interfaces);
1038
1039 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
1040 wpa_printf(MSG_ERROR, "Failed to start eloop");
1041 goto out;
1042 }
1043
1044 ret = 0;
1045
1046 out:
1047 #ifdef CONFIG_CTRL_IFACE_AIDL
1048 hostapd_aidl_deinit(&interfaces);
1049 #endif /* CONFIG_CTRL_IFACE_AIDL */
1050 hostapd_global_ctrl_iface_deinit(&interfaces);
1051 /* Deinitialize all interfaces */
1052 for (i = 0; i < interfaces.count; i++) {
1053 if (!interfaces.iface[i])
1054 continue;
1055 interfaces.iface[i]->driver_ap_teardown =
1056 !!(interfaces.iface[i]->drv_flags &
1057 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1058 hostapd_interface_deinit_free(interfaces.iface[i]);
1059 interfaces.iface[i] = NULL;
1060 }
1061 os_free(interfaces.iface);
1062 interfaces.iface = NULL;
1063 interfaces.count = 0;
1064
1065 hostapd_global_cleanup_mld(&interfaces);
1066
1067 #ifdef CONFIG_DPP
1068 dpp_global_deinit(interfaces.dpp);
1069 #endif /* CONFIG_DPP */
1070
1071 if (interfaces.eloop_initialized)
1072 eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
1073 hostapd_global_deinit(pid_file, interfaces.eloop_initialized);
1074 os_free(pid_file);
1075
1076 wpa_debug_close_syslog();
1077 if (log_file)
1078 wpa_debug_close_file();
1079 wpa_debug_close_linux_tracing();
1080
1081 os_free(bss_config);
1082
1083 for (i = 0; i < if_names_size; i++)
1084 os_free(if_names[i]);
1085 os_free(if_names);
1086
1087 fst_global_deinit();
1088
1089 crypto_unload();
1090 os_program_deinit();
1091
1092 return ret;
1093 }
1094