summaryrefslogtreecommitdiff
path: root/lwip/options.c
blob: d35b9f3212ffee67febf4264a216a5f98adac76d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
   Copyright (C) 1996, 1997, 2000, 2001, 2006, 2007, 2017
     Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.org>

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Fsysopts and command line option parsing */

#include <options.h>

#include <stdlib.h>
#include <argp.h>
#include <argz.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if_arp.h>
#include <error.h>

#include <lwip/netif.h>
#include <lwip/tcpip.h>

#include <lwip-hurd.h>
#include <lwip-util.h>
#include <netif/ifcommon.h>

/* Fsysopts and command line option parsing */

/* Adds an empty interface slot to H, and sets H's current interface to it, or
   returns an error. */
static error_t
parse_hook_add_interface (struct parse_hook *h)
{
  int i;

  struct parse_interface *new = realloc (h->interfaces,
					 (h->num_interfaces +
					  1) *
					 sizeof (struct parse_interface));
  if (!new)
    return ENOMEM;

  h->interfaces = new;
  h->num_interfaces++;
  h->curint = new + h->num_interfaces - 1;
  memset (&h->curint->dev_name, 0, DEV_NAME_LEN);
  h->curint->address.addr = INADDR_NONE;
  h->curint->netmask.addr = INADDR_NONE;
  h->curint->peer.addr = INADDR_NONE;
  h->curint->gateway.addr = INADDR_NONE;
  for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
    ip6_addr_set_zero ((ip6_addr_t *) & h->curint->addr6[i]);

  return 0;
}

