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