pub(crate) mod block {
pub const STATUS: usize = 8;
pub const NUM_PKTS: usize = 12;
pub const FIRST: usize = 16;
pub const LEN: usize = 48;
}
pub(crate) mod frame {
pub const NEXT: usize = 0;
pub const SEC: usize = 4;
pub const NSEC: usize = 8;
pub const SNAPLEN: usize = 12;
pub const LEN: usize = 16;
pub const STATUS: usize = 20;
pub const MAC: usize = 24;
pub const VLAN_TCI: usize = 32;
pub const VLAN_TPID: usize = 36;
pub const HDR: usize = 48;
}
pub(crate) mod status {
pub const USER: u32 = 1;
pub const VLAN_VALID: u32 = 1 << 4;
pub const VLAN_TPID_VALID: u32 = 1 << 6;
#[cfg_attr(not(test), allow(dead_code))]
pub const TS_SOFTWARE: u32 = 1 << 29;
pub const TS_RAW_HARDWARE: u32 = 1 << 31;
}
fn u32_at(b: &[u8], at: usize) -> Option<u32> {
let s = b.get(at..at.checked_add(4)?)?;
Some(u32::from_ne_bytes([s[0], s[1], s[2], s[3]]))
}
fn u16_at(b: &[u8], at: usize) -> Option<u16> {
let s = b.get(at..at.checked_add(2)?)?;
Some(u16::from_ne_bytes([s[0], s[1]]))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct BlockHeader {
pub status: u32,
pub num_pkts: u32,
pub first: usize,
}
impl BlockHeader {
pub(crate) fn read(block: &[u8]) -> Option<BlockHeader> {
if block.len() < block::LEN {
return None;
}
Some(BlockHeader {
status: u32_at(block, block::STATUS)?,
num_pkts: u32_at(block, block::NUM_PKTS)?,
first: u32_at(block, block::FIRST)? as usize,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct FrameMeta {
pub next: usize,
pub sec: u32,
pub nsec: u32,
pub status: u32,
pub mac: usize,
pub snaplen: usize,
pub len: usize,
pub vlan_tci: u16,
pub vlan_tpid: u16,
}
impl FrameMeta {
pub(crate) fn read(block: &[u8], at: usize) -> Option<FrameMeta> {
if at.checked_add(frame::HDR)? > block.len() {
return None;
}
let m = FrameMeta {
next: u32_at(block, at + frame::NEXT)? as usize,
sec: u32_at(block, at + frame::SEC)?,
nsec: u32_at(block, at + frame::NSEC)?,
snaplen: u32_at(block, at + frame::SNAPLEN)? as usize,
len: u32_at(block, at + frame::LEN)? as usize,
status: u32_at(block, at + frame::STATUS)?,
mac: usize::from(u16_at(block, at + frame::MAC)?),
vlan_tci: u32_at(block, at + frame::VLAN_TCI)? as u16,
vlan_tpid: u16_at(block, at + frame::VLAN_TPID)?,
};
let start = at.checked_add(m.mac)?;
if m.mac < frame::HDR || start.checked_add(m.snaplen)? > block.len() {
return None;
}
Some(m)
}
pub(crate) fn stripped_vlan(&self) -> Option<(u16, u16)> {
if self.status & status::VLAN_VALID == 0 {
return None;
}
let tpid = if self.status & status::VLAN_TPID_VALID != 0 && self.vlan_tpid != 0 {
self.vlan_tpid
} else {
0x8100
};
Some((tpid, self.vlan_tci))
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Cursor {
pub remaining: u32,
pub at: usize,
}
impl Cursor {
pub(crate) fn start(block: &[u8]) -> Option<Cursor> {
let h = BlockHeader::read(block)?;
if h.status & status::USER == 0 {
return None;
}
Some(Cursor { remaining: h.num_pkts, at: h.first })
}
pub(crate) fn next(&mut self, block: &[u8]) -> Option<(usize, FrameMeta)> {
if self.remaining == 0 {
return None;
}
let Some(meta) = FrameMeta::read(block, self.at) else {
self.remaining = 0;
return None;
};
let here = self.at;
self.remaining -= 1;
if self.remaining > 0 {
if meta.next == 0 {
self.remaining = 0;
} else {
self.at = here.saturating_add(meta.next);
}
}
Some((here, meta))
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
pub(crate) type FrameSpec<'a> = (&'a [u8], u32, Option<(u16, u16)>);
pub(crate) fn synthetic_block(size: usize, frames: &[FrameSpec<'_>]) -> Vec<u8> {
let mut b = vec![0u8; size];
let put32 = |b: &mut [u8], at: usize, v: u32| b[at..at + 4].copy_from_slice(&v.to_ne_bytes());
put32(&mut b, block::STATUS, status::USER);
put32(&mut b, block::NUM_PKTS, frames.len() as u32);
let mut at = 64usize;
put32(&mut b, block::FIRST, at as u32);
for (n, (data, ts_status, vlan)) in frames.iter().enumerate() {
let mac = 80usize; let total = (mac + data.len() + 15) & !15;
let next = if n + 1 == frames.len() { 0 } else { total };
put32(&mut b, at + frame::NEXT, next as u32);
put32(&mut b, at + frame::SEC, 1_700_000_000 + n as u32);
put32(&mut b, at + frame::NSEC, 42);
put32(&mut b, at + frame::SNAPLEN, data.len() as u32);
put32(&mut b, at + frame::LEN, data.len() as u32);
let mut st = *ts_status;
if let Some((tpid, tci)) = vlan {
st |= status::VLAN_VALID | status::VLAN_TPID_VALID;
put32(&mut b, at + frame::VLAN_TCI, u32::from(*tci));
b[at + frame::VLAN_TPID..at + frame::VLAN_TPID + 2].copy_from_slice(&tpid.to_ne_bytes());
}
put32(&mut b, at + frame::STATUS, st);
b[at + frame::MAC..at + frame::MAC + 2].copy_from_slice(&(mac as u16).to_ne_bytes());
b[at + mac..at + mac + data.len()].copy_from_slice(data);
at += total;
}
b
}
#[test]
fn a_block_is_walked_frame_by_frame() {
let a = [1u8; 60];
let c = [3u8; 100];
let blk = synthetic_block(
4096,
&[(&a, status::TS_SOFTWARE, None), (&c, status::TS_RAW_HARDWARE, Some((0x8100, 5)))],
);
let mut cur = Cursor::start(&blk).expect("user block");
let (at, m) = cur.next(&blk).expect("first");
assert_eq!(&blk[at + m.mac..at + m.mac + m.snaplen], &a[..]);
assert_eq!(m.stripped_vlan(), None);
let (at, m) = cur.next(&blk).expect("second");
assert_eq!(&blk[at + m.mac..at + m.mac + m.snaplen], &c[..]);
assert_eq!(m.stripped_vlan(), Some((0x8100, 5)));
assert!(m.status & status::TS_RAW_HARDWARE != 0);
assert!(cur.next(&blk).is_none());
}
#[test]
fn a_block_still_owned_by_the_kernel_is_not_read() {
let mut blk = synthetic_block(4096, &[(&[0u8; 60], 0, None)]);
blk[block::STATUS..block::STATUS + 4].copy_from_slice(&0u32.to_ne_bytes());
assert!(Cursor::start(&blk).is_none());
}
#[test]
fn offsets_that_point_outside_the_block_end_the_walk() {
let mut blk = synthetic_block(4096, &[(&[0u8; 60], 0, None), (&[0u8; 60], 0, None)]);
blk[64 + frame::SNAPLEN..64 + frame::SNAPLEN + 4].copy_from_slice(&100_000u32.to_ne_bytes());
let mut cur = Cursor::start(&blk).expect("user block");
assert!(cur.next(&blk).is_none());
assert!(cur.next(&blk).is_none(), "the rest of the block is not trusted");
let mut blk = synthetic_block(256, &[(&[0u8; 60], 0, None)]);
blk[block::FIRST..block::FIRST + 4].copy_from_slice(&250u32.to_ne_bytes());
let mut cur = Cursor::start(&blk).expect("user block");
assert!(cur.next(&blk).is_none());
assert!(Cursor::start(&blk[..20]).is_none(), "a block shorter than its header");
}
#[test]
fn a_frame_whose_data_overlaps_its_header_is_refused() {
let mut blk = synthetic_block(4096, &[(&[0u8; 60], 0, None)]);
blk[64 + frame::MAC..64 + frame::MAC + 2].copy_from_slice(&8u16.to_ne_bytes());
assert!(FrameMeta::read(&blk, 64).is_none());
}
}