main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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->config_fname = os_strdup(config_file);
  149. if (hapd_iface->config_fname == NULL)
  150. goto fail;
  151. conf = hostapd_config_read(hapd_iface->config_fname);
  152. if (conf == NULL)
  153. goto fail;
  154. hapd_iface->conf = conf;
  155. hapd_iface->num_bss = conf->num_bss;
  156. hapd_iface->bss = os_calloc(conf->num_bss,
  157. sizeof(struct hostapd_data *));
  158. if (hapd_iface->bss == NULL)
  159. goto fail;
  160. for (i = 0; i < conf->num_bss; i++) {
  161. hapd = hapd_iface->bss[i] =
  162. hostapd_alloc_bss_data(hapd_iface, conf,
  163. &conf->bss[i]);
  164. if (hapd == NULL)
  165. goto fail;
  166. hapd->msg_ctx = hapd;
  167. }
  168. return hapd_iface;
  169. fail:
  170. if (conf)
  171. hostapd_config_free(conf);
  172. if (hapd_iface) {
  173. os_free(hapd_iface->config_fname);
  174. os_free(hapd_iface->bss);
  175. os_free(hapd_iface);
  176. }
  177. return NULL;
  178. }
  179. static int hostapd_driver_init(struct hostapd_iface *iface)
  180. {
  181. struct wpa_init_params params;
  182. size_t i;
  183. struct hostapd_data *hapd = iface->bss[0];
  184. struct hostapd_bss_config *conf = hapd->conf;
  185. u8 *b = conf->bssid;
  186. struct wpa_driver_capa capa;
  187. if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
  188. wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
  189. return -1;
  190. }
  191. /* Initialize the driver interface */
  192. if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
  193. b = NULL;
  194. os_memset(&params, 0, sizeof(params));
  195. for (i = 0; wpa_drivers[i]; i++) {
  196. if (wpa_drivers[i] != hapd->driver)
  197. continue;
  198. if (global.drv_priv[i] == NULL &&
  199. wpa_drivers[i]->global_init) {
  200. global.drv_priv[i] = wpa_drivers[i]->global_init();
  201. if (global.drv_priv[i] == NULL) {
  202. wpa_printf(MSG_ERROR, "Failed to initialize "
  203. "driver '%s'",
  204. wpa_drivers[i]->name);
  205. return -1;
  206. }
  207. }
  208. params.global_priv = global.drv_priv[i];
  209. break;
  210. }
  211. params.bssid = b;
  212. params.ifname = hapd->conf->iface;
  213. params.ssid = hapd->conf->ssid.ssid;
  214. params.ssid_len = hapd->conf->ssid.ssid_len;
  215. params.test_socket = hapd->conf->test_socket;
  216. params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
  217. params.num_bridge = hapd->iface->num_bss;
  218. params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
  219. if (params.bridge == NULL)
  220. return -1;
  221. for (i = 0; i < hapd->iface->num_bss; i++) {
  222. struct hostapd_data *bss = hapd->iface->bss[i];
  223. if (bss->conf->bridge[0])
  224. params.bridge[i] = bss->conf->bridge;
  225. }
  226. params.own_addr = hapd->own_addr;
  227. hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
  228. os_free(params.bridge);
  229. if (hapd->drv_priv == NULL) {
  230. wpa_printf(MSG_ERROR, "%s driver initialization failed.",
  231. hapd->driver->name);
  232. hapd->driver = NULL;
  233. return -1;
  234. }
  235. if (hapd->driver->get_capa &&
  236. hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
  237. iface->drv_flags = capa.flags;
  238. iface->probe_resp_offloads = capa.probe_resp_offloads;
  239. }
  240. return 0;
  241. }
  242. static struct hostapd_iface *
  243. hostapd_interface_init(struct hapd_interfaces *interfaces,
  244. const char *config_fname, int debug)
  245. {
  246. struct hostapd_iface *iface;
  247. int k;
  248. wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
  249. iface = hostapd_init(config_fname);
  250. if (!iface)
  251. return NULL;
  252. iface->interfaces = interfaces;
  253. for (k = 0; k < debug; k++) {
  254. if (iface->bss[0]->conf->logger_stdout_level > 0)
  255. iface->bss[0]->conf->logger_stdout_level--;
  256. }
  257. if (iface->conf->bss[0].iface[0] != 0 ||
  258. hostapd_drv_none(iface->bss[0])) {
  259. if (hostapd_driver_init(iface) ||
  260. hostapd_setup_interface(iface)) {
  261. hostapd_interface_deinit_free(iface);
  262. return NULL;
  263. }
  264. }
  265. return iface;
  266. }
  267. /**
  268. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  269. */
  270. static void handle_term(int sig, void *signal_ctx)
  271. {
  272. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  273. eloop_terminate();
  274. }
  275. #ifndef CONFIG_NATIVE_WINDOWS
  276. static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
  277. {
  278. if (hostapd_reload_config(iface) < 0) {
  279. wpa_printf(MSG_WARNING, "Failed to read new configuration "
  280. "file - continuing with old.");
  281. }
  282. return 0;
  283. }
  284. /**
  285. * handle_reload - SIGHUP handler to reload configuration
  286. */
  287. static void handle_reload(int sig, void *signal_ctx)
  288. {
  289. struct hapd_interfaces *interfaces = signal_ctx;
  290. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  291. sig);
  292. hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
  293. }
  294. static void handle_dump_state(int sig, void *signal_ctx)
  295. {
  296. #ifdef HOSTAPD_DUMP_STATE
  297. struct hapd_interfaces *interfaces = signal_ctx;
  298. hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
  299. #endif /* HOSTAPD_DUMP_STATE */
  300. }
  301. #endif /* CONFIG_NATIVE_WINDOWS */
  302. static int hostapd_global_init(struct hapd_interfaces *interfaces,
  303. const char *entropy_file)
  304. {
  305. int i;
  306. os_memset(&global, 0, sizeof(global));
  307. hostapd_logger_register_cb(hostapd_logger_cb);
  308. if (eap_server_register_methods()) {
  309. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  310. return -1;
  311. }
  312. if (eloop_init()) {
  313. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  314. return -1;
  315. }
  316. random_init(entropy_file);
  317. #ifndef CONFIG_NATIVE_WINDOWS
  318. eloop_register_signal(SIGHUP, handle_reload, interfaces);
  319. eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
  320. #endif /* CONFIG_NATIVE_WINDOWS */
  321. eloop_register_signal_terminate(handle_term, interfaces);
  322. #ifndef CONFIG_NATIVE_WINDOWS
  323. openlog("hostapd", 0, LOG_DAEMON);
  324. #endif /* CONFIG_NATIVE_WINDOWS */
  325. for (i = 0; wpa_drivers[i]; i++)
  326. global.drv_count++;
  327. if (global.drv_count == 0) {
  328. wpa_printf(MSG_ERROR, "No drivers enabled");
  329. return -1;
  330. }
  331. global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
  332. if (global.drv_priv == NULL)
  333. return -1;
  334. return 0;
  335. }
  336. static void hostapd_global_deinit(const char *pid_file)
  337. {
  338. int i;
  339. for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
  340. if (!global.drv_priv[i])
  341. continue;
  342. wpa_drivers[i]->global_deinit(global.drv_priv[i]);
  343. }
  344. os_free(global.drv_priv);
  345. global.drv_priv = NULL;
  346. #ifdef EAP_SERVER_TNC
  347. tncs_global_deinit();
  348. #endif /* EAP_SERVER_TNC */
  349. random_deinit();
  350. eloop_destroy();
  351. #ifndef CONFIG_NATIVE_WINDOWS
  352. closelog();
  353. #endif /* CONFIG_NATIVE_WINDOWS */
  354. eap_server_unregister_methods();
  355. os_daemonize_terminate(pid_file);
  356. }
  357. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  358. const char *pid_file)
  359. {
  360. #ifdef EAP_SERVER_TNC
  361. int tnc = 0;
  362. size_t i, k;
  363. for (i = 0; !tnc && i < ifaces->count; i++) {
  364. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  365. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  366. tnc++;
  367. break;
  368. }
  369. }
  370. }
  371. if (tnc && tncs_global_init() < 0) {
  372. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  373. return -1;
  374. }
  375. #endif /* EAP_SERVER_TNC */
  376. if (daemonize && os_daemonize(pid_file)) {
  377. perror("daemon");
  378. return -1;
  379. }
  380. eloop_run();
  381. return 0;
  382. }
  383. static void show_version(void)
  384. {
  385. fprintf(stderr,
  386. "hostapd v" VERSION_STR "\n"
  387. "User space daemon for IEEE 802.11 AP management,\n"
  388. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  389. "Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> "
  390. "and contributors\n");
  391. }
  392. static void usage(void)
  393. {
  394. show_version();
  395. fprintf(stderr,
  396. "\n"
  397. "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
  398. "\\\n"
  399. " [-g <global ctrl_iface>] <configuration file(s)>\n"
  400. "\n"
  401. "options:\n"
  402. " -h show this usage\n"
  403. " -d show more debug messages (-dd for even more)\n"
  404. " -B run daemon in the background\n"
  405. " -e entropy file\n"
  406. " -g global control interface path\n"
  407. " -P PID file\n"
  408. " -K include key data in debug messages\n"
  409. #ifdef CONFIG_DEBUG_FILE
  410. " -f log output to debug file instead of stdout\n"
  411. #endif /* CONFIG_DEBUG_FILE */
  412. " -t include timestamps in some debug messages\n"
  413. " -v show hostapd version\n");
  414. exit(1);
  415. }
  416. static const char * hostapd_msg_ifname_cb(void *ctx)
  417. {
  418. struct hostapd_data *hapd = ctx;
  419. if (hapd && hapd->iconf && hapd->iconf->bss)
  420. return hapd->iconf->bss->iface;
  421. return NULL;
  422. }
  423. static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
  424. const char *path)
  425. {
  426. char *pos;
  427. os_free(interfaces->global_iface_path);
  428. interfaces->global_iface_path = os_strdup(path);
  429. if (interfaces->global_iface_path == NULL)
  430. return -1;
  431. pos = os_strrchr(interfaces->global_iface_path, '/');
  432. if (pos == NULL) {
  433. os_free(interfaces->global_iface_path);
  434. interfaces->global_iface_path = NULL;
  435. return -1;
  436. }
  437. *pos = '\0';
  438. interfaces->global_iface_name = pos + 1;
  439. return 0;
  440. }
  441. int main(int argc, char *argv[])
  442. {
  443. struct hapd_interfaces interfaces;
  444. int ret = 1;
  445. size_t i;
  446. int c, debug = 0, daemonize = 0;
  447. char *pid_file = NULL;
  448. const char *log_file = NULL;
  449. const char *entropy_file = NULL;
  450. if (os_program_init())
  451. return -1;
  452. os_memset(&interfaces, 0, sizeof(interfaces));
  453. interfaces.reload_config = hostapd_reload_config;
  454. interfaces.config_read_cb = hostapd_config_read;
  455. interfaces.for_each_interface = hostapd_for_each_interface;
  456. interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
  457. interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
  458. interfaces.driver_init = hostapd_driver_init;
  459. interfaces.global_iface_path = NULL;
  460. interfaces.global_iface_name = NULL;
  461. interfaces.global_ctrl_sock = -1;
  462. for (;;) {
  463. c = getopt(argc, argv, "Bde:f:hKP:tvg:");
  464. if (c < 0)
  465. break;
  466. switch (c) {
  467. case 'h':
  468. usage();
  469. break;
  470. case 'd':
  471. debug++;
  472. if (wpa_debug_level > 0)
  473. wpa_debug_level--;
  474. break;
  475. case 'B':
  476. daemonize++;
  477. break;
  478. case 'e':
  479. entropy_file = optarg;
  480. break;
  481. case 'f':
  482. log_file = optarg;
  483. break;
  484. case 'K':
  485. wpa_debug_show_keys++;
  486. break;
  487. case 'P':
  488. os_free(pid_file);
  489. pid_file = os_rel2abs_path(optarg);
  490. break;
  491. case 't':
  492. wpa_debug_timestamp++;
  493. break;
  494. case 'v':
  495. show_version();
  496. exit(1);
  497. break;
  498. case 'g':
  499. hostapd_get_global_ctrl_iface(&interfaces, optarg);
  500. break;
  501. default:
  502. usage();
  503. break;
  504. }
  505. }
  506. if (optind == argc && interfaces.global_iface_path == NULL)
  507. usage();
  508. wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
  509. if (log_file)
  510. wpa_debug_open_file(log_file);
  511. interfaces.count = argc - optind;
  512. if (interfaces.count) {
  513. interfaces.iface = os_calloc(interfaces.count,
  514. sizeof(struct hostapd_iface *));
  515. if (interfaces.iface == NULL) {
  516. wpa_printf(MSG_ERROR, "malloc failed");
  517. return -1;
  518. }
  519. }
  520. if (hostapd_global_init(&interfaces, entropy_file))
  521. return -1;
  522. /* Initialize interfaces */
  523. for (i = 0; i < interfaces.count; i++) {
  524. interfaces.iface[i] = hostapd_interface_init(&interfaces,
  525. argv[optind + i],
  526. debug);
  527. if (!interfaces.iface[i])
  528. goto out;
  529. }
  530. hostapd_global_ctrl_iface_init(&interfaces);
  531. if (hostapd_global_run(&interfaces, daemonize, pid_file))
  532. goto out;
  533. ret = 0;
  534. out:
  535. hostapd_global_ctrl_iface_deinit(&interfaces);
  536. /* Deinitialize all interfaces */
  537. for (i = 0; i < interfaces.count; i++)
  538. hostapd_interface_deinit_free(interfaces.iface[i]);
  539. os_free(interfaces.iface);
  540. hostapd_global_deinit(pid_file);
  541. os_free(pid_file);
  542. if (log_file)
  543. wpa_debug_close_file();
  544. os_program_deinit();
  545. return ret;
  546. }