These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Committer:
frank26080115
Date:
Sun Mar 20 05:38:56 2011 +0000
Revision:
0:bf7b9fba3924

        

Who changed what in which revision?

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