# /etc/conf.d/net: Global config file for net.* rc-scripts
# $Header: /var/cvsroot/gentoo-src/rc-scripts/etc/conf.d/net,v 1.10 2004/06/28 19:44:46 agriffis Exp $

##############################################################################
#
#                      READ THIS COMMENT FIRST
#
# PLEASE PICK ONE of the configuration methods listed below.  If you
# don't know which to pick, use the top one, NEW-STYLE IFCONFIG.
# The other two possible options are OLD-STYLE IFACE and
# IPROUTE2-STYLE.
#
# If you have multiple sections (for example IFCONFIG and IPROUTE2)
# the network scripts will attempt to make reasonable assumptions
# about what to use, but there is no guarantee on the results, so
# PLEASE just use one set of configuration variables.
#
# At the bottom there is an ADVANCED CONFIGURATION section with some
# additional configuration possibilities
#
##############################################################################


##############################################################################
#   NEW-STYLE IFCONFIG examples
#   ---------------------------
#   This uses /sbin/ifconfig and /sbin/route to set the network up.
#   It is suitable for most home users.
##############################################################################

# To use DHCP on eth0, simply uncomment the following line:
#ifconfig_eth0=( "dhcp" )

# For passing custom options to dhcpcd use something like the following.  This
# example reduces the timeout for retrieving an address from 60 seconds (the
# default) to 10 seconds.  Note that this might be too short for some servers,
# so the default is encouraged unless you know what you're doing.
#dhcpcd_eth0="-t 10"

# For a static configuration, use something like this.  This sets the primary
# eth0 address to 192.168.0.2 and adds two aliases, eth0:1 and eth0:2
#ifconfig_eth0=(
#	"192.168.0.2 broadcast 192.168.0.255 netmask 255.255.255.0"
#	"192.168.0.3 broadcast 192.168.0.255 netmask 255.255.255.0"
#	"192.168.0.4 broadcast 192.168.0.255 netmask 255.255.255.0"
#)

# This is just like setting the gateway config option in the old-style iface
# config, but allows one to add custom routes.  This should not be needed if you
# are obtaining an address via DHCP since the default route should be set
# automatically.
#routes_eth0=(
#	"default gw 192.168.0.1"
#)

# VLAN support - emerge net-misc/vconfig 
# Please ensure your VLAN IDs are NOT zero-padded
#vlans_eth0="1 2"
#ifconfig_eth0_1=( "dhcp" )
#ifconfig_eth0_2=( "172.16.2.1 broadcast 172.16.3.255 netmask 255.255.254.0" )


##############################################################################
#   OLD-STYLE IFACE examples (deprecated)
#   -------------------------------------
#   This uses /sbin/ifconfig and /sbin/route to set the network up,
#   but is not as flexible as the IFCONFIG method above or the
#   IPROUTE2 method below.
##############################################################################

# To use DHCP on eth0, simply uncomment the following line:
#iface_eth0="dhcp"

# For passing custom options to dhcpcd use something like the following.  This
# example reduces the timeout for retrieving an address from 60 seconds (the
# default) to 10 seconds.  Note that this might be too short for some servers,
# so the default is encouraged unless you know what you're doing.
#dhcpcd_eth0="-t 10"

# To set a static IP address use a line similar to this
#iface_eth0="192.168.0.2 broadcast 192.168.0.255 netmask 255.255.255.0"

# For setting the default gateway.  You should not need to do this if you use
# DHCP since the default gateway should be set automatically.
#gateway="eth0/192.168.0.1"

# For adding aliases to a interface, specify them space-separated in the
# following string
#alias_eth0="192.168.0.3 192.168.0.4"

# To add a custom netmask/broadcast address to created aliases, specify them in
# the same order as the alias_eth0 string.  If you find yourself doing this, you
# might consider switching to the new-style
#broadcast_eth0="192.168.0.255 192.168.0.255"
#netmask_eth0="255.255.255.0 255.255.255.0"

# VLAN support - emerge net-misc/vconfig
# Please ensure your VLAN IDs are NOT zero-padded
#vlans_eth0="1 2"
#iface_eth0_1="dhcp"
#iface_eth0_2="172.16.2.1 broadcast 172.16.3.255 netmask 255.255.254.0"


