home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / net / inet / arp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-23  |  27.9 KB  |  1,282 lines

  1. /* linux/net/inet/arp.c
  2.  *
  3.  * Copyright (C) 1994 by Florian  La Roche
  4.  *
  5.  * This module implements the Address Resolution Protocol ARP (RFC 826),
  6.  * which is used to convert IP addresses (or in the future maybe other
  7.  * high-level addresses into a low-level hardware address (like an Ethernet
  8.  * address).
  9.  *
  10.  * FIXME:
  11.  *    Experiment with better retransmit timers
  12.  *    Clean up the timer deletions
  13.  *    If you create a proxy entry set your interface address to the address
  14.  *    and then delete it, proxies may get out of sync with reality - check this
  15.  *
  16.  * This program is free software; you can redistribute it and/or
  17.  * modify it under the terms of the GNU General Public License
  18.  * as published by the Free Software Foundation; either version
  19.  * 2 of the License, or (at your option) any later version.
  20.  *
  21.  *
  22.  * Fixes:
  23.  *        Alan Cox    :    Removed the ethernet assumptions in Florian's code
  24.  *        Alan Cox    :    Fixed some small errors in the ARP logic
  25.  *        Alan Cox    :    Allow >4K in /proc
  26.  *        Alan Cox    :    Make ARP add its own protocol entry
  27.  *
  28.  *              Ross Martin     :       Rewrote arp_rcv() and arp_get_info()
  29.  *        Stephen Henson    :    Add AX25 support to arp_get_info()
  30.  *        Alan Cox    :    Drop data when a device is downed.
  31.  *        Alan Cox    :    Use init_timer().
  32.  *        Alan Cox    :    Double lock fixes.
  33.  *        Martin Seine    :    Move the arphdr structure
  34.  *                    to if_arp.h for compatibility.
  35.  *                    with BSD based programs.
  36.  *              Andrew Tridgell :       Added ARP netmask code and
  37.  *                                      re-arranged proxy handling.
  38.  *        Alan Cox    :    Changed to use notifiers.
  39.  *        Niibe Yutaka    :    Reply for this device or proxies only.
  40.  *        Alan Cox    :    Don't proxy across hardware types!
  41.  */
  42.  
  43. #include <linux/types.h>
  44. #include <linux/string.h>
  45. #include <linux/kernel.h>
  46. #include <linux/sched.h>
  47. #include <linux/config.h>
  48. #include <linux/socket.h>
  49. #include <linux/sockios.h>
  50. #include <linux/errno.h>
  51. #include <linux/if_arp.h>
  52. #include <linux/in.h>
  53. #include <linux/mm.h>
  54. #include <asm/system.h>
  55. #include <asm/segment.h>
  56. #include <stdarg.h>
  57. #include <linux/inet.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/etherdevice.h>
  60. #include "ip.h"
  61. #include "route.h"
  62. #include "protocol.h"
  63. #include "tcp.h"
  64. #include <linux/skbuff.h>
  65. #include "sock.h"
  66. #include "arp.h"
  67. #ifdef CONFIG_AX25
  68. #include "ax25.h"
  69. #endif
  70.  
  71.  
  72. /*
  73.  *    This structure defines the ARP mapping cache. As long as we make changes
  74.  *    in this structure, we keep interrupts of. But normally we can copy the
  75.  *    hardware address and the device pointer in a local variable and then make
  76.  *    any "long calls" to send a packet out.
  77.  */
  78.  
  79. struct arp_table
  80. {
  81.     struct arp_table        *next;            /* Linked entry list         */
  82.     unsigned long            last_used;        /* For expiry             */
  83.     unsigned int            flags;            /* Control status         */
  84.     unsigned long            ip;            /* ip address of entry         */
  85.     unsigned long            mask;            /* netmask - used for generalised proxy arps (tridge)         */
  86.     unsigned char            ha[MAX_ADDR_LEN];    /* Hardware address        */
  87.     unsigned char            hlen;            /* Length of hardware address     */
  88.     unsigned short            htype;            /* Type of hardware in use    */
  89.     struct device            *dev;            /* Device the entry is tied to     */
  90.  
  91.     /*
  92.      *    The following entries are only used for unresolved hw addresses.
  93.      */
  94.     
  95.     struct timer_list        timer;            /* expire timer         */
  96.     int                retries;        /* remaining retries         */
  97.     struct sk_buff_head        skb;            /* list of queued packets     */
  98. };
  99.  
  100.  
  101. /*
  102.  *    Configurable Parameters (don't touch unless you know what you are doing
  103.  */
  104.  
  105. /*
  106.  *    If an arp request is send, ARP_RES_TIME is the timeout value until the
  107.  *    next request is send.
  108.  */
  109.  
  110. #define ARP_RES_TIME        (250*(HZ/10))
  111.  
  112. /*
  113.  *    The number of times an arp request is send, until the host is
  114.  *    considered unreachable.
  115.  */
  116.  
  117. #define ARP_MAX_TRIES        3
  118.  
  119. /*
  120.  *    After that time, an unused entry is deleted from the arp table.
  121.  */
  122.  
  123. #define ARP_TIMEOUT        (600*HZ)
  124.  
  125. /*
  126.  *    How often is the function 'arp_check_retries' called.
  127.  *    An entry is invalidated in the time between ARP_TIMEOUT and
  128.  *    (ARP_TIMEOUT+ARP_CHECK_INTERVAL).
  129.  */
  130.  
  131. #define ARP_CHECK_INTERVAL    (60 * HZ)
  132.  
  133. /* Forward declarations. */
  134. static void arp_check_expire (unsigned long);  
  135. static struct arp_table *arp_lookup(unsigned long paddr, int exact);
  136.  
  137.  
  138. static struct timer_list arp_timer =
  139.     { NULL, NULL, ARP_CHECK_INTERVAL, 0L, &arp_check_expire };
  140.  
  141. /*
  142.  * The default arp netmask is just 255.255.255.255 which means it's
  143.  * a single machine entry. Only proxy entries can have other netmasks
  144.  *
  145. */
  146.  
  147. #define DEF_ARP_NETMASK (~0)
  148.  
  149.  
  150. /*
  151.  *     The size of the hash table. Must be a power of two.
  152.  *     Maybe we should remove hashing in the future for arp and concentrate
  153.  *     on Patrick Schaaf's Host-Cache-Lookup...
  154.  */
  155.  
  156.  
  157. #define ARP_TABLE_SIZE  16
  158.  
  159. /* The ugly +1 here is to cater for proxy entries. They are put in their 
  160.    own list for efficiency of lookup. If you don't want to find a proxy
  161.    entry then don't look in the last entry, otherwise do 
  162. */
  163.  
  164. #define FULL_ARP_TABLE_SIZE (ARP_TABLE_SIZE+1)
  165.  
  166. struct arp_table *arp_tables[FULL_ARP_TABLE_SIZE] =
  167. {
  168.     NULL,
  169. };
  170.  
  171.  
  172. /*
  173.  *    The last bits in the IP address are used for the cache lookup.
  174.  *      A special entry is used for proxy arp entries
  175.  */
  176.  
  177. #define HASH(paddr)         (htonl(paddr) & (ARP_TABLE_SIZE - 1))
  178. #define PROXY_HASH ARP_TABLE_SIZE
  179.  
  180. /*
  181.  *    Check if there are too old entries and remove them. If the ATF_PERM
  182.  *    flag is set, they are always left in the arp cache (permanent entry).
  183.  *    Note: Only fully resolved entries, which don't have any packets in
  184.  *    the queue, can be deleted, since ARP_TIMEOUT is much greater than
  185.  *    ARP_MAX_TRIES*ARP_RES_TIME.
  186.  */
  187.  
  188. static void arp_check_expire(unsigned long dummy)
  189. {
  190.     int i;
  191.     unsigned long now = jiffies;
  192.     unsigned long flags;
  193.     save_flags(flags);
  194.     cli();
  195.  
  196.     for (i = 0; i < FULL_ARP_TABLE_SIZE; i++)
  197.     {
  198.         struct arp_table *entry;
  199.         struct arp_table **pentry = &arp_tables[i];
  200.  
  201.         while ((entry = *pentry) != NULL)
  202.         {
  203.             if ((now - entry->last_used) > ARP_TIMEOUT
  204.                 && !(entry->flags & ATF_PERM))
  205.             {
  206.                 *pentry = entry->next;    /* remove from list */
  207.                 del_timer(&entry->timer);    /* Paranoia */
  208.                 kfree_s(entry, sizeof(struct arp_table));
  209.             }
  210.             else
  211.                 pentry = &entry->next;    /* go to next entry */
  212.         }
  213.     }
  214.     restore_flags(flags);
  215.  
  216.     /*
  217.      *    Set the timer again.
  218.      */
  219.  
  220.     del_timer(&arp_timer);
  221.     arp_timer.expires = ARP_CHECK_INTERVAL;
  222.     add_timer(&arp_timer);
  223. }
  224.  
  225.  
  226. /*
  227.  *    Release all linked skb's and the memory for this entry.
  228.  */
  229.  
  230. static void arp_release_entry(struct arp_table *entry)
  231. {
  232.     struct sk_buff *skb;
  233.     unsigned long flags;
  234.  
  235.     save_flags(flags);
  236.     cli();
  237.     /* Release the list of `skb' pointers. */
  238.     while ((skb = skb_dequeue(&entry->skb)) != NULL)
  239.     {
  240.         skb_device_lock(skb);
  241.         restore_flags(flags);
  242.         dev_kfree_skb(skb, FREE_WRITE);
  243.     }
  244.     restore_flags(flags);
  245.     del_timer(&entry->timer);
  246.     kfree_s(entry, sizeof(struct arp_table));
  247.     return;
  248. }
  249.  
  250. /*
  251.  *    Purge a device from the ARP queue
  252.  */
  253.  
  254. int arp_device_event(unsigned long event, void *ptr)
  255. {
  256.     struct device *dev=ptr;
  257.     int i;
  258.     unsigned long flags;
  259.     
  260.     if(event!=NETDEV_DOWN)
  261.         return NOTIFY_DONE;
  262.     /*
  263.      *    This is a bit OTT - maybe we need some arp semaphores instead.
  264.      */
  265.      
  266.     save_flags(flags);
  267.     cli();
  268.     for (i = 0; i < FULL_ARP_TABLE_SIZE; i++)
  269.     {
  270.         struct arp_table *entry;
  271.         struct arp_table **pentry = &arp_tables[i];
  272.  
  273.         while ((entry = *pentry) != NULL)
  274.         {
  275.             if(entry->dev==dev)
  276.             {
  277.                 *pentry = entry->next;    /* remove from list */
  278.                 del_timer(&entry->timer);    /* Paranoia */
  279.                 kfree_s(entry, sizeof(struct arp_table));
  280.             }
  281.             else
  282.                 pentry = &entry->next;    /* go to next entry */
  283.         }
  284.     }
  285.     restore_flags(flags);
  286.     return NOTIFY_DONE;
  287. }
  288.  
  289.  
  290. /*
  291.  *    Create and send an arp packet. If (dest_hw == NULL), we create a broadcast
  292.  *    message.
  293.  */
  294.  
  295. void arp_send(int type, int ptype, unsigned long dest_ip, 
  296.           struct device *dev, unsigned long src_ip, 
  297.           unsigned char *dest_hw, unsigned char *src_hw)
  298. {
  299.     struct sk_buff *skb;
  300.     struct arphdr *arp;
  301.     unsigned char *arp_ptr;
  302.  
  303.     /*
  304.      *    No arp on this interface.
  305.      */
  306.     
  307.     if(dev->flags&IFF_NOARP)
  308.         return;
  309.  
  310.     /*
  311.      *    Allocate a buffer
  312.      */
  313.     
  314.     skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4)
  315.                 + dev->hard_header_len, GFP_ATOMIC);
  316.     if (skb == NULL)
  317.     {
  318.         printk("ARP: no memory to send an arp packet\n");
  319.         return;
  320.     }
  321.     skb->len = sizeof(struct arphdr) + dev->hard_header_len + 2*(dev->addr_len+4);
  322.     skb->arp = 1;
  323.     skb->dev = dev;
  324.     skb->free = 1;
  325.  
  326.     /*
  327.      *    Fill the device header for the ARP frame
  328.      */
  329.  
  330.     dev->hard_header(skb->data,dev,ptype,dest_hw?dest_hw:dev->broadcast,src_hw?src_hw:NULL,skb->len,skb);
  331.  
  332.     /* Fill out the arp protocol part. */
  333.     arp = (struct arphdr *) (skb->data + dev->hard_header_len);
  334.     arp->ar_hrd = htons(dev->type);
  335. #ifdef CONFIG_AX25
  336.     arp->ar_pro = (dev->type != ARPHRD_AX25)? htons(ETH_P_IP) : htons(AX25_P_IP);
  337. #else
  338.     arp->ar_pro = htons(ETH_P_IP);
  339. #endif
  340.     arp->ar_hln = dev->addr_len;
  341.     arp->ar_pln = 4;
  342.     arp->ar_op = htons(type);
  343.  
  344.     arp_ptr=(unsigned char *)(arp+1);
  345.  
  346.     memcpy(arp_ptr, src_hw, dev->addr_len);
  347.     arp_ptr+=dev->addr_len;
  348.     memcpy(arp_ptr, &src_ip,4);
  349.     arp_ptr+=4;
  350.     if (dest_hw != NULL)
  351.         memcpy(arp_ptr, dest_hw, dev->addr_len);
  352.     else
  353.         memset(arp_ptr, 0, dev->addr_len);
  354.     arp_ptr+=dev->addr_len;
  355.     memcpy(arp_ptr, &dest_ip, 4);
  356.  
  357.     dev_queue_xmit(skb, dev, 0);
  358. }
  359.  
  360.  
  361. /*
  362.  *    This function is called, if an entry is not resolved in ARP_RES_TIME.
  363.  *    Either resend a request, or give it up and free the entry.
  364.  */
  365.  
  366. static void arp_expire_request (unsigned long arg)
  367. {
  368.     struct arp_table *entry = (struct arp_table *) arg;
  369.     struct arp_table **pentry;
  370.     unsigned long hash;
  371.     unsigned long flags;
  372.  
  373.     save_flags(flags);
  374.     cli();
  375.  
  376.     /*
  377.      *    Since all timeouts are handled with interrupts enabled, there is a
  378.      *    small chance, that this entry has just been resolved by an incoming
  379.      *    packet. This is the only race condition, but it is handled...
  380.      */
  381.     
  382.     if (entry->flags & ATF_COM)
  383.     {
  384.         restore_flags(flags);
  385.         return;
  386.     }
  387.  
  388.     if (--entry->retries > 0)
  389.     {
  390.         unsigned long ip = entry->ip;
  391.         struct device *dev = entry->dev;
  392.  
  393.         /* Set new timer. */
  394.         del_timer(&entry->timer);
  395.         entry->timer.expires = ARP_RES_TIME;
  396.         add_timer(&entry->timer);
  397.         restore_flags(flags);
  398.         arp_send(ARPOP_REQUEST, ETH_P_ARP, ip, dev, dev->pa_addr, 
  399.              NULL, dev->dev_addr);
  400.         return;
  401.     }
  402.  
  403.     /*
  404.      *    Arp request timed out. Delete entry and all waiting packets.
  405.      *    If we give each entry a pointer to itself, we don't have to
  406.      *    loop through everything again. Maybe hash is good enough, but
  407.      *    I will look at it later.
  408.      */
  409.  
  410.     hash = HASH(entry->ip);
  411.  
  412.     /* proxy entries shouldn't really time out so this is really
  413.        only here for completeness
  414.     */
  415.     if (entry->flags & ATF_PUBL)
  416.       pentry = &arp_tables[PROXY_HASH];
  417.     else
  418.       pentry = &arp_tables[hash];
  419.     while (*pentry != NULL)
  420.     {
  421.         if (*pentry == entry)
  422.         {
  423.             *pentry = entry->next;    /* delete from linked list */
  424.             del_timer(&entry->timer);
  425.             restore_flags(flags);
  426.             arp_release_entry(entry);
  427.             return;
  428.         }
  429.         pentry = &(*pentry)->next;
  430.     }
  431.     restore_flags(flags);
  432.     printk("Possible ARP queue corruption.\n");
  433.     /*
  434.      *    We should never arrive here.
  435.      */
  436. }
  437.  
  438.  
  439. /*
  440.  *    This will try to retransmit everything on the queue.
  441.  */
  442.  
  443. static void arp_send_q(struct arp_table *entry, unsigned char *hw_dest)
  444. {
  445.     struct sk_buff *skb;
  446.  
  447.     unsigned long flags;
  448.  
  449.     /*
  450.      *    Empty the entire queue, building its data up ready to send
  451.      */
  452.     
  453.     if(!(entry->flags&ATF_COM))
  454.     {
  455.         printk("arp_send_q: incomplete entry for %s\n",
  456.                 in_ntoa(entry->ip));
  457.         return;
  458.     }
  459.  
  460.     save_flags(flags);
  461.     
  462.     cli();
  463.     while((skb = skb_dequeue(&entry->skb)) != NULL)
  464.     {
  465.         IS_SKB(skb);
  466.         skb_device_lock(skb);
  467.         restore_flags(flags);
  468.         if(!skb->dev->rebuild_header(skb->data,skb->dev,skb->raddr,skb))
  469.         {
  470.             skb->arp  = 1;
  471.             if(skb->sk==NULL)
  472.                 dev_queue_xmit(skb, skb->dev, 0);
  473.             else
  474.                 dev_queue_xmit(skb,skb->dev,skb->sk->priority);
  475.         }
  476.         else
  477.         {
  478.             /* This routine is only ever called when 'entry' is
  479.                complete. Thus this can't fail. */
  480.             printk("arp_send_q: The impossible occurred. Please notify Alan.\n");
  481.             printk("arp_send_q: active entity %s\n",in_ntoa(entry->ip));
  482.             printk("arp_send_q: failed to find %s\n",in_ntoa(skb->raddr));
  483.         }
  484.     }
  485.     restore_flags(flags);
  486. }
  487.  
  488.  
  489. /*
  490.  *    Delete an ARP mapping entry in the cache.
  491.  */
  492.  
  493. void arp_destroy(unsigned long ip_addr, int force)
  494. {
  495.         int checked_proxies = 0;
  496.     struct arp_table *entry;
  497.     struct arp_table **pentry;
  498.     unsigned long hash = HASH(ip_addr);
  499.  
  500.     cli();
  501.     pentry = &arp_tables[hash];
  502.     if (! *pentry) /* also check proxy entries */
  503.       pentry = &arp_tables[PROXY_HASH];
  504.  
  505.     while ((entry = *pentry) != NULL)
  506.     {
  507.         if (entry->ip == ip_addr)
  508.         {
  509.             if ((entry->flags & ATF_PERM) && !force)
  510.                 return;
  511.             *pentry = entry->next;
  512.             del_timer(&entry->timer);
  513.             sti();
  514.             arp_release_entry(entry);
  515.             return;
  516.         }
  517.         pentry = &entry->next;
  518.         if (!checked_proxies && ! *pentry)
  519.           { /* ugly. we have to make sure we check proxy
  520.                entries as well */
  521.             checked_proxies = 1;
  522.             pentry = &arp_tables[PROXY_HASH];
  523.           }
  524.     }
  525.     sti();
  526. }
  527.  
  528.  
  529. /*
  530.  *    Receive an arp request by the device layer. Maybe I rewrite it, to
  531.  *    use the incoming packet for the reply. The time for the current
  532.  *    "overhead" isn't that high...
  533.  */
  534.  
  535. int arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
  536. {
  537. /*
  538.  *    We shouldn't use this type conversion. Check later.
  539.  */
  540.     
  541.     struct arphdr *arp = (struct arphdr *)skb->h.raw;
  542.     unsigned char *arp_ptr= (unsigned char *)(arp+1);
  543.     struct arp_table *entry;
  544.     struct arp_table *proxy_entry;
  545.     int addr_hint,hlen,htype;
  546.     unsigned long hash;
  547.     unsigned char ha[MAX_ADDR_LEN];    /* So we can enable ints again. */
  548.     long sip,tip;
  549.     unsigned char *sha,*tha;
  550.  
  551. /*
  552.  *    The hardware length of the packet should match the hardware length
  553.  *    of the device.  Similarly, the hardware types should match.  The
  554.  *    device should be ARP-able.  Also, if pln is not 4, then the lookup
  555.  *    is not from an IP number.  We can't currently handle this, so toss
  556.  *    it. 
  557.  */  
  558.     if (arp->ar_hln != dev->addr_len    || 
  559.              dev->type != ntohs(arp->ar_hrd) || 
  560.         dev->flags & IFF_NOARP          ||
  561.         arp->ar_pln != 4)
  562.     {
  563.         kfree_skb(skb, FREE_READ);
  564.         return 0;
  565.     }
  566.  
  567. /*
  568.  *    Another test.
  569.  *    The logic here is that the protocol being looked up by arp should 
  570.  *    match the protocol the device speaks.  If it doesn't, there is a
  571.  *    problem, so toss the packet.
  572.  */
  573.       switch(dev->type)
  574.       {
  575. #ifdef CONFIG_AX25
  576.         case ARPHRD_AX25:
  577.             if(arp->ar_pro != htons(AX25_P_IP))
  578.             {
  579.                 kfree_skb(skb, FREE_READ);
  580.                 return 0;
  581.             }
  582.             break;
  583. #endif
  584.         case ARPHRD_ETHER:
  585.         case ARPHRD_ARCNET:
  586.             if(arp->ar_pro != htons(ETH_P_IP))
  587.             {
  588.                 kfree_skb(skb, FREE_READ);
  589.                 return 0;
  590.             }
  591.             break;
  592.  
  593.         default:
  594.             printk("ARP: dev->type mangled!\n");
  595.             kfree_skb(skb, FREE_READ);
  596.             return 0;
  597.     }
  598.  
  599. /*
  600.  *    Extract fields
  601.  */
  602.  
  603.     hlen  = dev->addr_len;
  604.     htype = dev->type;
  605.  
  606.     sha=arp_ptr;
  607.     arp_ptr+=hlen;
  608.     memcpy(&sip,arp_ptr,4);
  609.     arp_ptr+=4;
  610.     tha=arp_ptr;
  611.     arp_ptr+=hlen;
  612.     memcpy(&tip,arp_ptr,4);
  613.   
  614. /* 
  615.  *    Check for bad requests for 127.0.0.1.  If this is one such, delete it.
  616.  */
  617.     if(tip == INADDR_LOOPBACK)
  618.     {
  619.         kfree_skb(skb, FREE_READ);
  620.         return 0;
  621.     }
  622.  
  623. /*
  624.  *  Process entry.  The idea here is we want to send a reply if it is a
  625.  *  request for us or if it is a request for someone else that we hold
  626.  *  a proxy for.  We want to add an entry to our cache if it is a reply
  627.  *  to us or if it is a request for our address.  
  628.  *  (The assumption for this last is that if someone is requesting our 
  629.  *  address, they are probably intending to talk to us, so it saves time 
  630.  *  if we cache their address.  Their address is also probably not in 
  631.  *  our cache, since ours is not in their cache.)
  632.  * 
  633.  *  Putting this another way, we only care about replies if they are to
  634.  *  us, in which case we add them to the cache.  For requests, we care
  635.  *  about those for us and those for our proxies.  We reply to both,
  636.  *  and in the case of requests for us we add the requester to the arp 
  637.  *  cache.
  638.  */
  639.  
  640.     addr_hint = ip_chk_addr(tip);
  641.  
  642.     if(arp->ar_op == htons(ARPOP_REPLY))
  643.     {
  644.         if(addr_hint!=IS_MYADDR)
  645.         {
  646. /* 
  647.  *    Replies to other machines get tossed. 
  648.  */
  649.             kfree_skb(skb, FREE_READ);
  650.             return 0;
  651.         }
  652. /*
  653.  *    Fall through to code below that adds sender to cache. 
  654.  */
  655.     }
  656.     else
  657.     { 
  658. /* 
  659.  *     It is now an arp request 
  660.  */
  661. /*
  662.  * Only reply for the real device address or when it's in our proxy tables
  663.  */
  664.         if(tip!=dev->pa_addr)
  665.         {
  666. /*
  667.  *     To get in here, it is a request for someone else.  We need to
  668.  *     check if that someone else is one of our proxies.  If it isn't,
  669.  *     we can toss it.
  670.  */
  671.             cli();
  672.             for(proxy_entry=arp_tables[PROXY_HASH];
  673.                 proxy_entry;
  674.                 proxy_entry = proxy_entry->next)
  675.             {
  676.               /* we will respond to a proxy arp request
  677.                  if the masked arp table ip matches the masked
  678.                  tip. This allows a single proxy arp table
  679.                  entry to be used on a gateway machine to handle
  680.                  all requests for a whole network, rather than
  681.                  having to use a huge number of proxy arp entries
  682.                  and having to keep them uptodate.
  683.                  */
  684.               if (proxy_entry->dev != dev && proxy_entry->htype == htype &&
  685.                   !((proxy_entry->ip^tip)&proxy_entry->mask))
  686.                 break;
  687.  
  688.             }
  689.             if (proxy_entry)
  690.             {
  691.                 memcpy(ha, proxy_entry->ha, hlen);
  692.                 sti();
  693.                 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,ha);
  694.                 kfree_skb(skb, FREE_READ);
  695.                 return 0;
  696.             }
  697.             else
  698.             {
  699.                 sti();
  700.                 kfree_skb(skb, FREE_READ);
  701.                 return 0;
  702.             }
  703.         }
  704.         else
  705.         {
  706. /*
  707.  *     To get here, it must be an arp request for us.  We need to reply.
  708.  */
  709.             arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr);
  710.         }
  711.     }
  712.  
  713.  
  714. /*
  715.  * Now all replies are handled.  Next, anything that falls through to here
  716.  * needs to be added to the arp cache, or have its entry updated if it is 
  717.  * there.
  718.  */
  719.  
  720.     hash = HASH(sip);
  721.     cli();
  722.     for(entry=arp_tables[hash];entry;entry=entry->next)
  723.         if(entry->ip==sip && entry->htype==htype)
  724.             break;
  725.  
  726.     if(entry)
  727.     {
  728. /*
  729.  *    Entry found; update it.
  730.  */
  731.         memcpy(entry->ha, sha, hlen);
  732.         entry->hlen = hlen;
  733.         entry->last_used = jiffies;
  734.         if (!(entry->flags & ATF_COM))
  735.         {
  736. /*
  737.  *    This entry was incomplete.  Delete the retransmit timer
  738.  *    and switch to complete status.
  739.  */
  740.             del_timer(&entry->timer);
  741.             entry->flags |= ATF_COM;
  742.             sti();
  743. /* 
  744.  *    Send out waiting packets. We might have problems, if someone is 
  745.  *    manually removing entries right now -- entry might become invalid 
  746.  *    underneath us.
  747.  */
  748.             arp_send_q(entry, sha);
  749.         }
  750.         else
  751.         {
  752.             sti();
  753.         }
  754.     }
  755.     else
  756.     {
  757. /*
  758.  *     No entry found.  Need to add a new entry to the arp table.
  759.  */
  760.         entry = (struct arp_table *)kmalloc(sizeof(struct arp_table),GFP_ATOMIC);
  761.         if(entry == NULL)
  762.         {
  763.             sti();
  764.             printk("ARP: no memory for new arp entry\n");
  765.  
  766.             kfree_skb(skb, FREE_READ);
  767.             return 0;
  768.         }
  769.  
  770.                 entry->mask = DEF_ARP_NETMASK;
  771.         entry->ip = sip;
  772.         entry->hlen = hlen;
  773.         entry->htype = htype;
  774.         entry->flags = ATF_COM;
  775.         init_timer(&entry->timer);
  776.         memcpy(entry->ha, sha, hlen);
  777.         entry->last_used = jiffies;
  778.         entry->dev = skb->dev;
  779.         skb_queue_head_init(&entry->skb);
  780.         entry->next = arp_tables[hash];
  781.         arp_tables[hash] = entry;
  782.         sti();
  783.     }
  784.  
  785. /*
  786.  *    Replies have been sent, and entries have been added.  All done.
  787.  */
  788.     kfree_skb(skb, FREE_READ);
  789.     return 0;
  790. }
  791.  
  792.  
  793. /*
  794.  *    Find an arp mapping in the cache. If not found, post a request.
  795.  */
  796.  
  797. int arp_find(unsigned char *haddr, unsigned long paddr, struct device *dev,
  798.        unsigned long saddr, struct sk_buff *skb)
  799. {
  800.     struct arp_table *entry;
  801.     unsigned long hash;
  802. #ifdef CONFIG_IP_MULTICAST
  803.     unsigned long taddr;
  804. #endif    
  805.  
  806.     switch (ip_chk_addr(paddr))
  807.     {
  808.         case IS_MYADDR:
  809.             printk("ARP: arp called for own IP address\n");
  810.             memcpy(haddr, dev->dev_addr, dev->addr_len);
  811.             skb->arp = 1;
  812.             return 0;
  813. #ifdef CONFIG_IP_MULTICAST
  814.         case IS_MULTICAST:
  815.             if(dev->type==ARPHRD_ETHER || dev->type==ARPHRD_IEEE802)
  816.             {
  817.                 haddr[0]=0x01;
  818.                 haddr[1]=0x00;
  819.                 haddr[2]=0x5e;
  820.                 taddr=ntohl(paddr);
  821.                 haddr[5]=taddr&0xff;
  822.                 taddr=taddr>>8;
  823.                 haddr[4]=taddr&0xff;
  824.                 taddr=taddr>>8;
  825.                 haddr[3]=taddr&0x7f;
  826.                 return 0;
  827.             }
  828.         /*
  829.          *    If a device does not support multicast broadcast the stuff (eg AX.25 for now)
  830.          */
  831. #endif
  832.         
  833.         case IS_BROADCAST:
  834.             memcpy(haddr, dev->broadcast, dev->addr_len);
  835.             skb->arp = 1;
  836.             return 0;
  837.     }
  838.  
  839.     hash = HASH(paddr);
  840.     cli();
  841.  
  842.     /*
  843.      *    Find an entry
  844.      */
  845.     entry = arp_lookup(paddr, 1);
  846.  
  847.     if (entry != NULL)     /* It exists */
  848.     {
  849.             if (!(entry->flags & ATF_COM))
  850.             {
  851.             /*
  852.              *    A request was already send, but no reply yet. Thus
  853.              *    queue the packet with the previous attempt
  854.              */
  855.             
  856.             if (skb != NULL)
  857.             {
  858.                 skb_queue_tail(&entry->skb, skb);
  859.                 skb_device_unlock(skb);
  860.             }
  861.             sti();
  862.             return 1;
  863.         }
  864.  
  865.         /*
  866.          *    Update the record
  867.          */
  868.         
  869.         entry->last_used = jiffies;
  870.         memcpy(haddr, entry->ha, dev->addr_len);
  871.         if (skb)
  872.             skb->arp = 1;
  873.         sti();
  874.         return 0;
  875.     }
  876.  
  877.     /*
  878.      *    Create a new unresolved entry.
  879.      */
  880.     
  881.     entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
  882.                     GFP_ATOMIC);
  883.     if (entry != NULL)
  884.     {
  885.             entry->mask = DEF_ARP_NETMASK;
  886.         entry->ip = paddr;
  887.         entry->hlen = dev->addr_len;
  888.         entry->htype = dev->type;
  889.         entry->flags = 0;
  890.         memset(entry->ha, 0, dev->addr_len);
  891.         entry->dev = dev;
  892.         entry->last_used = jiffies;
  893.         init_timer(&entry->timer);
  894.         entry->timer.function = arp_expire_request;
  895.         entry->timer.data = (unsigned long)entry;
  896.         entry->timer.expires = ARP_RES_TIME;
  897.         entry->next = arp_tables[hash];
  898.         arp_tables[hash] = entry;
  899.         add_timer(&entry->timer);
  900.         entry->retries = ARP_MAX_TRIES;
  901.         skb_queue_head_init(&entry->skb);
  902.         if (skb != NULL)
  903.         {
  904.             skb_queue_tail(&entry->skb, skb);
  905.             skb_device_unlock(skb);
  906.         }
  907.     }
  908.     else
  909.     {
  910.         if (skb != NULL && skb->free)
  911.             kfree_skb(skb, FREE_WRITE);
  912.       }
  913.     sti();
  914.  
  915.     /*
  916.      *    If we didn't find an entry, we will try to send an ARP packet.
  917.      */
  918.     
  919.     arp_send(ARPOP_REQUEST, ETH_P_ARP, paddr, dev, saddr, NULL, 
  920.          dev->dev_addr);
  921.  
  922.     return 1;
  923. }
  924.  
  925.  
  926. /*
  927.  *    Write the contents of the ARP cache to a PROCfs file.
  928.  */
  929.  
  930. #define HBUFFERLEN 30
  931.  
  932. int arp_get_info(char *buffer, char **start, off_t offset, int length)
  933. {
  934.     int len=0;
  935.     off_t begin=0;
  936.     off_t pos=0;
  937.     int size;
  938.     struct arp_table *entry;
  939.     char hbuffer[HBUFFERLEN];
  940.     int i,j,k;
  941.     const char hexbuf[] =  "0123456789ABCDEF";
  942.  
  943.     size = sprintf(buffer,"IP address       HW type     Flags       HW address            Mask\n");
  944.  
  945.     pos+=size;
  946.     len+=size;
  947.       
  948.     cli();
  949.     for(i=0; i<FULL_ARP_TABLE_SIZE; i++)
  950.     {
  951.         for(entry=arp_tables[i]; entry!=NULL; entry=entry->next)
  952.         {
  953. /*
  954.  *    Convert hardware address to XX:XX:XX:XX ... form.
  955.  */
  956. #ifdef CONFIG_AX25
  957.  
  958.             if(entry->htype==ARPHRD_AX25)
  959.                  strcpy(hbuffer,ax2asc((ax25_address *)entry->ha));
  960.             else {
  961. #endif
  962.  
  963.             for(k=0,j=0;k<HBUFFERLEN-3 && j<entry->hlen;j++)
  964.             {
  965.                 hbuffer[k++]=hexbuf[ (entry->ha[j]>>4)&15 ];
  966.                 hbuffer[k++]=hexbuf[  entry->ha[j]&15     ];
  967.                 hbuffer[k++]=':';
  968.             }
  969.             hbuffer[--k]=0;
  970.     
  971. #ifdef CONFIG_AX25
  972.             }
  973. #endif
  974.             size = sprintf(buffer+len,
  975.                 "%-17s0x%-10x0x%-10x%s",
  976.                 in_ntoa(entry->ip),
  977.                 (unsigned int)entry->htype,
  978.                 entry->flags,
  979.                 hbuffer);
  980.             size += sprintf(buffer+len+size,
  981.                  "     %-17s\n",
  982.                   entry->mask==DEF_ARP_NETMASK?
  983.                    "*":in_ntoa(entry->mask));
  984.     
  985.             len+=size;
  986.             pos=begin+len;
  987.           
  988.             if(pos<offset)
  989.             {
  990.                 len=0;
  991.                 begin=pos;
  992.             }
  993.             if(pos>offset+length)
  994.                 break;
  995.         }
  996.     }
  997.     sti();
  998.   
  999.     *start=buffer+(offset-begin);    /* Start of wanted data */
  1000.     len-=(offset-begin);        /* Start slop */
  1001.     if(len>length)
  1002.         len=length;                /* Ending slop */
  1003.     return len;
  1004. }
  1005.  
  1006.  
  1007. /*
  1008.  *    This will find an entry in the ARP table by looking at the IP address.
  1009.  *      If exact is true then only exact IP matches will be allowed
  1010.  *      for proxy entries, otherwise the netmask will be used
  1011.  */
  1012.  
  1013. static struct arp_table *arp_lookup(unsigned long paddr, int exact)
  1014. {
  1015.     struct arp_table *entry;
  1016.     unsigned long hash = HASH(paddr);
  1017.     
  1018.     for (entry = arp_tables[hash]; entry != NULL; entry = entry->next)
  1019.         if (entry->ip == paddr) break;
  1020.  
  1021.     /* it's possibly a proxy entry (with a netmask) */
  1022.     if (!entry)
  1023.     for (entry=arp_tables[PROXY_HASH]; entry != NULL; entry = entry->next)
  1024.       if (exact? (entry->ip==paddr) : !((entry->ip^paddr)&entry->mask)) 
  1025.         break;      
  1026.  
  1027.     return entry;
  1028. }
  1029.  
  1030.  
  1031. /*
  1032.  *    Set (create) an ARP cache entry.
  1033.  */
  1034.  
  1035. static int arp_req_set(struct arpreq *req)
  1036. {
  1037.     struct arpreq r;
  1038.     struct arp_table *entry;
  1039.     struct sockaddr_in *si;
  1040.     int htype, hlen;
  1041.     unsigned long ip;
  1042.     struct rtable *rt;
  1043.  
  1044.     memcpy_fromfs(&r, req, sizeof(r));
  1045.  
  1046.     /* We only understand about IP addresses... */
  1047.     if (r.arp_pa.sa_family != AF_INET)
  1048.         return -EPFNOSUPPORT;
  1049.  
  1050.     /*
  1051.      * Find out about the hardware type.
  1052.      * We have to be compatible with BSD UNIX, so we have to
  1053.      * assume that a "not set" value (i.e. 0) means Ethernet.
  1054.      */
  1055.     
  1056.     switch (r.arp_ha.sa_family) {
  1057.         case ARPHRD_ETHER:
  1058.             htype = ARPHRD_ETHER;
  1059.             hlen = ETH_ALEN;
  1060.             break;
  1061.  
  1062.         case ARPHRD_ARCNET:
  1063.             htype = ARPHRD_ARCNET;
  1064.             hlen = 1;    /* length of arcnet addresses */
  1065.             break;
  1066.  
  1067. #ifdef CONFIG_AX25
  1068.         case ARPHRD_AX25:
  1069.             htype = ARPHRD_AX25;
  1070.             hlen = 7;
  1071.             break;
  1072. #endif
  1073.         default:
  1074.             return -EPFNOSUPPORT;
  1075.     }
  1076.  
  1077.     si = (struct sockaddr_in *) &r.arp_pa;
  1078.     ip = si->sin_addr.s_addr;
  1079.     if (ip == 0)
  1080.     {
  1081.         printk("ARP: SETARP: requested PA is 0.0.0.0 !\n");
  1082.         return -EINVAL;
  1083.     }
  1084.  
  1085.     /*
  1086.      *    Is it reachable directly ?
  1087.      */
  1088.  
  1089.     rt = ip_rt_route(ip, NULL, NULL);
  1090.     if (rt == NULL)
  1091.         return -ENETUNREACH;
  1092.  
  1093.     /*
  1094.      *    Is there an existing entry for this address?
  1095.      */
  1096.     
  1097.     cli();
  1098.  
  1099.     /*
  1100.      *    Find the entry
  1101.      */
  1102.     entry = arp_lookup(ip, 1);
  1103.     if (entry && (entry->flags & ATF_PUBL) != (r.arp_flags & ATF_PUBL))
  1104.     {
  1105.         sti();
  1106.         arp_destroy(ip,1);
  1107.         cli();
  1108.         entry = NULL;
  1109.     }
  1110.  
  1111.     /*
  1112.      *    Do we need to create a new entry
  1113.      */
  1114.     
  1115.     if (entry == NULL)
  1116.     {
  1117.             unsigned long hash = HASH(ip);
  1118.         if (r.arp_flags & ATF_PUBL)
  1119.           hash = PROXY_HASH;
  1120.  
  1121.         entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
  1122.                     GFP_ATOMIC);
  1123.         if (entry == NULL)
  1124.         {
  1125.             sti();
  1126.             return -ENOMEM;
  1127.         }
  1128.         entry->ip = ip;
  1129.         entry->hlen = hlen;
  1130.         entry->htype = htype;
  1131.         init_timer(&entry->timer);
  1132.         entry->next = arp_tables[hash];
  1133.         arp_tables[hash] = entry;
  1134.         skb_queue_head_init(&entry->skb);
  1135.     }
  1136.     /*
  1137.      *    We now have a pointer to an ARP entry.  Update it!
  1138.      */
  1139.     
  1140.     memcpy(&entry->ha, &r.arp_ha.sa_data, hlen);
  1141.     entry->last_used = jiffies;
  1142.     entry->flags = r.arp_flags | ATF_COM;
  1143.     if ((entry->flags & ATF_PUBL) && (entry->flags & ATF_NETMASK))
  1144.       {
  1145.         si = (struct sockaddr_in *) &r.arp_netmask;
  1146.         entry->mask = si->sin_addr.s_addr;
  1147.       }
  1148.     else
  1149.       entry->mask = DEF_ARP_NETMASK;
  1150.     entry->dev = rt->rt_dev;
  1151.     sti();
  1152.  
  1153.     return 0;
  1154. }
  1155.  
  1156.  
  1157. /*
  1158.  *    Get an ARP cache entry.
  1159.  */
  1160.  
  1161. static int arp_req_get(struct arpreq *req)
  1162. {
  1163.     struct arpreq r;
  1164.     struct arp_table *entry;
  1165.     struct sockaddr_in *si;
  1166.  
  1167.     /*
  1168.      *    We only understand about IP addresses...
  1169.      */
  1170.     
  1171.     memcpy_fromfs(&r, req, sizeof(r));
  1172.  
  1173.     if (r.arp_pa.sa_family != AF_INET)
  1174.         return -EPFNOSUPPORT;
  1175.  
  1176.     /*
  1177.      *    Is there an existing entry for this address?
  1178.      */
  1179.     
  1180.     si = (struct sockaddr_in *) &r.arp_pa;
  1181.     cli();
  1182.     entry = arp_lookup(si->sin_addr.s_addr,0);
  1183.  
  1184.     if (entry == NULL)
  1185.     {
  1186.         sti();
  1187.         return -ENXIO;
  1188.     }
  1189.  
  1190.     /*
  1191.      *    We found it; copy into structure.
  1192.      */
  1193.     
  1194.     memcpy(r.arp_ha.sa_data, &entry->ha, entry->hlen);
  1195.     r.arp_ha.sa_family = entry->htype;
  1196.     r.arp_flags = entry->flags;
  1197.     sti();
  1198.  
  1199.     /*
  1200.      *    Copy the information back
  1201.      */
  1202.     
  1203.     memcpy_tofs(req, &r, sizeof(r));
  1204.     return 0;
  1205. }
  1206.  
  1207.  
  1208. /*
  1209.  *    Handle an ARP layer I/O control request.
  1210.  */
  1211.  
  1212. int arp_ioctl(unsigned int cmd, void *arg)
  1213. {
  1214.     struct arpreq r;
  1215.     struct sockaddr_in *si;
  1216.     int err;
  1217.  
  1218.     switch(cmd)
  1219.     {
  1220.         case SIOCDARP:
  1221.             if (!suser())
  1222.                 return -EPERM;
  1223.             err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq));
  1224.             if(err)
  1225.                 return err;
  1226.             memcpy_fromfs(&r, arg, sizeof(r));
  1227.             if (r.arp_pa.sa_family != AF_INET)
  1228.                 return -EPFNOSUPPORT;
  1229.             si = (struct sockaddr_in *) &r.arp_pa;
  1230.             arp_destroy(si->sin_addr.s_addr, 1);
  1231.             return 0;
  1232.         case SIOCGARP:
  1233.             err = verify_area(VERIFY_WRITE, arg, sizeof(struct arpreq));
  1234.             if(err)
  1235.                 return err;
  1236.             return arp_req_get((struct arpreq *)arg);
  1237.         case SIOCSARP:
  1238.             if (!suser())
  1239.                 return -EPERM;
  1240.             err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq));
  1241.             if(err)
  1242.                 return err;
  1243.             return arp_req_set((struct arpreq *)arg);
  1244.         default:
  1245.             return -EINVAL;
  1246.     }
  1247.     /*NOTREACHED*/
  1248.     return 0;
  1249. }
  1250.  
  1251.  
  1252. /*
  1253.  *    Called once on startup.
  1254.  */
  1255.  
  1256. static struct packet_type arp_packet_type =
  1257. {
  1258.     0,    /* Should be: __constant_htons(ETH_P_ARP) - but this _doesn't_ come out constant! */
  1259.     NULL,        /* All devices */
  1260.     arp_rcv,
  1261.     NULL,
  1262.     NULL
  1263. };
  1264.  
  1265. static struct notifier_block arp_dev_notifier={
  1266.     arp_device_event,
  1267.     NULL,
  1268.     0
  1269. };
  1270.  
  1271. void arp_init (void)
  1272. {
  1273.     /* Register the packet type */
  1274.     arp_packet_type.type=htons(ETH_P_ARP);
  1275.     dev_add_pack(&arp_packet_type);
  1276.     /* Start with the regular checks for expired arp entries. */
  1277.     add_timer(&arp_timer);
  1278.     /* Register for device down reports */
  1279.     register_netdevice_notifier(&arp_dev_notifier);
  1280. }
  1281.  
  1282.