#!/bin/sh
#
#	$Id: ifup,v 1.53 2000/11/16 15:58:55 baggins Exp $
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /etc/sysconfig/network
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network-scripts/.functions

need_hostname

DEV=$1

[ -z "$DEV" ] && {
    nls "usage: ifup <device name>" >&2
    exit 1
}

if [ `id -u` != 0 ]; then
    if [ -u /sbin/usernetctl ]; then
	exec /sbin/usernetctl $DEV up
    fi
    nls "Users cannot control this device." >&2
    exit 1
fi

if [ -f "/etc/sysconfig/interfaces/ifcfg-$DEV" ] ; then
	CONFIG="/etc/sysconfig/interfaces/ifcfg-$DEV"
else
	CONFIG="$DEV"
fi

source_config

if [ "foo$2" = "fooboot" ] && is_no "${ONBOOT}"; then
    exit
fi

IPSETUP=no

# full network device name
FULLDEVNAME="$DEVICE"
# detect network device type (ie. dummy, eth for dummy0, eth0 ..)
DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[0-9]*$/,NUL); print $0 } ')
# real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')

if echo "$FULLDEVNAME" | grep -q ':' ; then
    ISALIAS=yes
else
    ISALIAS=no
fi

# Old BOOTP variable
if is_yes "$BOOTP"; then
    BOOTPROTO=bootp
fi

if [ "$BOOTPROTO" = bootp ]; then
    DYNIP=pump
fi

if [ "$BOOTPROTO" = "dhcp" ]; then
    DYNIP=dhcpcd
fi

# IPv4, IPv6 or both ?
[ -n "$IPV6_ADDR" ]             && IPv6=yes || IPv6=no
[ -n "$IPADDR" -o -n "$DYNIP" ] && IPv4=yes || IPv4=no

# Tunnel device ?
[ -f /etc/sysconfig/interfaces/tnlcfg-${FULLDEVNAME} ] && TNL=yes || TNL=no

if [ "$TNL" = "yes" ]; then
	TNLOPTS="onlink"
fi

if [ -z "$PREFIX" -a -z "$NETMASK" ]; then
	PREFIX="32"
fi

if [ -z "$PREFIX" -a -n "$NETMASK" ]; then
	PREFIX="`calcprefix $NETMASK`"
fi

if [ -z "$NETMASK" -a -n "$PREFIX" ]; then
	NETMASK="`calcnetmask $PREFIX`"
fi

is_yes "$MULTICAST"	&& MULTICAST=on			|| MULTICAST=off
[ -n "$SRC_ADDR" ]	&& SRC_ADDR="src ${SRC_ADDR}"	|| SRC_ADDR=""
[ -n "$SCOPE" ]		&& SCOPE="scope ${SCOPE}"	|| SCOPE=""

if [ "$ARP" ]; then
	is_yes "$ARP"    && ARP=on	|| ARP=off
elif [ "$DEVICETYPE" = "eth" ]; then
	ARP=on
else
	ARP=off
fi

OTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-${DEVICETYPE}"

if [ -x "$OTHERSCRIPT" ]; then
    if [ "$DEFAULTHANDLING" = "yes" ]; then
        $OTHERSCRIPT $CONFIG $2
    else
        exec $OTHERSCRIPT $CONFIG $2
    fi
fi

# is this device available? (this catches PCMCIA devices for us)
if ! (/sbin/ip link set multicast ${MULTICAST} dev ${DEVICE} > /dev/null 2>&1); then
    echo "Delaying ${FULLDEVNAME} initialization."
    exit 0
fi

if [ "$SLAVE" = "yes" -a "$ISALIAS" = "no" -a "$MASTER" != "" -a -x /sbin/ifenslave ]; then
    RFLAG="" ; [ "${RECIEVE-ONLY}" = "yes" ] && RFLAG="-r"

    ip link set ${DEVICE} down
    echo "Enslaving $DEVICE to $MASTER"
    ifenslave $RFLAG "$MASTER" "$DEVICE"

    exit 0
fi

if [ -n "$MACADDR" ]; then
   ip link set ${DEVICE} address ${MACADDR}
fi

