The transforms write the protocol of the payload over the next header
field that led to the AH, ESP or IPComp header they remove.
ipsec6_input() always gave them the field of the IPv6 header. When
extension headers precede the IPsec header, the field to update is in
the last of them. The packet was still delivered, because the callback
reads the protocol back from the same place, but the chain left in the
mbuf was wrong: the IPv6 header named the payload while the extension
headers were still in front of it. Whatever walks the chain again
misread the packet; a socket with IPV6_RECVHOPOPTS set, for one, no
longer got the hop-by-hop options of a packet that ESP protected.
Look the field up with ip6_get_prevhdr(), as ipsec6_common_input() did
before the projects/ipsec merge.
Reported by: llm-postcr
Fixes: fcf596178b5f ("Merge projects/ipsec into head/.")
MFC after: 2 weeks
Sponsored by: Rubicon Communications, LLC ("Netgate")