uIP 1.0 based webserver for LPC1114 + ENC28J60

Dependencies:   mbed TMP102

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers uip_arp.c Source File

uip_arp.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup uip
00003  * @{
00004  */
00005 
00006 /**
00007  * \defgroup uiparp uIP Address Resolution Protocol
00008  * @{
00009  *
00010  * The Address Resolution Protocol ARP is used for mapping between IP
00011  * addresses and link level addresses such as the Ethernet MAC
00012  * addresses. ARP uses broadcast queries to ask for the link level
00013  * address of a known IP address and the host which is configured with
00014  * the IP address for which the query was meant, will respond with its
00015  * link level address.
00016  *
00017  * \note This ARP implementation only supports Ethernet.
00018  */
00019  
00020 /**
00021  * \file
00022  * Implementation of the ARP Address Resolution Protocol.
00023  * \author Adam Dunkels <adam@dunkels.com>
00024  *
00025  */
00026 
00027 /*
00028  * Copyright (c) 2001-2003, Adam Dunkels.
00029  * All rights reserved.
00030  *
00031  * Redistribution and use in source and binary forms, with or without
00032  * modification, are permitted provided that the following conditions
00033  * are met:
00034  * 1. Redistributions of source code must retain the above copyright
00035  *    notice, this list of conditions and the following disclaimer.
00036  * 2. Redistributions in binary form must reproduce the above copyright
00037  *    notice, this list of conditions and the following disclaimer in the
00038  *    documentation and/or other materials provided with the distribution.
00039  * 3. The name of the author may not be used to endorse or promote
00040  *    products derived from this software without specific prior
00041  *    written permission.
00042  *
00043  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00044  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00045  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00046  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00047  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00048  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00049  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00050  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00051  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00052  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00053  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00054  *
00055  * This file is part of the uIP TCP/IP stack.
00056  *
00057  *
00058  */
00059 
00060 
00061 #include "uip_arp.h"
00062 
00063 #include <string.h>
00064 
00065 struct arp_hdr {
00066   struct uip_eth_hdr ethhdr;
00067   uint16_t hwtype;
00068   uint16_t protocol;
00069   uint8_t hwlen;
00070   uint8_t protolen;
00071   uint16_t opcode;
00072   struct uip_eth_addr shwaddr;
00073   uip_ipaddr_t sipaddr;
00074   struct uip_eth_addr dhwaddr;
00075   uip_ipaddr_t dipaddr;
00076 };
00077 
00078 struct ethip_hdr {
00079   struct uip_eth_hdr ethhdr;
00080   /* IP header. */
00081   uint8_t vhl,
00082     tos,
00083     len[2],
00084     ipid[2],
00085     ipoffset[2],
00086     ttl,
00087     proto;
00088   uint16_t ipchksum;
00089   uip_ipaddr_t srcipaddr, destipaddr;
00090 };
00091 
00092 #define ARP_REQUEST 1
00093 #define ARP_REPLY   2
00094 
00095 #define ARP_HWTYPE_ETH 1
00096 
00097 struct arp_entry {
00098   uip_ipaddr_t ipaddr;
00099   struct uip_eth_addr ethaddr;
00100   uint8_t time;
00101 };
00102 
00103 static const struct uip_eth_addr broadcast_ethaddr =
00104   {{0xff,0xff,0xff,0xff,0xff,0xff}};
00105 static const uint16_t broadcast_ipaddr[2] = {0xffff,0xffff};
00106 
00107 static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
00108 static uip_ipaddr_t ipaddr;
00109 static uint8_t i, c;
00110 
00111 static uint8_t arptime;
00112 static uint8_t tmpage;
00113 
00114 #define BUF   ((struct arp_hdr *)&uip_buf[0])
00115 #define IPBUF ((struct ethip_hdr *)&uip_buf[0])
00116 
00117 #define DEBUG 0
00118 #if DEBUG
00119 #include <stdio.h>
00120 #define PRINTF(...) printf(__VA_ARGS__)
00121 #else
00122 #define PRINTF(...)
00123 #endif
00124 
00125 /*-----------------------------------------------------------------------------------*/
00126 /**
00127  * Initialize the ARP module.
00128  *
00129  */
00130 /*-----------------------------------------------------------------------------------*/
00131 void
00132 uip_arp_init(void)
00133 {
00134   for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
00135     memset(&arp_table[i].ipaddr, 0, 4);
00136   }
00137 }
00138 /*-----------------------------------------------------------------------------------*/
00139 /**
00140  * Periodic ARP processing function.
00141  *
00142  * This function performs periodic timer processing in the ARP module
00143  * and should be called at regular intervals. The recommended interval
00144  * is 10 seconds between the calls.
00145  *
00146  */
00147 /*-----------------------------------------------------------------------------------*/
00148 void
00149 uip_arp_timer(void)
00150 {
00151   struct arp_entry *tabptr;
00152   
00153   ++arptime;
00154   for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
00155     tabptr = &arp_table[i];
00156     if(uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) &&
00157        arptime - tabptr->time >= UIP_ARP_MAXAGE) {
00158       memset(&tabptr->ipaddr, 0, 4);
00159     }
00160   }
00161 
00162 }
00163 
00164 /*-----------------------------------------------------------------------------------*/
00165 static void
00166 uip_arp_update(uip_ipaddr_t *ipaddr, struct uip_eth_addr *ethaddr)
00167 {
00168   register struct arp_entry *tabptr = arp_table;
00169 
00170   /* Walk through the ARP mapping table and try to find an entry to
00171      update. If none is found, the IP -> MAC address mapping is
00172      inserted in the ARP table. */
00173   for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
00174     tabptr = &arp_table[i];
00175 
00176     /* Only check those entries that are actually in use. */
00177     if(!uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr)) {
00178 
00179       /* Check if the source IP address of the incoming packet matches
00180          the IP address in this ARP table entry. */
00181       if(uip_ipaddr_cmp(ipaddr, &tabptr->ipaddr)) {
00182      
00183     /* An old entry found, update this and return. */
00184     memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
00185     tabptr->time = arptime;
00186 
00187     return;
00188       }
00189     }
00190     tabptr++;
00191   }
00192 
00193   /* If we get here, no existing ARP table entry was found, so we
00194      create one. */
00195 
00196   /* First, we try to find an unused entry in the ARP table. */
00197   for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
00198     tabptr = &arp_table[i];
00199     if(uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr)) {
00200       break;
00201     }
00202   }
00203 
00204   /* If no unused entry is found, we try to find the oldest entry and
00205      throw it away. */
00206   if(i == UIP_ARPTAB_SIZE) {
00207     tmpage = 0;
00208     c = 0;
00209     for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
00210       tabptr = &arp_table[i];
00211       if(arptime - tabptr->time > tmpage) {
00212     tmpage = arptime - tabptr->time;
00213     c = i;
00214       }
00215     }
00216     i = c;
00217     tabptr = &arp_table[i];
00218   }
00219 
00220   /* Now, i is the ARP table entry which we will fill with the new
00221      information. */
00222   uip_ipaddr_copy(&tabptr->ipaddr, ipaddr);
00223   memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
00224   tabptr->time = arptime;
00225 }
00226 /*-----------------------------------------------------------------------------------*/
00227 /**
00228  * ARP processing for incoming IP packets
00229  *
00230  * This function should be called by the device driver when an IP
00231  * packet has been received. The function will check if the address is
00232  * in the ARP cache, and if so the ARP cache entry will be
00233  * refreshed. If no ARP cache entry was found, a new one is created.
00234  *
00235  * This function expects an IP packet with a prepended Ethernet header
00236  * in the uip_buf[] buffer, and the length of the packet in the global
00237  * variable uip_len.
00238  */
00239 /*-----------------------------------------------------------------------------------*/
00240 #if 0
00241 void
00242 uip_arp_ipin(void)
00243 {
00244   uip_len -= sizeof(struct uip_eth_hdr);
00245     
00246   /* Only insert/update an entry if the source IP address of the
00247      incoming IP packet comes from a host on the local network. */
00248   if((IPBUF->srcipaddr[0] & uip_netmask[0]) !=
00249      (uip_hostaddr[0] & uip_netmask[0])) {
00250     return;
00251   }
00252   if((IPBUF->srcipaddr[1] & uip_netmask[1]) !=
00253      (uip_hostaddr[1] & uip_netmask[1])) {
00254     return;
00255   }
00256   uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src));
00257   
00258   return;
00259 }
00260 #endif /* 0 */
00261 /*-----------------------------------------------------------------------------------*/
00262 /**
00263  * ARP processing for incoming ARP packets.
00264  *
00265  * This function should be called by the device driver when an ARP
00266  * packet has been received. The function will act differently
00267  * depending on the ARP packet type: if it is a reply for a request
00268  * that we previously sent out, the ARP cache will be filled in with
00269  * the values from the ARP reply. If the incoming ARP packet is an ARP
00270  * request for our IP address, an ARP reply packet is created and put
00271  * into the uip_buf[] buffer.
00272  *
00273  * When the function returns, the value of the global variable uip_len
00274  * indicates whether the device driver should send out a packet or
00275  * not. If uip_len is zero, no packet should be sent. If uip_len is
00276  * non-zero, it contains the length of the outbound packet that is
00277  * present in the uip_buf[] buffer.
00278  *
00279  * This function expects an ARP packet with a prepended Ethernet
00280  * header in the uip_buf[] buffer, and the length of the packet in the
00281  * global variable uip_len.
00282  */
00283 /*-----------------------------------------------------------------------------------*/
00284 void
00285 uip_arp_arpin(void)
00286 {
00287   
00288   if(uip_len < sizeof(struct arp_hdr)) {
00289     uip_len = 0;
00290     return;
00291   }
00292   uip_len = 0;
00293   
00294   switch(BUF->opcode) {
00295   case UIP_HTONS(ARP_REQUEST):
00296     /* ARP request. If it asked for our address, we send out a
00297        reply. */
00298     /*    if(BUF->dipaddr[0] == uip_hostaddr[0] &&
00299       BUF->dipaddr[1] == uip_hostaddr[1]) {*/
00300     PRINTF("uip_arp_arpin: request for %d.%d.%d.%d (we are %d.%d.%d.%d)\n",
00301        BUF->dipaddr.u8[0], BUF->dipaddr.u8[1],
00302        BUF->dipaddr.u8[2], BUF->dipaddr.u8[3],
00303        uip_hostaddr.u8[0], uip_hostaddr.u8[1],
00304        uip_hostaddr.u8[2], uip_hostaddr.u8[3]);
00305     if(uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr)) {
00306       /* First, we register the one who made the request in our ARP
00307      table, since it is likely that we will do more communication
00308      with this host in the future. */
00309       uip_arp_update(&BUF->sipaddr, &BUF->shwaddr);
00310       
00311       BUF->opcode = UIP_HTONS(ARP_REPLY);
00312 
00313       memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6);
00314       memcpy(BUF->shwaddr.addr, uip_lladdr.addr, 6);
00315       memcpy(BUF->ethhdr.src.addr, uip_lladdr.addr, 6);
00316       memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);
00317       
00318       uip_ipaddr_copy(&BUF->dipaddr, &BUF->sipaddr);
00319       uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr);
00320 
00321       BUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP);
00322       uip_len = sizeof(struct arp_hdr);
00323     }
00324     break;
00325   case UIP_HTONS(ARP_REPLY):
00326     /* ARP reply. We insert or update the ARP table if it was meant
00327        for us. */
00328     if(uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr)) {
00329       uip_arp_update(&BUF->sipaddr, &BUF->shwaddr);
00330     }
00331     break;
00332   }
00333 
00334   return;
00335 }
00336 /*-----------------------------------------------------------------------------------*/
00337 /**
00338  * Prepend Ethernet header to an outbound IP packet and see if we need
00339  * to send out an ARP request.
00340  *
00341  * This function should be called before sending out an IP packet. The
00342  * function checks the destination IP address of the IP packet to see
00343  * what Ethernet MAC address that should be used as a destination MAC
00344  * address on the Ethernet.
00345  *
00346  * If the destination IP address is in the local network (determined
00347  * by logical ANDing of netmask and our IP address), the function
00348  * checks the ARP cache to see if an entry for the destination IP
00349  * address is found. If so, an Ethernet header is prepended and the
00350  * function returns. If no ARP cache entry is found for the
00351  * destination IP address, the packet in the uip_buf[] is replaced by
00352  * an ARP request packet for the IP address. The IP packet is dropped
00353  * and it is assumed that they higher level protocols (e.g., TCP)
00354  * eventually will retransmit the dropped packet.
00355  *
00356  * If the destination IP address is not on the local network, the IP
00357  * address of the default router is used instead.
00358  *
00359  * When the function returns, a packet is present in the uip_buf[]
00360  * buffer, and the length of the packet is in the global variable
00361  * uip_len.
00362  */
00363 /*-----------------------------------------------------------------------------------*/
00364 void
00365 uip_arp_out(void)
00366 {
00367   struct arp_entry *tabptr = arp_table;
00368   
00369   /* Find the destination IP address in the ARP table and construct
00370      the Ethernet header. If the destination IP addres isn't on the
00371      local network, we use the default router's IP address instead.
00372 
00373      If not ARP table entry is found, we overwrite the original IP
00374      packet with an ARP request for the IP address. */
00375 
00376   /* First check if destination is a local broadcast. */
00377   if(uip_ipaddr_cmp(&IPBUF->destipaddr, &uip_broadcast_addr)) {
00378     memcpy(IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6);
00379   } else if(IPBUF->destipaddr.u8[0] == 224) {
00380     /* Multicast. */
00381     IPBUF->ethhdr.dest.addr[0] = 0x01;
00382     IPBUF->ethhdr.dest.addr[1] = 0x00;
00383     IPBUF->ethhdr.dest.addr[2] = 0x5e;
00384     IPBUF->ethhdr.dest.addr[3] = IPBUF->destipaddr.u8[1];
00385     IPBUF->ethhdr.dest.addr[4] = IPBUF->destipaddr.u8[2];
00386     IPBUF->ethhdr.dest.addr[5] = IPBUF->destipaddr.u8[3];
00387   } else {
00388     /* Check if the destination address is on the local network. */
00389     if(!uip_ipaddr_maskcmp(&IPBUF->destipaddr, &uip_hostaddr, &uip_netmask)) {
00390       /* Destination address was not on the local network, so we need to
00391      use the default router's IP address instead of the destination
00392      address when determining the MAC address. */
00393       uip_ipaddr_copy(&ipaddr, &uip_draddr);
00394     } else {
00395       /* Else, we use the destination IP address. */
00396       uip_ipaddr_copy(&ipaddr, &IPBUF->destipaddr);
00397     }
00398     for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
00399       if(uip_ipaddr_cmp(&ipaddr, &tabptr->ipaddr)) {
00400     break;
00401       }
00402       tabptr++;
00403     }
00404 
00405     if(i == UIP_ARPTAB_SIZE) {
00406       /* The destination address was not in our ARP table, so we
00407      overwrite the IP packet with an ARP request. */
00408 
00409       memset(BUF->ethhdr.dest.addr, 0xff, 6);
00410       memset(BUF->dhwaddr.addr, 0x00, 6);
00411       memcpy(BUF->ethhdr.src.addr, uip_lladdr.addr, 6);
00412       memcpy(BUF->shwaddr.addr, uip_lladdr.addr, 6);
00413     
00414       uip_ipaddr_copy(&BUF->dipaddr, &ipaddr);
00415       uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr);
00416       BUF->opcode = UIP_HTONS(ARP_REQUEST); /* ARP request. */
00417       BUF->hwtype = UIP_HTONS(ARP_HWTYPE_ETH);
00418       BUF->protocol = UIP_HTONS(UIP_ETHTYPE_IP);
00419       BUF->hwlen = 6;
00420       BUF->protolen = 4;
00421       BUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP);
00422 
00423       uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN];
00424     
00425       uip_len = sizeof(struct arp_hdr);
00426       return;
00427     }
00428 
00429     /* Build an ethernet header. */
00430     memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);
00431   }
00432   memcpy(IPBUF->ethhdr.src.addr, uip_lladdr.addr, 6);
00433   
00434   IPBUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_IP);
00435 
00436   uip_len += sizeof(struct uip_eth_hdr);
00437 }
00438 /*-----------------------------------------------------------------------------------*/
00439 
00440 /** @} */
00441 /** @} */
00442