1ba655a
#!/bin/bash
1ba655a
# Network Interface Configuration System
1ba655a
# Copyright (c) 1996-2005 Red Hat, Inc. all rights reserved.
1ba655a
#
1ba655a
# This software may be freely redistributed under the terms of the GNU
1ba655a
# public license.
1ba655a
#
1ba655a
# You should have received a copy of the GNU General Public License
1ba655a
# along with this program; if not, write to the Free Software
1ba655a
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1ba655a
1ba655a
. /etc/init.d/functions
1ba655a
1ba655a
cd /etc/sysconfig/network-scripts
1ba655a
. ./network-functions
1ba655a
1ba655a
[ -f ../network ] && . ../network
1ba655a
1ba655a
CONFIG=${1}
1ba655a
1ba655a
need_config ${CONFIG}
1ba655a
1ba655a
source_config
1ba655a
1ba655a
# InfiniBand doesn't support dhcp...yet
1ba655a
if [ "${BOOTPROTO}" = "dhcp" ]; then
1ba655a
    DYNCONFIG=true
1ba655a
fi
1ba655a
1ba655a
# load the module associated with that device
1ba655a
# /sbin/modprobe ${REALDEVICE}
1ba655a
is_available ${REALDEVICE}
1ba655a
1ba655a
# remap, if the device is bound with a MAC address and not the right device num
1ba655a
# bail out, if the MAC does not fit
1ba655a
if [ -n "${HWADDR}" ]; then
1ba655a
    FOUNDMACADDR=`get_hwaddr ${REALDEVICE}`
1ba655a
    if [ "${FOUNDMACADDR}" != "${HWADDR}" ]; then
1ba655a
        curdev=`get_device_by_hwaddr ${HWADDR}`
1ba655a
        if [ -n "$curdev" ]; then
1ba655a
	  rename_device "${REALDEVICE}" "${HWADDR}" "${curdev}" || {
1ba655a
	    echo $"Device ${DEVICE} has different MAC address than expected, ignoring."
1ba655a
	    exit 1
1ba655a
	  }
1ba655a
	fi
1ba655a
    fi
1ba655a
fi
1ba655a
1ba655a
# now check the real state
1ba655a
is_available ${REALDEVICE} || {
1ba655a
      if [ -n "$alias" ]; then
1ba655a
         echo $"$alias device ${DEVICE} does not seem to be present, delaying initialization."
1ba655a
      else
1ba655a
         echo $"Device ${DEVICE} does not seem to be present, delaying initialization."
1ba655a
      fi
1ba655a
      exit 1
1ba655a
}
1ba655a
1ba655a
# this isn't the same as the MAC in the configuration filename.  It is
1ba655a
# available as a configuration option in the config file, forcing the kernel
1ba655a
# to think an ethernet card has a different MAC address than it really has.
1ba655a
if [ -n "${MACADDR}" ]; then
1ba655a
   ip link set dev ${DEVICE} address ${MACADDR}
1ba655a
fi
1ba655a
65d850f
# First, do we even support setting connected mode?
65d850f
if [ -e /sys/class/net/${DEVICE}/mode ]; then
65d850f
    # OK, set the mode in all cases, that way it gets reset on a down/up
65d850f
    # cycle, allowing people to change the mode without rebooting
65d850f
    if [ "${CONNECTED_MODE}" = yes ]; then
65d850f
        echo connected > /sys/class/net/${DEVICE}/mode
65d850f
	# cap the MTU where we should based upon mode
65d850f
	if [ -n "${MTU}" -a $MTU -gt 65520 ]; then
65d850f
	    MTU=65520
65d850f
	fi
65d850f
    else
65d850f
        echo datagram > /sys/class/net/${DEVICE}/mode
65d850f
	# cap the MTU where we should based upon mode
65d850f
	if [ -n "${MTU}" -a $MTU -gt 2044 ]; then
65d850f
	    MTU=2044
65d850f
	fi
65d850f
    fi
65d850f
fi
65d850f
65d850f
if [ -n "${MTU}" ]; then
65d850f
    ip link set dev ${DEVICE} mtu ${MTU}
