Contiki 3.x
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 "net/ip/uip.h"
60 #include "net/ip/uip_arch.h"
61 #include "net/ipv4/uip-fw.h"
62 #ifdef AODV_COMPLIANCE
63 #include "net/ipv4/uaodv-def.h"
64 #endif
65 
66 /*
67  * The list of registered network interfaces.
68  */
69 static struct uip_fw_netif *netifs = NULL;
70 
71 /*
72  * A pointer to the default network interface.
73  */
74 static struct uip_fw_netif *defaultnetif = NULL;
75 
76 struct tcpip_hdr {
77  /* IP header. */
78  uint8_t vhl,
79  tos;
80  uint16_t len,
81  ipid,
82  ipoffset;
83  uint8_t ttl,
84  proto;
85  uint16_t ipchksum;
86  uip_ipaddr_t srcipaddr, destipaddr;
87 
88  /* TCP header. */
89  uint16_t srcport,
90  destport;
91  uint8_t seqno[4],
92  ackno[4],
93  tcpoffset,
94  flags,
95  wnd[2];
96  uint16_t tcpchksum;
97  uint8_t urgp[2];
98  uint8_t optdata[4];
99 };
100 
101 struct icmpip_hdr {
102  /* IP header. */
103  uint8_t vhl,
104  tos,
105  len[2],
106  ipid[2],
107  ipoffset[2],
108  ttl,
109  proto;
110  uint16_t ipchksum;
111  uip_ipaddr_t srcipaddr, destipaddr;
112  /* ICMP (echo) header. */
113  uint8_t type, icode;
114  uint16_t icmpchksum;
115  uint16_t id, seqno;
116  uint8_t payload[1];
117 };
118 
119 /* ICMP ECHO. */
120 #define ICMP_ECHO 8
121 
122 /* ICMP TIME-EXCEEDED. */
123 #define ICMP_TE 11
124 
125 /*
126  * Pointer to the TCP/IP headers of the packet in the uip_buf buffer.
127  */
128 #define BUF ((struct tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
129 
130 /*
131  * Pointer to the ICMP/IP headers of the packet in the uip_buf buffer.
132  */
133 #define ICMPBUF ((struct icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
134 
135 /*
136  * Certain fields of an IP packet that are used for identifying
137  * duplicate packets.
138  */
139 struct fwcache_entry {
140  uint16_t timer;
141 
142  uip_ipaddr_t srcipaddr;
143  uip_ipaddr_t destipaddr;
144  uint16_t ipid;
145  uint8_t proto;
146  uint8_t unused;
147 
148 #if notdef
149  uint16_t payload[2];
150 #endif
151 
152 #if UIP_REASSEMBLY > 0
153  uint16_t len, offset;
154 #endif
155 };
156 
157 /*
158  * The number of packets to remember when looking for duplicates.
159  */
160 #ifdef UIP_CONF_FWCACHE_SIZE
161 #define FWCACHE_SIZE UIP_CONF_FWCACHE_SIZE
162 #else
163 #define FWCACHE_SIZE 2
164 #endif
165 
166 
167 /*
168  * A cache of packet header fields which are used for
169  * identifying duplicate packets.
170  */
171 static struct fwcache_entry fwcache[FWCACHE_SIZE];
172 
173 /**
174  * \internal
175  * The time that a packet cache is active.
176  */
177 #define FW_TIME 20
178 
179 /*------------------------------------------------------------------------------*/
180 /**
181  * Initialize the uIP packet forwarding module.
182  */
183 /*------------------------------------------------------------------------------*/
184 void
186 {
187  struct uip_fw_netif *t;
188  defaultnetif = NULL;
189  while(netifs != NULL) {
190  t = netifs;
191  netifs = netifs->next;
192  t->next = NULL;
193  }
194 }
195 /*------------------------------------------------------------------------------*/
196 /**
197  * \internal
198  * Check if an IP address is within the network defined by an IP
199  * address and a netmask.
200  *
201  * \param ipaddr The IP address to be checked.
202  * \param netipaddr The IP address of the network.
203  * \param netmask The netmask of the network.
204  *
205  * \return Non-zero if IP address is in network, zero otherwise.
206  */
207 /*------------------------------------------------------------------------------*/
208 static unsigned char
209 ipaddr_maskcmp(uip_ipaddr_t *ipaddr,
210  uip_ipaddr_t *netipaddr,
211  uip_ipaddr_t *netmask)
212 {
213  return (ipaddr->u16[0] & netmask->u16[0]) == (netipaddr->u16[0] & netmask->u16[0]) &&
214  (ipaddr->u16[1] & netmask->u16[1]) == (netipaddr->u16[1] & netmask->u16[1]);
215 }
216 /*------------------------------------------------------------------------------*/
217 /**
218  * \internal
219  * Send out an ICMP TIME-EXCEEDED message.
220  *
221  * This function replaces the packet in the uip_buf buffer with the
222  * ICMP packet.
223  */
224 /*------------------------------------------------------------------------------*/
225 static void
226 time_exceeded(void)
227 {
228 
229  /* We don't send out ICMP errors for ICMP messages (unless they are pings). */
230  if(ICMPBUF->proto == UIP_PROTO_ICMP &&
231  ICMPBUF->type != ICMP_ECHO) {
232  uip_len = 0;
233  return;
234  }
235  /* Copy fields from packet header into payload of this ICMP packet. */
236  memcpy(&(ICMPBUF->payload[0]), ICMPBUF, UIP_IPH_LEN + 8);
237 
238  /* Set the ICMP type and code. */
239  ICMPBUF->type = ICMP_TE;
240  ICMPBUF->icode = 0;
241 
242  /* Calculate the ICMP checksum. */
243  ICMPBUF->icmpchksum = 0;
244  ICMPBUF->icmpchksum = ~uip_chksum((uint16_t *)&(ICMPBUF->type), 36);
245 
246  /* Set the IP destination address to be the source address of the
247  original packet. */
248  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
249 
250  /* Set our IP address as the source address. */
251  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
252 
253  /* The size of the ICMP time exceeded packet is 36 + the size of the
254  IP header (20) = 56. */
255  uip_len = 56;
256  ICMPBUF->len[0] = 0;
257  ICMPBUF->len[1] = (uint8_t)uip_len;
258 
259  /* Fill in the other fields in the IP header. */
260  ICMPBUF->vhl = 0x45;
261  ICMPBUF->tos = 0;
262  ICMPBUF->ipoffset[0] = ICMPBUF->ipoffset[1] = 0;
263  ICMPBUF->ttl = UIP_TTL;
264  ICMPBUF->proto = UIP_PROTO_ICMP;
265 
266  /* Calculate IP checksum. */
267  ICMPBUF->ipchksum = 0;
268  ICMPBUF->ipchksum = ~(uip_ipchksum());
269 
270 
271 }
272 /*------------------------------------------------------------------------------*/
273 /**
274  * \internal
275  * Register a packet in the forwarding cache so that it won't be
276  * forwarded again.
277  */
278 /*------------------------------------------------------------------------------*/
279 static void
280 fwcache_register(void)
281 {
282  struct fwcache_entry *fw;
283  int i, oldest;
284 
285  oldest = FW_TIME;
286  fw = NULL;
287 
288  /* Find the oldest entry in the cache. */
289  for(i = 0; i < FWCACHE_SIZE; ++i) {
290  if(fwcache[i].timer == 0) {
291  fw = &fwcache[i];
292  break;
293  } else if(fwcache[i].timer <= oldest) {
294  fw = &fwcache[i];
295  oldest = fwcache[i].timer;
296  }
297  }
298 
299  fw->timer = FW_TIME;
300  fw->ipid = BUF->ipid;
301  uip_ipaddr_copy(&fw->srcipaddr, &BUF->srcipaddr);
302  uip_ipaddr_copy(&fw->destipaddr, &BUF->destipaddr);
303  fw->proto = BUF->proto;
304 #if notdef
305  fw->payload[0] = BUF->srcport;
306  fw->payload[1] = BUF->destport;
307 #endif
308 #if UIP_REASSEMBLY > 0
309  fw->len = BUF->len;
310  fw->offset = BUF->ipoffset;
311 #endif
312 }
313 /*------------------------------------------------------------------------------*/
314 /**
315  * \internal
316  * Find a network interface for the IP packet in uip_buf.
317  */
318 /*------------------------------------------------------------------------------*/
319 static struct uip_fw_netif *
320 find_netif(void)
321 {
322  struct uip_fw_netif *netif;
323 
324  /* Walk through every network interface to check for a match. */
325  for(netif = netifs; netif != NULL; netif = netif->next) {
326  if(ipaddr_maskcmp(&BUF->destipaddr, &netif->ipaddr,
327  &netif->netmask)) {
328  /* If there was a match, we break the loop. */
329  return netif;
330  }
331  }
332 
333  /* If no matching netif was found, we use default netif. */
334  return defaultnetif;
335 }
336 /*------------------------------------------------------------------------------*/
337 /**
338  * Output an IP packet on the correct network interface.
339  *
340  * The IP packet should be present in the uip_buf buffer and its
341  * length in the global uip_len variable.
342  *
343  * \retval UIP_FW_ZEROLEN Indicates that a zero-length packet
344  * transmission was attempted and that no packet was sent.
345  *
346  * \retval UIP_FW_NOROUTE No suitable network interface could be found
347  * for the outbound packet, and the packet was not sent.
348  *
349  * \return The return value from the actual network interface output
350  * function is passed unmodified as a return value.
351  */
352 /*------------------------------------------------------------------------------*/
353 uint8_t
355 {
356  struct uip_fw_netif *netif;
357 #if UIP_BROADCAST
358  const struct uip_udpip_hdr *udp = (void *)BUF;
359 #endif /* UIP_BROADCAST */
360 
361  if(uip_len == 0) {
362  return UIP_FW_ZEROLEN;
363  }
364 
365  fwcache_register();
366 
367 #if UIP_BROADCAST
368  /* Link local broadcasts go out on all interfaces. */
369  if(uip_ipaddr_cmp(&udp->destipaddr, &uip_broadcast_addr)) {
370  if(defaultnetif != NULL) {
371  defaultnetif->output();
372  }
373  for(netif = netifs; netif != NULL; netif = netif->next) {
374  netif->output();
375  }
376  return UIP_FW_OK;
377  }
378 #endif /* UIP_BROADCAST */
379 
380  netif = find_netif();
381  /* printf("uip_fw_output: netif %p ->output %p len %d\n", netif,
382  netif->output,
383  uip_len);*/
384 
385  if(netif == NULL) {
386  return UIP_FW_NOROUTE;
387  }
388  /* If we now have found a suitable network interface, we call its
389  output function to send out the packet. */
390  return netif->output();
391 }
392 /*------------------------------------------------------------------------------*/
393 /**
394  * Forward an IP packet in the uip_buf buffer.
395  *
396  *
397  *
398  * \return UIP_FW_FORWARDED if the packet was forwarded, UIP_FW_LOCAL if
399  * the packet should be processed locally.
400  */
401 /*------------------------------------------------------------------------------*/
402 uint8_t
404 {
405  struct fwcache_entry *fw;
406 
407  /* First check if the packet is destined for ourselves and return 0
408  to indicate that the packet should be processed locally. */
409  if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) {
410  return UIP_FW_LOCAL;
411  }
412 
413 #ifdef AODV_COMPLIANCE
414 #define udp ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
415  if(udp->proto == UIP_PROTO_UDP && udp->destport == UIP_HTONS(UAODV_UDPPORT)) {
416  return UIP_FW_LOCAL;
417  }
418 #endif
419 
420  /* If we use ping IP address configuration, and our IP address is
421  not yet configured, we should intercept all ICMP echo packets. */
422 #if UIP_PINGADDRCONF
423  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr) &&
424  BUF->proto == UIP_PROTO_ICMP &&
425  ICMPBUF->type == ICMP_ECHO) {
426  return UIP_FW_LOCAL;
427  }
428 #endif /* UIP_PINGADDRCONF */
429 
430  /* Check if the packet is in the forwarding cache already, and if so
431  we drop it. */
432 
433  for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
434  if(fw->timer != 0 &&
435 #if UIP_REASSEMBLY > 0
436  fw->len == BUF->len &&
437  fw->offset == BUF->ipoffset &&
438 #endif
439  fw->ipid == BUF->ipid &&
440  uip_ipaddr_cmp(&fw->srcipaddr, &BUF->srcipaddr) &&
441  uip_ipaddr_cmp(&fw->destipaddr, &BUF->destipaddr) &&
442 #if notdef
443  fw->payload[0] == BUF->srcport &&
444  fw->payload[1] == BUF->destport &&
445 #endif
446  fw->proto == BUF->proto) {
447  /* Drop packet. */
448  return UIP_FW_FORWARDED;
449  }
450  }
451 
452  /* If the TTL reaches zero we produce an ICMP time exceeded message
453  in the uip_buf buffer and forward that packet back to the sender
454  of the packet. */
455 
456  if(BUF->ttl <= 1) {
457  /* No time exceeded for broadcasts and multicasts! */
458  if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) {
459  return UIP_FW_LOCAL;
460  }
461  time_exceeded();
462  }
463 
464  /* Decrement the TTL (time-to-live) value in the IP header */
465  BUF->ttl = BUF->ttl - 1;
466 
467  /* Update the IP checksum. */
468  if(BUF->ipchksum >= UIP_HTONS(0xffff - 0x0100)) {
469  BUF->ipchksum = BUF->ipchksum + UIP_HTONS(0x0100) + 1;
470  } else {
471  BUF->ipchksum = BUF->ipchksum + UIP_HTONS(0x0100);
472  }
473 
474  if(uip_len > 0) {
475  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN];
476  uip_fw_output();
477  }
478 
479 #if UIP_BROADCAST
480  if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) {
481  return UIP_FW_LOCAL;
482  }
483 #endif /* UIP_BROADCAST */
484 
485  /* Return non-zero to indicate that the packet was forwarded and that no
486  other processing should be made. */
487  return UIP_FW_FORWARDED;
488 }
489 /*------------------------------------------------------------------------------*/
490 /**
491  * Register a network interface with the forwarding module.
492  *
493  * \param netif A pointer to the network interface that is to be
494  * registered.
495  */
496 /*------------------------------------------------------------------------------*/
497 void
499 {
500  netif->next = netifs;
501  netifs = netif;
502 }
503 /*------------------------------------------------------------------------------*/
504 /**
505  * Register a default network interface.
506  *
507  * All packets that don't go out on any of the other interfaces will
508  * be routed to the default interface.
509  *
510  * \param netif A pointer to the network interface that is to be
511  * registered.
512  */
513 /*------------------------------------------------------------------------------*/
514 void
516 {
517  defaultnetif = netif;
518 }
519 /*------------------------------------------------------------------------------*/
520 /**
521  * Perform periodic processing.
522  */
523 /*------------------------------------------------------------------------------*/
524 void
526 {
527  struct fwcache_entry *fw;
528  for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
529  if(fw->timer > 0) {
530  --fw->timer;
531  }
532  }
533 }
534 /*------------------------------------------------------------------------------*/
535 /** @} */
536 /** @} */
uint16_t uip_ipchksum(void)
Calculate the IP header checksum of the packet header in uip_buf.
Definition: uip6.c:343
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
uIP packet forwarding header file.
A timer.
Definition: timer.h:86
Representation of a uIP network interface.
Definition: uip-fw.h:54
struct uip_fw_netif * next
Pointer to the next interface when linked in a list.
Definition: uip-fw.h:55
uint8_t(* output)(void)
A pointer to the function that sends a packet.
Definition: uip-fw.h:59
uip_ipaddr_t ipaddr
The IP address of this interface.
Definition: uip-fw.h:57
Header file for the uIP TCP/IP stack.
void uip_fw_init(void)
Initialize the uIP packet forwarding module.
Definition: uip-fw.c:185
#define UIP_REASSEMBLY
Turn on support for IP packet reassembly.
Definition: uipopt.h:271
uint16_t uip_chksum(uint16_t *buf, uint16_t len)
Calculate the Internet checksum over a buffer.
Definition: uip6.c:336
#define NULL
The null pointer.
#define UIP_FW_ZEROLEN
A non-error message that indicates that a zero-length packet transmission was attempted, and that no packet was sent.
Definition: uip-fw.h:147
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1238
uint8_t uip_fw_forward(void)
Forward an IP packet in the uip_buf buffer.
Definition: uip-fw.c:403
#define UIP_LLH_LEN
The link level header length.
Definition: uipopt.h:160
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
void uip_fw_default(struct uip_fw_netif *netif)
Register a default network interface.
Definition: uip-fw.c:515
void uip_fw_register(struct uip_fw_netif *netif)
Register a network interface with the forwarding module.
Definition: uip-fw.c:498
uint8_t uip_fw_output(void)
Output an IP packet on the correct network interface.
Definition: uip-fw.c:354
uip_ipaddr_t netmask
The netmask of the interface.
Definition: uip-fw.h:58
#define UIP_FW_FORWARDED
A non-error message that indicates that a packet was forwarded.
Definition: uip-fw.h:139
void uip_fw_periodic(void)
Perform periodic processing.
Definition: uip-fw.c:525
Definitions for the micro implementation of the AODV ad hoc routing protocol ...
#define UIP_TTL
The IP TTL (time to live) of IP packets sent by uIP.
Definition: uipopt.h:245
#define UIP_FW_LOCAL
A non-error message that indicates that a packet should be processed locally.
Definition: uip-fw.h:125
#define UIP_FW_OK
A non-error message that indicates that something went OK.
Definition: uip-fw.h:132
Declarations of architecture specific functions.
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74
#define UIP_FW_NOROUTE
An error message that indicates that no suitable interface could be found for an outbound packet...
Definition: uip-fw.h:163