Skip to contentSkip to conversation

xfrm: add SA information to the offloaded packet

PATCH In review Feng Wang 3 participants 9 messages Nov 4, 2024 – Nov 8, 2024

Subsystems (4)
lore
Share & tools
lore

Feng Wang

author
#0 lore raw

PATCH 1/2 xfrm: add SA information to the offloaded packet

From: wangfe <[email protected]>

In packet offload mode, append Security Association (SA) information to each packet, replicating the crypto offload implementation. The XFRM_XMIT flag is set to enable packet to be returned immediately from the validate_xmit_xfrm function, thus aligning with the existing code path for packet offload mode.

This SA info helps HW offload match packets to their correct security policies. The XFRM interface ID is included, which is used in setups with multiple XFRM interfaces where source/destination addresses alone can't pinpoint the right policy.

Enable packet offload mode on netdevsim and add code to check the XFRM interface ID.

---
v3: https://lore.kernel.org/all/[email protected]/
- Add XFRM interface ID checking on netdevsim in the packet offload mode.
v2:
- Add why HW offload requires the SA info to the commit message
v1: https://lore.kernel.org/all/[email protected]/
---

---
 drivers/net/netdevsim/ipsec.c     | 24 +++++++++++++++++++++++-
 drivers/net/netdevsim/netdevsim.h |  1 +
 net/xfrm/xfrm_output.c            | 21 +++++++++++++++++++++
 3 files changed, 45 insertions(+), 1 deletion(-)
drivers/net/netdevsim/ipsec.c +23 -1
index f0d58092e7e9..1ce7447dd269 100644
@@ -149,7 +149,8 @@ static int nsim_ipsec_add_sa(struct xfrm_state *xs,
 		return -EINVAL;
 	}
 
-	if (xs->xso.type != XFRM_DEV_OFFLOAD_CRYPTO) {
+	if (xs->xso.type != XFRM_DEV_OFFLOAD_CRYPTO &&
+	    xs->xso.type != XFRM_DEV_OFFLOAD_PACKET) {
 		NL_SET_ERR_MSG_MOD(extack, "Unsupported ipsec offload type");
 		return -EINVAL;
 	}
@@ -165,6 +166,7 @@ static int nsim_ipsec_add_sa(struct xfrm_state *xs,
 	memset(&sa, 0, sizeof(sa));
 	sa.used = true;
 	sa.xs = xs;
+	sa.if_id = xs->if_id;
 
 	if (sa.xs->id.proto & IPPROTO_ESP)
 		sa.crypt = xs->ealg || xs->aead;
@@ -224,10 +226,24 @@ static bool nsim_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
 	return true;
 }
 
+static int nsim_ipsec_add_policy(struct xfrm_policy *policy,
+				 struct netlink_ext_ack *extack)
+{
+	return 0;
+}
+
+static void nsim_ipsec_del_policy(struct xfrm_policy *policy)
+{
+}
+
 static const struct xfrmdev_ops nsim_xfrmdev_ops = {
 	.xdo_dev_state_add	= nsim_ipsec_add_sa,
 	.xdo_dev_state_delete	= nsim_ipsec_del_sa,
 	.xdo_dev_offload_ok	= nsim_ipsec_offload_ok,
+
+	.xdo_dev_policy_add     = nsim_ipsec_add_policy,
+	.xdo_dev_policy_delete  = nsim_ipsec_del_policy,
+
 };
 
 bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
@@ -272,6 +288,12 @@ bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
 		return false;
 	}
 
+	if (xs->if_id != tsa->if_id) {
+		netdev_err(ns->netdev, "unmatched if_id %d %d\n",
+			   xs->if_id, tsa->if_id);
+		return false;
+	}
+
 	ipsec->tx++;
 
 	return true;
drivers/net/netdevsim/netdevsim.h +1 -0
index bf02efa10956..4941b6e46d0a 100644
@@ -41,6 +41,7 @@ struct nsim_sa {
 	__be32 ipaddr[4];
 	u32 key[4];
 	u32 salt;
+	u32 if_id;
 	bool used;
 	bool crypt;
 	bool rx;
net/xfrm/xfrm_output.c +21 -0
index e5722c95b8bb..a12588e7b060 100644
@@ -706,6 +706,8 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb)
 	struct xfrm_state *x = skb_dst(skb)->xfrm;
 	int family;
 	int err;
+	struct xfrm_offload *xo;
+	struct sec_path *sp;
 
 	family = (x->xso.type != XFRM_DEV_OFFLOAD_PACKET) ? x->outer_mode.family
 		: skb_dst(skb)->ops->family;
@@ -728,6 +730,25 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb)
 			kfree_skb(skb);
 			return -EHOSTUNREACH;
 		}
