Skip to contentSkip to conversation

design-feature of nat?

RFC PATCH In review juergen baumann 4 participants 8 messages Nov 1, 2002 – Nov 5, 2002 2 messages not in the archive

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

possible design-problem within snat.

connecting with certain source-port to any destination. normal stop of this connection.

establishing a snat-rule which replaces the address used by the connection before.

conencting again with same source-port to same destination.

neither the established snat-rule will be used, nor any further packet-counter will be increased.

problem will not occure, if the corresponding conntrack-entry is deleted before.

to fix this problem I've patched ip_nat_rule.c

the patch is rather a quick hack than a distributable patch, cause the comparisons taken, aren't done with enough precision to prevent stopping established connections, not touched by the changed rules. but it helps for now for our certain local problem and maybe is a possible start for a real solution.

the patch itself:
=====================================================================================================

ipv4/netfilter/ip_nat_rule.c renamed from ipv4/netfilter/ip_nat_rule.c.dist +102 -2
@@ -155,6 +155,22 @@ static unsigned int ipt_dnat_target(stru
 	return ip_nat_setup_info(ct, targinfo, hooknum);
 }
 
+static int ipt_nat_cmpiptip(const struct ip_conntrack *i, void *iptip)
+{
+	struct ipt_ip *ip = iptip;
+	u_int32_t sip, dip;
+
+	sip = i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
+	dip = i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
+
+	if (((sip & ip->smsk.s_addr) == ip->src.s_addr) && ((dip & ip->dmsk.s_addr) == ip->dst.s_addr)) {
+		DEBUGP("SNAT: ctentry from %u.%u.%u.%u to %u.%u.%u.%u matches rule SRC: %u.%u.%u.%u/%u.%u.%u.%u DST: %u.%u.%u.%u/%u.%u.%u.%u\n",
+			NIPQUAD(sip), NIPQUAD(dip), NIPQUAD(ip->src.s_addr), NIPQUAD(ip->smsk.s_addr), NIPQUAD(ip->dst.s_addr), NIPQUAD(ip->dmsk.s_addr));
+		return 1;
+	}
+	return 0;
+}
+
 static int ipt_snat_checkentry(const char *tablename,
 			       const struct ipt_entry *e,
 			       void *targinfo,
@@ -187,9 +203,51 @@ static int ipt_snat_checkentry(const cha
 		DEBUGP("SNAT: hook mask 0x%x bad\n", hook_mask);
 		return 0;
 	}
+	if (e)
+		ip_ct_selective_cleanup(ipt_nat_cmpiptip, &(e->ip));
 	return 1;
 }
 
+static int ipt_snat_cmptinfo(const struct ip_conntrack *i, void *targetinfo)
+{
+	struct ip_nat_multi_range *mr = targetinfo;	/* validity already checked in ipt_snat_destroyentry() */
+	u_int32_t ctip, minip, maxip;
+	int n;
+
+	ctip = ntohl(i->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip);
+
+	for (n = 0; n < mr->rangesize; n++) {
+		minip = ntohl(mr->range[n].min_ip);
+		maxip = ntohl(mr->range[n].max_ip);
+		if ((ctip >= minip) && (ctip <= maxip)) {
+			DEBUGP("SNAT: %u.%u.%u.%u is within range[%d] %u.%u.%u.%u - %u.%u.%u.%u \n",
+				HIPQUAD(ctip), n, HIPQUAD(minip), HIPQUAD(maxip));
+			return 1;
+		}
+	}
+	return 0;
+}
+
+static void ipt_snat_destroyentry(void *targetinfo, unsigned int targetinfosize)
+{
+	struct ip_nat_multi_range *mr = targetinfo;
+
+	/* Must be a valid range */
+	if (targetinfosize < sizeof(struct ip_nat_multi_range)) {
+		DEBUGP("SNAT: (destroy) Target size %u too small\n", targetinfosize);
+		return;
+	}
+
+	if (targetinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
+				       + (sizeof(struct ip_nat_range)
+					  * (mr->rangesize - 1))))) {
+		DEBUGP("SNAT: (destroy) Target size %u wrong for %u ranges\n",
+		       targetinfosize, mr->rangesize);
+		return;
+	}
+	ip_ct_selective_cleanup(ipt_snat_cmptinfo, targetinfo);
+}
+
 static int ipt_dnat_checkentry(const char *tablename,
 			       const struct ipt_entry *e,
 			       void *targinfo,
@@ -230,9 +288,51 @@ static int ipt_dnat_checkentry(const cha
 	}
 #endif
 
+	if (e)
+		ip_ct_selective_cleanup(ipt_nat_cmpiptip, &(e->ip));
 	return 1;
 }
 
