root/net/inet/arp.c

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

DEFINITIONS

This source file includes following definitions.
  1. arp_check_expire
  2. arp_release_entry
  3. arp_device_event
  4. arp_send
  5. arp_expire_request
  6. arp_send_q
  7. arp_destroy
  8. arp_rcv
  9. arp_find
  10. arp_get_info
  11. arp_lookup
  12. arp_req_set
  13. arp_req_get
  14. arp_ioctl
  15. arp_init

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

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