Skip to contentSkip to conversation

psp: introduce socket-less associations

PATCH New Todd Freed 1 participant 13 messages Nov 19

Subsystems (12)
lore
Share & tools
lore

Todd Freed

author
#0 lore raw

v2 cover psp: introduce socket-less associations

PSP associations today are bound to a TCP socket when created. Traffic on that socket is encapsulated in transport mode PSP, and encrypted.

This series adds PSP which isn't bound to a socket, which allows PSP encapsulation and encryption to be applied to forwarded traffic. You can use this to transparently apply PSP to flows, with help from userspace to manage the tc rules and orchestrate key exchange.

Socket-less PSP is managed over the PSP netlink family. Tx associations hold the peer's key in the device, and are addressable by ID, for the tc psp encap action. Rx does not have any kernel state.

Since the kernel keeps no Rx state, it doesn't enforce key rotation for socket-less Rx. Userspace must remove the decap rules of an SPI before a second rotation lets the device allocate the same SPI again. The documentation patch describes this, with ways to use tc to keep track of the rules.

Series structure:

1, 2/ Prepare PSP to work with non-TCP packets.

3, 4/ Add the netlink commands for socket-less PSP.

5/ Adds a METADATA_PSP dst which carries a socket-less PSP association on an skb for Tx, and the transmit checks for it.

6/ Adds the act_psp tc action, psp encap and psp decap. Encap is egress only.

7/ Adds flower matches on PSP header fields, for Rx matching.

8, 9/ Add drv-net tests (netdevsim and hardware).

10/ Makes tc-testing run dependsOn probes outside the test namespace, where they currently always fail.

11/ Adds tc-testing tests.

12/ Documentation for socket-less PSP.

A following series adds hardware offload of the psp actions and flower matches, PSP tunnel mode (which is offload only and therefore not included here), encap on ingress (also offload only), and the mlx5 implementation for switchdev mode. It's available in the tree below.

Dependency on net:

v1 patches 6-8 are now a separate series to net:

"net: don't redirect decrypted skbs to ingress, psp time-wait validator"

https://lore.kernel.org/netdev/[email protected]

This series builds without it, but relies on it: act_psp marks the packets it encaps decrypted, which mirred and bpf must not send back to ingress (the data_tc_encap_ingress test fails without the fix), and act_psp drops packets of sockets with a validator, which non-PSP time-wait sockets have without the fix.

Relation to the PSP fixes in net:

"psp, tls: don't send offloaded contents in cleartext"

https://lore.kernel.org/netdev/[email protected]

When net is merged into net-next:

- net/psp/psp_main.c conflicts around psp_dev_encap_ok(), which both
add: net's only allows TCP, this series' (patch 2) takes any L4 protocol. Keep this series' version of the function, and its comments above it and above psp_dev_encapsulate().
- tools/testing/selftests/drivers/net/psp.py and tools/testing/selftests/drivers/net/config conflict: keep both sides (imports, constants, the list of test cases; the NET_ACT_* options).

Relation to the netfilter fixes in nf-next:

Packets which a PSP device decrypted don't have to end in a local PSP socket. They can already be forwarded or bridged today, but with socket-less PSP that is the intended use, e.g. a router that decaps and forwards. Netfilter handles two cases itself, outside the IP stack paths which the net series fixes, and would send such a packet's decrypted contents in clear text:

- The bridge refragments packets which bridge conntrack reassembled (br_ip6_fragment(), nf_br_ip_fragment()), without the check of "net:
don't fragment decrypted skbs".
- The reject code builds its own ICMP errors and TCP resets (nf_reject_skb_v4_unreach() and friends), without icmp_send() and icmpv6_send(), which the net series' ICMP patches cover.

A separate series to nf-next applies the net series' rules to both:

"netfilter: bridge: don't refragment decrypted skbs" "netfilter: reject: don't reply to decrypted skbs"

https://lore.kernel.org/netfilter-devel/[email protected]

It doesn't depend on this series, and the two don't conflict.

Trees:

The full tree, with the net fixes, the nf-next series, this series and the mlx5 offload series, and the conflicts above resolved, is at:

https://github.com/toddfreed/psptc branch psptc

iproute2 support for the psp action and the PSP flower matches, which the selftests need, is posted to iproute2-next as "tc: add the psp action and flower psp matches", and at:

https://github.com/toddfreed/iproute2-psptc branch tc-psp

It needs no change for v2: the tc uAPI is the same.

Testing:

- netdevsim: drivers/net psp.py passes (57 tests), and tc-testing psp.json (15 tests), with the net fixes series applied and tc from the iproute2-next series.
- mlx5 (ConnectX-7, FW 28.48), two hosts: drivers/net psp.py passes:
46 pass, 8 skip (GMAC versions not supported by the device), 3 xfail (netdevsim only), with the net fixes series applied.
- Each patch builds without warnings with gcc and clang, allmodconfig, W=1.

v2:
- Moved the mirred, bpf and time-wait validator fixes (v1 patches 6-8) to a net series, with Fixes tags. (Daniel)
- Don't store Rx associations at all (Daniel)
- Keep socket-less Tx associations on their own device list (Daniel)
- Documentation: the userspace key rotation contract, and the use of tc action cookies, filter handles and a chain per key phase to track decap rules. (Daniel)
- act_psp: add .get_fill_size. (Sashiko, Jamal)
- psp.py: use ctl_file_write(), and _check_data_outq() to wait for retransmits. (Sashiko)
- tc-testing dependsOn fix: add a Fixes tag, and document the probe stage. (Sashiko)
- Outer UDP source port: only use sk_hash for TCP sockets. (Sashiko)
- flower: don't offload a rule which uses the PSP matches, and reject it with skip_sw, until a driver supports them: some drivers would offload it without them. (Sashiko)
- Moved the drop of a packet with both a socket offload and a METADATA_PSP dst to the METADATA_PSP patch, with the rest of sk_validate_xmit_skb().
- Say that encap on ingress isn't supported, rather than hardware offload only, in act_psp.c, psp.rst and the commit messages. (Sashiko)
- psp.rst: an encapped packet is dropped if it loses its association or the PSP device is unregistered, not when the association is deleted. (Sashiko)
- tc-testing: the tests of rejected psp actions match the kernel's extack, the decap with an association and encap without an association tests are dropped (tc rejects those itself), and the flower decap tests also probe for the psp action. (Sashiko)
- Rebased on net-next.

v1: https://lore.kernel.org/netdev/[email protected]

Todd Freed (12):
psp: get netns from net_device instead of sock
psp: take the psp nexthdr from the packet on encap
psp: declare psp_assoc_set_tx() in psp.h
psp: add support for socket-less psp associations
net: add METADATA_PSP dst to carry a psp association on an skb net/sched: add act_psp for psp transport mode
net/sched: flower: add psp_spi, psp_ver, and psp_dev matches
selftests: drv-net: psp: test socket-less associations and the tc psp
action
selftests: drv-net: psp: test that software encap on ingress drops
selftests: tc-testing: run dependsOn probes outside the test namespace
selftests: tc-testing: add tests for the psp action
Documentation: networking: psp: document socket-less associations

 Documentation/netlink/specs/psp.yaml          |  91 +++
 Documentation/netlink/specs/tc.yaml           |  14 +
 Documentation/networking/psp.rst              | 169 +++-
 MAINTAINERS                                   |   3 +
 .../mellanox/mlx5/core/en_accel/psp_rxtx.c    |   2 +-
 .../net/ethernet/mellanox/mlx5/core/en_main.c |  33 +
 drivers/net/netdevsim/psp.c                   |   2 +-
 include/linux/skbuff.h                        |   4 +
 include/net/dst_metadata.h                    |  19 +
 include/net/flow_dissector.h                  |  18 +
 include/net/psp/functions.h                   |  82 +-
 include/net/psp/types.h                       |   2 +
 include/net/tc_act/tc_psp.h                   |  23 +
 include/uapi/linux/pkt_cls.h                  |   6 +
 include/uapi/linux/psp.h                      |   6 +
 include/uapi/linux/tc_act/tc_psp.h            |  28 +
 net/core/dev.c                                |  50 +-
 net/core/dst.c                                |   5 +
 net/core/flow_dissector.c                     |  27 +
 net/psp/Makefile                              |   2 +-
 net/psp/psp-nl-gen.c                          |  60 ++
 net/psp/psp-nl-gen.h                          |   8 +
 net/psp/psp.h                                 |   7 +
 net/psp/psp_assoc.c                           | 122 +++
 net/psp/psp_main.c                            |  62 +-
 net/psp/psp_nl.c                              | 242 ++++++
 net/psp/psp_sock.c                            |  16 +-
 net/sched/Kconfig                             |  13 +
 net/sched/Makefile                            |   1 +
 net/sched/act_psp.c                           | 439 +++++++++++
 net/sched/cls_flower.c                        |  74 ++
 tools/testing/selftests/drivers/net/config    |   5 +
 tools/testing/selftests/drivers/net/psp.py    | 729 +++++++++++++++++-
 tools/testing/selftests/tc-testing/README     |   1 +
 tools/testing/selftests/tc-testing/config     |   2 +
 .../creating-testcases/AddingTestCases.txt    |   4 +-
 .../tc-testing/tc-tests/actions/psp.json      | 403 ++++++++++
 tools/testing/selftests/tc-testing/tdc.py     |   2 +-
 tools/testing/selftests/tc-testing/tdc.sh     |   1 +
 39 files changed, 2727 insertions(+), 50 deletions(-)
 create mode 100644 include/net/tc_act/tc_psp.h
 create mode 100644 include/uapi/linux/tc_act/tc_psp.h
 create mode 100644 net/psp/psp_assoc.c
 create mode 100644 net/sched/act_psp.c
 create mode 100644 tools/testing/selftests/tc-testing/tc-tests/actions/psp.json

