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