#!/bin/bash PATH=/sbin:/usr/sbin:$PATH rt_tables=/etc/iproute2/rt_tables confdir=/etc/dgd linkdir=$confdir/links active=$confdir/active forwarders=/var/named/forwarders # 1 2 3 4 5 6 7 # linksetup NAME IF IP GW NS1 NS2 [NET] linksetup() { n=$(awk ' BEGIN { i = -1 } /^#/ { next } /^$/ { next } $2=="'$1'" { i = $1; exit(0) } { t[$1] = $2; if ($1>n) n = $1 } END { if (i == -1) { for (i=0; i < n && t[i]; i++); print i } }' $rt_tables) if [ -n "$n" ]; then echo "$n $1" >> $rt_tables fi ip rule add from $3 table $1 ip rule add from $5 table $1 if [ -n "$6" ]; then ip rule add from $6 table $1 fi ip route add default via $4 table $1 if [ -n "$7" ]; then ip route add $7 dev $2 src $3 table $1 fi test -d $linkdir || mkdir -p $linkdir cat > "$linkdir/$1" <&2 "$0: $linkdir/$name: gw not set" exit 1 fi ip route add default via $link_gw cat > $active < $forwarders rndc reload else echo >&2 "$0: $linkdir/$name: no such file" exit 1 fi } #linkdown NAME linkdown() { local name=$1 link_if link_ip link_gw link_ns link_net link_active shift if [ -f "$linkdir/$name" ]; then readlinkinfo "$linkdir/$name" if [ -z "$link_gw" ]; then echo >&2 "$0: $linkdir/$name: gw not set" exit 1 fi ip route del default via $link_gw if [ -r "$active" ]; then readlinkinfo $active if [ "$link_name" = "$name" ]; then rm $active fi fi else echo >&2 "$0: $linkdir/$name: no such file" exit 1 fi } command=$1 shift case $command in setup) linksetup "$@";; cleanup) linkcleanup "$@";; up) linkup "$@";; down) linkdown "$@";; *) echo >&2 "$0: unrecognized command";; esac