Skip to contentSkip to conversation

Logic for SNAT persistent handling introduced in 2.6.30 is inverted

DISCUSSION Maximilian Engelhardt 2 participants 2 messages Aug 24, 2009

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

Kernel 2.6.30 introduced a patch [1] for the persistent option in the netfilter SNAT target. This is exactly what we need here so I had a quick look at the code and noticed that the patch is wrong. The logic is simply inverted. The patch below fixes this.
Also note that because of this the default behavior of the SNAT target has changed since kernel 2.6.30 as it now ignores the destination IP in choosing the source IP for nating (which should only be the case if the persistent option is set).

--- net/ipv4/netfilter/nf_nat_core.c.orig 2009-08-24 14:44:00.000000000
+0200
+++ net/ipv4/netfilter/nf_nat_core.c 2009-08-24 14:44:43.000000000 +0200
@@ -212,7 +212,7 @@ find_best_ips_proto(struct nf_conntrack_

        maxip = ntohl(range->max_ip);
        j = jhash_2words((__force u32)tuple->src.u3.ip,
                         range->flags & IP_NAT_RANGE_PERSISTENT ?
-                               (__force u32)tuple->dst.u3.ip : 0, 0);
+                               0 : (__force u32)tuple->dst.u3.ip, 0);
        j = ((u64)j * (maxip - minip + 1)) >> 32;
        *var_ipp = htonl(minip + j);

}

Greetings,
Maximilian Engelhardt

[1]
http://git.eu.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;h=98d500d66cb7940747b424b245fc6a51ecfbf005

#1 lore raw
Quoted 4 lines · Maximilian Engelhardt

Kernel 2.6.30 introduced a patch [1] for the persistent option in the netfilter SNAT target. This is exactly what we need here so I had a quick look at the code and noticed that the patch is wrong. The logic is simply inverted. The patch below fixes this.

Good catch, applied. I'm wondering what the people who requested and tested this change did actually test :)

Quoted 4 lines

Also note that because of this the default behavior of the SNAT target has changed since kernel 2.6.30 as it now ignores the destination IP in choosing the source IP for nating (which should only be the case if the persistent option is set).

It fortunately only affects NAT to a range of IPs, which isn't used that much.