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