📄 aarp.c
字号:
/* * 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. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * * 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 * */#include <linux/config.h>#if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE) #include <asm/uaccess.h>#include <asm/system.h>#include <asm/bitops.h>#include <linux/types.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/string.h>#include <linux/mm.h>#include <linux/socket.h>#include <linux/sockios.h>#include <linux/in.h>#include <linux/errno.h>#include <linux/interrupt.h>#include <linux/if_ether.h>#include <linux/inet.h>#include <linux/notifier.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/if_arp.h>#include <linux/skbuff.h>#include <linux/spinlock.h>#include <net/sock.h>#include <net/datalink.h>#include <net/psnap.h>#include <linux/atalk.h>#include <linux/init.h>#include <linux/proc_fs.h>#include <linux/module.h>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 { /* These first two are only used for unresolved entries */ unsigned long last_sent; /* Last time we xmitted the aarp request */ struct sk_buff_head packet_queue; /* Queue of frames wait for resolution */ int status; /* Used for proxy AARP */ unsigned long expires_at; /* Entry expiry time */ struct at_addr target_addr; /* DDP Address */ struct net_device *dev; /* Device to use */ char hwaddr[6]; /* Physical i/f address of target/router */ unsigned short xmit_count; /* When this hits 10 we give up */ struct aarp_entry *next; /* Next entry in chain */};/* Hashed list of resolved, unresolved and proxy entries */static struct aarp_entry *resolved[AARP_HASH_SIZE];static struct aarp_entry *unresolved[AARP_HASH_SIZE];static struct aarp_entry *proxies[AARP_HASH_SIZE];static int unresolved_count;/* One lock protects it all. */static spinlock_t aarp_lock = SPIN_LOCK_UNLOCKED;/* Used to walk the list and purge/kick entries. */static struct timer_list aarp_timer;/* * Delete an aarp queue * * Must run under aarp_lock. */static void __aarp_expire(struct aarp_entry *a){ skb_queue_purge(&a->packet_queue); kfree(a);}/* * Send an aarp queue entry request * * Must run under aarp_lock. */ static void __aarp_send_query(struct aarp_entry *a){ static char aarp_eth_multicast[ETH_ALEN] = { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF }; struct net_device *dev = a->dev; int len = dev->hard_header_len + sizeof(struct elapaarp) + aarp_dl->header_length; struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC); struct at_addr *sat = atalk_find_dev_addr(dev); struct elapaarp *eah; if (!skb) return; if (!sat) { kfree_skb(skb); return; } /* Set up the buffer */ skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length); eah = (struct elapaarp *)skb_put(skb, sizeof(struct elapaarp)); skb->protocol = htons(ETH_P_ATALK); skb->nh.raw = skb->h.raw = (void *) eah; skb->dev = dev; /* 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_REQUEST); memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN); eah->pa_src_zero= 0; eah->pa_src_net = sat->s_net; eah->pa_src_node= sat->s_node; memset(eah->hw_dst, '\0', ETH_ALEN); eah->pa_dst_zero= 0; eah->pa_dst_net = a->target_addr.s_net; eah->pa_dst_node= a->target_addr.s_node; /* Add ELAP headers and set target to the AARP multicast */ aarp_dl->datalink_header(aarp_dl, skb, aarp_eth_multicast); /* Send it */ dev_queue_xmit(skb); /* Update the sending count */ a->xmit_count++;}/* This runs under aarp_lock and in softint context, so only atomic memory * allocations can be used. */static void aarp_send_reply(struct net_device *dev, struct at_addr *us, struct at_addr *them, unsigned char *sha){ int len = dev->hard_header_len + sizeof(struct elapaarp) + aarp_dl->header_length; struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC); struct elapaarp *eah; if (!skb) return; /* Set up the buffer */ skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length); eah = (struct elapaarp *)skb_put(skb, sizeof(struct elapaarp)); skb->protocol = htons(ETH_P_ATALK); skb->nh.raw = skb->h.raw = (void *) eah; skb->dev = dev; /* 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); memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN); eah->pa_src_zero= 0; eah->pa_src_net = us->s_net; eah->pa_src_node= us->s_node; if (!sha) memset(eah->hw_dst, '\0', ETH_ALEN); else memcpy(eah->hw_dst, sha, ETH_ALEN); eah->pa_dst_zero= 0; eah->pa_dst_net = them->s_net; eah->pa_dst_node= them->s_node; /* Add ELAP headers and set target to the AARP multicast */ aarp_dl->datalink_header(aarp_dl, skb, sha); /* Send it */ dev_queue_xmit(skb);}/* * Send probe frames. Called from aarp_probe_network and * aarp_proxy_probe_network. */void aarp_send_probe(struct net_device *dev, struct at_addr *us){ int len = dev->hard_header_len + sizeof(struct elapaarp) + aarp_dl->header_length; struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC); static char aarp_eth_multicast[ETH_ALEN] = { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF }; struct elapaarp *eah; if (!skb) return; /* Set up the buffer */ skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length); eah = (struct elapaarp *)skb_put(skb, sizeof(struct elapaarp)); skb->protocol = htons(ETH_P_ATALK); skb->nh.raw = skb->h.raw = (void *) eah; skb->dev = dev; /* 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_PROBE); memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN); eah->pa_src_zero= 0; eah->pa_src_net = us->s_net; eah->pa_src_node= us->s_node; memset(eah->hw_dst, '\0', ETH_ALEN); eah->pa_dst_zero= 0; eah->pa_dst_net = us->s_net; eah->pa_dst_node= us->s_node; /* Add ELAP headers and set target to the AARP multicast */ aarp_dl->datalink_header(aarp_dl, skb, aarp_eth_multicast); /* Send it */ dev_queue_xmit(skb);} /* * Handle an aarp timer expire * * Must run under the aarp_lock. */static void __aarp_expire_timer(struct aarp_entry **n){ struct aarp_entry *t; 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. */ static void __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. */ static void __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 */static void aarp_expire_timeout(unsigned long unused){ int ct; spin_lock_bh(&aarp_lock); for (ct = 0; ct < AARP_HASH_SIZE; ct++) { __aarp_expire_timer(&resolved[ct]); __aarp_kick(&unresolved[ct]); __aarp_expire_timer(&unresolved[ct]); __aarp_expire_timer(&proxies[ct]); } spin_unlock_bh(&aarp_lock); mod_timer(&aarp_timer, jiffies + (unresolved_count ? sysctl_aarp_tick_time : sysctl_aarp_expiry_time));}/* Network device notifier chain handler. */static int aarp_device_event(struct notifier_block *this, unsigned long event, void *ptr){ int ct; if (event == NETDEV_DOWN) { spin_lock_bh(&aarp_lock); for (ct = 0; ct < AARP_HASH_SIZE; ct++) { __aarp_expire_device(&resolved[ct], ptr); __aarp_expire_device(&unresolved[ct], ptr); __aarp_expire_device(&proxies[ct], ptr); } spin_unlock_bh(&aarp_lock); } return NOTIFY_DONE;}/* * Create a new aarp entry. This must use GFP_ATOMIC because it * runs while holding spinlocks. */ static struct aarp_entry *aarp_alloc(void){ struct aarp_entry *a = kmalloc(sizeof(struct aarp_entry), GFP_ATOMIC); if (a) skb_queue_head_init(&a->packet_queue); return a;}/* * 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. */static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list, struct net_device *dev, struct at_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 at_addr *sa){ int hash = sa->s_node % (AARP_HASH_SIZE - 1); struct aarp_entry *a; spin_lock_bh(&aarp_lock); a = __aarp_find_entry(proxies[hash], dev, sa); if (a) a->expires_at = jiffies - 1; spin_unlock_bh(&aarp_lock);}/* This must run under aarp_lock. */static struct at_addr *__aarp_proxy_find(struct net_device *dev, struct at_addr *sa){ int hash = sa->s_node % (AARP_HASH_SIZE - 1); struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa); return a ? sa : NULL;}/* * Probe a Phase 1 device or a device that requires its Net:Node to * be set via an ioctl. */void aarp_send_probe_phase1(struct atalk_iface *iface){ struct ifreq atreq; struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr; sa->sat_addr.s_node = iface->address.s_node; sa->sat_addr.s_net = ntohs(iface->address.s_net); /* We pass the Net:Node to the drivers/cards by a Device ioctl. */ if (!(iface->dev->do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) { (void)iface->dev->do_ioctl(iface->dev, &atreq, SIOCGIFADDR); if (iface->address.s_net != htons(sa->sat_addr.s_net) || iface->address.s_node != sa->sat_addr.s_node) iface->status |= ATIF_PROBE_FAIL; iface->address.s_net = htons(sa->sat_addr.s_net); iface->address.s_node = sa->sat_addr.s_node; }}void aarp_probe_network(struct atalk_iface *atif){ if (atif->dev->type == ARPHRD_LOCALTLK || atif->dev->type == ARPHRD_PPP) aarp_send_probe_phase1(atif); else { unsigned int count; for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) { aarp_send_probe(atif->dev, &atif->address); /* Defer 1/10th */ current->state = TASK_INTERRUPTIBLE; schedule_timeout(HZ/10); if (atif->status & ATIF_PROBE_FAIL) break; } }}int aarp_proxy_probe_network(struct atalk_iface *atif, struct at_addr *sa){ int hash, retval = 1; struct aarp_entry *entry; unsigned int 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) return -EPROTONOSUPPORT; if (atif->dev->type == ARPHRD_PPP) return -EPROTONOSUPPORT; /* * 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(); if (!entry)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -