5 kx #!/bin/bash
5 kx # /etc/rc.d/rc.inet1
5 kx # This script is used to bring up the various network interfaces.
5 kx #
5 kx # @(#)/etc/rc.d/rc.inet1 10.2 Sun Jul 24 12:45:56 PDT 2005 (pjv)
5 kx
5 kx ############################
5 kx # READ NETWORK CONFIG FILE #
5 kx ############################
5 kx
5 kx # Get the configuration information from /etc/rc.d/rc.inet1.conf:
5 kx . /etc/rc.d/rc.inet1.conf
5 kx
5 kx ###########
5 kx # LOGGING #
5 kx ###########
5 kx
5 kx # Message logging.
5 kx info_log() {
5 kx # If possible, log events in /var/log/messages:
5 kx if [ -f /var/run/syslogd.pid ] && [ -x /usr/bin/logger ]; then
5 kx /usr/bin/logger -t "rc.inet1" --id="$$" "$*"
5 kx else
5 kx printf "%s: %s\\n" "rc.inet1" "$*"
5 kx fi
5 kx }
5 kx
5 kx # Verbose logging.
5 kx debug_log() {
5 kx if [ "$DEBUG_ETH_UP" = "yes" ]; then
5 kx info_log "$*"
5 kx fi
5 kx }
5 kx
5 kx ############################
5 kx # DETERMINE INTERFACE LIST #
5 kx ############################
5 kx
5 kx # Compose a list of interfaces from /etc/rc.d/rc.inet1.conf (with a maximum
5 kx # of 6 interfaces, but you can easily enlarge the interface limit
5 kx # - send me a picture of such a box :-).
5 kx # If a value for IFNAME[n] is not set, we assume it is an eth'n' interface.
5 kx # This way, the new script is compatible with older rc.inet1.conf files.
5 kx # The IFNAME array will be used to determine which interfaces to bring up/down.
5 kx MAXNICS=${MAXNICS:-6}
5 kx i=0
5 kx while [ $i -lt $MAXNICS ];
5 kx do
5 kx IFNAME[$i]=${IFNAME[$i]:=eth${i}}
5 kx i=$((i+1))
5 kx done
5 kx debug_log "List of interfaces: ${IFNAME[*]}"
5 kx
5 kx ####################
5 kx # PRE-LOAD MODULES #
5 kx ####################
5 kx
5 kx for i in "${IFNAME[@]}"; do
5 kx # If the interface isn't in the kernel yet (but there's an alias for it in modules.conf),
5 kx # then it should be loaded first:
5 kx if [ ! -e /sys/class/net/${i%%[:.]*} ]; then # no interface yet
5 kx if /sbin/modprobe -c | grep -v "^#" | grep -w "alias ${i%%[:.]*}" | grep -vw "alias ${i%%[:.]*} off" >/dev/null; then
5 kx debug_log "/sbin/modprobe ${i%%[:.]*}"
5 kx /sbin/modprobe ${i%%[:.]*}
5 kx _DID_MODPROBE=1
5 kx fi
5 kx fi
5 kx done
5 kx # Normally the ipv6 module would be automatically loaded when the first IP is assigned to an
5 kx # interface (assuming ipv6 has not been disabled entirely), but autoconf/accept_ra need to be
5 kx # set to 0 before that happens, so try to pre-load ipv6 here.
5 kx if [ ! -e /proc/sys/net/ipv6 ]; then
5 kx debug_log "/sbin/modprobe ipv6"
5 kx /sbin/modprobe -q ipv6
5 kx _DID_MODPROBE=1
5 kx fi
5 kx # If we did any module loading in the blocks above, sleep for a couple of
5 kx # seconds to give time for everything to "take"
5 kx [ -n "${_DID_MODPROBE}" ] && sleep 2
5 kx unset _DID_MODPROBE
5 kx
5 kx ######################
5 kx # LOOPBACK FUNCTIONS #
5 kx ######################
5 kx
5 kx # Function to bring up the loopback interface. If loopback is
5 kx # already up, do nothing.
5 kx lo_up() {
5 kx if [ -e /sys/class/net/lo ]; then
5 kx if ! /sbin/ip link show dev lo | grep -wq -e "state UP" -e "state UNKNOWN" ; then
5 kx info_log "lo: configuring interface"
5 kx debug_log "/sbin/ip -4 address add 127.0.0.1/8 dev lo"
5 kx /sbin/ip -4 address add 127.0.0.1/8 dev lo
5 kx if [ -e /proc/sys/net/ipv6 ]; then
5 kx debug_log "/sbin/ip -6 address add ::1/128 dev lo"
5 kx /sbin/ip -6 address add ::1/128 dev lo
5 kx fi
5 kx debug_log "/sbin/ip link set dev lo up"
5 kx /sbin/ip link set dev lo up
5 kx debug_log "/sbin/ip route add 127.0.0.0/8 dev lo"
5 kx /sbin/ip route add 127.0.0.0/8 dev lo
5 kx fi
5 kx fi
5 kx }
5 kx
5 kx # Function to take down the loopback interface:
5 kx lo_down() {
5 kx if [ -e /sys/class/net/lo ]; then
5 kx info_log "lo: de-configuring interface"
5 kx debug_log "/sbin/ip address flush dev lo"
5 kx /sbin/ip address flush dev lo
5 kx debug_log "/sbin/ip link set dev lo down"
5 kx /sbin/ip link set dev lo down
5 kx fi
5 kx }
5 kx
5 kx #######################
5 kx # INTERFACE FUNCTIONS #
5 kx #######################
5 kx
5 kx # Function to create virtual interfaces
5 kx virtif_create() {
5 kx # argument is 'i' - the position of this interface in the VIRTIFNAME array.
5 kx # this loop goes from i=0 to i=number_of_configured_virtual_interfaces_minus_one
5 kx # which means it doesn't do anything if there are none.
5 kx for i in $(seq 0 $((${#VIRTIFNAME[@]} - 1))); do
5 kx info_log "${VIRTIFNAME[$i]}: creating virtual interface"
5 kx debug_log "/sbin/ip tuntap add dev ${VIRTIFNAME[$i]} mode ${VIRTIFTYPE[$i]} user ${VIRTIFUSER[$i]} group ${VIRTIFGROUP[$i]}"
5 kx /sbin/ip tuntap add dev ${VIRTIFNAME[$i]} mode ${VIRTIFTYPE[$i]} user ${VIRTIFUSER[$i]} group ${VIRTIFGROUP[$i]}
5 kx done
5 kx }
5 kx
5 kx # Function to destroy virtual interfaces
5 kx virtif_destroy() {
5 kx # argument is 'i' - the position of this interface in the VIRTIFNAME array.
5 kx for i in $(seq 0 $((${#VIRTIFNAME[@]} - 1))); do
5 kx info_log "${VIRTIFNAME[$i]}: destroying virtual interface"
5 kx debug_log "/sbin/ip tuntap del dev ${VIRTIFNAME[$i]} mode ${VIRTIFTYPE[$i]}"
5 kx /sbin/ip tuntap del dev ${VIRTIFNAME[$i]} mode ${VIRTIFTYPE[$i]}
5 kx done
5 kx }
5 kx
5 kx # Function to assemble a bridge interface.
5 kx br_open() {
5 kx # argument is 'i' - the position of this interface in the IFNAME array.
5 kx info_log "${IFNAME[$1]}: creating bridge"
5 kx debug_log "/sbin/ip link add name ${IFNAME[$1]} type bridge"
5 kx /sbin/ip link add name ${IFNAME[$1]} type bridge
5 kx for BRIF in ${BRNICS[$1]}; do
5 kx debug_log "/sbin/ip address flush dev $BRIF"
5 kx /sbin/ip address flush dev $BRIF
5 kx debug_log "/sbin/ip link set dev $BRIF master ${IFNAME[$1]}"
5 kx /sbin/ip link set dev $BRIF master ${IFNAME[$1]}
5 kx debug_log "/sbin/ip link set dev $BRIF up"
5 kx /sbin/ip link set dev $BRIF up
5 kx done
5 kx while read -r -d \| IFOPT; do
5 kx if [ -n "$IFOPT" ]; then
5 kx debug_log "/sbin/ip link set dev ${IFNAME[$1]} type bridge $IFOPT"
5 kx /sbin/ip link set dev ${IFNAME[$1]} type bridge $IFOPT
5 kx fi
5 kx done <<<"${IFOPTS[$1]/%|*([[:blank:]])}|" # The | on the end is required.
5 kx # Don't bring up the interface if it will be brought up later during IP configuration.
5 kx # This prevents a situation where SLAAC takes a while to apply if the interface is already up.
5 kx if [ -z "${IPADDRS[$1]}" ] && [ -z "${IP6ADDRS[$1]}" ] && [ -z "${IPADDR[$1]}" ] && [ "${USE_DHCP[$1]}" != "yes" ] && [ "${USE_DHCP6[$1]}" != "yes" ] && [ "${USE_SLAAC[$1]}" != "yes" ]; then
5 kx debug_log "/sbin/ip link set dev ${IFNAME[$1]} up"
5 kx /sbin/ip link set dev ${IFNAME[$1]} up
5 kx fi
5 kx }
5 kx
5 kx # Function to disassemble a bridge interface.
5 kx br_close() {
5 kx # argument is 'i' - the position of this interface in the IFNAME array.
5 kx info_log "${IFNAME[$1]}: destroying bridge"
5 kx debug_log "/sbin/ip link set dev ${IFNAME[$1]} down"
5 kx /sbin/ip link set dev ${IFNAME[$1]} down
5 kx for BRIF in $(ls --indicator-style=none /sys/class/net/${IFNAME[$1]}/brif/)
5 kx do
5 kx debug_log "/sbin/ip link set dev $BRIF nomaster"
5 kx /sbin/ip link set dev $BRIF nomaster
5 kx done
5 kx for BRIF in ${BRNICS[$1]}; do
5 kx debug_log "/sbin/ip link set dev $BRIF down"
5 kx /sbin/ip link set dev $BRIF down
5 kx done
5 kx debug_log "/sbin/ip link del ${IFNAME[$1]}"
5 kx /sbin/ip link del ${IFNAME[$1]}
5 kx }
5 kx
5 kx # Function to create a bond.
5 kx bond_create() {
5 kx # Argument is 'i' - the position of this interface in the IFNAME array.
5 kx info_log "${IFNAME[$1]}: creating bond"
5 kx debug_log "/sbin/ip link add name ${IFNAME[$1]} type bond"
5 kx /sbin/ip link add name ${IFNAME[$1]} type bond
5 kx debug_log "/sbin/ip link set dev ${IFNAME[$1]} type bond mode ${BONDMODE[$1]:-balance-rr}"
5 kx /sbin/ip link set dev ${IFNAME[$1]} type bond mode ${BONDMODE[$1]:-balance-rr}
5 kx for BONDIF in ${BONDNICS[$1]}; do
5 kx debug_log "/sbin/ip address flush dev $BONDIF"
5 kx /sbin/ip address flush dev $BONDIF
5 kx debug_log "/sbin/ip link set $BONDIF master ${IFNAME[$1]}"
5 kx /sbin/ip link set $BONDIF master ${IFNAME[$1]}
5 kx debug_log "/sbin/ip link set dev $BONDIF up"
5 kx /sbin/ip link set dev $BONDIF up
5 kx done
5 kx # This has to be done *after* the interface is brought up because the
5 kx # 'primary <interface>' option has to occur after the interface is active.
5 kx while read -r -d \| IFOPT; do
5 kx if [ -n "$IFOPT" ]; then
5 kx debug_log "/sbin/ip link set dev ${IFNAME[$1]} type bond $IFOPT"
5 kx /sbin/ip link set dev ${IFNAME[$1]} type bond $IFOPT
5 kx fi
5 kx done <<<"${IFOPTS[$1]/%|*([[:blank:]])}|" # The | on the end is required.
5 kx }
5 kx
5 kx # Function to destroy a bond.
5 kx bond_destroy() {
5 kx # Argument is 'i' - the position of this interface in the IFNAME array.
5 kx info_log "${IFNAME[$1]}: destroying bond"
5 kx debug_log "/sbin/ip link set dev ${IFNAME[$1]} down"
5 kx /sbin/ip link set dev ${IFNAME[$1]} down
5 kx debug_log "/sbin/ip address flush dev ${IFNAME[$1]}"
5 kx /sbin/ip address flush dev ${IFNAME[$1]}
5 kx for BONDIF in ${BONDNICS[$1]}; do
5 kx debug_log "/sbin/ip link set $BONDIF nomaster"
5 kx /sbin/ip link set $BONDIF nomaster
5 kx debug_log "/sbin/ip link set dev $BONDIF down"
5 kx /sbin/ip link set dev $BONDIF down
5 kx done
5 kx debug_log "/sbin/ip link del name ${IFNAME[$1]} type bond"
5 kx /sbin/ip link del name ${IFNAME[$1]} type bond
5 kx }
5 kx
5 kx # Function to bring up a network interface. If the interface is
5 kx # already up or does not yet exist (perhaps because the kernel driver
5 kx # is not loaded yet), do nothing.
5 kx if_up() {
5 kx # Determine position 'i' of this interface in the IFNAME array:
5 kx i=0
5 kx while [ $i -lt $MAXNICS ]; do
5 kx [ "${IFNAME[$i]}" = "${1}" ] && break
5 kx i=$((i+1))
5 kx done
5 kx # If "i" is greater or equal to "MAXNICS" at this point, it means we didn't
5 kx # find an entry in IFNAME array corresponding to "${1}", which likely means
5 kx # there are more interfaces configured than MAXNICS. Let's err on the
5 kx # side of caution and do nothing instead of possibly doing the wrong thing.
5 kx if [ $i -ge $MAXNICS ]; then
5 kx info_log "${1}: skipping - you might need to increase MAXNICS"
5 kx return
5 kx fi
5 kx info_log "${1}: configuring interface"
5 kx # If you need to set hardware addresses for the underlying interfaces in a
5 kx # bond or bridge, configure the interfaces with IPs of 0.0.0.0 and set the
5 kx # MAC address with HWADDR. Then, finally, define the bond or bridge.
5 kx # If the interface is a bond, create it.
5 kx [ -n "${BONDNICS[$i]}" ] && bond_create $i
5 kx # If the interface is a bridge, create it.
5 kx [ -n "${BRNICS[$i]}" ] && br_open $i
5 kx if [ -e /sys/class/net/${1%%[:.]*} ]; then # interface exists
5 kx if ! /sbin/ip address show scope global dev ${1} 2>/dev/null | grep -Ewq '(inet|inet6)' || \
5 kx ! /sbin/ip link show dev ${1} | grep -wq "state UP"; then # interface not up or not configured
5 kx local IF_UP=0
5 kx # Initialize any wireless parameters:
5 kx if [ -x /etc/rc.d/rc.wireless ]; then
5 kx . /etc/rc.d/rc.wireless ${1} start
5 kx fi
5 kx # Handle VLAN interfaces before trying to configure IP addresses.
5 kx if echo "${1}" | grep -Fq .; then
5 kx IFACE="${1%.*}"
5 kx VLAN="${1##*.}"
5 kx # Check if the underlying interface is already up.
5 kx if ! /sbin/ip link show dev $IFACE 2>/dev/null| grep -wq "state UP"; then
5 kx # Bring up the underlying interface.
5 kx debug_log "/sbin/ip link set dev $IFACE up"
5 kx if ! /sbin/ip link set dev $IFACE up; then
5 kx info_log "${1}: failed to bring up interface $IFACE"
5 kx return
5 kx fi
5 kx IF_UP=1
5 kx fi
5 kx # Configure the VLAN interface.
5 kx info_log "${1}: creating VLAN interface"
5 kx debug_log "/sbin/ip link add link $IFACE name ${1} type vlan id $VLAN"
5 kx if ! /sbin/ip link add link $IFACE name ${1} type vlan id $VLAN; then
5 kx info_log "${1}: failed to create VLAN interface"
5 kx ((IF_UP == 1)) && /sbin/ip link set dev $IFACE down
5 kx return
5 kx fi
5 kx while read -r -d \| IFOPT; do
5 kx if [ -n "$IFOPT" ]; then
5 kx debug_log "/sbin/ip link set dev ${1} type vlan $IFOPT"
5 kx /sbin/ip link set dev ${1} type vlan $IFOPT
5 kx fi
5 kx done <<<"${IFOPTS[$i]/%|*([[:blank:]])}|" # The | on the end is required.
5 kx elif [ -z "${BONDNICS[$i]}" ] && [ -z "${BRNICS[$i]}" ]; then
5 kx # Only apply IFOPTS for a physical interface if it's not been handled
5 kx # by a higher level interface.
5 kx while read -r -d \| IFOPT; do
5 kx if [ -n "$IFOPT" ]; then
5 kx debug_log "/sbin/ip link set dev ${1} $IFOPT"
5 kx /sbin/ip link set dev ${1} $IFOPT
5 kx fi
5 kx done <<<"${IFOPTS[$i]/%|*([[:blank:]])}|" # The | on the end is required.
5 kx fi
5 kx # Set hardware address:
5 kx if [ -n "${HWADDR[$i]}" ]; then
5 kx debug_log "/sbin/ip link set dev ${1} address ${HWADDR[$i]}"
5 kx if ! /sbin/ip link set dev ${1} address ${HWADDR[$i]} 2>/dev/null; then
5 kx info_log "${1}: failed to set hardware address"
5 kx fi
5 kx fi
5 kx if [ -e /proc/sys/net/ipv6 ]; then # ipv6 networking is available
5 kx # Disable v6 IP auto configuration before trying to bring up the interface:
5 kx debug_log "${1}: disabling IPv6 autoconf"
5 kx echo "0" >/proc/sys/net/ipv6/conf/${1}/autoconf
5 kx if [ "${USE_RA[$i]}" = "yes" ]; then
5 kx # Unconditionally accept router advertisements on this interface:
5 kx debug_log "${1}: accepting IPv6 RA"
5 kx echo "1" >/proc/sys/net/ipv6/conf/${1}/accept_ra
5 kx else
5 kx # Disable router advertisments on this interface until SLAAC is enabled:
5 kx debug_log "${1}: ignoring IPv6 RA"
5 kx echo "0" >/proc/sys/net/ipv6/conf/${1}/accept_ra
5 kx fi
5 kx fi
5 kx debug_log "/sbin/ip address flush dev ${1}"
5 kx /sbin/ip address flush dev ${1}
5 kx IF_UP=0
5 kx if [ -e /proc/sys/net/ipv6 ] && [ "${USE_DHCP6[$i]}" != "yes" ] && [ "${USE_SLAAC[$i]}" = "yes" ]; then # configure via SLAAC
5 kx info_log "${1}: enabling SLAAC"
5 kx # Enable accepting of RA packets, unless explicitly configured not to:
5 kx if [ "${USE_RA[$i]}" = "no" ]; then
5 kx debug_log "${1}: ignoring IPv6 RA"
5 kx echo "0" >/proc/sys/net/ipv6/conf/${1}/accept_ra
5 kx else
5 kx debug_log "${1}: accepting IPv6 RA"
5 kx echo "1" >/proc/sys/net/ipv6/conf/${1}/accept_ra
5 kx fi
5 kx # Set up SLAAC privacy enhancements if configured.
5 kx if [ "${SLAAC_PRIVIPGEN[$i]}" = "yes" ]; then
5 kx if [ -n "${SLAAC_SECRET[$i]}" ]; then
5 kx debug_log "${1}: seeding secret and enabling private IPv6 generation"
5 kx echo "${SLAAC_SECRET[$i]}" >/proc/sys/net/ipv6/conf/${1}/stable_secret
5 kx echo "2" >/proc/sys/net/ipv6/conf/${1}/addr_gen_mode
5 kx else
5 kx debug_log "${1}: using random secret and enabling private IPv6 generation"
5 kx echo -n >/proc/sys/net/ipv6/conf/${1}/stable_secret
5 kx echo "3" >/proc/sys/net/ipv6/conf/${1}/addr_gen_mode
5 kx fi
5 kx fi
5 kx if [ "${SLAAC_TEMPADDR[$i]}" = "yes" ]; then
5 kx debug_log "${1}: enabling SLAAC tempaddr"
5 kx echo "2" >/proc/sys/net/ipv6/conf/${1}/use_tempaddr
5 kx fi
5 kx # Enable auto configuration of interfaces:
5 kx echo "1" >/proc/sys/net/ipv6/conf/${1}/autoconf
5 kx # Bring the interface up:
5 kx debug_log "/sbin/ip link set dev ${1} up"
5 kx /sbin/ip link set dev ${1} up
5 kx echo "${1}: waiting for router announcement"
5 kx for ((j = ${SLAAC_TIMEOUT[$i]:=15} * 2; j--;)); do # by default, wait a max of 15 seconds for the interface to configure
5 kx /sbin/ip -6 address show dynamic dev ${1} 2>/dev/null | grep -Ewq 'inet6' && { IF_UP=1; break; }
5 kx sleep 0.5
5 kx done
5 kx if ((IF_UP != 1)); then
5 kx echo "${1}: timed out"
5 kx info_log "${1}: failed to auto configure after ${SLAAC_TIMEOUT[$i]} seconds"
5 kx debug_log "/sbin/ip address flush dev ${1}"
5 kx /sbin/ip address flush dev ${1}
5 kx debug_log "/sbin/ip link set dev ${1} down"
5 kx /sbin/ip link set dev ${1} down
5 kx fi
5 kx fi
5 kx # Slackware historically favours dynamic configuration over fixed IP to configure interfaces, so keep that tradition:
5 kx if [ "${USE_DHCP[$i]}" = "yes" ] || { [ -e /proc/sys/net/ipv6 ] && [ "${USE_DHCP6[$i]}" = "yes" ]; }; then # use dhcpcd
5 kx info_log "${1}: starting dhcpcd"
5 kx # Declare DHCP_OPTIONS array before adding new options to it:
5 kx local -a DHCP_OPTIONS=()
5 kx # Set DHCP_OPTIONS for this interface:
5 kx if [ -e /proc/sys/net/ipv6 ]; then
5 kx if [ "${USE_DHCP[$i]}" = "yes" ] && [ "${USE_DHCP6[$i]}" != "yes" ]; then # only try v4 dhcp
5 kx DHCP_OPTIONS+=("-4")
5 kx elif [ "${USE_DHCP[$i]}" != "yes" ] && [ "${USE_DHCP6[$i]}" = "yes" ]; then # only try v6 dhcp
5 kx DHCP_OPTIONS+=("-6")
5 kx fi
5 kx else
5 kx DHCP_OPTIONS+=("-4")
5 kx fi
5 kx [ -n "${DHCP_HOSTNAME[$i]}" ] && DHCP_OPTIONS+=("-h" "${DHCP_HOSTNAME[$i]}")
5 kx [ "${DHCP_KEEPRESOLV[$i]}" = "yes" ] && DHCP_OPTIONS+=("-C" "resolv.conf")
5 kx [ "${DHCP_KEEPNTP[$i]}" = "yes" ] && DHCP_OPTIONS+=("-C" "ntp.conf")
5 kx [ "${DHCP_KEEPGW[$i]}" = "yes" ] && DHCP_OPTIONS+=("-G")
5 kx [ "${DHCP_DEBUG[$i]}" = "yes" ] && DHCP_OPTIONS+=("-d")
5 kx # The -L option used to be hard coded into the dhcpcd command line in -current. It was added to assist ARM users
5 kx # get networking up and running. Previous versions of Slackware did not have -L hard coded - the code here keeps
5 kx # the 14.2 behaviour, but can be altered to make the use of -L default as in -current. To change the behaviour,
5 kx # alter the test below to be: [ "${DHCP_NOIPV4LL[$i]}" != "no" ].
5 kx # Note: ARM users should make use of the DHCP_NOIPV4LL[x]="yes" parameter in rc.inet1.conf - this is the correct
5 kx # way to get the behaviour they seek.
5 kx [ "${DHCP_NOIPV4LL[$i]}" = "yes" ] && DHCP_OPTIONS+=("-L")
5 kx echo "${1}: polling for DHCP server"
5 kx # 15 seconds should be a reasonable default DHCP timeout. 30 was too much.
5 kx debug_log "/sbin/dhcpcd -t ${DHCP_TIMEOUT[$i]:-15} ${DHCP_OPTIONS[*]} ${1}"
5 kx if /sbin/dhcpcd -t "${DHCP_TIMEOUT[$i]:-15}" "${DHCP_OPTIONS[@]}" ${1}; then
5 kx # Enable accepting of RA packets if explicitly told to:
5 kx if [ -e /proc/sys/net/ipv6 ] && [ "${USE_RA[$i]}" = "yes" ]; then
5 kx debug_log "${1}: unconditionally accepting IPv6 RA"
5 kx echo "1" >/proc/sys/net/ipv6/conf/${1}/accept_ra
5 kx fi
5 kx IF_UP=1
5 kx else
5 kx info_log "${1}: failed to obtain DHCP lease"
5 kx debug_log "/sbin/ip address flush dev ${1}"
5 kx /sbin/ip address flush dev ${1}
5 kx debug_log "/sbin/ip link set dev ${1} down"
5 kx /sbin/ip link set dev ${1} down
5 kx fi
5 kx fi
5 kx if [ -e /proc/sys/net/ipv6 ] && [ -n "${IP6ADDRS[$i]}" ]; then # add v6 IPs
5 kx info_log "${1}: setting IPv6 addresses"
5 kx # IPv6's Duplicate Address Detection (DAD) causes a race condition when bringing up interfaces, as
5 kx # described here: https://www.agwa.name/blog/post/beware_the_ipv6_dad_race_condition
5 kx # Disable DAD while bringing up the interface - but note that this means the loss of detection of a
5 kx # duplicate address. It's a trade off, unfortunately.
5 kx debug_log "${1}: disabling IPv6 DAD"
5 kx echo "0" >/proc/sys/net/ipv6/conf/${1}/accept_dad
5 kx for V6IP in ${IP6ADDRS[$i]}; do
5 kx IP="${V6IP%/*}"
5 kx PREFIX="${V6IP#*/}"
5 kx if [ -z "$PREFIX" ] || [ "$IP" == "$PREFIX" ]; then
5 kx info_log "${1}: no prefix length set for IP $IP - assuming 64"
5 kx PREFIX="64"
5 kx fi
5 kx debug_log "/sbin/ip -6 address add $IP/$PREFIX dev ${1}"
5 kx if /sbin/ip -6 address add $IP/$PREFIX dev ${1} && /sbin/ip link set dev ${1} up; then
5 kx # Enable accepting of RA packets if explicitly told to.
5 kx if [ "${USE_RA[$i]}" = "yes" ]; then
5 kx debug_log "${1}: unconditionally accepting IPv6 RA"
5 kx echo "1" >/proc/sys/net/ipv6/conf/${1}/accept_ra
5 kx fi
5 kx IF_UP=1
5 kx else
5 kx info_log "${1}: failed to set IP $IP"
5 kx if ((IF_UP != 1)); then # a v4 address was configured, don't flush it
5 kx debug_log "/sbin/ip address flush dev ${1}"
5 kx /sbin/ip address flush dev ${1}
5 kx debug_log "/sbin/ip link set dev ${1} down"
5 kx /sbin/ip link set dev ${1} down
5 kx fi
5 kx fi
5 kx done
5 kx # Reset accept_dad back to default now all the IPs are configured:
5 kx debug_log "${1}: resetting IPv6 DAD to default"
5 kx cat /proc/sys/net/ipv6/conf/default/accept_dad >/proc/sys/net/ipv6/conf/${1}/accept_dad
5 kx fi
5 kx if [ -n "${IPADDRS[$i]}" ] || [ -n "${IPADDR[$i]}" ]; then # add v4 IPs
5 kx info_log "${1}: setting IPv4 addresses"
5 kx # Only use IPADDR if no dynamic configuration was done.
5 kx if [ "${USE_DHCP[$i]}" == "yes" ] || [ "${USE_DHCP6[$i]}" == "yes" ] || [ "${USE_SLAAC[$i]}" == "yes" ]; then
5 kx V4IPS="${IPADDRS[$i]}"
5 kx else
5 kx V4IPS="${IPADDRS[$i]} ${IPADDR[$i]}${NETMASK[$i]:+/${NETMASK[$i]}}"
5 kx fi
5 kx for V4IP in $V4IPS; do
5 kx IP="${V4IP%/*}"
5 kx NM="${V4IP#*/}"
5 kx if [ -z "$NM" ] || [ "$IP" == "$NM" ]; then
5 kx info_log "${1}: no netmask set for IP $IP - assuming 24 (aka, 255.255.255.0)"
5 kx NM="24"
5 kx fi
5 kx debug_log "/sbin/ip -4 address add $IP/$NM broadcast + dev ${1}"
5 kx if /sbin/ip -4 address add $IP/$NM broadcast + dev ${1} && /sbin/ip link set dev ${1} up; then
5 kx IF_UP=1
5 kx else
5 kx info_log "${1}: failed to set IP $IP"
5 kx if ((IF_UP != 1)); then # if at least one address was configured, don't flush the device
5 kx debug_log "/sbin/ip address flush dev ${1}"
5 kx /sbin/ip address flush dev ${1}
5 kx debug_log "/sbin/ip link set dev ${1} down"
5 kx /sbin/ip link set dev ${1} down
5 kx fi
5 kx fi
5 kx done
5 kx fi
5 kx if ((IF_UP == 1)) && [ -n "${IPALIASES[$i]}" ]; then # Only apply IPALIASES onto an up interface
5 kx info_log "${1}: setting extra IPv4 addresses"
5 kx NUM=0
5 kx for EXTRAIP in ${IPALIASES[$i]}; do
5 kx IP="${EXTRAIP%/*}"
5 kx NM="${EXTRAIP#*/}"
5 kx if [ -z "$NM" ] || [ "$IP" == "$NM" ]; then
5 kx info_log "${1}: no netmask set for alias IP $IP - assuming 24 (aka, 255.255.255.0)"
5 kx NM="24"
5 kx fi
5 kx debug_log "/sbin/ip -4 address add $IP/$NM broadcast + dev ${1} label ${1}:$NUM"
5 kx if /sbin/ip -4 address add $IP/$NM broadcast + dev ${1} label ${1}:$NUM; then
5 kx NUM=$((NUM + 1))
5 kx else
5 kx info_log "${1}: failed to add alias IP $IP"
5 kx fi
5 kx done
5 kx fi
5 kx if ((IF_UP == 1)); then
5 kx # Force an MTU (possibly overriding that set by DHCP or RA):
5 kx if [ -n "${MTU[$i]}" ]; then
5 kx info_log "${1}: setting custom MTU"
5 kx debug_log "/sbin/ip link set dev ${1} mtu ${MTU[$i]}"
5 kx if ! /sbin/ip link set dev ${1} mtu ${MTU[$i]}; then
5 kx info_log "${1}: failed to set MTU"
5 kx fi
5 kx fi
5 kx # Set promiscuous mode on the interface:
5 kx if [ "${PROMISCUOUS[$i]}" = "yes" ]; then
5 kx info_log "${1}: setting promiscuous mode"
5 kx debug_log "/sbin/ip link set dev ${1} promisc on"
5 kx if ! /sbin/ip link set dev ${1} promisc on; then
5 kx info_log "${1}: failed to set promiscuous mode"
5 kx fi
5 kx fi
5 kx fi
5 kx else
5 kx debug_log "${1}: skipping configuration - already up"
5 kx fi
5 kx else
5 kx debug_log "${1}: skipping configuration - does not exist (yet)"
5 kx fi
5 kx }
5 kx
5 kx # Function to take down a network interface:
5 kx if_down() {
5 kx # Determine position 'i' of this interface in the IFNAME array:
5 kx i=0
5 kx while [ $i -lt $MAXNICS ]; do
5 kx [ "${IFNAME[$i]}" = "${1}" ] && break
5 kx i=$((i+1))
5 kx done
5 kx if [ $i -ge $MAXNICS ]; then
5 kx info_log "${1}: skipping - you might need to increase MAXNICS"
5 kx return
5 kx fi
5 kx info_log "${1}: de-configuring interface"
5 kx if [ -e /sys/class/net/${1} ]; then
5 kx if [ "${USE_DHCP[$i]}" = "yes" ] || [ "${USE_DHCP6[$i]}" = "yes" ]; then # take down dhcpcd
5 kx info_log "${1}: stopping dhcpcd"
5 kx # When using -k, dhcpcd requires some command line options to match those used to invoke it:
5 kx if [ "${USE_DHCP[$i]}" = "yes" ] && [ "${USE_DHCP6[$i]}" != "yes" ]; then # only v4 dhcp
5 kx DHCP_OPTIONS=( -4 )
5 kx elif [ "${USE_DHCP[$i]}" != "yes" ] && [ "${USE_DHCP6[$i]}" = "yes" ]; then # only v6 dhcp
5 kx DHCP_OPTIONS=( -6 )
5 kx fi
5 kx debug_log "/sbin/dhcpcd ${DHCP_OPTIONS[*]} -k -d ${1}"
5 kx /sbin/dhcpcd "${DHCP_OPTIONS[*]}" -k -d ${1} 2>/dev/null || info_log "${1}: failed to stop dhcpcd"
5 kx fi
5 kx # Disable v6 IP auto configuration and RA before trying to clear the IP from the interface:
5 kx if [ -e /proc/sys/net/ipv6 ]; then
5 kx debug_log "${1}: disabling IPv6 autoconf and RA"
5 kx echo "0" >/proc/sys/net/ipv6/conf/${1}/autoconf
5 kx echo "0" >/proc/sys/net/ipv6/conf/${1}/accept_ra
5 kx fi
5 kx sleep 0.5 # allow time for DHCP/RA to unconfigure the interface
5 kx # Flush any remaining IPs:
5 kx debug_log "/sbin/ip address flush dev ${1}"
5 kx /sbin/ip address flush dev ${1}
5 kx # Bring the interface down:
5 kx debug_log "/sbin/ip link set dev ${1} down"
5 kx /sbin/ip link set dev ${1} down
5 kx # Reset everything back to defaults:
5 kx if [ -e /proc/sys/net/ipv6 ]; then
5 kx debug_log "${1}: resetting IPv6 configuration to defaults"
5 kx cat /proc/sys/net/ipv6/conf/default/autoconf >/proc/sys/net/ipv6/conf/${1}/autoconf
5 kx cat /proc/sys/net/ipv6/conf/default/accept_ra >/proc/sys/net/ipv6/conf/${1}/accept_ra
5 kx cat /proc/sys/net/ipv6/conf/default/use_tempaddr >/proc/sys/net/ipv6/conf/${1}/use_tempaddr
5 kx cat /proc/sys/net/ipv6/conf/default/addr_gen_mode >/proc/sys/net/ipv6/conf/${1}/addr_gen_mode
5 kx echo -n >/proc/sys/net/ipv6/conf/${1}/stable_secret
5 kx fi
5 kx # If the interface is a bridge, then destroy it now:
5 kx [ -n "${BRNICS[$i]}" ] && br_close $i
5 kx # If the interface is a bond, then destroy it now.
5 kx [ -n "${BONDNICS[$i]}" ] && bond_destroy $i
5 kx # Take down VLAN interface, if configured.
5 kx if echo "${1}" | grep -Fq .; then
5 kx info_log "${1}: destroying VLAN interface"
5 kx debug_log "/sbin/ip link set dev ${1} down"
5 kx /sbin/ip link set dev ${1} down
5 kx debug_log "/sbin/ip link delete ${1}"
5 kx /sbin/ip link delete ${1}
5 kx if ! /sbin/ip address show scope global dev ${1%.*} 2>/dev/null | grep -Ewq '(inet|inet6)'; then
5 kx debug_log "/sbin/ip link set dev ${1%.*} down"
5 kx /sbin/ip link set dev ${1%.*} down
5 kx fi
5 kx fi
5 kx # Kill wireless daemons if any:
5 kx if [ -x /etc/rc.d/rc.wireless ]; then
5 kx . /etc/rc.d/rc.wireless ${1} stop
5 kx fi
5 kx fi
5 kx }
5 kx
5 kx #####################
5 kx # GATEWAY FUNCTIONS #
5 kx #####################
5 kx
5 kx # Function to bring up the gateway if there is not yet a default route:
5 kx gateway_up() {
5 kx info_log "Configuring gateways"
5 kx # Bring up the IPv4 gateway:
5 kx if [ -n "$GATEWAY" ]; then
5 kx if ! /sbin/ip -4 route show | grep -wq default; then
5 kx debug_log "/sbin/ip -4 route add default via ${GATEWAY}"
5 kx /sbin/ip -4 route add default via ${GATEWAY}
5 kx fi
5 kx fi
5 kx # Bring up the IPv6 gateway:
5 kx if [ -n "$GATEWAY6" ]; then
5 kx if ! /sbin/ip -6 route show | grep -wq default; then
5 kx debug_log "/sbin/ip -6 route add default via ${GATEWAY6}"
5 kx /sbin/ip -6 route add default via ${GATEWAY6}
5 kx fi
5 kx fi
5 kx }
5 kx
5 kx # Function to take down an existing default gateway:
5 kx gateway_down() {
5 kx info_log "De-configuring gateways"
5 kx if /sbin/ip -4 route show | grep -wq default ; then
5 kx debug_log "/sbin/ip -4 route del default"
5 kx /sbin/ip -4 route del default
5 kx fi
5 kx if /sbin/ip -6 route show | grep -wq default ; then
5 kx debug_log "/sbin/ip -6 route del default"
5 kx /sbin/ip -6 route del default
5 kx fi
5 kx }
5 kx
5 kx # Function to start the network:
5 kx start() {
5 kx echo "Starting the network interfaces..."
5 kx lo_up
5 kx virtif_create
5 kx for i in "${IFNAME[@]}" ; do
5 kx if_up $i
5 kx done
5 kx gateway_up
5 kx }
5 kx
5 kx # Function to stop the network:
5 kx stop() {
5 kx echo "Stopping the network interfaces..."
5 kx gateway_down
5 kx for (( i = MAXNICS - 1; i >= 0; i-- )); do
5 kx if_down ${IFNAME[$i]}
5 kx done
5 kx virtif_destroy
5 kx lo_down
5 kx }
5 kx
5 kx
5 kx ############
5 kx ### MAIN ###
5 kx ############
5 kx
5 kx # extglob is required for some functionallity.
5 kx shopt -s extglob
5 kx
5 kx case "${1}" in
5 kx start|up) # "start" (or "up") brings up all configured interfaces:
5 kx start
5 kx ;;
5 kx stop|down) # "stop" (or "down") takes down all configured interfaces:
5 kx stop
5 kx ;;
5 kx restart) # "restart" restarts the network:
5 kx stop
5 kx start
5 kx ;;
5 kx lo_start|lo_up) # Start the loopback interface:
5 kx lo_up
5 kx ;;
5 kx lo_stop|lo_down) # Stop the loopback interface:
5 kx lo_down
5 kx ;;
5 kx *_start|*_up) # Example: "eth1_start" (or "eth1_up") will start the specified interface 'eth1'
5 kx INTERFACE=$(echo ${1} | /bin/cut -d '_' -f 1)
5 kx if_up $INTERFACE
5 kx gateway_up
5 kx ;;
5 kx *_stop|*_down) # Example: "eth0_stop" (or "eth0_down") will stop the specified interface 'eth0'
5 kx INTERFACE=$(echo ${1} | /bin/cut -d '_' -f 1)
5 kx if_down $INTERFACE
5 kx ;;
5 kx *_restart) # Example: "wlan0_restart" will take 'wlan0' down and up again
5 kx INTERFACE=$(echo ${1} | /bin/cut -d '_' -f 1)
5 kx if_down $INTERFACE
5 kx sleep 1
5 kx if_up $INTERFACE
5 kx gateway_up
5 kx ;;
5 kx *) # The default is to bring up all configured interfaces:
5 kx start
5 kx esac
5 kx
5 kx # End of /etc/rc.d/rc.inet1