Skip to contentSkip to conversation

NAT/POSTROUTING rules doesn't match packets

DISCUSSION Marek Zachara 3 participants 10 messages Oct 1, 2005 – Oct 2, 2005

lore
Share & tools
lore
#0 lore raw

Hi,

I have been having problems with this for some time now. Some packets are not processed correctly by iptables. This applies only to POSTROUTING chain in NAT table.

I have an internal network addressed 10.0.0.0/24
Initially, i wanted all packets that go out of my network to a remote port 4569 to be SNAT-ed to address 1.2.3.4. But when i set up such rule for iptables:

$IPT -t nat -A POSTROUTING -p udp --destination-port 4569 -o eth1 -j SNAT --to-source 1.2.3.4

when i run tcpdump, i noticed on my external interface (eth1) the packets had source address of originating machine (10.0.0.7)

so i did some tests and found out that rules in POSTROUTING chain doesn't always work as expected. In the following experiment, i put exactly same rules in NAT/PREROUTING,NAT/POSTROUTING,MANGLE/PREROUTING and MANGLE/POSTROUTING:

$IPT -t mangle -A PREROUTING -p udp --destination-port 4569 -j LOG $IPT -t mangle -A POSTROUTING -p udp --destination-port 4569 -j LOG $IPT -t nat -A PREROUTING -p udp -m udp --destination-port 4569 -j LOG $IPT -t nat -A POSTROUTING -p udp -m udp --destination-port 4569 -j LOG

apparently all the rules EXCEPT the one in the NAT/POSTROUTING chain seem to work. Below is the number of packets that matched these rules. As you can see, the lat chain has counter 0 and the other three were matched by 23 packets.

I really have no clue on what could be the reason for this, maybe its a bug in netfilter code? I will appreciate any help.

Marek

P.S. Iptables 1.3.3, kernel 2.6.15 (both compiled from sources)

irongate:~# iptables -t mangle -nvL
Chain PREROUTING (policy ACCEPT 1681K packets, 495M bytes)
 pkts bytes target     prot opt in     out     source               
destination
   23  1134 LOG        udp  --  *      *       0.0.0.0/0            0.0.0.0/0           
udp dpt:4569 LOG flags 0 level 4

Chain POSTROUTING (policy ACCEPT 1630K packets, 478M bytes)
pkts bytes target prot opt in out source
destination
23 1134 LOG udp -- * * 0.0.0.0/0 0.0.0.0/0
udp dpt:4569 LOG flags 0 level 4

irongate:~# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 223K packets, 23M bytes)
 pkts bytes target     prot opt in     out     source               
destination
    0     0 LOG        udp  --  *      *       0.0.0.0/0            0.0.0.0/0           
udp dpt:4569 LOG flags 0 level 4

Chain POSTROUTING (policy ACCEPT 263 packets, 16175 bytes)
pkts bytes target prot opt in out source
destination

    0     0 LOG        udp  --  *      *       0.0.0.0/0            0.0.0.0/0           

udp dpt:4569 LOG flags 0 level 4
116 6546 SNAT all -- * eth1 0.0.0.0/0 0.0.0.0/0
to:192.168.100.3

#1 lore raw

I must be tired.... :(

Anyway, i have just noticed that not only POSTROUTING but also PREROUTING in the NAT table don't match packets as they should. So probably the issue is related to NAT table.
After some time, i have like 800 packets matched by rules in mangle table, but only 2 in the nat POSTROUTING/PREROUTING. So some of them match eventually, but a small fraction of all.

Marek.

P.S. The kernel version is 2.6.12.5 :)

Quoted 69 lines · Marek Zachara

Hi,

I have been having problems with this for some time now. Some packets are not processed correctly by iptables. This applies only to POSTROUTING chain in NAT table.

I have an internal network addressed 10.0.0.0/24
Initially, i wanted all packets that go out of my network to a remote port 4569 to be SNAT-ed to address 1.2.3.4. But when i set up such rule for iptables:

