Skip to contentSkip to conversation

Getting IPVS and Netfilter SNAT to play together

DISCUSSION Julius Volz 2 participants 7 messages Sep 23, 2008 – Sep 24, 2008

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

Hi,

I already mentioned this on netdev and lvs-devel a while ago, but it's a pretty specifc Netfilter-internals question, so I might have better luck getting an answer here.

Generally, I was trying to get IPVS to work together with netfilter SNAT. Currently, IPVS actively prevents the packets it manipulates from being processed by Netfilter NAT in POSTROUTING by returning NF_STOP from a POSTROUTING hook.

So I just removed this IPVS hook function that stops the further POSTROUTING processing. I then added a normal SNAT rule to POSTROUTING like this:

$ iptables –t nat –A POSTROUTING –o eth1 –j SNAT –-to <snat_ip>

Without any IPVS, SNAT works without any problems. As expected, only the first (SYN) packet of a connection appears on the POSTROUTING chain.

However, with packets passing first through IPVS, the behavior gets weird:

The first SYN and the SYN/ACK of a TCP connection through IPVS do _not_ traverse the NAT chain in POSTROUTING at all (verified by LOG target, which produces no output), so their source is not rewritten (resulting just in normal IPVS DNAT). However, the client's ACK response to the SYN/ACK _does_ finally appear in this table and only this third packet is SNATed correctly. The backend server then replies with an RST, since it doesn't know a connection from the SNATed source. The packet trace on the director looks like this:

CIP: client IP
VIP: virtual service IP
DIP: director (load balancer) IP

RIP: real server (backend) IP

Packet 1:
=======
CIP => VIP SYN (eth0, before IPVS)
CIP => RIP SYN (eth1, after IPVS)

Packet 2:
=======
RIP => CIP SYN/ACK (eth1, before IPVS)
VIP => CIP SYN/ACK (eth0, after IPVS)

Packet 3:
=======
CIP => VIP ACK (eth0, before IPVS)
DIP => RIP ACK (eth1, after IPVS) - this is the first successfully SNATed packet! Why?

Packet 4:
=======
RIP => DIP RST - real server doesn't know this connection from the DIP

Any ideas why the first packets never appear in the POSTROUTING chain but the ACK does? I do not understand this, since IPVS injects all handled packets into the OUTPUT chain, whereafter they should traverse POSTROUTING... maybe someone with more understanding of Netfilter can shed some light on this?

Thanks,
Julius

Signature · 7 lines
Julius Volz - Corporate Operations - SysOps

Google Switzerland GmbH - Identification No.: CH-020.4.028.116-1
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw
Quoted 3 lines · Julius Volz

Any ideas why the first packets never appear in the POSTROUTING chain but the ACK does?

It is not the first packet of the TCP connection that will show up in the nat table, it is the first packet of the *tracked* connection, which may not match the boundary of the classical connection.

Like...

	-t raw -A PREROUTING -p tcp --tcp-flags SYN,ACK,RST,FIN SYN -j NOTRACK
	-t raw -A PREROUTING -p tcp --tcp-flags SYN,ACK,RST,FIN SYN,ACK -j NOTRACK

Will cause the exact behavior that the 3rd packet that would match --tcp-flags SYN,ACK,RST,FIN ACK will be the one showing up in the nat table.

As such, it might be worth looking at the LOGMARK target in Xtables-addons which will print out whether such a connection even exists/has been untracked. Alternatively, if you don't mind typing lots,

	-t mangle -A PREROUTING -m conntrack --ctstate INVALID \
		-j LOG --log-prefix "this won't shop up in nat: "
	-t mangle -A PREROUTING -m conntrack --ctstate UNTRACKED \
		-j LOG --log-prefix "neither will this: "
#2 lore raw
Quoted 10 lines · Jan Engelhardt
Quoted 3 lines · Julius Volz

Any ideas why the first packets never appear in the POSTROUTING chain but the ACK does?

