Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * hostapd / main() |
| 3 | * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi> |
| 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" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 27 | #include "config_file.h" |
| 28 | #include "eap_register.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 29 | #include "ctrl_iface.h" |
| 30 | |
| 31 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 32 | struct hapd_global { |
| 33 | void **drv_priv; |
| 34 | size_t drv_count; |
| 35 | }; |
| 36 | |
| 37 | static struct hapd_global global; |
| 38 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 39 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 40 | #ifndef CONFIG_NO_HOSTAPD_LOGGER |
| 41 | static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module, |
| 42 | int level, const char *txt, size_t len) |
| 43 | { |
| 44 | struct hostapd_data *hapd = ctx; |
| 45 | char *format, *module_str; |
| 46 | int maxlen; |
| 47 | int conf_syslog_level, conf_stdout_level; |
| 48 | unsigned int conf_syslog, conf_stdout; |
| 49 | |
| 50 | maxlen = len + 100; |
| 51 | format = os_malloc(maxlen); |
| 52 | if (!format) |
| 53 | return; |
| 54 | |
| 55 | if (hapd && hapd->conf) { |
| 56 | conf_syslog_level = hapd->conf->logger_syslog_level; |
| 57 | conf_stdout_level = hapd->conf->logger_stdout_level; |
| 58 | conf_syslog = hapd->conf->logger_syslog; |
| 59 | conf_stdout = hapd->conf->logger_stdout; |
| 60 | } else { |
| 61 | conf_syslog_level = conf_stdout_level = 0; |
| 62 | conf_syslog = conf_stdout = (unsigned int) -1; |
| 63 | } |
| 64 | |
| 65 | switch (module) { |
| 66 | case HOSTAPD_MODULE_IEEE80211: |
| 67 | module_str = "IEEE 802.11"; |
| 68 | break; |
| 69 | case HOSTAPD_MODULE_IEEE8021X: |
| 70 | module_str = "IEEE 802.1X"; |
| 71 | break; |
| 72 | case HOSTAPD_MODULE_RADIUS: |
| 73 | module_str = "RADIUS"; |
| 74 | break; |
| 75 | case HOSTAPD_MODULE_WPA: |
| 76 | module_str = "WPA"; |
| 77 | break; |
| 78 | case HOSTAPD_MODULE_DRIVER: |
| 79 | module_str = "DRIVER"; |
| 80 | break; |
| 81 | case HOSTAPD_MODULE_IAPP: |
| 82 | module_str = "IAPP"; |
| 83 | break; |
| 84 | case HOSTAPD_MODULE_MLME: |
| 85 | module_str = "MLME"; |
| 86 | break; |
| 87 | default: |
| 88 | module_str = NULL; |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | if (hapd && hapd->conf && addr) |
| 93 | os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s", |
| 94 | hapd->conf->iface, MAC2STR(addr), |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 95 | module_str ? " " : "", module_str ? module_str : "", |
| 96 | txt); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 97 | else if (hapd && hapd->conf) |
| 98 | os_snprintf(format, maxlen, "%s:%s%s %s", |
| 99 | hapd->conf->iface, module_str ? " " : "", |
| 100 | module_str, txt); |
| 101 | else if (addr) |
| 102 | os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s", |
| 103 | MAC2STR(addr), module_str ? " " : "", |
| 104 | module_str, txt); |
| 105 | else |
| 106 | os_snprintf(format, maxlen, "%s%s%s", |
| 107 | module_str, module_str ? ": " : "", txt); |
| 108 | |
| 109 | if ((conf_stdout & module) && level >= conf_stdout_level) { |
| 110 | wpa_debug_print_timestamp(); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 111 | wpa_printf(MSG_INFO, "%s", format); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | #ifndef CONFIG_NATIVE_WINDOWS |
| 115 | if ((conf_syslog & module) && level >= conf_syslog_level) { |
| 116 | int priority; |
| 117 | switch (level) { |
| 118 | case HOSTAPD_LEVEL_DEBUG_VERBOSE: |
| 119 | case HOSTAPD_LEVEL_DEBUG: |
| 120 | priority = LOG_DEBUG; |
| 121 | break; |
| 122 | case HOSTAPD_LEVEL_INFO: |
| 123 | priority = LOG_INFO; |
| 124 | break; |
| 125 | case HOSTAPD_LEVEL_NOTICE: |
| 126 | priority = LOG_NOTICE; |
| 127 | break; |
| 128 | case HOSTAPD_LEVEL_WARNING: |
| 129 | priority = LOG_WARNING; |
| 130 | break; |
| 131 | default: |
| 132 | priority = LOG_INFO; |
| 133 | break; |
| 134 | } |
| 135 | syslog(priority, "%s", format); |
| 136 | } |
| 137 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 138 | |
| 139 | os_free(format); |
| 140 | } |
| 141 | #endif /* CONFIG_NO_HOSTAPD_LOGGER */ |
| 142 | |
| 143 | |
| 144 | /** |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 145 | * hostapd_driver_init - Preparate driver interface |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 146 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 147 | static int hostapd_driver_init(struct hostapd_iface *iface) |
| 148 | { |
| 149 | struct wpa_init_params params; |
| 150 | size_t i; |
| 151 | struct hostapd_data *hapd = iface->bss[0]; |
| 152 | struct hostapd_bss_config *conf = hapd->conf; |
| 153 | u8 *b = conf->bssid; |
| 154 | struct wpa_driver_capa capa; |
| 155 | |
| 156 | if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) { |
| 157 | wpa_printf(MSG_ERROR, "No hostapd driver wrapper available"); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | /* Initialize the driver interface */ |
| 162 | if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5])) |
| 163 | b = NULL; |
| 164 | |
| 165 | os_memset(¶ms, 0, sizeof(params)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 166 | for (i = 0; wpa_drivers[i]; i++) { |
| 167 | if (wpa_drivers[i] != hapd->driver) |
| 168 | continue; |
| 169 | |
| 170 | if (global.drv_priv[i] == NULL && |
| 171 | wpa_drivers[i]->global_init) { |
| 172 | global.drv_priv[i] = wpa_drivers[i]->global_init(); |
| 173 | if (global.drv_priv[i] == NULL) { |
| 174 | wpa_printf(MSG_ERROR, "Failed to initialize " |
| 175 | "driver '%s'", |
| 176 | wpa_drivers[i]->name); |
| 177 | return -1; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | params.global_priv = global.drv_priv[i]; |
| 182 | break; |
| 183 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 184 | params.bssid = b; |
| 185 | params.ifname = hapd->conf->iface; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 186 | params.ssid = hapd->conf->ssid.ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 187 | params.ssid_len = hapd->conf->ssid.ssid_len; |
| 188 | params.test_socket = hapd->conf->test_socket; |
| 189 | params.use_pae_group_addr = hapd->conf->use_pae_group_addr; |
| 190 | |
| 191 | params.num_bridge = hapd->iface->num_bss; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 192 | params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 193 | if (params.bridge == NULL) |
| 194 | return -1; |
| 195 | for (i = 0; i < hapd->iface->num_bss; i++) { |
| 196 | struct hostapd_data *bss = hapd->iface->bss[i]; |
| 197 | if (bss->conf->bridge[0]) |
| 198 | params.bridge[i] = bss->conf->bridge; |
| 199 | } |
| 200 | |
| 201 | params.own_addr = hapd->own_addr; |
| 202 | |
| 203 | hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms); |
| 204 | os_free(params.bridge); |
| 205 | if (hapd->drv_priv == NULL) { |
| 206 | wpa_printf(MSG_ERROR, "%s driver initialization failed.", |
| 207 | hapd->driver->name); |
| 208 | hapd->driver = NULL; |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | if (hapd->driver->get_capa && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 213 | hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 214 | iface->drv_flags = capa.flags; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 215 | iface->probe_resp_offloads = capa.probe_resp_offloads; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 216 | iface->extended_capa = capa.extended_capa; |
| 217 | iface->extended_capa_mask = capa.extended_capa_mask; |
| 218 | iface->extended_capa_len = capa.extended_capa_len; |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 219 | iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 220 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 226 | /** |
| 227 | * hostapd_interface_init - Read configuration file and init BSS data |
| 228 | * |
| 229 | * This function is used to parse configuration file for a full interface (one |
| 230 | * or more BSSes sharing the same radio) and allocate memory for the BSS |
| 231 | * interfaces. No actiual driver operations are started. |
| 232 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 233 | static struct hostapd_iface * |
| 234 | hostapd_interface_init(struct hapd_interfaces *interfaces, |
| 235 | const char *config_fname, int debug) |
| 236 | { |
| 237 | struct hostapd_iface *iface; |
| 238 | int k; |
| 239 | |
| 240 | wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 241 | iface = hostapd_init(interfaces, config_fname); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 242 | if (!iface) |
| 243 | return NULL; |
| 244 | iface->interfaces = interfaces; |
| 245 | |
| 246 | for (k = 0; k < debug; k++) { |
| 247 | if (iface->bss[0]->conf->logger_stdout_level > 0) |
| 248 | iface->bss[0]->conf->logger_stdout_level--; |
| 249 | } |
| 250 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 251 | if (iface->conf->bss[0]->iface[0] == '\0' && |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 252 | !hostapd_drv_none(iface->bss[0])) { |
| 253 | wpa_printf(MSG_ERROR, "Interface name not specified in %s", |
| 254 | config_fname); |
| 255 | hostapd_interface_deinit_free(iface); |
| 256 | return NULL; |
| 257 | } |
| 258 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 259 | return iface; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /** |
| 264 | * handle_term - SIGINT and SIGTERM handler to terminate hostapd process |
| 265 | */ |
| 266 | static void handle_term(int sig, void *signal_ctx) |
| 267 | { |
| 268 | wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig); |
| 269 | eloop_terminate(); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | #ifndef CONFIG_NATIVE_WINDOWS |
| 274 | |
| 275 | static int handle_reload_iface(struct hostapd_iface *iface, void *ctx) |
| 276 | { |
| 277 | if (hostapd_reload_config(iface) < 0) { |
| 278 | wpa_printf(MSG_WARNING, "Failed to read new configuration " |
| 279 | "file - continuing with old."); |
| 280 | } |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /** |
| 286 | * handle_reload - SIGHUP handler to reload configuration |
| 287 | */ |
| 288 | static void handle_reload(int sig, void *signal_ctx) |
| 289 | { |
| 290 | struct hapd_interfaces *interfaces = signal_ctx; |
| 291 | wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration", |
| 292 | sig); |
| 293 | hostapd_for_each_interface(interfaces, handle_reload_iface, NULL); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | static void handle_dump_state(int sig, void *signal_ctx) |
| 298 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 299 | /* Not used anymore - ignore signal */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 300 | } |
| 301 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 302 | |
| 303 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 304 | static int hostapd_global_init(struct hapd_interfaces *interfaces, |
| 305 | const char *entropy_file) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 306 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 307 | int i; |
| 308 | |
| 309 | os_memset(&global, 0, sizeof(global)); |
| 310 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 311 | hostapd_logger_register_cb(hostapd_logger_cb); |
| 312 | |
| 313 | if (eap_server_register_methods()) { |
| 314 | wpa_printf(MSG_ERROR, "Failed to register EAP methods"); |
| 315 | return -1; |
| 316 | } |
| 317 | |
| 318 | if (eloop_init()) { |
| 319 | wpa_printf(MSG_ERROR, "Failed to initialize event loop"); |
| 320 | return -1; |
| 321 | } |
| 322 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 323 | random_init(entropy_file); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 324 | |
| 325 | #ifndef CONFIG_NATIVE_WINDOWS |
| 326 | eloop_register_signal(SIGHUP, handle_reload, interfaces); |
| 327 | eloop_register_signal(SIGUSR1, handle_dump_state, interfaces); |
| 328 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 329 | eloop_register_signal_terminate(handle_term, interfaces); |
| 330 | |
| 331 | #ifndef CONFIG_NATIVE_WINDOWS |
| 332 | openlog("hostapd", 0, LOG_DAEMON); |
| 333 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 334 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 335 | for (i = 0; wpa_drivers[i]; i++) |
| 336 | global.drv_count++; |
| 337 | if (global.drv_count == 0) { |
| 338 | wpa_printf(MSG_ERROR, "No drivers enabled"); |
| 339 | return -1; |
| 340 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 341 | global.drv_priv = os_calloc(global.drv_count, sizeof(void *)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 342 | if (global.drv_priv == NULL) |
| 343 | return -1; |
| 344 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | |
| 349 | static void hostapd_global_deinit(const char *pid_file) |
| 350 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 351 | int i; |
| 352 | |
| 353 | for (i = 0; wpa_drivers[i] && global.drv_priv; i++) { |
| 354 | if (!global.drv_priv[i]) |
| 355 | continue; |
| 356 | wpa_drivers[i]->global_deinit(global.drv_priv[i]); |
| 357 | } |
| 358 | os_free(global.drv_priv); |
| 359 | global.drv_priv = NULL; |
| 360 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 361 | #ifdef EAP_SERVER_TNC |
| 362 | tncs_global_deinit(); |
| 363 | #endif /* EAP_SERVER_TNC */ |
| 364 | |
| 365 | random_deinit(); |
| 366 | |
| 367 | eloop_destroy(); |
| 368 | |
| 369 | #ifndef CONFIG_NATIVE_WINDOWS |
| 370 | closelog(); |
| 371 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 372 | |
| 373 | eap_server_unregister_methods(); |
| 374 | |
| 375 | os_daemonize_terminate(pid_file); |
| 376 | } |
| 377 | |
| 378 | |
| 379 | static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize, |
| 380 | const char *pid_file) |
| 381 | { |
| 382 | #ifdef EAP_SERVER_TNC |
| 383 | int tnc = 0; |
| 384 | size_t i, k; |
| 385 | |
| 386 | for (i = 0; !tnc && i < ifaces->count; i++) { |
| 387 | for (k = 0; k < ifaces->iface[i]->num_bss; k++) { |
| 388 | if (ifaces->iface[i]->bss[0]->conf->tnc) { |
| 389 | tnc++; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | if (tnc && tncs_global_init() < 0) { |
| 396 | wpa_printf(MSG_ERROR, "Failed to initialize TNCS"); |
| 397 | return -1; |
| 398 | } |
| 399 | #endif /* EAP_SERVER_TNC */ |
| 400 | |
| 401 | if (daemonize && os_daemonize(pid_file)) { |
| 402 | perror("daemon"); |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | eloop_run(); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | |
| 412 | static void show_version(void) |
| 413 | { |
| 414 | fprintf(stderr, |
| 415 | "hostapd v" VERSION_STR "\n" |
| 416 | "User space daemon for IEEE 802.11 AP management,\n" |
| 417 | "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n" |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 418 | "Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi> " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 419 | "and contributors\n"); |
| 420 | } |
| 421 | |
| 422 | |
| 423 | static void usage(void) |
| 424 | { |
| 425 | show_version(); |
| 426 | fprintf(stderr, |
| 427 | "\n" |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 428 | "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] " |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 429 | "\\\n" |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 430 | " [-g <global ctrl_iface>] [-G <group>] \\\n" |
| 431 | " <configuration file(s)>\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 432 | "\n" |
| 433 | "options:\n" |
| 434 | " -h show this usage\n" |
| 435 | " -d show more debug messages (-dd for even more)\n" |
| 436 | " -B run daemon in the background\n" |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 437 | " -e entropy file\n" |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 438 | " -g global control interface path\n" |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 439 | " -G group for control interfaces\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 440 | " -P PID file\n" |
| 441 | " -K include key data in debug messages\n" |
| 442 | #ifdef CONFIG_DEBUG_FILE |
| 443 | " -f log output to debug file instead of stdout\n" |
| 444 | #endif /* CONFIG_DEBUG_FILE */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 445 | #ifdef CONFIG_DEBUG_LINUX_TRACING |
| 446 | " -T = record to Linux tracing in addition to logging\n" |
| 447 | " (records all messages regardless of debug verbosity)\n" |
| 448 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 449 | " -t include timestamps in some debug messages\n" |
| 450 | " -v show hostapd version\n"); |
| 451 | |
| 452 | exit(1); |
| 453 | } |
| 454 | |
| 455 | |
| 456 | static const char * hostapd_msg_ifname_cb(void *ctx) |
| 457 | { |
| 458 | struct hostapd_data *hapd = ctx; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 459 | if (hapd && hapd->iconf && hapd->iconf->bss && |
| 460 | hapd->iconf->num_bss > 0 && hapd->iconf->bss[0]) |
| 461 | return hapd->iconf->bss[0]->iface; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 466 | static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces, |
| 467 | const char *path) |
| 468 | { |
| 469 | char *pos; |
| 470 | os_free(interfaces->global_iface_path); |
| 471 | interfaces->global_iface_path = os_strdup(path); |
| 472 | if (interfaces->global_iface_path == NULL) |
| 473 | return -1; |
| 474 | pos = os_strrchr(interfaces->global_iface_path, '/'); |
| 475 | if (pos == NULL) { |
Dmitry Shmidt | 1e78e76 | 2013-04-02 11:05:36 -0700 | [diff] [blame] | 476 | wpa_printf(MSG_ERROR, "No '/' in the global control interface " |
| 477 | "file"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 478 | os_free(interfaces->global_iface_path); |
| 479 | interfaces->global_iface_path = NULL; |
| 480 | return -1; |
| 481 | } |
| 482 | |
| 483 | *pos = '\0'; |
| 484 | interfaces->global_iface_name = pos + 1; |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 490 | static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces, |
| 491 | const char *group) |
| 492 | { |
| 493 | #ifndef CONFIG_NATIVE_WINDOWS |
| 494 | struct group *grp; |
| 495 | grp = getgrnam(group); |
| 496 | if (grp == NULL) { |
| 497 | wpa_printf(MSG_ERROR, "Unknown group '%s'", group); |
| 498 | return -1; |
| 499 | } |
| 500 | interfaces->ctrl_iface_group = grp->gr_gid; |
| 501 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 506 | #ifdef CONFIG_WPS |
| 507 | static int gen_uuid(const char *txt_addr) |
| 508 | { |
| 509 | u8 addr[ETH_ALEN]; |
| 510 | u8 uuid[UUID_LEN]; |
| 511 | char buf[100]; |
| 512 | |
| 513 | if (hwaddr_aton(txt_addr, addr) < 0) |
| 514 | return -1; |
| 515 | |
| 516 | uuid_gen_mac_addr(addr, uuid); |
| 517 | if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0) |
| 518 | return -1; |
| 519 | |
| 520 | printf("%s\n", buf); |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | #endif /* CONFIG_WPS */ |
| 525 | |
| 526 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 527 | int main(int argc, char *argv[]) |
| 528 | { |
| 529 | struct hapd_interfaces interfaces; |
| 530 | int ret = 1; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 531 | size_t i, j; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 532 | int c, debug = 0, daemonize = 0; |
| 533 | char *pid_file = NULL; |
| 534 | const char *log_file = NULL; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 535 | const char *entropy_file = NULL; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 536 | char **bss_config = NULL, **tmp_bss; |
| 537 | size_t num_bss_configs = 0; |
| 538 | #ifdef CONFIG_DEBUG_LINUX_TRACING |
| 539 | int enable_trace_dbg = 0; |
| 540 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 541 | |
| 542 | if (os_program_init()) |
| 543 | return -1; |
| 544 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 545 | os_memset(&interfaces, 0, sizeof(interfaces)); |
| 546 | interfaces.reload_config = hostapd_reload_config; |
| 547 | interfaces.config_read_cb = hostapd_config_read; |
| 548 | interfaces.for_each_interface = hostapd_for_each_interface; |
| 549 | interfaces.ctrl_iface_init = hostapd_ctrl_iface_init; |
| 550 | interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit; |
| 551 | interfaces.driver_init = hostapd_driver_init; |
| 552 | interfaces.global_iface_path = NULL; |
| 553 | interfaces.global_iface_name = NULL; |
| 554 | interfaces.global_ctrl_sock = -1; |
| 555 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 556 | for (;;) { |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 557 | c = getopt(argc, argv, "b:Bde:f:hKP:Ttu:vg:G:"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 558 | if (c < 0) |
| 559 | break; |
| 560 | switch (c) { |
| 561 | case 'h': |
| 562 | usage(); |
| 563 | break; |
| 564 | case 'd': |
| 565 | debug++; |
| 566 | if (wpa_debug_level > 0) |
| 567 | wpa_debug_level--; |
| 568 | break; |
| 569 | case 'B': |
| 570 | daemonize++; |
| 571 | break; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 572 | case 'e': |
| 573 | entropy_file = optarg; |
| 574 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 575 | case 'f': |
| 576 | log_file = optarg; |
| 577 | break; |
| 578 | case 'K': |
| 579 | wpa_debug_show_keys++; |
| 580 | break; |
| 581 | case 'P': |
| 582 | os_free(pid_file); |
| 583 | pid_file = os_rel2abs_path(optarg); |
| 584 | break; |
| 585 | case 't': |
| 586 | wpa_debug_timestamp++; |
| 587 | break; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 588 | #ifdef CONFIG_DEBUG_LINUX_TRACING |
| 589 | case 'T': |
| 590 | enable_trace_dbg = 1; |
| 591 | break; |
| 592 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 593 | case 'v': |
| 594 | show_version(); |
| 595 | exit(1); |
| 596 | break; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 597 | case 'g': |
Dmitry Shmidt | 1e78e76 | 2013-04-02 11:05:36 -0700 | [diff] [blame] | 598 | if (hostapd_get_global_ctrl_iface(&interfaces, optarg)) |
| 599 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 600 | break; |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 601 | case 'G': |
Dmitry Shmidt | 1e78e76 | 2013-04-02 11:05:36 -0700 | [diff] [blame] | 602 | if (hostapd_get_ctrl_iface_group(&interfaces, optarg)) |
| 603 | return -1; |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 604 | break; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 605 | case 'b': |
| 606 | tmp_bss = os_realloc_array(bss_config, |
| 607 | num_bss_configs + 1, |
| 608 | sizeof(char *)); |
| 609 | if (tmp_bss == NULL) |
| 610 | goto out; |
| 611 | bss_config = tmp_bss; |
| 612 | bss_config[num_bss_configs++] = optarg; |
| 613 | break; |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 614 | #ifdef CONFIG_WPS |
| 615 | case 'u': |
| 616 | return gen_uuid(optarg); |
| 617 | #endif /* CONFIG_WPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 618 | default: |
| 619 | usage(); |
| 620 | break; |
| 621 | } |
| 622 | } |
| 623 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 624 | if (optind == argc && interfaces.global_iface_path == NULL && |
| 625 | num_bss_configs == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 626 | usage(); |
| 627 | |
| 628 | wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb); |
| 629 | |
| 630 | if (log_file) |
| 631 | wpa_debug_open_file(log_file); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 632 | #ifdef CONFIG_DEBUG_LINUX_TRACING |
| 633 | if (enable_trace_dbg) { |
| 634 | int tret = wpa_debug_open_linux_tracing(); |
| 635 | if (tret) { |
| 636 | wpa_printf(MSG_ERROR, "Failed to enable trace logging"); |
| 637 | return -1; |
| 638 | } |
| 639 | } |
| 640 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 641 | |
| 642 | interfaces.count = argc - optind; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 643 | if (interfaces.count || num_bss_configs) { |
| 644 | interfaces.iface = os_calloc(interfaces.count + num_bss_configs, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 645 | sizeof(struct hostapd_iface *)); |
| 646 | if (interfaces.iface == NULL) { |
| 647 | wpa_printf(MSG_ERROR, "malloc failed"); |
| 648 | return -1; |
| 649 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 652 | if (hostapd_global_init(&interfaces, entropy_file)) { |
| 653 | wpa_printf(MSG_ERROR, "Failed to initilize global context"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 654 | return -1; |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 655 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 656 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 657 | /* Allocate and parse configuration for full interface files */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 658 | for (i = 0; i < interfaces.count; i++) { |
| 659 | interfaces.iface[i] = hostapd_interface_init(&interfaces, |
| 660 | argv[optind + i], |
| 661 | debug); |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 662 | if (!interfaces.iface[i]) { |
| 663 | wpa_printf(MSG_ERROR, "Failed to initialize interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 664 | goto out; |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 665 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 668 | /* Allocate and parse configuration for per-BSS files */ |
| 669 | for (i = 0; i < num_bss_configs; i++) { |
| 670 | struct hostapd_iface *iface; |
| 671 | char *fname; |
| 672 | |
| 673 | wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]); |
| 674 | fname = os_strchr(bss_config[i], ':'); |
| 675 | if (fname == NULL) { |
| 676 | wpa_printf(MSG_ERROR, |
| 677 | "Invalid BSS config identifier '%s'", |
| 678 | bss_config[i]); |
| 679 | goto out; |
| 680 | } |
| 681 | *fname++ = '\0'; |
| 682 | iface = hostapd_interface_init_bss(&interfaces, bss_config[i], |
| 683 | fname, debug); |
| 684 | if (iface == NULL) |
| 685 | goto out; |
| 686 | for (j = 0; j < interfaces.count; j++) { |
| 687 | if (interfaces.iface[j] == iface) |
| 688 | break; |
| 689 | } |
| 690 | if (j == interfaces.count) { |
| 691 | struct hostapd_iface **tmp; |
| 692 | tmp = os_realloc_array(interfaces.iface, |
| 693 | interfaces.count + 1, |
| 694 | sizeof(struct hostapd_iface *)); |
| 695 | if (tmp == NULL) { |
| 696 | hostapd_interface_deinit_free(iface); |
| 697 | goto out; |
| 698 | } |
| 699 | interfaces.iface = tmp; |
| 700 | interfaces.iface[interfaces.count++] = iface; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Enable configured interfaces. Depending on channel configuration, |
| 706 | * this may complete full initialization before returning or use a |
| 707 | * callback mechanism to complete setup in case of operations like HT |
| 708 | * co-ex scans, ACS, or DFS are needed to determine channel parameters. |
| 709 | * In such case, the interface will be enabled from eloop context within |
| 710 | * hostapd_global_run(). |
| 711 | */ |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 712 | interfaces.terminate_on_error = interfaces.count; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 713 | for (i = 0; i < interfaces.count; i++) { |
| 714 | if (hostapd_driver_init(interfaces.iface[i]) || |
| 715 | hostapd_setup_interface(interfaces.iface[i])) |
| 716 | goto out; |
| 717 | } |
| 718 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 719 | hostapd_global_ctrl_iface_init(&interfaces); |
| 720 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 721 | if (hostapd_global_run(&interfaces, daemonize, pid_file)) { |
| 722 | wpa_printf(MSG_ERROR, "Failed to start eloop"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 723 | goto out; |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 724 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 725 | |
| 726 | ret = 0; |
| 727 | |
| 728 | out: |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 729 | hostapd_global_ctrl_iface_deinit(&interfaces); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 730 | /* Deinitialize all interfaces */ |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame^] | 731 | for (i = 0; i < interfaces.count; i++) { |
| 732 | interfaces.iface[i]->driver_ap_teardown = |
| 733 | !!(interfaces.iface[i]->drv_flags & |
| 734 | WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 735 | hostapd_interface_deinit_free(interfaces.iface[i]); |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame^] | 736 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 737 | os_free(interfaces.iface); |
| 738 | |
| 739 | hostapd_global_deinit(pid_file); |
| 740 | os_free(pid_file); |
| 741 | |
| 742 | if (log_file) |
| 743 | wpa_debug_close_file(); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 744 | wpa_debug_close_linux_tracing(); |
| 745 | |
| 746 | os_free(bss_config); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 747 | |
| 748 | os_program_deinit(); |
| 749 | |
| 750 | return ret; |
| 751 | } |