Skip to contentSkip to conversation

PPTP connection tracking

PATCH Paul Mielke 3 participants 5 messages Jan 19, 2003 – Feb 18, 2003

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Greetings netfilter-devel denizens,

I have taken the PPTP connection tracking patch version 1.11 and applied it to the 2.4.20 kernel's version of netfilter. I then applied Philip Craig's subsequent patch to get his fixes. I would like to express my appreciation for the excellent work done by Philip, Harald and the other folks who have contributed to the PPTP/GRE connection tracking implementation.

In the course of getting this to work on my target systems, it became clear that our situation differs from those of the previous developers in the following respects:

1) In our case, we need to support the PPTP server running on the firewall machine itself, as well as the pass-through cases.

2) At least one of our platforms is big-endian.

The change required to make 1) work is pretty straightforward. Case 2) is a bit uglier.

The thing that prevented case 1) from working is the following fragment in the routine ip_nat_fn() in ip_nat_standalone.c starting around line 111:

case IP_CT_NEW:

#ifdef CONFIG_IP_NF_NAT_LOCAL
                 /* LOCAL_IN hook doesn't have a chain and thus doesn't care
                  * about new packets -HW */
                 if (hooknum == NF_IP_LOCAL_IN)
                         return NF_ACCEPT;

#endif

The result of this code in the CONFIG_IP_NF_NAT_LOCAL case is that the rest of the NAT initialization code gets skipped for a LOCAL_IN packet. This means that one direction of the manipulations doesn't get set up. The way I think about it is that hook NF_IP_LOCAL_IN is the equivalent of NF_POST_ROUTING for an INPUT packet. The DST manip gets initialized in NF_PRE_ROUTING, but if you skip this code for LOCAL_IN then the SRC manip doesn't get initialized on the first inbound packet. By the time the reply is sent, the connection entry is already hashed and it's too late to mess with it. My solution was just to turn the

#ifdef CONFIG_IP_NF_NAT_LOCAL

into

#if 0

thus removing this section of code. Things seem to work fine after this change, but perhaps there are other cases that I don't understand in which this change would break things. Harald, do you see any harm in this solution?

The endianness issue is a good deal uglier. The root cause of the problem is that the PPTP conntrack patch adds a 32 bit element to the union ip_conntrack_manip_proto and also to the corresponding destination portion of ip_conntrack_tuple, making them both a mix of 16 bit and 32 bit elements. There are a number of places in the netfilter code that use structure initializers to set a value for one of these structures and it now does the wrong thing on a big- endian machine. In fact, I claim it also does a more subtly incorrect thing on a little-endian machine. Consider the following example from ftp_nat_expected() in ip_nat_ftp.c:

         /* ... unless we're doing a MANIP_DST, in which case, make
            sure we map to the correct port */
         if (HOOK2MANIP(hooknum) == IP_NAT_MANIP_DST) {
                 mr.range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
                 mr.range[0].min = mr.range[0].max
                         = ((union ip_conntrack_manip_proto)
                                 { htons(exp_ftp_info->port) });

The structure "mr" is (struct ip_nat_multi_range) and is declared on the stack and never explicitly cleared. The code generated by the compiler we're using (gcc 2.95.2) does a 16 bit store word into the address of the min and max elements. This puts the data in the right place on an LE cpu, but in the wrong place on a BE cpu. On an LE cpu, the upper 16 bits of the union remain unitialized, which may also cause problems depending on how the union is referenced later.

Unfortunately this idiom exists in a number of places. I'm in the process of looking for more, but I have already found and fixed two bugs caused by this endianness issue. Diffs are attached below.

Comments?

Regards,
Paul Mielke
BroadOn Communications Corp.

net/ipv4/netfilter/ip_nat_ftp.c +4 -3
diff -u netfilter-2.4.20_philpatch/net/ipv4/netfilter/ip_nat_ftp.c netfilter_pptfixes/net/ipv4/netfilter/ip_nat_ftp.c
@@ -47,6 +47,7 @@
  
  	DEBUGP("nat_expected: We have a connection!\n");
  	exp_ftp_info = &ct->master->help.exp_ftp_info;
+	memset((void *)&mr, 0, sizeof (struct ip_nat_multi_range));
  
  	LOCK_BH(&ip_ftp_lock);
  
@@ -82,9 +83,8 @@
  	   sure we map to the correct port */
  	if (HOOK2MANIP(hooknum) == IP_NAT_MANIP_DST) {
  		mr.range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
-		mr.range[0].min = mr.range[0].max
-			= ((union ip_conntrack_manip_proto)
-				{ htons(exp_ftp_info->port) });
+		mr.range[0].min.tcp.port = mr.range[0].max.tcp.port =
+			htons(exp_ftp_info->port);
  	}
  	return ip_nat_setup_info(ct, &mr, hooknum);
  }
@@ -182,6 +182,7 @@
  	DEBUGP("FTP_NAT: seq %u + %u in %u\n",
  	       expect->seq, ct_ftp_info->len,
  	       ntohl(tcph->seq));
+	memset((void *)&newtuple, 0, sizeof(struct ip_conntrack_tuple));
  
  	/* Change address inside packet to match way we're mapping
  	   this connection. */
