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 (!x
->props
.replay_window
)
124 if (unlikely(seq
== 0))
127 if (likely(seq
> x
->replay
.seq
))
130 diff
= x
->replay
.seq
- seq
;
131 if (diff
>= min_t(unsigned int, x
->props
.replay_window
,
132 sizeof(x
->replay
.bitmap
) * 8)) {
133 x
->stats
.replay_window
++;
137 if (x
->replay
.bitmap
& (1U << diff
)) {
144 xfrm_audit_state_replay(x
, skb
, net_seq
);
148 static void xfrm_replay_advance(struct xfrm_state
*x
, __be32 net_seq
)
151 u32 seq
= ntohl(net_seq
);
153 if (!x
->props
.replay_window
)
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;
161 x
->replay
.bitmap
= 1;
164 diff
= x
->replay
.seq
- seq
;
165 x
->replay
.bitmap
|= (1U << diff
);
168 if (xfrm_aevent_is_on(xs_net(x
)))
169 xfrm_replay_notify(x
, XFRM_REPLAY_UPDATE
);
172 static int xfrm_replay_overflow_bmp(struct xfrm_state
*x
, struct sk_buff
*skb
)
175 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
176 struct net
*net
= xs_net(x
);
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)) {
182 xfrm_audit_state_replay_overflow(x
, skb
);
187 if (xfrm_aevent_is_on(net
))
188 x
->repl
->notify(x
, XFRM_REPLAY_UPDATE
);
194 static int xfrm_replay_check_bmp(struct xfrm_state
*x
,
195 struct sk_buff
*skb
, __be32 net_seq
)
197 unsigned int bitnr
, nr
;
198 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
200 u32 seq
= ntohl(net_seq
);
201 u32 diff
= replay_esn
->seq
- seq
;
203 if (!replay_esn
->replay_window
)
206 if (unlikely(seq
== 0))
209 if (likely(seq
> replay_esn
->seq
))
212 if (diff
>= replay_esn
->replay_window
) {
213 x
->stats
.replay_window
++;
217 pos
= (replay_esn
->seq
- 1) % replay_esn
->replay_window
;
220 bitnr
= (pos
- diff
) % replay_esn
->replay_window
;
222 bitnr
= replay_esn
->replay_window
- (diff
- pos
);
225 bitnr
= bitnr
& 0x1F;
226 if (replay_esn
->bmp
[nr
] & (1U << bitnr
))
234 xfrm_audit_state_replay(x
, skb
, net_seq
);
238 static void xfrm_replay_advance_bmp(struct xfrm_state
*x
, __be32 net_seq
)
240 unsigned int bitnr
, nr
, i
;
242 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
243 u32 seq
= ntohl(net_seq
);
244 u32 pos
= (replay_esn
->seq
- 1) % replay_esn
->replay_window
;
246 if (!replay_esn
->replay_window
)
249 if (seq
> replay_esn
->seq
) {
250 diff
= seq
- replay_esn
->seq
;
252 if (diff
< replay_esn
->replay_window
) {
253 for (i
= 1; i
< diff
; i
++) {
254 bitnr
= (pos
+ i
) % replay_esn
->replay_window
;
256 bitnr
= bitnr
& 0x1F;
257 replay_esn
->bmp
[nr
] &= ~(1U << bitnr
);
260 nr
= (replay_esn
->replay_window
- 1) >> 5;
261 for (i
= 0; i
<= nr
; i
++)
262 replay_esn
->bmp
[i
] = 0;
265 bitnr
= (pos
+ diff
) % replay_esn
->replay_window
;
266 replay_esn
->seq
= seq
;
268 diff
= replay_esn
->seq
- seq
;
271 bitnr
= (pos
- diff
) % replay_esn
->replay_window
;
273 bitnr
= replay_esn
->replay_window
- (diff
- pos
);
277 bitnr
= bitnr
& 0x1F;
278 replay_esn
->bmp
[nr
] |= (1U << bitnr
);
280 if (xfrm_aevent_is_on(xs_net(x
)))
281 xfrm_replay_notify(x
, XFRM_REPLAY_UPDATE
);
284 static void xfrm_replay_notify_bmp(struct xfrm_state
*x
, int event
)
287 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
288 struct xfrm_replay_state_esn
*preplay_esn
= x
->preplay_esn
;
290 /* we send notify messages in case
291 * 1. we updated on of the sequence numbers, and the seqno difference
292 * is at least x->replay_maxdiff, in this case we also update the
293 * timeout of our timer function
294 * 2. if x->replay_maxage has elapsed since last update,
295 * and there were changes
297 * The state structure must be locked!
301 case XFRM_REPLAY_UPDATE
:
302 if (x
->replay_maxdiff
&&
303 (replay_esn
->seq
- preplay_esn
->seq
< x
->replay_maxdiff
) &&
304 (replay_esn
->oseq
- preplay_esn
->oseq
< x
->replay_maxdiff
)) {
305 if (x
->xflags
& XFRM_TIME_DEFER
)
306 event
= XFRM_REPLAY_TIMEOUT
;
313 case XFRM_REPLAY_TIMEOUT
:
314 if (memcmp(x
->replay_esn
, x
->preplay_esn
,
315 xfrm_replay_state_esn_len(replay_esn
)) == 0) {
316 x
->xflags
|= XFRM_TIME_DEFER
;
323 memcpy(x
->preplay_esn
, x
->replay_esn
,
324 xfrm_replay_state_esn_len(replay_esn
));
325 c
.event
= XFRM_MSG_NEWAE
;
326 c
.data
.aevent
= event
;
327 km_state_notify(x
, &c
);
329 if (x
->replay_maxage
&&
330 !mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
))
331 x
->xflags
&= ~XFRM_TIME_DEFER
;
334 static int xfrm_replay_overflow_esn(struct xfrm_state
*x
, struct sk_buff
*skb
)
337 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
338 struct net
*net
= xs_net(x
);
340 if (x
->type
->flags
& XFRM_TYPE_REPLAY_PROT
) {
341 XFRM_SKB_CB(skb
)->seq
.output
.low
= ++replay_esn
->oseq
;
342 XFRM_SKB_CB(skb
)->seq
.output
.hi
= replay_esn
->oseq_hi
;
344 if (unlikely(replay_esn
->oseq
== 0)) {
345 XFRM_SKB_CB(skb
)->seq
.output
.hi
= ++replay_esn
->oseq_hi
;
347 if (replay_esn
->oseq_hi
== 0) {
349 replay_esn
->oseq_hi
--;
350 xfrm_audit_state_replay_overflow(x
, skb
);
356 if (xfrm_aevent_is_on(net
))
357 x
->repl
->notify(x
, XFRM_REPLAY_UPDATE
);
363 static int xfrm_replay_check_esn(struct xfrm_state
*x
,
364 struct sk_buff
*skb
, __be32 net_seq
)
366 unsigned int bitnr
, nr
;
368 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
370 u32 seq
= ntohl(net_seq
);
371 u32 wsize
= replay_esn
->replay_window
;
372 u32 top
= replay_esn
->seq
;
373 u32 bottom
= top
- wsize
+ 1;
378 if (unlikely(seq
== 0 && replay_esn
->seq_hi
== 0 &&
379 (replay_esn
->seq
< replay_esn
->replay_window
- 1)))
384 if (likely(top
>= wsize
- 1)) {
385 /* A. same subspace */
386 if (likely(seq
> top
) || seq
< bottom
)
389 /* B. window spans two subspaces */
390 if (likely(seq
> top
&& seq
< bottom
))
393 diff
= ~seq
+ top
+ 1;
396 if (diff
>= replay_esn
->replay_window
) {
397 x
->stats
.replay_window
++;
401 pos
= (replay_esn
->seq
- 1) % replay_esn
->replay_window
;
404 bitnr
= (pos
- diff
) % replay_esn
->replay_window
;
406 bitnr
= replay_esn
->replay_window
- (diff
- pos
);
409 bitnr
= bitnr
& 0x1F;
410 if (replay_esn
->bmp
[nr
] & (1U << bitnr
))
418 xfrm_audit_state_replay(x
, skb
, net_seq
);
422 static void xfrm_replay_advance_esn(struct xfrm_state
*x
, __be32 net_seq
)
424 unsigned int bitnr
, nr
, i
;
426 u32 diff
, pos
, seq
, seq_hi
;
427 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
429 if (!replay_esn
->replay_window
)
432 seq
= ntohl(net_seq
);
433 pos
= (replay_esn
->seq
- 1) % replay_esn
->replay_window
;
434 seq_hi
= xfrm_replay_seqhi(x
, net_seq
);
435 wrap
= seq_hi
- replay_esn
->seq_hi
;
437 if ((!wrap
&& seq
> replay_esn
->seq
) || wrap
> 0) {
439 diff
= seq
- replay_esn
->seq
;
441 diff
= ~replay_esn
->seq
+ seq
+ 1;
443 if (diff
< replay_esn
->replay_window
) {
444 for (i
= 1; i
< diff
; i
++) {
445 bitnr
= (pos
+ i
) % replay_esn
->replay_window
;
447 bitnr
= bitnr
& 0x1F;
448 replay_esn
->bmp
[nr
] &= ~(1U << bitnr
);
451 nr
= (replay_esn
->replay_window
- 1) >> 5;
452 for (i
= 0; i
<= nr
; i
++)
453 replay_esn
->bmp
[i
] = 0;
456 bitnr
= (pos
+ diff
) % replay_esn
->replay_window
;
457 replay_esn
->seq
= seq
;
459 if (unlikely(wrap
> 0))
460 replay_esn
->seq_hi
++;
462 diff
= replay_esn
->seq
- seq
;
465 bitnr
= (pos
- diff
) % replay_esn
->replay_window
;
467 bitnr
= replay_esn
->replay_window
- (diff
- pos
);
471 bitnr
= bitnr
& 0x1F;
472 replay_esn
->bmp
[nr
] |= (1U << bitnr
);
474 if (xfrm_aevent_is_on(xs_net(x
)))
475 xfrm_replay_notify(x
, XFRM_REPLAY_UPDATE
);
478 static struct xfrm_replay xfrm_replay_legacy
= {
479 .advance
= xfrm_replay_advance
,
480 .check
= xfrm_replay_check
,
481 .notify
= xfrm_replay_notify
,
482 .overflow
= xfrm_replay_overflow
,
485 static struct xfrm_replay xfrm_replay_bmp
= {
486 .advance
= xfrm_replay_advance_bmp
,
487 .check
= xfrm_replay_check_bmp
,
488 .notify
= xfrm_replay_notify_bmp
,
489 .overflow
= xfrm_replay_overflow_bmp
,
492 static struct xfrm_replay xfrm_replay_esn
= {
493 .advance
= xfrm_replay_advance_esn
,
494 .check
= xfrm_replay_check_esn
,
495 .notify
= xfrm_replay_notify_bmp
,
496 .overflow
= xfrm_replay_overflow_esn
,
499 int xfrm_init_replay(struct xfrm_state
*x
)
501 struct xfrm_replay_state_esn
*replay_esn
= x
->replay_esn
;
504 if (replay_esn
->replay_window
>
505 replay_esn
->bmp_len
* sizeof(__u32
) * 8)
508 if ((x
->props
.flags
& XFRM_STATE_ESN
) && replay_esn
->replay_window
== 0)
511 if ((x
->props
.flags
& XFRM_STATE_ESN
) && x
->replay_esn
)
512 x
->repl
= &xfrm_replay_esn
;
514 x
->repl
= &xfrm_replay_bmp
;
516 x
->repl
= &xfrm_replay_legacy
;
520 EXPORT_SYMBOL(xfrm_init_replay
);