retina_core/protocols/stream/quic/frame.rs
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
// QUIC Frame types and parsing
// Implemented per RFC 9000: https://datatracker.ietf.org/doc/html/rfc9000#name-frame-types-and-formats
use serde::Serialize;
use std::collections::BTreeMap;
use crate::protocols::stream::quic::QuicError;
use crate::protocols::stream::quic::QuicPacket;
// Types of supported QUIC frames
// Currently only includes those seen in the Init and Handshake packets
#[derive(Debug, Serialize, Clone)]
pub enum QuicFrame {
Padding {
length: usize,
},
Ping,
Ack {
largest_acknowledged: u64,
ack_delay: u64,
first_ack_range: u64,
ack_ranges: Vec<AckRange>,
ecn_counts: Option<EcnCounts>,
},
Crypto {
offset: u64,
},
}
// ACK Range field, part of ACK frame
// https://datatracker.ietf.org/doc/html/rfc9000#ack-range-format
#[derive(Debug, Serialize, Clone)]
pub struct AckRange {
gap: u64,
ack_range_len: u64,
}
// ECN Counts field, part of some ACK frames
// https://datatracker.ietf.org/doc/html/rfc9000#ecn-count-format
#[derive(Debug, Serialize, Clone)]
pub struct EcnCounts {
ect0_count: u64,
ect1_count: u64,
ecn_ce_count: u64,
}
impl QuicFrame {
// parse_frames takes the plaintext QUIC packet payload and parses the frame list
// it also returns the reassembled CRYPTO frame bytes as a Vec<u8>
pub fn parse_frames(
data: &[u8],
mut expected_offset: usize,
) -> Result<(Vec<QuicFrame>, Vec<u8>), QuicError> {
let mut frames: Vec<QuicFrame> = Vec::new();
let mut crypto_map: BTreeMap<usize, Vec<u8>> = BTreeMap::new();
let mut offset = 0;
// Iterate over plaintext payload bytes, this is a list of frames
while offset < data.len() {
// Parse frame type
let frame_type_len =
QuicPacket::get_var_len(QuicPacket::access_data(data, offset, offset + 1)?[0])?;
let frame_type = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + frame_type_len,
)?)?;
offset += frame_type_len;
match frame_type {
0x00 => {
// Handle PADDING
let mut length = 0;
while offset + length + 1 < data.len()
&& QuicPacket::access_data(data, offset + length, offset + length + 1)?[0]
== 0
{
length += 1;
}
offset += length;
length += frame_type_len; // Add the original frame type bytes to length. Wireshark also does this
frames.push(QuicFrame::Padding { length });
}
0x01 => {
// Handle PING
frames.push(QuicFrame::Ping);
}
0x02 | 0x03 => {
// Handle ACK
// Parse Largest Acknowledged
let largest_acknowledged_len = QuicPacket::get_var_len(
QuicPacket::access_data(data, offset, offset + 1)?[0],
)?;
let largest_acknowledged = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + largest_acknowledged_len,
)?)?;
offset += largest_acknowledged_len;
// Parse ACK Delay
let ack_delay_len = QuicPacket::get_var_len(
QuicPacket::access_data(data, offset, offset + 1)?[0],
)?;
let ack_delay = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + ack_delay_len,
)?)?;
offset += ack_delay_len;
// Parse ACK Range Count
let ack_range_count_len = QuicPacket::get_var_len(
QuicPacket::access_data(data, offset, offset + 1)?[0],
)?;
let ack_range_count = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + ack_range_count_len,
)?)?;
offset += ack_range_count_len;
// Parse First ACK Range
let first_ack_range_len = QuicPacket::get_var_len(
QuicPacket::access_data(data, offset, offset + 1)?[0],
)?;
let first_ack_range = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + first_ack_range_len,
)?)?;
offset += first_ack_range_len;
// Parse ACK Range list field
let mut ack_ranges = Vec::new();
for _ in 0..ack_range_count {
let gap_len = QuicPacket::get_var_len(
QuicPacket::access_data(data, offset, offset + 1)?[0],
)?;
let gap = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + gap_len,
)?)?;
offset += gap_len;
let ack_range_len_len = QuicPacket::get_var_len(
QuicPacket::access_data(data, offset, offset + 1)?[0],
)?;
let ack_range_len = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + ack_range_len_len,
)?)?;
offset += ack_range_len_len;
ack_ranges.push(AckRange { gap, ack_range_len })
}
// Parse ECN Counts, if the ACK frame contains them
let ecn_counts: Option<EcnCounts> = if frame_type == 0x03 {
let ect0_count_len = QuicPacket::get_var_len(
QuicPacket::access_data(data, offset, offset + 1)?[0],
)?;
let ect0_count = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + ect0_count_len,
)?)?;
offset += ect0_count_len;
let ect1_count_len = QuicPacket::get_var_len(
QuicPacket::access_data(data, offset, offset + 1)?[0],
)?;
let ect1_count = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + ect1_count_len,
)?)?;
offset += ect1_count_len;
let ecn_ce_count_len = QuicPacket::get_var_len(
QuicPacket::access_data(data, offset, offset + 1)?[0],
)?;
let ecn_ce_count = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + ecn_ce_count_len,
)?)?;
Some(EcnCounts {
ect0_count,
ect1_count,
ecn_ce_count,
})
} else {
None
};
frames.push(QuicFrame::Ack {
largest_acknowledged,
ack_delay,
first_ack_range,
ack_ranges,
ecn_counts,
})
}
0x06 => {
// Handle CRYPTO frame
// Parse offset
let crypto_offset_len = QuicPacket::get_var_len(
QuicPacket::access_data(data, offset, offset + 1)?[0],
)?;
let crypto_offset = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + crypto_offset_len,
)?)?;
offset += crypto_offset_len;
// Parse length
let crypto_len_len = QuicPacket::get_var_len(
QuicPacket::access_data(data, offset, offset + 1)?[0],
)?;
let crypto_len = QuicPacket::slice_to_u64(QuicPacket::access_data(
data,
offset,
offset + crypto_len_len,
)?)? as usize;
offset += crypto_len_len;
// Parse data
let crypto_data =
QuicPacket::access_data(data, offset, offset + crypto_len)?.to_vec();
crypto_map
.entry(crypto_offset as usize)
.or_insert(crypto_data);
frames.push(QuicFrame::Crypto {
offset: crypto_offset,
});
offset += crypto_len;
}
_ => return Err(QuicError::UnknownFrameType),
}
}
let mut reassembled_crypto: Vec<u8> = Vec::new();
for (crypto_offset, crypto_data) in crypto_map {
if crypto_offset != expected_offset {
return Err(QuicError::MissingCryptoFrames);
}
expected_offset += crypto_data.len();
reassembled_crypto.extend(crypto_data);
}
Ok((frames, reassembled_crypto))
}
}