// SPDX-License-Identifier: GPL-2.0-or-later /* * AARP: An implementation of the AppleTalk AARP protocol for * Ethernet 'ELAP'. * * Alan Cox <Alan.Cox@linux.org> * * This doesn't fit cleanly with the IP arp. Potentially we can use * the generic neighbour discovery code to clean this up. * * FIXME: * We ought to handle the retransmits with a single list and a * separate fast timer for when it is needed. * Use neighbour discovery code. * Token Ring Support. * * References: * Inside AppleTalk (2nd Ed). * Fixes: * Jaume Grau - flush caches on AARP_PROBE * Rob Newberry - Added proxy AARP and AARP proc fs, * moved probing from DDP module. * Arnaldo C. Melo - don't mangle rx packets
*/
int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME; int sysctl_aarp_tick_time = AARP_TICK_TIME; int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT; int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
/* Lists of aarp entries */ /** * struct aarp_entry - AARP entry * @refcnt: Reference count * @last_sent: Last time we xmitted the aarp request * @packet_queue: Queue of frames wait for resolution * @status: Used for proxy AARP * @expires_at: Entry expiry time * @target_addr: DDP Address * @dev: Device to use * @hwaddr: Physical i/f address of target/router * @xmit_count: When this hits 10 we give up * @next: Next entry in chain
*/ struct aarp_entry {
refcount_t refcnt; /* These first two are only used for unresolved entries */ unsignedlong last_sent; struct sk_buff_head packet_queue; int status; unsignedlong expires_at; struct atalk_addr target_addr; struct net_device *dev; char hwaddr[ETH_ALEN]; unsignedshort xmit_count; struct aarp_entry *next;
};
/* Hashed list of resolved, unresolved and proxy entries */ staticstruct aarp_entry *resolved[AARP_HASH_SIZE]; staticstruct aarp_entry *unresolved[AARP_HASH_SIZE]; staticstruct aarp_entry *proxies[AARP_HASH_SIZE]; staticint unresolved_count;
/* One lock protects it all. */ static DEFINE_RWLOCK(aarp_lock);
/* Used to walk the list and purge/kick entries. */ staticstruct timer_list aarp_timer;
/* Send it */
aarp_dl->request(aarp_dl, skb, aarp_eth_multicast); /* Update the sending count */
a->xmit_count++;
a->last_sent = jiffies;
}
/* This runs under aarp_lock and in softint context, so only atomic memory
* allocations can be used. */ staticvoid aarp_send_reply(struct net_device *dev, struct atalk_addr *us, struct atalk_addr *them, unsignedchar *sha)
{ struct elapaarp *eah; int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length; struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
if (!skb) return;
/* Set up the buffer */
skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
skb_reset_network_header(skb);
skb_reset_transport_header(skb);
skb_put(skb, sizeof(*eah));
skb->protocol = htons(ETH_P_ATALK);
skb->dev = dev;
eah = aarp_hdr(skb);
/* Set up the ARP */
eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
eah->pa_type = htons(ETH_P_ATALK);
eah->hw_len = ETH_ALEN;
eah->pa_len = AARP_PA_ALEN;
eah->function = htons(AARP_REPLY);
while (*n) /* Expired ? */ if (time_after(jiffies, (*n)->expires_at)) {
t = *n;
*n = (*n)->next;
__aarp_expire(t);
} else
n = &((*n)->next);
}
/* * Kick all pending requests 5 times a second. * * Must run under the aarp_lock.
*/ staticvoid __aarp_kick(struct aarp_entry **n)
{ struct aarp_entry *t;
while (*n) /* Expired: if this will be the 11th tx, we delete instead. */ if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
t = *n;
*n = (*n)->next;
__aarp_expire(t);
} else {
__aarp_send_query(*n);
n = &((*n)->next);
}
}
/* * A device has gone down. Take all entries referring to the device * and remove them. * * Must run under the aarp_lock.
*/ staticvoid __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
{ struct aarp_entry *t;
while (*n) if ((*n)->dev == dev) {
t = *n;
*n = (*n)->next;
__aarp_expire(t);
} else
n = &((*n)->next);
}
/* Handle the timer event */ staticvoid aarp_expire_timeout(struct timer_list *unused)
{ int ct;
/* * Create a new aarp entry. This must use GFP_ATOMIC because it * runs while holding spinlocks.
*/ staticstruct aarp_entry *aarp_alloc(void)
{ struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC); if (!a) return NULL;
/* * Find an entry. We might return an expired but not yet purged entry. We * don't care as it will do no harm. * * This must run under the aarp_lock.
*/ staticstruct aarp_entry *__aarp_find_entry(struct aarp_entry *list, struct net_device *dev, struct atalk_addr *sat)
{ while (list) { if (list->target_addr.s_net == sat->s_net &&
list->target_addr.s_node == sat->s_node &&
list->dev == dev) break;
list = list->next;
}
return list;
}
/* Called from the DDP code, and thus must be exported. */ void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
{ int hash = sa->s_node % (AARP_HASH_SIZE - 1); struct aarp_entry *a;
write_lock_bh(&aarp_lock);
a = __aarp_find_entry(proxies[hash], dev, sa); if (a)
a->expires_at = jiffies - 1;
write_unlock_bh(&aarp_lock);
}
/* This must run under aarp_lock. */ staticstruct atalk_addr *__aarp_proxy_find(struct net_device *dev, struct atalk_addr *sa)
{ int hash = sa->s_node % (AARP_HASH_SIZE - 1); struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
{ int hash, retval = -EPROTONOSUPPORT; struct aarp_entry *entry; unsignedint count;
/* * we don't currently support LocalTalk or PPP for proxy AARP; * if someone wants to try and add it, have fun
*/ if (atif->dev->type == ARPHRD_LOCALTLK ||
atif->dev->type == ARPHRD_PPP) goto out;
/* * create a new AARP entry with the flags set to be published -- * we need this one to hang around even if it's in use
*/
entry = aarp_alloc();
retval = -ENOMEM; if (!entry) goto out;
/* * The upper two remaining bytes are the port * numbers we just happen to need. Now put the * length in the lower two.
*/
*((__be16 *)skb->data) = htons(skb->len);
ft = 1;
} /* * Nice and easy. No AARP type protocols occur here so we can * just shovel it out with a 3 byte LLAP header
*/
/* Do we have a resolved entry? */ if (sa->s_node == ATADDR_BCAST) { /* Send it */
ddp_dl->request(ddp_dl, skb, ddp_eth_multicast); goto sent;
}
write_lock_bh(&aarp_lock);
a = __aarp_find_entry(resolved[hash], dev, sa);
if (a) { /* Return 1 and fill in the address */
a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
ddp_dl->request(ddp_dl, skb, a->hwaddr);
write_unlock_bh(&aarp_lock); goto sent;
}
/* Do we have an unresolved entry: This is the less common path */
a = __aarp_find_entry(unresolved[hash], dev, sa); if (a) { /* Queue onto the unresolved queue */
skb_queue_tail(&a->packet_queue, skb); goto out_unlock;
}
/* Allocate a new entry */
a = aarp_alloc(); if (!a) { /* Whoops slipped... good job it's an unreliable protocol 8) */
write_unlock_bh(&aarp_lock); goto free_it;
}
/* Set up the queue */
skb_queue_tail(&a->packet_queue, skb);
a->expires_at = jiffies + sysctl_aarp_resolve_time;
a->dev = dev;
a->next = unresolved[hash];
a->target_addr = *sa;
a->xmit_count = 0;
unresolved[hash] = a;
unresolved_count++;
/* Send an initial request for the address */
__aarp_send_query(a);
/* * Switch to fast timer if needed (That is if this is the first * unresolved entry to get added)
*/
if (unresolved_count == 1)
mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
/* Now finally, it is safe to drop the lock. */
out_unlock:
write_unlock_bh(&aarp_lock);
/* Tell the ddp layer we have taken over for this frame. */ goto sent;
sendit: if (skb->sk)
skb->priority = READ_ONCE(skb->sk->sk_priority); if (dev_queue_xmit(skb)) goto drop;
sent: return NET_XMIT_SUCCESS;
free_it:
kfree_skb(skb);
drop: return NET_XMIT_DROP;
}
EXPORT_SYMBOL(aarp_send_ddp);
/* * An entry in the aarp unresolved queue has become resolved. Send * all the frames queued under it. * * Must run under aarp_lock.
*/ staticvoid __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a, int hash)
{ struct sk_buff *skb;
while (*list) if (*list == a) {
unresolved_count--;
*list = a->next;
/* Move into the resolved list */
a->next = resolved[hash];
resolved[hash] = a;
/* Kick frames off */ while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
a->expires_at = jiffies +
sysctl_aarp_expiry_time * 10;
ddp_dl->request(ddp_dl, skb, a->hwaddr);
}
} else
list = &((*list)->next);
}
/* * This is called by the SNAP driver whenever we see an AARP SNAP * frame. We currently only support Ethernet.
*/ staticint aarp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{ struct elapaarp *ea = aarp_hdr(skb); int hash, ret = 0;
__u16 function; struct aarp_entry *a; struct atalk_addr sa, *ma, da; struct atalk_iface *ifa;
if (!net_eq(dev_net(dev), &init_net)) goto out0;
/* We only do Ethernet SNAP AARP. */ if (dev->type != ARPHRD_ETHER) goto out0;
/* Frame size ok? */ if (!skb_pull(skb, sizeof(*ea))) goto out0;
/* Process the packet. Check for replies of me. */
ifa = atalk_find_dev(dev); if (!ifa) goto out1;
if (ifa->status & ATIF_PROBE &&
ifa->address.s_node == ea->pa_dst_node &&
ifa->address.s_net == ea->pa_dst_net) {
ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */ goto out1;
}
/* Check for replies of proxy AARP entries */
da.s_node = ea->pa_dst_node;
da.s_net = ea->pa_dst_net;
write_lock_bh(&aarp_lock);
a = __aarp_find_entry(proxies[hash], dev, &da);
if (a && a->status & ATIF_PROBE) {
a->status |= ATIF_PROBE_FAIL; /* * we do not respond to probe or request packets of * this address while we are probing this address
*/ goto unlock;
}
switch (function) { case AARP_REPLY: if (!unresolved_count) /* Speed up */ break;
/* Find the entry. */
a = __aarp_find_entry(unresolved[hash], dev, &sa); if (!a || dev != a->dev) break;
/* We can fill one in - this is good. */
ether_addr_copy(a->hwaddr, ea->hw_src);
__aarp_resolved(&unresolved[hash], a, hash); if (!unresolved_count)
mod_timer(&aarp_timer,
jiffies + sysctl_aarp_expiry_time); break;
case AARP_REQUEST: case AARP_PROBE:
/* * If it is my address set ma to my address and reply. * We can treat probe and request the same. Probe * simply means we shouldn't cache the querying host, * as in a probe they are proposing an address not * using one. * * Support for proxy-AARP added. We check if the * address is one of our proxies before we toss the * packet out.
*/
/* See if we have a matching proxy. */
ma = __aarp_proxy_find(dev, &sa); if (!ma)
ma = &ifa->address; else { /* We need to make a copy of the entry. */
da.s_node = sa.s_node;
da.s_net = sa.s_net;
ma = &da;
}
if (function == AARP_PROBE) { /* * A probe implies someone trying to get an * address. So as a precaution flush any * entries we have for this address.
*/
a = __aarp_find_entry(resolved[sa.s_node %
(AARP_HASH_SIZE - 1)],
skb->dev, &sa);
/* * Make it expire next tick - that avoids us * getting into a probe/flush/learn/probe/ * flush/learn cycle during probing of a slow * to respond host addr.
*/ if (a) {
a->expires_at = jiffies - 1;
mod_timer(&aarp_timer, jiffies +
sysctl_aarp_tick_time);
}
}
if (sa.s_node != ma->s_node) break;
if (sa.s_net && ma->s_net && sa.s_net != ma->s_net) break;
#ifdef CONFIG_PROC_FS /* * Get the aarp entry that is in the chain described * by the iterator. * If pos is set then skip till that index. * pos = 1 is the first entry
*/ staticstruct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
{ int ct = iter->bucket; struct aarp_entry **table = iter->table;
loff_t off = 0; struct aarp_entry *entry;
/* General module cleanup. Called from cleanup_module() in ddp.c. */ void aarp_cleanup_module(void)
{
timer_delete_sync(&aarp_timer);
unregister_netdevice_notifier(&aarp_notifier);
unregister_snap_client(aarp_dl);
aarp_purge();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.