]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
xfrm: delay initialization of offload path till its actually requested
authorLeon Romanovsky <leonro@nvidia.com>
Wed, 19 Feb 2025 13:50:57 +0000 (15:50 +0200)
committerSteffen Klassert <steffen.klassert@secunet.com>
Fri, 21 Feb 2025 07:08:14 +0000 (08:08 +0100)
XFRM offload path is probed even if offload isn't needed at all. Let's
make sure that x->type_offload pointer stays NULL for such path to
reduce ambiguity.

Fixes: 9d389d7f84bb ("xfrm: Add a xfrm type offload.")
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
include/net/xfrm.h
net/xfrm/xfrm_device.c
net/xfrm/xfrm_state.c
net/xfrm/xfrm_user.c

index ed4b83696c77f9b296caffbc8504591b7ec0c296..e1eed5d47d072592d6cf106b6eaa37247c0fbfd5 100644 (file)
@@ -464,6 +464,15 @@ struct xfrm_type_offload {
 
 int xfrm_register_type_offload(const struct xfrm_type_offload *type, unsigned short family);
 void xfrm_unregister_type_offload(const struct xfrm_type_offload *type, unsigned short family);
+void xfrm_set_type_offload(struct xfrm_state *x);
+static inline void xfrm_unset_type_offload(struct xfrm_state *x)
+{
+       if (!x->type_offload)
+               return;
+
+       module_put(x->type_offload->owner);
+       x->type_offload = NULL;
+}
 
 /**
  * struct xfrm_mode_cbs - XFRM mode callbacks
@@ -1760,7 +1769,7 @@ void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si);
 u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq);
 int xfrm_init_replay(struct xfrm_state *x, struct netlink_ext_ack *extack);
 u32 xfrm_state_mtu(struct xfrm_state *x, int mtu);
-int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload,
+int __xfrm_init_state(struct xfrm_state *x, bool init_replay,
                      struct netlink_ext_ack *extack);
 int xfrm_init_state(struct xfrm_state *x);
 int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type);
index d1fa94e52ceae4bf24b792fec4ad10b30c4420b6..97c8030cc41733fa5460ae22721a580df69d6ff0 100644 (file)
@@ -244,11 +244,6 @@ int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
        xfrm_address_t *daddr;
        bool is_packet_offload;
 
-       if (!x->type_offload) {
-               NL_SET_ERR_MSG(extack, "Type doesn't support offload");
-               return -EINVAL;
-       }
-
        if (xuo->flags &
            ~(XFRM_OFFLOAD_IPV6 | XFRM_OFFLOAD_INBOUND | XFRM_OFFLOAD_PACKET)) {
                NL_SET_ERR_MSG(extack, "Unrecognized flags in offload request");
@@ -310,6 +305,13 @@ int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
                return -EINVAL;
        }
 
+       xfrm_set_type_offload(x);
+       if (!x->type_offload) {
+               NL_SET_ERR_MSG(extack, "Type doesn't support offload");
+               dev_put(dev);
+               return -EINVAL;
+       }
+
        xso->dev = dev;
        netdev_tracker_alloc(dev, &xso->dev_tracker, GFP_ATOMIC);
        xso->real_dev = dev;
@@ -332,6 +334,7 @@ int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
                netdev_put(dev, &xso->dev_tracker);
                xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
 
+               xfrm_unset_type_offload(x);
                /* User explicitly requested packet offload mode and configured
                 * policy in addition to the XFRM state. So be civil to users,
                 * and return an error instead of taking fallback path.
index ad2202fa82f34dcc021514e7c3ade4d68ac0d1c8..69af5964c886c9e7e2d37ce288a069ffc5e6a8c7 100644 (file)
@@ -424,18 +424,18 @@ void xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
 }
 EXPORT_SYMBOL(xfrm_unregister_type_offload);
 
-static const struct xfrm_type_offload *
-xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
+void xfrm_set_type_offload(struct xfrm_state *x)
 {
        const struct xfrm_type_offload *type = NULL;
        struct xfrm_state_afinfo *afinfo;
+       bool try_load = true;
 
 retry:
-       afinfo = xfrm_state_get_afinfo(family);
+       afinfo = xfrm_state_get_afinfo(x->props.family);
        if (unlikely(afinfo == NULL))
-               return NULL;
+               goto out;
 
-       switch (proto) {
+       switch (x->id.proto) {
        case IPPROTO_ESP:
                type = afinfo->type_offload_esp;
                break;
@@ -449,18 +449,16 @@ retry:
        rcu_read_unlock();
 
        if (!type && try_load) {
-               request_module("xfrm-offload-%d-%d", family, proto);
+               request_module("xfrm-offload-%d-%d", x->props.family,
+                              x->id.proto);
                try_load = false;
                goto retry;
        }
 
-       return type;
-}
-
-static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
-{
-       module_put(type->owner);
+out:
+       x->type_offload = type;
 }
+EXPORT_SYMBOL(xfrm_set_type_offload);
 
 static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
        [XFRM_MODE_BEET] = {
@@ -609,8 +607,6 @@ static void ___xfrm_state_destroy(struct xfrm_state *x)
        kfree(x->coaddr);
        kfree(x->replay_esn);
        kfree(x->preplay_esn);
-       if (x->type_offload)
-               xfrm_put_type_offload(x->type_offload);
        if (x->type) {
                x->type->destructor(x);
                xfrm_put_type(x->type);
@@ -784,6 +780,8 @@ void xfrm_dev_state_free(struct xfrm_state *x)
        struct xfrm_dev_offload *xso = &x->xso;
        struct net_device *dev = READ_ONCE(xso->dev);
 
+       xfrm_unset_type_offload(x);
+
        if (dev && dev->xfrmdev_ops) {
                spin_lock_bh(&xfrm_state_dev_gc_lock);
                if (!hlist_unhashed(&x->dev_gclist))
@@ -3122,7 +3120,7 @@ u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
 }
 EXPORT_SYMBOL_GPL(xfrm_state_mtu);
 
-int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload,
+int __xfrm_init_state(struct xfrm_state *x, bool init_replay,
                      struct netlink_ext_ack *extack)
 {
        const struct xfrm_mode *inner_mode;
@@ -3178,8 +3176,6 @@ int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload,
                goto error;
        }
 
-       x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
-
        err = x->type->init_state(x, extack);
        if (err)
                goto error;
@@ -3229,7 +3225,7 @@ int xfrm_init_state(struct xfrm_state *x)
 {
        int err;
 
-       err = __xfrm_init_state(x, true, false, NULL);
+       err = __xfrm_init_state(x, true, NULL);
        if (!err)
                x->km.state = XFRM_STATE_VALID;
 
index 5877eabe9d958e32bcebcc80d50efdffc0f5513c..b5266e0848e829358403810963b800f9657306c8 100644 (file)
@@ -919,7 +919,7 @@ static struct xfrm_state *xfrm_state_construct(struct net *net,
                        goto error;
        }
 
-       err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV], extack);
+       err = __xfrm_init_state(x, false, extack);
        if (err)
                goto error;