Set own MAC address in AP data structures
[wpasupplicant] / wpa_supplicant / main.c
1 /*
2  * WPA Supplicant / main() function for UNIX like OSes and MinGW
3  * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16 #ifdef __linux__
17 #include <fcntl.h>
18 #endif /* __linux__ */
19
20 #include "common.h"
21 #include "wpa_supplicant_i.h"
22 #include "driver_i.h"
23
24 extern struct wpa_driver_ops *wpa_supplicant_drivers[];
25
26
27 static void usage(void)
28 {
29         int i;
30         printf("%s\n\n%s\n"
31                "usage:\n"
32                "  wpa_supplicant [-BddhKLqqstuvW] [-P<pid file>] "
33                "[-g<global ctrl>] \\\n"
34                "        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
35                "[-p<driver_param>] \\\n"
36                "        [-b<br_ifname>] [-f<debug file>] \\\n"
37                "        [-N -i<ifname> -c<conf> [-C<ctrl>] "
38                "[-D<driver>] \\\n"
39                "        [-p<driver_param>] [-b<br_ifname>] ...]\n"
40                "\n"
41                "drivers:\n",
42                wpa_supplicant_version, wpa_supplicant_license);
43
44         for (i = 0; wpa_supplicant_drivers[i]; i++) {
45                 printf("  %s = %s\n",
46                        wpa_supplicant_drivers[i]->name,
47                        wpa_supplicant_drivers[i]->desc);
48         }
49
50 #ifndef CONFIG_NO_STDOUT_DEBUG
51         printf("options:\n"
52                "  -b = optional bridge interface name\n"
53                "  -B = run daemon in the background\n"
54                "  -c = Configuration file\n"
55                "  -C = ctrl_interface parameter (only used if -c is not)\n"
56                "  -i = interface name\n"
57                "  -d = increase debugging verbosity (-dd even more)\n"
58                "  -D = driver name (can be multiple drivers: nl80211,wext)\n");
59 #ifdef CONFIG_DEBUG_FILE
60         printf("  -f = log output to debug file instead of stdout\n");
61 #endif /* CONFIG_DEBUG_FILE */
62         printf("  -g = global ctrl_interface\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         printf("  -t = include timestamp in debug messages\n"
68                "  -h = show this help text\n"
69                "  -L = show license (GPL and BSD)\n"
70                "  -p = driver parameters\n"
71                "  -P = PID file\n"
72                "  -q = decrease debugging verbosity (-qq even less)\n");
73 #ifdef CONFIG_CTRL_IFACE_DBUS
74         printf("  -u = enable DBus control interface\n");
75 #endif /* CONFIG_CTRL_IFACE_DBUS */
76         printf("  -v = show version\n"
77                "  -W = wait for a control interface monitor before starting\n"
78                "  -N = start describing new interface\n");
79
80         printf("example:\n"
81                "  wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
82                wpa_supplicant_drivers[i] ?
83                    wpa_supplicant_drivers[i]->name : "wext");
84 #endif /* CONFIG_NO_STDOUT_DEBUG */
85 }
86
87
88 static void license(void)
89 {
90 #ifndef CONFIG_NO_STDOUT_DEBUG
91         printf("%s\n\n%s%s%s%s%s\n",
92                wpa_supplicant_version,
93                wpa_supplicant_full_license1,
94                wpa_supplicant_full_license2,
95                wpa_supplicant_full_license3,
96                wpa_supplicant_full_license4,
97                wpa_supplicant_full_license5);
98 #endif /* CONFIG_NO_STDOUT_DEBUG */
99 }
100
101
102 static void wpa_supplicant_fd_workaround(void)
103 {
104 #ifdef __linux__
105         int s, i;
106         /* When started from pcmcia-cs scripts, wpa_supplicant might start with
107          * fd 0, 1, and 2 closed. This will cause some issues because many
108          * places in wpa_supplicant are still printing out to stdout. As a
109          * workaround, make sure that fd's 0, 1, and 2 are not used for other
110          * sockets. */
111         for (i = 0; i < 3; i++) {
112                 s = open("/dev/null", O_RDWR);
113                 if (s > 2) {
114                         close(s);
115                         break;
116                 }
117         }
118 #endif /* __linux__ */
119 }
120
121
122 int main(int argc, char *argv[])
123 {
124         int c, i;
125         struct wpa_interface *ifaces, *iface;
126         int iface_count, exitcode = -1;
127         struct wpa_params params;
128         struct wpa_global *global;
129
130         if (os_program_init())
131                 return -1;
132
133         os_memset(&params, 0, sizeof(params));
134         params.wpa_debug_level = MSG_INFO;
135
136         iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
137         if (ifaces == NULL)
138                 return -1;
139         iface_count = 1;
140
141         wpa_supplicant_fd_workaround();
142
143         for (;;) {
144                 c = getopt(argc, argv, "b:Bc:C:D:df:g:hi:KLNp:P:qstuvW");
145                 if (c < 0)
146                         break;
147                 switch (c) {
148                 case 'b':
149                         iface->bridge_ifname = optarg;
150                         break;
151                 case 'B':
152                         params.daemonize++;
153                         break;
154                 case 'c':
155                         iface->confname = optarg;
156                         break;
157                 case 'C':
158                         iface->ctrl_interface = optarg;
159                         break;
160                 case 'D':
161                         iface->driver = optarg;
162                         break;
163                 case 'd':
164 #ifdef CONFIG_NO_STDOUT_DEBUG
165                         printf("Debugging disabled with "
166                                "CONFIG_NO_STDOUT_DEBUG=y build time "
167                                "option.\n");
168                         goto out;
169 #else /* CONFIG_NO_STDOUT_DEBUG */
170                         params.wpa_debug_level--;
171                         break;
172 #endif /* CONFIG_NO_STDOUT_DEBUG */
173 #ifdef CONFIG_DEBUG_FILE
174                 case 'f':
175                         params.wpa_debug_file_path = optarg;
176                         break;
177 #endif /* CONFIG_DEBUG_FILE */
178                 case 'g':
179                         params.ctrl_interface = optarg;
180                         break;
181                 case 'h':
182                         usage();
183                         exitcode = 0;
184                         goto out;
185                 case 'i':
186                         iface->ifname = optarg;
187                         break;
188                 case 'K':
189                         params.wpa_debug_show_keys++;
190                         break;
191                 case 'L':
192                         license();
193                         exitcode = 0;
194                         goto out;
195                 case 'p':
196                         iface->driver_param = optarg;
197                         break;
198                 case 'P':
199                         os_free(params.pid_file);
200                         params.pid_file = os_rel2abs_path(optarg);
201                         break;
202                 case 'q':
203                         params.wpa_debug_level++;
204                         break;
205 #ifdef CONFIG_DEBUG_SYSLOG
206                 case 's':
207                         params.wpa_debug_syslog++;
208                         break;
209 #endif /* CONFIG_DEBUG_SYSLOG */
210                 case 't':
211                         params.wpa_debug_timestamp++;
212                         break;
213 #ifdef CONFIG_CTRL_IFACE_DBUS
214                 case 'u':
215                         params.dbus_ctrl_interface = 1;
216                         break;
217 #endif /* CONFIG_CTRL_IFACE_DBUS */
218                 case 'v':
219                         printf("%s\n", wpa_supplicant_version);
220                         exitcode = 0;
221                         goto out;
222                 case 'W':
223                         params.wait_for_monitor++;
224                         break;
225                 case 'N':
226                         iface_count++;
227                         iface = os_realloc(ifaces, iface_count *
228                                            sizeof(struct wpa_interface));
229                         if (iface == NULL)
230                                 goto out;
231                         ifaces = iface;
232                         iface = &ifaces[iface_count - 1]; 
233                         os_memset(iface, 0, sizeof(*iface));
234                         break;
235                 default:
236                         usage();
237                         exitcode = 0;
238                         goto out;
239                 }
240         }
241
242         exitcode = 0;
243         global = wpa_supplicant_init(&params);
244         if (global == NULL) {
245                 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
246                 exitcode = -1;
247                 goto out;
248         }
249
250         for (i = 0; exitcode == 0 && i < iface_count; i++) {
251                 if ((ifaces[i].confname == NULL &&
252                      ifaces[i].ctrl_interface == NULL) ||
253                     ifaces[i].ifname == NULL) {
254                         if (iface_count == 1 && (params.ctrl_interface ||
255                                                  params.dbus_ctrl_interface))
256                                 break;
257                         usage();
258                         exitcode = -1;
259                         break;
260                 }
261                 if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
262                         exitcode = -1;
263         }
264
265         if (exitcode == 0)
266                 exitcode = wpa_supplicant_run(global);
267
268         wpa_supplicant_deinit(global);
269
270 out:
271         os_free(ifaces);
272         os_free(params.pid_file);
273
274         os_program_deinit();
275
276         return exitcode;
277 }