root/webserver/example/freeRTOS/Demo/Common/ethernet/FreeRTOS-uIP/uip_arp.c

Revision 14, 13.8 kB (checked in by phil, 15 years ago)

added unmodified FreeRTOS package V5.4.1 with only web srv demo source for LPC2368 for CrossWorks?

Line 
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.8 2006/06/02 23:36:21 adam Exp $
58  *
59  */
60 #include "uip_arp.h"
61
62 #include <string.h>
63
64 #ifdef __ICCARM__
65         #pragma pack( 1 )
66 #endif
67 struct arp_hdr
68 {
69         struct uip_eth_hdr      ethhdr;
70         u16_t                           hwtype;
71         u16_t                           protocol;
72         u8_t                            hwlen;
73         u8_t                            protolen;
74         u16_t                           opcode;
75         struct uip_eth_addr shwaddr;
76         u16_t                           sipaddr[2];
77         struct uip_eth_addr dhwaddr;
78         u16_t                           dipaddr[2];
79 } PACK_STRUCT_END;
80
81 #ifdef __ICCARM__
82         #pragma pack()
83 #endif
84 #ifdef __ICCARM__
85         #pragma pack( 1 )
86 #endif
87 struct ethip_hdr
88 {
89         struct uip_eth_hdr      ethhdr;
90
91         /* IP header. */
92         u8_t                            vhl, tos, len[2], ipid[2], ipoffset[2], ttl, proto;
93         u16_t                           ipchksum;
94         u16_t                           srcipaddr[2], destipaddr[2];
95 } PACK_STRUCT_END;
96
97 #ifdef __ICCARM__
98         #pragma pack()
99 #endif
100 #define ARP_REQUEST             1
101 #define ARP_REPLY               2
102
103 #define ARP_HWTYPE_ETH  1
104
105 struct arp_entry
106 {
107         u16_t                           ipaddr[2];
108         struct uip_eth_addr ethaddr;
109         u8_t                            time;
110 };
111
112 static const struct uip_eth_addr        broadcast_ethaddr = { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
113 static const u16_t                                      broadcast_ipaddr[2] = { 0xffff, 0xffff };
114
115 static struct arp_entry                         arp_table[UIP_ARPTAB_SIZE];
116 static u16_t                                            ipaddr[2];
117 static u8_t                                                     i, c;
118
119 static u8_t                                                     arptime;
120 static u8_t                                                     tmpage;
121
122 #define BUF             ( ( struct arp_hdr * ) &uip_buf[0] )
123 #define IPBUF   ( ( struct ethip_hdr * ) &uip_buf[0] )
124
125 /*-----------------------------------------------------------------------------------*/
126
127 /**
128  * Initialize the ARP module.
129  *
130  */
131
132 /*-----------------------------------------------------------------------------------*/
133 void uip_arp_init( void )
134 {
135         for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
136         {
137                 memset( arp_table[i].ipaddr, 0, 4 );
138         }
139 }
140
141 /*-----------------------------------------------------------------------------------*/
142
143 /**
144  * Periodic ARP processing function.
145  *
146  * This function performs periodic timer processing in the ARP module
147  * and should be called at regular intervals. The recommended interval
148  * is 10 seconds between the calls.
149  *
150  */
151
152 /*-----------------------------------------------------------------------------------*/
153 void uip_arp_timer( void )
154 {
155         struct arp_entry        *tabptr;
156
157         ++arptime;
158         for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
159         {
160                 tabptr = &arp_table[i];
161                 if( (tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 && arptime - tabptr->time >= UIP_ARP_MAXAGE )
162                 {
163                         memset( tabptr->ipaddr, 0, 4 );
164                 }
165         }
166 }
167
168 /*-----------------------------------------------------------------------------------*/
169 static void uip_arp_update( u16_t *ipaddr, struct uip_eth_addr *ethaddr )
170 {
171         register struct arp_entry       *tabptr;
172
173         /* Walk through the ARP mapping table and try to find an entry to
174      update. If none is found, the IP -> MAC address mapping is
175      inserted in the ARP table. */
176         for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
177         {
178                 tabptr = &arp_table[i];
179
180                 /* Only check those entries that are actually in use. */
181                 if( tabptr->ipaddr[0] != 0 && tabptr->ipaddr[1] != 0 )
182                 {
183                         /* Check if the source IP address of the incoming packet matches
184          the IP address in this ARP table entry. */
185                         if( ipaddr[0] == tabptr->ipaddr[0] && ipaddr[1] == tabptr->ipaddr[1] )
186                         {
187                                 /* An old entry found, update this and return. */
188                                 memcpy( tabptr->ethaddr.addr, ethaddr->addr, 6 );
189                                 tabptr->time = arptime;
190
191                                 return;
192                         }
193                 }
194         }
195
196         /* If we get here, no existing ARP table entry was found, so we
197      create one. */
198
199         /* First, we try to find an unused entry in the ARP table. */
200         for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
201         {
202                 tabptr = &arp_table[i];
203                 if( tabptr->ipaddr[0] == 0 && tabptr->ipaddr[1] == 0 )
204                 {
205                         break;
206                 }
207         }
208
209         /* If no unused entry is found, we try to find the oldest entry and
210      throw it away. */
211         if( i == UIP_ARPTAB_SIZE )
212         {
213                 tmpage = 0;
214                 c = 0;
215                 for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
216                 {
217                         tabptr = &arp_table[i];
218                         if( arptime - tabptr->time > tmpage )
219                         {
220                                 tmpage = arptime - tabptr->time;
221                                 c = i;
222                         }
223                 }
224
225                 i = c;
226                 tabptr = &arp_table[i];
227         }
228
229         /* Now, i is the ARP table entry which we will fill with the new
230      information. */
231         memcpy( tabptr->ipaddr, ipaddr, 4 );
232         memcpy( tabptr->ethaddr.addr, ethaddr->addr, 6 );
233         tabptr->time = arptime;
234 }
235
236 /*-----------------------------------------------------------------------------------*/
237
238 /**
239  * ARP processing for incoming IP packets
240  *
241  * This function should be called by the device driver when an IP
242  * packet has been received. The function will check if the address is
243  * in the ARP cache, and if so the ARP cache entry will be
244  * refreshed. If no ARP cache entry was found, a new one is created.
245  *
246  * This function expects an IP packet with a prepended Ethernet header
247  * in the uip_buf[] buffer, and the length of the packet in the global
248  * variable uip_len.
249  */
250
251 /*-----------------------------------------------------------------------------------*/
252 #if 1
253 void uip_arp_ipin( void )
254 {
255         uip_len -= sizeof( struct uip_eth_hdr );
256
257         /* Only insert/update an entry if the source IP address of the
258      incoming IP packet comes from a host on the local network. */
259         if( (IPBUF->srcipaddr[0] & uip_netmask[0]) != (uip_hostaddr[0] & uip_netmask[0]) )
260         {
261                 return;
262         }
263
264         if( (IPBUF->srcipaddr[1] & uip_netmask[1]) != (uip_hostaddr[1] & uip_netmask[1]) )
265         {
266                 return;
267         }
268
269         uip_arp_update( IPBUF->srcipaddr, &(IPBUF->ethhdr.src) );
270
271         return;
272 }
273
274 #endif /* 0 */
275
276 /*-----------------------------------------------------------------------------------*/
277
278 /**
279  * ARP processing for incoming ARP packets.
280  *
281  * This function should be called by the device driver when an ARP
282  * packet has been received. The function will act differently
283  * depending on the ARP packet type: if it is a reply for a request
284  * that we previously sent out, the ARP cache will be filled in with
285  * the values from the ARP reply. If the incoming ARP packet is an ARP
286  * request for our IP address, an ARP reply packet is created and put
287  * into the uip_buf[] buffer.
288  *
289  * When the function returns, the value of the global variable uip_len
290  * indicates whether the device driver should send out a packet or
291  * not. If uip_len is zero, no packet should be sent. If uip_len is
292  * non-zero, it contains the length of the outbound packet that is
293  * present in the uip_buf[] buffer.
294  *
295  * This function expects an ARP packet with a prepended Ethernet
296  * header in the uip_buf[] buffer, and the length of the packet in the
297  * global variable uip_len.
298  */
299
300 /*-----------------------------------------------------------------------------------*/
301 void uip_arp_arpin( void )
302 {
303         if( uip_len < sizeof(struct arp_hdr) )
304         {
305                 uip_len = 0;
306                 return;
307         }
308
309         uip_len = 0;
310
311         switch( BUF->opcode )
312         {
313                 case HTONS( ARP_REQUEST ):
314                         /* ARP request. If it asked for our address, we send out a
315        reply. */
316                         if( uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr) )
317                         {
318                                 /* First, we register the one who made the request in our ARP
319          table, since it is likely that we will do more communication
320          with this host in the future. */
321                                 uip_arp_update( BUF->sipaddr, &BUF->shwaddr );
322
323                                 /* The reply opcode is 2. */
324                                 BUF->opcode = HTONS( 2 );
325
326                                 memcpy( BUF->dhwaddr.addr, BUF->shwaddr.addr, 6 );
327                                 memcpy( BUF->shwaddr.addr, uip_ethaddr.addr, 6 );
328                                 memcpy( BUF->ethhdr.src.addr, uip_ethaddr.addr, 6 );
329                                 memcpy( BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6 );
330
331                                 BUF->dipaddr[0] = BUF->sipaddr[0];
332                                 BUF->dipaddr[1] = BUF->sipaddr[1];
333                                 BUF->sipaddr[0] = uip_hostaddr[0];
334                                 BUF->sipaddr[1] = uip_hostaddr[1];
335
336                                 BUF->ethhdr.type = HTONS( UIP_ETHTYPE_ARP );
337                                 uip_len = sizeof( struct arp_hdr );
338                         }
339
340                         break;
341
342                 case HTONS( ARP_REPLY ):
343                         /* ARP reply. We insert or update the ARP table if it was meant
344        for us. */
345                         if( uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr) )
346                         {
347                                 uip_arp_update( BUF->sipaddr, &BUF->shwaddr );
348                         }
349
350                         break;
351         }
352
353         return;
354 }
355
356 /*-----------------------------------------------------------------------------------*/
357
358 /**
359  * Prepend Ethernet header to an outbound IP packet and see if we need
360  * to send out an ARP request.
361  *
362  * This function should be called before sending out an IP packet. The
363  * function checks the destination IP address of the IP packet to see
364  * what Ethernet MAC address that should be used as a destination MAC
365  * address on the Ethernet.
366  *
367  * If the destination IP address is in the local network (determined
368  * by logical ANDing of netmask and our IP address), the function
369  * checks the ARP cache to see if an entry for the destination IP
370  * address is found. If so, an Ethernet header is prepended and the
371  * function returns. If no ARP cache entry is found for the
372  * destination IP address, the packet in the uip_buf[] is replaced by
373  * an ARP request packet for the IP address. The IP packet is dropped
374  * and it is assumed that they higher level protocols (e.g., TCP)
375  * eventually will retransmit the dropped packet.
376  *
377  * If the destination IP address is not on the local network, the IP
378  * address of the default router is used instead.
379  *
380  * When the function returns, a packet is present in the uip_buf[]
381  * buffer, and the length of the packet is in the global variable
382  * uip_len.
383  */
384
385 /*-----------------------------------------------------------------------------------*/
386 void uip_arp_out( void )
387 {
388         struct arp_entry        *tabptr;
389
390         /* Find the destination IP address in the ARP table and construct
391      the Ethernet header. If the destination IP addres isn't on the
392      local network, we use the default router's IP address instead.
393
394      If not ARP table entry is found, we overwrite the original IP
395      packet with an ARP request for the IP address. */
396
397         /* First check if destination is a local broadcast. */
398         if( uip_ipaddr_cmp(IPBUF->destipaddr, broadcast_ipaddr) )
399         {
400                 memcpy( IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6 );
401         }
402         else
403         {
404                 /* Check if the destination address is on the local network. */
405                 if( !uip_ipaddr_maskcmp(IPBUF->destipaddr, uip_hostaddr, uip_netmask) )
406                 {
407                         /* Destination address was not on the local network, so we need to
408          use the default router's IP address instead of the destination
409          address when determining the MAC address. */
410                         uip_ipaddr_copy( ipaddr, uip_draddr );
411                 }
412                 else
413                 {
414                         /* Else, we use the destination IP address. */
415                         uip_ipaddr_copy( ipaddr, IPBUF->destipaddr );
416                 }
417
418                 for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
419                 {
420                         tabptr = &arp_table[i];
421                         if( uip_ipaddr_cmp(ipaddr, tabptr->ipaddr) )
422                         {
423                                 break;
424                         }
425                 }
426
427                 if( i == UIP_ARPTAB_SIZE )
428                 {
429                         /* The destination address was not in our ARP table, so we
430          overwrite the IP packet with an ARP request. */
431                         memset( BUF->ethhdr.dest.addr, 0xff, 6 );
432                         memset( BUF->dhwaddr.addr, 0x00, 6 );
433                         memcpy( BUF->ethhdr.src.addr, uip_ethaddr.addr, 6 );
434                         memcpy( BUF->shwaddr.addr, uip_ethaddr.addr, 6 );
435
436                         uip_ipaddr_copy( BUF->dipaddr, ipaddr );
437                         uip_ipaddr_copy( BUF->sipaddr, uip_hostaddr );
438                         BUF->opcode = HTONS( ARP_REQUEST ); /* ARP request. */
439                         BUF->hwtype = HTONS( ARP_HWTYPE_ETH );
440                         BUF->protocol = HTONS( UIP_ETHTYPE_IP );
441                         BUF->hwlen = 6;
442                         BUF->protolen = 4;
443                         BUF->ethhdr.type = HTONS( UIP_ETHTYPE_ARP );
444
445                         uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN];
446
447                         uip_len = sizeof( struct arp_hdr );
448                         return;
449                 }
450
451                 /* Build an ethernet header. */
452                 memcpy( IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6 );
453         }
454
455         memcpy( IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6 );
456
457         IPBUF->ethhdr.type = HTONS( UIP_ETHTYPE_IP );
458
459         uip_len += sizeof( struct uip_eth_hdr );
460 }
461
462 /*-----------------------------------------------------------------------------------*/
463
464 /** @} */
465
466 /** @} */
Note: See TracBrowser for help on using the browser.