// Copyright (c) 2026 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ut_test

import (
	"net"
	"testing"

	"github.com/gopacket/gopacket/layers"
	. "github.com/onsi/gomega"
	"github.com/vishvananda/netlink"

	"github.com/projectcalico/calico/felix/bpf/conntrack"
	ctv4 "github.com/projectcalico/calico/felix/bpf/conntrack/v4"
	"github.com/projectcalico/calico/felix/bpf/ifstate"
	"github.com/projectcalico/calico/felix/bpf/maps"
	"github.com/projectcalico/calico/felix/bpf/routes"
	"github.com/projectcalico/calico/felix/ip"
)

// These tests drive the conntrack forwarding-hint state machine - the
// tunnel/pinned/checked leg flags - against crafted CT map state:
//
//   - the reply-side validator (ct_leg_validate_fwd), reached through the
//     from-workload program, and
//   - the ingress-mismatch reconcile arms, reached through the from-host
//     program with the RPF mode of each arm.
//
// Kernel FIB answers are made deterministic the same way as
// tunnel_mark_test.go: a dummy device plus a kernel route pointing at it.

const (
	ctPinWlIfindex = 1 // the BPF UT skb ifindex; the local workload leg must match it
	ctPinSvcPort   = 8080
	ctPinBackPort  = 8055
)

// ctPinFixture holds the devices the tests resolve or seed hints against.
type ctPinFixture struct {
	tunl  netlink.Link // the device kernel routes resolve to
	phys  netlink.Link // a second device to seed "wrong device" hints with
	phys2 netlink.Link // a non-encap device for kernel routes to resolve to
	ctMap maps.Map
}

func (f ctPinFixture) leg(t *testing.T, k ctv4.Key, b2a bool) ctv4.Leg {
	bs, err := f.ctMap.Get(k.AsBytes())
	Expect(err).NotTo(HaveOccurred(), "CT entry disappeared")
	d := ctv4.ValueFromBytes(bs).Data()
	if b2a {
		return d.B2A
	}
	return d.A2B
}

func setupCtPinFixture(t *testing.T, name string) ctPinFixture {
	RegisterTestingT(t) // rebind Gomega so a failure stops this subtest, not the parent

	bpfIfaceName = name
	t.Cleanup(func() { bpfIfaceName = "" })
	t.Cleanup(cleanUpMaps)

	// runBpfTest asserts the mark is clean on entry for from-* sections, and
	// a previous subtest's program run leaves its mark bits behind.
	skbMark = 0

	tunl := createHostIf("ctp_tunl0")
	t.Cleanup(func() { _ = netlink.LinkDel(tunl) })
	Expect(netlink.LinkSetUp(tunl)).NotTo(HaveOccurred())

	phys := createHostIf("ctp_phys0")
	t.Cleanup(func() { _ = netlink.LinkDel(phys) })
	Expect(netlink.LinkSetUp(phys)).NotTo(HaveOccurred())

	// The validator claims TUNNEL only on a device ifstate shows as
	// encapsulating, as the endpoint manager publishes it.
	phys2 := createHostIf("ctp_phys1")
	t.Cleanup(func() { _ = netlink.LinkDel(phys2) })
	Expect(netlink.LinkSetUp(phys2)).NotTo(HaveOccurred())

	ctPinIfState(t, tunl, ifstate.FlgVxlan)
	ctPinIfState(t, phys, ifstate.FlgHEP)
	ctPinIfState(t, phys2, ifstate.FlgHEP)

	ctMap := conntrack.Map()
	Expect(ctMap.EnsureExists()).NotTo(HaveOccurred())
	resetCTMap(ctMap)
	t.Cleanup(func() { resetCTMap(ctMap) })
	t.Cleanup(func() { resetRTMap(rtMap) })

	hostIP = node1ip

	// The local workload the packets come from / go to.
	rtKey := routes.NewKey(srcV4CIDR).AsBytes()
	rtVal := routes.NewValueWithIfIndex(routes.FlagsLocalWorkload|routes.FlagInIPAMPool, ctPinWlIfindex).AsBytes()
	Expect(rtMap.Update(rtKey, rtVal)).NotTo(HaveOccurred())

	return ctPinFixture{tunl: tunl, phys: phys, phys2: phys2, ctMap: ctMap}
}

