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