blob: 517c6bc94bffe6c0d78585d30f830f69327e594b [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / main() function for UNIX like OSes and MinGW
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003 * Copyright (c) 2003-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10#ifdef __linux__
11#include <fcntl.h>
12#endif /* __linux__ */
13
14#include "common.h"
Sunil Ravia04bd252022-05-02 22:54:18 -070015#include "crypto/crypto.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080016#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "wpa_supplicant_i.h"
18#include "driver_i.h"
Dmitry Shmidt34af3062013-07-11 10:46:32 -070019#include "p2p_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021
22static void usage(void)
23{
24 int i;
25 printf("%s\n\n%s\n"
26 "usage:\n"
Dmitry Shmidt18463232014-01-24 12:29:41 -080027 " wpa_supplicant [-BddhKLqq"
28#ifdef CONFIG_DEBUG_SYSLOG
29 "s"
30#endif /* CONFIG_DEBUG_SYSLOG */
31 "t"
Hai Shalom021b0b52019-04-10 11:17:58 -070032#ifdef CONFIG_CTRL_IFACE_DBUS_NEW
Dmitry Shmidt18463232014-01-24 12:29:41 -080033 "u"
Hai Shalom021b0b52019-04-10 11:17:58 -070034#endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
Dmitry Shmidt18463232014-01-24 12:29:41 -080035 "vW] [-P<pid file>] "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036 "[-g<global ctrl>] \\\n"
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070037 " [-G<group>] \\\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038 " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
39 "[-p<driver_param>] \\\n"
Dmitry Shmidt18463232014-01-24 12:29:41 -080040 " [-b<br_ifname>] [-e<entropy file>]"
41#ifdef CONFIG_DEBUG_FILE
42 " [-f<debug file>]"
43#endif /* CONFIG_DEBUG_FILE */
44 " \\\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045 " [-o<override driver>] [-O<override ctrl>] \\\n"
46 " [-N -i<ifname> -c<conf> [-C<ctrl>] "
47 "[-D<driver>] \\\n"
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -080048#ifdef CONFIG_P2P
49 " [-m<P2P Device config file>] \\\n"
50#endif /* CONFIG_P2P */
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -070051 " [-p<driver_param>] [-b<br_ifname>] [-I<config file>] "
52 "...]\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053 "\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"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069 " -d = increase debugging verbosity (-dd even more)\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -070070 " -D = driver name (can be multiple drivers: nl80211,wext)\n"
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080071 " -e = entropy file\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072#ifdef CONFIG_DEBUG_FILE
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080073 " -f = log output to debug file instead of stdout\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080075 " -g = global ctrl_interface\n"
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070076 " -G = global ctrl_interface group\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077 " -h = show this help text\n"
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080078 " -i = interface name\n"
79 " -I = additional configuration file\n"
80 " -K = include keys (passwords, etc.) in debug output\n"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080081 " -L = show license (BSD)\n"
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080082#ifdef CONFIG_P2P
83 " -m = Configuration file for the P2P Device interface\n"
84#endif /* CONFIG_P2P */
Dmitry Shmidte4663042016-04-04 10:07:49 -070085#ifdef CONFIG_MATCH_IFACE
86 " -M = start describing new matching interface\n"
87#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080088 " -N = start describing new interface\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089 " -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"
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080093 " -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 */
Hai Shalom021b0b52019-04-10 11:17:58 -0700102#ifdef CONFIG_CTRL_IFACE_DBUS_NEW
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800103 " -u = enable DBus control interface\n"
Hai Shalom021b0b52019-04-10 11:17:58 -0700104#endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800105 " -v = show version\n"
106 " -W = wait for a control interface monitor before starting\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107
108 printf("example:\n"
109 " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700110 wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111#endif /* CONFIG_NO_STDOUT_DEBUG */
112}
113
114
115static 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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700129static void wpa_supplicant_fd_workaround(int start)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130{
131#ifdef __linux__
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700132 static int fd[3] = { -1, -1, -1 };
133 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134 /* 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. */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700139 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 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154 }
155 }
156#endif /* __linux__ */
157}
158
159
Dmitry Shmidte4663042016-04-04 10:07:49 -0700160#ifdef CONFIG_MATCH_IFACE
161static 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
Gabriel Birenc98b77e2024-11-12 23:40:02 +0000181// 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184int 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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700203 wpa_supplicant_fd_workaround(1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204
205 for (;;) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700206 c = getopt(argc, argv,
Dmitry Shmidte4663042016-04-04 10:07:49 -0700207 "b:Bc:C:D:de:f:g:G:hi:I:KLMm:No:O:p:P:qsTtuvW");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208 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 */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700236 case 'e':
237 params.entropy_file = optarg;
238 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239#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;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700247 case 'G':
248 params.ctrl_interface_group = optarg;
249 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 case 'h':
251 usage();
252 exitcode = 0;
253 goto out;
254 case 'i':
255 iface->ifname = optarg;
256 break;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700257 case 'I':
258 iface->confanother = optarg;
259 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260 case 'K':
261 params.wpa_debug_show_keys++;
262 break;
263 case 'L':
264 license();
265 exitcode = 0;
266 goto out;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -0800267#ifdef CONFIG_P2P
268 case 'm':
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700269 params.conf_p2p_dev = optarg;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -0800270 break;
271#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 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 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700293#ifdef CONFIG_DEBUG_LINUX_TRACING
294 case 'T':
295 params.wpa_debug_tracing++;
296 break;
297#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298 case 't':
299 params.wpa_debug_timestamp++;
300 break;
Hai Shalom021b0b52019-04-10 11:17:58 -0700301#ifdef CONFIG_CTRL_IFACE_DBUS_NEW
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302 case 'u':
303 params.dbus_ctrl_interface = 1;
304 break;
Hai Shalom021b0b52019-04-10 11:17:58 -0700305#endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 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;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700313#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 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 case 'N':
328 iface_count++;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700329 iface = os_realloc_array(ifaces, iface_count,
330 sizeof(struct wpa_interface));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331 if (iface == NULL)
332 goto out;
333 ifaces = iface;
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700334 iface = &ifaces[iface_count - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 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;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700350 } else {
351 wpa_printf(MSG_INFO, "Successfully initialized "
352 "wpa_supplicant");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353 }
354
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800355 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366 for (i = 0; exitcode == 0 && i < iface_count; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700367 struct wpa_supplicant *wpa_s;
368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369 if ((ifaces[i].confname == NULL &&
370 ifaces[i].ctrl_interface == NULL) ||
371 ifaces[i].ifname == NULL) {
372 if (iface_count == 1 && (params.ctrl_interface ||
Dmitry Shmidte4663042016-04-04 10:07:49 -0700373#ifdef CONFIG_MATCH_IFACE
374 params.match_iface_count ||
375#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 params.dbus_ctrl_interface))
377 break;
378 usage();
379 exitcode = -1;
380 break;
381 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800382 wpa_s = wpa_supplicant_add_iface(global, &ifaces[i], NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700383 if (wpa_s == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 exitcode = -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700385 break;
386 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 }
388
Dmitry Shmidte4663042016-04-04 10:07:49 -0700389#ifdef CONFIG_MATCH_IFACE
390 if (exitcode == 0)
391 exitcode = wpa_supplicant_init_match(global);
392#endif /* CONFIG_MATCH_IFACE */
393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 if (exitcode == 0)
395 exitcode = wpa_supplicant_run(global);
396
397 wpa_supplicant_deinit(global);
398
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800399 fst_global_deinit();
400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700402 wpa_supplicant_fd_workaround(0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700403 os_free(ifaces);
Dmitry Shmidte4663042016-04-04 10:07:49 -0700404#ifdef CONFIG_MATCH_IFACE
405 os_free(params.match_ifaces);
406#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407 os_free(params.pid_file);
408
Sunil Ravia04bd252022-05-02 22:54:18 -0700409 crypto_unload();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410 os_program_deinit();
411
412 return exitcode;
413}
Gabriel Birenc98b77e2024-11-12 23:40:02 +0000414#endif /* SUPPLICANT_SERVICE_FUZZER */