• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt/router/wpa_supplicant/wpa_supplicant/
1/*
2 * WPA Supplicant / main() function for UNIX like OSes and MinGW
3 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16#ifdef __linux__
17#include <fcntl.h>
18#endif /* __linux__ */
19
20#include "common.h"
21#include "wpa_supplicant_i.h"
22
23
24static void usage(void)
25{
26	int i;
27	printf("%s\n\n%s\n"
28	       "usage:\n"
29	       "  wpa_supplicant [-BddhKLqqstuvW] [-P<pid file>] "
30	       "[-g<global ctrl>] \\\n"
31	       "        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
32	       "[-p<driver_param>] \\\n"
33	       "        [-b<br_ifname>] [-f<debug file>] \\\n"
34	       "        [-N -i<ifname> -c<conf> [-C<ctrl>] "
35	       "[-D<driver>] \\\n"
36	       "        [-p<driver_param>] [-b<br_ifname>] ...]\n"
37	       "\n"
38	       "drivers:\n",
39	       wpa_supplicant_version, wpa_supplicant_license);
40
41	for (i = 0; wpa_supplicant_drivers[i]; i++) {
42		printf("  %s = %s\n",
43		       wpa_supplicant_drivers[i]->name,
44		       wpa_supplicant_drivers[i]->desc);
45	}
46
47#ifndef CONFIG_NO_STDOUT_DEBUG
48	printf("options:\n"
49	       "  -b = optional bridge interface name\n"
50	       "  -B = run daemon in the background\n"
51	       "  -c = Configuration file\n"
52	       "  -C = ctrl_interface parameter (only used if -c is not)\n"
53	       "  -i = interface name\n"
54	       "  -d = increase debugging verbosity (-dd even more)\n"
55	       "  -D = driver name\n"
56#ifdef CONFIG_DEBUG_FILE
57	       "  -f = log output to debug file instead of stdout\n"
58#endif /* CONFIG_DEBUG_FILE */
59	       "  -g = global ctrl_interface\n"
60#ifdef CONFIG_DEBUG_SYSLOG
61	       "  -s = log output to syslog instead of stdout\n"
62#endif /* CONFIG_DEBUG_SYSLOG */
63	       "  -K = include keys (passwords, etc.) in debug output\n"
64	       "  -t = include timestamp in debug messages\n"
65	       "  -h = show this help text\n"
66	       "  -L = show license (GPL and BSD)\n");
67	printf("  -p = driver parameters\n"
68	       "  -P = PID file\n"
69	       "  -q = decrease debugging verbosity (-qq even less)\n"
70#ifdef CONFIG_CTRL_IFACE_DBUS
71	       "  -u = enable DBus control interface\n"
72#endif /* CONFIG_CTRL_IFACE_DBUS */
73	       "  -v = show version\n"
74	       "  -W = wait for a control interface monitor before starting\n"
75	       "  -N = start describing new interface\n");
76
77	printf("example:\n"
78	       "  wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf\n");
79#endif /* CONFIG_NO_STDOUT_DEBUG */
80}
81
82
83static void license(void)
84{
85#ifndef CONFIG_NO_STDOUT_DEBUG
86	printf("%s\n\n%s%s%s%s%s\n",
87	       wpa_supplicant_version,
88	       wpa_supplicant_full_license1,
89	       wpa_supplicant_full_license2,
90	       wpa_supplicant_full_license3,
91	       wpa_supplicant_full_license4,
92	       wpa_supplicant_full_license5);
93#endif /* CONFIG_NO_STDOUT_DEBUG */
94}
95
96
97static void wpa_supplicant_fd_workaround(void)
98{
99#ifdef __linux__
100	int s, i;
101	/* When started from pcmcia-cs scripts, wpa_supplicant might start with
102	 * fd 0, 1, and 2 closed. This will cause some issues because many
103	 * places in wpa_supplicant are still printing out to stdout. As a
104	 * workaround, make sure that fd's 0, 1, and 2 are not used for other
105	 * sockets. */
106	for (i = 0; i < 3; i++) {
107		s = open("/dev/null", O_RDWR);
108		if (s > 2) {
109			close(s);
110			break;
111		}
112	}
113#endif /* __linux__ */
114}
115
116
117int main(int argc, char *argv[])
118{
119	int c, i;
120	struct wpa_interface *ifaces, *iface;
121	int iface_count, exitcode = -1;
122	struct wpa_params params;
123	struct wpa_global *global;
124
125	if (os_program_init())
126		return -1;
127
128	os_memset(&params, 0, sizeof(params));
129	params.wpa_debug_level = MSG_INFO;
130
131	iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
132	if (ifaces == NULL)
133		return -1;
134	iface_count = 1;
135
136	wpa_supplicant_fd_workaround();
137
138	for (;;) {
139		c = getopt(argc, argv, "b:Bc:C:D:df:g:hi:KLNp:P:qstuvW");
140		if (c < 0)
141			break;
142		switch (c) {
143		case 'b':
144			iface->bridge_ifname = optarg;
145			break;
146		case 'B':
147			params.daemonize++;
148			break;
149		case 'c':
150			iface->confname = optarg;
151			break;
152		case 'C':
153			iface->ctrl_interface = optarg;
154			break;
155		case 'D':
156			iface->driver = optarg;
157			break;
158		case 'd':
159#ifdef CONFIG_NO_STDOUT_DEBUG
160			printf("Debugging disabled with "
161			       "CONFIG_NO_STDOUT_DEBUG=y build time "
162			       "option.\n");
163			goto out;
164#else /* CONFIG_NO_STDOUT_DEBUG */
165			params.wpa_debug_level--;
166			break;
167#endif /* CONFIG_NO_STDOUT_DEBUG */
168#ifdef CONFIG_DEBUG_FILE
169		case 'f':
170			params.wpa_debug_file_path = optarg;
171			break;
172#endif /* CONFIG_DEBUG_FILE */
173		case 'g':
174			params.ctrl_interface = optarg;
175			break;
176		case 'h':
177			usage();
178			exitcode = 0;
179			goto out;
180		case 'i':
181			iface->ifname = optarg;
182			break;
183		case 'K':
184			params.wpa_debug_show_keys++;
185			break;
186		case 'L':
187			license();
188			exitcode = 0;
189			goto out;
190		case 'p':
191			iface->driver_param = optarg;
192			break;
193		case 'P':
194			os_free(params.pid_file);
195			params.pid_file = os_rel2abs_path(optarg);
196			break;
197		case 'q':
198			params.wpa_debug_level++;
199			break;
200#ifdef CONFIG_DEBUG_SYSLOG
201		case 's':
202			params.wpa_debug_syslog++;
203			break;
204#endif /* CONFIG_DEBUG_SYSLOG */
205		case 't':
206			params.wpa_debug_timestamp++;
207			break;
208#ifdef CONFIG_CTRL_IFACE_DBUS
209		case 'u':
210			params.dbus_ctrl_interface = 1;
211			break;
212#endif /* CONFIG_CTRL_IFACE_DBUS */
213		case 'v':
214			printf("%s\n", wpa_supplicant_version);
215			exitcode = 0;
216			goto out;
217		case 'W':
218			params.wait_for_monitor++;
219			break;
220		case 'N':
221			iface_count++;
222			iface = os_realloc(ifaces, iface_count *
223					   sizeof(struct wpa_interface));
224			if (iface == NULL)
225				goto out;
226			ifaces = iface;
227			iface = &ifaces[iface_count - 1];
228			os_memset(iface, 0, sizeof(*iface));
229			break;
230		default:
231			usage();
232			exitcode = 0;
233			goto out;
234		}
235	}
236
237	exitcode = 0;
238	global = wpa_supplicant_init(&params);
239	if (global == NULL) {
240		wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
241		exitcode = -1;
242		goto out;
243	}
244
245	for (i = 0; exitcode == 0 && i < iface_count; i++) {
246		if ((ifaces[i].confname == NULL &&
247		     ifaces[i].ctrl_interface == NULL) ||
248		    ifaces[i].ifname == NULL) {
249			if (iface_count == 1 && (params.ctrl_interface ||
250						 params.dbus_ctrl_interface))
251				break;
252			usage();
253			exitcode = -1;
254			break;
255		}
256		if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
257			exitcode = -1;
258	}
259
260	if (exitcode == 0)
261		exitcode = wpa_supplicant_run(global);
262
263	wpa_supplicant_deinit(global);
264
265out:
266	os_free(ifaces);
267	os_free(params.pid_file);
268
269	os_program_deinit();
270
271	return exitcode;
272}
273