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