root/net/ipv4/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_find_cache
  13. arp_req_set
  14. arp_req_get
  15. arp_ioctl
  16. arp_init

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

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