adamdunkels | 4b70611 | 2003-09-23 08:57:40 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * uIP packet forwarding. |
| 4 | * \author Adam Dunkels <adam@sics.se> |
| 5 | * |
| 6 | * This file implements a number of simple functions which do packet |
| 7 | * forwarding over multiple network interfaces with uIP. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include "uip.h" |
| 12 | #include "uip-fw.h" |
| 13 | |
| 14 | /** |
| 15 | * \internal |
| 16 | * The list of registered network interfaces. |
| 17 | */ |
| 18 | static struct uip_fw_netif *netifs = NULL; |
| 19 | |
| 20 | /** |
| 21 | * \internal |
| 22 | * A pointer to the default network interface. |
| 23 | */ |
| 24 | static struct uip_fw_netif *defaultnetif = NULL; |
| 25 | |
| 26 | struct tcpip_hdr { |
| 27 | /* IP header. */ |
| 28 | u8_t vhl, |
| 29 | tos; |
| 30 | u16_t len, |
| 31 | ipid, |
| 32 | ipoffset; |
| 33 | u8_t ttl, |
| 34 | proto; |
| 35 | u16_t ipchksum; |
| 36 | u16_t srcipaddr[2], |
| 37 | destipaddr[2]; |
| 38 | |
| 39 | /* TCP header. */ |
| 40 | u16_t srcport, |
| 41 | destport; |
| 42 | u8_t seqno[4], |
| 43 | ackno[4], |
| 44 | tcpoffset, |
| 45 | flags, |
| 46 | wnd[2]; |
| 47 | u16_t tcpchksum; |
| 48 | u8_t urgp[2]; |
| 49 | u8_t optdata[4]; |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * \internal |
| 54 | * Pointer to the TCP/IP headers of the packet in the uip_buf buffer. |
| 55 | */ |
| 56 | #define BUF ((struct tcpip_hdr *)&uip_buf[UIP_LLH_LEN]) |
| 57 | |
| 58 | /** |
| 59 | * \internal |
| 60 | * Certain fields of an IP packet that are used for identifying |
| 61 | * duplicate packets. |
| 62 | */ |
| 63 | struct fwcache_entry { |
| 64 | u16_t timestamp; |
| 65 | |
| 66 | u16_t len, offset, ipid; |
| 67 | u16_t srcipaddr[2]; |
| 68 | u16_t destipaddr[2]; |
| 69 | u16_t payload[2]; |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * \internal |
| 74 | * The number of packets to remember when looking for duplicates. |
| 75 | */ |
| 76 | #define FWCACHE_SIZE 2 |
| 77 | |
| 78 | /** |
| 79 | * \internal |
| 80 | * A cache of packet header fields which are used for |
| 81 | * identifying duplicate packets. |
| 82 | */ |
| 83 | static struct fwcache_entry fwcache[FWCACHE_SIZE]; |
| 84 | |
| 85 | /*------------------------------------------------------------------------------*/ |
| 86 | /** |
| 87 | * Initialize the uIP packet forwarding module. |
| 88 | */ |
| 89 | /*------------------------------------------------------------------------------*/ |
| 90 | void |
| 91 | uip_fw_init(void) |
| 92 | { |
| 93 | defaultnetif = netifs = NULL; |
| 94 | } |
| 95 | /*------------------------------------------------------------------------------*/ |
| 96 | /** |
| 97 | * \internal |
| 98 | * Check if an IP address is within the network defined by an IP |
| 99 | * address and a netmask. |
| 100 | * |
| 101 | * \param ipaddr The IP address to be checked. |
| 102 | * \param netipaddr The IP address of the network. |
| 103 | * \param netmask The netmask of the network. |
| 104 | * |
| 105 | * \return Non-zero if IP address is in network, zero otherwise. |
| 106 | */ |
| 107 | /*------------------------------------------------------------------------------*/ |
| 108 | static unsigned char |
| 109 | ipaddr_maskcmp(u16_t *ipaddr, u16_t *netipaddr, u16_t *netmask) |
| 110 | { |
| 111 | return (ipaddr[0] & netmask [0]) == (netipaddr[0] & netmask[0]) && |
| 112 | (ipaddr[1] & netmask[1]) == (netipaddr[1] & netmask[1]); |
| 113 | } |
| 114 | /*------------------------------------------------------------------------------*/ |
| 115 | /** |
| 116 | * \internal |
| 117 | * Register a packet in the forwarding cache so that it won't be |
| 118 | * forwarded again. |
| 119 | */ |
| 120 | /*------------------------------------------------------------------------------*/ |
| 121 | static void |
| 122 | fwcache_register(void) |
| 123 | { |
| 124 | struct fwcache_entry *fw; |
| 125 | |
| 126 | /* Register packet in forwarding cache. */ |
| 127 | fw = &fwcache[0]; |
| 128 | fw->len = BUF->len; |
| 129 | fw->offset = BUF->ipoffset; |
| 130 | fw->ipid = BUF->ipid; |
| 131 | fw->srcipaddr[0] = BUF->srcipaddr[0]; |
| 132 | fw->srcipaddr[1] = BUF->srcipaddr[1]; |
| 133 | fw->destipaddr[0] = BUF->destipaddr[0]; |
| 134 | fw->destipaddr[1] = BUF->destipaddr[1]; |
| 135 | fw->payload[0] = BUF->srcport; |
| 136 | fw->payload[1] = BUF->destport; |
| 137 | } |
| 138 | /*------------------------------------------------------------------------------*/ |
| 139 | /** |
| 140 | * Output an IP packet on the correct network interface. |
| 141 | * |
| 142 | * The IP packet should be present in the uip_buf buffer and its |
| 143 | * length in the global uip_len variable. |
| 144 | */ |
| 145 | /*------------------------------------------------------------------------------*/ |
| 146 | void |
| 147 | uip_fw_output(void) |
| 148 | { |
| 149 | struct uip_fw_netif *netif; |
| 150 | |
| 151 | /* Walk through every network interface to check for a match. */ |
| 152 | for(netif = netifs; netif != NULL; netif = netif->next) { |
| 153 | if(ipaddr_maskcmp(BUF->destipaddr, netif->ipaddr, |
| 154 | netif->netmask)) { |
| 155 | /* If there was a match, we break the loop. */ |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* If no matching netif was found, we use default netif. */ |
| 161 | if(netif == NULL) { |
| 162 | netif = defaultnetif; |
| 163 | } |
| 164 | |
| 165 | /* If we now have found a suitable network interface, we call its |
| 166 | output function to send out the packet. */ |
| 167 | if(netif != NULL && uip_len > 0) { |
| 168 | fwcache_register(); |
| 169 | netif->output(); |
| 170 | } |
| 171 | } |
| 172 | /*------------------------------------------------------------------------------*/ |
| 173 | /** |
| 174 | * Forward an IP packet in the uip_buf buffer. |
| 175 | * |
| 176 | * |
| 177 | * |
| 178 | * \return Non-zero if the packet was forwarded, zero if the packet |
| 179 | * should be processed locally. |
| 180 | */ |
| 181 | /*------------------------------------------------------------------------------*/ |
| 182 | unsigned char |
| 183 | uip_fw_forward(void) |
| 184 | { |
| 185 | struct uip_fw_netif *netif; |
| 186 | struct fwcache_entry *fw; |
| 187 | |
| 188 | /* First check if the packet is destined for ourselves and return 0 |
| 189 | to indicate that the packet should be processed locally. */ |
| 190 | if(BUF->destipaddr[0] == uip_hostaddr[0] && |
| 191 | BUF->destipaddr[1] == uip_hostaddr[1]) { |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* Check if the packet is in the forwarding cache already, and if so |
| 196 | we drop it. */ |
| 197 | /* for(fw = fwcache; fw <= &fwcache[FWCACHE_SIZE]; ++fw) {*/ |
| 198 | fw = &fwcache[0]; |
| 199 | { |
| 200 | if(fw->len == BUF->len && |
| 201 | fw->offset == BUF->ipoffset && |
| 202 | fw->ipid == BUF->ipid && |
| 203 | fw->srcipaddr[0] == BUF->srcipaddr[0] && |
| 204 | fw->srcipaddr[1] == BUF->srcipaddr[1] && |
| 205 | fw->destipaddr[0] == BUF->destipaddr[0] && |
| 206 | fw->destipaddr[1] == BUF->destipaddr[1] && |
| 207 | fw->payload[0] == BUF->srcport && |
| 208 | fw->payload[1] == BUF->destport) { |
| 209 | /* Drop packet. */ |
| 210 | return 1; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /* Walk through every network interface to check for a match. */ |
| 216 | for(netif = netifs; netif != NULL; netif = netif->next) { |
| 217 | if(ipaddr_maskcmp(BUF->destipaddr, netif->ipaddr, |
| 218 | netif->netmask)) { |
| 219 | /* If there was a match, we break the loop. */ |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* If no matching netif was found, we use default netif. */ |
| 225 | if(netif == NULL) { |
| 226 | netif = defaultnetif; |
| 227 | } |
| 228 | |
| 229 | /* Decrement the TTL (time-to-live) value in the IP header */ |
| 230 | BUF->ttl = htons(htons(BUF->ttl) - 1); |
| 231 | |
| 232 | /* We should really send an ICMP message if TTL == 0, but we skip it |
| 233 | for now... Instead, we just drop the packet (and pretend that it |
| 234 | was forwarded by returning 1). */ |
| 235 | if(BUF->ttl == 0) { |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | /* Incrementally update the IP checksum. */ |
| 240 | if(BUF->ipchksum >= htons(0xffff - 0x0100)) { |
| 241 | BUF->ipchksum = BUF->ipchksum + HTONS(0x0100) + 1; |
| 242 | } else { |
| 243 | BUF->ipchksum = BUF->ipchksum + HTONS(0x0100); |
| 244 | } |
| 245 | |
| 246 | /* If we now have found a suitable network interface, we call its |
| 247 | output function to send out the packet. */ |
| 248 | if(netif != NULL && uip_len > 0) { |
| 249 | uip_appdata = &uip_buf[UIP_LLH_LEN + 40]; |
| 250 | fwcache_register(); |
| 251 | netif->output(); |
| 252 | } |
| 253 | |
| 254 | /* Return non-zero to indicate that the packet was forwarded and that no |
| 255 | other processing should be made. */ |
| 256 | return 1; |
| 257 | } |
| 258 | /*------------------------------------------------------------------------------*/ |
| 259 | /** |
| 260 | * Register a network interface with the forwarding module. |
| 261 | * |
| 262 | * \param netif A pointer to the network interface that is to be |
| 263 | * registered. |
| 264 | */ |
| 265 | /*------------------------------------------------------------------------------*/ |
| 266 | void |
| 267 | uip_fw_register(struct uip_fw_netif *netif) |
| 268 | { |
| 269 | netif->next = netifs; |
| 270 | netifs = netif; |
| 271 | } |
| 272 | /*------------------------------------------------------------------------------*/ |
| 273 | /** |
| 274 | * Register a default network interface. |
| 275 | * |
| 276 | * All packets that don't go out on any of the other interfaces will |
| 277 | * be routed to the default interface. |
| 278 | * |
| 279 | * \param netif A pointer to the network interface that is to be |
| 280 | * registered. |
| 281 | */ |
| 282 | /*------------------------------------------------------------------------------*/ |
| 283 | void |
| 284 | uip_fw_default(struct uip_fw_netif *netif) |
| 285 | { |
| 286 | defaultnetif = netif; |
| 287 | } |
| 288 | /*------------------------------------------------------------------------------*/ |