root / common.sh @ 0363b080
History | View | Annotate | Download (4.3 kB)
1 |
#!/bin/bash |
---|---|
2 |
|
3 |
function try { |
4 |
|
5 |
$1 &>/dev/null || true |
6 |
|
7 |
} |
8 |
|
9 |
function clear_routed_setup_ipv4 { |
10 |
|
11 |
arptables -D OUTPUT -o $INTERFACE --opcode request -j mangle |
12 |
while ip rule del dev $INTERFACE; do :; done |
13 |
iptables -D FORWARD -i $INTERFACE -p udp --dport 67 -j DROP |
14 |
|
15 |
} |
16 |
|
17 |
function clear_routed_setup_ipv6 { |
18 |
|
19 |
while ip -6 rule del dev $INTERFACE; do :; done |
20 |
|
21 |
} |
22 |
|
23 |
|
24 |
function clear_routed_setup_firewall { |
25 |
|
26 |
for oldchain in protected unprotected limited; do |
27 |
iptables -D FORWARD -o $INTERFACE -j $oldchain |
28 |
ip6tables -D FORWARD -o $INTERFACE -j $oldchain |
29 |
done |
30 |
|
31 |
} |
32 |
|
33 |
function clear_ebtables { |
34 |
|
35 |
runlocked $RUNLOCKED_OPTS ebtables -D FORWARD -i $INTERFACE -j $FROM |
36 |
runlocked $RUNLOCKED_OPTS ebtables -D FORWARD -o $INTERFACE -j $TO |
37 |
#runlocked $RUNLOCKED_OPTS ebtables -D OUTPUT -o $INTERFACE -j $TO |
38 |
|
39 |
runlocked $RUNLOCKED_OPTS ebtables -X $FROM |
40 |
runlocked $RUNLOCKED_OPTS ebtables -X $TO |
41 |
} |
42 |
|
43 |
|
44 |
function clear_nfdhcpd { |
45 |
|
46 |
rm $NFDHCPD_STATE_DIR/$INTERFACE |
47 |
|
48 |
} |
49 |
|
50 |
|
51 |
function routed_setup_ipv4 { |
52 |
|
53 |
if [ -z "$INTERFACE" -o -z "$NETWORK_GATEWAY" -o -z "$IP" -o -z "$TABLE" ] |
54 |
then |
55 |
return |
56 |
fi |
57 |
|
58 |
# mangle ARPs to come from the gw's IP |
59 |
arptables -A OUTPUT -o $INTERFACE --opcode request -j mangle --mangle-ip-s "$NETWORK_GATEWAY" |
60 |
|
61 |
# route interface to the proper routing table |
62 |
ip rule add dev $INTERFACE table $TABLE |
63 |
|
64 |
# static route mapping IP -> INTERFACE |
65 |
ip route replace $IP proto static dev $INTERFACE table $TABLE |
66 |
|
67 |
# Enable proxy ARP |
68 |
echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp |
69 |
} |
70 |
|
71 |
function routed_setup_ipv6 { |
72 |
# Add a routing entry for the eui-64 |
73 |
prefix=$NETWORK_SUBNET6 |
74 |
uplink=$(ip -6 route list table $TABLE | grep "default via" | awk '{print $5}') |
75 |
eui64=$($MAC2EUI64 $MAC $prefix) |
76 |
|
77 |
if [ -z "$eui64" -o -z "$TABLE" -o -z "$INTERFACE" -o -z "$uplink" ] |
78 |
then |
79 |
return |
80 |
fi |
81 |
|
82 |
ip -6 rule add dev $INTERFACE table $TABLE |
83 |
ip -6 ro replace $eui64/128 dev $INTERFACE table $TABLE |
84 |
ip -6 neigh add proxy $eui64 dev $uplink |
85 |
|
86 |
# disable proxy NDP since we're handling this on userspace |
87 |
# this should be the default, but better safe than sorry |
88 |
echo 0 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp |
89 |
} |
90 |
|
91 |
# pick a firewall profile per NIC, based on tags (and apply it) |
92 |
function routed_setup_firewall { |
93 |
# for latest ganeti there is no need to check other but uuid |
94 |
ifprefixindex="synnefo:network:$INTERFACE_INDEX:" |
95 |
ifprefixname="synnefo:network:$INTERFACE_NAME:" |
96 |
ifprefixuuid="synnefo:network:$INTERFACE_UUID:" |
97 |
for tag in $TAGS; do |
98 |
tag=${tag#$ifprefixindex} |
99 |
tag=${tag#$ifprefixname} |
100 |
tag=${tag#$ifprefixuuid} |
101 |
case $tag in |
102 |
protected) |
103 |
chain=protected |
104 |
;; |
105 |
unprotected) |
106 |
chain=unprotected |
107 |
;; |
108 |
limited) |
109 |
chain=limited |
110 |
;; |
111 |
esac |
112 |
done |
113 |
|
114 |
if [ "x$chain" != "x" ]; then |
115 |
iptables -A FORWARD -o $INTERFACE -j $chain |
116 |
ip6tables -A FORWARD -o $INTERFACE -j $chain |
117 |
fi |
118 |
} |
119 |
|
120 |
function init_ebtables { |
121 |
|
122 |
runlocked $RUNLOCKED_OPTS ebtables -N $FROM |
123 |
runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -i $INTERFACE -j $FROM |
124 |
runlocked $RUNLOCKED_OPTS ebtables -N $TO |
125 |
runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -o $INTERFACE -j $TO |
126 |
|
127 |
} |
128 |
|
129 |
|
130 |
function setup_ebtables { |
131 |
|
132 |
# do not allow changes in ip-mac pair |
133 |
if [ -n "$IP"]; then |
134 |
runlocked $RUNLOCKED_OPTS ebtables -A $FROM --ip-source \! $IP -p ipv4 -j DROP |
135 |
fi |
136 |
runlocked $RUNLOCKED_OPTS ebtables -A $FROM -s \! $MAC -j DROP |
137 |
#accept dhcp responses from host (nfdhcpd) |
138 |
runlocked $RUNLOCKED_OPTS ebtables -A $TO -p ipv4 --ip-protocol=udp --ip-destination-port=68 -j ACCEPT |
139 |
# allow only packets from the same mac prefix |
140 |
runlocked $RUNLOCKED_OPTS ebtables -A $TO -s \! $MAC/$MAC_MASK -j DROP |
141 |
} |
142 |
|
143 |
function setup_masq { |
144 |
|
145 |
# allow packets from/to router (for masquerading) |
146 |
# runlocked $RUNLOCKED_OPTS ebtables -A $TO -s $NODE_MAC -j ACCEPT |
147 |
# runlocked $RUNLOCKED_OPTS ebtables -A INPUT -i $INTERFACE -j $FROM |
148 |
# runlocked $RUNLOCKED_OPTS ebtables -A OUTPUT -o $INTERFACE -j $TO |
149 |
return |
150 |
|
151 |
} |
152 |
|
153 |
function setup_nfdhcpd { |
154 |
umask 022 |
155 |
FILE=$NFDHCPD_STATE_DIR/$INTERFACE |
156 |
#IFACE is the interface from which the packet seems to arrive |
157 |
#needed in bridged mode where the packets seems to arrive from the |
158 |
#bridge and not from the tap |
159 |
cat >$FILE <<EOF |
160 |
INDEV=$INDEV |
161 |
IP=$IP |
162 |
MAC=$MAC |
163 |
HOSTNAME=$INSTANCE |
164 |
TAGS="$TAGS" |
165 |
GATEWAY=$NETWORK_GATEWAY |
166 |
SUBNET=$NETWORK_SUBNET |
167 |
GATEWAY6=$NETWORK_GATEWAY6 |
168 |
SUBNET6=$NETWORK_SUBNET6 |
169 |
EUI64=$($MAC2EUI64 $MAC $NETWORK_SUBNET6 2>/dev/null) |
170 |
EOF |
171 |
|
172 |
} |
173 |
|