4
* Implements a replay database for packet security
12
* Copyright (c) 2001-2006, Cisco Systems, Inc.
13
* All rights reserved.
15
* Redistribution and use in source and binary forms, with or without
16
* modification, are permitted provided that the following conditions
19
* Redistributions of source code must retain the above copyright
20
* notice, this list of conditions and the following disclaimer.
22
* Redistributions in binary form must reproduce the above
23
* copyright notice, this list of conditions and the following
24
* disclaimer in the documentation and/or other materials provided
25
* with the distribution.
27
* Neither the name of the Cisco Systems, Inc. nor the names of its
28
* contributors may be used to endorse or promote products derived
29
* from this software without specific prior written permission.
31
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42
* OF THE POSSIBILITY OF SUCH DAMAGE.
51
* this implementation of a replay database works as follows:
53
* window_start is the index of the first packet in the window
54
* bitmask a bit-buffer, containing the most recently entered
55
* index as the leftmost bit
59
/* rdb_init initalizes rdb */
62
rdb_init(rdb_t *rdb) {
63
v128_set_to_zero(&rdb->bitmask);
64
rdb->window_start = 0;
69
* rdb_check checks to see if index appears in rdb
73
rdb_check(const rdb_t *rdb, uint32_t index) {
75
/* if the index appears after (or at very end of) the window, its good */
76
if (index >= rdb->window_start + rdb_bits_in_bitmask)
79
/* if the index appears before the window, its bad */
80
if (index < rdb->window_start)
81
return err_status_replay_old;
83
/* otherwise, the index appears within the window, so check the bitmask */
84
if (v128_get_bit(&rdb->bitmask, (index - rdb->window_start)) == 1)
85
return err_status_replay_fail;
87
/* otherwise, the index is okay */
92
* rdb_add_index adds index to rdb_t (and does *not* check if
93
* index appears in db)
95
* this function should be called only after rdb_check has
96
* indicated that the index does not appear in the rdb, e.g., a mutex
97
* should protect the rdb between these calls
101
rdb_add_index(rdb_t *rdb, uint32_t index) {
104
/* here we *assume* that index > rdb->window_start */
106
delta = (index - rdb->window_start);
107
if (delta < rdb_bits_in_bitmask) {
109
/* if the index is within the window, set the appropriate bit */
110
v128_set_bit(&rdb->bitmask, delta);
114
delta -= rdb_bits_in_bitmask - 1;
116
/* shift the window forward by delta bits*/
117
v128_left_shift(&rdb->bitmask, delta);
118
v128_set_bit(&rdb->bitmask, rdb_bits_in_bitmask-1);
119
rdb->window_start += delta;
123
return err_status_ok;
127
rdb_increment(rdb_t *rdb) {
129
if (rdb->window_start++ > 0x7fffffff)
130
return err_status_key_expired;
131
return err_status_ok;
135
rdb_get_value(const rdb_t *rdb) {
136
return rdb->window_start;