source: rtems-libbsd/freebsd/contrib/wpa/wpa_supplicant/main.c @ 9c9d11b

55-freebsd-126-freebsd-12
Last change on this file since 9c9d11b was 9c9d11b, checked in by Sichen Zhao <1473996754@…>, on 08/01/17 at 12:43:41

Import wpa from FreeBSD

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