2 * Copyright (c) 2004, Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * This file is part of the uIP TCP/IP stack
31 * Author: Adam Dunkels <adam@sics.se>
33 * $Id: uip-fw.c,v 1.1 2008/08/06 23:21:19 andrewlxia Exp $
41 * \defgroup uipfw uIP packet forwarding
48 * uIP packet forwarding.
49 * \author Adam Dunkels <adam@sics.se>
51 * This file implements a number of simple functions which do packet
52 * forwarding over multiple network interfaces with uIP.
57 #include <uip/uip_arch.h>
58 #include <uip/uip-fw.h>
60 #include <string.h> /* for memcpy() */
63 * The list of registered network interfaces.
65 static struct uip_fw_netif *netifs = NULL;
68 * A pointer to the default network interface.
70 static struct uip_fw_netif *defaultnetif = NULL;
110 /* ICMP (echo) header. */
120 /* ICMP TIME-EXCEEDED. */
124 * Pointer to the TCP/IP headers of the packet in the uip_buf buffer.
126 #define BUF ((struct tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
129 * Pointer to the ICMP/IP headers of the packet in the uip_buf buffer.
131 #define ICMPBUF ((struct icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
134 * Certain fields of an IP packet that are used for identifying
137 struct fwcache_entry {
150 #if UIP_REASSEMBLY > 0
156 * The number of packets to remember when looking for duplicates.
158 #ifdef UIP_CONF_FWCACHE_SIZE
159 #define FWCACHE_SIZE UIP_CONF_FWCACHE_SIZE
161 #define FWCACHE_SIZE 2
166 * A cache of packet header fields which are used for
167 * identifying duplicate packets.
169 static struct fwcache_entry fwcache[FWCACHE_SIZE];
173 * The time that a packet cache is active.
177 /*------------------------------------------------------------------------------*/
179 * Initialize the uIP packet forwarding module.
181 /*------------------------------------------------------------------------------*/
185 struct uip_fw_netif *t;
187 while(netifs != NULL) {
189 netifs = netifs->next;
193 /*------------------------------------------------------------------------------*/
196 * Check if an IP address is within the network defined by an IP
197 * address and a netmask.
199 * \param ipaddr The IP address to be checked.
200 * \param netipaddr The IP address of the network.
201 * \param netmask The netmask of the network.
203 * \return Non-zero if IP address is in network, zero otherwise.
205 /*------------------------------------------------------------------------------*/
207 ipaddr_maskcmp(u16_t *ipaddr, u16_t *netipaddr, u16_t *netmask)
209 return (ipaddr[0] & netmask [0]) == (netipaddr[0] & netmask[0]) &&
210 (ipaddr[1] & netmask[1]) == (netipaddr[1] & netmask[1]);
212 /*------------------------------------------------------------------------------*/
215 * Send out an ICMP TIME-EXCEEDED message.
217 * This function replaces the packet in the uip_buf buffer with the
220 /*------------------------------------------------------------------------------*/
226 /* We don't send out ICMP errors for ICMP messages. */
227 if(ICMPBUF->proto == UIP_PROTO_ICMP) {
231 /* Copy fields from packet header into payload of this ICMP packet. */
232 memcpy(&(ICMPBUF->payload[0]), ICMPBUF, 28);
234 /* Set the ICMP type and code. */
235 ICMPBUF->type = ICMP_TE;
238 /* Calculate the ICMP checksum. */
239 ICMPBUF->icmpchksum = 0;
240 ICMPBUF->icmpchksum = ~uip_chksum((u16_t *)&(ICMPBUF->type), 36);
242 /* Set the IP destination address to be the source address of the
244 tmp16= BUF->destipaddr[0];
245 BUF->destipaddr[0] = BUF->srcipaddr[0];
246 BUF->srcipaddr[0] = tmp16;
247 tmp16 = BUF->destipaddr[1];
248 BUF->destipaddr[1] = BUF->srcipaddr[1];
249 BUF->srcipaddr[1] = tmp16;
251 /* Set our IP address as the source address. */
252 BUF->srcipaddr[0] = uip_hostaddr[0];
253 BUF->srcipaddr[1] = uip_hostaddr[1];
255 /* The size of the ICMP time exceeded packet is 36 + the size of the
256 IP header (20) = 56. */
259 ICMPBUF->len[1] = uip_len;
261 /* Fill in the other fields in the IP header. */
264 ICMPBUF->ipoffset[0] = ICMPBUF->ipoffset[1] = 0;
265 ICMPBUF->ttl = UIP_TTL;
266 ICMPBUF->proto = UIP_PROTO_ICMP;
268 /* Calculate IP checksum. */
269 ICMPBUF->ipchksum = 0;
270 ICMPBUF->ipchksum = ~(uip_ipchksum());
274 /*------------------------------------------------------------------------------*/
277 * Register a packet in the forwarding cache so that it won't be
280 /*------------------------------------------------------------------------------*/
282 fwcache_register(void)
284 struct fwcache_entry *fw;
290 /* Find the oldest entry in the cache. */
291 for(i = 0; i < FWCACHE_SIZE; ++i) {
292 if(fwcache[i].timer == 0) {
295 } else if(fwcache[i].timer <= oldest) {
297 oldest = fwcache[i].timer;
302 fw->ipid = BUF->ipid;
303 fw->srcipaddr[0] = BUF->srcipaddr[0];
304 fw->srcipaddr[1] = BUF->srcipaddr[1];
305 fw->destipaddr[0] = BUF->destipaddr[0];
306 fw->destipaddr[1] = BUF->destipaddr[1];
307 fw->proto = BUF->proto;
309 fw->payload[0] = BUF->srcport;
310 fw->payload[1] = BUF->destport;
312 #if UIP_REASSEMBLY > 0
314 fw->offset = BUF->ipoffset;
317 /*------------------------------------------------------------------------------*/
320 * Find a network interface for the IP packet in uip_buf.
322 /*------------------------------------------------------------------------------*/
323 static struct uip_fw_netif *
326 struct uip_fw_netif *netif;
328 /* Walk through every network interface to check for a match. */
329 for(netif = netifs; netif != NULL; netif = netif->next) {
330 if(ipaddr_maskcmp(BUF->destipaddr, netif->ipaddr,
332 /* If there was a match, we break the loop. */
337 /* If no matching netif was found, we use default netif. */
340 /*------------------------------------------------------------------------------*/
342 * Output an IP packet on the correct network interface.
344 * The IP packet should be present in the uip_buf buffer and its
345 * length in the global uip_len variable.
347 * \retval UIP_FW_ZEROLEN Indicates that a zero-length packet
348 * transmission was attempted and that no packet was sent.
350 * \retval UIP_FW_NOROUTE No suitable network interface could be found
351 * for the outbound packet, and the packet was not sent.
353 * \return The return value from the actual network interface output
354 * function is passed unmodified as a return value.
356 /*------------------------------------------------------------------------------*/
360 struct uip_fw_netif *netif;
363 return UIP_FW_ZEROLEN;
369 /* Link local broadcasts go out on all interfaces. */
370 if(/*BUF->proto == UIP_PROTO_UDP &&*/
371 BUF->destipaddr[0] == 0xffff &&
372 BUF->destipaddr[1] == 0xffff) {
373 if(defaultnetif != NULL) {
374 defaultnetif->output();
376 for(netif = netifs; netif != NULL; netif = netif->next) {
381 #endif /* UIP_BROADCAST */
383 netif = find_netif();
384 /* printf("uip_fw_output: netif %p ->output %p len %d\n", netif,
389 return UIP_FW_NOROUTE;
391 /* If we now have found a suitable network interface, we call its
392 output function to send out the packet. */
393 return netif->output();
395 /*------------------------------------------------------------------------------*/
397 * Forward an IP packet in the uip_buf buffer.
401 * \return UIP_FW_FORWARDED if the packet was forwarded, UIP_FW_LOCAL if
402 * the packet should be processed locally.
404 /*------------------------------------------------------------------------------*/
408 struct fwcache_entry *fw;
410 /* First check if the packet is destined for ourselves and return 0
411 to indicate that the packet should be processed locally. */
412 if(BUF->destipaddr[0] == uip_hostaddr[0] &&
413 BUF->destipaddr[1] == uip_hostaddr[1]) {
417 /* If we use ping IP address configuration, and our IP address is
418 not yet configured, we should intercept all ICMP echo packets. */
420 if((uip_hostaddr[0] | uip_hostaddr[1]) == 0 &&
421 BUF->proto == UIP_PROTO_ICMP &&
422 ICMPBUF->type == ICMP_ECHO) {
425 #endif /* UIP_PINGADDRCONF */
427 /* Check if the packet is in the forwarding cache already, and if so
430 for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
432 #if UIP_REASSEMBLY > 0
433 fw->len == BUF->len &&
434 fw->offset == BUF->ipoffset &&
436 fw->ipid == BUF->ipid &&
437 fw->srcipaddr[0] == BUF->srcipaddr[0] &&
438 fw->srcipaddr[1] == BUF->srcipaddr[1] &&
439 fw->destipaddr[0] == BUF->destipaddr[0] &&
440 fw->destipaddr[1] == BUF->destipaddr[1] &&
442 fw->payload[0] == BUF->srcport &&
443 fw->payload[1] == BUF->destport &&
445 fw->proto == BUF->proto) {
447 return UIP_FW_FORWARDED;
451 /* If the TTL reaches zero we produce an ICMP time exceeded message
452 in the uip_buf buffer and forward that packet back to the sender
455 /* No time exceeded for broadcasts and multicasts! */
456 if(BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
462 /* Decrement the TTL (time-to-live) value in the IP header */
463 BUF->ttl = BUF->ttl - 1;
465 /* Update the IP checksum. */
466 if(BUF->ipchksum >= HTONS(0xffff - 0x0100)) {
467 BUF->ipchksum = BUF->ipchksum + HTONS(0x0100) + 1;
469 BUF->ipchksum = BUF->ipchksum + HTONS(0x0100);
473 uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN];
478 if(BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
481 #endif /* UIP_BROADCAST */
483 /* Return non-zero to indicate that the packet was forwarded and that no
484 other processing should be made. */
485 return UIP_FW_FORWARDED;
487 /*------------------------------------------------------------------------------*/
489 * Register a network interface with the forwarding module.
491 * \param netif A pointer to the network interface that is to be
494 /*------------------------------------------------------------------------------*/
496 uip_fw_register(struct uip_fw_netif *netif)
498 netif->next = netifs;
501 /*------------------------------------------------------------------------------*/
503 * Register a default network interface.
505 * All packets that don't go out on any of the other interfaces will
506 * be routed to the default interface.
508 * \param netif A pointer to the network interface that is to be
511 /*------------------------------------------------------------------------------*/
513 uip_fw_default(struct uip_fw_netif *netif)
515 defaultnetif = netif;
517 /*------------------------------------------------------------------------------*/
519 * Perform periodic processing.
521 /*------------------------------------------------------------------------------*/
523 uip_fw_periodic(void)
525 struct fwcache_entry *fw;
526 for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
532 /*------------------------------------------------------------------------------*/