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