main.c 14 KB

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