Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Examples

uip-fw.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * Author: Adam Dunkels <adam@sics.se>
00032  *
00033  * $Id: uip-fw.c,v 1.1 2006/06/17 22:41:19 adamdunkels Exp $
00034  */
00035 /**
00036  * \addtogroup uip
00037  * @{
00038  */
00039 
00040 /**
00041  * \defgroup uipfw uIP packet forwarding
00042  * @{
00043  *
00044  */
00045 
00046 /**
00047  * \file
00048  * uIP packet forwarding.
00049  * \author Adam Dunkels <adam@sics.se>
00050  *
00051  * This file implements a number of simple functions which do packet
00052  * forwarding over multiple network interfaces with uIP.
00053  *
00054  */
00055 
00056 #include "net/uip.h"
00057 #include "net/uip_arch.h"
00058 #include "net/uip-fw.h"
00059 #include "contiki-conf.h"
00060 
00061 #include <string.h> /* for memcpy() */
00062 
00063 /*
00064  * The list of registered network interfaces.
00065  */
00066 static struct uip_fw_netif *netifs = NULL;
00067 
00068 /*
00069  * A pointer to the default network interface.
00070  */
00071 static struct uip_fw_netif *defaultnetif = NULL;
00072 
00073 struct tcpip_hdr {
00074   /* IP header. */
00075   u8_t vhl,
00076     tos;
00077   u16_t len,
00078     ipid,
00079     ipoffset;
00080   u8_t ttl,
00081     proto;
00082   u16_t ipchksum;
00083   u16_t srcipaddr[2],
00084     destipaddr[2];
00085   
00086   /* TCP header. */
00087   u16_t srcport,
00088     destport;
00089   u8_t seqno[4],
00090     ackno[4],
00091     tcpoffset,
00092     flags,
00093     wnd[2];
00094   u16_t tcpchksum;
00095   u8_t urgp[2];
00096   u8_t optdata[4];
00097 };
00098 
00099 struct icmpip_hdr {
00100   /* IP header. */
00101   u8_t vhl,
00102     tos,
00103     len[2],
00104     ipid[2],
00105     ipoffset[2],
00106     ttl,
00107     proto;
00108   u16_t ipchksum;
00109   u16_t srcipaddr[2],
00110     destipaddr[2];
00111   /* ICMP (echo) header. */
00112   u8_t type, icode;
00113   u16_t icmpchksum;
00114   u16_t id, seqno;
00115   u8_t payload[1];
00116 };
00117 
00118 /* ICMP ECHO. */
00119 #define ICMP_ECHO 8
00120 
00121 /* ICMP TIME-EXCEEDED. */
00122 #define ICMP_TE 11
00123 
00124 /*
00125  * Pointer to the TCP/IP headers of the packet in the uip_buf buffer.
00126  */
00127 #define BUF ((struct tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
00128 
00129 /*
00130  * Pointer to the ICMP/IP headers of the packet in the uip_buf buffer.
00131  */
00132 #define ICMPBUF ((struct icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
00133 
00134 /*
00135  * Certain fields of an IP packet that are used for identifying
00136  * duplicate packets.
00137  */
00138 struct fwcache_entry {
00139   u16_t timer;
00140   
00141   u16_t srcipaddr[2];
00142   u16_t destipaddr[2];
00143   u16_t ipid;
00144   u8_t proto;
00145   u8_t unused;
00146 
00147 #if notdef
00148   u16_t payload[2];
00149 #endif
00150 
00151 #if UIP_REASSEMBLY > 0
00152   u16_t len, offset;
00153 #endif
00154 };
00155 
00156 /*
00157  * The number of packets to remember when looking for duplicates.
00158  */
00159 #ifdef UIP_CONF_FWCACHE_SIZE
00160 #define FWCACHE_SIZE UIP_CONF_FWCACHE_SIZE
00161 #else
00162 #define FWCACHE_SIZE 2
00163 #endif
00164 
00165 
00166 /*
00167  * A cache of packet header fields which are used for
00168  * identifying duplicate packets.
00169  */
00170 static struct fwcache_entry fwcache[FWCACHE_SIZE];
00171 
00172 /**
00173  * \internal
00174  * The time that a packet cache is active.
00175  */
00176 #define FW_TIME 20
00177 
00178 /*------------------------------------------------------------------------------*/
00179 /**
00180  * Initialize the uIP packet forwarding module.
00181  */
00182 /*------------------------------------------------------------------------------*/
00183 void
00184 uip_fw_init(void)
00185 {
00186   struct uip_fw_netif *t;
00187   defaultnetif = NULL;
00188   while(netifs != NULL) {
00189     t = netifs;
00190     netifs = netifs->next;
00191     t->next = NULL;
00192   }
00193 }
00194 /*------------------------------------------------------------------------------*/
00195 /**
00196  * \internal
00197  * Check if an IP address is within the network defined by an IP
00198  * address and a netmask.
00199  *
00200  * \param ipaddr The IP address to be checked.
00201  * \param netipaddr The IP address of the network.
00202  * \param netmask The netmask of the network.
00203  *
00204  * \return Non-zero if IP address is in network, zero otherwise.
00205  */
00206 /*------------------------------------------------------------------------------*/
00207 static unsigned char
00208 ipaddr_maskcmp(u16_t *ipaddr, u16_t *netipaddr, u16_t *netmask)
00209 {
00210   return (ipaddr[0] & netmask [0]) == (netipaddr[0] & netmask[0]) &&
00211     (ipaddr[1] & netmask[1]) == (netipaddr[1] & netmask[1]);
00212 }
00213 /*------------------------------------------------------------------------------*/
00214 /**
00215  * \internal
00216  * Send out an ICMP TIME-EXCEEDED message.
00217  *
00218  * This function replaces the packet in the uip_buf buffer with the
00219  * ICMP packet.
00220  */
00221 /*------------------------------------------------------------------------------*/
00222 static void
00223 time_exceeded(void)
00224 {
00225   u16_t tmp16;
00226 
00227   /* We don't send out ICMP errors for ICMP messages. */
00228   if(ICMPBUF->proto == UIP_PROTO_ICMP) {
00229     uip_len = 0;
00230     return;
00231   }
00232   /* Copy fields from packet header into payload of this ICMP packet. */
00233   memcpy(&(ICMPBUF->payload[0]), ICMPBUF, 28);
00234 
00235   /* Set the ICMP type and code. */
00236   ICMPBUF->type = ICMP_TE;
00237   ICMPBUF->icode = 0;
00238 
00239   /* Calculate the ICMP checksum. */
00240   ICMPBUF->icmpchksum = 0;
00241   ICMPBUF->icmpchksum = ~uip_chksum((u16_t *)&(ICMPBUF->type), 36);
00242 
00243   /* Set the IP destination address to be the source address of the
00244      original packet. */
00245   tmp16= BUF->destipaddr[0];
00246   BUF->destipaddr[0] = BUF->srcipaddr[0];
00247   BUF->srcipaddr[0] = tmp16;
00248   tmp16 = BUF->destipaddr[1];
00249   BUF->destipaddr[1] = BUF->srcipaddr[1];
00250   BUF->srcipaddr[1] = tmp16;
00251 
00252   /* Set our IP address as the source address. */
00253   BUF->srcipaddr[0] = uip_hostaddr[0];
00254   BUF->srcipaddr[1] = uip_hostaddr[1];
00255 
00256   /* The size of the ICMP time exceeded packet is 36 + the size of the
00257      IP header (20) = 56. */
00258   uip_len = 56;
00259   ICMPBUF->len[0] = 0;
00260   ICMPBUF->len[1] = uip_len;
00261 
00262   /* Fill in the other fields in the IP header. */
00263   ICMPBUF->vhl = 0x45;
00264   ICMPBUF->tos = 0;
00265   ICMPBUF->ipoffset[0] = ICMPBUF->ipoffset[1] = 0;
00266   ICMPBUF->ttl  = UIP_TTL;
00267   ICMPBUF->proto = UIP_PROTO_ICMP;
00268   
00269   /* Calculate IP checksum. */
00270   ICMPBUF->ipchksum = 0;
00271   ICMPBUF->ipchksum = ~(uip_ipchksum());
00272 
00273 
00274 }
00275 /*------------------------------------------------------------------------------*/
00276 /**
00277  * \internal
00278  * Register a packet in the forwarding cache so that it won't be
00279  * forwarded again.
00280  */
00281 /*------------------------------------------------------------------------------*/
00282 static void
00283 fwcache_register(void)
00284 {
00285   struct fwcache_entry *fw;
00286   int i, oldest;
00287 
00288   oldest = FW_TIME;
00289   fw = NULL;
00290   
00291   /* Find the oldest entry in the cache. */
00292   for(i = 0; i < FWCACHE_SIZE; ++i) {
00293     if(fwcache[i].timer == 0) {
00294       fw = &fwcache[i];
00295       break;
00296     } else if(fwcache[i].timer <= oldest) {
00297       fw = &fwcache[i];
00298       oldest = fwcache[i].timer;
00299     }
00300   }
00301 
00302   fw->timer = FW_TIME;
00303   fw->ipid = BUF->ipid;
00304   fw->srcipaddr[0] = BUF->srcipaddr[0];
00305   fw->srcipaddr[1] = BUF->srcipaddr[1];
00306   fw->destipaddr[0] = BUF->destipaddr[0];
00307   fw->destipaddr[1] = BUF->destipaddr[1];
00308   fw->proto = BUF->proto;
00309 #if notdef
00310   fw->payload[0] = BUF->srcport;
00311   fw->payload[1] = BUF->destport;
00312 #endif
00313 #if UIP_REASSEMBLY > 0
00314   fw->len = BUF->len;
00315   fw->offset = BUF->ipoffset;
00316 #endif
00317 }
00318 /*------------------------------------------------------------------------------*/
00319 /**
00320  * \internal
00321  * Find a network interface for the IP packet in uip_buf.
00322  */
00323 /*------------------------------------------------------------------------------*/
00324 static struct uip_fw_netif *
00325 find_netif(void)
00326 {
00327   struct uip_fw_netif *netif;
00328   
00329   /* Walk through every network interface to check for a match. */
00330   for(netif = netifs; netif != NULL; netif = netif->next) {
00331     if(ipaddr_maskcmp(BUF->destipaddr, netif->ipaddr,
00332                       netif->netmask)) {
00333       /* If there was a match, we break the loop. */
00334       return netif;
00335     }
00336   }
00337   
00338   /* If no matching netif was found, we use default netif. */
00339   return defaultnetif;
00340 }
00341 /*------------------------------------------------------------------------------*/
00342 /**
00343  * Output an IP packet on the correct network interface.
00344  *
00345  * The IP packet should be present in the uip_buf buffer and its
00346  * length in the global uip_len variable.
00347  *
00348  * \retval UIP_FW_ZEROLEN Indicates that a zero-length packet
00349  * transmission was attempted and that no packet was sent.
00350  *
00351  * \retval UIP_FW_NOROUTE No suitable network interface could be found
00352  * for the outbound packet, and the packet was not sent.
00353  *
00354  * \return The return value from the actual network interface output
00355  * function is passed unmodified as a return value.
00356  */
00357 /*------------------------------------------------------------------------------*/
00358 u8_t
00359 uip_fw_output(void)
00360 {
00361   struct uip_fw_netif *netif;
00362 
00363   if(uip_len == 0) {
00364     return UIP_FW_ZEROLEN;
00365   }
00366 
00367   fwcache_register();
00368 
00369 #if UIP_BROADCAST
00370   /* Link local broadcasts go out on all interfaces. */
00371   if(/*BUF->proto == UIP_PROTO_UDP &&*/
00372      BUF->destipaddr[0] == 0xffff &&
00373      BUF->destipaddr[1] == 0xffff) {
00374     if(defaultnetif != NULL) {
00375       defaultnetif->output();
00376     }
00377     for(netif = netifs; netif != NULL; netif = netif->next) {
00378       netif->output();
00379     }
00380     return UIP_FW_OK;
00381   }
00382 #endif /* UIP_BROADCAST */
00383   
00384   netif = find_netif();
00385   /*  printf("uip_fw_output: netif %p ->output %p len %d\n", netif,
00386          netif->output,
00387          uip_len);*/
00388 
00389   if(netif == NULL) {
00390     return UIP_FW_NOROUTE;
00391   }
00392   /* If we now have found a suitable network interface, we call its
00393      output function to send out the packet. */
00394   return netif->output();
00395 }
00396 /*------------------------------------------------------------------------------*/
00397 /**
00398  * Forward an IP packet in the uip_buf buffer.
00399  *
00400  *
00401  *
00402  * \return UIP_FW_FORWARDED if the packet was forwarded, UIP_FW_LOCAL if
00403  * the packet should be processed locally.
00404  */
00405 /*------------------------------------------------------------------------------*/
00406 u8_t
00407 uip_fw_forward(void)
00408 {
00409   struct fwcache_entry *fw;
00410 
00411   /* First check if the packet is destined for ourselves and return 0
00412      to indicate that the packet should be processed locally. */
00413   if(BUF->destipaddr[0] == uip_hostaddr[0] &&
00414      BUF->destipaddr[1] == uip_hostaddr[1]) {
00415     return UIP_FW_LOCAL;
00416   }
00417 
00418   /* If we use ping IP address configuration, and our IP address is
00419      not yet configured, we should intercept all ICMP echo packets. */
00420 #if UIP_PINGADDRCONF
00421   if((uip_hostaddr[0] | uip_hostaddr[1]) == 0 &&
00422      BUF->proto == UIP_PROTO_ICMP &&
00423      ICMPBUF->type == ICMP_ECHO) {
00424     return UIP_FW_LOCAL;
00425   }
00426 #endif /* UIP_PINGADDRCONF */
00427 
00428   /* Check if the packet is in the forwarding cache already, and if so
00429      we drop it. */
00430 
00431   for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
00432     if(fw->timer != 0 &&
00433 #if UIP_REASSEMBLY > 0
00434        fw->len == BUF->len &&
00435        fw->offset == BUF->ipoffset &&
00436 #endif
00437        fw->ipid == BUF->ipid &&
00438        fw->srcipaddr[0] == BUF->srcipaddr[0] &&
00439        fw->srcipaddr[1] == BUF->srcipaddr[1] &&
00440        fw->destipaddr[0] == BUF->destipaddr[0] &&
00441        fw->destipaddr[1] == BUF->destipaddr[1] &&
00442 #if notdef
00443        fw->payload[0] == BUF->srcport &&
00444        fw->payload[1] == BUF->destport &&
00445 #endif
00446        fw->proto == BUF->proto) {
00447       /* Drop packet. */
00448       return UIP_FW_FORWARDED;
00449     }
00450   }
00451 
00452   /* If the TTL reaches zero we produce an ICMP time exceeded message
00453      in the uip_buf buffer and forward that packet back to the sender
00454      of the packet. */
00455   if(BUF->ttl <= 1) {
00456     /* No time exceeded for broadcasts and multicasts! */
00457     if(BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
00458       return UIP_FW_LOCAL;
00459     }
00460     time_exceeded();
00461   }
00462   
00463   /* Decrement the TTL (time-to-live) value in the IP header */
00464   BUF->ttl = BUF->ttl - 1;
00465   
00466   /* Update the IP checksum. */
00467   if(BUF->ipchksum >= HTONS(0xffff - 0x0100)) {
00468     BUF->ipchksum = BUF->ipchksum + HTONS(0x0100) + 1;
00469   } else {
00470     BUF->ipchksum = BUF->ipchksum + HTONS(0x0100);
00471   }
00472 
00473   if(uip_len > 0) {
00474     uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN];
00475     uip_fw_output();
00476   }
00477 
00478 #if UIP_BROADCAST
00479   if(BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
00480     return UIP_FW_LOCAL;
00481   }
00482 #endif /* UIP_BROADCAST */
00483 
00484   /* Return non-zero to indicate that the packet was forwarded and that no
00485      other processing should be made. */
00486   return UIP_FW_FORWARDED;
00487 }
00488 /*------------------------------------------------------------------------------*/
00489 /**
00490  * Register a network interface with the forwarding module.
00491  *
00492  * \param netif A pointer to the network interface that is to be
00493  * registered.
00494  */
00495 /*------------------------------------------------------------------------------*/
00496 void
00497 uip_fw_register(struct uip_fw_netif *netif)
00498 {
00499   netif->next = netifs;
00500   netifs = netif;
00501 }
00502 /*------------------------------------------------------------------------------*/
00503 /**
00504  * Register a default network interface.
00505  *
00506  * All packets that don't go out on any of the other interfaces will
00507  * be routed to the default interface.
00508  *
00509  * \param netif A pointer to the network interface that is to be
00510  * registered.
00511  */
00512 /*------------------------------------------------------------------------------*/
00513 void
00514 uip_fw_default(struct uip_fw_netif *netif)
00515 {
00516   defaultnetif = netif;
00517 }
00518 /*------------------------------------------------------------------------------*/
00519 /**
00520  * Perform periodic processing.
00521  */
00522 /*------------------------------------------------------------------------------*/
00523 void
00524 uip_fw_periodic(void)
00525 {
00526   struct fwcache_entry *fw;
00527   for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
00528     if(fw->timer > 0) {
00529       --fw->timer;
00530     }
00531   }
00532 }
00533 /*------------------------------------------------------------------------------*/
00534 /** @} */
00535 /** @} */

Generated on Thu Jun 22 17:45:43 2006 for Contiki 2.x by  doxygen 1.4.4