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