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