blob: 3890dda59bf41eef54aea3fd8a3be73bd922eae8 [file] [log] [blame]
adamdunkels846aabf2003-09-05 21:03:35 +00001/**
adamdunkels0170b082003-10-01 11:25:37 +00002 * \addtogroup uip
3 * @{
4 */
5
6/**
7 * \defgroup uiparp uIP Address Resolution Protocol
8 * @{
9 *
adamdunkels846aabf2003-09-05 21:03:35 +000010 * 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 */
adamdunkels0170b082003-10-01 11:25:37 +000019
20/**
21 * \file
22 * Implementation of the ARP Address Resolution Protocol.
23 * \author Adam Dunkels <adam@dunkels.com>
24 *
25 */
adamdunkels846aabf2003-09-05 21:03:35 +000026
adamdunkelsca9ddcb2003-03-19 14:13:31 +000027/*
adamdunkels846aabf2003-09-05 21:03:35 +000028 * Copyright (c) 2001-2003, Adam Dunkels.
adamdunkelsca9ddcb2003-03-19 14:13:31 +000029 * 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.
adamdunkels846aabf2003-09-05 21:03:35 +000039 * 3. The name of the author may not be used to endorse or promote
adamdunkelsca9ddcb2003-03-19 14:13:31 +000040 * 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 *
adamdunkels8c365012004-03-25 09:46:10 +000057 * $Id: uip_arp.c,v 1.11 2004/03/25 09:46:10 adamdunkels Exp $
adamdunkelsca9ddcb2003-03-19 14:13:31 +000058 *
59 */
60
61
62#include "uip_arp.h"
63
adamdunkelsb489e7a2003-10-14 11:12:50 +000064#include <string.h>
65
adamdunkelsca9ddcb2003-03-19 14:13:31 +000066struct 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
79struct 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
adamdunkelsca9ddcb2003-03-19 14:13:31 +000099struct arp_entry {
100 u16_t ipaddr[2];
101 struct uip_eth_addr ethaddr;
102 u8_t time;
103};
104
adamdunkelsb489e7a2003-10-14 11:12:50 +0000105#if UIP_FIXEDETHADDR
106const struct uip_eth_addr uip_ethaddr = {{UIP_ETHADDR0,
107 UIP_ETHADDR1,
108 UIP_ETHADDR2,
109 UIP_ETHADDR3,
110 UIP_ETHADDR4,
111 UIP_ETHADDR5}};
112#else
113struct uip_eth_addr uip_ethaddr = {{0,0,0,0,0,0}};
114#endif
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000115
116static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
117static u16_t ipaddr[2];
118static u8_t i, c;
119
120static u8_t arptime;
121static u8_t tmpage;
122
123#define BUF ((struct arp_hdr *)&uip_buf[0])
124#define IPBUF ((struct ethip_hdr *)&uip_buf[0])
125/*-----------------------------------------------------------------------------------*/
adamdunkels846aabf2003-09-05 21:03:35 +0000126/**
127 * Initialize the ARP module.
128 *
129 */
130/*-----------------------------------------------------------------------------------*/
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000131void
132uip_arp_init(void)
133{
134 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000135 memset(arp_table[i].ipaddr, 0, 4);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000136 }
137}
138/*-----------------------------------------------------------------------------------*/
adamdunkels846aabf2003-09-05 21:03:35 +0000139/**
140 * Periodic ARP processing function.
141 *
142 * This function performs periodic timer processing in the ARP module
143 * and should be called at regular intervals. The recommended interval
144 * is 10 seconds between the calls.
145 *
146 */
147/*-----------------------------------------------------------------------------------*/
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000148void
149uip_arp_timer(void)
150{
adamdunkels23664022003-08-05 13:51:50 +0000151 struct arp_entry *tabptr;
152
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000153 ++arptime;
154 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000155 tabptr = &arp_table[i];
156 if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 &&
157 arptime - tabptr->time >= UIP_ARP_MAXAGE) {
158 memset(tabptr->ipaddr, 0, 4);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000159 }
160 }
161
162}
163/*-----------------------------------------------------------------------------------*/
164static void
165uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr)
166{
adamdunkels23664022003-08-05 13:51:50 +0000167 register struct arp_entry *tabptr;
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000168 /* Walk through the ARP mapping table and try to find an entry to
169 update. If none is found, the IP -> MAC address mapping is
170 inserted in the ARP table. */
171 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000172
173 tabptr = &arp_table[i];
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000174 /* Only check those entries that are actually in use. */
adamdunkels23664022003-08-05 13:51:50 +0000175 if(tabptr->ipaddr[0] != 0 &&
176 tabptr->ipaddr[1] != 0) {
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000177
178 /* Check if the source IP address of the incoming packet matches
179 the IP address in this ARP table entry. */
adamdunkels23664022003-08-05 13:51:50 +0000180 if(ipaddr[0] == tabptr->ipaddr[0] &&
181 ipaddr[1] == tabptr->ipaddr[1]) {
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000182
183 /* An old entry found, update this and return. */
adamdunkels23664022003-08-05 13:51:50 +0000184 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
185 tabptr->time = arptime;
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000186
187 return;
188 }
189 }
190 }
191
192 /* If we get here, no existing ARP table entry was found, so we
193 create one. */
194
195 /* First, we try to find an unused entry in the ARP table. */
196 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000197 tabptr = &arp_table[i];
198 if(tabptr->ipaddr[0] == 0 &&
199 tabptr->ipaddr[1] == 0) {
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000200 break;
201 }
202 }
203
204 /* If no unused entry is found, we try to find the oldest entry and
205 throw it away. */
206 if(i == UIP_ARPTAB_SIZE) {
207 tmpage = 0;
208 c = 0;
209 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000210 tabptr = &arp_table[i];
211 if(arptime - tabptr->time > tmpage) {
212 tmpage = arptime - tabptr->time;
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000213 c = i;
214 }
215 }
216 i = c;
adamdunkels5da99022004-02-24 10:13:55 +0000217 tabptr = &arp_table[i];
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000218 }
219
220 /* Now, i is the ARP table entry which we will fill with the new
221 information. */
adamdunkels23664022003-08-05 13:51:50 +0000222 memcpy(tabptr->ipaddr, ipaddr, 4);
223 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
adamdunkels23664022003-08-05 13:51:50 +0000224 tabptr->time = arptime;
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000225}
226/*-----------------------------------------------------------------------------------*/
adamdunkels846aabf2003-09-05 21:03:35 +0000227/**
228 * ARP processing for incoming IP packets
229 *
230 * This function should be called by the device driver when an IP
231 * packet has been received. The function will check if the address is
232 * in the ARP cache, and if so the ARP cache entry will be
233 * refreshed. If no ARP cache entry was found, a new one is created.
234 *
235 * This function expects an IP packet with a prepended Ethernet header
236 * in the uip_buf[] buffer, and the length of the packet in the global
237 * variable uip_len.
238 */
239/*-----------------------------------------------------------------------------------*/
adamdunkels8c365012004-03-25 09:46:10 +0000240#if 0
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000241void
242uip_arp_ipin(void)
243{
adamdunkelsb489e7a2003-10-14 11:12:50 +0000244 uip_len -= sizeof(struct uip_eth_hdr);
245
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000246 /* Only insert/update an entry if the source IP address of the
247 incoming IP packet comes from a host on the local network. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000248 if((IPBUF->srcipaddr[0] & uip_arp_netmask[0]) !=
249 (uip_hostaddr[0] & uip_arp_netmask[0])) {
250 return;
251 }
252 if((IPBUF->srcipaddr[1] & uip_arp_netmask[1]) !=
253 (uip_hostaddr[1] & uip_arp_netmask[1])) {
254 return;
255 }
256 uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src));
257
258 return;
259}
adamdunkels8c365012004-03-25 09:46:10 +0000260#endif /* 0 */
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000261/*-----------------------------------------------------------------------------------*/
adamdunkels846aabf2003-09-05 21:03:35 +0000262/**
263 * ARP processing for incoming ARP packets.
264 *
265 * This function should be called by the device driver when an ARP
266 * packet has been received. The function will act differently
267 * depending on the ARP packet type: if it is a reply for a request
268 * that we previously sent out, the ARP cache will be filled in with
269 * the values from the ARP reply. If the incoming ARP packet is an ARP
270 * request for our IP address, an ARP reply packet is created and put
271 * into the uip_buf[] buffer.
272 *
273 * When the function returns, the value of the global variable uip_len
274 * indicates whether the device driver should send out a packet or
275 * not. If uip_len is zero, no packet should be sent. If uip_len is
276 * non-zero, it contains the length of the outbound packet that is
277 * present in the uip_buf[] buffer.
278 *
279 * This function expects an ARP packet with a prepended Ethernet
280 * header in the uip_buf[] buffer, and the length of the packet in the
281 * global variable uip_len.
282 */
283/*-----------------------------------------------------------------------------------*/
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000284void
285uip_arp_arpin(void)
286{
287
288 if(uip_len < sizeof(struct arp_hdr)) {
289 uip_len = 0;
290 return;
291 }
292
293 uip_len = 0;
294
295 switch(BUF->opcode) {
adamdunkels47ec7fa2003-03-28 12:11:17 +0000296 case HTONS(ARP_REQUEST):
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000297 /* ARP request. If it asked for our address, we send out a
298 reply. */
299 if(BUF->dipaddr[0] == uip_hostaddr[0] &&
300 BUF->dipaddr[1] == uip_hostaddr[1]) {
adamdunkels8c365012004-03-25 09:46:10 +0000301
302 /* First, we register the one who made the request in our ARP
303 table, since it is likely that we will do more communication
304 with this host in the future. */
305 uip_arp_update(BUF->sipaddr, &BUF->shwaddr);
306
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000307 /* The reply opcode is 2. */
adamdunkels47ec7fa2003-03-28 12:11:17 +0000308 BUF->opcode = HTONS(2);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000309
adamdunkels23664022003-08-05 13:51:50 +0000310 memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6);
311 memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
312 memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
313 memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);
adamdunkels3ea09e62003-08-24 22:40:46 +0000314
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000315 BUF->dipaddr[0] = BUF->sipaddr[0];
316 BUF->dipaddr[1] = BUF->sipaddr[1];
317 BUF->sipaddr[0] = uip_hostaddr[0];
318 BUF->sipaddr[1] = uip_hostaddr[1];
319
adamdunkels47ec7fa2003-03-28 12:11:17 +0000320 BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000321 uip_len = sizeof(struct arp_hdr);
322 }
323 break;
adamdunkels47ec7fa2003-03-28 12:11:17 +0000324 case HTONS(ARP_REPLY):
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000325 /* ARP reply. We insert or update the ARP table if it was meant
326 for us. */
327 if(BUF->dipaddr[0] == uip_hostaddr[0] &&
328 BUF->dipaddr[1] == uip_hostaddr[1]) {
329
330 uip_arp_update(BUF->sipaddr, &BUF->shwaddr);
331 }
332 break;
333 }
334
335 return;
336}
337/*-----------------------------------------------------------------------------------*/
adamdunkels846aabf2003-09-05 21:03:35 +0000338/**
339 * Prepend Ethernet header to an outbound IP packet and see if we need
340 * to send out an ARP request.
341 *
342 * This function should be called before sending out an IP packet. The
343 * function checks the destination IP address of the IP packet to see
344 * what Ethernet MAC address that should be used as a destination MAC
345 * address on the Ethernet.
346 *
347 * If the destination IP address is in the local network (determined
348 * by logical ANDing of netmask and our IP address), the function
349 * checks the ARP cache to see if an entry for the destination IP
350 * address is found. If so, an Ethernet header is prepended and the
351 * function returns. If no ARP cache entry is found for the
352 * destination IP address, the packet in the uip_buf[] is replaced by
353 * an ARP request packet for the IP address. The IP packet is dropped
354 * and it is assumed that they higher level protocols (e.g., TCP)
355 * eventually will retransmit the dropped packet.
356 *
357 * If the destination IP address is not on the local network, the IP
358 * address of the default router is used instead.
359 *
360 * When the function returns, a packet is present in the uip_buf[]
361 * buffer, and the length of the packet is in the global variable
362 * uip_len.
363 */
364/*-----------------------------------------------------------------------------------*/
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000365void
366uip_arp_out(void)
367{
adamdunkels23664022003-08-05 13:51:50 +0000368 struct arp_entry *tabptr;
adamdunkels8c365012004-03-25 09:46:10 +0000369
370 /* Only work with IP packets. */
371 if(IPBUF->ethhdr.type != HTONS(UIP_ETHTYPE_IP)) {
372 return;
373 }
374
375
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000376 /* Find the destination IP address in the ARP table and construct
377 the Ethernet header. If the destination IP addres isn't on the
378 local network, we use the default router's IP address instead.
379
380 If not ARP table entry is found, we overwrite the original IP
381 packet with an ARP request for the IP address. */
382
383 /* Check if the destination address is on the local network. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000384 if((IPBUF->destipaddr[0] & uip_arp_netmask[0]) !=
385 (uip_hostaddr[0] & uip_arp_netmask[0]) ||
386 (IPBUF->destipaddr[1] & uip_arp_netmask[1]) !=
387 (uip_hostaddr[1] & uip_arp_netmask[1])) {
388 /* Destination address was not on the local network, so we need to
389 use the default router's IP address instead of the destination
390 address when determining the MAC address. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000391 ipaddr[0] = uip_arp_draddr[0];
392 ipaddr[1] = uip_arp_draddr[1];
393 } else {
394 /* Else, we use the destination IP address. */
395 ipaddr[0] = IPBUF->destipaddr[0];
396 ipaddr[1] = IPBUF->destipaddr[1];
397 }
398
399 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000400 tabptr = &arp_table[i];
401 if(ipaddr[0] == tabptr->ipaddr[0] &&
402 ipaddr[1] == tabptr->ipaddr[1])
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000403 break;
404 }
405
406 if(i == UIP_ARPTAB_SIZE) {
407 /* The destination address was not in our ARP table, so we
408 overwrite the IP packet with an ARP request. */
409
adamdunkels23664022003-08-05 13:51:50 +0000410 memset(BUF->ethhdr.dest.addr, 0xff, 6);
411 memset(BUF->dhwaddr.addr, 0x00, 6);
412 memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
413 memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000414
415 BUF->dipaddr[0] = ipaddr[0];
416 BUF->dipaddr[1] = ipaddr[1];
417 BUF->sipaddr[0] = uip_hostaddr[0];
418 BUF->sipaddr[1] = uip_hostaddr[1];
adamdunkels47ec7fa2003-03-28 12:11:17 +0000419 BUF->opcode = HTONS(ARP_REQUEST); /* ARP request. */
420 BUF->hwtype = HTONS(ARP_HWTYPE_ETH);
421 BUF->protocol = HTONS(UIP_ETHTYPE_IP);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000422 BUF->hwlen = 6;
423 BUF->protolen = 4;
adamdunkels47ec7fa2003-03-28 12:11:17 +0000424 BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000425
426 uip_appdata = &uip_buf[40 + UIP_LLH_LEN];
427
428 uip_len = sizeof(struct arp_hdr);
429 return;
430 }
431
432 /* Build an ethernet header. */
adamdunkels23664022003-08-05 13:51:50 +0000433 memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);
434 memcpy(IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
adamdunkels3ea09e62003-08-24 22:40:46 +0000435
adamdunkels47ec7fa2003-03-28 12:11:17 +0000436 IPBUF->ethhdr.type = HTONS(UIP_ETHTYPE_IP);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000437
438 uip_len += sizeof(struct uip_eth_hdr);
439}
440/*-----------------------------------------------------------------------------------*/
441
adamdunkels0170b082003-10-01 11:25:37 +0000442/** @} */
443/** @} */