summaryrefslogtreecommitdiff
path: root/nuttx/net/uip/uip-fw.c
blob: 187fb6c39c1c991e6ac5425f46351183b657ae6b (plain) (blame)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* uip-fw.c
 * uIP packet forwarding.
 * Author: Adam Dunkels <adam@sics.se>
 *
 * This file implements a number of simple functions which do packet
 * forwarding over multiple network interfaces with uIP.
 *
 * Copyright (c) 2004, Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <nuttx/config.h>
#include <debug.h>
#include <net/uip/uip.h>
#include <net/uip/uip-arch.h>

#include "uip-fw.h"

#include <string.h> /* for memcpy() */

/* The list of registered network interfaces. */

static struct uip_fw_netif *netifs = NULL;

/* A pointer to the default network interface. */

static struct uip_fw_netif *defaultnetif = NULL;

struct tcpip_hdr
{
  /* IP header */

  uint8  vhl;
  uint8  tos;
  uint16 len;
  uint16 ipid;
  uint16 ipoffset;
  uint8  ttl;
  uint8  proto;
  uint16 ipchksum;
  in_addr_t srcipaddr;
  in_addr_t destipaddr;

  /* TCP header */

  uint16 srcport;
  uint16 destport;
  uint8  seqno[4];
  uint8  ackno[4];
  uint8  tcpoffset;
  uint8  flags;
  uint8  wnd[2];
  uint16 tcpchksum;
  uint8  urgp[2];
  uint8 optdata[4];
};

struct icmpip_hdr
{
  /* IP header */

  uint8  vhl;
  uint8  tos;
  uint8  len[2];
  uint8  ipid[2];
  uint8  ipoffset[2];
  uint8  ttl;
  uint8  proto;
  uint16 ipchksum;
  in_addr_t srcipaddr;
  in_addr_t destipaddr;

  /* ICMP (echo) header */

  uint8 type, icode;
  uint16 icmpchksum;
  uint16 id, seqno;
  uint8 payload[1];
};

/* ICMP ECHO. */
#define ICMP_ECHO 8

/* ICMP TIME-EXCEEDED. */
#define ICMP_TE 11

/* Pointer to the TCP/IP headers of the packet in the d_buf buffer. */
#define BUF ((struct tcpip_hdr *)&dev->d_buf[UIP_LLH_LEN])

/* Pointer to the ICMP/IP headers of the packet in the d_buf buffer. */
#define ICMPBUF ((struct icmpip_hdr *)&dev->d_buf[UIP_LLH_LEN])

/* Certain fields of an IP packet that are used for identifying
 * duplicate packets.
 */

struct fwcache_entry
{
  uint16 timer;
  in_addr_t srcipaddr;
  in_addr_t destipaddr;
  uint16 ipid;
  uint8 proto;
  uint8 unused;

#if notdef
  uint16 payload[2];
#endif

#if UIP_REASSEMBLY > 0
  uint16 len, offset;
#endif
};

/* The number of packets to remember when looking for duplicates. */
#ifdef CONFIG_NET_FWCACHE_SIZE
# define FWCACHE_SIZE CONFIG_NET_FWCACHE_SIZE
#else
# define FWCACHE_SIZE 2
#endif

/* A cache of packet header fields which are used for
 * identifying duplicate packets.
 */

static struct fwcache_entry fwcache[FWCACHE_SIZE];

/* The time that a packet cache is active. */
#define FW_TIME 20

/* Initialize the uIP packet forwarding module. */

void uip_fw_init(void)
{
  struct uip_fw_netif *t;
  defaultnetif = NULL;
  while(netifs != NULL) {
    t = netifs;
    netifs = netifs->next;
    t->next = NULL;
  }
}

/* Send out an ICMP TIME-EXCEEDED message.
 *
 * This function replaces the packet in the d_buf buffer with the
 * ICMP packet.
 */