$IPT -t nat -A POSTROUTING -p udp --destination-port 4569 -o eth1 -j SNAT --to-source 1.2.3.4

when i run tcpdump, i noticed on my external interface (eth1) the packets had source address of originating machine (10.0.0.7)

so i did some tests and found out that rules in POSTROUTING chain doesn't always work as expected. In the following experiment, i put exactly same rules in NAT/PREROUTING,NAT/POSTROUTING,MANGLE/PREROUTING and MANGLE/POSTROUTING:

$IPT -t mangle -A PREROUTING -p udp --destination-port 4569 -j LOG $IPT -t mangle -A POSTROUTING -p udp --destination-port 4569 -j LOG $IPT -t nat -A PREROUTING -p udp -m udp --destination-port 4569 -j LOG $IPT -t nat -A POSTROUTING -p udp -m udp --destination-port 4569 -j LOG

apparently all the rules EXCEPT the one in the NAT/POSTROUTING chain seem to work. Below is the number of packets that matched these rules. As you can see, the lat chain has counter 0 and the other three were matched by 23 packets.

I really have no clue on what could be the reason for this, maybe its a bug in netfilter code? I will appreciate any help.

Marek

P.S. Iptables 1.3.3, kernel 2.6.15 (both compiled from sources)

irongate:~# iptables -t mangle -nvL
Chain PREROUTING (policy ACCEPT 1681K packets, 495M bytes)
 pkts bytes target     prot opt in     out     source
destination
   23  1134 LOG        udp  --  *      *       0.0.0.0/0           
0.0.0.0/0 udp dpt:4569 LOG flags 0 level 4

Chain POSTROUTING (policy ACCEPT 1630K packets, 478M bytes)
pkts bytes target prot opt in out source
destination
23 1134 LOG udp -- * * 0.0.0.0/0
0.0.0.0/0 udp dpt:4569 LOG flags 0 level 4

irongate:~# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 223K packets, 23M bytes)
 pkts bytes target     prot opt in     out     source
destination
    0     0 LOG        udp  --  *      *       0.0.0.0/0           
0.0.0.0/0 udp dpt:4569 LOG flags 0 level 4

Chain POSTROUTING (policy ACCEPT 263 packets, 16175 bytes)
pkts bytes target prot opt in out source
destination

    0     0 LOG        udp  --  *      *       0.0.0.0/0           

0.0.0.0/0 udp dpt:4569 LOG flags 0 level 4
116 6546 SNAT all -- * eth1 0.0.0.0/0
0.0.0.0/0 to:192.168.100.3

#2 lore raw
Quoted 3 lines · Marek Zachara

Anyway, i have just noticed that not only POSTROUTING but also PREROUTING in the NAT table don't match packets as they should. So probably the issue is related to NAT table.

The NAT table is a bit special. This table only sees the very first packet of each new conntrack session. For all consequtive packets of the same session the same NAT rules is automatically used.

What this means is

a) You can not change NAT for a session while it is running

b) When you add NAT rules they only start to apply on the next session.
Traffic flows already seen won't be NAT:ed.

You did not tell what kind of traffic flows you are having, but the rule fragments you show is based on UDP. UDP sessions are tracked by the

source IP, port, destination IP, port (and the reverse)

once a such pakcet has been seen the UDP session persists in conntrack/NAT until no matching packets have been seen for the udp conntrack timeout (usually 180 seconds).

Regards
Henrik

#3 lore raw
Quoted 9 lines · you
Quoted 3 lines · Marek Zachara

Anyway, i have just noticed that not only POSTROUTING but also PREROUTING in the NAT table don't match packets as they should. So probably the issue is related to NAT table.

The NAT table is a bit special. This table only sees the very first packet of each new conntrack session. For all consequtive packets of the same session the same NAT rules is automatically used.

OK, i understand this. But still can't it make to work.
I'm trying to get asterisk IAX protocol to be routed properly. This is UDP packets destined for port 4569. I need them to be sent out from the box at eth1 with source 192.168.100.1 (internal net at eth0 is 10.0.0.0/24)

the nat/postrouting table looks like this (i waited for about 10 minutes to make sure the UDP conntrack 'session' is reseted):

Chain POSTROUTING (policy ACCEPT 2454 packets, 136K bytes)
pkts bytes target prot opt in out source
destination

    0     0 SNAT       udp  --  *      eth1    0.0.0.0/0            0.0.0.0/0           
udp dpt:1194 to:192.168.100.1
    0     0 SNAT       udp  --  *      eth1    0.0.0.0/0            0.0.0.0/0           
udp dpt:5060 to:192.168.100.1
    1    53 SNAT       udp  --  *      eth1    0.0.0.0/0            0.0.0.0/0           
udp dpt:4569 to:192.168.100.1
    0     0 SNAT       udp  --  *      eth1    0.0.0.0/0            0.0.0.0/0           

udp dpt:3478 to:192.168.100.1
84 5040 SNAT tcp -- * eth1 0.0.0.0/0 0.0.0.0/0
tcp dpts:1024:32000 to:192.168.100.2
248 13976 SNAT udp -- * eth1 0.0.0.0/0 0.0.0.0/0
udp dpts:1024:32000 to:192.168.100.2

    0     0 SNAT       all  --  *      eth1    10.0.0.251           0.0.0.0/0           

to:192.168.100.2
106 6783 SNAT all -- * eth1 0.0.0.0/0 0.0.0.0/0
to:192.168.100.3

but if i run a tcpdump:

irongate:~# tcpdump -ni eth1 udp port 4569
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 96 bytes
19:56:41.784463 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP, length: 25
19:56:43.785641 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP, length: 25

2 packets captured
2 packets received by filter
0 packets dropped by kernel

so the packets still leave the box with the source 10.0.0.250 (and not 192.168.100.1 as I want them to).

Any suggestions what i could be doing wrong?

Marek

#4 lore raw
Quoted 5 lines · Marek Zachara
irongate:~# tcpdump -ni eth1 udp port 4569
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 96 bytes
19:56:41.784463 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP, length: 25
19:56:43.785641 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP, length: 25

Odd..

Quoted 1 line

Any suggestions what i could be doing wrong?

What does /proc/net/ip_conntrack say about the session?

grep 84.16.64.240 /proc/net/ip_conntrack

udp 17 50 src=10.1.1.3 dst=10.1.1.1 sport=32793 dport=53 src=10.1.1.1 dst=10.1.1.3 sport=53 dport=32793 [ASSURED] use=1

the second number (50) is the current timeout of that session.

the first src/dst/port pair is the original packet. The second pair is the NAT info.

And what timeouts is your kernel configured with?

cat /proc/sys/net/ipv4/netfilter/conntrack_udp_timeout cat /proc/sys/net/ipv4/netfilter/conntrack_udp_timeout_stream

Regards
Henrik

#5 lore raw
Quoted 8 lines · you
Quoted 6 lines · Marek Zachara
irongate:~# tcpdump -ni eth1 udp port 4569
tcpdump: verbose output suppressed, use -v or -vv for full protocol
decode listening on eth1, link-type EN10MB (Ethernet), capture size 96
bytes 19:56:41.784463 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP,
length: 25 19:56:43.785641 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP,
length: 25
Quoted 8 lines

Odd..

Quoted 1 line

Any suggestions what i could be doing wrong?

What does /proc/net/ip_conntrack say about the session?

   grep 84.16.64.240 /proc/net/ip_conntrack

here is the entry:

udp 17 23 src=10.0.0.250 dst=84.16.64.240 sport=4569 dport=4569
packets=13426 bytes=581092 [UNREPLIED] src=84.16.64.240 dst=10.0.0.250 sport=4569 dport=4569 packets=0 bytes=0 mark=0 use=1

