#!/bin/bash
PATH=/sbin:/usr/sbin:/bin:/usr/bin
TEXTDOMAIN=initscripts

. /etc/init.d/functions

cd /etc/sysconfig/network-scripts
[ -f ../network ] && . ../network
. network-functions

if [ "$DHCP_HOSTNAME" ] && [ "$DOMAINNAME" ]
then
	DHCP_HOSTNAME=`echo $DHCP_HOSTNAME | sed -e "s/\.$DOMAINNAME//g"`
fi

CONFIG=${1}
boot=${2}
daemon=
[ "foo${boot}" = "fooboot" -o "foo${boot}" = "foodaemon" ] && daemon=1

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

need_config ${CONFIG}

[ -f "${CONFIG}" ] || {
    echo $"$0: configuration for ${1} not found." >&2
    echo $"Usage: ifup <device name>" >&2
    exit 1
}

if [ ${UID} != 0 ]; then
    if [ -x /usr/sbin/usernetctl ]; then
	source_config
	if /usr/sbin/usernetctl ${CONFIG} report ; then
	    exec /usr/sbin/usernetctl ${CONFIG} up
	fi
    fi
    echo $"Users cannot control this device." >&2
    exit 1
fi

source_config

if [ "foo${boot}" = "fooboot" -a "${ONBOOT}" = "no" -o "${ONBOOT}" = "NO" ]
then
    exit 0
fi
if [ -n "$IN_HOTPLUG" -a "${HOTPLUG}" = "no" -o "${HOTPLUG}" = "NO" ]
then
    exit 0
fi
if [ -n "$IN_HOTPLUG" -a "${TYPE}" = "Bridge" ]; 
then
    exit 0
fi

# Ethernet 802.1Q VLAN support
if [ -x /sbin/vconfig -a "${VLAN}" = "yes" ]; then
    if echo ${DEVICE} | LANG=C egrep -v '(:)' | LANG=C egrep -q '(eth|bond)[0-9][0-9]*\.[0-9][0-9]?[0-9]?[0-9]?' ; then
	if [ ! -d /proc/net/vlan ]; then
            if modprobe 8021q >/dev/null 2>&1 ; then
		/sbin/vconfig set_name_type DEV_PLUS_VID_NO_PAD >/dev/null 2>&1 || {
		    echo $"Could not set 802.1Q VLAN parameters."
		}
	    else
		echo $"No 802.1Q VLAN support available in kernel for device ${DEVICE}"
		exit 1
	    fi
	fi
	VID="`echo ${DEVICE} | \
	    LANG=C egrep '(eth|bond)[0-9]+\.[0-9][0-9]?[0-9]?[0-9]?$' | \
	    LANG=C sed 's/^[a-z0-9]*\.//g;s/^0*//'`"
	PHYSDEV="`echo ${DEVICE} | \
	    LANG=C egrep '(eth|bond)[0-9]+\.[0-9][0-9]?[0-9]?[0-9]?$' | \
	    LANG=C sed 's/\.[a-z0-9]*$//g'`"

	is_available ${PHYSDEV} || {
	    if [ "$?" = "1" ] ; then
		echo $"$alias device ${DEVICE} does not seem to be present, delaying initialization."
		exit 1
	    else
		exit 0
	    fi
	}

	# Link on Physical device needs to be up but no ip required
	check_device_down ${PHYSDEV} && {
	    ip -o link set dev ${PHYSDEV} up
	}

	if [ ! -f /proc/net/vlan/${DEVICE} ]; then
	    /sbin/vconfig add ${PHYSDEV} ${VID} || {
		(logger -p daemon.info -t ifup \
		    $"ERROR: could not add vlan ${VID} as ${DEVICE} on dev ${PHYSDEV}" &)&
		echo $"ERROR: could not add vlan ${VID} as ${DEVICE} on dev ${PHYSDEV}"
		exit 1
	    }
	fi

	if [ -f /proc/net/vlan/${DEVICE} ]; then
	    case "$REORDER_HDR" in
		yes|1)
		    /sbin/vconfig set_flag ${DEVICE} 1 1 || {
			(logger -p daemon.info -t ifup \
			    "WARNING: vconfig not able to enable REORDER_HDR on ${DEVICE}" &)&
		    }
		    ;;
		no|0)
		    /sbin/vconfig set_flag ${DEVICE} 1 0 || {
			(logger -p daemon.info -t ifup \
			    $"WARNING: vconfig not able to disable REORDER_HDR on ${DEVICE}" &)&
		    }
		    ;;
	    esac
	fi
    fi
