home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / SRC_0618.ZIP / IPROUTE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-15  |  17.7 KB  |  692 lines

  1. /* Lower half of IP, consisting of gateway routines
  2.  * Includes routing and options processing code
  3.  *
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include "global.h"
  7. #include "mbuf.h"
  8. #include "iface.h"
  9. #include "timer.h"
  10. #include "internet.h"
  11. #include "ip.h"
  12. #include "netuser.h"
  13. #include "icmp.h"
  14. #include "rip.h"
  15. #include "trace.h"
  16. #include "pktdrvr.h"
  17. #include "bootp.h"
  18.  
  19.  
  20. struct route *Routes[32][HASHMOD];    /* Routing table */
  21. struct route R_default = {        /* Default route entry */
  22.     NULLROUTE, NULLROUTE,
  23.     0,0,0,
  24.     RIP_INFINITY        /* Init metric to infinity */
  25. };
  26.  
  27. int32 Ip_addr;
  28. static struct rt_cache Rt_cache;
  29.  
  30. /* Initialize modulo lookup table used by hash_ip() in pcgen.asm */
  31. void
  32. ipinit()
  33. {
  34.     int i;
  35.  
  36.     for(i=0;i<256;i++)
  37.         Hashtab[i] = i % HASHMOD;
  38. }
  39.  
  40. /* Route an IP datagram. This is the "hopper" through which all IP datagrams,
  41.  * coming or going, must pass.
  42.  *
  43.  * "rxbroadcast" is set to indicate that the packet came in on a subnet
  44.  * broadcast. The router will kick the packet upstairs regardless of the
  45.  * IP destination address.
  46.  */
  47. int
  48. ip_route(i_iface,bp,rxbroadcast)
  49. struct iface *i_iface;    /* Input interface */
  50. struct mbuf *bp;    /* Input packet */
  51. int rxbroadcast;    /* True if packet had link broadcast address */
  52. {
  53.     struct ip ip;            /* IP header being processed */
  54.     int16 ip_len;            /* IP header length */
  55.     int16 length;            /* Length of data portion */
  56.     int32 gateway;            /* Gateway IP address */
  57.     register struct route *rp;    /* Route table entry */
  58.     struct iface *iface;        /* Output interface, possibly forwarded */
  59.     int16 offset;            /* Offset into current fragment */
  60.     int16 mf_flag;            /* Original datagram MF flag */
  61.     int strict = 0;            /* Strict source routing flag */
  62.     char prec;            /* Extracted from tos field */
  63.     char del;
  64.     char tput;
  65.     char rel;
  66.     int16 opt_len;        /* Length of current option */
  67.     char *opt;        /* -> beginning of current option */
  68.     int i;
  69.     struct mbuf *tbp;
  70.     int ckgood = 1;
  71.     int pointer;        /* Relative pointer index for sroute/rroute */
  72.  
  73.     if(i_iface != NULLIF){
  74.         ipInReceives++;    /* Not locally generated */
  75.         i_iface->iprecvcnt++;
  76.     }
  77.     if(len_p(bp) < IPLEN){
  78.         /* The packet is shorter than a legal IP header */
  79.         ipInHdrErrors++;
  80.         free_p(bp);
  81.         return -1;
  82.     }
  83.     /* Sneak a peek at the IP header's IHL field to find its length */
  84.     ip_len = (bp->data[0] & 0xf) << 2;
  85.     if(ip_len < IPLEN){
  86.         /* The IP header length field is too small */
  87.         ipInHdrErrors++;
  88.         free_p(bp);
  89.         return -1;
  90.     }
  91.     if(cksum(NULLHEADER,bp,ip_len) != 0){
  92.         /* Bad IP header checksum; discard */
  93.         ipInHdrErrors++;
  94.         free_p(bp);
  95.         return -1;
  96.     }
  97.     /* Extract IP header */
  98.     ntohip(&ip,&bp);
  99.  
  100.     if(ip.version != IPVERSION){
  101.         /* We can't handle this version of IP */
  102.         ipInHdrErrors++;
  103.         free_p(bp);
  104.         return -1;
  105.     }
  106.     /* Trim data segment if necessary. */
  107.     length = ip.length - ip_len;    /* Length of data portion */
  108.     trim_mbuf(&bp,length);    
  109.                 
  110.     /* If we're running low on memory, return a source quench */
  111.     if(!rxbroadcast && availmem() < Memthresh)
  112.         icmp_output(&ip,bp,ICMP_QUENCH,0,NULLICMP);
  113.  
  114.     /* Process options, if any. Also compute length of secondary IP
  115.      * header in case fragmentation is needed later
  116.      */
  117.     strict = 0;
  118.     for(i=0;i<ip.optlen;i += opt_len){
  119.  
  120.         /* First check for the two special 1-byte options */
  121.         switch(ip.options[i] & OPT_NUMBER){
  122.         case IP_EOL:
  123.             goto no_opt;    /* End of options list, we're done */
  124.         case IP_NOOP:
  125.             opt_len = 1;
  126.             continue;    /* No operation, skip to next option */
  127.         }
  128.         /* Not a 1-byte option, so ensure that there's at least
  129.          * two bytes of option left, that the option length is
  130.          * at least two, and that there's enough space left for
  131.          * the specified option length.
  132.          */
  133.         if(ip.optlen - i < 2
  134.          || ((opt_len = uchar(ip.options[i+1])) < 2)
  135.          || ip.optlen - i < opt_len){
  136.             /* Truncated option, send ICMP and drop packet */
  137.             if(!rxbroadcast){
  138.                 union icmp_args icmp_args;
  139.  
  140.                 icmp_args.pointer = IPLEN + i;
  141.                 icmp_output(&ip,bp,ICMP_PARAM_PROB,0,&icmp_args);
  142.             }
  143.             free_p(bp);
  144.             return -1;
  145.         }
  146.         opt = &ip.options[i];
  147.  
  148.         switch(opt[0] & OPT_NUMBER){
  149.         case IP_SSROUTE:    /* Strict source route & record route */
  150.             strict = 1;    /* note fall-thru */
  151.         case IP_LSROUTE:    /* Loose source route & record route */
  152.             /* Source routes are ignored unless we're in the
  153.              * destination field
  154.              */
  155.             if(opt_len < 3){
  156.                 /* Option is too short to be a legal sroute.
  157.                  * Send an ICMP message and drop it.
  158.                  */
  159.                 if(!rxbroadcast){
  160.                     union icmp_args icmp_args;
  161.  
  162.                     icmp_args.pointer = IPLEN + i;
  163.                     icmp_output(&ip,bp,ICMP_PARAM_PROB,0,&icmp_args);
  164.                 }
  165.                 free_p(bp);
  166.                 return -1;
  167.             }
  168.             if(ismyaddr(ip.dest) == NULLIF)
  169.                 break;    /* Skip to next option */
  170.             pointer = uchar(opt[2]);
  171.             if(pointer + 4 > opt_len)
  172.                 break;    /* Route exhausted; it's for us */
  173.  
  174.             /* Put address for next hop into destination field,
  175.              * put our address into the route field, and bump
  176.              * the pointer. We've already ensured enough space.
  177.              */
  178.             ip.dest = get32(&opt[pointer]);
  179.             put32(&opt[pointer],locaddr(ip.dest));
  180.             opt[2] += 4;
  181.             ckgood = 0;
  182.             break;
  183.         case IP_RROUTE:    /* Record route */
  184.             if(opt_len < 3){
  185.                 /* Option is too short to be a legal rroute.
  186.                  * Send an ICMP message and drop it.
  187.                  */
  188.                 if(!rxbroadcast){
  189.                     union icmp_args icmp_args;
  190.  
  191.                     icmp_args.pointer = IPLEN + i;
  192.                     icmp_output(&ip,bp,ICMP_PARAM_PROB,0,&icmp_args);
  193.                 }
  194.                 free_p(bp);
  195.                 return -1;
  196.             }                
  197.             pointer = uchar(opt[2]);
  198.             if(pointer + 4 > opt_len){
  199.                 /* Route area exhausted; send an ICMP msg */
  200.                 if(!rxbroadcast){
  201.                     union icmp_args icmp_args;
  202.  
  203.                     icmp_args.pointer = IPLEN + i;
  204.                     icmp_output(&ip,bp,ICMP_PARAM_PROB,0,&icmp_args);
  205.                 }
  206.                 /* Also drop if odd-sized */
  207.                 if(pointer != opt_len){
  208.                     free_p(bp);
  209.                     return -1;
  210.                 }
  211.             } else {
  212.                 /* Add our address to the route.
  213.                  * We've already ensured there's enough space.
  214.                  */
  215.                 put32(&opt[pointer],locaddr(ip.dest));
  216.                  opt[2] += 4;
  217.                 ckgood = 0;
  218.             }
  219.             break;
  220.         }
  221.     }
  222. no_opt:
  223.  
  224.     /* See if it's a broadcast or addressed to us, and kick it upstairs */
  225.     if(ismyaddr(ip.dest) != NULLIF || rxbroadcast ||
  226.         (WantBootp && bootp_validPacket(&ip, &bp))){
  227. #ifdef    GWONLY
  228.     /* We're only a gateway, we have no host level protocols */
  229.         if(!rxbroadcast)
  230.             icmp_output(&ip,bp,ICMP_DEST_UNREACH,
  231.              ICMP_PROT_UNREACH,NULLICMP);
  232.         ipInUnknownProtos++;
  233.         free_p(bp);
  234. #else
  235.         ip_recv(i_iface,&ip,bp,rxbroadcast);
  236. #endif
  237.         return 0;
  238.     }
  239.     /* Packet is not destined to us. If it originated elsewhere, count
  240.      * it as a forwarded datagram.
  241.      */
  242.     if(i_iface != NULLIF)
  243.         ipForwDatagrams++;
  244.  
  245.     /* Adjust the header checksum to allow for the modified TTL */        
  246.     ip.checksum += 0x100;
  247.     if((ip.checksum & 0xff00) == 0)
  248.         ip.checksum++;    /* end-around carry */
  249.  
  250.     /* Decrement TTL and discard if zero. We don't have to check
  251.      * rxbroadcast here because it's already been checked
  252.      */
  253.     if(--ip.ttl == 0){
  254.         /* Send ICMP "Time Exceeded" message */
  255.         icmp_output(&ip,bp,ICMP_TIME_EXCEED,0,NULLICMP);
  256.         ipInHdrErrors++;
  257.         free_p(bp);
  258.         return -1;
  259.     }
  260.     /* Look up target address in routing table */
  261.     if((rp = rt_lookup(ip.dest)) == NULLROUTE){
  262.         /* No route exists, return unreachable message (we already
  263.          * know this can't be a broadcast)
  264.          */
  265.         icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,NULLICMP);
  266.         free_p(bp);
  267.         ipOutNoRoutes++;
  268.         return -1;
  269.     }
  270.     rp->uses++;
  271.  
  272.     /* Check for output forwarding and divert if necessary */
  273.     iface = rp->iface;
  274.     if(iface->forw != NULLIF)
  275.         iface = iface->forw;
  276.  
  277.     /* Find gateway; zero gateway in routing table means "send direct" */
  278.     if(rp->gateway == 0)
  279.         gateway = ip.dest;
  280.     else
  281.         gateway = rp->gateway;
  282.  
  283.     if(strict && gateway != ip.dest){
  284.         /* Strict source routing requires a direct entry
  285.          * Again, we know this isn't a broadcast
  286.          */
  287.         icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_ROUTE_FAIL,NULLICMP);
  288.         free_p(bp);
  289.         ipOutNoRoutes++;
  290.         return -1;
  291.     }
  292.     prec = PREC(ip.tos);
  293.     del = ip.tos & DELAY;
  294.     tput = ip.tos & THRUPUT;
  295.     rel = ip.tos & RELIABILITY;
  296.  
  297.     if(ip.length <= iface->mtu){
  298.         /* Datagram smaller than interface MTU; put header
  299.          * back on and send normally.
  300.          */
  301.         if((tbp = htonip(&ip,bp,ckgood)) == NULLBUF){
  302.             free_p(bp);
  303.             return -1;
  304.         }
  305.         iface->ipsndcnt++;
  306.         return (*iface->send)(tbp,iface,gateway,prec,del,tput,rel);
  307.     }
  308.     /* Fragmentation needed */
  309.     if(ip.flags.df){
  310.         /* Don't Fragment set; return ICMP message and drop */
  311.         union icmp_args icmp_args;
  312.  
  313.         icmp_args.mtu = iface->mtu;
  314.         icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED,&icmp_args);
  315.         free_p(bp);
  316.         ipFragFails++;
  317.         return -1;
  318.     }
  319.     /* Create fragments */
  320.     offset = ip.offset;
  321.     mf_flag = ip.flags.mf;        /* Save original MF flag */
  322.     while(length != 0){        /* As long as there's data left */
  323.         int16 fragsize;        /* Size of this fragment's data */
  324.         struct mbuf *f_data;    /* Data portion of fragment */
  325.  
  326.         /* After the first fragment, should remove those
  327.          * options that aren't supposed to be copied on fragmentation
  328.          */
  329.         ip.offset = offset;
  330.         if(length + ip_len <= iface->mtu){
  331.             /* Last fragment; send all that remains */
  332.             fragsize = length;
  333.             ip.flags.mf = mf_flag;    /* Pass original MF flag */
  334.         } else {
  335.             /* More to come, so send multiple of 8 bytes */
  336.             fragsize = (iface->mtu - ip_len) & 0xfff8;
  337.             ip.flags.mf = 1;
  338.         }
  339.         ip.length = fragsize + ip_len;
  340.  
  341.         /* Duplicate the fragment */
  342.         dup_p(&f_data,bp,offset,fragsize);
  343.         if(f_data == NULLBUF){
  344.             free_p(bp);
  345.             ipFragFails++;
  346.             return -1;
  347.         }
  348.         /* Put IP header back on, recomputing checksum */
  349.         if((tbp = htonip(&ip,f_data,0)) == NULLBUF){
  350.             free_p(f_data);
  351.             free_p(bp);
  352.             ipFragFails++;
  353.             return -1;
  354.         }
  355.         /* and ship it out */
  356.         if((*iface->send)(tbp,iface,gateway,prec,del,tput,rel) == -1){
  357.             ipFragFails++;
  358.             free_p(bp);
  359.             return -1;
  360.         }
  361.         iface->ipsndcnt++;
  362.         ipFragCreates++;
  363.         offset += fragsize;
  364.         length -= fragsize;
  365.     }
  366.     ipFragOKs++;
  367.     free_p(bp);
  368.     return 0;
  369. }
  370. int
  371. ip_encap(bp,iface,gateway,prec,del,tput,rel)
  372. struct mbuf *bp;
  373. struct iface *iface;
  374. int32 gateway;
  375. int prec;
  376. int del;
  377. int tput;
  378. int rel;
  379. {
  380.     struct ip ip;
  381.  
  382.     dump(iface,IF_TRACE_OUT,CL_NONE,bp);
  383.     iface->rawsndcnt++;
  384.     iface->lastsent = secclock();
  385.  
  386.     if(gateway == 0L){
  387.         /* Gateway must be specified */
  388.         ntohip(&ip,&bp);
  389.         icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,NULLICMP);
  390.         free_p(bp);
  391.         ipOutNoRoutes++;
  392.         return -1;
  393.     }
  394.     /* Encapsulate in an IP packet from us to the gateway */
  395.     return ip_send(INADDR_ANY,gateway,IP_PTCL,0,0,bp,0,0,0);
  396. }
  397.  
  398. /* Add an entry to the IP routing table. Returns 0 on success, -1 on failure */
  399. struct route *
  400. rt_add(target,bits,gateway,iface,metric,ttl,private)
  401. int32 target;        /* Target IP address prefix */
  402. unsigned int bits;    /* Size of target address prefix in bits (0-32) */
  403. int32 gateway;        /* Optional gateway to be reached via interface */
  404. struct iface *iface;    /* Interface to which packet is to be routed */
  405. int32 metric;        /* Metric for this route entry */
  406. int32 ttl;        /* Lifetime of this route entry in sec */
  407. char private;        /* Inhibit advertising this entry ? */
  408. {
  409.     struct route *rp,**hp;
  410.     struct route *rptmp;
  411.     int32 gwtmp;
  412.  
  413.     if(iface == NULLIF)
  414.         return NULLROUTE;
  415.  
  416.     if(bits == 32 && ismyaddr(target))
  417.         return NULLROUTE;    /* Don't accept routes to ourselves */
  418.  
  419.     /* Encapsulated routes must specify gateway, and it can't be
  420.      *  ourselves
  421.      */
  422.     if(iface == &Encap && (gateway == 0 || ismyaddr(gateway)))
  423.         return NULLROUTE;
  424.  
  425.     Rt_cache.route = NULLROUTE;    /* Flush cache */
  426.  
  427.     /* Zero bits refers to the default route */
  428.     if(bits == 0){
  429.         rp = &R_default;
  430.     } else {
  431.         rp = rt_blookup(target,bits);
  432.     }
  433.     if(rp == NULLROUTE){
  434.         /* The target is not already in the table, so create a new
  435.          * entry and put it in.
  436.          */
  437.         rp = (struct route *)callocw(1,sizeof(struct route));
  438.         /* Insert at head of table */
  439.         rp->prev = NULLROUTE;
  440.         hp = &Routes[bits-1][hash_ip(target)];
  441.         rp->next = *hp;
  442.         if(rp->next != NULLROUTE)
  443.             rp->next->prev = rp;
  444.         *hp = rp;
  445.         rp->uses = 0;
  446.     }
  447.     rp->target = target;
  448.     rp->bits = bits;
  449.     rp->gateway = gateway;
  450.     rp->metric = metric;
  451.     rp->iface = iface;
  452.     rp->flags = private ? RTPRIVATE : 0;    /* Should anyone be told of this route? */
  453.     rp->timer.func = rt_timeout;  /* Set the timer field */
  454.     rp->timer.arg = (void *)rp;
  455.     set_timer(&rp->timer,ttl*1000L);
  456.     stop_timer(&rp->timer);
  457.     start_timer(&rp->timer); /* start the timer if appropriate */
  458.  
  459.     /* Check to see if this created an encapsulation loop */
  460.     gwtmp = gateway;
  461.     for(;;){
  462.         rptmp = rt_lookup(gwtmp);
  463.         if(rptmp == NULLROUTE)
  464.             break;    /* No route to gateway, so no loop */
  465.         if(rptmp->iface != &Encap)
  466.             break;    /* Non-encap interface, so no loop */
  467.         if(rptmp == rp){
  468.             rt_drop(target,bits);    /* Definite loop */
  469.             return NULLROUTE;
  470.         }
  471.         if(rptmp->gateway != 0)
  472.             gwtmp = rptmp->gateway;
  473.     }
  474.     return rp;
  475. }
  476.  
  477. /* Remove an entry from the IP routing table. Returns 0 on success, -1
  478.  * if entry was not in table.
  479.  */
  480. int
  481. rt_drop(target,bits)
  482. int32 target;
  483. unsigned int bits;
  484. {
  485.     register struct route *rp;
  486.  
  487.     Rt_cache.route = NULLROUTE;    /* Flush the cache */
  488.  
  489.     if(bits == 0){
  490.         /* Nail the default entry */
  491.         stop_timer(&R_default.timer);
  492.         R_default.iface = NULLIF;
  493.         return 0;
  494.     }
  495.     if(bits > 32)
  496.         bits = 32;
  497.  
  498.     /* Mask off target according to width */
  499.     target &= ~0L << (32-bits);
  500.  
  501.     /* Search appropriate chain for existing entry */
  502.     for(rp = Routes[bits-1][hash_ip(target)];rp != NULLROUTE;rp = rp->next){
  503.         if(rp->target == target)
  504.             break;
  505.     }
  506.     if(rp == NULLROUTE)
  507.         return -1;    /* Not in table */
  508.  
  509.     stop_timer(&rp->timer);
  510.     if(rp->next != NULLROUTE)
  511.         rp->next->prev = rp->prev;
  512.     if(rp->prev != NULLROUTE)
  513.         rp->prev->next = rp->next;
  514.     else
  515.         Routes[bits-1][hash_ip(target)] = rp->next;
  516.  
  517.     free((char *)rp);
  518.     return 0;
  519. }
  520. #ifdef    notdef
  521.  
  522. /* Compute hash function on IP address */
  523. static int16
  524. hash_ip(addr)
  525. register int32 addr;
  526. {
  527.     register int16 ret;
  528.  
  529.     ret = hiword(addr);
  530.     ret ^= loword(addr);
  531.     return (int16)(ret % HASHMOD);
  532. }
  533. #endif
  534. #ifndef    GWONLY
  535. /* Given an IP address, return the MTU of the local interface used to
  536.  * reach that destination. This is used by TCP to avoid local fragmentation
  537.  */
  538. int16
  539. ip_mtu(addr)
  540. int32 addr;
  541. {
  542.     register struct route *rp;
  543.     struct iface *iface;
  544.  
  545.     rp = rt_lookup(addr);
  546.     if(rp == NULLROUTE || rp->iface == NULLIF)
  547.         return 0;
  548.  
  549.     iface = rp->iface;
  550.     if(iface->forw != NULLIF)
  551.         return iface->forw->mtu;
  552.     else
  553.         return iface->mtu;
  554. }
  555. /* Given a destination address, return the IP address of the local
  556.  * interface that will be used to reach it. If there is no route
  557.  * to the destination, pick the first non-loopback address.
  558.  */
  559. int32
  560. locaddr(addr)
  561. int32 addr;
  562. {
  563.     register struct route *rp;
  564.     struct iface *ifp;
  565.  
  566.     if(ismyaddr(addr) != NULLIF)
  567.         return addr;    /* Loopback case */
  568.  
  569.     rp = rt_lookup(addr);
  570.     if(rp != NULLROUTE && rp->iface != NULLIF)
  571.         ifp = rp->iface;
  572.     else {
  573.         /* No route currently exists, so just pick the first real
  574.          * interface and use its address
  575.          */
  576.         for(ifp = Ifaces;ifp != NULLIF;ifp = ifp->next){
  577.             if(ifp != &Loopback && ifp != &Encap)
  578.                 break;
  579.         }
  580.     }
  581.     if(ifp == NULLIF || ifp == &Loopback)
  582.         return 0;    /* No dice */
  583.  
  584.     if(ifp == &Encap){
  585.         /* Recursive call - we assume that there are no circular
  586.          * encapsulation references in the routing table!!
  587.          * (There is a check at the end of rt_add() that goes to
  588.          * great pains to ensure this.)
  589.          */
  590.         return locaddr(rp->gateway);
  591.     }
  592.     if(ifp->forw != NULLIF)
  593.         return ifp->forw->addr;
  594.     else
  595.         return ifp->addr;
  596. }
  597. #endif
  598. /* Look up target in hash table, matching the entry having the largest number
  599.  * of leading bits in common. Return default route if not found;
  600.  * if default route not set, return NULLROUTE
  601.  */
  602. struct route *
  603. rt_lookup(target)
  604. int32 target;
  605. {
  606.     register struct route *rp;
  607.     int bits;
  608.     int32 tsave;
  609.     int32 mask;
  610.  
  611.     /* Examine cache first */
  612.     if(target == Rt_cache.target && Rt_cache.route != NULLROUTE)
  613.         return Rt_cache.route;
  614.  
  615.     tsave = target;
  616.  
  617.     mask = ~0;    /* All ones */
  618.     for(bits = 31;bits >= 0; bits--){
  619.         target &= mask;
  620.         for(rp = Routes[bits][hash_ip(target)];rp != NULLROUTE;rp = rp->next){
  621.             if(rp->target == target){
  622.                 /* Stash in cache and return */
  623.                 Rt_cache.target = tsave;
  624.                 Rt_cache.route = rp;
  625.                 return rp;
  626.             }
  627.         }
  628.         mask <<= 1;
  629.     }
  630.     if(R_default.iface != NULLIF){
  631.         Rt_cache.target = tsave;
  632.         Rt_cache.route = &R_default;
  633.         return &R_default;
  634.     } else
  635.         return NULLROUTE;
  636. }
  637. /* Search routing table for entry with specific width */
  638. struct route *
  639. rt_blookup(target,bits)
  640. int32 target;
  641. unsigned int bits;
  642. {
  643.     register struct route *rp;
  644.  
  645.     if(bits == 0){
  646.         if(R_default.iface != NULLIF)
  647.             return &R_default;
  648.         else
  649.             return NULLROUTE;
  650.     }
  651.     /* Mask off target according to width */
  652.     target &= ~0L << (32-bits);
  653.  
  654.     for(rp = Routes[bits-1][hash_ip(target)];rp != NULLROUTE;rp = rp->next){
  655.         if(rp->target == target){
  656.             return rp;
  657.         }
  658.     }
  659.     return NULLROUTE;
  660. }
  661. /* Scan the routing table. For each entry, see if there's a less-specific
  662.  * one that points to the same interface and gateway. If so, delete
  663.  * the more specific entry, since it is redundant.
  664.  */
  665. void
  666. rt_merge(trace)
  667. int trace;
  668. {
  669.     int bits,i,j;
  670.     struct route *rp,*rpnext,*rp1;
  671.  
  672.     for(bits=32;bits>0;bits--){
  673.         for(i = 0;i<HASHMOD;i++){
  674.             for(rp = Routes[bits-1][i];rp != NULLROUTE;rp = rpnext){
  675.                 rpnext = rp->next;
  676.                 for(j=bits-1;j >= 0;j--){
  677.                     if((rp1 = rt_blookup(rp->target,j)) != NULLROUTE
  678.                      && rp1->iface == rp->iface
  679.                      && rp1->gateway == rp->gateway){
  680.                         if(trace > 1)
  681.                             printf("merge %s %d\n",
  682.                              inet_ntoa(rp->target),
  683.                              rp->bits);
  684.                         rt_drop(rp->target,rp->bits);
  685.                         break;
  686.                     }
  687.                 }
  688.             }
  689.         }
  690.     }
  691. }
  692.