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" |
| 17 | #include "crypto/random.h" |
| 18 | #include "crypto/tls.h" |
| 19 | #include "common/version.h" |
| 20 | #include "drivers/driver.h" |
| 21 | #include "eap_server/eap.h" |
| 22 | #include "eap_server/tncs.h" |
| 23 | #include "ap/hostapd.h" |
| 24 | #include "ap/ap_config.h" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 25 | #include "ap/ap_drv_ops.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 26 | #include "config_file.h" |
| 27 | #include "eap_register.h" |
| 28 | #include "dump_state.h" |
| 29 | #include "ctrl_iface.h" |
| 30 | |
| 31 | |
| 32 | extern int wpa_debug_level; |
| 33 | extern int wpa_debug_show_keys; |
| 34 | extern int wpa_debug_timestamp; |
| 35 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 36 | extern struct wpa_driver_ops *wpa_drivers[]; |
| 37 | |
| 38 | |
| 39 | struct hapd_global { |
| 40 | void **drv_priv; |
| 41 | size_t drv_count; |
| 42 | }; |
| 43 | |
| 44 | static struct hapd_global global; |
| 45 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 46 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 47 | #ifndef CONFIG_NO_HOSTAPD_LOGGER |
| 48 | static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module, |
| 49 | int level, const char *txt, size_t len) |
| 50 | { |
| 51 | struct hostapd_data *hapd = ctx; |
| 52 | char *format, *module_str; |
| 53 | int maxlen; |
| 54 | int conf_syslog_level, conf_stdout_level; |
| 55 | unsigned int conf_syslog, conf_stdout; |
| 56 | |
| 57 | maxlen = len + 100; |
| 58 | format = os_malloc(maxlen); |
| 59 | if (!format) |
| 60 | return; |
| 61 | |
| 62 | if (hapd && hapd->conf) { |
| 63 | conf_syslog_level = hapd->conf->logger_syslog_level; |
| 64 | conf_stdout_level = hapd->conf->logger_stdout_level; |
| 65 | conf_syslog = hapd->conf->logger_syslog; |
| 66 | conf_stdout = hapd->conf->logger_stdout; |
| 67 | } else { |
| 68 | conf_syslog_level = conf_stdout_level = 0; |
| 69 | conf_syslog = conf_stdout = (unsigned int) -1; |
| 70 | } |
| 71 | |
| 72 | switch (module) { |
| 73 | case HOSTAPD_MODULE_IEEE80211: |
| 74 | module_str = "IEEE 802.11"; |
| 75 | break; |
| 76 | case HOSTAPD_MODULE_IEEE8021X: |
| 77 | module_str = "IEEE 802.1X"; |
| 78 | break; |
| 79 | case HOSTAPD_MODULE_RADIUS: |
| 80 | module_str = "RADIUS"; |
| 81 | break; |
| 82 | case HOSTAPD_MODULE_WPA: |
| 83 | module_str = "WPA"; |
| 84 | break; |
| 85 | case HOSTAPD_MODULE_DRIVER: |
| 86 | module_str = "DRIVER"; |
| 87 | break; |
| 88 | case HOSTAPD_MODULE_IAPP: |
| 89 | module_str = "IAPP"; |
| 90 | break; |
| 91 | case HOSTAPD_MODULE_MLME: |
| 92 | module_str = "MLME"; |
| 93 | break; |
| 94 | default: |
| 95 | module_str = NULL; |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | if (hapd && hapd->conf && addr) |
| 100 | os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s", |
| 101 | hapd->conf->iface, MAC2STR(addr), |
| 102 | module_str ? " " : "", module_str, txt); |
| 103 | else if (hapd && hapd->conf) |
| 104 | os_snprintf(format, maxlen, "%s:%s%s %s", |
| 105 | hapd->conf->iface, module_str ? " " : "", |
| 106 | module_str, txt); |
| 107 | else if (addr) |
| 108 | os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s", |
| 109 | MAC2STR(addr), module_str ? " " : "", |
| 110 | module_str, txt); |
| 111 | else |
| 112 | os_snprintf(format, maxlen, "%s%s%s", |
| 113 | module_str, module_str ? ": " : "", txt); |
| 114 | |
| 115 | if ((conf_stdout & module) && level >= conf_stdout_level) { |
| 116 | wpa_debug_print_timestamp(); |
| 117 | printf("%s\n", format); |
| 118 | } |
| 119 | |
| 120 | #ifndef CONFIG_NATIVE_WINDOWS |
| 121 | if ((conf_syslog & module) && level >= conf_syslog_level) { |
| 122 | int priority; |
| 123 | switch (level) { |
| 124 | case HOSTAPD_LEVEL_DEBUG_VERBOSE: |
| 125 | case HOSTAPD_LEVEL_DEBUG: |
| 126 | priority = LOG_DEBUG; |
| 127 | break; |
| 128 | case HOSTAPD_LEVEL_INFO: |
| 129 | priority = LOG_INFO; |
| 130 | break; |
| 131 | case HOSTAPD_LEVEL_NOTICE: |
| 132 | priority = LOG_NOTICE; |
| 133 | break; |
| 134 | case HOSTAPD_LEVEL_WARNING: |
| 135 | priority = LOG_WARNING; |
| 136 | break; |
| 137 | default: |
| 138 | priority = LOG_INFO; |
| 139 | break; |
| 140 | } |
| 141 | syslog(priority, "%s", format); |
| 142 | } |
| 143 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 144 | |
| 145 | os_free(format); |
| 146 | } |
| 147 | #endif /* CONFIG_NO_HOSTAPD_LOGGER */ |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * hostapd_init - Allocate and initialize per-interface data |
| 152 | * @config_file: Path to the configuration file |
| 153 | * Returns: Pointer to the allocated interface data or %NULL on failure |
| 154 | * |
| 155 | * This function is used to allocate main data structures for per-interface |
| 156 | * data. The allocated data buffer will be freed by calling |
| 157 | * hostapd_cleanup_iface(). |
| 158 | */ |
| 159 | static struct hostapd_iface * hostapd_init(const char *config_file) |
| 160 | { |
| 161 | struct hostapd_iface *hapd_iface = NULL; |
| 162 | struct hostapd_config *conf = NULL; |
| 163 | struct hostapd_data *hapd; |
| 164 | size_t i; |
| 165 | |
| 166 | hapd_iface = os_zalloc(sizeof(*hapd_iface)); |
| 167 | if (hapd_iface == NULL) |
| 168 | goto fail; |
| 169 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 170 | hapd_iface->config_fname = os_strdup(config_file); |
| 171 | if (hapd_iface->config_fname == NULL) |
| 172 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 173 | |
| 174 | conf = hostapd_config_read(hapd_iface->config_fname); |
| 175 | if (conf == NULL) |
| 176 | goto fail; |
| 177 | hapd_iface->conf = conf; |
| 178 | |
| 179 | hapd_iface->num_bss = conf->num_bss; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 180 | hapd_iface->bss = os_calloc(conf->num_bss, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 181 | sizeof(struct hostapd_data *)); |
| 182 | if (hapd_iface->bss == NULL) |
| 183 | goto fail; |
| 184 | |
| 185 | for (i = 0; i < conf->num_bss; i++) { |
| 186 | hapd = hapd_iface->bss[i] = |
| 187 | hostapd_alloc_bss_data(hapd_iface, conf, |
| 188 | &conf->bss[i]); |
| 189 | if (hapd == NULL) |
| 190 | goto fail; |
| 191 | hapd->msg_ctx = hapd; |
| 192 | } |
| 193 | |
| 194 | return hapd_iface; |
| 195 | |
| 196 | fail: |
| 197 | if (conf) |
| 198 | hostapd_config_free(conf); |
| 199 | if (hapd_iface) { |
| 200 | os_free(hapd_iface->config_fname); |
| 201 | os_free(hapd_iface->bss); |
| 202 | os_free(hapd_iface); |
| 203 | } |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | static int hostapd_driver_init(struct hostapd_iface *iface) |
| 209 | { |
| 210 | struct wpa_init_params params; |
| 211 | size_t i; |
| 212 | struct hostapd_data *hapd = iface->bss[0]; |
| 213 | struct hostapd_bss_config *conf = hapd->conf; |
| 214 | u8 *b = conf->bssid; |
| 215 | struct wpa_driver_capa capa; |
| 216 | |
| 217 | if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) { |
| 218 | wpa_printf(MSG_ERROR, "No hostapd driver wrapper available"); |
| 219 | return -1; |
| 220 | } |
| 221 | |
| 222 | /* Initialize the driver interface */ |
| 223 | if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5])) |
| 224 | b = NULL; |
| 225 | |
| 226 | os_memset(¶ms, 0, sizeof(params)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 227 | for (i = 0; wpa_drivers[i]; i++) { |
| 228 | if (wpa_drivers[i] != hapd->driver) |
| 229 | continue; |
| 230 | |
| 231 | if (global.drv_priv[i] == NULL && |
| 232 | wpa_drivers[i]->global_init) { |
| 233 | global.drv_priv[i] = wpa_drivers[i]->global_init(); |
| 234 | if (global.drv_priv[i] == NULL) { |
| 235 | wpa_printf(MSG_ERROR, "Failed to initialize " |
| 236 | "driver '%s'", |
| 237 | wpa_drivers[i]->name); |
| 238 | return -1; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | params.global_priv = global.drv_priv[i]; |
| 243 | break; |
| 244 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 245 | params.bssid = b; |
| 246 | params.ifname = hapd->conf->iface; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 247 | params.ssid = hapd->conf->ssid.ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 248 | params.ssid_len = hapd->conf->ssid.ssid_len; |
| 249 | params.test_socket = hapd->conf->test_socket; |
| 250 | params.use_pae_group_addr = hapd->conf->use_pae_group_addr; |
| 251 | |
| 252 | params.num_bridge = hapd->iface->num_bss; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 253 | params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 254 | if (params.bridge == NULL) |
| 255 | return -1; |
| 256 | for (i = 0; i < hapd->iface->num_bss; i++) { |
| 257 | struct hostapd_data *bss = hapd->iface->bss[i]; |
| 258 | if (bss->conf->bridge[0]) |
| 259 | params.bridge[i] = bss->conf->bridge; |
| 260 | } |
| 261 | |
| 262 | params.own_addr = hapd->own_addr; |
| 263 | |
| 264 | hapd->drv_priv = hapd->driver->hapd_init(hapd, ¶ms); |
| 265 | os_free(params.bridge); |
| 266 | if (hapd->drv_priv == NULL) { |
| 267 | wpa_printf(MSG_ERROR, "%s driver initialization failed.", |
| 268 | hapd->driver->name); |
| 269 | hapd->driver = NULL; |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | if (hapd->driver->get_capa && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 274 | hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 275 | iface->drv_flags = capa.flags; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 276 | iface->probe_resp_offloads = capa.probe_resp_offloads; |
| 277 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 283 | static struct hostapd_iface * |
| 284 | hostapd_interface_init(struct hapd_interfaces *interfaces, |
| 285 | const char *config_fname, int debug) |
| 286 | { |
| 287 | struct hostapd_iface *iface; |
| 288 | int k; |
| 289 | |
| 290 | wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname); |
| 291 | iface = hostapd_init(config_fname); |
| 292 | if (!iface) |
| 293 | return NULL; |
| 294 | iface->interfaces = interfaces; |
| 295 | |
| 296 | for (k = 0; k < debug; k++) { |
| 297 | if (iface->bss[0]->conf->logger_stdout_level > 0) |
| 298 | iface->bss[0]->conf->logger_stdout_level--; |
| 299 | } |
| 300 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 301 | if (iface->conf->bss[0].iface[0] != 0 || |
| 302 | hostapd_drv_none(iface->bss[0])) { |
| 303 | if (hostapd_driver_init(iface) || |
| 304 | hostapd_setup_interface(iface)) { |
| 305 | hostapd_interface_deinit_free(iface); |
| 306 | return NULL; |
| 307 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | return iface; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /** |
| 315 | * handle_term - SIGINT and SIGTERM handler to terminate hostapd process |
| 316 | */ |
| 317 | static void handle_term(int sig, void *signal_ctx) |
| 318 | { |
| 319 | wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig); |
| 320 | eloop_terminate(); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | #ifndef CONFIG_NATIVE_WINDOWS |
| 325 | |
| 326 | static int handle_reload_iface(struct hostapd_iface *iface, void *ctx) |
| 327 | { |
| 328 | if (hostapd_reload_config(iface) < 0) { |
| 329 | wpa_printf(MSG_WARNING, "Failed to read new configuration " |
| 330 | "file - continuing with old."); |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | /** |
| 337 | * handle_reload - SIGHUP handler to reload configuration |
| 338 | */ |
| 339 | static void handle_reload(int sig, void *signal_ctx) |
| 340 | { |
| 341 | struct hapd_interfaces *interfaces = signal_ctx; |
| 342 | wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration", |
| 343 | sig); |
| 344 | hostapd_for_each_interface(interfaces, handle_reload_iface, NULL); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | static void handle_dump_state(int sig, void *signal_ctx) |
| 349 | { |
| 350 | #ifdef HOSTAPD_DUMP_STATE |
| 351 | struct hapd_interfaces *interfaces = signal_ctx; |
| 352 | hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL); |
| 353 | #endif /* HOSTAPD_DUMP_STATE */ |
| 354 | } |
| 355 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 356 | |
| 357 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 358 | static int hostapd_global_init(struct hapd_interfaces *interfaces, |
| 359 | const char *entropy_file) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 360 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 361 | int i; |
| 362 | |
| 363 | os_memset(&global, 0, sizeof(global)); |
| 364 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 365 | hostapd_logger_register_cb(hostapd_logger_cb); |
| 366 | |
| 367 | if (eap_server_register_methods()) { |
| 368 | wpa_printf(MSG_ERROR, "Failed to register EAP methods"); |
| 369 | return -1; |
| 370 | } |
| 371 | |
| 372 | if (eloop_init()) { |
| 373 | wpa_printf(MSG_ERROR, "Failed to initialize event loop"); |
| 374 | return -1; |
| 375 | } |
| 376 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 377 | random_init(entropy_file); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 378 | |
| 379 | #ifndef CONFIG_NATIVE_WINDOWS |
| 380 | eloop_register_signal(SIGHUP, handle_reload, interfaces); |
| 381 | eloop_register_signal(SIGUSR1, handle_dump_state, interfaces); |
| 382 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 383 | eloop_register_signal_terminate(handle_term, interfaces); |
| 384 | |
| 385 | #ifndef CONFIG_NATIVE_WINDOWS |
| 386 | openlog("hostapd", 0, LOG_DAEMON); |
| 387 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 388 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 389 | for (i = 0; wpa_drivers[i]; i++) |
| 390 | global.drv_count++; |
| 391 | if (global.drv_count == 0) { |
| 392 | wpa_printf(MSG_ERROR, "No drivers enabled"); |
| 393 | return -1; |
| 394 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 395 | global.drv_priv = os_calloc(global.drv_count, sizeof(void *)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 396 | if (global.drv_priv == NULL) |
| 397 | return -1; |
| 398 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | static void hostapd_global_deinit(const char *pid_file) |
| 404 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 405 | int i; |
| 406 | |
| 407 | for (i = 0; wpa_drivers[i] && global.drv_priv; i++) { |
| 408 | if (!global.drv_priv[i]) |
| 409 | continue; |
| 410 | wpa_drivers[i]->global_deinit(global.drv_priv[i]); |
| 411 | } |
| 412 | os_free(global.drv_priv); |
| 413 | global.drv_priv = NULL; |
| 414 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 415 | #ifdef EAP_SERVER_TNC |
| 416 | tncs_global_deinit(); |
| 417 | #endif /* EAP_SERVER_TNC */ |
| 418 | |
| 419 | random_deinit(); |
| 420 | |
| 421 | eloop_destroy(); |
| 422 | |
| 423 | #ifndef CONFIG_NATIVE_WINDOWS |
| 424 | closelog(); |
| 425 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 426 | |
| 427 | eap_server_unregister_methods(); |
| 428 | |
| 429 | os_daemonize_terminate(pid_file); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize, |
| 434 | const char *pid_file) |
| 435 | { |
| 436 | #ifdef EAP_SERVER_TNC |
| 437 | int tnc = 0; |
| 438 | size_t i, k; |
| 439 | |
| 440 | for (i = 0; !tnc && i < ifaces->count; i++) { |
| 441 | for (k = 0; k < ifaces->iface[i]->num_bss; k++) { |
| 442 | if (ifaces->iface[i]->bss[0]->conf->tnc) { |
| 443 | tnc++; |
| 444 | break; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | if (tnc && tncs_global_init() < 0) { |
| 450 | wpa_printf(MSG_ERROR, "Failed to initialize TNCS"); |
| 451 | return -1; |
| 452 | } |
| 453 | #endif /* EAP_SERVER_TNC */ |
| 454 | |
| 455 | if (daemonize && os_daemonize(pid_file)) { |
| 456 | perror("daemon"); |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | eloop_run(); |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | static void show_version(void) |
| 467 | { |
| 468 | fprintf(stderr, |
| 469 | "hostapd v" VERSION_STR "\n" |
| 470 | "User space daemon for IEEE 802.11 AP management,\n" |
| 471 | "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n" |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 472 | "Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 473 | "and contributors\n"); |
| 474 | } |
| 475 | |
| 476 | |
| 477 | static void usage(void) |
| 478 | { |
| 479 | show_version(); |
| 480 | fprintf(stderr, |
| 481 | "\n" |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 482 | "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] " |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 483 | "\\\n" |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame^] | 484 | " [-g <global ctrl_iface>] [-G <group>] \\\n" |
| 485 | " <configuration file(s)>\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 486 | "\n" |
| 487 | "options:\n" |
| 488 | " -h show this usage\n" |
| 489 | " -d show more debug messages (-dd for even more)\n" |
| 490 | " -B run daemon in the background\n" |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 491 | " -e entropy file\n" |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 492 | " -g global control interface path\n" |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame^] | 493 | " -G group for control interfaces\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 494 | " -P PID file\n" |
| 495 | " -K include key data in debug messages\n" |
| 496 | #ifdef CONFIG_DEBUG_FILE |
| 497 | " -f log output to debug file instead of stdout\n" |
| 498 | #endif /* CONFIG_DEBUG_FILE */ |
| 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; |
| 509 | if (hapd && hapd->iconf && hapd->iconf->bss) |
| 510 | return hapd->iconf->bss->iface; |
| 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 | { |
| 518 | char *pos; |
| 519 | os_free(interfaces->global_iface_path); |
| 520 | interfaces->global_iface_path = os_strdup(path); |
| 521 | if (interfaces->global_iface_path == NULL) |
| 522 | return -1; |
| 523 | pos = os_strrchr(interfaces->global_iface_path, '/'); |
| 524 | if (pos == NULL) { |
| 525 | os_free(interfaces->global_iface_path); |
| 526 | interfaces->global_iface_path = NULL; |
| 527 | return -1; |
| 528 | } |
| 529 | |
| 530 | *pos = '\0'; |
| 531 | interfaces->global_iface_name = pos + 1; |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame^] | 537 | static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces, |
| 538 | const char *group) |
| 539 | { |
| 540 | #ifndef CONFIG_NATIVE_WINDOWS |
| 541 | struct group *grp; |
| 542 | grp = getgrnam(group); |
| 543 | if (grp == NULL) { |
| 544 | wpa_printf(MSG_ERROR, "Unknown group '%s'", group); |
| 545 | return -1; |
| 546 | } |
| 547 | interfaces->ctrl_iface_group = grp->gr_gid; |
| 548 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 553 | int main(int argc, char *argv[]) |
| 554 | { |
| 555 | struct hapd_interfaces interfaces; |
| 556 | int ret = 1; |
| 557 | size_t i; |
| 558 | int c, debug = 0, daemonize = 0; |
| 559 | char *pid_file = NULL; |
| 560 | const char *log_file = NULL; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 561 | const char *entropy_file = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 562 | |
| 563 | if (os_program_init()) |
| 564 | return -1; |
| 565 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 566 | os_memset(&interfaces, 0, sizeof(interfaces)); |
| 567 | interfaces.reload_config = hostapd_reload_config; |
| 568 | interfaces.config_read_cb = hostapd_config_read; |
| 569 | interfaces.for_each_interface = hostapd_for_each_interface; |
| 570 | interfaces.ctrl_iface_init = hostapd_ctrl_iface_init; |
| 571 | interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit; |
| 572 | interfaces.driver_init = hostapd_driver_init; |
| 573 | interfaces.global_iface_path = NULL; |
| 574 | interfaces.global_iface_name = NULL; |
| 575 | interfaces.global_ctrl_sock = -1; |
| 576 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 577 | for (;;) { |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame^] | 578 | c = getopt(argc, argv, "Bde:f:hKP:tvg:G:"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 579 | if (c < 0) |
| 580 | break; |
| 581 | switch (c) { |
| 582 | case 'h': |
| 583 | usage(); |
| 584 | break; |
| 585 | case 'd': |
| 586 | debug++; |
| 587 | if (wpa_debug_level > 0) |
| 588 | wpa_debug_level--; |
| 589 | break; |
| 590 | case 'B': |
| 591 | daemonize++; |
| 592 | break; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 593 | case 'e': |
| 594 | entropy_file = optarg; |
| 595 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 596 | case 'f': |
| 597 | log_file = optarg; |
| 598 | break; |
| 599 | case 'K': |
| 600 | wpa_debug_show_keys++; |
| 601 | break; |
| 602 | case 'P': |
| 603 | os_free(pid_file); |
| 604 | pid_file = os_rel2abs_path(optarg); |
| 605 | break; |
| 606 | case 't': |
| 607 | wpa_debug_timestamp++; |
| 608 | break; |
| 609 | case 'v': |
| 610 | show_version(); |
| 611 | exit(1); |
| 612 | break; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 613 | case 'g': |
| 614 | hostapd_get_global_ctrl_iface(&interfaces, optarg); |
| 615 | break; |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame^] | 616 | case 'G': |
| 617 | hostapd_get_ctrl_iface_group(&interfaces, optarg); |
| 618 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 619 | default: |
| 620 | usage(); |
| 621 | break; |
| 622 | } |
| 623 | } |
| 624 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 625 | if (optind == argc && interfaces.global_iface_path == NULL) |
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); |
| 632 | |
| 633 | interfaces.count = argc - optind; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 634 | if (interfaces.count) { |
| 635 | interfaces.iface = os_calloc(interfaces.count, |
| 636 | sizeof(struct hostapd_iface *)); |
| 637 | if (interfaces.iface == NULL) { |
| 638 | wpa_printf(MSG_ERROR, "malloc failed"); |
| 639 | return -1; |
| 640 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 643 | if (hostapd_global_init(&interfaces, entropy_file)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 644 | return -1; |
| 645 | |
| 646 | /* Initialize interfaces */ |
| 647 | for (i = 0; i < interfaces.count; i++) { |
| 648 | interfaces.iface[i] = hostapd_interface_init(&interfaces, |
| 649 | argv[optind + i], |
| 650 | debug); |
| 651 | if (!interfaces.iface[i]) |
| 652 | goto out; |
| 653 | } |
| 654 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 655 | hostapd_global_ctrl_iface_init(&interfaces); |
| 656 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 657 | if (hostapd_global_run(&interfaces, daemonize, pid_file)) |
| 658 | goto out; |
| 659 | |
| 660 | ret = 0; |
| 661 | |
| 662 | out: |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 663 | hostapd_global_ctrl_iface_deinit(&interfaces); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 664 | /* Deinitialize all interfaces */ |
| 665 | for (i = 0; i < interfaces.count; i++) |
| 666 | hostapd_interface_deinit_free(interfaces.iface[i]); |
| 667 | os_free(interfaces.iface); |
| 668 | |
| 669 | hostapd_global_deinit(pid_file); |
| 670 | os_free(pid_file); |
| 671 | |
| 672 | if (log_file) |
| 673 | wpa_debug_close_file(); |
| 674 | |
| 675 | os_program_deinit(); |
| 676 | |
| 677 | return ret; |
| 678 | } |