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