main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * hostapd / main()
  3. * Copyright (c) 2002-2016, 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 "fst/fst.h"
  26. #include "config_file.h"
  27. #include "eap_register.h"
  28. #include "ctrl_iface.h"
  29. struct hapd_global {
  30. void **drv_priv;
  31. size_t drv_count;
  32. };
  33. static struct hapd_global global;
  34. #ifndef CONFIG_NO_HOSTAPD_LOGGER
  35. static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
  36. int level, const char *txt, size_t len)
  37. {
  38. struct hostapd_data *hapd = ctx;
  39. char *format, *module_str;
  40. int maxlen;
  41. int conf_syslog_level, conf_stdout_level;
  42. unsigned int conf_syslog, conf_stdout;
  43. maxlen = len + 100;
  44. format = os_malloc(maxlen);
  45. if (!format)
  46. return;
  47. if (hapd && hapd->conf) {
  48. conf_syslog_level = hapd->conf->logger_syslog_level;
  49. conf_stdout_level = hapd->conf->logger_stdout_level;
  50. conf_syslog = hapd->conf->logger_syslog;
  51. conf_stdout = hapd->conf->logger_stdout;
  52. } else {
  53. conf_syslog_level = conf_stdout_level = 0;
  54. conf_syslog = conf_stdout = (unsigned int) -1;
  55. }
  56. switch (module) {
  57. case HOSTAPD_MODULE_IEEE80211:
  58. module_str = "IEEE 802.11";
  59. break;
  60. case HOSTAPD_MODULE_IEEE8021X:
  61. module_str = "IEEE 802.1X";
  62. break;
  63. case HOSTAPD_MODULE_RADIUS:
  64. module_str = "RADIUS";
  65. break;
  66. case HOSTAPD_MODULE_WPA:
  67. module_str = "WPA";
  68. break;
  69. case HOSTAPD_MODULE_DRIVER:
  70. module_str = "DRIVER";
  71. break;
  72. case HOSTAPD_MODULE_IAPP:
  73. module_str = "IAPP";
  74. break;
  75. case HOSTAPD_MODULE_MLME:
  76. module_str = "MLME";
  77. break;
  78. default:
  79. module_str = NULL;
  80. break;
  81. }
  82. if (hapd && hapd->conf && addr)
  83. os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
  84. hapd->conf->iface, MAC2STR(addr),
  85. module_str ? " " : "", module_str ? module_str : "",
  86. txt);
  87. else if (hapd && hapd->conf)
  88. os_snprintf(format, maxlen, "%s:%s%s %s",
  89. hapd->conf->iface, module_str ? " " : "",
  90. module_str ? module_str : "", txt);
  91. else if (addr)
  92. os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
  93. MAC2STR(addr), module_str ? " " : "",
  94. module_str ? module_str : "", txt);
  95. else
  96. os_snprintf(format, maxlen, "%s%s%s",
  97. module_str ? module_str : "",
  98. module_str ? ": " : "", txt);
  99. if ((conf_stdout & module) && level >= conf_stdout_level) {
  100. wpa_debug_print_timestamp();
  101. wpa_printf(MSG_INFO, "%s", format);
  102. }
  103. #ifndef CONFIG_NATIVE_WINDOWS
  104. if ((conf_syslog & module) && level >= conf_syslog_level) {
  105. int priority;
  106. switch (level) {
  107. case HOSTAPD_LEVEL_DEBUG_VERBOSE:
  108. case HOSTAPD_LEVEL_DEBUG:
  109. priority = LOG_DEBUG;
  110. break;
  111. case HOSTAPD_LEVEL_INFO:
  112. priority = LOG_INFO;
  113. break;
  114. case HOSTAPD_LEVEL_NOTICE:
  115. priority = LOG_NOTICE;
  116. break;
  117. case HOSTAPD_LEVEL_WARNING:
  118. priority = LOG_WARNING;
  119. break;
  120. default:
  121. priority = LOG_INFO;
  122. break;
  123. }
  124. syslog(priority, "%s", format);
  125. }
  126. #endif /* CONFIG_NATIVE_WINDOWS */
  127. os_free(format);
  128. }
  129. #endif /* CONFIG_NO_HOSTAPD_LOGGER */
  130. /**
  131. * hostapd_driver_init - Preparate driver interface
  132. */
  133. static int hostapd_driver_init(struct hostapd_iface *iface)
  134. {
  135. struct wpa_init_params params;
  136. size_t i;
  137. struct hostapd_data *hapd = iface->bss[0];
  138. struct hostapd_bss_config *conf = hapd->conf;
  139. u8 *b = conf->bssid;
  140. struct wpa_driver_capa capa;
  141. if (hapd->driver == NULL || hapd->driver->hapd_init == NULL) {
  142. wpa_printf(MSG_ERROR, "No hostapd driver wrapper available");
  143. return -1;
  144. }
  145. /* Initialize the driver interface */
  146. if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
  147. b = NULL;
  148. os_memset(&params, 0, sizeof(params));
  149. for (i = 0; wpa_drivers[i]; i++) {
  150. if (wpa_drivers[i] != hapd->driver)
  151. continue;
  152. if (global.drv_priv[i] == NULL &&
  153. wpa_drivers[i]->global_init) {
  154. global.drv_priv[i] =
  155. wpa_drivers[i]->global_init(iface->interfaces);
  156. if (global.drv_priv[i] == NULL) {
  157. wpa_printf(MSG_ERROR, "Failed to initialize "
  158. "driver '%s'",
  159. wpa_drivers[i]->name);
  160. return -1;
  161. }
  162. }
  163. params.global_priv = global.drv_priv[i];
  164. break;
  165. }
  166. params.bssid = b;
  167. params.ifname = hapd->conf->iface;
  168. params.driver_params = hapd->iconf->driver_params;
  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. struct wowlan_triggers *triggs;
  191. iface->drv_flags = capa.flags;
  192. iface->smps_modes = capa.smps_modes;
  193. iface->probe_resp_offloads = capa.probe_resp_offloads;
  194. iface->extended_capa = capa.extended_capa;
  195. iface->extended_capa_mask = capa.extended_capa_mask;
  196. iface->extended_capa_len = capa.extended_capa_len;
  197. iface->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
  198. triggs = wpa_get_wowlan_triggers(conf->wowlan_triggers, &capa);
  199. if (triggs && hapd->driver->set_wowlan) {
  200. if (hapd->driver->set_wowlan(hapd->drv_priv, triggs))
  201. wpa_printf(MSG_ERROR, "set_wowlan failed");
  202. }
  203. os_free(triggs);
  204. }
  205. return 0;
  206. }
  207. /**
  208. * hostapd_interface_init - Read configuration file and init BSS data
  209. *
  210. * This function is used to parse configuration file for a full interface (one
  211. * or more BSSes sharing the same radio) and allocate memory for the BSS
  212. * interfaces. No actiual driver operations are started.
  213. */
  214. static struct hostapd_iface *
  215. hostapd_interface_init(struct hapd_interfaces *interfaces,
  216. const char *config_fname, int debug)
  217. {
  218. struct hostapd_iface *iface;
  219. int k;
  220. wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
  221. iface = hostapd_init(interfaces, config_fname);
  222. if (!iface)
  223. return NULL;
  224. iface->interfaces = interfaces;
  225. for (k = 0; k < debug; k++) {
  226. if (iface->bss[0]->conf->logger_stdout_level > 0)
  227. iface->bss[0]->conf->logger_stdout_level--;
  228. }
  229. if (iface->conf->bss[0]->iface[0] == '\0' &&
  230. !hostapd_drv_none(iface->bss[0])) {
  231. wpa_printf(MSG_ERROR, "Interface name not specified in %s",
  232. config_fname);
  233. hostapd_interface_deinit_free(iface);
  234. return NULL;
  235. }
  236. return iface;
  237. }
  238. /**
  239. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  240. */
  241. static void handle_term(int sig, void *signal_ctx)
  242. {
  243. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  244. eloop_terminate();
  245. }
  246. #ifndef CONFIG_NATIVE_WINDOWS
  247. static int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
  248. {
  249. if (hostapd_reload_config(iface) < 0) {
  250. wpa_printf(MSG_WARNING, "Failed to read new configuration "
  251. "file - continuing with old.");
  252. }
  253. return 0;
  254. }
  255. /**
  256. * handle_reload - SIGHUP handler to reload configuration
  257. */
  258. static void handle_reload(int sig, void *signal_ctx)
  259. {
  260. struct hapd_interfaces *interfaces = signal_ctx;
  261. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  262. sig);
  263. hostapd_for_each_interface(interfaces, handle_reload_iface, NULL);
  264. }
  265. static void handle_dump_state(int sig, void *signal_ctx)
  266. {
  267. /* Not used anymore - ignore signal */
  268. }
  269. #endif /* CONFIG_NATIVE_WINDOWS */
  270. static int hostapd_global_init(struct hapd_interfaces *interfaces,
  271. const char *entropy_file)
  272. {
  273. int i;
  274. os_memset(&global, 0, sizeof(global));
  275. hostapd_logger_register_cb(hostapd_logger_cb);
  276. if (eap_server_register_methods()) {
  277. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  278. return -1;
  279. }
  280. if (eloop_init()) {
  281. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  282. return -1;
  283. }
  284. random_init(entropy_file);
  285. #ifndef CONFIG_NATIVE_WINDOWS
  286. eloop_register_signal(SIGHUP, handle_reload, interfaces);
  287. eloop_register_signal(SIGUSR1, handle_dump_state, interfaces);
  288. #endif /* CONFIG_NATIVE_WINDOWS */
  289. eloop_register_signal_terminate(handle_term, interfaces);
  290. #ifndef CONFIG_NATIVE_WINDOWS
  291. openlog("hostapd", 0, LOG_DAEMON);
  292. #endif /* CONFIG_NATIVE_WINDOWS */
  293. for (i = 0; wpa_drivers[i]; i++)
  294. global.drv_count++;
  295. if (global.drv_count == 0) {
  296. wpa_printf(MSG_ERROR, "No drivers enabled");
  297. return -1;
  298. }
  299. global.drv_priv = os_calloc(global.drv_count, sizeof(void *));
  300. if (global.drv_priv == NULL)
  301. return -1;
  302. return 0;
  303. }
  304. static void hostapd_global_deinit(const char *pid_file)
  305. {
  306. int i;
  307. for (i = 0; wpa_drivers[i] && global.drv_priv; i++) {
  308. if (!global.drv_priv[i])
  309. continue;
  310. wpa_drivers[i]->global_deinit(global.drv_priv[i]);
  311. }
  312. os_free(global.drv_priv);
  313. global.drv_priv = NULL;
  314. #ifdef EAP_SERVER_TNC
  315. tncs_global_deinit();
  316. #endif /* EAP_SERVER_TNC */
  317. random_deinit();
  318. eloop_destroy();
  319. #ifndef CONFIG_NATIVE_WINDOWS
  320. closelog();
  321. #endif /* CONFIG_NATIVE_WINDOWS */
  322. eap_server_unregister_methods();
  323. os_daemonize_terminate(pid_file);
  324. }
  325. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  326. const char *pid_file)
  327. {
  328. #ifdef EAP_SERVER_TNC
  329. int tnc = 0;
  330. size_t i, k;
  331. for (i = 0; !tnc && i < ifaces->count; i++) {
  332. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  333. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  334. tnc++;
  335. break;
  336. }
  337. }
  338. }
  339. if (tnc && tncs_global_init() < 0) {
  340. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  341. return -1;
  342. }
  343. #endif /* EAP_SERVER_TNC */
  344. if (daemonize) {
  345. if (os_daemonize(pid_file)) {
  346. wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
  347. return -1;
  348. }
  349. if (eloop_sock_requeue()) {
  350. wpa_printf(MSG_ERROR, "eloop_sock_requeue: %s",
  351. strerror(errno));
  352. return -1;
  353. }
  354. }
  355. eloop_run();
  356. return 0;
  357. }
  358. static void show_version(void)
  359. {
  360. fprintf(stderr,
  361. "hostapd v" VERSION_STR "\n"
  362. "User space daemon for IEEE 802.11 AP management,\n"
  363. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  364. "Copyright (c) 2002-2016, Jouni Malinen <j@w1.fi> "
  365. "and contributors\n");
  366. }
  367. static void usage(void)
  368. {
  369. show_version();
  370. fprintf(stderr,
  371. "\n"
  372. "usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] "
  373. "\\\n"
  374. " [-g <global ctrl_iface>] [-G <group>] \\\n"
  375. " <configuration file(s)>\n"
  376. "\n"
  377. "options:\n"
  378. " -h show this usage\n"
  379. " -d show more debug messages (-dd for even more)\n"
  380. " -B run daemon in the background\n"
  381. " -e entropy file\n"
  382. " -g global control interface path\n"
  383. " -G group for control interfaces\n"
  384. " -P PID file\n"
  385. " -K include key data in debug messages\n"
  386. #ifdef CONFIG_DEBUG_FILE
  387. " -f log output to debug file instead of stdout\n"
  388. #endif /* CONFIG_DEBUG_FILE */
  389. #ifdef CONFIG_DEBUG_LINUX_TRACING
  390. " -T = record to Linux tracing in addition to logging\n"
  391. " (records all messages regardless of debug verbosity)\n"
  392. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  393. " -S start all the interfaces synchronously\n"
  394. " -t include timestamps in some debug messages\n"
  395. " -v show hostapd version\n");
  396. exit(1);
  397. }
  398. static const char * hostapd_msg_ifname_cb(void *ctx)
  399. {
  400. struct hostapd_data *hapd = ctx;
  401. if (hapd && hapd->conf)
  402. return hapd->conf->iface;
  403. return NULL;
  404. }
  405. static int hostapd_get_global_ctrl_iface(struct hapd_interfaces *interfaces,
  406. const char *path)
  407. {
  408. #ifndef CONFIG_CTRL_IFACE_UDP
  409. char *pos;
  410. #endif /* !CONFIG_CTRL_IFACE_UDP */
  411. os_free(interfaces->global_iface_path);
  412. interfaces->global_iface_path = os_strdup(path);
  413. if (interfaces->global_iface_path == NULL)
  414. return -1;
  415. #ifndef CONFIG_CTRL_IFACE_UDP
  416. pos = os_strrchr(interfaces->global_iface_path, '/');
  417. if (pos == NULL) {
  418. wpa_printf(MSG_ERROR, "No '/' in the global control interface "
  419. "file");
  420. os_free(interfaces->global_iface_path);
  421. interfaces->global_iface_path = NULL;
  422. return -1;
  423. }
  424. *pos = '\0';
  425. interfaces->global_iface_name = pos + 1;
  426. #endif /* !CONFIG_CTRL_IFACE_UDP */
  427. return 0;
  428. }
  429. static int hostapd_get_ctrl_iface_group(struct hapd_interfaces *interfaces,
  430. const char *group)
  431. {
  432. #ifndef CONFIG_NATIVE_WINDOWS
  433. struct group *grp;
  434. grp = getgrnam(group);
  435. if (grp == NULL) {
  436. wpa_printf(MSG_ERROR, "Unknown group '%s'", group);
  437. return -1;
  438. }
  439. interfaces->ctrl_iface_group = grp->gr_gid;
  440. #endif /* CONFIG_NATIVE_WINDOWS */
  441. return 0;
  442. }
  443. #ifdef CONFIG_WPS
  444. static int gen_uuid(const char *txt_addr)
  445. {
  446. u8 addr[ETH_ALEN];
  447. u8 uuid[UUID_LEN];
  448. char buf[100];
  449. if (hwaddr_aton(txt_addr, addr) < 0)
  450. return -1;
  451. uuid_gen_mac_addr(addr, uuid);
  452. if (uuid_bin2str(uuid, buf, sizeof(buf)) < 0)
  453. return -1;
  454. printf("%s\n", buf);
  455. return 0;
  456. }
  457. #endif /* CONFIG_WPS */
  458. #ifndef HOSTAPD_CLEANUP_INTERVAL
  459. #define HOSTAPD_CLEANUP_INTERVAL 10
  460. #endif /* HOSTAPD_CLEANUP_INTERVAL */
  461. static int hostapd_periodic_call(struct hostapd_iface *iface, void *ctx)
  462. {
  463. hostapd_periodic_iface(iface);
  464. return 0;
  465. }
  466. /* Periodic cleanup tasks */
  467. static void hostapd_periodic(void *eloop_ctx, void *timeout_ctx)
  468. {
  469. struct hapd_interfaces *interfaces = eloop_ctx;
  470. eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
  471. hostapd_periodic, interfaces, NULL);
  472. hostapd_for_each_interface(interfaces, hostapd_periodic_call, NULL);
  473. }
  474. int main(int argc, char *argv[])
  475. {
  476. struct hapd_interfaces interfaces;
  477. int ret = 1;
  478. size_t i, j;
  479. int c, debug = 0, daemonize = 0;
  480. char *pid_file = NULL;
  481. const char *log_file = NULL;
  482. const char *entropy_file = NULL;
  483. char **bss_config = NULL, **tmp_bss;
  484. size_t num_bss_configs = 0;
  485. #ifdef CONFIG_DEBUG_LINUX_TRACING
  486. int enable_trace_dbg = 0;
  487. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  488. int start_ifaces_in_sync = 0;
  489. if (os_program_init())
  490. return -1;
  491. os_memset(&interfaces, 0, sizeof(interfaces));
  492. interfaces.reload_config = hostapd_reload_config;
  493. interfaces.config_read_cb = hostapd_config_read;
  494. interfaces.for_each_interface = hostapd_for_each_interface;
  495. interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
  496. interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
  497. interfaces.driver_init = hostapd_driver_init;
  498. interfaces.global_iface_path = NULL;
  499. interfaces.global_iface_name = NULL;
  500. interfaces.global_ctrl_sock = -1;
  501. dl_list_init(&interfaces.global_ctrl_dst);
  502. for (;;) {
  503. c = getopt(argc, argv, "b:Bde:f:hKP:STtu:vg:G:");
  504. if (c < 0)
  505. break;
  506. switch (c) {
  507. case 'h':
  508. usage();
  509. break;
  510. case 'd':
  511. debug++;
  512. if (wpa_debug_level > 0)
  513. wpa_debug_level--;
  514. break;
  515. case 'B':
  516. daemonize++;
  517. break;
  518. case 'e':
  519. entropy_file = optarg;
  520. break;
  521. case 'f':
  522. log_file = optarg;
  523. break;
  524. case 'K':
  525. wpa_debug_show_keys++;
  526. break;
  527. case 'P':
  528. os_free(pid_file);
  529. pid_file = os_rel2abs_path(optarg);
  530. break;
  531. case 't':
  532. wpa_debug_timestamp++;
  533. break;
  534. #ifdef CONFIG_DEBUG_LINUX_TRACING
  535. case 'T':
  536. enable_trace_dbg = 1;
  537. break;
  538. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  539. case 'v':
  540. show_version();
  541. exit(1);
  542. break;
  543. case 'g':
  544. if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
  545. return -1;
  546. break;
  547. case 'G':
  548. if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
  549. return -1;
  550. break;
  551. case 'b':
  552. tmp_bss = os_realloc_array(bss_config,
  553. num_bss_configs + 1,
  554. sizeof(char *));
  555. if (tmp_bss == NULL)
  556. goto out;
  557. bss_config = tmp_bss;
  558. bss_config[num_bss_configs++] = optarg;
  559. break;
  560. case 'S':
  561. start_ifaces_in_sync = 1;
  562. break;
  563. #ifdef CONFIG_WPS
  564. case 'u':
  565. return gen_uuid(optarg);
  566. #endif /* CONFIG_WPS */
  567. default:
  568. usage();
  569. break;
  570. }
  571. }
  572. if (optind == argc && interfaces.global_iface_path == NULL &&
  573. num_bss_configs == 0)
  574. usage();
  575. wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
  576. if (log_file)
  577. wpa_debug_open_file(log_file);
  578. else
  579. wpa_debug_setup_stdout();
  580. #ifdef CONFIG_DEBUG_LINUX_TRACING
  581. if (enable_trace_dbg) {
  582. int tret = wpa_debug_open_linux_tracing();
  583. if (tret) {
  584. wpa_printf(MSG_ERROR, "Failed to enable trace logging");
  585. return -1;
  586. }
  587. }
  588. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  589. interfaces.count = argc - optind;
  590. if (interfaces.count || num_bss_configs) {
  591. interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
  592. sizeof(struct hostapd_iface *));
  593. if (interfaces.iface == NULL) {
  594. wpa_printf(MSG_ERROR, "malloc failed");
  595. return -1;
  596. }
  597. }
  598. if (hostapd_global_init(&interfaces, entropy_file)) {
  599. wpa_printf(MSG_ERROR, "Failed to initialize global context");
  600. return -1;
  601. }
  602. eloop_register_timeout(HOSTAPD_CLEANUP_INTERVAL, 0,
  603. hostapd_periodic, &interfaces, NULL);
  604. if (fst_global_init()) {
  605. wpa_printf(MSG_ERROR,
  606. "Failed to initialize global FST context");
  607. goto out;
  608. }
  609. #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
  610. if (!fst_global_add_ctrl(fst_ctrl_cli))
  611. wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
  612. #endif /* CONFIG_FST && CONFIG_CTRL_IFACE */
  613. /* Allocate and parse configuration for full interface files */
  614. for (i = 0; i < interfaces.count; i++) {
  615. interfaces.iface[i] = hostapd_interface_init(&interfaces,
  616. argv[optind + i],
  617. debug);
  618. if (!interfaces.iface[i]) {
  619. wpa_printf(MSG_ERROR, "Failed to initialize interface");
  620. goto out;
  621. }
  622. if (start_ifaces_in_sync)
  623. interfaces.iface[i]->need_to_start_in_sync = 1;
  624. }
  625. /* Allocate and parse configuration for per-BSS files */
  626. for (i = 0; i < num_bss_configs; i++) {
  627. struct hostapd_iface *iface;
  628. char *fname;
  629. wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
  630. fname = os_strchr(bss_config[i], ':');
  631. if (fname == NULL) {
  632. wpa_printf(MSG_ERROR,
  633. "Invalid BSS config identifier '%s'",
  634. bss_config[i]);
  635. goto out;
  636. }
  637. *fname++ = '\0';
  638. iface = hostapd_interface_init_bss(&interfaces, bss_config[i],
  639. fname, debug);
  640. if (iface == NULL)
  641. goto out;
  642. for (j = 0; j < interfaces.count; j++) {
  643. if (interfaces.iface[j] == iface)
  644. break;
  645. }
  646. if (j == interfaces.count) {
  647. struct hostapd_iface **tmp;
  648. tmp = os_realloc_array(interfaces.iface,
  649. interfaces.count + 1,
  650. sizeof(struct hostapd_iface *));
  651. if (tmp == NULL) {
  652. hostapd_interface_deinit_free(iface);
  653. goto out;
  654. }
  655. interfaces.iface = tmp;
  656. interfaces.iface[interfaces.count++] = iface;
  657. }
  658. }
  659. /*
  660. * Enable configured interfaces. Depending on channel configuration,
  661. * this may complete full initialization before returning or use a
  662. * callback mechanism to complete setup in case of operations like HT
  663. * co-ex scans, ACS, or DFS are needed to determine channel parameters.
  664. * In such case, the interface will be enabled from eloop context within
  665. * hostapd_global_run().
  666. */
  667. interfaces.terminate_on_error = interfaces.count;
  668. for (i = 0; i < interfaces.count; i++) {
  669. if (hostapd_driver_init(interfaces.iface[i]) ||
  670. hostapd_setup_interface(interfaces.iface[i]))
  671. goto out;
  672. }
  673. hostapd_global_ctrl_iface_init(&interfaces);
  674. if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
  675. wpa_printf(MSG_ERROR, "Failed to start eloop");
  676. goto out;
  677. }
  678. ret = 0;
  679. out:
  680. hostapd_global_ctrl_iface_deinit(&interfaces);
  681. /* Deinitialize all interfaces */
  682. for (i = 0; i < interfaces.count; i++) {
  683. if (!interfaces.iface[i])
  684. continue;
  685. interfaces.iface[i]->driver_ap_teardown =
  686. !!(interfaces.iface[i]->drv_flags &
  687. WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
  688. hostapd_interface_deinit_free(interfaces.iface[i]);
  689. }
  690. os_free(interfaces.iface);
  691. eloop_cancel_timeout(hostapd_periodic, &interfaces, NULL);
  692. hostapd_global_deinit(pid_file);
  693. os_free(pid_file);
  694. if (log_file)
  695. wpa_debug_close_file();
  696. wpa_debug_close_linux_tracing();
  697. os_free(bss_config);
  698. fst_global_deinit();
  699. os_program_deinit();
  700. return ret;
  701. }