main.c 8.6 KB

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