e6e54f48dc2b1b3c988ff5ce28ca414297a44543
[rusty/ctdb.git] / config / events.d / 11.natgw
1 #!/bin/sh
2 # Script to set up one of the nodes as a NAT gateway for all other nodes.
3 # This is used to ensure that all nodes in the cluster can still originate
4 # traffic to the external network even if there are no public addresses
5 # available.
6 #
7
8 . $CTDB_BASE/functions
9 loadconfig
10
11 [ -z "$CTDB_NATGW_PUBLIC_IFACE" ] && exit 0
12
13 delete_all() {
14         local _ip=`echo $CTDB_NATGW_PUBLIC_IP | cut -d '/' -f1`
15         local _maskbits=`echo $CTDB_NATGW_PUBLIC_IP | cut -d '/' -f2`
16
17         delete_ip_from_iface $CTDB_NATGW_PUBLIC_IFACE $_ip $_maskbits
18         delete_ip_from_iface lo $_ip 32
19
20         ip route del 0.0.0.0/0 metric 10 >/dev/null 2>/dev/null
21
22         # Delete the masquerading setup from a previous iteration where we
23         # were the NAT-GW
24         iptables -D POSTROUTING -t nat -s $CTDB_NATGW_PRIVATE_NETWORK -d ! $CTDB_NATGW_PRIVATE_NETWORK -j MASQUERADE >/dev/null 2>/dev/null
25
26 }
27
28 case "$1" in 
29     startup)
30         [ -z "$CTDB_PUBLIC_ADDRESSES" ] && {
31                 CTDB_PUBLIC_ADDRESSES=/etc/ctdb/public_addresses
32         }
33         egrep "^$CTDB_NATGW_PUBLIC_IP[ \t]" $CTDB_PUBLIC_ADDRESSES >/dev/null
34         [ "$?" = "0" ] && {
35                 echo ERROR: NATGW configured to use a public address. NATGW must not use a public address.
36                 exit 1
37         }
38
39         # do not respond to ARPs that are for ip addresses with scope 'host'
40         echo 3 > /proc/sys/net/ipv4/conf/all/arp_ignore
41         # do not send out arp requests from loopback addresses
42         echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
43         # update capabilities to show we are using natgw
44         ctdb setnatgwstate on
45         ;;
46
47     recovered|updatenatgw)
48         MYPNN=`ctdb pnn | cut -d: -f2`
49         NATGWMASTER=`ctdb natgwlist | head -1 | sed -e "s/ .*//"`
50         NATGWIP=`ctdb natgwlist | head -1 | sed -e "s/^[^ ]* *//"`
51
52         CTDB_NATGW_PUBLIC_IP_HOST=`echo $CTDB_NATGW_PUBLIC_IP | sed -e "s/\/.*/\/32/"`
53         if [ "$NATGWMASTER" = "-1" ]; then
54                 echo "There is not NATGW master node"
55                 exit 1
56         fi
57
58         delete_all
59
60         if [ "$MYPNN" = "$NATGWMASTER" ]; then
61                 # This is the first node, set it up as the NAT GW
62                 echo 1 >/proc/sys/net/ipv4/ip_forward
63                 iptables -A POSTROUTING -t nat -s $CTDB_NATGW_PRIVATE_NETWORK -d ! $CTDB_NATGW_PRIVATE_NETWORK -j MASQUERADE
64                 ip addr add $CTDB_NATGW_PUBLIC_IP dev $CTDB_NATGW_PUBLIC_IFACE
65                 ip route add 0.0.0.0/0 via $CTDB_NATGW_DEFAULT_GATEWAY >/dev/null 2>/dev/null
66         else
67                 # This is not the NAT-GW
68                 # Assign the public ip to the private interface and make
69                 # sure we dont respond to ARPs.
70                 # We do this so that the ip address will exist on a
71                 # non-loopback interface so that samba may send it along in the
72                 # KDC requests.
73                 ip addr add $CTDB_NATGW_PUBLIC_IP_HOST dev lo scope host
74                 ip route add 0.0.0.0/0 via $NATGWIP metric 10
75         fi
76
77         # flush our route cache
78         echo 1 > /proc/sys/net/ipv4/route/flush
79         ;;
80
81     shutdown|stopped|removenatgw)
82         delete_all
83         ;;
84
85     *)
86         ctdb_standard_event_handler "@"
87         ;;
88 esac
89
90 exit 0