It is not the first packet of the TCP connection that will show up in the nat table, it is the first packet of the *tracked* connection, which may not match the boundary of the classical connection.

Quoted 6 lines

Like...

       -t raw -A PREROUTING -p tcp --tcp-flags SYN,ACK,RST,FIN SYN -j NOTRACK
       -t raw -A PREROUTING -p tcp --tcp-flags SYN,ACK,RST,FIN SYN,ACK -j NOTRACK

Will cause the exact behavior that the 3rd packet that would match --tcp-flags SYN,ACK,RST,FIN ACK will be the one showing up in the nat table.

Thanks, that makes sense! I have no other iptables rules, by the way.

I just found out a minute ago why the SYN didn't enter the chain in my case. When you send packets through IPVS, you send them to the virtual IP that is configured on the load balancer so that IPVS can pick them up on LOCAL_IN. However, there is also a Netfilter NAT hook for LOCAL_IN (nf_nat_fn), which marks the packet as "already seen". This is mark is checked later in POSTROUTING and the chain is not traversed then... just disabling that LOCAL_IN hook in net/ipv4/nf_nat_standalone.c for testing leads to correct SNATing of the SYN, but the reply from the backend does not seem to get un-SNATed in PREROUTING for some reason. It gets picked up by IPVS from FORWARD, but still has the SNAT IP as its destination, so IPVS doesn't recognize it.

Quoted 8 lines

As such, it might be worth looking at the LOGMARK target in Xtables-addons which will print out whether such a connection even exists/has been untracked. Alternatively, if you don't mind typing lots,

       -t mangle -A PREROUTING -m conntrack --ctstate INVALID \
               -j LOG --log-prefix "this won't shop up in nat: "
       -t mangle -A PREROUTING -m conntrack --ctstate UNTRACKED \
               -j LOG --log-prefix "neither will this: "

Thanks, this is interesting and I'll use that for further debugging!

Julius

Signature · 3 lines
Julius Volz - Corporate Operations - SysOps

Google Switzerland GmbH - Identification No.: CH-020.4.028.116-1
#3 lore raw
Quoted 20 lines · Julius Volz
Quoted 10 lines · Jan Engelhardt
Quoted 3 lines · Julius Volz

Any ideas why the first packets never appear in the POSTROUTING chain but the ACK does?

It is not the first packet of the TCP connection that will show up in the nat table, it is the first packet of the *tracked* connection, which may not match the boundary of the classical connection.

Quoted 6 lines

Like...

       -t raw -A PREROUTING -p tcp --tcp-flags SYN,ACK,RST,FIN SYN -j NOTRACK
       -t raw -A PREROUTING -p tcp --tcp-flags SYN,ACK,RST,FIN SYN,ACK -j NOTRACK

Will cause the exact behavior that the 3rd packet that would match --tcp-flags SYN,ACK,RST,FIN ACK will be the one showing up in the nat table.

Thanks, that makes sense! I have no other iptables rules, by the way.

Quoted 12 lines

I just found out a minute ago why the SYN didn't enter the chain in my case. When you send packets through IPVS, you send them to the virtual IP that is configured on the load balancer so that IPVS can pick them up on LOCAL_IN. However, there is also a Netfilter NAT hook for LOCAL_IN (nf_nat_fn), which marks the packet as "already seen". This is mark is checked later in POSTROUTING and the chain is not traversed then... just disabling that LOCAL_IN hook in net/ipv4/nf_nat_standalone.c for testing leads to correct SNATing of the SYN, but the reply from the backend does not seem to get un-SNATed in PREROUTING for some reason. It gets picked up by IPVS from FORWARD, but still has the SNAT IP as its destination, so IPVS doesn't recognize it.

Ok, the SYN/ACK from the backend is logged as --cstate INVALID in PREROUTING and INPUT. This means that Netfilter thinks it doesn't belong to any connection, although it just SNATed the SYN to the backend correctly? Hmm... how can this be?

Julius

Signature · 3 lines
Julius Volz - Corporate Operations - SysOps