fi

# DVB Card
if [ -n "${DVB_ADAPTER_ID}" -a -n "${DVB_NETWORK_DEMUX}" -a -n "${DVB_NETWORK_PID}" ]; then
       dvbnet -a ${DVB_ADAPTER_ID} -n ${DVB_NETWORK_DEMUX} -p ${DVB_NETWORK_PID}
fi

if [ -x /sbin/ifup-pre-local ]; then
    /sbin/ifup-pre-local ${CONFIG} ${boot}
fi

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

if [ -x ${OTHERSCRIPT} ]; then
    exec ${OTHERSCRIPT} ${CONFIG} ${boot}
fi

# load the module associated with that device
# /sbin/modprobe ${REALDEVICE}
is_available ${REALDEVICE}

if [ "${TYPE}" = "Bridge" ]; then
      if [ ! -x /usr/sbin/brctl ]; then
	  echo $"Bridge support not available: brctl not found"
	  exit 1
      fi
      /sbin/ip link set dev ${DEVICE} down 2>/dev/null
      /usr/sbin/brctl delbr ${DEVICE} 2>/dev/null
      /usr/sbin/brctl addbr ${DEVICE} 2>/dev/null
      # brctl doesn't report success/failure (BZ #104408)
      # Test for ourselves whether it worked.
      if ! /usr/sbin/brctl show | LANG=C grep -q "^${DEVICE}	" ; then
	    echo $"Bridge support not available in this kernel"
	    exit 1
      fi
      [ -n "${DELAY}" ] && /usr/sbin/brctl setfd ${DEVICE} ${DELAY}
      [ -n "${GCINT}" ] && /usr/sbin/brctl setgcint ${DEVICE} ${GCINT}
fi

if [ -n "${BRIDGE}" -a -x /usr/sbin/brctl ]; then
      /sbin/ip addr flush dev ${DEVICE} 2>/dev/null
      /sbin/ip link set dev ${DEVICE} up
      /usr/sbin/brctl addif ${BRIDGE} ${DEVICE}
      # Upon adding a device to a bridge, 
      # it's necessary to make radvd reload its config
      [ -r /var/run/radvd/radvd.pid ] && kill -HUP `cat /var/run/radvd/radvd.pid`
      exit 0
fi 

if [ -n "${ATM_ADDR}" -a -x /usr/sbin/atmarpd -a -x /usr/sbin/atmarp ]; then
      pidof atmarpd >/dev/null 2>&1 || /usr/sbin/atmarpd -b -l syslog >/dev/null 2>&1
      atmarp -c ${DEVICE} 2>/dev/null
fi 

# now check the real state
is_available ${REALDEVICE} || {
      if [ "$?" = "1" ] ; then
         echo $"$alias device ${DEVICE} does not seem to be present, delaying initialization."
         exit 1
      else
         exit 0
      fi
}

if [ -n "${HWADDR}" ]; then
    FOUNDMACADDR=`get_hwaddr ${REALDEVICE}`
    if [ "${FOUNDMACADDR}" != "${HWADDR}" ]; then
        NEWCONFIG=`grep -li "^HWADDR=${FOUNDMACADDR}" /etc/sysconfig/network-scripts/ifcfg-*|head -n 1`
	rm -f /etc/sysconfig/networking/tmp/ifcfg-$DEVICE
	if [ -n "${NEWCONFIG}" ]; then
	    NEWSCRIPT=/etc/sysconfig/networking/tmp/ifcfg-$DEVICE
	    sed -e "s/^DEVICE=.*/DEVICE=$DEVICE/" < ${NEWCONFIG} > $NEWSCRIPT
	   exec /sbin/ifup ${NEWSCRIPT}
	fi
        NEWCONFIG=`grep -li "^HWADDR=${FOUNDMACADDR}" /etc/sysconfig/networking/default/ifcfg-*|head -n 1`
	if [ -n "${NEWCONFIG}" ]; then
	    NEWSCRIPT=/etc/sysconfig/networking/tmp/ifcfg-$DEVICE
	    sed -e "s/^DEVICE=.*/DEVICE=$DEVICE/" < ${NEWCONFIG} > $NEWSCRIPT
	   exec /sbin/ifup ${NEWSCRIPT}
	else
        /sbin/nameif "${REALDEVICE}" "${HWADDR}" || {
	   echo $"Device ${DEVICE} has different MAC address than expected, ignoring."
	   exit 1
	}
	fi
    fi
