Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/lwip/netif/etharp.c need to be fixed.

0001 /**
0002  * @file
0003  * Address Resolution Protocol module for IP over Ethernet
0004  *
0005  * Functionally, ARP is divided into two parts. The first maps an IP address
0006  * to a physical address when sending a packet, and the second part answers
0007  * requests from other machines for our physical address.
0008  *
0009  * This implementation complies with RFC 826 (Ethernet ARP). It supports
0010  * Gratuitious ARP from RFC3220 (IP Mobility Support for IPv4) section 4.6
0011  * if an interface calls etharp_gratuitous(our_netif) upon address change.
0012  */
0013 
0014 /*
0015  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
0016  * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv>
0017  * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
0018  * All rights reserved.
0019  *
0020  * Redistribution and use in source and binary forms, with or without modification,
0021  * are permitted provided that the following conditions are met:
0022  *
0023  * 1. Redistributions of source code must retain the above copyright notice,
0024  *    this list of conditions and the following disclaimer.
0025  * 2. Redistributions in binary form must reproduce the above copyright notice,
0026  *    this list of conditions and the following disclaimer in the documentation
0027  *    and/or other materials provided with the distribution.
0028  * 3. The name of the author may not be used to endorse or promote products
0029  *    derived from this software without specific prior written permission.
0030  *
0031  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
0032  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0033  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
0034  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0035  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
0036  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0037  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0038  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
0039  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
0040  * OF SUCH DAMAGE.
0041  *
0042  * This file is part of the lwIP TCP/IP stack.
0043  *
0044  */
0045  
0046 #include "lwip/opt.h"
0047 
0048 #if LWIP_ARP /* don't build if not configured for use in lwipopts.h */
0049 
0050 #include "lwip/inet.h"
0051 #include "lwip/ip.h"
0052 #include "lwip/stats.h"
0053 #include "lwip/snmp.h"
0054 #include "lwip/dhcp.h"
0055 #include "lwip/autoip.h"
0056 #include "netif/etharp.h"
0057 
0058 #if PPPOE_SUPPORT
0059 #include "netif/ppp_oe.h"
0060 #endif /* PPPOE_SUPPORT */
0061 
0062 #include <string.h>
0063 
0064 /** the time an ARP entry stays valid after its last update,
0065  *  for ARP_TMR_INTERVAL = 5000, this is
0066  *  (240 * 5) seconds = 20 minutes.
0067  */
0068 #define ARP_MAXAGE 240
0069 /** the time an ARP entry stays pending after first request,
0070  *  for ARP_TMR_INTERVAL = 5000, this is
0071  *  (2 * 5) seconds = 10 seconds.
0072  * 
0073  *  @internal Keep this number at least 2, otherwise it might
0074  *  run out instantly if the timeout occurs directly after a request.
0075  */
0076 #define ARP_MAXPENDING 2
0077 
0078 #define HWTYPE_ETHERNET 1
0079 
0080 #define ARPH_HWLEN(hdr) (ntohs((hdr)->_hwlen_protolen) >> 8)
0081 #define ARPH_PROTOLEN(hdr) (ntohs((hdr)->_hwlen_protolen) & 0xff)
0082 
0083 #define ARPH_HWLEN_SET(hdr, len) (hdr)->_hwlen_protolen = htons(ARPH_PROTOLEN(hdr) | ((len) << 8))
0084 #define ARPH_PROTOLEN_SET(hdr, len) (hdr)->_hwlen_protolen = htons((len) | (ARPH_HWLEN(hdr) << 8))
0085 
0086 enum etharp_state {
0087   ETHARP_STATE_EMPTY = 0,
0088   ETHARP_STATE_PENDING,
0089   ETHARP_STATE_STABLE
0090 };
0091 
0092 struct etharp_entry {
0093 #if ARP_QUEUEING
0094   /** 
0095    * Pointer to queue of pending outgoing packets on this ARP entry.
0096    */
0097   struct etharp_q_entry *q;
0098 #endif
0099   struct ip_addr ipaddr;
0100   struct eth_addr ethaddr;
0101   enum etharp_state state;
0102   u8_t ctime;
0103   struct netif *netif;
0104 };
0105 
0106 const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
0107 const struct eth_addr ethzero = {{0,0,0,0,0,0}};
0108 static struct etharp_entry arp_table[ARP_TABLE_SIZE];
0109 #if !LWIP_NETIF_HWADDRHINT
0110 static u8_t etharp_cached_entry;
0111 #endif
0112 
0113 /**
0114  * Try hard to create a new entry - we want the IP address to appear in
0115  * the cache (even if this means removing an active entry or so). */
0116 #define ETHARP_TRY_HARD 1
0117 #define ETHARP_FIND_ONLY  2
0118 
0119 #if LWIP_NETIF_HWADDRHINT
0120 #define NETIF_SET_HINT(netif, hint)  if (((netif) != NULL) && ((netif)->addr_hint != NULL))  \
0121                                       *((netif)->addr_hint) = (hint);
0122 static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags, struct netif *netif);
0123 #else /* LWIP_NETIF_HWADDRHINT */
0124 static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags);
0125 #endif /* LWIP_NETIF_HWADDRHINT */
0126 
0127 static err_t update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags);
0128 
0129 
0130 /* Some checks, instead of etharp_init(): */
0131 #if (LWIP_ARP && (ARP_TABLE_SIZE > 0x7f))
0132   #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"
0133 #endif
0134 
0135 
0136 #if ARP_QUEUEING
0137 /**
0138  * Free a complete queue of etharp entries
0139  *
0140  * @param q a qeueue of etharp_q_entry's to free
0141  */
0142 static void
0143 free_etharp_q(struct etharp_q_entry *q)
0144 {
0145   struct etharp_q_entry *r;
0146   LWIP_ASSERT("q != NULL", q != NULL);
0147   LWIP_ASSERT("q->p != NULL", q->p != NULL);
0148   while (q) {
0149     r = q;
0150     q = q->next;
0151     LWIP_ASSERT("r->p != NULL", (r->p != NULL));
0152     pbuf_free(r->p);
0153     memp_free(MEMP_ARP_QUEUE, r);
0154   }
0155 }
0156 #endif
0157 
0158 /**
0159  * Clears expired entries in the ARP table.
0160  *
0161  * This function should be called every ETHARP_TMR_INTERVAL microseconds (5 seconds),
0162  * in order to expire entries in the ARP table.
0163  */
0164 void
0165 etharp_tmr(void)
0166 {
0167   u8_t i;
0168 
0169   LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
0170   /* remove expired entries from the ARP table */
0171   for (i = 0; i < ARP_TABLE_SIZE; ++i) {
0172     arp_table[i].ctime++;
0173     if (((arp_table[i].state == ETHARP_STATE_STABLE) &&
0174          (arp_table[i].ctime >= ARP_MAXAGE)) ||
0175         ((arp_table[i].state == ETHARP_STATE_PENDING)  &&
0176          (arp_table[i].ctime >= ARP_MAXPENDING))) {
0177          /* pending or stable entry has become old! */
0178       LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %"U16_F".\n",
0179            arp_table[i].state == ETHARP_STATE_STABLE ? "stable" : "pending", (u16_t)i));
0180       /* clean up entries that have just been expired */
0181       /* remove from SNMP ARP index tree */
0182       snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr);
0183 #if ARP_QUEUEING
0184       /* and empty packet queue */
0185       if (arp_table[i].q != NULL) {
0186         /* remove all queued packets */
0187         LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q)));
0188         free_etharp_q(arp_table[i].q);
0189         arp_table[i].q = NULL;
0190       }
0191 #endif
0192       /* recycle entry for re-use */      
0193       arp_table[i].state = ETHARP_STATE_EMPTY;
0194     }
0195 #if ARP_QUEUEING
0196     /* still pending entry? (not expired) */
0197     if (arp_table[i].state == ETHARP_STATE_PENDING) {
0198         /* resend an ARP query here? */
0199     }
0200 #endif
0201   }
0202 }
0203 
0204 /**
0205  * Search the ARP table for a matching or new entry.
0206  * 
0207  * If an IP address is given, return a pending or stable ARP entry that matches
0208  * the address. If no match is found, create a new entry with this address set,
0209  * but in state ETHARP_EMPTY. The caller must check and possibly change the
0210  * state of the returned entry.
0211  * 
0212  * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
0213  * 
0214  * In all cases, attempt to create new entries from an empty entry. If no
0215  * empty entries are available and ETHARP_TRY_HARD flag is set, recycle
0216  * old entries. Heuristic choose the least important entry for recycling.
0217  *
0218  * @param ipaddr IP address to find in ARP cache, or to add if not found.
0219  * @param flags
0220  * - ETHARP_TRY_HARD: Try hard to create a entry by allowing recycling of
0221  * active (stable or pending) entries.
0222  *  
0223  * @return The ARP entry index that matched or is created, ERR_MEM if no
0224  * entry is found or could be recycled.
0225  */
0226 static s8_t
0227 #if LWIP_NETIF_HWADDRHINT
0228 find_entry(struct ip_addr *ipaddr, u8_t flags, struct netif *netif)
0229 #else /* LWIP_NETIF_HWADDRHINT */
0230 find_entry(struct ip_addr *ipaddr, u8_t flags)
0231 #endif /* LWIP_NETIF_HWADDRHINT */
0232 {
0233   s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
0234   s8_t empty = ARP_TABLE_SIZE;
0235   u8_t i = 0, age_pending = 0, age_stable = 0;
0236 #if ARP_QUEUEING
0237   /* oldest entry with packets on queue */
0238   s8_t old_queue = ARP_TABLE_SIZE;
0239   /* its age */
0240   u8_t age_queue = 0;
0241 #endif
0242 
0243   /* First, test if the last call to this function asked for the
0244    * same address. If so, we're really fast! */
0245   if (ipaddr) {
0246     /* ipaddr to search for was given */
0247 #if LWIP_NETIF_HWADDRHINT
0248     if ((netif != NULL) && (netif->addr_hint != NULL)) {
0249       /* per-pcb cached entry was given */
0250       u8_t per_pcb_cache = *(netif->addr_hint);
0251       if ((per_pcb_cache < ARP_TABLE_SIZE) && arp_table[per_pcb_cache].state == ETHARP_STATE_STABLE) {
0252         /* the per-pcb-cached entry is stable */
0253         if (ip_addr_cmp(ipaddr, &arp_table[per_pcb_cache].ipaddr)) {
0254           /* per-pcb cached entry was the right one! */
0255           ETHARP_STATS_INC(etharp.cachehit);
0256           return per_pcb_cache;
0257         }
0258       }
0259     }
0260 #else /* #if LWIP_NETIF_HWADDRHINT */
0261     if (arp_table[etharp_cached_entry].state == ETHARP_STATE_STABLE) {
0262       /* the cached entry is stable */
0263       if (ip_addr_cmp(ipaddr, &arp_table[etharp_cached_entry].ipaddr)) {
0264         /* cached entry was the right one! */
0265         ETHARP_STATS_INC(etharp.cachehit);
0266         return etharp_cached_entry;
0267       }
0268     }
0269 #endif /* #if LWIP_NETIF_HWADDRHINT */
0270   }
0271 
0272   /**
0273    * a) do a search through the cache, remember candidates
0274    * b) select candidate entry
0275    * c) create new entry
0276    */
0277 
0278   /* a) in a single search sweep, do all of this
0279    * 1) remember the first empty entry (if any)
0280    * 2) remember the oldest stable entry (if any)
0281    * 3) remember the oldest pending entry without queued packets (if any)
0282    * 4) remember the oldest pending entry with queued packets (if any)
0283    * 5) search for a matching IP entry, either pending or stable
0284    *    until 5 matches, or all entries are searched for.
0285    */
0286 
0287   for (i = 0; i < ARP_TABLE_SIZE; ++i) {
0288     /* no empty entry found yet and now we do find one? */
0289     if ((empty == ARP_TABLE_SIZE) && (arp_table[i].state == ETHARP_STATE_EMPTY)) {
0290       LWIP_DEBUGF(ETHARP_DEBUG, ("find_entry: found empty entry %"U16_F"\n", (u16_t)i));
0291       /* remember first empty entry */
0292       empty = i;
0293     }
0294     /* pending entry? */
0295     else if (arp_table[i].state == ETHARP_STATE_PENDING) {
0296       /* if given, does IP address match IP address in ARP entry? */
0297       if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
0298         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: found matching pending entry %"U16_F"\n", (u16_t)i));
0299         /* found exact IP address match, simply bail out */
0300 #if LWIP_NETIF_HWADDRHINT
0301         NETIF_SET_HINT(netif, i);
0302 #else /* #if LWIP_NETIF_HWADDRHINT */
0303         etharp_cached_entry = i;
0304 #endif /* #if LWIP_NETIF_HWADDRHINT */
0305         return i;
0306 #if ARP_QUEUEING
0307       /* pending with queued packets? */
0308       } else if (arp_table[i].q != NULL) {
0309         if (arp_table[i].ctime >= age_queue) {
0310           old_queue = i;
0311           age_queue = arp_table[i].ctime;
0312         }
0313 #endif
0314       /* pending without queued packets? */
0315       } else {
0316         if (arp_table[i].ctime >= age_pending) {
0317           old_pending = i;
0318           age_pending = arp_table[i].ctime;
0319         }
0320       }        
0321     }
0322     /* stable entry? */
0323     else if (arp_table[i].state == ETHARP_STATE_STABLE) {
0324       /* if given, does IP address match IP address in ARP entry? */
0325       if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
0326         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: found matching stable entry %"U16_F"\n", (u16_t)i));
0327         /* found exact IP address match, simply bail out */
0328 #if LWIP_NETIF_HWADDRHINT
0329         NETIF_SET_HINT(netif, i);
0330 #else /* #if LWIP_NETIF_HWADDRHINT */
0331         etharp_cached_entry = i;
0332 #endif /* #if LWIP_NETIF_HWADDRHINT */
0333         return i;
0334       /* remember entry with oldest stable entry in oldest, its age in maxtime */
0335       } else if (arp_table[i].ctime >= age_stable) {
0336         old_stable = i;
0337         age_stable = arp_table[i].ctime;
0338       }
0339     }
0340   }
0341   /* { we have no match } => try to create a new entry */
0342    
0343   /* no empty entry found and not allowed to recycle? */
0344   if (((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_TRY_HARD) == 0))
0345       /* or don't create new entry, only search? */
0346       || ((flags & ETHARP_FIND_ONLY) != 0)) {
0347     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: no empty entry found and not allowed to recycle\n"));
0348     return (s8_t)ERR_MEM;
0349   }
0350   
0351   /* b) choose the least destructive entry to recycle:
0352    * 1) empty entry
0353    * 2) oldest stable entry
0354    * 3) oldest pending entry without queued packets
0355    * 4) oldest pending entry with queued packets
0356    * 
0357    * { ETHARP_TRY_HARD is set at this point }
0358    */ 
0359 
0360   /* 1) empty entry available? */
0361   if (empty < ARP_TABLE_SIZE) {
0362     i = empty;
0363     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting empty entry %"U16_F"\n", (u16_t)i));
0364   }
0365   /* 2) found recyclable stable entry? */
0366   else if (old_stable < ARP_TABLE_SIZE) {
0367     /* recycle oldest stable*/
0368     i = old_stable;
0369     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest stable entry %"U16_F"\n", (u16_t)i));
0370 #if ARP_QUEUEING
0371     /* no queued packets should exist on stable entries */
0372     LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL);
0373 #endif
0374   /* 3) found recyclable pending entry without queued packets? */
0375   } else if (old_pending < ARP_TABLE_SIZE) {
0376     /* recycle oldest pending */
0377     i = old_pending;
0378     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest pending entry %"U16_F" (without queue)\n", (u16_t)i));
0379 #if ARP_QUEUEING
0380   /* 4) found recyclable pending entry with queued packets? */
0381   } else if (old_queue < ARP_TABLE_SIZE) {
0382     /* recycle oldest pending */
0383     i = old_queue;
0384     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)));
0385     free_etharp_q(arp_table[i].q);
0386     arp_table[i].q = NULL;
0387 #endif
0388     /* no empty or recyclable entries found */
0389   } else {
0390     return (s8_t)ERR_MEM;
0391   }
0392 
0393   /* { empty or recyclable entry found } */
0394   LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
0395 
0396   if (arp_table[i].state != ETHARP_STATE_EMPTY)
0397   {
0398     snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr);
0399   }
0400   /* recycle entry (no-op for an already empty entry) */
0401   arp_table[i].state = ETHARP_STATE_EMPTY;
0402 
0403   /* IP address given? */
0404   if (ipaddr != NULL) {
0405     /* set IP address */
0406     ip_addr_set(&arp_table[i].ipaddr, ipaddr);
0407   }
0408   arp_table[i].ctime = 0;
0409 #if LWIP_NETIF_HWADDRHINT
0410   NETIF_SET_HINT(netif, i);
0411 #else /* #if LWIP_NETIF_HWADDRHINT */
0412   etharp_cached_entry = i;
0413 #endif /* #if LWIP_NETIF_HWADDRHINT */
0414   return (err_t)i;
0415 }
0416 
0417 /**
0418  * Send an IP packet on the network using netif->linkoutput
0419  * The ethernet header is filled in before sending.
0420  *
0421  * @params netif the lwIP network interface on which to send the packet
0422  * @params p the packet to send, p->payload pointing to the (uninitialized) ethernet header
0423  * @params src the source MAC address to be copied into the ethernet header
0424  * @params dst the destination MAC address to be copied into the ethernet header
0425  * @return ERR_OK if the packet was sent, any other err_t on failure
0426  */
0427 static err_t
0428 etharp_send_ip(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
0429 {
0430   struct eth_hdr *ethhdr = p->payload;
0431   u8_t k;
0432 
0433   LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
0434               (netif->hwaddr_len == ETHARP_HWADDR_LEN));
0435   k = ETHARP_HWADDR_LEN;
0436   while(k > 0) {
0437     k--;
0438     ethhdr->dest.addr[k] = dst->addr[k];
0439     ethhdr->src.addr[k]  = src->addr[k];
0440   }
0441   ethhdr->type = htons(ETHTYPE_IP);
0442   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_send_ip: sending packet %p\n", (void *)p));
0443   /* send the packet */
0444   return netif->linkoutput(netif, p);
0445 }
0446 
0447 /**
0448  * Update (or insert) a IP/MAC address pair in the ARP cache.
0449  *
0450  * If a pending entry is resolved, any queued packets will be sent
0451  * at this point.
0452  * 
0453  * @param ipaddr IP address of the inserted ARP entry.
0454  * @param ethaddr Ethernet address of the inserted ARP entry.
0455  * @param flags Defines behaviour:
0456  * - ETHARP_TRY_HARD Allows ARP to insert this as a new item. If not specified,
0457  * only existing ARP entries will be updated.
0458  *
0459  * @return
0460  * - ERR_OK Succesfully updated ARP cache.
0461  * - ERR_MEM If we could not add a new ARP entry when ETHARP_TRY_HARD was set.
0462  * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
0463  *
0464  * @see pbuf_free()
0465  */
0466 static err_t
0467 update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags)
0468 {
0469   s8_t i;
0470   u8_t k;
0471   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry()\n"));
0472   LWIP_ASSERT("netif->hwaddr_len == ETHARP_HWADDR_LEN", netif->hwaddr_len == ETHARP_HWADDR_LEN);
0473   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",
0474                                         ip4_addr1(ipaddr), ip4_addr2(ipaddr), ip4_addr3(ipaddr), ip4_addr4(ipaddr), 
0475                                         ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
0476                                         ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
0477   /* non-unicast address? */
0478   if (ip_addr_isany(ipaddr) ||
0479       ip_addr_isbroadcast(ipaddr, netif) ||
0480       ip_addr_ismulticast(ipaddr)) {
0481     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
0482     return ERR_ARG;
0483   }
0484   /* find or create ARP entry */
0485 #if LWIP_NETIF_HWADDRHINT
0486   i = find_entry(ipaddr, flags, netif);
0487 #else /* LWIP_NETIF_HWADDRHINT */
0488   i = find_entry(ipaddr, flags);
0489 #endif /* LWIP_NETIF_HWADDRHINT */
0490   /* bail out if no entry could be found */
0491   if (i < 0)
0492     return (err_t)i;
0493   
0494   /* mark it stable */
0495   arp_table[i].state = ETHARP_STATE_STABLE;
0496   /* record network interface */
0497   arp_table[i].netif = netif;
0498 
0499   /* insert in SNMP ARP index tree */
0500   snmp_insert_arpidx_tree(netif, &arp_table[i].ipaddr);
0501 
0502   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i));
0503   /* update address */
0504   k = ETHARP_HWADDR_LEN;
0505   while (k > 0) {
0506     k--;
0507     arp_table[i].ethaddr.addr[k] = ethaddr->addr[k];
0508   }
0509   /* reset time stamp */
0510   arp_table[i].ctime = 0;
0511 #if ARP_QUEUEING
0512   /* this is where we will send out queued packets! */
0513   while (arp_table[i].q != NULL) {
0514     struct pbuf *p;
0515     /* remember remainder of queue */
0516     struct etharp_q_entry *q = arp_table[i].q;
0517     /* pop first item off the queue */
0518     arp_table[i].q = q->next;
0519     /* get the packet pointer */
0520     p = q->p;
0521     /* now queue entry can be freed */
0522     memp_free(MEMP_ARP_QUEUE, q);
0523     /* send the queued IP packet */
0524     etharp_send_ip(netif, p, (struct eth_addr*)(netif->hwaddr), ethaddr);
0525     /* free the queued IP packet */
0526     pbuf_free(p);
0527   }
0528 #endif
0529   return ERR_OK;
0530 }
0531 
0532 /**
0533  * Finds (stable) ethernet/IP address pair from ARP table
0534  * using interface and IP address index.
0535  * @note the addresses in the ARP table are in network order!
0536  *
0537  * @param netif points to interface index
0538  * @param ipaddr points to the (network order) IP address index
0539  * @param eth_ret points to return pointer
0540  * @param ip_ret points to return pointer
0541  * @return table index if found, -1 otherwise
0542  */
0543 s8_t
0544 etharp_find_addr(struct netif *netif, struct ip_addr *ipaddr,
0545          struct eth_addr **eth_ret, struct ip_addr **ip_ret)
0546 {
0547   s8_t i;
0548 
0549   LWIP_UNUSED_ARG(netif);
0550 
0551 #if LWIP_NETIF_HWADDRHINT
0552   i = find_entry(ipaddr, ETHARP_FIND_ONLY, NULL);
0553 #else /* LWIP_NETIF_HWADDRHINT */
0554   i = find_entry(ipaddr, ETHARP_FIND_ONLY);
0555 #endif /* LWIP_NETIF_HWADDRHINT */
0556   if((i >= 0) && arp_table[i].state == ETHARP_STATE_STABLE) {
0557       *eth_ret = &arp_table[i].ethaddr;
0558       *ip_ret = &arp_table[i].ipaddr;
0559       return i;
0560   }
0561   return -1;
0562 }
0563 
0564 /**
0565  * Updates the ARP table using the given IP packet.
0566  *
0567  * Uses the incoming IP packet's source address to update the
0568  * ARP cache for the local network. The function does not alter
0569  * or free the packet. This function must be called before the
0570  * packet p is passed to the IP layer.
0571  *
0572  * @param netif The lwIP network interface on which the IP packet pbuf arrived.
0573  * @param p The IP packet that arrived on netif.
0574  *
0575  * @return NULL
0576  *
0577  * @see pbuf_free()
0578  */
0579 void
0580 etharp_ip_input(struct netif *netif, struct pbuf *p)
0581 {
0582   struct eth_hdr *ethhdr;
0583   struct ip_hdr *iphdr;
0584   LWIP_ERROR("netif != NULL", (netif != NULL), return;);
0585   /* Only insert an entry if the source IP address of the
0586      incoming IP packet comes from a host on the local network. */
0587   ethhdr = p->payload;
0588   iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
0589 #if ETHARP_SUPPORT_VLAN
0590   if (ethhdr->type == ETHTYPE_VLAN) {
0591     iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR);
0592   }
0593 #endif /* ETHARP_SUPPORT_VLAN */
0594 
0595   /* source is not on the local network? */
0596   if (!ip_addr_netcmp(&(iphdr->src), &(netif->ip_addr), &(netif->netmask))) {
0597     /* do nothing */
0598     return;
0599   }
0600 
0601   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n"));
0602   /* update ARP table */
0603   /* @todo We could use ETHARP_TRY_HARD if we think we are going to talk
0604    * back soon (for example, if the destination IP address is ours. */
0605   update_arp_entry(netif, &(iphdr->src), &(ethhdr->src), 0);
0606 }
0607 
0608 
0609 /**
0610  * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache  
0611  * send out queued IP packets. Updates cache with snooped address pairs.
0612  *
0613  * Should be called for incoming ARP packets. The pbuf in the argument
0614  * is freed by this function.
0615  *
0616  * @param netif The lwIP network interface on which the ARP packet pbuf arrived.
0617  * @param ethaddr Ethernet address of netif.
0618  * @param p The ARP packet that arrived on netif. Is freed by this function.
0619  *
0620  * @return NULL
0621  *
0622  * @see pbuf_free()
0623  */
0624 void
0625 etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p)
0626 {
0627   struct etharp_hdr *hdr;
0628   struct eth_hdr *ethhdr;
0629   /* these are aligned properly, whereas the ARP header fields might not be */
0630   struct ip_addr sipaddr, dipaddr;
0631   u8_t i;
0632   u8_t for_us;
0633 #if LWIP_AUTOIP
0634   const u8_t * ethdst_hwaddr;
0635 #endif /* LWIP_AUTOIP */
0636 
0637   LWIP_ERROR("netif != NULL", (netif != NULL), return;);
0638   
0639   /* drop short ARP packets: we have to check for p->len instead of p->tot_len here
0640      since a struct etharp_hdr is pointed to p->payload, so it musn't be chained! */
0641   if (p->len < SIZEOF_ETHARP_PACKET) {
0642     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
0643       ("etharp_arp_input: packet dropped, too short (%"S16_F"/%"S16_F")\n", p->tot_len,
0644       (s16_t)SIZEOF_ETHARP_PACKET));
0645     ETHARP_STATS_INC(etharp.lenerr);
0646     ETHARP_STATS_INC(etharp.drop);
0647     pbuf_free(p);
0648     return;
0649   }
0650 
0651   ethhdr = p->payload;
0652   hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
0653 #if ETHARP_SUPPORT_VLAN
0654   if (ethhdr->type == ETHTYPE_VLAN) {
0655     hdr = (struct etharp_hdr *)(((u8_t*)ethhdr) + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR);
0656   }
0657 #endif /* ETHARP_SUPPORT_VLAN */
0658 
0659   /* RFC 826 "Packet Reception": */
0660   if ((hdr->hwtype != htons(HWTYPE_ETHERNET)) ||
0661       (hdr->_hwlen_protolen != htons((ETHARP_HWADDR_LEN << 8) | sizeof(struct ip_addr))) ||
0662       (hdr->proto != htons(ETHTYPE_IP)) ||
0663       (ethhdr->type != htons(ETHTYPE_ARP)))  {
0664     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
0665       ("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",
0666       hdr->hwtype, ARPH_HWLEN(hdr), hdr->proto, ARPH_PROTOLEN(hdr), ethhdr->type));
0667     ETHARP_STATS_INC(etharp.proterr);
0668     ETHARP_STATS_INC(etharp.drop);
0669     pbuf_free(p);
0670     return;
0671   }
0672   ETHARP_STATS_INC(etharp.recv);
0673 
0674 #if LWIP_AUTOIP
0675   /* We have to check if a host already has configured our random
0676    * created link local address and continously check if there is
0677    * a host with this IP-address so we can detect collisions */
0678   autoip_arp_reply(netif, hdr);
0679 #endif /* LWIP_AUTOIP */
0680 
0681   /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
0682    * structure packing (not using structure copy which breaks strict-aliasing rules). */
0683   SMEMCPY(&sipaddr, &hdr->sipaddr, sizeof(sipaddr));
0684   SMEMCPY(&dipaddr, &hdr->dipaddr, sizeof(dipaddr));
0685 
0686   /* this interface is not configured? */
0687   if (netif->ip_addr.addr == 0) {
0688     for_us = 0;
0689   } else {
0690     /* ARP packet directed to us? */
0691     for_us = ip_addr_cmp(&dipaddr, &(netif->ip_addr));
0692   }
0693 
0694   /* ARP message directed to us? */
0695   if (for_us) {
0696     /* add IP address in ARP cache; assume requester wants to talk to us.
0697      * can result in directly sending the queued packets for this host. */
0698     update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), ETHARP_TRY_HARD);
0699   /* ARP message not directed to us? */
0700   } else {
0701     /* update the source IP address in the cache, if present */
0702     update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), 0);
0703   }
0704 
0705   /* now act on the message itself */
0706   switch (htons(hdr->opcode)) {
0707   /* ARP request? */
0708   case ARP_REQUEST:
0709     /* ARP request. If it asked for our address, we send out a
0710      * reply. In any case, we time-stamp any existing ARP entry,
0711      * and possiby send out an IP packet that was queued on it. */
0712 
0713     LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP request\n"));
0714     /* ARP request for our address? */
0715     if (for_us) {
0716 
0717       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: replying to ARP request for our IP address\n"));
0718       /* Re-use pbuf to send ARP reply.
0719          Since we are re-using an existing pbuf, we can't call etharp_raw since
0720          that would allocate a new pbuf. */
0721       hdr->opcode = htons(ARP_REPLY);
0722 
0723       hdr->dipaddr = hdr->sipaddr;
0724       SMEMCPY(&hdr->sipaddr, &netif->ip_addr, sizeof(hdr->sipaddr));
0725 
0726       LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
0727                   (netif->hwaddr_len == ETHARP_HWADDR_LEN));
0728       i = ETHARP_HWADDR_LEN;
0729 #if LWIP_AUTOIP
0730       /* If we are using Link-Local, ARP packets must be broadcast on the
0731        * link layer. (See RFC3927 Section 2.5) */
0732       ethdst_hwaddr = ((netif->autoip != NULL) && (netif->autoip->state != AUTOIP_STATE_OFF)) ? (u8_t*)(ethbroadcast.addr) : hdr->shwaddr.addr;
0733 #endif /* LWIP_AUTOIP */
0734 
0735       while(i > 0) {
0736         i--;
0737         hdr->dhwaddr.addr[i] = hdr->shwaddr.addr[i];
0738 #if LWIP_AUTOIP
0739         ethhdr->dest.addr[i] = ethdst_hwaddr[i];
0740 #else  /* LWIP_AUTOIP */
0741         ethhdr->dest.addr[i] = hdr->shwaddr.addr[i];
0742 #endif /* LWIP_AUTOIP */
0743         hdr->shwaddr.addr[i] = ethaddr->addr[i];
0744         ethhdr->src.addr[i] = ethaddr->addr[i];
0745       }
0746 
0747       /* hwtype, hwaddr_len, proto, protolen and the type in the ethernet header
0748          are already correct, we tested that before */
0749 
0750       /* return ARP reply */
0751       netif->linkoutput(netif, p);
0752     /* we are not configured? */
0753     } else if (netif->ip_addr.addr == 0) {
0754       /* { for_us == 0 and netif->ip_addr.addr == 0 } */
0755       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n"));
0756     /* request was not directed to us */
0757     } else {
0758       /* { for_us == 0 and netif->ip_addr.addr != 0 } */
0759       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP request was not for us.\n"));
0760     }
0761     break;
0762   case ARP_REPLY:
0763     /* ARP reply. We already updated the ARP cache earlier. */
0764     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP reply\n"));
0765 #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
0766     /* DHCP wants to know about ARP replies from any host with an
0767      * IP address also offered to us by the DHCP server. We do not
0768      * want to take a duplicate IP address on a single network.
0769      * @todo How should we handle redundant (fail-over) interfaces? */
0770     dhcp_arp_reply(netif, &sipaddr);
0771 #endif
0772     break;
0773   default:
0774     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP unknown opcode type %"S16_F"\n", htons(hdr->opcode)));
0775     ETHARP_STATS_INC(etharp.err);
0776     break;
0777   }
0778   /* free ARP packet */
0779   pbuf_free(p);
0780 }
0781 
0782 /**
0783  * Resolve and fill-in Ethernet address header for outgoing IP packet.
0784  *
0785  * For IP multicast and broadcast, corresponding Ethernet addresses
0786  * are selected and the packet is transmitted on the link.
0787  *
0788  * For unicast addresses, the packet is submitted to etharp_query(). In
0789  * case the IP address is outside the local network, the IP address of
0790  * the gateway is used.
0791  *
0792  * @param netif The lwIP network interface which the IP packet will be sent on.
0793  * @param q The pbuf(s) containing the IP packet to be sent.
0794  * @param ipaddr The IP address of the packet destination.
0795  *
0796  * @return
0797  * - ERR_RTE No route to destination (no gateway to external networks),
0798  * or the return type of either etharp_query() or etharp_send_ip().
0799  */
0800 err_t
0801 etharp_output(struct netif *netif, struct pbuf *q, struct ip_addr *ipaddr)
0802 {
0803   struct eth_addr *dest, mcastaddr;
0804 
0805   /* make room for Ethernet header - should not fail */
0806   if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
0807     /* bail out */
0808     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
0809       ("etharp_output: could not allocate room for header.\n"));
0810     LINK_STATS_INC(link.lenerr);
0811     return ERR_BUF;
0812   }
0813 
0814   /* assume unresolved Ethernet address */
0815   dest = NULL;
0816   /* Determine on destination hardware address. Broadcasts and multicasts
0817    * are special, other IP addresses are looked up in the ARP table. */
0818 
0819   /* broadcast destination IP address? */
0820   if (ip_addr_isbroadcast(ipaddr, netif)) {
0821     /* broadcast on Ethernet also */
0822     dest = (struct eth_addr *)&ethbroadcast;
0823   /* multicast destination IP address? */
0824   } else if (ip_addr_ismulticast(ipaddr)) {
0825     /* Hash IP multicast address to MAC address.*/
0826     mcastaddr.addr[0] = 0x01;
0827     mcastaddr.addr[1] = 0x00;
0828     mcastaddr.addr[2] = 0x5e;
0829     mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
0830     mcastaddr.addr[4] = ip4_addr3(ipaddr);
0831     mcastaddr.addr[5] = ip4_addr4(ipaddr);
0832     /* destination Ethernet address is multicast */
0833     dest = &mcastaddr;
0834   /* unicast destination IP address? */
0835   } else {
0836     /* outside local network? */
0837     if (!ip_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask))) {
0838       /* interface has default gateway? */
0839       if (netif->gw.addr != 0) {
0840         /* send to hardware address of default gateway IP address */
0841         ipaddr = &(netif->gw);
0842       /* no default gateway available */
0843       } else {
0844         /* no route to destination error (default gateway missing) */
0845         return ERR_RTE;
0846       }
0847     }
0848     /* queue on destination Ethernet address belonging to ipaddr */
0849     return etharp_query(netif, ipaddr, q);
0850   }
0851 
0852   /* continuation for multicast/broadcast destinations */
0853   /* obtain source Ethernet address of the given interface */
0854   /* send packet directly on the link */
0855   return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr), dest);
0856 }
0857 
0858 /**
0859  * Send an ARP request for the given IP address and/or queue a packet.
0860  *
0861  * If the IP address was not yet in the cache, a pending ARP cache entry
0862  * is added and an ARP request is sent for the given address. The packet
0863  * is queued on this entry.
0864  *
0865  * If the IP address was already pending in the cache, a new ARP request
0866  * is sent for the given address. The packet is queued on this entry.
0867  *
0868  * If the IP address was already stable in the cache, and a packet is
0869  * given, it is directly sent and no ARP request is sent out. 
0870  * 
0871  * If the IP address was already stable in the cache, and no packet is
0872  * given, an ARP request is sent out.
0873  * 
0874  * @param netif The lwIP network interface on which ipaddr
0875  * must be queried for.
0876  * @param ipaddr The IP address to be resolved.
0877  * @param q If non-NULL, a pbuf that must be delivered to the IP address.
0878  * q is not freed by this function.
0879  *
0880  * @note q must only be ONE packet, not a packet queue!
0881  *
0882  * @return
0883  * - ERR_BUF Could not make room for Ethernet header.
0884  * - ERR_MEM Hardware address unknown, and no more ARP entries available
0885  *   to query for address or queue the packet.
0886  * - ERR_MEM Could not queue packet due to memory shortage.
0887  * - ERR_RTE No route to destination (no gateway to external networks).
0888  * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
0889  *
0890  */
0891 err_t
0892 etharp_query(struct netif *netif, struct ip_addr *ipaddr, struct pbuf *q)
0893 {
0894   struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
0895   err_t result = ERR_MEM;
0896   s8_t i; /* ARP entry index */
0897 
0898   /* non-unicast address? */
0899   if (ip_addr_isbroadcast(ipaddr, netif) ||
0900       ip_addr_ismulticast(ipaddr) ||
0901       ip_addr_isany(ipaddr)) {
0902     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
0903     return ERR_ARG;
0904   }
0905 
0906   /* find entry in ARP cache, ask to create entry if queueing packet */
0907 #if LWIP_NETIF_HWADDRHINT
0908   i = find_entry(ipaddr, ETHARP_TRY_HARD, netif);
0909 #else /* LWIP_NETIF_HWADDRHINT */
0910   i = find_entry(ipaddr, ETHARP_TRY_HARD);
0911 #endif /* LWIP_NETIF_HWADDRHINT */
0912 
0913   /* could not find or create entry? */
0914   if (i < 0) {
0915     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
0916     if (q) {
0917       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n"));
0918       ETHARP_STATS_INC(etharp.memerr);
0919     }
0920     return (err_t)i;
0921   }
0922 
0923   /* mark a fresh entry as pending (we just sent a request) */
0924   if (arp_table[i].state == ETHARP_STATE_EMPTY) {
0925     arp_table[i].state = ETHARP_STATE_PENDING;
0926   }
0927 
0928   /* { i is either a STABLE or (new or existing) PENDING entry } */
0929   LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
0930   ((arp_table[i].state == ETHARP_STATE_PENDING) ||
0931    (arp_table[i].state == ETHARP_STATE_STABLE)));
0932 
0933   /* do we have a pending entry? or an implicit query request? */
0934   if ((arp_table[i].state == ETHARP_STATE_PENDING) || (q == NULL)) {
0935     /* try to resolve it; send out ARP request */
0936     result = etharp_request(netif, ipaddr);
0937     if (result != ERR_OK) {
0938       /* ARP request couldn't be sent */
0939       /* We don't re-send arp request in etharp_tmr, but we still queue packets,
0940          since this failure could be temporary, and the next packet calling
0941          etharp_query again could lead to sending the queued packets. */
0942     }
0943   }
0944   
0945   /* packet given? */
0946   if (q != NULL) {
0947     /* stable entry? */
0948     if (arp_table[i].state == ETHARP_STATE_STABLE) {
0949       /* we have a valid IP->Ethernet address mapping */
0950       /* send the packet */
0951       result = etharp_send_ip(netif, q, srcaddr, &(arp_table[i].ethaddr));
0952     /* pending entry? (either just created or already pending */
0953     } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
0954 #if ARP_QUEUEING /* queue the given q packet */
0955       struct pbuf *p;
0956       int copy_needed = 0;
0957       /* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but
0958        * to copy the whole queue into a new PBUF_RAM (see bug #11400) 
0959        * PBUF_ROMs can be left as they are, since ROM must not get changed. */
0960       p = q;
0961       while (p) {
0962         LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0));
0963         if(p->type != PBUF_ROM) {
0964           copy_needed = 1;
0965           break;
0966         }
0967         p = p->next;
0968       }
0969       if(copy_needed) {
0970         /* copy the whole packet into new pbufs */
0971         p = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
0972         if(p != NULL) {
0973           if (pbuf_copy(p, q) != ERR_OK) {
0974             pbuf_free(p);
0975             p = NULL;
0976           }
0977         }
0978       } else {
0979         /* referencing the old pbuf is enough */
0980         p = q;
0981         pbuf_ref(p);
0982       }
0983       /* packet could be taken over? */
0984       if (p != NULL) {
0985         /* queue packet ... */
0986         struct etharp_q_entry *new_entry;
0987         /* allocate a new arp queue entry */
0988         new_entry = memp_malloc(MEMP_ARP_QUEUE);
0989         if (new_entry != NULL) {
0990           new_entry->next = 0;
0991           new_entry->p = p;
0992           if(arp_table[i].q != NULL) {
0993             /* queue was already existent, append the new entry to the end */
0994             struct etharp_q_entry *r;
0995             r = arp_table[i].q;
0996             while (r->next != NULL) {
0997               r = r->next;
0998             }
0999             r->next = new_entry;
1000           } else {
1001             /* queue did not exist, first item in queue */
1002             arp_table[i].q = new_entry;
1003           }
1004           LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
1005           result = ERR_OK;
1006         } else {
1007           /* the pool MEMP_ARP_QUEUE is empty */
1008           pbuf_free(p);
1009           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));
1010           /* { result == ERR_MEM } through initialization */
1011         }
1012       } else {
1013         ETHARP_STATS_INC(etharp.memerr);
1014         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));
1015         /* { result == ERR_MEM } through initialization */
1016       }
1017 #else /* ARP_QUEUEING == 0 */
1018       /* q && state == PENDING && ARP_QUEUEING == 0 => result = ERR_MEM */
1019       /* { result == ERR_MEM } through initialization */
1020       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: Ethernet destination address unknown, queueing disabled, packet %p dropped\n", (void *)q));
1021 #endif
1022     }
1023   }
1024   return result;
1025 }
1026 
1027 /**
1028  * Send a raw ARP packet (opcode and all addresses can be modified)
1029  *
1030  * @param netif the lwip network interface on which to send the ARP packet
1031  * @param ethsrc_addr the source MAC address for the ethernet header
1032  * @param ethdst_addr the destination MAC address for the ethernet header
1033  * @param hwsrc_addr the source MAC address for the ARP protocol header
1034  * @param ipsrc_addr the source IP address for the ARP protocol header
1035  * @param hwdst_addr the destination MAC address for the ARP protocol header
1036  * @param ipdst_addr the destination IP address for the ARP protocol header
1037  * @param opcode the type of the ARP packet
1038  * @return ERR_OK if the ARP packet has been sent
1039  *         ERR_MEM if the ARP packet couldn't be allocated
1040  *         any other err_t on failure
1041  */
1042 #if !LWIP_AUTOIP
1043 static
1044 #endif /* LWIP_AUTOIP */
1045 err_t
1046 etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
1047            const struct eth_addr *ethdst_addr,
1048            const struct eth_addr *hwsrc_addr, const struct ip_addr *ipsrc_addr,
1049            const struct eth_addr *hwdst_addr, const struct ip_addr *ipdst_addr,
1050            const u16_t opcode)
1051 {
1052   struct pbuf *p;
1053   err_t result = ERR_OK;
1054   u8_t k; /* ARP entry index */
1055   struct eth_hdr *ethhdr;
1056   struct etharp_hdr *hdr;
1057 #if LWIP_AUTOIP
1058   const u8_t * ethdst_hwaddr;
1059 #endif /* LWIP_AUTOIP */
1060 
1061   /* allocate a pbuf for the outgoing ARP request packet */
1062   p = pbuf_alloc(PBUF_RAW, SIZEOF_ETHARP_PACKET, PBUF_RAM);
1063   /* could allocate a pbuf for an ARP request? */
1064   if (p == NULL) {
1065     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
1066       ("etharp_raw: could not allocate pbuf for ARP request.\n"));
1067     ETHARP_STATS_INC(etharp.memerr);
1068     return ERR_MEM;
1069   }
1070   LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
1071               (p->len >= SIZEOF_ETHARP_PACKET));
1072 
1073   ethhdr = p->payload;
1074   hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
1075   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n"));
1076   hdr->opcode = htons(opcode);
1077 
1078   LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
1079               (netif->hwaddr_len == ETHARP_HWADDR_LEN));
1080   k = ETHARP_HWADDR_LEN;
1081 #if LWIP_AUTOIP
1082   /* If we are using Link-Local, ARP packets must be broadcast on the
1083    * link layer. (See RFC3927 Section 2.5) */
1084   ethdst_hwaddr = ((netif->autoip != NULL) && (netif->autoip->state != AUTOIP_STATE_OFF)) ? (u8_t*)(ethbroadcast.addr) : ethdst_addr->addr;
1085 #endif /* LWIP_AUTOIP */
1086   /* Write MAC-Addresses (combined loop for both headers) */
1087   while(k > 0) {
1088     k--;
1089     /* Write the ARP MAC-Addresses */
1090     hdr->shwaddr.addr[k] = hwsrc_addr->addr[k];
1091     hdr->dhwaddr.addr[k] = hwdst_addr->addr[k];
1092     /* Write the Ethernet MAC-Addresses */
1093 #if LWIP_AUTOIP
1094     ethhdr->dest.addr[k] = ethdst_hwaddr[k];
1095 #else  /* LWIP_AUTOIP */
1096     ethhdr->dest.addr[k] = ethdst_addr->addr[k];
1097 #endif /* LWIP_AUTOIP */
1098     ethhdr->src.addr[k]  = ethsrc_addr->addr[k];
1099   }
1100   hdr->sipaddr = *(struct ip_addr2 *)ipsrc_addr;
1101   hdr->dipaddr = *(struct ip_addr2 *)ipdst_addr;
1102 
1103   hdr->hwtype = htons(HWTYPE_ETHERNET);
1104   hdr->proto = htons(ETHTYPE_IP);
1105   /* set hwlen and protolen together */
1106   hdr->_hwlen_protolen = htons((ETHARP_HWADDR_LEN << 8) | sizeof(struct ip_addr));
1107 
1108   ethhdr->type = htons(ETHTYPE_ARP);
1109   /* send ARP query */
1110   result = netif->linkoutput(netif, p);
1111   ETHARP_STATS_INC(etharp.xmit);
1112   /* free ARP query packet */
1113   pbuf_free(p);
1114   p = NULL;
1115   /* could not allocate pbuf for ARP request */
1116 
1117   return result;
1118 }
1119 
1120 /**
1121  * Send an ARP request packet asking for ipaddr.
1122  *
1123  * @param netif the lwip network interface on which to send the request
1124  * @param ipaddr the IP address for which to ask
1125  * @return ERR_OK if the request has been sent
1126  *         ERR_MEM if the ARP packet couldn't be allocated
1127  *         any other err_t on failure
1128  */
1129 err_t
1130 etharp_request(struct netif *netif, struct ip_addr *ipaddr)
1131 {
1132   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
1133   return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, &ethbroadcast,
1134                     (struct eth_addr *)netif->hwaddr, &netif->ip_addr, &ethzero,
1135                     ipaddr, ARP_REQUEST);
1136 }
1137 
1138 /**
1139  * Process received ethernet frames. Using this function instead of directly
1140  * calling ip_input and passing ARP frames through etharp in ethernetif_input,
1141  * the ARP cache is protected from concurrent access.
1142  *
1143  * @param p the recevied packet, p->payload pointing to the ethernet header
1144  * @param netif the network interface on which the packet was received
1145  */
1146 err_t
1147 ethernet_input(struct pbuf *p, struct netif *netif)
1148 {
1149   struct eth_hdr* ethhdr;
1150   u16_t type;
1151 
1152   /* points to packet payload, which starts with an Ethernet header */
1153   ethhdr = p->payload;
1154   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
1155     ("ethernet_input: dest:%02x:%02x:%02x:%02x:%02x:%02x, src:%02x:%02x:%02x:%02x:%02x:%02x, type:%2hx\n",
1156      (unsigned)ethhdr->dest.addr[0], (unsigned)ethhdr->dest.addr[1], (unsigned)ethhdr->dest.addr[2],
1157      (unsigned)ethhdr->dest.addr[3], (unsigned)ethhdr->dest.addr[4], (unsigned)ethhdr->dest.addr[5],
1158      (unsigned)ethhdr->src.addr[0], (unsigned)ethhdr->src.addr[1], (unsigned)ethhdr->src.addr[2],
1159      (unsigned)ethhdr->src.addr[3], (unsigned)ethhdr->src.addr[4], (unsigned)ethhdr->src.addr[5],
1160      (unsigned)htons(ethhdr->type)));
1161 
1162   type = htons(ethhdr->type);
1163 #if ETHARP_SUPPORT_VLAN
1164   if (type == ETHTYPE_VLAN) {
1165     struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr*)(((char*)ethhdr) + SIZEOF_ETH_HDR);
1166 #ifdef ETHARP_VLAN_CHECK /* if not, allow all VLANs */
1167     if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) {
1168       /* silently ignore this packet: not for our VLAN */
1169       pbuf_free(p);
1170       return ERR_OK;
1171     }
1172 #endif /* ETHARP_VLAN_CHECK */
1173     type = htons(vlan->tpid);
1174   }
1175 #endif /* ETHARP_SUPPORT_VLAN */
1176 
1177   switch (type) {
1178     /* IP packet? */
1179     case ETHTYPE_IP:
1180 #if ETHARP_TRUST_IP_MAC
1181       /* update ARP table */
1182       etharp_ip_input(netif, p);
1183 #endif /* ETHARP_TRUST_IP_MAC */
1184       /* skip Ethernet header */
1185       if(pbuf_header(p, -(s16_t)SIZEOF_ETH_HDR)) {
1186         LWIP_ASSERT("Can't move over header in packet", 0);
1187         pbuf_free(p);
1188         p = NULL;
1189       } else {
1190         /* pass to IP layer */
1191         ip_input(p, netif);
1192       }
1193       break;
1194       
1195     case ETHTYPE_ARP:
1196       /* pass p to ARP module */
1197       etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p);
1198       break;
1199 
1200 #if PPPOE_SUPPORT
1201     case ETHTYPE_PPPOEDISC: /* PPP Over Ethernet Discovery Stage */
1202       pppoe_disc_input(netif, p);
1203       break;
1204 
1205     case ETHTYPE_PPPOE: /* PPP Over Ethernet Session Stage */
1206       pppoe_data_input(netif, p);
1207       break;
1208 #endif /* PPPOE_SUPPORT */
1209 
1210     default:
1211       ETHARP_STATS_INC(etharp.proterr);
1212       ETHARP_STATS_INC(etharp.drop);
1213       pbuf_free(p);
1214       p = NULL;
1215       break;
1216   }
1217 
1218   /* This means the pbuf is freed or consumed,
1219      so the caller doesn't have to free it again */
1220   return ERR_OK;
1221 }
1222 #endif /* LWIP_ARP */