main.c 23 KB

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