main.c 14 KB

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