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->data, 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(skb->data, dev, ETH_P_ARP, src, dst, skb->len);
 291   if (hlen < 0) {
 292         printk("ARP: cannot create HW frame header for REPLY !\n");
 293         kfree_skb(skb, FREE_WRITE);
 294         return(1);
 295   }
 296 
 297   /*
 298    * Fill in the ARP REPLY packet.
 299    * This looks ugly, but we have to deal with the variable-length
 300    * ARP packets and such.  It is not as bad as it looks- FvK
 301    */
 302   arp2 = (struct arphdr *) (skb->data + hlen);
 303   ptr2 = ((unsigned char *) &arp2->ar_op) + sizeof(u_short);
 304   arp2->ar_hrd = arp1->ar_hrd;
 305   arp2->ar_pro = arp1->ar_pro;
 306   arp2->ar_hln = arp1->ar_hln;
 307   arp2->ar_pln = arp1->ar_pln;
 308   arp2->ar_op = htons(ARPOP_REPLY);
 309   if(addrtype==IS_MYADDR)
 310           memcpy(ptr2, dev->dev_addr, arp2->ar_hln);
 311   else          /* Proxy arp, so pull from the table */
 312           memcpy(ptr2, apt->ha, arp2->ar_hln);
 313   ptr2 += arp2->ar_hln;
 314   memcpy(ptr2, ptr1 + (arp1->ar_hln * 2) + arp1->ar_pln, arp2->ar_pln);
 315   ptr2 += arp2->ar_pln;
 316   memcpy(ptr2, ptr1, arp2->ar_hln);
 317   ptr2 += arp2->ar_hln;
 318   memcpy(ptr2, ptr1 + arp1->ar_hln, arp2->ar_pln);
 319 
 320   skb->free = 1;
 321   skb->arp = 1;
 322   skb->sk = NULL;
 323   skb->next = NULL;
 324 
 325   DPRINTF((DBG_ARP, ">>"));
 326   arp_print(arp2);
 327 
 328   /* Queue the packet for transmission. */
 329   dev->queue_xmit(skb, dev, 0);
 330   return(0);
 331 }
 332 
 333 
 334 /* This will find an entry in the ARP table by looking at the IP address. */
 335 static struct arp_table *
 336 arp_lookup(unsigned long paddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 337 {
 338   struct arp_table *apt;
 339   unsigned long hash;
 340 
 341   DPRINTF((DBG_ARP, "ARP: lookup(%s)\n", in_ntoa(paddr)));
 342 
 343   /* We don't want to ARP ourselves. */
 344   if (chk_addr(paddr) == IS_MYADDR) {
 345         printk("ARP: ARPing my own IP address %s !\n", in_ntoa(paddr));
 346         return(NULL);
 347   }
 348 
 349   /* Loop through the table for the desired address. */
 350   hash = htonl(paddr) & (ARP_TABLE_SIZE - 1);
 351   cli();
 352   apt = arp_tables[hash];
 353   while(apt != NULL) {
 354         if (apt->ip == paddr) {
 355                 sti();
 356                 return(apt);
 357         }
 358         apt = apt->next;
 359   }
 360   sti();
 361   return(NULL);
 362 }
 363 
 364 
 365 /* This will find a proxy in the ARP table by looking at the IP address. */
 366 static struct arp_table *arp_lookup_proxy(unsigned long paddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 367 {
 368   struct arp_table *apt;
 369   unsigned long hash;
 370 
 371   DPRINTF((DBG_ARP, "ARP: lookup proxy(%s)\n", in_ntoa(paddr)));
 372 
 373   /* Loop through the table for the desired address. */
 374   hash = htonl(paddr) & (ARP_TABLE_SIZE - 1);
 375   cli();
 376   apt = arp_tables[hash];
 377   while(apt != NULL) {
 378         if (apt->ip == paddr && (apt->flags & ATF_PUBL) ) {
 379                 sti();
 380                 return(apt);
 381         }
 382         apt = apt->next;
 383   }
 384   sti();
 385   return(NULL);
 386 }
 387 
 388 
 389 /* Delete an ARP mapping entry in the cache. */
 390 void
 391 arp_destructor(unsigned long paddr, int force)
     /* [previous][next][first][last][top][bottom][index][help] */
 392 {
 393   struct arp_table *apt;
 394   struct arp_table **lapt;
 395   unsigned long hash;
 396 
 397   DPRINTF((DBG_ARP, "ARP: destroy(%s)\n", in_ntoa(paddr)));
 398 
 399   /* We cannot destroy our own ARP entry. */
 400   if (chk_addr(paddr) == IS_MYADDR) {
 401         DPRINTF((DBG_ARP, "ARP: Destroying my own IP address %s !\n",
 402                                                         in_ntoa(paddr)));
 403         return;
 404   }
 405   hash = htonl(paddr) & (ARP_TABLE_SIZE - 1);
 406 
 407   cli();
 408   lapt = &arp_tables[hash];
 409   while ((apt = *lapt) != NULL) {
 410         if (apt->ip == paddr) {
 411                 if((apt->flags&ATF_PERM) && !force)
 412                         return;
 413                 *lapt = apt->next;
 414                 if(apt->flags&ATF_PUBL)
 415                         arp_proxies--;                  
 416                 kfree_s(apt, sizeof(struct arp_table));
 417                 sti();
 418                 return;
 419         }
 420         lapt = &apt->next;
 421   }
 422   sti();
 423 }
 424 
 425 /*
 426  *      Kill an entry - eg for ioctl()
 427  */
 428 
 429 void arp_destroy(unsigned long paddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 430 {       
 431         arp_destructor(paddr,1);
 432 }
 433 
 434 /*
 435  *      Delete a possibly invalid entry (see timer.c)
 436  */
 437 
 438 void arp_destroy_maybe(unsigned long paddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 439 {
 440         arp_destructor(paddr,0);
 441 }
 442 
 443 /* Create an ARP entry.  The caller should check for duplicates! */
 444 static struct arp_table *
 445 arp_create(unsigned long paddr, unsigned char *addr, int hlen, int htype)
     /* [previous][next][first][last][top][bottom][index][help] */
 446 {
 447   struct arp_table *apt;
 448   unsigned long hash;
 449 
 450   DPRINTF((DBG_ARP, "ARP: create(%s, ", in_ntoa(paddr)));
 451   DPRINTF((DBG_ARP, "%s, ", eth_print(addr)));
 452   DPRINTF((DBG_ARP, "%d, %d)\n", hlen, htype));
 453 
 454   apt = (struct arp_table *) kmalloc(sizeof(struct arp_table), GFP_ATOMIC);
 455   if (apt == NULL) {
 456         printk("ARP: no memory available for new ARP entry!\n");
 457         return(NULL);
 458   }
 459 
 460   /* Fill in the allocated ARP cache entry. */
 461   hash = htonl(paddr) & (ARP_TABLE_SIZE - 1);
 462   apt->ip = paddr;
 463   apt->hlen = hlen;
 464   apt->htype = htype;
 465   apt->flags = (ATF_INUSE | ATF_COM);           /* USED and COMPLETED entry */
 466   memcpy(apt->ha, addr, hlen);
 467   apt->last_used = jiffies;
 468   cli();
 469   apt->next = arp_tables[hash];
 470   arp_tables[hash] = apt;
 471   sti();
 472   return(apt);
 473 }
 474 
 475 
 476 /*
 477  * An ARP REQUEST packet has arrived.
 478  * We try to be smart here, and fetch the data of the sender of the
 479  * packet- we might need it later, so fetching it now can save us a
 480  * broadcast later.
 481  * Then, if the packet was meant for us (i.e. the TARGET address was
 482  * one of our own IP addresses), we set up and send out an ARP REPLY
 483  * packet to the sender.
 484  */
 485 int
 486 arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
 487 {
 488   struct arphdr *arp;
 489   struct arp_table *tbl;
 490   unsigned long src, dst;
 491   unsigned char *ptr;
 492   int ret;
 493   int addr_hint;
 494 
 495   DPRINTF((DBG_ARP, "<<\n"));
 496   arp = skb->h.arp;
 497   arp_print(arp);
 498 
 499   /* If this test doesn't pass, its not IP. Might be DECNET or friends */
 500   if (arp->ar_hln != dev->addr_len || dev->type != NET16(arp->ar_hrd)) 
 501   {
 502         DPRINTF((DBG_ARP,"ARP: Bad packet received on device \"%s\" !\n", dev->name));
 503         kfree_skb(skb, FREE_READ);
 504         return(0);
 505   }
 506 
 507   /* For now we will only deal with IP addresses. */
 508   if (((arp->ar_pro != NET16(0x00CC) && dev->type==3) || (arp->ar_pro != NET16(ETH_P_IP) && dev->type!=3) ) || arp->ar_pln != 4) 
 509   {
 510         if (arp->ar_op != NET16(ARPOP_REQUEST))
 511                 DPRINTF((DBG_ARP,"ARP: Non-IP request on device \"%s\" !\n", dev->name));
 512         kfree_skb(skb, FREE_READ);
 513         return(0);
 514   }
 515 
 516   /*
 517    * As said before, we try to be smart by using the
 518    * info already present in the packet: the sender's
 519    * IP and hardware address.
 520    */
 521   ptr = ((unsigned char *) &arp->ar_op) + sizeof(u_short);
 522   memcpy(&src, ptr + arp->ar_hln, arp->ar_pln);
 523   tbl = arp_lookup(src);
 524   if (tbl != NULL) {
 525         DPRINTF((DBG_ARP, "ARP: udating entry for %s\n", in_ntoa(src)));
 526         memcpy(tbl->ha, ptr, arp->ar_hln);
 527         tbl->hlen = arp->ar_hln;
 528         tbl->flags |= ATF_COM;
 529         tbl->last_used = jiffies;
 530   } else {
 531         memcpy(&dst, ptr + (arp->ar_hln * 2) + arp->ar_pln, arp->ar_pln);
 532         if (chk_addr(dst) != IS_MYADDR && arp_proxies == 0) {
 533                 kfree_skb(skb, FREE_READ);
 534                 return(0);
 535         } else {
 536                 tbl = arp_create(src, ptr, arp->ar_hln, arp->ar_hrd);
 537                 if (tbl == NULL) {
 538                         kfree_skb(skb, FREE_READ);
 539                         return(0);
 540                 }
 541         }
 542   }
 543 
 544   /*
 545    * Since we updated the ARP cache, we might have enough
 546    * information to send out some previously queued IP
 547    * datagrams....
 548    */
 549   arp_send_q();
 550 
 551   /*
 552    * OK, we used that part of the info.  Now check if the
 553    * request was an ARP REQUEST for one of our own addresses..
 554    */
 555   if (arp->ar_op != NET16(ARPOP_REQUEST)) {
 556         kfree_skb(skb, FREE_READ);
 557         return(0);
 558   }
 559 
 560 /*
 561  * A broadcast arp, ignore it
 562  */
 563 
 564   if(chk_addr(dst)==IS_BROADCAST)
 565   {
 566         kfree_skb(skb, FREE_READ);
 567         return 0;
 568   }
 569   
 570   memcpy(&dst, ptr + (arp->ar_hln * 2) + arp->ar_pln, arp->ar_pln);
 571   if ((addr_hint=chk_addr(dst)) != IS_MYADDR && arp_proxies==0) {
 572         DPRINTF((DBG_ARP, "ARP: request was not for me!\n"));
 573         kfree_skb(skb, FREE_READ);
 574         return(0);
 575   }
 576 
 577   /*
 578    * Yes, it is for us.
 579    * Allocate, fill in and send an ARP REPLY packet.
 580    */
 581   ret = arp_response(arp, dev, addr_hint);
 582   kfree_skb(skb, FREE_READ);
 583   return(ret);
 584 }
 585 
 586 
 587 /* Create and send an ARP REQUEST packet. */
 588 void
 589 arp_send(unsigned long paddr, struct device *dev, unsigned long saddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 590 {
 591   struct sk_buff *skb;
 592   struct arphdr *arp;
 593   unsigned char *ptr;
 594   int tmp;
 595 
 596   DPRINTF((DBG_ARP, "ARP: send(paddr=%s, ", in_ntoa(paddr)));
 597   DPRINTF((DBG_ARP, "dev=%s, ", dev->name));
 598   DPRINTF((DBG_ARP, "saddr=%s)\n", in_ntoa(saddr)));
 599 
 600   skb = alloc_skb(sizeof(struct sk_buff) +
 601                 sizeof(struct arphdr) + (2 * dev->addr_len) +
 602                 dev->hard_header_len +
 603                 (2 * 4 /* arp->plen */), GFP_ATOMIC);
 604   if (skb == NULL) {
 605         printk("ARP: No memory available for REQUEST %s\n", in_ntoa(paddr));
 606         return;
 607   }
 608   
 609   /* Fill in the request. */
 610   skb->sk = NULL;
 611   skb->mem_addr = skb;
 612   skb->len = sizeof(struct arphdr) +
 613              dev->hard_header_len + (2 * dev->addr_len) + 8;
 614   skb->mem_len = sizeof(struct sk_buff) + skb->len;
 615   skb->arp = 1;
 616   skb->dev = dev;
 617   skb->next = NULL;
 618   skb->free = 1;
 619   tmp = dev->hard_header(skb->data, dev, ETH_P_ARP, 0, saddr, skb->len);
 620   if (tmp < 0) {
 621         kfree_skb(skb,FREE_WRITE);
 622         return;
 623   }
 624   arp = (struct arphdr *) (skb->data + tmp);
 625   arp->ar_hrd = htons(dev->type);
 626   if(dev->type!=3)      /* AX.25 */
 627         arp->ar_pro = htons(ETH_P_IP);
 628   else
 629         arp->ar_pro = htons(0xCC);
 630   arp->ar_hln = dev->addr_len;
 631   arp->ar_pln = 4;
 632   arp->ar_op = htons(ARPOP_REQUEST);
 633 
 634   ptr = ((unsigned char *) &arp->ar_op) + sizeof(u_short);
 635   memcpy(ptr, dev->dev_addr, arp->ar_hln);
 636   ptr += arp->ar_hln;
 637   memcpy(ptr, &saddr, arp->ar_pln);
 638   ptr += arp->ar_pln;
 639   /*memcpy(ptr, dev->broadcast, arp->ar_hln);*/
 640   memset(ptr,0,arp->ar_hln);
 641   ptr += arp->ar_hln;
 642   memcpy(ptr, &paddr, arp->ar_pln);
 643 
 644   DPRINTF((DBG_ARP, ">>\n"));
 645   arp_print(arp);
 646   dev->queue_xmit(skb, dev, 0);
 647 }
 648 
 649 
 650 /* Find an ARP mapping in the cache. If not found, post a REQUEST. */
 651 int
 652 arp_find(unsigned char *haddr, unsigned long paddr, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 653            unsigned long saddr)
 654 {
 655   struct arp_table *apt;
 656 
 657   DPRINTF((DBG_ARP, "ARP: find(haddr=%s, ", eth_print(haddr)));
 658   DPRINTF((DBG_ARP, "paddr=%s, ", in_ntoa(paddr)));
 659   DPRINTF((DBG_ARP, "dev=%s, saddr=%s)\n", dev->name, in_ntoa(saddr)));
 660 
 661   switch(chk_addr(paddr)) {
 662         case IS_MYADDR:
 663                 memcpy(haddr, dev->dev_addr, dev->addr_len);
 664                 return(0);
 665         case IS_BROADCAST:
 666                 memcpy(haddr, dev->broadcast, dev->addr_len);
 667                 return(0);
 668   }
 669                 
 670   apt = arp_lookup(paddr);
 671   if (apt != NULL) {
 672         /*
 673          * Make sure it's not too old. If it is too old, we will
 674          * just pretend we did not find it, and then arp_send will
 675          * verify the address for us.
 676          */
 677         if ((apt->flags & ATF_PERM) ||
 678             (apt->last_used < jiffies+ARP_TIMEOUT && apt->hlen != 0)) {
 679                 apt->last_used = jiffies;
 680                 memcpy(haddr, apt->ha, dev->addr_len);
 681                 return(0);
 682         } else {
 683                 DPRINTF((DBG_ARP, "ARP: find: found expired entry for %s\n",
 684                                                         in_ntoa(apt->ip)));
 685         }
 686   }
 687 
 688   /*
 689    * This assume haddr are at least 4 bytes.
 690    * If this isn't true we can use a lookup table, one for every dev.
 691    * NOTE: this bit of code still looks fishy to me- FvK
 692    */
 693   *(unsigned long *)haddr = paddr;
 694 
 695   /* If we didn't find an entry, we will try to send an ARP packet. */
 696   arp_send(paddr, dev, saddr);
 697 
 698   return(1);
 699 }
 700 
 701 
 702 /* Add an entry to the ARP cache.  Check for dupes! */
 703 void
 704 arp_add(unsigned long addr, unsigned char *haddr, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 705 {
 706   struct arp_table *apt;
 707 
 708   DPRINTF((DBG_ARP, "ARP: add(%s, ", in_ntoa(addr)));
 709   DPRINTF((DBG_ARP, "%s, ", eth_print(haddr)));
 710   DPRINTF((DBG_ARP, "%d, %d)\n", dev->hard_header_len, dev->type));
 711 
 712   /* This is probably a good check... */
 713   if (addr == 0) {
 714         printk("ARP: add: will not add entry for 0.0.0.0 !\n");
 715         return;
 716   }
 717 
 718   /* First see if the address is already in the table. */
 719   apt = arp_lookup(addr);
 720   if (apt != NULL) {
 721         DPRINTF((DBG_ARP, "ARP: updating entry for %s\n", in_ntoa(addr)));
 722         apt->last_used = jiffies;
 723         memcpy(apt->ha, haddr , dev->addr_len);
 724         return;
 725   }
 726   arp_create(addr, haddr, dev->addr_len, dev->type);
 727 }
 728 
 729 
 730 /* Create an ARP entry for a device's broadcast address. */
 731 void
 732 arp_add_broad(unsigned long addr, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 733 {
 734   struct arp_table *apt;
 735 
 736   arp_add(addr, dev->broadcast, dev);
 737   apt = arp_lookup(addr);
 738   if (apt != NULL) {
 739         apt->flags |= ATF_PERM;
 740   }
 741 }
 742 
 743 
 744 /* Queue an IP packet, while waiting for the ARP reply packet. */
 745 void
 746 arp_queue(struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 747 {
 748   cli();
 749   skb->tries = ARP_MAX_TRIES;
 750 
 751   if (skb->next != NULL) {
 752         sti();
 753         printk("ARP: arp_queue skb already on queue magic=%X.\n", skb->magic);
 754         return;
 755   }
 756   skb_queue_tail(&arp_q,skb);
 757   skb->magic = ARP_QUEUE_MAGIC;
 758   sti();
 759 }
 760 
 761 
 762 /*
 763  * Write the contents of the ARP cache to a PROCfs file.
 764  * This is not by long perfect, as the internal ARP table doesn't
 765  * have all the info we would like to have.  Oh well, it works for
 766  * now, eh? - FvK
 767  * Also note, that due to space limits, we cannot generate more than
 768  * 4Kbyte worth of data.  This usually is enough, but I have seen
 769  * machines die from under me because of a *very* large ARP cache.
 770  * This can be simply tested by doing:
 771  *
 772  *      # ping 255.255.255.255
 773  *      # arp -a
 774  *
 775  * Perhaps we should redo PROCfs to handle larger buffers?  Michael?
 776  */
 777 int
 778 arp_get_info(char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 779 {
 780   struct arpreq *req;
 781   struct arp_table *apt;
 782   int i;
 783   char *pos;
 784 
 785   /* Loop over the ARP table and copy structures to the buffer. */
 786   pos = buffer;
 787   i = 0;
 788   for (i = 0; i < ARP_TABLE_SIZE; i++) {
 789         cli();
 790         apt = arp_tables[i];
 791         sti();
 792         while (apt != NULL) {
 793                 if (pos < (buffer + 4000)) {
 794                         req = (struct arpreq *) pos;
 795                         memset((char *) req, 0, sizeof(struct arpreq));
 796                         req->arp_pa.sa_family = AF_INET;
 797                         memcpy((char *) req->arp_pa.sa_data, (char *) &apt->ip, 4);
 798                                 req->arp_ha.sa_family = apt->htype;
 799                         memcpy((char *) req->arp_ha.sa_data,
 800                                 (char *) &apt->ha, apt->hlen);
 801                         req->arp_flags = apt->flags;
 802                 }
 803                 pos += sizeof(struct arpreq);
 804                 cli();
 805                 apt = apt->next;
 806                 sti();
 807         }
 808   }
 809   return(pos - buffer);
 810 }
 811 
 812 
 813 /* Set (create) an ARP cache entry. */
 814 static int
 815 arp_req_set(struct arpreq *req)
     /* [previous][next][first][last][top][bottom][index][help] */
 816 {
 817   struct arpreq r;
 818   struct arp_table *apt;
 819   struct sockaddr_in *si;
 820   int htype, hlen;
 821 
 822   /* We only understand about IP addresses... */
 823   memcpy_fromfs(&r, req, sizeof(r));
 824   if (r.arp_pa.sa_family != AF_INET) return(-EPFNOSUPPORT);
 825 
 826   /*
 827    * Find out about the hardware type.
 828    * We have to be compatible with BSD UNIX, so we have to
 829    * assume that a "not set" value (i.e. 0) means Ethernet.
 830    */
 831   si = (struct sockaddr_in *) &r.arp_pa;
 832   switch(r.arp_ha.sa_family) {
 833         case 0:
 834         case ARPHRD_ETHER:
 835                 htype = ARPHRD_ETHER;
 836                 hlen = ETH_ALEN;
 837                 break;
 838                 case ARPHRD_AX25:
 839                         htype = ARPHRD_AX25;
 840                         hlen = 7;
 841                         break;
 842                 
 843         default:
 844                 return(-EPFNOSUPPORT);
 845   }
 846 
 847   /* Is there an existing entry for this address? */
 848   if (si->sin_addr.s_addr == 0) {
 849         printk("ARP: SETARP: requested PA is 0.0.0.0 !\n");
 850         return(-EINVAL);
 851   }
 852   apt = arp_lookup(si->sin_addr.s_addr);
 853   if (apt == NULL) {
 854         apt = arp_create(si->sin_addr.s_addr,
 855                 (unsigned char *) r.arp_ha.sa_data, hlen, htype);
 856         if (apt == NULL) return(-ENOMEM);
 857   }
 858 
 859   /* We now have a pointer to an ARP entry.  Update it! */
 860   memcpy((char *) &apt->ha, (char *) &r.arp_ha.sa_data, hlen);
 861   apt->last_used = jiffies;
 862   apt->flags = r.arp_flags;
 863   if(apt->flags&ATF_PUBL)
 864         arp_proxies++;          /* Count proxy arps so we know if to use it */
 865 
 866   return(0);
 867 }
 868 
 869 
 870 /* Get an ARP cache entry. */
 871 static int
 872 arp_req_get(struct arpreq *req)
     /* [previous][next][first][last][top][bottom][index][help] */
 873 {
 874   struct arpreq r;
 875   struct arp_table *apt;
 876   struct sockaddr_in *si;
 877 
 878   /* We only understand about IP addresses... */
 879   memcpy_fromfs(&r, req, sizeof(r));
 880   if (r.arp_pa.sa_family != AF_INET) return(-EPFNOSUPPORT);
 881 
 882   /* Is there an existing entry for this address? */
 883   si = (struct sockaddr_in *) &r.arp_pa;
 884   apt = arp_lookup(si->sin_addr.s_addr);
 885   if (apt == NULL) return(-ENXIO);
 886 
 887   /* We found it; copy into structure. */
 888   memcpy((char *) r.arp_ha.sa_data, (char *) &apt->ha, apt->hlen);
 889   r.arp_ha.sa_family = apt->htype;
 890 
 891   /* Copy the information back */
 892   memcpy_tofs(req, &r, sizeof(r));
 893   return(0);
 894 }
 895 
 896 
 897 /* Delete an ARP cache entry. */
 898 static int
 899 arp_req_del(struct arpreq *req)
     /* [previous][next][first][last][top][bottom][index][help] */
 900 {
 901   struct arpreq r;
 902   struct sockaddr_in *si;
 903 
 904   /* We only understand about IP addresses... */
 905   memcpy_fromfs(&r, req, sizeof(r));
 906   if (r.arp_pa.sa_family != AF_INET) return(-EPFNOSUPPORT);
 907 
 908   si = (struct sockaddr_in *) &r.arp_pa;
 909   
 910   /* The system cope with this but splats up a nasty kernel message 
 911      We trap it beforehand and tell the user off */
 912   if(chk_addr(si->sin_addr.s_addr)==IS_MYADDR)
 913         return -EINVAL;
 914         
 915   arp_destroy(si->sin_addr.s_addr);
 916 
 917   return(0);
 918 }
 919 
 920 
 921 /* Handle an ARP layer I/O control request. */
 922 int
 923 arp_ioctl(unsigned int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 924 {
 925   int err;
 926   switch(cmd) {
 927         case DDIOCSDBG:
 928                 return(dbg_ioctl(arg, DBG_ARP));
 929         case SIOCDARP:
 930                 if (!suser()) return(-EPERM);
 931                 err=verify_area(VERIFY_READ,arg,sizeof(struct arpreq));
 932                 if(err)
 933                         return err;
 934                 return(arp_req_del((struct arpreq *)arg));
 935         case SIOCGARP:
 936                 err=verify_area(VERIFY_WRITE,arg,sizeof(struct arpreq));
 937                 if(err)
 938                         return err;
 939                 return(arp_req_get((struct arpreq *)arg));
 940         case SIOCSARP:
 941                 if (!suser()) return(-EPERM);
 942                 err=verify_area(VERIFY_READ,arg,sizeof(struct arpreq));
 943                 if(err)
 944                         return err;
 945                 return(arp_req_set((struct arpreq *)arg));
 946         default:
 947                 return(-EINVAL);
 948   }
 949   /*NOTREACHED*/
 950   return(0);
 951 }

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