base-commit: 89f0fb60afdb35cf2a94e6279c8960feb1c4d1d4

Signature · 1 line
2.53.1-Meta
author
#1 lore raw

PATCH v2 1/12 psp: get netns from net_device instead of sock

Every PSP skb is related to a socket today, but that will no longer be true once a PSP association can be attached to the skb by other means, such as the tc psp action added later in the series.

Get the netns from the transmitting device instead.

For the outer source port range, psp_write_headers() now explicitly uses sock_net(sk) in the socket case specifically, to maintain the existing behavior.

---
 drivers/net/ethernet/mellanox/mlx5/core/en_accel/psp_rxtx.c | 2 +-
 drivers/net/netdevsim/psp.c                                 | 2 +-
 net/psp/psp_main.c                                          | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/psp_rxtx.c +1 -1
index 348fd7a96261..8b474f75dd61 100644
@@ -165,7 +165,7 @@ bool mlx5e_psp_handle_tx_skb(struct net_device *netdev,
 			     struct mlx5e_accel_tx_psp_state *psp_st)
 {
 	struct mlx5e_priv *priv = netdev_priv(netdev);
-	struct net *net = sock_net(skb->sk);
+	struct net *net = dev_net(netdev);
 
 	if (!mlx5e_psp_set_state(priv, skb, psp_st))
 		return true;
drivers/net/netdevsim/psp.c +1 -1
index b7452c49b581..0701f38aee6d 100644
@@ -36,7 +36,7 @@ nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
 		goto out_unlock;
 	}
 
