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