Commit 1d974374 authored by Steffen Klassert's avatar Steffen Klassert Committed by David S. Miller

xfrm: Simplify the replay check and advance functions

The replay check and replay advance functions had some code
duplications. This patch removes the duplications.
Signed-off-by: default avatarSteffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent a4886d52
...@@ -203,8 +203,6 @@ static int xfrm_replay_check_bmp(struct xfrm_state *x, ...@@ -203,8 +203,6 @@ static int xfrm_replay_check_bmp(struct xfrm_state *x,
if (!replay_esn->replay_window) if (!replay_esn->replay_window)
return 0; return 0;
pos = (replay_esn->seq - 1) % replay_esn->replay_window;
if (unlikely(seq == 0)) if (unlikely(seq == 0))
goto err; goto err;
...@@ -216,19 +214,18 @@ static int xfrm_replay_check_bmp(struct xfrm_state *x, ...@@ -216,19 +214,18 @@ static int xfrm_replay_check_bmp(struct xfrm_state *x,
goto err; goto err;
} }
if (pos >= diff) { pos = (replay_esn->seq - 1) % replay_esn->replay_window;
if (pos >= diff)
bitnr = (pos - diff) % replay_esn->replay_window; bitnr = (pos - diff) % replay_esn->replay_window;
nr = bitnr >> 5; else
bitnr = bitnr & 0x1F;
if (replay_esn->bmp[nr] & (1U << bitnr))
goto err_replay;
} else {
bitnr = replay_esn->replay_window - (diff - pos); bitnr = replay_esn->replay_window - (diff - pos);
nr = bitnr >> 5;
bitnr = bitnr & 0x1F; nr = bitnr >> 5;
if (replay_esn->bmp[nr] & (1U << bitnr)) bitnr = bitnr & 0x1F;
goto err_replay; if (replay_esn->bmp[nr] & (1U << bitnr))
} goto err_replay;
return 0; return 0;
err_replay: err_replay:
...@@ -259,39 +256,27 @@ static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq) ...@@ -259,39 +256,27 @@ static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq)
bitnr = bitnr & 0x1F; bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] &= ~(1U << bitnr); replay_esn->bmp[nr] &= ~(1U << bitnr);
} }
bitnr = (pos + diff) % replay_esn->replay_window;
nr = bitnr >> 5;
bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] |= (1U << bitnr);
} else { } else {
nr = (replay_esn->replay_window - 1) >> 5; nr = (replay_esn->replay_window - 1) >> 5;
for (i = 0; i <= nr; i++) for (i = 0; i <= nr; i++)
replay_esn->bmp[i] = 0; replay_esn->bmp[i] = 0;
bitnr = (pos + diff) % replay_esn->replay_window;
nr = bitnr >> 5;
bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] |= (1U << bitnr);
} }
bitnr = (pos + diff) % replay_esn->replay_window;
replay_esn->seq = seq; replay_esn->seq = seq;
} else { } else {
diff = replay_esn->seq - seq; diff = replay_esn->seq - seq;
if (pos >= diff) { if (pos >= diff)
bitnr = (pos - diff) % replay_esn->replay_window; bitnr = (pos - diff) % replay_esn->replay_window;
nr = bitnr >> 5; else
bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] |= (1U << bitnr);
} else {
bitnr = replay_esn->replay_window - (diff - pos); bitnr = replay_esn->replay_window - (diff - pos);
nr = bitnr >> 5;
bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] |= (1U << bitnr);
}
} }
nr = bitnr >> 5;
bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] |= (1U << bitnr);
if (xfrm_aevent_is_on(xs_net(x))) if (xfrm_aevent_is_on(xs_net(x)))
xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
} }
...@@ -390,8 +375,6 @@ static int xfrm_replay_check_esn(struct xfrm_state *x, ...@@ -390,8 +375,6 @@ static int xfrm_replay_check_esn(struct xfrm_state *x,
if (!wsize) if (!wsize)
return 0; return 0;
pos = (replay_esn->seq - 1) % replay_esn->replay_window;
if (unlikely(seq == 0 && replay_esn->seq_hi == 0 && if (unlikely(seq == 0 && replay_esn->seq_hi == 0 &&
(replay_esn->seq < replay_esn->replay_window - 1))) (replay_esn->seq < replay_esn->replay_window - 1)))
goto err; goto err;
...@@ -415,19 +398,18 @@ static int xfrm_replay_check_esn(struct xfrm_state *x, ...@@ -415,19 +398,18 @@ static int xfrm_replay_check_esn(struct xfrm_state *x,
goto err; goto err;
} }
if (pos >= diff) { pos = (replay_esn->seq - 1) % replay_esn->replay_window;
if (pos >= diff)
bitnr = (pos - diff) % replay_esn->replay_window; bitnr = (pos - diff) % replay_esn->replay_window;
nr = bitnr >> 5; else
bitnr = bitnr & 0x1F;
if (replay_esn->bmp[nr] & (1U << bitnr))
goto err_replay;
} else {
bitnr = replay_esn->replay_window - (diff - pos); bitnr = replay_esn->replay_window - (diff - pos);
nr = bitnr >> 5;
bitnr = bitnr & 0x1F; nr = bitnr >> 5;
if (replay_esn->bmp[nr] & (1U << bitnr)) bitnr = bitnr & 0x1F;
goto err_replay; if (replay_esn->bmp[nr] & (1U << bitnr))
} goto err_replay;
return 0; return 0;
err_replay: err_replay:
...@@ -465,22 +447,13 @@ static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq) ...@@ -465,22 +447,13 @@ static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq)
bitnr = bitnr & 0x1F; bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] &= ~(1U << bitnr); replay_esn->bmp[nr] &= ~(1U << bitnr);
} }
bitnr = (pos + diff) % replay_esn->replay_window;
nr = bitnr >> 5;
bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] |= (1U << bitnr);
} else { } else {
nr = (replay_esn->replay_window - 1) >> 5; nr = (replay_esn->replay_window - 1) >> 5;
for (i = 0; i <= nr; i++) for (i = 0; i <= nr; i++)
replay_esn->bmp[i] = 0; replay_esn->bmp[i] = 0;
bitnr = (pos + diff) % replay_esn->replay_window;
nr = bitnr >> 5;
bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] |= (1U << bitnr);
} }
bitnr = (pos + diff) % replay_esn->replay_window;
replay_esn->seq = seq; replay_esn->seq = seq;
if (unlikely(wrap > 0)) if (unlikely(wrap > 0))
...@@ -488,19 +461,16 @@ static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq) ...@@ -488,19 +461,16 @@ static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq)
} else { } else {
diff = replay_esn->seq - seq; diff = replay_esn->seq - seq;
if (pos >= diff) { if (pos >= diff)
bitnr = (pos - diff) % replay_esn->replay_window; bitnr = (pos - diff) % replay_esn->replay_window;
nr = bitnr >> 5; else
bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] |= (1U << bitnr);
} else {
bitnr = replay_esn->replay_window - (diff - pos); bitnr = replay_esn->replay_window - (diff - pos);
nr = bitnr >> 5;
bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] |= (1U << bitnr);
}
} }
nr = bitnr >> 5;
bitnr = bitnr & 0x1F;
replay_esn->bmp[nr] |= (1U << bitnr);
if (xfrm_aevent_is_on(xs_net(x))) if (xfrm_aevent_is_on(xs_net(x)))
xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment