📄 etharp.c
字号:
/** * @file * Address Resolution Protocol module for IP over Ethernet * * Functionally, ARP is divided into two parts. The first maps an IP address * to a physical address when sending a packet, and the second part answers * requests from other machines for our physical address. * * This implementation complies with RFC 826 (Ethernet ARP). It supports * Gratuitious ARP from RFC3220 (IP Mobility Support for IPv4) section 4.6 * if an interface calls etharp_gratuitous(our_netif) upon address change. *//* * Copyright (c) 2001-2003 Swedish Institute of Computer Science. * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv> * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * */ #include "lwip/opt.h"#if LWIP_ARP || LWIP_ETHERNET#include "lwip/ip_addr.h"#include "lwip/def.h"#include "lwip/ip.h"#include "lwip/stats.h"#include "lwip/snmp.h"#include "lwip/dhcp.h"#include "lwip/autoip.h"#include "netif/etharp.h"#if PPPOE_SUPPORT#include "netif/ppp_oe.h"#endif /* PPPOE_SUPPORT */#include <string.h>const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};const struct eth_addr ethzero = {{0,0,0,0,0,0}};#if LWIP_ARP /* don't build if not configured for use in lwipopts.h *//** the time an ARP entry stays valid after its last update, * for ARP_TMR_INTERVAL = 5000, this is * (240 * 5) seconds = 20 minutes. */#define ARP_MAXAGE 240/** the time an ARP entry stays pending after first request, * for ARP_TMR_INTERVAL = 5000, this is * (2 * 5) seconds = 10 seconds. * * @internal Keep this number at least 2, otherwise it might * run out instantly if the timeout occurs directly after a request. */#define ARP_MAXPENDING 2#define HWTYPE_ETHERNET 1enum etharp_state { ETHARP_STATE_EMPTY = 0, ETHARP_STATE_PENDING, ETHARP_STATE_STABLE};struct etharp_entry {#if ARP_QUEUEING /** Pointer to queue of pending outgoing packets on this ARP entry. */ struct etharp_q_entry *q;#else /* ARP_QUEUEING */ /** Pointer to a single pending outgoing packet on this ARP entry. */ struct pbuf *q;#endif /* ARP_QUEUEING */ ip_addr_t ipaddr; struct eth_addr ethaddr;#if LWIP_SNMP struct netif *netif;#endif /* LWIP_SNMP */ u8_t state; u8_t ctime;#if ETHARP_SUPPORT_STATIC_ENTRIES u8_t static_entry;#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */};static struct etharp_entry arp_table[ARP_TABLE_SIZE];#if !LWIP_NETIF_HWADDRHINTstatic u8_t etharp_cached_entry;#endif /* !LWIP_NETIF_HWADDRHINT *//** Try hard to create a new entry - we want the IP address to appear in the cache (even if this means removing an active entry or so). */#define ETHARP_FLAG_TRY_HARD 1#define ETHARP_FLAG_FIND_ONLY 2#define ETHARP_FLAG_STATIC_ENTRY 4#if LWIP_NETIF_HWADDRHINT#define ETHARP_SET_HINT(netif, hint) if (((netif) != NULL) && ((netif)->addr_hint != NULL)) \ *((netif)->addr_hint) = (hint);#else /* LWIP_NETIF_HWADDRHINT */#define ETHARP_SET_HINT(netif, hint) (etharp_cached_entry = (hint))#endif /* LWIP_NETIF_HWADDRHINT */static err_t update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags);/* Some checks, instead of etharp_init(): */#if (LWIP_ARP && (ARP_TABLE_SIZE > 0x7f)) #error "ARP_TABLE_SIZE must fit in an s8_t, you have to reduce it in your lwipopts.h"#endif#if ARP_QUEUEING/** * Free a complete queue of etharp entries * * @param q a qeueue of etharp_q_entry's to free */static voidfree_etharp_q(struct etharp_q_entry *q){ struct etharp_q_entry *r; LWIP_ASSERT("q != NULL", q != NULL); LWIP_ASSERT("q->p != NULL", q->p != NULL); while (q) { r = q; q = q->next; LWIP_ASSERT("r->p != NULL", (r->p != NULL)); pbuf_free(r->p); memp_free(MEMP_ARP_QUEUE, r); }}#else /* ARP_QUEUEING *//** Compatibility define: free the queued pbuf */#define free_etharp_q(q) pbuf_free(q)#endif /* ARP_QUEUEING *//** Clean up ARP table entries */static voidfree_entry(int i){ /* remove from SNMP ARP index tree */ snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr); /* and empty packet queue */ if (arp_table[i].q != NULL) { /* remove all queued packets */ LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q))); free_etharp_q(arp_table[i].q); arp_table[i].q = NULL; } /* recycle entry for re-use */ arp_table[i].state = ETHARP_STATE_EMPTY;#if ETHARP_SUPPORT_STATIC_ENTRIES arp_table[i].static_entry = 0;#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */#ifdef LWIP_DEBUG /* for debugging, clean out the complete entry */ arp_table[i].ctime = 0;#if LWIP_SNMP arp_table[i].netif = NULL;#endif /* LWIP_SNMP */ ip_addr_set_zero(&arp_table[i].ipaddr); arp_table[i].ethaddr = ethzero;#endif /* LWIP_DEBUG */}/** * Clears expired entries in the ARP table. * * This function should be called every ETHARP_TMR_INTERVAL milliseconds (5 seconds), * in order to expire entries in the ARP table. */voidetharp_tmr(void){ u8_t i; LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n")); /* remove expired entries from the ARP table */ for (i = 0; i < ARP_TABLE_SIZE; ++i) { u8_t state = arp_table[i].state; if (state != ETHARP_STATE_EMPTY#if ETHARP_SUPPORT_STATIC_ENTRIES && (arp_table[i].static_entry == 0)#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ ) { arp_table[i].ctime++; if ((arp_table[i].ctime >= ARP_MAXAGE) || ((arp_table[i].state == ETHARP_STATE_PENDING) && (arp_table[i].ctime >= ARP_MAXPENDING))) { /* pending or stable entry has become old! */ LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %"U16_F".\n", arp_table[i].state == ETHARP_STATE_STABLE ? "stable" : "pending", (u16_t)i)); /* clean up entries that have just been expired */ free_entry(i); }#if ARP_QUEUEING /* still pending entry? (not expired) */ if (arp_table[i].state == ETHARP_STATE_PENDING) { /* resend an ARP query here? */ }#endif /* ARP_QUEUEING */ } }}/** * Search the ARP table for a matching or new entry. * * If an IP address is given, return a pending or stable ARP entry that matches * the address. If no match is found, create a new entry with this address set, * but in state ETHARP_EMPTY. The caller must check and possibly change the * state of the returned entry. * * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY. * * In all cases, attempt to create new entries from an empty entry. If no * empty entries are available and ETHARP_FLAG_TRY_HARD flag is set, recycle * old entries. Heuristic choose the least important entry for recycling. * * @param ipaddr IP address to find in ARP cache, or to add if not found. * @param flags @see definition of ETHARP_FLAG_* * @param netif netif related to this address (used for NETIF_HWADDRHINT) * * @return The ARP entry index that matched or is created, ERR_MEM if no * entry is found or could be recycled. */static s8_tfind_entry(ip_addr_t *ipaddr, u8_t flags){ s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE; s8_t empty = ARP_TABLE_SIZE; u8_t i = 0, age_pending = 0, age_stable = 0; /* oldest entry with packets on queue */ s8_t old_queue = ARP_TABLE_SIZE; /* its age */ u8_t age_queue = 0; /** * a) do a search through the cache, remember candidates * b) select candidate entry * c) create new entry */ /* a) in a single search sweep, do all of this * 1) remember the first empty entry (if any) * 2) remember the oldest stable entry (if any) * 3) remember the oldest pending entry without queued packets (if any) * 4) remember the oldest pending entry with queued packets (if any) * 5) search for a matching IP entry, either pending or stable * until 5 matches, or all entries are searched for. */ for (i = 0; i < ARP_TABLE_SIZE; ++i) { u8_t state = arp_table[i].state; /* no empty entry found yet and now we do find one? */ if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) { LWIP_DEBUGF(ETHARP_DEBUG, ("find_entry: found empty entry %"U16_F"\n", (u16_t)i)); /* remember first empty entry */ empty = i; } else if (state != ETHARP_STATE_EMPTY) { LWIP_ASSERT("state == ETHARP_STATE_PENDING || state == ETHARP_STATE_STABLE", state == ETHARP_STATE_PENDING || state == ETHARP_STATE_STABLE); /* if given, does IP address match IP address in ARP entry? */ if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) { LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: found matching entry %"U16_F"\n", (u16_t)i)); /* found exact IP address match, simply bail out */ return i; } /* pending entry? */ if (state == ETHARP_STATE_PENDING) { /* pending with queued packets? */ if (arp_table[i].q != NULL) { if (arp_table[i].ctime >= age_queue) { old_queue = i; age_queue = arp_table[i].ctime; } } else /* pending without queued packets? */ { if (arp_table[i].ctime >= age_pending) { old_pending = i; age_pending = arp_table[i].ctime; } } /* stable entry? */ } else if (state == ETHARP_STATE_STABLE) {#if ETHARP_SUPPORT_STATIC_ENTRIES /* don't record old_stable for static entries since they never expire */ if (arp_table[i].static_entry == 0)#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ { /* remember entry with oldest stable entry in oldest, its age in maxtime */ if (arp_table[i].ctime >= age_stable) { old_stable = i; age_stable = arp_table[i].ctime; } } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -