7

I'm using CentOS 6.5 and I added the following commands to my iptables to forward all incoming traffic on port 8088 to 4569:

iptables -A PREROUTING -t nat -p udp --dport 8088 -i eth0 -j DNAT --to-destination 127.0.0.1:4569
iptables -I FORWARD 1 -d 127.0.0.1 -p udp --dport 4569 -j ACCEPT

iptables --list shows the following output:

iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
ACCEPT     udp  --  anywhere             localhost.localdomain udp dpt:iax

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

But when I take a packet trace on udp port 4569 I don't see any packets on that port. Then I added this:

iptables -A PREROUTING -t nat -p udp --dport 8088 -i eth0 -j REDIRECT --to-ports 4569

And my iptable looks like this:

Table: nat
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination
1    DNAT       udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:8088 to:127.0.0.1:4569
2    REDIRECT   udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:8088 redir ports 4569

But still no luck. What am I doing wrong?

2 Answers 2

7

To redirect packets to the loopback interface you need to use the REDIRECT target.

iptables -A PREROUTING -t nat -p udp --dport 8088 -i eth0 -j REDIRECT --to-ports 4569

Otherwise, you will change the destination address before the routing decision is taken to 127.0.0.1. This means that it will be considered a martian packet by the kernel and dropped by your reverse path filtering policy.

The two kernel parameters responsible for this behaviour are :

  • net.ipv4.conf.eth0.route_localnet

route_localnet - BOOLEAN

Do not consider loopback addresses as martian source or destination while routing. This enables the use of 127/8 for local routing purposes.

default FALSE

  • net.ipv4.conf.eth0.rp_filter

rp_filter - INTEGER

0 - No source validation.
1 - Strict mode as defined in RFC3704 Strict Reverse Path Each incoming packet is tested against the FIB and if the interface is not the best reverse path the packet check will fail. By default failed packets are discarded.
2 - Loose mode as defined in RFC3704 Loose Reverse Path Each incoming packet's source address is also tested against the FIB and if the source address is not reachable via any interface the packet check will fail.

Current recommended practice in RFC3704 is to enable strict mode to prevent IP spoofing from DDos attacks. If using asymmetric routing or other complicated routing, then loose mode is recommended.

The max value from conf/{all,interface}/rp_filter is used when doing source validation on the {interface}.

Default value is 0. Note that some distributions enable it in startup scripts.

As you totally want to keep this legitimate behaviour, the REDIRECT chain must be used to bypass this condition for a certain rule.

2
  • Tried but no luck. Please check my updated question. Dec 28, 2014 at 21:33
  • @user3360140 Why did you let the DNAT rule ? It's the only one reached as you appended the REDIRECT rule after it ... Dec 28, 2014 at 21:34
-1

You may need to enable ip forwarding in the Linux kernel.

First, check the current value.

sysctl net.ipv4.ip_forward

A zero means ip forwarding is disabled. This is the default.

Enable ip forwarding.

sysctl -w net.ipv4.ip_forward=1

Now, retest.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .