main.c 8.1 KB

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