#!/bin/sh
#
#	$Id: ifup-aliases 6491 2005-10-26 16:33:29Z baggins $
#
# adds all addresses of device $1

. /etc/sysconfig/network
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network-scripts/functions.network

# device name must be ifcfg-somename
DEV=$1

[ -z "$DEV" ] && {
	nls "Usage: %s <device name>" "ifup-aliases" >&2
	exit 1
}

if [ -f "/etc/sysconfig/interfaces/ifcfg-$DEV" ] ; then
	CONFIG="/etc/sysconfig/interfaces/ifcfg-$DEV"
else
	CONFIG="$DEV"
fi

source_config

# set all major variables
setup_ip_param

# Add all addresses
for IP_ADDR in $IPADDR_ALIASES; do
	IP_ADDR_OPT=""
	IP_ROUTE_OPT=""

	[ -z "$IP_ADDR" ] && continue
	# Be sure that primary address is added
	if [ "$IP_ADDR" = "$IP4ADDR" -o "$IP_ADDR" = "$IP6ADDR" ]; then
		if (ip addr show dev $DEVICE | egrep -q "[[:space:]]${IP_ADDR}[[:space:]]"); then
			continue
		fi
	fi
	if is_yes "${IPV4_NETWORKING}"; then
		# If broadcast is missing then autocalculate it
		if ! (echo $IP_ADDR_OPT | egrep -q "brd|broadcast"); then
			IP_ADDR_OPT="brd + ${IP_ADDR_OPT}"
		fi
		ip -4 addr add ${IP_ADDR} dev ${DEVICE} ${IP_ADDR_OPT}
	fi
done

for IP_ADDR in $IPV6_ADDR; do
	IP_ADDR_OPT=""
	IP_ROUTE_OPT=""

	[ -z "$IP_ADDR" ] && continue
	# Be sure that primary address is added
	if [ "$IP_ADDR" = "$IP4ADDR" -o "$IP_ADDR" = "$IP6ADDR" ]; then
		if (ip addr show dev $DEVICE | egrep -q "[[:space:]]${IP_ADDR}[[:space:]]"); then
			continue
		fi
	fi
	# Check for IPv6 address
	if (echo $IP_ADDR | grep -q ":"); then
		if is_yes "${IPV6_NETWORKING}"; then
			ip -6 addr add ${IP_ADDR} dev ${DEVICE} ${IP_ADDR_OPT}
		fi
	elif is_yes "${IPV4_NETWORKING}"; then
		# If broadcast is missing then autocalculate it
		if ! (echo $IP_ADDR_OPT | egrep -q "brd|broadcast"); then
			IP_ADDR_OPT="brd + ${IP_ADDR_OPT}"
		fi
		ip -4 addr add ${IP_ADDR} dev ${DEVICE} ${IP_ADDR_OPT}
	fi
done

for nr in $(awk 'BEGIN { for (j=1; j<256; j++) print j}'); do
	eval IP_ADDR="\$IPADDR${nr}"
	eval IP_ADDR_OPT="\$IP_AOPTS${nr}"
	eval IP_ROUTE_OPT="\$IP_ROPTS${nr}"

	[ -z "$IP_ADDR" ] && continue
	# Be sure that primary address is added
	if [ "$IP_ADDR" = "$IP4ADDR" -o "$IP_ADDR" = "$IP6ADDR" ]; then
		if (ip addr show dev $DEVICE | egrep -q "[[:space:]]${IP_ADDR}[[:space:]]"); then
			continue
		fi
	fi
	# Check for IPv6 address
	if (echo $IP_ADDR | grep -q ":"); then
		if is_yes "${IPV6_NETWORKING}"; then
			ip -6 addr add ${IP_ADDR} dev ${DEVICE} ${IP_ADDR_OPT}
		fi
	elif is_yes "${IPV4_NETWORKING}"; then
		# If broadcast is missing then autocalculate it
		if ! (echo $IP_ADDR_OPT | egrep -q "brd|broadcast"); then
			IP_ADDR_OPT="brd + ${IP_ADDR_OPT}"
		fi
		ip -4 addr add ${IP_ADDR} dev ${DEVICE} ${IP_ADDR_OPT}
	fi
done

# Setup IPv4-compatible addresses
if is_yes "$IPV6_NETWORKING" && [ "$BOOTPROTO6" = "v4compat" ]; then
	# enable automatic tunnels
	ip link set sit0 up

	# add v4compat addresses to device
	for addr in $(ip -4 addr show scope global dev $DEVICE | \
		awk '/inet/ { print $2 }' | awk -F"/" '{ print $1 }'); do
		ip -6 addr add ::$addr dev $DEVICE
	done
fi

# This must be last line !
# vi:syntax=sh
