#!/bin/sh
#
# Check if an IP 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 IF_ADDRESS=192.168.0.1 check-duplicate-ip 
#
# NOTE: IF_ADDRESS is optional
#
#   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.
#

# Defaults
ARPING=/usr/bin/arping
ETHTOOL=/usr/sbin/ethtool
ARP_COUNT=2
ARP_TIMEOUT=3
DO_SYSLOG=yes
[ -z "$VERBOSITY" ] && VERBOSITY=0
# Read system default file
[ -r /etc/default/network-test ] && . /etc/default/network-test

# Do not continue if ETHTOOL is not available
[ ! -x "$ARPING" ] && exit 0
# or if the user has told us to not do arpings
[ "$DO_ARPING" = "no" ]  && exit 0

# Try to obtain our IP address (DHCP case)
if [ -z "$IF_ADDRESS" ] ; then
        IF_ADDRESS=`ip addr show $IFACE | grep "inet " | awk '{print $2}' | sed -e 's/\/.*//'`
fi
# Still no IP? Bail out
[ -z "$IF_ADDRESS" ] && exit 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_arping() {
# Send ARP pings to detect if there is a duplicate address "out there"
# Curiously enough, the script will return faster if there *is* a system
# with the same IP address and will take ${ARP_TIMEOUT}*${ARP_COUNT} seconds
# to return if there is none.

# 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
	if [ -x "$ETHTOOL" ] ; then
	        LINK=`$ETHTOOL $IFACE 2>&1| grep "Link detected"`
	        if ! $ETHTOOL $IFACE | grep -q "Link detected: yes" ; then
			return
		fi
	fi

	[ "$VERBOSITY" -eq 1 ] && $OUTPUT "DEBUG: Sending arp pings through $IFACE to detect other systems using $IF_ADDRESS"
	if ! $ARPING -q -c $ARP_COUNT -w $ARP_TIMEOUT -D -I $IFACE $IF_ADDRESS ; then
		$OUTPUT "ERROR: Duplicate address $IF_ADDRESS assigned in the network where $IFACE is connected to"
	fi
}

# Check our IFACE name, if it does not start with eth, bail out
case "$IFACE" in 
	eth*) do_arping ;;
	*) ;;
esac
exit 0
