main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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, txt);
  90. else if (addr)
  91. os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
  92. MAC2STR(addr), module_str ? " " : "",
  93. module_str, txt);
  94. else
  95. os_snprintf(format, maxlen, "%s%s%s",
  96. module_str, module_str ? ": " : "", txt);
  97. if ((conf_stdout & module) && level >= conf_stdout_level) {
  98. wpa_debug_print_timestamp();
  99. wpa_printf(MSG_INFO, "%s", format);
  100. }
  101. #ifndef CONFIG_NATIVE_WINDOWS
  102. if ((conf_syslog & module) && level >= conf_syslog_level) {
  103. int priority;
  104. switch (level) {
  105. case HOSTAPD_LEVEL_DEBUG_VERBOSE:
  106. case HOSTAPD_LEVEL_DEBUG:
  107. priority = LOG_DEBUG;
  108. break;
  109. case HOSTAPD_LEVEL_INFO:
  110. priority = LOG_INFO;
  111. break;
  112. case HOSTAPD_LEVEL_NOTICE:
  113. priority = LOG_NOTICE;
  114. break;
  115. case HOSTAPD_LEVEL_WARNING:
  116. priority = LOG_WARNING;
  117. break;
  118. default:
  119. priority = LOG_INFO;
  120. break;
  121. }
  122. syslog(priority, "%s", format);
  123. }
  124. #endif /* CONFIG_NATIVE_WINDOWS */
  125. os_free(format);
  126. }
  127. #endif /* CONFIG_NO_HOSTAPD_LOGGER */
  128. /**
  129. * hostapd_driver_init - Preparate driver interface
  130. */
  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. return 0;
  197. }
  198. /**
  199. * hostapd_interface_init - Read configuration file and init BSS data
  200. *
  201. * This function is used to parse configuration file for a full interface (one
  202. * or more BSSes sharing the same radio) and allocate memory for the BSS
  203. * interfaces. No actiual driver operations are started.
  204. */
  205. static struct hostapd_iface *
  206. hostapd_interface_init(struct hapd_interfaces *interfaces,
  207. const char *config_fname, int debug)
  208. {
  209. struct hostapd_iface *iface;
  210. int k;
  211. wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
  212. iface = hostapd_init(interfaces, config_fname);
  213. if (!iface)
  214. return NULL;
  215. iface->interfaces = interfaces;
  216. for (k = 0; k < debug; k++) {
  217. if (iface->bss[0]->conf->logger_stdout_level > 0)
  218. iface->bss[0]->conf->logger_stdout_level--;
  219. }
  220. if (iface->conf->bss[0]->iface[0] == '\0' &&
  221. !hostapd_drv_none(iface->bss[0])) {
  222. wpa_printf(MSG_ERROR, "Interface name not specified in %s",
  223. config_fname);
  224. hostapd_interface_deinit_free(iface);
  225. return NULL;
  226. }
  227. return iface;
  228. }
  229. /**
  230. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  231. */
  232. static void handle_term(int sig, void *signal_ctx)
  233. {
  234. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  235. eloop_terminate();
  236. }
  237. #ifndef CONFIG_NATIVE_WINDOWS
  238. static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
  239. {
  240. if (hostapd_reload_config(iface) < 0) {
  241. wpa_printf(MSG_WARNING, "Failed to read new configuration "
  242. "file - continuing with old.");
  243. }
  244. return 0;
  245. }
  246. /**
  247. * handle_reload - SIGHUP handler to reload configuration
  248. */
  249. static void handle_reload(int sig, void *signal_ctx)
  250. {
  251. struct hapd_interfaces *interfaces = signal_ctx;
  252. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  253. sig);
  254. hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
  255. }
  256. static void handle_dump_state(int sig, void *signal_ctx)
  257. {
  258. /* Not used anymore - ignore signal */
  259. }
  260. #endif /* CONFIG_NATIVE_WINDOWS */
  261. static int hostapd_global_init(struct hapd_interfaces *interfaces,
  262. const char *entropy_file)
  263. {
  264. int i;
  265. os_memset(&global, 0, sizeof(global));
  266. hostapd_logger_register_cb(hostapd_logger_cb);
  267. if (eap_server_register_methods()) {
  268. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  269. return -1;
  270. }
  271. if (eloop_init()) {
  272. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  273. return -1;
  274. }
  275. random_init(entropy_file);
  276. #ifndef CONFIG_NATIVE_WINDOWS
  277. eloop_register_signal(SIGHUP, handle_reload, interfaces);
  278. eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
  279. #endif /* CONFIG_NATIVE_WINDOWS */
  280. eloop_register_signal_terminate(handle_term, interfaces);
  281. #ifndef CONFIG_NATIVE_WINDOWS
  282. openlog("hostapd", 0, LOG_DAEMON);
  283. #endif /* CONFIG_NATIVE_WINDOWS */
  284. for (i = 0; wpa_drivers[i]; i++)
  285. global.drv_count++;
  286. if (global.drv_count == 0) {
  287. wpa_printf(MSG_ERROR, "No drivers enabled");
  288. return -1;
  289. }
  290. global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
  291. if (global.drv_priv == NULL)
  292. return -1;
  293. return 0;
  294. }
  295. static void hostapd_global_deinit(const char *pid_file)
  296. {
  297. int i;
  298. for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
  299. if (!global.drv_priv[i])
  300. continue;
  301. wpa_drivers[i]->global_deinit(global.drv_priv[i]);
  302. }
  303. os_free(global.drv_priv);
  304. global.drv_priv = NULL;
  305. #ifdef EAP_SERVER_TNC
  306. tncs_global_deinit();
  307. #endif /* EAP_SERVER_TNC */
  308. random_deinit();
  309. eloop_destroy();
  310. #ifndef CONFIG_NATIVE_WINDOWS
  311. closelog();
  312. #endif /* CONFIG_NATIVE_WINDOWS */
  313. eap_server_unregister_methods();
  314. os_daemonize_terminate(pid_file);
  315. }
  316. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  317. const char *pid_file)
  318. {
  319. #ifdef EAP_SERVER_TNC
  320. int tnc = 0;
  321. size_t i, k;
  322. for (i = 0; !tnc && i < ifaces->count; i++) {
  323. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  324. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  325. tnc++;
  326. break;
  327. }
  328. }
  329. }
  330. if (tnc && tncs_global_init() < 0) {
  331. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  332. return -1;
  333. }
  334. #endif /* EAP_SERVER_TNC */
  335. if (daemonize && os_daemonize(pid_file)) {
  336. perror("daemon");
  337. return -1;
  338. }
  339. eloop_run();
  340. return 0;
  341. }
  342. static void show_version(void)
  343. {
  344. fprintf(stderr,
  345. "hostapd v" VERSION_STR "\n"
  346. "User space daemon for IEEE 802.11 AP management,\n"
  347. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  348. "Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi> "
  349. "and contributors\n");
  350. }
  351. static void usage(void)
  352. {
  353. show_version();
  354. fprintf(stderr,
  355. "\n"
  356. "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
  357. "\\\n"
  358. " [-g <global ctrl_iface>] [-G <group>] \\\n"
  359. " <configuration file(s)>\n"
  360. "\n"
  361. "options:\n"
  362. " -h show this usage\n"
  363. " -d show more debug messages (-dd for even more)\n"
  364. " -B run daemon in the background\n"
  365. " -e entropy file\n"
  366. " -g global control interface path\n"
  367. " -G group for control interfaces\n"
  368. " -P PID file\n"
  369. " -K include key data in debug messages\n"
  370. #ifdef CONFIG_DEBUG_FILE
  371. " -f log output to debug file instead of stdout\n"
  372. #endif /* CONFIG_DEBUG_FILE */
  373. #ifdef CONFIG_DEBUG_LINUX_TRACING
  374. " -T = record to Linux tracing in addition to logging\n"
  375. " (records all messages regardless of debug verbosity)\n"
  376. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  377. " -t include timestamps in some debug messages\n"
  378. " -v show hostapd version\n");
  379. exit(1);
  380. }
  381. static const char * hostapd_msg_ifname_cb(void *ctx)
  382. {
  383. struct hostapd_data *hapd = ctx;
  384. if (hapd && hapd->iconf && hapd->iconf->bss &&
  385. hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
  386. return hapd->iconf->bss[0]->iface;
  387. return NULL;
  388. }
  389. static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
  390. const char *path)
  391. {
  392. char *pos;
  393. os_free(interfaces->global_iface_path);
  394. interfaces->global_iface_path = os_strdup(path);
  395. if (interfaces->global_iface_path == NULL)
  396. return -1;
  397. pos = os_strrchr(interfaces->global_iface_path, '/');
  398. if (pos == NULL) {
  399. wpa_printf(MSG_ERROR, "No '/' in the global control interface "
  400. "file");
  401. os_free(interfaces->global_iface_path);
  402. interfaces->global_iface_path = NULL;
  403. return -1;
  404. }
  405. *pos = '\0';
  406. interfaces->global_iface_name = pos + 1;
  407. return 0;
  408. }
  409. static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
  410. const char *group)
  411. {
  412. #ifndef CONFIG_NATIVE_WINDOWS
  413. struct group *grp;
  414. grp = getgrnam(group);
  415. if (grp == NULL) {
  416. wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
  417. return -1;
  418. }
  419. interfaces->ctrl_iface_group = grp->gr_gid;
  420. #endif /* CONFIG_NATIVE_WINDOWS */
  421. return 0;
  422. }
  423. #ifdef CONFIG_WPS
  424. static int gen_uuid(const char *txt_addr)
  425. {
  426. u8 addr[ETH_ALEN];
  427. u8 uuid[UUID_LEN];
  428. char buf[100];
  429. if (hwaddr_aton(txt_addr, addr) < 0)
  430. return -1;
  431. uuid_gen_mac_addr(addr, uuid);
  432. if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
  433. return -1;
  434. printf("%s\n", buf);
  435. return 0;
  436. }
  437. #endif /* CONFIG_WPS */
  438. int main(int argc, char *argv[])
  439. {
  440. struct hapd_interfaces interfaces;
  441. int ret = 1;
  442. size_t i, j;
  443. int c, debug = 0, daemonize = 0;
  444. char *pid_file = NULL;
  445. const char *log_file = NULL;
  446. const char *entropy_file = NULL;
  447. char **bss_config = NULL, **tmp_bss;
  448. size_t num_bss_configs = 0;
  449. #ifdef CONFIG_DEBUG_LINUX_TRACING
  450. int enable_trace_dbg = 0;
  451. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  452. if (os_program_init())
  453. return -1;
  454. os_memset(&interfaces, 0, sizeof(interfaces));
  455. interfaces.reload_config = hostapd_reload_config;
  456. interfaces.config_read_cb = hostapd_config_read;
  457. interfaces.for_each_interface = hostapd_for_each_interface;
  458. interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
  459. interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
  460. interfaces.driver_init = hostapd_driver_init;
  461. interfaces.global_iface_path = NULL;
  462. interfaces.global_iface_name = NULL;
  463. interfaces.global_ctrl_sock = -1;
  464. for (;;) {
  465. c = getopt(argc, argv, "b:Bde:f:hKP:Ttu:vg:G:");
  466. if (c < 0)
  467. break;
  468. switch (c) {
  469. case 'h':
  470. usage();
  471. break;
  472. case 'd':
  473. debug++;
  474. if (wpa_debug_level > 0)
  475. wpa_debug_level--;
  476. break;
  477. case 'B':
  478. daemonize++;
  479. break;
  480. case 'e':
  481. entropy_file = optarg;
  482. break;
  483. case 'f':
  484. log_file = optarg;
  485. break;
  486. case 'K':
  487. wpa_debug_show_keys++;
  488. break;
  489. case 'P':
  490. os_free(pid_file);
  491. pid_file = os_rel2abs_path(optarg);
  492. break;
  493. case 't':
  494. wpa_debug_timestamp++;
  495. break;
  496. #ifdef CONFIG_DEBUG_LINUX_TRACING
  497. case 'T':
  498. enable_trace_dbg = 1;
  499. break;
  500. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  501. case 'v':
  502. show_version();
  503. exit(1);
  504. break;
  505. case 'g':
  506. if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
  507. return -1;
  508. break;
  509. case 'G':
  510. if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
  511. return -1;
  512. break;
  513. case 'b':
  514. tmp_bss = os_realloc_array(bss_config,
  515. num_bss_configs + 1,
  516. sizeof(char *));
  517. if (tmp_bss == NULL)
  518. goto out;
  519. bss_config = tmp_bss;
  520. bss_config[num_bss_configs++] = optarg;
  521. break;
  522. #ifdef CONFIG_WPS
  523. case 'u':
  524. return gen_uuid(optarg);
  525. #endif /* CONFIG_WPS */
  526. default:
  527. usage();
  528. break;
  529. }
  530. }
  531. if (optind == argc && interfaces.global_iface_path == NULL &&
  532. num_bss_configs == 0)
  533. usage();
  534. wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
  535. if (log_file)
  536. wpa_debug_open_file(log_file);
  537. #ifdef CONFIG_DEBUG_LINUX_TRACING
  538. if (enable_trace_dbg) {
  539. int tret = wpa_debug_open_linux_tracing();
  540. if (tret) {
  541. wpa_printf(MSG_ERROR, "Failed to enable trace logging");
  542. return -1;
  543. }
  544. }
  545. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  546. interfaces.count = argc - optind;
  547. if (interfaces.count || num_bss_configs) {
  548. interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
  549. sizeof(struct hostapd_iface *));
  550. if (interfaces.iface == NULL) {
  551. wpa_printf(MSG_ERROR, "malloc failed");
  552. return -1;
  553. }
  554. }
  555. if (hostapd_global_init(&interfaces, entropy_file)) {
  556. wpa_printf(MSG_ERROR, "Failed to initilize global context");
  557. return -1;
  558. }
  559. /* Allocate and parse configuration for full interface files */
  560. for (i = 0; i < interfaces.count; i++) {
  561. interfaces.iface[i] = hostapd_interface_init(&interfaces,
  562. argv[optind + i],
  563. debug);
  564. if (!interfaces.iface[i]) {
  565. wpa_printf(MSG_ERROR, "Failed to initialize interface");
  566. goto out;
  567. }
  568. }
  569. /* Allocate and parse configuration for per-BSS files */
  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. /*
  604. * Enable configured interfaces. Depending on channel configuration,
  605. * this may complete full initialization before returning or use a
  606. * callback mechanism to complete setup in case of operations like HT
  607. * co-ex scans, ACS, or DFS are needed to determine channel parameters.
  608. * In such case, the interface will be enabled from eloop context within
  609. * hostapd_global_run().
  610. */
  611. interfaces.terminate_on_error = interfaces.count;
  612. for (i = 0; i < interfaces.count; i++) {
  613. if (hostapd_driver_init(interfaces.iface[i]) ||
  614. hostapd_setup_interface(interfaces.iface[i]))
  615. goto out;
  616. }
  617. hostapd_global_ctrl_iface_init(&interfaces);
  618. if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
  619. wpa_printf(MSG_ERROR, "Failed to start eloop");
  620. goto out;
  621. }
  622. ret = 0;
  623. out:
  624. hostapd_global_ctrl_iface_deinit(&interfaces);
  625. /* Deinitialize all interfaces */
  626. for (i = 0; i < interfaces.count; i++) {
  627. if (!interfaces.iface[i])
  628. continue;
  629. interfaces.iface[i]->driver_ap_teardown =
  630. !!(interfaces.iface[i]->drv_flags &
  631. WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
  632. hostapd_interface_deinit_free(interfaces.iface[i]);
  633. }
  634. os_free(interfaces.iface);
  635. hostapd_global_deinit(pid_file);
  636. os_free(pid_file);
  637. if (log_file)
  638. wpa_debug_close_file();
  639. wpa_debug_close_linux_tracing();
  640. os_free(bss_config);
  641. os_program_deinit();
  642. return ret;
  643. }