tsnmap.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * (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. *
  8. * This file is part of the SCTP kernel implementation
  9. *
  10. * These functions manipulate sctp tsn mapping array.
  11. *
  12. * Please send any bug reports or fixes you make to the
  13. * email address(es):
  14. * lksctp developers <[email protected]>
  15. *
  16. * Written or modified by:
  17. * La Monte H.P. Yarroll <[email protected]>
  18. * Jon Grimm <[email protected]>
  19. * Karl Knutson <[email protected]>
  20. * Sridhar Samudrala <[email protected]>
  21. */
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <linux/bitmap.h>
  25. #include <net/sctp/sctp.h>
  26. #include <net/sctp/sm.h>
  27. static void sctp_tsnmap_update(struct sctp_tsnmap *map);
  28. static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
  29. __u16 len, __u16 *start, __u16 *end);
  30. static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size);
  31. /* Initialize a block of memory as a tsnmap. */
  32. struct sctp_tsnmap *sctp_tsnmap_init(struct sctp_tsnmap *map, __u16 len,
  33. __u32 initial_tsn, gfp_t gfp)
  34. {
  35. if (!map->tsn_map) {
  36. map->tsn_map = kzalloc(len>>3, gfp);
  37. if (map->tsn_map == NULL)
  38. return NULL;
  39. map->len = len;
  40. } else {
  41. bitmap_zero(map->tsn_map, map->len);
  42. }
  43. /* Keep track of TSNs represented by tsn_map. */
  44. map->base_tsn = initial_tsn;
  45. map->cumulative_tsn_ack_point = initial_tsn - 1;
  46. map->max_tsn_seen = map->cumulative_tsn_ack_point;
  47. map->num_dup_tsns = 0;
  48. return map;
  49. }
  50. void sctp_tsnmap_free(struct sctp_tsnmap *map)
  51. {
  52. map->len = 0;
  53. kfree(map->tsn_map);
  54. }
  55. /* Test the tracking state of this TSN.
  56. * Returns:
  57. * 0 if the TSN has not yet been seen
  58. * >0 if the TSN has been seen (duplicate)
  59. * <0 if the TSN is invalid (too large to track)
  60. */
  61. int sctp_tsnmap_check(const struct sctp_tsnmap *map, __u32 tsn)
  62. {
  63. u32 gap;
  64. /* Check to see if this is an old TSN */
  65. if (TSN_lte(tsn, map->cumulative_tsn_ack_point))
  66. return 1;
  67. /* Verify that we can hold this TSN and that it will not
  68. * overflow our map
  69. */
  70. if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
  71. return -1;
  72. /* Calculate the index into the mapping arrays. */
  73. gap = tsn - map->base_tsn;
  74. /* Check to see if TSN has already been recorded. */
  75. if (gap < map->len && test_bit(gap, map->tsn_map))
  76. return 1;
  77. else
  78. return 0;
  79. }
  80. /* Mark this TSN as seen. */
  81. int sctp_tsnmap_mark(struct sctp_tsnmap *map, __u32 tsn,
  82. struct sctp_transport *trans)
  83. {
  84. u16 gap;
  85. if (TSN_lt(tsn, map->base_tsn))
  86. return 0;
  87. gap = tsn - map->base_tsn;
  88. if (gap >= map->len && !sctp_tsnmap_grow(map, gap + 1))
  89. return -ENOMEM;
  90. if (!sctp_tsnmap_has_gap(map) && gap == 0) {
  91. /* In this case the map has no gaps and the tsn we are
  92. * recording is the next expected tsn. We don't touch
  93. * the map but simply bump the values.
  94. */
  95. map->max_tsn_seen++;
  96. map->cumulative_tsn_ack_point++;
  97. if (trans)
  98. trans->sack_generation =
  99. trans->asoc->peer.sack_generation;
  100. map->base_tsn++;
  101. } else {
  102. /* Either we already have a gap, or about to record a gap, so
  103. * have work to do.
  104. *
  105. * Bump the max.
  106. */
  107. if (TSN_lt(map->max_tsn_seen, tsn))
  108. map->max_tsn_seen = tsn;
  109. /* Mark the TSN as received. */
  110. set_bit(gap, map->tsn_map);
  111. /* Go fixup any internal TSN mapping variables including
  112. * cumulative_tsn_ack_point.
  113. */
  114. sctp_tsnmap_update(map);
  115. }
  116. return 0;
  117. }
  118. /* Initialize a Gap Ack Block iterator from memory being provided. */
  119. static void sctp_tsnmap_iter_init(const struct sctp_tsnmap *map,
  120. struct sctp_tsnmap_iter *iter)
  121. {
  122. /* Only start looking one past the Cumulative TSN Ack Point. */
  123. iter->start = map->cumulative_tsn_ack_point + 1;
  124. }
  125. /* Get the next Gap Ack Blocks. Returns 0 if there was not another block
  126. * to get.
  127. */
  128. static int sctp_tsnmap_next_gap_ack(const struct sctp_tsnmap *map,
  129. struct sctp_tsnmap_iter *iter,
  130. __u16 *start, __u16 *end)
  131. {
  132. int ended = 0;
  133. __u16 start_ = 0, end_ = 0, offset;
  134. /* If there are no more gap acks possible, get out fast. */
  135. if (TSN_lte(map->max_tsn_seen, iter->start))
  136. return 0;
  137. offset = iter->start - map->base_tsn;
  138. sctp_tsnmap_find_gap_ack(map->tsn_map, offset, map->len,
  139. &start_, &end_);
  140. /* The Gap Ack Block happens to end at the end of the map. */
  141. if (start_ && !end_)
  142. end_ = map->len - 1;
  143. /* If we found a Gap Ack Block, return the start and end and
  144. * bump the iterator forward.
  145. */
  146. if (end_) {
  147. /* Fix up the start and end based on the
  148. * Cumulative TSN Ack which is always 1 behind base.
  149. */
  150. *start = start_ + 1;
  151. *end = end_ + 1;
  152. /* Move the iterator forward. */
  153. iter->start = map->cumulative_tsn_ack_point + *end + 1;
  154. ended = 1;
  155. }
  156. return ended;
  157. }
  158. /* Mark this and any lower TSN as seen. */
  159. void sctp_tsnmap_skip(struct sctp_tsnmap *map, __u32 tsn)
  160. {
  161. u32 gap;
  162. if (TSN_lt(tsn, map->base_tsn))
  163. return;
  164. if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
  165. return;
  166. /* Bump the max. */
  167. if (TSN_lt(map->max_tsn_seen, tsn))
  168. map->max_tsn_seen = tsn;
  169. gap = tsn - map->base_tsn + 1;
  170. map->base_tsn += gap;
  171. map->cumulative_tsn_ack_point += gap;
  172. if (gap >= map->len) {
  173. /* If our gap is larger then the map size, just
  174. * zero out the map.
  175. */
  176. bitmap_zero(map->tsn_map, map->len);
  177. } else {
  178. /* If the gap is smaller than the map size,
  179. * shift the map by 'gap' bits and update further.
  180. */
  181. bitmap_shift_right(map->tsn_map, map->tsn_map, gap, map->len);
  182. sctp_tsnmap_update(map);
  183. }
  184. }
  185. /********************************************************************
  186. * 2nd Level Abstractions
  187. ********************************************************************/
  188. /* This private helper function updates the tsnmap buffers and
  189. * the Cumulative TSN Ack Point.
  190. */
  191. static void sctp_tsnmap_update(struct sctp_tsnmap *map)
  192. {
  193. u16 len;
  194. unsigned long zero_bit;
  195. len = map->max_tsn_seen - map->cumulative_tsn_ack_point;
  196. zero_bit = find_first_zero_bit(map->tsn_map, len);
  197. if (!zero_bit)
  198. return; /* The first 0-bit is bit 0. nothing to do */
  199. map->base_tsn += zero_bit;
  200. map->cumulative_tsn_ack_point += zero_bit;
  201. bitmap_shift_right(map->tsn_map, map->tsn_map, zero_bit, map->len);
  202. }
  203. /* How many data chunks are we missing from our peer?
  204. */
  205. __u16 sctp_tsnmap_pending(struct sctp_tsnmap *map)
  206. {
  207. __u32 cum_tsn = map->cumulative_tsn_ack_point;
  208. __u32 max_tsn = map->max_tsn_seen;
  209. __u32 base_tsn = map->base_tsn;
  210. __u16 pending_data;
  211. u32 gap;
  212. pending_data = max_tsn - cum_tsn;
  213. gap = max_tsn - base_tsn;
  214. if (gap == 0 || gap >= map->len)
  215. goto out;
  216. pending_data -= bitmap_weight(map->tsn_map, gap + 1);
  217. out:
  218. return pending_data;
  219. }
  220. /* This is a private helper for finding Gap Ack Blocks. It searches a
  221. * single array for the start and end of a Gap Ack Block.
  222. *
  223. * The flags "started" and "ended" tell is if we found the beginning
  224. * or (respectively) the end of a Gap Ack Block.
  225. */
  226. static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
  227. __u16 len, __u16 *start, __u16 *end)
  228. {
  229. int i = off;
  230. /* Look through the entire array, but break out
  231. * early if we have found the end of the Gap Ack Block.
  232. */
  233. /* Also, stop looking past the maximum TSN seen. */
  234. /* Look for the start. */
  235. i = find_next_bit(map, len, off);
  236. if (i < len)
  237. *start = i;
  238. /* Look for the end. */
  239. if (*start) {
  240. /* We have found the start, let's find the
  241. * end. If we find the end, break out.
  242. */
  243. i = find_next_zero_bit(map, len, i);
  244. if (i < len)
  245. *end = i - 1;
  246. }
  247. }
  248. /* Renege that we have seen a TSN. */
  249. void sctp_tsnmap_renege(struct sctp_tsnmap *map, __u32 tsn)
  250. {
  251. u32 gap;
  252. if (TSN_lt(tsn, map->base_tsn))
  253. return;
  254. /* Assert: TSN is in range. */
  255. if (!TSN_lt(tsn, map->base_tsn + map->len))
  256. return;
  257. gap = tsn - map->base_tsn;
  258. /* Pretend we never saw the TSN. */
  259. clear_bit(gap, map->tsn_map);
  260. }
  261. /* How many gap ack blocks do we have recorded? */
  262. __u16 sctp_tsnmap_num_gabs(struct sctp_tsnmap *map,
  263. struct sctp_gap_ack_block *gabs)
  264. {
  265. struct sctp_tsnmap_iter iter;
  266. int ngaps = 0;
  267. /* Refresh the gap ack information. */
  268. if (sctp_tsnmap_has_gap(map)) {
  269. __u16 start = 0, end = 0;
  270. sctp_tsnmap_iter_init(map, &iter);
  271. while (sctp_tsnmap_next_gap_ack(map, &iter,
  272. &start,
  273. &end)) {
  274. gabs[ngaps].start = htons(start);
  275. gabs[ngaps].end = htons(end);
  276. ngaps++;
  277. if (ngaps >= SCTP_MAX_GABS)
  278. break;
  279. }
  280. }
  281. return ngaps;
  282. }
  283. static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size)
  284. {
  285. unsigned long *new;
  286. unsigned long inc;
  287. u16 len;
  288. if (size > SCTP_TSN_MAP_SIZE)
  289. return 0;
  290. inc = ALIGN((size - map->len), BITS_PER_LONG) + SCTP_TSN_MAP_INCREMENT;
  291. len = min_t(u16, map->len + inc, SCTP_TSN_MAP_SIZE);
  292. new = kzalloc(len>>3, GFP_ATOMIC);
  293. if (!new)
  294. return 0;
  295. bitmap_copy(new, map->tsn_map,
  296. map->max_tsn_seen - map->cumulative_tsn_ack_point);
  297. kfree(map->tsn_map);
  298. map->tsn_map = new;
  299. map->len = len;
  300. return 1;
  301. }