// ctPinIfState publishes link in the ifstate map with the given type flags.
func ctPinIfState(t *testing.T, link netlink.Link, flags uint32) {
	k := ifstate.NewKey(uint32(link.Attrs().Index))
	v := ifstate.NewValue(flags|ifstate.FlgIPv4Ready, link.Attrs().Name,
		-1, -1, -1, -1, -1, -1, -1, -1)
	Expect(ifstateMap.Update(k.AsBytes(), v.AsBytes())).NotTo(HaveOccurred())
	t.Cleanup(func() { _ = ifstateMap.Delete(k.AsBytes()) })
}

// kernelRoute installs a kernel route for cidr via the given device so
// bpf_fib_lookup resolves it deterministically.
func (f ctPinFixture) kernelRoute(t *testing.T, cidr *net.IPNet, link netlink.Link) {
	rt := &netlink.Route{Dst: cidr, LinkIndex: link.Attrs().Index}
	Expect(netlink.RouteAdd(rt)).NotTo(HaveOccurred())
	t.Cleanup(func() { _ = netlink.RouteDel(rt) })
}

// tunneledCaliRoute marks cidr in the BPF routes map as a remote workload
// reached over the overlay (tunneled, not same-subnet).
func (f ctPinFixture) tunneledCaliRoute(t *testing.T, cidr *net.IPNet) {
	Expect(rtMap.Update(
		routes.NewKey(ip.CIDRFromIPNet(cidr).(ip.V4CIDR)).AsBytes(),
		routes.NewValueWithNextHop(
			routes.FlagsRemoteWorkload|routes.FlagInIPAMPool|routes.FlagTunneled,
			ip.FromNetIP(node2ip).(ip.V4Addr),
		).AsBytes(),
	)).NotTo(HaveOccurred())
}

func ctPinDstCIDR() *net.IPNet {
	return &net.IPNet{IP: net.IPv4(2, 2, 2, 0).To4(), Mask: net.CIDRMask(24, 32)}
}

// ctPinUDPPacket builds a UDP packet src -> dst on the default UDP ports.
func ctPinUDPPacket(src, dst net.IP) []byte {
	ipl := *ipv4Default
	ipl.SrcIP = src
	ipl.DstIP = dst
	ipl.Protocol = layers.IPProtocolUDP
	_, _, _, _, pkt, err := testPacketV4(nil, &ipl, udpDefault, nil)
	Expect(err).NotTo(HaveOccurred())
	return pkt
}

