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