diff options
author | Steffen Klassert <steffen.klassert@secunet.com> | 2011-10-10 21:58:37 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2011-10-18 23:51:30 -0400 |
commit | 1d9743745bf5ba30b57986d8da33c4fa072c355b (patch) | |
tree | 0c17d35afb44595a5241f8549cd832ca3cc55ddf /net/xfrm | |
parent | a4886d522e18e5d4a63b95a5ead72f6105e3ef98 (diff) |
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: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/xfrm')
-rw-r--r-- | net/xfrm/xfrm_replay.c | 98 |
1 files changed, 34 insertions, 64 deletions
diff --git a/net/xfrm/xfrm_replay.c b/net/xfrm/xfrm_replay.c index b11ea692bd7d..6ca357406ea8 100644 --- a/net/xfrm/xfrm_replay.c +++ b/net/xfrm/xfrm_replay.c | |||
@@ -203,8 +203,6 @@ static int xfrm_replay_check_bmp(struct xfrm_state *x, | |||
203 | if (!replay_esn->replay_window) | 203 | if (!replay_esn->replay_window) |
204 | return 0; | 204 | return 0; |
205 | 205 | ||
206 | pos = (replay_esn->seq - 1) % replay_esn->replay_window; | ||
207 | |||
208 | if (unlikely(seq == 0)) | 206 | if (unlikely(seq == 0)) |
209 | goto err; | 207 | goto err; |
210 | 208 | ||
@@ -216,19 +214,18 @@ static int xfrm_replay_check_bmp(struct xfrm_state *x, | |||
216 | goto err; | 214 | goto err; |
217 | } | 215 | } |
218 | 216 | ||
219 | if (pos >= diff) { | 217 | pos = (replay_esn->seq - 1) % replay_esn->replay_window; |
218 | |||
219 | if (pos >= diff) | ||
220 | bitnr = (pos - diff) % replay_esn->replay_window; | 220 | bitnr = (pos - diff) % replay_esn->replay_window; |
221 | nr = bitnr >> 5; | 221 | else |
222 | bitnr = bitnr & 0x1F; | ||
223 | if (replay_esn->bmp[nr] & (1U << bitnr)) | ||
224 | goto err_replay; | ||
225 | } else { | ||
226 | bitnr = replay_esn->replay_window - (diff - pos); | 222 | bitnr = replay_esn->replay_window - (diff - pos); |
227 | nr = bitnr >> 5; | 223 | |
228 | bitnr = bitnr & 0x1F; | 224 | nr = bitnr >> 5; |
229 | if (replay_esn->bmp[nr] & (1U << bitnr)) | 225 | bitnr = bitnr & 0x1F; |
230 | goto err_replay; | 226 | if (replay_esn->bmp[nr] & (1U << bitnr)) |
231 | } | 227 | goto err_replay; |
228 | |||
232 | return 0; | 229 | return 0; |
233 | 230 | ||
234 | err_replay: | 231 | err_replay: |
@@ -259,39 +256,27 @@ static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq) | |||
259 | bitnr = bitnr & 0x1F; | 256 | bitnr = bitnr & 0x1F; |
260 | replay_esn->bmp[nr] &= ~(1U << bitnr); | 257 | replay_esn->bmp[nr] &= ~(1U << bitnr); |
261 | } | 258 | } |
262 | |||
263 | bitnr = (pos + diff) % replay_esn->replay_window; | ||
264 | nr = bitnr >> 5; | ||
265 | bitnr = bitnr & 0x1F; | ||
266 | replay_esn->bmp[nr] |= (1U << bitnr); | ||
267 | } else { | 259 | } else { |
268 | nr = (replay_esn->replay_window - 1) >> 5; | 260 | nr = (replay_esn->replay_window - 1) >> 5; |
269 | for (i = 0; i <= nr; i++) | 261 | for (i = 0; i <= nr; i++) |
270 | replay_esn->bmp[i] = 0; | 262 | replay_esn->bmp[i] = 0; |
271 | |||
272 | bitnr = (pos + diff) % replay_esn->replay_window; | ||
273 | nr = bitnr >> 5; | ||
274 | bitnr = bitnr & 0x1F; | ||
275 | replay_esn->bmp[nr] |= (1U << bitnr); | ||
276 | } | 263 | } |
277 | 264 | ||
265 | bitnr = (pos + diff) % replay_esn->replay_window; | ||
278 | replay_esn->seq = seq; | 266 | replay_esn->seq = seq; |
279 | } else { | 267 | } else { |
280 | diff = replay_esn->seq - seq; | 268 | diff = replay_esn->seq - seq; |
281 | 269 | ||
282 | if (pos >= diff) { | 270 | if (pos >= diff) |
283 | bitnr = (pos - diff) % replay_esn->replay_window; | 271 | bitnr = (pos - diff) % replay_esn->replay_window; |
284 | nr = bitnr >> 5; | 272 | else |
285 | bitnr = bitnr & 0x1F; | ||
286 | replay_esn->bmp[nr] |= (1U << bitnr); | ||
287 | } else { | ||
288 | bitnr = replay_esn->replay_window - (diff - pos); | 273 | bitnr = replay_esn->replay_window - (diff - pos); |
289 | nr = bitnr >> 5; | ||
290 | bitnr = bitnr & 0x1F; | ||
291 | replay_esn->bmp[nr] |= (1U << bitnr); | ||
292 | } | ||
293 | } | 274 | } |
294 | 275 | ||
276 | nr = bitnr >> 5; | ||
277 | bitnr = bitnr & 0x1F; | ||
278 | replay_esn->bmp[nr] |= (1U << bitnr); | ||
279 | |||
295 | if (xfrm_aevent_is_on(xs_net(x))) | 280 | if (xfrm_aevent_is_on(xs_net(x))) |
296 | xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); | 281 | xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); |
297 | } | 282 | } |
@@ -390,8 +375,6 @@ static int xfrm_replay_check_esn(struct xfrm_state *x, | |||
390 | if (!wsize) | 375 | if (!wsize) |
391 | return 0; | 376 | return 0; |
392 | 377 | ||
393 | pos = (replay_esn->seq - 1) % replay_esn->replay_window; | ||
394 | |||
395 | if (unlikely(seq == 0 && replay_esn->seq_hi == 0 && | 378 | if (unlikely(seq == 0 && replay_esn->seq_hi == 0 && |
396 | (replay_esn->seq < replay_esn->replay_window - 1))) | 379 | (replay_esn->seq < replay_esn->replay_window - 1))) |
397 | goto err; | 380 | goto err; |
@@ -415,19 +398,18 @@ static int xfrm_replay_check_esn(struct xfrm_state *x, | |||
415 | goto err; | 398 | goto err; |
416 | } | 399 | } |
417 | 400 | ||
418 | if (pos >= diff) { | 401 | pos = (replay_esn->seq - 1) % replay_esn->replay_window; |
402 | |||
403 | if (pos >= diff) | ||
419 | bitnr = (pos - diff) % replay_esn->replay_window; | 404 | bitnr = (pos - diff) % replay_esn->replay_window; |
420 | nr = bitnr >> 5; | 405 | else |
421 | bitnr = bitnr & 0x1F; | ||
422 | if (replay_esn->bmp[nr] & (1U << bitnr)) | ||
423 | goto err_replay; | ||
424 | } else { | ||
425 | bitnr = replay_esn->replay_window - (diff - pos); | 406 | bitnr = replay_esn->replay_window - (diff - pos); |
426 | nr = bitnr >> 5; | 407 | |
427 | bitnr = bitnr & 0x1F; | 408 | nr = bitnr >> 5; |
428 | if (replay_esn->bmp[nr] & (1U << bitnr)) | 409 | bitnr = bitnr & 0x1F; |
429 | goto err_replay; | 410 | if (replay_esn->bmp[nr] & (1U << bitnr)) |
430 | } | 411 | goto err_replay; |
412 | |||
431 | return 0; | 413 | return 0; |
432 | 414 | ||
433 | err_replay: | 415 | err_replay: |
@@ -465,22 +447,13 @@ static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq) | |||
465 | bitnr = bitnr & 0x1F; | 447 | bitnr = bitnr & 0x1F; |
466 | replay_esn->bmp[nr] &= ~(1U << bitnr); | 448 | replay_esn->bmp[nr] &= ~(1U << bitnr); |
467 | } | 449 | } |
468 | |||
469 | bitnr = (pos + diff) % replay_esn->replay_window; | ||
470 | nr = bitnr >> 5; | ||
471 | bitnr = bitnr & 0x1F; | ||
472 | replay_esn->bmp[nr] |= (1U << bitnr); | ||
473 | } else { | 450 | } else { |
474 | nr = (replay_esn->replay_window - 1) >> 5; | 451 | nr = (replay_esn->replay_window - 1) >> 5; |
475 | for (i = 0; i <= nr; i++) | 452 | for (i = 0; i <= nr; i++) |
476 | replay_esn->bmp[i] = 0; | 453 | replay_esn->bmp[i] = 0; |
477 | |||
478 | bitnr = (pos + diff) % replay_esn->replay_window; | ||
479 | nr = bitnr >> 5; | ||
480 | bitnr = bitnr & 0x1F; | ||
481 | replay_esn->bmp[nr] |= (1U << bitnr); | ||
482 | } | 454 | } |
483 | 455 | ||
456 | bitnr = (pos + diff) % replay_esn->replay_window; | ||
484 | replay_esn->seq = seq; | 457 | replay_esn->seq = seq; |
485 | 458 | ||
486 | if (unlikely(wrap > 0)) | 459 | if (unlikely(wrap > 0)) |
@@ -488,19 +461,16 @@ static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq) | |||
488 | } else { | 461 | } else { |
489 | diff = replay_esn->seq - seq; | 462 | diff = replay_esn->seq - seq; |
490 | 463 | ||
491 | if (pos >= diff) { | 464 | if (pos >= diff) |
492 | bitnr = (pos - diff) % replay_esn->replay_window; | 465 | bitnr = (pos - diff) % replay_esn->replay_window; |
493 | nr = bitnr >> 5; | 466 | else |
494 | bitnr = bitnr & 0x1F; | ||
495 | replay_esn->bmp[nr] |= (1U << bitnr); | ||
496 | } else { | ||
497 | bitnr = replay_esn->replay_window - (diff - pos); | 467 | bitnr = replay_esn->replay_window - (diff - pos); |
498 | nr = bitnr >> 5; | ||
499 | bitnr = bitnr & 0x1F; | ||
500 | replay_esn->bmp[nr] |= (1U << bitnr); | ||
501 | } | ||
502 | } | 468 | } |
503 | 469 | ||
470 | nr = bitnr >> 5; | ||
471 | bitnr = bitnr & 0x1F; | ||
472 | replay_esn->bmp[nr] |= (1U << bitnr); | ||
473 | |||
504 | if (xfrm_aevent_is_on(xs_net(x))) | 474 | if (xfrm_aevent_is_on(xs_net(x))) |
505 | xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); | 475 | xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); |
506 | } | 476 | } |