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

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