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