Skip to contentSkip to conversation

captive web system using DNAT and SNAT

DISCUSSION Matt Zagrabelny 2 participants 3 messages May 5, 2005 – May 6, 2005

lore
Share & tools
lore
#0 lore raw

hello,

i am working on a captive web system and am using DNAT and SNAT to accomplish this. things are going well, but i am have hit a road block.

currently our router is configured (via next hop routing) to send traffic to the firewall for certain vlans on our network. thus i am dealing with an asymmetrical route map. traffic coming from a vlan is sent to the firewall, but traffic destined for the same vlan does not pass through the firewall. this is fine, but it does make the problem a little more challenging.

so in order to make the system captive, i am using DNAT. since it is an asymmetrical route map, i am using SNAT.

DNAT ensures that web traffic goes to the box i want, and SNAT ensures that the web talks back to firewall instead of directly to the original client (who is not expecting to be getting traffic from *that* web server).

the problem is that if the client has traffic destined for the captive web server, then i dont want the firewall to DNAT or SNAT the traffic, i just want to let it go peacefully.

here is my small firewall script that accomplishes the captive part:

# catch all redirects to registration server
iptables -t nat -A PREROUTING -i eth1 -p tcp -dport 80 -j DNAT
--to-destination ${REGISTRATION_SERVER}

iptables -t nat -A POSTROUTING -o eth0 -p tcp -dport 80 -j SNAT --to-source ${EXTERNAL_ADDRESS}

iptables -t nat -A PREROUTING -i eth1 -p tcp -dport 80 -j DNAT --to-destination ${REGISTRATION_SERVER}

iptables -t nat -A POSTROUTING -o eth0 -p tcp -dport 80 -j SNAT --to-source ${EXTERNAL_ADDRESS}

so the above works for me. on my client machine i go to google.com the firewall redirects me to the registration server (${REGISTRATION_SERVER} and all is good.

what i want to do is if i type in ${REGISTRATION_SERVER} in my address bar on the client system, then i dont want the firewall to do any DNAT or SNAT. what is the best way to accomplish this?

thanks,

matt zagrabelny

#1 lore raw
Quoted 4 lines
# catch all redirects to registration server
iptables -t nat -A PREROUTING -i eth1 -p tcp -dport 80 -j DNAT
--to-destination ${REGISTRATION_SERVER}
Quoted 8 lines

iptables -t nat -A POSTROUTING -o eth0 -p tcp -dport 80 -j SNAT --to-source ${EXTERNAL_ADDRESS}

iptables -t nat -A PREROUTING -i eth1 -p tcp -dport 80 -j DNAT --to-destination ${REGISTRATION_SERVER}

iptables -t nat -A POSTROUTING -o eth0 -p tcp -dport 80 -j SNAT --to-source ${EXTERNAL_ADDRESS}

Did you mean to post the same rules twice? Or am I really just missing something?

Quoted 3 lines

what i want to do is if i type in ${REGISTRATION_SERVER} in my address bar on the client system, then i dont want the firewall to do any DNAT or SNAT. what is the best way to accomplish this?

Try changing your DNAT rule to be like the following:

iptables -t nat -A PREROUTING -i eth1 -p tcp -d ! ${REGISTRATION_SERVER} -dport 80 -j DNAT --to-destination ${REGISTRATION_SERVER}

As this will DNAT any traffic that is not going to your ${REGISTRATION_SERVER} directly.

Grant. . . .

#2 lore raw
Quoted 7 lines · Grant
Quoted 7 lines
# catch all redirects to registration server
iptables -t nat -A PREROUTING -i eth1 -p tcp -dport 80 -j DNAT
--to-destination ${REGISTRATION_SERVER}

iptables -t nat -A POSTROUTING -o eth0 -p tcp -dport 80 -j SNAT --to-source ${EXTERNAL_ADDRESS}

Quoted 8 lines
Quoted 5 lines

iptables -t nat -A PREROUTING -i eth1 -p tcp -dport 80 -j DNAT --to-destination ${REGISTRATION_SERVER}

iptables -t nat -A POSTROUTING -o eth0 -p tcp -dport 80 -j SNAT --to-source ${EXTERNAL_ADDRESS}

Did you mean to post the same rules twice? Or am I really just missing something?

sort of. for the second set of rules s/80/443.

Quoted 7 lines
Quoted 3 lines

what i want to do is if i type in ${REGISTRATION_SERVER} in my address bar on the client system, then i dont want the firewall to do any DNAT or SNAT. what is the best way to accomplish this?

Try changing your DNAT rule to be like the following:

iptables -t nat -A PREROUTING -i eth1 -p tcp -d ! ${REGISTRATION_SERVER} -dport 80 -j DNAT --to-destination ${REGISTRATION_SERVER}

but the problem is that the traffic leaving the firewall will be SNATed. so all traffic will be essentially go to the ${REGISTRATION_SERVER} but i dont want to SNAT traffic that isnt DNATed.

ive tried the following yesterday:

iptables -t mangle -A PREROUTING -i eth1 -d ${REGISTRATION_SERVER} -j MARK --set-mark 123

iptables -t nat -A PREROUTING -i eth1 -m mark --mark 123 -j ACCEPT

iptables -t nat -A POSTROUTING -o eth0 -m mark --mark 123 -j ACCEPT

iptables -t nat -A PREROUTING -i eth1 -p tcp -dport 80 -j DNAT --to-destination ${REGISTRATION_SERVER}

iptables -t nat -A POSTROUTING -o eth0 -p tcp -dport 80 -j SNAT --to-source ${EXTERNAL_ADDRESS}

and everything was still being DNATed and SNATed (including the traffic destined for ${REGISTRATION_SERVER}). after a reboot this morning, the above rules work. (yes, i was flushing my tables (nat, mangle, filter)).

any thoughts?

so my problem is solved, but i am curious why a reboot changes the way the packet filtering/natting is done.

thanks,

matt zagrabelny