fi

# is the device wireless? If so, configure wireless device specifics
IS_WIRELESS=
# ralink devices need to be up for wireless-tools to work
[ -d "/sys/class/net/${DEVICE}/wireless" ] && ip link set dev ${DEVICE} up
if is_wireless_device ${DEVICE}; then
    if [ -n "${daemon}" -o "$MII_NOT_SUPPORTED" = "yes" ] ; then
        # try to trigger association
        if [ -n "$WIRELESS_WPA_DRIVER" -a -x /usr/sbin/wpa_supplicant -a -f /etc/wpa_supplicant.conf ]; then
            /usr/sbin/wpa_supplicant -B -i ${DEVICE} \
                -c /etc/wpa_supplicant.conf  \
                -D ${WIRELESS_WPA_DRIVER}
        else
	    WIRELESS_CONFIG="wireless.d/$WIRELESS_ESSID"
	    [ -f "$WIRELESS_CONFIG" ] && source $WIRELESS_CONFIG
	    . ./ifup-wireless
	fi
    elif [ "$WIRELESS_MODE" = "Roaming" ]; then
        # roaming enabled, on association
        # source interface parameters related to current ESSID or AP if any
        WIRELESS_CURRENT_ESSID=`iwgetid -r ${DEVICE}`
        if [ -n "$WIRELESS_CURRENT_ESSID" ]; then
            WIRELESS_CONFIG="wireless.d/$WIRELESS_CURRENT_ESSID"
            if [ ! -f "$WIRELESS_CONFIG" ]; then
                WIRELESS_CURRENT_AP=`iwgetid -r -a ${DEVICE}`
                WIRELESS_CONFIG="wireless.d/$WIRELESS_CURRENT_AP"
            fi
            if [ -f "$WIRELESS_CONFIG" ]; then
                source $WIRELESS_CONFIG
            else
                echo "Wireless device $DEVICE is associated with $WIRELESS_CURRENT_ESSID but configuration is missing"
            fi
        else
            echo "Wireless device $DEVICE is configured with a roaming daemon but isn't associated"
        fi
    fi
    IS_WIRELESS=1
fi

# slave device?
if [ "${SLAVE}" = yes -a "${ISALIAS}" = no -a "${MASTER}" != "" -a \
     -x /sbin/ifenslave ]; then
    RFLAG="" ; [ "${RECEIVEONLY}" = yes ] && RFLAG="-r"

    /sbin/ip link set dev ${DEVICE} down
    echo $"Enslaving ${DEVICE} to ${MASTER}"
    ifenslave ${RFLAG} "${MASTER}" "${DEVICE}" >/dev/null 2>&1

    if [ -n "$ETHTOOL_OPTS" ] ; then
        ethtool -s ${REALDEVICE} $ETHTOOL_OPTS
    fi

    exit 0
fi