+		sp = secpath_set(skb);
+		if (!sp) {
+			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
+			kfree_skb(skb);
+			return -ENOMEM;
+		}
+
+		sp->olen++;
+		sp->xvec[sp->len++] = x;
+		xfrm_state_hold(x);
+
+		xo = xfrm_offload(skb);
+		if (!xo) {
+			secpath_reset(skb);
+			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
+			kfree_skb(skb);
+			return -EINVAL;
+		}
+		xo->flags |= XFRM_XMIT;
 
 		return xfrm_output_resume(sk, skb, 0);
 	}
Signature · 1 line
2.47.0.199.ga7371fff76-goog
#1 lore raw
Quoted 13 lines · Feng Wang

From: wangfe <[email protected]>

In packet offload mode, append Security Association (SA) information to each packet, replicating the crypto offload implementation. The XFRM_XMIT flag is set to enable packet to be returned immediately from the validate_xmit_xfrm function, thus aligning with the existing code path for packet offload mode.

This SA info helps HW offload match packets to their correct security policies. The XFRM interface ID is included, which is used in setups with multiple XFRM interfaces where source/destination addresses alone can't pinpoint the right policy.

Quoted 4 lines

Enable packet offload mode on netdevsim and add code to check the XFRM interface ID.

Something wrong with your git setup, lore shows only one patch.
https://lore.kernel.org/all/[email protected]/

Quoted 8 lines

---
v3: https://lore.kernel.org/all/[email protected]/
- Add XFRM interface ID checking on netdevsim in the packet offload mode.
v2:
- Add why HW offload requires the SA info to the commit message
v1: https://lore.kernel.org/all/[email protected]/
---

Quoted 5 lines
---
 drivers/net/netdevsim/ipsec.c     | 24 +++++++++++++++++++++++-
 drivers/net/netdevsim/netdevsim.h |  1 +
 net/xfrm/xfrm_output.c            | 21 +++++++++++++++++++++
 3 files changed, 45 insertions(+), 1 deletion(-)

Don't you need to update current drivers who support packet offload with if(x->if_id) check in their SA/policy validator to return an error to the user? They don't support that flow.

Quoted 49 lines
drivers/net/netdevsim/ipsec.c +17 -1
index f0d58092e7e9..1ce7447dd269 100644
@@ -149,7 +149,8 @@ static int nsim_ipsec_add_sa(struct xfrm_state *xs,
 		return -EINVAL;
 	}
 
-	if (xs->xso.type != XFRM_DEV_OFFLOAD_CRYPTO) {
+	if (xs->xso.type != XFRM_DEV_OFFLOAD_CRYPTO &&
+	    xs->xso.type != XFRM_DEV_OFFLOAD_PACKET) {
 		NL_SET_ERR_MSG_MOD(extack, "Unsupported ipsec offload type");
 		return -EINVAL;
 	}
@@ -165,6 +166,7 @@ static int nsim_ipsec_add_sa(struct xfrm_state *xs,
 	memset(&sa, 0, sizeof(sa));
 	sa.used = true;
 	sa.xs = xs;
+	sa.if_id = xs->if_id;
 
 	if (sa.xs->id.proto & IPPROTO_ESP)
 		sa.crypt = xs->ealg || xs->aead;
@@ -224,10 +226,24 @@ static bool nsim_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
 	return true;
 }
 
+static int nsim_ipsec_add_policy(struct xfrm_policy *policy,
+				 struct netlink_ext_ack *extack)
+{
+	return 0;
+}
+
+static void nsim_ipsec_del_policy(struct xfrm_policy *policy)
+{
+}
+
 static const struct xfrmdev_ops nsim_xfrmdev_ops = {
 	.xdo_dev_state_add	= nsim_ipsec_add_sa,
 	.xdo_dev_state_delete	= nsim_ipsec_del_sa,
 	.xdo_dev_offload_ok	= nsim_ipsec_offload_ok,
+
+	.xdo_dev_policy_add     = nsim_ipsec_add_policy,
+	.xdo_dev_policy_delete  = nsim_ipsec_del_policy,
+
 };
 
 bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)

drivers/net/netdevsim/ipsec.c bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb) · line 291

Quoted 4 lines
drivers/net/netdevsim/ipsec.c +1 -0
@@ -272,6 +288,12 @@ bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
 		return false;
 	}
 
+	if (xs->if_id != tsa->if_id) {

if_id exists in policy and state, but you are checking only state.

drivers/net/netdevsim/ipsec.c bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb) · line 295

Quoted 4 lines
drivers/net/netdevsim/ipsec.c +4 -0
+		netdev_err(ns->netdev, "unmatched if_id %d %d\n",
+			   xs->if_id, tsa->if_id);
+		return false;
+	}

It is worth to add check for having XFRM_XMIT flag here.