+static int ipt_dnat_cmptinfo(const struct ip_conntrack *i, void *targetinfo)
+{
+	struct ip_nat_multi_range *mr = targetinfo;	/* validity already checked in ipt_snat_destroyentry() */
+	u_int32_t ctip, minip, maxip;
+	int n;
+
+	ctip = ntohl(i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip);
+
+	for (n = 0; n < mr->rangesize; n++) {
+		minip = ntohl(mr->range[n].min_ip);
+		maxip = ntohl(mr->range[n].max_ip);
+		if ((ctip >= minip) && (ctip <= maxip)) {
+			DEBUGP("DNAT: %u.%u.%u.%u is within range[%d] %u.%u.%u.%u - %u.%u.%u.%u \n",
+				HIPQUAD(ctip), n, HIPQUAD(minip), HIPQUAD(maxip));
+			return 1;
+		}
+	}
+	return 0;
+}
+
+static void ipt_dnat_destroyentry(void *targetinfo, unsigned int targetinfosize)
+{
+	struct ip_nat_multi_range *mr = targetinfo;
+
+	/* Must be a valid range */
+	if (targetinfosize < sizeof(struct ip_nat_multi_range)) {
+		DEBUGP("DNAT: (destroy) Target size %u too small\n", targetinfosize);
+		return;
+	}
+
+	if (targetinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
+				       + (sizeof(struct ip_nat_range)
+					  * (mr->rangesize - 1))))) {
+		DEBUGP("DNAT: (destroy) Target size %u wrong for %u ranges\n",
+		       targetinfosize, mr->rangesize);
+		return;
+	}
+	ip_ct_selective_cleanup(ipt_dnat_cmptinfo, targetinfo);
+}
+
 static inline unsigned int
 alloc_null_binding(struct ip_conntrack *conntrack,
 		   struct ip_nat_info *info,
@@ -314,9 +417,9 @@ void ip_nat_expect_unregister(struct ip_
 }
 
 static struct ipt_target ipt_snat_reg
-= { { NULL, NULL }, "SNAT", ipt_snat_target, ipt_snat_checkentry, NULL };
+= { { NULL, NULL }, "SNAT", ipt_snat_target, ipt_snat_checkentry, ipt_snat_destroyentry, NULL };
 static struct ipt_target ipt_dnat_reg
-= { { NULL, NULL }, "DNAT", ipt_dnat_target, ipt_dnat_checkentry, NULL };
+= { { NULL, NULL }, "DNAT", ipt_dnat_target, ipt_dnat_checkentry, ipt_dnat_destroyentry, NULL };
 
 int __init ip_nat_rule_init(void)
 {

=====================================================================================================

maybe you can fix a small bug in iptables-user, too:

in iptables.c (iptables6.c)

the long-option "rename-chain" should have a second arg of '1', not '2', or the option will fail. short option 'E' does work normally.

greetings

jb

Signature · 2 lines
26.hesekiel/31/10.txt (19396)
Darum - so spricht Gott der HERR: Weil er so hoch geworden war, daß sein Wipfel bis in die Wolken ragte, und weil sein Herz sich erhob, da er so hoch geworden war,
#1 lore raw

On Fri, Nov 01, 2002 at 11:52:45PM +0100, juergen baumann wrote:

Hi Juergen.

Quoted 8 lines

possible design-problem within snat.

connecting with certain source-port to any destination. normal stop of this connection.

establishing a snat-rule which replaces the address used by the connection before.

Quoted 7 lines

conencting again with same source-port to same destination.

neither the established snat-rule will be used, nor any further packet-counter will be increased.

problem will not occure, if the corresponding conntrack-entry is deleted before.

I don't really understand the problem. Are you talking about SNAT not occurring as long as the old conntrack entry is still in TIME_WAIT state?

Please describe it a bit more verbose (you can also write directly to me and use german language, if you want to).

Quoted 6 lines

maybe you can fix a small bug in iptables-user, too:

in iptables.c (iptables6.c)

the long-option "rename-chain" should have a second arg of '1', not '2', or the option will fail. short option 'E' does work normally.

thanks, fixed.

Quoted 2 lines

greetings
jb

Signature · 5 lines
Live long and prosper
- Harald Welte / [email protected]               http://www.gnumonks.org/
============================================================================
"If this were a dictatorship, it'd be a heck of a lot easier, just so long
 as I'm the dictator."  --  George W. Bush Dec 18, 2000

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 11 lines · juergen baumann

recently Harald Welte said ...

Quoted 2 lines

I don't really understand the problem. Are you talking about SNAT not occurring as long as the old conntrack entry is still in TIME_WAIT state?

right. that's the problem.

unfortunately some applications use the same source-port, so any *new* connection, which will be established during th TIME_WAIT-state will be handled by the old configuration.

Yes, this is true. I've now read the respective RFC's again, and RFC793 states:

Quoted 5 lines

TIME-WAIT STATE

The only thing that can arrive in this state is a retransmission of the remote FIN. Acknowledge it, and restart the 2 MSL timeout.

So we only need to use the old conntrack entry in case of FIN or FIN/ACK packets, and create new conntrack entries if the packet establishes a new connection (SYN, ..)

But then there is also another issue which we need to take into account (excerpt from RFC2993):

Quoted 11 lines

For address translators (which do not translate ports) to comply with the TCP_TIME_WAIT requirements, they must refrain from assigning the same address to a different host until a period of 2*MSL has elapsed since the last use of the address, where MSL is the Maximum Segment Lifetime defined in RFC-793 as two minutes. For address-and-port translators to comply with this requirement, they similarly must refrain from assigning the same host/port pair until 2*MSL has elapsed since the end of its first use. While these requirements are simple to state, they can place a great deal of pressure on the NAT, because they temporarily reduce the pool of available addresses and ports.

This is ensured by the current configuration, since we cannot use the same tuple until the old TIME_WAIT conntrack is gone. Is this still valid with your proposed change(s)?

Quoted 4 lines

the other problem (which is not fixed by my patch) is, that while such a conntrack-entry exists no new connection triggers the counter of such a rule nor a LOG-entry will be created, if one is prepended to the SNAT-rule. the packets pass the whole table invisible...

Well, since the packet is considered to be part of an already ESTABLISHED connection, it will use the rules you have configured for ESTABLISHED packets.

Quoted 1 line

I hope, it's now more clear, what I talk about.

Yes, thanks for clarification.

Quoted 2 lines

greetings
jb

Signature · 5 lines
Live long and prosper
- Harald Welte / [email protected]               http://www.gnumonks.org/
============================================================================
"If this were a dictatorship, it'd be a heck of a lot easier, just so long
 as I'm the dictator."  --  George W. Bush Dec 18, 2000
#4 lore raw

RFC PATCH 1/1 ipt_REJECT.c - nf_bridge source and

Hello,

A discussion on the Linux bridge mailing list showed the following problems:
1. send_reset() does a skb_copy() and doesn't yet initialize the nf_bridge skbuff field.
2. send_unreach() seems to copy the wrong source IP address in the ICMP message. Namely, it seems to always use an IP address of the router as source IP address. Is that a bug?

The following patch solves both problems.
If the second issue is a bug then the 2.4 kernel needs a fix too.

cheers,
Bart

net/ipv4/netfilter/ipt_REJECT.c +8 -1
@@ -76,6 +76,10 @@
 	nskb->nf_debug = 0;
 #endif
 	nskb->nfmark = 0;
+#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
+	nf_bridge_put(nskb->nf_bridge);
+	nskb->nf_bridge = NULL;
+#endif
 
 	tcph = (struct tcphdr *)((u_int32_t*)nskb->nh.iph + nskb->nh.iph->ihl);
 
@@ -222,6 +226,9 @@
 		if (ip_route_output_key(&rt, &fl))
 			return;
 	}
+
+	saddr = iph->daddr;
+
 	/* RFC says return as much as we can without exceeding 576 bytes. */
 	length = skb_in->len + sizeof(struct iphdr) + sizeof(struct icmphdr);
 
@@ -256,7 +263,7 @@
 	iph->ttl = MAXTTL;
 	ip_select_ident(iph, &rt->u.dst, NULL);
 	iph->protocol=IPPROTO_ICMP;
-	iph->saddr=rt->rt_src;
+	iph->saddr=saddr;
 	iph->daddr=rt->rt_dst;
 	iph->check=0;
 	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
#5 lore raw

Re: [PATCH][RFC] ipt_REJECT.c - nf_bridge source and

Quoted 5 lines · Bart De Schuymer

Hello,

A discussion on the Linux bridge mailing list showed the following problems:
1. send_reset() does a skb_copy() and doesn't yet initialize the nf_bridge skbuff field.

we can add that to ipt_REJECT, no problem.

Quoted 3 lines

2. send_unreach() seems to copy the wrong source IP address in the ICMP message. Namely, it seems to always use an IP address of the router as source IP address. Is that a bug?

no, this is considered to be a feature. IIRC, this has been discussed on the list before. The router, after all, has gereated the ICMP packet ;)

