FRV: Use generic show_interrupts()
[cris-mirror.git] / net / xfrm / xfrm_replay.c
blob2f5be5b15740507fac23201330899faf949055a0
1 /*
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
14 * more details.
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.
21 #include <net/xfrm.h>
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))
29 return 0;
31 seq = ntohl(net_seq);
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))
38 seq_hi++;
39 } else {
40 /* B. window spans two subspaces */
41 if (unlikely(seq >= bottom))
42 seq_hi--;
45 return seq_hi;
48 static void xfrm_replay_notify(struct xfrm_state *x, int event)
50 struct km_event c;
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!
61 switch (event) {
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;
68 else
69 return;
72 break;
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;
78 return;
81 break;
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)
96 int err = 0;
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)) {
102 x->replay.oseq--;
103 xfrm_audit_state_replay_overflow(x, skb);
104 err = -EOVERFLOW;
106 return err;
108 if (xfrm_aevent_is_on(net))
109 x->repl->notify(x, XFRM_REPLAY_UPDATE);
112 return err;
115 static int xfrm_replay_check(struct xfrm_state *x,
116 struct sk_buff *skb, __be32 net_seq)
118 u32 diff;
119 u32 seq = ntohl(net_seq);
121 if (unlikely(seq == 0))
122 goto err;
124 if (likely(seq > x->replay.seq))
125 return 0;
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++;
131 goto err;
134 if (x->replay.bitmap & (1U << diff)) {
135 x->stats.replay++;
136 goto err;
138 return 0;
140 err:
141 xfrm_audit_state_replay(x, skb, net_seq);
142 return -EINVAL;
145 static void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
147 u32 diff;
148 u32 seq = ntohl(net_seq);
150 if (!x->props.replay_window)
151 return;
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;
157 else
158 x->replay.bitmap = 1;
159 x->replay.seq = seq;
160 } else {
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)
171 int err = 0;
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)) {
178 replay_esn->oseq--;
179 xfrm_audit_state_replay_overflow(x, skb);
180 err = -EOVERFLOW;
182 return err;
184 if (xfrm_aevent_is_on(net))
185 x->repl->notify(x, XFRM_REPLAY_UPDATE);
188 return err;
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))
201 goto err;
203 if (likely(seq > replay_esn->seq))
204 return 0;
206 if (diff >= replay_esn->replay_window) {
207 x->stats.replay_window++;
208 goto err;
211 if (pos >= diff) {
212 bitnr = (pos - diff) % replay_esn->replay_window;
213 nr = bitnr >> 5;
214 bitnr = bitnr & 0x1F;
215 if (replay_esn->bmp[nr] & (1U << bitnr))
216 goto err_replay;
217 } else {
218 bitnr = replay_esn->replay_window - (diff - pos);
219 nr = bitnr >> 5;
220 bitnr = bitnr & 0x1F;
221 if (replay_esn->bmp[nr] & (1U << bitnr))
222 goto err_replay;
224 return 0;
226 err_replay:
227 x->stats.replay++;
228 err:
229 xfrm_audit_state_replay(x, skb, net_seq);
230 return -EINVAL;
233 static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq)
235 unsigned int bitnr, nr, i;
236 u32 diff;
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)
242 return;
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;
250 nr = bitnr >> 5;
251 bitnr = bitnr & 0x1F;
252 replay_esn->bmp[nr] &= ~(1U << bitnr);
255 bitnr = (pos + diff) % replay_esn->replay_window;
256 nr = bitnr >> 5;
257 bitnr = bitnr & 0x1F;
258 replay_esn->bmp[nr] |= (1U << bitnr);
259 } else {
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;
265 nr = bitnr >> 5;
266 bitnr = bitnr & 0x1F;
267 replay_esn->bmp[nr] |= (1U << bitnr);
270 replay_esn->seq = seq;
271 } else {
272 diff = replay_esn->seq - seq;
274 if (pos >= diff) {
275 bitnr = (pos - diff) % replay_esn->replay_window;
276 nr = bitnr >> 5;
277 bitnr = bitnr & 0x1F;
278 replay_esn->bmp[nr] |= (1U << bitnr);
279 } else {
280 bitnr = replay_esn->replay_window - (diff - pos);
281 nr = bitnr >> 5;
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)
293 struct km_event c;
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!
307 switch (event) {
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;
314 else
315 return;
318 break;
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;
324 return;
327 break;
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)
343 int err = 0;
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) {
355 replay_esn->oseq--;
356 replay_esn->oseq_hi--;
357 xfrm_audit_state_replay_overflow(x, skb);
358 err = -EOVERFLOW;
360 return err;
363 if (xfrm_aevent_is_on(net))
364 x->repl->notify(x, XFRM_REPLAY_UPDATE);
367 return err;
370 static int xfrm_replay_check_esn(struct xfrm_state *x,
371 struct sk_buff *skb, __be32 net_seq)
373 unsigned int bitnr, nr;
374 u32 diff;
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)))
384 goto err;
386 diff = top - seq;
388 if (likely(top >= wsize - 1)) {
389 /* A. same subspace */
390 if (likely(seq > top) || seq < bottom)
391 return 0;
392 } else {
393 /* B. window spans two subspaces */
394 if (likely(seq > top && seq < bottom))
395 return 0;
396 if (seq >= bottom)
397 diff = ~seq + top + 1;
400 if (diff >= replay_esn->replay_window) {
401 x->stats.replay_window++;
402 goto err;
405 if (pos >= diff) {
406 bitnr = (pos - diff) % replay_esn->replay_window;
407 nr = bitnr >> 5;
408 bitnr = bitnr & 0x1F;
409 if (replay_esn->bmp[nr] & (1U << bitnr))
410 goto err_replay;
411 } else {
412 bitnr = replay_esn->replay_window - (diff - pos);
413 nr = bitnr >> 5;
414 bitnr = bitnr & 0x1F;
415 if (replay_esn->bmp[nr] & (1U << bitnr))
416 goto err_replay;
418 return 0;
420 err_replay:
421 x->stats.replay++;
422 err:
423 xfrm_audit_state_replay(x, skb, net_seq);
424 return -EINVAL;
427 static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq)
429 unsigned int bitnr, nr, i;
430 int wrap;
431 u32 diff, pos, seq, seq_hi;
432 struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
434 if (!replay_esn->replay_window)
435 return;
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) {
443 if (likely(!wrap))
444 diff = seq - replay_esn->seq;
445 else
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;
451 nr = bitnr >> 5;
452 bitnr = bitnr & 0x1F;
453 replay_esn->bmp[nr] &= ~(1U << bitnr);
456 bitnr = (pos + diff) % replay_esn->replay_window;
457 nr = bitnr >> 5;
458 bitnr = bitnr & 0x1F;
459 replay_esn->bmp[nr] |= (1U << bitnr);
460 } else {
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;
466 nr = bitnr >> 5;
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++;
475 } else {
476 diff = replay_esn->seq - seq;
478 if (pos >= diff) {
479 bitnr = (pos - diff) % replay_esn->replay_window;
480 nr = bitnr >> 5;
481 bitnr = bitnr & 0x1F;
482 replay_esn->bmp[nr] |= (1U << bitnr);
483 } else {
484 bitnr = replay_esn->replay_window - (diff - pos);
485 nr = bitnr >> 5;
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;
520 if (replay_esn) {
521 if (replay_esn->replay_window >
522 replay_esn->bmp_len * sizeof(__u32))
523 return -EINVAL;
525 if ((x->props.flags & XFRM_STATE_ESN) && x->replay_esn)
526 x->repl = &xfrm_replay_esn;
527 else
528 x->repl = &xfrm_replay_bmp;
529 } else
530 x->repl = &xfrm_replay_legacy;
532 return 0;
534 EXPORT_SYMBOL(xfrm_init_replay);