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