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