forked from grnet/snf-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.sh
executable file
·568 lines (438 loc) · 14.7 KB
/
common.sh
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#!/bin/bash
#
# Copyright 2013-2014 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
source /etc/default/snf-network
: ${STATE_DIR:=/var/lib/snf-network}
: ${LOGFILE:=/var/log/ganeti/snf-network.log}
function try {
$1 &>/dev/null || true
}
function clear_save {
rm -f $STATE_DIR/$INTERFACE
}
function init_save {
cat > $STATE_DIR/$INTERFACE <<EOF
INSTANCE=$INSTANCE
IP=$IP
EUI64=$EUI64
LINK=$LINK
NETWORK_NAME=$NETWORK_NAME
INTERFACE_NAME=$INTERFACE_NAME
NETWORK_TAGS="$NETWORK_TAGS"
TAGS="$TAGS"
EOF
}
function save {
echo $@ >> $STATE_DIR/$INTERFACE
$@
}
function log {
snf-network-log "$0" "$@"
}
function clear_routed_setup_ipv4 {
arptables -D OUTPUT -o $INTERFACE --opcode request -j mangle
while ip rule del dev $INTERFACE; do :; done
# This is needed for older snf-network versions
iptables -D FORWARD -i $INTERFACE -p udp --dport 67 -j DROP
# This is needed because we do not know the IP of the stale rule.
# Additionally we cannot refer to line numbers due to possible race.
iptables -t filter -S FORWARD | grep -w $INTERFACE | sed -e 's/-A/-D/' | xargs -L1 iptables
}
function clear_routed_setup_ipv6 {
while ip -6 rule del dev $INTERFACE; do :; done
ip6tables -t filter -S FORWARD | grep -w $INTERFACE | sed -e 's/-A/-D/' | xargs -L1 ip6tables
}
function delete_neighbor_proxy {
if [ -z "$EUI64" -o -z "$UPLINK6" ]; then
return
fi
log "* Deleting Neighbor Proxy for $EUI64 on $UPLINK6"
ip -6 neigh del proxy $EUI64 dev $UPLINK6
}
function clear_routed_setup_firewall {
for oldchain in protected unprotected limited; do
iptables -D FORWARD -o $INTERFACE -j $oldchain
iptables -D FORWARD -o $INTERFACE -j $oldchain -m comment --comment "snf-network_firewall"
ip6tables -D FORWARD -o $INTERFACE -j $oldchain
ip6tables -D FORWARD -o $INTERFACE -j $oldchain -m comment --comment "snf-network_firewall"
done
}
function clear_bridged_setup_firewall {
for oldchain in protected unprotected limited; do
iptables -D FORWARD -m physdev --physdev-out $INTERFACE -j $oldchain
iptables -D FORWARD -m physdev --physdev-out $INTERFACE -j $oldchain -m comment --comment "snf-network_firewall"
ip6tables -D FORWARD -m physdev --physdev-out $INTERFACE -j $oldchain
ip6tables -D FORWARD -m physdev --physdev-out $INTERFACE -j $oldchain -m comment --comment "snf-network_firewall"
done
}
function clear_ebtables {
runlocked $RUNLOCKED_OPTS ebtables -D FORWARD -i $INTERFACE -j $FROM
runlocked $RUNLOCKED_OPTS ebtables -D INPUT -i $INTERFACE -j $FROM
runlocked $RUNLOCKED_OPTS ebtables -D FORWARD -o $INTERFACE -j $TO
runlocked $RUNLOCKED_OPTS ebtables -D OUTPUT -o $INTERFACE -j $TO
runlocked $RUNLOCKED_OPTS ebtables -X $FROM
runlocked $RUNLOCKED_OPTS ebtables -X $TO
}
function clear_nfdhcpd {
rm $NFDHCPD_STATE_DIR/$INTERFACE
}
function routed_setup_ipv4 {
if [ -z "$INTERFACE" -o -z "$NETWORK_GATEWAY" -o -z "$IP" -o -z "$TABLE" ]
then
return
fi
# mangle ARPs to come from the gw's IP
save arptables -A OUTPUT -o $INTERFACE --opcode request -j mangle --mangle-ip-s "$NETWORK_GATEWAY"
# route interface to the proper routing table
save ip rule add dev $INTERFACE table $TABLE
# static route mapping IP -> INTERFACE
save ip route replace $IP proto static dev $INTERFACE table $TABLE
# Do not allow packets with different source IP
save iptables -A FORWARD -i $INTERFACE ! -s $IP -j DROP -m comment --comment "snf-network_routed"
# Enable proxy ARP
echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp
}
function send_garp {
if [ -z "$IP" -o -z "$UPLINK" ]; then
return
fi
# Send GARP from host to upstream router
echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind
log "* Sending GARP for $IP on $UPLINK"
save arpsend -U -i $IP -c1 $UPLINK
echo 0 > /proc/sys/net/ipv4/ip_nonlocal_bind
}
function routed_setup_ipv6 {
if [ -z "$EUI64" -o -z "$TABLE" -o -z "$INTERFACE" -o -z "$UPLINK6" ]
then
return
fi
# Add a routing entry for the eui-64
save ip -6 rule add dev $INTERFACE table $TABLE
save ip -6 ro replace $EUI64/128 dev $INTERFACE table $TABLE
save ip -6 neigh add proxy $EUI64 dev $UPLINK6
# disable proxy NDP since we're handling this on userspace
# this should be the default, but better safe than sorry
echo 0 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp
save ip6tables -A FORWARD -i $INTERFACE ! -s $EUI64 -j DROP -m comment --comment "snf-network_routed"
# Send Unsolicited Neighbor Advertisement
log "* Sending Unsolicited NA for $EUI64 on $UPLINK6"
save ndsend $EUI64 $UPLINK6
}
# pick a firewall profile per NIC, based on tags (and apply it)
function routed_setup_firewall {
# for latest ganeti there is no need to check other but uuid
ifprefixindex="synnefo:network:$INTERFACE_INDEX:"
ifprefixname="synnefo:network:$INTERFACE_NAME:"
ifprefixuuid="synnefo:network:$INTERFACE_UUID:"
for tag in $TAGS; do
tag=${tag#$ifprefixindex}
tag=${tag#$ifprefixname}
tag=${tag#$ifprefixuuid}
case $tag in
protected)
chain=protected
;;
unprotected)
chain=unprotected
;;
limited)
chain=limited
;;
esac
done
if [ "x$chain" != "x" ]; then
save iptables -A FORWARD -o $INTERFACE -j $chain -m comment --comment "snf-network_firewall"
save ip6tables -A FORWARD -o $INTERFACE -j $chain -m comment --comment "snf-network_firewall"
fi
}
# pick a firewall profile per NIC, based on tags (and apply it)
function bridged_setup_firewall {
# for latest ganeti there is no need to check other but uuid
ifprefixindex="synnefo:network:$INTERFACE_INDEX:"
ifprefixname="synnefo:network:$INTERFACE_NAME:"
ifprefixuuid="synnefo:network:$INTERFACE_UUID:"
for tag in $TAGS; do
tag=${tag#$ifprefixindex}
tag=${tag#$ifprefixname}
tag=${tag#$ifprefixuuid}
case $tag in
protected)
chain=protected
;;
unprotected)
chain=unprotected
;;
limited)
chain=limited
;;
esac
done
if [ "x$chain" != "x" ]; then
save iptables -I FORWARD -m physdev --physdev-out $INTERFACE -j $chain -m comment --comment "snf-network_firewall"
save ip6tables -I FORWARD -m physdev --physdev-out $INTERFACE -j $chain -m comment --comment "snf-network_firewall"
fi
}
function init_ebtables {
save runlocked $RUNLOCKED_OPTS ebtables -N $FROM -P RETURN
save runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -i $INTERFACE -j $FROM
# This is needed for multicast packets
save runlocked $RUNLOCKED_OPTS ebtables -A INPUT -i $INTERFACE -j $FROM
save runlocked $RUNLOCKED_OPTS ebtables -N $TO -P RETURN
save runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -o $INTERFACE -j $TO
# This is needed for multicast packets
save runlocked $RUNLOCKED_OPTS ebtables -A OUTPUT -o $INTERFACE -j $TO
}
function setup_ebtables {
# do not allow changes in ip-mac pair
if [ -n "$IP" ]; then
:; # runlocked $RUNLOCKED_OPTS ebtables -A $FROM --ip-source \! $IP -p ipv4 -j DROP
fi
save runlocked $RUNLOCKED_OPTS ebtables -A $FROM -s \! $MAC -j DROP
# accept dhcp responses from host (nfdhcpd)
# this is actually not needed because nfdhcpd opens a socket and binds is with
# tap interface so dhcp response does not go through bridge
# INDEV_MAC=$(cat /sys/class/net/$INDEV/address)
# runlocked $RUNLOCKED_OPTS ebtables -A $TO -s $INDEV_MAC -p ipv4 --ip-protocol=udp --ip-destination-port=68 -j ACCEPT
# allow only packets from the same mac prefix
save runlocked $RUNLOCKED_OPTS ebtables -A $TO -s \! $MAC/$MAC_MASK -j DROP
}
function setup_masq {
# allow packets from/to router (for masquerading)
# runlocked $RUNLOCKED_OPTS ebtables -A $TO -s $NODE_MAC -j ACCEPT
# runlocked $RUNLOCKED_OPTS ebtables -A INPUT -i $INTERFACE -j $FROM
# runlocked $RUNLOCKED_OPTS ebtables -A OUTPUT -o $INTERFACE -j $TO
return
}
function setup_nfdhcpd {
umask 022
FILE=$NFDHCPD_STATE_DIR/$INTERFACE
#IFACE is the interface from which the packet seems to arrive
#needed in bridged mode where the packets seems to arrive from the
#bridge and not from the tap
cat >$FILE <<EOF
INDEV=$INDEV
IP=$IP
MAC=$MAC
HOSTNAME=$INSTANCE
TAGS="$TAGS"
GATEWAY=$NETWORK_GATEWAY
SUBNET=$NETWORK_SUBNET
GATEWAY6=$NETWORK_GATEWAY6
SUBNET6=$NETWORK_SUBNET6
EUI64=$EUI64
EOF
}
function get_uplink {
local table=$1
UPLINK=$(ip route list table $table | grep "default via" | awk '{print $5}')
UPLINK6=$(ip -6 route list table $table | grep "default via" | awk '{print $5}')
if [ -n "$UPLINK" ]; then
log "* uplink($table) -> $UPLINK"
fi
if [ -n "$UPLINK6" ]; then
log "* uplink6($table) -> $UPLINK6"
fi
}
# Because we do not have IPv6 value in our environment
# we caclulate it based on the NIC's MAC and the IPv6 subnet (if any)
# first argument MAC second IPv6 subnet
# Changes global value EUI64
get_eui64 () {
local mac=$1
local prefix=$2
if [ -z "$prefix" ]; then
EUI64=
else
EUI64=$($MAC2EUI64 $mac $prefix)
log "* eui64($mac, $prefix) -> $EUI64"
fi
}
# DDNS related functions
# ommit zone statement
# nsupdate will attempt determine the correct zone to update based on the rest of the input
send_command () {
local command="$1"
log "* $command"
nsupdate -k $KEYFILE > /dev/null << EOF
server $SERVER
$command
send
EOF
}
update_arecord () {
local action=$1
local command=
if [ -n "$IP" ]; then
command="update $action $INSTANCE.$FZONE $TTL A $IP"
send_command "$command"
fi
}
update_aaaarecord () {
local action=$1
local command=
if [ -n "$EUI64" ]; then
command="update $action $INSTANCE.$FZONE $TTL AAAA $EUI64"
send_command "$command"
fi
}
update_ptrrecord () {
local action=$1
local command=
if [ -n "$IP" ]; then
command="update $action $RLPART.$RZONE. $TTL PTR $INSTANCE.$FZONE"
send_command "$command"
fi
}
update_ptr6record () {
local action=$1
local command=
if [ -n "$EUI64" ]; then
command="update $action $R6LPART$R6ZONE. $TTL PTR $INSTANCE.$FZONE"
send_command "$command"
fi
}
update_all () {
local action=$1
log "Update ($action) dns for $INSTANCE $IP $EUI64"
update_arecord $action
update_aaaarecord $action
update_ptrrecord $action
update_ptr6record $action
}
# first argument is an eui64 (IPv6)
# sets GLOBAL args R6REC, R6ZONE, R6LPART
# lets assume eui64=2001:648:2ffc:1::1
# the following commands produce:
# 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
# R6ZONE=1.0.0.0.c.f.f.2.8.4.6.0.1.0.0.2.ip6.arpa
# R6LPART=1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.
get_rev6_info () {
local eui64=$1
if [ -z "$eui64" ]; then
R6REC= ; R6ZONE= ; R6LPART= ;
else
R6REC=$(host $eui64 | egrep -o '([[:alnum:]]\.){32}ip6.arpa' )
R6ZONE=$(echo $R6REC | awk -F. 'BEGIN{rpart="";} { for (i=32;i>16;i=i-1) rpart=$i "." rpart; } END{print rpart "ip6.arpa";}')
R6LPART=$(echo $R6REC | awk -F. 'BEGIN{lpart="";} { for (i=16;i>0;i=i-1) lpart=$i "." lpart; } END{print lpart;}')
log "* rev6($eui64) -> $R6LPART, $R6ZONE"
fi
}
# first argument is an ipv4
# sets args RZONE, RLPART
# lets assume IP=203.0.113.1
# RZONE="113.0.203.in-add.arpa"
# RLPART="1"
get_rev4_info () {
local ip=$1
if [ -z "$ip" ]; then
RZONE= ; RLPART= ;
else
OLDIFS=$IFS
IFS=". "
set -- $ip
a=$1 ; b=$2; c=$3; d=$4;
IFS=$OLDIFS
RZONE="$c.$b.$a.in-addr.arpa"
RLPART="$d"
log "* rev4($ip) -> $RLPART, $RZONE"
fi
}
get_ebtables_chains () {
local iface=$1
FROM=FROM${iface^^}
TO=TO${iface^^}
# log "* ebtables($iface) -> $FROM, $TO"
}
get_mode_info () {
local mode=$1
local link=$2
local iface=$3
if [ "$mode" = "routed" ]; then
BRIDGE=
TABLE=$link
INDEV=$iface
elif [ "$mode" = "bridged" ]; then
BRIDGE=$link
TABLE=
INDEV=$link
fi
log "* $iface: $mode @ $link"
}
# Use environment variables to calculate desired info
# IP, MAC, LINK, TABLE, BRIDGE,
# NETWORK_SUBNET, NETWORK_GATEWAY, NETWORK_SUBNET6, NETWORK_GATEWAY6
# Note that INTERFACE is available only during ifup scripts
function get_info () {
get_mode_info $MODE $LINK $INTERFACE
get_ebtables_chains $INTERFACE
get_rev4_info $IP
get_eui64 $MAC $NETWORK_SUBNET6
get_rev6_info $EUI64
get_uplink $TABLE
}
# Query nameserver for entries related to the specific instance
# An example output is the following:
# www.google.com has address 173.194.113.114
# www.google.com has address 173.194.113.115
# www.google.com has address 173.194.113.116
# www.google.com has address 173.194.113.112
# www.google.com has address 173.194.113.113
# www.google.com has IPv6 address 2a00:1450:4001:80b::1012
query_dns () {
log "Query dns for $INSTANCE"
HOSTQ="host -s -R 3 -W 3"
HOST_IP_ALL=$($HOSTQ $INSTANCE.$FZONE $SERVER | sed -n 's/.*has address //p')
HOST_IP6_ALL=$($HOSTQ $INSTANCE.$FZONE $SERVER | sed -n 's/.*has IPv6 address //p')
log "* ip($INSTANCE) -> $HOST_IP_ALL"
log "* ip6($INSTANCE) -> $HOST_IP6_ALL"
}
# Reset all entries related to the specific instance
# This should be invoced only during instance modification
# because we do not know which nics have been modify
reset_dns () {
# This should remove the A, AAAA, CNAME entries
log "Reset dns for $INSTANCE"
send_command "update delete $INSTANCE.$FZONE"
for ip in $HOST_IP_ALL; do
get_rev4_info $ip
# This should remove the IPv4 reverse entry
send_command "update delete $RLPART.$RZONE"
done
for ip6 in $HOST_IP6_ALL; do
get_rev6_info $ip6
# This should remove the IPv6 reverse entry
send_command "update delete $R6LPART$R6ZONE."
done
}