65d850f
fi
1ba655a
1ba655a
# slave device?
1ba655a
if [ "${SLAVE}" = yes -a "${ISALIAS}" = no -a "${MASTER}" != "" ]; then
1ba655a
    /sbin/ip link set dev ${DEVICE} down
1ba655a
    echo "+${DEVICE}" > /sys/class/net/${MASTER}/bonding/slaves 2>/dev/null
1ba655a
1ba655a
    if [ -n "$ETHTOOL_OPTS" ] ; then
1ba655a
        /sbin/ethtool -s ${REALDEVICE} $ETHTOOL_OPTS
1ba655a
    fi
1ba655a
1ba655a
    exit 0
1ba655a
fi
1ba655a
1ba655a
# Bonding initialization. For DHCP, we need to enslave the devices early,
1ba655a
# so it can actually get an IP.
1ba655a
if [ "$ISALIAS" = no ] && is_bonding_device ${DEVICE} ; then
1ba655a
1ba655a
    /sbin/ip link set dev ${DEVICE} down
1ba655a
1ba655a
    # add the bits to setup driver parameters here
1ba655a
    for arg in $BONDING_OPTS ; do   
1ba655a
        key=${arg%%=*};
1ba655a
        value=${arg##*=};
1ba655a
        if [ "${key}" = "arp_ip_target" ]; then
1ba655a
            OLDIFS=$IFS;
1ba655a
            IFS=',';
1ba655a
            for arp_ip in $value; do
1ba655a
                echo +$arp_ip > /sys/class/net/${DEVICE}/bonding/$key
1ba655a
            done
1ba655a
            IFS=$OLDIFS;
1ba655a
        else
1ba655a
            echo $value > /sys/class/net/${DEVICE}/bonding/$key
1ba655a
        fi
1ba655a
    done
1ba655a
1ba655a
    /sbin/ip link set dev ${DEVICE} up
1ba655a
    [ -n "${LINKDELAY}" ] && /bin/sleep ${LINKDELAY}
1ba655a
    for device in `LANG=C grep -l "^[[:space:]]*MASTER=${DEVICE}" /etc/sysconfig/network-scripts/ifcfg-*` ; do
1ba655a
	    is_ignored_file "$device" && continue
1ba655a
	    /sbin/ifup ${device##*/}
1ba655a
    done
1ba655a
1ba655a
    # add the bits to setup the needed post enslavement parameters
1ba655a
    for arg in $BONDING_OPTS ; do   
1ba655a
        key=${arg%%=*};
1ba655a
        value=${arg##*=};
1ba655a
	if [ "${key}" = "primary" ]; then 
1ba655a
            echo $value > /sys/class/net/${DEVICE}/bonding/$key
1ba655a
	fi
1ba655a
    done
1ba655a
fi
1ba655a
1ba655a
		     
1ba655a
if [ -n "${DYNCONFIG}" -a -x /sbin/dhclient ]; then
1ba655a
    # Remove any temporary references which were previously added to dhclient config
1ba655a
    if [ -w /etc/dhclient-${DEVICE}.conf ] ; then
1ba655a
        LC_ALL=C grep -v "# temporary RHL ifup addition" /etc/dhclient-${DEVICE}.conf > /etc/dhclient-${DEVICE}.conf.ifupnew 2> /dev/null
1ba655a
        cat /etc/dhclient-${DEVICE}.conf.ifupnew > /etc/dhclient-${DEVICE}.conf
1ba655a
        rm -f /etc/dhclient-${DEVICE}.conf.ifupnew
1ba655a
    fi
1ba655a
    if [[ "${PERSISTENT_DHCLIENT}" =  [yY1]* ]]; then
1ba655a
       ONESHOT="";
1ba655a
    else
1ba655a
       ONESHOT="-1";
1ba655a
    fi;
1ba655a
    if [ -n "${DHCP_HOSTNAME}" ]; then
1ba655a
       # Send a host-name to the DHCP server (requ. by some dhcp servers).
1ba655a
       if [ -w /etc/dhclient-${DEVICE}.conf ] ; then
1ba655a
           if ! LC_ALL=C grep "send *host-name *\"${DHCP_HOSTNAME}\"" /etc/dhclient-${DEVICE}.conf > /dev/null 2>&1 ; then
1ba655a
               echo "send host-name \"${DHCP_HOSTNAME}\";  # temporary RHL ifup addition" >> /etc/dhclient-${DEVICE}.conf
1ba655a
           fi
1ba655a
       elif ! [ -e /etc/dhclient-${DEVICE}.conf ] ; then
1ba655a
           echo "send host-name \"${DHCP_HOSTNAME}\";  # temporary RHL ifup addition" >> /etc/dhclient-${DEVICE}.conf
1ba655a
       fi
1ba655a
    fi
1ba655a
    # allow users to use generic '/etc/dhclient.conf' (as documented in manpage!) 
1ba655a
    # if per-device file doesn't exist or is empty
1ba655a
    if [ -s /etc/dhclient-${DEVICE}.conf ]; then
1ba655a
       DHCLIENTCONF="-cf /etc/dhclient-${DEVICE}.conf";
1ba655a
    else
1ba655a
       DHCLIENTCONF='';
1ba655a
    fi;
1ba655a
    # copy any lease obtained by the initrd
1ba655a
    if [ -f /dev/.dhclient-${DEVICE}.leases ] ; then
1ba655a
    	mv -f /dev/.dhclient-${DEVICE}.leases /var/lib/dhclient/dhclient-${DEVICE}.leases
1ba655a
	[ -x /sbin/restorecon ] && restorecon /var/lib/dhclient/dhclient-${DEVICE}.leases > /dev/null 2>&1
1ba655a
    fi
1ba655a
    DHCLIENTARGS="${DHCLIENTARGS} ${ONESHOT} -q ${DHCLIENTCONF} -lf /var/lib/dhclient/dhclient-${DEVICE}.leases -pf /var/run/dhclient-${DEVICE}.pid"
1ba655a
    echo
1ba655a
    echo -n $"Determining IP information for ${DEVICE}..."
1ba655a
    if check_link_down ${DEVICE}; then
1ba655a
	echo $" failed; no link present.  Check cable?"
1ba655a
	ip link set dev ${DEVICE} down >/dev/null 2>&1
1ba655a
	exit 1
1ba655a
    fi
1ba655a
1ba655a
    if [ -n "$ETHTOOL_OPTS" ] ; then
1ba655a
        /sbin/ethtool -s ${REALDEVICE} $ETHTOOL_OPTS
1ba655a
    fi
1ba655a
1ba655a
    if /sbin/dhclient ${DHCLIENTARGS} ${DEVICE} ; then
1ba655a
	echo $" done."
1ba655a
    else
1ba655a
	echo $" failed."
1ba655a
	exit 1
1ba655a
    fi
1ba655a
# end dynamic device configuration
1ba655a
else 
1ba655a
    if [ -z "${IPADDR}" ]; then
1ba655a
         # enable device without IP, useful for e.g. PPPoE
1ba655a
	 ip link set dev ${REALDEVICE} up
1ba655a
	 if [ -n "$ETHTOOL_OPTS" ] ; then
1ba655a
	 	/sbin/ethtool -s ${REALDEVICE} $ETHTOOL_OPTS
1ba655a
	 fi
1ba655a
	 [ -n "${LINKDELAY}" ] && /bin/sleep ${LINKDELAY}
1ba655a
1ba655a
	 if [ "${NETWORKING_IPV6}" = "yes" ]; then
1ba655a
	    /etc/sysconfig/network-scripts/ifup-ipv6 ${CONFIG}
1ba655a
	 fi
1ba655a
	 exec /etc/sysconfig/network-scripts/ifup-post ${CONFIG} ${2}
1ba655a
    fi
1ba655a
  
1ba655a
    expand_config
1ba655a
    
1ba655a
    [ -n "${ARP}" ] && \
1ba655a
	ip link set dev ${REALDEVICE} $(toggle_value arp $ARP)
1ba655a
   
1ba655a
    if ! ip link set dev ${REALDEVICE} up ; then
1ba655a
	echo $"Failed to bring up ${DEVICE}."
1ba655a
	exit 1
1ba655a
    fi
1ba655a
1ba655a
    if [ -n "$ETHTOOL_OPTS" ] ; then
1ba655a
        /sbin/ethtool -s ${REALDEVICE} $ETHTOOL_OPTS
1ba655a
    fi
1ba655a
1ba655a
    [ -n "${LINKDELAY}" ] && /bin/sleep ${LINKDELAY}
1ba655a
1ba655a
    if [ "${DEVICE}" = "lo" ]; then
1ba655a
    	SCOPE="scope host"
1ba655a
    else
1ba655a
        SCOPE=${SCOPE:-}
1ba655a
    fi
1ba655a
    
1ba655a
    if [ -n "$SRCADDR" ]; then
1ba655a
       SRC="src $SRCADDR"
1ba655a
    else
1ba655a
       SRC=
1ba655a
    fi
1ba655a
    
1ba655a
    if ! LC_ALL=C ip addr ls ${REALDEVICE} | LC_ALL=C grep -q "${IPADDR}/${PREFIX}" ; then
1ba655a
	 if ! arping -q -c 2 -w 3 -D -I ${REALDEVICE} ${IPADDR} ; then
1ba655a
	    echo $"Error, some other host already uses address ${IPADDR}."
1ba655a
	    exit 1
1ba655a
	 fi
1ba655a
	 if ! ip addr add ${IPADDR}/${PREFIX} \
1ba655a
	    brd ${BROADCAST:-+} dev ${REALDEVICE} ${SCOPE} label ${DEVICE}; then
1ba655a
	    echo $"Error adding address ${IPADDR} for ${DEVICE}."
1ba655a
	 fi
1ba655a
    fi
1ba655a
    
1ba655a
    if [ -n "$SRCADDR" ]; then
1ba655a
           sysctl -w "net.ipv4.conf.${REALDEVICE}.arp_filter=1" >/dev/null 2>&1
1ba655a
    fi
1ba655a
1ba655a
    # update ARP cache of neighboring computers
1ba655a
    arping -q -A -c 1 -I ${REALDEVICE} ${IPADDR}
1ba655a
    ( sleep 2;
1ba655a
      arping -q -U -c 1 -I ${REALDEVICE} ${IPADDR} ) > /dev/null 2>&1 < /dev/null &
1ba655a
1ba655a
    # Set a default route.
1ba655a
    if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${REALDEVICE}" ]; then
1ba655a
	# set up default gateway. replace if one already exists
1ba655a
	if [ -n "${GATEWAY}" -a "`ipcalc --network ${GATEWAY} ${NETMASK} 2>/dev/null`" = "NETWORK=${NETWORK}" ]; then
1ba655a
	    ip route replace default ${METRIC:+metric $METRIC} \
1ba655a
		via ${GATEWAY} ${WINDOW:+window $WINDOW} ${SRC} \
1ba655a
		${GATEWAYDEV:+dev $GATEWAYDEV}
1ba655a
	elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
1ba655a
	    ip route replace default ${METRIC:+metric $METRIC} \
1ba655a
		${SRC} ${WINDOW:+window $WINDOW} dev ${REALDEVICE}
1ba655a
	fi
1ba655a
    fi
1ba655a
fi
1ba655a
1ba655a
# Add Zeroconf route.
1ba655a
if [ -z "${NOZEROCONF}" -a "${ISALIAS}" = "no" -a "${REALDEVICE}" != "lo" ]; then
1ba655a
    ip route replace 169.254.0.0/16 dev ${REALDEVICE} 
1ba655a
fi
1ba655a
1ba655a
# IPv6 initialisation?
1ba655a
if [ "${NETWORKING_IPV6}" = "yes" ]; then
1ba655a
    /etc/sysconfig/network-scripts/ifup-ipv6 ${CONFIG}
1ba655a
    if [[ "${DHCPV6C}"  = [Yy1]* ]] && [ -x /sbin/dhcp6c ]; then
1ba655a
	/sbin/dhcp6c ${DEVICE};
1ba655a
        dhcp6_pid=(`/bin/ps -eo 'pid,args' | /bin/grep "dhcp6c ${DEVICE}" | egrep -v grep`);
1ba655a
	echo ${dhcp6_pid[0]} > /var/run/dhcp6c_${DEVICE}.pid
1ba655a
    fi;
1ba655a
fi
1ba655a
1ba655a
exec /etc/sysconfig/network-scripts/ifup-post ${CONFIG} ${2}
1ba655a