Skip to contentSkip to conversation

How does iptables redirect a packet that is not addressed to its local machine

DISCUSSION Grace Li 5 participants 6 messages Apr 7, 2004 – Apr 8, 2004 netfilter

lore
Share & tools
lore

Grace Li

author
#0 lore raw

Hi,

I am just wondering if anybody could explain what happened in the following experiments:

Client (192.168.1.134) tries to connect to port 1888 of Server (192.168.1.115) through Gateway (192.168.1.1). In the Gateway, the iptables has been instructed to redirect traffic on port 1888 to 2000 (iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 1888 -j REDIRECT -to-port 2000 ).

The results of my experiments is that the application on Gateway who listens to port 2000 could not get anything, while Server receives everything expected on its port 1888. So my question is that did iptables do anything here?

Many thanks,

Grace

#1 lore raw
Quoted 11 lines · Grace Li

I am just wondering if anybody could explain what happened in the following experiments:

Client (192.168.1.134) tries to connect to port 1888 of Server (192.168.1.115) through Gateway (192.168.1.1). In the Gateway, the iptables has been instructed to redirect traffic on port 1888 to 2000 (iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 1888 -j REDIRECT -to-port 2000 ).

The results of my experiments is that the application on Gateway who listens to port 2000 could not get anything, while Server receives everything expected on its port 1888. So my question is that did iptables do anything here?

If I understand what you are trying to do correctly, you need to use the DNAT target instead of the REDIRECT target. The REDIRECT target is for intercepting packets and redirecting them to ports on the gateway machine.

Try:

iptables -t nat -A PREROUTING -i eth0 -d 192.168.1.115 --dport 1888 \
-j DNAT --to-destination 192.168.1.115:2000

I don't believe there is a target for re-mapping destination ports without re-mapping addresses. One cannot simply change the destination port on the packet as it passes through, because the source port on packets coming back from the server need to be mangled as well.

Nicholas

#2 lore raw

I misread. Apologies.

Nicholas

Quoted 30 lines · Nicholas E. Walker
Quoted 11 lines · Grace Li

I am just wondering if anybody could explain what happened in the following experiments:

Client (192.168.1.134) tries to connect to port 1888 of Server (192.168.1.115) through Gateway (192.168.1.1). In the Gateway, the iptables has been instructed to redirect traffic on port 1888 to 2000 (iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 1888 -j REDIRECT -to-port 2000 ).

The results of my experiments is that the application on Gateway who listens to port 2000 could not get anything, while Server receives everything expected on its port 1888. So my question is that did iptables do anything here?

If I understand what you are trying to do correctly, you need to use the DNAT target instead of the REDIRECT target. The REDIRECT target is for intercepting packets and redirecting them to ports on the gateway machine.

Try:

iptables -t nat -A PREROUTING -i eth0 -d 192.168.1.115 --dport 1888 \
-j DNAT --to-destination 192.168.1.115:2000

I don't believe there is a target for re-mapping destination ports without re-mapping addresses. One cannot simply change the destination port on the packet as it passes through, because the source port on packets coming back from the server need to be mangled as well.

Nicholas

#3 lore raw
In reply to Grace Li
Quoted 8 lines · Grace Li

Hi,

I am just wondering if anybody could explain what happened in the following experiments:

Client (192.168.1.134) tries to connect to port 1888 of Server (192.168.1.115) through Gateway (192.168.1.1). In the Gateway, the iptables has been instructed to redirect traffic on port 1888 to 2000 (iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 1888 -j REDIRECT -to-port 2000 ).

Works here...

Quoted 4 lines

The results of my experiments is that the application on Gateway who listens to port 2000 could not get anything, while Server receives everything expected on its port 1888. So my question is that did iptables do anything here?

No idea. This is a kind of thing which has always worked for me, but you are the second person in a few days reporting this is not working..

Exacly what kernel and iptables version are you using on the gateway?

What does "iptables-save -t nat" give?

Regards
Henrik

#4 lore raw
In reply to Grace Li

Perhaps you haven't added a corresponding rule to the INPUT table to actually allow port 2000 to the gateway?

Phil

Quoted 18 lines · Grace Li

Hi,

I am just wondering if anybody could explain what happened in the following experiments:

Client (192.168.1.134) tries to connect to port 1888 of Server (192.168.1.115) through Gateway (192.168.1.1). In the Gateway, the iptables has been instructed to redirect traffic on port 1888 to 2000 (iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 1888 -j REDIRECT -to-port 2000 ).

The results of my experiments is that the application on Gateway who listens to port 2000 could not get anything, while Server receives everything expected on its port 1888. So my question is that did iptables do anything here?

Many thanks,

Grace

#5 lore raw
In reply to Grace Li
Quoted 4 lines

Client (192.168.1.134) tries to connect to port 1888 of Server (192.168.1.115) through Gateway (192.168.1.1). In the Gateway, the iptables has been instructed to redirect traffic on port 1888 to 2000 (iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 1888 -j REDIRECT -to-port 2000 ).

At the client execute:
traceroute -n 192.168.1.115
to make sure that the client DID send the packet to the gateway

__________________________________
Do you Yahoo!?
Yahoo! Small Business $15K Web Design Giveaway
http://promotions.yahoo.com/design_giveaway/