root/net/inet/arp.c

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

DEFINITIONS

This source file includes following definitions.
  1. arp_check_expire
  2. arp_release_entry
  3. arp_send
  4. arp_expire_request
  5. arp_send_q
  6. arp_destroy
  7. arp_rcv
  8. arp_find
  9. arp_get_info
  10. arp_lookup
  11. arp_req_set
  12. arp_req_get
  13. arp_ioctl
  14. arp_init

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

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