blob: 5ba456727f631887113b8b9a3f3ace8ff1c92eff [file] [log] [blame]
Thomas Gleixner47505b82019-05-23 11:14:41 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vlad Yasevich60c778b2008-01-11 09:57:09 -05002/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001 Intel Corp.
7 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05008 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * These functions manipulate sctp tsn mapping array.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Please send any bug reports or fixes you make to the
13 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020014 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Written or modified by:
17 * La Monte H.P. Yarroll <piggy@acm.org>
18 * Jon Grimm <jgrimm@us.ibm.com>
19 * Karl Knutson <karl@athena.chicago.il.us>
20 * Sridhar Samudrala <sri@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/types.h>
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070025#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/sctp/sctp.h>
27#include <net/sctp/sm.h>
28
29static void sctp_tsnmap_update(struct sctp_tsnmap *map);
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070030static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
31 __u16 len, __u16 *start, __u16 *end);
Lee A. Roberts70fc69b2013-02-28 04:37:27 +000032static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* Initialize a block of memory as a tsnmap. */
35struct sctp_tsnmap *sctp_tsnmap_init(struct sctp_tsnmap *map, __u16 len,
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070036 __u32 initial_tsn, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070038 if (!map->tsn_map) {
39 map->tsn_map = kzalloc(len>>3, gfp);
40 if (map->tsn_map == NULL)
41 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070043 map->len = len;
44 } else {
45 bitmap_zero(map->tsn_map, map->len);
46 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 /* Keep track of TSNs represented by tsn_map. */
49 map->base_tsn = initial_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 map->cumulative_tsn_ack_point = initial_tsn - 1;
51 map->max_tsn_seen = map->cumulative_tsn_ack_point;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 map->num_dup_tsns = 0;
53
54 return map;
55}
56
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070057void sctp_tsnmap_free(struct sctp_tsnmap *map)
58{
59 map->len = 0;
60 kfree(map->tsn_map);
61}
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* Test the tracking state of this TSN.
64 * Returns:
65 * 0 if the TSN has not yet been seen
66 * >0 if the TSN has been seen (duplicate)
67 * <0 if the TSN is invalid (too large to track)
68 */
69int sctp_tsnmap_check(const struct sctp_tsnmap *map, __u32 tsn)
70{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070071 u32 gap;
72
73 /* Check to see if this is an old TSN */
74 if (TSN_lte(tsn, map->cumulative_tsn_ack_point))
75 return 1;
76
77 /* Verify that we can hold this TSN and that it will not
Drew Fustinid93ef302021-03-03 21:55:49 -080078 * overflow our map
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070079 */
80 if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
81 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /* Calculate the index into the mapping arrays. */
84 gap = tsn - map->base_tsn;
85
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070086 /* Check to see if TSN has already been recorded. */
87 if (gap < map->len && test_bit(gap, map->tsn_map))
88 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 else
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93
94/* Mark this TSN as seen. */
Neil Horman42448542012-06-30 03:04:26 +000095int sctp_tsnmap_mark(struct sctp_tsnmap *map, __u32 tsn,
96 struct sctp_transport *trans)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -070098 u16 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (TSN_lt(tsn, map->base_tsn))
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 gap = tsn - map->base_tsn;
104
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000105 if (gap >= map->len && !sctp_tsnmap_grow(map, gap + 1))
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700106 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700108 if (!sctp_tsnmap_has_gap(map) && gap == 0) {
109 /* In this case the map has no gaps and the tsn we are
110 * recording is the next expected tsn. We don't touch
111 * the map but simply bump the values.
112 */
113 map->max_tsn_seen++;
114 map->cumulative_tsn_ack_point++;
Neil Horman42448542012-06-30 03:04:26 +0000115 if (trans)
116 trans->sack_generation =
117 trans->asoc->peer.sack_generation;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700118 map->base_tsn++;
119 } else {
120 /* Either we already have a gap, or about to record a gap, so
121 * have work to do.
122 *
123 * Bump the max.
124 */
125 if (TSN_lt(map->max_tsn_seen, tsn))
126 map->max_tsn_seen = tsn;
127
128 /* Mark the TSN as received. */
129 set_bit(gap, map->tsn_map);
130
131 /* Go fixup any internal TSN mapping variables including
132 * cumulative_tsn_ack_point.
133 */
134 sctp_tsnmap_update(map);
135 }
136
137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140
141/* Initialize a Gap Ack Block iterator from memory being provided. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200142static void sctp_tsnmap_iter_init(const struct sctp_tsnmap *map,
143 struct sctp_tsnmap_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 /* Only start looking one past the Cumulative TSN Ack Point. */
146 iter->start = map->cumulative_tsn_ack_point + 1;
147}
148
149/* Get the next Gap Ack Blocks. Returns 0 if there was not another block
150 * to get.
151 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200152static int sctp_tsnmap_next_gap_ack(const struct sctp_tsnmap *map,
153 struct sctp_tsnmap_iter *iter,
154 __u16 *start, __u16 *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700156 int ended = 0;
157 __u16 start_ = 0, end_ = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 /* If there are no more gap acks possible, get out fast. */
160 if (TSN_lte(map->max_tsn_seen, iter->start))
161 return 0;
162
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700163 offset = iter->start - map->base_tsn;
164 sctp_tsnmap_find_gap_ack(map->tsn_map, offset, map->len,
165 &start_, &end_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700167 /* The Gap Ack Block happens to end at the end of the map. */
168 if (start_ && !end_)
169 end_ = map->len - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /* If we found a Gap Ack Block, return the start and end and
172 * bump the iterator forward.
173 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700174 if (end_) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Fix up the start and end based on the
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700176 * Cumulative TSN Ack which is always 1 behind base.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700178 *start = start_ + 1;
179 *end = end_ + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* Move the iterator forward. */
182 iter->start = map->cumulative_tsn_ack_point + *end + 1;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700183 ended = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185
186 return ended;
187}
188
189/* Mark this and any lower TSN as seen. */
190void sctp_tsnmap_skip(struct sctp_tsnmap *map, __u32 tsn)
191{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700192 u32 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (TSN_lt(tsn, map->base_tsn))
195 return;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700196 if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return;
198
199 /* Bump the max. */
200 if (TSN_lt(map->max_tsn_seen, tsn))
201 map->max_tsn_seen = tsn;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 gap = tsn - map->base_tsn + 1;
204
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700205 map->base_tsn += gap;
206 map->cumulative_tsn_ack_point += gap;
207 if (gap >= map->len) {
208 /* If our gap is larger then the map size, just
209 * zero out the map.
210 */
211 bitmap_zero(map->tsn_map, map->len);
212 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200213 /* If the gap is smaller than the map size,
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700214 * shift the map by 'gap' bits and update further.
215 */
216 bitmap_shift_right(map->tsn_map, map->tsn_map, gap, map->len);
217 sctp_tsnmap_update(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/********************************************************************
222 * 2nd Level Abstractions
223 ********************************************************************/
224
225/* This private helper function updates the tsnmap buffers and
226 * the Cumulative TSN Ack Point.
227 */
228static void sctp_tsnmap_update(struct sctp_tsnmap *map)
229{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700230 u16 len;
231 unsigned long zero_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700234 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; /* The first 0-bit is bit 0. nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700239 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245/* How many data chunks are we missing from our peer?
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;
Akinobu Mitafc184f02012-11-16 20:39:49 +0000253 u32 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 pending_data = max_tsn - cum_tsn;
256 gap = max_tsn - base_tsn;
257
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700258 if (gap == 0 || gap >= map->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto out;
260
Akinobu Mitafc184f02012-11-16 20:39:49 +0000261 pending_data -= bitmap_weight(map->tsn_map, gap + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262out:
263 return pending_data;
264}
265
266/* This is a private helper for finding Gap Ack Blocks. It searches a
267 * single array for the start and end of a Gap Ack Block.
268 *
269 * The flags "started" and "ended" tell is if we found the beginning
270 * or (respectively) the end of a Gap Ack Block.
271 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700272static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
273 __u16 len, __u16 *start, __u16 *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 int i = off;
276
277 /* Look through the entire array, but break out
278 * early if we have found the end of the Gap Ack Block.
279 */
280
281 /* Also, stop looking past the maximum TSN seen. */
282
283 /* Look for the start. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700284 i = find_next_bit(map, len, off);
285 if (i < len)
286 *start = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /* Look for the end. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700289 if (*start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* We have found the start, let's find the
291 * end. If we find the end, break out.
292 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700293 i = find_next_zero_bit(map, len, i);
294 if (i < len)
295 *end = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297}
298
299/* Renege that we have seen a TSN. */
300void sctp_tsnmap_renege(struct sctp_tsnmap *map, __u32 tsn)
301{
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700302 u32 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 if (TSN_lt(tsn, map->base_tsn))
305 return;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700306 /* Assert: TSN is in range. */
307 if (!TSN_lt(tsn, map->base_tsn + map->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 gap = tsn - map->base_tsn;
311
312 /* Pretend we never saw the TSN. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700313 clear_bit(gap, map->tsn_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316/* How many gap ack blocks do we have recorded? */
Vlad Yasevich02015182008-10-08 14:19:01 -0700317__u16 sctp_tsnmap_num_gabs(struct sctp_tsnmap *map,
318 struct sctp_gap_ack_block *gabs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct sctp_tsnmap_iter iter;
Vlad Yasevich02015182008-10-08 14:19:01 -0700321 int ngaps = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* Refresh the gap ack information. */
324 if (sctp_tsnmap_has_gap(map)) {
Shan Wei59ed5ab2011-02-19 21:57:26 +0000325 __u16 start = 0, end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sctp_tsnmap_iter_init(map, &iter);
327 while (sctp_tsnmap_next_gap_ack(map, &iter,
Al Viro9f81bcd2006-11-20 17:26:34 -0800328 &start,
329 &end)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Vlad Yasevich02015182008-10-08 14:19:01 -0700331 gabs[ngaps].start = htons(start);
332 gabs[ngaps].end = htons(end);
333 ngaps++;
334 if (ngaps >= SCTP_MAX_GABS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 break;
336 }
337 }
Vlad Yasevich02015182008-10-08 14:19:01 -0700338 return ngaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700340
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000341static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size)
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700342{
343 unsigned long *new;
344 unsigned long inc;
345 u16 len;
346
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000347 if (size > SCTP_TSN_MAP_SIZE)
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700348 return 0;
349
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000350 inc = ALIGN((size - map->len), BITS_PER_LONG) + SCTP_TSN_MAP_INCREMENT;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700351 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
Lee A. Roberts70fc69b2013-02-28 04:37:27 +0000357 bitmap_copy(new, map->tsn_map,
358 map->max_tsn_seen - map->cumulative_tsn_ack_point);
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700359 kfree(map->tsn_map);
360 map->tsn_map = new;
361 map->len = len;
362
363 return 1;
364}