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