EVE 1.0
uip-fw.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 /**
35  * \addtogroup uip
36  * @{
37  */
38 
39 /**
40  * \defgroup uipfw uIP packet forwarding
41  * @{
42  *
43  */
44 
45 /**
46  * \file
47  * uIP packet forwarding.
48  * \author Adam Dunkels <adam@sics.se>
49  *
50  * This file implements a number of simple functions which do packet
51  * forwarding over multiple network interfaces with uIP.
52  *
53  */
54 
55 #include <string.h>
56 
57 #include "contiki-conf.h"
58 
59 #include "lib/assert.h"
60 #include "net/uip.h"
61 #include "net/uip_arch.h"
62 #include "net/uip-fw.h"
63 #ifdef AODV_COMPLIANCE
64 #include "net/uaodv-def.h"
65 #endif
66 
67 /*
68  * The list of registered network interfaces.
69  */
70 static struct uip_fw_netif *netifs = NULL;
71 
72 /*
73  * A pointer to the default network interface.
74  */
75 static struct uip_fw_netif *defaultnetif = NULL;
76 
77 struct tcpip_hdr {
78  /* IP header. */
79  uint8_t vhl,
80  tos;
81  uint16_t len,
82  ipid,
83  ipoffset;
84  uint8_t ttl,
85  proto;
86  uint16_t ipchksum;
87  uip_ipaddr_t srcipaddr, destipaddr;
88 
89  /* TCP header. */
90  uint16_t srcport,
91  destport;
92  uint8_t seqno[4],
93  ackno[4],
94  tcpoffset,
95  flags,
96  wnd[2];
97  uint16_t tcpchksum;
98  uint8_t urgp[2];
99  uint8_t optdata[4];
100 };
101 
102 struct icmpip_hdr {
103  /* IP header. */
104  uint8_t vhl,
105  tos,
106  len[2],
107  ipid[2],
108  ipoffset[2],
109  ttl,
110  proto;
111  uint16_t ipchksum;
112  uip_ipaddr_t srcipaddr, destipaddr;
113  /* ICMP (echo) header. */
114  uint8_t type, icode;
115  uint16_t icmpchksum;
116  uint16_t id, seqno;
117  uint8_t payload[1];
118 };
119 
120 /* ICMP ECHO. */
121 #define ICMP_ECHO 8
122 
123 /* ICMP TIME-EXCEEDED. */
124 #define ICMP_TE 11
125 
126 /*
127  * Pointer to the TCP/IP headers of the packet in the uip_buf buffer.
128  */
129 #define BUF ((struct tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
130 
131 /*
132  * Pointer to the ICMP/IP headers of the packet in the uip_buf buffer.
133  */
134 #define ICMPBUF ((struct icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
135 
136 /*
137  * Certain fields of an IP packet that are used for identifying
138  * duplicate packets.
139  */
140 struct fwcache_entry {
141  uint16_t timer;
142 
143  uip_ipaddr_t srcipaddr;
144  uip_ipaddr_t destipaddr;
145  uint16_t ipid;
146  uint8_t proto;
147  uint8_t unused;
148 
149 #if notdef
150  uint16_t payload[2];
151 #endif
152 
153 #if UIP_REASSEMBLY > 0
154  uint16_t len, offset;
155 #endif
156 };
157 
158 /*
159  * The number of packets to remember when looking for duplicates.
160  */
161 #ifdef UIP_CONF_FWCACHE_SIZE
162 #define FWCACHE_SIZE UIP_CONF_FWCACHE_SIZE
163 #else
164 #define FWCACHE_SIZE 2
165 #endif
166 
167 
168 /*
169  * A cache of packet header fields which are used for
170  * identifying duplicate packets.
171  */
172 static struct fwcache_entry fwcache[FWCACHE_SIZE];
173 
174 /**
175  * \internal
176  * The time that a packet cache is active.
177  */
178 #define FW_TIME 20
179 
180 /*------------------------------------------------------------------------------*/
181 /**
182  * Initialize the uIP packet forwarding module.
183  */
184 /*------------------------------------------------------------------------------*/
185 void
187 {
188  struct uip_fw_netif *t;
189  defaultnetif = NULL;
190  while(netifs != NULL) {
191  t = netifs;
192  netifs = netifs->next;
193  t->next = NULL;
194  }
195 }
196 /*------------------------------------------------------------------------------*/
197 /**
198  * \internal
199  * Check if an IP address is within the network defined by an IP
200  * address and a netmask.
201  *
202  * \param ipaddr The IP address to be checked.
203  * \param netipaddr The IP address of the network.
204  * \param netmask The netmask of the network.
205  *
206  * \return Non-zero if IP address is in network, zero otherwise.
207  */
208 /*------------------------------------------------------------------------------*/
209 static unsigned char
210 ipaddr_maskcmp(const uip_ipaddr_t *ipaddr,
211  const uip_ipaddr_t *netipaddr,
212  const uip_ipaddr_t *netmask)
213 {
214  return (ipaddr->u16[0] & netmask->u16[0]) == (netipaddr->u16[0] & netmask->u16[0]) &&
215  (ipaddr->u16[1] & netmask->u16[1]) == (netipaddr->u16[1] & netmask->u16[1]);
216 }
217 /*------------------------------------------------------------------------------*/
218 /**
219  * \internal
220  * Send out an ICMP TIME-EXCEEDED message.
221  *
222  * This function replaces the packet in the uip_buf buffer with the
223  * ICMP packet.
224  */
225 /*------------------------------------------------------------------------------*/
226 static void
227 time_exceeded(void)
228 {
229 
230  /* We don't send out ICMP errors for ICMP messages (unless they are pings). */
231  if(ICMPBUF->proto == UIP_PROTO_ICMP &&
232  ICMPBUF->type != ICMP_ECHO) {
233  uip_len = 0;
234  return;
235  }
236  /* Copy fields from packet header into payload of this ICMP packet. */
237  memcpy(&(ICMPBUF->payload[0]), ICMPBUF, UIP_IPH_LEN + 8);
238 
239  /* Set the ICMP type and code. */
240  ICMPBUF->type = ICMP_TE;
241  ICMPBUF->icode = 0;
242 
243  /* Calculate the ICMP checksum. */
244  ICMPBUF->icmpchksum = 0;
245  ICMPBUF->icmpchksum = ~uip_chksum((uint16_t *)&(ICMPBUF->type), 36);
246 
247  /* Set the IP destination address to be the source address of the
248  original packet. */
249  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
250 
251  /* Set our IP address as the source address. */
252  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
253 
254  /* The size of the ICMP time exceeded packet is 36 + the size of the
255  IP header (20) = 56. */
256  uip_len = 56;
257  ICMPBUF->len[0] = 0;
258  ICMPBUF->len[1] = (uint8_t)uip_len;
259 
260  /* Fill in the other fields in the IP header. */
261  ICMPBUF->vhl = 0x45;
262  ICMPBUF->tos = 0;
263  ICMPBUF->ipoffset[0] = ICMPBUF->ipoffset[1] = 0;
264  ICMPBUF->ttl = UIP_TTL;
265  ICMPBUF->proto = UIP_PROTO_ICMP;
266 
267  /* Calculate IP checksum. */
268  ICMPBUF->ipchksum = 0;
269  ICMPBUF->ipchksum = ~(uip_ipchksum());
270 
271 
272 }
273 /*------------------------------------------------------------------------------*/
274 /**
275  * \internal
276  * Register a packet in the forwarding cache so that it won't be
277  * forwarded again.
278  */
279 /*------------------------------------------------------------------------------*/
280 static void
281 fwcache_register(void)
282 {
283  struct fwcache_entry *fw;
284  int i, oldest;
285 
286  oldest = FW_TIME;
287  fw = NULL;
288 
289  /* Find the oldest entry in the cache. */
290  for(i = 0; i < FWCACHE_SIZE; ++i) {
291  if(fwcache[i].timer == 0) {
292  fw = &fwcache[i];
293  break;
294  } else if(fwcache[i].timer <= oldest) {
295  fw = &fwcache[i];
296  oldest = fwcache[i].timer;
297  }
298  }
299 
300  fw->timer = FW_TIME;
301  fw->ipid = BUF->ipid;
302  uip_ipaddr_copy(&fw->srcipaddr, &BUF->srcipaddr);
303  uip_ipaddr_copy(&fw->destipaddr, &BUF->destipaddr);
304  fw->proto = BUF->proto;
305 #if notdef
306  fw->payload[0] = BUF->srcport;
307  fw->payload[1] = BUF->destport;
308 #endif
309 #if UIP_REASSEMBLY > 0
310  fw->len = BUF->len;
311  fw->offset = BUF->ipoffset;
312 #endif
313 }
314 /*------------------------------------------------------------------------------*/
315 /**
316  * \internal
317  * Find a network interface for the IP packet in uip_buf.
318  */
319 /*------------------------------------------------------------------------------*/
320 static struct uip_fw_netif *
321 find_netif(const uip_ipaddr_t *destipaddr)
322 {
323  struct uip_fw_netif *netif;
324 
325  /* Walk through every network interface to check for a match. */
326  for(netif = netifs; netif != NULL; netif = netif->next) {
327  if(ipaddr_maskcmp(destipaddr, &netif->ipaddr,
328  &netif->netmask)) {
329  /* If there was a match, we break the loop. */
330  return netif;
331  }
332  }
333 
334  /* If no matching netif was found, we use default netif. */
335  return defaultnetif;
336 }
337 /*------------------------------------------------------------------------------*/
338 /**
339  * TODO: describe me!!!
340  */
341 /*------------------------------------------------------------------------------*/
342 bool
343 uip_fw_quote(const uip_ipaddr_t *destipaddr)
344 {
345  bool quote = true;
346  struct uip_fw_netif *netif;
347 
348 #if UIP_BROADCAST
349  assert(!uip_ipaddr_cmp(destipaddr, &uip_broadcast_addr));
350 #endif /* UIP_BROADCAST */
351 
352  netif = find_netif(destipaddr);
353  if(netif != NULL && netif->quote != NULL)
354  quote = netif->quote(netif);
355 
356  return quote;
357 }
358 /*------------------------------------------------------------------------------*/
359 /**
360  * Output an IP packet on the correct network interface.
361  *
362  * The IP packet should be present in the uip_buf buffer and its
363  * length in the global uip_len variable.
364  *
365  * \retval UIP_FW_ZEROLEN Indicates that a zero-length packet
366  * transmission was attempted and that no packet was sent.
367  *
368  * \retval UIP_FW_NOROUTE No suitable network interface could be found
369  * for the outbound packet, and the packet was not sent.
370  *
371  * \return The return value from the actual network interface output
372  * function is passed unmodified as a return value.
373  */
374 /*------------------------------------------------------------------------------*/
375 uint8_t
377 {
378  struct uip_fw_netif *netif;
379 #if UIP_BROADCAST
380  const struct uip_udpip_hdr *udp = (void *)BUF;
381 #endif /* UIP_BROADCAST */
382 
383  if(uip_len == 0) {
384  return UIP_FW_ZEROLEN;
385  }
386 
387  fwcache_register();
388 
389 #if UIP_BROADCAST
390  /* Link local broadcasts go out on all interfaces. */
391  if(uip_ipaddr_cmp(&udp->destipaddr, &uip_broadcast_addr) || /* Broadcast */
392  (udp->destipaddr.u8[3] >= 224 && udp->destipaddr.u8[3] <= 239)) { /* Multicast */
393  if(defaultnetif != NULL) {
394  defaultnetif->output(defaultnetif);
395  }
396  for(netif = netifs; netif != NULL; netif = netif->next) {
397  if (netif != defaultnetif) {
398  netif->output(netif);
399  }
400  }
401  return UIP_FW_OK;
402  }
403 #endif /* UIP_BROADCAST */
404 
405  netif = find_netif(&BUF->destipaddr);
406  /* printf("uip_fw_output: netif %p ->output %p len %d\n", netif,
407  netif->output,
408  uip_len);*/
409 
410  if(netif == NULL) {
411  return UIP_FW_NOROUTE;
412  }
413  /* If we now have found a suitable network interface, we call its
414  output function to send out the packet. */
415  return netif->output(netif);
416 }
417 /*------------------------------------------------------------------------------*/
418 /**
419  * Forward an IP packet in the uip_buf buffer.
420  *
421  *
422  *
423  * \return UIP_FW_FORWARDED if the packet was forwarded, UIP_FW_LOCAL if
424  * the packet should be processed locally.
425  */
426 /*------------------------------------------------------------------------------*/
427 uint8_t
429 {
430  struct fwcache_entry *fw;
431 
432  /* First check if the packet is destined for ourselves and return 0
433  to indicate that the packet should be processed locally. */
434  if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) {
435  return UIP_FW_LOCAL;
436  }
437 
438 #ifdef AODV_COMPLIANCE
439 #define udp ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
440  if(udp->proto == UIP_PROTO_UDP && udp->destport == UIP_HTONS(UAODV_UDPPORT)) {
441  return UIP_FW_LOCAL;
442  }
443 #endif
444 
445  /* If we use ping IP address configuration, and our IP address is
446  not yet configured, we should intercept all ICMP echo packets. */
447 #if UIP_PINGADDRCONF
448  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr) &&
449  BUF->proto == UIP_PROTO_ICMP &&
450  ICMPBUF->type == ICMP_ECHO) {
451  return UIP_FW_LOCAL;
452  }
453 #endif /* UIP_PINGADDRCONF */
454 
455  /* Check if the packet is in the forwarding cache already, and if so
456  we drop it. */
457 
458  for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
459  if(fw->timer != 0 &&
460 #if UIP_REASSEMBLY > 0
461  fw->len == BUF->len &&
462  fw->offset == BUF->ipoffset &&
463 #endif
464  fw->ipid == BUF->ipid &&
465  uip_ipaddr_cmp(&fw->srcipaddr, &BUF->srcipaddr) &&
466  uip_ipaddr_cmp(&fw->destipaddr, &BUF->destipaddr) &&
467 #if notdef
468  fw->payload[0] == BUF->srcport &&
469  fw->payload[1] == BUF->destport &&
470 #endif
471  fw->proto == BUF->proto) {
472  /* Drop packet. */
473  return UIP_FW_FORWARDED;
474  }
475  }
476 
477  /* If the TTL reaches zero we produce an ICMP time exceeded message
478  in the uip_buf buffer and forward that packet back to the sender
479  of the packet. */
480 
481  if(BUF->ttl <= 1) {
482  /* No time exceeded for broadcasts and multicasts! */
483  if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) {
484  return UIP_FW_LOCAL;
485  }
486  time_exceeded();
487  }
488 
489  /* Decrement the TTL (time-to-live) value in the IP header */
490  BUF->ttl = BUF->ttl - 1;
491 
492  /* Update the IP checksum. */
493  if(BUF->ipchksum >= UIP_HTONS(0xffff - 0x0100)) {
494  BUF->ipchksum = BUF->ipchksum + UIP_HTONS(0x0100) + 1;
495  } else {
496  BUF->ipchksum = BUF->ipchksum + UIP_HTONS(0x0100);
497  }
498 
499  if(uip_len > 0) {
500  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN];
501  uip_fw_output();
502  }
503 
504 #if UIP_BROADCAST
505  if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) {
506  return UIP_FW_LOCAL;
507  }
508 #endif /* UIP_BROADCAST */
509 
510  /* Return non-zero to indicate that the packet was forwarded and that no
511  other processing should be made. */
512  return UIP_FW_FORWARDED;
513 }
514 /*------------------------------------------------------------------------------*/
515 /**
516  * Register a network interface with the forwarding module.
517  *
518  * \param netif A pointer to the network interface that is to be
519  * registered.
520  */
521 /*------------------------------------------------------------------------------*/
522 void
524 {
525  netif->next = netifs;
526  netifs = netif;
527 }
528 /*------------------------------------------------------------------------------*/
529 /**
530  * Register a default network interface.
531  *
532  * All packets that don't go out on any of the other interfaces will
533  * be routed to the default interface.
534  *
535  * \param netif A pointer to the network interface that is to be
536  * registered.
537  */
538 /*------------------------------------------------------------------------------*/
539 void
541 {
542  defaultnetif = netif;
543 }
544 /*------------------------------------------------------------------------------*/
545 /**
546  * Perform periodic processing.
547  */
548 /*------------------------------------------------------------------------------*/
549 void
551 {
552  struct fwcache_entry *fw;
553  for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
554  if(fw->timer > 0) {
555  --fw->timer;
556  }
557  }
558 }
559 /*------------------------------------------------------------------------------*/
560 /** @} */
561 /** @} */
uint8_t uip_fw_forward(void)
Definition: uip-fw.c:428
#define UIP_FW_ZEROLEN
Definition: uip-fw.h:149
#define UIP_REASSEMBLY
Definition: uipopt.h:274
void uip_fw_default(struct uip_fw_netif *netif)
Definition: uip-fw.c:540
void uip_fw_register(struct uip_fw_netif *netif)
Definition: uip-fw.c:523
bool uip_fw_quote(const uip_ipaddr_t *destipaddr)
Definition: uip-fw.c:343
#define UIP_FW_OK
Definition: uip-fw.h:134
uint16_t uip_chksum(uint16_t *data, uint16_t len)
Definition: uip.c:328
#define UIP_FW_FORWARDED
Definition: uip-fw.h:141
bool(* quote)(struct uip_fw_netif *netif)
Definition: uip-fw.h:61
Definition: timer.h:86
void uip_fw_periodic(void)
Definition: uip-fw.c:550
uint16_t uip_ipchksum(void)
Definition: uip.c:335
uint16_t uip_len
Definition: uip.c:166
#define UIP_FW_LOCAL
Definition: uip-fw.h:127
uint8_t uip_fw_output(void)
Definition: uip-fw.c:376
#define UIP_LLH_LEN
Definition: uipopt.h:162
#define uip_ipaddr_copy(dest, src)
Definition: uip.h:1036
#define UIP_FW_NOROUTE
Definition: uip-fw.h:165
uint8_t(* output)(struct uip_fw_netif *netif)
Definition: uip-fw.h:58
#define UIP_HTONS(n)
Definition: uip.h:1248
struct uip_fw_netif * next
Definition: uip-fw.h:54
void uip_fw_init(void)
Definition: uip-fw.c:186
uip_ipaddr_t netmask
Definition: uip-fw.h:57
#define UIP_TTL
Definition: uipopt.h:248
uip_ipaddr_t ipaddr
Definition: uip-fw.h:56
void * uip_appdata
Definition: uip.c:154