A policy can bundle several requests, for example an ESP tunnel and an AH
transport request. A request at level "use" that has no SA is skipped (as
is "default", which the net.inet{,6}.ipsec.*_deflev sysctls make "use"),
and the packet is meant to leave with the transforms of the other requests.
ipsec_process_done() applies the next request of a bundle once a transform
completes. When all remaining requests are skipped,
ipsec{4,6}_perform_request() returns EJUSTRETURN and leaves the packet to
its caller, but ipsec_process_done() returned that to the transform, which
does not send the packet or free it. Keep the references to the SP and the
SA across the call, and on EJUSTRETURN send the packet with the transforms
applied so far, after the UDP encapsulation of the last SA applied.
ipcomp_output() calls ipsec_process_done() directly for packets below the
compression threshold and passed the EJUSTRETURN on to ip_output(), which
then went on with a packet that tunnel mode encapsulation had already
changed. It no longer sees one.
Fixes: 9e3bdede0f3d ("Correct IPsec behaviour with a 'use' level...")
MFC after: 2 weeks
Sponsored by: Rubicon Communications, LLC ("Netgate")