1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * WPA Supplicant / main() function for UNIX like OSes and MinGW |
---|
5 | * Copyright (c) 2003-2013, Jouni Malinen <j@w1.fi> |
---|
6 | * |
---|
7 | * This software may be distributed under the terms of the BSD license. |
---|
8 | * See README for more details. |
---|
9 | */ |
---|
10 | |
---|
11 | #include "includes.h" |
---|
12 | #ifdef __linux__ |
---|
13 | #include <fcntl.h> |
---|
14 | #endif /* __linux__ */ |
---|
15 | |
---|
16 | #include "common.h" |
---|
17 | #include "fst/fst.h" |
---|
18 | #include "wpa_supplicant_i.h" |
---|
19 | #include "driver_i.h" |
---|
20 | #include "p2p_supplicant.h" |
---|
21 | |
---|
22 | #ifdef __rtems__ |
---|
23 | #include <assert.h> |
---|
24 | #include <sys/mutex.h> |
---|
25 | #define RTEMS_BSD_PROGRAM_NO_MALLOC_WRAP |
---|
26 | #define RTEMS_BSD_PROGRAM_NO_STRDUP_WRAP |
---|
27 | #include <machine/rtems-bsd-program.h> |
---|
28 | #endif /* __rtems__ */ |
---|
29 | |
---|
30 | static void usage(void) |
---|
31 | { |
---|
32 | int i; |
---|
33 | printf("%s\n\n%s\n" |
---|
34 | "usage:\n" |
---|
35 | " wpa_supplicant [-BddhKLqq" |
---|
36 | #ifdef CONFIG_DEBUG_SYSLOG |
---|
37 | "s" |
---|
38 | #endif /* CONFIG_DEBUG_SYSLOG */ |
---|
39 | "t" |
---|
40 | #ifdef CONFIG_DBUS |
---|
41 | "u" |
---|
42 | #endif /* CONFIG_DBUS */ |
---|
43 | "vW] [-P<pid file>] " |
---|
44 | "[-g<global ctrl>] \\\n" |
---|
45 | " [-G<group>] \\\n" |
---|
46 | " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] " |
---|
47 | "[-p<driver_param>] \\\n" |
---|
48 | " [-b<br_ifname>] [-e<entropy file>]" |
---|
49 | #ifdef CONFIG_DEBUG_FILE |
---|
50 | " [-f<debug file>]" |
---|
51 | #endif /* CONFIG_DEBUG_FILE */ |
---|
52 | " \\\n" |
---|
53 | " [-o<override driver>] [-O<override ctrl>] \\\n" |
---|
54 | " [-N -i<ifname> -c<conf> [-C<ctrl>] " |
---|
55 | "[-D<driver>] \\\n" |
---|
56 | #ifdef CONFIG_P2P |
---|
57 | " [-m<P2P Device config file>] \\\n" |
---|
58 | #endif /* CONFIG_P2P */ |
---|
59 | " [-p<driver_param>] [-b<br_ifname>] [-I<config file>] " |
---|
60 | "...]\n" |
---|
61 | "\n" |
---|
62 | "drivers:\n", |
---|
63 | wpa_supplicant_version, wpa_supplicant_license); |
---|
64 | |
---|
65 | for (i = 0; wpa_drivers[i]; i++) { |
---|
66 | printf(" %s = %s\n", |
---|
67 | wpa_drivers[i]->name, |
---|
68 | wpa_drivers[i]->desc); |
---|
69 | } |
---|
70 | |
---|
71 | #ifndef CONFIG_NO_STDOUT_DEBUG |
---|
72 | printf("options:\n" |
---|
73 | " -b = optional bridge interface name\n" |
---|
74 | " -B = run daemon in the background\n" |
---|
75 | " -c = Configuration file\n" |
---|
76 | " -C = ctrl_interface parameter (only used if -c is not)\n" |
---|
77 | " -i = interface name\n" |
---|
78 | " -I = additional configuration file\n" |
---|
79 | " -d = increase debugging verbosity (-dd even more)\n" |
---|
80 | " -D = driver name (can be multiple drivers: nl80211,wext)\n" |
---|
81 | " -e = entropy file\n"); |
---|
82 | #ifdef CONFIG_DEBUG_FILE |
---|
83 | printf(" -f = log output to debug file instead of stdout\n"); |
---|
84 | #endif /* CONFIG_DEBUG_FILE */ |
---|
85 | printf(" -g = global ctrl_interface\n" |
---|
86 | " -G = global ctrl_interface group\n" |
---|
87 | " -K = include keys (passwords, etc.) in debug output\n"); |
---|
88 | #ifdef CONFIG_DEBUG_SYSLOG |
---|
89 | printf(" -s = log output to syslog instead of stdout\n"); |
---|
90 | #endif /* CONFIG_DEBUG_SYSLOG */ |
---|
91 | #ifdef CONFIG_DEBUG_LINUX_TRACING |
---|
92 | printf(" -T = record to Linux tracing in addition to logging\n"); |
---|
93 | printf(" (records all messages regardless of debug verbosity)\n"); |
---|
94 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ |
---|
95 | printf(" -t = include timestamp in debug messages\n" |
---|
96 | " -h = show this help text\n" |
---|
97 | " -L = show license (BSD)\n" |
---|
98 | " -o = override driver parameter for new interfaces\n" |
---|
99 | " -O = override ctrl_interface parameter for new interfaces\n" |
---|
100 | " -p = driver parameters\n" |
---|
101 | " -P = PID file\n" |
---|
102 | " -q = decrease debugging verbosity (-qq even less)\n"); |
---|
103 | #ifdef CONFIG_DBUS |
---|
104 | printf(" -u = enable DBus control interface\n"); |
---|
105 | #endif /* CONFIG_DBUS */ |
---|
106 | printf(" -v = show version\n" |
---|
107 | " -W = wait for a control interface monitor before starting\n" |
---|
108 | #ifdef CONFIG_P2P |
---|
109 | " -m = Configuration file for the P2P Device interface\n" |
---|
110 | #endif /* CONFIG_P2P */ |
---|
111 | " -N = start describing new interface\n"); |
---|
112 | |
---|
113 | printf("example:\n" |
---|
114 | " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n", |
---|
115 | wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211"); |
---|
116 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | static void license(void) |
---|
121 | { |
---|
122 | #ifndef CONFIG_NO_STDOUT_DEBUG |
---|
123 | printf("%s\n\n%s%s%s%s%s\n", |
---|
124 | wpa_supplicant_version, |
---|
125 | wpa_supplicant_full_license1, |
---|
126 | wpa_supplicant_full_license2, |
---|
127 | wpa_supplicant_full_license3, |
---|
128 | wpa_supplicant_full_license4, |
---|
129 | wpa_supplicant_full_license5); |
---|
130 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | static void wpa_supplicant_fd_workaround(int start) |
---|
135 | { |
---|
136 | #ifdef __linux__ |
---|
137 | static int fd[3] = { -1, -1, -1 }; |
---|
138 | int i; |
---|
139 | /* When started from pcmcia-cs scripts, wpa_supplicant might start with |
---|
140 | * fd 0, 1, and 2 closed. This will cause some issues because many |
---|
141 | * places in wpa_supplicant are still printing out to stdout. As a |
---|
142 | * workaround, make sure that fd's 0, 1, and 2 are not used for other |
---|
143 | * sockets. */ |
---|
144 | if (start) { |
---|
145 | for (i = 0; i < 3; i++) { |
---|
146 | fd[i] = open("/dev/null", O_RDWR); |
---|
147 | if (fd[i] > 2) { |
---|
148 | close(fd[i]); |
---|
149 | fd[i] = -1; |
---|
150 | break; |
---|
151 | } |
---|
152 | } |
---|
153 | } else { |
---|
154 | for (i = 0; i < 3; i++) { |
---|
155 | if (fd[i] >= 0) { |
---|
156 | close(fd[i]); |
---|
157 | fd[i] = -1; |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | #endif /* __linux__ */ |
---|
162 | } |
---|
163 | |
---|
164 | #ifdef __rtems__ |
---|
165 | #include <rtems/libio.h> |
---|
166 | |
---|
167 | static int |
---|
168 | main(int argc, char **argv); |
---|
169 | |
---|
170 | int rtems_bsd_command_wpa_supplicant(int argc, char **argv) |
---|
171 | { |
---|
172 | int exit_code; |
---|
173 | rtems_status_code sc; |
---|
174 | |
---|
175 | exit_code = rtems_bsd_program_call_main("wpa_supplicant", main, argc, argv); |
---|
176 | |
---|
177 | return exit_code; |
---|
178 | } |
---|
179 | |
---|
180 | struct myparams { |
---|
181 | int argc; |
---|
182 | char ** argv; |
---|
183 | }; |
---|
184 | |
---|
185 | static void |
---|
186 | new_wpa_supplicant_task(rtems_task_argument arg) |
---|
187 | { |
---|
188 | int argc; |
---|
189 | char ** argv; |
---|
190 | int i; |
---|
191 | |
---|
192 | struct myparams *params = (struct myparams *)arg; |
---|
193 | argc = params->argc; |
---|
194 | argv = params->argv; |
---|
195 | |
---|
196 | rtems_bsd_command_wpa_supplicant(argc, argv); |
---|
197 | |
---|
198 | for (i = 0; i < params->argc; i++) { |
---|
199 | free(params->argv[i]); |
---|
200 | } |
---|
201 | free(params->argv); |
---|
202 | free(params); |
---|
203 | |
---|
204 | rtems_task_delete( RTEMS_SELF ); |
---|
205 | } |
---|
206 | |
---|
207 | int rtems_bsd_command_wpa_supplicant_fork(int argc, char **argv) |
---|
208 | { |
---|
209 | rtems_status_code sc; |
---|
210 | rtems_id id; |
---|
211 | int i; |
---|
212 | |
---|
213 | struct myparams *params = malloc(sizeof(struct myparams)); |
---|
214 | if (params == NULL) |
---|
215 | return NULL; |
---|
216 | |
---|
217 | params->argc = argc; |
---|
218 | params->argv = malloc((argc + 1) * sizeof(argv[0])); |
---|
219 | if (params->argv == NULL) |
---|
220 | return NULL; |
---|
221 | |
---|
222 | for (i = 0; i < argc; i++) { |
---|
223 | params->argv[i] = strdup(argv[i]); |
---|
224 | if (params->argv[i] == NULL) |
---|
225 | return NULL; |
---|
226 | } |
---|
227 | params->argv[argc] = NULL; |
---|
228 | |
---|
229 | sc = rtems_task_create( |
---|
230 | rtems_build_name('W', 'P', 'A', 'S'), |
---|
231 | RTEMS_MAXIMUM_PRIORITY - 1, |
---|
232 | 8 * RTEMS_MINIMUM_STACK_SIZE, |
---|
233 | RTEMS_DEFAULT_MODES, |
---|
234 | RTEMS_FLOATING_POINT, |
---|
235 | &id |
---|
236 | ); |
---|
237 | assert(sc == RTEMS_SUCCESSFUL); |
---|
238 | |
---|
239 | sc = rtems_task_start(id, new_wpa_supplicant_task, params); |
---|
240 | assert(sc == RTEMS_SUCCESSFUL); |
---|
241 | } |
---|
242 | #endif /* __rtems__ */ |
---|
243 | |
---|
244 | int main(int argc, char *argv[]) |
---|
245 | { |
---|
246 | int c, i; |
---|
247 | struct wpa_interface *ifaces, *iface; |
---|
248 | int iface_count, exitcode = -1; |
---|
249 | struct wpa_params params; |
---|
250 | struct wpa_global *global; |
---|
251 | |
---|
252 | if (os_program_init()) |
---|
253 | return -1; |
---|
254 | |
---|
255 | os_memset(¶ms, 0, sizeof(params)); |
---|
256 | params.wpa_debug_level = MSG_INFO; |
---|
257 | |
---|
258 | iface = ifaces = os_zalloc(sizeof(struct wpa_interface)); |
---|
259 | if (ifaces == NULL) |
---|
260 | return -1; |
---|
261 | iface_count = 1; |
---|
262 | |
---|
263 | wpa_supplicant_fd_workaround(1); |
---|
264 | |
---|
265 | for (;;) { |
---|
266 | c = getopt(argc, argv, |
---|
267 | "b:Bc:C:D:de:f:g:G:hi:I:KLm:No:O:p:P:qsTtuvW"); |
---|
268 | if (c < 0) |
---|
269 | break; |
---|
270 | switch (c) { |
---|
271 | case 'b': |
---|
272 | iface->bridge_ifname = optarg; |
---|
273 | break; |
---|
274 | case 'B': |
---|
275 | params.daemonize++; |
---|
276 | break; |
---|
277 | case 'c': |
---|
278 | iface->confname = optarg; |
---|
279 | break; |
---|
280 | case 'C': |
---|
281 | iface->ctrl_interface = optarg; |
---|
282 | break; |
---|
283 | case 'D': |
---|
284 | iface->driver = optarg; |
---|
285 | break; |
---|
286 | case 'd': |
---|
287 | #ifdef CONFIG_NO_STDOUT_DEBUG |
---|
288 | printf("Debugging disabled with " |
---|
289 | "CONFIG_NO_STDOUT_DEBUG=y build time " |
---|
290 | "option.\n"); |
---|
291 | goto out; |
---|
292 | #else /* CONFIG_NO_STDOUT_DEBUG */ |
---|
293 | params.wpa_debug_level--; |
---|
294 | break; |
---|
295 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
---|
296 | case 'e': |
---|
297 | params.entropy_file = optarg; |
---|
298 | break; |
---|
299 | #ifdef CONFIG_DEBUG_FILE |
---|
300 | case 'f': |
---|
301 | params.wpa_debug_file_path = optarg; |
---|
302 | break; |
---|
303 | #endif /* CONFIG_DEBUG_FILE */ |
---|
304 | case 'g': |
---|
305 | params.ctrl_interface = optarg; |
---|
306 | break; |
---|
307 | case 'G': |
---|
308 | params.ctrl_interface_group = optarg; |
---|
309 | break; |
---|
310 | case 'h': |
---|
311 | usage(); |
---|
312 | exitcode = 0; |
---|
313 | goto out; |
---|
314 | case 'i': |
---|
315 | iface->ifname = optarg; |
---|
316 | break; |
---|
317 | case 'I': |
---|
318 | iface->confanother = optarg; |
---|
319 | break; |
---|
320 | case 'K': |
---|
321 | params.wpa_debug_show_keys++; |
---|
322 | break; |
---|
323 | case 'L': |
---|
324 | license(); |
---|
325 | exitcode = 0; |
---|
326 | goto out; |
---|
327 | #ifdef CONFIG_P2P |
---|
328 | case 'm': |
---|
329 | params.conf_p2p_dev = optarg; |
---|
330 | break; |
---|
331 | #endif /* CONFIG_P2P */ |
---|
332 | case 'o': |
---|
333 | params.override_driver = optarg; |
---|
334 | break; |
---|
335 | case 'O': |
---|
336 | params.override_ctrl_interface = optarg; |
---|
337 | break; |
---|
338 | case 'p': |
---|
339 | iface->driver_param = optarg; |
---|
340 | break; |
---|
341 | case 'P': |
---|
342 | os_free(params.pid_file); |
---|
343 | params.pid_file = os_rel2abs_path(optarg); |
---|
344 | break; |
---|
345 | case 'q': |
---|
346 | params.wpa_debug_level++; |
---|
347 | break; |
---|
348 | #ifdef CONFIG_DEBUG_SYSLOG |
---|
349 | case 's': |
---|
350 | params.wpa_debug_syslog++; |
---|
351 | break; |
---|
352 | #endif /* CONFIG_DEBUG_SYSLOG */ |
---|
353 | #ifdef CONFIG_DEBUG_LINUX_TRACING |
---|
354 | case 'T': |
---|
355 | params.wpa_debug_tracing++; |
---|
356 | break; |
---|
357 | #endif /* CONFIG_DEBUG_LINUX_TRACING */ |
---|
358 | case 't': |
---|
359 | params.wpa_debug_timestamp++; |
---|
360 | break; |
---|
361 | #ifdef CONFIG_DBUS |
---|
362 | case 'u': |
---|
363 | params.dbus_ctrl_interface = 1; |
---|
364 | break; |
---|
365 | #endif /* CONFIG_DBUS */ |
---|
366 | case 'v': |
---|
367 | printf("%s\n", wpa_supplicant_version); |
---|
368 | exitcode = 0; |
---|
369 | goto out; |
---|
370 | case 'W': |
---|
371 | params.wait_for_monitor++; |
---|
372 | break; |
---|
373 | case 'N': |
---|
374 | iface_count++; |
---|
375 | iface = os_realloc_array(ifaces, iface_count, |
---|
376 | sizeof(struct wpa_interface)); |
---|
377 | if (iface == NULL) |
---|
378 | goto out; |
---|
379 | ifaces = iface; |
---|
380 | iface = &ifaces[iface_count - 1]; |
---|
381 | os_memset(iface, 0, sizeof(*iface)); |
---|
382 | break; |
---|
383 | default: |
---|
384 | usage(); |
---|
385 | exitcode = 0; |
---|
386 | goto out; |
---|
387 | } |
---|
388 | } |
---|
389 | |
---|
390 | exitcode = 0; |
---|
391 | global = wpa_supplicant_init(¶ms); |
---|
392 | if (global == NULL) { |
---|
393 | wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant"); |
---|
394 | exitcode = -1; |
---|
395 | goto out; |
---|
396 | } else { |
---|
397 | wpa_printf(MSG_INFO, "Successfully initialized " |
---|
398 | "wpa_supplicant"); |
---|
399 | } |
---|
400 | |
---|
401 | if (fst_global_init()) { |
---|
402 | wpa_printf(MSG_ERROR, "Failed to initialize FST"); |
---|
403 | exitcode = -1; |
---|
404 | goto out; |
---|
405 | } |
---|
406 | |
---|
407 | #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE) |
---|
408 | if (!fst_global_add_ctrl(fst_ctrl_cli)) |
---|
409 | wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl"); |
---|
410 | #endif |
---|
411 | |
---|
412 | for (i = 0; exitcode == 0 && i < iface_count; i++) { |
---|
413 | struct wpa_supplicant *wpa_s; |
---|
414 | |
---|
415 | if ((ifaces[i].confname == NULL && |
---|
416 | ifaces[i].ctrl_interface == NULL) || |
---|
417 | ifaces[i].ifname == NULL) { |
---|
418 | if (iface_count == 1 && (params.ctrl_interface || |
---|
419 | params.dbus_ctrl_interface)) |
---|
420 | break; |
---|
421 | usage(); |
---|
422 | exitcode = -1; |
---|
423 | break; |
---|
424 | } |
---|
425 | wpa_s = wpa_supplicant_add_iface(global, &ifaces[i], NULL); |
---|
426 | if (wpa_s == NULL) { |
---|
427 | exitcode = -1; |
---|
428 | break; |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | if (exitcode == 0) |
---|
433 | exitcode = wpa_supplicant_run(global); |
---|
434 | |
---|
435 | wpa_supplicant_deinit(global); |
---|
436 | |
---|
437 | fst_global_deinit(); |
---|
438 | |
---|
439 | out: |
---|
440 | wpa_supplicant_fd_workaround(0); |
---|
441 | os_free(ifaces); |
---|
442 | os_free(params.pid_file); |
---|
443 | |
---|
444 | os_program_deinit(); |
---|
445 | |
---|
446 | return exitcode; |
---|
447 | } |
---|