blob: b46019568a86d15c71dc805c8d1b3e560866d726 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001 Intel Corp.
6 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05007 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * These functions manipulate sctp tsn mapping array.
10 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050011 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * you can redistribute it and/or modify it under the terms of
13 * the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050017 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19 * ************************
20 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 * See the GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with GNU CC; see the file COPYING. If not, write to
25 * the Free Software Foundation, 59 Temple Place - Suite 330,
26 * Boston, MA 02111-1307, USA.
27 *
28 * Please send any bug reports or fixes you make to the
29 * email address(es):
30 * lksctp developers <lksctp-developers@lists.sourceforge.net>
31 *
32 * Or submit a bug report through the following website:
33 * http://www.sf.net/projects/lksctp
34 *
35 * Written or modified by:
36 * La Monte H.P. Yarroll <piggy@acm.org>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Karl Knutson <karl@athena.chicago.il.us>
39 * Sridhar Samudrala <sri@us.ibm.com>
40 *
41 * Any bugs reported given to us we will try to fix... any fixes shared will
42 * be incorporated into the next SCTP release.
43 */
44
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/types.h>
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070047#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <net/sctp/sctp.h>
49#include <net/sctp/sm.h>
50
51static void sctp_tsnmap_update(struct sctp_tsnmap *map);
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070052static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
53 __u16 len, __u16 *start, __u16 *end);
Lee A. Roberts70fc69b2013-02-28 04:37:27 +000054static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* Initialize a block of memory as a tsnmap. */
57struct sctp_tsnmap *sctp_tsnmap_init(struct sctp_tsnmap *map, __u16 len,
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070058 __u32 initial_tsn, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070060 if (!map->tsn_map) {
61 map->tsn_map = kzalloc(len>>3, gfp);
62 if (map->tsn_map == NULL)
63 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070065 map->len = len;
66 } else {
67 bitmap_zero(map->tsn_map, map->len);
68 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 /* Keep track of TSNs represented by tsn_map. */
71 map->base_tsn = initial_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 map->cumulative_tsn_ack_point = initial_tsn - 1;
73 map->max_tsn_seen = map->cumulative_tsn_ack_point;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 map->num_dup_tsns = 0;
75
76 return map;
77}
78
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070079void sctp_tsnmap_free(struct sctp_tsnmap *map)
80{
81 map->len = 0;
82 kfree(map->tsn_map);
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Test the tracking state of this TSN.
86 * Returns:
87 * 0 if the TSN has not yet been seen
88 * >0 if the TSN has been seen (duplicate)
89 * <0 if the TSN is invalid (too large to track)
90 */
91int sctp_tsnmap_check(const struct sctp_tsnmap *map, __u32 tsn)
92{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070093 u32 gap;
94
95 /* Check to see if this is an old TSN */
96 if (TSN_lte(tsn, map->cumulative_tsn_ack_point))
97 return 1;
98
99 /* Verify that we can hold this TSN and that it will not
100 * overlfow our map
101 */
102 if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
103 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 /* Calculate the index into the mapping arrays. */
106 gap = tsn - map->base_tsn;
107
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700108 /* Check to see if TSN has already been recorded. */
109 if (gap < map->len && test_bit(gap, map->tsn_map))
110 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 else
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115
116/* Mark this TSN as seen. */
Neil Horman42448542012-06-30 03:04:26 +0000117int sctp_tsnmap_mark(struct sctp_tsnmap *map, __u32 tsn,
118 struct sctp_transport *trans)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700120 u16 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (TSN_lt(tsn, map->base_tsn))
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 gap = tsn - map->base_tsn;
126
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000127 if (gap >= map->len && !sctp_tsnmap_grow(map, gap + 1))
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700128 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700130 if (!sctp_tsnmap_has_gap(map) && gap == 0) {
131 /* In this case the map has no gaps and the tsn we are
132 * recording is the next expected tsn. We don't touch
133 * the map but simply bump the values.
134 */
135 map->max_tsn_seen++;
136 map->cumulative_tsn_ack_point++;
Neil Horman42448542012-06-30 03:04:26 +0000137 if (trans)
138 trans->sack_generation =
139 trans->asoc->peer.sack_generation;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700140 map->base_tsn++;
141 } else {
142 /* Either we already have a gap, or about to record a gap, so
143 * have work to do.
144 *
145 * Bump the max.
146 */
147 if (TSN_lt(map->max_tsn_seen, tsn))
148 map->max_tsn_seen = tsn;
149
150 /* Mark the TSN as received. */
151 set_bit(gap, map->tsn_map);
152
153 /* Go fixup any internal TSN mapping variables including
154 * cumulative_tsn_ack_point.
155 */
156 sctp_tsnmap_update(map);
157 }
158
159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162
163/* Initialize a Gap Ack Block iterator from memory being provided. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200164static void sctp_tsnmap_iter_init(const struct sctp_tsnmap *map,
165 struct sctp_tsnmap_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 /* Only start looking one past the Cumulative TSN Ack Point. */
168 iter->start = map->cumulative_tsn_ack_point + 1;
169}
170
171/* Get the next Gap Ack Blocks. Returns 0 if there was not another block
172 * to get.
173 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200174static int sctp_tsnmap_next_gap_ack(const struct sctp_tsnmap *map,
175 struct sctp_tsnmap_iter *iter,
176 __u16 *start, __u16 *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700178 int ended = 0;
179 __u16 start_ = 0, end_ = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* If there are no more gap acks possible, get out fast. */
182 if (TSN_lte(map->max_tsn_seen, iter->start))
183 return 0;
184
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700185 offset = iter->start - map->base_tsn;
186 sctp_tsnmap_find_gap_ack(map->tsn_map, offset, map->len,
187 &start_, &end_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700189 /* The Gap Ack Block happens to end at the end of the map. */
190 if (start_ && !end_)
191 end_ = map->len - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /* If we found a Gap Ack Block, return the start and end and
194 * bump the iterator forward.
195 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700196 if (end_) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* Fix up the start and end based on the
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700198 * Cumulative TSN Ack which is always 1 behind base.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700200 *start = start_ + 1;
201 *end = end_ + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* Move the iterator forward. */
204 iter->start = map->cumulative_tsn_ack_point + *end + 1;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700205 ended = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 return ended;
209}
210
211/* Mark this and any lower TSN as seen. */
212void sctp_tsnmap_skip(struct sctp_tsnmap *map, __u32 tsn)
213{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700214 u32 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (TSN_lt(tsn, map->base_tsn))
217 return;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700218 if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return;
220
221 /* Bump the max. */
222 if (TSN_lt(map->max_tsn_seen, tsn))
223 map->max_tsn_seen = tsn;
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 gap = tsn - map->base_tsn + 1;
226
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700227 map->base_tsn += gap;
228 map->cumulative_tsn_ack_point += gap;
229 if (gap >= map->len) {
230 /* If our gap is larger then the map size, just
231 * zero out the map.
232 */
233 bitmap_zero(map->tsn_map, map->len);
234 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200235 /* If the gap is smaller than the map size,
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700236 * shift the map by 'gap' bits and update further.
237 */
238 bitmap_shift_right(map->tsn_map, map->tsn_map, gap, map->len);
239 sctp_tsnmap_update(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/********************************************************************
244 * 2nd Level Abstractions
245 ********************************************************************/
246
247/* This private helper function updates the tsnmap buffers and
248 * the Cumulative TSN Ack Point.
249 */
250static void sctp_tsnmap_update(struct sctp_tsnmap *map)
251{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700252 u16 len;
253 unsigned long zero_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700256 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; /* The first 0-bit is bit 0. nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700261 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/* How many data chunks are we missing from our peer?
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;
Akinobu Mitafc184f02012-11-16 20:39:49 +0000275 u32 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 pending_data = max_tsn - cum_tsn;
278 gap = max_tsn - base_tsn;
279
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700280 if (gap == 0 || gap >= map->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 goto out;
282
Akinobu Mitafc184f02012-11-16 20:39:49 +0000283 pending_data -= bitmap_weight(map->tsn_map, gap + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284out:
285 return pending_data;
286}
287
288/* This is a private helper for finding Gap Ack Blocks. It searches a
289 * single array for the start and end of a Gap Ack Block.
290 *
291 * The flags "started" and "ended" tell is if we found the beginning
292 * or (respectively) the end of a Gap Ack Block.
293 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700294static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
295 __u16 len, __u16 *start, __u16 *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 int i = off;
298
299 /* Look through the entire array, but break out
300 * early if we have found the end of the Gap Ack Block.
301 */
302
303 /* Also, stop looking past the maximum TSN seen. */
304
305 /* Look for the start. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700306 i = find_next_bit(map, len, off);
307 if (i < len)
308 *start = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* Look for the end. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700311 if (*start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /* We have found the start, let's find the
313 * end. If we find the end, break out.
314 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700315 i = find_next_zero_bit(map, len, i);
316 if (i < len)
317 *end = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319}
320
321/* Renege that we have seen a TSN. */
322void sctp_tsnmap_renege(struct sctp_tsnmap *map, __u32 tsn)
323{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700324 u32 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (TSN_lt(tsn, map->base_tsn))
327 return;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700328 /* Assert: TSN is in range. */
329 if (!TSN_lt(tsn, map->base_tsn + map->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 gap = tsn - map->base_tsn;
333
334 /* Pretend we never saw the TSN. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700335 clear_bit(gap, map->tsn_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338/* How many gap ack blocks do we have recorded? */
Vlad Yasevich02015182008-10-08 14:19:01 -0700339__u16 sctp_tsnmap_num_gabs(struct sctp_tsnmap *map,
340 struct sctp_gap_ack_block *gabs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 struct sctp_tsnmap_iter iter;
Vlad Yasevich02015182008-10-08 14:19:01 -0700343 int ngaps = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* Refresh the gap ack information. */
346 if (sctp_tsnmap_has_gap(map)) {
Shan Wei59ed5ab2011-02-19 21:57:26 +0000347 __u16 start = 0, end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 sctp_tsnmap_iter_init(map, &iter);
349 while (sctp_tsnmap_next_gap_ack(map, &iter,
Al Viro9f81bcd2006-11-20 17:26:34 -0800350 &start,
351 &end)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Vlad Yasevich02015182008-10-08 14:19:01 -0700353 gabs[ngaps].start = htons(start);
354 gabs[ngaps].end = htons(end);
355 ngaps++;
356 if (ngaps >= SCTP_MAX_GABS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
358 }
359 }
Vlad Yasevich02015182008-10-08 14:19:01 -0700360 return ngaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700362
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000363static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size)
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700364{
365 unsigned long *new;
366 unsigned long inc;
367 u16 len;
368
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000369 if (size > SCTP_TSN_MAP_SIZE)
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700370 return 0;
371
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000372 inc = ALIGN((size - map->len), BITS_PER_LONG) + SCTP_TSN_MAP_INCREMENT;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700373 len = min_t(u16, map->len + inc, SCTP_TSN_MAP_SIZE);
374
375 new = kzalloc(len>>3, GFP_ATOMIC);
376 if (!new)
377 return 0;
378
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000379 bitmap_copy(new, map->tsn_map,
380 map->max_tsn_seen - map->cumulative_tsn_ack_point);
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700381 kfree(map->tsn_map);
382 map->tsn_map = new;
383 map->len = len;
384
385 return 1;
386}