Contiki 3.x
uip_arp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2003, Adam Dunkels.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  * products derived from this software without specific prior
15  * written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the uIP TCP/IP stack.
30  *
31  *
32  */
33 
34 /**
35  * \file
36  * Implementation of the ARP Address Resolution Protocol.
37  * \author Adam Dunkels <adam@dunkels.com>
38  *
39  */
40 
41 /**
42  * \addtogroup uip
43  * @{
44  */
45 
46 /**
47  * \defgroup uiparp uIP Address Resolution Protocol
48  * @{
49  *
50  * The Address Resolution Protocol ARP is used for mapping between IP
51  * addresses and link level addresses such as the Ethernet MAC
52  * addresses. ARP uses broadcast queries to ask for the link level
53  * address of a known IP address and the host which is configured with
54  * the IP address for which the query was meant, will respond with its
55  * link level address.
56  *
57  * \note This ARP implementation only supports Ethernet.
58  */
59 
60 #include "net/ipv4/uip_arp.h"
61 
62 #include <string.h>
63 
64 struct arp_hdr {
65  struct uip_eth_hdr ethhdr;
66  uint16_t hwtype;
67  uint16_t protocol;
68  uint8_t hwlen;
69  uint8_t protolen;
70  uint16_t opcode;
71  struct uip_eth_addr shwaddr;
72  uip_ipaddr_t sipaddr;
73  struct uip_eth_addr dhwaddr;
74  uip_ipaddr_t dipaddr;
75 };
76 
77 struct ethip_hdr {
78  struct uip_eth_hdr ethhdr;
79  /* IP header. */
80  uint8_t vhl,
81  tos,
82  len[2],
83  ipid[2],
84  ipoffset[2],
85  ttl,
86  proto;
87  uint16_t ipchksum;
88  uip_ipaddr_t srcipaddr, destipaddr;
89 };
90 
91 #define ARP_REQUEST 1
92 #define ARP_REPLY 2
93 
94 #define ARP_HWTYPE_ETH 1
95 
96 struct arp_entry {
97  uip_ipaddr_t ipaddr;
98  struct uip_eth_addr ethaddr;
99  uint8_t time;
100 };
101 
102 static const struct uip_eth_addr broadcast_ethaddr =
103  {{0xff,0xff,0xff,0xff,0xff,0xff}};
104 
105 static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
106 static uip_ipaddr_t ipaddr;
107 static uint8_t i, c;
108 
109 static uint8_t arptime;
110 static uint8_t tmpage;
111 
112 #define BUF ((struct arp_hdr *)&uip_buf[0])
113 #define IPBUF ((struct ethip_hdr *)&uip_buf[0])
114 
115 #define DEBUG 0
116 #if DEBUG
117 #include <stdio.h>
118 #define PRINTF(...) printf(__VA_ARGS__)
119 #else
120 #define PRINTF(...)
121 #endif
122 
123 /*-----------------------------------------------------------------------------------*/
124 /**
125  * Initialize the ARP module.
126  *
127  */
128 /*-----------------------------------------------------------------------------------*/
129 void
131 {
132  for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
133  memset(&arp_table[i].ipaddr, 0, 4);
134  }
135 }
136 /*-----------------------------------------------------------------------------------*/
137 /**
138  * Periodic ARP processing function.
139  *
140  * This function performs periodic timer processing in the ARP module
141  * and should be called at regular intervals. The recommended interval
142  * is 10 seconds between the calls.
143  *
144  */
145 /*-----------------------------------------------------------------------------------*/
146 void
148 {
149  struct arp_entry *tabptr;
150 
151  ++arptime;
152  for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
153  tabptr = &arp_table[i];
154  if(uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) &&
155  arptime - tabptr->time >= UIP_ARP_MAXAGE) {
156  memset(&tabptr->ipaddr, 0, 4);
157  }
158  }
159 
160 }
161 
162 /*-----------------------------------------------------------------------------------*/
163 static void
164 uip_arp_update(uip_ipaddr_t *ipaddr, struct uip_eth_addr *ethaddr)
165 {
166  register struct arp_entry *tabptr = arp_table;
167 
168  /* 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) {
172  tabptr = &arp_table[i];
173 
174  /* Only check those entries that are actually in use. */
175  if(!uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr)) {
176 
177  /* Check if the source IP address of the incoming packet matches
178  the IP address in this ARP table entry. */
179  if(uip_ipaddr_cmp(ipaddr, &tabptr->ipaddr)) {
180 
181  /* An old entry found, update this and return. */
182  memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
183  tabptr->time = arptime;
184 
185  return;
186  }
187  }
188  tabptr++;
189  }
190 
191  /* If we get here, no existing ARP table entry was found, so we
192  create one. */
193 
194  /* First, we try to find an unused entry in the ARP table. */
195  for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
196  tabptr = &arp_table[i];
197  if(uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr)) {
198  break;
199  }
200  }
201 
202  /* If no unused entry is found, we try to find the oldest entry and
203  throw it away. */
204  if(i == UIP_ARPTAB_SIZE) {
205  tmpage = 0;
206  c = 0;
207  for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
208  tabptr = &arp_table[i];
209  if(arptime - tabptr->time > tmpage) {
210  tmpage = arptime - tabptr->time;
211  c = i;
212  }
213  }
214  i = c;
215  tabptr = &arp_table[i];
216  }
217 
218  /* Now, i is the ARP table entry which we will fill with the new
219  information. */
220  uip_ipaddr_copy(&tabptr->ipaddr, ipaddr);
221  memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
222  tabptr->time = arptime;
223 }
224 /*-----------------------------------------------------------------------------------*/
225 /**
226  * ARP processing for incoming IP packets
227  *
228  * This function should be called by the device driver when an IP
229  * packet has been received. The function will check if the address is
230  * in the ARP cache, and if so the ARP cache entry will be
231  * refreshed. If no ARP cache entry was found, a new one is created.
232  *
233  * This function expects an IP packet with a prepended Ethernet header
234  * in the uip_buf[] buffer, and the length of the packet in the global
235  * variable uip_len.
236  */
237 /*-----------------------------------------------------------------------------------*/
238 #if 0
239 void
240 uip_arp_ipin(void)
241 {
242  uip_len -= sizeof(struct uip_eth_hdr);
243 
244  /* Only insert/update an entry if the source IP address of the
245  incoming IP packet comes from a host on the local network. */
246  if((IPBUF->srcipaddr[0] & uip_netmask[0]) !=
247  (uip_hostaddr[0] & uip_netmask[0])) {
248  return;
249  }
250  if((IPBUF->srcipaddr[1] & uip_netmask[1]) !=
251  (uip_hostaddr[1] & uip_netmask[1])) {
252  return;
253  }
254  uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src));
255 
256  return;
257 }
258 #endif /* 0 */
259 /*-----------------------------------------------------------------------------------*/
260 /**
261  * ARP processing for incoming ARP packets.
262  *
263  * This function should be called by the device driver when an ARP
264  * packet has been received. The function will act differently
265  * depending on the ARP packet type: if it is a reply for a request
266  * that we previously sent out, the ARP cache will be filled in with
267  * the values from the ARP reply. If the incoming ARP packet is an ARP
268  * request for our IP address, an ARP reply packet is created and put
269  * into the uip_buf[] buffer.
270  *
271  * When the function returns, the value of the global variable uip_len
272  * indicates whether the device driver should send out a packet or
273  * not. If uip_len is zero, no packet should be sent. If uip_len is
274  * non-zero, it contains the length of the outbound packet that is
275  * present in the uip_buf[] buffer.
276  *
277  * This function expects an ARP packet with a prepended Ethernet
278  * header in the uip_buf[] buffer, and the length of the packet in the
279  * global variable uip_len.
280  */
281 /*-----------------------------------------------------------------------------------*/
282 void
284 {
285 
286  if(uip_len < sizeof(struct arp_hdr)) {
287  uip_len = 0;
288  return;
289  }
290  uip_len = 0;
291 
292  switch(BUF->opcode) {
293  case UIP_HTONS(ARP_REQUEST):
294  /* ARP request. If it asked for our address, we send out a
295  reply. */
296  /* if(BUF->dipaddr[0] == uip_hostaddr[0] &&
297  BUF->dipaddr[1] == uip_hostaddr[1]) {*/
298  PRINTF("uip_arp_arpin: request for %d.%d.%d.%d (we are %d.%d.%d.%d)\n",
299  BUF->dipaddr.u8[0], BUF->dipaddr.u8[1],
300  BUF->dipaddr.u8[2], BUF->dipaddr.u8[3],
301  uip_hostaddr.u8[0], uip_hostaddr.u8[1],
302  uip_hostaddr.u8[2], uip_hostaddr.u8[3]);
303  if(uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr)) {
304  /* First, we register the one who made the request in our ARP
305  table, since it is likely that we will do more communication
306  with this host in the future. */
307  uip_arp_update(&BUF->sipaddr, &BUF->shwaddr);
308 
309  BUF->opcode = UIP_HTONS(ARP_REPLY);
310 
311  memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6);
312  memcpy(BUF->shwaddr.addr, uip_lladdr.addr, 6);
313  memcpy(BUF->ethhdr.src.addr, uip_lladdr.addr, 6);
314  memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);
315 
316  uip_ipaddr_copy(&BUF->dipaddr, &BUF->sipaddr);
317  uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr);
318 
319  BUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP);
320  uip_len = sizeof(struct arp_hdr);
321  }
322  break;
323  case UIP_HTONS(ARP_REPLY):
324  /* ARP reply. We insert or update the ARP table if it was meant
325  for us. */
326  if(uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr)) {
327  uip_arp_update(&BUF->sipaddr, &BUF->shwaddr);
328  }
329  break;
330  }
331 
332  return;
333 }
334 /*-----------------------------------------------------------------------------------*/
335 /**
336  * Prepend Ethernet header to an outbound IP packet and see if we need
337  * to send out an ARP request.
338  *
339  * This function should be called before sending out an IP packet. The
340  * function checks the destination IP address of the IP packet to see
341  * what Ethernet MAC address that should be used as a destination MAC
342  * address on the Ethernet.
343  *
344  * If the destination IP address is in the local network (determined
345  * by logical ANDing of netmask and our IP address), the function
346  * checks the ARP cache to see if an entry for the destination IP
347  * address is found. If so, an Ethernet header is prepended and the
348  * function returns. If no ARP cache entry is found for the
349  * destination IP address, the packet in the uip_buf[] is replaced by
350  * an ARP request packet for the IP address. The IP packet is dropped
351  * and it is assumed that they higher level protocols (e.g., TCP)
352  * eventually will retransmit the dropped packet.
353  *
354  * If the destination IP address is not on the local network, the IP
355  * address of the default router is used instead.
356  *
357  * When the function returns, a packet is present in the uip_buf[]
358  * buffer, and the length of the packet is in the global variable
359  * uip_len.
360  */
361 /*-----------------------------------------------------------------------------------*/
362 void
364 {
365  struct arp_entry *tabptr = arp_table;
366 
367  /* Find the destination IP address in the ARP table and construct
368  the Ethernet header. If the destination IP addres isn't on the
369  local network, we use the default router's IP address instead.
370 
371  If not ARP table entry is found, we overwrite the original IP
372  packet with an ARP request for the IP address. */
373 
374  /* First check if destination is a local broadcast. */
375  if(uip_ipaddr_cmp(&IPBUF->destipaddr, &uip_broadcast_addr)) {
376  memcpy(IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6);
377  } else if(IPBUF->destipaddr.u8[0] == 224) {
378  /* Multicast. */
379  IPBUF->ethhdr.dest.addr[0] = 0x01;
380  IPBUF->ethhdr.dest.addr[1] = 0x00;
381  IPBUF->ethhdr.dest.addr[2] = 0x5e;
382  IPBUF->ethhdr.dest.addr[3] = IPBUF->destipaddr.u8[1];
383  IPBUF->ethhdr.dest.addr[4] = IPBUF->destipaddr.u8[2];
384  IPBUF->ethhdr.dest.addr[5] = IPBUF->destipaddr.u8[3];
385  } else {
386  /* Check if the destination address is on the local network. */
387  if(!uip_ipaddr_maskcmp(&IPBUF->destipaddr, &uip_hostaddr, &uip_netmask)) {
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. */
391  uip_ipaddr_copy(&ipaddr, &uip_draddr);
392  } else {
393  /* Else, we use the destination IP address. */
394  uip_ipaddr_copy(&ipaddr, &IPBUF->destipaddr);
395  }
396  for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
397  if(uip_ipaddr_cmp(&ipaddr, &tabptr->ipaddr)) {
398  break;
399  }
400  tabptr++;
401  }
402 
403  if(i == UIP_ARPTAB_SIZE) {
404  /* The destination address was not in our ARP table, so we
405  overwrite the IP packet with an ARP request. */
406 
407  memset(BUF->ethhdr.dest.addr, 0xff, 6);
408  memset(BUF->dhwaddr.addr, 0x00, 6);
409  memcpy(BUF->ethhdr.src.addr, uip_lladdr.addr, 6);
410  memcpy(BUF->shwaddr.addr, uip_lladdr.addr, 6);
411 
412  uip_ipaddr_copy(&BUF->dipaddr, &ipaddr);
413  uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr);
414  BUF->opcode = UIP_HTONS(ARP_REQUEST); /* ARP request. */
415  BUF->hwtype = UIP_HTONS(ARP_HWTYPE_ETH);
416  BUF->protocol = UIP_HTONS(UIP_ETHTYPE_IP);
417  BUF->hwlen = 6;
418  BUF->protolen = 4;
419  BUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP);
420 
421  uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN];
422 
423  uip_len = sizeof(struct arp_hdr);
424  return;
425  }
426 
427  /* Build an ethernet header. */
428  memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);
429  }
430  memcpy(IPBUF->ethhdr.src.addr, uip_lladdr.addr, 6);
431 
432  IPBUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_IP);
433 
434  uip_len += sizeof(struct uip_eth_hdr);
435 }
436 /*-----------------------------------------------------------------------------------*/
437 
438 /** @} */
439 /** @} */
440 
void uip_arp_out(void)
Prepend Ethernet header to an outbound IP packet and see if we need to send out an ARP request...
Definition: uip_arp.c:363
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
#define uip_ipaddr_maskcmp(addr1, addr2, mask)
Compare two IP addresses with netmasks.
Definition: uip.h:1090
#define UIP_ARPTAB_SIZE
The size of the ARP table.
Definition: uipopt.h:532
void uip_arp_timer(void)
Periodic ARP processing function.
Definition: uip_arp.c:147
void uip_arp_init(void)
Initialize the ARP module.
Definition: uip_arp.c:130
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1238
#define UIP_ARP_MAXAGE
The maximum age of ARP table entries measured in 10ths of seconds.
Definition: uipopt.h:541
The Ethernet header.
Definition: uip_arp.h:60
#define UIP_LLH_LEN
The link level header length.
Definition: uipopt.h:160
Macros and definitions for the ARP module.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
802.3 address
Definition: uip.h:135
void uip_arp_arpin(void)
ARP processing for incoming IP packets.
Definition: uip_arp.c:283
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74