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