root/net/inet/arp.c

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

DEFINITIONS

This source file includes following definitions.
  1. unk_print
  2. eth_aprint
  3. arp_print
  4. arp_send_q
  5. arp_response
  6. arp_lookup
  7. arp_lookup_proxy
  8. arp_destructor
  9. arp_destroy
  10. arp_destroy_maybe
  11. arp_create
  12. arp_rcv
  13. arp_send
  14. arp_find
  15. arp_add
  16. arp_add_broad
  17. arp_queue
  18. arp_get_info
  19. arp_req_set
  20. arp_req_get
  21. arp_req_del
  22. arp_ioctl

   1 /*
   2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3  *              operating system.  INET is implemented using the  BSD Socket
   4  *              interface as the means of communication with the user level.
   5  *
   6  *              This file implements the Address Resolution Protocol (ARP),
   7  *              which is used by TCP/IP to map the IP addresses from a host
   8  *              to a low-level hardware address (like an Ethernet address)
   9  *              which it can use to talk to that host.
  10  *
  11  * NOTE:        This module will be rewritten completely in the near future,
  12  *              because I want it to become a multi-address-family address
  13  *              resolver, like it should be.  It will be put in a separate
  14  *              directory under 'net', being a protocol of its own. -FvK
  15  *
  16  * Version:     @(#)arp.c       1.0.15  05/25/93
  17  *
  18  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  19  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  20  *              Stephen A. Wood, <saw@hallc1.cebaf.gov>
  21  *              Arnt Gulbrandsen, <agulbra@pvv.unit.no>
  22  *
  23  * Fixes:
  24  *              'Mr Linux'      :       arp problems.
  25  *              Alan Cox        :       arp_ioctl now checks memory areas with verify_area.
  26  *              Alan Cox        :       Non IP arp message now only appears with debugging on.
  27  *              Alan Cox        :       arp queue is volatile (may be altered by arp messages while doing sends) 
  28  *                                      Generic queue code is urgently needed!
  29  *              Alan Cox        :       Deleting your own ip addr now gives EINVAL not a printk message.
  30  *              Alan Cox        :       Fix to arp linked list error
  31  *              Alan Cox        :       Ignore broadcast arp (Linus' idea 8-))
  32  *              Alan Cox        :       arp_send memory leak removed
  33  *              Alan Cox        :       generic skbuff code fixes.
  34  *              Alan Cox        :       'Bad Packet' only reported on debugging
  35  *              Alan Cox        :       Proxy arp.
  36  *              Alan Cox        :       skb->link3 maintained by letting the other xmit queue kill the packet.
  37  *              Alan Cox        :       Knows about type 3 devices (AX.25) using an AX.25 protocol ID not the ethernet
  38  *                                      one.
  39  *              Dominik Kubla   :       Better checking
  40  *              Tegge           :       Assorted corrections on cross port stuff
  41  *              Alan Cox        :       ATF_PERM was backwards! - might be useful now (sigh)
  42  *
  43  * To Fix:
  44  *                              :       arp response allocates an skbuff to send. However there is a perfectly
  45  *                                      good spare skbuff the right size about to be freed (the query). Use the
  46  *                                      query for the reply. This avoids an out of memory case _and_ speeds arp
  47  *                                      up.
  48  *                              :       FREE_READ v FREE_WRITE errors. Not critical as loopback arps don't occur
  49  *
  50  *
  51  *              This program is free software; you can redistribute it and/or
  52  *              modify it under the terms of the GNU General Public License
  53  *              as published by the Free Software Foundation; either version
  54  *              2 of the License, or (at your option) any later version.
  55  */
  56 #include <linux/types.h>
  57 #include <linux/string.h>
  58 #include <linux/kernel.h>
  59 #include <linux/sched.h>
  60 #include <linux/config.h>
  61 #include <linux/socket.h>
  62 #include <linux/sockios.h>
  63 #include <linux/errno.h>
  64 #include <linux/if_arp.h>
  65 #include <linux/in.h>
  66 #include <asm/system.h>
  67 #include <asm/segment.h>
  68 #include <stdarg.h>
  69 #include "inet.h"
  70 #include "dev.h"
  71 #include "eth.h"
  72 #include "ip.h"
  73 #include "route.h"
  74 #include "protocol.h"
  75 #include "tcp.h"
  76 #include "skbuff.h"
  77 #include "sock.h"
  78 #include "arp.h"
  79 
  80 
  81 #define ARP_MAX_TRIES   3
  82 
  83 
  84 static char *unk_print(unsigned char *, int);
  85 static char *eth_aprint(unsigned char *, int);
  86 
  87 
  88 static char *arp_cmds[] = {
  89   "0x%04X",
  90   "REQUEST",
  91   "REPLY",
  92   "REVERSE REQUEST",
  93   "REVERSE REPLY",
  94   NULL
  95 };
  96 #define ARP_MAX_CMDS    (sizeof(arp_cmds) / sizeof(arp_cmds[0]))
  97 
  98 static struct {
  99   char  *name;
 100   char  *(*print)(unsigned char *ptr, int len);
 101 } arp_types[] = {
 102   { "0x%04X",                   unk_print       },
 103   { "10 Mbps Ethernet",         eth_aprint      },
 104   { "3 Mbps Ethernet",          eth_aprint      },
 105   { "AX.25",                    unk_print       },
 106   { "Pronet",                   unk_print       },
 107   { "Chaos",                    unk_print       },
 108   { "IEEE 802.2 Ethernet (?)",  eth_aprint      },
 109   { "Arcnet",                   unk_print       },
 110   { "AppleTalk",                unk_print       },
 111   { NULL,                       NULL            }
 112 };
 113 #define ARP_MAX_TYPE    (sizeof(arp_types) / sizeof(arp_types[0]))
 114 
 115 
 116 struct arp_table *arp_tables[ARP_TABLE_SIZE] = {
 117   NULL,
 118 };
 119 
 120 static int arp_proxies=0;       /* So we can avoid the proxy arp 
 121                                    overhead with the usual case of
 122                                    no proxy arps */
 123 
 124 struct sk_buff * volatile arp_q = NULL;
 125 
 126 static struct arp_table *arp_lookup(unsigned long addr);
 127 static struct arp_table *arp_lookup_proxy(unsigned long addr);
 128 
 129 /* Dump the ADDRESS bytes of an unknown hardware type. */
 130 static char *
 131 unk_print(unsigned char *ptr, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133   static char buff[32];
 134   char *bufp = buff;
 135   int i;
 136 
 137   for (i = 0; i < len; i++)
 138         bufp += sprintf(bufp, "%02X ", (*ptr++ & 0377));
 139   return(buff);
 140 }
 141 
 142 
 143 /* Dump the ADDRESS bytes of an Ethernet hardware type. */
 144 static char *
 145 eth_aprint(unsigned char *ptr, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 146 {
 147   if (len != ETH_ALEN) return("");
 148   return(eth_print(ptr));
 149 }
 150 
 151 
 152 /* Dump an ARP packet. Not complete yet for non-Ethernet packets. */
 153 static void
 154 arp_print(struct arphdr *arp)
     /* [previous][next][first][last][top][bottom][index][help] */
 155 {
 156   int len, idx;
 157   unsigned char *ptr;
 158 
 159   if (inet_debug != DBG_ARP) return;
 160 
 161   printk("ARP: ");
 162   if (arp == NULL) {
 163         printk("(null)\n");
 164         return;
 165   }
 166 
 167   /* Print the opcode name. */
 168   len = htons(arp->ar_op);
 169   if (len < ARP_MAX_CMDS) idx = len;
 170     else idx = 0;
 171   printk("op ");
 172   printk(arp_cmds[idx], len);
 173 
 174   /* Print the ARP header. */
 175   len = htons(arp->ar_hrd);
 176   if (len < ARP_MAX_TYPE) idx = len;
 177     else idx = 0;
 178   printk("   hrd = "); printk(arp_types[idx].name, len);
 179   printk("   pro = 0x%04X\n", htons(arp->ar_pro));
 180   printk("   hlen = %d plen = %d\n", arp->ar_hln, arp->ar_pln);
 181 
 182   /*
 183    * Print the variable data.
 184    * When ARP gets redone (after the formal introduction of NET-2),
 185    * this part will be redone.  ARP will then be a multi-family address
 186    * resolver, and the code below will be made more general. -FvK
 187    */
 188   ptr = ((unsigned char *) &arp->ar_op) + sizeof(u_short);
 189   printk("   sender HA = %s ", arp_types[idx].print(ptr, arp->ar_hln));
 190   ptr += arp->ar_hln;
 191   printk("  PA = %s\n", in_ntoa(*(unsigned long *) ptr));
 192   ptr += arp->ar_pln;
 193   printk("   target HA = %s ", arp_types[idx].print(ptr, arp->ar_hln));
 194   ptr += arp->ar_hln;
 195   printk("  PA = %s\n", in_ntoa(*(unsigned long *) ptr));
 196 }
 197 
 198 
 199 /* This will try to retransmit everything on the queue. */
 200 static void
 201 arp_send_q(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 202 {
 203   struct sk_buff *skb;
 204   struct sk_buff *volatile work_q;
 205   cli();
 206   work_q = arp_q;
 207   skb_new_list_head(&work_q);
 208   arp_q = NULL;
 209   sti();
 210   while((skb=skb_dequeue(&work_q))!=NULL)
 211   {
 212         IS_SKB(skb);
 213         skb->magic = 0;
 214         skb->next = NULL;
 215         skb->prev = NULL;
 216 
 217         /* Decrement the 'tries' counter. */
 218         cli();
 219         skb->tries--;
 220         if (skb->tries == 0) {
 221                 /*
 222                  * Grmpf.
 223                  * We have tried ARP_MAX_TRIES to resolve the IP address
 224                  * from this datagram.  This means that the machine does
 225                  * not listen to our ARP requests.  Perhaps someone tur-
 226                  * ned off the thing?
 227                  * In any case, trying further is useless.  So, we kill
 228                  * this packet from the queue.  (grinnik) -FvK
 229                  */
 230                 skb->sk = NULL;
 231                 if(skb->free)
 232                         kfree_skb(skb, FREE_WRITE);
 233                         /* If free was 0, magic is now 0, next is 0 and 
 234                            the write queue will notice and kill */
 235                 sti();
 236                 continue;
 237         }
 238 
 239         /* Can we now complete this packet? */
 240         sti();
 241         if (skb->arp || !skb->dev->rebuild_header(skb+1, skb->dev)) {
 242                 skb->arp  = 1;
 243                 skb->dev->queue_xmit(skb, skb->dev, 0);
 244         } else {
 245                 /* Alas.  Re-queue it... */
 246                 skb->magic = ARP_QUEUE_MAGIC;      
 247                 skb_queue_head(&arp_q,skb);
 248         }
 249   }
 250 }
 251 
 252 
 253 /* Create and send our response to an ARP request. */
 254 static int
 255 arp_response(struct arphdr *arp1, struct device *dev,  int addrtype)
     /* [previous][next][first][last][top][bottom][index][help] */
 256 {
 257   struct arphdr *arp2;
 258   struct sk_buff *skb;
 259   unsigned long src, dst;
 260   unsigned char *ptr1, *ptr2;
 261   int hlen;
 262   struct arp_table *apt = NULL;/* =NULL otherwise the compiler gives warnings */
 263 
 264   /* Decode the source (REQUEST) message. */
 265   ptr1 = ((unsigned char *) &arp1->ar_op) + sizeof(u_short);
 266   src = *((unsigned long *) (ptr1 + arp1->ar_hln));
 267   dst = *((unsigned long *) (ptr1 + (arp1->ar_hln * 2) + arp1->ar_pln));
 268   
 269   if(addrtype!=IS_MYADDR)
 270   {
 271         apt=arp_lookup_proxy(dst);
 272         if(apt==NULL)
 273                 return(1);
 274   }
 275 
 276   /* Get some mem and initialize it for the return trip. */
 277   skb = alloc_skb(sizeof(struct sk_buff) +
 278                 sizeof(struct arphdr) +
 279                 (2 * arp1->ar_hln) + (2 * arp1->ar_pln) +
 280                 dev->hard_header_len, GFP_ATOMIC);
 281   if (skb == NULL) {
 282         printk("ARP: no memory available for ARP REPLY!\n");
 283         return(1);
 284   }
 285 
 286   skb->mem_addr = skb;
 287   skb->len      = sizeof(struct arphdr) + (2 * arp1->ar_hln) + 
 288                   (2 * arp1->ar_pln) + dev->hard_header_len;
 289   skb->mem_len  = sizeof(struct sk_buff) + skb->len;
 290   hlen = dev->hard_header((unsigned char *)(skb+1), dev,
 291                          ETH_P_ARP, src, dst, skb->len);
 292   if (hlen < 0) {
 293         printk("ARP: cannot create HW frame header for REPLY !\n");
 294         kfree_skb(skb, FREE_WRITE);
 295         return(1);
 296   }
 297 
 298   /*
 299    * Fill in the ARP REPLY packet.
 300    * This looks ugly, but we have to deal with the variable-length
 301    * ARP packets and such.  It is not as bad as it looks- FvK
 302    */
 303   arp2 = (struct arphdr *) ((unsigned char *) (skb+1) + hlen);
 304   ptr2 = ((unsigned char *) &arp2->ar_op) + sizeof(u_short);
 305   arp2->ar_hrd = arp1->ar_hrd;
 306   arp2->ar_pro = arp1->ar_pro;
 307   arp2->ar_hln = arp1->ar_hln;
 308   arp2->ar_pln = arp1->ar_pln;
 309   arp2->ar_op = htons(ARPOP_REPLY);
 310   if(addrtype==IS_MYADDR)
 311           memcpy(ptr2, dev->dev_addr, arp2->ar_hln);
 312   else          /* Proxy arp, so pull from the table */
 313           memcpy(ptr2, apt->ha, arp2->ar_hln);
 314   ptr2 += arp2->ar_hln;
 315   memcpy(ptr2, ptr1 + (arp1->ar_hln * 2) + arp1->ar_pln, arp2->ar_pln);
 316   ptr2 += arp2->ar_pln;
 317   memcpy(ptr2, ptr1, arp2->ar_hln);
 318   ptr2 += arp2->ar_hln;
 319   memcpy(ptr2, ptr1 + arp1->ar_hln, arp2->ar_pln);
 320 
 321   skb->free = 1;
 322   skb->arp = 1;
 323   skb->sk = NULL;
 324   skb->next = NULL;
 325 
 326   DPRINTF((DBG_ARP, ">>"));
 327   arp_print(arp2);
 328 
 329   /* Queue the packet for transmission. */
 330   dev->queue_xmit(skb, dev, 0);
 331   return(0);
 332 }
 333 
 334 
 335 /* This will find an entry in the ARP table by looking at the IP address. */
 336 static struct arp_table *
 337 arp_lookup(unsigned long paddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 338 {
 339   struct arp_table *apt;
 340   unsigned long hash;
 341 
 342   DPRINTF((DBG_ARP, "ARP: lookup(%s)\n", in_ntoa(paddr)));
 343 
 344   /* We don't want to ARP ourselves. */
 345   if (chk_addr(paddr) == IS_MYADDR) {
 346         printk("ARP: ARPing my own IP address %s !\n", in_ntoa(paddr));
 347         return(NULL);
 348   }
 349 
 350   /* Loop through the table for the desired address. */
 351   hash = htonl(paddr) & (ARP_TABLE_SIZE - 1);
 352   cli();
 353   apt = arp_tables[hash];
 354   while(apt != NULL) {
 355         if (apt->ip == paddr) {
 356                 sti();
 357                 return(apt);
 358         }
 359         apt = apt->next;
 360   }
 361   sti();
 362   return(NULL);
 363 }
 364 
 365 
 366 /* This will find a proxy in the ARP table by looking at the IP address. */
 367 static struct arp_table *arp_lookup_proxy(unsigned long paddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 368 {
 369   struct arp_table *apt;
 370   unsigned long hash;
 371 
 372   DPRINTF((DBG_ARP, "ARP: lookup proxy(%s)\n", in_ntoa(paddr)));
 373 
 374   /* Loop through the table for the desired address. */
 375   hash = htonl(paddr) & (ARP_TABLE_SIZE - 1);
 376   cli();
 377   apt = arp_tables[hash];
 378   while(apt != NULL) {
 379         if (apt->ip == paddr && (apt->flags & ATF_PUBL) ) {
 380                 sti();
 381                 return(apt);
 382         }
 383         apt = apt->next;
 384   }
 385   sti();
 386   return(NULL);
 387 }
 388 
 389 
 390 /* Delete an ARP mapping entry in the cache. */
 391 void
 392 arp_destructor(unsigned long paddr, int force)
     /* [previous][next][first][last][top][bottom][index][help] */
 393 {
 394   struct arp_table *apt;
 395   struct arp_table **lapt;
 396   unsigned long hash;
 397 
 398   DPRINTF((DBG_ARP, "ARP: destroy(%s)\n", in_ntoa(paddr)));
 399 
 400   /* We cannot destroy our own ARP entry. */
 401   if (chk_addr(paddr) == IS_MYADDR) {
 402         DPRINTF((DBG_ARP, "ARP: Destroying my own IP address %s !\n",
 403                                                         in_ntoa(paddr)));
 404         return;
 405   }
 406   hash = htonl(paddr) & (ARP_TABLE_SIZE - 1);
 407 
 408   cli();
 409   lapt = &arp_tables[hash];
 410   while ((apt = *lapt) != NULL) {
 411         if (apt->ip == paddr) {
 412                 if((apt->flags&ATF_PERM) && !force)
 413                         return;
 414                 *lapt = apt->next;
 415                 if(apt->flags&ATF_PUBL)
 416                         arp_proxies--;                  
 417                 kfree_s(apt, sizeof(struct arp_table));
 418                 sti();
 419                 return;
 420         }
 421         lapt = &apt->next;
 422   }
 423   sti();
 424 }
 425 
 426 /*
 427  *      Kill an entry - eg for ioctl()
 428  */
 429 
 430 void arp_destroy(unsigned long paddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 431 {       
 432         arp_destructor(paddr,1);
 433 }
 434 
 435 /*
 436  *      Delete a possibly invalid entry (see timer.c)
 437  */
 438 
 439 void arp_destroy_maybe(unsigned long paddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 440 {
 441         arp_destructor(paddr,0);
 442 }
 443 
 444 /* Create an ARP entry.  The caller should check for duplicates! */
 445 static struct arp_table *
 446 arp_create(unsigned long paddr, unsigned char *addr, int hlen, int htype)
     /* [previous][next][first][last][top][bottom][index][help] */
 447 {
 448   struct arp_table *apt;
 449   unsigned long hash;
 450 
 451   DPRINTF((DBG_ARP, "ARP: create(%s, ", in_ntoa(paddr)));
 452   DPRINTF((DBG_ARP, "%s, ", eth_print(addr)));
 453   DPRINTF((DBG_ARP, "%d, %d)\n", hlen, htype));
 454 
 455   apt = (struct arp_table *) kmalloc(sizeof(struct arp_table), GFP_ATOMIC);
 456   if (apt == NULL) {
 457         printk("ARP: no memory available for new ARP entry!\n");
 458         return(NULL);
 459   }
 460 
 461   /* Fill in the allocated ARP cache entry. */
 462   hash = htonl(paddr) & (ARP_TABLE_SIZE - 1);
 463   apt->ip = paddr;
 464   apt->hlen = hlen;
 465   apt->htype = htype;
 466   apt->flags = (ATF_INUSE | ATF_COM);           /* USED and COMPLETED entry */
 467   memcpy(apt->ha, addr, hlen);
 468   apt->last_used = jiffies;
 469   cli();
 470   apt->next = arp_tables[hash];
 471   arp_tables[hash] = apt;
 472   sti();
 473   return(apt);
 474 }
 475 
 476 
 477 /*
 478  * An ARP REQUEST packet has arrived.
 479  * We try to be smart here, and fetch the data of the sender of the
 480  * packet- we might need it later, so fetching it now can save us a
 481  * broadcast later.
 482  * Then, if the packet was meant for us (i.e. the TARGET address was
 483  * one of our own IP addresses), we set up and send out an ARP REPLY
 484  * packet to the sender.
 485  */
 486 int
 487 arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
 488 {
 489   struct arphdr *arp;
 490   struct arp_table *tbl;
 491   unsigned long src, dst;
 492   unsigned char *ptr;
 493   int ret;
 494   int addr_hint;
 495 
 496   DPRINTF((DBG_ARP, "<<\n"));
 497   arp = skb->h.arp;
 498   arp_print(arp);
 499 
 500   /* If this test doesn't pass, its not IP. Might be DECNET or friends */
 501   if (arp->ar_hln != dev->addr_len || dev->type != NET16(arp->ar_hrd)) 
 502   {
 503         DPRINTF((DBG_ARP,"ARP: Bad packet received on device \"%s\" !\n", dev->name));
 504         kfree_skb(skb, FREE_READ);
 505         return(0);
 506   }
 507 
 508   /* For now we will only deal with IP addresses. */
 509   if (((arp->ar_pro != NET16(0x00CC) && dev->type==3) || (arp->ar_pro != NET16(ETH_P_IP) && dev->type!=3) ) || arp->ar_pln != 4) 
 510   {
 511         if (arp->ar_op != NET16(ARPOP_REQUEST))
 512                 DPRINTF((DBG_ARP,"ARP: Non-IP request on device \"%s\" !\n", dev->name));
 513         kfree_skb(skb, FREE_READ);
 514         return(0);
 515   }
 516 
 517   /*
 518    * As said before, we try to be smart by using the
 519    * info already present in the packet: the sender's
 520    * IP and hardware address.
 521    */
 522   ptr = ((unsigned char *) &arp->ar_op) + sizeof(u_short);
 523   memcpy(&src, ptr + arp->ar_hln, arp->ar_pln);
 524   tbl = arp_lookup(src);
 525   if (tbl != NULL) {
 526         DPRINTF((DBG_ARP, "ARP: udating entry for %s\n", in_ntoa(src)));
 527         memcpy(tbl->ha, ptr, arp->ar_hln);
 528         tbl->hlen = arp->ar_hln;
 529         tbl->flags |= ATF_COM;
 530         tbl->last_used = jiffies;
 531   } else {
 532         memcpy(&dst, ptr + (arp->ar_hln * 2) + arp->ar_pln, arp->ar_pln);
 533         if (chk_addr(dst) != IS_MYADDR) {
 534                 kfree_skb(skb, FREE_READ);
 535                 return(0);
 536         } else {
 537                 tbl = arp_create(src, ptr, arp->ar_hln, arp->ar_hrd);
 538                 if (tbl == NULL) {
 539                         kfree_skb(skb, FREE_READ);
 540                         return(0);
 541                 }
 542         }
 543   }
 544 
 545   /*
 546    * Since we updated the ARP cache, we might have enough
 547    * information to send out some previously queued IP
 548    * datagrams....
 549    */
 550   arp_send_q();
 551 
 552   /*
 553    * OK, we used that part of the info.  Now check if the
 554    * request was an ARP REQUEST for one of our own addresses..
 555    */
 556   if (arp->ar_op != NET16(ARPOP_REQUEST)) {
 557         kfree_skb(skb, FREE_READ);
 558         return(0);
 559   }
 560 
 561 /*
 562  * A broadcast arp, ignore it
 563  */
 564 
 565   if(chk_addr(dst)==IS_BROADCAST)
 566   {
 567         kfree_skb(skb, FREE_READ);
 568         return 0;
 569   }
 570   
 571   memcpy(&dst, ptr + (arp->ar_hln * 2) + arp->ar_pln, arp->ar_pln);
 572   if ((addr_hint=chk_addr(dst)) != IS_MYADDR && arp_proxies==0) {
 573         DPRINTF((DBG_ARP, "ARP: request was not for me!\n"));
 574         kfree_skb(skb, FREE_READ);
 575         return(0);
 576   }
 577 
 578   /*
 579    * Yes, it is for us.
 580    * Allocate, fill in and send an ARP REPLY packet.
 581    */
 582   ret = arp_response(arp, dev, addr_hint);
 583   kfree_skb(skb, FREE_READ);
 584   return(ret);
 585 }
 586 
 587 
 588 /* Create and send an ARP REQUEST packet. */
 589 void
 590 arp_send(unsigned long paddr, struct device *dev, unsigned long saddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 591 {
 592   struct sk_buff *skb;
 593   struct arphdr *arp;
 594   unsigned char *ptr;
 595   int tmp;
 596 
 597   DPRINTF((DBG_ARP, "ARP: send(paddr=%s, ", in_ntoa(paddr)));
 598   DPRINTF((DBG_ARP, "dev=%s, ", dev->name));
 599   DPRINTF((DBG_ARP, "saddr=%s)\n", in_ntoa(saddr)));
 600 
 601   skb = alloc_skb(sizeof(struct sk_buff) +
 602                 sizeof(struct arphdr) + (2 * dev->addr_len) +
 603                 dev->hard_header_len +
 604                 (2 * 4 /* arp->plen */), GFP_ATOMIC);
 605   if (skb == NULL) {
 606         printk("ARP: No memory available for REQUEST %s\n", in_ntoa(paddr));
 607         return;
 608   }
 609   
 610   /* Fill in the request. */
 611   skb->sk = NULL;
 612   skb->mem_addr = skb;
 613   skb->len = sizeof(struct arphdr) +
 614              dev->hard_header_len + (2 * dev->addr_len) + 8;
 615   skb->mem_len = sizeof(struct sk_buff) + skb->len;
 616   skb->arp = 1;
 617   skb->dev = dev;
 618   skb->next = NULL;
 619   skb->free = 1;
 620   tmp = dev->hard_header((unsigned char *)(skb+1), dev,
 621                           ETH_P_ARP, 0, saddr, skb->len);
 622   if (tmp < 0) {
 623         kfree_skb(skb,FREE_WRITE);
 624         return;
 625   }
 626   arp = (struct arphdr *) ((unsigned char *) (skb+1) + tmp);
 627   arp->ar_hrd = htons(dev->type);
 628   if(dev->type!=3)      /* AX.25 */
 629         arp->ar_pro = htons(ETH_P_IP);
 630   else
 631         arp->ar_pro = htons(0xCC);
 632   arp->ar_hln = dev->addr_len;
 633   arp->ar_pln = 4;
 634   arp->ar_op = htons(ARPOP_REQUEST);
 635 
 636   ptr = ((unsigned char *) &arp->ar_op) + sizeof(u_short);
 637   memcpy(ptr, dev->dev_addr, arp->ar_hln);
 638   ptr += arp->ar_hln;
 639   memcpy(ptr, &saddr, arp->ar_pln);
 640   ptr += arp->ar_pln;
 641   /*memcpy(ptr, dev->broadcast, arp->ar_hln);*/
 642   memset(ptr,0,arp->ar_hln);
 643   ptr += arp->ar_hln;
 644   memcpy(ptr, &paddr, arp->ar_pln);
 645 
 646   DPRINTF((DBG_ARP, ">>\n"));
 647   arp_print(arp);
 648   dev->queue_xmit(skb, dev, 0);
 649 }
 650 
 651 
 652 /* Find an ARP mapping in the cache. If not found, post a REQUEST. */
 653 int
 654 arp_find(unsigned char *haddr, unsigned long paddr, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 655            unsigned long saddr)
 656 {
 657   struct arp_table *apt;
 658 
 659   DPRINTF((DBG_ARP, "ARP: find(haddr=%s, ", eth_print(haddr)));
 660   DPRINTF((DBG_ARP, "paddr=%s, ", in_ntoa(paddr)));
 661   DPRINTF((DBG_ARP, "dev=%s, saddr=%s)\n", dev->name, in_ntoa(saddr)));
 662 
 663   switch(chk_addr(paddr)) {
 664         case IS_MYADDR:
 665                 memcpy(haddr, dev->dev_addr, dev->addr_len);
 666                 return(0);
 667         case IS_BROADCAST:
 668                 memcpy(haddr, dev->broadcast, dev->addr_len);
 669                 return(0);
 670   }
 671                 
 672   apt = arp_lookup(paddr);
 673   if (apt != NULL) {
 674         /*
 675          * Make sure it's not too old. If it is too old, we will
 676          * just pretend we did not find it, and then arp_send will
 677          * verify the address for us.
 678          */
 679         if ((apt->flags & ATF_PERM) ||
 680             (apt->last_used < jiffies+ARP_TIMEOUT && apt->hlen != 0)) {
 681                 apt->last_used = jiffies;
 682                 memcpy(haddr, apt->ha, dev->addr_len);
 683                 return(0);
 684         } else {
 685                 DPRINTF((DBG_ARP, "ARP: find: found expired entry for %s\n",
 686                                                         in_ntoa(apt->ip)));
 687         }
 688   }
 689 
 690   /*
 691    * This assume haddr are at least 4 bytes.
 692    * If this isn't true we can use a lookup table, one for every dev.
 693    * NOTE: this bit of code still looks fishy to me- FvK
 694    */
 695   *(unsigned long *)haddr = paddr;
 696 
 697   /* If we didn't find an entry, we will try to send an ARP packet. */
 698   arp_send(paddr, dev, saddr);
 699 
 700   return(1);
 701 }
 702 
 703 
 704 /* Add an entry to the ARP cache.  Check for dupes! */
 705 void
 706 arp_add(unsigned long addr, unsigned char *haddr, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 707 {
 708   struct arp_table *apt;
 709 
 710   DPRINTF((DBG_ARP, "ARP: add(%s, ", in_ntoa(addr)));
 711   DPRINTF((DBG_ARP, "%s, ", eth_print(haddr)));
 712   DPRINTF((DBG_ARP, "%d, %d)\n", dev->hard_header_len, dev->type));
 713 
 714   /* This is probably a good check... */
 715   if (addr == 0) {
 716         printk("ARP: add: will not add entry for 0.0.0.0 !\n");
 717         return;
 718   }
 719 
 720   /* First see if the address is already in the table. */
 721   apt = arp_lookup(addr);
 722   if (apt != NULL) {
 723         DPRINTF((DBG_ARP, "ARP: updating entry for %s\n", in_ntoa(addr)));
 724         apt->last_used = jiffies;
 725         memcpy(apt->ha, haddr , dev->addr_len);
 726         return;
 727   }
 728   arp_create(addr, haddr, dev->addr_len, dev->type);
 729 }
 730 
 731 
 732 /* Create an ARP entry for a device's broadcast address. */
 733 void
 734 arp_add_broad(unsigned long addr, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 735 {
 736   struct arp_table *apt;
 737 
 738   arp_add(addr, dev->broadcast, dev);
 739   apt = arp_lookup(addr);
 740   if (apt != NULL) {
 741         apt->flags |= ATF_PERM;
 742   }
 743 }
 744 
 745 
 746 /* Queue an IP packet, while waiting for the ARP reply packet. */
 747 void
 748 arp_queue(struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 749 {
 750   cli();
 751   skb->tries = ARP_MAX_TRIES;
 752 
 753   if (skb->next != NULL) {
 754         sti();
 755         printk("ARP: arp_queue skb already on queue magic=%X.\n", skb->magic);
 756         return;
 757   }
 758   skb_queue_tail(&arp_q,skb);
 759   skb->magic = ARP_QUEUE_MAGIC;
 760   sti();
 761 }
 762 
 763 
 764 /*
 765  * Write the contents of the ARP cache to a PROCfs file.
 766  * This is not by long perfect, as the internal ARP table doesn't
 767  * have all the info we would like to have.  Oh well, it works for
 768  * now, eh? - FvK
 769  * Also note, that due to space limits, we cannot generate more than
 770  * 4Kbyte worth of data.  This usually is enough, but I have seen
 771  * machines die from under me because of a *very* large ARP cache.
 772  * This can be simply tested by doing:
 773  *
 774  *      # ping 255.255.255.255
 775  *      # arp -a
 776  *
 777  * Perhaps we should redo PROCfs to handle larger buffers?  Michael?
 778  */
 779 int
 780 arp_get_info(char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 781 {
 782   struct arpreq *req;
 783   struct arp_table *apt;
 784   int i;
 785   char *pos;
 786 
 787   /* Loop over the ARP table and copy structures to the buffer. */
 788   pos = buffer;
 789   i = 0;
 790   for (i = 0; i < ARP_TABLE_SIZE; i++) {
 791         cli();
 792         apt = arp_tables[i];
 793         sti();
 794         while (apt != NULL) {
 795                 if (pos < (buffer + 4000)) {
 796                         req = (struct arpreq *) pos;
 797                         memset((char *) req, 0, sizeof(struct arpreq));
 798                         req->arp_pa.sa_family = AF_INET;
 799                         memcpy((char *) req->arp_pa.sa_data, (char *) &apt->ip, 4);
 800                                 req->arp_ha.sa_family = apt->htype;
 801                         memcpy((char *) req->arp_ha.sa_data,
 802                                 (char *) &apt->ha, apt->hlen);
 803                 }
 804                 pos += sizeof(struct arpreq);
 805                 cli();
 806                 apt = apt->next;
 807                 sti();
 808         }
 809   }
 810   return(pos - buffer);
 811 }
 812 
 813 
 814 /* Set (create) an ARP cache entry. */
 815 static int
 816 arp_req_set(struct arpreq *req)
     /* [previous][next][first][last][top][bottom][index][help] */
 817 {
 818   struct arpreq r;
 819   struct arp_table *apt;
 820   struct sockaddr_in *si;
 821   int htype, hlen;
 822 
 823   /* We only understand about IP addresses... */
 824   memcpy_fromfs(&r, req, sizeof(r));
 825   if (r.arp_pa.sa_family != AF_INET) return(-EPFNOSUPPORT);
 826 
 827   /*
 828    * Find out about the hardware type.
 829    * We have to be compatible with BSD UNIX, so we have to
 830    * assume that a "not set" value (i.e. 0) means Ethernet.
 831    */
 832   si = (struct sockaddr_in *) &r.arp_pa;
 833   switch(r.arp_ha.sa_family) {
 834         case 0:
 835         case ARPHRD_ETHER:
 836                 htype = ARPHRD_ETHER;
 837                 hlen = ETH_ALEN;
 838                 break;
 839                 case ARPHRD_AX25:
 840                         htype = ARPHRD_AX25;
 841                         hlen = 7;
 842                         break;
 843                 
 844         default:
 845                 return(-EPFNOSUPPORT);
 846   }
 847 
 848   /* Is there an existing entry for this address? */
 849   if (si->sin_addr.s_addr == 0) {
 850         printk("ARP: SETARP: requested PA is 0.0.0.0 !\n");
 851         return(-EINVAL);
 852   }
 853   apt = arp_lookup(si->sin_addr.s_addr);
 854   if (apt == NULL) {
 855         apt = arp_create(si->sin_addr.s_addr,
 856                 (unsigned char *) r.arp_ha.sa_data, hlen, htype);
 857         if (apt == NULL) return(-ENOMEM);
 858   }
 859 
 860   /* We now have a pointer to an ARP entry.  Update it! */
 861   memcpy((char *) &apt->ha, (char *) &r.arp_ha.sa_data, hlen);
 862   apt->last_used = jiffies;
 863   apt->flags = r.arp_flags;
 864   if(apt->flags&ATF_PUBL)
 865         arp_proxies++;          /* Count proxy arps so we know if to use it */
 866 
 867   return(0);
 868 }
 869 
 870 
 871 /* Get an ARP cache entry. */
 872 static int
 873 arp_req_get(struct arpreq *req)
     /* [previous][next][first][last][top][bottom][index][help] */
 874 {
 875   struct arpreq r;
 876   struct arp_table *apt;
 877   struct sockaddr_in *si;
 878 
 879   /* We only understand about IP addresses... */
 880   memcpy_fromfs(&r, req, sizeof(r));
 881   if (r.arp_pa.sa_family != AF_INET) return(-EPFNOSUPPORT);
 882 
 883   /* Is there an existing entry for this address? */
 884   si = (struct sockaddr_in *) &r.arp_pa;
 885   apt = arp_lookup(si->sin_addr.s_addr);
 886   if (apt == NULL) return(-ENXIO);
 887 
 888   /* We found it; copy into structure. */
 889   memcpy((char *) r.arp_ha.sa_data, (char *) &apt->ha, apt->hlen);
 890   r.arp_ha.sa_family = apt->htype;
 891 
 892   /* Copy the information back */
 893   memcpy_tofs(req, &r, sizeof(r));
 894   return(0);
 895 }
 896 
 897 
 898 /* Delete an ARP cache entry. */
 899 static int
 900 arp_req_del(struct arpreq *req)
     /* [previous][next][first][last][top][bottom][index][help] */
 901 {
 902   struct arpreq r;
 903   struct sockaddr_in *si;
 904 
 905   /* We only understand about IP addresses... */
 906   memcpy_fromfs(&r, req, sizeof(r));
 907   if (r.arp_pa.sa_family != AF_INET) return(-EPFNOSUPPORT);
 908 
 909   si = (struct sockaddr_in *) &r.arp_pa;
 910   
 911   /* The system cope with this but splats up a nasty kernel message 
 912      We trap it beforehand and tell the user off */
 913   if(chk_addr(si->sin_addr.s_addr)==IS_MYADDR)
 914         return -EINVAL;
 915         
 916   arp_destroy(si->sin_addr.s_addr);
 917 
 918   return(0);
 919 }
 920 
 921 
 922 /* Handle an ARP layer I/O control request. */
 923 int
 924 arp_ioctl(unsigned int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 925 {
 926   int err;
 927   switch(cmd) {
 928         case DDIOCSDBG:
 929                 return(dbg_ioctl(arg, DBG_ARP));
 930         case SIOCDARP:
 931                 if (!suser()) return(-EPERM);
 932                 err=verify_area(VERIFY_READ,arg,sizeof(struct arpreq));
 933                 if(err)
 934                         return err;
 935                 return(arp_req_del((struct arpreq *)arg));
 936         case SIOCGARP:
 937                 err=verify_area(VERIFY_WRITE,arg,sizeof(struct arpreq));
 938                 if(err)
 939                         return err;
 940                 return(arp_req_get((struct arpreq *)arg));
 941         case SIOCSARP:
 942                 if (!suser()) return(-EPERM);
 943                 err=verify_area(VERIFY_READ,arg,sizeof(struct arpreq));
 944                 if(err)
 945                         return err;
 946                 return(arp_req_set((struct arpreq *)arg));
 947         default:
 948                 return(-EINVAL);
 949   }
 950   /*NOTREACHED*/
 951   return(0);
 952 }

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