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