##############################################################################
#   IPROUTE2-STYLE CONFIGURATION
#   ----------------------------
#   This makes use of the /sbin/ip command to set routes and ip
#   addresses.  This style is very useful if you are building an
#   advanced linux router or using VRRP For more information on how to
#   use the iproute2 command have a look at the ip(8) man page.
#
#   NOTE: You must emerge sys-apps/iproute2 to use this configuration
##############################################################################

# To use DHCP on eth0, simply uncomment the following line:
#ipaddr_eth0=( "dhcp" )

# For passing custom options to dhcpcd use something like the following.  This
# example reduces the timeout for retrieving an address from 60 seconds (the
# default) to 10 seconds.  Note that this might be too short for some servers,
# so the default is encouraged unless you know what you're doing.
#dhcpcd_eth0="-t 10"

# For a static configuration, use something like this.  This sets the primary
# eth0 address to 192.168.0.2 and adds two aliases, eth0:1 and eth0:2.
# You may also pass advanced ip addr commands via this option.
#ipaddr_eth0=(
#	"192.168.0.2/24 brd 192.168.0.255"
#	"192.168.0.3/24 brd 192.168.0.255"
#	"192.168.0.4/24 brd 192.168.0.255"
#)

# The following will set up the default gateway. You can also pass advanced
# route configs via this option.
#iproute_eth0=(
#	"default via 192.168.0.1"
#)

# VLAN support - emerge net-misc/vconfig
# Please ensure your VLAN IDs are NOT zero-padded
#vlans_eth0="1 2"
#ipaddr_eth0_1=( "dhcp" )
#ipaddr_eth0_2=( "172.16.2.1/23 brd 172.16.3.255" )


##############################################################################
#   ADVANCED CONFIGURATION
#   ----------------------
#   The items in this section can be combined with one of the sections
#   above to create a complete network configuration.  None of the
#   items in this section are necessary to configure for most users.
##############################################################################

# Four functions can be defined which will be called surrounding the
# start/stop operations.  The functions are called with the interface
# name first so that one function can control multiple adapters.
#
# The return values for the preup and predown functions should be 0
# (success) to indicate that configuration or deconfiguration of the
# interface can continue.  If preup returns a non-zero value, then
# interface configuration will be aborted.  If predown returns a
# non-zero value, then the interface will not be allowed to continue
# deconfiguration.
#
# The return values for the postup and postdown functions are ignored
# since there's nothing to do if they indicate failure.
#
#preup() {
#	# Test for link on the interface prior to bringing it up.  This
#	# only works on some network adapters and requires the ethtool
#	# package to be installed.
#	if ethtool $1 | grep -q 'Link detected: no'; then
#		ewarn "No link on $1, aborting configuration"
#		return 1
#	fi
#
#	# Remember to return 0 on success
#	return 0
#}
#
#predown() {
#	# Test to make sure the root filesystem is not mounted via NFS.
#	# Interface ($1) is ignored but could also be tested if you know
#	# what interface is providing NFS root.
#	if grep -q ' / nfs' /proc/mounts; then
#		ewarn "Root is NFS mounted, aborting deconfiguration of $1"
#		return 1
#	fi
#
#	# Remember to return 0 on success
#	return 0
#}
#
#postup() {
#	# This function could be used, for example, to register with a
#	# dynamic DNS service.  Another possibility would be to
#	# send/receive mail once the interface is brought up.
#}
#
#postdown() {
#	# This function is mostly here for completeness... I haven't
#	# thought of anything nifty to do with it yet ;-)
#}

# For changing MAC addresses - emerge net-analyzer/macchanger
# - to set a specific MAC address
#mac_eth0="00:11:22:33:44:55"
# - to randomize the last 3 bytes only
#mac_eth0="random-ending"
# - to randomize between the same physical type of connection (eg fibre, copper, wireless) , all vendors
#mac_eth0="random-samekind"
# - to randomize between any physical type of connection (eg fibre, copper, wireless) , all vendors
#mac_eth0="random-anykind"
# - full randomization - WARNING: some MAC addresses generated by this may NOT act as expected
#mac_eth0="random-full"
# custom - passes all parameters directly to net-analyzer/macchanger
#mac_eth0="some custom set of parameters"

