root/net/ipv4/arp.c

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

DEFINITIONS

This source file includes following definitions.
  1. arp_fast_lock
  2. arp_fast_unlock
  3. arp_unlock
  4. arp_enqueue
  5. arp_dequeue
  6. arp_release_entry
  7. arp_free_entry
  8. arp_count_hhs
  9. arp_invalidate_hhs
  10. arp_update_hhs
  11. arp_check_expire
  12. arp_expire_request
  13. arp_device_event
  14. arp_send
  15. arp_send_q
  16. arp_destroy
  17. arp_rcv
  18. arp_lookup
  19. arp_query
  20. arp_set_predefined
  21. arp_find
  22. arp_get_info
  23. arp_bind_cache
  24. arp_run_bh
  25. arp_req_set
  26. arp_req_get
  27. arp_req_delete
  28. arp_ioctl
  29. 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  *              Russ Nelson     :       Tidied up a few bits.
  49  */
  50 
  51 /* RFC1122 Status:
  52    2.3.2.1 (ARP Cache Validation):
  53      MUST provide mechanism to flush stale cache entries (OK)
  54      SHOULD be able to configure cache timeout (NOT YET)
  55      MUST throttle ARP retransmits (OK)
  56    2.3.2.2 (ARP Packet Queue):
  57      SHOULD save at least one packet from each "conversation" with an
  58        unresolved IP address.  (OK)
  59    950727 -- MS
  60 */
  61       
  62 #include <linux/types.h>
  63 #include <linux/string.h>
  64 #include <linux/kernel.h>
  65 #include <linux/sched.h>
  66 #include <linux/config.h>
  67 #include <linux/socket.h>
  68 #include <linux/sockios.h>
  69 #include <linux/errno.h>
  70 #include <linux/if_arp.h>
  71 #include <linux/in.h>
  72 #include <linux/mm.h>
  73 #include <asm/system.h>
  74 #include <asm/segment.h>
  75 #include <stdarg.h>
  76 #include <linux/inet.h>
  77 #include <linux/netdevice.h>
  78 #include <linux/etherdevice.h>
  79 #include <linux/trdevice.h>
  80 #include <net/ip.h>
  81 #include <net/route.h>
  82 #include <net/protocol.h>
  83 #include <net/tcp.h>
  84 #include <linux/skbuff.h>
  85 #include <net/sock.h>
  86 #include <net/arp.h>
  87 #ifdef CONFIG_AX25
  88 #include <net/ax25.h>
  89 #ifdef CONFIG_NETROM
  90 #include <net/netrom.h>
  91 #endif
  92 #endif
  93 #include <linux/proc_fs.h>
  94 #include <linux/stat.h>
  95 
  96 
  97 /*
  98  *      This structure defines the ARP mapping cache. As long as we make changes
  99  *      in this structure, we keep interrupts off. But normally we can copy the
 100  *      hardware address and the device pointer in a local variable and then 
 101  *      make any "long calls" to send a packet out.
 102  */
 103 
 104 struct arp_table
 105 {
 106         struct arp_table                *next;                  /* Linked entry list            */
 107         unsigned long                   last_used;              /* For expiry                   */
 108         unsigned long                   last_updated;           /* For expiry                   */
 109         unsigned int                    flags;                  /* Control status               */
 110         u32                             ip;                     /* ip address of entry          */
 111         u32                             mask;                   /* netmask - used for generalised proxy arps (tridge)           */
 112         unsigned char                   ha[MAX_ADDR_LEN];       /* Hardware address             */
 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         struct hh_cache                 *hh;
 123 };
 124 
 125 
 126 /*
 127  *      Configurable Parameters (don't touch unless you know what you are doing
 128  */
 129 
 130 /*
 131  *      If an arp request is send, ARP_RES_TIME is the timeout value until the
 132  *      next request is send.
 133  *      RFC1122: OK.  Throttles ARPing, as per 2.3.2.1. (MUST)
 134  *      The recommended minimum timeout is 1 second per destination.
 135  *      This timeout is prolongated to ARP_DEAD_RES_TIME, if
 136  *      destination does not respond.
 137  */
 138 
 139 #define ARP_RES_TIME            (5*HZ)
 140 #define ARP_DEAD_RES_TIME       (60*HZ)
 141 
 142 /*
 143  *      The number of times an arp request is send, until the host is
 144  *      considered temporarily unreachable.
 145  */
 146 
 147 #define ARP_MAX_TRIES           3
 148 
 149 /*
 150  *      After that time, an unused entry is deleted from the arp table.
 151  */
 152 
 153 #define ARP_TIMEOUT             (600*HZ)
 154 
 155 /*
 156  *      How often is the function 'arp_check_retries' called.
 157  *      An unused entry is invalidated in the time between ARP_TIMEOUT and
 158  *      (ARP_TIMEOUT+ARP_CHECK_INTERVAL).
 159  */
 160 
 161 #define ARP_CHECK_INTERVAL      (60*HZ)
 162 
 163 /*
 164  *      The entry is reconfirmed by sending point-to-point ARP
 165  *      request after ARP_CONFIRM_INTERVAL. If destinations does not respond
 166  *      for ARP_CONFIRM_TIMEOUT, normal broadcast resolution scheme is started.
 167  */
 168 
 169 #define ARP_CONFIRM_INTERVAL    (300*HZ)
 170 #define ARP_CONFIRM_TIMEOUT     ARP_RES_TIME
 171 
 172 static unsigned long arp_lock;
 173 static unsigned long arp_bh_mask;
 174 
 175 #define ARP_BH_BACKLOG  1
 176 
 177 static struct arp_table *arp_backlog;
 178 
 179 static void arp_run_bh(void);
 180 static void arp_check_expire (unsigned long);  
 181 
 182 static struct timer_list arp_timer =
 183         { NULL, NULL, ARP_CHECK_INTERVAL, 0L, &arp_check_expire };
 184 
 185 /*
 186  * The default arp netmask is just 255.255.255.255 which means it's
 187  * a single machine entry. Only proxy entries can have other netmasks
 188  */
 189 
 190 #define DEF_ARP_NETMASK (~0)
 191 
 192 /*
 193  *      The size of the hash table. Must be a power of two.
 194  *      Maybe we should remove hashing in the future for arp and concentrate
 195  *      on Patrick Schaaf's Host-Cache-Lookup...
 196  */
 197 
 198 #define ARP_TABLE_SIZE  16
 199 #define FULL_ARP_TABLE_SIZE (ARP_TABLE_SIZE+1)
 200 
 201 struct arp_table *arp_tables[FULL_ARP_TABLE_SIZE] =
 202 {
 203         NULL,
 204 };
 205 
 206 #define arp_proxy_list arp_tables[ARP_TABLE_SIZE]
 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 
 215 /*
 216  * Lock/unlock arp_table chains.
 217  */
 218 
 219 static __inline__ void arp_fast_lock(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 220 {
 221         ATOMIC_INCR(&arp_lock);
 222 }
 223 
 224 static __inline__ void arp_fast_unlock(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 225 {
 226         ATOMIC_DECR(&arp_lock);
 227 }
 228 
 229 static __inline__ void arp_unlock(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 230 {
 231         if (!ATOMIC_DECR_AND_CHECK(&arp_lock) && arp_bh_mask)
 232                 arp_run_bh();
 233 }
 234 
 235 /*
 236  * Enqueue to FIFO list.
 237  */
 238 
 239 static void arp_enqueue(struct arp_table **q, struct arp_table *entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241         unsigned long flags;
 242         struct arp_table * tail;
 243 
 244         save_flags(flags);
 245         cli();
 246         tail = *q;
 247         if (!tail)
 248                 entry->next = entry;
 249         else
 250         {
 251                 entry->next = tail->next;
 252                 tail->next = entry;
 253         }
 254         *q = entry;
 255         restore_flags(flags);
 256         return;
 257 }
 258 
 259 /*
 260  * Dequeue from FIFO list,
 261  * caller should mask interrupts.
 262  */
 263 
 264 static struct arp_table * arp_dequeue(struct arp_table **q)
     /* [previous][next][first][last][top][bottom][index][help] */
 265 {
 266         struct arp_table * entry;
 267 
 268         if (*q)
 269         {
 270                 entry = (*q)->next;
 271                 (*q)->next = entry->next;
 272                 if (entry->next == entry)
 273                         *q = NULL;
 274                 entry->next = NULL;
 275                 return entry;
 276         }
 277         return NULL;
 278 }
 279 
 280 /*
 281  * Purge all linked skb's of the entry.
 282  */
 283 
 284 static void arp_release_entry(struct arp_table *entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 285 {
 286         struct sk_buff *skb;
 287         unsigned long flags;
 288 
 289         save_flags(flags);
 290         cli();
 291         /* Release the list of `skb' pointers. */
 292         while ((skb = skb_dequeue(&entry->skb)) != NULL)
 293         {
 294                 skb_device_lock(skb);
 295                 restore_flags(flags);
 296                 dev_kfree_skb(skb, FREE_WRITE);
 297                 cli();
 298         }
 299         restore_flags(flags);
 300         return;
 301 }
 302 
 303 /*
 304  *      Release the entry and all resources linked to it: skb's, hh's, timer
 305  *      and certainly memory.
 306  */
 307 
 308 static void arp_free_entry(struct arp_table *entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 309 {
 310         unsigned long flags;
 311         struct hh_cache *hh, *next;
 312 
 313         del_timer(&entry->timer);
 314 
 315         save_flags(flags);
 316         cli();
 317         arp_release_entry(entry);
 318 
 319         for (hh = entry->hh; hh; hh = next)
 320         {
 321                 next = hh->hh_next;
 322                 hh->hh_arp = NULL;
 323                 if (!--hh->hh_refcnt)
 324                         kfree_s(hh, sizeof(struct(struct hh_cache)));
 325         }
 326         restore_flags(flags);
 327 
 328         kfree_s(entry, sizeof(struct arp_table));
 329         return;
 330 }
 331 
 332 /*
 333  * How many users has this entry?
 334  */
 335 
 336 static __inline__ int arp_count_hhs(struct arp_table * entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 337 {
 338         struct hh_cache *hh, **hhp;
 339         int count = 0;
 340 
 341         hhp = &entry->hh;
 342         while ((hh=*hhp) != NULL)
 343         {
 344                 if (hh->hh_refcnt == 1)
 345                 {
 346                         *hhp = hh->hh_next;
 347                         kfree_s(hh, sizeof(struct hh_cache));
 348                         continue;
 349                 }
 350                 count += hh->hh_refcnt-1;
 351                 hhp = &hh->hh_next;
 352         }
 353 
 354         return count;
 355 }
 356 
 357 /*
 358  * Invalidate all hh's, so that higher level will not try to use it.
 359  */
 360 
 361 static __inline__ void arp_invalidate_hhs(struct arp_table * entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 362 {
 363         struct hh_cache *hh;
 364 
 365         for (hh=entry->hh; hh; hh=hh->hh_next)
 366                 hh->hh_uptodate = 0;
 367 }
 368 
 369 /*
 370  * Signal to device layer, that hardware address may be changed.
 371  */
 372 
 373 static __inline__ void arp_update_hhs(struct arp_table * entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 374 {
 375         struct hh_cache *hh;
 376 
 377         for (hh=entry->hh; hh; hh=hh->hh_next)
 378                 entry->dev->header_cache_update(hh, entry->dev, entry->ha);
 379 }
 380 
 381 /*
 382  *      Check if there are too old entries and remove them. If the ATF_PERM
 383  *      flag is set, they are always left in the arp cache (permanent entry).
 384  *      If an entry was not be confirmed  for ARP_CONFIRM_INTERVAL,
 385  *      declare it invalid and send point-to-point ARP request.
 386  *      If it will not be confirmed for ARP_CONFIRM_TIMEOUT,
 387  *      give it to shred by arp_expire_entry.
 388  */
 389 
 390 static void arp_check_expire(unsigned long dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 391 {
 392         int i;
 393         unsigned long now = jiffies;
 394 
 395         del_timer(&arp_timer);
 396 
 397         if (!arp_lock)
 398         {
 399                 arp_fast_lock();
 400 
 401                 for (i = 0; i < ARP_TABLE_SIZE; i++)
 402                 {
 403                         struct arp_table *entry;
 404                         struct arp_table **pentry;
 405                 
 406                         pentry = &arp_tables[i];
 407 
 408                         while ((entry = *pentry) != NULL)
 409                         {
 410                                 cli();
 411                                 if (now - entry->last_used > ARP_TIMEOUT
 412                                     && !(entry->flags & ATF_PERM)
 413                                     && !arp_count_hhs(entry))
 414                                 {
 415                                         *pentry = entry->next;
 416                                         sti();
 417 #if RT_CACHE_DEBUG >= 2
 418                                         printk("arp_expire: %08x expired\n", entry->ip);
 419 #endif
 420                                         arp_free_entry(entry);
 421                                 }
 422                                 else if (entry->last_updated
 423                                          && now - entry->last_updated > ARP_CONFIRM_INTERVAL
 424                                          && !(entry->flags & ATF_PERM))
 425                                 {
 426                                         struct device * dev = entry->dev;
 427                                         pentry = &entry->next;
 428                                         entry->flags &= ~ATF_COM;
 429                                         arp_invalidate_hhs(entry);
 430                                         sti();
 431                                         entry->retries = ARP_MAX_TRIES+1;
 432                                         del_timer(&entry->timer);
 433                                         entry->timer.expires = jiffies + ARP_CONFIRM_TIMEOUT;
 434                                         add_timer(&entry->timer);
 435                                         arp_send(ARPOP_REQUEST, ETH_P_ARP, entry->ip,
 436                                                  dev, dev->pa_addr, entry->ha,
 437                                                  dev->dev_addr, NULL);
 438 #if RT_CACHE_DEBUG >= 2
 439                                         printk("arp_expire: %08x requires confirmation\n", entry->ip);
 440 #endif
 441                                 }
 442                                 else
 443                                         pentry = &entry->next;  /* go to next entry */
 444                         }
 445                 }
 446                 arp_unlock();
 447         }
 448 
 449         ip_rt_check_expire();
 450 
 451         /*
 452          *      Set the timer again.
 453          */
 454 
 455         arp_timer.expires = jiffies + ARP_CHECK_INTERVAL;
 456         add_timer(&arp_timer);
 457 }
 458 
 459 /*
 460  *      This function is called, if an entry is not resolved in ARP_RES_TIME.
 461  *      When more than MAX_ARP_TRIES retries was done, release queued skb's,
 462  *      but not discard entry itself if  it is in use.
 463  */
 464 
 465 static void arp_expire_request (unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 466 {
 467         struct arp_table *entry = (struct arp_table *) arg;
 468         struct arp_table **pentry;
 469         unsigned long hash;
 470         unsigned long flags;
 471 
 472         save_flags(flags);
 473         cli();
 474 
 475         /*
 476          *      Since all timeouts are handled with interrupts enabled, there is a
 477          *      small chance, that this entry has just been resolved by an incoming
 478          *      packet. This is the only race condition, but it is handled...
 479          */
 480         
 481         if (entry->flags & ATF_COM)
 482         {
 483                 restore_flags(flags);
 484                 return;
 485         }
 486 
 487         if (arp_lock)
 488         {
 489 #if RT_CACHE_DEBUG >= 1
 490                 printk("arp_expire_request: %08x postponed\n", entry->ip);
 491 #endif
 492                 del_timer(&entry->timer);
 493                 entry->timer.expires = jiffies + HZ/10;
 494                 add_timer(&entry->timer);
 495                 restore_flags(flags);
 496                 return;
 497         }
 498 
 499         arp_fast_lock();
 500         restore_flags(flags);
 501 
 502         if (entry->last_updated && --entry->retries > 0)
 503         {
 504                 struct device *dev = entry->dev;
 505 
 506 #if RT_CACHE_DEBUG >= 2
 507                 printk("arp_expire_request: %08x timed out\n", entry->ip);
 508 #endif
 509                 /* Set new timer. */
 510                 del_timer(&entry->timer);
 511                 entry->timer.expires = jiffies + ARP_RES_TIME;
 512                 add_timer(&entry->timer);
 513                 arp_send(ARPOP_REQUEST, ETH_P_ARP, entry->ip, dev, dev->pa_addr, 
 514                          NULL, dev->dev_addr, NULL);
 515                 arp_unlock();
 516                 return;
 517         }
 518 
 519         arp_release_entry(entry);
 520 
 521         cli();
 522         if (arp_count_hhs(entry))
 523         {
 524                 struct device *dev = entry->dev;
 525 #if RT_CACHE_DEBUG >= 2
 526                 printk("arp_expire_request: %08x is dead\n", entry->ip);
 527 #endif
 528                 arp_release_entry(entry);
 529                 entry->retries = ARP_MAX_TRIES;
 530                 restore_flags(flags);
 531                 entry->last_updated = 0;
 532                 del_timer(&entry->timer);
 533                 entry->timer.expires = jiffies + ARP_DEAD_RES_TIME;
 534                 add_timer(&entry->timer);
 535                 arp_send(ARPOP_REQUEST, ETH_P_ARP, entry->ip, dev, dev->pa_addr, 
 536                          NULL, dev->dev_addr, NULL);
 537                 arp_unlock();
 538                 return;
 539         }
 540         restore_flags(flags);
 541 
 542         hash = HASH(entry->ip);
 543 
 544         pentry = &arp_tables[hash];
 545 
 546         while (*pentry != NULL)
 547         {
 548                 if (*pentry == entry)
 549                 {
 550                         cli();
 551                         *pentry = entry->next;
 552                         restore_flags(flags);
 553 #if RT_CACHE_DEBUG >= 2
 554                         printk("arp_expire_request: %08x is killed\n", entry->ip);
 555 #endif
 556                         arp_free_entry(entry);
 557                         arp_unlock();
 558                         return;
 559                 }
 560                 pentry = &(*pentry)->next;
 561         }
 562         printk("arp_expire_request: bug: ARP entry is lost!\n");
 563         arp_unlock();
 564 }
 565 
 566 /*
 567  *      Purge a device from the ARP queue
 568  */
 569  
 570 int arp_device_event(struct notifier_block *this, unsigned long event, void *ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 571 {
 572         struct device *dev=ptr;
 573         int i;
 574         
 575         if (event != NETDEV_DOWN)
 576                 return NOTIFY_DONE;
 577         /*
 578          *      This is a bit OTT - maybe we need some arp semaphores instead.
 579          */
 580 
 581 #if RT_CACHE_DEBUG >= 1  
 582         if (arp_lock)
 583                 printk("arp_device_event: bug\n");
 584 #endif
 585         arp_fast_lock();
 586 
 587         for (i = 0; i < FULL_ARP_TABLE_SIZE; i++)
 588         {
 589                 struct arp_table *entry;
 590                 struct arp_table **pentry = &arp_tables[i];
 591 
 592                 while ((entry = *pentry) != NULL)
 593                 {
 594                         if (entry->dev == dev)
 595                         {
 596                                 *pentry = entry->next;  /* remove from list */
 597                                 arp_free_entry(entry);
 598                         }
 599                         else
 600                                 pentry = &entry->next;  /* go to next entry */
 601                 }
 602         }
 603         return NOTIFY_DONE;
 604 }
 605 
 606 
 607 /*
 608  *      Create and send an arp packet. If (dest_hw == NULL), we create a broadcast
 609  *      message.
 610  */
 611 
 612 void arp_send(int type, int ptype, u32 dest_ip, 
     /* [previous][next][first][last][top][bottom][index][help] */
 613               struct device *dev, u32 src_ip, 
 614               unsigned char *dest_hw, unsigned char *src_hw,
 615               unsigned char *target_hw)
 616 {
 617         struct sk_buff *skb;
 618         struct arphdr *arp;
 619         unsigned char *arp_ptr;
 620 
 621         /*
 622          *      No arp on this interface.
 623          */
 624         
 625         if (dev->flags&IFF_NOARP)
 626                 return;
 627 
 628         /*
 629          *      Allocate a buffer
 630          */
 631         
 632         skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4)
 633                                 + dev->hard_header_len, GFP_ATOMIC);
 634         if (skb == NULL)
 635         {
 636                 printk("ARP: no memory to send an arp packet\n");
 637                 return;
 638         }
 639         skb_reserve(skb, dev->hard_header_len);
 640         arp = (struct arphdr *) skb_put(skb,sizeof(struct arphdr) + 2*(dev->addr_len+4));
 641         skb->arp = 1;
 642         skb->dev = dev;
 643         skb->free = 1;
 644 
 645         /*
 646          *      Fill the device header for the ARP frame
 647          */
 648 
 649         dev->hard_header(skb,dev,ptype,dest_hw?dest_hw:dev->broadcast,src_hw?src_hw:NULL,skb->len);
 650 
 651         /* Fill out the arp protocol part. */
 652         arp->ar_hrd = htons(dev->type);
 653 #ifdef CONFIG_AX25
 654 #ifdef CONFIG_NETROM
 655         arp->ar_pro = (dev->type == ARPHRD_AX25 || dev->type == ARPHRD_NETROM) ? htons(AX25_P_IP) : htons(ETH_P_IP);
 656 #else
 657         arp->ar_pro = (dev->type != ARPHRD_AX25) ? htons(ETH_P_IP) : htons(AX25_P_IP);
 658 #endif
 659 #else
 660         arp->ar_pro = htons(ETH_P_IP);
 661 #endif
 662         arp->ar_hln = dev->addr_len;
 663         arp->ar_pln = 4;
 664         arp->ar_op = htons(type);
 665 
 666         arp_ptr=(unsigned char *)(arp+1);
 667 
 668         memcpy(arp_ptr, src_hw, dev->addr_len);
 669         arp_ptr+=dev->addr_len;
 670         memcpy(arp_ptr, &src_ip,4);
 671         arp_ptr+=4;
 672         if (target_hw != NULL)
 673                 memcpy(arp_ptr, target_hw, dev->addr_len);
 674         else
 675                 memset(arp_ptr, 0, dev->addr_len);
 676         arp_ptr+=dev->addr_len;
 677         memcpy(arp_ptr, &dest_ip, 4);
 678 
 679         dev_queue_xmit(skb, dev, 0);
 680 }
 681 
 682 /*
 683  *      This will try to retransmit everything on the queue.
 684  */
 685 
 686 static void arp_send_q(struct arp_table *entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 687 {
 688         struct sk_buff *skb;
 689 
 690         unsigned long flags;
 691 
 692         /*
 693          *      Empty the entire queue, building its data up ready to send
 694          */
 695         
 696         if(!(entry->flags&ATF_COM))
 697         {
 698                 printk("arp_send_q: incomplete entry for %s\n",
 699                                 in_ntoa(entry->ip));
 700                 /* Can't flush the skb, because RFC1122 says to hang on to */
 701                 /* at least one from any unresolved entry.  --MS */
 702                 /* Whats happened is that someone has 'unresolved' the entry
 703                    as we got to use it - this 'can't happen' -- AC */
 704                 return;
 705         }
 706 
 707         save_flags(flags);
 708         
 709         cli();
 710         while((skb = skb_dequeue(&entry->skb)) != NULL)
 711         {
 712                 IS_SKB(skb);
 713                 skb_device_lock(skb);
 714                 restore_flags(flags);
 715                 if(!skb->dev->rebuild_header(skb->data,skb->dev,skb->raddr,skb))
 716                 {
 717                         skb->arp  = 1;
 718                         if(skb->sk==NULL)
 719                                 dev_queue_xmit(skb, skb->dev, 0);
 720                         else
 721                                 dev_queue_xmit(skb,skb->dev,skb->sk->priority);
 722                 }
 723         }
 724         restore_flags(flags);
 725 }
 726 
 727 
 728 /*
 729  *      Delete an ARP mapping entry in the cache.
 730  */
 731 
 732 static void arp_destroy(struct arp_table * entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 733 {
 734         struct arp_table *entry1;
 735         struct arp_table **pentry;
 736 
 737         if (entry->flags & ATF_PUBL)
 738                 pentry = &arp_proxy_list;
 739         else
 740                 pentry = &arp_tables[HASH(entry->ip)];
 741 
 742         while ((entry1 = *pentry) != NULL)
 743         {
 744                 if (entry1 == entry)
 745                 {
 746                         *pentry = entry1->next;
 747                         del_timer(&entry->timer);
 748                         arp_free_entry(entry);
 749                         return;
 750                 }
 751                 pentry = &entry1->next;
 752         }
 753 }
 754 
 755 /*
 756  *      Receive an arp request by the device layer. Maybe I rewrite it, to
 757  *      use the incoming packet for the reply. The time for the current
 758  *      "overhead" isn't that high...
 759  */
 760 
 761 int arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
 762 {
 763 /*
 764  *      We shouldn't use this type conversion. Check later.
 765  */
 766         
 767         struct arphdr *arp = (struct arphdr *)skb->h.raw;
 768         unsigned char *arp_ptr= (unsigned char *)(arp+1);
 769         struct arp_table *entry;
 770         struct arp_table *proxy_entry;
 771         unsigned long hash;
 772         unsigned char ha[MAX_ADDR_LEN]; /* So we can enable ints again. */
 773         unsigned char *sha,*tha;
 774         u32 sip,tip;
 775         
 776 /*
 777  *      The hardware length of the packet should match the hardware length
 778  *      of the device.  Similarly, the hardware types should match.  The
 779  *      device should be ARP-able.  Also, if pln is not 4, then the lookup
 780  *      is not from an IP number.  We can't currently handle this, so toss
 781  *      it. 
 782  */  
 783         if (arp->ar_hln != dev->addr_len    || 
 784                 dev->type != ntohs(arp->ar_hrd) || 
 785                 dev->flags & IFF_NOARP          ||
 786                 arp->ar_pln != 4)
 787         {
 788                 kfree_skb(skb, FREE_READ);
 789                 return 0;
 790                 /* Should this be an error/printk?  Seems like something */
 791                 /* you'd want to know about. Unless it's just !IFF_NOARP. -- MS */
 792         }
 793 
 794 /*
 795  *      Another test.
 796  *      The logic here is that the protocol being looked up by arp should 
 797  *      match the protocol the device speaks.  If it doesn't, there is a
 798  *      problem, so toss the packet.
 799  */
 800 /* Again, should this be an error/printk? -- MS */
 801 
 802         switch (dev->type)
 803         {
 804 #ifdef CONFIG_AX25
 805                 case ARPHRD_AX25:
 806                         if(arp->ar_pro != htons(AX25_P_IP))
 807                         {
 808                                 kfree_skb(skb, FREE_READ);
 809                                 return 0;
 810                         }
 811                         break;
 812 #endif
 813 #ifdef CONFIG_NETROM
 814                 case ARPHRD_NETROM:
 815                         if(arp->ar_pro != htons(AX25_P_IP))
 816                         {
 817                                 kfree_skb(skb, FREE_READ);
 818                                 return 0;
 819                         }
 820                         break;
 821 #endif
 822                 case ARPHRD_ETHER:
 823                 case ARPHRD_ARCNET:
 824                         if(arp->ar_pro != htons(ETH_P_IP))
 825                         {
 826                                 kfree_skb(skb, FREE_READ);
 827                                 return 0;
 828                         }
 829                         break;
 830 
 831                 case ARPHRD_IEEE802:
 832                         if(arp->ar_pro != htons(ETH_P_IP))
 833                         {
 834                                 kfree_skb(skb, FREE_READ);
 835                                 return 0;
 836                         }
 837                         break;
 838 
 839                 default:
 840                         printk("ARP: dev->type mangled!\n");
 841                         kfree_skb(skb, FREE_READ);
 842                         return 0;
 843         }
 844 
 845 /*
 846  *      Extract fields
 847  */
 848 
 849         sha=arp_ptr;
 850         arp_ptr += dev->addr_len;
 851         memcpy(&sip, arp_ptr, 4);
 852         arp_ptr += 4;
 853         tha=arp_ptr;
 854         arp_ptr += dev->addr_len;
 855         memcpy(&tip, arp_ptr, 4);
 856   
 857 /* 
 858  *      Check for bad requests for 127.x.x.x and requests for multicast
 859  *      addresses.  If this is one such, delete it.
 860  */
 861         if (LOOPBACK(tip) || MULTICAST(tip))
 862         {
 863                 kfree_skb(skb, FREE_READ);
 864                 return 0;
 865         }
 866 
 867 /*
 868  *  Process entry.  The idea here is we want to send a reply if it is a
 869  *  request for us or if it is a request for someone else that we hold
 870  *  a proxy for.  We want to add an entry to our cache if it is a reply
 871  *  to us or if it is a request for our address.  
 872  *  (The assumption for this last is that if someone is requesting our 
 873  *  address, they are probably intending to talk to us, so it saves time 
 874  *  if we cache their address.  Their address is also probably not in 
 875  *  our cache, since ours is not in their cache.)
 876  * 
 877  *  Putting this another way, we only care about replies if they are to
 878  *  us, in which case we add them to the cache.  For requests, we care
 879  *  about those for us and those for our proxies.  We reply to both,
 880  *  and in the case of requests for us we add the requester to the arp 
 881  *  cache.
 882  */
 883 
 884         if (arp->ar_op == htons(ARPOP_REQUEST))
 885         { 
 886 /*
 887  * Only reply for the real device address or when it's in our proxy tables
 888  */
 889                 if (tip != dev->pa_addr)
 890                 {
 891 /*
 892  *      To get in here, it is a request for someone else.  We need to
 893  *      check if that someone else is one of our proxies.  If it isn't,
 894  *      we can toss it.
 895  */
 896                         arp_fast_lock();
 897 
 898                         for (proxy_entry=arp_proxy_list;
 899                              proxy_entry;
 900                              proxy_entry = proxy_entry->next)
 901                         {
 902                                 /* we will respond to a proxy arp request
 903                                    if the masked arp table ip matches the masked
 904                                    tip. This allows a single proxy arp table
 905                                    entry to be used on a gateway machine to handle
 906                                    all requests for a whole network, rather than
 907                                    having to use a huge number of proxy arp entries
 908                                    and having to keep them uptodate.
 909                                    */
 910                                 if (proxy_entry->dev == dev &&
 911                                     !((proxy_entry->ip^tip)&proxy_entry->mask))
 912                                         break;
 913 
 914                         }
 915                         if (proxy_entry)
 916                         {
 917                                 memcpy(ha, proxy_entry->ha, dev->addr_len);
 918                                 arp_unlock();
 919                                 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,ha, sha);
 920                                 kfree_skb(skb, FREE_READ);
 921                                 return 0;
 922                         }
 923                         else
 924                         {
 925                                 arp_unlock();
 926                                 kfree_skb(skb, FREE_READ);
 927                                 return 0;
 928                         }
 929                 }
 930                 else
 931                 {
 932 /*
 933  *      To get here, it must be an arp request for us.  We need to reply.
 934  */
 935                         arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);
 936                 }
 937         }
 938 /*
 939  *      It is now an arp reply.
 940  */
 941         if(ip_chk_addr(tip)!=IS_MYADDR)
 942         {
 943 /*
 944  *      Replies to other machines get tossed.
 945  */
 946                 kfree_skb(skb, FREE_READ);
 947                 return 0;
 948         }
 949 /*
 950  * Now all replies are handled.  Next, anything that falls through to here
 951  * needs to be added to the arp cache, or have its entry updated if it is 
 952  * there.
 953  */
 954 
 955         arp_fast_lock();
 956 
 957         hash = HASH(sip);
 958 
 959         for (entry=arp_tables[hash]; entry; entry=entry->next)
 960                 if (entry->ip == sip && entry->dev == dev)
 961                         break;
 962 
 963         if (entry)
 964         {
 965 /*
 966  *      Entry found; update it only if it is not a permanent entry.
 967  */
 968                 if (!(entry->flags & ATF_PERM)) {
 969                         memcpy(entry->ha, sha, dev->addr_len);
 970                         entry->last_updated = jiffies;
 971                 }
 972                 if (!(entry->flags & ATF_COM))
 973                 {
 974 /*
 975  *      This entry was incomplete.  Delete the retransmit timer
 976  *      and switch to complete status.
 977  */
 978                         del_timer(&entry->timer);
 979                         entry->flags |= ATF_COM;
 980                         arp_update_hhs(entry);
 981 /* 
 982  *      Send out waiting packets. We might have problems, if someone is 
 983  *      manually removing entries right now -- entry might become invalid 
 984  *      underneath us.
 985  */
 986                         arp_send_q(entry);
 987                 }
 988         }
 989         else
 990         {
 991 /*
 992  *      No entry found.  Need to add a new entry to the arp table.
 993  */
 994                 entry = (struct arp_table *)kmalloc(sizeof(struct arp_table),GFP_ATOMIC);
 995                 if(entry == NULL)
 996                 {
 997                         arp_unlock();
 998                         printk("ARP: no memory for new arp entry\n");
 999                         kfree_skb(skb, FREE_READ);
1000                         return 0;
1001                 }
1002 
1003                 entry->mask = DEF_ARP_NETMASK;
1004                 entry->ip = sip;
1005                 entry->flags = ATF_COM;
1006                 entry->hh    = NULL;
1007                 init_timer(&entry->timer);
1008                 entry->timer.function = arp_expire_request;
1009                 entry->timer.data = (unsigned long)entry;
1010                 memcpy(entry->ha, sha, dev->addr_len);
1011                 entry->last_updated = entry->last_used = jiffies;
1012                 entry->dev = skb->dev;
1013                 skb_queue_head_init(&entry->skb);
1014                 if (arp_lock == 1)
1015                 {
1016                         entry->next = arp_tables[hash];
1017                         arp_tables[hash] = entry;
1018                 }
1019                 else
1020                 {
1021 #if RT_CACHE_DEBUG >= 1
1022                         printk("arp_rcv: %08x backlogged\n", entry->ip);
1023 #endif
1024                         arp_enqueue(&arp_backlog, entry);
1025                         arp_bh_mask |= ARP_BH_BACKLOG;
1026                 }
1027         }
1028 
1029 /*
1030  *      Replies have been sent, and entries have been added.  All done.
1031  */
1032         kfree_skb(skb, FREE_READ);
1033         arp_unlock();
1034         return 0;
1035 }
1036 
1037 /*
1038  * Lookup ARP entry by (addr, dev) pair.
1039  * Flags: ATF_PUBL - search for proxy entries
1040  *        ATF_NETMASK - search for proxy network entry.
1041  * NOTE:  should be called with locked ARP tables.
1042  */
1043 
1044 static struct arp_table *arp_lookup(u32 paddr, unsigned short flags, struct device * dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1045 {
1046         struct arp_table *entry;
1047 
1048         if (!(flags & ATF_PUBL))
1049         {
1050                 for (entry = arp_tables[HASH(paddr)];
1051                      entry != NULL; entry = entry->next)
1052                         if (entry->ip == paddr && entry->dev == dev)
1053                                 break;
1054                 return entry;
1055         }
1056 
1057         if (!(flags & ATF_NETMASK))
1058         {
1059                 for (entry = arp_proxy_list;
1060                      entry != NULL; entry = entry->next)
1061                         if (entry->ip == paddr && entry->dev == dev)
1062                                 break;
1063                 return entry;
1064         }
1065 
1066         for (entry=arp_proxy_list; entry != NULL; entry = entry->next)
1067                 if (!((entry->ip^paddr)&entry->mask) && entry->dev == dev)
1068                         break;
1069         return entry;
1070 }
1071 
1072 /*
1073  *      Find an arp mapping in the cache. If not found, return false.
1074  */
1075 
1076 int arp_query(unsigned char *haddr, u32 paddr, struct device * dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1077 {
1078         struct arp_table *entry;
1079 
1080         arp_fast_lock();
1081 
1082         entry = arp_lookup(paddr, 0, dev);
1083 
1084         if (entry != NULL)
1085         {
1086                 entry->last_used = jiffies;
1087                 if (entry->flags & ATF_COM)
1088                 {
1089                         memcpy(haddr, entry->ha, dev->addr_len);
1090                         arp_unlock();
1091                         return 1;
1092                 }
1093         }
1094         arp_unlock();
1095         return 0;
1096 }
1097 
1098 
1099 static int arp_set_predefined(int addr_hint, unsigned char * haddr, __u32 paddr, struct device * dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1100 {
1101         switch (addr_hint)
1102         {
1103                 case IS_MYADDR:
1104                         printk("ARP: arp called for own IP address\n");
1105                         memcpy(haddr, dev->dev_addr, dev->addr_len);
1106                         return 1;
1107 #ifdef CONFIG_IP_MULTICAST
1108                 case IS_MULTICAST:
1109                         if(dev->type==ARPHRD_ETHER || dev->type==ARPHRD_IEEE802)
1110                         {
1111                                 u32 taddr;
1112                                 haddr[0]=0x01;
1113                                 haddr[1]=0x00;
1114                                 haddr[2]=0x5e;
1115                                 taddr=ntohl(paddr);
1116                                 haddr[5]=taddr&0xff;
1117                                 taddr=taddr>>8;
1118                                 haddr[4]=taddr&0xff;
1119                                 taddr=taddr>>8;
1120                                 haddr[3]=taddr&0x7f;
1121                                 return 1;
1122                         }
1123                 /*
1124                  *      If a device does not support multicast broadcast the stuff (eg AX.25 for now)
1125                  */
1126 #endif
1127                 
1128                 case IS_BROADCAST:
1129                         memcpy(haddr, dev->broadcast, dev->addr_len);
1130                         return 1;
1131         }
1132         return 0;
1133 }
1134 
1135 /*
1136  *      Find an arp mapping in the cache. If not found, post a request.
1137  */
1138 
1139 int arp_find(unsigned char *haddr, u32 paddr, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
1140              u32 saddr, struct sk_buff *skb)
1141 {
1142         struct arp_table *entry;
1143         unsigned long hash;
1144 
1145         if (arp_set_predefined(ip_chk_addr(paddr), haddr, paddr, dev))
1146         {
1147                 if (skb)
1148                         skb->arp = 1;
1149                 return 0;
1150         }
1151 
1152         hash = HASH(paddr);
1153         arp_fast_lock();
1154 
1155         /*
1156          *      Find an entry
1157          */
1158         entry = arp_lookup(paddr, 0, dev);
1159 
1160         if (entry != NULL)      /* It exists */
1161         {
1162                 if (!(entry->flags & ATF_COM))
1163                 {
1164                         /*
1165                          *      A request was already send, but no reply yet. Thus
1166                          *      queue the packet with the previous attempt
1167                          */
1168                         
1169                         if (skb != NULL)
1170                         {
1171                                 if (entry->last_updated)
1172                                 {
1173                                         skb_queue_tail(&entry->skb, skb);
1174                                         skb_device_unlock(skb);
1175                                 }
1176                                 /*
1177                                  * If last_updated==0 host is dead, so
1178                                  * drop skb's and set socket error.
1179                                  */
1180                                 else
1181                                 {
1182                                         /*
1183                                          * FIXME: ICMP HOST UNREACHABLE should be
1184                                          *        sent in this situation. --ANK
1185                                          */
1186                                         if (skb->sk)
1187                                         {
1188                                                 skb->sk->err = EHOSTDOWN;
1189                                                 skb->sk->error_report(skb->sk);
1190                                         }
1191                                         dev_kfree_skb(skb, FREE_WRITE);
1192                                 }
1193                         }
1194                         arp_unlock();
1195                         return 1;
1196                 }
1197 
1198                 /*
1199                  *      Update the record
1200                  */
1201                 
1202                 entry->last_used = jiffies;
1203                 memcpy(haddr, entry->ha, dev->addr_len);
1204                 if (skb)
1205                         skb->arp = 1;
1206                 arp_unlock();
1207                 return 0;
1208         }
1209 
1210         /*
1211          *      Create a new unresolved entry.
1212          */
1213         
1214         entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
1215                                         GFP_ATOMIC);
1216         if (entry != NULL)
1217         {
1218                 entry->last_updated = entry->last_used = jiffies;
1219                 entry->flags = 0;
1220                 entry->ip = paddr;
1221                 entry->mask = DEF_ARP_NETMASK;
1222                 memset(entry->ha, 0, dev->addr_len);
1223                 entry->dev = dev;
1224                 entry->hh    = NULL;
1225                 init_timer(&entry->timer);
1226                 entry->timer.function = arp_expire_request;
1227                 entry->timer.data = (unsigned long)entry;
1228                 entry->timer.expires = jiffies + ARP_RES_TIME;
1229                 skb_queue_head_init(&entry->skb);
1230                 if (skb != NULL)
1231                 {
1232                         skb_queue_tail(&entry->skb, skb);
1233                         skb_device_unlock(skb);
1234                 }
1235                 if (arp_lock == 1)
1236                 {
1237                         entry->next = arp_tables[hash];
1238                         arp_tables[hash] = entry;
1239                         add_timer(&entry->timer);
1240                         entry->retries = ARP_MAX_TRIES;
1241                 }
1242                 else
1243                 {
1244 #if RT_CACHE_DEBUG >= 1
1245                         printk("arp_find: %08x backlogged\n", entry->ip);
1246 #endif
1247                         arp_enqueue(&arp_backlog, entry);
1248                         arp_bh_mask |= ARP_BH_BACKLOG;
1249                 }
1250         }
1251         else if (skb != NULL)
1252                 dev_kfree_skb(skb, FREE_WRITE);
1253         arp_unlock();
1254 
1255         /*
1256          *      If we didn't find an entry, we will try to send an ARP packet.
1257          */
1258         
1259         arp_send(ARPOP_REQUEST, ETH_P_ARP, paddr, dev, saddr, NULL, 
1260                  dev->dev_addr, NULL);
1261 
1262         return 1;
1263 }
1264 
1265 
1266 /*
1267  *      Write the contents of the ARP cache to a PROCfs file.
1268  */
1269 
1270 #define HBUFFERLEN 30
1271 
1272 int arp_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
1273 {
1274         int len=0;
1275         off_t pos=0;
1276         int size;
1277         struct arp_table *entry;
1278         char hbuffer[HBUFFERLEN];
1279         int i,j,k;
1280         const char hexbuf[] =  "0123456789ABCDEF";
1281 
1282         size = sprintf(buffer,"IP address       HW type     Flags       HW address            Mask     Device\n");
1283 
1284         pos+=size;
1285         len+=size;
1286 
1287         arp_fast_lock();
1288 
1289         for(i=0; i<FULL_ARP_TABLE_SIZE; i++)
1290         {
1291                 for(entry=arp_tables[i]; entry!=NULL; entry=entry->next)
1292                 {
1293 /*
1294  *      Convert hardware address to XX:XX:XX:XX ... form.
1295  */
1296 #ifdef CONFIG_AX25
1297 #ifdef CONFIG_NETROM
1298                         if (entry->dev->type == ARPHRD_AX25 || entry->dev->type == ARPHRD_NETROM)
1299                              strcpy(hbuffer,ax2asc((ax25_address *)entry->ha));
1300                         else {
1301 #else
1302                         if(entry->dev->type==ARPHRD_AX25)
1303                              strcpy(hbuffer,ax2asc((ax25_address *)entry->ha));
1304                         else {
1305 #endif
1306 #endif
1307 
1308                         for(k=0,j=0;k<HBUFFERLEN-3 && j<entry->dev->addr_len;j++)
1309                         {
1310                                 hbuffer[k++]=hexbuf[ (entry->ha[j]>>4)&15 ];
1311                                 hbuffer[k++]=hexbuf[  entry->ha[j]&15     ];
1312                                 hbuffer[k++]=':';
1313                         }
1314                         hbuffer[--k]=0;
1315         
1316 #ifdef CONFIG_AX25
1317                         }
1318 #endif
1319                         size = sprintf(buffer+len,
1320                                 "%-17s0x%-10x0x%-10x%s",
1321                                 in_ntoa(entry->ip),
1322                                 (unsigned int)entry->dev->type,
1323                                 entry->flags,
1324                                 hbuffer);
1325 #if RT_CACHE_DEBUG < 2
1326                         size += sprintf(buffer+len+size,
1327                                  "     %-17s %s\n",
1328                                  entry->mask==DEF_ARP_NETMASK ?
1329                                  "*" : in_ntoa(entry->mask), entry->dev->name);
1330 #else
1331                         size += sprintf(buffer+len+size,
1332                                  "     %-17s %s\t%ld\t%1d\n",
1333                                  entry->mask==DEF_ARP_NETMASK ?
1334                                  "*" : in_ntoa(entry->mask), entry->dev->name, 
1335                                  entry->hh ? entry->hh->hh_refcnt : -1,
1336                                  entry->hh ? entry->hh->hh_uptodate : 0);
1337 #endif
1338         
1339                         len += size;
1340                         pos += size;
1341                   
1342                         if (pos <= offset)
1343                                 len=0;
1344                         if (pos >= offset+length)
1345                                 break;
1346                 }
1347         }
1348         arp_unlock();
1349   
1350         *start = buffer+len-(pos-offset);       /* Start of wanted data */
1351         len = pos-offset;                       /* Start slop */
1352         if (len>length)
1353                 len = length;                   /* Ending slop */
1354         return len;
1355 }
1356 
1357 
1358 
1359 int arp_bind_cache(struct hh_cache ** hhp, struct device *dev, unsigned short htype, u32 paddr)
     /* [previous][next][first][last][top][bottom][index][help] */
1360 {
1361         struct arp_table *entry;
1362         struct hh_cache *hh = *hhp;
1363         int addr_hint;
1364         unsigned long flags;
1365 
1366         if (hh)
1367                 return 1;
1368 
1369         if ((addr_hint = ip_chk_addr(paddr)) != 0)
1370         {
1371                 unsigned char haddr[MAX_ADDR_LEN];
1372                 if (hh)
1373                         return 1;
1374                 hh = kmalloc(sizeof(struct hh_cache), GFP_ATOMIC);
1375                 if (!hh)
1376                         return 1;
1377                 arp_set_predefined(addr_hint, haddr, paddr, dev);
1378                 hh->hh_uptodate = 0;
1379                 hh->hh_refcnt = 1;
1380                 hh->hh_arp = NULL;
1381                 hh->hh_next = NULL;
1382                 hh->hh_type = htype;
1383                 *hhp = hh;
1384                 dev->header_cache_update(hh, dev, haddr);
1385                 return 0;
1386         }
1387 
1388         save_flags(flags);
1389 
1390         arp_fast_lock();
1391 
1392         entry = arp_lookup(paddr, 0, dev);
1393 
1394         if (entry)
1395         {
1396                 cli();
1397                 for (hh = entry->hh; hh; hh=hh->hh_next)
1398                         if (hh->hh_type == htype)
1399                                 break;
1400                 if (hh)
1401                 {
1402                         hh->hh_refcnt++;
1403                         *hhp = hh;
1404                         restore_flags(flags);
1405                         arp_unlock();
1406                         return 1;
1407                 }
1408                 restore_flags(flags);
1409         }
1410 
1411         hh = kmalloc(sizeof(struct hh_cache), GFP_ATOMIC);
1412         if (!hh)
1413         {
1414                 arp_unlock();
1415                 return 1;
1416         }
1417 
1418         hh->hh_uptodate = 0;
1419         hh->hh_refcnt = 1;
1420         hh->hh_arp = NULL;
1421         hh->hh_next = NULL;
1422         hh->hh_type = htype;
1423 
1424         if (entry)
1425         {
1426                 dev->header_cache_update(hh, dev, entry->ha);
1427                 *hhp = hh;
1428                 cli();
1429                 hh->hh_arp = (void*)entry;
1430                 entry->hh = hh;
1431                 hh->hh_refcnt++;
1432                 restore_flags(flags);
1433                 entry->last_used = jiffies;
1434                 arp_unlock();
1435                 return 0;
1436         }
1437 
1438 
1439         /*
1440          *      Create a new unresolved entry.
1441          */
1442         
1443         entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
1444                                         GFP_ATOMIC);
1445         if (entry == NULL)
1446         {
1447                 kfree_s(hh, sizeof(struct hh_cache));
1448                 arp_unlock();
1449                 return 1;
1450         }
1451 
1452         entry->last_updated = entry->last_used = jiffies;
1453         entry->flags = 0;
1454         entry->ip = paddr;
1455         entry->mask = DEF_ARP_NETMASK;
1456         memset(entry->ha, 0, dev->addr_len);
1457         entry->dev = dev;
1458         entry->hh = hh;
1459         ATOMIC_INCR(&hh->hh_refcnt);
1460         init_timer(&entry->timer);
1461         entry->timer.function = arp_expire_request;
1462         entry->timer.data = (unsigned long)entry;
1463         entry->timer.expires = jiffies + ARP_RES_TIME;
1464         skb_queue_head_init(&entry->skb);
1465 
1466         if (arp_lock == 1)
1467         {
1468                 unsigned long hash = HASH(paddr);
1469                 cli();
1470                 entry->next = arp_tables[hash];
1471                 arp_tables[hash] = entry;
1472                 hh->hh_arp = (void*)entry;
1473                 entry->retries = ARP_MAX_TRIES;
1474                 restore_flags(flags);
1475 
1476                 add_timer(&entry->timer);
1477                 arp_send(ARPOP_REQUEST, ETH_P_ARP, paddr, dev, dev->pa_addr, NULL, dev->dev_addr, NULL);
1478         }
1479         else
1480         {
1481 #if RT_CACHE_DEBUG >= 1
1482                 printk("arp_cache_bind: %08x backlogged\n", entry->ip);
1483 #endif
1484                 arp_enqueue(&arp_backlog, entry);
1485                 arp_bh_mask |= ARP_BH_BACKLOG;
1486         }
1487         *hhp = hh;
1488         arp_unlock();
1489         return 0;
1490 }
1491 
1492 static void arp_run_bh()
     /* [previous][next][first][last][top][bottom][index][help] */
1493 {
1494         unsigned long flags;
1495         struct arp_table *entry, *entry1;
1496         struct hh_cache *hh;
1497         __u32 sip;
1498 
1499         save_flags(flags);
1500         cli();
1501         if (!arp_lock)
1502         {
1503                 arp_fast_lock();
1504 
1505                 while ((entry = arp_dequeue(&arp_backlog)) != NULL)
1506                 {
1507                         unsigned long hash;
1508                         sti();
1509                         sip = entry->ip;
1510                         hash = HASH(sip);
1511 
1512                         /* It's possible, that an entry with the same pair 
1513                          * (addr,type) was already created. Our entry is older,
1514                          * so it should be discarded.
1515                          */
1516                         for (entry1=arp_tables[hash]; entry1; entry1=entry1->next)
1517                                 if (entry1->ip==sip && entry1->dev == entry->dev)
1518                                         break;
1519 
1520                         if (!entry1)
1521                         {
1522                                 struct device  * dev = entry->dev;
1523                                 cli();
1524                                 entry->next = arp_tables[hash];
1525                                 arp_tables[hash] = entry;
1526                                 for (hh=entry->hh; hh; hh=hh->hh_next)
1527                                         hh->hh_arp = (void*)entry;
1528                                 sti();
1529                                 del_timer(&entry->timer);
1530                                 entry->timer.expires = jiffies + ARP_RES_TIME;
1531                                 add_timer(&entry->timer);
1532                                 entry->retries = ARP_MAX_TRIES;
1533                                 arp_send(ARPOP_REQUEST, ETH_P_ARP, entry->ip, dev, dev->pa_addr, NULL, dev->dev_addr, NULL);
1534 #if RT_CACHE_DEBUG >= 1
1535                                 printk("arp_run_bh: %08x reinstalled\n", sip);
1536 #endif
1537                         }
1538                         else
1539                         {
1540                                 struct sk_buff * skb;
1541                                 struct hh_cache * next;
1542 
1543                                 /* Discard entry, but preserve its hh's and
1544                                  * skb's.
1545                                  */
1546                                 cli();
1547                                 for (hh=entry->hh; hh; hh=next)
1548                                 {
1549                                         next = hh->hh_next;
1550                                         hh->hh_next = entry1->hh;
1551                                         entry1->hh = hh;
1552                                         hh->hh_arp = (void*)entry1;
1553                                 }
1554                                 entry->hh = NULL;
1555 
1556                                 /* Prune skb list from entry
1557                                  * and graft it to entry1.
1558                                  */
1559                                 while ((skb = skb_dequeue(&entry->skb)) != NULL)
1560                                 {
1561                                         skb_device_lock(skb);
1562                                         sti();
1563                                         skb_queue_tail(&entry1->skb, skb);
1564                                         skb_device_unlock(skb);
1565                                         cli();
1566                                 }
1567                                 sti();
1568                                 
1569 #if RT_CACHE_DEBUG >= 1
1570                                 printk("arp_run_bh: entry %08x was born dead\n", entry->ip);
1571 #endif
1572                                 arp_free_entry(entry);
1573 
1574                                 if (entry1->flags & ATF_COM)
1575                                 {
1576                                         arp_update_hhs(entry1);
1577                                         arp_send_q(entry1);
1578                                 }
1579                         }
1580                         cli();
1581                 }
1582                 arp_bh_mask  &= ~ARP_BH_BACKLOG;
1583                 arp_unlock();
1584         }
1585         restore_flags(flags);
1586 }
1587 
1588 
1589 /*
1590  *      Set (create) an ARP cache entry.
1591  */
1592 
1593 static int arp_req_set(struct arpreq *r, struct device * dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1594 {
1595         struct arp_table *entry;
1596         struct sockaddr_in *si;
1597         struct rtable *rt;
1598         struct device * dev1;
1599         u32 ip;
1600 
1601         /*
1602          * Find out about the hardware type.
1603          * We have to be compatible with BSD UNIX, so we have to
1604          * assume that a "not set" value (i.e. 0) means Ethernet.
1605          *
1606          * ANK: Hey, who wrote it? Do you really mean that BSD considers 
1607          *      ARPHRD_NETROM as ARPHRD_ETHER, or somthing another?
1608          */
1609         
1610         si = (struct sockaddr_in *) &r->arp_pa;
1611         ip = si->sin_addr.s_addr;
1612 
1613         /*
1614          *      Is it reachable ?
1615          */
1616 
1617         rt = ip_rt_route(ip, 0);
1618         if (!rt)
1619                 return -ENETUNREACH;
1620         dev1 = rt->rt_dev;
1621         ip_rt_put(rt);
1622 
1623         if (((r->arp_flags & ATF_PUBL) && dev == dev1) ||
1624             (!(r->arp_flags & ATF_PUBL) && dev != dev1))
1625                 return -EINVAL;
1626 
1627 #if RT_CACHE_DEBUG >= 1
1628         if (arp_lock)
1629                 printk("arp_req_set: bug\n");
1630 #endif
1631         arp_fast_lock();
1632 
1633         /*
1634          *      Is there an existing entry for this address?
1635          */
1636 
1637         /*
1638          *      Find the entry
1639          */
1640         
1641         entry = arp_lookup(ip, r->arp_flags & ~ATF_NETMASK, dev);
1642 
1643         if (entry)
1644         {
1645                 arp_destroy(entry);
1646                 entry = NULL;
1647         }
1648 
1649         /*
1650          *      Do we need to create a new entry
1651          */
1652         
1653         if (entry == NULL)
1654         {
1655                 entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
1656                                         GFP_ATOMIC);
1657                 if (entry == NULL)
1658                 {
1659                         arp_unlock();
1660                         return -ENOMEM;
1661                 }
1662                 entry->ip = ip;
1663                 entry->hh = NULL;
1664                 init_timer(&entry->timer);
1665                 entry->timer.function = arp_expire_request;
1666                 entry->timer.data = (unsigned long)entry;
1667 
1668                 if (r->arp_flags & ATF_PUBL)
1669                 {
1670                         cli();
1671                         entry->next = arp_proxy_list;
1672                         arp_proxy_list = entry;
1673                         sti();
1674                 }
1675                 else
1676                 {
1677                         unsigned long hash = HASH(ip);
1678                         cli();
1679                         entry->next = arp_tables[hash];
1680                         arp_tables[hash] = entry;
1681                         sti();
1682                 }
1683                 skb_queue_head_init(&entry->skb);
1684         }
1685         /*
1686          *      We now have a pointer to an ARP entry.  Update it!
1687          */
1688         
1689         if ((r->arp_flags & ATF_COM) && !r->arp_ha.sa_data[0])
1690                 memcpy(&entry->ha, dev->dev_addr, dev->addr_len);
1691         else
1692                 memcpy(&entry->ha, &r->arp_ha.sa_data, dev->addr_len);
1693         entry->last_updated = entry->last_used = jiffies;
1694         entry->flags = r->arp_flags | ATF_COM;
1695         if ((entry->flags & ATF_PUBL) && (entry->flags & ATF_NETMASK))
1696         {
1697                 si = (struct sockaddr_in *) &r->arp_netmask;
1698                 entry->mask = si->sin_addr.s_addr;
1699         }
1700         else
1701                 entry->mask = DEF_ARP_NETMASK;
1702         entry->dev = dev;
1703         arp_update_hhs(entry);
1704         arp_unlock();
1705         return 0;
1706 }
1707 
1708 
1709 
1710 /*
1711  *      Get an ARP cache entry.
1712  */
1713 
1714 static int arp_req_get(struct arpreq *r, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1715 {
1716         struct arp_table *entry;
1717         struct sockaddr_in *si;
1718 
1719         si = (struct sockaddr_in *) &r->arp_pa;
1720 
1721 #if RT_CACHE_DEBUG >= 1
1722         if (arp_lock)
1723                 printk("arp_req_set: bug\n");
1724 #endif
1725         arp_fast_lock();
1726 
1727         entry = arp_lookup(si->sin_addr.s_addr, r->arp_flags|ATF_NETMASK, dev);
1728 
1729         if (entry == NULL)
1730         {
1731                 arp_unlock();
1732                 return -ENXIO;
1733         }
1734 
1735         /*
1736          *      We found it; copy into structure.
1737          */
1738         
1739         memcpy(r->arp_ha.sa_data, &entry->ha, entry->dev->addr_len);
1740         r->arp_ha.sa_family = entry->dev->type;
1741         r->arp_flags = entry->flags;
1742         strncpy(r->arp_dev, entry->dev->name, 16);
1743         arp_unlock();
1744         return 0;
1745 }
1746 
1747 static int arp_req_delete(struct arpreq *r, struct device * dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1748 {
1749         struct arp_table *entry;
1750         struct sockaddr_in *si;
1751 
1752         si = (struct sockaddr_in *) &r->arp_pa;
1753 #if RT_CACHE_DEBUG >= 1
1754         if (arp_lock)
1755                 printk("arp_req_delete: bug\n");
1756 #endif
1757         arp_fast_lock();
1758 
1759         if (!(r->arp_flags & ATF_PUBL))
1760         {
1761                 for (entry = arp_tables[HASH(si->sin_addr.s_addr)];
1762                      entry != NULL; entry = entry->next)
1763                         if (entry->ip == si->sin_addr.s_addr 
1764                             && entry->dev == dev)
1765                         {
1766                                 arp_destroy(entry);
1767                                 arp_unlock();
1768                                 return 0;
1769                         }
1770         }
1771         else
1772         {
1773                 for (entry = arp_proxy_list;
1774                      entry != NULL; entry = entry->next)
1775                         if (entry->ip == si->sin_addr.s_addr 
1776                             && entry->dev == dev) 
1777                         {
1778                                 arp_destroy(entry);
1779                                 arp_unlock();
1780                                 return 0;
1781                         }
1782         }
1783 
1784         arp_unlock();
1785         return -ENXIO;
1786 }
1787 
1788 /*
1789  *      Handle an ARP layer I/O control request.
1790  */
1791 
1792 int arp_ioctl(unsigned int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1793 {
1794         int err;
1795         struct arpreq r;
1796 
1797         struct device * dev = NULL;
1798 
1799         switch(cmd)
1800         {
1801                 case SIOCDARP:
1802                 case SIOCSARP:
1803                         if (!suser())
1804                                 return -EPERM;
1805                 case SIOCGARP:
1806                         err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq));
1807                         if (err)
1808                                 return err;
1809                         memcpy_fromfs(&r, arg, sizeof(struct arpreq));
1810                         break;
1811                 case OLD_SIOCDARP:
1812                 case OLD_SIOCSARP:
1813                         if (!suser())
1814                                 return -EPERM;
1815                 case OLD_SIOCGARP:
1816                         err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq_old));
1817                         if (err)
1818                                 return err;
1819                         memcpy_fromfs(&r, arg, sizeof(struct arpreq_old));
1820                         memset(&r.arp_dev, 0, sizeof(r.arp_dev));
1821                         break;
1822                 default:
1823                         return -EINVAL;
1824         }
1825 
1826         if (r.arp_pa.sa_family != AF_INET)
1827                 return -EPFNOSUPPORT;
1828         if (((struct sockaddr_in *)&r.arp_pa)->sin_addr.s_addr == 0)
1829                 return -EINVAL;
1830 
1831         if (r.arp_dev[0])
1832         {
1833                 if ((dev = dev_get(r.arp_dev)) == NULL)
1834                         return -ENODEV;
1835 
1836                 if (!r.arp_ha.sa_family)
1837                         r.arp_ha.sa_family = dev->type;
1838                 else if (r.arp_ha.sa_family != dev->type)
1839                         return -EINVAL;
1840         }
1841         else
1842         {
1843                 /*
1844                  * Device was not specified. Take the first suitable one.
1845                  */
1846                 if ((dev = dev_getbytype(r.arp_ha.sa_family)) == NULL)
1847                         return -ENODEV;
1848         }
1849 
1850         switch(cmd)
1851         {
1852                 case SIOCDARP:
1853                         return arp_req_delete(&r, dev);
1854                 case SIOCSARP:
1855                         return arp_req_set(&r, dev);
1856                 case OLD_SIOCDARP:
1857                         /* old  SIOCDARP destoyes both
1858                          * normal and proxy mappings
1859                          */
1860                         r.arp_flags &= ~ATF_PUBL;
1861                         err = arp_req_delete(&r, dev);
1862                         r.arp_flags |= ATF_PUBL;
1863                         if (!err)
1864                                 arp_req_delete(&r, dev);
1865                         else
1866                                 err = arp_req_delete(&r, dev);
1867                         return err;
1868                 case OLD_SIOCSARP:
1869                         err = arp_req_set(&r, dev);
1870                         /* old SIOCSARP works so funny,
1871                          * that its behaviour can be emulated
1872                          * only approximately 8).
1873                          * It should work. --ANK
1874                          */
1875                         if (r.arp_flags & ATF_PUBL)
1876                         {       
1877                                 r.arp_flags &= ~ATF_PUBL;
1878                                 arp_req_delete(&r, dev);
1879                         }
1880                         return err;
1881                 case SIOCGARP:
1882                         err = verify_area(VERIFY_WRITE, arg, sizeof(struct arpreq));
1883                         if (err)
1884                                 return err;
1885                         err = arp_req_get(&r, dev);
1886                         if (!err)
1887                                 memcpy_tofs(arg, &r, sizeof(r));
1888                         return err;
1889                 case OLD_SIOCGARP:
1890                         err = verify_area(VERIFY_WRITE, arg, sizeof(struct arpreq_old));
1891                         if (err)
1892                                 return err;
1893                         r.arp_flags &= ~ATF_PUBL;
1894                         err = arp_req_get(&r, dev);
1895                         if (err < 0)
1896                         {
1897                                 r.arp_flags |= ATF_PUBL;
1898                                 err = arp_req_get(&r, dev);
1899                         }
1900                         if (!err)
1901                                 memcpy_tofs(arg, &r, sizeof(struct arpreq_old));
1902                         return err;
1903         }
1904         /*NOTREACHED*/
1905         return 0;
1906 }
1907 
1908 
1909 /*
1910  *      Called once on startup.
1911  */
1912 
1913 static struct packet_type arp_packet_type =
1914 {
1915         0,      /* Should be: __constant_htons(ETH_P_ARP) - but this _doesn't_ come out constant! */
1916         NULL,           /* All devices */
1917         arp_rcv,
1918         NULL,
1919         NULL
1920 };
1921 
1922 static struct notifier_block arp_dev_notifier={
1923         arp_device_event,
1924         NULL,
1925         0
1926 };
1927 
1928 void arp_init (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1929 {
1930         /* Register the packet type */
1931         arp_packet_type.type=htons(ETH_P_ARP);
1932         dev_add_pack(&arp_packet_type);
1933         /* Start with the regular checks for expired arp entries. */
1934         add_timer(&arp_timer);
1935         /* Register for device down reports */
1936         register_netdevice_notifier(&arp_dev_notifier);
1937 
1938         proc_net_register(&(struct proc_dir_entry) {
1939                 PROC_NET_ARP, 3, "arp",
1940                 S_IFREG | S_IRUGO, 1, 0, 0,
1941                 0, &proc_net_inode_operations,
1942                 arp_get_info
1943         });
1944 }
1945 

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