summaryrefslogtreecommitdiff
path: root/scripts/vyatta-address
blob: f14eff9bb7590fb89ac7078cba96a741cca247f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /bin/bash
#
# Wrapper around ip link command that handles IPv4, IPv6 and DHCP
# This is done in shell rather than perl to avoid the overhead of recompilation

if [ $# -ne 3 ]; then
    echo "Usage: $0 {add|delete} interface address"
    exit 1
fi

case $1 in
 add)
	if [[ "$3" = "dhcp" ]]
	then
	    exec /opt/vyatta/sbin/vyatta-interfaces.pl --dev="$2" --dhcp=start
	elif [[ "$3" = "dhcpv6" ]]
	then
            exec /opt/vyatta/sbin/vyatta-dhcpv6-client.pl --start -ifname "$2"
	elif [[ "$3" =~ ":" ]]
	then # Ipv6 address
		# If IPv6 is disabled, the address is in the configuration
		# and can not be added to the system. By not running the
		# ip command and failing the configuration for the interface
		# is preserved.
		disable_ipv6=`cat /proc/sys/net/ipv6/conf/$2/disable_ipv6`
		clpsIp=`echo "$3" | sed -e 's/^\(.*\):0:[:0]*:0:\(.*\)$/\1::\2/'`
		ip -6 addr list dev "$2" | grep -q "$clpsIp"
		inList=$?
		if [ "$disable_ipv6" = "0" -a "$inList" != "0" ]
		then
			exec ip -6 addr add "$3" dev "$2"
		fi
	else
		if ! ip addr list dev $2 | grep -q $3; then
			exec ip addr add "$3" broadcast + dev "$2"
		fi
	fi ;;

 delete)
	if [ ! -d "/sys/class/net/$2" ]
	then # device is already gone
	    exit 0
	elif [[ "$3" = "dhcp" ]]
	then
	    exec /opt/vyatta/sbin/vyatta-interfaces.pl --dev="$2" --dhcp=stop
	elif [[ "$3" = "dhcpv6" ]]
	then
	    exec /opt/vyatta/sbin/vyatta-dhcpv6-client.pl --stop --ifname "$2"
	elif [[ "$3" =~ ":" ]]
        then
		# If IPv6 is disabled the address is in the configuration
		# and not in the system. By not running the ip command and
		# failing the address is deleted from the configuration.
		disable_ipv6=`cat /proc/sys/net/ipv6/conf/$2/disable_ipv6`
		clpsIp=`echo "$3" | sed -e 's/^\(.*\):0:[:0]*:0:\(.*\)$/\1::\2/'`
		ip -6 addr list dev "$2" | grep -q "$clpsIp"
		inList=$?
		if [ "$disable_ipv6" = "0" -a "$inList" != "0" ]
		then
			exec ip -6 addr del "$3" dev "$2"
		fi
	else
	    exec ip addr del "$3" dev "$2"
	fi ;;
 *)
	echo "Unknown option $1"
	exit 1 ;;
esac