Quoted 30 lines
drivers/net/netdevsim/ipsec.c +1 -0
+
 	ipsec->tx++;
 
 	return true;
drivers/net/netdevsim/netdevsim.h +1 -0
index bf02efa10956..4941b6e46d0a 100644
@@ -41,6 +41,7 @@ struct nsim_sa {
 	__be32 ipaddr[4];
 	u32 key[4];
 	u32 salt;
+	u32 if_id;
 	bool used;
 	bool crypt;
 	bool rx;
net/xfrm/xfrm_output.c +2 -0
index e5722c95b8bb..a12588e7b060 100644
@@ -706,6 +706,8 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb)
 	struct xfrm_state *x = skb_dst(skb)->xfrm;
 	int family;
 	int err;
+	struct xfrm_offload *xo;
+	struct sec_path *sp;
 
 	family = (x->xso.type != XFRM_DEV_OFFLOAD_PACKET) ? x->outer_mode.family
 		: skb_dst(skb)->ops->family;

net/xfrm/xfrm_output.c int xfrm_output(struct sock *sk, struct sk_buff *skb) · line 732

Quoted 3 lines
net/xfrm/xfrm_output.c +0 -0
@@ -728,6 +730,25 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb)
 			kfree_skb(skb);
 			return -EHOSTUNREACH;
 		}

I think that code below should be set under "if (x->if_id)".

Thanks

Quoted 26 lines
net/xfrm/xfrm_output.c +19 -0
+		sp = secpath_set(skb);
+		if (!sp) {
+			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
+			kfree_skb(skb);
+			return -ENOMEM;
+		}
+
+		sp->olen++;
+		sp->xvec[sp->len++] = x;
+		xfrm_state_hold(x);
+
+		xo = xfrm_offload(skb);
+		if (!xo) {
+			secpath_reset(skb);
+			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
+			kfree_skb(skb);
+			return -EINVAL;
+		}
+		xo->flags |= XFRM_XMIT;
 
 		return xfrm_output_resume(sk, skb, 0);
 	}
Signature · 1 line
2.47.0.199.ga7371fff76-goog
author
#2 lore raw

Hi Leon,
I checked the current tree and there are no drivers who support packet offload. Even for the mlx5 driver, it only supports crypto offload mode. If I miss anything, please let me know.
Since the driver only requires the Security Association (SA) to perform the necessary transformations, policy information is not needed. Storing policy information, matching the policy and checking the if_id within the driver wouldn't provide much benefit. It would increase CPU and memory usage without a clear advantage. For all other suggestions, I totally agree with you.

Thanks,
Feng

#3 lore raw
Quoted 4 lines · Feng Wang

Hi Leon,
I checked the current tree and there are no drivers who support packet offload. Even for the mlx5 driver, it only supports crypto offload mode.

I don't know what to add here. We already had this discussion for more than once.
https://lore.kernel.org/all/[email protected]/
Let's me cite Steffen:

"There are 'packet offload drivers' in the kernel, that's why we support this kind of offload."

Quoted 5 lines

If I miss anything, please let me know.
Since the driver only requires the Security Association (SA) to perform the necessary transformations, policy information is not needed. Storing policy information, matching the policy and checking the if_id within the driver wouldn't provide much benefit.

You need to make sure that policy and SA has match in their selectors, and IMHO you can't add support to SA without adding same support to policy.

Quoted 5 lines

It would increase CPU and memory usage without a clear advantage. For all other suggestions, I totally agree with you.

Thanks,
Feng

#4 lore raw
Quoted 21 lines · Feng Wang
Quoted 4 lines

Hi Leon,
I checked the current tree and there are no drivers who support packet offload. Even for the mlx5 driver, it only supports crypto offload mode.

I don't know what to add here. We already had this discussion for more than once.
https://lore.kernel.org/all/[email protected]/
Let's me cite Steffen:

"There are 'packet offload drivers' in the kernel, that's why we support this kind of offload."

Quoted 5 lines

If I miss anything, please let me know.
Since the driver only requires the Security Association (SA) to perform the necessary transformations, policy information is not needed. Storing policy information, matching the policy and checking the if_id within the driver wouldn't provide much benefit.

Quoted 3 lines

You need to make sure that policy and SA has match in their selectors, and IMHO you can't add support to SA without adding same support to policy.

when a packet enters an XFRMi, xfrm_lookup_with_ifid() is called? What does that call return, incase of packet offload?

My guess is that call is returning NULL and that is why Feng is trying hard code state here?

I imagine the policy may not match because that policy has offload packet? May be this look up need to handle packet offload?

Quoted 6 lines
Quoted 6 lines

It would increase CPU and memory usage without a clear advantage. For all other suggestions, I totally agree with you.

Thanks,
Feng

author
#5 lore raw

