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_query(our_netif, its_ip_addr, NULL) upon
16 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
17 * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv>
18 * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
19 * All rights reserved.
21 * Redistribution and use in source and binary forms, with or without modification,
22 * are permitted provided that the following conditions are met:
24 * 1. Redistributions of source code must retain the above copyright notice,
25 * this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright notice,
27 * this list of conditions and the following disclaimer in the documentation
28 * and/or other materials provided with the distribution.
29 * 3. The name of the author may not be used to endorse or promote products
30 * derived from this software without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
35 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
37 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
43 * This file is part of the lwIP TCP/IP stack.
49 #if LWIP_ARP /* don't build if not configured for use in lwipopts.h */
51 #include "lwip/inet.h"
53 #include "lwip/stats.h"
54 #include "lwip/snmp.h"
55 #include "lwip/dhcp.h"
56 #include "lwip/autoip.h"
57 #include "netif/etharp.h"
60 #include "netif/ppp_oe.h"
61 #endif /* PPPOE_SUPPORT */
65 /** the time an ARP entry stays valid after its last update,
66 * for ARP_TMR_INTERVAL = 5000, this is
67 * (240 * 5) seconds = 20 minutes.
69 #define ARP_MAXAGE 240
70 /** the time an ARP entry stays pending after first request,
71 * for ARP_TMR_INTERVAL = 5000, this is
72 * (2 * 5) seconds = 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 2
79 #define HWTYPE_ETHERNET 1
81 #define ARPH_HWLEN(hdr) (ntohs((hdr)->_hwlen_protolen) >> 8)
82 #define ARPH_PROTOLEN(hdr) (ntohs((hdr)->_hwlen_protolen) & 0xff)
84 #define ARPH_HWLEN_SET(hdr, len) (hdr)->_hwlen_protolen = htons(ARPH_PROTOLEN(hdr) | ((len) << 8))
85 #define ARPH_PROTOLEN_SET(hdr, len) (hdr)->_hwlen_protolen = htons((len) | (ARPH_HWLEN(hdr) << 8))
88 ETHARP_STATE_EMPTY = 0,
96 * Pointer to queue of pending outgoing packets on this ARP entry.
98 struct etharp_q_entry *q;
100 struct ip_addr ipaddr;
101 struct eth_addr ethaddr;
102 enum etharp_state state;
107 const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
108 const struct eth_addr ethzero = {{0,0,0,0,0,0}};
109 static struct etharp_entry arp_table[ARP_TABLE_SIZE];
110 #if !LWIP_NETIF_HWADDRHINT
111 static u8_t etharp_cached_entry;
115 * Try hard to create a new entry - we want the IP address to appear in
116 * the cache (even if this means removing an active entry or so). */
117 #define ETHARP_TRY_HARD 1
118 #define ETHARP_FIND_ONLY 2
120 #if LWIP_NETIF_HWADDRHINT
121 #define NETIF_SET_HINT(netif, hint) if (((netif) != NULL) && ((netif)->addr_hint != NULL)) \
122 *((netif)->addr_hint) = (hint);
123 static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags, struct netif *netif);
124 #else /* LWIP_NETIF_HWADDRHINT */
125 static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags);
126 #endif /* LWIP_NETIF_HWADDRHINT */
128 static err_t update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags);
131 /* Some checks, instead of etharp_init(): */
132 #if (LWIP_ARP && (ARP_TABLE_SIZE > 0x7f))
133 #error "If you want to use ARP, ARP_TABLE_SIZE must fit in an s8_t, so, you have to reduce it in your lwipopts.h"
139 * Free a complete queue of etharp entries
141 * @param q a qeueue of etharp_q_entry's to free
144 free_etharp_q(struct etharp_q_entry *q)
146 struct etharp_q_entry *r;
147 LWIP_ASSERT("q != NULL", q != NULL);
148 LWIP_ASSERT("q->p != NULL", q->p != NULL);
152 LWIP_ASSERT("r->p != NULL", (r->p != NULL));
154 memp_free(MEMP_ARP_QUEUE, r);
160 * Clears expired entries in the ARP table.
162 * This function should be called every ETHARP_TMR_INTERVAL microseconds (5 seconds),
163 * in order to expire entries in the ARP table.
170 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
171 /* remove expired entries from the ARP table */
172 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
173 arp_table[i].ctime++;
174 if (((arp_table[i].state == ETHARP_STATE_STABLE) &&
175 (arp_table[i].ctime >= ARP_MAXAGE)) ||
176 ((arp_table[i].state == ETHARP_STATE_PENDING) &&
177 (arp_table[i].ctime >= ARP_MAXPENDING))) {
178 /* pending or stable entry has become old! */
179 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %"U16_F".\n",
180 arp_table[i].state == ETHARP_STATE_STABLE ? "stable" : "pending", (u16_t)i));
181 /* clean up entries that have just been expired */
182 /* remove from SNMP ARP index tree */
183 snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr);
185 /* and empty packet queue */
186 if (arp_table[i].q != NULL) {
187 /* remove all queued packets */
188 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q)));
189 free_etharp_q(arp_table[i].q);
190 arp_table[i].q = NULL;
193 /* recycle entry for re-use */
194 arp_table[i].state = ETHARP_STATE_EMPTY;
197 /* still pending entry? (not expired) */
198 if (arp_table[i].state == ETHARP_STATE_PENDING) {
199 /* resend an ARP query here? */
206 * Search the ARP table for a matching or new entry.
208 * If an IP address is given, return a pending or stable ARP entry that matches
209 * the address. If no match is found, create a new entry with this address set,
210 * but in state ETHARP_EMPTY. The caller must check and possibly change the
211 * state of the returned entry.
213 * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
215 * In all cases, attempt to create new entries from an empty entry. If no
216 * empty entries are available and ETHARP_TRY_HARD flag is set, recycle
217 * old entries. Heuristic choose the least important entry for recycling.
219 * @param ipaddr IP address to find in ARP cache, or to add if not found.
221 * - ETHARP_TRY_HARD: Try hard to create a entry by allowing recycling of
222 * active (stable or pending) entries.
224 * @return The ARP entry index that matched or is created, ERR_MEM if no
225 * entry is found or could be recycled.
228 #if LWIP_NETIF_HWADDRHINT
229 find_entry(struct ip_addr *ipaddr, u8_t flags, struct netif *netif)
230 #else /* LWIP_NETIF_HWADDRHINT */
231 find_entry(struct ip_addr *ipaddr, u8_t flags)
232 #endif /* LWIP_NETIF_HWADDRHINT */
234 s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
235 s8_t empty = ARP_TABLE_SIZE;
236 u8_t i = 0, age_pending = 0, age_stable = 0;
238 /* oldest entry with packets on queue */
239 s8_t old_queue = ARP_TABLE_SIZE;
244 /* First, test if the last call to this function asked for the
245 * same address. If so, we're really fast! */
247 /* ipaddr to search for was given */
248 #if LWIP_NETIF_HWADDRHINT
249 if ((netif != NULL) && (netif->addr_hint != NULL)) {
250 /* per-pcb cached entry was given */
251 u8_t per_pcb_cache = *(netif->addr_hint);
252 if ((per_pcb_cache < ARP_TABLE_SIZE) && arp_table[per_pcb_cache].state == ETHARP_STATE_STABLE) {
253 /* the per-pcb-cached entry is stable */
254 if (ip_addr_cmp(ipaddr, &arp_table[per_pcb_cache].ipaddr)) {
255 /* per-pcb cached entry was the right one! */
256 ETHARP_STATS_INC(etharp.cachehit);
257 return per_pcb_cache;
261 #else /* #if LWIP_NETIF_HWADDRHINT */
262 if (arp_table[etharp_cached_entry].state == ETHARP_STATE_STABLE) {
263 /* the cached entry is stable */
264 if (ip_addr_cmp(ipaddr, &arp_table[etharp_cached_entry].ipaddr)) {
265 /* cached entry was the right one! */
266 ETHARP_STATS_INC(etharp.cachehit);
267 return etharp_cached_entry;
270 #endif /* #if LWIP_NETIF_HWADDRHINT */
274 * a) do a search through the cache, remember candidates
275 * b) select candidate entry
276 * c) create new entry
279 /* a) in a single search sweep, do all of this
280 * 1) remember the first empty entry (if any)
281 * 2) remember the oldest stable entry (if any)
282 * 3) remember the oldest pending entry without queued packets (if any)
283 * 4) remember the oldest pending entry with queued packets (if any)
284 * 5) search for a matching IP entry, either pending or stable
285 * until 5 matches, or all entries are searched for.
288 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
289 /* no empty entry found yet and now we do find one? */
290 if ((empty == ARP_TABLE_SIZE) && (arp_table[i].state == ETHARP_STATE_EMPTY)) {
291 LWIP_DEBUGF(ETHARP_DEBUG, ("find_entry: found empty entry %"U16_F"\n", (u16_t)i));
292 /* remember first empty entry */
296 else if (arp_table[i].state == ETHARP_STATE_PENDING) {
297 /* if given, does IP address match IP address in ARP entry? */
298 if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
299 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: found matching pending entry %"U16_F"\n", (u16_t)i));
300 /* found exact IP address match, simply bail out */
301 #if LWIP_NETIF_HWADDRHINT
302 NETIF_SET_HINT(netif, i);
303 #else /* #if LWIP_NETIF_HWADDRHINT */
304 etharp_cached_entry = i;
305 #endif /* #if LWIP_NETIF_HWADDRHINT */
308 /* pending with queued packets? */
309 } else if (arp_table[i].q != NULL) {
310 if (arp_table[i].ctime >= age_queue) {
312 age_queue = arp_table[i].ctime;
315 /* pending without queued packets? */
317 if (arp_table[i].ctime >= age_pending) {
319 age_pending = arp_table[i].ctime;
324 else if (arp_table[i].state == ETHARP_STATE_STABLE) {
325 /* if given, does IP address match IP address in ARP entry? */
326 if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
327 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: found matching stable entry %"U16_F"\n", (u16_t)i));
328 /* found exact IP address match, simply bail out */
329 #if LWIP_NETIF_HWADDRHINT
330 NETIF_SET_HINT(netif, i);
331 #else /* #if LWIP_NETIF_HWADDRHINT */
332 etharp_cached_entry = i;
333 #endif /* #if LWIP_NETIF_HWADDRHINT */
335 /* remember entry with oldest stable entry in oldest, its age in maxtime */
336 } else if (arp_table[i].ctime >= age_stable) {
338 age_stable = arp_table[i].ctime;
342 /* { we have no match } => try to create a new entry */
344 /* no empty entry found and not allowed to recycle? */
345 if (((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_TRY_HARD) == 0))
346 /* or don't create new entry, only search? */
347 || ((flags & ETHARP_FIND_ONLY) != 0)) {
348 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: no empty entry found and not allowed to recycle\n"));
349 return (s8_t)ERR_MEM;
352 /* b) choose the least destructive entry to recycle:
354 * 2) oldest stable entry
355 * 3) oldest pending entry without queued packets
356 * 4) oldest pending entry without queued packets
358 * { ETHARP_TRY_HARD is set at this point }
361 /* 1) empty entry available? */
362 if (empty < ARP_TABLE_SIZE) {
364 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting empty entry %"U16_F"\n", (u16_t)i));
366 /* 2) found recyclable stable entry? */
367 else if (old_stable < ARP_TABLE_SIZE) {
368 /* recycle oldest stable*/
370 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest stable entry %"U16_F"\n", (u16_t)i));
372 /* no queued packets should exist on stable entries */
373 LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL);
375 /* 3) found recyclable pending entry without queued packets? */
376 } else if (old_pending < ARP_TABLE_SIZE) {
377 /* recycle oldest pending */
379 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest pending entry %"U16_F" (without queue)\n", (u16_t)i));
381 /* 4) found recyclable pending entry with queued packets? */
382 } else if (old_queue < ARP_TABLE_SIZE) {
383 /* recycle oldest pending */
385 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest pending entry %"U16_F", freeing packet queue %p\n", (u16_t)i, (void *)(arp_table[i].q)));
386 free_etharp_q(arp_table[i].q);
387 arp_table[i].q = NULL;
389 /* no empty or recyclable entries found */
391 return (s8_t)ERR_MEM;
394 /* { empty or recyclable entry found } */
395 LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
397 if (arp_table[i].state != ETHARP_STATE_EMPTY)
399 snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr);
401 /* recycle entry (no-op for an already empty entry) */
402 arp_table[i].state = ETHARP_STATE_EMPTY;
404 /* IP address given? */
405 if (ipaddr != NULL) {
407 ip_addr_set(&arp_table[i].ipaddr, ipaddr);
409 arp_table[i].ctime = 0;
410 #if LWIP_NETIF_HWADDRHINT
411 NETIF_SET_HINT(netif, i);
412 #else /* #if LWIP_NETIF_HWADDRHINT */
413 etharp_cached_entry = i;
414 #endif /* #if LWIP_NETIF_HWADDRHINT */
419 * Send an IP packet on the network using netif->linkoutput
420 * The ethernet header is filled in before sending.
422 * @params netif the lwIP network interface on which to send the packet
423 * @params p the packet to send, p->payload pointing to the (uninitialized) ethernet header
424 * @params src the source MAC address to be copied into the ethernet header
425 * @params dst the destination MAC address to be copied into the ethernet header
426 * @return ERR_OK if the packet was sent, any other err_t on failure
429 etharp_send_ip(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
431 struct eth_hdr *ethhdr = p->payload;
434 LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
435 (netif->hwaddr_len == ETHARP_HWADDR_LEN));
436 k = ETHARP_HWADDR_LEN;
439 ethhdr->dest.addr[k] = dst->addr[k];
440 ethhdr->src.addr[k] = src->addr[k];
442 ethhdr->type = htons(ETHTYPE_IP);
443 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_send_ip: sending packet %p\n", (void *)p));
444 /* send the packet */
445 return netif->linkoutput(netif, p);
449 * Update (or insert) a IP/MAC address pair in the ARP cache.
451 * If a pending entry is resolved, any queued packets will be sent
454 * @param ipaddr IP address of the inserted ARP entry.
455 * @param ethaddr Ethernet address of the inserted ARP entry.
456 * @param flags Defines behaviour:
457 * - ETHARP_TRY_HARD Allows ARP to insert this as a new item. If not specified,
458 * only existing ARP entries will be updated.
461 * - ERR_OK Succesfully updated ARP cache.
462 * - ERR_MEM If we could not add a new ARP entry when ETHARP_TRY_HARD was set.
463 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
468 update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags)
472 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | 3, ("update_arp_entry()\n"));
473 LWIP_ASSERT("netif->hwaddr_len == ETHARP_HWADDR_LEN", netif->hwaddr_len == ETHARP_HWADDR_LEN);
474 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n",
475 ip4_addr1(ipaddr), ip4_addr2(ipaddr), ip4_addr3(ipaddr), ip4_addr4(ipaddr),
476 ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
477 ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
478 /* non-unicast address? */
479 if (ip_addr_isany(ipaddr) ||
480 ip_addr_isbroadcast(ipaddr, netif) ||
481 ip_addr_ismulticast(ipaddr)) {
482 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
485 /* find or create ARP entry */
486 #if LWIP_NETIF_HWADDRHINT
487 i = find_entry(ipaddr, flags, netif);
488 #else /* LWIP_NETIF_HWADDRHINT */
489 i = find_entry(ipaddr, flags);
490 #endif /* LWIP_NETIF_HWADDRHINT */
491 /* bail out if no entry could be found */
496 arp_table[i].state = ETHARP_STATE_STABLE;
497 /* record network interface */
498 arp_table[i].netif = netif;
500 /* insert in SNMP ARP index tree */
501 snmp_insert_arpidx_tree(netif, &arp_table[i].ipaddr);
503 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i));
505 k = ETHARP_HWADDR_LEN;
508 arp_table[i].ethaddr.addr[k] = ethaddr->addr[k];
510 /* reset time stamp */
511 arp_table[i].ctime = 0;
513 /* this is where we will send out queued packets! */
514 while (arp_table[i].q != NULL) {
516 /* remember remainder of queue */
517 struct etharp_q_entry *q = arp_table[i].q;
518 /* pop first item off the queue */
519 arp_table[i].q = q->next;
520 /* get the packet pointer */
522 /* now queue entry can be freed */
523 memp_free(MEMP_ARP_QUEUE, q);
524 /* send the queued IP packet */
525 etharp_send_ip(netif, p, (struct eth_addr*)(netif->hwaddr), ethaddr);
526 /* free the queued IP packet */
534 * Finds (stable) ethernet/IP address pair from ARP table
535 * using interface and IP address index.
536 * @note the addresses in the ARP table are in network order!
538 * @param netif points to interface index
539 * @param ipaddr points to the (network order) IP address index
540 * @param eth_ret points to return pointer
541 * @param ip_ret points to return pointer
542 * @return table index if found, -1 otherwise
545 etharp_find_addr(struct netif *netif, struct ip_addr *ipaddr,
546 struct eth_addr **eth_ret, struct ip_addr **ip_ret)
550 LWIP_UNUSED_ARG(netif);
552 #if LWIP_NETIF_HWADDRHINT
553 i = find_entry(ipaddr, ETHARP_FIND_ONLY, NULL);
554 #else /* LWIP_NETIF_HWADDRHINT */
555 i = find_entry(ipaddr, ETHARP_FIND_ONLY);
556 #endif /* LWIP_NETIF_HWADDRHINT */
557 if((i >= 0) && arp_table[i].state == ETHARP_STATE_STABLE) {
558 *eth_ret = &arp_table[i].ethaddr;
559 *ip_ret = &arp_table[i].ipaddr;
566 * Updates the ARP table using the given IP packet.
568 * Uses the incoming IP packet's source address to update the
569 * ARP cache for the local network. The function does not alter
570 * or free the packet. This function must be called before the
571 * packet p is passed to the IP layer.
573 * @param netif The lwIP network interface on which the IP packet pbuf arrived.
574 * @param p The IP packet that arrived on netif.
581 etharp_ip_input(struct netif *netif, struct pbuf *p)
583 struct ethip_hdr *hdr;
584 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
585 /* Only insert an entry if the source IP address of the
586 incoming IP packet comes from a host on the local network. */
588 /* source is not on the local network? */
589 if (!ip_addr_netcmp(&(hdr->ip.src), &(netif->ip_addr), &(netif->netmask))) {
594 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n"));
595 /* update ARP table */
596 /* @todo We could use ETHARP_TRY_HARD if we think we are going to talk
597 * back soon (for example, if the destination IP address is ours. */
598 update_arp_entry(netif, &(hdr->ip.src), &(hdr->eth.src), 0);
603 * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache
604 * send out queued IP packets. Updates cache with snooped address pairs.
606 * Should be called for incoming ARP packets. The pbuf in the argument
607 * is freed by this function.
609 * @param netif The lwIP network interface on which the ARP packet pbuf arrived.
610 * @param ethaddr Ethernet address of netif.
611 * @param p The ARP packet that arrived on netif. Is freed by this function.
618 etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p)
620 struct etharp_hdr *hdr;
621 /* these are aligned properly, whereas the ARP header fields might not be */
622 struct ip_addr sipaddr, dipaddr;
626 const u8_t * ethdst_hwaddr;
627 #endif /* LWIP_AUTOIP */
629 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
631 /* drop short ARP packets: we have to check for p->len instead of p->tot_len here
632 since a struct etharp_hdr is pointed to p->payload, so it musn't be chained! */
633 if (p->len < sizeof(struct etharp_hdr)) {
634 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | 1, ("etharp_arp_input: packet dropped, too short (%"S16_F"/%"S16_F")\n", p->tot_len, (s16_t)sizeof(struct etharp_hdr)));
635 ETHARP_STATS_INC(etharp.lenerr);
636 ETHARP_STATS_INC(etharp.drop);
643 /* RFC 826 "Packet Reception": */
644 if ((hdr->hwtype != htons(HWTYPE_ETHERNET)) ||
645 (hdr->_hwlen_protolen != htons((ETHARP_HWADDR_LEN << 8) | sizeof(struct ip_addr))) ||
646 (hdr->proto != htons(ETHTYPE_IP)) ||
647 (hdr->ethhdr.type != htons(ETHTYPE_ARP))) {
648 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | 1,
649 ("etharp_arp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n",
650 hdr->hwtype, ARPH_HWLEN(hdr), hdr->proto, ARPH_PROTOLEN(hdr), hdr->ethhdr.type));
651 ETHARP_STATS_INC(etharp.proterr);
652 ETHARP_STATS_INC(etharp.drop);
656 ETHARP_STATS_INC(etharp.recv);
659 /* We have to check if a host already has configured our random
660 * created link local address and continously check if there is
661 * a host with this IP-address so we can detect collisions */
662 autoip_arp_reply(netif, hdr);
663 #endif /* LWIP_AUTOIP */
665 /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
666 * structure packing (not using structure copy which breaks strict-aliasing rules). */
667 SMEMCPY(&sipaddr, &hdr->sipaddr, sizeof(sipaddr));
668 SMEMCPY(&dipaddr, &hdr->dipaddr, sizeof(dipaddr));
670 /* this interface is not configured? */
671 if (netif->ip_addr.addr == 0) {
674 /* ARP packet directed to us? */
675 for_us = ip_addr_cmp(&dipaddr, &(netif->ip_addr));
678 /* ARP message directed to us? */
680 /* add IP address in ARP cache; assume requester wants to talk to us.
681 * can result in directly sending the queued packets for this host. */
682 update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), ETHARP_TRY_HARD);
683 /* ARP message not directed to us? */
685 /* update the source IP address in the cache, if present */
686 update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), 0);
689 /* now act on the message itself */
690 switch (htons(hdr->opcode)) {
693 /* ARP request. If it asked for our address, we send out a
694 * reply. In any case, we time-stamp any existing ARP entry,
695 * and possiby send out an IP packet that was queued on it. */
697 LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP request\n"));
698 /* ARP request for our address? */
701 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: replying to ARP request for our IP address\n"));
702 /* Re-use pbuf to send ARP reply.
703 Since we are re-using an existing pbuf, we can't call etharp_raw since
704 that would allocate a new pbuf. */
705 hdr->opcode = htons(ARP_REPLY);
707 hdr->dipaddr = hdr->sipaddr;
708 hdr->sipaddr = *(struct ip_addr2 *)&netif->ip_addr;
710 LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
711 (netif->hwaddr_len == ETHARP_HWADDR_LEN));
712 i = ETHARP_HWADDR_LEN;
714 /* If we are using Link-Local, ARP packets must be broadcast on the
715 * link layer. (See RFC3927 Section 2.5) */
716 ethdst_hwaddr = ((netif->autoip != NULL) && (netif->autoip->state != AUTOIP_STATE_OFF)) ? (u8_t*)(ethbroadcast.addr) : hdr->shwaddr.addr;
717 #endif /* LWIP_AUTOIP */
721 hdr->dhwaddr.addr[i] = hdr->shwaddr.addr[i];
723 hdr->ethhdr.dest.addr[i] = ethdst_hwaddr[i];
724 #else /* LWIP_AUTOIP */
725 hdr->ethhdr.dest.addr[i] = hdr->shwaddr.addr[i];
726 #endif /* LWIP_AUTOIP */
727 hdr->shwaddr.addr[i] = ethaddr->addr[i];
728 hdr->ethhdr.src.addr[i] = ethaddr->addr[i];
731 /* hwtype, hwaddr_len, proto, protolen and the type in the ethernet header
732 are already correct, we tested that before */
734 /* return ARP reply */
735 netif->linkoutput(netif, p);
736 /* we are not configured? */
737 } else if (netif->ip_addr.addr == 0) {
738 /* { for_us == 0 and netif->ip_addr.addr == 0 } */
739 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n"));
740 /* request was not directed to us */
742 /* { for_us == 0 and netif->ip_addr.addr != 0 } */
743 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP request was not for us.\n"));
747 /* ARP reply. We already updated the ARP cache earlier. */
748 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP reply\n"));
749 #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
750 /* DHCP wants to know about ARP replies from any host with an
751 * IP address also offered to us by the DHCP server. We do not
752 * want to take a duplicate IP address on a single network.
753 * @todo How should we handle redundant (fail-over) interfaces? */
754 dhcp_arp_reply(netif, &sipaddr);
758 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP unknown opcode type %"S16_F"\n", htons(hdr->opcode)));
759 ETHARP_STATS_INC(etharp.err);
762 /* free ARP packet */
767 * Resolve and fill-in Ethernet address header for outgoing IP packet.
769 * For IP multicast and broadcast, corresponding Ethernet addresses
770 * are selected and the packet is transmitted on the link.
772 * For unicast addresses, the packet is submitted to etharp_query(). In
773 * case the IP address is outside the local network, the IP address of
774 * the gateway is used.
776 * @param netif The lwIP network interface which the IP packet will be sent on.
777 * @param q The pbuf(s) containing the IP packet to be sent.
778 * @param ipaddr The IP address of the packet destination.
781 * - ERR_RTE No route to destination (no gateway to external networks),
782 * or the return type of either etharp_query() or etharp_send_ip().
785 etharp_output(struct netif *netif, struct pbuf *q, struct ip_addr *ipaddr)
787 struct eth_addr *dest, mcastaddr;
789 /* make room for Ethernet header - should not fail */
790 if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
792 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | 2, ("etharp_output: could not allocate room for header.\n"));
793 LINK_STATS_INC(link.lenerr);
797 /* assume unresolved Ethernet address */
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 (ip_addr_isbroadcast(ipaddr, netif)) {
804 /* broadcast on Ethernet also */
805 dest = (struct eth_addr *)ðbroadcast;
806 /* multicast destination IP address? */
807 } else if (ip_addr_ismulticast(ipaddr)) {
808 /* Hash IP multicast address to MAC address.*/
809 mcastaddr.addr[0] = 0x01;
810 mcastaddr.addr[1] = 0x00;
811 mcastaddr.addr[2] = 0x5e;
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 */
817 /* unicast destination IP address? */
819 /* outside local network? */
820 if (!ip_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask))) {
821 /* interface has default gateway? */
822 if (netif->gw.addr != 0) {
823 /* send to hardware address of default gateway IP address */
824 ipaddr = &(netif->gw);
825 /* no default gateway available */
827 /* no route to destination error (default gateway missing) */
831 /* queue on destination Ethernet address belonging to ipaddr */
832 return etharp_query(netif, ipaddr, q);
835 /* continuation for multicast/broadcast destinations */
836 /* obtain source Ethernet address of the given interface */
837 /* send packet directly on the link */
838 return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr), dest);
842 * Send an ARP request for the given IP address and/or queue a packet.
844 * If the IP address was not yet in the cache, a pending ARP cache entry
845 * is added and an ARP request is sent for the given address. The packet
846 * is queued on this entry.
848 * If the IP address was already pending in the cache, a new ARP request
849 * is sent for the given address. The packet is queued on this entry.
851 * If the IP address was already stable in the cache, and a packet is
852 * given, it is directly sent and no ARP request is sent out.
854 * If the IP address was already stable in the cache, and no packet is
855 * given, an ARP request is sent out.
857 * @param netif The lwIP network interface on which ipaddr
858 * must be queried for.
859 * @param ipaddr The IP address to be resolved.
860 * @param q If non-NULL, a pbuf that must be delivered to the IP address.
861 * q is not freed by this function.
863 * @note q must only be ONE packet, not a packet queue!
866 * - ERR_BUF Could not make room for Ethernet header.
867 * - ERR_MEM Hardware address unknown, and no more ARP entries available
868 * to query for address or queue the packet.
869 * - ERR_MEM Could not queue packet due to memory shortage.
870 * - ERR_RTE No route to destination (no gateway to external networks).
871 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
875 etharp_query(struct netif *netif, struct ip_addr *ipaddr, struct pbuf *q)
877 struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
878 err_t result = ERR_MEM;
879 s8_t i; /* ARP entry index */
881 /* non-unicast address? */
882 if (ip_addr_isbroadcast(ipaddr, netif) ||
883 ip_addr_ismulticast(ipaddr) ||
884 ip_addr_isany(ipaddr)) {
885 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
889 /* find entry in ARP cache, ask to create entry if queueing packet */
890 #if LWIP_NETIF_HWADDRHINT
891 i = find_entry(ipaddr, ETHARP_TRY_HARD, netif);
892 #else /* LWIP_NETIF_HWADDRHINT */
893 i = find_entry(ipaddr, ETHARP_TRY_HARD);
894 #endif /* LWIP_NETIF_HWADDRHINT */
896 /* could not find or create entry? */
898 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
900 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n"));
901 ETHARP_STATS_INC(etharp.memerr);
906 /* mark a fresh entry as pending (we just sent a request) */
907 if (arp_table[i].state == ETHARP_STATE_EMPTY) {
908 arp_table[i].state = ETHARP_STATE_PENDING;
911 /* { i is either a STABLE or (new or existing) PENDING entry } */
912 LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
913 ((arp_table[i].state == ETHARP_STATE_PENDING) ||
914 (arp_table[i].state == ETHARP_STATE_STABLE)));
916 /* do we have a pending entry? or an implicit query request? */
917 if ((arp_table[i].state == ETHARP_STATE_PENDING) || (q == NULL)) {
918 /* try to resolve it; send out ARP request */
919 result = etharp_request(netif, ipaddr);
920 if (result != ERR_OK) {
921 /* ARP request couldn't be sent */
922 /* We don't re-send arp request in etharp_tmr, but we still queue packets,
923 since this failure could be temporary, and the next packet calling
924 etharp_query again could lead to sending the queued packets. */
931 if (arp_table[i].state == ETHARP_STATE_STABLE) {
932 /* we have a valid IP->Ethernet address mapping */
933 /* send the packet */
934 result = etharp_send_ip(netif, q, srcaddr, &(arp_table[i].ethaddr));
935 /* pending entry? (either just created or already pending */
936 } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
937 #if ARP_QUEUEING /* queue the given q packet */
940 /* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but
941 * to copy the whole queue into a new PBUF_RAM (see bug #11400)
942 * PBUF_ROMs can be left as they are, since ROM must not get changed. */
945 LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0));
946 if(p->type != PBUF_ROM) {
953 /* copy the whole packet into new pbufs */
954 p = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
956 if (pbuf_copy(p, q) != ERR_OK) {
962 /* referencing the old pbuf is enough */
966 /* packet could be taken over? */
968 /* queue packet ... */
969 struct etharp_q_entry *new_entry;
970 /* allocate a new arp queue entry */
971 new_entry = memp_malloc(MEMP_ARP_QUEUE);
972 if (new_entry != NULL) {
975 if(arp_table[i].q != NULL) {
976 /* queue was already existent, append the new entry to the end */
977 struct etharp_q_entry *r;
979 while (r->next != NULL) {
984 /* queue did not exist, first item in queue */
985 arp_table[i].q = new_entry;
987 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
990 /* the pool MEMP_ARP_QUEUE is empty */
992 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));
993 /* { result == ERR_MEM } through initialization */
996 ETHARP_STATS_INC(etharp.memerr);
997 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));
998 /* { result == ERR_MEM } through initialization */
1000 #else /* ARP_QUEUEING == 0 */
1001 /* q && state == PENDING && ARP_QUEUEING == 0 => result = ERR_MEM */
1002 /* { result == ERR_MEM } through initialization */
1003 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: Ethernet destination address unknown, queueing disabled, packet %p dropped\n", (void *)q));
1011 * Send a raw ARP packet (opcode and all addresses can be modified)
1013 * @param netif the lwip network interface on which to send the ARP packet
1014 * @param ethsrc_addr the source MAC address for the ethernet header
1015 * @param ethdst_addr the destination MAC address for the ethernet header
1016 * @param hwsrc_addr the source MAC address for the ARP protocol header
1017 * @param ipsrc_addr the source IP address for the ARP protocol header
1018 * @param hwdst_addr the destination MAC address for the ARP protocol header
1019 * @param ipdst_addr the destination IP address for the ARP protocol header
1020 * @param opcode the type of the ARP packet
1021 * @return ERR_OK if the ARP packet has been sent
1022 * ERR_MEM if the ARP packet couldn't be allocated
1023 * any other err_t on failure
1027 #endif /* LWIP_AUTOIP */
1029 etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
1030 const struct eth_addr *ethdst_addr,
1031 const struct eth_addr *hwsrc_addr, const struct ip_addr *ipsrc_addr,
1032 const struct eth_addr *hwdst_addr, const struct ip_addr *ipdst_addr,
1036 err_t result = ERR_OK;
1037 u8_t k; /* ARP entry index */
1038 struct etharp_hdr *hdr;
1040 const u8_t * ethdst_hwaddr;
1041 #endif /* LWIP_AUTOIP */
1043 /* allocate a pbuf for the outgoing ARP request packet */
1044 p = pbuf_alloc(PBUF_LINK, sizeof(struct etharp_hdr), PBUF_RAM);
1045 /* could allocate a pbuf for an ARP request? */
1047 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | 2, ("etharp_raw: could not allocate pbuf for ARP request.\n"));
1048 ETHARP_STATS_INC(etharp.memerr);
1051 LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
1052 (p->len >= sizeof(struct etharp_hdr)));
1055 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n"));
1056 hdr->opcode = htons(opcode);
1058 LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
1059 (netif->hwaddr_len == ETHARP_HWADDR_LEN));
1060 k = ETHARP_HWADDR_LEN;
1062 /* If we are using Link-Local, ARP packets must be broadcast on the
1063 * link layer. (See RFC3927 Section 2.5) */
1064 ethdst_hwaddr = ((netif->autoip != NULL) && (netif->autoip->state != AUTOIP_STATE_OFF)) ? (u8_t*)(ethbroadcast.addr) : ethdst_addr->addr;
1065 #endif /* LWIP_AUTOIP */
1066 /* Write MAC-Addresses (combined loop for both headers) */
1069 /* Write the ARP MAC-Addresses */
1070 hdr->shwaddr.addr[k] = hwsrc_addr->addr[k];
1071 hdr->dhwaddr.addr[k] = hwdst_addr->addr[k];
1072 /* Write the Ethernet MAC-Addresses */
1074 hdr->ethhdr.dest.addr[k] = ethdst_hwaddr[k];
1075 #else /* LWIP_AUTOIP */
1076 hdr->ethhdr.dest.addr[k] = ethdst_addr->addr[k];
1077 #endif /* LWIP_AUTOIP */
1078 hdr->ethhdr.src.addr[k] = ethsrc_addr->addr[k];
1080 hdr->sipaddr = *(struct ip_addr2 *)ipsrc_addr;
1081 hdr->dipaddr = *(struct ip_addr2 *)ipdst_addr;
1083 hdr->hwtype = htons(HWTYPE_ETHERNET);
1084 hdr->proto = htons(ETHTYPE_IP);
1085 /* set hwlen and protolen together */
1086 hdr->_hwlen_protolen = htons((ETHARP_HWADDR_LEN << 8) | sizeof(struct ip_addr));
1088 hdr->ethhdr.type = htons(ETHTYPE_ARP);
1089 /* send ARP query */
1090 result = netif->linkoutput(netif, p);
1091 ETHARP_STATS_INC(etharp.xmit);
1092 /* free ARP query packet */
1095 /* could not allocate pbuf for ARP request */
1101 * Send an ARP request packet asking for ipaddr.
1103 * @param netif the lwip network interface on which to send the request
1104 * @param ipaddr the IP address for which to ask
1105 * @return ERR_OK if the request has been sent
1106 * ERR_MEM if the ARP packet couldn't be allocated
1107 * any other err_t on failure
1110 etharp_request(struct netif *netif, struct ip_addr *ipaddr)
1112 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
1113 return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, ðbroadcast,
1114 (struct eth_addr *)netif->hwaddr, &netif->ip_addr, ðzero,
1115 ipaddr, ARP_REQUEST);
1119 * Process received ethernet frames. Using this function instead of directly
1120 * calling ip_input and passing ARP frames through etharp in ethernetif_input,
1121 * the ARP cache is protected from concurrent access.
1123 * @param p the recevied packet, p->payload pointing to the ethernet header
1124 * @param netif the network interface on which the packet was received
1127 ethernet_input(struct pbuf *p, struct netif *netif)
1129 struct eth_hdr* ethhdr;
1131 /* points to packet payload, which starts with an Ethernet header */
1132 ethhdr = p->payload;
1134 switch (htons(ethhdr->type)) {
1137 #if ETHARP_TRUST_IP_MAC
1138 /* update ARP table */
1139 etharp_ip_input(netif, p);
1140 #endif /* ETHARP_TRUST_IP_MAC */
1141 /* skip Ethernet header */
1142 if(pbuf_header(p, -(s16_t)sizeof(struct eth_hdr))) {
1143 LWIP_ASSERT("Can't move over header in packet", 0);
1147 /* pass to IP layer */
1153 /* pass p to ARP module */
1154 etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p);
1158 case ETHTYPE_PPPOEDISC: /* PPP Over Ethernet Discovery Stage */
1159 pppoe_disc_input(netif, p);
1162 case ETHTYPE_PPPOE: /* PPP Over Ethernet Session Stage */
1163 pppoe_data_input(netif, p);
1165 #endif /* PPPOE_SUPPORT */
1173 /* This means the pbuf is freed or consumed,
1174 so the caller doesn't have to free it again */
1177 #endif /* LWIP_ARP */