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

6-freebsd-12
Last change on this file was 705e362, checked in by Sebastian Huber <sebastian.huber@…>, on 06/05/19 at 09:35:39

Update to FreeBSD stable/12 2019-06-05

Git mirror commit 78576620f2689e23144a1cf1bf55106cc6abe2b7.

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