Antony brought out an important function xfrm_lookup_with_ifid(), this function returns the next dst_entry.

The xfrm_lookup_with_ifid() function utilizes xfrm_sk_policy_lookup() to find a matching policy based on the given if_id. The if_id checking is handled in it.
Once the policy is found, xfrm_resolve_and_create_bundle() determines the correct Security Association (SA) and associates it with the destination entry (dst->xfrm).
This SA information is then passed directly to the driver. Since the kernel has already performed the necessary if_id checks for policy, there's no need for the driver to duplicate this effort.

#6 lore raw
Quoted 4 lines · Feng Wang

Antony brought out an important function xfrm_lookup_with_ifid(), this function returns the next dst_entry.

The xfrm_lookup_with_ifid() function utilizes xfrm_sk_policy_lookup()

would the output packet, looked up using xfrm_lookup_with_ifid , match xfrm policy with "offload packet" set?
When lookup is in the xfrm device.

ip xfrm policy .... offload packet dev <if-name>

Quoted 5 lines

to find a matching policy based on the given if_id. The if_id checking is handled in it.
Once the policy is found, xfrm_resolve_and_create_bundle() determines the correct Security Association (SA) and associates it with the destination entry (dst->xfrm).

If the output packet got this far, dst is set in skb? And when the packet reach the driver dst = skb_dst(skb); dst->xfrm is the state?
If this is the case why add state to skb as your patch proose? May be I am missing something in the packet path.

Quoted 3 lines

This SA information is then passed directly to the driver. Since the kernel has already performed the necessary if_id checks for policy, there's no need for the driver to duplicate this effort.

Is this how packet offload would work? My guess was in packet offload policy look happens in the driver.

#7 lore raw
Quoted 8 lines · Antony Antony
Quoted 4 lines · Feng Wang

Antony brought out an important function xfrm_lookup_with_ifid(), this function returns the next dst_entry.

The xfrm_lookup_with_ifid() function utilizes xfrm_sk_policy_lookup()

would the output packet, looked up using xfrm_lookup_with_ifid , match xfrm policy with "offload packet" set?

According to my understanding, no.

Quoted 17 lines

When lookup is in the xfrm device.

ip xfrm policy .... offload packet dev <if-name>

Quoted 5 lines

to find a matching policy based on the given if_id. The if_id checking is handled in it.
Once the policy is found, xfrm_resolve_and_create_bundle() determines the correct Security Association (SA) and associates it with the destination entry (dst->xfrm).

If the output packet got this far, dst is set in skb? And when the packet reach the driver dst = skb_dst(skb); dst->xfrm is the state?
If this is the case why add state to skb as your patch proose? May be I am missing something in the packet path.

Quoted 6 lines
Quoted 3 lines

This SA information is then passed directly to the driver. Since the kernel has already performed the necessary if_id checks for policy, there's no need for the driver to duplicate this effort.

Is this how packet offload would work? My guess was in packet offload policy look happens in the driver.

You are right, this is not how packet offload works. The expectation is that HW sees and catches same selectors as SW. It ensures that if SW finds policy/SA, HW will find the same.

Thanks

author
#8 lore raw

Based on current kernel implementation, the packet offload does match policy and state on the TX side in the kernel. For the RX side, the driver is responsible for the selector policy and SA match etc. I added some logs in the kernel, and listed the kernel function call flow as below for your reference, this is for TX side only.

(1) ip_output -> neighbor_output -> dev_queue_xmit ->
netdev_start_xmit -> ops->ndo_start_xmit -> xfrmi_xmit -> xfrmi_xmit2
(2) In the xfrmi_xmit2 { first call xfrm_lookup_with_ifid() to get
next destination(dst), then call dst_output() to send the packet, the dst_output in fact is xfrm_output() }
(3) To trace xfrm_lookup_with_ifid() xfrm_lookup_with_ifid() ->
xfrm_bundle_lookup() ->
{ first call xfrm_policy_lookup() to find the policy, the
policy id is checked here.
Then call xfrm_resolve_and_create_bundle() to find the
SA and generate the next dst

                return dst;
        }

(4)To trace xfrm_output(), now we know the next dst, calls xfrm_output to send the packet. xfrm_output() -> xfrm_output_resume()
->
{
first call xfrm_output_one() which goes to resume part
directly without packet transformation(this is special handling for packet offload) but it calls skb_dst_pop to get final dst. This dst has no xfrm(SA) so the driver can't access xfrm(SA) using skb_dst.
Then calls dst_output() with final dst, this will route
the packet to stack again, going through ip_output() etc to pass the packet to the driver transmit function, and send out.

         }

I did make a mistake on calling xfrm_sk_policy_lookup in the xfrm_lookup_with_ifid function. In fact, it is NOT called.

Thanks,
Feng