summaryrefslogtreecommitdiff
path: root/lwip/lwip-util.c
blob: 4f632185b5a33e57d34ce878d1e04bb7433f5046 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
   Copyright (C) 2017 Free Software Foundation, Inc.
   Written by Joan Lledó.

   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/>.
*/

/* Lwip management module */

#include <lwip-util.h>

#include <error.h>
#include <net/if_arp.h>

#include <lwip/sockets.h>
#include <lwip/inet.h>
#include <lwip/tcpip.h>

#include <lwip-hurd.h>
#include <options.h>
#include <netif/hurdethif.h>
#include <netif/hurdtunif.h>
#include <netif/hurdloopif.h>

/*
 * Detect the proper module for the given device name
 * and returns its init callback
 */
static error_t
create_netif_state (char *name, struct ifcommon *ifc)
{
  char *base_name;

  memset (ifc, 0, sizeof (struct ifcommon));

  base_name = strrchr (name, '/');
  if (base_name)
    base_name++;
  else
    base_name = name;

  if (strncmp (base_name, "tun", 3) == 0)
    ifc->init = hurdtunif_device_init;
  else
    ifc->init = hurdethif_device_init;

  /* Freed in the module terminate callback */
  ifc->devname = strndup (name, strlen (name));

  return errno;
}

/* Some checks for IPv4 configurations */
static int
ipv4config_is_valid (uint32_t addr, uint32_t netmask,
		     uint32_t gateway, uint32_t broadcast)
{
  /* Check whether the user provided a valid netmask */
  if (netmask != INADDR_NONE && !ip4_addr_netmask_valid (netmask))
    {
      error (0, 0, "Error: Invalid network mask.\n");
      return 0;
    }

  /* The given gateway, if any, must be in the same network as the address */
  if (gateway != INADDR_NONE && (gateway & netmask) != (addr & netmask))
    {
      error (0, 0,
	     "Error: the gateway is not in the same network as the address.\n");
      return 0;
    }

  /*
   * LwIP doesn't allow setting the broadcast address.
   * We must ensure the given broadcast address is the default one for this
   * network.
   */
  if (broadcast != INADDR_NONE
      && netmask != INADDR_NONE && broadcast != (addr | ~netmask))
    {
      error (0, 0,
	     "Error: the broadcast address doesn't match the network mask.\n");
      return 0;
    }

  return 1;
}

/* Configure the loopback interface */
static void
init_loopback ()
{
  struct ifcommon ifc;

  memset (&ifc, 0, sizeof (struct ifcommon));
  ifc.init = hurdloopif_device_init;
  netif_list->state = &ifc;

  if_init (netif_list);
}

/* Remove the existing interfaces, but the loopback one */
void
remove_ifs ()
{
  struct netif *netif;

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

      netif = netif_list;
    }

  return;
}

/* Initialize the interfaces given by the user through command line */
void
init_ifs (void *arg)
{
  struct parse_interface *in;
  struct parse_hook *ifs;
  struct netif *netif;
  struct ifcommon ifc;
  int8_t ipv6_addr_idx;
  ip6_addr_t *address6;
  int i;

  if (netif_list != 0)
    {
      if (netif_list->next == 0)
	init_loopback ();
      else
	remove_ifs ();
    }

  /*
   * Go through the list backwards. For LwIP
   * to create its list in the proper order.
   */
  ifs = (struct parse_hook *) arg;
  for (in = ifs->interfaces + ifs->num_interfaces - 1;
       in >= ifs->interfaces; in--)
    {
      /* The interface hasn't been completely configured */
      if (!in->dev_name[0])
	continue;

      if (!ipv4config_is_valid (in->address.addr, in->netmask.addr,
				in->gateway.addr, INADDR_NONE))
	continue;

      netif = calloc (1, sizeof (struct netif));

      create_netif_state (in->dev_name, &ifc);

      /*
       * Create a new interface and configre IPv4.
       *
       * Fifth parameter (ifc) is a hook.
       */
      if (!netif_add (netif, &in->address, &in->netmask, &in->gateway, &ifc,
			if_init, tcpip_input))
	{
	  /* The interface failed to init */
	  if (netif->state != &ifc)
	    /* It failed after setting the control block, must free it */
	    mem_free (netif->state);
	  free (netif);
	  continue;
	}

      /* Add IPv6 configuration */
      netif->ip6_autoconfig_enabled = 1;
      netif_create_ip6_linklocal_address (netif, 1);

      /* Add user given unicast addresses */
      for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
	{
	  address6 = (ip6_addr_t *) & in->addr6[i];

	  if (!ip6_addr_isany (address6) && !ip6_addr_ismulticast (address6))
	    {
	      netif_add_ip6_address (netif, address6, &ipv6_addr_idx);

	      if (ipv6_addr_idx >= 0)
		/* First use DAD to make sure nobody else has it */
		netif_ip6_addr_set_state (netif, ipv6_addr_idx,
					  IP6_ADDR_TENTATIVE);
	      else
		error (0, 0, "No free slot for IPv6 address: %s\n",
		       ip6addr_ntoa (address6));
	    }
	}

      /* Up the inerface */
      netif_set_up (netif);

      /* Set the first interface with valid gateway as default */
      if (in->gateway.addr != INADDR_NONE)
	{
	  netif_set_default (netif);
	}
    }

  /* Free the hook */
  free (ifs->interfaces);
  free (ifs);

  return;
}

