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/types.h>
46#include <net/sctp/sctp.h>
47#include <net/sctp/sm.h>
48
49static void sctp_tsnmap_update(struct sctp_tsnmap *map);
50static void sctp_tsnmap_find_gap_ack(__u8 *map, __u16 off,
51 __u16 len, __u16 base,
52 int *started, __u16 *start,
53 int *ended, __u16 *end);
54
55
56struct sctp_tsnmap *sctp_tsnmap_init(struct sctp_tsnmap *map, __u16 len,
57 __u32 initial_tsn)
58{
59 map->tsn_map = map->raw_map;
60 map->overflow_map = map->tsn_map + len;
61 map->len = len;
62
63
64 memset(map->tsn_map, 0x00, map->len + map->len);
65
66
67 map->base_tsn = initial_tsn;
68 map->overflow_tsn = initial_tsn + map->len;
69 map->cumulative_tsn_ack_point = initial_tsn - 1;
70 map->max_tsn_seen = map->cumulative_tsn_ack_point;
71 map->malloced = 0;
72 map->num_dup_tsns = 0;
73
74 return map;
75}
76
77
78
79
80
81
82
83int sctp_tsnmap_check(const struct sctp_tsnmap *map, __u32 tsn)
84{
85 __s32 gap;
86 int dup;
87
88
89 gap = tsn - map->base_tsn;
90
91
92 if (gap >= ( map->len + map->len)) {
93 dup = -1;
94 goto out;
95 }
96
97
98
99
100
101
102
103 if (gap < 0 ||
104 (gap < map->len && map->tsn_map[gap]) ||
105 (gap >= map->len && map->overflow_map[gap - map->len]))
106 dup = 1;
107 else
108 dup = 0;
109
110out:
111 return dup;
112}
113
114
115
116void sctp_tsnmap_mark(struct sctp_tsnmap *map, __u32 tsn)
117{
118 __s32 gap;
119
120
121
122
123 if (TSN_lt(tsn, map->base_tsn))
124 return;
125 if (!TSN_lt(tsn, map->base_tsn + map->len + map->len))
126 return;
127
128
129 if (TSN_lt(map->max_tsn_seen, tsn))
130 map->max_tsn_seen = tsn;
131
132
133 gap = tsn - map->base_tsn;
134
135
136 if (gap < map->len)
137 map->tsn_map[gap]++;
138 else
139 map->overflow_map[gap - map->len]++;
140
141
142
143
144 sctp_tsnmap_update(map);
145}
146
147
148
149SCTP_STATIC void sctp_tsnmap_iter_init(const struct sctp_tsnmap *map,
150 struct sctp_tsnmap_iter *iter)
151{
152
153 iter->start = map->cumulative_tsn_ack_point + 1;
154}
155
156
157
158
159SCTP_STATIC int sctp_tsnmap_next_gap_ack(const struct sctp_tsnmap *map,
160 struct sctp_tsnmap_iter *iter,
161 __u16 *start, __u16 *end)
162{
163 int started, ended;
164 __u16 start_, end_, offset;
165
166
167 started = ended = 0;
168
169
170 if (TSN_lte(map->max_tsn_seen, iter->start))
171 return 0;
172
173
174 if (iter->start - map->base_tsn < map->len) {
175
176 offset = iter->start - map->base_tsn;
177 sctp_tsnmap_find_gap_ack(map->tsn_map, offset, map->len, 0,
178 &started, &start_, &ended, &end_);
179 }
180
181
182 if (!ended) {
183
184
185
186 if (iter->start - map->base_tsn < map->len)
187 offset = 0;
188 else
189 offset = iter->start - map->base_tsn - map->len;
190
191
192 sctp_tsnmap_find_gap_ack(map->overflow_map,
193 offset,
194 map->len,
195 map->len,
196 &started, &start_,
197 &ended, &end_);
198 }
199
200
201
202
203 if (started && !ended) {
204 ended++;
205 end_ = map->len + map->len - 1;
206 }
207
208
209
210
211 if (ended) {
212
213
214
215 int gap = map->cumulative_tsn_ack_point -
216 map->base_tsn;
217
218 *start = start_ - gap;
219 *end = end_ - gap;
220
221
222 iter->start = map->cumulative_tsn_ack_point + *end + 1;
223 }
224
225 return ended;
226}
227
228
229void sctp_tsnmap_skip(struct sctp_tsnmap *map, __u32 tsn)
230{
231 __s32 gap;
232
233
234
235
236 if (TSN_lt(tsn, map->base_tsn))
237 return;
238 if (!TSN_lt(tsn, map->base_tsn + map->len + map->len))
239 return;
240
241
242 if (TSN_lt(map->max_tsn_seen, tsn))
243 map->max_tsn_seen = tsn;
244
245
246 gap = tsn - map->base_tsn + 1;
247
248
249 if (gap <= map->len)
250 memset(map->tsn_map, 0x01, gap);
251 else {
252 memset(map->tsn_map, 0x01, map->len);
253 memset(map->overflow_map, 0x01, (gap - map->len));
254 }
255
256
257
258
259 sctp_tsnmap_update(map);
260}
261
262
263
264
265
266
267
268
269static void sctp_tsnmap_update(struct sctp_tsnmap *map)
270{
271 __u32 ctsn;
272
273 ctsn = map->cumulative_tsn_ack_point;
274 do {
275 ctsn++;
276 if (ctsn == map->overflow_tsn) {
277
278
279
280 __u8 *tmp = map->tsn_map;
281 memset(tmp, 0, map->len);
282 map->tsn_map = map->overflow_map;
283 map->overflow_map = tmp;
284
285
286 map->base_tsn += map->len;
287 map->overflow_tsn += map->len;
288 }
289 } while (map->tsn_map[ctsn - map->base_tsn]);
290
291 map->cumulative_tsn_ack_point = ctsn - 1;
292}
293
294
295
296__u16 sctp_tsnmap_pending(struct sctp_tsnmap *map)
297{
298 __u32 cum_tsn = map->cumulative_tsn_ack_point;
299 __u32 max_tsn = map->max_tsn_seen;
300 __u32 base_tsn = map->base_tsn;
301 __u16 pending_data;
302 __s32 gap, start, end, i;
303
304 pending_data = max_tsn - cum_tsn;
305 gap = max_tsn - base_tsn;
306
307 if (gap <= 0 || gap >= (map->len + map->len))
308 goto out;
309
310 start = ((cum_tsn >= base_tsn) ? (cum_tsn - base_tsn + 1) : 0);
311 end = ((gap > map->len ) ? map->len : gap + 1);
312
313 for (i = start; i < end; i++) {
314 if (map->tsn_map[i])
315 pending_data--;
316 }
317
318 if (gap >= map->len) {
319 start = 0;
320 end = gap - map->len + 1;
321 for (i = start; i < end; i++) {
322 if (map->overflow_map[i])
323 pending_data--;
324 }
325 }
326
327out:
328 return pending_data;
329}
330
331
332
333
334
335
336
337static void sctp_tsnmap_find_gap_ack(__u8 *map, __u16 off,
338 __u16 len, __u16 base,
339 int *started, __u16 *start,
340 int *ended, __u16 *end)
341{
342 int i = off;
343
344
345
346
347
348
349
350
351 if (!(*started)) {
352 for (; i < len; i++) {
353 if (map[i]) {
354 (*started)++;
355 *start = base + i;
356 break;
357 }
358 }
359 }
360
361
362 if (*started) {
363
364
365
366 for (; i < len; i++) {
367 if (!map[i]) {
368 (*ended)++;
369 *end = base + i - 1;
370 break;
371 }
372 }
373 }
374}
375
376
377void sctp_tsnmap_renege(struct sctp_tsnmap *map, __u32 tsn)
378{
379 __s32 gap;
380
381 if (TSN_lt(tsn, map->base_tsn))
382 return;
383 if (!TSN_lt(tsn, map->base_tsn + map->len + map->len))
384 return;
385
386
387 gap = tsn - map->base_tsn;
388
389
390 if (gap < map->len)
391 map->tsn_map[gap] = 0;
392 else
393 map->overflow_map[gap - map->len] = 0;
394}
395
396
397__u16 sctp_tsnmap_num_gabs(struct sctp_tsnmap *map)
398{
399 struct sctp_tsnmap_iter iter;
400 int gabs = 0;
401
402
403 if (sctp_tsnmap_has_gap(map)) {
404 __u16 start, end;
405 sctp_tsnmap_iter_init(map, &iter);
406 while (sctp_tsnmap_next_gap_ack(map, &iter,
407 &start,
408 &end)) {
409
410 map->gabs[gabs].start = htons(start);
411 map->gabs[gabs].end = htons(end);
412 gabs++;
413 if (gabs >= SCTP_MAX_GABS)
414 break;
415 }
416 }
417 return gabs;
418}
419