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         switch (ip_chk_addr(paddr))
 791         {
 792                 case IS_MYADDR:
 793                         printk("ARP: arp called for own IP address\n");
 794                         memcpy(haddr, dev->dev_addr, dev->addr_len);
 795                         skb->arp = 1;
 796                         return 0;
 797                 case IS_BROADCAST:
 798                         memcpy(haddr, dev->broadcast, dev->addr_len);
 799                         skb->arp = 1;
 800                         return 0;
 801         }
 802 
 803         hash = HASH(paddr);
 804         cli();
 805 
 806         /*
 807          *      Find an entry
 808          */
 809         entry = arp_lookup(paddr, 0);
 810 
 811         if (entry != NULL)      /* It exists */
 812         {
 813                 if (!(entry->flags & ATF_COM))
 814                 {
 815                         /*
 816                          *      A request was already send, but no reply yet. Thus
 817                          *      queue the packet with the previous attempt
 818                          */
 819                         
 820                         if (skb != NULL)
 821                         {
 822                                 skb_queue_tail(&entry->skb, skb);
 823                                 skb_device_unlock(skb);
 824                         }
 825                         sti();
 826                         return 1;
 827                 }
 828 
 829                 /*
 830                  *      Update the record
 831                  */
 832                 
 833                 entry->last_used = jiffies;
 834                 memcpy(haddr, entry->ha, dev->addr_len);
 835                 if (skb)
 836                         skb->arp = 1;
 837                 sti();
 838                 return 0;
 839         }
 840 
 841         /*
 842          *      Create a new unresolved entry.
 843          */
 844         
 845         entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
 846                                         GFP_ATOMIC);
 847         if (entry != NULL)
 848         {
 849                 entry->mask = DEF_ARP_NETMASK;
 850                 entry->ip = paddr;
 851                 entry->hlen = dev->addr_len;
 852                 entry->htype = dev->type;
 853                 entry->flags = 0;
 854                 memset(entry->ha, 0, dev->addr_len);
 855                 entry->dev = dev;
 856                 entry->last_used = jiffies;
 857                 init_timer(&entry->timer);
 858                 entry->timer.function = arp_expire_request;
 859                 entry->timer.data = (unsigned long)entry;
 860                 entry->timer.expires = ARP_RES_TIME;
 861                 entry->next = arp_tables[hash];
 862                 arp_tables[hash] = entry;
 863                 add_timer(&entry->timer);
 864                 entry->retries = ARP_MAX_TRIES;
 865                 skb_queue_head_init(&entry->skb);
 866                 if (skb != NULL)
 867                 {
 868                         skb_queue_tail(&entry->skb, skb);
 869                         skb_device_unlock(skb);
 870                 }
 871         }
 872         else
 873         {
 874                 if (skb != NULL && skb->free)
 875                         kfree_skb(skb, FREE_WRITE);
 876         }
 877         sti();
 878 
 879         /*
 880          *      If we didn't find an entry, we will try to send an ARP packet.
 881          */
 882         
 883         arp_send(ARPOP_REQUEST, ETH_P_ARP, paddr, dev, saddr, NULL, 
 884                  dev->dev_addr);
 885 
 886         return 1;
 887 }
 888 
 889 
 890 /*
 891  *      Write the contents of the ARP cache to a PROCfs file.
 892  */
 893 
 894 #define HBUFFERLEN 30
 895 
 896 int arp_get_info(char *buffer, char **start, off_t offset, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
 897 {
 898         int len=0;
 899         off_t begin=0;
 900         off_t pos=0;
 901         int size;
 902         struct arp_table *entry;
 903         char hbuffer[HBUFFERLEN];
 904         int i,j,k;
 905         const char hexbuf[] =  "0123456789ABCDEF";
 906 
 907         size = sprintf(buffer,"IP address       HW type     Flags       HW address            Mask\n");
 908 
 909         pos+=size;
 910         len+=size;
 911           
 912         cli();
 913         for(i=0; i<FULL_ARP_TABLE_SIZE; i++)
 914         {
 915                 for(entry=arp_tables[i]; entry!=NULL; entry=entry->next)
 916                 {
 917 /*
 918  *      Convert hardware address to XX:XX:XX:XX ... form.
 919  */
 920 #ifdef CONFIG_AX25
 921 
 922                         if(entry->htype==ARPHRD_AX25)
 923                              strcpy(hbuffer,ax2asc((ax25_address *)entry->ha));
 924                         else {
 925 #endif
 926 
 927                         for(k=0,j=0;k<HBUFFERLEN-3 && j<entry->hlen;j++)
 928                         {
 929                                 hbuffer[k++]=hexbuf[ (entry->ha[j]>>4)&15 ];
 930                                 hbuffer[k++]=hexbuf[  entry->ha[j]&15     ];
 931                                 hbuffer[k++]=':';
 932                         }
 933                         hbuffer[--k]=0;
 934         
 935 #ifdef CONFIG_AX25
 936                         }
 937 #endif
 938                         size = sprintf(buffer+len,
 939                                 "%-17s0x%-10x0x%-10x%s",
 940                                 in_ntoa(entry->ip),
 941                                 (unsigned int)entry->htype,
 942                                 entry->flags,
 943                                 hbuffer);
 944                         size += sprintf(buffer+len+size,
 945                                  "     %-17s\n",
 946                                   entry->mask==DEF_ARP_NETMASK?
 947                                    "*":in_ntoa(entry->mask));
 948         
 949                         len+=size;
 950                         pos=begin+len;
 951                   
 952                         if(pos<offset)
 953                         {
 954                                 len=0;
 955                                 begin=pos;
 956                         }
 957                         if(pos>offset+length)
 958                                 break;
 959                 }
 960         }
 961         sti();
 962   
 963         *start=buffer+(offset-begin);   /* Start of wanted data */
 964         len-=(offset-begin);            /* Start slop */
 965         if(len>length)
 966                 len=length;                     /* Ending slop */
 967         return len;
 968 }
 969 
 970 
 971 /*
 972  *      This will find an entry in the ARP table by looking at the IP address.
 973  *      If exact is true then only exact IP matches will be allowed
 974  *      for proxy entries, otherwise the netmask will be used
 975  */
 976 
 977 static struct arp_table *arp_lookup(unsigned long paddr, int exact)
     /* [previous][next][first][last][top][bottom][index][help] */
 978 {
 979         struct arp_table *entry;
 980         unsigned long hash = HASH(paddr);
 981         
 982         for (entry = arp_tables[hash]; entry != NULL; entry = entry->next)
 983                 if (entry->ip == paddr) break;
 984 
 985         /* it's possibly a proxy entry (with a netmask) */
 986         if (!entry)
 987         for (entry=arp_tables[PROXY_HASH]; entry != NULL; entry = entry->next)
 988           if (exact? (entry->ip==paddr) : !((entry->ip^paddr)&entry->mask)) 
 989             break;        
 990 
 991         return entry;
 992 }
 993 
 994 
 995 /*
 996  *      Set (create) an ARP cache entry.
 997  */
 998 
 999 static int arp_req_set(struct arpreq *req)
     /* [previous][next][first][last][top][bottom][index][help] */
1000 {
1001         struct arpreq r;
1002         struct arp_table *entry;
1003         struct sockaddr_in *si;
1004         int htype, hlen;
1005         unsigned long ip;
1006         struct rtable *rt;
1007 
1008         memcpy_fromfs(&r, req, sizeof(r));
1009 
1010         /* We only understand about IP addresses... */
1011         if (r.arp_pa.sa_family != AF_INET)
1012                 return -EPFNOSUPPORT;
1013 
1014         /*
1015          * Find out about the hardware type.
1016          * We have to be compatible with BSD UNIX, so we have to
1017          * assume that a "not set" value (i.e. 0) means Ethernet.
1018          */
1019         
1020         switch (r.arp_ha.sa_family) {
1021                 case ARPHRD_ETHER:
1022                         htype = ARPHRD_ETHER;
1023                         hlen = ETH_ALEN;
1024                         break;
1025                 case ARPHRD_ARCNET:
1026                         htype = ARPHRD_ARCNET;
1027                         hlen = 1;       /* length of arcnet addresses */
1028                         break;
1029 #ifdef CONFIG_AX25
1030                 case ARPHRD_AX25:
1031                         htype = ARPHRD_AX25;
1032                         hlen = 7;
1033                         break;
1034 #endif
1035                 default:
1036                         return -EPFNOSUPPORT;
1037         }
1038 
1039         si = (struct sockaddr_in *) &r.arp_pa;
1040         ip = si->sin_addr.s_addr;
1041         if (ip == 0)
1042         {
1043                 printk("ARP: SETARP: requested PA is 0.0.0.0 !\n");
1044                 return -EINVAL;
1045         }
1046 
1047         /*
1048          *      Is it reachable directly ?
1049          */
1050 
1051         rt = ip_rt_route(ip, NULL, NULL);
1052         if (rt == NULL)
1053                 return -ENETUNREACH;
1054 
1055         /*
1056          *      Is there an existing entry for this address?
1057          */
1058         
1059         cli();
1060 
1061         /*
1062          *      Find the entry
1063          */
1064         entry = arp_lookup(ip, 1);
1065 
1066         /*
1067          *      Do we need to create a new entry
1068          */
1069         
1070         if (entry == NULL)
1071         {
1072                 unsigned long hash = HASH(ip);
1073                 if (r.arp_flags & ATF_PUBL)
1074                   hash = PROXY_HASH;
1075 
1076                 entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
1077                                         GFP_ATOMIC);
1078                 if (entry == NULL)
1079                 {
1080                         sti();
1081                         return -ENOMEM;
1082                 }
1083                 entry->ip = ip;
1084                 entry->hlen = hlen;
1085                 entry->htype = htype;
1086                 init_timer(&entry->timer);
1087                 entry->next = arp_tables[hash];
1088                 arp_tables[hash] = entry;
1089                 skb_queue_head_init(&entry->skb);
1090         }
1091         /*
1092          *      We now have a pointer to an ARP entry.  Update it!
1093          */
1094         
1095         memcpy(&entry->ha, &r.arp_ha.sa_data, hlen);
1096         entry->last_used = jiffies;
1097         entry->flags = r.arp_flags | ATF_COM;
1098         if ((entry->flags & ATF_PUBL) && (entry->flags & ATF_NETMASK))
1099           {
1100             si = (struct sockaddr_in *) &r.arp_netmask;
1101             entry->mask = si->sin_addr.s_addr;
1102           }
1103         else
1104           entry->mask = DEF_ARP_NETMASK;
1105         entry->dev = rt->rt_dev;
1106         sti();
1107 
1108         return 0;
1109 }
1110 
1111 
1112 /*
1113  *      Get an ARP cache entry.
1114  */
1115 
1116 static int arp_req_get(struct arpreq *req)
     /* [previous][next][first][last][top][bottom][index][help] */
