main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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 "crypto/random.h"
  16. #include "crypto/tls.h"
  17. #include "common/version.h"
  18. #include "drivers/driver.h"
  19. #include "eap_server/eap.h"
  20. #include "eap_server/tncs.h"
  21. #include "ap/hostapd.h"
  22. #include "ap/ap_config.h"
  23. #include "ap/ap_drv_ops.h"
  24. #include "config_file.h"
  25. #include "eap_register.h"
  26. #include "dump_state.h"
  27. #include "ctrl_iface.h"
  28. extern int wpa_debug_level;
  29. extern int wpa_debug_show_keys;
  30. extern int wpa_debug_timestamp;
  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. 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.ssid = hapd->conf->ssid.ssid;
  168. params.ssid_len = hapd->conf->ssid.ssid_len;
  169. params.test_socket = hapd->conf->test_socket;
  170. params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
  171. params.num_bridge = hapd->iface->num_bss;
  172. params.bridge = os_calloc(hapd->iface->num_bss, sizeof(char *));
  173. if (params.bridge == NULL)
  174. return -1;
  175. for (i = 0; i < hapd->iface->num_bss; i++) {
  176. struct hostapd_data *bss = hapd->iface->bss[i];
  177. if (bss->conf->bridge[0])
  178. params.bridge[i] = bss->conf->bridge;
  179. }
  180. params.own_addr = hapd->own_addr;
  181. hapd->drv_priv = hapd->driver->hapd_init(hapd, &params);
  182. os_free(params.bridge);
  183. if (hapd->drv_priv == NULL) {
  184. wpa_printf(MSG_ERROR, "%s driver initialization failed.",
  185. hapd->driver->name);
  186. hapd->driver = NULL;
  187. return -1;
  188. }
  189. if (hapd->driver->get_capa &&
  190. hapd->driver->get_capa(hapd->drv_priv, &capa) == 0) {
  191. iface->drv_flags = capa.flags;
  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. }
  198. return 0;
  199. }
  200. /**
  201. * hostapd_interface_init - Read configuration file and init BSS data
  202. *
  203. * This function is used to parse configuration file for a full interface (one
  204. * or more BSSes sharing the same radio) and allocate memory for the BSS
  205. * interfaces. No actiual driver operations are started.
  206. */
  207. static struct hostapd_iface *
  208. hostapd_interface_init(struct hapd_interfaces *interfaces,
  209. const char *config_fname, int debug)
  210. {
  211. struct hostapd_iface *iface;
  212. int k;
  213. wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
  214. iface = hostapd_init(interfaces, config_fname);
  215. if (!iface)
  216. return NULL;
  217. iface->interfaces = interfaces;
  218. for (k = 0; k < debug; k++) {
  219. if (iface->bss[0]->conf->logger_stdout_level > 0)
  220. iface->bss[0]->conf->logger_stdout_level--;
  221. }
  222. if (iface->conf->bss[0]->iface[0] == '\0' &&
  223. !hostapd_drv_none(iface->bss[0])) {
  224. wpa_printf(MSG_ERROR, "Interface name not specified in %s",
  225. config_fname);
  226. hostapd_interface_deinit_free(iface);
  227. return NULL;
  228. }
  229. return iface;
  230. }
  231. /**
  232. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  233. */
  234. static void handle_term(int sig, void *signal_ctx)
  235. {
  236. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  237. eloop_terminate();
  238. }
  239. #ifndef CONFIG_NATIVE_WINDOWS
  240. static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
  241. {
  242. if (hostapd_reload_config(iface) < 0) {
  243. wpa_printf(MSG_WARNING, "Failed to read new configuration "
  244. "file - continuing with old.");
  245. }
  246. return 0;
  247. }
  248. /**
  249. * handle_reload - SIGHUP handler to reload configuration
  250. */
  251. static void handle_reload(int sig, void *signal_ctx)
  252. {
  253. struct hapd_interfaces *interfaces = signal_ctx;
  254. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  255. sig);
  256. hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
  257. }
  258. static void handle_dump_state(int sig, void *signal_ctx)
  259. {
  260. #ifdef HOSTAPD_DUMP_STATE
  261. struct hapd_interfaces *interfaces = signal_ctx;
  262. hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
  263. #endif /* HOSTAPD_DUMP_STATE */
  264. }
  265. #endif /* CONFIG_NATIVE_WINDOWS */
  266. static int hostapd_global_init(struct hapd_interfaces *interfaces,
  267. const char *entropy_file)
  268. {
  269. int i;
  270. os_memset(&global, 0, sizeof(global));
  271. hostapd_logger_register_cb(hostapd_logger_cb);
  272. if (eap_server_register_methods()) {
  273. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  274. return -1;
  275. }
  276. if (eloop_init()) {
  277. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  278. return -1;
  279. }
  280. random_init(entropy_file);
  281. #ifndef CONFIG_NATIVE_WINDOWS
  282. eloop_register_signal(SIGHUP, handle_reload, interfaces);
  283. eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
  284. #endif /* CONFIG_NATIVE_WINDOWS */
  285. eloop_register_signal_terminate(handle_term, interfaces);
  286. #ifndef CONFIG_NATIVE_WINDOWS
  287. openlog("hostapd", 0, LOG_DAEMON);
  288. #endif /* CONFIG_NATIVE_WINDOWS */
  289. for (i = 0; wpa_drivers[i]; i++)
  290. global.drv_count++;
  291. if (global.drv_count == 0) {
  292. wpa_printf(MSG_ERROR, "No drivers enabled");
  293. return -1;
  294. }
  295. global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
  296. if (global.drv_priv == NULL)
  297. return -1;
  298. return 0;
  299. }
  300. static void hostapd_global_deinit(const char *pid_file)
  301. {
  302. int i;
  303. for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
  304. if (!global.drv_priv[i])
  305. continue;
  306. wpa_drivers[i]->global_deinit(global.drv_priv[i]);
  307. }
  308. os_free(global.drv_priv);
  309. global.drv_priv = NULL;
  310. #ifdef EAP_SERVER_TNC
  311. tncs_global_deinit();
  312. #endif /* EAP_SERVER_TNC */
  313. random_deinit();
  314. eloop_destroy();
  315. #ifndef CONFIG_NATIVE_WINDOWS
  316. closelog();
  317. #endif /* CONFIG_NATIVE_WINDOWS */
  318. eap_server_unregister_methods();
  319. os_daemonize_terminate(pid_file);
  320. }
  321. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  322. const char *pid_file)
  323. {
  324. #ifdef EAP_SERVER_TNC
  325. int tnc = 0;
  326. size_t i, k;
  327. for (i = 0; !tnc && i < ifaces->count; i++) {
  328. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  329. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  330. tnc++;
  331. break;
  332. }
  333. }
  334. }
  335. if (tnc && tncs_global_init() < 0) {
  336. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  337. return -1;
  338. }
  339. #endif /* EAP_SERVER_TNC */
  340. if (daemonize && os_daemonize(pid_file)) {
  341. perror("daemon");
  342. return -1;
  343. }
  344. eloop_run();
  345. return 0;
  346. }
  347. static void show_version(void)
  348. {
  349. fprintf(stderr,
  350. "hostapd v" VERSION_STR "\n"
  351. "User space daemon for IEEE 802.11 AP management,\n"
  352. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  353. "Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> "
  354. "and contributors\n");
  355. }
  356. static void usage(void)
  357. {
  358. show_version();
  359. fprintf(stderr,
  360. "\n"
  361. "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
  362. "\\\n"
  363. " [-g <global ctrl_iface>] [-G <group>] \\\n"
  364. " <configuration file(s)>\n"
  365. "\n"
  366. "options:\n"
  367. " -h show this usage\n"
  368. " -d show more debug messages (-dd for even more)\n"
  369. " -B run daemon in the background\n"
  370. " -e entropy file\n"
  371. " -g global control interface path\n"
  372. " -G group for control interfaces\n"
  373. " -P PID file\n"
  374. " -K include key data in debug messages\n"
  375. #ifdef CONFIG_DEBUG_FILE
  376. " -f log output to debug file instead of stdout\n"
  377. #endif /* CONFIG_DEBUG_FILE */
  378. #ifdef CONFIG_DEBUG_LINUX_TRACING
  379. " -T = record to Linux tracing in addition to logging\n"
  380. " (records all messages regardless of debug verbosity)\n"
  381. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  382. " -t include timestamps in some debug messages\n"
  383. " -v show hostapd version\n");
  384. exit(1);
  385. }
  386. static const char * hostapd_msg_ifname_cb(void *ctx)
  387. {
  388. struct hostapd_data *hapd = ctx;
  389. if (hapd && hapd->iconf && hapd->iconf->bss &&
  390. hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
  391. return hapd->iconf->bss[0]->iface;
  392. return NULL;
  393. }
  394. static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
  395. const char *path)
  396. {
  397. char *pos;
  398. os_free(interfaces->global_iface_path);
  399. interfaces->global_iface_path = os_strdup(path);
  400. if (interfaces->global_iface_path == NULL)
  401. return -1;
  402. pos = os_strrchr(interfaces->global_iface_path, '/');
  403. if (pos == NULL) {
  404. wpa_printf(MSG_ERROR, "No '/' in the global control interface "
  405. "file");
  406. os_free(interfaces->global_iface_path);
  407. interfaces->global_iface_path = NULL;
  408. return -1;
  409. }
  410. *pos = '\0';
  411. interfaces->global_iface_name = pos + 1;
  412. return 0;
  413. }
  414. static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
  415. const char *group)
  416. {
  417. #ifndef CONFIG_NATIVE_WINDOWS
  418. struct group *grp;
  419. grp = getgrnam(group);
  420. if (grp == NULL) {
  421. wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
  422. return -1;
  423. }
  424. interfaces->ctrl_iface_group = grp->gr_gid;
  425. #endif /* CONFIG_NATIVE_WINDOWS */
  426. return 0;
  427. }
  428. int main(int argc, char *argv[])
  429. {
  430. struct hapd_interfaces interfaces;
  431. int ret = 1;
  432. size_t i, j;
  433. int c, debug = 0, daemonize = 0;
  434. char *pid_file = NULL;
  435. const char *log_file = NULL;
  436. const char *entropy_file = NULL;
  437. char **bss_config = NULL, **tmp_bss;
  438. size_t num_bss_configs = 0;
  439. #ifdef CONFIG_DEBUG_LINUX_TRACING
  440. int enable_trace_dbg = 0;
  441. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  442. if (os_program_init())
  443. return -1;
  444. os_memset(&interfaces, 0, sizeof(interfaces));
  445. interfaces.reload_config = hostapd_reload_config;
  446. interfaces.config_read_cb = hostapd_config_read;
  447. interfaces.for_each_interface = hostapd_for_each_interface;
  448. interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
  449. interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
  450. interfaces.driver_init = hostapd_driver_init;
  451. interfaces.global_iface_path = NULL;
  452. interfaces.global_iface_name = NULL;
  453. interfaces.global_ctrl_sock = -1;
  454. for (;;) {
  455. c = getopt(argc, argv, "b:Bde:f:hKP:Ttvg:G:");
  456. if (c < 0)
  457. break;
  458. switch (c) {
  459. case 'h':
  460. usage();
  461. break;
  462. case 'd':
  463. debug++;
  464. if (wpa_debug_level > 0)
  465. wpa_debug_level--;
  466. break;
  467. case 'B':
  468. daemonize++;
  469. break;
  470. case 'e':
  471. entropy_file = optarg;
  472. break;
  473. case 'f':
  474. log_file = optarg;
  475. break;
  476. case 'K':
  477. wpa_debug_show_keys++;
  478. break;
  479. case 'P':
  480. os_free(pid_file);
  481. pid_file = os_rel2abs_path(optarg);
  482. break;
  483. case 't':
  484. wpa_debug_timestamp++;
  485. break;
  486. #ifdef CONFIG_DEBUG_LINUX_TRACING
  487. case 'T':
  488. enable_trace_dbg = 1;
  489. break;
  490. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  491. case 'v':
  492. show_version();
  493. exit(1);
  494. break;
  495. case 'g':
  496. if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
  497. return -1;
  498. break;
  499. case 'G':
  500. if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
  501. return -1;
  502. break;
  503. case 'b':
  504. tmp_bss = os_realloc_array(bss_config,
  505. num_bss_configs + 1,
  506. sizeof(char *));
  507. if (tmp_bss == NULL)
  508. goto out;
  509. bss_config = tmp_bss;
  510. bss_config[num_bss_configs++] = optarg;
  511. break;
  512. default:
  513. usage();
  514. break;
  515. }
  516. }
  517. if (optind == argc && interfaces.global_iface_path == NULL &&
  518. num_bss_configs == 0)
  519. usage();
  520. wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
  521. if (log_file)
  522. wpa_debug_open_file(log_file);
  523. #ifdef CONFIG_DEBUG_LINUX_TRACING
  524. if (enable_trace_dbg) {
  525. int tret = wpa_debug_open_linux_tracing();
  526. if (tret) {
  527. wpa_printf(MSG_ERROR, "Failed to enable trace logging");
  528. return -1;
  529. }
  530. }
  531. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  532. interfaces.count = argc - optind;
  533. if (interfaces.count || num_bss_configs) {
  534. interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
  535. sizeof(struct hostapd_iface *));
  536. if (interfaces.iface == NULL) {
  537. wpa_printf(MSG_ERROR, "malloc failed");
  538. return -1;
  539. }
  540. }
  541. if (hostapd_global_init(&interfaces, entropy_file)) {
  542. wpa_printf(MSG_ERROR, "Failed to initilize global context");
  543. return -1;
  544. }
  545. /* Allocate and parse configuration for full interface files */
  546. for (i = 0; i < interfaces.count; i++) {
  547. interfaces.iface[i] = hostapd_interface_init(&interfaces,
  548. argv[optind + i],
  549. debug);
  550. if (!interfaces.iface[i]) {
  551. wpa_printf(MSG_ERROR, "Failed to initialize interface");
  552. goto out;
  553. }
  554. }
  555. /* Allocate and parse configuration for per-BSS files */
  556. for (i = 0; i < num_bss_configs; i++) {
  557. struct hostapd_iface *iface;
  558. char *fname;
  559. wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
  560. fname = os_strchr(bss_config[i], ':');
  561. if (fname == NULL) {
  562. wpa_printf(MSG_ERROR,
  563. "Invalid BSS config identifier '%s'",
  564. bss_config[i]);
  565. goto out;
  566. }
  567. *fname++ = '\0';
  568. iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
  569. fname, debug);
  570. if (iface == NULL)
  571. goto out;
  572. for (j = 0; j < interfaces.count; j++) {
  573. if (interfaces.iface[j] == iface)
  574. break;
  575. }
  576. if (j == interfaces.count) {
  577. struct hostapd_iface **tmp;
  578. tmp = os_realloc_array(interfaces.iface,
  579. interfaces.count + 1,
  580. sizeof(struct hostapd_iface *));
  581. if (tmp == NULL) {
  582. hostapd_interface_deinit_free(iface);
  583. goto out;
  584. }
  585. interfaces.iface = tmp;
  586. interfaces.iface[interfaces.count++] = iface;
  587. }
  588. }
  589. /*
  590. * Enable configured interfaces. Depending on channel configuration,
  591. * this may complete full initialization before returning or use a
  592. * callback mechanism to complete setup in case of operations like HT
  593. * co-ex scans, ACS, or DFS are needed to determine channel parameters.
  594. * In such case, the interface will be enabled from eloop context within
  595. * hostapd_global_run().
  596. */
  597. interfaces.terminate_on_error = interfaces.count;
  598. for (i = 0; i < interfaces.count; i++) {
  599. if (hostapd_driver_init(interfaces.iface[i]) ||
  600. hostapd_setup_interface(interfaces.iface[i]))
  601. goto out;
  602. }
  603. hostapd_global_ctrl_iface_init(&interfaces);
  604. if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
  605. wpa_printf(MSG_ERROR, "Failed to start eloop");
  606. goto out;
  607. }
  608. ret = 0;
  609. out:
  610. hostapd_global_ctrl_iface_deinit(&interfaces);
  611. /* Deinitialize all interfaces */
  612. for (i = 0; i < interfaces.count; i++)
  613. hostapd_interface_deinit_free(interfaces.iface[i]);
  614. os_free(interfaces.iface);
  615. hostapd_global_deinit(pid_file);
  616. os_free(pid_file);
  617. if (log_file)
  618. wpa_debug_close_file();
  619. wpa_debug_close_linux_tracing();
  620. os_free(bss_config);
  621. os_program_deinit();
  622. return ret;
  623. }