root / kvm-vif-bridge @ 7d163a24
History | View | Annotate | Download (4.5 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 clear_tap { |
11 |
|
12 |
arptables -D OUTPUT -o $INTERFACE --opcode request -j mangle >/dev/null 2>&1 |
13 |
while ip rule del dev $INTERFACE; do :; done |
14 |
iptables -D FORWARD -i $INTERFACE -p udp --dport 67 -j DROP 2>/dev/null |
15 |
|
16 |
|
17 |
} |
18 |
|
19 |
function routed_setup_ipv4 { |
20 |
# get the link's default gateway |
21 |
gw=$(ip route list table $TABLE | sed -n 's/default via \([^ ]\+\).*/\1/p' | head -1) |
22 |
|
23 |
# mangle ARPs to come from the gw's IP |
24 |
arptables -A OUTPUT -o $INTERFACE --opcode request -j mangle --mangle-ip-s "$gw" |
25 |
|
26 |
# route interface to the proper routing table |
27 |
ip rule add dev $INTERFACE table $TABLE |
28 |
|
29 |
# static route mapping IP -> INTERFACE |
30 |
ip route replace $IP proto static dev $INTERFACE table $TABLE |
31 |
|
32 |
# Enable proxy ARP |
33 |
echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp |
34 |
} |
35 |
|
36 |
function routed_setup_ipv6 { |
37 |
# Add a routing entry for the eui-64 |
38 |
prefix=$(ip -6 route list table $TABLE | awk '/\/64/ {print $1; exit}') |
39 |
uplink=$(ip -6 route list table $TABLE | sed -n 's/default via .* dev \([^ ]\+\).*/\1/p' | head -1) |
40 |
eui64=$($MAC2EUI64 $MAC $prefix) |
41 |
|
42 |
while ip -6 rule del dev $INTERFACE; do :; done |
43 |
ip -6 rule add dev $INTERFACE table $TABLE |
44 |
ip -6 ro replace $eui64/128 dev $INTERFACE table $TABLE |
45 |
ip -6 neigh add proxy $eui64 dev $uplink |
46 |
|
47 |
# disable proxy NDP since we're handling this on userspace |
48 |
# this should be the default, but better safe than sorry |
49 |
echo 0 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp |
50 |
} |
51 |
|
52 |
# pick a firewall profile per NIC, based on tags (and apply it) |
53 |
function routed_setup_firewall { |
54 |
ifprefix="synnefo:network:$INTERFACE_INDEX:" |
55 |
for tag in $TAGS; do |
56 |
case ${tag#$ifprefix} in |
57 |
protected) |
58 |
chain=protected |
59 |
;; |
60 |
unprotected) |
61 |
chain=unprotected |
62 |
;; |
63 |
limited) |
64 |
chain=limited |
65 |
;; |
66 |
esac |
67 |
done |
68 |
|
69 |
# Flush any old rules. We have to consider all chains, since |
70 |
# we are not sure the instance was on the same chain, or had the same |
71 |
# tap interface. |
72 |
for oldchain in protected unprotected limited; do |
73 |
iptables -D FORWARD -o $INTERFACE -j $oldchain 2>/dev/null |
74 |
ip6tables -D FORWARD -o $INTERFACE -j $oldchain 2>/dev/null |
75 |
done |
76 |
|
77 |
if [ "x$chain" != "x" ]; then |
78 |
iptables -A FORWARD -o $INTERFACE -j $chain |
79 |
ip6tables -A FORWARD -o $INTERFACE -j $chain |
80 |
fi |
81 |
} |
82 |
|
83 |
function setup_nfdhcpd { |
84 |
umask 022 |
85 |
cat >$NFDHCPD_STATE_DIR/$INTERFACE <<EOF |
86 |
IFACE=$1 |
87 |
IP=$IP |
88 |
MAC=$MAC |
89 |
LINK=$TABLE |
90 |
HOSTNAME=$INSTANCE |
91 |
TAGS="$TAGS" |
92 |
EOF |
93 |
} |
94 |
|
95 |
function clear_ebtables { |
96 |
TAP=$INTERFACE |
97 |
FROM=FROM${TAP^^} |
98 |
TO=TO${TAP^^} |
99 |
|
100 |
exist=$(ebtables -L | grep $TAP) |
101 |
|
102 |
if [ ! -z "$exist" ]; then |
103 |
ebtables -D INPUT -i $TAP -j $FROM |
104 |
ebtables -D FORWARD -i $TAP -j $FROM |
105 |
ebtables -D FORWARD -o $TAP -j $TO |
106 |
ebtables -D OUTPUT -o $TAP -j $TO |
107 |
|
108 |
ebtables -X $FROM |
109 |
ebtables -X $TO |
110 |
fi |
111 |
} |
112 |
|
113 |
function setup_ebtables { |
114 |
TAP=$INTERFACE |
115 |
FROM=FROM${TAP^^} |
116 |
TO=TO${TAP^^} |
117 |
|
118 |
ebtables -N $FROM |
119 |
# do not allow changes in ip-mac pair |
120 |
ebtables -A $FROM --ip-source \! $IP -p ipv4 -j DROP |
121 |
ebtables -A $FROM -s \! $MAC -j DROP |
122 |
ebtables -A FORWARD -i $TAP -j $FROM |
123 |
ebtables -N $TO |
124 |
ebtables -A FORWARD -o $TAP -j $TO |
125 |
#accept dhcp responses from host (nfdhcpd) |
126 |
ebtables -A $TO -p ipv4 --ip-protocol=udp --ip-destination-port=68 -j ACCEPT |
127 |
if [ $TYPE == "private" ]; then |
128 |
if [ ! -z $GATEWAY ]; then |
129 |
# allow packets from/to router (for masquerading |
130 |
ebtables -A $TO -s $ROUTER_MAC -j ACCEPT |
131 |
ebtables -A INPUT -i $TAP -j $FROM |
132 |
ebtables -A OUTPUT -o $TAP -j $TO |
133 |
fi |
134 |
# allow only packets from the same mac prefix |
135 |
ebtables -A $TO -s \! $MAC/$MAC_MASK -j DROP |
136 |
fi |
137 |
} |
138 |
|
139 |
#FIXME: import router mac from the config files |
140 |
# must know node group!! how??? |
141 |
ROUTER_MAC=e4:11:5b:b2:8d:ca |
142 |
MAC_MASK=ff:ff:ff:0:0:0 |
143 |
|
144 |
TABLE=rt_$NETWORK |
145 |
|
146 |
source /var/lib/snf-network/networks/$NETWORK |
147 |
|
148 |
|
149 |
if [ "$MODE" = "routed" ]; then |
150 |
# special proxy-ARP/NDP routing mode |
151 |
clear_tap |
152 |
# use a constant predefined MAC address for the tap |
153 |
ip link set $INTERFACE addr $TAP_CONSTANT_MAC |
154 |
# bring the tap up |
155 |
ifconfig $INTERFACE 0.0.0.0 up |
156 |
|
157 |
# Drop unicast BOOTP/DHCP packets |
158 |
iptables -A FORWARD -i $INTERFACE -p udp --dport 67 -j DROP |
159 |
|
160 |
routed_setup_ipv4 |
161 |
# routed_setup_ipv6 |
162 |
# routed_setup_firewall |
163 |
setup_nfdhcpd $INTERFACE |
164 |
clear_ebtables >/dev/null 2>&1 |
165 |
elif [ "$MODE" = "bridged" ]; then |
166 |
clear_tap |
167 |
clear_ebtables >/dev/null 2>&1 |
168 |
ifconfig $INTERFACE 0.0.0.0 up |
169 |
brctl addif $BRIDGE $INTERFACE |
170 |
setup_nfdhcpd $BRIDGE |
171 |
setup_ebtables |
172 |
fi |