summaryrefslogtreecommitdiffabout
path: root/dgd/rc.uplink
blob: 239d85fb2cf47596158337b64db12586dd7b1e40 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/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 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"	<<EOF
[link $1]
name=$1
if=$2
ip=$3
gw=$4
ns=$5 $6
net=$7
EOF
}

readlinkinfo() {
    unset link_if link_ip link_gw link_ns link_net
    eval `sed -r 's/^[[:space:]]+//;/^#/d;/^\[/d;s/(.[^=[:space:]]*)[[:space:]]*=[[:space:]]*(.*)/link_\1="\2"/' $1`
}

# linkcleanup NAME
linkcleanup() {
    test -f "$linkdir/$1" && rm "$linkdir/$1"
    ip route flush table $1
}

#linkup NAME
linkup() {
    local name=$1 link_if link_ip link_gw link_ns link_net
    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 add default via $link_gw
	cat > $active <<EOT
[core]
	active = $name
EOT
	echo "$link_ns" | awk '
BEGIN { print "forwarders {" }
{ for (i = 1; i <= NF; i++) print "    " $(i) ";"; }
END { print "};" }' > $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

Return to:

Send suggestions and report system problems to the System administrator.