#!/bin/initsh # /etc/rc.d/rc.inet1 # This script is used to bring up the various network interfaces. # # Modified by: Gilbert Ashley to use the 'dash' or 'ash' shell instead of bash (as /bin/sh) action=${action:-$1} ############################ # READ NETWORK CONFIG FILE # ############################ # Get the configuration information from /etc/rc.d/rc.inet1.conf: . /etc/rc.d/rc.inet1.conf ########### # LOGGING # ########### # If possible, log events in /var/log/messages: if [ -f /var/run/syslogd.pid ] && [ -x /usr/bin/logger ]; then LOGGER=/usr/bin/logger else # output to stdout/stderr: LOGGER=/bin/cat fi ############################ # DETERMINE INTERFACE LIST # ############################ if [ "yes" = "$DEBUG_ETH_UP" ] ; then echo "$0: List of interfaces: '$INTERFACES'" | $LOGGER fi ###################### # LOOPBACK FUNCTIONS # ###################### # Function to bring up the loopback interface. If loopback is # already up, do nothing. lo_up() { if /bin/grep "lo:" /proc/net/dev 1> /dev/null ; then if ! /sbin/ifconfig | grep "^lo" 1> /dev/null ; then echo "$0: /sbin/ifconfig lo 127.0.0.1" | $LOGGER /sbin/ifconfig lo 127.0.0.1 echo "$0: /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo" | $LOGGER /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo fi fi } # Function to take down the loopback interface: lo_down() { if /bin/grep "lo:" /proc/net/dev 1> /dev/null ; then echo "$0: /sbin/ifconfig lo down" | $LOGGER /sbin/ifconfig lo down fi } ####################### # INTERFACE FUNCTIONS # ####################### # Function to bring up a network interface. If the interface is # already up or does not yet exist (perhaps because the kernel driver # is not loaded yet), do nothing. if_up() { IFNAME="$1" # If the interface isn't in the kernel yet (but there's an alias for it in # modules.conf), then it should be loaded first: if ! /bin/grep -q "${IFNAME%%:*}:" /proc/net/dev ; then # no interface yet if /sbin/modprobe -c | /bin/grep -v "^#" | /bin/grep -w "alias $IFNAME" | /bin/grep -vwq "alias $IFNAME off" ; then echo "$1: /sbin/modprobe $IFNAME" | $LOGGER /sbin/modprobe "$IFNAME" fi fi if grep -q "${IFNAME%%:*}:" /proc/net/dev ; then # no interface yet eval "local HWADDR=\"\$HWADDR_$IFNAME\"" eval "local MTU=\"\$MTU_$IFNAME\"" eval "local USE_DHCP=\"\$USE_DHCP_$IFNAME\"" if ! /sbin/ifconfig | grep -q -w "$IFNAME" || ! /sbin/ifconfig "$IFNAME" | /bin/grep -q "inet addr" ; then # interface not up or not configured if [ -n "$HWADDR" ]; then # Set hardware address _before_ the interface goes up: echo "$0: /sbin/ifconfig $IFNAME hw ether $HWADDR" | $LOGGER /sbin/ifconfig "$IFNAME" hw ether "$HWADDR" fi if [ -n "$MTU" ]; then # Set MTU to something else than 1500 echo "$0: /sbin/ifconfig "$IFNAME" mtu $MTU" | $LOGGER /sbin/ifconfig "$IFNAME" mtu "$MTU" fi if /bin/grep -q "${1%%:*}:" /proc/net/wireless ; then eval "local WLAN_CHANNEL=\"\$WLAN_CHANNEL_$IFNAME\"" eval "local WLAN_ESSID=\"\$WLAN_ESSID_$IFNAME\"" eval "local WLAN_FREQ=\"\$WLAN_FREQ_$IFNAME\"" eval "local WLAN_FRAG=\"\$WLAN_FRAG_$IFNAME\"" eval "local WLAN_IWCONFIG=\"\$WLAN_IWCONFIG_$IFNAME\"" eval "local WLAN_IWPRIV=\"\$WLAN_IWPRIV_$IFNAME\"" eval "local WLAN_IWSPY=\"\$WLAN_IWSPY_$IFNAME\"" eval "local WLAN_KEY=\"\$WLAN_KEY_$IFNAME\"" eval "local WLAN_MODE=\"\$WLAN_MODE_$IFNAME\"" eval "local WLAN_NICKNAME=\"\$WLAN_NICKNAME_$IFNAME\"" eval "local WLAN_NWID=\"\$WLAN_NWID_$IFNAME\"" eval "local WLAN_RATE=\"\$WLAN_RATE_$IFNAME\"" eval "local WLAN_RTS=\"\$WLAN_RTS_$IFNAME\"" eval "local WLAN_SENS=\"\$WLAN_SENS_$IFNAME\"" eval "local WLAN_WPA=\"\$WLAN_WPA_$IFNAME\"" eval "local WLAN_WPADRIVER=\"\$WLAN_WPADRIVER_$IFNAME\"" eval "local WLAN_WPACONF=\"\$WLAN_WPACONF_$IFNAME\"" eval "local WLAN_WPAWAIT=\"\$WLAN_WPAWAIT_$IFNAME\"" : "${WLAN_WPADRIVER:="wext"}" : "${WLAN_WPACONF:="/etc/wpa_supplicant.conf"}" : "${WLAN_WPAWAIT:="10"}" if [ -n "$MODE" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" mode \"$WLAN_MODE\"" | $LOGGER if ! /sbin/iwconfig "$IFNAME" mode "$WLAN_MODE" 2> /dev/null ; then /sbin/ifconfig "$IFNAME" down /sbin/iwconfig "$IFNAME" mode "$WLAN_MODE" /sbin/ifconfig "$IFNAME" up sleep 3 fi fi if [ -z "$NICKNAME" ]; then WLAN_NICKNAME="`/bin/hostname`" fi if [ -n "$WLAN_ESSID" ] || [ -n "$MODE" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" nick \"$WLAN_NICKNAME\"" | $LOGGER /sbin/iwconfig "$IFNAME" nick "$WLAN_NICKNAME" fi if [ -n "$WLAN_NWID" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" nwid \"$WLAN_NWID\"" | $LOGGER /sbin/iwconfig "$IFNAME" nwid "$WLAN_NWID" fi if [ -n "$WLAN_FREQ" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" freq \"$WLAN_FREQ\"" | $LOGGER /sbin/iwconfig "$IFNAME" freq "$WLAN_FREQ" elif [ -n "$WLAN_CHANNEL" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" channel \"$WLAN_CHANNEL\"" | $LOGGER /sbin/iwconfig "$IFNAME" channel "$WLAN_CHANNEL" fi if [ -n "$KEY" ] && [ -z "$WPA" ]; then if [ "$KEY" = "off" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" key open" | $LOGGER /sbin/iwconfig "$IFNAME" key open echo "$0: /sbin/iwconfig \"$IFNAME\" key off" | $LOGGER /sbin/iwconfig "$IFNAME" key off else echo "$0: /sbin/iwconfig \"$IFNAME\" key ************" | $LOGGER /sbin/iwconfig "$IFNAME" key "$KEY" case "$KEY" in *open* | *restricted*) echo "$0: /sbin/iwconfig \"$IFNAME\" key restricted" | $LOGGER /sbin/iwconfig "$IFNAME" key restricted ;; esac fi fi if [ -n "$WLAN_SENS" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" sens \"$WLAN_SENS\"" | $LOGGER /sbin/iwconfig "$IFNAME" sens "$WLAN_SENS" fi if [ -n "$WLAN_RATE" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" rate \"$WLAN_RATE\"" | $LOGGER /sbin/iwconfig "$IFNAME" rate "$WLAN_RATE" fi if [ -n "$WLAN_RTS" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" rts \"$WLAN_RTS\"" | $LOGGER /sbin/iwconfig "$IFNAME" rts "$WLAN_RTS" fi if [ -n "$WLAN_FRAG" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" frag \"$WLAN_FRAG\"" | $LOGGER /sbin/iwconfig "$IFNAME" frag "$WLAN_FRAG" fi if [ -n "$WLAN_IWCONFIG" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" $WLAN_IWCONFIG" | $LOGGER /sbin/iwconfig "$IFNAME" $WLAN_IWCONFIG fi if [ -n "$WLAN_IWSPY" ]; then echo "$0: /sbin/iwspy \"$IFNAME\" $WLAN_IWSPY" | $LOGGER /sbin/iwspy "$IFNAME" $WLAN_IWSPY fi # For RaLink cards, the SSID must be set right before configuring # WPAPSK/TKIP parameters using iwpriv commands in order to generate # the wpapsk password. This should not hurt other cards: if [ -n "$WLAN_ESSID" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" essid \"$WLAN_ESSID\"" | $LOGGER /sbin/iwconfig "$IFNAME" essid "$WLAN_ESSID" fi # The iwpriv can set one private IOCTL at the time, so if the # $IWPRIV variable contains multiple pipe ('|') separated settings, # we split them here: WARNING: if your iwpriv commands contain a # WEP/WPA key, these can be logged in /var/log/messages! if [ -n "$WLAN_IWPRIV" ]; then ( IFS="|" for iwi in $WLAN_IWPRIV ; do if [ -n "$iwi" ]; then echo "$0: /sbin/iwpriv \"$IFNAME\" $iwi" | $LOGGER /sbin/iwpriv "$IFNAME" $iwi fi done ) fi ################## # WPA_SUPPLICANT # ################## # Support for WPA (wireless protected access) is provided by # wpa_supplicant for those drivers that support it (and it looks # like wpa_supplicant is the future for WPA support in Linux # anyway) if [ "$WLAN_WPA" = "wpa_supplicant" ] || [ "$WLAN_WPA" = "wpaxsupplicant" ] && [ -x /usr/sbin/wpa_supplicant ]; then /sbin/ifconfig "$IFNAME" down WPA_OPTIONS="" [ -n "$WLAN_WPADRIVER" ] && WPA_OPTIONS="-D$WLAN_WPADRIVER" [ -n "$WLAN_WPA" = "wpaxsupplicant" ] && WPA_OPTIONS="$WPA_OPTIONS -e" WPAPID="`pgrep -f "wpa_supplicant.*$1"`" if [ -n "$WPAPID" ]; then echo "$0: wpa_supplicant found running already" | $LOGGER else echo "$0: wpa_supplicant -B -c \"$WLAN_WPACONF\" $WPA_OPTIONS -i \"$IFNAME\"" | $LOGGER /usr/sbin/wpa_supplicant -B -c "$WLAN_WPACONF" $WPA_OPTIONS -i "$IFNAME" fi wi=0 while [ "$wi" -lt "$WLAN_WPAWAIT" ]; do wi=$(($wi+1)) /bin/sleep 1 if /bin/grep -q "^ctrl_interface=" "$WLAN_WPACONF" ; then if (LANG=C LC_ALL=C /usr/sbin/wpa_cli -i "$IFNAME" status | /bin/grep -q "^wpa_state=COMPLETED") ; then break fi else if (LANG=C LC_ALL=C /sbin/iwconfig "$IFNAME" | /bin/grep -Eq "Encryption key:....-") ; then break fi fi done if [ "$wi" -eq "$WPAWAIT" ]; then echo "WPA authentication did not complete, try running '$0 ${IFNAME}_start' in a few seconds." | $LOGGER fi /sbin/ifconfig "$IFNAME" up /bin/sleep 3 else # ESSID need to be last: most devices re-perform the # scanning/discovery when this is set, and things like # encryption keys had better be defined if we want to discover # the right set of APs/nodes. if [ -n "$WLAN_ESSID" ]; then echo "$0: /sbin/iwconfig \"$IFNAME\" essid \"$WLAN_ESSID\"" | $LOGGER /sbin/iwconfig "$IFNAME" essid "$WLAN_ESSID" fi fi fi if [ "$USE_DHCP" = "yes" ]; then # use DHCP to bring interface up eval "local DHCP_HOSTNAME=\"\$DHCP_HOSTNAME_$IFNAME\"" eval "local DHCP_KEEPRESOLV=\"\$DHCP_KEEPRESOLV_$IFNAME\"" eval "local DHCP_KEEPNTP=\"\$DHCP_KEEPNTP_$IFNAME\"" eval "local DHCP_KEEPGW=\"\$DHCP_KEEPGW_$IFNAME\"" eval "local DHCP_DEBUG=\"\$DHCP_DEBUG_$IFNAME\"" eval "local DHCP_NOIPV4LL=\"\$DHCP_NOIPV4LL_$IFNAME\"" eval "local DHCP_IPADDR=\"\$DHCP_IPADDR_$IFNAME\"" eval "local DHCP_TIMEOUT=\"\$DHCP_TIMEOUT_$IFNAME\"" [ -n "$DHCP_HOSTNAME" ] && DHCP_OPTIONS="-h \"$DHCP_HOSTNAME\"" [ -n "$DHCP_KEEPRESOLV" = "yes" ] && DHCP_OPTIONS="$DHCP_OPTIONS -R" [ -n "$DHCP_KEEPNTP" = "yes" ] && DHCP_OPTIONS="$DHCP_OPTIONS -N" [ -n "$DHCP_KEEPGW" = "yes" ] && DHCP_OPTIONS="$DHCP_OPTIONS -G" [ -n "$DHCP_DEBUG" = "yes" ] && DHCP_OPTIONS="$DHCP_OPTIONS -d" [ -n "$DHCP_NOIPV4LL" = "yes" ] && DHCP_OPTIONS="$DHCP_OPTIONS -L" [ -n "$DHCP_IPADDR" && ] DHCP_OPTIONS="$DHCP_OPTIONS -s \"$DHCP_IPADDR\")" # If you set a timeout, you get one, even if the kernel doesn't think # that your device is connected, in case /sys isn't right (which it # usually isn't except right after the device is loaded, when it # usually is): #### (start commented out) # This is deactivated for now since the kernel has been returning # incorrect results concerning whether the interface carrier is # detected. #if [ -z "$DHCP_TIMEOUT" ]; then # /sbin/ifconfig "$IFNAME" up && sleep 1 # read CONNSTATUS < "/sys/class/net/$IFNAME/carrier" # /sbin/ifconfig "$IFNAME" down # if [ "$CONNSTATUS" = "0" ]; then # # The kernel has just told us the cable isn't even plugged in, # # but we will give any DHCP server a short chance to reply # # anyway: # echo "No carrier detected on $IFNAME. Reducing DHCP timeout to 10 seconds." # DHCP_TIMEOUT=10 # fi #fi #### (end commented out) # 10 seconds should be a reasonable default DHCP timeout. 30 was too # much. if [ -x /sbin/ethwireck ]; then if [ /sbin/ethwireck ${IFNAME} ]; then echo "$0: /sbin/dhcpcd -t ${DHCP_TIMEOUT:-10} $DHCP_OPTIONS \"$IFNAME\"" | $LOGGER /sbin/dhcpcd -t ${DHCP_TIMEOUT:-10} ${DHCP_OPTIONS} "$IFNAME" else echo "No active connection to ${IFNAME}, skipping DHCP configuration..." fi else echo "Polling for DHCP server on interface $IFNAME:" echo "$0: /sbin/dhcpcd -t ${DHCP_TIMEOUT:-10} $DHCP_OPTIONS \"$IFNAME\"" | $LOGGER /sbin/dhcpcd -t ${DHCP_TIMEOUT:-10} ${DHCP_OPTIONS} "$IFNAME" fi else # bring up interface using a static IP address eval "local IPADDR=\"\$IPADDR_$IFNAME\"" eval "local NETMASK=\"\$NETMASK_$IFNAME\"" eval "local BROADCAST=\"\$BROADCAST_$IFNAME\"" eval "local NETWORK=\"\$NETMASK_$IFNAME\"" if [ -n "$IPADDR" ]; then # skip unconfigured interfaces # Determine broadcast address from the IP address and netmask: IPMASK_OUTPUT="`/bin/ipmask "$NETMASK" "$IPADDR"`" BROADCAST="${IPMASK_OUTPUT%% *}" NETWORK="${IPMASK_OUTPUT#* }" # Set up the network card: echo "$0: /sbin/ifconfig \"$IFNAME\" \"$IPADDR\" broadcast \"$BROADCAST\" netmask \"$NETMASK\"" | $LOGGER /sbin/ifconfig "$IFNAME" "$IPADDR" broadcast "$BROADCAST" netmask "$NETMASK" else if [ "$DEBUG_ETH_UP" = "yes" ]; then echo "$0: $IFNAME interface is not configured in /etc/rc.d/rc.inet1.conf" | $LOGGER fi fi fi else [ "$DEBUG_ETH_UP" = "yes" ] && echo "$0: $IFNAME is already up, skipping" | $LOGGER fi else if [ "$DEBUG_ETH_UP" = "yes" ]; then echo "$0: $IFNAME interface does not exist (yet)" | $LOGGER fi fi } # Function to take down a network interface: if_down() { if /bin/grep -q "${1%%:*}:" /proc/net/dev ; then if [ "`eval "echo \$USE_DHCP_$1"}`" = "yes" ]; then echo "$0: /sbin/dhcpcd -k -d ${1}" | $LOGGER /sbin/dhcpcd -k -d "$1" 2> /dev/null || /sbin/ifconfig "$1" down sleep 1 else echo "$0: /sbin/ifconfig $1 down" | $LOGGER /sbin/ifconfig "$1" down fi if /bin/grep -q "${1%%:*}:" /proc/net/wireless ; then WPAPID="`pgrep -f "wpa_supplicant.*$1"`" [ -n "$WPAPID" ] && kill "$WPAPID" fi fi } ##################### # GATEWAY FUNCTIONS # ##################### # Function to bring up the gateway if there is not yet a default route: gateway_up() { if ! /sbin/route -n | /bin/grep -q "^0.0.0.0" ; then if [ -n "$GATEWAY" ] ; then echo "$0: /sbin/route add default gw ${GATEWAY} metric 1" | $LOGGER /sbin/route add default gw ${GATEWAY} metric 1 2>&1 | $LOGGER fi fi } # Function to take down an existing default gateway: gateway_down() { if /sbin/route -n | /bin/grep -q "^0.0.0.0" ; then echo "$0: /sbin/route del default" | $LOGGER /sbin/route del default fi } # Function to start the network: start() { lo_up for IFNAME in $INTERFACES ; do if_up "$IFNAME" done gateway_up } # Function to stop the network: stop() { gateway_down for IFNAME in $INTERFACES ; do if_down "$IFNAME" done lo_down } ############ ### MAIN ### ############ case "$action" in 'start' | 'up') # "start" brings up all configured interfaces: start ;; 'stop' | 'down') # "stop" takes down all configured interfaces: stop ;; 'restart') # "restart" restarts the network: stop start ;; *_start | *_up) # Example: "eth1_start" will start the specified interface 'eth1' if_up "${1%_*}" gateway_up ;; *_stop | *_down) # Example: "eth0_stop" will stop the specified interface 'eth0' if_down "${1%_*}" ;; *_restart) # Example: "wlan0_restart" will take 'wlan0' down and up again if_down "${1%_*}" /bin/sleep 1 if_up "${1%_*}" gateway_up ;; *) # The default is to bring up all configured interfaces: start ;; esac # End of /etc/rc.d/rc.inet1