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