Remove building with NOCRYPTO option
[minix3.git] / minix / lib / liblwip / dist / src / core / ipv4 / etharp.c
blobd76b7f2b3b72a6d7fcb69383b90a60a58692be4d
1 /**
2 * @file
3 * Address Resolution Protocol module for IP over Ethernet
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.
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.
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.
20 * Redistribution and use in source and binary forms, with or without modification,
21 * are permitted provided that the following conditions are met:
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.
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.
42 * This file is part of the lwIP TCP/IP stack.
46 #include "lwip/opt.h"
48 #if LWIP_ARP || LWIP_ETHERNET
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 "netif/ethernet.h"
57 #include <string.h>
59 #ifdef LWIP_HOOK_FILENAME
60 #include LWIP_HOOK_FILENAME
61 #endif
63 #if LWIP_IPV4 && LWIP_ARP /* don't build if not configured for use in lwipopts.h */
65 /** Re-request a used ARP entry 1 minute before it would expire to prevent
66 * breaking a steadily used connection because the ARP entry timed out. */
67 #define ARP_AGE_REREQUEST_USED_UNICAST (ARP_MAXAGE - 30)
68 #define ARP_AGE_REREQUEST_USED_BROADCAST (ARP_MAXAGE - 15)
70 /** the time an ARP entry stays pending after first request,
71 * for ARP_TMR_INTERVAL = 1000, this is
72 * 10 seconds.
74 * @internal Keep this number at least 2, otherwise it might
75 * run out instantly if the timeout occurs directly after a request.
77 #define ARP_MAXPENDING 5
79 /** ARP states */
80 enum etharp_state {
81 ETHARP_STATE_EMPTY = 0,
82 ETHARP_STATE_PENDING,
83 ETHARP_STATE_STABLE,
84 ETHARP_STATE_STABLE_REREQUESTING_1,
85 ETHARP_STATE_STABLE_REREQUESTING_2
86 #if ETHARP_SUPPORT_STATIC_ENTRIES
87 ,ETHARP_STATE_STATIC
88 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
91 struct etharp_entry {
92 #if ARP_QUEUEING
93 /** Pointer to queue of pending outgoing packets on this ARP entry. */
94 struct etharp_q_entry *q;
95 #else /* ARP_QUEUEING */
96 /** Pointer to a single pending outgoing packet on this ARP entry. */
97 struct pbuf *q;
98 #endif /* ARP_QUEUEING */
99 ip4_addr_t ipaddr;
100 struct netif *netif;
101 struct eth_addr ethaddr;
102 u16_t ctime;
103 u8_t state;
106 static struct etharp_entry arp_table[ARP_TABLE_SIZE];
108 #if !LWIP_NETIF_HWADDRHINT
109 static u8_t etharp_cached_entry;
110 #endif /* !LWIP_NETIF_HWADDRHINT */
112 /** Try hard to create a new entry - we want the IP address to appear in
113 the cache (even if this means removing an active entry or so). */
114 #define ETHARP_FLAG_TRY_HARD 1
115 #define ETHARP_FLAG_FIND_ONLY 2
116 #if ETHARP_SUPPORT_STATIC_ENTRIES
117 #define ETHARP_FLAG_STATIC_ENTRY 4
118 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
120 #if LWIP_NETIF_HWADDRHINT
121 #define ETHARP_SET_HINT(netif, hint) if (((netif) != NULL) && ((netif)->addr_hint != NULL)) \
122 *((netif)->addr_hint) = (hint);
123 #else /* LWIP_NETIF_HWADDRHINT */
124 #define ETHARP_SET_HINT(netif, hint) (etharp_cached_entry = (hint))
125 #endif /* LWIP_NETIF_HWADDRHINT */
128 /* Some checks, instead of etharp_init(): */
129 #if (LWIP_ARP && (ARP_TABLE_SIZE > 0x7f))
130 #error "ARP_TABLE_SIZE must fit in an s8_t, you have to reduce it in your lwipopts.h"
131 #endif
134 static err_t etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr* hw_dst_addr);
135 static err_t etharp_raw(struct netif *netif,
136 const struct eth_addr *ethsrc_addr, const struct eth_addr *ethdst_addr,
137 const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr,
138 const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr,
139 const u16_t opcode);
141 #if ARP_QUEUEING
143 * Free a complete queue of etharp entries
145 * @param q a qeueue of etharp_q_entry's to free
147 static void
148 free_etharp_q(struct etharp_q_entry *q)
150 struct etharp_q_entry *r;
151 LWIP_ASSERT("q != NULL", q != NULL);
152 LWIP_ASSERT("q->p != NULL", q->p != NULL);
153 while (q) {
154 r = q;
155 q = q->next;
156 LWIP_ASSERT("r->p != NULL", (r->p != NULL));
157 pbuf_free(r->p);
158 memp_free(MEMP_ARP_QUEUE, r);
161 #else /* ARP_QUEUEING */
163 /** Compatibility define: free the queued pbuf */
164 #define free_etharp_q(q) pbuf_free(q)
166 #endif /* ARP_QUEUEING */
168 /** Clean up ARP table entries */
169 static void
170 etharp_free_entry(int i)
172 /* remove from SNMP ARP index tree */
173 mib2_remove_arp_entry(arp_table[i].netif, &arp_table[i].ipaddr);
174 /* and empty packet queue */
175 if (arp_table[i].q != NULL) {
176 /* remove all queued packets */
177 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_free_entry: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q)));
178 free_etharp_q(arp_table[i].q);
179 arp_table[i].q = NULL;
181 /* recycle entry for re-use */
182 arp_table[i].state = ETHARP_STATE_EMPTY;
183 #ifdef LWIP_DEBUG
184 /* for debugging, clean out the complete entry */
185 arp_table[i].ctime = 0;
186 arp_table[i].netif = NULL;
187 ip4_addr_set_zero(&arp_table[i].ipaddr);
188 arp_table[i].ethaddr = ethzero;
189 #endif /* LWIP_DEBUG */
193 * Clears expired entries in the ARP table.
195 * This function should be called every ARP_TMR_INTERVAL milliseconds (1 second),
196 * in order to expire entries in the ARP table.
198 void
199 etharp_tmr(void)
201 u8_t i;
203 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
204 /* remove expired entries from the ARP table */
205 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
206 u8_t state = arp_table[i].state;
207 if (state != ETHARP_STATE_EMPTY
208 #if ETHARP_SUPPORT_STATIC_ENTRIES
209 && (state != ETHARP_STATE_STATIC)
210 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
212 arp_table[i].ctime++;
213 if ((arp_table[i].ctime >= ARP_MAXAGE) ||
214 ((arp_table[i].state == ETHARP_STATE_PENDING) &&
215 (arp_table[i].ctime >= ARP_MAXPENDING))) {
216 /* pending or stable entry has become old! */
217 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %"U16_F".\n",
218 arp_table[i].state >= ETHARP_STATE_STABLE ? "stable" : "pending", (u16_t)i));
219 /* clean up entries that have just been expired */
220 etharp_free_entry(i);
221 } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_1) {
222 /* Don't send more than one request every 2 seconds. */
223 arp_table[i].state = ETHARP_STATE_STABLE_REREQUESTING_2;
224 } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_2) {
225 /* Reset state to stable, so that the next transmitted packet will
226 re-send an ARP request. */
227 arp_table[i].state = ETHARP_STATE_STABLE;
228 } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
229 /* still pending, resend an ARP query */
230 etharp_request(arp_table[i].netif, &arp_table[i].ipaddr);
237 * Search the ARP table for a matching or new entry.
239 * If an IP address is given, return a pending or stable ARP entry that matches
240 * the address. If no match is found, create a new entry with this address set,
241 * but in state ETHARP_EMPTY. The caller must check and possibly change the
242 * state of the returned entry.
244 * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
246 * In all cases, attempt to create new entries from an empty entry. If no
247 * empty entries are available and ETHARP_FLAG_TRY_HARD flag is set, recycle
248 * old entries. Heuristic choose the least important entry for recycling.
250 * @param ipaddr IP address to find in ARP cache, or to add if not found.
251 * @param flags See @ref etharp_state
252 * @param netif netif related to this address (used for NETIF_HWADDRHINT)
254 * @return The ARP entry index that matched or is created, ERR_MEM if no
255 * entry is found or could be recycled.
257 static s8_t
258 etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif* netif)
260 s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
261 s8_t empty = ARP_TABLE_SIZE;
262 u8_t i = 0;
263 /* oldest entry with packets on queue */
264 s8_t old_queue = ARP_TABLE_SIZE;
265 /* its age */
266 u16_t age_queue = 0, age_pending = 0, age_stable = 0;
268 LWIP_UNUSED_ARG(netif);
271 * a) do a search through the cache, remember candidates
272 * b) select candidate entry
273 * c) create new entry
276 /* a) in a single search sweep, do all of this
277 * 1) remember the first empty entry (if any)
278 * 2) remember the oldest stable entry (if any)
279 * 3) remember the oldest pending entry without queued packets (if any)
280 * 4) remember the oldest pending entry with queued packets (if any)
281 * 5) search for a matching IP entry, either pending or stable
282 * until 5 matches, or all entries are searched for.
285 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
286 u8_t state = arp_table[i].state;
287 /* no empty entry found yet and now we do find one? */
288 if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) {
289 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_find_entry: found empty entry %"U16_F"\n", (u16_t)i));
290 /* remember first empty entry */
291 empty = i;
292 } else if (state != ETHARP_STATE_EMPTY) {
293 LWIP_ASSERT("state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE",
294 state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE);
295 /* if given, does IP address match IP address in ARP entry? */
296 if (ipaddr && ip4_addr_cmp(ipaddr, &arp_table[i].ipaddr)
297 #if ETHARP_TABLE_MATCH_NETIF
298 && ((netif == NULL) || (netif == arp_table[i].netif))
299 #endif /* ETHARP_TABLE_MATCH_NETIF */
301 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: found matching entry %"U16_F"\n", (u16_t)i));
302 /* found exact IP address match, simply bail out */
303 return i;
305 /* pending entry? */
306 if (state == ETHARP_STATE_PENDING) {
307 /* pending with queued packets? */
308 if (arp_table[i].q != NULL) {
309 if (arp_table[i].ctime >= age_queue) {
310 old_queue = i;
311 age_queue = arp_table[i].ctime;
313 } else
314 /* pending without queued packets? */
316 if (arp_table[i].ctime >= age_pending) {
317 old_pending = i;
318 age_pending = arp_table[i].ctime;
321 /* stable entry? */
322 } else if (state >= ETHARP_STATE_STABLE) {
323 #if ETHARP_SUPPORT_STATIC_ENTRIES
324 /* don't record old_stable for static entries since they never expire */
325 if (state < ETHARP_STATE_STATIC)
326 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
328 /* remember entry with oldest stable entry in oldest, its age in maxtime */
329 if (arp_table[i].ctime >= age_stable) {
330 old_stable = i;
331 age_stable = arp_table[i].ctime;
337 /* { we have no match } => try to create a new entry */
339 /* don't create new entry, only search? */
340 if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) ||
341 /* or no empty entry found and not allowed to recycle? */
342 ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) {
343 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty entry found and not allowed to recycle\n"));
344 return (s8_t)ERR_MEM;
347 /* b) choose the least destructive entry to recycle:
348 * 1) empty entry
349 * 2) oldest stable entry
350 * 3) oldest pending entry without queued packets
351 * 4) oldest pending entry with queued packets
353 * { ETHARP_FLAG_TRY_HARD is set at this point }
356 /* 1) empty entry available? */
357 if (empty < ARP_TABLE_SIZE) {
358 i = empty;
359 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting empty entry %"U16_F"\n", (u16_t)i));
360 } else {
361 /* 2) found recyclable stable entry? */
362 if (old_stable < ARP_TABLE_SIZE) {
363 /* recycle oldest stable*/
364 i = old_stable;
365 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest stable entry %"U16_F"\n", (u16_t)i));
366 /* no queued packets should exist on stable entries */
367 LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL);
368 /* 3) found recyclable pending entry without queued packets? */
369 } else if (old_pending < ARP_TABLE_SIZE) {
370 /* recycle oldest pending */
371 i = old_pending;
372 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %"U16_F" (without queue)\n", (u16_t)i));
373 /* 4) found recyclable pending entry with queued packets? */
374 } else if (old_queue < ARP_TABLE_SIZE) {
375 /* recycle oldest pending (queued packets are free in etharp_free_entry) */
376 i = old_queue;
377 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %"U16_F", freeing packet queue %p\n", (u16_t)i, (void *)(arp_table[i].q)));
378 /* no empty or recyclable entries found */
379 } else {
380 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty or recyclable entries found\n"));
381 return (s8_t)ERR_MEM;
384 /* { empty or recyclable entry found } */
385 LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
386 etharp_free_entry(i);
389 LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
390 LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY",
391 arp_table[i].state == ETHARP_STATE_EMPTY);
393 /* IP address given? */
394 if (ipaddr != NULL) {
395 /* set IP address */
396 ip4_addr_copy(arp_table[i].ipaddr, *ipaddr);
398 arp_table[i].ctime = 0;
399 #if ETHARP_TABLE_MATCH_NETIF
400 arp_table[i].netif = netif;
401 #endif /* ETHARP_TABLE_MATCH_NETIF*/
402 return (err_t)i;
406 * Update (or insert) a IP/MAC address pair in the ARP cache.
408 * If a pending entry is resolved, any queued packets will be sent
409 * at this point.
411 * @param netif netif related to this entry (used for NETIF_ADDRHINT)
412 * @param ipaddr IP address of the inserted ARP entry.
413 * @param ethaddr Ethernet address of the inserted ARP entry.
414 * @param flags See @ref etharp_state
416 * @return
417 * - ERR_OK Successfully updated ARP cache.
418 * - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set.
419 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
421 * @see pbuf_free()
423 static err_t
424 etharp_update_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
426 s8_t i;
427 LWIP_ASSERT("netif->hwaddr_len == ETH_HWADDR_LEN", netif->hwaddr_len == ETH_HWADDR_LEN);
428 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",
429 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
430 (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2],
431 (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5]));
432 /* non-unicast address? */
433 if (ip4_addr_isany(ipaddr) ||
434 ip4_addr_isbroadcast(ipaddr, netif) ||
435 ip4_addr_ismulticast(ipaddr)) {
436 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
437 return ERR_ARG;
439 /* find or create ARP entry */
440 i = etharp_find_entry(ipaddr, flags, netif);
441 /* bail out if no entry could be found */
442 if (i < 0) {
443 return (err_t)i;
446 #if ETHARP_SUPPORT_STATIC_ENTRIES
447 if (flags & ETHARP_FLAG_STATIC_ENTRY) {
448 /* record static type */
449 arp_table[i].state = ETHARP_STATE_STATIC;
450 } else if (arp_table[i].state == ETHARP_STATE_STATIC) {
451 /* found entry is a static type, don't overwrite it */
452 return ERR_VAL;
453 } else
454 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
456 /* mark it stable */
457 arp_table[i].state = ETHARP_STATE_STABLE;
460 /* record network interface */
461 arp_table[i].netif = netif;
462 /* insert in SNMP ARP index tree */
463 mib2_add_arp_entry(netif, &arp_table[i].ipaddr);
465 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i));
466 /* update address */
467 ETHADDR16_COPY(&arp_table[i].ethaddr, ethaddr);
468 /* reset time stamp */
469 arp_table[i].ctime = 0;
470 /* this is where we will send out queued packets! */
471 #if ARP_QUEUEING
472 while (arp_table[i].q != NULL) {
473 struct pbuf *p;
474 /* remember remainder of queue */
475 struct etharp_q_entry *q = arp_table[i].q;
476 /* pop first item off the queue */
477 arp_table[i].q = q->next;
478 /* get the packet pointer */
479 p = q->p;
480 /* now queue entry can be freed */
481 memp_free(MEMP_ARP_QUEUE, q);
482 #else /* ARP_QUEUEING */
483 if (arp_table[i].q != NULL) {
484 struct pbuf *p = arp_table[i].q;
485 arp_table[i].q = NULL;
486 #endif /* ARP_QUEUEING */
487 /* send the queued IP packet */
488 ethernet_output(netif, p, (struct eth_addr*)(netif->hwaddr), ethaddr, ETHTYPE_IP);
489 /* free the queued IP packet */
490 pbuf_free(p);
492 return ERR_OK;
495 #if ETHARP_SUPPORT_STATIC_ENTRIES
496 /** Add a new static entry to the ARP table. If an entry exists for the
497 * specified IP address, this entry is overwritten.
498 * If packets are queued for the specified IP address, they are sent out.
500 * @param ipaddr IP address for the new static entry
501 * @param ethaddr ethernet address for the new static entry
502 * @return See return values of etharp_add_static_entry
504 err_t
505 etharp_add_static_entry(const ip4_addr_t *ipaddr, struct eth_addr *ethaddr)
507 struct netif *netif;
508 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",
509 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
510 (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2],
511 (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5]));
513 netif = ip4_route(ipaddr);
514 if (netif == NULL) {
515 return ERR_RTE;
518 return etharp_update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY);
521 /** Remove a static entry from the ARP table previously added with a call to
522 * etharp_add_static_entry.
524 * @param ipaddr IP address of the static entry to remove
525 * @return ERR_OK: entry removed
526 * ERR_MEM: entry wasn't found
527 * ERR_ARG: entry wasn't a static entry but a dynamic one
529 err_t
530 etharp_remove_static_entry(const ip4_addr_t *ipaddr)
532 s8_t i;
533 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_remove_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
534 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
536 /* find or create ARP entry */
537 i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, NULL);
538 /* bail out if no entry could be found */
539 if (i < 0) {
540 return (err_t)i;
543 if (arp_table[i].state != ETHARP_STATE_STATIC) {
544 /* entry wasn't a static entry, cannot remove it */
545 return ERR_ARG;
547 /* entry found, free it */
548 etharp_free_entry(i);
549 return ERR_OK;
551 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
554 * Remove all ARP table entries of the specified netif.
556 * @param netif points to a network interface
558 void
559 etharp_cleanup_netif(struct netif *netif)
561 u8_t i;
563 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
564 u8_t state = arp_table[i].state;
565 if ((state != ETHARP_STATE_EMPTY) && (arp_table[i].netif == netif)) {
566 etharp_free_entry(i);
572 * Finds (stable) ethernet/IP address pair from ARP table
573 * using interface and IP address index.
574 * @note the addresses in the ARP table are in network order!
576 * @param netif points to interface index
577 * @param ipaddr points to the (network order) IP address index
578 * @param eth_ret points to return pointer
579 * @param ip_ret points to return pointer
580 * @return table index if found, -1 otherwise
582 s8_t
583 etharp_find_addr(struct netif *netif, const ip4_addr_t *ipaddr,
584 struct eth_addr **eth_ret, const ip4_addr_t **ip_ret)
586 s8_t i;
588 LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL",
589 eth_ret != NULL && ip_ret != NULL);
591 LWIP_UNUSED_ARG(netif);
593 i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, netif);
594 if ((i >= 0) && (arp_table[i].state >= ETHARP_STATE_STABLE)) {
595 *eth_ret = &arp_table[i].ethaddr;
596 *ip_ret = &arp_table[i].ipaddr;
597 return i;
599 return -1;
603 * Possibility to iterate over stable ARP table entries
605 * @param i entry number, 0 to ARP_TABLE_SIZE
606 * @param ipaddr return value: IP address
607 * @param netif return value: points to interface
608 * @param eth_ret return value: ETH address
609 * @return 1 on valid index, 0 otherwise
611 u8_t
612 etharp_get_entry(u8_t i, ip4_addr_t **ipaddr, struct netif **netif, struct eth_addr **eth_ret)
614 LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL);
615 LWIP_ASSERT("netif != NULL", netif != NULL);
616 LWIP_ASSERT("eth_ret != NULL", eth_ret != NULL);
618 if((i < ARP_TABLE_SIZE) && (arp_table[i].state >= ETHARP_STATE_STABLE)) {
619 *ipaddr = &arp_table[i].ipaddr;
620 *netif = arp_table[i].netif;
621 *eth_ret = &arp_table[i].ethaddr;
622 return 1;
623 } else {
624 return 0;
629 * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache
630 * send out queued IP packets. Updates cache with snooped address pairs.
632 * Should be called for incoming ARP packets. The pbuf in the argument
633 * is freed by this function.
635 * @param p The ARP packet that arrived on netif. Is freed by this function.
636 * @param netif The lwIP network interface on which the ARP packet pbuf arrived.
638 * @see pbuf_free()
640 void
641 etharp_input(struct pbuf *p, struct netif *netif)
643 struct etharp_hdr *hdr;
644 /* these are aligned properly, whereas the ARP header fields might not be */
645 ip4_addr_t sipaddr, dipaddr;
646 u8_t for_us;
648 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
650 hdr = (struct etharp_hdr *)p->payload;
652 /* RFC 826 "Packet Reception": */
653 if ((hdr->hwtype != PP_HTONS(HWTYPE_ETHERNET)) ||
654 (hdr->hwlen != ETH_HWADDR_LEN) ||
655 (hdr->protolen != sizeof(ip4_addr_t)) ||
656 (hdr->proto != PP_HTONS(ETHTYPE_IP))) {
657 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
658 ("etharp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n",
659 hdr->hwtype, (u16_t)hdr->hwlen, hdr->proto, (u16_t)hdr->protolen));
660 ETHARP_STATS_INC(etharp.proterr);
661 ETHARP_STATS_INC(etharp.drop);
662 pbuf_free(p);
663 return;
665 ETHARP_STATS_INC(etharp.recv);
667 #if LWIP_AUTOIP
668 /* We have to check if a host already has configured our random
669 * created link local address and continuously check if there is
670 * a host with this IP-address so we can detect collisions */
671 autoip_arp_reply(netif, hdr);
672 #endif /* LWIP_AUTOIP */
674 /* Copy struct ip4_addr_wordaligned to aligned ip4_addr, to support compilers without
675 * structure packing (not using structure copy which breaks strict-aliasing rules). */
676 IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&sipaddr, &hdr->sipaddr);
677 IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&dipaddr, &hdr->dipaddr);
679 /* this interface is not configured? */
680 if (ip4_addr_isany_val(*netif_ip4_addr(netif))) {
681 for_us = 0;
682 } else {
683 /* ARP packet directed to us? */
684 for_us = (u8_t)ip4_addr_cmp(&dipaddr, netif_ip4_addr(netif));
687 /* ARP message directed to us?
688 -> add IP address in ARP cache; assume requester wants to talk to us,
689 can result in directly sending the queued packets for this host.
690 ARP message not directed to us?
691 -> update the source IP address in the cache, if present */
692 etharp_update_arp_entry(netif, &sipaddr, &(hdr->shwaddr),
693 for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY);
695 /* now act on the message itself */
696 switch (hdr->opcode) {
697 /* ARP request? */
698 case PP_HTONS(ARP_REQUEST):
699 /* ARP request. If it asked for our address, we send out a
700 * reply. In any case, we time-stamp any existing ARP entry,
701 * and possibly send out an IP packet that was queued on it. */
703 LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP request\n"));
704 /* ARP request for our address? */
705 if (for_us) {
706 /* send ARP response */
707 etharp_raw(netif,
708 (struct eth_addr *)netif->hwaddr, &hdr->shwaddr,
709 (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif),
710 &hdr->shwaddr, &sipaddr,
711 ARP_REPLY);
712 /* we are not configured? */
713 } else if (ip4_addr_isany_val(*netif_ip4_addr(netif))) {
714 /* { for_us == 0 and netif->ip_addr.addr == 0 } */
715 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: we are unconfigured, ARP request ignored.\n"));
716 /* request was not directed to us */
717 } else {
718 /* { for_us == 0 and netif->ip_addr.addr != 0 } */
719 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP request was not for us.\n"));
721 break;
722 case PP_HTONS(ARP_REPLY):
723 /* ARP reply. We already updated the ARP cache earlier. */
724 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP reply\n"));
725 #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
726 /* DHCP wants to know about ARP replies from any host with an
727 * IP address also offered to us by the DHCP server. We do not
728 * want to take a duplicate IP address on a single network.
729 * @todo How should we handle redundant (fail-over) interfaces? */
730 dhcp_arp_reply(netif, &sipaddr);
731 #endif /* (LWIP_DHCP && DHCP_DOES_ARP_CHECK) */
732 break;
733 default:
734 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP unknown opcode type %"S16_F"\n", lwip_htons(hdr->opcode)));
735 ETHARP_STATS_INC(etharp.err);
736 break;
738 /* free ARP packet */
739 pbuf_free(p);
742 /** Just a small helper function that sends a pbuf to an ethernet address
743 * in the arp_table specified by the index 'arp_idx'.
745 static err_t
746 etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, u8_t arp_idx)
748 LWIP_ASSERT("arp_table[arp_idx].state >= ETHARP_STATE_STABLE",
749 arp_table[arp_idx].state >= ETHARP_STATE_STABLE);
750 /* if arp table entry is about to expire: re-request it,
751 but only if its state is ETHARP_STATE_STABLE to prevent flooding the
752 network with ARP requests if this address is used frequently. */
753 if (arp_table[arp_idx].state == ETHARP_STATE_STABLE) {
754 if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_BROADCAST) {
755 /* issue a standard request using broadcast */
756 if (etharp_request(netif, &arp_table[arp_idx].ipaddr) == ERR_OK) {
757 arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1;
759 } else if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_UNICAST) {
760 /* issue a unicast request (for 15 seconds) to prevent unnecessary broadcast */
761 if (etharp_request_dst(netif, &arp_table[arp_idx].ipaddr, &arp_table[arp_idx].ethaddr) == ERR_OK) {
762 arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1;
767 return ethernet_output(netif, q, (struct eth_addr*)(netif->hwaddr), &arp_table[arp_idx].ethaddr, ETHTYPE_IP);
771 * Resolve and fill-in Ethernet address header for outgoing IP packet.
773 * For IP multicast and broadcast, corresponding Ethernet addresses
774 * are selected and the packet is transmitted on the link.
776 * For unicast addresses, the packet is submitted to etharp_query(). In
777 * case the IP address is outside the local network, the IP address of
778 * the gateway is used.
780 * @param netif The lwIP network interface which the IP packet will be sent on.
781 * @param q The pbuf(s) containing the IP packet to be sent.
782 * @param ipaddr The IP address of the packet destination.
784 * @return
785 * - ERR_RTE No route to destination (no gateway to external networks),
786 * or the return type of either etharp_query() or ethernet_output().
788 err_t
789 etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr)
791 const struct eth_addr *dest;
792 struct eth_addr mcastaddr;
793 const ip4_addr_t *dst_addr = ipaddr;
795 LWIP_ASSERT("netif != NULL", netif != NULL);
796 LWIP_ASSERT("q != NULL", q != NULL);
797 LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL);
799 /* Determine on destination hardware address. Broadcasts and multicasts
800 * are special, other IP addresses are looked up in the ARP table. */
802 /* broadcast destination IP address? */
803 if (ip4_addr_isbroadcast(ipaddr, netif)) {
804 /* broadcast on Ethernet also */
805 dest = (const struct eth_addr *)&ethbroadcast;
806 /* multicast destination IP address? */
807 } else if (ip4_addr_ismulticast(ipaddr)) {
808 /* Hash IP multicast address to MAC address.*/
809 mcastaddr.addr[0] = LL_IP4_MULTICAST_ADDR_0;
810 mcastaddr.addr[1] = LL_IP4_MULTICAST_ADDR_1;
811 mcastaddr.addr[2] = LL_IP4_MULTICAST_ADDR_2;
812 mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
813 mcastaddr.addr[4] = ip4_addr3(ipaddr);
814 mcastaddr.addr[5] = ip4_addr4(ipaddr);
815 /* destination Ethernet address is multicast */
816 dest = &mcastaddr;
817 /* unicast destination IP address? */
818 } else {
819 s8_t i;
820 /* outside local network? if so, this can neither be a global broadcast nor
821 a subnet broadcast. */
822 if (!ip4_addr_netcmp(ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) &&
823 !ip4_addr_islinklocal(ipaddr)) {
824 #if LWIP_AUTOIP
825 struct ip_hdr *iphdr = LWIP_ALIGNMENT_CAST(struct ip_hdr*, q->payload);
826 /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with
827 a link-local source address must always be "directly to its destination
828 on the same physical link. The host MUST NOT send the packet to any
829 router for forwarding". */
830 if (!ip4_addr_islinklocal(&iphdr->src))
831 #endif /* LWIP_AUTOIP */
833 #ifdef LWIP_HOOK_ETHARP_GET_GW
834 /* For advanced routing, a single default gateway might not be enough, so get
835 the IP address of the gateway to handle the current destination address. */
836 dst_addr = LWIP_HOOK_ETHARP_GET_GW(netif, ipaddr);
837 if (dst_addr == NULL)
838 #endif /* LWIP_HOOK_ETHARP_GET_GW */
840 /* interface has default gateway? */
841 if (!ip4_addr_isany_val(*netif_ip4_gw(netif))) {
842 /* send to hardware address of default gateway IP address */
843 dst_addr = netif_ip4_gw(netif);
844 /* no default gateway available */
845 } else {
846 /* no route to destination error (default gateway missing) */
847 return ERR_RTE;
852 #if LWIP_NETIF_HWADDRHINT
853 if (netif->addr_hint != NULL) {
854 /* per-pcb cached entry was given */
855 u8_t etharp_cached_entry = *(netif->addr_hint);
856 if (etharp_cached_entry < ARP_TABLE_SIZE) {
857 #endif /* LWIP_NETIF_HWADDRHINT */
858 if ((arp_table[etharp_cached_entry].state >= ETHARP_STATE_STABLE) &&
859 #if ETHARP_TABLE_MATCH_NETIF
860 (arp_table[etharp_cached_entry].netif == netif) &&
861 #endif
862 (ip4_addr_cmp(dst_addr, &arp_table[etharp_cached_entry].ipaddr))) {
863 /* the per-pcb-cached entry is stable and the right one! */
864 ETHARP_STATS_INC(etharp.cachehit);
865 return etharp_output_to_arp_index(netif, q, etharp_cached_entry);
867 #if LWIP_NETIF_HWADDRHINT
870 #endif /* LWIP_NETIF_HWADDRHINT */
872 /* find stable entry: do this here since this is a critical path for
873 throughput and etharp_find_entry() is kind of slow */
874 for (i = 0; i < ARP_TABLE_SIZE; i++) {
875 if ((arp_table[i].state >= ETHARP_STATE_STABLE) &&
876 #if ETHARP_TABLE_MATCH_NETIF
877 (arp_table[i].netif == netif) &&
878 #endif
879 (ip4_addr_cmp(dst_addr, &arp_table[i].ipaddr))) {
880 /* found an existing, stable entry */
881 ETHARP_SET_HINT(netif, i);
882 return etharp_output_to_arp_index(netif, q, i);
885 /* no stable entry found, use the (slower) query function:
886 queue on destination Ethernet address belonging to ipaddr */
887 return etharp_query(netif, dst_addr, q);
890 /* continuation for multicast/broadcast destinations */
891 /* obtain source Ethernet address of the given interface */
892 /* send packet directly on the link */
893 return ethernet_output(netif, q, (struct eth_addr*)(netif->hwaddr), dest, ETHTYPE_IP);
897 * Send an ARP request for the given IP address and/or queue a packet.
899 * If the IP address was not yet in the cache, a pending ARP cache entry
900 * is added and an ARP request is sent for the given address. The packet
901 * is queued on this entry.
903 * If the IP address was already pending in the cache, a new ARP request
904 * is sent for the given address. The packet is queued on this entry.
906 * If the IP address was already stable in the cache, and a packet is
907 * given, it is directly sent and no ARP request is sent out.
909 * If the IP address was already stable in the cache, and no packet is
910 * given, an ARP request is sent out.
912 * @param netif The lwIP network interface on which ipaddr
913 * must be queried for.
914 * @param ipaddr The IP address to be resolved.
915 * @param q If non-NULL, a pbuf that must be delivered to the IP address.
916 * q is not freed by this function.
918 * @note q must only be ONE packet, not a packet queue!
920 * @return
921 * - ERR_BUF Could not make room for Ethernet header.
922 * - ERR_MEM Hardware address unknown, and no more ARP entries available
923 * to query for address or queue the packet.
924 * - ERR_MEM Could not queue packet due to memory shortage.
925 * - ERR_RTE No route to destination (no gateway to external networks).
926 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
929 err_t
930 etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q)
932 struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
933 err_t result = ERR_MEM;
934 int is_new_entry = 0;
935 s8_t i; /* ARP entry index */
937 /* non-unicast address? */
938 if (ip4_addr_isbroadcast(ipaddr, netif) ||
939 ip4_addr_ismulticast(ipaddr) ||
940 ip4_addr_isany(ipaddr)) {
941 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
942 return ERR_ARG;
945 /* find entry in ARP cache, ask to create entry if queueing packet */
946 i = etharp_find_entry(ipaddr, ETHARP_FLAG_TRY_HARD, netif);
948 /* could not find or create entry? */
949 if (i < 0) {
950 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
951 if (q) {
952 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n"));
953 ETHARP_STATS_INC(etharp.memerr);
955 return (err_t)i;
958 /* mark a fresh entry as pending (we just sent a request) */
959 if (arp_table[i].state == ETHARP_STATE_EMPTY) {
960 is_new_entry = 1;
961 arp_table[i].state = ETHARP_STATE_PENDING;
962 /* record network interface for re-sending arp request in etharp_tmr */
963 arp_table[i].netif = netif;
966 /* { i is either a STABLE or (new or existing) PENDING entry } */
967 LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
968 ((arp_table[i].state == ETHARP_STATE_PENDING) ||
969 (arp_table[i].state >= ETHARP_STATE_STABLE)));
971 /* do we have a new entry? or an implicit query request? */
972 if (is_new_entry || (q == NULL)) {
973 /* try to resolve it; send out ARP request */
974 result = etharp_request(netif, ipaddr);
975 if (result != ERR_OK) {
976 /* ARP request couldn't be sent */
977 /* We don't re-send arp request in etharp_tmr, but we still queue packets,
978 since this failure could be temporary, and the next packet calling
979 etharp_query again could lead to sending the queued packets. */
981 if (q == NULL) {
982 return result;
986 /* packet given? */
987 LWIP_ASSERT("q != NULL", q != NULL);
988 /* stable entry? */
989 if (arp_table[i].state >= ETHARP_STATE_STABLE) {
990 /* we have a valid IP->Ethernet address mapping */
991 ETHARP_SET_HINT(netif, i);
992 /* send the packet */
993 result = ethernet_output(netif, q, srcaddr, &(arp_table[i].ethaddr), ETHTYPE_IP);
994 /* pending entry? (either just created or already pending */
995 } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
996 /* entry is still pending, queue the given packet 'q' */
997 struct pbuf *p;
998 int copy_needed = 0;
999 /* IF q includes a pbuf that must be copied, copy the whole chain into a
1000 * new PBUF_RAM. See the definition of PBUF_NEEDS_COPY for details. */
1001 p = q;
1002 while (p) {
1003 LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0));
1004 if (PBUF_NEEDS_COPY(p)) {
1005 copy_needed = 1;
1006 break;
1008 p = p->next;
1010 if (copy_needed) {
1011 /* copy the whole packet into new pbufs */
1012 p = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
1013 if (p != NULL) {
1014 if (pbuf_copy(p, q) != ERR_OK) {
1015 pbuf_free(p);
1016 p = NULL;
1019 } else {
1020 /* referencing the old pbuf is enough */
1021 p = q;
1022 pbuf_ref(p);
1024 /* packet could be taken over? */
1025 if (p != NULL) {
1026 /* queue packet ... */
1027 #if ARP_QUEUEING
1028 struct etharp_q_entry *new_entry;
1029 /* allocate a new arp queue entry */
1030 new_entry = (struct etharp_q_entry *)memp_malloc(MEMP_ARP_QUEUE);
1031 if (new_entry != NULL) {
1032 unsigned int qlen = 0;
1033 new_entry->next = 0;
1034 new_entry->p = p;
1035 if (arp_table[i].q != NULL) {
1036 /* queue was already existent, append the new entry to the end */
1037 struct etharp_q_entry *r;
1038 r = arp_table[i].q;
1039 qlen++;
1040 while (r->next != NULL) {
1041 r = r->next;
1042 qlen++;
1044 r->next = new_entry;
1045 } else {
1046 /* queue did not exist, first item in queue */
1047 arp_table[i].q = new_entry;
1049 #if ARP_QUEUE_LEN
1050 if (qlen >= ARP_QUEUE_LEN) {
1051 struct etharp_q_entry *old;
1052 old = arp_table[i].q;
1053 arp_table[i].q = arp_table[i].q->next;
1054 pbuf_free(old->p);
1055 memp_free(MEMP_ARP_QUEUE, old);
1057 #endif
1058 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
1059 result = ERR_OK;
1060 } else {
1061 /* the pool MEMP_ARP_QUEUE is empty */
1062 pbuf_free(p);
1063 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));
1064 result = ERR_MEM;
1066 #else /* ARP_QUEUEING */
1067 /* always queue one packet per ARP request only, freeing a previously queued packet */
1068 if (arp_table[i].q != NULL) {
1069 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: dropped previously queued packet %p for ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
1070 pbuf_free(arp_table[i].q);
1072 arp_table[i].q = p;
1073 result = ERR_OK;
1074 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
1075 #endif /* ARP_QUEUEING */
1076 } else {
1077 ETHARP_STATS_INC(etharp.memerr);
1078 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));
1079 result = ERR_MEM;
1082 return result;
1086 * Send a raw ARP packet (opcode and all addresses can be modified)
1088 * @param netif the lwip network interface on which to send the ARP packet
1089 * @param ethsrc_addr the source MAC address for the ethernet header
1090 * @param ethdst_addr the destination MAC address for the ethernet header
1091 * @param hwsrc_addr the source MAC address for the ARP protocol header
1092 * @param ipsrc_addr the source IP address for the ARP protocol header
1093 * @param hwdst_addr the destination MAC address for the ARP protocol header
1094 * @param ipdst_addr the destination IP address for the ARP protocol header
1095 * @param opcode the type of the ARP packet
1096 * @return ERR_OK if the ARP packet has been sent
1097 * ERR_MEM if the ARP packet couldn't be allocated
1098 * any other err_t on failure
1100 static err_t
1101 etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
1102 const struct eth_addr *ethdst_addr,
1103 const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr,
1104 const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr,
1105 const u16_t opcode)
1107 struct pbuf *p;
1108 err_t result = ERR_OK;
1109 struct etharp_hdr *hdr;
1111 LWIP_ASSERT("netif != NULL", netif != NULL);
1113 /* allocate a pbuf for the outgoing ARP request packet */
1114 p = pbuf_alloc(PBUF_LINK, SIZEOF_ETHARP_HDR, PBUF_RAM);
1115 /* could allocate a pbuf for an ARP request? */
1116 if (p == NULL) {
1117 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
1118 ("etharp_raw: could not allocate pbuf for ARP request.\n"));
1119 ETHARP_STATS_INC(etharp.memerr);
1120 return ERR_MEM;
1122 LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
1123 (p->len >= SIZEOF_ETHARP_HDR));
1125 hdr = (struct etharp_hdr *)p->payload;
1126 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n"));
1127 hdr->opcode = lwip_htons(opcode);
1129 LWIP_ASSERT("netif->hwaddr_len must be the same as ETH_HWADDR_LEN for etharp!",
1130 (netif->hwaddr_len == ETH_HWADDR_LEN));
1132 /* Write the ARP MAC-Addresses */
1133 ETHADDR16_COPY(&hdr->shwaddr, hwsrc_addr);
1134 ETHADDR16_COPY(&hdr->dhwaddr, hwdst_addr);
1135 /* Copy struct ip4_addr_wordaligned to aligned ip4_addr, to support compilers without
1136 * structure packing. */
1137 IPADDR_WORDALIGNED_COPY_FROM_IP4_ADDR_T(&hdr->sipaddr, ipsrc_addr);
1138 IPADDR_WORDALIGNED_COPY_FROM_IP4_ADDR_T(&hdr->dipaddr, ipdst_addr);
1140 hdr->hwtype = PP_HTONS(HWTYPE_ETHERNET);
1141 hdr->proto = PP_HTONS(ETHTYPE_IP);
1142 /* set hwlen and protolen */
1143 hdr->hwlen = ETH_HWADDR_LEN;
1144 hdr->protolen = sizeof(ip4_addr_t);
1146 /* send ARP query */
1147 #if LWIP_AUTOIP
1148 /* If we are using Link-Local, all ARP packets that contain a Link-Local
1149 * 'sender IP address' MUST be sent using link-layer broadcast instead of
1150 * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
1151 if(ip4_addr_islinklocal(ipsrc_addr)) {
1152 ethernet_output(netif, p, ethsrc_addr, &ethbroadcast, ETHTYPE_ARP);
1153 } else
1154 #endif /* LWIP_AUTOIP */
1156 ethernet_output(netif, p, ethsrc_addr, ethdst_addr, ETHTYPE_ARP);
1159 ETHARP_STATS_INC(etharp.xmit);
1160 /* free ARP query packet */
1161 pbuf_free(p);
1162 p = NULL;
1163 /* could not allocate pbuf for ARP request */
1165 return result;
1169 * Send an ARP request packet asking for ipaddr to a specific eth address.
1170 * Used to send unicast request to refresh the ARP table just before an entry
1171 * times out
1173 * @param netif the lwip network interface on which to send the request
1174 * @param ipaddr the IP address for which to ask
1175 * @param hw_dst_addr the ethernet address to send this packet to
1176 * @return ERR_OK if the request has been sent
1177 * ERR_MEM if the ARP packet couldn't be allocated
1178 * any other err_t on failure
1180 static err_t
1181 etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr* hw_dst_addr)
1183 return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, hw_dst_addr,
1184 (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif), &ethzero,
1185 ipaddr, ARP_REQUEST);
1189 * Send an ARP request packet asking for ipaddr.
1191 * @param netif the lwip network interface on which to send the request
1192 * @param ipaddr the IP address for which to ask
1193 * @return ERR_OK if the request has been sent
1194 * ERR_MEM if the ARP packet couldn't be allocated
1195 * any other err_t on failure
1197 err_t
1198 etharp_request(struct netif *netif, const ip4_addr_t *ipaddr)
1200 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
1201 return etharp_request_dst(netif, ipaddr, &ethbroadcast);
1203 #endif /* LWIP_IPV4 && LWIP_ARP */
1205 #endif /* LWIP_ARP || LWIP_ETHERNET */