chiark / gitweb /
server/admin.c: Remove spurious `ping' in usage message.
[tripe] / svc / tripe-ifup.in
CommitLineData
a62f8e8a
MW
1#! /bin/sh
2###
3### TrIPE interface initialization script
4### suitable for Linux; other operating systems probably want something
5### similar
6
7###----- Licensing notica ---------------------------------------------------
8###
9### Redistribution, modification and use of this file is permitted without
10### limitation.
11###
12### This file is distributed in the hope that it will be useful,
13### but WITHOUT ANY WARRANTY; without even the implied warranty of
14### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
16set -e
17
90b20d79 18## Import compile-time configuration.
a62f8e8a
MW
19: ${bindir=@bindir@}
20: ${tripectl=$bindir/tripectl}
21PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
22export PATH TRIPEDIR
23
90b20d79
MW
24## Determine whether we have IPv6 support.
25if [ -d /proc/sys/net/ipv6 ]; then have6=t; else have6=nil; fi
26
49bfe6a2
MW
27###--------------------------------------------------------------------------
28### Error handling.
29
30win=t
31try () {
32 if "$@"; then :; else
33 rc=$?
34 tripectl warn tripe-ifup command-failed rc=$rc "$*"
35 win=nil
36 fi
37}
38
a62f8e8a
MW
39###--------------------------------------------------------------------------
40### Collect arguments.
41
42## Collect the simple arguments.
43if [ $# -lt 3 ]; then
44 echo >&2 "usage: $0 PEER IFNAME ADDR..."; exit 1
45fi
46peer=$1 ifname=$2 family=$3; shift 3
47
48## Parse the address family.
47828bd9
MW
49case "$family" in
50 INET) ipsz=20 ;;
51 INET6) ipsz=40 ;;
52 *) echo >&2 "$0: unknown address family $family"; exit 1 ;;
53esac
a62f8e8a 54case "$family,$#" in
47828bd9
MW
55 INET,1 | INET6,1) addr=$1 port=4070 ;;
56 INET,2 | INET6,2) addr=$1 port=$2 ;;
57 INET,* | INET6,*) echo >&2 "$0: bad $family address"; exit 1 ;;
58 *) echo >&2 "$0: unknown address family $family"; exit 1 ;;
a62f8e8a
MW
59esac
60
61###--------------------------------------------------------------------------
62### Set the interface name.
63
64case "${P_IFNAME+set}" in
65 set)
49bfe6a2 66 try ip link set "$ifname" name "$P_IFNAME"
a62f8e8a
MW
67 ifname=$P_IFNAME
68 $tripectl setifname "$peer" "$ifname"
69 ;;
70esac
71
72###--------------------------------------------------------------------------
baa631c5 73### Configure the link.
a62f8e8a 74
90b20d79
MW
75## Split local addresses into v4 and v6 lists.
76unset l4addr l6addr
77for a in $P_LADDR; do
78 case "$a" in
79 *:*) l6addr=${l6addr+$l6addr }$a ;;
80 *) l4addr=${l4addr+$l4addr }$a ;;
81 esac
82done
83
84## Determine the remote v4 and v6 addresses. We only allow one remote
85## address for each: others can be added as routes.
86unset r4addr r6addr
87for a in $P_RADDR; do
88 case "$a" in
89 *:*) r6addr=$a ;;
90 *) r4addr=$a ;;
91 esac
92done
93
94## Configure the first v4 address as point-to-point; add the others as plain
95## addresses.
96haveaddr4=nil
97set -- $l4addr
98case $#,${r4addr+set} in
99 [1-9]*,set)
49bfe6a2 100 try ip addr add "$1" peer "$r4addr" dev "$ifname"
90b20d79
MW
101 haveaddr4=t
102 shift
103 ;;
104esac
105for a in "$@"; do
49bfe6a2 106 try ip addr add "$a" dev "$ifname"
90b20d79
MW
107 haveaddr4=t
108done
109
110## IPv6 point-to-point links seem broken in Linux. Attach the local and
111## remote addresses by hand.
112haveaddr6=nil
113set -- $l6addr
114case $have6,$# in
115 t,[1-9]*)
152a2182
MW
116
117 ## If we're configured to set IPv6 addresses then we should ensure that
118 ## they're going to work, even if the default setting for new interfaces
119 ## is to disable IPv6.
120 try sysctl -q net.ipv6.conf."$ifname".disable_ipv6=0
121
122 ## Now add the source and destination addresses.
90b20d79 123 for a in "$@"; do
49bfe6a2 124 try ip addr add "$a" dev "$ifname"
90b20d79
MW
125 haveaddr6=t
126 done
a62f8e8a
MW
127 ;;
128esac
129
f5d185e4
MW
130###--------------------------------------------------------------------------
131### Bring the interface up.
132
133case $haveaddr4,$haveaddr6 in
134 nil,nil)
135 ;;
136 *)
137 case "${P_MTU+set}" in
138 set)
139 mtu=$P_MTU;;
140 *)
141 pathmtu=$(pathmtu "$addr")
47828bd9 142 mtu=$(( $pathmtu - $ipsz - 9 - $A_BULK_OVERHEAD ))
f5d185e4
MW
143 ;;
144 esac
49bfe6a2 145 try ip link set dev "$ifname" up mtu "$mtu"
f5d185e4
MW
146 ;;
147esac
148
0fc1588c
MW
149###--------------------------------------------------------------------------
150### Set the peer IPv6 address if any.
151
152## IPv6 point-to-point links seem broken in Linux. Attach the local and
153## remote addresses by hand.
154set -- $l6addr
155case $have6,$#,${r6addr+set} in
156 t,[1-9]*,set)
157 try ip route add $r6addr proto static dev "$ifname"
158 ;;
159esac
160
a62f8e8a
MW
161###--------------------------------------------------------------------------
162### Set up routing.
163
90b20d79
MW
164## Split the routes into v4 and v6 lists.
165unset route4 route6
166for p in $P_NETS; do
167 case "$p" in
168 *:*) route6=${route6+$route6 }$p ;;
169 *) route4=${route4+$route4 }$p ;;
170 esac
171done
172
173## Add the v4 routes.
174set -- $route4
175case $haveaddr4,$# in
176 t,[1-9]*)
177 for p in "$@"; do
49bfe6a2 178 try ip route add $p proto static via "$r4addr"
90b20d79
MW
179 done
180 ;;
181esac
182
183## Add the v6 routes.
184set -- $route6
185case $haveaddr6,$# in
186 t,[1-9]*)
187 for p in "$@"; do
49bfe6a2 188 try ip route add $p proto static via "${r6addr%/*}"
a62f8e8a
MW
189 done
190 ;;
191esac
192
193###--------------------------------------------------------------------------
194### Maybe invoke a follow-on script.
195
196case "${P_IFUPEXTRA+set}" in
197 set)
198 eval "$P_IFUPEXTRA"
199 ;;
200esac
201
202###--------------------------------------------------------------------------
203### Issue a notification that we've won.
204
49bfe6a2
MW
205case $win in
206 t) $tripectl notify tripe-ifup configured "$peer" ;;
207 nil) $tripectl notify tripe-ifup configured "$peer" failed ;;
208esac
a62f8e8a
MW
209
210###----- That's all, folks --------------------------------------------------