net/ipv4/netfilter/ip_conntrack_core.c +8 -3
diff -u netfilter-2.4.20_philpatch/net/ipv4/netfilter/ip_conntrack_core.c netfilter_pptp_fixes/net/ipv4/netfilter/ip_conntrack_core.c
@@ -1302,9 +1302,14 @@
  getorigdst(struct sock *sk, int optval, void *user, int *len)
  {
  	struct ip_conntrack_tuple_hash *h;
-	struct ip_conntrack_tuple tuple = { { sk->rcv_saddr, { sk->sport } },
-					    { sk->daddr, { sk->dport },
-					      IPPROTO_TCP } };
+	struct ip_conntrack_tuple tuple;
+
+	memset((void *)&tuple, 0, sizeof (struct ip_conntrack_tuple));
+	tuple.src.ip = sk->rcv_saddr;
+	tuple.src.u.tcp.port = sk->sport;
+	tuple.dst.ip = sk->daddr;
+	tuple.dst.u.tcp.port = sk->dport;
+	tuple.dst.protonum = IPPROTO_TCP;
  
  	/* We only do TCP at the moment: is there a better way? */
  	if (strcmp(sk->prot->name, "TCP") != 0) {

Paul Mielke [email protected]
RouteFree, Inc. (650) 739-5377

#1 lore raw
Quoted 11 lines · Paul Mielke

The thing that prevented case 1) from working is the following fragment in the routine ip_nat_fn() in ip_nat_standalone.c starting around line 111:

case IP_CT_NEW:

#ifdef CONFIG_IP_NF_NAT_LOCAL
                 /* LOCAL_IN hook doesn't have a chain and thus doesn't care
                  * about new packets -HW */
                 if (hooknum == NF_IP_LOCAL_IN)
                         return NF_ACCEPT;

#endif

you are definitely correct, althought your solution is problematic. In the case NAT_LOCAL is enabled, we are still not allowed to call ip_nat_rule_find, because this in turn calls ipt_do_table()... but there is no chain attached to the LOCAL_IN hook of the 'nat' table, and bad things start to happen.

I have put a proposed patch into CVS:
(patch-o-matic/pending/10_local-nat-expectfn.patch)

This patch rather moves the particular piece of code a couple of lines down, so we assure that an expectfn (if it exists) gets called.

Can you please test if your setup works with this patch?

Quoted 2 lines

Paul Mielke [email protected]
RouteFree, Inc. (650) 739-5377

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
#2 lore raw

Hi Harald,

Quoted 7 lines · Harald Welte

I have put a proposed patch into CVS:
(patch-o-matic/pending/10_local-nat-expectfn.patch)

This patch rather moves the particular piece of code a couple of lines down, so we assure that an expectfn (if it exists) gets called.

Can you please test if your setup works with this patch?

Paul and I have tested that the attached patch fixes the problem. It is similar to yours, except that it returns earlier. I don't think the calls to place_in_hashes() and do_bindings() are necessary if we just need to return NF_ACCEPT without doing any NAT?

Regards,

Signature · 2 lines
Philip Craig - [email protected] - http://www.SnapGear.com
SnapGear - Custom Embedded Solutions and Security Appliances
net/ipv4/netfilter/ip_nat_standalone.c +8 -6
diff -u -r1.3 ip_nat_standalone.c
@@ -109,12 +109,6 @@
 		}
 		/* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
 	case IP_CT_NEW:
-#ifdef CONFIG_IP_NF_NAT_LOCAL
-		/* LOCAL_IN hook doesn't have a chain and thus doesn't care
-		 * about new packets -HW */
-		if (hooknum == NF_IP_LOCAL_IN)
-			return NF_ACCEPT;
-#endif
 		info = &ct->nat.info;
 
 		WRITE_LOCK(&ip_nat_lock);
@@ -130,6 +124,14 @@
 				ret = call_expect(master_ct(ct), pskb, 
 						  hooknum, ct, info);
 			} else {
+#ifdef CONFIG_IP_NF_NAT_LOCAL
+				/* LOCAL_IN hook doesn't have a chain and thus
+				 * doesn't care about new packets -HW */
+				if (hooknum == NF_IP_LOCAL_IN) {
+					WRITE_UNLOCK(&ip_nat_lock);
+					return NF_ACCEPT;
+				}
+#endif
 				ret = ip_nat_rule_find(pskb, hooknum, in, out,
 						       ct, info);
 			}
#3 lore raw
Quoted 7 lines · Philip Craig
Quoted 1 line

Can you please test if your setup works with this patch?

Paul and I have tested that the attached patch fixes the problem. It is similar to yours, except that it returns earlier. I don't think the calls to place_in_hashes() and do_bindings() are necessary if we just need to return NF_ACCEPT without doing any NAT?

yes, but I can imagine the case where somebody actually _does_ want to do some nat from the expectfn() - and we should keep that option open...

Quoted 1 line

Philip Craig - [email protected] - http://www.SnapGear.com

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
#4 lore raw
Quoted 11 lines · Harald Welte
Quoted 7 lines · Philip Craig
Quoted 1 line

Can you please test if your setup works with this patch?

Paul and I have tested that the attached patch fixes the problem. It is similar to yours, except that it returns earlier. I don't think the calls to place_in_hashes() and do_bindings() are necessary if we just need to return NF_ACCEPT without doing any NAT?

Quoted 2 lines

yes, but I can imagine the case where somebody actually _does_ want to do some nat from the expectfn() - and we should keep that option open...

That is exactly the problem this patch is solving. We want to do NAT in expectfn() for PPTP, but the #ifdef for local NAT was too early and expectfn() wasn't being called at all. So this patch moves the #ifdef into a code path that isn't followed if expectfn() needs to be called.

Signature · 2 lines
Philip Craig - [email protected] - http://www.SnapGear.com
SnapGear - Custom Embedded Solutions and Security Appliances