1117 {
1118         struct arpreq r;
1119         struct arp_table *entry;
1120         struct sockaddr_in *si;
1121 
1122         /*
1123          *      We only understand about IP addresses...
1124          */
1125         
1126         memcpy_fromfs(&r, req, sizeof(r));
1127 
1128         if (r.arp_pa.sa_family != AF_INET)
1129                 return -EPFNOSUPPORT;
1130 
1131         /*
1132          *      Is there an existing entry for this address?
1133          */
1134         
1135         si = (struct sockaddr_in *) &r.arp_pa;
1136         cli();
1137         entry = arp_lookup(si->sin_addr.s_addr,0);
1138 
1139         if (entry == NULL)
1140         {
1141                 sti();
1142                 return -ENXIO;
1143         }
1144 
1145         /*
1146          *      We found it; copy into structure.
1147          */
1148         
1149         memcpy(r.arp_ha.sa_data, &entry->ha, entry->hlen);
1150         r.arp_ha.sa_family = entry->htype;
1151         r.arp_flags = entry->flags;
1152         sti();
1153 
1154         /*
1155          *      Copy the information back
1156          */
1157         
1158         memcpy_tofs(req, &r, sizeof(r));
1159         return 0;
1160 }
1161 
1162 
1163 /*
1164  *      Handle an ARP layer I/O control request.
1165  */
1166 
1167 int arp_ioctl(unsigned int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1168 {
1169         struct arpreq r;
1170         struct sockaddr_in *si;
1171         int err;
1172 
1173         switch(cmd)
1174         {
1175                 case SIOCDARP:
1176                         if (!suser())
1177                                 return -EPERM;
1178                         err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq));
1179                         if(err)
1180                                 return err;
1181                         memcpy_fromfs(&r, arg, sizeof(r));
1182                         if (r.arp_pa.sa_family != AF_INET)
1183                                 return -EPFNOSUPPORT;
1184                         si = (struct sockaddr_in *) &r.arp_pa;
1185                         arp_destroy(si->sin_addr.s_addr, 1);
1186                         return 0;
1187                 case SIOCGARP:
1188                         err = verify_area(VERIFY_WRITE, arg, sizeof(struct arpreq));
1189                         if(err)
1190                                 return err;
1191                         return arp_req_get((struct arpreq *)arg);
1192                 case SIOCSARP:
1193                         if (!suser())
1194                                 return -EPERM;
1195                         err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq));
1196                         if(err)
1197                                 return err;
1198                         return arp_req_set((struct arpreq *)arg);
1199                 default:
1200                         return -EINVAL;
1201         }
1202         /*NOTREACHED*/
1203         return 0;
1204 }
1205 
1206 
1207 /*
1208  *      Called once on startup.
1209  */
1210 
1211 static struct packet_type arp_packet_type =
1212 {
1213         0,      /* Should be: __constant_htons(ETH_P_ARP) - but this _doesn't_ come out constant! */
1214         0,              /* copy */
1215         arp_rcv,
1216         NULL,
1217         NULL
1218 };
1219 
1220 void arp_init (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1221 {
1222         /* Register the packet type */
1223         arp_packet_type.type=htons(ETH_P_ARP);
1224         dev_add_pack(&arp_packet_type);
1225         /* Start with the regular checks for expired arp entries. */
1226         add_timer(&arp_timer);
1227 }
1228 

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