#!/bin/sh
# Watch the bearer's public SLAAC prefix and keep it routed to the LAN.
#
# The carrier hands out one /64 by RA on sipa_eth0, and odhcpd relays it to LAN
# clients, which SLAAC their own public addresses out of it. Replies to those
# addresses (NAT66 DNAT on the way back, or any traffic inside the prefix) must
# be delivered on br-lan - but no on-link route for the prefix exists there:
# mu300cell.sh flushes the bearer's v6 routes on every setup, so a lookup for
# a client address falls through to the default route and the reply leaves via
# the modem towards the carrier gateway, where it disappears. Measured: every
# connection from a client address without a pinned /128 timed out (stable
# SLAAC addresses never made it into the neighbour table, so only the privacy
# addresses ever got pinned), and Android clients reacted by suppressing AAAA
# queries for the whole network - "DNS resolves nothing over IPv6, IPv6 looks
# dead end to end" - until the next reconnect forgot the verdict.
#
# The /64 on br-lan is the mechanism: NDP there resolves each client on
# demand, with no race for addresses that never showed up in the neighbour
# table. The prefix is tracked so a carrier renumber replaces the route
# instead of stacking a stale one. Its metric is below the RA's own route for
# the same /64 on sipa_eth0 (metric 256, re-created by the carrier's RA after
# every accept_ra cycle): the lower metric wins, so replies go to the LAN, not
# back out the bearer. Nothing legitimate in the /64 lives behind sipa_eth0,
# and the router's own address there is in the local table, which a main-table
# route cannot override.
#
# Nothing is learnt from the LAN (security review of the port): the fork also
# pinned a /128 to br-lan for every global address in br-lan's neighbour table,
# "a small latency optimisation". Any LAN client can put any address there with
# a spoofed neighbour advertisement, so that pinned another client's address,
# the router's own, or an internet host's (a DNS server, say) to br-lan: a route
# hijack. The pins are left out; the /64 route does the work. Every value that
# reaches `ip` is checked to be a plain IPv6 address inside the bearer's /64.
#
# Every /64 the bearer holds is routed, not only one (#87). A PDP re-activation the event monitor rides out without
# an ifdown (+CGEV, mu300cell-v6.sh) can bring a new prefix by RA while the old SLAAC address stays on sipa_eth0
# (this carrier's lifetimes are infinite; only setup and teardown flush it). odhcpd relays the new RA, so LAN clients
# SLAAC in the new /64 - and this script routed whichever of the two /proc/net/if_inet6 listed first, so clients on
# the other one lost every NAT66 reply until a WAN restart flushed the old address. The relay itself never withdraws
# a prefix the carrier stopped announcing (odhcpd forwards the upstream RA's PIOs as they are and keeps no prefix
# state), so clients keep their old-prefix addresses for those prefixes' lifetimes either way.
#
# So this script withdraws such a prefix itself (ra-deprecate): an RA on br-lan with preferred lifetime 0 for the old
# /64, sent once a different /64 is on the bearer, in three rounds (an RA is a multicast that a client can miss).
# Not as soon as it leaves: a WAN restart flushes the bearer's address and the carrier usually gives the same /64
# back, and deprecating that one would take the live prefix from every client until the carrier's next RA.
#
# Relay mode of mu300cell only (K37, spec D9; openwrt-luci): init.d/mu300-ndp starts this when network.wan.ipv6 is
# relay. Ported from kanoqwq/clean-tf-7.2 (cd4fb28, 89b376d), without the neighbour pins.
# ndp-learn --prefix: print the bearer's /64s, one per line (nothing when it has none), and exit.
# ndp-learn --once: one round (no router solicitation, no sleep) and exit.

# fixed-width hex, one line per address: address, ifindex, prefix length, scope, flags, name
SIM_SLOT=0
if [ -x /opt/mu300/bin/mu300-sim ]; then
    SIM_SLOT=$(/opt/mu300/bin/mu300-sim active) || exit 1
fi
IFACE=sipa_eth$((SIM_SLOT * 8))
IF_INET6=${MU300_IF_INET6:-/proc/net/if_inet6}

