#!/bin/sh
# 

do_firewall() {

#
# rc.firewall - Initial SIMPLE IP Firewall script for Linux 2.4.x and iptables
#
# Copyright (C) 2001  Oskar Andreasson &lt;bluefluxATkoffeinDOTnet&gt;
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
###########################################################################
# 1. Configuration options.

# 1.1 Internet Configuration.
INET_IP=$IP
INET_IFACE="eth0"

# 1.1.1 DHCP
# 1.1.2 PPPoE
# 1.2 Local Area Network configuration.

# 1.3 DMZ Configuration.
# 1.4 Localhost Configuration.
LO_IFACE="lo"
LO_IP="127.0.0.1"

# 1.5 IPTables Configuration.
IPTABLES="/sbin/iptables"

# 1.6 Other Configuration.

###########################################################################
# 2. Module loading.

# Needed to initially load modules
# No - not on Luna
# /sbin/depmod -a

# 2.1 Required modules
#/sbin/modprobe ip_tables
#/sbin/modprobe ip_conntrack
#/sbin/modprobe iptable_filter
#/sbin/modprobe iptable_mangle
#/sbin/modprobe iptable_nat
#/sbin/modprobe ipt_LOG
#/sbin/modprobe ipt_limit
#/sbin/modprobe ipt_state

# 2.2 Non-Required modules
#/sbin/modprobe ipt_owner
#/sbin/modprobe ipt_REJECT
#/sbin/modprobe ipt_MASQUERADE
#/sbin/modprobe ip_conntrack_ftp
#/sbin/modprobe ip_conntrack_irc
#/sbin/modprobe ip_nat_ftp
#/sbin/modprobe ip_nat_irc

###########################################################################
# 3. /proc set up.

# 3.1 Required proc configuration
echo "1" > /proc/sys/net/ipv4/ip_forward

# 3.2 Non-Required proc configuration
#echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
#echo "1" > /proc/sys/net/ipv4/conf/all/proxy_arp
#echo "1" > /proc/sys/net/ipv4/ip_dynaddr

###########################################################################
# 4. rules set up.

# 4.1 Filter table

# 4.1.1 Set policies
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -P FORWARD DROP

# Clear out any old rules
$IPTABLES -F INPUT 
$IPTABLES -F OUTPUT 
$IPTABLES -F FORWARD 
# 4.1.2 Create userspecified chains

# Create chain for bad tcp packets
$IPTABLES -N bad_tcp_packets

# Clear out any old rules
$IPTABLES -F bad_tcp_packets

# Create separate chains for ICMP, TCP and UDP to traverse
$IPTABLES -N allowed
$IPTABLES -N tcp_packets
$IPTABLES -N udpincoming_packets
$IPTABLES -N icmp_packets

# Clear out any old rules
$IPTABLES -F allowed
$IPTABLES -F tcp_packets
$IPTABLES -F udpincoming_packets
$IPTABLES -F icmp_packets

# 4.1.3 Create content in userspecified chains

# bad_tcp_packets chain
$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG \
-m limit --limit 1/hour --limit-burst 2 --log-prefix "New not syn:"
$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP

# allowed chain
$IPTABLES -A allowed -p TCP --syn -j ACCEPT
$IPTABLES -A allowed -p TCP -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A allowed -p TCP -j DROP

# TCP rules
# Allow ssh only
$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 22 -j allowed

# UDP ports
# No UDP ports at this time. 
#$IPTABLES -A udpincoming_packets -p UDP -s 0/0 --destination-port 53 -j ACCEPT

# ICMP rules
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 \
-m limit --limit 3/second --limit-burst 5 -j ACCEPT
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 \
-m limit --limit 3/second --limit-burst 5 -j ACCEPT

# 4.1.4 INPUT chain

# Bad TCP packets we don't want.
$IPTABLES -A INPUT -p tcp -j bad_tcp_packets

# Rules for special networks not part of the Internet
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $LO_IP -j ACCEPT
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $INET_IP -j ACCEPT

# Special rule for DHCP requests from LAN, which are not caught properly
# otherwise. (DHCP Server)
#$IPTABLES -A INPUT -p UDP -i $LAN_IFACE --dport 67 --sport 68 -j ACCEPT

# Rules for incoming packets from the internet.
$IPTABLES -A INPUT -p ALL -d $INET_IP -m state --state ESTABLISHED,RELATED \
-j ACCEPT
$IPTABLES -A INPUT -p TCP -i $INET_IFACE -j tcp_packets
$IPTABLES -A INPUT -p UDP -i $INET_IFACE -j udpincoming_packets
$IPTABLES -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets

# Log weird packets that don't match the above.
$IPTABLES -A INPUT -m limit --limit 1/hour --limit-burst 2 -j LOG \
--log-level DEBUG --log-prefix "INPUT died:"

# 4.1.5 FORWARD chain

# Bad TCP packets we don't want
$IPTABLES -A FORWARD -p tcp -j bad_tcp_packets

# Accept the packets we actually want to forward
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

# Log weird packets that don't match the above.
$IPTABLES -A FORWARD -m limit --limit 1/hour --limit-burst 2 -j LOG \
--log-level DEBUG --log-prefix "FORWARD died:"

# 4.1.6 OUTPUT chain

# Bad TCP packets we don't want.
$IPTABLES -A OUTPUT -p tcp -j bad_tcp_packets

# Special OUTPUT rules to decide which IP's to allow.
$IPTABLES -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -s $INET_IP -j ACCEPT

# Log weird packets that don't match the above.
$IPTABLES -A OUTPUT -m limit --limit 1/hour --limit-burst 2 -j LOG \
--log-level DEBUG --log-prefix "OUTPUT died:"

######
# 4.2 nat table
# 4.2.1 Set policies
# 4.2.2 Create user specified chains
# 4.2.3 Create content in user specified chains
# 4.2.4 PREROUTING chain
# 4.2.5 POSTROUTING chain

# Enable simple IP Forwarding and Network Address Translation
$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j SNAT --to-source $INET_IP

# 4.2.6 OUTPUT chain

######
# 4.3 mangle table
# 4.3.1 Set policies
# 4.3.2 Create user specified chains
# 4.3.3 Create content in user specified chains
# 4.3.4 PREROUTING chain
# 4.3.5 INPUT chain
# 4.3.6 FORWARD chain
# 4.3.7 OUTPUT chain
# 4.3.8 POSTROUTING chain


}
#
# for bash display all the lines processed by this script. 
#
set -x

case "$1" in
  stop)
 	echo "."
	;;

  start|reload|force-reload|restart)
# do not setup the firewall if in a test fixture. 
    if [ -f /var/run/InTestFixture ]; then
	  exit 0;
	fi
# disable interface 1st to no holes for the window this scipt is being run
	IP="`/usr/local/bin/ipaddr.sh`"
    ifdown eth0
	echo "."
 	do_firewall
	echo "."
# re-enable interface now that we have a firewall up. 
	ifup eth0
	echo "."
	;;

  *)
 	echo "Usage: /etc/init.d/ipchains {start|stop|reload|force-reload|restart}"
	exit 1
esac

exit 0