// TestCtLegPinValidator exercises the reply-side validator's write policy
// through the from-workload program: what it pins, what it merely stamps, and
// which flows it must leave alone.
func TestCtLegPinValidator(t *testing.T) {
	RegisterTestingT(t)

	srcPort := uint16(udpDefault.SrcPort)
	dstPort := uint16(udpDefault.DstPort)

	// The flow's key and the wrong-device hint every case starts from: the
	// opposite direction's leg records a physical device while the
	// destination needs encap.
	normalEntry := func(f ctPinFixture, hintIfindex uint32) ctv4.Key {
		key := ctv4.NewKey(17, srcIP, srcPort, dstIP, dstPort)
		// srcIP (1.1.1.1) < dstIP (2.2.2.2), so A2B is src->dst.
		legSrcToDst := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true, Opener: true,
			Workload: true, Ifindex: ctPinWlIfindex}
		legDstToSrc := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true, Ifindex: hintIfindex}
		val := ctv4.NewValueNormal(0, 0, legSrcToDst, legDstToSrc)
		Expect(f.ctMap.Update(key.AsBytes(), val.AsBytes())).NotTo(HaveOccurred())
		return key
	}

	pkt := ctPinUDPPacket(srcIP, dstIP)

	t.Run("pins the resolved egress for an encap dest with a non-tunnel hint", func(t *testing.T) {
		f := setupCtPinFixture(t, "PIN1")
		f.tunneledCaliRoute(t, ctPinDstCIDR())
		f.kernelRoute(t, ctPinDstCIDR(), f.tunl)
		key := normalEntry(f, uint32(f.phys.Attrs().Index))

		runBpfTest(t, "calico_from_workload_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(f.tunl.Attrs().Index)),
				"hint should be replaced by the FIB-resolved egress")
			Expect(leg.Tunnel).To(BeTrue())
			Expect(leg.Pinned).To(BeTrue())
			Expect(leg.Checked).To(BeTrue())
		})
	})

	t.Run("marks checked only when the hint already names the right device", func(t *testing.T) {
		f := setupCtPinFixture(t, "PIN2")
		f.tunneledCaliRoute(t, ctPinDstCIDR())
		f.kernelRoute(t, ctPinDstCIDR(), f.tunl)
		key := normalEntry(f, uint32(f.tunl.Attrs().Index))

		runBpfTest(t, "calico_from_workload_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(f.tunl.Attrs().Index)), "hint should be untouched")
			Expect(leg.Checked).To(BeTrue())
			Expect(leg.Tunnel).To(BeFalse(),
				"the kind claim belongs to the device's own program, not route inference")
			Expect(leg.Pinned).To(BeFalse(), "an honest ingress record must not become a pin")
		})
	})

	t.Run("completes the tunnel claim when confirming a loose-arm-pinned leg", func(t *testing.T) {
		f := setupCtPinFixture(t, "PIN2B")
		// The hint leg is already pinned to the tunnel by the loose arm
		// (PINNED, ifindex = tunnel) but carries no kind claim - its returns
		// ingress natively, so no device program will ever stamp it. The
		// validator, reaching here only for an encap dest whose FIB egress is
		// this same ifindex, must complete the claim the loose arm deferred.
		f.tunneledCaliRoute(t, ctPinDstCIDR())
		f.kernelRoute(t, ctPinDstCIDR(), f.tunl)
		key := ctv4.NewKey(17, srcIP, srcPort, dstIP, dstPort)
		legSrcToDst := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true, Opener: true,
			Workload: true, Ifindex: ctPinWlIfindex}
		legDstToSrc := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true,
			Pinned: true, Ifindex: uint32(f.tunl.Attrs().Index)}
		val := ctv4.NewValueNormal(0, 0, legSrcToDst, legDstToSrc)
		Expect(f.ctMap.Update(key.AsBytes(), val.AsBytes())).NotTo(HaveOccurred())

		runBpfTest(t, "calico_from_workload_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(f.tunl.Attrs().Index)), "hint should be untouched")
			Expect(leg.Pinned).To(BeTrue(), "the pin stands")
			Expect(leg.Tunnel).To(BeTrue(),
				"a pinned tunnel egress must get the kind claim the loose arm deferred to the validator")
			Expect(leg.Checked).To(BeTrue())
		})
	})

	// bpf_route_mgr flags every remote-workload route tunneled while WireGuard
	// is enabled, whatever the peer's key state, so the BPF route can want encap
	// while the kernel still routes the peer over a physical NIC. Claiming
	// TUNNEL beside that NIC would cache a device that emits the inner frame
	// raw, and nothing re-validates a checked leg.
	t.Run("does not pin an encap dest to a device that cannot encap", func(t *testing.T) {
		f := setupCtPinFixture(t, "PIN2C")
		f.tunneledCaliRoute(t, ctPinDstCIDR())
		f.kernelRoute(t, ctPinDstCIDR(), f.phys2)
		key := normalEntry(f, uint32(f.phys.Attrs().Index))

		runBpfTest(t, "calico_from_workload_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(f.phys.Attrs().Index)), "hint should be untouched")
			Expect(leg.Tunnel).To(BeFalse())
			Expect(leg.Pinned).To(BeFalse())
			Expect(leg.Checked).To(BeFalse(),
				"a leg left unclaimed must re-validate once the kernel agrees")
		})
	})

	t.Run("does not complete a tunnel claim on a device that cannot encap", func(t *testing.T) {
		f := setupCtPinFixture(t, "PIN2D")
		f.tunneledCaliRoute(t, ctPinDstCIDR())
		f.kernelRoute(t, ctPinDstCIDR(), f.phys)
		key := ctv4.NewKey(17, srcIP, srcPort, dstIP, dstPort)
		legSrcToDst := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true, Opener: true,
			Workload: true, Ifindex: ctPinWlIfindex}
		legDstToSrc := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true,
			Pinned: true, Ifindex: uint32(f.phys.Attrs().Index)}
		val := ctv4.NewValueNormal(0, 0, legSrcToDst, legDstToSrc)
		Expect(f.ctMap.Update(key.AsBytes(), val.AsBytes())).NotTo(HaveOccurred())

		runBpfTest(t, "calico_from_workload_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Tunnel).To(BeFalse(),
				"the FIB agreeing does not make a physical NIC a tunnel egress")
			Expect(leg.Checked).To(BeFalse())
		})
	})

	t.Run("marks checked and nothing else for a non-encap dest", func(t *testing.T) {
		f := setupCtPinFixture(t, "PIN3")
		// Cali route present but not tunneled.
		Expect(rtMap.Update(
			routes.NewKey(ip.CIDRFromIPNet(ctPinDstCIDR()).(ip.V4CIDR)).AsBytes(),
			routes.NewValueWithIfIndex(routes.FlagsRemoteWorkload|routes.FlagInIPAMPool, ctPinWlIfindex).AsBytes(),
		)).NotTo(HaveOccurred())
		key := normalEntry(f, uint32(f.phys.Attrs().Index))

		runBpfTest(t, "calico_from_workload_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(f.phys.Attrs().Index)))
			Expect(leg.Checked).To(BeTrue())
			Expect(leg.Tunnel).To(BeFalse())
			Expect(leg.Pinned).To(BeFalse())
		})
	})

	// A destination with no route in the BPF routes map is off-cluster: the
	// map carries an entry for every IP pool CIDR, so an in-cluster address
	// always matches at least the enclosing pool. The consumer bails to
	// try_fib_external before it reads the hint, so retrying per packet would
	// tax every outbound flow for an answer nobody uses.
	t.Run("marks checked for a dest with no route instead of retrying per packet", func(t *testing.T) {
		f := setupCtPinFixture(t, "PIN4")
		key := normalEntry(f, uint32(f.phys.Attrs().Index))

		runBpfTest(t, "calico_from_workload_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(f.phys.Attrs().Index)))
			Expect(leg.Checked).To(BeTrue())
			Expect(leg.Tunnel).To(BeFalse())
			Expect(leg.Pinned).To(BeFalse())
		})
	})

	t.Run("leaves a tun_ip flow's hint alone - it keys the ARP return path", func(t *testing.T) {
		f := setupCtPinFixture(t, "PIN5")
		// Identical routing to the pinning case: without the tun_ip gate the
		// validator would rewrite this hint too.
		f.tunneledCaliRoute(t, ctPinDstCIDR())
		f.kernelRoute(t, ctPinDstCIDR(), f.tunl)

		// A nodeport flow's tracking entry on the backend node: opened from
		// the remote client (dstIP here plays the client), tun_ip holding the
		// forwarding node, the client->pod leg recording the HEP the request
		// ingressed through.
		key := ctv4.NewKey(17, srcIP, srcPort, dstIP, dstPort)
		legPodToClient := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true,
			Workload: true, Ifindex: ctPinWlIfindex}
		legClientToPod := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true, Opener: true,
			Ifindex: uint32(f.phys.Attrs().Index)}
		val := ctv4.NewValueNATReverse(0, 0, legPodToClient, legClientToPod,
			node1ip, srcIP, ctPinSvcPort)
		Expect(f.ctMap.Update(key.AsBytes(), val.AsBytes())).NotTo(HaveOccurred())

		runBpfTest(t, "calico_from_workload_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(f.phys.Attrs().Index)),
				"the ARP-path egress record must survive")
			Expect(leg.Tunnel).To(BeFalse())
			Expect(leg.Pinned).To(BeFalse())
			Expect(leg.Checked).To(BeFalse())
		})
	})

	t.Run("validates the post-NAT destination for a DNAT flow", func(t *testing.T) {
		f := setupCtPinFixture(t, "PIN6")
		// The service VIP deliberately has no route anywhere; only the
		// backend does. Validating the VIP would bail without marking the
		// leg; validating the backend pins.
		f.tunneledCaliRoute(t, ctPinDstCIDR())
		f.kernelRoute(t, ctPinDstCIDR(), f.tunl)
		svcIP := net.IPv4(10, 101, 0, 10)

		fwdKey := ctv4.NewKey(17, srcIP, srcPort, svcIP, ctPinSvcPort)
		revKey := ctv4.NewKey(17, srcIP, srcPort, dstIP, ctPinBackPort)
		legPodToBackend := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true, Opener: true,
			Workload: true, Ifindex: ctPinWlIfindex}
		legBackendToPod := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true,
			Ifindex: uint32(f.phys.Attrs().Index)}
		revVal := ctv4.NewValueNATReverse(0, 0, legPodToBackend, legBackendToPod,
			nil, svcIP, ctPinSvcPort)
		Expect(f.ctMap.Update(revKey.AsBytes(), revVal.AsBytes())).NotTo(HaveOccurred())
		fwdVal := ctv4.NewValueNATForward(0, 0, revKey)
		Expect(f.ctMap.Update(fwdKey.AsBytes(), fwdVal.AsBytes())).NotTo(HaveOccurred())

		ipl := *ipv4Default
		ipl.SrcIP = srcIP
		ipl.DstIP = svcIP
		_, _, _, _, svcPkt, err := testPacketV4(nil, &ipl, &layers.UDP{
			SrcPort: udpDefault.SrcPort,
			DstPort: layers.UDPPort(ctPinSvcPort),
		}, nil)
		Expect(err).NotTo(HaveOccurred())

		runBpfTest(t, "calico_from_workload_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(svcPkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, revKey, true)
			Expect(leg.Checked).To(BeTrue(),
				"the validator must engage for service traffic at all")
			Expect(leg.Ifindex).To(Equal(uint32(f.tunl.Attrs().Index)),
				"the backend's route decides, not the VIP's absence of one")
			Expect(leg.Tunnel).To(BeTrue())
			Expect(leg.Pinned).To(BeTrue())
		})
	})
}

