home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / ARP.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  14KB  |  398 lines

  1. /* Address Resolution Protocol (ARP) functions. Sits between IP and
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  * Level 2, mapping IP to Level 2 addresses for all outgoing datagrams.
  4.  *
  5.  * Mods by G1EMM
  6.  *
  7.  * Mods by SM6RPZ
  8.  * 1992-05-28 - Added interface to arp_lookup() and arp_add().
  9.  * 1992-07-07 - Added arp_timeout(). This function now works according to the
  10.  *              last page in RFC826. When an ARP entry times out we will first
  11.  *              try an ARP reply before deleting the entry.
  12.  * 1992-07-08 - We will update the ARP table and create new entries each time
  13.  *              we hear an ARP request message on the channel. By doing so we
  14.  *              will gain knowledge of other stations hardware addresses
  15.  *              whithout sending a lot of ARP requests. If we are running RSPF,
  16.  *              it will be informed about these "findings".
  17.  * 1992-07-11 - Datagrams to be sent while awaiting an resolution will be put
  18.  *              on a queue. The length of this queue in datagrams is defined
  19.  *              in arp.h (ARP_QUEUE).
  20.  *
  21.  * 1992-10-27 The above 3 behaviours are now configurable via additional
  22.  *      arp subcommands - WG7J
  23.  *
  24.  */
  25.   
  26. #include "global.h"
  27. #include "mbuf.h"
  28. #include "timer.h"
  29. #include "iface.h"
  30. #include "enet.h"
  31. #include "ax25.h"
  32. #include "icmp.h"
  33. #include "ip.h"
  34. #include "arp.h"
  35. #include "icmp.h"
  36. #include "rspf.h"
  37.   
  38. extern int Maxarpq;
  39.   
  40. static void arp_output __ARGS((struct iface *iface,int16 hardware,int32 target,char *hw_addr));
  41. static void arp_timeout __ARGS((void *p));  /* sm6rpz */
  42.   
  43. /* Hash table headers */
  44. struct arp_tab *Arp_tab[HASHMOD];
  45. struct arp_stat Arp_stat;
  46.   
  47. /* Resolve an IP address to a hardware address; if not found,
  48.  * initiate query and return NULLCHAR.  If an address is returned, the
  49.  * interface driver may send the packet; if NULLCHAR is returned,
  50.  * res_arp() will have saved the packet on its pending queue,
  51.  * so no further action (like freeing the packet) is necessary.
  52.  */
  53. char *
  54. res_arp(iface,hardware,target,bp)
  55. struct iface *iface;    /* Pointer to interface block */
  56. int16 hardware;         /* Hardware type */
  57. int32 target;           /* Target IP address */
  58. struct mbuf *bp;        /* IP datagram to be queued if unresolved */
  59. {
  60.     register struct arp_tab *arp;
  61.     struct ip ip;
  62.   
  63.     if((arp = arp_lookup(hardware,target,iface)) != NULLARP && arp->state == ARP_VALID)
  64.         return arp->hw_addr;
  65.     if(arp != NULLARP){
  66.         if(len_q(arp->pending) > Maxarpq) {
  67.             /* Earlier packets are already pending, kick
  68.              * this one back as a source quench
  69.              */
  70.             ntohip(&ip,&bp);
  71.             icmp_output(&ip,bp,ICMP_QUENCH,0,NULL);
  72.             free_p(bp);
  73.         } else
  74.             enqueue(&arp->pending,bp);
  75.     } else {
  76.         /* Create an entry and put the datagram on the
  77.          * queue pending an answer
  78.          */
  79.         arp = arp_add(target,hardware,NULLCHAR,0,iface);
  80.         enqueue(&arp->pending,bp);
  81.         arp_output(iface,hardware,target,NULLCHAR);
  82.     }
  83.     return NULLCHAR;
  84. }
  85.   
  86. /* Handle incoming ARP packets. This is almost a direct implementation of
  87.  * the algorithm on page 5 of RFC 826, except for:
  88.  * 1. Outgoing datagrams to unresolved addresses are kept on a queue
  89.  *    pending a reply to our ARP request.
  90.  * 2. The names of the fields in the ARP packet were made more mnemonic.
  91.  * 3. Requests for IP addresses listed in our table as "published" are
  92.  *    responded to, even if the address is not our own.
  93.  */
  94. void
  95. arp_input(iface,bp)
  96. struct iface *iface;
  97. struct mbuf *bp;
  98. {
  99.     struct arp arp;
  100.     struct arp_tab *ap;
  101.     struct arp_type *at;
  102.     int i;
  103.     int32 wanted;
  104.   
  105.     Arp_stat.recv++;
  106.     if(ntoharp(&arp,&bp) == -1)     /* Convert into host format */
  107.         return;
  108.     if(arp.hardware >= NHWTYPES){
  109.         /* Unknown hardware type, ignore */
  110.         Arp_stat.badtype++;
  111.         return;
  112.     }
  113.     at = &Arp_type[arp.hardware];
  114.     if(arp.protocol != at->iptype){
  115.         /* Unsupported protocol type, ignore */
  116.         Arp_stat.badtype++;
  117.         return;
  118.     }
  119.     if((int16)uchar(arp.hwalen) > MAXHWALEN || uchar(arp.pralen) != sizeof(int32)){
  120.         /* Incorrect protocol addr length (different hw addr lengths
  121.          * are OK since AX.25 addresses can be of variable length)
  122.          */
  123.         Arp_stat.badlen++;
  124.         return;
  125.   
  126.     }
  127.     if(arp.sprotaddr == 0L || arp.tprotaddr == 0L){
  128.         /* We are going to dead-end references for [0.0.0.0], since
  129.          * experience shows that these cause total lock up -- N1BEE
  130.          */
  131.         Arp_stat.badaddr++;
  132.         return;
  133.     }
  134.     if(memcmp(arp.shwaddr,at->bdcst,at->hwalen) == 0){
  135.         /* This guy is trying to say he's got the broadcast address! */
  136.         Arp_stat.badaddr++;
  137.         return;
  138.     }
  139.     /* Try to refine ARP according to section 5.7 in Douglas E.
  140.      * Comers book "Internetworking With TCP/IP", 2nd ed. page 77.
  141.      * I.e we will use all ARP-packets we can see thereby lessen
  142.      * the ARP traffic a little. -- sm6rpz
  143.      */
  144.     if(((ap = arp_lookup(arp.hardware,arp.sprotaddr,iface)) != NULLARP
  145.         && dur_timer(&ap->timer) != 0)
  146.         || ( (iface->flags & ARP_EAVESDROP) &&
  147.         ap == NULLARP && arp.opcode != REVARP_REQUEST)
  148.     ) {
  149.         ap = arp_add(arp.sprotaddr,arp.hardware,arp.shwaddr,0,iface);
  150.     }
  151.     /* See if we're the address they're looking for */
  152.     if(ismyaddr(arp.tprotaddr) != NULLIF){
  153.         if(ap == NULLARP)   /* Only if not already in the table */
  154.             arp_add(arp.sprotaddr,arp.hardware,arp.shwaddr,0,iface);
  155.   
  156.         if(arp.opcode == ARP_REQUEST){
  157.             /* Swap sender's and target's (us) hardware and protocol
  158.              * fields, and send the packet back as a reply
  159.              */
  160.             memcpy(arp.thwaddr,arp.shwaddr,(int16)uchar(arp.hwalen));
  161.             /* Mark the end of the sender's AX.25 address
  162.              * in case he didn't
  163.              */
  164.             if(arp.hardware == ARP_AX25)
  165.                 arp.thwaddr[uchar(arp.hwalen)-1] |= E;
  166.   
  167.             memcpy(arp.shwaddr,iface->hwaddr,at->hwalen);
  168.             wanted = arp.tprotaddr;
  169.             arp.tprotaddr = arp.sprotaddr;
  170. /*            arp.sprotaddr = iface->addr;*/
  171.             /* reply with what was asked for ! From Mike Galagher */
  172.             arp.sprotaddr = wanted;
  173.             arp.opcode = ARP_REPLY;
  174.             if((bp = htonarp(&arp)) == NULLBUF)
  175.                 return;
  176.   
  177.             if(iface->forw != NULLIF)
  178.                 (*iface->forw->output)(iface->forw,
  179.                 arp.thwaddr,iface->forw->hwaddr,at->arptype,bp);
  180.             else
  181.                 (*iface->output)(iface,arp.thwaddr,
  182.                 iface->hwaddr,at->arptype,bp);
  183.             Arp_stat.inreq++;
  184. #ifdef  RSPF
  185.             /* Do an RSPF upcall */
  186.             rspfarpupcall(arp.tprotaddr,arp.hardware,NULLIF);
  187. #endif  /* RSPF*/
  188.         } else {
  189.             Arp_stat.replies++;
  190. #ifdef  RSPF
  191.             /* Do an RSPF upcall */
  192.             rspfarpupcall(arp.sprotaddr,arp.hardware,iface);
  193. #endif  /* RSPF*/
  194.         }
  195.     } else if(arp.opcode == ARP_REQUEST
  196.         && (ap = arp_lookup(arp.hardware,arp.tprotaddr,iface)) != NULLARP
  197.     && ap->pub){
  198.         /* Otherwise, respond if the guy he's looking for is
  199.          * published in our table.
  200.          */
  201.         memcpy(arp.thwaddr,arp.shwaddr,(int16)uchar(arp.hwalen));
  202.         memcpy(arp.shwaddr,ap->hw_addr,at->hwalen);
  203.         arp.tprotaddr = arp.sprotaddr;
  204.         arp.sprotaddr = ap->ip_addr;
  205.         arp.opcode = ARP_REPLY;
  206.         if((bp = htonarp(&arp)) == NULLBUF)
  207.             return;
  208.         if(iface->forw != NULLIF)
  209.             (*iface->forw->output)(iface->forw,
  210.             arp.thwaddr,iface->forw->hwaddr,at->arptype,bp);
  211.         else
  212.             (*iface->output)(iface,arp.thwaddr,
  213.             iface->hwaddr,at->arptype,bp);
  214.         Arp_stat.inreq++;
  215.     } else if(arp.opcode == REVARP_REQUEST){
  216.         for(i=0;i<HASHMOD;i++)
  217.             for(ap = Arp_tab[i];ap != NULLARP;ap = ap->next)
  218.                 if(memcmp(ap->hw_addr,arp.thwaddr,at->hwalen) == 0)
  219.                     goto found;
  220.         found:  if(ap != NULLARP && ap->pub){
  221.             memcpy(arp.shwaddr,iface->hwaddr,at->hwalen);
  222.             arp.tprotaddr = ap->ip_addr;
  223.             arp.sprotaddr = iface->addr;
  224.             arp.opcode = REVARP_REPLY;
  225.             if((bp = htonarp(&arp)) == NULLBUF)
  226.                 return;
  227.             if(iface->forw != NULLIF)
  228.                 (*iface->forw->output)(iface->forw,
  229.                 arp.thwaddr,iface->forw->hwaddr,REVARP_TYPE,bp);
  230.             else
  231.                 (*iface->output)(iface,arp.thwaddr,
  232.                 iface->hwaddr,REVARP_TYPE,bp);
  233.             Arp_stat.inreq++;
  234.         }
  235.     }
  236. }
  237. /* Add an IP-addr / hardware-addr pair to the ARP table */
  238. struct arp_tab *
  239. arp_add(ipaddr,hardware,hw_addr,pub,iface)
  240. int32 ipaddr;           /* IP address, host order */
  241. int16 hardware;         /* Hardware type */
  242. char *hw_addr;          /* Hardware address, if known; NULLCHAR otherwise */
  243. int pub;                /* Publish this entry? */
  244. struct iface *iface;
  245. {
  246.     struct mbuf *bp;
  247.     register struct arp_tab *ap;
  248.     struct arp_type *at;
  249.     unsigned hashval;
  250.   
  251.     if(hardware >=NHWTYPES)
  252.         return NULLARP; /* Invalid hardware type */
  253.     at = &Arp_type[hardware];
  254.   
  255.     if((ap = arp_lookup(hardware,ipaddr,iface)) == NULLARP){
  256.         /* New entry */
  257.         ap = (struct arp_tab *)callocw(1,sizeof(struct arp_tab));
  258.         ap->hw_addr = mallocw(at->hwalen);
  259.         ap->timer.func = arp_timeout;
  260.         ap->timer.arg = ap;
  261.         ap->hardware = hardware;
  262.         ap->ip_addr = ipaddr;
  263.         ap->iface = iface;
  264.   
  265.         /* Put on head of hash chain */
  266.         hashval = hash_ip(ipaddr);
  267.         ap->prev = NULLARP;
  268.         ap->next = Arp_tab[hashval];
  269.         Arp_tab[hashval] = ap;
  270.         if(ap->next != NULLARP){
  271.             ap->next->prev = ap;
  272.         }
  273.     }
  274.     if(hw_addr == NULLCHAR){
  275.         /* Await response */
  276.         ap->state = ARP_PENDING;
  277.         set_timer(&ap->timer,Arp_type[hardware].pendtime * 1000L);
  278.     } else {
  279.         /* Response has come in, update entry and run through queue */
  280.         ap->state = ARP_VALID;
  281.         set_timer(&ap->timer,ARPLIFE*1000L);
  282.         memcpy(ap->hw_addr,hw_addr,at->hwalen);
  283.         ap->pub = pub;
  284.         while((bp = dequeue(&ap->pending)) != NULLBUF)
  285.             ip_route(NULLIF,bp,0);
  286.     }
  287.     start_timer(&ap->timer);
  288.     return ap;
  289. }
  290.   
  291. /* Remove an entry from the ARP table */
  292. void
  293. arp_drop(p)
  294. void *p;
  295. {
  296.     register struct arp_tab *ap;
  297.   
  298.     ap = (struct arp_tab *)p;
  299.     if(ap == NULLARP)
  300.         return;
  301.     stop_timer(&ap->timer); /* Shouldn't be necessary */
  302.     if(ap->next != NULLARP)
  303.         ap->next->prev = ap->prev;
  304.     if(ap->prev != NULLARP)
  305.         ap->prev->next = ap->next;
  306.     else
  307.         Arp_tab[hash_ip(ap->ip_addr)] = ap->next;
  308.     free_q(&ap->pending);
  309.     free(ap->hw_addr);
  310.     free((char *)ap);
  311. }
  312.   
  313. /* Look up the given IP address in the ARP table */
  314. struct arp_tab *
  315. arp_lookup(hardware,ipaddr,iface)
  316. int16 hardware;
  317. int32 ipaddr;
  318. struct iface *iface;
  319. {
  320.     register struct arp_tab *ap;
  321.   
  322.     for(ap = Arp_tab[hash_ip(ipaddr)]; ap != NULLARP; ap = ap->next){
  323.         if(ap->ip_addr == ipaddr && ap->hardware == hardware \
  324.             && ap->iface == iface)
  325.             break;
  326.     }
  327.     return ap;
  328. }
  329. /* Send an ARP request to resolve IP address target_ip */
  330. static void
  331. arp_output(iface,hardware,target,hw_addr)
  332. struct iface *iface;
  333. int16 hardware;
  334. int32 target;
  335. char *hw_addr;
  336. {
  337.     struct arp arp;
  338.     struct mbuf *bp;
  339.     struct arp_type *at;
  340.   
  341.     at = &Arp_type[hardware];
  342.     if(iface->output == NULLFP)
  343.         return;
  344.   
  345.     arp.hardware = hardware;
  346.     arp.protocol = at->iptype;
  347.     arp.hwalen = at->hwalen;
  348.     arp.pralen = sizeof(int32);
  349.     arp.opcode = ARP_REQUEST;
  350.     memcpy(arp.shwaddr,iface->hwaddr,at->hwalen);
  351.     arp.sprotaddr = iface->addr;
  352.     memset(arp.thwaddr,0,at->hwalen);
  353.     arp.tprotaddr = target;
  354.     if((bp = htonarp(&arp)) == NULLBUF)
  355.         return;
  356.     if(hw_addr == NULLCHAR)
  357.         (*iface->output)(iface,at->bdcst,iface->hwaddr,at->arptype,bp);
  358.     else
  359.         (*iface->output)(iface,hw_addr,iface->hwaddr,at->arptype,bp);
  360.     Arp_stat.outreq++;
  361. }
  362.   
  363. /* Called when an ARP entry times out. If an entry has been a valid
  364.  * one we will send out an ARP reply. If this one does not succed,
  365.  * the ARP entry will be dropped. -- sm6rpz
  366.  *
  367.  * Made configurable by WG7J.
  368.  */
  369. static void
  370. arp_timeout(p)
  371. void *p;
  372. {
  373.     struct arp_type *at;
  374.     char *hwaddr;
  375.     struct arp_tab *ap = (struct arp_tab *)p;
  376.   
  377.     if(ap == NULLARP)
  378.         return;
  379.     stop_timer(&ap->timer);
  380.     /* Check to see if the timer is set to ARPLIFE or pendtime. Set_timer()
  381.      * adds at least one tick so we must do a little more flexible check.
  382.      */
  383.     if( (ap->iface->flags & ARP_KEEPALIVE) &&
  384.     (dur_timer(&ap->timer) >= ARPLIFE * 1000L)) {
  385.         at = &Arp_type[ap->hardware];
  386.         set_timer(&ap->timer,at->pendtime * 1000L);
  387.     /* timer functions should NEVER call blocked allocs ! - WG7J */
  388.         if((hwaddr = (char *)mallocw((unsigned)at->hwalen))!=NULLCHAR) {
  389.             memcpy(hwaddr,ap->hw_addr,at->hwalen);
  390.             arp_output(ap->iface,ap->hardware,ap->ip_addr,hwaddr);
  391.             free(hwaddr);           /* clean up */
  392.         }
  393.         start_timer(&ap->timer);
  394.     } else
  395.         arp_drop(ap);
  396. }
  397.   
  398.