# The bearer's global /64s, from $IF_INET6 (the kernel's own addresses, never the LAN's), sorted, each once. That
# file uses fixed-width hex, so this also handles prefixes whose first four hextets are compressed by `ip -6 addr`.
# Global unicast only (2000::/3): never link-local, multicast or anything else a misconfigured scope could let through.
bearer_prefix() {
    awk -v iface="$IFACE" '$6==iface && $4=="00" && $1 ~ /^[23][0-9a-f]{31}$/ {
        p="";
        for (i=1; i<=4; i++) {
            h=substr($1,(i-1)*4+1,4); sub(/^0+/,"",h);
            if (h=="") h="0";
            p=p (i>1?":":"") h;
        }
        print p "::/64"
    }' "$IF_INET6" 2>/dev/null | sort -u
}

# in_prefix ADDR STEM: ADDR is a plain IPv6 address (hex digits and colons only, so nothing ip could read as an
# option or a second argument) that starts with STEM: (inside the /64), and is not the subnet-router anycast STEM::.
in_prefix() {
    case $1 in
        ''|-*|*[!0-9a-f:]*) return 1 ;;
        "$2::") return 1 ;;
        "$2:"*) return 0 ;;
    esac
    return 1
}

# Below the RA route's 256 (see above). Before review I2 this was 1024, which lost to the RA route; a route an
# earlier ndp-learn installed at that metric is removed, so it never lingers beside this one.
LAN_METRIC=128
OLD_METRIC=1024

# lan_route PREFIX: route PREFIX to br-lan. replace is idempotent, so every round can run it: one route, never two.
lan_route() {
    ip -6 route replace "$1" dev br-lan metric $LAN_METRIC 2>/dev/null
    ip -6 route del "$1" dev br-lan metric $OLD_METRIC 2>/dev/null
}

drop_old_prefix() {
    [ -n "$1" ] || return 0
    ip -6 route del "$1" dev br-lan metric $LAN_METRIC 2>/dev/null
    ip -6 route del "$1" dev br-lan metric $OLD_METRIC 2>/dev/null
}

# has WORD LIST: WORD is one of the whitespace-separated words of LIST
has() {
    case " $(echo $2) " in
        *" $1 "*) return 0 ;;
    esac
    return 1
}

# router_lifetime: what is left of the carrier's default router on the bearer, in seconds (1-9000), for the
# withdrawing RA; 1800 when the route shows no expiry
router_lifetime() {
    ip -6 route show default dev "$IFACE" 2>/dev/null | awk '{
        for (i = 1; i < NF; i++) if ($i == "expires") { v = $(i + 1); sub(/sec$/, "", v) }
    } END {
        if (v !~ /^[0-9]+$/ || v + 0 < 1) v = 1800; else if (v + 0 > 9000) v = 9000
        print v + 0
    }'
}

# Prefixes that left the bearer (gone), and the ones being withdrawn from the LAN with the rounds left (withdraw,
# withdraw_rounds). See the top for why a prefix waits in gone until another one is there.
WITHDRAW_ROUNDS=3
RA_DEPRECATE=${MU300_RA_DEPRECATE:-/opt/mu300/bin/ra-deprecate}
gone=
withdraw=
withdraw_rounds=0
withdraw_old() {
    local p g= w=
    for p in $last_prefixes; do
        has "$p" "$prefixes" || gone="$gone $p"
    done
    # one that came back (a WAN restart that got the same /64) is neither waiting nor withdrawn any more
    for p in $gone; do
        has "$p" "$prefixes" || has "$p" "$g" || g="$g $p"
    done
    for p in $withdraw; do
        has "$p" "$prefixes" || w="$w $p"
    done
    gone=$(echo $g); withdraw=$(echo $w)
    [ -n "$prefixes" ] || return 0
    if [ -n "$gone" ]; then
        for p in $gone; do
            has "$p" "$withdraw" || withdraw="$withdraw $p"
        done
        withdraw=$(echo $withdraw); withdraw_rounds=$WITHDRAW_ROUNDS; gone=
        logger -t ndp-learn "withdrawing from the LAN: $withdraw"
    fi
    [ -n "$withdraw" ] && [ "$withdraw_rounds" -gt 0 ] || return 0
    withdraw_rounds=$((withdraw_rounds - 1))
    # shellcheck disable=SC2086
    "$RA_DEPRECATE" br-lan "$(router_lifetime)" $withdraw ||
        logger -t ndp-learn "could not withdraw $withdraw (ra-deprecate)"
}

