#!/bin/sh
# DF-3448: sysctl_ip6_tempvltime restores the WRONG variable on the EINVAL
# path (sys/netinet6/in6_proto.c:437-453).
#
# Expected (correct) behavior when writing an invalid vltime (< pltime):
#   - write fails with EINVAL
#   - net.inet6.ip6.tempvltime keeps its OLD value
#   - net.inet6.ip6.temppltime is untouched
#
# Actual (buggy) behavior on DragonFly:
#   - write fails with EINVAL
#   - net.inet6.ip6.tempvltime KEEPS THE REJECTED VALUE
#   - net.inet6.ip6.temppltime is CLOBBERED with the old vltime
#
# Must run as root (sysctl writes are privileged).

PL=net.inet6.ip6.temppltime
VL=net.inet6.ip6.tempvltime

echo "== baseline =="
echo "pltime=$(sysctl -n $PL)  vltime=$(sysctl -n $VL)"

# Make the state deterministic: pltime=86400 (default), vltime=604800 (default)
sysctl -w $PL=86400 >/dev/null
sysctl -w $VL=604800 >/dev/null
echo "== reset to defaults =="
echo "pltime=$(sysctl -n $PL)  vltime=$(sysctl -n $VL)"

# Now try to set vltime=100, which is < pltime (86400): kernel must reject it.
echo "== attempting invalid write: tempvltime=100 (100 < pltime 86400) =="
sysctl -w $VL=100
echo "sysctl exit status: $?"

echo "== state after rejected write =="
echo "pltime=$(sysctl -n $PL)  vltime=$(sysctl -n $VL)"

if [ "$(sysctl -n $VL)" = "100" ]; then
	echo "BUG PART 1 CONFIRMED: rejected tempvltime=100 persisted (should still be 604800)"
fi
if [ "$(sysctl -n $PL)" = "604800" ]; then
	echo "BUG PART 2 CONFIRMED: temppltime clobbered to 604800 (old vltime; should still be 86400)"
fi
