2 * xfrm_replay.c - xfrm replay detection, derived from xfrm_state.c.
4 * Copyright (C) 2010 secunet Security Networks AG
5 * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com>
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.
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
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.
23 u32
xfrm_replay_seqhi(struct xfrm_state
*x
, __be32 net_seq
)
25 u32 seq
, seq_hi
, bottom
;
26 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
28 if (!(x
->props
.flags
& XFRM_STATE_ESN
))
32 seq_hi
= replay_esn
->seq_hi
;
33 bottom
= replay_esn
->seq
- replay_esn
->replay_window
+ 1;
35 if (likely(replay_esn
->seq
>= replay_esn
->replay_window
- 1)) {
36 /* A. same subspace */
37 if (unlikely(seq
< bottom
))
40 /* B. window spans two subspaces */
41 if (unlikely(seq
>= bottom
))
48 static void xfrm_replay_notify(struct xfrm_state
*x
, int event
)
51 /* we send notify messages in case
52 * 1. we updated on of the sequence numbers, and the seqno difference
53 * is at least x->replay_maxdiff, in this case we also update the
54 * timeout of our timer function
55 * 2. if x->replay_maxage has elapsed since last update,
56 * and there were changes
58 * The state structure must be locked!
62 case XFRM_REPLAY_UPDATE
:
63 if (x
->replay_maxdiff
&&
64 (x
->replay
.seq
- x
->preplay
.seq
< x
->replay_maxdiff
) &&
65 (x
->replay
.oseq
- x
->preplay
.oseq
< x
->replay_maxdiff
)) {
66 if (x
->xflags
& XFRM_TIME_DEFER
)
67 event
= XFRM_REPLAY_TIMEOUT
;
74 case XFRM_REPLAY_TIMEOUT
:
75 if (memcmp(&x
->replay
, &x
->preplay
,
76 sizeof(struct xfrm_replay_state
)) == 0) {
77 x
->xflags
|= XFRM_TIME_DEFER
;
84 memcpy(&x
->preplay
, &x
->replay
, sizeof(struct xfrm_replay_state
));
85 c
.event
= XFRM_MSG_NEWAE
;
86 c
.data
.aevent
= event
;
87 km_state_notify(x
, &c
);
89 if (x
->replay_maxage
&&
90 !mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
))
91 x
->xflags
&= ~XFRM_TIME_DEFER
;
94 static int xfrm_replay_overflow(struct xfrm_state
*x
, struct sk_buff
*skb
)
97 struct net
*net
= xs_net(x
);
99 if (x
->type
->flags
& XFRM_TYPE_REPLAY_PROT
) {
100 XFRM_SKB_CB(skb
)->seq
.output
.low
= ++x
->replay
.oseq
;
101 if (unlikely(x
->replay
.oseq
== 0)) {
103 xfrm_audit_state_replay_overflow(x
, skb
);
108 if (xfrm_aevent_is_on(net
))
109 x
->repl
->notify(x
, XFRM_REPLAY_UPDATE
);
115 static int xfrm_replay_check(struct xfrm_state
*x
,
116 struct sk_buff
*skb
, __be32 net_seq
)
119 u32 seq
= ntohl(net_seq
);
121 if (unlikely(seq
== 0))
124 if (likely(seq
> x
->replay
.seq
))
127 diff
= x
->replay
.seq
- seq
;
128 if (diff
>= min_t(unsigned int, x
->props
.replay_window
,
129 sizeof(x
->replay
.bitmap
) * 8)) {
130 x
->stats
.replay_window
++;
134 if (x
->replay
.bitmap
& (1U << diff
)) {
141 xfrm_audit_state_replay(x
, skb
, net_seq
);
145 static void xfrm_replay_advance(struct xfrm_state
*x
, __be32 net_seq
)
148 u32 seq
= ntohl(net_seq
);
150 if (!x
->props
.replay_window
)
153 if (seq
> x
->replay
.seq
) {
154 diff
= seq
- x
->replay
.seq
;
155 if (diff
< x
->props
.replay_window
)
156 x
->replay
.bitmap
= ((x
->replay
.bitmap
) << diff
) | 1;
158 x
->replay
.bitmap
= 1;
161 diff
= x
->replay
.seq
- seq
;
162 x
->replay
.bitmap
|= (1U << diff
);
165 if (xfrm_aevent_is_on(xs_net(x
)))
166 xfrm_replay_notify(x
, XFRM_REPLAY_UPDATE
);
169 static int xfrm_replay_overflow_bmp(struct xfrm_state
*x
, struct sk_buff
*skb
)
172 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
173 struct net
*net
= xs_net(x
);
175 if (x
->type
->flags
& XFRM_TYPE_REPLAY_PROT
) {
176 XFRM_SKB_CB(skb
)->seq
.output
.low
= ++replay_esn
->oseq
;
177 if (unlikely(replay_esn
->oseq
== 0)) {
179 xfrm_audit_state_replay_overflow(x
, skb
);
184 if (xfrm_aevent_is_on(net
))
185 x
->repl
->notify(x
, XFRM_REPLAY_UPDATE
);
191 static int xfrm_replay_check_bmp(struct xfrm_state
*x
,
192 struct sk_buff
*skb
, __be32 net_seq
)
194 unsigned int bitnr
, nr
;
195 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
196 u32 seq
= ntohl(net_seq
);
197 u32 diff
= replay_esn
->seq
- seq
;
198 u32 pos
= (replay_esn
->seq
- 1) % replay_esn
->replay_window
;
200 if (unlikely(seq
== 0))
203 if (likely(seq
> replay_esn
->seq
))
206 if (diff
>= replay_esn
->replay_window
) {
207 x
->stats
.replay_window
++;
212 bitnr
= (pos
- diff
) % replay_esn
->replay_window
;
214 bitnr
= bitnr
& 0x1F;
215 if (replay_esn
->bmp
[nr
] & (1U << bitnr
))
218 bitnr
= replay_esn
->replay_window
- (diff
- pos
);
220 bitnr
= bitnr
& 0x1F;
221 if (replay_esn
->bmp
[nr
] & (1U << bitnr
))
229 xfrm_audit_state_replay(x
, skb
, net_seq
);
233 static void xfrm_replay_advance_bmp(struct xfrm_state
*x
, __be32 net_seq
)
235 unsigned int bitnr
, nr
, i
;
237 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
238 u32 seq
= ntohl(net_seq
);
239 u32 pos
= (replay_esn
->seq
- 1) % replay_esn
->replay_window
;
241 if (!replay_esn
->replay_window
)
244 if (seq
> replay_esn
->seq
) {
245 diff
= seq
- replay_esn
->seq
;
247 if (diff
< replay_esn
->replay_window
) {
248 for (i
= 1; i
< diff
; i
++) {
249 bitnr
= (pos
+ i
) % replay_esn
->replay_window
;
251 bitnr
= bitnr
& 0x1F;
252 replay_esn
->bmp
[nr
] &= ~(1U << bitnr
);
255 bitnr
= (pos
+ diff
) % replay_esn
->replay_window
;
257 bitnr
= bitnr
& 0x1F;
258 replay_esn
->bmp
[nr
] |= (1U << bitnr
);
260 nr
= replay_esn
->replay_window
>> 5;
261 for (i
= 0; i
<= nr
; i
++)
262 replay_esn
->bmp
[i
] = 0;
264 bitnr
= (pos
+ diff
) % replay_esn
->replay_window
;
266 bitnr
= bitnr
& 0x1F;
267 replay_esn
->bmp
[nr
] |= (1U << bitnr
);
270 replay_esn
->seq
= seq
;
272 diff
= replay_esn
->seq
- seq
;
275 bitnr
= (pos
- diff
) % replay_esn
->replay_window
;
277 bitnr
= bitnr
& 0x1F;
278 replay_esn
->bmp
[nr
] |= (1U << bitnr
);
280 bitnr
= replay_esn
->replay_window
- (diff
- pos
);
282 bitnr
= bitnr
& 0x1F;
283 replay_esn
->bmp
[nr
] |= (1U << bitnr
);
287 if (xfrm_aevent_is_on(xs_net(x
)))
288 xfrm_replay_notify(x
, XFRM_REPLAY_UPDATE
);
291 static void xfrm_replay_notify_bmp(struct xfrm_state
*x
, int event
)
294 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
295 struct xfrm_replay_state_esn
*preplay_esn
= x
->preplay_esn
;
297 /* we send notify messages in case
298 * 1. we updated on of the sequence numbers, and the seqno difference
299 * is at least x->replay_maxdiff, in this case we also update the
300 * timeout of our timer function
301 * 2. if x->replay_maxage has elapsed since last update,
302 * and there were changes
304 * The state structure must be locked!
308 case XFRM_REPLAY_UPDATE
:
309 if (x
->replay_maxdiff
&&
310 (replay_esn
->seq
- preplay_esn
->seq
< x
->replay_maxdiff
) &&
311 (replay_esn
->oseq
- preplay_esn
->oseq
< x
->replay_maxdiff
)) {
312 if (x
->xflags
& XFRM_TIME_DEFER
)
313 event
= XFRM_REPLAY_TIMEOUT
;
320 case XFRM_REPLAY_TIMEOUT
:
321 if (memcmp(x
->replay_esn
, x
->preplay_esn
,
322 xfrm_replay_state_esn_len(replay_esn
)) == 0) {
323 x
->xflags
|= XFRM_TIME_DEFER
;
330 memcpy(x
->preplay_esn
, x
->replay_esn
,
331 xfrm_replay_state_esn_len(replay_esn
));
332 c
.event
= XFRM_MSG_NEWAE
;
333 c
.data
.aevent
= event
;
334 km_state_notify(x
, &c
);
336 if (x
->replay_maxage
&&
337 !mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
))
338 x
->xflags
&= ~XFRM_TIME_DEFER
;
341 static int xfrm_replay_overflow_esn(struct xfrm_state
*x
, struct sk_buff
*skb
)
344 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
345 struct net
*net
= xs_net(x
);
347 if (x
->type
->flags
& XFRM_TYPE_REPLAY_PROT
) {
348 XFRM_SKB_CB(skb
)->seq
.output
.low
= ++replay_esn
->oseq
;
349 XFRM_SKB_CB(skb
)->seq
.output
.hi
= replay_esn
->oseq_hi
;
351 if (unlikely(replay_esn
->oseq
== 0)) {
352 XFRM_SKB_CB(skb
)->seq
.output
.hi
= ++replay_esn
->oseq_hi
;
354 if (replay_esn
->oseq_hi
== 0) {
356 replay_esn
->oseq_hi
--;
357 xfrm_audit_state_replay_overflow(x
, skb
);
363 if (xfrm_aevent_is_on(net
))
364 x
->repl
->notify(x
, XFRM_REPLAY_UPDATE
);
370 static int xfrm_replay_check_esn(struct xfrm_state
*x
,
371 struct sk_buff
*skb
, __be32 net_seq
)
373 unsigned int bitnr
, nr
;
375 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
376 u32 seq
= ntohl(net_seq
);
377 u32 pos
= (replay_esn
->seq
- 1) % replay_esn
->replay_window
;
378 u32 wsize
= replay_esn
->replay_window
;
379 u32 top
= replay_esn
->seq
;
380 u32 bottom
= top
- wsize
+ 1;
382 if (unlikely(seq
== 0 && replay_esn
->seq_hi
== 0 &&
383 (replay_esn
->seq
< replay_esn
->replay_window
- 1)))
388 if (likely(top
>= wsize
- 1)) {
389 /* A. same subspace */
390 if (likely(seq
> top
) || seq
< bottom
)
393 /* B. window spans two subspaces */
394 if (likely(seq
> top
&& seq
< bottom
))
397 diff
= ~seq
+ top
+ 1;
400 if (diff
>= replay_esn
->replay_window
) {
401 x
->stats
.replay_window
++;
406 bitnr
= (pos
- diff
) % replay_esn
->replay_window
;
408 bitnr
= bitnr
& 0x1F;
409 if (replay_esn
->bmp
[nr
] & (1U << bitnr
))
412 bitnr
= replay_esn
->replay_window
- (diff
- pos
);
414 bitnr
= bitnr
& 0x1F;
415 if (replay_esn
->bmp
[nr
] & (1U << bitnr
))
423 xfrm_audit_state_replay(x
, skb
, net_seq
);
427 static void xfrm_replay_advance_esn(struct xfrm_state
*x
, __be32 net_seq
)
429 unsigned int bitnr
, nr
, i
;
431 u32 diff
, pos
, seq
, seq_hi
;
432 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
434 if (!replay_esn
->replay_window
)
437 seq
= ntohl(net_seq
);
438 pos
= (replay_esn
->seq
- 1) % replay_esn
->replay_window
;
439 seq_hi
= xfrm_replay_seqhi(x
, net_seq
);
440 wrap
= seq_hi
- replay_esn
->seq_hi
;
442 if ((!wrap
&& seq
> replay_esn
->seq
) || wrap
> 0) {
444 diff
= seq
- replay_esn
->seq
;
446 diff
= ~replay_esn
->seq
+ seq
+ 1;
448 if (diff
< replay_esn
->replay_window
) {
449 for (i
= 1; i
< diff
; i
++) {
450 bitnr
= (pos
+ i
) % replay_esn
->replay_window
;
452 bitnr
= bitnr
& 0x1F;
453 replay_esn
->bmp
[nr
] &= ~(1U << bitnr
);
456 bitnr
= (pos
+ diff
) % replay_esn
->replay_window
;
458 bitnr
= bitnr
& 0x1F;
459 replay_esn
->bmp
[nr
] |= (1U << bitnr
);
461 nr
= replay_esn
->replay_window
>> 5;
462 for (i
= 0; i
<= nr
; i
++)
463 replay_esn
->bmp
[i
] = 0;
465 bitnr
= (pos
+ diff
) % replay_esn
->replay_window
;
467 bitnr
= bitnr
& 0x1F;
468 replay_esn
->bmp
[nr
] |= (1U << bitnr
);
471 replay_esn
->seq
= seq
;
473 if (unlikely(wrap
> 0))
474 replay_esn
->seq_hi
++;
476 diff
= replay_esn
->seq
- seq
;
479 bitnr
= (pos
- diff
) % replay_esn
->replay_window
;
481 bitnr
= bitnr
& 0x1F;
482 replay_esn
->bmp
[nr
] |= (1U << bitnr
);
484 bitnr
= replay_esn
->replay_window
- (diff
- pos
);
486 bitnr
= bitnr
& 0x1F;
487 replay_esn
->bmp
[nr
] |= (1U << bitnr
);
491 if (xfrm_aevent_is_on(xs_net(x
)))
492 xfrm_replay_notify(x
, XFRM_REPLAY_UPDATE
);
495 static struct xfrm_replay xfrm_replay_legacy
= {
496 .advance
= xfrm_replay_advance
,
497 .check
= xfrm_replay_check
,
498 .notify
= xfrm_replay_notify
,
499 .overflow
= xfrm_replay_overflow
,
502 static struct xfrm_replay xfrm_replay_bmp
= {
503 .advance
= xfrm_replay_advance_bmp
,
504 .check
= xfrm_replay_check_bmp
,
505 .notify
= xfrm_replay_notify_bmp
,
506 .overflow
= xfrm_replay_overflow_bmp
,
509 static struct xfrm_replay xfrm_replay_esn
= {
510 .advance
= xfrm_replay_advance_esn
,
511 .check
= xfrm_replay_check_esn
,
512 .notify
= xfrm_replay_notify_bmp
,
513 .overflow
= xfrm_replay_overflow_esn
,
516 int xfrm_init_replay(struct xfrm_state
*x
)
518 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
521 if (replay_esn
->replay_window
>
522 replay_esn
->bmp_len
* sizeof(__u32
))
525 if ((x
->props
.flags
& XFRM_STATE_ESN
) && x
->replay_esn
)
526 x
->repl
= &xfrm_replay_esn
;
528 x
->repl
= &xfrm_replay_bmp
;
530 x
->repl
= &xfrm_replay_legacy
;
534 EXPORT_SYMBOL(xfrm_init_replay
);