2004-11-25 Leon Woestenberg <leon.woestenberg@gmx.net>

* etharp.c: ETHARP_CREATE is renamed to ETHARP_TRY_HARD.
    Do not try hard to insert arbitrary packet's source address,
    etharp_ip_input() now calls etharp_update() without ETHARP_TRY_HARD.
    etharp_query() now always DOES call ETHARP_TRY_HARD so that users
    querying an address will see it appear in the cache (DHCP could
    suffer from this when a server invalidly gave an in-use address.)
This commit is contained in:
likewise 2004-11-25 13:32:31 +00:00
parent ae4955f59e
commit 3488a5c3c4

View File

@ -96,9 +96,11 @@ struct etharp_entry {
static const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}}; static const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
static struct etharp_entry arp_table[ARP_TABLE_SIZE]; static struct etharp_entry arp_table[ARP_TABLE_SIZE];
/** ask update_arp_entry() to create new entry instead of merely update existing */ /**
/** ask find_entry() to create new entry instead of merely finding existing */ * Try hard to create a new entry - we want the IP address to appear in
#define ETHARP_CREATE 1 * the cache (even if this means removing an active entry or so). */
#define ETHARP_TRY_HARD 1
static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags); static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags);
static err_t update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags); static err_t update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags);
/** /**
@ -179,12 +181,12 @@ etharp_tmr(void)
* If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY. * 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 * In all cases, attempt to create new entries from an empty entry. If no
* empty entries are available and ETHARP_CREATE flag is set, recycle * empty entries are available and ETHARP_TRY_HARD flag is set, recycle
* old entries. Heuristic choose the least important entry for recycling. * 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 ipaddr IP address to find in ARP cache, or to add if not found.
* @param flags * @param flags
* - ETHARP_CREATE: Try hard to create a entry by allowing recycling. * - ETHARP_TRY_HARD: Try hard to create a entry by allowing recycling.
* *
* @return The ARP entry index that matched or is created, ERR_MEM if no * @return The ARP entry index that matched or is created, ERR_MEM if no
* entry is found or could be recycled. * entry is found or could be recycled.
@ -304,7 +306,7 @@ static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags)
LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE); LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
/* allowed to recycle a entry? */ /* allowed to recycle a entry? */
if (flags & ETHARP_CREATE) { if (flags & ETHARP_TRY_HARD) {
/* recycle (no-op for an already empty entry) */ /* recycle (no-op for an already empty entry) */
arp_table[i].state = ETHARP_STATE_EMPTY; arp_table[i].state = ETHARP_STATE_EMPTY;
} }
@ -340,12 +342,12 @@ static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags)
* @param ipaddr IP address of the inserted ARP entry. * @param ipaddr IP address of the inserted ARP entry.
* @param ethaddr Ethernet address of the inserted ARP entry. * @param ethaddr Ethernet address of the inserted ARP entry.
* @param flags Defines behaviour: * @param flags Defines behaviour:
* - ETHARP_CREATE Allows ARP to insert this as a new item. If not specified, * - ETHARP_TRY_HARD Allows ARP to insert this as a new item. If not specified,
* only existing ARP entries will be updated. * only existing ARP entries will be updated.
* *
* @return * @return
* - ERR_OK Succesfully updated ARP cache. * - ERR_OK Succesfully updated ARP cache.
* - ERR_MEM If we could not add a new ARP entry when ETHARP_CREATE was set. * - ERR_MEM If we could not add a new ARP entry when ETHARP_TRY_HARD was set.
* - ERR_ARG Non-unicast address given, those will not appear in ARP cache. * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
* *
* @see pbuf_free() * @see pbuf_free()
@ -432,14 +434,16 @@ etharp_ip_input(struct netif *netif, struct pbuf *p)
incoming IP packet comes from a host on the local network. */ incoming IP packet comes from a host on the local network. */
hdr = p->payload; hdr = p->payload;
/* source is on local network? */ /* source is on local network? */
if (!ip_addr_maskcmp(&(hdr->ip.src), &(netif->ip_addr), &(netif->netmask))) { if (!ip_addr_netcmp(&(hdr->ip.src), &(netif->ip_addr), &(netif->netmask))) {
/* do nothing */ /* do nothing */
return; return;
} }
LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n")); LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n"));
/* update ARP table, ask to insert entry */ /* update ARP table */
update_arp_entry(netif, &(hdr->ip.src), &(hdr->eth.src), ETHARP_CREATE); /* @todo We could use ETHARP_TRY_HARD if we think we are going to talk
* back soon (for example, if the destination IP address is ours. */
update_arp_entry(netif, &(hdr->ip.src), &(hdr->eth.src), 0);
} }
@ -492,7 +496,7 @@ etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p)
if (for_us) { if (for_us) {
/* add IP address in ARP cache; assume requester wants to talk to us. /* add IP address in ARP cache; assume requester wants to talk to us.
* can result in directly sending the queued packets for this host. */ * can result in directly sending the queued packets for this host. */
update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), ETHARP_CREATE); update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), ETHARP_TRY_HARD);
/* ARP message not directed to us? */ /* ARP message not directed to us? */
} else { } else {
/* update the source IP address in the cache, if present */ /* update the source IP address in the cache, if present */
@ -625,7 +629,7 @@ etharp_output(struct netif *netif, struct ip_addr *ipaddr, struct pbuf *q)
/* destination IP address is an IP unicast address */ /* destination IP address is an IP unicast address */
} else { } else {
/* outside local network? */ /* outside local network? */
if (!ip_addr_maskcmp(ipaddr, &(netif->ip_addr), &(netif->netmask))) { if (!ip_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask))) {
/* interface has default gateway? */ /* interface has default gateway? */
if (netif->gw.addr != 0) { if (netif->gw.addr != 0) {
/* send to hardware address of default gateway IP address */ /* send to hardware address of default gateway IP address */
@ -707,7 +711,7 @@ err_t etharp_query(struct netif *netif, struct ip_addr *ipaddr, struct pbuf *q)
} }
/* find entry in ARP cache, ask to create entry if queueing packet */ /* find entry in ARP cache, ask to create entry if queueing packet */
i = find_entry(ipaddr, (q != NULL) ? ETHARP_CREATE : 0); i = find_entry(ipaddr, ETHARP_TRY_HARD);
/* could not find or create entry? */ /* could not find or create entry? */
if (i < 0) return (err_t)i; if (i < 0) return (err_t)i;