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

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