# Bonding initialization. For DHCP, we need to enslave the devices early,
# so it can actually get an IP.
if [ "${TYPE}" =  "Bonding" ] || ethtool -i $DEVICE 2>/dev/null| grep -q "driver: bonding" ; then
    /sbin/ip link set dev ${DEVICE} down
    /sbin/ip link set dev ${DEVICE} up
    for device in `fgrep -l "MASTER=${DEVICE}" /etc/sysconfig/network-scripts/ifcfg-*` ; do
        if [ "$BOOTPROTO" = "dhcp" ]; then
		/sbin/ifup ${device##*/}
    	fi
    done
fi

# this isn't the same as the MAC in the configuration filename.  It is
# available as a configuration option in the config file, forcing the kernel
# to think an ethernet card has a different MAC address than it really has.
if [ -n "${MACADDR}" ]; then
   ip link set dev ${DEVICE} address ${MACADDR}
fi

# exec ifplugd daemon at boot if supported
if [ -n "${daemon}" -a "$MII_NOT_SUPPORTED" != "yes" ]; then
    IFPLUGD_ARGS="${IFPLUGD_ARGS=-b}"
    exec /sbin/ifplugd $IFPLUGD_ARGS -i $DEVICE
fi

# Old BOOTP variable
if [ "${BOOTP}" = "yes" ]; then
    BOOTPROTO=bootp
fi

if [ "${BOOTPROTO}" = "bootp" -o "${BOOTPROTO}" = "dhcp" ]; then
    DYNCONFIG=true
    [ "${BOOTPROTO}" = bootp ] && DHCP_CLIENT=/sbin/pump
fi

BLURB="# temporary ifup addition"

if [ -n "$DYNCONFIG" -a "XXX$DHCP_CLIENT" = "XXX" ];then
    if [ -x /sbin/dhclient ];then
	DHCP_CLIENT=/sbin/dhclient
	# Remove any temporary references which were previously added to dhclient config
	if [ -w /etc/dhclient-${DEVICE}.conf -a -x /sbin/dhclient ] ; then
	    grep -v "$BLURB" /etc/dhclient-${DEVICE}.conf > /etc/dhclient-${DEVICE}.conf.ifupnew 2> /dev/null
	    cat /etc/dhclient-${DEVICE}.conf.ifupnew > /etc/dhclient-${DEVICE}.conf
	    rm -f /etc/dhclient-${DEVICE}.conf.ifupnew
	fi
    elif [ -x /sbin/dhcpcd ];then
	DHCP_CLIENT=/sbin/dhcpcd
    elif [ -x /sbin/pump ];then
	DHCP_CLIENT=/sbin/pump
    elif [ -x /sbin/dhcpxd ];then
	DHCP_CLIENT=/sbin/dhcpxd
    else 
	echo $"Can't find a dhcp client"
	exit 1;
    fi
fi  

DHCP_ARGS=
if [ "XXX$DHCP_CLIENT" != "XXX" ];then
case $(basename $DHCP_CLIENT) in
    dhcpcd)
    [ -n "$DHCP_HOSTNAME" ] && DHCP_ARGS="-h $DHCP_HOSTNAME"
    [ -n "$NEEDHOSTNAME" ] && DHCP_ARGS="$DHCP_ARGS -H"
    [ "${PEERDNS}" = "no" ] && DHCP_ARGS="$DHCP_ARGS -R"
    [ "${PEERYP}" = "yes" ] || DHCP_ARGS="$DHCP_ARGS -Y"
    [ "${PEERNTPD}" = "yes" ] || DHCP_ARGS="$DHCP_ARGS -N"
    [ "X${DOMAINNAME}" = "X" ] && DHCP_ARGS="$DHCP_ARGS -D"
    [ -n "$DHCP_TIMEOUT" ] && DHCP_ARGS="$DHCP_ARGS -t ${DHCP_TIMEOUT}"
    DHCP_ARGS="$DHCP_ARGS $DEVICE"
    PIDF=/etc/dhcpc/dhcpcd-$DEVICE.pid
    if [[ -f $PIDF ]];then
	PID=$(cat /etc/dhcpc/dhcpcd-$DEVICE.pid)
	if [[ -n $PID ]] && kill -0 $PID;then
	    echo "dhcpcd is running ifdown it before"
	    exit;
	fi
	/bin/rm -f $PIDF; #clear it
    fi
    ;;
    pump)
    [ -n "$DHCP_HOSTNAME" ] && DHCP_ARGS="-h $DHCP_HOSTNAME"
    [ -n "$NEEDHOSTNAME" ] && DHCP_ARGS="$DHCP_ARGS --lookup-hostname"
    [ "${PEERDNS}" = "no" ] && DHCP_ARGS="$DHCP_ARGS -d"
    DHCP_ARGS="$DHCP_ARGS -i $DEVICE"
    ;;
    dhcpxd)
    # Dhcpxd don't support NEED_HOSTNAME ? guess i need time to patch
    # this animal
    [ -n "$DHCP_HOSTNAME" ] && DHCP_ARGS="-H $HOSTNAME"
    DHCP_ARGS="$DHCP_ARGS --wait $DEVICE"
    ;;
    dhclient)
    DHCP_ARGS="$DHCP_ARGS -1 -q -lf /var/lib/dhcp/dhclient-${DEVICE}.leases -pf /var/run/dhclient-${DEVICE}.pid -cf /etc/dhclient-${DEVICE}.conf $DEVICE"
    if [ -n "$DHCP_HOSTNAME" ]; then
	if [ ! -e /etc/dhclient-${DEVICE}.conf ] || ! grep -q "[^# ]*send *host-name" /etc/dhclient-${DEVICE}.conf; then
	    echo "send host-name \"${DHCP_HOSTNAME}\";  $BLURB" >> /etc/dhclient-${DEVICE}.conf
	fi
    fi
    if [ -n "$DHCP_TIMEOUT" ]; then
	if [ ! -e /etc/dhclient-${DEVICE}.conf ] || ! grep -q "[^# ]*timeout" /etc/dhclient-${DEVICE}.conf; then
	    echo "timeout $DHCP_TIMEOUT;  $BLURB" >> /etc/dhclient-${DEVICE}.conf
	fi
    fi	
    ;;
