#!/bin/sh
# DF-3449 fix validation - runs on the guest with the PATCHED dummynet3.ko.
# Expected: unload refused (EBUSY) while a pipe rule exists; after the rule
# is deleted the unload succeeds; traffic keeps flowing at every step; a new
# pipe rule is rejected after the unload (module unregistered).
exec >/root/df3449fix.log 2>&1
set -x
date
uname -a

sysctl net.filters_default_to_accept=1
kldload /usr/obj/usr/src/sys/net/ipfw3/ipfw3.ko || exit 1
kldload /usr/obj/usr/src/sys/net/ipfw3_basic/ipfw3_basic.ko || exit 1
kldload /usr/obj/usr/src/sys/net/dummynet3/dummynet3.ko || exit 1

ipfw3 pipe 1 config bw 1Mbit/s delay 10ms
ipfw3 add 100 pipe 1 ip from any to any
ping -c 2 -t 5 127.0.0.1

# unload must now be REFUSED while rule 100 references module 2
kldunload dummynet3
echo "UNLOAD_WITH_RULE_RC=$?"
kldstat | grep dummynet

# traffic still flows (module never went away)
ping -c 2 -t 5 127.0.0.1
echo "PING_WITH_RULE_AFTER_REFUSED_UNLOAD_OK"

# remove the rule, then unload must succeed
ipfw3 delete 100
kldunload dummynet3
echo "UNLOAD_AFTER_DELETE_RC=$?"
kldstat | grep dummynet || echo "dummynet3 no longer loaded"

# traffic still flows: no stale filter_funcs, no rules referencing module 2
ping -c 2 -t 5 127.0.0.1
echo "PING_AFTER_UNLOAD_OK"

# a new pipe rule must be rejected: module no longer registered
ipfw3 add 200 pipe 1 ip from any to any
echo "ADDRULE_AFTER_UNLOAD_RC=$?"
echo "FIX_VALIDATED"
