7 * \defgroup uiparp uIP Address Resolution Protocol
\r
10 * The Address Resolution Protocol ARP is used for mapping between IP
\r
11 * addresses and link level addresses such as the Ethernet MAC
\r
12 * addresses. ARP uses broadcast queries to ask for the link level
\r
13 * address of a known IP address and the host which is configured with
\r
14 * the IP address for which the query was meant, will respond with its
\r
15 * link level address.
\r
17 * \note This ARP implementation only supports Ethernet.
\r
22 * Implementation of the ARP Address Resolution Protocol.
\r
23 * \author Adam Dunkels <adam@dunkels.com>
\r
28 * Copyright (c) 2001-2003, Adam Dunkels.
\r
29 * All rights reserved.
\r
31 * Redistribution and use in source and binary forms, with or without
\r
32 * modification, are permitted provided that the following conditions
\r
34 * 1. Redistributions of source code must retain the above copyright
\r
35 * notice, this list of conditions and the following disclaimer.
\r
36 * 2. Redistributions in binary form must reproduce the above copyright
\r
37 * notice, this list of conditions and the following disclaimer in the
\r
38 * documentation and/or other materials provided with the distribution.
\r
39 * 3. The name of the author may not be used to endorse or promote
\r
40 * products derived from this software without specific prior
\r
41 * written permission.
\r
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
\r
44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
\r
45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
\r
47 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
\r
49 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
\r
51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
52 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
\r
53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
55 * This file is part of the uIP TCP/IP stack.
\r
57 * $Id: uip_arp.c,v 1.5 2008/02/07 01:35:00 adamdunkels Exp $
\r
60 #include "net/uip_arp.h"
\r
64 #include "net/pack_struct_start.h"
\r
67 struct uip_eth_hdr ethhdr;
\r
73 struct uip_eth_addr shwaddr;
\r
74 uip_ipaddr_t sipaddr;
\r
75 struct uip_eth_addr dhwaddr;
\r
76 uip_ipaddr_t dipaddr;
\r
79 #include "net/pack_struct_end.h"
\r
81 #include "net/pack_struct_start.h"
\r
85 struct uip_eth_hdr ethhdr;
\r
88 u8_t vhl, tos, len[2], ipid[2], ipoffset[2], ttl, proto;
\r
90 uip_ipaddr_t srcipaddr, destipaddr;
\r
93 #include "net/pack_struct_end.h"
\r
95 #define ARP_REQUEST 1
\r
98 #define ARP_HWTYPE_ETH 1
\r
102 uip_ipaddr_t ipaddr;
\r
103 struct uip_eth_addr ethaddr;
\r
107 static const struct uip_eth_addr broadcast_ethaddr = { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
\r
108 static const u16_t broadcast_ipaddr[2] = { 0xffff, 0xffff };
\r
110 static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
\r
111 static uip_ipaddr_t ipaddr;
\r
114 static u8_t arptime;
\r
115 static u8_t tmpage;
\r
117 #define BUF ( ( struct arp_hdr * ) &uip_buf[0] )
\r
118 #define IPBUF ( ( struct ethip_hdr * ) &uip_buf[0] )
\r
122 #define PRINTF( ... ) printf( __VA_ARGS__ )
\r
125 //#define PRINTF( ... )
\r
128 /*-----------------------------------------------------------------------------------*/
\r
131 * Initialize the ARP module.
\r
135 /*-----------------------------------------------------------------------------------*/
\r
136 void uip_arp_init( void )
\r
138 for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
\r
140 memset( &arp_table[i].ipaddr, 0, 4 );
\r
144 /*-----------------------------------------------------------------------------------*/
\r
147 * Periodic ARP processing function.
\r
149 * This function performs periodic timer processing in the ARP module
\r
150 * and should be called at regular intervals. The recommended interval
\r
151 * is 10 seconds between the calls.
\r
155 /*-----------------------------------------------------------------------------------*/
\r
156 void uip_arp_timer( void )
\r
158 struct arp_entry *tabptr;
\r
161 for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
\r
163 tabptr = &arp_table[i];
\r
164 if( uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) && arptime - tabptr->time >= UIP_ARP_MAXAGE )
\r
166 memset( &tabptr->ipaddr, 0, 4 );
\r
171 /*-----------------------------------------------------------------------------------*/
\r
172 static void uip_arp_update( uip_ipaddr_t *ipaddr, struct uip_eth_addr *ethaddr )
\r
174 register struct arp_entry *tabptr;
\r
176 /* Walk through the ARP mapping table and try to find an entry to
\r
177 update. If none is found, the IP -> MAC address mapping is
\r
178 inserted in the ARP table. */
\r
179 for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
\r
181 tabptr = &arp_table[i];
\r
183 /* Only check those entries that are actually in use. */
\r
184 if( !uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) )
\r
186 /* Check if the source IP address of the incoming packet matches
\r
187 the IP address in this ARP table entry. */
\r
188 if( uip_ipaddr_cmp(ipaddr, &tabptr->ipaddr) )
\r
190 /* An old entry found, update this and return. */
\r
191 memcpy( tabptr->ethaddr.addr, ethaddr->addr, 6 );
\r
192 tabptr->time = arptime;
\r
199 /* If we get here, no existing ARP table entry was found, so we
\r
202 /* First, we try to find an unused entry in the ARP table. */
\r
203 for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
\r
205 tabptr = &arp_table[i];
\r
206 if( uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) )
\r
212 /* If no unused entry is found, we try to find the oldest entry and
\r
214 if( i == UIP_ARPTAB_SIZE )
\r
218 for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
\r
220 tabptr = &arp_table[i];
\r
221 if( arptime - tabptr->time > tmpage )
\r
223 tmpage = arptime - tabptr->time;
\r
229 tabptr = &arp_table[i];
\r
232 /* Now, i is the ARP table entry which we will fill with the new
\r
234 uip_ipaddr_copy( &tabptr->ipaddr, ipaddr );
\r
235 memcpy( tabptr->ethaddr.addr, ethaddr->addr, 6 );
\r
236 tabptr->time = arptime;
\r
239 /*-----------------------------------------------------------------------------------*/
\r
242 * ARP processing for incoming IP packets
\r
244 * This function should be called by the device driver when an IP
\r
245 * packet has been received. The function will check if the address is
\r
246 * in the ARP cache, and if so the ARP cache entry will be
\r
247 * refreshed. If no ARP cache entry was found, a new one is created.
\r
249 * This function expects an IP packet with a prepended Ethernet header
\r
250 * in the uip_buf[] buffer, and the length of the packet in the global
\r
251 * variable uip_len.
\r
254 /*-----------------------------------------------------------------------------------*/
\r
256 void uip_arp_ipin( void )
\r
258 uip_len -= sizeof( struct uip_eth_hdr );
\r
260 /* Only insert/update an entry if the source IP address of the
\r
261 incoming IP packet comes from a host on the local network. */
\r
262 if( (IPBUF->srcipaddr[0] & uip_netmask[0]) != (uip_hostaddr[0] & uip_netmask[0]) )
\r
267 if( (IPBUF->srcipaddr[1] & uip_netmask[1]) != (uip_hostaddr[1] & uip_netmask[1]) )
\r
272 uip_arp_update( IPBUF->srcipaddr, &(IPBUF->ethhdr.src) );
\r
279 /*-----------------------------------------------------------------------------------*/
\r
282 * ARP processing for incoming ARP packets.
\r
284 * This function should be called by the device driver when an ARP
\r
285 * packet has been received. The function will act differently
\r
286 * depending on the ARP packet type: if it is a reply for a request
\r
287 * that we previously sent out, the ARP cache will be filled in with
\r
288 * the values from the ARP reply. If the incoming ARP packet is an ARP
\r
289 * request for our IP address, an ARP reply packet is created and put
\r
290 * into the uip_buf[] buffer.
\r
292 * When the function returns, the value of the global variable uip_len
\r
293 * indicates whether the device driver should send out a packet or
\r
294 * not. If uip_len is zero, no packet should be sent. If uip_len is
\r
295 * non-zero, it contains the length of the outbound packet that is
\r
296 * present in the uip_buf[] buffer.
\r
298 * This function expects an ARP packet with a prepended Ethernet
\r
299 * header in the uip_buf[] buffer, and the length of the packet in the
\r
300 * global variable uip_len.
\r
303 /*-----------------------------------------------------------------------------------*/
\r
304 void uip_arp_arpin( void )
\r
306 if( uip_len < sizeof(struct arp_hdr) )
\r
314 switch( BUF->opcode )
\r
316 case HTONS( ARP_REQUEST ):
\r
317 /* ARP request. If it asked for our address, we send out a
\r
320 /* if(BUF->dipaddr[0] == uip_hostaddr[0] &&
\r
321 BUF->dipaddr[1] == uip_hostaddr[1]) {*/
\r
323 //PRINTF( "uip_arp_arpin: request for %d.%d.%d.%d (we are %d.%d.%d.%d)\n", BUF->dipaddr.u8[0], BUF->dipaddr.u8[1], BUF->dipaddr.u8[2],
\r
324 //BUF->dipaddr.u8[3], uip_hostaddr.u8[0], uip_hostaddr.u8[1], uip_hostaddr.u8[2], uip_hostaddr.u8[3] );
\r
325 if( uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr) )
\r
327 /* First, we register the one who made the request in our ARP
\r
328 table, since it is likely that we will do more communication
\r
329 with this host in the future. */
\r
330 uip_arp_update( &BUF->sipaddr, &BUF->shwaddr );
\r
332 BUF->opcode = HTONS( ARP_REPLY );
\r
334 memcpy( BUF->dhwaddr.addr, BUF->shwaddr.addr, 6 );
\r
335 memcpy( BUF->shwaddr.addr, uip_ethaddr.addr, 6 );
\r
336 memcpy( BUF->ethhdr.src.addr, uip_ethaddr.addr, 6 );
\r
337 memcpy( BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6 );
\r
339 uip_ipaddr_copy( &BUF->dipaddr, &BUF->sipaddr );
\r
340 uip_ipaddr_copy( &BUF->sipaddr, &uip_hostaddr );
\r
342 BUF->ethhdr.type = HTONS( UIP_ETHTYPE_ARP );
\r
343 uip_len = sizeof( struct arp_hdr );
\r
348 case HTONS( ARP_REPLY ):
\r
349 /* ARP reply. We insert or update the ARP table if it was meant
\r
351 if( uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr) )
\r
353 uip_arp_update( &BUF->sipaddr, &BUF->shwaddr );
\r
362 /*-----------------------------------------------------------------------------------*/
\r
365 * Prepend Ethernet header to an outbound IP packet and see if we need
\r
366 * to send out an ARP request.
\r
368 * This function should be called before sending out an IP packet. The
\r
369 * function checks the destination IP address of the IP packet to see
\r
370 * what Ethernet MAC address that should be used as a destination MAC
\r
371 * address on the Ethernet.
\r
373 * If the destination IP address is in the local network (determined
\r
374 * by logical ANDing of netmask and our IP address), the function
\r
375 * checks the ARP cache to see if an entry for the destination IP
\r
376 * address is found. If so, an Ethernet header is prepended and the
\r
377 * function returns. If no ARP cache entry is found for the
\r
378 * destination IP address, the packet in the uip_buf[] is replaced by
\r
379 * an ARP request packet for the IP address. The IP packet is dropped
\r
380 * and it is assumed that they higher level protocols (e.g., TCP)
\r
381 * eventually will retransmit the dropped packet.
\r
383 * If the destination IP address is not on the local network, the IP
\r
384 * address of the default router is used instead.
\r
386 * When the function returns, a packet is present in the uip_buf[]
\r
387 * buffer, and the length of the packet is in the global variable
\r
391 /*-----------------------------------------------------------------------------------*/
\r
392 void uip_arp_out( void )
\r
394 struct arp_entry *tabptr;
\r
396 /* Find the destination IP address in the ARP table and construct
\r
397 the Ethernet header. If the destination IP addres isn't on the
\r
398 local network, we use the default router's IP address instead.
\r
400 If not ARP table entry is found, we overwrite the original IP
\r
401 packet with an ARP request for the IP address. */
\r
403 /* First check if destination is a local broadcast. */
\r
404 if( uip_ipaddr_cmp(&IPBUF->destipaddr, &uip_broadcast_addr) )
\r
406 memcpy( IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6 );
\r
410 /* Check if the destination address is on the local network. */
\r
411 if( !uip_ipaddr_maskcmp(&IPBUF->destipaddr, &uip_hostaddr, &uip_netmask) )
\r
413 /* Destination address was not on the local network, so we need to
\r
414 use the default router's IP address instead of the destination
\r
415 address when determining the MAC address. */
\r
416 uip_ipaddr_copy( &ipaddr, &uip_draddr );
\r
420 /* Else, we use the destination IP address. */
\r
421 uip_ipaddr_copy( &ipaddr, &IPBUF->destipaddr );
\r
424 for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
\r
426 tabptr = &arp_table[i];
\r
427 if( uip_ipaddr_cmp(&ipaddr, &tabptr->ipaddr) )
\r
433 if( i == UIP_ARPTAB_SIZE )
\r
435 /* The destination address was not in our ARP table, so we
\r
436 overwrite the IP packet with an ARP request. */
\r
437 memset( BUF->ethhdr.dest.addr, 0xff, 6 );
\r
438 memset( BUF->dhwaddr.addr, 0x00, 6 );
\r
439 memcpy( BUF->ethhdr.src.addr, uip_ethaddr.addr, 6 );
\r
440 memcpy( BUF->shwaddr.addr, uip_ethaddr.addr, 6 );
\r
442 uip_ipaddr_copy( &BUF->dipaddr, &ipaddr );
\r
443 uip_ipaddr_copy( &BUF->sipaddr, &uip_hostaddr );
\r
444 BUF->opcode = HTONS( ARP_REQUEST ); /* ARP request. */
\r
445 BUF->hwtype = HTONS( ARP_HWTYPE_ETH );
\r
446 BUF->protocol = HTONS( UIP_ETHTYPE_IP );
\r
449 BUF->ethhdr.type = HTONS( UIP_ETHTYPE_ARP );
\r
451 uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN];
\r
453 uip_len = sizeof( struct arp_hdr );
\r
457 /* Build an ethernet header. */
\r
458 memcpy( IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6 );
\r
461 memcpy( IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6 );
\r
463 IPBUF->ethhdr.type = HTONS( UIP_ETHTYPE_IP );
\r
465 uip_len += sizeof( struct uip_eth_hdr );
\r
468 /*-----------------------------------------------------------------------------------*/
\r