esac
fi

if [ -n "${MTU}" ]; then
   ip link set dev ${DEVICE} mtu ${MTU}
fi

# Remove any temporary references which were previously added to dhclient config
if [ -w /etc/dhclient-${DEVICE}.conf ] && [ -x /sbin/dhclient ] ; then
   LC_ALL=C grep -v "# temporary MDK ifup addition" /etc/dhclient-${DEVICE}.conf > /etc/dhclient-${DEVICE}.conf.ifupnew 2> /dev/null
   cat /etc/dhclient-${DEVICE}.conf.ifupnew > /etc/dhclient-${DEVICE}.conf
   rm -f /etc/dhclient-${DEVICE}.conf.ifupnew
fi
		     
if [ -n "${DYNCONFIG}" ]; then
    PUMPARGS=$PUMPARGS
    DHCPCDARGS="$DHCPCDARGS -n"
    if [[ "${PERSISTENT_DHCLIENT}" =  [yY1]* ]]; then
       ONESHOT="";
    else
       ONESHOT="-1";
    fi;
    DHCLIENTARGS="${DHCLIENTARGS} ${ONESHOT} -q -lf /var/lib/dhcp/dhclient-${DEVICE}.leases -pf /var/run/dhclient-${DEVICE}.pid -cf /etc/dhclient-${DEVICE}.conf"
    if [ -n "${DHCP_HOSTNAME}" ]; then
       # Send a host-name to the DHCP server (requ. by some dhcp servers).
       PUMPARGS="${PUMPARGS} -h ${DHCP_HOSTNAME}"
       DHCPCDARGS="${DHCPCDARGS} -h ${DHCP_HOSTNAME}"
       if [ -x /sbin/dhclient ] ; then
          if [ -w /etc/dhclient-${DEVICE}.conf ] ; then
             if ! LC_ALL=C grep "send *host-name *\"${DHCP_HOSTNAME}\"" /etc/dhclient-${DEVICE}.conf > /dev/null 2>&1 ; then
                echo "send host-name \"${DHCP_HOSTNAME}\";  # temporary MDK ifup addition" >> /etc/dhclient-${DEVICE}.conf
             fi
          elif ! [ -e /etc/dhclient-${DEVICE}.conf ] ; then
             echo "send host-name \"${DHCP_HOSTNAME}\";  # temporary MDK ifup addition" >> /etc/dhclient-${DEVICE}.conf
          fi
       fi
    fi
    if need_hostname; then
       # Set hostname of host to host-name option supplied by DHCP.
       PUMPARGS="${PUMPARGS} --lookup-hostname"
       DHCPCDARGS="${DHCPCDARGS} -H"
    fi
    if [ "${PEERDNS}" = "no" ]; then
       # Do not update/replace resolv.conf.
       PUMPARGS="${PUMPARGS} -d"
       DHCPCDARGS="${DHCPCDARGS} -R"
    fi
    echo
    echo -n $"Determining IP information for ${DEVICE}..."
    if $DHCP_CLIENT $DHCP_ARGS ; then
	echo $" done."
    else
	# Associate a zeroconf IP address to an alias of the interface
	if [ -x /sbin/zcip ]; then
	    if /sbin/zcip -s -i $DEVICE:${ZEROCONF_ALIAS_NUMER=9}; then
		echo $" done."
		if [ -x /etc/init.d/tmdns ]; then
		    /etc/init.d/tmdns reload > /dev/null 2>&1
		fi
		exit 0
	    fi
	fi
	echo $" failed."
	exit 1
    fi

    # DHCP likes to create duplicate routes.  Fix that up.
    NUMDEFROUTES=`ip -o route | grep ${DEVICE} | \
                  grep "^default" | \
                  awk '{ nlines++ } END { if (nlines > 0) print nlines; else print 0 }'`
    if [ -n "$NUMDEFROUTES" -a "$NUMDEFROUTES" -gt 1 ]; then
	# remove the default route for the new device (old route wins)
	ip route del default dev ${DEVICE}
    fi
