From nobody@FreeBSD.org  Thu Aug  5 15:42:46 2004
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 5782116A4CE
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  5 Aug 2004 15:42:46 +0000 (GMT)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 3D9CB43D73
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  5 Aug 2004 15:42:46 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.12.11/8.12.11) with ESMTP id i75FgkZI097353
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 5 Aug 2004 15:42:46 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.12.11/8.12.11/Submit) id i75FgkeC097352;
	Thu, 5 Aug 2004 15:42:46 GMT
	(envelope-from nobody)
Message-Id: <200408051542.i75FgkeC097352@www.freebsd.org>
Date: Thu, 5 Aug 2004 15:42:46 GMT
From: Ming Fu <fming@borderware.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: ipfilter rdr rule does not work for non-TCP/UDP protocols
X-Send-Pr-Version: www-2.3

>Number:         70038
>Category:       kern
>Synopsis:       ipfilter rdr rule does not work for non-TCP/UDP protocols
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    darrenr
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Aug 05 15:50:24 GMT 2004
>Closed-Date:    Fri Dec 17 02:30:27 GMT 2004
>Last-Modified:  Fri Dec 17 02:30:27 GMT 2004
>Originator:     Ming Fu
>Release:        4.10 and 4.7
>Organization:
Borderware Technologies Ltd
>Environment:
FreeBSD bsd410.borderware.com 4.10-RELEASE FreeBSD 4.10-RELEASE #0: Tue May 25 22:47:12 GMT 2004      root@perseus.cse.buffalo.edu:/usr/obj/src/sys/GENERIC i386
>Description:
ipfilter redirect rule can not match for non-TCP/UDP protocols if there is specific source destination filter on the rule. The rule matching code in sys/contrib/ipfilter/netinet/ip_nat.c: ip_natin() will always fail the match.     
>How-To-Repeat:
On a machine with two interface card:
lnc0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        inet 10.1.19.3 netmask 0xff000000 broadcast 10.255.255.255
        inet6 fe80::20c:29ff:fe90:3a47%lnc0 prefixlen 64 scopeid 0x1
        ether 00:0c:29:90:3a:47
lnc1: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        inet 172.31.19.3 netmask 0xffff0000 broadcast 172.31.255.255
        inet6 fe80::20c:29ff:fe90:3a51%lnc1 prefixlen 64 scopeid 0x2
        ether 00:0c:29:90:3a:51

Apply these two ipfilter rules: you can substitude esp with any protocol other than TCP and UDP.

pass in log quick on lnc1 proto esp from 0.0.0.0/0 to 0.0.0.0/0
rdr lnc1 from 172.31.2.146 to 172.31.19.3 -> 10.1.19.1 port 0 esp

send an esp packet from ip 172.31.2.146 to 172.31.19.3
from ipmon, you can see that the packet hit filter rule without NAT.



>Fix:
In the /usr/src/sys/contrib/ipfilter/netinet/ip_nat.c function ip_natin(), after search through known nat states and find none, it starts to look through the rules. However, because the rdr rule has src/dst restriction, the np->in_flags will have IPN_FILTER bit set, and the nflags will always be zero if the incoming packet is not of TCP and UDP. The test on the line #3 of the if statement will always fail. As far as I know, the line #3 only test if the TCP or UDP protocol matches, which has already tested by the line #2 of the if. The test looked redundant to me. The fix was tested on FreeBSD 4.7

 *** 2631,2638 ****
                hv = NAT_HASH_FN(iph, 0, ipf_rdrrules_sz);
                for (np = rdr_rules[hv]; np; np = np->in_rnext) {
                        if ((np->in_ifp && (np->in_ifp != ifp)) ||
!                           (np->in_p && (np->in_p != fin->fin_p)) ||
!                           (np->in_flags && !(nflags & np->in_flags)))
                                continue;
                        if (np->in_flags & IPN_FILTER) {
                                if (!nat_match(fin, np, ip))
--- 2649,2656 ----
                hv = NAT_HASH_FN(iph, 0, ipf_rdrrules_sz);
                for (np = rdr_rules[hv]; np; np = np->in_rnext) {
                        if ((np->in_ifp && (np->in_ifp != ifp)) ||
!                           (np->in_p && (np->in_p != fin->fin_p))
!                           )
                                continue;
                        if (np->in_flags & IPN_FILTER) {
                                if (!nat_match(fin, np, ip))
    
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->darrenr 
Responsible-Changed-By: simon 
Responsible-Changed-When: Thu Aug 5 16:27:59 GMT 2004 
Responsible-Changed-Why:  
Over ti IPFilter maintainer. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=70038 
State-Changed-From-To: open->closed 
State-Changed-By: darrenr 
State-Changed-When: Fri Dec 17 02:24:52 GMT 2004 
State-Changed-Why:  
patch applied to RELENG_4, RELENG_5 and -current. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=70038 
>Unformatted:
