| 1 | #!/bin/bash |
| 2 | # librist. Copyright © 2026 SipRadius LLC. |
| 3 | # SPDX-License-Identifier: BSD-2-Clause |
| 4 | # |
| 5 | # Regression for the bonded EAP-SRP sender-leg re-authentication bug |
| 6 | # (upstream issue #224). |
| 7 | # |
| 8 | # A bonded caller (sender) with EAP-SRP has two legs to one listening |
| 9 | # receiver. One leg is silenced (100% packet loss BOTH ways) long enough |
| 10 | # for the listener to time out and purge that leg's peer, while the other |
| 11 | # leg keeps the bond -- and the caller process -- alive. Crucially the |
| 12 | # silenced leg's interface stays UP, so its miface-bound UDP socket is |
| 13 | # never torn down: when the loss clears the leg resumes from the EXACT |
| 14 | # same source tuple, with the caller still in EAP SUCCESS. The listener |
| 15 | # purged the session and now silently drops the caller's data while it |
| 16 | # waits for an EAPOL START the caller never sends (EAP is authenticator- |
| 17 | # driven and the caller believes it is still authenticated). The leg is |
| 18 | # wedged forever. |
| 19 | # |
| 20 | # This is why the reproduction needs real interfaces + tc packet loss and |
| 21 | # cannot be an in-process loopback test: a loopback leg has no miface |
| 22 | # binding, so it is recreated on timeout with a fresh ephemeral port and |
| 23 | # self-heals with a cold handshake -- hiding the bug. Likewise an |
| 24 | # "ip link set ... down" makes the sends fail and forces the same |
| 25 | # new-port reconnect. Only same-tuple silence reproduces the wedge. |
| 26 | # |
| 27 | # The fix drives the caller to reset EAP and re-run the SRP handshake on |
| 28 | # that leg once it has been silent past session_timeout, so the leg |
| 29 | # re-authenticates AND rejoins the weighted bond on its own. |
| 30 | # |
| 31 | # Two discriminators (the receiver is a stable listener the whole time, so |
| 32 | # the flow never resets): |
| 33 | # 1. Successful EAP authentications. Two legs authenticate at startup; a |
| 34 | # third is only possible if the flapped leg re-authenticated on its own. |
| 35 | # Before any fix it stays wedged at two and the receiver floods |
| 36 | # "handshake is still pending". |
| 37 | # 2. Legs the sender is balancing over after recovery, counted as the |
| 38 | # distinct mifaces in the final sender-stats line. A leg only appears |
| 39 | # there while it is authenticated and in the balancing rotation, so this |
| 40 | # catches the reintegration defect where a leg re-authenticates but is |
| 41 | # left out of balancing and streams only NACK retransmits (returns 1 |
| 42 | # instead of 2). |
| 43 | # |
| 44 | # Usage: test_bonded_leg_flap_netns.sh <ristsender> <ristreceiver> |
| 45 | # Exit: 0 = leg re-authenticated and rejoined the bond (fixed) |
| 46 | # 1 = leg wedged or did not rejoin balancing (bug) |
| 47 | # 77 = SKIP (needs Linux root + netns + tc) 99 = setup error |
| 48 | set -u |
| 49 | |
| 50 | TX="${1:-}" |
| 51 | RX="${2:-}" |
| 52 | USER=flapuser; PASS=flappass; CN=testcn |
| 53 | RXIP=10.0.1.51 |
| 54 | NS=rist224_snd |
| 55 | RXLOG="$(mktemp)"; TXLOG="$(mktemp)" |
| 56 | |
| 57 | skip() { echo "SKIP: $*"; cleanup 2>/dev/null; exit 77; } |
| 58 | setup_err() { echo "SETUP ERROR: $*"; cleanup 2>/dev/null; exit 99; } |
| 59 | |
| 60 | cleanup() { |
| 61 | ip netns del "$NS" 2>/dev/null |
| 62 | ip link del veth-a 2>/dev/null |
| 63 | ip link del veth-b 2>/dev/null |
| 64 | ip link del rbr0 2>/dev/null |
| 65 | [ -n "${TXPID:-}" ] && kill "$TXPID" 2>/dev/null |
| 66 | [ -n "${RXPID:-}" ] && kill "$RXPID" 2>/dev/null |
| 67 | [ -n "${FEEDPID:-}" ] && kill "$FEEDPID" 2>/dev/null |
| 68 | rm -f "$RXLOG" "$TXLOG" 2>/dev/null |
| 69 | } |
| 70 | trap cleanup EXIT |
| 71 | |
| 72 | # ---- environment gate: skip cleanly where we cannot reproduce ---------- |
| 73 | [ "$(uname -s)" = "Linux" ] || skip "not Linux" |
| 74 | [ "$(id -u)" = "0" ] || skip "needs root (CAP_NET_ADMIN) for netns/veth/tc" |
| 75 | [ -n "$TX" ] && [ -x "$TX" ] || setup_err "ristsender not found ($TX)" |
| 76 | [ -n "$RX" ] && [ -x "$RX" ] || setup_err "ristreceiver not found ($RX)" |
| 77 | command -v ip >/dev/null 2>&1 || skip "iproute2 'ip' missing" |
| 78 | command -v tc >/dev/null 2>&1 || skip "iproute2 'tc' missing" |
| 79 | ip netns add "$NS" 2>/dev/null || skip "cannot create netns (no CAP_NET_ADMIN?)" |
| 80 | |
| 81 | # ---- topology: bridge in root ns, two veth legs into a sender netns ----- |
| 82 | ip link add rbr0 type bridge || setup_err "bridge add" |
| 83 | ip addr add ${RXIP}/24 dev rbr0 || setup_err "bridge addr" |
| 84 | ip link set rbr0 up || setup_err "bridge up" |
| 85 | |
| 86 | ip link add veth-a type veth peer name sa || setup_err "veth-a" |
| 87 | ip link add veth-b type veth peer name sb || setup_err "veth-b" |
| 88 | ip link set veth-a master rbr0; ip link set veth-a up |
| 89 | ip link set veth-b master rbr0; ip link set veth-b up |
| 90 | ip link set sa netns "$NS"; ip link set sb netns "$NS" |
| 91 | ip netns exec "$NS" ip addr add 10.0.1.20/24 dev sa |
| 92 | ip netns exec "$NS" ip addr add 10.0.1.122/24 dev sb |
| 93 | ip netns exec "$NS" ip link set sa up |
| 94 | ip netns exec "$NS" ip link set sb up |
| 95 | ip netns exec "$NS" ip link set lo up |
| 96 | # same-subnet multi-homing: disable rp_filter so miface egress is kept |
| 97 | ip netns exec "$NS" sysctl -qw net.ipv4.conf.all.rp_filter=0 |
| 98 | ip netns exec "$NS" sysctl -qw net.ipv4.conf.sa.rp_filter=0 |
| 99 | ip netns exec "$NS" sysctl -qw net.ipv4.conf.sb.rp_filter=0 |
| 100 | sysctl -qw net.ipv4.conf.all.rp_filter=0 >/dev/null 2>&1 |
| 101 | |
| 102 | ip netns exec "$NS" ping -c1 -W2 -I sa ${RXIP} >/dev/null 2>&1 || skip "leg A no connectivity" |
| 103 | ip netns exec "$NS" ping -c1 -W2 -I sb ${RXIP} >/dev/null 2>&1 || skip "leg B no connectivity" |
| 104 | |
| 105 | # ---- receiver: listener, advanced profile, SRP authenticator ----------- |
| 106 | $RX -p 2 -v 6 \ |
| 107 | -i "rist://@${RXIP}:2030?username=${USER}&password=${PASS}" \ |
| 108 | -o "udp://127.0.0.1:12345" >"$RXLOG" 2>&1 & |
| 109 | RXPID=$! |
| 110 | sleep 1 |
| 111 | |
| 112 | # udp feeder inside the sender netns -> ristsender input |
| 113 | ip netns exec "$NS" bash -c 'while :; do printf "RISTTESTPACKET%08d" $RANDOM > /dev/udp/127.0.0.1/5556; sleep 0.02; done' & |
| 114 | FEEDPID=$! |
| 115 | sleep 0.5 |
| 116 | |
| 117 | # ---- sender: bonded caller, two SRP legs (same cname + creds) ---------- |
| 118 | ip netns exec "$NS" $TX -p 2 -v 6 \ |
| 119 | -i "udp://@127.0.0.1:5556" \ |
| 120 | -o "rist://${RXIP}:2030?miface=sa&weight=10&username=${USER}&password=${PASS}&cname=${CN},rist://${RXIP}:2030?miface=sb&weight=5&username=${USER}&password=${PASS}&cname=${CN}" \ |
| 121 | >"$TXLOG" 2>&1 & |
| 122 | TXPID=$! |
| 123 | |
| 124 | echo "== warmup 12s (both legs authenticate + stream) ==" |
| 125 | sleep 12 |
| 126 | auths_setup=$(grep -c "Successfully authenticated" "$RXLOG") |
| 127 | echo "after warmup: successful auths=${auths_setup}" |
| 128 | if [ "$auths_setup" -lt 2 ]; then |
| 129 | echo "INDETERMINATE: both legs did not authenticate before the flap." |
| 130 | exit 99 |
| 131 | fi |
| 132 | |
| 133 | echo "== silence leg B (sb): 100% loss both ways, interface stays UP, 10s ==" |
| 134 | ip netns exec "$NS" tc qdisc add dev sb root netem loss 100% || setup_err "tc add sb" |
| 135 | tc qdisc add dev veth-b root netem loss 100% || setup_err "tc add veth-b" |
| 136 | sleep 10 |
| 137 | echo "== leg B restored (same socket / same source tuple) ==" |
| 138 | ip netns exec "$NS" tc qdisc del dev sb root 2>/dev/null |
| 139 | tc qdisc del dev veth-b root 2>/dev/null |
| 140 | echo "== observe 28s (leg B must re-authenticate AND rejoin balancing) ==" |
| 141 | sleep 28 |
| 142 | |
| 143 | auths_final=$(grep -c "Successfully authenticated" "$RXLOG") |
| 144 | flood=$(grep -c "handshake is still pending" "$RXLOG") |
| 145 | resets=$(grep -c "reset EAP and re-initiated the SRP" "$TXLOG") |
| 146 | |
| 147 | # Reintegration check: the sender only lists a leg in its per-peer stats |
| 148 | # while that leg is authenticated and in the weighted balancing rotation, so |
| 149 | # the number of distinct mifaces in the final sender-stats line is the number |
| 150 | # of legs actually carrying balanced data. Re-authenticating alone is not |
| 151 | # enough -- a leg that re-auths but is left out of balancing streams only NACK |
| 152 | # retransmits and never returns to its share (that was the original defect). |
| 153 | last_sstats=$(grep '"sender-stats"' "$TXLOG" | tail -1) |
| 154 | legs_balancing=$(printf '%s' "$last_sstats" | grep -o '"miface":"[^"]*"' | sort -u | wc -l) |
| 155 | legs_balancing=$(printf '%s' "$legs_balancing" | tr -d ' ') |
| 156 | echo "== after flap: successful auths ${auths_setup} -> ${auths_final}, "\ |
| 157 | "handshake-pending flood=${flood}, sender EAP resets=${resets}, "\ |
| 158 | "legs balancing after recovery=${legs_balancing} ==" |
| 159 | |
| 160 | # A fresh EAP authentication after the flap proves the wedged leg |
| 161 | # re-handshook on its own. Without the fix the count never moves and the |
| 162 | # receiver floods "handshake is still pending". |
| 163 | if [ "$auths_final" -le "$auths_setup" ]; then |
| 164 | echo "FAIL (issue #224): the flapped SRP leg never re-authenticated; it"\ |
| 165 | "stayed wedged in EAP SUCCESS while the listener waited for an"\ |
| 166 | "EAPOL START it never sent (successful auths stuck at ${auths_final},"\ |
| 167 | "handshake-pending flood=${flood})." |
| 168 | exit 1 |
| 169 | fi |
| 170 | |
| 171 | if [ "${legs_balancing:-0}" -lt 2 ]; then |
| 172 | echo "FAIL (issue #224 reintegration): the flapped leg re-authenticated"\ |
| 173 | "(successful auths ${auths_setup} -> ${auths_final}) but did not rejoin"\ |
| 174 | "the weighted bond; the sender balances over only ${legs_balancing} leg"\ |
| 175 | "so the returning leg carries no share." |
| 176 | exit 1 |
| 177 | fi |
| 178 | |
| 179 | echo "PASS: the flapped SRP leg reset EAP, re-authenticated (successful auths"\ |
| 180 | "${auths_setup} -> ${auths_final}) and rejoined the weighted bond"\ |
| 181 | "(${legs_balancing} legs balancing) on its own." |
| 182 | exit 0 |