# The prefixes routed last round (space-separated). A prefix that left the bearer loses its br-lan route.
last_prefixes=
learn_once() {
    prefixes=$(bearer_prefix)
    for p in $last_prefixes; do
        has "$p" "$prefixes" || drop_old_prefix "$p"
    done
    withdraw_old
    for p in $prefixes; do
        has "$p" "$last_prefixes" || logger -t ndp-learn "LAN prefix route: $p dev br-lan metric $LAN_METRIC"
        # every round, so a route someone or something removed comes back within one round
        lan_route "$p"
    done
    last_prefixes=$(echo $prefixes)
    [ -n "$prefixes" ] || return 0
    # odhcpd's NDP relay can learn a downstream client's /128 on the WAN side.
    # 91-mu300-luci leaves NDP relay off (review I1), but a hand-made config can
    # turn it back on, and that more-specific route overrides the LAN /64 and
    # blackholes NAT66 replies. Remove only host routes inside the shared
    # prefixes; the LAN /64 resolves the client by NDP even before it appears in
    # the neighbour table.
    for r in $(ip -6 route show dev "$IFACE" 2>/dev/null | awk '$1 !~ /\// {print $1}'); do
        for p in $prefixes; do
            in_prefix "$r" "${p%::/64}" && { ip -6 route del "$r/128" dev "$IFACE" 2>/dev/null; break; }
        done
    done
    return 0
}

case ${1:-} in
    --prefix) bearer_prefix; exit 0 ;;
    --once) learn_once; exit 0 ;;
esac

# The daemon (FINDINGS 235). A round looks at the kernel's addresses (/proc/net/if_inet6, a few lines, read by the shell
# itself) and, every ROUTE_EVERY rounds, at the routing table's lines of the bearer's /64s (one grep); learn_once (two
# ip calls per prefix, the host routes, the withdrawal) runs only when one of them changed since its last run, or a
# withdrawal still has rounds to go. A route someone removed comes back within ROUTE_EVERY rounds (30 s), a new or
# changed prefix within one round, as before; an unchanged bearer costs no process at all, where every round used to
# start a dozen (ip, awk, grep, subshells: 11 ticks a minute).
ROUTE_EVERY=${MU300_NDP_ROUTE_EVERY:-3}
INTERVAL=${MU300_NDP_INTERVAL:-10}   # seconds between two rounds
IPV6_ROUTE=${MU300_IPV6_ROUTE:-/proc/net/ipv6_route}
# bearer_look: the addresses as they are (_addrs), the bearer's global /64s as the routing table writes them (_stems:
# the first 16 hex digits of each 2000::/3 address) and whether it has any global address (_global)
bearer_look() {
    _addrs= _stems= _global=0
    { while read -r _ad _ix _pl _sc _fl _nm; do
        _addrs="$_addrs $_ad/$_pl/$_sc/$_nm"
        [ "$_nm" = "$IFACE" ] && [ "$_sc" = 00 ] || continue
        _global=1
        case $_ad in [23]???????????????????????????????) ;; *) continue ;; esac
        _s=${_ad%????????????????}
        case " $_stems " in *" $_s "*) ;; *) _stems="$_stems $_s" ;; esac
    done < "$IF_INET6"; } 2>/dev/null
}
# routes_now: the routing table's lines of those /64s (ours on br-lan, the RA's, host routes), into _routes
routes_now() {
    _routes=
    [ -n "$_stems" ] || return 0
    set --
    for _s in $_stems; do set -- "$@" -e "$_s"; done
    _routes=$(grep -F "$@" "$IPV6_ROUTE" 2>/dev/null)
}
# nap SECONDS: on a fifo nobody writes to (read -t: no sleep process) where the shell has it, else sleep; the wait's
# timer may fire up to half a second late, so the kernel can serve the timers of several daemons with one wakeup
wait_for() { sleep "$1"; }
read_t=0
nap_d=$(mktemp -d "${TMPDIR:-/tmp}/ndp-learn.XXXXXX" 2>/dev/null) || nap_d=
if [ -n "$nap_d" ] && mkfifo "$nap_d/wait" 2>/dev/null; then
    exec 9<>"$nap_d/wait"
    if case ${BASH_VERSION:-} in [123].*) false ;; esac && { read -t 0 _n <&9; [ $? = 1 ]; } 2>/dev/null; then
        read_t=1
        wait_for() { read -t "$1" _n <&9 || :; }
    else
        exec 9<&-
    fi
