#!/bin/sh
#RC:RUNINRC
#
#	$Id: rc.network,v 1.35 1998/11/15 20:30:04 msmith Exp $
#	From: @(#)netstart	5.9 (Berkeley) 3/30/91

# Note that almost all the user-configurable behavior is no longer in
# this file, but rather in /etc/rc.conf.  Please check that file
# first before contemplating any changes here.  If you do need to change
# this file for some reason, we would like to know about it.

echo -n 'Doing initial network setup:'
# Set the host name if it is not already set
if [ -z "`hostname -s`" ] ; then
	    hostname $hostname
	    echo -n ' hostname'
fi

# Set the domainname if we're using NIS
if [ -n "$nisdomainname" -a "x$nisdomainname" != "xNO" ] ; then
	    domainname $nisdomainname
	    echo -n ' domain'
fi
echo '.'

# Initial ATM interface configuration
if [ "X${atm_enable}" = X"YES" -a -f /etc/rc.atm ]; then
	    . /etc/rc.atm
	    atm_pass1
fi

# Set up all the network interfaces, calling startup scripts if needed
for ifn in ${network_interfaces}; do
	    if [ -e /etc/start_if.${ifn} ]; then
		    . /etc/start_if.${ifn}
	    fi
	    # Do the primary ifconfig if specified
	    eval ifconfig_args=\$ifconfig_${ifn}
	    if [ -n "${ifconfig_args}" ] ; then
		    ifconfig ${ifn} ${ifconfig_args}
	    fi
	    # Check to see if aliases need to be added
	    alias=0
	    while :
	    do
		    eval ifconfig_args=\$ifconfig_${ifn}_alias${alias}
		    if [ -n "${ifconfig_args}" ]; then
			    ifconfig ${ifn} ${ifconfig_args} alias
			    alias=`expr ${alias} + 1`
		    else
			    break;
		    fi
	    done
	    # Do ipx address if specified
	    eval ifconfig_args=\$ifconfig_${ifn}_ipx
	    if [ -n "${ifconfig_args}" ]; then
		    ifconfig ${ifn} ${ifconfig_args}
	    fi
	    ifconfig ${ifn}
done

# Initialize IP filtering using ipfw
echo ""
/sbin/ipfw -q flush > /dev/null 2>&1
if [ $? = 0 ] ; then
	firewall_in_kernel=1
else 
	firewall_in_kernel=0
fi

if [ $firewall_in_kernel = 0 -a "x$firewall_enable"  = "xYES" ] ; then
	if kldload ipfw; then
		firewall_in_kernel=1		# module loaded successfully
		echo "Kernel firewall module loaded."
	else
		echo "Warning: firewall kernel module failed to load."
	fi
fi

# Load the filters if required
if [ $firewall_in_kernel = 1 ]; then
	if [ -n "$firewall_enable" -a -f /etc/rc.firewall -a \
		"x$firewall_enable" = "xYES" ] ; then
	    . /etc/rc.firewall
	    echo "Firewall rules loaded."
	else
	    IPFW_DEFAULT=`ipfw l 65535`
	    if [ "$IPFW_DEFAULT" = "65535 deny ip from any to any" ]; then
		echo -n "Warning: kernel has firewall functionality, "
		echo "but firewall rules are not enabled."
		echo "         All ip services are disabled."
	    fi
	fi
fi

# Additional ATM interface configuration
if [ -n "${atm_pass1_done}" ]; then
	    atm_pass2
fi

# Configure routing

if [ "x$defaultrouter" != "xNO" ] ; then
	    static_routes="default ${static_routes}"
	    route_default="default ${defaultrouter}"
fi

# Set up any static routes.  This should be done before router discovery.
if [ "x${static_routes}" != "x" ]; then
	    for i in ${static_routes}; do
		    eval route_args=\$route_${i}
		    route add ${route_args}
	    done
fi

echo -n 'Additional routing options:'
if [ -n "$tcp_extensions" -a "x$tcp_extensions" != "xYES" ] ; then
	    echo -n ' tcp extensions=NO'
	    sysctl -w net.inet.tcp.rfc1323=0 >/dev/null 2>&1
fi

if [ X"$icmp_bmcastecho" = X"YES" ]; then
	    echo -n ' broadcast ping responses=YES'
	    sysctl -w net.inet.icmp.bmcastecho=1 >/dev/null 2>&1
fi

if [ "X$gateway_enable" = X"YES" ]; then
	    echo -n ' IP gateway=YES'
	    sysctl -w net.inet.ip.forwarding=1 >/dev/null 2>&1
fi

if [ "X$forward_sourceroute" = X"YES" ]; then
	    echo -n ' do source routing=YES'
	    sysctl -w net.inet.ip.sourceroute=1 >/dev/null 2>&1
fi

if [ "X$accept_sourceroute" = X"YES" ]; then
	    echo -n ' accept source routing=YES'
	    sysctl -w net.inet.ip.accept_sourceroute=1 >/dev/null 2>&1
fi

if [ "X$ipxgateway_enable" = X"YES" ]; then
	    echo -n ' IPX gateway=YES'
	    sysctl -w net.ipx.ipx.ipxforwarding=1 >/dev/null 2>&1
fi

if [ "X$arpproxy_all" = X"YES" ]; then
	    echo -n ' enabling ARP_PROXY_ALL: '
	    sysctl -w net.link.ether.inet.proxyall=1 2>&1
fi
echo '.'

echo -n 'routing daemons:'
if [ "X$router_enable" = X"YES" ]; then
	    echo -n " ${router}";	${router} ${router_flags}
fi

if [ "X$ipxrouted_enable" = X"YES" ]; then
	    echo -n ' IPXrouted'
	    IPXrouted ${ipxrouted_flags} > /dev/null 2>&1
fi

if [ "X${mrouted_enable}" = X"YES" ]; then
	    echo -n ' mrouted'; mrouted ${mrouted_flags}
fi

if [ "X$rarpd_enable" = X"YES" ]; then
	    echo -n ' rarpd';     rarpd ${rarpd_flags}
fi
echo '.'
network_pass1_done=YES	# Let future generations know we made it.
