main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
  208. wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
  209. return -1;
  210. }
  211. /* Initialize the driver interface */
  212. if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
  213. b = NULL;
  214. os_memset(&params, 0, sizeof(params));
  215. params.bssid = b;
  216. params.ifname = hapd->conf->iface;
  217. params.ssid = (const u8 *) hapd->conf->ssid.ssid;
  218. params.ssid_len = hapd->conf->ssid.ssid_len;
  219. params.test_socket = hapd->conf->test_socket;
  220. params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
  221. params.num_bridge = hapd->iface->num_bss;
  222. params.bridge = os_zalloc(hapd->iface->num_bss * sizeof(char *));
  223. if (params.bridge == NULL)
  224. return -1;
  225. for (i = 0; i < hapd->iface->num_bss; i++) {
  226. struct hostapd_data *bss = hapd->iface->bss[i];
  227. if (bss->conf->bridge[0])
  228. params.bridge[i] = bss->conf->bridge;
  229. }
  230. params.own_addr = hapd->own_addr;
  231. hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
  232. os_free(params.bridge);
  233. if (hapd->drv_priv == NULL) {
  234. wpa_printf(MSG_ERROR, "%s driver initialization failed.",
  235. hapd->driver->name);
  236. hapd->driver = NULL;
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. static void hostapd_interface_deinit_free(struct hostapd_iface *iface)
  242. {
  243. const struct wpa_driver_ops *driver;
  244. void *drv_priv;
  245. if (iface == NULL)
  246. return;
  247. driver = iface->bss[0]->driver;
  248. drv_priv = iface->bss[0]->drv_priv;
  249. hostapd_interface_deinit(iface);
  250. if (driver && driver->hapd_deinit)
  251. driver->hapd_deinit(drv_priv);
  252. hostapd_interface_free(iface);
  253. }
  254. static struct hostapd_iface *
  255. hostapd_interface_init(struct hapd_interfaces *interfaces,
  256. const char *config_fname, int debug)
  257. {
  258. struct hostapd_iface *iface;
  259. int k;
  260. wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
  261. iface = hostapd_init(config_fname);
  262. if (!iface)
  263. return NULL;
  264. iface->interfaces = interfaces;
  265. for (k = 0; k < debug; k++) {
  266. if (iface->bss[0]->conf->logger_stdout_level > 0)
  267. iface->bss[0]->conf->logger_stdout_level--;
  268. }
  269. if (hostapd_driver_init(iface) ||
  270. hostapd_setup_interface(iface)) {
  271. hostapd_interface_deinit_free(iface);
  272. return NULL;
  273. }
  274. return iface;
  275. }
  276. /**
  277. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  278. */
  279. static void handle_term(int sig, void *signal_ctx)
  280. {
  281. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  282. eloop_terminate();
  283. }
  284. #ifndef CONFIG_NATIVE_WINDOWS
  285. static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
  286. {
  287. if (hostapd_reload_config(iface) < 0) {
  288. wpa_printf(MSG_WARNING, "Failed to read new configuration "
  289. "file - continuing with old.");
  290. }
  291. return 0;
  292. }
  293. /**
  294. * handle_reload - SIGHUP handler to reload configuration
  295. */
  296. static void handle_reload(int sig, void *signal_ctx)
  297. {
  298. struct hapd_interfaces *interfaces = signal_ctx;
  299. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  300. sig);
  301. hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
  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. static int hostapd_global_init(struct hapd_interfaces *interfaces)
  312. {
  313. hostapd_logger_register_cb(hostapd_logger_cb);
  314. if (eap_server_register_methods()) {
  315. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  316. return -1;
  317. }
  318. if (eloop_init()) {
  319. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  320. return -1;
  321. }
  322. random_init();
  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. #ifndef CONFIG_NATIVE_WINDOWS
  329. openlog("hostapd", 0, LOG_DAEMON);
  330. #endif /* CONFIG_NATIVE_WINDOWS */
  331. return 0;
  332. }
  333. static void hostapd_global_deinit(const char *pid_file)
  334. {
  335. #ifdef EAP_SERVER_TNC
  336. tncs_global_deinit();
  337. #endif /* EAP_SERVER_TNC */
  338. random_deinit();
  339. eloop_destroy();
  340. #ifndef CONFIG_NATIVE_WINDOWS
  341. closelog();
  342. #endif /* CONFIG_NATIVE_WINDOWS */
  343. eap_server_unregister_methods();
  344. os_daemonize_terminate(pid_file);
  345. }
  346. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  347. const char *pid_file)
  348. {
  349. #ifdef EAP_SERVER_TNC
  350. int tnc = 0;
  351. size_t i, k;
  352. for (i = 0; !tnc && i < ifaces->count; i++) {
  353. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  354. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  355. tnc++;
  356. break;
  357. }
  358. }
  359. }
  360. if (tnc && tncs_global_init() < 0) {
  361. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  362. return -1;
  363. }
  364. #endif /* EAP_SERVER_TNC */
  365. if (daemonize && os_daemonize(pid_file)) {
  366. perror("daemon");
  367. return -1;
  368. }
  369. eloop_run();
  370. return 0;
  371. }
  372. static void show_version(void)
  373. {
  374. fprintf(stderr,
  375. "hostapd v" VERSION_STR "\n"
  376. "User space daemon for IEEE 802.11 AP management,\n"
  377. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  378. "Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi> "
  379. "and contributors\n");
  380. }
  381. static void usage(void)
  382. {
  383. show_version();
  384. fprintf(stderr,
  385. "\n"
  386. "usage: hostapd [-hdBKtv] [-P <PID file>] "
  387. "<configuration file(s)>\n"
  388. "\n"
  389. "options:\n"
  390. " -h show this usage\n"
  391. " -d show more debug messages (-dd for even more)\n"
  392. " -B run daemon in the background\n"
  393. " -P PID file\n"
  394. " -K include key data in debug messages\n"
  395. #ifdef CONFIG_DEBUG_FILE
  396. " -f log output to debug file instead of stdout\n"
  397. #endif /* CONFIG_DEBUG_FILE */
  398. " -t include timestamps in some debug messages\n"
  399. " -v show hostapd version\n");
  400. exit(1);
  401. }
  402. static const char * hostapd_msg_ifname_cb(void *ctx)
  403. {
  404. struct hostapd_data *hapd = ctx;
  405. if (hapd && hapd->iconf && hapd->iconf->bss)
  406. return hapd->iconf->bss->iface;
  407. return NULL;
  408. }
  409. int main(int argc, char *argv[])
  410. {
  411. struct hapd_interfaces interfaces;
  412. int ret = 1;
  413. size_t i;
  414. int c, debug = 0, daemonize = 0;
  415. char *pid_file = NULL;
  416. const char *log_file = NULL;
  417. if (os_program_init())
  418. return -1;
  419. for (;;) {
  420. c = getopt(argc, argv, "Bdf:hKP:tv");
  421. if (c < 0)
  422. break;
  423. switch (c) {
  424. case 'h':
  425. usage();
  426. break;
  427. case 'd':
  428. debug++;
  429. if (wpa_debug_level > 0)
  430. wpa_debug_level--;
  431. break;
  432. case 'B':
  433. daemonize++;
  434. break;
  435. case 'f':
  436. log_file = optarg;
  437. break;
  438. case 'K':
  439. wpa_debug_show_keys++;
  440. break;
  441. case 'P':
  442. os_free(pid_file);
  443. pid_file = os_rel2abs_path(optarg);
  444. break;
  445. case 't':
  446. wpa_debug_timestamp++;
  447. break;
  448. case 'v':
  449. show_version();
  450. exit(1);
  451. break;
  452. default:
  453. usage();
  454. break;
  455. }
  456. }
  457. if (optind == argc)
  458. usage();
  459. wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
  460. if (log_file)
  461. wpa_debug_open_file(log_file);
  462. interfaces.count = argc - optind;
  463. interfaces.iface = os_malloc(interfaces.count *
  464. sizeof(struct hostapd_iface *));
  465. if (interfaces.iface == NULL) {
  466. wpa_printf(MSG_ERROR, "malloc failed\n");
  467. return -1;
  468. }
  469. if (hostapd_global_init(&interfaces))
  470. return -1;
  471. /* Initialize interfaces */
  472. for (i = 0; i < interfaces.count; i++) {
  473. interfaces.iface[i] = hostapd_interface_init(&interfaces,
  474. argv[optind + i],
  475. debug);
  476. if (!interfaces.iface[i])
  477. goto out;
  478. }
  479. if (hostapd_global_run(&interfaces, daemonize, pid_file))
  480. goto out;
  481. ret = 0;
  482. out:
  483. /* Deinitialize all interfaces */
  484. for (i = 0; i < interfaces.count; i++)
  485. hostapd_interface_deinit_free(interfaces.iface[i]);
  486. os_free(interfaces.iface);
  487. hostapd_global_deinit(pid_file);
  488. os_free(pid_file);
  489. if (log_file)
  490. wpa_debug_close_file();
  491. os_program_deinit();
  492. return ret;
  493. }