2007-08-27 01:12:17 +02:00
|
|
|
/* uip-fw.c
|
|
|
|
* uIP packet forwarding.
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
* This file implements a number of simple functions which do packet
|
|
|
|
* forwarding over multiple network interfaces with uIP.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <debug.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
#include <net/uip/uip.h>
|
|
|
|
#include <net/uip/uip-arch.h>
|
|
|
|
|
|
|
|
#include "uip-fw.h"
|
|
|
|
|
|
|
|
#include <string.h> /* for memcpy() */
|
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
/* The list of registered network interfaces. */
|
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
static struct uip_fw_netif *netifs = NULL;
|
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
/* A pointer to the default network interface. */
|
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
static struct uip_fw_netif *defaultnetif = NULL;
|
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
struct tcpip_hdr
|
|
|
|
{
|
2007-09-17 00:12:04 +02:00
|
|
|
/* IP header */
|
|
|
|
|
|
|
|
uint8 vhl;
|
|
|
|
uint8 tos;
|
|
|
|
uint16 len;
|
|
|
|
uint16 ipid;
|
|
|
|
uint16 ipoffset;
|
|
|
|
uint8 ttl;
|
|
|
|
uint8 proto;
|
2007-08-27 01:12:17 +02:00
|
|
|
uint16 ipchksum;
|
2007-09-17 00:12:04 +02:00
|
|
|
in_addr_t srcipaddr;
|
|
|
|
in_addr_t destipaddr;
|
|
|
|
|
|
|
|
/* TCP header */
|
|
|
|
|
|
|
|
uint16 srcport;
|
|
|
|
uint16 destport;
|
|
|
|
uint8 seqno[4];
|
|
|
|
uint8 ackno[4];
|
|
|
|
uint8 tcpoffset;
|
|
|
|
uint8 flags;
|
|
|
|
uint8 wnd[2];
|
2007-08-27 01:12:17 +02:00
|
|
|
uint16 tcpchksum;
|
2007-09-17 00:12:04 +02:00
|
|
|
uint8 urgp[2];
|
2007-08-27 01:12:17 +02:00
|
|
|
uint8 optdata[4];
|
|
|
|
};
|
|
|
|
|
2007-09-17 00:12:04 +02:00
|
|
|
struct icmpip_hdr
|
|
|
|
{
|
|
|
|
/* IP header */
|
|
|
|
|
|
|
|
uint8 vhl;
|
|
|
|
uint8 tos;
|
|
|
|
uint8 len[2];
|
|
|
|
uint8 ipid[2];
|
|
|
|
uint8 ipoffset[2];
|
|
|
|
uint8 ttl;
|
|
|
|
uint8 proto;
|
2007-08-27 01:12:17 +02:00
|
|
|
uint16 ipchksum;
|
2007-09-17 00:12:04 +02:00
|
|
|
in_addr_t srcipaddr;
|
|
|
|
in_addr_t destipaddr;
|
|
|
|
|
|
|
|
/* ICMP (echo) header */
|
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
uint8 type, icode;
|
|
|
|
uint16 icmpchksum;
|
|
|
|
uint16 id, seqno;
|
|
|
|
uint8 payload[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ICMP ECHO. */
|
|
|
|
#define ICMP_ECHO 8
|
|
|
|
|
|
|
|
/* ICMP TIME-EXCEEDED. */
|
|
|
|
#define ICMP_TE 11
|
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
/* Pointer to the TCP/IP headers of the packet in the d_buf buffer. */
|
|
|
|
#define BUF ((struct tcpip_hdr *)&dev->d_buf[UIP_LLH_LEN])
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
/* Pointer to the ICMP/IP headers of the packet in the d_buf buffer. */
|
|
|
|
#define ICMPBUF ((struct icmpip_hdr *)&dev->d_buf[UIP_LLH_LEN])
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
/* Certain fields of an IP packet that are used for identifying
|
2007-08-27 01:12:17 +02:00
|
|
|
* duplicate packets.
|
|
|
|
*/
|
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
struct fwcache_entry
|
|
|
|
{
|
|
|
|
uint16 timer;
|
2007-09-17 00:12:04 +02:00
|
|
|
in_addr_t srcipaddr;
|
|
|
|
in_addr_t destipaddr;
|
2007-08-27 01:12:17 +02:00
|
|
|
uint16 ipid;
|
|
|
|
uint8 proto;
|
|
|
|
uint8 unused;
|
|
|
|
|
|
|
|
#if notdef
|
|
|
|
uint16 payload[2];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if UIP_REASSEMBLY > 0
|
|
|
|
uint16 len, offset;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
/* The number of packets to remember when looking for duplicates. */
|
2007-09-01 20:06:15 +02:00
|
|
|
#ifdef CONFIG_NET_FWCACHE_SIZE
|
|
|
|
# define FWCACHE_SIZE CONFIG_NET_FWCACHE_SIZE
|
2007-08-27 01:12:17 +02:00
|
|
|
#else
|
|
|
|
# define FWCACHE_SIZE 2
|
|
|
|
#endif
|
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
/* A cache of packet header fields which are used for
|
2007-08-27 01:12:17 +02:00
|
|
|
* identifying duplicate packets.
|
|
|
|
*/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
static struct fwcache_entry fwcache[FWCACHE_SIZE];
|
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
/* The time that a packet cache is active. */
|
2007-08-27 01:12:17 +02:00
|
|
|
#define FW_TIME 20
|
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
/* Initialize the uIP packet forwarding module. */
|
|
|
|
|
|
|
|
void uip_fw_init(void)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
|
|
|
struct uip_fw_netif *t;
|
|
|
|
defaultnetif = NULL;
|
|
|
|
while(netifs != NULL) {
|
|
|
|
t = netifs;
|
|
|
|
netifs = netifs->next;
|
|
|
|
t->next = NULL;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
/* Send out an ICMP TIME-EXCEEDED message.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-09-16 00:45:45 +02:00
|
|
|
* This function replaces the packet in the d_buf buffer with the
|
2007-08-27 01:12:17 +02:00
|
|
|
* ICMP packet.
|
|
|
|
*/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
static void time_exceeded(struct uip_driver_s *dev)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-09-17 00:12:04 +02:00
|
|
|
in_addr_t tmp_addr;
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
/* We don't send out ICMP errors for ICMP messages. */
|
2007-09-17 00:12:04 +02:00
|
|
|
if (ICMPBUF->proto == UIP_PROTO_ICMP) {
|
2007-09-16 00:45:45 +02:00
|
|
|
dev->d_len = 0;
|
2007-08-27 01:12:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Copy fields from packet header into payload of this ICMP packet. */
|
|
|
|
memcpy(&(ICMPBUF->payload[0]), ICMPBUF, 28);
|
|
|
|
|
|
|
|
/* Set the ICMP type and code. */
|
|
|
|
ICMPBUF->type = ICMP_TE;
|
|
|
|
ICMPBUF->icode = 0;
|
|
|
|
|
|
|
|
/* Calculate the ICMP checksum. */
|
|
|
|
ICMPBUF->icmpchksum = 0;
|
|
|
|
ICMPBUF->icmpchksum = ~uip_chksum((uint16 *)&(ICMPBUF->type), 36);
|
|
|
|
|
|
|
|
/* Set the IP destination address to be the source address of the
|
2007-09-17 00:12:04 +02:00
|
|
|
* original packet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tmp_addr = BUF->destipaddr;
|
|
|
|
BUF->destipaddr = BUF->srcipaddr;
|
|
|
|
BUF->srcipaddr = tmp_addr;
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
/* Set our IP address as the source address. */
|
2007-09-17 00:12:04 +02:00
|
|
|
|
|
|
|
BUF->srcipaddr = dev->d_ipaddr;
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
/* The size of the ICMP time exceeded packet is 36 + the size of the
|
|
|
|
IP header (20) = 56. */
|
2007-09-16 00:45:45 +02:00
|
|
|
dev->d_len = 56;
|
2007-08-27 01:12:17 +02:00
|
|
|
ICMPBUF->len[0] = 0;
|
2007-09-16 00:45:45 +02:00
|
|
|
ICMPBUF->len[1] = dev->d_len;
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
/* Fill in the other fields in the IP header. */
|
|
|
|
ICMPBUF->vhl = 0x45;
|
|
|
|
ICMPBUF->tos = 0;
|
|
|
|
ICMPBUF->ipoffset[0] = ICMPBUF->ipoffset[1] = 0;
|
|
|
|
ICMPBUF->ttl = UIP_TTL;
|
|
|
|
ICMPBUF->proto = UIP_PROTO_ICMP;
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
/* Calculate IP checksum. */
|
|
|
|
ICMPBUF->ipchksum = 0;
|
2007-09-16 00:45:45 +02:00
|
|
|
ICMPBUF->ipchksum = ~(uip_ipchksum(dev));
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
/* Register a packet in the forwarding cache so that it won't be
|
2007-08-27 01:12:17 +02:00
|
|
|
* forwarded again.
|
|
|
|
*/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
static void fwcache_register(struct uip_driver_s *dev)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
|
|
|
struct fwcache_entry *fw;
|
|
|
|
int i, oldest;
|
|
|
|
|
|
|
|
oldest = FW_TIME;
|
|
|
|
fw = NULL;
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
/* Find the oldest entry in the cache. */
|
2007-09-17 00:12:04 +02:00
|
|
|
for (i = 0; i < FWCACHE_SIZE; ++i) {
|
|
|
|
if (fwcache[i].timer == 0) {
|
2007-08-27 01:12:17 +02:00
|
|
|
fw = &fwcache[i];
|
|
|
|
break;
|
2007-09-17 00:12:04 +02:00
|
|
|
} else if (fwcache[i].timer <= oldest) {
|
2007-08-27 01:12:17 +02:00
|
|
|
fw = &fwcache[i];
|
|
|
|
oldest = fwcache[i].timer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fw->timer = FW_TIME;
|
|
|
|
fw->ipid = BUF->ipid;
|
2007-09-17 00:12:04 +02:00
|
|
|
fw->srcipaddr = BUF->srcipaddr;
|
|
|
|
fw->destipaddr = BUF->destipaddr;
|
2007-08-27 01:12:17 +02:00
|
|
|
fw->proto = BUF->proto;
|
|
|
|
#if notdef
|
|
|
|
fw->payload[0] = BUF->srcport;
|
|
|
|
fw->payload[1] = BUF->destport;
|
|
|
|
#endif
|
|
|
|
#if UIP_REASSEMBLY > 0
|
|
|
|
fw->len = BUF->len;
|
|
|
|
fw->offset = BUF->ipoffset;
|
|
|
|
#endif
|
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
/* Find a network interface for the IP packet in d_buf. */
|
|
|
|
|
2007-09-17 00:12:04 +02:00
|
|
|
static struct uip_fw_netif *find_netif (struct uip_driver_s *dev)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
|
|
|
struct uip_fw_netif *netif;
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
/* Walk through every network interface to check for a match. */
|
2007-09-17 00:12:04 +02:00
|
|
|
|
|
|
|
for (netif = netifs; netif != NULL; netif = netif->next)
|
|
|
|
{
|
|
|
|
if (uip_ipaddr_maskcmp(BUF->destipaddr, netif->ipaddr, netif->netmask))
|
|
|
|
{
|
|
|
|
/* If there was a match, we break the loop. */
|
|
|
|
|
|
|
|
return netif;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
/* If no matching netif was found, we use default netif. */
|
2007-09-17 00:12:04 +02:00
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
return defaultnetif;
|
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
/* Output an IP packet on the correct network interface.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-09-16 00:45:45 +02:00
|
|
|
* The IP packet should be present in the d_buf buffer and its
|
|
|
|
* length in the d_len field.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-09-16 00:45:45 +02:00
|
|
|
* Return: UIP_FW_ZEROLEN Indicates that a zero-length packet
|
2007-08-27 01:12:17 +02:00
|
|
|
* transmission was attempted and that no packet was sent.
|
|
|
|
*
|
2007-09-16 00:45:45 +02:00
|
|
|
* Return: UIP_FW_NOROUTE No suitable network interface could be found
|
2007-08-27 01:12:17 +02:00
|
|
|
* for the outbound packet, and the packet was not sent.
|
|
|
|
*
|
2007-09-16 00:45:45 +02:00
|
|
|
* Return: The return value from the actual network interface output
|
2007-08-27 01:12:17 +02:00
|
|
|
* function is passed unmodified as a return value.
|
|
|
|
*/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
uint8 uip_fw_output(struct uip_driver_s *dev)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
|
|
|
struct uip_fw_netif *netif;
|
|
|
|
|
2007-09-17 00:12:04 +02:00
|
|
|
if (dev->d_len == 0) {
|
2007-08-27 01:12:17 +02:00
|
|
|
return UIP_FW_ZEROLEN;
|
|
|
|
}
|
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
fwcache_register(dev);
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
#if UIP_BROADCAST
|
|
|
|
/* Link local broadcasts go out on all interfaces. */
|
2007-09-17 00:12:04 +02:00
|
|
|
if (/*BUF->proto == UIP_PROTO_UDP &&*/
|
2007-08-27 01:12:17 +02:00
|
|
|
BUF->destipaddr[0] == 0xffff &&
|
|
|
|
BUF->destipaddr[1] == 0xffff) {
|
2007-09-17 00:12:04 +02:00
|
|
|
if (defaultnetif != NULL) {
|
2007-08-27 01:12:17 +02:00
|
|
|
defaultnetif->output();
|
|
|
|
}
|
2007-09-17 00:12:04 +02:00
|
|
|
for (netif = netifs; netif != NULL; netif = netif->next) {
|
2007-08-27 01:12:17 +02:00
|
|
|
netif->output();
|
|
|
|
}
|
|
|
|
return UIP_FW_OK;
|
|
|
|
}
|
|
|
|
#endif /* UIP_BROADCAST */
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-09-17 00:12:04 +02:00
|
|
|
netif = find_netif (dev);
|
2007-09-16 00:45:45 +02:00
|
|
|
dbg("uip_fw_output: netif %p ->output %p len %d\n", netif, netif->output, dev->d_len);
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-17 00:12:04 +02:00
|
|
|
if (netif == NULL) {
|
2007-08-27 01:12:17 +02:00
|
|
|
return UIP_FW_NOROUTE;
|
|
|
|
}
|
|
|
|
/* If we now have found a suitable network interface, we call its
|
|
|
|
output function to send out the packet. */
|
|
|
|
return netif->output();
|
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
/* Forward an IP packet in the d_buf buffer.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-09-16 00:45:45 +02:00
|
|
|
* Return: UIP_FW_FORWARDED if the packet was forwarded, UIP_FW_LOCAL if
|
2007-08-27 01:12:17 +02:00
|
|
|
* the packet should be processed locally.
|
|
|
|
*/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
uint8 uip_fw_forward(struct uip_driver_s *dev)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
|
|
|
struct fwcache_entry *fw;
|
|
|
|
|
|
|
|
/* First check if the packet is destined for ourselves and return 0
|
2007-09-17 00:12:04 +02:00
|
|
|
* to indicate that the packet should be processed locally.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (BUF->destipaddr == dev->d_ipaddr)
|
|
|
|
{
|
|
|
|
return UIP_FW_LOCAL;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
/* If we use ping IP address configuration, and our IP address is
|
|
|
|
not yet configured, we should intercept all ICMP echo packets. */
|
|
|
|
#if UIP_PINGADDRCONF
|
2007-09-17 00:12:04 +02:00
|
|
|
if (dev->d_ipaddr == 0 && BUF->proto == UIP_PROTO_ICMP && ICMPBUF->type == ICMP_ECHO)
|
|
|
|
{
|
|
|
|
return UIP_FW_LOCAL;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
#endif /* UIP_PINGADDRCONF */
|
|
|
|
|
|
|
|
/* Check if the packet is in the forwarding cache already, and if so
|
|
|
|
we drop it. */
|
|
|
|
|
2007-09-17 00:12:04 +02:00
|
|
|
for (fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
|
|
|
|
if (fw->timer != 0 &&
|
2007-08-27 01:12:17 +02:00
|
|
|
#if UIP_REASSEMBLY > 0
|
|
|
|
fw->len == BUF->len &&
|
|
|
|
fw->offset == BUF->ipoffset &&
|
|
|
|
#endif
|
|
|
|
fw->ipid == BUF->ipid &&
|
2007-09-17 00:12:04 +02:00
|
|
|
fw->srcipaddr == BUF->srcipaddr &&
|
|
|
|
fw->destipaddr == BUF->destipaddr &&
|
2007-08-27 01:12:17 +02:00
|
|
|
#if notdef
|
|
|
|
fw->payload[0] == BUF->srcport &&
|
|
|
|
fw->payload[1] == BUF->destport &&
|
|
|
|
#endif
|
|
|
|
fw->proto == BUF->proto) {
|
|
|
|
/* Drop packet. */
|
|
|
|
return UIP_FW_FORWARDED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the TTL reaches zero we produce an ICMP time exceeded message
|
2007-09-16 00:45:45 +02:00
|
|
|
in the d_buf buffer and forward that packet back to the sender
|
|
|
|
of the packet.
|
|
|
|
*/
|
|
|
|
|
2007-09-17 00:12:04 +02:00
|
|
|
if (BUF->ttl <= 1)
|
|
|
|
{
|
|
|
|
/* No time exceeded for broadcasts and multicasts! */
|
|
|
|
|
|
|
|
if (BUF->destipaddr == 0xffffffff)
|
|
|
|
{
|
|
|
|
return UIP_FW_LOCAL;
|
|
|
|
}
|
|
|
|
time_exceeded(dev);
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
/* Decrement the TTL (time-to-live) value in the IP header */
|
|
|
|
BUF->ttl = BUF->ttl - 1;
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
/* Update the IP checksum. */
|
2007-09-17 00:12:04 +02:00
|
|
|
if (BUF->ipchksum >= HTONS(0xffff - 0x0100)) {
|
2007-08-27 01:12:17 +02:00
|
|
|
BUF->ipchksum = BUF->ipchksum + HTONS(0x0100) + 1;
|
|
|
|
} else {
|
|
|
|
BUF->ipchksum = BUF->ipchksum + HTONS(0x0100);
|
|
|
|
}
|
|
|
|
|
2007-09-17 00:12:04 +02:00
|
|
|
if (dev->d_len > 0) {
|
2007-09-16 00:45:45 +02:00
|
|
|
dev->d_appdata = &dev->d_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN];
|
|
|
|
uip_fw_output(dev);
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if UIP_BROADCAST
|
2007-09-17 00:12:04 +02:00
|
|
|
if (BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
|
2007-08-27 01:12:17 +02:00
|
|
|
return UIP_FW_LOCAL;
|
|
|
|
}
|
|
|
|
#endif /* UIP_BROADCAST */
|
|
|
|
|
|
|
|
/* Return non-zero to indicate that the packet was forwarded and that no
|
|
|
|
other processing should be made. */
|
|
|
|
return UIP_FW_FORWARDED;
|
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
/* Register a network interface with the forwarding module.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-09-16 00:45:45 +02:00
|
|
|
* netif A pointer to the network interface that is to be
|
2007-08-27 01:12:17 +02:00
|
|
|
* registered.
|
|
|
|
*/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
void uip_fw_register(struct uip_fw_netif *netif)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
|
|
|
netif->next = netifs;
|
|
|
|
netifs = netif;
|
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
/* Register a default network interface.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
|
|
|
* All packets that don't go out on any of the other interfaces will
|
|
|
|
* be routed to the default interface.
|
|
|
|
*
|
2007-09-16 00:45:45 +02:00
|
|
|
* netif A pointer to the network interface that is to be
|
2007-08-27 01:12:17 +02:00
|
|
|
* registered.
|
|
|
|
*/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
void uip_fw_default(struct uip_fw_netif *netif)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
|
|
|
defaultnetif = netif;
|
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
|
|
|
/* Perform periodic processing. */
|
|
|
|
|
|
|
|
void uip_fw_periodic(void)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
|
|
|
struct fwcache_entry *fw;
|
2007-09-17 00:12:04 +02:00
|
|
|
for (fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
|
|
|
|
if (fw->timer > 0) {
|
2007-08-27 01:12:17 +02:00
|
|
|
--fw->timer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|