main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * hostapd / main()
  3. * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include <syslog.h>
  17. #endif /* CONFIG_NATIVE_WINDOWS */
  18. #include "utils/common.h"
  19. #include "utils/eloop.h"
  20. #include "crypto/random.h"
  21. #include "crypto/tls.h"
  22. #include "common/version.h"
  23. #include "drivers/driver.h"
  24. #include "eap_server/eap.h"
  25. #include "eap_server/tncs.h"
  26. #include "ap/hostapd.h"
  27. #include "ap/ap_config.h"
  28. #include "config_file.h"
  29. #include "eap_register.h"
  30. #include "dump_state.h"
  31. #include "ctrl_iface.h"
  32. extern int wpa_debug_level;
  33. extern int wpa_debug_show_keys;
  34. extern int wpa_debug_timestamp;
  35. extern struct wpa_driver_ops *wpa_drivers[];
  36. struct hapd_global {
  37. void **drv_priv;
  38. size_t drv_count;
  39. };
  40. static struct hapd_global global;
  41. struct hapd_interfaces {
  42. size_t count;
  43. struct hostapd_iface **iface;
  44. };
  45. static int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
  46. int (*cb)(struct hostapd_iface *iface,
  47. void *ctx), void *ctx)
  48. {
  49. size_t i;
  50. int ret;
  51. for (i = 0; i < interfaces->count; i++) {
  52. ret = cb(interfaces->iface[i], ctx);
  53. if (ret)
  54. return ret;
  55. }
  56. return 0;
  57. }
  58. #ifndef CONFIG_NO_HOSTAPD_LOGGER
  59. static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
  60. int level, const char *txt, size_t len)
  61. {
  62. struct hostapd_data *hapd = ctx;
  63. char *format, *module_str;
  64. int maxlen;
  65. int conf_syslog_level, conf_stdout_level;
  66. unsigned int conf_syslog, conf_stdout;
  67. maxlen = len + 100;
  68. format = os_malloc(maxlen);
  69. if (!format)
  70. return;
  71. if (hapd && hapd->conf) {
  72. conf_syslog_level = hapd->conf->logger_syslog_level;
  73. conf_stdout_level = hapd->conf->logger_stdout_level;
  74. conf_syslog = hapd->conf->logger_syslog;
  75. conf_stdout = hapd->conf->logger_stdout;
  76. } else {
  77. conf_syslog_level = conf_stdout_level = 0;
  78. conf_syslog = conf_stdout = (unsigned int) -1;
  79. }
  80. switch (module) {
  81. case HOSTAPD_MODULE_IEEE80211:
  82. module_str = "IEEE 802.11";
  83. break;
  84. case HOSTAPD_MODULE_IEEE8021X:
  85. module_str = "IEEE 802.1X";
  86. break;
  87. case HOSTAPD_MODULE_RADIUS:
  88. module_str = "RADIUS";
  89. break;
  90. case HOSTAPD_MODULE_WPA:
  91. module_str = "WPA";
  92. break;
  93. case HOSTAPD_MODULE_DRIVER:
  94. module_str = "DRIVER";
  95. break;
  96. case HOSTAPD_MODULE_IAPP:
  97. module_str = "IAPP";
  98. break;
  99. case HOSTAPD_MODULE_MLME:
  100. module_str = "MLME";
  101. break;
  102. default:
  103. module_str = NULL;
  104. break;
  105. }
  106. if (hapd && hapd->conf && addr)
  107. os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
  108. hapd->conf->iface, MAC2STR(addr),
  109. module_str ? " " : "", module_str, txt);
  110. else if (hapd && hapd->conf)
  111. os_snprintf(format, maxlen, "%s:%s%s %s",
  112. hapd->conf->iface, module_str ? " " : "",
  113. module_str, txt);
  114. else if (addr)
  115. os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
  116. MAC2STR(addr), module_str ? " " : "",
  117. module_str, txt);
  118. else
  119. os_snprintf(format, maxlen, "%s%s%s",
  120. module_str, module_str ? ": " : "", txt);
  121. if ((conf_stdout & module) && level >= conf_stdout_level) {
  122. wpa_debug_print_timestamp();
  123. printf("%s\n", format);
  124. }
  125. #ifndef CONFIG_NATIVE_WINDOWS
  126. if ((conf_syslog & module) && level >= conf_syslog_level) {
  127. int priority;
  128. switch (level) {
  129. case HOSTAPD_LEVEL_DEBUG_VERBOSE:
  130. case HOSTAPD_LEVEL_DEBUG:
  131. priority = LOG_DEBUG;
  132. break;
  133. case HOSTAPD_LEVEL_INFO:
  134. priority = LOG_INFO;
  135. break;
  136. case HOSTAPD_LEVEL_NOTICE:
  137. priority = LOG_NOTICE;
  138. break;
  139. case HOSTAPD_LEVEL_WARNING:
  140. priority = LOG_WARNING;
  141. break;
  142. default:
  143. priority = LOG_INFO;
  144. break;
  145. }
  146. syslog(priority, "%s", format);
  147. }
  148. #endif /* CONFIG_NATIVE_WINDOWS */
  149. os_free(format);
  150. }
  151. #endif /* CONFIG_NO_HOSTAPD_LOGGER */
  152. /**
  153. * hostapd_init - Allocate and initialize per-interface data
  154. * @config_file: Path to the configuration file
  155. * Returns: Pointer to the allocated interface data or %NULL on failure
  156. *
  157. * This function is used to allocate main data structures for per-interface
  158. * data. The allocated data buffer will be freed by calling
  159. * hostapd_cleanup_iface().
  160. */
  161. static struct hostapd_iface * hostapd_init(const char *config_file)
  162. {
  163. struct hostapd_iface *hapd_iface = NULL;
  164. struct hostapd_config *conf = NULL;
  165. struct hostapd_data *hapd;
  166. size_t i;
  167. hapd_iface = os_zalloc(sizeof(*hapd_iface));
  168. if (hapd_iface == NULL)
  169. goto fail;
  170. hapd_iface->reload_config = hostapd_reload_config;
  171. hapd_iface->config_read_cb = hostapd_config_read;
  172. hapd_iface->config_fname = os_strdup(config_file);
  173. if (hapd_iface->config_fname == NULL)
  174. goto fail;
  175. hapd_iface->ctrl_iface_init = hostapd_ctrl_iface_init;
  176. hapd_iface->ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
  177. hapd_iface->for_each_interface = hostapd_for_each_interface;
  178. conf = hostapd_config_read(hapd_iface->config_fname);
  179. if (conf == NULL)
  180. goto fail;
  181. hapd_iface->conf = conf;
  182. hapd_iface->num_bss = conf->num_bss;
  183. hapd_iface->bss = os_zalloc(conf->num_bss *
  184. sizeof(struct hostapd_data *));
  185. if (hapd_iface->bss == NULL)
  186. goto fail;
  187. for (i = 0; i < conf->num_bss; i++) {
  188. hapd = hapd_iface->bss[i] =
  189. hostapd_alloc_bss_data(hapd_iface, conf,
  190. &conf->bss[i]);
  191. if (hapd == NULL)
  192. goto fail;
  193. hapd->msg_ctx = hapd;
  194. }
  195. return hapd_iface;
  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. static int hostapd_driver_init(struct hostapd_iface *iface)
  207. {
  208. struct wpa_init_params params;
  209. size_t i;
  210. struct hostapd_data *hapd = iface->bss[0];
  211. struct hostapd_bss_config *conf = hapd->conf;
  212. u8 *b = conf->bssid;
  213. struct wpa_driver_capa capa;
  214. if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
  215. wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
  216. return -1;
  217. }
  218. /* Initialize the driver interface */
  219. if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
  220. b = NULL;
  221. os_memset(&params, 0, sizeof(params));
  222. for (i = 0; wpa_drivers[i]; i++) {
  223. if (wpa_drivers[i] != hapd->driver)
  224. continue;
  225. if (global.drv_priv[i] == NULL &&
  226. wpa_drivers[i]->global_init) {
  227. global.drv_priv[i] = wpa_drivers[i]->global_init();
  228. if (global.drv_priv[i] == NULL) {
  229. wpa_printf(MSG_ERROR, "Failed to initialize "
  230. "driver '%s'",
  231. wpa_drivers[i]->name);
  232. return -1;
  233. }
  234. }
  235. params.global_priv = global.drv_priv[i];
  236. break;
  237. }
  238. params.bssid = b;
  239. params.ifname = hapd->conf->iface;
  240. params.ssid = (const u8 *) hapd->conf->ssid.ssid;
  241. params.ssid_len = hapd->conf->ssid.ssid_len;
  242. params.test_socket = hapd->conf->test_socket;
  243. params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
  244. params.num_bridge = hapd->iface->num_bss;
  245. params.bridge = os_zalloc(hapd->iface->num_bss * sizeof(char *));
  246. if (params.bridge == NULL)
  247. return -1;
  248. for (i = 0; i < hapd->iface->num_bss; i++) {
  249. struct hostapd_data *bss = hapd->iface->bss[i];
  250. if (bss->conf->bridge[0])
  251. params.bridge[i] = bss->conf->bridge;
  252. }
  253. params.own_addr = hapd->own_addr;
  254. hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
  255. os_free(params.bridge);
  256. if (hapd->drv_priv == NULL) {
  257. wpa_printf(MSG_ERROR, "%s driver initialization failed.",
  258. hapd->driver->name);
  259. hapd->driver = NULL;
  260. return -1;
  261. }
  262. if (hapd->driver->get_capa &&
  263. hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
  264. iface->drv_flags = capa.flags;
  265. iface->probe_resp_offloads = capa.probe_resp_offloads;
  266. }
  267. return 0;
  268. }
  269. static void hostapd_interface_deinit_free(struct hostapd_iface *iface)
  270. {
  271. const struct wpa_driver_ops *driver;
  272. void *drv_priv;
  273. if (iface == NULL)
  274. return;
  275. driver = iface->bss[0]->driver;
  276. drv_priv = iface->bss[0]->drv_priv;
  277. hostapd_interface_deinit(iface);
  278. if (driver && driver->hapd_deinit)
  279. driver->hapd_deinit(drv_priv);
  280. hostapd_interface_free(iface);
  281. }
  282. static struct hostapd_iface *
  283. hostapd_interface_init(struct hapd_interfaces *interfaces,
  284. const char *config_fname, int debug)
  285. {
  286. struct hostapd_iface *iface;
  287. int k;
  288. wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
  289. iface = hostapd_init(config_fname);
  290. if (!iface)
  291. return NULL;
  292. iface->interfaces = interfaces;
  293. for (k = 0; k < debug; k++) {
  294. if (iface->bss[0]->conf->logger_stdout_level > 0)
  295. iface->bss[0]->conf->logger_stdout_level--;
  296. }
  297. if (hostapd_driver_init(iface) ||
  298. hostapd_setup_interface(iface)) {
  299. hostapd_interface_deinit_free(iface);
  300. return NULL;
  301. }
  302. return iface;
  303. }
  304. /**
  305. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  306. */
  307. static void handle_term(int sig, void *signal_ctx)
  308. {
  309. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  310. eloop_terminate();
  311. }
  312. #ifndef CONFIG_NATIVE_WINDOWS
  313. static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
  314. {
  315. if (hostapd_reload_config(iface) < 0) {
  316. wpa_printf(MSG_WARNING, "Failed to read new configuration "
  317. "file - continuing with old.");
  318. }
  319. return 0;
  320. }
  321. /**
  322. * handle_reload - SIGHUP handler to reload configuration
  323. */
  324. static void handle_reload(int sig, void *signal_ctx)
  325. {
  326. struct hapd_interfaces *interfaces = signal_ctx;
  327. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  328. sig);
  329. hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
  330. }
  331. static void handle_dump_state(int sig, void *signal_ctx)
  332. {
  333. #ifdef HOSTAPD_DUMP_STATE
  334. struct hapd_interfaces *interfaces = signal_ctx;
  335. hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
  336. #endif /* HOSTAPD_DUMP_STATE */
  337. }
  338. #endif /* CONFIG_NATIVE_WINDOWS */
  339. static int hostapd_global_init(struct hapd_interfaces *interfaces,
  340. const char *entropy_file)
  341. {
  342. int i;
  343. os_memset(&global, 0, sizeof(global));
  344. hostapd_logger_register_cb(hostapd_logger_cb);
  345. if (eap_server_register_methods()) {
  346. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  347. return -1;
  348. }
  349. if (eloop_init()) {
  350. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  351. return -1;
  352. }
  353. random_init(entropy_file);
  354. #ifndef CONFIG_NATIVE_WINDOWS
  355. eloop_register_signal(SIGHUP, handle_reload, interfaces);
  356. eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
  357. #endif /* CONFIG_NATIVE_WINDOWS */
  358. eloop_register_signal_terminate(handle_term, interfaces);
  359. #ifndef CONFIG_NATIVE_WINDOWS
  360. openlog("hostapd", 0, LOG_DAEMON);
  361. #endif /* CONFIG_NATIVE_WINDOWS */
  362. for (i = 0; wpa_drivers[i]; i++)
  363. global.drv_count++;
  364. if (global.drv_count == 0) {
  365. wpa_printf(MSG_ERROR, "No drivers enabled");
  366. return -1;
  367. }
  368. global.drv_priv = os_zalloc(global.drv_count * sizeof(void *));
  369. if (global.drv_priv == NULL)
  370. return -1;
  371. return 0;
  372. }
  373. static void hostapd_global_deinit(const char *pid_file)
  374. {
  375. int i;
  376. for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
  377. if (!global.drv_priv[i])
  378. continue;
  379. wpa_drivers[i]->global_deinit(global.drv_priv[i]);
  380. }
  381. os_free(global.drv_priv);
  382. global.drv_priv = NULL;
  383. #ifdef EAP_SERVER_TNC
  384. tncs_global_deinit();
  385. #endif /* EAP_SERVER_TNC */
  386. random_deinit();
  387. eloop_destroy();
  388. #ifndef CONFIG_NATIVE_WINDOWS
  389. closelog();
  390. #endif /* CONFIG_NATIVE_WINDOWS */
  391. eap_server_unregister_methods();
  392. os_daemonize_terminate(pid_file);
  393. }
  394. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  395. const char *pid_file)
  396. {
  397. #ifdef EAP_SERVER_TNC
  398. int tnc = 0;
  399. size_t i, k;
  400. for (i = 0; !tnc && i < ifaces->count; i++) {
  401. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  402. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  403. tnc++;
  404. break;
  405. }
  406. }
  407. }
  408. if (tnc && tncs_global_init() < 0) {
  409. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  410. return -1;
  411. }
  412. #endif /* EAP_SERVER_TNC */
  413. if (daemonize && os_daemonize(pid_file)) {
  414. perror("daemon");
  415. return -1;
  416. }
  417. eloop_run();
  418. return 0;
  419. }
  420. static void show_version(void)
  421. {
  422. fprintf(stderr,
  423. "hostapd v" VERSION_STR "\n"
  424. "User space daemon for IEEE 802.11 AP management,\n"
  425. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  426. "Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi> "
  427. "and contributors\n");
  428. }
  429. static void usage(void)
  430. {
  431. show_version();
  432. fprintf(stderr,
  433. "\n"
  434. "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
  435. "<configuration file(s)>\n"
  436. "\n"
  437. "options:\n"
  438. " -h show this usage\n"
  439. " -d show more debug messages (-dd for even more)\n"
  440. " -B run daemon in the background\n"
  441. " -e entropy file\n"
  442. " -P PID file\n"
  443. " -K include key data in debug messages\n"
  444. #ifdef CONFIG_DEBUG_FILE
  445. " -f log output to debug file instead of stdout\n"
  446. #endif /* CONFIG_DEBUG_FILE */
  447. " -t include timestamps in some debug messages\n"
  448. " -v show hostapd version\n");
  449. exit(1);
  450. }
  451. static const char * hostapd_msg_ifname_cb(void *ctx)
  452. {
  453. struct hostapd_data *hapd = ctx;
  454. if (hapd && hapd->iconf && hapd->iconf->bss)
  455. return hapd->iconf->bss->iface;
  456. return NULL;
  457. }
  458. int main(int argc, char *argv[])
  459. {
  460. struct hapd_interfaces interfaces;
  461. int ret = 1;
  462. size_t i;
  463. int c, debug = 0, daemonize = 0;
  464. char *pid_file = NULL;
  465. const char *log_file = NULL;
  466. const char *entropy_file = NULL;
  467. if (os_program_init())
  468. return -1;
  469. for (;;) {
  470. c = getopt(argc, argv, "Bde:f:hKP:tv");
  471. if (c < 0)
  472. break;
  473. switch (c) {
  474. case 'h':
  475. usage();
  476. break;
  477. case 'd':
  478. debug++;
  479. if (wpa_debug_level > 0)
  480. wpa_debug_level--;
  481. break;
  482. case 'B':
  483. daemonize++;
  484. break;
  485. case 'e':
  486. entropy_file = optarg;
  487. break;
  488. case 'f':
  489. log_file = optarg;
  490. break;
  491. case 'K':
  492. wpa_debug_show_keys++;
  493. break;
  494. case 'P':
  495. os_free(pid_file);
  496. pid_file = os_rel2abs_path(optarg);
  497. break;
  498. case 't':
  499. wpa_debug_timestamp++;
  500. break;
  501. case 'v':
  502. show_version();
  503. exit(1);
  504. break;
  505. default:
  506. usage();
  507. break;
  508. }
  509. }
  510. if (optind == argc)
  511. usage();
  512. wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
  513. if (log_file)
  514. wpa_debug_open_file(log_file);
  515. interfaces.count = argc - optind;
  516. interfaces.iface = os_zalloc(interfaces.count *
  517. sizeof(struct hostapd_iface *));
  518. if (interfaces.iface == NULL) {
  519. wpa_printf(MSG_ERROR, "malloc failed");
  520. return -1;
  521. }
  522. if (hostapd_global_init(&interfaces, entropy_file))
  523. return -1;
  524. /* Initialize interfaces */
  525. for (i = 0; i < interfaces.count; i++) {
  526. interfaces.iface[i] = hostapd_interface_init(&interfaces,
  527. argv[optind + i],
  528. debug);
  529. if (!interfaces.iface[i])
  530. goto out;
  531. }
  532. if (hostapd_global_run(&interfaces, daemonize, pid_file))
  533. goto out;
  534. ret = 0;
  535. out:
  536. /* Deinitialize all interfaces */
  537. for (i = 0; i < interfaces.count; i++)
  538. hostapd_interface_deinit_free(interfaces.iface[i]);
  539. os_free(interfaces.iface);
  540. hostapd_global_deinit(pid_file);
  541. os_free(pid_file);
  542. if (log_file)
  543. wpa_debug_close_file();
  544. os_program_deinit();
  545. return ret;
  546. }