Google Switzerland GmbH - Identification No.: CH-020.4.028.116-1
#4 lore raw
Quoted 5 lines · Julius Volz

Ok, the SYN/ACK from the backend is logged as --cstate INVALID in PREROUTING and INPUT. This means that Netfilter thinks it doesn't belong to any connection, although it just SNATed the SYN to the backend correctly? Hmm... how can this be?

That probably means skb->nfct is lost (set to NULL, which is what INVALID indicates) after SNAT (PREROUTING), when IPVS kicks in.

#5 lore raw
Quoted 10 lines · Jan Engelhardt
Quoted 5 lines · Julius Volz

Ok, the SYN/ACK from the backend is logged as --cstate INVALID in PREROUTING and INPUT. This means that Netfilter thinks it doesn't belong to any connection, although it just SNATed the SYN to the backend correctly? Hmm... how can this be?

That probably means skb->nfct is lost (set to NULL, which is what INVALID indicates) after SNAT (PREROUTING), when IPVS kicks in.

But at PREROUTING, the skb hasn't seen any part of IPVS yet. And the prior SNAT for the SYN packet happened in POSTROUTING after all of IPVS was finished. Basically, there should be no IPVS involved from the time that the SYN is correctly SNATed and the time that the SYN/ACK is not. Please correct me if I'm missing something here...

Julius

Signature · 3 lines
Julius Volz - Corporate Operations - SysOps

Google Switzerland GmbH - Identification No.: CH-020.4.028.116-1
#6 lore raw

Didn't include lvs-devel in CC of the original message, adding it now...

Quoted 9 lines · Julius Volz
Quoted 8 lines · Jan Engelhardt
Quoted 5 lines · Julius Volz

Ok, the SYN/ACK from the backend is logged as --cstate INVALID in PREROUTING and INPUT. This means that Netfilter thinks it doesn't belong to any connection, although it just SNATed the SYN to the backend correctly? Hmm... how can this be?

Quoted 8 lines
Quoted 2 lines

That probably means skb->nfct is lost (set to NULL, which is what INVALID indicates) after SNAT (PREROUTING), when IPVS kicks in.

But at PREROUTING, the skb hasn't seen any part of IPVS yet. And the prior SNAT for the SYN packet happened in POSTROUTING after all of IPVS was finished. Basically, there should be no IPVS involved from the time that the SYN is correctly SNATed and the time that the SYN/ACK is not. Please correct me if I'm missing something here...

Ok, after lots of debugging, I've found the problem and got SNAT working with IPVS (in a very hacky way).

The problem was that Netfilter already assigns a conntrack entry (in skb->nfct) to the CIP->VIP SYN packet in PREROUTING. This contains a tuple which contains source and destination addresses as seen by Netfilter during PREROUTING (before IPVS). IPVS then modifies the destination address without updating the associated skb->nfct entry. Later, Netfilter SNAT in POSTROUTING finds the already assigned tuple and updates it with the new source address, but the destination address changed by IPVS is never updated in this tuple. This is why this wrong conntrack entry does not match the SYN/ACK packet returning from the backend server.

So as a proof of concept for IPVS SNAT, it is enough to remove the ip_vs_post_routing() function (to let Netfilter handle anything at all in POSTROUTING) and put a simple "skb->nfct = NULL;" into the IPVS NAT transmitter function, ip_vs_nat_xmit(), to let the conntrack tuple be regenerated in by Netfilter POSTROUTING. Of course, this is a memory leak, the conntrack hash entries aren't removed, etc.

I guess to do this correctly, we should probably update the skb->nfct entry in IPVS while doing our manual DNAT?

I realize there's a big patch integrating IPVS with conntrack
(http://www.ssi.bg/~ja/nfct/) in some way, but it seems like that does much more than would be needed for SNAT.

Julius

Signature · 3 lines
Julius Volz - Corporate Operations - SysOps

Google Switzerland GmbH - Identification No.: CH-020.4.028.116-1