Skip to content

Commit 585b64f

Browse files
rleonklassert
authored andcommitted
xfrm: delay initialization of offload path till its actually requested
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: 9d389d7 ("xfrm: Add a xfrm type offload.") Signed-off-by: Leon Romanovsky <[email protected]> Signed-off-by: Steffen Klassert <[email protected]>
1 parent e3aa43a commit 585b64f

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

include/net/xfrm.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,15 @@ struct xfrm_type_offload {
464464

465465
int xfrm_register_type_offload(const struct xfrm_type_offload *type, unsigned short family);
466466
void xfrm_unregister_type_offload(const struct xfrm_type_offload *type, unsigned short family);
467+
void xfrm_set_type_offload(struct xfrm_state *x);
468+
static inline void xfrm_unset_type_offload(struct xfrm_state *x)
469+
{
470+
if (!x->type_offload)
471+
return;
472+
473+
module_put(x->type_offload->owner);
474+
x->type_offload = NULL;
475+
}
467476

468477
/**
469478
* struct xfrm_mode_cbs - XFRM mode callbacks
@@ -1760,7 +1769,7 @@ void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si);
17601769
u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq);
17611770
int xfrm_init_replay(struct xfrm_state *x, struct netlink_ext_ack *extack);
17621771
u32 xfrm_state_mtu(struct xfrm_state *x, int mtu);
1763-
int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload,
1772+
int __xfrm_init_state(struct xfrm_state *x, bool init_replay,
17641773
struct netlink_ext_ack *extack);
17651774
int xfrm_init_state(struct xfrm_state *x);
17661775
int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type);

net/xfrm/xfrm_device.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,6 @@ int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
244244
xfrm_address_t *daddr;
245245
bool is_packet_offload;
246246

247-
if (!x->type_offload) {
248-
NL_SET_ERR_MSG(extack, "Type doesn't support offload");
249-
return -EINVAL;
250-
}
251-
252247
if (xuo->flags &
253248
~(XFRM_OFFLOAD_IPV6 | XFRM_OFFLOAD_INBOUND | XFRM_OFFLOAD_PACKET)) {
254249
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,
310305
return -EINVAL;
311306
}
312307

308+
xfrm_set_type_offload(x);
309+
if (!x->type_offload) {
310+
NL_SET_ERR_MSG(extack, "Type doesn't support offload");
311+
dev_put(dev);
312+
return -EINVAL;
313+
}
314+
313315
xso->dev = dev;
314316
netdev_tracker_alloc(dev, &xso->dev_tracker, GFP_ATOMIC);
315317
xso->real_dev = dev;
@@ -332,6 +334,7 @@ int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
332334
netdev_put(dev, &xso->dev_tracker);
333335
xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
334336

337+
xfrm_unset_type_offload(x);
335338
/* User explicitly requested packet offload mode and configured
336339
* policy in addition to the XFRM state. So be civil to users,
337340
* and return an error instead of taking fallback path.

net/xfrm/xfrm_state.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -424,18 +424,18 @@ void xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
424424
}
425425
EXPORT_SYMBOL(xfrm_unregister_type_offload);
426426

427-
static const struct xfrm_type_offload *
428-
xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
427+
void xfrm_set_type_offload(struct xfrm_state *x)
429428
{
430429
const struct xfrm_type_offload *type = NULL;
431430
struct xfrm_state_afinfo *afinfo;
431+
bool try_load = true;
432432

433433
retry:
434-
afinfo = xfrm_state_get_afinfo(family);
434+
afinfo = xfrm_state_get_afinfo(x->props.family);
435435
if (unlikely(afinfo == NULL))
436-
return NULL;
436+
goto out;
437437

438-
switch (proto) {
438+
switch (x->id.proto) {
439439
case IPPROTO_ESP:
440440
type = afinfo->type_offload_esp;
441441
break;
@@ -449,18 +449,16 @@ xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
449449
rcu_read_unlock();
450450

451451
if (!type && try_load) {
452-
request_module("xfrm-offload-%d-%d", family, proto);
452+
request_module("xfrm-offload-%d-%d", x->props.family,
453+
x->id.proto);
453454
try_load = false;
454455
goto retry;
455456
}
456457

457-
return type;
458-
}
459-
460-
static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
461-
{
462-
module_put(type->owner);
458+
out:
459+
x->type_offload = type;
463460
}
461+
EXPORT_SYMBOL(xfrm_set_type_offload);
464462

465463
static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
466464
[XFRM_MODE_BEET] = {
@@ -609,8 +607,6 @@ static void ___xfrm_state_destroy(struct xfrm_state *x)
609607
kfree(x->coaddr);
610608
kfree(x->replay_esn);
611609
kfree(x->preplay_esn);
612-
if (x->type_offload)
613-
xfrm_put_type_offload(x->type_offload);
614610
if (x->type) {
615611
x->type->destructor(x);
616612
xfrm_put_type(x->type);
@@ -784,6 +780,8 @@ void xfrm_dev_state_free(struct xfrm_state *x)
784780
struct xfrm_dev_offload *xso = &x->xso;
785781
struct net_device *dev = READ_ONCE(xso->dev);
786782

783+
xfrm_unset_type_offload(x);
784+
787785
if (dev && dev->xfrmdev_ops) {
788786
spin_lock_bh(&xfrm_state_dev_gc_lock);
789787
if (!hlist_unhashed(&x->dev_gclist))
@@ -3122,7 +3120,7 @@ u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
31223120
}
31233121
EXPORT_SYMBOL_GPL(xfrm_state_mtu);
31243122

3125-
int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload,
3123+
int __xfrm_init_state(struct xfrm_state *x, bool init_replay,
31263124
struct netlink_ext_ack *extack)
31273125
{
31283126
const struct xfrm_mode *inner_mode;
@@ -3178,8 +3176,6 @@ int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload,
31783176
goto error;
31793177
}
31803178

3181-
x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
3182-
31833179
err = x->type->init_state(x, extack);
31843180
if (err)
31853181
goto error;
@@ -3229,7 +3225,7 @@ int xfrm_init_state(struct xfrm_state *x)
32293225
{
32303226
int err;
32313227

3232-
err = __xfrm_init_state(x, true, false, NULL);
3228+
err = __xfrm_init_state(x, true, NULL);
32333229
if (!err)
32343230
x->km.state = XFRM_STATE_VALID;
32353231

net/xfrm/xfrm_user.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ static struct xfrm_state *xfrm_state_construct(struct net *net,
919919
goto error;
920920
}
921921

922-
err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV], extack);
922+
err = __xfrm_init_state(x, false, extack);
923923
if (err)
924924
goto error;
925925

0 commit comments

Comments
 (0)