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
8 [ -n "$CTDB_BASE" ] || \
9 CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; dirname "$PWD")
11 . "${CTDB_BASE}/functions"
17 [ -n "$CTDB_NATGW_NODES" ] || exit 0
18 export CTDB_NATGW_NODES
20 ctdb_setup_service_state_dir
22 natgw_cfg_new="${service_state_dir}/cfg_new"
23 natgw_cfg_old="${service_state_dir}/cfg_old"
24 natgw_master_old="${service_state_dir}/master_old"
26 ctdb_natgw_slave_only ()
30 awk -v my_ip="$ip_address" \
31 '$1 == my_ip { if ($2 ~ "slave-only") { exit 0 } else { exit 1 } }' \
37 [ -r "$CTDB_NATGW_NODES" ] || \
38 die "error: CTDB_NATGW_NODES=${CTDB_NATGW_NODES} unreadable"
39 if ! ctdb_natgw_slave_only ; then
40 [ -n "$CTDB_NATGW_PUBLIC_IP" ] || \
41 die "Invalid configuration: CTDB_NATGW_PUBLIC_IP not set"
42 [ -n "$CTDB_NATGW_PUBLIC_IFACE" ] || \
43 die "Invalid configuration: CTDB_NATGW_PUBLIC_IFACE not set"
45 [ -n "$CTDB_NATGW_PRIVATE_NETWORK" ] || \
46 die "Invalid configuration: CTDB_NATGW_PRIVATE_NETWORK not set"
48 if [ "$CTDB_PARTIALLY_ONLINE_INTERFACES" = "yes" ] ; then
49 die "Invalid configuration: CTDB_PARTIALLY_ONLINE_INTERFACES=yes incompatible with NAT gateway"
52 # The default is to create a single default route
53 [ -n "$CTDB_NATGW_STATIC_ROUTES" ] || CTDB_NATGW_STATIC_ROUTES="0.0.0.0/0"
61 CTDB_NATGW_NODES="$CTDB_NATGW_NODES"
62 CTDB_NATGW_PUBLIC_IP="$CTDB_NATGW_PUBLIC_IP"
63 CTDB_NATGW_PUBLIC_IFACE="$CTDB_NATGW_PUBLIC_IFACE"
64 CTDB_NATGW_DEFAULT_GATEWAY="$CTDB_NATGW_DEFAULT_GATEWAY"
65 CTDB_NATGW_PRIVATE_NETWORK="$CTDB_NATGW_PRIVATE_NETWORK"
66 CTDB_NATGW_STATIC_ROUTES="$CTDB_NATGW_STATIC_ROUTES"
70 natgw_config_has_changed ()
72 natgw_write_config "$natgw_cfg_new"
74 # Non-existent old returns true, no log message
75 if [ ! -f "$natgw_cfg_old" ] ; then
80 if cmp "$natgw_cfg_old" "$natgw_cfg_new" >/dev/null 2>&1 ; then
84 echo "NAT gateway configuration has changed"
90 _ip="${CTDB_NATGW_PUBLIC_IP%/*}"
91 _maskbits="${CTDB_NATGW_PUBLIC_IP#*/}"
93 delete_ip_from_iface \
94 "$CTDB_NATGW_PUBLIC_IFACE" "$_ip" "$_maskbits" >/dev/null 2>&1
95 for _net_gw in $CTDB_NATGW_STATIC_ROUTES ; do
97 ip route del "$_net" metric 10 >/dev/null 2>/dev/null
100 # Delete the masquerading setup from a previous iteration where we
102 iptables -D POSTROUTING -t nat \
103 -s "$CTDB_NATGW_PRIVATE_NETWORK" ! -d "$CTDB_NATGW_PRIVATE_NETWORK" \
104 -j MASQUERADE >/dev/null 2>/dev/null
106 iptables -D INPUT -p tcp --syn -d "${_ip}/32" -j REJECT 2>/dev/null
111 if [ -r "$natgw_cfg_old" ] ; then
112 (. "$natgw_cfg_old" ; _natgw_clear)
120 set_proc sys/net/ipv4/ip_forward 1
121 iptables -A POSTROUTING -t nat \
122 -s "$CTDB_NATGW_PRIVATE_NETWORK" ! -d "$CTDB_NATGW_PRIVATE_NETWORK" \
125 # block all incoming connections to the NATGW IP address
126 ctdb_natgw_public_ip_host="${CTDB_NATGW_PUBLIC_IP%/*}/32"
127 iptables -D INPUT -p tcp --syn \
128 -d "$ctdb_natgw_public_ip_host" -j REJECT 2>/dev/null
129 iptables -I INPUT -p tcp --syn \
130 -d "$ctdb_natgw_public_ip_host" -j REJECT 2>/dev/null
132 ip addr add "$CTDB_NATGW_PUBLIC_IP" dev "$CTDB_NATGW_PUBLIC_IFACE"
133 for _net_gw in $CTDB_NATGW_STATIC_ROUTES ; do
135 if [ "$_net" != "$_net_gw" ] ; then
138 _gw="$CTDB_NATGW_DEFAULT_GATEWAY"
141 [ -n "$_gw" ] || continue
142 ip route add "$_net" metric 10 via "$_gw"
150 for _net_gw in $CTDB_NATGW_STATIC_ROUTES ; do
152 ip route add "$_net" via "$_natgwip" metric 10
156 natgw_ensure_master ()
158 set -- $("${CTDB_HELPER_BINDIR}/ctdb_natgw" master)
159 natgwmaster="${1:--1}" # Default is -1, for failure above
162 if [ "$natgwmaster" = "-1" ]; then
164 die "There is no NATGW master node"
168 natgw_master_has_changed ()
170 if [ -r "$natgw_master_old" ] ; then
171 read _old_natgwmaster <"$natgw_master_old"
175 [ "$_old_natgwmaster" != "$natgwmaster" ]
180 echo "$natgwmaster" >"$natgw_master_old"
181 # Created by natgw_config_has_changed()
182 mv "$natgw_cfg_new" "$natgw_cfg_old"
194 # Error if CTDB_NATGW_PUBLIC_IP is listed in public addresses
195 ip_pat=$(echo "$CTDB_NATGW_PUBLIC_IP" | sed -e 's@\.@\\.@g')
196 if grep -q "^${ip_pat}[[:space:]]" \
197 "${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}" ; then
198 die "ERROR: CTDB_NATGW_PUBLIC_IP same as a public address"
201 # do not send out arp requests from loopback addresses
202 set_proc sys/net/ipv4/conf/all/arp_announce 2
205 updatenatgw|ipreallocated)
212 natgw_config_has_changed || natgw_master_has_changed || exit 0
216 if [ "$pnn" = "$natgwmaster" ]; then
219 natgw_set_slave "$natgwip"
222 # flush our route cache
223 set_proc sys/net/ipv4/route/flush 1
225 # Only update saved state when NATGW successfully updated
229 shutdown|removenatgw)
237 if [ -n "$CTDB_NATGW_PUBLIC_IFACE" ] ; then
238 interface_monitor "$CTDB_NATGW_PUBLIC_IFACE" || exit 1
243 ctdb_standard_event_handler "@"