main.c 13 KB

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