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