Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  AARP:       An implementation of the AppleTalk AARP protocol for
0004  *          Ethernet 'ELAP'.
0005  *
0006  *      Alan Cox  <Alan.Cox@linux.org>
0007  *
0008  *  This doesn't fit cleanly with the IP arp. Potentially we can use
0009  *  the generic neighbour discovery code to clean this up.
0010  *
0011  *  FIXME:
0012  *      We ought to handle the retransmits with a single list and a
0013  *  separate fast timer for when it is needed.
0014  *      Use neighbour discovery code.
0015  *      Token Ring Support.
0016  *
0017  *  References:
0018  *      Inside AppleTalk (2nd Ed).
0019  *  Fixes:
0020  *      Jaume Grau  -   flush caches on AARP_PROBE
0021  *      Rob Newberry    -   Added proxy AARP and AARP proc fs,
0022  *                  moved probing from DDP module.
0023  *      Arnaldo C. Melo -   don't mangle rx packets
0024  */
0025 
0026 #include <linux/if_arp.h>
0027 #include <linux/slab.h>
0028 #include <net/sock.h>
0029 #include <net/datalink.h>
0030 #include <net/psnap.h>
0031 #include <linux/atalk.h>
0032 #include <linux/delay.h>
0033 #include <linux/init.h>
0034 #include <linux/proc_fs.h>
0035 #include <linux/seq_file.h>
0036 #include <linux/export.h>
0037 #include <linux/etherdevice.h>
0038 
0039 int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
0040 int sysctl_aarp_tick_time = AARP_TICK_TIME;
0041 int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
0042 int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
0043 
0044 /* Lists of aarp entries */
0045 /**
0046  *  struct aarp_entry - AARP entry
0047  *  @last_sent: Last time we xmitted the aarp request
0048  *  @packet_queue: Queue of frames wait for resolution
0049  *  @status: Used for proxy AARP
0050  *  @expires_at: Entry expiry time
0051  *  @target_addr: DDP Address
0052  *  @dev:  Device to use
0053  *  @hwaddr:  Physical i/f address of target/router
0054  *  @xmit_count:  When this hits 10 we give up
0055  *  @next: Next entry in chain
0056  */
0057 struct aarp_entry {
0058     /* These first two are only used for unresolved entries */
0059     unsigned long       last_sent;
0060     struct sk_buff_head packet_queue;
0061     int         status;
0062     unsigned long       expires_at;
0063     struct atalk_addr   target_addr;
0064     struct net_device   *dev;
0065     char            hwaddr[ETH_ALEN];
0066     unsigned short      xmit_count;
0067     struct aarp_entry   *next;
0068 };
0069 
0070 /* Hashed list of resolved, unresolved and proxy entries */
0071 static struct aarp_entry *resolved[AARP_HASH_SIZE];
0072 static struct aarp_entry *unresolved[AARP_HASH_SIZE];
0073 static struct aarp_entry *proxies[AARP_HASH_SIZE];
0074 static int unresolved_count;
0075 
0076 /* One lock protects it all. */
0077 static DEFINE_RWLOCK(aarp_lock);
0078 
0079 /* Used to walk the list and purge/kick entries.  */
0080 static struct timer_list aarp_timer;
0081 
0082 /*
0083  *  Delete an aarp queue
0084  *
0085  *  Must run under aarp_lock.
0086  */
0087 static void __aarp_expire(struct aarp_entry *a)
0088 {
0089     skb_queue_purge(&a->packet_queue);
0090     kfree(a);
0091 }
0092 
0093 /*
0094  *  Send an aarp queue entry request
0095  *
0096  *  Must run under aarp_lock.
0097  */
0098 static void __aarp_send_query(struct aarp_entry *a)
0099 {
0100     static unsigned char aarp_eth_multicast[ETH_ALEN] =
0101                     { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
0102     struct net_device *dev = a->dev;
0103     struct elapaarp *eah;
0104     int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
0105     struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
0106     struct atalk_addr *sat = atalk_find_dev_addr(dev);
0107 
0108     if (!skb)
0109         return;
0110 
0111     if (!sat) {
0112         kfree_skb(skb);
0113         return;
0114     }
0115 
0116     /* Set up the buffer */
0117     skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
0118     skb_reset_network_header(skb);
0119     skb_reset_transport_header(skb);
0120     skb_put(skb, sizeof(*eah));
0121     skb->protocol    = htons(ETH_P_ATALK);
0122     skb->dev     = dev;
0123     eah      = aarp_hdr(skb);
0124 
0125     /* Set up the ARP */
0126     eah->hw_type     = htons(AARP_HW_TYPE_ETHERNET);
0127     eah->pa_type     = htons(ETH_P_ATALK);
0128     eah->hw_len  = ETH_ALEN;
0129     eah->pa_len  = AARP_PA_ALEN;
0130     eah->function    = htons(AARP_REQUEST);
0131 
0132     ether_addr_copy(eah->hw_src, dev->dev_addr);
0133 
0134     eah->pa_src_zero = 0;
0135     eah->pa_src_net  = sat->s_net;
0136     eah->pa_src_node = sat->s_node;
0137 
0138     eth_zero_addr(eah->hw_dst);
0139 
0140     eah->pa_dst_zero = 0;
0141     eah->pa_dst_net  = a->target_addr.s_net;
0142     eah->pa_dst_node = a->target_addr.s_node;
0143 
0144     /* Send it */
0145     aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
0146     /* Update the sending count */
0147     a->xmit_count++;
0148     a->last_sent = jiffies;
0149 }
0150 
0151 /* This runs under aarp_lock and in softint context, so only atomic memory
0152  * allocations can be used. */
0153 static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
0154                 struct atalk_addr *them, unsigned char *sha)
0155 {
0156     struct elapaarp *eah;
0157     int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
0158     struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
0159 
0160     if (!skb)
0161         return;
0162 
0163     /* Set up the buffer */
0164     skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
0165     skb_reset_network_header(skb);
0166     skb_reset_transport_header(skb);
0167     skb_put(skb, sizeof(*eah));
0168     skb->protocol    = htons(ETH_P_ATALK);
0169     skb->dev     = dev;
0170     eah      = aarp_hdr(skb);
0171 
0172     /* Set up the ARP */
0173     eah->hw_type     = htons(AARP_HW_TYPE_ETHERNET);
0174     eah->pa_type     = htons(ETH_P_ATALK);
0175     eah->hw_len  = ETH_ALEN;
0176     eah->pa_len  = AARP_PA_ALEN;
0177     eah->function    = htons(AARP_REPLY);
0178 
0179     ether_addr_copy(eah->hw_src, dev->dev_addr);
0180 
0181     eah->pa_src_zero = 0;
0182     eah->pa_src_net  = us->s_net;
0183     eah->pa_src_node = us->s_node;
0184 
0185     if (!sha)
0186         eth_zero_addr(eah->hw_dst);
0187     else
0188         ether_addr_copy(eah->hw_dst, sha);
0189 
0190     eah->pa_dst_zero = 0;
0191     eah->pa_dst_net  = them->s_net;
0192     eah->pa_dst_node = them->s_node;
0193 
0194     /* Send it */
0195     aarp_dl->request(aarp_dl, skb, sha);
0196 }
0197 
0198 /*
0199  *  Send probe frames. Called from aarp_probe_network and
0200  *  aarp_proxy_probe_network.
0201  */
0202 
0203 static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
0204 {
0205     struct elapaarp *eah;
0206     int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
0207     struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
0208     static unsigned char aarp_eth_multicast[ETH_ALEN] =
0209                     { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
0210 
0211     if (!skb)
0212         return;
0213 
0214     /* Set up the buffer */
0215     skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
0216     skb_reset_network_header(skb);
0217     skb_reset_transport_header(skb);
0218     skb_put(skb, sizeof(*eah));
0219     skb->protocol    = htons(ETH_P_ATALK);
0220     skb->dev     = dev;
0221     eah      = aarp_hdr(skb);
0222 
0223     /* Set up the ARP */
0224     eah->hw_type     = htons(AARP_HW_TYPE_ETHERNET);
0225     eah->pa_type     = htons(ETH_P_ATALK);
0226     eah->hw_len  = ETH_ALEN;
0227     eah->pa_len  = AARP_PA_ALEN;
0228     eah->function    = htons(AARP_PROBE);
0229 
0230     ether_addr_copy(eah->hw_src, dev->dev_addr);
0231 
0232     eah->pa_src_zero = 0;
0233     eah->pa_src_net  = us->s_net;
0234     eah->pa_src_node = us->s_node;
0235 
0236     eth_zero_addr(eah->hw_dst);
0237 
0238     eah->pa_dst_zero = 0;
0239     eah->pa_dst_net  = us->s_net;
0240     eah->pa_dst_node = us->s_node;
0241 
0242     /* Send it */
0243     aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
0244 }
0245 
0246 /*
0247  *  Handle an aarp timer expire
0248  *
0249  *  Must run under the aarp_lock.
0250  */
0251 
0252 static void __aarp_expire_timer(struct aarp_entry **n)
0253 {
0254     struct aarp_entry *t;
0255 
0256     while (*n)
0257         /* Expired ? */
0258         if (time_after(jiffies, (*n)->expires_at)) {
0259             t = *n;
0260             *n = (*n)->next;
0261             __aarp_expire(t);
0262         } else
0263             n = &((*n)->next);
0264 }
0265 
0266 /*
0267  *  Kick all pending requests 5 times a second.
0268  *
0269  *  Must run under the aarp_lock.
0270  */
0271 static void __aarp_kick(struct aarp_entry **n)
0272 {
0273     struct aarp_entry *t;
0274 
0275     while (*n)
0276         /* Expired: if this will be the 11th tx, we delete instead. */
0277         if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
0278             t = *n;
0279             *n = (*n)->next;
0280             __aarp_expire(t);
0281         } else {
0282             __aarp_send_query(*n);
0283             n = &((*n)->next);
0284         }
0285 }
0286 
0287 /*
0288  *  A device has gone down. Take all entries referring to the device
0289  *  and remove them.
0290  *
0291  *  Must run under the aarp_lock.
0292  */
0293 static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
0294 {
0295     struct aarp_entry *t;
0296 
0297     while (*n)
0298         if ((*n)->dev == dev) {
0299             t = *n;
0300             *n = (*n)->next;
0301             __aarp_expire(t);
0302         } else
0303             n = &((*n)->next);
0304 }
0305 
0306 /* Handle the timer event */
0307 static void aarp_expire_timeout(struct timer_list *unused)
0308 {
0309     int ct;
0310 
0311     write_lock_bh(&aarp_lock);
0312 
0313     for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
0314         __aarp_expire_timer(&resolved[ct]);
0315         __aarp_kick(&unresolved[ct]);
0316         __aarp_expire_timer(&unresolved[ct]);
0317         __aarp_expire_timer(&proxies[ct]);
0318     }
0319 
0320     write_unlock_bh(&aarp_lock);
0321     mod_timer(&aarp_timer, jiffies +
0322                    (unresolved_count ? sysctl_aarp_tick_time :
0323                 sysctl_aarp_expiry_time));
0324 }
0325 
0326 /* Network device notifier chain handler. */
0327 static int aarp_device_event(struct notifier_block *this, unsigned long event,
0328                  void *ptr)
0329 {
0330     struct net_device *dev = netdev_notifier_info_to_dev(ptr);
0331     int ct;
0332 
0333     if (!net_eq(dev_net(dev), &init_net))
0334         return NOTIFY_DONE;
0335 
0336     if (event == NETDEV_DOWN) {
0337         write_lock_bh(&aarp_lock);
0338 
0339         for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
0340             __aarp_expire_device(&resolved[ct], dev);
0341             __aarp_expire_device(&unresolved[ct], dev);
0342             __aarp_expire_device(&proxies[ct], dev);
0343         }
0344 
0345         write_unlock_bh(&aarp_lock);
0346     }
0347     return NOTIFY_DONE;
0348 }
0349 
0350 /* Expire all entries in a hash chain */
0351 static void __aarp_expire_all(struct aarp_entry **n)
0352 {
0353     struct aarp_entry *t;
0354 
0355     while (*n) {
0356         t = *n;
0357         *n = (*n)->next;
0358         __aarp_expire(t);
0359     }
0360 }
0361 
0362 /* Cleanup all hash chains -- module unloading */
0363 static void aarp_purge(void)
0364 {
0365     int ct;
0366 
0367     write_lock_bh(&aarp_lock);
0368     for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
0369         __aarp_expire_all(&resolved[ct]);
0370         __aarp_expire_all(&unresolved[ct]);
0371         __aarp_expire_all(&proxies[ct]);
0372     }
0373     write_unlock_bh(&aarp_lock);
0374 }
0375 
0376 /*
0377  *  Create a new aarp entry.  This must use GFP_ATOMIC because it
0378  *  runs while holding spinlocks.
0379  */
0380 static struct aarp_entry *aarp_alloc(void)
0381 {
0382     struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
0383 
0384     if (a)
0385         skb_queue_head_init(&a->packet_queue);
0386     return a;
0387 }
0388 
0389 /*
0390  * Find an entry. We might return an expired but not yet purged entry. We
0391  * don't care as it will do no harm.
0392  *
0393  * This must run under the aarp_lock.
0394  */
0395 static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
0396                         struct net_device *dev,
0397                         struct atalk_addr *sat)
0398 {
0399     while (list) {
0400         if (list->target_addr.s_net == sat->s_net &&
0401             list->target_addr.s_node == sat->s_node &&
0402             list->dev == dev)
0403             break;
0404         list = list->next;
0405     }
0406 
0407     return list;
0408 }
0409 
0410 /* Called from the DDP code, and thus must be exported. */
0411 void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
0412 {
0413     int hash = sa->s_node % (AARP_HASH_SIZE - 1);
0414     struct aarp_entry *a;
0415 
0416     write_lock_bh(&aarp_lock);
0417 
0418     a = __aarp_find_entry(proxies[hash], dev, sa);
0419     if (a)
0420         a->expires_at = jiffies - 1;
0421 
0422     write_unlock_bh(&aarp_lock);
0423 }
0424 
0425 /* This must run under aarp_lock. */
0426 static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
0427                         struct atalk_addr *sa)
0428 {
0429     int hash = sa->s_node % (AARP_HASH_SIZE - 1);
0430     struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
0431 
0432     return a ? sa : NULL;
0433 }
0434 
0435 /*
0436  * Probe a Phase 1 device or a device that requires its Net:Node to
0437  * be set via an ioctl.
0438  */
0439 static void aarp_send_probe_phase1(struct atalk_iface *iface)
0440 {
0441     struct ifreq atreq;
0442     struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
0443     const struct net_device_ops *ops = iface->dev->netdev_ops;
0444 
0445     sa->sat_addr.s_node = iface->address.s_node;
0446     sa->sat_addr.s_net = ntohs(iface->address.s_net);
0447 
0448     /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
0449     if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
0450         ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
0451         if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
0452             iface->address.s_node != sa->sat_addr.s_node)
0453             iface->status |= ATIF_PROBE_FAIL;
0454 
0455         iface->address.s_net  = htons(sa->sat_addr.s_net);
0456         iface->address.s_node = sa->sat_addr.s_node;
0457     }
0458 }
0459 
0460 
0461 void aarp_probe_network(struct atalk_iface *atif)
0462 {
0463     if (atif->dev->type == ARPHRD_LOCALTLK ||
0464         atif->dev->type == ARPHRD_PPP)
0465         aarp_send_probe_phase1(atif);
0466     else {
0467         unsigned int count;
0468 
0469         for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
0470             aarp_send_probe(atif->dev, &atif->address);
0471 
0472             /* Defer 1/10th */
0473             msleep(100);
0474 
0475             if (atif->status & ATIF_PROBE_FAIL)
0476                 break;
0477         }
0478     }
0479 }
0480 
0481 int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
0482 {
0483     int hash, retval = -EPROTONOSUPPORT;
0484     struct aarp_entry *entry;
0485     unsigned int count;
0486 
0487     /*
0488      * we don't currently support LocalTalk or PPP for proxy AARP;
0489      * if someone wants to try and add it, have fun
0490      */
0491     if (atif->dev->type == ARPHRD_LOCALTLK ||
0492         atif->dev->type == ARPHRD_PPP)
0493         goto out;
0494 
0495     /*
0496      * create a new AARP entry with the flags set to be published --
0497      * we need this one to hang around even if it's in use
0498      */
0499     entry = aarp_alloc();
0500     retval = -ENOMEM;
0501     if (!entry)
0502         goto out;
0503 
0504     entry->expires_at = -1;
0505     entry->status = ATIF_PROBE;
0506     entry->target_addr.s_node = sa->s_node;
0507     entry->target_addr.s_net = sa->s_net;
0508     entry->dev = atif->dev;
0509 
0510     write_lock_bh(&aarp_lock);
0511 
0512     hash = sa->s_node % (AARP_HASH_SIZE - 1);
0513     entry->next = proxies[hash];
0514     proxies[hash] = entry;
0515 
0516     for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
0517         aarp_send_probe(atif->dev, sa);
0518 
0519         /* Defer 1/10th */
0520         write_unlock_bh(&aarp_lock);
0521         msleep(100);
0522         write_lock_bh(&aarp_lock);
0523 
0524         if (entry->status & ATIF_PROBE_FAIL)
0525             break;
0526     }
0527 
0528     if (entry->status & ATIF_PROBE_FAIL) {
0529         entry->expires_at = jiffies - 1; /* free the entry */
0530         retval = -EADDRINUSE; /* return network full */
0531     } else { /* clear the probing flag */
0532         entry->status &= ~ATIF_PROBE;
0533         retval = 1;
0534     }
0535 
0536     write_unlock_bh(&aarp_lock);
0537 out:
0538     return retval;
0539 }
0540 
0541 /* Send a DDP frame */
0542 int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
0543           struct atalk_addr *sa, void *hwaddr)
0544 {
0545     static char ddp_eth_multicast[ETH_ALEN] =
0546         { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
0547     int hash;
0548     struct aarp_entry *a;
0549 
0550     skb_reset_network_header(skb);
0551 
0552     /* Check for LocalTalk first */
0553     if (dev->type == ARPHRD_LOCALTLK) {
0554         struct atalk_addr *at = atalk_find_dev_addr(dev);
0555         struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
0556         int ft = 2;
0557 
0558         /*
0559          * Compressible ?
0560          *
0561          * IFF: src_net == dest_net == device_net
0562          * (zero matches anything)
0563          */
0564 
0565         if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
0566             (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
0567             skb_pull(skb, sizeof(*ddp) - 4);
0568 
0569             /*
0570              *  The upper two remaining bytes are the port
0571              *  numbers we just happen to need. Now put the
0572              *  length in the lower two.
0573              */
0574             *((__be16 *)skb->data) = htons(skb->len);
0575             ft = 1;
0576         }
0577         /*
0578          * Nice and easy. No AARP type protocols occur here so we can
0579          * just shovel it out with a 3 byte LLAP header
0580          */
0581 
0582         skb_push(skb, 3);
0583         skb->data[0] = sa->s_node;
0584         skb->data[1] = at->s_node;
0585         skb->data[2] = ft;
0586         skb->dev     = dev;
0587         goto sendit;
0588     }
0589 
0590     /* On a PPP link we neither compress nor aarp.  */
0591     if (dev->type == ARPHRD_PPP) {
0592         skb->protocol = htons(ETH_P_PPPTALK);
0593         skb->dev = dev;
0594         goto sendit;
0595     }
0596 
0597     /* Non ELAP we cannot do. */
0598     if (dev->type != ARPHRD_ETHER)
0599         goto free_it;
0600 
0601     skb->dev = dev;
0602     skb->protocol = htons(ETH_P_ATALK);
0603     hash = sa->s_node % (AARP_HASH_SIZE - 1);
0604 
0605     /* Do we have a resolved entry? */
0606     if (sa->s_node == ATADDR_BCAST) {
0607         /* Send it */
0608         ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
0609         goto sent;
0610     }
0611 
0612     write_lock_bh(&aarp_lock);
0613     a = __aarp_find_entry(resolved[hash], dev, sa);
0614 
0615     if (a) { /* Return 1 and fill in the address */
0616         a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
0617         ddp_dl->request(ddp_dl, skb, a->hwaddr);
0618         write_unlock_bh(&aarp_lock);
0619         goto sent;
0620     }
0621 
0622     /* Do we have an unresolved entry: This is the less common path */
0623     a = __aarp_find_entry(unresolved[hash], dev, sa);
0624     if (a) { /* Queue onto the unresolved queue */
0625         skb_queue_tail(&a->packet_queue, skb);
0626         goto out_unlock;
0627     }
0628 
0629     /* Allocate a new entry */
0630     a = aarp_alloc();
0631     if (!a) {
0632         /* Whoops slipped... good job it's an unreliable protocol 8) */
0633         write_unlock_bh(&aarp_lock);
0634         goto free_it;
0635     }
0636 
0637     /* Set up the queue */
0638     skb_queue_tail(&a->packet_queue, skb);
0639     a->expires_at    = jiffies + sysctl_aarp_resolve_time;
0640     a->dev       = dev;
0641     a->next      = unresolved[hash];
0642     a->target_addr   = *sa;
0643     a->xmit_count    = 0;
0644     unresolved[hash] = a;
0645     unresolved_count++;
0646 
0647     /* Send an initial request for the address */
0648     __aarp_send_query(a);
0649 
0650     /*
0651      * Switch to fast timer if needed (That is if this is the first
0652      * unresolved entry to get added)
0653      */
0654 
0655     if (unresolved_count == 1)
0656         mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
0657 
0658     /* Now finally, it is safe to drop the lock. */
0659 out_unlock:
0660     write_unlock_bh(&aarp_lock);
0661 
0662     /* Tell the ddp layer we have taken over for this frame. */
0663     goto sent;
0664 
0665 sendit:
0666     if (skb->sk)
0667         skb->priority = skb->sk->sk_priority;
0668     if (dev_queue_xmit(skb))
0669         goto drop;
0670 sent:
0671     return NET_XMIT_SUCCESS;
0672 free_it:
0673     kfree_skb(skb);
0674 drop:
0675     return NET_XMIT_DROP;
0676 }
0677 EXPORT_SYMBOL(aarp_send_ddp);
0678 
0679 /*
0680  *  An entry in the aarp unresolved queue has become resolved. Send
0681  *  all the frames queued under it.
0682  *
0683  *  Must run under aarp_lock.
0684  */
0685 static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
0686                 int hash)
0687 {
0688     struct sk_buff *skb;
0689 
0690     while (*list)
0691         if (*list == a) {
0692             unresolved_count--;
0693             *list = a->next;
0694 
0695             /* Move into the resolved list */
0696             a->next = resolved[hash];
0697             resolved[hash] = a;
0698 
0699             /* Kick frames off */
0700             while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
0701                 a->expires_at = jiffies +
0702                         sysctl_aarp_expiry_time * 10;
0703                 ddp_dl->request(ddp_dl, skb, a->hwaddr);
0704             }
0705         } else
0706             list = &((*list)->next);
0707 }
0708 
0709 /*
0710  *  This is called by the SNAP driver whenever we see an AARP SNAP
0711  *  frame. We currently only support Ethernet.
0712  */
0713 static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
0714             struct packet_type *pt, struct net_device *orig_dev)
0715 {
0716     struct elapaarp *ea = aarp_hdr(skb);
0717     int hash, ret = 0;
0718     __u16 function;
0719     struct aarp_entry *a;
0720     struct atalk_addr sa, *ma, da;
0721     struct atalk_iface *ifa;
0722 
0723     if (!net_eq(dev_net(dev), &init_net))
0724         goto out0;
0725 
0726     /* We only do Ethernet SNAP AARP. */
0727     if (dev->type != ARPHRD_ETHER)
0728         goto out0;
0729 
0730     /* Frame size ok? */
0731     if (!skb_pull(skb, sizeof(*ea)))
0732         goto out0;
0733 
0734     function = ntohs(ea->function);
0735 
0736     /* Sanity check fields. */
0737     if (function < AARP_REQUEST || function > AARP_PROBE ||
0738         ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
0739         ea->pa_src_zero || ea->pa_dst_zero)
0740         goto out0;
0741 
0742     /* Looks good. */
0743     hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
0744 
0745     /* Build an address. */
0746     sa.s_node = ea->pa_src_node;
0747     sa.s_net = ea->pa_src_net;
0748 
0749     /* Process the packet. Check for replies of me. */
0750     ifa = atalk_find_dev(dev);
0751     if (!ifa)
0752         goto out1;
0753 
0754     if (ifa->status & ATIF_PROBE &&
0755         ifa->address.s_node == ea->pa_dst_node &&
0756         ifa->address.s_net == ea->pa_dst_net) {
0757         ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
0758         goto out1;
0759     }
0760 
0761     /* Check for replies of proxy AARP entries */
0762     da.s_node = ea->pa_dst_node;
0763     da.s_net  = ea->pa_dst_net;
0764 
0765     write_lock_bh(&aarp_lock);
0766     a = __aarp_find_entry(proxies[hash], dev, &da);
0767 
0768     if (a && a->status & ATIF_PROBE) {
0769         a->status |= ATIF_PROBE_FAIL;
0770         /*
0771          * we do not respond to probe or request packets of
0772          * this address while we are probing this address
0773          */
0774         goto unlock;
0775     }
0776 
0777     switch (function) {
0778     case AARP_REPLY:
0779         if (!unresolved_count)  /* Speed up */
0780             break;
0781 
0782         /* Find the entry.  */
0783         a = __aarp_find_entry(unresolved[hash], dev, &sa);
0784         if (!a || dev != a->dev)
0785             break;
0786 
0787         /* We can fill one in - this is good. */
0788         ether_addr_copy(a->hwaddr, ea->hw_src);
0789         __aarp_resolved(&unresolved[hash], a, hash);
0790         if (!unresolved_count)
0791             mod_timer(&aarp_timer,
0792                   jiffies + sysctl_aarp_expiry_time);
0793         break;
0794 
0795     case AARP_REQUEST:
0796     case AARP_PROBE:
0797 
0798         /*
0799          * If it is my address set ma to my address and reply.
0800          * We can treat probe and request the same.  Probe
0801          * simply means we shouldn't cache the querying host,
0802          * as in a probe they are proposing an address not
0803          * using one.
0804          *
0805          * Support for proxy-AARP added. We check if the
0806          * address is one of our proxies before we toss the
0807          * packet out.
0808          */
0809 
0810         sa.s_node = ea->pa_dst_node;
0811         sa.s_net  = ea->pa_dst_net;
0812 
0813         /* See if we have a matching proxy. */
0814         ma = __aarp_proxy_find(dev, &sa);
0815         if (!ma)
0816             ma = &ifa->address;
0817         else { /* We need to make a copy of the entry. */
0818             da.s_node = sa.s_node;
0819             da.s_net = sa.s_net;
0820             ma = &da;
0821         }
0822 
0823         if (function == AARP_PROBE) {
0824             /*
0825              * A probe implies someone trying to get an
0826              * address. So as a precaution flush any
0827              * entries we have for this address.
0828              */
0829             a = __aarp_find_entry(resolved[sa.s_node %
0830                                (AARP_HASH_SIZE - 1)],
0831                           skb->dev, &sa);
0832 
0833             /*
0834              * Make it expire next tick - that avoids us
0835              * getting into a probe/flush/learn/probe/
0836              * flush/learn cycle during probing of a slow
0837              * to respond host addr.
0838              */
0839             if (a) {
0840                 a->expires_at = jiffies - 1;
0841                 mod_timer(&aarp_timer, jiffies +
0842                       sysctl_aarp_tick_time);
0843             }
0844         }
0845 
0846         if (sa.s_node != ma->s_node)
0847             break;
0848 
0849         if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
0850             break;
0851 
0852         sa.s_node = ea->pa_src_node;
0853         sa.s_net = ea->pa_src_net;
0854 
0855         /* aarp_my_address has found the address to use for us.
0856          */
0857         aarp_send_reply(dev, ma, &sa, ea->hw_src);
0858         break;
0859     }
0860 
0861 unlock:
0862     write_unlock_bh(&aarp_lock);
0863 out1:
0864     ret = 1;
0865 out0:
0866     kfree_skb(skb);
0867     return ret;
0868 }
0869 
0870 static struct notifier_block aarp_notifier = {
0871     .notifier_call = aarp_device_event,
0872 };
0873 
0874 static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
0875 
0876 int __init aarp_proto_init(void)
0877 {
0878     int rc;
0879 
0880     aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
0881     if (!aarp_dl) {
0882         printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
0883         return -ENOMEM;
0884     }
0885     timer_setup(&aarp_timer, aarp_expire_timeout, 0);
0886     aarp_timer.expires  = jiffies + sysctl_aarp_expiry_time;
0887     add_timer(&aarp_timer);
0888     rc = register_netdevice_notifier(&aarp_notifier);
0889     if (rc) {
0890         del_timer_sync(&aarp_timer);
0891         unregister_snap_client(aarp_dl);
0892     }
0893     return rc;
0894 }
0895 
0896 /* Remove the AARP entries associated with a device. */
0897 void aarp_device_down(struct net_device *dev)
0898 {
0899     int ct;
0900 
0901     write_lock_bh(&aarp_lock);
0902 
0903     for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
0904         __aarp_expire_device(&resolved[ct], dev);
0905         __aarp_expire_device(&unresolved[ct], dev);
0906         __aarp_expire_device(&proxies[ct], dev);
0907     }
0908 
0909     write_unlock_bh(&aarp_lock);
0910 }
0911 
0912 #ifdef CONFIG_PROC_FS
0913 /*
0914  * Get the aarp entry that is in the chain described
0915  * by the iterator.
0916  * If pos is set then skip till that index.
0917  * pos = 1 is the first entry
0918  */
0919 static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
0920 {
0921     int ct = iter->bucket;
0922     struct aarp_entry **table = iter->table;
0923     loff_t off = 0;
0924     struct aarp_entry *entry;
0925 
0926  rescan:
0927     while (ct < AARP_HASH_SIZE) {
0928         for (entry = table[ct]; entry; entry = entry->next) {
0929             if (!pos || ++off == *pos) {
0930                 iter->table = table;
0931                 iter->bucket = ct;
0932                 return entry;
0933             }
0934         }
0935         ++ct;
0936     }
0937 
0938     if (table == resolved) {
0939         ct = 0;
0940         table = unresolved;
0941         goto rescan;
0942     }
0943     if (table == unresolved) {
0944         ct = 0;
0945         table = proxies;
0946         goto rescan;
0947     }
0948     return NULL;
0949 }
0950 
0951 static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
0952     __acquires(aarp_lock)
0953 {
0954     struct aarp_iter_state *iter = seq->private;
0955 
0956     read_lock_bh(&aarp_lock);
0957     iter->table     = resolved;
0958     iter->bucket    = 0;
0959 
0960     return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
0961 }
0962 
0963 static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
0964 {
0965     struct aarp_entry *entry = v;
0966     struct aarp_iter_state *iter = seq->private;
0967 
0968     ++*pos;
0969 
0970     /* first line after header */
0971     if (v == SEQ_START_TOKEN)
0972         entry = iter_next(iter, NULL);
0973 
0974     /* next entry in current bucket */
0975     else if (entry->next)
0976         entry = entry->next;
0977 
0978     /* next bucket or table */
0979     else {
0980         ++iter->bucket;
0981         entry = iter_next(iter, NULL);
0982     }
0983     return entry;
0984 }
0985 
0986 static void aarp_seq_stop(struct seq_file *seq, void *v)
0987     __releases(aarp_lock)
0988 {
0989     read_unlock_bh(&aarp_lock);
0990 }
0991 
0992 static const char *dt2str(unsigned long ticks)
0993 {
0994     static char buf[32];
0995 
0996     sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100) / HZ);
0997 
0998     return buf;
0999 }
1000 
1001 static int aarp_seq_show(struct seq_file *seq, void *v)
1002 {
1003     struct aarp_iter_state *iter = seq->private;
1004     struct aarp_entry *entry = v;
1005     unsigned long now = jiffies;
1006 
1007     if (v == SEQ_START_TOKEN)
1008         seq_puts(seq,
1009              "Address  Interface   Hardware Address"
1010              "   Expires LastSend  Retry Status\n");
1011     else {
1012         seq_printf(seq, "%04X:%02X  %-12s",
1013                ntohs(entry->target_addr.s_net),
1014                (unsigned int) entry->target_addr.s_node,
1015                entry->dev ? entry->dev->name : "????");
1016         seq_printf(seq, "%pM", entry->hwaddr);
1017         seq_printf(seq, " %8s",
1018                dt2str((long)entry->expires_at - (long)now));
1019         if (iter->table == unresolved)
1020             seq_printf(seq, " %8s %6hu",
1021                    dt2str(now - entry->last_sent),
1022                    entry->xmit_count);
1023         else
1024             seq_puts(seq, "                ");
1025         seq_printf(seq, " %s\n",
1026                (iter->table == resolved) ? "resolved"
1027                : (iter->table == unresolved) ? "unresolved"
1028                : (iter->table == proxies) ? "proxies"
1029                : "unknown");
1030     }
1031     return 0;
1032 }
1033 
1034 const struct seq_operations aarp_seq_ops = {
1035     .start  = aarp_seq_start,
1036     .next   = aarp_seq_next,
1037     .stop   = aarp_seq_stop,
1038     .show   = aarp_seq_show,
1039 };
1040 #endif
1041 
1042 /* General module cleanup. Called from cleanup_module() in ddp.c. */
1043 void aarp_cleanup_module(void)
1044 {
1045     del_timer_sync(&aarp_timer);
1046     unregister_netdevice_notifier(&aarp_notifier);
1047     unregister_snap_client(aarp_dl);
1048     aarp_purge();
1049 }