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