1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45#include <linux/slab.h>
46#include <linux/types.h>
47#include <linux/bitmap.h>
48#include <net/sctp/sctp.h>
49#include <net/sctp/sm.h>
50
51static void sctp_tsnmap_update(struct sctp_tsnmap *map);
52static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
53 __u16 len, __u16 *start, __u16 *end);
54static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 gap);
55
56
57struct sctp_tsnmap *sctp_tsnmap_init(struct sctp_tsnmap *map, __u16 len,
58 __u32 initial_tsn, gfp_t gfp)
59{
60 if (!map->tsn_map) {
61 map->tsn_map = kzalloc(len>>3, gfp);
62 if (map->tsn_map == NULL)
63 return NULL;
64
65 map->len = len;
66 } else {
67 bitmap_zero(map->tsn_map, map->len);
68 }
69
70
71 map->base_tsn = initial_tsn;
72 map->cumulative_tsn_ack_point = initial_tsn - 1;
73 map->max_tsn_seen = map->cumulative_tsn_ack_point;
74 map->num_dup_tsns = 0;
75
76 return map;
77}
78
79void sctp_tsnmap_free(struct sctp_tsnmap *map)
80{
81 map->len = 0;
82 kfree(map->tsn_map);
83}
84
85
86
87
88
89
90
91int sctp_tsnmap_check(const struct sctp_tsnmap *map, __u32 tsn)
92{
93 u32 gap;
94
95
96 if (TSN_lte(tsn, map->cumulative_tsn_ack_point))
97 return 1;
98
99
100
101
102 if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
103 return -1;
104
105
106 gap = tsn - map->base_tsn;
107
108
109 if (gap < map->len && test_bit(gap, map->tsn_map))
110 return 1;
111 else
112 return 0;
113}
114
115
116
117int sctp_tsnmap_mark(struct sctp_tsnmap *map, __u32 tsn,
118 struct sctp_transport *trans)
119{
120 u16 gap;
121
122 if (TSN_lt(tsn, map->base_tsn))
123 return 0;
124
125 gap = tsn - map->base_tsn;
126
127 if (gap >= map->len && !sctp_tsnmap_grow(map, gap))
128 return -ENOMEM;
129
130 if (!sctp_tsnmap_has_gap(map) && gap == 0) {
131
132
133
134
135 map->max_tsn_seen++;
136 map->cumulative_tsn_ack_point++;
137 if (trans)
138 trans->sack_generation =
139 trans->asoc->peer.sack_generation;
140 map->base_tsn++;
141 } else {
142
143
144
145
146
147 if (TSN_lt(map->max_tsn_seen, tsn))
148 map->max_tsn_seen = tsn;
149
150
151 set_bit(gap, map->tsn_map);
152
153
154
155
156 sctp_tsnmap_update(map);
157 }
158
159 return 0;
160}
161
162
163
164SCTP_STATIC void sctp_tsnmap_iter_init(const struct sctp_tsnmap *map,
165 struct sctp_tsnmap_iter *iter)
166{
167
168 iter->start = map->cumulative_tsn_ack_point + 1;
169}
170
171
172
173
174SCTP_STATIC int sctp_tsnmap_next_gap_ack(const struct sctp_tsnmap *map,
175 struct sctp_tsnmap_iter *iter,
176 __u16 *start, __u16 *end)
177{
178 int ended = 0;
179 __u16 start_ = 0, end_ = 0, offset;
180
181
182 if (TSN_lte(map->max_tsn_seen, iter->start))
183 return 0;
184
185 offset = iter->start - map->base_tsn;
186 sctp_tsnmap_find_gap_ack(map->tsn_map, offset, map->len,
187 &start_, &end_);
188
189
190 if (start_ && !end_)
191 end_ = map->len - 1;
192
193
194
195
196 if (end_) {
197
198
199
200 *start = start_ + 1;
201 *end = end_ + 1;
202
203
204 iter->start = map->cumulative_tsn_ack_point + *end + 1;
205 ended = 1;
206 }
207
208 return ended;
209}
210
211
212void sctp_tsnmap_skip(struct sctp_tsnmap *map, __u32 tsn)
213{
214 u32 gap;
215
216 if (TSN_lt(tsn, map->base_tsn))
217 return;
218 if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
219 return;
220
221
222 if (TSN_lt(map->max_tsn_seen, tsn))
223 map->max_tsn_seen = tsn;
224
225 gap = tsn - map->base_tsn + 1;
226
227 map->base_tsn += gap;
228 map->cumulative_tsn_ack_point += gap;
229 if (gap >= map->len) {
230
231
232
233 bitmap_zero(map->tsn_map, map->len);
234 } else {
235
236
237
238 bitmap_shift_right(map->tsn_map, map->tsn_map, gap, map->len);
239 sctp_tsnmap_update(map);
240 }
241}
242
243
244
245
246
247
248
249
250static void sctp_tsnmap_update(struct sctp_tsnmap *map)
251{
252 u16 len;
253 unsigned long zero_bit;
254
255
256 len = map->max_tsn_seen - map->cumulative_tsn_ack_point;
257 zero_bit = find_first_zero_bit(map->tsn_map, len);
258 if (!zero_bit)
259 return;
260
261 map->base_tsn += zero_bit;
262 map->cumulative_tsn_ack_point += zero_bit;
263
264 bitmap_shift_right(map->tsn_map, map->tsn_map, zero_bit, map->len);
265}
266
267
268
269__u16 sctp_tsnmap_pending(struct sctp_tsnmap *map)
270{
271 __u32 cum_tsn = map->cumulative_tsn_ack_point;
272 __u32 max_tsn = map->max_tsn_seen;
273 __u32 base_tsn = map->base_tsn;
274 __u16 pending_data;
275 u32 gap, i;
276
277 pending_data = max_tsn - cum_tsn;
278 gap = max_tsn - base_tsn;
279
280 if (gap == 0 || gap >= map->len)
281 goto out;
282
283 for (i = 0; i < gap+1; i++) {
284 if (test_bit(i, map->tsn_map))
285 pending_data--;
286 }
287
288out:
289 return pending_data;
290}
291
292
293
294
295
296
297
298static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
299 __u16 len, __u16 *start, __u16 *end)
300{
301 int i = off;
302
303
304
305
306
307
308
309
310 i = find_next_bit(map, len, off);
311 if (i < len)
312 *start = i;
313
314
315 if (*start) {
316
317
318
319 i = find_next_zero_bit(map, len, i);
320 if (i < len)
321 *end = i - 1;
322 }
323}
324
325
326void sctp_tsnmap_renege(struct sctp_tsnmap *map, __u32 tsn)
327{
328 u32 gap;
329
330 if (TSN_lt(tsn, map->base_tsn))
331 return;
332
333 if (!TSN_lt(tsn, map->base_tsn + map->len))
334 return;
335
336 gap = tsn - map->base_tsn;
337
338
339 clear_bit(gap, map->tsn_map);
340}
341
342
343__u16 sctp_tsnmap_num_gabs(struct sctp_tsnmap *map,
344 struct sctp_gap_ack_block *gabs)
345{
346 struct sctp_tsnmap_iter iter;
347 int ngaps = 0;
348
349
350 if (sctp_tsnmap_has_gap(map)) {
351 __u16 start = 0, end = 0;
352 sctp_tsnmap_iter_init(map, &iter);
353 while (sctp_tsnmap_next_gap_ack(map, &iter,
354 &start,
355 &end)) {
356
357 gabs[ngaps].start = htons(start);
358 gabs[ngaps].end = htons(end);
359 ngaps++;
360 if (ngaps >= SCTP_MAX_GABS)
361 break;
362 }
363 }
364 return ngaps;
365}
366
367static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 gap)
368{
369 unsigned long *new;
370 unsigned long inc;
371 u16 len;
372
373 if (gap >= SCTP_TSN_MAP_SIZE)
374 return 0;
375
376 inc = ALIGN((gap - map->len),BITS_PER_LONG) + SCTP_TSN_MAP_INCREMENT;
377 len = min_t(u16, map->len + inc, SCTP_TSN_MAP_SIZE);
378
379 new = kzalloc(len>>3, GFP_ATOMIC);
380 if (!new)
381 return 0;
382
383 bitmap_copy(new, map->tsn_map, map->max_tsn_seen - map->base_tsn);
384 kfree(map->tsn_map);
385 map->tsn_map = new;
386 map->len = len;
387
388 return 1;
389}
390