• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * Address Resolution Protocol module for IP over Ethernet
4  *
5  * Functionally, ARP is divided into two parts. The first maps an IP address
6  * to a physical address when sending a packet, and the second part answers
7  * requests from other machines for our physical address.
8  *
9  * This implementation complies with RFC 826 (Ethernet ARP). It supports
10  * Gratuitious ARP from RFC3220 (IP Mobility Support for IPv4) section 4.6
11  * if an interface calls etharp_gratuitous(our_netif) upon address change.
12  */
13 
14 /*
15  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
16  * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv>
17  * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without modification,
21  * are permitted provided that the following conditions are met:
22  *
23  * 1. Redistributions of source code must retain the above copyright notice,
24  *    this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright notice,
26  *    this list of conditions and the following disclaimer in the documentation
27  *    and/or other materials provided with the distribution.
28  * 3. The name of the author may not be used to endorse or promote products
29  *    derived from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
34  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
36  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
40  * OF SUCH DAMAGE.
41  *
42  * This file is part of the lwIP TCP/IP stack.
43  *
44  */
45 
46 #include "lwip/opt.h"
47 
48 #if LWIP_IPV4 && LWIP_ARP /* don't build if not configured for use in lwipopts.h */
49 #include "lwip/sys.h"
50 #include "lwip/etharp.h"
51 #include "lwip/stats.h"
52 #include "lwip/snmp.h"
53 #include "lwip/dhcp.h"
54 #include "lwip/autoip.h"
55 #include "lwip/prot/iana.h"
56 #include "netif/ethernet.h"
57 
58 #include <string.h>
59 
60 #if LWIP_NAT64
61 #include "lwip/nat64.h"
62 #endif
63 
64 #ifdef LWIP_HOOK_FILENAME
65 #include LWIP_HOOK_FILENAME
66 #endif
67 
68 /** Re-request a used ARP entry 1 minute before it would expire to prevent
69  *  breaking a steadily used connection because the ARP entry timed out. */
70 #define ARP_AGE_REREQUEST_USED_UNICAST   (ARP_MAXAGE - 30)
71 #define ARP_AGE_REREQUEST_USED_BROADCAST (ARP_MAXAGE - 15)
72 
73 /** the time an ARP entry stays pending after first request,
74  *  for ARP_TMR_INTERVAL = 1000, this is
75  *  10 seconds.
76  *
77  *  @internal Keep this number at least 2, otherwise it might
78  *  run out instantly if the timeout occurs directly after a request.
79  */
80 #define ARP_MAXPENDING 5
81 
82 #if LWIP_ENABLE_IP_CONFLICT_SIGNAL
83 u32_t is_ip_conflict_signal = 0;
84 sys_sem_t ip_conflict_detect;
85 #endif
86 struct etharp_entry arp_table[ARP_TABLE_SIZE];
87 
88 #if ETHARP_FAST_RTE
89 #define ARP_FAST_RTE_TIME 10
90 static ip4_addr_t g_last_unresolved_dest = { IPADDR_ANY };
91 static u16_t g_fast_rte_time;
92 #endif
93 
94 #if !LWIP_NETIF_HWADDRHINT
95 static netif_addr_idx_t etharp_cached_entry;
96 #endif /* !LWIP_NETIF_HWADDRHINT */
97 
98 #if LWIP_NETIF_HWADDRHINT
99 #define ETHARP_SET_ADDRHINT(netif, addrhint)  do { if (((netif) != NULL) && ((netif)->hints != NULL)) { \
100                                               (netif)->hints->addr_hint = (addrhint); }} while(0)
101 #else /* LWIP_NETIF_HWADDRHINT */
102 #define ETHARP_SET_ADDRHINT(netif, addrhint)  (etharp_cached_entry = (addrhint))
103 #endif /* LWIP_NETIF_HWADDRHINT */
104 
105 
106 /* Check for maximum ARP_TABLE_SIZE */
107 #if (ARP_TABLE_SIZE > NETIF_ADDR_IDX_MAX)
108 #error "ARP_TABLE_SIZE must fit in an s16_t, you have to reduce it in your lwipopts.h"
109 #endif
110 
111 
112 static err_t etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr *hw_dst_addr);
113 static err_t etharp_raw(struct netif *netif,
114                         const struct eth_addr *ethsrc_addr, const struct eth_addr *ethdst_addr,
115                         const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr,
116                         const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr,
117                         const u16_t opcode);
118 
119 #if ARP_QUEUEING
120 /**
121  * Free a complete queue of etharp entries
122  *
123  * @param q a qeueue of etharp_q_entry's to free
124  */
125 static void
free_etharp_q(struct etharp_q_entry * q)126 free_etharp_q(struct etharp_q_entry *q)
127 {
128   struct etharp_q_entry *r;
129   LWIP_ASSERT("q != NULL", q != NULL);
130   while (q) {
131     r = q;
132     q = q->next;
133     LWIP_ASSERT("r->p != NULL", (r->p != NULL));
134     pbuf_free(r->p);
135     memp_free(MEMP_ARP_QUEUE, r);
136   }
137 }
138 #else /* ARP_QUEUEING */
139 
140 /** Compatibility define: free the queued pbuf */
141 #define free_etharp_q(q) pbuf_free(q)
142 
143 #endif /* ARP_QUEUEING */
144 
145 /** Clean up ARP table entries */
146 static void
etharp_free_entry(s16_t i)147 etharp_free_entry(s16_t i)
148 {
149   /* remove from SNMP ARP index tree */
150   mib2_remove_arp_entry(arp_table[i].netif, &arp_table[i].ipaddr);
151   /* and empty packet queue */
152   if (arp_table[i].q != NULL) {
153     /* remove all queued packets */
154     LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_free_entry: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q)));
155     free_etharp_q(arp_table[i].q);
156     arp_table[i].q = NULL;
157   }
158   /* recycle entry for re-use */
159   arp_table[i].state = ETHARP_STATE_EMPTY;
160 #ifdef LWIP_DEBUG
161   /* for debugging, clean out the complete entry */
162   arp_table[i].ctime = 0;
163   arp_table[i].netif = NULL;
164   ip4_addr_set_zero(&arp_table[i].ipaddr);
165   arp_table[i].ethaddr = ethzero;
166 #endif /* LWIP_DEBUG */
167 }
168 
169 #if LWIP_LOWPOWER
170 #include "lwip/lowpower.h"
171 u32_t
etharp_tmr_tick(void)172 etharp_tmr_tick(void)
173 {
174   s32_t i;
175   u32_t tick = 0;
176   u32_t time;
177 
178   for (i = 0; i < ARP_TABLE_SIZE; i++) {
179     u8_t state = arp_table[i].state;
180     if ((state != ETHARP_STATE_EMPTY)
181 #if ETHARP_SUPPORT_STATIC_ENTRIES
182         && (state != ETHARP_STATE_STATIC)
183 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
184        ) {
185       if (arp_table[i].state != ETHARP_STATE_STABLE) {
186         LOWPOWER_DEBUG(("%s tmr tick: 1\n", __func__));
187         return 1;
188       }
189       time = (u32_t)ARP_MAXAGE - arp_table[i].ctime;
190       SET_TMR_TICK(tick, time);
191     }
192   }
193   LOWPOWER_DEBUG(("%s tmr tick: %u\n", __func__, tick));
194   return tick;
195 }
196 #endif /* LWIP_LOWPOWER */
197 
198 #if LWIP_ARP_GRATUITOUS_REXMIT
199 err_t
etharp_gratuitous_start(struct netif * netif)200 etharp_gratuitous_start(struct netif *netif)
201 {
202   if (netif == NULL) {
203     return ERR_ARG;
204   }
205   if ((netif->flags & NETIF_FLAG_ETHARP) == 0) {
206     return ERR_OPNOTSUPP;
207   }
208   (void)etharp_request(netif, netif_ip4_addr(netif));
209   netif->arp_gratuitous_doing = lwIP_TRUE;
210   netif->arp_gratuitous_cnt = 1;
211   return ERR_OK;
212 }
213 
214 static void
etharp_gratuitous_retransmit(void)215 etharp_gratuitous_retransmit(void)
216 {
217   struct netif *netif;
218   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_gratuitous_retransmit()\n"));
219   /* iterate through all network interfaces */
220   NETIF_FOREACH(netif) {
221     if ((netif->flags & NETIF_FLAG_ETHARP) == 0) {
222       continue;
223     }
224     if (netif->arp_gratuitous_doing != lwIP_TRUE) {
225       continue;
226     }
227     if (netif->arp_gratuitous_cnt < ETHARP_GRATUITOUS_NUM) {
228       (void)etharp_request(netif, netif_ip4_addr(netif));
229       netif->arp_gratuitous_cnt++;
230     } else {
231       netif->arp_gratuitous_doing = lwIP_FALSE;
232       netif->arp_gratuitous_cnt = 0;
233     }
234   }
235 }
236 #endif /* LWIP_ARP_GRATUITOUS_REXMIT */
237 
238 /**
239  * Clears expired entries in the ARP table.
240  *
241  * This function should be called every ARP_TMR_INTERVAL milliseconds (1 second),
242  * in order to expire entries in the ARP table.
243  */
244 void
etharp_tmr(void)245 etharp_tmr(void)
246 {
247   s16_t i;
248 
249   LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
250 
251 #if ETHARP_FAST_RTE
252   if (g_fast_rte_time > 0) {
253     --g_fast_rte_time;
254     if (g_fast_rte_time == 0) {
255       ip4_addr_set_any(&g_last_unresolved_dest);
256     }
257   }
258 #endif
259   /* remove expired entries from the ARP table */
260   for (i = 0; i < ARP_TABLE_SIZE; ++i) {
261     u8_t state = arp_table[i].state;
262     if (state != ETHARP_STATE_EMPTY
263 #if ETHARP_SUPPORT_STATIC_ENTRIES
264         && (state != ETHARP_STATE_STATIC)
265 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
266        ) {
267       arp_table[i].ctime++;
268       if ((arp_table[i].ctime >= ARP_MAXAGE) ||
269           ((arp_table[i].state == ETHARP_STATE_PENDING)  &&
270            (arp_table[i].ctime >= ARP_MAXPENDING))) {
271         /* pending or stable entry has become old! */
272         LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %d.\n",
273                                    arp_table[i].state >= ETHARP_STATE_STABLE ? "stable" : "pending", i));
274 #if ETHARP_FAST_RTE
275         if (arp_table[i].state == ETHARP_STATE_PENDING) {
276           LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
277                       ("etharp_tmr: save the last unresolved ip %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
278                        ip4_addr1_16_val(arp_table[i].ipaddr), ip4_addr2_16_val(arp_table[i].ipaddr),
279                        ip4_addr3_16_val(arp_table[i].ipaddr), ip4_addr4_16_val(arp_table[i].ipaddr)));
280           ip4_addr_copy(g_last_unresolved_dest, arp_table[i].ipaddr);
281           g_fast_rte_time = ARP_FAST_RTE_TIME;
282         }
283 #endif
284         /* clean up entries that have just been expired */
285         etharp_free_entry(i);
286       } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_1) {
287         /* Don't send more than one request every 2 seconds. */
288         arp_table[i].state = ETHARP_STATE_STABLE_REREQUESTING_2;
289       } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_2) {
290         /* Reset state to stable, so that the next transmitted packet will
291            re-send an ARP request. */
292         arp_table[i].state = ETHARP_STATE_STABLE;
293       } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
294         /* still pending, resend an ARP query */
295         etharp_request(arp_table[i].netif, &arp_table[i].ipaddr);
296       }
297     }
298   }
299 #if LWIP_ARP_GRATUITOUS_REXMIT
300   etharp_gratuitous_retransmit();
301 #endif
302 }
303 
304 /**
305  * Search the ARP table for a matching or new entry.
306  *
307  * If an IP address is given, return a pending or stable ARP entry that matches
308  * the address. If no match is found, create a new entry with this address set,
309  * but in state ETHARP_EMPTY. The caller must check and possibly change the
310  * state of the returned entry.
311  *
312  * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
313  *
314  * In all cases, attempt to create new entries from an empty entry. If no
315  * empty entries are available and ETHARP_FLAG_TRY_HARD flag is set, recycle
316  * old entries. Heuristic choose the least important entry for recycling.
317  *
318  * @param ipaddr IP address to find in ARP cache, or to add if not found.
319  * @param flags See @ref etharp_state
320  * @param netif netif related to this address (used for NETIF_HWADDRHINT)
321  *
322  * @return The ARP entry index that matched or is created, ERR_MEM if no
323  * entry is found or could be recycled.
324  */
325 static s16_t
etharp_find_entry(const ip4_addr_t * ipaddr,u8_t flags,struct netif * netif)326 etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif *netif)
327 {
328   s16_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
329   s16_t empty = ARP_TABLE_SIZE;
330   s16_t i = 0;
331   /* oldest entry with packets on queue */
332   s16_t old_queue = ARP_TABLE_SIZE;
333   /* its age */
334   u16_t age_queue = 0, age_pending = 0, age_stable = 0;
335 
336   LWIP_UNUSED_ARG(netif);
337 
338   /**
339    * a) do a search through the cache, remember candidates
340    * b) select candidate entry
341    * c) create new entry
342    */
343 
344   /* a) in a single search sweep, do all of this
345    * 1) remember the first empty entry (if any)
346    * 2) remember the oldest stable entry (if any)
347    * 3) remember the oldest pending entry without queued packets (if any)
348    * 4) remember the oldest pending entry with queued packets (if any)
349    * 5) search for a matching IP entry, either pending or stable
350    *    until 5 matches, or all entries are searched for.
351    */
352 
353   for (i = 0; i < ARP_TABLE_SIZE; ++i) {
354     u8_t state = arp_table[i].state;
355     /* no empty entry found yet and now we do find one? */
356     if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) {
357       LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_find_entry: found empty entry %d\n", (int)i));
358       /* remember first empty entry */
359       empty = i;
360     } else if (state != ETHARP_STATE_EMPTY) {
361       LWIP_ASSERT("state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE",
362                   state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE);
363       /* if given, does IP address match IP address in ARP entry? */
364       if (ipaddr && ip4_addr_cmp(ipaddr, &arp_table[i].ipaddr)
365 #if ETHARP_TABLE_MATCH_NETIF
366           && ((netif == NULL) || (netif == arp_table[i].netif))
367 #endif /* ETHARP_TABLE_MATCH_NETIF */
368          ) {
369         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: found matching entry %d\n", (int)i));
370         /* found exact IP address match, simply bail out */
371         return i;
372       }
373       /* pending entry? */
374       if (state == ETHARP_STATE_PENDING) {
375         /* pending with queued packets? */
376         if (arp_table[i].q != NULL) {
377           if (arp_table[i].ctime >= age_queue) {
378             old_queue = i;
379             age_queue = arp_table[i].ctime;
380           }
381         } else
382           /* pending without queued packets? */
383         {
384           if (arp_table[i].ctime >= age_pending) {
385             old_pending = i;
386             age_pending = arp_table[i].ctime;
387           }
388         }
389         /* stable entry? */
390       } else if (state >= ETHARP_STATE_STABLE) {
391 #if ETHARP_SUPPORT_STATIC_ENTRIES
392         /* don't record old_stable for static entries since they never expire */
393         if (state < ETHARP_STATE_STATIC)
394 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
395         {
396           /* remember entry with oldest stable entry in oldest, its age in maxtime */
397           if (arp_table[i].ctime >= age_stable) {
398             old_stable = i;
399             age_stable = arp_table[i].ctime;
400           }
401         }
402       }
403     }
404   }
405   /* { we have no match } => try to create a new entry */
406 
407   /* don't create new entry, only search? */
408   if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) ||
409       /* or no empty entry found and not allowed to recycle? */
410       ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) {
411     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty entry found and not allowed to recycle\n"));
412     return (s16_t)ERR_MEM;
413   }
414 
415   /* b) choose the least destructive entry to recycle:
416    * 1) empty entry
417    * 2) oldest stable entry
418    * 3) oldest pending entry without queued packets
419    * 4) oldest pending entry with queued packets
420    *
421    * { ETHARP_FLAG_TRY_HARD is set at this point }
422    */
423 
424   /* 1) empty entry available? */
425   if (empty < ARP_TABLE_SIZE) {
426     i = empty;
427     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting empty entry %d\n", (int)i));
428   } else {
429     /* 2) found recyclable stable entry? */
430     if (old_stable < ARP_TABLE_SIZE) {
431       /* recycle oldest stable*/
432       i = old_stable;
433       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest stable entry %d\n", (int)i));
434       /* no queued packets should exist on stable entries */
435       LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL);
436       /* 3) found recyclable pending entry without queued packets? */
437     } else if (old_pending < ARP_TABLE_SIZE) {
438       /* recycle oldest pending */
439       i = old_pending;
440       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %d (without queue)\n", (int)i));
441       /* 4) found recyclable pending entry with queued packets? */
442     } else if (old_queue < ARP_TABLE_SIZE) {
443       /* recycle oldest pending (queued packets are free in etharp_free_entry) */
444       i = old_queue;
445       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %d, freeing packet queue %p\n", (int)i, (void *)(arp_table[i].q)));
446       /* no empty or recyclable entries found */
447     } else {
448       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty or recyclable entries found\n"));
449       return (s16_t)ERR_MEM;
450     }
451 
452     /* { empty or recyclable entry found } */
453     LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
454     etharp_free_entry(i);
455   }
456 
457   LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
458   LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY",
459               arp_table[i].state == ETHARP_STATE_EMPTY);
460 
461   /* IP address given? */
462   if (ipaddr != NULL) {
463     /* set IP address */
464     ip4_addr_copy(arp_table[i].ipaddr, *ipaddr);
465   }
466   arp_table[i].ctime = 0;
467 #if ETHARP_TABLE_MATCH_NETIF
468   arp_table[i].netif = netif;
469 #endif /* ETHARP_TABLE_MATCH_NETIF */
470   return (s16_t)i;
471 }
472 
473 /**
474  * Update (or insert) a IP/MAC address pair in the ARP cache.
475  *
476  * If a pending entry is resolved, any queued packets will be sent
477  * at this point.
478  *
479  * @param netif netif related to this entry (used for NETIF_ADDRHINT)
480  * @param ipaddr IP address of the inserted ARP entry.
481  * @param ethaddr Ethernet address of the inserted ARP entry.
482  * @param flags See @ref etharp_state
483  *
484  * @return
485  * - ERR_OK Successfully updated ARP cache.
486  * - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set.
487  * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
488  *
489  * @see pbuf_free()
490  */
491 err_t
etharp_update_arp_entry(struct netif * netif,const ip4_addr_t * ipaddr,struct eth_addr * ethaddr,u8_t flags)492 etharp_update_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
493 {
494   s16_t i;
495   LWIP_ASSERT("netif->hwaddr_len == ETH_HWADDR_LEN", netif->hwaddr_len == ETH_HWADDR_LEN);
496   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n",
497               ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
498               (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2],
499               (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5]));
500   /* non-unicast address? */
501   if (ip4_addr_isany(ipaddr) ||
502       ip4_addr_isbroadcast(ipaddr, netif) ||
503       ip4_addr_ismulticast(ipaddr)) {
504     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
505     return ERR_ARG;
506   }
507   /* find or create ARP entry */
508   i = etharp_find_entry(ipaddr, flags, netif);
509   /* bail out if no entry could be found */
510   if (i < 0) {
511     return (err_t)i;
512   }
513 
514 #if ETHARP_SUPPORT_STATIC_ENTRIES
515   if (flags & ETHARP_FLAG_STATIC_ENTRY) {
516     /* record static type */
517     arp_table[i].state = ETHARP_STATE_STATIC;
518   } else if (arp_table[i].state == ETHARP_STATE_STATIC) {
519     /* found entry is a static type, don't overwrite it */
520     return ERR_VAL;
521   } else
522 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
523   {
524     /* mark it stable */
525     arp_table[i].state = ETHARP_STATE_STABLE;
526   }
527 
528 #if ETHARP_FAST_RTE
529   if (ip4_addr_cmp(ipaddr, &g_last_unresolved_dest)) {
530     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
531                 ("etharp_update_arp_entry: ip %"U16_F".%"U16_F".%"U16_F".%"U16_F" is resolved now\n",
532                  ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
533     g_fast_rte_time = 0;
534     ip4_addr_set_any(&g_last_unresolved_dest);
535   }
536 #endif
537 
538   /* record network interface */
539   arp_table[i].netif = netif;
540   /* insert in SNMP ARP index tree */
541   mib2_add_arp_entry(netif, &arp_table[i].ipaddr);
542 
543   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: updating stable entry %"S16_F"\n", i));
544   /* update address */
545   SMEMCPY(&arp_table[i].ethaddr, ethaddr, ETH_HWADDR_LEN);
546   /* reset time stamp */
547   arp_table[i].ctime = 0;
548   /* this is where we will send out queued packets! */
549 #if ARP_QUEUEING
550   while (arp_table[i].q != NULL) {
551     struct pbuf *p;
552     /* remember remainder of queue */
553     struct etharp_q_entry *q = arp_table[i].q;
554     /* pop first item off the queue */
555     arp_table[i].q = q->next;
556     /* get the packet pointer */
557     p = q->p;
558     /* now queue entry can be freed */
559     memp_free(MEMP_ARP_QUEUE, q);
560 #else /* ARP_QUEUEING */
561   if (arp_table[i].q != NULL) {
562     struct pbuf *p = arp_table[i].q;
563     arp_table[i].q = NULL;
564 #endif /* ARP_QUEUEING */
565     /* send the queued IP packet */
566     ethernet_output(netif, p, (struct eth_addr *)(netif->hwaddr), ethaddr, ETHTYPE_IP);
567     /* free the queued IP packet */
568     pbuf_free(p);
569   }
570 
571   return ERR_OK;
572 }
573 
574 /*
575  * delete a IP/MAC_address pair in the ARP cache.
576  *
577  * @param netif netif related to this entry (used for NETIF_ADDRHINT)
578  * @param ipaddr IP_add of the inserted ARP entry.
579  *
580  * @return
581  * - ERR_OK Succesfully updated ARP cache..
582  * - ERR_ARG Non-unicast address given, or the entry is not found, or the entry
583  *   state is not ETHARP_STATE_STABLE.
584  */
585 err_t
586 etharp_delete_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr)
587 {
588   s16_t i;
589   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
590               ("etharp_delete_arp_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
591               ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
592   /* non-unicast address? */
593   if (ip4_addr_isany(ipaddr) ||
594       ip4_addr_isbroadcast(ipaddr, netif) ||
595       ip4_addr_ismulticast(ipaddr)) {
596     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
597                 ("etharp_update_arp_entry: will not add non-unicast IP_addr to ARP cache\n"));
598     return ERR_ARG;
599   }
600   /* find or create ARP entry */
601   i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, netif);
602   /* bail out if no entry could be found */
603   if (i < 0) {
604     return ERR_ARG;
605   }
606 
607   /* entry found, free it */
608   etharp_free_entry(i);
609   return ERR_OK;
610 }
611 
612 #if ETHARP_SUPPORT_STATIC_ENTRIES
613 /** Add a new static entry to the ARP table. If an entry exists for the
614  * specified IP address, this entry is overwritten.
615  * If packets are queued for the specified IP address, they are sent out.
616  *
617  * @param ipaddr IP address for the new static entry
618  * @param ethaddr ethernet address for the new static entry
619  * @return See return values of etharp_add_static_entry
620  */
621 err_t
622 etharp_add_static_entry(const ip4_addr_t *ipaddr, struct eth_addr *ethaddr)
623 {
624   struct netif *netif;
625   LWIP_ASSERT_CORE_LOCKED();
626   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_add_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n",
627               ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
628               (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2],
629               (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5]));
630 
631   netif = ip4_route(ipaddr);
632   if (netif == NULL) {
633     return ERR_RTE;
634   }
635 
636   return etharp_update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY);
637 }
638 
639 /** Remove a static entry from the ARP table previously added with a call to
640  * etharp_add_static_entry.
641  *
642  * @param ipaddr IP address of the static entry to remove
643  * @return ERR_OK: entry removed
644  *         ERR_MEM: entry wasn't found
645  *         ERR_ARG: entry wasn't a static entry but a dynamic one
646  */
647 err_t
648 etharp_remove_static_entry(const ip4_addr_t *ipaddr)
649 {
650   s16_t i;
651   LWIP_ASSERT_CORE_LOCKED();
652   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_remove_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
653               ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
654 
655   /* find or create ARP entry */
656   i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, NULL);
657   /* bail out if no entry could be found */
658   if (i < 0) {
659     return (err_t)i;
660   }
661 
662   if (arp_table[i].state != ETHARP_STATE_STATIC) {
663     /* entry wasn't a static entry, cannot remove it */
664     return ERR_ARG;
665   }
666   /* entry found, free it */
667   etharp_free_entry(i);
668   return ERR_OK;
669 }
670 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
671 
672 /**
673  * Remove all ARP table entries of the specified netif.
674  *
675  * @param netif points to a network interface
676  */
677 void
678 etharp_cleanup_netif(struct netif *netif)
679 {
680   s16_t i;
681 
682   for (i = 0; i < ARP_TABLE_SIZE; ++i) {
683     u8_t state = arp_table[i].state;
684     if ((state != ETHARP_STATE_EMPTY) && (arp_table[i].netif == netif)) {
685       etharp_free_entry(i);
686     }
687   }
688 }
689 
690 /**
691  * Finds (stable) ethernet/IP address pair from ARP table
692  * using interface and IP address index.
693  * @note the addresses in the ARP table are in network order!
694  *
695  * @param netif points to interface index
696  * @param ipaddr points to the (network order) IP address index
697  * @param eth_ret points to return pointer
698  * @param ip_ret points to return pointer
699  * @return table index if found, -1 otherwise
700  */
701 ssize_t
702 etharp_find_addr(struct netif *netif, const ip4_addr_t *ipaddr,
703                  struct eth_addr **eth_ret, const ip4_addr_t **ip_ret)
704 {
705   s16_t i;
706 
707   LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL",
708               eth_ret != NULL && ip_ret != NULL);
709 
710   LWIP_UNUSED_ARG(netif);
711 
712   i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, netif);
713   if ((i >= 0) && (arp_table[i].state >= ETHARP_STATE_STABLE)) {
714     *eth_ret = &arp_table[i].ethaddr;
715     *ip_ret = &arp_table[i].ipaddr;
716     return i;
717   }
718   return -1;
719 }
720 
721 err_t
722 etharp_ip_to_mac(const ip4_addr_t *ipaddr, u8_t *mac, u8_t *maclen)
723 {
724   const ip4_addr_t *ip_add = NULL;
725   struct eth_addr *tdst_mac = NULL;
726   s8_t ret;
727 
728   if (*maclen < ETH_HWADDR_LEN) {
729     return ERR_VAL;
730   }
731 
732   ret = etharp_find_addr(NULL, ipaddr, &tdst_mac, &ip_add);
733   if (ret == -1) {
734     return ERR_NOADDR;
735   }
736 
737   if (memcpy_s(mac, *maclen, tdst_mac->addr, ETH_HWADDR_LEN) != EOK) {
738     return ERR_MEM;
739   }
740   *maclen = ETH_HWADDR_LEN;
741 
742   return ERR_OK;
743 }
744 
745 /**
746  * Possibility to iterate over stable ARP table entries
747  *
748  * @param i entry number, 0 to ARP_TABLE_SIZE
749  * @param ipaddr return value: IP address
750  * @param netif return value: points to interface
751  * @param eth_ret return value: ETH address
752  * @return 1 on valid index, 0 otherwise
753  */
754 int
755 etharp_get_entry(size_t i, ip4_addr_t **ipaddr, struct netif **netif, struct eth_addr **eth_ret)
756 {
757   LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL);
758   LWIP_ASSERT("netif != NULL", netif != NULL);
759   LWIP_ASSERT("eth_ret != NULL", eth_ret != NULL);
760 
761   if ((i < ARP_TABLE_SIZE) && (arp_table[i].state >= ETHARP_STATE_STABLE)) {
762     *ipaddr  = &arp_table[i].ipaddr;
763     *netif   = arp_table[i].netif;
764     *eth_ret = &arp_table[i].ethaddr;
765     return 1;
766   } else {
767     return 0;
768   }
769 }
770 
771 #if ETHARP_TRUST_IP_MAC
772 /**
773  * Updates the ARP table using the given IP packet.
774  *
775  * Uses the incoming IP packet's source address to update the
776  * ARP cache for the local network. The function does not alter
777  * or free the packet. This function must be called before the
778  * packet p is passed to the IP layer.
779  *
780  * @param netif The lwIP network interface on which the IP packet pbuf arrived.
781  * @param p The IP packet that arrived on netif.
782  *
783  * @return NULL
784  *
785  * @see pbuf_free()
786  */
787 void
788 etharp_ip_input(struct netif *netif, struct pbuf *p)
789 {
790   struct eth_hdr *ethhdr = NULL;
791   struct ip_hdr *iphdr = NULL;
792   ip4_addr_t iphdr_src;
793   struct netif *loc_netif = NULL;
794   err_t err;
795 
796   LWIP_ERROR("netif != NULL", (netif != NULL), return);
797 
798   /* Only insert an entry if the source IP_add of the
799      incoming IP packet comes from a host on the local network. */
800   ethhdr = (struct eth_hdr *)p->payload;
801 
802 #if ETHARP_SUPPORT_VLAN
803   if (ethhdr->type == PP_HTONS(ETHTYPE_VLAN)) {
804     /* drop the packet as IP header size is small */
805     if (p->len < SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR + IP_HLEN) {
806       return;
807     }
808 
809     iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR);
810   } else
811 #endif /* ETHARP_SUPPORT_VLAN */
812   {
813     /* drop the packet as IP header size is small */
814     if (p->len < SIZEOF_ETH_HDR + IP_HLEN) {
815       return;
816     }
817 
818     iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
819   }
820 
821   ip4_addr_copy(iphdr_src, iphdr->src);
822 
823   /* source is not on the local network? */
824   if (!ip4_addr_netcmp(&iphdr_src, ip_2_ip4(&(netif->ip_addr)), ip_2_ip4(&(netif->netmask)))) {
825     loc_netif = netif_list;
826     while (loc_netif != NULL) {
827       if (loc_netif == netif) {
828         loc_netif = loc_netif->next;
829         continue;
830       }
831 
832       if ((!strncmp(loc_netif->name, netif->name, NETIF_NAMESIZE)) && (loc_netif->num == netif->num)) {
833         netif = loc_netif;
834         break;
835       }
836       loc_netif = loc_netif->next;
837     }
838     if (loc_netif == NULL) {
839       /* do nothing */
840       return;
841     }
842   }
843 
844   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n"));
845   /* update the source IP_add in the cache, if present */
846   /* @Note We could use ETHARP_FLAG_TRY_HARD if we think we are going to talk
847    * back soon (for example, if the destination IP_add is ours. */
848   err = etharp_update_arp_entry(netif, &iphdr_src, &(ethhdr->src), ETHARP_FLAG_FIND_ONLY);
849   if (err != ERR_OK) {
850     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_ip_input: updating ETHARP table return ERROR.\n"));
851   }
852   return;
853 }
854 #endif /* ETHARP_TRUST_IP_MAC */
855 
856 /**
857  * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache
858  * send out queued IP packets. Updates cache with snooped address pairs.
859  *
860  * Should be called for incoming ARP packets. The pbuf in the argument
861  * is freed by this function.
862  *
863  * @param p The ARP packet that arrived on netif. Is freed by this function.
864  * @param netif The lwIP network interface on which the ARP packet pbuf arrived.
865  *
866  * @see pbuf_free()
867  */
868 void
869 etharp_input(struct pbuf *p, struct netif *netif)
870 {
871   struct etharp_hdr *hdr;
872   const ip4_addr_t *targetip = NULL;
873   /* these are aligned properly, whereas the ARP header fields might not be */
874   ip4_addr_t sipaddr, dipaddr;
875   u8_t for_us;
876 
877   LWIP_ASSERT_CORE_LOCKED();
878 
879   LWIP_ERROR("netif != NULL", (netif != NULL), return;);
880 
881   hdr = (struct etharp_hdr *)p->payload;
882 
883   /* drop short ARP packets: we have to check for p->len instead of p->tot_len here
884      since a struct etharp_hdr is pointed to p->payload, so it musn't be chained! */
885   if (p->len < SIZEOF_ETHARP_HDR) {
886     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
887       ("etharp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type ( \
888       %"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n",
889       hdr->hwtype, (u16_t)hdr->hwlen, hdr->proto, (u16_t)hdr->protolen));
890     ETHARP_STATS_INC(etharp.proterr);
891     ETHARP_STATS_INC(etharp.drop);
892     (void)pbuf_free(p);
893     return;
894   }
895 
896   /* RFC 826 "Packet Reception": */
897   if ((hdr->hwtype != PP_HTONS(LWIP_IANA_HWTYPE_ETHERNET)) ||
898       (hdr->hwlen != ETH_HWADDR_LEN) ||
899       (hdr->protolen != sizeof(ip4_addr_t)) ||
900       (hdr->proto != PP_HTONS(ETHTYPE_IP)))  {
901     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
902                 ("etharp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n",
903                  hdr->hwtype, (u16_t)hdr->hwlen, hdr->proto, (u16_t)hdr->protolen));
904     ETHARP_STATS_INC(etharp.proterr);
905     ETHARP_STATS_INC(etharp.drop);
906     pbuf_free(p);
907     return;
908   }
909   ETHARP_STATS_INC(etharp.recv);
910 
911 #if LWIP_AUTOIP
912   /* We have to check if a host already has configured our random
913    * created link local address and continuously check if there is
914    * a host with this IP-address so we can detect collisions */
915   autoip_arp_reply(netif, hdr);
916 #endif /* LWIP_AUTOIP */
917 
918   /* Copy struct ip4_addr_wordaligned to aligned ip4_addr, to support compilers without
919    * structure packing (not using structure copy which breaks strict-aliasing rules). */
920   IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&sipaddr, &hdr->sipaddr);
921   IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&dipaddr, &hdr->dipaddr);
922 
923   targetip = netif_ip4_addr(netif);
924   /* this interface is not configured? */
925   if (ip4_addr_isany_val(*targetip)) {
926     for_us = 0;
927   } else {
928     /* ARP packet directed to us? */
929     for_us = (u8_t)ip4_addr_cmp(&dipaddr, netif_ip4_addr(netif));
930 #if LWIP_NAT64
931     if ((for_us == 0) && (nat64_arp_ip4_is_proxy(sipaddr, dipaddr) == lwIP_TRUE)) {
932       targetip = &dipaddr;
933       for_us = 1;
934     }
935 #endif
936   }
937 
938   /* ARP message directed to us?
939       -> add IP address in ARP cache; assume requester wants to talk to us,
940          can result in directly sending the queued packets for this host.
941      ARP message not directed to us?
942       ->  update the source IP address in the cache, if present */
943   etharp_update_arp_entry(netif, &sipaddr, &(hdr->shwaddr),
944                           for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY);
945 
946   /* now act on the message itself */
947   switch (hdr->opcode) {
948     /* ARP request? */
949     case PP_HTONS(ARP_REQUEST):
950       /* ARP request. If it asked for our address, we send out a
951        * reply. In any case, we time-stamp any existing ARP entry,
952        * and possibly send out an IP packet that was queued on it. */
953 
954       LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP request\n"));
955       /* ARP request for our address? */
956       if (for_us) {
957         /* send ARP response */
958         etharp_raw(netif,
959                    (struct eth_addr *)netif->hwaddr, &hdr->shwaddr,
960                    (struct eth_addr *)netif->hwaddr, targetip,
961                    &hdr->shwaddr, &sipaddr,
962                    ARP_REPLY);
963         /* we are not configured? */
964       } else if (ip4_addr_isany_val(*netif_ip4_addr(netif))) {
965         /* { for_us == 0 and netif->ip_addr.addr == 0 } */
966         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: we are unconfigured, ARP request ignored.\n"));
967         /* request was not directed to us */
968       } else {
969         /* { for_us == 0 and netif->ip_addr.addr != 0 } */
970         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP request was not for us.\n"));
971       }
972       break;
973     case PP_HTONS(ARP_REPLY):
974       /* ARP reply. We already updated the ARP cache earlier. */
975       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP reply\n"));
976 
977 #if LWIP_ENABLE_IP_CONFLICT_SIGNAL
978       if (ip4_addr_cmp(&sipaddr, netif_ip4_addr(netif))) {
979         /* Receive an arp reply. It means there is already a device use the same ip in the network. */
980         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: Ip conflict.\n"));
981         if (is_ip_conflict_signal) {
982 #if LWIP_ARP_GRATUITOUS_REXMIT
983           netif->arp_gratuitous_doing = lwIP_FALSE;
984           netif->arp_gratuitous_cnt = 0;
985 #endif
986           sys_sem_signal(&ip_conflict_detect);
987         }
988       }
989 #endif
990 
991 #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
992       /* DHCP wants to know about ARP replies from any host with an
993        * IP address also offered to us by the DHCP server. We do not
994        * want to take a duplicate IP address on a single network.
995        * @todo How should we handle redundant (fail-over) interfaces? */
996       dhcp_arp_reply(netif, &sipaddr);
997 #endif /* (LWIP_DHCP && DHCP_DOES_ARP_CHECK) */
998     break;
999   default:
1000     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP unknown opcode type %"S16_F"\n", lwip_htons(hdr->opcode)));
1001     ETHARP_STATS_INC(etharp.err);
1002     break;
1003   }
1004   /* free ARP packet */
1005   (void)pbuf_free(p);
1006 }
1007 
1008 /** Just a small helper function that sends a pbuf to an ethernet address
1009  * in the arp_table specified by the index 'arp_idx'.
1010  */
1011 static err_t
1012 etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, netif_addr_idx_t arp_idx)
1013 {
1014   LWIP_ASSERT("arp_table[arp_idx].state >= ETHARP_STATE_STABLE",
1015               arp_table[arp_idx].state >= ETHARP_STATE_STABLE);
1016   /* if arp table entry is about to expire: re-request it,
1017      but only if its state is ETHARP_STATE_STABLE to prevent flooding the
1018      network with ARP requests if this address is used frequently. */
1019   if (arp_table[arp_idx].state == ETHARP_STATE_STABLE) {
1020     if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_BROADCAST) {
1021       /* issue a standard request using broadcast */
1022       if (etharp_request(netif, &arp_table[arp_idx].ipaddr) == ERR_OK) {
1023         arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1;
1024       }
1025     } else if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_UNICAST) {
1026       /* issue a unicast request (for 15 seconds) to prevent unnecessary broadcast */
1027       if (etharp_request_dst(netif, &arp_table[arp_idx].ipaddr, &arp_table[arp_idx].ethaddr) == ERR_OK) {
1028         arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1;
1029       }
1030     }
1031   }
1032 
1033   return ethernet_output(netif, q, (struct eth_addr *)(netif->hwaddr), &arp_table[arp_idx].ethaddr, ETHTYPE_IP);
1034 }
1035 
1036 #if ETHARP_TABLE_MATCH_NETIF
1037 #define LWIP_ETHARP_MATCH_NETIF(_etharp_cached_entry, _netif) \
1038     (arp_table[(_etharp_cached_entry)].netif == (_netif)) &&
1039 #else
1040 #define LWIP_ETHARP_MATCH_NETIF(_etharp_cached_entry, _netif)
1041 #endif
1042 /**
1043  * Resolve and fill-in Ethernet address header for outgoing IP packet.
1044  *
1045  * For IP multicast and broadcast, corresponding Ethernet addresses
1046  * are selected and the packet is transmitted on the link.
1047  *
1048  * For unicast addresses, the packet is submitted to etharp_query(). In
1049  * case the IP address is outside the local network, the IP address of
1050  * the gateway is used.
1051  *
1052  * @param netif The lwIP network interface which the IP packet will be sent on.
1053  * @param q The pbuf(s) containing the IP packet to be sent.
1054  * @param ipaddr The IP address of the packet destination.
1055  *
1056  * @return
1057  * - ERR_RTE No route to destination (no gateway to external networks),
1058  * or the return type of either etharp_query() or ethernet_output().
1059  */
1060 err_t
1061 etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr)
1062 {
1063   const struct eth_addr *dest;
1064   struct eth_addr mcastaddr;
1065   const ip4_addr_t *dst_addr = ipaddr;
1066 
1067   LWIP_ASSERT_CORE_LOCKED();
1068   LWIP_ASSERT("netif != NULL", netif != NULL);
1069   LWIP_ASSERT("q != NULL", q != NULL);
1070   LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL);
1071 
1072 #if DRIVER_STATUS_CHECK
1073   if (netif_is_ready(netif) == 0) {
1074     LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_output: netif driver not ready\n"));
1075     /* Do not drop if it is a DHCP buffer */
1076     if (!(q->flags & PBUF_FLAG_DHCP_BUF)) {
1077       /* Drop the packet here and return success */
1078       return ERR_OK;
1079     }
1080   }
1081 #endif
1082 
1083 #if LWIP_DROP_PKT_WHEN_NETIF_DOWN
1084   if (!netif_is_up(netif) ||
1085       !netif_is_link_up(netif)) {
1086     LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_output: Only send packet when both driver and lwip states are up\n"));
1087     (void)pbuf_free(q);
1088     return ERR_OK;
1089   }
1090 #endif
1091 
1092   /* Determine on destination hardware address. Broadcasts and multicasts
1093    * are special, other IP addresses are looked up in the ARP table. */
1094 
1095   /* broadcast destination IP address? */
1096   if (ip4_addr_isbroadcast(ipaddr, netif)) {
1097     /* broadcast on Ethernet also */
1098     dest = (const struct eth_addr *)&ethbroadcast;
1099     /* multicast destination IP address? */
1100   } else if (ip4_addr_ismulticast(ipaddr)) {
1101     /* Hash IP multicast address to MAC address.*/
1102     mcastaddr.addr[0] = LL_IP4_MULTICAST_ADDR_0;
1103     mcastaddr.addr[1] = LL_IP4_MULTICAST_ADDR_1;
1104     mcastaddr.addr[2] = LL_IP4_MULTICAST_ADDR_2;
1105     mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
1106     mcastaddr.addr[4] = ip4_addr3(ipaddr);
1107     mcastaddr.addr[5] = ip4_addr4(ipaddr);
1108     /* destination Ethernet address is multicast */
1109     dest = &mcastaddr;
1110     /* unicast destination IP address? */
1111   } else {
1112     netif_addr_idx_t i;
1113     /* outside local network? if so, this can neither be a global broadcast nor
1114        a subnet broadcast. */
1115     /* the "PBUF_FLAG_IS_LINK_ONLY" flag means that the destination is also the nexthop,
1116        use the destination to do ARP resolution, don't use the gateway as the nexthop. */
1117     if (!ip4_addr_netcmp(ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) &&
1118         !ip4_addr_islinklocal(ipaddr)
1119 #if LWIP_SO_DONTROUTE
1120         && !(q->flags & PBUF_FLAG_IS_LINK_ONLY)
1121 #endif /* LWIP_SO_DONTROUTE */
1122         ) {
1123 #if LWIP_AUTOIP
1124       struct ip_hdr *iphdr = LWIP_ALIGNMENT_CAST(struct ip_hdr *, q->payload);
1125       /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with
1126          a link-local source address must always be "directly to its destination
1127          on the same physical link. The host MUST NOT send the packet to any
1128          router for forwarding". */
1129       if (!ip4_addr_islinklocal(&iphdr->src))
1130 #endif /* LWIP_AUTOIP */
1131       {
1132 #ifdef LWIP_HOOK_ETHARP_GET_GW
1133         /* For advanced routing, a single default gateway might not be enough, so get
1134            the IP address of the gateway to handle the current destination address. */
1135         dst_addr = LWIP_HOOK_ETHARP_GET_GW(netif, ipaddr);
1136         if (dst_addr == NULL)
1137 #endif /* LWIP_HOOK_ETHARP_GET_GW */
1138         {
1139           /* interface has default gateway? */
1140           if (!ip4_addr_isany_val(*netif_ip4_gw(netif))) {
1141             /* send to hardware address of default gateway IP address */
1142             dst_addr = netif_ip4_gw(netif);
1143             /* no default gateway available */
1144           } else {
1145             /* no route to destination error (default gateway missing) */
1146             return ERR_RTE;
1147           }
1148         }
1149       }
1150     }
1151 #if LWIP_NETIF_HWADDRHINT
1152     if (netif->hints != NULL) {
1153       /* per-pcb cached entry was given */
1154       netif_addr_idx_t etharp_cached_entry = netif->hints->addr_hint;
1155       if (etharp_cached_entry < ARP_TABLE_SIZE) {
1156 #endif /* LWIP_NETIF_HWADDRHINT */
1157         if ((arp_table[etharp_cached_entry].state >= ETHARP_STATE_STABLE) &&
1158             (arp_table[etharp_cached_entry].netif == netif) &&
1159              LWIP_ETHARP_MATCH_NETIF(etharp_cached_entry, netif)
1160             (ip4_addr_cmp(dst_addr, &arp_table[etharp_cached_entry].ipaddr))) {
1161           /* the per-pcb-cached entry is stable and the right one! */
1162           ETHARP_STATS_INC(etharp.cachehit);
1163           return etharp_output_to_arp_index(netif, q, etharp_cached_entry);
1164         }
1165 #if LWIP_NETIF_HWADDRHINT
1166       }
1167     }
1168 #endif /* LWIP_NETIF_HWADDRHINT */
1169 
1170     /* find stable entry: do this here since this is a critical path for
1171        throughput and etharp_find_entry() is kind of slow */
1172     for (i = 0; i < ARP_TABLE_SIZE; i++) {
1173       if ((arp_table[i].state >= ETHARP_STATE_STABLE) &&
1174 #if ETHARP_TABLE_MATCH_NETIF
1175           (arp_table[i].netif == netif) &&
1176 #endif
1177           (ip4_addr_cmp(dst_addr, &arp_table[i].ipaddr))) {
1178         /* found an existing, stable entry */
1179         ETHARP_SET_ADDRHINT(netif, i);
1180         return etharp_output_to_arp_index(netif, q, i);
1181       }
1182     }
1183     /* no stable entry found, use the (slower) query function:
1184        queue on destination Ethernet address belonging to ipaddr */
1185     return etharp_query(netif, dst_addr, q);
1186   }
1187 
1188   /* continuation for multicast/broadcast destinations */
1189   /* obtain source Ethernet address of the given interface */
1190   /* send packet directly on the link */
1191   return ethernet_output(netif, q, (struct eth_addr *)(netif->hwaddr), dest, ETHTYPE_IP);
1192 }
1193 
1194 /**
1195  * Send an ARP request for the given IP address and/or queue a packet.
1196  *
1197  * If the IP address was not yet in the cache, a pending ARP cache entry
1198  * is added and an ARP request is sent for the given address. The packet
1199  * is queued on this entry.
1200  *
1201  * If the IP address was already pending in the cache, a new ARP request
1202  * is sent for the given address. The packet is queued on this entry.
1203  *
1204  * If the IP address was already stable in the cache, and a packet is
1205  * given, it is directly sent and no ARP request is sent out.
1206  *
1207  * If the IP address was already stable in the cache, and no packet is
1208  * given, an ARP request is sent out.
1209  *
1210  * @param netif The lwIP network interface on which ipaddr
1211  * must be queried for.
1212  * @param ipaddr The IP address to be resolved.
1213  * @param q If non-NULL, a pbuf that must be delivered to the IP address.
1214  * q is not freed by this function.
1215  *
1216  * @note q must only be ONE packet, not a packet queue!
1217  *
1218  * @return
1219  * - ERR_BUF Could not make room for Ethernet header.
1220  * - ERR_MEM Hardware address unknown, and no more ARP entries available
1221  *   to query for address or queue the packet.
1222  * - ERR_MEM Could not queue packet due to memory shortage.
1223  * - ERR_RTE No route to destination (no gateway to external networks).
1224  * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
1225  *
1226  */
1227 err_t
1228 etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q)
1229 {
1230   struct eth_addr *srcaddr = (struct eth_addr *)netif->hwaddr;
1231   err_t result = ERR_MEM;
1232   int is_new_entry = 0;
1233   s16_t i_err;
1234   netif_addr_idx_t i;
1235 
1236   /* non-unicast address? */
1237   if (ip4_addr_isbroadcast(ipaddr, netif) ||
1238       ip4_addr_ismulticast(ipaddr) ||
1239       ip4_addr_isany(ipaddr)) {
1240     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
1241     return ERR_ARG;
1242   }
1243 
1244   /* find entry in ARP cache, ask to create entry if queueing packet */
1245   i_err = etharp_find_entry(ipaddr, ETHARP_FLAG_TRY_HARD, netif);
1246 
1247   /* could not find or create entry? */
1248   if (i_err < 0) {
1249     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
1250     if (q) {
1251       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n"));
1252       ETHARP_STATS_INC(etharp.memerr);
1253     }
1254     return (err_t)i_err;
1255   }
1256   LWIP_ASSERT("type overflow", (size_t)i_err < NETIF_ADDR_IDX_MAX);
1257   i = (netif_addr_idx_t)i_err;
1258 
1259   /* mark a fresh entry as pending (we just sent a request) */
1260   if (arp_table[i].state == ETHARP_STATE_EMPTY) {
1261     is_new_entry = 1;
1262     arp_table[i].state = ETHARP_STATE_PENDING;
1263     /* record network interface for re-sending arp request in etharp_tmr */
1264     arp_table[i].netif = netif;
1265   }
1266 
1267   /* { i is either a STABLE or (new or existing) PENDING entry } */
1268   LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
1269               ((arp_table[i].state == ETHARP_STATE_PENDING) ||
1270                (arp_table[i].state >= ETHARP_STATE_STABLE)));
1271 
1272   /* do we have a new entry? or an implicit query request? */
1273   if (is_new_entry || (q == NULL)) {
1274     /* try to resolve it; send out ARP request */
1275     result = etharp_request(netif, ipaddr);
1276     if (result != ERR_OK) {
1277       /* ARP request couldn't be sent */
1278       /* We don't re-send arp request in etharp_tmr, but we still queue packets,
1279          since this failure could be temporary, and the next packet calling
1280          etharp_query again could lead to sending the queued packets. */
1281     } else {
1282       /* ARP request successfully sent */
1283       if ((arp_table[i].state == ETHARP_STATE_PENDING) && !is_new_entry) {
1284         /* A new ARP request has been sent for a pending entry. Reset the ctime to
1285            not let it expire too fast. */
1286         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: reset ctime for entry %"S16_F"\n", (s16_t)i));
1287         arp_table[i].ctime = 0;
1288       }
1289     }
1290     if (q == NULL) {
1291       return result;
1292     }
1293   }
1294 
1295   /* packet given? */
1296   LWIP_ASSERT("q != NULL", q != NULL);
1297   /* stable entry? */
1298   if (arp_table[i].state >= ETHARP_STATE_STABLE) {
1299     /* we have a valid IP->Ethernet address mapping */
1300     ETHARP_SET_ADDRHINT(netif, i);
1301     /* send the packet */
1302     result = ethernet_output(netif, q, srcaddr, &(arp_table[i].ethaddr), ETHTYPE_IP);
1303     /* pending entry? (either just created or already pending */
1304   } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
1305 #if ETHARP_FAST_RTE
1306     if (ip4_addr_cmp(ipaddr, &g_last_unresolved_dest)) {
1307       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
1308                   ("etharp_query: try sending to the last unresolved ip %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
1309                    ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
1310       result = ERR_RTE;
1311       return result;
1312     }
1313 #endif
1314     /* entry is still pending, queue the given packet 'q' */
1315     struct pbuf *p;
1316     int copy_needed = 0;
1317     /* IF q includes a pbuf that must be copied, copy the whole chain into a
1318      * new PBUF_RAM. See the definition of PBUF_NEEDS_COPY for details. */
1319     p = q;
1320     while (p) {
1321       LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0));
1322       if (PBUF_NEEDS_COPY(p)) {
1323         copy_needed = 1;
1324         break;
1325       }
1326       p = p->next;
1327     }
1328     if (copy_needed) {
1329       /* copy the whole packet into new pbufs */
1330       p = pbuf_clone(PBUF_LINK, PBUF_RAM, q);
1331     } else {
1332       /* referencing the old pbuf is enough */
1333       p = q;
1334       pbuf_ref(p);
1335     }
1336     /* packet could be taken over? */
1337     if (p != NULL) {
1338       /* queue packet ... */
1339 #if ARP_QUEUEING
1340       struct etharp_q_entry *new_entry;
1341       /* allocate a new arp queue entry */
1342       new_entry = (struct etharp_q_entry *)memp_malloc(MEMP_ARP_QUEUE);
1343       if (new_entry != NULL) {
1344         unsigned int qlen = 0;
1345         new_entry->next = 0;
1346         new_entry->p = p;
1347         if (arp_table[i].q != NULL) {
1348           /* queue was already existent, append the new entry to the end */
1349           struct etharp_q_entry *r;
1350           r = arp_table[i].q;
1351           qlen++;
1352           while (r->next != NULL) {
1353             r = r->next;
1354             qlen++;
1355           }
1356           r->next = new_entry;
1357         } else {
1358           /* queue did not exist, first item in queue */
1359           arp_table[i].q = new_entry;
1360         }
1361 #if ARP_QUEUE_LEN
1362         if (qlen >= ARP_QUEUE_LEN) {
1363           struct etharp_q_entry *old;
1364           old = arp_table[i].q;
1365           arp_table[i].q = arp_table[i].q->next;
1366           pbuf_free(old->p);
1367           memp_free(MEMP_ARP_QUEUE, old);
1368         }
1369 #endif
1370         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"U16_F"\n", (void *)q, i));
1371         result = ERR_OK;
1372       } else {
1373         /* the pool MEMP_ARP_QUEUE is empty */
1374         pbuf_free(p);
1375         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));
1376         result = ERR_MEM;
1377       }
1378 #else /* ARP_QUEUEING */
1379       /* always queue one packet per ARP request only, freeing a previously queued packet */
1380       if (arp_table[i].q != NULL) {
1381         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: dropped previously queued packet %p for ARP entry %"U16_F"\n", (void *)q, (u16_t)i));
1382         pbuf_free(arp_table[i].q);
1383       }
1384       arp_table[i].q = p;
1385       result = ERR_OK;
1386       LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"U16_F"\n", (void *)q, (u16_t)i));
1387 #endif /* ARP_QUEUEING */
1388     } else {
1389       ETHARP_STATS_INC(etharp.memerr);
1390       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));
1391       result = ERR_MEM;
1392     }
1393   }
1394   return result;
1395 }
1396 
1397 /**
1398  * Send a raw ARP packet (opcode and all addresses can be modified)
1399  *
1400  * @param netif the lwip network interface on which to send the ARP packet
1401  * @param ethsrc_addr the source MAC address for the ethernet header
1402  * @param ethdst_addr the destination MAC address for the ethernet header
1403  * @param hwsrc_addr the source MAC address for the ARP protocol header
1404  * @param ipsrc_addr the source IP address for the ARP protocol header
1405  * @param hwdst_addr the destination MAC address for the ARP protocol header
1406  * @param ipdst_addr the destination IP address for the ARP protocol header
1407  * @param opcode the type of the ARP packet
1408  * @return ERR_OK if the ARP packet has been sent
1409  *         ERR_MEM if the ARP packet couldn't be allocated
1410  *         any other err_t on failure
1411  */
1412 static err_t
1413 etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
1414            const struct eth_addr *ethdst_addr,
1415            const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr,
1416            const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr,
1417            const u16_t opcode)
1418 {
1419   struct pbuf *p;
1420   err_t result = ERR_OK;
1421   struct etharp_hdr *hdr;
1422   struct netif *tmp_netif = NULL;
1423   LWIP_ASSERT("netif != NULL", netif != NULL);
1424   for (tmp_netif = netif_list; tmp_netif != NULL; tmp_netif = tmp_netif->next) {
1425     if (tmp_netif == netif) {
1426       break;
1427     }
1428   }
1429   if (tmp_netif != netif) {
1430     return ERR_OK; /* netif is not on the list */
1431   }
1432   /* allocate a pbuf for the outgoing ARP request packet */
1433   p = pbuf_alloc(PBUF_LINK, SIZEOF_ETHARP_HDR, PBUF_RAM);
1434   /* could allocate a pbuf for an ARP request? */
1435   if (p == NULL) {
1436     LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
1437                 ("etharp_raw: could not allocate pbuf for ARP request.\n"));
1438     ETHARP_STATS_INC(etharp.memerr);
1439     return ERR_MEM;
1440   }
1441   LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
1442               (p->len >= SIZEOF_ETHARP_HDR));
1443 
1444   hdr = (struct etharp_hdr *)p->payload;
1445   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n"));
1446   hdr->opcode = lwip_htons(opcode);
1447 
1448   LWIP_ASSERT("netif->hwaddr_len must be the same as ETH_HWADDR_LEN for etharp!",
1449               (netif->hwaddr_len == ETH_HWADDR_LEN));
1450 
1451   /* Write the ARP MAC-Addresses */
1452   SMEMCPY(&hdr->shwaddr, hwsrc_addr, ETH_HWADDR_LEN);
1453   SMEMCPY(&hdr->dhwaddr, hwdst_addr, ETH_HWADDR_LEN);
1454   /* Copy struct ip4_addr_wordaligned to aligned ip4_addr, to support compilers without
1455    * structure packing. */
1456   IPADDR_WORDALIGNED_COPY_FROM_IP4_ADDR_T(&hdr->sipaddr, ipsrc_addr);
1457   IPADDR_WORDALIGNED_COPY_FROM_IP4_ADDR_T(&hdr->dipaddr, ipdst_addr);
1458 
1459   hdr->hwtype = PP_HTONS(LWIP_IANA_HWTYPE_ETHERNET);
1460   hdr->proto = PP_HTONS(ETHTYPE_IP);
1461   /* set hwlen and protolen */
1462   hdr->hwlen = ETH_HWADDR_LEN;
1463   hdr->protolen = sizeof(ip4_addr_t);
1464 
1465   /* send ARP query */
1466 #if LWIP_AUTOIP
1467   /* If we are using Link-Local, all ARP packets that contain a Link-Local
1468    * 'sender IP address' MUST be sent using link-layer broadcast instead of
1469    * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
1470   if (ip4_addr_islinklocal(ipsrc_addr)) {
1471     ethernet_output(netif, p, ethsrc_addr, &ethbroadcast, ETHTYPE_ARP);
1472   } else
1473 #endif /* LWIP_AUTOIP */
1474   {
1475     ethernet_output(netif, p, ethsrc_addr, ethdst_addr, ETHTYPE_ARP);
1476   }
1477 
1478   ETHARP_STATS_INC(etharp.xmit);
1479   /* free ARP query packet */
1480   pbuf_free(p);
1481   p = NULL;
1482   /* could not allocate pbuf for ARP request */
1483 
1484   return result;
1485 }
1486 
1487 /**
1488  * Send an ARP request packet asking for ipaddr to a specific eth address.
1489  * Used to send unicast request to refresh the ARP table just before an entry
1490  * times out
1491  *
1492  * @param netif the lwip network interface on which to send the request
1493  * @param ipaddr the IP address for which to ask
1494  * @param hw_dst_addr the ethernet address to send this packet to
1495  * @return ERR_OK if the request has been sent
1496  *         ERR_MEM if the ARP packet couldn't be allocated
1497  *         any other err_t on failure
1498  */
1499 static err_t
1500 etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr *hw_dst_addr)
1501 {
1502   return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, hw_dst_addr,
1503                     (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif), &ethzero,
1504                     ipaddr, ARP_REQUEST);
1505 }
1506 
1507 /**
1508  * Send an ARP request packet asking for ipaddr.
1509  *
1510  * @param netif the lwip network interface on which to send the request
1511  * @param ipaddr the IP address for which to ask
1512  * @return ERR_OK if the request has been sent
1513  *         ERR_MEM if the ARP packet couldn't be allocated
1514  *         any other err_t on failure
1515  */
1516 err_t
1517 etharp_request(struct netif *netif, const ip4_addr_t *ipaddr)
1518 {
1519   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
1520   return etharp_request_dst(netif, ipaddr, &ethbroadcast);
1521 }
1522 
1523 /**
1524  * Send an ARP request packet announcing an ipaddr.
1525  *
1526  * @param netif the lwip network interface on which to send the request
1527  * @param ipaddr the IP address to announce
1528  * @return ERR_OK if the request has been sent
1529  *         ERR_MEM if the ARP packet couldn't be allocated
1530  *         any other err_t on failure
1531  */
1532 err_t
1533 etharp_announce(struct netif *netif, const ip4_addr_t *ipaddr)
1534 {
1535   return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, &ethbroadcast,
1536                     (struct eth_addr *)netif->hwaddr, ipaddr, &ethzero,
1537                     ipaddr, ARP_REQUEST);
1538 }
1539 #endif /* LWIP_IPV4 && LWIP_ARP */
1540