# uncomment this function for support for changing MAC addresses
# also uncomment it's invocation in preup() above
preup_macchanger() {
	eval mac_IFACE=\"\$\{mac_${IFACE}\}\"
	if [ -n "${mac_IFACE}" ]; then
		if [[ -x /sbin/macchanger ]]; then
			local macchanger_opts=""
			case "${mac_IFACE}" in
				# specific mac-addr, i wish there were a shorter way to specify this 
				[0-9a-zA-Z][0-9a-zA-Z]:[0-9a-zA-Z][0-9a-zA-Z]:[0-9a-zA-Z][0-9a-zA-Z]:[0-9a-zA-Z][0-9a-zA-Z]:[0-9a-zA-Z][0-9a-zA-Z]:[0-9a-zA-Z][0-9a-zA-Z]) macchanger_opts="${macchanger_opts} --mac=${mac_IFACE}" ;;
				# increment MAC address, default macchanger behavior
				increment) macchanger_opts="${macchanger_opts}" ;;
        	    # randomize just the ending bytes
        	    random-ending) macchanger_opts="${macchanger_opts} -e" ;;
        	    # keep the same kind of physical layer (eg fibre, copper)
        	    random-samekind) macchanger_opts="${macchanger_opts} -a" ;;
        	    # randomize to any known vendor of any physical layer type
        	    random-anykind) macchanger_opts="${macchanger_opts} -A" ;;
        	    # fully random bytes
        	    random-full) macchanger_opts="${macchanger_opts} -r" ;;
        	    # default case is just to pass on all the options
				*) macchanger_opts="${macchanger_opts} ${mac_IFACE}" ;;
			esac
        	#echo "/sbin/macchanger ${macchanger_opts} ${IFACE}"
        	/sbin/macchanger ${macchanger_opts} ${IFACE} >/dev/null 
			retval=$?
			if [ $retval -gt 0 ]; then
				eerror "Failed to set MAC address"
				return 1
			fi
		else	
			eerror "For changing MAC addresses, emerge net-analyzer/macchanger"
			return 1
		fi
	fi
	return 0 #important
}

# For link bonding/trunking - emerge net-misc/ifenslave
# if you are using any of the slave interfaces, it is important that you remove
# configurations for them and take them down first!
slaves_bond0="eth2"
ipaddr_bond0=( "172.16.2.1/23 brd 172.16.3.255" )

postup_bonding() {
	# return silently if this is not a bonding interface
	if [ -n "${IFACE/bond*/}" ]; then
		return 0
	fi
	eval slaves_IFACE=\"\$\{slaves_${IFACE}\}\"
	if [[ -n "${slaves_IFACE}" ]]; then
		if [[ -x /sbin/ifenslave ]]; then
			# must force the slaves to a particular state before adding them
			for slaveiface in ${slaves_IFACE}; do
				ifconfig ${slaveiface} 0.0.0.0 up
			done
			# now force the master to up
			ifconfig ${IFACE} up
			# finally add in slaves
			/sbin/ifenslave ${IFACE} ${slaves_IFACE}
		else
			eerror "For link aggregation (bonding), emerge net-misc/ifenslave"
			return 1
		fi
	fi
	return 0 #important
}

predown_bonding() {
	# return silently if this is not a bonding interface
	if [ -n "${IFACE/bond*/}" ]; then
		return 0
	fi
	# don't trust the config, get the active list instead
	slaves_IFACE=$(awk '/^Slave Interface:/ { printf $3" " }' /proc/net/bonding/${IFACE})
	if [[ -n "${slaves_IFACE}" ]]; then
		# remove all slaves
		echo /sbin/ifenslave -d ${IFACE} ${slaves_IFACE}
		/sbin/ifenslave -d ${IFACE} ${slaves_IFACE}
		# reset all slaves
		for slaveiface in ${slaves_IFACE}; do
			ifconfig ${slaveiface} 0.0.0.0 down
		done
	fi
	return 0
}

preup_linkdetect() {
	# Test for link on the interface prior to bringing it up.  This
	# only works on some network adapters and requires the ethtool
	# package to be installed.
	if ethtool $1 | grep -q 'Link detected: no'; then
		ewarn "No link on $1, aborting configuration"
		return 1
	fi
}

