main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * hostapd / main()
  3. * Copyright (c) 2002-2015, 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. #include <grp.h>
  12. #endif /* CONFIG_NATIVE_WINDOWS */
  13. #include "utils/common.h"
  14. #include "utils/eloop.h"
  15. #include "utils/uuid.h"
  16. #include "crypto/random.h"
  17. #include "crypto/tls.h"
  18. #include "common/version.h"
  19. #include "drivers/driver.h"
  20. #include "eap_server/eap.h"
  21. #include "eap_server/tncs.h"
  22. #include "ap/hostapd.h"
  23. #include "ap/ap_config.h"
  24. #include "ap/ap_drv_ops.h"
  25. #include "fst/fst.h"
  26. #include "config_file.h"
  27. #include "eap_register.h"
  28. #include "ctrl_iface.h"
  29. struct hapd_global {
  30. void **drv_priv;
  31. size_t drv_count;
  32. };
  33. static struct hapd_global global;
  34. #ifndef CONFIG_NO_HOSTAPD_LOGGER
  35. static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
  36. int level, const char *txt, size_t len)
  37. {
  38. struct hostapd_data *hapd = ctx;
  39. char *format, *module_str;
  40. int maxlen;
  41. int conf_syslog_level, conf_stdout_level;
  42. unsigned int conf_syslog, conf_stdout;
  43. maxlen = len + 100;
  44. format = os_malloc(maxlen);
  45. if (!format)
  46. return;
  47. if (hapd && hapd->conf) {
  48. conf_syslog_level = hapd->conf->logger_syslog_level;
  49. conf_stdout_level = hapd->conf->logger_stdout_level;
  50. conf_syslog = hapd->conf->logger_syslog;
  51. conf_stdout = hapd->conf->logger_stdout;
  52. } else {
  53. conf_syslog_level = conf_stdout_level = 0;
  54. conf_syslog = conf_stdout = (unsigned int) -1;
  55. }
  56. switch (module) {
  57. case HOSTAPD_MODULE_IEEE80211:
  58. module_str = "IEEE 802.11";
  59. break;
  60. case HOSTAPD_MODULE_IEEE8021X:
  61. module_str = "IEEE 802.1X";
  62. break;
  63. case HOSTAPD_MODULE_RADIUS:
  64. module_str = "RADIUS";
  65. break;
  66. case HOSTAPD_MODULE_WPA:
  67. module_str = "WPA";
  68. break;
  69. case HOSTAPD_MODULE_DRIVER:
  70. module_str = "DRIVER";
  71. break;
  72. case HOSTAPD_MODULE_IAPP:
  73. module_str = "IAPP";
  74. break;
  75. case HOSTAPD_MODULE_MLME:
  76. module_str = "MLME";
  77. break;
  78. default:
  79. module_str = NULL;
  80. break;
  81. }
  82. if (hapd && hapd->conf && addr)
  83. os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
  84. hapd->conf->iface, MAC2STR(addr),
  85. module_str ? " " : "", module_str ? module_str : "",
  86. txt);
  87. else if (hapd && hapd->conf)
  88. os_snprintf(format, maxlen, "%s:%s%s %s",
  89. hapd->conf->iface, module_str ? " " : "",
  90. module_str ? module_str : "", txt);
  91. else if (addr)
  92. os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
  93. MAC2STR(addr), module_str ? " " : "",
  94. module_str ? module_str : "", txt);
  95. else
  96. os_snprintf(format, maxlen, "%s%s%s",
  97. module_str ? module_str : "",
  98. module_str ? ": " : "", txt);
  99. if ((conf_stdout & module) && level >= conf_stdout_level) {
  100. wpa_debug_print_timestamp();
  101. wpa_printf(MSG_INFO, "%s", 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_driver_init - Preparate driver interface
  132. */
  133. static int hostapd_driver_init(struct hostapd_iface *iface)
  134. {
  135. struct wpa_init_params params;
  136. size_t i;
  137. struct hostapd_data *hapd = iface->bss[0];
  138. struct hostapd_bss_config *conf = hapd->conf;
  139. u8 *b = conf->bssid;
  140. struct wpa_driver_capa capa;
  141. if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
  142. wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
  143. return -1;
  144. }
  145. /* Initialize the driver interface */
  146. if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
  147. b = NULL;
  148. os_memset(&params, 0, sizeof(params));
  149. for (i = 0; wpa_drivers[i]; i++) {
  150. if (wpa_drivers[i] != hapd->driver)
  151. continue;
  152. if (global.drv_priv[i] == NULL &&
  153. wpa_drivers[i]->global_init) {
  154. global.drv_priv[i] = wpa_drivers[i]->global_init();
  155. if (global.drv_priv[i] == NULL) {
  156. wpa_printf(MSG_ERROR, "Failed to initialize "
  157. "driver '%s'",
  158. wpa_drivers[i]->name);
  159. return -1;
  160. }
  161. }
  162. params.global_priv = global.drv_priv[i];
  163. break;
  164. }
  165. params.bssid = b;
  166. params.ifname = hapd->conf->iface;
  167. params.driver_params = hapd->iconf->driver_params;
  168. params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
  169. params.num_bridge = hapd->iface->num_bss;
  170. params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
  171. if (params.bridge == NULL)
  172. return -1;
  173. for (i = 0; i < hapd->iface->num_bss; i++) {
  174. struct hostapd_data *bss = hapd->iface->bss[i];
  175. if (bss->conf->bridge[0])
  176. params.bridge[i] = bss->conf->bridge;
  177. }
  178. params.own_addr = hapd->own_addr;
  179. hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
  180. os_free(params.bridge);
  181. if (hapd->drv_priv == NULL) {
  182. wpa_printf(MSG_ERROR, "%s driver initialization failed.",
  183. hapd->driver->name);
  184. hapd->driver = NULL;
  185. return -1;
  186. }
  187. if (hapd->driver->get_capa &&
  188. hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
  189. struct wowlan_triggers *triggs;
  190. iface->drv_flags = capa.flags;
  191. iface->smps_modes = capa.smps_modes;
  192. iface->probe_resp_offloads = capa.probe_resp_offloads;
  193. iface->extended_capa = capa.extended_capa;
  194. iface->extended_capa_mask = capa.extended_capa_mask;
  195. iface->extended_capa_len = capa.extended_capa_len;
  196. iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
  197. triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
  198. if (triggs && hapd->driver->set_wowlan) {
  199. if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
  200. wpa_printf(MSG_ERROR, "set_wowlan failed");
  201. }
  202. os_free(triggs);
  203. }
  204. return 0;
  205. }
  206. /**
  207. * hostapd_interface_init - Read configuration file and init BSS data
  208. *
  209. * This function is used to parse configuration file for a full interface (one
  210. * or more BSSes sharing the same radio) and allocate memory for the BSS
  211. * interfaces. No actiual driver operations are started.
  212. */
  213. static struct hostapd_iface *
  214. hostapd_interface_init(struct hapd_interfaces *interfaces,
  215. const char *config_fname, int debug)
  216. {
  217. struct hostapd_iface *iface;
  218. int k;
  219. wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
  220. iface = hostapd_init(interfaces, config_fname);
  221. if (!iface)
  222. return NULL;
  223. iface->interfaces = interfaces;
  224. for (k = 0; k < debug; k++) {
  225. if (iface->bss[0]->conf->logger_stdout_level > 0)
  226. iface->bss[0]->conf->logger_stdout_level--;
  227. }
  228. if (iface->conf->bss[0]->iface[0] == '\0' &&
  229. !hostapd_drv_none(iface->bss[0])) {
  230. wpa_printf(MSG_ERROR, "Interface name not specified in %s",
  231. config_fname);
  232. hostapd_interface_deinit_free(iface);
  233. return NULL;
  234. }
  235. return iface;
  236. }
  237. /**
  238. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  239. */
  240. static void handle_term(int sig, void *signal_ctx)
  241. {
  242. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  243. eloop_terminate();
  244. }
  245. #ifndef CONFIG_NATIVE_WINDOWS
  246. static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
  247. {
  248. if (hostapd_reload_config(iface) < 0) {
  249. wpa_printf(MSG_WARNING, "Failed to read new configuration "
  250. "file - continuing with old.");
  251. }
  252. return 0;
  253. }
  254. /**
  255. * handle_reload - SIGHUP handler to reload configuration
  256. */
  257. static void handle_reload(int sig, void *signal_ctx)
  258. {
  259. struct hapd_interfaces *interfaces = signal_ctx;
  260. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  261. sig);
  262. hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
  263. }
  264. static void handle_dump_state(int sig, void *signal_ctx)
  265. {
  266. /* Not used anymore - ignore signal */
  267. }
  268. #endif /* CONFIG_NATIVE_WINDOWS */
  269. static int hostapd_global_init(struct hapd_interfaces *interfaces,
  270. const char *entropy_file)
  271. {
  272. int i;
  273. os_memset(&global, 0, sizeof(global));
  274. hostapd_logger_register_cb(hostapd_logger_cb);
  275. if (eap_server_register_methods()) {
  276. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  277. return -1;
  278. }
  279. if (eloop_init()) {
  280. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  281. return -1;
  282. }
  283. random_init(entropy_file);
  284. #ifndef CONFIG_NATIVE_WINDOWS
  285. eloop_register_signal(SIGHUP, handle_reload, interfaces);
  286. eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
  287. #endif /* CONFIG_NATIVE_WINDOWS */
  288. eloop_register_signal_terminate(handle_term, interfaces);
  289. #ifndef CONFIG_NATIVE_WINDOWS
  290. openlog("hostapd", 0, LOG_DAEMON);
  291. #endif /* CONFIG_NATIVE_WINDOWS */
  292. for (i = 0; wpa_drivers[i]; i++)
  293. global.drv_count++;
  294. if (global.drv_count == 0) {
  295. wpa_printf(MSG_ERROR, "No drivers enabled");
  296. return -1;
  297. }
  298. global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
  299. if (global.drv_priv == NULL)
  300. return -1;
  301. return 0;
  302. }
  303. static void hostapd_global_deinit(const char *pid_file)
  304. {
  305. int i;
  306. for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
  307. if (!global.drv_priv[i])
  308. continue;
  309. wpa_drivers[i]->global_deinit(global.drv_priv[i]);
  310. }
  311. os_free(global.drv_priv);
  312. global.drv_priv = NULL;
  313. #ifdef EAP_SERVER_TNC
  314. tncs_global_deinit();
  315. #endif /* EAP_SERVER_TNC */
  316. random_deinit();
  317. eloop_destroy();
  318. #ifndef CONFIG_NATIVE_WINDOWS
  319. closelog();
  320. #endif /* CONFIG_NATIVE_WINDOWS */
  321. eap_server_unregister_methods();
  322. os_daemonize_terminate(pid_file);
  323. }
  324. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  325. const char *pid_file)
  326. {
  327. #ifdef EAP_SERVER_TNC
  328. int tnc = 0;
  329. size_t i, k;
  330. for (i = 0; !tnc && i < ifaces->count; i++) {
  331. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  332. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  333. tnc++;
  334. break;
  335. }
  336. }
  337. }
  338. if (tnc && tncs_global_init() < 0) {
  339. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  340. return -1;
  341. }
  342. #endif /* EAP_SERVER_TNC */
  343. if (daemonize && os_daemonize(pid_file)) {
  344. wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
  345. return -1;
  346. }
  347. eloop_run();
  348. return 0;
  349. }
  350. static void show_version(void)
  351. {
  352. fprintf(stderr,
  353. "hostapd v" VERSION_STR "\n"
  354. "User space daemon for IEEE 802.11 AP management,\n"
  355. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  356. "Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi> "
  357. "and contributors\n");
  358. }
  359. static void usage(void)
  360. {
  361. show_version();
  362. fprintf(stderr,
  363. "\n"
  364. "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
  365. "\\\n"
  366. " [-g <global ctrl_iface>] [-G <group>] \\\n"
  367. " <configuration file(s)>\n"
  368. "\n"
  369. "options:\n"
  370. " -h show this usage\n"
  371. " -d show more debug messages (-dd for even more)\n"
  372. " -B run daemon in the background\n"
  373. " -e entropy file\n"
  374. " -g global control interface path\n"
  375. " -G group for control interfaces\n"
  376. " -P PID file\n"
  377. " -K include key data in debug messages\n"
  378. #ifdef CONFIG_DEBUG_FILE
  379. " -f log output to debug file instead of stdout\n"
  380. #endif /* CONFIG_DEBUG_FILE */
  381. #ifdef CONFIG_DEBUG_LINUX_TRACING
  382. " -T = record to Linux tracing in addition to logging\n"
  383. " (records all messages regardless of debug verbosity)\n"
  384. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  385. " -t include timestamps in some debug messages\n"
  386. " -v show hostapd version\n");
  387. exit(1);
  388. }
  389. static const char * hostapd_msg_ifname_cb(void *ctx)
  390. {
  391. struct hostapd_data *hapd = ctx;
  392. if (hapd && hapd->iconf && hapd->iconf->bss &&
  393. hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
  394. return hapd->iconf->bss[0]->iface;
  395. return NULL;
  396. }
  397. static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
  398. const char *path)
  399. {
  400. char *pos;
  401. os_free(interfaces->global_iface_path);
  402. interfaces->global_iface_path = os_strdup(path);
  403. if (interfaces->global_iface_path == NULL)
  404. return -1;
  405. pos = os_strrchr(interfaces->global_iface_path, '/');
  406. if (pos == NULL) {
  407. wpa_printf(MSG_ERROR, "No '/' in the global control interface "
  408. "file");
  409. os_free(interfaces->global_iface_path);
  410. interfaces->global_iface_path = NULL;
  411. return -1;
  412. }
  413. *pos = '\0';
  414. interfaces->global_iface_name = pos + 1;
  415. return 0;
  416. }
  417. static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
  418. const char *group)
  419. {
  420. #ifndef CONFIG_NATIVE_WINDOWS
  421. struct group *grp;
  422. grp = getgrnam(group);
  423. if (grp == NULL) {
  424. wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
  425. return -1;
  426. }
  427. interfaces->ctrl_iface_group = grp->gr_gid;
  428. #endif /* CONFIG_NATIVE_WINDOWS */
  429. return 0;
  430. }
  431. #ifdef CONFIG_WPS
  432. static int gen_uuid(const char *txt_addr)
  433. {
  434. u8 addr[ETH_ALEN];
  435. u8 uuid[UUID_LEN];
  436. char buf[100];
  437. if (hwaddr_aton(txt_addr, addr) < 0)
  438. return -1;
  439. uuid_gen_mac_addr(addr, uuid);
  440. if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
  441. return -1;
  442. printf("%s\n", buf);
  443. return 0;
  444. }
  445. #endif /* CONFIG_WPS */
  446. #ifndef HOSTAPD_CLEANUP_INTERVAL
  447. #define HOSTAPD_CLEANUP_INTERVAL 10
  448. #endif /* HOSTAPD_CLEANUP_INTERVAL */
  449. static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
  450. {
  451. hostapd_periodic_iface(iface);
  452. return 0;
  453. }
  454. /* Periodic cleanup tasks */
  455. static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
  456. {
  457. struct hapd_interfaces *interfaces = eloop_ctx;
  458. eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
  459. hostapd_periodic, interfaces, NULL);
  460. hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
  461. }
  462. int main(int argc, char *argv[])
  463. {
  464. struct hapd_interfaces interfaces;
  465. int ret = 1;
  466. size_t i, j;
  467. int c, debug = 0, daemonize = 0;
  468. char *pid_file = NULL;
  469. const char *log_file = NULL;
  470. const char *entropy_file = NULL;
  471. char **bss_config = NULL, **tmp_bss;
  472. size_t num_bss_configs = 0;
  473. #ifdef CONFIG_DEBUG_LINUX_TRACING
  474. int enable_trace_dbg = 0;
  475. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  476. if (os_program_init())
  477. return -1;
  478. os_memset(&interfaces, 0, sizeof(interfaces));
  479. interfaces.reload_config = hostapd_reload_config;
  480. interfaces.config_read_cb = hostapd_config_read;
  481. interfaces.for_each_interface = hostapd_for_each_interface;
  482. interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
  483. interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
  484. interfaces.driver_init = hostapd_driver_init;
  485. interfaces.global_iface_path = NULL;
  486. interfaces.global_iface_name = NULL;
  487. interfaces.global_ctrl_sock = -1;
  488. interfaces.global_ctrl_dst = NULL;
  489. for (;;) {
  490. c = getopt(argc, argv, "b:Bde:f:hKP:Ttu:vg:G:");
  491. if (c < 0)
  492. break;
  493. switch (c) {
  494. case 'h':
  495. usage();
  496. break;
  497. case 'd':
  498. debug++;
  499. if (wpa_debug_level > 0)
  500. wpa_debug_level--;
  501. break;
  502. case 'B':
  503. daemonize++;
  504. break;
  505. case 'e':
  506. entropy_file = optarg;
  507. break;
  508. case 'f':
  509. log_file = optarg;
  510. break;
  511. case 'K':
  512. wpa_debug_show_keys++;
  513. break;
  514. case 'P':
  515. os_free(pid_file);
  516. pid_file = os_rel2abs_path(optarg);
  517. break;
  518. case 't':
  519. wpa_debug_timestamp++;
  520. break;
  521. #ifdef CONFIG_DEBUG_LINUX_TRACING
  522. case 'T':
  523. enable_trace_dbg = 1;
  524. break;
  525. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  526. case 'v':
  527. show_version();
  528. exit(1);
  529. break;
  530. case 'g':
  531. if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
  532. return -1;
  533. break;
  534. case 'G':
  535. if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
  536. return -1;
  537. break;
  538. case 'b':
  539. tmp_bss = os_realloc_array(bss_config,
  540. num_bss_configs + 1,
  541. sizeof(char *));
  542. if (tmp_bss == NULL)
  543. goto out;
  544. bss_config = tmp_bss;
  545. bss_config[num_bss_configs++] = optarg;
  546. break;
  547. #ifdef CONFIG_WPS
  548. case 'u':
  549. return gen_uuid(optarg);
  550. #endif /* CONFIG_WPS */
  551. default:
  552. usage();
  553. break;
  554. }
  555. }
  556. if (optind == argc && interfaces.global_iface_path == NULL &&
  557. num_bss_configs == 0)
  558. usage();
  559. wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
  560. if (log_file)
  561. wpa_debug_open_file(log_file);
  562. else
  563. wpa_debug_setup_stdout();
  564. #ifdef CONFIG_DEBUG_LINUX_TRACING
  565. if (enable_trace_dbg) {
  566. int tret = wpa_debug_open_linux_tracing();
  567. if (tret) {
  568. wpa_printf(MSG_ERROR, "Failed to enable trace logging");
  569. return -1;
  570. }
  571. }
  572. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  573. interfaces.count = argc - optind;
  574. if (interfaces.count || num_bss_configs) {
  575. interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
  576. sizeof(struct hostapd_iface *));
  577. if (interfaces.iface == NULL) {
  578. wpa_printf(MSG_ERROR, "malloc failed");
  579. return -1;
  580. }
  581. }
  582. if (hostapd_global_init(&interfaces, entropy_file)) {
  583. wpa_printf(MSG_ERROR, "Failed to initialize global context");
  584. return -1;
  585. }
  586. eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
  587. hostapd_periodic, &interfaces, NULL);
  588. if (fst_global_init()) {
  589. wpa_printf(MSG_ERROR,
  590. "Failed to initialize global FST context");
  591. goto out;
  592. }
  593. #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
  594. if (!fst_global_add_ctrl(fst_ctrl_cli))
  595. wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
  596. #endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
  597. /* Allocate and parse configuration for full interface files */
  598. for (i = 0; i < interfaces.count; i++) {
  599. interfaces.iface[i] = hostapd_interface_init(&interfaces,
  600. argv[optind + i],
  601. debug);
  602. if (!interfaces.iface[i]) {
  603. wpa_printf(MSG_ERROR, "Failed to initialize interface");
  604. goto out;
  605. }
  606. }
  607. /* Allocate and parse configuration for per-BSS files */
  608. for (i = 0; i < num_bss_configs; i++) {
  609. struct hostapd_iface *iface;
  610. char *fname;
  611. wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
  612. fname = os_strchr(bss_config[i], ':');
  613. if (fname == NULL) {
  614. wpa_printf(MSG_ERROR,
  615. "Invalid BSS config identifier '%s'",
  616. bss_config[i]);
  617. goto out;
  618. }
  619. *fname++ = '\0';
  620. iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
  621. fname, debug);
  622. if (iface == NULL)
  623. goto out;
  624. for (j = 0; j < interfaces.count; j++) {
  625. if (interfaces.iface[j] == iface)
  626. break;
  627. }
  628. if (j == interfaces.count) {
  629. struct hostapd_iface **tmp;
  630. tmp = os_realloc_array(interfaces.iface,
  631. interfaces.count + 1,
  632. sizeof(struct hostapd_iface *));
  633. if (tmp == NULL) {
  634. hostapd_interface_deinit_free(iface);
  635. goto out;
  636. }
  637. interfaces.iface = tmp;
  638. interfaces.iface[interfaces.count++] = iface;
  639. }
  640. }
  641. /*
  642. * Enable configured interfaces. Depending on channel configuration,
  643. * this may complete full initialization before returning or use a
  644. * callback mechanism to complete setup in case of operations like HT
  645. * co-ex scans, ACS, or DFS are needed to determine channel parameters.
  646. * In such case, the interface will be enabled from eloop context within
  647. * hostapd_global_run().
  648. */
  649. interfaces.terminate_on_error = interfaces.count;
  650. for (i = 0; i < interfaces.count; i++) {
  651. if (hostapd_driver_init(interfaces.iface[i]) ||
  652. hostapd_setup_interface(interfaces.iface[i]))
  653. goto out;
  654. }
  655. hostapd_global_ctrl_iface_init(&interfaces);
  656. if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
  657. wpa_printf(MSG_ERROR, "Failed to start eloop");
  658. goto out;
  659. }
  660. ret = 0;
  661. out:
  662. hostapd_global_ctrl_iface_deinit(&interfaces);
  663. /* Deinitialize all interfaces */
  664. for (i = 0; i < interfaces.count; i++) {
  665. if (!interfaces.iface[i])
  666. continue;
  667. interfaces.iface[i]->driver_ap_teardown =
  668. !!(interfaces.iface[i]->drv_flags &
  669. WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
  670. hostapd_interface_deinit_free(interfaces.iface[i]);
  671. }
  672. os_free(interfaces.iface);
  673. eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
  674. hostapd_global_deinit(pid_file);
  675. os_free(pid_file);
  676. if (log_file)
  677. wpa_debug_close_file();
  678. wpa_debug_close_linux_tracing();
  679. os_free(bss_config);
  680. fst_global_deinit();
  681. os_program_deinit();
  682. return ret;
  683. }