Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

main.c 13KB

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