root/net/ipv4/arp.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. arp_check_expire
  2. arp_release_entry
  3. arp_device_event
  4. arp_send
  5. arp_expire_request
  6. arp_send_q
  7. arp_destroy
  8. arp_rcv
  9. arp_query
  10. arp_find
  11. arp_get_info
  12. arp_lookup
  13. arp_find_cache
  14. arp_req_set
  15. arp_req_get
  16. arp_ioctl
  17. arp_init

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

/* [previous][next][first][last][top][bottom][index][help] */