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

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