#!/bin/sh
# DF-3331 fix validation v2 -- run AFTER applying fix.diff (3 hunks:
# timer NOSTATE case + RESOLVE grace + cache_lladdr no-lladdr guard)
# to /usr/src, rebuilding (make nativekernel), installing and rebooting.
set -
cd "$(dirname "$0")"

IF=gif0
ifconfig $IF create 2>/dev/null || true
./nsimmort3 setup $IF || true
ifconfig $IF up
TGT=$(ifconfig $IF inet6 | awk '/fe80/ {print $2}' | head -1 | cut -d% -f1)
IDX=$(ifconfig $IF | sed -n 's/.*scopeid 0x\([0-9a-f]*\).*/\1/p' | head -1)
echo "== kernel =="
uname -a
echo "target: $TGT idx: $IDX"
echo "== baseline gif0 entries: =="
ndp -an | grep -c '%gif0' || true

echo
echo "== identical PoC: 5000 NS (no SLLA) from 5000 distinct sources =="
./nsimmort3 blast -t $TGT -g $IDX -n 5000
sleep 3
echo "-- entries 3s after attack (may exist transiently from NA-reply resolve): --"
ndp -an | grep -c '%gif0' || true
echo "-- entries after 30s (grace=nd6_delay=5s + prune sweep): EXPECT 1 (own LL only) --"
sleep 27
ndp -an | grep -c '%gif0' || true
ndp -an | grep '%gif0' || true
echo "-- total NC / routes: --"
ndp -an | wc -l
netstat -rn -f inet6 | wc -l

echo
echo "== scale: 20000 more, then 30s wait: count must return to baseline =="
./nsimmort3 blast -t $TGT -g $IDX -n 20000 -S 0x10000
sleep 30
ndp -an | grep -c '%gif0' || true

echo
echo "== functionality 1: NS WITH SLLA still creates an entry =="
./nsimmort3 blast -t $TGT -g $IDX -n 2 -s -S 0xdead
sleep 1
ndp -an | grep -iE '%gif0' | head -5 || true

echo
echo "== functionality 2: normal v6 traffic still works =="
ping6 -c1 ::1 >/dev/null 2>&1 && echo "ping6 ::1 OK" || echo "ping6 ::1 FAIL"
ping6 -c1 -t1 fe80::5054:ff:fe12:3456%vtnet0 >/dev/null 2>&1 && echo "ping6 vtnet0-LL OK" || echo "ping6 vtnet0-LL FAIL(nd)"
ndp -an | head -8
echo
echo "FIX-RUN-V2 DONE"
