Skip to contentSkip to conversation

add basic PSP encryption for TCP connections

RFC In review Jakub Kicinski 10 participants 49 messages May 10, 2024 – Jun 28, 2024

Subsystems (13)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

cover add basic PSP encryption for TCP connections

Hi!

Add support for PSP encryption of TCP connections.

PSP is a protocol out of Google:
https://github.com/google/psp/blob/main/doc/PSP_Arch_Spec.pdf
which shares some similarities with IPsec. I added some more info in the first patch so I'll keep it short here.

The protocol can work in multiple modes including tunneling. But I'm mostly interested in using it as TLS replacement because of its superior offload characteristics. So this patch does three things:

- it adds "core" PSP code
PSP is offload-centric, and requires some additional care and feeding, so first chunk of the code exposes device info. This part can be reused by PSP implementations in xfrm, tunneling etc.

- TCP integration TLS style
Reuse some of the existing concepts from TLS offload, such as attaching crypto state to a socket, marking skbs as "decrypted", egress validation. PSP does not prescribe key exchange protocols. To use PSP as a more efficient TLS offload we intend to perform a TLS handshake ("inline" in the same TCP connection) and negotiate switching to PSP based on capabilities of both endpoints. This is also why I'm not including a software implementation. Nobody would use it in production, software TLS is faster, it has larger crypto records.

- mlx5 implementation
That's mostly other people's work, not 100% sure those folks consider it ready hence the RFC in the title. But it works :)

Not posted, queued a branch [1] are follow up pieces:
- standard stats
- netdevsim implementation and tests

[1] https://github.com/kuba-moo/linux/tree/psp

Jakub Kicinski (8):
psp: add documentation
psp: base PSP device support
net: modify core data structures for PSP datapath support
tcp: add datapath logic for PSP with inline key exchange
psp: add op for rotation of secret state
net: psp: add socket security association code
net: psp: update the TCP MSS to reflect PSP packet overhead
psp: track generations of secret state

Raed Salem (7):
net/mlx5e: Support PSP offload functionality net/mlx5e: Implement PSP operations .assoc_add and .assoc_del net/mlx5e: Implement PSP Tx data path
net/mlx5e: Add PSP steering in local NIC RX net/mlx5e: Configure PSP Rx flow steering rules net/mlx5e: Add Rx data path offload
net/mlx5e: Implement PSP key_rotate operation

 Documentation/netlink/specs/psp.yaml          | 186 +++++
 Documentation/networking/index.rst            |   1 +
 Documentation/networking/psp.rst              | 138 ++++
 .../net/ethernet/mellanox/mlx5/core/Kconfig   |  11 +
 .../net/ethernet/mellanox/mlx5/core/Makefile  |   5 +-
 drivers/net/ethernet/mellanox/mlx5/core/en.h  |   7 +-
 .../net/ethernet/mellanox/mlx5/core/en/fs.h   |   2 +-
 .../ethernet/mellanox/mlx5/core/en/params.c   |   4 +-
 .../mellanox/mlx5/core/en_accel/en_accel.h    |  50 +-
 .../mellanox/mlx5/core/en_accel/ipsec_rxtx.h  |   2 +-
 .../mellanox/mlx5/core/en_accel/nisp.c        | 209 +++++
 .../mellanox/mlx5/core/en_accel/nisp.h        |  55 ++
 .../mellanox/mlx5/core/en_accel/nisp_fs.c     | 737 ++++++++++++++++++
 .../mellanox/mlx5/core/en_accel/nisp_fs.h     |  30 +
 .../mlx5/core/en_accel/nisp_offload.c         |  52 ++
 .../mellanox/mlx5/core/en_accel/nisp_rxtx.c   | 304 ++++++++
 .../mellanox/mlx5/core/en_accel/nisp_rxtx.h   | 124 +++
 .../net/ethernet/mellanox/mlx5/core/en_main.c |   9 +
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   |  10 +
 .../net/ethernet/mellanox/mlx5/core/en_tx.c   |  10 +-
 drivers/net/ethernet/mellanox/mlx5/core/fw.c  |   6 +
 .../ethernet/mellanox/mlx5/core/lib/crypto.h  |   1 +
 .../mellanox/mlx5/core/lib/psp_defs.h         |  28 +
 .../net/ethernet/mellanox/mlx5/core/main.c    |   5 +
 .../net/ethernet/mellanox/mlx5/core/nisp.c    |  24 +
 .../net/ethernet/mellanox/mlx5/core/nisp.h    |  15 +
 include/linux/mlx5/device.h                   |   4 +
 include/linux/mlx5/driver.h                   |   2 +
 include/linux/mlx5/mlx5_ifc.h                 |  98 ++-
 include/linux/netdevice.h                     |   4 +
 include/linux/skbuff.h                        |   3 +
 include/linux/tcp.h                           |   3 +
 include/net/dropreason-core.h                 |   6 +
 include/net/psp.h                             |  12 +
 include/net/psp/functions.h                   | 150 ++++
 include/net/psp/types.h                       | 182 +++++
 include/net/sock.h                            |   4 +
 include/uapi/linux/psp.h                      |  66 ++
 net/Kconfig                                   |   1 +
 net/Makefile                                  |   1 +
 net/core/gro.c                                |   2 +
 net/core/skbuff.c                             |   4 +
 net/core/sock.c                               |   2 +
 net/ipv4/inet_connection_sock.c               |   2 +
 net/ipv4/tcp.c                                |   2 +
 net/ipv4/tcp_ipv4.c                           |  13 +-
 net/ipv4/tcp_minisocks.c                      |  21 +-
 net/ipv4/tcp_output.c                         |  16 +-
 net/ipv6/ipv6_sockglue.c                      |   6 +-
 net/ipv6/tcp_ipv6.c                           |  22 +-
 net/mptcp/protocol.c                          |   2 +
 net/psp/Kconfig                               |  15 +
 net/psp/Makefile                              |   5 +
 net/psp/psp-nl-gen.c                          | 119 +++
 net/psp/psp-nl-gen.h                          |  39 +
 net/psp/psp.h                                 |  54 ++
 net/psp/psp_main.c                            | 144 ++++
 net/psp/psp_nl.c                              | 517 ++++++++++++
 net/psp/psp_sock.c                            | 276 +++++++
 tools/net/ynl/Makefile.deps                   |   1 +
 60 files changed, 3791 insertions(+), 32 deletions(-)
 create mode 100644 Documentation/netlink/specs/psp.yaml
 create mode 100644 Documentation/networking/psp.rst
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp.h
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_fs.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_fs.h
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_offload.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.h
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/lib/psp_defs.h
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/nisp.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/nisp.h
 create mode 100644 include/net/psp.h
 create mode 100644 include/net/psp/functions.h
 create mode 100644 include/net/psp/types.h
 create mode 100644 include/uapi/linux/psp.h
 create mode 100644 net/psp/Kconfig
 create mode 100644 net/psp/Makefile
 create mode 100644 net/psp/psp-nl-gen.c
 create mode 100644 net/psp/psp-nl-gen.h
 create mode 100644 net/psp/psp.h
 create mode 100644 net/psp/psp_main.c
 create mode 100644 net/psp/psp_nl.c
 create mode 100644 net/psp/psp_sock.c
Signature · 1 line
2.45.0
author maintainer
#1 lore raw

RFC 1/15 psp: add documentation

Add documentation of things which belong in the docs rather than commit messages.

---
 Documentation/networking/index.rst |   1 +
 Documentation/networking/psp.rst   | 138 +++++++++++++++++++++++++++++
 2 files changed, 139 insertions(+)
 create mode 100644 Documentation/networking/psp.rst
Documentation/networking/index.rst +1 -0
index 7664c0bfe461..0376029ecbdf 100644
@@ -94,6 +94,7 @@ Refer to :ref:`netdev-FAQ` for a guide on netdev development process specifics.
    ppp_generic
    proc_net_tcp
    pse-pd/index
+   psp
    radiotap-headers
    rds
    regulatory
Documentation/networking/psp.rst new file +138 -0

138 lines not on this page · raw message

Signature · 1 line
2.45.0
#2 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 49 lines · Jakub Kicinski

Add documentation of things which belong in the docs rather than commit messages.

---
Documentation/networking/index.rst | 1 +
Documentation/networking/psp.rst | 138 +++++++++++++++++++++++++++++

2 files changed, 139 insertions(+)

create mode 100644 Documentation/networking/psp.rst

Documentation/networking/index.rst +1 -0
index 7664c0bfe461..0376029ecbdf 100644
@@ -94,6 +94,7 @@ Refer to :ref:`netdev-FAQ` for a guide on netdev development process specifics.
   ppp_generic
   proc_net_tcp
   pse-pd/index
+   psp
   radiotap-headers
   rds
   regulatory
Documentation/networking/psp.rst new file +21 -0
new file mode 100644
index 000000000000..a39b464813ab
@@ -0,0 +1,138 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+=====================
+PSP Security Protocol
+=====================
+
+Protocol
+========
+
+PSP Security Protocol (PSP) was defined at Google and published in:
+
+https://raw.githubusercontent.com/google/psp/main/doc/PSP_Arch_Spec.pdf
+
+This section briefly covers protocol aspects crucial for understanding
+the kernel API. Refer to the protocol specification for further details.
+
+Note that the kernel implementation and documentation uses the term
+"secret state" in place of "master key", it is both less confusing
+to an average developer and is less likely to run afoul any naming
+guidelines.
+

[ ... ]

Documentation/networking/psp.rst line 61

Quoted 13 lines
Documentation/networking/psp.rst new file +13 -0
+User facing API
+===============
+
+PSP is designed primarily for hardware offloads. There is currently
+no software fallback for systems which do not have PSP capable NICs.
+There is also no standard (or otherwise defined) way of establishing
+a PSP-secured connection or exchanging the symmetric keys.
+
+The expectation is that higher layer protocols will take care of
+protocol and key negotiation. For example one may use TLS key exchange,
+announce the PSP capability, and switch to PSP if both endpoints
+are PSP-capable.
+

The documentation doesn't include anything about userspace, other than highlevel remarks on how this is expected to work.
What are we planning for userspace? I know we have kperf basic support and some experimental python library, but nothing official or psp centric.

I propose to start community driven project with a well established library, with some concrete sample implementation for key negotiation, as a plugin maybe, so anyone can implement their own key-exchange mechanisms on top of the official psp library.

author maintainer
#3 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 4 lines · On Fri, 10 May 2024 15:19:23 -0700 Saeed Mahameed wrote:
Quoted 4 lines
Documentation/networking/psp.rst new file +4 -0
+PSP is designed primarily for hardware offloads. There is currently
+no software fallback for systems which do not have PSP capable NICs.
+There is also no standard (or otherwise defined) way of establishing
+a PSP-secured connection or exchanging the symmetric keys.
Quoted 8 lines

Documentation/networking/psp.rst line 60

Quoted 5 lines
Documentation/networking/psp.rst new file +5 -0
+
+The expectation is that higher layer protocols will take care of
+protocol and key negotiation. For example one may use TLS key exchange,
+announce the PSP capability, and switch to PSP if both endpoints
+are PSP-capable.

The documentation doesn't include anything about userspace, other than highlevel remarks on how this is expected to work.

The cover letter does.

Quoted 2 lines

What are we planning for userspace? I know we have kperf basic support and some experimental python library, but nothing official or psp centric.

Remind me, how long did it take for kernel TLS support to be merged into OpenSSL? ;)

Quoted 4 lines

I propose to start community driven project with a well established library, with some concrete sample implementation for key negotiation, as a plugin maybe, so anyone can implement their own key-exchange mechanisms on top of the official psp library.

Yes, I should have CCed Meta's folks who work on TLS [1]. Adding them now. More than happy to facilitate the discussion, maybe Willem can CC the right Google folks, IDK who else...

We should start moving with the kernel support, IMO, until we do the user space implementation is stalled. I don't expect that the way we install keys in the kernel would be impacted by the handshake.

[1] https://github.com/facebookincubator/fizz

#4 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 14 lines · Jakub Kicinski
Quoted 4 lines · On Fri, 10 May 2024 15:19:23 -0700 Saeed Mahameed wrote:
Quoted 4 lines
Documentation/networking/psp.rst new file +4 -0
+PSP is designed primarily for hardware offloads. There is currently
+no software fallback for systems which do not have PSP capable NICs.
+There is also no standard (or otherwise defined) way of establishing
+a PSP-secured connection or exchanging the symmetric keys.
Quoted 8 lines

Documentation/networking/psp.rst line 60

Quoted 5 lines
Documentation/networking/psp.rst new file +5 -0
+
+The expectation is that higher layer protocols will take care of
+protocol and key negotiation. For example one may use TLS key exchange,
+announce the PSP capability, and switch to PSP if both endpoints
+are PSP-capable.

The documentation doesn't include anything about userspace, other than highlevel remarks on how this is expected to work.

Quoted 7 lines

The cover letter does.

Quoted 2 lines

What are we planning for userspace? I know we have kperf basic support and some experimental python library, but nothing official or psp centric.

Remind me, how long did it take for kernel TLS support to be merged into OpenSSL? ;)

I believe it was bad timing for OpenSSL. The patches with kTLS support were sitting in the main branch for long time, the problem was postponed release with with jump to new versioning schema.

But I agree, there is no easy way to start coding user-space lib without initial support from kernel.

Quoted 14 lines
Quoted 4 lines

I propose to start community driven project with a well established library, with some concrete sample implementation for key negotiation, as a plugin maybe, so anyone can implement their own key-exchange mechanisms on top of the official psp library.

Yes, I should have CCed Meta's folks who work on TLS [1]. Adding them now. More than happy to facilitate the discussion, maybe Willem can CC the right Google folks, IDK who else...

We should start moving with the kernel support, IMO, until we do the user space implementation is stalled. I don't expect that the way we install keys in the kernel would be impacted by the handshake.

[1] https://github.com/facebookincubator/fizz

maintainer
#5 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 3 lines · Vadim Fedorenko

But I agree, there is no easy way to start coding user-space lib without initial
support from kernel.

The 2 sides should be co-developed; it is the only way to merge a sane uapi.

#6 lore raw

Re: [RFC net-next 01/15] psp: add documentation

In reply to Jakub Kicinski
Quoted 9 lines · Jakub Kicinski

Yes, I should have CCed Meta's folks who work on TLS [1]. Adding them now. More than happy to facilitate the discussion, maybe Willem can CC the right Google folks, IDK who else...

We should start moving with the kernel support, IMO, until we do the user space implementation is stalled. I don't expect that the way we install keys in the kernel would be impacted by the handshake.

[1] https://github.com/facebookincubator/fizz

Having worked on this in the past, I was curious about looking at the kernel/userspace implementation, and was surprised there's nothing at all on the userspace side of things.

What's the rush in merging this in prior to having a usable, open userspace? Can't they be developed side by side and merged in together?

Signature · 2 lines
Thanks,
Sasha
maintainer
#7 lore raw

Re: [RFC net-next 01/15] psp: add documentation

In reply to Jakub Kicinski
Quoted 38 lines · Jakub Kicinski

Add documentation of things which belong in the docs rather than commit messages.

---
 Documentation/networking/index.rst |   1 +
 Documentation/networking/psp.rst   | 138 +++++++++++++++++++++++++++++
 2 files changed, 139 insertions(+)
 create mode 100644 Documentation/networking/psp.rst
Documentation/networking/index.rst +1 -0
index 7664c0bfe461..0376029ecbdf 100644
@@ -94,6 +94,7 @@ Refer to :ref:`netdev-FAQ` for a guide on netdev development process specifics.
    ppp_generic
    proc_net_tcp
    pse-pd/index
+   psp
    radiotap-headers
    rds
    regulatory
Documentation/networking/psp.rst new file +10 -0
new file mode 100644
index 000000000000..a39b464813ab
@@ -0,0 +1,138 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+=====================
+PSP Security Protocol
+=====================
+
+Protocol
+========
+
+PSP Security Protocol (PSP) was defined at Google and published in:

Documentation/networking/psp.rst line 20

Quoted 10 lines
Documentation/networking/psp.rst new file +10 -0
+
+https://raw.githubusercontent.com/google/psp/main/doc/PSP_Arch_Spec.pdf
+
+This section briefly covers protocol aspects crucial for understanding
+the kernel API. Refer to the protocol specification for further details.
+
+Note that the kernel implementation and documentation uses the term
+"secret state" in place of "master key", it is both less confusing
+to an average developer and is less likely to run afoul any naming
+guidelines.

There is some value in using the same terminology in the code as in the spec.

And the session keys are derived from a key. That is more precise than state. Specifically, counter-mode KDF from an AES key.

Perhaps device key, instead of master key?

Documentation/networking/psp.rst line 28

Quoted 7 lines
Documentation/networking/psp.rst new file +7 -0
+Derived Rx keys
+---------------
+
+PSP borrows some terms and mechanisms from IPsec. PSP was designed
+with HW offloads in mind. The key feature of PSP is that Rx keys for every
+connection do not have to be stored by the receiver but can be derived
+from secret state and information present in packet headers.

A second less obvious, but neat, feature is that it supports an encryption offset, such that (say) the L4 ports are integrity protected, but not encrypted, to allow for in-network telemetry.

Documentation/networking/psp.rst

Quoted 4 lines
Documentation/networking/psp.rst new file +4 -0
+This makes it possible to implement receivers which require a constant
+amount of memory regardless of the number of connections (``O(1)`` scaling).
+
+Tx keys have to be stored like with any other protocol,

Keys can optionally be passed in descriptor.

Quoted 9 lines
Documentation/networking/psp.rst new file +8 -0
+The expectation is that higher layer protocols will take care of
+protocol and key negotiation. For example one may use TLS key exchange,
+announce the PSP capability, and switch to PSP if both endpoints
+are PSP-capable.
 
+Securing a connection
+---------------------
+
+PSP encryption is currently only supported for TCP connections.

Documentation/networking/psp.rst line 84

Quoted 10 lines
Documentation/networking/psp.rst new file +10 -0
+Rx and Tx keys are allocated separately. First the ``rx-assoc``
+Netlink command needs to be issued, specifying a target TCP socket.
+Kernel will allocate a new PSP Rx key from the NIC and associate it
+with given socket. At this stage socket will accept both PSP-secured
+and plain text TCP packets.
+
+Tx keys are installed using the ``tx-assoc`` Netlink command.
+Once the Tx keys are installed all data read from the socket will
+be PSP-secured. In other words act of installing Tx keys has the secondary
+effect on the Rx direction, requring all received packets to be encrypted.

Consider clarifying the entire state diagram from when one pair initiates upgrade.

And some edge cases:

- retransmits
- TCP fin handshake, if only one peer succeeds
- TCP control socket response to encrypted pkt

What is the expectation for data already queued for transmission when the tx assocation is made?

More generally, what happens for data in flight. One possible simplification is to only allow an upgrade sequence (possibly including in-band exchange of keys) when no other data is in flight.

Documentation/networking/psp.rst line 96

Quoted 12 lines
Documentation/networking/psp.rst new file +12 -0
+Since packet reception is asynchronous, to make it possible for the
+application to trust that any data read from the socket after the ``tx-assoc``
+call returns success has been encrypted, the kernel will scan the receive
+queue of the socket at ``tx-assoc`` time. If any enqueued packet was received
+in clear text the Tx association will fail, and application should retry
+installing the Tx key after draining the socket (this should not be necessary
+if both endpoints are well behaved).
+
+Rotation notifications
+----------------------
+
+The rotations of secret state happen asynchornously and are usually

typo: asynchronously

Documentation/networking/psp.rst line 100

Quoted 4 lines
Documentation/networking/psp.rst new file +4 -0
+performed by management daemons, not under application control.
+The PSP netlink family will generate a notification whenever keys
+are rotated. The applications are expected to re-establish connections
+before keys are rotated again.

Connection key rotation is not supported? I did notice that tx key insertion fails if a key is already present, so this does appear to be the behavior.

author maintainer
#8 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 16 lines · On Sun, 12 May 2024 21:24:23 -0400 Willem de Bruijn wrote:

Documentation/networking/psp.rst line 20

Quoted 11 lines · Jakub Kicinski
Documentation/networking/psp.rst new file +11 -0
+PSP Security Protocol (PSP) was defined at Google and published in:
+
+https://raw.githubusercontent.com/google/psp/main/doc/PSP_Arch_Spec.pdf
+
+This section briefly covers protocol aspects crucial for understanding
+the kernel API. Refer to the protocol specification for further details.
+
+Note that the kernel implementation and documentation uses the term
+"secret state" in place of "master key", it is both less confusing
+to an average developer and is less likely to run afoul any naming
+guidelines.  

There is some value in using the same terminology in the code as in the spec.

Quoted 4 lines

And the session keys are derived from a key. That is more precise than state. Specifically, counter-mode KDF from an AES key.

Perhaps device key, instead of master key?

Weak preference towards secret state, but device key works, too.

Quoted 11 lines

Documentation/networking/psp.rst line 28

Quoted 7 lines
Documentation/networking/psp.rst new file +7 -0
+Derived Rx keys
+---------------
+
+PSP borrows some terms and mechanisms from IPsec. PSP was designed
+with HW offloads in mind. The key feature of PSP is that Rx keys for every
+connection do not have to be stored by the receiver but can be derived
+from secret state and information present in packet headers.  

A second less obvious, but neat, feature is that it supports an encryption offset, such that (say) the L4 ports are integrity protected, but not encrypted, to allow for in-network telemetry.

I know, but the opening paragraph has:

This section briefly covers protocol aspects crucial for
understanding the kernel API. Refer to the protocol specification for further details.

:) .. and I didn't implement the offset, yet. (It's trivial to add and ETOOMANYPATCHES.)

Quoted 6 lines

Documentation/networking/psp.rst

Quoted 4 lines
Documentation/networking/psp.rst new file +4 -0
+This makes it possible to implement receivers which require a constant
+amount of memory regardless of the number of connections (``O(1)`` scaling).
+
+Tx keys have to be stored like with any other protocol,  

Keys can optionally be passed in descriptor.

Added: Preferably, the Tx keys should be provided with the packet (e.g. as part of the descriptors).

Quoted 14 lines
Quoted 14 lines
Documentation/networking/psp.rst new file +13 -0
+The expectation is that higher layer protocols will take care of
+protocol and key negotiation. For example one may use TLS key exchange,
+announce the PSP capability, and switch to PSP if both endpoints
+are PSP-capable.  
 
+Securing a connection
+---------------------
+
+PSP encryption is currently only supported for TCP connections.
+Rx and Tx keys are allocated separately. First the ``rx-assoc``
+Netlink command needs to be issued, specifying a target TCP socket.
+Kernel will allocate a new PSP Rx key from the NIC and associate it
+with given socket. At this stage socket will accept both PSP-secured
+and plain text TCP packets.
Quoted 8 lines

Documentation/networking/psp.rst line 84

Quoted 5 lines
Documentation/networking/psp.rst new file +5 -0
+
+Tx keys are installed using the ``tx-assoc`` Netlink command.
+Once the Tx keys are installed all data read from the socket will
+be PSP-secured. In other words act of installing Tx keys has the secondary
+effect on the Rx direction, requring all received packets to be encrypted.  

Consider clarifying the entire state diagram from when one pair initiates upgrade.

Not sure about state diagram, there are only 3 states. Or do you mean extend TCP state diagrams? I think a table may be better:

Event | Normal TCP | Rx PSP key present | Tx PSP key present |
---------------------------------------------------------------------------
Rx plain text | accept | accept | drop |

Rx PSP (good) | drop | accept | accept |

Rx PSP (bad) | drop | drop | drop |

Tx | plain text | plain text | encrypted * |

* data enqueued before Tx key in installed will not be encrypted (either initial send nor retranmissions)

What should I add?

Quoted 4 lines

And some edge cases:

- retransmits
- TCP fin handshake, if only one peer succeeds

So FIN when one end is "locked down" and the other isn't?

Quoted 1 line

- TCP control socket response to encrypted pkt

Control sock ignores PSP.

Quoted 7 lines

What is the expectation for data already queued for transmission when the tx assocation is made?

More generally, what happens for data in flight. One possible simplification is to only allow an upgrade sequence (possibly including in-band exchange of keys) when no other data is in flight.

Like TLS offload, the data is annotated "for encryption" when queued. So data queued earlier or retransmits of such data will never be encrypted.

Quoted 8 lines

Documentation/networking/psp.rst line 100

Quoted 4 lines
Documentation/networking/psp.rst new file +4 -0
+performed by management daemons, not under application control.
+The PSP netlink family will generate a notification whenever keys
+are rotated. The applications are expected to re-establish connections
+before keys are rotated again.  

Connection key rotation is not supported? I did notice that tx key insertion fails if a key is already present, so this does appear to be the behavior.

Correct, for now connections need to be re-established once a day. Rx should be easy, Tx we can make easy by only supporting rotation when there's no data queued.

maintainer
#9 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 17 lines · Jakub Kicinski
Quoted 16 lines · On Sun, 12 May 2024 21:24:23 -0400 Willem de Bruijn wrote:

Documentation/networking/psp.rst line 20

