| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
[ -n "$CTDB_BASE" ] || \ |
| 4 |
CTDB_BASE=$(d="$(dirname "$0")" && cd -P "$d/../../" && pwd) |
| 5 |
|
| 6 |
. "${CTDB_BASE}/functions" |
| 7 |
|
| 8 |
load_script_options |
| 9 |
|
| 10 |
service_name="per_ip_routing" |
| 11 |
|
| 12 |
# Do nothing if unconfigured |
| 13 |
[ -n "$CTDB_PER_IP_ROUTING_CONF" ] || exit 0 |
| 14 |
|
| 15 |
table_id_prefix="ctdb." |
| 16 |
|
| 17 |
[ -n "$CTDB_PER_IP_ROUTING_RULE_PREF" ] || \ |
| 18 |
die "error: CTDB_PER_IP_ROUTING_RULE_PREF not configured" |
| 19 |
|
| 20 |
[ "$CTDB_PER_IP_ROUTING_TABLE_ID_LOW" -lt "$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" ] 2>/dev/null || \ |
| 21 |
die "error: CTDB_PER_IP_ROUTING_TABLE_ID_LOW[$CTDB_PER_IP_ROUTING_TABLE_ID_LOW] and/or CTDB_PER_IP_ROUTING_TABLE_ID_HIGH[$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH] improperly configured" |
| 22 |
|
| 23 |
if [ "$CTDB_PER_IP_ROUTING_TABLE_ID_LOW" -le 253 ] && \ |
| 24 |
[ 255 -le "$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" ] ; then |
| 25 |
die "error: range CTDB_PER_IP_ROUTING_TABLE_ID_LOW[$CTDB_PER_IP_ROUTING_TABLE_ID_LOW]..CTDB_PER_IP_ROUTING_TABLE_ID_HIGH[$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH] must not include 253-255" |
| 26 |
fi |
| 27 |
|
| 28 |
have_link_local_config () |
| 29 |
{ |
| 30 |
[ "$CTDB_PER_IP_ROUTING_CONF" = "__auto_link_local__" ] |
| 31 |
} |
| 32 |
|
| 33 |
if ! have_link_local_config && [ ! -r "$CTDB_PER_IP_ROUTING_CONF" ] ; then |
| 34 |
die "error: CTDB_PER_IP_ROUTING_CONF=$CTDB_PER_IP_ROUTING_CONF file not found" |
| 35 |
fi |
| 36 |
|
| 37 |
ctdb_setup_state_dir "failover" "$service_name" |
| 38 |
|
| 39 |
###################################################################### |
| 40 |
|
| 41 |
ipv4_is_valid_addr() |
| 42 |
{ |
| 43 |
_ip="$1" |
| 44 |
|
| 45 |
_count=0 |
| 46 |
# Get the shell to break up the address into 1 word per octet |
| 47 |
# Intentional word splitting here |
| 48 |
# shellcheck disable=SC2086 |
| 49 |
for _o in $(export IFS="." ; echo $_ip) ; do |
| 50 |
# The 2>/dev/null stops output from failures where an "octet" |
| 51 |
# is not numeric. The test will still fail. |
| 52 |
if ! [ 0 -le $_o ] && [ $_o -le 255 ] 2>/dev/null ; then |
| 53 |
return 1 |
| 54 |
fi |
| 55 |
_count=$((_count + 1)) |
| 56 |
done |
| 57 |
|
| 58 |
# A valid IPv4 address has 4 octets |
| 59 |
[ $_count -eq 4 ] |
| 60 |
} |
| 61 |
|
| 62 |
ensure_ipv4_is_valid_addr () |
| 63 |
{ |
| 64 |
_event="$1" |
| 65 |
_ip="$2" |
| 66 |
|
| 67 |
ipv4_is_valid_addr "$_ip" || { |
| 68 |
echo "$0: $_event not an ipv4 address skipping IP:$_ip" |
| 69 |
exit 0 |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
ipv4_host_addr_to_net () |
| 74 |
{ |
| 75 |
_host="$1" |
| 76 |
_maskbits="$2" |
| 77 |
|
| 78 |
# Convert the host address to an unsigned long by splitting out |
| 79 |
# the octets and doing the math. |
| 80 |
_host_ul=0 |
| 81 |
# Intentional word splitting here |
| 82 |
# shellcheck disable=SC2086 |
| 83 |
for _o in $(export IFS="." ; echo $_host) ; do |
| 84 |
_host_ul=$(( (_host_ul << 8) + _o)) # work around Emacs color bug |
| 85 |
done |
| 86 |
|
| 87 |
# Calculate the mask and apply it. |
| 88 |
_mask_ul=$(( 0xffffffff << (32 - _maskbits) )) |
| 89 |
_net_ul=$(( _host_ul & _mask_ul )) |
| 90 |
|
| 91 |
# Now convert to a network address one byte at a time. |
| 92 |
_net="" |
| 93 |
for _o in $(seq 1 4) ; do |
| 94 |
_net="$((_net_ul & 255))${_net:+.}${_net}" |
| 95 |
_net_ul=$((_net_ul >> 8)) |
| 96 |
done |
| 97 |
|
| 98 |
echo "${_net}/${_maskbits}" |
| 99 |
} |
| 100 |
|
| 101 |
###################################################################### |
| 102 |
|
| 103 |
ensure_rt_tables () |
| 104 |
{ |
| 105 |
rt_tables="$CTDB_SYS_ETCDIR/iproute2/rt_tables" |
| 106 |
# script_state_dir set by ctdb_setup_state_dir() |
| 107 |
# shellcheck disable=SC2154 |
| 108 |
rt_tables_lock="${script_state_dir}/rt_tables_lock" |
| 109 |
|
| 110 |
# This file should always exist. Even if this didn't exist on the |
| 111 |
# system, adding a route will have created it. What if we startup |
| 112 |
# and immediately shutdown? Let's be sure. |
| 113 |
if [ ! -f "$rt_tables" ] ; then |
| 114 |
mkdir -p "${rt_tables%/*}" # dirname |
| 115 |
touch "$rt_tables" |
| 116 |
fi |
| 117 |
} |
| 118 |
|
| 119 |
# Setup a table id to use for the given IP. We don't need to know it, |
| 120 |
# it just needs to exist in /etc/iproute2/rt_tables. Fail if no free |
| 121 |
# table id could be found in the configured range. |
| 122 |
ensure_table_id_for_ip () |
| 123 |
{ |
| 124 |
_ip=$1 |
| 125 |
|
| 126 |
ensure_rt_tables |
| 127 |
|
| 128 |
# Maintain a table id for each IP address we've ever seen in |
| 129 |
# rt_tables. We use a "ctdb." prefix on the label. |
| 130 |
_label="${table_id_prefix}${_ip}" |
| 131 |
|
| 132 |
# This finds either the table id corresponding to the label or a |
| 133 |
# new unused one (that is greater than all the used ones in the |
| 134 |
# range). |
| 135 |
( |
| 136 |
# Note that die() just gets us out of the subshell... |
| 137 |
flock --timeout 30 9 || \ |
| 138 |
die "ensure_table_id_for_ip: failed to lock file $rt_tables" |
| 139 |
|
| 140 |
_new="$CTDB_PER_IP_ROUTING_TABLE_ID_LOW" |
| 141 |
while read _t _l ; do |
| 142 |
# Skip comments |
| 143 |
case "$_t" in |
| 144 |
\#*) continue ;; |
| 145 |
esac |
| 146 |
# Found existing: done |
| 147 |
if [ "$_l" = "$_label" ] ; then |
| 148 |
return 0 |
| 149 |
fi |
| 150 |
# Potentially update the new table id to be used. The |
| 151 |
# redirect stops error spam for a non-numeric value. |
| 152 |
if [ "$_new" -le "$_t" ] && \ |
| 153 |
[ "$_t" -le "$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" ] \ |
| 154 |
2>/dev/null ; then |
| 155 |
_new=$((_t + 1)) |
| 156 |
fi |
| 157 |
done <"$rt_tables" |
| 158 |
|
| 159 |
# If the new table id is legal then add it to the file and |
| 160 |
# print it. |
| 161 |
if [ "$_new" -le "$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" ] ; then |
| 162 |
printf '%d\t%s\n' "$_new" "$_label" >>"$rt_tables" |
| 163 |
return 0 |
| 164 |
else |
| 165 |
return 1 |
| 166 |
fi |
| 167 |
) 9>"$rt_tables_lock" |
| 168 |
} |
| 169 |
|
| 170 |
# Clean up all the table ids that we might own. |
| 171 |
clean_up_table_ids () |
| 172 |
{ |
| 173 |
ensure_rt_tables |
| 174 |
|
| 175 |
( |
| 176 |
# Note that die() just gets us out of the subshell... |
| 177 |
flock --timeout 30 9 || \ |
| 178 |
die "clean_up_table_ids: failed to lock file $rt_tables" |
| 179 |
|
| 180 |
# Delete any items from the file that have a table id in our |
| 181 |
# range or a label matching our label. Preserve comments. |
| 182 |
_tmp="${rt_tables}.$$.ctdb" |
| 183 |
awk -v min="$CTDB_PER_IP_ROUTING_TABLE_ID_LOW" \ |
| 184 |
-v max="$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" \ |
| 185 |
-v pre="$table_id_prefix" \ |
| 186 |
'/^#/ || |
| 187 |
!(min <= $1 && $1 <= max) && |
| 188 |
!(index($2, pre) == 1) { |
| 189 |
print $0 }' "$rt_tables" >"$_tmp" |
| 190 |
|
| 191 |
mv "$_tmp" "$rt_tables" |
| 192 |
) 9>"$rt_tables_lock" |
| 193 |
} |
| 194 |
|
| 195 |
###################################################################### |
| 196 |
|
| 197 |
# This prints the config for an IP, which is either relevant entries |
| 198 |
# from the config file or, if set to the magic link local value, some |
| 199 |
# link local routing config for the IP. |
| 200 |
get_config_for_ip () |
| 201 |
{ |
| 202 |
_ip="$1" |
| 203 |
|
| 204 |
if have_link_local_config ; then |
| 205 |
# When parsing public_addresses also split on '/'. This means |
| 206 |
# that we get the maskbits as item #2 without further parsing. |
| 207 |
while IFS="/$IFS" read _i _maskbits _x ; do |
| 208 |
if [ "$_ip" = "$_i" ] ; then |
| 209 |
printf "%s" "$_ip "; ipv4_host_addr_to_net "$_ip" "$_maskbits" |
| 210 |
fi |
| 211 |
done <"${CTDB_BASE}/public_addresses" |
| 212 |
else |
| 213 |
while read _i _rest ; do |
| 214 |
if [ "$_ip" = "$_i" ] ; then |
| 215 |
printf '%s\t%s\n' "$_ip" "$_rest" |
| 216 |
fi |
| 217 |
done <"$CTDB_PER_IP_ROUTING_CONF" |
| 218 |
fi |
| 219 |
} |
| 220 |
|
| 221 |
ip_has_configuration () |
| 222 |
{ |
| 223 |
_ip="$1" |
| 224 |
|
| 225 |
_conf=$(get_config_for_ip "$_ip") |
| 226 |
[ -n "$_conf" ] |
| 227 |
} |
| 228 |
|
| 229 |
add_routing_for_ip () |
| 230 |
{ |
| 231 |
_iface="$1" |
| 232 |
_ip="$2" |
| 233 |
|
| 234 |
# Do nothing if no config for this IP. |
| 235 |
ip_has_configuration "$_ip" || return 0 |
| 236 |
|
| 237 |
ensure_table_id_for_ip "$_ip" || \ |
| 238 |
die "add_routing_for_ip: out of table ids in range $CTDB_PER_IP_ROUTING_TABLE_ID_LOW - $CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" |
| 239 |
|
| 240 |
_pref="$CTDB_PER_IP_ROUTING_RULE_PREF" |
| 241 |
_table_id="${table_id_prefix}${_ip}" |
| 242 |
|
| 243 |
del_routing_for_ip "$_ip" >/dev/null 2>&1 |
| 244 |
|
| 245 |
ip rule add from "$_ip" pref "$_pref" table "$_table_id" || \ |
| 246 |
die "add_routing_for_ip: failed to add rule for $_ip" |
| 247 |
|
| 248 |
# Add routes to table for any lines matching the IP. |
| 249 |
get_config_for_ip "$_ip" | |
| 250 |
while read _i _dest _gw ; do |
| 251 |
_r="$_dest ${_gw:+via} $_gw dev $_iface table $_table_id" |
| 252 |
# Intentionally unquoted multi-word value here |
| 253 |
# shellcheck disable=SC2086 |
| 254 |
ip route add $_r || \ |
| 255 |
die "add_routing_for_ip: failed to add route: $_r" |
| 256 |
done |
| 257 |
} |
| 258 |
|
| 259 |
del_routing_for_ip () |
| 260 |
{ |
| 261 |
_ip="$1" |
| 262 |
|
| 263 |
_pref="$CTDB_PER_IP_ROUTING_RULE_PREF" |
| 264 |
_table_id="${table_id_prefix}${_ip}" |
| 265 |
|
| 266 |
# Do this unconditionally since we own any matching table ids. |
| 267 |
# However, print a meaningful message if something goes wrong. |
| 268 |
_cmd="ip rule del from $_ip pref $_pref table $_table_id" |
| 269 |
_out=$($_cmd 2>&1) || \ |
| 270 |
cat <<EOF |
| 271 |
WARNING: Failed to delete policy routing rule |
| 272 |
Command "$_cmd" failed: |
| 273 |
$_out |
| 274 |
EOF |
| 275 |
# This should never usually fail, so don't redirect output. |
| 276 |
# However, it can fail when deleting a rogue IP, since there will |
| 277 |
# be no routes for that IP. In this case it should only fail when |
| 278 |
# the rule deletion above has already failed because the table id |
| 279 |
# is invalid. Therefore, go to a little bit of trouble to indent |
| 280 |
# the failure message so that it is associated with the above |
| 281 |
# warning message and doesn't look too nasty. |
| 282 |
ip route flush table "$_table_id" 2>&1 | sed -e 's@^.@ &@' |
| 283 |
} |
| 284 |
|
| 285 |
###################################################################### |
| 286 |
|
| 287 |
flush_rules_and_routes () |
| 288 |
{ |
| 289 |
ip rule show | |
| 290 |
while read _p _x _i _x _t ; do |
| 291 |
# Remove trailing colon after priority/preference. |
| 292 |
_p="${_p%:}" |
| 293 |
# Only remove rules that match our priority/preference. |
| 294 |
[ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue |
| 295 |
|
| 296 |
echo "Removing ip rule for public address $_i for routing table $_t" |
| 297 |
ip rule del from "$_i" table "$_t" pref "$_p" |
| 298 |
ip route flush table "$_t" 2>/dev/null |
| 299 |
done |
| 300 |
} |
| 301 |
|
| 302 |
# Add any missing routes. Some might have gone missing if, for |
| 303 |
# example, all IPs on the network were removed (possibly if the |
| 304 |
# primary was removed). If $1 is "force" then (re-)add all the |
| 305 |
# routes. |
| 306 |
add_missing_routes () |
| 307 |
{ |
| 308 |
$CTDB ip -v -X | { |
| 309 |
read _x # skip header line |
| 310 |
|
| 311 |
# Read the rest of the lines. We're only interested in the |
| 312 |
# "IP" and "ActiveInterface" columns. The latter is only set |
| 313 |
# for addresses local to this node, making it easy to skip |
| 314 |
# non-local addresses. For each IP local address we check if |
| 315 |
# the relevant routing table is populated and populate it if |
| 316 |
# not. |
| 317 |
while IFS="|" read _x _ip _x _iface _x ; do |
| 318 |
[ -n "$_iface" ] || continue |
| 319 |
|
| 320 |
_table_id="${table_id_prefix}${_ip}" |
| 321 |
if [ -z "$(ip route show table "$_table_id" 2>/dev/null)" ] || \ |
| 322 |
[ "$1" = "force" ] ; then |
| 323 |
add_routing_for_ip "$_iface" "$_ip" |
| 324 |
fi |
| 325 |
done |
| 326 |
} || exit $? |
| 327 |
} |
| 328 |
|
| 329 |
# Remove rules/routes for addresses that we're not hosting. If a |
| 330 |
# releaseip event failed in an earlier script then we might not have |
| 331 |
# had a chance to remove the corresponding rules/routes. |
| 332 |
remove_bogus_routes () |
| 333 |
{ |
| 334 |
# Get a IPs current hosted by this node, each anchored with '@'. |
| 335 |
_ips=$($CTDB ip -v -X | awk -F'|' 'NR > 1 && $4 != "" {printf "@%s@\n", $2}') |
| 336 |
|
| 337 |
# x is intentionally ignored |
| 338 |
# shellcheck disable=SC2034 |
| 339 |
ip rule show | |
| 340 |
while read _p _x _i _x _t ; do |
| 341 |
# Remove trailing colon after priority/preference. |
| 342 |
_p="${_p%:}" |
| 343 |
# Only remove rules that match our priority/preference. |
| 344 |
[ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue |
| 345 |
# Only remove rules for which we don't have an IP. This could |
| 346 |
# be done with grep, but let's do it with shell prefix removal |
| 347 |
# to avoid unnecessary processes. This falls through if |
| 348 |
# "@${_i}@" isn't present in $_ips. |
| 349 |
[ "$_ips" = "${_ips#*@"${_i}"@}" ] || continue |
| 350 |
|
| 351 |
echo "Removing ip rule/routes for unhosted public address $_i" |
| 352 |
del_routing_for_ip "$_i" |
| 353 |
done |
| 354 |
} |
| 355 |
|
| 356 |
###################################################################### |
| 357 |
|
| 358 |
ctdb_check_args "$@" |
| 359 |
|
| 360 |
case "$1" in |
| 361 |
startup) |
| 362 |
flush_rules_and_routes |
| 363 |
|
| 364 |
# make sure that we only respond to ARP messages from the NIC |
| 365 |
# where a particular ip address is associated. |
| 366 |
get_proc sys/net/ipv4/conf/all/arp_filter >/dev/null 2>&1 && { |
| 367 |
set_proc sys/net/ipv4/conf/all/arp_filter 1 |
| 368 |
} |
| 369 |
;; |
| 370 |
|
| 371 |
shutdown) |
| 372 |
flush_rules_and_routes |
| 373 |
clean_up_table_ids |
| 374 |
;; |
| 375 |
|
| 376 |
takeip) |
| 377 |
iface=$2 |
| 378 |
ip=$3 |
| 379 |
# maskbits included here so argument order is obvious |
| 380 |
# shellcheck disable=SC2034 |
| 381 |
maskbits=$4 |
| 382 |
|
| 383 |
ensure_ipv4_is_valid_addr "$1" "$ip" |
| 384 |
add_routing_for_ip "$iface" "$ip" |
| 385 |
|
| 386 |
# flush our route cache |
| 387 |
set_proc sys/net/ipv4/route/flush 1 |
| 388 |
|
| 389 |
$CTDB gratarp "$ip" "$iface" |
| 390 |
;; |
| 391 |
|
| 392 |
updateip) |
| 393 |
# oiface, maskbits included here so argument order is obvious |
| 394 |
# shellcheck disable=SC2034 |
| 395 |
oiface=$2 |
| 396 |
niface=$3 |
| 397 |
ip=$4 |
| 398 |
# shellcheck disable=SC2034 |
| 399 |
maskbits=$5 |
| 400 |
|
| 401 |
ensure_ipv4_is_valid_addr "$1" "$ip" |
| 402 |
add_routing_for_ip "$niface" "$ip" |
| 403 |
|
| 404 |
# flush our route cache |
| 405 |
set_proc sys/net/ipv4/route/flush 1 |
| 406 |
|
| 407 |
$CTDB gratarp "$ip" "$niface" |
| 408 |
tickle_tcp_connections "$ip" |
| 409 |
;; |
| 410 |
|
| 411 |
releaseip) |
| 412 |
iface=$2 |
| 413 |
ip=$3 |
| 414 |
# maskbits included here so argument order is obvious |
| 415 |
# shellcheck disable=SC2034 |
| 416 |
maskbits=$4 |
| 417 |
|
| 418 |
ensure_ipv4_is_valid_addr "$1" "$ip" |
| 419 |
del_routing_for_ip "$ip" |
| 420 |
;; |
| 421 |
|
| 422 |
ipreallocated) |
| 423 |
add_missing_routes |
| 424 |
remove_bogus_routes |
| 425 |
;; |
| 426 |
|
| 427 |
reconfigure) |
| 428 |
echo "Reconfiguring service \"${service_name}\"..." |
| 429 |
|
| 430 |
add_missing_routes "force" |
| 431 |
remove_bogus_routes |
| 432 |
|
| 433 |
# flush our route cache |
| 434 |
set_proc sys/net/ipv4/route/flush 1 |
| 435 |
;; |
| 436 |
esac |
| 437 |
|
| 438 |
exit 0 |