Commit 54e755bd authored by Vitaly Lipatov's avatar Vitaly Lipatov

route-update.sh: fix "default" gateway resolution for IPv6

Pass ip command to read_group_config() so "default" keyword resolves via "ip -6 route" for routes6.d groups instead of always using IPv4. Also detect address family from directory location instead of gateway content for --add/--del/--flush commands. Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 0bc2bee2
......@@ -59,20 +59,30 @@ rule_pref()
echo $(( $1 * 10 ))
}
# Resolve "default" keyword to actual default gateway
# Usage: resolve_default_gw IPCMD
# IPCMD: "ip" for IPv4, "ip -6" for IPv6
resolve_default_gw()
{
$1 route show default | awk '/default/ {print $3; exit}'
}
# Read gateway and table from a group directory
# Sets: gw, table, route_via
# gw - first gateway (for display and IPv4/v6 detection)
# table - routing table number
# route_via - route suffix: "via GW table T" or "table T nexthop via GW1 ... nexthop via GW2 ..."
# $1 - directory, $2 - ip command ("ip" or "ip -6")
read_group_config()
{
local dir="$1"
local ipcmd="${2:-ip}"
gw="" ; table="" ; route_via=""
if [ -f "$dir/gateway" ] ; then
gw=$(grep -v '^#' "$dir/gateway" | grep -m1 .)
if [ "$gw" = "default" ] ; then
gw=$(ip route show default | awk '/default/ {print $3; exit}')
gw=$(resolve_default_gw "$ipcmd")
fi
else
echo "[$(basename "$dir")] No gateway file, skipping" >&2
......@@ -95,7 +105,7 @@ read_group_config()
local g
for g in $(grep -v '^#' "$dir/gateway" | grep .) ; do
if [ "$g" = "default" ] ; then
g=$(ip route show default | awk '/default/ {print $3; exit}')
g=$(resolve_default_gw "$ipcmd")
fi
route_via="$route_via nexthop via $g weight 1"
done
......@@ -120,11 +130,11 @@ find_group()
if [ "$ACTION" = "add" ] || [ "$ACTION" = "del" ] ; then
[ -n "$ADD_DEL_TARGET" ] && [ -n "$ADD_DEL_GROUP" ] || { echo "Usage: $0 --$ACTION IP|DOMAIN GROUP" >&2 ; exit 1 ; }
groupdir=$(find_group "$ADD_DEL_GROUP") || exit 1
read_group_config "$groupdir" || exit 1
# Detect IPv4 vs IPv6
# Detect IPv4 vs IPv6 from directory location
ipcmd="ip"
echo "$gw" | grep -q ':' && ipcmd="ip -6"
echo "$groupdir" | grep -q "$ROUTES6_DIR" && ipcmd="ip -6"
read_group_config "$groupdir" "$ipcmd" || exit 1
if [ "$ACTION" = "add" ] ; then
if is_ipv4 "$ADD_DEL_TARGET" || is_ipv6 "$ADD_DEL_TARGET" ; then
......@@ -151,9 +161,9 @@ fi
# --- Flush a specific group ---
if [ -n "$FLUSH_GROUP" ] ; then
groupdir=$(find_group "$FLUSH_GROUP") || exit 1
read_group_config "$groupdir" || exit 1
ipcmd="ip"
echo "$gw" | grep -q ':' && ipcmd="ip -6"
echo "$groupdir" | grep -q "$ROUTES6_DIR" && ipcmd="ip -6"
read_group_config "$groupdir" "$ipcmd" || exit 1
log "Flushing table $table (group $FLUSH_GROUP)"
[ -z "$SHOW" ] && $ipcmd route flush table "$table" 2>/dev/null
exit
......@@ -176,7 +186,7 @@ process_routes()
[ -d "$gwdir" ] || continue
local name=$(basename "$gwdir")
read_group_config "$gwdir" || continue
read_group_config "$gwdir" "$ipcmd" || continue
local pref=$(rule_pref "$table")
local state="$routes_dir/$name"
......@@ -328,7 +338,7 @@ if [ -n "$SET_RULES" ] ; then
for gwdir in "$routes_dir"/*/ ; do
[ -d "$gwdir" ] || continue
read_group_config "$gwdir" || continue
read_group_config "$gwdir" "$ipcmd" || continue
pref=$(rule_pref "$table")
if ! $ipcmd rule show | grep -q "lookup $table.*pref $pref" ; then
$ipcmd rule add lookup "$table" pref "$pref"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment