Contiki 2.5
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  * $Id: uip-fw.c,v 1.12 2010/10/19 18:29:04 adamdunkels Exp $
34  */
35 /**
36  * \addtogroup uip
37  * @{
38  */
39 
40 /**
41  * \defgroup uipfw uIP packet forwarding
42  * @{
43  *
44  */
45 
46 /**
47  * \file
48  * uIP packet forwarding.
49  * \author Adam Dunkels <adam@sics.se>
50  *
51  * This file implements a number of simple functions which do packet
52  * forwarding over multiple network interfaces with uIP.
53  *
54  */
55 
56 #include <string.h>
57 
58 #include "contiki-conf.h"
59 
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  u8_t vhl,
80  tos;
81  u16_t len,
82  ipid,
83  ipoffset;
84  u8_t ttl,
85  proto;
86  u16_t ipchksum;
87  uip_ipaddr_t srcipaddr, destipaddr;
88 
89  /* TCP header. */
90  u16_t srcport,
91  destport;
92  u8_t seqno[4],
93  ackno[4],
94  tcpoffset,
95  flags,
96  wnd[2];
97  u16_t tcpchksum;
98  u8_t urgp[2];
99  u8_t optdata[4];
100 };
101 
102 struct icmpip_hdr {
103  /* IP header. */
104  u8_t vhl,
105  tos,
106  len[2],
107  ipid[2],
108  ipoffset[2],
109  ttl,
110  proto;
111  u16_t ipchksum;
112  uip_ipaddr_t srcipaddr, destipaddr;
113  /* ICMP (echo) header. */
114  u8_t type, icode;
115  u16_t icmpchksum;
116  u16_t id, seqno;
117  u8_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  u16_t timer;
142 
143  uip_ipaddr_t srcipaddr;
144  uip_ipaddr_t destipaddr;
145  u16_t ipid;
146  u8_t proto;
147  u8_t unused;
148 
149 #if notdef
150  u16_t payload[2];
151 #endif
152 
153 #if UIP_REASSEMBLY > 0
154  u16_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(uip_ipaddr_t *ipaddr,
211  uip_ipaddr_t *netipaddr,
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((u16_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] = (u8_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(void)
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(&BUF->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  * Output an IP packet on the correct network interface.
340  *
341  * The IP packet should be present in the uip_buf buffer and its
342  * length in the global uip_len variable.
343  *
344  * \retval UIP_FW_ZEROLEN Indicates that a zero-length packet
345  * transmission was attempted and that no packet was sent.
346  *
347  * \retval UIP_FW_NOROUTE No suitable network interface could be found
348  * for the outbound packet, and the packet was not sent.
349  *
350  * \return The return value from the actual network interface output
351  * function is passed unmodified as a return value.
352  */
353 /*------------------------------------------------------------------------------*/
354 u8_t
356 {
357  struct uip_fw_netif *netif;
358 #if UIP_BROADCAST
359  const struct uip_udpip_hdr *udp = (void *)BUF;
360 #endif /* UIP_BROADCAST */
361 
362  if(uip_len == 0) {
363  return UIP_FW_ZEROLEN;
364  }
365 
366  fwcache_register();
367 
368 #if UIP_BROADCAST
369  /* Link local broadcasts go out on all interfaces. */
370  if(uip_ipaddr_cmp(&udp->destipaddr, &uip_broadcast_addr)) {
371  if(defaultnetif != NULL) {
372  defaultnetif->output();
373  }
374  for(netif = netifs; netif != NULL; netif = netif->next) {
375  netif->output();
376  }
377  return UIP_FW_OK;
378  }
379 #endif /* UIP_BROADCAST */
380 
381  netif = find_netif();
382  /* printf("uip_fw_output: netif %p ->output %p len %d\n", netif,
383  netif->output,
384  uip_len);*/
385 
386  if(netif == NULL) {
387  return UIP_FW_NOROUTE;
388  }
389  /* If we now have found a suitable network interface, we call its
390  output function to send out the packet. */
391  return netif->output();
392 }
393 /*------------------------------------------------------------------------------*/
394 /**
395  * Forward an IP packet in the uip_buf buffer.
396  *
397  *
398  *
399  * \return UIP_FW_FORWARDED if the packet was forwarded, UIP_FW_LOCAL if
400  * the packet should be processed locally.
401  */
402 /*------------------------------------------------------------------------------*/
403 u8_t
405 {
406  struct fwcache_entry *fw;
407 
408  /* First check if the packet is destined for ourselves and return 0
409  to indicate that the packet should be processed locally. */
410  if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) {
411  return UIP_FW_LOCAL;
412  }
413 
414 #ifdef AODV_COMPLIANCE
415 #define udp ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
416  if(udp->proto == UIP_PROTO_UDP && udp->destport == UIP_HTONS(UAODV_UDPPORT)) {
417  return UIP_FW_LOCAL;
418  }
419 #endif
420 
421  /* If we use ping IP address configuration, and our IP address is
422  not yet configured, we should intercept all ICMP echo packets. */
423 #if UIP_PINGADDRCONF
424  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr) &&
425  BUF->proto == UIP_PROTO_ICMP &&
426  ICMPBUF->type == ICMP_ECHO) {
427  return UIP_FW_LOCAL;
428  }
429 #endif /* UIP_PINGADDRCONF */
430 
431  /* Check if the packet is in the forwarding cache already, and if so
432  we drop it. */
433 
434  for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
435  if(fw->timer != 0 &&
436 #if UIP_REASSEMBLY > 0
437  fw->len == BUF->len &&
438  fw->offset == BUF->ipoffset &&
439 #endif
440  fw->ipid == BUF->ipid &&
441  uip_ipaddr_cmp(&fw->srcipaddr, &BUF->srcipaddr) &&
442  uip_ipaddr_cmp(&fw->destipaddr, &BUF->destipaddr) &&
443 #if notdef
444  fw->payload[0] == BUF->srcport &&
445  fw->payload[1] == BUF->destport &&
446 #endif
447  fw->proto == BUF->proto) {
448  /* Drop packet. */
449  return UIP_FW_FORWARDED;
450  }
451  }
452 
453  /* If the TTL reaches zero we produce an ICMP time exceeded message
454  in the uip_buf buffer and forward that packet back to the sender
455  of the packet. */
456 
457  if(BUF->ttl <= 1) {
458  /* No time exceeded for broadcasts and multicasts! */
459  if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) {
460  return UIP_FW_LOCAL;
461  }
462  time_exceeded();
463  }
464 
465  /* Decrement the TTL (time-to-live) value in the IP header */
466  BUF->ttl = BUF->ttl - 1;
467 
468  /* Update the IP checksum. */
469  if(BUF->ipchksum >= UIP_HTONS(0xffff - 0x0100)) {
470  BUF->ipchksum = BUF->ipchksum + UIP_HTONS(0x0100) + 1;
471  } else {
472  BUF->ipchksum = BUF->ipchksum + UIP_HTONS(0x0100);
473  }
474 
475  if(uip_len > 0) {
476  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN];
477  uip_fw_output();
478  }
479 
480 #if UIP_BROADCAST
481  if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) {
482  return UIP_FW_LOCAL;
483  }
484 #endif /* UIP_BROADCAST */
485 
486  /* Return non-zero to indicate that the packet was forwarded and that no
487  other processing should be made. */
488  return UIP_FW_FORWARDED;
489 }
490 /*------------------------------------------------------------------------------*/
491 /**
492  * Register a network interface with the forwarding module.
493  *
494  * \param netif A pointer to the network interface that is to be
495  * registered.
496  */
497 /*------------------------------------------------------------------------------*/
498 void
500 {
501  netif->next = netifs;
502  netifs = netif;
503 }
504 /*------------------------------------------------------------------------------*/
505 /**
506  * Register a default network interface.
507  *
508  * All packets that don't go out on any of the other interfaces will
509  * be routed to the default interface.
510  *
511  * \param netif A pointer to the network interface that is to be
512  * registered.
513  */
514 /*------------------------------------------------------------------------------*/
515 void
517 {
518  defaultnetif = netif;
519 }
520 /*------------------------------------------------------------------------------*/
521 /**
522  * Perform periodic processing.
523  */
524 /*------------------------------------------------------------------------------*/
525 void
527 {
528  struct fwcache_entry *fw;
529  for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
530  if(fw->timer > 0) {
531  --fw->timer;
532  }
533  }
534 }
535 /*------------------------------------------------------------------------------*/
536 /** @} */
537 /** @} */