main.c 18 KB

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