main.c 13 KB

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