main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. extern struct wpa_driver_ops *wpa_drivers[];
  32. struct hapd_global {
  33. void **drv_priv;
  34. size_t drv_count;
  35. };
  36. static struct hapd_global global;
  37. #ifndef CONFIG_NO_HOSTAPD_LOGGER
  38. static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
  39. int level, const char *txt, size_t len)
  40. {
  41. struct hostapd_data *hapd = ctx;
  42. char *format, *module_str;
  43. int maxlen;
  44. int conf_syslog_level, conf_stdout_level;
  45. unsigned int conf_syslog, conf_stdout;
  46. maxlen = len + 100;
  47. format = os_malloc(maxlen);
  48. if (!format)
  49. return;
  50. if (hapd && hapd->conf) {
  51. conf_syslog_level = hapd->conf->logger_syslog_level;
  52. conf_stdout_level = hapd->conf->logger_stdout_level;
  53. conf_syslog = hapd->conf->logger_syslog;
  54. conf_stdout = hapd->conf->logger_stdout;
  55. } else {
  56. conf_syslog_level = conf_stdout_level = 0;
  57. conf_syslog = conf_stdout = (unsigned int) -1;
  58. }
  59. switch (module) {
  60. case HOSTAPD_MODULE_IEEE80211:
  61. module_str = "IEEE 802.11";
  62. break;
  63. case HOSTAPD_MODULE_IEEE8021X:
  64. module_str = "IEEE 802.1X";
  65. break;
  66. case HOSTAPD_MODULE_RADIUS:
  67. module_str = "RADIUS";
  68. break;
  69. case HOSTAPD_MODULE_WPA:
  70. module_str = "WPA";
  71. break;
  72. case HOSTAPD_MODULE_DRIVER:
  73. module_str = "DRIVER";
  74. break;
  75. case HOSTAPD_MODULE_IAPP:
  76. module_str = "IAPP";
  77. break;
  78. case HOSTAPD_MODULE_MLME:
  79. module_str = "MLME";
  80. break;
  81. default:
  82. module_str = NULL;
  83. break;
  84. }
  85. if (hapd && hapd->conf && addr)
  86. os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
  87. hapd->conf->iface, MAC2STR(addr),
  88. module_str ? " " : "", module_str, txt);
  89. else if (hapd && hapd->conf)
  90. os_snprintf(format, maxlen, "%s:%s%s %s",
  91. hapd->conf->iface, module_str ? " " : "",
  92. module_str, txt);
  93. else if (addr)
  94. os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
  95. MAC2STR(addr), module_str ? " " : "",
  96. module_str, txt);
  97. else
  98. os_snprintf(format, maxlen, "%s%s%s",
  99. module_str, module_str ? ": " : "", txt);
  100. if ((conf_stdout & module) && level >= conf_stdout_level) {
  101. wpa_debug_print_timestamp();
  102. printf("%s\n", format);
  103. }
  104. #ifndef CONFIG_NATIVE_WINDOWS
  105. if ((conf_syslog & module) && level >= conf_syslog_level) {
  106. int priority;
  107. switch (level) {
  108. case HOSTAPD_LEVEL_DEBUG_VERBOSE:
  109. case HOSTAPD_LEVEL_DEBUG:
  110. priority = LOG_DEBUG;
  111. break;
  112. case HOSTAPD_LEVEL_INFO:
  113. priority = LOG_INFO;
  114. break;
  115. case HOSTAPD_LEVEL_NOTICE:
  116. priority = LOG_NOTICE;
  117. break;
  118. case HOSTAPD_LEVEL_WARNING:
  119. priority = LOG_WARNING;
  120. break;
  121. default:
  122. priority = LOG_INFO;
  123. break;
  124. }
  125. syslog(priority, "%s", format);
  126. }
  127. #endif /* CONFIG_NATIVE_WINDOWS */
  128. os_free(format);
  129. }
  130. #endif /* CONFIG_NO_HOSTAPD_LOGGER */
  131. static int hostapd_driver_init(struct hostapd_iface *iface)
  132. {
  133. struct wpa_init_params params;
  134. size_t i;
  135. struct hostapd_data *hapd = iface->bss[0];
  136. struct hostapd_bss_config *conf = hapd->conf;
  137. u8 *b = conf->bssid;
  138. struct wpa_driver_capa capa;
  139. if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
  140. wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
  141. return -1;
  142. }
  143. /* Initialize the driver interface */
  144. if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
  145. b = NULL;
  146. os_memset(&params, 0, sizeof(params));
  147. for (i = 0; wpa_drivers[i]; i++) {
  148. if (wpa_drivers[i] != hapd->driver)
  149. continue;
  150. if (global.drv_priv[i] == NULL &&
  151. wpa_drivers[i]->global_init) {
  152. global.drv_priv[i] = wpa_drivers[i]->global_init();
  153. if (global.drv_priv[i] == NULL) {
  154. wpa_printf(MSG_ERROR, "Failed to initialize "
  155. "driver '%s'",
  156. wpa_drivers[i]->name);
  157. return -1;
  158. }
  159. }
  160. params.global_priv = global.drv_priv[i];
  161. break;
  162. }
  163. params.bssid = b;
  164. params.ifname = hapd->conf->iface;
  165. params.ssid = hapd->conf->ssid.ssid;
  166. params.ssid_len = hapd->conf->ssid.ssid_len;
  167. params.test_socket = hapd->conf->test_socket;
  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. iface->drv_flags = capa.flags;
  190. iface->probe_resp_offloads = capa.probe_resp_offloads;
  191. iface->extended_capa = capa.extended_capa;
  192. iface->extended_capa_mask = capa.extended_capa_mask;
  193. iface->extended_capa_len = capa.extended_capa_len;
  194. iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
  195. }
  196. #ifdef CONFIG_INTERWORKING
  197. if (hapd->driver->set_qos_map && conf->qos_map_set_len &&
  198. hapd->driver->set_qos_map(hapd->drv_priv, conf->qos_map_set,
  199. conf->qos_map_set_len)) {
  200. wpa_printf(MSG_ERROR, "Failed to initialize QoS Map.");
  201. return -1;
  202. }
  203. #endif /* CONFIG_INTERWORKING */
  204. return 0;
  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. if (hostapd_driver_init(iface) ||
  229. hostapd_setup_interface(iface)) {
  230. hostapd_interface_deinit_free(iface);
  231. return NULL;
  232. }
  233. iface->init_done = 1;
  234. return iface;
  235. }
  236. static int hostapd_interface_init2(struct hostapd_iface *iface)
  237. {
  238. if (iface->init_done)
  239. return 0;
  240. if (hostapd_driver_init(iface) ||
  241. hostapd_setup_interface(iface))
  242. return -1;
  243. iface->init_done = 1;
  244. return 0;
  245. }
  246. /**
  247. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  248. */
  249. static void handle_term(int sig, void *signal_ctx)
  250. {
  251. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  252. eloop_terminate();
  253. }
  254. #ifndef CONFIG_NATIVE_WINDOWS
  255. static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
  256. {
  257. if (hostapd_reload_config(iface) < 0) {
  258. wpa_printf(MSG_WARNING, "Failed to read new configuration "
  259. "file - continuing with old.");
  260. }
  261. return 0;
  262. }
  263. /**
  264. * handle_reload - SIGHUP handler to reload configuration
  265. */
  266. static void handle_reload(int sig, void *signal_ctx)
  267. {
  268. struct hapd_interfaces *interfaces = signal_ctx;
  269. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  270. sig);
  271. hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
  272. }
  273. static void handle_dump_state(int sig, void *signal_ctx)
  274. {
  275. #ifdef HOSTAPD_DUMP_STATE
  276. struct hapd_interfaces *interfaces = signal_ctx;
  277. hostapd_for_each_interface(interfaces, handle_dump_state_iface, NULL);
  278. #endif /* HOSTAPD_DUMP_STATE */
  279. }
  280. #endif /* CONFIG_NATIVE_WINDOWS */
  281. static int hostapd_global_init(struct hapd_interfaces *interfaces,
  282. const char *entropy_file)
  283. {
  284. int i;
  285. os_memset(&global, 0, sizeof(global));
  286. hostapd_logger_register_cb(hostapd_logger_cb);
  287. if (eap_server_register_methods()) {
  288. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  289. return -1;
  290. }
  291. if (eloop_init()) {
  292. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  293. return -1;
  294. }
  295. random_init(entropy_file);
  296. #ifndef CONFIG_NATIVE_WINDOWS
  297. eloop_register_signal(SIGHUP, handle_reload, interfaces);
  298. eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
  299. #endif /* CONFIG_NATIVE_WINDOWS */
  300. eloop_register_signal_terminate(handle_term, interfaces);
  301. #ifndef CONFIG_NATIVE_WINDOWS
  302. openlog("hostapd", 0, LOG_DAEMON);
  303. #endif /* CONFIG_NATIVE_WINDOWS */
  304. for (i = 0; wpa_drivers[i]; i++)
  305. global.drv_count++;
  306. if (global.drv_count == 0) {
  307. wpa_printf(MSG_ERROR, "No drivers enabled");
  308. return -1;
  309. }
  310. global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
  311. if (global.drv_priv == NULL)
  312. return -1;
  313. return 0;
  314. }
  315. static void hostapd_global_deinit(const char *pid_file)
  316. {
  317. int i;
  318. for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
  319. if (!global.drv_priv[i])
  320. continue;
  321. wpa_drivers[i]->global_deinit(global.drv_priv[i]);
  322. }
  323. os_free(global.drv_priv);
  324. global.drv_priv = NULL;
  325. #ifdef EAP_SERVER_TNC
  326. tncs_global_deinit();
  327. #endif /* EAP_SERVER_TNC */
  328. random_deinit();
  329. eloop_destroy();
  330. #ifndef CONFIG_NATIVE_WINDOWS
  331. closelog();
  332. #endif /* CONFIG_NATIVE_WINDOWS */
  333. eap_server_unregister_methods();
  334. os_daemonize_terminate(pid_file);
  335. }
  336. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  337. const char *pid_file)
  338. {
  339. #ifdef EAP_SERVER_TNC
  340. int tnc = 0;
  341. size_t i, k;
  342. for (i = 0; !tnc && i < ifaces->count; i++) {
  343. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  344. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  345. tnc++;
  346. break;
  347. }
  348. }
  349. }
  350. if (tnc && tncs_global_init() < 0) {
  351. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  352. return -1;
  353. }
  354. #endif /* EAP_SERVER_TNC */
  355. if (daemonize && os_daemonize(pid_file)) {
  356. perror("daemon");
  357. return -1;
  358. }
  359. eloop_run();
  360. return 0;
  361. }
  362. static void show_version(void)
  363. {
  364. fprintf(stderr,
  365. "hostapd v" VERSION_STR "\n"
  366. "User space daemon for IEEE 802.11 AP management,\n"
  367. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  368. "Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> "
  369. "and contributors\n");
  370. }
  371. static void usage(void)
  372. {
  373. show_version();
  374. fprintf(stderr,
  375. "\n"
  376. "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
  377. "\\\n"
  378. " [-g <global ctrl_iface>] [-G <group>] \\\n"
  379. " <configuration file(s)>\n"
  380. "\n"
  381. "options:\n"
  382. " -h show this usage\n"
  383. " -d show more debug messages (-dd for even more)\n"
  384. " -B run daemon in the background\n"
  385. " -e entropy file\n"
  386. " -g global control interface path\n"
  387. " -G group for control interfaces\n"
  388. " -P PID file\n"
  389. " -K include key data in debug messages\n"
  390. #ifdef CONFIG_DEBUG_FILE
  391. " -f log output to debug file instead of stdout\n"
  392. #endif /* CONFIG_DEBUG_FILE */
  393. #ifdef CONFIG_DEBUG_LINUX_TRACING
  394. " -T = record to Linux tracing in addition to logging\n"
  395. " (records all messages regardless of debug verbosity)\n"
  396. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  397. " -t include timestamps in some debug messages\n"
  398. " -v show hostapd version\n");
  399. exit(1);
  400. }
  401. static const char * hostapd_msg_ifname_cb(void *ctx)
  402. {
  403. struct hostapd_data *hapd = ctx;
  404. if (hapd && hapd->iconf && hapd->iconf->bss &&
  405. hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
  406. return hapd->iconf->bss[0]->iface;
  407. return NULL;
  408. }
  409. static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
  410. const char *path)
  411. {
  412. char *pos;
  413. os_free(interfaces->global_iface_path);
  414. interfaces->global_iface_path = os_strdup(path);
  415. if (interfaces->global_iface_path == NULL)
  416. return -1;
  417. pos = os_strrchr(interfaces->global_iface_path, '/');
  418. if (pos == NULL) {
  419. wpa_printf(MSG_ERROR, "No '/' in the global control interface "
  420. "file");
  421. os_free(interfaces->global_iface_path);
  422. interfaces->global_iface_path = NULL;
  423. return -1;
  424. }
  425. *pos = '\0';
  426. interfaces->global_iface_name = pos + 1;
  427. return 0;
  428. }
  429. static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
  430. const char *group)
  431. {
  432. #ifndef CONFIG_NATIVE_WINDOWS
  433. struct group *grp;
  434. grp = getgrnam(group);
  435. if (grp == NULL) {
  436. wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
  437. return -1;
  438. }
  439. interfaces->ctrl_iface_group = grp->gr_gid;
  440. #endif /* CONFIG_NATIVE_WINDOWS */
  441. return 0;
  442. }
  443. int main(int argc, char *argv[])
  444. {
  445. struct hapd_interfaces interfaces;
  446. int ret = 1;
  447. size_t i, j;
  448. int c, debug = 0, daemonize = 0;
  449. char *pid_file = NULL;
  450. const char *log_file = NULL;
  451. const char *entropy_file = NULL;
  452. char **bss_config = NULL, **tmp_bss;
  453. size_t num_bss_configs = 0;
  454. #ifdef CONFIG_DEBUG_LINUX_TRACING
  455. int enable_trace_dbg = 0;
  456. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  457. if (os_program_init())
  458. return -1;
  459. os_memset(&interfaces, 0, sizeof(interfaces));
  460. interfaces.reload_config = hostapd_reload_config;
  461. interfaces.config_read_cb = hostapd_config_read;
  462. interfaces.for_each_interface = hostapd_for_each_interface;
  463. interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
  464. interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
  465. interfaces.driver_init = hostapd_driver_init;
  466. interfaces.global_iface_path = NULL;
  467. interfaces.global_iface_name = NULL;
  468. interfaces.global_ctrl_sock = -1;
  469. for (;;) {
  470. c = getopt(argc, argv, "b:Bde:f:hKP:Ttvg:G:");
  471. if (c < 0)
  472. break;
  473. switch (c) {
  474. case 'h':
  475. usage();
  476. break;
  477. case 'd':
  478. debug++;
  479. if (wpa_debug_level > 0)
  480. wpa_debug_level--;
  481. break;
  482. case 'B':
  483. daemonize++;
  484. break;
  485. case 'e':
  486. entropy_file = optarg;
  487. break;
  488. case 'f':
  489. log_file = optarg;
  490. break;
  491. case 'K':
  492. wpa_debug_show_keys++;
  493. break;
  494. case 'P':
  495. os_free(pid_file);
  496. pid_file = os_rel2abs_path(optarg);
  497. break;
  498. case 't':
  499. wpa_debug_timestamp++;
  500. break;
  501. #ifdef CONFIG_DEBUG_LINUX_TRACING
  502. case 'T':
  503. enable_trace_dbg = 1;
  504. break;
  505. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  506. case 'v':
  507. show_version();
  508. exit(1);
  509. break;
  510. case 'g':
  511. if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
  512. return -1;
  513. break;
  514. case 'G':
  515. if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
  516. return -1;
  517. break;
  518. case 'b':
  519. tmp_bss = os_realloc_array(bss_config,
  520. num_bss_configs + 1,
  521. sizeof(char *));
  522. if (tmp_bss == NULL)
  523. goto out;
  524. bss_config = tmp_bss;
  525. bss_config[num_bss_configs++] = optarg;
  526. break;
  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. /* Initialize interfaces */
  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. for (i = 0; i < num_bss_configs; i++) {
  571. struct hostapd_iface *iface;
  572. char *fname;
  573. wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
  574. fname = os_strchr(bss_config[i], ':');
  575. if (fname == NULL) {
  576. wpa_printf(MSG_ERROR,
  577. "Invalid BSS config identifier '%s'",
  578. bss_config[i]);
  579. goto out;
  580. }
  581. *fname++ = '\0';
  582. iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
  583. fname, debug);
  584. if (iface == NULL)
  585. goto out;
  586. for (j = 0; j < interfaces.count; j++) {
  587. if (interfaces.iface[j] == iface)
  588. break;
  589. }
  590. if (j == interfaces.count) {
  591. struct hostapd_iface **tmp;
  592. tmp = os_realloc_array(interfaces.iface,
  593. interfaces.count + 1,
  594. sizeof(struct hostapd_iface *));
  595. if (tmp == NULL) {
  596. hostapd_interface_deinit_free(iface);
  597. goto out;
  598. }
  599. interfaces.iface = tmp;
  600. interfaces.iface[interfaces.count++] = iface;
  601. }
  602. }
  603. for (i = 0; i < interfaces.count; i++) {
  604. if (hostapd_interface_init2(interfaces.iface[i]) < 0)
  605. goto out;
  606. }
  607. hostapd_global_ctrl_iface_init(&interfaces);
  608. if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
  609. wpa_printf(MSG_ERROR, "Failed to start eloop");
  610. goto out;
  611. }
  612. ret = 0;
  613. out:
  614. hostapd_global_ctrl_iface_deinit(&interfaces);
  615. /* Deinitialize all interfaces */
  616. for (i = 0; i < interfaces.count; i++)
  617. hostapd_interface_deinit_free(interfaces.iface[i]);
  618. os_free(interfaces.iface);
  619. hostapd_global_deinit(pid_file);
  620. os_free(pid_file);
  621. if (log_file)
  622. wpa_debug_close_file();
  623. wpa_debug_close_linux_tracing();
  624. os_free(bss_config);
  625. os_program_deinit();
  626. return ret;
  627. }