if [ -n "$DYNIP" -a "$ISALIAS" = "no" ]; then

    echo -n "`nls "Determining IP information for %s" $FULLDEVNAME...`"
    if [ "$DYNIP" = "pump" ]; then
       if /sbin/pump -i $FULLDEVNAME ; then
          nls " done."
       else
          nls " failed."
          exit 1
       fi
    else
       if /sbin/dhcpcd $FULLDEVNAME "$DHCPOPTIONS" ; then
          nls " done."
       else
          nls " failed."
          exit 1
       fi
    fi

else

    if [ "$IPv4" = "yes" ]; then
        if [ -z "$PREFIX" ]; then
	    eval `/bin/ipcalc --prefix ${IPADDR}`
        fi

        if [ -z "$BROADCAST" ]; then
	    eval `/bin/ipcalc --broadcast ${IPADDR} ${NETMASK}`
        fi

        if [ -z "$NETWORK" ]; then
	    eval `/bin/ipcalc --network ${IPADDR} ${NETMASK}`
        fi
    fi

    # IPv4 rules
    if [ "$IPv4" = "yes" ]; then
	if [ -n "$REMIP" ]; then
	    ip -family inet addr add ${IPADDR} peer ${REMIP} dev ${DEVICE} label ${FULLDEVNAME} ${SCOPE}
	else    
    	    ip -family inet addr add ${IPADDR}/${PREFIX} broadcast ${BROADCAST} dev ${DEVICE} label ${FULLDEVNAME} ${SCOPE}
	fi
        # Another IPv4 aliases support
        for ADDR4 in ${IPADDR_ALIASES} ; do
	    ip -family inet addr add ${ADDR4} dev ${DEVICE} label ${FULLDEVNAME} broadcast \+
        done
    fi

    ip link set ${DEVICE} multicast ${MULTICAST} arp ${ARP} up

    # IPv6 rules
    if is_yes "${IPV6_NETWORKING}"; then
      proc_net ipv6/conf/${DEVICE}/autoconf start 0 1 IPV6_AUTOCONF "IPv6 address autoconfiguration on ${DEVICE}"

      for ADDR6 in ${IPV6_ADDR} ; do
	ip -family inet6 addr add ${ADDR6} dev ${DEVICE} label ${FULLDEVNAME}
      done  

      if is_no "${IPV6_FORWARDING}" ; then
	if [ "${GATEWAY6}" != "" ]; then
	  if [ "${GATEWAY6DEV}" = "" -o "${GATEWAY6DEV}" = "${FULLDEVNAME}" ]; then
		# set up default gateway
		ip -family inet6 route add default via ${GATEWAY6} dev ${DEVICE}
	  fi
	fi
      else
	if [ "${IPV6_GLOBALROUTE}" != "" ] ; then
	  if [ "${IPV6_GLOBALROUTEDEV}" = "" -o "${IPV6_GLOBALROUTEDEV}" = "${FULLDEVNAME}" ]; then
	    ip -family inet6 route add 2000::/3 via ${IPV6_GLOBALROUTE} dev ${DEVICE}
	  fi
	fi
      fi
    fi

    if [ "${GATEWAY}" != "" ]; then
	if [ "${GATEWAYDEV}" = "" -o "${GATEWAYDEV}" = "${FULLDEVNAME}" ]; then
	    # set up default gateway
	    ip -family inet route add default via ${GATEWAY} dev ${DEVICE} ${SRC_ADDR} ${TNLOPTS}
	    DEFGW=${GATEWAY}
	fi
    fi

    if [ "$BOOTPROTO" = bootp -a "$ISALIAS" = no ]; then
	if [ -n "$GATEWAYS" ]; then
	    for gw in $GATEWAYS; do
		if [ "$gw" != "${DEFGW}" ]; then
		    ip -family inet route add default via $gw dev ${DEVICE} ${SRC_ADDR} ${TNLOPTS}
		fi
	    done
	fi

	if [ -n "$DNSSRVS" -a -n "$SEARCH" ]; then
	    echo "search $SEARCH" > /etc/resolv.conf
	    for dns in $DNSSRVS; do
		    echo "nameserver $dns" >> /etc/resolv.conf
	    done
	fi

	if [ -n "$BOOTPHOSTNAME" -a -n "$NEEDHOSTNAME" ]; then
	    set_hostname $BOOTPHOSTNAME
	fi
    fi
fi

if is_yes "$IPX"; then
	/etc/sysconfig/network-scripts/ifup-ipx $DEVICE
fi

exec /etc/sysconfig/network-scripts/ifup-post $CONFIG
