Merge commit 'rusty/ports-from-1.0.112' into foo
[sahlberg/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         # remove any iptables rule we may have on this address
27         iptables -D INPUT -p tcp --syn -d _ip/32 -j REJECT 2>/dev/null
28 }
29
30 case "$1" in 
31     startup)
32         [ -z "$CTDB_PUBLIC_ADDRESSES" ] && {
33                 CTDB_PUBLIC_ADDRESSES=/etc/ctdb/public_addresses
34         }
35         egrep "^$CTDB_NATGW_PUBLIC_IP[ \t]" $CTDB_PUBLIC_ADDRESSES >/dev/null
36         [ "$?" = "0" ] && {
37                 echo ERROR: NATGW configured to use a public address. NATGW must not use a public address.
38                 exit 1
39         }
40
41         # do not respond to ARPs that are for ip addresses with scope 'host'
42         echo 3 > /proc/sys/net/ipv4/conf/all/arp_ignore
43         # do not send out arp requests from loopback addresses
44         echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
45         # update capabilities to show we are using natgw
46         ctdb setnatgwstate on
47         ;;
48
49     recovered|updatenatgw)
50         MYPNN=`ctdb pnn | cut -d: -f2`
51         NATGWMASTER=`ctdb natgwlist | head -1 | sed -e "s/ .*//"`
52         NATGWIP=`ctdb natgwlist | head -1 | sed -e "s/^[^ ]* *//"`
53
54         CTDB_NATGW_PUBLIC_IP_HOST=`echo $CTDB_NATGW_PUBLIC_IP | sed -e "s/\/.*/\/32/"`
55
56         # block all incoming connections to the natgw address
57         iptables -D INPUT -p tcp --syn -d $CTDB_NATGW_PUBLIC_IP_HOST -j REJECT 2>/dev/null 
58         iptables -I INPUT -p tcp --syn -d $CTDB_NATGW_PUBLIC_IP_HOST -j REJECT 2>/dev/null 
59
60
61         if [ "$NATGWMASTER" = "-1" ]; then
62                 echo "There is not NATGW master node"
63                 exit 1
64         fi
65
66         delete_all
67
68         if [ "$MYPNN" = "$NATGWMASTER" ]; then
69                 # This is the first node, set it up as the NAT GW
70                 echo 1 >/proc/sys/net/ipv4/ip_forward
71                 iptables -A POSTROUTING -t nat -s $CTDB_NATGW_PRIVATE_NETWORK -d ! $CTDB_NATGW_PRIVATE_NETWORK -j MASQUERADE
72
73                 # block all incoming connections to the natgw address
74                 CTDB_NATGW_PUBLIC_IP_HOST=`echo $CTDB_NATGW_PUBLIC_IP | sed -e "s/\/.*/\/32/"`
75                 iptables -D INPUT -p tcp --syn -d $CTDB_NATGW_PUBLIC_IP_HOST -j REJECT 2>/dev/null
76                 iptables -I INPUT -p tcp --syn -d $CTDB_NATGW_PUBLIC_IP_HOST -j REJECT 2>/dev/null
77
78                 ip addr add $CTDB_NATGW_PUBLIC_IP dev $CTDB_NATGW_PUBLIC_IFACE
79                 ip route add 0.0.0.0/0 via $CTDB_NATGW_DEFAULT_GATEWAY >/dev/null 2>/dev/null
80         else
81                 # This is not the NAT-GW
82                 # Assign the public ip to the private interface and make
83                 # sure we dont respond to ARPs.
84                 # We do this so that the ip address will exist on a
85                 # non-loopback interface so that samba may send it along in the
86                 # KDC requests.
87                 ip addr add $CTDB_NATGW_PUBLIC_IP_HOST dev lo scope host
88                 ip route add 0.0.0.0/0 via $NATGWIP metric 10
89         fi
90
91         # flush our route cache
92         echo 1 > /proc/sys/net/ipv4/route/flush
93         ;;
94
95     shutdown|stopped|removenatgw)
96         delete_all
97         ;;
98
99     *)
100         ctdb_standard_event_handler "@"
101         ;;
102 esac
103
104 exit 0