static void time_exceeded(struct uip_driver_s *dev)
{
  in_addr_t tmp_addr;

  /* We don't send out ICMP errors for ICMP messages. */
  if (ICMPBUF->proto == UIP_PROTO_ICMP) {
    dev->d_len = 0;
    return;
  }
  /* Copy fields from packet header into payload of this ICMP packet. */
  memcpy(&(ICMPBUF->payload[0]), ICMPBUF, 28);

  /* Set the ICMP type and code. */
  ICMPBUF->type = ICMP_TE;
  ICMPBUF->icode = 0;

  /* Calculate the ICMP checksum. */
  ICMPBUF->icmpchksum = 0;
  ICMPBUF->icmpchksum = ~uip_chksum((uint16 *)&(ICMPBUF->type), 36);

  /* Set the IP destination address to be the source address of the
   * original packet.
   */

  tmp_addr = BUF->destipaddr;
  BUF->destipaddr = BUF->srcipaddr;
  BUF->srcipaddr = tmp_addr;

  /* Set our IP address as the source address. */

  BUF->srcipaddr = dev->d_ipaddr;

  /* The size of the ICMP time exceeded packet is 36 + the size of the
     IP header (20) = 56. */
  dev->d_len = 56;
  ICMPBUF->len[0] = 0;
  ICMPBUF->len[1] = dev->d_len;

  /* Fill in the other fields in the IP header. */
  ICMPBUF->vhl = 0x45;
  ICMPBUF->tos = 0;
  ICMPBUF->ipoffset[0] = ICMPBUF->ipoffset[1] = 0;
  ICMPBUF->ttl  = UIP_TTL;
  ICMPBUF->proto = UIP_PROTO_ICMP;

  /* Calculate IP checksum. */
  ICMPBUF->ipchksum = 0;
  ICMPBUF->ipchksum = ~(uip_ipchksum(dev));
}

/* Register a packet in the forwarding cache so that it won't be
 * forwarded again.
 */

static void fwcache_register(struct uip_driver_s *dev)
{
  struct fwcache_entry *fw;
  int i, oldest;

  oldest = FW_TIME;
  fw = NULL;

  /* Find the oldest entry in the cache. */
  for (i = 0; i < FWCACHE_SIZE; ++i) {
    if (fwcache[i].timer == 0) {
      fw = &fwcache[i];
      break;
    } else if (fwcache[i].timer <= oldest) {
      fw = &fwcache[i];
      oldest = fwcache[i].timer;
    }
  }

  fw->timer = FW_TIME;
  fw->ipid = BUF->ipid;
  fw->srcipaddr = BUF->srcipaddr;
  fw->destipaddr = BUF->destipaddr;
  fw->proto = BUF->proto;
#if notdef
  fw->payload[0] = BUF->srcport;
  fw->payload[1] = BUF->destport;
#endif
#if UIP_REASSEMBLY > 0
  fw->len = BUF->len;
  fw->offset = BUF->ipoffset;
#endif
}

/* Find a network interface for the IP packet in d_buf. */

static struct uip_fw_netif *find_netif (struct uip_driver_s *dev)
{
  struct uip_fw_netif *netif;

  /* Walk through every network interface to check for a match. */

  for (netif = netifs; netif != NULL; netif = netif->next)
    {
      if (uip_ipaddr_maskcmp(BUF->destipaddr, netif->ipaddr, netif->netmask))
        {
          /* If there was a match, we break the loop. */

          return netif;
        }
    }

  /* If no matching netif was found, we use default netif. */

  return defaultnetif;
}

/* Output an IP packet on the correct network interface.
 *
 * The IP packet should be present in the d_buf buffer and its
 * length in the d_len field.
 *
 * Return: UIP_FW_ZEROLEN Indicates that a zero-length packet
 * transmission was attempted and that no packet was sent.
 *
 * Return: UIP_FW_NOROUTE No suitable network interface could be found
 * for the outbound packet, and the packet was not sent.
 *
 * Return: The return value from the actual network interface output
 * function is passed unmodified as a return value.
 */

