diff options
Diffstat (limited to 'lib/lufa/Projects/Webserver/Lib/uip/uip_arp.c')
| -rw-r--r-- | lib/lufa/Projects/Webserver/Lib/uip/uip_arp.c | 432 |
1 files changed, 0 insertions, 432 deletions
diff --git a/lib/lufa/Projects/Webserver/Lib/uip/uip_arp.c b/lib/lufa/Projects/Webserver/Lib/uip/uip_arp.c deleted file mode 100644 index fcb783b14..000000000 --- a/lib/lufa/Projects/Webserver/Lib/uip/uip_arp.c +++ /dev/null | |||
| @@ -1,432 +0,0 @@ | |||
| 1 | /** | ||
| 2 | * \addtogroup uip | ||
| 3 | * @{ | ||
| 4 | */ | ||
| 5 | |||
| 6 | /** | ||
| 7 | * \defgroup uiparp uIP Address Resolution Protocol | ||
| 8 | * @{ | ||
| 9 | * | ||
| 10 | * The Address Resolution Protocol ARP is used for mapping between IP | ||
| 11 | * addresses and link level addresses such as the Ethernet MAC | ||
| 12 | * addresses. ARP uses broadcast queries to ask for the link level | ||
| 13 | * address of a known IP address and the host which is configured with | ||
| 14 | * the IP address for which the query was meant, will respond with its | ||
| 15 | * link level address. | ||
| 16 | * | ||
| 17 | * \note This ARP implementation only supports Ethernet. | ||
| 18 | */ | ||
| 19 | |||
| 20 | /** | ||
| 21 | * \file | ||
| 22 | * Implementation of the ARP Address Resolution Protocol. | ||
| 23 | * \author Adam Dunkels <adam@dunkels.com> | ||
| 24 | * | ||
| 25 | */ | ||
| 26 | |||
| 27 | /* | ||
| 28 | * Copyright (c) 2001-2003, Adam Dunkels. | ||
| 29 | * All rights reserved. | ||
| 30 | * | ||
| 31 | * Redistribution and use in source and binary forms, with or without | ||
| 32 | * modification, are permitted provided that the following conditions | ||
| 33 | * are met: | ||
| 34 | * 1. Redistributions of source code must retain the above copyright | ||
| 35 | * notice, this list of conditions and the following disclaimer. | ||
| 36 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 37 | * notice, this list of conditions and the following disclaimer in the | ||
| 38 | * documentation and/or other materials provided with the distribution. | ||
| 39 | * 3. The name of the author may not be used to endorse or promote | ||
| 40 | * products derived from this software without specific prior | ||
| 41 | * written permission. | ||
| 42 | * | ||
| 43 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | ||
| 44 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| 45 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||
| 47 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | ||
| 49 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 50 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
| 51 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| 52 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| 53 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 54 | * | ||
| 55 | * This file is part of the uIP TCP/IP stack. | ||
| 56 | * | ||
| 57 | * $Id: uip_arp.c,v 1.5 2008/02/07 01:35:00 adamdunkels Exp $ | ||
| 58 | * | ||
| 59 | */ | ||
| 60 | |||
| 61 | |||
| 62 | #include "uip_arp.h" | ||
| 63 | |||
| 64 | #include <string.h> | ||
| 65 | |||
| 66 | struct arp_hdr { | ||
| 67 | struct uip_eth_hdr ethhdr; | ||
| 68 | u16_t hwtype; | ||
| 69 | u16_t protocol; | ||
| 70 | u8_t hwlen; | ||
| 71 | u8_t protolen; | ||
| 72 | u16_t opcode; | ||
| 73 | struct uip_eth_addr shwaddr; | ||
| 74 | uip_ipaddr_t sipaddr; | ||
| 75 | struct uip_eth_addr dhwaddr; | ||
| 76 | uip_ipaddr_t dipaddr; | ||
| 77 | }; | ||
| 78 | |||
| 79 | struct ethip_hdr { | ||
| 80 | struct uip_eth_hdr ethhdr; | ||
| 81 | /* IP header. */ | ||
| 82 | u8_t vhl, | ||
| 83 | tos, | ||
| 84 | len[2], | ||
| 85 | ipid[2], | ||
| 86 | ipoffset[2], | ||
| 87 | ttl, | ||
| 88 | proto; | ||
| 89 | u16_t ipchksum; | ||
| 90 | uip_ipaddr_t srcipaddr, destipaddr; | ||
| 91 | }; | ||
| 92 | |||
| 93 | #define ARP_REQUEST 1 | ||
| 94 | #define ARP_REPLY 2 | ||
| 95 | |||
| 96 | #define ARP_HWTYPE_ETH 1 | ||
| 97 | |||
| 98 | struct arp_entry { | ||
| 99 | uip_ipaddr_t ipaddr; | ||
| 100 | struct uip_eth_addr ethaddr; | ||
| 101 | u8_t time; | ||
| 102 | }; | ||
| 103 | |||
| 104 | static const struct uip_eth_addr broadcast_ethaddr = | ||
| 105 | {{0xff,0xff,0xff,0xff,0xff,0xff}}; | ||
| 106 | static const u16_t broadcast_ipaddr[2] = {0xffff,0xffff}; | ||
| 107 | |||
| 108 | static struct arp_entry arp_table[UIP_ARPTAB_SIZE]; | ||
| 109 | static uip_ipaddr_t ipaddr; | ||
| 110 | static u8_t i, c; | ||
| 111 | |||
| 112 | static u8_t arptime; | ||
| 113 | static u8_t tmpage; | ||
| 114 | |||
| 115 | #define BUF ((struct arp_hdr *)&uip_buf[0]) | ||
| 116 | #define IPBUF ((struct ethip_hdr *)&uip_buf[0]) | ||
| 117 | |||
| 118 | #define DEBUG 0 | ||
| 119 | #if DEBUG | ||
| 120 | #include <stdio.h> | ||
| 121 | #define PRINTF(...) printf(__VA_ARGS__) | ||
| 122 | #else | ||
| 123 | #define PRINTF(...) | ||
| 124 | #endif | ||
| 125 | |||
| 126 | /*-----------------------------------------------------------------------------------*/ | ||
| 127 | /** | ||
| 128 | * Initialize the ARP module. | ||
| 129 | * | ||
| 130 | */ | ||
| 131 | /*-----------------------------------------------------------------------------------*/ | ||
| 132 | void | ||
| 133 | uip_arp_init(void) | ||
| 134 | { | ||
| 135 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { | ||
| 136 | memset(&arp_table[i].ipaddr, 0, 4); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | /*-----------------------------------------------------------------------------------*/ | ||
| 140 | /** | ||
| 141 | * Periodic ARP processing function. | ||
| 142 | * | ||
| 143 | * This function performs periodic timer processing in the ARP module | ||
| 144 | * and should be called at regular intervals. The recommended interval | ||
| 145 | * is 10 seconds between the calls. | ||
| 146 | * | ||
| 147 | */ | ||
| 148 | /*-----------------------------------------------------------------------------------*/ | ||
| 149 | void | ||
| 150 | uip_arp_timer(void) | ||
| 151 | { | ||
| 152 | struct arp_entry *tabptr = NULL; | ||
| 153 | |||
| 154 | ++arptime; | ||
| 155 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { | ||
| 156 | tabptr = &arp_table[i]; | ||
| 157 | if(uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) && | ||
| 158 | arptime - tabptr->time >= UIP_ARP_MAXAGE) { | ||
| 159 | memset(&tabptr->ipaddr, 0, 4); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | } | ||
| 164 | /*-----------------------------------------------------------------------------------*/ | ||
| 165 | static void | ||
| 166 | uip_arp_update(uip_ipaddr_t *ipaddr, struct uip_eth_addr *ethaddr) | ||
| 167 | { | ||
| 168 | register struct arp_entry *tabptr = NULL; | ||
| 169 | /* Walk through the ARP mapping table and try to find an entry to | ||
| 170 | update. If none is found, the IP -> MAC address mapping is | ||
| 171 | inserted in the ARP table. */ | ||
| 172 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { | ||
| 173 | |||
| 174 | tabptr = &arp_table[i]; | ||
| 175 | /* Only check those entries that are actually in use. */ | ||
| 176 | if(!uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr)) { | ||
| 177 | |||
| 178 | /* Check if the source IP address of the incoming packet matches | ||
| 179 | the IP address in this ARP table entry. */ | ||
| 180 | if(uip_ipaddr_cmp(ipaddr, &tabptr->ipaddr)) { | ||
| 181 | |||
| 182 | /* An old entry found, update this and return. */ | ||
| 183 | memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); | ||
| 184 | tabptr->time = arptime; | ||
| 185 | |||
| 186 | return; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | /* If we get here, no existing ARP table entry was found, so we | ||
| 192 | create one. */ | ||
| 193 | |||
| 194 | /* First, we try to find an unused entry in the ARP table. */ | ||
| 195 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { | ||
| 196 | tabptr = &arp_table[i]; | ||
| 197 | if(uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr)) { | ||
| 198 | break; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | /* If no unused entry is found, we try to find the oldest entry and | ||
| 203 | throw it away. */ | ||
| 204 | if(i == UIP_ARPTAB_SIZE) { | ||
| 205 | tmpage = 0; | ||
| 206 | c = 0; | ||
| 207 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { | ||
| 208 | tabptr = &arp_table[i]; | ||
| 209 | if(arptime - tabptr->time > tmpage) { | ||
| 210 | tmpage = arptime - tabptr->time; | ||
| 211 | c = i; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | i = c; | ||
| 215 | tabptr = &arp_table[i]; | ||
| 216 | } | ||
| 217 | |||
| 218 | /* Now, i is the ARP table entry which we will fill with the new | ||
| 219 | information. */ | ||
| 220 | uip_ipaddr_copy(&tabptr->ipaddr, ipaddr); | ||
| 221 | memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); | ||
| 222 | tabptr->time = arptime; | ||
| 223 | } | ||
| 224 | /*-----------------------------------------------------------------------------------*/ | ||
| 225 | /** | ||
| 226 | * ARP processing for incoming IP packets | ||
| 227 | * | ||
| 228 | * This function should be called by the device driver when an IP | ||
| 229 | * packet has been received. The function will check if the address is | ||
| 230 | * in the ARP cache, and if so the ARP cache entry will be | ||
| 231 | * refreshed. If no ARP cache entry was found, a new one is created. | ||
| 232 | * | ||
| 233 | * This function expects an IP packet with a prepended Ethernet header | ||
| 234 | * in the uip_buf[] buffer, and the length of the packet in the global | ||
| 235 | * variable uip_len. | ||
| 236 | */ | ||
| 237 | /*-----------------------------------------------------------------------------------*/ | ||
| 238 | #if 0 | ||
| 239 | void | ||
| 240 | uip_arp_ipin(void) | ||
| 241 | { | ||
| 242 | uip_len -= sizeof(struct uip_eth_hdr); | ||
| 243 | |||
| 244 | /* Only insert/update an entry if the source IP address of the | ||
| 245 | incoming IP packet comes from a host on the local network. */ | ||
| 246 | if((IPBUF->srcipaddr[0] & uip_netmask[0]) != | ||
| 247 | (uip_hostaddr[0] & uip_netmask[0])) { | ||
| 248 | return; | ||
| 249 | } | ||
| 250 | if((IPBUF->srcipaddr[1] & uip_netmask[1]) != | ||
| 251 | (uip_hostaddr[1] & uip_netmask[1])) { | ||
| 252 | return; | ||
| 253 | } | ||
| 254 | uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src)); | ||
| 255 | |||
| 256 | return; | ||
| 257 | } | ||
| 258 | #endif /* 0 */ | ||
| 259 | /*-----------------------------------------------------------------------------------*/ | ||
| 260 | /** | ||
| 261 | * ARP processing for incoming ARP packets. | ||
| 262 | * | ||
| 263 | * This function should be called by the device driver when an ARP | ||
| 264 | * packet has been received. The function will act differently | ||
| 265 | * depending on the ARP packet type: if it is a reply for a request | ||
| 266 | * that we previously sent out, the ARP cache will be filled in with | ||
| 267 | * the values from the ARP reply. If the incoming ARP packet is an ARP | ||
| 268 | * request for our IP address, an ARP reply packet is created and put | ||
| 269 | * into the uip_buf[] buffer. | ||
| 270 | * | ||
| 271 | * When the function returns, the value of the global variable uip_len | ||
| 272 | * indicates whether the device driver should send out a packet or | ||
| 273 | * not. If uip_len is zero, no packet should be sent. If uip_len is | ||
| 274 | * non-zero, it contains the length of the outbound packet that is | ||
| 275 | * present in the uip_buf[] buffer. | ||
| 276 | * | ||
| 277 | * This function expects an ARP packet with a prepended Ethernet | ||
| 278 | * header in the uip_buf[] buffer, and the length of the packet in the | ||
| 279 | * global variable uip_len. | ||
| 280 | */ | ||
| 281 | /*-----------------------------------------------------------------------------------*/ | ||
| 282 | void | ||
| 283 | uip_arp_arpin(void) | ||
| 284 | { | ||
| 285 | if(uip_len < sizeof(struct arp_hdr)) { | ||
| 286 | uip_len = 0; | ||
| 287 | return; | ||
| 288 | } | ||
| 289 | uip_len = 0; | ||
| 290 | |||
| 291 | switch(BUF->opcode) { | ||
| 292 | case HTONS(ARP_REQUEST): | ||
| 293 | /* ARP request. If it asked for our address, we send out a | ||
| 294 | reply. */ | ||
| 295 | /* if(BUF->dipaddr[0] == uip_hostaddr[0] && | ||
| 296 | BUF->dipaddr[1] == uip_hostaddr[1]) {*/ | ||
| 297 | PRINTF("uip_arp_arpin: request for %d.%d.%d.%d (we are %d.%d.%d.%d)\n", | ||
| 298 | BUF->dipaddr.u8[0], BUF->dipaddr.u8[1], | ||
| 299 | BUF->dipaddr.u8[2], BUF->dipaddr.u8[3], | ||
| 300 | uip_hostaddr.u8[0], uip_hostaddr.u8[1], | ||
| 301 | uip_hostaddr.u8[2], uip_hostaddr.u8[3]); | ||
| 302 | if(uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr)) { | ||
| 303 | /* First, we register the one who made the request in our ARP | ||
| 304 | table, since it is likely that we will do more communication | ||
| 305 | with this host in the future. */ | ||
| 306 | uip_arp_update(&BUF->sipaddr, &BUF->shwaddr); | ||
| 307 | |||
| 308 | BUF->opcode = HTONS(ARP_REPLY); | ||
| 309 | |||
| 310 | memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6); | ||
| 311 | memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6); | ||
| 312 | memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6); | ||
| 313 | memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6); | ||
| 314 | |||
| 315 | uip_ipaddr_copy(&BUF->dipaddr, &BUF->sipaddr); | ||
| 316 | uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr); | ||
| 317 | |||
| 318 | BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP); | ||
| 319 | uip_len = sizeof(struct arp_hdr); | ||
| 320 | } | ||
| 321 | break; | ||
| 322 | case HTONS(ARP_REPLY): | ||
| 323 | /* ARP reply. We insert or update the ARP table if it was meant | ||
| 324 | for us. */ | ||
| 325 | if(uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr)) { | ||
| 326 | uip_arp_update(&BUF->sipaddr, &BUF->shwaddr); | ||
| 327 | } | ||
| 328 | break; | ||
| 329 | } | ||
| 330 | |||
| 331 | return; | ||
| 332 | } | ||
| 333 | /*-----------------------------------------------------------------------------------*/ | ||
| 334 | /** | ||
| 335 | * Prepend Ethernet header to an outbound IP packet and see if we need | ||
| 336 | * to send out an ARP request. | ||
| 337 | * | ||
| 338 | * This function should be called before sending out an IP packet. The | ||
| 339 | * function checks the destination IP address of the IP packet to see | ||
| 340 | * what Ethernet MAC address that should be used as a destination MAC | ||
| 341 | * address on the Ethernet. | ||
| 342 | * | ||
| 343 | * If the destination IP address is in the local network (determined | ||
| 344 | * by logical ANDing of netmask and our IP address), the function | ||
| 345 | * checks the ARP cache to see if an entry for the destination IP | ||
| 346 | * address is found. If so, an Ethernet header is prepended and the | ||
| 347 | * function returns. If no ARP cache entry is found for the | ||
| 348 | * destination IP address, the packet in the uip_buf[] is replaced by | ||
| 349 | * an ARP request packet for the IP address. The IP packet is dropped | ||
| 350 | * and it is assumed that they higher level protocols (e.g., TCP) | ||
| 351 | * eventually will retransmit the dropped packet. | ||
| 352 | * | ||
| 353 | * If the destination IP address is not on the local network, the IP | ||
| 354 | * address of the default router is used instead. | ||
| 355 | * | ||
| 356 | * When the function returns, a packet is present in the uip_buf[] | ||
| 357 | * buffer, and the length of the packet is in the global variable | ||
| 358 | * uip_len. | ||
| 359 | */ | ||
| 360 | /*-----------------------------------------------------------------------------------*/ | ||
| 361 | void | ||
| 362 | uip_arp_out(void) | ||
| 363 | { | ||
| 364 | struct arp_entry *tabptr = NULL; | ||
| 365 | |||
| 366 | /* Find the destination IP address in the ARP table and construct | ||
| 367 | the Ethernet header. If the destination IP address isn't on the | ||
| 368 | local network, we use the default router's IP address instead. | ||
| 369 | |||
| 370 | If not ARP table entry is found, we overwrite the original IP | ||
| 371 | packet with an ARP request for the IP address. */ | ||
| 372 | |||
| 373 | /* First check if destination is a local broadcast. */ | ||
| 374 | if(uip_ipaddr_cmp(&IPBUF->destipaddr, &uip_broadcast_addr)) { | ||
| 375 | memcpy(IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6); | ||
| 376 | } else { | ||
| 377 | /* Check if the destination address is on the local network. */ | ||
| 378 | if(!uip_ipaddr_maskcmp(&IPBUF->destipaddr, &uip_hostaddr, &uip_netmask)) { | ||
| 379 | /* Destination address was not on the local network, so we need to | ||
| 380 | use the default router's IP address instead of the destination | ||
| 381 | address when determining the MAC address. */ | ||
| 382 | uip_ipaddr_copy(&ipaddr, &uip_draddr); | ||
| 383 | } else { | ||
| 384 | /* Else, we use the destination IP address. */ | ||
| 385 | uip_ipaddr_copy(&ipaddr, &IPBUF->destipaddr); | ||
| 386 | } | ||
| 387 | |||
| 388 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { | ||
| 389 | tabptr = &arp_table[i]; | ||
| 390 | if(uip_ipaddr_cmp(&ipaddr, &tabptr->ipaddr)) { | ||
| 391 | break; | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 395 | if(i == UIP_ARPTAB_SIZE) { | ||
| 396 | /* The destination address was not in our ARP table, so we | ||
| 397 | overwrite the IP packet with an ARP request. */ | ||
| 398 | |||
| 399 | memset(BUF->ethhdr.dest.addr, 0xff, 6); | ||
| 400 | memset(BUF->dhwaddr.addr, 0x00, 6); | ||
| 401 | memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6); | ||
| 402 | memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6); | ||
| 403 | |||
| 404 | uip_ipaddr_copy(&BUF->dipaddr, &ipaddr); | ||
| 405 | uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr); | ||
| 406 | BUF->opcode = HTONS(ARP_REQUEST); /* ARP request. */ | ||
| 407 | BUF->hwtype = HTONS(ARP_HWTYPE_ETH); | ||
| 408 | BUF->protocol = HTONS(UIP_ETHTYPE_IP); | ||
| 409 | BUF->hwlen = 6; | ||
| 410 | BUF->protolen = 4; | ||
| 411 | BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP); | ||
| 412 | |||
| 413 | uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN]; | ||
| 414 | |||
| 415 | uip_len = sizeof(struct arp_hdr); | ||
| 416 | return; | ||
| 417 | } | ||
| 418 | |||
| 419 | /* Build an ethernet header. */ | ||
| 420 | memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6); | ||
| 421 | } | ||
| 422 | memcpy(IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6); | ||
| 423 | |||
| 424 | IPBUF->ethhdr.type = HTONS(UIP_ETHTYPE_IP); | ||
| 425 | |||
| 426 | uip_len += sizeof(struct uip_eth_hdr); | ||
| 427 | } | ||
| 428 | /*-----------------------------------------------------------------------------------*/ | ||
| 429 | |||
| 430 | /** @} */ | ||
| 431 | /** @} */ | ||
| 432 | |||
