Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * hostapd / main() |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3 | * Copyright (c) 2002-2022, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "utils/includes.h" |
| 10 | #ifndef CONFIG_NATIVE_WINDOWS |
| 11 | #include <syslog.h> |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 12 | #include <grp.h> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 13 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 14 | |
| 15 | #include "utils/common.h" |
| 16 | #include "utils/eloop.h" |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 17 | #include "utils/uuid.h" |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 18 | #include "crypto/crypto.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 19 | #include "crypto/random.h" |
| 20 | #include "crypto/tls.h" |
| 21 | #include "common/version.h" |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 22 | #include "common/dpp.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 28 | #include "ap/ap_drv_ops.h" |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 29 | #include "ap/dpp_hostapd.h" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 30 | #include "fst/fst.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 31 | #include "config_file.h" |
| 32 | #include "eap_register.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 33 | #include "ctrl_iface.h" |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 34 | #ifdef CONFIG_CTRL_IFACE_AIDL |
| 35 | #include "aidl.h" |
| 36 | #endif /* CONFIG_CTRL_IFACE_AIDL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 37 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 38 | struct hapd_global { |
| 39 | void **drv_priv; |
| 40 | size_t drv_count; |
| 41 | }; |
| 42 | |
| 43 | static struct hapd_global global; |
| 44 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 45 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 46 | #ifndef CONFIG_NO_HOSTAPD_LOGGER |
| 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; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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), |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 98 | module_str ? " " : "", module_str ? module_str : "", |
| 99 | txt); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 100 | else if (hapd && hapd->conf) |
| 101 | os_snprintf(format, maxlen, "%s:%s%s %s", |
| 102 | hapd->conf->iface, module_str ? " " : "", |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 103 | module_str ? module_str : "", txt); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 104 | else if (addr) |
| 105 | os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s", |
| 106 | MAC2STR(addr), module_str ? " " : "", |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 107 | module_str ? module_str : "", txt); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 108 | else |
| 109 | os_snprintf(format, maxlen, "%s%s%s", |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 110 | module_str ? module_str : "", |
| 111 | module_str ? ": " : "", txt); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 112 | |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 113 | #ifdef CONFIG_DEBUG_SYSLOG |
| 114 | if (wpa_debug_syslog) |
| 115 | conf_stdout = 0; |
| 116 | #endif /* CONFIG_DEBUG_SYSLOG */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 117 | if ((conf_stdout & module) && level >= conf_stdout_level) { |
| 118 | wpa_debug_print_timestamp(); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 119 | wpa_printf(MSG_INFO, "%s", format); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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 | /** |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 153 | * hostapd_driver_init - Preparate driver interface |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 154 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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 | |
| 164 | if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) { |
| 165 | wpa_printf(MSG_ERROR, "No hostapd driver wrapper available"); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | /* Initialize the driver interface */ |
| 170 | if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5])) |
| 171 | b = NULL; |
| 172 | |
| 173 | os_memset(¶ms, 0, sizeof(params)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 174 | for (i = 0; wpa_drivers[i]; i++) { |
| 175 | if (wpa_drivers[i] != hapd->driver) |
| 176 | continue; |
| 177 | |
| 178 | if (global.drv_priv[i] == NULL && |
| 179 | wpa_drivers[i]->global_init) { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 180 | global.drv_priv[i] = |
| 181 | wpa_drivers[i]->global_init(iface->interfaces); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 182 | if (global.drv_priv[i] == NULL) { |
| 183 | wpa_printf(MSG_ERROR, "Failed to initialize " |
| 184 | "driver '%s'", |
| 185 | wpa_drivers[i]->name); |
| 186 | return -1; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | params.global_priv = global.drv_priv[i]; |
| 191 | break; |
| 192 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 193 | params.bssid = b; |
| 194 | params.ifname = hapd->conf->iface; |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 195 | params.driver_params = hapd->iconf->driver_params; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 196 | params.use_pae_group_addr = hapd->conf->use_pae_group_addr; |
| 197 | |
| 198 | params.num_bridge = hapd->iface->num_bss; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 199 | params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 200 | if (params.bridge == NULL) |
| 201 | return -1; |
| 202 | for (i = 0; i < hapd->iface->num_bss; i++) { |
| 203 | struct hostapd_data *bss = hapd->iface->bss[i]; |
| 204 | if (bss->conf->bridge[0]) |
| 205 | params.bridge[i] = bss->conf->bridge; |
| 206 | } |
| 207 | |
| 208 | params.own_addr = hapd->own_addr; |
| 209 | |
| 210 | hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms); |
| 211 | os_free(params.bridge); |
| 212 | if (hapd->drv_priv == NULL) { |
| 213 | wpa_printf(MSG_ERROR, "%s driver initialization failed.", |
| 214 | hapd->driver->name); |
| 215 | hapd->driver = NULL; |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | if (hapd->driver->get_capa && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 220 | hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) { |
Dmitry Shmidt | 0207e23 | 2014-09-03 14:58:37 -0700 | [diff] [blame] | 221 | struct wowlan_triggers *triggs; |
| 222 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 223 | iface->drv_flags = capa.flags; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 224 | iface->drv_flags2 = capa.flags2; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 225 | iface->probe_resp_offloads = capa.probe_resp_offloads; |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 226 | /* |
| 227 | * Use default extended capa values from per-radio information |
| 228 | */ |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 229 | iface->extended_capa = capa.extended_capa; |
| 230 | iface->extended_capa_mask = capa.extended_capa_mask; |
| 231 | iface->extended_capa_len = capa.extended_capa_len; |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 232 | iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs; |
Dmitry Shmidt | 0207e23 | 2014-09-03 14:58:37 -0700 | [diff] [blame] | 233 | |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 234 | /* |
| 235 | * Override extended capa with per-interface type (AP), if |
| 236 | * available from the driver. |
| 237 | */ |
| 238 | hostapd_get_ext_capa(iface); |
| 239 | |
Dmitry Shmidt | 0207e23 | 2014-09-03 14:58:37 -0700 | [diff] [blame] | 240 | triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa); |
| 241 | if (triggs && hapd->driver->set_wowlan) { |
| 242 | if (hapd->driver->set_wowlan(hapd->drv_priv, triggs)) |
| 243 | wpa_printf(MSG_ERROR, "set_wowlan failed"); |
| 244 | } |
| 245 | os_free(triggs); |
Sunil Ravi | 77d572f | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 246 | |
| 247 | iface->mbssid_max_interfaces = capa.mbssid_max_interfaces; |
| 248 | iface->ema_max_periodicity = capa.ema_max_periodicity; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 249 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 255 | /** |
| 256 | * hostapd_interface_init - Read configuration file and init BSS data |
| 257 | * |
| 258 | * This function is used to parse configuration file for a full interface (one |
| 259 | * or more BSSes sharing the same radio) and allocate memory for the BSS |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 260 | * interfaces. No actual driver operations are started. |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 261 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 262 | static struct hostapd_iface * |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 263 | hostapd_interface_init(struct hapd_interfaces *interfaces, const char *if_name, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 264 | const char *config_fname, int debug) |
| 265 | { |
| 266 | struct hostapd_iface *iface; |
| 267 | int k; |
| 268 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 269 | wpa_printf(MSG_DEBUG, "Configuration file: %s", config_fname); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 270 | iface = hostapd_init(interfaces, config_fname); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 271 | if (!iface) |
| 272 | return NULL; |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 273 | |
| 274 | if (if_name) { |
| 275 | os_strlcpy(iface->conf->bss[0]->iface, if_name, |
| 276 | sizeof(iface->conf->bss[0]->iface)); |
| 277 | } |
| 278 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 279 | iface->interfaces = interfaces; |
| 280 | |
| 281 | for (k = 0; k < debug; k++) { |
| 282 | if (iface->bss[0]->conf->logger_stdout_level > 0) |
| 283 | iface->bss[0]->conf->logger_stdout_level--; |
| 284 | } |
| 285 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 286 | if (iface->conf->bss[0]->iface[0] == '\0' && |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 287 | !hostapd_drv_none(iface->bss[0])) { |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 288 | wpa_printf(MSG_ERROR, |
| 289 | "Interface name not specified in %s, nor by '-i' parameter", |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 290 | config_fname); |
| 291 | hostapd_interface_deinit_free(iface); |
| 292 | return NULL; |
| 293 | } |
| 294 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 295 | return iface; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | /** |
| 300 | * handle_term - SIGINT and SIGTERM handler to terminate hostapd process |
| 301 | */ |
| 302 | static void handle_term(int sig, void *signal_ctx) |
| 303 | { |
| 304 | wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig); |
| 305 | eloop_terminate(); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | #ifndef CONFIG_NATIVE_WINDOWS |
| 310 | |
| 311 | static int handle_reload_iface(struct hostapd_iface *iface, void *ctx) |
| 312 | { |
| 313 | if (hostapd_reload_config(iface) < 0) { |
| 314 | wpa_printf(MSG_WARNING, "Failed to read new configuration " |
| 315 | "file - continuing with old."); |
| 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | /** |
| 322 | * handle_reload - SIGHUP handler to reload configuration |
| 323 | */ |
| 324 | static void handle_reload(int sig, void *signal_ctx) |
| 325 | { |
| 326 | struct hapd_interfaces *interfaces = signal_ctx; |
| 327 | wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration", |
| 328 | sig); |
| 329 | hostapd_for_each_interface(interfaces, handle_reload_iface, NULL); |
| 330 | } |
| 331 | |
| 332 | |
| 333 | static void handle_dump_state(int sig, void *signal_ctx) |
| 334 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 335 | /* Not used anymore - ignore signal */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 336 | } |
| 337 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 338 | |
| 339 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 340 | static int hostapd_global_init(struct hapd_interfaces *interfaces, |
| 341 | const char *entropy_file) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 342 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 343 | int i; |
| 344 | |
| 345 | os_memset(&global, 0, sizeof(global)); |
| 346 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 347 | hostapd_logger_register_cb(hostapd_logger_cb); |
| 348 | |
| 349 | if (eap_server_register_methods()) { |
| 350 | wpa_printf(MSG_ERROR, "Failed to register EAP methods"); |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | if (eloop_init()) { |
| 355 | wpa_printf(MSG_ERROR, "Failed to initialize event loop"); |
| 356 | return -1; |
| 357 | } |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 358 | interfaces->eloop_initialized = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 359 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 360 | random_init(entropy_file); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 361 | |
| 362 | #ifndef CONFIG_NATIVE_WINDOWS |
| 363 | eloop_register_signal(SIGHUP, handle_reload, interfaces); |
| 364 | eloop_register_signal(SIGUSR1, handle_dump_state, interfaces); |
| 365 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 366 | eloop_register_signal_terminate(handle_term, interfaces); |
| 367 | |
| 368 | #ifndef CONFIG_NATIVE_WINDOWS |
| 369 | openlog("hostapd", 0, LOG_DAEMON); |
| 370 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 371 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 372 | for (i = 0; wpa_drivers[i]; i++) |
| 373 | global.drv_count++; |
| 374 | if (global.drv_count == 0) { |
| 375 | wpa_printf(MSG_ERROR, "No drivers enabled"); |
| 376 | return -1; |
| 377 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 378 | global.drv_priv = os_calloc(global.drv_count, sizeof(void *)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 379 | if (global.drv_priv == NULL) |
| 380 | return -1; |
| 381 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 386 | static void hostapd_global_deinit(const char *pid_file, int eloop_initialized) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 387 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 388 | int i; |
| 389 | |
| 390 | for (i = 0; wpa_drivers[i] && global.drv_priv; i++) { |
| 391 | if (!global.drv_priv[i]) |
| 392 | continue; |
| 393 | wpa_drivers[i]->global_deinit(global.drv_priv[i]); |
| 394 | } |
| 395 | os_free(global.drv_priv); |
| 396 | global.drv_priv = NULL; |
| 397 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 398 | #ifdef EAP_SERVER_TNC |
| 399 | tncs_global_deinit(); |
| 400 | #endif /* EAP_SERVER_TNC */ |
| 401 | |
| 402 | random_deinit(); |
| 403 | |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 404 | if (eloop_initialized) |
| 405 | eloop_destroy(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 406 | |
| 407 | #ifndef CONFIG_NATIVE_WINDOWS |
| 408 | closelog(); |
| 409 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 410 | |
| 411 | eap_server_unregister_methods(); |
| 412 | |
| 413 | os_daemonize_terminate(pid_file); |
| 414 | } |
| 415 | |
| 416 | |
| 417 | static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize, |
| 418 | const char *pid_file) |
| 419 | { |
| 420 | #ifdef EAP_SERVER_TNC |
| 421 | int tnc = 0; |
| 422 | size_t i, k; |
| 423 | |
| 424 | for (i = 0; !tnc && i < ifaces->count; i++) { |
| 425 | for (k = 0; k < ifaces->iface[i]->num_bss; k++) { |
| 426 | if (ifaces->iface[i]->bss[0]->conf->tnc) { |
| 427 | tnc++; |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if (tnc && tncs_global_init() < 0) { |
| 434 | wpa_printf(MSG_ERROR, "Failed to initialize TNCS"); |
| 435 | return -1; |
| 436 | } |
| 437 | #endif /* EAP_SERVER_TNC */ |
| 438 | |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 439 | if (daemonize) { |
| 440 | if (os_daemonize(pid_file)) { |
| 441 | wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno)); |
| 442 | return -1; |
| 443 | } |
| 444 | if (eloop_sock_requeue()) { |
| 445 | wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s", |
| 446 | strerror(errno)); |
| 447 | return -1; |
| 448 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Veerendranath Jakkam | 9d0d521 | 2023-02-21 19:55:58 +0530 | [diff] [blame] | 451 | #ifdef CONFIG_CTRL_IFACE_AIDL |
| 452 | if (hostapd_aidl_init(ifaces)) { |
| 453 | wpa_printf(MSG_ERROR, "Failed to initialize AIDL interface"); |
| 454 | return -1; |
| 455 | } |
| 456 | #endif /* CONFIG_CTRL_IFACE_AIDL */ |
| 457 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 458 | eloop_run(); |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | |
| 464 | static void show_version(void) |
| 465 | { |
| 466 | fprintf(stderr, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 467 | "hostapd v%s\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 468 | "User space daemon for IEEE 802.11 AP management,\n" |
| 469 | "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n" |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 470 | "Copyright (c) 2002-2022, Jouni Malinen <j@w1.fi> " |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 471 | "and contributors\n", |
| 472 | VERSION_STR); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | |
| 476 | static void usage(void) |
| 477 | { |
| 478 | show_version(); |
| 479 | fprintf(stderr, |
| 480 | "\n" |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 481 | "usage: hostapd [-hdBKtvq] [-P <PID file>] [-e <entropy file>] " |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 482 | "\\\n" |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 483 | " [-g <global ctrl_iface>] [-G <group>]\\\n" |
| 484 | " [-i <comma-separated list of interface names>]\\\n" |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 485 | " <configuration file(s)>\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 486 | "\n" |
| 487 | "options:\n" |
| 488 | " -h show this usage\n" |
| 489 | " -d show more debug messages (-dd for even more)\n" |
| 490 | " -B run daemon in the background\n" |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 491 | " -e entropy file\n" |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 492 | " -g global control interface path\n" |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 493 | " -G group for control interfaces\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 494 | " -P PID file\n" |
| 495 | " -K include key data in debug messages\n" |
| 496 | #ifdef CONFIG_DEBUG_FILE |
| 497 | " -f log output to debug file instead of stdout\n" |
| 498 | #endif /* CONFIG_DEBUG_FILE */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 499 | #ifdef CONFIG_DEBUG_LINUX_TRACING |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 500 | " -T record to Linux tracing in addition to logging\n" |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 501 | " (records all messages regardless of debug verbosity)\n" |
| 502 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 503 | " -i list of interface names to use\n" |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 504 | #ifdef CONFIG_DEBUG_SYSLOG |
| 505 | " -s log output to syslog instead of stdout\n" |
| 506 | #endif /* CONFIG_DEBUG_SYSLOG */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 507 | " -S start all the interfaces synchronously\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 508 | " -t include timestamps in some debug messages\n" |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 509 | " -v show hostapd version\n" |
| 510 | " -q show less debug messages (-qq for even less)\n"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 511 | |
| 512 | exit(1); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | static const char * hostapd_msg_ifname_cb(void *ctx) |
| 517 | { |
| 518 | struct hostapd_data *hapd = ctx; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 519 | if (hapd && hapd->conf) |
| 520 | return hapd->conf->iface; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 521 | return NULL; |
| 522 | } |
| 523 | |
| 524 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 525 | static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces, |
| 526 | const char *path) |
| 527 | { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 528 | #ifndef CONFIG_CTRL_IFACE_UDP |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 529 | char *pos; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 530 | #endif /* !CONFIG_CTRL_IFACE_UDP */ |
| 531 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 532 | os_free(interfaces->global_iface_path); |
| 533 | interfaces->global_iface_path = os_strdup(path); |
| 534 | if (interfaces->global_iface_path == NULL) |
| 535 | return -1; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 536 | |
| 537 | #ifndef CONFIG_CTRL_IFACE_UDP |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 538 | pos = os_strrchr(interfaces->global_iface_path, '/'); |
| 539 | if (pos == NULL) { |
Dmitry Shmidt | 1e78e76 | 2013-04-02 11:05:36 -0700 | [diff] [blame] | 540 | wpa_printf(MSG_ERROR, "No '/' in the global control interface " |
| 541 | "file"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 542 | os_free(interfaces->global_iface_path); |
| 543 | interfaces->global_iface_path = NULL; |
| 544 | return -1; |
| 545 | } |
| 546 | |
| 547 | *pos = '\0'; |
| 548 | interfaces->global_iface_name = pos + 1; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 549 | #endif /* !CONFIG_CTRL_IFACE_UDP */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 555 | static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces, |
| 556 | const char *group) |
| 557 | { |
| 558 | #ifndef CONFIG_NATIVE_WINDOWS |
| 559 | struct group *grp; |
| 560 | grp = getgrnam(group); |
| 561 | if (grp == NULL) { |
| 562 | wpa_printf(MSG_ERROR, "Unknown group '%s'", group); |
| 563 | return -1; |
| 564 | } |
| 565 | interfaces->ctrl_iface_group = grp->gr_gid; |
| 566 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 571 | static int hostapd_get_interface_names(char ***if_names, |
| 572 | size_t *if_names_size, |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 573 | char *arg) |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 574 | { |
| 575 | char *if_name, *tmp, **nnames; |
| 576 | size_t i; |
| 577 | |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 578 | if (!arg) |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 579 | return -1; |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 580 | if_name = strtok_r(arg, ",", &tmp); |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 581 | |
| 582 | while (if_name) { |
| 583 | nnames = os_realloc_array(*if_names, 1 + *if_names_size, |
| 584 | sizeof(char *)); |
| 585 | if (!nnames) |
| 586 | goto fail; |
| 587 | *if_names = nnames; |
| 588 | |
| 589 | (*if_names)[*if_names_size] = os_strdup(if_name); |
| 590 | if (!(*if_names)[*if_names_size]) |
| 591 | goto fail; |
| 592 | (*if_names_size)++; |
| 593 | if_name = strtok_r(NULL, ",", &tmp); |
| 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | |
| 598 | fail: |
| 599 | for (i = 0; i < *if_names_size; i++) |
| 600 | os_free((*if_names)[i]); |
| 601 | os_free(*if_names); |
| 602 | *if_names = NULL; |
| 603 | *if_names_size = 0; |
| 604 | return -1; |
| 605 | } |
| 606 | |
| 607 | |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 608 | #ifdef CONFIG_WPS |
| 609 | static int gen_uuid(const char *txt_addr) |
| 610 | { |
| 611 | u8 addr[ETH_ALEN]; |
| 612 | u8 uuid[UUID_LEN]; |
| 613 | char buf[100]; |
| 614 | |
| 615 | if (hwaddr_aton(txt_addr, addr) < 0) |
| 616 | return -1; |
| 617 | |
| 618 | uuid_gen_mac_addr(addr, uuid); |
| 619 | if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0) |
| 620 | return -1; |
| 621 | |
| 622 | printf("%s\n", buf); |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | #endif /* CONFIG_WPS */ |
| 627 | |
| 628 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 629 | #ifndef HOSTAPD_CLEANUP_INTERVAL |
| 630 | #define HOSTAPD_CLEANUP_INTERVAL 10 |
| 631 | #endif /* HOSTAPD_CLEANUP_INTERVAL */ |
| 632 | |
| 633 | static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx) |
| 634 | { |
| 635 | hostapd_periodic_iface(iface); |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | |
| 640 | /* Periodic cleanup tasks */ |
| 641 | static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx) |
| 642 | { |
| 643 | struct hapd_interfaces *interfaces = eloop_ctx; |
| 644 | |
| 645 | eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0, |
| 646 | hostapd_periodic, interfaces, NULL); |
| 647 | hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL); |
| 648 | } |
| 649 | |
| 650 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 651 | int main(int argc, char *argv[]) |
| 652 | { |
| 653 | struct hapd_interfaces interfaces; |
| 654 | int ret = 1; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 655 | size_t i, j; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 656 | int c, debug = 0, daemonize = 0; |
| 657 | char *pid_file = NULL; |
| 658 | const char *log_file = NULL; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 659 | const char *entropy_file = NULL; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 660 | char **bss_config = NULL, **tmp_bss; |
| 661 | size_t num_bss_configs = 0; |
| 662 | #ifdef CONFIG_DEBUG_LINUX_TRACING |
| 663 | int enable_trace_dbg = 0; |
| 664 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 665 | int start_ifaces_in_sync = 0; |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 666 | char **if_names = NULL; |
| 667 | size_t if_names_size = 0; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 668 | #ifdef CONFIG_DPP |
| 669 | struct dpp_global_config dpp_conf; |
| 670 | #endif /* CONFIG_DPP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 671 | |
| 672 | if (os_program_init()) |
| 673 | return -1; |
| 674 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 675 | os_memset(&interfaces, 0, sizeof(interfaces)); |
| 676 | interfaces.reload_config = hostapd_reload_config; |
| 677 | interfaces.config_read_cb = hostapd_config_read; |
| 678 | interfaces.for_each_interface = hostapd_for_each_interface; |
| 679 | interfaces.ctrl_iface_init = hostapd_ctrl_iface_init; |
| 680 | interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit; |
| 681 | interfaces.driver_init = hostapd_driver_init; |
| 682 | interfaces.global_iface_path = NULL; |
| 683 | interfaces.global_iface_name = NULL; |
| 684 | interfaces.global_ctrl_sock = -1; |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 685 | dl_list_init(&interfaces.global_ctrl_dst); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 686 | #ifdef CONFIG_ETH_P_OUI |
| 687 | dl_list_init(&interfaces.eth_p_oui); |
| 688 | #endif /* CONFIG_ETH_P_OUI */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 689 | #ifdef CONFIG_DPP |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 690 | os_memset(&dpp_conf, 0, sizeof(dpp_conf)); |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 691 | dpp_conf.cb_ctx = &interfaces; |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 692 | #ifdef CONFIG_DPP2 |
| 693 | dpp_conf.remove_bi = hostapd_dpp_remove_bi; |
| 694 | #endif /* CONFIG_DPP2 */ |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 695 | interfaces.dpp = dpp_global_init(&dpp_conf); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 696 | if (!interfaces.dpp) |
| 697 | return -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 698 | #endif /* CONFIG_DPP */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 699 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 700 | for (;;) { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 701 | c = getopt(argc, argv, "b:Bde:f:hi:KP:sSTtu:vg:G:q"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 702 | if (c < 0) |
| 703 | break; |
| 704 | switch (c) { |
| 705 | case 'h': |
| 706 | usage(); |
| 707 | break; |
| 708 | case 'd': |
| 709 | debug++; |
| 710 | if (wpa_debug_level > 0) |
| 711 | wpa_debug_level--; |
| 712 | break; |
| 713 | case 'B': |
| 714 | daemonize++; |
| 715 | break; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 716 | case 'e': |
| 717 | entropy_file = optarg; |
| 718 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 719 | case 'f': |
| 720 | log_file = optarg; |
| 721 | break; |
| 722 | case 'K': |
| 723 | wpa_debug_show_keys++; |
| 724 | break; |
| 725 | case 'P': |
| 726 | os_free(pid_file); |
| 727 | pid_file = os_rel2abs_path(optarg); |
| 728 | break; |
| 729 | case 't': |
| 730 | wpa_debug_timestamp++; |
| 731 | break; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 732 | #ifdef CONFIG_DEBUG_LINUX_TRACING |
| 733 | case 'T': |
| 734 | enable_trace_dbg = 1; |
| 735 | break; |
| 736 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 737 | case 'v': |
| 738 | show_version(); |
| 739 | exit(1); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 740 | case 'g': |
Dmitry Shmidt | 1e78e76 | 2013-04-02 11:05:36 -0700 | [diff] [blame] | 741 | if (hostapd_get_global_ctrl_iface(&interfaces, optarg)) |
| 742 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 743 | break; |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 744 | case 'G': |
Dmitry Shmidt | 1e78e76 | 2013-04-02 11:05:36 -0700 | [diff] [blame] | 745 | if (hostapd_get_ctrl_iface_group(&interfaces, optarg)) |
| 746 | return -1; |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 747 | break; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 748 | case 'b': |
| 749 | tmp_bss = os_realloc_array(bss_config, |
| 750 | num_bss_configs + 1, |
| 751 | sizeof(char *)); |
| 752 | if (tmp_bss == NULL) |
| 753 | goto out; |
| 754 | bss_config = tmp_bss; |
| 755 | bss_config[num_bss_configs++] = optarg; |
| 756 | break; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 757 | #ifdef CONFIG_DEBUG_SYSLOG |
| 758 | case 's': |
| 759 | wpa_debug_syslog = 1; |
| 760 | break; |
| 761 | #endif /* CONFIG_DEBUG_SYSLOG */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 762 | case 'S': |
| 763 | start_ifaces_in_sync = 1; |
| 764 | break; |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 765 | #ifdef CONFIG_WPS |
| 766 | case 'u': |
| 767 | return gen_uuid(optarg); |
| 768 | #endif /* CONFIG_WPS */ |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 769 | case 'i': |
| 770 | if (hostapd_get_interface_names(&if_names, |
| 771 | &if_names_size, optarg)) |
| 772 | goto out; |
| 773 | break; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 774 | case 'q': |
| 775 | wpa_debug_level++; |
| 776 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 777 | default: |
| 778 | usage(); |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 783 | #ifndef CONFIG_CTRL_IFACE_AIDL |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 784 | if (optind == argc && interfaces.global_iface_path == NULL && |
| 785 | num_bss_configs == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 786 | usage(); |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 787 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 788 | |
| 789 | wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb); |
| 790 | |
| 791 | if (log_file) |
| 792 | wpa_debug_open_file(log_file); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 793 | if (!log_file && !wpa_debug_syslog) |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 794 | wpa_debug_setup_stdout(); |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 795 | #ifdef CONFIG_DEBUG_SYSLOG |
| 796 | if (wpa_debug_syslog) |
| 797 | wpa_debug_open_syslog(); |
| 798 | #endif /* CONFIG_DEBUG_SYSLOG */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 799 | #ifdef CONFIG_DEBUG_LINUX_TRACING |
| 800 | if (enable_trace_dbg) { |
| 801 | int tret = wpa_debug_open_linux_tracing(); |
| 802 | if (tret) { |
| 803 | wpa_printf(MSG_ERROR, "Failed to enable trace logging"); |
| 804 | return -1; |
| 805 | } |
| 806 | } |
| 807 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 808 | |
| 809 | interfaces.count = argc - optind; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 810 | if (interfaces.count || num_bss_configs) { |
| 811 | interfaces.iface = os_calloc(interfaces.count + num_bss_configs, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 812 | sizeof(struct hostapd_iface *)); |
| 813 | if (interfaces.iface == NULL) { |
| 814 | wpa_printf(MSG_ERROR, "malloc failed"); |
| 815 | return -1; |
| 816 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 819 | if (hostapd_global_init(&interfaces, entropy_file)) { |
Dmitry Shmidt | 7a53dbb | 2015-06-11 13:13:53 -0700 | [diff] [blame] | 820 | wpa_printf(MSG_ERROR, "Failed to initialize global context"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 821 | return -1; |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 822 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 823 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 824 | eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0, |
| 825 | hostapd_periodic, &interfaces, NULL); |
| 826 | |
| 827 | if (fst_global_init()) { |
| 828 | wpa_printf(MSG_ERROR, |
| 829 | "Failed to initialize global FST context"); |
| 830 | goto out; |
| 831 | } |
| 832 | |
| 833 | #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE) |
| 834 | if (!fst_global_add_ctrl(fst_ctrl_cli)) |
| 835 | wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl"); |
| 836 | #endif /* CONFIG_FST && CONFIG_CTRL_IFACE */ |
| 837 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 838 | /* Allocate and parse configuration for full interface files */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 839 | for (i = 0; i < interfaces.count; i++) { |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 840 | char *if_name = NULL; |
| 841 | |
| 842 | if (i < if_names_size) |
| 843 | if_name = if_names[i]; |
| 844 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 845 | interfaces.iface[i] = hostapd_interface_init(&interfaces, |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 846 | if_name, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 847 | argv[optind + i], |
| 848 | debug); |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 849 | if (!interfaces.iface[i]) { |
| 850 | wpa_printf(MSG_ERROR, "Failed to initialize interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 851 | goto out; |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 852 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 853 | if (start_ifaces_in_sync) |
| 854 | interfaces.iface[i]->need_to_start_in_sync = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 857 | /* Allocate and parse configuration for per-BSS files */ |
| 858 | for (i = 0; i < num_bss_configs; i++) { |
| 859 | struct hostapd_iface *iface; |
| 860 | char *fname; |
| 861 | |
| 862 | wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]); |
| 863 | fname = os_strchr(bss_config[i], ':'); |
| 864 | if (fname == NULL) { |
| 865 | wpa_printf(MSG_ERROR, |
| 866 | "Invalid BSS config identifier '%s'", |
| 867 | bss_config[i]); |
| 868 | goto out; |
| 869 | } |
| 870 | *fname++ = '\0'; |
| 871 | iface = hostapd_interface_init_bss(&interfaces, bss_config[i], |
| 872 | fname, debug); |
| 873 | if (iface == NULL) |
| 874 | goto out; |
| 875 | for (j = 0; j < interfaces.count; j++) { |
| 876 | if (interfaces.iface[j] == iface) |
| 877 | break; |
| 878 | } |
| 879 | if (j == interfaces.count) { |
| 880 | struct hostapd_iface **tmp; |
| 881 | tmp = os_realloc_array(interfaces.iface, |
| 882 | interfaces.count + 1, |
| 883 | sizeof(struct hostapd_iface *)); |
| 884 | if (tmp == NULL) { |
| 885 | hostapd_interface_deinit_free(iface); |
| 886 | goto out; |
| 887 | } |
| 888 | interfaces.iface = tmp; |
| 889 | interfaces.iface[interfaces.count++] = iface; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * Enable configured interfaces. Depending on channel configuration, |
| 895 | * this may complete full initialization before returning or use a |
| 896 | * callback mechanism to complete setup in case of operations like HT |
| 897 | * co-ex scans, ACS, or DFS are needed to determine channel parameters. |
| 898 | * In such case, the interface will be enabled from eloop context within |
| 899 | * hostapd_global_run(). |
| 900 | */ |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 901 | interfaces.terminate_on_error = interfaces.count; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 902 | for (i = 0; i < interfaces.count; i++) { |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 903 | if (hostapd_driver_init(interfaces.iface[i]) || |
| 904 | hostapd_setup_interface(interfaces.iface[i])) |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 905 | goto out; |
| 906 | } |
| 907 | |
Daichi Ueura | caa74a8 | 2018-02-14 22:25:39 +0900 | [diff] [blame] | 908 | hostapd_global_ctrl_iface_init(&interfaces); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 909 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 910 | if (hostapd_global_run(&interfaces, daemonize, pid_file)) { |
| 911 | wpa_printf(MSG_ERROR, "Failed to start eloop"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 912 | goto out; |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 913 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 914 | |
| 915 | ret = 0; |
| 916 | |
| 917 | out: |
Gabriel Biren | 72cf9a5 | 2021-06-25 23:29:26 +0000 | [diff] [blame] | 918 | #ifdef CONFIG_CTRL_IFACE_AIDL |
| 919 | hostapd_aidl_deinit(&interfaces); |
| 920 | #endif /* CONFIG_CTRL_IFACE_AIDL */ |
Daichi Ueura | caa74a8 | 2018-02-14 22:25:39 +0900 | [diff] [blame] | 921 | hostapd_global_ctrl_iface_deinit(&interfaces); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 922 | /* Deinitialize all interfaces */ |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 923 | for (i = 0; i < interfaces.count; i++) { |
Dmitry Shmidt | 818ea48 | 2014-03-10 13:15:21 -0700 | [diff] [blame] | 924 | if (!interfaces.iface[i]) |
| 925 | continue; |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 926 | interfaces.iface[i]->driver_ap_teardown = |
| 927 | !!(interfaces.iface[i]->drv_flags & |
| 928 | WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 929 | hostapd_interface_deinit_free(interfaces.iface[i]); |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 930 | interfaces.iface[i] = NULL; |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 931 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 932 | os_free(interfaces.iface); |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 933 | interfaces.iface = NULL; |
| 934 | interfaces.count = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 935 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 936 | #ifdef CONFIG_DPP |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 937 | dpp_global_deinit(interfaces.dpp); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 938 | #endif /* CONFIG_DPP */ |
| 939 | |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 940 | if (interfaces.eloop_initialized) |
| 941 | eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL); |
| 942 | hostapd_global_deinit(pid_file, interfaces.eloop_initialized); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 943 | os_free(pid_file); |
| 944 | |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 945 | wpa_debug_close_syslog(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 946 | if (log_file) |
| 947 | wpa_debug_close_file(); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 948 | wpa_debug_close_linux_tracing(); |
| 949 | |
| 950 | os_free(bss_config); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 951 | |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 952 | for (i = 0; i < if_names_size; i++) |
| 953 | os_free(if_names[i]); |
| 954 | os_free(if_names); |
| 955 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 956 | fst_global_deinit(); |
| 957 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 958 | crypto_unload(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 959 | os_program_deinit(); |
| 960 | |
| 961 | return ret; |
| 962 | } |