main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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->smps_modes = capa.smps_modes;
  192. iface->probe_resp_offloads = capa.probe_resp_offloads;
  193. iface->extended_capa = capa.extended_capa;
  194. iface->extended_capa_mask = capa.extended_capa_mask;
  195. iface->extended_capa_len = capa.extended_capa_len;
  196. iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
  197. }
  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. /* Not used anymore - ignore signal */
  261. }
  262. #endif /* CONFIG_NATIVE_WINDOWS */
  263. static int hostapd_global_init(struct hapd_interfaces *interfaces,
  264. const char *entropy_file)
  265. {
  266. int i;
  267. os_memset(&global, 0, sizeof(global));
  268. hostapd_logger_register_cb(hostapd_logger_cb);
  269. if (eap_server_register_methods()) {
  270. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  271. return -1;
  272. }
  273. if (eloop_init()) {
  274. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  275. return -1;
  276. }
  277. random_init(entropy_file);
  278. #ifndef CONFIG_NATIVE_WINDOWS
  279. eloop_register_signal(SIGHUP, handle_reload, interfaces);
  280. eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
  281. #endif /* CONFIG_NATIVE_WINDOWS */
  282. eloop_register_signal_terminate(handle_term, interfaces);
  283. #ifndef CONFIG_NATIVE_WINDOWS
  284. openlog("hostapd", 0, LOG_DAEMON);
  285. #endif /* CONFIG_NATIVE_WINDOWS */
  286. for (i = 0; wpa_drivers[i]; i++)
  287. global.drv_count++;
  288. if (global.drv_count == 0) {
  289. wpa_printf(MSG_ERROR, "No drivers enabled");
  290. return -1;
  291. }
  292. global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
  293. if (global.drv_priv == NULL)
  294. return -1;
  295. return 0;
  296. }
  297. static void hostapd_global_deinit(const char *pid_file)
  298. {
  299. int i;
  300. for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
  301. if (!global.drv_priv[i])
  302. continue;
  303. wpa_drivers[i]->global_deinit(global.drv_priv[i]);
  304. }
  305. os_free(global.drv_priv);
  306. global.drv_priv = NULL;
  307. #ifdef EAP_SERVER_TNC
  308. tncs_global_deinit();
  309. #endif /* EAP_SERVER_TNC */
  310. random_deinit();
  311. eloop_destroy();
  312. #ifndef CONFIG_NATIVE_WINDOWS
  313. closelog();
  314. #endif /* CONFIG_NATIVE_WINDOWS */
  315. eap_server_unregister_methods();
  316. os_daemonize_terminate(pid_file);
  317. }
  318. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  319. const char *pid_file)
  320. {
  321. #ifdef EAP_SERVER_TNC
  322. int tnc = 0;
  323. size_t i, k;
  324. for (i = 0; !tnc && i < ifaces->count; i++) {
  325. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  326. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  327. tnc++;
  328. break;
  329. }
  330. }
  331. }
  332. if (tnc && tncs_global_init() < 0) {
  333. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  334. return -1;
  335. }
  336. #endif /* EAP_SERVER_TNC */
  337. if (daemonize && os_daemonize(pid_file)) {
  338. perror("daemon");
  339. return -1;
  340. }
  341. eloop_run();
  342. return 0;
  343. }
  344. static void show_version(void)
  345. {
  346. fprintf(stderr,
  347. "hostapd v" VERSION_STR "\n"
  348. "User space daemon for IEEE 802.11 AP management,\n"
  349. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  350. "Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi> "
  351. "and contributors\n");
  352. }
  353. static void usage(void)
  354. {
  355. show_version();
  356. fprintf(stderr,
  357. "\n"
  358. "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
  359. "\\\n"
  360. " [-g <global ctrl_iface>] [-G <group>] \\\n"
  361. " <configuration file(s)>\n"
  362. "\n"
  363. "options:\n"
  364. " -h show this usage\n"
  365. " -d show more debug messages (-dd for even more)\n"
  366. " -B run daemon in the background\n"
  367. " -e entropy file\n"
  368. " -g global control interface path\n"
  369. " -G group for control interfaces\n"
  370. " -P PID file\n"
  371. " -K include key data in debug messages\n"
  372. #ifdef CONFIG_DEBUG_FILE
  373. " -f log output to debug file instead of stdout\n"
  374. #endif /* CONFIG_DEBUG_FILE */
  375. #ifdef CONFIG_DEBUG_LINUX_TRACING
  376. " -T = record to Linux tracing in addition to logging\n"
  377. " (records all messages regardless of debug verbosity)\n"
  378. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  379. " -t include timestamps in some debug messages\n"
  380. " -v show hostapd version\n");
  381. exit(1);
  382. }
  383. static const char * hostapd_msg_ifname_cb(void *ctx)
  384. {
  385. struct hostapd_data *hapd = ctx;
  386. if (hapd && hapd->iconf && hapd->iconf->bss &&
  387. hapd->iconf->num_bss > 0 && hapd->iconf->bss[0])
  388. return hapd->iconf->bss[0]->iface;
  389. return NULL;
  390. }
  391. static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
  392. const char *path)
  393. {
  394. char *pos;
  395. os_free(interfaces->global_iface_path);
  396. interfaces->global_iface_path = os_strdup(path);
  397. if (interfaces->global_iface_path == NULL)
  398. return -1;
  399. pos = os_strrchr(interfaces->global_iface_path, '/');
  400. if (pos == NULL) {
  401. wpa_printf(MSG_ERROR, "No '/' in the global control interface "
  402. "file");
  403. os_free(interfaces->global_iface_path);
  404. interfaces->global_iface_path = NULL;
  405. return -1;
  406. }
  407. *pos = '\0';
  408. interfaces->global_iface_name = pos + 1;
  409. return 0;
  410. }
  411. static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
  412. const char *group)
  413. {
  414. #ifndef CONFIG_NATIVE_WINDOWS
  415. struct group *grp;
  416. grp = getgrnam(group);
  417. if (grp == NULL) {
  418. wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
  419. return -1;
  420. }
  421. interfaces->ctrl_iface_group = grp->gr_gid;
  422. #endif /* CONFIG_NATIVE_WINDOWS */
  423. return 0;
  424. }
  425. #ifdef CONFIG_WPS
  426. static int gen_uuid(const char *txt_addr)
  427. {
  428. u8 addr[ETH_ALEN];
  429. u8 uuid[UUID_LEN];
  430. char buf[100];
  431. if (hwaddr_aton(txt_addr, addr) < 0)
  432. return -1;
  433. uuid_gen_mac_addr(addr, uuid);
  434. if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
  435. return -1;
  436. printf("%s\n", buf);
  437. return 0;
  438. }
  439. #endif /* CONFIG_WPS */
  440. int main(int argc, char *argv[])
  441. {
  442. struct hapd_interfaces interfaces;
  443. int ret = 1;
  444. size_t i, j;
  445. int c, debug = 0, daemonize = 0;
  446. char *pid_file = NULL;
  447. const char *log_file = NULL;
  448. const char *entropy_file = NULL;
  449. char **bss_config = NULL, **tmp_bss;
  450. size_t num_bss_configs = 0;
  451. #ifdef CONFIG_DEBUG_LINUX_TRACING
  452. int enable_trace_dbg = 0;
  453. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  454. if (os_program_init())
  455. return -1;
  456. os_memset(&interfaces, 0, sizeof(interfaces));
  457. interfaces.reload_config = hostapd_reload_config;
  458. interfaces.config_read_cb = hostapd_config_read;
  459. interfaces.for_each_interface = hostapd_for_each_interface;
  460. interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
  461. interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
  462. interfaces.driver_init = hostapd_driver_init;
  463. interfaces.global_iface_path = NULL;
  464. interfaces.global_iface_name = NULL;
  465. interfaces.global_ctrl_sock = -1;
  466. for (;;) {
  467. c = getopt(argc, argv, "b:Bde:f:hKP:Ttu:vg:G:");
  468. if (c < 0)
  469. break;
  470. switch (c) {
  471. case 'h':
  472. usage();
  473. break;
  474. case 'd':
  475. debug++;
  476. if (wpa_debug_level > 0)
  477. wpa_debug_level--;
  478. break;
  479. case 'B':
  480. daemonize++;
  481. break;
  482. case 'e':
  483. entropy_file = optarg;
  484. break;
  485. case 'f':
  486. log_file = optarg;
  487. break;
  488. case 'K':
  489. wpa_debug_show_keys++;
  490. break;
  491. case 'P':
  492. os_free(pid_file);
  493. pid_file = os_rel2abs_path(optarg);
  494. break;
  495. case 't':
  496. wpa_debug_timestamp++;
  497. break;
  498. #ifdef CONFIG_DEBUG_LINUX_TRACING
  499. case 'T':
  500. enable_trace_dbg = 1;
  501. break;
  502. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  503. case 'v':
  504. show_version();
  505. exit(1);
  506. break;
  507. case 'g':
  508. if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
  509. return -1;
  510. break;
  511. case 'G':
  512. if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
  513. return -1;
  514. break;
  515. case 'b':
  516. tmp_bss = os_realloc_array(bss_config,
  517. num_bss_configs + 1,
  518. sizeof(char *));
  519. if (tmp_bss == NULL)
  520. goto out;
  521. bss_config = tmp_bss;
  522. bss_config[num_bss_configs++] = optarg;
  523. break;
  524. #ifdef CONFIG_WPS
  525. case 'u':
  526. return gen_uuid(optarg);
  527. #endif /* CONFIG_WPS */
  528. default:
  529. usage();
  530. break;
  531. }
  532. }
  533. if (optind == argc && interfaces.global_iface_path == NULL &&
  534. num_bss_configs == 0)
  535. usage();
  536. wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
  537. if (log_file)
  538. wpa_debug_open_file(log_file);
  539. #ifdef CONFIG_DEBUG_LINUX_TRACING
  540. if (enable_trace_dbg) {
  541. int tret = wpa_debug_open_linux_tracing();
  542. if (tret) {
  543. wpa_printf(MSG_ERROR, "Failed to enable trace logging");
  544. return -1;
  545. }
  546. }
  547. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  548. interfaces.count = argc - optind;
  549. if (interfaces.count || num_bss_configs) {
  550. interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
  551. sizeof(struct hostapd_iface *));
  552. if (interfaces.iface == NULL) {
  553. wpa_printf(MSG_ERROR, "malloc failed");
  554. return -1;
  555. }
  556. }
  557. if (hostapd_global_init(&interfaces, entropy_file)) {
  558. wpa_printf(MSG_ERROR, "Failed to initilize global context");
  559. return -1;
  560. }
  561. /* Allocate and parse configuration for full interface files */
  562. for (i = 0; i < interfaces.count; i++) {
  563. interfaces.iface[i] = hostapd_interface_init(&interfaces,
  564. argv[optind + i],
  565. debug);
  566. if (!interfaces.iface[i]) {
  567. wpa_printf(MSG_ERROR, "Failed to initialize interface");
  568. goto out;
  569. }
  570. }
  571. /* Allocate and parse configuration for per-BSS files */
  572. for (i = 0; i < num_bss_configs; i++) {
  573. struct hostapd_iface *iface;
  574. char *fname;
  575. wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
  576. fname = os_strchr(bss_config[i], ':');
  577. if (fname == NULL) {
  578. wpa_printf(MSG_ERROR,
  579. "Invalid BSS config identifier '%s'",
  580. bss_config[i]);
  581. goto out;
  582. }
  583. *fname++ = '\0';
  584. iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
  585. fname, debug);
  586. if (iface == NULL)
  587. goto out;
  588. for (j = 0; j < interfaces.count; j++) {
  589. if (interfaces.iface[j] == iface)
  590. break;
  591. }
  592. if (j == interfaces.count) {
  593. struct hostapd_iface **tmp;
  594. tmp = os_realloc_array(interfaces.iface,
  595. interfaces.count + 1,
  596. sizeof(struct hostapd_iface *));
  597. if (tmp == NULL) {
  598. hostapd_interface_deinit_free(iface);
  599. goto out;
  600. }
  601. interfaces.iface = tmp;
  602. interfaces.iface[interfaces.count++] = iface;
  603. }
  604. }
  605. /*
  606. * Enable configured interfaces. Depending on channel configuration,
  607. * this may complete full initialization before returning or use a
  608. * callback mechanism to complete setup in case of operations like HT
  609. * co-ex scans, ACS, or DFS are needed to determine channel parameters.
  610. * In such case, the interface will be enabled from eloop context within
  611. * hostapd_global_run().
  612. */
  613. interfaces.terminate_on_error = interfaces.count;
  614. for (i = 0; i < interfaces.count; i++) {
  615. if (hostapd_driver_init(interfaces.iface[i]) ||
  616. hostapd_setup_interface(interfaces.iface[i]))
  617. goto out;
  618. }
  619. hostapd_global_ctrl_iface_init(&interfaces);
  620. if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
  621. wpa_printf(MSG_ERROR, "Failed to start eloop");
  622. goto out;
  623. }
  624. ret = 0;
  625. out:
  626. hostapd_global_ctrl_iface_deinit(&interfaces);
  627. /* Deinitialize all interfaces */
  628. for (i = 0; i < interfaces.count; i++) {
  629. if (!interfaces.iface[i])
  630. continue;
  631. interfaces.iface[i]->driver_ap_teardown =
  632. !!(interfaces.iface[i]->drv_flags &
  633. WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
  634. hostapd_interface_deinit_free(interfaces.iface[i]);
  635. }
  636. os_free(interfaces.iface);
  637. hostapd_global_deinit(pid_file);
  638. os_free(pid_file);
  639. if (log_file)
  640. wpa_debug_close_file();
  641. wpa_debug_close_linux_tracing();
  642. os_free(bss_config);
  643. os_program_deinit();
  644. return ret;
  645. }