Skip to contentSkip to conversation

DNAT, SNAT, Portforward

DISCUSSION Thomas Meindl 4 participants 6 messages Nov 1, 2002

lore
Share & tools
lore
#0 lore raw

okey, let's start again for the correct guys out there
here's my original script which i have testet since two days
doing all kinds of variations and combinations anyone could think of
my problem is situated at the PREROUTING chain with DNAT
I simply want a portforward
someone told me i don't need the POSTROUTING chain and i couldn't really figure out why (i can't understand how the PREROUTING chain could alter the packets coming from the answer from the wwwserver, that's why I would use the POSTROUTING chain to alter that backcoming packet at the router, to let the www-client know that it receieved the packet from where it had come from) anyway, see detailed describing at my initial post:
iptables, Portforward, DNAT, SNAT
and following

#/bin/bash

IPTABLES=/sbin/iptables

EXTIF="eth0"
INTIF="eth1"

echo "external interface: $EXTIF"
echo -e "internal interface: $INTIF\n"

echo "enabling syn-cookies.."
echo "1" > /proc/sys/net/ipv4/tcp_syncookies

echo "enabling dynamic addressing.."
echo "1" > /proc/sys/net/ipv4/ip_dynaddr

echo "enabling icmp broadcast reply.."
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

echo "enabling ip-forwarding.."
echo "1" > /proc/sys/net/ipv4/ip_forward

