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

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