xfrm: remove xfrm replay indirections
Subsystems (3)
v2 cover xfrm: remove xfrm replay indirections
v2: fix unitialized variable buf reported by kbuild robot.
The xfrm replay logic is implemented via indirect calls.
xfrm_state struct holds a pointer to a
'struct xfrm_replay', which is one of several replay protection backends.
XFRM then invokes the backend via state->repl->callback(). Due to retpoline all indirect calls have become a lot more expensive. Fortunately, there are no 'replay modules', all are available for direct calls.
This series removes the 'struct xfrm_replay' and adds replay functions that can be called instead of the redirection.
Example:
- err = x->repl->overflow(x, skb);
+ err = xfrm_replay_overflow(x, skb);
Instead of a pointer to a struct with function pointers, xfrm_state now holds an enum that tells the replay core what kind of replay test is to be done.
Florian Westphal (6):
xfrm: replay: avoid xfrm replay notify indirection
xfrm: replay: get rid of duplicated notification code
xfrm: replay: remove advance indirection
xfrm: replay: remove recheck indirection
xfrm: replay: avoid replay indirection
xfrm: replay: remove last replay indirection
include/net/xfrm.h | 29 ++++---- net/xfrm/xfrm_input.c | 6 +- net/xfrm/xfrm_output.c | 2 +- net/xfrm/xfrm_replay.c | 189 +++++++++++++++++++++++++++---------------------- net/xfrm/xfrm_state.c | 2 +- 5 files changed, 125 insertions(+), 103 deletions(-)
PATCH v2 1/6 xfrm: replay: avoid xfrm replay notify indirection
replay protection is implemented using a callback structure and then called via
x->repl->notify(), x->repl->recheck(), and so on.
all the differect functions are always built-in, so this could be direct calls instead.
This first patch prepares for removal of the x->repl structure. Add an enum with the three available replay modes to the xfrm_state structure and then replace all x->repl->notify() calls by the new xfrm_replay_notify() helper.
The helper checks the enum internally to adapt behaviour as needed.
--- include/net/xfrm.h | 11 ++++++++++- net/xfrm/xfrm_replay.c | 45 ++++++++++++++++++++++++++---------------- net/xfrm/xfrm_state.c | 2 +- 3 files changed, 39 insertions(+), 19 deletions(-)
include/net/xfrm.h +10 -1
@@ -144,6 +144,12 @@ enum { XFRM_MODE_FLAG_TUNNEL = 1, }; +enum xfrm_replay_mode { +XFRM_REPLAY_MODE_LEGACY , +XFRM_REPLAY_MODE_BMP , +XFRM_REPLAY_MODE_ESN , +}; +/* Full description of state of transformer. */ struct xfrm_state {possible_net_t xs_net;
@@ -216,6 +222,8 @@ struct xfrm_state {/* The functions for replay detection. */ const struct xfrm_replay *repl; +/* replay detection mode */ +enum xfrm_replay_mode repl_mode;/* internal flag that only holds state for delayed aevent at the * moment */
@@ -303,7 +311,6 @@ struct xfrm_replay {int (*recheck)(struct xfrm_state *x,struct sk_buff *skb,__be32 net_seq); -void (*notify)(struct xfrm_state *x,int event);int (*overflow)(struct xfrm_state *x,struct sk_buff *skb); };
@@ -1712,6 +1719,8 @@ static inline int xfrm_policy_id2dir(u32 index) }#ifdef CONFIG_XFRM +void xfrm_replay_notify (struct xfrm_state *x,int event); +static inline int xfrm_aevent_is_on (struct net *net) {struct sock *nlsk;
net/xfrm/xfrm_replay.c +28 -17
158 lines not on this page · raw message
net/xfrm/xfrm_state.c +1 -1
@@ -2110,7 +2110,7 @@ static void xfrm_replay_timer_handler(struct timer_list *t)if (x->km.state ==XFRM_STATE_VALID ) {if (xfrm_aevent_is_on (xs_net (x))) - x->repl->notify (x,XFRM_REPLAY_TIMEOUT ); +xfrm_replay_notify (x,XFRM_REPLAY_TIMEOUT );else x->xflags |=XFRM_TIME_DEFER ; }
Signature · 1 line
2.26.2
PATCH v2 2/6 xfrm: replay: get rid of duplicated notification code
In reply to Florian WestphalAfter previous patch, we can consolidate some code:
xfrm_replay_notify, xfrm_replay_notify_bmp and _esn all contain the same code at the end.
Remove it from xfrm_replay_notify_bmp/esn and reuse the one in xfrm_replay_notify.
--- net/xfrm/xfrm_replay.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-)
net/xfrm/xfrm_replay.c +4 -18
@@ -56,10 +56,10 @@ void xfrm_replay_notify(struct xfrm_state *x, int event)break ;case XFRM_REPLAY_MODE_BMP :xfrm_replay_notify_bmp (x, event); -return ; + goto notify;case XFRM_REPLAY_MODE_ESN :xfrm_replay_notify_esn (x, event); -return ; + goto notify; }switch (event) {
@@ -86,6 +86,8 @@ void xfrm_replay_notify(struct xfrm_state *x, int event) }memcpy (&x->preplay, &x->replay,sizeof (struct xfrm_replay_state )); + +notify: c.event =XFRM_MSG_NEWAE ; c.data.aevent = event;km_state_notify (x, &c);
@@ -290,7 +292,6 @@ static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq)static void xfrm_replay_notify_bmp (struct xfrm_state *x,int event) { -struct km_event c;struct xfrm_replay_state_esn *replay_esn = x->replay_esn;struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn;
@@ -330,19 +331,11 @@ static void xfrm_replay_notify_bmp(struct xfrm_state *x, int event)memcpy (x->preplay_esn, x->replay_esn,xfrm_replay_state_esn_len (replay_esn)); - c.event =XFRM_MSG_NEWAE ; - c.data.aevent = event; -km_state_notify (x, &c); - -if (x->replay_maxage && - !mod_timer (&x->rtimer, jiffies + x->replay_maxage)) - x->xflags &= ~XFRM_TIME_DEFER ; }static void xfrm_replay_notify_esn (struct xfrm_state *x,int event) {u32 seq_diff, oseq_diff; -struct km_event c;struct xfrm_replay_state_esn *replay_esn = x->replay_esn;struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn;
@@ -396,13 +389,6 @@ static void xfrm_replay_notify_esn(struct xfrm_state *x, int event)memcpy (x->preplay_esn, x->replay_esn,xfrm_replay_state_esn_len (replay_esn)); - c.event =XFRM_MSG_NEWAE ; - c.data.aevent = event; -km_state_notify (x, &c); - -if (x->replay_maxage && - !mod_timer (&x->rtimer, jiffies + x->replay_maxage)) - x->xflags &= ~XFRM_TIME_DEFER ; }static int xfrm_replay_overflow_esn (struct xfrm_state *x,struct sk_buff *skb)
Signature · 1 line
2.26.2
PATCH v2 3/6 xfrm: replay: remove advance indirection
In reply to Florian WestphalSimilar to other patches: add a new helper to avoid an indirection.
v2: fix 'net/xfrm/xfrm_replay.c:519:13: warning: 'seq' may be used uninitialized in this function' warning.
--- include/net/xfrm.h | 2 +- net/xfrm/xfrm_input.c | 2 +- net/xfrm/xfrm_replay.c | 24 +++++++++++++++--------- 3 files changed, 17 insertions(+), 11 deletions(-)
include/net/xfrm.h +1 -1
@@ -304,7 +304,6 @@ struct km_event { };struct xfrm_replay { -void (*advance)(struct xfrm_state *x,__be32 net_seq);int (*check)(struct xfrm_state *x,struct sk_buff *skb,__be32 net_seq);
@@ -1719,6 +1718,7 @@ static inline int xfrm_policy_id2dir(u32 index) }#ifdef CONFIG_XFRM +void xfrm_replay_advance (struct xfrm_state *x,__be32 net_seq);void xfrm_replay_notify (struct xfrm_state *x,int event);static inline int xfrm_aevent_is_on (struct net *net)
net/xfrm/xfrm_input.c +1 -1
@@ -663,7 +663,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) goto drop_unlock; } - x->repl->advance (x, seq); +xfrm_replay_advance (x, seq); x->curlft.bytes += skb->len; x->curlft.packets++;
net/xfrm/xfrm_replay.c +15 -9
@@ -151,14 +151,26 @@ static int xfrm_replay_check(struct xfrm_state *x,return -EINVAL ; } -static void xfrm_replay_advance (struct xfrm_state *x,__be32 net_seq) +static void xfrm_replay_advance_bmp (struct xfrm_state *x,__be32 net_seq); +static void xfrm_replay_advance_esn (struct xfrm_state *x,__be32 net_seq); + +void xfrm_replay_advance (struct xfrm_state *x,__be32 net_seq) { -u32 diff; -u32 seq =ntohl (net_seq); +u32 diff, seq; + +switch (x->repl_mode) { +case XFRM_REPLAY_MODE_LEGACY : +break ; +case XFRM_REPLAY_MODE_BMP : +return xfrm_replay_advance_bmp (x, net_seq); +case XFRM_REPLAY_MODE_ESN : +return xfrm_replay_advance_esn (x, net_seq); + }if (!x->props.replay_window)return ; + seq =ntohl (net_seq);if (seq > x->replay.seq) { diff = seq - x->replay.seq;if (diff < x->props.replay_window)
@@ -677,42 +689,36 @@ static int xfrm_replay_overflow_offload_esn(struct xfrm_state *x, struct sk_buff }static const struct xfrm_replay xfrm_replay_legacy = { - .advance = xfrm_replay_advance, .check = xfrm_replay_check, .recheck = xfrm_replay_check, .overflow = xfrm_replay_overflow_offload, };static const struct xfrm_replay xfrm_replay_bmp = { - .advance = xfrm_replay_advance_bmp, .check = xfrm_replay_check_bmp, .recheck = xfrm_replay_check_bmp, .overflow = xfrm_replay_overflow_offload_bmp, };static const struct xfrm_replay xfrm_replay_esn = { - .advance = xfrm_replay_advance_esn, .check = xfrm_replay_check_esn, .recheck = xfrm_replay_recheck_esn, .overflow = xfrm_replay_overflow_offload_esn, };#else static const struct xfrm_replay xfrm_replay_legacy = { - .advance = xfrm_replay_advance, .check = xfrm_replay_check, .recheck = xfrm_replay_check, .overflow = xfrm_replay_overflow, };static const struct xfrm_replay xfrm_replay_bmp = { - .advance = xfrm_replay_advance_bmp, .check = xfrm_replay_check_bmp, .recheck = xfrm_replay_check_bmp, .overflow = xfrm_replay_overflow_bmp, };static const struct xfrm_replay xfrm_replay_esn = { - .advance = xfrm_replay_advance_esn, .check = xfrm_replay_check_esn, .recheck = xfrm_replay_recheck_esn, .overflow = xfrm_replay_overflow_esn,
Signature · 1 line
2.26.2
PATCH v2 4/6 xfrm: replay: remove recheck indirection
In reply to Florian WestphalAdds new xfrm_replay_recheck() helper and calls it from xfrm input path instead of the indirection.
--- include/net/xfrm.h | 4 +--- net/xfrm/xfrm_input.c | 2 +- net/xfrm/xfrm_replay.c | 22 ++++++++++++++++------ 3 files changed, 18 insertions(+), 10 deletions(-)
include/net/xfrm.h +1 -3
@@ -307,9 +307,6 @@ struct xfrm_replay {int (*check)(struct xfrm_state *x,struct sk_buff *skb,__be32 net_seq); -int (*recheck)(struct xfrm_state *x, -struct sk_buff *skb, -__be32 net_seq);int (*overflow)(struct xfrm_state *x,struct sk_buff *skb); };
@@ -1720,6 +1717,7 @@ static inline int xfrm_policy_id2dir(u32 index)#ifdef CONFIG_XFRM void xfrm_replay_advance (struct xfrm_state *x,__be32 net_seq);void xfrm_replay_notify (struct xfrm_state *x,int event); +int xfrm_replay_recheck (struct xfrm_state *x,struct sk_buff *skb,__be32 net_seq);static inline int xfrm_aevent_is_on (struct net *net) {
net/xfrm/xfrm_input.c +1 -1
@@ -658,7 +658,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)/* only the first xfrm gets the encap type */ encap_type =0 ; -if (async && x->repl->recheck (x, skb, seq)) { +if (async &&xfrm_replay_recheck (x, skb, seq)) {XFRM_INC_STATS (net,LINUX_MIB_XFRMINSTATESEQERROR ); goto drop_unlock; }
net/xfrm/xfrm_replay.c +16 -6
@@ -503,6 +503,22 @@ static int xfrm_replay_recheck_esn(struct xfrm_state *x,return xfrm_replay_check_esn (x, skb, net_seq); } +int xfrm_replay_recheck (struct xfrm_state *x, +struct sk_buff *skb,__be32 net_seq) +{ +switch (x->repl_mode) { +case XFRM_REPLAY_MODE_LEGACY : +break ; +case XFRM_REPLAY_MODE_BMP : +/* no special recheck treatment */ +return xfrm_replay_check_bmp (x, skb, net_seq); +case XFRM_REPLAY_MODE_ESN : +return xfrm_replay_recheck_esn (x, skb, net_seq); + } + +return xfrm_replay_check (x, skb, net_seq); +} +static void xfrm_replay_advance_esn (struct xfrm_state *x,__be32 net_seq) {unsigned int bitnr, nr, i;
@@ -690,37 +706,31 @@ static int xfrm_replay_overflow_offload_esn(struct xfrm_state *x, struct sk_buffstatic const struct xfrm_replay xfrm_replay_legacy = { .check = xfrm_replay_check, - .recheck = xfrm_replay_check, .overflow = xfrm_replay_overflow_offload, };static const struct xfrm_replay xfrm_replay_bmp = { .check = xfrm_replay_check_bmp, - .recheck = xfrm_replay_check_bmp, .overflow = xfrm_replay_overflow_offload_bmp, };static const struct xfrm_replay xfrm_replay_esn = { .check = xfrm_replay_check_esn, - .recheck = xfrm_replay_recheck_esn, .overflow = xfrm_replay_overflow_offload_esn, };#else static const struct xfrm_replay xfrm_replay_legacy = { .check = xfrm_replay_check, - .recheck = xfrm_replay_check, .overflow = xfrm_replay_overflow, };static const struct xfrm_replay xfrm_replay_bmp = { .check = xfrm_replay_check_bmp, - .recheck = xfrm_replay_check_bmp, .overflow = xfrm_replay_overflow_bmp, };static const struct xfrm_replay xfrm_replay_esn = { .check = xfrm_replay_check_esn, - .recheck = xfrm_replay_recheck_esn, .overflow = xfrm_replay_overflow_esn, };#endif
Signature · 1 line
2.26.2
PATCH v2 5/6 xfrm: replay: avoid replay indirection
In reply to Florian WestphalAdd and use xfrm_replay_check helper instead of indirection.
--- include/net/xfrm.h | 4 +--- net/xfrm/xfrm_input.c | 2 +- net/xfrm/xfrm_replay.c | 27 ++++++++++++++++++--------- 3 files changed, 20 insertions(+), 13 deletions(-)
include/net/xfrm.h +1 -3
@@ -304,9 +304,6 @@ struct km_event { };struct xfrm_replay { -int (*check)(struct xfrm_state *x, -struct sk_buff *skb, -__be32 net_seq);int (*overflow)(struct xfrm_state *x,struct sk_buff *skb); };
@@ -1716,6 +1713,7 @@ static inline int xfrm_policy_id2dir(u32 index)#ifdef CONFIG_XFRM void xfrm_replay_advance (struct xfrm_state *x,__be32 net_seq); +int xfrm_replay_check (struct xfrm_state *x,struct sk_buff *skb,__be32 net_seq);void xfrm_replay_notify (struct xfrm_state *x,int event);int xfrm_replay_recheck (struct xfrm_state *x,struct sk_buff *skb,__be32 net_seq);
net/xfrm/xfrm_input.c +1 -1
@@ -610,7 +610,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) goto drop_unlock; } -if (x->repl->check (x, skb, seq)) { +if (xfrm_replay_check (x, skb, seq)) {XFRM_INC_STATS (net,LINUX_MIB_XFRMINSTATESEQERROR ); goto drop_unlock; }
net/xfrm/xfrm_replay.c +18 -9
@@ -119,8 +119,8 @@ static int xfrm_replay_overflow(struct xfrm_state *x, struct sk_buff *skb)return err; } -static int xfrm_replay_check (struct xfrm_state *x, -struct sk_buff *skb,__be32 net_seq) +static int xfrm_replay_check_legacy (struct xfrm_state *x, +struct sk_buff *skb,__be32 net_seq) {u32 diff;u32 seq =ntohl (net_seq);
@@ -491,6 +491,21 @@ static int xfrm_replay_check_esn(struct xfrm_state *x,return -EINVAL ; } +int xfrm_replay_check (struct xfrm_state *x, +struct sk_buff *skb,__be32 net_seq) +{ +switch (x->repl_mode) { +case XFRM_REPLAY_MODE_LEGACY : +break ; +case XFRM_REPLAY_MODE_BMP : +return xfrm_replay_check_bmp (x, skb, net_seq); +case XFRM_REPLAY_MODE_ESN : +return xfrm_replay_check_esn (x, skb, net_seq); + } + +return xfrm_replay_check_legacy (x, skb, net_seq); +} +static int xfrm_replay_recheck_esn (struct xfrm_state *x,struct sk_buff *skb,__be32 net_seq) {
@@ -516,7 +531,7 @@ int xfrm_replay_recheck(struct xfrm_state *x,return xfrm_replay_recheck_esn (x, skb, net_seq); } -return xfrm_replay_check (x, skb, net_seq); +return xfrm_replay_check_legacy (x, skb, net_seq); }static void xfrm_replay_advance_esn (struct xfrm_state *x,__be32 net_seq)
@@ -705,32 +720,26 @@ static int xfrm_replay_overflow_offload_esn(struct xfrm_state *x, struct sk_buff }static const struct xfrm_replay xfrm_replay_legacy = { - .check = xfrm_replay_check, .overflow = xfrm_replay_overflow_offload, };static const struct xfrm_replay xfrm_replay_bmp = { - .check = xfrm_replay_check_bmp, .overflow = xfrm_replay_overflow_offload_bmp, };static const struct xfrm_replay xfrm_replay_esn = { - .check = xfrm_replay_check_esn, .overflow = xfrm_replay_overflow_offload_esn, };#else static const struct xfrm_replay xfrm_replay_legacy = { - .check = xfrm_replay_check, .overflow = xfrm_replay_overflow, };static const struct xfrm_replay xfrm_replay_bmp = { - .check = xfrm_replay_check_bmp, .overflow = xfrm_replay_overflow_bmp, };static const struct xfrm_replay xfrm_replay_esn = { - .check = xfrm_replay_check_esn, .overflow = xfrm_replay_overflow_esn, };#endif
Signature · 1 line
2.26.2
PATCH v2 6/6 xfrm: replay: remove last replay indirection
In reply to Florian WestphalThis replaces the overflow indirection with the new xfrm_replay_overflow helper. After this, the 'repl' pointer in xfrm_state is no longer needed and can be removed as well.
xfrm_replay_overflow() is added in two incarnations, one is used when the kernel is configured with xfrm offload enabled, the other when its disabled.
--- include/net/xfrm.h | 8 +------ net/xfrm/xfrm_output.c | 2 +- net/xfrm/xfrm_replay.c | 51 +++++++++++++++++++++--------------------- 3 files changed, 28 insertions(+), 33 deletions(-)
include/net/xfrm.h +1 -7
@@ -219,9 +219,6 @@ struct xfrm_state {struct xfrm_replay_state preplay;struct xfrm_replay_state_esn *preplay_esn; -/* The functions for replay detection. */ -const struct xfrm_replay *repl; -/* replay detection mode */ enum xfrm_replay_mode repl_mode;/* internal flag that only holds state for delayed aevent at the
@@ -303,10 +300,6 @@ struct km_event {struct net *net; }; -struct xfrm_replay { -int (*overflow)(struct xfrm_state *x,struct sk_buff *skb); -}; -struct xfrm_if_cb {struct xfrm_if *(*decode_session)(struct sk_buff *skb,unsigned short family);
@@ -1715,6 +1708,7 @@ static inline int xfrm_policy_id2dir(u32 index)void xfrm_replay_advance (struct xfrm_state *x,__be32 net_seq);int xfrm_replay_check (struct xfrm_state *x,struct sk_buff *skb,__be32 net_seq);void xfrm_replay_notify (struct xfrm_state *x,int event); +int xfrm_replay_overflow (struct xfrm_state *x,struct sk_buff *skb);int xfrm_replay_recheck (struct xfrm_state *x,struct sk_buff *skb,__be32 net_seq);static inline int xfrm_aevent_is_on (struct net *net)
net/xfrm/xfrm_output.c +1 -1
@@ -448,7 +448,7 @@ static int xfrm_output_one(struct sk_buff *skb, int err) goto error; } - err = x->repl->overflow (x, skb); + err =xfrm_replay_overflow (x, skb);if (err) {XFRM_INC_STATS (net,LINUX_MIB_XFRMOUTSTATESEQERROR ); goto error;
net/xfrm/xfrm_replay.c +26 -25
83 lines not on this page · raw message
Signature · 1 line
2.26.2
Re: [PATCH ipsec-next v2 2/6] xfrm: replay: get rid of duplicated notification code
In reply to Florian WestphalHi Florian,
Quoted 18 lines · Florian Westphal
After previous patch, we can consolidate some code:
xfrm_replay_notify,xfrm_replay_notify_bmpand_esnall contain the same code at the end.Remove it from xfrm_replay_notify_bmp/esn and reuse the one in
xfrm_replay_notify.Signed-off-by Florian Westphal <[email protected]>--- net/xfrm/xfrm_replay.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-)net/xfrm/xfrm_replay.c +0
-0
net/xfrm/xfrm_replay.c void xfrm_replay_notify(struct xfrm_state *x, int event) · line 62
Quoted 9 lines
net/xfrm/xfrm_replay.c +2
-2@@ -56,10 +56,10 @@ void xfrm_replay_notify(struct xfrm_state *x, int event)break ;case XFRM_REPLAY_MODE_BMP :xfrm_replay_notify_bmp (x, event); -return ; + goto notify;case XFRM_REPLAY_MODE_ESN :xfrm_replay_notify_esn (x, event); -return ; + goto notify;
These two functions have some early returns that skip the notification, but now the notification will be sent in all cases:
static void xfrm_replay_notify_bmp(struct xfrm_state *x, int event)
{
<snip>
switch (event) {
case XFRM_REPLAY_UPDATE:
if (...) {
if (x->xflags & XFRM_TIME_DEFER)
event = XFRM_REPLAY_TIMEOUT;
else
return;
}
break;And this also changes the value that ends up in c.data.aevent. That change will be lost after this patch.
Quoted 12 lines
net/xfrm/xfrm_replay.c +2
-0}switch (event) {@@ -86,6 +86,8 @@ void xfrm_replay_notify(struct xfrm_state *x, int event) }memcpy (&x->preplay, &x->replay,sizeof (struct xfrm_replay_state )); + +notify: c.event =XFRM_MSG_NEWAE ; c.data.aevent = event;km_state_notify (x, &c);
Signature · 1 line
Sabrina
Re: [PATCH ipsec-next v2 2/6] xfrm: replay: get rid of duplicated notification code
Quoted 11 lines · Sabrina Dubroca
net/xfrm/xfrm_replay.c void xfrm_replay_notify(struct xfrm_state *x, int event) · line 62
Quoted 8 lines
net/xfrm/xfrm_replay.c +2
-2case XFRM_REPLAY_MODE_BMP :xfrm_replay_notify_bmp (x, event); -return ; + goto notify;case XFRM_REPLAY_MODE_ESN :xfrm_replay_notify_esn (x, event); -return ; + goto notify;These two functions have some early returns that skip the notification, but now the notification will be sent in all cases:
Right, I will rework this series.
Thanks for pointing this out.