root / common.sh @ 58645c13
History | View | Annotate | Download (8 kB)
1 |
#!/bin/bash |
---|---|
2 |
|
3 |
function try { |
4 |
|
5 |
$1 &>/dev/null || true |
6 |
|
7 |
} |
8 |
|
9 |
function clear_routed_setup_ipv4 { |
10 |
|
11 |
arptables -D OUTPUT -o $INTERFACE --opcode request -j mangle |
12 |
while ip rule del dev $INTERFACE; do :; done |
13 |
iptables -D FORWARD -i $INTERFACE -p udp --dport 67 -j DROP |
14 |
|
15 |
} |
16 |
|
17 |
function clear_routed_setup_ipv6 { |
18 |
|
19 |
while ip -6 rule del dev $INTERFACE; do :; done |
20 |
|
21 |
} |
22 |
|
23 |
|
24 |
function clear_routed_setup_firewall { |
25 |
|
26 |
for oldchain in protected unprotected limited; do |
27 |
iptables -D FORWARD -o $INTERFACE -j $oldchain |
28 |
ip6tables -D FORWARD -o $INTERFACE -j $oldchain |
29 |
done |
30 |
|
31 |
} |
32 |
|
33 |
function clear_ebtables { |
34 |
|
35 |
runlocked $RUNLOCKED_OPTS ebtables -D FORWARD -i $INTERFACE -j $FROM |
36 |
runlocked $RUNLOCKED_OPTS ebtables -D FORWARD -o $INTERFACE -j $TO |
37 |
#runlocked $RUNLOCKED_OPTS ebtables -D OUTPUT -o $INTERFACE -j $TO |
38 |
|
39 |
runlocked $RUNLOCKED_OPTS ebtables -X $FROM |
40 |
runlocked $RUNLOCKED_OPTS ebtables -X $TO |
41 |
} |
42 |
|
43 |
|
44 |
function clear_nfdhcpd { |
45 |
|
46 |
rm $NFDHCPD_STATE_DIR/$INTERFACE |
47 |
|
48 |
} |
49 |
|
50 |
|
51 |
function routed_setup_ipv4 { |
52 |
|
53 |
if [ -z "$INTERFACE" -o -z "$NETWORK_GATEWAY" -o -z "$IP" -o -z "$TABLE" ] |
54 |
then |
55 |
return |
56 |
fi |
57 |
|
58 |
# mangle ARPs to come from the gw's IP |
59 |
arptables -A OUTPUT -o $INTERFACE --opcode request -j mangle --mangle-ip-s "$NETWORK_GATEWAY" |
60 |
|
61 |
# route interface to the proper routing table |
62 |
ip rule add dev $INTERFACE table $TABLE |
63 |
|
64 |
# static route mapping IP -> INTERFACE |
65 |
ip route replace $IP proto static dev $INTERFACE table $TABLE |
66 |
|
67 |
# Enable proxy ARP |
68 |
echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp |
69 |
|
70 |
# Send GARP from host to upstream router |
71 |
get_uplink $TABLE |
72 |
echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind |
73 |
$SNF_NETWORK_LOG $0 "arping -c3 -I $UPLINK -U $IP" |
74 |
arping -c3 -I $UPLINK -U $IP |
75 |
echo 0 > /proc/sys/net/ipv4/ip_nonlocal_bind |
76 |
|
77 |
} |
78 |
|
79 |
function routed_setup_ipv6 { |
80 |
# Add a routing entry for the eui-64 |
81 |
get_uplink $TABLE "-6" |
82 |
get_eui64 $MAC $NETWORK_SUBNET6 |
83 |
|
84 |
if [ -z "$EUI64" -o -z "$TABLE" -o -z "$INTERFACE" -o -z "$UPLINK" ] |
85 |
then |
86 |
return |
87 |
fi |
88 |
|
89 |
ip -6 rule add dev $INTERFACE table $TABLE |
90 |
ip -6 ro replace $EUI64/128 dev $INTERFACE table $TABLE |
91 |
ip -6 neigh add proxy $EUI64 dev $UPLINK |
92 |
|
93 |
# disable proxy NDP since we're handling this on userspace |
94 |
# this should be the default, but better safe than sorry |
95 |
echo 0 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp |
96 |
|
97 |
# Send Unsolicited Neighbor Advertisement |
98 |
$SNF_NETWORK_LOG $0 "ndsend $EUI64 $UPLINK" |
99 |
ndsend $EUI64 $UPLINK |
100 |
|
101 |
} |
102 |
|
103 |
# pick a firewall profile per NIC, based on tags (and apply it) |
104 |
function routed_setup_firewall { |
105 |
# for latest ganeti there is no need to check other but uuid |
106 |
ifprefixindex="synnefo:network:$INTERFACE_INDEX:" |
107 |
ifprefixname="synnefo:network:$INTERFACE_NAME:" |
108 |
ifprefixuuid="synnefo:network:$INTERFACE_UUID:" |
109 |
for tag in $TAGS; do |
110 |
tag=${tag#$ifprefixindex} |
111 |
tag=${tag#$ifprefixname} |
112 |
tag=${tag#$ifprefixuuid} |
113 |
case $tag in |
114 |
protected) |
115 |
chain=protected |
116 |
;; |
117 |
unprotected) |
118 |
chain=unprotected |
119 |
;; |
120 |
limited) |
121 |
chain=limited |
122 |
;; |
123 |
esac |
124 |
done |
125 |
|
126 |
if [ "x$chain" != "x" ]; then |
127 |
iptables -A FORWARD -o $INTERFACE -j $chain |
128 |
ip6tables -A FORWARD -o $INTERFACE -j $chain |
129 |
fi |
130 |
} |
131 |
|
132 |
function init_ebtables { |
133 |
|
134 |
runlocked $RUNLOCKED_OPTS ebtables -N $FROM |
135 |
runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -i $INTERFACE -j $FROM |
136 |
runlocked $RUNLOCKED_OPTS ebtables -N $TO |
137 |
runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -o $INTERFACE -j $TO |
138 |
|
139 |
} |
140 |
|
141 |
|
142 |
function setup_ebtables { |
143 |
|
144 |
# do not allow changes in ip-mac pair |
145 |
if [ -n "$IP"]; then |
146 |
runlocked $RUNLOCKED_OPTS ebtables -A $FROM --ip-source \! $IP -p ipv4 -j DROP |
147 |
fi |
148 |
runlocked $RUNLOCKED_OPTS ebtables -A $FROM -s \! $MAC -j DROP |
149 |
#accept dhcp responses from host (nfdhcpd) |
150 |
runlocked $RUNLOCKED_OPTS ebtables -A $TO -p ipv4 --ip-protocol=udp --ip-destination-port=68 -j ACCEPT |
151 |
# allow only packets from the same mac prefix |
152 |
runlocked $RUNLOCKED_OPTS ebtables -A $TO -s \! $MAC/$MAC_MASK -j DROP |
153 |
} |
154 |
|
155 |
function setup_masq { |
156 |
|
157 |
# allow packets from/to router (for masquerading) |
158 |
# runlocked $RUNLOCKED_OPTS ebtables -A $TO -s $NODE_MAC -j ACCEPT |
159 |
# runlocked $RUNLOCKED_OPTS ebtables -A INPUT -i $INTERFACE -j $FROM |
160 |
# runlocked $RUNLOCKED_OPTS ebtables -A OUTPUT -o $INTERFACE -j $TO |
161 |
return |
162 |
|
163 |
} |
164 |
|
165 |
function setup_nfdhcpd { |
166 |
umask 022 |
167 |
FILE=$NFDHCPD_STATE_DIR/$INTERFACE |
168 |
#IFACE is the interface from which the packet seems to arrive |
169 |
#needed in bridged mode where the packets seems to arrive from the |
170 |
#bridge and not from the tap |
171 |
cat >$FILE <<EOF |
172 |
INDEV=$INDEV |
173 |
IP=$IP |
174 |
MAC=$MAC |
175 |
HOSTNAME=$GANETI_INSTANCE_NAME |
176 |
TAGS="$TAGS" |
177 |
GATEWAY=$NETWORK_GATEWAY |
178 |
SUBNET=$NETWORK_SUBNET |
179 |
GATEWAY6=$NETWORK_GATEWAY6 |
180 |
SUBNET6=$NETWORK_SUBNET6 |
181 |
EUI64=$($MAC2EUI64 $MAC $NETWORK_SUBNET6 2>/dev/null) |
182 |
EOF |
183 |
|
184 |
} |
185 |
|
186 |
function get_uplink { |
187 |
|
188 |
local table=$1 |
189 |
local version=$2 |
190 |
UPLINK=$(ip "$version" route list table "$table" | grep "default via" | awk '{print $5}') |
191 |
|
192 |
} |
193 |
|
194 |
# Because we do not have IPv6 value in our environment |
195 |
# we caclulate it based on the NIC's MAC and the IPv6 subnet (if any) |
196 |
# first argument MAC second IPv6 subnet |
197 |
# Changes global value EUI64 |
198 |
get_eui64 () { |
199 |
|
200 |
local mac=$1 |
201 |
local prefix=$2 |
202 |
|
203 |
if [ -z "$prefix" ]; then |
204 |
EUI64= |
205 |
else |
206 |
EUI64=$($MAC2EUI64 $mac $prefix) |
207 |
fi |
208 |
|
209 |
} |
210 |
|
211 |
|
212 |
# DDNS related functions |
213 |
|
214 |
# ommit zone statement |
215 |
# nsupdate will attempt determine the correct zone to update based on the rest of the input |
216 |
send_command () { |
217 |
|
218 |
local command="$1" |
219 |
$SNF_NETWORK_LOG "nsupdate:" "$command" |
220 |
nsupdate -k $KEYFILE > /dev/null << EOF |
221 |
server $SERVER |
222 |
$command |
223 |
send |
224 |
EOF |
225 |
|
226 |
} |
227 |
|
228 |
|
229 |
update_arecord () { |
230 |
|
231 |
local action=$1 |
232 |
local command= |
233 |
if [ -n "$IP" ]; then |
234 |
command="update $action $GANETI_INSTANCE_NAME.$FZONE $TTL A $IP" |
235 |
send_command "$command" |
236 |
fi |
237 |
|
238 |
} |
239 |
|
240 |
|
241 |
update_aaaarecord () { |
242 |
|
243 |
local action=$1 |
244 |
local command= |
245 |
if [ -n "$EUI64" ]; then |
246 |
command="update $action $GANETI_INSTANCE_NAME.$FZONE $TTL AAAA $EUI64" |
247 |
send_command "$command" |
248 |
fi |
249 |
|
250 |
} |
251 |
|
252 |
|
253 |
update_ptrrecord () { |
254 |
|
255 |
local action=$1 |
256 |
local command= |
257 |
if [ -n "$IP" ]; then |
258 |
command="update $action $RLPART.$RZONE. $TTL PTR $GANETI_INSTANCE_NAME.$FZONE" |
259 |
send_command "$command" |
260 |
fi |
261 |
|
262 |
} |
263 |
|
264 |
update_ptr6record () { |
265 |
|
266 |
local action=$1 |
267 |
local command= |
268 |
if [ -n "$EUI64" ]; then |
269 |
command="update $action $R6LPART$R6ZONE. $TTL PTR $GANETI_INSTANCE_NAME.$FZONE" |
270 |
send_command "$command" |
271 |
fi |
272 |
|
273 |
} |
274 |
|
275 |
update_all () { |
276 |
|
277 |
local action=$1 |
278 |
update_arecord $action |
279 |
update_aaaarecord $action |
280 |
update_ptrrecord $action |
281 |
update_ptr6record $action |
282 |
|
283 |
} |
284 |
|
285 |
|
286 |
# first argument is an eui64 (IPv6) |
287 |
# sets GLOBAL args R6REC, R6ZONE, R6LPART |
288 |
# lets assume eui64=2001:648:2ffc:1::1 |
289 |
# the following commands produce: |
290 |
# R6REC=1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.c.f.f.2.8.4.6.0.1.0.0.2.ip6.arpa |
291 |
# R6ZONE=1.0.0.0.c.f.f.2.8.4.6.0.1.0.0.2.ip6.arpa |
292 |
# R6LPART=1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0. |
293 |
get_rev6_info () { |
294 |
|
295 |
local eui64=$1 |
296 |
if [ -z "$eui64" ]; then |
297 |
R6REC= ; R6ZONE= ; R6LPART= ; |
298 |
else |
299 |
R6REC=$(host $eui64 | egrep -o '([[:alnum:]]\.){32}ip6.arpa' ) |
300 |
R6ZONE=$(echo $R6REC | awk -F. 'BEGIN{rpart="";} { for (i=32;i>16;i=i-1) rpart=$i "." rpart; } END{print rpart "ip6.arpa";}') |
301 |
R6LPART=$(echo $R6REC | awk -F. 'BEGIN{lpart="";} { for (i=16;i>0;i=i-1) lpart=$i "." lpart; } END{print lpart;}') |
302 |
fi |
303 |
|
304 |
} |
305 |
|
306 |
|
307 |
# first argument is an ipv4 |
308 |
# sets args RZONE, RLPART |
309 |
# lets assume IP=203.0.113.1 |
310 |
# RZONE="113.0.203.in-add.arpa" |
311 |
# RLPART="1" |
312 |
get_rev4_info () { |
313 |
|
314 |
local ip=$1 |
315 |
if [ -z "$ip" ]; then |
316 |
RZONE= ; RLPART= ; |
317 |
else |
318 |
OLDIFS=$IFS |
319 |
IFS=". " |
320 |
set -- $ip |
321 |
a=$1 ; b=$2; c=$3; d=$4; |
322 |
IFS=$OLDIFS |
323 |
RZONE="$c.$b.$a.in-addr.arpa" |
324 |
RLPART="$d" |
325 |
fi |
326 |
|
327 |
} |
328 |
|
329 |
|
330 |
# Query nameserver for entries related to the specific instance |
331 |
# An example output is the following: |
332 |
# www.google.com has address 173.194.113.114 |
333 |
# www.google.com has address 173.194.113.115 |
334 |
# www.google.com has address 173.194.113.116 |
335 |
# www.google.com has address 173.194.113.112 |
336 |
# www.google.com has address 173.194.113.113 |
337 |
# www.google.com has IPv6 address 2a00:1450:4001:80b::1012 |
338 |
query_dns () { |
339 |
|
340 |
HOSTQ="host -s -R 3 -W 3" |
341 |
HOST_IP_ALL=$($HOSTQ $GANETI_INSTANCE_NAME.$FZONE $SERVER | sed -n 's/.*has address //p') |
342 |
HOST_IP6_ALL=$($HOSTQ $GANETI_INSTANCE_NAME.$FZONE $SERVER | sed -n 's/.*has IPv6 address //p') |
343 |
|
344 |
} |