blob: 51a8a0298a9b8b2ea5a640280501ead931b5cf87 [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"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080015#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "wpa_supplicant_i.h"
17#include "driver_i.h"
Dmitry Shmidt34af3062013-07-11 10:46:32 -070018#include "p2p_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020
21static void usage(void)
22{
23 int i;
24 printf("%s\n\n%s\n"
25 "usage:\n"
Dmitry Shmidt18463232014-01-24 12:29:41 -080026 " wpa_supplicant [-BddhKLqq"
27#ifdef CONFIG_DEBUG_SYSLOG
28 "s"
29#endif /* CONFIG_DEBUG_SYSLOG */
30 "t"
Hai Shalom021b0b52019-04-10 11:17:58 -070031#ifdef CONFIG_CTRL_IFACE_DBUS_NEW
Dmitry Shmidt18463232014-01-24 12:29:41 -080032 "u"
Hai Shalom021b0b52019-04-10 11:17:58 -070033#endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
Dmitry Shmidt18463232014-01-24 12:29:41 -080034 "vW] [-P<pid file>] "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035 "[-g<global ctrl>] \\\n"
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070036 " [-G<group>] \\\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037 " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
38 "[-p<driver_param>] \\\n"
Dmitry Shmidt18463232014-01-24 12:29:41 -080039 " [-b<br_ifname>] [-e<entropy file>]"
40#ifdef CONFIG_DEBUG_FILE
41 " [-f<debug file>]"
42#endif /* CONFIG_DEBUG_FILE */
43 " \\\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044 " [-o<override driver>] [-O<override ctrl>] \\\n"
45 " [-N -i<ifname> -c<conf> [-C<ctrl>] "
46 "[-D<driver>] \\\n"
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -080047#ifdef CONFIG_P2P
48 " [-m<P2P Device config file>] \\\n"
49#endif /* CONFIG_P2P */
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -070050 " [-p<driver_param>] [-b<br_ifname>] [-I<config file>] "
51 "...]\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052 "\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"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068 " -d = increase debugging verbosity (-dd even more)\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -070069 " -D = driver name (can be multiple drivers: nl80211,wext)\n"
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080070 " -e = entropy file\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070071#ifdef CONFIG_DEBUG_FILE
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080072 " -f = log output to debug file instead of stdout\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073#endif /* CONFIG_DEBUG_FILE */
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080074 " -g = global ctrl_interface\n"
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070075 " -G = global ctrl_interface group\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070076 " -h = show this help text\n"
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080077 " -i = interface name\n"
78 " -I = additional configuration file\n"
79 " -K = include keys (passwords, etc.) in debug output\n"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080080 " -L = show license (BSD)\n"
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080081#ifdef CONFIG_P2P
82 " -m = Configuration file for the P2P Device interface\n"
83#endif /* CONFIG_P2P */
Dmitry Shmidte4663042016-04-04 10:07:49 -070084#ifdef CONFIG_MATCH_IFACE
85 " -M = start describing new matching interface\n"
86#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080087 " -N = start describing new interface\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088 " -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"
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080092 " -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 */
Hai Shalom021b0b52019-04-10 11:17:58 -0700101#ifdef CONFIG_CTRL_IFACE_DBUS_NEW
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800102 " -u = enable DBus control interface\n"
Hai Shalom021b0b52019-04-10 11:17:58 -0700103#endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800104 " -v = show version\n"
105 " -W = wait for a control interface monitor before starting\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106
107 printf("example:\n"
108 " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700109 wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110#endif /* CONFIG_NO_STDOUT_DEBUG */
111}
112
113
114static 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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700128static void wpa_supplicant_fd_workaround(int start)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129{
130#ifdef __linux__
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700131 static int fd[3] = { -1, -1, -1 };
132 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 /* 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. */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700138 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 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 }
154 }
155#endif /* __linux__ */
156}
157
158
Dmitry Shmidte4663042016-04-04 10:07:49 -0700159#ifdef CONFIG_MATCH_IFACE
160static 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181int 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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700200 wpa_supplicant_fd_workaround(1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201
202 for (;;) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700203 c = getopt(argc, argv,
Dmitry Shmidte4663042016-04-04 10:07:49 -0700204 "b:Bc:C:D:de:f:g:G:hi:I:KLMm:No:O:p:P:qsTtuvW");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 if (c < 0)
206 break;
207 switch (c) {
208 case 'b':
209 iface->bridge_ifname = optarg;
210 break;
211 case 'B':
212 params.daemonize++;
213 break;
214 case 'c':
215 iface->confname = optarg;
216 break;
217 case 'C':
218 iface->ctrl_interface = optarg;
219 break;
220 case 'D':
221 iface->driver = optarg;
222 break;
223 case 'd':
224#ifdef CONFIG_NO_STDOUT_DEBUG
225 printf("Debugging disabled with "
226 "CONFIG_NO_STDOUT_DEBUG=y build time "
227 "option.\n");
228 goto out;
229#else /* CONFIG_NO_STDOUT_DEBUG */
230 params.wpa_debug_level--;
231 break;
232#endif /* CONFIG_NO_STDOUT_DEBUG */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700233 case 'e':
234 params.entropy_file = optarg;
235 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236#ifdef CONFIG_DEBUG_FILE
237 case 'f':
238 params.wpa_debug_file_path = optarg;
239 break;
240#endif /* CONFIG_DEBUG_FILE */
241 case 'g':
242 params.ctrl_interface = optarg;
243 break;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700244 case 'G':
245 params.ctrl_interface_group = optarg;
246 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247 case 'h':
248 usage();
249 exitcode = 0;
250 goto out;
251 case 'i':
252 iface->ifname = optarg;
253 break;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700254 case 'I':
255 iface->confanother = optarg;
256 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257 case 'K':
258 params.wpa_debug_show_keys++;
259 break;
260 case 'L':
261 license();
262 exitcode = 0;
263 goto out;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -0800264#ifdef CONFIG_P2P
265 case 'm':
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700266 params.conf_p2p_dev = optarg;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -0800267 break;
268#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 case 'o':
270 params.override_driver = optarg;
271 break;
272 case 'O':
273 params.override_ctrl_interface = optarg;
274 break;
275 case 'p':
276 iface->driver_param = optarg;
277 break;
278 case 'P':
279 os_free(params.pid_file);
280 params.pid_file = os_rel2abs_path(optarg);
281 break;
282 case 'q':
283 params.wpa_debug_level++;
284 break;
285#ifdef CONFIG_DEBUG_SYSLOG
286 case 's':
287 params.wpa_debug_syslog++;
288 break;
289#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700290#ifdef CONFIG_DEBUG_LINUX_TRACING
291 case 'T':
292 params.wpa_debug_tracing++;
293 break;
294#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 case 't':
296 params.wpa_debug_timestamp++;
297 break;
Hai Shalom021b0b52019-04-10 11:17:58 -0700298#ifdef CONFIG_CTRL_IFACE_DBUS_NEW
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 case 'u':
300 params.dbus_ctrl_interface = 1;
301 break;
Hai Shalom021b0b52019-04-10 11:17:58 -0700302#endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 case 'v':
304 printf("%s\n", wpa_supplicant_version);
305 exitcode = 0;
306 goto out;
307 case 'W':
308 params.wait_for_monitor++;
309 break;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700310#ifdef CONFIG_MATCH_IFACE
311 case 'M':
312 params.match_iface_count++;
313 iface = os_realloc_array(params.match_ifaces,
314 params.match_iface_count,
315 sizeof(struct wpa_interface));
316 if (!iface)
317 goto out;
318 params.match_ifaces = iface;
319 iface = &params.match_ifaces[params.match_iface_count -
320 1];
321 os_memset(iface, 0, sizeof(*iface));
322 break;
323#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 case 'N':
325 iface_count++;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700326 iface = os_realloc_array(ifaces, iface_count,
327 sizeof(struct wpa_interface));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 if (iface == NULL)
329 goto out;
330 ifaces = iface;
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700331 iface = &ifaces[iface_count - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332 os_memset(iface, 0, sizeof(*iface));
333 break;
334 default:
335 usage();
336 exitcode = 0;
337 goto out;
338 }
339 }
340
341 exitcode = 0;
342 global = wpa_supplicant_init(&params);
343 if (global == NULL) {
344 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
345 exitcode = -1;
346 goto out;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700347 } else {
348 wpa_printf(MSG_INFO, "Successfully initialized "
349 "wpa_supplicant");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 }
351
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800352 if (fst_global_init()) {
353 wpa_printf(MSG_ERROR, "Failed to initialize FST");
354 exitcode = -1;
355 goto out;
356 }
357
358#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
359 if (!fst_global_add_ctrl(fst_ctrl_cli))
360 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
361#endif
362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 for (i = 0; exitcode == 0 && i < iface_count; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700364 struct wpa_supplicant *wpa_s;
365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366 if ((ifaces[i].confname == NULL &&
367 ifaces[i].ctrl_interface == NULL) ||
368 ifaces[i].ifname == NULL) {
369 if (iface_count == 1 && (params.ctrl_interface ||
Dmitry Shmidte4663042016-04-04 10:07:49 -0700370#ifdef CONFIG_MATCH_IFACE
371 params.match_iface_count ||
372#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 params.dbus_ctrl_interface))
374 break;
375 usage();
376 exitcode = -1;
377 break;
378 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800379 wpa_s = wpa_supplicant_add_iface(global, &ifaces[i], NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700380 if (wpa_s == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 exitcode = -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700382 break;
383 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 }
385
Dmitry Shmidte4663042016-04-04 10:07:49 -0700386#ifdef CONFIG_MATCH_IFACE
387 if (exitcode == 0)
388 exitcode = wpa_supplicant_init_match(global);
389#endif /* CONFIG_MATCH_IFACE */
390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 if (exitcode == 0)
392 exitcode = wpa_supplicant_run(global);
393
394 wpa_supplicant_deinit(global);
395
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800396 fst_global_deinit();
397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700399 wpa_supplicant_fd_workaround(0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400 os_free(ifaces);
Dmitry Shmidte4663042016-04-04 10:07:49 -0700401#ifdef CONFIG_MATCH_IFACE
402 os_free(params.match_ifaces);
403#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 os_free(params.pid_file);
405
406 os_program_deinit();
407
408 return exitcode;
409}