In case of NAT, the nat code will take care of replacing the IP address.

Quoted 1 line

The following patch solves both problems.

I will put the first in my list of pending patches to davem.

Quoted 2 lines

cheers,
Bart

Signature · 5 lines
Live long and prosper
- Harald Welte / [email protected]               http://www.gnumonks.org/
============================================================================
"If this were a dictatorship, it'd be a heck of a lot easier, just so long
 as I'm the dictator."  --  George W. Bush Dec 18, 2000
#6 lore raw

[RFC] ipt_REJECT.c - option to use different ICMP source address

Hello netfilter team,

Since the bridge-netfilter code is in the 2.5 kernel, Linux networking life gets some new interesting problems.

When a bridged IP packet is forwarded (inside the bridge code) it is seen by the iptables FORWARD chains.
There the REJECT target can be used. It seems most natural that the ICMP message sent by this target should have the IP destination address of the original message, as the IP source address of the ICMP message. This should be the normal behaviour for a bridging firewall (as pointed out by Pekka Savola) because first of all the bridge device could have no IP, and secondly, the IP message wasn't destined for the bridging box (on the MAC level) so it is not good to have its IP address as source address by default.

So, if I were to implement code inside ipt_REJECT.c (for 2.5) that checks if the packet is being bridged and we are on the NF_IP_FORWARD hook. And if so, it uses the destination address of the original packet as the source address of the ICMP message. Would this then be accepted (if it looks ok)? Ofcourse, this code would only be compiled if the bridge is enabled.

