#!/bin/sh
### Intranet = eth1 Internet = eth0
###
### Accepts
### eth1 <- eth0 Ping,DNS
### eth1 <-> eth0 All established connections.
### eth1 -> eth0 All patckets.
###
### Declines
### All others except ACCEPTED.
### Load Module
echo " IP-masq Starting....."
/sbin/modprobe iptable_nat
### Enables IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
### Disables rp_filter(for IPSEC)
echo 0 > /proc/sys/net/ipv4/conf/eth0/rp_filter
### Flush ###
iptables -t filter -F FORWARD
iptables -t filter -F INPUT
iptables -t nat -F POSTROUTING
### Masquerade ###
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
####### Forward-----------------------
### Basically, All packets are dropped.
iptables -t filter -P FORWARD DROP
###Accept all trafic from intranet to the internet.
iptables -t filter -A FORWARD -i eth1 -j ACCEPT
### Established connections and Related packets are accepted.
# For exsample, This will allow DNS queries from local
network.
# but not allows incoming DNS queries.
# And related FTP Data Port and ICMP error are also accepted.
iptables -t filter -A FORWARD -m state --state ESTABLISHED,RELATED
-j ACCEPT
### ping( ICMP ) is accepted.
iptables -A FORWARD -i eth0 -p icmp -j ACCEPT
####### Input--------------------------
### Basically, All incoming packets are dropped.
iptables -t filter -P INPUT DROP
### All packets from eth1 are accepted.
iptables -t filter -A INPUT -i eth1 -j ACCEPT
### All packets from local loopback are accepted.
iptables -t filter -A INPUT -i lo -j ACCEPT
### Established connections and Related packets are accepted.
# For exsample, This will allow DNS queries from this
server.
# but not allows incoming DNS queries.
# And related FTP Data Port and ICMP error are also accepted.
iptables -t filter -A INPUT -m state --state ESTABLISHED,RELATED
-j ACCEPT
### Allows DNS query
iptables -t filter -A INPUT -i eth0 -p udp --dport 53
-j ACCEPT
iptables -t filter -A INPUT -i eth0 -p udp --sport 53
-j ACCEPT
####### IPSEC--------------------------
### Allow IKE
iptables -t filter -A INPUT -p udp --dport 500 -j ACCEPT
iptables -t filter -A INPUT -p udp --sport 500 -j ACCEPT
### Allows ESP
iptables -t filter -A INPUT -p 50 -j ACCEPT
### Allows AH
iptables -t filter -A INPUT -p 51 -j ACCEPT
### IPSEC packets come from all interfaces are completely
accepted.
iptables -t filter -A FORWARD -i ipsec+ -j ACCEPT
iptables -t filter -A FORWARD -o ipsec+ -j ACCEPT
iptables -t filter -A INPUT -i ipsec+ -j ACCEPT