| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# Handle public IP address release and takeover, as well as monitoring |
| 4 |
# interfaces used by public IP addresses. |
| 5 |
|
| 6 |
[ -n "$CTDB_BASE" ] || |
| 7 |
CTDB_BASE=$(d="$(dirname "$0")" && cd -P "$d/../../" && pwd) |
| 8 |
|
| 9 |
. "${CTDB_BASE}/functions" |
| 10 |
|
| 11 |
load_script_options |
| 12 |
|
| 13 |
if ! have_public_addresses; then |
| 14 |
if [ "$1" = "init" ]; then |
| 15 |
echo "No public addresses file found" |
| 16 |
fi |
| 17 |
exit 0 |
| 18 |
fi |
| 19 |
|
| 20 |
monitor_interfaces() |
| 21 |
{ |
| 22 |
get_public_ifaces |
| 23 |
|
| 24 |
down_interfaces_found=false |
| 25 |
up_interfaces_found=false |
| 26 |
|
| 27 |
# Note that this loop must not exit early. It must process |
| 28 |
# all interfaces so that the correct state for each interface |
| 29 |
# is set in CTDB using setifacelink. |
| 30 |
# |
| 31 |
# public_ifaces set by get_public_ifaces() above |
| 32 |
# shellcheck disable=SC2154 |
| 33 |
for _iface in $public_ifaces; do |
| 34 |
if interface_monitor "$_iface"; then |
| 35 |
up_interfaces_found=true |
| 36 |
$CTDB setifacelink "$_iface" up >/dev/null 2>&1 |
| 37 |
else |
| 38 |
down_interfaces_found=true |
| 39 |
$CTDB setifacelink "$_iface" down >/dev/null 2>&1 |
| 40 |
fi |
| 41 |
done |
| 42 |
|
| 43 |
if ! $down_interfaces_found; then |
| 44 |
return 0 |
| 45 |
fi |
| 46 |
|
| 47 |
if ! $up_interfaces_found; then |
| 48 |
return 1 |
| 49 |
fi |
| 50 |
|
| 51 |
if [ "$CTDB_PARTIALLY_ONLINE_INTERFACES" != "yes" ]; then |
| 52 |
return 1 |
| 53 |
fi |
| 54 |
|
| 55 |
return 0 |
| 56 |
} |
| 57 |
|
| 58 |
# Sets: iface, ip, maskbits |
| 59 |
get_iface_ip_maskbits() |
| 60 |
{ |
| 61 |
_iface_in="$1" |
| 62 |
ip="$2" |
| 63 |
_maskbits_in="$3" |
| 64 |
|
| 65 |
# Intentional word splitting here |
| 66 |
# shellcheck disable=SC2046 |
| 67 |
set -- $(ip_maskbits_iface "$ip") |
| 68 |
if [ -n "$1" ]; then |
| 69 |
maskbits="$1" |
| 70 |
iface="$2" |
| 71 |
|
| 72 |
if [ "$iface" != "$_iface_in" ]; then |
| 73 |
printf 'WARNING: Public IP %s hosted on interface %s but VNN says %s\n' \ |
| 74 |
"$ip" "$iface" "$_iface_in" |
| 75 |
fi |
| 76 |
if [ "$maskbits" != "$_maskbits_in" ]; then |
| 77 |
printf 'WARNING: Public IP %s has %s bit netmask but VNN says %s\n' \ |
| 78 |
"$ip" "$maskbits" "$_maskbits_in" |
| 79 |
fi |
| 80 |
else |
| 81 |
if [ "$_iface_in" = "__none__" ]; then |
| 82 |
echo "WARNING: Unable to determine interface for IP ${ip}" |
| 83 |
iface="$_iface_in" |
| 84 |
return |
| 85 |
fi |
| 86 |
die "ERROR: Unable to determine interface for IP ${ip}" |
| 87 |
fi |
| 88 |
} |
| 89 |
|
| 90 |
ip_block() |
| 91 |
{ |
| 92 |
_ip="$1" |
| 93 |
_iface="$2" |
| 94 |
|
| 95 |
case "$_ip" in |
| 96 |
*:*) _family="inet6" ;; |
| 97 |
*) _family="inet" ;; |
| 98 |
esac |
| 99 |
|
| 100 |
# Extra delete copes with previously killed script |
| 101 |
iptables_wrapper "$_family" \ |
| 102 |
-D INPUT -i "$_iface" -d "$_ip" -j DROP 2>/dev/null |
| 103 |
iptables_wrapper "$_family" \ |
| 104 |
-I INPUT -i "$_iface" -d "$_ip" -j DROP |
| 105 |
} |
| 106 |
|
| 107 |
ip_unblock() |
| 108 |
{ |
| 109 |
_ip="$1" |
| 110 |
_iface="$2" |
| 111 |
|
| 112 |
case "$_ip" in |
| 113 |
*:*) _family="inet6" ;; |
| 114 |
*) _family="inet" ;; |
| 115 |
esac |
| 116 |
|
| 117 |
iptables_wrapper "$_family" \ |
| 118 |
-D INPUT -i "$_iface" -d "$_ip" -j DROP 2>/dev/null |
| 119 |
} |
| 120 |
|
| 121 |
ctdb_check_args "$@" |
| 122 |
|
| 123 |
case "$1" in |
| 124 |
init) |
| 125 |
_promote="sys/net/ipv4/conf/all/promote_secondaries" |
| 126 |
get_proc "$_promote" >/dev/null 2>&1 || |
| 127 |
die "Public IPs only supported if promote_secondaries is available" |
| 128 |
|
| 129 |
# Make sure we drop any IPs that might still be held if |
| 130 |
# previous instance of ctdbd got killed with -9 or similar |
| 131 |
drop_all_public_ips |
| 132 |
;; |
| 133 |
|
| 134 |
startup) |
| 135 |
monitor_interfaces |
| 136 |
;; |
| 137 |
|
| 138 |
shutdown) |
| 139 |
drop_all_public_ips |
| 140 |
;; |
| 141 |
|
| 142 |
takeip) |
| 143 |
iface=$2 |
| 144 |
ip=$3 |
| 145 |
maskbits=$4 |
| 146 |
|
| 147 |
update_my_public_ip_addresses "takeip" "$ip" |
| 148 |
|
| 149 |
add_ip_to_iface "$iface" "$ip" "$maskbits" || { |
| 150 |
exit 1 |
| 151 |
} |
| 152 |
|
| 153 |
# In case a previous "releaseip" for this IP was killed... |
| 154 |
ip_unblock "$ip" "$iface" |
| 155 |
|
| 156 |
flush_route_cache |
| 157 |
;; |
| 158 |
|
| 159 |
releaseip) |
| 160 |
# Releasing an IP is a bit more complex than it seems. Once |
| 161 |
# the IP is released, any open TCP connections to that IP on |
| 162 |
# this host will end up being stuck. Some of them (such as NFS |
| 163 |
# connections) will be unkillable so we need to terminate |
| 164 |
# them. We also need to make sure that no new connections get |
| 165 |
# established while we are doing this. |
| 166 |
# |
| 167 |
# The steps are: |
| 168 |
# |
| 169 |
# 1) firewall this IP, so no new external packets arrive for it |
| 170 |
# 2) find existing connections, and kill them |
| 171 |
# 3) remove the IP from the interface |
| 172 |
# 4) remove the firewall rule |
| 173 |
shift |
| 174 |
get_iface_ip_maskbits "$@" |
| 175 |
|
| 176 |
ip_block "$ip" "$iface" |
| 177 |
|
| 178 |
kill_tcp_connections "$iface" "$ip" |
| 179 |
|
| 180 |
update_my_public_ip_addresses "releaseip" "$ip" |
| 181 |
|
| 182 |
delete_ip_from_iface "$iface" "$ip" "$maskbits" || { |
| 183 |
ip_unblock "$ip" "$iface" |
| 184 |
exit 1 |
| 185 |
} |
| 186 |
|
| 187 |
ip_unblock "$ip" "$iface" |
| 188 |
|
| 189 |
flush_route_cache |
| 190 |
;; |
| 191 |
|
| 192 |
updateip) |
| 193 |
# Moving an IP is a bit more complex than it seems. First we |
| 194 |
# drop all traffic on the old interface. Then we try to |
| 195 |
# remove the IP from the old interface and add it to the new |
| 196 |
# interface. |
| 197 |
# |
| 198 |
# The steps are: |
| 199 |
# |
| 200 |
# 1) firewall this IP, so no new external packets arrive for it |
| 201 |
# 2) remove the IP from the old interface (and new interface, to be sure) |
| 202 |
# 3) add the IP to the new interface |
| 203 |
# 4) remove the firewall rule |
| 204 |
# 5) use ctdb gratarp to propagate the new mac address |
| 205 |
# 6) send tickle ACKs for existing connections, so dropped |
| 206 |
# packets are resent |
| 207 |
_oiface=$2 |
| 208 |
niface=$3 |
| 209 |
_ip=$4 |
| 210 |
_maskbits=$5 |
| 211 |
|
| 212 |
get_iface_ip_maskbits "$_oiface" "$_ip" "$_maskbits" |
| 213 |
oiface="$iface" |
| 214 |
|
| 215 |
# Could check maskbits too. However, that should never change |
| 216 |
# so we want to notice if it does. |
| 217 |
if [ "$oiface" = "$niface" ]; then |
| 218 |
echo "Redundant \"updateip\" - ${ip} already on ${niface}" |
| 219 |
exit 0 |
| 220 |
fi |
| 221 |
|
| 222 |
# Behave more like takeip when the IP is not assigned. No |
| 223 |
# need for a similar condition around ip_unblock()s because |
| 224 |
# they will silently fail. |
| 225 |
if [ "$oiface" != "__none__" ]; then |
| 226 |
ip_block "$ip" "$oiface" |
| 227 |
delete_ip_from_iface "$oiface" "$ip" "$maskbits" >/dev/null 2>&1 |
| 228 |
fi |
| 229 |
delete_ip_from_iface "$niface" "$ip" "$maskbits" >/dev/null 2>&1 |
| 230 |
|
| 231 |
add_ip_to_iface "$niface" "$ip" "$maskbits" || { |
| 232 |
ip_unblock "$ip" "$oiface" |
| 233 |
exit 1 |
| 234 |
} |
| 235 |
|
| 236 |
ip_unblock "$ip" "$oiface" |
| 237 |
|
| 238 |
flush_route_cache |
| 239 |
|
| 240 |
# Propagate the new MAC address |
| 241 |
$CTDB gratarp "$ip" "$niface" |
| 242 |
|
| 243 |
# Tickle all existing connections, so that dropped packets |
| 244 |
# are retransmitted and the tcp streams work |
| 245 |
tickle_tcp_connections "$ip" |
| 246 |
;; |
| 247 |
|
| 248 |
ipreallocated) |
| 249 |
# Just to make sure |
| 250 |
update_my_public_ip_addresses "ipreallocated" |
| 251 |
;; |
| 252 |
|
| 253 |
monitor) |
| 254 |
monitor_interfaces || exit 1 |
| 255 |
|
| 256 |
update_tickles |
| 257 |
;; |
| 258 |
esac |
| 259 |
|
| 260 |
exit 0 |