#!/bin/sh
#
# Check if an IPv6 address we are going to assign to an Ethernet interface is
# already in use by another system.
#
# This script should be installed in /etc/network/if-up.d/
# if you want it to be used whenever an interface is configured.
# 
# It can also be used as a standalone script by setting up
# its environment:
#    IFACE=eth0 IFACE=<IPv6 address> check-duplicate-ip6
#
# NOTE: IF_ADDRESS is optional, if not provided it will be determined
# by using the ip tools
#
# This script only works with IPv6 addresses
#
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#  
# You can also find a copy of the GNU General Public License at
# http://www.gnu.org/licenses/licenses.html#TOCLGPL
# Check if an IP we are going to assign to an Ethernet interface
# is already in use by another system.
#

DEFAULT=/etc/default/network-test
# Read system default file
[ -r "$DEFAULT" ] && . $DEFAULT

# Do not continue if the user has told us to not send network probes
[ "$DO_ARPING" = "no" ]  && exit 0

# Defaults
ETHTOOL=/sbin/ethtool
NDISC=/usr/bin/ndisc6
[ ! -x "$ETHTOOL" ] && [ -x "/usr/sbin/ethtool" ] && ETHTOOL=/usr/sbin/ethtool
[ ! -x "$NDISC" ] && exit 0 # Silent exit if ndisc is not installed
DO_SYSLOG=${DO_SYSLOG:-yes}
VERBOSITY=${VERBOSITY:-0}


# Set up our environment
LC_ALL=C
export LC_ALL

if [ "$DO_SYSLOG" = "yes" ] ; then
	OUTPUT="logger -i -p daemon.err -s"
else
	OUTPUT="echo"
fi

do_ndisc() {
# Use the Network Discovery Protocol to detect if there is a duplicate address
# "out there"

# Do not do the check if ethtool (if installed) tells us the interface
# does not have link, notice that ARPING will try to send the ARP requests
# even if there is no link so we use this to speed things up

# First determine physical interface in case aliased interfaces are used
        real_iface=$(echo "$IFACE" | sed -e 's|:[[:digit:]]\+||')
	if [ -x "$ETHTOOL" ] ; then
	        LINK="`$ETHTOOL $real_iface 2>&1| grep \"Link[[:blank:]]\+detected:\"`"
	        if ! echo $LINK | grep -q "yes$" ; then
			return
		fi
	fi

        for ADDR in $IF_ADDRESS; do
        # Only check IP address if it is IPv6
            if echo ${ADDR} | grep -q ":" ; then
		[ "$VERBOSITY" -eq 1 ] && $OUTPUT "DEBUG: Sending arp pings through $real_iface (for $IFACE) to detect other systems using $ADDR"
                $NDISC -q $ADDR $real_iface
		if [ $? -eq 0 ] ; then
                    $OUTPUT "ERROR: Duplicate address $ADDR assigned in the network where $real_iface is connected to."
		fi
            fi
	done
}

find_ip6() {
# Try to obtain our IPv6 addresses
       export IF_ADDRESS
       IF_ADDRESS=$(ip addr show "$IFACE" | sed -rne 's|^[[:blank:]]*inet6[[:blank:]]+([^/]+)/.*|\1|p')
       return 0
}

if [ -z "$IFACE" ] ; then
    echo "ERROR: Do not know what interface to check. IFACE environment variable is not defined!" >&2
    exit 1
fi



# Check our IFACE name, if it does not start with eth, bail out
case "$IFACE" in 
	eth*) 
        [ -z "$IF_ADDRESS" ] && find_ip6
        # Still no IPv6 address, then Bail out without error (IPv6 addresses are not required)
        [ -z "$IF_ADDRESS" ] && exit 0
        do_ndisc ;;
	*) ;;
esac

exit 0
