summaryrefslogtreecommitdiffstats
path: root/ipv6.sh
blob: e9625c972c3e4b27cca0ac2dcb71a993db91f104 (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
#!/bin/dash

VERBOSE=1
LOCAL_V6_ADDR='::1/64'

TUNNEL_IF='sit0'
RELAY='192.88.99.1'

case "$1" in
    start)
	if [ -z "$LOCAL4" ]; then
	    if [ -z "$LOCAL_IF" ]; then
	 	LOCAL_IF=$(ip -o route show \
			|sed -nre '/^default /s/^default .*dev ([^ ]+).*/\1/p')
	    fi
	    LOCAL4=$(ip -o addr show $LOCAL_IF | grep ' inet ' \
		    | grep -v ' secondary ' | head -n 1 \
	    	    | sed -e 's/.*inet \([^ ]*\) .*/\1/' -e 's/\/.*//')
	    if [ -z "$LOCAL4" ]; then
		echo "Cannot find the IP address assigned to $LOCAL_IF"
		exit 1
	     fi
	fi
	
	SUBNET=$(printf "%x%02x:%x%02x" $(echo $LOCAL4 | sed -e 's/\./ /g'))
	LOCAL6NET="2002:${SUBNET}"
	LOCAL6="${LOCAL6NET}${LOCAL_V6_ADDR}"

	if [ "$VERBOSE" ]; then
	    echo "Local v4 address: $LOCAL4"
	    echo "Local v6 address: $LOCAL6"
	    echo "6to4 relay address: $RELAY"
	fi

	ip link  set $TUNNEL_IF  up
	ip addr  add $LOCAL6  dev $TUNNEL_IF
	ip route add 2000::/3  via ::$RELAY
	ip route add $LOCAL6NET  dev lo 2> /dev/null || true
	;;
    stop)
	ip link  set $TUNNEL_IF  down
	;;
    *)
	echo "Usage: $0 {start|stop}"
	exit 1
	;;
esac

exit 0