main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * hostapd / main()
  3. * Copyright (c) 2002-2009, 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 "includes.h"
  15. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include <syslog.h>
  17. #endif /* CONFIG_NATIVE_WINDOWS */
  18. #include "common.h"
  19. #include "eloop.h"
  20. #include "crypto/tls.h"
  21. #include "common/version.h"
  22. #include "eap_server/eap.h"
  23. #include "eap_server/tncs.h"
  24. #include "ap/hostapd.h"
  25. #include "ap/config.h"
  26. #include "config_file.h"
  27. extern int wpa_debug_level;
  28. extern int wpa_debug_show_keys;
  29. extern int wpa_debug_timestamp;
  30. struct hapd_interfaces {
  31. size_t count;
  32. struct hostapd_iface **iface;
  33. };
  34. int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
  35. int (*cb)(struct hostapd_iface *iface,
  36. void *ctx), void *ctx)
  37. {
  38. size_t i;
  39. int ret;
  40. for (i = 0; i < interfaces->count; i++) {
  41. ret = cb(interfaces->iface[i], ctx);
  42. if (ret)
  43. return ret;
  44. }
  45. return 0;
  46. }
  47. #ifndef CONFIG_NO_HOSTAPD_LOGGER
  48. static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
  49. int level, const char *txt, size_t len)
  50. {
  51. struct hostapd_data *hapd = ctx;
  52. char *format, *module_str;
  53. int maxlen;
  54. int conf_syslog_level, conf_stdout_level;
  55. unsigned int conf_syslog, conf_stdout;
  56. maxlen = len + 100;
  57. format = os_malloc(maxlen);
  58. if (!format)
  59. return;
  60. if (hapd && hapd->conf) {
  61. conf_syslog_level = hapd->conf->logger_syslog_level;
  62. conf_stdout_level = hapd->conf->logger_stdout_level;
  63. conf_syslog = hapd->conf->logger_syslog;
  64. conf_stdout = hapd->conf->logger_stdout;
  65. } else {
  66. conf_syslog_level = conf_stdout_level = 0;
  67. conf_syslog = conf_stdout = (unsigned int) -1;
  68. }
  69. switch (module) {
  70. case HOSTAPD_MODULE_IEEE80211:
  71. module_str = "IEEE 802.11";
  72. break;
  73. case HOSTAPD_MODULE_IEEE8021X:
  74. module_str = "IEEE 802.1X";
  75. break;
  76. case HOSTAPD_MODULE_RADIUS:
  77. module_str = "RADIUS";
  78. break;
  79. case HOSTAPD_MODULE_WPA:
  80. module_str = "WPA";
  81. break;
  82. case HOSTAPD_MODULE_DRIVER:
  83. module_str = "DRIVER";
  84. break;
  85. case HOSTAPD_MODULE_IAPP:
  86. module_str = "IAPP";
  87. break;
  88. case HOSTAPD_MODULE_MLME:
  89. module_str = "MLME";
  90. break;
  91. default:
  92. module_str = NULL;
  93. break;
  94. }
  95. if (hapd && hapd->conf && addr)
  96. os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
  97. hapd->conf->iface, MAC2STR(addr),
  98. module_str ? " " : "", module_str, txt);
  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. if ((conf_stdout & module) && level >= conf_stdout_level) {
  111. wpa_debug_print_timestamp();
  112. printf("%s\n", format);
  113. }
  114. #ifndef CONFIG_NATIVE_WINDOWS
  115. if ((conf_syslog & module) && level >= conf_syslog_level) {
  116. int priority;
  117. switch (level) {
  118. case HOSTAPD_LEVEL_DEBUG_VERBOSE:
  119. case HOSTAPD_LEVEL_DEBUG:
  120. priority = LOG_DEBUG;
  121. break;
  122. case HOSTAPD_LEVEL_INFO:
  123. priority = LOG_INFO;
  124. break;
  125. case HOSTAPD_LEVEL_NOTICE:
  126. priority = LOG_NOTICE;
  127. break;
  128. case HOSTAPD_LEVEL_WARNING:
  129. priority = LOG_WARNING;
  130. break;
  131. default:
  132. priority = LOG_INFO;
  133. break;
  134. }
  135. syslog(priority, "%s", format);
  136. }
  137. #endif /* CONFIG_NATIVE_WINDOWS */
  138. os_free(format);
  139. }
  140. #endif /* CONFIG_NO_HOSTAPD_LOGGER */
  141. /**
  142. * hostapd_init - Allocate and initialize per-interface data
  143. * @config_file: Path to the configuration file
  144. * Returns: Pointer to the allocated interface data or %NULL on failure
  145. *
  146. * This function is used to allocate main data structures for per-interface
  147. * data. The allocated data buffer will be freed by calling
  148. * hostapd_cleanup_iface().
  149. */
  150. static struct hostapd_iface * hostapd_init(const char *config_file)
  151. {
  152. struct hostapd_iface *hapd_iface = NULL;
  153. struct hostapd_config *conf = NULL;
  154. struct hostapd_data *hapd;
  155. size_t i;
  156. hapd_iface = os_zalloc(sizeof(*hapd_iface));
  157. if (hapd_iface == NULL)
  158. goto fail;
  159. hapd_iface->reload_config = hostapd_reload_config;
  160. hapd_iface->config_read_cb = hostapd_config_read;
  161. hapd_iface->config_fname = os_strdup(config_file);
  162. if (hapd_iface->config_fname == NULL)
  163. goto fail;
  164. conf = hostapd_config_read(hapd_iface->config_fname);
  165. if (conf == NULL)
  166. goto fail;
  167. hapd_iface->conf = conf;
  168. hapd_iface->num_bss = conf->num_bss;
  169. hapd_iface->bss = os_zalloc(conf->num_bss *
  170. sizeof(struct hostapd_data *));
  171. if (hapd_iface->bss == NULL)
  172. goto fail;
  173. for (i = 0; i < conf->num_bss; i++) {
  174. hapd = hapd_iface->bss[i] =
  175. hostapd_alloc_bss_data(hapd_iface, conf,
  176. &conf->bss[i]);
  177. if (hapd == NULL)
  178. goto fail;
  179. }
  180. return hapd_iface;
  181. fail:
  182. if (conf)
  183. hostapd_config_free(conf);
  184. if (hapd_iface) {
  185. for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
  186. hapd = hapd_iface->bss[i];
  187. if (hapd && hapd->ssl_ctx)
  188. tls_deinit(hapd->ssl_ctx);
  189. }
  190. os_free(hapd_iface->config_fname);
  191. os_free(hapd_iface->bss);
  192. os_free(hapd_iface);
  193. }
  194. return NULL;
  195. }
  196. static struct hostapd_iface *
  197. hostapd_interface_init(struct hapd_interfaces *interfaces,
  198. const char *config_fname, int debug)
  199. {
  200. struct hostapd_iface *iface;
  201. int k;
  202. wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
  203. iface = hostapd_init(config_fname);
  204. if (!iface)
  205. return NULL;
  206. iface->interfaces = interfaces;
  207. for (k = 0; k < debug; k++) {
  208. if (iface->bss[0]->conf->logger_stdout_level > 0)
  209. iface->bss[0]->conf->logger_stdout_level--;
  210. }
  211. if (hostapd_setup_interface(iface)) {
  212. hostapd_interface_deinit(iface);
  213. return NULL;
  214. }
  215. return iface;
  216. }
  217. /**
  218. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  219. */
  220. static void handle_term(int sig, void *signal_ctx)
  221. {
  222. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  223. eloop_terminate();
  224. }
  225. #ifndef CONFIG_NATIVE_WINDOWS
  226. /**
  227. * handle_reload - SIGHUP handler to reload configuration
  228. */
  229. static void handle_reload(int sig, void *signal_ctx)
  230. {
  231. struct hapd_interfaces *interfaces = signal_ctx;
  232. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  233. sig);
  234. hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
  235. }
  236. static void handle_dump_state(int sig, void *signal_ctx)
  237. {
  238. #ifdef HOSTAPD_DUMP_STATE
  239. struct hapd_interfaces *interfaces = signal_ctx;
  240. hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
  241. #endif /* HOSTAPD_DUMP_STATE */
  242. }
  243. #endif /* CONFIG_NATIVE_WINDOWS */
  244. static int hostapd_global_init(struct hapd_interfaces *interfaces)
  245. {
  246. hostapd_logger_register_cb(hostapd_logger_cb);
  247. if (eap_server_register_methods()) {
  248. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  249. return -1;
  250. }
  251. if (eloop_init()) {
  252. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  253. return -1;
  254. }
  255. #ifndef CONFIG_NATIVE_WINDOWS
  256. eloop_register_signal(SIGHUP, handle_reload, interfaces);
  257. eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
  258. #endif /* CONFIG_NATIVE_WINDOWS */
  259. eloop_register_signal_terminate(handle_term, interfaces);
  260. #ifndef CONFIG_NATIVE_WINDOWS
  261. openlog("hostapd", 0, LOG_DAEMON);
  262. #endif /* CONFIG_NATIVE_WINDOWS */
  263. return 0;
  264. }
  265. static void hostapd_global_deinit(const char *pid_file)
  266. {
  267. #ifdef EAP_SERVER_TNC
  268. tncs_global_deinit();
  269. #endif /* EAP_SERVER_TNC */
  270. eloop_destroy();
  271. #ifndef CONFIG_NATIVE_WINDOWS
  272. closelog();
  273. #endif /* CONFIG_NATIVE_WINDOWS */
  274. eap_server_unregister_methods();
  275. os_daemonize_terminate(pid_file);
  276. }
  277. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  278. const char *pid_file)
  279. {
  280. #ifdef EAP_SERVER_TNC
  281. int tnc = 0;
  282. size_t i, k;
  283. for (i = 0; !tnc && i < ifaces->count; i++) {
  284. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  285. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  286. tnc++;
  287. break;
  288. }
  289. }
  290. }
  291. if (tnc && tncs_global_init() < 0) {
  292. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  293. return -1;
  294. }
  295. #endif /* EAP_SERVER_TNC */
  296. if (daemonize && os_daemonize(pid_file)) {
  297. perror("daemon");
  298. return -1;
  299. }
  300. eloop_run();
  301. return 0;
  302. }
  303. static void show_version(void)
  304. {
  305. fprintf(stderr,
  306. "hostapd v" VERSION_STR "\n"
  307. "User space daemon for IEEE 802.11 AP management,\n"
  308. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  309. "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
  310. "and contributors\n");
  311. }
  312. static void usage(void)
  313. {
  314. show_version();
  315. fprintf(stderr,
  316. "\n"
  317. "usage: hostapd [-hdBKtv] [-P <PID file>] "
  318. "<configuration file(s)>\n"
  319. "\n"
  320. "options:\n"
  321. " -h show this usage\n"
  322. " -d show more debug messages (-dd for even more)\n"
  323. " -B run daemon in the background\n"
  324. " -P PID file\n"
  325. " -K include key data in debug messages\n"
  326. " -t include timestamps in some debug messages\n"
  327. " -v show hostapd version\n");
  328. exit(1);
  329. }
  330. int main(int argc, char *argv[])
  331. {
  332. struct hapd_interfaces interfaces;
  333. int ret = 1;
  334. size_t i;
  335. int c, debug = 0, daemonize = 0;
  336. char *pid_file = NULL;
  337. if (os_program_init())
  338. return -1;
  339. for (;;) {
  340. c = getopt(argc, argv, "BdhKP:tv");
  341. if (c < 0)
  342. break;
  343. switch (c) {
  344. case 'h':
  345. usage();
  346. break;
  347. case 'd':
  348. debug++;
  349. if (wpa_debug_level > 0)
  350. wpa_debug_level--;
  351. break;
  352. case 'B':
  353. daemonize++;
  354. break;
  355. case 'K':
  356. wpa_debug_show_keys++;
  357. break;
  358. case 'P':
  359. os_free(pid_file);
  360. pid_file = os_rel2abs_path(optarg);
  361. break;
  362. case 't':
  363. wpa_debug_timestamp++;
  364. break;
  365. case 'v':
  366. show_version();
  367. exit(1);
  368. break;
  369. default:
  370. usage();
  371. break;
  372. }
  373. }
  374. if (optind == argc)
  375. usage();
  376. interfaces.count = argc - optind;
  377. interfaces.iface = os_malloc(interfaces.count *
  378. sizeof(struct hostapd_iface *));
  379. if (interfaces.iface == NULL) {
  380. wpa_printf(MSG_ERROR, "malloc failed\n");
  381. return -1;
  382. }
  383. if (hostapd_global_init(&interfaces))
  384. return -1;
  385. /* Initialize interfaces */
  386. for (i = 0; i < interfaces.count; i++) {
  387. interfaces.iface[i] = hostapd_interface_init(&interfaces,
  388. argv[optind + i],
  389. debug);
  390. if (!interfaces.iface[i])
  391. goto out;
  392. }
  393. if (hostapd_global_run(&interfaces, daemonize, pid_file))
  394. goto out;
  395. ret = 0;
  396. out:
  397. /* Deinitialize all interfaces */
  398. for (i = 0; i < interfaces.count; i++)
  399. hostapd_interface_deinit(interfaces.iface[i]);
  400. os_free(interfaces.iface);
  401. hostapd_global_deinit(pid_file);
  402. os_free(pid_file);
  403. os_program_deinit();
  404. return ret;
  405. }