Quoted 11 lines · Jakub Kicinski
Documentation/networking/psp.rst new file +11 -0
+PSP Security Protocol (PSP) was defined at Google and published in:
+
+https://raw.githubusercontent.com/google/psp/main/doc/PSP_Arch_Spec.pdf
+
+This section briefly covers protocol aspects crucial for understanding
+the kernel API. Refer to the protocol specification for further details.
+
+Note that the kernel implementation and documentation uses the term
+"secret state" in place of "master key", it is both less confusing
+to an average developer and is less likely to run afoul any naming
+guidelines.  

There is some value in using the same terminology in the code as in the spec.

Quoted 6 lines
Quoted 4 lines

And the session keys are derived from a key. That is more precise than state. Specifically, counter-mode KDF from an AES key.

Perhaps device key, instead of master key?

Weak preference towards secret state, but device key works, too.

Totally your choice. I just wanted to make sure this was considered.

Quoted 12 lines
Quoted 11 lines

Documentation/networking/psp.rst line 28

Quoted 7 lines
Documentation/networking/psp.rst new file +7 -0
+Derived Rx keys
+---------------
+
+PSP borrows some terms and mechanisms from IPsec. PSP was designed
+with HW offloads in mind. The key feature of PSP is that Rx keys for every
+connection do not have to be stored by the receiver but can be derived
+from secret state and information present in packet headers.  

A second less obvious, but neat, feature is that it supports an encryption offset, such that (say) the L4 ports are integrity protected, but not encrypted, to allow for in-network telemetry.

Quoted 7 lines

I know, but the opening paragraph has:

This section briefly covers protocol aspects crucial for
understanding the kernel API. Refer to the protocol specification for further details.

:) .. and I didn't implement the offset, yet. (It's trivial to add and ETOOMANYPATCHES.)

Ack, sounds good.

Quoted 45 lines
Quoted 7 lines

Documentation/networking/psp.rst

Quoted 5 lines
Documentation/networking/psp.rst new file +4 -0
+This makes it possible to implement receivers which require a constant
+amount of memory regardless of the number of connections (``O(1)`` scaling).
+
+Tx keys have to be stored like with any other protocol,  

Keys can optionally be passed in descriptor.

Added: Preferably, the Tx keys should be provided with the packet (e.g. as part of the descriptors).

Quoted 14 lines
Quoted 14 lines
Documentation/networking/psp.rst new file +13 -0
+The expectation is that higher layer protocols will take care of
+protocol and key negotiation. For example one may use TLS key exchange,
+announce the PSP capability, and switch to PSP if both endpoints
+are PSP-capable.  
 
+Securing a connection
+---------------------
+
+PSP encryption is currently only supported for TCP connections.
+Rx and Tx keys are allocated separately. First the ``rx-assoc``
+Netlink command needs to be issued, specifying a target TCP socket.
+Kernel will allocate a new PSP Rx key from the NIC and associate it
+with given socket. At this stage socket will accept both PSP-secured
+and plain text TCP packets.
Quoted 8 lines

Documentation/networking/psp.rst line 84

Quoted 5 lines
Documentation/networking/psp.rst new file +5 -0
+
+Tx keys are installed using the ``tx-assoc`` Netlink command.
+Once the Tx keys are installed all data read from the socket will
+be PSP-secured. In other words act of installing Tx keys has the secondary
+effect on the Rx direction, requring all received packets to be encrypted.  

Consider clarifying the entire state diagram from when one pair initiates upgrade.

Not sure about state diagram, there are only 3 states. Or do you mean extend TCP state diagrams? I think a table may be better:

Event | Normal TCP | Rx PSP key present | Tx PSP key present |
---------------------------------------------------------------------------
Rx plain text | accept | accept | drop |

Rx PSP (good) | drop | accept | accept |

Rx PSP (bad) | drop | drop | drop |

Quoted 7 lines

Tx | plain text | plain text | encrypted * |

* data enqueued before Tx key in installed will not be encrypted (either initial send nor retranmissions)

What should I add?

I've mostly been concerned about the below edge cases.

If both peers are in TCP_ESTABLISHED for the during of the upgrade, and data is aligned on message boundary, things are straightforward.

The retransmit logic is clear, as this is controlled by skb->decrypted on the individual skbs on the retransmit queue.

That also solves another edge case: skb geometry changes on retransmit (due to different MSS or segs, using tcp_fragment, tso_fragment, tcp_retrans_try_collapse, ..) maintain skb->decrypted. It's not possible that skb is accidentally created that combines plaintext and ciphertext content.

Although.. does this require adding that skb->decrypted check to tcp_skb_can_collapse?

Quoted 6 lines
Quoted 4 lines

And some edge cases:

- retransmits
- TCP fin handshake, if only one peer succeeds

So FIN when one end is "locked down" and the other isn't?

If one peer can enter the state where it drops all plaintext, while the other decides to close the connection before completing the upgrade, and thus sends a plaintext FIN.

If (big if) that can happen, then the connection cannot be cleanly closed.

Quoted 3 lines
Quoted 1 line

- TCP control socket response to encrypted pkt

Control sock ignores PSP.

Another example where a peer stays open and stays retrying if it has upgraded and drops all plaintext.

author maintainer
#10 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 5 lines · On Wed, 29 May 2024 20:47:02 -0400 Willem de Bruijn wrote:
Quoted 4 lines · Jakub Kicinski
Quoted 3 lines · On Sun, 12 May 2024 21:24:23 -0400 Willem de Bruijn wrote:

There is some value in using the same terminology in the code as in the spec.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

And the session keys are derived from a key. That is more precise than state. Specifically, counter-mode KDF from an AES key.

Perhaps device key, instead of master key?

Weak preference towards secret state, but device key works, too.

Totally your choice. I just wanted to make sure this was considered.

Already run the sed, device key it is :)

Quoted 30 lines
Quoted 14 lines
Quoted 2 lines

Consider clarifying the entire state diagram from when one pair initiates upgrade.

Not sure about state diagram, there are only 3 states. Or do you mean extend TCP state diagrams? I think a table may be better:

Event | Normal TCP | Rx PSP key present | Tx PSP key present |
---------------------------------------------------------------------------
Rx plain text | accept | accept | drop |

Rx PSP (good) | drop | accept | accept |

Rx PSP (bad) | drop | drop | drop |

Quoted 7 lines

Tx | plain text | plain text | encrypted * |

* data enqueued before Tx key in installed will not be encrypted (either initial send nor retranmissions)

What should I add?

I've mostly been concerned about the below edge cases.

If both peers are in TCP_ESTABLISHED for the during of the upgrade, and data is aligned on message boundary, things are straightforward.

The retransmit logic is clear, as this is controlled by skb->decrypted on the individual skbs on the retransmit queue.

Quoted 8 lines

That also solves another edge case: skb geometry changes on retransmit (due to different MSS or segs, using tcp_fragment, tso_fragment, tcp_retrans_try_collapse, ..) maintain skb->decrypted. It's not possible that skb is accidentally created that combines plaintext and ciphertext content.

Although.. does this require adding that skb->decrypted check to tcp_skb_can_collapse?

Good catch. The TLS checks predate tcp_skb_can_collapse() (and MPTCP). We've grown the check in tcp_shift_skb_data() and the logic in tcp_grow_skb(), both missing the decrypted check.

I'll send some fixes, these are existing bugs :(

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

And some edge cases:

- retransmits
- TCP fin handshake, if only one peer succeeds

Quoted 8 lines
Quoted 1 line

So FIN when one end is "locked down" and the other isn't?

If one peer can enter the state where it drops all plaintext, while the other decides to close the connection before completing the upgrade, and thus sends a plaintext FIN.

If (big if) that can happen, then the connection cannot be cleanly closed.

Hm. And we can avoid this by only enforcing encryption of data-less segments once we've seen some encrypted data?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

- TCP control socket response to encrypted pkt

Control sock ignores PSP.

Another example where a peer stays open and stays retrying if it has upgraded and drops all plaintext.

author maintainer
#11 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 14 lines · On Thu, 30 May 2024 12:51:20 -0700 Jakub Kicinski wrote:
Quoted 14 lines

I've mostly been concerned about the below edge cases.

If both peers are in TCP_ESTABLISHED for the during of the upgrade, and data is aligned on message boundary, things are straightforward.

The retransmit logic is clear, as this is controlled by skb->decrypted on the individual skbs on the retransmit queue.

That also solves another edge case: skb geometry changes on retransmit (due to different MSS or segs, using tcp_fragment, tso_fragment, tcp_retrans_try_collapse, ..) maintain skb->decrypted. It's not possible that skb is accidentally created that combines plaintext and ciphertext content.

Quoted 8 lines
Quoted 2 lines

Although.. does this require adding that skb->decrypted check to tcp_skb_can_collapse?

Good catch. The TLS checks predate tcp_skb_can_collapse() (and MPTCP). We've grown the check in tcp_shift_skb_data() and the logic in tcp_grow_skb(), both missing the decrypted check.

I'll send some fixes, these are existing bugs :(

I take that back, we can depend on EOR like TLS does.

maintainer
#12 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 18 lines · Jakub Kicinski
Quoted 17 lines · On Thu, 30 May 2024 12:51:20 -0700 Jakub Kicinski wrote:
Quoted 16 lines

I've mostly been concerned about the below edge cases.

If both peers are in TCP_ESTABLISHED for the during of the upgrade, and data is aligned on message boundary, things are straightforward.

The retransmit logic is clear, as this is controlled by skb->decrypted on the individual skbs on the retransmit queue.

That also solves another edge case: skb geometry changes on retransmit (due to different MSS or segs, using tcp_fragment, tso_fragment, tcp_retrans_try_collapse, ..) maintain skb->decrypted. It's not possible that skb is accidentally created that combines plaintext and ciphertext content.

Although.. does this require adding that skb->decrypted check to tcp_skb_can_collapse?

Quoted 7 lines
Quoted 5 lines

Good catch. The TLS checks predate tcp_skb_can_collapse() (and MPTCP). We've grown the check in tcp_shift_skb_data() and the logic in tcp_grow_skb(), both missing the decrypted check.

I'll send some fixes, these are existing bugs :(

I take that back, we can depend on EOR like TLS does.

Oh yes. Neat solution.

This relies on userspace doing the right thing by passing MSG_EOR right? That is easy to get wrong. Should we still add a check or a WARN_ONCE. That would be net-next material.

maintainer
#13 lore raw

Re: [RFC net-next 01/15] psp: add documentation

In reply to Jakub Kicinski
Quoted 73 lines · Jakub Kicinski
Quoted 5 lines · On Wed, 29 May 2024 20:47:02 -0400 Willem de Bruijn wrote:
Quoted 4 lines · Jakub Kicinski
Quoted 3 lines · On Sun, 12 May 2024 21:24:23 -0400 Willem de Bruijn wrote:

There is some value in using the same terminology in the code as in the spec.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

And the session keys are derived from a key. That is more precise than state. Specifically, counter-mode KDF from an AES key.

Perhaps device key, instead of master key?

Weak preference towards secret state, but device key works, too.

Totally your choice. I just wanted to make sure this was considered.

Already run the sed, device key it is :)

Quoted 30 lines
Quoted 14 lines
Quoted 2 lines

Consider clarifying the entire state diagram from when one pair initiates upgrade.

Not sure about state diagram, there are only 3 states. Or do you mean extend TCP state diagrams? I think a table may be better:

Event | Normal TCP | Rx PSP key present | Tx PSP key present |
---------------------------------------------------------------------------
Rx plain text | accept | accept | drop |

Rx PSP (good) | drop | accept | accept |

Rx PSP (bad) | drop | drop | drop |

Quoted 7 lines

Tx | plain text | plain text | encrypted * |

* data enqueued before Tx key in installed will not be encrypted (either initial send nor retranmissions)

What should I add?

I've mostly been concerned about the below edge cases.

If both peers are in TCP_ESTABLISHED for the during of the upgrade, and data is aligned on message boundary, things are straightforward.

The retransmit logic is clear, as this is controlled by skb->decrypted on the individual skbs on the retransmit queue.

Quoted 8 lines

That also solves another edge case: skb geometry changes on retransmit (due to different MSS or segs, using tcp_fragment, tso_fragment, tcp_retrans_try_collapse, ..) maintain skb->decrypted. It's not possible that skb is accidentally created that combines plaintext and ciphertext content.

Although.. does this require adding that skb->decrypted check to tcp_skb_can_collapse?

Good catch. The TLS checks predate tcp_skb_can_collapse() (and MPTCP). We've grown the check in tcp_shift_skb_data() and the logic in tcp_grow_skb(), both missing the decrypted check.

I'll send some fixes, these are existing bugs :(

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

And some edge cases:

- retransmits
- TCP fin handshake, if only one peer succeeds

So FIN when one end is "locked down" and the other isn't?

If one peer can enter the state where it drops all plaintext, while the other decides to close the connection before completing the upgrade, and thus sends a plaintext FIN.

Quoted 5 lines
Quoted 2 lines

If (big if) that can happen, then the connection cannot be cleanly closed.

Hm. And we can avoid this by only enforcing encryption of data-less segments once we've seen some encrypted data?

That would help. It may also be needed to accept a pure ACK right at the upgrade seqno. Depends on the upgrade process.

Which may be worth documenting explicitly: the system call and network packet exchange from when one peer initiates (by generating its local key) until the connection is fully encrypted. That also allows poking at the various edge cases that may happen if packets are lost, or when actions can race.

One unexpected example of the latter that I came across was Tx SADB key insertion in tail edge cases taking longer than network RTT, for instance.

The kernel API can be exercised in a variety of ways, not all of them will uphold the correctness. Documenting how it should be used should help.

Even better when it reduces the option space. As it already does by failing a Tx key install until Rx is configured.

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

- TCP control socket response to encrypted pkt

Control sock ignores PSP.

Another example where a peer stays open and stays retrying if it has upgraded and drops all plaintext.

May want to always allow plaintext RSTs. This is a potential DoS vector. In all these cases, I suppose this has already been figured out for TLS.

author maintainer
#14 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 10 lines · On Fri, 31 May 2024 09:56:42 -0400 Willem de Bruijn wrote:
Quoted 9 lines
Quoted 6 lines

If one peer can enter the state where it drops all plaintext, while the other decides to close the connection before completing the upgrade, and thus sends a plaintext FIN.

If (big if) that can happen, then the connection cannot be cleanly closed.

Hm. And we can avoid this by only enforcing encryption of data-less segments once we've seen some encrypted data?

Quoted 8 lines

That would help. It may also be needed to accept a pure ACK right at the upgrade seqno. Depends on the upgrade process.

Which may be worth documenting explicitly: the system call and network packet exchange from when one peer initiates (by generating its local key) until the connection is fully encrypted. That also allows poking at the various edge cases that may happen if packets are lost, or when actions can race.

Dunno if the format below is good, but you're very right. At least to me writing the diagram was an hour well spent :)

Quoted 10 lines

One unexpected example of the latter that I came across was Tx SADB key insertion in tail edge cases taking longer than network RTT, for instance.

The kernel API can be exercised in a variety of ways, not all of them will uphold the correctness. Documenting how it should be used should help.

Even better when it reduces the option space. As it already does by failing a Tx key install until Rx is configured.

Something along these lines?

"Sequence" diagram of the worst case scenario:

01 p Host A Host B
02 l t ~~~~~~~~~~~[TCP 3 WHS]~~~~~~~~~~
03 a e ~~~~~~[crypto negotiation]~~~~~~
04 i x [Rx key alloc = K-B]
05 n t <--- [app] K-B key send
06 ------[Rx key alloc = K-A]-
07 [app] K-A key send -->|
08 [TCP] K-B input <-----

08 P      [TCP] K-B ACK ---->|
09 S R [app] recv(K-B)       |
10 P x [app] [Tx key set]    |  
11 -------------------------- 
12 P T [app] send(RPC) #####>|   
13 S x                       |<----    [TCP] Seq OoO! queue RPC, SACK
14 P      [TCP] retr K-A --->|   
15                           |  `->    [TCP] K-A input
16                           | <---    [TCP] K-A ACK (or FIN) 
17                           |      [app] recv(K-A)
18                           |      [app] [Tx key set]

19 -----------------------------------
20

There is a causal dependency between Host B allocating the key (line 4), sending it (line 5) and Host A receiving it (line 8). Since Host B will accept PSP packets as soon as it allocated the key, Host A does not need to wait to start using the key (line 12). Host B will queue the RPC to the socket (line 13).

[Problem #1]

However, because Host B does not have a Tx key, the ACK / SACK packet (line 13) will not be encrypted. (Similarly if Host B decided to close the connection at this point, the resulting FIN packet would not be encrypted.) Host B needs to accept unencrypted non-data segments (pure acks, pure FIN) until it sees an encrypted packet from Host B.

[Problem #2]

The retansmissions of K-A are unencrypted, to avoid sending the same data in encrypted and unencrypted form. This poses a risk if an ACK gets lost but both hosts end up in the PSP Tx state. Assume that Host A did not send the RPC (line 12), and the retransmission (line 14) happens as an RTO or TLP. Host B may already reach PSP Tx state (line "20") and expect encrypted data. Plain text retransmissions (with sequence number before rcv_nxt) must be accepted until Host B sees encrypted data from Host A.

With that I think the state machine needs to be amended:

Event | Normal TCP | Rx PSP | Tx PSP | PSP full |
-----------------------------------------------------------------------
Rx plain (new) | accept | accept | drop | drop |

Rx plain       | accept      | accept      | accept      | drop        |
(ACK|FIN|rtx)  |             |             |             |             |

Rx PSP (good) | drop | accept | accept | accept |

Rx PSP (bad    | drop        | drop        | drop        | drop        |
(crypt, !=SPI) |             |             |             |             |
Tx             | plain text  | plain text  | encrypted   | encrypted   |
               |             |             | (excl. rtx) | (excl. rtx) |
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Another example where a peer stays open and stays retrying if it has upgraded and drops all plaintext.

May want to always allow plaintext RSTs. This is a potential DoS vector.

Because of key exhaustion? Or we can be tricked into spamming someone with retranmissions and ignoring their RST?

Quoted 2 lines

In all these cases, I suppose this has already been figured out for TLS.

Assuming the answer above is "key exhaustion" - I wouldn't be surprised if it wasn't :(

maintainer
#15 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 14 lines · Jakub Kicinski
Quoted 13 lines · On Fri, 31 May 2024 09:56:42 -0400 Willem de Bruijn wrote:
Quoted 9 lines
Quoted 6 lines

If one peer can enter the state where it drops all plaintext, while the other decides to close the connection before completing the upgrade, and thus sends a plaintext FIN.

If (big if) that can happen, then the connection cannot be cleanly closed.

Hm. And we can avoid this by only enforcing encryption of data-less segments once we've seen some encrypted data?

That would help. It may also be needed to accept a pure ACK right at the upgrade seqno. Depends on the upgrade process.

Quoted 8 lines
Quoted 5 lines

Which may be worth documenting explicitly: the system call and network packet exchange from when one peer initiates (by generating its local key) until the connection is fully encrypted. That also allows poking at the various edge cases that may happen if packets are lost, or when actions can race.

Dunno if the format below is good, but you're very right. At least to me writing the diagram was an hour well spent :)

Great :)

Quoted 43 lines
Quoted 10 lines

One unexpected example of the latter that I came across was Tx SADB key insertion in tail edge cases taking longer than network RTT, for instance.

The kernel API can be exercised in a variety of ways, not all of them will uphold the correctness. Documenting how it should be used should help.

Even better when it reduces the option space. As it already does by failing a Tx key install until Rx is configured.

Something along these lines?

"Sequence" diagram of the worst case scenario:

01 p Host A Host B
02 l t ~~~~~~~~~~~[TCP 3 WHS]~~~~~~~~~~
03 a e ~~~~~~[crypto negotiation]~~~~~~
04 i x [Rx key alloc = K-B]
05 n t <--- [app] K-B key send
06 ------[Rx key alloc = K-A]-
07 [app] K-A key send -->|
08 [TCP] K-B input <-----

08 P      [TCP] K-B ACK ---->|
09 S R [app] recv(K-B)       |
10 P x [app] [Tx key set]    |  
11 -------------------------- 
12 P T [app] send(RPC) #####>|   
13 S x                       |<----    [TCP] Seq OoO! queue RPC, SACK
14 P      [TCP] retr K-A --->|   
15                           |  `->    [TCP] K-A input
16                           | <---    [TCP] K-A ACK (or FIN) 
17                           |      [app] recv(K-A)
18                           |      [app] [Tx key set]

19 -----------------------------------
20

There is a causal dependency between Host B allocating the key (line 4), sending it (line 5) and Host A receiving it (line 8). Since Host B will accept PSP packets as soon as it allocated the key, Host A does not need to wait to start using the key (line 12). Host B will queue the RPC to the socket (line 13).

Quoted 6 lines

