root/net/inet/arp.c

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

DEFINITIONS

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

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