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