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