main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. if (iface == NULL)
  244. return;
  245. driver = iface->bss[0]->driver;
  246. drv_priv = iface->bss[0]->drv_priv;
  247. hostapd_interface_deinit(iface);
  248. if (driver && driver->hapd_deinit)
  249. driver->hapd_deinit(drv_priv);
  250. hostapd_interface_free(iface);
  251. }
  252. static struct hostapd_iface *
  253. hostapd_interface_init(struct hapd_interfaces *interfaces,
  254. const char *config_fname, int debug)
  255. {
  256. struct hostapd_iface *iface;
  257. int k;
  258. wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
  259. iface = hostapd_init(config_fname);
  260. if (!iface)
  261. return NULL;
  262. iface->interfaces = interfaces;
  263. for (k = 0; k < debug; k++) {
  264. if (iface->bss[0]->conf->logger_stdout_level > 0)
  265. iface->bss[0]->conf->logger_stdout_level--;
  266. }
  267. if (hostapd_driver_init(iface) ||
  268. hostapd_setup_interface(iface)) {
  269. hostapd_interface_deinit_free(iface);
  270. return NULL;
  271. }
  272. return iface;
  273. }
  274. /**
  275. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  276. */
  277. static void handle_term(int sig, void *signal_ctx)
  278. {
  279. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  280. eloop_terminate();
  281. }
  282. #ifndef CONFIG_NATIVE_WINDOWS
  283. static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
  284. {
  285. if (hostapd_reload_config(iface) < 0) {
  286. wpa_printf(MSG_WARNING, "Failed to read new configuration "
  287. "file - continuing with old.");
  288. }
  289. return 0;
  290. }
  291. /**
  292. * handle_reload - SIGHUP handler to reload configuration
  293. */
  294. static void handle_reload(int sig, void *signal_ctx)
  295. {
  296. struct hapd_interfaces *interfaces = signal_ctx;
  297. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  298. sig);
  299. hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
  300. }
  301. static void handle_dump_state(int sig, void *signal_ctx)
  302. {
  303. #ifdef HOSTAPD_DUMP_STATE
  304. struct hapd_interfaces *interfaces = signal_ctx;
  305. hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
  306. #endif /* HOSTAPD_DUMP_STATE */
  307. }
  308. #endif /* CONFIG_NATIVE_WINDOWS */
  309. static int hostapd_global_init(struct hapd_interfaces *interfaces)
  310. {
  311. hostapd_logger_register_cb(hostapd_logger_cb);
  312. if (eap_server_register_methods()) {
  313. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  314. return -1;
  315. }
  316. if (eloop_init()) {
  317. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  318. return -1;
  319. }
  320. #ifndef CONFIG_NATIVE_WINDOWS
  321. eloop_register_signal(SIGHUP, handle_reload, interfaces);
  322. eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
  323. #endif /* CONFIG_NATIVE_WINDOWS */
  324. eloop_register_signal_terminate(handle_term, interfaces);
  325. #ifndef CONFIG_NATIVE_WINDOWS
  326. openlog("hostapd", 0, LOG_DAEMON);
  327. #endif /* CONFIG_NATIVE_WINDOWS */
  328. return 0;
  329. }
  330. static void hostapd_global_deinit(const char *pid_file)
  331. {
  332. #ifdef EAP_SERVER_TNC
  333. tncs_global_deinit();
  334. #endif /* EAP_SERVER_TNC */
  335. eloop_destroy();
  336. #ifndef CONFIG_NATIVE_WINDOWS
  337. closelog();
  338. #endif /* CONFIG_NATIVE_WINDOWS */
  339. eap_server_unregister_methods();
  340. os_daemonize_terminate(pid_file);
  341. }
  342. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  343. const char *pid_file)
  344. {
  345. #ifdef EAP_SERVER_TNC
  346. int tnc = 0;
  347. size_t i, k;
  348. for (i = 0; !tnc && i < ifaces->count; i++) {
  349. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  350. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  351. tnc++;
  352. break;
  353. }
  354. }
  355. }
  356. if (tnc && tncs_global_init() < 0) {
  357. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  358. return -1;
  359. }
  360. #endif /* EAP_SERVER_TNC */
  361. if (daemonize && os_daemonize(pid_file)) {
  362. perror("daemon");
  363. return -1;
  364. }
  365. eloop_run();
  366. return 0;
  367. }
  368. static void show_version(void)
  369. {
  370. fprintf(stderr,
  371. "hostapd v" VERSION_STR "\n"
  372. "User space daemon for IEEE 802.11 AP management,\n"
  373. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  374. "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
  375. "and contributors\n");
  376. }
  377. static void usage(void)
  378. {
  379. show_version();
  380. fprintf(stderr,
  381. "\n"
  382. "usage: hostapd [-hdBKtv] [-P <PID file>] "
  383. "<configuration file(s)>\n"
  384. "\n"
  385. "options:\n"
  386. " -h show this usage\n"
  387. " -d show more debug messages (-dd for even more)\n"
  388. " -B run daemon in the background\n"
  389. " -P PID file\n"
  390. " -K include key data in debug messages\n"
  391. " -t include timestamps in some debug messages\n"
  392. " -v show hostapd version\n");
  393. exit(1);
  394. }
  395. int main(int argc, char *argv[])
  396. {
  397. struct hapd_interfaces interfaces;
  398. int ret = 1;
  399. size_t i;
  400. int c, debug = 0, daemonize = 0;
  401. char *pid_file = NULL;
  402. if (os_program_init())
  403. return -1;
  404. for (;;) {
  405. c = getopt(argc, argv, "BdhKP:tv");
  406. if (c < 0)
  407. break;
  408. switch (c) {
  409. case 'h':
  410. usage();
  411. break;
  412. case 'd':
  413. debug++;
  414. if (wpa_debug_level > 0)
  415. wpa_debug_level--;
  416. break;
  417. case 'B':
  418. daemonize++;
  419. break;
  420. case 'K':
  421. wpa_debug_show_keys++;
  422. break;
  423. case 'P':
  424. os_free(pid_file);
  425. pid_file = os_rel2abs_path(optarg);
  426. break;
  427. case 't':
  428. wpa_debug_timestamp++;
  429. break;
  430. case 'v':
  431. show_version();
  432. exit(1);
  433. break;
  434. default:
  435. usage();
  436. break;
  437. }
  438. }
  439. if (optind == argc)
  440. usage();
  441. interfaces.count = argc - optind;
  442. interfaces.iface = os_malloc(interfaces.count *
  443. sizeof(struct hostapd_iface *));
  444. if (interfaces.iface == NULL) {
  445. wpa_printf(MSG_ERROR, "malloc failed\n");
  446. return -1;
  447. }
  448. if (hostapd_global_init(&interfaces))
  449. return -1;
  450. /* Initialize interfaces */
  451. for (i = 0; i < interfaces.count; i++) {
  452. interfaces.iface[i] = hostapd_interface_init(&interfaces,
  453. argv[optind + i],
  454. debug);
  455. if (!interfaces.iface[i])
  456. goto out;
  457. }
  458. if (hostapd_global_run(&interfaces, daemonize, pid_file))
  459. goto out;
  460. ret = 0;
  461. out:
  462. /* Deinitialize all interfaces */
  463. for (i = 0; i < interfaces.count; i++)
  464. hostapd_interface_deinit_free(interfaces.iface[i]);
  465. os_free(interfaces.iface);
  466. hostapd_global_deinit(pid_file);
  467. os_free(pid_file);
  468. os_program_deinit();
  469. return ret;
  470. }