main.c 18 KB

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