main.c 21 KB

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