Skip to contentSkip to conversation

IPTABLES port forwarding woes

DISCUSSION Erich Weiler 2 participants 5 messages May 30, 2008

lore
Share & tools
lore
#0 lore raw
Quoted 1 line

However why would you want to do so ? It would hide the real source address from the SSH server on machine 2. Doesn't machine 2 use machine 1's private address as default gateway ?

Ah, I forgot to mention, machine 2 is on a private network that is not routed in any way. So, there is no default gateway for machine 2... That's why I was thinking the source port would have to be re-written internally such that machine 2 would know to send it back through machine 1 and then on to the internet... ?

#1 lore raw
Quoted 6 lines · Erich Weiler

Ah, I forgot to mention, machine 2 is on a private network that is not routed in any way. So, there is no default gateway for machine 2... That's why I was thinking the source port would have to be re-written internally such that machine 2 would know to send it back through machine 1 and then on to the internet... ?

Then indeed you need to SNAT the forwarded connection. Also make sure that machine 1 has IP forwarding enabled.

#2 lore raw

Hmmm.. Follow up question to this. How would the outgoing connection back to the internet find it's way back to the remote internet host, if the source address was re-written to machine 1 when in came in to begin with? Wouldn't the original remote internet source address be lost in all the translation?

Thanks for your insight by the way!

-erich

Quoted 13 lines · Pascal Hambourg
Quoted 6 lines · Erich Weiler

Ah, I forgot to mention, machine 2 is on a private network that is not routed in any way. So, there is no default gateway for machine 2... That's why I was thinking the source port would have to be re-written internally such that machine 2 would know to send it back through machine 1 and then on to the internet... ?

Then indeed you need to SNAT the forwarded connection. Also make sure that machine 1 has IP forwarding enabled.

Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe netfilter" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw
Quoted 1 line · Erich Weiler

Hmmm.. Follow up question to this. How would the outgoing connection back to the internet find it's way back to the remote internet host, if the source address was re-written to machine 1 when in came in to begin with? Wouldn't the original remote internet source address be lost in all the translation?

No, the connection tracking takes care of all this. The NAT operations applied by iptables rules to the first packet creating the connection are recorded and applied to subsequent packets in the same direction, while the inverse operations are applied to reply packets in the opposite direction.

Quoted 1 line

Thanks for your insight by the way!

You're welcome.

#4 lore raw
Quoted 1 line

No, the connection tracking takes care of all this. The NAT operations applied by iptables rules to the first packet creating the connection are recorded and applied to subsequent packets in the same direction, while the inverse operations are applied to reply packets in the opposite direction.

Excellent, it works just fine now! Here's my final iptables file, just in case it would help anyone in the list:

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [66:7948]
-A FORWARD -d 10.1.3.133 -i eth0 -o eth1 -p tcp -m tcp --dport 22 -m state --state NEW -j ACCEPT -A FORWARD -o eth0 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT
*nat
:PREROUTING ACCEPT [451:32699]
:POSTROUTING ACCEPT [2:236]
:OUTPUT ACCEPT [2:236]
-A PREROUTING -d 120.1.1.10 -i eth0 -p tcp -m tcp --dport 2222 -j DNAT --to-destination 10.1.3.133:22 -A POSTROUTING -s 10.1.3.133 -o eth0 -j SNAT --to-source 120.1.1.10
COMMIT
-A POSTROUTING -o eth1 -p tcp --dport 22 -d 10.1.3.133 -j SNAT --to 10.1.1.50

Thanks again!

-erich