A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

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