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

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