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

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