fi
[ -z "$nap_d" ] || rm -rf "$nap_d"
slack=; { read -r slack < /proc/$$/timerslack_ns; } 2>/dev/null || slack=
nap() {
    [ -z "$slack" ] || { echo 500000000 > /proc/$$/timerslack_ns; } 2>/dev/null
    wait_for "$1"
    [ -z "$slack" ] || { echo "$slack" > /proc/$$/timerslack_ns; } 2>/dev/null
}

# The kernel's word instead of a round every INTERVAL seconds (FINDINGS 235). With iproute2's monitor (OpenWrt's
# ip-full; busybox's ip has none) `ip -o -6 monitor address route` runs as a child and writes a line per change into a
# fifo this reads with read -t: no process and no wakeup while the kernel says nothing. An address line (any
# interface's: the round compares them all, as before) starts a round, which reads if_inet6 itself; a route line of
# br-lan or the bearer starts one that also greps the routing table's lines of the /64s (one process). So a new or
# changed prefix is routed at once instead of within 10 s, and a route someone removed comes back at once instead of
# within 30 s; the routes learn_once itself replaces come back as lines too, and find nothing changed. The lines of one
# change come in a burst (an ifup, a flush): the round comes once they have stopped for a second (at most 5 s).
# Timers stay for what no line says: a round that also looks at the routes every SAFETY seconds (300), the
# withdrawal's rounds INTERVAL apart, and the self-heal below - while the bearer has no global address it solicits
# after INTERVAL seconds, then after twice as long each time up to SAFETY, and from INTERVAL again when the addresses
# change (a bearer that comes up gets its link-local address first). Every 10 s for ever was what an IPv4-only bearer
# got: a router solicitation sent over the radio, and a sleep process, six times a minute.
# Without a monitor (busybox's ip, a shell whose read has no timeout, a monitor that ended) the rounds come every
# INTERVAL seconds as before; a monitor that ended after it ran is started again after SAFETY seconds, one that ended
# at once (an ip without monitor) is not. The monitor goes with this process (procd stops it with SIGTERM).
SAFETY=${MU300_NDP_SAFETY:-300}
T=0
now() { { read -r _up _ < /proc/uptime; } 2>/dev/null && T=${_up%%.*}; }
# cs: centiseconds of uptime in $C - a read of the monitor that fails before half a second has passed met the end of
# its stream, not its timeout (busybox's read says 1 for both)
C=0
cs() { { read -r _up _ < /proc/uptime; } 2>/dev/null && C=$((${_up%.*} * 100 + 1${_up#*.} - 100)); }
mon=0 mon_pid= mon_off=0 mon_t0=0 mon_retry=0 ml=
mon_start() {
    [ "$mon" = 0 ] && [ "$mon_off" = 0 ] && [ "$read_t" = 1 ] && [ "${MU300_NDP_MONITOR:-1}" = 1 ] || return 1
    # (whole seconds: the timers below count them, and OpenWrt's busybox read takes no other)
    case $INTERVAL$SAFETY in *[!0-9]*) return 1 ;; esac
    [ -r /proc/uptime ] || return 1
    _d=$(mktemp -d "${TMPDIR:-/tmp}/ndp-learn.XXXXXX" 2>/dev/null) || return 1
    if mkfifo "$_d/monitor" 2>/dev/null; then
        # (the monitor's open of the fifo waits for this one's, and this one's for the monitor's)
        ip -o -6 monitor address route > "$_d/monitor" 2>/dev/null < /dev/null 9<&- &
        mon_pid=$!
        exec 8< "$_d/monitor"
        mon=1 mon_t0=
        now; safety_at=$((T + SAFETY))
    fi
    rm -rf "$_d"
    [ "$mon" = 1 ]
}
mon_stop() {
    [ "$mon" = 1 ] || return 0
    kill "$mon_pid" 2>/dev/null
    exec 8<&-
    mon=0
}
# mon_next SECONDS: the monitor's next line in $ml; false when none came in that time or the stream ended (within 2 s
# of the first read: it never ran)
mon_next() {
    [ -n "$mon_t0" ] || { now; mon_t0=$T; }
    cs; _c0=$C
    [ -z "$slack" ] || { echo 500000000 > /proc/$$/timerslack_ns; } 2>/dev/null
    IFS= read -r -t "$1" ml <&8; _r=$?
    [ -z "$slack" ] || { echo "$slack" > /proc/$$/timerslack_ns; } 2>/dev/null
    [ "$_r" != 0 ] || return 0
    cs
    [ $((C - _c0)) -lt 50 ] || return 1
    mon_stop
    now
    if [ $((T - mon_t0)) -lt 2 ]; then
        mon_off=1
        logger -t ndp-learn "no address and route monitor (ip -6 monitor); a round every $INTERVAL s"
    else
        mon_retry=$((T + SAFETY))
        logger -t ndp-learn "the address and route monitor ended; a round every $INTERVAL s"
    fi
    return 1
}
# mon_line: the line in $ml says something a round looks at (route_due: a route of br-lan or the bearer)
mon_line() {
    case " $ml " in
        *" inet6 "*) return 0 ;;
        *" br-lan "*|*" $IFACE "*) route_due=1; return 0 ;;
    esac
    return 1
}
# mon_wait: until a line says something a round looks at (and the rest of its burst), or a timer
mon_wait() {
    route_due=0
    now
    _due=$safety_at
    [ "$withdraw_rounds" -le 0 ] || [ $((T + INTERVAL)) -ge "$_due" ] || _due=$((T + INTERVAL))
    [ "$_global" = 1 ] || [ "$heal_at" -ge "$_due" ] || _due=$heal_at
    while [ "$T" -lt "$_due" ]; do
        mon_next $((_due - T)) || break
        now
        mon_line || continue
        _end=$((T + 5))
        while [ "$T" -lt "$_end" ] && mon_next 1; do
            mon_line || :
            now
        done
        break
    done
    now
    [ "$T" -lt "$safety_at" ] || route_due=1
}
solicit() {
    echo 0 > "/proc/sys/net/ipv6/conf/$IFACE/accept_ra" 2>/dev/null
    sleep 1
    echo 2 > "/proc/sys/net/ipv6/conf/$IFACE/accept_ra" 2>/dev/null
}
trap mon_stop EXIT
trap 'exit 0' INT TERM

seen_addrs=- seen_routes=- n=0 route_due=0 safety_at=0 heal=$INTERVAL heal_at=0
mon_start || :
while :; do
    now
    bearer_look
    n=$((n + 1))
    full=0
    [ "$_addrs" = "$seen_addrs" ] || full=1
    [ "$withdraw_rounds" -le 0 ] || full=1
    if [ "$mon" = 1 ]; then rd=$route_due; else rd=0; [ $((n % ROUTE_EVERY)) != 0 ] || rd=1; fi
    if [ "$full" = 0 ] && [ "$rd" = 1 ]; then
        routes_now
        [ "$_routes" = "$seen_routes" ] || full=1
    fi
    if [ "$full" = 1 ]; then
        # new addresses: the self-heal's solicitations start over (with the monitor)
        [ "$_addrs" = "$seen_addrs" ] || heal=$INTERVAL heal_at=0
        learn_once
        seen_addrs=$_addrs
        routes_now; seen_routes=$_routes   # what learn_once left, as the next look compares it
    fi
    # (with the monitor: the routes were looked at, the next safety round is SAFETY seconds away)
    [ "$mon" = 0 ] || [ "$full$rd" = 00 ] || safety_at=$((T + SAFETY))
    # Self-heal the bearer's own public address: the carrier's RA can be missed while the
    # bearer settles after boot, and nothing else ever re-solicits - flip accept_ra to send
    # a fresh router solicitation whenever the bearer has no global address (with the
    # monitor: when the self-heal's time has come, see above).
    if [ "$mon" = 0 ]; then
        [ "$_global" = 1 ] || solicit
    else
        if [ "$_global" = 1 ]; then
            heal=$INTERVAL heal_at=0
        elif [ "$T" -ge "$heal_at" ]; then
            solicit
            heal_at=$((T + heal)); heal=$((heal * 2)); [ "$heal" -le "$SAFETY" ] || heal=$SAFETY
        fi
    fi
    if [ "$mon" = 1 ]; then
        mon_wait
    else
        nap "$INTERVAL"
        [ "$mon_retry" = 0 ] || { now; [ "$T" -lt "$mon_retry" ] || { mon_retry=0; mon_start || :; }; }
    fi
done
