main.c 21 KB

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