I've looked at that section of code and come up with the following
patch, given your insights...

Darren

Index: ip_state.c
===================================================================
RCS file: /devel/CVS/IP-Filter/ip_state.c,v
retrieving revision 2.30.2.84
diff -c -r2.30.2.84 ip_state.c
*** ip_state.c  7 Jun 2003 11:55:55 -0000       2.30.2.84
--- ip_state.c  16 Nov 2003 05:09:37 -0000
***************
*** 1210,1215 ****
--- 1210,1219 ----
  
        oip = (ip_t *)((char *)ic + ICMPERR_ICMPHLEN);
        ohlen = oip->ip_hl << 2;
+       /*
+        * Check if the at least the old IP header (with options) and
+        * 8 bytes of payload is present.
+        */
        if (fin->fin_plen < ICMPERR_MAXPKTLEN + ohlen - sizeof(*oip))
                return NULL;
  
***************
*** 1253,1266 ****
        bzero((char *)&src, sizeof(src));
        bzero((char *)&dst, sizeof(dst));
        bzero((char *)&ofin, sizeof(ofin));
!       ofin.fin_ifp = fin->fin_ifp;
!       ofin.fin_out = !fin->fin_out;
!       ofin.fin_mp = NULL;
        ofin.fin_v = 4;
!       fr = NULL;
  #if SOLARIS
        ofin.fin_qfm = NULL;
  #endif
  
        switch (oip->ip_p)
        {
--- 1257,1296 ----
        bzero((char *)&src, sizeof(src));
        bzero((char *)&dst, sizeof(dst));
        bzero((char *)&ofin, sizeof(ofin));
!       /*
!        * We make an fin entry to be able to feed it to
!        * matchsrcdst.  Note that not all fields are encessary
!        * but this is the cleanest way. Note further that we
!        * fill in fin_mp such that if someone uses it we'll get
!        * a kernel panic. fr_matchsrcdst does not use this.
!        *
!        * watch out here, as ip is in host order and oip in network
!        * order. Any change we make must be undone afterwards, like
!        * oip->ip_off - it is still in network byte order so fix it.
!        */
!       savelen = oip->ip_len;
!       oip->ip_len = len;
!       oip->ip_off = ntohs(oip->ip_off);
        ofin.fin_v = 4;
!       (void) fr_makefrip(ohlen, oip, &ofin);
!       /*
!        * Reset the short flag here because in fr_matchsrcdst() the flags
!        * for the current packet (fin_fl) are compared against * those for
!        * the existing session.
!        */
!       ofin.fin_fl &= ~FI_SHORT;
! 
!       /*
!        * Put old values of ip_len and ip_off back as we don't know
!        * if we have to forward the packet (or process it again.
!        */
!       oip->ip_len = savelen;
!       oip->ip_off = htons(oip->ip_off);
! 
  #if SOLARIS
        ofin.fin_qfm = NULL;
  #endif
+       fr = NULL;
  
        switch (oip->ip_p)
        {
***************
*** 1292,1309 ****
                hv += icmp->icmp_seq;
                hv %= fr_statesize;
  
-               savelen = oip->ip_len;
-               oip->ip_len = len;
-               fr_makefrip(ohlen, oip, &ofin);
-               oip->ip_len = savelen;
- 
                READ_ENTER(&ipf_state);
                for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext)
                        if ((is->is_p == pr) && (is->is_v == 4) &&
                            fr_matchsrcdst(is, src, dst, &ofin, NULL) &&
!                           fr_matchicmpqueryreply(is->is_v, is, icmp, fin->fin_rev)) {
                                ips_stats.iss_hits++;
!                               is->is_pkts++;
                                is->is_bytes += ip->ip_len;
                                fr = is->is_rule;
                                break;
--- 1322,1336 ----
                hv += icmp->icmp_seq;
                hv %= fr_statesize;
  
                READ_ENTER(&ipf_state);
                for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext)
                        if ((is->is_p == pr) && (is->is_v == 4) &&
+                           (is->is_icmppkts < is->is_pkts) &&
                            fr_matchsrcdst(is, src, dst, &ofin, NULL) &&
!                           fr_matchicmpqueryreply(is->is_v, is, icmp,
!                                                  fin->fin_rev)) {
                                ips_stats.iss_hits++;
!                               is->is_icmppkts++;
                                is->is_bytes += ip->ip_len;
                                fr = is->is_rule;
                                break;
***************
*** 1332,1351 ****
        hv += dport;
        hv += sport;
        hv %= fr_statesize;
!       /*
!        * we make an fin entry to be able to feed it to
!        * matchsrcdst note that not all fields are encessary
!        * but this is the cleanest way. Note further we fill
!        * in fin_mp such that if someone uses it we'll get
!        * a kernel panic. fr_matchsrcdst does not use this.
!        *
!        * watch out here, as ip is in host order and oip in network
!        * order. Any change we make must be undone afterwards.
!        */
!       savelen = oip->ip_len;
!       oip->ip_len = len;
!       fr_makefrip(ohlen, oip, &ofin);
!       oip->ip_len = savelen;
        READ_ENTER(&ipf_state);
        for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
                /*
--- 1359,1365 ----
        hv += dport;
        hv += sport;
        hv %= fr_statesize;
! 
        READ_ENTER(&ipf_state);
        for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
                /*
***************
*** 1353,1365 ****
                 * encapsulated packet was allowed through the
                 * other way around. Note that the minimal amount
                 * of info present does not allow for checking against
!                * tcp internals such as seq and ack numbers.
                 */
                if ((is->is_p == pr) && (is->is_v == 4) &&
                    fr_matchsrcdst(is, src, dst, &ofin, tcp)) {
                        fr = is->is_rule;
                        ips_stats.iss_hits++;
!                       is->is_pkts++;
                        is->is_bytes += fin->fin_plen;
                        /*
                         * we deliberately do not touch the timeouts
--- 1367,1382 ----
                 * encapsulated packet was allowed through the
                 * other way around. Note that the minimal amount
                 * of info present does not allow for checking against
!                * tcp internals such as seq and ack numbers.  Only the
!                * ports are known to be present and can be even if the
!                * short flag is set.
                 */
                if ((is->is_p == pr) && (is->is_v == 4) &&
+                   (is->is_icmppkts < is->is_pkts) &&
                    fr_matchsrcdst(is, src, dst, &ofin, tcp)) {
                        fr = is->is_rule;
                        ips_stats.iss_hits++;
!                       is->is_icmppkts++;
                        is->is_bytes += fin->fin_plen;
                        /*
                         * we deliberately do not touch the timeouts
***************
*** 2086,2092 ****
        int types[1];
  
        ipsl.isl_type = type;
!       ipsl.isl_pkts = is->is_pkts;
        ipsl.isl_bytes = is->is_bytes;
        ipsl.isl_src = is->is_src;
        ipsl.isl_dst = is->is_dst;
--- 2103,2109 ----
        int types[1];
  
        ipsl.isl_type = type;
!       ipsl.isl_pkts = is->is_pkts + is->is_icmppkts;
        ipsl.isl_bytes = is->is_bytes;
        ipsl.isl_src = is->is_src;
        ipsl.isl_dst = is->is_dst;


Reply via email to