Modify kvm-vif-bridge to support network tags
[snf-network] / kvm-vif-bridge
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_routed_setup_ipv4 {
11
12  arptables -D OUTPUT -o $INTERFACE --opcode request -j mangle
13  while ip rule del dev $INTERFACE; do :; done
14  iptables -D FORWARD -i $INTERFACE -p udp --dport 67 -j DROP
15
16 }
17
18 function clear_routed_setup_ipv6 {
19
20  while ip -6 rule del dev $INTERFACE; do :; done
21
22 }
23
24
25 function clear_routed_setup_firewall {
26
27   for oldchain in protected unprotected limited; do
28     iptables  -D FORWARD -o $INTERFACE -j $oldchain
29     ip6tables -D FORWARD -o $INTERFACE -j $oldchain
30   done
31
32 }
33
34 function clear_ebtables {
35   TAP=$INTERFACE
36   FROM=FROM${TAP^^}
37   TO=TO${TAP^^}
38
39   ebtables -D INPUT -i $TAP -j $FROM
40   ebtables -D FORWARD -i $TAP -j $FROM
41   ebtables -D FORWARD -o $TAP -j $TO
42   ebtables -D OUTPUT -o $TAP -j $TO
43
44   ebtables -X $FROM
45   ebtables -X $TO
46 }
47
48
49
50 function routed_setup_ipv4 {
51
52         # mangle ARPs to come from the gw's IP
53         arptables -A OUTPUT -o $INTERFACE --opcode request -j mangle --mangle-ip-s    "$NETWORK_GATEWAY"
54
55         # route interface to the proper routing table
56         ip rule add dev $INTERFACE table $TABLE
57
58         # static route mapping IP -> INTERFACE
59         ip route replace $IP proto static dev $INTERFACE table $TABLE
60
61         # Enable proxy ARP
62         echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp
63 }
64
65 function routed_setup_ipv6 {
66         # Add a routing entry for the eui-64
67         prefix=$NETWORK_SUBNET6
68         uplink=$PUBLIC_VLAN
69         eui64=$($MAC2EUI64 $MAC $prefix)
70
71
72         ip -6 rule add dev $INTERFACE table $TABLE
73         ip -6 ro replace $eui64/128 dev $INTERFACE table $TABLE
74         ip -6 neigh add proxy $eui64 dev $uplink
75
76         # disable proxy NDP since we're handling this on userspace
77         # this should be the default, but better safe than sorry
78         echo 0 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp
79 }
80
81 # pick a firewall profile per NIC, based on tags (and apply it)
82 function routed_setup_firewall {
83         ifprefix="synnefo:network:$INTERFACE_INDEX:"
84         for tag in $TAGS; do
85                 case ${tag#$ifprefix} in
86                 protected)
87                         chain=protected
88                 ;;
89                 unprotected)
90                         chain=unprotected
91                 ;;
92                 limited)
93                         chain=limited
94                 ;;
95                 esac
96         done
97
98         if [ "x$chain" != "x" ]; then
99                 iptables  -A FORWARD -o $INTERFACE -j $chain
100                 ip6tables -A FORWARD -o $INTERFACE -j $chain
101         fi
102 }
103
104 function setup_ebtables {
105   TAP=$INTERFACE
106   FROM=FROM${TAP^^}
107   TO=TO${TAP^^}
108
109   ebtables -N $FROM
110   # do not allow changes in ip-mac pair
111   if [ -n "$IP"]; then
112     ebtables -A $FROM --ip-source \! $IP -p ipv4 -j DROP
113   fi
114   ebtables -A $FROM -s \! $MAC -j DROP
115   ebtables -A FORWARD -i $TAP -j $FROM
116   ebtables -N $TO
117   ebtables -A FORWARD -o $TAP -j $TO
118   #accept dhcp responses from host (nfdhcpd)
119   ebtables -A $TO -p ipv4 --ip-protocol=udp  --ip-destination-port=68 -j ACCEPT
120   # allow only packets from the same mac prefix
121   ebtables -A $TO -s \! $MAC/$MAC_MASK -j DROP
122 }
123
124 function setup_masq {
125   TAP=$INTERFACE
126   FROM=FROM${TAP^^}
127   TO=TO${TAP^^}
128
129   # allow packets from/to router (for masquerading)
130   ebtables -A $TO -s $PUBLIC_MAC -j ACCEPT
131   ebtables -A INPUT -i $TAP -j $FROM
132   ebtables -A OUTPUT -o $TAP -j $TO
133 }
134
135 function setup_nfdhcpd {
136         umask 022
137   FILE=$NFDHCPD_STATE_DIR/$INTERFACE
138   #IFACE is the interface from which the packet seems to arrive
139   #needed in bridged mode where the packets seems to arrive from the
140   #bridge and not from the tap
141         cat >$FILE <<EOF
142 INDEV=$INDEV
143 IP=$IP
144 MAC=$MAC
145 HOSTNAME=$INSTANCE
146 TAGS="$TAGS"
147 GATEWAY=$NETWORK_GATEWAY
148 SUBNET=$NETWORK_SUBNET
149 GATEWAY6=$NETWORK_GATEWAY6
150 SUBNET6=$NETWORK_SUBNET6
151 EUI64=$($MAC2EUI64 $MAC $NETWORK_SUBNET6 2>/dev/null)
152 EOF
153
154 }
155
156
157 DEFAULT=/etc/default/snf-network
158 source $DEFAULT
159 source $CONF
160
161 INFRA=$STATE_DIR/infra
162
163 source $INFRA
164
165 log-env
166
167 clear_routed_setup_ipv4 > /dev/null 2>&1
168 clear_routed_setup_ipv6 > /dev/null 2>&1
169 clear_routed_setup_firewall > /dev/null 2>&1
170 clear_ebtables > /dev/null 2>&1
171
172 if [ "$MODE" = "routed" ]; then
173   TABLE=$LINK
174   ip link set $INTERFACE addr $TAP_CONSTANT_MAC up
175   INDEV=$INTERFACE
176 elif [ "$MODE" = "bridged" ]; then
177   ip link set $INTERFACE up
178   brctl addif $BRIDGE $INTERFACE
179   INDEV=$BRIDGE
180 fi
181
182
183 for tag in $NETWORK_TAGS; do
184   case $tag in
185   ip-less-routed)
186     routed_setup_ipv4 > /dev/null 2>&1
187     routed_setup_ipv6 > /dev/null 2>&1
188     routed_setup_firewall > /dev/null 2>&1
189   ;;
190   nfdhcpd)
191     # Drop unicast BOOTP/DHCP packets
192     iptables -A FORWARD -i $INTERFACE -p udp --dport 67 -j DROP
193     setup_nfdhcpd > /dev/null 2>&1
194   ;;
195   mac-filtered)
196     setup_ebtables > /dev/null 2>&1
197   ;;
198   masq)
199     setup_masq > /dev/null 2>&1
200   ;;
201   esac
202 done
203