// TestCtLegPinIfaceEncaps pins the provenance side of the tunnel claim: the
// attach point's IFACE_ENCAPS global decides, not the compiled object, which
// wireguard shares with a plain L3-classified NIC.
func TestCtLegPinIfaceEncaps(t *testing.T) {
	RegisterTestingT(t)

	extIP := net.IPv4(3, 3, 3, 3)
	srcPort := uint16(udpDefault.SrcPort)
	dstPort := uint16(udpDefault.DstPort)
	pkt := ctPinUDPPacket(extIP, srcIP)

	// srcIP (1.1.1.1) < extIP (3.3.3.3), so B2A is the client->workload leg
	// the from-host program records its ingress on.
	key := ctv4.NewKey(17, srcIP, dstPort, extIP, srcPort)

	for _, tc := range []struct {
		name   string
		encaps bool
	}{
		{"a device that does not encapsulate", false},
		{"an encapsulating device", true},
	} {
		opts := []testOption{}
		if tc.encaps {
			opts = append(opts, withIfaceEncaps())
		}

		t.Run("creation claims the kind of "+tc.name, func(t *testing.T) {
			f := setupCtPinFixture(t, "ENC1")
			resetCTMap(f.ctMap)

			runBpfTest(t, "calico_from_host_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
				_, err := bpfrun(pkt)
				Expect(err).NotTo(HaveOccurred())

				leg := f.leg(t, key, true)
				Expect(leg.Ifindex).To(Equal(uint32(ctPinWlIfindex)),
					"the ingress record is this program's own device")
				Expect(leg.Tunnel).To(Equal(tc.encaps))
			}, opts...)
		})

		t.Run("refresh reconciles a stale claim against "+tc.name, func(t *testing.T) {
			f := setupCtPinFixture(t, "ENC2")
			// The leg already names the arrival device, so the packet
			// confirms the ingress record and ct_leg_refresh_kind runs. Its
			// claim is the opposite of the truth in both directions.
			wlLeg := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true,
				Workload: true, Ifindex: ctPinWlIfindex}
			hostLeg := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true,
				Ifindex: ctPinWlIfindex, Tunnel: !tc.encaps, Checked: true}
			val := ctv4.NewValueNormal(0, 0, wlLeg, hostLeg)
			Expect(f.ctMap.Update(key.AsBytes(), val.AsBytes())).NotTo(HaveOccurred())

			runBpfTest(t, "calico_from_host_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
				_, err := bpfrun(pkt)
				Expect(err).NotTo(HaveOccurred())

				leg := f.leg(t, key, true)
				Expect(leg.Tunnel).To(Equal(tc.encaps))
				Expect(leg.Checked).To(BeFalse(),
					"a changed kind claim voids whatever validation stamped it")
			}, opts...)
		})
	}
}

