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 
 726         switch (ip_chk_addr(paddr))
 727         {
 728                 case IS_MYADDR:
 729                         printk("ARP: arp called for own IP address\n");
 730                         memcpy(haddr, dev->dev_addr, dev->addr_len);
 731                         skb->arp = 1;
 732                         return 0;
 733                 case IS_BROADCAST:
 734                         memcpy(haddr, dev->broadcast, dev->addr_len);
 735                         skb->arp = 1;
 736                         return 0;
 737         }
 738 
 739         hash = HASH(paddr);
 740         cli();
 741 
 742         /*
 743          *      Find an entry
 744          */
 745         for (entry = arp_tables[hash]; entry != NULL; entry = entry->next)
 746                 if (entry->ip == paddr)
 747                         break;
 748 
 749 
 750         if (entry != NULL)      /* It exists */
 751         {
 752                 if (!(entry->flags & ATF_COM))
 753                 {
 754                         /*
 755                          *      A request was already send, but no reply yet. Thus
 756                          *      queue the packet with the previous attempt
 757                          */
 758                         
 759                         if (skb != NULL)
 760                                 skb_queue_tail(&entry->skb, skb);
 761                         sti();
 762                         return 1;
 763                 }
 764 
 765                 /*
 766                  *      Update the record
 767                  */
 768                 
 769                 entry->last_used = jiffies;
 770                 memcpy(haddr, entry->ha, dev->addr_len);
 771                 if (skb)
 772                         skb->arp = 1;
 773                 sti();
 774                 return 0;
 775         }
 776 
 777         /*
 778          *      Create a new unresolved entry.
 779          */
 780         
 781         entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
 782                                         GFP_ATOMIC);
 783         if (entry != NULL)
 784         {
 785                 entry->ip = paddr;
 786                 entry->hlen = dev->addr_len;
 787                 entry->htype = dev->type;
 788                 entry->flags = 0;
 789                 memset(entry->ha, 0, dev->addr_len);
 790                 entry->last_used = jiffies;
 791                 entry->next = arp_tables[hash];
 792                 entry->dev = dev;
 793                 arp_tables[hash] = entry;
 794                 entry->timer.function = arp_expire_request;
 795                 entry->timer.data = (unsigned long)entry;
 796                 entry->timer.expires = ARP_RES_TIME;
 797                 add_timer(&entry->timer);
 798                 entry->retries = ARP_MAX_TRIES;
 799                 skb_queue_head_init(&entry->skb);
 800                 if (skb != NULL)
 801                         skb_queue_tail(&entry->skb, skb);
 802         }
 803         else
 804         {
 805                 if (skb != NULL && skb->free)
 806                         kfree_skb(skb, FREE_WRITE);
 807         }
 808         sti();
 809 
 810         /*
 811          *      If we didn't find an entry, we will try to send an ARP packet.
 812          */
 813         
 814         arp_send(ARPOP_REQUEST, ETH_P_ARP, paddr, dev, saddr, NULL, 
 815                  dev->dev_addr);
 816 
 817         return 1;
 818 }
 819 
 820 
 821 /*
 822  *      Write the contents of the ARP cache to a PROCfs file.
 823  */
 824 
 825 #define HBUFFERLEN 30
 826 
 827 int arp_get_info(char *buffer, char **start, off_t offset, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
 828 {
 829         int len=0;
 830         off_t begin=0;
 831         off_t pos=0;
 832         int size;
 833         struct arp_table *entry;
 834         char hbuffer[HBUFFERLEN];
 835         int i,j,k;
 836         const char hexbuf[] =  "0123456789ABCDEF";
 837 
 838         size = sprintf(buffer,"IP address       HW type     Flags       HW address\n");
 839         pos+=size;
 840         len+=size;
 841           
 842         cli();
 843         for(i=0; i<ARP_TABLE_SIZE; i++)
 844         {
 845                 for(entry=arp_tables[i]; entry!=NULL; entry=entry->next)
 846                 {
 847 /*
 848  *      Convert hardware address to XX:XX:XX:XX ... form.
 849  */
 850                         for(k=0,j=0;k<HBUFFERLEN-3 && j<entry->hlen;j++)
 851                         {
 852                                 hbuffer[k++]=hexbuf[ (entry->ha[j]>>4)&15 ];
 853                                 hbuffer[k++]=hexbuf[  entry->ha[j]&15     ];
 854                                 hbuffer[k++]=':';
 855                         }
 856                         hbuffer[--k]=0;
 857         
 858                         size = sprintf(buffer+len,
 859                                 "%-17s0x%-10x0x%-10x%s\n",
 860                                 in_ntoa(entry->ip),
 861                                 (unsigned int)entry->htype,
 862                                 entry->flags,
 863                                 hbuffer);
 864         
 865                         len+=size;
 866                         pos=begin+len;
 867                   
 868                         if(pos<offset)
 869                         {
 870                                 len=0;
 871                                 begin=pos;
 872                         }
 873                         if(pos>offset+length)
 874                                 break;
 875                 }
 876         }
 877         sti();
 878   
 879         *start=buffer+(offset-begin);   /* Start of wanted data */
 880         len-=(offset-begin);            /* Start slop */
 881         if(len>length)
 882                 len=length;                     /* Ending slop */
 883         return len;
 884 }
 885 
 886 
 887 /*
 888  *      This will find an entry in the ARP table by looking at the IP address.
 889  *      Be careful, interrupts are turned off on exit!!!
 890  */
 891 
 892 static struct arp_table *arp_lookup(unsigned long paddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 893 {
 894         struct arp_table *entry;
 895         unsigned long hash = HASH(paddr);
 896 
 897         cli();
 898         for (entry = arp_tables[hash]; entry != NULL; entry = entry->next)
 899                 if (entry->ip == paddr) break;
 900         return entry;
 901 }
 902 
 903 
 904 /*
 905  *      Set (create) an ARP cache entry.
 906  */
 907 
 908 static int arp_req_set(struct arpreq *req)
     /* [previous][next][first][last][top][bottom][index][help] */
 909 {
 910         struct arpreq r;
 911         struct arp_table *entry;
 912         struct sockaddr_in *si;
 913         int htype, hlen;
 914         unsigned long ip, hash;
 915         struct rtable *rt;
 916 
 917         memcpy_fromfs(&r, req, sizeof(r));
 918 
 919         /* We only understand about IP addresses... */
 920         if (r.arp_pa.sa_family != AF_INET)
 921                 return -EPFNOSUPPORT;
 922 
 923         /*
 924          * Find out about the hardware type.
 925          * We have to be compatible with BSD UNIX, so we have to
 926          * assume that a "not set" value (i.e. 0) means Ethernet.
 927          */
 928         
 929         switch (r.arp_ha.sa_family) {
 930                 case 0:
 931                         /* Moan about this. ARP family 0 is NetROM and _will_ be needed */
 932                         printk("Application using old BSD convention for arp set. Please recompile it.\n");
 933                 case ARPHRD_ETHER:
 934                         htype = ARPHRD_ETHER;
 935                         hlen = ETH_ALEN;
 936                         break;
 937 #ifdef CONFIG_AX25
 938                 case ARPHRD_AX25:
 939                         htype = ARPHRD_AX25;
 940                         hlen = 7;
 941                         break;
 942 #endif
 943                 default:
 944                         return -EPFNOSUPPORT;
 945         }
 946 
 947         si = (struct sockaddr_in *) &r.arp_pa;
 948         ip = si->sin_addr.s_addr;
 949         if (ip == 0)
 950         {
 951                 printk("ARP: SETARP: requested PA is 0.0.0.0 !\n");
 952                 return -EINVAL;
 953         }
 954 
 955         /*
 956          *      Is it reachable directly ?
 957          */
 958 
 959         rt = ip_rt_route(ip, NULL, NULL);
 960         if (rt == NULL)
 961                 return -ENETUNREACH;
 962 
 963         /*
 964          *      Is there an existing entry for this address?
 965          */
 966         
 967         hash = HASH(ip);
 968         cli();
 969 
 970         /*
 971          *      Find the entry
 972          */
 973         for (entry = arp_tables[hash]; entry != NULL; entry = entry->next)
 974                 if (entry->ip == ip)
 975                         break;
 976 
 977         /*
 978          *      Do we need to create a new entry
 979          */
 980         
 981         if (entry == NULL)
 982         {
 983                 entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
 984                                         GFP_ATOMIC);
 985                 if (entry == NULL)
 986                 {
 987                         sti();
 988                         return -ENOMEM;
 989                 }
 990                 entry->ip = ip;
 991                 entry->hlen = hlen;
 992                 entry->htype = htype;
 993                 entry->next = arp_tables[hash];
 994                 arp_tables[hash] = entry;
 995                 skb_queue_head_init(&entry->skb);
 996         }
 997         else
 998                 if (entry->flags & ATF_PUBL)
 999                         proxies--;
1000         /*
1001          *      We now have a pointer to an ARP entry.  Update it!
1002          */
1003         
1004         memcpy(&entry->ha, &r.arp_ha.sa_data, hlen);
1005         entry->last_used = jiffies;
1006         entry->flags = r.arp_flags | ATF_COM;
1007         if (entry->flags & ATF_PUBL)
1008                 proxies++;
1009         entry->dev = rt->rt_dev;
1010         sti();
1011 
1012         return 0;
1013 }
1014 
1015 
1016 /*
1017  *      Get an ARP cache entry.
1018  */
1019 
1020 static int arp_req_get(struct arpreq *req)
     /* [previous][next][first][last][top][bottom][index][help] */
1021 {
1022         struct arpreq r;
1023         struct arp_table *entry;
1024         struct sockaddr_in *si;
1025 
1026         /*
1027          *      We only understand about IP addresses...
1028          */
1029         
1030         memcpy_fromfs(&r, req, sizeof(r));
1031 
1032         if (r.arp_pa.sa_family != AF_INET)
1033                 return -EPFNOSUPPORT;
1034 
1035         /*
1036          *      Is there an existing entry for this address?
1037          */
1038         
1039         si = (struct sockaddr_in *) &r.arp_pa;
1040         entry = arp_lookup(si->sin_addr.s_addr);
1041 
1042         if (entry == NULL)
1043         {
1044                 sti();
1045                 return -ENXIO;
1046         }
1047 
1048         /*
1049          *      We found it; copy into structure.
1050          */
1051         
1052         memcpy(r.arp_ha.sa_data, &entry->ha, entry->hlen);
1053         r.arp_ha.sa_family = entry->htype;
1054         r.arp_flags = entry->flags;
1055         sti();
1056 
1057         /*
1058          *      Copy the information back
1059          */
1060         
1061         memcpy_tofs(req, &r, sizeof(r));
1062         return 0;
1063 }
1064 
1065 
1066 /*
1067  *      Handle an ARP layer I/O control request.
1068  */
1069 
1070 int arp_ioctl(unsigned int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1071 {
1072         struct arpreq r;
1073         struct sockaddr_in *si;
1074         int err;
1075 
1076         switch(cmd)
1077         {
1078                 case DDIOCSDBG:
1079                         return dbg_ioctl(arg, DBG_ARP);
1080                 case SIOCDARP:
1081                         if (!suser())
1082                                 return -EPERM;
1083                         err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq));
1084                         if(err)
1085                                 return err;
1086                         memcpy_fromfs(&r, arg, sizeof(r));
1087                         if (r.arp_pa.sa_family != AF_INET)
1088                                 return -EPFNOSUPPORT;
1089                         si = (struct sockaddr_in *) &r.arp_pa;
1090                         arp_destroy(si->sin_addr.s_addr, 1);
1091                         return 0;
1092                 case SIOCGARP:
1093                         err = verify_area(VERIFY_WRITE, arg, sizeof(struct arpreq));
1094                         if(err)
1095                                 return err;
1096                         return arp_req_get((struct arpreq *)arg);
1097                 case SIOCSARP:
1098                         if (!suser())
1099                                 return -EPERM;
1100                         err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq));
1101                         if(err)
1102                                 return err;
1103                         return arp_req_set((struct arpreq *)arg);
1104                 default:
1105                         return -EINVAL;
1106         }
1107         /*NOTREACHED*/
1108         return 0;
1109 }
1110 
1111 
1112 /*
1113  *      Called once on startup.
1114  */
1115 
1116 static struct packet_type arp_packet_type =
1117 {
1118         0,      /* Should be: __constant_htons(ETH_P_ARP) - but this _doesn't_ come out constant! */
1119         0,              /* copy */
1120         arp_rcv,
1121         NULL,
1122         NULL
1123 };
1124 
1125 void arp_init (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1126 {
1127         /* Register the packet type */
1128         arp_packet_type.type=htons(ETH_P_ARP);
1129         dev_add_pack(&arp_packet_type);
1130         /* Start with the regular checks for expired arp entries. */
1131         add_timer(&arp_timer);
1132 }
1133 

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