# end dynamic device configuration
else 
    if [ -z "${IPADDR}" ]; then
         # enable device without IP, useful for e.g. PPPoE
	 ip link set dev ${REALDEVICE} up
	 # Bonding initialization part II
	 if [ "${TYPE}" =  "Bonding" ] || ethtool -i $DEVICE 2>/dev/null| grep -q "driver: bonding" ; then
    		for device in `fgrep -l "MASTER=${DEVICE}" /etc/sysconfig/network-scripts/ifcfg-*` ; do
			/sbin/ifup ${device##*/}
    		done
	 fi

	 if [ "${NETWORKING_IPV6}" = "yes" ]; then
	    /etc/sysconfig/network-scripts/ifup-ipv6 ${CONFIG}
	 fi
	 exec /etc/sysconfig/network-scripts/ifup-post ${CONFIG} ${2}
    fi
  
    expand_config
    
    [ -n "${ARP}" ] && \
	ip link set dev ${REALDEVICE} $(toggle_value arp $ARP)
   
    if ! ip link set dev ${REALDEVICE} up ; then
	echo $"Failed to bring up ${DEVICE}."
	exit 1
    fi

    if [ -n "$ETHTOOL_OPTS" ] ; then
        ethtool -s ${REALDEVICE} $ETHTOOL_OPTS
    fi

    if ! arping -q -c 2 -w 3 -D -I ${REALDEVICE} ${IPADDR} ; then
	echo $"Error, some other host already uses address ${IPADDR}."
	exit 1
    fi

    if [ "${DEVICE}" = "lo" ]; then
    	SCOPE="scope host"
    else
        SCOPE=${SCOPE:-}
    fi
    
    if [ -n "$SRCADDR" ]; then
       SRC="src $SRCADDR"
    else
       SRC=
    fi
    
    if ! LC_ALL=C ip addr ls ${REALDEVICE} | LC_ALL=C grep -q "${IPADDR}/${PREFIX}" ; then
	 if ! ip addr add ${IPADDR}/${PREFIX} \
	    brd ${BROADCAST:-+} dev ${REALDEVICE} ${SCOPE} label ${DEVICE}; then
	    echo $"Error adding address ${IPADDR} for ${DEVICE}."
	 fi
    fi
    
    if [ -n "$SRCADDR" ]; then
           sysctl -n -w "net.ipv4.conf.${REALDEVICE}.arp_filter=1" >/dev/null 2>&1
    fi

    # update ARP cache of neighboring computers
    arping -q -A -c 1 -I ${REALDEVICE} ${IPADDR}
    ( sleep 2;
      arping -q -U -c 1 -I ${REALDEVICE} ${IPADDR} ) > /dev/null 2>&1 < /dev/null &

    # Set a default route.
    if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${REALDEVICE}" ]; then
	# set up default gateway. replace if one already exists
	if [ -n "${GATEWAY}" -a "`ipcalc --network ${GATEWAY} ${NETMASK} 2>/dev/null`" = "NETWORK=${NETWORK}" ]; then
	    if [ -n "${ATM_ADDR}" -a -x /usr/sbin/atmarpd -a -x /usr/sbin/atmarp ]; then
		ip link set dev ${REALDEVICE} down
		ip link set dev ${REALDEVICE} up
		sleep 2
		atmarp -s ${GATEWAY} ${ATM_ADDR} null 2>/dev/null
	    fi 
	    ip route replace default via ${GATEWAY} ${WINDOW:+window $WINDOW} ${SRC}
	elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
	    ip route replace default ${SRC} ${WINDOW:+window $WINDOW} dev ${REALDEVICE}
	fi
    fi
    
fi

# Bonding initialization part II - for static, enslave the devices. For
# DHCP, remove any routes for the slaves.
if [ "${TYPE}" =  "Bonding" ] || ethtool -i $DEVICE 2>/dev/null| grep -q "driver: bonding" ; then
    for device in `fgrep -l "MASTER=${DEVICE}" /etc/sysconfig/network-scripts/ifcfg-*` ; do
	if [ "$BOOTPROTO" = "dhcp" ]; then
		DEV=$DEVICE
                eval $(LANG=C fgrep "DEVICE=" $device)
		ifenslave -d $DEV DEVICE
		DEVICE=$DEV
	fi
	/sbin/ifup ${device##*/}
    done
fi

# IPv6 initialisation?
if [ "${NETWORKING_IPV6}" = "yes" ]; then
    /etc/sysconfig/network-scripts/ifup-ipv6 ${CONFIG}
fi

if [ "${IPX}" = yes ]; then
       /etc/sysconfig/network-scripts/ifup-ipx ${DEVICE}
fi

exec /etc/sysconfig/network-scripts/ifup-post ${CONFIG} ${2}
