#!/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
NDISC=/usr/bin/ndisc6
[ ! -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:]]\+||')
        link_address=$(ip link show wlan0 | grep link | awk '{print toupper($2)}')

        if [ -z "`ip link show $real_iface up`" ]; then
            return
        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"
                dup_link_address=$($NDISC -q $ADDR $real_iface)
		if [ $? -eq 0 ] ; then
                    # If the link address is the same as our address this is not a problem
                    # (ndisc returns it in at least Wireless interfaces), only report if the link 
                    # address does not match
                    if [ "$link_address" != "$dup_link_address" ] ; then
                        $OUTPUT "ERROR: Duplicate address $ADDR assigned in the network where $real_iface is connected to."
                    fi
		fi
            fi
	done
}

find_ip6() {
# Try to obtain our IPv6 addresses
       export IF_ADDRESS
       IF_ADDRESS=$(ip addr show "$IFACE" 2>/dev/null | 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

# If the interface is a serial (sl), WAN interfaces (ww) or localhost (lo) then skip the test
# as it does not make sense in these
case $IFACE in
        sl* | ww* | lo*) exit 0 ;;
        *) ;;
esac

[ -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 

exit 0
