chiark / gitweb /
svc/tripe-ifup.in: Allow netmasks on local and remote inside addresses.
[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
a62f8e8a
MW
27###--------------------------------------------------------------------------
28### Collect arguments.
29
30## Collect the simple arguments.
31if [ $# -lt 3 ]; then
32 echo >&2 "usage: $0 PEER IFNAME ADDR..."; exit 1
33fi
34peer=$1 ifname=$2 family=$3; shift 3
35
36## Parse the address family.
37case "$family,$#" in
38 INET,1) addr=$1 port=4070 ;;
39 INET,2) addr=$1 port=$2 ;;
40 INET,*) echo >&2 "$0: bad INET address"; exit 1 ;;
41 *) echo >&2 "$0: unknown address family $family"; exit 1 ;;
42esac
43
44###--------------------------------------------------------------------------
45### Set the interface name.
46
47case "${P_IFNAME+set}" in
48 set)
49 ip link set "$ifname" name "$P_IFNAME"
50 ifname=$P_IFNAME
51 $tripectl setifname "$peer" "$ifname"
52 ;;
53esac
54
55###--------------------------------------------------------------------------
baa631c5 56### Configure the link.
a62f8e8a 57
90b20d79
MW
58## Split local addresses into v4 and v6 lists.
59unset l4addr l6addr
60for a in $P_LADDR; do
61 case "$a" in
62 *:*) l6addr=${l6addr+$l6addr }$a ;;
63 *) l4addr=${l4addr+$l4addr }$a ;;
64 esac
65done
66
67## Determine the remote v4 and v6 addresses. We only allow one remote
68## address for each: others can be added as routes.
69unset r4addr r6addr
70for a in $P_RADDR; do
71 case "$a" in
72 *:*) r6addr=$a ;;
73 *) r4addr=$a ;;
74 esac
75done
76
77## Configure the first v4 address as point-to-point; add the others as plain
78## addresses.
79haveaddr4=nil
80set -- $l4addr
81case $#,${r4addr+set} in
82 [1-9]*,set)
83 ip addr add "$1" peer "$r4addr" dev "$ifname"
84 haveaddr4=t
85 shift
86 ;;
87esac
88for a in "$@"; do
baa631c5 89 ip addr add "$a" dev "$ifname"
90b20d79
MW
90 haveaddr4=t
91done
92
93## IPv6 point-to-point links seem broken in Linux. Attach the local and
94## remote addresses by hand.
95haveaddr6=nil
96set -- $l6addr
97case $have6,$# in
98 t,[1-9]*)
99 for a in "$@"; do
baa631c5 100 ip addr add "$a" dev "$ifname"
90b20d79
MW
101 haveaddr6=t
102 done
103 case ${r6addr+set} in
baa631c5 104 set) ip route add $r6addr proto static dev "$ifname" ;;
a62f8e8a 105 esac
a62f8e8a
MW
106 ;;
107esac
108
f5d185e4
MW
109###--------------------------------------------------------------------------
110### Bring the interface up.
111
112case $haveaddr4,$haveaddr6 in
113 nil,nil)
114 ;;
115 *)
116 case "${P_MTU+set}" in
117 set)
118 mtu=$P_MTU;;
119 *)
120 pathmtu=$(pathmtu "$addr")
121 mtu=$(expr "$pathmtu" - 33 - $A_CIPHER_BLKSZ - $A_MAC_TAGSZ)
122 ;;
123 esac
124 ip link set dev "$ifname" up mtu "$mtu"
125 ;;
126esac
127
a62f8e8a
MW
128###--------------------------------------------------------------------------
129### Set up routing.
130
90b20d79
MW
131## Split the routes into v4 and v6 lists.
132unset route4 route6
133for p in $P_NETS; do
134 case "$p" in
135 *:*) route6=${route6+$route6 }$p ;;
136 *) route4=${route4+$route4 }$p ;;
137 esac
138done
139
140## Add the v4 routes.
141set -- $route4
142case $haveaddr4,$# in
143 t,[1-9]*)
144 for p in "$@"; do
f3cd8d05 145 ip route add $p proto static via "$r4addr"
90b20d79
MW
146 done
147 ;;
148esac
149
150## Add the v6 routes.
151set -- $route6
152case $haveaddr6,$# in
153 t,[1-9]*)
154 for p in "$@"; do
f3cd8d05 155 ip route add $p proto static via "$r6addr"
a62f8e8a
MW
156 done
157 ;;
158esac
159
160###--------------------------------------------------------------------------
161### Maybe invoke a follow-on script.
162
163case "${P_IFUPEXTRA+set}" in
164 set)
165 eval "$P_IFUPEXTRA"
166 ;;
167esac
168
169###--------------------------------------------------------------------------
170### Issue a notification that we've won.
171
172$tripectl notify tripe-ifup configured "$peer"
173
174###----- That's all, folks --------------------------------------------------