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