#!/bin/sh
#
# Miredo client hook script for Linux/iproute2
# Copyright © 2007 Rémi Denis-Courmont.
# Distributed under the terms of the GNU General Public License version 2.
#
# $Id: client-hook.iproute 2051 2007-09-21 14:39:30Z remi $

# Linux iproute2 path:
IP="$(which ip)"

# Linux default route default metric is 1024
# (we put 1029 so that Teredo is used as a last resort):
METRIC=1029

# Linux routing table ID
# (possible values: 1-252 from /etc/iproute/rt_tables):
TABLE=teredo

# Linux routing rule priority
# (possible values: 1-32765, lowest number is highest priority):
PRIO=32765

# MTU for the tunnel interface
# (default: specified by the Teredo server, or 1280)
#MTU=1400

if ! test -x "$IP"; then
	echo "$0: iproute2 is required! Please install it." >&2
	exit 1
fi

test "$OLD_ADDRESS" && \
"$IP" -6 rule del from "$OLD_ADDRESS" prio "$PRIO" table "$TABLE" 2>/dev/null

"$IP" -6 route flush table "$TABLE" 2>/dev/null
"$IP" -6 route flush dev "$IFACE" 2>/dev/null
"$IP" -6 address flush dev "$IFACE" 2>/dev/null

"$IP" -6 link set dev "$IFACE" "$STATE"
test "$MTU" && "$IP" link set dev "$IFACE" mtu "$MTU"

case "$STATE" in
	up)
		"$IP" -6 address add "${LLADDRESS}/64" dev "$IFACE"
		"$IP" -6 address add "${ADDRESS}/32" dev "$IFACE"
		"$IP" -6 route add default dev "$IFACE" metric "$METRIC"

		"$IP" -6 route add default dev "$IFACE" table "$TABLE"
		"$IP" -6 rule add from "$ADDRESS" prio "$PRIO" table "$TABLE"
		;;
esac

"$IP" -6 route flush cache 2>/dev/null
exit 0
