source: rtems-libbsd/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c @ 3d1e767

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 3d1e767 was 3d1e767, checked in by Sebastian Huber <sebastian.huber@…>, on 04/27/16 at 08:25:22

Directly use <sys/types.h> provided by Newlib

  • Property mode set to 100644
File size: 7.3 KB
Line 
1/*
2 * Copyright (c) 2016 Chris Johns <chrisj@rtems.org>.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26/*
27 * Handle the networking directives found in rc.conf.
28 *  - ifconfig_*
29 *  - cloned_interfaces
30 *  - autobridge_interfaces
31 *  - autobridge_bridge*
32 *  - defaultrouter
33 */
34
35#include <rtems/bsd/sys/param.h>
36#include <sys/types.h>
37#include <sys/queue.h>
38#include <sys/kernel.h>
39#include <sysexits.h>
40
41#include <errno.h>
42#include <stddef.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <unistd.h>
46
47#include <rtems.h>
48#include <rtems/chain.h>
49
50#include <machine/rtems-bsd-commands.h>
51#include <machine/rtems-bsd-rc-conf.h>
52
53/*
54 * cloned_interfaces
55 *
56 * eg cloned_interfaces="vlan0 bridge0 tap1 tap2"
57 *
58 * See 'man rc.conf(5)' on FreeBSD.
59 */
60static int
61cloned_interfaces(rtems_bsd_rc_conf* rc_conf,
62                  int                argc,
63                  const char**       argv)
64{
65  int arg;
66  for (arg = 1; arg < argc; ++arg) {
67    const char* ifconfg_args[] = {
68      "ifconfig", argv[arg], "create", NULL
69    };
70    int r;
71    rtems_bsd_rc_conf_print_cmd(rc_conf, "cloning_interfaces", 3, ifconfg_args);
72    r = rtems_bsd_command_ifconfig(3, (char**) ifconfg_args);
73    if (r != EX_OK) {
74      errno = ECANCELED;
75      return -1;
76    }
77  }
78  return 0;
79}
80
81/*
82 * create_args_'interface'
83 *
84 * eg create_args_myvlan="vlan 102"
85 *
86 * See 'man rc.conf(5)' on FreeBSD.
87 */
88typedef struct {
89  rtems_chain_node node;
90  const char*      label;
91  int              argc;
92  const char**     argv;
93} create_args_item;
94
95static RTEMS_CHAIN_DEFINE_EMPTY(create_args_items);
96
97static int
98create_args_(rtems_bsd_rc_conf* rc_conf,
99             int                argc,
100             const char**       argv)
101{
102  rtems_chain_node*       node = rtems_chain_first(&create_args_items);
103  const rtems_chain_node* tail = rtems_chain_tail(&create_args_items);
104  const char*             label = argv[0] + strlen("create_args_");
105  create_args_item*       item;
106  int                     arg;
107
108  while (node != tail) {
109    item = (create_args_item*) node;
110    if (strcasecmp(item->label, label) == 0) {
111      fprintf(stderr, "error: %s:%d: duplicate create args entry: %s\n",
112              rtems_bsd_rc_conf_name(rc_conf),
113              rtems_bsd_rc_conf_line(rc_conf),
114              argv[0]);
115      errno = EEXIST;
116      return -1;
117    }
118    node = rtems_chain_next(node);
119  }
120
121  item = calloc(1, sizeof(*item));
122  if (item == NULL) {
123    errno = ENOMEM;
124    fprintf(stderr, "error: %s:%d: %s\n",
125            rtems_bsd_rc_conf_name(rc_conf),
126            rtems_bsd_rc_conf_line(rc_conf),
127            strerror(errno));
128    return -1;
129  }
130
131  item->argc = argc;
132
133  item->label = strdup(label);
134  if (item->label == NULL) {
135    free(item);
136    errno = ENOMEM;
137    fprintf(stderr, "error: %s:%d: %s\n",
138            rtems_bsd_rc_conf_name(rc_conf),
139            rtems_bsd_rc_conf_line(rc_conf),
140            strerror(errno));
141    return -1;
142  }
143
144  item->argv = calloc(argc + 1, sizeof(char*));
145  if (item->argv == NULL) {
146    free((void*) item->label);
147    free(item);
148    errno = ENOMEM;
149    fprintf(stderr, "error: %s:%d: %s\n",
150            rtems_bsd_rc_conf_name(rc_conf),
151            rtems_bsd_rc_conf_line(rc_conf),
152            strerror(errno));
153    return -1;
154  }
155
156  for (arg = 0; arg < argc; ++arg) {
157    item->argv[arg] = strdup(argv[0]);
158    if (item->argv[arg] == NULL) {
159      int a;
160      for (a = 0; a < arg; ++a)
161        free((void*) item->argv[a]);
162      free(item->argv);
163      free((void*) item->label);
164      free(item);
165      errno = ENOMEM;
166      fprintf(stderr, "error: %s:%d: %s\n",
167              rtems_bsd_rc_conf_name(rc_conf),
168              rtems_bsd_rc_conf_line(rc_conf),
169              strerror(errno));
170      return -1;
171    }
172  }
173
174  rtems_chain_append(&create_args_items, &item->node);
175
176  return 0;
177}
178
179/*
180 * ifconfig_'interface'
181 *
182 * eg ifconfig_em0="inet 10.10.5.33 netmask 255.255.255.0"
183 *
184 * See 'man rc.conf(5)' on FreeBSD.
185 */
186static int
187ifconfig_(rtems_bsd_rc_conf* rc_conf,
188          int                argc,
189          const char**       argv)
190{
191  const char** args;
192  int          arg;
193  int          r;
194
195  for (arg = 1; arg < argc; ++arg) {
196    if (strcasecmp(argv[arg], "NOAUTO") == 0)
197      return 0;
198  }
199
200  args = calloc(argc + 3, sizeof(char*));
201  if (args == NULL) {
202    errno = ENOMEM;
203    return -1;
204  }
205
206  args[0] = "ifconfig";
207  args[1] = argv[0] + strlen("ifconfig_");
208
209  for (arg = 1; arg < argc; ++arg)
210    args[arg + 1] = argv[arg];
211
212  args[argc + 1] = "up";
213
214  rtems_bsd_rc_conf_print_cmd(rc_conf, "ifconfig", argc + 2, args);
215
216  r = rtems_bsd_command_ifconfig(argc + 2, (char**) args);
217
218  free(args);
219
220  if (r != EX_OK) {
221    errno = ECANCELED;
222    return -1;
223  }
224
225  return 0;
226}
227
228/*
229 * hostname
230 *
231 * eg hostname="myhost"
232 *
233 * See 'man rc.conf(5)' on FreeBSD.
234 */
235static int
236hostname(rtems_bsd_rc_conf* rc_conf,
237         int                argc,
238         const char**       argv)
239{
240  if (argc > 2) {
241    errno = EINVAL;
242    return -1;
243  }
244
245  rtems_bsd_rc_conf_print_cmd(rc_conf, "hostname", argc, argv);
246
247  return sethostname(argv[1], strlen(argv[1]));
248}
249
250/*
251 * defaultrouter
252 *
253 * eg defaultrouter="1.2.3.4"
254 *
255 * See 'man rc.conf(5)' on FreeBSD.
256 */
257static int
258defaultrouter(rtems_bsd_rc_conf* rc_conf,
259              int                argc,
260              const char**       argv)
261{
262  if (argc > 2) {
263    errno = EINVAL;
264    return -1;
265  }
266
267  if (strcasecmp(argv[1], "NO") != 0) {
268    const char* args[] = {
269      "route", "add", "default", argv[1], NULL
270    };
271    int r;
272
273    rtems_bsd_rc_conf_print_cmd(rc_conf, "defaultrouter", 4, args);
274
275    r = rtems_bsd_command_route(4, (char**) args);
276    if (r != EX_OK) {
277      errno = ECANCELED;
278      return -1;
279    }
280  }
281
282  return 0;
283}
284
285static void
286add_directive(const char* name, rtems_bsd_rc_conf_directive handler)
287{
288  int r;
289  r = rtems_bsd_rc_conf_directive_add(name, handler);
290  if (r < 0)
291    fprintf(stderr, "error: cannot register rc.conf handler: %s\n", name);
292}
293
294void
295rc_conf_net_init(void* arg)
296{
297  add_directive("cloned_interfaces", cloned_interfaces);
298  add_directive("create_args_.*", create_args_);
299  add_directive("ifconfig_.*", ifconfig_);
300  add_directive("hostname", hostname);
301  add_directive("defaultrouter", defaultrouter);
302}
Note: See TracBrowser for help on using the repository browser.