blob: d5d47e1d77b2485ce962177b3c0146429493245f [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"
31#ifdef CONFIG_DBUS
32 "u"
33#endif /* CONFIG_DBUS */
34 "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"
68 " -i = interface name\n"
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -070069 " -I = additional configuration file\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070 " -d = increase debugging verbosity (-dd even more)\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -070071 " -D = driver name (can be multiple drivers: nl80211,wext)\n"
72 " -e = entropy file\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073#ifdef CONFIG_DEBUG_FILE
74 printf(" -f = log output to debug file instead of stdout\n");
75#endif /* CONFIG_DEBUG_FILE */
76 printf(" -g = global ctrl_interface\n"
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070077 " -G = global ctrl_interface group\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078 " -K = include keys (passwords, etc.) in debug output\n");
79#ifdef CONFIG_DEBUG_SYSLOG
80 printf(" -s = log output to syslog instead of stdout\n");
81#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -070082#ifdef CONFIG_DEBUG_LINUX_TRACING
83 printf(" -T = record to Linux tracing in addition to logging\n");
84 printf(" (records all messages regardless of debug verbosity)\n");
85#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086 printf(" -t = include timestamp in debug messages\n"
87 " -h = show this help text\n"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080088 " -L = show license (BSD)\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"
93 " -q = decrease debugging verbosity (-qq even less)\n");
94#ifdef CONFIG_DBUS
95 printf(" -u = enable DBus control interface\n");
96#endif /* CONFIG_DBUS */
97 printf(" -v = show version\n"
98 " -W = wait for a control interface monitor before starting\n"
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -080099#ifdef CONFIG_P2P
100 " -m = Configuration file for the P2P Device interface\n"
101#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102 " -N = start describing new interface\n");
103
104 printf("example:\n"
105 " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700106 wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107#endif /* CONFIG_NO_STDOUT_DEBUG */
108}
109
110
111static void license(void)
112{
113#ifndef CONFIG_NO_STDOUT_DEBUG
114 printf("%s\n\n%s%s%s%s%s\n",
115 wpa_supplicant_version,
116 wpa_supplicant_full_license1,
117 wpa_supplicant_full_license2,
118 wpa_supplicant_full_license3,
119 wpa_supplicant_full_license4,
120 wpa_supplicant_full_license5);
121#endif /* CONFIG_NO_STDOUT_DEBUG */
122}
123
124
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700125static void wpa_supplicant_fd_workaround(int start)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126{
127#ifdef __linux__
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700128 static int fd[3] = { -1, -1, -1 };
129 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 /* When started from pcmcia-cs scripts, wpa_supplicant might start with
131 * fd 0, 1, and 2 closed. This will cause some issues because many
132 * places in wpa_supplicant are still printing out to stdout. As a
133 * workaround, make sure that fd's 0, 1, and 2 are not used for other
134 * sockets. */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700135 if (start) {
136 for (i = 0; i < 3; i++) {
137 fd[i] = open("/dev/null", O_RDWR);
138 if (fd[i] > 2) {
139 close(fd[i]);
140 fd[i] = -1;
141 break;
142 }
143 }
144 } else {
145 for (i = 0; i < 3; i++) {
146 if (fd[i] >= 0) {
147 close(fd[i]);
148 fd[i] = -1;
149 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150 }
151 }
152#endif /* __linux__ */
153}
154
155
156int main(int argc, char *argv[])
157{
158 int c, i;
159 struct wpa_interface *ifaces, *iface;
160 int iface_count, exitcode = -1;
161 struct wpa_params params;
162 struct wpa_global *global;
163
164 if (os_program_init())
165 return -1;
166
167 os_memset(&params, 0, sizeof(params));
168 params.wpa_debug_level = MSG_INFO;
169
170 iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
171 if (ifaces == NULL)
172 return -1;
173 iface_count = 1;
174
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700175 wpa_supplicant_fd_workaround(1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176
177 for (;;) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700178 c = getopt(argc, argv,
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -0800179 "b:Bc:C:D:de:f:g:G:hi:I:KLm:No:O:p:P:qsTtuvW");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700180 if (c < 0)
181 break;
182 switch (c) {
183 case 'b':
184 iface->bridge_ifname = optarg;
185 break;
186 case 'B':
187 params.daemonize++;
188 break;
189 case 'c':
190 iface->confname = optarg;
191 break;
192 case 'C':
193 iface->ctrl_interface = optarg;
194 break;
195 case 'D':
196 iface->driver = optarg;
197 break;
198 case 'd':
199#ifdef CONFIG_NO_STDOUT_DEBUG
200 printf("Debugging disabled with "
201 "CONFIG_NO_STDOUT_DEBUG=y build time "
202 "option.\n");
203 goto out;
204#else /* CONFIG_NO_STDOUT_DEBUG */
205 params.wpa_debug_level--;
206 break;
207#endif /* CONFIG_NO_STDOUT_DEBUG */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700208 case 'e':
209 params.entropy_file = optarg;
210 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211#ifdef CONFIG_DEBUG_FILE
212 case 'f':
213 params.wpa_debug_file_path = optarg;
214 break;
215#endif /* CONFIG_DEBUG_FILE */
216 case 'g':
217 params.ctrl_interface = optarg;
218 break;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700219 case 'G':
220 params.ctrl_interface_group = optarg;
221 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 case 'h':
223 usage();
224 exitcode = 0;
225 goto out;
226 case 'i':
227 iface->ifname = optarg;
228 break;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700229 case 'I':
230 iface->confanother = optarg;
231 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232 case 'K':
233 params.wpa_debug_show_keys++;
234 break;
235 case 'L':
236 license();
237 exitcode = 0;
238 goto out;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -0800239#ifdef CONFIG_P2P
240 case 'm':
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700241 params.conf_p2p_dev = optarg;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -0800242 break;
243#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 case 'o':
245 params.override_driver = optarg;
246 break;
247 case 'O':
248 params.override_ctrl_interface = optarg;
249 break;
250 case 'p':
251 iface->driver_param = optarg;
252 break;
253 case 'P':
254 os_free(params.pid_file);
255 params.pid_file = os_rel2abs_path(optarg);
256 break;
257 case 'q':
258 params.wpa_debug_level++;
259 break;
260#ifdef CONFIG_DEBUG_SYSLOG
261 case 's':
262 params.wpa_debug_syslog++;
263 break;
264#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700265#ifdef CONFIG_DEBUG_LINUX_TRACING
266 case 'T':
267 params.wpa_debug_tracing++;
268 break;
269#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 case 't':
271 params.wpa_debug_timestamp++;
272 break;
273#ifdef CONFIG_DBUS
274 case 'u':
275 params.dbus_ctrl_interface = 1;
276 break;
277#endif /* CONFIG_DBUS */
278 case 'v':
279 printf("%s\n", wpa_supplicant_version);
280 exitcode = 0;
281 goto out;
282 case 'W':
283 params.wait_for_monitor++;
284 break;
285 case 'N':
286 iface_count++;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700287 iface = os_realloc_array(ifaces, iface_count,
288 sizeof(struct wpa_interface));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (iface == NULL)
290 goto out;
291 ifaces = iface;
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700292 iface = &ifaces[iface_count - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 os_memset(iface, 0, sizeof(*iface));
294 break;
295 default:
296 usage();
297 exitcode = 0;
298 goto out;
299 }
300 }
301
302 exitcode = 0;
303 global = wpa_supplicant_init(&params);
304 if (global == NULL) {
305 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
306 exitcode = -1;
307 goto out;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700308 } else {
309 wpa_printf(MSG_INFO, "Successfully initialized "
310 "wpa_supplicant");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 }
312
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800313 if (fst_global_init()) {
314 wpa_printf(MSG_ERROR, "Failed to initialize FST");
315 exitcode = -1;
316 goto out;
317 }
318
319#if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE)
320 if (!fst_global_add_ctrl(fst_ctrl_cli))
321 wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl");
322#endif
323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 for (i = 0; exitcode == 0 && i < iface_count; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700325 struct wpa_supplicant *wpa_s;
326
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 if ((ifaces[i].confname == NULL &&
328 ifaces[i].ctrl_interface == NULL) ||
329 ifaces[i].ifname == NULL) {
330 if (iface_count == 1 && (params.ctrl_interface ||
331 params.dbus_ctrl_interface))
332 break;
333 usage();
334 exitcode = -1;
335 break;
336 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800337 wpa_s = wpa_supplicant_add_iface(global, &ifaces[i], NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700338 if (wpa_s == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 exitcode = -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700340 break;
341 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342 }
343
344 if (exitcode == 0)
345 exitcode = wpa_supplicant_run(global);
346
347 wpa_supplicant_deinit(global);
348
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800349 fst_global_deinit();
350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700352 wpa_supplicant_fd_workaround(0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353 os_free(ifaces);
354 os_free(params.pid_file);
355
356 os_program_deinit();
357
358 return exitcode;
359}