-	net = sock_net(skb->sk);
+	net = dev_net(ns->netdev);
 	if (!psp_dev_encapsulate(net, skb, pas->tx.spi, pas->version, 0)) {
 		rc = SKB_DROP_REASON_PSP_OUTPUT;
 		goto out_unlock;
net/psp/psp_main.c +1 -1
index 273b010d2355..e09f6fec948d 100644
@@ -221,7 +221,7 @@ static void psp_write_headers(struct net *net, struct sk_buff *skb, __be32 spi,
 		/* These operations are cheap, no need to cache the result
 		 * in another socket field.
 		 */
-		inet_get_local_port_range(net, &min, &max);
+		inet_get_local_port_range(sock_net(sk), &min, &max);
 		/* Since this is being sent on the wire obfuscate hash a bit
 		 * to minimize possibility that any useful information to an
 		 * attacker is leaked. Only upper 16 bits are relevant in the
Signature · 1 line
2.53.1-Meta
author
#2 lore raw

PATCH v2 2/12 psp: take the psp nexthdr from the packet on encap

In reply to Todd Freed

PSP socket support has thus far been limited to TCP, and psp_dev_encapsulate() hardcodes IPPROTO_TCP as the PSP next header.

Socket-less PSP associations will also support UDP traffic. Populate the PSP next header from the IP header of the original packet instead.

With the next header taken from the fixed header, psp_dev_encapsulate() now refuses some cases which are unsupported:

- IPv4 and IPv6 fragments. Non-initial fragments have no L4 header, so this cannot work. Not a behavior change for PSP sockets.
- IPv6 extension headers. More generally, any packet whose transport header isn't right after the fixed IP header. Some of these could be supported in the future. Not a behavior change for PSP sockets.
- Already encapsulated for a tunnel (skb->encapsulation). Their inner headers are already set up.

Both callers of psp_dev_encapsulate(), mlx5 and netdevsim, drop the packet when it returns false.

mlx5e also fixes up the TCP checksum of GSO skbs for the device, which is only valid for TCP. Have the stack segment other GSO skbs, e.g. UDP from socket-less associations, in software. Likewise the device only completes a TCP or UDP checksum behind the PSP header: have the stack checksum the other CHECKSUM_PARTIAL skbs.

For the outer UDP source port, psp_write_headers() already branches to use TCP ehash if the skb has a socket, with fallback to udp_flow_src_port(). Previously, sk always meant TCP. Add an explicit check on nexthdr, and on the socket being a TCP one (a raw or packet socket can send TCP segments, and has no sk_hash), so that the fallback branch is taken for other packets. udp_flow_src_port() hashes the flow if the skb has no hash yet: compute it in psp_dev_encapsulate() before the headers are moved and the IP protocol rewritten, so that it covers the packet's own flow rather than the outer headers being written.

---
 .../net/ethernet/mellanox/mlx5/core/en_main.c | 33 +++++++++++
 net/psp/psp_main.c                            | 56 +++++++++++++++----
 2 files changed, 79 insertions(+), 10 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_main.c +33 -0
index 2be8147fdb64..d6c32080634d 100644
@@ -5139,6 +5139,23 @@ static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
 }
 
+/* The L4 protocol right after the fixed IP header, which the PSP Tx path
+ * requires, is one the device can complete the checksum of
+ */
+static bool mlx5e_psp_l4_csum_ok(const struct sk_buff *skb)
+{
+	u8 proto;
+
+	if (skb->protocol == htons(ETH_P_IP))
+		proto = ip_hdr(skb)->protocol;
+	else if (skb->protocol == htons(ETH_P_IPV6))
+		proto = ipv6_hdr(skb)->nexthdr;
+	else
+		return false;
+
+	return proto == IPPROTO_TCP || proto == IPPROTO_UDP;
+}
+
 netdev_features_t mlx5e_features_check(struct sk_buff *skb,
 				       struct net_device *netdev,
 				       netdev_features_t features)
@@ -5147,6 +5164,22 @@ netdev_features_t mlx5e_features_check(struct sk_buff *skb,
 
 	features = vlan_features_check(skb, features);
 
+	/* The PSP tx path fixes up the TCP checksum of a GSO skb for the
+	 * device, which is only valid for TCP, and the device only completes
+	 * a TCP or UDP checksum behind the PSP header. Have the stack segment
+	 * and checksum other skbs.
+	 */
+	if ((skb_is_gso(skb) || skb->ip_summed == CHECKSUM_PARTIAL) &&
+	    mlx5e_psp_is_offload(skb, netdev)) {
+		if (skb_is_gso(skb) &&
+		    !(skb_shinfo(skb)->gso_type &
+		      (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
+			features &= ~NETIF_F_GSO_MASK;
+		if (skb->ip_summed == CHECKSUM_PARTIAL &&
+		    !mlx5e_psp_l4_csum_ok(skb))
+			features &= ~NETIF_F_CSUM_MASK;
+	}
+
 	/* Validate if the tunneled packet is being offloaded by HW */
 	if (skb->encapsulation &&
 	    (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
net/psp/psp_main.c +46 -10

100 lines not on this page · raw message

Signature · 1 line
2.53.1-Meta
author
#3 lore raw

PATCH v2 3/12 psp: declare psp_assoc_set_tx() in psp.h

In reply to Todd Freed

Socket-less PSP associations will need to install a Tx key the same way sockets do.

Make psp_assoc_set_tx() non-static, and declare it in net/psp/psp.h.

---
 net/psp/psp.h      |  3 +++
 net/psp/psp_sock.c | 15 ++++++++++++---
 2 files changed, 15 insertions(+), 3 deletions(-)
net/psp/psp.h +3 -0
index b123c2427905..de4be3320009 100644
@@ -24,6 +24,9 @@ void psp_nl_notify_disassoc(struct psp_dev *psd, struct net *net);
 struct psp_assoc *psp_assoc_create(struct psp_dev *psd);
 struct psp_dev *psp_dev_get_for_sock(struct sock *sk);
 void psp_dev_tx_key_del(struct psp_dev *psd, struct psp_assoc *pas);
+int psp_assoc_set_tx(struct psp_dev *psd, struct psp_assoc *pas,
+		     struct psp_key_parsed *key,
+		     struct netlink_ext_ack *extack);
 int psp_sock_assoc_set_rx(struct sock *sk, struct psp_assoc *pas,
 			  struct psp_key_parsed *key,
 			  struct netlink_ext_ack *extack);
net/psp/psp_sock.c +12 -3
index f2cba3fa3a23..d04146273bf2 100644
@@ -185,9 +185,18 @@ int psp_sock_assoc_set_rx(struct sock *sk, struct psp_assoc *pas,
 	return err;
 }
 
-static int psp_assoc_set_tx(struct psp_dev *psd, struct psp_assoc *pas,
-			    struct psp_key_parsed *key,
-			    struct netlink_ext_ack *extack)
+/**
+ * psp_assoc_set_tx() - install a Tx key for an association
+ * @psd:	PSP device, must be locked
+ * @pas:	association, must belong to @psd and not have a Tx key yet
+ * @key:	Tx SPI and key
+ * @extack:	netlink extended ack
+ *
+ * Return: 0 on success, negative errno otherwise.
+ */
+int psp_assoc_set_tx(struct psp_dev *psd, struct psp_assoc *pas,
+		     struct psp_key_parsed *key,
+		     struct netlink_ext_ack *extack)
 {
 	int err;
 
Signature · 1 line
2.53.1-Meta
author
#4 lore raw

PATCH v2 4/12 psp: add support for socket-less psp associations

In reply to Todd Freed

Currently, a PSP association is bound to a TCP socket when it is created, and can hold both an Rx and a Tx key.

Add Tx associations which are not bound to a socket, and are referenced by an ID, so that other parts of the stack can attach them to packets. See the subsequent patches, where the tc psp action does just this.

Socket-less Rx does not need an association, and the kernel does not keep anything for this case. For Rx, userspace calls the rx-spi-alloc operation, and then uses tc filters that match the corresponding SPI, version, and PSP device. The flower matches for these fields are added in a subsequent patch.

Add the following new netlink commands. They are admin-only, and restricted to the netns of the PSP device.

rx-spi-alloc allocate an Rx SPI and derived key, without a socket.
The key is delivered to the peer out of band. Also returns the number of key rotations of the PSP device so far (key-rotations)
assoc-add create a Tx association and install the peer's key,
returns its ID
assoc-del delete a Tx association
assoc-get get / dump Tx associations; only the SPI is reported

Socket associations are not addressed by these commands, so for example assoc-get does not list them.

The kernel doesn't enforce key rotation for socket-less Rx the way it does for socket associations. A tc filter which matches a SPI of one generation will also match that SPI if subsequently re-used in another generation. It is up to userspace to deal with this eventuality by removing such rules in time. Userspace may subscribe to rotation notifications for this purpose.

Tx associations hold the peer's key, which device key rotation doesn't affect. They are on a device list of their own, which key rotation doesn't walk, and are deleted when their PSP device is unregistered.

The Rx key is returned once, by rx-spi-alloc, and not stored. The Tx key is passed in by assoc-add and never reported back: assoc-get only reports the SPI, and users reference Tx associations by ID.

---
 Documentation/netlink/specs/psp.yaml |  91 ++++++++++
 include/net/psp/functions.h          |   1 +
 include/net/psp/types.h              |   2 +
 include/uapi/linux/psp.h             |   6 +
 net/psp/Makefile                     |   2 +-
 net/psp/psp-nl-gen.c                 |  60 +++++++
 net/psp/psp-nl-gen.h                 |   8 +
 net/psp/psp.h                        |   4 +
 net/psp/psp_assoc.c                  | 122 ++++++++++++++
 net/psp/psp_main.c                   |   4 +
 net/psp/psp_nl.c                     | 242 +++++++++++++++++++++++++++
 net/psp/psp_sock.c                   |   1 +
 12 files changed, 542 insertions(+), 1 deletion(-)
 create mode 100644 net/psp/psp_assoc.c
Documentation/netlink/specs/psp.yaml +91 -0
index f3266763c325..5bb08829503d 100644
@@ -103,6 +103,21 @@ attribute-sets:
         name: sock-fd
         doc: Sockets which should be bound to the association immediately.
         type: u32
+      -
+        name: id
+        doc: |
+          ID of a Tx association which is not bound to a socket.
+          Such associations can be attached to packets with the tc psp action.
+        type: u32
+        checks:
+          min: 1
+      -
+        name: key-rotations
+        doc: |
+          Number of key rotations of the device (key-rotations in get-stats)
+          when an Rx SPI was allocated. The device decrypts the SPI until its
+          key has been rotated twice more.
+        type: uint
   -
     name: keys
     attributes:
@@ -350,6 +365,82 @@ operations:
           attributes: []
         pre: psp-device-get-locked
         post: psp-device-unlock
+    -
+      name: rx-spi-alloc
+      doc: |
+        Allocate a new Rx key + SPI pair without binding it to a socket.
+        The key is derived by the device and must be passed to the peer
+        out of band, to be installed there with assoc-add. The kernel
+        keeps no state for it: a tc rule takes the packets the device
+        decrypted with the SPI with the flower psp_spi, psp_ver and
+        psp_dev keys. The SPI is decrypted until the device key has been
+        rotated twice more, after which the device can allocate it again:
+        user space must remove rules matching it by then.
+      attribute-set: assoc
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - dev-id
+            - version
+        reply:
+          attributes:
+            - dev-id
+            - rx-key
+            - key-rotations
+        pre: psp-assoc-device-get-locked-admin
+        post: psp-device-unlock
+    -
+      name: assoc-add
+      doc: |
+        Add a PSP Tx association which is not bound to a socket.
+        Returns the ID of the new association.
+      attribute-set: assoc
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - dev-id
+            - version
+            - tx-key
+        reply:
+          attributes:
+            - id
+            - dev-id
+        pre: psp-assoc-device-get-locked-admin
+        post: psp-device-unlock
+    -
+      name: assoc-del
+      doc: |
+        Delete a PSP Tx association which is not bound to a socket.
+        Packets and tc actions which still reference it keep it alive.
+      attribute-set: assoc
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - id
+        reply:
+          attributes: []
+    -
+      name: assoc-get
+      doc: |
+        Get / dump Tx associations which are not bound to a socket.
+        Key material is never reported: tx-key carries the SPI only.
+      attribute-set: assoc
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - id
+        reply: &assoc-all
+          attributes:
+            - id
+            - dev-id
+            - version
+            - tx-key
+      dump:
+        reply: *assoc-all
 
 mcast-groups:
   list:
include/net/psp/functions.h +1 -0
index 410adb6718c1..210aedc070e6 100644
@@ -23,6 +23,7 @@ int psp_dev_rcv(struct sk_buff *skb, u16 dev_id, u8 generation, bool strip_icv);
 
 /* Kernel-facing API */
 void psp_assoc_put(struct psp_assoc *pas);
+struct psp_assoc *psp_assoc_get_by_id(struct net *net, u32 id);
 
 #if IS_ENABLED(CONFIG_INET_PSP)
 unsigned int psp_key_size(u32 version);
include/net/psp/types.h +2 -0
index b8905efbd604..be383f8e65d0 100644
@@ -107,6 +107,7 @@ struct psp_dev {
 	struct list_head active_assocs;
 	struct list_head prev_assocs;
 	struct list_head stale_assocs;
+	struct list_head sockless_assocs;
 
 	struct {
 		unsigned long rotations;
@@ -154,6 +155,7 @@ struct psp_key_parsed {
 
 struct psp_assoc {
 	struct psp_dev *psd;
+	u32 id;
 
 	u16 dev_id;
 	u8 generation;
include/uapi/linux/psp.h +6 -0
index 1c8899cd4da5..e62462a9542b 100644
@@ -44,6 +44,8 @@ enum {
 	PSP_A_ASSOC_RX_KEY,
 	PSP_A_ASSOC_TX_KEY,
 	PSP_A_ASSOC_SOCK_FD,
+	PSP_A_ASSOC_ID,
+	PSP_A_ASSOC_KEY_ROTATIONS,
 
 	__PSP_A_ASSOC_MAX,
 	PSP_A_ASSOC_MAX = (__PSP_A_ASSOC_MAX - 1)
@@ -87,6 +89,10 @@ enum {
 	PSP_CMD_GET_STATS,
 	PSP_CMD_DEV_ASSOC,
 	PSP_CMD_DEV_DISASSOC,
+	PSP_CMD_RX_SPI_ALLOC,
+	PSP_CMD_ASSOC_ADD,
+	PSP_CMD_ASSOC_DEL,
+	PSP_CMD_ASSOC_GET,
 
 	__PSP_CMD_MAX,
 	PSP_CMD_MAX = (__PSP_CMD_MAX - 1)
net/psp/Makefile +1 -1
index eb5ff3c5bfb2..d7f026f937cb 100644
@@ -2,4 +2,4 @@
 
 obj-$(CONFIG_INET_PSP) += psp.o
 
-psp-y := psp_main.o psp_nl.o psp_sock.o psp-nl-gen.o
+psp-y := psp_main.o psp_nl.o psp_sock.o psp_assoc.o psp-nl-gen.o
net/psp/psp-nl-gen.c +60 -0

72 lines not on this page · raw message

net/psp/psp-nl-gen.h +8 -0
index 24d51bff997f..27474f1ce112 100644
@@ -24,6 +24,9 @@ int psp_assoc_device_get_locked(const struct genl_split_ops *ops,
 int psp_device_get_locked_dev_assoc(const struct genl_split_ops *ops,
 				    struct sk_buff *skb,
 				    struct genl_info *info);
+int psp_assoc_device_get_locked_admin(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);
@@ -38,6 +41,11 @@ int psp_nl_get_stats_doit(struct sk_buff *skb, struct genl_info *info);
 int psp_nl_get_stats_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
 int psp_nl_dev_assoc_doit(struct sk_buff *skb, struct genl_info *info);
 int psp_nl_dev_disassoc_doit(struct sk_buff *skb, struct genl_info *info);
+int psp_nl_rx_spi_alloc_doit(struct sk_buff *skb, struct genl_info *info);
+int psp_nl_assoc_add_doit(struct sk_buff *skb, struct genl_info *info);
+int psp_nl_assoc_del_doit(struct sk_buff *skb, struct genl_info *info);
+int psp_nl_assoc_get_doit(struct sk_buff *skb, struct genl_info *info);
+int psp_nl_assoc_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
 
 enum {
 	PSP_NLGRP_MGMT,
net/psp/psp.h +4 -0
index de4be3320009..cadeb1ad04a2 100644
@@ -35,6 +35,10 @@ int psp_sock_assoc_set_tx(struct sock *sk, struct psp_dev *psd,
 			  struct netlink_ext_ack *extack);
 void psp_assocs_key_rotated(struct psp_dev *psd);
 
+int psp_assoc_publish(struct psp_assoc *pas);
+void psp_assoc_unpublish(struct psp_assoc *pas);
+struct psp_assoc *psp_assoc_get_next(struct net *net, unsigned long *index);
+
 static inline void psp_dev_get(struct psp_dev *psd)
 {
 	refcount_inc(&psd->refcnt);
net/psp/psp_assoc.c new file +122 -0

122 lines not on this page · raw message

net/psp/psp_main.c +4 -0
index 4b48070fb9e5..dde653c81fc5 100644
@@ -87,6 +87,7 @@ psp_dev_create(struct net_device *netdev,
 	INIT_LIST_HEAD(&psd->active_assocs);
 	INIT_LIST_HEAD(&psd->prev_assocs);
 	INIT_LIST_HEAD(&psd->stale_assocs);
+	INIT_LIST_HEAD(&psd->sockless_assocs);
 	refcount_set(&psd->refcnt, 1);
 
 	mutex_lock(&psp_devs_lock);
@@ -147,10 +148,13 @@ void psp_dev_unregister(struct psp_dev *psd)
 
 	list_splice_init(&psd->active_assocs, &psd->prev_assocs);
 	list_splice_init(&psd->prev_assocs, &psd->stale_assocs);
+	list_splice_init(&psd->sockless_assocs, &psd->stale_assocs);
 	list_for_each_entry_safe(pas, next, &psd->stale_assocs, assocs_list) {
 		if (psp_assoc_needs_tx_key_del(pas))
 			psp_dev_tx_key_del(psd, pas);
 		list_del(&pas->assocs_list);
+		if (pas->id)
+			psp_assoc_unpublish(pas);
 	}
 
 	list_for_each_entry_safe(entry, entry_tmp, &psd->assoc_dev_list,
net/psp/psp_nl.c +242 -0

254 lines not on this page · raw message

net/psp/psp_sock.c +1 -0
index d04146273bf2..fe9fc5cbdc71 100644
@@ -140,6 +140,7 @@ void psp_assoc_put(struct psp_assoc *pas)
 	if (pas && refcount_dec_and_test(&pas->refcnt))
 		call_rcu(&pas->rcu, psp_assoc_free_queue);
 }
+EXPORT_SYMBOL_GPL(psp_assoc_put);
 
 void psp_sk_assoc_free(struct sock *sk)
 {
Signature · 1 line
2.53.1-Meta
author
#5 lore raw

PATCH v2 5/12 net: add METADATA_PSP dst to carry a psp association on an skb

In reply to Todd Freed

Add a metadata_dst type holding a reference to a PSP association, so that an association can be attached to an skb that has no socket, e.g. by a tc action.

Change psp_skb_get_assoc_rcu() to check for METADATA_PSP, then fall back to the socket, so that existing driver Tx paths (mlx5, netdevsim) encrypt such packets without any changes.

Once an skb has been marked for PSP xmit, it must be transmitted on the correct device, otherwise it may go onto the wire in clear text. For packets of a PSP socket, sk_validate_xmit_skb() handles this.

For socket-less PSP packets, the PSP metadata dst carries this information, but __dev_queue_xmit() normally releases the dst before queueing. Add a new check for a PSP metadata dst at that point. If it's the wrong device, drop the packet; if it's the right device, keep the dst so that the driver can use it.

The right device is the PSP device's main netdev: devices associated with it (dev-assoc, e.g. veth or netkit) have its psp_dev too, but forward the packet rather than encrypt it, and drop the dst on the way. Prevent xmit on those.

Also check that the packet will still fit the device's MTU once the device adds the PSP encapsulation (UDP + PSP + ICV, 40 bytes), and drop it with SKB_DROP_REASON_PKT_TOO_BIG otherwise. Note that no ICMP error is sent in this case. PSP sockets do leave room for it (psp_sk_overhead()).

Check a packet with a METADATA_PSP dst again when it is validated at dequeue, in sk_validate_xmit_skb(). If the PSP device was unregistered since enqueue, the packet is dropped.

A packet of a socket with its own encryption offload (PSP or kTLS) must not carry a METADATA_PSP dst: sk_validate_xmit_skb() would apply the socket's validator instead of checking the association. Whatever attaches the dst refuses such packets, but the socket can gain an offload while the packet is queued, so sk_validate_xmit_skb() drops a packet which has both. It reads sk->sk_validate_xmit_skb with READ_ONCE(), which pairs with the WRITE_ONCE() in psp_sock_set_tx_key().

Note that a packet which the qdisc requeues after validating it (q->gso_skb) isn't validated again when it is dequeued. That is a pre-existing gap that also affects packets of PSP sockets. A separate fix will address this for both use cases.

---
 include/net/dst_metadata.h  | 19 +++++++++
 include/net/psp/functions.h | 81 ++++++++++++++++++++++++++++++++++++-
 net/core/dev.c              | 50 +++++++++++++++++++----
 net/core/dst.c              |  5 +++
 4 files changed, 146 insertions(+), 9 deletions(-)
include/net/dst_metadata.h +19 -0
index 02fffbb85099..bc6ac51dde9c 100644
@@ -13,6 +13,7 @@ enum metadata_type {
 	METADATA_HW_PORT_MUX,
 	METADATA_MACSEC,
 	METADATA_XFRM,
+	METADATA_PSP,
 };
 
 struct hw_port_info {
@@ -30,6 +31,14 @@ struct xfrm_md_info {
 	struct dst_entry *dst_orig;
 };
 
+/**
+ * struct psp_md_info - PSP association to encrypt a packet with
+ * @pas: association, the metadata_dst holds a reference on it
+ */
+struct psp_md_info {
+	struct psp_assoc *pas;
+};
+
 struct metadata_dst {
 	struct dst_entry		dst;
 	enum metadata_type		type;
@@ -38,6 +47,7 @@ struct metadata_dst {
 		struct hw_port_info	port_info;
 		struct macsec_info	macsec_info;
 		struct xfrm_md_info	xfrm_info;
+		struct psp_md_info	psp_info;
 	} u;
 };
 
@@ -51,6 +61,13 @@ static inline struct metadata_dst *skb_metadata_dst(const struct sk_buff *skb)
 	return NULL;
 }
 
+static inline bool skb_psp_md(const struct sk_buff *skb)
+{
+	struct metadata_dst *md_dst = skb_metadata_dst(skb);
+
+	return md_dst && md_dst->type == METADATA_PSP;
+}
+
 static inline struct ip_tunnel_info *
 skb_tunnel_info(const struct sk_buff *skb)
 {
@@ -127,6 +144,8 @@ static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
 	case METADATA_XFRM:
 		return memcmp(&a->u.xfrm_info, &b->u.xfrm_info,
 			      sizeof(a->u.xfrm_info));
+	case METADATA_PSP:
+		return a->u.psp_info.pas != b->u.psp_info.pas;
 	default:
 		return 1;
 	}
include/net/psp/functions.h +80 -1

116 lines not on this page · raw message

net/core/dev.c +42 -8

87 lines not on this page · raw message

net/core/dst.c +5 -0
index 092861133023..38ad136cb46f 100644
@@ -26,6 +26,7 @@
 
 #include <net/dst.h>
 #include <net/dst_metadata.h>
+#include <net/psp/functions.h>
 
 int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
 {
@@ -313,6 +314,8 @@ void metadata_dst_free(struct metadata_dst *md_dst)
 #endif
 	if (md_dst->type == METADATA_XFRM)
 		dst_release(md_dst->u.xfrm_info.dst_orig);
+	if (md_dst->type == METADATA_PSP)
+		psp_assoc_put(md_dst->u.psp_info.pas);
 	kfree(md_dst);
 }
 EXPORT_SYMBOL_GPL(metadata_dst_free);
@@ -349,6 +352,8 @@ void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst)
 #endif
 		if (one_md_dst->type == METADATA_XFRM)
 			dst_release(one_md_dst->u.xfrm_info.dst_orig);
+		if (one_md_dst->type == METADATA_PSP)
+			psp_assoc_put(one_md_dst->u.psp_info.pas);
 	}
 	free_percpu(md_dst);
 }
Signature · 1 line
2.53.1-Meta
author
#6 lore raw

PATCH v2 6/12 net/sched: add act_psp for psp transport mode

In reply to Todd Freed

Add the tc action act_psp, for PSP transport mode encapsulation and encryption.

encap attaches a PSP Tx association to the skb as a METADATA_PSP dst. The packet must then be transmitted on the main netdev of the association's PSP device, which encrypts it: encap on that netdev's egress, or a redirect there from another netdev's egress. Any other device drops it.

Encap on ingress isn't supported. Conceptually it could work, but many paths on the way out replace the dst (routing, netfilter, bpf redirects, tc actions), and the packet would then leave in clear text. A later series will add support for encap on ingress, in hardware offload only.

On egress, encap sets skb->decrypted, so that sk_validate_xmit_skb() drops the packet if it loses the METADATA_PSP dst on the way.

Some kinds of packets are not supported and are dropped:

- IPv4 and IPv6 fragments. Encap inserts UDP + PSP between the L3 and L4 headers, and non-initial fragments don't have L4 headers, so transport mode can't support fragments. Tunnel mode PSP, which a later series adds, does support them.
- IPv6 packets with extension headers. Some extension headers could be supported in the future.
- Packets already encapsulated for a tunnel (skb->encapsulation). Some tunnel types could be supported in the future.
- Packets which are too large. On transmit, packets which won't fit the device's MTU once encapsulated are dropped (see the METADATA_PSP patch). The traffic's MTU needs to leave room for the PSP encap.
- Packets with another encryption offload. Packets of a PSP or kTLS offload socket, for which sk_validate_xmit_skb() would apply the socket's validator instead of checking the association.

Decap removes the PSP Rx state of a packet the device decrypted. To take the packets of only one Rx SPI, a rule matches the PSP header fields it was decrypted with: psp_spi, psp_ver and psp_dev. Those flower matches are added in a subsequent patch.

---
 MAINTAINERS                        |   3 +
 include/net/tc_act/tc_psp.h        |  23 ++
 include/uapi/linux/pkt_cls.h       |   1 +
 include/uapi/linux/tc_act/tc_psp.h |  28 ++
 net/sched/Kconfig                  |  13 +
 net/sched/Makefile                 |   1 +
 net/sched/act_psp.c                | 439 +++++++++++++++++++++++++++++
 7 files changed, 508 insertions(+)
 create mode 100644 include/net/tc_act/tc_psp.h
 create mode 100644 include/uapi/linux/tc_act/tc_psp.h
 create mode 100644 net/sched/act_psp.c
MAINTAINERS +3 -0
index 40bb77cbc6b0..7454532cd35a 100644
@@ -22557,8 +22557,11 @@ F:	Documentation/netlink/specs/psp.yaml
 F:	Documentation/networking/psp.rst
 F:	include/net/psp/
 F:	include/net/psp.h
+F:	include/net/tc_act/tc_psp.h
 F:	include/uapi/linux/psp.h
+F:	include/uapi/linux/tc_act/tc_psp.h
 F:	net/psp/
+F:	net/sched/act_psp.c
 K:	struct\ psp(_assoc|_dev|hdr)\b
 
 PSTORE FILESYSTEM
include/net/tc_act/tc_psp.h new file +23 -0
new file mode 100644
index 000000000000..8640d7685475
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __NET_TC_PSP_H
+#define __NET_TC_PSP_H
+
+#include <net/act_api.h>
+#include <net/dst_metadata.h>
+#include <linux/tc_act/tc_psp.h>
+
+struct tcf_psp_params {
+	struct rcu_head		rcu;
+	int			action;
+	int			psp_action;
+	struct metadata_dst	*md;
+};
+
+struct tcf_psp {
+	struct tc_action		common;
+	struct tcf_psp_params __rcu	*params;
+};
+
+#define to_psp(a) ((struct tcf_psp *)a)
+
+#endif /* __NET_TC_PSP_H */
include/uapi/linux/pkt_cls.h +1 -0
index 28d94b11d1aa..14cff8f5da74 100644
@@ -139,6 +139,7 @@ enum tca_id {
 	TCA_ID_MPLS,
 	TCA_ID_CT,
 	TCA_ID_GATE,
+	TCA_ID_PSP,
 	/* other actions go here */
 	__TCA_ID_MAX = 255
 };
include/uapi/linux/tc_act/tc_psp.h new file +28 -0
new file mode 100644
index 000000000000..e58ebcae77be
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef __LINUX_TC_PSP_H
+#define __LINUX_TC_PSP_H
+
+#include <linux/pkt_cls.h>
+
+#define TCA_PSP_ACT_ENCAP	1	/* attach a PSP association */
+#define TCA_PSP_ACT_DECAP	2	/* drop PSP Rx state */
+
+struct tc_psp {
+	tc_gen;
+};
+
+enum {
+	TCA_PSP_UNSPEC,
+	TCA_PSP_TM,
+	TCA_PSP_PARMS,
+	TCA_PSP_PAD,
+	TCA_PSP_ACTION,		/* u32, TCA_PSP_ACT_* */
+	TCA_PSP_ASSOC_ID,	/* u32, encap only: Tx association ID from
+				 * the PSP netlink family (assoc-add)
+				 */
+	__TCA_PSP_MAX,
+};
+
+#define TCA_PSP_MAX (__TCA_PSP_MAX - 1)
+
+#endif
net/sched/Kconfig +13 -0
index 6ddff028b81a..35f9d76c4cae 100644
@@ -939,6 +939,19 @@ config NET_ACT_GATE
 	  To compile this code as a module, choose M here: the
 	  module will be called act_gate.
 
+config NET_ACT_PSP
+	tristate "PSP encryption tc action"
+	depends on NET_CLS_ACT && INET_PSP
+	help
+	  Say Y here to allow PSP associations, created through the PSP
+	  netlink family, to be attached to packets. PSP capable devices can
+	  then encrypt them on transmit. Enables transparent psp tunnels for
+	  forwarded traffic.
+
+	  If unsure, say N.
+	  To compile this code as a module, choose M here: the
+	  module will be called act_psp.
+
 config NET_IFE_SKBMARK
 	tristate "Support to encoding decoding skb mark on IFE action"
 	depends on NET_ACT_IFE
net/sched/Makefile +1 -0
index 5078ea84e6ad..9fe8cc0abfdf 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_NET_IFE_SKBTCINDEX)	+= act_meta_skbtcindex.o
 obj-$(CONFIG_NET_ACT_TUNNEL_KEY)+= act_tunnel_key.o
 obj-$(CONFIG_NET_ACT_CT)	+= act_ct.o
 obj-$(CONFIG_NET_ACT_GATE)	+= act_gate.o
+obj-$(CONFIG_NET_ACT_PSP)	+= act_psp.o
 obj-$(CONFIG_NET_SCH_FIFO)	+= sch_fifo.o
 obj-$(CONFIG_NET_SCH_HTB)	+= sch_htb.o
 obj-$(CONFIG_NET_SCH_HFSC)	+= sch_hfsc.o
net/sched/act_psp.c new file +439 -0

439 lines not on this page · raw message

Signature · 1 line
2.53.1-Meta
author
#7 lore raw

PATCH v2 7/12 net/sched: flower: add psp_spi, psp_ver, and psp_dev matches

In reply to Todd Freed

Add a flow dissector key filled from the PSP Rx state (SKB_EXT_PSP) the device attached to a decrypted packet, and corresponding flower matches:
the SPI the packet was decrypted with (psp_spi, with a mask), the PSP version (psp_ver), and the ID of the PSP device which decrypted it (psp_dev).

For example, to take the packets of one Rx SPI (allocated with rx-spi-alloc), and remove their PSP state so that they can be forwarded:

tc filter add dev eth0 ingress protocol ipv6 flower
psp_spi 0x80000001 psp_ver 0 psp_dev 1 action psp decap

Together these matches tie a rule to one Rx SPI, in a form which hardware can match on (offload comes in a follow-up series). The SPI is only unique on its PSP device, and is allocated for one PSP version. This matches the per-packet check that PSP sockets have against their association, with an important exception - the generation of the device key is not checked.

This means that a rule for an SPI could match packets after two device key rotations, if the device allocates the same SPI again (which it is free to do).

Userspace is responsible for handling this. It must replace a rule which matches on SPI before the device key has had two rotations. The existing key rotation notification may be used for this purpose.

No driver offloads these matches yet, and some drivers offload a rule without checking which keys it uses, so it would match without them. Don't offload a rule which uses them, and reject it with skip_sw. A rule which isn't offloaded is not on the filter's hw list either, so it isn't offloaded later when the block gets another device.

Note that these flower matches only ever match packets which were decrypted, even if the mask is 0. So "psp_spi 0/0" matches any decrypted PSP packet.

---
 Documentation/netlink/specs/tc.yaml | 14 ++++++
 include/linux/skbuff.h              |  4 ++
 include/net/flow_dissector.h        | 18 +++++++
 include/uapi/linux/pkt_cls.h        |  5 ++
 net/core/flow_dissector.c           | 27 +++++++++++
 net/sched/cls_flower.c              | 74 +++++++++++++++++++++++++++++
 6 files changed, 142 insertions(+)
Documentation/netlink/specs/tc.yaml +14 -0
index b6ccc645b0e3..044938316ffd 100644
@@ -2908,6 +2908,20 @@ attribute-sets:
         byte-order: big-endian
         enum: flower-key-ctrl-flags
         enum-as-flags: true
+      -
+        name: key-psp-spi
+        type: u32
+        byte-order: big-endian
+      -
+        name: key-psp-spi-mask
+        type: u32
+        byte-order: big-endian
+      -
+        name: key-psp-version
+        type: u8
+      -
+        name: key-psp-dev
+        type: u32
   -
     name: flower-key-enc-opts-attrs
     name-prefix: tca-flower-key-enc-opts-
include/linux/skbuff.h +4 -0
index 1d6832878930..4ea17cafa5df 100644
@@ -1660,6 +1660,10 @@ void skb_flow_dissect_hash(const struct sk_buff *skb,
 			   struct flow_dissector *flow_dissector,
 			   void *target_container);
 
+void skb_flow_dissect_psp(const struct sk_buff *skb,
+			  struct flow_dissector *flow_dissector,
+			  void *target_container);
+
 static inline __u32 skb_get_hash_net(const struct net *net, struct sk_buff *skb)
 {
 	if (!skb->l4_hash && !skb->sw_hash)
include/net/flow_dissector.h +18 -0
index ced79dc8e856..151ae03d5ad5 100644
@@ -339,6 +339,23 @@ struct flow_dissector_key_cfm {
 	u8	opcode;
 };
 
+/**
+ * struct flow_dissector_key_psp - PSP Rx state of a decrypted packet
+ * @spi: SPI of the PSP association the packet was decrypted with
+ * @dev_id: ID of the PSP device which decrypted the packet
+ * @version: PSP version (enum psp_version) the packet was decrypted with
+ * @present: packet was decrypted by a PSP device
+ *
+ * Taken from the PSP Rx state the device attached to the skb, not from the
+ * packet, which no longer carries the PSP header.
+ */
+struct flow_dissector_key_psp {
+	__be32	spi;
+	u16	dev_id;
+	u8	version;
+	u8	present;
+};
+
 #define FLOW_DIS_CFM_MDL_MASK GENMASK(7, 5)
 #define FLOW_DIS_CFM_MDL_MAX 7
 
@@ -376,6 +393,7 @@ enum flow_dissector_key_id {
 	FLOW_DISSECTOR_KEY_L2TPV3, /* struct flow_dissector_key_l2tpv3 */
 	FLOW_DISSECTOR_KEY_CFM, /* struct flow_dissector_key_cfm */
 	FLOW_DISSECTOR_KEY_IPSEC, /* struct flow_dissector_key_ipsec */
+	FLOW_DISSECTOR_KEY_PSP, /* struct flow_dissector_key_psp */
 
 	FLOW_DISSECTOR_KEY_MAX,
 };
include/uapi/linux/pkt_cls.h +5 -0
index 14cff8f5da74..0cfa7425404b 100644
@@ -561,6 +561,11 @@ enum {
 	TCA_FLOWER_KEY_ENC_FLAGS,	/* be32 */
 	TCA_FLOWER_KEY_ENC_FLAGS_MASK,	/* be32 */
 
+	TCA_FLOWER_KEY_PSP_SPI,		/* be32 */
+	TCA_FLOWER_KEY_PSP_SPI_MASK,	/* be32 */
+	TCA_FLOWER_KEY_PSP_VERSION,	/* u8, enum psp_version */
+	TCA_FLOWER_KEY_PSP_DEV,		/* u32, PSP device ID */
+
 	__TCA_FLOWER_MAX,
 };
 
net/core/flow_dissector.c +27 -0
index 1be6c739e3c7..95d3fa3b1568 100644
@@ -28,6 +28,7 @@
 #include <linux/ptp_classify.h>
 #include <net/flow_dissector.h>
 #include <net/pkt_cls.h>
+#include <net/psp/types.h>
 #include <scsi/fc/fc_fcoe.h>
 #include <uapi/linux/batadv_packet.h>
 #include <linux/bpf.h>
@@ -511,6 +512,32 @@ void skb_flow_dissect_hash(const struct sk_buff *skb,
 }
 EXPORT_SYMBOL(skb_flow_dissect_hash);
 
+void skb_flow_dissect_psp(const struct sk_buff *skb,
+			  struct flow_dissector *flow_dissector,
+			  void *target_container)
+{
+#if IS_ENABLED(CONFIG_INET_PSP)
+	struct flow_dissector_key_psp *key;
+	struct psp_skb_ext *pse;
+
+	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PSP))
+		return;
+
+	pse = skb_ext_find(skb, SKB_EXT_PSP);
+	if (!pse)
+		return;
+
+	key = skb_flow_dissector_target(flow_dissector,
+					FLOW_DISSECTOR_KEY_PSP,
+					target_container);
+	key->spi = pse->spi;
+	key->dev_id = pse->dev_id;
+	key->version = pse->version;
+	key->present = 1;
+#endif
+}
+EXPORT_SYMBOL(skb_flow_dissect_psp);
+
 static enum flow_dissect_ret
 __skb_flow_dissect_mpls(const struct sk_buff *skb,
 			struct flow_dissector *flow_dissector,
net/sched/cls_flower.c +74 -0

122 lines not on this page · raw message

Signature · 1 line
2.53.1-Meta
author
#8 lore raw

PATCH v2 8/12 selftests: drv-net: psp: test socket-less associations and the tc psp action

In reply to Todd Freed

Cover the socket-less PSP commands and the tc psp action.

The data path tests connect a plain local TCP socket to a PSP socket of the responder, and give the local side PSP with tc and socket-less PSP:
on ingress, flower psp_spi, psp_ver and psp_dev match what was decrypted with the local Rx SPI, and psp decap removes its PSP state; on egress, psp encap attaches the Tx association to the socket's packets, which the device encrypts.

Test cases:

assoc_sockless_basic rx-spi-alloc, and assoc-add / get / dump / del
of a Tx association, only the SPI is ever reported
assoc_sockless_rotations rx-spi-alloc reports the key rotations, Tx
associations are unaffected by them

  assoc_sockless_bad       bad device, key size, missing key
  assoc_sockless_tc        encap only takes a Tx association, decap none
  data_tc_basic_send       data to the responder and its ACKs back, for

each PSP version and IP version
data_tc_no_decap decrypted packets don't reach a plain socket
data_tc_assoc_del the Tx association deleted while encap uses
it, which holds it
data_tc_decap_match decap only takes the packets of the filter's
SPI, version and device
data_tc_encap_wrong_dev packets redirected to another device drop data_tc_encap_short_hdr an IPv4 packet shorter than its header
length is dropped by encap
data_tc_encap_too_big packets which won't fit the MTU once
encapsulated are dropped, with and without GSO
data_tc_encap_psp_sock packets of a PSP socket, which are marked
decrypted, are dropped by the psp encap action
data_tc_encap_ingress packets with an association aren't redirected
from egress to ingress
removal_device_tc PSP device removed while tc holds its Tx
association

data_tc_encap_ingress needs the mirred and bpf fixes of the net series this series depends on.

Note that the tc tests require a newer tc, which has support for the psp action and the flower psp matches. If tc is missing that support, the tc tests skip.

---
 tools/testing/selftests/drivers/net/config |   3 +
 tools/testing/selftests/drivers/net/psp.py | 572 ++++++++++++++++++++-
 2 files changed, 570 insertions(+), 5 deletions(-)
tools/testing/selftests/drivers/net/config +3 -0
index 4838adf27fa1..baf1c9d0dd13 100644
@@ -1,9 +1,12 @@
 CONFIG_CONFIGFS_FS=y
 CONFIG_DEBUG_INFO_BTF=y
 CONFIG_DEBUG_INFO_BTF_MODULES=n
+CONFIG_DUMMY=m
 CONFIG_INET_PSP=y
 CONFIG_IPV6=y
 CONFIG_MACSEC=m
+CONFIG_NET_ACT_MIRRED=m
+CONFIG_NET_ACT_PSP=m
 CONFIG_NET_ACT_SKBEDIT=m
 CONFIG_NET_CLS_ACT=y
 CONFIG_NET_CLS_BPF=y
tools/testing/selftests/drivers/net/psp.py +567 -5

605 lines not on this page · raw message

Signature · 1 line
2.53.1-Meta
author
#9 lore raw

PATCH v2 9/12 selftests: drv-net: psp: test that software encap on ingress drops

In reply to Todd Freed

Encap on ingress isn't supported: the action drops the packet.

Verify it on traffic of a client behind a veth, with encap on the veth's
ingress: forwarded, the packets must not reach the remote in clear text, and to a local address, they must not reach the local socket.

data_tc_encap_routed routed packets are dropped by encap, IPv4 and
IPv6
data_tc_encap_local so are packets to a local address

---
 tools/testing/selftests/drivers/net/config |   2 +
 tools/testing/selftests/drivers/net/psp.py | 159 ++++++++++++++++++++-
 2 files changed, 159 insertions(+), 2 deletions(-)
tools/testing/selftests/drivers/net/config +2 -0
index baf1c9d0dd13..b39de6e64263 100644
@@ -5,6 +5,7 @@ CONFIG_DUMMY=m
 CONFIG_INET_PSP=y
 CONFIG_IPV6=y
 CONFIG_MACSEC=m
+CONFIG_NET_ACT_GACT=m
 CONFIG_NET_ACT_MIRRED=m
 CONFIG_NET_ACT_PSP=m
 CONFIG_NET_ACT_SKBEDIT=m
@@ -25,5 +26,6 @@ CONFIG_NET_SCH_PRIO=m
 CONFIG_PPP=y
 CONFIG_PPPOE=y
 CONFIG_TLS=y
+CONFIG_VETH=m
 CONFIG_VLAN_8021Q=m
 CONFIG_XDP_SOCKETS=y
tools/testing/selftests/drivers/net/psp.py +157 -2

177 lines not on this page · raw message

Signature · 1 line
2.53.1-Meta
author
#10 lore raw

PATCH v2 10/12 selftests: tc-testing: run dependsOn probes outside the test namespace

In reply to Todd Freed

A test's dependsOn probe runs as an execute stage command, but before the test's namespace is created. nsPlugin prefixes the command with "ip netns exec tcut", which makes it fail. Every test with a probe that requires nsPlugin is therefore skipped currently, for example the mirred blockid tests and the tunnel_key nofrag test.

Probes are meant to check what tc and the kernel support, not anything in the test's namespace. Run them as their own stage ("probe"), which makes them run in the base namespace. Add the stage to the README, and say in AddingTestCases.txt that dependsOn runs in the base namespace.

Fixes 98cfbe4234a4 ("selftests/tc-testing: localize test resources")
---
 tools/testing/selftests/tc-testing/README                     | 1 +
 .../tc-testing/creating-testcases/AddingTestCases.txt         | 4 +++-
 tools/testing/selftests/tc-testing/tdc.py                     | 2 +-
 3 files changed, 5 insertions(+), 2 deletions(-)
tools/testing/selftests/tc-testing/README +1 -0
index fc8e858ff119..430ac0bf68ab 100644
@@ -168,6 +168,7 @@ of the command.
 The stages are identified by the following strings:
 
   - pre  (pre-suite)
+  - probe (dependsOn, before setup)
   - setup
   - command
   - verify
tools/testing/selftests/tc-testing/creating-testcases/AddingTestCases.txt +3 -1
index ff956d8c99c5..c61f2e2020e0 100644
@@ -39,7 +39,9 @@ skip:         A completely optional key, if the corresponding value is "yes"
               marked as skipped. This key can be placed anywhere inside the
               test case at the top level.
 dependsOn:    Same as 'skip', but the value is executed as a command. The test
-              is skipped when the command returns non-zero.
+              is skipped when the command returns non-zero. The command runs
+              in the base namespace, before setup, so it can't use the test's
+              devices.
 category:     A list of single-word descriptions covering what the command
               under test is testing. Example: filter, actions, u32, gact, etc.
 setup:        The list of commands required to ensure the command under test
tools/testing/selftests/tc-testing/tdc.py +1 -1
index 511d66c36a2a..541caf984519 100755
@@ -399,7 +399,7 @@ def run_one_test(pm, args, index, tidx):
     if 'dependsOn' in tidx:
         if (args.verbose > 0):
             print('probe command for test skip')
-        (p, procout) = exec_cmd(tidx, args, pm, 'execute', tidx['dependsOn'])
+        (p, procout) = exec_cmd(tidx, args, pm, 'probe', tidx['dependsOn'])
         if p:
             if (p.returncode != 0):
                 res = TestResult(tidx['id'], tidx['name'])
Signature · 1 line
2.53.1-Meta
author
#11 lore raw

PATCH v2 11/12 selftests: tc-testing: add tests for the psp action

In reply to Todd Freed

Add / replace / delete / flush psp decap actions, check that associations which don't exist are rejected, and add flower filters matching on psp_spi, psp_ver and psp_dev, out of range psp_ver and psp_dev rejected.

The tests of rejected actions match the kernel's extack message, so that they don't pass on tc's own argument checks. tc itself refuses decap with an association, and encap without one, so those kernel checks aren't covered here.

Creating associations takes the PSP netlink family and a PSP device; the drv-net psp.py test covers actions using them.

The tests need a tc with the psp action and the flower psp_* keys; they skip without it (dependsOn).

---
 tools/testing/selftests/tc-testing/config     |   2 +
 .../tc-testing/tc-tests/actions/psp.json      | 403 ++++++++++++++++++
 tools/testing/selftests/tc-testing/tdc.sh     |   1 +
 3 files changed, 406 insertions(+)
 create mode 100644 tools/testing/selftests/tc-testing/tc-tests/actions/psp.json
tools/testing/selftests/tc-testing/config +2 -0
index 0e5618be0335..7828161cc3c8 100644
@@ -101,6 +101,8 @@ CONFIG_NET_ACT_TUNNEL_KEY=m
 CONFIG_NET_ACT_CT=m
 CONFIG_NET_ACT_MPLS=m
 CONFIG_NET_ACT_GATE=m
+CONFIG_INET_PSP=y
+CONFIG_NET_ACT_PSP=m
 CONFIG_NET_IFE_SKBMARK=m
 CONFIG_NET_IFE_SKBPRIO=m
 CONFIG_NET_IFE_SKBTCINDEX=m
tools/testing/selftests/tc-testing/tc-tests/actions/psp.json new file +403 -0

403 lines not on this page · raw message

tools/testing/selftests/tc-testing/tdc.sh +1 -0
index 25cdc256287d..433af92364ac 100755
@@ -24,6 +24,7 @@ try_modprobe act_mpls
 try_modprobe act_nat
 try_modprobe act_pedit
 try_modprobe act_police
+try_modprobe act_psp
 try_modprobe act_sample
 try_modprobe act_simple
 try_modprobe act_skbedit
Signature · 1 line
2.53.1-Meta
author
#12 lore raw

PATCH v2 12/12 Documentation: networking: psp: document socket-less associations

In reply to Todd Freed

Describe PSP associations, and how they differ for the socket and tc use cases.

Document the new netlink commands for socket-less PSP, and what userspace must do on key rotation, since the kernel keeps no state for socket-less Rx SPIs.

Document the psp tc action, and the flower psp_spi, psp_ver, and psp_dev matches.

Add a new section to describe pre-existing PSP device stats.

---
 Documentation/networking/psp.rst | 169 ++++++++++++++++++++++++++++---
 1 file changed, 153 insertions(+), 16 deletions(-)
Documentation/networking/psp.rst +153 -16
index 0f9b6b73f244..32d53f6050b7 100644
@@ -69,20 +69,43 @@ The PSP netlink family defines operations to retrieve information
 about the PSP devices available on the system, configure them and
 access PSP related statistics.
 
-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.
+PSP Associations
+----------------
 
-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 a secondary
-effect on the Rx direction.
+PSP encryption and decryption happen in the context of an association.
+An association holds the PSP device, PSP version, and either Rx state
+for decrypting and authenticating packets (the SPI and Rx key), a Tx key
+for encrypting packets, or both.
+
+Associations are used in two ways. First, a TCP socket can be bound to
+an association, and all packets flowing over that socket inherit its
+association. Second, socket-less Tx associations are used with tc,
+together with Rx SPIs allocated without a socket. This allows PSP for
+forwarded traffic, or for non-TCP traffic. A tc action attaches a Tx
+association to packets. On Rx, the device decrypts packets sent to an
+SPI it allocated, and a tc filter matches the SPI, version and PSP
+device a packet was decrypted with. The kernel keeps no state for
+socket-less Rx.
+
+An association is of one type or the other; that is, sockets may not
+use socket-less associations, and tc actions do not address associations
+which are attached to a socket. Device statistics count both.
+
+Securing a socket connection
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+PSP encryption can be enabled for TCP sockets. Other kinds of sockets
+are not supported. For a TCP socket, 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, create an association with it, and bind it to the given
+socket. At this stage socket will accept both PSP-secured and plain text
+TCP packets.
+
+The Tx key is installed into the socket's association with the
+``tx-assoc`` Netlink command. Once it is installed, all data read from
+the socket will be PSP-secured. In other words act of installing Tx keys
+has a secondary effect on the Rx direction.
 
 There is an intermediate period after ``tx-assoc`` successfully
 returns and before the TCP socket encounters it's first PSP
@@ -140,6 +163,83 @@ The PSP assoc state of a socket is not reset when the connection is
 torn down. ``connect()`` on a socket that has PSP assoc state will
 return ``-EINVAL``.
 
+Socket-less associations
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+Socket-less PSP is managed with these netlink commands:
+
+- ``rx-spi-alloc`` allocates an Rx SPI and key, like ``rx-assoc`` but
+  without a socket, and creates no association. It also returns
+  ``key-rotations``, the number of key rotations of the PSP device so
+  far. The key should be passed to the peer out of band, and installed
+  with ``assoc-add``.
+- ``assoc-add`` adds a Tx association with the peer's SPI and key, and
+  returns its ID.
+- ``assoc-del`` deletes a Tx association. tc actions and packets which
+  still reference it keep it alive.
+- ``assoc-get`` reports Tx associations, never their keys.
+
+Tx associations are removed when their PSP device is unregistered.
+
+The ``psp`` tc action attaches these associations to packets:
+
+- ``psp encap assoc ID`` attaches a Tx association. The packet must then
+  be transmitted on the main netdev of the association's PSP device, which
+  encrypts it (``mirred egress redirect``). If the packet loses the
+  association on the way (e.g. its dst is replaced), is transmitted on any
+  other device, or the PSP device is unregistered before transmit, it will
+  be dropped, rather than sent in the clear. Encap on ingress isn't
+  supported: the action drops the packet.
+
+- ``psp decap`` removes the PSP Rx state (the ``SKB_EXT_PSP`` extension and
+  ``skb->decrypted``) of a decrypted packet, so that it can be forwarded. It
+  takes no association. Its filter should match ``psp_spi``, ``psp_ver``
+  and ``psp_dev``.
+
+The flower classifier matches what a packet was decrypted with:
+``psp_spi SPI[/MASK]``, ``psp_ver VERSION`` and ``psp_dev DEV_ID``, the
+ID of the PSP device which decrypted it. These never match a clear text
+packet, so ``psp_spi 0/0`` matches any packet decrypted with PSP.
+Together they tie a decap rule to one Rx SPI.
+
+The top bit of an SPI, the key phase, selects which of the two device
+keys its Rx key is derived from. A key rotation replaces the device key
+of the other phase than the SPIs allocated since the previous rotation,
+and the SPIs allocated after it have that phase. An SPI keeps decrypting
+through one rotation, which leaves time to move peers to new SPIs. After
+the second rotation its device key is gone, and the device may allocate
+the same SPI again, with a new key.
+
+Unlike for sockets, the kernel does not enforce key rotation for
+socket-less Rx: a decap rule for an SPI also takes the packets of a later
+allocation of the same SPI. Userspace which installs decap rules is
+responsible for removing them in time:
+
+- Any process with ``CAP_NET_ADMIN`` over the PSP device can rotate its
+  key, or allocate SPIs. Once the device has rotated twice,
+  ``rx-spi-alloc`` can return an SPI which a stale decap rule still
+  matches. Remove the decap rules of the SPIs allocated before the
+  previous rotation, which have the phase of the SPIs allocated from
+  now on, on a rotation notification (``key-rotate-ntf``), and in any
+  case before an SPI allocated after the rotation is given to a peer.
+  This is simplest when one process allocates the SPIs and owns the
+  decap rules. Then move peers to new SPIs before the next rotation.
+- An SPI allocated when ``key-rotations`` was N stops decrypting with
+  rotation N + 2. Comparing it with the ``key-rotations`` of a later
+  ``rx-spi-alloc``, or of ``get-stats``, detects missed notifications.
+
+tc can help with this bookkeeping. A tc action cookie (``cookie``, up to
+16 bytes) is opaque to the kernel and dumped with the action, so it can
+record the owner of a decap rule, its SPI and its ``key-rotations``.
+Filters added with a known priority and handle can be deleted without a
+dump. Putting the decap rules of each key phase in their own chain lets
+one command remove all of them on rotation.
+
+The device adds 40 bytes to each packet on transmit: the UDP and PSP
+headers, and the ICV. The operator must size the MTU of the traffic
+source for this. A packet which would not fit the PSP device's MTU once
+encapsulated is dropped on transmit.
+
 Rotation notifications
 ----------------------
 
@@ -149,6 +249,39 @@ 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.
 
+Fragments
+---------
+
+PSP transport mode does not support IP fragments, either for sockets or
+socket-less associations.
+
+Statistics
+----------
+
+The ``get-stats`` netlink command reports the PSP device statistics:
+kernel statistics (key rotations, stale events), and the device statistics
+defined by the PSP specification. The device statistics count what the
+device decrypts and encrypts.
+
+The tc action counts the packets it processes and the ones it drops
+(``tc -s``). Encapsulated packets dropped on transmit count in the
+device's ``tx_dropped``. Drops are reported with these drop reasons:
+
+- ``SKB_DROP_REASON_PSP_OUTPUT``: encap ran on ingress in software, or
+  failed to parse the packet, or it is an IPv4 fragment or has IPv6
+  extension headers, which transport mode doesn't support, or it is
+  already encapsulated for a tunnel, already marked decrypted, or of a
+  socket with its own encryption offload; or an encapsulated packet
+  reached a device which isn't its PSP device's netdev, or its PSP
+  device was unregistered while it was queued.
+- ``SKB_DROP_REASON_PKT_TOO_BIG``: an encapsulated packet would not fit
+  the device's MTU once the device adds the PSP encapsulation.
+
+A packet encapsulated on egress which lost its association on the way is
+dropped on transmit by ``sk_validate_xmit_skb()``, without a specific drop
+reason, unless its socket has an encryption offload of its own: then the
+socket's validator handles it.
+
 Kernel implementation
 =====================
 
@@ -177,14 +310,18 @@ device key twice), to avoid potentially using a SPI+key which previous
 OS instance already had access to.
 
 Drivers must use ``psp_skb_get_assoc_rcu()`` to check if PSP Tx offload
-was requested for given skb. On Rx drivers should allocate and populate
-the ``SKB_EXT_PSP`` skb extension, and set the skb->decrypted bit to 1.
+was requested for given skb. The association comes from the skb's socket
+or, for socket-less associations, from a ``METADATA_PSP`` dst which the
+core keeps on the skb for the driver to use. On Rx drivers should
+allocate and populate the ``SKB_EXT_PSP`` skb extension, and set the
+``skb->decrypted`` bit to 1.
 
 Kernel implementation notes
 ---------------------------
 
 PSP implementation follows the TLS offload more closely than the IPsec
-offload, with per-socket state, and the use of skb->decrypted to prevent
+offload, with per-socket state (or per-association state, for
+socket-less associations), and the use of ``skb->decrypted`` to prevent
 clear text leaks.
 
 PSP device is separate from netdev, to make it possible to "delegate"
Signature · 1 line
2.53.1-Meta