main.c 22 KB

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