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

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