Merge branch 'develop'
[snf-network] / common.sh
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         # mangle ARPs to come from the gw's IP
54         arptables -A OUTPUT -o $INTERFACE --opcode request -j mangle --mangle-ip-s    "$NETWORK_GATEWAY"
55
56         # route interface to the proper routing table
57         ip rule add dev $INTERFACE table $TABLE
58
59         # static route mapping IP -> INTERFACE
60         ip route replace $IP proto static dev $INTERFACE table $TABLE
61
62         # Enable proxy ARP
63         echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp
64 }
65
66 function routed_setup_ipv6 {
67         # Add a routing entry for the eui-64
68         prefix=$NETWORK_SUBNET6
69         uplink=$(ip -6 route list table $TABLE | grep "default via" | awk '{print $5}')
70         eui64=$($MAC2EUI64 $MAC $prefix)
71
72
73         ip -6 rule add dev $INTERFACE table $TABLE
74         ip -6 ro replace $eui64/128 dev $INTERFACE table $TABLE
75         ip -6 neigh add proxy $eui64 dev $uplink
76
77         # disable proxy NDP since we're handling this on userspace
78         # this should be the default, but better safe than sorry
79         echo 0 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp
80 }
81
82 # pick a firewall profile per NIC, based on tags (and apply it)
83 function routed_setup_firewall {
84         # for latest ganeti there is no need to check other but uuid
85         ifprefixindex="synnefo:network:$INTERFACE_INDEX:"
86         ifprefixname="synnefo:network:$INTERFACE_NAME:"
87         ifprefixuuid="synnefo:network:$INTERFACE_UUID:"
88         for tag in $TAGS; do
89                 tag=${tag#$ifprefixindex}
90                 tag=${tag#$ifprefixname}
91                 tag=${tag#$ifprefixuuid}
92                 case $tag in
93                 protected)
94                         chain=protected
95                 ;;
96                 unprotected)
97                         chain=unprotected
98                 ;;
99                 limited)
100                         chain=limited
101                 ;;
102                 esac
103         done
104
105         if [ "x$chain" != "x" ]; then
106                 iptables  -A FORWARD -o $INTERFACE -j $chain
107                 ip6tables -A FORWARD -o $INTERFACE -j $chain
108         fi
109 }
110
111 function init_ebtables {
112
113   runlocked $RUNLOCKED_OPTS ebtables -N $FROM
114   runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -i $INTERFACE -j $FROM
115   runlocked $RUNLOCKED_OPTS ebtables -N $TO
116   runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -o $INTERFACE -j $TO
117
118 }
119
120
121 function setup_ebtables {
122
123   # do not allow changes in ip-mac pair
124   if [ -n "$IP"]; then
125     runlocked $RUNLOCKED_OPTS ebtables -A $FROM --ip-source \! $IP -p ipv4 -j DROP
126   fi
127   runlocked $RUNLOCKED_OPTS ebtables -A $FROM -s \! $MAC -j DROP
128   #accept dhcp responses from host (nfdhcpd)
129   runlocked $RUNLOCKED_OPTS ebtables -A $TO -p ipv4 --ip-protocol=udp  --ip-destination-port=68 -j ACCEPT
130   # allow only packets from the same mac prefix
131   runlocked $RUNLOCKED_OPTS ebtables -A $TO -s \! $MAC/$MAC_MASK -j DROP
132 }
133
134 function setup_masq {
135
136   # allow packets from/to router (for masquerading)
137   # runlocked $RUNLOCKED_OPTS ebtables -A $TO -s $NODE_MAC -j ACCEPT
138   # runlocked $RUNLOCKED_OPTS ebtables -A INPUT -i $INTERFACE -j $FROM
139   # runlocked $RUNLOCKED_OPTS ebtables -A OUTPUT -o $INTERFACE -j $TO
140   return
141
142 }
143
144 function setup_nfdhcpd {
145         umask 022
146   FILE=$NFDHCPD_STATE_DIR/$INTERFACE
147   #IFACE is the interface from which the packet seems to arrive
148   #needed in bridged mode where the packets seems to arrive from the
149   #bridge and not from the tap
150         cat >$FILE <<EOF
151 INDEV=$INDEV
152 IP=$IP
153 MAC=$MAC
154 HOSTNAME=$INSTANCE
155 TAGS="$TAGS"
156 GATEWAY=$NETWORK_GATEWAY
157 SUBNET=$NETWORK_SUBNET
158 GATEWAY6=$NETWORK_GATEWAY6
159 SUBNET6=$NETWORK_SUBNET6
160 EUI64=$($MAC2EUI64 $MAC $NETWORK_SUBNET6 2>/dev/null)
161 EOF
162
163 }
164