blob: dab57daae40856030790fa8070068a59d82220af [file] [log] [blame]
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001/*
2 * xfrm_replay.c - xfrm replay detection, derived from xfrm_state.c.
Steffen Klassert97e15c32011-03-08 00:09:09 +00003 *
4 * Copyright (C) 2010 secunet Security Networks AG
5 * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
Steffen Klassert9fdc4882011-03-08 00:08:32 +000019 */
20
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040021#include <linux/export.h>
Steffen Klassert9fdc4882011-03-08 00:08:32 +000022#include <net/xfrm.h>
23
Steffen Klassert2cd08462011-03-08 00:09:51 +000024u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq)
25{
26 u32 seq, seq_hi, bottom;
27 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
28
29 if (!(x->props.flags & XFRM_STATE_ESN))
30 return 0;
31
32 seq = ntohl(net_seq);
33 seq_hi = replay_esn->seq_hi;
34 bottom = replay_esn->seq - replay_esn->replay_window + 1;
35
36 if (likely(replay_esn->seq >= replay_esn->replay_window - 1)) {
37 /* A. same subspace */
38 if (unlikely(seq < bottom))
39 seq_hi++;
40 } else {
41 /* B. window spans two subspaces */
42 if (unlikely(seq >= bottom))
43 seq_hi--;
44 }
45
46 return seq_hi;
47}
48
Steffen Klassert9fdc4882011-03-08 00:08:32 +000049static void xfrm_replay_notify(struct xfrm_state *x, int event)
50{
51 struct km_event c;
52 /* we send notify messages in case
53 * 1. we updated on of the sequence numbers, and the seqno difference
54 * is at least x->replay_maxdiff, in this case we also update the
55 * timeout of our timer function
56 * 2. if x->replay_maxage has elapsed since last update,
57 * and there were changes
58 *
59 * The state structure must be locked!
60 */
61
62 switch (event) {
63 case XFRM_REPLAY_UPDATE:
Thomas Egerercd808fc2013-09-19 13:19:19 +020064 if (!x->replay_maxdiff ||
65 ((x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
66 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff))) {
Steffen Klassert9fdc4882011-03-08 00:08:32 +000067 if (x->xflags & XFRM_TIME_DEFER)
68 event = XFRM_REPLAY_TIMEOUT;
69 else
70 return;
71 }
72
73 break;
74
75 case XFRM_REPLAY_TIMEOUT:
76 if (memcmp(&x->replay, &x->preplay,
77 sizeof(struct xfrm_replay_state)) == 0) {
78 x->xflags |= XFRM_TIME_DEFER;
79 return;
80 }
81
82 break;
83 }
84
85 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
86 c.event = XFRM_MSG_NEWAE;
87 c.data.aevent = event;
88 km_state_notify(x, &c);
89
90 if (x->replay_maxage &&
91 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
92 x->xflags &= ~XFRM_TIME_DEFER;
93}
94
95static int xfrm_replay_overflow(struct xfrm_state *x, struct sk_buff *skb)
96{
97 int err = 0;
98 struct net *net = xs_net(x);
99
100 if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
101 XFRM_SKB_CB(skb)->seq.output.low = ++x->replay.oseq;
102 if (unlikely(x->replay.oseq == 0)) {
103 x->replay.oseq--;
104 xfrm_audit_state_replay_overflow(x, skb);
105 err = -EOVERFLOW;
106
107 return err;
108 }
109 if (xfrm_aevent_is_on(net))
110 x->repl->notify(x, XFRM_REPLAY_UPDATE);
111 }
112
113 return err;
114}
115
116static int xfrm_replay_check(struct xfrm_state *x,
117 struct sk_buff *skb, __be32 net_seq)
118{
119 u32 diff;
120 u32 seq = ntohl(net_seq);
121
Steffen Klassert36ae0142011-03-28 19:45:52 +0000122 if (!x->props.replay_window)
123 return 0;
124
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000125 if (unlikely(seq == 0))
126 goto err;
127
128 if (likely(seq > x->replay.seq))
129 return 0;
130
131 diff = x->replay.seq - seq;
Fan Du33fce602013-09-17 15:14:13 +0800132 if (diff >= x->props.replay_window) {
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000133 x->stats.replay_window++;
134 goto err;
135 }
136
137 if (x->replay.bitmap & (1U << diff)) {
138 x->stats.replay++;
139 goto err;
140 }
141 return 0;
142
143err:
144 xfrm_audit_state_replay(x, skb, net_seq);
145 return -EINVAL;
146}
147
148static void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
149{
150 u32 diff;
151 u32 seq = ntohl(net_seq);
152
153 if (!x->props.replay_window)
154 return;
155
156 if (seq > x->replay.seq) {
157 diff = seq - x->replay.seq;
158 if (diff < x->props.replay_window)
159 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
160 else
161 x->replay.bitmap = 1;
162 x->replay.seq = seq;
163 } else {
164 diff = x->replay.seq - seq;
165 x->replay.bitmap |= (1U << diff);
166 }
167
168 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert1265fd62012-03-21 23:36:13 +0000169 x->repl->notify(x, XFRM_REPLAY_UPDATE);
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000170}
171
Steffen Klassert97e15c32011-03-08 00:09:09 +0000172static int xfrm_replay_overflow_bmp(struct xfrm_state *x, struct sk_buff *skb)
173{
174 int err = 0;
175 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
176 struct net *net = xs_net(x);
177
178 if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
179 XFRM_SKB_CB(skb)->seq.output.low = ++replay_esn->oseq;
180 if (unlikely(replay_esn->oseq == 0)) {
181 replay_esn->oseq--;
182 xfrm_audit_state_replay_overflow(x, skb);
183 err = -EOVERFLOW;
184
185 return err;
186 }
187 if (xfrm_aevent_is_on(net))
188 x->repl->notify(x, XFRM_REPLAY_UPDATE);
189 }
190
191 return err;
192}
193
194static int xfrm_replay_check_bmp(struct xfrm_state *x,
195 struct sk_buff *skb, __be32 net_seq)
196{
197 unsigned int bitnr, nr;
198 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
Steffen Klassert36ae0142011-03-28 19:45:52 +0000199 u32 pos;
Steffen Klassert97e15c32011-03-08 00:09:09 +0000200 u32 seq = ntohl(net_seq);
201 u32 diff = replay_esn->seq - seq;
Steffen Klassert36ae0142011-03-28 19:45:52 +0000202
203 if (!replay_esn->replay_window)
204 return 0;
205
Steffen Klassert97e15c32011-03-08 00:09:09 +0000206 if (unlikely(seq == 0))
207 goto err;
208
209 if (likely(seq > replay_esn->seq))
210 return 0;
211
212 if (diff >= replay_esn->replay_window) {
213 x->stats.replay_window++;
214 goto err;
215 }
216
Steffen Klassert1d974372011-10-11 01:58:37 +0000217 pos = (replay_esn->seq - 1) % replay_esn->replay_window;
218
219 if (pos >= diff)
Steffen Klassert97e15c32011-03-08 00:09:09 +0000220 bitnr = (pos - diff) % replay_esn->replay_window;
Steffen Klassert1d974372011-10-11 01:58:37 +0000221 else
Steffen Klassert97e15c32011-03-08 00:09:09 +0000222 bitnr = replay_esn->replay_window - (diff - pos);
Steffen Klassert1d974372011-10-11 01:58:37 +0000223
224 nr = bitnr >> 5;
225 bitnr = bitnr & 0x1F;
226 if (replay_esn->bmp[nr] & (1U << bitnr))
227 goto err_replay;
228
Steffen Klassert97e15c32011-03-08 00:09:09 +0000229 return 0;
230
231err_replay:
232 x->stats.replay++;
233err:
234 xfrm_audit_state_replay(x, skb, net_seq);
235 return -EINVAL;
236}
237
238static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq)
239{
240 unsigned int bitnr, nr, i;
241 u32 diff;
242 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
243 u32 seq = ntohl(net_seq);
Nickolai Zeldoviche2f67252013-01-17 13:58:28 -0500244 u32 pos;
Steffen Klassert97e15c32011-03-08 00:09:09 +0000245
246 if (!replay_esn->replay_window)
247 return;
248
Nickolai Zeldoviche2f67252013-01-17 13:58:28 -0500249 pos = (replay_esn->seq - 1) % replay_esn->replay_window;
250
Steffen Klassert97e15c32011-03-08 00:09:09 +0000251 if (seq > replay_esn->seq) {
252 diff = seq - replay_esn->seq;
253
254 if (diff < replay_esn->replay_window) {
255 for (i = 1; i < diff; i++) {
256 bitnr = (pos + i) % replay_esn->replay_window;
257 nr = bitnr >> 5;
258 bitnr = bitnr & 0x1F;
259 replay_esn->bmp[nr] &= ~(1U << bitnr);
260 }
Steffen Klassert97e15c32011-03-08 00:09:09 +0000261 } else {
Steffen Klasserte7566822011-06-05 20:46:03 +0000262 nr = (replay_esn->replay_window - 1) >> 5;
Steffen Klassert97e15c32011-03-08 00:09:09 +0000263 for (i = 0; i <= nr; i++)
264 replay_esn->bmp[i] = 0;
Steffen Klassert97e15c32011-03-08 00:09:09 +0000265 }
266
Steffen Klassert1d974372011-10-11 01:58:37 +0000267 bitnr = (pos + diff) % replay_esn->replay_window;
Steffen Klassert97e15c32011-03-08 00:09:09 +0000268 replay_esn->seq = seq;
269 } else {
270 diff = replay_esn->seq - seq;
271
Steffen Klassert1d974372011-10-11 01:58:37 +0000272 if (pos >= diff)
Steffen Klassert97e15c32011-03-08 00:09:09 +0000273 bitnr = (pos - diff) % replay_esn->replay_window;
Steffen Klassert1d974372011-10-11 01:58:37 +0000274 else
Steffen Klassert97e15c32011-03-08 00:09:09 +0000275 bitnr = replay_esn->replay_window - (diff - pos);
Steffen Klassert97e15c32011-03-08 00:09:09 +0000276 }
277
Steffen Klassert1d974372011-10-11 01:58:37 +0000278 nr = bitnr >> 5;
279 bitnr = bitnr & 0x1F;
280 replay_esn->bmp[nr] |= (1U << bitnr);
281
Steffen Klassert97e15c32011-03-08 00:09:09 +0000282 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert1265fd62012-03-21 23:36:13 +0000283 x->repl->notify(x, XFRM_REPLAY_UPDATE);
Steffen Klassert97e15c32011-03-08 00:09:09 +0000284}
285
286static void xfrm_replay_notify_bmp(struct xfrm_state *x, int event)
287{
288 struct km_event c;
289 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
290 struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn;
291
292 /* we send notify messages in case
293 * 1. we updated on of the sequence numbers, and the seqno difference
294 * is at least x->replay_maxdiff, in this case we also update the
295 * timeout of our timer function
296 * 2. if x->replay_maxage has elapsed since last update,
297 * and there were changes
298 *
299 * The state structure must be locked!
300 */
301
302 switch (event) {
303 case XFRM_REPLAY_UPDATE:
Thomas Egerercd808fc2013-09-19 13:19:19 +0200304 if (!x->replay_maxdiff ||
305 ((replay_esn->seq - preplay_esn->seq < x->replay_maxdiff) &&
306 (replay_esn->oseq - preplay_esn->oseq
307 < x->replay_maxdiff))) {
Steffen Klassert97e15c32011-03-08 00:09:09 +0000308 if (x->xflags & XFRM_TIME_DEFER)
309 event = XFRM_REPLAY_TIMEOUT;
310 else
311 return;
312 }
313
314 break;
315
316 case XFRM_REPLAY_TIMEOUT:
317 if (memcmp(x->replay_esn, x->preplay_esn,
318 xfrm_replay_state_esn_len(replay_esn)) == 0) {
319 x->xflags |= XFRM_TIME_DEFER;
320 return;
321 }
322
323 break;
324 }
325
326 memcpy(x->preplay_esn, x->replay_esn,
327 xfrm_replay_state_esn_len(replay_esn));
328 c.event = XFRM_MSG_NEWAE;
329 c.data.aevent = event;
330 km_state_notify(x, &c);
331
332 if (x->replay_maxage &&
333 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
334 x->xflags &= ~XFRM_TIME_DEFER;
335}
336
Steffen Klassert0017c0b2013-03-11 10:32:16 +0100337static void xfrm_replay_notify_esn(struct xfrm_state *x, int event)
338{
339 u32 seq_diff, oseq_diff;
340 struct km_event c;
341 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
342 struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn;
343
344 /* we send notify messages in case
345 * 1. we updated on of the sequence numbers, and the seqno difference
346 * is at least x->replay_maxdiff, in this case we also update the
347 * timeout of our timer function
348 * 2. if x->replay_maxage has elapsed since last update,
349 * and there were changes
350 *
351 * The state structure must be locked!
352 */
353
354 switch (event) {
355 case XFRM_REPLAY_UPDATE:
Thomas Egerercd808fc2013-09-19 13:19:19 +0200356 if (x->replay_maxdiff) {
357 if (replay_esn->seq_hi == preplay_esn->seq_hi)
358 seq_diff = replay_esn->seq - preplay_esn->seq;
Steffen Klassert0017c0b2013-03-11 10:32:16 +0100359 else
Thomas Egerercd808fc2013-09-19 13:19:19 +0200360 seq_diff = ~preplay_esn->seq + replay_esn->seq
361 + 1;
362
363 if (replay_esn->oseq_hi == preplay_esn->oseq_hi)
364 oseq_diff = replay_esn->oseq
365 - preplay_esn->oseq;
366 else
367 oseq_diff = ~preplay_esn->oseq
368 + replay_esn->oseq + 1;
369
370 if (seq_diff >= x->replay_maxdiff ||
371 oseq_diff >= x->replay_maxdiff)
372 break;
Steffen Klassert0017c0b2013-03-11 10:32:16 +0100373 }
374
Thomas Egerercd808fc2013-09-19 13:19:19 +0200375 if (x->xflags & XFRM_TIME_DEFER)
376 event = XFRM_REPLAY_TIMEOUT;
377 else
378 return;
379
Steffen Klassert0017c0b2013-03-11 10:32:16 +0100380 break;
381
382 case XFRM_REPLAY_TIMEOUT:
383 if (memcmp(x->replay_esn, x->preplay_esn,
384 xfrm_replay_state_esn_len(replay_esn)) == 0) {
385 x->xflags |= XFRM_TIME_DEFER;
386 return;
387 }
388
389 break;
390 }
391
392 memcpy(x->preplay_esn, x->replay_esn,
393 xfrm_replay_state_esn_len(replay_esn));
394 c.event = XFRM_MSG_NEWAE;
395 c.data.aevent = event;
396 km_state_notify(x, &c);
397
398 if (x->replay_maxage &&
399 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
400 x->xflags &= ~XFRM_TIME_DEFER;
401}
402
Steffen Klassert2cd08462011-03-08 00:09:51 +0000403static int xfrm_replay_overflow_esn(struct xfrm_state *x, struct sk_buff *skb)
404{
405 int err = 0;
406 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
407 struct net *net = xs_net(x);
408
409 if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
410 XFRM_SKB_CB(skb)->seq.output.low = ++replay_esn->oseq;
411 XFRM_SKB_CB(skb)->seq.output.hi = replay_esn->oseq_hi;
412
413 if (unlikely(replay_esn->oseq == 0)) {
414 XFRM_SKB_CB(skb)->seq.output.hi = ++replay_esn->oseq_hi;
415
416 if (replay_esn->oseq_hi == 0) {
417 replay_esn->oseq--;
418 replay_esn->oseq_hi--;
419 xfrm_audit_state_replay_overflow(x, skb);
420 err = -EOVERFLOW;
421
422 return err;
423 }
424 }
425 if (xfrm_aevent_is_on(net))
426 x->repl->notify(x, XFRM_REPLAY_UPDATE);
427 }
428
429 return err;
430}
431
432static int xfrm_replay_check_esn(struct xfrm_state *x,
433 struct sk_buff *skb, __be32 net_seq)
434{
435 unsigned int bitnr, nr;
436 u32 diff;
437 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
Steffen Klassert36ae0142011-03-28 19:45:52 +0000438 u32 pos;
Steffen Klassert2cd08462011-03-08 00:09:51 +0000439 u32 seq = ntohl(net_seq);
Steffen Klassert2cd08462011-03-08 00:09:51 +0000440 u32 wsize = replay_esn->replay_window;
441 u32 top = replay_esn->seq;
442 u32 bottom = top - wsize + 1;
443
Steffen Klassert36ae0142011-03-28 19:45:52 +0000444 if (!wsize)
445 return 0;
446
Steffen Klassert2cd08462011-03-08 00:09:51 +0000447 if (unlikely(seq == 0 && replay_esn->seq_hi == 0 &&
448 (replay_esn->seq < replay_esn->replay_window - 1)))
449 goto err;
450
451 diff = top - seq;
452
453 if (likely(top >= wsize - 1)) {
454 /* A. same subspace */
455 if (likely(seq > top) || seq < bottom)
456 return 0;
457 } else {
458 /* B. window spans two subspaces */
459 if (likely(seq > top && seq < bottom))
460 return 0;
461 if (seq >= bottom)
462 diff = ~seq + top + 1;
463 }
464
465 if (diff >= replay_esn->replay_window) {
466 x->stats.replay_window++;
467 goto err;
468 }
469
Steffen Klassert1d974372011-10-11 01:58:37 +0000470 pos = (replay_esn->seq - 1) % replay_esn->replay_window;
471
472 if (pos >= diff)
Steffen Klassert2cd08462011-03-08 00:09:51 +0000473 bitnr = (pos - diff) % replay_esn->replay_window;
Steffen Klassert1d974372011-10-11 01:58:37 +0000474 else
Steffen Klassert2cd08462011-03-08 00:09:51 +0000475 bitnr = replay_esn->replay_window - (diff - pos);
Steffen Klassert1d974372011-10-11 01:58:37 +0000476
477 nr = bitnr >> 5;
478 bitnr = bitnr & 0x1F;
479 if (replay_esn->bmp[nr] & (1U << bitnr))
480 goto err_replay;
481
Steffen Klassert2cd08462011-03-08 00:09:51 +0000482 return 0;
483
484err_replay:
485 x->stats.replay++;
486err:
487 xfrm_audit_state_replay(x, skb, net_seq);
488 return -EINVAL;
489}
490
Steffen Klassert3b59df42012-09-04 00:03:29 +0000491static int xfrm_replay_recheck_esn(struct xfrm_state *x,
492 struct sk_buff *skb, __be32 net_seq)
493{
494 if (unlikely(XFRM_SKB_CB(skb)->seq.input.hi !=
495 htonl(xfrm_replay_seqhi(x, net_seq)))) {
496 x->stats.replay_window++;
497 return -EINVAL;
498 }
499
500 return xfrm_replay_check_esn(x, skb, net_seq);
501}
502
Steffen Klassert2cd08462011-03-08 00:09:51 +0000503static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq)
504{
505 unsigned int bitnr, nr, i;
506 int wrap;
507 u32 diff, pos, seq, seq_hi;
508 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
509
510 if (!replay_esn->replay_window)
511 return;
512
513 seq = ntohl(net_seq);
514 pos = (replay_esn->seq - 1) % replay_esn->replay_window;
515 seq_hi = xfrm_replay_seqhi(x, net_seq);
516 wrap = seq_hi - replay_esn->seq_hi;
517
518 if ((!wrap && seq > replay_esn->seq) || wrap > 0) {
519 if (likely(!wrap))
520 diff = seq - replay_esn->seq;
521 else
522 diff = ~replay_esn->seq + seq + 1;
523
524 if (diff < replay_esn->replay_window) {
525 for (i = 1; i < diff; i++) {
526 bitnr = (pos + i) % replay_esn->replay_window;
527 nr = bitnr >> 5;
528 bitnr = bitnr & 0x1F;
529 replay_esn->bmp[nr] &= ~(1U << bitnr);
530 }
Steffen Klassert2cd08462011-03-08 00:09:51 +0000531 } else {
Steffen Klasserte7566822011-06-05 20:46:03 +0000532 nr = (replay_esn->replay_window - 1) >> 5;
Steffen Klassert2cd08462011-03-08 00:09:51 +0000533 for (i = 0; i <= nr; i++)
534 replay_esn->bmp[i] = 0;
Steffen Klassert2cd08462011-03-08 00:09:51 +0000535 }
536
Steffen Klassert1d974372011-10-11 01:58:37 +0000537 bitnr = (pos + diff) % replay_esn->replay_window;
Steffen Klassert2cd08462011-03-08 00:09:51 +0000538 replay_esn->seq = seq;
539
540 if (unlikely(wrap > 0))
541 replay_esn->seq_hi++;
542 } else {
543 diff = replay_esn->seq - seq;
544
Steffen Klassert1d974372011-10-11 01:58:37 +0000545 if (pos >= diff)
Steffen Klassert2cd08462011-03-08 00:09:51 +0000546 bitnr = (pos - diff) % replay_esn->replay_window;
Steffen Klassert1d974372011-10-11 01:58:37 +0000547 else
Steffen Klassert2cd08462011-03-08 00:09:51 +0000548 bitnr = replay_esn->replay_window - (diff - pos);
Steffen Klassert2cd08462011-03-08 00:09:51 +0000549 }
550
Steffen Klassert1d974372011-10-11 01:58:37 +0000551 nr = bitnr >> 5;
552 bitnr = bitnr & 0x1F;
553 replay_esn->bmp[nr] |= (1U << bitnr);
554
Steffen Klassert2cd08462011-03-08 00:09:51 +0000555 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert1265fd62012-03-21 23:36:13 +0000556 x->repl->notify(x, XFRM_REPLAY_UPDATE);
Steffen Klassert2cd08462011-03-08 00:09:51 +0000557}
558
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000559static struct xfrm_replay xfrm_replay_legacy = {
560 .advance = xfrm_replay_advance,
561 .check = xfrm_replay_check,
Steffen Klassert3b59df42012-09-04 00:03:29 +0000562 .recheck = xfrm_replay_check,
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000563 .notify = xfrm_replay_notify,
564 .overflow = xfrm_replay_overflow,
565};
566
Steffen Klassert97e15c32011-03-08 00:09:09 +0000567static struct xfrm_replay xfrm_replay_bmp = {
568 .advance = xfrm_replay_advance_bmp,
569 .check = xfrm_replay_check_bmp,
Steffen Klassert3b59df42012-09-04 00:03:29 +0000570 .recheck = xfrm_replay_check_bmp,
Steffen Klassert97e15c32011-03-08 00:09:09 +0000571 .notify = xfrm_replay_notify_bmp,
572 .overflow = xfrm_replay_overflow_bmp,
573};
574
Steffen Klassert2cd08462011-03-08 00:09:51 +0000575static struct xfrm_replay xfrm_replay_esn = {
576 .advance = xfrm_replay_advance_esn,
577 .check = xfrm_replay_check_esn,
Steffen Klassert3b59df42012-09-04 00:03:29 +0000578 .recheck = xfrm_replay_recheck_esn,
Steffen Klassert0017c0b2013-03-11 10:32:16 +0100579 .notify = xfrm_replay_notify_esn,
Steffen Klassert2cd08462011-03-08 00:09:51 +0000580 .overflow = xfrm_replay_overflow_esn,
581};
582
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000583int xfrm_init_replay(struct xfrm_state *x)
584{
Steffen Klassert97e15c32011-03-08 00:09:09 +0000585 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
586
587 if (replay_esn) {
588 if (replay_esn->replay_window >
Steffen Klassert3f602b02011-04-25 19:39:24 +0000589 replay_esn->bmp_len * sizeof(__u32) * 8)
Steffen Klassert97e15c32011-03-08 00:09:09 +0000590 return -EINVAL;
591
Ulrich Weberaafd0d82012-11-08 11:15:44 +0100592 if (x->props.flags & XFRM_STATE_ESN) {
593 if (replay_esn->replay_window == 0)
594 return -EINVAL;
595 x->repl = &xfrm_replay_esn;
596 } else
597 x->repl = &xfrm_replay_bmp;
Steffen Klassert97e15c32011-03-08 00:09:09 +0000598 } else
599 x->repl = &xfrm_replay_legacy;
600
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000601 return 0;
602}
603EXPORT_SYMBOL(xfrm_init_replay);