root / snf-cyclades-gtools / kvm-vif-bridge @ b47b895d
History | View | Annotate | Download (3.1 kB)
1 |
#!/bin/bash |
---|---|
2 |
|
3 |
# This is an example of a Ganeti kvm ifup script that configures network |
4 |
# interfaces based on the initial deployment of the Okeanos project |
5 |
|
6 |
TAP_CONSTANT_MAC=cc:47:52:4e:45:54 # GRNET in hex :-) |
7 |
MAC2EUI64=/usr/bin/mac2eui64 |
8 |
NFDHCPD_STATE_DIR=/var/lib/nfdhcpd |
9 |
|
10 |
function routed_setup_ipv4 { |
11 |
# get the link's default gateway |
12 |
gw=$(ip route list table $LINK | sed -n 's/default via \([^ ]\+\).*/\1/p' | head -1) |
13 |
|
14 |
# mangle ARPs to come from the gw's IP |
15 |
arptables -D OUTPUT -o $INTERFACE --opcode request -j mangle >/dev/null 2>&1 |
16 |
arptables -A OUTPUT -o $INTERFACE --opcode request -j mangle --mangle-ip-s "$gw" |
17 |
|
18 |
# route interface to the proper routing table |
19 |
while ip rule del dev $INTERFACE; do :; done |
20 |
ip rule add dev $INTERFACE table $LINK |
21 |
|
22 |
# static route mapping IP -> INTERFACE |
23 |
ip route replace $IP table $LINK proto static dev $INTERFACE |
24 |
|
25 |
# Enable proxy ARP |
26 |
echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp |
27 |
} |
28 |
|
29 |
function routed_setup_ipv6 { |
30 |
# Add a routing entry for the eui-64 |
31 |
prefix=$(ip -6 route list table $LINK | awk '/\/64/ {print $1; exit}') |
32 |
uplink=$(ip -6 route list table $LINK | sed -n 's/default via .* dev \([^ ]\+\).*/\1/p' | head -1) |
33 |
eui64=$($MAC2EUI64 $MAC $prefix) |
34 |
|
35 |
while ip -6 rule del dev $INTERFACE; do :; done |
36 |
ip -6 rule add dev $INTERFACE table $LINK |
37 |
ip -6 ro replace $eui64/128 dev $INTERFACE table $LINK |
38 |
ip -6 neigh add proxy $eui64 dev $uplink |
39 |
|
40 |
# disable proxy NDP since we're handling this on userspace |
41 |
# this should be the default, but better safe than sorry |
42 |
echo 0 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp |
43 |
} |
44 |
|
45 |
# pick a firewall profile per NIC, based on tags (and apply it) |
46 |
function routed_setup_firewall { |
47 |
ifprefix="synnefo:network:$INTERFACE_INDEX:" |
48 |
for tag in $TAGS; do |
49 |
case ${tag#$ifprefix} in |
50 |
protected) |
51 |
chain=protected |
52 |
;; |
53 |
unprotected) |
54 |
chain=unprotected |
55 |
;; |
56 |
limited) |
57 |
chain=limited |
58 |
;; |
59 |
esac |
60 |
done |
61 |
|
62 |
# Flush any old rules. We have to consider all chains, since |
63 |
# we are not sure the instance was on the same chain, or had the same |
64 |
# tap interface. |
65 |
for oldchain in protected unprotected limited; do |
66 |
iptables -D FORWARD -o $INTERFACE -j $oldchain 2>/dev/null |
67 |
ip6tables -D FORWARD -o $INTERFACE -j $oldchain 2>/dev/null |
68 |
done |
69 |
|
70 |
if [ "x$chain" != "x" ]; then |
71 |
iptables -A FORWARD -o $INTERFACE -j $chain |
72 |
ip6tables -A FORWARD -o $INTERFACE -j $chain |
73 |
fi |
74 |
} |
75 |
|
76 |
function routed_setup_nfdhcpd { |
77 |
umask 022 |
78 |
cat >$NFDHCPD_STATE_DIR/$INTERFACE <<EOF |
79 |
IP=$IP |
80 |
MAC=$MAC |
81 |
LINK=$LINK |
82 |
HOSTNAME=$INSTANCE |
83 |
TAGS="$TAGS" |
84 |
EOF |
85 |
} |
86 |
|
87 |
if [ "$MODE" = "routed" ]; then |
88 |
# special proxy-ARP/NDP routing mode |
89 |
|
90 |
# use a constant predefined MAC address for the tap |
91 |
ip link set $INTERFACE addr $TAP_CONSTANT_MAC |
92 |
# bring the tap up |
93 |
ifconfig $INTERFACE 0.0.0.0 up |
94 |
|
95 |
# Drop unicast BOOTP/DHCP packets |
96 |
iptables -D FORWARD -i $INTERFACE -p udp --dport 67 -j DROP 2>/dev/null |
97 |
iptables -A FORWARD -i $INTERFACE -p udp --dport 67 -j DROP |
98 |
|
99 |
routed_setup_ipv4 |
100 |
routed_setup_ipv6 |
101 |
routed_setup_firewall |
102 |
routed_setup_nfdhcpd |
103 |
elif [ "$MODE" = "bridged" ]; then |
104 |
ifconfig $INTERFACE 0.0.0.0 up |
105 |
brctl addif $BRIDGE $INTERFACE |
106 |
rm -f $NFDHCPD_STATE_DIR/$INTERFACE |
107 |
fi |