blob: b19b16ff4c5be404c5e21f3fc5d9406be150f9e8 [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 *
adamdunkelsb380a3e2004-09-17 20:59:23 +000057 * $Id: uip_arp.c,v 1.13 2004/09/17 20:59:23 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
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000105static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
106static u16_t ipaddr[2];
107static u8_t i, c;
108
109static u8_t arptime;
110static u8_t tmpage;
111
112#define BUF ((struct arp_hdr *)&uip_buf[0])
113#define IPBUF ((struct ethip_hdr *)&uip_buf[0])
114/*-----------------------------------------------------------------------------------*/
adamdunkels846aabf2003-09-05 21:03:35 +0000115/**
116 * Initialize the ARP module.
117 *
118 */
119/*-----------------------------------------------------------------------------------*/
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000120void
121uip_arp_init(void)
122{
123 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000124 memset(arp_table[i].ipaddr, 0, 4);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000125 }
126}
127/*-----------------------------------------------------------------------------------*/
adamdunkels846aabf2003-09-05 21:03:35 +0000128/**
129 * Periodic ARP processing function.
130 *
131 * This function performs periodic timer processing in the ARP module
132 * and should be called at regular intervals. The recommended interval
133 * is 10 seconds between the calls.
134 *
135 */
136/*-----------------------------------------------------------------------------------*/
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000137void
138uip_arp_timer(void)
139{
adamdunkels23664022003-08-05 13:51:50 +0000140 struct arp_entry *tabptr;
141
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000142 ++arptime;
143 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000144 tabptr = &arp_table[i];
145 if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 &&
146 arptime - tabptr->time >= UIP_ARP_MAXAGE) {
147 memset(tabptr->ipaddr, 0, 4);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000148 }
149 }
150
151}
152/*-----------------------------------------------------------------------------------*/
153static void
154uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr)
155{
adamdunkels23664022003-08-05 13:51:50 +0000156 register struct arp_entry *tabptr;
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000157 /* Walk through the ARP mapping table and try to find an entry to
158 update. If none is found, the IP -> MAC address mapping is
159 inserted in the ARP table. */
160 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000161
162 tabptr = &arp_table[i];
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000163 /* Only check those entries that are actually in use. */
adamdunkels23664022003-08-05 13:51:50 +0000164 if(tabptr->ipaddr[0] != 0 &&
165 tabptr->ipaddr[1] != 0) {
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000166
167 /* Check if the source IP address of the incoming packet matches
168 the IP address in this ARP table entry. */
adamdunkels23664022003-08-05 13:51:50 +0000169 if(ipaddr[0] == tabptr->ipaddr[0] &&
170 ipaddr[1] == tabptr->ipaddr[1]) {
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000171
172 /* An old entry found, update this and return. */
adamdunkels23664022003-08-05 13:51:50 +0000173 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
174 tabptr->time = arptime;
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000175
176 return;
177 }
178 }
179 }
180
181 /* If we get here, no existing ARP table entry was found, so we
182 create one. */
183
184 /* First, we try to find an unused entry in the ARP table. */
185 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000186 tabptr = &arp_table[i];
187 if(tabptr->ipaddr[0] == 0 &&
188 tabptr->ipaddr[1] == 0) {
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000189 break;
190 }
191 }
192
193 /* If no unused entry is found, we try to find the oldest entry and
194 throw it away. */
195 if(i == UIP_ARPTAB_SIZE) {
196 tmpage = 0;
197 c = 0;
198 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000199 tabptr = &arp_table[i];
200 if(arptime - tabptr->time > tmpage) {
201 tmpage = arptime - tabptr->time;
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000202 c = i;
203 }
204 }
205 i = c;
adamdunkels5da99022004-02-24 10:13:55 +0000206 tabptr = &arp_table[i];
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000207 }
208
209 /* Now, i is the ARP table entry which we will fill with the new
210 information. */
adamdunkels23664022003-08-05 13:51:50 +0000211 memcpy(tabptr->ipaddr, ipaddr, 4);
212 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
adamdunkels23664022003-08-05 13:51:50 +0000213 tabptr->time = arptime;
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000214}
215/*-----------------------------------------------------------------------------------*/
adamdunkels846aabf2003-09-05 21:03:35 +0000216/**
217 * ARP processing for incoming IP packets
218 *
219 * This function should be called by the device driver when an IP
220 * packet has been received. The function will check if the address is
221 * in the ARP cache, and if so the ARP cache entry will be
222 * refreshed. If no ARP cache entry was found, a new one is created.
223 *
224 * This function expects an IP packet with a prepended Ethernet header
225 * in the uip_buf[] buffer, and the length of the packet in the global
226 * variable uip_len.
227 */
228/*-----------------------------------------------------------------------------------*/
adamdunkels8c365012004-03-25 09:46:10 +0000229#if 0
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000230void
231uip_arp_ipin(void)
232{
adamdunkelsb489e7a2003-10-14 11:12:50 +0000233 uip_len -= sizeof(struct uip_eth_hdr);
234
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000235 /* Only insert/update an entry if the source IP address of the
236 incoming IP packet comes from a host on the local network. */
adamdunkels2e649e12004-06-06 06:16:41 +0000237 if((IPBUF->srcipaddr[0] & uip_netmask[0]) !=
238 (uip_hostaddr[0] & uip_netmask[0])) {
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000239 return;
240 }
adamdunkels2e649e12004-06-06 06:16:41 +0000241 if((IPBUF->srcipaddr[1] & uip_netmask[1]) !=
242 (uip_hostaddr[1] & uip_netmask[1])) {
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000243 return;
244 }
245 uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src));
246
247 return;
248}
adamdunkels8c365012004-03-25 09:46:10 +0000249#endif /* 0 */
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000250/*-----------------------------------------------------------------------------------*/
adamdunkels846aabf2003-09-05 21:03:35 +0000251/**
252 * ARP processing for incoming ARP packets.
253 *
254 * This function should be called by the device driver when an ARP
255 * packet has been received. The function will act differently
256 * depending on the ARP packet type: if it is a reply for a request
257 * that we previously sent out, the ARP cache will be filled in with
258 * the values from the ARP reply. If the incoming ARP packet is an ARP
259 * request for our IP address, an ARP reply packet is created and put
260 * into the uip_buf[] buffer.
261 *
262 * When the function returns, the value of the global variable uip_len
263 * indicates whether the device driver should send out a packet or
264 * not. If uip_len is zero, no packet should be sent. If uip_len is
265 * non-zero, it contains the length of the outbound packet that is
266 * present in the uip_buf[] buffer.
267 *
268 * This function expects an ARP packet with a prepended Ethernet
269 * header in the uip_buf[] buffer, and the length of the packet in the
270 * global variable uip_len.
271 */
272/*-----------------------------------------------------------------------------------*/
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000273void
274uip_arp_arpin(void)
275{
adamdunkels2e649e12004-06-06 06:16:41 +0000276
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000277 if(uip_len < sizeof(struct arp_hdr)) {
278 uip_len = 0;
279 return;
280 }
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000281 uip_len = 0;
282
283 switch(BUF->opcode) {
adamdunkels47ec7fa2003-03-28 12:11:17 +0000284 case HTONS(ARP_REQUEST):
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000285 /* ARP request. If it asked for our address, we send out a
adamdunkels2e649e12004-06-06 06:16:41 +0000286 reply. */
287 /* if(BUF->dipaddr[0] == uip_hostaddr[0] &&
288 BUF->dipaddr[1] == uip_hostaddr[1]) {*/
289 if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) {
adamdunkels8c365012004-03-25 09:46:10 +0000290 /* First, we register the one who made the request in our ARP
291 table, since it is likely that we will do more communication
292 with this host in the future. */
293 uip_arp_update(BUF->sipaddr, &BUF->shwaddr);
294
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000295 /* The reply opcode is 2. */
adamdunkels47ec7fa2003-03-28 12:11:17 +0000296 BUF->opcode = HTONS(2);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000297
adamdunkels23664022003-08-05 13:51:50 +0000298 memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6);
299 memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
300 memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
301 memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);
adamdunkels3ea09e62003-08-24 22:40:46 +0000302
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000303 BUF->dipaddr[0] = BUF->sipaddr[0];
304 BUF->dipaddr[1] = BUF->sipaddr[1];
305 BUF->sipaddr[0] = uip_hostaddr[0];
306 BUF->sipaddr[1] = uip_hostaddr[1];
307
adamdunkels47ec7fa2003-03-28 12:11:17 +0000308 BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000309 uip_len = sizeof(struct arp_hdr);
310 }
311 break;
adamdunkels47ec7fa2003-03-28 12:11:17 +0000312 case HTONS(ARP_REPLY):
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000313 /* ARP reply. We insert or update the ARP table if it was meant
314 for us. */
adamdunkels2e649e12004-06-06 06:16:41 +0000315 /* if(BUF->dipaddr[0] == uip_hostaddr[0] &&
316 BUF->dipaddr[1] == uip_hostaddr[1]) {*/
317 if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) {
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000318 uip_arp_update(BUF->sipaddr, &BUF->shwaddr);
319 }
320 break;
321 }
322
323 return;
324}
325/*-----------------------------------------------------------------------------------*/
adamdunkels846aabf2003-09-05 21:03:35 +0000326/**
327 * Prepend Ethernet header to an outbound IP packet and see if we need
328 * to send out an ARP request.
329 *
330 * This function should be called before sending out an IP packet. The
331 * function checks the destination IP address of the IP packet to see
332 * what Ethernet MAC address that should be used as a destination MAC
333 * address on the Ethernet.
334 *
335 * If the destination IP address is in the local network (determined
336 * by logical ANDing of netmask and our IP address), the function
337 * checks the ARP cache to see if an entry for the destination IP
338 * address is found. If so, an Ethernet header is prepended and the
339 * function returns. If no ARP cache entry is found for the
340 * destination IP address, the packet in the uip_buf[] is replaced by
341 * an ARP request packet for the IP address. The IP packet is dropped
342 * and it is assumed that they higher level protocols (e.g., TCP)
343 * eventually will retransmit the dropped packet.
344 *
345 * If the destination IP address is not on the local network, the IP
346 * address of the default router is used instead.
347 *
348 * When the function returns, a packet is present in the uip_buf[]
349 * buffer, and the length of the packet is in the global variable
350 * uip_len.
351 */
352/*-----------------------------------------------------------------------------------*/
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000353void
354uip_arp_out(void)
355{
adamdunkels23664022003-08-05 13:51:50 +0000356 struct arp_entry *tabptr;
adamdunkels8c365012004-03-25 09:46:10 +0000357
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000358 /* Find the destination IP address in the ARP table and construct
359 the Ethernet header. If the destination IP addres isn't on the
360 local network, we use the default router's IP address instead.
361
362 If not ARP table entry is found, we overwrite the original IP
363 packet with an ARP request for the IP address. */
364
365 /* Check if the destination address is on the local network. */
adamdunkels2e649e12004-06-06 06:16:41 +0000366 /* if((IPBUF->destipaddr[0] & uip_netmask[0]) !=
367 (uip_hostaddr[0] & uip_netmask[0]) ||
368 (IPBUF->destipaddr[1] & uip_netmask[1]) !=
369 (uip_hostaddr[1] & uip_netmask[1])) {*/
370 if(!uip_ipaddr_maskcmp(IPBUF->destipaddr, uip_hostaddr, uip_netmask)) {
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000371 /* Destination address was not on the local network, so we need to
372 use the default router's IP address instead of the destination
373 address when determining the MAC address. */
adamdunkels2e649e12004-06-06 06:16:41 +0000374 /* ipaddr[0] = uip_draddr[0];
375 ipaddr[1] = uip_draddr[1];*/
376 uip_ipaddr_copy(ipaddr, uip_draddr);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000377 } else {
378 /* Else, we use the destination IP address. */
adamdunkels2e649e12004-06-06 06:16:41 +0000379 /*ipaddr[0] = IPBUF->destipaddr[0];
380 ipaddr[1] = IPBUF->destipaddr[1];*/
381 uip_ipaddr_copy(ipaddr, IPBUF->destipaddr);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000382 }
383
384 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
adamdunkels23664022003-08-05 13:51:50 +0000385 tabptr = &arp_table[i];
adamdunkels2e649e12004-06-06 06:16:41 +0000386 /* if(ipaddr[0] == tabptr->ipaddr[0] &&
387 ipaddr[1] == tabptr->ipaddr[1])*/
388 if(uip_ipaddr_cmp(ipaddr, tabptr->ipaddr)) {
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000389 break;
adamdunkels2e649e12004-06-06 06:16:41 +0000390 }
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000391 }
392
393 if(i == UIP_ARPTAB_SIZE) {
394 /* The destination address was not in our ARP table, so we
395 overwrite the IP packet with an ARP request. */
396
adamdunkels23664022003-08-05 13:51:50 +0000397 memset(BUF->ethhdr.dest.addr, 0xff, 6);
398 memset(BUF->dhwaddr.addr, 0x00, 6);
399 memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
400 memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000401
adamdunkels2e649e12004-06-06 06:16:41 +0000402 /* BUF->dipaddr[0] = ipaddr[0];
403 BUF->dipaddr[1] = ipaddr[1];*/
404 uip_ipaddr_copy(BUF->dipaddr, ipaddr);
405 /* BUF->sipaddr[0] = uip_hostaddr[0];
406 BUF->sipaddr[1] = uip_hostaddr[1];*/
407 uip_ipaddr_copy(BUF->sipaddr, uip_hostaddr);
adamdunkels47ec7fa2003-03-28 12:11:17 +0000408 BUF->opcode = HTONS(ARP_REQUEST); /* ARP request. */
409 BUF->hwtype = HTONS(ARP_HWTYPE_ETH);
410 BUF->protocol = HTONS(UIP_ETHTYPE_IP);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000411 BUF->hwlen = 6;
412 BUF->protolen = 4;
adamdunkels47ec7fa2003-03-28 12:11:17 +0000413 BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000414
415 uip_appdata = &uip_buf[40 + UIP_LLH_LEN];
416
417 uip_len = sizeof(struct arp_hdr);
418 return;
419 }
420
421 /* Build an ethernet header. */
adamdunkels23664022003-08-05 13:51:50 +0000422 memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);
423 memcpy(IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
adamdunkels3ea09e62003-08-24 22:40:46 +0000424
adamdunkels47ec7fa2003-03-28 12:11:17 +0000425 IPBUF->ethhdr.type = HTONS(UIP_ETHTYPE_IP);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000426
427 uip_len += sizeof(struct uip_eth_hdr);
428}
429/*-----------------------------------------------------------------------------------*/
430
adamdunkels0170b082003-10-01 11:25:37 +0000431/** @} */
432/** @} */