]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/main.c
offchannel: Use wpas_get_tx_interface() src parameter more consistently
[thirdparty/hostap.git] / wpa_supplicant / main.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant / main() function for UNIX like OSes and MinGW
29257565 3 * Copyright (c) 2003-2013, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
9#include "includes.h"
10#ifdef __linux__
11#include <fcntl.h>
12#endif /* __linux__ */
13
14#include "common.h"
15#include "wpa_supplicant_i.h"
2d5b792d 16#include "driver_i.h"
c68f6200 17#include "p2p_supplicant.h"
2d5b792d 18
6fc6879b 19
6fc6879b
JM
20static void usage(void)
21{
22 int i;
23 printf("%s\n\n%s\n"
24 "usage:\n"
36bd29ee
DW
25 " wpa_supplicant [-BddhKLqq"
26#ifdef CONFIG_DEBUG_SYSLOG
27 "s"
28#endif /* CONFIG_DEBUG_SYSLOG */
29 "t"
30#ifdef CONFIG_DBUS
31 "u"
32#endif /* CONFIG_DBUS */
33 "vW] [-P<pid file>] "
6fc6879b 34 "[-g<global ctrl>] \\\n"
29257565 35 " [-G<group>] \\\n"
6fc6879b
JM
36 " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
37 "[-p<driver_param>] \\\n"
36bd29ee
DW
38 " [-b<br_ifname>] [-e<entropy file>]"
39#ifdef CONFIG_DEBUG_FILE
40 " [-f<debug file>]"
41#endif /* CONFIG_DEBUG_FILE */
42 " \\\n"
d27df100 43 " [-o<override driver>] [-O<override ctrl>] \\\n"
6fc6879b
JM
44 " [-N -i<ifname> -c<conf> [-C<ctrl>] "
45 "[-D<driver>] \\\n"
c16a7590
IP
46#ifdef CONFIG_P2P
47 " [-m<P2P Device config file>] \\\n"
48#endif /* CONFIG_P2P */
e6304cad
DS
49 " [-p<driver_param>] [-b<br_ifname>] [-I<config file>] "
50 "...]\n"
6fc6879b
JM
51 "\n"
52 "drivers:\n",
53 wpa_supplicant_version, wpa_supplicant_license);
54
c5121837 55 for (i = 0; wpa_drivers[i]; i++) {
6fc6879b 56 printf(" %s = %s\n",
c5121837
JM
57 wpa_drivers[i]->name,
58 wpa_drivers[i]->desc);
6fc6879b
JM
59 }
60
61#ifndef CONFIG_NO_STDOUT_DEBUG
62 printf("options:\n"
63 " -b = optional bridge interface name\n"
64 " -B = run daemon in the background\n"
65 " -c = Configuration file\n"
66 " -C = ctrl_interface parameter (only used if -c is not)\n"
67 " -i = interface name\n"
e6304cad 68 " -I = additional configuration file\n"
6fc6879b 69 " -d = increase debugging verbosity (-dd even more)\n"
38e24575
JM
70 " -D = driver name (can be multiple drivers: nl80211,wext)\n"
71 " -e = entropy file\n");
6fc6879b 72#ifdef CONFIG_DEBUG_FILE
c511c8c6 73 printf(" -f = log output to debug file instead of stdout\n");
6fc6879b 74#endif /* CONFIG_DEBUG_FILE */
c511c8c6 75 printf(" -g = global ctrl_interface\n"
29257565 76 " -G = global ctrl_interface group\n"
daa70d49
SL
77 " -K = include keys (passwords, etc.) in debug output\n");
78#ifdef CONFIG_DEBUG_SYSLOG
79 printf(" -s = log output to syslog instead of stdout\n");
80#endif /* CONFIG_DEBUG_SYSLOG */
4f68895e
JB
81#ifdef CONFIG_DEBUG_LINUX_TRACING
82 printf(" -T = record to Linux tracing in addition to logging\n");
83 printf(" (records all messages regardless of debug verbosity)\n");
84#endif /* CONFIG_DEBUG_LINUX_TRACING */
daa70d49 85 printf(" -t = include timestamp in debug messages\n"
6fc6879b 86 " -h = show this help text\n"
331f89ff 87 " -L = show license (BSD)\n"
d27df100
JM
88 " -o = override driver parameter for new interfaces\n"
89 " -O = override ctrl_interface parameter for new interfaces\n"
c511c8c6 90 " -p = driver parameters\n"
6fc6879b 91 " -P = PID file\n"
c511c8c6 92 " -q = decrease debugging verbosity (-qq even less)\n");
64bde6d7 93#ifdef CONFIG_DBUS
c511c8c6 94 printf(" -u = enable DBus control interface\n");
64bde6d7 95#endif /* CONFIG_DBUS */
c511c8c6 96 printf(" -v = show version\n"
6fc6879b 97 " -W = wait for a control interface monitor before starting\n"
c16a7590
IP
98#ifdef CONFIG_P2P
99 " -m = Configuration file for the P2P Device interface\n"
100#endif /* CONFIG_P2P */
6fc6879b
JM
101 " -N = start describing new interface\n");
102
103 printf("example:\n"
daa70d49 104 " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
b57b5600 105 wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
6fc6879b
JM
106#endif /* CONFIG_NO_STDOUT_DEBUG */
107}
108
109
110static void license(void)
111{
112#ifndef CONFIG_NO_STDOUT_DEBUG
113 printf("%s\n\n%s%s%s%s%s\n",
114 wpa_supplicant_version,
115 wpa_supplicant_full_license1,
116 wpa_supplicant_full_license2,
117 wpa_supplicant_full_license3,
118 wpa_supplicant_full_license4,
119 wpa_supplicant_full_license5);
120#endif /* CONFIG_NO_STDOUT_DEBUG */
121}
122
123
392824ef 124static void wpa_supplicant_fd_workaround(int start)
6fc6879b
JM
125{
126#ifdef __linux__
392824ef
JM
127 static int fd[3] = { -1, -1, -1 };
128 int i;
6fc6879b
JM
129 /* When started from pcmcia-cs scripts, wpa_supplicant might start with
130 * fd 0, 1, and 2 closed. This will cause some issues because many
131 * places in wpa_supplicant are still printing out to stdout. As a
132 * workaround, make sure that fd's 0, 1, and 2 are not used for other
133 * sockets. */
392824ef
JM
134 if (start) {
135 for (i = 0; i < 3; i++) {
136 fd[i] = open("/dev/null", O_RDWR);
137 if (fd[i] > 2) {
138 close(fd[i]);
139 fd[i] = -1;
140 break;
141 }
142 }
143 } else {
144 for (i = 0; i < 3; i++) {
145 if (fd[i] >= 0) {
146 close(fd[i]);
147 fd[i] = -1;
148 }
6fc6879b
JM
149 }
150 }
151#endif /* __linux__ */
152}
153
154
155int main(int argc, char *argv[])
156{
157 int c, i;
158 struct wpa_interface *ifaces, *iface;
159 int iface_count, exitcode = -1;
160 struct wpa_params params;
161 struct wpa_global *global;
162
163 if (os_program_init())
164 return -1;
165
166 os_memset(&params, 0, sizeof(params));
167 params.wpa_debug_level = MSG_INFO;
168
169 iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
170 if (ifaces == NULL)
171 return -1;
172 iface_count = 1;
173
392824ef 174 wpa_supplicant_fd_workaround(1);
6fc6879b
JM
175
176 for (;;) {
4f68895e 177 c = getopt(argc, argv,
c16a7590 178 "b:Bc:C:D:de:f:g:G:hi:I:KLm:No:O:p:P:qsTtuvW");
6fc6879b
JM
179 if (c < 0)
180 break;
181 switch (c) {
182 case 'b':
183 iface->bridge_ifname = optarg;
184 break;
185 case 'B':
186 params.daemonize++;
187 break;
188 case 'c':
189 iface->confname = optarg;
190 break;
191 case 'C':
192 iface->ctrl_interface = optarg;
193 break;
194 case 'D':
195 iface->driver = optarg;
196 break;
197 case 'd':
198#ifdef CONFIG_NO_STDOUT_DEBUG
199 printf("Debugging disabled with "
200 "CONFIG_NO_STDOUT_DEBUG=y build time "
201 "option.\n");
202 goto out;
203#else /* CONFIG_NO_STDOUT_DEBUG */
204 params.wpa_debug_level--;
205 break;
206#endif /* CONFIG_NO_STDOUT_DEBUG */
38e24575
JM
207 case 'e':
208 params.entropy_file = optarg;
209 break;
6fc6879b
JM
210#ifdef CONFIG_DEBUG_FILE
211 case 'f':
212 params.wpa_debug_file_path = optarg;
213 break;
214#endif /* CONFIG_DEBUG_FILE */
215 case 'g':
216 params.ctrl_interface = optarg;
217 break;
29257565
JM
218 case 'G':
219 params.ctrl_interface_group = optarg;
220 break;
6fc6879b
JM
221 case 'h':
222 usage();
223 exitcode = 0;
224 goto out;
225 case 'i':
226 iface->ifname = optarg;
227 break;
e6304cad
DS
228 case 'I':
229 iface->confanother = optarg;
230 break;
6fc6879b
JM
231 case 'K':
232 params.wpa_debug_show_keys++;
233 break;
234 case 'L':
235 license();
236 exitcode = 0;
237 goto out;
c16a7590
IP
238#ifdef CONFIG_P2P
239 case 'm':
240 iface->conf_p2p_dev = optarg;
241 break;
242#endif /* CONFIG_P2P */
d27df100
JM
243 case 'o':
244 params.override_driver = optarg;
245 break;
246 case 'O':
247 params.override_ctrl_interface = optarg;
248 break;
6fc6879b
JM
249 case 'p':
250 iface->driver_param = optarg;
251 break;
252 case 'P':
253 os_free(params.pid_file);
254 params.pid_file = os_rel2abs_path(optarg);
255 break;
256 case 'q':
257 params.wpa_debug_level++;
258 break;
daa70d49
SL
259#ifdef CONFIG_DEBUG_SYSLOG
260 case 's':
261 params.wpa_debug_syslog++;
262 break;
263#endif /* CONFIG_DEBUG_SYSLOG */
4f68895e
JB
264#ifdef CONFIG_DEBUG_LINUX_TRACING
265 case 'T':
266 params.wpa_debug_tracing++;
267 break;
268#endif /* CONFIG_DEBUG_LINUX_TRACING */
6fc6879b
JM
269 case 't':
270 params.wpa_debug_timestamp++;
271 break;
64bde6d7 272#ifdef CONFIG_DBUS
6fc6879b
JM
273 case 'u':
274 params.dbus_ctrl_interface = 1;
275 break;
64bde6d7 276#endif /* CONFIG_DBUS */
6fc6879b
JM
277 case 'v':
278 printf("%s\n", wpa_supplicant_version);
279 exitcode = 0;
280 goto out;
281 case 'W':
282 params.wait_for_monitor++;
283 break;
284 case 'N':
285 iface_count++;
067ffa26
JM
286 iface = os_realloc_array(ifaces, iface_count,
287 sizeof(struct wpa_interface));
6fc6879b
JM
288 if (iface == NULL)
289 goto out;
290 ifaces = iface;
291 iface = &ifaces[iface_count - 1];
292 os_memset(iface, 0, sizeof(*iface));
293 break;
294 default:
295 usage();
296 exitcode = 0;
297 goto out;
298 }
299 }
300
301 exitcode = 0;
302 global = wpa_supplicant_init(&params);
303 if (global == NULL) {
304 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
305 exitcode = -1;
306 goto out;
bae489d2
BG
307 } else {
308 wpa_printf(MSG_INFO, "Successfully initialized "
309 "wpa_supplicant");
6fc6879b
JM
310 }
311
312 for (i = 0; exitcode == 0 && i < iface_count; i++) {
c68f6200
AS
313 struct wpa_supplicant *wpa_s;
314
6fc6879b
JM
315 if ((ifaces[i].confname == NULL &&
316 ifaces[i].ctrl_interface == NULL) ||
317 ifaces[i].ifname == NULL) {
318 if (iface_count == 1 && (params.ctrl_interface ||
319 params.dbus_ctrl_interface))
320 break;
321 usage();
322 exitcode = -1;
323 break;
324 }
c68f6200
AS
325 wpa_s = wpa_supplicant_add_iface(global, &ifaces[i]);
326 if (wpa_s == NULL) {
327 exitcode = -1;
328 break;
329 }
6fc6879b
JM
330 }
331
332 if (exitcode == 0)
333 exitcode = wpa_supplicant_run(global);
334
335 wpa_supplicant_deinit(global);
336
337out:
392824ef 338 wpa_supplicant_fd_workaround(0);
6fc6879b
JM
339 os_free(ifaces);
340 os_free(params.pid_file);
341
342 os_program_deinit();
343
344 return exitcode;
345}