1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001 Intel Corp.
8 * This file is part of the SCTP kernel implementation
10 * These functions manipulate sctp tsn mapping array.
12 * Please send any bug reports or fixes you make to the
14 * lksctp developers <linux-sctp@vger.kernel.org>
16 * Written or modified by:
17 * La Monte H.P. Yarroll <piggy@acm.org>
18 * Jon Grimm <jgrimm@us.ibm.com>
19 * Karl Knutson <karl@athena.chicago.il.us>
20 * Sridhar Samudrala <sri@us.ibm.com>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 #include <linux/bitmap.h>
26 #include <net/sctp/sctp.h>
27 #include <net/sctp/sm.h>
29 static void sctp_tsnmap_update(struct sctp_tsnmap
*map
);
30 static void sctp_tsnmap_find_gap_ack(unsigned long *map
, __u16 off
,
31 __u16 len
, __u16
*start
, __u16
*end
);
32 static int sctp_tsnmap_grow(struct sctp_tsnmap
*map
, u16 size
);
34 /* Initialize a block of memory as a tsnmap. */
35 struct sctp_tsnmap
*sctp_tsnmap_init(struct sctp_tsnmap
*map
, __u16 len
,
36 __u32 initial_tsn
, gfp_t gfp
)
39 map
->tsn_map
= kzalloc(len
>>3, gfp
);
40 if (map
->tsn_map
== NULL
)
45 bitmap_zero(map
->tsn_map
, map
->len
);
48 /* Keep track of TSNs represented by tsn_map. */
49 map
->base_tsn
= initial_tsn
;
50 map
->cumulative_tsn_ack_point
= initial_tsn
- 1;
51 map
->max_tsn_seen
= map
->cumulative_tsn_ack_point
;
52 map
->num_dup_tsns
= 0;
57 void sctp_tsnmap_free(struct sctp_tsnmap
*map
)
63 /* Test the tracking state of this TSN.
65 * 0 if the TSN has not yet been seen
66 * >0 if the TSN has been seen (duplicate)
67 * <0 if the TSN is invalid (too large to track)
69 int sctp_tsnmap_check(const struct sctp_tsnmap
*map
, __u32 tsn
)
73 /* Check to see if this is an old TSN */
74 if (TSN_lte(tsn
, map
->cumulative_tsn_ack_point
))
77 /* Verify that we can hold this TSN and that it will not
80 if (!TSN_lt(tsn
, map
->base_tsn
+ SCTP_TSN_MAP_SIZE
))
83 /* Calculate the index into the mapping arrays. */
84 gap
= tsn
- map
->base_tsn
;
86 /* Check to see if TSN has already been recorded. */
87 if (gap
< map
->len
&& test_bit(gap
, map
->tsn_map
))
94 /* Mark this TSN as seen. */
95 int sctp_tsnmap_mark(struct sctp_tsnmap
*map
, __u32 tsn
,
96 struct sctp_transport
*trans
)
100 if (TSN_lt(tsn
, map
->base_tsn
))
103 gap
= tsn
- map
->base_tsn
;
105 if (gap
>= map
->len
&& !sctp_tsnmap_grow(map
, gap
+ 1))
108 if (!sctp_tsnmap_has_gap(map
) && gap
== 0) {
109 /* In this case the map has no gaps and the tsn we are
110 * recording is the next expected tsn. We don't touch
111 * the map but simply bump the values.
114 map
->cumulative_tsn_ack_point
++;
116 trans
->sack_generation
=
117 trans
->asoc
->peer
.sack_generation
;
120 /* Either we already have a gap, or about to record a gap, so
125 if (TSN_lt(map
->max_tsn_seen
, tsn
))
126 map
->max_tsn_seen
= tsn
;
128 /* Mark the TSN as received. */
129 set_bit(gap
, map
->tsn_map
);
131 /* Go fixup any internal TSN mapping variables including
132 * cumulative_tsn_ack_point.
134 sctp_tsnmap_update(map
);
141 /* Initialize a Gap Ack Block iterator from memory being provided. */
142 static void sctp_tsnmap_iter_init(const struct sctp_tsnmap
*map
,
143 struct sctp_tsnmap_iter
*iter
)
145 /* Only start looking one past the Cumulative TSN Ack Point. */
146 iter
->start
= map
->cumulative_tsn_ack_point
+ 1;
149 /* Get the next Gap Ack Blocks. Returns 0 if there was not another block
152 static int sctp_tsnmap_next_gap_ack(const struct sctp_tsnmap
*map
,
153 struct sctp_tsnmap_iter
*iter
,
154 __u16
*start
, __u16
*end
)
157 __u16 start_
= 0, end_
= 0, offset
;
159 /* If there are no more gap acks possible, get out fast. */
160 if (TSN_lte(map
->max_tsn_seen
, iter
->start
))
163 offset
= iter
->start
- map
->base_tsn
;
164 sctp_tsnmap_find_gap_ack(map
->tsn_map
, offset
, map
->len
,
167 /* The Gap Ack Block happens to end at the end of the map. */
171 /* If we found a Gap Ack Block, return the start and end and
172 * bump the iterator forward.
175 /* Fix up the start and end based on the
176 * Cumulative TSN Ack which is always 1 behind base.
181 /* Move the iterator forward. */
182 iter
->start
= map
->cumulative_tsn_ack_point
+ *end
+ 1;
189 /* Mark this and any lower TSN as seen. */
190 void sctp_tsnmap_skip(struct sctp_tsnmap
*map
, __u32 tsn
)
194 if (TSN_lt(tsn
, map
->base_tsn
))
196 if (!TSN_lt(tsn
, map
->base_tsn
+ SCTP_TSN_MAP_SIZE
))
200 if (TSN_lt(map
->max_tsn_seen
, tsn
))
201 map
->max_tsn_seen
= tsn
;
203 gap
= tsn
- map
->base_tsn
+ 1;
205 map
->base_tsn
+= gap
;
206 map
->cumulative_tsn_ack_point
+= gap
;
207 if (gap
>= map
->len
) {
208 /* If our gap is larger then the map size, just
211 bitmap_zero(map
->tsn_map
, map
->len
);
213 /* If the gap is smaller than the map size,
214 * shift the map by 'gap' bits and update further.
216 bitmap_shift_right(map
->tsn_map
, map
->tsn_map
, gap
, map
->len
);
217 sctp_tsnmap_update(map
);
221 /********************************************************************
222 * 2nd Level Abstractions
223 ********************************************************************/
225 /* This private helper function updates the tsnmap buffers and
226 * the Cumulative TSN Ack Point.
228 static void sctp_tsnmap_update(struct sctp_tsnmap
*map
)
231 unsigned long zero_bit
;
234 len
= map
->max_tsn_seen
- map
->cumulative_tsn_ack_point
;
235 zero_bit
= find_first_zero_bit(map
->tsn_map
, len
);
237 return; /* The first 0-bit is bit 0. nothing to do */
239 map
->base_tsn
+= zero_bit
;
240 map
->cumulative_tsn_ack_point
+= zero_bit
;
242 bitmap_shift_right(map
->tsn_map
, map
->tsn_map
, zero_bit
, map
->len
);
245 /* How many data chunks are we missing from our peer?
247 __u16
sctp_tsnmap_pending(struct sctp_tsnmap
*map
)
249 __u32 cum_tsn
= map
->cumulative_tsn_ack_point
;
250 __u32 max_tsn
= map
->max_tsn_seen
;
251 __u32 base_tsn
= map
->base_tsn
;
255 pending_data
= max_tsn
- cum_tsn
;
256 gap
= max_tsn
- base_tsn
;
258 if (gap
== 0 || gap
>= map
->len
)
261 pending_data
-= bitmap_weight(map
->tsn_map
, gap
+ 1);
266 /* This is a private helper for finding Gap Ack Blocks. It searches a
267 * single array for the start and end of a Gap Ack Block.
269 * The flags "started" and "ended" tell is if we found the beginning
270 * or (respectively) the end of a Gap Ack Block.
272 static void sctp_tsnmap_find_gap_ack(unsigned long *map
, __u16 off
,
273 __u16 len
, __u16
*start
, __u16
*end
)
277 /* Look through the entire array, but break out
278 * early if we have found the end of the Gap Ack Block.
281 /* Also, stop looking past the maximum TSN seen. */
283 /* Look for the start. */
284 i
= find_next_bit(map
, len
, off
);
288 /* Look for the end. */
290 /* We have found the start, let's find the
291 * end. If we find the end, break out.
293 i
= find_next_zero_bit(map
, len
, i
);
299 /* Renege that we have seen a TSN. */
300 void sctp_tsnmap_renege(struct sctp_tsnmap
*map
, __u32 tsn
)
304 if (TSN_lt(tsn
, map
->base_tsn
))
306 /* Assert: TSN is in range. */
307 if (!TSN_lt(tsn
, map
->base_tsn
+ map
->len
))
310 gap
= tsn
- map
->base_tsn
;
312 /* Pretend we never saw the TSN. */
313 clear_bit(gap
, map
->tsn_map
);
316 /* How many gap ack blocks do we have recorded? */
317 __u16
sctp_tsnmap_num_gabs(struct sctp_tsnmap
*map
,
318 struct sctp_gap_ack_block
*gabs
)
320 struct sctp_tsnmap_iter iter
;
323 /* Refresh the gap ack information. */
324 if (sctp_tsnmap_has_gap(map
)) {
325 __u16 start
= 0, end
= 0;
326 sctp_tsnmap_iter_init(map
, &iter
);
327 while (sctp_tsnmap_next_gap_ack(map
, &iter
,
331 gabs
[ngaps
].start
= htons(start
);
332 gabs
[ngaps
].end
= htons(end
);
334 if (ngaps
>= SCTP_MAX_GABS
)
341 static int sctp_tsnmap_grow(struct sctp_tsnmap
*map
, u16 size
)
347 if (size
> SCTP_TSN_MAP_SIZE
)
350 inc
= ALIGN((size
- map
->len
), BITS_PER_LONG
) + SCTP_TSN_MAP_INCREMENT
;
351 len
= min_t(u16
, map
->len
+ inc
, SCTP_TSN_MAP_SIZE
);
353 new = kzalloc(len
>>3, GFP_ATOMIC
);
357 bitmap_copy(new, map
->tsn_map
,
358 map
->max_tsn_seen
- map
->cumulative_tsn_ack_point
);