main.c 18 KB

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