Eduardo Pellini / F7_Ethernet_Pellini

Fork of F7_Ethernet by Dieter Graef

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers etharp.c Source File

etharp.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Address Resolution Protocol module for IP over Ethernet
00004  *
00005  * Functionally, ARP is divided into two parts. The first maps an IP address
00006  * to a physical address when sending a packet, and the second part answers
00007  * requests from other machines for our physical address.
00008  *
00009  * This implementation complies with RFC 826 (Ethernet ARP). It supports
00010  * Gratuitious ARP from RFC3220 (IP Mobility Support for IPv4) section 4.6
00011  * if an interface calls etharp_gratuitous(our_netif) upon address change.
00012  */
00013 
00014 /*
00015  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
00016  * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv>
00017  * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
00018  * All rights reserved.
00019  *
00020  * Redistribution and use in source and binary forms, with or without modification,
00021  * are permitted provided that the following conditions are met:
00022  *
00023  * 1. Redistributions of source code must retain the above copyright notice,
00024  *    this list of conditions and the following disclaimer.
00025  * 2. Redistributions in binary form must reproduce the above copyright notice,
00026  *    this list of conditions and the following disclaimer in the documentation
00027  *    and/or other materials provided with the distribution.
00028  * 3. The name of the author may not be used to endorse or promote products
00029  *    derived from this software without specific prior written permission.
00030  *
00031  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00032  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00033  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00034  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00035  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00036  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00037  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00038  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00039  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00040  * OF SUCH DAMAGE.
00041  *
00042  * This file is part of the lwIP TCP/IP stack.
00043  *
00044  */
00045  
00046 #include "lwip/opt.h"
00047 
00048 //By Pellini
00049 #if LWIP_ARP || LWIP_ETHERNET || LWIP_GOOSE
00050 
00051 #include "lwip/ip_addr.h"
00052 #include "lwip/def.h"
00053 #include "lwip/ip.h"
00054 #include "lwip/stats.h"
00055 #include "lwip/snmp.h"
00056 #include "lwip/dhcp.h "
00057 #include "lwip/autoip.h"
00058 #include "netif/etharp.h"
00059 
00060 #if PPPOE_SUPPORT
00061 #include "netif/ppp_oe.h"
00062 #endif /* PPPOE_SUPPORT */
00063 
00064 #include <string.h>
00065 
00066 const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
00067 const struct eth_addr ethzero = {{0,0,0,0,0,0}};
00068 
00069 /** The 24-bit IANA multicast OUI is 01-00-5e: */
00070 #define LL_MULTICAST_ADDR_0 0x01
00071 #define LL_MULTICAST_ADDR_1 0x00
00072 #define LL_MULTICAST_ADDR_2 0x5e
00073 
00074 #if LWIP_ARP /* don't build if not configured for use in lwipopts.h */
00075 
00076 /** the time an ARP entry stays valid after its last update,
00077  *  for ARP_TMR_INTERVAL = 5000, this is
00078  *  (240 * 5) seconds = 20 minutes.
00079  */
00080 #define ARP_MAXAGE              240
00081 /** Re-request a used ARP entry 1 minute before it would expire to prevent
00082  *  breaking a steadily used connection because the ARP entry timed out. */
00083 #define ARP_AGE_REREQUEST_USED  (ARP_MAXAGE - 12)
00084 
00085 /** the time an ARP entry stays pending after first request,
00086  *  for ARP_TMR_INTERVAL = 5000, this is
00087  *  (2 * 5) seconds = 10 seconds.
00088  * 
00089  *  @internal Keep this number at least 2, otherwise it might
00090  *  run out instantly if the timeout occurs directly after a request.
00091  */
00092 #define ARP_MAXPENDING 2
00093 
00094 #define HWTYPE_ETHERNET 1
00095 
00096 enum etharp_state {
00097   ETHARP_STATE_EMPTY = 0,
00098   ETHARP_STATE_PENDING,
00099   ETHARP_STATE_STABLE,
00100   ETHARP_STATE_STABLE_REREQUESTING
00101 #if ETHARP_SUPPORT_STATIC_ENTRIES
00102   ,ETHARP_STATE_STATIC
00103 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
00104 };
00105 
00106 struct etharp_entry {
00107 #if ARP_QUEUEING
00108   /** Pointer to queue of pending outgoing packets on this ARP entry. */
00109   struct etharp_q_entry *q;
00110 #else /* ARP_QUEUEING */
00111   /** Pointer to a single pending outgoing packet on this ARP entry. */
00112   struct pbuf *q;
00113 #endif /* ARP_QUEUEING */
00114   ip_addr_t ipaddr;
00115   struct netif *netif;
00116   struct eth_addr ethaddr;
00117   u8_t state;
00118   u8_t ctime;
00119 };
00120 
00121 static struct etharp_entry arp_table[ARP_TABLE_SIZE];
00122 
00123 #if !LWIP_NETIF_HWADDRHINT
00124 static u8_t etharp_cached_entry;
00125 #endif /* !LWIP_NETIF_HWADDRHINT */
00126 
00127 /** Try hard to create a new entry - we want the IP address to appear in
00128     the cache (even if this means removing an active entry or so). */
00129 #define ETHARP_FLAG_TRY_HARD     1
00130 #define ETHARP_FLAG_FIND_ONLY    2
00131 #if ETHARP_SUPPORT_STATIC_ENTRIES
00132 #define ETHARP_FLAG_STATIC_ENTRY 4
00133 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
00134 
00135 #if LWIP_NETIF_HWADDRHINT
00136 #define ETHARP_SET_HINT(netif, hint)  if (((netif) != NULL) && ((netif)->addr_hint != NULL))  \
00137                                       *((netif)->addr_hint) = (hint);
00138 #else /* LWIP_NETIF_HWADDRHINT */
00139 #define ETHARP_SET_HINT(netif, hint)  (etharp_cached_entry = (hint))
00140 #endif /* LWIP_NETIF_HWADDRHINT */
00141 
00142 
00143 /* Some checks, instead of etharp_init(): */
00144 #if (LWIP_ARP && (ARP_TABLE_SIZE > 0x7f))
00145   #error "ARP_TABLE_SIZE must fit in an s8_t, you have to reduce it in your lwipopts.h"
00146 #endif
00147 
00148 
00149 #if ARP_QUEUEING
00150 /**
00151  * Free a complete queue of etharp entries
00152  *
00153  * @param q a qeueue of etharp_q_entry's to free
00154  */
00155 static void
00156 free_etharp_q(struct etharp_q_entry *q)
00157 {
00158   struct etharp_q_entry *r;
00159   LWIP_ASSERT("q != NULL", q != NULL);
00160   LWIP_ASSERT("q->p != NULL", q->p != NULL);
00161   while (q) {
00162     r = q;
00163     q = q->next;
00164     LWIP_ASSERT("r->p != NULL", (r->p != NULL));
00165     pbuf_free(r->p);
00166     memp_free(MEMP_ARP_QUEUE, r);
00167   }
00168 }
00169 #else /* ARP_QUEUEING */
00170 
00171 /** Compatibility define: free the queued pbuf */
00172 #define free_etharp_q(q) pbuf_free(q)
00173 
00174 #endif /* ARP_QUEUEING */
00175 
00176 /** Clean up ARP table entries */
00177 static void
00178 etharp_free_entry(int i)
00179 {
00180   /* remove from SNMP ARP index tree */
00181   snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr);
00182   /* and empty packet queue */
00183   if (arp_table[i].q != NULL) {
00184     /* remove all queued packets */
00185     LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_free_entry: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q)));
00186     free_etharp_q(arp_table[i].q);
00187     arp_table[i].q = NULL;
00188   }
00189   /* recycle entry for re-use */
00190   arp_table[i].state = ETHARP_STATE_EMPTY;
00191 #ifdef LWIP_DEBUG
00192   /* for debugging, clean out the complete entry */
00193   arp_table[i].ctime = 0;
00194   arp_table[i].netif = NULL;
00195   ip_addr_set_zero(&arp_table[i].ipaddr);
00196   arp_table[i].ethaddr = ethzero;
00197 #endif /* LWIP_DEBUG */
00198 }
00199 
00200 /**
00201  * Clears expired entries in the ARP table.
00202  *
00203  * This function should be called every ETHARP_TMR_INTERVAL milliseconds (5 seconds),
00204  * in order to expire entries in the ARP table.
00205  */
00206 void
00207 etharp_tmr(void)
00208 {
00209   u8_t i;
00210 
00211   LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
00212   /* remove expired entries from the ARP table */
00213   for (i = 0; i < ARP_TABLE_SIZE; ++i) {
00214     u8_t state = arp_table[i].state;
00215     if (state != ETHARP_STATE_EMPTY
00216 #if ETHARP_SUPPORT_STATIC_ENTRIES
00217       && (state != ETHARP_STATE_STATIC)
00218 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
00219       ) {
00220       arp_table[i].ctime++;
00221       if ((arp_table[i].ctime >= ARP_MAXAGE) ||
00222           ((arp_table[i].state == ETHARP_STATE_PENDING)  &&
00223            (arp_table[i].ctime >= ARP_MAXPENDING))) {
00224         /* pending or stable entry has become old! */
00225         LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %"U16_F".\n",
00226              arp_table[i].state >= ETHARP_STATE_STABLE ? "stable" : "pending", (u16_t)i));
00227         /* clean up entries that have just been expired */
00228         etharp_free_entry(i);
00229       }
00230       else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING) {
00231         /* Reset state to stable, so that the next transmitted packet will
00232            re-send an ARP request. */
00233         arp_table[i].state = ETHARP_STATE_STABLE;
00234       }
00235 #if ARP_QUEUEING
00236       /* still pending entry? (not expired) */
00237       if (arp_table[i].state == ETHARP_STATE_PENDING) {
00238         /* resend an ARP query here? */
00239       }
00240 #endif /* ARP_QUEUEING */
00241     }
00242   }
00243 }
00244 
00245 /**
00246  * Search the ARP table for a matching or new entry.
00247  * 
00248  * If an IP address is given, return a pending or stable ARP entry that matches
00249  * the address. If no match is found, create a new entry with this address set,
00250  * but in state ETHARP_EMPTY. The caller must check and possibly change the
00251  * state of the returned entry.
00252  * 
00253  * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
00254  * 
00255  * In all cases, attempt to create new entries from an empty entry. If no
00256  * empty entries are available and ETHARP_FLAG_TRY_HARD flag is set, recycle
00257  * old entries. Heuristic choose the least important entry for recycling.
00258  *
00259  * @param ipaddr IP address to find in ARP cache, or to add if not found.
00260  * @param flags @see definition of ETHARP_FLAG_*
00261  * @param netif netif related to this address (used for NETIF_HWADDRHINT)
00262  *  
00263  * @return The ARP entry index that matched or is created, ERR_MEM if no
00264  * entry is found or could be recycled.
00265  */
00266 static s8_t
00267 etharp_find_entry(ip_addr_t *ipaddr, u8_t flags)
00268 {
00269   s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
00270   s8_t empty = ARP_TABLE_SIZE;
00271   u8_t i = 0, age_pending = 0, age_stable = 0;
00272   /* oldest entry with packets on queue */
00273   s8_t old_queue = ARP_TABLE_SIZE;
00274   /* its age */
00275   u8_t age_queue = 0;
00276 
00277   /**
00278    * a) do a search through the cache, remember candidates
00279    * b) select candidate entry
00280    * c) create new entry
00281    */
00282 
00283   /* a) in a single search sweep, do all of this
00284    * 1) remember the first empty entry (if any)
00285    * 2) remember the oldest stable entry (if any)
00286    * 3) remember the oldest pending entry without queued packets (if any)
00287    * 4) remember the oldest pending entry with queued packets (if any)
00288    * 5) search for a matching IP entry, either pending or stable
00289    *    until 5 matches, or all entries are searched for.
00290    */
00291 
00292   for (i = 0; i < ARP_TABLE_SIZE; ++i) {
00293     u8_t state = arp_table[i].state;
00294     /* no empty entry found yet and now we do find one? */
00295     if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) {
00296       LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_find_entry: found empty entry %"U16_F"\n", (u16_t)i));
00297       /* remember first empty entry */
00298       empty = i;
00299     } else if (state != ETHARP_STATE_EMPTY) {
00300       LWIP_ASSERT("state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE",
00301         state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE);
00302       /* if given, does IP address match IP address in ARP entry? */
00303       if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
00304         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: found matching entry %"U16_F"\n", (u16_t)i));
00305         /* found exact IP address match, simply bail out */
00306         return i;
00307       }
00308       /* pending entry? */
00309       if (state == ETHARP_STATE_PENDING) {
00310         /* pending with queued packets? */
00311         if (arp_table[i].q != NULL) {
00312           if (arp_table[i].ctime >= age_queue) {
00313             old_queue = i;
00314             age_queue = arp_table[i].ctime;
00315           }
00316         } else
00317         /* pending without queued packets? */
00318         {
00319           if (arp_table[i].ctime >= age_pending) {
00320             old_pending = i;
00321             age_pending = arp_table[i].ctime;
00322           }
00323         }
00324       /* stable entry? */
00325       } else if (state >= ETHARP_STATE_STABLE) {
00326 #if ETHARP_SUPPORT_STATIC_ENTRIES
00327         /* don't record old_stable for static entries since they never expire */
00328         if (state < ETHARP_STATE_STATIC)
00329 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
00330         {
00331           /* remember entry with oldest stable entry in oldest, its age in maxtime */
00332           if (arp_table[i].ctime >= age_stable) {
00333             old_stable = i;
00334             age_stable = arp_table[i].ctime;
00335           }
00336         }
00337       }
00338     }
00339   }
00340   /* { we have no match } => try to create a new entry */
00341    
00342   /* don't create new entry, only search? */
00343   if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) ||
00344       /* or no empty entry found and not allowed to recycle? */
00345       ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) {
00346     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty entry found and not allowed to recycle\n"));
00347     return (s8_t)ERR_MEM;
00348   }
00349   
00350   /* b) choose the least destructive entry to recycle:
00351    * 1) empty entry
00352    * 2) oldest stable entry
00353    * 3) oldest pending entry without queued packets
00354    * 4) oldest pending entry with queued packets
00355    * 
00356    * { ETHARP_FLAG_TRY_HARD is set at this point }
00357    */ 
00358 
00359   /* 1) empty entry available? */
00360   if (empty < ARP_TABLE_SIZE) {
00361     i = empty;
00362     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting empty entry %"U16_F"\n", (u16_t)i));
00363   } else {
00364     /* 2) found recyclable stable entry? */
00365     if (old_stable < ARP_TABLE_SIZE) {
00366       /* recycle oldest stable*/
00367       i = old_stable;
00368       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest stable entry %"U16_F"\n", (u16_t)i));
00369       /* no queued packets should exist on stable entries */
00370       LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL);
00371     /* 3) found recyclable pending entry without queued packets? */
00372     } else if (old_pending < ARP_TABLE_SIZE) {
00373       /* recycle oldest pending */
00374       i = old_pending;
00375       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %"U16_F" (without queue)\n", (u16_t)i));
00376     /* 4) found recyclable pending entry with queued packets? */
00377     } else if (old_queue < ARP_TABLE_SIZE) {
00378       /* recycle oldest pending (queued packets are free in etharp_free_entry) */
00379       i = old_queue;
00380       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %"U16_F", freeing packet queue %p\n", (u16_t)i, (void *)(arp_table[i].q)));
00381       /* no empty or recyclable entries found */
00382     } else {
00383       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty or recyclable entries found\n"));
00384       return (s8_t)ERR_MEM;
00385     }
00386 
00387     /* { empty or recyclable entry found } */
00388     LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
00389     etharp_free_entry(i);
00390   }
00391 
00392   LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
00393   LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY",
00394     arp_table[i].state == ETHARP_STATE_EMPTY);
00395 
00396   /* IP address given? */
00397   if (ipaddr != NULL) {
00398     /* set IP address */
00399     ip_addr_copy(arp_table[i].ipaddr, *ipaddr);
00400   }
00401   arp_table[i].ctime = 0;
00402   return (err_t)i;
00403 }
00404 
00405 /**
00406  * Send an IP packet on the network using netif->linkoutput
00407  * The ethernet header is filled in before sending.
00408  *
00409  * @params netif the lwIP network interface on which to send the packet
00410  * @params p the packet to send, p->payload pointing to the (uninitialized) ethernet header
00411  * @params src the source MAC address to be copied into the ethernet header
00412  * @params dst the destination MAC address to be copied into the ethernet header
00413  * @return ERR_OK if the packet was sent, any other err_t on failure
00414  */
00415 static err_t
00416 etharp_send_ip(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
00417 {
00418   struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
00419 
00420   LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
00421               (netif->hwaddr_len == ETHARP_HWADDR_LEN));
00422   ETHADDR32_COPY(&ethhdr->dest, dst);
00423   ETHADDR16_COPY(&ethhdr->src, src);
00424   ethhdr->type = PP_HTONS(ETHTYPE_IP);
00425   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_send_ip: sending packet %p\n", (void *)p));
00426   /* send the packet */
00427   return netif->linkoutput(netif, p);
00428 }
00429 
00430 /**
00431  * Update (or insert) a IP/MAC address pair in the ARP cache.
00432  *
00433  * If a pending entry is resolved, any queued packets will be sent
00434  * at this point.
00435  * 
00436  * @param netif netif related to this entry (used for NETIF_ADDRHINT)
00437  * @param ipaddr IP address of the inserted ARP entry.
00438  * @param ethaddr Ethernet address of the inserted ARP entry.
00439  * @param flags @see definition of ETHARP_FLAG_*
00440  *
00441  * @return
00442  * - ERR_OK Succesfully updated ARP cache.
00443  * - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set.
00444  * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
00445  *
00446  * @see pbuf_free()
00447  */
00448 static err_t
00449 etharp_update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
00450 {
00451   s8_t i;
00452   LWIP_ASSERT("netif->hwaddr_len == ETHARP_HWADDR_LEN", netif->hwaddr_len == ETHARP_HWADDR_LEN);
00453   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n",
00454     ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
00455     ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
00456     ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
00457   /* non-unicast address? */
00458   if (ip_addr_isany(ipaddr) ||
00459       ip_addr_isbroadcast(ipaddr, netif) ||
00460       ip_addr_ismulticast(ipaddr)) {
00461     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
00462     return ERR_ARG;
00463   }
00464   /* find or create ARP entry */
00465   i = etharp_find_entry(ipaddr, flags);
00466   /* bail out if no entry could be found */
00467   if (i < 0) {
00468     return (err_t)i;
00469   }
00470 
00471 #if ETHARP_SUPPORT_STATIC_ENTRIES
00472   if (flags & ETHARP_FLAG_STATIC_ENTRY) {
00473     /* record static type */
00474     arp_table[i].state = ETHARP_STATE_STATIC;
00475   } else
00476 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
00477   {
00478     /* mark it stable */
00479     arp_table[i].state = ETHARP_STATE_STABLE;
00480   }
00481 
00482   /* record network interface */
00483   arp_table[i].netif = netif;
00484   /* insert in SNMP ARP index tree */
00485   snmp_insert_arpidx_tree(netif, &arp_table[i].ipaddr);
00486 
00487   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i));
00488   /* update address */
00489   ETHADDR32_COPY(&arp_table[i].ethaddr, ethaddr);
00490   /* reset time stamp */
00491   arp_table[i].ctime = 0;
00492   /* this is where we will send out queued packets! */
00493 #if ARP_QUEUEING
00494   while (arp_table[i].q != NULL) {
00495     struct pbuf *p;
00496     /* remember remainder of queue */
00497     struct etharp_q_entry *q = arp_table[i].q;
00498     /* pop first item off the queue */
00499     arp_table[i].q = q->next;
00500     /* get the packet pointer */
00501     p = q->p;
00502     /* now queue entry can be freed */
00503     memp_free(MEMP_ARP_QUEUE, q);
00504 #else /* ARP_QUEUEING */
00505   if (arp_table[i].q != NULL) {
00506     struct pbuf *p = arp_table[i].q;
00507     arp_table[i].q = NULL;
00508 #endif /* ARP_QUEUEING */
00509     /* send the queued IP packet */
00510     etharp_send_ip(netif, p, (struct eth_addr*)(netif->hwaddr), ethaddr);
00511     /* free the queued IP packet */
00512     pbuf_free(p);
00513   }
00514   return ERR_OK;
00515 }
00516 
00517 #if ETHARP_SUPPORT_STATIC_ENTRIES
00518 /** Add a new static entry to the ARP table. If an entry exists for the
00519  * specified IP address, this entry is overwritten.
00520  * If packets are queued for the specified IP address, they are sent out.
00521  *
00522  * @param ipaddr IP address for the new static entry
00523  * @param ethaddr ethernet address for the new static entry
00524  * @return @see return values of etharp_add_static_entry
00525  */
00526 err_t
00527 etharp_add_static_entry(ip_addr_t *ipaddr, struct eth_addr *ethaddr)
00528 {
00529   struct netif *netif;
00530   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_add_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n",
00531     ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
00532     ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
00533     ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
00534 
00535   netif = ip_route(ipaddr);
00536   if (netif == NULL) {
00537     return ERR_RTE;
00538   }
00539 
00540   return etharp_update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY);
00541 }
00542 
00543 /** Remove a static entry from the ARP table previously added with a call to
00544  * etharp_add_static_entry.
00545  *
00546  * @param ipaddr IP address of the static entry to remove
00547  * @return ERR_OK: entry removed
00548  *         ERR_MEM: entry wasn't found
00549  *         ERR_ARG: entry wasn't a static entry but a dynamic one
00550  */
00551 err_t
00552 etharp_remove_static_entry(ip_addr_t *ipaddr)
00553 {
00554   s8_t i;
00555   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_remove_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
00556     ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
00557 
00558   /* find or create ARP entry */
00559   i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY);
00560   /* bail out if no entry could be found */
00561   if (i < 0) {
00562     return (err_t)i;
00563   }
00564 
00565   if (arp_table[i].state != ETHARP_STATE_STATIC) {
00566     /* entry wasn't a static entry, cannot remove it */
00567     return ERR_ARG;
00568   }
00569   /* entry found, free it */
00570   etharp_free_entry(i);
00571   return ERR_OK;
00572 }
00573 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
00574 
00575 /**
00576  * Remove all ARP table entries of the specified netif.
00577  *
00578  * @param netif points to a network interface
00579  */
00580 void etharp_cleanup_netif(struct netif *netif)
00581 {
00582   u8_t i;
00583 
00584   for (i = 0; i < ARP_TABLE_SIZE; ++i) {
00585     u8_t state = arp_table[i].state;
00586     if ((state != ETHARP_STATE_EMPTY) && (arp_table[i].netif == netif)) {
00587       etharp_free_entry(i);
00588     }
00589   }
00590 }
00591 
00592 /**
00593  * Finds (stable) ethernet/IP address pair from ARP table
00594  * using interface and IP address index.
00595  * @note the addresses in the ARP table are in network order!
00596  *
00597  * @param netif points to interface index
00598  * @param ipaddr points to the (network order) IP address index
00599  * @param eth_ret points to return pointer
00600  * @param ip_ret points to return pointer
00601  * @return table index if found, -1 otherwise
00602  */
00603 s8_t
00604 etharp_find_addr(struct netif *netif, ip_addr_t *ipaddr,
00605          struct eth_addr **eth_ret, ip_addr_t **ip_ret)
00606 {
00607   s8_t i;
00608 
00609   LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL",
00610     eth_ret != NULL && ip_ret != NULL);
00611 
00612   LWIP_UNUSED_ARG(netif);
00613 
00614   i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY);
00615   if((i >= 0) && (arp_table[i].state >= ETHARP_STATE_STABLE)) {
00616       *eth_ret = &arp_table[i].ethaddr;
00617       *ip_ret = &arp_table[i].ipaddr;
00618       return i;
00619   }
00620   return -1;
00621 }
00622 
00623 #if ETHARP_TRUST_IP_MAC
00624 /**
00625  * Updates the ARP table using the given IP packet.
00626  *
00627  * Uses the incoming IP packet's source address to update the
00628  * ARP cache for the local network. The function does not alter
00629  * or free the packet. This function must be called before the
00630  * packet p is passed to the IP layer.
00631  *
00632  * @param netif The lwIP network interface on which the IP packet pbuf arrived.
00633  * @param p The IP packet that arrived on netif.
00634  *
00635  * @return NULL
00636  *
00637  * @see pbuf_free()
00638  */
00639 static void
00640 etharp_ip_input(struct netif *netif, struct pbuf *p)
00641 {
00642   struct eth_hdr *ethhdr;
00643   struct ip_hdr *iphdr;
00644   ip_addr_t iphdr_src;
00645   LWIP_ERROR("netif != NULL", (netif != NULL), return;);
00646 
00647   /* Only insert an entry if the source IP address of the
00648      incoming IP packet comes from a host on the local network. */
00649   ethhdr = (struct eth_hdr *)p->payload;
00650   iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
00651 #if ETHARP_SUPPORT_VLAN
00652   if (ethhdr->type == PP_HTONS(ETHTYPE_VLAN)) {
00653     iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR);
00654   }
00655 #endif /* ETHARP_SUPPORT_VLAN */
00656 
00657   ip_addr_copy(iphdr_src, iphdr->src);
00658 
00659   /* source is not on the local network? */
00660   if (!ip_addr_netcmp(&iphdr_src, &(netif->ip_addr), &(netif->netmask))) {
00661     /* do nothing */
00662     return;
00663   }
00664 
00665   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n"));
00666   /* update the source IP address in the cache, if present */
00667   /* @todo We could use ETHARP_FLAG_TRY_HARD if we think we are going to talk
00668    * back soon (for example, if the destination IP address is ours. */
00669   etharp_update_arp_entry(netif, &iphdr_src, &(ethhdr->src), ETHARP_FLAG_FIND_ONLY);
00670 }
00671 #endif /* ETHARP_TRUST_IP_MAC */
00672 
00673 /**
00674  * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache  
00675  * send out queued IP packets. Updates cache with snooped address pairs.
00676  *
00677  * Should be called for incoming ARP packets. The pbuf in the argument
00678  * is freed by this function.
00679  *
00680  * @param netif The lwIP network interface on which the ARP packet pbuf arrived.
00681  * @param ethaddr Ethernet address of netif.
00682  * @param p The ARP packet that arrived on netif. Is freed by this function.
00683  *
00684  * @return NULL
00685  *
00686  * @see pbuf_free()
00687  */
00688 static void
00689 etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p)
00690 {
00691   struct etharp_hdr *hdr;
00692   struct eth_hdr *ethhdr;
00693   /* these are aligned properly, whereas the ARP header fields might not be */
00694   ip_addr_t sipaddr, dipaddr;
00695   u8_t for_us;
00696 #if LWIP_AUTOIP
00697   const u8_t * ethdst_hwaddr;
00698 #endif /* LWIP_AUTOIP */
00699 
00700   LWIP_ERROR("netif != NULL", (netif != NULL), return;);
00701 
00702   /* drop short ARP packets: we have to check for p->len instead of p->tot_len here
00703      since a struct etharp_hdr is pointed to p->payload, so it musn't be chained! */
00704   if (p->len < SIZEOF_ETHARP_PACKET) {
00705     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
00706       ("etharp_arp_input: packet dropped, too short (%"S16_F"/%"S16_F")\n", p->tot_len,
00707       (s16_t)SIZEOF_ETHARP_PACKET));
00708     ETHARP_STATS_INC(etharp.lenerr);
00709     ETHARP_STATS_INC(etharp.drop);
00710     pbuf_free(p);
00711     return;
00712   }
00713 
00714   ethhdr = (struct eth_hdr *)p->payload;
00715   hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
00716 #if ETHARP_SUPPORT_VLAN
00717   if (ethhdr->type == PP_HTONS(ETHTYPE_VLAN)) {
00718     hdr = (struct etharp_hdr *)(((u8_t*)ethhdr) + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR);
00719   }
00720 #endif /* ETHARP_SUPPORT_VLAN */
00721 
00722   /* RFC 826 "Packet Reception": */
00723   if ((hdr->hwtype != PP_HTONS(HWTYPE_ETHERNET)) ||
00724       (hdr->hwlen != ETHARP_HWADDR_LEN) ||
00725       (hdr->protolen != sizeof(ip_addr_t)) ||
00726       (hdr->proto != PP_HTONS(ETHTYPE_IP)))  {
00727     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
00728       ("etharp_arp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n",
00729       hdr->hwtype, hdr->hwlen, hdr->proto, hdr->protolen));
00730     ETHARP_STATS_INC(etharp.proterr);
00731     ETHARP_STATS_INC(etharp.drop);
00732     pbuf_free(p);
00733     return;
00734   }
00735   ETHARP_STATS_INC(etharp.recv);
00736 
00737 #if LWIP_AUTOIP
00738   /* We have to check if a host already has configured our random
00739    * created link local address and continously check if there is
00740    * a host with this IP-address so we can detect collisions */
00741   autoip_arp_reply(netif, hdr);
00742 #endif /* LWIP_AUTOIP */
00743 
00744   /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
00745    * structure packing (not using structure copy which breaks strict-aliasing rules). */
00746   IPADDR2_COPY(&sipaddr, &hdr->sipaddr);
00747   IPADDR2_COPY(&dipaddr, &hdr->dipaddr);
00748 
00749   /* this interface is not configured? */
00750   if (ip_addr_isany(&netif->ip_addr)) {
00751     for_us = 0;
00752   } else {
00753     /* ARP packet directed to us? */
00754     for_us = (u8_t)ip_addr_cmp(&dipaddr, &(netif->ip_addr));
00755   }
00756 
00757   /* ARP message directed to us?
00758       -> add IP address in ARP cache; assume requester wants to talk to us,
00759          can result in directly sending the queued packets for this host.
00760      ARP message not directed to us?
00761       ->  update the source IP address in the cache, if present */
00762   etharp_update_arp_entry(netif, &sipaddr, &(hdr->shwaddr),
00763                    for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY);
00764 
00765   /* now act on the message itself */
00766   switch (hdr->opcode) {
00767   /* ARP request? */
00768   case PP_HTONS(ARP_REQUEST):
00769     /* ARP request. If it asked for our address, we send out a
00770      * reply. In any case, we time-stamp any existing ARP entry,
00771      * and possiby send out an IP packet that was queued on it. */
00772 
00773     LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP request\n"));
00774     /* ARP request for our address? */
00775     if (for_us) {
00776 
00777       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: replying to ARP request for our IP address\n"));
00778       /* Re-use pbuf to send ARP reply.
00779          Since we are re-using an existing pbuf, we can't call etharp_raw since
00780          that would allocate a new pbuf. */
00781       hdr->opcode = htons(ARP_REPLY);
00782 
00783       IPADDR2_COPY(&hdr->dipaddr, &hdr->sipaddr);
00784       IPADDR2_COPY(&hdr->sipaddr, &netif->ip_addr);
00785 
00786       LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
00787                   (netif->hwaddr_len == ETHARP_HWADDR_LEN));
00788 #if LWIP_AUTOIP
00789       /* If we are using Link-Local, all ARP packets that contain a Link-Local
00790        * 'sender IP address' MUST be sent using link-layer broadcast instead of
00791        * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
00792       ethdst_hwaddr = ip_addr_islinklocal(&netif->ip_addr) ? (u8_t*)(ethbroadcast.addr) : hdr->shwaddr.addr;
00793 #endif /* LWIP_AUTOIP */
00794 
00795       ETHADDR16_COPY(&hdr->dhwaddr, &hdr->shwaddr);
00796 #if LWIP_AUTOIP
00797       ETHADDR16_COPY(&ethhdr->dest, ethdst_hwaddr);
00798 #else  /* LWIP_AUTOIP */
00799       ETHADDR16_COPY(&ethhdr->dest, &hdr->shwaddr);
00800 #endif /* LWIP_AUTOIP */
00801       ETHADDR16_COPY(&hdr->shwaddr, ethaddr);
00802       ETHADDR16_COPY(&ethhdr->src, ethaddr);
00803 
00804       /* hwtype, hwaddr_len, proto, protolen and the type in the ethernet header
00805          are already correct, we tested that before */
00806 
00807       /* return ARP reply */
00808       netif->linkoutput(netif, p);
00809     /* we are not configured? */
00810     } else if (ip_addr_isany(&netif->ip_addr)) {
00811       /* { for_us == 0 and netif->ip_addr.addr == 0 } */
00812       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n"));
00813     /* request was not directed to us */
00814     } else {
00815       /* { for_us == 0 and netif->ip_addr.addr != 0 } */
00816       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP request was not for us.\n"));
00817     }
00818     break;
00819   case PP_HTONS(ARP_REPLY):
00820     /* ARP reply. We already updated the ARP cache earlier. */
00821     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP reply\n"));
00822 #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
00823     /* DHCP wants to know about ARP replies from any host with an
00824      * IP address also offered to us by the DHCP server. We do not
00825      * want to take a duplicate IP address on a single network.
00826      * @todo How should we handle redundant (fail-over) interfaces? */
00827     dhcp_arp_reply(netif, &sipaddr);
00828 #endif /* (LWIP_DHCP && DHCP_DOES_ARP_CHECK) */
00829     break;
00830   default:
00831     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP unknown opcode type %"S16_F"\n", htons(hdr->opcode)));
00832     ETHARP_STATS_INC(etharp.err);
00833     break;
00834   }
00835   /* free ARP packet */
00836   pbuf_free(p);
00837 }
00838 
00839 /** Just a small helper function that sends a pbuf to an ethernet address
00840  * in the arp_table specified by the index 'arp_idx'.
00841  */
00842 static err_t
00843 etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, u8_t arp_idx)
00844 {
00845   LWIP_ASSERT("arp_table[arp_idx].state >= ETHARP_STATE_STABLE",
00846               arp_table[arp_idx].state >= ETHARP_STATE_STABLE);
00847   /* if arp table entry is about to expire: re-request it,
00848      but only if its state is ETHARP_STATE_STABLE to prevent flooding the
00849      network with ARP requests if this address is used frequently. */
00850   if ((arp_table[arp_idx].state == ETHARP_STATE_STABLE) && 
00851       (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED)) {
00852     if (etharp_request(netif, &arp_table[arp_idx].ipaddr) == ERR_OK) {
00853       arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING;
00854     }
00855   }
00856   
00857   return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr),
00858     &arp_table[arp_idx].ethaddr);
00859 }
00860 
00861 /**
00862  * Resolve and fill-in Ethernet address header for outgoing IP packet.
00863  *
00864  * For IP multicast and broadcast, corresponding Ethernet addresses
00865  * are selected and the packet is transmitted on the link.
00866  *
00867  * For unicast addresses, the packet is submitted to etharp_query(). In
00868  * case the IP address is outside the local network, the IP address of
00869  * the gateway is used.
00870  *
00871  * @param netif The lwIP network interface which the IP packet will be sent on.
00872  * @param q The pbuf(s) containing the IP packet to be sent.
00873  * @param ipaddr The IP address of the packet destination.
00874  *
00875  * @return
00876  * - ERR_RTE No route to destination (no gateway to external networks),
00877  * or the return type of either etharp_query() or etharp_send_ip().
00878  */
00879 err_t
00880 etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr)
00881 {
00882   struct eth_addr *dest;
00883   struct eth_addr mcastaddr;
00884   ip_addr_t *dst_addr = ipaddr;
00885 
00886   LWIP_ASSERT("netif != NULL", netif != NULL);
00887   LWIP_ASSERT("q != NULL", q != NULL);
00888   LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL);
00889 
00890   /* make room for Ethernet header - should not fail */
00891   if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
00892     /* bail out */
00893     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
00894       ("etharp_output: could not allocate room for header.\n"));
00895     LINK_STATS_INC(link.lenerr);
00896     return ERR_BUF;
00897   }
00898 
00899   /* Determine on destination hardware address. Broadcasts and multicasts
00900    * are special, other IP addresses are looked up in the ARP table. */
00901 
00902   /* broadcast destination IP address? */
00903   if (ip_addr_isbroadcast(ipaddr, netif)) {
00904     /* broadcast on Ethernet also */
00905     dest = (struct eth_addr *)&ethbroadcast;
00906   /* multicast destination IP address? */
00907   } else if (ip_addr_ismulticast(ipaddr)) {
00908     /* Hash IP multicast address to MAC address.*/
00909     mcastaddr.addr[0] = LL_MULTICAST_ADDR_0;
00910     mcastaddr.addr[1] = LL_MULTICAST_ADDR_1;
00911     mcastaddr.addr[2] = LL_MULTICAST_ADDR_2;
00912     mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
00913     mcastaddr.addr[4] = ip4_addr3(ipaddr);
00914     mcastaddr.addr[5] = ip4_addr4(ipaddr);
00915     /* destination Ethernet address is multicast */
00916     dest = &mcastaddr;
00917   /* unicast destination IP address? */
00918   } else {
00919     s8_t i;
00920     /* outside local network? if so, this can neither be a global broadcast nor
00921        a subnet broadcast. */
00922     if (!ip_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask)) &&
00923         !ip_addr_islinklocal(ipaddr)) {
00924 #if LWIP_AUTOIP
00925       struct ip_hdr *iphdr = (struct ip_hdr*)((u8_t*)q->payload +
00926         sizeof(struct eth_hdr));
00927       /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with
00928          a link-local source address must always be "directly to its destination
00929          on the same physical link. The host MUST NOT send the packet to any
00930          router for forwarding". */
00931       if (!ip_addr_islinklocal(&iphdr->src))
00932 #endif /* LWIP_AUTOIP */
00933       {
00934         /* interface has default gateway? */
00935         if (!ip_addr_isany(&netif->gw)) {
00936           /* send to hardware address of default gateway IP address */
00937           dst_addr = &(netif->gw);
00938         /* no default gateway available */
00939         } else {
00940           /* no route to destination error (default gateway missing) */
00941           return ERR_RTE;
00942         }
00943       }
00944     }
00945 #if LWIP_NETIF_HWADDRHINT
00946     if (netif->addr_hint != NULL) {
00947       /* per-pcb cached entry was given */
00948       u8_t etharp_cached_entry = *(netif->addr_hint);
00949       if (etharp_cached_entry < ARP_TABLE_SIZE) {
00950 #endif /* LWIP_NETIF_HWADDRHINT */
00951         if ((arp_table[etharp_cached_entry].state >= ETHARP_STATE_STABLE) &&
00952             (ip_addr_cmp(dst_addr, &arp_table[etharp_cached_entry].ipaddr))) {
00953           /* the per-pcb-cached entry is stable and the right one! */
00954           ETHARP_STATS_INC(etharp.cachehit);
00955           return etharp_output_to_arp_index(netif, q, etharp_cached_entry);
00956         }
00957 #if LWIP_NETIF_HWADDRHINT
00958       }
00959     }
00960 #endif /* LWIP_NETIF_HWADDRHINT */
00961 
00962     /* find stable entry: do this here since this is a critical path for
00963        throughput and etharp_find_entry() is kind of slow */
00964     for (i = 0; i < ARP_TABLE_SIZE; i++) {
00965       if ((arp_table[i].state >= ETHARP_STATE_STABLE) &&
00966           (ip_addr_cmp(dst_addr, &arp_table[i].ipaddr))) {
00967         /* found an existing, stable entry */
00968         ETHARP_SET_HINT(netif, i);
00969         return etharp_output_to_arp_index(netif, q, i);
00970       }
00971     }
00972     /* no stable entry found, use the (slower) query function:
00973        queue on destination Ethernet address belonging to ipaddr */
00974     return etharp_query(netif, dst_addr, q);
00975   }
00976 
00977   /* continuation for multicast/broadcast destinations */
00978   /* obtain source Ethernet address of the given interface */
00979   /* send packet directly on the link */
00980   return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr), dest);
00981 }
00982 
00983 /**
00984  * Send an ARP request for the given IP address and/or queue a packet.
00985  *
00986  * If the IP address was not yet in the cache, a pending ARP cache entry
00987  * is added and an ARP request is sent for the given address. The packet
00988  * is queued on this entry.
00989  *
00990  * If the IP address was already pending in the cache, a new ARP request
00991  * is sent for the given address. The packet is queued on this entry.
00992  *
00993  * If the IP address was already stable in the cache, and a packet is
00994  * given, it is directly sent and no ARP request is sent out. 
00995  * 
00996  * If the IP address was already stable in the cache, and no packet is
00997  * given, an ARP request is sent out.
00998  * 
00999  * @param netif The lwIP network interface on which ipaddr
01000  * must be queried for.
01001  * @param ipaddr The IP address to be resolved.
01002  * @param q If non-NULL, a pbuf that must be delivered to the IP address.
01003  * q is not freed by this function.
01004  *
01005  * @note q must only be ONE packet, not a packet queue!
01006  *
01007  * @return
01008  * - ERR_BUF Could not make room for Ethernet header.
01009  * - ERR_MEM Hardware address unknown, and no more ARP entries available
01010  *   to query for address or queue the packet.
01011  * - ERR_MEM Could not queue packet due to memory shortage.
01012  * - ERR_RTE No route to destination (no gateway to external networks).
01013  * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
01014  *
01015  */
01016 err_t
01017 etharp_query(struct netif *netif, ip_addr_t *ipaddr, struct pbuf *q)
01018 {
01019   struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
01020   err_t result = ERR_MEM;
01021   s8_t i; /* ARP entry index */
01022 
01023   /* non-unicast address? */
01024   if (ip_addr_isbroadcast(ipaddr, netif) ||
01025       ip_addr_ismulticast(ipaddr) ||
01026       ip_addr_isany(ipaddr)) {
01027     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
01028     return ERR_ARG;
01029   }
01030 
01031   /* find entry in ARP cache, ask to create entry if queueing packet */
01032   i = etharp_find_entry(ipaddr, ETHARP_FLAG_TRY_HARD);
01033 
01034   /* could not find or create entry? */
01035   if (i < 0) {
01036     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
01037     if (q) {
01038       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n"));
01039       ETHARP_STATS_INC(etharp.memerr);
01040     }
01041     return (err_t)i;
01042   }
01043 
01044   /* mark a fresh entry as pending (we just sent a request) */
01045   if (arp_table[i].state == ETHARP_STATE_EMPTY) {
01046     arp_table[i].state = ETHARP_STATE_PENDING;
01047   }
01048 
01049   /* { i is either a STABLE or (new or existing) PENDING entry } */
01050   LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
01051   ((arp_table[i].state == ETHARP_STATE_PENDING) ||
01052    (arp_table[i].state >= ETHARP_STATE_STABLE)));
01053 
01054   /* do we have a pending entry? or an implicit query request? */
01055   if ((arp_table[i].state == ETHARP_STATE_PENDING) || (q == NULL)) {
01056     /* try to resolve it; send out ARP request */
01057     result = etharp_request(netif, ipaddr);
01058     if (result != ERR_OK) {
01059       /* ARP request couldn't be sent */
01060       /* We don't re-send arp request in etharp_tmr, but we still queue packets,
01061          since this failure could be temporary, and the next packet calling
01062          etharp_query again could lead to sending the queued packets. */
01063     }
01064     if (q == NULL) {
01065       return result;
01066     }
01067   }
01068 
01069   /* packet given? */
01070   LWIP_ASSERT("q != NULL", q != NULL);
01071   /* stable entry? */
01072   if (arp_table[i].state >= ETHARP_STATE_STABLE) {
01073     /* we have a valid IP->Ethernet address mapping */
01074     ETHARP_SET_HINT(netif, i);
01075     /* send the packet */
01076     result = etharp_send_ip(netif, q, srcaddr, &(arp_table[i].ethaddr));
01077   /* pending entry? (either just created or already pending */
01078   } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
01079     /* entry is still pending, queue the given packet 'q' */
01080     struct pbuf *p;
01081     int copy_needed = 0;
01082     /* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but
01083      * to copy the whole queue into a new PBUF_RAM (see bug #11400) 
01084      * PBUF_ROMs can be left as they are, since ROM must not get changed. */
01085     p = q;
01086     while (p) {
01087       LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0));
01088       if(p->type != PBUF_ROM) {
01089         copy_needed = 1;
01090         break;
01091       }
01092       p = p->next;
01093     }
01094     if(copy_needed) {
01095       /* copy the whole packet into new pbufs */
01096       p = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
01097       if(p != NULL) {
01098         if (pbuf_copy(p, q) != ERR_OK) {
01099           pbuf_free(p);
01100           p = NULL;
01101         }
01102       }
01103     } else {
01104       /* referencing the old pbuf is enough */
01105       p = q;
01106       pbuf_ref(p);
01107     }
01108     /* packet could be taken over? */
01109     if (p != NULL) {
01110       /* queue packet ... */
01111 #if ARP_QUEUEING
01112       struct etharp_q_entry *new_entry;
01113       /* allocate a new arp queue entry */
01114       new_entry = (struct etharp_q_entry *)memp_malloc(MEMP_ARP_QUEUE);
01115       if (new_entry != NULL) {
01116         new_entry->next = 0;
01117         new_entry->p = p;
01118         if(arp_table[i].q != NULL) {
01119           /* queue was already existent, append the new entry to the end */
01120           struct etharp_q_entry *r;
01121           r = arp_table[i].q;
01122           while (r->next != NULL) {
01123             r = r->next;
01124           }
01125           r->next = new_entry;
01126         } else {
01127           /* queue did not exist, first item in queue */
01128           arp_table[i].q = new_entry;
01129         }
01130         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
01131         result = ERR_OK;
01132       } else {
01133         /* the pool MEMP_ARP_QUEUE is empty */
01134         pbuf_free(p);
01135         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
01136         result = ERR_MEM;
01137       }
01138 #else /* ARP_QUEUEING */
01139       /* always queue one packet per ARP request only, freeing a previously queued packet */
01140       if (arp_table[i].q != NULL) {
01141         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: dropped previously queued packet %p for ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
01142         pbuf_free(arp_table[i].q);
01143       }
01144       arp_table[i].q = p;
01145       result = ERR_OK;
01146       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
01147 #endif /* ARP_QUEUEING */
01148     } else {
01149       ETHARP_STATS_INC(etharp.memerr);
01150       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
01151       result = ERR_MEM;
01152     }
01153   }
01154   return result;
01155 }
01156 
01157 /**
01158  * Send a raw ARP packet (opcode and all addresses can be modified)
01159  *
01160  * @param netif the lwip network interface on which to send the ARP packet
01161  * @param ethsrc_addr the source MAC address for the ethernet header
01162  * @param ethdst_addr the destination MAC address for the ethernet header
01163  * @param hwsrc_addr the source MAC address for the ARP protocol header
01164  * @param ipsrc_addr the source IP address for the ARP protocol header
01165  * @param hwdst_addr the destination MAC address for the ARP protocol header
01166  * @param ipdst_addr the destination IP address for the ARP protocol header
01167  * @param opcode the type of the ARP packet
01168  * @return ERR_OK if the ARP packet has been sent
01169  *         ERR_MEM if the ARP packet couldn't be allocated
01170  *         any other err_t on failure
01171  */
01172 #if !LWIP_AUTOIP
01173 static
01174 #endif /* LWIP_AUTOIP */
01175 err_t
01176 etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
01177            const struct eth_addr *ethdst_addr,
01178            const struct eth_addr *hwsrc_addr, const ip_addr_t *ipsrc_addr,
01179            const struct eth_addr *hwdst_addr, const ip_addr_t *ipdst_addr,
01180            const u16_t opcode)
01181 {
01182   struct pbuf *p;
01183   err_t result = ERR_OK;
01184   struct eth_hdr *ethhdr;
01185   struct etharp_hdr *hdr;
01186 #if LWIP_AUTOIP
01187   const u8_t * ethdst_hwaddr;
01188 #endif /* LWIP_AUTOIP */
01189 
01190   LWIP_ASSERT("netif != NULL", netif != NULL);
01191 
01192   /* allocate a pbuf for the outgoing ARP request packet */
01193   p = pbuf_alloc(PBUF_RAW, SIZEOF_ETHARP_PACKET, PBUF_RAM);
01194   /* could allocate a pbuf for an ARP request? */
01195   if (p == NULL) {
01196     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
01197       ("etharp_raw: could not allocate pbuf for ARP request.\n"));
01198     ETHARP_STATS_INC(etharp.memerr);
01199     return ERR_MEM;
01200   }
01201   LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
01202               (p->len >= SIZEOF_ETHARP_PACKET));
01203 
01204   ethhdr = (struct eth_hdr *)p->payload;
01205   hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
01206   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n"));
01207   hdr->opcode = htons(opcode);
01208 
01209   LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
01210               (netif->hwaddr_len == ETHARP_HWADDR_LEN));
01211 #if LWIP_AUTOIP
01212   /* If we are using Link-Local, all ARP packets that contain a Link-Local
01213    * 'sender IP address' MUST be sent using link-layer broadcast instead of
01214    * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
01215   ethdst_hwaddr = ip_addr_islinklocal(ipsrc_addr) ? (u8_t*)(ethbroadcast.addr) : ethdst_addr->addr;
01216 #endif /* LWIP_AUTOIP */
01217   /* Write the ARP MAC-Addresses */
01218   ETHADDR16_COPY(&hdr->shwaddr, hwsrc_addr);
01219   ETHADDR16_COPY(&hdr->dhwaddr, hwdst_addr);
01220   /* Write the Ethernet MAC-Addresses */
01221 #if LWIP_AUTOIP
01222   ETHADDR16_COPY(&ethhdr->dest, ethdst_hwaddr);
01223 #else  /* LWIP_AUTOIP */
01224   ETHADDR16_COPY(&ethhdr->dest, ethdst_addr);
01225 #endif /* LWIP_AUTOIP */
01226   ETHADDR16_COPY(&ethhdr->src, ethsrc_addr);
01227   /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
01228    * structure packing. */ 
01229   IPADDR2_COPY(&hdr->sipaddr, ipsrc_addr);
01230   IPADDR2_COPY(&hdr->dipaddr, ipdst_addr);
01231 
01232   hdr->hwtype = PP_HTONS(HWTYPE_ETHERNET);
01233   hdr->proto = PP_HTONS(ETHTYPE_IP);
01234   /* set hwlen and protolen */
01235   hdr->hwlen = ETHARP_HWADDR_LEN;
01236   hdr->protolen = sizeof(ip_addr_t);
01237 
01238   ethhdr->type = PP_HTONS(ETHTYPE_ARP);
01239   /* send ARP query */
01240   result = netif->linkoutput(netif, p);
01241   ETHARP_STATS_INC(etharp.xmit);
01242   /* free ARP query packet */
01243   pbuf_free(p);
01244   p = NULL;
01245   /* could not allocate pbuf for ARP request */
01246 
01247   return result;
01248 }
01249 
01250 /**
01251  * Send an ARP request packet asking for ipaddr.
01252  *
01253  * @param netif the lwip network interface on which to send the request
01254  * @param ipaddr the IP address for which to ask
01255  * @return ERR_OK if the request has been sent
01256  *         ERR_MEM if the ARP packet couldn't be allocated
01257  *         any other err_t on failure
01258  */
01259 err_t
01260 etharp_request(struct netif *netif, ip_addr_t *ipaddr)
01261 {
01262   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
01263   return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, &ethbroadcast,
01264                     (struct eth_addr *)netif->hwaddr, &netif->ip_addr, &ethzero,
01265                     ipaddr, ARP_REQUEST);
01266 }
01267 #endif /* LWIP_ARP */
01268 
01269 /**
01270  * Process received ethernet frames. Using this function instead of directly
01271  * calling ip_input and passing ARP frames through etharp in ethernetif_input,
01272  * the ARP cache is protected from concurrent access.
01273  *
01274  * @param p the recevied packet, p->payload pointing to the ethernet header
01275  * @param netif the network interface on which the packet was received
01276  */
01277 err_t
01278 ethernet_input(struct pbuf *p, struct netif *netif)
01279 {
01280   struct eth_hdr* ethhdr;
01281   u16_t type;
01282 #if LWIP_ARP || ETHARP_SUPPORT_VLAN
01283   s16_t ip_hdr_offset = SIZEOF_ETH_HDR;
01284 #endif /* LWIP_ARP || ETHARP_SUPPORT_VLAN */
01285 
01286   if (p->len <= SIZEOF_ETH_HDR) {
01287     /* a packet with only an ethernet header (or less) is not valid for us */
01288     ETHARP_STATS_INC(etharp.proterr);
01289     ETHARP_STATS_INC(etharp.drop);
01290     goto free_and_return;
01291   }
01292 
01293   /* points to packet payload, which starts with an Ethernet header */
01294   ethhdr = (struct eth_hdr *)p->payload;
01295   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
01296     ("ethernet_input: dest:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", src:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", type:%"X16_F"\n",
01297      (unsigned)ethhdr->dest.addr[0], (unsigned)ethhdr->dest.addr[1], (unsigned)ethhdr->dest.addr[2],
01298      (unsigned)ethhdr->dest.addr[3], (unsigned)ethhdr->dest.addr[4], (unsigned)ethhdr->dest.addr[5],
01299      (unsigned)ethhdr->src.addr[0], (unsigned)ethhdr->src.addr[1], (unsigned)ethhdr->src.addr[2],
01300      (unsigned)ethhdr->src.addr[3], (unsigned)ethhdr->src.addr[4], (unsigned)ethhdr->src.addr[5],
01301      (unsigned)htons(ethhdr->type)));
01302 
01303   type = ethhdr->type;
01304  
01305 #if ETHARP_SUPPORT_VLAN
01306   if (type == PP_HTONS(ETHTYPE_VLAN)) {
01307     struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr*)(((char*)ethhdr) + SIZEOF_ETH_HDR);
01308     if (p->len <= SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) {
01309       /* a packet with only an ethernet/vlan header (or less) is not valid for us */
01310       ETHARP_STATS_INC(etharp.proterr);
01311       ETHARP_STATS_INC(etharp.drop);
01312       goto free_and_return;
01313     }
01314 #if defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) /* if not, allow all VLANs */
01315 #ifdef ETHARP_VLAN_CHECK_FN
01316     if (!ETHARP_VLAN_CHECK_FN(ethhdr, vlan)) {
01317 #elif defined(ETHARP_VLAN_CHECK)
01318     if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) {
01319 #endif
01320       /* silently ignore this packet: not for our VLAN */
01321       pbuf_free(p);
01322       return ERR_OK;
01323     }
01324 #endif /* defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) */
01325     type = vlan->tpid;
01326     ip_hdr_offset = SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR;
01327   }
01328 #endif /* ETHARP_SUPPORT_VLAN */
01329 
01330 #if LWIP_ARP_FILTER_NETIF
01331   netif = LWIP_ARP_FILTER_NETIF_FN(p, netif, htons(type));
01332 #endif /* LWIP_ARP_FILTER_NETIF*/
01333 
01334   if (ethhdr->dest.addr[0] & 1) {
01335     /* this might be a multicast or broadcast packet */
01336     if (ethhdr->dest.addr[0] == LL_MULTICAST_ADDR_0) {
01337       if ((ethhdr->dest.addr[1] == LL_MULTICAST_ADDR_1) &&
01338           (ethhdr->dest.addr[2] == LL_MULTICAST_ADDR_2)) {
01339         /* mark the pbuf as link-layer multicast */
01340         p->flags |= PBUF_FLAG_LLMCAST;
01341       }
01342     } else if (eth_addr_cmp(&ethhdr->dest, &ethbroadcast)) {
01343       /* mark the pbuf as link-layer broadcast */
01344       p->flags |= PBUF_FLAG_LLBCAST;
01345     }
01346   }
01347 
01348   switch (type) {
01349 #if LWIP_GOOSE //by Pellini
01350     /* IEC 61850 fixed GOOSE reception - described in GOOSE.C */
01351     case PP_HTONS(ETHTYPE_GOOSE):
01352         gooseRcv_input(netif, p);
01353         break;
01354 #endif
01355 
01356 #if LWIP_ARP
01357     /* IP packet? */
01358     case PP_HTONS(ETHTYPE_IP):
01359       if (!(netif->flags & NETIF_FLAG_ETHARP)) {
01360         goto free_and_return;
01361       }
01362 #if ETHARP_TRUST_IP_MAC
01363       /* update ARP table */
01364       etharp_ip_input(netif, p);
01365 #endif /* ETHARP_TRUST_IP_MAC */
01366       /* skip Ethernet header */
01367       if(pbuf_header(p, -ip_hdr_offset)) {
01368         LWIP_ASSERT("Can't move over header in packet", 0);
01369         goto free_and_return;
01370       } else {
01371         /* pass to IP layer */
01372         ip_input(p, netif);
01373       }
01374       break;
01375       
01376     case PP_HTONS(ETHTYPE_ARP):
01377       if (!(netif->flags & NETIF_FLAG_ETHARP)) {
01378         goto free_and_return;
01379       }
01380       /* pass p to ARP module */
01381       etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p);
01382       break;
01383 #endif /* LWIP_ARP */
01384 #if PPPOE_SUPPORT
01385     case PP_HTONS(ETHTYPE_PPPOEDISC): /* PPP Over Ethernet Discovery Stage */
01386       pppoe_disc_input(netif, p);
01387       break;
01388 
01389     case PP_HTONS(ETHTYPE_PPPOE): /* PPP Over Ethernet Session Stage */
01390       pppoe_data_input(netif, p);
01391       break;
01392 #endif /* PPPOE_SUPPORT */
01393 
01394     default:
01395       ETHARP_STATS_INC(etharp.proterr);
01396       ETHARP_STATS_INC(etharp.drop);
01397       goto free_and_return;
01398   }
01399 
01400   /* This means the pbuf is freed or consumed,
01401      so the caller doesn't have to free it again */
01402   return ERR_OK;
01403 
01404 free_and_return:
01405   pbuf_free(p);
01406   return ERR_OK;
01407 }
01408 #endif /* LWIP_ARP || LWIP_ETHERNET */