1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Bundling of control chunks (RFC 9260 sections 6.10 and 12.4): what answers one received
//! packet leaves in one packet, a SACK that is due travels with it or with outbound DATA, and
//! so does a FORWARD TSN (RFC 3758 3.5 F2).
use super::*;
use crate::chunk::chunk_type::{
CT_COOKIE_ACK, CT_COOKIE_ECHO, CT_ERROR, CT_HEARTBEAT, CT_HEARTBEAT_ACK, CT_I_DATA,
CT_PAYLOAD_DATA, CT_SACK,
};
use crate::util::get_padding_size;
/// Room kept in the packet that answers a received packet for the SACK that goes with it:
/// the chunk without Gap Ack Blocks, and a few duplicate TSNs.
const SACK_RESERVE: usize = CHUNK_HEADER_SIZE + SELECTIVE_ACK_HEADER_SIZE + 4 * 8;
fn chunk_wire_size(c: &dyn Chunk) -> usize {
let len = CHUNK_HEADER_SIZE + c.value_length();
len + get_padding_size(len)
}
fn packet_wire_size(p: &Packet) -> usize {
COMMON_HEADER_SIZE as usize
+ p.chunks
.iter()
.map(|c| chunk_wire_size(c.as_ref()))
.sum::<usize>()
}
impl Association {
/// Whether `p` may share a packet with other control chunks: it goes to the peer with
/// the association's tag, and holds none of the chunks that travel alone (INIT, INIT
/// ACK and SHUTDOWN COMPLETE by RFC 9260 6.10; ABORT, COOKIE ECHO and the SHUTDOWN
/// chunks by choice, each being the one thing its packet is for).
fn is_bundle(&self, p: &Packet) -> bool {
p.common_header.verification_tag == self.peer_verification_tag
&& self.peer_verification_tag != 0
&& p.common_header.source_port == self.source_port
&& p.common_header.destination_port == self.destination_port
&& !p.chunks.is_empty()
&& p.chunks.iter().all(|c| {
let typ = c.header().typ;
[
CT_SACK,
CT_HEARTBEAT,
CT_HEARTBEAT_ACK,
CT_ERROR,
CT_COOKIE_ACK,
CT_RECONFIG,
CT_FORWARD_TSN,
CT_I_FORWARD_TSN,
]
.contains(&typ)
})
}
/// RFC 9260 5.1 C, 3.3.11 (`TransportConfig::with_data_in_cookie_echo`): in COOKIE-ECHOED
/// the packet of a COOKIE ECHO, which stays its first chunk, carries the DATA that fits the
/// MTU after it: what went with an earlier COOKIE ECHO again first, then new DATA. No other
/// packet carries DATA until the COOKIE ACK.
pub(super) fn bundle_data_with_cookie_echo(
&mut self,
mut packets: Vec<Packet>,
now: Instant,
) -> Vec<Packet> {
if self.state() != AssociationState::CookieEchoed || !self.data_in_cookie_echo() {
return packets;
}
for p in &mut packets {
if p.chunks
.first()
.is_none_or(|c| c.header().typ != CT_COOKIE_ECHO)
{
continue;
}
let size = packet_wire_size(p);
let mut data = match self.outstanding_data_that_fits(now, size) {
Ok(data) => data,
Err(err) => {
warn!("[{}] no DATA with the COOKIE ECHO: {err}", self.side);
continue;
}
};
let used = size + data.iter().map(|c| c.wire_size()).sum::<usize>();
if data.is_empty() || used < self.mtu as usize {
let room = (self.mtu as usize).saturating_sub(used);
data.extend(self.pending_data_for_cookie_echo(room, now));
}
if !data.is_empty() {
debug!(
"[{}] COOKIE ECHO with {} DATA chunk(s)",
self.side,
data.len()
);
}
for c in data {
p.chunks.push(Box::new(c));
}
}
packets
}
/// RFC 9260 6.10: control packets that follow one another become one packet while they
/// fit the MTU. `sack`, when one is due, joins the last of them if it fits and is taken;
/// otherwise it is left for `gather_outbound_sack_packets`.
pub(super) fn bundle_control_packets(
&self,
packets: Vec<Packet>,
sack: &mut Option<ChunkSelectiveAck>,
) -> Vec<Packet> {
let mtu = self.mtu as usize;
let mut out: Vec<Packet> = Vec::with_capacity(packets.len());
for p in packets {
if self.is_bundle(&p)
&& let Some(last) = out.last_mut()
&& self.is_bundle(last)
&& packet_wire_size(last) + packet_wire_size(&p) - COMMON_HEADER_SIZE as usize
<= mtu
{
last.chunks.extend(p.chunks);
} else {
out.push(p);
}
}
if let Some(chunk) = sack.take() {
match out.last_mut() {
Some(last)
if self.is_bundle(last)
&& packet_wire_size(last) + chunk_wire_size(&chunk) <= mtu =>
{
last.chunks.push(Box::new(chunk));
}
_ => *sack = Some(chunk),
}
}
out
}
/// RFC 9260 12.4: "an SCTP receiver MUST NOT respond with more than one packet containing
/// chunks other than DATA chunks" to one received packet. The packets queued since the
/// control queue held `before` of them are the answers to the packet just processed:
/// they become one packet, and the chunks that do not fit it are not sent (the sender
/// of two hundred HEARTBEATs in a packet gets as many HEARTBEAT ACKs as fit one).
pub(super) fn bundle_replies(&mut self, before: usize) {
if self.control_queue.len() <= before + 1 {
return;
}
let replies: Vec<Packet> = self.control_queue.drain(before..).collect();
// An ABORT is the whole answer.
if let Some(abort) = replies
.iter()
.position(|p| p.chunks.iter().any(|c| c.as_any().is::<ChunkAbort>()))
{
self.control_queue
.extend(replies.into_iter().skip(abort).take(1));
return;
}
let room = (self.mtu as usize).saturating_sub(if self.immediate_ack_triggered {
SACK_RESERVE
} else {
0
});
let mut bundle: Option<Packet> = None;
let mut alone: Option<Packet> = None;
for p in replies {
if !self.is_bundle(&p) {
// A chunk that travels alone (an INIT ACK, a SHUTDOWN ACK): it is the answer.
alone.get_or_insert(p);
continue;
}
match bundle.as_mut() {
None => bundle = Some(p),
Some(b) => {
let mut size = packet_wire_size(b);
for c in p.chunks {
let wire = chunk_wire_size(c.as_ref());
if size + wire <= room {
size += wire;
b.chunks.push(c);
} else {
debug!(
"[{}] a reply chunk does not fit the one packet of answers",
self.side
);
}
}
}
}
}
match (alone, bundle) {
(Some(alone), _) => self.control_queue.push_back(alone),
(None, Some(bundle)) => self.control_queue.push_back(bundle),
(None, None) => {}
}
}
/// Puts a control chunk into the first packet of DATA gathered so far that has room for
/// it, ahead of the DATA chunks (RFC 9260 6.10: control chunks come first). `false` when
/// no packet has room: the chunk then travels in a packet of its own.
pub(super) fn bundle_with_data(
&self,
chunk: &dyn Chunk,
raw_packets: &mut [Bytes],
first_data_packet: usize,
) -> bool {
let wire = chunk_wire_size(chunk);
let header = COMMON_HEADER_SIZE as usize;
let Some(slot) = raw_packets.iter_mut().skip(first_data_packet).find(|raw| {
raw.len() > header
&& (raw[header] == CT_PAYLOAD_DATA.0 || raw[header] == CT_I_DATA.0)
&& raw.len() + wire <= self.mtu as usize
}) else {
return false;
};
let common_header = CommonHeader {
verification_tag: self.peer_verification_tag,
source_port: self.source_port,
destination_port: self.destination_port,
};
let mut buf = BytesMut::with_capacity(slot.len() + wire);
if Packet::write_framed_with(&common_header, std::iter::once(chunk), &mut buf, true)
.is_err()
{
return false;
}
buf.extend_from_slice(&slot[header..]);
if !self.send_zero_checksum {
// The checksum covers the packet with its checksum field zero.
let checksum = crc32c::crc32c(&buf);
buf[8..12].copy_from_slice(&checksum.to_le_bytes());
}
*slot = buf.freeze();
true
}
}