One can then use SNAT to force the IP address of the bridge instead of this default behaviour, if needed.

Signature · 2 lines
cheers,
Bart
#7 lore raw

Re: [RFC] ipt_REJECT.c - option to use different ICMP source address

Hi Bart,

Dans un message du 04 Nov à 23:43, Bart De Schuymer écrivait :

Quoted 6 lines

So, if I were to implement code inside ipt_REJECT.c (for 2.5) that checks if the packet is being bridged and we are on the NF_IP_FORWARD hook. And if so, it uses the destination address of the original packet as the source address of the ICMP message. Would this then be accepted (if it looks ok)? Ofcourse, this code would only be compiled if the bridge is enabled.

Something in the patch-o-matic already does that. Look at
http://www.netfilter.org/documentation/pomlist/pom-userspace.html#ipt_REJECT-fake-source

I guess you would just need to slightly modify it.

Regards,

Signature · 3 lines
Guillaume Morin <[email protected]>

                       Pastis servi, pastis bu (Patrice)

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw
Quoted 2 lines · juergen baumann

well, I see: I've to read some more RFCs next time, before wondering what happens there...

;) no problem, it's sometimes not easy to find the precise documents that apply to a particular case, unless you've read them before and remember there was a particular precondition.

Quoted 5 lines
Quoted 3 lines

This is ensured by the current configuration, since we cannot use the same tuple until the old TIME_WAIT conntrack is gone. Is this still valid with your proposed change(s)?

not under all circumstances...

that's a problem

Quoted 3 lines

a tcpdump on eth0 shows, that the deleted snat-rule seems still to be active, the src-address of the outgoing packets is 1.2.3.4, not 4.3.2.1 as defined by the new snat-rule.

the snat rule is not active. The NAT binding assigned by the SNAT rule for this connection is still active.

Quoted 1 line

is this necessary due to the RFCs?

yes and no. It is necessarry for FIN and FIN/ACK packets, but not for SYN packets. The problem is that we lookup connection tracking entries only by [srcip:srcport,dstip:dstport] tuples, not by TCP flags. [tcp flags are completely hidden by the layer 4 conntrack protocol helper, not known to the conntrack core at all.

So either we comply with the RFC's (and have the strange side-effect you are describing), _or_ we don't comply with the RFC's and avoid the side-effect.

Quoted 6 lines
Quoted 3 lines

Well, since the packet is considered to be part of an already ESTABLISHED connection, it will use the rules you have configured for ESTABLISHED packe= ts.

mmmh. there's no such rule in table nat (but in table filter). does it mean, that the packet/byte-counter of a snat-rule only increase on session-startup?

yes. the chains of a nat table are only traversed for the connection-initiating (NEW) packets. The nat mapping is then saved with the conntrack entry and all other packets within the connection are manipulated according to this nat mapping.

Quoted 2 lines

it seems, that the whole POSTROUTING-tables does not see the packages of an established connection at all...

no. only the POSTROUTING chain (and all others) of nat. This is documented in the HOWTOS.

Quoted 2 lines

greetings
jürgen

Signature · 5 lines
Live long and prosper
- Harald Welte / [email protected]               http://www.gnumonks.org/
============================================================================
"If this were a dictatorship, it'd be a heck of a lot easier, just so long
 as I'm the dictator."  --  George W. Bush Dec 18, 2000