// TestCtLegPinReconcileArms exercises what each RPF arm of the
// ingress-mismatch block does to a pinned leg, through the from-host program.
// The packet arrives from an external client (extIP) toward the local
// workload; the client->workload leg carries the pin under test and always
// mismatches the arrival ifindex, so the block runs on every packet.
func TestCtLegPinReconcileArms(t *testing.T) {
	RegisterTestingT(t)

	extIP := net.IPv4(3, 3, 3, 3)
	extCIDR := &net.IPNet{IP: net.IPv4(3, 3, 3, 0).To4(), Mask: net.CIDRMask(24, 32)}
	srcPort := uint16(udpDefault.SrcPort)
	dstPort := uint16(udpDefault.DstPort)

	entry := func(f ctPinFixture, leg ctv4.Leg) ctv4.Key {
		// NewKey stores the tuple as given while the BPF side sorts by
		// address, so pass it pre-sorted: srcIP (1.1.1.1) < extIP (3.3.3.3),
		// A2B is workload->client and B2A the client->workload leg under
		// test.
		key := ctv4.NewKey(17, srcIP, dstPort, extIP, srcPort)
		wlLeg := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true,
			Workload: true, Ifindex: ctPinWlIfindex}
		leg.SynSeen, leg.AckSeen, leg.Approved = true, true, true
		val := ctv4.NewValueNormal(0, 0, wlLeg, leg)
		Expect(f.ctMap.Update(key.AsBytes(), val.AsBytes())).NotTo(HaveOccurred())
		return key
	}

	pkt := ctPinUDPPacket(extIP, srcIP)

	t.Run("loose arm re-validates an unchanged pin without destroying its claims", func(t *testing.T) {
		f := setupCtPinFixture(t, "ARM1")
		f.kernelRoute(t, extCIDR, f.tunl)
		key := entry(f, ctv4.Leg{Ifindex: uint32(f.tunl.Attrs().Index),
			Tunnel: true, Pinned: true, Checked: true})

		runBpfTest(t, "calico_from_host_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(f.tunl.Attrs().Index)))
			Expect(leg.Tunnel).To(BeTrue(), "a pure re-validation must not clear the kind stamp")
			Expect(leg.Pinned).To(BeTrue())
			Expect(leg.Checked).To(BeTrue(), "clearing checked here would ping-pong the reply side")
		}, withRPFEnabled(), withIngressIfindex(uint32(f.phys.Attrs().Index)))
	})

	t.Run("loose arm re-pins when routing moved and leaves the kind to the reply side", func(t *testing.T) {
		f := setupCtPinFixture(t, "ARM2")
		f.kernelRoute(t, extCIDR, f.tunl)
		key := entry(f, ctv4.Leg{Ifindex: uint32(f.phys.Attrs().Index),
			Tunnel: true, Pinned: true, Checked: true})

		runBpfTest(t, "calico_from_host_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(f.tunl.Attrs().Index)))
			Expect(leg.Pinned).To(BeTrue())
			Expect(leg.Tunnel).To(BeFalse(), "the new device's kind is unknown to a from-host program")
			Expect(leg.Checked).To(BeFalse(), "the reply side must revalidate the new device")
		}, withRPFEnabled(), withIngressIfindex(uint32(f.phys.Attrs().Index)))
	})

	t.Run("loose arm leaves a tun_ip flow's egress record alone", func(t *testing.T) {
		f := setupCtPinFixture(t, "ARM6")
		// Same routing as the re-pin case. The tun_ip gate blocks the pin -
		// the ifindex is half the {tun_ip, ifindex} ARP-map key - not the
		// discard.
		f.kernelRoute(t, extCIDR, f.tunl)
		key := ctv4.NewKey(17, srcIP, dstPort, extIP, srcPort)
		wlLeg := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true,
			Workload: true, Ifindex: ctPinWlIfindex}
		extLeg := ctv4.Leg{SynSeen: true, AckSeen: true, Approved: true,
			Ifindex: uint32(f.phys.Attrs().Index)}
		val := ctv4.NewValueNATReverse(0, 0, wlLeg, extLeg, node1ip, srcIP, dstPort)
		Expect(f.ctMap.Update(key.AsBytes(), val.AsBytes())).NotTo(HaveOccurred())

		runBpfTest(t, "calico_from_host_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(0)),
				"a contradicted record is discarded, not pinned to the FIB answer")
			Expect(leg.Pinned).To(BeFalse())
			Expect(leg.Tunnel).To(BeFalse())
			Expect(leg.Checked).To(BeFalse())
		}, withRPFEnabled(), withIngressIfindex(uint32(f.phys.Attrs().Index)))
	})

	t.Run("kind refresh drops a stale pin once traffic symmetrizes onto it", func(t *testing.T) {
		f := setupCtPinFixture(t, "ARM7")
		// The packet arrives on the very device the pin names (the UT skb
		// ifindex), so the leg is an honest ingress record again: nothing
		// else ever reaches the PINNED bit for a leg that no longer
		// mismatches, so the refresh path must drop the label.
		key := entry(f, ctv4.Leg{Ifindex: ctPinWlIfindex, Pinned: true, Checked: true})

		runBpfTest(t, "calico_from_host_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(ctPinWlIfindex)))
			Expect(leg.Pinned).To(BeFalse(), "a confirmed ingress record must shed the pin label")
			Expect(leg.Checked).To(BeTrue(), "the kind claim was right, validation stands")
			Expect(leg.Tunnel).To(BeFalse())
		})
	})

	t.Run("disabled RPF resets the record like any unexpected ingress", func(t *testing.T) {
		f := setupCtPinFixture(t, "ARM3")
		f.kernelRoute(t, extCIDR, f.tunl)
		key := entry(f, ctv4.Leg{Ifindex: uint32(f.tunl.Attrs().Index),
			Tunnel: true, Pinned: true, Checked: true})

		runBpfTest(t, "calico_from_host_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(0)))
			Expect(leg.Tunnel).To(BeFalse())
			Expect(leg.Pinned).To(BeFalse())
			Expect(leg.Checked).To(BeFalse())
		})
	})

	t.Run("strict arm denies a wrong-interface packet and clears the pin", func(t *testing.T) {
		f := setupCtPinFixture(t, "ARM4")
		f.kernelRoute(t, extCIDR, f.tunl)
		key := entry(f, ctv4.Leg{Ifindex: uint32(f.tunl.Attrs().Index),
			Tunnel: true, Pinned: true, Checked: true})

		runBpfTest(t, "calico_from_host_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			res, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())
			Expect(res.Retval).To(Equal(resTC_ACT_SHOT),
				"strict RPF must deny the wrong-interface packet")

			leg := f.leg(t, key, true)
			Expect(leg.Ifindex).To(Equal(uint32(0)))
			Expect(leg.Tunnel).To(BeFalse())
			Expect(leg.Pinned).To(BeFalse())
			Expect(leg.Checked).To(BeFalse())
		}, withRPFStrict(), withIngressIfindex(uint32(f.phys.Attrs().Index)))
	})

	t.Run("strict arm restores an honest ingress record when RPF passes", func(t *testing.T) {
		f := setupCtPinFixture(t, "ARM5")
		f.kernelRoute(t, extCIDR, f.tunl)
		key := entry(f, ctv4.Leg{Ifindex: uint32(f.phys.Attrs().Index),
			Tunnel: true, Pinned: true, Checked: true})

		runBpfTest(t, "calico_from_host_ep", rulesDefaultAllow, func(bpfrun bpfProgRunFn) {
			_, err := bpfrun(pkt)
			Expect(err).NotTo(HaveOccurred())

			leg := f.leg(t, key, true)
			// For a to-host program the recorded ingress is the skb ifindex.
			Expect(leg.Ifindex).To(Equal(uint32(ctPinWlIfindex)))
			Expect(leg.Tunnel).To(BeFalse())
			Expect(leg.Pinned).To(BeFalse(), "an honest ingress record must not stay a pin")
			Expect(leg.Checked).To(BeFalse())
		}, withRPFStrict(), withIngressIfindex(uint32(f.tunl.Attrs().Index)))
	})
}
