3 # Handle public IP address release and takeover, as well as monitoring
4 # interfaces used by public IP addresses.
7 CTDB_BASE
=$
(d
=$
(dirname "$0") && cd -P "$d" && dirname "$PWD")
9 .
"${CTDB_BASE}/functions"
13 if ! have_public_addresses
; then
14 if [ "$1" = "init" ]; then
15 echo "No public addresses file found"
24 down_interfaces_found
=false
25 up_interfaces_found
=false
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.
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
38 down_interfaces_found
=true
39 $CTDB setifacelink
"$_iface" down
>/dev
/null
2>&1
43 if ! $down_interfaces_found; then
47 if ! $up_interfaces_found; then
51 if [ "$CTDB_PARTIALLY_ONLINE_INTERFACES" != "yes" ]; then
58 # Sets: iface, ip, maskbits
59 get_iface_ip_maskbits
()
65 # Intentional word splitting here
66 # shellcheck disable=SC2046
67 set -- $
(ip_maskbits_iface
"$ip")
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"
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"
81 die
"ERROR: Unable to determine interface for IP ${ip}"
91 *:*) _family
="inet6" ;;
95 # Extra delete copes with previously killed script
96 iptables_wrapper
"$_family" \
97 -D INPUT
-i "$_iface" -d "$_ip" -j DROP
2>/dev
/null
98 iptables_wrapper
"$_family" \
99 -I INPUT
-i "$_iface" -d "$_ip" -j DROP
108 *:*) _family
="inet6" ;;
112 iptables_wrapper
"$_family" \
113 -D INPUT
-i "$_iface" -d "$_ip" -j DROP
2>/dev
/null
120 _promote
="sys/net/ipv4/conf/all/promote_secondaries"
121 get_proc
"$_promote" >/dev
/null
2>&1 ||
122 die
"Public IPs only supported if promote_secondaries is available"
124 # Make sure we drop any IPs that might still be held if
125 # previous instance of ctdbd got killed with -9 or similar
142 update_my_public_ip_addresses
"takeip" "$ip"
144 add_ip_to_iface
"$iface" "$ip" "$maskbits" ||
{
148 # In case a previous "releaseip" for this IP was killed...
149 ip_unblock
"$ip" "$iface"
155 # Releasing an IP is a bit more complex than it seems. Once
156 # the IP is released, any open TCP connections to that IP on
157 # this host will end up being stuck. Some of them (such as NFS
158 # connections) will be unkillable so we need to terminate
159 # them. We also need to make sure that no new connections get
160 # established while we are doing this.
164 # 1) firewall this IP, so no new external packets arrive for it
165 # 2) find existing connections, and kill them
166 # 3) remove the IP from the interface
167 # 4) remove the firewall rule
169 get_iface_ip_maskbits
"$@"
171 ip_block
"$ip" "$iface"
173 kill_tcp_connections
"$iface" "$ip"
175 update_my_public_ip_addresses
"releaseip" "$ip"
177 delete_ip_from_iface
"$iface" "$ip" "$maskbits" ||
{
178 ip_unblock
"$ip" "$iface"
182 ip_unblock
"$ip" "$iface"
188 # Moving an IP is a bit more complex than it seems. First we
189 # drop all traffic on the old interface. Then we try to
190 # remove the IP from the old interface and add it to the new
195 # 1) firewall this IP, so no new external packets arrive for it
196 # 2) remove the IP from the old interface (and new interface, to be sure)
197 # 3) add the IP to the new interface
198 # 4) remove the firewall rule
199 # 5) use ctdb gratarp to propagate the new mac address
200 # 6) send tickle ACKs for existing connections, so dropped
207 get_iface_ip_maskbits
"$_oiface" "$_ip" "$_maskbits"
210 # Could check maskbits too. However, that should never change
211 # so we want to notice if it does.
212 if [ "$oiface" = "$niface" ]; then
213 echo "Redundant \"updateip\" - ${ip} already on ${niface}"
217 ip_block
"$ip" "$oiface"
219 delete_ip_from_iface
"$oiface" "$ip" "$maskbits" 2>/dev
/null
220 delete_ip_from_iface
"$niface" "$ip" "$maskbits" 2>/dev
/null
222 add_ip_to_iface
"$niface" "$ip" "$maskbits" ||
{
223 ip_unblock
"$ip" "$oiface"
227 ip_unblock
"$ip" "$oiface"
231 # Propagate the new MAC address
232 $CTDB gratarp
"$ip" "$niface"
234 # Tickle all existing connections, so that dropped packets
235 # are retransmitted and the tcp streams work
236 tickle_tcp_connections
"$ip"
241 update_my_public_ip_addresses
"ipreallocated"
245 monitor_interfaces ||
exit 1