main.c 8.5 KB

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