main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. " -d = increase debugging verbosity (-dd even more)\n"
  63. " -D = driver name (can be multiple drivers: nl80211,wext)\n"
  64. " -e = entropy file\n"
  65. #ifdef CONFIG_DEBUG_FILE
  66. " -f = log output to debug file instead of stdout\n"
  67. #endif /* CONFIG_DEBUG_FILE */
  68. " -g = global ctrl_interface\n"
  69. " -G = global ctrl_interface group\n"
  70. " -h = show this help text\n"
  71. " -i = interface name\n"
  72. " -I = additional configuration file\n"
  73. " -K = include keys (passwords, etc.) in debug output\n"
  74. " -L = show license (BSD)\n"
  75. #ifdef CONFIG_P2P
  76. " -m = Configuration file for the P2P Device interface\n"
  77. #endif /* CONFIG_P2P */
  78. #ifdef CONFIG_MATCH_IFACE
  79. " -M = start describing new matching interface\n"
  80. #endif /* CONFIG_MATCH_IFACE */
  81. " -N = start describing new interface\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_DEBUG_SYSLOG
  88. " -s = log output to syslog instead of stdout\n"
  89. #endif /* CONFIG_DEBUG_SYSLOG */
  90. " -t = include timestamp in debug messages\n"
  91. #ifdef CONFIG_DEBUG_LINUX_TRACING
  92. " -T = record to Linux tracing in addition to logging\n"
  93. " (records all messages regardless of debug verbosity)\n"
  94. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  95. #ifdef CONFIG_DBUS
  96. " -u = enable DBus control interface\n"
  97. #endif /* CONFIG_DBUS */
  98. " -v = show version\n"
  99. " -W = wait for a control interface monitor before starting\n");
  100. printf("example:\n"
  101. " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
  102. wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
  103. #endif /* CONFIG_NO_STDOUT_DEBUG */
  104. }
  105. static void license(void)
  106. {
  107. #ifndef CONFIG_NO_STDOUT_DEBUG
  108. printf("%s\n\n%s%s%s%s%s\n",
  109. wpa_supplicant_version,
  110. wpa_supplicant_full_license1,
  111. wpa_supplicant_full_license2,
  112. wpa_supplicant_full_license3,
  113. wpa_supplicant_full_license4,
  114. wpa_supplicant_full_license5);
  115. #endif /* CONFIG_NO_STDOUT_DEBUG */
  116. }
  117. static void wpa_supplicant_fd_workaround(int start)
  118. {
  119. #ifdef __linux__
  120. static int fd[3] = { -1, -1, -1 };
  121. int i;
  122. /* When started from pcmcia-cs scripts, wpa_supplicant might start with
  123. * fd 0, 1, and 2 closed. This will cause some issues because many
  124. * places in wpa_supplicant are still printing out to stdout. As a
  125. * workaround, make sure that fd's 0, 1, and 2 are not used for other
  126. * sockets. */
  127. if (start) {
  128. for (i = 0; i < 3; i++) {
  129. fd[i] = open("/dev/null", O_RDWR);
  130. if (fd[i] > 2) {
  131. close(fd[i]);
  132. fd[i] = -1;
  133. break;
  134. }
  135. }
  136. } else {
  137. for (i = 0; i < 3; i++) {
  138. if (fd[i] >= 0) {
  139. close(fd[i]);
  140. fd[i] = -1;
  141. }
  142. }
  143. }
  144. #endif /* __linux__ */
  145. }
  146. #ifdef CONFIG_MATCH_IFACE
  147. static int wpa_supplicant_init_match(struct wpa_global *global)
  148. {
  149. /*
  150. * The assumption is that the first driver is the primary driver and
  151. * will handle the arrival / departure of interfaces.
  152. */
  153. if (wpa_drivers[0]->global_init && !global->drv_priv[0]) {
  154. global->drv_priv[0] = wpa_drivers[0]->global_init(global);
  155. if (!global->drv_priv[0]) {
  156. wpa_printf(MSG_ERROR,
  157. "Failed to initialize driver '%s'",
  158. wpa_drivers[0]->name);
  159. return -1;
  160. }
  161. }
  162. return 0;
  163. }
  164. #endif /* CONFIG_MATCH_IFACE */
  165. int main(int argc, char *argv[])
  166. {
  167. int c, i;
  168. struct wpa_interface *ifaces, *iface;
  169. int iface_count, exitcode = -1;
  170. struct wpa_params params;
  171. struct wpa_global *global;
  172. if (os_program_init())
  173. return -1;
  174. os_memset(&params, 0, sizeof(params));
  175. params.wpa_debug_level = MSG_INFO;
  176. iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
  177. if (ifaces == NULL)
  178. return -1;
  179. iface_count = 1;
  180. wpa_supplicant_fd_workaround(1);
  181. for (;;) {
  182. c = getopt(argc, argv,
  183. "b:Bc:C:D:de:f:g:G:hi:I:KLMm:No:O:p:P:qsTtuvW");
  184. if (c < 0)
  185. break;
  186. switch (c) {
  187. case 'b':
  188. iface->bridge_ifname = optarg;
  189. break;
  190. case 'B':
  191. params.daemonize++;
  192. break;
  193. case 'c':
  194. iface->confname = optarg;
  195. break;
  196. case 'C':
  197. iface->ctrl_interface = optarg;
  198. break;
  199. case 'D':
  200. iface->driver = optarg;
  201. break;
  202. case 'd':
  203. #ifdef CONFIG_NO_STDOUT_DEBUG
  204. printf("Debugging disabled with "
  205. "CONFIG_NO_STDOUT_DEBUG=y build time "
  206. "option.\n");
  207. goto out;
  208. #else /* CONFIG_NO_STDOUT_DEBUG */
  209. params.wpa_debug_level--;
  210. break;
  211. #endif /* CONFIG_NO_STDOUT_DEBUG */
  212. case 'e':
  213. params.entropy_file = optarg;
  214. break;
  215. #ifdef CONFIG_DEBUG_FILE
  216. case 'f':
  217. params.wpa_debug_file_path = optarg;
  218. break;
  219. #endif /* CONFIG_DEBUG_FILE */
  220. case 'g':
  221. params.ctrl_interface = optarg;
  222. break;
  223. case 'G':
  224. params.ctrl_interface_group = optarg;
  225. break;
  226. case 'h':
  227. usage();
  228. exitcode = 0;
  229. goto out;
  230. case 'i':
  231. iface->ifname = optarg;
  232. break;
  233. case 'I':
  234. iface->confanother = optarg;
  235. break;
  236. case 'K':
  237. params.wpa_debug_show_keys++;
  238. break;
  239. case 'L':
  240. license();
  241. exitcode = 0;
  242. goto out;
  243. #ifdef CONFIG_P2P
  244. case 'm':
  245. params.conf_p2p_dev = optarg;
  246. break;
  247. #endif /* CONFIG_P2P */
  248. case 'o':
  249. params.override_driver = optarg;
  250. break;
  251. case 'O':
  252. params.override_ctrl_interface = optarg;
  253. break;
  254. case 'p':
  255. iface->driver_param = optarg;
  256. break;
  257. case 'P':
  258. os_free(params.pid_file);
  259. params.pid_file = os_rel2abs_path(optarg);
  260. break;
  261. case 'q':
  262. params.wpa_debug_level++;
  263. break;
  264. #ifdef CONFIG_DEBUG_SYSLOG
  265. case 's':
  266. params.wpa_debug_syslog++;
  267. break;
  268. #endif /* CONFIG_DEBUG_SYSLOG */
  269. #ifdef CONFIG_DEBUG_LINUX_TRACING
  270. case 'T':
  271. params.wpa_debug_tracing++;
  272. break;
  273. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  274. case 't':
  275. params.wpa_debug_timestamp++;
  276. break;
  277. #ifdef CONFIG_DBUS
  278. case 'u':
  279. params.dbus_ctrl_interface = 1;
  280. break;
  281. #endif /* CONFIG_DBUS */
  282. case 'v':
  283. printf("%s\n", wpa_supplicant_version);
  284. exitcode = 0;
  285. goto out;
  286. case 'W':
  287. params.wait_for_monitor++;
  288. break;
  289. #ifdef CONFIG_MATCH_IFACE
  290. case 'M':
  291. params.match_iface_count++;
  292. iface = os_realloc_array(params.match_ifaces,
  293. params.match_iface_count,
  294. sizeof(struct wpa_interface));
  295. if (!iface)
  296. goto out;
  297. params.match_ifaces = iface;
  298. iface = &params.match_ifaces[params.match_iface_count -
  299. 1];
  300. os_memset(iface, 0, sizeof(*iface));
  301. break;
  302. #endif /* CONFIG_MATCH_IFACE */
  303. case 'N':
  304. iface_count++;
  305. iface = os_realloc_array(ifaces, iface_count,
  306. sizeof(struct wpa_interface));
  307. if (iface == NULL)
  308. goto out;
  309. ifaces = iface;
  310. iface = &ifaces[iface_count - 1];
  311. os_memset(iface, 0, sizeof(*iface));
  312. break;
  313. default:
  314. usage();
  315. exitcode = 0;
  316. goto out;
  317. }
  318. }
  319. exitcode = 0;
  320. global = wpa_supplicant_init(&params);
  321. if (global == NULL) {
  322. wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
  323. exitcode = -1;
  324. goto out;
  325. } else {
  326. wpa_printf(MSG_INFO, "Successfully initialized "
  327. "wpa_supplicant");
  328. }
  329. if (fst_global_init()) {
  330. wpa_printf(MSG_ERROR, "Failed to initialize FST");
  331. exitcode = -1;
  332. goto out;
  333. }
  334. #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
  335. if (!fst_global_add_ctrl(fst_ctrl_cli))
  336. wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
  337. #endif
  338. for (i = 0; exitcode == 0 && i < iface_count; i++) {
  339. struct wpa_supplicant *wpa_s;
  340. if ((ifaces[i].confname == NULL &&
  341. ifaces[i].ctrl_interface == NULL) ||
  342. ifaces[i].ifname == NULL) {
  343. if (iface_count == 1 && (params.ctrl_interface ||
  344. #ifdef CONFIG_MATCH_IFACE
  345. params.match_iface_count ||
  346. #endif /* CONFIG_MATCH_IFACE */
  347. params.dbus_ctrl_interface))
  348. break;
  349. usage();
  350. exitcode = -1;
  351. break;
  352. }
  353. wpa_s = wpa_supplicant_add_iface(global, &ifaces[i], NULL);
  354. if (wpa_s == NULL) {
  355. exitcode = -1;
  356. break;
  357. }
  358. }
  359. #ifdef CONFIG_MATCH_IFACE
  360. if (exitcode == 0)
  361. exitcode = wpa_supplicant_init_match(global);
  362. #endif /* CONFIG_MATCH_IFACE */
  363. if (exitcode == 0)
  364. exitcode = wpa_supplicant_run(global);
  365. wpa_supplicant_deinit(global);
  366. fst_global_deinit();
  367. out:
  368. wpa_supplicant_fd_workaround(0);
  369. os_free(ifaces);
  370. #ifdef CONFIG_MATCH_IFACE
  371. os_free(params.match_ifaces);
  372. #endif /* CONFIG_MATCH_IFACE */
  373. os_free(params.pid_file);
  374. os_program_deinit();
  375. return exitcode;
  376. }