main.c 12 KB

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