Quoted 6 lines

And what timeouts is your kernel configured with?

cat /proc/sys/net/ipv4/netfilter/conntrack_udp_timeout cat /proc/sys/net/ipv4/netfilter/conntrack_udp_timeout_stream

irongate:~# cat /proc/sys/net/ipv4/netfilter/ip_conntrack_udp_timeout
30
irongate:~# cat /proc/sys/net/ipv4/netfilter/ip_conntrack_udp_timeout_stream
180

but still the packets dont get SNAT-ed:

irongate:~# tcpdump -ni eth0 udp port 4569
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
21:26:22.239340 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP, length: 12

and the nat/POSTROUTING rule has been hit only once since router reboot approx 2 hrs ago (the asterisk keeps sending about 1packet/5seconds) :

Chain POSTROUTING (policy ACCEPT 20148 packets, 1036K bytes)
pkts bytes target prot opt in out source
destination

    1    53 SNAT       udp  --  *      eth1    0.0.0.0/0            0.0.0.0/0           

udp dpt:4569 to:192.168.100.1

any hints? :)

Marek

#6 lore raw
Quoted 3 lines · Marek Zachara

udp 17 23 src=10.0.0.250 dst=84.16.64.240 sport=4569 dport=4569
packets=13426 bytes=581092 [UNREPLIED] src=84.16.64.240 dst=10.0.0.250 sport=4569 dport=4569 packets=0 bytes=0 mark=0 use=1

Then it is NAT:ed..

Probably tcpdump gets fooled and shows you the packet as received.

If in doubt try running the capture on a separate box connected with a hub to your "outside" connection. This will give you an exact picture of what the packets on the outside link looks like.

Quoted 6 lines

but still the packets dont get SNAT-ed:

irongate:~# tcpdump -ni eth0 udp port 4569
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
21:26:22.239340 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP, length: 12

To me it looks like you are looking at the packets as they arrive from the internal network before SNAT. Nothing wrong in the above.

tcpdump -ni eth1 udp port 4569

should show you a different picture.

Regards
Henrik

#7 lore raw
Quoted 5 lines · you
Quoted 3 lines · Marek Zachara

udp 17 23 src=10.0.0.250 dst=84.16.64.240 sport=4569 dport=4569
packets=13426 bytes=581092 [UNREPLIED] src=84.16.64.240 dst=10.0.0.250 sport=4569 dport=4569 packets=0 bytes=0 mark=0 use=1

Quoted 8 lines

Then it is NAT:ed..

Probably tcpdump gets fooled and shows you the packet as received.

If in doubt try running the capture on a separate box connected with a hub to your "outside" connection. This will give you an exact picture of what the packets on the outside link looks like.

I also checked tcpdump on the next router in line (this one connects 192.168.x.x to the internet) and still i see packets with source 10.0.0.250 there

Quoted 11 lines
Quoted 7 lines

but still the packets dont get SNAT-ed:

irongate:~# tcpdump -ni eth0 udp port 4569
tcpdump: verbose output suppressed, use -v or -vv for full protocol
decode listening on eth0, link-type EN10MB (Ethernet), capture size 96
bytes 21:26:22.239340 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP,
length: 12

To me it looks like you are looking at the packets as they arrive from the internal network before SNAT. Nothing wrong in the above.

yes, you are right that was an internal interface. But the output on eth1 looks exactly the same

Marek

#8 lore raw
In reply to Marek Zachara
Quoted 6 lines · Marek Zachara

I'm trying to get asterisk IAX protocol to be routed properly. This is UDP packets destined for port 4569. I need them to be sent out from the box at eth1 with source 192.168.100.1 (internal net at eth0 is 10.0.0.0/24)

