#!/bin/sh
#####################################################################
echo IP MASQ script by Mongoose stu7440@westga.edu
echo http://www.westga.edu/~stu7440

# This script should be easy to modify to your needs.
# I wrote thid for a fully connected private network at home.
# If you have ideas, comments, or suggestions email me!
#####################################################################

#####################################################################
# Use this handy dandy chart from the IP MASQ HOWTO
# to configure subnets
#
#       netmask         | N  | Subnet
#       ~~~~~~~~~~~~~~~~|~~~~|~~~~~~~~~~~~~~~
#       255.0.0.0       | 8  | Class A
#       255.255.0.0     | 16 | Class B
#       255.255.255.0   | 24 | Class C
#       255.255.255.255 | 32 | Point-to-point
#
# EXAMPLE: Allow all machines on 10.0.0.X 
#   
#   FORWARD_A = 10.0.0.0 
#   FMASK_A = 24
#
#   ipchains -A forward -s 10.0.0.0/24 -j MASQ
#
# I have only 3 machines I want to reach the outside net
#####################################################################
FORWARD_A="10.0.0.3"
FMASK_A="32"
FORWARD_B="10.0.1.2"
FMASK_B="32"
FORWARD_C="10.0.1.3"
FMASK_C="32"

#####################################################################
# I use a dailup connection, here I use ppp0 as my gw to internet
#####################################################################
GATEWAY="0.0.0.0"

#####################################################################
# The eth0 card connects to network ( 10.0.0.0, machine A )
#####################################################################
ETH0_IP="10.0.0.1"
NETWORK_ETH0="10.0.0.0"
NETMASK_ETH0="255.255.255.0"
ETH0_DRIVER="tulip"

#####################################################################
# The eth1 card connects to network ( 10.0.1.0, machines B and C ) 
#####################################################################
ETH1_IP="10.0.1.1"
NETWORK_ETH1="10.0.1.0"
NETMASK_ETH1="255.255.255.0"
ETH1_DRIVER="ne2k-pci"



#####################################################################
# If you're gateway is setup like mine no need to edit below here
#####################################################################



#####################################################################
# This section is for arg handling
#####################################################################

case "$1" in
    start)
    ;;

    stop)       
    ;;

    restart)
	/sbin/ipchains --flush
	/sbin/ifconfig eth0 down
	/sbin/ifconfig eth1 down
    ;;
    
    reload)
    ;;

    force-reload)
    ;;

    *)
      echo "Usage: /etc/init.d/ip_masq-gw.sh {start|stop|reload|restart|force-reload}" >&2
      exit 1
    ;;
esac


echo {

#####################################################################
# This brings up the network interface cards
#####################################################################
echo -n Initializing eth0...
/sbin/modprobe ${ETH0_DRIVER}
/sbin/ifconfig eth0 ${ETH0_IP} netmask ${NETMASK_ETH0} up
/sbin/route add -net ${NETWORK_ETH0} netmask ${NETMASK_ETH0} eth0
/sbin/route add default gw ${GATEWAY} eth0
echo done

echo -n Initializing eth1...
/sbin/modprobe ${ETH1_DRIVER}
/sbin/ifconfig eth1 ${ETH1_IP} netmask ${NETMASK_ETH1} up
/sbin/route add -net ${NETWORK_ETH1} netmask ${NETMASK_ETH1} eth1
/sbin/route add default gw ${GATEWAY} eth1
echo done


#####################################################################
# This allows your box to forward packets
#####################################################################
echo -n Acrtivating IP Forwading...
echo "1" > /proc/sys/net/ipv4/ip_forward
echo done

#####################################################################
# This prevents smurf attacks, etc
#####################################################################
echo -n Activating tcp cookies...
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
echo done


#####################################################################
# This sets up IP forwarding for the clients/networks
#####################################################################
echo -n Starting IP MASQ rules...
/sbin/ipchains -P forward DENY
/sbin/ipchains -A forward -s ${FORWARD_A}/${FMASK_A} -j MASQ
/sbin/ipchains -A forward -s ${FORWARD_B}/${FMASK_B} -j MASQ
/sbin/ipchains -A forward -s ${FORWARD_C}/${FMASK_C} -j MASQ
echo done

echo -n Loading Masq modules [
/sbin/modprobe ip_masq_ftp
echo -n .
/sbin/modprobe ip_masq_irc
echo -n .
/sbin/modprobe ip_masq_quake
echo -n .
/sbin/modprobe ip_masq_raudio
echo -n .
/sbin/modprobe ip_masq_user
echo -n .
/sbin/modprobe ip_masq_autofw
echo -n .
/sbin/modprobe ip_masq_mfw
echo -n .
/sbin/modprobe ip_masq_cuseeme
echo -n .
/sbin/modprobe ip_masq_portfw
echo -n .
/sbin/modprobe ip_masq_vdolive
echo .]


#####################################################################
# Add routes for ip masq clients here
#####################################################################
echo -n Adding routes for masq clients...
/sbin/route add -host ${FORWARD_A} gw ${ETH0_IP} eth0
/sbin/route add -host ${FORWARD_B} gw ${ETH1_IP} eth1
/sbin/route add -host ${FORWARD_C} gw ${ETH1_IP} eth1
echo done

echo }




#####################################################################
# Add rules for PPP fw here
#####################################################################

# Allow web server use ( httpd )
/sbin/ipchains -A input -d 0.0.0.0/0 80 -i ppp+ -p tcp -j ACCEPT
/sbin/ipchains -A input -d 0.0.0.0/0 80 -i ppp+ -p udp -j ACCEPT

# Don't allow any other low port
# Add '-l' for logging of denyed attempts
/sbin/ipchains -A input -d 0.0.0.0/0 0:1024 -i ppp+ -p tcp -j DENY
/sbin/ipchains -A input -d 0.0.0.0/0 0:1024 -i ppp+ -p udp -j DENY



exit 0

