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

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