[Problem #1]

However, because Host B does not have a Tx key, the ACK / SACK packet (line 13) will not be encrypted. (Similarly if Host B decided to close the connection at this point, the resulting FIN packet would not be encrypted.)

Or if it plays SO_LINGER games the resulting RST.

Quoted 5 lines

Host B needs to accept unencrypted non-data segments (pure acks, pure FIN) until it sees an encrypted packet from Host B.

[Problem #2]

Quoted 8 lines

The retansmissions of K-A are unencrypted, to avoid sending the same data in encrypted and unencrypted form. This poses a risk if an ACK gets lost but both hosts end up in the PSP Tx state. Assume that Host A did not send the RPC (line 12), and the retransmission (line 14) happens as an RTO or TLP. Host B may already reach PSP Tx state (line "20") and expect encrypted data. Plain text retransmissions (with sequence number before rcv_nxt) must be accepted until Host B sees encrypted data from Host A.

Is that sufficient if an initial encrypted packet could get reordered by the network to arrive before a plaintext retransmit of a lower seqno?

Both scenarios make sense. It is unfortunately harder to be sure that we have captured all edge cases.

An issue related to the rcv_nxt cut-point, not sure how important: the plaintext packet contents are protected by user crypto before upgrade. But the TCP headers are not. PSP relies on TCP PAWS against replay protection. It is possible for a MITM to offset all seqno from the start of connection establishment. I don't see an immediate issue. But at a minimum it could be possible to insert or delete before PSP is upgraded.

Quoted 18 lines

With that I think the state machine needs to be amended:

Event | Normal TCP | Rx PSP | Tx PSP | PSP full |
-----------------------------------------------------------------------
Rx plain (new) | accept | accept | drop | drop |

Rx plain       | accept      | accept      | accept      | drop        |
(ACK|FIN|rtx)  |             |             |             |             |

Rx PSP (good) | drop | accept | accept | accept |

Rx PSP (bad    | drop        | drop        | drop        | drop        |
(crypt, !=SPI) |             |             |             |             |
Tx             | plain text  | plain text  | encrypted   | encrypted   |
               |             |             | (excl. rtx) | (excl. rtx) |
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Another example where a peer stays open and stays retrying if it has upgraded and drops all plaintext.

May want to always allow plaintext RSTs. This is a potential DoS vector.

Because of key exhaustion? Or we can be tricked into spamming someone with retranmissions and ignoring their RST?

Simpler: this falls back onto unencrypted TCP where someone capable of spoofing valid data is capable of terminating a connection.

If denying all plaintext after upgrade, PSP protects against this.

It is arguably low on the list of concerns, especially in a closed world hyperscaler setting. As it is hardly the only DoS vector.

Quoted 5 lines
Quoted 2 lines

In all these cases, I suppose this has already been figured out for TLS.

Assuming the answer above is "key exhaustion" - I wouldn't be surprised if it wasn't :(

author maintainer
#16 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 9 lines · On Wed, 05 Jun 2024 16:11:31 -0400 Willem de Bruijn wrote:
Quoted 8 lines

The retansmissions of K-A are unencrypted, to avoid sending the same data in encrypted and unencrypted form. This poses a risk if an ACK gets lost but both hosts end up in the PSP Tx state. Assume that Host A did not send the RPC (line 12), and the retransmission (line 14) happens as an RTO or TLP. Host B may already reach PSP Tx state (line "20") and expect encrypted data. Plain text retransmissions (with sequence number before rcv_nxt) must be accepted until Host B sees encrypted data from Host A.

Quoted 3 lines

Is that sufficient if an initial encrypted packet could get reordered by the network to arrive before a plaintext retransmit of a lower seqno?

Yes, I believe that's fine.

I will document this clearer but both sides must be pretty precise in their understanding when the switchover happens. They must read what they expect to be clear text, and then install the Tx key thus locking down the socket.

1. If they under-read and clear text data is already queued - the kernel will error out.
2. If they under-read and clear text arrives later - the connection will hang.
3. If they over-read they will presumably get PSP-protected data which they have no way of validating, since it won't be secured by user crypto.

We could protect from over-read (case 3) by refusing to give out PSP-protected data until keys are installed. But it adds to the fast path and I don't think it's all that beneficial, since there's no way to protect a sloppy application from under-read (case 2).

Back to your question about reordering plain text with cipher text:
the application should not lock down the socket until it gets all its clear text. So clear text retransmissions _after_ lock down must be spurious. The only worry is that we lose an ACK and never tell the other side that we got all the clear text. But we're guaranteed to successfully ACK any PSP-protected data, so if we receive some there is no way to get stuck. Let me copy / paste the diagram:

01 p Host A Host B
02 l t ~~~~~~~~~~~[TCP 3 WHS]~~~~~~~~~~
03 a e ~~~~~~[crypto negotiation]~~~~~~
04 i x [Rx key alloc = K-B]
05 n t <--- [app] K-B key send
06 ------[Rx key alloc = K-A]-
07 [app] K-A key send -->|
08 [TCP] K-B input <-----

08 P      [TCP] K-B ACK ---->|
09 S R [app] recv(K-B)       |
10 P x [app] [Tx key set]    |  
11 -------------------------- 
12 P T [app] send(RPC) #####>|   
13 S x                       |<----    [TCP] Seq OoO! queue RPC, SACK
14 P      [TCP] retr K-A --->|   
15                           |  `->    [TCP] K-A input
16                           | <---    [TCP] K-A ACK (or FIN) 
17                           |      [app] recv(K-A)
18                           |      [app] [Tx key set]

19 -----------------------------------
20

Looking as Host A, if we receive encrypted data, we must have allocated and sent key (line 7) so we will start accepting encrypted data. But at this point we are also accepting plain text (until we reach line 9). We will send a plain text (S)ACK to encrypted data, but that's fine too since Host B hasn't seen any encrypted data from us and will accept such ACKs.

Quoted 2 lines

Both scenarios make sense. It is unfortunately harder to be sure that we have captured all edge cases.

Are you trying to say packetdrill without saying packetdrill? :)

Quoted 7 lines

An issue related to the rcv_nxt cut-point, not sure how important: the plaintext packet contents are protected by user crypto before upgrade. But the TCP headers are not. PSP relies on TCP PAWS against replay protection. It is possible for a MITM to offset all seqno from the start of connection establishment. I don't see an immediate issue. But at a minimum it could be possible to insert or delete before PSP is upgraded.

Yes, the "cut off" point must be quite clearly defined, because both sides must precisely read out all the clear text. Then they install the Tx key and anything they read must have been PSP-protected.

Hope I understood the point.

maintainer
#17 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 34 lines · Jakub Kicinski
Quoted 9 lines · On Wed, 05 Jun 2024 16:11:31 -0400 Willem de Bruijn wrote:
Quoted 8 lines

The retansmissions of K-A are unencrypted, to avoid sending the same data in encrypted and unencrypted form. This poses a risk if an ACK gets lost but both hosts end up in the PSP Tx state. Assume that Host A did not send the RPC (line 12), and the retransmission (line 14) happens as an RTO or TLP. Host B may already reach PSP Tx state (line "20") and expect encrypted data. Plain text retransmissions (with sequence number before rcv_nxt) must be accepted until Host B sees encrypted data from Host A.

Quoted 3 lines

Is that sufficient if an initial encrypted packet could get reordered by the network to arrive before a plaintext retransmit of a lower seqno?

Yes, I believe that's fine.

I will document this clearer but both sides must be pretty precise in their understanding when the switchover happens. They must read what they expect to be clear text, and then install the Tx key thus locking down the socket.

1. If they under-read and clear text data is already queued - the kernel will error out.
2. If they under-read and clear text arrives later - the connection will hang.
3. If they over-read they will presumably get PSP-protected data which they have no way of validating, since it won't be secured by user crypto.

We could protect from over-read (case 3) by refusing to give out PSP-protected data until keys are installed. But it adds to the fast path and I don't think it's all that beneficial, since there's no way to protect a sloppy application from under-read (case 2).

Quoted 4 lines

Back to your question about reordering plain text with cipher text:
the application should not lock down the socket until it gets all its clear text. So clear text retransmissions _after_ lock down must be spurious.

Ah yes, good point.

Quoted 34 lines

The only worry is that we lose an ACK and never tell
the other side that we got all the clear text. But we're guaranteed to successfully ACK any PSP-protected data, so if we receive some there is no way to get stuck. Let me copy / paste the diagram:

01 p Host A Host B
02 l t ~~~~~~~~~~~[TCP 3 WHS]~~~~~~~~~~
03 a e ~~~~~~[crypto negotiation]~~~~~~
04 i x [Rx key alloc = K-B]
05 n t <--- [app] K-B key send
06 ------[Rx key alloc = K-A]-
07 [app] K-A key send -->|
08 [TCP] K-B input <-----

08 P      [TCP] K-B ACK ---->|
09 S R [app] recv(K-B)       |
10 P x [app] [Tx key set]    |  
11 -------------------------- 
12 P T [app] send(RPC) #####>|   
13 S x                       |<----    [TCP] Seq OoO! queue RPC, SACK
14 P      [TCP] retr K-A --->|   
15                           |  `->    [TCP] K-A input
16                           | <---    [TCP] K-A ACK (or FIN) 
17                           |      [app] recv(K-A)
18                           |      [app] [Tx key set]

19 -----------------------------------
20

Looking as Host A, if we receive encrypted data, we must have allocated and sent key (line 7) so we will start accepting encrypted data. But at this point we are also accepting plain text (until we reach line 9). We will send a plain text (S)ACK to encrypted data, but that's fine too since Host B hasn't seen any encrypted data from us and will accept such ACKs.

Quoted 4 lines
Quoted 2 lines

Both scenarios make sense. It is unfortunately harder to be sure that we have captured all edge cases.

Are you trying to say packetdrill without saying packetdrill? :)

Ha, no, no such hint implied.

I did expand packetdrill to PSP to exercise the cases that I could come up with, at a minimum to ensure coverage of all branches.

But does that cover all edge cases possible? Including drops, reorders, geometry changes from MTU changes, SO_LINGER 0, races with slow OS operations (like that slow SADB insertion I mentioned)? The unknown unknowns. Stuck connections are a low risk, bugs that can be fixed later. As long as it is easy to reason that actual crypto issues like plaintext leaks are not reachable.

Extending packetdrill to netlink would be quite some work, I suspect. A quick scan shows that it knows NLA, but only for OPT_STATS decoding.

Quoted 8 lines
Quoted 7 lines

An issue related to the rcv_nxt cut-point, not sure how important: the plaintext packet contents are protected by user crypto before upgrade. But the TCP headers are not. PSP relies on TCP PAWS against replay protection. It is possible for a MITM to offset all seqno from the start of connection establishment. I don't see an immediate issue. But at a minimum it could be possible to insert or delete before PSP is upgraded.

Quoted 5 lines

Yes, the "cut off" point must be quite clearly defined, because both sides must precisely read out all the clear text. Then they install the Tx key and anything they read must have been PSP-protected.

Hope I understood the point.

I think the issue, if any, is that there may be a gap between the two methods of integrity protection. What we call "cleartext" here is integrity protected such that no insertion or deletion attacks are possible. And PSP ensures the same. But is a a deletion of the last plaintext or first ciphertext possible?

An insertion is not an issue as it will be protected by neither, while PSP is expected, so it is dropped.

As long as the application (or is it presentation?) layer has a clear definition of at what point in the stream it must insert the Tx key, plaintext deletion is not possible, as the key is not inserted until all plaintext has been received.

Which leaves: is it possible for a MITM to offset the seqno, such that the first PSP encrypted packet can be removed from the stream and this goes undetected?

#18 lore raw

Re: [RFC net-next 01/15] psp: add documentation

In reply to Jakub Kicinski
Quoted 2 lines · Jakub Kicinski

On Sun, 12 May 2024 21:24:23 -0400 Willem de Bruijn wrote:

... snip ...

Quoted 8 lines
Quoted 3 lines

Connection key rotation is not supported? I did notice that tx key insertion fails if a key is already present, so this does appear to be the behavior.

Correct, for now connections need to be re-established once a day. Rx should be easy, Tx we can make easy by only supporting rotation when there's no data queued.

Could you elaborate on why updating the Tx key should only be allowed when no data is queued? At the point rekeying is being done, the receiver should accept both the new and previous key:spi.

The lack of support for rekeying existing connections is a significant gap. At a minimum the API for notifying the application that a rotation has occurred should be defined, and the implementation should allow the configuration of a new Tx key:spi for rekeying. A tiny bit logic would also be needed on the Rx side to track the current and previous SPI, if the hardware supports keys indescriptors then nothing more should be needed on the Tx side. If the NIC maintains an SA database and doesn't allow existing entries to be updated, a small amount of additional logic would be needed, but perhaps that could be (waving hands a bit) the responsibility of the driver.

- Lance

author maintainer
#19 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 11 lines · On Thu, 27 Jun 2024 11:14:39 -0400 Lance Richardson wrote:
Quoted 7 lines
Quoted 3 lines

Connection key rotation is not supported? I did notice that tx key insertion fails if a key is already present, so this does appear to be the behavior.

Correct, for now connections need to be re-established once a day. Rx should be easy, Tx we can make easy by only supporting rotation when there's no data queued.

Could you elaborate on why updating the Tx key should only be allowed when no data is queued? At the point rekeying is being done, the receiver should accept both the new and previous key:spi.

I didn't say it shouldn't be allowed, just that disallowing it initially would make the implementation easier ;)

Quoted 3 lines

The lack of support for rekeying existing connections is a significant gap. At a minimum the API for notifying the application that a rotation has occurred should be defined,

Notifications are in place, that's one of the reasons I chose netlink.

Quoted 2 lines

and the implementation should allow the configuration of a new Tx key:spi for rekeying.

I was under the possibly mistaken impression that Google have used PSP for years without rekeying... Did I misunderstand?

Quoted 7 lines

A tiny bit logic would also be needed on the Rx
side to track the current and previous SPI, if the hardware supports keys indescriptors then nothing more should be needed on the Tx side. If the NIC maintains an SA database and doesn't allow existing entries to be updated, a small amount of additional logic would be needed, but perhaps that could be (waving hands a bit) the responsibility of the driver.

Interesting. Hm. But SADB drivers would then have to implement some complex logic to make sure all rings have cycled, or take references. I'd rather have an opt-in for core to delay reaping old keys until all sockets which used them went empty at least once (in wmem sense).

#20 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 4 lines · Jakub Kicinski

I was under the possibly mistaken impression that Google have used PSP for years without rekeying... Did I misunderstand?

Actually Google does implement connection rekeying when master key rotation occurs. I believe this was the case even in the first production deployment (Willem would know the history better).

Quoted 8 lines
Quoted 7 lines

A tiny bit logic would also be needed on the Rx
side to track the current and previous SPI, if the hardware supports keys indescriptors then nothing more should be needed on the Tx side. If the NIC maintains an SA database and doesn't allow existing entries to be updated, a small amount of additional logic would be needed, but perhaps that could be (waving hands a bit) the responsibility of the driver.

Quoted 4 lines

Interesting. Hm. But SADB drivers would then have to implement some complex logic to make sure all rings have cycled, or take references. I'd rather have an opt-in for core to delay reaping old keys until all sockets which used them went empty at least once (in wmem sense).

Right, ensuring that the old entry is no longer referenced by packets in the transmit pipeline before removing is definitely a concern. One simple approach is to simply keep the old entry around for long enough (e.g. a minute or two) to ensure that any packets referencing it have been transmitted.

author maintainer
#21 lore raw

Re: [RFC net-next 01/15] psp: add documentation

Quoted 9 lines · On Fri, 28 Jun 2024 15:33:28 -0400 Lance Richardson wrote:
Quoted 4 lines

Interesting. Hm. But SADB drivers would then have to implement some complex logic to make sure all rings have cycled, or take references. I'd rather have an opt-in for core to delay reaping old keys until all sockets which used them went empty at least once (in wmem sense).

Right, ensuring that the old entry is no longer referenced by packets in the transmit pipeline before removing is definitely a concern. One simple approach is to simply keep the old entry around for long enough (e.g. a minute or two) to ensure that any packets referencing it have been transmitted.

Sounds good, we can opportunistically remove immediately if socket is idle, otherwise use a timeout. I'll try to have the patches on GH before I post v1, it won't all fit in one reviewable series.

author maintainer
#22 lore raw

RFC 2/15 psp: base PSP device support

In reply to Jakub Kicinski

Add a netlink family for PSP and allow drivers to register support.

The "PSP device" is its own object. This allows us to perform more flexible reference counting / lifetime control than if PSP information was part of net_device. In the future we should also be able to "delegate" PSP access to software devices, such as *vlan, veth or netkit more easily.

---
 Documentation/netlink/specs/psp.yaml |  94 +++++++++++
 include/linux/netdevice.h            |   4 +
 include/net/psp.h                    |  12 ++
 include/net/psp/functions.h          |  14 ++
 include/net/psp/types.h              | 102 ++++++++++++
 include/uapi/linux/psp.h             |  42 +++++
 net/Kconfig                          |   1 +
 net/Makefile                         |   1 +
 net/psp/Kconfig                      |  13 ++
 net/psp/Makefile                     |   5 +
 net/psp/psp-nl-gen.c                 |  65 ++++++++
 net/psp/psp-nl-gen.h                 |  30 ++++
 net/psp/psp.h                        |  31 ++++
 net/psp/psp_main.c                   | 130 ++++++++++++++++
 net/psp/psp_nl.c                     | 223 +++++++++++++++++++++++++++
 tools/net/ynl/Makefile.deps          |   1 +
 16 files changed, 768 insertions(+)
 create mode 100644 Documentation/netlink/specs/psp.yaml
 create mode 100644 include/net/psp.h
 create mode 100644 include/net/psp/functions.h
 create mode 100644 include/net/psp/types.h
 create mode 100644 include/uapi/linux/psp.h
 create mode 100644 net/psp/Kconfig
 create mode 100644 net/psp/Makefile
 create mode 100644 net/psp/psp-nl-gen.c
 create mode 100644 net/psp/psp-nl-gen.h
 create mode 100644 net/psp/psp.h
 create mode 100644 net/psp/psp_main.c
 create mode 100644 net/psp/psp_nl.c
Documentation/netlink/specs/psp.yaml new file +94 -0
new file mode 100644
index 000000000000..dbb5ef148045
@@ -0,0 +1,94 @@
+# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
+
+name: psp
+
+doc:
+  PSP Security Protocol Generic Netlink family.
+
+definitions:
+  -
+    type: enum
+    name: version
+    entries: [ hdr0-aes-gcm-128, hdr0-aes-gcm-256,
+               hdr0-aes-gmac-128, hdr0-aes-gmac-256, ]
+
+attribute-sets:
+  -
+    name: dev
+    attributes:
+      -
+        name: id
+        doc: PSP device ID.
+        type: u32
+        checks:
+          min: 1
+      -
+        name: ifindex
+        doc: ifindex of the main netdevice linked to the PSP device.
+        type: u32
+      -
+        name: psp-versions-cap
+        doc: Bitmask of PSP versions supported by the device.
+        type: u32
+        enum: version
+        enum-as-flags: true
+      -
+        name: psp-versions-ena
+        doc: Bitmask of currently enabled (accepted on Rx) PSP versions.
+        type: u32
+        enum: version
+        enum-as-flags: true
+
+operations:
+  list:
+    -
+      name: dev-get
+      doc: Get / dump information about PSP capable devices on the system.
+      attribute-set: dev
+      do:
+        request:
+          attributes:
+            - id
+        reply: &dev-all
+          attributes:
+            - id
+            - ifindex
+            - psp-versions-cap
+            - psp-versions-ena
+        pre: psp-device-get-locked
+        post: psp-device-unlock
+      dump:
+        reply: *dev-all
+    -
+      name: dev-add-ntf
+      doc: Notification about device appearing.
+      notify: dev-get
+      mcgrp: mgmt
+    -
+      name: dev-del-ntf
+      doc: Notification about device disappearing.
+      notify: dev-get
+      mcgrp: mgmt
+    -
+      name: dev-set
+      doc: Set the configuration of a PSP device.
+      attribute-set: dev
+      do:
+        request:
+          attributes:
+            - id
+            - psp-versions-ena
+        reply:
+          attributes: []
+        pre: psp-device-get-locked
+        post: psp-device-unlock
+    -
+      name: dev-change-ntf
+      doc: Notification about device configuration being changed.
+      notify: dev-get
+      mcgrp: mgmt
+
+mcast-groups:
+  list:
+    -
+      name: mgmt
include/linux/netdevice.h +4 -0
index cf261fb89d73..7327ed157bc2 100644
@@ -1874,6 +1874,7 @@ enum netdev_reg_state {
  *			 device struct
  *	@mpls_ptr:	mpls_dev struct pointer
  *	@mctp_ptr:	MCTP specific data
+ *	@psp_dev:	PSP crypto device registered for this netdev
  *
  *	@dev_addr:	Hw address (before bcast,
  *			because most packets are unicast)
@@ -2251,6 +2252,9 @@ struct net_device {
 #if IS_ENABLED(CONFIG_MCTP)
 	struct mctp_dev __rcu	*mctp_ptr;
 #endif
+#if IS_ENABLED(CONFIG_INET_PSP)
+	struct psp_dev __rcu	*psp_dev;
+#endif
 
 /*
  * Cache lines mostly used on receive path (including eth_type_trans())
include/net/psp.h new file +12 -0
new file mode 100644
index 000000000000..33bb4d1dc46e
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __NET_PSP_ALL_H
+#define __NET_PSP_ALL_H
+
+#include <uapi/linux/psp.h>
+#include <net/psp/functions.h>
+#include <net/psp/types.h>
+
+/* Do not add any code here. Put it in the sub-headers instead. */
+
+#endif /* __NET_PSP_ALL_H */
include/net/psp/functions.h new file +14 -0
new file mode 100644
index 000000000000..074f9df9afc3
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __NET_PSP_HELPERS_H
+#define __NET_PSP_HELPERS_H
+
+#include <net/psp/types.h>
+
+/* Driver-facing API */
+struct psp_dev *
+psp_dev_create(struct net_device *netdev, struct psp_dev_ops *psd_ops,
+	       struct psp_dev_caps *psd_caps, void *priv_ptr);
+void psp_dev_unregister(struct psp_dev *psd);
+
+#endif /* __NET_PSP_HELPERS_H */
include/net/psp/types.h new file +102 -0

102 lines not on this page · raw message

include/uapi/linux/psp.h new file +42 -0

42 lines not on this page · raw message

net/Kconfig +1 -0
index f0a8692496ff..3079ed6711c0 100644
@@ -69,6 +69,7 @@ config SKB_EXTENSIONS
 menu "Networking options"
 
 source "net/packet/Kconfig"
+source "net/psp/Kconfig"
 source "net/unix/Kconfig"
 source "net/tls/Kconfig"
 source "net/xfrm/Kconfig"
net/Makefile +1 -0
index 65bb8c72a35e..c47b1ae4540d 100644
@@ -18,6 +18,7 @@ obj-$(CONFIG_INET)		+= ipv4/
 obj-$(CONFIG_TLS)		+= tls/
 obj-$(CONFIG_XFRM)		+= xfrm/
 obj-$(CONFIG_UNIX)		+= unix/
+obj-$(CONFIG_INET_PSP)		+= psp/
 obj-y				+= ipv6/
 obj-$(CONFIG_PACKET)		+= packet/
 obj-$(CONFIG_NET_KEY)		+= key/
net/psp/Kconfig new file +13 -0
new file mode 100644
index 000000000000..55f9dd87446b
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# PSP configuration
+#
+config INET_PSP
+	bool "PSP Security Protocol support"
+	depends on INET
+	help
+	Enable kernel support for the PSP protocol.
+	For more information see:
+	  https://raw.githubusercontent.com/google/psp/main/doc/PSP_Arch_Spec.pdf
+
+	If unsure, say N.
net/psp/Makefile new file +5 -0
new file mode 100644
index 000000000000..41b51d06e560
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_INET_PSP) += psp.o
+
+psp-y := psp_main.o psp_nl.o psp-nl-gen.o
net/psp/psp-nl-gen.c new file +65 -0

65 lines not on this page · raw message

net/psp/psp-nl-gen.h new file +30 -0
new file mode 100644
index 000000000000..a099686cab5d
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/* Do not edit directly, auto-generated from: */
+/*	Documentation/netlink/specs/psp.yaml */
+/* YNL-GEN kernel header */
+
+#ifndef _LINUX_PSP_GEN_H
+#define _LINUX_PSP_GEN_H
+
+#include <net/netlink.h>
+#include <net/genetlink.h>
+
+#include <uapi/linux/psp.h>
+
+int psp_device_get_locked(const struct genl_split_ops *ops,
+			  struct sk_buff *skb, struct genl_info *info);
+void
+psp_device_unlock(const struct genl_split_ops *ops, struct sk_buff *skb,
+		  struct genl_info *info);
+
+int psp_nl_dev_get_doit(struct sk_buff *skb, struct genl_info *info);
+int psp_nl_dev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
+int psp_nl_dev_set_doit(struct sk_buff *skb, struct genl_info *info);
+
+enum {
+	PSP_NLGRP_MGMT,
+};
+
+extern struct genl_family psp_nl_family;
+
+#endif /* _LINUX_PSP_GEN_H */
net/psp/psp.h new file +31 -0
new file mode 100644
index 000000000000..94d0cc31a61f
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __PSP_PSP_H
+#define __PSP_PSP_H
+
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <net/netns/generic.h>
+#include <net/psp.h>
+#include <net/sock.h>
+
+extern struct xarray psp_devs;
+extern struct mutex psp_devs_lock;
+
+void psp_dev_destroy(struct psp_dev *psd);
+int psp_dev_check_access(struct psp_dev *psd, struct net *net);
+
+void psp_nl_notify_dev(struct psp_dev *psd, u32 cmd);
+
+static inline void psp_dev_get(struct psp_dev *psd)
+{
+	refcount_inc(&psd->refcnt);
+}
+
+static inline void psp_dev_put(struct psp_dev *psd)
+{
+	if (refcount_dec_and_test(&psd->refcnt))
+		psp_dev_destroy(psd);
+}
+
+#endif /* __PSP_PSP_H */
net/psp/psp_main.c new file +130 -0

130 lines not on this page · raw message

net/psp/psp_nl.c new file +223 -0

223 lines not on this page · raw message

tools/net/ynl/Makefile.deps +1 -0
index f4e8eb79c1b8..e191ea3cefc0 100644
@@ -25,3 +25,4 @@ CFLAGS_nfsd:=$(call get_hdr_inc,_LINUX_NFSD_NETLINK_H,nfsd_netlink.h)
 CFLAGS_ovs_datapath:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
 CFLAGS_ovs_flow:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
 CFLAGS_ovs_vport:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
+CFLAGS_psp:=$(call get_hdr_inc,_LINUX_PSP_H,psp.h)
Signature · 1 line
2.45.0
author maintainer
#23 lore raw

RFC 3/15 net: modify core data structures for PSP datapath support

In reply to Jakub Kicinski

Add pointers to psp data structures to core networking structs, and an SKB extension to carry the PSP information from the drivers to the socket layer.

---
Split out to a separate patch for ease of review,
I will squash if that's not helpful.

---
 include/linux/skbuff.h          | 3 +++
 include/linux/tcp.h             | 3 +++
 include/net/psp/functions.h     | 5 +++++
 include/net/psp/types.h         | 7 +++++++
 include/net/sock.h              | 4 ++++
 net/core/skbuff.c               | 4 ++++
 net/core/sock.c                 | 2 ++
 net/ipv4/inet_connection_sock.c | 2 ++
 net/ipv4/tcp_minisocks.c        | 6 ++++--
 net/mptcp/protocol.c            | 2 ++
 10 files changed, 36 insertions(+), 2 deletions(-)
include/linux/skbuff.h +3 -0
index c0b97c93a6de..4689255c66d2 100644
@@ -4659,6 +4659,9 @@ enum skb_ext_id {
 #endif
 #if IS_ENABLED(CONFIG_MCTP_FLOWS)
 	SKB_EXT_MCTP,
+#endif
+#if IS_ENABLED(CONFIG_INET_PSP)
+	SKB_EXT_PSP,
 #endif
 	SKB_EXT_NUM, /* must be last */
 };
include/linux/tcp.h +3 -0
index 6a5e08b937b3..368ea3a2b338 100644
@@ -551,6 +551,9 @@ struct tcp_timewait_sock {
 #ifdef CONFIG_TCP_AO
 	struct tcp_ao_info	__rcu *ao_info;
 #endif
+#if IS_ENABLED(CONFIG_INET_PSP)
+	struct psp_assoc __rcu	  *psp_assoc;
+#endif
 };
 
 static inline struct tcp_timewait_sock *tcp_twsk(const struct sock *sk)
include/net/psp/functions.h +5 -0
index 074f9df9afc3..9ff0f2b5744f 100644
@@ -5,10 +5,15 @@
 
 #include <net/psp/types.h>
 
+struct tcp_timewait_sock;
+
 /* Driver-facing API */
 struct psp_dev *
 psp_dev_create(struct net_device *netdev, struct psp_dev_ops *psd_ops,
 	       struct psp_dev_caps *psd_caps, void *priv_ptr);
 void psp_dev_unregister(struct psp_dev *psd);
 
+static inline void psp_sk_assoc_free(struct sock *sk) { }
+static inline void psp_twsk_assoc_free(struct tcp_timewait_sock *tw) { }
+
 #endif /* __NET_PSP_HELPERS_H */
include/net/psp/types.h +7 -0
index dbc5423a53df..a23d9bd9ce96 100644
@@ -86,6 +86,13 @@ struct psp_dev_caps {
 #define PSP_V1_KEY	32
 #define PSP_MAX_KEY	32
 
+struct psp_skb_ext {
+	__be32 spi;
+	/* generation and version are 8b but we don't want holes */
+	u16 generation;
+	u16 version;
+};
+
 /**
  * struct psp_dev_ops - netdev driver facing PSP callbacks
  */
include/net/sock.h +4 -0
index 0450494a1766..dc4c46ac0984 100644
@@ -249,6 +249,7 @@ struct sk_filter;
   *	@sk_dst_cache: destination cache
   *	@sk_dst_pending_confirm: need to confirm neighbour
   *	@sk_policy: flow policy
+  *	@psp_assoc: PSP association, if socket is PSP-secured
   *	@sk_receive_queue: incoming packets
   *	@sk_wmem_alloc: transmit queue bytes committed
   *	@sk_tsq_flags: TCP Small Queues flags
@@ -436,6 +437,9 @@ struct sock {
 	struct mem_cgroup	*sk_memcg;
 #ifdef CONFIG_XFRM
 	struct xfrm_policy __rcu *sk_policy[2];
+#endif
+#if IS_ENABLED(CONFIG_INET_PSP)
+	struct psp_assoc __rcu	*psp_assoc;
 #endif
 	__cacheline_group_end(sock_read_rxtx);
 
net/core/skbuff.c +4 -0
index 466999a7515e..1b6821d8dede 100644
@@ -77,6 +77,7 @@
 #include <net/mptcp.h>
 #include <net/mctp.h>
 #include <net/page_pool/helpers.h>
+#include <net/psp/types.h>
 #include <net/dropreason.h>
 
 #include <linux/uaccess.h>
@@ -4957,6 +4958,9 @@ static const u8 skb_ext_type_len[] = {
 #if IS_ENABLED(CONFIG_MCTP_FLOWS)
 	[SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow),
 #endif
+#if IS_ENABLED(CONFIG_INET_PSP)
+	[SKB_EXT_PSP] = SKB_EXT_CHUNKSIZEOF(struct psp_skb_ext),
+#endif
 };
 
 static __always_inline unsigned int skb_ext_total_length(void)
net/core/sock.c +2 -0
index 8d6e638b5426..24e9113e0417 100644
@@ -142,6 +142,7 @@
 #include <trace/events/sock.h>
 
 #include <net/tcp.h>
+#include <net/psp.h>
 #include <net/busy_poll.h>
 #include <net/phonet/phonet.h>
 
@@ -3757,6 +3758,7 @@ void sk_common_release(struct sock *sk)
 	sock_orphan(sk);
 
 	xfrm_sk_free_policy(sk);
+	psp_sk_assoc_free(sk);
 
 	sock_put(sk);
 }
net/ipv4/inet_connection_sock.c +2 -0
index 3b38610958ee..10d4be66046a 100644
@@ -21,6 +21,7 @@
 #include <net/xfrm.h>
 #include <net/tcp.h>
 #include <net/sock_reuseport.h>
+#include <net/psp.h>
 #include <net/addrconf.h>
 
 #if IS_ENABLED(CONFIG_IPV6)
@@ -1226,6 +1227,7 @@ void inet_csk_destroy_sock(struct sock *sk)
 	sk_stream_kill_queues(sk);
 
 	xfrm_sk_free_policy(sk);
+	psp_sk_assoc_free(sk);
 
 	this_cpu_dec(*sk->sk_prot->orphan_count);
 
net/ipv4/tcp_minisocks.c +4 -2
index 7d543569a180..660e890f3c74 100644
@@ -23,6 +23,7 @@
 #include <net/xfrm.h>
 #include <net/busy_poll.h>
 #include <net/rstreason.h>
+#include <net/psp.h>
 
 static bool tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
 {
@@ -377,15 +378,16 @@ static void tcp_md5_twsk_free_rcu(struct rcu_head *head)
 
 void tcp_twsk_destructor(struct sock *sk)
 {
+	struct tcp_timewait_sock *twsk = tcp_twsk(sk);
+
 #ifdef CONFIG_TCP_MD5SIG
 	if (static_branch_unlikely(&tcp_md5_needed.key)) {
-		struct tcp_timewait_sock *twsk = tcp_twsk(sk);
-
 		if (twsk->tw_md5_key)
 			call_rcu(&twsk->tw_md5_key->rcu, tcp_md5_twsk_free_rcu);
 	}
 #endif
 	tcp_ao_destroy_sock(sk, true);
+	psp_twsk_assoc_free(twsk);
 }
 EXPORT_SYMBOL_GPL(tcp_twsk_destructor);
 
net/mptcp/protocol.c +2 -0
index bb8f96f2b86f..cd79bcecebc2 100644
@@ -23,6 +23,7 @@
 #include <net/hotdata.h>
 #include <net/xfrm.h>
 #include <asm/ioctls.h>
+#include <net/psp.h>
 #include "protocol.h"
 #include "mib.h"
 
@@ -3010,6 +3011,7 @@ static void __mptcp_destroy_sock(struct sock *sk)
 	WARN_ON_ONCE(msk->rmem_released);
 	sk_stream_kill_queues(sk);
 	xfrm_sk_free_policy(sk);
+	psp_sk_assoc_free(sk);
 
 	sock_put(sk);
 }
Signature · 1 line
2.45.0
author maintainer
#24 lore raw

RFC 4/15 tcp: add datapath logic for PSP with inline key exchange

In reply to Jakub Kicinski

Add validation points and state propagation to support PSP key exchange inline, on TCP connections. The expectation is that application will use some well established mechanism like TLS handshake to establish a secure channel over the connection and if both endpoints are PSP-capable - exchange and install PSP keys. Because the connection can existing in PSP-unsecured and PSP-secured state we need to make sure that there are no race conditions or retransmission leaks.

On Tx - mark packets with the skb->decrypted bit when PSP key is at the enqueue time. Drivers should only encrypt packets with this bit set. This prevents retransmissions getting encrypted when original transmission was not. Similarly to TLS, we'll use sk->sk_validate_xmit_skb to make sure PSP skbs can't "escape" via a PSP-unaware device without being encrypted.

On Rx - validation is done under socket lock. This moves the validation point later than xfrm, for example. Please see the documentation patch for more details on the flow of securing a connection, but for the purpose of this patch what's important is that we want to enforce the invariant that once connection is secured any skb in the receive queue has been encrypted with PSP.

Add trivialities like GRO and coalescing checks.

This change only adds the validation points, for ease of review. Subsequent change will add the ability to install keys, and flesh the enforcement logic out

---
 include/net/dropreason-core.h |  6 +++
 include/net/psp/functions.h   | 74 +++++++++++++++++++++++++++++++++++
 net/core/gro.c                |  2 +
 net/ipv4/tcp.c                |  2 +
 net/ipv4/tcp_ipv4.c           |  9 ++++-
 net/ipv4/tcp_minisocks.c      | 15 +++++++
 net/ipv4/tcp_output.c         | 16 +++++---
 net/ipv6/tcp_ipv6.c           | 10 +++++
 net/psp/Kconfig               |  1 +
 9 files changed, 128 insertions(+), 7 deletions(-)
include/net/dropreason-core.h +6 -0
index 9707ab54fdd5..29f1de9f5148 100644
@@ -92,6 +92,8 @@
 	FN(PACKET_SOCK_ERROR)		\
 	FN(TC_CHAIN_NOTFOUND)		\
 	FN(TC_RECLASSIFY_LOOP)		\
+	FN(PSP_INPUT)			\
+	FN(PSP_OUTPUT)			\
 	FNe(MAX)
 
 /**
@@ -418,6 +420,10 @@ enum skb_drop_reason {
 	 * iterations.
 	 */
 	SKB_DROP_REASON_TC_RECLASSIFY_LOOP,
+	/** @SKB_DROP_REASON_PSP_INPUT: PSP input checks failed */
+	SKB_DROP_REASON_PSP_INPUT,
+	/** @SKB_DROP_REASON_PSP_OUTPUT: PSP output checks failed */
+	SKB_DROP_REASON_PSP_OUTPUT,
 	/**
 	 * @SKB_DROP_REASON_MAX: the maximum of core drop reasons, which
 	 * shouldn't be used as a real 'reason' - only for tracing code gen
include/net/psp/functions.h +74 -0

87 lines not on this page · raw message

net/core/gro.c +2 -0
index e2f84947fb74..56c6c0e43a11 100644
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
+#include <net/psp.h>
 #include <net/gro.h>
 #include <net/dst_metadata.h>
 #include <net/busy_poll.h>
@@ -387,6 +388,7 @@ static void gro_list_prepare(const struct list_head *head,
 			diffs |= skb_get_nfct(p) ^ skb_get_nfct(skb);
 
 			diffs |= gro_list_prepare_tc_ext(skb, p, diffs);
+			diffs |= __psp_skb_coalesce_diff(skb, p, diffs);
 		}
 
 		NAPI_GRO_CB(p)->same_flow = !diffs;
net/ipv4/tcp.c +2 -0
index 06aab937d60a..b81447bcb884 100644
@@ -275,6 +275,7 @@
 #include <net/proto_memory.h>
 #include <net/xfrm.h>
 #include <net/ip.h>
+#include <net/psp.h>
 #include <net/sock.h>
 #include <net/rstreason.h>
 
@@ -674,6 +675,7 @@ void tcp_skb_entail(struct sock *sk, struct sk_buff *skb)
 	tcb->seq     = tcb->end_seq = tp->write_seq;
 	tcb->tcp_flags = TCPHDR_ACK;
 	__skb_header_release(skb);
+	psp_enqueue_set_decrypted(sk, skb);
 	tcp_add_write_queue_tail(sk, skb);
 	sk_wmem_queued_add(sk, skb->truesize);
 	sk_mem_charge(sk, skb->truesize);
net/ipv4/tcp_ipv4.c +8 -1
index 95e3d28b83b8..9539c4a7b55d 100644
@@ -71,6 +71,7 @@
 #include <net/secure_seq.h>
 #include <net/busy_poll.h>
 #include <net/rstreason.h>
+#include <net/psp.h>
 
 #include <linux/inet.h>
 #include <linux/ipv6.h>
@@ -1895,6 +1896,10 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
 	enum skb_drop_reason reason;
 	struct sock *rsk;
 
+	reason = psp_sk_rx_policy_check(sk, skb);
+	if (reason)
+		goto err_discard;
+
 	if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
 		struct dst_entry *dst;
 
@@ -1956,6 +1961,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
 	reason = SKB_DROP_REASON_TCP_CSUM;
 	trace_tcp_bad_csum(skb);
 	TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
+err_discard:
 	TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
 	goto discard;
 }
@@ -2057,7 +2063,8 @@ bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb,
 	    !mptcp_skb_can_collapse(tail, skb) ||
 	    skb_cmp_decrypted(tail, skb) ||
 	    thtail->doff != th->doff ||
-	    memcmp(thtail + 1, th + 1, hdrlen - sizeof(*th)))
+	    memcmp(thtail + 1, th + 1, hdrlen - sizeof(*th)) ||
+	    psp_skb_coalesce_diff(tail, skb))
 		goto no_coalesce;
 
 	__skb_pull(skb, hdrlen);
net/ipv4/tcp_minisocks.c +15 -0
index 660e890f3c74..501b8bc8573e 100644
@@ -101,8 +101,11 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
 {
 	struct tcp_options_received tmp_opt;
 	struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
+	enum skb_drop_reason psp_drop;
 	bool paws_reject = false;
 
+	psp_drop = psp_twsk_rx_policy_check(tcptw, skb);
+
 	tmp_opt.saw_tstamp = 0;
 	if (th->doff > (sizeof(*th) >> 2) && tcptw->tw_ts_recent_stamp) {
 		tcp_parse_options(twsk_net(tw), skb, &tmp_opt, 0, NULL);
@@ -119,6 +122,9 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
 	if (tw->tw_substate == TCP_FIN_WAIT2) {
 		/* Just repeat all the checks of tcp_rcv_state_process() */
 
+		if (psp_drop)
+			goto out_put;
+
 		/* Out of window, send ACK */
 		if (paws_reject ||
 		    !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
@@ -183,6 +189,9 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
 	     (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
 		/* In window segment, it may be only reset or bare ack. */
 
+		if (psp_drop)
+			goto out_put;
+
 		if (th->rst) {
 			/* This is TIME_WAIT assassination, in two flavors.
 			 * Oh well... nobody has a sufficient solution to this
@@ -234,6 +243,9 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
 		return TCP_TW_SYN;
 	}
 
+	if (psp_drop)
+		goto out_put;
+
 	if (paws_reject)
 		__NET_INC_STATS(twsk_net(tw), LINUX_MIB_PAWSESTABREJECTED);
 
@@ -250,6 +262,8 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
 		return tcp_timewait_check_oow_rate_limit(
 			tw, skb, LINUX_MIB_TCPACKSKIPPEDTIMEWAIT);
 	}
+
+out_put:
 	inet_twsk_put(tw);
 	return TCP_TW_SUCCESS;
 }
@@ -332,6 +346,7 @@ void tcp_time_wait(struct sock *sk, int state, int timeo)
 
 		tcp_time_wait_init(sk, tcptw);
 		tcp_ao_time_wait(tcptw, tp);
+		psp_twsk_init(tcptw, sk);
 
 		/* Get the TIME_WAIT timeout firing. */
 		if (timeo < rto)
net/ipv4/tcp_output.c +10 -6
index 95caf8aaa8be..90d0a1d759ae 100644
@@ -40,6 +40,7 @@
 #include <net/tcp.h>
 #include <net/mptcp.h>
 #include <net/proto_memory.h>
+#include <net/psp.h>
 
 #include <linux/compiler.h>
 #include <linux/gfp.h>
@@ -400,13 +401,15 @@ static void tcp_ecn_send(struct sock *sk, struct sk_buff *skb,
 /* Constructs common control bits of non-data skb. If SYN/FIN is present,
  * auto increment end seqno.
  */
-static void tcp_init_nondata_skb(struct sk_buff *skb, u32 seq, u8 flags)
+static void tcp_init_nondata_skb(struct sk_buff *skb, struct sock *sk,
+				 u32 seq, u8 flags)
 {
 	skb->ip_summed = CHECKSUM_PARTIAL;
 
 	TCP_SKB_CB(skb)->tcp_flags = flags;
 
 	tcp_skb_pcount_set(skb, 1);
+	psp_enqueue_set_decrypted(sk, skb);
 
 	TCP_SKB_CB(skb)->seq = seq;
 	if (flags & (TCPHDR_SYN | TCPHDR_FIN))
@@ -1497,6 +1500,7 @@ static void tcp_queue_skb(struct sock *sk, struct sk_buff *skb)
 	/* Advance write_seq and place onto the write_queue. */
 	WRITE_ONCE(tp->write_seq, TCP_SKB_CB(skb)->end_seq);
 	__skb_header_release(skb);
+	psp_enqueue_set_decrypted(sk, skb);
 	tcp_add_write_queue_tail(sk, skb);
 	sk_wmem_queued_add(sk, skb->truesize);
 	sk_mem_charge(sk, skb->truesize);
@@ -3611,7 +3615,7 @@ void tcp_send_fin(struct sock *sk)
 		skb_reserve(skb, MAX_TCP_HEADER);
 		sk_forced_mem_schedule(sk, skb->truesize);
 		/* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */
-		tcp_init_nondata_skb(skb, tp->write_seq,
+		tcp_init_nondata_skb(skb, sk, tp->write_seq,
 				     TCPHDR_ACK | TCPHDR_FIN);
 		tcp_queue_skb(sk, skb);
 	}
@@ -3639,7 +3643,7 @@ void tcp_send_active_reset(struct sock *sk, gfp_t priority,
 
 	/* Reserve space for headers and prepare control bits. */
 	skb_reserve(skb, MAX_TCP_HEADER);
-	tcp_init_nondata_skb(skb, tcp_acceptable_seq(sk),
+	tcp_init_nondata_skb(skb, sk, tcp_acceptable_seq(sk),
 			     TCPHDR_ACK | TCPHDR_RST);
 	tcp_mstamp_refresh(tcp_sk(sk));
 	/* Send it off. */
@@ -4129,7 +4133,7 @@ int tcp_connect(struct sock *sk)
 	if (unlikely(!buff))
 		return -ENOBUFS;
 
-	tcp_init_nondata_skb(buff, tp->write_seq++, TCPHDR_SYN);
+	tcp_init_nondata_skb(buff, sk, tp->write_seq++, TCPHDR_SYN);
 	tcp_mstamp_refresh(tp);
 	tp->retrans_stamp = tcp_time_stamp_ts(tp);
 	tcp_connect_queue_skb(sk, buff);
@@ -4261,7 +4265,7 @@ void __tcp_send_ack(struct sock *sk, u32 rcv_nxt)
 
 	/* Reserve space for headers and prepare control bits. */
 	skb_reserve(buff, MAX_TCP_HEADER);
-	tcp_init_nondata_skb(buff, tcp_acceptable_seq(sk), TCPHDR_ACK);
+	tcp_init_nondata_skb(buff, sk, tcp_acceptable_seq(sk), TCPHDR_ACK);
 
 	/* We do not want pure acks influencing TCP Small Queues or fq/pacing
 	 * too much.
@@ -4307,7 +4311,7 @@ static int tcp_xmit_probe_skb(struct sock *sk, int urgent, int mib)
 	 * end to send an ack.  Don't queue or clone SKB, just
 	 * send it.
 	 */
-	tcp_init_nondata_skb(skb, tp->snd_una - !urgent, TCPHDR_ACK);
+	tcp_init_nondata_skb(skb, sk, tp->snd_una - !urgent, TCPHDR_ACK);
 	NET_INC_STATS(sock_net(sk), mib);
 	return tcp_transmit_skb(sk, skb, 0, (__force gfp_t)0);
 }
net/ipv6/tcp_ipv6.c +10 -0
index 37201c4fb393..6991464511c3 100644
@@ -61,6 +61,7 @@
 #include <net/hotdata.h>
 #include <net/busy_poll.h>
 #include <net/rstreason.h>
+#include <net/psp.h>
 
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
@@ -1607,6 +1608,10 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
 	if (skb->protocol == htons(ETH_P_IP))
 		return tcp_v4_do_rcv(sk, skb);
 
+	reason = psp_sk_rx_policy_check(sk, skb);
+	if (reason)
+		goto err_discard;
+
 	/*
 	 *	socket locking is here for SMP purposes as backlog rcv
 	 *	is currently called with bh processing disabled.
@@ -1688,6 +1693,7 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
 	reason = SKB_DROP_REASON_TCP_CSUM;
 	trace_tcp_bad_csum(skb);
 	TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
+err_discard:
 	TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
 	goto discard;
 
@@ -1992,6 +1998,10 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
 			__this_cpu_write(tcp_tw_isn, isn);
 			goto process;
 		}
+
+		drop_reason = psp_twsk_rx_policy_check(tcp_twsk(sk), skb);
+		if (drop_reason)
+			break;
 	}
 		/* to ACK */
 		fallthrough;
net/psp/Kconfig +1 -0
index 55f9dd87446b..5e3908a40945 100644
@@ -5,6 +5,7 @@
 config INET_PSP
 	bool "PSP Security Protocol support"
 	depends on INET
+	select SKB_DECRYPTED
 	help
 	Enable kernel support for the PSP protocol.
 	For more information see:
Signature · 1 line
2.45.0
author maintainer
#25 lore raw

RFC 5/15 psp: add op for rotation of secret state

In reply to Jakub Kicinski

Rotating the secret state is a key part of the PSP protocol design. Some external daemon needs to do it once a day, or so. Add a netlink op to perform this operation.
Add a notification group for informing users that key has been rotated and they should rekey (next rotation will cut them off).

---
 Documentation/netlink/specs/psp.yaml | 21 +++++++++++++++
 include/net/psp/types.h              |  5 ++++
 include/uapi/linux/psp.h             |  3 +++
 net/psp/psp-nl-gen.c                 | 15 +++++++++++
 net/psp/psp-nl-gen.h                 |  2 ++
 net/psp/psp_main.c                   |  3 ++-
 net/psp/psp_nl.c                     | 40 ++++++++++++++++++++++++++++
 7 files changed, 88 insertions(+), 1 deletion(-)
Documentation/netlink/specs/psp.yaml +21 -0
index dbb5ef148045..6ae598a60b3f 100644
@@ -88,7 +88,28 @@ name: psp
       notify: dev-get
       mcgrp: mgmt
 
+    -
+      name: key-rotate
+      doc: Rotate the secret state.
+      attribute-set: dev
+      do:
+        request:
+          attributes:
+            - id
+        reply:
+          attributes:
+            - id
+        pre: psp-device-get-locked
+        post: psp-device-unlock
+    -
+      name: key-rotate-ntf
+      doc: Notification about secret state getting rotated.
+      notify: key-rotate
+      mcgrp: use
+
 mcast-groups:
   list:
     -
       name: mgmt
+    -
+      name: use
include/net/psp/types.h +5 -0
index a23d9bd9ce96..43950caf70f1 100644
@@ -104,6 +104,11 @@ struct psp_dev_ops {
 	 */
 	int (*set_config)(struct psp_dev *psd, struct psp_dev_config *conf,
 			  struct netlink_ext_ack *extack);
+
+	/**
+	 * @key_rotate: rotate the secret state
+	 */
+	int (*key_rotate)(struct psp_dev *psd, struct netlink_ext_ack *extack);
 };
 
 #endif /* __NET_PSP_H */
include/uapi/linux/psp.h +3 -0
index 4a404f085190..cbfbf3f0f364 100644
@@ -32,11 +32,14 @@ enum {
 	PSP_CMD_DEV_DEL_NTF,
 	PSP_CMD_DEV_SET,
 	PSP_CMD_DEV_CHANGE_NTF,
+	PSP_CMD_KEY_ROTATE,
+	PSP_CMD_KEY_ROTATE_NTF,
 
 	__PSP_CMD_MAX,
 	PSP_CMD_MAX = (__PSP_CMD_MAX - 1)
 };
 
 #define PSP_MCGRP_MGMT	"mgmt"
+#define PSP_MCGRP_USE	"use"
 
 #endif /* _UAPI_LINUX_PSP_H */
net/psp/psp-nl-gen.c +15 -0
index 859712e7c2c1..7f49577ac72f 100644
@@ -21,6 +21,11 @@ static const struct nla_policy psp_dev_set_nl_policy[PSP_A_DEV_PSP_VERSIONS_ENA
 	[PSP_A_DEV_PSP_VERSIONS_ENA] = NLA_POLICY_MASK(NLA_U32, 0xf),
 };
 
+/* PSP_CMD_KEY_ROTATE - do */
+static const struct nla_policy psp_key_rotate_nl_policy[PSP_A_DEV_ID + 1] = {
+	[PSP_A_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+};
+
 /* Ops table for psp */
 static const struct genl_split_ops psp_nl_ops[] = {
 	{
@@ -46,10 +51,20 @@ static const struct genl_split_ops psp_nl_ops[] = {
 		.maxattr	= PSP_A_DEV_PSP_VERSIONS_ENA,
 		.flags		= GENL_CMD_CAP_DO,
 	},
+	{
+		.cmd		= PSP_CMD_KEY_ROTATE,
+		.pre_doit	= psp_device_get_locked,
+		.doit		= psp_nl_key_rotate_doit,
+		.post_doit	= psp_device_unlock,
+		.policy		= psp_key_rotate_nl_policy,
+		.maxattr	= PSP_A_DEV_ID,
+		.flags		= GENL_CMD_CAP_DO,
+	},
 };
 
 static const struct genl_multicast_group psp_nl_mcgrps[] = {
 	[PSP_NLGRP_MGMT] = { "mgmt", },
+	[PSP_NLGRP_USE] = { "use", },
 };
 
 struct genl_family psp_nl_family __ro_after_init = {
net/psp/psp-nl-gen.h +2 -0
index a099686cab5d..00a2d4ec59e4 100644
@@ -20,9 +20,11 @@ psp_device_unlock(const struct genl_split_ops *ops, struct sk_buff *skb,
 int psp_nl_dev_get_doit(struct sk_buff *skb, struct genl_info *info);
 int psp_nl_dev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
 int psp_nl_dev_set_doit(struct sk_buff *skb, struct genl_info *info);
+int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info);
 
 enum {
 	PSP_NLGRP_MGMT,
+	PSP_NLGRP_USE,
 };
 
 extern struct genl_family psp_nl_family;
net/psp/psp_main.c +2 -1
index cf463f757892..536f50d82fb2 100644
@@ -54,7 +54,8 @@ psp_dev_create(struct net_device *netdev,
 	int err;
 
 	if (WARN_ON(!psd_caps->versions ||
-		    !psd_ops->set_config))
+		    !psd_ops->set_config ||
+		    !psd_ops->key_rotate))
 		return ERR_PTR(-EINVAL);
 
 	psd = kzalloc(sizeof(*psd), GFP_KERNEL);
net/psp/psp_nl.c +40 -0
index fda5ce800f82..b7006e50dc87 100644
@@ -221,3 +221,43 @@ int psp_nl_dev_set_doit(struct sk_buff *skb, struct genl_info *info)
 	nlmsg_free(rsp);
 	return err;
 }
+
+int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info)
+{
+	struct psp_dev *psd = info->user_ptr[0];
+	struct genl_info ntf_info;
+	struct sk_buff *ntf, *rsp;
+	int err;
+
+	rsp = psp_nl_reply_new(info);
+	if (!rsp)
+		return -ENOMEM;
+
+	genl_info_init_ntf(&ntf_info, &psp_nl_family, PSP_CMD_KEY_ROTATE);
+	ntf = psp_nl_reply_new(&ntf_info);
+	if (!ntf) {
+		err = -ENOMEM;
+		goto err_free_rsp;
+	}
+
+	if (nla_put_u32(rsp, PSP_A_DEV_ID, psd->id) ||
+	    nla_put_u32(ntf, PSP_A_DEV_ID, psd->id)) {
+		err = -EMSGSIZE;
+		goto err_free_ntf;
+	}
+
+	err = psd->ops->key_rotate(psd, info->extack);
+	if (err)
+		goto err_free_ntf;
+
+	nlmsg_end(ntf, (struct nlmsghdr *)ntf->data);
+	genlmsg_multicast_netns(&psp_nl_family, dev_net(psd->main_netdev), ntf,
+				0, PSP_NLGRP_USE, GFP_KERNEL);
+	return psp_nl_reply_send(rsp, info);
+
+err_free_ntf:
+	nlmsg_free(ntf);
+err_free_rsp:
+	nlmsg_free(rsp);
+	return err;
+}
Signature · 1 line
2.45.0
#26 lore raw

Re: [RFC net-next 05/15] psp: add op for rotation of secret state

Quoted 7 lines · Jakub Kicinski

Rotating the secret state is a key part of the PSP protocol design. Some external daemon needs to do it once a day, or so. Add a netlink op to perform this operation.
Add a notification group for informing users that key has been rotated and they should rekey (next rotation will cut them off).

Does this allow for the possibility of NIC firmware or the driver initiating a rotation? E.g. during key generation if the SPI space has been exhausted a rotation will be required in order to generate a new derived key.

author maintainer
#27 lore raw

Re: [RFC net-next 05/15] psp: add op for rotation of secret state

Quoted 8 lines · On Thu, 16 May 2024 15:59:14 -0400 Lance Richardson wrote:
Quoted 7 lines · Jakub Kicinski

Rotating the secret state is a key part of the PSP protocol design. Some external daemon needs to do it once a day, or so. Add a netlink op to perform this operation.
Add a notification group for informing users that key has been rotated and they should rekey (next rotation will cut them off).

Quoted 4 lines

Does this allow for the possibility of NIC firmware or the driver initiating a rotation? E.g. during key generation if the SPI space has been exhausted a rotation will be required in order to generate a new derived key.

I think it should be fine - I was designing with that use case in mind, but ended up not needing it. We can export a driver facing function which will basically perform the equivalent of psp_nl_key_rotate_doit().

I added the "key generation", because I worried that if unsynchronized agents on multiple hosts can rotate the key - the chances of double-rotation and immediate reuse of a SPI are much higher. Not sure if the extra key generation bits are really necessary.. it seemed like a good idea at the time :)

author maintainer
#28 lore raw

RFC 6/15 net: psp: add socket security association code

In reply to Jakub Kicinski

Add the ability to install PSP Rx and Tx crypto keys on TCP connections. Netlink ops are provided for both operations. Rx side combines allocating a new Rx key and installing it on the socket. Theoretically these are separate actions, but in practice they will always be used one after the other. We can add distinct "alloc" and "install" ops later.

---
 Documentation/netlink/specs/psp.yaml |  71 ++++++++
 include/net/psp/functions.h          |  69 ++++++--
 include/net/psp/types.h              |  55 ++++++
 include/uapi/linux/psp.h             |  21 +++
 net/psp/Kconfig                      |   1 +
 net/psp/Makefile                     |   2 +-
 net/psp/psp-nl-gen.c                 |  39 ++++
 net/psp/psp-nl-gen.h                 |   7 +
 net/psp/psp.h                        |  22 +++
 net/psp/psp_main.c                   |  11 +-
 net/psp/psp_nl.c                     | 244 +++++++++++++++++++++++++
 net/psp/psp_sock.c                   | 255 +++++++++++++++++++++++++++
 12 files changed, 783 insertions(+), 14 deletions(-)
 create mode 100644 net/psp/psp_sock.c
Documentation/netlink/specs/psp.yaml +71 -0
index 6ae598a60b3f..a81f19e7b20d 100644
@@ -38,6 +38,44 @@ name: psp
         type: u32
         enum: version
         enum-as-flags: true
+  -
+    name: assoc
+    attributes:
+      -
+        name: dev-id
+        doc: PSP device ID.
+        type: u32
+        checks:
+          min: 1
+      -
+        name: version
+        doc: |
+          PSP versions (AEAD and protocol version) used by this association,
+          dictates the size of the key.
+        type: u32
+        enum: version
+      -
+        name: rx-key
+        type: nest
+        nested-attributes: keys
+      -
+        name: tx-key
+        type: nest
+        nested-attributes: keys
+      -
+        name: sock-fd
+        doc: Sockets which should be bound to the association immediately.
+        type: u32
+  -
+    name: keys
+    attributes:
+      -
+        name: key
+        type: binary
+      -
+        name: spi
+        doc: Security Parameters Index (SPI) of the association.
+        type: u32
 
 operations:
   list:
@@ -107,6 +145,39 @@ name: psp
       notify: key-rotate
       mcgrp: use
 
+    -
+      name: rx-assoc
+      doc: Allocate a new Rx key + SPI pair, associate it with a socket.
+      attribute-set: assoc
+      do:
+        request:
+          attributes:
+            - dev-id
+            - version
+            - sock-fd
+        reply:
+          attributes:
+            - dev-id
+            - version
+            - rx-key
+        pre: psp-assoc-device-get-locked
+        post: psp-device-unlock
+    -
+      name: tx-assoc
+      doc: Add a PSP Tx association.
+      attribute-set: assoc
+      do:
+        request:
+          attributes:
+            - dev-id
+            - version
+            - tx-key
+            - sock-fd
+        reply:
+          attributes: []
+        pre: psp-assoc-device-get-locked
+        post: psp-device-unlock
+
 mcast-groups:
   list:
     -
include/net/psp/functions.h +57 -12

108 lines not on this page · raw message

include/net/psp/types.h +55 -0

85 lines not on this page · raw message

include/uapi/linux/psp.h +21 -0
index cbfbf3f0f364..607c42c39ba5 100644
@@ -26,6 +26,25 @@ enum {
 	PSP_A_DEV_MAX = (__PSP_A_DEV_MAX - 1)
 };
 
+enum {
+	PSP_A_ASSOC_DEV_ID = 1,
+	PSP_A_ASSOC_VERSION,
+	PSP_A_ASSOC_RX_KEY,
+	PSP_A_ASSOC_TX_KEY,
+	PSP_A_ASSOC_SOCK_FD,
+
+	__PSP_A_ASSOC_MAX,
+	PSP_A_ASSOC_MAX = (__PSP_A_ASSOC_MAX - 1)
+};
+
+enum {
+	PSP_A_KEYS_KEY = 1,
+	PSP_A_KEYS_SPI,
+
+	__PSP_A_KEYS_MAX,
+	PSP_A_KEYS_MAX = (__PSP_A_KEYS_MAX - 1)
+};
+
 enum {
 	PSP_CMD_DEV_GET = 1,
 	PSP_CMD_DEV_ADD_NTF,
@@ -34,6 +53,8 @@ enum {
 	PSP_CMD_DEV_CHANGE_NTF,
 	PSP_CMD_KEY_ROTATE,
 	PSP_CMD_KEY_ROTATE_NTF,
+	PSP_CMD_RX_ASSOC,
+	PSP_CMD_TX_ASSOC,
 
 	__PSP_CMD_MAX,
 	PSP_CMD_MAX = (__PSP_CMD_MAX - 1)
net/psp/Kconfig +1 -0
index 5e3908a40945..a7d24691a7e1 100644
@@ -6,6 +6,7 @@ config INET_PSP
 	bool "PSP Security Protocol support"
 	depends on INET
 	select SKB_DECRYPTED
+	select SOCK_VALIDATE_XMIT
 	help
 	Enable kernel support for the PSP protocol.
 	For more information see:
net/psp/Makefile +1 -1
index 41b51d06e560..eb5ff3c5bfb2 100644
@@ -2,4 +2,4 @@
 
 obj-$(CONFIG_INET_PSP) += psp.o
 
-psp-y := psp_main.o psp_nl.o psp-nl-gen.o
+psp-y := psp_main.o psp_nl.o psp_sock.o psp-nl-gen.o
net/psp/psp-nl-gen.c +39 -0
index 7f49577ac72f..9fdd6f831803 100644
@@ -10,6 +10,12 @@
 
 #include <uapi/linux/psp.h>
 
+/* Common nested types */
+const struct nla_policy psp_keys_nl_policy[PSP_A_KEYS_SPI + 1] = {
+	[PSP_A_KEYS_KEY] = { .type = NLA_BINARY, },
+	[PSP_A_KEYS_SPI] = { .type = NLA_U32, },
+};
+
 /* PSP_CMD_DEV_GET - do */
 static const struct nla_policy psp_dev_get_nl_policy[PSP_A_DEV_ID + 1] = {
 	[PSP_A_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
@@ -26,6 +32,21 @@ static const struct nla_policy psp_key_rotate_nl_policy[PSP_A_DEV_ID + 1] = {
 	[PSP_A_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
 };
 
+/* PSP_CMD_RX_ASSOC - do */
+static const struct nla_policy psp_rx_assoc_nl_policy[PSP_A_ASSOC_SOCK_FD + 1] = {
+	[PSP_A_ASSOC_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PSP_A_ASSOC_VERSION] = NLA_POLICY_MAX(NLA_U32, 3),
+	[PSP_A_ASSOC_SOCK_FD] = { .type = NLA_U32, },
+};
+
+/* PSP_CMD_TX_ASSOC - do */
+static const struct nla_policy psp_tx_assoc_nl_policy[PSP_A_ASSOC_SOCK_FD + 1] = {
+	[PSP_A_ASSOC_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PSP_A_ASSOC_VERSION] = NLA_POLICY_MAX(NLA_U32, 3),
+	[PSP_A_ASSOC_TX_KEY] = NLA_POLICY_NESTED(psp_keys_nl_policy),
+	[PSP_A_ASSOC_SOCK_FD] = { .type = NLA_U32, },
+};
+
 /* Ops table for psp */
 static const struct genl_split_ops psp_nl_ops[] = {
 	{
@@ -60,6 +81,24 @@ static const struct genl_split_ops psp_nl_ops[] = {
 		.maxattr	= PSP_A_DEV_ID,
 		.flags		= GENL_CMD_CAP_DO,
 	},
+	{
+		.cmd		= PSP_CMD_RX_ASSOC,
+		.pre_doit	= psp_assoc_device_get_locked,
+		.doit		= psp_nl_rx_assoc_doit,
+		.post_doit	= psp_device_unlock,
+		.policy		= psp_rx_assoc_nl_policy,
+		.maxattr	= PSP_A_ASSOC_SOCK_FD,
+		.flags		= GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd		= PSP_CMD_TX_ASSOC,
+		.pre_doit	= psp_assoc_device_get_locked,
+		.doit		= psp_nl_tx_assoc_doit,
+		.post_doit	= psp_device_unlock,
+		.policy		= psp_tx_assoc_nl_policy,
+		.maxattr	= PSP_A_ASSOC_SOCK_FD,
+		.flags		= GENL_CMD_CAP_DO,
+	},
 };
 
 static const struct genl_multicast_group psp_nl_mcgrps[] = {
net/psp/psp-nl-gen.h +7 -0
index 00a2d4ec59e4..25268ed11fb5 100644
@@ -11,8 +11,13 @@
 
 #include <uapi/linux/psp.h>
 
+/* Common nested types */
+extern const struct nla_policy psp_keys_nl_policy[PSP_A_KEYS_SPI + 1];
+
 int psp_device_get_locked(const struct genl_split_ops *ops,
 			  struct sk_buff *skb, struct genl_info *info);
+int psp_assoc_device_get_locked(const struct genl_split_ops *ops,
+				struct sk_buff *skb, struct genl_info *info);
 void
 psp_device_unlock(const struct genl_split_ops *ops, struct sk_buff *skb,
 		  struct genl_info *info);
@@ -21,6 +26,8 @@ int psp_nl_dev_get_doit(struct sk_buff *skb, struct genl_info *info);
 int psp_nl_dev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
 int psp_nl_dev_set_doit(struct sk_buff *skb, struct genl_info *info);
 int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info);
+int psp_nl_rx_assoc_doit(struct sk_buff *skb, struct genl_info *info);
+int psp_nl_tx_assoc_doit(struct sk_buff *skb, struct genl_info *info);
 
 enum {
 	PSP_NLGRP_MGMT,
net/psp/psp.h +22 -0
index 94d0cc31a61f..b4092936bc64 100644
@@ -4,6 +4,7 @@
 #define __PSP_PSP_H
 
 #include <linux/list.h>
+#include <linux/lockdep.h>
 #include <linux/mutex.h>
 #include <net/netns/generic.h>
 #include <net/psp.h>
@@ -17,15 +18,36 @@ int psp_dev_check_access(struct psp_dev *psd, struct net *net);
 
 void psp_nl_notify_dev(struct psp_dev *psd, u32 cmd);
 
+struct psp_assoc *psp_assoc_create(struct psp_dev *psd);
+struct psp_dev *psd_get_for_sock(struct sock *sk);
+void psp_dev_tx_key_del(struct psp_dev *psd, struct psp_assoc *pas);
+int psp_sock_assoc_set_rx(struct sock *sk, struct psp_assoc *pas,
+			  struct psp_key_parsed *key,
+			  struct netlink_ext_ack *extack);
+int psp_sock_assoc_set_tx(struct sock *sk, struct psp_dev *psd,
+			  u32 version, struct psp_key_parsed *key,
+			  struct netlink_ext_ack *extack);
+
 static inline void psp_dev_get(struct psp_dev *psd)
 {
 	refcount_inc(&psd->refcnt);
 }
 
+static inline bool psp_dev_tryget(struct psp_dev *psd)
+{
+	return refcount_inc_not_zero(&psd->refcnt);
+}
+
 static inline void psp_dev_put(struct psp_dev *psd)
 {
 	if (refcount_dec_and_test(&psd->refcnt))
 		psp_dev_destroy(psd);
 }
 
+static inline bool psp_dev_is_registered(struct psp_dev *psd)
+{
+	lockdep_assert_held(&psd->lock);
+	return !!psd->ops;
+}
+
 #endif /* __PSP_PSP_H */
net/psp/psp_main.c +10 -1
index 536f50d82fb2..59066c4db048 100644
@@ -55,7 +55,10 @@ psp_dev_create(struct net_device *netdev,
 
 	if (WARN_ON(!psd_caps->versions ||
 		    !psd_ops->set_config ||
-		    !psd_ops->key_rotate))
+		    !psd_ops->key_rotate ||
+		    !psd_ops->rx_spi_alloc ||
+		    !psd_ops->tx_key_add ||
+		    !psd_ops->tx_key_del))
 		return ERR_PTR(-EINVAL);
 
 	psd = kzalloc(sizeof(*psd), GFP_KERNEL);
@@ -68,6 +71,7 @@ psp_dev_create(struct net_device *netdev,
 	psd->drv_priv = priv_ptr;
 
 	mutex_init(&psd->lock);
+	INIT_LIST_HEAD(&psd->active_assocs);
 	refcount_set(&psd->refcnt, 1);
 
 	mutex_lock(&psp_devs_lock);
@@ -103,6 +107,8 @@ void psp_dev_destroy(struct psp_dev *psd)
  */
 void psp_dev_unregister(struct psp_dev *psd)
 {
+	struct psp_assoc *pas, *next;
+
 	mutex_lock(&psp_devs_lock);
 	mutex_lock(&psd->lock);
 
@@ -110,6 +116,9 @@ void psp_dev_unregister(struct psp_dev *psd)
 	xa_erase(&psp_devs, psd->id);
 	mutex_unlock(&psp_devs_lock);
 
+	list_for_each_entry_safe(pas, next, &psd->active_assocs, assocs_list)
+		psp_dev_tx_key_del(psd, pas);
+
 	rcu_assign_pointer(psd->main_netdev->psp_dev, NULL);
 
 	psd->ops = NULL;
net/psp/psp_nl.c +244 -0

256 lines not on this page · raw message

net/psp/psp_sock.c new file +255 -0

255 lines not on this page · raw message

Signature · 1 line
2.45.0
author maintainer
#29 lore raw

RFC 7/15 net: psp: update the TCP MSS to reflect PSP packet overhead

In reply to Jakub Kicinski

PSP eats 32B of header space. Adjust MSS appropriately.

We can either modify tcp_mtu_to_mss() / tcp_mss_to_mtu() or reuse icsk_ext_hdr_len. The former option is more TCP specific and has runtime overhead. The latter is a bit of a hack as PSP is not an ext_hdr. If one squints hard enough, UDP encap is just a more practical version of IPv6 exthdr, so go with the latter. Happy to change.

---
 include/net/psp/functions.h | 12 ++++++++++++
 include/net/psp/types.h     |  3 +++
 net/ipv4/tcp_ipv4.c         |  4 ++--
 net/ipv6/ipv6_sockglue.c    |  6 +++++-
 net/ipv6/tcp_ipv6.c         | 12 ++++++------
 net/psp/psp_sock.c          |  5 +++++
 6 files changed, 33 insertions(+), 9 deletions(-)
include/net/psp/functions.h +12 -0
index ad81322dd4ed..be4f78dec425 100644
@@ -90,6 +90,13 @@ static inline struct psp_assoc *psp_skb_get_assoc_rcu(struct sk_buff *skb)
 		return NULL;
 	return rcu_dereference(skb->sk->psp_assoc);
 }
+
+static inline unsigned int psp_sk_overhead(const struct sock *sk)
+{
+	bool has_psp = rcu_access_pointer(sk->psp_assoc);
+
+	return has_psp ? PSP_HDR_SIZE + PSP_TRL_SIZE : 0;
+}
 #else
 static inline void psp_sk_assoc_free(struct sock *sk) { }
 static inline void
@@ -127,6 +134,11 @@ static inline struct psp_assoc *psp_skb_get_assoc_rcu(struct sk_buff *skb)
 {
 	return NULL;
 }
+
+static inline unsigned int psp_sk_overhead(const struct sock *sk)
+{
+	return 0;
+}
 #endif
 
 static inline unsigned long
include/net/psp/types.h +3 -0
index e39abf10c03c..aad836c1c2ca 100644
@@ -95,6 +95,9 @@ struct psp_dev_caps {
 #define PSP_V1_KEY	32
 #define PSP_MAX_KEY	32
 
+#define PSP_HDR_SIZE	16	/* We don't support optional fields, yet */
+#define PSP_TRL_SIZE	16	/* AES-GCM/GMAC trailer size */
+
 struct psp_skb_ext {
 	__be32 spi;
 	/* generation and version are 8b but we don't want holes */
net/ipv4/tcp_ipv4.c +2 -2
index 9539c4a7b55d..2a602cf51009 100644
@@ -279,9 +279,9 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
 	inet->inet_dport = usin->sin_port;
 	sk_daddr_set(sk, daddr);
 
-	inet_csk(sk)->icsk_ext_hdr_len = 0;
+	inet_csk(sk)->icsk_ext_hdr_len = psp_sk_overhead(sk);
 	if (inet_opt)
-		inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
+		inet_csk(sk)->icsk_ext_hdr_len += inet_opt->opt.optlen;
 
 	tp->rx_opt.mss_clamp = TCP_MSS_DEFAULT;
 
net/ipv6/ipv6_sockglue.c +5 -1
index d4c28ec1bc51..b4505bbb9e2c 100644
@@ -49,6 +49,7 @@
 #include <net/xfrm.h>
 #include <net/compat.h>
 #include <net/seg6.h>
+#include <net/psp.h>
 
 #include <linux/uaccess.h>
 
@@ -107,7 +108,10 @@ struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
 		    !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
 		    inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
 			struct inet_connection_sock *icsk = inet_csk(sk);
-			icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
+
+			icsk->icsk_ext_hdr_len =
+				psp_sk_overhead(sk) +
+				opt->opt_flen + opt->opt_nflen;
 			icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
 		}
 	}
net/ipv6/tcp_ipv6.c +6 -6
index 6991464511c3..c67700fc49a1 100644
@@ -299,10 +299,10 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
 	sk->sk_gso_type = SKB_GSO_TCPV6;
 	ip6_dst_store(sk, dst, NULL, NULL);
 
-	icsk->icsk_ext_hdr_len = 0;
+	icsk->icsk_ext_hdr_len = psp_sk_overhead(sk);
 	if (opt)
-		icsk->icsk_ext_hdr_len = opt->opt_flen +
-					 opt->opt_nflen;
+		icsk->icsk_ext_hdr_len += opt->opt_flen +
+					  opt->opt_nflen;
 
 	tp->rx_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
 
@@ -1500,10 +1500,10 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
 		opt = ipv6_dup_options(newsk, opt);
 		RCU_INIT_POINTER(newnp->opt, opt);
 	}
-	inet_csk(newsk)->icsk_ext_hdr_len = 0;
+	inet_csk(newsk)->icsk_ext_hdr_len = psp_sk_overhead(sk);
 	if (opt)
-		inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
-						    opt->opt_flen;
+		inet_csk(newsk)->icsk_ext_hdr_len += opt->opt_nflen +
+						     opt->opt_flen;
 
 	tcp_ca_openreq_child(newsk, dst);
 
net/psp/psp_sock.c +5 -0
index 42b881e681b9..bcef042cb8a5 100644
@@ -170,6 +170,7 @@ int psp_sock_assoc_set_tx(struct sock *sk, struct psp_dev *psd,
 			  u32 version, struct psp_key_parsed *key,
 			  struct netlink_ext_ack *extack)
 {
+	struct inet_connection_sock *icsk;
 	struct psp_assoc *pas, *dummy;
 	int err;
 
@@ -220,6 +221,10 @@ int psp_sock_assoc_set_tx(struct sock *sk, struct psp_dev *psd,
 
 	WRITE_ONCE(sk->sk_validate_xmit_skb, psp_validate_xmit);
 
+	icsk = inet_csk(sk);
+	icsk->icsk_ext_hdr_len += psp_sk_overhead(sk);
+	icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
+
 exit_free_dummy:
 	kfree(dummy);
 exit_clear_rx:
Signature · 1 line
2.45.0
maintainer
#30 lore raw

Re: [RFC net-next 07/15] net: psp: update the TCP MSS to reflect PSP packet overhead

Quoted 39 lines · Jakub Kicinski

PSP eats 32B of header space. Adjust MSS appropriately.

We can either modify tcp_mtu_to_mss() / tcp_mss_to_mtu() or reuse icsk_ext_hdr_len. The former option is more TCP specific and has runtime overhead. The latter is a bit of a hack as PSP is not an ext_hdr. If one squints hard enough, UDP encap is just a more practical version of IPv6 exthdr, so go with the latter. Happy to change.

include/net/psp/functions.h +6 -0
+static inline unsigned int psp_sk_overhead(const struct sock *sk)
+{
+	bool has_psp = rcu_access_pointer(sk->psp_assoc);
+
+	return has_psp ? PSP_HDR_SIZE + PSP_TRL_SIZE : 0;
+}
net/ipv6/tcp_ipv6.c +3 -3
index 6991464511c3..c67700fc49a1 100644
@@ -299,10 +299,10 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
 	sk->sk_gso_type = SKB_GSO_TCPV6;
 	ip6_dst_store(sk, dst, NULL, NULL);
 
-	icsk->icsk_ext_hdr_len = 0;
+	icsk->icsk_ext_hdr_len = psp_sk_overhead(sk);
 	if (opt)
-		icsk->icsk_ext_hdr_len = opt->opt_flen +
-					 opt->opt_nflen;
+		icsk->icsk_ext_hdr_len += opt->opt_flen +
+					  opt->opt_nflen;
 
 	tp->rx_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
 
@@ -1500,10 +1500,10 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
 		opt = ipv6_dup_options(newsk, opt);
 		RCU_INIT_POINTER(newnp->opt, opt);

net/ipv6/tcp_ipv6.c static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * · line 1508

Quoted 10 lines
net/ipv6/tcp_ipv6.c +3 -3
 	}
-	inet_csk(newsk)->icsk_ext_hdr_len = 0;
+	inet_csk(newsk)->icsk_ext_hdr_len = psp_sk_overhead(sk);
 	if (opt)
-		inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
-						    opt->opt_flen;
+		inet_csk(newsk)->icsk_ext_hdr_len += opt->opt_nflen +
+						     opt->opt_flen;
 
 	tcp_ca_openreq_child(newsk, dst);

The below code adjusts ext_hdr_len and recalculates mss when setting the tx association.

Why already include it at connect and syn_recv, above?

My assumption was that the upgrade to PSP only happens during TCP_ESTABLISHED. But perhaps I'm wrong.

Is it allowed to set rx and tx association even from as early as the initial socket(), when still in TCP_CLOSE, client-side?

Server-side, there is no connection fd to pass to netlink commands before TCP_ESTABLISHED.

Quoted 26 lines
net/psp/psp_sock.c +5 -0
index 42b881e681b9..bcef042cb8a5 100644
@@ -170,6 +170,7 @@ int psp_sock_assoc_set_tx(struct sock *sk, struct psp_dev *psd,
 			  u32 version, struct psp_key_parsed *key,
 			  struct netlink_ext_ack *extack)
 {
+	struct inet_connection_sock *icsk;
 	struct psp_assoc *pas, *dummy;
 	int err;
 
@@ -220,6 +221,10 @@ int psp_sock_assoc_set_tx(struct sock *sk, struct psp_dev *psd,
 
 	WRITE_ONCE(sk->sk_validate_xmit_skb, psp_validate_xmit);
 
+	icsk = inet_csk(sk);
+	icsk->icsk_ext_hdr_len += psp_sk_overhead(sk);
+	icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
+
 exit_free_dummy:
 	kfree(dummy);
 exit_clear_rx:
Signature · 1 line
2.45.0
author maintainer
#31 lore raw

Re: [RFC net-next 07/15] net: psp: update the TCP MSS to reflect PSP packet overhead

Quoted 15 lines · On Sun, 12 May 2024 21:47:16 -0400 Willem de Bruijn wrote:

net/ipv6/tcp_ipv6.c static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * · line 1508

Quoted 9 lines
net/ipv6/tcp_ipv6.c +3 -3
-	inet_csk(newsk)->icsk_ext_hdr_len = 0;
+	inet_csk(newsk)->icsk_ext_hdr_len = psp_sk_overhead(sk);
 	if (opt)
-		inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
-						    opt->opt_flen;
+		inet_csk(newsk)->icsk_ext_hdr_len += opt->opt_nflen +
+						     opt->opt_flen;
 
 	tcp_ca_openreq_child(newsk, dst);  

The below code adjusts ext_hdr_len and recalculates mss when setting the tx association.

Why already include it at connect and syn_recv, above?

Quoted 8 lines

My assumption was that the upgrade to PSP only happens during TCP_ESTABLISHED. But perhaps I'm wrong.

Is it allowed to set rx and tx association even from as early as the initial socket(), when still in TCP_CLOSE, client-side?

Server-side, there is no connection fd to pass to netlink commands before TCP_ESTABLISHED.

Mostly for symmetry, really. IDK what's worse, the dead code or that someone may be surprised it's not there.. Should I delete it?

maintainer
#32 lore raw

Re: [RFC net-next 07/15] net: psp: update the TCP MSS to reflect PSP packet overhead

Quoted 19 lines · Jakub Kicinski
Quoted 18 lines · On Sun, 12 May 2024 21:47:16 -0400 Willem de Bruijn wrote:

net/ipv6/tcp_ipv6.c static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * · line 1508

Quoted 9 lines
net/ipv6/tcp_ipv6.c +3 -3
-	inet_csk(newsk)->icsk_ext_hdr_len = 0;
+	inet_csk(newsk)->icsk_ext_hdr_len = psp_sk_overhead(sk);
 	if (opt)
-		inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
-						    opt->opt_flen;
+		inet_csk(newsk)->icsk_ext_hdr_len += opt->opt_nflen +
+						     opt->opt_flen;
 
 	tcp_ca_openreq_child(newsk, dst);  

The below code adjusts ext_hdr_len and recalculates mss when setting the tx association.

Why already include it at connect and syn_recv, above?

My assumption was that the upgrade to PSP only happens during TCP_ESTABLISHED. But perhaps I'm wrong.

Quoted 8 lines
Quoted 5 lines

Is it allowed to set rx and tx association even from as early as the initial socket(), when still in TCP_CLOSE, client-side?

Server-side, there is no connection fd to pass to netlink commands before TCP_ESTABLISHED.

Mostly for symmetry, really. IDK what's worse, the dead code or that someone may be surprised it's not there.. Should I delete it?

Symmetry with what?

This dead code had me scratching my head what it was doing, so my vote to drop it. If you want something, maybe a code comment instead?

author maintainer
#33 lore raw

RFC 8/15 psp: track generations of secret state

In reply to Jakub Kicinski

There is a (somewhat theoretical in absence of multi-host support) possibility that another entity will rotate the key and we won't know. This may lead to accepting packets with matching SPI but which used different crypto keys than we expected. Maintain and compare "key generation" per PSP spec.

Since we're tracking "key generations" more explicitly now, maintain different lists for associations from different generations. This way we can catch stale associations (the user space should listen to rotation notifications and change the keys).

Drivers can "opt out" of generation tracking by setting the generation value to 0.

---
 include/net/psp/types.h | 10 ++++++++++
 net/psp/psp.h           |  1 +
 net/psp/psp_main.c      |  6 +++++-
 net/psp/psp_nl.c        | 10 ++++++++++
 net/psp/psp_sock.c      | 16 ++++++++++++++++
 5 files changed, 42 insertions(+), 1 deletion(-)
include/net/psp/types.h +10 -0
index aad836c1c2ca..a9e406c979a8 100644
@@ -50,8 +50,12 @@ struct psp_dev_config {
  * @lock:	instance lock, protects all fields
  * @refcnt:	reference count for the instance
  * @id:		instance id
+ * @generation:	current generation of the secret state
  * @config:	current device configuration
  * @active_assocs:	list of registered associations
+ * @prev_assocs:	associations which use old (but still usable)
+ *			secret state
+ * @stale_assocs:	associations which use a rotated out key
  *
  * @rcu:	RCU head for freeing the structure
  */
@@ -67,13 +71,19 @@ struct psp_dev {
 
 	u32 id;
 
+	u8 generation;
+
 	struct psp_dev_config config;
 
 	struct list_head active_assocs;
+	struct list_head prev_assocs;
+	struct list_head stale_assocs;
 
 	struct rcu_head rcu;
 };
 
+#define PSP_GEN_VALID_MASK	0x7f
+
 /**
  * struct psp_dev_caps - PSP device capabilities
  */
net/psp/psp.h +1 -0
index b4092936bc64..a511ec85e1c7 100644
@@ -27,6 +27,7 @@ int psp_sock_assoc_set_rx(struct sock *sk, struct psp_assoc *pas,
 int psp_sock_assoc_set_tx(struct sock *sk, struct psp_dev *psd,
 			  u32 version, struct psp_key_parsed *key,
 			  struct netlink_ext_ack *extack);
+void psp_assocs_key_rotated(struct psp_dev *psd);
 
 static inline void psp_dev_get(struct psp_dev *psd)
 {
net/psp/psp_main.c +5 -1
index 59066c4db048..f9c5ee57df78 100644
@@ -72,6 +72,8 @@ psp_dev_create(struct net_device *netdev,
 
 	mutex_init(&psd->lock);
 	INIT_LIST_HEAD(&psd->active_assocs);
+	INIT_LIST_HEAD(&psd->prev_assocs);
+	INIT_LIST_HEAD(&psd->stale_assocs);
 	refcount_set(&psd->refcnt, 1);
 
 	mutex_lock(&psp_devs_lock);
@@ -116,7 +118,9 @@ void psp_dev_unregister(struct psp_dev *psd)
 	xa_erase(&psp_devs, psd->id);
 	mutex_unlock(&psp_devs_lock);
 
-	list_for_each_entry_safe(pas, next, &psd->active_assocs, assocs_list)
+	list_splice_init(&psd->active_assocs, &psd->prev_assocs);
+	list_splice_init(&psd->prev_assocs, &psd->stale_assocs);
+	list_for_each_entry_safe(pas, next, &psd->stale_assocs, assocs_list)
 		psp_dev_tx_key_del(psd, pas);
 
 	rcu_assign_pointer(psd->main_netdev->psp_dev, NULL);
net/psp/psp_nl.c +10 -0
index 58508e642185..7b8a1d390cde 100644
@@ -230,6 +230,7 @@ int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info)
 	struct psp_dev *psd = info->user_ptr[0];
 	struct genl_info ntf_info;
 	struct sk_buff *ntf, *rsp;
+	u8 prev_gen;
 	int err;
 
 	rsp = psp_nl_reply_new(info);
@@ -249,10 +250,19 @@ int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info)
 		goto err_free_ntf;
 	}
 
+	/* suggest the next gen number, driver can override */
+	prev_gen = psd->generation;
+	psd->generation = (prev_gen + 1) & PSP_GEN_VALID_MASK;
+
 	err = psd->ops->key_rotate(psd, info->extack);
 	if (err)
 		goto err_free_ntf;
 
+	WARN_ON_ONCE((psd->generation && psd->generation == prev_gen) ||
+		     psd->generation & ~PSP_GEN_VALID_MASK);
+
+	psp_assocs_key_rotated(psd);
+
 	nlmsg_end(ntf, (struct nlmsghdr *)ntf->data);
 	genlmsg_multicast_netns(&psp_nl_family, dev_net(psd->main_netdev), ntf,
 				0, PSP_NLGRP_USE, GFP_KERNEL);
net/psp/psp_sock.c +16 -0
index bcef042cb8a5..7a791703850c 100644
@@ -58,6 +58,7 @@ struct psp_assoc *psp_assoc_create(struct psp_dev *psd)
 		return NULL;
 
 	pas->psd = psd;
+	pas->generation = psd->generation;
 	psp_dev_get(psd);
 	refcount_set(&pas->refcnt, 1);
 
@@ -235,6 +236,21 @@ int psp_sock_assoc_set_tx(struct sock *sk, struct psp_dev *psd,
 	return err;
 }
 
+void psp_assocs_key_rotated(struct psp_dev *psd)
+{
+	struct psp_assoc *pas, *next;
+
+	/* Mark the stale associations as invalid, they will no longer
+	 * be able to Rx any traffic.
+	 */
+	list_for_each_entry_safe(pas, next, &psd->prev_assocs, assocs_list)
+		pas->generation |= ~PSP_GEN_VALID_MASK;
+	list_splice_init(&psd->prev_assocs, &psd->stale_assocs);
+	list_splice_init(&psd->active_assocs, &psd->prev_assocs);
+
+	/* TODO: we should inform the sockets that got shut down */
+}
+
 void psp_twsk_init(struct tcp_timewait_sock *tw, struct sock *sk)
 {
 	struct psp_assoc *pas = psp_sk_assoc(sk);
Signature · 1 line
2.45.0
author maintainer
#34 lore raw

RFC 9/15 net/mlx5e: Support PSP offload functionality

In reply to Jakub Kicinski

From: Raed Salem <[email protected]>

Add PSP offload related IFC structs, layouts, and enumerations. Implement .set_config and .rx_spi_alloc PSP device operations. Driver does not need to make use of the .set_config operation. Stub .assoc_add and .assoc_del PSP operations.

Introduce the MLX5_EN_PSP configuration option for enabling PSP offload support on mlx5 devices.

---
 .../net/ethernet/mellanox/mlx5/core/Kconfig   |  11 ++
 .../net/ethernet/mellanox/mlx5/core/Makefile  |   4 +-
 drivers/net/ethernet/mellanox/mlx5/core/en.h  |   3 +
 .../ethernet/mellanox/mlx5/core/en/params.c   |   4 +-
 .../mellanox/mlx5/core/en_accel/nisp.c        | 149 ++++++++++++++++++
 .../mellanox/mlx5/core/en_accel/nisp.h        |  53 +++++++
 .../mlx5/core/en_accel/nisp_offload.c         |  52 ++++++
 .../net/ethernet/mellanox/mlx5/core/en_main.c |   9 ++
 drivers/net/ethernet/mellanox/mlx5/core/fw.c  |   6 +
 .../net/ethernet/mellanox/mlx5/core/main.c    |   5 +
 .../net/ethernet/mellanox/mlx5/core/nisp.c    |  24 +++
 .../net/ethernet/mellanox/mlx5/core/nisp.h    |  15 ++
 include/linux/mlx5/device.h                   |   4 +
 include/linux/mlx5/driver.h                   |   2 +
 include/linux/mlx5/mlx5_ifc.h                 |  98 +++++++++++-
 15 files changed, 431 insertions(+), 8 deletions(-)
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp.h
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_offload.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/nisp.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/nisp.h
drivers/net/ethernet/mellanox/mlx5/core/Kconfig +11 -0
index 685335832a93..0dc9665a9557 100644
@@ -197,3 +197,14 @@ config MLX5_DPLL
 	help
 	  DPLL support in Mellanox Technologies ConnectX NICs.
 
+config MLX5_EN_PSP
+	bool "Mellanox Technologies support for PSP cryptography-offload acceleration"
+	depends on INET_PSP
+	depends on MLX5_CORE_EN
+	default y
+	help
+	  mlx5 device offload support for Google PSP Security Protocol offload.
+	  Adds support for PSP encryption offload and for SPI and key generation
+	  interfaces to PSP Stack which supports PSP crypto offload.
+
+	  If unsure, say Y.
drivers/net/ethernet/mellanox/mlx5/core/Makefile +3 -1
index 76dc5a9b9648..c17a5e343603 100644
@@ -17,7 +17,7 @@ mlx5_core-y :=	main.o cmd.o debugfs.o fw.o eq.o uar.o pagealloc.o \
 		fs_counters.o fs_ft_pool.o rl.o lag/debugfs.o lag/lag.o dev.o events.o wq.o lib/gid.o \
 		lib/devcom.o lib/pci_vsc.o lib/dm.o lib/fs_ttc.o diag/fs_tracepoint.o \
 		diag/fw_tracer.o diag/crdump.o devlink.o diag/rsc_dump.o diag/reporter_vnic.o \
-		fw_reset.o qos.o lib/tout.o lib/aso.o
+		fw_reset.o qos.o lib/tout.o lib/aso.o nisp.o
 
 #
 # Netdev basic
@@ -109,6 +109,8 @@ mlx5_core-$(CONFIG_MLX5_EN_TLS) += en_accel/ktls_stats.o \
 				   en_accel/fs_tcp.o en_accel/ktls.o en_accel/ktls_txrx.o \
 				   en_accel/ktls_tx.o en_accel/ktls_rx.o
 
+mlx5_core-$(CONFIG_MLX5_EN_PSP) += en_accel/nisp.o en_accel/nisp_offload.o
+
 mlx5_core-$(CONFIG_MLX5_SW_STEERING) += steering/dr_domain.o steering/dr_table.o \
 					steering/dr_matcher.o steering/dr_rule.o \
 					steering/dr_icm_pool.o steering/dr_buddy.o \
drivers/net/ethernet/mellanox/mlx5/core/en.h +3 -0
index f8bd9dbf59cd..b7ceb8011a92 100644
@@ -941,6 +941,9 @@ struct mlx5e_priv {
 #ifdef CONFIG_MLX5_EN_IPSEC
 	struct mlx5e_ipsec        *ipsec;
 #endif
+#ifdef CONFIG_MLX5_EN_PSP
+	struct mlx5e_nisp        *nisp;
+#endif
 #ifdef CONFIG_MLX5_EN_TLS
 	struct mlx5e_tls          *tls;
 #endif
drivers/net/ethernet/mellanox/mlx5/core/en/params.c +3 -1
index ec819dfc98be..3141abb33ff7 100644
@@ -6,6 +6,7 @@
 #include "en/port.h"
 #include "en_accel/en_accel.h"
 #include "en_accel/ipsec.h"
+#include "en_accel/nisp.h"
 #include <linux/dim.h>
 #include <net/page_pool/types.h>
 #include <net/xdp_sock_drv.h>
@@ -1005,7 +1006,8 @@ void mlx5e_build_sq_param(struct mlx5_core_dev *mdev,
 	bool allow_swp;
 
 	allow_swp = mlx5_geneve_tx_allowed(mdev) ||
-		    (mlx5_ipsec_device_caps(mdev) & MLX5_IPSEC_CAP_CRYPTO);
+		    (mlx5_ipsec_device_caps(mdev) & MLX5_IPSEC_CAP_CRYPTO) ||
+		    mlx5_is_nisp_device(mdev);
 	mlx5e_build_sq_param_common(mdev, param);
 	MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
 	MLX5_SET(sqc, sqc, allow_swp, allow_swp);
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp.c new file +149 -0

149 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp.h new file +53 -0

53 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_offload.c new file +52 -0

52 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx5/core/en_main.c +9 -0
index ffe8919494d5..4948e19c3f3f 100644
@@ -49,6 +49,7 @@
 #include "en_tc.h"
 #include "en_rep.h"
 #include "en_accel/ipsec.h"
+#include "en_accel/nisp.h"
 #include "en_accel/macsec.h"
 #include "en_accel/en_accel.h"
 #include "en_accel/ktls.h"
@@ -5506,6 +5507,7 @@ static int mlx5e_nic_init(struct mlx5_core_dev *mdev,
 	if (take_rtnl)
 		rtnl_lock();
 
+	mlx5e_nisp_register(priv);
 	/* update XDP supported features */
 	mlx5e_set_xdp_feature(netdev);
 
@@ -5518,6 +5520,7 @@ static int mlx5e_nic_init(struct mlx5_core_dev *mdev,
 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
 {
 	mlx5e_health_destroy_reporters(priv);
+	mlx5e_nisp_unregister(priv);
 	mlx5e_ktls_cleanup(priv);
 	mlx5e_fs_cleanup(priv->fs);
 	debugfs_remove_recursive(priv->dfs_root);
@@ -5645,6 +5648,10 @@ static void mlx5e_nic_enable(struct mlx5e_priv *priv)
 	if (err)
 		mlx5_core_err(mdev, "MACsec initialization failed, %d\n", err);
 
+	err = mlx5e_nisp_init(priv);
+	if (err)
+		mlx5_core_err(mdev, "PSP initialization failed, %d\n", err);
+
 	/* Marking the link as currently not needed by the Driver */
 	if (!netif_running(netdev))
 		mlx5e_modify_admin_state(mdev, MLX5_PORT_DOWN);
@@ -5702,6 +5709,7 @@ static void mlx5e_nic_disable(struct mlx5e_priv *priv)
 	mlx5e_disable_async_events(priv);
 	mlx5_lag_remove_netdev(mdev, priv->netdev);
 	mlx5_vxlan_reset_to_default(mdev->vxlan);
+	mlx5e_nisp_cleanup(priv);
 	mlx5e_macsec_cleanup(priv);
 	mlx5e_ipsec_cleanup(priv);
 }
@@ -6337,6 +6345,7 @@ static void _mlx5e_remove(struct auxiliary_device *adev)
 
 	mlx5_core_uplink_netdev_set(mdev, NULL);
 	mlx5e_dcbnl_delete_app(priv);
+	mlx5e_nisp_unregister(priv);
 	unregister_netdev(priv->netdev);
 	_mlx5e_suspend(adev);
 	priv->profile->cleanup(priv);
drivers/net/ethernet/mellanox/mlx5/core/fw.c +6 -0
index 2d95a9b7b44e..eee318d64d98 100644
@@ -280,6 +280,12 @@ int mlx5_query_hca_caps(struct mlx5_core_dev *dev)
 			return err;
 	}
 
+	if (MLX5_CAP_GEN(dev, psp)) {
+		err = mlx5_core_get_caps(dev, MLX5_CAP_NISP);
+		if (err)
+			return err;
+	}
+
 	return 0;
 }
 
drivers/net/ethernet/mellanox/mlx5/core/main.c +5 -0
index 331ce47f51a1..8625d593ed89 100644
@@ -74,6 +74,7 @@
 #include "mlx5_irq.h"
 #include "hwmon.h"
 #include "lag/lag.h"
+#include "nisp.h"
 
 MODULE_AUTHOR("Eli Cohen <[email protected]>");
 MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) core driver");
@@ -1013,6 +1014,7 @@ static int mlx5_init_once(struct mlx5_core_dev *dev)
 
 	dev->vxlan = mlx5_vxlan_create(dev);
 	dev->geneve = mlx5_geneve_create(dev);
+	dev->nisp = mlx5_nisp_create(dev);
 
 	err = mlx5_init_rl_table(dev);
 	if (err) {
@@ -1095,6 +1097,7 @@ static int mlx5_init_once(struct mlx5_core_dev *dev)
 err_rl_cleanup:
 	mlx5_cleanup_rl_table(dev);
 err_tables_cleanup:
+	mlx5_nisp_destroy(dev->nisp);
 	mlx5_geneve_destroy(dev->geneve);
 	mlx5_vxlan_destroy(dev->vxlan);
 	mlx5_cleanup_clock(dev);
@@ -1129,6 +1132,7 @@ static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
 	mlx5_sriov_cleanup(dev);
 	mlx5_mpfs_cleanup(dev);
 	mlx5_cleanup_rl_table(dev);
+	mlx5_nisp_destroy(dev->nisp);
 	mlx5_geneve_destroy(dev->geneve);
 	mlx5_vxlan_destroy(dev->vxlan);
 	mlx5_cleanup_clock(dev);
@@ -1763,6 +1767,7 @@ static const int types[] = {
 	MLX5_CAP_VDPA_EMULATION,
 	MLX5_CAP_IPSEC,
 	MLX5_CAP_PORT_SELECTION,
+	MLX5_CAP_NISP,
 	MLX5_CAP_MACSEC,
 	MLX5_CAP_ADV_VIRTUALIZATION,
 	MLX5_CAP_CRYPTO,
drivers/net/ethernet/mellanox/mlx5/core/nisp.c new file +24 -0
new file mode 100644
index 000000000000..f82734df8bf0
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#include "nisp.h"
+
+struct mlx5_nisp *mlx5_nisp_create(struct mlx5_core_dev *mdev)
+{
+	struct mlx5_nisp *nisp = kzalloc(sizeof(*nisp), GFP_KERNEL);
+
+	if (!nisp)
+		return ERR_PTR(-ENOMEM);
+
+	nisp->mdev = mdev;
+
+	return nisp;
+}
+
+void mlx5_nisp_destroy(struct mlx5_nisp *nisp)
+{
+	if (IS_ERR_OR_NULL(nisp))
+		return;
+
+	kfree(nisp);
+}
drivers/net/ethernet/mellanox/mlx5/core/nisp.h new file +15 -0
new file mode 100644
index 000000000000..c15e50e7ada7
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
+/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#ifndef __MLX5_NISP_H__
+#define __MLX5_NISP_H__
+#include <linux/mlx5/driver.h>
+
+struct mlx5_nisp {
+	struct mlx5_core_dev *mdev;
+};
+
+struct mlx5_nisp *mlx5_nisp_create(struct mlx5_core_dev *mdev);
+void mlx5_nisp_destroy(struct mlx5_nisp *nisp);
+
+#endif /* __MLX5_NISP_H__ */
include/linux/mlx5/device.h +4 -0
index d7bb31d9a446..6c00c78bad53 100644
@@ -1223,6 +1223,7 @@ enum mlx5_cap_type {
 	MLX5_CAP_DEV_EVENT = 0x14,
 	MLX5_CAP_IPSEC,
 	MLX5_CAP_CRYPTO = 0x1a,
+	MLX5_CAP_NISP = 0x1e,
 	MLX5_CAP_MACSEC = 0x1f,
 	MLX5_CAP_GENERAL_2 = 0x20,
 	MLX5_CAP_PORT_SELECTION = 0x25,
@@ -1435,6 +1436,9 @@ enum mlx5_qcam_feature_groups {
 #define MLX5_CAP_MACSEC(mdev, cap)\
 	MLX5_GET(macsec_cap, (mdev)->caps.hca[MLX5_CAP_MACSEC]->cur, cap)
 
+#define MLX5_CAP_NISP(mdev, cap)\
+	MLX5_GET(nisp_cap, (mdev)->caps.hca[MLX5_CAP_NISP]->cur, cap)
+
 enum {
 	MLX5_CMD_STAT_OK			= 0x0,
 	MLX5_CMD_STAT_INT_ERR			= 0x1,
include/linux/mlx5/driver.h +2 -0
index bf9324a31ae9..8d6060866163 100644
@@ -515,6 +515,7 @@ struct mlx5_sf_dev_table;
 struct mlx5_sf_hw_table;
 struct mlx5_sf_table;
 struct mlx5_crypto_dek_priv;
+struct mlx5_nisp;
 
 struct mlx5_rate_limit {
 	u32			rate;
@@ -824,6 +825,7 @@ struct mlx5_core_dev {
 #endif
 	u64 num_ipsec_offloads;
 	struct mlx5_sd          *sd;
+	struct mlx5_nisp        *nisp;
 };
 
 struct mlx5_db {
include/linux/mlx5/mlx5_ifc.h +92 -6

178 lines not on this page · raw message

Signature · 1 line
2.45.0
author maintainer
#35 lore raw

RFC 10/15 net/mlx5e: Implement PSP operations .assoc_add and .assoc_del

In reply to Jakub Kicinski

From: Raed Salem <[email protected]>

Implement .assoc_add and .assoc_del PSP operations used in the tx control path. Allocate the relevant hardware resources when a new key is registered using .assoc_add. Destroy the key when .assoc_del is called. Use a atomic counter to keep track of the current number of keys being used by the device.

---
 .../net/ethernet/mellanox/mlx5/core/Makefile  |   2 +-
 .../mellanox/mlx5/core/en_accel/en_accel.h    |   8 +
 .../mellanox/mlx5/core/en_accel/nisp.c        |  57 ++++-
 .../mellanox/mlx5/core/en_accel/nisp.h        |   2 +
 .../mellanox/mlx5/core/en_accel/nisp_fs.c     | 234 ++++++++++++++++++
 .../mellanox/mlx5/core/en_accel/nisp_fs.h     |  23 ++
 .../net/ethernet/mellanox/mlx5/core/en_main.c |  10 +-
 .../ethernet/mellanox/mlx5/core/lib/crypto.h  |   1 +
 8 files changed, 327 insertions(+), 10 deletions(-)
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_fs.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_fs.h
drivers/net/ethernet/mellanox/mlx5/core/Makefile +1 -1
index c17a5e343603..5ce78b84c763 100644
@@ -109,7 +109,7 @@ mlx5_core-$(CONFIG_MLX5_EN_TLS) += en_accel/ktls_stats.o \
 				   en_accel/fs_tcp.o en_accel/ktls.o en_accel/ktls_txrx.o \
 				   en_accel/ktls_tx.o en_accel/ktls_rx.o
 
-mlx5_core-$(CONFIG_MLX5_EN_PSP) += en_accel/nisp.o en_accel/nisp_offload.o
+mlx5_core-$(CONFIG_MLX5_EN_PSP) += en_accel/nisp.o en_accel/nisp_offload.o en_accel/nisp_fs.o
 
 mlx5_core-$(CONFIG_MLX5_SW_STEERING) += steering/dr_domain.o steering/dr_table.o \
 					steering/dr_matcher.o steering/dr_rule.o \
drivers/net/ethernet/mellanox/mlx5/core/en_accel/en_accel.h +8 -0
index caa34b9c161e..c15e48b0724c 100644
@@ -42,6 +42,7 @@
 #include <en_accel/macsec.h>
 #include "en.h"
 #include "en/txrx.h"
+#include "en_accel/nisp_fs.h"
 
 #if IS_ENABLED(CONFIG_GENEVE)
 #include <net/geneve.h>
@@ -212,11 +213,18 @@ static inline void mlx5e_accel_cleanup_rx(struct mlx5e_priv *priv)
 
 static inline int mlx5e_accel_init_tx(struct mlx5e_priv *priv)
 {
+	int err;
+
+	err = mlx5_accel_nisp_fs_init_tx_tables(priv);
+	if (err)
+		return err;
+
 	return mlx5e_ktls_init_tx(priv);
 }
 
 static inline void mlx5e_accel_cleanup_tx(struct mlx5e_priv *priv)
 {
 	mlx5e_ktls_cleanup_tx(priv);
+	mlx5_accel_nisp_fs_cleanup_tx_tables(priv);
 }
 #endif /* __MLX5E_EN_ACCEL_H__ */
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp.c +53 -4

99 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp.h +2 -0
index 93eaea8b6f77..14e5813367a7 100644
@@ -10,6 +10,8 @@
 struct mlx5e_nisp {
 	struct psp_dev *psp;
 	struct psp_dev_caps caps;
+	struct mlx5e_nisp_fs *fs;
+	atomic_t tx_key_cnt;
 };
 
 struct nisp_key_spi {
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_fs.c new file +234 -0

234 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_fs.h new file +23 -0
new file mode 100644
index 000000000000..11cdc447a401
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
+/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#ifndef __MLX5_NISP_FS_H__
+#define __MLX5_NISP_FS_H__
+
+#ifdef CONFIG_MLX5_EN_PSP
+
+struct mlx5e_nisp_fs;
+
+struct mlx5e_nisp_fs *mlx5e_accel_nisp_fs_init(struct mlx5e_priv *priv);
+void mlx5e_accel_nisp_fs_cleanup(struct mlx5e_nisp_fs *fs);
+int mlx5_accel_nisp_fs_init_tx_tables(struct mlx5e_priv *priv);
+void mlx5_accel_nisp_fs_cleanup_tx_tables(struct mlx5e_priv *priv);
+#else
+static inline int mlx5_accel_nisp_fs_init_tx_tables(struct mlx5e_priv *priv)
+{
+	return 0;
+}
+
+static inline void mlx5_accel_nisp_fs_cleanup_tx_tables(struct mlx5e_priv *priv) { }
+#endif /* CONFIG_MLX5_EN_PSP */
+#endif /* __MLX5_NISP_FS_H__ */
drivers/net/ethernet/mellanox/mlx5/core/en_main.c +5 -5
index 4948e19c3f3f..38e0c4786b1c 100644
@@ -5495,6 +5495,10 @@ static int mlx5e_nic_init(struct mlx5_core_dev *mdev,
 	}
 	priv->fs = fs;
 
+	err = mlx5e_nisp_init(priv);
+	if (err)
+		mlx5_core_err(mdev, "PSP initialization failed, %d\n", err);
+
 	err = mlx5e_ktls_init(priv);
 	if (err)
 		mlx5_core_err(mdev, "TLS initialization failed, %d\n", err);
@@ -5522,6 +5526,7 @@ static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
 	mlx5e_health_destroy_reporters(priv);
 	mlx5e_nisp_unregister(priv);
 	mlx5e_ktls_cleanup(priv);
+	mlx5e_nisp_cleanup(priv);
 	mlx5e_fs_cleanup(priv->fs);
 	debugfs_remove_recursive(priv->dfs_root);
 	priv->fs = NULL;
@@ -5648,10 +5653,6 @@ static void mlx5e_nic_enable(struct mlx5e_priv *priv)
 	if (err)
 		mlx5_core_err(mdev, "MACsec initialization failed, %d\n", err);
 
-	err = mlx5e_nisp_init(priv);
-	if (err)
-		mlx5_core_err(mdev, "PSP initialization failed, %d\n", err);
-
 	/* Marking the link as currently not needed by the Driver */
 	if (!netif_running(netdev))
 		mlx5e_modify_admin_state(mdev, MLX5_PORT_DOWN);
@@ -5709,7 +5710,6 @@ static void mlx5e_nic_disable(struct mlx5e_priv *priv)
 	mlx5e_disable_async_events(priv);
 	mlx5_lag_remove_netdev(mdev, priv->netdev);
 	mlx5_vxlan_reset_to_default(mdev->vxlan);
-	mlx5e_nisp_cleanup(priv);
 	mlx5e_macsec_cleanup(priv);
 	mlx5e_ipsec_cleanup(priv);
 }
drivers/net/ethernet/mellanox/mlx5/core/lib/crypto.h +1 -0
index c819c047bb9c..f257dfcf45d6 100644
@@ -8,6 +8,7 @@ enum {
 	MLX5_ACCEL_OBJ_TLS_KEY = MLX5_GENERAL_OBJECT_TYPE_ENCRYPTION_KEY_PURPOSE_TLS,
 	MLX5_ACCEL_OBJ_IPSEC_KEY = MLX5_GENERAL_OBJECT_TYPE_ENCRYPTION_KEY_PURPOSE_IPSEC,
 	MLX5_ACCEL_OBJ_MACSEC_KEY = MLX5_GENERAL_OBJECT_TYPE_ENCRYPTION_KEY_PURPOSE_MACSEC,
+	MLX5_ACCEL_OBJ_NISP_KEY = MLX5_GENERAL_OBJECT_TYPE_ENCRYPTION_KEY_PURPOSE_NISP,
 	MLX5_ACCEL_OBJ_TYPE_KEY_NUM,
 };
 
Signature · 1 line
2.45.0
author maintainer
#36 lore raw

RFC 11/15 net/mlx5e: Implement PSP Tx data path

In reply to Jakub Kicinski

From: Raed Salem <[email protected]>

Setup PSP offload on Tx data path based on whether skb indicates that it is intended for PSP or not. Support driver side encapsulation of the UDP headers, PSP headers, and PSP trailer for the PSP traffic that will be encrypted by the NIC.

---
 .../net/ethernet/mellanox/mlx5/core/Makefile  |   3 +-
 drivers/net/ethernet/mellanox/mlx5/core/en.h  |   4 +-
 .../mellanox/mlx5/core/en_accel/en_accel.h    |  28 +++
 .../mellanox/mlx5/core/en_accel/nisp_rxtx.c   | 225 ++++++++++++++++++
 .../mellanox/mlx5/core/en_accel/nisp_rxtx.h   |  96 ++++++++
 .../net/ethernet/mellanox/mlx5/core/en_tx.c   |  10 +-
 .../mellanox/mlx5/core/lib/psp_defs.h         |  28 +++
 7 files changed, 390 insertions(+), 4 deletions(-)
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.c
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.h
 create mode 100644 drivers/net/ethernet/mellanox/mlx5/core/lib/psp_defs.h
drivers/net/ethernet/mellanox/mlx5/core/Makefile +2 -1
index 5ce78b84c763..858ab2e7cb1f 100644
@@ -109,7 +109,8 @@ mlx5_core-$(CONFIG_MLX5_EN_TLS) += en_accel/ktls_stats.o \
 				   en_accel/fs_tcp.o en_accel/ktls.o en_accel/ktls_txrx.o \
 				   en_accel/ktls_tx.o en_accel/ktls_rx.o
 
-mlx5_core-$(CONFIG_MLX5_EN_PSP) += en_accel/nisp.o en_accel/nisp_offload.o en_accel/nisp_fs.o
+mlx5_core-$(CONFIG_MLX5_EN_PSP) += en_accel/nisp.o en_accel/nisp_offload.o en_accel/nisp_fs.o \
+				   en_accel/nisp_rxtx.o
 
 mlx5_core-$(CONFIG_MLX5_SW_STEERING) += steering/dr_domain.o steering/dr_table.o \
 					steering/dr_matcher.o steering/dr_rule.o \
drivers/net/ethernet/mellanox/mlx5/core/en.h +3 -1
index b7ceb8011a92..92e2554d6271 100644
@@ -47,6 +47,7 @@
 #include <linux/rhashtable.h>
 #include <net/udp_tunnel.h>
 #include <net/switchdev.h>
+#include <net/psp/types.h>
 #include <net/xdp.h>
 #include <linux/dim.h>
 #include <linux/bits.h>
@@ -61,6 +62,7 @@
 #include "en/rx_res.h"
 #include "en/selq.h"
 #include "lib/sd.h"
+#include "lib/psp_defs.h"
 
 extern const struct net_device_ops mlx5e_netdev_ops;
 struct page_pool;
@@ -68,7 +70,7 @@ struct page_pool;
 #define MLX5E_METADATA_ETHER_TYPE (0x8CE4)
 #define MLX5E_METADATA_ETHER_LEN 8
 
-#define MLX5E_ETH_HARD_MTU (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN)
+#define MLX5E_ETH_HARD_MTU (ETH_HLEN + PSP_ENCAP_HLEN + PSP_TRL_SIZE + VLAN_HLEN + ETH_FCS_LEN)
 
 #define MLX5E_HW2SW_MTU(params, hwmtu) ((hwmtu) - ((params)->hard_mtu))
 #define MLX5E_SW2HW_MTU(params, swmtu) ((swmtu) + ((params)->hard_mtu))
drivers/net/ethernet/mellanox/mlx5/core/en_accel/en_accel.h +28 -0
index c15e48b0724c..cea997847fa4 100644
@@ -43,6 +43,7 @@
 #include "en.h"
 #include "en/txrx.h"
 #include "en_accel/nisp_fs.h"
+#include "en_accel/nisp_rxtx.h"
 
 #if IS_ENABLED(CONFIG_GENEVE)
 #include <net/geneve.h>
@@ -114,6 +115,9 @@ struct mlx5e_accel_tx_state {
 #ifdef CONFIG_MLX5_EN_IPSEC
 	struct mlx5e_accel_tx_ipsec_state ipsec;
 #endif
+#ifdef CONFIG_MLX5_EN_PSP
+	struct mlx5e_accel_tx_nisp_state nisp_st;
+#endif
 };
 
 static inline bool mlx5e_accel_tx_begin(struct net_device *dev,
@@ -132,6 +136,13 @@ static inline bool mlx5e_accel_tx_begin(struct net_device *dev,
 			return false;
 #endif
 
+#ifdef CONFIG_MLX5_EN_PSP
+	if (mlx5e_psp_is_offload(skb, dev)) {
+		if (unlikely(!mlx5e_nisp_handle_tx_skb(dev, skb, &state->nisp_st)))
+			return false;
+	}
+#endif
+
 #ifdef CONFIG_MLX5_EN_IPSEC
 	if (test_bit(MLX5E_SQ_STATE_IPSEC, &sq->state) && xfrm_offload(skb)) {
 		if (unlikely(!mlx5e_ipsec_handle_tx_skb(dev, skb, &state->ipsec)))
@@ -152,8 +163,14 @@ static inline bool mlx5e_accel_tx_begin(struct net_device *dev,
 }
 
 static inline unsigned int mlx5e_accel_tx_ids_len(struct mlx5e_txqsq *sq,
+						  struct sk_buff *skb,
 						  struct mlx5e_accel_tx_state *state)
 {
+#ifdef CONFIG_MLX5_EN_PSP
+	if (mlx5e_psp_is_offload_state(&state->nisp_st))
+		return mlx5e_nisp_tx_ids_len(&state->nisp_st);
+#endif
+
 #ifdef CONFIG_MLX5_EN_IPSEC
 	if (test_bit(MLX5E_SQ_STATE_IPSEC, &sq->state))
 		return mlx5e_ipsec_tx_ids_len(&state->ipsec);
@@ -167,8 +184,14 @@ static inline unsigned int mlx5e_accel_tx_ids_len(struct mlx5e_txqsq *sq,
 
 static inline void mlx5e_accel_tx_eseg(struct mlx5e_priv *priv,
 				       struct sk_buff *skb,
+				       struct mlx5e_accel_tx_state *accel,
 				       struct mlx5_wqe_eth_seg *eseg, u16 ihs)
 {
+#ifdef CONFIG_MLX5_EN_PSP
+	if (mlx5e_psp_is_offload_state(&accel->nisp_st))
+		mlx5e_nisp_tx_build_eseg(priv, skb, &accel->nisp_st, eseg);
+#endif
+
 #ifdef CONFIG_MLX5_EN_IPSEC
 	if (xfrm_offload(skb))
 		mlx5e_ipsec_tx_build_eseg(priv, skb, eseg);
@@ -194,6 +217,11 @@ static inline void mlx5e_accel_tx_finish(struct mlx5e_txqsq *sq,
 	mlx5e_ktls_handle_tx_wqe(&wqe->ctrl, &state->tls);
 #endif
 
+#ifdef CONFIG_MLX5_EN_PSP
+	if (mlx5e_psp_is_offload_state(&state->nisp_st))
+		mlx5e_nisp_handle_tx_wqe(wqe, &state->nisp_st, inlseg);
+#endif
+
 #ifdef CONFIG_MLX5_EN_IPSEC
 	if (test_bit(MLX5E_SQ_STATE_IPSEC, &sq->state) &&
 	    state->ipsec.xo && state->ipsec.tailen)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.c new file +225 -0

225 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.h new file +96 -0

96 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx5/core/en_tx.c +8 -2
index 099bf1078889..cc4d236a976f 100644
@@ -39,6 +39,7 @@
 #include "ipoib/ipoib.h"
 #include "en_accel/en_accel.h"
 #include "en_accel/ipsec_rxtx.h"
+#include "en_accel/nisp_rxtx.h"
 #include "en_accel/macsec.h"
 #include "en/ptp.h"
 #include <net/ipv6.h>
@@ -120,6 +121,11 @@ mlx5e_txwqe_build_eseg_csum(struct mlx5e_txqsq *sq, struct sk_buff *skb,
 			    struct mlx5e_accel_tx_state *accel,
 			    struct mlx5_wqe_eth_seg *eseg)
 {
+#ifdef CONFIG_MLX5_EN_PSP
+	if (unlikely(mlx5e_nisp_txwqe_build_eseg_csum(sq, skb, &accel->nisp_st, eseg)))
+		return;
+#endif
+
 	if (unlikely(mlx5e_ipsec_txwqe_build_eseg_csum(sq, skb, eseg)))
 		return;
 
@@ -294,7 +300,7 @@ static void mlx5e_sq_xmit_prepare(struct mlx5e_txqsq *sq, struct sk_buff *skb,
 		stats->packets++;
 	}
 
-	attr->insz = mlx5e_accel_tx_ids_len(sq, accel);
+	attr->insz = mlx5e_accel_tx_ids_len(sq, skb, accel);
 	stats->bytes += attr->num_bytes;
 }
 
@@ -663,7 +669,7 @@ static void mlx5e_txwqe_build_eseg(struct mlx5e_priv *priv, struct mlx5e_txqsq *
 				   struct sk_buff *skb, struct mlx5e_accel_tx_state *accel,
 				   struct mlx5_wqe_eth_seg *eseg, u16 ihs)
 {
-	mlx5e_accel_tx_eseg(priv, skb, eseg, ihs);
+	mlx5e_accel_tx_eseg(priv, skb, accel, eseg, ihs);
 	mlx5e_txwqe_build_eseg_csum(sq, skb, accel, eseg);
 	if (unlikely(sq->ptpsq))
 		mlx5e_cqe_ts_id_eseg(sq->ptpsq, skb, eseg);
drivers/net/ethernet/mellanox/mlx5/core/lib/psp_defs.h new file +28 -0
new file mode 100644
index 000000000000..7dd2aa90ed62
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
+/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
+
+#ifndef _LIB_PSP_DEFS_H
+#define _LIB_PSP_DEFS_H
+
+/*  PSP Security Payload (PSP) Header
+ *
+ *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |  Next Header  |  Hdr Ext Len  |  Crypt Offset | R |Version|V|1|
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                Security Parameters Index (SPI)                |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                                                               |
+ * +                  Initialization Vector (IV)                   +
+ * |                                                               |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                Virtualization Key (VK) [Optional]             |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                  Pad to 8*N bytes [if needed]                 |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ */
+
+/* total length of headers for PSP encapsulation (UDP + PSP) */
+#define PSP_ENCAP_HLEN (sizeof(struct udphdr) + sizeof(struct psphdr))
+
+#endif  /* _LIB_PSP_DEFS_H */
Signature · 1 line
2.45.0
author maintainer
#37 lore raw

RFC 12/15 net/mlx5e: Add PSP steering in local NIC RX

In reply to Jakub Kicinski

From: Raed Salem <[email protected]>

Introduce decrypt FT, the RX error FT, and the default rules.

The PSP (PSP) RX decrypt flow table is pointed by the TTC (Traffic Type Classifier) UDP steering rules.
The decrypt flow table has two flow groups. The first flow group keeps the decrypt steering rule programmed always when PSP packet is recognized using the dedicated udp destenation port number 1000, if packet is decrypted then a PSP marker is set in metadata_regB[30]. The second flow group has a default rule to forward all non-offloaded PSP packet to the TTC UDP default RSS TIR.

The RX error flow table is the destination of the decrypt steering rules in the PSP RX decrypt flow table. It has two fixed rule one with single copy action that copies nisp_syndrome to metadata_regB[23:29]. The PSP marker and syndrome is used to filter out non-nisp packet and to return the PSP crypto offload status in Rx flow. The marker is used to identify such packet in driver so the driver could set SKB PSP metadata. The destination of RX error flow table is the TTC UDP default RSS TIR. The second rule will drop packets that failed to be decrypted (like in case illegal SPI or expired SPI is used).

---
 .../net/ethernet/mellanox/mlx5/core/en/fs.h   |   2 +-
 .../mellanox/mlx5/core/en_accel/nisp_fs.c     | 481 +++++++++++++++++-
 2 files changed, 476 insertions(+), 7 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en/fs.h +1 -1
index 4d6225e0eec7..23af74e4f8c5 100644
@@ -83,7 +83,7 @@ enum {
 #ifdef CONFIG_MLX5_EN_ARFS
 	MLX5E_ARFS_FT_LEVEL = MLX5E_INNER_TTC_FT_LEVEL + 1,
 #endif
-#ifdef CONFIG_MLX5_EN_IPSEC
+#if defined(CONFIG_MLX5_EN_IPSEC) || defined(CONFIG_MLX5_EN_PSP)
 	MLX5E_ACCEL_FS_POL_FT_LEVEL = MLX5E_INNER_TTC_FT_LEVEL + 1,
 	MLX5E_ACCEL_FS_ESP_FT_LEVEL,
 	MLX5E_ACCEL_FS_ESP_FT_ERR_LEVEL,
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_fs.c +475 -6

521 lines not on this page · raw message

Signature · 1 line
2.45.0
maintainer
#38 lore raw

Re: [RFC net-next 12/15] net/mlx5e: Add PSP steering in local NIC RX

Quoted 9 lines · Jakub Kicinski

From: Raed Salem <[email protected]>

Introduce decrypt FT, the RX error FT, and the default rules.

The PSP (PSP) RX decrypt flow table is pointed by the TTC (Traffic Type Classifier) UDP steering rules.
The decrypt flow table has two flow groups. The first flow group keeps the decrypt steering rule programmed always when PSP packet is recognized using the dedicated udp destenation port number 1000, if

typo: destination

Quoted 17 lines

packet is decrypted then a PSP marker is set in metadata_regB[30]. The second flow group has a default rule to forward all non-offloaded PSP packet to the TTC UDP default RSS TIR.

The RX error flow table is the destination of the decrypt steering rules in the PSP RX decrypt flow table. It has two fixed rule one with single copy action that copies nisp_syndrome to metadata_regB[23:29]. The PSP marker and syndrome is used to filter out non-nisp packet and to return the PSP crypto offload status in Rx flow. The marker is used to identify such packet in driver so the driver could set SKB PSP metadata. The destination of RX error flow table is the TTC UDP default RSS TIR. The second rule will drop packets that failed to be decrypted (like in case illegal SPI or expired SPI is used).

author maintainer
#39 lore raw

RFC 13/15 net/mlx5e: Configure PSP Rx flow steering rules

In reply to Jakub Kicinski

From: Raed Salem <[email protected]>

Set the Rx PSP flow steering rule where PSP packet is identified and decrypted using the dedicated UDP destination port number 1000. If packet is decrypted then a PSP marker and syndrome are added to metadata so SW can use it later on in Rx data path.

The rule is set as part of init_rx netdev profile implementation.

---
 .../mellanox/mlx5/core/en_accel/en_accel.h    | 14 +++++-
 .../mellanox/mlx5/core/en_accel/nisp_fs.c     | 46 ++++++++++++++++---
 .../mellanox/mlx5/core/en_accel/nisp_fs.h     |  7 +++
 3 files changed, 60 insertions(+), 7 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/en_accel.h +13 -1
index cea997847fa4..38d2c73a0f79 100644
@@ -231,12 +231,24 @@ static inline void mlx5e_accel_tx_finish(struct mlx5e_txqsq *sq,
 
 static inline int mlx5e_accel_init_rx(struct mlx5e_priv *priv)
 {
-	return mlx5e_ktls_init_rx(priv);
+	int err;
+
+	err = mlx5_accel_nisp_fs_init_rx_tables(priv);
+	if (err)
+		goto out;
+
+	err = mlx5e_ktls_init_rx(priv);
+	if (err)
+		mlx5_accel_nisp_fs_cleanup_rx_tables(priv);
+
+out:
+	return err;
 }
 
 static inline void mlx5e_accel_cleanup_rx(struct mlx5e_priv *priv)
 {
 	mlx5e_ktls_cleanup_rx(priv);
+	mlx5_accel_nisp_fs_cleanup_rx_tables(priv);
 }
 
 static inline int mlx5e_accel_init_tx(struct mlx5e_priv *priv)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_fs.c +40 -6

62 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_fs.h +7 -0
index 11cdc447a401..8c44dd51317c 100644
@@ -12,6 +12,8 @@ struct mlx5e_nisp_fs *mlx5e_accel_nisp_fs_init(struct mlx5e_priv *priv);
 void mlx5e_accel_nisp_fs_cleanup(struct mlx5e_nisp_fs *fs);
 int mlx5_accel_nisp_fs_init_tx_tables(struct mlx5e_priv *priv);
 void mlx5_accel_nisp_fs_cleanup_tx_tables(struct mlx5e_priv *priv);
+int mlx5_accel_nisp_fs_init_rx_tables(struct mlx5e_priv *priv);
+void mlx5_accel_nisp_fs_cleanup_rx_tables(struct mlx5e_priv *priv);
 #else
 static inline int mlx5_accel_nisp_fs_init_tx_tables(struct mlx5e_priv *priv)
 {
@@ -19,5 +21,10 @@ static inline int mlx5_accel_nisp_fs_init_tx_tables(struct mlx5e_priv *priv)
 }
 
 static inline void mlx5_accel_nisp_fs_cleanup_tx_tables(struct mlx5e_priv *priv) { }
+static inline int mlx5_accel_nisp_fs_init_rx_tables(struct mlx5e_priv *priv)
+{
+	return 0;
+}
+static inline void mlx5_accel_nisp_fs_cleanup_rx_tables(struct mlx5e_priv *priv) { }
 #endif /* CONFIG_MLX5_EN_PSP */
 #endif /* __MLX5_NISP_FS_H__ */
Signature · 1 line
2.45.0
author maintainer
#40 lore raw

RFC 14/15 net/mlx5e: Add Rx data path offload

In reply to Jakub Kicinski

From: Raed Salem <[email protected]>

On receive flow inspect received packets for PSP offload indication using the cqe, for PSP offloaded packets set SKB PSP metadata i.e spi, header length and key generation number to stack for further processing.

---
 .../mellanox/mlx5/core/en_accel/ipsec_rxtx.h  |  2 +-
 .../mellanox/mlx5/core/en_accel/nisp_rxtx.c   | 79 +++++++++++++++++++
 .../mellanox/mlx5/core/en_accel/nisp_rxtx.h   | 28 +++++++
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   | 10 +++
 4 files changed, 118 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_rxtx.h +1 -1
index 82064614846f..9f025c80a6ef 100644
@@ -40,7 +40,7 @@
 #include "en/txrx.h"
 
 /* Bit31: IPsec marker, Bit30: reserved, Bit29-24: IPsec syndrome, Bit23-0: IPsec obj id */
-#define MLX5_IPSEC_METADATA_MARKER(metadata)  (((metadata) >> 31) & 0x1)
+#define MLX5_IPSEC_METADATA_MARKER(metadata)  ((((metadata) >> 30) & 0x3) == 0x2)
 #define MLX5_IPSEC_METADATA_SYNDROM(metadata) (((metadata) >> 24) & GENMASK(5, 0))
 #define MLX5_IPSEC_METADATA_HANDLE(metadata)  ((metadata) & GENMASK(23, 0))
 
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.c +79 -0

91 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.h +28 -0
index 1350a73c2019..834481232b21 100644
@@ -10,6 +10,11 @@
 #include "en.h"
 #include "en/txrx.h"
 
+/* Bit30: NISP marker, Bit29-23: NISP syndrome, Bit22-0: NISP obj id */
+#define MLX5_NISP_METADATA_MARKER(metadata)  ((((metadata) >> 30) & 0x3) == 0x3)
+#define MLX5_NISP_METADATA_SYNDROM(metadata) (((metadata) >> 23) & GENMASK(6, 0))
+#define MLX5_NISP_METADATA_HANDLE(metadata)  ((metadata) & GENMASK(22, 0))
+
 struct mlx5e_accel_tx_nisp_state {
 	u32 tailen;
 	u32 keyid;
@@ -75,6 +80,16 @@ static inline unsigned int mlx5e_nisp_tx_ids_len(struct mlx5e_accel_tx_nisp_stat
 {
 	return nisp_st->tailen;
 }
+
+static inline bool mlx5e_nisp_is_rx_flow(struct mlx5_cqe64 *cqe)
+{
+	return MLX5_NISP_METADATA_MARKER(be32_to_cpu(cqe->ft_metadata));
+}
+
+void mlx5e_nisp_offload_handle_rx_skb(struct net_device *netdev, struct sk_buff *skb,
+				      struct mlx5_cqe64 *cqe);
+
+void mlx5e_nisp_csum_complete(struct net_device *netdev, struct sk_buff *skb);
 #else
 static inline bool mlx5e_psp_is_offload_state(struct mlx5e_accel_tx_nisp_state *nisp_state)
 {
@@ -92,5 +107,18 @@ static inline bool mlx5e_nisp_txwqe_build_eseg_csum(struct mlx5e_txqsq *sq, stru
 {
 	return false;
 }
+
+static inline bool mlx5e_nisp_is_rx_flow(struct mlx5_cqe64 *cqe)
+{
+	return false;
+}
+
+static inline void mlx5e_nisp_offload_handle_rx_skb(struct net_device *netdev,
+						    struct sk_buff *skb,
+						    struct mlx5_cqe64 *cqe)
+{
+}
+
+static inline void mlx5e_nisp_csum_complete(struct net_device *netdev, struct sk_buff *skb) { }
 #endif
 #endif
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +10 -0
index d601b5faaed5..41a4f8832f2f 100644
@@ -51,6 +51,7 @@
 #include "ipoib/ipoib.h"
 #include "en_accel/ipsec.h"
 #include "en_accel/macsec.h"
+#include "en_accel/nisp_rxtx.h"
 #include "en_accel/ipsec_rxtx.h"
 #include "en_accel/ktls_txrx.h"
 #include "en/xdp.h"
@@ -1517,6 +1518,12 @@ static inline void mlx5e_handle_csum(struct net_device *netdev,
 		skb->ip_summed = CHECKSUM_COMPLETE;
 		skb->csum = csum_unfold((__force __sum16)cqe->check_sum);
 
+		if (unlikely(mlx5e_nisp_is_rx_flow(cqe))) {
+			/* TBD: PSP csum complete corrections for now chose csum_unnecessary path */
+			mlx5e_nisp_csum_complete(netdev, skb);
+			goto csum_unnecessary;
+		}
+
 		if (test_bit(MLX5E_RQ_STATE_CSUM_FULL, &rq->state))
 			return; /* CQE csum covers all received bytes */
 
@@ -1559,6 +1566,9 @@ static inline void mlx5e_build_rx_skb(struct mlx5_cqe64 *cqe,
 	if (unlikely(get_cqe_tls_offload(cqe)))
 		mlx5e_ktls_handle_rx_skb(rq, skb, cqe, &cqe_bcnt);
 
+	if (unlikely(mlx5e_nisp_is_rx_flow(cqe)))
+		mlx5e_nisp_offload_handle_rx_skb(netdev, skb, cqe);
+
 	if (unlikely(mlx5_ipsec_is_rx_flow(cqe)))
 		mlx5e_ipsec_offload_handle_rx_skb(netdev, skb,
 						  be32_to_cpu(cqe->ft_metadata));
Signature · 1 line
2.45.0
maintainer
#41 lore raw

Re: [RFC net-next 14/15] net/mlx5e: Add Rx data path offload

Quoted 103 lines · Jakub Kicinski

From: Raed Salem <[email protected]>

On receive flow inspect received packets for PSP offload indication using the cqe, for PSP offloaded packets set SKB PSP metadata i.e spi, header length and key generation number to stack for further processing.

---
 .../mellanox/mlx5/core/en_accel/ipsec_rxtx.h  |  2 +-
 .../mellanox/mlx5/core/en_accel/nisp_rxtx.c   | 79 +++++++++++++++++++
 .../mellanox/mlx5/core/en_accel/nisp_rxtx.h   | 28 +++++++
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   | 10 +++
 4 files changed, 118 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_rxtx.h +1 -1
index 82064614846f..9f025c80a6ef 100644
@@ -40,7 +40,7 @@
 #include "en/txrx.h"
 
 /* Bit31: IPsec marker, Bit30: reserved, Bit29-24: IPsec syndrome, Bit23-0: IPsec obj id */
-#define MLX5_IPSEC_METADATA_MARKER(metadata)  (((metadata) >> 31) & 0x1)
+#define MLX5_IPSEC_METADATA_MARKER(metadata)  ((((metadata) >> 30) & 0x3) == 0x2)
 #define MLX5_IPSEC_METADATA_SYNDROM(metadata) (((metadata) >> 24) & GENMASK(5, 0))
 #define MLX5_IPSEC_METADATA_HANDLE(metadata)  ((metadata) & GENMASK(23, 0))
 
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.c +59 -0
index c719b2916677..17f42b8d9fd8 100644
@@ -15,6 +15,12 @@
 #include "en_accel/nisp.h"
 #include "lib/psp_defs.h"
 
+enum {
+	MLX5E_NISP_OFFLOAD_RX_SYNDROME_DECRYPTED,
+	MLX5E_NISP_OFFLOAD_RX_SYNDROME_AUTH_FAILED,
+	MLX5E_NISP_OFFLOAD_RX_SYNDROME_BAD_TRAILER,
+};
+
 static void mlx5e_nisp_set_swp(struct sk_buff *skb,
 			       struct mlx5e_accel_tx_nisp_state *nisp_st,
 			       struct mlx5_wqe_eth_seg *eseg)
@@ -114,6 +120,79 @@ static bool mlx5e_nisp_set_state(struct mlx5e_priv *priv,
 	return ret;
 }
 
+void mlx5e_nisp_csum_complete(struct net_device *netdev, struct sk_buff *skb)
+{
+	pskb_trim(skb, skb->len - PSP_TRL_SIZE);
+}
+
+/* Receive handler for PSP packets.
+ *
+ * Presently it accepts only already-authenticated packets and does not
+ * support optional fields, such as virtualization cookies.
+ */
+static int psp_rcv(struct sk_buff *skb)
+{
+	const struct psphdr *psph;
+	int depth = 0, end_depth;
+	struct psp_skb_ext *pse;
+	struct ipv6hdr *ipv6h;
+	struct ethhdr *eth;
+	__be16 proto;
+	u32 spi;
+
+	eth = (struct ethhdr *)(skb->data);
+	proto = __vlan_get_protocol(skb, eth->h_proto, &depth);
+	if (proto != htons(ETH_P_IPV6))
+		return -EINVAL;
+
+	ipv6h = (struct ipv6hdr *)(skb->data + depth);
+	depth += sizeof(*ipv6h);
+	end_depth = depth + sizeof(struct udphdr) + sizeof(struct psphdr);
+
+	if (unlikely(end_depth > skb_headlen(skb)))
+		return -EINVAL;
+
+	pse = skb_ext_add(skb, SKB_EXT_PSP);
+	if (!pse)
+		return -EINVAL;
+
+	psph = (const struct psphdr *)(skb->data + depth + sizeof(struct udphdr));
+	pse->spi = psph->spi;
+	spi = ntohl(psph->spi);
+	pse->generation = 0;
+	pse->version = FIELD_GET(PSPHDR_VERFL_VERSION, psph->verfl);
+
+	ipv6h->nexthdr = psph->nexthdr;
+	ipv6h->payload_len =
+		htons(ntohs(ipv6h->payload_len) - PSP_ENCAP_HLEN - PSP_TRL_SIZE);
+
+	memmove(skb->data + PSP_ENCAP_HLEN, skb->data, depth);
+	skb_pull(skb, PSP_ENCAP_HLEN);
+
+	return 0;
+}
+
+void mlx5e_nisp_offload_handle_rx_skb(struct net_device *netdev, struct sk_buff *skb,

drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.c static bool mlx5e_nisp_set_state(struct mlx5e_priv *priv, · line 185

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.c +10 -0
+				      struct mlx5_cqe64 *cqe)
+{
+	u32 nisp_meta_data = be32_to_cpu(cqe->ft_metadata);
+
+	/* TBD: report errors as SW counters to ethtool, any further handling ? */
+	switch (MLX5_NISP_METADATA_SYNDROM(nisp_meta_data)) {
+	case MLX5E_NISP_OFFLOAD_RX_SYNDROME_DECRYPTED:
+		if (psp_rcv(skb))
+			netdev_warn_once(netdev, "PSP handling failed");
+		skb->decrypted = 1;

Do not set skb->decrypted if psp_rcv failed? But drop the packet and account the drop, likely.

Quoted 9 lines
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.c +9 -0
+		break;
+	case MLX5E_NISP_OFFLOAD_RX_SYNDROME_AUTH_FAILED:
+		break;
+	case MLX5E_NISP_OFFLOAD_RX_SYNDROME_BAD_TRAILER:
+		break;
+	default:
+		break;
+	}
+}
author maintainer
#42 lore raw

Re: [RFC net-next 14/15] net/mlx5e: Add Rx data path offload

Quoted 9 lines · On Sun, 12 May 2024 21:54:38 -0400 Willem de Bruijn wrote:

Review

Quoted 6 lines
(quoted diff) +6 -0
+	/* TBD: report errors as SW counters to ethtool, any further handling ? */
+	switch (MLX5_NISP_METADATA_SYNDROM(nisp_meta_data)) {
+	case MLX5E_NISP_OFFLOAD_RX_SYNDROME_DECRYPTED:
+		if (psp_rcv(skb))
+			netdev_warn_once(netdev, "PSP handling failed");
+		skb->decrypted = 1;  

Do not set skb->decrypted if psp_rcv failed? But drop the packet and account the drop, likely.

nVidia folks does this seem reasonable?

drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.c +13 -11
index 7ae3e8246d8f..8cf6a8daf721 100644
@@ -172,22 +172,24 @@ static int psp_rcv(struct sk_buff *skb)
 	return 0;
 }
 
-void mlx5e_nisp_offload_handle_rx_skb(struct net_device *netdev, struct sk_buff *skb,
+bool mlx5e_nisp_offload_handle_rx_skb(struct net_device *netdev, struct sk_buff *skb,
 				      struct mlx5_cqe64 *cqe)
 {
 	u32 nisp_meta_data = be32_to_cpu(cqe->ft_metadata);
 
 	/* TBD: report errors as SW counters to ethtool, any further handling ? */
-	switch (MLX5_NISP_METADATA_SYNDROM(nisp_meta_data)) {
-	case MLX5E_NISP_OFFLOAD_RX_SYNDROME_DECRYPTED:
-		if (psp_rcv(skb))
-			netdev_warn_once(netdev, "PSP handling failed");
-		skb->decrypted = 1;
-		break;
-	default:
-		WARN_ON_ONCE(true);
-		break;
-	}
+	if (MLX5_NISP_METADATA_SYNDROM(nisp_meta_data) != MLX5E_NISP_OFFLOAD_RX_SYNDROME_DECRYPTED)
+		goto drop;
+
+	if (psp_rcv(skb))
+		goto drop;
+
+	skb->decrypted = 1;
+	return false;
+
+drop:
+	kfree_skb(skb);
+	return true;
 }
 
 void mlx5e_nisp_tx_build_eseg(struct mlx5e_priv *priv, struct sk_buff *skb,
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp_rxtx.h +3 -2
index 834481232b21..1e13b09b3522 100644
@@ -86,7 +86,7 @@ static inline bool mlx5e_nisp_is_rx_flow(struct mlx5_cqe64 *cqe)
 	return MLX5_NISP_METADATA_MARKER(be32_to_cpu(cqe->ft_metadata));
 }
 
-void mlx5e_nisp_offload_handle_rx_skb(struct net_device *netdev, struct sk_buff *skb,
+bool mlx5e_nisp_offload_handle_rx_skb(struct net_device *netdev, struct sk_buff *skb,
 				      struct mlx5_cqe64 *cqe);
 
 void mlx5e_nisp_csum_complete(struct net_device *netdev, struct sk_buff *skb);
@@ -113,10 +113,11 @@ static inline bool mlx5e_nisp_is_rx_flow(struct mlx5_cqe64 *cqe)
 	return false;
 }
 
-static inline void mlx5e_nisp_offload_handle_rx_skb(struct net_device *netdev,
+static inline bool mlx5e_nisp_offload_handle_rx_skb(struct net_device *netdev,
 						    struct sk_buff *skb,
 						    struct mlx5_cqe64 *cqe)
 {
+	return false;
 }
 
 static inline void mlx5e_nisp_csum_complete(struct net_device *netdev, struct sk_buff *skb) { }
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +30 -14

119 lines not on this page · raw message

#43 lore raw

Re: [RFC net-next 14/15] net/mlx5e: Add Rx data path offload

Quoted 11 lines · Jakub Kicinski
Quoted 9 lines · On Sun, 12 May 2024 21:54:38 -0400 Willem de Bruijn wrote:

Review

Quoted 6 lines
(quoted diff) +6 -0
+	/* TBD: report errors as SW counters to ethtool, any further handling ? */
+	switch (MLX5_NISP_METADATA_SYNDROM(nisp_meta_data)) {
+	case MLX5E_NISP_OFFLOAD_RX_SYNDROME_DECRYPTED:
+		if (psp_rcv(skb))
+			netdev_warn_once(netdev, "PSP handling failed");
+		skb->decrypted = 1;  

Do not set skb->decrypted if psp_rcv failed? But drop the packet and account the drop, likely.

Quoted 1 line

nVidia folks does this seem reasonable?

This seems reasonable. It's also what the comment above the switch suggests should be done.
psp_rcv unrefs the skb on errors (doesn't even return error in all cases) and I think it's no longer safe to touch it, unless there's another ref held somewhere.

I've tried implementing this tweak in the shared repo we have, but it seems it doesn't have the versions of this patch that you sent.

Cosmin.

author maintainer
#44 lore raw

RFC 15/15 net/mlx5e: Implement PSP key_rotate operation

In reply to Jakub Kicinski

From: Raed Salem <[email protected]>

Implement .key_rotate operation where when invoked will cause the HW to use a new master key to derive PSP spi/key pairs with complience with PSP spec.

---
 .../net/ethernet/mellanox/mlx5/core/en_accel/nisp.c   | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/nisp.c +11 -0
index 1131aa6e9b3d..cab4e79135d8 100644
@@ -96,11 +96,22 @@ static void mlx5e_psp_assoc_del(struct psp_dev *psd, struct psp_assoc *pas)
 	atomic_dec(&nisp->tx_key_cnt);
 }
 
+static int mlx5e_psp_key_rotate(struct psp_dev *psd, struct netlink_ext_ack *exack)
+{
+	struct mlx5e_priv *priv = netdev_priv(psd->main_netdev);
+
+	/* no support for protecting against external rotations */
+	psd->generation = 0;
+
+	return mlx5e_nisp_rotate_key(priv->mdev);
+}
+
 static struct psp_dev_ops mlx5_psp_ops = {
 	.set_config   = mlx5e_psp_set_config,
 	.rx_spi_alloc = mlx5e_psp_rx_spi_alloc,
 	.tx_key_add   = mlx5e_psp_assoc_add,
 	.tx_key_del   = mlx5e_psp_assoc_del,
+	.key_rotate   = mlx5e_psp_key_rotate,
 };
 
 void mlx5e_nisp_unregister(struct mlx5e_priv *priv)
Signature · 1 line
2.45.0
#45 lore raw
In reply to Jakub Kicinski
Quoted 7 lines · Jakub Kicinski

External email: Use caution opening links or attachments

Hi!

Add support for PSP encryption of TCP connections.

Quoted 8 lines

PSP is a protocol out of Google:
https://github.com/google/psp/blob/main/doc/PSP_Arch_Spec.pdf
which shares some similarities with IPsec. I added some more info in the first patch so I'll keep it short here.

The protocol can work in multiple modes including tunneling. But I'm mostly interested in using it as TLS replacement because of its superior offload characteristics.

Hi!

Thank you for doing this. I agree that TLS-like socket support is a main use-case. I'd like to hear what you think on a few other use-cases that I think should be considered as well since it may be difficult to add them as an afterthought:
- Tunnel mode. What are your plans for tunnel mode? Clearly it is different from the current approach in some aspects, for example, no sockets will be involved.
- RDMA. The ultra ethernet group has mentioned RDMA encryption using PSP. Do you think that RDMA verbs will support PSP in a similar manner to sockets? i.e., using netlink to pass parameters to the device and linking QPs to PSP SAs?
- Virtualization. How does PSP work from a VM? is the key shared with the hypervisor or is it private per-VM? and what about containers?

author maintainer
#46 lore raw
Quoted 7 lines · On Wed, 29 May 2024 11:16:12 +0200 Boris Pismenny wrote:

Thank you for doing this. I agree that TLS-like socket support is a main use-case. I'd like to hear what you think on a few other use-cases that I think should be considered as well since it may be difficult to add them as an afterthought:
- Tunnel mode. What are your plans for tunnel mode? Clearly it is different from the current approach in some aspects, for example, no sockets will be involved.

The drivers should only decap for known L4 protos, I think that's the only catch when we add tunnel support. Otherwise it should be fairly straightforward. Open a UDP socket in the kernel. Get a key
+ SPI using existing ops. Demux within the UDP socket using SPI.

Quoted 6 lines

- RDMA. The ultra ethernet group has mentioned RDMA encryption using PSP. Do you think that RDMA verbs will support PSP in a similar manner to sockets? i.e., using netlink to pass parameters to the device and linking QPs to PSP SAs?
- Virtualization. How does PSP work from a VM? is the key shared with the hypervisor or is it private per-VM?

Depends on the deployment and security model, really, but I'd expect the device key is shared, hypervisor is responsible for rotations, and mediates all key ops from the guests.

Quoted 1 line

and what about containers?

I tried to apply some of the lessons learned from TLS offload and made the "PSP device" a separate object. This should make it easy to "forward" the offload to software/container netdevs.

#47 lore raw
Quoted 8 lines · Jakub Kicinski
Quoted 7 lines · On Wed, 29 May 2024 11:16:12 +0200 Boris Pismenny wrote:

Thank you for doing this. I agree that TLS-like socket support is a main use-case. I'd like to hear what you think on a few other use-cases that I think should be considered as well since it may be difficult to add them as an afterthought:
- Tunnel mode. What are your plans for tunnel mode? Clearly it is different from the current approach in some aspects, for example, no sockets will be involved.

Quoted 4 lines

The drivers should only decap for known L4 protos, I think that's the only catch when we add tunnel support. Otherwise it should be fairly straightforward. Open a UDP socket in the kernel. Get a key
+ SPI using existing ops. Demux within the UDP socket using SPI.

IIUC, you refer to tunnel mode as if it offloads encryption alone while keeping headers intact. But, what I had in mind is a fully offloaded tunnel. This is called packet offload mode in IPsec, and with encryption such offloads rely on TC.

Note that the main use-case for PSP tunnel mode,
unlike transport mode, is carrying VM traffic as
indicated by the spec:
"The tunnel mode packet format is typically used in
virtualized environments.". With virtualization, encap/decap offload is an implicit assumption if not a performance necessity.

Quoted 9 lines
Quoted 6 lines

- RDMA. The ultra ethernet group has mentioned RDMA encryption using PSP. Do you think that RDMA verbs will support PSP in a similar manner to sockets? i.e., using netlink to pass parameters to the device and linking QPs to PSP SAs?
- Virtualization. How does PSP work from a VM? is the key shared with the hypervisor or is it private per-VM?

Depends on the deployment and security model, really, but I'd expect the device key is shared, hypervisor is responsible for rotations, and mediates all key ops from the guests.

I can imagine how this will work, but there are a few issues:
- Guests may run out of Tx keys, but they can't initiate key rotation without affecting others. This fate sharing between VMs seems undesirable.
- Unclear what sort of mediation is the hypervisor expected to provide: on the one hand, block a key rotation request and the requesting guest is denied service, on the other hand, allow key rotation and a guest may spam these requests to the hypervisor, which will also spam other VMs with notifications of key rotation.

Quoted 5 lines
Quoted 2 lines

and what about containers?

I tried to apply some of the lessons learned from TLS offload and made the "PSP device" a separate object. This should make it easy to "forward" the offload to software/container netdevs.

author maintainer
#48 lore raw
Quoted 10 lines · On Wed, 29 May 2024 22:01:52 +0200 Boris Pismenny wrote:
Quoted 4 lines

The drivers should only decap for known L4 protos, I think that's the only catch when we add tunnel support. Otherwise it should be fairly straightforward. Open a UDP socket in the kernel. Get a key
+ SPI using existing ops. Demux within the UDP socket using SPI.

IIUC, you refer to tunnel mode as if it offloads encryption alone while keeping headers intact. But, what I had in mind is a fully offloaded tunnel. This is called packet offload mode in IPsec, and with encryption such offloads rely on TC.

Do you see any challenge?

Quoted 4 lines
Quoted 3 lines

Depends on the deployment and security model, really, but I'd expect the device key is shared, hypervisor is responsible for rotations, and mediates all key ops from the guests.

Quoted 10 lines

I can imagine how this will work, but there are a few issues:
- Guests may run out of Tx keys, but they can't initiate key rotation without affecting others. This fate sharing between VMs seems undesirable.
- Unclear what sort of mediation is the hypervisor expected to provide: on the one hand, block a key rotation request and the requesting guest is denied service, on the other hand, allow key rotation and a guest may spam these requests to the hypervisor, which will also spam other VMs with notifications of key rotation.

I don't have much experience working with VMs, or a good understanding of what mlx5 does internally. Without access to the details of even a single device which does PSP - any comment I'd make would be too much of a speculation for my taste :(

On the NFP I'm pretty sure we could have given every VM a separate device key, no problem.