uint8 uip_fw_output(struct uip_driver_s *dev)
{
  struct uip_fw_netif *netif;

  if (dev->d_len == 0) {
    return UIP_FW_ZEROLEN;
  }

  fwcache_register(dev);

#if UIP_BROADCAST
  /* Link local broadcasts go out on all interfaces. */
  if (/*BUF->proto == UIP_PROTO_UDP &&*/
     BUF->destipaddr[0] == 0xffff &&
     BUF->destipaddr[1] == 0xffff) {
    if (defaultnetif != NULL) {
      defaultnetif->output();
    }
    for (netif = netifs; netif != NULL; netif = netif->next) {
      netif->output();
    }
    return UIP_FW_OK;
  }
#endif /* UIP_BROADCAST */

  netif = find_netif (dev);
  dbg("netif: %p output: %p len: %d\n", netif, netif->output, dev->d_len);

  if (netif == NULL) {
    return UIP_FW_NOROUTE;
  }
  /* If we now have found a suitable network interface, we call its
     output function to send out the packet. */
  return netif->output();
}

/* Forward an IP packet in the d_buf buffer.
 *
 * Return: UIP_FW_FORWARDED if the packet was forwarded, UIP_FW_LOCAL if
 * the packet should be processed locally.
 */

uint8 uip_fw_forward(struct uip_driver_s *dev)
{
  struct fwcache_entry *fw;

  /* First check if the packet is destined for ourselves and return 0
   * to indicate that the packet should be processed locally.
   */

  if (BUF->destipaddr == dev->d_ipaddr)
    {
      return UIP_FW_LOCAL;
    }

  /* If we use ping IP address configuration, and our IP address is
     not yet configured, we should intercept all ICMP echo packets. */
#if UIP_PINGADDRCONF
  if (dev->d_ipaddr == 0 && BUF->proto == UIP_PROTO_ICMP && ICMPBUF->type == ICMP_ECHO)
    {
      return UIP_FW_LOCAL;
    }
#endif /* UIP_PINGADDRCONF */

  /* Check if the packet is in the forwarding cache already, and if so
     we drop it. */

  for (fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
    if (fw->timer != 0 &&
#if UIP_REASSEMBLY > 0
       fw->len == BUF->len &&
       fw->offset == BUF->ipoffset &&
#endif
       fw->ipid == BUF->ipid &&
       fw->srcipaddr == BUF->srcipaddr &&
       fw->destipaddr == BUF->destipaddr &&
#if notdef
       fw->payload[0] == BUF->srcport &&
       fw->payload[1] == BUF->destport &&
#endif
       fw->proto == BUF->proto) {
      /* Drop packet. */
      return UIP_FW_FORWARDED;
    }
  }

  /* If the TTL reaches zero we produce an ICMP time exceeded message
     in the d_buf buffer and forward that packet back to the sender
     of the packet.
   */

  if (BUF->ttl <= 1)
    {
      /* No time exceeded for broadcasts and multicasts! */

      if (BUF->destipaddr == 0xffffffff)
        {
          return UIP_FW_LOCAL;
        }
      time_exceeded(dev);
    }

  /* Decrement the TTL (time-to-live) value in the IP header */
  BUF->ttl = BUF->ttl - 1;

  /* Update the IP checksum. */
  if (BUF->ipchksum >= HTONS(0xffff - 0x0100)) {
    BUF->ipchksum = BUF->ipchksum + HTONS(0x0100) + 1;
  } else {
    BUF->ipchksum = BUF->ipchksum + HTONS(0x0100);
  }

  if (dev->d_len > 0) {
    dev->d_appdata = &dev->d_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN];
    uip_fw_output(dev);
  }

#if UIP_BROADCAST
  if (BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
    return UIP_FW_LOCAL;
  }
#endif /* UIP_BROADCAST */

  /* Return non-zero to indicate that the packet was forwarded and that no
     other processing should be made. */
  return UIP_FW_FORWARDED;
}

/* Register a network interface with the forwarding module.
 *
 * netif A pointer to the network interface that is to be
 * registered.
 */

void uip_fw_register(struct uip_fw_netif *netif)
{
  netif->next = netifs;
  netifs = netif;
}

/* Register a default network interface.
 *
 * All packets that don't go out on any of the other interfaces will
 * be routed to the default interface.
 *
 * netif A pointer to the network interface that is to be
 * registered.
 */

void uip_fw_default(struct uip_fw_netif *netif)
{
  defaultnetif = netif;
}

/* Perform periodic processing. */

void uip_fw_periodic(void)
{
  struct fwcache_entry *fw;
  for (fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
    if (fw->timer > 0) {
      --fw->timer;
    }
  }
}