^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * net/dccp/ackvec.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * An implementation of Ack Vectors for the DCCP protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2007 University of Aberdeen, Scotland, UK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "dccp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static struct kmem_cache *dccp_ackvec_slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static struct kmem_cache *dccp_ackvec_record_slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct dccp_ackvec *av = kmem_cache_zalloc(dccp_ackvec_slab, priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) if (av != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) av->av_buf_head = av->av_buf_tail = DCCPAV_MAX_ACKVEC_LEN - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) INIT_LIST_HEAD(&av->av_records);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static void dccp_ackvec_purge_records(struct dccp_ackvec *av)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct dccp_ackvec_record *cur, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) list_for_each_entry_safe(cur, next, &av->av_records, avr_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) kmem_cache_free(dccp_ackvec_record_slab, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) INIT_LIST_HEAD(&av->av_records);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) void dccp_ackvec_free(struct dccp_ackvec *av)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (likely(av != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) dccp_ackvec_purge_records(av);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) kmem_cache_free(dccp_ackvec_slab, av);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * dccp_ackvec_update_records - Record information about sent Ack Vectors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * @av: Ack Vector records to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * @seqno: Sequence number of the packet carrying the Ack Vector just sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @nonce_sum: The sum of all buffer nonces contained in the Ack Vector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int dccp_ackvec_update_records(struct dccp_ackvec *av, u64 seqno, u8 nonce_sum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct dccp_ackvec_record *avr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) avr = kmem_cache_alloc(dccp_ackvec_record_slab, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (avr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) avr->avr_ack_seqno = seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) avr->avr_ack_ptr = av->av_buf_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) avr->avr_ack_ackno = av->av_buf_ackno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) avr->avr_ack_nonce = nonce_sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) avr->avr_ack_runlen = dccp_ackvec_runlen(av->av_buf + av->av_buf_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * When the buffer overflows, we keep no more than one record. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * the simplest way of disambiguating sender-Acks dating from before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * overflow from sender-Acks which refer to after the overflow; a simple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * solution is preferable here since we are handling an exception.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (av->av_overflow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) dccp_ackvec_purge_records(av);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * Since GSS is incremented for each packet, the list is automatically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * arranged in descending order of @ack_seqno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) list_add(&avr->avr_node, &av->av_records);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) dccp_pr_debug("Added Vector, ack_seqno=%llu, ack_ackno=%llu (rl=%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) (unsigned long long)avr->avr_ack_seqno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) (unsigned long long)avr->avr_ack_ackno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) avr->avr_ack_runlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static struct dccp_ackvec_record *dccp_ackvec_lookup(struct list_head *av_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) const u64 ackno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct dccp_ackvec_record *avr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * Exploit that records are inserted in descending order of sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * number, start with the oldest record first. If @ackno is `before'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * the earliest ack_ackno, the packet is too old to be considered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) list_for_each_entry_reverse(avr, av_list, avr_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (avr->avr_ack_seqno == ackno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return avr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (before48(ackno, avr->avr_ack_seqno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * Buffer index and length computation using modulo-buffersize arithmetic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * Note that, as pointers move from right to left, head is `before' tail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static inline u16 __ackvec_idx_add(const u16 a, const u16 b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return (a + b) % DCCPAV_MAX_ACKVEC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static inline u16 __ackvec_idx_sub(const u16 a, const u16 b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return __ackvec_idx_add(a, DCCPAV_MAX_ACKVEC_LEN - b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) u16 dccp_ackvec_buflen(const struct dccp_ackvec *av)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (unlikely(av->av_overflow))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return DCCPAV_MAX_ACKVEC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return __ackvec_idx_sub(av->av_buf_tail, av->av_buf_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * dccp_ackvec_update_old - Update previous state as per RFC 4340, 11.4.1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * @av: non-empty buffer to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * @distance: negative or zero distance of @seqno from buf_ackno downward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * @seqno: the (old) sequence number whose record is to be updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * @state: state in which packet carrying @seqno was received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static void dccp_ackvec_update_old(struct dccp_ackvec *av, s64 distance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) u64 seqno, enum dccp_ackvec_states state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) u16 ptr = av->av_buf_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) BUG_ON(distance > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (unlikely(dccp_ackvec_is_empty(av)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) u8 runlen = dccp_ackvec_runlen(av->av_buf + ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (distance + runlen >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * Only update the state if packet has not been received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * yet. This is OK as per the second table in RFC 4340,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * 11.4.1; i.e. here we are using the following table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * RECEIVED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * 0 1 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * S +---+---+---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * T 0 | 0 | 0 | 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * O +---+---+---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * R 1 | 1 | 1 | 1 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * E +---+---+---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * D 3 | 0 | 1 | 3 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * +---+---+---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * The "Not Received" state was set by reserve_seats().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (av->av_buf[ptr] == DCCPAV_NOT_RECEIVED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) av->av_buf[ptr] = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) dccp_pr_debug("Not changing %llu state to %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) (unsigned long long)seqno, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) distance += runlen + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ptr = __ackvec_idx_add(ptr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) } while (ptr != av->av_buf_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /* Mark @num entries after buf_head as "Not yet received". */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static void dccp_ackvec_reserve_seats(struct dccp_ackvec *av, u16 num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) u16 start = __ackvec_idx_add(av->av_buf_head, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) len = DCCPAV_MAX_ACKVEC_LEN - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* check for buffer wrap-around */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (num > len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) memset(av->av_buf + start, DCCPAV_NOT_RECEIVED, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) num -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) memset(av->av_buf + start, DCCPAV_NOT_RECEIVED, num);
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * dccp_ackvec_add_new - Record one or more new entries in Ack Vector buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * @av: container of buffer to update (can be empty or non-empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * @num_packets: number of packets to register (must be >= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * @seqno: sequence number of the first packet in @num_packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * @state: state in which packet carrying @seqno was received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static void dccp_ackvec_add_new(struct dccp_ackvec *av, u32 num_packets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) u64 seqno, enum dccp_ackvec_states state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) u32 num_cells = num_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (num_packets > DCCPAV_BURST_THRESH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) u32 lost_packets = num_packets - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) DCCP_WARN("Warning: large burst loss (%u)\n", lost_packets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * We received 1 packet and have a loss of size "num_packets-1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * which we squeeze into num_cells-1 rather than reserving an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * entire byte for each lost packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * The reason is that the vector grows in O(burst_length); when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * it grows too large there will no room left for the payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * This is a trade-off: if a few packets out of the burst show
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * up later, their state will not be changed; it is simply too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * costly to reshuffle/reallocate/copy the buffer each time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * Should such problems persist, we will need to switch to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * different underlying data structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) for (num_packets = num_cells = 1; lost_packets; ++num_cells) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) u8 len = min_t(u32, lost_packets, DCCPAV_MAX_RUNLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) av->av_buf_head = __ackvec_idx_sub(av->av_buf_head, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) av->av_buf[av->av_buf_head] = DCCPAV_NOT_RECEIVED | len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) lost_packets -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (num_cells + dccp_ackvec_buflen(av) >= DCCPAV_MAX_ACKVEC_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) DCCP_CRIT("Ack Vector buffer overflow: dropping old entries");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) av->av_overflow = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) av->av_buf_head = __ackvec_idx_sub(av->av_buf_head, num_packets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (av->av_overflow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) av->av_buf_tail = av->av_buf_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) av->av_buf[av->av_buf_head] = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) av->av_buf_ackno = seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (num_packets > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) dccp_ackvec_reserve_seats(av, num_packets - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * dccp_ackvec_input - Register incoming packet in the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) void dccp_ackvec_input(struct dccp_ackvec *av, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) u64 seqno = DCCP_SKB_CB(skb)->dccpd_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) enum dccp_ackvec_states state = DCCPAV_RECEIVED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (dccp_ackvec_is_empty(av)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) dccp_ackvec_add_new(av, 1, seqno, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) av->av_tail_ackno = seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) s64 num_packets = dccp_delta_seqno(av->av_buf_ackno, seqno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) u8 *current_head = av->av_buf + av->av_buf_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (num_packets == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) dccp_ackvec_state(current_head) == state &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) dccp_ackvec_runlen(current_head) < DCCPAV_MAX_RUNLEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) *current_head += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) av->av_buf_ackno = seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) } else if (num_packets > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) dccp_ackvec_add_new(av, num_packets, seqno, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) dccp_ackvec_update_old(av, num_packets, seqno, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * dccp_ackvec_clear_state - Perform house-keeping / garbage-collection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * This routine is called when the peer acknowledges the receipt of Ack Vectors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * up to and including @ackno. While based on section A.3 of RFC 4340, here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * are additional precautions to prevent corrupted buffer state. In particular,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * we use tail_ackno to identify outdated records; it always marks the earliest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * packet of group (2) in 11.4.2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) void dccp_ackvec_clear_state(struct dccp_ackvec *av, const u64 ackno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct dccp_ackvec_record *avr, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) u8 runlen_now, eff_runlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) s64 delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) avr = dccp_ackvec_lookup(&av->av_records, ackno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (avr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * Deal with outdated acknowledgments: this arises when e.g. there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * several old records and the acks from the peer come in slowly. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * that case we may still have records that pre-date tail_ackno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) delta = dccp_delta_seqno(av->av_tail_ackno, avr->avr_ack_ackno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (delta < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) goto free_records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * Deal with overlapping Ack Vectors: don't subtract more than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * number of packets between tail_ackno and ack_ackno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) eff_runlen = delta < avr->avr_ack_runlen ? delta : avr->avr_ack_runlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) runlen_now = dccp_ackvec_runlen(av->av_buf + avr->avr_ack_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * The run length of Ack Vector cells does not decrease over time. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * the run length is the same as at the time the Ack Vector was sent, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * free the ack_ptr cell. That cell can however not be freed if the run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * length has increased: in this case we need to move the tail pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * backwards (towards higher indices), to its next-oldest neighbour.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (runlen_now > eff_runlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) av->av_buf[avr->avr_ack_ptr] -= eff_runlen + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) av->av_buf_tail = __ackvec_idx_add(avr->avr_ack_ptr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /* This move may not have cleared the overflow flag. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (av->av_overflow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) av->av_overflow = (av->av_buf_head == av->av_buf_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) av->av_buf_tail = avr->avr_ack_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * We have made sure that avr points to a valid cell within the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * buffer. This cell is either older than head, or equals head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * (empty buffer): in both cases we no longer have any overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) av->av_overflow = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * The peer has acknowledged up to and including ack_ackno. Hence the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * first packet in group (2) of 11.4.2 is the successor of ack_ackno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) av->av_tail_ackno = ADD48(avr->avr_ack_ackno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) free_records:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) list_for_each_entry_safe_from(avr, next, &av->av_records, avr_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) list_del(&avr->avr_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) kmem_cache_free(dccp_ackvec_record_slab, avr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * Routines to keep track of Ack Vectors received in an skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) int dccp_ackvec_parsed_add(struct list_head *head, u8 *vec, u8 len, u8 nonce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct dccp_ackvec_parsed *new = kmalloc(sizeof(*new), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (new == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) new->vec = vec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) new->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) new->nonce = nonce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) list_add_tail(&new->node, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) EXPORT_SYMBOL_GPL(dccp_ackvec_parsed_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) void dccp_ackvec_parsed_cleanup(struct list_head *parsed_chunks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct dccp_ackvec_parsed *cur, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) list_for_each_entry_safe(cur, next, parsed_chunks, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) kfree(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) INIT_LIST_HEAD(parsed_chunks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) EXPORT_SYMBOL_GPL(dccp_ackvec_parsed_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) int __init dccp_ackvec_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) dccp_ackvec_slab = kmem_cache_create("dccp_ackvec",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) sizeof(struct dccp_ackvec), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) SLAB_HWCACHE_ALIGN, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (dccp_ackvec_slab == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) dccp_ackvec_record_slab = kmem_cache_create("dccp_ackvec_record",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) sizeof(struct dccp_ackvec_record),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 0, SLAB_HWCACHE_ALIGN, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (dccp_ackvec_record_slab == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) goto out_destroy_slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) out_destroy_slab:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) kmem_cache_destroy(dccp_ackvec_slab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) dccp_ackvec_slab = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) DCCP_CRIT("Unable to create Ack Vector slab cache");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) void dccp_ackvec_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) kmem_cache_destroy(dccp_ackvec_slab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) dccp_ackvec_slab = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) kmem_cache_destroy(dccp_ackvec_record_slab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) dccp_ackvec_record_slab = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }