uIP 1.0 based webserver for LPC1114 + ENC28J60

Dependencies:   mbed TMP102

Committer:
ban4jp
Date:
Mon Jun 30 16:00:08 2014 +0000
Revision:
3:a2715e9c7737
Parent:
0:685224d2f66d
backported from Contiki 2.7

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ban4jp 0:685224d2f66d 1 /*
ban4jp 0:685224d2f66d 2 * Copyright (c) 2004, Swedish Institute of Computer Science.
ban4jp 0:685224d2f66d 3 * All rights reserved.
ban4jp 0:685224d2f66d 4 *
ban4jp 0:685224d2f66d 5 * Redistribution and use in source and binary forms, with or without
ban4jp 0:685224d2f66d 6 * modification, are permitted provided that the following conditions
ban4jp 0:685224d2f66d 7 * are met:
ban4jp 0:685224d2f66d 8 * 1. Redistributions of source code must retain the above copyright
ban4jp 0:685224d2f66d 9 * notice, this list of conditions and the following disclaimer.
ban4jp 0:685224d2f66d 10 * 2. Redistributions in binary form must reproduce the above copyright
ban4jp 0:685224d2f66d 11 * notice, this list of conditions and the following disclaimer in the
ban4jp 0:685224d2f66d 12 * documentation and/or other materials provided with the distribution.
ban4jp 0:685224d2f66d 13 * 3. Neither the name of the Institute nor the names of its contributors
ban4jp 0:685224d2f66d 14 * may be used to endorse or promote products derived from this software
ban4jp 0:685224d2f66d 15 * without specific prior written permission.
ban4jp 0:685224d2f66d 16 *
ban4jp 0:685224d2f66d 17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
ban4jp 0:685224d2f66d 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ban4jp 0:685224d2f66d 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ban4jp 0:685224d2f66d 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
ban4jp 0:685224d2f66d 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ban4jp 0:685224d2f66d 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ban4jp 0:685224d2f66d 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ban4jp 0:685224d2f66d 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ban4jp 0:685224d2f66d 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ban4jp 0:685224d2f66d 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ban4jp 0:685224d2f66d 27 * SUCH DAMAGE.
ban4jp 0:685224d2f66d 28 *
ban4jp 3:a2715e9c7737 29 * This file is part of the Contiki operating system.
ban4jp 0:685224d2f66d 30 *
ban4jp 0:685224d2f66d 31 * Author: Adam Dunkels <adam@sics.se>
ban4jp 0:685224d2f66d 32 *
ban4jp 0:685224d2f66d 33 */
ban4jp 0:685224d2f66d 34 /**
ban4jp 0:685224d2f66d 35 * \addtogroup uip
ban4jp 0:685224d2f66d 36 * @{
ban4jp 0:685224d2f66d 37 */
ban4jp 0:685224d2f66d 38
ban4jp 0:685224d2f66d 39 /**
ban4jp 0:685224d2f66d 40 * \defgroup uipfw uIP packet forwarding
ban4jp 0:685224d2f66d 41 * @{
ban4jp 0:685224d2f66d 42 *
ban4jp 0:685224d2f66d 43 */
ban4jp 0:685224d2f66d 44
ban4jp 0:685224d2f66d 45 /**
ban4jp 0:685224d2f66d 46 * \file
ban4jp 0:685224d2f66d 47 * uIP packet forwarding.
ban4jp 0:685224d2f66d 48 * \author Adam Dunkels <adam@sics.se>
ban4jp 0:685224d2f66d 49 *
ban4jp 0:685224d2f66d 50 * This file implements a number of simple functions which do packet
ban4jp 0:685224d2f66d 51 * forwarding over multiple network interfaces with uIP.
ban4jp 0:685224d2f66d 52 *
ban4jp 0:685224d2f66d 53 */
ban4jp 0:685224d2f66d 54
ban4jp 3:a2715e9c7737 55 #include <string.h>
ban4jp 3:a2715e9c7737 56
ban4jp 3:a2715e9c7737 57 #include "uip-conf.h"
ban4jp 3:a2715e9c7737 58
ban4jp 0:685224d2f66d 59 #include "uip.h"
ban4jp 0:685224d2f66d 60 #include "uip_arch.h"
ban4jp 0:685224d2f66d 61 #include "uip-fw.h"
ban4jp 3:a2715e9c7737 62 #ifdef AODV_COMPLIANCE
ban4jp 3:a2715e9c7737 63 #include "uaodv-def.h"
ban4jp 3:a2715e9c7737 64 #endif
ban4jp 0:685224d2f66d 65
ban4jp 0:685224d2f66d 66 /*
ban4jp 0:685224d2f66d 67 * The list of registered network interfaces.
ban4jp 0:685224d2f66d 68 */
ban4jp 0:685224d2f66d 69 static struct uip_fw_netif *netifs = NULL;
ban4jp 0:685224d2f66d 70
ban4jp 0:685224d2f66d 71 /*
ban4jp 0:685224d2f66d 72 * A pointer to the default network interface.
ban4jp 0:685224d2f66d 73 */
ban4jp 0:685224d2f66d 74 static struct uip_fw_netif *defaultnetif = NULL;
ban4jp 0:685224d2f66d 75
ban4jp 0:685224d2f66d 76 struct tcpip_hdr {
ban4jp 0:685224d2f66d 77 /* IP header. */
ban4jp 3:a2715e9c7737 78 uint8_t vhl,
ban4jp 0:685224d2f66d 79 tos;
ban4jp 3:a2715e9c7737 80 uint16_t len,
ban4jp 0:685224d2f66d 81 ipid,
ban4jp 0:685224d2f66d 82 ipoffset;
ban4jp 3:a2715e9c7737 83 uint8_t ttl,
ban4jp 0:685224d2f66d 84 proto;
ban4jp 3:a2715e9c7737 85 uint16_t ipchksum;
ban4jp 3:a2715e9c7737 86 uip_ipaddr_t srcipaddr, destipaddr;
ban4jp 0:685224d2f66d 87
ban4jp 0:685224d2f66d 88 /* TCP header. */
ban4jp 3:a2715e9c7737 89 uint16_t srcport,
ban4jp 0:685224d2f66d 90 destport;
ban4jp 3:a2715e9c7737 91 uint8_t seqno[4],
ban4jp 0:685224d2f66d 92 ackno[4],
ban4jp 0:685224d2f66d 93 tcpoffset,
ban4jp 0:685224d2f66d 94 flags,
ban4jp 0:685224d2f66d 95 wnd[2];
ban4jp 3:a2715e9c7737 96 uint16_t tcpchksum;
ban4jp 3:a2715e9c7737 97 uint8_t urgp[2];
ban4jp 3:a2715e9c7737 98 uint8_t optdata[4];
ban4jp 0:685224d2f66d 99 };
ban4jp 0:685224d2f66d 100
ban4jp 0:685224d2f66d 101 struct icmpip_hdr {
ban4jp 0:685224d2f66d 102 /* IP header. */
ban4jp 3:a2715e9c7737 103 uint8_t vhl,
ban4jp 0:685224d2f66d 104 tos,
ban4jp 0:685224d2f66d 105 len[2],
ban4jp 0:685224d2f66d 106 ipid[2],
ban4jp 0:685224d2f66d 107 ipoffset[2],
ban4jp 0:685224d2f66d 108 ttl,
ban4jp 0:685224d2f66d 109 proto;
ban4jp 3:a2715e9c7737 110 uint16_t ipchksum;
ban4jp 3:a2715e9c7737 111 uip_ipaddr_t srcipaddr, destipaddr;
ban4jp 0:685224d2f66d 112 /* ICMP (echo) header. */
ban4jp 3:a2715e9c7737 113 uint8_t type, icode;
ban4jp 3:a2715e9c7737 114 uint16_t icmpchksum;
ban4jp 3:a2715e9c7737 115 uint16_t id, seqno;
ban4jp 3:a2715e9c7737 116 uint8_t payload[1];
ban4jp 0:685224d2f66d 117 };
ban4jp 0:685224d2f66d 118
ban4jp 0:685224d2f66d 119 /* ICMP ECHO. */
ban4jp 0:685224d2f66d 120 #define ICMP_ECHO 8
ban4jp 0:685224d2f66d 121
ban4jp 0:685224d2f66d 122 /* ICMP TIME-EXCEEDED. */
ban4jp 0:685224d2f66d 123 #define ICMP_TE 11
ban4jp 0:685224d2f66d 124
ban4jp 0:685224d2f66d 125 /*
ban4jp 0:685224d2f66d 126 * Pointer to the TCP/IP headers of the packet in the uip_buf buffer.
ban4jp 0:685224d2f66d 127 */
ban4jp 0:685224d2f66d 128 #define BUF ((struct tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
ban4jp 0:685224d2f66d 129
ban4jp 0:685224d2f66d 130 /*
ban4jp 0:685224d2f66d 131 * Pointer to the ICMP/IP headers of the packet in the uip_buf buffer.
ban4jp 0:685224d2f66d 132 */
ban4jp 0:685224d2f66d 133 #define ICMPBUF ((struct icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
ban4jp 0:685224d2f66d 134
ban4jp 0:685224d2f66d 135 /*
ban4jp 0:685224d2f66d 136 * Certain fields of an IP packet that are used for identifying
ban4jp 0:685224d2f66d 137 * duplicate packets.
ban4jp 0:685224d2f66d 138 */
ban4jp 0:685224d2f66d 139 struct fwcache_entry {
ban4jp 3:a2715e9c7737 140 uint16_t timer;
ban4jp 0:685224d2f66d 141
ban4jp 3:a2715e9c7737 142 uip_ipaddr_t srcipaddr;
ban4jp 3:a2715e9c7737 143 uip_ipaddr_t destipaddr;
ban4jp 3:a2715e9c7737 144 uint16_t ipid;
ban4jp 3:a2715e9c7737 145 uint8_t proto;
ban4jp 3:a2715e9c7737 146 uint8_t unused;
ban4jp 0:685224d2f66d 147
ban4jp 0:685224d2f66d 148 #if notdef
ban4jp 3:a2715e9c7737 149 uint16_t payload[2];
ban4jp 0:685224d2f66d 150 #endif
ban4jp 0:685224d2f66d 151
ban4jp 0:685224d2f66d 152 #if UIP_REASSEMBLY > 0
ban4jp 3:a2715e9c7737 153 uint16_t len, offset;
ban4jp 0:685224d2f66d 154 #endif
ban4jp 0:685224d2f66d 155 };
ban4jp 0:685224d2f66d 156
ban4jp 0:685224d2f66d 157 /*
ban4jp 0:685224d2f66d 158 * The number of packets to remember when looking for duplicates.
ban4jp 0:685224d2f66d 159 */
ban4jp 0:685224d2f66d 160 #ifdef UIP_CONF_FWCACHE_SIZE
ban4jp 0:685224d2f66d 161 #define FWCACHE_SIZE UIP_CONF_FWCACHE_SIZE
ban4jp 0:685224d2f66d 162 #else
ban4jp 0:685224d2f66d 163 #define FWCACHE_SIZE 2
ban4jp 0:685224d2f66d 164 #endif
ban4jp 0:685224d2f66d 165
ban4jp 0:685224d2f66d 166
ban4jp 0:685224d2f66d 167 /*
ban4jp 0:685224d2f66d 168 * A cache of packet header fields which are used for
ban4jp 0:685224d2f66d 169 * identifying duplicate packets.
ban4jp 0:685224d2f66d 170 */
ban4jp 0:685224d2f66d 171 static struct fwcache_entry fwcache[FWCACHE_SIZE];
ban4jp 0:685224d2f66d 172
ban4jp 0:685224d2f66d 173 /**
ban4jp 0:685224d2f66d 174 * \internal
ban4jp 0:685224d2f66d 175 * The time that a packet cache is active.
ban4jp 0:685224d2f66d 176 */
ban4jp 0:685224d2f66d 177 #define FW_TIME 20
ban4jp 0:685224d2f66d 178
ban4jp 0:685224d2f66d 179 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 180 /**
ban4jp 0:685224d2f66d 181 * Initialize the uIP packet forwarding module.
ban4jp 0:685224d2f66d 182 */
ban4jp 0:685224d2f66d 183 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 184 void
ban4jp 0:685224d2f66d 185 uip_fw_init(void)
ban4jp 0:685224d2f66d 186 {
ban4jp 0:685224d2f66d 187 struct uip_fw_netif *t;
ban4jp 0:685224d2f66d 188 defaultnetif = NULL;
ban4jp 0:685224d2f66d 189 while(netifs != NULL) {
ban4jp 0:685224d2f66d 190 t = netifs;
ban4jp 0:685224d2f66d 191 netifs = netifs->next;
ban4jp 0:685224d2f66d 192 t->next = NULL;
ban4jp 0:685224d2f66d 193 }
ban4jp 0:685224d2f66d 194 }
ban4jp 0:685224d2f66d 195 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 196 /**
ban4jp 0:685224d2f66d 197 * \internal
ban4jp 0:685224d2f66d 198 * Check if an IP address is within the network defined by an IP
ban4jp 0:685224d2f66d 199 * address and a netmask.
ban4jp 0:685224d2f66d 200 *
ban4jp 0:685224d2f66d 201 * \param ipaddr The IP address to be checked.
ban4jp 0:685224d2f66d 202 * \param netipaddr The IP address of the network.
ban4jp 0:685224d2f66d 203 * \param netmask The netmask of the network.
ban4jp 0:685224d2f66d 204 *
ban4jp 0:685224d2f66d 205 * \return Non-zero if IP address is in network, zero otherwise.
ban4jp 0:685224d2f66d 206 */
ban4jp 0:685224d2f66d 207 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 208 static unsigned char
ban4jp 3:a2715e9c7737 209 ipaddr_maskcmp(uip_ipaddr_t *ipaddr,
ban4jp 3:a2715e9c7737 210 uip_ipaddr_t *netipaddr,
ban4jp 3:a2715e9c7737 211 uip_ipaddr_t *netmask)
ban4jp 0:685224d2f66d 212 {
ban4jp 3:a2715e9c7737 213 return (ipaddr->u16[0] & netmask->u16[0]) == (netipaddr->u16[0] & netmask->u16[0]) &&
ban4jp 3:a2715e9c7737 214 (ipaddr->u16[1] & netmask->u16[1]) == (netipaddr->u16[1] & netmask->u16[1]);
ban4jp 0:685224d2f66d 215 }
ban4jp 0:685224d2f66d 216 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 217 /**
ban4jp 0:685224d2f66d 218 * \internal
ban4jp 0:685224d2f66d 219 * Send out an ICMP TIME-EXCEEDED message.
ban4jp 0:685224d2f66d 220 *
ban4jp 0:685224d2f66d 221 * This function replaces the packet in the uip_buf buffer with the
ban4jp 0:685224d2f66d 222 * ICMP packet.
ban4jp 0:685224d2f66d 223 */
ban4jp 0:685224d2f66d 224 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 225 static void
ban4jp 0:685224d2f66d 226 time_exceeded(void)
ban4jp 0:685224d2f66d 227 {
ban4jp 0:685224d2f66d 228
ban4jp 3:a2715e9c7737 229 /* We don't send out ICMP errors for ICMP messages (unless they are pings). */
ban4jp 3:a2715e9c7737 230 if(ICMPBUF->proto == UIP_PROTO_ICMP &&
ban4jp 3:a2715e9c7737 231 ICMPBUF->type != ICMP_ECHO) {
ban4jp 0:685224d2f66d 232 uip_len = 0;
ban4jp 0:685224d2f66d 233 return;
ban4jp 0:685224d2f66d 234 }
ban4jp 0:685224d2f66d 235 /* Copy fields from packet header into payload of this ICMP packet. */
ban4jp 3:a2715e9c7737 236 memcpy(&(ICMPBUF->payload[0]), ICMPBUF, UIP_IPH_LEN + 8);
ban4jp 0:685224d2f66d 237
ban4jp 0:685224d2f66d 238 /* Set the ICMP type and code. */
ban4jp 0:685224d2f66d 239 ICMPBUF->type = ICMP_TE;
ban4jp 0:685224d2f66d 240 ICMPBUF->icode = 0;
ban4jp 0:685224d2f66d 241
ban4jp 0:685224d2f66d 242 /* Calculate the ICMP checksum. */
ban4jp 0:685224d2f66d 243 ICMPBUF->icmpchksum = 0;
ban4jp 3:a2715e9c7737 244 ICMPBUF->icmpchksum = ~uip_chksum((uint16_t *)&(ICMPBUF->type), 36);
ban4jp 0:685224d2f66d 245
ban4jp 0:685224d2f66d 246 /* Set the IP destination address to be the source address of the
ban4jp 0:685224d2f66d 247 original packet. */
ban4jp 3:a2715e9c7737 248 uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
ban4jp 0:685224d2f66d 249
ban4jp 0:685224d2f66d 250 /* Set our IP address as the source address. */
ban4jp 3:a2715e9c7737 251 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
ban4jp 0:685224d2f66d 252
ban4jp 0:685224d2f66d 253 /* The size of the ICMP time exceeded packet is 36 + the size of the
ban4jp 0:685224d2f66d 254 IP header (20) = 56. */
ban4jp 0:685224d2f66d 255 uip_len = 56;
ban4jp 0:685224d2f66d 256 ICMPBUF->len[0] = 0;
ban4jp 3:a2715e9c7737 257 ICMPBUF->len[1] = (uint8_t)uip_len;
ban4jp 0:685224d2f66d 258
ban4jp 0:685224d2f66d 259 /* Fill in the other fields in the IP header. */
ban4jp 0:685224d2f66d 260 ICMPBUF->vhl = 0x45;
ban4jp 0:685224d2f66d 261 ICMPBUF->tos = 0;
ban4jp 0:685224d2f66d 262 ICMPBUF->ipoffset[0] = ICMPBUF->ipoffset[1] = 0;
ban4jp 0:685224d2f66d 263 ICMPBUF->ttl = UIP_TTL;
ban4jp 0:685224d2f66d 264 ICMPBUF->proto = UIP_PROTO_ICMP;
ban4jp 0:685224d2f66d 265
ban4jp 0:685224d2f66d 266 /* Calculate IP checksum. */
ban4jp 0:685224d2f66d 267 ICMPBUF->ipchksum = 0;
ban4jp 0:685224d2f66d 268 ICMPBUF->ipchksum = ~(uip_ipchksum());
ban4jp 0:685224d2f66d 269
ban4jp 0:685224d2f66d 270
ban4jp 0:685224d2f66d 271 }
ban4jp 0:685224d2f66d 272 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 273 /**
ban4jp 0:685224d2f66d 274 * \internal
ban4jp 0:685224d2f66d 275 * Register a packet in the forwarding cache so that it won't be
ban4jp 0:685224d2f66d 276 * forwarded again.
ban4jp 0:685224d2f66d 277 */
ban4jp 0:685224d2f66d 278 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 279 static void
ban4jp 0:685224d2f66d 280 fwcache_register(void)
ban4jp 0:685224d2f66d 281 {
ban4jp 0:685224d2f66d 282 struct fwcache_entry *fw;
ban4jp 0:685224d2f66d 283 int i, oldest;
ban4jp 0:685224d2f66d 284
ban4jp 0:685224d2f66d 285 oldest = FW_TIME;
ban4jp 0:685224d2f66d 286 fw = NULL;
ban4jp 0:685224d2f66d 287
ban4jp 0:685224d2f66d 288 /* Find the oldest entry in the cache. */
ban4jp 0:685224d2f66d 289 for(i = 0; i < FWCACHE_SIZE; ++i) {
ban4jp 0:685224d2f66d 290 if(fwcache[i].timer == 0) {
ban4jp 0:685224d2f66d 291 fw = &fwcache[i];
ban4jp 0:685224d2f66d 292 break;
ban4jp 0:685224d2f66d 293 } else if(fwcache[i].timer <= oldest) {
ban4jp 0:685224d2f66d 294 fw = &fwcache[i];
ban4jp 0:685224d2f66d 295 oldest = fwcache[i].timer;
ban4jp 0:685224d2f66d 296 }
ban4jp 0:685224d2f66d 297 }
ban4jp 0:685224d2f66d 298
ban4jp 0:685224d2f66d 299 fw->timer = FW_TIME;
ban4jp 0:685224d2f66d 300 fw->ipid = BUF->ipid;
ban4jp 3:a2715e9c7737 301 uip_ipaddr_copy(&fw->srcipaddr, &BUF->srcipaddr);
ban4jp 3:a2715e9c7737 302 uip_ipaddr_copy(&fw->destipaddr, &BUF->destipaddr);
ban4jp 0:685224d2f66d 303 fw->proto = BUF->proto;
ban4jp 0:685224d2f66d 304 #if notdef
ban4jp 0:685224d2f66d 305 fw->payload[0] = BUF->srcport;
ban4jp 0:685224d2f66d 306 fw->payload[1] = BUF->destport;
ban4jp 0:685224d2f66d 307 #endif
ban4jp 0:685224d2f66d 308 #if UIP_REASSEMBLY > 0
ban4jp 0:685224d2f66d 309 fw->len = BUF->len;
ban4jp 0:685224d2f66d 310 fw->offset = BUF->ipoffset;
ban4jp 0:685224d2f66d 311 #endif
ban4jp 0:685224d2f66d 312 }
ban4jp 0:685224d2f66d 313 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 314 /**
ban4jp 0:685224d2f66d 315 * \internal
ban4jp 0:685224d2f66d 316 * Find a network interface for the IP packet in uip_buf.
ban4jp 0:685224d2f66d 317 */
ban4jp 0:685224d2f66d 318 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 319 static struct uip_fw_netif *
ban4jp 0:685224d2f66d 320 find_netif(void)
ban4jp 0:685224d2f66d 321 {
ban4jp 0:685224d2f66d 322 struct uip_fw_netif *netif;
ban4jp 0:685224d2f66d 323
ban4jp 0:685224d2f66d 324 /* Walk through every network interface to check for a match. */
ban4jp 0:685224d2f66d 325 for(netif = netifs; netif != NULL; netif = netif->next) {
ban4jp 3:a2715e9c7737 326 if(ipaddr_maskcmp(&BUF->destipaddr, &netif->ipaddr,
ban4jp 3:a2715e9c7737 327 &netif->netmask)) {
ban4jp 0:685224d2f66d 328 /* If there was a match, we break the loop. */
ban4jp 0:685224d2f66d 329 return netif;
ban4jp 0:685224d2f66d 330 }
ban4jp 0:685224d2f66d 331 }
ban4jp 0:685224d2f66d 332
ban4jp 0:685224d2f66d 333 /* If no matching netif was found, we use default netif. */
ban4jp 0:685224d2f66d 334 return defaultnetif;
ban4jp 0:685224d2f66d 335 }
ban4jp 0:685224d2f66d 336 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 337 /**
ban4jp 0:685224d2f66d 338 * Output an IP packet on the correct network interface.
ban4jp 0:685224d2f66d 339 *
ban4jp 0:685224d2f66d 340 * The IP packet should be present in the uip_buf buffer and its
ban4jp 0:685224d2f66d 341 * length in the global uip_len variable.
ban4jp 0:685224d2f66d 342 *
ban4jp 0:685224d2f66d 343 * \retval UIP_FW_ZEROLEN Indicates that a zero-length packet
ban4jp 0:685224d2f66d 344 * transmission was attempted and that no packet was sent.
ban4jp 0:685224d2f66d 345 *
ban4jp 0:685224d2f66d 346 * \retval UIP_FW_NOROUTE No suitable network interface could be found
ban4jp 0:685224d2f66d 347 * for the outbound packet, and the packet was not sent.
ban4jp 0:685224d2f66d 348 *
ban4jp 0:685224d2f66d 349 * \return The return value from the actual network interface output
ban4jp 0:685224d2f66d 350 * function is passed unmodified as a return value.
ban4jp 0:685224d2f66d 351 */
ban4jp 0:685224d2f66d 352 /*------------------------------------------------------------------------------*/
ban4jp 3:a2715e9c7737 353 uint8_t
ban4jp 0:685224d2f66d 354 uip_fw_output(void)
ban4jp 0:685224d2f66d 355 {
ban4jp 0:685224d2f66d 356 struct uip_fw_netif *netif;
ban4jp 3:a2715e9c7737 357 #if UIP_BROADCAST
ban4jp 3:a2715e9c7737 358 const struct uip_udpip_hdr *udp = (void *)BUF;
ban4jp 3:a2715e9c7737 359 #endif /* UIP_BROADCAST */
ban4jp 0:685224d2f66d 360
ban4jp 0:685224d2f66d 361 if(uip_len == 0) {
ban4jp 0:685224d2f66d 362 return UIP_FW_ZEROLEN;
ban4jp 0:685224d2f66d 363 }
ban4jp 0:685224d2f66d 364
ban4jp 0:685224d2f66d 365 fwcache_register();
ban4jp 0:685224d2f66d 366
ban4jp 0:685224d2f66d 367 #if UIP_BROADCAST
ban4jp 0:685224d2f66d 368 /* Link local broadcasts go out on all interfaces. */
ban4jp 3:a2715e9c7737 369 if(uip_ipaddr_cmp(&udp->destipaddr, &uip_broadcast_addr)) {
ban4jp 0:685224d2f66d 370 if(defaultnetif != NULL) {
ban4jp 0:685224d2f66d 371 defaultnetif->output();
ban4jp 0:685224d2f66d 372 }
ban4jp 0:685224d2f66d 373 for(netif = netifs; netif != NULL; netif = netif->next) {
ban4jp 0:685224d2f66d 374 netif->output();
ban4jp 0:685224d2f66d 375 }
ban4jp 0:685224d2f66d 376 return UIP_FW_OK;
ban4jp 0:685224d2f66d 377 }
ban4jp 0:685224d2f66d 378 #endif /* UIP_BROADCAST */
ban4jp 0:685224d2f66d 379
ban4jp 0:685224d2f66d 380 netif = find_netif();
ban4jp 0:685224d2f66d 381 /* printf("uip_fw_output: netif %p ->output %p len %d\n", netif,
ban4jp 0:685224d2f66d 382 netif->output,
ban4jp 0:685224d2f66d 383 uip_len);*/
ban4jp 0:685224d2f66d 384
ban4jp 0:685224d2f66d 385 if(netif == NULL) {
ban4jp 0:685224d2f66d 386 return UIP_FW_NOROUTE;
ban4jp 0:685224d2f66d 387 }
ban4jp 0:685224d2f66d 388 /* If we now have found a suitable network interface, we call its
ban4jp 0:685224d2f66d 389 output function to send out the packet. */
ban4jp 0:685224d2f66d 390 return netif->output();
ban4jp 0:685224d2f66d 391 }
ban4jp 0:685224d2f66d 392 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 393 /**
ban4jp 0:685224d2f66d 394 * Forward an IP packet in the uip_buf buffer.
ban4jp 0:685224d2f66d 395 *
ban4jp 0:685224d2f66d 396 *
ban4jp 0:685224d2f66d 397 *
ban4jp 0:685224d2f66d 398 * \return UIP_FW_FORWARDED if the packet was forwarded, UIP_FW_LOCAL if
ban4jp 0:685224d2f66d 399 * the packet should be processed locally.
ban4jp 0:685224d2f66d 400 */
ban4jp 0:685224d2f66d 401 /*------------------------------------------------------------------------------*/
ban4jp 3:a2715e9c7737 402 uint8_t
ban4jp 0:685224d2f66d 403 uip_fw_forward(void)
ban4jp 0:685224d2f66d 404 {
ban4jp 0:685224d2f66d 405 struct fwcache_entry *fw;
ban4jp 0:685224d2f66d 406
ban4jp 0:685224d2f66d 407 /* First check if the packet is destined for ourselves and return 0
ban4jp 0:685224d2f66d 408 to indicate that the packet should be processed locally. */
ban4jp 3:a2715e9c7737 409 if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) {
ban4jp 0:685224d2f66d 410 return UIP_FW_LOCAL;
ban4jp 0:685224d2f66d 411 }
ban4jp 0:685224d2f66d 412
ban4jp 3:a2715e9c7737 413 #ifdef AODV_COMPLIANCE
ban4jp 3:a2715e9c7737 414 #define udp ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
ban4jp 3:a2715e9c7737 415 if(udp->proto == UIP_PROTO_UDP && udp->destport == UIP_HTONS(UAODV_UDPPORT)) {
ban4jp 3:a2715e9c7737 416 return UIP_FW_LOCAL;
ban4jp 3:a2715e9c7737 417 }
ban4jp 3:a2715e9c7737 418 #endif
ban4jp 3:a2715e9c7737 419
ban4jp 0:685224d2f66d 420 /* If we use ping IP address configuration, and our IP address is
ban4jp 0:685224d2f66d 421 not yet configured, we should intercept all ICMP echo packets. */
ban4jp 0:685224d2f66d 422 #if UIP_PINGADDRCONF
ban4jp 3:a2715e9c7737 423 if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr) &&
ban4jp 0:685224d2f66d 424 BUF->proto == UIP_PROTO_ICMP &&
ban4jp 0:685224d2f66d 425 ICMPBUF->type == ICMP_ECHO) {
ban4jp 0:685224d2f66d 426 return UIP_FW_LOCAL;
ban4jp 0:685224d2f66d 427 }
ban4jp 0:685224d2f66d 428 #endif /* UIP_PINGADDRCONF */
ban4jp 0:685224d2f66d 429
ban4jp 0:685224d2f66d 430 /* Check if the packet is in the forwarding cache already, and if so
ban4jp 0:685224d2f66d 431 we drop it. */
ban4jp 0:685224d2f66d 432
ban4jp 0:685224d2f66d 433 for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
ban4jp 0:685224d2f66d 434 if(fw->timer != 0 &&
ban4jp 0:685224d2f66d 435 #if UIP_REASSEMBLY > 0
ban4jp 0:685224d2f66d 436 fw->len == BUF->len &&
ban4jp 0:685224d2f66d 437 fw->offset == BUF->ipoffset &&
ban4jp 0:685224d2f66d 438 #endif
ban4jp 0:685224d2f66d 439 fw->ipid == BUF->ipid &&
ban4jp 3:a2715e9c7737 440 uip_ipaddr_cmp(&fw->srcipaddr, &BUF->srcipaddr) &&
ban4jp 3:a2715e9c7737 441 uip_ipaddr_cmp(&fw->destipaddr, &BUF->destipaddr) &&
ban4jp 0:685224d2f66d 442 #if notdef
ban4jp 0:685224d2f66d 443 fw->payload[0] == BUF->srcport &&
ban4jp 0:685224d2f66d 444 fw->payload[1] == BUF->destport &&
ban4jp 0:685224d2f66d 445 #endif
ban4jp 0:685224d2f66d 446 fw->proto == BUF->proto) {
ban4jp 0:685224d2f66d 447 /* Drop packet. */
ban4jp 0:685224d2f66d 448 return UIP_FW_FORWARDED;
ban4jp 0:685224d2f66d 449 }
ban4jp 0:685224d2f66d 450 }
ban4jp 0:685224d2f66d 451
ban4jp 0:685224d2f66d 452 /* If the TTL reaches zero we produce an ICMP time exceeded message
ban4jp 0:685224d2f66d 453 in the uip_buf buffer and forward that packet back to the sender
ban4jp 0:685224d2f66d 454 of the packet. */
ban4jp 3:a2715e9c7737 455
ban4jp 0:685224d2f66d 456 if(BUF->ttl <= 1) {
ban4jp 0:685224d2f66d 457 /* No time exceeded for broadcasts and multicasts! */
ban4jp 3:a2715e9c7737 458 if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) {
ban4jp 0:685224d2f66d 459 return UIP_FW_LOCAL;
ban4jp 0:685224d2f66d 460 }
ban4jp 0:685224d2f66d 461 time_exceeded();
ban4jp 0:685224d2f66d 462 }
ban4jp 0:685224d2f66d 463
ban4jp 0:685224d2f66d 464 /* Decrement the TTL (time-to-live) value in the IP header */
ban4jp 0:685224d2f66d 465 BUF->ttl = BUF->ttl - 1;
ban4jp 0:685224d2f66d 466
ban4jp 0:685224d2f66d 467 /* Update the IP checksum. */
ban4jp 3:a2715e9c7737 468 if(BUF->ipchksum >= UIP_HTONS(0xffff - 0x0100)) {
ban4jp 3:a2715e9c7737 469 BUF->ipchksum = BUF->ipchksum + UIP_HTONS(0x0100) + 1;
ban4jp 0:685224d2f66d 470 } else {
ban4jp 3:a2715e9c7737 471 BUF->ipchksum = BUF->ipchksum + UIP_HTONS(0x0100);
ban4jp 0:685224d2f66d 472 }
ban4jp 0:685224d2f66d 473
ban4jp 0:685224d2f66d 474 if(uip_len > 0) {
ban4jp 0:685224d2f66d 475 uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN];
ban4jp 0:685224d2f66d 476 uip_fw_output();
ban4jp 0:685224d2f66d 477 }
ban4jp 0:685224d2f66d 478
ban4jp 0:685224d2f66d 479 #if UIP_BROADCAST
ban4jp 3:a2715e9c7737 480 if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) {
ban4jp 0:685224d2f66d 481 return UIP_FW_LOCAL;
ban4jp 0:685224d2f66d 482 }
ban4jp 0:685224d2f66d 483 #endif /* UIP_BROADCAST */
ban4jp 0:685224d2f66d 484
ban4jp 0:685224d2f66d 485 /* Return non-zero to indicate that the packet was forwarded and that no
ban4jp 0:685224d2f66d 486 other processing should be made. */
ban4jp 0:685224d2f66d 487 return UIP_FW_FORWARDED;
ban4jp 0:685224d2f66d 488 }
ban4jp 0:685224d2f66d 489 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 490 /**
ban4jp 0:685224d2f66d 491 * Register a network interface with the forwarding module.
ban4jp 0:685224d2f66d 492 *
ban4jp 0:685224d2f66d 493 * \param netif A pointer to the network interface that is to be
ban4jp 0:685224d2f66d 494 * registered.
ban4jp 0:685224d2f66d 495 */
ban4jp 0:685224d2f66d 496 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 497 void
ban4jp 0:685224d2f66d 498 uip_fw_register(struct uip_fw_netif *netif)
ban4jp 0:685224d2f66d 499 {
ban4jp 0:685224d2f66d 500 netif->next = netifs;
ban4jp 0:685224d2f66d 501 netifs = netif;
ban4jp 0:685224d2f66d 502 }
ban4jp 0:685224d2f66d 503 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 504 /**
ban4jp 0:685224d2f66d 505 * Register a default network interface.
ban4jp 0:685224d2f66d 506 *
ban4jp 0:685224d2f66d 507 * All packets that don't go out on any of the other interfaces will
ban4jp 0:685224d2f66d 508 * be routed to the default interface.
ban4jp 0:685224d2f66d 509 *
ban4jp 0:685224d2f66d 510 * \param netif A pointer to the network interface that is to be
ban4jp 0:685224d2f66d 511 * registered.
ban4jp 0:685224d2f66d 512 */
ban4jp 0:685224d2f66d 513 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 514 void
ban4jp 0:685224d2f66d 515 uip_fw_default(struct uip_fw_netif *netif)
ban4jp 0:685224d2f66d 516 {
ban4jp 0:685224d2f66d 517 defaultnetif = netif;
ban4jp 0:685224d2f66d 518 }
ban4jp 0:685224d2f66d 519 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 520 /**
ban4jp 0:685224d2f66d 521 * Perform periodic processing.
ban4jp 0:685224d2f66d 522 */
ban4jp 0:685224d2f66d 523 /*------------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 524 void
ban4jp 0:685224d2f66d 525 uip_fw_periodic(void)
ban4jp 0:685224d2f66d 526 {
ban4jp 0:685224d2f66d 527 struct fwcache_entry *fw;
ban4jp 0:685224d2f66d 528 for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
ban4jp 0:685224d2f66d 529 if(fw->timer > 0) {
ban4jp 0:685224d2f66d 530 --fw->timer;
ban4jp 0:685224d2f66d 531 }
ban4jp 0:685224d2f66d 532 }
ban4jp 0:685224d2f66d 533 }
ban4jp 0:685224d2f66d 534 /*------------------------------------------------------------------------------*/
ban4jp 3:a2715e9c7737 535 /** @} */
ban4jp 3:a2715e9c7737 536 /** @} */