main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * WPA Supplicant / main() function for UNIX like OSes and MinGW
  3. * Copyright (c) 2003-2013, 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 "includes.h"
  9. #ifdef __linux__
  10. #include <fcntl.h>
  11. #endif /* __linux__ */
  12. #include "common.h"
  13. #include "fst/fst.h"
  14. #include "wpa_supplicant_i.h"
  15. #include "driver_i.h"
  16. #include "p2p_supplicant.h"
  17. static void usage(void)
  18. {
  19. int i;
  20. printf("%s\n\n%s\n"
  21. "usage:\n"
  22. " wpa_supplicant [-BddhKLqq"
  23. #ifdef CONFIG_DEBUG_SYSLOG
  24. "s"
  25. #endif /* CONFIG_DEBUG_SYSLOG */
  26. "t"
  27. #ifdef CONFIG_DBUS
  28. "u"
  29. #endif /* CONFIG_DBUS */
  30. "vW] [-P<pid file>] "
  31. "[-g<global ctrl>] \\\n"
  32. " [-G<group>] \\\n"
  33. " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
  34. "[-p<driver_param>] \\\n"
  35. " [-b<br_ifname>] [-e<entropy file>]"
  36. #ifdef CONFIG_DEBUG_FILE
  37. " [-f<debug file>]"
  38. #endif /* CONFIG_DEBUG_FILE */
  39. " \\\n"
  40. " [-o<override driver>] [-O<override ctrl>] \\\n"
  41. " [-N -i<ifname> -c<conf> [-C<ctrl>] "
  42. "[-D<driver>] \\\n"
  43. #ifdef CONFIG_P2P
  44. " [-m<P2P Device config file>] \\\n"
  45. #endif /* CONFIG_P2P */
  46. " [-p<driver_param>] [-b<br_ifname>] [-I<config file>] "
  47. "...]\n"
  48. "\n"
  49. "drivers:\n",
  50. wpa_supplicant_version, wpa_supplicant_license);
  51. for (i = 0; wpa_drivers[i]; i++) {
  52. printf(" %s = %s\n",
  53. wpa_drivers[i]->name,
  54. wpa_drivers[i]->desc);
  55. }
  56. #ifndef CONFIG_NO_STDOUT_DEBUG
  57. printf("options:\n"
  58. " -b = optional bridge interface name\n"
  59. " -B = run daemon in the background\n"
  60. " -c = Configuration file\n"
  61. " -C = ctrl_interface parameter (only used if -c is not)\n"
  62. " -i = interface name\n"
  63. " -I = additional configuration file\n"
  64. " -d = increase debugging verbosity (-dd even more)\n"
  65. " -D = driver name (can be multiple drivers: nl80211,wext)\n"
  66. " -e = entropy file\n");
  67. #ifdef CONFIG_DEBUG_FILE
  68. printf(" -f = log output to debug file instead of stdout\n");
  69. #endif /* CONFIG_DEBUG_FILE */
  70. printf(" -g = global ctrl_interface\n"
  71. " -G = global ctrl_interface group\n"
  72. " -K = include keys (passwords, etc.) in debug output\n");
  73. #ifdef CONFIG_DEBUG_SYSLOG
  74. printf(" -s = log output to syslog instead of stdout\n");
  75. #endif /* CONFIG_DEBUG_SYSLOG */
  76. #ifdef CONFIG_DEBUG_LINUX_TRACING
  77. printf(" -T = record to Linux tracing in addition to logging\n");
  78. printf(" (records all messages regardless of debug verbosity)\n");
  79. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  80. printf(" -t = include timestamp in debug messages\n"
  81. " -h = show this help text\n"
  82. " -L = show license (BSD)\n"
  83. " -o = override driver parameter for new interfaces\n"
  84. " -O = override ctrl_interface parameter for new interfaces\n"
  85. " -p = driver parameters\n"
  86. " -P = PID file\n"
  87. " -q = decrease debugging verbosity (-qq even less)\n");
  88. #ifdef CONFIG_DBUS
  89. printf(" -u = enable DBus control interface\n");
  90. #endif /* CONFIG_DBUS */
  91. printf(" -v = show version\n"
  92. " -W = wait for a control interface monitor before starting\n"
  93. #ifdef CONFIG_P2P
  94. " -m = Configuration file for the P2P Device interface\n"
  95. #endif /* CONFIG_P2P */
  96. " -N = start describing new interface\n");
  97. printf("example:\n"
  98. " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
  99. wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
  100. #endif /* CONFIG_NO_STDOUT_DEBUG */
  101. }
  102. static void license(void)
  103. {
  104. #ifndef CONFIG_NO_STDOUT_DEBUG
  105. printf("%s\n\n%s%s%s%s%s\n",
  106. wpa_supplicant_version,
  107. wpa_supplicant_full_license1,
  108. wpa_supplicant_full_license2,
  109. wpa_supplicant_full_license3,
  110. wpa_supplicant_full_license4,
  111. wpa_supplicant_full_license5);
  112. #endif /* CONFIG_NO_STDOUT_DEBUG */
  113. }
  114. static void wpa_supplicant_fd_workaround(int start)
  115. {
  116. #ifdef __linux__
  117. static int fd[3] = { -1, -1, -1 };
  118. int i;
  119. /* When started from pcmcia-cs scripts, wpa_supplicant might start with
  120. * fd 0, 1, and 2 closed. This will cause some issues because many
  121. * places in wpa_supplicant are still printing out to stdout. As a
  122. * workaround, make sure that fd's 0, 1, and 2 are not used for other
  123. * sockets. */
  124. if (start) {
  125. for (i = 0; i < 3; i++) {
  126. fd[i] = open("/dev/null", O_RDWR);
  127. if (fd[i] > 2) {
  128. close(fd[i]);
  129. fd[i] = -1;
  130. break;
  131. }
  132. }
  133. } else {
  134. for (i = 0; i < 3; i++) {
  135. if (fd[i] >= 0) {
  136. close(fd[i]);
  137. fd[i] = -1;
  138. }
  139. }
  140. }
  141. #endif /* __linux__ */
  142. }
  143. int main(int argc, char *argv[])
  144. {
  145. int c, i;
  146. struct wpa_interface *ifaces, *iface;
  147. int iface_count, exitcode = -1;
  148. struct wpa_params params;
  149. struct wpa_global *global;
  150. if (os_program_init())
  151. return -1;
  152. os_memset(&params, 0, sizeof(params));
  153. params.wpa_debug_level = MSG_INFO;
  154. iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
  155. if (ifaces == NULL)
  156. return -1;
  157. iface_count = 1;
  158. wpa_supplicant_fd_workaround(1);
  159. for (;;) {
  160. c = getopt(argc, argv,
  161. "b:Bc:C:D:de:f:g:G:hi:I:KLm:No:O:p:P:qsTtuvW");
  162. if (c < 0)
  163. break;
  164. switch (c) {
  165. case 'b':
  166. iface->bridge_ifname = optarg;
  167. break;
  168. case 'B':
  169. params.daemonize++;
  170. break;
  171. case 'c':
  172. iface->confname = optarg;
  173. break;
  174. case 'C':
  175. iface->ctrl_interface = optarg;
  176. break;
  177. case 'D':
  178. iface->driver = optarg;
  179. break;
  180. case 'd':
  181. #ifdef CONFIG_NO_STDOUT_DEBUG
  182. printf("Debugging disabled with "
  183. "CONFIG_NO_STDOUT_DEBUG=y build time "
  184. "option.\n");
  185. goto out;
  186. #else /* CONFIG_NO_STDOUT_DEBUG */
  187. params.wpa_debug_level--;
  188. break;
  189. #endif /* CONFIG_NO_STDOUT_DEBUG */
  190. case 'e':
  191. params.entropy_file = optarg;
  192. break;
  193. #ifdef CONFIG_DEBUG_FILE
  194. case 'f':
  195. params.wpa_debug_file_path = optarg;
  196. break;
  197. #endif /* CONFIG_DEBUG_FILE */
  198. case 'g':
  199. params.ctrl_interface = optarg;
  200. break;
  201. case 'G':
  202. params.ctrl_interface_group = optarg;
  203. break;
  204. case 'h':
  205. usage();
  206. exitcode = 0;
  207. goto out;
  208. case 'i':
  209. iface->ifname = optarg;
  210. break;
  211. case 'I':
  212. iface->confanother = optarg;
  213. break;
  214. case 'K':
  215. params.wpa_debug_show_keys++;
  216. break;
  217. case 'L':
  218. license();
  219. exitcode = 0;
  220. goto out;
  221. #ifdef CONFIG_P2P
  222. case 'm':
  223. params.conf_p2p_dev = optarg;
  224. break;
  225. #endif /* CONFIG_P2P */
  226. case 'o':
  227. params.override_driver = optarg;
  228. break;
  229. case 'O':
  230. params.override_ctrl_interface = optarg;
  231. break;
  232. case 'p':
  233. iface->driver_param = optarg;
  234. break;
  235. case 'P':
  236. os_free(params.pid_file);
  237. params.pid_file = os_rel2abs_path(optarg);
  238. break;
  239. case 'q':
  240. params.wpa_debug_level++;
  241. break;
  242. #ifdef CONFIG_DEBUG_SYSLOG
  243. case 's':
  244. params.wpa_debug_syslog++;
  245. break;
  246. #endif /* CONFIG_DEBUG_SYSLOG */
  247. #ifdef CONFIG_DEBUG_LINUX_TRACING
  248. case 'T':
  249. params.wpa_debug_tracing++;
  250. break;
  251. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  252. case 't':
  253. params.wpa_debug_timestamp++;
  254. break;
  255. #ifdef CONFIG_DBUS
  256. case 'u':
  257. params.dbus_ctrl_interface = 1;
  258. break;
  259. #endif /* CONFIG_DBUS */
  260. case 'v':
  261. printf("%s\n", wpa_supplicant_version);
  262. exitcode = 0;
  263. goto out;
  264. case 'W':
  265. params.wait_for_monitor++;
  266. break;
  267. case 'N':
  268. iface_count++;
  269. iface = os_realloc_array(ifaces, iface_count,
  270. sizeof(struct wpa_interface));
  271. if (iface == NULL)
  272. goto out;
  273. ifaces = iface;
  274. iface = &ifaces[iface_count - 1];
  275. os_memset(iface, 0, sizeof(*iface));
  276. break;
  277. default:
  278. usage();
  279. exitcode = 0;
  280. goto out;
  281. }
  282. }
  283. exitcode = 0;
  284. global = wpa_supplicant_init(&params);
  285. if (global == NULL) {
  286. wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
  287. exitcode = -1;
  288. goto out;
  289. } else {
  290. wpa_printf(MSG_INFO, "Successfully initialized "
  291. "wpa_supplicant");
  292. }
  293. if (fst_global_init()) {
  294. wpa_printf(MSG_ERROR, "Failed to initialize FST");
  295. exitcode = -1;
  296. goto out;
  297. }
  298. #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
  299. if (!fst_global_add_ctrl(fst_ctrl_cli))
  300. wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
  301. #endif
  302. for (i = 0; exitcode == 0 && i < iface_count; i++) {
  303. struct wpa_supplicant *wpa_s;
  304. if ((ifaces[i].confname == NULL &&
  305. ifaces[i].ctrl_interface == NULL) ||
  306. ifaces[i].ifname == NULL) {
  307. if (iface_count == 1 && (params.ctrl_interface ||
  308. params.dbus_ctrl_interface))
  309. break;
  310. usage();
  311. exitcode = -1;
  312. break;
  313. }
  314. wpa_s = wpa_supplicant_add_iface(global, &ifaces[i], NULL);
  315. if (wpa_s == NULL) {
  316. exitcode = -1;
  317. break;
  318. }
  319. }
  320. if (exitcode == 0)
  321. exitcode = wpa_supplicant_run(global);
  322. wpa_supplicant_deinit(global);
  323. fst_global_deinit();
  324. out:
  325. wpa_supplicant_fd_workaround(0);
  326. os_free(ifaces);
  327. os_free(params.pid_file);
  328. os_program_deinit();
  329. return exitcode;
  330. }