#!/sbin/runscript # Copyright 1999-2003 Gentoo Technologies, Inc. # Distributed under the terms of the GNU General Public License v2 # $Header: /opt/cvsroot/rc-scripts/net.eth0,v 1.2 2003/08/14 11:49:24 ms Exp $ # # Modified by Michael Sliwak #NB: Config is in /etc/conf.d/net # For pcmcia users. note that pcmcia must be added to the same # runlevel as the net.* script that needs it. depend() { use hotplug pcmcia } checkconfig() { if [ -z "${iface_IFACE}" ] then eerror "Please make sure that /etc/conf.d/net has \$iface_$IFACE set" return 1 fi } setup_env() { # No reason to check these multiple times in the file iface_IFACE="$(eval echo \$\{iface_${IFACE}\})" dhcpcd_IFACE="$(eval echo \$\{dhcpcd_${IFACE}\})" inet6_IFACE="$(eval echo \$\{inet6_${IFACE}\})" alias_IFACE="$(eval echo \$\{alias_${IFACE}\})" status_IFACE="$(ifconfig | gawk -v IFACE="${IFACE}" '/Link/ { if ($1 == IFACE) print "up" }')" } start() { local retval=0 setup_env checkconfig || return 1 ebegin "Bringing ${IFACE} up" if [ "${iface_IFACE}" != "dhcp" ] then /sbin/ifconfig ${IFACE} ${iface_IFACE} >/dev/null || { retval=$? eend ${retval} "Failed to bring ${IFACE} up" return ${retval} } # ifconfig do not always return failure .. /sbin/ifconfig ${IFACE} &> /dev/null || { retval=$? eend ${retval} "Failed to bring ${IFACE} up" return ${retval} } else # Check that eth0 was not brough up by the kernel ... if [ "${status_IFACE}" != "up" ] then if [ -f /sbin/mii-diag ] then # Check if cable is plugged into the interface /sbin/ifconfig ${IFACE} up /sbin/mii-diag -s ${IFACE} >/dev/null || { retval=$? /sbin/ifconfig ${IFACE} down einfo "There is no cable plugged into ${IFACE}" einfo "Due to faster startup dhcpcd has not been started" einfo "Plug in the cable and start manually ;-)" eend ${retval} "DHCP-Client not started on ${IFACE}" return ${retval} } /sbin/ifconfig ${IFACE} down # Starting DHCP-Client /sbin/dhcpcd ${dhcpcd_IFACE} -t 10 ${IFACE} >/dev/null || { retval=$? eend ${retval} "Failed to bring ${IFACE} up" return ${retval} } else retval=$? einfo "mii-diag is not installed on the system" einfo "However it is required to run this script" einfo "Please install mii-diag" eend ${retval} "run \"emerge mii-diag\"" return ${retval} fi fi fi eend 0 if [ -n "${alias_IFACE}" ] then local x="" local num=0 local aliasbcast="" local aliasnmask="" ebegin " Adding aliases" for x in ${alias_IFACE} do aliasbcast="$(eval echo \$\{broadcast_${IFACE}\} \| awk \'\{ print \$$((num + 1)) \}\')" if [ -n "${aliasbcast}" ] then aliasbcast="broadcast ${aliasbcast}" fi aliasnmask="$(eval echo \$\{netmask_${IFACE}\} \| awk \'\{ print \$$((num + 1)) \}\')" if [ -n "${aliasnmask}" ] then aliasnmask="netmask ${aliasnmask}" fi ebegin " ${IFACE}:${num}" /sbin/ifconfig ${IFACE}:${num} ${x} \ ${aliasbcast} ${aliasnmask} >/dev/null num=$((num + 1)) eend 0 done save_options "alias" "${alias_IFACE}" fi if [ -n "${inet6_IFACE}" ] then local x="" ebegin " Adding inet6 addresses" for x in ${inet6_IFACE} do ebegin " ${IFACE} inet6 add ${x}" /sbin/ifconfig ${IFACE} inet6 add ${x} >/dev/null eend 0 done save_options "inet6" "${inet6_IFACE}" fi if [ -n "${gateway}" ] && [ "${gateway%/*}" = "${IFACE}" ] then ebegin " Setting default gateway" # First delete any existing routes if it was setup by kernel .. /sbin/route del default dev ${gateway%/*} &>/dev/null /sbin/route add default gw ${gateway#*/} dev ${gateway%/*} \ netmask 0.0.0.0 metric 1 >/dev/null || { local error=$? ifconfig ${IFACE} down &>/dev/null eend ${error} "Failed to bring ${IFACE} up" stop return ${error} } eend 0 fi # Enabling rp_filter causes wacky packets to be auto-dropped by # the kernel if [ -e /proc/sys/net/ipv4/conf/${IFACE}/rp_filter ] then echo 1 > /proc/sys/net/ipv4/conf/${IFACE}/rp_filter fi } stop() { local myalias="$(get_options alias)" local myinet6="$(get_options inet6)" setup_env ebegin "Bringing ${IFACE} down" # Also down the inet6 interfaces if [ -n "${myinet6}" ] then local x="" for x in ${myinet6} do /sbin/ifconfig ${IFACE} inet6 del ${x} >/dev/null done fi # Do some cleanup in case the amount of aliases change if [ -n "${myalias}" ] then local x="" local num=0 for x in ${myalias} do /sbin/ifconfig ${IFACE}:${num} down >/dev/null num=$((num + 1)) done fi if [ "${iface_IFACE}" = "dhcp" ] then local count=0 while /sbin/dhcpcd -k ${IFACE} &>/dev/null && [ "${count}" -lt 9 ] do # Give dhcpcd time to properly shutdown sleep 1 count=$((count + 1)) done if [ "${count}" -ge 9 ] then eerror "Timed out trying to stop dhcpcd" fi else /sbin/ifconfig ${IFACE} down >/dev/null fi eend 0 } # vim:ts=4