Commit graph

556 commits

Author SHA1 Message Date
Konstantin Belousov
183513d15f sys/netipsec: ensure sah stability during input callback processing
Citing ae: this fixes some rare panics, that are reported in
derived projects: `panic: esp_input_cb: Unexpected address family'.

Reported by:	ae
Tested by:	ae, Daniel Dubnikov <ddaniel@nvidia.com>
Reviewed by:	ae, Ariel Ehrenberg <aehrenberg@nvidia.com> (previous version)
Sponsored by:	NVidia networking
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D54325
2025-12-22 14:31:25 +02:00
Mateusz Piotrowski
15afd75e67 ipsec: Fix typos in references to IPsec's ESP
ESP is "Encapsulating Security Payload",
not "Encapsulated Security Payload".

This patch fixes all the place in the tree I could find
with `grep -i encapsulated security`.

MFC after:	3 days
Reviewed by:	ae
Differential Revision:	https://reviews.freebsd.org/D53769
2025-12-04 16:17:12 +01:00
Konstantin Belousov
4accefc998 ipsec_offload: do not leak drv_spi unr
in the ipsec_accel_sa_newkey_cb() when the SA offload is only enabled
on a specific different interface, not the current one.

Also remove no longer relevant XXX comment.

Noted and reviewed by:	slavash
Sponsored by:	NVidia networking
MFC after:	1 week
2025-11-04 21:20:39 +02:00
Konstantin Belousov
8b2f4e6b3a ipsec offload: never return error from the newkey/spdadd callbacks
Returning an error causes premature termination of if_foreach_sleep()
loop over the interfaces.  Whatever problem we have with the specific
interface trying to install an element, should not prevent an attempt to
install the same element into all other interfaces.

Noted by:	Ariel Ehrenberg
Sponsored by:	NVidia networking
MFC after:	1 week
2025-10-30 16:54:36 +02:00
Zhenlei Huang
360bb45690 netipsec: Use proper prototype for SYSINIT functions
MFC after:	1 week
2025-10-13 18:12:33 +08:00
Konstantin Belousov
39598c2a9f ipsec offload: ipsec_accel_fill_xh() should indirect through fn pointer
The config with IPSEC_SUPPORT + IPSEC_OFFLOAD is the valid one.

Fixes:	5be5a0bde5
Sponsored by:	Nvidia networking
2025-07-17 13:57:35 +03:00
Konstantin Belousov
5be5a0bde5 ipsec offload: make hw-decrypted plain text packet like sw decrypted.
Mark hw-decrypted mbufs with M_DECRYPTED in the CHECK_POLICY() hook,
when the flag is owned by IPSEC.

Convert PACKET_TAG_IPSEC_ACCEL_IN to PACKET_TAG_IPSEC_IN_DONE to
provide the xform history for ipsec transform history check.

The hw-decrypted packets are then subject to exactly the same checks at
CHECK_POLICY() hooks as the sw-decrypted packet.  This includes the
policy checking, and updating the corresponding policy' lastused field,
needed for IKE daemons to track association lifetime.

Reviewed by:	Ariel Ehrenberg <aehrenberg@nvidia.com>, slavash
Sponsored by:	Nvidia networking
2025-07-17 12:36:30 +03:00
Konstantin Belousov
b494e256a4 ipsec offload: add xform_history to the ipsec_accel_in_tag
The layout makes ipsec_accel_in_tag compatible with
PACKET_TAG_IPSEC_IN_DONE.

Reviewed by:	Ariel Ehrenberg <aehrenberg@nvidia.com>, slavash
Sponsored by:   Nvidia networking
2025-07-17 12:36:22 +03:00
Konstantin Belousov
fb65357d87 ipsec_offload: pre-calculate xform_history for recorded accelerated SA
Also provide the helper to fill the xform_history into mtag, provided by
a driver.

Reviewed by:	Ariel Ehrenberg <aehrenberg@nvidia.com>, slavash
Sponsored by:	Nvidia networking
2025-07-17 12:36:18 +03:00
Brooks Davis
e453e498cb machine/stdarg.h -> sys/stdarg.h
Switch to using sys/stdarg.h for va_list type and va_* builtins.

Make an attempt to insert the include in a sensible place.  Where
style(9) was followed this is easy, where it was ignored, aim for the
first block of sys/*.h headers and don't get too fussy or try to fix
other style bugs.

Reviewed by:	imp
Exp-run by:	antoine (PR 286274)
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1595
2025-06-11 17:39:02 +01:00
Gordon Bergling
56a9d245eb ipsec(4): Fix a typo in a source code comment
- s/limitiation/limitation/

MFC after:	3 days
2025-05-31 10:59:09 +02:00
Andrey V. Elsukov
04207850a9 pfkey2: use correct value for a key length
The length of key data is specified via sadb_key_bits field.
Use specified size for buffer allocation and key copying.
Also add a check that the value of sadb_key_bits is not zero,
as explicitly required in RFC 2367.

PR:		241010
Submitted by:	jean-francois.hren at stormshield eu
MFC after:	1 week
2025-03-06 15:18:59 +03:00
acazuc
70703aa922 netinet: allow per protocol random IP id control, single out IPSEC
A globally enabled random IP id generation maybe useful in most IP
contexts, but it may be unnecessary in the case of IPsec encapsulated
packets because IPsec can be configured to use anti-replay windows.

This commit adds a new net.inet.ipsec.random_id sysctl to control whether
or not IPsec packets should use random IP id generation.

Rest of the protocols/modules are still controlled by the global
net.inet.ip.random_id, but can be easily augmented with a knob.

Reviewed by:		glebius
Sponsored by:		Stormshield
Differential Revision:	https://reviews.freebsd.org/D49164
2025-03-04 08:45:32 -08:00
Konstantin Belousov
0ff2d00d2a ipsec: allow it to work with unmapped mbufs
Only map mbuf when a policy is looked up and indicates that IPSEC needs
to transform the packet.  If IPSEC is inline offloaded, it is up to the
interface driver to request remap if needed.

Fetch the IP header using m_copydata() instead of using mtod() to select
policy/SA.

Reviewed by:	markj
Sponsored by:	NVidia networking
Differential revision:	https://reviews.freebsd.org/D48265
2025-01-13 21:29:32 +02:00
Mark Johnston
378a2b155a netipsec: Pass the right mbuf up
Note that key_spdacquire() is dead code, as the SADB_X_SPDACQUIRE
message handler is not set.

PR:		243057
MFC after:	2 weeks
2025-01-06 23:20:18 +00:00
Tobias Heider
0dab21248b pfkey: Fix some checks in kdebug_sadb()
Besides not doing any sufficient check that the length of a parsed
message is not bigger than the actual allocated buffer, kdebug_sadb()
incorrectly compares ext->sadb_ext_len, the extension payload size in 8
byte chunks, with tlen, which is the full message payload size in bytes.

This should compare PFKEY_UNUNIT64(ext->sadb_ext_len) with tlen instead.

PR:		277456
MFC after:	2 weeks
2024-12-04 16:22:50 +00:00
Andrey V. Elsukov
c94d6389e4 ipsec: fix IPv6 over IPv4 tunneling.
Properly initialize setdf variable in ipsec_encap().
It is used for AF_INET6 case when IPv6 datagram is going to be
encapsulated into IPv4 datagram.

PR:		282535
Fixes:		4046178557
MFC after:	1 week
2024-11-25 20:42:00 +03:00
Justin Hibbits
4d0c95384f net: Include private header in more needed places
sys/netinet and sys/netipsec are both part of the 'blessed' netstack, so
can access struct ifnet directly.  With this structure becoming private
very soon, the necessary files need to get direct access.

Sponsored by:	Juniper Networks, Inc.
2024-11-13 14:30:59 -05:00
Mark Johnston
01f43479b5 ipsec: Drain async ipsec_offload work when destroying a vnet
Re-apply commit e196b12f4d.  This was reverted by commit 28294dc924
because it could trigger a deadlock, but the underlying problem there
was fixed in commit f76826b892.

Reported by:	KASAN
Reviewed by:	kib
Fixes:		ef2a572bf6 ("ipsec_offload: kernel infrastructure")
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D46483
2024-09-04 14:28:28 +00:00
Konstantin Belousov
f76826b892 ipsec offload: use private taskqueue thread
Using global taskqueue_thread XXX with the vnet tasks scheduled during
VNET destruction.  VNET shutdown needs to wait for all vnet-scoped
SAs/SPs to be handled, and doing that from taskqueue_thread task
deadlocks because the same thread proceeds the removals.

Reviewed by:	markj
Sponsored by:	NVidia networking
Differential revision:	https://reviews.freebsd.org/D46494
2024-09-04 11:49:38 +03:00
Konstantin Belousov
1af77be327 ipsec_offlad: remove not needed IFP_HS_INPUT/OUTPUT flags
Calculate the hdr_ext_size unconditionally, it is kept unused for SAs
not handling the input.

Sponsored by:	NVidia networking
2024-09-04 11:49:38 +03:00
Konstantin Belousov
d02e1a3ffa ipsec_accel_output(): do not process packet if interface rejected offload
Sponsored by:	NVidia networking
2024-09-04 11:49:38 +03:00
Mark Johnston
28294dc924 Revert "ipsec: Drain async ipsec_offload work when destroying a vnet"
This change can cause a deadlock in some cases, since it's possible for
VNET teardown to happen in the context of taskqueue_thread, and
ipsec_accel_sync() drains taskqueue_thread's work queue.

This reverts commit e196b12f4d.
2024-08-30 15:00:16 +00:00
Mark Johnston
e196b12f4d ipsec: Drain async ipsec_offload work when destroying a vnet
The ipsec_offload code in some cases releases object references in an
asynchronous context where it needs to set the current VNET.  Make sure
that all such work completes before the VNET is actually destroyed,
otherwise a use-after-free is possible.

Reported by:	KASAN
Reviewed by:	kib
Fixes:		ef2a572bf6 ("ipsec_offload: kernel infrastructure")
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D46483
2024-08-30 13:08:20 +00:00
Kristof Provost
b1c3a4d75f netipsec: add probe points for the ipsec/esp/ah/ipcomp counters
Extend what we did for netinet counters in 60d8dbbef0 (netinet: add a probe
point for IP, IP6, ICMP, ICMP6, UDP and TCP stats counters, 2024-01-18) to the
IPsec code.

Sponsored by:	Rubicon Communications, LLC ("Netgate")
Differential Revision:	https://reviews.freebsd.org/D46416
2024-08-28 12:02:45 +02:00
Konstantin Belousov
66f0e2017f ipsec_offload: add ipsec_accel_drv_sa_lifetime_fetch()
A function to fetch hardware counters for offloaded SA on specific
interface.

Sponsored by:	NVidia networking
2024-08-20 15:42:13 +03:00
Konstantin Belousov
c4a0ee9b97 ipsec_offload: add handler for interface down events
Remove all offloaded SAs and SPs on ifdown.

Sponsored by:	NVIDIA networking
2024-08-20 15:42:12 +03:00
Konstantin Belousov
65f264dcf7 ipsec_offload: indirect two more functions on the ipsec.ko module load
Specifically, ipsec_accel_on_ifdown() and ipsec_accel_drv_sa_lifetime_update()
should be present in kernel for future mlx5en driver to be statically
linkable into the kernel built with IPSEC_HOOKS + IPSEC_OFFLOAD.

Sponsored by:	NVIDIA networking
2024-07-30 17:59:49 +03:00
Konstantin Belousov
9a7096ff54 ipsec_offload: hide SA/SP offload lifecycle prints under verbose sysctl
Reported and reviewed by:	kp
Discussed with:	np
Sponsored by:	NVIDIA networking
Differential revision:	https://reviews.freebsd.org/D46045
2024-07-21 11:53:19 +03:00
Konstantin Belousov
6023bd1d52 netipsec: move declaration of the sysctl net.inet{,6}.ipsec nodes to header
Reviewed by:	kp
Sponsored by:	NVIDIA networking
Differential revision:	https://reviews.freebsd.org/D46045
2024-07-21 11:53:19 +03:00
Konstantin Belousov
eb0fdc7753 netinet/ipsec.h: remove unneeded "extern"s
Reviewed by:	kp
Sponsored by:	NVIDIA networking
Differential revision:	https://reviews.freebsd.org/D46045
2024-07-21 11:53:19 +03:00
Konstantin Belousov
e6e2c0a5ef ipsec_offload: switch TF2_IPSEC_TSO on/off as appropriate on output
after the interface ipsec_accel method if_hwassist() is consulted.

Sponsored by:	NVIDIA networking
2024-07-12 07:27:59 +03:00
Konstantin Belousov
240b7bfe56 ipsec_offload: offload inner checksums calculations for UDP/TCP/TSO
and allow the interface driver to declare such support.

Sponsored by:	NVIDIA networking
Differential revision:	https://reviews.freebsd.org/D44221
2024-07-12 07:27:58 +03:00
Konstantin Belousov
ef2a572bf6 ipsec_offload: kernel infrastructure
Inline IPSEC offload moves almost whole IPSEC processing from the
CPU/MCU and possibly crypto accelerator, to the network card.

The transmitted packet content is not touched by CPU during TX
operations, kernel only does the required policy and security
association lookups to find out that given flow is offloaded, and then
packet is transmitted as plain text to the card. For driver convenience,
a metadata is attached to the packet identifying SA which must process
the packet. Card does encryption of the payload, padding, calculates
authentication, and does the reformat according to the policy.

Similarly, on receive, card does the decapsulation, decryption, and
authentification.  Kernel receives the identifier of SA that was
used to process the packet, together with the plain-text packet.

Overall, payload octets are only read or written by card DMA engine,
removing a lot of memory subsystem overhead, and saving CPU time because
IPSEC algos calculations are avoided.

If driver declares support for inline IPSEC offload (with the
IFCAP2_IPSEC_OFFLOAD capability set and registering method table struct
if_ipsec_accel_methods), kernel offers the SPD and SAD to driver.
Driver decides which policies and SAs can be offloaded based on
hardware capacity, and acks/nacks each SA for given interface to
kernel.  Kernel needs to keep this information to make a decision to
skip software processing on TX, and to assume processing already done
on RX.  This shadow SPD/SAD database of offloads is rooted from
policies (struct secpolicy accel_ifps, struct ifp_handle_sp) and SAs
(struct secasvar accel_ipfs, struct ifp_handle_sav).

Some extensions to the PF_KEY socket allow to limit interfaces for
which given SP/SA could be offloaded (proposed for offload).  Also,
additional statistics extensions allow to observe allocation/octet/use
counters for specific SA.

Since SPs and SAs are typically instantiated in non-sleepable context,
while offloading them into card is expected to require costly async
manipulations of the card state, calls to the driver for offload and
termination are executed in the threaded taskqueue.  It also solves
the issue of allocating resources needed for the offload database.
Neither ipf_handle_sp nor ipf_handle_sav do not add reference to the
owning SP/SA, the offload must be terminated before last reference is
dropped.  ipsec_accel only adds transient references to ensure safe
pointer ownership by taskqueue.

Maintaining the SA counters for hardware-accelerated packets is the
duty of the driver.  The helper ipsec_accel_drv_sa_lifetime_update()
is provided to hide accel infrastructure from drivers which would use
expected callout to query hardware periodically for updates.

Reviewed by:	rscheff	(transport, stack integration), np
Sponsored by:	NVIDIA networking
Differential revision:	https://reviews.freebsd.org/D44219
2024-07-12 07:27:58 +03:00
Konstantin Belousov
00524fd475 ipsec_output(): add mtu argument
Similarly, mtu is needed to decide inline IPSEC offloiad for the driver.

Sponsored by: NVIDIA networking
Differential revision:	https://reviews.freebsd.org/D44224
2024-07-12 06:29:31 +03:00
Konstantin Belousov
de1da299da ipsec_output(): add outcoming ifp argument
The information about the interface is needed to coordinate inline
offloading of IPSEC processing with corresponding driver.

Sponsored by:	NVIDIA networking
Differential revision:	https://reviews.freebsd.org/D44223
2024-07-12 06:29:31 +03:00
Konstantin Belousov
41106f5aa0 netipsec/xform_esp.c: make esp_ctr_compatibility global
Sponsored by:	NVIDIA networking
2024-07-12 06:29:31 +03:00
Konstantin Belousov
54ac7b969f ipsec: make key_do_allocsp() global
Sponsored by:	NVIDIA networking
2024-07-12 06:29:31 +03:00
Lexi Winter
50ecbc5142 libipsec: make const-correct
- add const to the appropriate places in the libipsec public API and the
  relevant internal functions needed to support that.

- replace caddr_t with c_caddr_t in ipsec_dump_policy()

- update the ipsec_dump_policy manpage to use c_caddr_t (this manpage
  was already wrong as it had "char *" instead of caddr_t previously).

While here, update pfkeyv2.h to not cast away const in the PFKEY_*()
macros.

This should not cause any ABI changes as the actual types have not
changed.

Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/1099
2024-04-22 22:36:34 -06:00
Lexi Winter
122dd78c14 sys/netipsec: fix IPSEC_SUPPORT for non-INET kernels
The functions ipsec_kmod_udp_input() and ipsec_kmod_udp_pcbctl() are
used by netinet6 for IPSEC_SUPPORT, but are guarded behind #ifdef INET.

Since neither of these require INET, remove the guard so they're built
even without INET.

Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/1158
2024-04-12 10:30:22 -06:00
Konstantin Belousov
1a56620b79 ipsec esp: avoid dereferencing freed secasindex
It is possible that SA was removed while processing packed, in which
case it is changed to the DEAD state and it index is removed from the
tree. Dereferencing sav->sah then touches freed memory.

Reviewed by:	ae
Sponsored by:	NVIDIA networking
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D44079
2024-02-26 16:27:46 +02:00
Kristof Provost
c3d7bb5aca netipsec: fix LINT-NOINET build
udp_ipsec_input() is used for INET6, so we need it even in NOINET
builds. Build the relevant file if either of INET or INET6 are set.
2024-01-20 22:22:21 +01:00
Allan Jude
dc02374f54 Fix KASSERT in 80044c78 causing build failures
Move the KASSERT to where struct ip6_hdr is populated

Fixes:		80044c785c
Reported-by:	bapt
Reviewed-by:	markj
Sponsored-by:	Klara, Inc.
2024-01-16 23:15:00 +00:00
Xavier Beaudouin
80044c785c Add UDP encapsulation of ESP in IPv6
This patch provides UDP encapsulation of ESP packets over IPv6.
Ports the IPv4 code to IPv6 and adds support for IPv6 in udpencap.c
As required by the RFC and unlike in IPv4 encapsulation,
UDP checksums are calculated.

Co-authored-by:	Aurelien Cazuc <aurelien.cazuc.external@stormshield.eu>
Sponsored-by:	Stormshield
Sponsored-by:	Wiktel
Sponsored-by:	Klara, Inc.
2024-01-16 20:44:34 +00:00
Gleb Smirnoff
296a4cb5c5 sockets: provide correct pr_shutdown for keysock and SDP
My failure to run all kinds of kernel builds lead to missing the keysock
and incorrectly assuming SDP as not having a shutdown method.

Fixes:	5bba272807
2024-01-16 12:02:59 -08:00
Warner Losh
fdafd315ad sys: Automated cleanup of cdefs and other formatting
Apply the following automated changes to try to eliminate
no-longer-needed sys/cdefs.h includes as well as now-empty
blank lines in a row.

Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/
Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/
Remove /\n+#if.*\n#endif.*\n+/
Remove /^#if.*\n#endif.*\n/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/

Sponsored by:		Netflix
2023-11-26 22:24:00 -07:00
Warner Losh
685dc743dc sys: Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:36 -06:00
Warner Losh
71625ec9ad sys: Remove $FreeBSD$: one-line .c comment pattern
Remove /^/[*/]\s*\$FreeBSD\$.*\n/
2023-08-16 11:54:24 -06:00
Warner Losh
95ee2897e9 sys: Remove $FreeBSD$: two-line .h pattern
Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
2023-08-16 11:54:11 -06:00
Michael Tuexen
0fb0711dba tcp: fix TCP MD5 digest computation for TCP over UDP
Skip the UDP header for the computation. This is similar to
skipping IPv6 extension headers.

Reviewed by:		cc, rscheff
MFC after:		3 days
Sponsored by:		Netflix, Inc.
Differential Revision:	https://reviews.freebsd.org/D40596
2023-06-21 22:48:12 +02:00