/* Args for update_if() */
struct update_if_args
{
  struct netif *netif;
  uint32_t addr;
  uint32_t netmask;
  uint32_t peer;
  uint32_t broadcast;
  uint32_t gateway;
  uint32_t *addr6;
  uint8_t *addr6_prefix_len;
};

/*
 * Change the IP configuration of an interface
 */
static void
update_if (void *arg)
{
  int i;
  struct update_if_args *args = (struct update_if_args *) arg;

  netif_set_addr (args->netif, (ip4_addr_t *) & args->addr,
			   (ip4_addr_t *) & args->netmask,
			   (ip4_addr_t *) & args->gateway);

  if (args->addr6)
    for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
      {
	ip6_addr_t *laddr6 = ((ip6_addr_t *) args->addr6 + i);
	if (!ip6_addr_isany (laddr6))
	  {
	    netif_ip6_addr_set (args->netif, i, laddr6);

	    if (!ip6_addr_islinklocal (laddr6))
	      netif_ip6_addr_set_state (args->netif, i, IP6_ADDR_TENTATIVE);
	  }
      }

  if (args->addr6_prefix_len)
    for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
      *(args->addr6_prefix_len + i) = 64;

  free (args);

  return;
}

/* Get the IP configuration of an interface */
void
inquire_device (struct netif *netif, uint32_t * addr, uint32_t * netmask,
		uint32_t * peer, uint32_t * broadcast, uint32_t * gateway,
		uint32_t * addr6, uint8_t * addr6_prefix_len)
{
  int i;

  if (netif)
    {
      if (addr)
	*addr = netif_ip4_addr (netif)->addr;

      if (netmask)
	*netmask = netif_ip4_netmask (netif)->addr;

      if (peer)
	*peer = INADDR_NONE;

      if (broadcast)
	*broadcast =
	  netif_ip4_addr (netif)->addr | ~netif_ip4_netmask (netif)->addr;

      if (gateway)
	*gateway = netif_ip4_gw (netif)->addr;

      if (addr6)
	for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
	  {
	    *(addr6 + i * 4 + 0) = netif_ip6_addr (netif, i)->addr[0];
	    *(addr6 + i * 4 + 1) = netif_ip6_addr (netif, i)->addr[1];
	    *(addr6 + i * 4 + 2) = netif_ip6_addr (netif, i)->addr[2];
	    *(addr6 + i * 4 + 3) = netif_ip6_addr (netif, i)->addr[3];
	  }

      if (addr6_prefix_len)
	for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
	  *(addr6_prefix_len + i) = 64;
    }
}

/*
 * Check and change the IP configuration of an interface.
 * Called from ioctls.
 */
error_t
configure_device (struct netif *netif, uint32_t addr, uint32_t netmask,
		  uint32_t peer, uint32_t broadcast, uint32_t gateway,
		  uint32_t * addr6, uint8_t * addr6_prefix_len)
{
  error_t err = 0;

  if (netmask != INADDR_NONE)
    /*
     * If broadcasting is enabled and we have a netmask lesser than 31 bits
     * long, we need to update the broadcast address too.
     */
    if ((netif->flags & NETIF_FLAG_BROADCAST)
	&& ip4_addr_netmask_valid (netmask) && netmask <= 0xfffffffc)
      broadcast = (addr | ~netmask);

  if (!ipv4config_is_valid (addr, netmask, gateway, broadcast))
    err = EINVAL;
  else
    {
      /* Call update_if() inside the tcpip_thread */
      struct update_if_args *arg = calloc (1, sizeof (struct update_if_args));
      arg->netif = netif;
      arg->addr = addr;
      arg->netmask = netmask;
      arg->peer = peer;
      arg->broadcast = broadcast;
      arg->gateway = gateway;
      arg->addr6 = addr6;
      arg->addr6_prefix_len = addr6_prefix_len;
      err = tcpip_callback (update_if, arg);
      if (err)
	return err;
    }

  return errno;
}