adamdunkels | 846aabf | 2003-09-05 21:03:35 +0000 | [diff] [blame] | 1 | /** |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 2 | * \addtogroup uip |
| 3 | * @{ |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * \defgroup uiparp uIP Address Resolution Protocol |
| 8 | * @{ |
| 9 | * |
adamdunkels | 846aabf | 2003-09-05 21:03:35 +0000 | [diff] [blame] | 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 | */ |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * \file |
| 22 | * Implementation of the ARP Address Resolution Protocol. |
| 23 | * \author Adam Dunkels <adam@dunkels.com> |
| 24 | * |
| 25 | */ |
adamdunkels | 846aabf | 2003-09-05 21:03:35 +0000 | [diff] [blame] | 26 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 27 | /* |
adamdunkels | 846aabf | 2003-09-05 21:03:35 +0000 | [diff] [blame] | 28 | * Copyright (c) 2001-2003, Adam Dunkels. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 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. |
adamdunkels | 846aabf | 2003-09-05 21:03:35 +0000 | [diff] [blame] | 39 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 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 | * |
oliverschmidt | 3742087 | 2005-02-23 22:38:43 +0000 | [diff] [blame] | 57 | * $Id: uip_arp.c,v 1.15 2005/02/23 22:40:40 oliverschmidt Exp $ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 58 | * |
| 59 | */ |
| 60 | |
| 61 | |
| 62 | #include "uip_arp.h" |
| 63 | |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 64 | #include <string.h> |
| 65 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 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 | u16_t sipaddr[2]; |
| 75 | struct uip_eth_addr dhwaddr; |
| 76 | u16_t dipaddr[2]; |
| 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 | u16_t srcipaddr[2], |
| 91 | destipaddr[2]; |
| 92 | }; |
| 93 | |
| 94 | #define ARP_REQUEST 1 |
| 95 | #define ARP_REPLY 2 |
| 96 | |
| 97 | #define ARP_HWTYPE_ETH 1 |
| 98 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 99 | struct arp_entry { |
| 100 | u16_t ipaddr[2]; |
| 101 | struct uip_eth_addr ethaddr; |
| 102 | u8_t time; |
| 103 | }; |
| 104 | |
adamdunkels | 5f64c24 | 2004-09-18 20:18:40 +0000 | [diff] [blame] | 105 | static const struct uip_eth_addr broadcast_ethaddr = |
| 106 | {{0xff,0xff,0xff,0xff,0xff,0xff}}; |
| 107 | static const u16_t broadcast_ipaddr[2] = {0xffff,0xffff}; |
| 108 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 109 | static struct arp_entry arp_table[UIP_ARPTAB_SIZE]; |
| 110 | static u16_t ipaddr[2]; |
| 111 | static u8_t i, c; |
| 112 | |
| 113 | static u8_t arptime; |
| 114 | static u8_t tmpage; |
| 115 | |
| 116 | #define BUF ((struct arp_hdr *)&uip_buf[0]) |
| 117 | #define IPBUF ((struct ethip_hdr *)&uip_buf[0]) |
| 118 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 846aabf | 2003-09-05 21:03:35 +0000 | [diff] [blame] | 119 | /** |
| 120 | * Initialize the ARP module. |
| 121 | * |
| 122 | */ |
| 123 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 124 | void |
| 125 | uip_arp_init(void) |
| 126 | { |
| 127 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 128 | memset(arp_table[i].ipaddr, 0, 4); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 846aabf | 2003-09-05 21:03:35 +0000 | [diff] [blame] | 132 | /** |
| 133 | * Periodic ARP processing function. |
| 134 | * |
| 135 | * This function performs periodic timer processing in the ARP module |
| 136 | * and should be called at regular intervals. The recommended interval |
| 137 | * is 10 seconds between the calls. |
| 138 | * |
| 139 | */ |
| 140 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 141 | void |
| 142 | uip_arp_timer(void) |
| 143 | { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 144 | struct arp_entry *tabptr; |
| 145 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 146 | ++arptime; |
| 147 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 148 | tabptr = &arp_table[i]; |
| 149 | if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 && |
| 150 | arptime - tabptr->time >= UIP_ARP_MAXAGE) { |
| 151 | memset(tabptr->ipaddr, 0, 4); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | /*-----------------------------------------------------------------------------------*/ |
| 157 | static void |
| 158 | uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr) |
| 159 | { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 160 | register struct arp_entry *tabptr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 161 | /* Walk through the ARP mapping table and try to find an entry to |
| 162 | update. If none is found, the IP -> MAC address mapping is |
| 163 | inserted in the ARP table. */ |
| 164 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 165 | |
| 166 | tabptr = &arp_table[i]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 167 | /* Only check those entries that are actually in use. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 168 | if(tabptr->ipaddr[0] != 0 && |
| 169 | tabptr->ipaddr[1] != 0) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 170 | |
| 171 | /* Check if the source IP address of the incoming packet matches |
| 172 | the IP address in this ARP table entry. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 173 | if(ipaddr[0] == tabptr->ipaddr[0] && |
| 174 | ipaddr[1] == tabptr->ipaddr[1]) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 175 | |
| 176 | /* An old entry found, update this and return. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 177 | memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); |
| 178 | tabptr->time = arptime; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 179 | |
| 180 | return; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* If we get here, no existing ARP table entry was found, so we |
| 186 | create one. */ |
| 187 | |
| 188 | /* First, we try to find an unused entry in the ARP table. */ |
| 189 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 190 | tabptr = &arp_table[i]; |
| 191 | if(tabptr->ipaddr[0] == 0 && |
| 192 | tabptr->ipaddr[1] == 0) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* If no unused entry is found, we try to find the oldest entry and |
| 198 | throw it away. */ |
| 199 | if(i == UIP_ARPTAB_SIZE) { |
| 200 | tmpage = 0; |
| 201 | c = 0; |
| 202 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 203 | tabptr = &arp_table[i]; |
| 204 | if(arptime - tabptr->time > tmpage) { |
| 205 | tmpage = arptime - tabptr->time; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 206 | c = i; |
| 207 | } |
| 208 | } |
| 209 | i = c; |
adamdunkels | 5da9902 | 2004-02-24 10:13:55 +0000 | [diff] [blame] | 210 | tabptr = &arp_table[i]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* Now, i is the ARP table entry which we will fill with the new |
| 214 | information. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 215 | memcpy(tabptr->ipaddr, ipaddr, 4); |
| 216 | memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 217 | tabptr->time = arptime; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 218 | } |
| 219 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 846aabf | 2003-09-05 21:03:35 +0000 | [diff] [blame] | 220 | /** |
| 221 | * ARP processing for incoming IP packets |
| 222 | * |
| 223 | * This function should be called by the device driver when an IP |
| 224 | * packet has been received. The function will check if the address is |
| 225 | * in the ARP cache, and if so the ARP cache entry will be |
| 226 | * refreshed. If no ARP cache entry was found, a new one is created. |
| 227 | * |
| 228 | * This function expects an IP packet with a prepended Ethernet header |
| 229 | * in the uip_buf[] buffer, and the length of the packet in the global |
| 230 | * variable uip_len. |
| 231 | */ |
| 232 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 8c36501 | 2004-03-25 09:46:10 +0000 | [diff] [blame] | 233 | #if 0 |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 234 | void |
| 235 | uip_arp_ipin(void) |
| 236 | { |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 237 | uip_len -= sizeof(struct uip_eth_hdr); |
| 238 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 239 | /* Only insert/update an entry if the source IP address of the |
| 240 | incoming IP packet comes from a host on the local network. */ |
adamdunkels | 2e649e1 | 2004-06-06 06:16:41 +0000 | [diff] [blame] | 241 | if((IPBUF->srcipaddr[0] & uip_netmask[0]) != |
| 242 | (uip_hostaddr[0] & uip_netmask[0])) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 243 | return; |
| 244 | } |
adamdunkels | 2e649e1 | 2004-06-06 06:16:41 +0000 | [diff] [blame] | 245 | if((IPBUF->srcipaddr[1] & uip_netmask[1]) != |
| 246 | (uip_hostaddr[1] & uip_netmask[1])) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src)); |
| 250 | |
| 251 | return; |
| 252 | } |
adamdunkels | 8c36501 | 2004-03-25 09:46:10 +0000 | [diff] [blame] | 253 | #endif /* 0 */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 254 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 846aabf | 2003-09-05 21:03:35 +0000 | [diff] [blame] | 255 | /** |
| 256 | * ARP processing for incoming ARP packets. |
| 257 | * |
| 258 | * This function should be called by the device driver when an ARP |
| 259 | * packet has been received. The function will act differently |
| 260 | * depending on the ARP packet type: if it is a reply for a request |
| 261 | * that we previously sent out, the ARP cache will be filled in with |
| 262 | * the values from the ARP reply. If the incoming ARP packet is an ARP |
| 263 | * request for our IP address, an ARP reply packet is created and put |
| 264 | * into the uip_buf[] buffer. |
| 265 | * |
| 266 | * When the function returns, the value of the global variable uip_len |
| 267 | * indicates whether the device driver should send out a packet or |
| 268 | * not. If uip_len is zero, no packet should be sent. If uip_len is |
| 269 | * non-zero, it contains the length of the outbound packet that is |
| 270 | * present in the uip_buf[] buffer. |
| 271 | * |
| 272 | * This function expects an ARP packet with a prepended Ethernet |
| 273 | * header in the uip_buf[] buffer, and the length of the packet in the |
| 274 | * global variable uip_len. |
| 275 | */ |
| 276 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 277 | void |
| 278 | uip_arp_arpin(void) |
| 279 | { |
adamdunkels | 2e649e1 | 2004-06-06 06:16:41 +0000 | [diff] [blame] | 280 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 281 | if(uip_len < sizeof(struct arp_hdr)) { |
| 282 | uip_len = 0; |
| 283 | return; |
| 284 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 285 | uip_len = 0; |
| 286 | |
| 287 | switch(BUF->opcode) { |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 288 | case HTONS(ARP_REQUEST): |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 289 | /* ARP request. If it asked for our address, we send out a |
adamdunkels | 2e649e1 | 2004-06-06 06:16:41 +0000 | [diff] [blame] | 290 | reply. */ |
| 291 | /* if(BUF->dipaddr[0] == uip_hostaddr[0] && |
| 292 | BUF->dipaddr[1] == uip_hostaddr[1]) {*/ |
| 293 | if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) { |
adamdunkels | 8c36501 | 2004-03-25 09:46:10 +0000 | [diff] [blame] | 294 | /* First, we register the one who made the request in our ARP |
| 295 | table, since it is likely that we will do more communication |
| 296 | with this host in the future. */ |
| 297 | uip_arp_update(BUF->sipaddr, &BUF->shwaddr); |
| 298 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 299 | /* The reply opcode is 2. */ |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 300 | BUF->opcode = HTONS(2); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 301 | |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 302 | memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6); |
| 303 | memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6); |
| 304 | memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6); |
| 305 | memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6); |
adamdunkels | 3ea09e6 | 2003-08-24 22:40:46 +0000 | [diff] [blame] | 306 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 307 | BUF->dipaddr[0] = BUF->sipaddr[0]; |
| 308 | BUF->dipaddr[1] = BUF->sipaddr[1]; |
| 309 | BUF->sipaddr[0] = uip_hostaddr[0]; |
| 310 | BUF->sipaddr[1] = uip_hostaddr[1]; |
| 311 | |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 312 | BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 313 | uip_len = sizeof(struct arp_hdr); |
| 314 | } |
| 315 | break; |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 316 | case HTONS(ARP_REPLY): |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 317 | /* ARP reply. We insert or update the ARP table if it was meant |
| 318 | for us. */ |
adamdunkels | 2e649e1 | 2004-06-06 06:16:41 +0000 | [diff] [blame] | 319 | /* if(BUF->dipaddr[0] == uip_hostaddr[0] && |
| 320 | BUF->dipaddr[1] == uip_hostaddr[1]) {*/ |
| 321 | if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 322 | uip_arp_update(BUF->sipaddr, &BUF->shwaddr); |
| 323 | } |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | return; |
| 328 | } |
| 329 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 846aabf | 2003-09-05 21:03:35 +0000 | [diff] [blame] | 330 | /** |
| 331 | * Prepend Ethernet header to an outbound IP packet and see if we need |
| 332 | * to send out an ARP request. |
| 333 | * |
| 334 | * This function should be called before sending out an IP packet. The |
| 335 | * function checks the destination IP address of the IP packet to see |
| 336 | * what Ethernet MAC address that should be used as a destination MAC |
| 337 | * address on the Ethernet. |
| 338 | * |
| 339 | * If the destination IP address is in the local network (determined |
| 340 | * by logical ANDing of netmask and our IP address), the function |
| 341 | * checks the ARP cache to see if an entry for the destination IP |
| 342 | * address is found. If so, an Ethernet header is prepended and the |
| 343 | * function returns. If no ARP cache entry is found for the |
| 344 | * destination IP address, the packet in the uip_buf[] is replaced by |
| 345 | * an ARP request packet for the IP address. The IP packet is dropped |
| 346 | * and it is assumed that they higher level protocols (e.g., TCP) |
| 347 | * eventually will retransmit the dropped packet. |
| 348 | * |
| 349 | * If the destination IP address is not on the local network, the IP |
| 350 | * address of the default router is used instead. |
| 351 | * |
| 352 | * When the function returns, a packet is present in the uip_buf[] |
| 353 | * buffer, and the length of the packet is in the global variable |
| 354 | * uip_len. |
| 355 | */ |
| 356 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 357 | void |
| 358 | uip_arp_out(void) |
| 359 | { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 360 | struct arp_entry *tabptr; |
adamdunkels | 8c36501 | 2004-03-25 09:46:10 +0000 | [diff] [blame] | 361 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 362 | /* Find the destination IP address in the ARP table and construct |
| 363 | the Ethernet header. If the destination IP addres isn't on the |
| 364 | local network, we use the default router's IP address instead. |
| 365 | |
| 366 | If not ARP table entry is found, we overwrite the original IP |
| 367 | packet with an ARP request for the IP address. */ |
| 368 | |
adamdunkels | 5f64c24 | 2004-09-18 20:18:40 +0000 | [diff] [blame] | 369 | /* First check if destination is a local broadcast. */ |
| 370 | if(uip_ipaddr_cmp(IPBUF->destipaddr, broadcast_ipaddr)) { |
| 371 | memcpy(IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 372 | } else { |
adamdunkels | 5f64c24 | 2004-09-18 20:18:40 +0000 | [diff] [blame] | 373 | /* Check if the destination address is on the local network. */ |
| 374 | if(!uip_ipaddr_maskcmp(IPBUF->destipaddr, uip_hostaddr, uip_netmask)) { |
| 375 | /* Destination address was not on the local network, so we need to |
| 376 | use the default router's IP address instead of the destination |
| 377 | address when determining the MAC address. */ |
| 378 | uip_ipaddr_copy(ipaddr, uip_draddr); |
| 379 | } else { |
| 380 | /* Else, we use the destination IP address. */ |
| 381 | uip_ipaddr_copy(ipaddr, IPBUF->destipaddr); |
adamdunkels | 2e649e1 | 2004-06-06 06:16:41 +0000 | [diff] [blame] | 382 | } |
adamdunkels | 5f64c24 | 2004-09-18 20:18:40 +0000 | [diff] [blame] | 383 | |
| 384 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { |
| 385 | tabptr = &arp_table[i]; |
| 386 | if(uip_ipaddr_cmp(ipaddr, tabptr->ipaddr)) { |
| 387 | break; |
| 388 | } |
| 389 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 390 | |
adamdunkels | 5f64c24 | 2004-09-18 20:18:40 +0000 | [diff] [blame] | 391 | if(i == UIP_ARPTAB_SIZE) { |
| 392 | /* The destination address was not in our ARP table, so we |
| 393 | overwrite the IP packet with an ARP request. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 394 | |
adamdunkels | 5f64c24 | 2004-09-18 20:18:40 +0000 | [diff] [blame] | 395 | memset(BUF->ethhdr.dest.addr, 0xff, 6); |
| 396 | memset(BUF->dhwaddr.addr, 0x00, 6); |
| 397 | memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6); |
| 398 | memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 399 | |
adamdunkels | 5f64c24 | 2004-09-18 20:18:40 +0000 | [diff] [blame] | 400 | uip_ipaddr_copy(BUF->dipaddr, ipaddr); |
| 401 | uip_ipaddr_copy(BUF->sipaddr, uip_hostaddr); |
| 402 | BUF->opcode = HTONS(ARP_REQUEST); /* ARP request. */ |
| 403 | BUF->hwtype = HTONS(ARP_HWTYPE_ETH); |
| 404 | BUF->protocol = HTONS(UIP_ETHTYPE_IP); |
| 405 | BUF->hwlen = 6; |
| 406 | BUF->protolen = 4; |
| 407 | BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 408 | |
oliverschmidt | 3742087 | 2005-02-23 22:38:43 +0000 | [diff] [blame] | 409 | uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 410 | |
adamdunkels | 5f64c24 | 2004-09-18 20:18:40 +0000 | [diff] [blame] | 411 | uip_len = sizeof(struct arp_hdr); |
| 412 | return; |
| 413 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 414 | |
adamdunkels | 5f64c24 | 2004-09-18 20:18:40 +0000 | [diff] [blame] | 415 | /* Build an ethernet header. */ |
| 416 | memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6); |
| 417 | } |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 418 | memcpy(IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6); |
adamdunkels | 3ea09e6 | 2003-08-24 22:40:46 +0000 | [diff] [blame] | 419 | |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 420 | IPBUF->ethhdr.type = HTONS(UIP_ETHTYPE_IP); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 421 | |
| 422 | uip_len += sizeof(struct uip_eth_hdr); |
| 423 | } |
| 424 | /*-----------------------------------------------------------------------------------*/ |
| 425 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 426 | /** @} */ |
| 427 | /** @} */ |