echo -e "enable source address verification (anti spoofing)..\n" for f in /proc/sys/net/ipv4/conf/*; do

    echo "2" > $f/rp_filter

done

echo "clearing any existing rules and setting default policy.." $IPTABLES -P INPUT ACCEPT
$IPTABLES -F INPUT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -F OUTPUT
$IPTABLES -P FORWARD DROP
$IPTABLES -F FORWARD
$IPTABLES -t nat -F

echo "forwarding port 1234 to 192.168.0.2:80.."
$IPTABLES -t nat -A PREROUTING -p tcp -i $EXTIF -d www.xxx.yyy.zzz --dport 1234 -j DNAT --to 192.168.0.2:80 $IPTABLES -A FORWARD -p tcp -i $EXTIF -o $INTIF -d 192.168.0.2 --dport 80 -j ACCEPT $IPTABLES -t nat -A POSTROUTING -p tcp -o $EXTIF -s 192.168.0.2 --sport 80 -j SNAT --to www.xxx.yyy.zzz:1234

echo "allow all connections out and only existing and related ones in.." $IPTABLES -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT $IPTABLES -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT
$IPTABLES -A FORWARD -j LOG --log-prefix "FIREWALL (FORWARD Chain): "

echo "enabling snat (masquerade) functionality on $EXTIF.." $IPTABLES -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE

thanks in advance for real help
and sorry to some who didn't like my initial help-scream

__________________________________________________________________
The NEW Netscape 7.0 browser is now available. Upgrade now! http://channels.netscape.com/ns/browsers/download.jsp

Get your own FREE, personal Netscape Mail account today at http://webmail.netscape.com/

#1 lore raw

I'm not going to read your detailed description or even your initial post so I apologize if there is some complication that I'm overlooking...I'm lazy;-)

To forward incoming connection to an internal server you typically need two rules.

A PREROUTING rule to change the destination IP address and/or port. Something like:
iptables -t nat -A PREROUTING -i <external interface> -p tcp -d <external addr> --dport 80 -j DNAT --to <internal addr>

A FORWARD rule to allow this traffic to be forwarded:
iptables -A FORWARD -i <external interface> -m state --state NEW -p tcp -d <internal addr> --dport 80 -j ACCEPT

This assumes that you have rules to allow EST, REL traffic in the FORWARD chain. The reply traffic is taken care of by netfilter....don't worry about it.

----- Original Message -----
From: "Thomas Meindl" <[email protected]>
To: <[email protected]>
Sent: Thursday, October 31, 2002 7:33 PM
Subject: DNAT, SNAT, Portforward

Quoted 6 lines

okey, let's start again for the correct guys out there here's my original script which i have testet since two days doing all kinds of variations and combinations anyone could think of my problem is situated at the PREROUTING chain with DNAT
I simply want a portforward
someone told me i don't need the POSTROUTING chain and i couldn't really

figure out why (i can't understand how the PREROUTING chain could alter the packets coming from the answer from the wwwserver, that's why I would use the POSTROUTING chain to alter that backcoming packet at the router, to let the www-client know that it receieved the packet from where it had come from)

Quoted 40 lines

anyway, see detailed describing at my initial post:
iptables, Portforward, DNAT, SNAT
and following

#/bin/bash

IPTABLES=/sbin/iptables

EXTIF="eth0"
INTIF="eth1"

echo "external interface: $EXTIF"
echo -e "internal interface: $INTIF\n"

echo "enabling syn-cookies.."
echo "1" > /proc/sys/net/ipv4/tcp_syncookies

echo "enabling dynamic addressing.."
echo "1" > /proc/sys/net/ipv4/ip_dynaddr

echo "enabling icmp broadcast reply.."
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

echo "enabling ip-forwarding.."
echo "1" > /proc/sys/net/ipv4/ip_forward

echo -e "enable source address verification (anti spoofing)..\n" for f in /proc/sys/net/ipv4/conf/*; do

    echo "2" > $f/rp_filter

done

echo "clearing any existing rules and setting default policy.." $IPTABLES -P INPUT ACCEPT
$IPTABLES -F INPUT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -F OUTPUT
$IPTABLES -P FORWARD DROP
$IPTABLES -F FORWARD
$IPTABLES -t nat -F

Quoted 2 lines

echo "forwarding port 1234 to 192.168.0.2:80.."
$IPTABLES -t nat -A PREROUTING -p tcp -i $EXTIF -d www.xxx.yyy.zzz --dport

1234 -j DNAT --to 192.168.0.2:80

Quoted 1 line

$IPTABLES -A FORWARD -p tcp -i $EXTIF -o $INTIF -d 192.168.0.2 --dport

80 -j ACCEPT

Quoted 1 line

$IPTABLES -t nat -A POSTROUTING -p tcp -o $EXTIF -s 192.168.0.2 --sport

80 -j SNAT --to www.xxx.yyy.zzz:1234

Quoted 3 lines

echo "allow all connections out and only existing and related ones in.." $IPTABLES -A FORWARD -i $EXTIF -o $INTIF -m state --state

ESTABLISHED,RELATED -j ACCEPT

Quoted 6 lines

$IPTABLES -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT $IPTABLES -A FORWARD -j LOG --log-prefix "FIREWALL (FORWARD Chain): "

echo "enabling snat (masquerade) functionality on $EXTIF.." $IPTABLES -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE

Quoted 6 lines

thanks in advance for real help
and sorry to some who didn't like my initial help-scream

__________________________________________________________________
The NEW Netscape 7.0 browser is now available. Upgrade now!

http://channels.netscape.com/ns/browsers/download.jsp

Quoted 2 lines

Get your own FREE, personal Netscape Mail account today at

http://webmail.netscape.com/

Quoted 1 line
#2 lore raw
In reply to Thomas Meindl
Quoted 5 lines · Thomas Meindl

okey, let's start again for the correct guys out there here's my original script which i have testet since two days doing all kinds of variations and combinations anyone could think of my problem is situated at the PREROUTING chain with DNAT I simply want a portforward

{Snip a few dozen lines :^}

Quoted 7 lines

echo "forwarding port 1234 to 192.168.0.2:80.."
$IPTABLES -t nat -A PREROUTING -p tcp -i $EXTIF -d www.xxx.yyy.zzz \
--dport 1234 -j DNAT --to 192.168.0.2:80
$IPTABLES -A FORWARD -p tcp -i $EXTIF -o $INTIF -d 192.168.0.2 \
--dport 80 -j ACCEPT
$IPTABLES -t nat -A POSTROUTING -p tcp -o $EXTIF -s 192.168.0.2 \
--sport 80 -j SNAT --to www.xxx.yyy.zzz:1234

umm. Have you tried this with "-j DNAT --to-destination"?
Also, I think the third rule should go. In the situation you've outlined, Netfilter should handle the reversal of your DNAT operation automatically. Wouldn't "$IPTABLES -A FORWARD -p tcp -d 192.168.0.2 --dport 80 -j ACCEPT" be sufficient for forwarding?

Quoted 8 lines

echo "allow all connections out and only existing and related ones in.." $IPTABLES -A FORWARD -i $EXTIF -o $INTIF -m state \
--state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT
$IPTABLES -A FORWARD -j LOG --log-prefix "FIREWALL (FORWARD Chain): "

echo "enabling snat (masquerade) functionality on $EXTIF.." $IPTABLES -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE

Question to list: At what point in its own travels does the returning packet get automatically un-DNATed? Prerouting again?

j

#3 lore raw
Quoted 2 lines · Joel Newkirk

Question to list: At what point in its own travels does the returning packet get automatically un-DNATed? Prerouting again?

No, it happens in POSTROUTING - because it is then a SNAT operation.

Useful rule of thumb - in the FORWARD chain, all packets have their "real" IP addresses.

That's because DNAT rules have already happened in PREROUTING, so packets now have the destination address of the machine they're really being sent on to, and SNAT rules have not yet happened in POSTROUTING, so packets still have the source address of the machine they really came in from.

Antony.

Signature · 11 lines
This email is intended for the use of the individual addressee(s) named above 
and may contain information that is confidential, privileged or unsuitable 
for overly sensitive persons with low self-esteem, no sense of humour, or 
irrational religious beliefs.

If you have received this email in error, you are required to shred it 
immediately, add some nutmeg, three egg whites and a dessertspoonful of 
caster sugar.   Whisk until soft peaks form, then place in a warm oven for 40 
minutes.   Remove promptly and let stand for 2 hours before adding some 
decorative kiwi fruit and cream.   Then notify me immediately by return email 
and eat the original message.
#4 lore raw
Quoted 6 lines · Antony Stone
Quoted 2 lines · Joel Newkirk

Question to list: At what point in its own travels does the returning packet get automatically un-DNATed? Prerouting again?

No, it happens in POSTROUTING - because it is then a SNAT operation.

Quoted 7 lines

Useful rule of thumb - in the FORWARD chain, all packets have their "real" IP addresses.

That's because DNAT rules have already happened in PREROUTING, so packets now have the destination address of the machine they're really being sent on to, and SNAT rules have not yet happened in POSTROUTING, so packets still have the source address of the machine they really came in from.

Is there any chance then of interference from a masquerade rule in postrouting? Or do the automatic reversals take precedence?

j

#5 lore raw
Quoted 10 lines · Joel Newkirk
Quoted 9 lines · Antony Stone
Quoted 2 lines · Joel Newkirk

Question to list: At what point in its own travels does the returning packet get automatically un-DNATed? Prerouting again?

No, it happens in POSTROUTING - because it is then a SNAT operation.

Useful rule of thumb - in the FORWARD chain, all packets have their "real" IP addresses.

Quoted 7 lines
Quoted 4 lines

That's because DNAT rules have already happened in PREROUTING, so packets now have the destination address of the machine they're really being sent on to, and SNAT rules have not yet happened in POSTROUTING, so packets still have the source address of the machine they really came in from.

Is there any chance then of interference from a masquerade rule in postrouting? Or do the automatic reversals take precedence?

Automatic reversals take precedence.

Reply packets are matched by the entry in the connection tracking table (the entry which identifies them as ESTABLISHED or RELATED), and are immediately diverted away in the background to be processed.

A side effect of this is that there is no way of specifying a manual rule in the nat tables which will get applied to reply packets (other than the automatic reverse of what got applied to the outgoing packet).

Antony.

Signature · 5 lines
You can spend the whole of your life trying to be popular,
but at the end of the day the size of the crowd at your funeral
will be largely dictated by the weather.

 - Frank Skinner