source: rtems-libbsd/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c @ 84665b5

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 84665b5 was 84665b5, checked in by Chris Johns <chrisj@…>, on 05/18/16 at 09:03:20

rc.conf: Fix the wild card regex for ifconfig_

  • 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 <rtems/bsd/sys/types.h>
37
38#include <sys/queue.h>
39#include <sys/kernel.h>
40#include <sysexits.h>
41
42#include <errno.h>
43#include <stddef.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <unistd.h>
47
48#include <rtems.h>
49#include <rtems/chain.h>
50
51#include <machine/rtems-bsd-commands.h>
52#include <machine/rtems-bsd-rc-conf.h>
53
54/*
55 * cloned_interfaces
56 *
57 * eg cloned_interfaces="vlan0 bridge0 tap1 tap2"
58 *
59 * See 'man rc.conf(5)' on FreeBSD.
60 */
61static int
62cloned_interfaces(rtems_bsd_rc_conf* rc_conf,
63                  int                argc,
64                  const char**       argv)
65{
66  int arg;
67  for (arg = 1; arg < argc; ++arg) {
68    const char* ifconfg_args[] = {
69      "ifconfig", argv[arg], "create", NULL
70    };
71    int r;
72    rtems_bsd_rc_conf_print_cmd(rc_conf, "cloning_interfaces", 3, ifconfg_args);
73    r = rtems_bsd_command_ifconfig(3, (char**) ifconfg_args);
74    if (r != EX_OK) {
75      errno = ECANCELED;
76      return -1;
77    }
78  }
79  return 0;
80}
81
82/*
83 * create_args_'interface'
84 *
85 * eg create_args_myvlan="vlan 102"
86 *
87 * See 'man rc.conf(5)' on FreeBSD.
88 */
89typedef struct {
90  rtems_chain_node node;
91  const char*      label;
92  int              argc;
93  const char**     argv;
94} create_args_item;
95
96static RTEMS_CHAIN_DEFINE_EMPTY(create_args_items);
97
98static int
99create_args_(rtems_bsd_rc_conf* rc_conf,
100             int                argc,
101             const char**       argv)
102{
103  rtems_chain_node*       node = rtems_chain_first(&create_args_items);
104  const rtems_chain_node* tail = rtems_chain_tail(&create_args_items);
105  const char*             label = argv[0] + strlen("create_args_");
106  create_args_item*       item;
107  int                     arg;
108
109  while (node != tail) {
110    item = (create_args_item*) node;
111    if (strcasecmp(item->label, label) == 0) {
112      fprintf(stderr, "error: %s:%d: duplicate create args entry: %s\n",
113              rtems_bsd_rc_conf_name(rc_conf),
114              rtems_bsd_rc_conf_line(rc_conf),
115              argv[0]);
116      errno = EEXIST;
117      return -1;
118    }
119    node = rtems_chain_next(node);
120  }
121
122  item = calloc(1, sizeof(*item));
123  if (item == NULL) {
124    errno = ENOMEM;
125    fprintf(stderr, "error: %s:%d: %s\n",
126            rtems_bsd_rc_conf_name(rc_conf),
127            rtems_bsd_rc_conf_line(rc_conf),
128            strerror(errno));
129    return -1;
130  }
131
132  item->argc = argc;
133
134  item->label = strdup(label);
135  if (item->label == NULL) {
136    free(item);
137    errno = ENOMEM;
138    fprintf(stderr, "error: %s:%d: %s\n",
139            rtems_bsd_rc_conf_name(rc_conf),
140            rtems_bsd_rc_conf_line(rc_conf),
141            strerror(errno));
142    return -1;
143  }
144
145  item->argv = calloc(argc + 1, sizeof(char*));
146  if (item->argv == NULL) {
147    free((void*) item->label);
148    free(item);
149    errno = ENOMEM;
150    fprintf(stderr, "error: %s:%d: %s\n",
151            rtems_bsd_rc_conf_name(rc_conf),
152            rtems_bsd_rc_conf_line(rc_conf),
153            strerror(errno));
154    return -1;
155  }
156
157  for (arg = 0; arg < argc; ++arg) {
158    item->argv[arg] = strdup(argv[0]);
159    if (item->argv[arg] == NULL) {
160      int a;
161      for (a = 0; a < arg; ++a)
162        free((void*) item->argv[a]);
163      free(item->argv);
164      free((void*) item->label);
165      free(item);
166      errno = ENOMEM;
167      fprintf(stderr, "error: %s:%d: %s\n",
168              rtems_bsd_rc_conf_name(rc_conf),
169              rtems_bsd_rc_conf_line(rc_conf),
170              strerror(errno));
171      return -1;
172    }
173  }
174
175  rtems_chain_append(&create_args_items, &item->node);
176
177  return 0;
178}
179
180/*
181 * ifconfig_'interface'
182 *
183 * eg ifconfig_em0="inet 10.10.5.33 netmask 255.255.255.0"
184 *
185 * See 'man rc.conf(5)' on FreeBSD.
186 */
187static int
188ifconfig_(rtems_bsd_rc_conf* rc_conf,
189          int                argc,
190          const char**       argv)
191{
192  const char** args;
193  int          arg;
194  int          r;
195
196  for (arg = 1; arg < argc; ++arg) {
197    if (strcasecmp(argv[arg], "NOAUTO") == 0)
198      return 0;
199  }
200
201  args = calloc(argc + 3, sizeof(char*));
202  if (args == NULL) {
203    errno = ENOMEM;
204    return -1;
205  }
206
207  args[0] = "ifconfig";
208  args[1] = argv[0] + strlen("ifconfig_");
209
210  for (arg = 1; arg < argc; ++arg)
211    args[arg + 1] = argv[arg];
212
213  args[argc + 1] = "up";
214
215  rtems_bsd_rc_conf_print_cmd(rc_conf, "ifconfig", argc + 2, args);
216
217  r = rtems_bsd_command_ifconfig(argc + 2, (char**) args);
218
219  free(args);
220
221  if (r != EX_OK) {
222    errno = ECANCELED;
223    return -1;
224  }
225
226  return 0;
227}
228
229/*
230 * hostname
231 *
232 * eg hostname="myhost"
233 *
234 * See 'man rc.conf(5)' on FreeBSD.
235 */
236static int
237hostname(rtems_bsd_rc_conf* rc_conf,
238         int                argc,
239         const char**       argv)
240{
241  if (argc > 2) {
242    errno = EINVAL;
243    return -1;
244  }
245
246  rtems_bsd_rc_conf_print_cmd(rc_conf, "hostname", argc, argv);
247
248  return sethostname(argv[1], strlen(argv[1]));
249}
250
251/*
252 * defaultrouter
253 *
254 * eg defaultrouter="1.2.3.4"
255 *
256 * See 'man rc.conf(5)' on FreeBSD.
257 */
258static int
259defaultrouter(rtems_bsd_rc_conf* rc_conf,
260              int                argc,
261              const char**       argv)
262{
263  if (argc > 2) {
264    errno = EINVAL;
265    return -1;
266  }
267
268  if (strcasecmp(argv[1], "NO") != 0) {
269    const char* args[] = {
270      "route", "add", "default", argv[1], NULL
271    };
272    int r;
273
274    rtems_bsd_rc_conf_print_cmd(rc_conf, "defaultrouter", 4, args);
275
276    r = rtems_bsd_command_route(4, (char**) args);
277    if (r != EX_OK) {
278      errno = ECANCELED;
279      return -1;
280    }
281  }
282
283  return 0;
284}
285
286static void
287add_directive(const char* name, rtems_bsd_rc_conf_directive handler)
288{
289  int r;
290  r = rtems_bsd_rc_conf_directive_add(name, handler);
291  if (r < 0)
292    fprintf(stderr, "error: cannot register rc.conf handler: %s\n", name);
293}
294
295void
296rc_conf_net_init(void* arg)
297{
298  add_directive("cloned_interfaces", cloned_interfaces);
299  add_directive("create_args_.*", create_args_);
300  add_directive("ifconfig_.*", ifconfig_);
301  add_directive("hostname", hostname);
302  add_directive("defaultrouter", defaultrouter);
303}
Note: See TracBrowser for help on using the repository browser.