/* Option parser */
static error_t
parse_opt (int opt, char *arg, struct argp_state *state)
{
  error_t err = 0;
  struct parse_hook *h = state->hook;
  int i;

  /* Return _ERR from this routine */
#define RETURN(_err)                          \
  do { return _err; } while (0)

  /* Print a parsing error message and (if exiting is turned off) return the
     error code ERR.  */
#define PERR(err, fmt, args...)               \
  do { argp_error (state, fmt , ##args); RETURN (err); } while (0)

  /* Like PERR but for non-parsing errors.  */
#define FAIL(rerr, status, perr, fmt, args...)  \
  do{ argp_failure (state, status, perr, fmt , ##args); RETURN (rerr); } while(0)

  /* Parse STR and return the corresponding  internet address.  If STR is not
     a valid internet address, signal an error mentioned TYPE.  */
#undef	ADDR
#define ADDR(str, type)                         \
  ({ unsigned long addr = inet_addr (str);      \
     if (addr == INADDR_NONE) PERR (EINVAL, "Malformed %s", type);  \
     addr; })

  if (!arg && state->next < state->argc && (*state->argv[state->next] != '-'))
    {
      arg = state->argv[state->next];
      state->next++;
    }

  switch (opt)
    {
      struct parse_interface *in;
      uint8_t addr6_prefix_len;
      ip6_addr_t *address6;
      char *ptr;

    case 'i':
      /* An interface.  */
      err = 0;

      /* First see if a previously specified one is being re-specified.  */
      for (in = h->interfaces; in < h->interfaces + h->num_interfaces; in++)
	if (strcmp (in->dev_name, arg) == 0)
	  /* Re-use an old slot.  */
	  {
	    h->curint = in;
	    return 0;
	  }

      if (h->curint->dev_name[0])
	/* The current interface slot is not available.  */
	{
	  /* Add a new interface entry.  */
	  err = parse_hook_add_interface (h);
	}
      in = h->curint;

      strncpy (in->dev_name, arg, sizeof(in->dev_name)-1);
      break;

    case 'a':
      /* An address */
      if (arg)
	{
	  /* Check if it's legal */
	  h->curint->address.addr = ADDR (arg, "address");
	  if (!IN_CLASSA (ntohl (h->curint->address.addr))
	      && !IN_CLASSB (ntohl (h->curint->address.addr))
	      && !IN_CLASSC (ntohl (h->curint->address.addr)))
	    {
	      if (IN_MULTICAST (ntohl (h->curint->address.addr)))
		FAIL (EINVAL, 1, 0,
		      "%s: Cannot set interface address to multicast address",
		      arg);
	      else
		FAIL (EINVAL, 1, 0,
		      "%s: Illegal or undefined network address", arg);
	    }
	}
      else
	{
	  /* No address given, set default values */
	  h->curint->address.addr = ADDR ("0.0.0.0", "address");
	  h->curint->netmask.addr = ADDR ("255.0.0.0", "netmask");
	  h->curint->gateway.addr = INADDR_NONE;
	}
      break;

    case 'm':
      /* Netmask */
      if (arg)
	h->curint->netmask.addr = ADDR (arg, "netmask");
      else
	h->curint->netmask.addr = INADDR_NONE;
      break;

    case 'p':
      /* Peer address */
      if (arg)
	h->curint->peer.addr = ADDR (arg, "peer");
      else
	h->curint->peer.addr = INADDR_NONE;
      break;

    case 'g':
      /* Gateway for the current interface */
      if (arg)
	{
	  h->curint->gateway.addr = ADDR (arg, "gateway");
	}
      else
	h->curint->gateway.addr = INADDR_NONE;
      break;

    case '4':
      translator_bind (PORTCLASS_INET, arg);

      /* Install IPv6 port class on bootstrap port. */
      lwip_bootstrap_portclass = PORTCLASS_INET6;
      break;

    case '6':
      translator_bind (PORTCLASS_INET6, arg);
      break;

    case 'A':
      /* IPv6 address */
      if (arg)
	{
	  /* Check prefix */
	  if ((ptr = strchr (arg, '/')))
	    {
	      addr6_prefix_len = atoi (ptr + 1);
	      if (addr6_prefix_len > 128)
		FAIL (EINVAL, 1, 0, "%s: The prefix-length is invalid", arg);

	      /* Remove the prefix from the address */
	      *ptr = 0;

	      if (addr6_prefix_len != 64)
		{
		  error (0, 0,
			 "The only supported value for the prefix-length"
			 " is /64. Defaulting to %s/64.\n", arg);
		}
	    }
	  else
	    {
	      error (0, 0, "No prefix-length given, "
		     "defaulting to %s/64.\n", arg);
	    }

	  for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
	    {
	      address6 = (ip6_addr_t *) & h->curint->addr6[i];

	      /* Is the slot free? */
	      if (!ip6_addr_isany (address6))
		continue;

	      /* Use the slot */
	      if (ip6addr_aton (arg, address6) <= 0)
		PERR (EINVAL, "Malformed address");

	      break;
	    }
	}

      break;

    case ARGP_KEY_INIT:
      /* Initialize our parsing state.  */
      h = malloc (sizeof (struct parse_hook));
      if (!h)
	FAIL (ENOMEM, 11, ENOMEM, "option parsing");

      h->interfaces = 0;
      h->num_interfaces = 0;
      err = parse_hook_add_interface (h);
      if (err)
	FAIL (err, 12, err, "option parsing");

      state->hook = h;
      break;

    case ARGP_KEY_SUCCESS:
      /* If the interface list is not empty, a previous configuration exists */
      if (netif_list == 0)
	/* Inititalize LwIP */
	tcpip_init (init_ifs, h);
      else
	/* No need to initialize the stack again */
	init_ifs (h);
      break;

    case ARGP_KEY_ERROR:
      /* Parsing error occurred, free everything. */
      free (h->interfaces);
      free (h);
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }

  return err;
}

/* Create the output for fsysopts */
error_t
trivfs_append_args (struct trivfs_control * fsys, char **argz,
		    size_t * argz_len)
{
  error_t err = 0;
  struct netif *netif;
  int i;
  uint32_t addr, netmask, gateway;
  uint32_t addr6[LWIP_IPV6_NUM_ADDRESSES][4];
  uint8_t addr6_prefix_len[LWIP_IPV6_NUM_ADDRESSES];

#define ADD_OPT(fmt, args...)           \
  do { char buf[100];                   \
       if (! err) {                     \
         snprintf (buf, sizeof buf, fmt , ##args);      \
         err = argz_add (argz, argz_len, buf); } } while (0)
#define ADD_ADDR_OPT(name, addr)        \
  do { struct in_addr i;                \
       i.s_addr = (addr);               \
       ADD_OPT ("--%s=%s", name, inet_ntoa (i)); } while (0)

  for (netif = netif_list; netif != 0; netif = netif->next)
    {
      /* Skip the loopback interface */
      if (netif_get_state (netif)->type == ARPHRD_LOOPBACK)
	{
	  continue;
	}

      inquire_device (netif, &addr, &netmask, 0, 0, &gateway,
		      (uint32_t *) addr6, addr6_prefix_len);

      ADD_OPT ("--interface=%s", netif_get_state (netif)->devname);
      if (addr != INADDR_NONE)
	ADD_ADDR_OPT ("address", addr);
      if (netmask != INADDR_NONE)
	ADD_ADDR_OPT ("netmask", netmask);
      if (gateway != INADDR_NONE)
	ADD_ADDR_OPT ("gateway", gateway);
      for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
	if (!ip6_addr_isany (((ip6_addr_t *) & addr6[i])))
	  ADD_OPT ("--address6=%s/%d",
		   ip6addr_ntoa (((ip6_addr_t *) & addr6[i])),
		   addr6_prefix_len[i]);
    }

#undef ADD_ADDR_OPT

#undef ADD_OPT
  return err;
}

struct argp lwip_argp = { options, parse_opt, 0, doc };

struct argp *trivfs_runtime_argp = &lwip_argp;