-
-
Notifications
You must be signed in to change notification settings - Fork 537
Expand file tree
/
Copy pathpacket_builder.rs
More file actions
264 lines (244 loc) · 9.67 KB
/
packet_builder.rs
File metadata and controls
264 lines (244 loc) · 9.67 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use bytes::{BufMut, Bytes};
use rand::Rng;
use tracing::{trace, trace_span};
use super::{Connection, DatagramBuffer, SentFrames, spaces::SentPacket};
use crate::{
ConnectionId, Instant, TransportError, TransportErrorCode,
connection::ConnectionSide,
frame::{self, Close},
packet::{FIXED_BIT, Header, InitialHeader, LongType, PacketNumber, PartialEncode, SpaceId},
};
pub(super) struct PacketBuilder {
pub(super) space: SpaceId,
pub(super) partial_encode: PartialEncode,
pub(super) ack_eliciting: bool,
pub(super) exact_number: u64,
pub(super) short_header: bool,
/// The smallest datagram offset that must be occupied by this packet's frames
///
/// This is the smallest offset into the datagram this packet is being written into,
/// that must contain frames for this packet.
pub(super) min_size: usize,
/// The largest datagram offset that may be occupied by this packet's frames
pub(super) max_size: usize,
pub(super) tag_len: usize,
pub(super) _span: tracing::span::EnteredSpan,
}
impl PacketBuilder {
/// Write a new packet header to `buffer` and determine the packet's properties
///
/// Marks the connection drained and returns `None` if the confidentiality limit would be
/// violated.
pub(super) fn new(
now: Instant,
space_id: SpaceId,
dst_cid: ConnectionId,
datagram: &mut DatagramBuffer<'_>,
ack_eliciting: bool,
conn: &mut Connection,
) -> Option<Self> {
let version = conn.version;
// Initiate key update if we're approaching the confidentiality limit
let sent_with_keys = conn.spaces[space_id].sent_with_keys;
if space_id == SpaceId::Data {
if sent_with_keys >= conn.key_phase_size {
conn.force_key_update();
}
} else {
let confidentiality_limit = conn.spaces[space_id]
.crypto
.as_ref()
.map_or_else(
|| &conn.zero_rtt_crypto.as_ref().unwrap().packet,
|keys| &keys.packet.local,
)
.confidentiality_limit();
if sent_with_keys.saturating_add(1) == confidentiality_limit {
// We still have time to attempt a graceful close
conn.close_inner(
now,
Close::Connection(frame::ConnectionClose {
error_code: TransportErrorCode::AEAD_LIMIT_REACHED,
frame_type: None,
reason: Bytes::from_static(b"confidentiality limit reached"),
}),
)
} else if sent_with_keys > confidentiality_limit {
// Confidentiality limited violated and there's nothing we can do
conn.kill(
TransportError::AEAD_LIMIT_REACHED("confidentiality limit reached").into(),
);
return None;
}
}
let space = &mut conn.spaces[space_id];
let exact_number = match space_id {
SpaceId::Data => conn.packet_number_filter.allocate(&mut conn.rng, space),
_ => space.get_tx_number(),
};
let span = trace_span!("send", space = ?space_id, pn = exact_number).entered();
let number = PacketNumber::new(exact_number, space.largest_acked_packet.unwrap_or(0));
let header = match space_id {
SpaceId::Data if space.crypto.is_some() => Header::Short {
dst_cid,
number,
spin: if conn.spin_enabled {
conn.spin
} else {
conn.rng.random()
},
key_phase: conn.key_phase,
},
SpaceId::Data => Header::Long {
ty: LongType::ZeroRtt,
src_cid: conn.handshake_cid,
dst_cid,
number,
version,
},
SpaceId::Handshake => Header::Long {
ty: LongType::Handshake,
src_cid: conn.handshake_cid,
dst_cid,
number,
version,
},
SpaceId::Initial => Header::Initial(InitialHeader {
src_cid: conn.handshake_cid,
dst_cid,
token: match &conn.side {
ConnectionSide::Client { token, .. } => token.clone(),
ConnectionSide::Server { .. } => Bytes::new(),
},
number,
version,
}),
};
let partial_encode = header.encode(datagram);
if conn.peer_params.grease_quic_bit && conn.rng.random() {
datagram[partial_encode.start] ^= FIXED_BIT;
}
let (sample_size, tag_len) = if let Some(ref crypto) = space.crypto {
(
crypto.header.local.sample_size(),
crypto.packet.local.tag_len(),
)
} else if space_id == SpaceId::Data {
let zero_rtt = conn.zero_rtt_crypto.as_ref().unwrap();
(zero_rtt.header.sample_size(), zero_rtt.packet.tag_len())
} else {
unreachable!();
};
// Each packet must be large enough for header protection sampling, i.e. the combined
// lengths of the encoded packet number and protected payload must be at least 4 bytes
// longer than the sample required for header protection. Further, each packet should be at
// least tag_len + 6 bytes larger than the destination CID on incoming packets so that the
// peer may send stateless resets that are indistinguishable from regular traffic.
// pn_len + payload_len + tag_len >= sample_size + 4
// payload_len >= sample_size + 4 - pn_len - tag_len
let min_size = Ord::max(
datagram.len() + (sample_size + 4).saturating_sub(number.len() + tag_len),
partial_encode.start + dst_cid.len() + 6,
);
let max_size = datagram.capacity() - tag_len;
debug_assert!(max_size >= min_size);
Some(Self {
space: space_id,
partial_encode,
exact_number,
short_header: header.is_short(),
min_size,
max_size,
tag_len,
ack_eliciting,
_span: span,
})
}
/// Append the minimum amount of padding to the packet such that, after encryption, the
/// enclosing datagram will occupy at least `min_size` bytes
pub(super) fn pad_to(&mut self, min_size: u16) {
// The datagram might already have a larger minimum size than the caller is requesting, if
// e.g. we're coalescing packets and have populated more than `min_size` bytes with packets
// already.
self.min_size = Ord::max(self.min_size, (min_size as usize) - self.tag_len);
}
pub(super) fn finish_and_track(
self,
now: Instant,
conn: &mut Connection,
sent: Option<SentFrames>,
datagram: &mut DatagramBuffer<'_>,
) {
let ack_eliciting = self.ack_eliciting;
let exact_number = self.exact_number;
let space_id = self.space;
let (size, padded) = self.finish(conn, datagram);
let sent = match sent {
Some(sent) => sent,
None => return,
};
let size = match padded || ack_eliciting {
true => size as u16,
false => 0,
};
let packet = SentPacket {
largest_acked: sent.largest_acked,
time_sent: now,
size,
ack_eliciting,
retransmits: sent.retransmits,
stream_frames: sent.stream_frames,
};
conn.path
.sent(exact_number, packet, &mut conn.spaces[space_id]);
conn.stats.path.sent_packets += 1;
conn.reset_keep_alive(now);
if size != 0 {
if ack_eliciting {
conn.spaces[space_id].time_of_last_ack_eliciting_packet = Some(now);
if conn.permit_idle_reset {
conn.reset_idle_timeout(now, space_id);
}
conn.permit_idle_reset = false;
}
conn.set_loss_detection_timer(now);
conn.path.pacing.on_transmit(size);
}
}
/// Encrypt packet, returning the length of the packet and whether padding was added
pub(super) fn finish(
self,
conn: &mut Connection,
datagram: &mut DatagramBuffer<'_>,
) -> (usize, bool) {
let pad = self.min_size > datagram.len();
if pad {
let padding_bytes = self.min_size - datagram.len();
trace!("PADDING * {padding_bytes}");
datagram.put_bytes(0, padding_bytes);
}
let space = &conn.spaces[self.space];
let (header_crypto, packet_crypto) = if let Some(ref crypto) = space.crypto {
(&*crypto.header.local, &*crypto.packet.local)
} else if self.space == SpaceId::Data {
let zero_rtt = conn.zero_rtt_crypto.as_ref().unwrap();
(&*zero_rtt.header, &*zero_rtt.packet)
} else {
unreachable!("tried to send {:?} packet without keys", self.space);
};
debug_assert_eq!(
packet_crypto.tag_len(),
self.tag_len,
"Mismatching crypto tag len"
);
datagram.put_bytes(0, packet_crypto.tag_len());
let encode_start = self.partial_encode.start;
let packet_buf = &mut datagram[encode_start..];
self.partial_encode.finish(
packet_buf,
header_crypto,
Some((self.exact_number, packet_crypto)),
);
(datagram.len() - encode_start, pad)
}
}