blob: 7635f9f2311d31d71297045cb93c2e4217f24b2d [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
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080024 * along with GNU CC; see the file COPYING. If not, see
25 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 * Please send any bug reports or fixes you make to the
28 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020029 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * Written or modified by:
32 * La Monte H.P. Yarroll <piggy@acm.org>
33 * Jon Grimm <jgrimm@us.ibm.com>
34 * Karl Knutson <karl@athena.chicago.il.us>
35 * Sridhar Samudrala <sri@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
37
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070040#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/sctp/sctp.h>
42#include <net/sctp/sm.h>
43
44static void sctp_tsnmap_update(struct sctp_tsnmap *map);
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070045static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
46 __u16 len, __u16 *start, __u16 *end);
Lee A. Roberts70fc69b2013-02-28 04:37:27 +000047static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* Initialize a block of memory as a tsnmap. */
50struct sctp_tsnmap *sctp_tsnmap_init(struct sctp_tsnmap *map, __u16 len,
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070051 __u32 initial_tsn, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070053 if (!map->tsn_map) {
54 map->tsn_map = kzalloc(len>>3, gfp);
55 if (map->tsn_map == NULL)
56 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070058 map->len = len;
59 } else {
60 bitmap_zero(map->tsn_map, map->len);
61 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 /* Keep track of TSNs represented by tsn_map. */
64 map->base_tsn = initial_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 map->cumulative_tsn_ack_point = initial_tsn - 1;
66 map->max_tsn_seen = map->cumulative_tsn_ack_point;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 map->num_dup_tsns = 0;
68
69 return map;
70}
71
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070072void sctp_tsnmap_free(struct sctp_tsnmap *map)
73{
74 map->len = 0;
75 kfree(map->tsn_map);
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* Test the tracking state of this TSN.
79 * Returns:
80 * 0 if the TSN has not yet been seen
81 * >0 if the TSN has been seen (duplicate)
82 * <0 if the TSN is invalid (too large to track)
83 */
84int sctp_tsnmap_check(const struct sctp_tsnmap *map, __u32 tsn)
85{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070086 u32 gap;
87
88 /* Check to see if this is an old TSN */
89 if (TSN_lte(tsn, map->cumulative_tsn_ack_point))
90 return 1;
91
92 /* Verify that we can hold this TSN and that it will not
93 * overlfow our map
94 */
95 if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
96 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* Calculate the index into the mapping arrays. */
99 gap = tsn - map->base_tsn;
100
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700101 /* Check to see if TSN has already been recorded. */
102 if (gap < map->len && test_bit(gap, map->tsn_map))
103 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 else
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108
109/* Mark this TSN as seen. */
Neil Horman42448542012-06-30 03:04:26 +0000110int sctp_tsnmap_mark(struct sctp_tsnmap *map, __u32 tsn,
111 struct sctp_transport *trans)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700113 u16 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (TSN_lt(tsn, map->base_tsn))
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 gap = tsn - map->base_tsn;
119
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000120 if (gap >= map->len && !sctp_tsnmap_grow(map, gap + 1))
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700121 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700123 if (!sctp_tsnmap_has_gap(map) && gap == 0) {
124 /* In this case the map has no gaps and the tsn we are
125 * recording is the next expected tsn. We don't touch
126 * the map but simply bump the values.
127 */
128 map->max_tsn_seen++;
129 map->cumulative_tsn_ack_point++;
Neil Horman42448542012-06-30 03:04:26 +0000130 if (trans)
131 trans->sack_generation =
132 trans->asoc->peer.sack_generation;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700133 map->base_tsn++;
134 } else {
135 /* Either we already have a gap, or about to record a gap, so
136 * have work to do.
137 *
138 * Bump the max.
139 */
140 if (TSN_lt(map->max_tsn_seen, tsn))
141 map->max_tsn_seen = tsn;
142
143 /* Mark the TSN as received. */
144 set_bit(gap, map->tsn_map);
145
146 /* Go fixup any internal TSN mapping variables including
147 * cumulative_tsn_ack_point.
148 */
149 sctp_tsnmap_update(map);
150 }
151
152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155
156/* Initialize a Gap Ack Block iterator from memory being provided. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200157static void sctp_tsnmap_iter_init(const struct sctp_tsnmap *map,
158 struct sctp_tsnmap_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 /* Only start looking one past the Cumulative TSN Ack Point. */
161 iter->start = map->cumulative_tsn_ack_point + 1;
162}
163
164/* Get the next Gap Ack Blocks. Returns 0 if there was not another block
165 * to get.
166 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200167static int sctp_tsnmap_next_gap_ack(const struct sctp_tsnmap *map,
168 struct sctp_tsnmap_iter *iter,
169 __u16 *start, __u16 *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700171 int ended = 0;
172 __u16 start_ = 0, end_ = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* If there are no more gap acks possible, get out fast. */
175 if (TSN_lte(map->max_tsn_seen, iter->start))
176 return 0;
177
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700178 offset = iter->start - map->base_tsn;
179 sctp_tsnmap_find_gap_ack(map->tsn_map, offset, map->len,
180 &start_, &end_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700182 /* The Gap Ack Block happens to end at the end of the map. */
183 if (start_ && !end_)
184 end_ = map->len - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /* If we found a Gap Ack Block, return the start and end and
187 * bump the iterator forward.
188 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700189 if (end_) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* Fix up the start and end based on the
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700191 * Cumulative TSN Ack which is always 1 behind base.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700193 *start = start_ + 1;
194 *end = end_ + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 /* Move the iterator forward. */
197 iter->start = map->cumulative_tsn_ack_point + *end + 1;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700198 ended = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
201 return ended;
202}
203
204/* Mark this and any lower TSN as seen. */
205void sctp_tsnmap_skip(struct sctp_tsnmap *map, __u32 tsn)
206{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700207 u32 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (TSN_lt(tsn, map->base_tsn))
210 return;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700211 if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return;
213
214 /* Bump the max. */
215 if (TSN_lt(map->max_tsn_seen, tsn))
216 map->max_tsn_seen = tsn;
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 gap = tsn - map->base_tsn + 1;
219
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700220 map->base_tsn += gap;
221 map->cumulative_tsn_ack_point += gap;
222 if (gap >= map->len) {
223 /* If our gap is larger then the map size, just
224 * zero out the map.
225 */
226 bitmap_zero(map->tsn_map, map->len);
227 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200228 /* If the gap is smaller than the map size,
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700229 * shift the map by 'gap' bits and update further.
230 */
231 bitmap_shift_right(map->tsn_map, map->tsn_map, gap, map->len);
232 sctp_tsnmap_update(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236/********************************************************************
237 * 2nd Level Abstractions
238 ********************************************************************/
239
240/* This private helper function updates the tsnmap buffers and
241 * the Cumulative TSN Ack Point.
242 */
243static void sctp_tsnmap_update(struct sctp_tsnmap *map)
244{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700245 u16 len;
246 unsigned long zero_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700249 len = map->max_tsn_seen - map->cumulative_tsn_ack_point;
250 zero_bit = find_first_zero_bit(map->tsn_map, len);
251 if (!zero_bit)
252 return; /* The first 0-bit is bit 0. nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700254 map->base_tsn += zero_bit;
255 map->cumulative_tsn_ack_point += zero_bit;
256
257 bitmap_shift_right(map->tsn_map, map->tsn_map, zero_bit, map->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/* How many data chunks are we missing from our peer?
261 */
262__u16 sctp_tsnmap_pending(struct sctp_tsnmap *map)
263{
264 __u32 cum_tsn = map->cumulative_tsn_ack_point;
265 __u32 max_tsn = map->max_tsn_seen;
266 __u32 base_tsn = map->base_tsn;
267 __u16 pending_data;
Akinobu Mitafc184f02012-11-16 20:39:49 +0000268 u32 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 pending_data = max_tsn - cum_tsn;
271 gap = max_tsn - base_tsn;
272
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700273 if (gap == 0 || gap >= map->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 goto out;
275
Akinobu Mitafc184f02012-11-16 20:39:49 +0000276 pending_data -= bitmap_weight(map->tsn_map, gap + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277out:
278 return pending_data;
279}
280
281/* This is a private helper for finding Gap Ack Blocks. It searches a
282 * single array for the start and end of a Gap Ack Block.
283 *
284 * The flags "started" and "ended" tell is if we found the beginning
285 * or (respectively) the end of a Gap Ack Block.
286 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700287static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
288 __u16 len, __u16 *start, __u16 *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 int i = off;
291
292 /* Look through the entire array, but break out
293 * early if we have found the end of the Gap Ack Block.
294 */
295
296 /* Also, stop looking past the maximum TSN seen. */
297
298 /* Look for the start. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700299 i = find_next_bit(map, len, off);
300 if (i < len)
301 *start = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* Look for the end. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700304 if (*start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* We have found the start, let's find the
306 * end. If we find the end, break out.
307 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700308 i = find_next_zero_bit(map, len, i);
309 if (i < len)
310 *end = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312}
313
314/* Renege that we have seen a TSN. */
315void sctp_tsnmap_renege(struct sctp_tsnmap *map, __u32 tsn)
316{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700317 u32 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 if (TSN_lt(tsn, map->base_tsn))
320 return;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700321 /* Assert: TSN is in range. */
322 if (!TSN_lt(tsn, map->base_tsn + map->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 gap = tsn - map->base_tsn;
326
327 /* Pretend we never saw the TSN. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700328 clear_bit(gap, map->tsn_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/* How many gap ack blocks do we have recorded? */
Vlad Yasevich02015182008-10-08 14:19:01 -0700332__u16 sctp_tsnmap_num_gabs(struct sctp_tsnmap *map,
333 struct sctp_gap_ack_block *gabs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct sctp_tsnmap_iter iter;
Vlad Yasevich02015182008-10-08 14:19:01 -0700336 int ngaps = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* Refresh the gap ack information. */
339 if (sctp_tsnmap_has_gap(map)) {
Shan Wei59ed5ab2011-02-19 21:57:26 +0000340 __u16 start = 0, end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 sctp_tsnmap_iter_init(map, &iter);
342 while (sctp_tsnmap_next_gap_ack(map, &iter,
Al Viro9f81bcd2006-11-20 17:26:34 -0800343 &start,
344 &end)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Vlad Yasevich02015182008-10-08 14:19:01 -0700346 gabs[ngaps].start = htons(start);
347 gabs[ngaps].end = htons(end);
348 ngaps++;
349 if (ngaps >= SCTP_MAX_GABS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 break;
351 }
352 }
Vlad Yasevich02015182008-10-08 14:19:01 -0700353 return ngaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700355
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000356static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size)
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700357{
358 unsigned long *new;
359 unsigned long inc;
360 u16 len;
361
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000362 if (size > SCTP_TSN_MAP_SIZE)
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700363 return 0;
364
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000365 inc = ALIGN((size - map->len), BITS_PER_LONG) + SCTP_TSN_MAP_INCREMENT;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700366 len = min_t(u16, map->len + inc, SCTP_TSN_MAP_SIZE);
367
368 new = kzalloc(len>>3, GFP_ATOMIC);
369 if (!new)
370 return 0;
371
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000372 bitmap_copy(new, map->tsn_map,
373 map->max_tsn_seen - map->cumulative_tsn_ack_point);
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700374 kfree(map->tsn_map);
375 map->tsn_map = new;
376 map->len = len;
377
378 return 1;
379}