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