main.c 20 KB

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