the nat/postrouting table looks like this (i waited for about 10

snip

Quoted 1 line

Any suggestions what i could be doing wrong?

This is asterisk running on the iptables box itself? Try nat/OUTPUT.

I won't guess. Post complete network topology information and your iptables-save output. Why do you want to NAT from one RFC 1918 address to another one? I bet there is a better way to do this. I don't have any trouble with IAX/IAX2 behind NAT.

Signature · 2 lines
    mail to this address is discarded unless "/dev/rob0"
    or "not-spam" is in Subject: header
#9 lore raw
Quoted 10 lines · On Saturday 01 of October 2005 22:57, /dev/rob0 wrote:
Quoted 6 lines · Marek Zachara

I'm trying to get asterisk IAX protocol to be routed properly. This is UDP packets destined for port 4569. I need them to be sent out from the box at eth1 with source 192.168.100.1 (internal net at eth0 is 10.0.0.0/24)

the nat/postrouting table looks like this (i waited for about 10

snip

Quoted 8 lines
Quoted 1 line

Any suggestions what i could be doing wrong?

This is asterisk running on the iptables box itself? Try nat/OUTPUT.

I won't guess. Post complete network topology information and your iptables-save output. Why do you want to NAT from one RFC 1918 address to another one? I bet there is a better way to do this. I don't have any trouble with IAX/IAX2 behind NAT.

here is (in short) the topology:

10.0.0.250 (asterisk)

    |
10.0.0.1 (eth0)
192.168.100.1-3 (eth1) (router1)
    |
192.168.192.1 (eth0)
public.ip (eth1) (router2)
    |

internet

now i want packets generated by asterisk (10.0.0.250) and destined to outside world to leave router1 with source address of 192.168.100.1 as this is needed to route them properly at router2

unfortunately, the IAX packets leave router1 with source 10.0.0.250 which makes the response undeliverable (as router2 doesn't know anything about 10.x.x.x network)

here is the tcpdump at router2:

cerber:~# tcpdump -ni eth0 udp port 4569
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
09:20:31.825854 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP, length: 12
09:20:33.827356 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP, length: 12
09:20:33.827450 IP 10.0.0.250.4569 > 84.16.64.240.4569: UDP, length: 25

here is the full ruleset of NAT table at router1. Most of it are not relevant. However there are two types of UDP packets generated by asterisk: SIP (5060) and IAX2 (4569). Now, the SIP packets are routed correctly - they hit the SNAT rule and leave the router1 with source 192.168.100.1 but the IAX packets somehow manage to 'bypass' the iptables rules and leave unmodified...

irongate:~# iptables -t nat -nL
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DNAT       tcp  --  0.0.0.0/0            192.168.100.1       tcp dpt:21 
to:10.0.0.250
DNAT       tcp  --  0.0.0.0/0            192.168.100.1       tcp dpt:22 
to:10.0.0.7
DNAT       udp  --  0.0.0.0/0            192.168.100.1       udp dpt:5060 
to:10.0.0.250
DNAT       udp  --  0.0.0.0/0            192.168.100.1       udp dpt:4569 
to:10.0.0.250
DNAT       tcp  --  0.0.0.0/0            192.168.100.2       tcp 
dpts:5000:5999 to:10.0.0.251
DNAT       udp  --  0.0.0.0/0            192.168.100.2       udp 
dpts:5000:5999 to:10.0.0.251

Chain POSTROUTING (policy ACCEPT)

target     prot opt source               destination
SNAT       udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:1194 

to:192.168.100.1
SNAT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:5060
to:192.168.100.1
SNAT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:4569
to:192.168.100.1
SNAT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:3478
to:192.168.100.1
SNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp
dpts:1024:32000 to:192.168.100.2
SNAT udp -- 0.0.0.0/0 0.0.0.0/0 udp
dpts:1024:32000 to:192.168.100.2

SNAT       all  --  10.0.0.251           0.0.0.0/0           to:192.168.100.2
SNAT       all  --  0.0.0.0/0            0.0.0.0/0           to:192.168.100.3

thanks for any help ;)

Marek